pico.c 9.3 KB

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