debugCPU.c 6.8 KB

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