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 #define NEED_DMA_SOURCE
\r
14 int line_base_cycles;
\r
15 extern const unsigned char hcounts_32[];
\r
16 extern const unsigned char hcounts_40[];
\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 **base, unsigned int *mask) = 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 if (a - ((unsigned)(Pico.video.reg[5]&0x7f) << 9) < 0x400)
\r
41 Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
\r
43 case 3: Pico.m.dirtyPal = 1;
\r
44 Pico.cram [(a>>1)&0x003f]=d; break; // wraps (Desert Strike)
\r
45 case 5: Pico.vsram[(a>>1)&0x003f]=d; break;
\r
46 //default:elprintf(EL_ANOMALY, "VDP write %04x with bad type %i", d, Pico.video.type); break;
\r
52 static unsigned int VideoRead(void)
\r
54 unsigned int a=0,d=0;
\r
56 a=Pico.video.addr; a>>=1;
\r
58 switch (Pico.video.type)
\r
60 case 0: d=Pico.vram [a&0x7fff]; break;
\r
61 case 8: d=Pico.cram [a&0x003f]; break;
\r
62 case 4: d=Pico.vsram[a&0x003f]; break;
\r
63 default:elprintf(EL_ANOMALY, "VDP read with bad type %i", Pico.video.type); break;
\r
70 static int GetDmaLength(void)
\r
72 struct PicoVideo *pvid=&Pico.video;
\r
74 // 16-bit words to transfer:
\r
75 len =pvid->reg[0x13];
\r
76 len|=pvid->reg[0x14]<<8;
\r
77 len = ((len - 1) & 0xffff) + 1;
\r
81 static void DmaSlow(int len, unsigned int source)
\r
83 u32 inc = Pico.video.reg[0xf];
\r
84 u32 a = Pico.video.addr;
\r
85 u16 *r, *base = NULL;
\r
88 elprintf(EL_VDPDMA, "DmaSlow[%i] %06x->%04x len %i inc=%i blank %i [%u] @ %06x",
\r
89 Pico.video.type, source, a, len, inc, (Pico.video.status&8)||!(Pico.video.reg[1]&0x40),
\r
90 SekCyclesDone(), SekPc);
\r
92 Pico.m.dma_xfers += len;
\r
93 if (Pico.m.dma_xfers < len) // lame 16bit var
\r
94 Pico.m.dma_xfers = ~0;
\r
95 SekCyclesBurnRun(CheckDMA());
\r
97 if ((source & 0xe00000) == 0xe00000) { // Ram
\r
98 base = (u16 *)Pico.ram;
\r
101 else if (PicoAHW & PAHW_MCD)
\r
103 u8 r3 = Pico_mcd->s68k_regs[3];
\r
104 elprintf(EL_VDPDMA, "DmaSlow CD, r3=%02x", r3);
\r
105 if (source < 0x20000) { // Bios area
\r
106 base = (u16 *)Pico_mcd->bios;
\r
107 } else if ((source & 0xfc0000) == 0x200000) { // Word Ram
\r
108 if (!(r3 & 4)) { // 2M mode
\r
109 base = (u16 *)(Pico_mcd->word_ram2M + (source & 0x20000));
\r
111 if (source < 0x220000) { // 1M mode
\r
113 base = (u16 *)(Pico_mcd->word_ram1M[bank]);
\r
115 DmaSlowCell(source - 2, a, len, inc);
\r
120 } else if ((source & 0xfe0000) == 0x020000) { // Prg Ram
\r
121 base = (u16 *)Pico_mcd->prg_ram_b[r3 >> 6];
\r
122 source -= 2; // XXX: test
\r
127 // if we have DmaHook, let it handle ROM because of possible DMA delay
\r
129 if (PicoDmaHook && (source2 = PicoDmaHook(source, len, &base, &mask)))
\r
132 base = m68k_dma_source(source);
\r
135 elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: invalid src", Pico.video.type, source, a);
\r
139 // operate in words
\r
143 switch (Pico.video.type)
\r
147 if (inc == 2 && !(a & 1) && a + len * 2 < 0x10000
\r
148 && !(((source + len - 1) ^ source) & ~mask))
\r
150 // most used DMA mode
\r
151 memcpy((char *)r + a, base + (source & mask), len * 2);
\r
158 u16 d = base[source++ & mask];
\r
159 if(a & 1) d=(d<<8)|(d>>8);
\r
162 a = (u16)(a + inc);
\r
165 Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
\r
169 Pico.m.dirtyPal = 1;
\r
173 r[(a / 2) & 0x3f] = base[source++ & mask];
\r
183 r[(a / 2) & 0x3f] = base[source++ & mask];
\r
190 if (Pico.video.type != 0 || (EL_LOGMASK & EL_VDPDMA))
\r
191 elprintf(EL_VDPDMA|EL_ANOMALY, "DMA with bad type %i", Pico.video.type);
\r
195 Pico.video.addr=(u16)a;
\r
198 static void DmaCopy(int len)
\r
200 u16 a=Pico.video.addr;
\r
201 unsigned char *vr = (unsigned char *) Pico.vram;
\r
202 unsigned char inc=Pico.video.reg[0xf];
\r
204 elprintf(EL_VDPDMA, "DmaCopy len %i [%i]", len, SekCyclesDone());
\r
206 Pico.m.dma_xfers += len;
\r
207 if (Pico.m.dma_xfers < len)
\r
208 Pico.m.dma_xfers = ~0;
\r
209 Pico.video.status |= 2; // dma busy
\r
211 source =Pico.video.reg[0x15];
\r
212 source|=Pico.video.reg[0x16]<<8;
\r
216 vr[a] = vr[source++ & 0xffff];
\r
222 Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
\r
225 static NOINLINE void DmaFill(int data)
\r
227 unsigned short a=Pico.video.addr;
\r
228 unsigned char *vr=(unsigned char *) Pico.vram;
\r
229 unsigned char high = (unsigned char) (data >> 8);
\r
230 unsigned char inc=Pico.video.reg[0xf];
\r
234 len = GetDmaLength();
\r
235 elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%i]", len, inc, SekCyclesDone());
\r
237 Pico.m.dma_xfers += len;
\r
238 if (Pico.m.dma_xfers < len) // lame 16bit var
\r
239 Pico.m.dma_xfers = ~0;
\r
240 Pico.video.status |= 2; // dma busy
\r
242 switch (Pico.video.type)
\r
245 for (l = len; l; l--) {
\r
246 // Write upper byte to adjacent address
\r
247 // (here we are byteswapped, so address is already 'adjacent')
\r
250 // Increment address register
\r
251 a = (u16)(a + inc);
\r
256 // TODO: needs fifo; anyone using these?
\r
259 elprintf(EL_STATUS|EL_ANOMALY|EL_VDPDMA, "TODO: cram/vsram fill");
\r
267 Pico.video.addr = a;
\r
269 Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0;
\r
270 source = Pico.video.reg[0x15];
\r
271 source |= Pico.video.reg[0x16] << 8;
\r
273 Pico.video.reg[0x15] = source;
\r
274 Pico.video.reg[0x16] = source >> 8;
\r
276 Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
\r
279 static NOINLINE void CommandDma(void)
\r
281 struct PicoVideo *pvid=&Pico.video;
\r
285 if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled
\r
287 len = GetDmaLength();
\r
288 source =Pico.video.reg[0x15];
\r
289 source|=Pico.video.reg[0x16] << 8;
\r
290 source|=Pico.video.reg[0x17] << 16;
\r
292 method=pvid->reg[0x17]>>6;
\r
294 DmaSlow(len, source << 1); // 68000 to VDP
\r
295 else if (method == 3)
\r
296 DmaCopy(len); // VRAM Copy
\r
301 Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0;
\r
302 Pico.video.reg[0x15] = source;
\r
303 Pico.video.reg[0x16] = source >> 8;
\r
306 static void CommandChange(void)
\r
308 struct PicoVideo *pvid=&Pico.video;
\r
309 unsigned int cmd=0,addr=0;
\r
313 // Get type of transfer 0xc0000030 (v/c/vsram read/write)
\r
314 pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));
\r
316 // Get address 0x3fff0003
\r
317 addr =(cmd>>16)&0x3fff;
\r
318 addr|=(cmd<<14)&0xc000;
\r
319 pvid->addr=(unsigned short)addr;
\r
322 static void DrawSync(int blank_on)
\r
324 if (Pico.m.scanline < 224 && !(PicoOpt & POPT_ALT_RENDERER) &&
\r
325 !PicoSkipFrame && Pico.est.DrawScanline <= Pico.m.scanline) {
\r
326 //elprintf(EL_ANOMALY, "sync");
\r
327 PicoDrawSync(Pico.m.scanline, blank_on);
\r
331 PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
\r
333 struct PicoVideo *pvid=&Pico.video;
\r
335 //if (Pico.m.scanline < 224)
\r
336 // elprintf(EL_STATUS, "PicoVideoWrite [%06x] %04x", a, d);
\r
339 if (a==0x00) // Data port 0 or 2
\r
341 // try avoiding the sync..
\r
342 if (Pico.m.scanline < 224 && (pvid->reg[1]&0x40) &&
\r
343 !(!pvid->pending &&
\r
344 ((pvid->command & 0xc00000f0) == 0x40000010 && Pico.vsram[pvid->addr>>1] == d))
\r
348 if (pvid->pending) {
\r
353 // preliminary FIFO emulation for Chaos Engine, The (E)
\r
354 if (!(pvid->status&8) && (pvid->reg[1]&0x40) && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display?
\r
356 pvid->status&=~0x200; // FIFO no longer empty
\r
357 pvid->lwrite_cnt++;
\r
358 if (pvid->lwrite_cnt >= 4) pvid->status|=0x100; // FIFO full
\r
359 if (pvid->lwrite_cnt > 4) {
\r
360 SekCyclesBurnRun(32); // penalty // 488/12-8
\r
362 elprintf(EL_ASVDP, "VDP data write: %04x [%06x] {%i} #%i @ %06x", d, Pico.video.addr,
\r
363 Pico.video.type, pvid->lwrite_cnt, SekPc);
\r
367 if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)
\r
373 if (a==0x04) // Control (command) port 4 or 6
\r
377 // Low word of command:
\r
378 pvid->command &= 0xffff0000;
\r
379 pvid->command |= d;
\r
390 if ((d&0xc000)==0x8000)
\r
393 int num=(d>>8)&0x1f;
\r
394 int dold=pvid->reg[num];
\r
396 pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)
\r
397 if (num > 0x0a && !(pvid->reg[1]&4)) {
\r
398 elprintf(EL_ANOMALY, "%02x written to reg %02x in SMS mode @ %06x", d, num, SekPc);
\r
402 if (num == 1 && !(d&0x40) && SekCyclesDone() - line_base_cycles <= 488-390)
\r
404 DrawSync(blank_on);
\r
405 pvid->reg[num]=(unsigned char)d;
\r
409 elprintf(EL_INTSW, "hint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x10)>>4,
\r
410 (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);
\r
413 elprintf(EL_INTSW, "vint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x20)>>5,
\r
414 (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);
\r
417 //elprintf(EL_STATUS, "spritep moved to %04x", (unsigned)(Pico.video.reg[5]&0x7f) << 9);
\r
418 if (d^dold) Pico.est.rendstatus |= PDRAW_SPRITES_MOVED;
\r
421 // renderers should update their palettes if sh/hi mode is changed
\r
422 if ((d^dold)&8) Pico.m.dirtyPal = 2;
\r
428 #ifndef EMU_CORE_DEBUG
\r
429 // update IRQ level
\r
430 if (!SekShouldInterrupt()) // hack
\r
432 int lines, pints, irq=0;
\r
433 lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);
\r
434 pints = (pvid->pending_ints&lines);
\r
435 if (pints & 0x20) irq = 6;
\r
436 else if (pints & 0x10) irq = 4;
\r
437 SekInterrupt(irq); // update line
\r
439 if (irq) SekEndRun(24); // make it delayed
\r
445 // High word of command:
\r
446 pvid->command&=0x0000ffff;
\r
447 pvid->command|=d<<16;
\r
454 PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
\r
458 if (a==0x04) // control port
\r
460 struct PicoVideo *pv=&Pico.video;
\r
463 //if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)
\r
464 if (SekCyclesDone() - line_base_cycles >= 488-88)
\r
465 d|=0x0004; // H-Blank (Sonic3 vs)
\r
467 d |= ((pv->reg[1]&0x40)^0x40) >> 3; // set V-Blank if display is disabled
\r
468 d |= (pv->pending_ints&0x20)<<2; // V-int pending?
\r
469 if (d&0x100) pv->status&=~0x100; // FIFO no longer full
\r
471 pv->pending = 0; // ctrl port reads clear write-pending flag (Charles MacDonald)
\r
473 elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);
\r
477 // H-counter info (based on Generator):
\r
479 // | <- hblank? -> |
\r
480 // start <416> hint <36> hdisplay <38> end // CPU cycles
\r
481 // |---------...---------|------------|-------------|
\r
482 // 0 B6 E4 FF // 40 cells
\r
483 // 0 93 E8 FF // 32 cells
\r
485 // Gens (?) v-render
\r
486 // start <hblank=84> hint hdisplay <404> |
\r
487 // |---------------------|--------------------------|
\r
488 // E4 (hc[0x43]==0) 07 B1 // 40
\r
489 // E8 (hc[0x45]==0) 05 91 // 32
\r
491 // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune
\r
492 if ((a&0x1c)==0x08)
\r
496 d = (SekCyclesDone() - line_base_cycles) & 0x1ff; // FIXME
\r
497 if (Pico.video.reg[12]&1)
\r
499 else d = hcounts_32[d];
\r
501 elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", d, Pico.video.v_counter, SekCyclesDone(), SekPc);
\r
502 return d | (Pico.video.v_counter << 8);
\r
505 if (a==0x00) // data port
\r
507 return VideoRead();
\r
513 unsigned int PicoVideoRead8(unsigned int a)
\r
520 case 0: return VideoRead() >> 8;
\r
521 case 1: return VideoRead() & 0xff;
\r
522 case 4: // control port/status reg
\r
523 d = Pico.video.status >> 8;
\r
524 if (d&1) Pico.video.status&=~0x100; // FIFO no longer full
\r
525 Pico.video.pending = 0;
\r
526 elprintf(EL_SR, "SR read (h): %02x @ %06x", d, SekPc);
\r
529 d = Pico.video.status & 0xff;
\r
530 //if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)
\r
531 d |= ((Pico.video.reg[1]&0x40)^0x40) >> 3; // set V-Blank if display is disabled
\r
532 d |= (Pico.video.pending_ints&0x20)<<2; // V-int pending?
\r
533 if (SekCyclesDone() - line_base_cycles >= 488-88) d |= 4; // H-Blank
\r
534 Pico.video.pending = 0;
\r
535 elprintf(EL_SR, "SR read (l): %02x @ %06x", d, SekPc);
\r
537 case 8: // hv counter
\r
538 elprintf(EL_HVCNT, "vcounter: %02x (%i) @ %06x", Pico.video.v_counter, SekCyclesDone(), SekPc);
\r
539 return Pico.video.v_counter;
\r
541 d = (SekCyclesDone() - line_base_cycles) & 0x1ff; // FIXME
\r
542 if (Pico.video.reg[12]&1)
\r
544 else d = hcounts_32[d];
\r
545 elprintf(EL_HVCNT, "hcounter: %02x (%i) @ %06x", d, SekCyclesDone(), SekPc);
\r
552 // vim:shiftwidth=2:ts=2:expandtab
\r