menu bg, pc linux build
[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
142 for(; len; len--)\r
143 {\r
144 d=*pd++;\r
145 if(a&1) d=(d<<8)|(d>>8);\r
146 r[a>>1] = (u16)d; // will drop the upper bits\r
147 // AutoIncrement\r
148 a=(u16)(a+inc);\r
149 // didn't src overlap?\r
ab0607f7 150 //if(pd >= pdend) pd-=0x8000; // should be good for RAM, bad for ROM\r
cc68a136 151 }\r
152 rendstatus|=0x10;\r
153 break;\r
4f672280 154\r
cc68a136 155 case 3: // cram\r
cc68a136 156 Pico.m.dirtyPal = 1;\r
157 r = Pico.cram;\r
158 for(a2=a&0x7f; len; len--)\r
159 {\r
160 r[a2>>1] = (u16)*pd++;; // bit 0 is ignored\r
161 // AutoIncrement\r
162 a2+=inc;\r
163 // didn't src overlap?\r
ab0607f7 164 //if(pd >= pdend) pd-=0x8000;\r
cc68a136 165 // good dest?\r
166 if(a2 >= 0x80) break; // Todds Adventures in Slime World / Andre Agassi tennis\r
167 }\r
168 a=(a&0xff00)|a2;\r
169 break;\r
170\r
171 case 5: // vsram[a&0x003f]=d;\r
172 r = Pico.vsram;\r
173 for(a2=a&0x7f; len; len--)\r
174 {\r
175 r[a2>>1] = (u16)*pd++;\r
176 // AutoIncrement\r
177 a2+=inc;\r
178 // didn't src overlap?\r
ab0607f7 179 //if(pd >= pdend) pd-=0x8000;\r
cc68a136 180 // good dest?\r
181 if(a2 >= 0x80) break;\r
182 }\r
183 a=(a&0xff00)|a2;\r
184 break;\r
185 }\r
186 // remember addr\r
187 Pico.video.addr=(u16)a;\r
188}\r
189\r
190static void DmaCopy(int len)\r
191{\r
192 u16 a=Pico.video.addr;\r
193 unsigned char *vr = (unsigned char *) Pico.vram;\r
194 unsigned char *vrs;\r
195 unsigned char inc=Pico.video.reg[0xf];\r
196 int source;\r
4f672280 197 dprintf("DmaCopy len %i [%i|%i]", len, Pico.m.scanline, SekCyclesDone());\r
198\r
199 Pico.m.dma_bytes += len;\r
8c1952f0 200 if(Pico.m.scanline != -1)\r
201 Pico.video.status|=2; // dma busy\r
cc68a136 202\r
203 source =Pico.video.reg[0x15];\r
204 source|=Pico.video.reg[0x16]<<8;\r
205 vrs=vr+source;\r
206\r
207 if(source+len > 0x10000) len=0x10000-source; // clip??\r
208\r
209 for(;len;len--)\r
210 {\r
211 vr[a] = *vrs++;\r
212 // AutoIncrement\r
213 a=(u16)(a+inc);\r
214 }\r
215 // remember addr\r
216 Pico.video.addr=a;\r
217 rendstatus|=0x10;\r
218}\r
219\r
220// check: Contra, Megaman\r
221// note: this is still inaccurate\r
222static void DmaFill(int data)\r
223{\r
224 int len;\r
225 unsigned short a=Pico.video.addr;\r
226 unsigned char *vr=(unsigned char *) Pico.vram;\r
227 unsigned char high = (unsigned char) (data >> 8);\r
228 unsigned char inc=Pico.video.reg[0xf];\r
4f672280 229\r
cc68a136 230 len=GetDmaLength();\r
4f672280 231 dprintf("DmaFill len %i inc %i [%i|%i]", len, inc, Pico.m.scanline, SekCyclesDone());\r
232\r
233 Pico.m.dma_bytes += len;\r
8c1952f0 234 if(Pico.m.scanline != -1)\r
235 Pico.video.status|=2; // dma busy (in accurate mode)\r
cc68a136 236\r
237 // from Charles MacDonald's genvdp.txt:\r
238 // Write lower byte to address specified\r
239 vr[a] = (unsigned char) data;\r
240 a=(u16)(a+inc);\r
241\r
242 if(!inc) len=1;\r
243\r
244 for(;len;len--) {\r
245 // Write upper byte to adjacent address\r
246 // (here we are byteswapped, so address is already 'adjacent')\r
247 vr[a] = high;\r
248\r
249 // Increment address register\r
250 a=(u16)(a+inc);\r
251 }\r
252 // remember addr\r
253 Pico.video.addr=a;\r
254 // update length\r
255 Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0; // Dino Dini's Soccer (E) (by Haze)\r
256\r
257 rendstatus|=0x10;\r
258}\r
259\r
260static void CommandDma()\r
261{\r
262 struct PicoVideo *pvid=&Pico.video;\r
263 int len=0,method=0;\r
264\r
265 if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled\r
266\r
267 len=GetDmaLength();\r
268\r
269 method=pvid->reg[0x17]>>6;\r
270 if (method< 2) DmaSlow(len); // 68000 to VDP\r
271 if (method==3) DmaCopy(len); // VRAM Copy\r
272}\r
273\r
274static void CommandChange()\r
275{\r
276 struct PicoVideo *pvid=&Pico.video;\r
277 unsigned int cmd=0,addr=0;\r
278\r
279 cmd=pvid->command;\r
280\r
281 // Get type of transfer 0xc0000030 (v/c/vsram read/write)\r
282 pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));\r
283\r
284 // Get address 0x3fff0003\r
285 addr =(cmd>>16)&0x3fff;\r
286 addr|=(cmd<<14)&0xc000;\r
287 pvid->addr=(unsigned short)addr;\r
288 //dprintf("addr set: %04x", addr);\r
289\r
290 // Check for dma:\r
291 if (cmd&0x80) CommandDma();\r
292}\r
293\r
294void PicoVideoWrite(unsigned int a,unsigned short d)\r
295{\r
296 struct PicoVideo *pvid=&Pico.video;\r
297\r
298 a&=0x1c;\r
299\r
300 if (a==0x00) // Data port 0 or 2\r
4f672280 301 {\r
cc68a136 302 if (pvid->pending) CommandChange();\r
303 pvid->pending=0;\r
304\r
305 // If a DMA fill has been set up, do it\r
306 if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)\r
307 {\r
308 DmaFill(d);\r
309 }\r
310 else\r
311 {\r
312 VideoWrite(d);\r
313 }\r
314 return;\r
315 }\r
316\r
317 if (a==0x04) // Control (command) port 4 or 6\r
318 {\r
319 //dprintf("vdp cw(%04x), p=%i @ %06x [%i]", d, pvid->pending, SekPc, SekCyclesDone());\r
320 if(pvid->pending)\r
321 {\r
322 // Low word of command:\r
323 pvid->command&=0xffff0000;\r
324 pvid->command|=d;\r
325 pvid->pending=0;\r
326 CommandChange();\r
327 } else {\r
328 if((d&0xc000)==0x8000)\r
329 {\r
330 // Register write:\r
331 int num=(d>>8)&0x1f;\r
312e9ce1 332 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
333 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 334 //if(num==01) dprintf("set_blank: %i @ %06x [%i|%i]", !((d&0x40)>>6), SekPc, Pico.m.scanline, SekCyclesDone());\r
335 //if(num==05) dprintf("spr_set: %i @ %06x [%i|%i]", (unsigned char)d, SekPc, Pico.m.scanline, SekCyclesDone());\r
336 //if(num==10) dprintf("hint_set: %i @ %06x [%i|%i]", (unsigned char)d, SekPc, Pico.m.scanline, SekCyclesDone());\r
337 pvid->reg[num]=(unsigned char)d;\r
338#if !(defined(EMU_C68K) && defined(EMU_M68K)) // not debugging Cyclone\r
339 // update IRQ level (Lemmings, Wiz 'n' Liz intro, ... )\r
340 // may break if done improperly:\r
341 // International Superstar Soccer Deluxe (crash), Street Racer (logos), Burning Force (gfx), Fatal Rewind (hang), Sesame Street Counting Cafe\r
342 if(num < 2) {\r
343#ifdef EMU_C68K\r
344 // hack: make sure we do not touch the irq line if Cyclone is just about to take the IRQ\r
345 if (PicoCpu.irq <= (PicoCpu.srh&7)) {\r
346#endif\r
347 int lines, pints;\r
348 lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);\r
349 pints = (pvid->pending_ints&lines);\r
350 if(pints & 0x20) SekInterrupt(6);\r
351 else if(pints & 0x10) SekInterrupt(4);\r
352 else SekInterrupt(0);\r
353#ifdef EMU_C68K\r
354 // adjust cycles for Cyclone so it would take the int "in time"\r
355 if(PicoCpu.irq) {\r
356 SekEndRun(24);\r
357 }\r
358 }\r
359#endif\r
360 }\r
361 else\r
362#endif\r
363 if(num == 5) rendstatus|=1;\r
364 else if(num == 0xc) Pico.m.dirtyPal = 2; // renderers should update their palettes if sh/hi mode is changed\r
365 pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)\r
366 } else {\r
367 // High word of command:\r
368 pvid->command&=0x0000ffff;\r
369 pvid->command|=d<<16;\r
370 pvid->pending=1;\r
371 }\r
372 }\r
373 }\r
374}\r
375\r
376unsigned int PicoVideoRead(unsigned int a)\r
377{\r
378 unsigned int d=0;\r
4f672280 379\r
cc68a136 380 a&=0x1c;\r
381\r
382 if (a==0x00) // data port\r
383 {\r
384 d=VideoRead();\r
385 goto end;\r
386 }\r
387\r
388 if (a==0x04) // control port\r
389 {\r
cc68a136 390 d=Pico.video.status;\r
391 if(PicoOpt&0x10) d|=0x0020; // sprite collision (Shadow of the Beast)\r
392 if(Pico.m.rotate++&8) d|=0x0100; else d|=0x0200; // Toggle fifo full empty (who uses that stuff?)\r
393 if(!(Pico.video.reg[1]&0x40)) d|=0x0008; // set V-Blank if display is disabled\r
394 if(SekCyclesLeft < 84+4) d|=0x0004; // H-Blank (Sonic3 vs)\r
bf098bc5 395 // dprintf("sr_read %04x @ %06x [%i|%i]", d, SekPc, Pico.m.scanline, SekCyclesDone());\r
cc68a136 396\r
397 Pico.video.pending=0; // ctrl port reads clear write-pending flag (Charles MacDonald)\r
398\r
399 goto end;\r
400 }\r
401\r
402 // H-counter info (based on Generator):\r
403 // frame:\r
404 // | <- hblank? -> |\r
405 // start <416> hint <36> hdisplay <38> end // CPU cycles\r
406 // |---------...---------|------------|-------------|\r
407 // 0 B6 E4 FF // 40 cells\r
408 // 0 93 E8 FF // 32 cells\r
409\r
410 // Gens (?) v-render\r
411 // start <hblank=84> hint hdisplay <404> |\r
412 // |---------------------|--------------------------|\r
413 // E4 (hc[0x43]==0) 07 B1 // 40\r
414 // E8 (hc[0x45]==0) 05 91 // 32\r
4f672280 415\r
cc68a136 416 // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune\r
417 if ((a&0x1c)==0x08)\r
418 {\r
419 unsigned int hc;\r
420\r
421 if(Pico.m.scanline != -1) {\r
422 int lineCycles=(488-SekCyclesLeft)&0x1ff;\r
423 d=Pico.m.scanline; // V-Counter\r
424\r
425 if(Pico.video.reg[12]&1)\r
426 hc=hcounts_40[lineCycles];\r
427 else hc=hcounts_32[lineCycles];\r
428\r
8c1952f0 429 //if(lineCycles > 488-12) d++; // Wheel of Fortune\r
cc68a136 430 } else {\r
431 // get approximate V-Counter\r
432 d=vcounts[SekCyclesDone()>>8];\r
433 hc = Pico.m.rotate&0xff;\r
434 }\r
435\r
436 if(Pico.m.pal) {\r
437 if(d >= 0x103) d-=56; // based on Gens\r
438 } else {\r
439 if(d >= 0xEB) d-=6;\r
440 }\r
441\r
442 if((Pico.video.reg[12]&6) == 6) {\r
443 // interlace mode 2 (Combat Cars (UE) [!])\r
444 d <<= 1;\r
445 if (d&0xf00) d|= 1;\r
446 }\r
447\r
4f672280 448 dprintf("hv: %02x %02x (%i) @ %06x", hc, d, SekCyclesDone(), SekPc);\r
cc68a136 449 d&=0xff; d<<=8;\r
450 d|=hc;\r
451 goto end;\r
452 }\r
453\r
454end:\r
455\r
456 return d;\r
457}\r