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