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