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];
119 int avr_run(avr_t * avr)
121 avr_gdb_processor(avr, avr->state == cpu_Stopped);
123 if (avr->state == cpu_Stopped)
126 // if we are stepping one insruction, we "run" for one..
127 int step = avr->state == cpu_Step;
129 avr->state = cpu_Running;
132 uint16_t new_pc = avr->pc;
134 if (avr->state == cpu_Running) {
135 new_pc = avr_run_one(avr);
142 // if we just re-enabled the interrupts...
143 if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
144 // printf("*** %s: Renabling interrupts\n", __FUNCTION__);
147 avr_io_t * port = avr->io_port;
156 if (avr->state == cpu_Sleeping) {
157 if (!avr->sreg[S_I]) {
158 printf("simavr: sleeping with interrupts off, quitting gracefuly\n");
162 while (avr_gdb_processor(avr, 1))
166 long sleep = (float)avr->frequency * (1.0f / 500.0f);
168 // avr->state = cpu_Running;
170 // Interrupt servicing might change the PC too
171 if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
172 avr_service_interrupts(avr);
174 avr->data[R_SREG] = 0;
175 for (int i = 0; i < 8; i++)
176 if (avr->sreg[i] > 1) {
177 printf("** Invalid SREG!!\n");
179 } else if (avr->sreg[i])
180 avr->data[R_SREG] |= (1 << i);
184 avr->state = cpu_StepDone;
191 extern avr_kind_t tiny85;
192 extern avr_kind_t mega48,mega88,mega168;
193 extern avr_kind_t mega644;
195 avr_kind_t * avr_kind[] = {
204 avr_t * avr_make_mcu_by_name(const char *name)
206 avr_kind_t * maker = NULL;
207 for (int i = 0; avr_kind[i] && !maker; i++) {
208 for (int j = 0; avr_kind[i]->names[j]; j++)
209 if (!strcmp(avr_kind[i]->names[j], name)) {
215 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
219 avr_t * avr = maker->make();
220 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);