Commit | Line | Data |
---|---|---|
cff531af | 1 | /*\r |
2 | * PicoDrive\r | |
3 | * (c) Copyright Dave, 2004\r | |
4 | * (C) notaz, 2006-2010\r | |
5 | *\r | |
6 | * This work is licensed under the terms of MAME license.\r | |
7 | * See COPYING file in the top-level directory.\r | |
8 | */\r | |
cc68a136 | 9 | \r |
efcba75f | 10 | #include "pico_int.h"\r |
cc68a136 | 11 | #include "sound/ym2612.h"\r |
12 | \r | |
cc68a136 | 13 | struct Pico Pico;\r |
5e128c6d | 14 | int PicoOpt; \r |
48582bcf D |
15 | int PicoSkipFrame; // skip rendering frame?\r |
16 | int PicoPad[2]; // Joypads, format is MXYZ SACB RLDU\r | |
17 | int PicoPadInt[2]; // internal copy\r | |
18 | int PicoAHW; // active addon hardware: PAHW_*\r | |
19 | int PicoQuirks; // game-specific quirks\r | |
20 | int PicoRegionOverride; // override the region detection 0: Auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe\r | |
21 | int PicoRegionFPSOverride; // override the refresh rate of the region 0: Auto, 1: NTSC, 2: PAL\r | |
5e128c6d | 22 | int PicoAutoRgnOrder;\r |
23 | \r | |
24 | struct PicoSRAM SRam;\r | |
25 | int emustatus; // rapid_ym2612, multi_ym_updates\r | |
26 | int scanlines_total;\r | |
602133e1 | 27 | \r |
f8ef8ff7 | 28 | void (*PicoWriteSound)(int len) = NULL; // called at the best time to send sound buffer (PsndOut) to hardware\r |
29 | void (*PicoResetHook)(void) = NULL;\r | |
b0677887 | 30 | void (*PicoLineHook)(void) = NULL;\r |
cc68a136 | 31 | \r |
cc68a136 | 32 | // to be called once on emu init\r |
2aa27095 | 33 | void PicoInit(void)\r |
cc68a136 | 34 | {\r |
35 | // Blank space for state:\r | |
36 | memset(&Pico,0,sizeof(Pico));\r | |
37 | memset(&PicoPad,0,sizeof(PicoPad));\r | |
5f9a0d16 | 38 | memset(&PicoPadInt,0,sizeof(PicoPadInt));\r |
cc68a136 | 39 | \r |
d01fa204 | 40 | Pico.est.Pico_video = &Pico.video;\r |
41 | Pico.est.Pico_vram = Pico.vram;\r | |
42 | \r | |
cc68a136 | 43 | // Init CPUs:\r |
44 | SekInit();\r | |
45 | z80_init(); // init even if we aren't going to use it\r | |
46 | \r | |
cc68a136 | 47 | PicoInitMCD();\r |
e807ac75 | 48 | PicoSVPInit();\r |
be2c4208 | 49 | Pico32xInit();\r |
cc68a136 | 50 | }\r |
51 | \r | |
52 | // to be called once on emu exit\r | |
53 | void PicoExit(void)\r | |
54 | {\r | |
602133e1 | 55 | if (PicoAHW & PAHW_MCD)\r |
4f265db7 | 56 | PicoExitMCD();\r |
ca482e5d | 57 | PicoCartUnload();\r |
cc68a136 | 58 | z80_exit();\r |
59 | \r | |
45f2f245 | 60 | if (SRam.data)\r |
61 | free(SRam.data);\r | |
19886062 | 62 | pevt_dump();\r |
cc68a136 | 63 | }\r |
64 | \r | |
1cb1584b | 65 | void PicoPower(void)\r |
66 | {\r | |
053fd9b4 | 67 | Pico.m.frame_count = 0;\r |
8e4e84c2 | 68 | SekCycleCnt = SekCycleAim = 0;\r |
053fd9b4 | 69 | \r |
1cb1584b | 70 | // clear all memory of the emulated machine\r |
b8a1c09a | 71 | memset(&Pico.ram,0,(unsigned char *)&Pico.rom - Pico.ram);\r |
1cb1584b | 72 | \r |
73 | memset(&Pico.video,0,sizeof(Pico.video));\r | |
74 | memset(&Pico.m,0,sizeof(Pico.m));\r | |
75 | \r | |
76 | Pico.video.pending_ints=0;\r | |
77 | z80_reset();\r | |
78 | \r | |
531a8f38 | 79 | // my MD1 VA6 console has this in IO\r |
80 | Pico.ioports[1] = Pico.ioports[2] = Pico.ioports[3] = 0xff;\r | |
81 | \r | |
1cb1584b | 82 | // default VDP register values (based on Fusion)\r |
83 | Pico.video.reg[0] = Pico.video.reg[1] = 0x04;\r | |
84 | Pico.video.reg[0xc] = 0x81;\r | |
85 | Pico.video.reg[0xf] = 0x02;\r | |
86 | \r | |
602133e1 | 87 | if (PicoAHW & PAHW_MCD)\r |
1cb1584b | 88 | PicoPowerMCD();\r |
89 | \r | |
db1d3564 | 90 | if (PicoOpt & POPT_EN_32X)\r |
974fdb5b | 91 | PicoPower32x();\r |
92 | \r | |
1cb1584b | 93 | PicoReset();\r |
94 | }\r | |
95 | \r | |
1e6b5e39 | 96 | PICO_INTERNAL void PicoDetectRegion(void)\r |
cc68a136 | 97 | {\r |
48582bcf D |
98 | // PicoDetectRegion not ready yet\r |
99 | if (Pico.romsize <= 0)\r | |
100 | return;\r | |
101 | \r | |
1e6b5e39 | 102 | int support=0, hw=0, i;\r |
cc68a136 | 103 | unsigned char pal=0;\r |
cc68a136 | 104 | \r |
1e6b5e39 | 105 | if (PicoRegionOverride)\r |
cc68a136 | 106 | {\r |
107 | support = PicoRegionOverride;\r | |
108 | }\r | |
109 | else\r | |
110 | {\r | |
111 | // Read cartridge region data:\r | |
af37bca8 | 112 | unsigned short *rd = (unsigned short *)(Pico.rom + 0x1f0);\r |
113 | int region = (rd[0] << 16) | rd[1];\r | |
cc68a136 | 114 | \r |
af37bca8 | 115 | for (i = 0; i < 4; i++)\r |
cc68a136 | 116 | {\r |
af37bca8 | 117 | int c;\r |
cc68a136 | 118 | \r |
af37bca8 | 119 | c = region >> (i<<3);\r |
120 | c &= 0xff;\r | |
121 | if (c <= ' ') continue;\r | |
cc68a136 | 122 | \r |
51a902ae | 123 | if (c=='J') support|=1;\r |
124 | else if (c=='U') support|=4;\r | |
125 | else if (c=='E') support|=8;\r | |
126 | else if (c=='j') {support|=1; break; }\r | |
127 | else if (c=='u') {support|=4; break; }\r | |
128 | else if (c=='e') {support|=8; break; }\r | |
cc68a136 | 129 | else\r |
130 | {\r | |
131 | // New style code:\r | |
132 | char s[2]={0,0};\r | |
133 | s[0]=(char)c;\r | |
134 | support|=strtol(s,NULL,16);\r | |
135 | }\r | |
136 | }\r | |
137 | }\r | |
138 | \r | |
51a902ae | 139 | // auto detection order override\r |
140 | if (PicoAutoRgnOrder) {\r | |
141 | if (((PicoAutoRgnOrder>>0)&0xf) & support) support = (PicoAutoRgnOrder>>0)&0xf;\r | |
142 | else if (((PicoAutoRgnOrder>>4)&0xf) & support) support = (PicoAutoRgnOrder>>4)&0xf;\r | |
143 | else if (((PicoAutoRgnOrder>>8)&0xf) & support) support = (PicoAutoRgnOrder>>8)&0xf;\r | |
144 | }\r | |
145 | \r | |
cc68a136 | 146 | // Try to pick the best hardware value for English/50hz:\r |
147 | if (support&8) { hw=0xc0; pal=1; } // Europe\r | |
148 | else if (support&4) hw=0x80; // USA\r | |
149 | else if (support&2) { hw=0x40; pal=1; } // Japan PAL\r | |
150 | else if (support&1) hw=0x00; // Japan NTSC\r | |
151 | else hw=0x80; // USA\r | |
152 | \r | |
153 | Pico.m.hardware=(unsigned char)(hw|0x20); // No disk attached\r | |
48582bcf D |
154 | \r |
155 | if (PicoRegionFPSOverride > 0)\r | |
156 | pal = PicoRegionFPSOverride - 1; // pal - 0: NTSC, 1: PAL | PicoRegionFPSOverride - 0: Auto, 1: NTSC, 2: PAL\r | |
157 | \r | |
cc68a136 | 158 | Pico.m.pal=pal;\r |
1e6b5e39 | 159 | }\r |
160 | \r | |
161 | int PicoReset(void)\r | |
162 | {\r | |
2ec9bec5 | 163 | if (Pico.romsize <= 0)\r |
164 | return 1;\r | |
1e6b5e39 | 165 | \r |
12da51c2 | 166 | #if defined(CPU_CMP_R) || defined(CPU_CMP_W) || defined(DRC_CMP)\r |
6d797957 | 167 | PicoOpt |= POPT_DIS_VDP_FIFO|POPT_DIS_IDLE_DET;\r |
168 | #endif\r | |
169 | \r | |
1e6b5e39 | 170 | /* must call now, so that banking is reset, and correct vectors get fetched */\r |
2ec9bec5 | 171 | if (PicoResetHook)\r |
172 | PicoResetHook();\r | |
1e6b5e39 | 173 | \r |
5f9a0d16 | 174 | memset(&PicoPadInt,0,sizeof(PicoPadInt));\r |
2ec9bec5 | 175 | emustatus = 0;\r |
176 | \r | |
177 | if (PicoAHW & PAHW_SMS) {\r | |
178 | PicoResetMS();\r | |
179 | return 0;\r | |
180 | }\r | |
181 | \r | |
182 | SekReset();\r | |
8e4e84c2 | 183 | // ..but do not reset SekCycle* to not desync with addons\r |
184 | \r | |
1e6b5e39 | 185 | // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games).\r |
186 | SekSetRealTAS(PicoAHW & PAHW_MCD);\r | |
1e6b5e39 | 187 | \r |
1e6b5e39 | 188 | Pico.m.dirtyPal = 1;\r |
189 | \r | |
1832075e | 190 | Pico.m.z80_bank68k = 0;\r |
af37bca8 | 191 | Pico.m.z80_reset = 1;\r |
1832075e | 192 | \r |
1e6b5e39 | 193 | PicoDetectRegion();\r |
e5fa9817 | 194 | Pico.video.status = 0x3428 | Pico.m.pal; // 'always set' bits | vblank | collision | pal\r |
cc68a136 | 195 | \r |
9d917eea | 196 | PsndReset(); // pal must be known here\r |
cc68a136 | 197 | \r |
1cb1584b | 198 | // create an empty "dma" to cause 68k exec start at random frame location\r |
2ec9bec5 | 199 | if (Pico.m.dma_xfers == 0 && !(PicoOpt & POPT_DIS_VDP_FIFO))\r |
1cb1584b | 200 | Pico.m.dma_xfers = rand() & 0x1fff;\r |
201 | \r | |
5ed2a20e | 202 | SekFinishIdleDet();\r |
203 | \r | |
602133e1 | 204 | if (PicoAHW & PAHW_MCD) {\r |
1cb1584b | 205 | PicoResetMCD();\r |
cc68a136 | 206 | return 0;\r |
207 | }\r | |
5ed2a20e | 208 | \r |
209 | // reinit, so that checksum checks pass\r | |
210 | if (!(PicoOpt & POPT_DIS_IDLE_DET))\r | |
211 | SekInitIdleDet();\r | |
cc68a136 | 212 | \r |
1f1ff763 | 213 | if (PicoOpt & POPT_EN_32X)\r |
be2c4208 | 214 | PicoReset32x();\r |
be2c4208 | 215 | \r |
1dceadae | 216 | // reset sram state; enable sram access by default if it doesn't overlap with ROM\r |
45f2f245 | 217 | Pico.m.sram_reg = 0;\r |
218 | if ((SRam.flags & SRF_EEPROM) || Pico.romsize <= SRam.start)\r | |
219 | Pico.m.sram_reg |= SRR_MAPPED;\r | |
cc68a136 | 220 | \r |
45f2f245 | 221 | if (SRam.flags & SRF_ENABLED)\r |
222 | elprintf(EL_STATUS, "sram: %06x - %06x; eeprom: %i", SRam.start, SRam.end,\r | |
223 | !!(SRam.flags & SRF_EEPROM));\r | |
cc68a136 | 224 | \r |
225 | return 0;\r | |
226 | }\r | |
227 | \r | |
46bcb899 | 228 | // flush config changes before emu loop starts\r |
5e128c6d | 229 | void PicoLoopPrepare(void)\r |
230 | {\r | |
48582bcf D |
231 | if (PicoRegionFPSOverride) // PicoRegionFPSOverride is proprietary on PicoRegionOverride\r |
232 | {\r | |
233 | // force setting possibly changed..\r | |
234 | Pico.m.pal = PicoRegionFPSOverride - 1; // Pico.m.pal - 0: NTSC, 1: PAL | PicoRegionFPSOverride - 0: Auto, 1: NTSC, 2: PAL\r | |
235 | }\r | |
236 | else if (PicoRegionOverride)\r | |
237 | {\r | |
5e128c6d | 238 | // force setting possibly changed..\r |
239 | Pico.m.pal = (PicoRegionOverride == 2 || PicoRegionOverride == 8) ? 1 : 0;\r | |
48582bcf | 240 | }\r |
5e128c6d | 241 | \r |
242 | // FIXME: PAL has 313 scanlines..\r | |
243 | scanlines_total = Pico.m.pal ? 312 : 262;\r | |
db1d3564 | 244 | \r |
2446536b | 245 | Pico.m.dirtyPal = 1;\r |
246 | rendstatus_old = -1;\r | |
5e128c6d | 247 | }\r |
248 | \r | |
1dceadae | 249 | \r |
69996cb7 | 250 | // dma2vram settings are just hacks to unglitch Legend of Galahad (needs <= 104 to work)\r |
251 | // same for Outrunners (92-121, when active is set to 24)\r | |
48df6e9e | 252 | // 96 is VR hack\r |
69996cb7 | 253 | static const int dma_timings[] = {\r |
a4dfdb6d | 254 | 167, 167, 166, 83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy\r |
053fd9b4 | 255 | 102, 205, 204, 102, // vblank: 40cell:\r |
256 | 16, 16, 15, 8, // active: 32cell:\r | |
257 | 24, 18, 17, 9 // ...\r | |
4f672280 | 258 | };\r |
259 | \r | |
69996cb7 | 260 | static const int dma_bsycles[] = {\r |
a4dfdb6d | 261 | (488<<8)/167, (488<<8)/167, (488<<8)/166, (488<<8)/83,\r |
262 | (488<<8)/102, (488<<8)/233, (488<<8)/204, (488<<8)/102,\r | |
053fd9b4 | 263 | (488<<8)/16, (488<<8)/16, (488<<8)/15, (488<<8)/8,\r |
264 | (488<<8)/24, (488<<8)/18, (488<<8)/17, (488<<8)/9\r | |
312e9ce1 | 265 | };\r |
266 | \r | |
a4dfdb6d | 267 | // grossly inaccurate.. FIXME FIXXXMEE\r |
eff55556 | 268 | PICO_INTERNAL int CheckDMA(void)\r |
4f672280 | 269 | {\r |
69996cb7 | 270 | int burn = 0, xfers_can, dma_op = Pico.video.reg[0x17]>>6; // see gens for 00 and 01 modes\r |
271 | int xfers = Pico.m.dma_xfers;\r | |
312e9ce1 | 272 | int dma_op1;\r |
4f672280 | 273 | \r |
312e9ce1 | 274 | if(!(dma_op&2)) dma_op = (Pico.video.type==1) ? 0 : 1; // setting dma_timings offset here according to Gens\r |
275 | dma_op1 = dma_op;\r | |
276 | if(Pico.video.reg[12] & 1) dma_op |= 4; // 40 cell mode?\r | |
277 | if(!(Pico.video.status&8)&&(Pico.video.reg[1]&0x40)) dma_op|=8; // active display?\r | |
69996cb7 | 278 | xfers_can = dma_timings[dma_op];\r |
9761a7d0 | 279 | if(xfers <= xfers_can)\r |
280 | {\r | |
4f672280 | 281 | if(dma_op&2) Pico.video.status&=~2; // dma no longer busy\r |
282 | else {\r | |
69996cb7 | 283 | burn = xfers * dma_bsycles[dma_op] >> 8; // have to be approximate because can't afford division..\r |
4f672280 | 284 | }\r |
69996cb7 | 285 | Pico.m.dma_xfers = 0;\r |
4f672280 | 286 | } else {\r |
287 | if(!(dma_op&2)) burn = 488;\r | |
69996cb7 | 288 | Pico.m.dma_xfers -= xfers_can;\r |
4f672280 | 289 | }\r |
290 | \r | |
69996cb7 | 291 | elprintf(EL_VDPDMA, "~Dma %i op=%i can=%i burn=%i [%i]", Pico.m.dma_xfers, dma_op1, xfers_can, burn, SekCyclesDone());\r |
312e9ce1 | 292 | //dprintf("~aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);\r |
293 | return burn;\r | |
4f672280 | 294 | }\r |
295 | \r | |
efcba75f | 296 | #include "pico_cmn.c"\r |
4b9c5888 | 297 | \r |
ae214f1c | 298 | unsigned int last_z80_sync; /* in 68k cycles */\r |
299 | int z80_cycle_cnt;\r | |
4b9c5888 | 300 | int z80_cycle_aim;\r |
301 | int z80_scanline;\r | |
302 | int z80_scanline_cycles; /* cycles done until z80_scanline */\r | |
303 | \r | |
304 | /* sync z80 to 68k */\r | |
ae214f1c | 305 | PICO_INTERNAL void PicoSyncZ80(unsigned int m68k_cycles_done)\r |
cc68a136 | 306 | {\r |
a6523294 | 307 | int m68k_cnt;\r |
4b9c5888 | 308 | int cnt;\r |
a6523294 | 309 | \r |
310 | m68k_cnt = m68k_cycles_done - last_z80_sync;\r | |
311 | z80_cycle_aim += cycles_68k_to_z80(m68k_cnt);\r | |
4b9c5888 | 312 | cnt = z80_cycle_aim - z80_cycle_cnt;\r |
ae214f1c | 313 | last_z80_sync = m68k_cycles_done;\r |
cc68a136 | 314 | \r |
f6c49d38 | 315 | pprof_start(z80);\r |
316 | \r | |
ae214f1c | 317 | elprintf(EL_BUSREQ, "z80 sync %i (%u|%u -> %u|%u)", cnt,\r |
318 | z80_cycle_cnt, z80_cycle_cnt / 288,\r | |
319 | z80_cycle_aim, z80_cycle_aim / 288);\r | |
4b9c5888 | 320 | \r |
321 | if (cnt > 0)\r | |
322 | z80_cycle_cnt += z80_run(cnt);\r | |
f6c49d38 | 323 | \r |
324 | pprof_end(z80);\r | |
cc68a136 | 325 | }\r |
326 | \r | |
4b9c5888 | 327 | \r |
2aa27095 | 328 | void PicoFrame(void)\r |
cc68a136 | 329 | {\r |
f6c49d38 | 330 | pprof_start(frame);\r |
331 | \r | |
8c1952f0 | 332 | Pico.m.frame_count++;\r |
333 | \r | |
19954be1 | 334 | if (PicoAHW & PAHW_SMS) {\r |
335 | PicoFrameMS();\r | |
f6c49d38 | 336 | goto end;\r |
cc68a136 | 337 | }\r |
19954be1 | 338 | \r |
fa8fb754 | 339 | if (PicoAHW & PAHW_32X) {\r |
340 | PicoFrame32x(); // also does MCD+32X\r | |
f6c49d38 | 341 | goto end;\r |
3e49ffd0 | 342 | }\r |
cc68a136 | 343 | \r |
fa8fb754 | 344 | if (PicoAHW & PAHW_MCD) {\r |
345 | PicoFrameMCD();\r | |
f6c49d38 | 346 | goto end;\r |
974fdb5b | 347 | }\r |
348 | \r | |
cc68a136 | 349 | //if(Pico.video.reg[12]&0x2) Pico.video.status ^= 0x10; // change odd bit in interlace mode\r |
350 | \r | |
19954be1 | 351 | PicoFrameStart();\r |
2aa27095 | 352 | PicoFrameHints();\r |
f6c49d38 | 353 | \r |
354 | end:\r | |
355 | pprof_end(frame);\r | |
cc68a136 | 356 | }\r |
357 | \r | |
a12e0116 | 358 | void PicoFrameDrawOnly(void)\r |
359 | {\r | |
87b0845f | 360 | if (!(PicoAHW & PAHW_SMS)) {\r |
361 | PicoFrameStart();\r | |
362 | PicoDrawSync(223, 0);\r | |
363 | } else {\r | |
364 | PicoFrameDrawOnlyMS();\r | |
365 | }\r | |
a12e0116 | 366 | }\r |
367 | \r | |
4609d0cd | 368 | void PicoGetInternal(pint_t which, pint_ret_t *r)\r |
8e5427a0 | 369 | {\r |
370 | switch (which)\r | |
371 | {\r | |
4609d0cd | 372 | case PI_ROM: r->vptr = Pico.rom; break;\r |
373 | case PI_ISPAL: r->vint = Pico.m.pal; break;\r | |
374 | case PI_IS40_CELL: r->vint = Pico.video.reg[12]&1; break;\r | |
375 | case PI_IS240_LINES: r->vint = Pico.m.pal && (Pico.video.reg[1]&8); break;\r | |
8e5427a0 | 376 | }\r |
8e5427a0 | 377 | }\r |
378 | \r | |
66fdc0f0 | 379 | // callback to output message from emu\r |
380 | void (*PicoMessage)(const char *msg)=NULL;\r | |
cc68a136 | 381 | \r |