f39fb3b1ec6a26882ed415a920224853f3ca5eaa
[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_uart.h"
31
32 void hdump(const char *w, uint8_t *b, size_t l)
33 {
34         uint32_t i;
35         if (l < 16) {
36                 printf("%s: ",w);
37                 for (i = 0; i < l; i++) printf("%02x",b[i]);
38         } else {
39                 printf("%s:\n",w);
40                 for (i = 0; i < l; i++) {
41                         if (!(i & 0x1f)) printf("    ");
42                         printf("%02x",b[i]);
43                         if ((i & 0x1f) == 0x1f) {
44                                 printf(" ");
45                                 printf("\n");
46                         }
47                 }
48         }
49         printf("\n");
50 }
51
52
53 void display_usage()
54 {
55         printf("usage: simavr [-t] [-g] [-m <device>] [-f <frequency>] firmware\n");
56         printf("       -t: run full scale decoder trace\n");
57         printf("       -g: listen for gdb connection on port 1234\n");
58         exit(1);
59 }
60
61 int main(int argc, char *argv[])
62 {
63         elf_firmware_t f;
64         long f_cpu = 0;
65         int trace = 0;
66         int gdb = 0;
67         char name[16] = "";
68         int option_count;
69         int option_index = 0;
70
71         struct option long_options[] = {
72                 {"help", no_argument, 0, 'h'},
73                 {"mcu", required_argument, 0, 'm'},
74                 {"freq", required_argument, 0, 'f'},
75                 {"trace", no_argument, 0, 't'},
76                 {"gdb", no_argument, 0, 'g'},
77                 {0, 0, 0, 0}
78         };
79
80         if (argc == 1)
81                 display_usage();
82
83         while ((option_count = getopt_long(argc, argv, "tghm:f:", long_options, &option_index)) != -1) {
84                 switch (option_count) {
85                         case 'h':
86                                 display_usage();
87                                 break;
88                         case 'm':
89                                 strcpy(name, optarg);
90                                 break;
91                         case 'f':
92                                 f_cpu = atoi(optarg);
93                                 break;
94                         case 't':
95                                 trace++;
96                                 break;
97                         case 'g':
98                                 gdb++;
99                                 break;
100                 }
101         }
102
103         elf_read_firmware(argv[argc-1], &f);
104
105         if (strlen(name))
106                 strcpy(f.mmcu, name);
107         if (f_cpu)
108                 f.frequency = f_cpu;
109
110         printf("firmware %s f=%d mmcu=%s\n", argv[argc-1], (int)f.frequency, f.mmcu);
111
112         avr_t * avr = avr_make_mcu_by_name(f.mmcu);
113         if (!avr) {
114                 fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu);
115                 exit(1);
116         }
117         avr_init(avr);
118         avr_load_firmware(avr, &f);
119         avr->trace = trace;
120
121         // try to enable "local echo" on the first uart, for testing purposes
122         {
123                 avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
124                 avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
125                 if (src && dst) {
126                         printf("%s:%s activating uart local echo IRQ src %p dst %p\n", __FILE__, __FUNCTION__, src, dst);
127                         avr_connect_irq(src, dst);
128                 }
129         }
130         // even if not setup at startup, activate gdb if crashing
131         avr->gdb_port = 1234;
132         if (gdb) {
133                 avr->state = cpu_Stopped;
134                 avr_gdb_init(avr);
135         }
136
137         for (;;)
138                 avr_run(avr);
139         
140         avr_terminate(avr);
141 }