DebugCPU.c 6.6 KB

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