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