support for zipped ISOs
[picodrive.git] / Pico / VideoPort.c
CommitLineData
cc68a136 1// This is part of Pico Library\r
2\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
6\r
7// For commercial use, separate licencing terms must be obtained.\r
8\r
9\r
10#include "PicoInt.h"\r
fa1e5e29 11#include "cd/gfx_cd.h"\r
cc68a136 12\r
13extern const unsigned char hcounts_32[];\r
14extern const unsigned char hcounts_40[];\r
15extern const unsigned short vcounts[];\r
16extern int rendstatus;\r
17\r
ab0607f7 18typedef unsigned char u8;\r
cc68a136 19typedef unsigned short u16;\r
20\r
21\r
22static __inline void AutoIncrement()\r
23{\r
24 Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);\r
25}\r
26\r
27static void VideoWrite(u16 d)\r
28{\r
29 unsigned int a=Pico.video.addr;\r
30\r
31 switch (Pico.video.type)\r
32 {\r
33 case 1: if(a&1) d=(u16)((d<<8)|(d>>8)); // If address is odd, bytes are swapped (which game needs this?)\r
34 Pico.vram [(a>>1)&0x7fff]=d;\r
35 rendstatus|=0x10; break;\r
36 case 3: Pico.m.dirtyPal = 1;\r
37 //dprintf("w[%i] @ %04x, inc=%i [%i|%i]", Pico.video.type, a, Pico.video.reg[0xf], Pico.m.scanline, SekCyclesDone());\r
38 Pico.cram [(a>>1)&0x003f]=d; break; // wraps (Desert Strike)\r
39 case 5: Pico.vsram[(a>>1)&0x003f]=d; break;\r
40 }\r
41\r
42 //dprintf("w[%i] @ %04x, inc=%i [%i|%i]", Pico.video.type, a, Pico.video.reg[0xf], Pico.m.scanline, SekCyclesDone());\r
43 AutoIncrement();\r
44}\r
45\r
46static unsigned int VideoRead()\r
47{\r
48 unsigned int a=0,d=0;\r
49\r
50 a=Pico.video.addr; a>>=1;\r
51\r
52 switch (Pico.video.type)\r
53 {\r
54 case 0: d=Pico.vram [a&0x7fff]; break;\r
55 case 8: d=Pico.cram [a&0x003f]; break;\r
56 case 4: d=Pico.vsram[a&0x003f]; break;\r
57 }\r
4f672280 58\r
cc68a136 59 AutoIncrement();\r
60 return d;\r
61}\r
62\r
cc68a136 63static int GetDmaLength()\r
64{\r
65 struct PicoVideo *pvid=&Pico.video;\r
66 int len=0;\r
67 // 16-bit words to transfer:\r
68 len =pvid->reg[0x13];\r
69 len|=pvid->reg[0x14]<<8;\r
70 // Charles MacDonald:\r
71 if(!len) len = 0xffff;\r
72 return len;\r
73}\r
74\r
75static void DmaSlow(int len)\r
76{\r
77 u16 *pd=0, *pdend, *r;\r
78 unsigned int a=Pico.video.addr, a2, d;\r
79 unsigned char inc=Pico.video.reg[0xf];\r
672ad671 80 unsigned int source;\r
cc68a136 81\r
82 source =Pico.video.reg[0x15]<<1;\r
83 source|=Pico.video.reg[0x16]<<9;\r
84 source|=Pico.video.reg[0x17]<<17;\r
85\r
312e9ce1 86 dprintf("DmaSlow[%i] %06x->%04x len %i inc=%i blank %i [%i|%i] @ %x",\r
87 Pico.video.type, source, a, len, inc, (Pico.video.status&8)||!(Pico.video.reg[1]&0x40),\r
88 Pico.m.scanline, SekCyclesDone(), SekPc);\r
cc68a136 89\r
672ad671 90 if(Pico.m.scanline != -1) {\r
91 Pico.m.dma_bytes += len;\r
92 SekSetCyclesLeft(SekCyclesLeft - CheckDMA());\r
93 } else {\r
94 // be approximate in non-accurate mode\r
95 SekSetCyclesLeft(SekCyclesLeft - (len*(((488<<8)/167))>>8));\r
96 }\r
97\r
ab0607f7 98 if ((source&0xe00000)==0xe00000) { // Ram\r
99 pd=(u16 *)(Pico.ram+(source&0xfffe));\r
100 pdend=(u16 *)(Pico.ram+0x10000);\r
101 } else if(PicoMCD & 1) {\r
fa1e5e29 102 dprintf("DmaSlow CD, r3=%02x", Pico_mcd->s68k_regs[3]);\r
ab0607f7 103 if(source<0x20000) { // Bios area\r
104 pd=(u16 *)(Pico_mcd->bios+(source&~1));\r
105 pdend=(u16 *)(Pico_mcd->bios+0x20000);\r
fa1e5e29 106 } else if ((source&0xfc0000)==0x200000) { // Word Ram\r
107 source -= 2;\r
ab0607f7 108 if (!(Pico_mcd->s68k_regs[3]&4)) { // 2M mode\r
fa1e5e29 109 pd=(u16 *)(Pico_mcd->word_ram2M+(source&0x3fffe));\r
110 pdend=(u16 *)(Pico_mcd->word_ram2M+0x40000);\r
ab0607f7 111 } else {\r
fa1e5e29 112 if (source < 0x220000) { // 1M mode\r
113 int bank = Pico_mcd->s68k_regs[3]&1;\r
114 pd=(u16 *)(Pico_mcd->word_ram1M[bank]+(source&0x1fffe));\r
115 pdend=(u16 *)(Pico_mcd->word_ram1M[bank]+0x20000);\r
116 } else {\r
117 DmaSlowCell(source, a, len, inc);\r
118 return;\r
119 }\r
ab0607f7 120 }\r
121 } else if ((source&0xfe0000)==0x020000) { // Prg Ram\r
122 u8 *prg_ram = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];\r
123 pd=(u16 *)(prg_ram+(source&0x1fffe));\r
124 pdend=(u16 *)(prg_ram+0x20000);\r
125 } else {\r
fa1e5e29 126 dprintf("DmaSlow FIXME: unsupported src");\r
ab0607f7 127 return;\r
128 }\r
672ad671 129 } else {\r
ab0607f7 130 if(source<Pico.romsize) { // Rom\r
131 pd=(u16 *)(Pico.rom+(source&~1));\r
132 pdend=(u16 *)(Pico.rom+Pico.romsize);\r
133 } else {\r
134 dprintf("DmaSlow: invalid dma src");\r
135 return;\r
136 }\r
137 }\r
138\r
139 // overflow protection, might break something..\r
140 if (len > pdend - pd) {\r
141 len = pdend - pd;\r
142 dprintf("DmaSlow overflow");\r
672ad671 143 }\r
cc68a136 144\r
145 switch (Pico.video.type)\r
146 {\r
147 case 1: // vram\r
148 r = Pico.vram;\r
cea65903 149 if (inc == 2 && !(a&1) && a+len*2 < 0x10000)\r
cc68a136 150 {\r
cea65903 151 // most used DMA mode\r
fa1e5e29 152 memcpy16(r + (a>>1), pd, len);\r
153 a += len*2;\r
cea65903 154 }\r
155 else\r
156 {\r
157 for(; len; len--)\r
158 {\r
159 d=*pd++;\r
160 if(a&1) d=(d<<8)|(d>>8);\r
161 r[a>>1] = (u16)d; // will drop the upper bits\r
162 // AutoIncrement\r
163 a=(u16)(a+inc);\r
164 // didn't src overlap?\r
165 //if(pd >= pdend) pd-=0x8000; // should be good for RAM, bad for ROM\r
166 }\r
cc68a136 167 }\r
168 rendstatus|=0x10;\r
169 break;\r
4f672280 170\r
cc68a136 171 case 3: // cram\r
cc68a136 172 Pico.m.dirtyPal = 1;\r
173 r = Pico.cram;\r
174 for(a2=a&0x7f; len; len--)\r
175 {\r
176 r[a2>>1] = (u16)*pd++;; // bit 0 is ignored\r
177 // AutoIncrement\r
178 a2+=inc;\r
179 // didn't src overlap?\r
ab0607f7 180 //if(pd >= pdend) pd-=0x8000;\r
cc68a136 181 // good dest?\r
182 if(a2 >= 0x80) break; // Todds Adventures in Slime World / Andre Agassi tennis\r
183 }\r
184 a=(a&0xff00)|a2;\r
185 break;\r
186\r
187 case 5: // vsram[a&0x003f]=d;\r
188 r = Pico.vsram;\r
189 for(a2=a&0x7f; len; len--)\r
190 {\r
191 r[a2>>1] = (u16)*pd++;\r
192 // AutoIncrement\r
193 a2+=inc;\r
194 // didn't src overlap?\r
ab0607f7 195 //if(pd >= pdend) pd-=0x8000;\r
cc68a136 196 // good dest?\r
197 if(a2 >= 0x80) break;\r
198 }\r
199 a=(a&0xff00)|a2;\r
200 break;\r
201 }\r
202 // remember addr\r
203 Pico.video.addr=(u16)a;\r
204}\r
205\r
206static void DmaCopy(int len)\r
207{\r
208 u16 a=Pico.video.addr;\r
209 unsigned char *vr = (unsigned char *) Pico.vram;\r
210 unsigned char *vrs;\r
211 unsigned char inc=Pico.video.reg[0xf];\r
212 int source;\r
4f672280 213 dprintf("DmaCopy len %i [%i|%i]", len, Pico.m.scanline, SekCyclesDone());\r
214\r
215 Pico.m.dma_bytes += len;\r
8c1952f0 216 if(Pico.m.scanline != -1)\r
217 Pico.video.status|=2; // dma busy\r
cc68a136 218\r
219 source =Pico.video.reg[0x15];\r
220 source|=Pico.video.reg[0x16]<<8;\r
221 vrs=vr+source;\r
222\r
223 if(source+len > 0x10000) len=0x10000-source; // clip??\r
224\r
225 for(;len;len--)\r
226 {\r
227 vr[a] = *vrs++;\r
228 // AutoIncrement\r
229 a=(u16)(a+inc);\r
230 }\r
231 // remember addr\r
232 Pico.video.addr=a;\r
233 rendstatus|=0x10;\r
234}\r
235\r
236// check: Contra, Megaman\r
237// note: this is still inaccurate\r
238static void DmaFill(int data)\r
239{\r
240 int len;\r
241 unsigned short a=Pico.video.addr;\r
242 unsigned char *vr=(unsigned char *) Pico.vram;\r
243 unsigned char high = (unsigned char) (data >> 8);\r
244 unsigned char inc=Pico.video.reg[0xf];\r
4f672280 245\r
cc68a136 246 len=GetDmaLength();\r
4f672280 247 dprintf("DmaFill len %i inc %i [%i|%i]", len, inc, Pico.m.scanline, SekCyclesDone());\r
248\r
249 Pico.m.dma_bytes += len;\r
8c1952f0 250 if(Pico.m.scanline != -1)\r
251 Pico.video.status|=2; // dma busy (in accurate mode)\r
cc68a136 252\r
253 // from Charles MacDonald's genvdp.txt:\r
254 // Write lower byte to address specified\r
255 vr[a] = (unsigned char) data;\r
256 a=(u16)(a+inc);\r
257\r
258 if(!inc) len=1;\r
259\r
260 for(;len;len--) {\r
261 // Write upper byte to adjacent address\r
262 // (here we are byteswapped, so address is already 'adjacent')\r
263 vr[a] = high;\r
264\r
265 // Increment address register\r
266 a=(u16)(a+inc);\r
267 }\r
268 // remember addr\r
269 Pico.video.addr=a;\r
270 // update length\r
271 Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0; // Dino Dini's Soccer (E) (by Haze)\r
272\r
273 rendstatus|=0x10;\r
274}\r
275\r
276static void CommandDma()\r
277{\r
278 struct PicoVideo *pvid=&Pico.video;\r
279 int len=0,method=0;\r
280\r
281 if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled\r
282\r
283 len=GetDmaLength();\r
284\r
285 method=pvid->reg[0x17]>>6;\r
286 if (method< 2) DmaSlow(len); // 68000 to VDP\r
287 if (method==3) DmaCopy(len); // VRAM Copy\r
288}\r
289\r
290static void CommandChange()\r
291{\r
292 struct PicoVideo *pvid=&Pico.video;\r
293 unsigned int cmd=0,addr=0;\r
294\r
295 cmd=pvid->command;\r
296\r
297 // Get type of transfer 0xc0000030 (v/c/vsram read/write)\r
298 pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));\r
299\r
300 // Get address 0x3fff0003\r
301 addr =(cmd>>16)&0x3fff;\r
302 addr|=(cmd<<14)&0xc000;\r
303 pvid->addr=(unsigned short)addr;\r
304 //dprintf("addr set: %04x", addr);\r
305\r
306 // Check for dma:\r
307 if (cmd&0x80) CommandDma();\r
308}\r
309\r
310void PicoVideoWrite(unsigned int a,unsigned short d)\r
311{\r
312 struct PicoVideo *pvid=&Pico.video;\r
313\r
314 a&=0x1c;\r
315\r
316 if (a==0x00) // Data port 0 or 2\r
4f672280 317 {\r
cc68a136 318 if (pvid->pending) CommandChange();\r
319 pvid->pending=0;\r
320\r
321 // If a DMA fill has been set up, do it\r
322 if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)\r
323 {\r
324 DmaFill(d);\r
325 }\r
326 else\r
327 {\r
328 VideoWrite(d);\r
329 }\r
330 return;\r
331 }\r
332\r
333 if (a==0x04) // Control (command) port 4 or 6\r
334 {\r
335 //dprintf("vdp cw(%04x), p=%i @ %06x [%i]", d, pvid->pending, SekPc, SekCyclesDone());\r
336 if(pvid->pending)\r
337 {\r
338 // Low word of command:\r
339 pvid->command&=0xffff0000;\r
340 pvid->command|=d;\r
341 pvid->pending=0;\r
342 CommandChange();\r
343 } else {\r
344 if((d&0xc000)==0x8000)\r
345 {\r
346 // Register write:\r
347 int num=(d>>8)&0x1f;\r
312e9ce1 348 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
349 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
cc68a136 350 //if(num==01) dprintf("set_blank: %i @ %06x [%i|%i]", !((d&0x40)>>6), SekPc, Pico.m.scanline, SekCyclesDone());\r
351 //if(num==05) dprintf("spr_set: %i @ %06x [%i|%i]", (unsigned char)d, SekPc, Pico.m.scanline, SekCyclesDone());\r
352 //if(num==10) dprintf("hint_set: %i @ %06x [%i|%i]", (unsigned char)d, SekPc, Pico.m.scanline, SekCyclesDone());\r
353 pvid->reg[num]=(unsigned char)d;\r
354#if !(defined(EMU_C68K) && defined(EMU_M68K)) // not debugging Cyclone\r
355 // update IRQ level (Lemmings, Wiz 'n' Liz intro, ... )\r
356 // may break if done improperly:\r
357 // International Superstar Soccer Deluxe (crash), Street Racer (logos), Burning Force (gfx), Fatal Rewind (hang), Sesame Street Counting Cafe\r
358 if(num < 2) {\r
359#ifdef EMU_C68K\r
360 // hack: make sure we do not touch the irq line if Cyclone is just about to take the IRQ\r
361 if (PicoCpu.irq <= (PicoCpu.srh&7)) {\r
362#endif\r
363 int lines, pints;\r
364 lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);\r
365 pints = (pvid->pending_ints&lines);\r
366 if(pints & 0x20) SekInterrupt(6);\r
367 else if(pints & 0x10) SekInterrupt(4);\r
368 else SekInterrupt(0);\r
369#ifdef EMU_C68K\r
370 // adjust cycles for Cyclone so it would take the int "in time"\r
371 if(PicoCpu.irq) {\r
372 SekEndRun(24);\r
373 }\r
374 }\r
375#endif\r
376 }\r
377 else\r
378#endif\r
379 if(num == 5) rendstatus|=1;\r
380 else if(num == 0xc) Pico.m.dirtyPal = 2; // renderers should update their palettes if sh/hi mode is changed\r
381 pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)\r
382 } else {\r
383 // High word of command:\r
384 pvid->command&=0x0000ffff;\r
385 pvid->command|=d<<16;\r
386 pvid->pending=1;\r
387 }\r
388 }\r
389 }\r
390}\r
391\r
392unsigned int PicoVideoRead(unsigned int a)\r
393{\r
394 unsigned int d=0;\r
4f672280 395\r
cc68a136 396 a&=0x1c;\r
397\r
398 if (a==0x00) // data port\r
399 {\r
400 d=VideoRead();\r
401 goto end;\r
402 }\r
403\r
404 if (a==0x04) // control port\r
405 {\r
cc68a136 406 d=Pico.video.status;\r
407 if(PicoOpt&0x10) d|=0x0020; // sprite collision (Shadow of the Beast)\r
408 if(Pico.m.rotate++&8) d|=0x0100; else d|=0x0200; // Toggle fifo full empty (who uses that stuff?)\r
409 if(!(Pico.video.reg[1]&0x40)) d|=0x0008; // set V-Blank if display is disabled\r
410 if(SekCyclesLeft < 84+4) d|=0x0004; // H-Blank (Sonic3 vs)\r
bf098bc5 411 // dprintf("sr_read %04x @ %06x [%i|%i]", d, SekPc, Pico.m.scanline, SekCyclesDone());\r
cc68a136 412\r
413 Pico.video.pending=0; // ctrl port reads clear write-pending flag (Charles MacDonald)\r
414\r
415 goto end;\r
416 }\r
417\r
418 // H-counter info (based on Generator):\r
419 // frame:\r
420 // | <- hblank? -> |\r
421 // start <416> hint <36> hdisplay <38> end // CPU cycles\r
422 // |---------...---------|------------|-------------|\r
423 // 0 B6 E4 FF // 40 cells\r
424 // 0 93 E8 FF // 32 cells\r
425\r
426 // Gens (?) v-render\r
427 // start <hblank=84> hint hdisplay <404> |\r
428 // |---------------------|--------------------------|\r
429 // E4 (hc[0x43]==0) 07 B1 // 40\r
430 // E8 (hc[0x45]==0) 05 91 // 32\r
4f672280 431\r
cc68a136 432 // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune\r
433 if ((a&0x1c)==0x08)\r
434 {\r
435 unsigned int hc;\r
436\r
437 if(Pico.m.scanline != -1) {\r
438 int lineCycles=(488-SekCyclesLeft)&0x1ff;\r
439 d=Pico.m.scanline; // V-Counter\r
440\r
441 if(Pico.video.reg[12]&1)\r
442 hc=hcounts_40[lineCycles];\r
443 else hc=hcounts_32[lineCycles];\r
444\r
8c1952f0 445 //if(lineCycles > 488-12) d++; // Wheel of Fortune\r
cc68a136 446 } else {\r
447 // get approximate V-Counter\r
448 d=vcounts[SekCyclesDone()>>8];\r
449 hc = Pico.m.rotate&0xff;\r
450 }\r
451\r
452 if(Pico.m.pal) {\r
453 if(d >= 0x103) d-=56; // based on Gens\r
454 } else {\r
455 if(d >= 0xEB) d-=6;\r
456 }\r
457\r
458 if((Pico.video.reg[12]&6) == 6) {\r
459 // interlace mode 2 (Combat Cars (UE) [!])\r
460 d <<= 1;\r
461 if (d&0xf00) d|= 1;\r
462 }\r
463\r
4f672280 464 dprintf("hv: %02x %02x (%i) @ %06x", hc, d, SekCyclesDone(), SekPc);\r
cc68a136 465 d&=0xff; d<<=8;\r
466 d|=hc;\r
467 goto end;\r
468 }\r
469\r
470end:\r
471\r
472 return d;\r
473}\r