some Pico work
[picodrive.git] / Pico / Pico.c
CommitLineData
cc68a136 1// This is part of Pico Library\r
2\r
3// (c) Copyright 2004 Dave, All rights reserved.\r
4// (c) Copyright 2006 notaz, All rights reserved.\r
5// Free for non-commercial use.\r
6\r
7// For commercial use, separate licencing terms must be obtained.\r
8\r
9\r
10#include "PicoInt.h"\r
cc68a136 11#include "sound/ym2612.h"\r
12\r
69996cb7 13int PicoVer=0x0133;\r
cc68a136 14struct Pico Pico;\r
602133e1 15int PicoOpt = 0;\r
16int PicoSkipFrame = 0; // skip rendering frame?\r
17int emustatus = 0; // rapid_ym2612, multi_ym_updates\r
18int PicoPad[2]; // Joypads, format is SACB RLDU\r
9037e45d 19int PicoAHW = 0; // active addon hardware: scd_active, 32x_active, svp_active, pico_active\r
cc68a136 20int PicoRegionOverride = 0; // override the region detection 0: Auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe\r
51a902ae 21int PicoAutoRgnOrder = 0;\r
602133e1 22int z80startCycle, z80stopCycle; // in 68k cycles\r
23struct PicoSRAM SRam = {0,};\r
24\r
f8ef8ff7 25void (*PicoWriteSound)(int len) = NULL; // called at the best time to send sound buffer (PsndOut) to hardware\r
26void (*PicoResetHook)(void) = NULL;\r
0ffefdb8 27void (*PicoLineHook)(int count) = NULL;\r
cc68a136 28\r
cc68a136 29// to be called once on emu init\r
30int PicoInit(void)\r
31{\r
32 // Blank space for state:\r
33 memset(&Pico,0,sizeof(Pico));\r
34 memset(&PicoPad,0,sizeof(PicoPad));\r
35\r
36 // Init CPUs:\r
37 SekInit();\r
38 z80_init(); // init even if we aren't going to use it\r
39\r
cc68a136 40 PicoInitMCD();\r
e807ac75 41 PicoSVPInit();\r
cc68a136 42\r
cc68a136 43 SRam.data=0;\r
cc68a136 44\r
45 return 0;\r
46}\r
47\r
48// to be called once on emu exit\r
49void PicoExit(void)\r
50{\r
602133e1 51 if (PicoAHW & PAHW_MCD)\r
4f265db7 52 PicoExitMCD();\r
cc68a136 53 z80_exit();\r
54\r
1cb1584b 55 if (SRam.data) free(SRam.data); SRam.data=0;\r
cc68a136 56}\r
57\r
1cb1584b 58void PicoPower(void)\r
59{\r
583ab72c 60 unsigned char sram_reg=Pico.m.sram_reg; // must be preserved\r
61\r
1cb1584b 62 // clear all memory of the emulated machine\r
63 memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram);\r
64\r
65 memset(&Pico.video,0,sizeof(Pico.video));\r
66 memset(&Pico.m,0,sizeof(Pico.m));\r
67\r
68 Pico.video.pending_ints=0;\r
69 z80_reset();\r
70\r
71 // default VDP register values (based on Fusion)\r
72 Pico.video.reg[0] = Pico.video.reg[1] = 0x04;\r
73 Pico.video.reg[0xc] = 0x81;\r
74 Pico.video.reg[0xf] = 0x02;\r
75\r
602133e1 76 if (PicoAHW & PAHW_MCD)\r
1cb1584b 77 PicoPowerMCD();\r
78\r
583ab72c 79 Pico.m.sram_reg=sram_reg;\r
1cb1584b 80 PicoReset();\r
81}\r
82\r
83int PicoReset(void)\r
cc68a136 84{\r
85 unsigned int region=0;\r
86 int support=0,hw=0,i=0;\r
87 unsigned char pal=0;\r
1dceadae 88 unsigned char sram_reg=Pico.m.sram_reg; // must be preserved\r
cc68a136 89\r
90 if (Pico.romsize<=0) return 1;\r
91\r
a12b1b29 92 /* must call now, so that banking is reset, and correct vectors get fetched */\r
93 if (PicoResetHook) PicoResetHook();\r
94\r
cc68a136 95 PicoMemReset();\r
96 SekReset();\r
c008977e 97 // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games).\r
602133e1 98 SekSetRealTAS(PicoAHW & PAHW_MCD);\r
cc68a136 99 SekCycleCntT=0;\r
cc68a136 100\r
602133e1 101 if (PicoAHW & PAHW_MCD)\r
1cb1584b 102 // needed for MCD to reset properly, probably some bug hides behind this..\r
103 memset(Pico.ioports,0,sizeof(Pico.ioports));\r
cc68a136 104 emustatus = 0;\r
105\r
cc68a136 106 Pico.m.dirtyPal = 1;\r
107\r
108 if(PicoRegionOverride)\r
109 {\r
110 support = PicoRegionOverride;\r
111 }\r
112 else\r
113 {\r
114 // Read cartridge region data:\r
115 region=PicoRead32(0x1f0);\r
116\r
117 for (i=0;i<4;i++)\r
118 {\r
119 int c=0;\r
120\r
121 c=region>>(i<<3); c&=0xff;\r
122 if (c<=' ') continue;\r
123\r
51a902ae 124 if (c=='J') support|=1;\r
125 else if (c=='U') support|=4;\r
126 else if (c=='E') support|=8;\r
127 else if (c=='j') {support|=1; break; }\r
128 else if (c=='u') {support|=4; break; }\r
129 else if (c=='e') {support|=8; break; }\r
cc68a136 130 else\r
131 {\r
132 // New style code:\r
133 char s[2]={0,0};\r
134 s[0]=(char)c;\r
135 support|=strtol(s,NULL,16);\r
136 }\r
137 }\r
138 }\r
139\r
51a902ae 140 // auto detection order override\r
141 if (PicoAutoRgnOrder) {\r
142 if (((PicoAutoRgnOrder>>0)&0xf) & support) support = (PicoAutoRgnOrder>>0)&0xf;\r
143 else if (((PicoAutoRgnOrder>>4)&0xf) & support) support = (PicoAutoRgnOrder>>4)&0xf;\r
144 else if (((PicoAutoRgnOrder>>8)&0xf) & support) support = (PicoAutoRgnOrder>>8)&0xf;\r
145 }\r
146\r
cc68a136 147 // Try to pick the best hardware value for English/50hz:\r
148 if (support&8) { hw=0xc0; pal=1; } // Europe\r
149 else if (support&4) hw=0x80; // USA\r
150 else if (support&2) { hw=0x40; pal=1; } // Japan PAL\r
151 else if (support&1) hw=0x00; // Japan NTSC\r
152 else hw=0x80; // USA\r
153\r
154 Pico.m.hardware=(unsigned char)(hw|0x20); // No disk attached\r
155 Pico.m.pal=pal;\r
1cb1584b 156 Pico.video.status = 0x3408 | pal; // 'always set' bits | vblank | pal\r
cc68a136 157\r
9d917eea 158 PsndReset(); // pal must be known here\r
cc68a136 159\r
1cb1584b 160 // create an empty "dma" to cause 68k exec start at random frame location\r
602133e1 161 if (Pico.m.dma_xfers == 0 && !(PicoOpt&POPT_DIS_VDP_FIFO))\r
1cb1584b 162 Pico.m.dma_xfers = rand() & 0x1fff;\r
163\r
602133e1 164 if (PicoAHW & PAHW_MCD) {\r
1cb1584b 165 PicoResetMCD();\r
cc68a136 166 return 0;\r
167 }\r
168\r
1dceadae 169 // reset sram state; enable sram access by default if it doesn't overlap with ROM\r
170 Pico.m.sram_reg=sram_reg&0x14;\r
171 if (!(Pico.m.sram_reg&4) && Pico.romsize <= SRam.start) Pico.m.sram_reg |= 1;\r
cc68a136 172\r
1dceadae 173 elprintf(EL_STATUS, "sram: det: %i; eeprom: %i; start: %06x; end: %06x",\r
174 (Pico.m.sram_reg>>4)&1, (Pico.m.sram_reg>>2)&1, SRam.start, SRam.end);\r
cc68a136 175\r
176 return 0;\r
177}\r
178\r
1dceadae 179\r
69996cb7 180// dma2vram settings are just hacks to unglitch Legend of Galahad (needs <= 104 to work)\r
181// same for Outrunners (92-121, when active is set to 24)\r
48df6e9e 182// 96 is VR hack\r
69996cb7 183static const int dma_timings[] = {\r
48df6e9e 18496, 167, 166, 83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy\r
4f672280 185102, 205, 204, 102, // vblank: 40cell:\r
69996cb7 18616, 16, 15, 8, // active: 32cell:\r
18724, 18, 17, 9 // ...\r
4f672280 188};\r
189\r
69996cb7 190static const int dma_bsycles[] = {\r
48df6e9e 191(488<<8)/96, (488<<8)/167, (488<<8)/166, (488<<8)/83,\r
312e9ce1 192(488<<8)/102, (488<<8)/205, (488<<8)/204, (488<<8)/102,\r
69996cb7 193(488<<8)/16, (488<<8)/16, (488<<8)/15, (488<<8)/8,\r
194(488<<8)/24, (488<<8)/18, (488<<8)/17, (488<<8)/9\r
312e9ce1 195};\r
196\r
eff55556 197PICO_INTERNAL int CheckDMA(void)\r
4f672280 198{\r
69996cb7 199 int burn = 0, xfers_can, dma_op = Pico.video.reg[0x17]>>6; // see gens for 00 and 01 modes\r
200 int xfers = Pico.m.dma_xfers;\r
312e9ce1 201 int dma_op1;\r
4f672280 202\r
312e9ce1 203 if(!(dma_op&2)) dma_op = (Pico.video.type==1) ? 0 : 1; // setting dma_timings offset here according to Gens\r
204 dma_op1 = dma_op;\r
205 if(Pico.video.reg[12] & 1) dma_op |= 4; // 40 cell mode?\r
206 if(!(Pico.video.status&8)&&(Pico.video.reg[1]&0x40)) dma_op|=8; // active display?\r
69996cb7 207 xfers_can = dma_timings[dma_op];\r
208 if(xfers <= xfers_can) {\r
4f672280 209 if(dma_op&2) Pico.video.status&=~2; // dma no longer busy\r
210 else {\r
69996cb7 211 burn = xfers * dma_bsycles[dma_op] >> 8; // have to be approximate because can't afford division..\r
4f672280 212 }\r
69996cb7 213 Pico.m.dma_xfers = 0;\r
4f672280 214 } else {\r
215 if(!(dma_op&2)) burn = 488;\r
69996cb7 216 Pico.m.dma_xfers -= xfers_can;\r
4f672280 217 }\r
218\r
69996cb7 219 elprintf(EL_VDPDMA, "~Dma %i op=%i can=%i burn=%i [%i]", Pico.m.dma_xfers, dma_op1, xfers_can, burn, SekCyclesDone());\r
312e9ce1 220 //dprintf("~aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);\r
221 return burn;\r
4f672280 222}\r
223\r
bf5fbbb4 224static __inline void SekRunM68k(int cyc)\r
cc68a136 225{\r
226 int cyc_do;\r
227 SekCycleAim+=cyc;\r
4f672280 228 if((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;\r
03e4f2a3 229#if defined(EMU_CORE_DEBUG)\r
230 // this means we do run-compare\r
b5e5172d 231 SekCycleCnt+=CM_compareRun(cyc_do, 0);\r
cc68a136 232#elif defined(EMU_C68K)\r
3aa1e148 233 PicoCpuCM68k.cycles=cyc_do;\r
234 CycloneRun(&PicoCpuCM68k);\r
235 SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;\r
cc68a136 236#elif defined(EMU_M68K)\r
237 SekCycleCnt+=m68k_execute(cyc_do);\r
70357ce5 238#elif defined(EMU_F68K)\r
8022f53d 239 SekCycleCnt+=fm68k_emulate(cyc_do+1, 0);\r
cc68a136 240#endif\r
241}\r
242\r
243static __inline void SekStep(void)\r
244{\r
245 // this is required for timing sensitive stuff to work\r
246 int realaim=SekCycleAim; SekCycleAim=SekCycleCnt+1;\r
03e4f2a3 247#if defined(EMU_CORE_DEBUG)\r
b5e5172d 248 SekCycleCnt+=CM_compareRun(1, 0);\r
cc68a136 249#elif defined(EMU_C68K)\r
3aa1e148 250 PicoCpuCM68k.cycles=1;\r
251 CycloneRun(&PicoCpuCM68k);\r
252 SekCycleCnt+=1-PicoCpuCM68k.cycles;\r
cc68a136 253#elif defined(EMU_M68K)\r
254 SekCycleCnt+=m68k_execute(1);\r
70357ce5 255#elif defined(EMU_F68K)\r
8022f53d 256 SekCycleCnt+=fm68k_emulate(1, 0);\r
cc68a136 257#endif\r
258 SekCycleAim=realaim;\r
259}\r
260\r
261static int CheckIdle(void)\r
262{\r
fad24893 263 int i, state[0x24];\r
cc68a136 264\r
265 // See if the state is the same after 2 steps:\r
fad24893 266 SekState(state); SekStep(); SekStep(); SekState(state+0x12);\r
267 for (i = 0x11; i >= 0; i--)\r
268 if (state[i] != state[i+0x12]) return 0;\r
cc68a136 269\r
3aa1e148 270 return 1;\r
cc68a136 271}\r
272\r
fa283c9a 273\r
cc68a136 274// to be called on 224 or line_sample scanlines only\r
275static __inline void getSamples(int y)\r
276{\r
03a265e5 277#if SIMPLE_WRITE_SOUND\r
e0978fa8 278 if (y != 224) return;\r
03a265e5 279 PsndRender(0, PsndLen);\r
280 if (PicoWriteSound) PicoWriteSound(PsndLen);\r
281 PsndClear();\r
282#else\r
7a93adeb 283 static int curr_pos = 0;\r
284\r
cc68a136 285 if(y == 224) {\r
cc68a136 286 if(emustatus & 2)\r
9d917eea 287 curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
288 else curr_pos = PsndRender(0, PsndLen);\r
cc68a136 289 if (emustatus&1) emustatus|=2; else emustatus&=~2;\r
7a93adeb 290 if (PicoWriteSound) PicoWriteSound(curr_pos);\r
cc68a136 291 // clear sound buffer\r
9d917eea 292 PsndClear();\r
cc68a136 293 }\r
294 else if(emustatus & 3) {\r
295 emustatus|= 2;\r
296 emustatus&=~1;\r
9d917eea 297 curr_pos = PsndRender(0, PsndLen/2);\r
cc68a136 298 }\r
03a265e5 299#endif\r
cc68a136 300}\r
301\r
69996cb7 302\r
69996cb7 303#include "PicoFrameHints.c"\r
cc68a136 304\r
9a8ffeee 305// helper z80 runner. Runs only if z80 is enabled at this point\r
306// (z80WriteBusReq will handle the rest)\r
cc68a136 307static void PicoRunZ80Simple(int line_from, int line_to)\r
308{\r
9a8ffeee 309 int line_from_r=line_from, line_to_r=line_to, line=0;\r
cc68a136 310 int line_sample = Pico.m.pal ? 68 : 93;\r
cc68a136 311\r
602133e1 312 if (!(PicoOpt&POPT_EN_Z80) || Pico.m.z80Run == 0) line_to_r = 0;\r
9a8ffeee 313 else {\r
314 extern const unsigned short vcounts[];\r
315 if (z80startCycle) {\r
316 line = vcounts[z80startCycle>>8];\r
317 if (line > line_from)\r
318 line_from_r = line;\r
319 }\r
320 z80startCycle = SekCyclesDone();\r
321 }\r
cc68a136 322\r
602133e1 323 if (PicoOpt&POPT_EN_FM) {\r
cc68a136 324 // we have ym2612 enabled, so we have to run Z80 in lines, so we could update DAC and timers\r
9a8ffeee 325 for (line = line_from; line < line_to; line++) {\r
9d917eea 326 Psnd_timers_and_dac(line);\r
9a8ffeee 327 if ((line == 224 || line == line_sample) && PsndOut) getSamples(line);\r
328 if (line == 32 && PsndOut) emustatus &= ~1;\r
329 if (line >= line_from_r && line < line_to_r)\r
b542be46 330 z80_run_nr(228);\r
cc68a136 331 }\r
9a8ffeee 332 } else if (line_to_r-line_from_r > 0) {\r
b542be46 333 z80_run_nr(228*(line_to_r-line_from_r));\r
cc68a136 334 // samples will be taken by caller\r
335 }\r
336}\r
337\r
338// Simple frame without H-Ints\r
339static int PicoFrameSimple(void)\r
340{\r
341 struct PicoVideo *pv=&Pico.video;\r
342 int y=0,line=0,lines=0,lines_step=0,sects;\r
343 int cycles_68k_vblock,cycles_68k_block;\r
344\r
e849512f 345 // split to 16 run calls for active scan, for vblank split to 2 (ntsc), 3 (pal 240), 4 (pal 224)\r
8ab3e3c1 346 if (Pico.m.pal && (pv->reg[1]&8)) {\r
cc68a136 347 if(pv->reg[1]&8) { // 240 lines\r
e849512f 348 cycles_68k_block = 7329; // (488*240+148)/16.0, -4\r
349 cycles_68k_vblock = 11640; // (72*488-148-68)/3.0, 0\r
cc68a136 350 lines_step = 15;\r
351 } else {\r
e849512f 352 cycles_68k_block = 6841; // (488*224+148)/16.0, -4\r
353 cycles_68k_vblock = 10682; // (88*488-148-68)/4.0, 0\r
cc68a136 354 lines_step = 14;\r
355 }\r
356 } else {\r
357 // M68k cycles/frame: 127840.71\r
e849512f 358 cycles_68k_block = 6841; // (488*224+148)/16.0, -4\r
359 cycles_68k_vblock = 9164; // (38*488-148-68)/2.0, 0\r
cc68a136 360 lines_step = 14;\r
361 }\r
362\r
fad24893 363 // a hack for VR, to get it running in fast mode\r
602133e1 364 if (PicoAHW & PAHW_SVP)\r
fad24893 365 Pico.ram[0xd864^1] = 0x1a;\r
366\r
69996cb7 367 // we don't emulate DMA timing in this mode\r
368 if (Pico.m.dma_xfers) {\r
369 Pico.m.dma_xfers=0;\r
370 Pico.video.status&=~2;\r
371 }\r
372\r
373 // VDP FIFO too\r
374 pv->lwrite_cnt = 0;\r
375 Pico.video.status|=0x200;\r
376\r
cc68a136 377 Pico.m.scanline=-1;\r
9a8ffeee 378 z80startCycle=0;\r
cc68a136 379\r
380 SekCyclesReset();\r
381\r
cc68a136 382 // 6 button pad: let's just say it timed out now\r
383 Pico.m.padTHPhase[0]=Pico.m.padTHPhase[1]=0;\r
384\r
385 // ---- Active Scan ----\r
69996cb7 386 pv->status&=~0x88; // clear V-Int, come out of vblank\r
cc68a136 387\r
388 // Run in sections:\r
389 for(sects=16; sects; sects--)\r
390 {\r
391 if (CheckIdle()) break;\r
392\r
393 lines += lines_step;\r
bf5fbbb4 394 SekRunM68k(cycles_68k_block);\r
cc68a136 395\r
396 PicoRunZ80Simple(line, lines);\r
0ffefdb8 397 if (PicoLineHook) PicoLineHook(lines_step);\r
cc68a136 398 line=lines;\r
399 }\r
400\r
401 // run Z80 for remaining sections\r
402 if(sects) {\r
403 int c = sects*cycles_68k_block;\r
404\r
9a8ffeee 405 // this "run" is for approriate line counter, etc\r
cc68a136 406 SekCycleCnt += c;\r
407 SekCycleAim += c;\r
9a8ffeee 408\r
409 lines += sects*lines_step;\r
410 PicoRunZ80Simple(line, lines);\r
0ffefdb8 411 if (PicoLineHook) PicoLineHook(sects*lines_step);\r
cc68a136 412 }\r
413\r
fad24893 414 // another hack for VR (it needs hints to work)\r
602133e1 415 if (PicoAHW & PAHW_SVP) {\r
fad24893 416 Pico.ram[0xd864^1] = 1;\r
417 pv->pending_ints|=0x10;\r
418 if (pv->reg[0]&0x10) SekInterrupt(4);\r
419 SekRunM68k(160);\r
420 }\r
421\r
cc68a136 422 // render screen\r
e849512f 423 if (!PicoSkipFrame)\r
424 {\r
602133e1 425 if (!(PicoOpt&POPT_ALT_RENDERER))\r
fad24893 426 {\r
cc68a136 427 // Draw the screen\r
428#if CAN_HANDLE_240_LINES\r
e849512f 429 if (pv->reg[1]&8) {\r
cc68a136 430 for (y=0;y<240;y++) PicoLine(y);\r
431 } else {\r
432 for (y=0;y<224;y++) PicoLine(y);\r
433 }\r
434#else\r
435 for (y=0;y<224;y++) PicoLine(y);\r
436#endif\r
fad24893 437 }\r
cc68a136 438 else PicoFrameFull();\r
8ab3e3c1 439#ifdef DRAW_FINISH_FUNC\r
440 DRAW_FINISH_FUNC();\r
441#endif\r
442 }\r
443\r
444 // here we render sound if ym2612 is disabled\r
602133e1 445 if (!(PicoOpt&POPT_EN_FM) && PsndOut) {\r
9d917eea 446 int len = PsndRender(0, PsndLen);\r
8ab3e3c1 447 if (PicoWriteSound) PicoWriteSound(len);\r
448 // clear sound buffer\r
9d917eea 449 PsndClear();\r
cc68a136 450 }\r
451\r
e849512f 452 // a gap between flags set and vint\r
453 pv->pending_ints|=0x20;\r
454 pv->status|=8; // go into vblank\r
bf5fbbb4 455 SekRunM68k(68+4);\r
e849512f 456\r
cc68a136 457 // ---- V-Blanking period ----\r
458 // fix line counts\r
459 if(Pico.m.pal) {\r
460 if(pv->reg[1]&8) { // 240 lines\r
461 lines = line = 240;\r
462 sects = 3;\r
463 lines_step = 24;\r
464 } else {\r
465 lines = line = 224;\r
466 sects = 4;\r
467 lines_step = 22;\r
468 }\r
469 } else {\r
470 lines = line = 224;\r
471 sects = 2;\r
472 lines_step = 19;\r
473 }\r
474\r
cc68a136 475 if (pv->reg[1]&0x20) SekInterrupt(6); // Set IRQ\r
602133e1 476 if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))\r
cc68a136 477 z80_int();\r
478\r
602133e1 479 while (sects)\r
480 {\r
cc68a136 481 lines += lines_step;\r
482\r
bf5fbbb4 483 SekRunM68k(cycles_68k_vblock);\r
cc68a136 484\r
485 PicoRunZ80Simple(line, lines);\r
0ffefdb8 486 if (PicoLineHook) PicoLineHook(lines_step);\r
cc68a136 487 line=lines;\r
488\r
489 sects--;\r
e849512f 490 if (sects && CheckIdle()) break;\r
cc68a136 491 }\r
492\r
493 // run Z80 for remaining sections\r
e849512f 494 if (sects) {\r
cc68a136 495 lines += sects*lines_step;\r
496 PicoRunZ80Simple(line, lines);\r
0ffefdb8 497 if (PicoLineHook) PicoLineHook(sects*lines_step);\r
cc68a136 498 }\r
499\r
500 return 0;\r
501}\r
502\r
503int PicoFrame(void)\r
504{\r
505 int acc;\r
506\r
8c1952f0 507 Pico.m.frame_count++;\r
508\r
602133e1 509 if (PicoAHW & PAHW_MCD) {\r
cc68a136 510 PicoFrameMCD();\r
511 return 0;\r
512 }\r
513\r
514 // be accurate if we are asked for this\r
602133e1 515 if (PicoOpt&POPT_ACC_TIMING) acc=1;\r
cc68a136 516 // don't be accurate in alternative render mode, as hint effects will not be rendered anyway\r
602133e1 517 else if (PicoOpt&POPT_ALT_RENDERER) acc = 0;\r
cc68a136 518 else acc=Pico.video.reg[0]&0x10; // be accurate if hints are used\r
519\r
520 //if(Pico.video.reg[12]&0x2) Pico.video.status ^= 0x10; // change odd bit in interlace mode\r
521\r
602133e1 522 if (!(PicoOpt&POPT_ALT_RENDERER))\r
cc68a136 523 PicoFrameStart();\r
524\r
602133e1 525 if (acc)\r
cc68a136 526 PicoFrameHints();\r
527 else PicoFrameSimple();\r
528\r
529 return 0;\r
530}\r
531\r
a12e0116 532void PicoFrameDrawOnly(void)\r
533{\r
534 int y;\r
535 PicoFrameStart();\r
536 for (y=0;y<224;y++) PicoLine(y);\r
537}\r
538\r
8e5427a0 539int PicoGetStat(pstat_t which)\r
540{\r
541 switch (which)\r
542 {\r
543 case PS_PAL: return Pico.m.pal;\r
544 case PS_40_CELL: return Pico.video.reg[12]&1;\r
545 case PS_240_LINES: return Pico.m.pal && (Pico.video.reg[1]&8);\r
546 }\r
547 return 0;\r
548}\r
549\r
66fdc0f0 550// callback to output message from emu\r
551void (*PicoMessage)(const char *msg)=NULL;\r
cc68a136 552\r
1820b5a7 553#if 1 // defined(__DEBUG_PRINT)\r
cc68a136 554// tmp debug: dump some stuff\r
555#define bit(r, x) ((r>>x)&1)\r
556void z80_debug(char *dstr);\r
0af33fe0 557char *debugString(void)\r
cc68a136 558{\r
559#if 1\r
560 static char dstr[1024];\r
0af33fe0 561 struct PicoVideo *pv=&Pico.video;\r
562 unsigned char *reg=pv->reg, r;\r
563 char *dstrp;\r
564\r
565 dstrp = dstr;\r
566 sprintf(dstrp, "mode set 1: %02x\n", (r=reg[0])); dstrp+=strlen(dstrp);\r
567 sprintf(dstrp, "display_disable: %i, M3: %i, palette: %i, ?, hints: %i\n", bit(r,0), bit(r,1), bit(r,2), bit(r,4));\r
568 dstrp+=strlen(dstrp);\r
569 sprintf(dstrp, "mode set 2: %02x\n", (r=reg[1])); dstrp+=strlen(dstrp);\r
570 sprintf(dstrp, "SMS/gen: %i, pal: %i, dma: %i, vints: %i, disp: %i, TMS: %i\n", bit(r,2), bit(r,3), bit(r,4),\r
571 bit(r,5), bit(r,6), bit(r,7)); dstrp+=strlen(dstrp);\r
572 sprintf(dstrp, "mode set 3: %02x\n", (r=reg[0xB])); dstrp+=strlen(dstrp);\r
573 sprintf(dstrp, "LSCR: %i, HSCR: %i, 2cell vscroll: %i, IE2: %i\n", bit(r,0), bit(r,1), bit(r,2), bit(r,3)); dstrp+=strlen(dstrp);\r
574 sprintf(dstrp, "mode set 4: %02x\n", (r=reg[0xC])); dstrp+=strlen(dstrp);\r
575 sprintf(dstrp, "interlace: %i%i, cells: %i, shadow: %i\n", bit(r,2), bit(r,1), (r&0x80) ? 40 : 32, bit(r,3));\r
576 dstrp+=strlen(dstrp);\r
1dceadae 577 sprintf(dstrp, "scroll size: w: %i, h: %i SRAM: %i; eeprom: %i (%i)\n", reg[0x10]&3, (reg[0x10]&0x30)>>4,\r
578 bit(Pico.m.sram_reg, 4), bit(Pico.m.sram_reg, 2), SRam.eeprom_type); dstrp+=strlen(dstrp);\r
7969166e 579 sprintf(dstrp, "sram range: %06x-%06x, reg: %02x\n", SRam.start, SRam.end, Pico.m.sram_reg); dstrp+=strlen(dstrp);\r
0af33fe0 580 sprintf(dstrp, "pend int: v:%i, h:%i, vdp status: %04x\n", bit(pv->pending_ints,5), bit(pv->pending_ints,4), pv->status);\r
581 dstrp+=strlen(dstrp);\r
7d4906bf 582#if defined(EMU_C68K)\r
3aa1e148 583 sprintf(dstrp, "M68k: PC: %06x, st_flg: %x, cycles: %u\n", SekPc, PicoCpuCM68k.state_flags, SekCyclesDoneT());\r
0af33fe0 584 dstrp+=strlen(dstrp);\r
3aa1e148 585 sprintf(dstrp, "d0=%08x, a0=%08x, osp=%08x, irql=%i\n", PicoCpuCM68k.d[0], PicoCpuCM68k.a[0], PicoCpuCM68k.osp, PicoCpuCM68k.irq); dstrp+=strlen(dstrp);\r
586 sprintf(dstrp, "d1=%08x, a1=%08x, sr=%04x\n", PicoCpuCM68k.d[1], PicoCpuCM68k.a[1], CycloneGetSr(&PicoCpuCM68k)); dstrp+=strlen(dstrp);\r
0af33fe0 587 for(r=2; r < 8; r++) {\r
3aa1e148 588 sprintf(dstrp, "d%i=%08x, a%i=%08x\n", r, PicoCpuCM68k.d[r], r, PicoCpuCM68k.a[r]); dstrp+=strlen(dstrp);\r
0af33fe0 589 }\r
7d4906bf 590#elif defined(EMU_M68K)\r
3aa1e148 591 sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuMM68k.int_level>>8); dstrp+=strlen(dstrp);\r
70357ce5 592#elif defined(EMU_F68K)\r
3aa1e148 593 sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuFM68k.interrupts[0]); dstrp+=strlen(dstrp);\r
cc68a136 594#endif\r
0af33fe0 595 sprintf(dstrp, "z80Run: %i, pal: %i, frame#: %i\n", Pico.m.z80Run, Pico.m.pal, Pico.m.frame_count); dstrp+=strlen(dstrp);\r
596 z80_debug(dstrp); dstrp+=strlen(dstrp);\r
597 if (strlen(dstr) > sizeof(dstr))\r
598 printf("warning: debug buffer overflow (%i/%i)\n", strlen(dstr), sizeof(dstr));\r
cc68a136 599\r
600#else\r
601 struct PicoVideo *pvid=&Pico.video;\r
602 int table=0;\r
603 int i,u,n,link=0;\r
604 static char dstr[1024*8];\r
605 dstr[0] = 0;\r
606\r
607 table=pvid->reg[5]&0x7f;\r
608 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
609 table<<=8; // Get sprite table address/2\r
610\r
611 for (i=u=n=0; u < 80 && n < 20; u++)\r
612 {\r
613 unsigned int *sprite;\r
614 int code, code2, sx, sy, height;\r
615\r
616 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
617\r
618 // get sprite info\r
619 code = sprite[0];\r
620\r
621 // check if it is on this line\r
622 sy = (code&0x1ff);//-0x80;\r
623 height = ((code>>24)&3)+1;\r
624\r
625 // masking sprite?\r
626 code2 = sprite[1];\r
627 sx = (code2>>16)&0x1ff;\r
628\r
69996cb7 629 printf("#%02i x: %03i y: %03i %ix%i\n", u, sx, sy, ((code>>26)&3)+1, height);\r
cc68a136 630\r
631 link=(code>>16)&0x7f;\r
632 if(!link) break; // End of sprites\r
633 }\r
634#endif\r
635\r
cc68a136 636 return dstr;\r
637}\r
638#endif\r