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