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