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