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/>.
27 typedef uint64_t avr_cycle_count_t;
28 typedef uint16_t avr_io_addr_t;
31 typedef uint8_t (*avr_io_read_t)(struct avr_t * avr, avr_io_addr_t addr, void * param);
32 typedef void (*avr_io_write_t)(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param);
33 typedef avr_cycle_count_t (*avr_cycle_timer_t)(struct avr_t * avr, avr_cycle_count_t when, void * param);
37 S_C = 0,S_Z,S_N,S_V,S_S,S_H,S_T,S_I,
39 // 16 bits register pairs
40 R_XL = 0x1a, R_XH,R_YL,R_YH,R_ZL,R_ZH,
42 R_SPL = 32+0x3d, R_SPH,
46 // maximum number of IO regisrer, on normal AVRs
47 MAX_IOs = 256 - 32, // minus 32 GP registers
50 #define AVR_DATA_TO_IO(v) ((v) - 32)
51 #define AVR_IO_TO_DATA(v) ((v) + 32)
54 * Core states. This will need populating with debug states for gdb
57 cpu_Limbo = 0, // before initialization is finished
67 * Main AVR instance. Some of these fields are set by the AVR "Core" definition files
68 * the rest is runtime data (as little as possible)
70 typedef struct avr_t {
71 const char * mmcu; // name of the AVR
72 // these are filled by sim_core_declare from constants in /usr/lib/avr/include/avr/io*.h
80 // filled by the ELF data, this allow tracking of invalid jumps
83 int state; // stopped, running, sleeping
84 uint32_t frequency; // frequency we are running at
85 avr_cycle_count_t cycle; // current cycle
87 // called at init time
88 void (*init)(struct avr_t * avr);
89 // called at reset time
90 void (*reset)(struct avr_t * avr);
92 // Mirror of the SREG register, to facilitate the access to bits
93 // in the opcode decoder.
94 // This array is re-synthetized back/forth when SREG changes
99 * Note that the PC is reoresenting /bytes/ while the AVR value is
100 * assumed to be "words". This is in line with what GDB does...
101 * this is why you will see >>1 ane <<1 in the decoder to handle jumps
106 * callback when specific IO registers are read/written
107 * these should probably be allocated dynamically in init()..
118 // flash memory (initialized to 0xff, and code loaded into it)
120 // this is the general purpose registers, IO registers, and SRAM
123 // queue of io modules
124 struct avr_io_t *io_port;
126 // cycle timers are callbacks that will be called when "when" cycle is reached
127 // the bitmap allows quick knowledge of whether there is anything to call
128 // these timers are one shots, then get cleared if the timer function returns zero,
129 // they get reset if the callback function returns a new cycle number
130 uint32_t cycle_timer_map;
132 avr_cycle_count_t when;
133 avr_cycle_timer_t timer;
137 // interrupt vectors, and their enable/clear registers
138 struct avr_int_vector_t * vector[64];
139 uint8_t pending_wait; // number of cycles to wait for pending
140 uint32_t pending[2]; // pending interrupts
145 #if CONFIG_SIMAVR_TRACE
146 struct avr_symbol_t ** codeline;
149 * this keeps track of "jumps" ie, call,jmp,ret,reti and so on
150 * allows dumping of a meaningful data even if the stack is
153 #define OLD_PC_SIZE 32
157 } old[OLD_PC_SIZE]; // catches reset..
161 #define STACK_FRAME_SIZE 32
162 // this records the call/ret pairs, to try to catch
163 // code that munches the stack -under- their own frame
167 } stack_frame[STACK_FRAME_SIZE];
168 int stack_frame_index;
172 // keeps track of which registers gets touched by instructions
173 // reset before each new instructions. Allows meaningful traces
174 uint32_t touched[256 / 32]; // debug
177 // gdb hooking structure. Only present when gdb server is active
178 struct avr_gdb_t * gdb;
179 // if non-zero, the gdb server will be started when the core
180 // crashed even if not activated at startup
181 // if zero, the simulator will just exit() in case of a crash
186 // this is a static constructor for each of the AVR devices
187 typedef struct avr_kind_t {
188 const char * names[4]; // name aliases
192 // a symbol loaded from the .elf file
193 typedef struct avr_symbol_t {
198 // locate the maker for mcu "name" and allocates a new avr instance
199 avr_t * avr_make_mcu_by_name(const char *name);
200 // initializes a new AVR instance. Will call the IO registers init(), and then reset()
201 int avr_init(avr_t * avr);
202 // resets the AVR, and the IO modules
203 void avr_reset(avr_t * avr);
204 // run one cycle of the AVR, sleep if necessary
205 int avr_run(avr_t * avr);
207 // load code in the "flash"
208 void avr_loadcode(avr_t * avr, uint8_t * code, uint32_t size, uint32_t address);
210 // converts a nunber of usec to a nunber of machine cycles, at current speed
211 avr_cycle_count_t avr_usec_to_cycles(avr_t * avr, uint32_t usec);
212 // converts a number of hz (to megahertz etc) to a number of cycle
213 avr_cycle_count_t avr_hz_to_cycles(avr_t * avr, uint32_t hz);
214 // converts back a number of cycles to usecs (for usleep)
215 uint32_t avr_cycles_to_usec(avr_t * avr, avr_cycle_count_t cycles);
217 // register for calling 'timer' in 'when' cycles
218 void avr_cycle_timer_register(avr_t * avr, avr_cycle_count_t when, avr_cycle_timer_t timer, void * param);
219 // register a timer to call in 'when' usec
220 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param);
221 // cancel a previously set timer
222 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param);
225 * these are accessors for avr->data but allows watchpoints to be set for gdb
226 * IO modules use that to set values to registers, and the AVR core decoder uses
227 * that to register "public" read by instructions.
229 void avr_core_watch_write(avr_t *avr, uint16_t addr, uint8_t v);
230 uint8_t avr_core_watch_read(avr_t *avr, uint16_t addr);
232 // called when the core has detected a crash somehow.
233 // this might activate gdb server
234 void avr_sadly_crashed(avr_t *avr, uint8_t signal);
237 #include "sim_regbit.h"
238 #include "sim_interrupts.h"
241 #endif /*__SIM_AVR_H__*/