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