GDB working, some more source massaging
[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(avr, port);
62                 port = port->next;
63         }
64 }
65
66
67 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
68 {
69         memcpy(avr->flash + address, code, size);
70 }
71
72 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
73 {
74         if (addr > avr->ramend) {
75                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
76                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
77                 CRASH();
78         }
79         if (addr < 32) {
80                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
81                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
82                 CRASH();
83         }
84 #if AVR_STACK_WATCH
85         /*
86          * this checks that the current "function" is not doctoring the stack frame that is located
87          * higher on the stack than it should be. It's a sign of code that has overrun it's stack
88          * frame and is munching on it's own return address.
89          */
90         if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
91                 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
92         }
93 #endif
94         avr->data[addr] = v;
95 }
96
97 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
98 {
99         if (addr > avr->ramend) {
100                 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
101                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
102                 CRASH();
103         }
104         return avr->data[addr];
105 }
106
107
108 int avr_run(avr_t * avr)
109 {
110         avr_gdb_processor(avr);
111
112         if (avr->state == cpu_Stopped) {
113                 usleep(500);
114                 return avr->state;
115         }
116
117         int step = avr->state == cpu_Step;
118         if (step) {
119                 avr->state = cpu_Running;
120         }
121         
122         uint16_t new_pc = avr->pc;
123
124         if (avr->state == cpu_Running) {
125                 new_pc = avr_run_one(avr);
126                 avr_dump_state(avr);
127         } else
128                 avr->cycle ++;
129
130         // re-synth the SREG
131         //SREG();
132         // if we just re-enabled the interrupts...
133         if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
134         //      printf("*** %s: Renabling interrupts\n", __FUNCTION__);
135                 avr->pending_wait++;
136         }
137         avr_io_t * port = avr->io_port;
138         while (port) {
139                 if (port->run)
140                         port->run(avr, port);
141                 port = port->next;
142         }
143
144         avr->pc = new_pc;
145
146         if (avr->state == cpu_Sleeping) {
147                 if (!avr->sreg[S_I]) {
148                         printf("simavr: sleeping with interrupts off, quitting gracefuly\n");
149                         exit(0);
150                 }
151                 usleep(500);
152                 long sleep = (float)avr->frequency * (1.0f / 500.0f);
153                 avr->cycle += sleep;
154         //      avr->state = cpu_Running;
155         }
156         // Interrupt servicing might change the PC too
157         if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
158                 avr_service_interrupts(avr);
159
160                 avr->data[R_SREG] = 0;
161                 for (int i = 0; i < 8; i++)
162                         if (avr->sreg[i] > 1) {
163                                 printf("** Invalid SREG!!\n");
164                                 CRASH();
165                         } else if (avr->sreg[i])
166                                 avr->data[R_SREG] |= (1 << i);
167         }
168
169         if (step) {
170                 avr->state = cpu_StepDone;
171         }
172
173         return avr->state;
174 }
175
176
177 extern avr_kind_t tiny85;
178 extern avr_kind_t mega48,mega88,mega168;
179 extern avr_kind_t mega644;
180
181 avr_kind_t * avr_kind[] = {
182         &tiny85,
183         &mega48,
184         &mega88,
185         &mega168,
186         &mega644,
187         NULL
188 };
189
190 avr_t * avr_make_mcu_by_name(const char *name)
191 {
192         avr_kind_t * maker = NULL;
193         for (int i = 0; avr_kind[i] && !maker; i++) {
194                 for (int j = 0; avr_kind[i]->names[j]; j++)
195                         if (!strcmp(avr_kind[i]->names[j], name)) {
196                                 maker = avr_kind[i];
197                                 break;
198                         }
199         }
200         if (!maker) {
201                 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
202                 return NULL;
203         }
204
205         avr_t * avr = maker->make();
206         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
207         return avr;     
208 }
209