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