GDB working, some more source massaging
[simavr] / simavr / sim / run_avr.c
1 /*
2         run_avr.c
3
4         Copyright 2008, 2009 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 <getopt.h>
25 #include <string.h>
26 #include "sim_avr.h"
27 #include "sim_elf.h"
28 #include "sim_core.h"
29 #include "sim_gdb.h"
30 #include "avr_eeprom.h"
31 #include "avr_uart.h"
32
33 void hdump(const char *w, uint8_t *b, size_t l)
34 {
35         uint32_t i;
36         if (l < 16) {
37                 printf("%s: ",w);
38                 for (i = 0; i < l; i++) printf("%02x",b[i]);
39         } else {
40                 printf("%s:\n",w);
41                 for (i = 0; i < l; i++) {
42                         if (!(i & 0x1f)) printf("    ");
43                         printf("%02x",b[i]);
44                         if ((i & 0x1f) == 0x1f) {
45                                 printf(" ");
46                                 printf("\n");
47                         }
48                 }
49         }
50         printf("\n");
51 }
52
53
54 void display_usage()
55 {
56         printf("usage: simavr [-t] [-g] [-m <device>] [-f <frequency>] firmware\n");
57         printf("       -t: run full scale decoder trace\n");
58         printf("       -g: listen for gdb connection on port 1234\n");
59         exit(1);
60 }
61
62 int main(int argc, char *argv[])
63 {
64         elf_firmware_t f;
65         long f_cpu = 0;
66         int trace = 0;
67         int gdb = 0;
68         char name[16] = "";
69         int option_count;
70         int option_index = 0;
71
72         struct option long_options[] = {
73                 {"help", no_argument, 0, 'h'},
74                 {"mcu", required_argument, 0, 'm'},
75                 {"freq", required_argument, 0, 'f'},
76                 {"trace", no_argument, 0, 't'},
77                 {"gdb", no_argument, 0, 'g'},
78                 {0, 0, 0, 0}
79         };
80
81         if (argc == 1)
82                 display_usage();
83
84         while ((option_count = getopt_long(argc, argv, "tghm:f:", long_options, &option_index)) != -1) {
85                 switch (option_count) {
86                         case 'h':
87                                 display_usage();
88                                 break;
89                         case 'm':
90                                 strcpy(name, optarg);
91                                 break;
92                         case 'f':
93                                 f_cpu = atoi(optarg);
94                                 break;
95                         case 't':
96                                 trace++;
97                                 break;
98                         case 'g':
99                                 gdb++;
100                                 break;
101                 }
102         }
103
104         elf_read_firmware(argv[argc-1], &f);
105
106         if (strlen(name))
107                 strcpy(f.mmcu.name, name);
108         if (f_cpu)
109                 f.mmcu.f_cpu = f_cpu;
110
111         printf("firmware %s f=%d mmcu=%s\n", argv[argc-1], (int)f.mmcu.f_cpu, f.mmcu.name);
112
113         avr_t * avr = avr_make_mcu_by_name(f.mmcu.name);
114         if (!avr) {
115                 fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu.name);
116                 exit(1);
117         }
118         avr_init(avr);
119         avr->frequency = f.mmcu.f_cpu;
120         avr->codeline = f.codeline;
121         avr_loadcode(avr, f.flash, f.flashsize, 0);
122         avr->codeend = f.flashsize - f.datasize;
123         if (f.eeprom && f.eesize) {
124                 avr_eeprom_desc_t d = { .ee = f.eeprom, .offset = 0, .size = f.eesize };
125                 avr_ioctl(avr, AVR_IOCTL_EEPROM_SET, &d);
126         }
127         avr->trace = trace;
128
129         // try to enable "local echo" on the first uart, for testing purposes
130         {
131                 avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
132                 avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
133                 printf("%s:%s activating uart local echo IRQ src %p dst %p\n", __FILE__, __FUNCTION__, src, dst);
134                 if (src && dst)
135                         avr_connect_irq(avr, src, dst);
136         }
137
138         if (gdb) {
139                 avr->state = cpu_Stopped;
140                 avr_gdb_init(avr);
141         }
142
143 //      for (long long i = 0; i < 8000000*10; i++)
144 //      for (long long i = 0; i < 80000; i++)
145         for (;;)
146                 avr_run(avr);
147         
148 }