core: Added a new ELF tag with AVR->simavr command path
[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 #include "avr_uart.h"
30 #include "sim_vcd_file.h"
31 #include "avr_mcu_section.h"
32
33
34 int avr_init(avr_t * avr)
35 {
36         avr->flash = malloc(avr->flashend + 1);
37         memset(avr->flash, 0xff, avr->flashend + 1);
38         avr->data = malloc(avr->ramend + 1);
39         memset(avr->data, 0, avr->ramend + 1);
40
41         // cpu is in limbo before init is finished.
42         avr->state = cpu_Limbo;
43         avr->frequency = 1000000;       // can be overriden via avr_mcu_section
44         if (avr->init)
45                 avr->init(avr);
46         avr->state = cpu_Running;
47         avr_reset(avr); 
48         return 0;
49 }
50
51 void avr_terminate(avr_t * avr)
52 {
53         if (avr->vcd)
54                 avr_vcd_close(avr->vcd);
55         avr->vcd = NULL;
56 }
57
58 void avr_reset(avr_t * avr)
59 {
60         memset(avr->data, 0x0, avr->ramend + 1);
61         _avr_sp_set(avr, avr->ramend);
62         avr->pc = 0;
63         for (int i = 0; i < 8; i++)
64                 avr->sreg[i] = 0;
65         if (avr->reset)
66                 avr->reset(avr);
67
68         avr_io_t * port = avr->io_port;
69         while (port) {
70                 if (port->reset)
71                         port->reset(port);
72                 port = port->next;
73         }
74 }
75
76 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
77 {
78         printf("%s\n", __FUNCTION__);
79         avr->state = cpu_Stopped;
80         if (avr->gdb_port) {
81                 // enable gdb server, and wait
82                 if (!avr->gdb)
83                         avr_gdb_init(avr);
84         } 
85         if (!avr->gdb)
86                 exit(1); // no gdb ?
87 }
88
89 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
90 {
91         printf("%s %02x\n", __FUNCTION__, v);
92         switch (v) {
93                 case SIMAVR_CMD_VCD_START_TRACE:
94                         if (avr->vcd)
95                                 avr_vcd_start(avr->vcd);
96                         break;
97                 case SIMAVR_CMD_VCD_STOP_TRACE:
98                         if (avr->vcd)
99                                 avr_vcd_stop(avr->vcd);
100                         break;
101                 case SIMAVR_CMD_UART_LOOPBACK: {
102                         avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
103                         avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
104                         if (src && dst) {
105                                 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
106                                 avr_connect_irq(src, dst);
107                         }
108                 }       break;
109
110         }
111 }
112
113 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
114 {
115         if (addr)
116                 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
117 }
118
119 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
120 {
121         memcpy(avr->flash + address, code, size);
122 }
123
124 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
125 {
126         if (addr > avr->ramend) {
127                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
128                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
129                 CRASH();
130         }
131         if (addr < 32) {
132                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
133                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
134                 CRASH();
135         }
136 #if AVR_STACK_WATCH
137         /*
138          * this checks that the current "function" is not doctoring the stack frame that is located
139          * higher on the stack than it should be. It's a sign of code that has overrun it's stack
140          * frame and is munching on it's own return address.
141          */
142         if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
143                 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
144         }
145 #endif
146         avr->data[addr] = v;
147 }
148
149 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
150 {
151         if (addr > avr->ramend) {
152                 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
153                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
154                 CRASH();
155         }
156         return avr->data[addr];
157 }
158
159 // converts a number of usec to a number of machine cycles, at current speed
160 avr_cycle_count_t avr_usec_to_cycles(avr_t * avr, uint32_t usec)
161 {
162         return avr->frequency * (avr_cycle_count_t)usec / 1000000;
163 }
164
165 uint32_t avr_cycles_to_usec(avr_t * avr, avr_cycle_count_t cycles)
166 {
167         return 1000000 * cycles / avr->frequency;
168 }
169
170 // converts a number of hz (to megahertz etc) to a number of cycle
171 avr_cycle_count_t avr_hz_to_cycles(avr_t * avr, uint32_t hz)
172 {
173         return avr->frequency / hz;
174 }
175
176 void avr_cycle_timer_register(avr_t * avr, avr_cycle_count_t when, avr_cycle_timer_t timer, void * param)
177 {
178         avr_cycle_timer_cancel(avr, timer, param);
179
180         if (avr->cycle_timer_map == 0xffffffff) {
181                 fprintf(stderr, "avr_cycle_timer_register is full!\n");
182                 return;
183         }
184         when += avr->cycle;
185         for (int i = 0; i < 32; i++)
186                 if (!(avr->cycle_timer_map & (1 << i))) {
187                         avr->cycle_timer[i].timer = timer;
188                         avr->cycle_timer[i].param = param;
189                         avr->cycle_timer[i].when = when;
190                         avr->cycle_timer_map |= (1 << i);
191                         return;
192                 }
193 }
194
195 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param)
196 {
197         avr_cycle_timer_register(avr, avr_usec_to_cycles(avr, when), timer, param);
198 }
199
200 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param)
201 {
202         if (!avr->cycle_timer_map)
203                 return;
204         for (int i = 0; i < 32; i++)
205                 if ((avr->cycle_timer_map & (1 << i)) &&
206                                 avr->cycle_timer[i].timer == timer &&
207                                 avr->cycle_timer[i].param == param) {
208                         avr->cycle_timer[i].timer = NULL;
209                         avr->cycle_timer[i].param = NULL;
210                         avr->cycle_timer[i].when = 0;
211                         avr->cycle_timer_map &= ~(1 << i);
212                         return;
213                 }
214 }
215
216 /*
217  * run thru all the timers, call the ones that needs it,
218  * clear the ones that wants it, and calculate the next
219  * potential cycle we could sleep for...
220  */
221 static avr_cycle_count_t avr_cycle_timer_check(avr_t * avr)
222 {
223         if (!avr->cycle_timer_map)
224                 return (avr_cycle_count_t)-1;
225
226         avr_cycle_count_t min = (avr_cycle_count_t)-1;
227
228         for (int i = 0; i < 32; i++) {
229                 if (!(avr->cycle_timer_map & (1 << i)))
230                         continue;
231                 // do it several times, in case we're late
232                 while (avr->cycle_timer[i].when && avr->cycle_timer[i].when <= avr->cycle) {
233                         // call it
234                         avr->cycle_timer[i].when =
235                                         avr->cycle_timer[i].timer(avr,
236                                                         avr->cycle_timer[i].when,
237                                                         avr->cycle_timer[i].param);
238                         if (avr->cycle_timer[i].when == 0) {
239                                 // clear it
240                                 avr->cycle_timer[i].timer = NULL;
241                                 avr->cycle_timer[i].param = NULL;
242                                 avr->cycle_timer[i].when = 0;
243                                 avr->cycle_timer_map &= ~(1 << i);
244                                 break;
245                         }
246                 }
247                 if (avr->cycle_timer[i].when && avr->cycle_timer[i].when < min)
248                         min = avr->cycle_timer[i].when;
249         }
250         return min - avr->cycle;
251 }
252
253 int avr_run(avr_t * avr)
254 {
255         avr_gdb_processor(avr, avr->state == cpu_Stopped);
256
257         if (avr->state == cpu_Stopped)
258                 return avr->state;
259
260         // if we are stepping one instruction, we "run" for one..
261         int step = avr->state == cpu_Step;
262         if (step) {
263                 avr->state = cpu_Running;
264         }
265         
266         uint16_t new_pc = avr->pc;
267
268         if (avr->state == cpu_Running) {
269                 new_pc = avr_run_one(avr);
270 #if CONFIG_SIMAVR_TRACE
271                 avr_dump_state(avr);
272 #endif
273         }
274
275         // if we just re-enabled the interrupts...
276         if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
277         //      printf("*** %s: Renabling interrupts\n", __FUNCTION__);
278                 avr->pending_wait++;
279         }
280         avr_io_t * port = avr->io_port;
281         while (port) {
282                 if (port->run)
283                         port->run(port);
284                 port = port->next;
285         }
286         avr_cycle_count_t sleep = avr_cycle_timer_check(avr);
287
288         avr->pc = new_pc;
289
290         if (avr->state == cpu_Sleeping) {
291                 if (!avr->sreg[S_I]) {
292                         printf("simavr: sleeping with interrupts off, quitting gracefully\n");
293                         avr_terminate(avr);
294                         exit(0);
295                 }
296                 /*
297                  * try to sleep for as long as we can (?)
298                  */
299                 uint32_t usec = avr_cycles_to_usec(avr, sleep);
300         //      printf("sleep usec %d cycles %d\n", usec, sleep);
301                 if (avr->gdb) {
302                         while (avr_gdb_processor(avr, usec))
303                                 ;
304                 } else
305                         usleep(usec);
306                 avr->cycle += 1 + sleep;
307         }
308         // Interrupt servicing might change the PC too
309         if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
310                 avr_service_interrupts(avr);
311
312                 avr->data[R_SREG] = 0;
313                 for (int i = 0; i < 8; i++)
314                         if (avr->sreg[i] > 1) {
315                                 printf("** Invalid SREG!!\n");
316                                 CRASH();
317                         } else if (avr->sreg[i])
318                                 avr->data[R_SREG] |= (1 << i);
319         }
320
321         if (step) {
322                 avr->state = cpu_StepDone;
323         }
324
325         return avr->state;
326 }
327
328
329 extern avr_kind_t tiny13;
330 extern avr_kind_t tiny2313;
331 extern avr_kind_t tiny25,tiny45,tiny85;
332 extern avr_kind_t mega48,mega88,mega168,mega328;
333 extern avr_kind_t mega164,mega324,mega644;
334
335 avr_kind_t * avr_kind[] = {
336         &tiny13,
337         &tiny2313,
338         &tiny25, &tiny45, &tiny85,
339         &mega48, &mega88, &mega168, &mega328,
340         &mega164, &mega324, &mega644,
341         NULL
342 };
343
344 avr_t * avr_make_mcu_by_name(const char *name)
345 {
346         avr_kind_t * maker = NULL;
347         for (int i = 0; avr_kind[i] && !maker; i++) {
348                 for (int j = 0; avr_kind[i]->names[j]; j++)
349                         if (!strcmp(avr_kind[i]->names[j], name)) {
350                                 maker = avr_kind[i];
351                                 break;
352                         }
353         }
354         if (!maker) {
355                 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
356                 return NULL;
357         }
358
359         avr_t * avr = maker->make();
360         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
361         return avr;     
362 }
363