34 #include "avrmalloc.h"
58 #include "gnu_getopt.h"
66 static char *global_device_type = NULL;
68 static int global_eeprom_image_type = FFMT_BIN;
69 static char *global_eeprom_image_file = NULL;
71 static int global_flash_image_type = FFMT_BIN;
72 static char *global_flash_image_file = NULL;
74 static int global_gdbserver_mode = 0;
75 static int global_gdbserver_port = 1212;
76 static int global_gdb_debug = 0;
78 static char *global_disp_prog = NULL;
79 static int global_disp_without_xterm = 0;
81 static int global_dump_core = 0;
83 static int global_clock_freq = 8000000;
88 #define LEN_BREAK_LIST 50
89 static int global_break_count = 0;
90 static int global_break_list[LEN_BREAK_LIST];
92 static AvrCore *global_core = NULL;
95 static GdbComm_T global_gdb_comm[1] = {{
98 .read_reg = (CommFuncReadReg) avr_core_gpwr_get,
99 .write_reg = (CommFuncWriteReg) avr_core_gpwr_set,
101 .read_sreg = (CommFuncReadSREG) avr_core_sreg_get,
102 .write_sreg = (CommFuncWriteSREG) avr_core_sreg_set,
104 .read_pc = (CommFuncReadPC) avr_core_PC_get,
105 .write_pc = (CommFuncWritePC) avr_core_PC_set,
106 .max_pc = (CommFuncMaxPC) avr_core_PC_max,
108 .read_sram = (CommFuncReadSRAM) avr_core_mem_read,
109 .write_sram = (CommFuncWriteSRAM) avr_core_mem_write,
111 .read_flash = (CommFuncReadFlash) avr_core_flash_read,
112 .write_flash = (CommFuncWriteFlash) avr_core_flash_write,
113 .write_flash_lo8 = (CommFuncWriteFlashLo8) avr_core_flash_write_lo8,
114 .write_flash_hi8 = (CommFuncWriteFlashHi8) avr_core_flash_write_hi8,
124 .io_fetch = (CommFuncIORegFetch) avr_core_io_fetch,
129 static char *usage_fmt_str =
130 "\nUsage: %s [OPTIONS]... [flash_image]\n" "\n"
131 "Simulate an avr device. The optional flash_image file is loaded\n"
132 "into the flash program memory space of the device.\n" "\n" "Options:\n"
133 " -h, --help : Show this message\n"
134 " -D, --debug : Debug instruction output\n"
135 " -v, --version : Print out the version number and exit\n"
136 " -g, --gdbserver : Run as a gdbserver process\n"
137 " -G, --gdb-debug : Print out debug messages for gdbserver\n"
138 " -p, --port <port> : Listen for gdb connection on TCP port\n"
139 " -d, --device <dev> : Specify device type\n"
140 " -e, --eeprom-image <img> : Specify an eeprom image file\n"
141 " -E, --eeprom-type <type> : Specify the type of the eeprom image file\n"
142 " -F, --flash-type <type> : Specify the type of the flash image file\n"
143 " -L, --list-devices : Print supported devices to stdout and exit\n"
144 " -P, --disp-prog <prog> : Display register and memory info with prog\n"
145 " -X, --without-xterm : Don't start disp prog in an xterm\n"
146 " -C, --core-dump : Dump a core memory image to file on exit\n"
147 " -c, --clock-freq <freq> : Set the simulated mcu clock freqency (in Hz)\n"
148 " -B, --breakpoint <addr> : Set a breakpoint (address is a byte address)\n"
149 "\n" "If the image file types for eeprom or flash images are not given,\n"
150 "the default file type is binary.\n" "\n"
151 "If you wish to run the simulator in gdbserver mode, you do not\n"
152 "have to specify a flash-image file since the program can be loaded\n"
153 "from gdb via the `load` command.\n" "\n"
154 "If '--port' option is given, and '--gdbserver' is not, port is ignored\n"
155 "\n" "If running in gdbserver mode and port is not specified, a default\n"
156 "port of 1212 is used.\n" "\n"
157 "If using the '--breakpoint' option, note the simulator will terminate when\n"
158 "the address is hit if you are not running in gdbserver mode. This feature\n"
159 "not intended for use in gdbserver mode. It is really intended for testing\n"
160 "the simulator itself, but may be useful for testing avr programs too.\n"
161 "\n" "Currently available device types:\n";
171 fprintf (stdout, usage_fmt_str, prog);
173 fprintf (stdout,
"\n");
179 static struct option long_opts[] = {
181 {
"help", 0, 0,
'h' },
182 {
"debug", 0, 0,
'D' },
183 {
"version", 0, 0,
'v' },
184 {
"gdbserver", 0, 0,
'g' },
185 {
"gdb-debug", 0, 0,
'G' },
186 {
"port", 1, 0,
'p' },
187 {
"device", 1, 0,
'd' },
188 {
"eeprom-type", 1, 0,
'E' },
189 {
"eeprom-image", 1, 0,
'e' },
190 {
"flash-type", 1, 0,
'F' },
191 {
"list-devices", 0, 0,
'L' },
192 {
"disp-prog", 1, 0,
'P' },
193 {
"without-xterm", 1, 0,
'X' },
194 {
"core-dump", 0, 0,
'C' },
195 {
"clock-freq", 1, 0,
'c' },
196 {
"breakpoint", 1, 0,
'B' },
205 parse_cmd_line (
int argc,
char **argv)
208 char *prog = argv[0];
218 c = getopt_long (argc, argv,
"hgGvDLd:e:E:F:p:P:XCc:B:", long_opts,
229 global_gdbserver_mode = 1;
232 global_gdb_debug = 1;
235 global_gdbserver_port = atoi (optarg);
238 printf (
"\n%s version %s\n", PACKAGE, VERSION);
239 printf (
"Copyright 2001, 2002, 2003, 2004"
240 " Theodore A. Roth.\n");
241 printf (
"\n%s is free software, covered by the GNU General "
242 "Public License,\n", PACKAGE);
243 printf (
"and you are welcome to change it and/or distribute "
244 "copies of it under\n");
245 printf (
"the conditions of the GNU General Public License."
252 global_device_type = optarg;
255 global_eeprom_image_file =
avr_strdup (optarg);
259 global_eeprom_image_type =
str2ffmt (optarg);
261 global_flash_image_type =
str2ffmt (optarg);
270 global_disp_without_xterm = 1;
273 global_dump_core = 1;
276 if (sscanf (optarg,
"%d%c", &global_clock_freq, &dummy_char)
279 avr_error (
"Invalid clock value: %s", optarg);
285 if (sscanf (optarg,
"%i%c", &break_addr, &dummy_char) != 1)
287 avr_error (
"Ignoring invalid break addres: %s", optarg);
290 if (global_break_count < LEN_BREAK_LIST)
292 global_break_list[global_break_count] = break_addr;
293 global_break_count++;
297 avr_warning (
"Too many break points: igoring %s\n",
303 avr_error (
"getop() did something screwey");
307 if ((optind + 1) == argc)
308 global_flash_image_file = argv[optind];
309 else if (optind != argc)
315 if ((global_eeprom_image_type != FFMT_BIN)
316 || (global_flash_image_type != FFMT_BIN))
319 "Only the bin file format is currently "
320 "implemented. Sorry.\n");
327 if (global_device_type == NULL)
330 basename = strrchr (prog,
'/');
331 if (basename == NULL)
333 global_device_type = prog;
335 global_device_type = ++basename;
340 ext_port_rd (
int addr)
347 fprintf (stderr,
"\nEnter a byte of data to read into 0x%04x: ",
351 if (fgets (line,
sizeof (line), stdin) == NULL)
355 if (sscanf (line,
"%i\n", &data) != 1)
360 return (uint8_t) (data & 0xff);
364 ext_port_wr (
int addr, uint8_t val)
366 fprintf (stderr,
"writing 0x%02x to 0x%04x\n", val, addr);
373 atexit_cleanup (
void)
377 if (global_dump_core)
379 if ((dump = fopen (
"core_avr_dump.core",
"w")) == NULL)
382 fprintf (stderr,
"fopen failed: core_avr_dump.core: %s\n",
400 main (
int argc,
char **argv)
403 int flash_sz = 0, sram_sz = 0, eeprom_sz = 0;
406 parse_cmd_line (argc, argv);
409 if (global_core == NULL)
411 avr_warning (
"Device not supported: %s\n", global_device_type);
415 avr_message (
"Simulating clock frequency of %d Hz\n", global_clock_freq);
419 display_open (global_disp_prog, global_disp_without_xterm, flash_sz,
420 sram_sz, sram_start, eeprom_sz);
427 if (atexit (atexit_cleanup))
428 avr_error (
"Failed to install exit handler");
441 if (global_flash_image_file)
443 global_flash_image_type);
446 if (global_eeprom_image_file)
448 global_eeprom_image_type);
450 for (i = 0; i < global_break_count; i++)
456 avr_message (
"Setting breakpoint at 0x%x.\n", global_break_list[i]);
460 if (global_gdbserver_mode == 1)
462 global_gdb_comm->user_data = global_core;
468 if (global_flash_image_file)
472 fprintf (stderr,
"No program was specified to be run.\n");