1/*
2 * Copyright (c) 2015, University of Kaiserslautern
3 * Copyright (c) 2016, Dresden University of Technology (TU Dresden)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 *    this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 *    contributors may be used to endorse or promote products derived from
19 *    this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Authors: Matthias Jung
34 *          Christian Menard
35 */
36
37#ifndef __SC_SIM_CONTROL_HH__
38#define __SC_SIM_CONTROL_HH__
39
40#include <tlm_utils/simple_target_socket.h>
41
42#include <systemc>
43#include <tlm>
44
45#include "sc_logger.hh"
46#include "sc_module.hh"
47#include "sim/cxx_manager.hh"
48#include "sim/system.hh"
49#include "sim_control_if.hh"
50
51namespace Gem5SystemC
52{
53
54/**
55 * This is the central SystemC module that orchestrates the gem5 simulation.
56 *
57 * The module is responsible for loading the configuration file, setting up and
58 * maintaining the event queues, as well as starting and ending the simulation.
59 * While it is mandatory to have one instance of this class for running a gem5
60 * simulation in SystemC, it is not allowed to have multiple instances!
61 */
62class Gem5SimControl : public Module, public Gem5SimControlInterface
63{
64  protected:
65    CxxConfigManager* config_manager;
66    Gem5SystemC::Logger logger;
67
68    Tick simulationEnd;
69
70    /*
71     * Keep track of the slave and master ports that are created by gem5
72     * according to the config file.
73     */
74    std::map<const std::string, SCSlavePort*> slavePorts;
75    std::map<const std::string, SCMasterPort*> masterPorts;
76
77    /// Pointer to a previously created instance.
78    static Gem5SimControl* instance;
79
80    /** A callback that is called from the run thread before gem5 simulation is
81     * started.
82     *
83     * A derived class may use this to perform any additional initializations
84     * prior simulation.
85     */
86    virtual void beforeSimulate() {}
87
88    /** A callback that is called from the run thread after gem5 simulation
89     * completed.
90     *
91     * A derived class may use this to perform any additional tasks after gem5
92     * exits. For instance, a derived class could use this to call sc_stop().
93     */
94    virtual void afterSimulate() {}
95
96  public:
97    SC_HAS_PROCESS(Gem5SimControl);
98
99    /**
100     * Constructor.
101     *
102     * This class has a public constructor although the class is actually a
103     * singleton. The public constructor is required to ensure compatibility
104     * to external SystemC based tools. For the same reason, the constructor
105     * parameters are basic types (int, string).
106     *
107     * @param configFile     location of the gem5 configuration file
108     * @param simulationEnd  number of ticks to simulate
109     * @param gem5DebugFlags a space separated list of gem5 debug flags to be
110     *                       set, a prepended '-' clears the flag
111     */
112    Gem5SimControl(sc_core::sc_module_name name,
113                   const std::string& configFile,
114                   uint64_t simulationEnd,
115                   const std::string& gem5DebugFlags);
116
117    void registerSlavePort(const std::string& name, SCSlavePort* port);
118    void registerMasterPort(const std::string& name, SCMasterPort* port);
119    SCSlavePort* getSlavePort(const std::string& name) override;
120    SCMasterPort* getMasterPort(const std::string& name) override;
121
122    void end_of_elaboration();
123
124    void run();
125};
126
127}
128
129#endif
130