4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
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.
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.
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/>.
31 #include "sim_vcd_file.h"
32 #include "avr_mcu_section.h"
35 int avr_init(avr_t * avr)
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));
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);
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;
61 void avr_terminate(avr_t * avr)
63 if (avr->special_deinit)
64 avr->special_deinit(avr);
66 avr_vcd_close(avr->vcd);
69 avr_deallocate_ios(avr);
71 if (avr->flash) free(avr->flash);
72 if (avr->data) free(avr->data);
73 avr->flash = avr->data = NULL;
76 void avr_reset(avr_t * avr)
78 memset(avr->data, 0x0, avr->ramend + 1);
79 _avr_sp_set(avr, avr->ramend);
81 for (int i = 0; i < 8; i++)
86 avr_io_t * port = avr->io_port;
94 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
96 printf("%s\n", __FUNCTION__);
97 avr->state = cpu_Stopped;
99 // enable gdb server, and wait
104 avr->state = cpu_Crashed;
107 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
109 printf("%s %02x\n", __FUNCTION__, v);
111 case SIMAVR_CMD_VCD_START_TRACE:
113 avr_vcd_start(avr->vcd);
115 case SIMAVR_CMD_VCD_STOP_TRACE:
117 avr_vcd_stop(avr->vcd);
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);
123 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
124 avr_connect_irq(src, dst);
131 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
134 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
137 static void _avr_io_console_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
139 static char * buf = NULL;
140 static int size = 0, len = 0;
142 if (v == '\r' && buf) {
144 printf("O:" "%s" "" "\n", buf);
149 if (len + 1 >= size) {
151 buf = (char*)realloc(buf, size);
157 void avr_set_console_register(avr_t * avr, avr_io_addr_t addr)
160 avr_register_io_write(avr, addr, _avr_io_console_write, NULL);
163 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
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);
170 memcpy(avr->flash + address, code, size);
173 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong)
175 uint32_t usec = avr_cycles_to_usec(avr, howLong);
176 while (avr_gdb_processor(avr, usec))
180 void avr_callback_run_gdb(avr_t * avr)
182 avr_gdb_processor(avr, avr->state == cpu_Stopped);
184 if (avr->state == cpu_Stopped)
187 // if we are stepping one instruction, we "run" for one..
188 int step = avr->state == cpu_Step;
190 avr->state = cpu_Running;
192 uint16_t new_pc = avr->pc;
194 if (avr->state == cpu_Running) {
195 new_pc = avr_run_one(avr);
196 #if CONFIG_SIMAVR_TRACE
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)
205 avr->i_shadow = avr->sreg[S_I];
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);
213 if (avr->state == cpu_Sleeping) {
214 if (!avr->sreg[S_I]) {
216 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
218 avr->state = cpu_Done;
222 * try to sleep for as long as we can (?)
224 avr->sleep(avr, sleep);
225 avr->cycle += 1 + sleep;
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);
231 // if we were stepping, use this state to inform remote gdb
233 avr->state = cpu_StepDone;
237 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong)
239 uint32_t usec = avr_cycles_to_usec(avr, howLong);
243 void avr_callback_run_raw(avr_t * avr)
246 uint16_t new_pc = avr->pc;
248 if (avr->state == cpu_Running) {
249 new_pc = avr_run_one(avr);
250 #if CONFIG_SIMAVR_TRACE
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)
259 avr->i_shadow = avr->sreg[S_I];
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);
267 if (avr->state == cpu_Sleeping) {
268 if (!avr->sreg[S_I]) {
270 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
272 avr->state = cpu_Done;
276 * try to sleep for as long as we can (?)
278 avr->sleep(avr, sleep);
279 avr->cycle += 1 + sleep;
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);
287 int avr_run(avr_t * avr)
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;
305 avr_kind_t * avr_kind[] = {
308 &tiny25, &tiny45, &tiny85,
309 &tiny24, &tiny44, &tiny84,
311 &mega48, &mega88, &mega168, &mega328,
312 &mega164, &mega324, &mega644,
319 avr_t * avr_make_mcu_by_name(const char *name)
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)) {
330 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
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);