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;
61 port->reset(avr, port);
67 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
69 memcpy(avr->flash + address, code, size);
72 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
74 if (addr > avr->ramend) {
75 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
76 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
80 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
81 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
86 * this checks that the current "function" is not doctoring the stack frame that is located
87 * higher on the stack than it should be. It's a sign of code that has overrun it's stack
88 * frame and is munching on it's own return address.
90 if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
91 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
97 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
99 if (addr > avr->ramend) {
100 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
101 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
104 return avr->data[addr];
108 int avr_run(avr_t * avr)
110 avr_gdb_processor(avr);
112 if (avr->state == cpu_Stopped) {
117 int step = avr->state == cpu_Step;
119 avr->state = cpu_Running;
122 uint16_t new_pc = avr->pc;
124 if (avr->state == cpu_Running) {
125 new_pc = avr_run_one(avr);
132 // if we just re-enabled the interrupts...
133 if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
134 // printf("*** %s: Renabling interrupts\n", __FUNCTION__);
137 avr_io_t * port = avr->io_port;
140 port->run(avr, port);
146 if (avr->state == cpu_Sleeping) {
147 if (!avr->sreg[S_I]) {
148 printf("simavr: sleeping with interrupts off, quitting gracefuly\n");
152 long sleep = (float)avr->frequency * (1.0f / 500.0f);
154 // avr->state = cpu_Running;
156 // Interrupt servicing might change the PC too
157 if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
158 avr_service_interrupts(avr);
160 avr->data[R_SREG] = 0;
161 for (int i = 0; i < 8; i++)
162 if (avr->sreg[i] > 1) {
163 printf("** Invalid SREG!!\n");
165 } else if (avr->sreg[i])
166 avr->data[R_SREG] |= (1 << i);
170 avr->state = cpu_StepDone;
177 extern avr_kind_t tiny85;
178 extern avr_kind_t mega48,mega88,mega168;
179 extern avr_kind_t mega644;
181 avr_kind_t * avr_kind[] = {
190 avr_t * avr_make_mcu_by_name(const char *name)
192 avr_kind_t * maker = NULL;
193 for (int i = 0; avr_kind[i] && !maker; i++) {
194 for (int j = 0; avr_kind[i]->names[j]; j++)
195 if (!strcmp(avr_kind[i]->names[j], name)) {
201 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
205 avr_t * avr = maker->make();
206 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);