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
16 extern int rendstatus;
\r
18 #ifndef UTYPES_DEFINED
\r
19 typedef unsigned char u8;
\r
20 typedef unsigned short u16;
\r
21 typedef unsigned int u32;
\r
22 #define UTYPES_DEFINED
\r
25 int (*PicoDmaHook)(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) = NULL;
\r
27 static __inline void AutoIncrement(void)
\r
29 Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);
\r
32 static void VideoWrite(u16 d)
\r
34 unsigned int a=Pico.video.addr;
\r
36 switch (Pico.video.type)
\r
38 case 1: if(a&1) d=(u16)((d<<8)|(d>>8)); // If address is odd, bytes are swapped (which game needs this?)
\r
39 Pico.vram [(a>>1)&0x7fff]=d;
\r
40 rendstatus|=0x10; break;
\r
41 case 3: Pico.m.dirtyPal = 1;
\r
42 Pico.cram [(a>>1)&0x003f]=d; break; // wraps (Desert Strike)
\r
43 case 5: Pico.vsram[(a>>1)&0x003f]=d; break;
\r
44 //default:elprintf(EL_ANOMALY, "VDP write %04x with bad type %i", d, Pico.video.type); break;
\r
47 //dprintf("w[%i] @ %04x, inc=%i [%i|%i]", Pico.video.type, a, Pico.video.reg[0xf], Pico.m.scanline, SekCyclesDone());
\r
51 static unsigned int VideoRead(void)
\r
53 unsigned int a=0,d=0;
\r
55 a=Pico.video.addr; a>>=1;
\r
57 switch (Pico.video.type)
\r
59 case 0: d=Pico.vram [a&0x7fff]; break;
\r
60 case 8: d=Pico.cram [a&0x003f]; break;
\r
61 case 4: d=Pico.vsram[a&0x003f]; break;
\r
62 default:elprintf(EL_ANOMALY, "VDP read with bad type %i", Pico.video.type); break;
\r
69 static int GetDmaLength(void)
\r
71 struct PicoVideo *pvid=&Pico.video;
\r
73 // 16-bit words to transfer:
\r
74 len =pvid->reg[0x13];
\r
75 len|=pvid->reg[0x14]<<8;
\r
76 // Charles MacDonald:
\r
77 if(!len) len = 0xffff;
\r
81 static void DmaSlow(int len)
\r
83 u16 *pd=0, *pdend, *r;
\r
84 unsigned int a=Pico.video.addr, a2, d;
\r
85 unsigned char inc=Pico.video.reg[0xf];
\r
86 unsigned int source;
\r
88 source =Pico.video.reg[0x15]<<1;
\r
89 source|=Pico.video.reg[0x16]<<9;
\r
90 source|=Pico.video.reg[0x17]<<17;
\r
92 elprintf(EL_VDPDMA, "DmaSlow[%i] %06x->%04x len %i inc=%i blank %i [%i] @ %06x",
\r
93 Pico.video.type, source, a, len, inc, (Pico.video.status&8)||!(Pico.video.reg[1]&0x40),
\r
94 SekCyclesDone(), SekPc);
\r
96 if(Pico.m.scanline != -1) {
\r
97 Pico.m.dma_xfers += len;
\r
98 if ((PicoMCD&1) && (PicoOpt & 0x2000)) SekCyclesBurn(CheckDMA());
\r
99 else SekSetCyclesLeftNoMCD(SekCyclesLeftNoMCD - CheckDMA());
\r
101 // be approximate in non-accurate mode
\r
102 SekSetCyclesLeft(SekCyclesLeft - (len*(((488<<8)/167))>>8));
\r
105 if ((source&0xe00000)==0xe00000) { // Ram
\r
106 pd=(u16 *)(Pico.ram+(source&0xfffe));
\r
107 pdend=(u16 *)(Pico.ram+0x10000);
\r
108 } else if (PicoMCD & 1) {
\r
109 elprintf(EL_VDPDMA, "DmaSlow CD, r3=%02x", Pico_mcd->s68k_regs[3]);
\r
110 if(source<0x20000) { // Bios area
\r
111 pd=(u16 *)(Pico_mcd->bios+(source&~1));
\r
112 pdend=(u16 *)(Pico_mcd->bios+0x20000);
\r
113 } else if ((source&0xfc0000)==0x200000) { // Word Ram
\r
115 if (!(Pico_mcd->s68k_regs[3]&4)) { // 2M mode
\r
116 pd=(u16 *)(Pico_mcd->word_ram2M+(source&0x3fffe));
\r
117 pdend=(u16 *)(Pico_mcd->word_ram2M+0x40000);
\r
119 if (source < 0x220000) { // 1M mode
\r
120 int bank = Pico_mcd->s68k_regs[3]&1;
\r
121 pd=(u16 *)(Pico_mcd->word_ram1M[bank]+(source&0x1fffe));
\r
122 pdend=(u16 *)(Pico_mcd->word_ram1M[bank]+0x20000);
\r
124 DmaSlowCell(source, a, len, inc);
\r
128 } else if ((source&0xfe0000)==0x020000) { // Prg Ram
\r
129 u8 *prg_ram = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];
\r
130 pd=(u16 *)(prg_ram+(source&0x1fffe));
\r
131 pdend=(u16 *)(prg_ram+0x20000);
\r
133 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: FIXME: unsupported src", Pico.video.type, source, a);
\r
137 // if we have DmaHook, let it handle ROM because of possible DMA delay
\r
138 if (PicoDmaHook && PicoDmaHook(source, len, &pd, &pdend));
\r
139 else if (source<Pico.romsize) { // Rom
\r
140 pd=(u16 *)(Pico.rom+(source&~1));
\r
141 pdend=(u16 *)(Pico.rom+Pico.romsize);
\r
144 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: invalid src", Pico.video.type, source, a);
\r
149 // overflow protection, might break something..
\r
150 if (len > pdend - pd) {
\r
152 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow overflow");
\r
155 switch (Pico.video.type)
\r
159 if (inc == 2 && !(a&1) && a+len*2 < 0x10000)
\r
161 // most used DMA mode
\r
162 memcpy16(r + (a>>1), pd, len);
\r
170 if(a&1) d=(d<<8)|(d>>8);
\r
171 r[a>>1] = (u16)d; // will drop the upper bits
\r
174 // didn't src overlap?
\r
175 //if(pd >= pdend) pd-=0x8000; // should be good for RAM, bad for ROM
\r
182 Pico.m.dirtyPal = 1;
\r
184 for(a2=a&0x7f; len; len--)
\r
186 r[a2>>1] = (u16)*pd++; // bit 0 is ignored
\r
189 // didn't src overlap?
\r
190 //if(pd >= pdend) pd-=0x8000;
\r
192 if(a2 >= 0x80) break; // Todds Adventures in Slime World / Andre Agassi tennis
\r
197 case 5: // vsram[a&0x003f]=d;
\r
199 for(a2=a&0x7f; len; len--)
\r
201 r[a2>>1] = (u16)*pd++;
\r
204 // didn't src overlap?
\r
205 //if(pd >= pdend) pd-=0x8000;
\r
207 if(a2 >= 0x80) break;
\r
213 elprintf(EL_VDPDMA|EL_ANOMALY, "DMA with bad type %i", Pico.video.type);
\r
217 Pico.video.addr=(u16)a;
\r
220 static void DmaCopy(int len)
\r
222 u16 a=Pico.video.addr;
\r
223 unsigned char *vr = (unsigned char *) Pico.vram;
\r
224 unsigned char *vrs;
\r
225 unsigned char inc=Pico.video.reg[0xf];
\r
227 elprintf(EL_VDPDMA, "DmaCopy len %i [%i]", len, SekCyclesDone());
\r
229 Pico.m.dma_xfers += len;
\r
230 if(Pico.m.scanline != -1)
\r
231 Pico.video.status|=2; // dma busy
\r
233 source =Pico.video.reg[0x15];
\r
234 source|=Pico.video.reg[0x16]<<8;
\r
237 if(source+len > 0x10000) len=0x10000-source; // clip??
\r
250 // check: Contra, Megaman
\r
251 // note: this is still inaccurate
\r
252 static void DmaFill(int data)
\r
255 unsigned short a=Pico.video.addr;
\r
256 unsigned char *vr=(unsigned char *) Pico.vram;
\r
257 unsigned char high = (unsigned char) (data >> 8);
\r
258 unsigned char inc=Pico.video.reg[0xf];
\r
260 len=GetDmaLength();
\r
261 elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%i]", len, inc, SekCyclesDone());
\r
263 Pico.m.dma_xfers += len;
\r
264 if(Pico.m.scanline != -1)
\r
265 Pico.video.status|=2; // dma busy (in accurate mode)
\r
267 // from Charles MacDonald's genvdp.txt:
\r
268 // Write lower byte to address specified
\r
269 vr[a] = (unsigned char) data;
\r
275 // Write upper byte to adjacent address
\r
276 // (here we are byteswapped, so address is already 'adjacent')
\r
279 // Increment address register
\r
285 Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0; // Dino Dini's Soccer (E) (by Haze)
\r
290 static void CommandDma(void)
\r
292 struct PicoVideo *pvid=&Pico.video;
\r
293 int len=0,method=0;
\r
295 if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled
\r
297 len=GetDmaLength();
\r
299 method=pvid->reg[0x17]>>6;
\r
300 if (method< 2) DmaSlow(len); // 68000 to VDP
\r
301 if (method==3) DmaCopy(len); // VRAM Copy
\r
304 static void CommandChange(void)
\r
306 struct PicoVideo *pvid=&Pico.video;
\r
307 unsigned int cmd=0,addr=0;
\r
311 // Get type of transfer 0xc0000030 (v/c/vsram read/write)
\r
312 pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));
\r
314 // Get address 0x3fff0003
\r
315 addr =(cmd>>16)&0x3fff;
\r
316 addr|=(cmd<<14)&0xc000;
\r
317 pvid->addr=(unsigned short)addr;
\r
320 if (cmd&0x80) CommandDma();
\r
323 PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
\r
325 struct PicoVideo *pvid=&Pico.video;
\r
329 if (a==0x00) // Data port 0 or 2
\r
331 if (pvid->pending) {
\r
336 // If a DMA fill has been set up, do it
\r
337 if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)
\r
343 // preliminary FIFO emulation for Chaos Engine, The (E)
\r
344 if(!(pvid->status&8) && (pvid->reg[1]&0x40) && Pico.m.scanline!=-1 && !(PicoOpt&0x10000)) // active display, accurate mode?
\r
346 pvid->status&=~0x200; // FIFO no longer empty
\r
347 pvid->lwrite_cnt++;
\r
348 if (pvid->lwrite_cnt >= 4) pvid->status|=0x100; // FIFO full
\r
349 if (pvid->lwrite_cnt > 4) {
\r
350 SekCyclesBurn(32); // penalty // 488/12-8
\r
351 if (SekCycleCnt>=SekCycleAim) SekEndRun(0);
\r
353 elprintf(EL_ASVDP, "VDP data write: %04x {%i} #%i @ %06x", d, Pico.video.type, pvid->lwrite_cnt, SekPc);
\r
360 if (a==0x04) // Control (command) port 4 or 6
\r
364 // Low word of command:
\r
365 pvid->command&=0xffff0000;
\r
370 if((d&0xc000)==0x8000)
\r
373 int num=(d>>8)&0x1f;
\r
374 int dold=pvid->reg[num];
\r
375 if (num > 0x0a && !(pvid->reg[1]&4)) {
\r
376 elprintf(EL_ANOMALY, "%02x written to reg %02x in SMS mode @ %06x", d, num, SekPc);
\r
378 pvid->reg[num]=(unsigned char)d;
\r
379 if (num==00) elprintf(EL_INTSW, "hint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x10)>>4,
\r
380 (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);
\r
381 if (num==01) elprintf(EL_INTSW, "vint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x20)>>5,
\r
382 (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);
\r
383 if (num == 5 && (d^dold)) rendstatus|=1;
\r
384 // renderers should update their palettes if sh/hi mode is changed
\r
385 else if (num == 0xc && ((d^dold)&8)) Pico.m.dirtyPal = 2;
\r
386 #ifndef EMU_CORE_DEBUG
\r
387 // update IRQ level (Lemmings, Wiz 'n' Liz intro, ... )
\r
388 // may break if done improperly:
\r
389 // International Superstar Soccer Deluxe (crash), Street Racer (logos), Burning Force (gfx),
\r
390 // Fatal Rewind (crash), Sesame Street Counting Cafe
\r
393 if (!SekShouldInterrupt) // hack
\r
395 int lines, pints, irq=0;
\r
396 lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);
\r
397 pints = (pvid->pending_ints&lines);
\r
398 if(pints & 0x20) irq = 6;
\r
399 else if(pints & 0x10) irq = 4;
\r
400 SekInterrupt(irq); // update line
\r
402 if (irq && Pico.m.scanline!=-1) SekEndRun(24); // make it delayed
\r
406 pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)
\r
408 // High word of command:
\r
409 pvid->command&=0x0000ffff;
\r
410 pvid->command|=d<<16;
\r
417 PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
\r
424 if (a==0x00) // data port
\r
430 if (a==0x04) // control port
\r
432 struct PicoVideo *pv=&Pico.video;
\r
434 if (PicoOpt&0x10) d|=0x0020; // sprite collision (Shadow of the Beast)
\r
435 if (!(pv->reg[1]&0x40)) d|=0x0008; // set V-Blank if display is disabled
\r
436 if (SekCyclesLeft < 84+4) d|=0x0004; // H-Blank (Sonic3 vs)
\r
438 d|=(pv->pending_ints&0x20)<<2; // V-int pending?
\r
439 if (d&0x100) pv->status&=~0x100; // FIFO no longer full
\r
441 pv->pending=0; // ctrl port reads clear write-pending flag (Charles MacDonald)
\r
443 elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);
\r
447 // H-counter info (based on Generator):
\r
449 // | <- hblank? -> |
\r
450 // start <416> hint <36> hdisplay <38> end // CPU cycles
\r
451 // |---------...---------|------------|-------------|
\r
452 // 0 B6 E4 FF // 40 cells
\r
453 // 0 93 E8 FF // 32 cells
\r
455 // Gens (?) v-render
\r
456 // start <hblank=84> hint hdisplay <404> |
\r
457 // |---------------------|--------------------------|
\r
458 // E4 (hc[0x43]==0) 07 B1 // 40
\r
459 // E8 (hc[0x45]==0) 05 91 // 32
\r
461 // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune
\r
462 if ((a&0x1c)==0x08)
\r
466 if(Pico.m.scanline != -1) {
\r
467 int lineCycles=(488-SekCyclesLeft)&0x1ff;
\r
468 d=Pico.m.scanline; // V-Counter
\r
470 if(Pico.video.reg[12]&1)
\r
471 hc=hcounts_40[lineCycles];
\r
472 else hc=hcounts_32[lineCycles];
\r
474 //if(lineCycles > 488-12) d++; // Wheel of Fortune
\r
476 // get approximate V-Counter
\r
477 d=vcounts[SekCyclesDone()>>8];
\r
478 hc = Pico.m.rotate&0xff;
\r
482 if(d >= 0x103) d-=56; // based on Gens
\r
484 if(d >= 0xEB) d-=6;
\r
487 if((Pico.video.reg[12]&6) == 6) {
\r
488 // interlace mode 2 (Combat Cars (UE) [!])
\r
490 if (d&0xf00) d|= 1;
\r
493 elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", hc, d, SekCyclesDone(), SekPc);
\r