9 #include "SimulationClock.h"
10 #include "GdbServer.h"
12 #include "BfdProgram.h"
13 #include "ScriptEngine.h"
16 #include "DeviceSettings.h"
20 #define AVR_SIM_VERSION "0.1"
22 static void list_supported_devices() {
23 DeviceSettings::listAll();
26 static void print_usage() {
27 std::cout
<< "AVR-Simulator"<< std::endl
28 << "-u run with user interface for external pin handling at port 7777\n"
29 << "-f --file <name> load elf-file <name> for simulation in simulated target\n"
30 << "-d --device <device name> simulate <device name>\n "
31 << "-a --analyzer <name> add analyzer <name> (scripts are added using script:<script>)"
32 << "-s --scriptconfig <filename> config file for scripts"
33 << "-g --gdbserver running as gdb-server\n"
34 << "-p <port> change <port> for gdb server to port\n"
35 << "-t --trace <file name> enable trace outputs into <file name>\n"
36 << "-l --listtrace <file name> list trace outputs from <file name>\n"
37 << "-F --cpufrequence set the cpu frequence to <Hz>\n"
38 << "-c --clockticks set the number of clocks ticks to execute\n"
39 << "-v --verbose output some hints to console\n"
40 << "-h --help outputs this\n"
43 std::cout
<< "Supported devices:"<< std::endl
;
44 list_supported_devices();
45 std::cout
<< std::endl
;
50 static SimulationClock simclock
;
53 static void signal_handler(int sig
) {
57 static void enable_signal_handler(int sig
= SIGINT
) {
58 struct sigaction action
;
60 action
.sa_handler
= signal_handler
;
61 sigemptyset( &action
.sa_mask
);
63 sigaction( sig
, &action
, 0 );
68 bool operator ()(const char *str
) {
69 const char *scriptPrefix
= "script:";
70 return strncmp( str
, scriptPrefix
, strlen(scriptPrefix
) ) == 0;
74 static bool HaveScripts(const std::list
<const char *> & analyzers
) {
75 return ( std::find_if(analyzers
.begin(), analyzers
.end(), IsScript()) != analyzers
.end() );
78 int main(int argc
, char *argv
[]) {
80 const char *devicename
= "atmega8";
81 const char *filename
= 0;
82 const char *listtrace
= 0;
83 unsigned long long fcpu
= 0;
84 bool gdbserver
= false;
85 int gdbserver_port
= 1234;
86 ClockOffset clockTicks
= 0;
88 std::list
<const char *> analyzers
;
89 const char *scriptconfig
= 0;
91 static struct option long_options
[] = {
92 { "file", 1, 0, 'f' },
93 { "device", 1, 0, 'd' },
94 { "analyzer", 1, 0, 'a' },
95 { "scriptconfig", 1, 0, 's' },
97 { "gdbserver", 0, 0, 's' },
98 { "gdbport", 1, 0, 'p' },
99 { "trace", 1, 0, 't' },
100 { "listtrace", 1, 0, 'l' },
101 { "version", 0, 0, 'V' },
102 { "cpufrequency", 1, 0, 'F' },
103 { "verbose", 0, 0, 'v' },
104 { "help", 0, 0, 'h' },
109 while ( (c
= getopt_long (argc
, argv
, "vd:F:f:a:s:gp:t:l:c:Vh",
110 long_options
, 0)) != -1) {
120 std::cout
<< "Device to simulate "<< devicename
125 fcpu
= strtoll(optarg
, NULL
, 10);
127 std::cout
<< "Running with CPU frequency: "<< fcpu
134 std::cout
<< "File to load "<< filename
<< std::endl
;
138 analyzers
.push_back( optarg
);
140 std::cout
<< "Adding analyzer "<< optarg
<< std::endl
;
144 scriptconfig
= optarg
;
146 std::cout
<< "Using configfile "<< scriptconfig
<< std::endl
;
152 std::cout
<< "Running as gdb-server"<< std::endl
;
156 gdbserver_port
= atoi(optarg
);
158 std::cout
<< "Running gdb-server on port: "
159 << gdbserver_port
<< std::endl
;
163 Trace::instance().enableLogging( optarg
);
165 std::cout
<< "Running in Trace Mode"<< std::endl
;
171 std::cout
<< "Listing Trace"<< std::endl
;
175 clockTicks
= strtoll(optarg
, NULL
, 10);
177 std::cout
<< "Ending executing after "<< clockTicks
178 << " clock ticks" << std::endl
;
182 std::cout
<< "Simulavr++ "<< AVR_SIM_VERSION
<< std::endl
;
184 << "See documentation for copyright and distribution terms"
186 std::cout
<< std::endl
;
196 ScriptEngine
*vm
= 0;
197 if( HaveScripts(analyzers
) /*|| scripted peripherals */ )
198 vm
= new ScriptEngine();
200 int ret
= EXIT_SUCCESS
;
202 //SimulationClock simclock; this is global now for signal_handlers
203 Device
dev( simclock
, devicename
);
206 enable_signal_handler(SIGINT
);
216 vm
->setProgram( program
);
218 if( filename
!= 0 ) {
219 program
.load( filename
);
223 if( clockTicks
!= 0 )
224 simclock
.setMaxTicks( clockTicks
);
226 if( clockTicks
!= 0 )
227 simclock
.setMaxTicks( clockTicks
);
230 dev
.setClockFrequency( fcpu
);
232 if( analyzers
.size() != 0 )
233 dev
.addAnalyzers( vm
, analyzers
);
235 if( scriptconfig
!= 0 ) {
237 std::cerr
<< "Warning: scriptconfig specified but no VM" << std::endl
;
239 vm
->loadConfig( scriptconfig
);
242 if( listtrace
!= 0 ) {
243 Trace::instance().list( listtrace
, std::cout
, dev
);
244 } else if( gdbserver
) {
245 GdbServer
gdb( simclock
, gdbserver_port
);
252 } catch( util::Exception
& ex
) {
253 std::cerr
<< ex
.message() << std::endl
;
254 ex
.printStackTrace( std::cerr
);
256 } catch( std::exception
& ex
) {
257 std::cerr
<< ex
.what() << std::endl
;
265 std::cout
<< "Simulation ran for " << simclock
.ticks()
266 << " clock ticks" << std::endl
;