sim_object.hh revision 7534
14486Sbinkertn@umich.edu/*
24486Sbinkertn@umich.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan
34486Sbinkertn@umich.edu * Copyright (c) 2010 Advanced Micro Devices, Inc.
44486Sbinkertn@umich.edu * All rights reserved.
54486Sbinkertn@umich.edu *
64486Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
74486Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
84486Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
94486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
104486Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
114486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
124486Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
134486Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
144486Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
154486Sbinkertn@umich.edu * this software without specific prior written permission.
164486Sbinkertn@umich.edu *
174486Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184486Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194486Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
204486Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
214486Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
224486Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
234486Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244486Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254486Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264486Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
274486Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284486Sbinkertn@umich.edu *
296654Snate@binkert.org * Authors: Steve Reinhardt
303102SN/A *          Nathan Binkert
313102SN/A */
321681SN/A
333223SN/A/* @file
348887Sgeoffrey.blake@arm.com * User Console Definitions
354486Sbinkertn@umich.edu */
362817SN/A
372817SN/A#ifndef __SIM_OBJECT_HH__
389341SAndreas.Sandberg@arm.com#define __SIM_OBJECT_HH__
399341SAndreas.Sandberg@arm.com
402932SN/A#include <iostream>
411681SN/A#include <list>
424597Sbinkertn@umich.edu#include <map>
431681SN/A#include <string>
449184Sandreas.hansson@arm.com#include <vector>
459184Sandreas.hansson@arm.com
469184Sandreas.hansson@arm.com#include "params/SimObject.hh"
479184Sandreas.hansson@arm.com#include "sim/eventq.hh"
489184Sandreas.hansson@arm.com#include "sim/serialize.hh"
492932SN/A
502932SN/Aclass BaseCPU;
519184Sandreas.hansson@arm.comclass Event;
529184Sandreas.hansson@arm.com
539184Sandreas.hansson@arm.com/*
549184Sandreas.hansson@arm.com * Abstract superclass for simulation objects.  Represents things that
559184Sandreas.hansson@arm.com * correspond to physical components and can be specified via the
562932SN/A * config file (CPUs, caches, etc.).
571681SN/A */
589184Sandreas.hansson@arm.comclass SimObject : public EventManager, public Serializable
599184Sandreas.hansson@arm.com{
609184Sandreas.hansson@arm.com  public:
619184Sandreas.hansson@arm.com    enum State {
622932SN/A        Running,
631681SN/A        Draining,
649184Sandreas.hansson@arm.com        Drained
652932SN/A    };
669184Sandreas.hansson@arm.com
672932SN/A  private:
689184Sandreas.hansson@arm.com    State state;
692932SN/A
702932SN/A  protected:
712932SN/A    void changeState(State new_state) { state = new_state; }
722932SN/A
732932SN/A  public:
743223SN/A    State getState() { return state; }
752932SN/A
769184Sandreas.hansson@arm.com  private:
771681SN/A    typedef std::vector<SimObject *> SimObjectList;
789184Sandreas.hansson@arm.com
792932SN/A    // list of all instantiated simulation objects
802932SN/A    static SimObjectList simObjectList;
819184Sandreas.hansson@arm.com
829184Sandreas.hansson@arm.com  protected:
831681SN/A    const SimObjectParams *_params;
842932SN/A
852932SN/A  public:
861681SN/A    typedef SimObjectParams Params;
872932SN/A    const Params *params() const { return _params; }
882932SN/A    SimObject(const Params *_params);
892932SN/A    virtual ~SimObject() {}
902932SN/A
912932SN/A  public:
922932SN/A
932932SN/A    virtual const std::string name() const { return params()->name; }
943223SN/A
952932SN/A    // The following SimObject initialization methods are called from
962932SN/A    // the instantiate() method in src/python/m5/simulate.py.  See
971681SN/A    // that function for details on how/when these methods are
982932SN/A    // invoked.
992932SN/A
1002873SN/A    /**
1012932SN/A     * init() is called after all C++ SimObjects have been created and
1021681SN/A     * all ports are connected.  Initializations that are independent
1032932SN/A     * of unserialization but rely on a fully instantiated and
1042932SN/A     * connected SimObject graph should be done here.
1058199SAli.Saidi@ARM.com     */
1068199SAli.Saidi@ARM.com    virtual void init();
1078199SAli.Saidi@ARM.com
1088519SAli.Saidi@ARM.com    /**
1098519SAli.Saidi@ARM.com     * loadState() is called on each SimObject when restoring from a
1102932SN/A     * checkpoint.  The default implementation simply calls
1112932SN/A     * unserialize() if there is a corresponding section in the
1121681SN/A     * checkpoint.  However, objects can override loadState() to get
1132932SN/A     * other behaviors, e.g., doing other programmed initializations
1141681SN/A     * after unserialize(), or complaining if no checkpoint section is
1152932SN/A     * found.
1162932SN/A     */
1172932SN/A    virtual void loadState(Checkpoint *cp);
1182932SN/A
1192932SN/A    /**
1201681SN/A     * initState() is called on each SimObject when *not* restoring
1212932SN/A     * from a checkpoint.  This provides a hook for state
1221681SN/A     * initializations that are only required for a "cold start".
1234597Sbinkertn@umich.edu     */
1244597Sbinkertn@umich.edu    virtual void initState();
1254597Sbinkertn@umich.edu
1264597Sbinkertn@umich.edu    // register statistics for this object
1274597Sbinkertn@umich.edu    virtual void regStats();
1284597Sbinkertn@umich.edu    virtual void regFormulas();
1294597Sbinkertn@umich.edu    virtual void resetStats();
1304597Sbinkertn@umich.edu
1314597Sbinkertn@umich.edu    /**
1324303SN/A     * startup() is the final initialization call before simulation.
1338727Snilay@cs.wisc.edu     * All state is initialized (including unserialized state, if any,
1348727Snilay@cs.wisc.edu     * such as the curTick value), so this is the appropriate place to
1358887Sgeoffrey.blake@arm.com     * schedule initial event(s) for objects that need them.
1368887Sgeoffrey.blake@arm.com     */
1378887Sgeoffrey.blake@arm.com    virtual void startup();
1388887Sgeoffrey.blake@arm.com
1398887Sgeoffrey.blake@arm.com    // static: call nameOut() & serialize() on all SimObjects
1408887Sgeoffrey.blake@arm.com    static void serializeAll(std::ostream &);
1418887Sgeoffrey.blake@arm.com    static void unserializeAll(Checkpoint *cp);
1428887Sgeoffrey.blake@arm.com
1438887Sgeoffrey.blake@arm.com    // Methods to drain objects in order to take checkpoints
1448887Sgeoffrey.blake@arm.com    // Or switch from timing -> atomic memory model
1458887Sgeoffrey.blake@arm.com    // Drain returns 0 if the simobject can drain immediately or
1469132Satgutier@umich.edu    // the number of times the drain_event's process function will be called
1478887Sgeoffrey.blake@arm.com    // before the object will be done draining. Normally this should be 1
1488887Sgeoffrey.blake@arm.com    virtual unsigned int drain(Event *drain_event);
1498887Sgeoffrey.blake@arm.com    virtual void resume();
1508887Sgeoffrey.blake@arm.com    virtual void setMemoryMode(State new_mode);
151    virtual void switchOut();
152    virtual void takeOverFrom(BaseCPU *cpu);
153
154#ifdef DEBUG
155  public:
156    bool doDebugBreak;
157    static void debugObjectBreak(const std::string &objs);
158#endif
159
160    /**
161     * Find the SimObject with the given name and return a pointer to
162     * it.  Primarily used for interactive debugging.  Argument is
163     * char* rather than std::string to make it callable from gdb.
164     */
165    static SimObject *find(const char *name);
166};
167
168#endif // __SIM_OBJECT_HH__
169