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