misc: SystemC Elastic Trace Player Example.
[gem5.git] / util / tlm / main.cc
1 /*
2 * Copyright (c) 2015, University of Kaiserslautern
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
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.
15 *
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.
19 *
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.
31 *
32 * Authors: Matthias Jung
33 * Abdul Mutaal Ahmad
34 */
35
36 /**
37 * @file
38 *
39 * Example top level file for SystemC-TLM integration with C++-only
40 * instantiation.
41 *
42 */
43
44 #include <tlm_utils/simple_target_socket.h>
45
46 #include <cstdlib>
47 #include <iomanip>
48 #include <iostream>
49 #include <sstream>
50 #include <systemc>
51 #include <tlm>
52 #include <typeinfo>
53
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"
60 #include "sc_port.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"
69 #include "stats.hh"
70
71 // Defining global string variable decalred in stats.hh
72 std::string filename;
73
74 void usage(const std::string &prog_name)
75 {
76 std::cerr << "Usage: " << prog_name << (
77 " <config_file.ini> [ <option> ]\n\n"
78 "OPTIONS:\n"
79
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"
86 " -D -- debug on\n"
87 " -e <ticks> -- end of simulation after a \n"
88 " given number of ticks\n"
89 "\n"
90 );
91 std::exit(EXIT_FAILURE);
92 }
93
94 class SimControl : public Gem5SystemC::Module
95 {
96 protected:
97 int argc;
98 char **argv;
99 CxxConfigManager *config_manager;
100 Gem5SystemC::Logger logger;
101
102 Tick sim_end;
103 bool debug;
104 unsigned int offset;
105
106 public:
107 SC_HAS_PROCESS(SimControl);
108
109 SimControl(sc_core::sc_module_name name, int argc_, char **argv_);
110
111 void before_end_of_elaboration();
112
113 bool getDebugFlag() { return debug; }
114
115 unsigned int getOffset() { return offset; }
116
117 void run();
118 };
119
120 SimControl::SimControl(sc_core::sc_module_name name,
121 int argc_,
122 char **argv_) : Gem5SystemC::Module(name),
123 argc(argc_),
124 argv(argv_)
125 {
126 SC_THREAD(run);
127
128 std::string prog_name(argv[0]);
129 unsigned int arg_ptr = 1;
130
131 if (argc == 1) {
132 usage(prog_name);
133 }
134
135 cxxConfigInit();
136 Gem5SystemC::registerSCPorts();
137
138 Trace::setDebugLogger(&logger);
139
140 Gem5SystemC::setTickFrequency();
141 sc_core::sc_set_time_resolution(1, sc_core::SC_PS);
142
143 Gem5SystemC::Module::setupEventQueues(*this);
144 initSignals();
145
146 Stats::initSimStats();
147 Stats::registerHandlers(CxxConfig::statsReset, CxxConfig::statsDump);
148
149 Trace::enable();
150
151 sim_end = 0;
152 debug = false;
153 offset = 0;
154
155 const std::string config_file(argv[arg_ptr]);
156
157 CxxConfigFileBase *conf = new CxxIniFile();
158
159 if (!conf->load(config_file.c_str())) {
160 std::cerr << "Can't open config file: " << config_file << '\n';
161 std::exit(EXIT_FAILURE);
162 }
163 arg_ptr++;
164
165 config_manager = new CxxConfigManager(*conf);
166
167 try {
168 while (arg_ptr < argc) {
169 std::string option(argv[arg_ptr]);
170 arg_ptr++;
171 unsigned num_args = argc - arg_ptr;
172
173 if (option == "-p") {
174 if (num_args < 3) {
175 usage(prog_name);
176 }
177
178 config_manager->setParam(argv[arg_ptr], argv[arg_ptr + 1],
179 argv[arg_ptr + 2]);
180 arg_ptr += 3;
181 } else if (option == "-v") {
182 std::vector<std::string> values;
183
184 if (num_args < 3) {
185 usage(prog_name);
186 }
187 tokenize(values, argv[2], ',');
188 config_manager->setParamVector(argv[arg_ptr],
189 argv[arg_ptr],
190 values);
191 arg_ptr += 3;
192 } else if (option == "-d") {
193 if (num_args < 1) {
194 usage(prog_name);
195 }
196 if (argv[arg_ptr][0] == '-') {
197 clearDebugFlag(argv[arg_ptr] + 1);
198 } else {
199 setDebugFlag(argv[arg_ptr]);
200 }
201 arg_ptr++;
202 } else if (option == "-e") {
203 if (num_args < 1) {
204 usage(prog_name);
205 }
206 std::istringstream(argv[arg_ptr]) >> sim_end;
207 arg_ptr++;
208 } else if (option == "-D") {
209 debug = true;
210 } else if (option == "-o") {
211 if (num_args < 1) {
212 usage(prog_name);
213 }
214 std::istringstream(argv[arg_ptr]) >> offset;
215 arg_ptr++;
216 /* code */
217 } else {
218 usage(prog_name);
219 }
220 }
221 } catch (CxxConfigManager::Exception &e) {
222 std::cerr << e.name << ": " << e.message << "\n";
223 std::exit(EXIT_FAILURE);
224 }
225
226 CxxConfig::statsEnable();
227 getEventQueue(0)->dump();
228
229 try {
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);
235 }
236 }
237
238 void
239 SimControl::before_end_of_elaboration()
240 {
241 try {
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);
248 }
249 }
250
251 void
252 SimControl::run()
253 {
254 GlobalSimLoopExitEvent *exit_event = NULL;
255
256 if (sim_end == 0) {
257 exit_event = simulate();
258 } else {
259 exit_event = simulate(sim_end);
260 }
261
262 std::cerr << "Exit at tick " << curTick()
263 << ", cause: " << exit_event->getCause() << '\n';
264
265 getEventQueue(0)->dump();
266
267 #if TRY_CLEAN_DELETE
268 config_manager->deleteObjects();
269 #endif
270 }
271
272
273 void
274 reportHandler(const sc_core::sc_report &report,
275 const sc_core::sc_actions &actions)
276 {
277 uint64_t systemc_time = report.get_time().value();
278 uint64_t gem5_time = curTick();
279
280 std::cerr << report.get_time();
281
282 if (gem5_time < systemc_time) {
283 std::cerr << " (<) ";
284 } else if (gem5_time > systemc_time) {
285 std::cerr << " (!) ";
286 } else {
287 std::cerr << " (=) ";
288 }
289
290 std::cerr << ": " << report.get_msg_type()
291 << ' ' << report.get_msg() << '\n';
292 }
293
294
295 int
296 sc_main(int argc, char **argv)
297 {
298 sc_core::sc_report_handler::set_handler(reportHandler);
299
300 SimControl sim_control("gem5", argc, argv);
301 Target *memory;
302
303 filename = "m5out/stats-tlm.txt";
304
305 tlm::tlm_initiator_socket <> *mem_port =
306 dynamic_cast<tlm::tlm_initiator_socket<> *>(
307 sc_core::sc_find_object("gem5.memory")
308 );
309
310 if (mem_port) {
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(),
315 size,
316 sim_control.getOffset());
317
318 memory->socket.bind(*mem_port);
319 } else {
320 SC_REPORT_FATAL("sc_main", "Port Not Found");
321 std::exit(EXIT_FAILURE);
322 }
323
324 sc_core::sc_start();
325
326 SC_REPORT_INFO("sc_main", "End of Simulation");
327
328 CxxConfig::statsDump();
329
330 return EXIT_SUCCESS;
331 }