sek.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * PicoDrive
  3. * (c) Copyright Dave, 2004
  4. * (C) notaz, 2006-2009
  5. *
  6. * This work is licensed under the terms of MAME license.
  7. * See COPYING file in the top-level directory.
  8. */
  9. #include "pico_int.h"
  10. #include "memory.h"
  11. int SekCycleCnt=0; // cycles done in this frame
  12. int SekCycleAim=0; // cycle aim
  13. unsigned int SekCycleCntT=0;
  14. /* context */
  15. // Cyclone 68000
  16. #ifdef EMU_C68K
  17. struct Cyclone PicoCpuCM68k;
  18. #endif
  19. // MUSASHI 68000
  20. #ifdef EMU_M68K
  21. m68ki_cpu_core PicoCpuMM68k;
  22. #endif
  23. // FAME 68000
  24. #ifdef EMU_F68K
  25. M68K_CONTEXT PicoCpuFM68k;
  26. #endif
  27. /* callbacks */
  28. #ifdef EMU_C68K
  29. // interrupt acknowledgment
  30. static int SekIntAck(int level)
  31. {
  32. // try to emulate VDP's reaction to 68000 int ack
  33. if (level == 4) { Pico.video.pending_ints = 0; elprintf(EL_INTS, "hack: @ %06x [%i]", SekPc, SekCycleCnt); }
  34. else if(level == 6) { Pico.video.pending_ints &= ~0x20; elprintf(EL_INTS, "vack: @ %06x [%i]", SekPc, SekCycleCnt); }
  35. PicoCpuCM68k.irq = 0;
  36. return CYCLONE_INT_ACK_AUTOVECTOR;
  37. }
  38. static void SekResetAck(void)
  39. {
  40. elprintf(EL_ANOMALY, "Reset encountered @ %06x", SekPc);
  41. }
  42. static int SekUnrecognizedOpcode()
  43. {
  44. unsigned int pc;
  45. pc = SekPc;
  46. elprintf(EL_ANOMALY, "Unrecognized Opcode @ %06x", pc);
  47. // see if we are still in a mapped region
  48. pc &= 0x00ffffff;
  49. if (map_flag_set(m68k_read16_map[pc >> M68K_MEM_SHIFT])) {
  50. elprintf(EL_STATUS|EL_ANOMALY, "m68k crash @%06x", pc);
  51. PicoCpuCM68k.cycles = 0;
  52. PicoCpuCM68k.state_flags |= 1;
  53. return 1;
  54. }
  55. #ifdef EMU_M68K // debugging cyclone
  56. {
  57. extern int have_illegal;
  58. have_illegal = 1;
  59. }
  60. #endif
  61. return 0;
  62. }
  63. #endif
  64. #ifdef EMU_M68K
  65. static int SekIntAckM68K(int level)
  66. {
  67. if (level == 4) { Pico.video.pending_ints = 0; elprintf(EL_INTS, "hack: @ %06x [%i]", SekPc, SekCycleCnt); }
  68. else if(level == 6) { Pico.video.pending_ints &= ~0x20; elprintf(EL_INTS, "vack: @ %06x [%i]", SekPc, SekCycleCnt); }
  69. CPU_INT_LEVEL = 0;
  70. return M68K_INT_ACK_AUTOVECTOR;
  71. }
  72. static int SekTasCallback(void)
  73. {
  74. return 0; // no writeback
  75. }
  76. #endif
  77. #ifdef EMU_F68K
  78. static void SekIntAckF68K(unsigned level)
  79. {
  80. if (level == 4) { Pico.video.pending_ints = 0; elprintf(EL_INTS, "hack: @ %06x [%i]", SekPc, SekCycleCnt); }
  81. else if(level == 6) { Pico.video.pending_ints &= ~0x20; elprintf(EL_INTS, "vack: @ %06x [%i]", SekPc, SekCycleCnt); }
  82. PicoCpuFM68k.interrupts[0] = 0;
  83. }
  84. #endif
  85. PICO_INTERNAL void SekInit(void)
  86. {
  87. #ifdef EMU_C68K
  88. CycloneInit();
  89. memset(&PicoCpuCM68k,0,sizeof(PicoCpuCM68k));
  90. PicoCpuCM68k.IrqCallback=SekIntAck;
  91. PicoCpuCM68k.ResetCallback=SekResetAck;
  92. PicoCpuCM68k.UnrecognizedCallback=SekUnrecognizedOpcode;
  93. PicoCpuCM68k.flags=4; // Z set
  94. #endif
  95. #ifdef EMU_M68K
  96. {
  97. void *oldcontext = m68ki_cpu_p;
  98. m68k_set_context(&PicoCpuMM68k);
  99. m68k_set_cpu_type(M68K_CPU_TYPE_68000);
  100. m68k_init();
  101. m68k_set_int_ack_callback(SekIntAckM68K);
  102. m68k_set_tas_instr_callback(SekTasCallback);
  103. //m68k_pulse_reset();
  104. m68k_set_context(oldcontext);
  105. }
  106. #endif
  107. #ifdef EMU_F68K
  108. {
  109. void *oldcontext = g_m68kcontext;
  110. g_m68kcontext = &PicoCpuFM68k;
  111. memset(&PicoCpuFM68k, 0, sizeof(PicoCpuFM68k));
  112. fm68k_init();
  113. PicoCpuFM68k.iack_handler = SekIntAckF68K;
  114. PicoCpuFM68k.sr = 0x2704; // Z flag
  115. g_m68kcontext = oldcontext;
  116. }
  117. #endif
  118. }
  119. // Reset the 68000:
  120. PICO_INTERNAL int SekReset(void)
  121. {
  122. if (Pico.rom==NULL) return 1;
  123. #ifdef EMU_C68K
  124. CycloneReset(&PicoCpuCM68k);
  125. #endif
  126. #ifdef EMU_M68K
  127. m68k_set_context(&PicoCpuMM68k); // if we ever reset m68k, we always need it's context to be set
  128. m68ki_cpu.sp[0]=0;
  129. m68k_set_irq(0);
  130. m68k_pulse_reset();
  131. REG_USP = 0; // ?
  132. #endif
  133. #ifdef EMU_F68K
  134. {
  135. g_m68kcontext = &PicoCpuFM68k;
  136. fm68k_reset();
  137. }
  138. #endif
  139. return 0;
  140. }
  141. void SekStepM68k(void)
  142. {
  143. SekCycleAim=SekCycleCnt+1;
  144. #if defined(EMU_CORE_DEBUG)
  145. SekCycleCnt+=CM_compareRun(1, 0);
  146. #elif defined(EMU_C68K)
  147. PicoCpuCM68k.cycles=1;
  148. CycloneRun(&PicoCpuCM68k);
  149. SekCycleCnt+=1-PicoCpuCM68k.cycles;
  150. #elif defined(EMU_M68K)
  151. SekCycleCnt+=m68k_execute(1);
  152. #elif defined(EMU_F68K)
  153. SekCycleCnt+=fm68k_emulate(1, 0, 0);
  154. #endif
  155. }
  156. PICO_INTERNAL void SekSetRealTAS(int use_real)
  157. {
  158. #ifdef EMU_C68K
  159. CycloneSetRealTAS(use_real);
  160. #endif
  161. #ifdef EMU_F68K
  162. // TODO
  163. #endif
  164. }
  165. // Pack the cpu into a common format:
  166. // XXX: rename
  167. PICO_INTERNAL void SekPackCpu(unsigned char *cpu, int is_sub)
  168. {
  169. unsigned int pc=0;
  170. #if defined(EMU_C68K)
  171. struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
  172. memcpy(cpu,context->d,0x40);
  173. pc=context->pc-context->membase;
  174. *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);
  175. *(unsigned int *)(cpu+0x48)=context->osp;
  176. cpu[0x4c] = context->irq;
  177. cpu[0x4d] = context->state_flags & 1;
  178. #elif defined(EMU_M68K)
  179. void *oldcontext = m68ki_cpu_p;
  180. m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
  181. memcpy(cpu,m68ki_cpu_p->dar,0x40);
  182. pc=m68ki_cpu_p->pc;
  183. *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
  184. *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET];
  185. cpu[0x4c] = CPU_INT_LEVEL>>8;
  186. cpu[0x4d] = CPU_STOPPED;
  187. m68k_set_context(oldcontext);
  188. #elif defined(EMU_F68K)
  189. M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
  190. memcpy(cpu,context->dreg,0x40);
  191. pc=context->pc;
  192. *(unsigned int *)(cpu+0x44)=context->sr;
  193. *(unsigned int *)(cpu+0x48)=context->asp;
  194. cpu[0x4c] = context->interrupts[0];
  195. cpu[0x4d] = (context->execinfo & FM68K_HALTED) ? 1 : 0;
  196. #endif
  197. *(unsigned int *)(cpu+0x40) = pc;
  198. *(unsigned int *)(cpu+0x50) = SekCycleCntT;
  199. }
  200. PICO_INTERNAL void SekUnpackCpu(const unsigned char *cpu, int is_sub)
  201. {
  202. #if defined(EMU_C68K)
  203. struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
  204. CycloneSetSr(context, *(unsigned int *)(cpu+0x44));
  205. context->osp=*(unsigned int *)(cpu+0x48);
  206. memcpy(context->d,cpu,0x40);
  207. context->membase = 0;
  208. context->pc = *(unsigned int *)(cpu+0x40);
  209. CycloneUnpack(context, NULL); // rebase PC
  210. context->irq = cpu[0x4c];
  211. context->state_flags = 0;
  212. if (cpu[0x4d])
  213. context->state_flags |= 1;
  214. #elif defined(EMU_M68K)
  215. void *oldcontext = m68ki_cpu_p;
  216. m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
  217. m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
  218. memcpy(m68ki_cpu_p->dar,cpu,0x40);
  219. m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);
  220. m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET]=*(unsigned int *)(cpu+0x48);
  221. CPU_INT_LEVEL = cpu[0x4c] << 8;
  222. CPU_STOPPED = cpu[0x4d];
  223. m68k_set_context(oldcontext);
  224. #elif defined(EMU_F68K)
  225. M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
  226. memcpy(context->dreg,cpu,0x40);
  227. context->pc =*(unsigned int *)(cpu+0x40);
  228. context->sr =*(unsigned int *)(cpu+0x44);
  229. context->asp=*(unsigned int *)(cpu+0x48);
  230. context->interrupts[0] = cpu[0x4c];
  231. context->execinfo &= ~FM68K_HALTED;
  232. if (cpu[0x4d]&1) context->execinfo |= FM68K_HALTED;
  233. #endif
  234. SekCycleCntT = *(unsigned int *)(cpu+0x50);
  235. }
  236. /* idle loop detection, not to be used in CD mode */
  237. #ifdef EMU_C68K
  238. #include "cpu/cyclone/tools/idle.h"
  239. #endif
  240. static unsigned short **idledet_ptrs = NULL;
  241. static int idledet_count = 0, idledet_bads = 0;
  242. int idledet_start_frame = 0;
  243. #if 0
  244. #define IDLE_STATS 1
  245. unsigned int idlehit_addrs[128], idlehit_counts[128];
  246. void SekRegisterIdleHit(unsigned int pc)
  247. {
  248. int i;
  249. for (i = 0; i < 127 && idlehit_addrs[i]; i++) {
  250. if (idlehit_addrs[i] == pc) {
  251. idlehit_counts[i]++;
  252. return;
  253. }
  254. }
  255. idlehit_addrs[i] = pc;
  256. idlehit_counts[i] = 1;
  257. idlehit_addrs[i+1] = 0;
  258. }
  259. #endif
  260. void SekInitIdleDet(void)
  261. {
  262. unsigned short **tmp = realloc(idledet_ptrs, 0x200*4);
  263. if (tmp == NULL) {
  264. free(idledet_ptrs);
  265. idledet_ptrs = NULL;
  266. }
  267. else
  268. idledet_ptrs = tmp;
  269. idledet_count = idledet_bads = 0;
  270. idledet_start_frame = Pico.m.frame_count + 360;
  271. #ifdef IDLE_STATS
  272. idlehit_addrs[0] = 0;
  273. #endif
  274. #ifdef EMU_C68K
  275. CycloneInitIdle();
  276. #endif
  277. #ifdef EMU_F68K
  278. fm68k_emulate(0, 0, 1);
  279. #endif
  280. }
  281. int SekIsIdleCode(unsigned short *dst, int bytes)
  282. {
  283. // printf("SekIsIdleCode %04x %i\n", *dst, bytes);
  284. switch (bytes)
  285. {
  286. case 2:
  287. if ((*dst & 0xf000) != 0x6000) // not another branch
  288. return 1;
  289. break;
  290. case 4:
  291. if ( (*dst & 0xfff8) == 0x4a10 || // tst.b ($aX) // there should be no need to wait
  292. (*dst & 0xfff8) == 0x4a28 || // tst.b ($xxxx,a0) // for byte change anywhere
  293. (*dst & 0xff3f) == 0x4a38 || // tst.x ($xxxx.w); tas ($xxxx.w)
  294. (*dst & 0xc1ff) == 0x0038 || // move.x ($xxxx.w), dX
  295. (*dst & 0xf13f) == 0xb038) // cmp.x ($xxxx.w), dX
  296. return 1;
  297. break;
  298. case 6:
  299. if ( ((dst[1] & 0xe0) == 0xe0 && ( // RAM and
  300. *dst == 0x4a39 || // tst.b ($xxxxxxxx)
  301. *dst == 0x4a79 || // tst.w ($xxxxxxxx)
  302. *dst == 0x4ab9 || // tst.l ($xxxxxxxx)
  303. (*dst & 0xc1ff) == 0x0039 || // move.x ($xxxxxxxx), dX
  304. (*dst & 0xf13f) == 0xb039))||// cmp.x ($xxxxxxxx), dX
  305. *dst == 0x0838 || // btst $X, ($xxxx.w) [6 byte op]
  306. (*dst & 0xffbf) == 0x0c38) // cmpi.{b,w} $X, ($xxxx.w)
  307. return 1;
  308. break;
  309. case 8:
  310. if ( ((dst[2] & 0xe0) == 0xe0 && ( // RAM and
  311. *dst == 0x0839 || // btst $X, ($xxxxxxxx.w) [8 byte op]
  312. (*dst & 0xffbf) == 0x0c39))||// cmpi.{b,w} $X, ($xxxxxxxx)
  313. *dst == 0x0cb8) // cmpi.l $X, ($xxxx.w)
  314. return 1;
  315. break;
  316. case 12:
  317. if ((*dst & 0xf1f8) == 0x3010 && // move.w (aX), dX
  318. (dst[1]&0xf100) == 0x0000 && // arithmetic
  319. (dst[3]&0xf100) == 0x0000) // arithmetic
  320. return 1;
  321. break;
  322. }
  323. return 0;
  324. }
  325. int SekRegisterIdlePatch(unsigned int pc, int oldop, int newop, void *ctx)
  326. {
  327. int is_main68k = 1;
  328. u16 *target;
  329. uptr v;
  330. #if defined(EMU_C68K)
  331. struct Cyclone *cyc = ctx;
  332. is_main68k = cyc == &PicoCpuCM68k;
  333. pc -= cyc->membase;
  334. #elif defined(EMU_F68K)
  335. is_main68k = ctx == &PicoCpuFM68k;
  336. #endif
  337. pc &= ~0xff000000;
  338. elprintf(EL_IDLE, "idle: patch %06x %04x %04x %c %c #%i", pc, oldop, newop,
  339. (newop&0x200)?'n':'y', is_main68k?'m':'s', idledet_count);
  340. // XXX: probably shouldn't patch RAM too
  341. v = m68k_read16_map[pc >> M68K_MEM_SHIFT];
  342. if (!(v & 0x80000000))
  343. target = (u16 *)((v << 1) + pc);
  344. else {
  345. if (++idledet_bads > 128)
  346. return 2; // remove detector
  347. return 1; // don't patch
  348. }
  349. if (idledet_count >= 0x200 && (idledet_count & 0x1ff) == 0) {
  350. unsigned short **tmp = realloc(idledet_ptrs, (idledet_count+0x200)*4);
  351. if (tmp == NULL)
  352. return 1;
  353. idledet_ptrs = tmp;
  354. }
  355. idledet_ptrs[idledet_count++] = target;
  356. return 0;
  357. }
  358. void SekFinishIdleDet(void)
  359. {
  360. #ifdef EMU_C68K
  361. CycloneFinishIdle();
  362. #endif
  363. #ifdef EMU_F68K
  364. fm68k_emulate(0, 0, 2);
  365. #endif
  366. while (idledet_count > 0)
  367. {
  368. unsigned short *op = idledet_ptrs[--idledet_count];
  369. if ((*op & 0xfd00) == 0x7100)
  370. *op &= 0xff, *op |= 0x6600;
  371. else if ((*op & 0xfd00) == 0x7500)
  372. *op &= 0xff, *op |= 0x6700;
  373. else if ((*op & 0xfd00) == 0x7d00)
  374. *op &= 0xff, *op |= 0x6000;
  375. else
  376. elprintf(EL_STATUS|EL_IDLE, "idle: don't know how to restore %04x", *op);
  377. }
  378. }
  379. #if defined(CPU_CMP_R) || defined(CPU_CMP_W)
  380. #include "debug.h"
  381. struct ref_68k {
  382. u32 dar[16];
  383. u32 pc;
  384. u32 sr;
  385. u32 cycles;
  386. u32 pc_prev;
  387. };
  388. struct ref_68k ref_68ks[2];
  389. static int current_68k;
  390. void SekTrace(int is_s68k)
  391. {
  392. struct ref_68k *x68k = &ref_68ks[is_s68k];
  393. u32 pc = is_s68k ? SekPcS68k : SekPc;
  394. u32 sr = is_s68k ? SekSrS68k : SekSr;
  395. u32 cycles = is_s68k ? SekCycleCntS68k : SekCycleCnt;
  396. u32 r;
  397. u8 cmd;
  398. #ifdef CPU_CMP_W
  399. int i;
  400. if (is_s68k != current_68k) {
  401. current_68k = is_s68k;
  402. cmd = CTL_68K_SLAVE | current_68k;
  403. tl_write(&cmd, sizeof(cmd));
  404. }
  405. if (pc != x68k->pc) {
  406. x68k->pc = pc;
  407. tl_write_uint(CTL_68K_PC, x68k->pc);
  408. }
  409. if (sr != x68k->sr) {
  410. x68k->sr = sr;
  411. tl_write_uint(CTL_68K_SR, x68k->sr);
  412. }
  413. for (i = 0; i < 16; i++) {
  414. r = is_s68k ? SekDarS68k(i) : SekDar(i);
  415. if (r != x68k->dar[i]) {
  416. x68k->dar[i] = r;
  417. tl_write_uint(CTL_68K_R + i, r);
  418. }
  419. }
  420. tl_write_uint(CTL_68K_CYCLES, cycles);
  421. #else
  422. int i, bad = 0;
  423. while (1)
  424. {
  425. int ret = tl_read(&cmd, sizeof(cmd));
  426. if (ret == 0) {
  427. elprintf(EL_STATUS, "EOF");
  428. exit(1);
  429. }
  430. switch (cmd) {
  431. case CTL_68K_SLAVE:
  432. case CTL_68K_SLAVE + 1:
  433. current_68k = cmd & 1;
  434. break;
  435. case CTL_68K_PC:
  436. tl_read_uint(&x68k->pc);
  437. break;
  438. case CTL_68K_SR:
  439. tl_read_uint(&x68k->sr);
  440. break;
  441. case CTL_68K_CYCLES:
  442. tl_read_uint(&x68k->cycles);
  443. goto breakloop;
  444. default:
  445. if (CTL_68K_R <= cmd && cmd < CTL_68K_R + 0x10)
  446. tl_read_uint(&x68k->dar[cmd - CTL_68K_R]);
  447. else
  448. elprintf(EL_STATUS, "invalid cmd: %02x", cmd);
  449. }
  450. }
  451. breakloop:
  452. if (is_s68k != current_68k) {
  453. printf("bad 68k: %d %d\n", is_s68k, current_68k);
  454. bad = 1;
  455. }
  456. if (cycles != x68k->cycles) {
  457. printf("bad cycles: %u %u\n", cycles, x68k->cycles);
  458. bad = 1;
  459. }
  460. if ((pc ^ x68k->pc) & 0xffffff) {
  461. printf("bad PC: %08x %08x\n", pc, x68k->pc);
  462. bad = 1;
  463. }
  464. if (sr != x68k->sr) {
  465. printf("bad SR: %03x %03x\n", sr, x68k->sr);
  466. bad = 1;
  467. }
  468. for (i = 0; i < 16; i++) {
  469. r = is_s68k ? SekDarS68k(i) : SekDar(i);
  470. if (r != x68k->dar[i]) {
  471. printf("bad %c%d: %08x %08x\n", i < 8 ? 'D' : 'A', i & 7,
  472. r, x68k->dar[i]);
  473. bad = 1;
  474. }
  475. }
  476. if (bad) {
  477. for (i = 0; i < 8; i++)
  478. printf("D%d: %08x A%d: %08x\n", i, x68k->dar[i],
  479. i, x68k->dar[i + 8]);
  480. printf("PC: %08x, %08x\n", x68k->pc, x68k->pc_prev);
  481. PDebugDumpMem();
  482. exit(1);
  483. }
  484. x68k->pc_prev = x68k->pc;
  485. #endif
  486. }
  487. #endif // CPU_CMP_*
  488. #if defined(EMU_M68K) && M68K_INSTRUCTION_HOOK == OPT_SPECIFY_HANDLER
  489. static unsigned char op_flags[0x400000/2] = { 0, };
  490. static int atexit_set = 0;
  491. static void make_idc(void)
  492. {
  493. FILE *f = fopen("idc.idc", "w");
  494. int i;
  495. if (!f) return;
  496. fprintf(f, "#include <idc.idc>\nstatic main() {\n");
  497. for (i = 0; i < 0x400000/2; i++)
  498. if (op_flags[i] != 0)
  499. fprintf(f, " MakeCode(0x%06x);\n", i*2);
  500. fprintf(f, "}\n");
  501. fclose(f);
  502. }
  503. void instruction_hook(void)
  504. {
  505. if (!atexit_set) {
  506. atexit(make_idc);
  507. atexit_set = 1;
  508. }
  509. if (REG_PC < 0x400000)
  510. op_flags[REG_PC/2] = 1;
  511. }
  512. #endif
  513. // vim:shiftwidth=2:ts=2:expandtab