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