3 * (c) Copyright Dave, 2004
\r
4 * (C) notaz, 2006-2009
\r
6 * This work is licensed under the terms of MAME license.
\r
7 * See COPYING file in the top-level directory.
\r
10 #include "pico_int.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
16 #ifndef UTYPES_DEFINED
\r
17 typedef unsigned char u8;
\r
18 typedef unsigned short u16;
\r
19 typedef unsigned int u32;
\r
20 #define UTYPES_DEFINED
\r
23 int (*PicoDmaHook)(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) = NULL;
\r
25 static __inline void AutoIncrement(void)
\r
27 Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);
\r
30 static void VideoWrite(u16 d)
\r
32 unsigned int a=Pico.video.addr;
\r
34 switch (Pico.video.type)
\r
36 case 1: if(a&1) d=(u16)((d<<8)|(d>>8)); // If address is odd, bytes are swapped (which game needs this?)
\r
37 Pico.vram [(a>>1)&0x7fff]=d;
\r
38 if (a - ((unsigned)(Pico.video.reg[5]&0x7f) << 9) < 0x400)
\r
39 rendstatus |= PDRAW_DIRTY_SPRITES;
\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
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 Pico.m.dma_xfers += len;
\r
96 if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_PSYNC)) SekCyclesBurn(CheckDMA());
\r
97 else SekEndTimeslice(SekCyclesLeftNoMCD - CheckDMA());
\r
99 if ((source&0xe00000)==0xe00000) { // Ram
\r
100 pd=(u16 *)(Pico.ram+(source&0xfffe));
\r
101 pdend=(u16 *)(Pico.ram+0x10000);
\r
103 else if (PicoAHW & PAHW_MCD)
\r
105 elprintf(EL_VDPDMA, "DmaSlow CD, r3=%02x", Pico_mcd->s68k_regs[3]);
\r
106 if(source<0x20000) { // Bios area
\r
107 pd=(u16 *)(Pico_mcd->bios+(source&~1));
\r
108 pdend=(u16 *)(Pico_mcd->bios+0x20000);
\r
109 } else if ((source&0xfc0000)==0x200000) { // Word Ram
\r
111 if (!(Pico_mcd->s68k_regs[3]&4)) { // 2M mode
\r
112 pd=(u16 *)(Pico_mcd->word_ram2M+(source&0x3fffe));
\r
113 pdend=(u16 *)(Pico_mcd->word_ram2M+0x40000);
\r
115 if (source < 0x220000) { // 1M mode
\r
116 int bank = Pico_mcd->s68k_regs[3]&1;
\r
117 pd=(u16 *)(Pico_mcd->word_ram1M[bank]+(source&0x1fffe));
\r
118 pdend=(u16 *)(Pico_mcd->word_ram1M[bank]+0x20000);
\r
120 DmaSlowCell(source, a, len, inc);
\r
124 } else if ((source&0xfe0000)==0x020000) { // Prg Ram
\r
125 u8 *prg_ram = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];
\r
126 pd=(u16 *)(prg_ram+(source&0x1fffe));
\r
127 pdend=(u16 *)(prg_ram+0x20000);
\r
129 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: FIXME: unsupported src", Pico.video.type, source, a);
\r
135 // if we have DmaHook, let it handle ROM because of possible DMA delay
\r
136 if (PicoDmaHook && PicoDmaHook(source, len, &pd, &pdend));
\r
137 else if (source<Pico.romsize) { // Rom
\r
138 pd=(u16 *)(Pico.rom+(source&~1));
\r
139 pdend=(u16 *)(Pico.rom+Pico.romsize);
\r
142 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: invalid src", Pico.video.type, source, a);
\r
147 // overflow protection, might break something..
\r
148 if (len > pdend - pd) {
\r
150 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow overflow");
\r
153 switch (Pico.video.type)
\r
157 if (inc == 2 && !(a&1) && a+len*2 < 0x10000)
\r
159 // most used DMA mode
\r
160 memcpy16(r + (a>>1), pd, len);
\r
168 if(a&1) d=(d<<8)|(d>>8);
\r
169 r[a>>1] = (u16)d; // will drop the upper bits
\r
172 // didn't src overlap?
\r
173 //if(pd >= pdend) pd-=0x8000; // should be good for RAM, bad for ROM
\r
176 rendstatus |= PDRAW_DIRTY_SPRITES;
\r
180 Pico.m.dirtyPal = 1;
\r
182 for(a2=a&0x7f; len; len--)
\r
184 r[a2>>1] = (u16)*pd++; // bit 0 is ignored
\r
187 // didn't src overlap?
\r
188 //if(pd >= pdend) pd-=0x8000;
\r
190 if(a2 >= 0x80) break; // Todds Adventures in Slime World / Andre Agassi tennis
\r
195 case 5: // vsram[a&0x003f]=d;
\r
197 for(a2=a&0x7f; len; len--)
\r
199 r[a2>>1] = (u16)*pd++;
\r
202 // didn't src overlap?
\r
203 //if(pd >= pdend) pd-=0x8000;
\r
205 if(a2 >= 0x80) break;
\r
211 elprintf(EL_VDPDMA|EL_ANOMALY, "DMA with bad type %i", Pico.video.type);
\r
215 Pico.video.addr=(u16)a;
\r
218 static void DmaCopy(int len)
\r
220 u16 a=Pico.video.addr;
\r
221 unsigned char *vr = (unsigned char *) Pico.vram;
\r
222 unsigned char *vrs;
\r
223 unsigned char inc=Pico.video.reg[0xf];
\r
225 elprintf(EL_VDPDMA, "DmaCopy len %i [%i]", len, SekCyclesDone());
\r
227 Pico.m.dma_xfers += len;
\r
228 Pico.video.status |= 2; // dma busy
\r
230 source =Pico.video.reg[0x15];
\r
231 source|=Pico.video.reg[0x16]<<8;
\r
234 if (source+len > 0x10000) len=0x10000-source; // clip??
\r
244 rendstatus |= PDRAW_DIRTY_SPRITES;
\r
247 // check: Contra, Megaman
\r
248 // note: this is still inaccurate
\r
249 static void DmaFill(int data)
\r
252 unsigned short a=Pico.video.addr;
\r
253 unsigned char *vr=(unsigned char *) Pico.vram;
\r
254 unsigned char high = (unsigned char) (data >> 8);
\r
255 unsigned char inc=Pico.video.reg[0xf];
\r
257 len=GetDmaLength();
\r
258 elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%i]", len, inc, SekCyclesDone());
\r
260 Pico.m.dma_xfers += len;
\r
261 Pico.video.status |= 2; // dma busy
\r
263 // from Charles MacDonald's genvdp.txt:
\r
264 // Write lower byte to address specified
\r
265 vr[a] = (unsigned char) data;
\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
275 // Increment address register
\r
281 Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0; // Dino Dini's Soccer (E) (by Haze)
\r
283 rendstatus |= PDRAW_DIRTY_SPRITES;
\r
286 static void CommandDma(void)
\r
288 struct PicoVideo *pvid=&Pico.video;
\r
289 int len=0,method=0;
\r
291 if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled
\r
293 len=GetDmaLength();
\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
300 static void CommandChange(void)
\r
302 struct PicoVideo *pvid=&Pico.video;
\r
303 unsigned int cmd=0,addr=0;
\r
307 // Get type of transfer 0xc0000030 (v/c/vsram read/write)
\r
308 pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));
\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
316 if (cmd&0x80) CommandDma();
\r
319 static void DrawSync(int blank_on)
\r
321 if (Pico.m.scanline < 224 && !(PicoOpt & POPT_ALT_RENDERER) &&
\r
322 !PicoSkipFrame && DrawScanline <= Pico.m.scanline) {
\r
323 //elprintf(EL_ANOMALY, "sync");
\r
324 PicoDrawSync(Pico.m.scanline, blank_on);
\r
328 PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
\r
330 struct PicoVideo *pvid=&Pico.video;
\r
332 //if (Pico.m.scanline < 224)
\r
333 // elprintf(EL_STATUS, "PicoVideoWrite [%06x] %04x", a, d);
\r
336 if (a==0x00) // Data port 0 or 2
\r
338 // try avoiding the sync..
\r
339 if (Pico.m.scanline < 224 && (pvid->reg[1]&0x40) &&
\r
340 !(!pvid->pending &&
\r
341 ((pvid->command & 0xc00000f0) == 0x40000010 && Pico.vsram[pvid->addr>>1] == d))
\r
345 if (pvid->pending) {
\r
350 // If a DMA fill has been set up, do it
\r
351 if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)
\r
357 // preliminary FIFO emulation for Chaos Engine, The (E)
\r
358 if (!(pvid->status&8) && (pvid->reg[1]&0x40) && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display?
\r
360 pvid->status&=~0x200; // FIFO no longer empty
\r
361 pvid->lwrite_cnt++;
\r
362 if (pvid->lwrite_cnt >= 4) pvid->status|=0x100; // FIFO full
\r
363 if (pvid->lwrite_cnt > 4) {
\r
364 SekCyclesBurn(32); // penalty // 488/12-8
\r
365 if (SekCycleCnt>=SekCycleAim) SekEndRun(0);
\r
367 elprintf(EL_ASVDP, "VDP data write: %04x [%06x] {%i} #%i @ %06x", d, Pico.video.addr,
\r
368 Pico.video.type, pvid->lwrite_cnt, SekPc);
\r
375 if (a==0x04) // Control (command) port 4 or 6
\r
379 if (d & 0x80) DrawSync(0); // only need sync for DMA
\r
380 // Low word of command:
\r
381 pvid->command&=0xffff0000;
\r
388 if ((d&0xc000)==0x8000)
\r
391 int num=(d>>8)&0x1f;
\r
392 int dold=pvid->reg[num];
\r
394 pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)
\r
395 if (num > 0x0a && !(pvid->reg[1]&4)) {
\r
396 elprintf(EL_ANOMALY, "%02x written to reg %02x in SMS mode @ %06x", d, num, SekPc);
\r
400 if (num == 1 && !(d&0x40) && SekCyclesLeft > 390) blank_on = 1;
\r
401 DrawSync(blank_on);
\r
402 pvid->reg[num]=(unsigned char)d;
\r
406 elprintf(EL_INTSW, "hint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x10)>>4,
\r
407 (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);
\r
410 elprintf(EL_INTSW, "vint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x20)>>5,
\r
411 (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);
\r
414 //elprintf(EL_STATUS, "spritep moved to %04x", (unsigned)(Pico.video.reg[5]&0x7f) << 9);
\r
415 if (d^dold) rendstatus |= PDRAW_SPRITES_MOVED;
\r
418 // renderers should update their palettes if sh/hi mode is changed
\r
419 if ((d^dold)&8) Pico.m.dirtyPal = 2;
\r
425 #ifndef EMU_CORE_DEBUG
\r
426 // update IRQ level
\r
427 if (!SekShouldInterrupt) // hack
\r
429 int lines, pints, irq=0;
\r
430 lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);
\r
431 pints = (pvid->pending_ints&lines);
\r
432 if (pints & 0x20) irq = 6;
\r
433 else if (pints & 0x10) irq = 4;
\r
434 SekInterrupt(irq); // update line
\r
436 if (irq) SekEndRun(24); // make it delayed
\r
442 // High word of command:
\r
443 pvid->command&=0x0000ffff;
\r
444 pvid->command|=d<<16;
\r
451 PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
\r
455 if (a==0x04) // control port
\r
457 struct PicoVideo *pv=&Pico.video;
\r
460 //if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)
\r
461 if (SekCyclesLeft < 84+4) d|=0x0004; // H-Blank (Sonic3 vs)
\r
463 d |= ((pv->reg[1]&0x40)^0x40) >> 3; // set V-Blank if display is disabled
\r
464 d |= (pv->pending_ints&0x20)<<2; // V-int pending?
\r
465 if (d&0x100) pv->status&=~0x100; // FIFO no longer full
\r
467 pv->pending = 0; // ctrl port reads clear write-pending flag (Charles MacDonald)
\r
469 elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);
\r
473 // H-counter info (based on Generator):
\r
475 // | <- hblank? -> |
\r
476 // start <416> hint <36> hdisplay <38> end // CPU cycles
\r
477 // |---------...---------|------------|-------------|
\r
478 // 0 B6 E4 FF // 40 cells
\r
479 // 0 93 E8 FF // 32 cells
\r
481 // Gens (?) v-render
\r
482 // start <hblank=84> hint hdisplay <404> |
\r
483 // |---------------------|--------------------------|
\r
484 // E4 (hc[0x43]==0) 07 B1 // 40
\r
485 // E8 (hc[0x45]==0) 05 91 // 32
\r
487 // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune
\r
488 if ((a&0x1c)==0x08)
\r
493 lineCycles = (488-SekCyclesLeft)&0x1ff;
\r
494 if (Pico.video.reg[12]&1)
\r
495 d = hcounts_40[lineCycles];
\r
496 else d = hcounts_32[lineCycles];
\r
498 elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", d, Pico.video.v_counter, SekCyclesDone(), SekPc);
\r
499 return d | (Pico.video.v_counter << 8);
\r
502 if (a==0x00) // data port
\r
504 return VideoRead();
\r
510 unsigned int PicoVideoRead8(unsigned int a)
\r
517 case 0: return VideoRead() >> 8;
\r
518 case 1: return VideoRead() & 0xff;
\r
519 case 4: // control port/status reg
\r
520 d = Pico.video.status >> 8;
\r
521 if (d&1) Pico.video.status&=~0x100; // FIFO no longer full
\r
522 Pico.video.pending = 0;
\r
523 elprintf(EL_SR, "SR read (h): %02x @ %06x", d, SekPc);
\r
526 d = Pico.video.status & 0xff;
\r
527 //if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)
\r
528 d |= ((Pico.video.reg[1]&0x40)^0x40) >> 3; // set V-Blank if display is disabled
\r
529 d |= (Pico.video.pending_ints&0x20)<<2; // V-int pending?
\r
530 if (SekCyclesLeft < 84+4) d |= 4; // H-Blank
\r
531 Pico.video.pending = 0;
\r
532 elprintf(EL_SR, "SR read (l): %02x @ %06x", d, SekPc);
\r
534 case 8: // hv counter
\r
535 elprintf(EL_HVCNT, "vcounter: %02x (%i) @ %06x", Pico.video.v_counter, SekCyclesDone(), SekPc);
\r
536 return Pico.video.v_counter;
\r
538 d = (488-SekCyclesLeft)&0x1ff;
\r
539 if (Pico.video.reg[12]&1)
\r
541 else d = hcounts_32[d];
\r
542 elprintf(EL_HVCNT, "hcounter: %02x (%i) @ %06x", d, SekCyclesDone(), SekPc);
\r