849389d3d04ce2ce20ef1ccc639e7643b06b1b8a
[picodrive.git] / Pico / VideoPort.c
1 // This is part of Pico Library\r
2 \r
3 // (c) Copyright 2004 Dave, All rights reserved.\r
4 // (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas\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 "cd/gfx_cd.h"\r
12 \r
13 extern const unsigned char  hcounts_32[];\r
14 extern const unsigned char  hcounts_40[];\r
15 extern const unsigned short vcounts[];\r
16 \r
17 #ifndef UTYPES_DEFINED\r
18 typedef unsigned char  u8;\r
19 typedef unsigned short u16;\r
20 typedef unsigned int   u32;\r
21 #define UTYPES_DEFINED\r
22 #endif\r
23 \r
24 int (*PicoDmaHook)(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) = NULL;\r
25 \r
26 static __inline void AutoIncrement(void)\r
27 {\r
28   Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);\r
29 }\r
30 \r
31 static void VideoWrite(u16 d)\r
32 {\r
33   unsigned int a=Pico.video.addr;\r
34 \r
35   switch (Pico.video.type)\r
36   {\r
37     case 1: if(a&1) d=(u16)((d<<8)|(d>>8)); // If address is odd, bytes are swapped (which game needs this?)\r
38             Pico.vram [(a>>1)&0x7fff]=d;\r
39             rendstatus |= PDRAW_DIRTY_SPRITES; break;\r
40     case 3: Pico.m.dirtyPal = 1;\r
41             Pico.cram [(a>>1)&0x003f]=d; break; // wraps (Desert Strike)\r
42     case 5: Pico.vsram[(a>>1)&0x003f]=d; break;\r
43     //default:elprintf(EL_ANOMALY, "VDP write %04x with bad type %i", d, Pico.video.type); break;\r
44   }\r
45 \r
46   //dprintf("w[%i] @ %04x, inc=%i [%i|%i]", Pico.video.type, a, Pico.video.reg[0xf], Pico.m.scanline, SekCyclesDone());\r
47   AutoIncrement();\r
48 }\r
49 \r
50 static unsigned int VideoRead(void)\r
51 {\r
52   unsigned int a=0,d=0;\r
53 \r
54   a=Pico.video.addr; a>>=1;\r
55 \r
56   switch (Pico.video.type)\r
57   {\r
58     case 0: d=Pico.vram [a&0x7fff]; break;\r
59     case 8: d=Pico.cram [a&0x003f]; break;\r
60     case 4: d=Pico.vsram[a&0x003f]; break;\r
61     default:elprintf(EL_ANOMALY, "VDP read with bad type %i", Pico.video.type); break;\r
62   }\r
63 \r
64   AutoIncrement();\r
65   return d;\r
66 }\r
67 \r
68 static int GetDmaLength(void)\r
69 {\r
70   struct PicoVideo *pvid=&Pico.video;\r
71   int len=0;\r
72   // 16-bit words to transfer:\r
73   len =pvid->reg[0x13];\r
74   len|=pvid->reg[0x14]<<8;\r
75   // Charles MacDonald:\r
76   if(!len) len = 0xffff;\r
77   return len;\r
78 }\r
79 \r
80 static void DmaSlow(int len)\r
81 {\r
82   u16 *pd=0, *pdend, *r;\r
83   unsigned int a=Pico.video.addr, a2, d;\r
84   unsigned char inc=Pico.video.reg[0xf];\r
85   unsigned int source;\r
86 \r
87   source =Pico.video.reg[0x15]<<1;\r
88   source|=Pico.video.reg[0x16]<<9;\r
89   source|=Pico.video.reg[0x17]<<17;\r
90 \r
91   elprintf(EL_VDPDMA, "DmaSlow[%i] %06x->%04x len %i inc=%i blank %i [%i] @ %06x",\r
92     Pico.video.type, source, a, len, inc, (Pico.video.status&8)||!(Pico.video.reg[1]&0x40),\r
93     SekCyclesDone(), SekPc);\r
94 \r
95   if (Pico.m.scanline != -1) {\r
96     Pico.m.dma_xfers += len;\r
97     if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_PSYNC)) SekCyclesBurn(CheckDMA());\r
98     else SekSetCyclesLeftNoMCD(SekCyclesLeftNoMCD - CheckDMA());\r
99   } else {\r
100     // be approximate in non-accurate mode\r
101     SekSetCyclesLeft(SekCyclesLeft - (len*(((488<<8)/167))>>8));\r
102   }\r
103 \r
104   if ((source&0xe00000)==0xe00000) { // Ram\r
105     pd=(u16 *)(Pico.ram+(source&0xfffe));\r
106     pdend=(u16 *)(Pico.ram+0x10000);\r
107   }\r
108   else if (PicoAHW & PAHW_MCD)\r
109   {\r
110     elprintf(EL_VDPDMA, "DmaSlow CD, r3=%02x", Pico_mcd->s68k_regs[3]);\r
111     if(source<0x20000) { // Bios area\r
112       pd=(u16 *)(Pico_mcd->bios+(source&~1));\r
113       pdend=(u16 *)(Pico_mcd->bios+0x20000);\r
114     } else if ((source&0xfc0000)==0x200000) { // Word Ram\r
115       source -= 2;\r
116       if (!(Pico_mcd->s68k_regs[3]&4)) { // 2M mode\r
117         pd=(u16 *)(Pico_mcd->word_ram2M+(source&0x3fffe));\r
118         pdend=(u16 *)(Pico_mcd->word_ram2M+0x40000);\r
119       } else {\r
120         if (source < 0x220000) { // 1M mode\r
121           int bank = Pico_mcd->s68k_regs[3]&1;\r
122           pd=(u16 *)(Pico_mcd->word_ram1M[bank]+(source&0x1fffe));\r
123           pdend=(u16 *)(Pico_mcd->word_ram1M[bank]+0x20000);\r
124         } else {\r
125           DmaSlowCell(source, a, len, inc);\r
126           return;\r
127         }\r
128       }\r
129     } else if ((source&0xfe0000)==0x020000) { // Prg Ram\r
130       u8 *prg_ram = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];\r
131       pd=(u16 *)(prg_ram+(source&0x1fffe));\r
132       pdend=(u16 *)(prg_ram+0x20000);\r
133     } else {\r
134       elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: FIXME: unsupported src", Pico.video.type, source, a);\r
135       return;\r
136     }\r
137   }\r
138   else\r
139   {\r
140     // if we have DmaHook, let it handle ROM because of possible DMA delay\r
141     if (PicoDmaHook && PicoDmaHook(source, len, &pd, &pdend));\r
142     else if (source<Pico.romsize) { // Rom\r
143       pd=(u16 *)(Pico.rom+(source&~1));\r
144       pdend=(u16 *)(Pico.rom+Pico.romsize);\r
145     }\r
146     else {\r
147       elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: invalid src", Pico.video.type, source, a);\r
148       return;\r
149     }\r
150   }\r
151 \r
152   // overflow protection, might break something..\r
153   if (len > pdend - pd) {\r
154     len = pdend - pd;\r
155     elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow overflow");\r
156   }\r
157 \r
158   switch (Pico.video.type)\r
159   {\r
160     case 1: // vram\r
161       r = Pico.vram;\r
162       if (inc == 2 && !(a&1) && a+len*2 < 0x10000)\r
163       {\r
164         // most used DMA mode\r
165         memcpy16(r + (a>>1), pd, len);\r
166         a += len*2;\r
167       }\r
168       else\r
169       {\r
170         for(; len; len--)\r
171         {\r
172           d=*pd++;\r
173           if(a&1) d=(d<<8)|(d>>8);\r
174           r[a>>1] = (u16)d; // will drop the upper bits\r
175           // AutoIncrement\r
176           a=(u16)(a+inc);\r
177           // didn't src overlap?\r
178           //if(pd >= pdend) pd-=0x8000; // should be good for RAM, bad for ROM\r
179         }\r
180       }\r
181       rendstatus |= PDRAW_DIRTY_SPRITES;\r
182       break;\r
183 \r
184     case 3: // cram\r
185       Pico.m.dirtyPal = 1;\r
186       r = Pico.cram;\r
187       for(a2=a&0x7f; len; len--)\r
188       {\r
189         r[a2>>1] = (u16)*pd++; // bit 0 is ignored\r
190         // AutoIncrement\r
191         a2+=inc;\r
192         // didn't src overlap?\r
193         //if(pd >= pdend) pd-=0x8000;\r
194         // good dest?\r
195         if(a2 >= 0x80) break; // Todds Adventures in Slime World / Andre Agassi tennis\r
196       }\r
197       a=(a&0xff00)|a2;\r
198       break;\r
199 \r
200     case 5: // vsram[a&0x003f]=d;\r
201       r = Pico.vsram;\r
202       for(a2=a&0x7f; len; len--)\r
203       {\r
204         r[a2>>1] = (u16)*pd++;\r
205         // AutoIncrement\r
206         a2+=inc;\r
207         // didn't src overlap?\r
208         //if(pd >= pdend) pd-=0x8000;\r
209         // good dest?\r
210         if(a2 >= 0x80) break;\r
211       }\r
212       a=(a&0xff00)|a2;\r
213       break;\r
214 \r
215     default:\r
216       elprintf(EL_VDPDMA|EL_ANOMALY, "DMA with bad type %i", Pico.video.type);\r
217       break;\r
218   }\r
219   // remember addr\r
220   Pico.video.addr=(u16)a;\r
221 }\r
222 \r
223 static void DmaCopy(int len)\r
224 {\r
225   u16 a=Pico.video.addr;\r
226   unsigned char *vr = (unsigned char *) Pico.vram;\r
227   unsigned char *vrs;\r
228   unsigned char inc=Pico.video.reg[0xf];\r
229   int source;\r
230   elprintf(EL_VDPDMA, "DmaCopy len %i [%i]", len, SekCyclesDone());\r
231 \r
232   Pico.m.dma_xfers += len;\r
233   if(Pico.m.scanline != -1)\r
234     Pico.video.status|=2; // dma busy\r
235 \r
236   source =Pico.video.reg[0x15];\r
237   source|=Pico.video.reg[0x16]<<8;\r
238   vrs=vr+source;\r
239 \r
240   if(source+len > 0x10000) len=0x10000-source; // clip??\r
241 \r
242   for(;len;len--)\r
243   {\r
244     vr[a] = *vrs++;\r
245     // AutoIncrement\r
246     a=(u16)(a+inc);\r
247   }\r
248   // remember addr\r
249   Pico.video.addr=a;\r
250   rendstatus |= PDRAW_DIRTY_SPRITES;\r
251 }\r
252 \r
253 // check: Contra, Megaman\r
254 // note: this is still inaccurate\r
255 static void DmaFill(int data)\r
256 {\r
257   int len;\r
258   unsigned short a=Pico.video.addr;\r
259   unsigned char *vr=(unsigned char *) Pico.vram;\r
260   unsigned char high = (unsigned char) (data >> 8);\r
261   unsigned char inc=Pico.video.reg[0xf];\r
262 \r
263   len=GetDmaLength();\r
264   elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%i]", len, inc, SekCyclesDone());\r
265 \r
266   Pico.m.dma_xfers += len;\r
267   if(Pico.m.scanline != -1)\r
268     Pico.video.status|=2; // dma busy (in accurate mode)\r
269 \r
270   // from Charles MacDonald's genvdp.txt:\r
271   // Write lower byte to address specified\r
272   vr[a] = (unsigned char) data;\r
273   a=(u16)(a+inc);\r
274 \r
275   if(!inc) len=1;\r
276 \r
277   for(;len;len--) {\r
278     // Write upper byte to adjacent address\r
279     // (here we are byteswapped, so address is already 'adjacent')\r
280     vr[a] = high;\r
281 \r
282     // Increment address register\r
283     a=(u16)(a+inc);\r
284   }\r
285   // remember addr\r
286   Pico.video.addr=a;\r
287   // update length\r
288   Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0; // Dino Dini's Soccer (E) (by Haze)\r
289 \r
290   rendstatus |= PDRAW_DIRTY_SPRITES;\r
291 }\r
292 \r
293 static void CommandDma(void)\r
294 {\r
295   struct PicoVideo *pvid=&Pico.video;\r
296   int len=0,method=0;\r
297 \r
298   if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled\r
299 \r
300   len=GetDmaLength();\r
301 \r
302   method=pvid->reg[0x17]>>6;\r
303   if (method< 2) DmaSlow(len); // 68000 to VDP\r
304   if (method==3) DmaCopy(len); // VRAM Copy\r
305 }\r
306 \r
307 static void CommandChange(void)\r
308 {\r
309   struct PicoVideo *pvid=&Pico.video;\r
310   unsigned int cmd=0,addr=0;\r
311 \r
312   cmd=pvid->command;\r
313 \r
314   // Get type of transfer 0xc0000030 (v/c/vsram read/write)\r
315   pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));\r
316 \r
317   // Get address 0x3fff0003\r
318   addr =(cmd>>16)&0x3fff;\r
319   addr|=(cmd<<14)&0xc000;\r
320   pvid->addr=(unsigned short)addr;\r
321 \r
322   // Check for dma:\r
323   if (cmd&0x80) CommandDma();\r
324 }\r
325 \r
326 static __inline void DrawSync(int blank_on)\r
327 {\r
328   if (Pico.m.scanline < 224 && !(PicoOpt & POPT_ALT_RENDERER) &&\r
329       !PicoSkipFrame && DrawScanline <= Pico.m.scanline) {\r
330     elprintf(EL_ANOMALY, "sync");\r
331     PicoDrawSync(Pico.m.scanline, blank_on);\r
332   }\r
333 }\r
334 \r
335 PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)\r
336 {\r
337   struct PicoVideo *pvid=&Pico.video;\r
338 \r
339   if (Pico.m.scanline < 224)\r
340     elprintf(EL_STATUS, "PicoVideoWrite [%06x] %04x", a, d);\r
341   a&=0x1c;\r
342 \r
343   if (a==0x00) // Data port 0 or 2\r
344   {\r
345     // try avoiding the sync..\r
346     if (Pico.m.scanline < 224 && (pvid->reg[1]&0x40) &&\r
347         !(!pvid->pending &&\r
348           ((pvid->command & 0xc00000f0) == 0x40000010 && Pico.vsram[pvid->addr>>1] == d))\r
349        )\r
350       DrawSync(0);\r
351 \r
352     if (pvid->pending) {\r
353       CommandChange();\r
354       pvid->pending=0;\r
355     }\r
356 \r
357     // If a DMA fill has been set up, do it\r
358     if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)\r
359     {\r
360       DmaFill(d);\r
361     }\r
362     else\r
363     {\r
364       // preliminary FIFO emulation for Chaos Engine, The (E)\r
365       if (!(pvid->status&8) && (pvid->reg[1]&0x40) && Pico.m.scanline!=-1 && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display, accurate mode?\r
366       {\r
367         pvid->status&=~0x200; // FIFO no longer empty\r
368         pvid->lwrite_cnt++;\r
369         if (pvid->lwrite_cnt >= 4) pvid->status|=0x100; // FIFO full\r
370         if (pvid->lwrite_cnt >  4) {\r
371           SekCyclesBurn(32); // penalty // 488/12-8\r
372           if (SekCycleCnt>=SekCycleAim) SekEndRun(0);\r
373         }\r
374         elprintf(EL_ASVDP, "VDP data write: %04x {%i} #%i @ %06x", d, Pico.video.type, pvid->lwrite_cnt, SekPc);\r
375       }\r
376       VideoWrite(d);\r
377     }\r
378     return;\r
379   }\r
380 \r
381   if (a==0x04) // Control (command) port 4 or 6\r
382   {\r
383     if (pvid->pending)\r
384     {\r
385       if (d & 0x80) DrawSync(0); // only need sync for DMA\r
386       // Low word of command:\r
387       pvid->command&=0xffff0000;\r
388       pvid->command|=d;\r
389       pvid->pending=0;\r
390       CommandChange();\r
391     }\r
392     else\r
393     {\r
394       if ((d&0xc000)==0x8000)\r
395       {\r
396         // Register write:\r
397         int num=(d>>8)&0x1f;\r
398         int dold=pvid->reg[num];\r
399         int update_irq = 0, blank_on = 0;\r
400         pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)\r
401         if (num > 0x0a && !(pvid->reg[1]&4)) {\r
402           elprintf(EL_ANOMALY, "%02x written to reg %02x in SMS mode @ %06x", d, num, SekPc);\r
403           return;\r
404         }\r
405 \r
406         switch (num)\r
407         {\r
408           case 0x00:\r
409             elprintf(EL_INTSW, "hint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x10)>>4,\r
410                     (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);\r
411             update_irq = 1;\r
412             break;\r
413           case 0x01:\r
414             elprintf(EL_INTSW, "vint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x20)>>5,\r
415                     (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);\r
416             if (!(d&0x40) && SekCyclesLeft > 390) blank_on = 1;\r
417             update_irq = 1;\r
418             break;\r
419           case 0x05:\r
420             if (d^dold) rendstatus |= PDRAW_SPRITES_MOVED;\r
421             break;\r
422           case 0x0c:\r
423             // renderers should update their palettes if sh/hi mode is changed\r
424             if ((d^dold)&8) Pico.m.dirtyPal = 2;\r
425             break;\r
426         }\r
427         DrawSync(blank_on);\r
428         pvid->reg[num]=(unsigned char)d;\r
429         if (!update_irq) return;\r
430 \r
431 #ifndef EMU_CORE_DEBUG\r
432         // update IRQ level\r
433         if (!SekShouldInterrupt) // hack\r
434         {\r
435           int lines, pints, irq=0;\r
436           lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);\r
437           pints = (pvid->pending_ints&lines);\r
438                if (pints & 0x20) irq = 6;\r
439           else if (pints & 0x10) irq = 4;\r
440           SekInterrupt(irq); // update line\r
441 \r
442           if (irq && Pico.m.scanline!=-1) SekEndRun(24); // make it delayed\r
443         }\r
444 #endif\r
445       }\r
446       else\r
447       {\r
448         // High word of command:\r
449         pvid->command&=0x0000ffff;\r
450         pvid->command|=d<<16;\r
451         pvid->pending=1;\r
452       }\r
453     }\r
454   }\r
455 }\r
456 \r
457 PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)\r
458 {\r
459   a&=0x1c;\r
460 \r
461   if (a==0x00) // data port\r
462   {\r
463     return VideoRead();\r
464   }\r
465 \r
466   if (a==0x04) // control port\r
467   {\r
468     struct PicoVideo *pv=&Pico.video;\r
469     unsigned int d;\r
470     d=pv->status;\r
471     if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)\r
472     if (!(pv->reg[1]&0x40))        d|=0x0008; // set V-Blank if display is disabled\r
473     if (SekCyclesLeft < 84+4)      d|=0x0004; // H-Blank (Sonic3 vs)\r
474 \r
475     d|=(pv->pending_ints&0x20)<<2; // V-int pending?\r
476     if (d&0x100) pv->status&=~0x100; // FIFO no longer full\r
477 \r
478     pv->pending=0; // ctrl port reads clear write-pending flag (Charles MacDonald)\r
479 \r
480     elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);\r
481     return d;\r
482   }\r
483 \r
484   // H-counter info (based on Generator):\r
485   // frame:\r
486   //                       |       <- hblank? ->      |\r
487   // start    <416>       hint  <36> hdisplay <38>  end // CPU cycles\r
488   // |---------...---------|------------|-------------|\r
489   // 0                   B6 E4                       FF // 40 cells\r
490   // 0                   93 E8                       FF // 32 cells\r
491 \r
492   // Gens (?)              v-render\r
493   // start  <hblank=84>   hint    hdisplay <404>      |\r
494   // |---------------------|--------------------------|\r
495   // E4  (hc[0x43]==0)    07                         B1 // 40\r
496   // E8  (hc[0x45]==0)    05                         91 // 32\r
497 \r
498   // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune\r
499   if ((a&0x1c)==0x08)\r
500   {\r
501     unsigned int hc, d;\r
502 \r
503     if (Pico.m.scanline != -1)\r
504     {\r
505       int lineCycles=(488-SekCyclesLeft)&0x1ff;\r
506       d=Pico.m.scanline; // V-Counter\r
507 \r
508       if(Pico.video.reg[12]&1)\r
509            hc=hcounts_40[lineCycles];\r
510       else hc=hcounts_32[lineCycles];\r
511 \r
512       //if(lineCycles > 488-12) d++; // Wheel of Fortune\r
513     } else {\r
514       // get approximate V-Counter\r
515       d=vcounts[SekCyclesDone()>>8];\r
516       hc = Pico.m.rotate&0xff;\r
517     }\r
518 \r
519     if(Pico.m.pal) {\r
520       if (d >= 0x103) d-=56; // based on Gens\r
521     } else {\r
522       if (d >= 0xEB)  d-=6;\r
523     }\r
524 \r
525     if((Pico.video.reg[12]&6) == 6) {\r
526       // interlace mode 2 (Combat Cars (UE) [!])\r
527       d <<= 1;\r
528       if (d&0xf00) d|= 1;\r
529     }\r
530 \r
531     elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", hc, d, SekCyclesDone(), SekPc);\r
532     d&=0xff; d<<=8;\r
533     d|=hc;\r
534     return d;\r
535   }\r
536 \r
537   return 0;\r
538 }\r