Added tiny13
[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 // converts a number of usec to a number of machine cycles, at current speed
119 uint64_t avr_usec_to_cycles(avr_t * avr, uint32_t usec)
120 {
121         return avr->frequency * (uint64_t)usec / 1000000;
122 }
123
124 uint32_t avr_cycles_to_usec(avr_t * avr, uint64_t cycles)
125 {
126         return 1000000 * cycles / avr->frequency;
127 }
128
129 // converts a number of hz (to megahertz etc) to a number of cycle
130 uint64_t avr_hz_to_cycles(avr_t * avr, uint32_t hz)
131 {
132         return avr->frequency / hz;
133 }
134
135 void avr_cycle_timer_register(avr_t * avr, uint64_t when, avr_cycle_timer_t timer, void * param)
136 {
137         avr_cycle_timer_cancel(avr, timer, param);
138
139         if (avr->cycle_timer_map == 0xffffffff) {
140                 fprintf(stderr, "avr_cycle_timer_register is full!\n");
141                 return;
142         }
143         when += avr->cycle;
144         for (int i = 0; i < 32; i++)
145                 if (!(avr->cycle_timer_map & (1 << i))) {
146                         avr->cycle_timer[i].timer = timer;
147                         avr->cycle_timer[i].param = param;
148                         avr->cycle_timer[i].when = when;
149                         avr->cycle_timer_map |= (1 << i);
150                         return;
151                 }
152 }
153
154 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param)
155 {
156         avr_cycle_timer_register(avr, avr_usec_to_cycles(avr, when), timer, param);
157 }
158
159 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param)
160 {
161         if (!avr->cycle_timer_map)
162                 return;
163         for (int i = 0; i < 32; i++)
164                 if ((avr->cycle_timer_map & (1 << i)) &&
165                                 avr->cycle_timer[i].timer == timer &&
166                                 avr->cycle_timer[i].param == param) {
167                         avr->cycle_timer[i].timer = NULL;
168                         avr->cycle_timer[i].param = NULL;
169                         avr->cycle_timer[i].when = 0;
170                         avr->cycle_timer_map &= ~(1 << i);
171                         return;
172                 }
173 }
174
175 /*
176  * run thru all the timers, call the ones that needs it,
177  * clear the ones that wants it, and calculate the next
178  * potential cycle we could sleep for...
179  */
180 static uint64_t avr_cycle_timer_check(avr_t * avr)
181 {
182         if (!avr->cycle_timer_map)
183                 return (uint32_t)-1;
184
185         uint64_t min = (uint64_t)-1;
186
187         for (int i = 0; i < 32; i++) {
188                 if (!(avr->cycle_timer_map & (1 << i)))
189                         continue;
190
191                 if (avr->cycle_timer[i].when <= avr->cycle) {
192                         // call it
193                         avr->cycle_timer[i].when =
194                                         avr->cycle_timer[i].timer(avr,
195                                                         avr->cycle_timer[i].when,
196                                                         avr->cycle_timer[i].param);
197                         if (avr->cycle_timer[i].when == 0) {
198                                 // clear it
199                                 avr->cycle_timer[i].timer = NULL;
200                                 avr->cycle_timer[i].param = NULL;
201                                 avr->cycle_timer[i].when = 0;
202                                 avr->cycle_timer_map &= ~(1 << i);
203                                 continue;
204                         }
205                 }
206                 if (avr->cycle_timer[i].when < min)
207                         min = avr->cycle_timer[i].when;
208         }
209         return min - avr->cycle;
210 }
211
212 int avr_run(avr_t * avr)
213 {
214         avr_gdb_processor(avr, avr->state == cpu_Stopped);
215
216         if (avr->state == cpu_Stopped)
217                 return avr->state;
218
219         // if we are stepping one instruction, we "run" for one..
220         int step = avr->state == cpu_Step;
221         if (step) {
222                 avr->state = cpu_Running;
223         }
224         
225         uint16_t new_pc = avr->pc;
226
227         if (avr->state == cpu_Running) {
228                 new_pc = avr_run_one(avr);
229                 avr_dump_state(avr);
230         }
231
232         // if we just re-enabled the interrupts...
233         if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
234         //      printf("*** %s: Renabling interrupts\n", __FUNCTION__);
235                 avr->pending_wait++;
236         }
237         avr_io_t * port = avr->io_port;
238         while (port) {
239                 if (port->run)
240                         port->run(port);
241                 port = port->next;
242         }
243         avr_cycle_count_t sleep = avr_cycle_timer_check(avr);
244
245         avr->pc = new_pc;
246
247         if (avr->state == cpu_Sleeping) {
248                 if (!avr->sreg[S_I]) {
249                         printf("simavr: sleeping with interrupts off, quitting gracefully\n");
250                         exit(0);
251                 }
252                 /*
253                  * try to sleep for as long as we can (?)
254                  */
255                 uint32_t usec = avr_cycles_to_usec(avr, sleep);
256                 if (avr->gdb) {
257                         while (avr_gdb_processor(avr, usec))
258                                 ;
259                 } else
260                         usleep(usec);
261                 avr->cycle += sleep;
262         }
263         // Interrupt servicing might change the PC too
264         if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
265                 avr_service_interrupts(avr);
266
267                 avr->data[R_SREG] = 0;
268                 for (int i = 0; i < 8; i++)
269                         if (avr->sreg[i] > 1) {
270                                 printf("** Invalid SREG!!\n");
271                                 CRASH();
272                         } else if (avr->sreg[i])
273                                 avr->data[R_SREG] |= (1 << i);
274         }
275
276         if (step) {
277                 avr->state = cpu_StepDone;
278         }
279
280         return avr->state;
281 }
282
283
284 extern avr_kind_t tiny13;
285 extern avr_kind_t tiny25,tiny45,tiny85;
286 extern avr_kind_t mega48,mega88,mega168;
287 extern avr_kind_t mega644;
288
289 avr_kind_t * avr_kind[] = {
290         &tiny13,
291         &tiny25,
292         &tiny45,
293         &tiny85,
294         &mega48,
295         &mega88,
296         &mega168,
297         &mega644,
298         NULL
299 };
300
301 avr_t * avr_make_mcu_by_name(const char *name)
302 {
303         avr_kind_t * maker = NULL;
304         for (int i = 0; avr_kind[i] && !maker; i++) {
305                 for (int j = 0; avr_kind[i]->names[j]; j++)
306                         if (!strcmp(avr_kind[i]->names[j], name)) {
307                                 maker = avr_kind[i];
308                                 break;
309                         }
310         }
311         if (!maker) {
312                 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
313                 return NULL;
314         }
315
316         avr_t * avr = maker->make();
317         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
318         return avr;     
319 }
320