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