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