pico.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. PicoInterface PicoIn;
  14. void (*PicoResetHook)(void) = NULL;
  15. void (*PicoLineHook)(void) = NULL;
  16. // to be called once on emu init
  17. void PicoInit(void)
  18. {
  19. // Blank space for state:
  20. memset(&Pico,0,sizeof(Pico));
  21. memset(&PicoMem,0,sizeof(PicoMem));
  22. memset(&PicoIn.pad,0,sizeof(PicoIn.pad));
  23. memset(&PicoIn.padInt,0,sizeof(PicoIn.padInt));
  24. Pico.est.Pico = &Pico;
  25. Pico.est.PicoMem_vram = PicoMem.vram;
  26. Pico.est.PicoMem_cram = PicoMem.cram;
  27. Pico.est.PicoOpt = &PicoIn.opt;
  28. // Init CPUs:
  29. SekInit();
  30. z80_init(); // init even if we aren't going to use it
  31. PicoInitMCD();
  32. PicoSVPInit();
  33. Pico32xInit();
  34. PsndInit();
  35. PicoVideoInit();
  36. PicoDrawInit();
  37. PicoDraw2Init();
  38. }
  39. // to be called once on emu exit
  40. void PicoExit(void)
  41. {
  42. if (PicoIn.AHW & PAHW_MCD)
  43. PicoExitMCD();
  44. PicoCartUnload();
  45. z80_exit();
  46. PsndExit();
  47. free(Pico.sv.data);
  48. Pico.sv.data = NULL;
  49. Pico.sv.start = Pico.sv.end = 0;
  50. pevt_dump();
  51. }
  52. void PicoPower(void)
  53. {
  54. Pico.m.frame_count = 0;
  55. Pico.t.m68c_cnt = Pico.t.m68c_aim = 0;
  56. // clear all memory of the emulated machine
  57. memset(&PicoMem,0,sizeof(PicoMem));
  58. memset(&Pico.video,0,sizeof(Pico.video));
  59. memset(&Pico.m,0,sizeof(Pico.m));
  60. memset(&Pico.t,0,sizeof(Pico.t));
  61. Pico.video.pending_ints=0;
  62. z80_reset();
  63. // my MD1 VA6 console has this in IO
  64. PicoMem.ioports[1] = PicoMem.ioports[2] = PicoMem.ioports[3] = 0xff;
  65. // default VDP register values (based on Fusion)
  66. Pico.video.reg[0] = Pico.video.reg[1] = 0x04;
  67. Pico.video.reg[0xc] = 0x81;
  68. Pico.video.reg[0xf] = 0x02;
  69. PicoVideoFIFOMode(0, 1);
  70. if (PicoIn.AHW & PAHW_MCD)
  71. PicoPowerMCD();
  72. if (PicoIn.opt & POPT_EN_32X)
  73. PicoPower32x();
  74. PicoReset();
  75. }
  76. PICO_INTERNAL void PicoDetectRegion(void)
  77. {
  78. int support=0, hw=0, i;
  79. unsigned char pal=0;
  80. if (PicoIn.regionOverride)
  81. {
  82. support = PicoIn.regionOverride;
  83. }
  84. else
  85. {
  86. // Read cartridge region data:
  87. unsigned short *rd = (unsigned short *)(Pico.rom + 0x1f0);
  88. int region = (rd[0] << 16) | rd[1];
  89. for (i = 0; i < 4; i++)
  90. {
  91. int c;
  92. c = region >> (i<<3);
  93. c &= 0xff;
  94. if (c <= ' ') continue;
  95. if (c=='J') support|=1;
  96. else if (c=='U') support|=4;
  97. else if (c=='E') support|=8;
  98. else if (c=='j') {support|=1; break; }
  99. else if (c=='u') {support|=4; break; }
  100. else if (c=='e') {support|=8; break; }
  101. else
  102. {
  103. // New style code:
  104. char s[2]={0,0};
  105. s[0]=(char)c;
  106. support|=strtol(s,NULL,16);
  107. }
  108. }
  109. }
  110. // auto detection order override
  111. if (PicoIn.autoRgnOrder) {
  112. if (((PicoIn.autoRgnOrder>>0)&0xf) & support) support = (PicoIn.autoRgnOrder>>0)&0xf;
  113. else if (((PicoIn.autoRgnOrder>>4)&0xf) & support) support = (PicoIn.autoRgnOrder>>4)&0xf;
  114. else if (((PicoIn.autoRgnOrder>>8)&0xf) & support) support = (PicoIn.autoRgnOrder>>8)&0xf;
  115. }
  116. // Try to pick the best hardware value for English/50hz:
  117. if (support&8) { hw=0xc0; pal=1; } // Europe
  118. else if (support&4) hw=0x80; // USA
  119. else if (support&2) { hw=0x40; pal=1; } // Japan PAL
  120. else if (support&1) hw=0x00; // Japan NTSC
  121. else hw=0x80; // USA
  122. Pico.m.hardware=(unsigned char)(hw|0x20); // No disk attached
  123. Pico.m.pal=pal;
  124. }
  125. int PicoReset(void)
  126. {
  127. if (Pico.romsize <= 0)
  128. return 1;
  129. #if defined(CPU_CMP_R) || defined(CPU_CMP_W) || defined(DRC_CMP)
  130. PicoIn.opt |= POPT_DIS_VDP_FIFO|POPT_DIS_IDLE_DET;
  131. #endif
  132. /* must call now, so that banking is reset, and correct vectors get fetched */
  133. if (PicoResetHook)
  134. PicoResetHook();
  135. memset(&PicoIn.padInt, 0, sizeof(PicoIn.padInt));
  136. if (PicoIn.AHW & PAHW_SMS) {
  137. PicoResetMS();
  138. return 0;
  139. }
  140. SekReset();
  141. // ..but do not reset SekCycle* to not desync with addons
  142. // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games).
  143. SekSetRealTAS(PicoIn.AHW & PAHW_MCD);
  144. Pico.m.dirtyPal = 1;
  145. Pico.m.z80_bank68k = 0;
  146. Pico.m.z80_reset = 1;
  147. PicoDetectRegion();
  148. Pico.video.status = 0x3428 | Pico.m.pal; // 'always set' bits | vblank | collision | pal
  149. PsndReset(); // pal must be known here
  150. // create an empty "dma" to cause 68k exec start at random frame location
  151. PicoVideoFIFOWrite(rand() & 0x1fff, 0, 0, PVS_CPURD);
  152. SekFinishIdleDet();
  153. if (PicoIn.AHW & PAHW_MCD) {
  154. PicoResetMCD();
  155. return 0;
  156. }
  157. // reinit, so that checksum checks pass
  158. if (!(PicoIn.opt & POPT_DIS_IDLE_DET))
  159. SekInitIdleDet();
  160. if (PicoIn.opt & POPT_EN_32X)
  161. PicoReset32x();
  162. // reset sram state; enable sram access by default if it doesn't overlap with ROM
  163. Pico.m.sram_reg = 0;
  164. if ((Pico.sv.flags & SRF_EEPROM) || Pico.romsize <= Pico.sv.start)
  165. Pico.m.sram_reg |= SRR_MAPPED;
  166. if (Pico.sv.flags & SRF_ENABLED)
  167. elprintf(EL_STATUS, "sram: %06x - %06x; eeprom: %i", Pico.sv.start, Pico.sv.end,
  168. !!(Pico.sv.flags & SRF_EEPROM));
  169. return 0;
  170. }
  171. // flush config changes before emu loop starts
  172. void PicoLoopPrepare(void)
  173. {
  174. if (PicoIn.regionOverride)
  175. // force setting possibly changed..
  176. Pico.m.pal = (PicoIn.regionOverride == 2 || PicoIn.regionOverride == 8) ? 1 : 0;
  177. Pico.m.dirtyPal = 1;
  178. rendstatus_old = -1;
  179. }
  180. #include "pico_cmn.c"
  181. /* sync z80 to 68k */
  182. PICO_INTERNAL void PicoSyncZ80(unsigned int m68k_cycles_done)
  183. {
  184. int m68k_cnt;
  185. int cnt;
  186. m68k_cnt = m68k_cycles_done - Pico.t.m68c_frame_start;
  187. Pico.t.z80c_aim = cycles_68k_to_z80(m68k_cnt);
  188. cnt = Pico.t.z80c_aim - Pico.t.z80c_cnt;
  189. pprof_start(z80);
  190. elprintf(EL_BUSREQ, "z80 sync %i (%u|%u -> %u|%u)", cnt,
  191. Pico.t.z80c_cnt, Pico.t.z80c_cnt * 15 / 7 / 488,
  192. Pico.t.z80c_aim, Pico.t.z80c_aim * 15 / 7 / 488);
  193. if (cnt > 0)
  194. Pico.t.z80c_cnt += z80_run(cnt);
  195. pprof_end(z80);
  196. }
  197. void PicoFrame(void)
  198. {
  199. pprof_start(frame);
  200. Pico.m.frame_count++;
  201. if (PicoIn.AHW & PAHW_SMS) {
  202. PicoFrameMS();
  203. goto end;
  204. }
  205. if (PicoIn.AHW & PAHW_32X) {
  206. PicoFrame32x(); // also does MCD+32X
  207. goto end;
  208. }
  209. if (PicoIn.AHW & PAHW_MCD) {
  210. PicoFrameMCD();
  211. goto end;
  212. }
  213. //if(Pico.video.reg[12]&0x2) Pico.video.status ^= SR_ODD; // change odd bit in interlace mode
  214. PicoFrameStart();
  215. PicoFrameHints();
  216. end:
  217. pprof_end(frame);
  218. }
  219. void PicoFrameDrawOnly(void)
  220. {
  221. if (!(PicoIn.AHW & PAHW_SMS)) {
  222. PicoFrameStart();
  223. PicoDrawSync(Pico.m.pal?239:223, 0);
  224. } else {
  225. PicoFrameDrawOnlyMS();
  226. }
  227. }
  228. void PicoGetInternal(pint_t which, pint_ret_t *r)
  229. {
  230. switch (which)
  231. {
  232. case PI_ROM: r->vptr = Pico.rom; break;
  233. case PI_ISPAL: r->vint = Pico.m.pal; break;
  234. case PI_IS40_CELL: r->vint = Pico.video.reg[12]&1; break;
  235. case PI_IS240_LINES: r->vint = Pico.m.pal && (Pico.video.reg[1]&8); break;
  236. }
  237. }
  238. // vim:ts=2:sw=2:expandtab