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/avr_mcu_section.h"
35 #include "sim_core_decl.h"
37 int avr_init(avr_t * avr)
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));
47 printf("%s init\n", avr->mmcu);
49 // cpu is in limbo before init is finished.
50 avr->state = cpu_Limbo;
51 avr->frequency = 1000000; // can be overridden via avr_mcu_section
52 if (avr->special_init)
53 avr->special_init(avr);
56 // set default (non gdb) fast callbacks
57 avr->run = avr_callback_run_raw;
58 avr->sleep = avr_callback_sleep_raw;
59 avr->state = cpu_Running;
65 void avr_terminate(avr_t * avr)
67 if (avr->special_deinit)
68 avr->special_deinit(avr);
74 avr_vcd_close(avr->vcd);
77 avr_deallocate_ios(avr);
79 if (avr->flash) free(avr->flash);
80 if (avr->data) free(avr->data);
81 avr->flash = avr->data = NULL;
84 void avr_reset(avr_t * avr)
86 printf("%s reset\n", avr->mmcu);
88 memset(avr->data, 0x0, avr->ramend + 1);
89 _avr_sp_set(avr, avr->ramend);
91 for (int i = 0; i < 8; i++)
95 avr_interrupt_reset(avr);
96 avr_cycle_timer_reset(avr);
97 avr_io_t * port = avr->io_port;
105 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
107 printf("%s\n", __FUNCTION__);
108 avr->state = cpu_Stopped;
110 // enable gdb server, and wait
115 avr->state = cpu_Crashed;
118 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
120 printf("%s %02x\n", __FUNCTION__, v);
122 case SIMAVR_CMD_VCD_START_TRACE:
124 avr_vcd_start(avr->vcd);
126 case SIMAVR_CMD_VCD_STOP_TRACE:
128 avr_vcd_stop(avr->vcd);
130 case SIMAVR_CMD_UART_LOOPBACK: {
131 avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
132 avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
134 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
135 avr_connect_irq(src, dst);
142 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
145 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
148 static void _avr_io_console_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
150 static char * buf = NULL;
151 static int size = 0, len = 0;
153 if (v == '\r' && buf) {
155 printf("O:" "%s" "" "\n", buf);
160 if (len + 1 >= size) {
162 buf = (char*)realloc(buf, size);
168 void avr_set_console_register(avr_t * avr, avr_io_addr_t addr)
171 avr_register_io_write(avr, addr, _avr_io_console_write, NULL);
174 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, avr_flashaddr_t address)
176 if (size > avr->flashend+1) {
177 fprintf(stderr, "avr_loadcode(): Attempted to load code of size %d but flash size is only %d.\n",
178 size, avr->flashend+1);
181 memcpy(avr->flash + address, code, size);
185 * Accumulates sleep requests (and returns a sleep time of 0) until
186 * a minimum count of requested sleep microseconds are reached
187 * (low amounts cannot be handled accurately).
189 static inline uint32_t avr_pending_sleep_usec(avr_t * avr, avr_cycle_count_t howLong)
191 avr->sleep_usec += avr_cycles_to_usec(avr, howLong);
192 uint32_t usec = avr->sleep_usec;
200 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong)
202 uint32_t usec = avr_pending_sleep_usec(avr, howLong);
203 while (avr_gdb_processor(avr, usec))
207 void avr_callback_run_gdb(avr_t * avr)
209 avr_gdb_processor(avr, avr->state == cpu_Stopped);
211 if (avr->state == cpu_Stopped)
214 // if we are stepping one instruction, we "run" for one..
215 int step = avr->state == cpu_Step;
217 avr->state = cpu_Running;
219 avr_flashaddr_t new_pc = avr->pc;
221 if (avr->state == cpu_Running) {
222 new_pc = avr_run_one(avr);
223 #if CONFIG_SIMAVR_TRACE
228 // if we just re-enabled the interrupts...
229 // double buffer the I flag, to detect that edge
230 if (avr->sreg[S_I] && !avr->i_shadow)
231 avr->interrupts.pending_wait++;
232 avr->i_shadow = avr->sreg[S_I];
234 // run the cycle timers, get the suggested sleep time
235 // until the next timer is due
236 avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
240 if (avr->state == cpu_Sleeping) {
241 if (!avr->sreg[S_I]) {
243 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
244 avr->state = cpu_Done;
248 * try to sleep for as long as we can (?)
250 avr->sleep(avr, sleep);
251 avr->cycle += 1 + sleep;
253 // Interrupt servicing might change the PC too, during 'sleep'
254 if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
255 avr_service_interrupts(avr);
257 // if we were stepping, use this state to inform remote gdb
259 avr->state = cpu_StepDone;
263 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong)
265 uint32_t usec = avr_pending_sleep_usec(avr, howLong);
271 void avr_callback_run_raw(avr_t * avr)
273 avr_flashaddr_t new_pc = avr->pc;
275 if (avr->state == cpu_Running) {
276 new_pc = avr_run_one(avr);
277 #if CONFIG_SIMAVR_TRACE
282 // if we just re-enabled the interrupts...
283 // double buffer the I flag, to detect that edge
284 if (avr->sreg[S_I] && !avr->i_shadow)
285 avr->interrupts.pending_wait++;
286 avr->i_shadow = avr->sreg[S_I];
288 // run the cycle timers, get the suggested sleep time
289 // until the next timer is due
290 avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
294 if (avr->state == cpu_Sleeping) {
295 if (!avr->sreg[S_I]) {
297 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
298 avr->state = cpu_Done;
302 * try to sleep for as long as we can (?)
304 avr->sleep(avr, sleep);
305 avr->cycle += 1 + sleep;
307 // Interrupt servicing might change the PC too, during 'sleep'
308 if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
309 avr_service_interrupts(avr);
313 int avr_run(avr_t * avr)
324 uint8_t * b = malloc(coreLen);
325 memcpy(b, core, coreLen);
330 avr_make_mcu_by_name(
333 avr_kind_t * maker = NULL;
334 for (int i = 0; avr_kind[i] && !maker; i++) {
335 for (int j = 0; avr_kind[i]->names[j]; j++)
336 if (!strcmp(avr_kind[i]->names[j], name)) {
342 fprintf(stderr, "%s: AVR '%s' not known\n", __FUNCTION__, name);
346 avr_t * avr = maker->make();
347 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);