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/>.
30 #include "sim_vcd_file.h"
31 #include "avr_mcu_section.h"
34 int avr_init(avr_t * avr)
36 avr->flash = malloc(avr->flashend + 1);
37 memset(avr->flash, 0xff, avr->flashend + 1);
38 avr->data = malloc(avr->ramend + 1);
39 memset(avr->data, 0, avr->ramend + 1);
41 // cpu is in limbo before init is finished.
42 avr->state = cpu_Limbo;
43 avr->frequency = 1000000; // can be overriden via avr_mcu_section
46 avr->state = cpu_Running;
51 void avr_terminate(avr_t * avr)
54 avr_vcd_close(avr->vcd);
58 void avr_reset(avr_t * avr)
60 memset(avr->data, 0x0, avr->ramend + 1);
61 _avr_sp_set(avr, avr->ramend);
63 for (int i = 0; i < 8; i++)
68 avr_io_t * port = avr->io_port;
76 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
78 printf("%s\n", __FUNCTION__);
79 avr->state = cpu_Stopped;
81 // enable gdb server, and wait
89 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
91 printf("%s %02x\n", __FUNCTION__, v);
93 case SIMAVR_CMD_VCD_START_TRACE:
95 avr_vcd_start(avr->vcd);
97 case SIMAVR_CMD_VCD_STOP_TRACE:
99 avr_vcd_stop(avr->vcd);
101 case SIMAVR_CMD_UART_LOOPBACK: {
102 avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
103 avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
105 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
106 avr_connect_irq(src, dst);
113 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
116 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
119 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
121 memcpy(avr->flash + address, code, size);
125 int avr_run(avr_t * avr)
127 avr_gdb_processor(avr, avr->state == cpu_Stopped);
129 if (avr->state == cpu_Stopped)
132 // if we are stepping one instruction, we "run" for one..
133 int step = avr->state == cpu_Step;
135 avr->state = cpu_Running;
137 uint16_t new_pc = avr->pc;
139 if (avr->state == cpu_Running) {
140 new_pc = avr_run_one(avr);
141 #if CONFIG_SIMAVR_TRACE
146 // if we just re-enabled the interrupts...
147 // double buffer the I flag, to detect that edge
148 if (avr->sreg[S_I] && !avr->i_shadow)
150 avr->i_shadow = avr->sreg[S_I];
152 // run IO modules that wants it
153 avr_io_t * port = avr->io_port;
160 // run the cycle timers, get the suggested sleeo time
161 // until the next timer is due
162 avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
166 if (avr->state == cpu_Sleeping) {
167 if (!avr->sreg[S_I]) {
168 printf("simavr: sleeping with interrupts off, quitting gracefully\n");
173 * try to sleep for as long as we can (?)
175 uint32_t usec = avr_cycles_to_usec(avr, sleep);
176 // printf("sleep usec %d cycles %d\n", usec, sleep);
178 while (avr_gdb_processor(avr, usec))
182 avr->cycle += 1 + sleep;
184 // Interrupt servicing might change the PC too, during 'sleep'
185 if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
186 avr_service_interrupts(avr);
188 // if we were stepping, use this state to inform remote gdb
190 avr->state = cpu_StepDone;
196 extern avr_kind_t tiny13;
197 extern avr_kind_t tiny2313;
198 extern avr_kind_t tiny25,tiny45,tiny85;
199 extern avr_kind_t mega48,mega88,mega168,mega328;
200 extern avr_kind_t mega164,mega324,mega644;
201 extern avr_kind_t mega128;
203 avr_kind_t * avr_kind[] = {
206 &tiny25, &tiny45, &tiny85,
207 &mega48, &mega88, &mega168, &mega328,
208 &mega164, &mega324, &mega644,
213 avr_t * avr_make_mcu_by_name(const char *name)
215 avr_kind_t * maker = NULL;
216 for (int i = 0; avr_kind[i] && !maker; i++) {
217 for (int j = 0; avr_kind[i]->names[j]; j++)
218 if (!strcmp(avr_kind[i]->names[j], name)) {
224 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
228 avr_t * avr = maker->make();
229 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);