core: Reworked the cycle timers
[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 "sim_vcd_file.h"
30
31
32 int avr_init(avr_t * avr)
33 {
34         avr->flash = malloc(avr->flashend + 1);
35         memset(avr->flash, 0xff, avr->flashend + 1);
36         avr->data = malloc(avr->ramend + 1);
37         memset(avr->data, 0, avr->ramend + 1);
38
39         // cpu is in limbo before init is finished.
40         avr->state = cpu_Limbo;
41         avr->frequency = 1000000;       // can be overriden via avr_mcu_section
42         if (avr->init)
43                 avr->init(avr);
44         avr->state = cpu_Running;
45         avr_reset(avr); 
46         return 0;
47 }
48
49 void avr_terminate(avr_t * avr)
50 {
51         if (avr->vcd)
52                 avr_vcd_close(avr->vcd);
53         avr->vcd = NULL;
54 }
55
56 void avr_reset(avr_t * avr)
57 {
58         memset(avr->data, 0x0, avr->ramend + 1);
59         _avr_sp_set(avr, avr->ramend);
60         avr->pc = 0;
61         for (int i = 0; i < 8; i++)
62                 avr->sreg[i] = 0;
63         if (avr->reset)
64                 avr->reset(avr);
65
66         avr_io_t * port = avr->io_port;
67         while (port) {
68                 if (port->reset)
69                         port->reset(port);
70                 port = port->next;
71         }
72 }
73
74 void avr_sadly_crashed(avr_t *avr, uint8_t signal)
75 {
76         avr->state = cpu_Stopped;
77         if (avr->gdb_port) {
78                 // enable gdb server, and wait
79                 if (!avr->gdb)
80                         avr_gdb_init(avr);
81         } 
82         if (!avr->gdb)
83                 exit(1); // no gdb ?
84 }
85
86 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address)
87 {
88         memcpy(avr->flash + address, code, size);
89 }
90
91 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
92 {
93         if (addr > avr->ramend) {
94                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x out of ram\n",
95                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
96                 CRASH();
97         }
98         if (addr < 32) {
99                 printf("*** Invalid write address PC=%04x SP=%04x O=%04x Address %04x=%02x low registers\n",
100                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, v);
101                 CRASH();
102         }
103 #if AVR_STACK_WATCH
104         /*
105          * this checks that the current "function" is not doctoring the stack frame that is located
106          * higher on the stack than it should be. It's a sign of code that has overrun it's stack
107          * frame and is munching on it's own return address.
108          */
109         if (avr->stack_frame_index > 1 && addr > avr->stack_frame[avr->stack_frame_index-2].sp) {
110                 printf("\e[31m%04x : munching stack SP %04x, A=%04x <= %02x\e[0m\n", avr->pc, _avr_sp_get(avr), addr, v);
111         }
112 #endif
113         avr->data[addr] = v;
114 }
115
116 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
117 {
118         if (addr > avr->ramend) {
119                 printf("*** Invalid read address PC=%04x SP=%04x O=%04x Address %04x out of ram (%04x)\n",
120                                 avr->pc, _avr_sp_get(avr), avr->flash[avr->pc] | (avr->flash[avr->pc]<<8), addr, avr->ramend);
121                 CRASH();
122         }
123         return avr->data[addr];
124 }
125
126 // converts a number of usec to a number of machine cycles, at current speed
127 avr_cycle_count_t avr_usec_to_cycles(avr_t * avr, uint32_t usec)
128 {
129         return avr->frequency * (avr_cycle_count_t)usec / 1000000;
130 }
131
132 uint32_t avr_cycles_to_usec(avr_t * avr, avr_cycle_count_t cycles)
133 {
134         return 1000000 * cycles / avr->frequency;
135 }
136
137 // converts a number of hz (to megahertz etc) to a number of cycle
138 avr_cycle_count_t avr_hz_to_cycles(avr_t * avr, uint32_t hz)
139 {
140         return avr->frequency / hz;
141 }
142
143 void avr_cycle_timer_register(avr_t * avr, avr_cycle_count_t when, avr_cycle_timer_t timer, void * param)
144 {
145         avr_cycle_timer_cancel(avr, timer, param);
146
147         if (avr->cycle_timer_map == 0xffffffff) {
148                 fprintf(stderr, "avr_cycle_timer_register is full!\n");
149                 return;
150         }
151         when += avr->cycle;
152         for (int i = 0; i < 32; i++)
153                 if (!(avr->cycle_timer_map & (1 << i))) {
154                         avr->cycle_timer[i].timer = timer;
155                         avr->cycle_timer[i].param = param;
156                         avr->cycle_timer[i].when = when;
157                         avr->cycle_timer_map |= (1 << i);
158                         return;
159                 }
160 }
161
162 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param)
163 {
164         avr_cycle_timer_register(avr, avr_usec_to_cycles(avr, when), timer, param);
165 }
166
167 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param)
168 {
169         if (!avr->cycle_timer_map)
170                 return;
171         for (int i = 0; i < 32; i++)
172                 if ((avr->cycle_timer_map & (1 << i)) &&
173                                 avr->cycle_timer[i].timer == timer &&
174                                 avr->cycle_timer[i].param == param) {
175                         avr->cycle_timer[i].timer = NULL;
176                         avr->cycle_timer[i].param = NULL;
177                         avr->cycle_timer[i].when = 0;
178                         avr->cycle_timer_map &= ~(1 << i);
179                         return;
180                 }
181 }
182
183 /*
184  * run thru all the timers, call the ones that needs it,
185  * clear the ones that wants it, and calculate the next
186  * potential cycle we could sleep for...
187  */
188 static avr_cycle_count_t avr_cycle_timer_check(avr_t * avr)
189 {
190         if (!avr->cycle_timer_map)
191                 return (avr_cycle_count_t)-1;
192
193         avr_cycle_count_t min = (avr_cycle_count_t)-1;
194
195         for (int i = 0; i < 32; i++) {
196                 if (!(avr->cycle_timer_map & (1 << i)))
197                         continue;
198                 // do it several times, in case we're late
199                 while (avr->cycle_timer[i].when && avr->cycle_timer[i].when <= avr->cycle) {
200                         // call it
201                         avr->cycle_timer[i].when =
202                                         avr->cycle_timer[i].timer(avr,
203                                                         avr->cycle_timer[i].when,
204                                                         avr->cycle_timer[i].param);
205                         if (avr->cycle_timer[i].when == 0) {
206                                 // clear it
207                                 avr->cycle_timer[i].timer = NULL;
208                                 avr->cycle_timer[i].param = NULL;
209                                 avr->cycle_timer[i].when = 0;
210                                 avr->cycle_timer_map &= ~(1 << i);
211                                 break;
212                         }
213                 }
214                 if (avr->cycle_timer[i].when && avr->cycle_timer[i].when < min)
215                         min = avr->cycle_timer[i].when;
216         }
217         return min - avr->cycle;
218 }
219
220 int avr_run(avr_t * avr)
221 {
222         avr_gdb_processor(avr, avr->state == cpu_Stopped);
223
224         if (avr->state == cpu_Stopped)
225                 return avr->state;
226
227         // if we are stepping one instruction, we "run" for one..
228         int step = avr->state == cpu_Step;
229         if (step) {
230                 avr->state = cpu_Running;
231         }
232         
233         uint16_t new_pc = avr->pc;
234
235         if (avr->state == cpu_Running) {
236                 new_pc = avr_run_one(avr);
237 #if CONFIG_SIMAVR_TRACE
238                 avr_dump_state(avr);
239 #endif
240         }
241
242         // if we just re-enabled the interrupts...
243         if (avr->sreg[S_I] && !(avr->data[R_SREG] & (1 << S_I))) {
244         //      printf("*** %s: Renabling interrupts\n", __FUNCTION__);
245                 avr->pending_wait++;
246         }
247         avr_io_t * port = avr->io_port;
248         while (port) {
249                 if (port->run)
250                         port->run(port);
251                 port = port->next;
252         }
253         avr_cycle_count_t sleep = avr_cycle_timer_check(avr);
254
255         avr->pc = new_pc;
256
257         if (avr->state == cpu_Sleeping) {
258                 if (!avr->sreg[S_I]) {
259                         printf("simavr: sleeping with interrupts off, quitting gracefully\n");
260                         avr_terminate(avr);
261                         exit(0);
262                 }
263                 /*
264                  * try to sleep for as long as we can (?)
265                  */
266                 uint32_t usec = avr_cycles_to_usec(avr, sleep);
267         //      printf("sleep usec %d cycles %d\n", usec, sleep);
268                 if (avr->gdb) {
269                         while (avr_gdb_processor(avr, usec))
270                                 ;
271                 } else
272                         usleep(usec);
273                 avr->cycle += 1 + sleep;
274         }
275         // Interrupt servicing might change the PC too
276         if (avr->state == cpu_Running || avr->state == cpu_Sleeping) {
277                 avr_service_interrupts(avr);
278
279                 avr->data[R_SREG] = 0;
280                 for (int i = 0; i < 8; i++)
281                         if (avr->sreg[i] > 1) {
282                                 printf("** Invalid SREG!!\n");
283                                 CRASH();
284                         } else if (avr->sreg[i])
285                                 avr->data[R_SREG] |= (1 << i);
286         }
287
288         if (step) {
289                 avr->state = cpu_StepDone;
290         }
291
292         return avr->state;
293 }
294
295
296 extern avr_kind_t tiny13;
297 extern avr_kind_t tiny25,tiny45,tiny85;
298 extern avr_kind_t mega48,mega88,mega168,mega328;
299 extern avr_kind_t mega164,mega324,mega644;
300
301 avr_kind_t * avr_kind[] = {
302         &tiny13,
303         &tiny25, &tiny45, &tiny85,
304         &mega48, &mega88, &mega168, &mega328,
305         &mega164, &mega324, &mega644,
306         NULL
307 };
308
309 avr_t * avr_make_mcu_by_name(const char *name)
310 {
311         avr_kind_t * maker = NULL;
312         for (int i = 0; avr_kind[i] && !maker; i++) {
313                 for (int j = 0; avr_kind[i]->names[j]; j++)
314                         if (!strcmp(avr_kind[i]->names[j], name)) {
315                                 maker = avr_kind[i];
316                                 break;
317                         }
318         }
319         if (!maker) {
320                 fprintf(stderr, "%s: AVR '%s' now known\n", __FUNCTION__, name);
321                 return NULL;
322         }
323
324         avr_t * avr = maker->make();
325         printf("Starting %s - flashend %04x ramend %04x e2end %04x\n", avr->mmcu, avr->flashend, avr->ramend, avr->e2end);
326         return avr;     
327 }
328