Cores, decoder, uart, ioports - lots of changes
[simavr] / simavr / sim / simavr.h
1 /*
2         simavr.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 __SIMAVR_H__
23 #define __SIMAVR_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
59 /*
60  * Main AVR instance. Some of these fields are set by the AVR "Core" definition files
61  * the rest is runtime data (as little as possible)
62  */
63 typedef struct avr_t {
64         const char * mmcu;      // name of the AVR
65         // these are filled by sim_core_declare from constants in /usr/lib/avr/include/avr/io*.h
66         uint16_t        ramend;         
67         uint32_t        flashend;
68         uint32_t        e2end;
69         uint8_t         vector_size;
70         uint8_t         signature[3];
71         uint8_t         fuse[4];
72
73         // filled by the ELF data, this allow tracking of invalid jumps
74         uint32_t        codeend;
75
76         int                     state;          // stopped, running, sleeping
77         uint32_t        frequency;      // frequency we are running at
78         uint64_t        cycle;          // current cycle
79         
80         // called at init time
81         void (*init)(struct avr_t * avr);
82         // called at reset time
83         void (*reset)(struct avr_t * avr);
84
85         // Mirror of the SREG register, to facilitate the access to bits
86         // in the opcode decoder.
87         // This array is re-synthetized back/forth when SREG changes
88         uint8_t         sreg[8];
89
90         /* 
91          * ** current PC **
92          * Note that the PC is reoresenting /bytes/ while the AVR value is
93          * assumed to be "words". This is in line with what GDB does...
94          * this is why you will see >>1 ane <<1 in the decoder to handle jumps
95          */
96         uint32_t        pc;
97
98         /*
99          * callback when specific IO registers are read/written
100          */
101         struct {
102                 void * param;
103                 avr_io_read_t r;
104         } ior[MAX_IOs];
105         struct {
106                 void * param;
107                 avr_io_write_t w;
108         } iow[MAX_IOs];
109
110         // flash memory (initialized to 0xff, and code loaded into it)
111         uint8_t *       flash;
112         // this is the general purpose registers, IO registers, and SRAM
113         uint8_t *       data;
114
115         // queue of io modules
116         struct avr_io_t *io_port;
117
118         // interupt vectors, and their enable/clear registers
119         struct avr_int_vector_t * vector[64];
120         uint8_t         pending_wait;   // number of cycles to wait for pending
121         uint32_t        pending[2];             // pending interupts
122
123         // DEBUG ONLY
124         int             trace;
125         struct avr_symbol_t ** codeline;
126
127         /* DEBUG ONLY
128          * this keeps track of "jumps" ie, call,jmp,ret,reti and so on
129          * allows dumping of a meaningful data even if the stack is
130          * munched and so on
131          */
132         #define OLD_PC_SIZE     32
133         struct {
134                 uint32_t pc;
135                 uint16_t sp;
136         } old[OLD_PC_SIZE]; // catches reset..
137         int                     old_pci;
138
139 #if AVR_STACK_WATCH
140         #define STACK_FRAME_SIZE        32
141         // this records the call/ret pairs, to try to catch
142         // code that munches the stack -under- their own frame
143         struct {
144                 uint32_t        pc;
145                 uint16_t        sp;             
146         } stack_frame[STACK_FRAME_SIZE];
147         int                     stack_frame_index;
148 #endif
149
150         // DEBUG ONLY
151         // keeps track of wich registers gets touched by instructions
152         // reset before each new instructions. Allows meaningful traces
153         uint32_t        touched[256 / 32];      // debug
154
155         // placeholder
156         struct avr_gdb_t * gdb;
157 } avr_t;
158
159
160 // this is a static constructor for each of the AVR devices
161 typedef struct avr_kind_t {
162         const char * names[4];  // name aliases
163         avr_t * (*make)();
164 } avr_kind_t;
165
166 // a symbol loaded from the .elf file
167 typedef struct avr_symbol_t {
168         const char * symbol;
169         uint32_t        addr;
170 } avr_symbol_t;
171
172 /*
173  * this 'structure' is a packed representation of an IO register 'bit'
174  * (or consecutive bits). This allows a way to set/get/clear them.
175  * gcc is happy passing these as register value, so you don't need to
176  * use a pointer when passing them along to functions.
177  */
178 typedef struct avr_regbit_t {
179         unsigned long reg : 8, bit : 3, mask : 8;
180 } avr_regbit_t;
181
182 // interupt vector for the IO modules
183 typedef struct avr_int_vector_t {
184         uint8_t vector;         // vector number, zero (reset) is reserved
185
186         avr_regbit_t enable;    // IO register index for the "interupt enable" flag for this vector
187         avr_regbit_t raised;    // IO register index for the register where the "raised" flag is (optional)
188 } avr_int_vector_t;
189
190 /*
191  * used by the ioports to implement their own features
192  * see avr_eeprom.* for an example, and avr_ioctl().
193  */
194 #define AVR_IOCTL_DEF(_a,_b,_c,_d) \
195         (((_a) << 24)|((_b) << 16)|((_c) << 8)|((_d)))
196
197 /*
198  * IO module base struct
199  * Modules uses that as their first member in their own struct
200  */
201 typedef struct avr_io_t {
202         struct avr_io_t * next;
203         const char * kind;
204         // called at every instruction
205         void (*run)(avr_t * avr, struct avr_io_t *io);
206         // called at reset time
207         void (*reset)(avr_t * avr, struct avr_io_t *io);
208         // called externally. allow access to io modules and so on
209         int (*ioctl)(avr_t * avr, struct avr_io_t *io, uint32_t ctl, void *io_param);
210 } avr_io_t;
211
212 // initializes a new AVR instance. Will call the IO registers init(), and then reset()
213 int avr_init(avr_t * avr);
214 // resets the AVR, and the IO modules
215 void avr_reset(avr_t * avr);
216
217 // load code in the "flash"
218 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address);
219
220 /*
221  * IO modules helper functions
222  */
223
224 // registers an IO module, so it's run(), reset() etc are called
225 // this is called by the AVR core init functions, you /could/ register an external
226 // one after instanciation, for whatever purpose...
227 void avr_register_io(avr_t *avr, avr_io_t * io);
228 // register a callback for when IO register "addr" is read
229 void avr_register_io_read(avr_t *avr, uint8_t addr, avr_io_read_t read, void * param);
230 // register a callback for when the IO register is written. callback has to set the memory itself
231 void avr_register_io_write(avr_t *avr, uint8_t addr, avr_io_write_t write, void * param);
232 // call every IO modules until one responds to this
233 int avr_ioctl(avr_t *avr, uint32_t ctl, void * io_param);
234
235 /*
236  * Interupt Helper Functions
237  */
238 // register an interupt vector. It's only needed if you want to use the "r_raised" flags
239 void avr_register_vector(avr_t *avr, avr_int_vector_t * vector);
240 // raise an interupt (if enabled). The interupt is latched and will be called later
241 // return non-zero if the interupt was raised and is now pending
242 int avr_raise_interupt(avr_t * avr, avr_int_vector_t * vector);
243 // return non-zero if the AVR core has any pending interupts
244 int avr_has_pending_interupts(avr_t * avr);
245 // return nonzero if a soecific interupt vector is pending
246 int avr_is_interupt_pending(avr_t * avr, avr_int_vector_t * vector);
247
248 /*
249  * these are accessors for avr->data but allows watchpoints to be set for gdb
250  * IO modules use that to set values to registers, and the AVR core decoder uses
251  * that to register "public" read by instructions.
252  */
253 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v);
254 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr);
255
256
257 /*
258  * These accessors are inlined and are used to perform the operations on
259  * avr_regbit_t definitions. This is the "official" way to access bits into registers
260  * The small footorint costs brings much better versatility for functions/bits that are
261  * not always defined in the same place on real AVR cores
262  */
263 /*
264  * set/get/clear io register bits in one operation
265  */
266 static inline uint8_t avr_regbit_set(avr_t * avr, avr_regbit_t rb)
267 {
268         uint8_t a = rb.reg;
269         if (!a)
270                 return 0;
271         uint8_t m = rb.mask << rb.bit;
272         avr_core_watch_write(avr, a, avr->data[a] | m);
273         return (avr->data[a] >> rb.bit) & rb.mask;
274 }
275
276 static inline uint8_t avr_regbit_setto(avr_t * avr, avr_regbit_t rb, uint8_t v)
277 {
278         uint8_t a = rb.reg;
279         if (!a)
280                 return 0;
281         uint8_t m = rb.mask << rb.bit;
282         avr_core_watch_write(avr, a, (avr->data[a] & ~(m)) | ((v << rb.bit) & m));
283         return (avr->data[a] >> rb.bit) & rb.mask;
284 }
285
286 static inline uint8_t avr_regbit_get(avr_t * avr, avr_regbit_t rb)
287 {
288         uint8_t a = rb.reg;
289         if (!a)
290                 return 0;
291         //uint8_t m = rb.mask << rb.bit;
292         return (avr->data[a] >> rb.bit) & rb.mask;
293 }
294
295 static inline uint8_t avr_regbit_clear(avr_t * avr, avr_regbit_t rb)
296 {
297         uint8_t a = (rb.reg);
298         uint8_t m = rb.mask << rb.bit;
299         avr_core_watch_write(avr, a, avr->data[a] & ~m);
300         return avr->data[a];
301 }
302
303 #define ARRAY_SIZE(_aa) (sizeof(_aa) / sizeof((_aa)[0]))
304
305 /*
306  * This reads the bits for an array of avr_regbit_t, make up a "byte" with them.
307  * This allows reading bits like CS0, CS1, CS2 etc even if they are not in the same
308  * physical IO register.
309  */
310 static inline uint8_t avr_regbit_get_array(avr_t * avr, avr_regbit_t *rb, int count)
311 {
312         uint8_t res = 0;
313
314         for (int i = 0; i < count; i++, rb++) if (rb->reg) {
315                 uint8_t a = (rb->reg);
316                 res |= ((avr->data[a] >> rb->bit) & rb->mask) << i;
317         }
318         return res;
319 }
320
321 #define AVR_IO_REGBIT(_io, _bit) { . reg = (_io), .bit = (_bit), .mask = 1 }
322 #define AVR_IO_REGBITS(_io, _bit, _mask) { . reg = (_io), .bit = (_bit), .mask = (_mask) }
323
324 /*
325  * Internal IRQ system
326  * 
327  * This subsystem allow any piece of code to "register" a hook to be called when an IRQ is
328  * raised. The IRQ definition is up to the module defining it, for example a IOPORT pin change
329  * might be an IRQ in wich case any oiece of code can be notified when a pin has changed state
330  * 
331  * The notify hooks are chained, and duplicates are filtered out so you can't register a
332  * notify hook twice on one particylar IRQ
333  * 
334  * IRQ calling order is not defined, so don't rely on it.
335  * 
336  * IRQ hook needs to be registered in reset() handlers, ie after all modules init() bits
337  * have been called, to prevent race condition of the initialization order.
338  */
339 // internal structure for a hook, never seen by the notify procs
340 struct avr_irq_t;
341
342 typedef void (*avr_irq_notify_t)(avr_t * avr, struct avr_irq_t * irq, uint32_t value, void * param);
343
344 typedef struct avr_irq_hook_t {
345         struct avr_irq_hook_t * next;
346         void * param;
347         int busy;       // prevent reentrance of callbacks
348         avr_irq_notify_t notify;
349 } avr_irq_hook_t;
350
351 typedef struct avr_irq_t {
352         uint32_t                        irq;
353         uint32_t                        value;
354         avr_irq_hook_t *        hook;
355 } avr_irq_t;
356
357 avr_irq_t * avr_alloc_irq(avr_t * avr, uint32_t base, uint32_t count);
358 void avr_init_irq(avr_t * avr, avr_irq_t * irq, uint32_t base, uint32_t count);
359 void avr_raise_irq(avr_t * avr, avr_irq_t * irq, uint32_t value);
360 // this connects a "source" IRQ to a "destination" IRQ
361 void avr_connect_irq(avr_t * avr, avr_irq_t * src, avr_irq_t * dst);
362 void avr_irq_register_notify(avr_t * avr, avr_irq_t * irq, avr_irq_notify_t notify, void * param);
363
364 #endif /*__SIMAVR_H__*/
365