88026f7637da7eef60479d6d10f1efaffbd7d55b
[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
29 struct avr_t;
30 typedef uint8_t (*avr_io_read_t)(struct avr_t * avr, uint8_t addr, void * param);
31 typedef void (*avr_io_write_t)(struct avr_t * avr, uint8_t addr, uint8_t v, void * param);
32 typedef avr_cycle_count_t (*avr_cycle_timer_t)(struct avr_t * avr, avr_cycle_count_t when, void * param);
33
34 enum {
35         // SREG bit indexes
36         S_C = 0,S_Z,S_N,S_V,S_S,S_H,S_T,S_I,
37
38         // 16 bits register pairs
39         R_XL    = 0x1a, R_XH,R_YL,R_YH,R_ZL,R_ZH,
40         // stack pointer
41         R_SPL   = 32+0x3d, R_SPH,
42         // real SREG
43         R_SREG  = 32+0x3f,
44
45         // maximum number of IO regisrer, on normal AVRs
46         MAX_IOs = 256 - 32,     // minus 32 GP registers
47 };
48
49 #define AVR_DATA_TO_IO(v) ((v) - 32)
50 #define AVR_IO_TO_DATA(v) ((v) + 32)
51
52 /*
53  * Core states. This will need populating with debug states for gdb
54  */
55 enum {
56         cpu_Limbo = 0,  // before initialization is finished
57         cpu_Stopped,
58         cpu_Running,
59         cpu_Sleeping,
60
61         cpu_Step,
62         cpu_StepDone,
63 };
64
65 /*
66  * Main AVR instance. Some of these fields are set by the AVR "Core" definition files
67  * the rest is runtime data (as little as possible)
68  */
69 typedef struct avr_t {
70         const char * mmcu;      // name of the AVR
71         // these are filled by sim_core_declare from constants in /usr/lib/avr/include/avr/io*.h
72         uint16_t        ramend;         
73         uint32_t        flashend;
74         uint32_t        e2end;
75         uint8_t         vector_size;
76         uint8_t         signature[3];
77         uint8_t         fuse[4];
78
79         // filled by the ELF data, this allow tracking of invalid jumps
80         uint32_t                        codeend;
81
82         int                                     state;          // stopped, running, sleeping
83         uint32_t                        frequency;      // frequency we are running at
84         avr_cycle_count_t       cycle;          // current cycle
85         
86         // called at init time
87         void (*init)(struct avr_t * avr);
88         // called at reset time
89         void (*reset)(struct avr_t * avr);
90
91         // Mirror of the SREG register, to facilitate the access to bits
92         // in the opcode decoder.
93         // This array is re-synthetized back/forth when SREG changes
94         uint8_t         sreg[8];
95
96         /* 
97          * ** current PC **
98          * Note that the PC is reoresenting /bytes/ while the AVR value is
99          * assumed to be "words". This is in line with what GDB does...
100          * this is why you will see >>1 ane <<1 in the decoder to handle jumps
101          */
102         uint32_t        pc;
103
104         /*
105          * callback when specific IO registers are read/written
106          */
107         struct {
108                 void * param;
109                 avr_io_read_t r;
110         } ior[MAX_IOs];
111         struct {
112                 void * param;
113                 avr_io_write_t w;
114         } iow[MAX_IOs];
115
116         // flash memory (initialized to 0xff, and code loaded into it)
117         uint8_t *       flash;
118         // this is the general purpose registers, IO registers, and SRAM
119         uint8_t *       data;
120
121         // queue of io modules
122         struct avr_io_t *io_port;
123
124         // cycle timers are callbacks that will be called when "when" cycle is reached
125         // the bitmap allows quick knowledge of whether there is anything to call
126         // these timers are one shots, then get cleared if the timer function returns zero,
127         // they get reset if the callback function returns a new cycle number
128         uint32_t        cycle_timer_map;
129         struct {
130                 avr_cycle_count_t       when;
131                 avr_cycle_timer_t       timer;
132                 void * param;
133         } cycle_timer[32];
134
135         // interrupt vectors, and their enable/clear registers
136         struct avr_int_vector_t * vector[64];
137         uint8_t         pending_wait;   // number of cycles to wait for pending
138         uint32_t        pending[2];             // pending interrupts
139
140         // DEBUG ONLY
141         int             trace;
142         struct avr_symbol_t ** codeline;
143
144         /* DEBUG ONLY
145          * this keeps track of "jumps" ie, call,jmp,ret,reti and so on
146          * allows dumping of a meaningful data even if the stack is
147          * munched and so on
148          */
149         #define OLD_PC_SIZE     32
150         struct {
151                 uint32_t pc;
152                 uint16_t sp;
153         } old[OLD_PC_SIZE]; // catches reset..
154         int                     old_pci;
155
156 #if AVR_STACK_WATCH
157         #define STACK_FRAME_SIZE        32
158         // this records the call/ret pairs, to try to catch
159         // code that munches the stack -under- their own frame
160         struct {
161                 uint32_t        pc;
162                 uint16_t        sp;             
163         } stack_frame[STACK_FRAME_SIZE];
164         int                     stack_frame_index;
165 #endif
166
167         // DEBUG ONLY
168         // keeps track of wich registers gets touched by instructions
169         // reset before each new instructions. Allows meaningful traces
170         uint32_t        touched[256 / 32];      // debug
171
172         // gdb hooking structure. Only present when gdb server is active
173         struct avr_gdb_t * gdb;
174         // if non-zero, the gdb server will be started when the core
175         // crashed even if not activated at startup
176         // if zero, the simulator will just exit() in case of a crash
177         int             gdb_port;
178 } avr_t;
179
180
181 // this is a static constructor for each of the AVR devices
182 typedef struct avr_kind_t {
183         const char * names[4];  // name aliases
184         avr_t * (*make)();
185 } avr_kind_t;
186
187 // a symbol loaded from the .elf file
188 typedef struct avr_symbol_t {
189         const char * symbol;
190         uint32_t        addr;
191 } avr_symbol_t;
192
193 // locate the maker for mcu "name" and allocates a new avr instance
194 avr_t * avr_make_mcu_by_name(const char *name);
195 // initializes a new AVR instance. Will call the IO registers init(), and then reset()
196 int avr_init(avr_t * avr);
197 // resets the AVR, and the IO modules
198 void avr_reset(avr_t * avr);
199 // run one cycle of the AVR, sleep if necessary
200 int avr_run(avr_t * avr);
201
202 // load code in the "flash"
203 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address);
204
205 // converts a nunber of usec to a nunber of machine cycles, at current speed
206 avr_cycle_count_t avr_usec_to_cycles(avr_t * avr, uint32_t usec);
207 // converts a number of hz (to megahertz etc) to a number of cycle
208 avr_cycle_count_t avr_hz_to_cycles(avr_t * avr, uint32_t hz);
209 // converts back a number of cycles to usecs (for usleep)
210 uint32_t avr_cycles_to_usec(avr_t * avr, avr_cycle_count_t cycles);
211
212 // register for calling 'timer' in 'when' cycles
213 void avr_cycle_timer_register(avr_t * avr, avr_cycle_count_t when, avr_cycle_timer_t timer, void * param);
214 // register a timer to call in 'when' usec
215 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param);
216 // cancel a previously set timer
217 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param);
218
219 /*
220  * these are accessors for avr->data but allows watchpoints to be set for gdb
221  * IO modules use that to set values to registers, and the AVR core decoder uses
222  * that to register "public" read by instructions.
223  */
224 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v);
225 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr);
226
227 // called when the core has detected a crash somehow.
228 // this might activate gdb server
229 void avr_sadly_crashed(avr_t *avr, uint8_t signal);
230
231 #include "sim_io.h"
232 #include "sim_regbit.h"
233 #include "sim_interrupts.h"
234 #include "sim_irq.h"
235
236 #endif /*__SIM_AVR_H__*/
237