core: Introduce run() and sleep() callbacks
[simavr] / simavr / sim / sim_avr.h
1 /*
2         sim_avr.h
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 #ifndef __SIM_AVR_H__
23 #define __SIM_AVR_H__
24
25 #include <stdint.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 typedef uint64_t avr_cycle_count_t;
32 typedef uint16_t        avr_io_addr_t;
33
34 struct avr_t;
35 typedef uint8_t (*avr_io_read_t)(struct avr_t * avr, avr_io_addr_t addr, void * param);
36 typedef void (*avr_io_write_t)(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param);
37 typedef avr_cycle_count_t (*avr_cycle_timer_t)(struct avr_t * avr, avr_cycle_count_t when, void * param);
38
39 enum {
40         // SREG bit indexes
41         S_C = 0,S_Z,S_N,S_V,S_S,S_H,S_T,S_I,
42
43         // 16 bits register pairs
44         R_XL    = 0x1a, R_XH,R_YL,R_YH,R_ZL,R_ZH,
45         // stack pointer
46         R_SPL   = 32+0x3d, R_SPH,
47         // real SREG
48         R_SREG  = 32+0x3f,
49
50         // maximum number of IO registers, on normal AVRs
51         MAX_IOs = 256 - 32,     // minus 32 GP registers
52 };
53
54 #define AVR_DATA_TO_IO(v) ((v) - 32)
55 #define AVR_IO_TO_DATA(v) ((v) + 32)
56
57 /*
58  * Core states.
59  */
60 enum {
61         cpu_Limbo = 0,  // before initialization is finished
62         cpu_Stopped,    // all is stopped, timers included
63
64         cpu_Running,    // we're free running
65
66         cpu_Sleeping,   // we're now sleeping until an interrupt
67
68         cpu_Step,               // run ONE instruction, then...
69         cpu_StepDone,   // tell gdb it's all OK, and give it registers
70 };
71
72 /*
73  * Main AVR instance. Some of these fields are set by the AVR "Core" definition files
74  * the rest is runtime data (as little as possible)
75  */
76 typedef struct avr_t {
77         const char * mmcu;      // name of the AVR
78         // these are filled by sim_core_declare from constants in /usr/lib/avr/include/avr/io*.h
79         uint16_t        ramend;         
80         uint32_t        flashend;
81         uint32_t        e2end;
82         uint8_t         vector_size;
83         uint8_t         signature[3];
84         uint8_t         fuse[4];
85         avr_io_addr_t   rampz;  // optional, only for ELPM/SPM on >64Kb cores
86         avr_io_addr_t   eind;   // optional, only for EIJMP/EICALL on >64Kb cores
87
88         // filled by the ELF data, this allow tracking of invalid jumps
89         uint32_t                        codeend;
90
91         int                                     state;          // stopped, running, sleeping
92         uint32_t                        frequency;      // frequency we are running at
93         // mostly used by the ADC for now
94         uint32_t                        vcc,avcc,aref; // (optional) voltages in millivolts
95
96         // cycles gets incremented when sleeping and when running; it corresponds
97         // not only to "cycles that runs" but also "cycles that might have run"
98         // like, sleeping.
99         avr_cycle_count_t       cycle;          // current cycle
100         
101         // called at init time
102         void (*init)(struct avr_t * avr);
103         // called at init time (for special purposes like using a memory mapped file as flash see: simduino)
104         void (*special_init)(struct avr_t * avr);
105         // called at termination time ( to clean special initalizations)
106         void (*special_deinit)(struct avr_t * avr);
107         // called at reset time
108         void (*reset)(struct avr_t * avr);
109
110         /*
111          * Default AVR core run function.
112          * Two modes are available, a "raw" run that goes as fast as
113          * it can, and a "gdb" mode that also watchouts for gdb events
114          * and is a little bit slower.
115          */
116         void (*run)(struct avr_t * avr);
117
118         /*
119          * Sleep default behaviour.
120          * In "raw" mode, it calls usleep, in gdb mode, it waits
121          * for howLong for gdb command on it's sockets.
122          */
123         void (*sleep)(struct avr_t * avr, avr_cycle_count_t howLong);
124
125
126         // Mirror of the SREG register, to facilitate the access to bits
127         // in the opcode decoder.
128         // This array is re-synthetized back/forth when SREG changes
129         uint8_t         sreg[8];
130         uint8_t         i_shadow;       // used to detect edges on I flag
131
132         /* 
133          * ** current PC **
134          * Note that the PC is representing /bytes/ while the AVR value is
135          * assumed to be "words". This is in line with what GDB does...
136          * this is why you will see >>1 and <<1 in the decoder to handle jumps.
137          * It CAN be a little confusing, so concentrate, young grasshopper.
138          */
139         uint32_t        pc;
140
141         /*
142          * callback when specific IO registers are read/written.
143          * There is one drawback here, there is in way of knowing what is the
144          * "beginning of useful sram" on a core, so there is no way to deduce
145          * what is the maximum IO register for a core, and thus, we can't
146          * allocate this table dynamically.
147          * If you wanted to emulate the BIG AVRs, and XMegas, this would need
148          * work.
149          */
150         struct {
151                 struct avr_irq_t * irq; // optional, used only if asked for with avr_iomem_getirq()
152                 struct {
153                         void * param;
154                         avr_io_read_t c;
155                 } r;
156                 struct {
157                         void * param;
158                         avr_io_write_t c;
159                 } w;
160         } io[MAX_IOs];
161
162         // flash memory (initialized to 0xff, and code loaded into it)
163         uint8_t *       flash;
164         // this is the general purpose registers, IO registers, and SRAM
165         uint8_t *       data;
166
167         // queue of io modules
168         struct avr_io_t *io_port;
169
170         // cycle timers are callbacks that will be called when "when" cycle is reached
171         // the bitmap allows quick knowledge of whether there is anything to call
172         // these timers are one shots, then get cleared if the timer function returns zero,
173         // they get reset if the callback function returns a new cycle number
174         uint32_t        cycle_timer_map;
175         avr_cycle_count_t next_cycle_timer;
176         struct {
177                 avr_cycle_count_t       when;
178                 avr_cycle_timer_t       timer;
179                 void * param;
180         } cycle_timer[32];
181
182         // interrupt vectors, and their enable/clear registers
183         struct avr_int_vector_t * vector[64];
184         uint8_t         pending_wait;   // number of cycles to wait for pending
185         uint32_t        pending[2];             // pending interrupts
186
187         // DEBUG ONLY -- value ignored if CONFIG_SIMAVR_TRACE = 0
188         int             trace;
189
190 #if CONFIG_SIMAVR_TRACE
191         struct avr_symbol_t ** codeline;
192
193         /* DEBUG ONLY
194          * this keeps track of "jumps" ie, call,jmp,ret,reti and so on
195          * allows dumping of a meaningful data even if the stack is
196          * munched and so on
197          */
198         #define OLD_PC_SIZE     32
199         struct {
200                 uint32_t pc;
201                 uint16_t sp;
202         } old[OLD_PC_SIZE]; // catches reset..
203         int                     old_pci;
204
205 #if AVR_STACK_WATCH
206         #define STACK_FRAME_SIZE        32
207         // this records the call/ret pairs, to try to catch
208         // code that munches the stack -under- their own frame
209         struct {
210                 uint32_t        pc;
211                 uint16_t        sp;             
212         } stack_frame[STACK_FRAME_SIZE];
213         int                     stack_frame_index;
214 #endif
215
216         // DEBUG ONLY
217         // keeps track of which registers gets touched by instructions
218         // reset before each new instructions. Allows meaningful traces
219         uint32_t        touched[256 / 32];      // debug
220 #endif
221
222         // VALUE CHANGE DUMP file (waveforms)
223         // this is the VCD file that gets allocated if the 
224         // firmware that is loaded explicitly asks for a trace
225         // to be generated, and allocates it's own symbols
226         // using AVR_MMCU_TAG_VCD_TRACE (see avr_mcu_section.h)
227         struct avr_vcd_t * vcd;
228         
229         // gdb hooking structure. Only present when gdb server is active
230         struct avr_gdb_t * gdb;
231
232         // if non-zero, the gdb server will be started when the core
233         // crashed even if not activated at startup
234         // if zero, the simulator will just exit() in case of a crash
235         int             gdb_port;
236 } avr_t;
237
238
239 // this is a static constructor for each of the AVR devices
240 typedef struct avr_kind_t {
241         const char * names[4];  // name aliases
242         avr_t * (*make)();
243 } avr_kind_t;
244
245 // a symbol loaded from the .elf file
246 typedef struct avr_symbol_t {
247         const char * symbol;
248         uint32_t        addr;
249 } avr_symbol_t;
250
251 // locate the maker for mcu "name" and allocates a new avr instance
252 avr_t * avr_make_mcu_by_name(const char *name);
253 // initializes a new AVR instance. Will call the IO registers init(), and then reset()
254 int avr_init(avr_t * avr);
255 // resets the AVR, and the IO modules
256 void avr_reset(avr_t * avr);
257 // run one cycle of the AVR, sleep if necessary
258 int avr_run(avr_t * avr);
259 // finish any pending operations 
260 void avr_terminate(avr_t * avr);
261
262 // set an IO register to receive commands from the AVR firmware
263 // it's optional, and uses the ELF tags
264 void avr_set_command_register(avr_t * avr, avr_io_addr_t addr);
265
266 // specify the "console register" -- output sent to this register
267 // is printed on the simulator console, without using a UART
268 void avr_set_console_register(avr_t * avr, avr_io_addr_t addr);
269
270 // load code in the "flash"
271 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address);
272
273
274 /*
275  * these are accessors for avr->data but allows watchpoints to be set for gdb
276  * IO modules use that to set values to registers, and the AVR core decoder uses
277  * that to register "public" read by instructions.
278  */
279 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v);
280 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr);
281
282 // called when the core has detected a crash somehow.
283 // this might activate gdb server
284 void avr_sadly_crashed(avr_t *avr, uint8_t signal);
285
286
287 /*
288  * These are callbacks for the two 'main' bahaviour in simavr
289  */
290 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong);
291 void avr_callback_run_gdb(avr_t * avr);
292 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong);
293 void avr_callback_run_raw(avr_t * avr);
294
295 #ifdef __cplusplus
296 };
297 #endif
298
299 #include "sim_io.h"
300 #include "sim_regbit.h"
301 #include "sim_interrupts.h"
302 #include "sim_irq.h"
303 #include "sim_cycle_timers.h"
304
305 #endif /*__SIM_AVR_H__*/
306