drc: lots of new debug code
[picodrive.git] / cpu / sh2 / mame / sh2pico.c
1 #include "../sh2.h"
2 #ifdef DRC_CMP
3 #include "../compiler.c"
4 #endif
5
6 // MAME types
7 #ifndef INT8
8 typedef signed char  INT8;
9 typedef signed short INT16;
10 typedef signed int   INT32;
11 typedef unsigned int   UINT32;
12 typedef unsigned short UINT16;
13 typedef unsigned char  UINT8;
14 #endif
15
16 #define RB(sh2, a) p32x_sh2_read8(a,sh2)
17 #define RW(sh2, a) p32x_sh2_read16(a,sh2)
18 #define RL(sh2, a) p32x_sh2_read32(a,sh2)
19 #define WB(sh2, a, d) p32x_sh2_write8(a,d,sh2)
20 #define WW(sh2, a, d) p32x_sh2_write16(a,d,sh2)
21 #define WL(sh2, a, d) p32x_sh2_write32(a,d,sh2)
22
23 // some stuff from sh2comn.h
24 #define T       0x00000001
25 #define S       0x00000002
26 #define I       0x000000f0
27 #define Q       0x00000100
28 #define M       0x00000200
29
30 #define AM      0xc7ffffff
31
32 #define FLAGS   (M|Q|I|S|T)
33
34 #define Rn      ((opcode>>8)&15)
35 #define Rm      ((opcode>>4)&15)
36
37 #define sh2_state SH2
38
39 extern void lprintf(const char *fmt, ...);
40 #define logerror lprintf
41
42 #ifdef SH2_STATS
43 static SH2 sh2_stats;
44 static unsigned int op_refs[0x10000];
45 # define LRN  1
46 # define LRM  2
47 # define LRNM (LRN|LRM)
48 # define rlog(rnm) {   \
49   int op = opcode;     \
50   if ((rnm) & LRN) {   \
51     op &= ~0x0f00;     \
52     sh2_stats.r[Rn]++; \
53   }                    \
54   if ((rnm) & LRM) {   \
55     op &= ~0x00f0;     \
56     sh2_stats.r[Rm]++; \
57   }                    \
58   op_refs[op]++;       \
59 }
60 # define rlog1(x) sh2_stats.r[x]++
61 # define rlog2(x1,x2) sh2_stats.r[x1]++; sh2_stats.r[x2]++
62 #else
63 # define rlog(x)
64 # define rlog1(...)
65 # define rlog2(...)
66 #endif
67
68 #include "sh2.c"
69
70 #ifndef DRC_SH2
71
72 int sh2_execute(SH2 *sh2, int cycles)
73 {
74 #ifdef DRC_CMP
75         unsigned int base_pc = 0, end_pc = 0;
76         unsigned char op_flags[BLOCK_CYCLE_LIMIT];
77 #endif
78         UINT32 opcode;
79
80         sh2->icount = cycles;
81
82         if (sh2->icount <= 0)
83                 return cycles;
84
85         sh2->cycles_timeslice = cycles;
86
87         do
88         {
89 #ifdef DRC_CMP
90                 if (!sh2->delay) {
91                         if (sh2->pc < base_pc || sh2->pc > end_pc) {
92                                 base_pc = sh2->pc;
93                                 scan_block(base_pc, sh2->is_slave,
94                                         op_flags, &end_pc);
95                         }
96                         if ((OP_FLAGS(sh2->pc) & OF_TARGET) || sh2->pc == base_pc) {
97                                 if (sh2->icount < 0)
98                                         break;
99                         }
100
101                         do_sh2_trace(sh2, sh2->icount);
102                 }
103 #endif
104
105                 if (sh2->delay)
106                 {
107                         sh2->ppc = sh2->delay;
108                         opcode = RW(sh2, sh2->delay);
109                         sh2->pc -= 2;
110                 }
111                 else
112                 {
113                         sh2->ppc = sh2->pc;
114                         opcode = RW(sh2, sh2->pc);
115                 }
116
117                 sh2->delay = 0;
118                 sh2->pc += 2;
119
120                 switch (opcode & ( 15 << 12))
121                 {
122                 case  0<<12: op0000(sh2, opcode); break;
123                 case  1<<12: op0001(sh2, opcode); break;
124                 case  2<<12: op0010(sh2, opcode); break;
125                 case  3<<12: op0011(sh2, opcode); break;
126                 case  4<<12: op0100(sh2, opcode); break;
127                 case  5<<12: op0101(sh2, opcode); break;
128                 case  6<<12: op0110(sh2, opcode); break;
129                 case  7<<12: op0111(sh2, opcode); break;
130                 case  8<<12: op1000(sh2, opcode); break;
131                 case  9<<12: op1001(sh2, opcode); break;
132                 case 10<<12: op1010(sh2, opcode); break;
133                 case 11<<12: op1011(sh2, opcode); break;
134                 case 12<<12: op1100(sh2, opcode); break;
135                 case 13<<12: op1101(sh2, opcode); break;
136                 case 14<<12: op1110(sh2, opcode); break;
137                 default: op1111(sh2, opcode); break;
138                 }
139
140                 sh2->icount--;
141
142                 if (sh2->test_irq && !sh2->delay && sh2->pending_level > ((sh2->sr >> 4) & 0x0f))
143                 {
144                         int level = sh2->pending_level;
145                         int vector = sh2->irq_callback(sh2, level);
146                         sh2_do_irq(sh2, level, vector);
147                         sh2->test_irq = 0;
148                 }
149
150         }
151 #ifndef DRC_CMP
152         while (sh2->icount > 0 || sh2->delay);  /* can't interrupt before delay */
153 #else
154         while (1);
155 #endif
156
157         return sh2->cycles_timeslice - sh2->icount;
158 }
159
160 #endif // DRC_SH2
161
162 #ifdef SH2_STATS
163 #include <stdio.h>
164 #include <string.h>
165 #include "sh2dasm.h"
166
167 void sh2_dump_stats(void)
168 {
169         static const char *rnames[] = {
170                 "R0", "R1", "R2",  "R3",  "R4",  "R5",  "R6",  "R7",
171                 "R8", "R9", "R10", "R11", "R12", "R13", "R14", "SP",
172                 "PC", "", "PR", "SR", "GBR", "VBR", "MACH", "MACL"
173         };
174         long long total;
175         char buff[64];
176         int u, i;
177
178         // dump reg usage
179         total = 0;
180         for (i = 0; i < 24; i++)
181                 total += sh2_stats.r[i];
182
183         for (i = 0; i < 24; i++) {
184                 if (i == 16 || i == 17 || i == 19)
185                         continue;
186                 printf("r %6.3f%% %-4s %9d\n", (double)sh2_stats.r[i] * 100.0 / total,
187                         rnames[i], sh2_stats.r[i]);
188         }
189
190         memset(&sh2_stats, 0, sizeof(sh2_stats));
191
192         // dump ops
193         printf("\n");
194         total = 0;
195         for (i = 0; i < 0x10000; i++)
196                 total += op_refs[i];
197
198         for (u = 0; u < 16; u++) {
199                 int max = 0, op = 0;
200                 for (i = 0; i < 0x10000; i++) {
201                         if (op_refs[i] > max) {
202                                 max = op_refs[i];
203                                 op = i;
204                         }
205                 }
206                 DasmSH2(buff, 0, op);
207                 printf("i %6.3f%% %9d %s\n", (double)op_refs[op] * 100.0 / total,
208                         op_refs[op], buff);
209                 op_refs[op] = 0;
210         }
211         memset(op_refs, 0, sizeof(op_refs));
212 }
213 #endif
214