pico.c 9.2 KB

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