SimAVR
AVR Simulator
sim_avr.h
Go to the documentation of this file.
1 /*
2  * sim_avr.h
3  *
4  * Copyright 2008-2012 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 it under the terms of the GNU
9  * General Public License as published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * simavr is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
13  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with simavr. If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
25 #ifndef __SIM_AVR_H__
26 #define __SIM_AVR_H__
27 
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #endif
32 
33 #include "sim_irq.h"
34 #include "sim_interrupts.h"
35 #include "sim_cycle_timers.h"
36 
40  enum
41  {
46  };
47 
48 #ifndef AVR_LOG
49 #define AVR_LOG(avr, level, ...) \
50  do { \
51  avr_global_logger(avr, level, __VA_ARGS__); \
52  } while(0)
53 #endif
54 
55 #define AVR_TRACE(avr, ... ) \
56  AVR_LOG(avr, LOG_TRACE, __VA_ARGS__)
57 
58  typedef uint32_t avr_flashaddr_t;
59 
60  struct avr_t;
61 
62  typedef uint8_t (*avr_io_read_t) (struct avr_t * avr, avr_io_addr_t addr, void *param);
63  typedef void (*avr_io_write_t) (struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void *param);
64 
66 #define IO_START_ADDRESS 0x20
67 #define AVR_IO_TO_DATA(v) ((v) + IO_START_ADDRESS)
69 #define AVR_DATA_TO_IO(v) ((v) - IO_START_ADDRESS)
71 
72  enum
73  {
75  S_C = 0, S_Z, S_N, S_V, S_S, S_H, S_T, S_I,
76 
78  R_XL = 0x1a, R_XH, R_YL, R_YH, R_ZL, R_ZH,
83 
84  // maximum number of IO registers, on normal AVRs
85  MAX_IOs = 280,
86  };
87 
89  enum
90  {
91  cpu_Limbo = 0,
99  };
100 
103  {
105 
110 #define OLD_PC_SIZE 32
111  struct
112  {
113  uint32_t pc;
114  uint16_t sp;
115  } old[OLD_PC_SIZE];
116  int old_pci;
117 
118 #if AVR_STACK_WATCH
119 #define STACK_FRAME_SIZE 32
120  struct
123  {
124  uint32_t pc;
125  uint16_t sp;
126  } stack_frame[STACK_FRAME_SIZE];
127  int stack_frame_index;
128 #endif
129 
133  // The touched structure map each register to a bit in a 32-bit array.
134  uint32_t touched[256 / 32];
135  };
136 
137  typedef void (*avr_run_t) (struct avr_t * avr);
138 
142  typedef struct avr_t
143  {
144  const char *mmcu;
145  // these are filled by sim_core_declare from constants in /usr/lib/avr/include/avr/io*.h
146  uint16_t ramend;
147  uint32_t flashend;
148  uint32_t e2end;
149  uint8_t vector_size;
150  uint8_t signature[3];
151  uint8_t fuse[4];
154  uint8_t address_size;
155 
156  uint32_t codeend;
157 
158  int state;
159  uint32_t frequency;
160  // mostly used by the ADC for now
161  uint32_t vcc, avcc, aref;
162 
163  // Cycles gets incremented when sleeping and when running; it corresponds not only to "cycles
164  // that runs" but also "cycles that might have run" like, sleeping.
166 
167  // These next two allow the core to freely run between cycle timers and also allows for a
168  // maximum run cycle limit... run_cycle_count is set during cycle timer processing.
171 
175  uint32_t sleep_usec;
176 
178  void (*init) (struct avr_t * avr);
180  void (*special_init) (struct avr_t * avr, void *data);
182  void (*special_deinit) (struct avr_t * avr, void *data);
186  void (*reset) (struct avr_t * avr);
187 
193 
198  void (*sleep) (struct avr_t * avr, avr_cycle_count_t howLong);
199 
204 
207  uint8_t sreg[8];
208 
215 
221  avr_flashaddr_t pc;
222 
229  struct
230  {
231  struct avr_irq_t *irq;
232  struct
233  {
234  void *param;
236  } r;
237  struct
238  {
239  void *param;
241  } w;
242  } io[MAX_IOs];
243 
252  struct
253  {
254  int used;
255  struct
256  {
257  void *param;
258  void *c;
259  } io[4];
260  } io_shared_io[4];
261 
263  uint8_t *flash;
265  uint8_t *data;
266 
268  struct avr_io_t *io_port;
269 
274 
276  uint8_t trace:1, log:2;
277 
280 
285  struct avr_vcd_t *vcd;
286 
288  struct avr_gdb_t *gdb;
289 
292  int gdb_port;
293  } avr_t;
294 
296  typedef struct avr_kind_t
297  {
298  const char *names[4];
299  avr_t *(*make) (void);
300  } avr_kind_t;
301 
303  typedef struct avr_symbol_t
304  {
305  uint32_t addr;
306  const char symbol[0];
307  } avr_symbol_t;
308 
310  avr_t *avr_make_mcu_by_name (const char *name);
312  int avr_init (avr_t * avr);
314  avr_t *avr_core_allocate (const avr_t * core, uint32_t coreLen);
315 
317  void avr_reset (avr_t * avr);
319  int avr_run (avr_t * avr);
321  void avr_terminate (avr_t * avr);
322 
325  void avr_set_command_register (avr_t * avr, avr_io_addr_t addr);
326 
329  void avr_set_console_register (avr_t * avr, avr_io_addr_t addr);
330 
332  void avr_loadcode (avr_t * avr, uint8_t * code, uint32_t size, avr_flashaddr_t address);
333 
339  void avr_core_watch_write (avr_t * avr, uint16_t addr, uint8_t v);
340  uint8_t avr_core_watch_read (avr_t * avr, uint16_t addr);
341 
344  void avr_sadly_crashed (avr_t * avr, uint8_t signal);
345 
347  void avr_global_logger (struct avr_t *avr, const int level, const char *format, ...);
348 
349 #ifndef AVR_CORE
350 #include <stdarg.h>
352  typedef void (*avr_logger_p) (struct avr_t * avr, const int level, const char *format, va_list ap);
353 
355  void avr_global_logger_set (avr_logger_p logger);
358 #endif
359 
363  void avr_callback_sleep_gdb (avr_t * avr, avr_cycle_count_t howLong);
364  void avr_callback_run_gdb (avr_t * avr);
365  void avr_callback_sleep_raw (avr_t * avr, avr_cycle_count_t howLong);
366  void avr_callback_run_raw (avr_t * avr);
367 
372  uint32_t avr_pending_sleep_usec (avr_t * avr, avr_cycle_count_t howLong);
373 
374 #ifdef __cplusplus
375 };
376 #endif
377 
378 /**************************************************************************************************/
379 
380 #include "sim_io.h"
381 #include "sim_regbit.h"
382 
383 /**************************************************************************************************/
384 
385 #ifdef __GNUC__
386 
387 #ifndef likely
388 #define likely(x) __builtin_expect(!!(x), 1)
389 #endif
390 
391 #ifndef unlikely
392 #define unlikely(x) __builtin_expect(!!(x), 0)
393 #endif
394 
395 #else /* ! __GNUC__ */
396 
397 #ifndef likely
398 #define likely(x) x
399 #endif
400 
401 #ifndef unlikely
402 #define unlikely(x) x
403 #endif
404 
405 #endif /* __GNUC__ */
406 
407 /**************************************************************************************************/
408 
409 #endif /*__SIM_AVR_H__*/
410 
Definition: sim_avr.h:78
uint16_t sp
Definition: sim_avr.h:114
struct avr_symbol_t ** codeline
Definition: sim_avr.h:104
void avr_callback_sleep_raw(avr_t *avr, avr_cycle_count_t howLong)
Definition: sim_avr.c:343
void * c
Definition: sim_avr.h:258
void avr_callback_sleep_gdb(avr_t *avr, avr_cycle_count_t howLong)
These are callbacks for the two 'main' behaviour in simavr.
Definition: sim_avr.c:283
uint32_t frequency
frequency we are running at
Definition: sim_avr.h:159
uint32_t e2end
Definition: sim_avr.h:148
Definition: sim_avr.h:42
#define AVR_IO_TO_DATA(v)
Map an IO address to the SRAM address space, i.e. add the register file offset to the address...
Definition: sim_avr.h:68
void avr_loadcode(avr_t *avr, uint8_t *code, uint32_t size, avr_flashaddr_t address)
Load code in the "flash".
Definition: sim_avr.c:252
void * param
Definition: sim_avr.h:234
Definition: sim_avr.h:44
we're free running
Definition: sim_avr.h:93
struct avr_gdb_t * gdb
gdb hooking structure. Only present when gdb server is active
Definition: sim_avr.h:288
avr_io_addr_t rampz
optional, only for ELPM/SPM on >64Kb cores
Definition: sim_avr.h:152
int old_pci
Definition: sim_avr.h:116
uint8_t vector_size
Definition: sim_avr.h:149
void avr_terminate(avr_t *avr)
Finish any pending operations.
Definition: sim_avr.c:118
int avr_run(avr_t *avr)
Run one cycle of the AVR, sleep if necessary.
Definition: sim_avr.c:396
Definition: sim_avr.h:78
uint16_t ramend
Definition: sim_avr.h:146
uint32_t aref
(optional) voltages in millivolts
Definition: sim_avr.h:161
struct avr_t::@27 io_shared_io[4]
struct avr_symbol_t avr_symbol_t
Symbol loaded from the .elf file.
uint16_t avr_io_addr_t
Definition: sim_avr_types.h:37
uint32_t addr
Definition: sim_avr.h:305
avr_flashaddr_t pc
Current PC Note that the PC is representing /bytes/ while the AVR value is assumed to be "words"...
Definition: sim_avr.h:221
16 bits register pairs
Definition: sim_avr.h:78
int gdb_port
if non-zero, the gdb server will be started when the core crashed even if not activated at startup if...
Definition: sim_avr.h:292
void avr_reset(avr_t *avr)
Resets the AVR, and the IO modules.
Definition: sim_avr.c:145
const char * mmcu
name of the AVR
Definition: sim_avr.h:144
uint8_t * data
SRAM memory, starting by the general purpose registers, and IO registers.
Definition: sim_avr.h:265
Definition: sim_avr.h:75
avr_t * avr
Definition: run_avr.c:54
Definition: sim_avr.h:78
avr_io_addr_t eind
optional, only for EIJMP/EICALL on >64Kb cores
Definition: sim_avr.h:153
Interrupt vectors, and their enable/clear registers.
Definition: sim_interrupts.h:51
int avr_init(avr_t *avr)
Initializes a new AVR instance. Will call the IO registers init(), and then reset() ...
Definition: sim_avr.c:78
struct avr_trace_data_t * trace_data
Only used if CONFIG_SIMAVR_TRACE is defined.
Definition: sim_avr.h:279
IRQ Pool structure.
Definition: sim_irq.h:69
uint8_t trace
DEBUG ONLY – value ignored if CONFIG_SIMAVR_TRACE = 0.
Definition: sim_avr.h:276
avr_io_read_t c
Definition: sim_avr.h:235
avr software stopped gracefully
Definition: sim_avr.h:97
uint32_t flashend
Definition: sim_avr.h:147
uint32_t avcc
Definition: sim_avr.h:161
void avr_set_command_register(avr_t *avr, avr_io_addr_t addr)
Set an IO register to receive commands from the AVR firmware it's optional, and uses the ELF tags...
Definition: sim_avr.c:216
Definition: sim_avr.h:75
avr_t * avr_make_mcu_by_name(const char *name)
Locate the maker for mcu "name" and allocates a new avr instance.
Definition: sim_avr.c:411
stack pointer
Definition: sim_avr.h:80
struct avr_t avr_t
Main AVR instance.
uint8_t address_size
2, or 3 for cores >128KB in flash
Definition: sim_avr.h:154
uint8_t fuse[4]
Definition: sim_avr.h:151
avr_cycle_timer_pool_t cycle_timers
Cycle timers tracking & delivery.
Definition: sim_avr.h:271
void(* special_deinit)(struct avr_t *avr, void *data)
called at termination time ( to clean special initializations)
Definition: sim_avr.h:182
uint32_t avr_flashaddr_t
Definition: sim_avr.h:58
struct avr_t::@26::@29 w
uint8_t log
log level, default to 1
Definition: sim_avr.h:276
uint32_t sleep_usec
Sleep requests are accumulated in sleep_usec until the minimum sleep value is reached, at which point sleep_usec is cleared and the sleep request is passed on to the operating system.
Definition: sim_avr.h:175
avr_cycle_count_t cycle
current cycle
Definition: sim_avr.h:165
all is stopped, timers included
Definition: sim_avr.h:92
Timer pool contains a pool of timer slots available, they all start queued into the 'free' qeueue...
Definition: sim_cycle_timers.h:70
void(* avr_io_write_t)(struct avr_t *avr, avr_io_addr_t addr, uint8_t v, void *param)
Definition: sim_avr.h:63
Definition: sim_avr.h:80
avr_int_table_t interrupts
Interrupt vectors and delivery fifo.
Definition: sim_avr.h:273
void avr_callback_run_gdb(avr_t *avr)
Definition: sim_avr.c:291
void(* reset)(struct avr_t *avr)
called at reset time
Definition: sim_avr.h:186
Definition: sim_avr.h:75
Definition: sim_avr.h:75
const char * names[4]
name aliases
Definition: sim_avr.h:298
void avr_global_logger(struct avr_t *avr, const int level, const char *format,...)
Logs a message using the current logger.
Definition: sim_avr.c:54
struct avr_t::@26 io[MAX_IOs]
Callback when specific IO registers are read/written.
void * special_data
value passed to special_init() and special_deinit()
Definition: sim_avr.h:184
uint8_t signature[3]
Definition: sim_avr.h:150
SREG bit indexes.
Definition: sim_avr.h:75
uint8_t sreg[8]
Mirror of the SREG register, to facilitate the access to bits in the opcode decoder.
Definition: sim_avr.h:207
void(* avr_run_t)(struct avr_t *avr)
Definition: sim_avr.h:137
Definition: sim_avr.h:75
Definition: sim_avr.h:75
real SREG
Definition: sim_avr.h:82
avr_cycle_count_t run_cycle_count
cycles to run before next timer
Definition: sim_avr.h:169
#define OLD_PC_SIZE
DEBUG ONLY This keeps track of "jumps" ie, call,jmp,ret,reti and so on allows dumping of a meaningful...
Definition: sim_avr.h:110
tell gdb it's all OK, and give it registers
Definition: sim_avr.h:96
void avr_global_logger_set(avr_logger_p logger)
Sets a global logging function in place of the default.
Definition: sim_avr.c:64
struct avr_t::@26::@28 r
struct avr_irq_t * irq
optional, used only if asked for with avr_iomem_getirq()
Definition: sim_avr.h:231
void avr_set_console_register(avr_t *avr, avr_io_addr_t addr)
Specify the "console register" – output sent to this register is printed on the simulator console...
Definition: sim_avr.c:245
avr_io_write_t c
Definition: sim_avr.h:240
uint8_t(* avr_io_read_t)(struct avr_t *avr, avr_io_addr_t addr, void *param)
Definition: sim_avr.h:62
uint32_t codeend
filled by the ELF data, this allow tracking of invalid jumps
Definition: sim_avr.h:156
uint64_t avr_cycle_count_t
Definition: sim_avr_types.h:36
Definition: sim_avr.h:45
Definition: sim_gdb.c:51
uint8_t * flash
Flash memory (initialized to 0xff, and code loaded into it)
Definition: sim_avr.h:263
uint32_t avr_pending_sleep_usec(avr_t *avr, avr_cycle_count_t howLong)
Accumulates sleep requests (and returns a sleep time of 0) until a minimum count of requested sleep m...
Definition: sim_avr.c:269
void(* init)(struct avr_t *avr)
Called at init time.
Definition: sim_avr.h:178
struct avr_kind_t avr_kind_t
Static constructor for each of the AVR devices.
avr_run_t run
Default AVR core run function.
Definition: sim_avr.h:192
void(* sleep)(struct avr_t *avr, avr_cycle_count_t howLong)
Sleep default behaviour.
Definition: sim_avr.h:198
Definition: sim_avr.h:78
avr software crashed (watchdog fired)
Definition: sim_avr.h:98
int io_shared_io_count
This block allows sharing of the IO write/read on addresses between multiple callbacks.
Definition: sim_avr.h:251
Symbol loaded from the .elf file.
Definition: sim_avr.h:303
This is only ever used if CONFIG_SIMAVR_TRACE is defined.
Definition: sim_avr.h:102
void avr_callback_run_raw(avr_t *avr)
Definition: sim_avr.c:353
Definition: sim_avr.h:78
uint32_t touched[256/32]
DEBUG ONLY keeps track of which registers gets touched by instructions reset before each new instruct...
Definition: sim_avr.h:134
we're now sleeping until an interrupt
Definition: sim_avr.h:94
Definition: sim_avr.h:43
run ONE instruction, then...
Definition: sim_avr.h:95
avr_logger_p avr_global_logger_get(void)
Gets the current global logger function.
Definition: sim_avr.c:70
void(* special_init)(struct avr_t *avr, void *data)
called at init time (for special purposes like using a memory mapped file as flash see: simduino) ...
Definition: sim_avr.h:180
int state
stopped, running, sleeping
Definition: sim_avr.h:158
struct avr_io_t * io_port
Queue of io modules.
Definition: sim_avr.h:268
void(* avr_logger_p)(struct avr_t *avr, const int level, const char *format, va_list ap)
Type for custom logging functions.
Definition: sim_avr.h:352
avr_irq_pool_t irq_pool
Every IRQs will be stored in this pool.
Definition: sim_avr.h:203
uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr)
Definition: sim_core.c:195
struct avr_vcd_t * vcd
Value Change Dump file (waveforms) This is the VCD file that gets allocated if the firmware that is l...
Definition: sim_avr.h:285
struct avr_trace_data_t::@25 old[OLD_PC_SIZE]
catches reset..
Main AVR instance.
Definition: sim_avr.h:142
IO module base struct Modules uses that as their first member in their own struct.
Definition: sim_io.h:42
Definition: sim_avr.h:75
int used
Definition: sim_avr.h:254
before initialization is finished
Definition: sim_avr.h:91
uint32_t pc
Definition: sim_avr.h:113
Public IRQ structure.
Definition: sim_irq.h:76
const char symbol[0]
Definition: sim_avr.h:306
void avr_sadly_crashed(avr_t *avr, uint8_t signal)
Called when the core has detected a crash somehow.
Definition: sim_avr.c:173
Bigger AVRs need more than 256-32 (mega1280)
Definition: sim_avr.h:85
avr_cycle_count_t run_cycle_limit
maximum run cycle interval limit
Definition: sim_avr.h:170
avr_t * avr_core_allocate(const avr_t *core, uint32_t coreLen)
Used by the cores, allocated a mutable avr_t from the const global.
Definition: sim_avr.c:403
Static constructor for each of the AVR devices.
Definition: sim_avr.h:296
uint32_t vcc
Definition: sim_avr.h:161
int8_t interrupt_state
Interrupt state: 00: idle (no wait, no pending interrupts) or disabled <0: wait till zero >0: interru...
Definition: sim_avr.h:214
Definition: sim_vcd_file.h:61
void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v)
These are accessors for avr->data but allows watchpoints to be set for gdb IO modules use that to set...
Definition: sim_core.c:156