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