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