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/>.
22 #ifndef __SIM_CORE_H__
23 #define __SIM_CORE_H__
34 #define FONT_GREEN "\e[32m"
35 #define FONT_RED "\e[31m"
36 #define FONT_DEFAULT "\e[0m"
40 * Instruction decoder, run ONE instruction
42 avr_flashaddr_t avr_run_one(avr_t * avr);
45 * These are for internal access to the stack (for interrupts)
47 uint16_t _avr_sp_get(avr_t * avr);
48 void _avr_sp_set(avr_t * avr, uint16_t sp);
49 void _avr_push16(avr_t * avr, uint16_t v);
51 #if CONFIG_SIMAVR_TRACE
54 * Get a "pretty" register name
56 const char * avr_regname(uint8_t reg);
60 * These will disappear when gdb arrives
62 void avr_dump_state(avr_t * avr);
64 #define DUMP_REG() { \
65 for (int i = 0; i < 32; i++) printf("%s=%02x%c", avr_regname(i), avr->data[i],i==15?'\n':' ');\
67 uint16_t y = avr->data[R_YL] | (avr->data[R_YH]<<8);\
68 for (int i = 0; i < 20; i++) printf("Y+%02d=%02x ", i, avr->data[y+i]);\
74 #define DUMP_STACK() \
75 for (int i = avr->trace_data->stack_frame_index; i; i--) {\
77 printf(FONT_RED "*** %04x: %-25s sp %04x\n" FONT_DEFAULT,\
78 avr->trace_data->stack_frame[pci].pc, \
79 avr->trace_data->codeline ? avr->trace_data->codeline[avr->trace_data->stack_frame[pci].pc>>1]->symbol : "unknown", \
80 avr->trace_data->stack_frame[pci].sp);\
88 printf("*** CYCLE %" PRI_avr_cycle_count "PC %04x\n", avr->cycle, avr->pc);\
89 for (int i = OLD_PC_SIZE-1; i > 0; i--) {\
90 int pci = (avr->trace_data->old_pci + i) & 0xf;\
91 printf(FONT_RED "*** %04x: %-25s RESET -%d; sp %04x\n" FONT_DEFAULT,\
92 avr->trace_data->old[pci].pc, avr->trace_data->codeline ? avr->trace_data->codeline[avr->trace_data->old[pci].pc>>1]->symbol : "unknown", OLD_PC_SIZE-i, avr->trace_data->old[pci].sp);\
94 printf("Stack Ptr %04x/%04x = %d \n", _avr_sp_get(avr), avr->ramend, avr->ramend - _avr_sp_get(avr));\
96 avr_sadly_crashed(avr, 0);\
98 #else /* CONFIG_SIMAVR_TRACE */
101 avr_sadly_crashed(avr, 0);\
109 * Reconstructs the SREG value from avr->sreg into dst.
111 #define READ_SREG_INTO(avr, dst) { \
113 for (int i = 0; i < 8; i++) \
114 if (avr->sreg[i] > 1) { \
115 printf("** Invalid SREG!!\n"); \
116 } else if (avr->sreg[i]) \
121 * Splits the SREG value from src into the avr->sreg array.
123 #define SET_SREG_FROM(avr, src) { \
124 for (int i = 0; i < 8; i++) \
125 avr->sreg[i] = (src & (1 << i)) != 0; \
132 #endif /*__SIM_CORE_H__*/