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