pico.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "pico_int.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. }
  35. // to be called once on emu exit
  36. void PicoExit(void)
  37. {
  38. if (PicoAHW & PAHW_MCD)
  39. PicoExitMCD();
  40. PicoCartUnload();
  41. z80_exit();
  42. if (SRam.data)
  43. free(SRam.data);
  44. }
  45. void PicoPower(void)
  46. {
  47. Pico.m.frame_count = 0;
  48. // clear all memory of the emulated machine
  49. memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);
  50. memset(&Pico.video,0,sizeof(Pico.video));
  51. memset(&Pico.m,0,sizeof(Pico.m));
  52. Pico.video.pending_ints=0;
  53. z80_reset();
  54. // default VDP register values (based on Fusion)
  55. Pico.video.reg[0] = Pico.video.reg[1] = 0x04;
  56. Pico.video.reg[0xc] = 0x81;
  57. Pico.video.reg[0xf] = 0x02;
  58. if (PicoAHW & PAHW_MCD)
  59. PicoPowerMCD();
  60. PicoReset();
  61. }
  62. PICO_INTERNAL void PicoDetectRegion(void)
  63. {
  64. int support=0, hw=0, i;
  65. unsigned char pal=0;
  66. if (PicoRegionOverride)
  67. {
  68. support = PicoRegionOverride;
  69. }
  70. else
  71. {
  72. // Read cartridge region data:
  73. unsigned short *rd = (unsigned short *)(Pico.rom + 0x1f0);
  74. int region = (rd[0] << 16) | rd[1];
  75. for (i = 0; i < 4; i++)
  76. {
  77. int c;
  78. c = region >> (i<<3);
  79. 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. if (Pico.romsize <= 0)
  114. return 1;
  115. /* must call now, so that banking is reset, and correct vectors get fetched */
  116. if (PicoResetHook)
  117. PicoResetHook();
  118. memset(&PicoPadInt,0,sizeof(PicoPadInt));
  119. emustatus = 0;
  120. if (PicoAHW & PAHW_SMS) {
  121. PicoResetMS();
  122. return 0;
  123. }
  124. SekReset();
  125. // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games).
  126. SekSetRealTAS(PicoAHW & PAHW_MCD);
  127. SekCycleCntT=0;
  128. if (PicoAHW & PAHW_MCD)
  129. // needed for MCD to reset properly, probably some bug hides behind this..
  130. memset(Pico.ioports,0,sizeof(Pico.ioports));
  131. Pico.m.dirtyPal = 1;
  132. Pico.m.z80_bank68k = 0;
  133. Pico.m.z80_reset = 1;
  134. memset(Pico.zram, 0, sizeof(Pico.zram)); // ??
  135. PicoDetectRegion();
  136. Pico.video.status = 0x3428 | Pico.m.pal; // 'always set' bits | vblank | collision | pal
  137. PsndReset(); // pal must be known here
  138. // create an empty "dma" to cause 68k exec start at random frame location
  139. if (Pico.m.dma_xfers == 0 && !(PicoOpt & POPT_DIS_VDP_FIFO))
  140. Pico.m.dma_xfers = rand() & 0x1fff;
  141. SekFinishIdleDet();
  142. if (PicoAHW & PAHW_MCD) {
  143. PicoResetMCD();
  144. return 0;
  145. }
  146. // reinit, so that checksum checks pass
  147. if (!(PicoOpt & POPT_DIS_IDLE_DET))
  148. SekInitIdleDet();
  149. // reset sram state; enable sram access by default if it doesn't overlap with ROM
  150. Pico.m.sram_reg = 0;
  151. if ((SRam.flags & SRF_EEPROM) || Pico.romsize <= SRam.start)
  152. Pico.m.sram_reg |= SRR_MAPPED;
  153. if (SRam.flags & SRF_ENABLED)
  154. elprintf(EL_STATUS, "sram: %06x - %06x; eeprom: %i", SRam.start, SRam.end,
  155. !!(SRam.flags & SRF_EEPROM));
  156. return 0;
  157. }
  158. // dma2vram settings are just hacks to unglitch Legend of Galahad (needs <= 104 to work)
  159. // same for Outrunners (92-121, when active is set to 24)
  160. // 96 is VR hack
  161. static const int dma_timings[] = {
  162. 96, 167, 166, 83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy
  163. 102, 205, 204, 102, // vblank: 40cell:
  164. 16, 16, 15, 8, // active: 32cell:
  165. 24, 18, 17, 9 // ...
  166. };
  167. static const int dma_bsycles[] = {
  168. (488<<8)/96, (488<<8)/167, (488<<8)/166, (488<<8)/83,
  169. (488<<8)/102, (488<<8)/205, (488<<8)/204, (488<<8)/102,
  170. (488<<8)/16, (488<<8)/16, (488<<8)/15, (488<<8)/8,
  171. (488<<8)/24, (488<<8)/18, (488<<8)/17, (488<<8)/9
  172. };
  173. PICO_INTERNAL int CheckDMA(void)
  174. {
  175. int burn = 0, xfers_can, dma_op = Pico.video.reg[0x17]>>6; // see gens for 00 and 01 modes
  176. int xfers = Pico.m.dma_xfers;
  177. int dma_op1;
  178. if(!(dma_op&2)) dma_op = (Pico.video.type==1) ? 0 : 1; // setting dma_timings offset here according to Gens
  179. dma_op1 = dma_op;
  180. if(Pico.video.reg[12] & 1) dma_op |= 4; // 40 cell mode?
  181. if(!(Pico.video.status&8)&&(Pico.video.reg[1]&0x40)) dma_op|=8; // active display?
  182. xfers_can = dma_timings[dma_op];
  183. if(xfers <= xfers_can)
  184. {
  185. if(dma_op&2) Pico.video.status&=~2; // dma no longer busy
  186. else {
  187. burn = xfers * dma_bsycles[dma_op] >> 8; // have to be approximate because can't afford division..
  188. }
  189. Pico.m.dma_xfers = 0;
  190. } else {
  191. if(!(dma_op&2)) burn = 488;
  192. Pico.m.dma_xfers -= xfers_can;
  193. }
  194. elprintf(EL_VDPDMA, "~Dma %i op=%i can=%i burn=%i [%i]", Pico.m.dma_xfers, dma_op1, xfers_can, burn, SekCyclesDone());
  195. //dprintf("~aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);
  196. return burn;
  197. }
  198. static __inline void SekRunM68k(int cyc)
  199. {
  200. int cyc_do;
  201. SekCycleAim+=cyc;
  202. if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;
  203. #if defined(EMU_CORE_DEBUG)
  204. // this means we do run-compare
  205. SekCycleCnt+=CM_compareRun(cyc_do, 0);
  206. #elif defined(EMU_C68K)
  207. PicoCpuCM68k.cycles=cyc_do;
  208. CycloneRun(&PicoCpuCM68k);
  209. SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;
  210. #elif defined(EMU_M68K)
  211. SekCycleCnt+=m68k_execute(cyc_do);
  212. #elif defined(EMU_F68K)
  213. SekCycleCnt+=fm68k_emulate(cyc_do+1, 0, 0);
  214. #endif
  215. }
  216. #include "pico_cmn.c"
  217. int z80stopCycle;
  218. int z80_cycle_cnt; /* 'done' z80 cycles before z80_run() */
  219. int z80_cycle_aim;
  220. int z80_scanline;
  221. int z80_scanline_cycles; /* cycles done until z80_scanline */
  222. /* sync z80 to 68k */
  223. PICO_INTERNAL void PicoSyncZ80(int m68k_cycles_done)
  224. {
  225. int cnt;
  226. z80_cycle_aim = cycles_68k_to_z80(m68k_cycles_done);
  227. cnt = z80_cycle_aim - z80_cycle_cnt;
  228. elprintf(EL_BUSREQ, "z80 sync %i (%i|%i -> %i|%i)", cnt, z80_cycle_cnt, z80_cycle_cnt / 228,
  229. z80_cycle_aim, z80_cycle_aim / 228);
  230. if (cnt > 0)
  231. z80_cycle_cnt += z80_run(cnt);
  232. }
  233. void PicoFrame(void)
  234. {
  235. Pico.m.frame_count++;
  236. if (PicoAHW & PAHW_SMS) {
  237. PicoFrameMS();
  238. return;
  239. }
  240. if (PicoAHW & PAHW_MCD) {
  241. PicoFrameMCD();
  242. return;
  243. }
  244. //if(Pico.video.reg[12]&0x2) Pico.video.status ^= 0x10; // change odd bit in interlace mode
  245. PicoFrameStart();
  246. PicoFrameHints();
  247. }
  248. void PicoFrameDrawOnly(void)
  249. {
  250. if (!(PicoAHW & PAHW_SMS)) {
  251. PicoFrameStart();
  252. PicoDrawSync(223, 0);
  253. } else {
  254. PicoFrameDrawOnlyMS();
  255. }
  256. }
  257. void PicoGetInternal(pint_t which, pint_ret_t *r)
  258. {
  259. switch (which)
  260. {
  261. case PI_ROM: r->vptr = Pico.rom; break;
  262. case PI_ISPAL: r->vint = Pico.m.pal; break;
  263. case PI_IS40_CELL: r->vint = Pico.video.reg[12]&1; break;
  264. case PI_IS240_LINES: r->vint = Pico.m.pal && (Pico.video.reg[1]&8); break;
  265. }
  266. }
  267. // callback to output message from emu
  268. void (*PicoMessage)(const char *msg)=NULL;