Pico.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // PicoDrive
  2. // (c) Copyright 2004 Dave, All rights reserved.
  3. // (c) Copyright 2006-2008 notaz, All rights reserved.
  4. // Free for non-commercial use.
  5. // For commercial use, separate licencing terms must be obtained.
  6. #include "PicoInt.h"
  7. #include "sound/ym2612.h"
  8. int PicoVer=0x0133;
  9. struct Pico Pico;
  10. int PicoOpt = 0;
  11. int PicoSkipFrame = 0; // skip rendering frame?
  12. int emustatus = 0; // rapid_ym2612, multi_ym_updates
  13. int PicoPad[2]; // Joypads, format is MXYZ SACB RLDU
  14. int PicoPadInt[2]; // internal copy
  15. int PicoAHW = 0; // active addon hardware: scd_active, 32x_active, svp_active, pico_active
  16. int PicoRegionOverride = 0; // override the region detection 0: Auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe
  17. int PicoAutoRgnOrder = 0;
  18. struct PicoSRAM SRam = {0,};
  19. void (*PicoWriteSound)(int len) = NULL; // called at the best time to send sound buffer (PsndOut) to hardware
  20. void (*PicoResetHook)(void) = NULL;
  21. void (*PicoLineHook)(void) = NULL;
  22. // to be called once on emu init
  23. void PicoInit(void)
  24. {
  25. // Blank space for state:
  26. memset(&Pico,0,sizeof(Pico));
  27. memset(&PicoPad,0,sizeof(PicoPad));
  28. memset(&PicoPadInt,0,sizeof(PicoPadInt));
  29. // Init CPUs:
  30. SekInit();
  31. z80_init(); // init even if we aren't going to use it
  32. PicoInitMCD();
  33. PicoSVPInit();
  34. SRam.data=0;
  35. }
  36. // to be called once on emu exit
  37. void PicoExit(void)
  38. {
  39. if (PicoAHW & PAHW_MCD)
  40. PicoExitMCD();
  41. PicoCartUnload();
  42. z80_exit();
  43. if (SRam.data) free(SRam.data); SRam.data=0;
  44. }
  45. void PicoPower(void)
  46. {
  47. unsigned char sram_reg=Pico.m.sram_reg; // must be preserved
  48. Pico.m.frame_count = 0;
  49. // clear all memory of the emulated machine
  50. memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);
  51. memset(&Pico.video,0,sizeof(Pico.video));
  52. memset(&Pico.m,0,sizeof(Pico.m));
  53. Pico.video.pending_ints=0;
  54. z80_reset();
  55. // default VDP register values (based on Fusion)
  56. Pico.video.reg[0] = Pico.video.reg[1] = 0x04;
  57. Pico.video.reg[0xc] = 0x81;
  58. Pico.video.reg[0xf] = 0x02;
  59. if (PicoAHW & PAHW_MCD)
  60. PicoPowerMCD();
  61. Pico.m.sram_reg=sram_reg;
  62. PicoReset();
  63. }
  64. PICO_INTERNAL void PicoDetectRegion(void)
  65. {
  66. int support=0, hw=0, i;
  67. unsigned char pal=0;
  68. if (PicoRegionOverride)
  69. {
  70. support = PicoRegionOverride;
  71. }
  72. else
  73. {
  74. // Read cartridge region data:
  75. int region=PicoRead32(0x1f0);
  76. for (i=0;i<4;i++)
  77. {
  78. int c=0;
  79. c=region>>(i<<3); c&=0xff;
  80. if (c<=' ') continue;
  81. if (c=='J') support|=1;
  82. else if (c=='U') support|=4;
  83. else if (c=='E') support|=8;
  84. else if (c=='j') {support|=1; break; }
  85. else if (c=='u') {support|=4; break; }
  86. else if (c=='e') {support|=8; break; }
  87. else
  88. {
  89. // New style code:
  90. char s[2]={0,0};
  91. s[0]=(char)c;
  92. support|=strtol(s,NULL,16);
  93. }
  94. }
  95. }
  96. // auto detection order override
  97. if (PicoAutoRgnOrder) {
  98. if (((PicoAutoRgnOrder>>0)&0xf) & support) support = (PicoAutoRgnOrder>>0)&0xf;
  99. else if (((PicoAutoRgnOrder>>4)&0xf) & support) support = (PicoAutoRgnOrder>>4)&0xf;
  100. else if (((PicoAutoRgnOrder>>8)&0xf) & support) support = (PicoAutoRgnOrder>>8)&0xf;
  101. }
  102. // Try to pick the best hardware value for English/50hz:
  103. if (support&8) { hw=0xc0; pal=1; } // Europe
  104. else if (support&4) hw=0x80; // USA
  105. else if (support&2) { hw=0x40; pal=1; } // Japan PAL
  106. else if (support&1) hw=0x00; // Japan NTSC
  107. else hw=0x80; // USA
  108. Pico.m.hardware=(unsigned char)(hw|0x20); // No disk attached
  109. Pico.m.pal=pal;
  110. }
  111. int PicoReset(void)
  112. {
  113. unsigned char sram_reg=Pico.m.sram_reg; // must be preserved
  114. if (Pico.romsize<=0) return 1;
  115. /* must call now, so that banking is reset, and correct vectors get fetched */
  116. if (PicoResetHook) PicoResetHook();
  117. PicoMemReset();
  118. SekReset();
  119. memset(&PicoPadInt,0,sizeof(PicoPadInt));
  120. // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games).
  121. SekSetRealTAS(PicoAHW & PAHW_MCD);
  122. SekCycleCntT=0;
  123. if (PicoAHW & PAHW_MCD)
  124. // needed for MCD to reset properly, probably some bug hides behind this..
  125. memset(Pico.ioports,0,sizeof(Pico.ioports));
  126. emustatus = 0;
  127. Pico.m.dirtyPal = 1;
  128. Pico.m.z80_bank68k = 0;
  129. memset(Pico.zram, 0, sizeof(Pico.zram)); // ??
  130. PicoDetectRegion();
  131. Pico.video.status = 0x3428 | Pico.m.pal; // 'always set' bits | vblank | collision | pal
  132. PsndReset(); // pal must be known here
  133. // create an empty "dma" to cause 68k exec start at random frame location
  134. if (Pico.m.dma_xfers == 0 && !(PicoOpt&POPT_DIS_VDP_FIFO))
  135. Pico.m.dma_xfers = rand() & 0x1fff;
  136. SekFinishIdleDet();
  137. if (PicoAHW & PAHW_MCD) {
  138. PicoResetMCD();
  139. return 0;
  140. }
  141. // reinit, so that checksum checks pass
  142. if (!(PicoOpt & POPT_DIS_IDLE_DET))
  143. SekInitIdleDet();
  144. // reset sram state; enable sram access by default if it doesn't overlap with ROM
  145. Pico.m.sram_reg=sram_reg&0x14;
  146. if (!(Pico.m.sram_reg&4) && Pico.romsize <= SRam.start) Pico.m.sram_reg |= 1;
  147. elprintf(EL_STATUS, "sram: det: %i; eeprom: %i; start: %06x; end: %06x",
  148. (Pico.m.sram_reg>>4)&1, (Pico.m.sram_reg>>2)&1, SRam.start, SRam.end);
  149. return 0;
  150. }
  151. // dma2vram settings are just hacks to unglitch Legend of Galahad (needs <= 104 to work)
  152. // same for Outrunners (92-121, when active is set to 24)
  153. // 96 is VR hack
  154. static const int dma_timings[] = {
  155. 96, 167, 166, 83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy
  156. 102, 205, 204, 102, // vblank: 40cell:
  157. 16, 16, 15, 8, // active: 32cell:
  158. 24, 18, 17, 9 // ...
  159. };
  160. static const int dma_bsycles[] = {
  161. (488<<8)/96, (488<<8)/167, (488<<8)/166, (488<<8)/83,
  162. (488<<8)/102, (488<<8)/205, (488<<8)/204, (488<<8)/102,
  163. (488<<8)/16, (488<<8)/16, (488<<8)/15, (488<<8)/8,
  164. (488<<8)/24, (488<<8)/18, (488<<8)/17, (488<<8)/9
  165. };
  166. PICO_INTERNAL int CheckDMA(void)
  167. {
  168. int burn = 0, xfers_can, dma_op = Pico.video.reg[0x17]>>6; // see gens for 00 and 01 modes
  169. int xfers = Pico.m.dma_xfers;
  170. int dma_op1;
  171. if(!(dma_op&2)) dma_op = (Pico.video.type==1) ? 0 : 1; // setting dma_timings offset here according to Gens
  172. dma_op1 = dma_op;
  173. if(Pico.video.reg[12] & 1) dma_op |= 4; // 40 cell mode?
  174. if(!(Pico.video.status&8)&&(Pico.video.reg[1]&0x40)) dma_op|=8; // active display?
  175. xfers_can = dma_timings[dma_op];
  176. if(xfers <= xfers_can)
  177. {
  178. if(dma_op&2) Pico.video.status&=~2; // dma no longer busy
  179. else {
  180. burn = xfers * dma_bsycles[dma_op] >> 8; // have to be approximate because can't afford division..
  181. }
  182. Pico.m.dma_xfers = 0;
  183. } else {
  184. if(!(dma_op&2)) burn = 488;
  185. Pico.m.dma_xfers -= xfers_can;
  186. }
  187. elprintf(EL_VDPDMA, "~Dma %i op=%i can=%i burn=%i [%i]", Pico.m.dma_xfers, dma_op1, xfers_can, burn, SekCyclesDone());
  188. //dprintf("~aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);
  189. return burn;
  190. }
  191. static __inline void SekRunM68k(int cyc)
  192. {
  193. int cyc_do;
  194. SekCycleAim+=cyc;
  195. if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;
  196. #if defined(EMU_CORE_DEBUG)
  197. // this means we do run-compare
  198. SekCycleCnt+=CM_compareRun(cyc_do, 0);
  199. #elif defined(EMU_C68K)
  200. PicoCpuCM68k.cycles=cyc_do;
  201. CycloneRun(&PicoCpuCM68k);
  202. SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;
  203. #elif defined(EMU_M68K)
  204. SekCycleCnt+=m68k_execute(cyc_do);
  205. #elif defined(EMU_F68K)
  206. SekCycleCnt+=fm68k_emulate(cyc_do+1, 0, 0);
  207. #endif
  208. }
  209. // to be called on 224 or line_sample scanlines only
  210. static __inline void getSamples(int y)
  211. {
  212. #if SIMPLE_WRITE_SOUND
  213. if (y != 224) return;
  214. PsndRender(0, PsndLen);
  215. if (PicoWriteSound) PicoWriteSound(PsndLen);
  216. PsndClear();
  217. #else
  218. static int curr_pos = 0;
  219. if(y == 224) {
  220. if(emustatus & 2)
  221. curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);
  222. else curr_pos = PsndRender(0, PsndLen);
  223. if (emustatus&1) emustatus|=2; else emustatus&=~2;
  224. if (PicoWriteSound) PicoWriteSound(curr_pos);
  225. // clear sound buffer
  226. PsndClear();
  227. }
  228. else if(emustatus & 3) {
  229. emustatus|= 2;
  230. emustatus&=~1;
  231. curr_pos = PsndRender(0, PsndLen/2);
  232. }
  233. #endif
  234. }
  235. #include "PicoFrameHints.c"
  236. int z80stopCycle;
  237. int z80_cycle_cnt; /* 'done' z80 cycles before z80_run() */
  238. int z80_cycle_aim;
  239. int z80_scanline;
  240. int z80_scanline_cycles; /* cycles done until z80_scanline */
  241. /* sync z80 to 68k */
  242. PICO_INTERNAL void PicoSyncZ80(int m68k_cycles_done)
  243. {
  244. int cnt;
  245. z80_cycle_aim = cycles_68k_to_z80(m68k_cycles_done);
  246. cnt = z80_cycle_aim - z80_cycle_cnt;
  247. elprintf(EL_BUSREQ, "z80 sync %i (%i|%i -> %i|%i)", cnt, z80_cycle_cnt, z80_cycle_cnt / 228,
  248. z80_cycle_aim, z80_cycle_aim / 228);
  249. if (cnt > 0)
  250. z80_cycle_cnt += z80_run(cnt);
  251. }
  252. void PicoFrame(void)
  253. {
  254. Pico.m.frame_count++;
  255. #if 0
  256. if ((Pico.m.frame_count & 0x3f) == 0)
  257. {
  258. extern int idlehit_addrs[], idlehit_counts[];
  259. int i;
  260. printf("--\n");
  261. for (i = 0; i < 128 && idlehit_addrs[i] != 0; i++) {
  262. if (idlehit_counts[i] != 0) {
  263. printf("%06x %i %i\n", idlehit_addrs[i], idlehit_counts[i], idlehit_counts[i] >> 6);
  264. idlehit_counts[i] = 0;
  265. }
  266. }
  267. }
  268. #endif
  269. if (PicoAHW & PAHW_MCD) {
  270. PicoFrameMCD();
  271. return;
  272. }
  273. //if(Pico.video.reg[12]&0x2) Pico.video.status ^= 0x10; // change odd bit in interlace mode
  274. if (!(PicoOpt&POPT_ALT_RENDERER))
  275. PicoFrameStart();
  276. PicoFrameHints();
  277. }
  278. void PicoFrameDrawOnly(void)
  279. {
  280. PicoFrameStart();
  281. PicoDrawSync(223, 0);
  282. }
  283. void PicoGetInternal(pint_t which, pint_ret_t *r)
  284. {
  285. switch (which)
  286. {
  287. case PI_ROM: r->vptr = Pico.rom; break;
  288. case PI_ISPAL: r->vint = Pico.m.pal; break;
  289. case PI_IS40_CELL: r->vint = Pico.video.reg[12]&1; break;
  290. case PI_IS240_LINES: r->vint = Pico.m.pal && (Pico.video.reg[1]&8); break;
  291. }
  292. }
  293. // callback to output message from emu
  294. void (*PicoMessage)(const char *msg)=NULL;