misc: Update NO_COLOR define switch
[simavr] / simavr / sim / sim_core.h
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
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_CORE_H__
23 #define __SIM_CORE_H__
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #ifdef NO_COLOR
30         #define FONT_GREEN
31         #define FONT_RED                
32         #define FONT_DEFAULT    
33 #else
34         #define FONT_GREEN              "\e[32m"
35         #define FONT_RED                "\e[31m"
36         #define FONT_DEFAULT    "\e[0m"
37 #endif
38
39 /*
40  * Instruction decoder, run ONE instruction
41  */
42 avr_flashaddr_t avr_run_one(avr_t * avr);
43
44 /*
45  * These are for internal access to the stack (for interrupts)
46  */
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);
50
51 #if CONFIG_SIMAVR_TRACE
52
53 /*
54  * Get a "pretty" register name
55  */
56 const char * avr_regname(uint8_t reg);
57
58 /* 
59  * DEBUG bits follow 
60  * These will disappear when gdb arrives
61  */
62 void avr_dump_state(avr_t * avr);
63
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':' ');\
66                                 printf("\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]);\
69                                 printf("\n");\
70                 }
71
72
73 #if AVR_STACK_WATCH
74 #define DUMP_STACK() \
75                 for (int i = avr->trace_data->stack_frame_index; i; i--) {\
76                         int pci = i-1;\
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);\
81                 }
82 #else
83 #define DUMP_STACK()
84 #endif
85
86 #define CRASH()  {\
87                 DUMP_REG();\
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);\
93                 }\
94                 printf("Stack Ptr %04x/%04x = %d \n", _avr_sp_get(avr), avr->ramend, avr->ramend - _avr_sp_get(avr));\
95                 DUMP_STACK();\
96                 avr_sadly_crashed(avr, 0);\
97         }
98 #else /* CONFIG_SIMAVR_TRACE */
99
100 #define CRASH() { \
101                 avr_sadly_crashed(avr, 0);\
102         }
103 #define DUMP_STACK()
104 #define DUMP_REG();
105
106 #endif 
107
108 /**
109  * Reconstructs the SREG value from avr->sreg into dst.
110  */
111 #define READ_SREG_INTO(avr, dst) { \
112                         dst = 0; \
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]) \
117                                         dst |= (1 << i); \
118                 }
119
120 /**
121  * Splits the SREG value from src into the avr->sreg array.
122  */
123 #define SET_SREG_FROM(avr, src) { \
124                         for (int i = 0; i < 8; i++) \
125                                 avr->sreg[i] = (src & (1 << i)) != 0; \
126                 }
127
128 #ifdef __cplusplus
129 };
130 #endif
131
132 #endif /*__SIM_CORE_H__*/