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