Polished gdb support, etc
[simavr] / simavr / sim / sim_avr.c
1 /*
2         sim_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 <string.h>
25 #include <unistd.h>
26 #include "sim_avr.h"
27 #include "sim_core.h"
28 #include "sim_gdb.h"
29
30
31 int avr_init(avr_t * avr)
32 {
33         avr->flash = malloc(avr->flashend + 1);
34         memset(avr->flash, 0xff, avr->flashend + 1);
35         avr->data = malloc(avr->ramend + 1);
36         memset(avr->data, 0, avr->ramend + 1);
37
38         // cpu is in limbo before init is finished.
39         avr->state = cpu_Limbo;
40         avr->frequency = 1000000;       // can be overriden via avr_mcu_section
41         if (avr->init)
42                 avr->init(avr);
43         avr->state = cpu_Running;
44         avr_reset(avr); 
45         return 0;
46 }
47
48 void avr_reset(avr_t * avr)
49 {
50         memset(avr->data, 0x0, avr->ramend + 1);
51         _avr_sp_set(avr, avr->ramend);
52         avr->pc = 0;
53         for (int i = 0; i < 8; i++)
54                 avr->sreg[i] = 0;
55         if (avr->reset)
56                 avr->reset(avr);
57
58         avr_io_t * port = avr->io_port;
59         while (port) {
60                 if (port->reset)
61                         port->reset(port);
62                 port = port->next;
63         }
64 }
65
66 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
67 {
68         avr->state = cpu_Stopped;
69         if (avr->gdb_port) {
70                 // enable gdb server, and wait
71                 if (!avr->gdb)
72                         avr_gdb_init(avr);
73         } 
74         if (!avr->gdb)
75                 exit(1); // no gdb ?
76 }
77
78 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
79 {
80         memcpy(avr->flash + address, code, size);
81 }
82
83 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
84 {
85         if (addr > avr->ramend) {
86                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
87                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
88                 CRASH();
89         }
90         if (addr < 32) {
91                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
92                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
93                 CRASH();
94         }
95 #if AVR_STACK_WATCH
96         /*
97          * this checks that the current "function" is not doctoring the stack frame that is located
98          * higher on the stack than it should be. It's a sign of code that has overrun it's stack
99          * frame and is munching on it's own return address.
100          */
101         if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
102                 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
103         }
104 #endif
105         avr->data[addr] = v;
106 }
107
108 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
109 {
110         if (addr > avr->ramend) {
111                 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
112                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
113                 CRASH();
114         }
115         return avr->data[addr];
116 }
117
118
119 int avr_run(avr_t * avr)
120 {
121         avr_gdb_processor(avr, avr->state == cpu_Stopped);
122
123         if (avr->state == cpu_Stopped)
124                 return avr->state;
125
126         // if we are stepping one insruction, we "run" for one..
127         int step = avr->state == cpu_Step;
128         if (step) {
129                 avr->state = cpu_Running;
130         }
131         
132         uint16_t new_pc = avr->pc;
133
134         if (avr->state == cpu_Running) {
135                 new_pc = avr_run_one(avr);
136                 avr_dump_state(avr);
137         } else
138                 avr->cycle ++;
139
140         // re-synth the SREG
141         //SREG();
142         // if we just re-enabled the interrupts...
143         if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
144         //      printf("*** %s: Renabling interrupts\n", __FUNCTION__);
145                 avr->pending_wait++;
146         }
147         avr_io_t * port = avr->io_port;
148         while (port) {
149                 if (port->run)
150                         port->run(port);
151                 port = port->next;
152         }
153
154         avr->pc = new_pc;
155
156         if (avr->state == cpu_Sleeping) {
157                 if (!avr->sreg[S_I]) {
158                         printf("simavr: sleeping with interrupts off, quitting gracefuly\n");
159                         exit(0);
160                 }
161                 if (avr->gdb) {
162                         while (avr_gdb_processor(avr, 1))
163                                 ;
164                 } else
165                         usleep(500);
166                 long sleep = (float)avr->frequency * (1.0f / 500.0f);
167                 avr->cycle += sleep;
168         //      avr->state = cpu_Running;
169         }
170         // Interrupt servicing might change the PC too
171         if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
172                 avr_service_interrupts(avr);
173
174                 avr->data[R_SREG] = 0;
175                 for (int i = 0; i < 8; i++)
176                         if (avr->sreg[i] > 1) {
177                                 printf("** Invalid SREG!!\n");
178                                 CRASH();
179                         } else if (avr->sreg[i])
180                                 avr->data[R_SREG] |= (1 << i);
181         }
182
183         if (step) {
184                 avr->state = cpu_StepDone;
185         }
186
187         return avr->state;
188 }
189
190
191 extern avr_kind_t tiny85;
192 extern avr_kind_t mega48,mega88,mega168;
193 extern avr_kind_t mega644;
194
195 avr_kind_t * avr_kind[] = {
196         &tiny85,
197         &mega48,
198         &mega88,
199         &mega168,
200         &mega644,
201         NULL
202 };
203
204 avr_t * avr_make_mcu_by_name(const char *name)
205 {
206         avr_kind_t * maker = NULL;
207         for (int i = 0; avr_kind[i] && !maker; i++) {
208                 for (int j = 0; avr_kind[i]->names[j]; j++)
209                         if (!strcmp(avr_kind[i]->names[j], name)) {
210                                 maker = avr_kind[i];
211                                 break;
212                         }
213         }
214         if (!maker) {
215                 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
216                 return NULL;
217         }
218
219         avr_t * avr = maker->make();
220         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
221         return avr;     
222 }
223