4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
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.
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.
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/>.
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);
33 S_C = 0,S_Z,S_N,S_V,S_S,S_H,S_T,S_I,
35 // 16 bits register pairs
36 R_XL = 0x1a, R_XH,R_YL,R_YH,R_ZL,R_ZH,
38 R_SPL = 32+0x3d, R_SPH,
42 // maximum number of IO regisrer, on normal AVRs
43 MAX_IOs = 256 - 32, // minus 32 GP registers
46 #define AVR_DATA_TO_IO(v) ((v) - 32)
47 #define AVR_IO_TO_DATA(v) ((v) + 32)
50 * Core states. This will need populating with debug states for gdb
53 cpu_Limbo = 0, // before initialization is finished
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)
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
73 // filled by the ELF data, this allow tracking of invalid jumps
76 int state; // stopped, running, sleeping
77 uint32_t frequency; // frequency we are running at
78 uint64_t cycle; // current cycle
80 // called at init time
81 void (*init)(struct avr_t * avr);
82 // called at reset time
83 void (*reset)(struct avr_t * avr);
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
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
99 * callback when specific IO registers are read/written
110 // flash memory (initialized to 0xff, and code loaded into it)
112 // this is the general purpose registers, IO registers, and SRAM
115 // queue of io modules
116 struct avr_io_t *io_port;
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
125 struct avr_symbol_t ** codeline;
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
132 #define OLD_PC_SIZE 32
136 } old[OLD_PC_SIZE]; // catches reset..
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
146 } stack_frame[STACK_FRAME_SIZE];
147 int stack_frame_index;
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
156 struct avr_gdb_t * gdb;
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
166 // a symbol loaded from the .elf file
167 typedef struct avr_symbol_t {
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.
178 typedef struct avr_regbit_t {
179 unsigned long reg : 8, bit : 3, mask : 8;
182 // interupt vector for the IO modules
183 typedef struct avr_int_vector_t {
184 uint8_t vector; // vector number, zero (reset) is reserved
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)
191 * used by the ioports to implement their own features
192 * see avr_eeprom.* for an example, and avr_ioctl().
194 #define AVR_IOCTL_DEF(_a,_b,_c,_d) \
195 (((_a) << 24)|((_b) << 16)|((_c) << 8)|((_d)))
198 * IO module base struct
199 * Modules uses that as their first member in their own struct
201 typedef struct avr_io_t {
202 struct avr_io_t * next;
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);
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);
217 // load code in the "flash"
218 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address);
221 * IO modules helper functions
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);
236 * Interupt Helper Functions
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);
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.
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);
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
264 * set/get/clear io register bits in one operation
266 static inline uint8_t avr_regbit_set(avr_t * avr, avr_regbit_t rb)
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;
276 static inline uint8_t avr_regbit_setto(avr_t * avr, avr_regbit_t rb, uint8_t v)
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;
286 static inline uint8_t avr_regbit_get(avr_t * avr, avr_regbit_t rb)
291 //uint8_t m = rb.mask << rb.bit;
292 return (avr->data[a] >> rb.bit) & rb.mask;
295 static inline uint8_t avr_regbit_clear(avr_t * avr, avr_regbit_t rb)
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);
303 #define ARRAY_SIZE(_aa) (sizeof(_aa) / sizeof((_aa)[0]))
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.
310 static inline uint8_t avr_regbit_get_array(avr_t * avr, avr_regbit_t *rb, int count)
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;
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) }
325 * Internal IRQ system
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
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
334 * IRQ calling order is not defined, so don't rely on it.
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.
339 // internal structure for a hook, never seen by the notify procs
342 typedef void (*avr_irq_notify_t)(avr_t * avr, struct avr_irq_t * irq, uint32_t value, void * param);
344 typedef struct avr_irq_hook_t {
345 struct avr_irq_hook_t * next;
347 int busy; // prevent reentrance of callbacks
348 avr_irq_notify_t notify;
351 typedef struct avr_irq_t {
354 avr_irq_hook_t * hook;
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);
364 #endif /*__SIMAVR_H__*/