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