SimAVR
AVR Simulator
sim_core.h
Go to the documentation of this file.
1 /*
2  * sim_core.h
3  *
4  * Copyright 2008, 2009 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 it under the terms of the GNU
9  * General Public License as published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * simavr is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
13  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with simavr. If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
25 #ifndef __SIM_CORE_H__
26 #define __SIM_CORE_H__
27 
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #endif
32 
35 
36  /*
37  * These are for internal access to the stack (for interrupts)
38  */
39  uint16_t _avr_sp_get (avr_t * avr);
40  void _avr_sp_set (avr_t * avr, uint16_t sp);
42 
43 #if CONFIG_SIMAVR_TRACE
44 
46  const char *avr_regname (uint8_t reg);
47 
52  void avr_dump_state (avr_t * avr);
53 
54 #define DUMP_REG() { \
55  for (int i = 0; i < 32; i++) \
56  printf("%s=%02x%c", avr_regname(i), avr->data[i],i==15?'\n':' '); \
57  printf("\n"); \
58  uint16_t y = avr->data[R_YL] | (avr->data[R_YH] << 8); \
59  for (int i = 0; i < 20; i++) \
60  printf("Y+%02d=%02x ", i, avr->data[y+i]); \
61  printf("\n"); \
62  }
63 
64 #if AVR_STACK_WATCH
65 #define DUMP_STACK() \
66  for (int i = avr->trace_data->stack_frame_index; i; i--) { \
67  int pci = i-1; \
68  printf(FONT_RED "*** %04x: %-25s sp %04x\n" FONT_DEFAULT, \
69  avr->trace_data->stack_frame[pci].pc, \
70  avr->trace_data->codeline ? \
71  avr->trace_data->codeline[avr->trace_data->stack_frame[pci].pc>>1]->symbol : "unknown", \
72  avr->trace_data->stack_frame[pci].sp); \
73  }
74 #else
75 #define DUMP_STACK()
76 #endif
77 
78 #else /* CONFIG_SIMAVR_TRACE */
79 
80 #define DUMP_STACK()
81 #define DUMP_REG();
82 
83 #endif
84 
88 #define READ_SREG_INTO(avr, dst) { \
89  dst = 0; \
90  for (int i = 0; i < 8; i++) \
91  if (avr->sreg[i] > 1) \
92  printf("** Invalid SREG!!\n"); \
93  else if (avr->sreg[i]) \
94  dst |= (1 << i); \
95  }
96 
102  static inline void avr_sreg_set (avr_t * avr, uint8_t flag, uint8_t ival)
103  {
104  if (flag == S_I)
105  {
106  if (ival)
107  {
108  if (!avr->sreg[S_I])
109  avr->interrupt_state = -2;
110  }
111  else
112  avr->interrupt_state = 0;
113  }
114 
115  avr->sreg[flag] = ival;
116  }
117 
121 #define SET_SREG_FROM(avr, src) { \
122  for (int i = 0; i < 8; i++) \
123  avr_sreg_set(avr, i, (src & (1 << i)) != 0); \
124  }
125 
126 #ifdef __cplusplus
127 };
128 #endif
129 
130 #endif /*__SIM_CORE_H__*/
131 
avr_flashaddr_t avr_run_one(avr_t *avr)
Instruction decoder, run ONE instruction.
Definition: sim_core.c:705
avr_t * avr
Definition: run_avr.c:54
const char * avr_regname(uint8_t reg)
Definition: sim_core.c:356
uint32_t avr_flashaddr_t
Definition: sim_avr.h:58
uint8_t sreg[8]
Mirror of the SREG register, to facilitate the access to bits in the opcode decoder.
Definition: sim_avr.h:207
int _avr_push_addr(avr_t *avr, avr_flashaddr_t addr)
Definition: sim_core.c:324
uint16_t _avr_sp_get(avr_t *avr)
Stack pointer access.
Definition: sim_core.c:256
Main AVR instance.
Definition: sim_avr.h:142
Definition: sim_avr.h:75
static void avr_sreg_set(avr_t *avr, uint8_t flag, uint8_t ival)
clear interrupt_state if disabling interrupts.
Definition: sim_core.h:102
void _avr_sp_set(avr_t *avr, uint16_t sp)
Definition: sim_core.c:262
int8_t interrupt_state
Interrupt state: 00: idle (no wait, no pending interrupts) or disabled <0: wait till zero >0: interru...
Definition: sim_avr.h:214