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