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);
40 #ifdef CONFIG_SIMAVR_TRACE
41 avr->trace_data = calloc(1, sizeof(struct avr_trace_data_t));
44 // cpu is in limbo before init is finished.
45 avr->state = cpu_Limbo;
46 avr->frequency = 1000000; // can be overriden via avr_mcu_section
47 if (avr->special_init)
48 avr->special_init(avr);
51 // set default (non gdb) fast callbacks
52 avr->run = avr_callback_run_raw;
53 avr->sleep = avr_callback_sleep_raw;
54 avr->state = cpu_Running;
59 void avr_terminate(avr_t * avr)
61 if (avr->special_deinit)
62 avr->special_deinit(avr);
64 avr_vcd_close(avr->vcd);
67 avr_deallocate_ios(avr);
69 if (avr->flash) free(avr->flash);
70 if (avr->data) free(avr->data);
71 avr->flash = avr->data = NULL;
74 void avr_reset(avr_t * avr)
76 memset(avr->data, 0x0, avr->ramend + 1);
77 _avr_sp_set(avr, avr->ramend);
79 for (int i = 0; i < 8; i++)
84 avr_io_t * port = avr->io_port;
92 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
94 printf("%s\n", __FUNCTION__);
95 avr->state = cpu_Stopped;
97 // enable gdb server, and wait
102 avr->state = cpu_Crashed;
105 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
107 printf("%s %02x\n", __FUNCTION__, v);
109 case SIMAVR_CMD_VCD_START_TRACE:
111 avr_vcd_start(avr->vcd);
113 case SIMAVR_CMD_VCD_STOP_TRACE:
115 avr_vcd_stop(avr->vcd);
117 case SIMAVR_CMD_UART_LOOPBACK: {
118 avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
119 avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
121 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
122 avr_connect_irq(src, dst);
129 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
132 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
135 static void _avr_io_console_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
137 static char * buf = NULL;
138 static int size = 0, len = 0;
140 if (v == '\r' && buf) {
142 printf("O:" "%s" "" "\n", buf);
147 if (len + 1 >= size) {
149 buf = (char*)realloc(buf, size);
155 void avr_set_console_register(avr_t * avr, avr_io_addr_t addr)
158 avr_register_io_write(avr, addr, _avr_io_console_write, NULL);
161 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
163 if (size > avr->flashend+1) {
164 fprintf(stderr, "avr_loadcode(): Attempted to load code of size %d but flash size is only %d.\n",
165 size, avr->flashend+1);
168 memcpy(avr->flash + address, code, size);
171 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong)
173 uint32_t usec = avr_cycles_to_usec(avr, howLong);
174 while (avr_gdb_processor(avr, usec))
178 void avr_callback_run_gdb(avr_t * avr)
180 avr_gdb_processor(avr, avr->state == cpu_Stopped);
182 if (avr->state == cpu_Stopped)
185 // if we are stepping one instruction, we "run" for one..
186 int step = avr->state == cpu_Step;
188 avr->state = cpu_Running;
190 uint16_t new_pc = avr->pc;
192 if (avr->state == cpu_Running) {
193 new_pc = avr_run_one(avr);
194 #if CONFIG_SIMAVR_TRACE
199 // if we just re-enabled the interrupts...
200 // double buffer the I flag, to detect that edge
201 if (avr->sreg[S_I] && !avr->i_shadow)
203 avr->i_shadow = avr->sreg[S_I];
205 // run the cycle timers, get the suggested sleep time
206 // until the next timer is due
207 avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
211 if (avr->state == cpu_Sleeping) {
212 if (!avr->sreg[S_I]) {
213 if ( avr->log_level) printf("simavr: sleeping with interrupts off, quitting gracefully\n");
215 avr->state = cpu_Done;
219 * try to sleep for as long as we can (?)
221 avr->sleep(avr, sleep);
222 avr->cycle += 1 + sleep;
224 // Interrupt servicing might change the PC too, during 'sleep'
225 if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
226 avr_service_interrupts(avr);
228 // if we were stepping, use this state to inform remote gdb
230 avr->state = cpu_StepDone;
234 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong)
236 uint32_t usec = avr_cycles_to_usec(avr, howLong);
240 void avr_callback_run_raw(avr_t * avr)
243 uint16_t new_pc = avr->pc;
245 if (avr->state == cpu_Running) {
246 new_pc = avr_run_one(avr);
247 #if CONFIG_SIMAVR_TRACE
252 // if we just re-enabled the interrupts...
253 // double buffer the I flag, to detect that edge
254 if (avr->sreg[S_I] && !avr->i_shadow)
256 avr->i_shadow = avr->sreg[S_I];
258 // run the cycle timers, get the suggested sleeo time
259 // until the next timer is due
260 avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
264 if (avr->state == cpu_Sleeping) {
265 if (!avr->sreg[S_I]) {
266 if ( avr->log_level) printf("simavr: sleeping with interrupts off, quitting gracefully\n");
268 avr->state = cpu_Done;
272 * try to sleep for as long as we can (?)
274 avr->sleep(avr, sleep);
275 avr->cycle += 1 + sleep;
277 // Interrupt servicing might change the PC too, during 'sleep'
278 if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
279 avr_service_interrupts(avr);
283 int avr_run(avr_t * avr)
290 extern avr_kind_t tiny13;
291 extern avr_kind_t tiny2313;
292 extern avr_kind_t tiny25,tiny45,tiny85;
293 extern avr_kind_t tiny24,tiny44,tiny84;
294 extern avr_kind_t mega8;
295 extern avr_kind_t mega48,mega88,mega168,mega328;
296 extern avr_kind_t mega164,mega324,mega644;
297 extern avr_kind_t mega128;
298 extern avr_kind_t mega1281;
300 avr_kind_t * avr_kind[] = {
303 &tiny25, &tiny45, &tiny85,
304 &tiny24, &tiny44, &tiny84,
306 &mega48, &mega88, &mega168, &mega328,
307 &mega164, &mega324, &mega644,
313 avr_t * avr_make_mcu_by_name(const char *name)
315 avr_kind_t * maker = NULL;
316 for (int i = 0; avr_kind[i] && !maker; i++) {
317 for (int j = 0; avr_kind[i]->names[j]; j++)
318 if (!strcmp(avr_kind[i]->names[j], name)) {
324 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
328 avr_t * avr = maker->make();
329 printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);