run_avr: Use the new read_ihex_chunks
[simavr] / simavr / sim / run_avr.c
1 /*
2         run_avr.c
3
4         Copyright 2008, 2010 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <libgen.h>
25 #include <string.h>
26 #include <signal.h>
27 #include "sim_avr.h"
28 #include "sim_elf.h"
29 #include "sim_core.h"
30 #include "sim_gdb.h"
31 #include "sim_hex.h"
32
33 #include "sim_core_decl.h"
34
35 void display_usage(char * app)
36 {
37         printf("usage: %s [-t] [-g] [-m <device>] [-f <frequency>] firmware\n", app);
38         printf("       -t: run full scale decoder trace\n"
39                    "       -g: listen for gdb connection on port 1234\n"
40                    "       -ff: Loads next .hex file as flash\n"
41                    "       -ee: Loads next .hex file as eeprom\n"
42                    "   Supported AVR cores:\n");
43         for (int i = 0; avr_kind[i]; i++) {
44                 printf("       ");
45                 for (int ti = 0; ti < 4 && avr_kind[i]->names[ti]; ti++)
46                         printf("%s ", avr_kind[i]->names[ti]);
47                 printf("\n");
48         }
49         exit(1);
50 }
51
52 avr_t * avr = NULL;
53
54 void
55 sig_int(
56                 int sign)
57 {
58         printf("signal caught, simavr terminating\n");
59         if (avr)
60                 avr_terminate(avr);
61         exit(0);
62 }
63
64 int main(int argc, char *argv[])
65 {
66         elf_firmware_t f = {{0}};
67         long f_cpu = 0;
68         int trace = 0;
69         int gdb = 0;
70         char name[16] = "";
71         uint32_t loadBase = AVR_SEGMENT_OFFSET_FLASH;
72         int trace_vectors[8] = {0};
73         int trace_vectors_count = 0;
74
75         if (argc == 1)
76                 display_usage(basename(argv[0]));
77
78         for (int pi = 1; pi < argc; pi++) {
79                 if (!strcmp(argv[pi], "-h") || !strcmp(argv[pi], "-help")) {
80                         display_usage(basename(argv[0]));
81                 } else if (!strcmp(argv[pi], "-m") || !strcmp(argv[pi], "-mcu")) {
82                         if (pi < argc-1)
83                                 strcpy(name, argv[++pi]);
84                         else
85                                 display_usage(basename(argv[0]));
86                 } else if (!strcmp(argv[pi], "-f") || !strcmp(argv[pi], "-freq")) {
87                         if (pi < argc-1)
88                                 f_cpu = atoi(argv[++pi]);
89                         else
90                                 display_usage(basename(argv[0]));
91                 } else if (!strcmp(argv[pi], "-t") || !strcmp(argv[pi], "-trace")) {
92                         trace++;
93                 } else if (!strcmp(argv[pi], "-ti")) {
94                         if (pi < argc-1)
95                                 trace_vectors[trace_vectors_count++] = atoi(argv[++pi]);
96                 } else if (!strcmp(argv[pi], "-g") || !strcmp(argv[pi], "-gdb")) {
97                         gdb++;
98                 } else if (!strcmp(argv[pi], "-ee")) {
99                         loadBase = AVR_SEGMENT_OFFSET_EEPROM;
100                 } else if (!strcmp(argv[pi], "-ff")) {
101                         loadBase = AVR_SEGMENT_OFFSET_FLASH;                    
102                 } else if (argv[pi][0] != '-') {
103                         char * filename = argv[pi];
104                         char * suffix = strrchr(filename, '.');
105                         if (suffix && !strcasecmp(suffix, ".hex")) {
106                                 if (!name[0] || !f_cpu) {
107                                         fprintf(stderr, "%s: -mcu and -freq are mandatory to load .hex files\n", argv[0]);
108                                         exit(1);
109                                 }
110                                 ihex_chunk_p chunk = NULL;
111                                 int cnt = read_ihex_chunks(filename, &chunk);
112                                 if (cnt <= 0) {
113                                         fprintf(stderr, "%s: Unable to load IHEX file %s\n", 
114                                                 argv[0], argv[pi]);
115                                         exit(1);
116                                 }
117                                 printf("Loaded %d section of ihex\n", cnt);
118                                 for (int ci = 0; ci < cnt; ci++) {
119                                         if (chunk[ci].baseaddr < (1*1024*1024)) {
120                                                 f.flash = chunk[ci].data;
121                                                 f.flashsize = chunk[ci].size;
122                                                 f.flashbase = chunk[ci].baseaddr;
123                                                 printf("Load HEX flash %08x, %d\n", f.flashbase, f.flashsize);
124                                         } else if (chunk[ci].baseaddr >= AVR_SEGMENT_OFFSET_EEPROM ||
125                                                         chunk[ci].baseaddr + loadBase >= AVR_SEGMENT_OFFSET_EEPROM) {
126                                                 // eeprom!
127                                                 f.eeprom = chunk[ci].data;
128                                                 f.eesize = chunk[ci].size;
129                                                 printf("Load HEX eeprom %08x, %d\n", chunk[ci].baseaddr, f.eesize);
130                                         }
131                                 }
132                         } else {
133                                 elf_read_firmware(filename, &f);
134                         }
135                 }
136         }
137
138         if (strlen(name))
139                 strcpy(f.mmcu, name);
140         if (f_cpu)
141                 f.frequency = f_cpu;
142
143         avr = avr_make_mcu_by_name(f.mmcu);
144         if (!avr) {
145                 fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu);
146                 exit(1);
147         }
148         avr_init(avr);
149         avr_load_firmware(avr, &f);
150         if (f.flashbase) {
151                 printf("Attempted to load a bootloader at %04x\n", f.flashbase);
152                 avr->pc = f.flashbase;
153         }
154         avr->trace = trace;
155         for (int ti = 0; ti < trace_vectors_count; ti++)
156                 if (avr->interrupts.vector[trace_vectors[ti]])
157                         avr->interrupts.vector[trace_vectors[ti]]->trace = 1;
158
159         // even if not setup at startup, activate gdb if crashing
160         avr->gdb_port = 1234;
161         if (gdb) {
162                 avr->state = cpu_Stopped;
163                 avr_gdb_init(avr);
164         }
165
166         signal(SIGINT, sig_int);
167         signal(SIGTERM, sig_int);
168
169         for (;;) {
170                 int state = avr_run(avr);
171                 if ( state == cpu_Done || state == cpu_Crashed)
172                         break;
173         }
174         
175         avr_terminate(avr);
176 }