2 * Copyright (c) 2015, University of Kaiserslautern
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
24 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Authors: Matthias Jung
38 * Example top level file for SystemC-TLM integration with C++-only
43 #include <tlm_utils/simple_target_socket.h>
53 #include "base/statistics.hh"
54 #include "base/str.hh"
55 #include "base/trace.hh"
56 #include "cpu/base.hh"
57 #include "sc_logger.hh"
58 #include "sc_module.hh"
60 #include "sc_target.hh"
61 #include "sim/cxx_config_ini.hh"
62 #include "sim/cxx_manager.hh"
63 #include "sim/init_signals.hh"
64 #include "sim/serialize.hh"
65 #include "sim/simulate.hh"
66 #include "sim/stat_control.hh"
67 #include "sim/system.hh"
70 void usage(const std::string
&prog_name
)
72 std::cerr
<< "Usage: " << prog_name
<< (
73 " <config_file.ini> [ <option> ]\n\n"
76 " -o <offset> -- set memory offset\n"
77 " -p <object> <param> <value> -- set a parameter\n"
78 " -v <object> <param> <values> -- set a vector parameter from a\n"
79 " comma separated values string\n"
80 " -d <flag> -- set a debug flag\n"
81 " (-<flag> clear a flag)\n"
83 " -e <ticks> -- end of simulation after a \n"
84 " given number of ticks\n"
87 std::exit(EXIT_FAILURE
);
90 class SimControl
: public Gem5SystemC::Module
95 CxxConfigManager
*config_manager
;
96 Gem5SystemC::Logger logger
;
103 SC_HAS_PROCESS(SimControl
);
105 SimControl(sc_core::sc_module_name name
, int argc_
, char **argv_
);
107 void before_end_of_elaboration();
109 bool getDebugFlag() { return debug
; }
111 unsigned int getOffset() { return offset
; }
116 SimControl::SimControl(sc_core::sc_module_name name
,
118 char **argv_
) : Gem5SystemC::Module(name
),
124 std::string
prog_name(argv
[0]);
125 unsigned int arg_ptr
= 1;
132 Gem5SystemC::registerSCPorts();
134 Trace::setDebugLogger(&logger
);
136 Gem5SystemC::setTickFrequency();
137 sc_core::sc_set_time_resolution(1, sc_core::SC_PS
);
139 Gem5SystemC::Module::setupEventQueues(*this);
142 Stats::initSimStats();
143 Stats::registerHandlers(CxxConfig::statsReset
, CxxConfig::statsDump
);
151 const std::string
config_file(argv
[arg_ptr
]);
153 CxxConfigFileBase
*conf
= new CxxIniFile();
155 if (!conf
->load(config_file
.c_str())) {
156 std::cerr
<< "Can't open config file: " << config_file
<< '\n';
157 std::exit(EXIT_FAILURE
);
161 config_manager
= new CxxConfigManager(*conf
);
164 while (arg_ptr
< argc
) {
165 std::string
option(argv
[arg_ptr
]);
167 unsigned num_args
= argc
- arg_ptr
;
169 if (option
== "-p") {
174 config_manager
->setParam(argv
[arg_ptr
], argv
[arg_ptr
+ 1],
177 } else if (option
== "-v") {
178 std::vector
<std::string
> values
;
183 tokenize(values
, argv
[2], ',');
184 config_manager
->setParamVector(argv
[arg_ptr
],
188 } else if (option
== "-d") {
192 if (argv
[arg_ptr
][0] == '-') {
193 clearDebugFlag(argv
[arg_ptr
] + 1);
195 setDebugFlag(argv
[arg_ptr
]);
198 } else if (option
== "-e") {
202 std::istringstream(argv
[arg_ptr
]) >> sim_end
;
204 } else if (option
== "-D") {
206 } else if (option
== "-o") {
210 std::istringstream(argv
[arg_ptr
]) >> offset
;
217 } catch (CxxConfigManager::Exception
&e
) {
218 std::cerr
<< e
.name
<< ": " << e
.message
<< "\n";
219 std::exit(EXIT_FAILURE
);
222 CxxConfig::statsEnable();
223 getEventQueue(0)->dump();
226 config_manager
->instantiate();
227 } catch (CxxConfigManager::Exception
&e
) {
228 std::cerr
<< "Config problem in sim object "
229 << e
.name
<< ": " << e
.message
<< "\n";
230 std::exit(EXIT_FAILURE
);
235 SimControl::before_end_of_elaboration()
238 config_manager
->initState();
239 config_manager
->startup();
240 } catch (CxxConfigManager::Exception
&e
) {
241 std::cerr
<< "Config problem in sim object "
242 << e
.name
<< ": " << e
.message
<< "\n";
243 std::exit(EXIT_FAILURE
);
250 GlobalSimLoopExitEvent
*exit_event
= NULL
;
253 exit_event
= simulate();
255 exit_event
= simulate(sim_end
);
258 std::cerr
<< "Exit at tick " << curTick()
259 << ", cause: " << exit_event
->getCause() << '\n';
261 getEventQueue(0)->dump();
264 config_manager
->deleteObjects();
270 reportHandler(const sc_core::sc_report
&report
,
271 const sc_core::sc_actions
&actions
)
273 uint64_t systemc_time
= report
.get_time().value();
274 uint64_t gem5_time
= curTick();
276 std::cerr
<< report
.get_time();
278 if (gem5_time
< systemc_time
) {
279 std::cerr
<< " (<) ";
280 } else if (gem5_time
> systemc_time
) {
281 std::cerr
<< " (!) ";
283 std::cerr
<< " (=) ";
286 std::cerr
<< ": " << report
.get_msg_type()
287 << ' ' << report
.get_msg() << '\n';
292 sc_main(int argc
, char **argv
)
294 sc_core::sc_report_handler::set_handler(reportHandler
);
296 SimControl
sim_control("gem5", argc
, argv
);
299 tlm::tlm_initiator_socket
<> *mem_port
=
300 dynamic_cast<tlm::tlm_initiator_socket
<> *>(
301 sc_core::sc_find_object("gem5.memory")
305 SC_REPORT_INFO("sc_main", "Port Found");
306 unsigned long long int size
= 512*1024*1024ULL;
307 memory
= new Target("memory",
308 sim_control
.getDebugFlag(),
310 sim_control
.getOffset());
312 memory
->socket
.bind(*mem_port
);
314 SC_REPORT_FATAL("sc_main", "Port Not Found");
315 std::exit(EXIT_FAILURE
);
320 SC_REPORT_INFO("sc_main", "End of Simulation");