mingw: make simavr compilable with MinGW
[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_time.h"
29 #include "sim_gdb.h"
30 #include "avr_uart.h"
31 #include "sim_vcd_file.h"
32 #include "avr/avr_mcu_section.h"
33
34 #define AVR_KIND_DECL
35 #include "sim_core_decl.h"
36
37 int avr_init(avr_t * avr)
38 {
39         avr->flash = malloc(avr->flashend + 1);
40         memset(avr->flash, 0xff, avr->flashend + 1);
41         avr->data = malloc(avr->ramend + 1);
42         memset(avr->data, 0, avr->ramend + 1);
43 #ifdef CONFIG_SIMAVR_TRACE
44         avr->trace_data = calloc(1, sizeof(struct avr_trace_data_t));
45 #endif
46         
47         printf("%s init\n", avr->mmcu);
48
49         // cpu is in limbo before init is finished.
50         avr->state = cpu_Limbo;
51         avr->frequency = 1000000;       // can be overridden via avr_mcu_section
52         if (avr->special_init)
53                 avr->special_init(avr);
54         if (avr->init)
55                 avr->init(avr);
56         // set default (non gdb) fast callbacks
57         avr->run = avr_callback_run_raw;
58         avr->sleep = avr_callback_sleep_raw;
59         avr->state = cpu_Running;
60         avr->log = 1;
61         avr_reset(avr); 
62         return 0;
63 }
64
65 void avr_terminate(avr_t * avr)
66 {
67         if (avr->special_deinit)
68                 avr->special_deinit(avr);
69         if (avr->gdb) {
70                 avr_deinit_gdb(avr);
71                 avr->gdb = NULL;
72         }
73         if (avr->vcd) {
74                 avr_vcd_close(avr->vcd);
75                 avr->vcd = NULL;
76         }
77         avr_deallocate_ios(avr);
78
79         if (avr->flash) free(avr->flash);
80         if (avr->data) free(avr->data);
81         avr->flash = avr->data = NULL;
82 }
83
84 void avr_reset(avr_t * avr)
85 {
86         printf("%s reset\n", avr->mmcu);
87
88         memset(avr->data, 0x0, avr->ramend + 1);
89         _avr_sp_set(avr, avr->ramend);
90         avr->pc = 0;
91         for (int i = 0; i < 8; i++)
92                 avr->sreg[i] = 0;
93         if (avr->reset)
94                 avr->reset(avr);
95         avr_interrupt_reset(avr);
96         avr_cycle_timer_reset(avr);
97         avr_io_t * port = avr->io_port;
98         while (port) {
99                 if (port->reset)
100                         port->reset(port);
101                 port = port->next;
102         }
103 }
104
105 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
106 {
107         printf("%s\n", __FUNCTION__);
108         avr->state = cpu_Stopped;
109         if (avr->gdb_port) {
110                 // enable gdb server, and wait
111                 if (!avr->gdb)
112                         avr_gdb_init(avr);
113         } 
114         if (!avr->gdb)
115                 avr->state = cpu_Crashed;
116 }
117
118 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
119 {
120         printf("%s %02x\n", __FUNCTION__, v);
121         switch (v) {
122                 case SIMAVR_CMD_VCD_START_TRACE:
123                         if (avr->vcd)
124                                 avr_vcd_start(avr->vcd);
125                         break;
126                 case SIMAVR_CMD_VCD_STOP_TRACE:
127                         if (avr->vcd)
128                                 avr_vcd_stop(avr->vcd);
129                         break;
130                 case SIMAVR_CMD_UART_LOOPBACK: {
131                         avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
132                         avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
133                         if (src && dst) {
134                                 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
135                                 avr_connect_irq(src, dst);
136                         }
137                 }       break;
138
139         }
140 }
141
142 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
143 {
144         if (addr)
145                 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
146 }
147
148 static void _avr_io_console_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
149 {
150         static char * buf = NULL;
151         static int size = 0, len = 0;
152
153         if (v == '\r' && buf) {
154                 buf[len] = 0;
155                 printf("O:" "%s" "" "\n", buf);
156                 fflush(stdout);
157                 len = 0;
158                 return;
159         }
160         if (len + 1 >= size) {
161                 size += 128;
162                 buf = (char*)realloc(buf, size);
163         }
164         if (v >= ' ')
165                 buf[len++] = v;
166 }
167
168 void avr_set_console_register(avr_t * avr, avr_io_addr_t addr)
169 {
170         if (addr)
171                 avr_register_io_write(avr, addr, _avr_io_console_write, NULL);
172 }
173
174 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, avr_flashaddr_t address)
175 {
176         if (size > avr->flashend+1) {
177                 fprintf(stderr, "avr_loadcode(): Attempted to load code of size %d but flash size is only %d.\n",
178                         size, avr->flashend+1);
179                 abort();
180         }
181         memcpy(avr->flash + address, code, size);
182 }
183
184 /**
185  * Accumulates sleep requests (and returns a sleep time of 0) until
186  * a minimum count of requested sleep microseconds are reached
187  * (low amounts cannot be handled accurately).
188  */
189 static inline uint32_t avr_pending_sleep_usec(avr_t * avr, avr_cycle_count_t howLong)
190 {
191         avr->sleep_usec += avr_cycles_to_usec(avr, howLong);
192         uint32_t usec = avr->sleep_usec;
193         if (usec > 200) {
194                 avr->sleep_usec = 0;
195                 return usec;
196         }
197         return 0;
198 }
199
200 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong)
201 {
202         uint32_t usec = avr_pending_sleep_usec(avr, howLong);
203         while (avr_gdb_processor(avr, usec))
204                 ;
205 }
206
207 void avr_callback_run_gdb(avr_t * avr)
208 {
209         avr_gdb_processor(avr, avr->state == cpu_Stopped);
210
211         if (avr->state == cpu_Stopped)
212                 return ;
213
214         // if we are stepping one instruction, we "run" for one..
215         int step = avr->state == cpu_Step;
216         if (step)
217                 avr->state = cpu_Running;
218         
219         avr_flashaddr_t new_pc = avr->pc;
220
221         if (avr->state == cpu_Running) {
222                 new_pc = avr_run_one(avr);
223 #if CONFIG_SIMAVR_TRACE
224                 avr_dump_state(avr);
225 #endif
226         }
227
228         // if we just re-enabled the interrupts...
229         // double buffer the I flag, to detect that edge
230         if (avr->sreg[S_I] && !avr->i_shadow)
231                 avr->interrupts.pending_wait++;
232         avr->i_shadow = avr->sreg[S_I];
233
234         // run the cycle timers, get the suggested sleep time
235         // until the next timer is due
236         avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
237
238         avr->pc = new_pc;
239
240         if (avr->state == cpu_Sleeping) {
241                 if (!avr->sreg[S_I]) {
242                         if (avr->log)
243                                 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
244                         avr->state = cpu_Done;
245                         return;
246                 }
247                 /*
248                  * try to sleep for as long as we can (?)
249                  */
250                 avr->sleep(avr, sleep);
251                 avr->cycle += 1 + sleep;
252         }
253         // Interrupt servicing might change the PC too, during 'sleep'
254         if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
255                 avr_service_interrupts(avr);
256         
257         // if we were stepping, use this state to inform remote gdb
258         if (step)
259                 avr->state = cpu_StepDone;
260
261 }
262
263 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong)
264 {
265         uint32_t usec = avr_pending_sleep_usec(avr, howLong);
266         if (usec > 0) {
267                 usleep(usec);
268         }
269 }
270
271 void avr_callback_run_raw(avr_t * avr)
272 {
273         avr_flashaddr_t new_pc = avr->pc;
274
275         if (avr->state == cpu_Running) {
276                 new_pc = avr_run_one(avr);
277 #if CONFIG_SIMAVR_TRACE
278                 avr_dump_state(avr);
279 #endif
280         }
281
282         // if we just re-enabled the interrupts...
283         // double buffer the I flag, to detect that edge
284         if (avr->sreg[S_I] && !avr->i_shadow)
285                 avr->interrupts.pending_wait++;
286         avr->i_shadow = avr->sreg[S_I];
287
288         // run the cycle timers, get the suggested sleep time
289         // until the next timer is due
290         avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
291
292         avr->pc = new_pc;
293
294         if (avr->state == cpu_Sleeping) {
295                 if (!avr->sreg[S_I]) {
296                         if (avr->log)
297                                 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
298                         avr->state = cpu_Done;
299                         return;
300                 }
301                 /*
302                  * try to sleep for as long as we can (?)
303                  */
304                 avr->sleep(avr, sleep);
305                 avr->cycle += 1 + sleep;
306         }
307         // Interrupt servicing might change the PC too, during 'sleep'
308         if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
309                 avr_service_interrupts(avr);
310 }
311
312
313 int avr_run(avr_t * avr)
314 {
315         avr->run(avr);
316         return avr->state;
317 }
318
319 avr_t *
320 avr_core_allocate(
321                 const avr_t * core,
322                 uint32_t coreLen)
323 {
324         uint8_t * b = malloc(coreLen);
325         memcpy(b, core, coreLen);
326         return (avr_t *)b;
327 }
328
329 avr_t *
330 avr_make_mcu_by_name(
331                 const char *name)
332 {
333         avr_kind_t * maker = NULL;
334         for (int i = 0; avr_kind[i] && !maker; i++) {
335                 for (int j = 0; avr_kind[i]->names[j]; j++)
336                         if (!strcmp(avr_kind[i]->names[j], name)) {
337                                 maker = avr_kind[i];
338                                 break;
339                         }
340         }
341         if (!maker) {
342                 fprintf(stderr, "%s: AVR '%s' not known\n", __FUNCTION__, name);
343                 return NULL;
344         }
345
346         avr_t * avr = maker->make();
347         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
348         return avr;     
349 }
350