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 *          Abdul Mutaal Ahmad
35 *          Christian Menard
36 */
37
38/**
39 * @file
40 *
41 *  Example top level file for SystemC-TLM integration with C++-only
42 *  instantiation.
43 *
44 */
45
46#include <systemc>
47#include <tlm>
48
49#include "sc_master_port.hh"
50#include "sc_slave_port.hh"
51#include "sim/cxx_config_ini.hh"
52#include "sim/init_signals.hh"
53#include "sim/stat_control.hh"
54#include "sim_control.hh"
55#include "stats.hh"
56
57// Define global string variable decalred in stats.hh
58std::string filename = "m5out/stats-systemc.txt";
59
60namespace Gem5SystemC
61{
62
63Gem5SimControl* Gem5SimControl::instance = nullptr;
64
65Gem5SimControl::Gem5SimControl(sc_core::sc_module_name name,
66                               const std::string& configFile,
67                               uint64_t simulationEnd,
68                               const std::string& gem5DebugFlags)
69  : Gem5SystemC::Module(name),
70    simulationEnd(simulationEnd)
71{
72    SC_THREAD(run);
73
74    if (instance != nullptr) {
75        panic("Tried to instantiate Gem5SimControl more than once!\n");
76    }
77    instance = this;
78
79    cxxConfigInit();
80
81    // register the systemc slave and master port handler
82    ExternalSlave::registerHandler("tlm_slave", new SCSlavePortHandler(*this));
83    ExternalMaster::registerHandler("tlm_master",
84                                    new SCMasterPortHandler(*this));
85
86    Trace::setDebugLogger(&logger);
87
88    Gem5SystemC::setTickFrequency();
89    assert(sc_core::sc_get_time_resolution()
90                    == sc_core::sc_time(1,sc_core::SC_PS));
91
92    Gem5SystemC::Module::setupEventQueues(*this);
93    initSignals();
94
95    Stats::initSimStats();
96    Stats::registerHandlers(CxxConfig::statsReset, CxxConfig::statsDump);
97
98    Trace::enable();
99
100    CxxConfigFileBase* conf = new CxxIniFile();
101
102    if (configFile.empty()) {
103        std::cerr << "No gem5 config file specified!\n";
104        std::exit(EXIT_FAILURE);
105    }
106
107    if (!conf->load(configFile.c_str())) {
108        std::cerr << "Can't open config file: " << configFile << '\n';
109        std::exit(EXIT_FAILURE);
110    }
111
112    config_manager = new CxxConfigManager(*conf);
113
114    // parse debug flags string and clear/set flags accordingly
115    std::stringstream ss;
116    ss.str(gem5DebugFlags);
117    std::string flag;
118    while (std::getline(ss, flag, ' ')) {
119        if (flag.at(0) == '-') {
120            flag.erase(0, 1); // remove the '-'
121            clearDebugFlag(flag.c_str());
122        }
123        else {
124            setDebugFlag(flag.c_str());
125        }
126    }
127
128    CxxConfig::statsEnable();
129    getEventQueue(0)->dump();
130
131    try {
132        config_manager->instantiate();
133    } catch (CxxConfigManager::Exception &e) {
134        std::cerr << "Config problem in sim object "
135                  << e.name << ": " << e.message << "\n";
136        std::exit(EXIT_FAILURE);
137    }
138}
139
140void
141Gem5SimControl::end_of_elaboration()
142{
143    try {
144        config_manager->initState();
145        config_manager->startup();
146    } catch (CxxConfigManager::Exception &e) {
147        std::cerr << "Config problem in sim object "
148            << e.name << ": " << e.message << "\n";
149        std::exit(EXIT_FAILURE);
150    }
151}
152
153void
154Gem5SimControl::run()
155{
156    // notify callback
157    beforeSimulate();
158
159    GlobalSimLoopExitEvent *exit_event = NULL;
160
161    if (simulationEnd == 0) {
162        exit_event = simulate();
163    } else {
164        exit_event = simulate(simulationEnd);
165    }
166
167    std::cerr << "Exit at tick " << curTick()
168              << ", cause: " << exit_event->getCause() << '\n';
169
170    getEventQueue(0)->dump();
171
172    // notify callback
173    afterSimulate();
174
175#if TRY_CLEAN_DELETE
176    config_manager->deleteObjects();
177#endif
178}
179
180void
181Gem5SimControl::registerSlavePort(const std::string& name, SCSlavePort* port)
182{
183    if (slavePorts.find(name) == slavePorts.end()) {
184        slavePorts[name] = port;
185    } else {
186        std::cerr << "Slave Port " << name << " is already registered!\n";
187        std::exit(EXIT_FAILURE);
188    }
189}
190
191void
192Gem5SimControl::registerMasterPort(const std::string& name, SCMasterPort* port)
193{
194    if (masterPorts.find(name) == masterPorts.end()) {
195        masterPorts[name] = port;
196    } else {
197        std::cerr << "Master Port " << name << " is already registered!\n";
198        std::exit(EXIT_FAILURE);
199    }
200}
201
202SCSlavePort*
203Gem5SimControl::getSlavePort(const std::string& name)
204{
205    if (slavePorts.find(name) == slavePorts.end()) {
206        std::cerr << "Slave Port " << name << " was not found!\n";
207        std::exit(EXIT_FAILURE);
208    }
209
210    return slavePorts.at(name);
211}
212
213SCMasterPort*
214Gem5SimControl::getMasterPort(const std::string& name)
215{
216    if (masterPorts.find(name) == masterPorts.end()) {
217        std::cerr << "Master Port " << name << " was not found!\n";
218        std::exit(EXIT_FAILURE);
219    }
220
221    return masterPorts.at(name);
222}
223
224}
225