core: Introduce run() and sleep() callbacks
[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->special_init)
45                 avr->special_init(avr);
46         if (avr->init)
47                 avr->init(avr);
48         // set default (non gdb) fast callbacks
49         avr->run = avr_callback_run_raw;
50         avr->sleep = avr_callback_sleep_raw;
51         avr->state = cpu_Running;
52         avr_reset(avr); 
53         return 0;
54 }
55
56 void avr_terminate(avr_t * avr)
57 {
58         if (avr->special_deinit)
59                 avr->special_deinit(avr);
60         if (avr->vcd) {
61                 avr_vcd_close(avr->vcd);
62                 avr->vcd = NULL;
63         }
64         avr_deallocate_ios(avr);
65
66         if (avr->flash) free(avr->flash);
67         if (avr->data) free(avr->data);
68         avr->flash = avr->data = NULL;
69 }
70
71 void avr_reset(avr_t * avr)
72 {
73         memset(avr->data, 0x0, avr->ramend + 1);
74         _avr_sp_set(avr, avr->ramend);
75         avr->pc = 0;
76         for (int i = 0; i < 8; i++)
77                 avr->sreg[i] = 0;
78         if (avr->reset)
79                 avr->reset(avr);
80
81         avr_io_t * port = avr->io_port;
82         while (port) {
83                 if (port->reset)
84                         port->reset(port);
85                 port = port->next;
86         }
87 }
88
89 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
90 {
91         printf("%s\n", __FUNCTION__);
92         avr->state = cpu_Stopped;
93         if (avr->gdb_port) {
94                 // enable gdb server, and wait
95                 if (!avr->gdb)
96                         avr_gdb_init(avr);
97         } 
98         if (!avr->gdb)
99                 exit(1); // no gdb ?
100 }
101
102 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
103 {
104         printf("%s %02x\n", __FUNCTION__, v);
105         switch (v) {
106                 case SIMAVR_CMD_VCD_START_TRACE:
107                         if (avr->vcd)
108                                 avr_vcd_start(avr->vcd);
109                         break;
110                 case SIMAVR_CMD_VCD_STOP_TRACE:
111                         if (avr->vcd)
112                                 avr_vcd_stop(avr->vcd);
113                         break;
114                 case SIMAVR_CMD_UART_LOOPBACK: {
115                         avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
116                         avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
117                         if (src && dst) {
118                                 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
119                                 avr_connect_irq(src, dst);
120                         }
121                 }       break;
122
123         }
124 }
125
126 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
127 {
128         if (addr)
129                 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
130 }
131
132 static void _avr_io_console_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
133 {
134         static char * buf = NULL;
135         static int size = 0, len = 0;
136
137         if (v == '\r') {
138                 printf("O:" "%s" "" "\n", buf);
139                 fflush(stdout);
140         }
141         if (len + 1 >= size) {
142                 size += 128;
143                 buf = (char*)realloc(buf, size);
144         }
145         buf[len++] = v;
146 }
147
148 void avr_set_console_register(avr_t * avr, avr_io_addr_t addr)
149 {
150         if (addr)
151                 avr_register_io_write(avr, addr, _avr_io_console_write, NULL);
152 }
153
154 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
155 {
156         if (size > avr->flashend+1) {
157                 fprintf(stderr, "avr_loadcode(): Attempted to load code of size %d but flash size is only %d.\n",
158                         size, avr->flashend+1);
159                 abort();
160         }
161         memcpy(avr->flash + address, code, size);
162 }
163
164 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong)
165 {
166         uint32_t usec = avr_cycles_to_usec(avr, howLong);
167         while (avr_gdb_processor(avr, usec))
168                 ;
169 }
170
171 void avr_callback_run_gdb(avr_t * avr)
172 {
173         avr_gdb_processor(avr, avr->state == cpu_Stopped);
174
175         if (avr->state == cpu_Stopped)
176                 return ;
177
178         // if we are stepping one instruction, we "run" for one..
179         int step = avr->state == cpu_Step;
180         if (step)
181                 avr->state = cpu_Running;
182         
183         uint16_t new_pc = avr->pc;
184
185         if (avr->state == cpu_Running) {
186                 new_pc = avr_run_one(avr);
187 #if CONFIG_SIMAVR_TRACE
188                 avr_dump_state(avr);
189 #endif
190         }
191
192         // if we just re-enabled the interrupts...
193         // double buffer the I flag, to detect that edge
194         if (avr->sreg[S_I] && !avr->i_shadow)
195                 avr->pending_wait++;
196         avr->i_shadow = avr->sreg[S_I];
197
198         // run the cycle timers, get the suggested sleeo time
199         // until the next timer is due
200         avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
201
202         avr->pc = new_pc;
203
204         if (avr->state == cpu_Sleeping) {
205                 if (!avr->sreg[S_I]) {
206                         printf("simavr: sleeping with interrupts off, quitting gracefully\n");
207                         avr_terminate(avr);
208                         exit(0);
209                 }
210                 /*
211                  * try to sleep for as long as we can (?)
212                  */
213                 avr->sleep(avr, sleep);
214                 avr->cycle += 1 + sleep;
215         }
216         // Interrupt servicing might change the PC too, during 'sleep'
217         if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
218                 avr_service_interrupts(avr);
219         
220         // if we were stepping, use this state to inform remote gdb
221         if (step)
222                 avr->state = cpu_StepDone;
223
224 }
225
226 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong)
227 {
228         uint32_t usec = avr_cycles_to_usec(avr, howLong);
229         usleep(usec);
230 }
231
232 void avr_callback_run_raw(avr_t * avr)
233 {
234
235         uint16_t new_pc = avr->pc;
236
237         if (avr->state == cpu_Running) {
238                 new_pc = avr_run_one(avr);
239 #if CONFIG_SIMAVR_TRACE
240                 avr_dump_state(avr);
241 #endif
242         }
243
244         // if we just re-enabled the interrupts...
245         // double buffer the I flag, to detect that edge
246         if (avr->sreg[S_I] && !avr->i_shadow)
247                 avr->pending_wait++;
248         avr->i_shadow = avr->sreg[S_I];
249
250         // run the cycle timers, get the suggested sleeo time
251         // until the next timer is due
252         avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
253
254         avr->pc = new_pc;
255
256         if (avr->state == cpu_Sleeping) {
257                 if (!avr->sreg[S_I]) {
258                         printf("simavr: sleeping with interrupts off, quitting gracefully\n");
259                         avr_terminate(avr);
260                         exit(0);
261                 }
262                 /*
263                  * try to sleep for as long as we can (?)
264                  */
265                 avr->sleep(avr, sleep);
266                 avr->cycle += 1 + sleep;
267         }
268         // Interrupt servicing might change the PC too, during 'sleep'
269         if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
270                 avr_service_interrupts(avr);
271 }
272
273
274 int avr_run(avr_t * avr)
275 {
276         avr->run(avr);
277         return avr->state;
278 }
279
280
281 extern avr_kind_t tiny13;
282 extern avr_kind_t tiny2313;
283 extern avr_kind_t tiny25,tiny45,tiny85;
284 extern avr_kind_t tiny24,tiny44,tiny84;
285 extern avr_kind_t mega48,mega88,mega168,mega328;
286 extern avr_kind_t mega164,mega324,mega644;
287 extern avr_kind_t mega128;
288
289 avr_kind_t * avr_kind[] = {
290         &tiny13,
291         &tiny2313,
292         &tiny25, &tiny45, &tiny85,
293         &tiny24, &tiny44, &tiny84,
294         &mega48, &mega88, &mega168, &mega328,
295         &mega164, &mega324, &mega644,
296         &mega128,
297         NULL
298 };
299
300 avr_t * avr_make_mcu_by_name(const char *name)
301 {
302         avr_kind_t * maker = NULL;
303         for (int i = 0; avr_kind[i] && !maker; i++) {
304                 for (int j = 0; avr_kind[i]->names[j]; j++)
305                         if (!strcmp(avr_kind[i]->names[j], name)) {
306                                 maker = avr_kind[i];
307                                 break;
308                         }
309         }
310         if (!maker) {
311                 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
312                 return NULL;
313         }
314
315         avr_t * avr = maker->make();
316         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
317         return avr;     
318 }
319