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/>.
34 #include "avr_eeprom.h"
37 void hdump(const char *w, uint8_t *b, size_t l)
42 for (i = 0; i < l; i++) printf("%02x",b[i]);
45 for (i = 0; i < l; i++) {
46 if (!(i & 0x1f)) printf(" ");
48 if ((i & 0x1f) == 0x1f) {
59 int avr_init(avr_t * avr)
61 avr->flash = malloc(avr->flashend + 1);
62 memset(avr->flash, 0xff, avr->flashend + 1);
63 avr->data = malloc(avr->ramend + 1);
64 memset(avr->data, 0, avr->ramend + 1);
66 // cpu is in limbo before init is finished.
67 avr->state = cpu_Limbo;
68 avr->frequency = 1000000; // can be overriden via avr_mcu_section
71 avr->state = cpu_Running;
76 void avr_reset(avr_t * avr)
78 memset(avr->data, 0x0, avr->ramend + 1);
79 _avr_sp_set(avr, avr->ramend);
81 for (int i = 0; i < 8; i++)
86 avr_io_t * port = avr->io_port;
89 port->reset(avr, port);
96 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
98 memcpy(avr->flash + address, code, size);
101 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
103 if (addr > avr->ramend) {
104 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
105 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
109 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
110 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
115 * this checks that the current "function" is not doctoring the stack frame that is located
116 * higher on the stack than it should be. It's a sign of code that has overrun it's stack
117 * frame and is munching on it's own return address.
119 if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
120 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
126 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
128 if (addr > avr->ramend) {
129 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
130 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
133 return avr->data[addr];
137 int avr_run(avr_t * avr)
139 if (avr->state == cpu_Stopped)
142 uint16_t new_pc = avr->pc;
144 if (avr->state == cpu_Running) {
145 new_pc = avr_run_one(avr);
152 // if we just re-enabled the interrupts...
153 if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
154 // printf("*** %s: Renabling interrupts\n", __FUNCTION__);
157 avr_io_t * port = avr->io_port;
160 port->run(avr, port);
166 if (avr->state == cpu_Sleeping) {
167 if (!avr->sreg[S_I]) {
168 printf("simavr: sleeping with interrupts off, quitting gracefuly\n");
172 long sleep = (float)avr->frequency * (1.0f / 500.0f);
174 // avr->state = cpu_Running;
176 // Interrupt servicing might change the PC too
177 if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
178 avr_service_interrupts(avr);
180 avr->data[R_SREG] = 0;
181 for (int i = 0; i < 8; i++)
182 if (avr->sreg[i] > 1) {
183 printf("** Invalid SREG!!\n");
185 } else if (avr->sreg[i])
186 avr->data[R_SREG] |= (1 << i);
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[] = {
206 printf("usage: simavr [-t] [-m <device>] [-f <frequency>] firmware\n");
207 printf(" -t: run full scale decoder trace\n");
211 int main(int argc, char *argv[])
218 int option_index = 0;
220 struct option long_options[] = {
221 {"help", no_argument, 0, 'h'},
222 {"mcu", required_argument, 0, 'm'},
223 {"freq", required_argument, 0, 'f'},
224 {"trace", no_argument, 0, 't'},
231 while ((option_count = getopt_long(argc, argv, "thm:f:", long_options, &option_index)) != -1) {
232 switch (option_count) {
237 strcpy(name, optarg);
240 f_cpu = atoi(optarg);
248 elf_read_firmware(argv[argc-1], &f);
251 strcpy(f.mmcu.name, name);
253 f.mmcu.f_cpu = f_cpu;
255 printf("firmware %s f=%d mmcu=%s\n", argv[argc-1], f.mmcu.f_cpu, f.mmcu.name);
257 avr_kind_t * maker = NULL;
258 for (int i = 0; avr_kind[i] && !maker; i++) {
259 for (int j = 0; avr_kind[i]->names[j]; j++)
260 if (!strcmp(avr_kind[i]->names[j], f.mmcu.name)) {
266 fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu.name);
270 avr_t * avr = maker->make();
271 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
273 avr->frequency = f.mmcu.f_cpu;
274 avr->codeline = f.codeline;
275 avr_loadcode(avr, f.flash, f.flashsize, 0);
276 avr->codeend = f.flashsize - f.datasize;
277 if (f.eeprom && f.eesize) {
278 avr_eeprom_desc_t d = { .ee = f.eeprom, .offset = 0, .size = f.eesize };
279 avr_ioctl(avr, AVR_IOCTL_EEPROM_SET, &d);
283 // try to enable "local echo" on the first uart, for testing purposes
285 avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
286 avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
287 printf("%s:%s activating uart local echo IRQ src %p dst %p\n", __FILE__, __FUNCTION__, src, dst);
289 avr_connect_irq(avr, src, dst);
292 for (long long i = 0; i < 8000000*10; i++)
293 // for (long long i = 0; i < 80000; i++)