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