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