4 Copyright 2008-2012 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/>.
30 #include "sim_interrupts.h"
31 #include "sim_cycle_timers.h"
33 typedef uint32_t avr_flashaddr_t;
36 typedef uint8_t (*avr_io_read_t)(
40 typedef void (*avr_io_write_t)(
48 S_C = 0,S_Z,S_N,S_V,S_S,S_H,S_T,S_I,
50 // 16 bits register pairs
51 R_XL = 0x1a, R_XH,R_YL,R_YH,R_ZL,R_ZH,
53 R_SPL = 32+0x3d, R_SPH,
57 // maximum number of IO registers, on normal AVRs
58 MAX_IOs = 279, // Bigger AVRs need more than 256-32 (mega1280)
61 #define AVR_DATA_TO_IO(v) ((v) - 32)
62 #define AVR_IO_TO_DATA(v) ((v) + 32)
69 cpu_Limbo = 0, // before initialization is finished
70 cpu_Stopped, // all is stopped, timers included
72 cpu_Running, // we're free running
74 cpu_Sleeping, // we're now sleeping until an interrupt
76 cpu_Step, // run ONE instruction, then...
77 cpu_StepDone, // tell gdb it's all OK, and give it registers
78 cpu_Done, // avr software stopped gracefully
79 cpu_Crashed, // avr software crashed (watchdog fired)
82 // this is only ever used if CONFIG_SIMAVR_TRACE is defined
83 struct avr_trace_data_t {
84 struct avr_symbol_t ** codeline;
87 * this keeps track of "jumps" ie, call,jmp,ret,reti and so on
88 * allows dumping of a meaningful data even if the stack is
91 #define OLD_PC_SIZE 32
95 } old[OLD_PC_SIZE]; // catches reset..
99 #define STACK_FRAME_SIZE 32
100 // this records the call/ret pairs, to try to catch
101 // code that munches the stack -under- their own frame
105 } stack_frame[STACK_FRAME_SIZE];
106 int stack_frame_index;
110 // keeps track of which registers gets touched by instructions
111 // reset before each new instructions. Allows meaningful traces
112 uint32_t touched[256 / 32]; // debug
116 * Main AVR instance. Some of these fields are set by the AVR "Core" definition files
117 * the rest is runtime data (as little as possible)
119 typedef struct avr_t {
120 const char * mmcu; // name of the AVR
121 // these are filled by sim_core_declare from constants in /usr/lib/avr/include/avr/io*.h
126 uint8_t signature[3];
128 avr_io_addr_t rampz; // optional, only for ELPM/SPM on >64Kb cores
129 avr_io_addr_t eind; // optional, only for EIJMP/EICALL on >64Kb cores
131 // filled by the ELF data, this allow tracking of invalid jumps
134 int state; // stopped, running, sleeping
135 uint32_t frequency; // frequency we are running at
136 // mostly used by the ADC for now
137 uint32_t vcc,avcc,aref; // (optional) voltages in millivolts
139 // cycles gets incremented when sleeping and when running; it corresponds
140 // not only to "cycles that runs" but also "cycles that might have run"
142 avr_cycle_count_t cycle; // current cycle
145 * Sleep requests are accumulated in sleep_usec until the minimum sleep value
146 * is reached, at which point sleep_usec is cleared and the sleep request
147 * is passed on to the operating system.
151 // called at init time
152 void (*init)(struct avr_t * avr);
153 // called at init time (for special purposes like using a memory mapped file as flash see: simduino)
154 void (*special_init)(struct avr_t * avr);
155 // called at termination time ( to clean special initializations)
156 void (*special_deinit)(struct avr_t * avr);
157 // called at reset time
158 void (*reset)(struct avr_t * avr);
161 * Default AVR core run function.
162 * Two modes are available, a "raw" run that goes as fast as
163 * it can, and a "gdb" mode that also watchouts for gdb events
164 * and is a little bit slower.
166 void (*run)(struct avr_t * avr);
169 * Sleep default behaviour.
170 * In "raw" mode, it calls usleep, in gdb mode, it waits
171 * for howLong for gdb command on it's sockets.
173 void (*sleep)(struct avr_t * avr, avr_cycle_count_t howLong);
176 * Every IRQs will be stored in this pool. It is not
177 * mandatory (yet) but will allow listing IRQs and their connections
179 avr_irq_pool_t irq_pool;
181 // Mirror of the SREG register, to facilitate the access to bits
182 // in the opcode decoder.
183 // This array is re-synthesized back/forth when SREG changes
185 uint8_t i_shadow; // used to detect edges on I flag
189 * Note that the PC is representing /bytes/ while the AVR value is
190 * assumed to be "words". This is in line with what GDB does...
191 * this is why you will see >>1 and <<1 in the decoder to handle jumps.
192 * It CAN be a little confusing, so concentrate, young grasshopper.
197 * callback when specific IO registers are read/written.
198 * There is one drawback here, there is in way of knowing what is the
199 * "beginning of useful sram" on a core, so there is no way to deduce
200 * what is the maximum IO register for a core, and thus, we can't
201 * allocate this table dynamically.
202 * If you wanted to emulate the BIG AVRs, and XMegas, this would need
206 struct avr_irq_t * irq; // optional, used only if asked for with avr_iomem_getirq()
218 * This block allows sharing of the IO write/read on addresses between
219 * multiple callbacks. In 99% of case it's not needed, however on the tiny*
220 * (tiny85 at last) some registers have bits that are used by different
222 * If this case is detected, a special "dispatch" callback is installed that
223 * will handle this particular case, without impacting the performance of the
224 * other, normal cases...
226 int io_shared_io_count;
235 // flash memory (initialized to 0xff, and code loaded into it)
237 // this is the general purpose registers, IO registers, and SRAM
240 // queue of io modules
241 struct avr_io_t *io_port;
243 // cycle timers tracking & delivery
244 avr_cycle_timer_pool_t cycle_timers;
245 // interrupt vectors and delivery fifo
246 avr_int_table_t interrupts;
248 // DEBUG ONLY -- value ignored if CONFIG_SIMAVR_TRACE = 0
250 log : 2; // log level, default to 1
252 // Only used if CONFIG_SIMAVR_TRACE is defined
253 struct avr_trace_data_t *trace_data;
255 // VALUE CHANGE DUMP file (waveforms)
256 // this is the VCD file that gets allocated if the
257 // firmware that is loaded explicitly asks for a trace
258 // to be generated, and allocates it's own symbols
259 // using AVR_MMCU_TAG_VCD_TRACE (see avr_mcu_section.h)
260 struct avr_vcd_t * vcd;
262 // gdb hooking structure. Only present when gdb server is active
263 struct avr_gdb_t * gdb;
265 // if non-zero, the gdb server will be started when the core
266 // crashed even if not activated at startup
267 // if zero, the simulator will just exit() in case of a crash
272 // this is a static constructor for each of the AVR devices
273 typedef struct avr_kind_t {
274 const char * names[4]; // name aliases
278 // a symbol loaded from the .elf file
279 typedef struct avr_symbol_t {
284 // locate the maker for mcu "name" and allocates a new avr instance
286 avr_make_mcu_by_name(
288 // initializes a new AVR instance. Will call the IO registers init(), and then reset()
292 // Used by the cores, allocated a mutable avr_t from the const global
298 // resets the AVR, and the IO modules
302 // run one cycle of the AVR, sleep if necessary
306 // finish any pending operations
311 // set an IO register to receive commands from the AVR firmware
312 // it's optional, and uses the ELF tags
314 avr_set_command_register(
318 // specify the "console register" -- output sent to this register
319 // is printed on the simulator console, without using a UART
321 avr_set_console_register(
325 // load code in the "flash"
331 avr_flashaddr_t address);
334 * These are accessors for avr->data but allows watchpoints to be set for gdb
335 * IO modules use that to set values to registers, and the AVR core decoder uses
336 * that to register "public" read by instructions.
339 avr_core_watch_write(
348 // called when the core has detected a crash somehow.
349 // this might activate gdb server
357 * These are callbacks for the two 'main' behaviour in simavr
359 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong);
360 void avr_callback_run_gdb(avr_t * avr);
361 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong);
362 void avr_callback_run_raw(avr_t * avr);
365 * Logging macros and associated log levels.
366 * The current log level is kept in avr->log.
373 typedef void (*logger_t)(avr_t* avr, const int level, const char * format, ... );
374 extern logger_t global_logger;
376 #define AVR_LOG(avr, level, ...) \
378 global_logger( avr, level, __VA_ARGS__); \
387 #include "sim_regbit.h"
389 #endif /*__SIM_AVR_H__*/