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