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 int avr_init(avr_t * avr)
33 avr->flash = malloc(avr->flashend + 1);
34 memset(avr->flash, 0xff, avr->flashend + 1);
35 avr->data = malloc(avr->ramend + 1);
36 memset(avr->data, 0, avr->ramend + 1);
38 // cpu is in limbo before init is finished.
39 avr->state = cpu_Limbo;
40 avr->frequency = 1000000; // can be overriden via avr_mcu_section
43 avr->state = cpu_Running;
48 void avr_reset(avr_t * avr)
50 memset(avr->data, 0x0, avr->ramend + 1);
51 _avr_sp_set(avr, avr->ramend);
53 for (int i = 0; i < 8; i++)
58 avr_io_t * port = avr->io_port;
66 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
68 avr->state = cpu_Stopped;
70 // enable gdb server, and wait
78 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
80 memcpy(avr->flash + address, code, size);
83 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
85 if (addr > avr->ramend) {
86 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
87 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
91 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
92 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
97 * this checks that the current "function" is not doctoring the stack frame that is located
98 * higher on the stack than it should be. It's a sign of code that has overrun it's stack
99 * frame and is munching on it's own return address.
101 if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
102 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
108 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
110 if (addr > avr->ramend) {
111 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
112 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
115 return avr->data[addr];
118 // converts a number of usec to a number of machine cycles, at current speed
119 uint64_t avr_usec_to_cycles(avr_t * avr, uint32_t usec)
121 return avr->frequency * (uint64_t)usec / 1000000;
124 uint32_t avr_cycles_to_usec(avr_t * avr, uint64_t cycles)
126 return 1000000 * cycles / avr->frequency;
129 // converts a number of hz (to megahertz etc) to a number of cycle
130 uint64_t avr_hz_to_cycles(avr_t * avr, uint32_t hz)
132 return avr->frequency / hz;
135 void avr_cycle_timer_register(avr_t * avr, uint64_t when, avr_cycle_timer_t timer, void * param)
137 avr_cycle_timer_cancel(avr, timer, param);
139 if (avr->cycle_timer_map == 0xffffffff) {
140 fprintf(stderr, "avr_cycle_timer_register is full!\n");
144 for (int i = 0; i < 32; i++)
145 if (!(avr->cycle_timer_map & (1 << i))) {
146 avr->cycle_timer[i].timer = timer;
147 avr->cycle_timer[i].param = param;
148 avr->cycle_timer[i].when = when;
149 avr->cycle_timer_map |= (1 << i);
154 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param)
156 avr_cycle_timer_register(avr, avr_usec_to_cycles(avr, when), timer, param);
159 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param)
161 if (!avr->cycle_timer_map)
163 for (int i = 0; i < 32; i++)
164 if ((avr->cycle_timer_map & (1 << i)) &&
165 avr->cycle_timer[i].timer == timer &&
166 avr->cycle_timer[i].param == param) {
167 avr->cycle_timer[i].timer = NULL;
168 avr->cycle_timer[i].param = NULL;
169 avr->cycle_timer[i].when = 0;
170 avr->cycle_timer_map &= ~(1 << i);
176 * run thru all the timers, call the ones that needs it,
177 * clear the ones that wants it, and calculate the next
178 * potential cycle we could sleep for...
180 static uint64_t avr_cycle_timer_check(avr_t * avr)
182 if (!avr->cycle_timer_map)
185 uint64_t min = (uint64_t)-1;
187 for (int i = 0; i < 32; i++) {
188 if (!(avr->cycle_timer_map & (1 << i)))
191 if (avr->cycle_timer[i].when <= avr->cycle) {
193 avr->cycle_timer[i].when =
194 avr->cycle_timer[i].timer(avr,
195 avr->cycle_timer[i].when,
196 avr->cycle_timer[i].param);
197 if (avr->cycle_timer[i].when == 0) {
199 avr->cycle_timer[i].timer = NULL;
200 avr->cycle_timer[i].param = NULL;
201 avr->cycle_timer[i].when = 0;
202 avr->cycle_timer_map &= ~(1 << i);
206 if (avr->cycle_timer[i].when < min)
207 min = avr->cycle_timer[i].when;
209 return min - avr->cycle;
212 int avr_run(avr_t * avr)
214 avr_gdb_processor(avr, avr->state == cpu_Stopped);
216 if (avr->state == cpu_Stopped)
219 // if we are stepping one instruction, we "run" for one..
220 int step = avr->state == cpu_Step;
222 avr->state = cpu_Running;
225 uint16_t new_pc = avr->pc;
227 if (avr->state == cpu_Running) {
228 new_pc = avr_run_one(avr);
232 // if we just re-enabled the interrupts...
233 if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
234 // printf("*** %s: Renabling interrupts\n", __FUNCTION__);
237 avr_io_t * port = avr->io_port;
243 avr_cycle_count_t sleep = avr_cycle_timer_check(avr);
247 if (avr->state == cpu_Sleeping) {
248 if (!avr->sreg[S_I]) {
249 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
253 * try to sleep for as long as we can (?)
255 uint32_t usec = avr_cycles_to_usec(avr, sleep);
257 while (avr_gdb_processor(avr, usec))
263 // Interrupt servicing might change the PC too
264 if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
265 avr_service_interrupts(avr);
267 avr->data[R_SREG] = 0;
268 for (int i = 0; i < 8; i++)
269 if (avr->sreg[i] > 1) {
270 printf("** Invalid SREG!!\n");
272 } else if (avr->sreg[i])
273 avr->data[R_SREG] |= (1 << i);
277 avr->state = cpu_StepDone;
284 extern avr_kind_t tiny13;
285 extern avr_kind_t tiny25,tiny45,tiny85;
286 extern avr_kind_t mega48,mega88,mega168;
287 extern avr_kind_t mega644;
289 avr_kind_t * avr_kind[] = {
301 avr_t * avr_make_mcu_by_name(const char *name)
303 avr_kind_t * maker = NULL;
304 for (int i = 0; avr_kind[i] && !maker; i++) {
305 for (int j = 0; avr_kind[i]->names[j]; j++)
306 if (!strcmp(avr_kind[i]->names[j], name)) {
312 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
316 avr_t * avr = maker->make();
317 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);