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