e5e2a96e4486812b50c274aa29ebe3fd76149f61
[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 /*
26  * Instruction decoder, run ONE instruction
27  */
28 uint16_t avr_run_one(avr_t * avr);
29
30 /*
31  * These are for internal access to the stack (for interrupts)
32  */
33 uint16_t _avr_sp_get(avr_t * avr);
34 void _avr_sp_set(avr_t * avr, uint16_t sp);
35 void _avr_push16(avr_t * avr, uint16_t v);
36
37 /*
38  * Get a "pretty" register name
39  */
40 const char * avr_regname(uint8_t reg);
41
42
43 /* 
44  * DEBUG bits follow 
45  * These will diseapear when gdb arrives
46  */
47 void avr_dump_state(avr_t * avr);
48
49 #define DUMP_REG() { \
50                                 for (int i = 0; i < 32; i++) printf("%s=%02x%c", avr_regname(i), avr->data[i],i==15?'\n':' ');\
51                                 printf("\n");\
52                                 uint16_t y = avr->data[R_YL] | (avr->data[R_YH]<<8);\
53                                 for (int i = 0; i < 20; i++) printf("Y+%02d=%02x ", i, avr->data[y+i]);\
54                                 printf("\n");\
55                 }
56
57
58 #if AVR_STACK_WATCH
59 #define DUMP_STACK() \
60                 for (int i = avr->stack_frame_index; i; i--) {\
61                         int pci = i-1;\
62                         printf("\e[31m*** %04x: %-25s sp %04x\e[0m\n",\
63                                         avr->stack_frame[pci].pc, avr->codeline[avr->stack_frame[pci].pc>>1]->symbol, avr->stack_frame[pci].sp);\
64                 }
65 #else
66 #define DUMP_STACK()
67 #endif
68
69 #define CRASH()  {\
70                 DUMP_REG();\
71                 printf("*** CYCLE %lld PC %04x\n", avr->cycle, avr->pc);\
72                 for (int i = OLD_PC_SIZE-1; i > 0; i--) {\
73                         int pci = (avr->old_pci + i) & 0xf;\
74                         printf("\e[31m*** %04x: %-25s RESET -%d; sp %04x\e[0m\n",\
75                                         avr->old[pci].pc, avr->codeline[avr->old[pci].pc>>1]->symbol, OLD_PC_SIZE-i, avr->old[pci].sp);\
76                 }\
77                 printf("Stack Ptr %04x/%04x = %d \n", _avr_sp_get(avr), avr->ramend, avr->ramend - _avr_sp_get(avr));\
78                 DUMP_STACK();\
79                 avr_sadly_crashed(avr, 0);\
80         }
81
82 #endif /* SIM_CORE_H_ */