debugCPU.c 6.8 KB

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