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