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 #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 // cpu is in limbo before init is finished.
48 avr->state = cpu_Limbo;
49 avr->frequency = 1000000; // can be overridden via avr_mcu_section
50 if (avr->special_init)
51 avr->special_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;
63 void avr_terminate(avr_t * avr)
65 if (avr->special_deinit)
66 avr->special_deinit(avr);
68 avr_vcd_close(avr->vcd);
71 avr_deallocate_ios(avr);
73 if (avr->flash) free(avr->flash);
74 if (avr->data) free(avr->data);
75 avr->flash = avr->data = NULL;
78 void avr_reset(avr_t * avr)
80 memset(avr->data, 0x0, avr->ramend + 1);
81 _avr_sp_set(avr, avr->ramend);
83 for (int i = 0; i < 8; i++)
87 avr_interrupt_reset(avr);
88 avr_cycle_timer_reset(avr);
89 avr_io_t * port = avr->io_port;
97 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
99 printf("%s\n", __FUNCTION__);
100 avr->state = cpu_Stopped;
102 // enable gdb server, and wait
107 avr->state = cpu_Crashed;
110 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
112 printf("%s %02x\n", __FUNCTION__, v);
114 case SIMAVR_CMD_VCD_START_TRACE:
116 avr_vcd_start(avr->vcd);
118 case SIMAVR_CMD_VCD_STOP_TRACE:
120 avr_vcd_stop(avr->vcd);
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);
126 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
127 avr_connect_irq(src, dst);
134 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
137 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
140 static void _avr_io_console_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
142 static char * buf = NULL;
143 static int size = 0, len = 0;
145 if (v == '\r' && buf) {
147 printf("O:" "%s" "" "\n", buf);
152 if (len + 1 >= size) {
154 buf = (char*)realloc(buf, size);
160 void avr_set_console_register(avr_t * avr, avr_io_addr_t addr)
163 avr_register_io_write(avr, addr, _avr_io_console_write, NULL);
166 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, avr_flashaddr_t address)
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);
173 memcpy(avr->flash + address, code, size);
177 * Accumulates sleep requests (and returns a sleep time of 0) until
178 * a minimum count of requested sleep microseconds are reached
179 * (low amounts cannot be handled accurately).
181 static inline uint32_t avr_pending_sleep_usec(avr_t * avr, avr_cycle_count_t howLong)
183 avr->sleep_usec += avr_cycles_to_usec(avr, howLong);
184 uint32_t usec = avr->sleep_usec;
192 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong)
194 uint32_t usec = avr_pending_sleep_usec(avr, howLong);
195 while (avr_gdb_processor(avr, usec))
199 void avr_callback_run_gdb(avr_t * avr)
201 avr_gdb_processor(avr, avr->state == cpu_Stopped);
203 if (avr->state == cpu_Stopped)
206 // if we are stepping one instruction, we "run" for one..
207 int step = avr->state == cpu_Step;
209 avr->state = cpu_Running;
211 avr_flashaddr_t new_pc = avr->pc;
213 if (avr->state == cpu_Running) {
214 new_pc = avr_run_one(avr);
215 #if CONFIG_SIMAVR_TRACE
220 // if we just re-enabled the interrupts...
221 // double buffer the I flag, to detect that edge
222 if (avr->sreg[S_I] && !avr->i_shadow)
223 avr->interrupts.pending_wait++;
224 avr->i_shadow = avr->sreg[S_I];
226 // run the cycle timers, get the suggested sleep time
227 // until the next timer is due
228 avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
232 if (avr->state == cpu_Sleeping) {
233 if (!avr->sreg[S_I]) {
235 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
236 avr->state = cpu_Done;
240 * try to sleep for as long as we can (?)
242 avr->sleep(avr, sleep);
243 avr->cycle += 1 + sleep;
245 // Interrupt servicing might change the PC too, during 'sleep'
246 if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
247 avr_service_interrupts(avr);
249 // if we were stepping, use this state to inform remote gdb
251 avr->state = cpu_StepDone;
255 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong)
257 uint32_t usec = avr_pending_sleep_usec(avr, howLong);
263 void avr_callback_run_raw(avr_t * avr)
265 avr_flashaddr_t new_pc = avr->pc;
267 if (avr->state == cpu_Running) {
268 new_pc = avr_run_one(avr);
269 #if CONFIG_SIMAVR_TRACE
274 // if we just re-enabled the interrupts...
275 // double buffer the I flag, to detect that edge
276 if (avr->sreg[S_I] && !avr->i_shadow)
277 avr->interrupts.pending_wait++;
278 avr->i_shadow = avr->sreg[S_I];
280 // run the cycle timers, get the suggested sleep time
281 // until the next timer is due
282 avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
286 if (avr->state == cpu_Sleeping) {
287 if (!avr->sreg[S_I]) {
289 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
290 avr->state = cpu_Done;
294 * try to sleep for as long as we can (?)
296 avr->sleep(avr, sleep);
297 avr->cycle += 1 + sleep;
299 // Interrupt servicing might change the PC too, during 'sleep'
300 if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
301 avr_service_interrupts(avr);
305 int avr_run(avr_t * avr)
316 uint8_t * b = malloc(coreLen);
317 memcpy(b, core, coreLen);
322 avr_make_mcu_by_name(
325 avr_kind_t * maker = NULL;
326 for (int i = 0; avr_kind[i] && !maker; i++) {
327 for (int j = 0; avr_kind[i]->names[j]; j++)
328 if (!strcmp(avr_kind[i]->names[j], name)) {
334 fprintf(stderr, "%s: AVR '%s' not known\n", __FUNCTION__, name);
338 avr_t * avr = maker->make();
339 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);