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