core: Shuffled code around
[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->init)
45                 avr->init(avr);
46         avr->state = cpu_Running;
47         avr_reset(avr); 
48         return 0;
49 }
50
51 void avr_terminate(avr_t * avr)
52 {
53         if (avr->vcd)
54                 avr_vcd_close(avr->vcd);
55         avr->vcd = NULL;
56 }
57
58 void avr_reset(avr_t * avr)
59 {
60         memset(avr->data, 0x0, avr->ramend + 1);
61         _avr_sp_set(avr, avr->ramend);
62         avr->pc = 0;
63         for (int i = 0; i < 8; i++)
64                 avr->sreg[i] = 0;
65         if (avr->reset)
66                 avr->reset(avr);
67
68         avr_io_t * port = avr->io_port;
69         while (port) {
70                 if (port->reset)
71                         port->reset(port);
72                 port = port->next;
73         }
74 }
75
76 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
77 {
78         printf("%s\n", __FUNCTION__);
79         avr->state = cpu_Stopped;
80         if (avr->gdb_port) {
81                 // enable gdb server, and wait
82                 if (!avr->gdb)
83                         avr_gdb_init(avr);
84         } 
85         if (!avr->gdb)
86                 exit(1); // no gdb ?
87 }
88
89 static void _avr_io_command_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param)
90 {
91         printf("%s %02x\n", __FUNCTION__, v);
92         switch (v) {
93                 case SIMAVR_CMD_VCD_START_TRACE:
94                         if (avr->vcd)
95                                 avr_vcd_start(avr->vcd);
96                         break;
97                 case SIMAVR_CMD_VCD_STOP_TRACE:
98                         if (avr->vcd)
99                                 avr_vcd_stop(avr->vcd);
100                         break;
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);
104                         if (src && dst) {
105                                 printf("%s activating uart local echo IRQ src %p dst %p\n", __FUNCTION__, src, dst);
106                                 avr_connect_irq(src, dst);
107                         }
108                 }       break;
109
110         }
111 }
112
113 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr)
114 {
115         if (addr)
116                 avr_register_io_write(avr, addr, _avr_io_command_write, NULL);
117 }
118
119 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
120 {
121         memcpy(avr->flash + address, code, size);
122 }
123
124
125 int avr_run(avr_t * avr)
126 {
127         avr_gdb_processor(avr, avr->state == cpu_Stopped);
128
129         if (avr->state == cpu_Stopped)
130                 return avr->state;
131
132         // if we are stepping one instruction, we "run" for one..
133         int step = avr->state == cpu_Step;
134         if (step) {
135                 avr->state = cpu_Running;
136         }
137         
138         uint16_t new_pc = avr->pc;
139
140         if (avr->state == cpu_Running) {
141                 new_pc = avr_run_one(avr);
142 #if CONFIG_SIMAVR_TRACE
143                 avr_dump_state(avr);
144 #endif
145         }
146
147         // if we just re-enabled the interrupts...
148         if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
149         //      printf("*** %s: Renabling interrupts\n", __FUNCTION__);
150                 avr->pending_wait++;
151         }
152         avr_io_t * port = avr->io_port;
153         while (port) {
154                 if (port->run)
155                         port->run(port);
156                 port = port->next;
157         }
158         avr_cycle_count_t sleep = avr_cycle_timer_process(avr);
159
160         avr->pc = new_pc;
161
162         if (avr->state == cpu_Sleeping) {
163                 if (!avr->sreg[S_I]) {
164                         printf("simavr: sleeping with interrupts off, quitting gracefully\n");
165                         avr_terminate(avr);
166                         exit(0);
167                 }
168                 /*
169                  * try to sleep for as long as we can (?)
170                  */
171                 uint32_t usec = avr_cycles_to_usec(avr, sleep);
172         //      printf("sleep usec %d cycles %d\n", usec, sleep);
173                 if (avr->gdb) {
174                         while (avr_gdb_processor(avr, usec))
175                                 ;
176                 } else
177                         usleep(usec);
178                 avr->cycle += 1 + sleep;
179         }
180         // Interrupt servicing might change the PC too
181         if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
182                 avr_service_interrupts(avr);
183
184                 avr->data[R_SREG] = 0;
185                 for (int i = 0; i < 8; i++)
186                         if (avr->sreg[i] > 1) {
187                                 printf("** Invalid SREG!!\n");
188                                 CRASH();
189                         } else if (avr->sreg[i])
190                                 avr->data[R_SREG] |= (1 << i);
191         }
192
193         if (step) {
194                 avr->state = cpu_StepDone;
195         }
196
197         return avr->state;
198 }
199
200
201 extern avr_kind_t tiny13;
202 extern avr_kind_t tiny2313;
203 extern avr_kind_t tiny25,tiny45,tiny85;
204 extern avr_kind_t mega48,mega88,mega168,mega328;
205 extern avr_kind_t mega164,mega324,mega644;
206
207 avr_kind_t * avr_kind[] = {
208         &tiny13,
209         &tiny2313,
210         &tiny25, &tiny45, &tiny85,
211         &mega48, &mega88, &mega168, &mega328,
212         &mega164, &mega324, &mega644,
213         NULL
214 };
215
216 avr_t * avr_make_mcu_by_name(const char *name)
217 {
218         avr_kind_t * maker = NULL;
219         for (int i = 0; avr_kind[i] && !maker; i++) {
220                 for (int j = 0; avr_kind[i]->names[j]; j++)
221                         if (!strcmp(avr_kind[i]->names[j], name)) {
222                                 maker = avr_kind[i];
223                                 break;
224                         }
225         }
226         if (!maker) {
227                 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
228                 return NULL;
229         }
230
231         avr_t * avr = maker->make();
232         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
233         return avr;     
234 }
235