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