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