misc: Clean up and complete the gem5<->SystemC-TLM bridge [2/10]
[gem5.git] / util / tlm / sim_control.cc
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 void
58 usage(const std::string& prog_name)
59 {
60 std::cerr
61 << "Usage: " << prog_name
62 << (" <config_file.ini> [ <option> ]\n\n"
63 "OPTIONS:\n"
64
65 " -o <offset> -- set memory offset\n"
66 " -p <object> <param> <value> -- set a parameter\n"
67 " -v <object> <param> <values> -- set a vector parameter from a\n"
68 " comma separated values string\n"
69 " -d <flag> -- set a debug flag\n"
70 " (-<flag> clear a flag)\n"
71 " -D -- debug on\n"
72 " -e <ticks> -- end of simulation after a \n"
73 " given number of ticks\n"
74 "\n");
75 std::exit(EXIT_FAILURE);
76 }
77
78 SimControl::SimControl(sc_core::sc_module_name name, int argc_, char** argv_)
79 : Gem5SystemC::Module(name),
80 argc(argc_),
81 argv(argv_)
82 {
83 SC_THREAD(run);
84
85 std::string prog_name(argv[0]);
86 unsigned int arg_ptr = 1;
87
88 if (argc == 1) {
89 usage(prog_name);
90 }
91
92 cxxConfigInit();
93 Gem5SystemC::SCSlavePort::registerPortHandler();
94 Gem5SystemC::SCMasterPort::registerPortHandler(*this);
95
96 Trace::setDebugLogger(&logger);
97
98 Gem5SystemC::setTickFrequency();
99 sc_core::sc_set_time_resolution(1, sc_core::SC_PS);
100
101 Gem5SystemC::Module::setupEventQueues(*this);
102 initSignals();
103
104 Stats::initSimStats();
105 Stats::registerHandlers(CxxConfig::statsReset, CxxConfig::statsDump);
106
107 Trace::enable();
108
109 sim_end = 0;
110 debug = false;
111 offset = 0;
112
113 const std::string config_file(argv[arg_ptr]);
114
115 CxxConfigFileBase *conf = new CxxIniFile();
116
117 if (!conf->load(config_file.c_str())) {
118 std::cerr << "Can't open config file: " << config_file << '\n';
119 std::exit(EXIT_FAILURE);
120 }
121 arg_ptr++;
122
123 config_manager = new CxxConfigManager(*conf);
124
125 try {
126 while (arg_ptr < argc) {
127 std::string option(argv[arg_ptr]);
128 arg_ptr++;
129 unsigned num_args = argc - arg_ptr;
130
131 if (option == "-p") {
132 if (num_args < 3) {
133 usage(prog_name);
134 }
135
136 config_manager->setParam(argv[arg_ptr], argv[arg_ptr + 1],
137 argv[arg_ptr + 2]);
138 arg_ptr += 3;
139 } else if (option == "-v") {
140 std::vector<std::string> values;
141
142 if (num_args < 3) {
143 usage(prog_name);
144 }
145 tokenize(values, argv[2], ',');
146 config_manager->setParamVector(argv[arg_ptr],
147 argv[arg_ptr],
148 values);
149 arg_ptr += 3;
150 } else if (option == "-d") {
151 if (num_args < 1) {
152 usage(prog_name);
153 }
154 if (argv[arg_ptr][0] == '-') {
155 clearDebugFlag(argv[arg_ptr] + 1);
156 } else {
157 setDebugFlag(argv[arg_ptr]);
158 }
159 arg_ptr++;
160 } else if (option == "-e") {
161 if (num_args < 1) {
162 usage(prog_name);
163 }
164 std::istringstream(argv[arg_ptr]) >> sim_end;
165 arg_ptr++;
166 } else if (option == "-D") {
167 debug = true;
168 } else if (option == "-o") {
169 if (num_args < 1) {
170 usage(prog_name);
171 }
172 std::istringstream(argv[arg_ptr]) >> offset;
173 arg_ptr++;
174 /* code */
175 } else {
176 usage(prog_name);
177 }
178 }
179 } catch (CxxConfigManager::Exception &e) {
180 std::cerr << e.name << ": " << e.message << "\n";
181 std::exit(EXIT_FAILURE);
182 }
183
184 CxxConfig::statsEnable();
185 getEventQueue(0)->dump();
186
187 try {
188 config_manager->instantiate();
189 } catch (CxxConfigManager::Exception &e) {
190 std::cerr << "Config problem in sim object "
191 << e.name << ": " << e.message << "\n";
192 std::exit(EXIT_FAILURE);
193 }
194 }
195
196 void
197 SimControl::before_end_of_elaboration()
198 {
199 try {
200 config_manager->initState();
201 config_manager->startup();
202 } catch (CxxConfigManager::Exception &e) {
203 std::cerr << "Config problem in sim object "
204 << e.name << ": " << e.message << "\n";
205 std::exit(EXIT_FAILURE);
206 }
207 }
208
209 void
210 SimControl::run()
211 {
212 GlobalSimLoopExitEvent *exit_event = NULL;
213
214 if (sim_end == 0) {
215 exit_event = simulate();
216 } else {
217 exit_event = simulate(sim_end);
218 }
219
220 std::cerr << "Exit at tick " << curTick()
221 << ", cause: " << exit_event->getCause() << '\n';
222
223 getEventQueue(0)->dump();
224
225 #if TRY_CLEAN_DELETE
226 config_manager->deleteObjects();
227 #endif
228 }