get rid of port_config.h
[picodrive.git] / pico / cd / gfx_cd.c
1 // This is a direct rewrite of gfx_cd.asm (x86 asm to C).
2 // You can even find some x86 register names :)
3 // Original code (c) 2002 by Stéphane Dallongeville
4
5 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
6
7
8 #include "../pico_int.h"
9
10 #undef dprintf
11 #define dprintf(...)
12
13 #define _rot_comp Pico_mcd->rot_comp
14
15 static const int Table_Rot_Time[] =
16 {
17         0x00054000, 0x00048000, 0x00040000, 0x00036000,          //; 008-032               ; briefing - sprite
18         0x0002E000, 0x00028000, 0x00024000, 0x00022000,          //; 036-064               ; arbre souvent
19         0x00021000, 0x00020000, 0x0001E000, 0x0001B800,          //; 068-096               ; map thunderstrike
20         0x00019800, 0x00017A00, 0x00015C00, 0x00013E00,          //; 100-128               ; logo défoncé
21
22         0x00012000, 0x00011800, 0x00011000, 0x00010800,          //; 132-160               ; briefing - map
23         0x00010000, 0x0000F800, 0x0000F000, 0x0000E800,          //; 164-192
24         0x0000E000, 0x0000D800, 0x0000D000, 0x0000C800,          //; 196-224
25         0x0000C000, 0x0000B800, 0x0000B000, 0x0000A800,          //; 228-256               ; batman visage
26
27         0x0000A000, 0x00009F00, 0x00009E00, 0x00009D00,          //; 260-288
28         0x00009C00, 0x00009B00, 0x00009A00, 0x00009900,          //; 292-320
29         0x00009800, 0x00009700, 0x00009600, 0x00009500,          //; 324-352
30         0x00009400, 0x00009300, 0x00009200, 0x00009100,          //; 356-384
31
32         0x00009000, 0x00008F00, 0x00008E00, 0x00008D00,          //; 388-416
33         0x00008C00, 0x00008B00, 0x00008A00, 0x00008900,          //; 420-448
34         0x00008800, 0x00008700, 0x00008600, 0x00008500,          //; 452-476
35         0x00008400, 0x00008300, 0x00008200, 0x00008100,          //; 480-512
36 };
37
38
39 static void gfx_cd_start(void)
40 {
41         int upd_len;
42
43         // _rot_comp.XD_Mul = ((_rot_comp.Reg_5C & 0x1f) + 1) * 4; // unused
44         _rot_comp.Function = (_rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18);  // Jmp_Adr
45         // _rot_comp.Buffer_Adr = (_rot_comp.Reg_5E & 0xfff8) << 2; // unused?
46         _rot_comp.YD = (_rot_comp.Reg_60 >> 3) & 7;
47         _rot_comp.Vector_Adr = (_rot_comp.Reg_66 & 0xfffe) << 2;
48
49         upd_len = (_rot_comp.Reg_62 >> 3) & 0x3f;
50         upd_len = Table_Rot_Time[upd_len];
51         _rot_comp.Draw_Speed = _rot_comp.Float_Part = upd_len;
52
53         _rot_comp.Reg_58 |= 0x8000;     // Stamp_Size,  we start a new GFX operation
54
55         switch (_rot_comp.Reg_58 & 6)   // Scr_16?
56         {
57                 case 0: // ?
58                         _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xff80) << 2;
59                         break;
60                 case 2: // .Dot_32
61                         _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xffe0) << 2;
62                         break;
63                 case 4: // .Scr_16
64                         _rot_comp.Stamp_Map_Adr = 0x20000;
65                         break;
66                 case 6: // .Scr_16_Dot_32
67                         _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xe000) << 2;
68                         break;
69         }
70
71         dprintf("gfx_cd_start, stamp_map_addr=%06x", _rot_comp.Stamp_Map_Adr);
72
73         gfx_cd_update();
74 }
75
76
77 static void gfx_completed(void)
78 {
79         _rot_comp.Reg_58 &= 0x7fff;     // Stamp_Size
80         _rot_comp.Reg_64  = 0;
81         if (Pico_mcd->s68k_regs[0x33] & (1<<1))
82         {
83                 elprintf(EL_INTS, "gfx_cd irq 1");
84                 SekInterruptS68k(1);
85         }
86 }
87
88
89 static void gfx_do(unsigned int func, unsigned short *stamp_base, unsigned int H_Dot)
90 {
91         unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
92         unsigned int XD, Buffer_Adr;
93         int DYXS;
94
95         XD = _rot_comp.Reg_60 & 7;
96         Buffer_Adr = ((_rot_comp.Reg_5E & 0xfff8) + _rot_comp.YD) << 2;
97         ecx = *(unsigned int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr);
98         edx = ecx >> 16;
99         ecx = (ecx & 0xffff) << 8;
100         edx <<= 8;
101         DYXS = *(int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr + 4);
102         _rot_comp.Vector_Adr += 8;
103
104         // MAKE_IMAGE_LINE
105         while (H_Dot)
106         {
107                 // MAKE_IMAGE_PIXEL
108                 if (!(func & 1))        // NOT TILED
109                 {
110                         int mask = (func & 4) ? 0x00800000 : 0x00f80000;
111                         if ((ecx | edx) & mask)
112                         {
113                                 if (func & 0x18) goto Next_Pixel;
114                                 pixel = 0;
115                                 goto Pixel_Out;
116                         }
117                 }
118
119                 if (func & 2)           // mode 32x32 dot
120                 {
121                         if (func & 4)   // 16x16 screen
122                         {
123                                 ebx = ((ecx >> (11+5)) & 0x007f) |
124                                       ((edx >> (11-2)) & 0x3f80);
125                         }
126                         else            // 1x1 screen
127                         {
128                                 ebx = ((ecx >> (11+5)) & 0x07) |
129                                       ((edx >> (11+2)) & 0x38);
130                         }
131                 }
132                 else                    // mode 16x16 dot
133                 {
134                         if (func & 4)   // 16x16 screen
135                         {
136                                 ebx = ((ecx >> (11+4)) & 0x00ff) |
137                                       ((edx >> (11-4)) & 0xff00);
138                         }
139                         else            // 1x1 screen
140                         {
141                                 ebx = ((ecx >> (11+4)) & 0x0f) |
142                                       ((edx >> (11+0)) & 0xf0);
143                         }
144                 }
145
146                 edi = stamp_base[ebx];
147                 esi = (edi & 0x7ff) << 7;
148                 if (!esi) { pixel = 0; goto Pixel_Out; }
149                 edi >>= (11+1);
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;
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;
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;
173                                 break;
174                         case 0x03:      // No_Flip_90, 32x32 dots
175                                 eax = (eax >> 9) & 0x7c;
176                                 eax += esi;
177                                 edi = (ebx & 0x3800) ^ 0x2800;          // bswap
178                                 eax += ((ebx >> 7) & 0x180) ^ 0x180;
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;
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;
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;
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;
203                                 break;
204                         case 0x08:      // Flip_0, 16x16 dots
205                                 ebx = (ebx >> 9) & 0x3c;
206                                 ebx += esi;
207                                 edi = (eax & 0x3800) ^ 0x2800;          // bswap, flip
208                                 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
209                                 break;
210                         case 0x09:      // Flip_0, 32x32 dots
211                                 ebx = (ebx >> 9) & 0x7c;
212                                 ebx += esi;
213                                 edi = (eax & 0x3800) ^ 0x2800;          // bswap, flip
214                                 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
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;
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;
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;
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;
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;
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;
251                                 break;
252                 }
253
254                 pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
255                 if (!(edi & 0x800)) pixel >>= 4;
256                 else pixel &= 0x0f;
257
258 Pixel_Out:
259                 if (!pixel && (func & 0x18)) goto Next_Pixel;
260                 esi = Buffer_Adr + ((XD>>1)^1);                         // pixel addr
261                 eax = *(Pico_mcd->word_ram2M + esi);                    // old pixel
262                 if (XD & 1)
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
274 Next_Pixel:
275                 ecx += (DYXS << 16) >> 16;      // _rot_comp.DXS;
276                 edx +=  DYXS >> 16;             // _rot_comp.DYS;
277                 XD++;
278                 if (XD >= 8)
279                 {
280                         Buffer_Adr += ((_rot_comp.Reg_5C & 0x1f) + 1) << 5;
281                         XD = 0;
282                 }
283                 H_Dot--;
284         }
285         // end while
286
287
288 // nothing_to_draw:
289         _rot_comp.YD++;
290         // _rot_comp.V_Dot--; // will be done by caller
291 }
292
293
294 PICO_INTERNAL void gfx_cd_update(void)
295 {
296         int V_Dot = _rot_comp.Reg_64 & 0xff;
297         int jobs;
298
299         dprintf("gfx_cd_update, Reg_64 = %04x", _rot_comp.Reg_64);
300
301         if (!V_Dot)
302         {
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         if (PicoOpt & POPT_EN_MCD_GFX)
319         {
320                 unsigned int func = _rot_comp.Function;
321                 unsigned int H_Dot = _rot_comp.Reg_62 & 0x1ff;
322                 unsigned short *stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + _rot_comp.Stamp_Map_Adr);
323
324                 while (jobs--)
325                 {
326                         gfx_do(func, stamp_base, H_Dot);        // jmp [Jmp_Adr]:
327
328                         V_Dot--;                                // dec byte [V_Dot]
329                         if (V_Dot == 0)
330                         {
331                                 // GFX_Completed:
332                                 gfx_completed();
333                                 return;
334                         }
335                 }
336         }
337         else
338         {
339                 if (jobs >= V_Dot)
340                 {
341                         gfx_completed();
342                         return;
343                 }
344                 V_Dot -= jobs;
345         }
346
347         _rot_comp.Reg_64 = V_Dot;
348 }
349
350
351 PICO_INTERNAL_ASM unsigned int gfx_cd_read(unsigned int a)
352 {
353         unsigned int d = 0;
354
355         switch (a) {
356                 case 0x58: d = _rot_comp.Reg_58; break;
357                 case 0x5A: d = _rot_comp.Reg_5A; break;
358                 case 0x5C: d = _rot_comp.Reg_5C; break;
359                 case 0x5E: d = _rot_comp.Reg_5E; break;
360                 case 0x60: d = _rot_comp.Reg_60; break;
361                 case 0x62: d = _rot_comp.Reg_62; break;
362                 case 0x64: d = _rot_comp.Reg_64; break;
363                 case 0x66: break;
364                 default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
365         }
366
367         dprintf("gfx_cd_read(%02x) = %04x", a, d);
368
369         return d;
370 }
371
372 PICO_INTERNAL_ASM void gfx_cd_write16(unsigned int a, unsigned int d)
373 {
374         dprintf("gfx_cd_write16(%x, %04x)", a, d);
375
376         switch (a) {
377                 case 0x58: // .Reg_Stamp_Size
378                         _rot_comp.Reg_58 = d & 7;
379                         return;
380
381                 case 0x5A: // .Reg_Stamp_Adr
382                         _rot_comp.Reg_5A = d & 0xffe0;
383                         return;
384
385                 case 0x5C: // .Reg_IM_VCell_Size
386                         _rot_comp.Reg_5C = d & 0x1f;
387                         return;
388
389                 case 0x5E: // .Reg_IM_Adr
390                         _rot_comp.Reg_5E = d & 0xFFF8;
391                         return;
392
393                 case 0x60: // .Reg_IM_Offset
394                         _rot_comp.Reg_60 = d & 0x3f;
395                         return;
396
397                 case 0x62: // .Reg_IM_HDot_Size
398                         _rot_comp.Reg_62 = d & 0x1ff;
399                         return;
400
401                 case 0x64: // .Reg_IM_VDot_Size
402                         _rot_comp.Reg_64 = d & 0xff;    // V_Dot, must be 32bit?
403                         return;
404
405                 case 0x66: // .Reg_Vector_Adr
406                         _rot_comp.Reg_66 = d & 0xfffe;
407                         if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
408                         gfx_cd_start();
409                         return;
410
411                 default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
412         }
413 }
414
415
416 PICO_INTERNAL void gfx_cd_reset(void)
417 {
418         memset(&_rot_comp.Reg_58, 0, sizeof(_rot_comp));
419 }
420
421
422 // --------------------------------
423
424 #include "cell_map.c"
425
426 #ifndef UTYPES_DEFINED
427 typedef unsigned short u16;
428 #endif
429
430 // check: Heart of the alien, jaguar xj 220
431 PICO_INTERNAL void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
432 {
433   unsigned char *base;
434   unsigned int asrc, a2;
435   u16 *r;
436
437   base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
438
439   switch (Pico.video.type)
440   {
441     case 1: // vram
442       r = Pico.vram;
443       for(; len; len--)
444       {
445         asrc = cell_map(source >> 2) << 2;
446         asrc |= source & 2;
447         // if(a&1) d=(d<<8)|(d>>8); // ??
448         r[a>>1] = *(u16 *)(base + asrc);
449         source += 2;
450         // AutoIncrement
451         a=(u16)(a+inc);
452       }
453       rendstatus |= PDRAW_SPRITES_MOVED;
454       break;
455
456     case 3: // cram
457       Pico.m.dirtyPal = 1;
458       r = Pico.cram;
459       for(a2=a&0x7f; len; len--)
460       {
461         asrc = cell_map(source >> 2) << 2;
462         asrc |= source & 2;
463         r[a2>>1] = *(u16 *)(base + asrc);
464         source += 2;
465         // AutoIncrement
466         a2+=inc;
467         // good dest?
468         if(a2 >= 0x80) break;
469       }
470       a=(a&0xff00)|a2;
471       break;
472
473     case 5: // vsram[a&0x003f]=d;
474       r = Pico.vsram;
475       for(a2=a&0x7f; len; len--)
476       {
477         asrc = cell_map(source >> 2) << 2;
478         asrc |= source & 2;
479         r[a2>>1] = *(u16 *)(base + asrc);
480         source += 2;
481         // AutoIncrement
482         a2+=inc;
483         // good dest?
484         if(a2 >= 0x80) break;
485       }
486       a=(a&0xff00)|a2;
487       break;
488   }
489   // remember addr
490   Pico.video.addr=(u16)a;
491 }
492