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/>.
29 #include "sim_vcd_file.h"
32 int avr_init(avr_t * avr)
34 avr->flash = malloc(avr->flashend + 1);
35 memset(avr->flash, 0xff, avr->flashend + 1);
36 avr->data = malloc(avr->ramend + 1);
37 memset(avr->data, 0, avr->ramend + 1);
39 // cpu is in limbo before init is finished.
40 avr->state = cpu_Limbo;
41 avr->frequency = 1000000; // can be overriden via avr_mcu_section
44 avr->state = cpu_Running;
49 void avr_terminate(avr_t * avr)
52 avr_vcd_close(avr->vcd);
56 void avr_reset(avr_t * avr)
58 memset(avr->data, 0x0, avr->ramend + 1);
59 _avr_sp_set(avr, avr->ramend);
61 for (int i = 0; i < 8; i++)
66 avr_io_t * port = avr->io_port;
74 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
76 avr->state = cpu_Stopped;
78 // enable gdb server, and wait
86 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
88 memcpy(avr->flash + address, code, size);
91 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
93 if (addr > avr->ramend) {
94 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
95 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
99 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
100 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
105 * this checks that the current "function" is not doctoring the stack frame that is located
106 * higher on the stack than it should be. It's a sign of code that has overrun it's stack
107 * frame and is munching on it's own return address.
109 if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
110 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
116 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
118 if (addr > avr->ramend) {
119 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
120 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
123 return avr->data[addr];
126 // converts a number of usec to a number of machine cycles, at current speed
127 avr_cycle_count_t avr_usec_to_cycles(avr_t * avr, uint32_t usec)
129 return avr->frequency * (avr_cycle_count_t)usec / 1000000;
132 uint32_t avr_cycles_to_usec(avr_t * avr, avr_cycle_count_t cycles)
134 return 1000000 * cycles / avr->frequency;
137 // converts a number of hz (to megahertz etc) to a number of cycle
138 avr_cycle_count_t avr_hz_to_cycles(avr_t * avr, uint32_t hz)
140 return avr->frequency / hz;
143 void avr_cycle_timer_register(avr_t * avr, avr_cycle_count_t when, avr_cycle_timer_t timer, void * param)
145 avr_cycle_timer_cancel(avr, timer, param);
147 if (avr->cycle_timer_map == 0xffffffff) {
148 fprintf(stderr, "avr_cycle_timer_register is full!\n");
152 for (int i = 0; i < 32; i++)
153 if (!(avr->cycle_timer_map & (1 << i))) {
154 avr->cycle_timer[i].timer = timer;
155 avr->cycle_timer[i].param = param;
156 avr->cycle_timer[i].when = when;
157 avr->cycle_timer_map |= (1 << i);
162 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param)
164 avr_cycle_timer_register(avr, avr_usec_to_cycles(avr, when), timer, param);
167 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param)
169 if (!avr->cycle_timer_map)
171 for (int i = 0; i < 32; i++)
172 if ((avr->cycle_timer_map & (1 << i)) &&
173 avr->cycle_timer[i].timer == timer &&
174 avr->cycle_timer[i].param == param) {
175 avr->cycle_timer[i].timer = NULL;
176 avr->cycle_timer[i].param = NULL;
177 avr->cycle_timer[i].when = 0;
178 avr->cycle_timer_map &= ~(1 << i);
184 * run thru all the timers, call the ones that needs it,
185 * clear the ones that wants it, and calculate the next
186 * potential cycle we could sleep for...
188 static avr_cycle_count_t avr_cycle_timer_check(avr_t * avr)
190 if (!avr->cycle_timer_map)
191 return (avr_cycle_count_t)-1;
193 avr_cycle_count_t min = (avr_cycle_count_t)-1;
195 for (int i = 0; i < 32; i++) {
196 if (!(avr->cycle_timer_map & (1 << i)))
198 // do it several times, in case we're late
199 while (avr->cycle_timer[i].when && avr->cycle_timer[i].when <= avr->cycle) {
201 avr->cycle_timer[i].when =
202 avr->cycle_timer[i].timer(avr,
203 avr->cycle_timer[i].when,
204 avr->cycle_timer[i].param);
205 if (avr->cycle_timer[i].when == 0) {
207 avr->cycle_timer[i].timer = NULL;
208 avr->cycle_timer[i].param = NULL;
209 avr->cycle_timer[i].when = 0;
210 avr->cycle_timer_map &= ~(1 << i);
214 if (avr->cycle_timer[i].when && avr->cycle_timer[i].when < min)
215 min = avr->cycle_timer[i].when;
217 return min - avr->cycle;
220 int avr_run(avr_t * avr)
222 avr_gdb_processor(avr, avr->state == cpu_Stopped);
224 if (avr->state == cpu_Stopped)
227 // if we are stepping one instruction, we "run" for one..
228 int step = avr->state == cpu_Step;
230 avr->state = cpu_Running;
233 uint16_t new_pc = avr->pc;
235 if (avr->state == cpu_Running) {
236 new_pc = avr_run_one(avr);
237 #if CONFIG_SIMAVR_TRACE
242 // if we just re-enabled the interrupts...
243 if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
244 // printf("*** %s: Renabling interrupts\n", __FUNCTION__);
247 avr_io_t * port = avr->io_port;
253 avr_cycle_count_t sleep = avr_cycle_timer_check(avr);
257 if (avr->state == cpu_Sleeping) {
258 if (!avr->sreg[S_I]) {
259 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
264 * try to sleep for as long as we can (?)
266 uint32_t usec = avr_cycles_to_usec(avr, sleep);
267 // printf("sleep usec %d cycles %d\n", usec, sleep);
269 while (avr_gdb_processor(avr, usec))
273 avr->cycle += 1 + sleep;
275 // Interrupt servicing might change the PC too
276 if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
277 avr_service_interrupts(avr);
279 avr->data[R_SREG] = 0;
280 for (int i = 0; i < 8; i++)
281 if (avr->sreg[i] > 1) {
282 printf("** Invalid SREG!!\n");
284 } else if (avr->sreg[i])
285 avr->data[R_SREG] |= (1 << i);
289 avr->state = cpu_StepDone;
296 extern avr_kind_t tiny13;
297 extern avr_kind_t tiny25,tiny45,tiny85;
298 extern avr_kind_t mega48,mega88,mega168,mega328;
299 extern avr_kind_t mega164,mega324,mega644;
301 avr_kind_t * avr_kind[] = {
303 &tiny25, &tiny45, &tiny85,
304 &mega48, &mega88, &mega168, &mega328,
305 &mega164, &mega324, &mega644,
309 avr_t * avr_make_mcu_by_name(const char *name)
311 avr_kind_t * maker = NULL;
312 for (int i = 0; avr_kind[i] && !maker; i++) {
313 for (int j = 0; avr_kind[i]->names[j]; j++)
314 if (!strcmp(avr_kind[i]->names[j], name)) {
320 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
324 avr_t * avr = maker->make();
325 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);