Sek.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // (c) Copyright 2006 notaz, All rights reserved.
  2. #include "../PicoInt.h"
  3. int SekCycleCntS68k=0; // cycles done in this frame
  4. int SekCycleAimS68k=0; // cycle aim
  5. #ifdef EMU_C68K
  6. // ---------------------- Cyclone 68000 ----------------------
  7. struct Cyclone PicoCpuS68k;
  8. #endif
  9. #ifdef EMU_M68K
  10. // ---------------------- MUSASHI 68000 ----------------------
  11. m68ki_cpu_core PicoS68kCPU; // Mega CD's CPU
  12. #endif
  13. static int new_irq_level(int level)
  14. {
  15. int level_new = 0, irqs;
  16. Pico_mcd->m.s68k_pend_ints &= ~(1 << level);
  17. irqs = Pico_mcd->m.s68k_pend_ints;
  18. irqs &= Pico_mcd->s68k_regs[0x33];
  19. while ((irqs >>= 1)) level_new++;
  20. return level_new;
  21. }
  22. #ifdef EMU_M68K
  23. static int SekIntAckS68kM(int level)
  24. {
  25. int level_new = new_irq_level(level);
  26. dprintf("s68kACK %i -> %i", level, level_new);
  27. CPU_INT_LEVEL = level_new << 8;
  28. return M68K_INT_ACK_AUTOVECTOR;
  29. }
  30. #endif
  31. #ifdef EMU_C68K
  32. // interrupt acknowledgement
  33. static int SekIntAckS68k(int level)
  34. {
  35. int level_new = new_irq_level(level);
  36. dprintf("s68kACK %i -> %i", level, level_new);
  37. PicoCpuS68k.irq = level_new;
  38. return CYCLONE_INT_ACK_AUTOVECTOR;
  39. }
  40. static void SekResetAckS68k(void)
  41. {
  42. dprintf("s68k: Reset encountered @ %06x", SekPcS68k);
  43. }
  44. static int SekUnrecognizedOpcodeS68k(void)
  45. {
  46. unsigned int pc, op;
  47. pc = SekPcS68k;
  48. op = PicoCpuS68k.read16(pc);
  49. dprintf("Unrecognized Opcode %04x @ %06x", op, pc);
  50. //exit(1);
  51. return 0;
  52. }
  53. #endif
  54. PICO_INTERNAL int SekInitS68k()
  55. {
  56. #ifdef EMU_C68K
  57. // CycloneInit();
  58. memset(&PicoCpuS68k,0,sizeof(PicoCpuS68k));
  59. PicoCpuS68k.IrqCallback=SekIntAckS68k;
  60. PicoCpuS68k.ResetCallback=SekResetAckS68k;
  61. PicoCpuS68k.UnrecognizedCallback=SekUnrecognizedOpcodeS68k;
  62. #endif
  63. #ifdef EMU_M68K
  64. {
  65. // Musashi is not very context friendly..
  66. void *oldcontext = m68ki_cpu_p;
  67. m68k_set_context(&PicoS68kCPU);
  68. m68k_set_cpu_type(M68K_CPU_TYPE_68000);
  69. m68k_init();
  70. m68k_set_int_ack_callback(SekIntAckS68kM);
  71. // m68k_pulse_reset(); // not yet, memmap is not set up
  72. m68k_set_context(oldcontext);
  73. }
  74. #endif
  75. return 0;
  76. }
  77. // Reset the 68000:
  78. PICO_INTERNAL int SekResetS68k()
  79. {
  80. if (Pico.rom==NULL) return 1;
  81. #ifdef EMU_C68K
  82. PicoCpuS68k.state_flags=0;
  83. PicoCpuS68k.osp=0;
  84. PicoCpuS68k.srh =0x27; // Supervisor mode
  85. PicoCpuS68k.flags=4; // Z set
  86. PicoCpuS68k.irq=0;
  87. PicoCpuS68k.a[7]=PicoCpuS68k.read32(0); // Stack Pointer
  88. PicoCpuS68k.membase=0;
  89. PicoCpuS68k.pc=PicoCpuS68k.checkpc(PicoCpuS68k.read32(4)); // Program Counter
  90. #endif
  91. #ifdef EMU_M68K
  92. {
  93. void *oldcontext = m68ki_cpu_p;
  94. m68k_set_context(&PicoS68kCPU);
  95. m68ki_cpu.sp[0]=0;
  96. m68k_set_irq(0);
  97. m68k_pulse_reset();
  98. m68k_set_context(oldcontext);
  99. }
  100. #endif
  101. return 0;
  102. }
  103. PICO_INTERNAL int SekInterruptS68k(int irq)
  104. {
  105. int irqs, real_irq = 1;
  106. Pico_mcd->m.s68k_pend_ints |= 1 << irq;
  107. irqs = Pico_mcd->m.s68k_pend_ints >> 1;
  108. while ((irqs >>= 1)) real_irq++; // this is probably only needed for Cyclone
  109. #ifdef EMU_C68K
  110. PicoCpuS68k.irq=real_irq;
  111. #endif
  112. #ifdef EMU_M68K
  113. void *oldcontext = m68ki_cpu_p;
  114. m68k_set_context(&PicoS68kCPU);
  115. m68k_set_irq(real_irq); // raise irq (gets lowered after taken or must be done in ack)
  116. m68k_set_context(oldcontext);
  117. #endif
  118. return 0;
  119. }