1 // This is part of Pico Library
\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
7 // For commercial use, separate licencing terms must be obtained.
\r
10 #include "PicoInt.h"
\r
11 #include "cd/gfx_cd.h"
\r
13 extern const unsigned char hcounts_32[];
\r
14 extern const unsigned char hcounts_40[];
\r
15 extern const unsigned short vcounts[];
\r
17 #ifndef UTYPES_DEFINED
\r
18 typedef unsigned char u8;
\r
19 typedef unsigned short u16;
\r
20 typedef unsigned int u32;
\r
21 #define UTYPES_DEFINED
\r
24 int (*PicoDmaHook)(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) = NULL;
\r
26 static __inline void AutoIncrement(void)
\r
28 Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);
\r
31 static void VideoWrite(u16 d)
\r
33 unsigned int a=Pico.video.addr;
\r
35 switch (Pico.video.type)
\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 |= PDRAW_DIRTY_SPRITES; break;
\r
40 case 3: Pico.m.dirtyPal = 1;
\r
41 Pico.cram [(a>>1)&0x003f]=d; break; // wraps (Desert Strike)
\r
42 case 5: Pico.vsram[(a>>1)&0x003f]=d; break;
\r
43 //default:elprintf(EL_ANOMALY, "VDP write %04x with bad type %i", d, Pico.video.type); break;
\r
46 //dprintf("w[%i] @ %04x, inc=%i [%i|%i]", Pico.video.type, a, Pico.video.reg[0xf], Pico.m.scanline, SekCyclesDone());
\r
50 static unsigned int VideoRead(void)
\r
52 unsigned int a=0,d=0;
\r
54 a=Pico.video.addr; a>>=1;
\r
56 switch (Pico.video.type)
\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 default:elprintf(EL_ANOMALY, "VDP read with bad type %i", Pico.video.type); break;
\r
68 static int GetDmaLength(void)
\r
70 struct PicoVideo *pvid=&Pico.video;
\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
80 static void DmaSlow(int len)
\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
85 unsigned int source;
\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
91 elprintf(EL_VDPDMA, "DmaSlow[%i] %06x->%04x len %i inc=%i blank %i [%i] @ %06x",
\r
92 Pico.video.type, source, a, len, inc, (Pico.video.status&8)||!(Pico.video.reg[1]&0x40),
\r
93 SekCyclesDone(), SekPc);
\r
95 if (Pico.m.scanline != -1) {
\r
96 Pico.m.dma_xfers += len;
\r
97 if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_PSYNC)) SekCyclesBurn(CheckDMA());
\r
98 else SekSetCyclesLeftNoMCD(SekCyclesLeftNoMCD - CheckDMA());
\r
100 // be approximate in non-accurate mode
\r
101 SekSetCyclesLeft(SekCyclesLeft - (len*(((488<<8)/167))>>8));
\r
104 if ((source&0xe00000)==0xe00000) { // Ram
\r
105 pd=(u16 *)(Pico.ram+(source&0xfffe));
\r
106 pdend=(u16 *)(Pico.ram+0x10000);
\r
108 else if (PicoAHW & PAHW_MCD)
\r
110 elprintf(EL_VDPDMA, "DmaSlow CD, r3=%02x", Pico_mcd->s68k_regs[3]);
\r
111 if(source<0x20000) { // Bios area
\r
112 pd=(u16 *)(Pico_mcd->bios+(source&~1));
\r
113 pdend=(u16 *)(Pico_mcd->bios+0x20000);
\r
114 } else if ((source&0xfc0000)==0x200000) { // Word Ram
\r
116 if (!(Pico_mcd->s68k_regs[3]&4)) { // 2M mode
\r
117 pd=(u16 *)(Pico_mcd->word_ram2M+(source&0x3fffe));
\r
118 pdend=(u16 *)(Pico_mcd->word_ram2M+0x40000);
\r
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
125 DmaSlowCell(source, a, len, inc);
\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
134 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: FIXME: unsupported src", Pico.video.type, source, a);
\r
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
143 pd=(u16 *)(Pico.rom+(source&~1));
\r
144 pdend=(u16 *)(Pico.rom+Pico.romsize);
\r
147 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: invalid src", Pico.video.type, source, a);
\r
152 // overflow protection, might break something..
\r
153 if (len > pdend - pd) {
\r
155 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow overflow");
\r
158 switch (Pico.video.type)
\r
162 if (inc == 2 && !(a&1) && a+len*2 < 0x10000)
\r
164 // most used DMA mode
\r
165 memcpy16(r + (a>>1), pd, len);
\r
173 if(a&1) d=(d<<8)|(d>>8);
\r
174 r[a>>1] = (u16)d; // will drop the upper bits
\r
177 // didn't src overlap?
\r
178 //if(pd >= pdend) pd-=0x8000; // should be good for RAM, bad for ROM
\r
181 rendstatus |= PDRAW_DIRTY_SPRITES;
\r
185 Pico.m.dirtyPal = 1;
\r
187 for(a2=a&0x7f; len; len--)
\r
189 r[a2>>1] = (u16)*pd++; // bit 0 is ignored
\r
192 // didn't src overlap?
\r
193 //if(pd >= pdend) pd-=0x8000;
\r
195 if(a2 >= 0x80) break; // Todds Adventures in Slime World / Andre Agassi tennis
\r
200 case 5: // vsram[a&0x003f]=d;
\r
202 for(a2=a&0x7f; len; len--)
\r
204 r[a2>>1] = (u16)*pd++;
\r
207 // didn't src overlap?
\r
208 //if(pd >= pdend) pd-=0x8000;
\r
210 if(a2 >= 0x80) break;
\r
216 elprintf(EL_VDPDMA|EL_ANOMALY, "DMA with bad type %i", Pico.video.type);
\r
220 Pico.video.addr=(u16)a;
\r
223 static void DmaCopy(int len)
\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
230 elprintf(EL_VDPDMA, "DmaCopy len %i [%i]", len, SekCyclesDone());
\r
232 Pico.m.dma_xfers += len;
\r
233 if(Pico.m.scanline != -1)
\r
234 Pico.video.status|=2; // dma busy
\r
236 source =Pico.video.reg[0x15];
\r
237 source|=Pico.video.reg[0x16]<<8;
\r
240 if(source+len > 0x10000) len=0x10000-source; // clip??
\r
250 rendstatus |= PDRAW_DIRTY_SPRITES;
\r
253 // check: Contra, Megaman
\r
254 // note: this is still inaccurate
\r
255 static void DmaFill(int data)
\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
263 len=GetDmaLength();
\r
264 elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%i]", len, inc, SekCyclesDone());
\r
266 Pico.m.dma_xfers += len;
\r
267 if(Pico.m.scanline != -1)
\r
268 Pico.video.status|=2; // dma busy (in accurate mode)
\r
270 // from Charles MacDonald's genvdp.txt:
\r
271 // Write lower byte to address specified
\r
272 vr[a] = (unsigned char) data;
\r
278 // Write upper byte to adjacent address
\r
279 // (here we are byteswapped, so address is already 'adjacent')
\r
282 // Increment address register
\r
288 Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0; // Dino Dini's Soccer (E) (by Haze)
\r
290 rendstatus |= PDRAW_DIRTY_SPRITES;
\r
293 static void CommandDma(void)
\r
295 struct PicoVideo *pvid=&Pico.video;
\r
296 int len=0,method=0;
\r
298 if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled
\r
300 len=GetDmaLength();
\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
307 static void CommandChange(void)
\r
309 struct PicoVideo *pvid=&Pico.video;
\r
310 unsigned int cmd=0,addr=0;
\r
314 // Get type of transfer 0xc0000030 (v/c/vsram read/write)
\r
315 pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));
\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
323 if (cmd&0x80) CommandDma();
\r
326 PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
\r
328 struct PicoVideo *pvid=&Pico.video;
\r
332 if (a==0x00) // Data port 0 or 2
\r
334 if (pvid->pending) {
\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
346 // preliminary FIFO emulation for Chaos Engine, The (E)
\r
347 if(!(pvid->status&8) && (pvid->reg[1]&0x40) && Pico.m.scanline!=-1 && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display, accurate mode?
\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
356 elprintf(EL_ASVDP, "VDP data write: %04x {%i} #%i @ %06x", d, Pico.video.type, pvid->lwrite_cnt, SekPc);
\r
363 if (a==0x04) // Control (command) port 4 or 6
\r
367 // Low word of command:
\r
368 pvid->command&=0xffff0000;
\r
375 if ((d&0xc000)==0x8000)
\r
378 int num=(d>>8)&0x1f;
\r
379 int dold=pvid->reg[num];
\r
380 pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)
\r
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
385 pvid->reg[num]=(unsigned char)d;
\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
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
396 if (!(d&0x40) && SekCyclesLeft > 390) rendstatus |= PDRAW_EARLY_BLANK;
\r
399 if (d^dold) rendstatus |= PDRAW_SPRITES_MOVED;
\r
402 // renderers should update their palettes if sh/hi mode is changed
\r
403 if ((d^dold)&8) Pico.m.dirtyPal = 2;
\r
409 #ifndef EMU_CORE_DEBUG
\r
410 // update IRQ level (Lemmings, Wiz 'n' Liz intro, ... )
\r
411 // may break if done improperly:
\r
412 // International Superstar Soccer Deluxe (crash), Street Racer (logos), Burning Force (gfx),
\r
413 // Fatal Rewind (crash), Sesame Street Counting Cafe
\r
414 if (!SekShouldInterrupt) // hack
\r
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
423 if (irq && Pico.m.scanline!=-1) SekEndRun(24); // make it delayed
\r
429 // High word of command:
\r
430 pvid->command&=0x0000ffff;
\r
431 pvid->command|=d<<16;
\r
438 PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
\r
445 if (a==0x00) // data port
\r
451 if (a==0x04) // control port
\r
453 struct PicoVideo *pv=&Pico.video;
\r
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
459 d|=(pv->pending_ints&0x20)<<2; // V-int pending?
\r
460 if (d&0x100) pv->status&=~0x100; // FIFO no longer full
\r
462 pv->pending=0; // ctrl port reads clear write-pending flag (Charles MacDonald)
\r
464 elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);
\r
468 // H-counter info (based on Generator):
\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
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
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
487 if(Pico.m.scanline != -1) {
\r
488 int lineCycles=(488-SekCyclesLeft)&0x1ff;
\r
489 d=Pico.m.scanline; // V-Counter
\r
491 if(Pico.video.reg[12]&1)
\r
492 hc=hcounts_40[lineCycles];
\r
493 else hc=hcounts_32[lineCycles];
\r
495 //if(lineCycles > 488-12) d++; // Wheel of Fortune
\r
497 // get approximate V-Counter
\r
498 d=vcounts[SekCyclesDone()>>8];
\r
499 hc = Pico.m.rotate&0xff;
\r
503 if(d >= 0x103) d-=56; // based on Gens
\r
505 if(d >= 0xEB) d-=6;
\r
508 if((Pico.video.reg[12]&6) == 6) {
\r
509 // interlace mode 2 (Combat Cars (UE) [!])
\r
511 if (d&0xf00) d|= 1;
\r
514 elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", hc, d, SekCyclesDone(), SekPc);
\r