core: Increased the number of possible IOs
[simavr] / simavr / sim / sim_avr.h
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
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.
12
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.
17
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/>.
20  */
21
22 #ifndef __SIM_AVR_H__
23 #define __SIM_AVR_H__
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #include "sim_irq.h"
30 #include "sim_interrupts.h"
31 #include "sim_cycle_timers.h"
32
33 struct avr_t;
34 typedef uint8_t (*avr_io_read_t)(
35                 struct avr_t * avr,
36                 avr_io_addr_t addr,
37                 void * param);
38 typedef void (*avr_io_write_t)(
39                 struct avr_t * avr,
40                 avr_io_addr_t addr,
41                 uint8_t v,
42                 void * param);
43
44 enum {
45         // SREG bit indexes
46         S_C = 0,S_Z,S_N,S_V,S_S,S_H,S_T,S_I,
47
48         // 16 bits register pairs
49         R_XL    = 0x1a, R_XH,R_YL,R_YH,R_ZL,R_ZH,
50         // stack pointer
51         R_SPL   = 32+0x3d, R_SPH,
52         // real SREG
53         R_SREG  = 32+0x3f,
54
55         // maximum number of IO registers, on normal AVRs
56         MAX_IOs = 256,  // Bigger AVRs need more than 256-32 (mega1280)
57 };
58
59 #define AVR_DATA_TO_IO(v) ((v) - 32)
60 #define AVR_IO_TO_DATA(v) ((v) + 32)
61
62 /*
63  * Core states.
64  */
65 enum {
66         cpu_Limbo = 0,  // before initialization is finished
67         cpu_Stopped,    // all is stopped, timers included
68
69         cpu_Running,    // we're free running
70
71         cpu_Sleeping,   // we're now sleeping until an interrupt
72
73         cpu_Step,               // run ONE instruction, then...
74         cpu_StepDone,   // tell gdb it's all OK, and give it registers
75         cpu_Done,       // avr software stopped gracefully
76         cpu_Crashed,    // avr software crashed (watchdog fired)
77 };
78
79 // this is only ever used if CONFIG_SIMAVR_TRACE is defined
80 struct avr_trace_data_t {
81         struct avr_symbol_t ** codeline;
82
83         /* DEBUG ONLY
84          * this keeps track of "jumps" ie, call,jmp,ret,reti and so on
85          * allows dumping of a meaningful data even if the stack is
86          * munched and so on
87          */
88         #define OLD_PC_SIZE     32
89         struct {
90                 uint32_t pc;
91                 uint16_t sp;
92         } old[OLD_PC_SIZE]; // catches reset..
93         int                     old_pci;
94
95 #if AVR_STACK_WATCH
96         #define STACK_FRAME_SIZE        32
97         // this records the call/ret pairs, to try to catch
98         // code that munches the stack -under- their own frame
99         struct {
100                 uint32_t        pc;
101                 uint16_t        sp;             
102         } stack_frame[STACK_FRAME_SIZE];
103         int                     stack_frame_index;
104 #endif
105
106         // DEBUG ONLY
107         // keeps track of which registers gets touched by instructions
108         // reset before each new instructions. Allows meaningful traces
109         uint32_t        touched[256 / 32];      // debug
110 };
111
112 /*
113  * Main AVR instance. Some of these fields are set by the AVR "Core" definition files
114  * the rest is runtime data (as little as possible)
115  */
116 typedef struct avr_t {
117         const char * mmcu;      // name of the AVR
118         // these are filled by sim_core_declare from constants in /usr/lib/avr/include/avr/io*.h
119         uint16_t        ramend;         
120         uint32_t        flashend;
121         uint32_t        e2end;
122         uint8_t         vector_size;
123         uint8_t         signature[3];
124         uint8_t         fuse[4];
125         avr_io_addr_t   rampz;  // optional, only for ELPM/SPM on >64Kb cores
126         avr_io_addr_t   eind;   // optional, only for EIJMP/EICALL on >64Kb cores
127
128         // filled by the ELF data, this allow tracking of invalid jumps
129         uint32_t                        codeend;
130
131         int                                     state;          // stopped, running, sleeping
132         uint32_t                        frequency;      // frequency we are running at
133         // mostly used by the ADC for now
134         uint32_t                        vcc,avcc,aref; // (optional) voltages in millivolts
135
136         // cycles gets incremented when sleeping and when running; it corresponds
137         // not only to "cycles that runs" but also "cycles that might have run"
138         // like, sleeping.
139         avr_cycle_count_t       cycle;          // current cycle
140         
141         // called at init time
142         void (*init)(struct avr_t * avr);
143         // called at init time (for special purposes like using a memory mapped file as flash see: simduino)
144         void (*special_init)(struct avr_t * avr);
145         // called at termination time ( to clean special initalizations)
146         void (*special_deinit)(struct avr_t * avr);
147         // called at reset time
148         void (*reset)(struct avr_t * avr);
149
150         /*!
151          * Default AVR core run function.
152          * Two modes are available, a "raw" run that goes as fast as
153          * it can, and a "gdb" mode that also watchouts for gdb events
154          * and is a little bit slower.
155          */
156         void (*run)(struct avr_t * avr);
157
158         /*!
159          * Sleep default behaviour.
160          * In "raw" mode, it calls usleep, in gdb mode, it waits
161          * for howLong for gdb command on it's sockets.
162          */
163         void (*sleep)(struct avr_t * avr, avr_cycle_count_t howLong);
164
165         /*!
166          * Every IRQs will be stored in this pool. It is not
167          * mandatory (yet) but will allow listing IRQs and their connections
168          */
169         avr_irq_pool_t  irq_pool;
170
171         // Mirror of the SREG register, to facilitate the access to bits
172         // in the opcode decoder.
173         // This array is re-synthetized back/forth when SREG changes
174         uint8_t         sreg[8];
175         uint8_t         i_shadow;       // used to detect edges on I flag
176
177         /* 
178          * ** current PC **
179          * Note that the PC is representing /bytes/ while the AVR value is
180          * assumed to be "words". This is in line with what GDB does...
181          * this is why you will see >>1 and <<1 in the decoder to handle jumps.
182          * It CAN be a little confusing, so concentrate, young grasshopper.
183          */
184         uint32_t        pc;
185
186         /*
187          * callback when specific IO registers are read/written.
188          * There is one drawback here, there is in way of knowing what is the
189          * "beginning of useful sram" on a core, so there is no way to deduce
190          * what is the maximum IO register for a core, and thus, we can't
191          * allocate this table dynamically.
192          * If you wanted to emulate the BIG AVRs, and XMegas, this would need
193          * work.
194          */
195         struct {
196                 struct avr_irq_t * irq; // optional, used only if asked for with avr_iomem_getirq()
197                 struct {
198                         void * param;
199                         avr_io_read_t c;
200                 } r;
201                 struct {
202                         void * param;
203                         avr_io_write_t c;
204                 } w;
205         } io[MAX_IOs];
206
207         /*
208          * This block allows sharing of the IO write/read on addresses between
209          * multiple callbacks. In 99% of case it's not needed, however on the tiny*
210          * (tiny85 at last) some registers have bits that are used by different
211          * IO modules.
212          * If this case is detected, a special "dispatch" callback is installed that
213          * will handle this particular case, without impacting the performance of the
214          * other, normal cases...
215          */
216         int     io_shared_io_count;
217         struct {
218                 int used;
219                 struct {
220                         void * param;
221                         void * c;
222                 } io[4];
223         } io_shared_io[4];
224
225         // flash memory (initialized to 0xff, and code loaded into it)
226         uint8_t *       flash;
227         // this is the general purpose registers, IO registers, and SRAM
228         uint8_t *       data;
229
230         // queue of io modules
231         struct avr_io_t *io_port;
232
233         // cycle timers tracking & delivery
234         avr_cycle_timer_pool_t  cycle_timers;
235         // interrupt vectors and delivery fifo
236         avr_int_table_t interrupts;
237
238         // DEBUG ONLY -- value ignored if CONFIG_SIMAVR_TRACE = 0
239         int             trace : 1,
240                         log : 2; // log level, default to 1
241
242         // Only used if CONFIG_SIMAVR_TRACE is defined
243         struct avr_trace_data_t *trace_data;
244
245         // VALUE CHANGE DUMP file (waveforms)
246         // this is the VCD file that gets allocated if the 
247         // firmware that is loaded explicitly asks for a trace
248         // to be generated, and allocates it's own symbols
249         // using AVR_MMCU_TAG_VCD_TRACE (see avr_mcu_section.h)
250         struct avr_vcd_t * vcd;
251         
252         // gdb hooking structure. Only present when gdb server is active
253         struct avr_gdb_t * gdb;
254
255         // if non-zero, the gdb server will be started when the core
256         // crashed even if not activated at startup
257         // if zero, the simulator will just exit() in case of a crash
258         int             gdb_port;
259 } avr_t;
260
261
262 // this is a static constructor for each of the AVR devices
263 typedef struct avr_kind_t {
264         const char * names[4];  // name aliases
265         avr_t * (*make)();
266 } avr_kind_t;
267
268 // a symbol loaded from the .elf file
269 typedef struct avr_symbol_t {
270         const char * symbol;
271         uint32_t        addr;
272 } avr_symbol_t;
273
274 // locate the maker for mcu "name" and allocates a new avr instance
275 avr_t *
276 avr_make_mcu_by_name(
277                 const char *name);
278 // initializes a new AVR instance. Will call the IO registers init(), and then reset()
279 int
280 avr_init(
281                 avr_t * avr);
282 // resets the AVR, and the IO modules
283 void
284 avr_reset(
285                 avr_t * avr);
286 // run one cycle of the AVR, sleep if necessary
287 int
288 avr_run(
289                 avr_t * avr);
290 // finish any pending operations 
291 void
292 avr_terminate(
293                 avr_t * avr);
294
295 // set an IO register to receive commands from the AVR firmware
296 // it's optional, and uses the ELF tags
297 void
298 avr_set_command_register(
299                 avr_t * avr,
300                 avr_io_addr_t addr);
301
302 // specify the "console register" -- output sent to this register
303 // is printed on the simulator console, without using a UART
304 void
305 avr_set_console_register(
306                 avr_t * avr,
307                 avr_io_addr_t addr);
308
309 // load code in the "flash"
310 void
311 avr_loadcode(
312                 avr_t * avr,
313                 uint8_t * code,
314                 uint32_t size,
315                 uint32_t address);
316
317 /*
318  * these are accessors for avr->data but allows watchpoints to be set for gdb
319  * IO modules use that to set values to registers, and the AVR core decoder uses
320  * that to register "public" read by instructions.
321  */
322 void
323 avr_core_watch_write(
324                 avr_t *avr,
325                 uint16_t addr,
326                 uint8_t v);
327 uint8_t
328 avr_core_watch_read(
329                 avr_t *avr,
330                 uint16_t addr);
331
332 // called when the core has detected a crash somehow.
333 // this might activate gdb server
334 void
335 avr_sadly_crashed(
336                 avr_t *avr,
337                 uint8_t signal);
338
339
340 /*
341  * These are callbacks for the two 'main' bahaviour in simavr
342  */
343 void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong);
344 void avr_callback_run_gdb(avr_t * avr);
345 void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong);
346 void avr_callback_run_raw(avr_t * avr);
347
348 #ifdef __cplusplus
349 };
350 #endif
351
352 #include "sim_io.h"
353 #include "sim_regbit.h"
354
355 #endif /*__SIM_AVR_H__*/
356