menu text adjustment
[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
cc68a136 16\r
eff55556 17#ifndef UTYPES_DEFINED\r
ab0607f7 18typedef unsigned char u8;\r
cc68a136 19typedef unsigned short u16;\r
eff55556 20typedef unsigned int u32;\r
21#define UTYPES_DEFINED\r
22#endif\r
cc68a136 23\r
5de27868 24int (*PicoDmaHook)(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) = NULL;\r
cc68a136 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
602133e1 39 rendstatus |= PDRAW_DIRTY_SPRITES; break;\r
cc68a136 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
d26dc685 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
602133e1 95 if (Pico.m.scanline != -1) {\r
69996cb7 96 Pico.m.dma_xfers += len;\r
602133e1 97 if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_PSYNC)) 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
602133e1 107 }\r
108 else if (PicoAHW & PAHW_MCD)\r
109 {\r
69996cb7 110 elprintf(EL_VDPDMA, "DmaSlow CD, r3=%02x", Pico_mcd->s68k_regs[3]);\r
ab0607f7 111 if(source<0x20000) { // Bios area\r
112 pd=(u16 *)(Pico_mcd->bios+(source&~1));\r
113 pdend=(u16 *)(Pico_mcd->bios+0x20000);\r
fa1e5e29 114 } else if ((source&0xfc0000)==0x200000) { // Word Ram\r
115 source -= 2;\r
ab0607f7 116 if (!(Pico_mcd->s68k_regs[3]&4)) { // 2M mode\r
fa1e5e29 117 pd=(u16 *)(Pico_mcd->word_ram2M+(source&0x3fffe));\r
118 pdend=(u16 *)(Pico_mcd->word_ram2M+0x40000);\r
ab0607f7 119 } else {\r
fa1e5e29 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
69996cb7 124 } else {\r
fa1e5e29 125 DmaSlowCell(source, a, len, inc);\r
126 return;\r
69996cb7 127 }\r
ab0607f7 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
03e4f2a3 134 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: FIXME: unsupported src", Pico.video.type, source, a);\r
ab0607f7 135 return;\r
136 }\r
602133e1 137 }\r
138 else\r
139 {\r
50483b53 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
ab0607f7 143 pd=(u16 *)(Pico.rom+(source&~1));\r
144 pdend=(u16 *)(Pico.rom+Pico.romsize);\r
f8ef8ff7 145 }\r
f8ef8ff7 146 else {\r
03e4f2a3 147 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: invalid src", Pico.video.type, source, a);\r
ab0607f7 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
69996cb7 155 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow overflow");\r
672ad671 156 }\r
cc68a136 157\r
158 switch (Pico.video.type)\r
159 {\r
160 case 1: // vram\r
161 r = Pico.vram;\r
cea65903 162 if (inc == 2 && !(a&1) && a+len*2 < 0x10000)\r
cc68a136 163 {\r
cea65903 164 // most used DMA mode\r
fa1e5e29 165 memcpy16(r + (a>>1), pd, len);\r
166 a += len*2;\r
cea65903 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
cc68a136 180 }\r
602133e1 181 rendstatus |= PDRAW_DIRTY_SPRITES;\r
cc68a136 182 break;\r
4f672280 183\r
cc68a136 184 case 3: // cram\r
cc68a136 185 Pico.m.dirtyPal = 1;\r
186 r = Pico.cram;\r
187 for(a2=a&0x7f; len; len--)\r
188 {\r
0a051f55 189 r[a2>>1] = (u16)*pd++; // bit 0 is ignored\r
cc68a136 190 // AutoIncrement\r
191 a2+=inc;\r
192 // didn't src overlap?\r
ab0607f7 193 //if(pd >= pdend) pd-=0x8000;\r
cc68a136 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
ab0607f7 208 //if(pd >= pdend) pd-=0x8000;\r
cc68a136 209 // good dest?\r
210 if(a2 >= 0x80) break;\r
211 }\r
212 a=(a&0xff00)|a2;\r
213 break;\r
69996cb7 214\r
215 default:\r
216 elprintf(EL_VDPDMA|EL_ANOMALY, "DMA with bad type %i", Pico.video.type);\r
217 break;\r
cc68a136 218 }\r
219 // remember addr\r
220 Pico.video.addr=(u16)a;\r
221}\r
222\r
223static 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
69996cb7 230 elprintf(EL_VDPDMA, "DmaCopy len %i [%i]", len, SekCyclesDone());\r
4f672280 231\r
69996cb7 232 Pico.m.dma_xfers += len;\r
8c1952f0 233 if(Pico.m.scanline != -1)\r
234 Pico.video.status|=2; // dma busy\r
cc68a136 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
602133e1 250 rendstatus |= PDRAW_DIRTY_SPRITES;\r
cc68a136 251}\r
252\r
253// check: Contra, Megaman\r
254// note: this is still inaccurate\r
255static 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
4f672280 262\r
cc68a136 263 len=GetDmaLength();\r
69996cb7 264 elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%i]", len, inc, SekCyclesDone());\r
4f672280 265\r
69996cb7 266 Pico.m.dma_xfers += len;\r
8c1952f0 267 if(Pico.m.scanline != -1)\r
268 Pico.video.status|=2; // dma busy (in accurate mode)\r
cc68a136 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
602133e1 290 rendstatus |= PDRAW_DIRTY_SPRITES;\r
cc68a136 291}\r
292\r
69996cb7 293static void CommandDma(void)\r
cc68a136 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
69996cb7 307static void CommandChange(void)\r
cc68a136 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
cc68a136 321\r
322 // Check for dma:\r
323 if (cmd&0x80) CommandDma();\r
324}\r
325\r
eff55556 326PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)\r
cc68a136 327{\r
328 struct PicoVideo *pvid=&Pico.video;\r
329\r
330 a&=0x1c;\r
331\r
332 if (a==0x00) // Data port 0 or 2\r
4f672280 333 {\r
69996cb7 334 if (pvid->pending) {\r
335 CommandChange();\r
336 pvid->pending=0;\r
337 }\r
cc68a136 338\r
339 // If a DMA fill has been set up, do it\r
340 if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)\r
341 {\r
342 DmaFill(d);\r
343 }\r
344 else\r
345 {\r
69996cb7 346 // preliminary FIFO emulation for Chaos Engine, The (E)\r
602133e1 347 if(!(pvid->status&8) && (pvid->reg[1]&0x40) && Pico.m.scanline!=-1 && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display, accurate mode?\r
69996cb7 348 {\r
349 pvid->status&=~0x200; // FIFO no longer empty\r
350 pvid->lwrite_cnt++;\r
351 if (pvid->lwrite_cnt >= 4) pvid->status|=0x100; // FIFO full\r
352 if (pvid->lwrite_cnt > 4) {\r
353 SekCyclesBurn(32); // penalty // 488/12-8\r
354 if (SekCycleCnt>=SekCycleAim) SekEndRun(0);\r
355 }\r
356 elprintf(EL_ASVDP, "VDP data write: %04x {%i} #%i @ %06x", d, Pico.video.type, pvid->lwrite_cnt, SekPc);\r
357 }\r
cc68a136 358 VideoWrite(d);\r
359 }\r
360 return;\r
361 }\r
362\r
363 if (a==0x04) // Control (command) port 4 or 6\r
364 {\r
cc68a136 365 if(pvid->pending)\r
366 {\r
367 // Low word of command:\r
368 pvid->command&=0xffff0000;\r
369 pvid->command|=d;\r
370 pvid->pending=0;\r
371 CommandChange();\r
602133e1 372 }\r
373 else\r
374 {\r
375 if ((d&0xc000)==0x8000)\r
cc68a136 376 {\r
377 // Register write:\r
378 int num=(d>>8)&0x1f;\r
0ffefdb8 379 int dold=pvid->reg[num];\r
602133e1 380 pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)\r
0ffefdb8 381 if (num > 0x0a && !(pvid->reg[1]&4)) {\r
382 elprintf(EL_ANOMALY, "%02x written to reg %02x in SMS mode @ %06x", d, num, SekPc);\r
602133e1 383 return;\r
0ffefdb8 384 } else\r
602133e1 385 pvid->reg[num]=(unsigned char)d;\r
386\r
387 switch (num)\r
388 {\r
389 case 0x00:\r
390 elprintf(EL_INTSW, "hint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x10)>>4,\r
391 (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);\r
392 goto update_irq;\r
393 case 0x01:\r
394 elprintf(EL_INTSW, "vint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x20)>>5,\r
395 (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);\r
9333d7f7 396 if (!(d&0x40) && SekCyclesLeft > 390) rendstatus |= PDRAW_EARLY_BLANK;\r
602133e1 397 goto update_irq;\r
398 case 0x05:\r
399 if (d^dold) rendstatus |= PDRAW_SPRITES_MOVED;\r
400 break;\r
401 case 0x0c:\r
402 // renderers should update their palettes if sh/hi mode is changed\r
403 if ((d^dold)&8) Pico.m.dirtyPal = 2;\r
404 break;\r
405 }\r
406 return;\r
407\r
99464b62 408update_irq:;\r
03e4f2a3 409#ifndef EMU_CORE_DEBUG\r
cc68a136 410 // update IRQ level (Lemmings, Wiz 'n' Liz intro, ... )\r
411 // may break if done improperly:\r
69996cb7 412 // International Superstar Soccer Deluxe (crash), Street Racer (logos), Burning Force (gfx),\r
03e4f2a3 413 // Fatal Rewind (crash), Sesame Street Counting Cafe\r
602133e1 414 if (!SekShouldInterrupt) // hack\r
03e4f2a3 415 {\r
602133e1 416 int lines, pints, irq=0;\r
417 lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);\r
418 pints = (pvid->pending_ints&lines);\r
419 if(pints & 0x20) irq = 6;\r
420 else if(pints & 0x10) irq = 4;\r
421 SekInterrupt(irq); // update line\r
422\r
423 if (irq && Pico.m.scanline!=-1) SekEndRun(24); // make it delayed\r
cc68a136 424 }\r
cc68a136 425#endif\r
602133e1 426 }\r
427 else\r
428 {\r
cc68a136 429 // High word of command:\r
430 pvid->command&=0x0000ffff;\r
431 pvid->command|=d<<16;\r
432 pvid->pending=1;\r
433 }\r
434 }\r
435 }\r
436}\r
437\r
eff55556 438PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)\r
cc68a136 439{\r
440 unsigned int d=0;\r
4f672280 441\r
cc68a136 442 a&=0x1c;\r
443\r
69996cb7 444\r
cc68a136 445 if (a==0x00) // data port\r
446 {\r
447 d=VideoRead();\r
448 goto end;\r
449 }\r
450\r
451 if (a==0x04) // control port\r
452 {\r
69996cb7 453 struct PicoVideo *pv=&Pico.video;\r
454 d=pv->status;\r
602133e1 455 if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)\r
456 if (!(pv->reg[1]&0x40)) d|=0x0008; // set V-Blank if display is disabled\r
457 if (SekCyclesLeft < 84+4) d|=0x0004; // H-Blank (Sonic3 vs)\r
69996cb7 458\r
459 d|=(pv->pending_ints&0x20)<<2; // V-int pending?\r
460 if (d&0x100) pv->status&=~0x100; // FIFO no longer full\r
cc68a136 461\r
69996cb7 462 pv->pending=0; // ctrl port reads clear write-pending flag (Charles MacDonald)\r
cc68a136 463\r
69996cb7 464 elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);\r
cc68a136 465 goto end;\r
466 }\r
467\r
468 // H-counter info (based on Generator):\r
469 // frame:\r
470 // | <- hblank? -> |\r
471 // start <416> hint <36> hdisplay <38> end // CPU cycles\r
472 // |---------...---------|------------|-------------|\r
473 // 0 B6 E4 FF // 40 cells\r
474 // 0 93 E8 FF // 32 cells\r
475\r
476 // Gens (?) v-render\r
477 // start <hblank=84> hint hdisplay <404> |\r
478 // |---------------------|--------------------------|\r
479 // E4 (hc[0x43]==0) 07 B1 // 40\r
480 // E8 (hc[0x45]==0) 05 91 // 32\r
4f672280 481\r
cc68a136 482 // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune\r
483 if ((a&0x1c)==0x08)\r
484 {\r
485 unsigned int hc;\r
486\r
487 if(Pico.m.scanline != -1) {\r
488 int lineCycles=(488-SekCyclesLeft)&0x1ff;\r
489 d=Pico.m.scanline; // V-Counter\r
490\r
491 if(Pico.video.reg[12]&1)\r
492 hc=hcounts_40[lineCycles];\r
493 else hc=hcounts_32[lineCycles];\r
494\r
8c1952f0 495 //if(lineCycles > 488-12) d++; // Wheel of Fortune\r
cc68a136 496 } else {\r
497 // get approximate V-Counter\r
498 d=vcounts[SekCyclesDone()>>8];\r
499 hc = Pico.m.rotate&0xff;\r
500 }\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
69996cb7 514 elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", hc, d, SekCyclesDone(), SekPc);\r
cc68a136 515 d&=0xff; d<<=8;\r
516 d|=hc;\r
517 goto end;\r
518 }\r
519\r
520end:\r
521\r
522 return d;\r
523}\r