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