.cue support, Pico stubs
[picodrive.git] / Pico / Pico.c
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
11 #include "sound/ym2612.h"\r
12 \r
13 int PicoVer=0x0133;\r
14 struct Pico Pico;\r
15 int PicoOpt = 0;\r
16 int PicoSkipFrame = 0; // skip rendering frame?\r
17 int emustatus = 0;     // rapid_ym2612, multi_ym_updates\r
18 int PicoPad[2];        // Joypads, format is SACB RLDU\r
19 int PicoAHW = 0;       // active addon hardware: scd_active, 32x_active, svp_active, pico_active\r
20 int PicoRegionOverride = 0; // override the region detection 0: Auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe\r
21 int PicoAutoRgnOrder = 0;\r
22 int z80startCycle, z80stopCycle; // in 68k cycles\r
23 struct PicoSRAM SRam = {0,};\r
24 \r
25 void (*PicoWriteSound)(int len) = NULL; // called at the best time to send sound buffer (PsndOut) to hardware\r
26 void (*PicoResetHook)(void) = NULL;\r
27 void (*PicoLineHook)(int count) = NULL;\r
28 \r
29 // to be called once on emu init\r
30 int 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
40   PicoInitMCD();\r
41   PicoSVPInit();\r
42 \r
43   SRam.data=0;\r
44 \r
45   return 0;\r
46 }\r
47 \r
48 // to be called once on emu exit\r
49 void PicoExit(void)\r
50 {\r
51   if (PicoAHW & PAHW_MCD)\r
52     PicoExitMCD();\r
53   z80_exit();\r
54 \r
55   if (SRam.data) free(SRam.data); SRam.data=0;\r
56 }\r
57 \r
58 void PicoPower(void)\r
59 {\r
60   unsigned char sram_reg=Pico.m.sram_reg; // must be preserved\r
61 \r
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
76   if (PicoAHW & PAHW_MCD)\r
77     PicoPowerMCD();\r
78 \r
79   Pico.m.sram_reg=sram_reg;\r
80   PicoReset();\r
81 }\r
82 \r
83 int PicoReset(void)\r
84 {\r
85   unsigned int region=0;\r
86   int support=0,hw=0,i=0;\r
87   unsigned char pal=0;\r
88   unsigned char sram_reg=Pico.m.sram_reg; // must be preserved\r
89 \r
90   if (Pico.romsize<=0) return 1;\r
91 \r
92   /* must call now, so that banking is reset, and correct vectors get fetched */\r
93   if (PicoResetHook) PicoResetHook();\r
94 \r
95   PicoMemReset();\r
96   SekReset();\r
97   // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games).\r
98   SekSetRealTAS(PicoAHW & PAHW_MCD);\r
99   SekCycleCntT=0;\r
100 \r
101   if (PicoAHW & PAHW_MCD)\r
102     // needed for MCD to reset properly, probably some bug hides behind this..\r
103     memset(Pico.ioports,0,sizeof(Pico.ioports));\r
104   emustatus = 0;\r
105 \r
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
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
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
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
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
156   Pico.video.status = 0x3408 | pal; // 'always set' bits | vblank | pal\r
157 \r
158   PsndReset(); // pal must be known here\r
159 \r
160   // create an empty "dma" to cause 68k exec start at random frame location\r
161   if (Pico.m.dma_xfers == 0 && !(PicoOpt&POPT_DIS_VDP_FIFO))\r
162     Pico.m.dma_xfers = rand() & 0x1fff;\r
163 \r
164   if (PicoAHW & PAHW_MCD) {\r
165     PicoResetMCD();\r
166     return 0;\r
167   }\r
168 \r
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
172 \r
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
175 \r
176   return 0;\r
177 }\r
178 \r
179 \r
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
182 // 96 is VR hack\r
183 static const int dma_timings[] = {\r
184 96,  167, 166,  83, // vblank: 32cell: dma2vram dma2[vs|c]ram vram_fill vram_copy\r
185 102, 205, 204, 102, // vblank: 40cell:\r
186 16,   16,  15,   8, // active: 32cell:\r
187 24,   18,  17,   9  // ...\r
188 };\r
189 \r
190 static const int dma_bsycles[] = {\r
191 (488<<8)/96,  (488<<8)/167, (488<<8)/166, (488<<8)/83,\r
192 (488<<8)/102, (488<<8)/205, (488<<8)/204, (488<<8)/102,\r
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
195 };\r
196 \r
197 PICO_INTERNAL int CheckDMA(void)\r
198 {\r
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
201   int dma_op1;\r
202 \r
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
207   xfers_can = dma_timings[dma_op];\r
208   if(xfers <= xfers_can) {\r
209     if(dma_op&2) Pico.video.status&=~2; // dma no longer busy\r
210     else {\r
211       burn = xfers * dma_bsycles[dma_op] >> 8; // have to be approximate because can't afford division..\r
212     }\r
213     Pico.m.dma_xfers = 0;\r
214   } else {\r
215     if(!(dma_op&2)) burn = 488;\r
216     Pico.m.dma_xfers -= xfers_can;\r
217   }\r
218 \r
219   elprintf(EL_VDPDMA, "~Dma %i op=%i can=%i burn=%i [%i]", Pico.m.dma_xfers, dma_op1, xfers_can, burn, SekCyclesDone());\r
220   //dprintf("~aim: %i, cnt: %i", SekCycleAim, SekCycleCnt);\r
221   return burn;\r
222 }\r
223 \r
224 static __inline void SekRunM68k(int cyc)\r
225 {\r
226   int cyc_do;\r
227   SekCycleAim+=cyc;\r
228   if((cyc_do=SekCycleAim-SekCycleCnt) <= 0) return;\r
229 #if defined(EMU_CORE_DEBUG)\r
230   // this means we do run-compare\r
231   SekCycleCnt+=CM_compareRun(cyc_do, 0);\r
232 #elif defined(EMU_C68K)\r
233   PicoCpuCM68k.cycles=cyc_do;\r
234   CycloneRun(&PicoCpuCM68k);\r
235   SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;\r
236 #elif defined(EMU_M68K)\r
237   SekCycleCnt+=m68k_execute(cyc_do);\r
238 #elif defined(EMU_F68K)\r
239   SekCycleCnt+=fm68k_emulate(cyc_do+1, 0);\r
240 #endif\r
241 }\r
242 \r
243 static __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
247 #if defined(EMU_CORE_DEBUG)\r
248   SekCycleCnt+=CM_compareRun(1, 0);\r
249 #elif defined(EMU_C68K)\r
250   PicoCpuCM68k.cycles=1;\r
251   CycloneRun(&PicoCpuCM68k);\r
252   SekCycleCnt+=1-PicoCpuCM68k.cycles;\r
253 #elif defined(EMU_M68K)\r
254   SekCycleCnt+=m68k_execute(1);\r
255 #elif defined(EMU_F68K)\r
256   SekCycleCnt+=fm68k_emulate(1, 0);\r
257 #endif\r
258   SekCycleAim=realaim;\r
259 }\r
260 \r
261 static int CheckIdle(void)\r
262 {\r
263   int i, state[0x24];\r
264 \r
265   // See if the state is the same after 2 steps:\r
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
269 \r
270   return 1;\r
271 }\r
272 \r
273 \r
274 // to be called on 224 or line_sample scanlines only\r
275 static __inline void getSamples(int y)\r
276 {\r
277 #if SIMPLE_WRITE_SOUND\r
278   if (y != 224) return;\r
279   PsndRender(0, PsndLen);\r
280   if (PicoWriteSound) PicoWriteSound(PsndLen);\r
281   PsndClear();\r
282 #else\r
283   static int curr_pos = 0;\r
284 \r
285   if(y == 224) {\r
286     if(emustatus & 2)\r
287          curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
288     else curr_pos  = PsndRender(0, PsndLen);\r
289     if (emustatus&1) emustatus|=2; else emustatus&=~2;\r
290     if (PicoWriteSound) PicoWriteSound(curr_pos);\r
291     // clear sound buffer\r
292     PsndClear();\r
293   }\r
294   else if(emustatus & 3) {\r
295     emustatus|= 2;\r
296     emustatus&=~1;\r
297     curr_pos = PsndRender(0, PsndLen/2);\r
298   }\r
299 #endif\r
300 }\r
301 \r
302 \r
303 #include "PicoFrameHints.c"\r
304 \r
305 // helper z80 runner. Runs only if z80 is enabled at this point\r
306 // (z80WriteBusReq will handle the rest)\r
307 static void PicoRunZ80Simple(int line_from, int line_to)\r
308 {\r
309   int line_from_r=line_from, line_to_r=line_to, line=0;\r
310   int line_sample = Pico.m.pal ? 68 : 93;\r
311 \r
312   if (!(PicoOpt&POPT_EN_Z80) || Pico.m.z80Run == 0) line_to_r = 0;\r
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
322 \r
323   if (PicoOpt&POPT_EN_FM) {\r
324     // we have ym2612 enabled, so we have to run Z80 in lines, so we could update DAC and timers\r
325     for (line = line_from; line < line_to; line++) {\r
326       Psnd_timers_and_dac(line);\r
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
330         z80_run_nr(228);\r
331     }\r
332   } else if (line_to_r-line_from_r > 0) {\r
333     z80_run_nr(228*(line_to_r-line_from_r));\r
334     // samples will be taken by caller\r
335   }\r
336 }\r
337 \r
338 // Simple frame without H-Ints\r
339 static 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
345   // split to 16 run calls for active scan, for vblank split to 2 (ntsc), 3 (pal 240), 4 (pal 224)\r
346   if (Pico.m.pal && (pv->reg[1]&8)) {\r
347     if(pv->reg[1]&8) { // 240 lines\r
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
350       lines_step = 15;\r
351     } else {\r
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
354       lines_step = 14;\r
355     }\r
356   } else {\r
357     // M68k cycles/frame: 127840.71\r
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
360     lines_step = 14;\r
361   }\r
362 \r
363   // a hack for VR, to get it running in fast mode\r
364   if (PicoAHW & PAHW_SVP)\r
365     Pico.ram[0xd864^1] = 0x1a;\r
366 \r
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
377   Pico.m.scanline=-1;\r
378   z80startCycle=0;\r
379 \r
380   SekCyclesReset();\r
381 \r
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
386   pv->status&=~0x88; // clear V-Int, come out of vblank\r
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
394     SekRunM68k(cycles_68k_block);\r
395 \r
396     PicoRunZ80Simple(line, lines);\r
397     if (PicoLineHook) PicoLineHook(lines_step);\r
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
405     // this "run" is for approriate line counter, etc\r
406     SekCycleCnt += c;\r
407     SekCycleAim += c;\r
408 \r
409     lines += sects*lines_step;\r
410     PicoRunZ80Simple(line, lines);\r
411     if (PicoLineHook) PicoLineHook(sects*lines_step);\r
412   }\r
413 \r
414   // another hack for VR (it needs hints to work)\r
415   if (PicoAHW & PAHW_SVP) {\r
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
422   // render screen\r
423   if (!PicoSkipFrame)\r
424   {\r
425     if (!(PicoOpt&POPT_ALT_RENDERER))\r
426     {\r
427       // Draw the screen\r
428 #if CAN_HANDLE_240_LINES\r
429       if (pv->reg[1]&8) {\r
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
437     }\r
438     else PicoFrameFull();\r
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
445   if (!(PicoOpt&POPT_EN_FM) && PsndOut) {\r
446     int len = PsndRender(0, PsndLen);\r
447     if (PicoWriteSound) PicoWriteSound(len);\r
448     // clear sound buffer\r
449     PsndClear();\r
450   }\r
451 \r
452   // a gap between flags set and vint\r
453   pv->pending_ints|=0x20;\r
454   pv->status|=8; // go into vblank\r
455   SekRunM68k(68+4);\r
456 \r
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
475   if (pv->reg[1]&0x20) SekInterrupt(6); // Set IRQ\r
476   if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))\r
477     z80_int();\r
478 \r
479   while (sects)\r
480   {\r
481     lines += lines_step;\r
482 \r
483     SekRunM68k(cycles_68k_vblock);\r
484 \r
485     PicoRunZ80Simple(line, lines);\r
486     if (PicoLineHook) PicoLineHook(lines_step);\r
487     line=lines;\r
488 \r
489     sects--;\r
490     if (sects && CheckIdle()) break;\r
491   }\r
492 \r
493   // run Z80 for remaining sections\r
494   if (sects) {\r
495     lines += sects*lines_step;\r
496     PicoRunZ80Simple(line, lines);\r
497     if (PicoLineHook) PicoLineHook(sects*lines_step);\r
498   }\r
499 \r
500   return 0;\r
501 }\r
502 \r
503 int PicoFrame(void)\r
504 {\r
505   int acc;\r
506 \r
507   Pico.m.frame_count++;\r
508 \r
509   if (PicoAHW & PAHW_MCD) {\r
510     PicoFrameMCD();\r
511     return 0;\r
512   }\r
513 \r
514   // be accurate if we are asked for this\r
515   if (PicoOpt&POPT_ACC_TIMING) acc=1;\r
516   // don't be accurate in alternative render mode, as hint effects will not be rendered anyway\r
517   else if (PicoOpt&POPT_ALT_RENDERER) acc = 0;\r
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
522   if (!(PicoOpt&POPT_ALT_RENDERER))\r
523     PicoFrameStart();\r
524 \r
525   if (acc)\r
526        PicoFrameHints();\r
527   else PicoFrameSimple();\r
528 \r
529   return 0;\r
530 }\r
531 \r
532 void 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
539 int 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
550 // callback to output message from emu\r
551 void (*PicoMessage)(const char *msg)=NULL;\r
552 \r
553 #if 1 // defined(__DEBUG_PRINT)\r
554 // tmp debug: dump some stuff\r
555 #define bit(r, x) ((r>>x)&1)\r
556 void z80_debug(char *dstr);\r
557 char *debugString(void)\r
558 {\r
559 #if 1\r
560   static char dstr[1024];\r
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
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
579   sprintf(dstrp, "sram range: %06x-%06x, reg: %02x\n", SRam.start, SRam.end, Pico.m.sram_reg); dstrp+=strlen(dstrp);\r
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
582 #if defined(EMU_C68K)\r
583   sprintf(dstrp, "M68k: PC: %06x, st_flg: %x, cycles: %u\n", SekPc, PicoCpuCM68k.state_flags, SekCyclesDoneT());\r
584   dstrp+=strlen(dstrp);\r
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
587   for(r=2; r < 8; r++) {\r
588     sprintf(dstrp, "d%i=%08x, a%i=%08x\n", r, PicoCpuCM68k.d[r], r, PicoCpuCM68k.a[r]); dstrp+=strlen(dstrp);\r
589   }\r
590 #elif defined(EMU_M68K)\r
591   sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuMM68k.int_level>>8); dstrp+=strlen(dstrp);\r
592 #elif defined(EMU_F68K)\r
593   sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuFM68k.interrupts[0]); dstrp+=strlen(dstrp);\r
594 #endif\r
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
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
629     printf("#%02i x: %03i y: %03i %ix%i\n", u, sx, sy, ((code>>26)&3)+1, height);\r
630 \r
631     link=(code>>16)&0x7f;\r
632     if(!link) break; // End of sprites\r
633   }\r
634 #endif\r
635 \r
636   return dstr;\r
637 }\r
638 #endif\r