d5611ba4eac4bb6625bcc02de80e36371095b894
[simavr] / simavr / sim / sim_avr.c
1 /*
2         sim_avr.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
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.
12
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.
17
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/>.
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include "sim_avr.h"
27 #include "sim_core.h"
28 #include "sim_gdb.h"
29 #include "avr_uart.h"
30 #include "sim_vcd_file.h"
31 #include "avr_mcu_section.h"
32
33
34 int avr_init(avr_t * avr)
35 {
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
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
44         if (avr->special_init)
45                 avr->special_init(avr);
46         if (avr->init)
47                 avr->init(avr);
48         avr->state = cpu_Running;
49         avr_reset(avr); 
50         return 0;
51 }
52
53 void avr_terminate(avr_t * avr)
54 {
55         if (avr->special_deinit)
56                 avr->special_deinit(avr);
57         if (avr->vcd) {
58                 avr_vcd_close(avr->vcd);
59                 avr->vcd = NULL;
60         }
61         avr_deallocate_ios(avr);
62
63         if (avr->flash) free(avr->flash);
64         if (avr->data) free(avr->data);
65         avr->flash = avr->data = NULL;
66 }
67
68 void avr_reset(avr_t * avr)
69 {
70         memset(avr->data, 0x0, avr->ramend + 1);
71         _avr_sp_set(avr, avr->ramend);
72         avr->pc = 0;
73         for (int i = 0; i < 8; i++)
74                 avr->sreg[i] = 0;
75         if (avr->reset)
76                 avr->reset(avr);
77
78         avr_io_t * port = avr->io_port;
79         while (port) {
80                 if (port->reset)
81                         port->reset(port);
82                 port = port->next;
83         }
84 }
85
86 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
87 {
88         printf("%s\n", __FUNCTION__);
89         avr->state = cpu_Stopped;
90         if (avr->gdb_port) {
91                 // enable gdb server, and wait
92                 if (!avr->gdb)
93                         avr_gdb_init(avr);
94         } 
95         if (!avr->gdb)
96                 exit(1); // no gdb ?
97 }
98
99 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
100 {
101         printf("%s %02x\n", __FUNCTION__, v);
102         switch (v) {
103                 case SIMAVR_CMD_VCD_START_TRACE:
104                         if (avr->vcd)
105                                 avr_vcd_start(avr->vcd);
106                         break;
107                 case SIMAVR_CMD_VCD_STOP_TRACE:
108                         if (avr->vcd)
109                                 avr_vcd_stop(avr->vcd);
110                         break;
111                 case SIMAVR_CMD_UART_LOOPBACK: {
112                         avr_irq_t * src = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_OUTPUT);
113                         avr_irq_t * dst = avr_io_getirq(avr, AVR_IOCTL_UART_GETIRQ('0'), UART_IRQ_INPUT);
114                         if (src && dst) {
115                                 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
116                                 avr_connect_irq(src, dst);
117                         }
118                 }       break;
119
120         }
121 }
122
123 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
124 {
125         if (addr)
126                 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
127 }
128
129 static void _avr_io_console_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
130 {
131         static char * buf = NULL;
132         static int size = 0, len = 0;
133
134         if (v == '\r') {
135                 printf("O:" "%s" "" "\n", buf);
136                 fflush(stdout);
137         }
138         if (len + 1 >= size) {
139                 size += 128;
140                 buf = (char*)realloc(buf, size);
141         }
142         buf[len++] = v;
143 }
144
145 void avr_set_console_register(avr_t * avr, avr_io_addr_t addr)
146 {
147         if (addr)
148                 avr_register_io_write(avr, addr, _avr_io_console_write, NULL);
149 }
150
151 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
152 {
153         if (size > avr->flashend+1) {
154                 fprintf(stderr, "avr_loadcode(): Attempted to load code of size %d but flash size is only %d.\n",
155                         size, avr->flashend+1);
156                 abort();
157         }
158         memcpy(avr->flash + address, code, size);
159 }
160
161
162 int avr_run(avr_t * avr)
163 {
164         avr_gdb_processor(avr, avr->state == cpu_Stopped);
165
166         if (avr->state == cpu_Stopped)
167                 return avr->state;
168
169         // if we are stepping one instruction, we "run" for one..
170         int step = avr->state == cpu_Step;
171         if (step)
172                 avr->state = cpu_Running;
173         
174         uint16_t new_pc = avr->pc;
175
176         if (avr->state == cpu_Running) {
177                 new_pc = avr_run_one(avr);
178 #if CONFIG_SIMAVR_TRACE
179                 avr_dump_state(avr);
180 #endif
181         }
182
183         // if we just re-enabled the interrupts...
184         // double buffer the I flag, to detect that edge
185         if (avr->sreg[S_I] && !avr->i_shadow)
186                 avr->pending_wait++;
187         avr->i_shadow = avr->sreg[S_I];
188
189         // run the cycle timers, get the suggested sleeo time
190         // until the next timer is due
191         avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
192
193         avr->pc = new_pc;
194
195         if (avr->state == cpu_Sleeping) {
196                 if (!avr->sreg[S_I]) {
197                         printf("simavr: sleeping with interrupts off, quitting gracefully\n");
198                         avr_terminate(avr);
199                         exit(0);
200                 }
201                 /*
202                  * try to sleep for as long as we can (?)
203                  */
204                 uint32_t usec = avr_cycles_to_usec(avr, sleep);
205         //      printf("sleep usec %d cycles %d\n", usec, sleep);
206                 if (avr->gdb) {
207                         while (avr_gdb_processor(avr, usec))
208                                 ;
209                 } else
210                         usleep(usec);
211                 avr->cycle += 1 + sleep;
212         }
213         // Interrupt servicing might change the PC too, during 'sleep'
214         if (avr->state == cpu_Running || avr->state == cpu_Sleeping)
215                 avr_service_interrupts(avr);
216         
217         // if we were stepping, use this state to inform remote gdb
218         if (step)
219                 avr->state = cpu_StepDone;
220
221         return avr->state;
222 }
223
224
225 extern avr_kind_t tiny13;
226 extern avr_kind_t tiny2313;
227 extern avr_kind_t tiny25,tiny45,tiny85;
228 extern avr_kind_t tiny24,tiny44,tiny84;
229 extern avr_kind_t mega48,mega88,mega168,mega328;
230 extern avr_kind_t mega164,mega324,mega644;
231 extern avr_kind_t mega128;
232
233 avr_kind_t * avr_kind[] = {
234         &tiny13,
235         &tiny2313,
236         &tiny25, &tiny45, &tiny85,
237         &tiny24, &tiny44, &tiny84,
238         &mega48, &mega88, &mega168, &mega328,
239         &mega164, &mega324, &mega644,
240         &mega128,
241         NULL
242 };
243
244 avr_t * avr_make_mcu_by_name(const char *name)
245 {
246         avr_kind_t * maker = NULL;
247         for (int i = 0; avr_kind[i] && !maker; i++) {
248                 for (int j = 0; avr_kind[i]->names[j]; j++)
249                         if (!strcmp(avr_kind[i]->names[j], name)) {
250                                 maker = avr_kind[i];
251                                 break;
252                         }
253         }
254         if (!maker) {
255                 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
256                 return NULL;
257         }
258
259         avr_t * avr = maker->make();
260         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
261         return avr;     
262 }
263