pico.c 7.2 KB

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