Pico.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // (c) Copyright 2007 notaz, All rights reserved.
  2. #include "../PicoInt.h"
  3. extern unsigned char formatted_bram[4*0x10];
  4. extern unsigned int s68k_poll_adclk;
  5. void (*PicoMCDopenTray)(void) = NULL;
  6. int (*PicoMCDcloseTray)(void) = NULL;
  7. #define dump_ram(ram,fname) \
  8. { \
  9. int i, d; \
  10. FILE *f; \
  11. \
  12. for (i = 0; i < sizeof(ram); i+=2) { \
  13. d = (ram[i]<<8) | ram[i+1]; \
  14. *(unsigned short *)(ram+i) = d; \
  15. } \
  16. f = fopen(fname, "wb"); \
  17. if (f) { \
  18. fwrite(ram, 1, sizeof(ram), f); \
  19. fclose(f); \
  20. } \
  21. for (i = 0; i < sizeof(ram); i+=2) { \
  22. d = (ram[i]<<8) | ram[i+1]; \
  23. *(unsigned short *)(ram+i) = d; \
  24. } \
  25. }
  26. PICO_INTERNAL int PicoInitMCD(void)
  27. {
  28. SekInitS68k();
  29. Init_CD_Driver();
  30. return 0;
  31. }
  32. PICO_INTERNAL void PicoExitMCD(void)
  33. {
  34. End_CD_Driver();
  35. //dump_ram(Pico_mcd->prg_ram, "prg.bin");
  36. //dump_ram(Pico.ram, "ram.bin");
  37. }
  38. PICO_INTERNAL int PicoResetMCD(int hard)
  39. {
  40. if (hard) {
  41. int fmt_size = sizeof(formatted_bram);
  42. memset(Pico_mcd->prg_ram, 0, sizeof(Pico_mcd->prg_ram));
  43. memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
  44. memset(Pico_mcd->pcm_ram, 0, sizeof(Pico_mcd->pcm_ram));
  45. memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
  46. memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
  47. }
  48. memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
  49. memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
  50. memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
  51. *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
  52. Pico_mcd->m.state_flags |= 1; // s68k reset pending
  53. Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
  54. Reset_CD();
  55. LC89510_Reset();
  56. gfx_cd_reset();
  57. #ifdef _ASM_CD_MEMORY_C
  58. PicoMemResetCD(1);
  59. //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
  60. #endif
  61. // use SRam.data for RAM cart
  62. if (SRam.data) free(SRam.data);
  63. SRam.data = NULL;
  64. if (PicoOpt&0x8000)
  65. SRam.data = calloc(1, 0x12000);
  66. return 0;
  67. }
  68. static __inline void SekRunM68k(int cyc)
  69. {
  70. int cyc_do;
  71. SekCycleAim+=cyc;
  72. if((cyc_do=SekCycleAim-SekCycleCnt) < 0) return;
  73. #if defined(EMU_C68K)
  74. PicoCpu.cycles=cyc_do;
  75. CycloneRun(&PicoCpu);
  76. SekCycleCnt+=cyc_do-PicoCpu.cycles;
  77. #elif defined(EMU_M68K)
  78. m68k_set_context(&PicoM68kCPU);
  79. SekCycleCnt+=m68k_execute(cyc_do);
  80. #endif
  81. }
  82. static __inline void SekRunS68k(int cyc)
  83. {
  84. int cyc_do;
  85. SekCycleAimS68k+=cyc;
  86. if((cyc_do=SekCycleAimS68k-SekCycleCntS68k) < 0) return;
  87. #if defined(EMU_C68K)
  88. PicoCpuS68k.cycles=cyc_do;
  89. CycloneRun(&PicoCpuS68k);
  90. SekCycleCntS68k+=cyc_do-PicoCpuS68k.cycles;
  91. #elif defined(EMU_M68K)
  92. m68k_set_context(&PicoS68kCPU);
  93. SekCycleCntS68k+=m68k_execute(cyc_do);
  94. #endif
  95. }
  96. #define PS_STEP_M68K ((488<<16)/20) // ~24
  97. //#define PS_STEP_S68K 13
  98. #ifdef _ASM_CD_PICO_C
  99. void SekRunPS(int cyc_m68k, int cyc_s68k);
  100. #else
  101. static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
  102. {
  103. int cycn, cycn_s68k, cyc_do;
  104. int d_cm = 0, d_cs = 0, ex;
  105. SekCycleAim+=cyc_m68k;
  106. SekCycleAimS68k+=cyc_s68k;
  107. // fprintf(stderr, "=== start %3i/%3i [%3i/%3i] {%05i.%i} ===\n", cyc_m68k, cyc_s68k,
  108. // SekCycleAim-SekCycleCnt, SekCycleAimS68k-SekCycleCntS68k, Pico.m.frame_count, Pico.m.scanline);
  109. /* loop 488 downto 0 in steps of PS_STEP */
  110. for (cycn = (488<<16)-PS_STEP_M68K; cycn >= 0; cycn -= PS_STEP_M68K)
  111. {
  112. ex = 0;
  113. cycn_s68k = (cycn + cycn/2 + cycn/8) >> 16;
  114. //fprintf(stderr, "%3i/%3i: ", cycn>>16, cycn_s68k);
  115. if ((cyc_do = SekCycleAim-SekCycleCnt-(cycn>>16)) > 0) {
  116. #if defined(EMU_C68K)
  117. PicoCpu.cycles = cyc_do;
  118. CycloneRun(&PicoCpu);
  119. SekCycleCnt += cyc_do - PicoCpu.cycles;
  120. #elif defined(EMU_M68K)
  121. m68k_set_context(&PicoM68kCPU);
  122. SekCycleCnt += (ex = m68k_execute(cyc_do));
  123. #endif
  124. }
  125. //fprintf(stderr, "%3i ", ex); d_cm += ex; ex = 0;
  126. if ((cyc_do = SekCycleAimS68k-SekCycleCntS68k-cycn_s68k) > 0) {
  127. #if defined(EMU_C68K)
  128. PicoCpuS68k.cycles = cyc_do;
  129. CycloneRun(&PicoCpuS68k);
  130. SekCycleCntS68k += cyc_do - PicoCpuS68k.cycles;
  131. #elif defined(EMU_M68K)
  132. m68k_set_context(&PicoS68kCPU);
  133. SekCycleCntS68k += (ex = m68k_execute(cyc_do));
  134. #endif
  135. }
  136. //fprintf(stderr, "%3i\n", ex); d_cs += ex;
  137. }
  138. //fprintf(stderr, "== end %3i/%3i ==\n", d_cm, d_cs);
  139. }
  140. #endif
  141. static __inline void check_cd_dma(void)
  142. {
  143. int ddx;
  144. if (!(Pico_mcd->scd.Status_CDC & 0x08)) return;
  145. ddx = Pico_mcd->s68k_regs[4] & 7;
  146. if (ddx < 2) return; // invalid
  147. if (ddx < 4) {
  148. Pico_mcd->s68k_regs[4] |= 0x40; // Data set ready in host port
  149. return;
  150. }
  151. if (ddx == 6) return; // invalid
  152. Update_CDC_TRansfer(ddx); // now go and do the actual transfer
  153. }
  154. static __inline void update_chips(void)
  155. {
  156. int counter_timer, int3_set;
  157. int counter75hz_lim = Pico.m.pal ? 2080 : 2096;
  158. // 75Hz CDC update
  159. if ((Pico_mcd->m.counter75hz+=10) >= counter75hz_lim) {
  160. Pico_mcd->m.counter75hz -= counter75hz_lim;
  161. Check_CD_Command();
  162. }
  163. // update timers
  164. counter_timer = Pico.m.pal ? 0x21630 : 0x2121c; // 136752 : 135708;
  165. Pico_mcd->m.timer_stopwatch += counter_timer;
  166. if ((int3_set = Pico_mcd->s68k_regs[0x31])) {
  167. Pico_mcd->m.timer_int3 -= counter_timer;
  168. if (Pico_mcd->m.timer_int3 < 0) {
  169. if (Pico_mcd->s68k_regs[0x33] & (1<<3)) {
  170. dprintf("s68k: timer irq 3");
  171. SekInterruptS68k(3);
  172. Pico_mcd->m.timer_int3 += int3_set << 16;
  173. }
  174. // is this really what happens if irq3 is masked out?
  175. Pico_mcd->m.timer_int3 &= 0xffffff;
  176. }
  177. }
  178. // update gfx chip
  179. if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
  180. gfx_cd_update();
  181. // delayed setting of DMNA bit (needed for Silpheed)
  182. if (Pico_mcd->m.state_flags & 2) {
  183. Pico_mcd->m.state_flags &= ~2;
  184. if (!(Pico_mcd->s68k_regs[3] & 4)) {
  185. Pico_mcd->s68k_regs[3] |= 2;
  186. Pico_mcd->s68k_regs[3] &= ~1;
  187. #ifdef USE_POLL_DETECT
  188. if ((s68k_poll_adclk&0xfe) == 2) {
  189. SekSetStopS68k(0); s68k_poll_adclk = 0;
  190. }
  191. #endif
  192. }
  193. }
  194. }
  195. static int PicoFrameHintsMCD(void)
  196. {
  197. struct PicoVideo *pv=&Pico.video;
  198. int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
  199. const int cycles_68k=488,cycles_z80=228,cycles_s68k=795; // both PAL and NTSC compile to same values
  200. int skip=PicoSkipFrame || (PicoOpt&0x10);
  201. int hint; // Hint counter
  202. if(Pico.m.pal) { //
  203. //cycles_68k = (int) ((double) OSC_PAL / 7 / 50 / 312 + 0.4); // should compile to a constant (488)
  204. //cycles_z80 = (int) ((double) OSC_PAL / 15 / 50 / 312 + 0.4); // 228
  205. lines = 312; // Steve Snake says there are 313 lines, but this seems to also work well
  206. line_sample = 68;
  207. if(pv->reg[1]&8) lines_vis = 240;
  208. } else {
  209. //cycles_68k = (int) ((double) OSC_NTSC / 7 / 60 / 262 + 0.4); // 488
  210. //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
  211. lines = 262;
  212. line_sample = 93;
  213. }
  214. SekCyclesReset();
  215. SekCyclesResetS68k();
  216. //z80ExtraCycles = 0;
  217. if(PicoOpt&4)
  218. z80CycleAim = 0;
  219. // z80_resetCycles();
  220. pv->status&=~0x88; // clear V-Int, come out of vblank
  221. hint=pv->reg[10]; // Load H-Int counter
  222. //dprintf("-hint: %i", hint);
  223. for (y=0;y<lines;y++)
  224. {
  225. Pico.m.scanline=(short)y;
  226. // pad delay (for 6 button pads)
  227. if(PicoOpt&0x20) {
  228. if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;
  229. if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;
  230. }
  231. check_cd_dma();
  232. // H-Interrupts:
  233. if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
  234. {
  235. //dprintf("rhint:old @ %06x", SekPc);
  236. hint=pv->reg[10]; // Reload H-Int counter
  237. pv->pending_ints|=0x10;
  238. if (pv->reg[0]&0x10) SekInterrupt(4);
  239. //dprintf("rhint: %i @ %06x [%i|%i]", hint, SekPc, y, SekCycleCnt);
  240. //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));
  241. }
  242. // V-Interrupt:
  243. if (y == lines_vis)
  244. {
  245. //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
  246. pv->status|=0x88; // V-Int happened, go into vblank
  247. SekRunM68k(128); SekCycleAim-=128; // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula)
  248. /*if(Pico.m.z80Run && (PicoOpt&4)) {
  249. z80CycleAim+=cycles_z80/2;
  250. total_z80+=z80_run(z80CycleAim-total_z80);
  251. z80CycleAim-=cycles_z80/2;
  252. }*/
  253. pv->pending_ints|=0x20;
  254. if(pv->reg[1]&0x20) SekInterrupt(6);
  255. if(Pico.m.z80Run && (PicoOpt&4)) // ?
  256. z80_int();
  257. //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));
  258. }
  259. // decide if we draw this line
  260. #if CAN_HANDLE_240_LINES
  261. if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )
  262. #else
  263. if(!skip && y<224)
  264. #endif
  265. PicoLine(y);
  266. if(PicoOpt&1)
  267. sound_timers_and_dac(y);
  268. // get samples from sound chips
  269. if (y == 224 && PsndOut) {
  270. int len = sound_render(0, PsndLen);
  271. if (PicoWriteSound) PicoWriteSound(len);
  272. // clear sound buffer
  273. sound_clear();
  274. }
  275. // Run scanline:
  276. //dprintf("m68k starting exec @ %06x", SekPc);
  277. if (Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
  278. if ((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
  279. SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync"
  280. } else {
  281. SekRunM68k(cycles_68k);
  282. if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset
  283. SekRunS68k(cycles_s68k);
  284. }
  285. if ((PicoOpt&4) && Pico.m.z80Run) {
  286. if (Pico.m.z80Run & 2) z80CycleAim+=cycles_z80;
  287. else {
  288. int cnt = SekCyclesDone() - z80startCycle;
  289. cnt = (cnt>>1)-(cnt>>5);
  290. //if (cnt > cycles_z80) printf("FIXME: z80 cycles: %i\n", cnt);
  291. if (cnt > cycles_z80) cnt = cycles_z80;
  292. Pico.m.z80Run |= 2;
  293. z80CycleAim+=cnt;
  294. }
  295. total_z80+=z80_run(z80CycleAim-total_z80);
  296. }
  297. update_chips();
  298. }
  299. // draw a frame just after vblank in alternative render mode
  300. if (!PicoSkipFrame && (PicoOpt&0x10))
  301. PicoFrameFull();
  302. return 0;
  303. }
  304. PICO_INTERNAL int PicoFrameMCD(void)
  305. {
  306. if(!(PicoOpt&0x10))
  307. PicoFrameStart();
  308. PicoFrameHintsMCD();
  309. return 0;
  310. }