1 // This is part of Pico Library
\r
3 // (c) Copyright 2004 Dave, All rights reserved.
\r
4 // (c) Copyright 2006 notaz, All rights reserved.
\r
5 // Free for non-commercial use.
\r
7 // For commercial use, separate licencing terms must be obtained.
\r
10 #include "PicoInt.h"
\r
12 extern const unsigned char hcounts_32[];
\r
13 extern const unsigned char hcounts_40[];
\r
14 extern const unsigned short vcounts[];
\r
15 extern int rendstatus;
\r
17 typedef unsigned short u16;
\r
20 static __inline void AutoIncrement()
\r
22 Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);
\r
25 static void VideoWrite(u16 d)
\r
27 unsigned int a=Pico.video.addr;
\r
29 switch (Pico.video.type)
\r
31 case 1: if(a&1) d=(u16)((d<<8)|(d>>8)); // If address is odd, bytes are swapped (which game needs this?)
\r
32 Pico.vram [(a>>1)&0x7fff]=d;
\r
33 rendstatus|=0x10; break;
\r
34 case 3: Pico.m.dirtyPal = 1;
\r
35 //dprintf("w[%i] @ %04x, inc=%i [%i|%i]", Pico.video.type, a, Pico.video.reg[0xf], Pico.m.scanline, SekCyclesDone());
\r
36 Pico.cram [(a>>1)&0x003f]=d; break; // wraps (Desert Strike)
\r
37 case 5: Pico.vsram[(a>>1)&0x003f]=d; break;
\r
40 //dprintf("w[%i] @ %04x, inc=%i [%i|%i]", Pico.video.type, a, Pico.video.reg[0xf], Pico.m.scanline, SekCyclesDone());
\r
44 static unsigned int VideoRead()
\r
46 unsigned int a=0,d=0;
\r
48 a=Pico.video.addr; a>>=1;
\r
50 switch (Pico.video.type)
\r
52 case 0: d=Pico.vram [a&0x7fff]; break;
\r
53 case 8: d=Pico.cram [a&0x003f]; break;
\r
54 case 4: d=Pico.vsram[a&0x003f]; break;
\r
62 // calculate the number of cycles 68k->VDP dma operation would take
\r
63 static int DmaSlowBurn(int len)
\r
65 // test: Legend of Galahad, Time Killers
\r
66 int burn,maxlen,line=Pico.m.scanline;
\r
68 if(line == -1) line=vcounts[SekCyclesDone()>>8];
\r
69 maxlen=(224-line)*18;
\r
71 burn = len*(((488<<8)/18))>>8;
\r
73 burn = maxlen*(((488<<8)/18))>>8;
\r
74 burn += (len-maxlen)*(((488<<8)/180))>>8;
\r
81 static int GetDmaLength()
\r
83 struct PicoVideo *pvid=&Pico.video;
\r
85 // 16-bit words to transfer:
\r
86 len =pvid->reg[0x13];
\r
87 len|=pvid->reg[0x14]<<8;
\r
88 // Charles MacDonald:
\r
89 if(!len) len = 0xffff;
\r
93 static void DmaSlow(int len)
\r
95 u16 *pd=0, *pdend, *r;
\r
96 unsigned int a=Pico.video.addr, a2, d;
\r
97 unsigned char inc=Pico.video.reg[0xf];
\r
98 unsigned int source; // , burn;
\r
100 source =Pico.video.reg[0x15]<<1;
\r
101 source|=Pico.video.reg[0x16]<<9;
\r
102 source|=Pico.video.reg[0x17]<<17;
\r
104 dprintf("DmaSlow[%i] %06x->%04x len %i inc=%i blank %i [%i|%i] @ %x",
\r
105 Pico.video.type, source, a, len, inc, (Pico.video.status&8)||!(Pico.video.reg[1]&0x40),
\r
106 Pico.m.scanline, SekCyclesDone(), SekPc);
\r
108 if ((source&0xe00000)==0xe00000) { pd=(u16 *)(Pico.ram+(source&0xfffe)); pdend=(u16 *)(Pico.ram+0x10000); } // Ram
\r
109 else if(source<Pico.romsize) { pd=(u16 *)(Pico.rom+(source&~1)); pdend=(u16 *)(Pico.rom+Pico.romsize); } // Rom
\r
110 else return; // Invalid source address
\r
113 // CPU is stopped during DMA, so we burn some cycles to compensate that
\r
114 if((Pico.video.status&8)||!(Pico.video.reg[1]&0x40)) { // vblank?
\r
115 burn = (len*(((488<<8)/167))>>8); // very approximate
\r
116 if(!(Pico.video.status&8)) burn+=burn>>1; // a hack for Legend of Galahad
\r
117 } else burn = DmaSlowBurn(len);
\r
118 SekCyclesBurn(burn);
\r
120 Pico.m.dma_bytes += len;
\r
122 //if(!(Pico.video.status&8))
\r
124 //Pico.m.dma_endcycles = 0;//SekCyclesLeft;
\r
125 //Pico.m.dma_endcycles -= Pico.m.dma_endcycles>>3; // hack
\r
126 SekSetCyclesLeft(SekCyclesLeft - CheckDMA());
\r
128 // dprintf("DmaSlow burn: %i @ %06x", burn, SekPc);
\r
130 switch (Pico.video.type)
\r
137 if(a&1) d=(d<<8)|(d>>8);
\r
138 r[a>>1] = (u16)d; // will drop the upper bits
\r
141 // didn't src overlap?
\r
142 if(pd >= pdend) pd-=0x8000; // should be good for RAM, bad for ROM
\r
148 Pico.m.dirtyPal = 1;
\r
150 for(a2=a&0x7f; len; len--)
\r
152 r[a2>>1] = (u16)*pd++;; // bit 0 is ignored
\r
155 // didn't src overlap?
\r
156 if(pd >= pdend) pd-=0x8000;
\r
158 if(a2 >= 0x80) break; // Todds Adventures in Slime World / Andre Agassi tennis
\r
163 case 5: // vsram[a&0x003f]=d;
\r
165 for(a2=a&0x7f; len; len--)
\r
167 r[a2>>1] = (u16)*pd++;
\r
170 // didn't src overlap?
\r
171 if(pd >= pdend) pd-=0x8000;
\r
173 if(a2 >= 0x80) break;
\r
179 Pico.video.addr=(u16)a;
\r
182 static void DmaCopy(int len)
\r
184 u16 a=Pico.video.addr;
\r
185 unsigned char *vr = (unsigned char *) Pico.vram;
\r
186 unsigned char *vrs;
\r
187 unsigned char inc=Pico.video.reg[0xf];
\r
189 dprintf("DmaCopy len %i [%i|%i]", len, Pico.m.scanline, SekCyclesDone());
\r
191 Pico.m.dma_bytes += len;
\r
192 if(Pico.m.scanline != -1)
\r
193 Pico.video.status|=2; // dma busy
\r
195 source =Pico.video.reg[0x15];
\r
196 source|=Pico.video.reg[0x16]<<8;
\r
199 if(source+len > 0x10000) len=0x10000-source; // clip??
\r
212 // check: Contra, Megaman
\r
213 // note: this is still inaccurate
\r
214 static void DmaFill(int data)
\r
217 unsigned short a=Pico.video.addr;
\r
218 unsigned char *vr=(unsigned char *) Pico.vram;
\r
219 unsigned char high = (unsigned char) (data >> 8);
\r
220 unsigned char inc=Pico.video.reg[0xf];
\r
222 len=GetDmaLength();
\r
223 dprintf("DmaFill len %i inc %i [%i|%i]", len, inc, Pico.m.scanline, SekCyclesDone());
\r
225 Pico.m.dma_bytes += len;
\r
226 if(Pico.m.scanline != -1)
\r
227 Pico.video.status|=2; // dma busy (in accurate mode)
\r
229 // from Charles MacDonald's genvdp.txt:
\r
230 // Write lower byte to address specified
\r
231 vr[a] = (unsigned char) data;
\r
237 // Write upper byte to adjacent address
\r
238 // (here we are byteswapped, so address is already 'adjacent')
\r
241 // Increment address register
\r
247 Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0; // Dino Dini's Soccer (E) (by Haze)
\r
252 static void CommandDma()
\r
254 struct PicoVideo *pvid=&Pico.video;
\r
255 int len=0,method=0;
\r
257 if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled
\r
259 len=GetDmaLength();
\r
261 method=pvid->reg[0x17]>>6;
\r
262 if (method< 2) DmaSlow(len); // 68000 to VDP
\r
263 if (method==3) DmaCopy(len); // VRAM Copy
\r
266 static void CommandChange()
\r
268 struct PicoVideo *pvid=&Pico.video;
\r
269 unsigned int cmd=0,addr=0;
\r
273 // Get type of transfer 0xc0000030 (v/c/vsram read/write)
\r
274 pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));
\r
276 // Get address 0x3fff0003
\r
277 addr =(cmd>>16)&0x3fff;
\r
278 addr|=(cmd<<14)&0xc000;
\r
279 pvid->addr=(unsigned short)addr;
\r
280 //dprintf("addr set: %04x", addr);
\r
283 if (cmd&0x80) CommandDma();
\r
286 void PicoVideoWrite(unsigned int a,unsigned short d)
\r
288 struct PicoVideo *pvid=&Pico.video;
\r
292 if (a==0x00) // Data port 0 or 2
\r
294 if (pvid->pending) CommandChange();
\r
297 // If a DMA fill has been set up, do it
\r
298 if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)
\r
309 if (a==0x04) // Control (command) port 4 or 6
\r
311 //dprintf("vdp cw(%04x), p=%i @ %06x [%i]", d, pvid->pending, SekPc, SekCyclesDone());
\r
314 // Low word of command:
\r
315 pvid->command&=0xffff0000;
\r
320 if((d&0xc000)==0x8000)
\r
323 int num=(d>>8)&0x1f;
\r
324 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
325 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
326 //if(num==01) dprintf("set_blank: %i @ %06x [%i|%i]", !((d&0x40)>>6), SekPc, Pico.m.scanline, SekCyclesDone());
\r
327 //if(num==05) dprintf("spr_set: %i @ %06x [%i|%i]", (unsigned char)d, SekPc, Pico.m.scanline, SekCyclesDone());
\r
328 //if(num==10) dprintf("hint_set: %i @ %06x [%i|%i]", (unsigned char)d, SekPc, Pico.m.scanline, SekCyclesDone());
\r
329 pvid->reg[num]=(unsigned char)d;
\r
330 #if !(defined(EMU_C68K) && defined(EMU_M68K)) // not debugging Cyclone
\r
331 // update IRQ level (Lemmings, Wiz 'n' Liz intro, ... )
\r
332 // may break if done improperly:
\r
333 // International Superstar Soccer Deluxe (crash), Street Racer (logos), Burning Force (gfx), Fatal Rewind (hang), Sesame Street Counting Cafe
\r
336 // hack: make sure we do not touch the irq line if Cyclone is just about to take the IRQ
\r
337 if (PicoCpu.irq <= (PicoCpu.srh&7)) {
\r
340 lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);
\r
341 pints = (pvid->pending_ints&lines);
\r
342 if(pints & 0x20) SekInterrupt(6);
\r
343 else if(pints & 0x10) SekInterrupt(4);
\r
344 else SekInterrupt(0);
\r
346 // adjust cycles for Cyclone so it would take the int "in time"
\r
355 if(num == 5) rendstatus|=1;
\r
356 else if(num == 0xc) Pico.m.dirtyPal = 2; // renderers should update their palettes if sh/hi mode is changed
\r
357 pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)
\r
359 // High word of command:
\r
360 pvid->command&=0x0000ffff;
\r
361 pvid->command|=d<<16;
\r
368 unsigned int PicoVideoRead(unsigned int a)
\r
374 if (a==0x00) // data port
\r
380 if (a==0x04) // control port
\r
382 d=Pico.video.status;
\r
383 if(PicoOpt&0x10) d|=0x0020; // sprite collision (Shadow of the Beast)
\r
384 if(Pico.m.rotate++&8) d|=0x0100; else d|=0x0200; // Toggle fifo full empty (who uses that stuff?)
\r
385 if(!(Pico.video.reg[1]&0x40)) d|=0x0008; // set V-Blank if display is disabled
\r
386 if(SekCyclesLeft < 84+4) d|=0x0004; // H-Blank (Sonic3 vs)
\r
387 dprintf("sr_read %04x @ %06x [%i|%i]", d, SekPc, Pico.m.scanline, SekCyclesDone());
\r
389 Pico.video.pending=0; // ctrl port reads clear write-pending flag (Charles MacDonald)
\r
394 // H-counter info (based on Generator):
\r
396 // | <- hblank? -> |
\r
397 // start <416> hint <36> hdisplay <38> end // CPU cycles
\r
398 // |---------...---------|------------|-------------|
\r
399 // 0 B6 E4 FF // 40 cells
\r
400 // 0 93 E8 FF // 32 cells
\r
402 // Gens (?) v-render
\r
403 // start <hblank=84> hint hdisplay <404> |
\r
404 // |---------------------|--------------------------|
\r
405 // E4 (hc[0x43]==0) 07 B1 // 40
\r
406 // E8 (hc[0x45]==0) 05 91 // 32
\r
408 // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune
\r
409 if ((a&0x1c)==0x08)
\r
413 if(Pico.m.scanline != -1) {
\r
414 int lineCycles=(488-SekCyclesLeft)&0x1ff;
\r
415 d=Pico.m.scanline; // V-Counter
\r
417 if(Pico.video.reg[12]&1)
\r
418 hc=hcounts_40[lineCycles];
\r
419 else hc=hcounts_32[lineCycles];
\r
421 //if(lineCycles > 488-12) d++; // Wheel of Fortune
\r
423 // get approximate V-Counter
\r
424 d=vcounts[SekCyclesDone()>>8];
\r
425 hc = Pico.m.rotate&0xff;
\r
429 if(d >= 0x103) d-=56; // based on Gens
\r
431 if(d >= 0xEB) d-=6;
\r
434 if((Pico.video.reg[12]&6) == 6) {
\r
435 // interlace mode 2 (Combat Cars (UE) [!])
\r
437 if (d&0xf00) d|= 1;
\r
440 dprintf("hv: %02x %02x (%i) @ %06x", hc, d, SekCyclesDone(), SekPc);
\r