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/>.
30 #include "sim_vcd_file.h"
31 #include "avr_mcu_section.h"
34 int avr_init(avr_t * avr)
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);
41 // cpu is in limbo before init is finished.
42 avr->state = cpu_Limbo;
43 avr->frequency = 1000000; // can be overriden via avr_mcu_section
46 avr->state = cpu_Running;
51 void avr_terminate(avr_t * avr)
54 avr_vcd_close(avr->vcd);
58 void avr_reset(avr_t * avr)
60 memset(avr->data, 0x0, avr->ramend + 1);
61 _avr_sp_set(avr, avr->ramend);
63 for (int i = 0; i < 8; i++)
68 avr_io_t * port = avr->io_port;
76 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
78 printf("%s\n", __FUNCTION__);
79 avr->state = cpu_Stopped;
81 // enable gdb server, and wait
89 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
91 printf("%s %02x\n", __FUNCTION__, v);
93 case SIMAVR_CMD_VCD_START_TRACE:
95 avr_vcd_start(avr->vcd);
97 case SIMAVR_CMD_VCD_STOP_TRACE:
99 avr_vcd_stop(avr->vcd);
101 case SIMAVR_CMD_UART_LOOPBACK: {
102 avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
103 avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
105 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
106 avr_connect_irq(src, dst);
113 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
116 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
119 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
121 memcpy(avr->flash + address, code, size);
124 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
126 if (addr > avr->ramend) {
127 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
128 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
132 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
133 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
138 * this checks that the current "function" is not doctoring the stack frame that is located
139 * higher on the stack than it should be. It's a sign of code that has overrun it's stack
140 * frame and is munching on it's own return address.
142 if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
143 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
149 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
151 if (addr > avr->ramend) {
152 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
153 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
156 return avr->data[addr];
159 // converts a number of usec to a number of machine cycles, at current speed
160 avr_cycle_count_t avr_usec_to_cycles(avr_t * avr, uint32_t usec)
162 return avr->frequency * (avr_cycle_count_t)usec / 1000000;
165 uint32_t avr_cycles_to_usec(avr_t * avr, avr_cycle_count_t cycles)
167 return 1000000 * cycles / avr->frequency;
170 // converts a number of hz (to megahertz etc) to a number of cycle
171 avr_cycle_count_t avr_hz_to_cycles(avr_t * avr, uint32_t hz)
173 return avr->frequency / hz;
176 void avr_cycle_timer_register(avr_t * avr, avr_cycle_count_t when, avr_cycle_timer_t timer, void * param)
178 avr_cycle_timer_cancel(avr, timer, param);
180 if (avr->cycle_timer_map == 0xffffffff) {
181 fprintf(stderr, "avr_cycle_timer_register is full!\n");
185 for (int i = 0; i < 32; i++)
186 if (!(avr->cycle_timer_map & (1 << i))) {
187 avr->cycle_timer[i].timer = timer;
188 avr->cycle_timer[i].param = param;
189 avr->cycle_timer[i].when = when;
190 avr->cycle_timer_map |= (1 << i);
195 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param)
197 avr_cycle_timer_register(avr, avr_usec_to_cycles(avr, when), timer, param);
200 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param)
202 if (!avr->cycle_timer_map)
204 for (int i = 0; i < 32; i++)
205 if ((avr->cycle_timer_map & (1 << i)) &&
206 avr->cycle_timer[i].timer == timer &&
207 avr->cycle_timer[i].param == param) {
208 avr->cycle_timer[i].timer = NULL;
209 avr->cycle_timer[i].param = NULL;
210 avr->cycle_timer[i].when = 0;
211 avr->cycle_timer_map &= ~(1 << i);
217 * run thru all the timers, call the ones that needs it,
218 * clear the ones that wants it, and calculate the next
219 * potential cycle we could sleep for...
221 static avr_cycle_count_t avr_cycle_timer_check(avr_t * avr)
223 if (!avr->cycle_timer_map)
224 return (avr_cycle_count_t)-1;
226 avr_cycle_count_t min = (avr_cycle_count_t)-1;
228 for (int i = 0; i < 32; i++) {
229 if (!(avr->cycle_timer_map & (1 << i)))
231 // do it several times, in case we're late
232 while (avr->cycle_timer[i].when && avr->cycle_timer[i].when <= avr->cycle) {
234 avr->cycle_timer[i].when =
235 avr->cycle_timer[i].timer(avr,
236 avr->cycle_timer[i].when,
237 avr->cycle_timer[i].param);
238 if (avr->cycle_timer[i].when == 0) {
240 avr->cycle_timer[i].timer = NULL;
241 avr->cycle_timer[i].param = NULL;
242 avr->cycle_timer[i].when = 0;
243 avr->cycle_timer_map &= ~(1 << i);
247 if (avr->cycle_timer[i].when && avr->cycle_timer[i].when < min)
248 min = avr->cycle_timer[i].when;
250 return min - avr->cycle;
253 int avr_run(avr_t * avr)
255 avr_gdb_processor(avr, avr->state == cpu_Stopped);
257 if (avr->state == cpu_Stopped)
260 // if we are stepping one instruction, we "run" for one..
261 int step = avr->state == cpu_Step;
263 avr->state = cpu_Running;
266 uint16_t new_pc = avr->pc;
268 if (avr->state == cpu_Running) {
269 new_pc = avr_run_one(avr);
270 #if CONFIG_SIMAVR_TRACE
275 // if we just re-enabled the interrupts...
276 if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
277 // printf("*** %s: Renabling interrupts\n", __FUNCTION__);
280 avr_io_t * port = avr->io_port;
286 avr_cycle_count_t sleep = avr_cycle_timer_check(avr);
290 if (avr->state == cpu_Sleeping) {
291 if (!avr->sreg[S_I]) {
292 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
297 * try to sleep for as long as we can (?)
299 uint32_t usec = avr_cycles_to_usec(avr, sleep);
300 // printf("sleep usec %d cycles %d\n", usec, sleep);
302 while (avr_gdb_processor(avr, usec))
306 avr->cycle += 1 + sleep;
308 // Interrupt servicing might change the PC too
309 if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
310 avr_service_interrupts(avr);
312 avr->data[R_SREG] = 0;
313 for (int i = 0; i < 8; i++)
314 if (avr->sreg[i] > 1) {
315 printf("** Invalid SREG!!\n");
317 } else if (avr->sreg[i])
318 avr->data[R_SREG] |= (1 << i);
322 avr->state = cpu_StepDone;
329 extern avr_kind_t tiny13;
330 extern avr_kind_t tiny2313;
331 extern avr_kind_t tiny25,tiny45,tiny85;
332 extern avr_kind_t mega48,mega88,mega168,mega328;
333 extern avr_kind_t mega164,mega324,mega644;
335 avr_kind_t * avr_kind[] = {
338 &tiny25, &tiny45, &tiny85,
339 &mega48, &mega88, &mega168, &mega328,
340 &mega164, &mega324, &mega644,
344 avr_t * avr_make_mcu_by_name(const char *name)
346 avr_kind_t * maker = NULL;
347 for (int i = 0; avr_kind[i] && !maker; i++) {
348 for (int j = 0; avr_kind[i]->names[j]; j++)
349 if (!strcmp(avr_kind[i]->names[j], name)) {
355 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
359 avr_t * avr = maker->make();
360 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);