bugfixes in cd/Memory.s, poll loop detection
[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
00bd648e 82static void gfx_do(unsigned int func, unsigned short *stamp_base, unsigned int H_Dot)
a4030801 83{
528ec956 84 unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
00bd648e 85 unsigned int XD, Buffer_Adr;
528ec956 86 int DYXS;
a4030801 87
528ec956 88 XD = rot_comp.Reg_60 & 7;
89 Buffer_Adr = ((rot_comp.Reg_5E & 0xfff8) + rot_comp.YD) << 2;
a4030801 90 ecx = *(unsigned int *)(Pico_mcd->word_ram2M + rot_comp.Vector_Adr);
91 edx = ecx >> 16;
92 ecx = (ecx & 0xffff) << 8;
93 edx <<= 8;
528ec956 94 DYXS = *(int *)(Pico_mcd->word_ram2M + rot_comp.Vector_Adr + 4);
a4030801 95 rot_comp.Vector_Adr += 8;
a4030801 96
97 // MAKE_IMAGE_LINE
528ec956 98 while (H_Dot)
a4030801 99 {
100 if (func & 2) // mode 32x32 dot
101 {
102 if (func & 4) // 16x16 screen
103 {
00bd648e 104 ebx = ((ecx >> (11+5)) & 0x007f) |
105 ((edx >> (11-2)) & 0x3f80);
a4030801 106 }
107 else // 1x1 screen
108 {
00bd648e 109 ebx = ((ecx >> (11+5)) & 0x07) |
110 ((edx >> (11+2)) & 0x38);
a4030801 111 }
112 }
113 else // mode 16x16 dot
114 {
115 if (func & 4) // 16x16 screen
116 {
00bd648e 117 ebx = ((ecx >> (11+4)) & 0x00ff) |
118 ((edx >> (11-4)) & 0xff00);
a4030801 119 }
120 else // 1x1 screen
121 {
00bd648e 122 ebx = ((ecx >> (11+4)) & 0x0f) |
123 ((edx >> (11+0)) & 0xf0);
a4030801 124 }
125 }
a4030801 126
a4030801 127 // MAKE_IMAGE_PIXEL
128 if (!(func & 1)) // NOT TILED
129 {
528ec956 130 int mask = (func & 4) ? 0x00800000 : 0x00f80000;
131 if ((ecx | edx) & mask)
a4030801 132 {
528ec956 133 if (func & 0x18) goto Next_Pixel;
134 pixel = 0;
135 goto Pixel_Out;
a4030801 136 }
137 }
528ec956 138
00bd648e 139 edi = stamp_base[ebx];// | (stamp_base[ebx+1] << 16);
140 esi = (edi & 0x7ff) << 7;
528ec956 141 if (!esi) { pixel = 0; goto Pixel_Out; }
00bd648e 142 edi >>= (11+1);
a4030801 143 edi &= (0x1c>>1);
144 eax = ecx;
145 ebx = edx;
146 if (func & 2) edi |= 1; // 32 dots?
147 switch (edi)
148 {
149 case 0x00: // No_Flip_0, 16x16 dots
150 ebx = (ebx >> 9) & 0x3c;
151 ebx += esi;
152 edi = (eax & 0x3800) ^ 0x1000; // bswap
153 eax = ((eax >> 8) & 0x40) + ebx;
a4030801 154 break;
155 case 0x01: // No_Flip_0, 32x32 dots
156 ebx = (ebx >> 9) & 0x7c;
157 ebx += esi;
158 edi = (eax & 0x3800) ^ 0x1000; // bswap
159 eax = ((eax >> 7) & 0x180) + ebx;
a4030801 160 break;
161 case 0x02: // No_Flip_90, 16x16 dots
162 eax = (eax >> 9) & 0x3c;
163 eax += esi;
164 edi = (ebx & 0x3800) ^ 0x2800; // bswap
165 eax += ((ebx >> 8) & 0x40) ^ 0x40;
a4030801 166 break;
167 case 0x03: // No_Flip_90, 32x32 dots
168 eax = (eax >> 9) & 0x7c;
169 eax += esi;
170 edi = (ebx & 0x3800) ^ 0x2800; // bswap
01bc6b19 171 eax += ((ebx >> 7) & 0x180) ^ 0x180;
a4030801 172 break;
173 case 0x04: // No_Flip_180, 16x16 dots
174 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
175 ebx += esi;
176 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
177 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
a4030801 178 break;
179 case 0x05: // No_Flip_180, 32x32 dots
180 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
181 ebx += esi;
182 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
183 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
a4030801 184 break;
185 case 0x06: // No_Flip_270, 16x16 dots
186 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
187 eax += esi;
188 edi = (ebx & 0x3800) ^ 0x1000; // bswap
189 eax += (ebx >> 8) & 0x40;
a4030801 190 break;
191 case 0x07: // No_Flip_270, 32x32 dots
192 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
193 eax += esi;
194 edi = (ebx & 0x3800) ^ 0x1000; // bswap
195 eax += (ebx >> 7) & 0x180;
a4030801 196 break;
197 case 0x08: // Flip_0, 16x16 dots
01bc6b19 198 ebx = (ebx >> 9) & 0x3c;
a4030801 199 ebx += esi;
200 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
201 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
a4030801 202 break;
203 case 0x09: // Flip_0, 32x32 dots
01bc6b19 204 ebx = (ebx >> 9) & 0x7c;
a4030801 205 ebx += esi;
206 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
207 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
a4030801 208 break;
209 case 0x0a: // Flip_90, 16x16 dots
210 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
211 eax += esi;
212 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
213 eax += ((ebx >> 8) & 0x40) ^ 0x40;
a4030801 214 break;
215 case 0x0b: // Flip_90, 32x32 dots
216 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
217 eax += esi;
218 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
219 eax += ((ebx >> 7) & 0x180) ^ 0x180;
a4030801 220 break;
221 case 0x0c: // Flip_180, 16x16 dots
222 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
223 ebx += esi;
224 edi = (eax & 0x3800) ^ 0x1000; // bswap
225 eax = ((eax >> 8) & 0x40) + ebx;
a4030801 226 break;
227 case 0x0d: // Flip_180, 32x32 dots
228 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
229 ebx += esi;
230 edi = (eax & 0x3800) ^ 0x1000; // bswap
231 eax = ((eax >> 7) & 0x180) + ebx;
a4030801 232 break;
233 case 0x0e: // Flip_270, 16x16 dots
234 eax = (eax >> 9) & 0x3c;
235 eax += esi;
236 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
237 eax += (ebx >> 8) & 0x40;
a4030801 238 break;
239 case 0x0f: // Flip_270, 32x32 dots
240 eax = (eax >> 9) & 0x7c;
241 eax += esi;
242 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
243 eax += (ebx >> 7) & 0x180;
a4030801 244 break;
245 }
246
01bc6b19 247 pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
248 if (!(edi & 0x800)) pixel >>= 4;
528ec956 249 else pixel &= 0x0f;
01bc6b19 250
a4030801 251Pixel_Out:
a4030801 252 if (!pixel && (func & 0x18)) goto Next_Pixel;
528ec956 253 esi = Buffer_Adr + ((XD>>1)^1); // pixel addr
a4030801 254 eax = *(Pico_mcd->word_ram2M + esi); // old pixel
528ec956 255 if (XD & 1)
a4030801 256 {
257 if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
258 *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
259 }
260 else
261 {
262 if ((eax & 0xf0) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
263 *(Pico_mcd->word_ram2M + esi) = (pixel << 4) | (eax & 0xf);
264 }
265
266
267Next_Pixel:
528ec956 268 ecx += (DYXS << 16) >> 16; // rot_comp.DXS;
269 edx += DYXS >> 16; // rot_comp.DYS;
270 XD++;
271 if (XD >= 8)
a4030801 272 {
528ec956 273 Buffer_Adr += ((rot_comp.Reg_5C & 0x1f) + 1) << 5;
274 XD = 0;
a4030801 275 }
528ec956 276 H_Dot--;
a4030801 277 }
528ec956 278 // end while
a4030801 279
280
00bd648e 281// nothing_to_draw:
a4030801 282 rot_comp.YD++;
283 // rot_comp.V_Dot--; // will be done by caller
284}
d1df8786 285
286
cb4a513a 287void gfx_cd_update(void)
288{
00bd648e 289 int V_Dot = rot_comp.Reg_64 & 0xff;
d1df8786 290 int jobs;
291
292 dprintf("gfx_cd_update, Reg_64 = %04x", rot_comp.Reg_64);
293
00bd648e 294 if (!V_Dot)
d1df8786 295 {
d1df8786 296 gfx_completed();
297 return;
298 }
299
300 jobs = rot_comp.Float_Part >> 16;
301
302 if (!jobs)
303 {
304 rot_comp.Float_Part += rot_comp.Draw_Speed;
305 return;
306 }
307
308 rot_comp.Float_Part &= 0xffff;
309 rot_comp.Float_Part += rot_comp.Draw_Speed;
310
00bd648e 311 if (PicoOpt & 0x1000) // scale/rot enabled
d1df8786 312 {
00bd648e 313 unsigned int func = rot_comp.Function;
314 unsigned int H_Dot = rot_comp.Reg_62 & 0x1ff;
315 unsigned short *stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + rot_comp.Stamp_Map_Adr);
a4030801 316
00bd648e 317 while (jobs--)
318 {
319 gfx_do(func, stamp_base, H_Dot); // jmp [Jmp_Adr]:
d1df8786 320
00bd648e 321 V_Dot--; // dec byte [V_Dot]
322 if (V_Dot == 0)
323 {
324 // GFX_Completed:
325 gfx_completed();
326 return;
327 }
328 }
329 }
330 else
331 {
332 if (jobs >= V_Dot)
d1df8786 333 {
d1df8786 334 gfx_completed();
335 return;
336 }
00bd648e 337 V_Dot -= jobs;
d1df8786 338 }
00bd648e 339
340 rot_comp.Reg_64 = V_Dot;
cb4a513a 341}
342
343
344unsigned int gfx_cd_read(unsigned int a)
345{
d1df8786 346 unsigned int d = 0;
347
348 switch (a) {
349 case 0x58: d = rot_comp.Reg_58; break;
350 case 0x5A: d = rot_comp.Reg_5A; break;
351 case 0x5C: d = rot_comp.Reg_5C; break;
352 case 0x5E: d = rot_comp.Reg_5E; break;
353 case 0x60: d = rot_comp.Reg_60; break;
354 case 0x62: d = rot_comp.Reg_62; break;
355 case 0x64: d = rot_comp.Reg_64; break;
356 case 0x66: break;
fa1e5e29 357 default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
d1df8786 358 }
359
360 dprintf("gfx_cd_read(%02x) = %04x", a, d);
cb4a513a 361
cb4a513a 362 return 0;
363}
364
5c69a605 365void gfx_cd_write16(unsigned int a, unsigned int d)
cb4a513a 366{
5c69a605 367 dprintf("gfx_cd_write16(%x, %04x)", a, d);
cb4a513a 368
369 switch (a) {
d1df8786 370 case 0x58: // .Reg_Stamp_Size
371 rot_comp.Reg_58 = d & 7;
372 return;
373
374 case 0x5A: // .Reg_Stamp_Adr
375 rot_comp.Reg_5A = d & 0xffe0;
376 return;
377
378 case 0x5C: // .Reg_IM_VCell_Size
379 rot_comp.Reg_5C = d & 0x1f;
380 return;
381
382 case 0x5E: // .Reg_IM_Adr
383 rot_comp.Reg_5E = d & 0xFFF8;
384 return;
385
386 case 0x60: // .Reg_IM_Offset
387 rot_comp.Reg_60 = d & 0x3f;
388 return;
389
390 case 0x62: // .Reg_IM_HDot_Size
391 rot_comp.Reg_62 = d & 0x1ff;
392 return;
393
394 case 0x64: // .Reg_IM_VDot_Size
395 rot_comp.Reg_64 = d & 0xff; // V_Dot, must be 32bit?
396 return;
397
398 case 0x66: // .Reg_Vector_Adr
399 rot_comp.Reg_66 = d & 0xfffe;
cb4a513a 400 if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
401 gfx_cd_start();
402 return;
d1df8786 403
5c69a605 404 default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
cb4a513a 405 }
406}
407
d1df8786 408
51a902ae 409void gfx_cd_reset(void)
410{
fa1e5e29 411 memset(&rot_comp.Reg_58, 0, sizeof(rot_comp));
412}
413
414
415// --------------------------------
416
417#include "cell_map.c"
418
419typedef unsigned short u16;
420
0a051f55 421// check: Heart of the alien, jaguar xj 220
fa1e5e29 422void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
423{
424 unsigned char *base;
425 unsigned int asrc, a2;
426 u16 *r;
427
428 base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
429
430 switch (Pico.video.type)
431 {
432 case 1: // vram
433 r = Pico.vram;
434 for(; len; len--)
435 {
436 asrc = cell_map(source >> 2) << 2;
437 asrc |= source & 2;
438 // if(a&1) d=(d<<8)|(d>>8); // ??
439 r[a>>1] = *(u16 *)(base + asrc);
440 source += 2;
441 // AutoIncrement
442 a=(u16)(a+inc);
443 }
444 rendstatus|=0x10;
445 break;
446
447 case 3: // cram
448 Pico.m.dirtyPal = 1;
449 r = Pico.cram;
450 for(a2=a&0x7f; len; len--)
451 {
452 asrc = cell_map(source >> 2) << 2;
453 asrc |= source & 2;
454 r[a2>>1] = *(u16 *)(base + asrc);
455 source += 2;
456 // AutoIncrement
457 a2+=inc;
458 // good dest?
459 if(a2 >= 0x80) break;
460 }
461 a=(a&0xff00)|a2;
462 break;
463
464 case 5: // vsram[a&0x003f]=d;
465 r = Pico.vsram;
466 for(a2=a&0x7f; len; len--)
467 {
468 asrc = cell_map(source >> 2) << 2;
469 asrc |= source & 2;
470 r[a2>>1] = *(u16 *)(base + asrc);
471 source += 2;
472 // AutoIncrement
473 a2+=inc;
474 // good dest?
475 if(a2 >= 0x80) break;
476 }
477 a=(a&0xff00)|a2;
478 break;
479 }
480 // remember addr
481 Pico.video.addr=(u16)a;
51a902ae 482}
483