fd312142ed3b6f1186b0867a0b01ea5ce4a4c9d0
[picodrive.git] / pico / debugCPU.c
1 /*\r
2  * debug stuff\r
3  * (C) notaz, 2006-2008\r
4  *\r
5  * This work is licensed under the terms of MAME license.\r
6  * See COPYING file in the top-level directory.\r
7  */\r
8 \r
9 #include "pico_int.h"\r
10 \r
11 typedef unsigned char  u8;\r
12 \r
13 static unsigned int pppc, ops=0;\r
14 extern unsigned int lastread_a, lastread_d[16], lastwrite_cyc_d[16], lastwrite_mus_d[16];\r
15 extern int lrp_cyc, lrp_mus, lwp_cyc, lwp_mus;\r
16 unsigned int old_regs[16], old_sr, ppop, have_illegal = 0;\r
17 int dbg_irq_level = 0, dbg_irq_level_sub = 0;\r
18 \r
19 #undef dprintf\r
20 #define dprintf(f,...) printf("%05i:%03i: " f "\n",Pico.m.frame_count,Pico.m.scanline,##__VA_ARGS__)\r
21 \r
22 #if defined(EMU_C68K)\r
23 static struct Cyclone *currentC68k = NULL;\r
24 #define other_set_sub(s)   currentC68k=(s)?&PicoCpuCS68k:&PicoCpuCM68k;\r
25 #define other_get_sr()     CycloneGetSr(currentC68k)\r
26 #define other_dar(i)       currentC68k->d[i]\r
27 #define other_osp          currentC68k->osp\r
28 #define other_get_irq()    currentC68k->irq\r
29 #define other_set_irq(i)   currentC68k->irq=i\r
30 #define other_is_stopped()  (currentC68k->state_flags&1)\r
31 #define other_is_tracing() ((currentC68k->state_flags&2)?1:0)\r
32 #elif defined(EMU_F68K)\r
33 #define other_set_sub(s)   g_m68kcontext=(s)?&PicoCpuFS68k:&PicoCpuFM68k;\r
34 #define other_get_sr()     g_m68kcontext->sr\r
35 #define other_dar(i)       ((unsigned int*)g_m68kcontext->dreg)[i]\r
36 #define other_osp          g_m68kcontext->asp\r
37 #define other_get_irq()    g_m68kcontext->interrupts[0]\r
38 #define other_set_irq(irq) g_m68kcontext->interrupts[0]=irq\r
39 #define other_is_stopped() ((g_m68kcontext->execinfo&FM68K_HALTED)?1:0)\r
40 #define other_is_tracing() ((g_m68kcontext->execinfo&FM68K_EMULATE_TRACE)?1:0)\r
41 #else\r
42 #error other core missing, don't compile this file\r
43 #endif\r
44 \r
45 static int otherRun(void)\r
46 {\r
47 #if defined(EMU_C68K)\r
48   currentC68k->cycles=1;\r
49   CycloneRun(currentC68k);\r
50   return 1-currentC68k->cycles;\r
51 #elif defined(EMU_F68K)\r
52   return fm68k_emulate(1, 0);\r
53 #endif\r
54 }\r
55 \r
56 //static\r
57 void dumpPCandExit(int is_sub)\r
58 {\r
59   char buff[128];\r
60   int i;\r
61 \r
62   m68k_disassemble(buff, pppc, M68K_CPU_TYPE_68000);\r
63   dprintf("PC: %06x: %04x: %s", pppc, ppop, buff);\r
64   dprintf("                    this | prev");\r
65   for(i=0; i < 8; i++)\r
66     dprintf("d%i=%08x, a%i=%08x | d%i=%08x, a%i=%08x", i, other_dar(i), i, other_dar(i+8), i, old_regs[i], i, old_regs[i+8]);\r
67   dprintf("SR:                 %04x | %04x (??s? 0iii 000x nzvc)", other_get_sr(), old_sr);\r
68   dprintf("last_read: %08x @ %06x", lastread_d[--lrp_cyc&15], lastread_a);\r
69   dprintf("ops done: %i, is_sub: %i", ops, is_sub);\r
70   exit(1);\r
71 }\r
72 \r
73 int CM_compareRun(int cyc, int is_sub)\r
74 {\r
75   char *str;\r
76   int cyc_done=0, cyc_other, cyc_musashi, *irq_level, err=0;\r
77   unsigned int i, pc, mu_sr;\r
78 \r
79   m68ki_cpu_p=is_sub?&PicoCpuMS68k:&PicoCpuMM68k;\r
80   other_set_sub(is_sub);\r
81   lrp_cyc = lrp_mus = 0;\r
82 \r
83   while (cyc_done < cyc)\r
84   {\r
85     if (have_illegal && m68k_read_disassembler_16(m68ki_cpu.pc) != 0x4e73) // not rte\r
86     {\r
87       have_illegal = 0;\r
88       m68ki_cpu.pc += 2;\r
89 #ifdef EMU_C68K\r
90       currentC68k->pc=currentC68k->checkpc(currentC68k->pc + 2);\r
91 #endif\r
92     }\r
93     // hacks for test_misc2\r
94     if (m68ki_cpu.pc == 0x0002e0 && m68k_read_disassembler_16(m68ki_cpu.pc) == 0x4e73)\r
95     {\r
96       // get out of "priviledge violation" loop\r
97       have_illegal = 1;\r
98       //m68ki_cpu.s_flag = SFLAG_SET;\r
99       //currentC68k->srh|=0x20;\r
100     }\r
101 \r
102     pppc = is_sub ? SekPcS68k : SekPc;\r
103     ppop = m68k_read_disassembler_16(pppc);\r
104     memcpy(old_regs, &other_dar(0), 4*16);\r
105     old_sr = other_get_sr();\r
106 \r
107 #if 0\r
108     {\r
109       char buff[128];\r
110       dprintf("---");\r
111       m68k_disassemble(buff, pppc, M68K_CPU_TYPE_68000);\r
112       dprintf("PC: %06x: %04x: %s", pppc, ppop, buff);\r
113       //dprintf("A7: %08x", currentC68k->a[7]);\r
114     }\r
115 #endif\r
116 \r
117     irq_level = is_sub ? &dbg_irq_level_sub : &dbg_irq_level;\r
118     if (*irq_level)\r
119     {\r
120       other_set_irq(*irq_level);\r
121       m68k_set_irq(*irq_level);\r
122       *irq_level=0;\r
123     }\r
124 \r
125     cyc_other=otherRun();\r
126     // Musashi takes irq even if it hasn't got cycles left, let othercpu do it too\r
127     if (other_get_irq() && other_get_irq() > ((other_get_sr()>>8)&7))\r
128       cyc_other+=otherRun();\r
129     cyc_musashi=m68k_execute(1);\r
130 \r
131     if (cyc_other != cyc_musashi) {\r
132       dprintf("cycles: %i vs %i", cyc_other, cyc_musashi);\r
133       err=1;\r
134     }\r
135 \r
136     if (lrp_cyc != lrp_mus) {\r
137       dprintf("lrp: %i vs %i", lrp_cyc&15, lrp_mus&15);\r
138       err=1;\r
139     }\r
140 \r
141     if (lwp_cyc != lwp_mus) {\r
142       dprintf("lwp: %i vs %i", lwp_cyc&15, lwp_mus&15);\r
143       err=1;\r
144     }\r
145 \r
146     for (i=0; i < 16; i++) {\r
147       if (lastwrite_cyc_d[i] != lastwrite_mus_d[i]) {\r
148         dprintf("lastwrite: [%i]= %08x vs %08x", i, lastwrite_cyc_d[i], lastwrite_mus_d[i]);\r
149         err=1;\r
150         break;\r
151       }\r
152     }\r
153 \r
154     // compare PC\r
155     pc = is_sub ? SekPcS68k : SekPc;\r
156     m68ki_cpu.pc&=~1;\r
157     if (pc != m68ki_cpu.pc) {\r
158       dprintf("PC: %06x vs %06x", pc, m68ki_cpu.pc);\r
159       err=1;\r
160     }\r
161 \r
162 #if 0\r
163     if( SekPc > Pico.romsize || SekPc < 0x200 ) {\r
164       dprintf("PC out of bounds: %06x", SekPc);\r
165       err=1;\r
166     }\r
167 #endif\r
168 \r
169     // compare regs\r
170     for (i=0; i < 16; i++) {\r
171       if (other_dar(i) != m68ki_cpu.dar[i]) {\r
172         str = (i < 8) ? "d" : "a";\r
173         dprintf("reg: %s%i: %08x vs %08x", str, i&7, other_dar(i), m68ki_cpu.dar[i]);\r
174         err=1;\r
175       }\r
176     }\r
177 \r
178     // SR\r
179     if (other_get_sr() != (mu_sr = m68k_get_reg(NULL, M68K_REG_SR))) {\r
180       dprintf("SR: %04x vs %04x (??s? 0iii 000x nzvc)", other_get_sr(), mu_sr);\r
181       err=1;\r
182     }\r
183 \r
184     // IRQl\r
185     if (other_get_irq() != (m68ki_cpu.int_level>>8)) {\r
186       dprintf("IRQ: %i vs %i", other_get_irq(), (m68ki_cpu.int_level>>8));\r
187       err=1;\r
188     }\r
189 \r
190     // OSP/USP\r
191     if (other_osp != m68ki_cpu.sp[((mu_sr>>11)&4)^4]) {\r
192       dprintf("OSP: %06x vs %06x", other_osp, m68ki_cpu.sp[((mu_sr>>11)&4)^4]);\r
193       err=1;\r
194     }\r
195 \r
196     // stopped\r
197     if ((other_is_stopped() && !m68ki_cpu.stopped) || (!other_is_stopped() && m68ki_cpu.stopped)) {\r
198       dprintf("stopped: %i vs %i", other_is_stopped(), m68ki_cpu.stopped);\r
199       err=1;\r
200     }\r
201 \r
202     // tracing\r
203     if((other_is_tracing() && !m68ki_tracing) || (!other_is_tracing() && m68ki_tracing)) {\r
204       dprintf("tracing: %i vs %i", other_is_tracing(), m68ki_tracing);\r
205       err=1;\r
206     }\r
207 \r
208     if(err) dumpPCandExit(is_sub);\r
209 \r
210 #if 0\r
211     if (m68ki_cpu.dar[15] < 0x00ff0000 || m68ki_cpu.dar[15] >= 0x01000000)\r
212     {\r
213       other_dar(15) = m68ki_cpu.dar[15] = 0xff8000;\r
214     }\r
215 #endif\r
216 #if 0\r
217     m68k_set_reg(M68K_REG_SR, ((mu_sr-1)&~0x2000)|(mu_sr&0x2000)); // broken\r
218     CycloneSetSr(currentC68k, ((mu_sr-1)&~0x2000)|(mu_sr&0x2000));\r
219     currentC68k->stopped = m68ki_cpu.stopped = 0;\r
220     if(SekPc > 0x400 && (currentC68k->a[7] < 0xff0000 || currentC68k->a[7] > 0xffffff))\r
221     currentC68k->a[7] = m68ki_cpu.dar[15] = 0xff8000;\r
222 #endif\r
223 \r
224     cyc_done += cyc_other;\r
225     ops++;\r
226   }\r
227 \r
228   return cyc_done;\r
229 }\r