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
39 * Example top level file for SystemC-TLM integration with C++-only
44 #include <tlm_utils/simple_target_socket.h>
54 #include "base/statistics.hh"
55 #include "base/str.hh"
56 #include "base/trace.hh"
57 #include "cpu/base.hh"
58 #include "sc_logger.hh"
59 #include "sc_module.hh"
61 #include "sc_target.hh"
62 #include "sim/cxx_config_ini.hh"
63 #include "sim/cxx_manager.hh"
64 #include "sim/init_signals.hh"
65 #include "sim/serialize.hh"
66 #include "sim/simulate.hh"
67 #include "sim/stat_control.hh"
68 #include "sim/system.hh"
71 // Defining global string variable decalred in stats.hh
74 void usage(const std::string
&prog_name
)
76 std::cerr
<< "Usage: " << prog_name
<< (
77 " <config_file.ini> [ <option> ]\n\n"
80 " -o <offset> -- set memory offset\n"
81 " -p <object> <param> <value> -- set a parameter\n"
82 " -v <object> <param> <values> -- set a vector parameter from a\n"
83 " comma separated values string\n"
84 " -d <flag> -- set a debug flag\n"
85 " (-<flag> clear a flag)\n"
87 " -e <ticks> -- end of simulation after a \n"
88 " given number of ticks\n"
91 std::exit(EXIT_FAILURE
);
94 class SimControl
: public Gem5SystemC::Module
99 CxxConfigManager
*config_manager
;
100 Gem5SystemC::Logger logger
;
107 SC_HAS_PROCESS(SimControl
);
109 SimControl(sc_core::sc_module_name name
, int argc_
, char **argv_
);
111 void before_end_of_elaboration();
113 bool getDebugFlag() { return debug
; }
115 unsigned int getOffset() { return offset
; }
120 SimControl::SimControl(sc_core::sc_module_name name
,
122 char **argv_
) : Gem5SystemC::Module(name
),
128 std::string
prog_name(argv
[0]);
129 unsigned int arg_ptr
= 1;
136 Gem5SystemC::registerSCPorts();
138 Trace::setDebugLogger(&logger
);
140 Gem5SystemC::setTickFrequency();
141 sc_core::sc_set_time_resolution(1, sc_core::SC_PS
);
143 Gem5SystemC::Module::setupEventQueues(*this);
146 Stats::initSimStats();
147 Stats::registerHandlers(CxxConfig::statsReset
, CxxConfig::statsDump
);
155 const std::string
config_file(argv
[arg_ptr
]);
157 CxxConfigFileBase
*conf
= new CxxIniFile();
159 if (!conf
->load(config_file
.c_str())) {
160 std::cerr
<< "Can't open config file: " << config_file
<< '\n';
161 std::exit(EXIT_FAILURE
);
165 config_manager
= new CxxConfigManager(*conf
);
168 while (arg_ptr
< argc
) {
169 std::string
option(argv
[arg_ptr
]);
171 unsigned num_args
= argc
- arg_ptr
;
173 if (option
== "-p") {
178 config_manager
->setParam(argv
[arg_ptr
], argv
[arg_ptr
+ 1],
181 } else if (option
== "-v") {
182 std::vector
<std::string
> values
;
187 tokenize(values
, argv
[2], ',');
188 config_manager
->setParamVector(argv
[arg_ptr
],
192 } else if (option
== "-d") {
196 if (argv
[arg_ptr
][0] == '-') {
197 clearDebugFlag(argv
[arg_ptr
] + 1);
199 setDebugFlag(argv
[arg_ptr
]);
202 } else if (option
== "-e") {
206 std::istringstream(argv
[arg_ptr
]) >> sim_end
;
208 } else if (option
== "-D") {
210 } else if (option
== "-o") {
214 std::istringstream(argv
[arg_ptr
]) >> offset
;
221 } catch (CxxConfigManager::Exception
&e
) {
222 std::cerr
<< e
.name
<< ": " << e
.message
<< "\n";
223 std::exit(EXIT_FAILURE
);
226 CxxConfig::statsEnable();
227 getEventQueue(0)->dump();
230 config_manager
->instantiate();
231 } catch (CxxConfigManager::Exception
&e
) {
232 std::cerr
<< "Config problem in sim object "
233 << e
.name
<< ": " << e
.message
<< "\n";
234 std::exit(EXIT_FAILURE
);
239 SimControl::before_end_of_elaboration()
242 config_manager
->initState();
243 config_manager
->startup();
244 } catch (CxxConfigManager::Exception
&e
) {
245 std::cerr
<< "Config problem in sim object "
246 << e
.name
<< ": " << e
.message
<< "\n";
247 std::exit(EXIT_FAILURE
);
254 GlobalSimLoopExitEvent
*exit_event
= NULL
;
257 exit_event
= simulate();
259 exit_event
= simulate(sim_end
);
262 std::cerr
<< "Exit at tick " << curTick()
263 << ", cause: " << exit_event
->getCause() << '\n';
265 getEventQueue(0)->dump();
268 config_manager
->deleteObjects();
274 reportHandler(const sc_core::sc_report
&report
,
275 const sc_core::sc_actions
&actions
)
277 uint64_t systemc_time
= report
.get_time().value();
278 uint64_t gem5_time
= curTick();
280 std::cerr
<< report
.get_time();
282 if (gem5_time
< systemc_time
) {
283 std::cerr
<< " (<) ";
284 } else if (gem5_time
> systemc_time
) {
285 std::cerr
<< " (!) ";
287 std::cerr
<< " (=) ";
290 std::cerr
<< ": " << report
.get_msg_type()
291 << ' ' << report
.get_msg() << '\n';
296 sc_main(int argc
, char **argv
)
298 sc_core::sc_report_handler::set_handler(reportHandler
);
300 SimControl
sim_control("gem5", argc
, argv
);
303 filename
= "m5out/stats-tlm.txt";
305 tlm::tlm_initiator_socket
<> *mem_port
=
306 dynamic_cast<tlm::tlm_initiator_socket
<> *>(
307 sc_core::sc_find_object("gem5.memory")
311 SC_REPORT_INFO("sc_main", "Port Found");
312 unsigned long long int size
= 512*1024*1024ULL;
313 memory
= new Target("memory",
314 sim_control
.getDebugFlag(),
316 sim_control
.getOffset());
318 memory
->socket
.bind(*mem_port
);
320 SC_REPORT_FATAL("sc_main", "Port Not Found");
321 std::exit(EXIT_FAILURE
);
326 SC_REPORT_INFO("sc_main", "End of Simulation");
328 CxxConfig::statsDump();