sim_object.hh revision 8320:c03e683e83fe
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 2001-2005 The Regents of The University of Michigan
36145Snate@binkert.org * Copyright (c) 2010 Advanced Micro Devices, Inc.
46145Snate@binkert.org * All rights reserved.
56145Snate@binkert.org *
66145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
76145Snate@binkert.org * modification, are permitted provided that the following conditions are
86145Snate@binkert.org * met: redistributions of source code must retain the above copyright
96145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
106145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
116145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
126145Snate@binkert.org * documentation and/or other materials provided with the distribution;
136145Snate@binkert.org * neither the name of the copyright holders nor the names of its
146145Snate@binkert.org * contributors may be used to endorse or promote products derived from
156145Snate@binkert.org * this software without specific prior written permission.
166145Snate@binkert.org *
176145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286145Snate@binkert.org *
297054Snate@binkert.org * Authors: Steve Reinhardt
307054Snate@binkert.org *          Nathan Binkert
317054Snate@binkert.org */
327054Snate@binkert.org
337054Snate@binkert.org/* @file
346154Snate@binkert.org * User Console Definitions
356154Snate@binkert.org */
366145Snate@binkert.org
377055Snate@binkert.org#ifndef __SIM_OBJECT_HH__
387055Snate@binkert.org#define __SIM_OBJECT_HH__
396145Snate@binkert.org
406145Snate@binkert.org#include <iostream>
417054Snate@binkert.org#include <list>
427054Snate@binkert.org#include <map>
437054Snate@binkert.org#include <string>
446145Snate@binkert.org#include <vector>
456145Snate@binkert.org
466145Snate@binkert.org#include "params/SimObject.hh"
476145Snate@binkert.org#include "sim/eventq.hh"
487054Snate@binkert.org#include "sim/serialize.hh"
496145Snate@binkert.org
507054Snate@binkert.orgclass BaseCPU;
517054Snate@binkert.orgclass Event;
526145Snate@binkert.org
537054Snate@binkert.org/*
547054Snate@binkert.org * Abstract superclass for simulation objects.  Represents things that
556145Snate@binkert.org * correspond to physical components and can be specified via the
566145Snate@binkert.org * config file (CPUs, caches, etc.).
577054Snate@binkert.org */
587054Snate@binkert.orgclass SimObject : public EventManager, public Serializable
596145Snate@binkert.org{
607054Snate@binkert.org  public:
616145Snate@binkert.org    enum State {
626145Snate@binkert.org        Running,
637054Snate@binkert.org        Draining,
647054Snate@binkert.org        Drained
657054Snate@binkert.org    };
666145Snate@binkert.org
677054Snate@binkert.org  private:
686145Snate@binkert.org    State state;
697054Snate@binkert.org
707054Snate@binkert.org  protected:
717054Snate@binkert.org    void changeState(State new_state) { state = new_state; }
727054Snate@binkert.org
736145Snate@binkert.org  public:
747054Snate@binkert.org    State getState() { return state; }
757054Snate@binkert.org
767054Snate@binkert.org  private:
777054Snate@binkert.org    typedef std::vector<SimObject *> SimObjectList;
787054Snate@binkert.org
797054Snate@binkert.org    // list of all instantiated simulation objects
807054Snate@binkert.org    static SimObjectList simObjectList;
817054Snate@binkert.org
827054Snate@binkert.org  protected:
837054Snate@binkert.org    const SimObjectParams *_params;
847054Snate@binkert.org
857054Snate@binkert.org  public:
866145Snate@binkert.org    typedef SimObjectParams Params;
876145Snate@binkert.org    const Params *params() const { return _params; }
887054Snate@binkert.org    SimObject(const Params *_params);
897054Snate@binkert.org    virtual ~SimObject() {}
906145Snate@binkert.org
917054Snate@binkert.org  public:
927054Snate@binkert.org
936145Snate@binkert.org    virtual const std::string name() const { return params()->name; }
946145Snate@binkert.org
957054Snate@binkert.org    // The following SimObject initialization methods are called from
967054Snate@binkert.org    // the instantiate() method in src/python/m5/simulate.py.  See
976145Snate@binkert.org    // that function for details on how/when these methods are
987054Snate@binkert.org    // invoked.
996145Snate@binkert.org
1006145Snate@binkert.org    /**
1017054Snate@binkert.org     * init() is called after all C++ SimObjects have been created and
1027054Snate@binkert.org     * all ports are connected.  Initializations that are independent
1036145Snate@binkert.org     * of unserialization but rely on a fully instantiated and
1047054Snate@binkert.org     * connected SimObject graph should be done here.
1057054Snate@binkert.org     */
1067054Snate@binkert.org    virtual void init();
1077054Snate@binkert.org
1087054Snate@binkert.org    /**
1096145Snate@binkert.org     * loadState() is called on each SimObject when restoring from a
1106145Snate@binkert.org     * checkpoint.  The default implementation simply calls
1116145Snate@binkert.org     * unserialize() if there is a corresponding section in the
1127054Snate@binkert.org     * checkpoint.  However, objects can override loadState() to get
1137054Snate@binkert.org     * other behaviors, e.g., doing other programmed initializations
1146145Snate@binkert.org     * after unserialize(), or complaining if no checkpoint section is
1157054Snate@binkert.org     * found.
1166145Snate@binkert.org     */
1176145Snate@binkert.org    virtual void loadState(Checkpoint *cp);
1187054Snate@binkert.org
1197054Snate@binkert.org    /**
1206145Snate@binkert.org     * initState() is called on each SimObject when *not* restoring
1217054Snate@binkert.org     * from a checkpoint.  This provides a hook for state
1227054Snate@binkert.org     * initializations that are only required for a "cold start".
1236145Snate@binkert.org     */
1246145Snate@binkert.org    virtual void initState();
1257054Snate@binkert.org
1267054Snate@binkert.org    // register statistics for this object
1276145Snate@binkert.org    virtual void regStats();
1287054Snate@binkert.org    virtual void regFormulas();
1296145Snate@binkert.org    virtual void resetStats();
1306145Snate@binkert.org
1317054Snate@binkert.org    /**
1327054Snate@binkert.org     * startup() is the final initialization call before simulation.
1336145Snate@binkert.org     * All state is initialized (including unserialized state, if any,
1347054Snate@binkert.org     * such as the curTick() value), so this is the appropriate place to
1357054Snate@binkert.org     * schedule initial event(s) for objects that need them.
1367054Snate@binkert.org     */
1377054Snate@binkert.org    virtual void startup();
1386145Snate@binkert.org
1397054Snate@binkert.org    // static: call nameOut() & serialize() on all SimObjects
1407054Snate@binkert.org    static void serializeAll(std::ostream &);
1417054Snate@binkert.org
1426145Snate@binkert.org    // Methods to drain objects in order to take checkpoints
1437054Snate@binkert.org    // Or switch from timing -> atomic memory model
1447054Snate@binkert.org    // Drain returns 0 if the simobject can drain immediately or
1457054Snate@binkert.org    // the number of times the drain_event's process function will be called
1467054Snate@binkert.org    // before the object will be done draining. Normally this should be 1
1477054Snate@binkert.org    virtual unsigned int drain(Event *drain_event);
1487054Snate@binkert.org    virtual void resume();
1496145Snate@binkert.org    virtual void setMemoryMode(State new_mode);
1507054Snate@binkert.org    virtual void switchOut();
1517054Snate@binkert.org    virtual void takeOverFrom(BaseCPU *cpu);
1526145Snate@binkert.org
1537054Snate@binkert.org#ifdef DEBUG
1547054Snate@binkert.org  public:
1557054Snate@binkert.org    bool doDebugBreak;
1567054Snate@binkert.org    static void debugObjectBreak(const std::string &objs);
1577054Snate@binkert.org#endif
1587054Snate@binkert.org
1597054Snate@binkert.org    /**
1607054Snate@binkert.org     * Find the SimObject with the given name and return a pointer to
1617054Snate@binkert.org     * it.  Primarily used for interactive debugging.  Argument is
1627054Snate@binkert.org     * char* rather than std::string to make it callable from gdb.
1637054Snate@binkert.org     */
1647054Snate@binkert.org    static SimObject *find(const char *name);
1657054Snate@binkert.org};
1666145Snate@binkert.org
1677054Snate@binkert.org#endif // __SIM_OBJECT_HH__
1686145Snate@binkert.org