gfx_cd optimization wip, line glitch fixed
[picodrive.git] / Pico / cd / gfx_cd.c
CommitLineData
cb4a513a 1// TODO...
2
51a902ae 3// #include <string.h>
cb4a513a 4#include "../PicoInt.h"
5
d1df8786 6#define rot_comp Pico_mcd->rot_comp
7
75736070 8static const int Table_Rot_Time[] =
d1df8786 9{
10 0x00054000, 0x00048000, 0x00040000, 0x00036000, //; 008-032 ; briefing - sprite
11 0x0002E000, 0x00028000, 0x00024000, 0x00022000, //; 036-064 ; arbre souvent
12 0x00021000, 0x00020000, 0x0001E000, 0x0001B800, //; 068-096 ; map thunderstrike
13 0x00019800, 0x00017A00, 0x00015C00, 0x00013E00, //; 100-128 ; logo défoncé
14
15 0x00012000, 0x00011800, 0x00011000, 0x00010800, //; 132-160 ; briefing - map
16 0x00010000, 0x0000F800, 0x0000F000, 0x0000E800, //; 164-192
17 0x0000E000, 0x0000D800, 0x0000D000, 0x0000C800, //; 196-224
18 0x0000C000, 0x0000B800, 0x0000B000, 0x0000A800, //; 228-256 ; batman visage
19
20 0x0000A000, 0x00009F00, 0x00009E00, 0x00009D00, //; 260-288
21 0x00009C00, 0x00009B00, 0x00009A00, 0x00009900, //; 292-320
22 0x00009800, 0x00009700, 0x00009600, 0x00009500, //; 324-352
23 0x00009400, 0x00009300, 0x00009200, 0x00009100, //; 356-384
24
25 0x00009000, 0x00008F00, 0x00008E00, 0x00008D00, //; 388-416
26 0x00008C00, 0x00008B00, 0x00008A00, 0x00008900, //; 420-448
27 0x00008800, 0x00008700, 0x00008600, 0x00008500, //; 452-476
28 0x00008400, 0x00008300, 0x00008200, 0x00008100, //; 480-512
29};
30
31
cb4a513a 32static void gfx_cd_start(void)
33{
d1df8786 34 int upd_len;
35
36 dprintf("gfx_cd_start()");
37
528ec956 38 // rot_comp.XD_Mul = ((rot_comp.Reg_5C & 0x1f) + 1) * 4; // unused
a4030801 39 rot_comp.Function = (rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18); // Jmp_Adr
528ec956 40 // rot_comp.Buffer_Adr = (rot_comp.Reg_5E & 0xfff8) << 2; // unused?
a4030801 41 rot_comp.YD = (rot_comp.Reg_60 >> 3) & 7;
42 rot_comp.Vector_Adr = (rot_comp.Reg_66 & 0xfffe) << 2;
43
d1df8786 44 upd_len = (rot_comp.Reg_62 >> 3) & 0x3f;
45 upd_len = Table_Rot_Time[upd_len];
d1df8786 46 rot_comp.Draw_Speed = rot_comp.Float_Part = upd_len;
47
48 rot_comp.Reg_58 |= 0x8000; // Stamp_Size, we start a new GFX operation
49
a4030801 50 switch (rot_comp.Reg_58 & 6) // Scr_16?
51 {
52 case 0: // ?
53 rot_comp.Stamp_Map_Adr = (rot_comp.Reg_5A & 0xff80) << 2;
54 break;
55 case 2: // .Dot_32
56 rot_comp.Stamp_Map_Adr = (rot_comp.Reg_5A & 0xffe0) << 2;
57 break;
58 case 4: // .Scr_16
59 rot_comp.Stamp_Map_Adr = 0x20000;
60 break;
61 case 6: // .Scr_16_Dot_32
62 rot_comp.Stamp_Map_Adr = (rot_comp.Reg_5A & 0xe000) << 2;
63 break;
64 }
65
d1df8786 66 gfx_cd_update();
67}
68
69
70static void gfx_completed(void)
71{
72 rot_comp.Reg_58 &= 0x7fff; // Stamp_Size
73 rot_comp.Reg_64 = 0;
cb4a513a 74 if (Pico_mcd->s68k_regs[0x33] & (1<<1))
75 {
76 dprintf("gfx_cd irq 1");
77 SekInterruptS68k(1);
78 }
79}
80
d1df8786 81
a4030801 82static void gfx_do(void)
83{
528ec956 84 unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
85 unsigned int XD, Buffer_Adr, H_Dot;
a4030801 86 unsigned int func = rot_comp.Function;
528ec956 87 unsigned short *stamp_base;
88 int DYXS;
a4030801 89
528ec956 90 XD = rot_comp.Reg_60 & 7;
91 Buffer_Adr = ((rot_comp.Reg_5E & 0xfff8) + rot_comp.YD) << 2;
92 stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + rot_comp.Stamp_Map_Adr);
93 H_Dot = rot_comp.Reg_62 & 0x1ff;
a4030801 94 ecx = *(unsigned int *)(Pico_mcd->word_ram2M + rot_comp.Vector_Adr);
95 edx = ecx >> 16;
96 ecx = (ecx & 0xffff) << 8;
97 edx <<= 8;
528ec956 98 DYXS = *(int *)(Pico_mcd->word_ram2M + rot_comp.Vector_Adr + 4);
a4030801 99 rot_comp.Vector_Adr += 8;
a4030801 100
101 // MAKE_IMAGE_LINE
528ec956 102 while (H_Dot)
a4030801 103 {
104 if (func & 2) // mode 32x32 dot
105 {
106 if (func & 4) // 16x16 screen
107 {
01bc6b19 108 eax = (ecx >> (11+5)) & 0x007f;
a4030801 109 ebx = (edx >> (11-2)) & 0x3f80;
110 }
111 else // 1x1 screen
112 {
01bc6b19 113 eax = (ecx >> (11+5)) & 0x07;
a4030801 114 ebx = (edx >> (11+2)) & 0x38;
115 }
116 }
117 else // mode 16x16 dot
118 {
119 if (func & 4) // 16x16 screen
120 {
01bc6b19 121 eax = (ecx >> (11+4)) & 0x00ff;
a4030801 122 ebx = (edx >> (11-4)) & 0xff00;
123 }
124 else // 1x1 screen
125 {
126 eax = (ecx >> (11+4)) & 0x0f;
127 ebx = (edx >> (11+0)) & 0xf0;
128 }
129 }
130 ebx += eax;
131
a4030801 132 // MAKE_IMAGE_PIXEL
133 if (!(func & 1)) // NOT TILED
134 {
528ec956 135 int mask = (func & 4) ? 0x00800000 : 0x00f80000;
136 if ((ecx | edx) & mask)
a4030801 137 {
528ec956 138 if (func & 0x18) goto Next_Pixel;
139 pixel = 0;
140 goto Pixel_Out;
a4030801 141 }
142 }
528ec956 143
144 // esi = rot_comp.Stamp_Map_Adr;
145 edi = stamp_base[ebx] | (stamp_base[ebx+1] << 16);
a4030801 146 esi = edi;
147 edi >>= (11+1);
148 esi = (esi & 0x7ff) << 7;
528ec956 149 if (!esi) { pixel = 0; goto Pixel_Out; }
a4030801 150 edi &= (0x1c>>1);
151 eax = ecx;
152 ebx = edx;
153 if (func & 2) edi |= 1; // 32 dots?
154 switch (edi)
155 {
156 case 0x00: // No_Flip_0, 16x16 dots
157 ebx = (ebx >> 9) & 0x3c;
158 ebx += esi;
159 edi = (eax & 0x3800) ^ 0x1000; // bswap
160 eax = ((eax >> 8) & 0x40) + ebx;
a4030801 161 break;
162 case 0x01: // No_Flip_0, 32x32 dots
163 ebx = (ebx >> 9) & 0x7c;
164 ebx += esi;
165 edi = (eax & 0x3800) ^ 0x1000; // bswap
166 eax = ((eax >> 7) & 0x180) + ebx;
a4030801 167 break;
168 case 0x02: // No_Flip_90, 16x16 dots
169 eax = (eax >> 9) & 0x3c;
170 eax += esi;
171 edi = (ebx & 0x3800) ^ 0x2800; // bswap
172 eax += ((ebx >> 8) & 0x40) ^ 0x40;
a4030801 173 break;
174 case 0x03: // No_Flip_90, 32x32 dots
175 eax = (eax >> 9) & 0x7c;
176 eax += esi;
177 edi = (ebx & 0x3800) ^ 0x2800; // bswap
01bc6b19 178 eax += ((ebx >> 7) & 0x180) ^ 0x180;
a4030801 179 break;
180 case 0x04: // No_Flip_180, 16x16 dots
181 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
182 ebx += esi;
183 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
184 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
a4030801 185 break;
186 case 0x05: // No_Flip_180, 32x32 dots
187 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
188 ebx += esi;
189 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
190 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
a4030801 191 break;
192 case 0x06: // No_Flip_270, 16x16 dots
193 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
194 eax += esi;
195 edi = (ebx & 0x3800) ^ 0x1000; // bswap
196 eax += (ebx >> 8) & 0x40;
a4030801 197 break;
198 case 0x07: // No_Flip_270, 32x32 dots
199 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
200 eax += esi;
201 edi = (ebx & 0x3800) ^ 0x1000; // bswap
202 eax += (ebx >> 7) & 0x180;
a4030801 203 break;
204 case 0x08: // Flip_0, 16x16 dots
01bc6b19 205 ebx = (ebx >> 9) & 0x3c;
a4030801 206 ebx += esi;
207 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
208 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
a4030801 209 break;
210 case 0x09: // Flip_0, 32x32 dots
01bc6b19 211 ebx = (ebx >> 9) & 0x7c;
a4030801 212 ebx += esi;
213 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
214 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
a4030801 215 break;
216 case 0x0a: // Flip_90, 16x16 dots
217 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
218 eax += esi;
219 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
220 eax += ((ebx >> 8) & 0x40) ^ 0x40;
a4030801 221 break;
222 case 0x0b: // Flip_90, 32x32 dots
223 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
224 eax += esi;
225 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
226 eax += ((ebx >> 7) & 0x180) ^ 0x180;
a4030801 227 break;
228 case 0x0c: // Flip_180, 16x16 dots
229 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
230 ebx += esi;
231 edi = (eax & 0x3800) ^ 0x1000; // bswap
232 eax = ((eax >> 8) & 0x40) + ebx;
a4030801 233 break;
234 case 0x0d: // Flip_180, 32x32 dots
235 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
236 ebx += esi;
237 edi = (eax & 0x3800) ^ 0x1000; // bswap
238 eax = ((eax >> 7) & 0x180) + ebx;
a4030801 239 break;
240 case 0x0e: // Flip_270, 16x16 dots
241 eax = (eax >> 9) & 0x3c;
242 eax += esi;
243 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
244 eax += (ebx >> 8) & 0x40;
a4030801 245 break;
246 case 0x0f: // Flip_270, 32x32 dots
247 eax = (eax >> 9) & 0x7c;
248 eax += esi;
249 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
250 eax += (ebx >> 7) & 0x180;
a4030801 251 break;
252 }
253
01bc6b19 254 pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
255 if (!(edi & 0x800)) pixel >>= 4;
528ec956 256 else pixel &= 0x0f;
01bc6b19 257
a4030801 258Pixel_Out:
a4030801 259 if (!pixel && (func & 0x18)) goto Next_Pixel;
528ec956 260 esi = Buffer_Adr + ((XD>>1)^1); // pixel addr
a4030801 261 eax = *(Pico_mcd->word_ram2M + esi); // old pixel
528ec956 262 if (XD & 1)
a4030801 263 {
264 if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
265 *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
266 }
267 else
268 {
269 if ((eax & 0xf0) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
270 *(Pico_mcd->word_ram2M + esi) = (pixel << 4) | (eax & 0xf);
271 }
272
273
274Next_Pixel:
528ec956 275 ecx += (DYXS << 16) >> 16; // rot_comp.DXS;
276 edx += DYXS >> 16; // rot_comp.DYS;
277 XD++;
278 if (XD >= 8)
a4030801 279 {
528ec956 280 Buffer_Adr += ((rot_comp.Reg_5C & 0x1f) + 1) << 5;
281 XD = 0;
a4030801 282 }
528ec956 283 H_Dot--;
a4030801 284 }
528ec956 285 // end while
a4030801 286
287
288//nothing_to_draw:
289 rot_comp.YD++;
290 // rot_comp.V_Dot--; // will be done by caller
291}
d1df8786 292
293
cb4a513a 294void gfx_cd_update(void)
295{
d1df8786 296 unsigned char *V_Dot = (unsigned char *) &rot_comp.Reg_64;
297 int jobs;
298
299 dprintf("gfx_cd_update, Reg_64 = %04x", rot_comp.Reg_64);
300
301 if (!*V_Dot)
302 {
d1df8786 303 gfx_completed();
304 return;
305 }
306
307 jobs = rot_comp.Float_Part >> 16;
308
309 if (!jobs)
310 {
311 rot_comp.Float_Part += rot_comp.Draw_Speed;
312 return;
313 }
314
315 rot_comp.Float_Part &= 0xffff;
316 rot_comp.Float_Part += rot_comp.Draw_Speed;
317
318 while (jobs--)
319 {
a4030801 320 if (PicoOpt & 0x1000)
321 gfx_do(); // jmp [Jmp_Adr]:
322
323 (*V_Dot)--; // dec byte [V_Dot]
d1df8786 324
325 if (!*V_Dot)
326 {
327 // GFX_Completed:
328 gfx_completed();
329 return;
330 }
331 }
cb4a513a 332}
333
334
335unsigned int gfx_cd_read(unsigned int a)
336{
d1df8786 337 unsigned int d = 0;
338
339 switch (a) {
340 case 0x58: d = rot_comp.Reg_58; break;
341 case 0x5A: d = rot_comp.Reg_5A; break;
342 case 0x5C: d = rot_comp.Reg_5C; break;
343 case 0x5E: d = rot_comp.Reg_5E; break;
344 case 0x60: d = rot_comp.Reg_60; break;
345 case 0x62: d = rot_comp.Reg_62; break;
346 case 0x64: d = rot_comp.Reg_64; break;
347 case 0x66: break;
fa1e5e29 348 default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
d1df8786 349 }
350
351 dprintf("gfx_cd_read(%02x) = %04x", a, d);
cb4a513a 352
cb4a513a 353 return 0;
354}
355
5c69a605 356void gfx_cd_write16(unsigned int a, unsigned int d)
cb4a513a 357{
5c69a605 358 dprintf("gfx_cd_write16(%x, %04x)", a, d);
cb4a513a 359
360 switch (a) {
d1df8786 361 case 0x58: // .Reg_Stamp_Size
362 rot_comp.Reg_58 = d & 7;
363 return;
364
365 case 0x5A: // .Reg_Stamp_Adr
366 rot_comp.Reg_5A = d & 0xffe0;
367 return;
368
369 case 0x5C: // .Reg_IM_VCell_Size
370 rot_comp.Reg_5C = d & 0x1f;
371 return;
372
373 case 0x5E: // .Reg_IM_Adr
374 rot_comp.Reg_5E = d & 0xFFF8;
375 return;
376
377 case 0x60: // .Reg_IM_Offset
378 rot_comp.Reg_60 = d & 0x3f;
379 return;
380
381 case 0x62: // .Reg_IM_HDot_Size
382 rot_comp.Reg_62 = d & 0x1ff;
383 return;
384
385 case 0x64: // .Reg_IM_VDot_Size
386 rot_comp.Reg_64 = d & 0xff; // V_Dot, must be 32bit?
387 return;
388
389 case 0x66: // .Reg_Vector_Adr
390 rot_comp.Reg_66 = d & 0xfffe;
cb4a513a 391 if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
392 gfx_cd_start();
393 return;
d1df8786 394
5c69a605 395 default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
cb4a513a 396 }
397}
398
d1df8786 399
51a902ae 400void gfx_cd_reset(void)
401{
fa1e5e29 402 memset(&rot_comp.Reg_58, 0, sizeof(rot_comp));
403}
404
405
406// --------------------------------
407
408#include "cell_map.c"
409
410typedef unsigned short u16;
411
0a051f55 412// check: Heart of the alien, jaguar xj 220
fa1e5e29 413void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
414{
415 unsigned char *base;
416 unsigned int asrc, a2;
417 u16 *r;
418
419 base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
420
421 switch (Pico.video.type)
422 {
423 case 1: // vram
424 r = Pico.vram;
425 for(; len; len--)
426 {
427 asrc = cell_map(source >> 2) << 2;
428 asrc |= source & 2;
429 // if(a&1) d=(d<<8)|(d>>8); // ??
430 r[a>>1] = *(u16 *)(base + asrc);
431 source += 2;
432 // AutoIncrement
433 a=(u16)(a+inc);
434 }
435 rendstatus|=0x10;
436 break;
437
438 case 3: // cram
439 Pico.m.dirtyPal = 1;
440 r = Pico.cram;
441 for(a2=a&0x7f; len; len--)
442 {
443 asrc = cell_map(source >> 2) << 2;
444 asrc |= source & 2;
445 r[a2>>1] = *(u16 *)(base + asrc);
446 source += 2;
447 // AutoIncrement
448 a2+=inc;
449 // good dest?
450 if(a2 >= 0x80) break;
451 }
452 a=(a&0xff00)|a2;
453 break;
454
455 case 5: // vsram[a&0x003f]=d;
456 r = Pico.vsram;
457 for(a2=a&0x7f; len; len--)
458 {
459 asrc = cell_map(source >> 2) << 2;
460 asrc |= source & 2;
461 r[a2>>1] = *(u16 *)(base + asrc);
462 source += 2;
463 // AutoIncrement
464 a2+=inc;
465 // good dest?
466 if(a2 >= 0x80) break;
467 }
468 a=(a&0xff00)|a2;
469 break;
470 }
471 // remember addr
472 Pico.video.addr=(u16)a;
51a902ae 473}
474