platform ps2, handle audio similar to psp
[picodrive.git] / pico / pico.c
CommitLineData
cff531af 1/*\r
2 * PicoDrive\r
3 * (c) Copyright Dave, 2004\r
4 * (C) notaz, 2006-2010\r
7bf552b5 5 * (C) irixxxx, 2020-2024\r
cff531af 6 *\r
7 * This work is licensed under the terms of MAME license.\r
8 * See COPYING file in the top-level directory.\r
9 */\r
cc68a136 10\r
efcba75f 11#include "pico_int.h"\r
cc68a136 12#include "sound/ym2612.h"\r
13\r
cc68a136 14struct Pico Pico;\r
88fd63ad 15struct PicoMem PicoMem;\r
93f9619e 16PicoInterface PicoIn;\r
602133e1 17\r
f8ef8ff7 18void (*PicoResetHook)(void) = NULL;\r
b0677887 19void (*PicoLineHook)(void) = NULL;\r
cc68a136 20\r
cc68a136 21// to be called once on emu init\r
2aa27095 22void PicoInit(void)\r
cc68a136 23{\r
24 // Blank space for state:\r
25 memset(&Pico,0,sizeof(Pico));\r
88fd63ad 26 memset(&PicoMem,0,sizeof(PicoMem));\r
93f9619e 27 memset(&PicoIn.pad,0,sizeof(PicoIn.pad));\r
28 memset(&PicoIn.padInt,0,sizeof(PicoIn.padInt));\r
cc68a136 29\r
88fd63ad 30 Pico.est.Pico = &Pico;\r
31 Pico.est.PicoMem_vram = PicoMem.vram;\r
32 Pico.est.PicoMem_cram = PicoMem.cram;\r
93f9619e 33 Pico.est.PicoOpt = &PicoIn.opt;\r
ea38612f 34\r
cc68a136 35 // Init CPUs:\r
36 SekInit();\r
37 z80_init(); // init even if we aren't going to use it\r
38\r
cc68a136 39 PicoInitMCD();\r
e807ac75 40 PicoSVPInit();\r
be2c4208 41 Pico32xInit();\r
a2f24bfa 42 PsndInit();\r
99bdfd31 43\r
1886ac5f 44 PicoVideoInit();\r
99bdfd31 45 PicoDrawInit();\r
98a27142 46 PicoDraw2Init();\r
cc68a136 47}\r
48\r
49// to be called once on emu exit\r
50void PicoExit(void)\r
51{\r
93f9619e 52 if (PicoIn.AHW & PAHW_MCD)\r
4f265db7 53 PicoExitMCD();\r
ca482e5d 54 PicoCartUnload();\r
cc68a136 55 z80_exit();\r
a2f24bfa 56 PsndExit();\r
cc68a136 57\r
a4fa71d4 58 free(Pico.sv.data);\r
59 Pico.sv.data = NULL;\r
60 Pico.sv.start = Pico.sv.end = 0;\r
19886062 61 pevt_dump();\r
cc68a136 62}\r
63\r
1cb1584b 64void PicoPower(void)\r
65{\r
053fd9b4 66 Pico.m.frame_count = 0;\r
88fd63ad 67 Pico.t.m68c_cnt = Pico.t.m68c_aim = 0;\r
053fd9b4 68\r
1cb1584b 69 // clear all memory of the emulated machine\r
88fd63ad 70 memset(&PicoMem,0,sizeof(PicoMem));\r
1cb1584b 71\r
72 memset(&Pico.video,0,sizeof(Pico.video));\r
73 memset(&Pico.m,0,sizeof(Pico.m));\r
17bd69ad 74 memset(&Pico.t,0,sizeof(Pico.t));\r
1cb1584b 75\r
531a8f38 76 // my MD1 VA6 console has this in IO\r
88fd63ad 77 PicoMem.ioports[1] = PicoMem.ioports[2] = PicoMem.ioports[3] = 0xff;\r
531a8f38 78\r
c7661b80 79 Pico.video.hint_irq = (PicoIn.AHW & PAHW_PICO ? 5 : 4);\r
80\r
93f9619e 81 if (PicoIn.AHW & PAHW_MCD)\r
1cb1584b 82 PicoPowerMCD();\r
83\r
93f9619e 84 if (PicoIn.opt & POPT_EN_32X)\r
974fdb5b 85 PicoPower32x();\r
86\r
1cb1584b 87 PicoReset();\r
96e59821 88\r
89 // powerup default VDP register values from TMSS BIOS\r
90 Pico.video.reg[0] = Pico.video.reg[1] = 0x04;\r
91 Pico.video.reg[0xc] = 0x81;\r
92 Pico.video.reg[0xf] = 0x02;\r
93 SATaddr = 0x0000;\r
94 SATmask = ~0x3ff;\r
1cb1584b 95}\r
96\r
1e6b5e39 97PICO_INTERNAL void PicoDetectRegion(void)\r
cc68a136 98{\r
1e6b5e39 99 int support=0, hw=0, i;\r
cc68a136 100 unsigned char pal=0;\r
7cbd42bc 101 char *pr = (char *)(Pico.rom + 0x1f0);\r
cc68a136 102\r
93f9619e 103 if (PicoIn.regionOverride)\r
cc68a136 104 {\r
93f9619e 105 support = PicoIn.regionOverride;\r
cc68a136 106 }\r
7cbd42bc 107 else if (strcmp(pr, "EUROPE") == 0 || strcmp(pr, "Europe") == 0)\r
108 {\r
109 // Unusual cartridge region 'code'\r
110 support|=8;\r
111 }\r
cc68a136 112 else\r
113 {\r
114 // Read cartridge region data:\r
7cbd42bc 115 unsigned short *rd = (unsigned short *)pr;\r
af37bca8 116 int region = (rd[0] << 16) | rd[1];\r
cc68a136 117\r
af37bca8 118 for (i = 0; i < 4; i++)\r
cc68a136 119 {\r
af37bca8 120 int c;\r
cc68a136 121\r
af37bca8 122 c = region >> (i<<3);\r
123 c &= 0xff;\r
124 if (c <= ' ') continue;\r
cc68a136 125\r
51a902ae 126 if (c=='J') support|=1;\r
127 else if (c=='U') support|=4;\r
128 else if (c=='E') support|=8;\r
129 else if (c=='j') {support|=1; break; }\r
130 else if (c=='u') {support|=4; break; }\r
131 else if (c=='e') {support|=8; break; }\r
cc68a136 132 else\r
133 {\r
134 // New style code:\r
135 char s[2]={0,0};\r
136 s[0]=(char)c;\r
137 support|=strtol(s,NULL,16);\r
138 }\r
139 }\r
140 }\r
141\r
51a902ae 142 // auto detection order override\r
93f9619e 143 if (PicoIn.autoRgnOrder) {\r
144 if (((PicoIn.autoRgnOrder>>0)&0xf) & support) support = (PicoIn.autoRgnOrder>>0)&0xf;\r
145 else if (((PicoIn.autoRgnOrder>>4)&0xf) & support) support = (PicoIn.autoRgnOrder>>4)&0xf;\r
146 else if (((PicoIn.autoRgnOrder>>8)&0xf) & support) support = (PicoIn.autoRgnOrder>>8)&0xf;\r
51a902ae 147 }\r
148\r
cc68a136 149 // Try to pick the best hardware value for English/50hz:\r
150 if (support&8) { hw=0xc0; pal=1; } // Europe\r
151 else if (support&4) hw=0x80; // USA\r
152 else if (support&2) { hw=0x40; pal=1; } // Japan PAL\r
153 else if (support&1) hw=0x00; // Japan NTSC\r
154 else hw=0x80; // USA\r
155\r
02ff0254 156 if (!(PicoIn.AHW & PAHW_MCD)) hw |= 0x20; // No disk attached\r
157\r
158 Pico.m.hardware=(unsigned char)hw; \r
cc68a136 159 Pico.m.pal=pal;\r
1e6b5e39 160}\r
161\r
162int PicoReset(void)\r
163{\r
2ec9bec5 164 if (Pico.romsize <= 0)\r
165 return 1;\r
1e6b5e39 166\r
12da51c2 167#if defined(CPU_CMP_R) || defined(CPU_CMP_W) || defined(DRC_CMP)\r
93f9619e 168 PicoIn.opt |= POPT_DIS_VDP_FIFO|POPT_DIS_IDLE_DET;\r
6d797957 169#endif\r
170\r
1e6b5e39 171 /* must call now, so that banking is reset, and correct vectors get fetched */\r
2ec9bec5 172 if (PicoResetHook)\r
173 PicoResetHook();\r
1e6b5e39 174\r
93f9619e 175 memset(&PicoIn.padInt, 0, sizeof(PicoIn.padInt));\r
2ec9bec5 176\r
db2b6d99 177 z80_reset();\r
93f9619e 178 if (PicoIn.AHW & PAHW_SMS) {\r
2ec9bec5 179 PicoResetMS();\r
180 return 0;\r
181 }\r
182\r
183 SekReset();\r
8e4e84c2 184 // ..but do not reset SekCycle* to not desync with addons\r
185\r
1e6b5e39 186 // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games).\r
93f9619e 187 SekSetRealTAS(PicoIn.AHW & PAHW_MCD);\r
1e6b5e39 188\r
1832075e 189 Pico.m.z80_bank68k = 0;\r
af37bca8 190 Pico.m.z80_reset = 1;\r
1832075e 191\r
1e6b5e39 192 PicoDetectRegion();\r
51d6248b 193\r
194 PicoVideoReset();\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
133006a9 199 Pico.t.m68c_line_start = Pico.t.m68c_aim;\r
17bd69ad 200 PicoVideoFIFOWrite(rand() & 0x1fff, 0, 0, PVS_CPURD);\r
1cb1584b 201\r
5ed2a20e 202 SekFinishIdleDet();\r
203\r
eb990fd6 204 if (PicoIn.opt & POPT_EN_32X)\r
205 PicoReset32x();\r
206\r
93f9619e 207 if (PicoIn.AHW & PAHW_MCD) {\r
1cb1584b 208 PicoResetMCD();\r
cc68a136 209 return 0;\r
210 }\r
5ed2a20e 211\r
212 // reinit, so that checksum checks pass\r
93f9619e 213 if (!(PicoIn.opt & POPT_DIS_IDLE_DET))\r
5ed2a20e 214 SekInitIdleDet();\r
cc68a136 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
88fd63ad 218 if ((Pico.sv.flags & SRF_EEPROM) || Pico.romsize <= Pico.sv.start)\r
45f2f245 219 Pico.m.sram_reg |= SRR_MAPPED;\r
cc68a136 220\r
88fd63ad 221 if (Pico.sv.flags & SRF_ENABLED)\r
222 elprintf(EL_STATUS, "sram: %06x - %06x; eeprom: %i", Pico.sv.start, Pico.sv.end,\r
223 !!(Pico.sv.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 229void PicoLoopPrepare(void)\r
230{\r
93f9619e 231 if (PicoIn.regionOverride)\r
5e128c6d 232 // force setting possibly changed..\r
93f9619e 233 Pico.m.pal = (PicoIn.regionOverride == 2 || PicoIn.regionOverride == 8) ? 1 : 0;\r
5e128c6d 234\r
70216221 235 if (Pico.m.pal) {\r
236 Pico.t.vcnt_wrap = 0x103;\r
237 Pico.t.vcnt_adj = 57;\r
238 }\r
239 else {\r
240 Pico.t.vcnt_wrap = 0xEB;\r
241 Pico.t.vcnt_adj = 6;\r
242 }\r
c5ecd7a0 243 PicoVideoFIFOMode(Pico.video.reg[1]&0x40, Pico.video.reg[12]&1);\r
70216221 244\r
2446536b 245 Pico.m.dirtyPal = 1;\r
246 rendstatus_old = -1;\r
3d7abd69 247\r
7263343d 248 if (PicoIn.AHW & PAHW_MCD)\r
249 PicoMCDPrepare();\r
3d7abd69 250 if (PicoIn.AHW & PAHW_32X)\r
251 Pico32xPrepare();\r
5e128c6d 252}\r
253\r
efcba75f 254#include "pico_cmn.c"\r
4b9c5888 255\r
4b9c5888 256/* sync z80 to 68k */\r
ae214f1c 257PICO_INTERNAL void PicoSyncZ80(unsigned int m68k_cycles_done)\r
cc68a136 258{\r
a6523294 259 int m68k_cnt;\r
4b9c5888 260 int cnt;\r
a6523294 261\r
88fd63ad 262 m68k_cnt = m68k_cycles_done - Pico.t.m68c_frame_start;\r
263 Pico.t.z80c_aim = cycles_68k_to_z80(m68k_cnt);\r
264 cnt = Pico.t.z80c_aim - Pico.t.z80c_cnt;\r
cc68a136 265\r
f6c49d38 266 pprof_start(z80);\r
267\r
ae214f1c 268 elprintf(EL_BUSREQ, "z80 sync %i (%u|%u -> %u|%u)", cnt,\r
88fd63ad 269 Pico.t.z80c_cnt, Pico.t.z80c_cnt * 15 / 7 / 488,\r
270 Pico.t.z80c_aim, Pico.t.z80c_aim * 15 / 7 / 488);\r
4b9c5888 271\r
272 if (cnt > 0)\r
88fd63ad 273 Pico.t.z80c_cnt += z80_run(cnt);\r
f6c49d38 274\r
275 pprof_end(z80);\r
cc68a136 276}\r
277\r
4b9c5888 278\r
2aa27095 279void PicoFrame(void)\r
cc68a136 280{\r
f6c49d38 281 pprof_start(frame);\r
282\r
8c1952f0 283 Pico.m.frame_count++;\r
284\r
93f9619e 285 if (PicoIn.AHW & PAHW_SMS) {\r
19954be1 286 PicoFrameMS();\r
f6c49d38 287 goto end;\r
cc68a136 288 }\r
19954be1 289\r
93f9619e 290 if (PicoIn.AHW & PAHW_32X) {\r
fa8fb754 291 PicoFrame32x(); // also does MCD+32X\r
f6c49d38 292 goto end;\r
3e49ffd0 293 }\r
cc68a136 294\r
93f9619e 295 if (PicoIn.AHW & PAHW_MCD) {\r
fa8fb754 296 PicoFrameMCD();\r
f6c49d38 297 goto end;\r
974fdb5b 298 }\r
299\r
17bd69ad 300 //if(Pico.video.reg[12]&0x2) Pico.video.status ^= SR_ODD; // change odd bit in interlace mode\r
cc68a136 301\r
19954be1 302 PicoFrameStart();\r
2aa27095 303 PicoFrameHints();\r
f6c49d38 304\r
305end:\r
306 pprof_end(frame);\r
cc68a136 307}\r
308\r
a12e0116 309void PicoFrameDrawOnly(void)\r
310{\r
93f9619e 311 if (!(PicoIn.AHW & PAHW_SMS)) {\r
87b0845f 312 PicoFrameStart();\r
d515a352 313 PicoDrawSync(Pico.m.pal?239:223, 0, 0);\r
87b0845f 314 } else {\r
315 PicoFrameDrawOnlyMS();\r
316 }\r
a12e0116 317}\r
318\r
4609d0cd 319void PicoGetInternal(pint_t which, pint_ret_t *r)\r
8e5427a0 320{\r
321 switch (which)\r
322 {\r
4609d0cd 323 case PI_ROM: r->vptr = Pico.rom; break;\r
324 case PI_ISPAL: r->vint = Pico.m.pal; break;\r
325 case PI_IS40_CELL: r->vint = Pico.video.reg[12]&1; break;\r
326 case PI_IS240_LINES: r->vint = Pico.m.pal && (Pico.video.reg[1]&8); break;\r
8e5427a0 327 }\r
8e5427a0 328}\r
329\r
6311a3ba 330// vim:ts=2:sw=2:expandtab\r