new timing for main and cd
[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 void gfx_do_line(unsigned int func, unsigned short *stamp_base,
16         unsigned int H_Dot);
17
18 static void gfx_cd_start(void)
19 {
20         int w, h;
21
22         w = _rot_comp.Reg_62;
23         h = _rot_comp.Reg_64;
24         if (w == 0 || h == 0) {
25                 elprintf(EL_CD|EL_ANOMALY, "gfx_cd_start with %ux%u", w, h);
26                 _rot_comp.Reg_64 = 0;
27                 // irq?
28                 return;
29         }
30
31         // _rot_comp.XD_Mul = ((_rot_comp.Reg_5C & 0x1f) + 1) * 4; // unused
32         _rot_comp.Function = (_rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18);  // Jmp_Adr
33         // _rot_comp.Buffer_Adr = (_rot_comp.Reg_5E & 0xfff8) << 2; // unused?
34         _rot_comp.YD = (_rot_comp.Reg_60 >> 3) & 7;
35         _rot_comp.Vector_Adr = (_rot_comp.Reg_66 & 0xfffe) << 2;
36
37         _rot_comp.Reg_58 |= 0x8000;     // Stamp_Size,  we start a new GFX operation
38
39         pcd_event_schedule_s68k(PCD_EVENT_GFX, 5 * w * h);
40
41         switch (_rot_comp.Reg_58 & 6)   // Scr_16?
42         {
43                 case 0: // ?
44                         _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xff80) << 2;
45                         break;
46                 case 2: // .Dot_32
47                         _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xffe0) << 2;
48                         break;
49                 case 4: // .Scr_16
50                         _rot_comp.Stamp_Map_Adr = 0x20000;
51                         break;
52                 case 6: // .Scr_16_Dot_32
53                         _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xe000) << 2;
54                         break;
55         }
56
57         if (PicoOpt & POPT_EN_MCD_GFX)
58         {
59                 unsigned int func = _rot_comp.Function;
60                 unsigned short *stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + _rot_comp.Stamp_Map_Adr);
61
62                 while (h--)
63                         gfx_do_line(func, stamp_base, w);
64         }
65 }
66
67
68 PICO_INTERNAL_ASM unsigned int gfx_cd_read(unsigned int a)
69 {
70         unsigned int d = 0;
71
72         switch (a) {
73                 case 0x58: d = _rot_comp.Reg_58; break;
74                 case 0x5A: d = _rot_comp.Reg_5A; break;
75                 case 0x5C: d = _rot_comp.Reg_5C; break;
76                 case 0x5E: d = _rot_comp.Reg_5E; break;
77                 case 0x60: d = _rot_comp.Reg_60; break;
78                 case 0x62: d = _rot_comp.Reg_62; break;
79                 case 0x64:
80                         d = _rot_comp.Reg_64;
81                         if (_rot_comp.Reg_64 > 1)
82                                 // fudge..
83                                 _rot_comp.Reg_64--;
84                         break;
85                 case 0x66: break;
86                 default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
87         }
88
89         dprintf("gfx_cd_read(%02x) = %04x", a, d);
90
91         return d;
92
93 }
94
95 static void gfx_do_line(unsigned int func, unsigned short *stamp_base,
96         unsigned int H_Dot)
97 {
98         unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
99         unsigned int XD, Buffer_Adr;
100         int DYXS;
101
102         XD = _rot_comp.Reg_60 & 7;
103         Buffer_Adr = ((_rot_comp.Reg_5E & 0xfff8) + _rot_comp.YD) << 2;
104         ecx = *(unsigned int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr);
105         edx = ecx >> 16;
106         ecx = (ecx & 0xffff) << 8;
107         edx <<= 8;
108         DYXS = *(int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr + 4);
109         _rot_comp.Vector_Adr += 8;
110
111         // MAKE_IMAGE_LINE
112         while (H_Dot)
113         {
114                 // MAKE_IMAGE_PIXEL
115                 if (!(func & 1))        // NOT TILED
116                 {
117                         int mask = (func & 4) ? 0x00800000 : 0x00f80000;
118                         if ((ecx | edx) & mask)
119                         {
120                                 if (func & 0x18) goto Next_Pixel;
121                                 pixel = 0;
122                                 goto Pixel_Out;
123                         }
124                 }
125
126                 if (func & 2)           // mode 32x32 dot
127                 {
128                         if (func & 4)   // 16x16 screen
129                         {
130                                 ebx = ((ecx >> (11+5)) & 0x007f) |
131                                       ((edx >> (11-2)) & 0x3f80);
132                         }
133                         else            // 1x1 screen
134                         {
135                                 ebx = ((ecx >> (11+5)) & 0x07) |
136                                       ((edx >> (11+2)) & 0x38);
137                         }
138                 }
139                 else                    // mode 16x16 dot
140                 {
141                         if (func & 4)   // 16x16 screen
142                         {
143                                 ebx = ((ecx >> (11+4)) & 0x00ff) |
144                                       ((edx >> (11-4)) & 0xff00);
145                         }
146                         else            // 1x1 screen
147                         {
148                                 ebx = ((ecx >> (11+4)) & 0x0f) |
149                                       ((edx >> (11+0)) & 0xf0);
150                         }
151                 }
152
153                 edi = stamp_base[ebx];
154                 esi = (edi & 0x7ff) << 7;
155                 if (!esi) { pixel = 0; goto Pixel_Out; }
156                 edi >>= (11+1);
157                 edi &= (0x1c>>1);
158                 eax = ecx;
159                 ebx = edx;
160                 if (func & 2) edi |= 1; // 32 dots?
161                 switch (edi)
162                 {
163                         case 0x00:      // No_Flip_0, 16x16 dots
164                                 ebx = (ebx >> 9) & 0x3c;
165                                 ebx += esi;
166                                 edi = (eax & 0x3800) ^ 0x1000;          // bswap
167                                 eax = ((eax >> 8) & 0x40) + ebx;
168                                 break;
169                         case 0x01:      // No_Flip_0, 32x32 dots
170                                 ebx = (ebx >> 9) & 0x7c;
171                                 ebx += esi;
172                                 edi = (eax & 0x3800) ^ 0x1000;          // bswap
173                                 eax = ((eax >> 7) & 0x180) + ebx;
174                                 break;
175                         case 0x02:      // No_Flip_90, 16x16 dots
176                                 eax = (eax >> 9) & 0x3c;
177                                 eax += esi;
178                                 edi = (ebx & 0x3800) ^ 0x2800;          // bswap
179                                 eax += ((ebx >> 8) & 0x40) ^ 0x40;
180                                 break;
181                         case 0x03:      // No_Flip_90, 32x32 dots
182                                 eax = (eax >> 9) & 0x7c;
183                                 eax += esi;
184                                 edi = (ebx & 0x3800) ^ 0x2800;          // bswap
185                                 eax += ((ebx >> 7) & 0x180) ^ 0x180;
186                                 break;
187                         case 0x04:      // No_Flip_180, 16x16 dots
188                                 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
189                                 ebx += esi;
190                                 edi = (eax & 0x3800) ^ 0x2800;          // bswap and flip
191                                 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
192                                 break;
193                         case 0x05:      // No_Flip_180, 32x32 dots
194                                 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
195                                 ebx += esi;
196                                 edi = (eax & 0x3800) ^ 0x2800;          // bswap and flip
197                                 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
198                                 break;
199                         case 0x06:      // No_Flip_270, 16x16 dots
200                                 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
201                                 eax += esi;
202                                 edi = (ebx & 0x3800) ^ 0x1000;          // bswap
203                                 eax += (ebx >> 8) & 0x40;
204                                 break;
205                         case 0x07:      // No_Flip_270, 32x32 dots
206                                 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
207                                 eax += esi;
208                                 edi = (ebx & 0x3800) ^ 0x1000;          // bswap
209                                 eax += (ebx >> 7) & 0x180;
210                                 break;
211                         case 0x08:      // Flip_0, 16x16 dots
212                                 ebx = (ebx >> 9) & 0x3c;
213                                 ebx += esi;
214                                 edi = (eax & 0x3800) ^ 0x2800;          // bswap, flip
215                                 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
216                                 break;
217                         case 0x09:      // Flip_0, 32x32 dots
218                                 ebx = (ebx >> 9) & 0x7c;
219                                 ebx += esi;
220                                 edi = (eax & 0x3800) ^ 0x2800;          // bswap, flip
221                                 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
222                                 break;
223                         case 0x0a:      // Flip_90, 16x16 dots
224                                 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
225                                 eax += esi;
226                                 edi = (ebx & 0x3800) ^ 0x2800;          // bswap, flip
227                                 eax += ((ebx >> 8) & 0x40) ^ 0x40;
228                                 break;
229                         case 0x0b:      // Flip_90, 32x32 dots
230                                 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
231                                 eax += esi;
232                                 edi = (ebx & 0x3800) ^ 0x2800;          // bswap, flip
233                                 eax += ((ebx >> 7) & 0x180) ^ 0x180;
234                                 break;
235                         case 0x0c:      // Flip_180, 16x16 dots
236                                 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
237                                 ebx += esi;
238                                 edi = (eax & 0x3800) ^ 0x1000;          // bswap
239                                 eax = ((eax >> 8) & 0x40) + ebx;
240                                 break;
241                         case 0x0d:      // Flip_180, 32x32 dots
242                                 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
243                                 ebx += esi;
244                                 edi = (eax & 0x3800) ^ 0x1000;          // bswap
245                                 eax = ((eax >> 7) & 0x180) + ebx;
246                                 break;
247                         case 0x0e:      // Flip_270, 16x16 dots
248                                 eax = (eax >> 9) & 0x3c;
249                                 eax += esi;
250                                 edi = (ebx & 0x3800) ^ 0x1000;          // bswap, flip
251                                 eax += (ebx >> 8) & 0x40;
252                                 break;
253                         case 0x0f:      // Flip_270, 32x32 dots
254                                 eax = (eax >> 9) & 0x7c;
255                                 eax += esi;
256                                 edi = (ebx & 0x3800) ^ 0x1000;          // bswap, flip
257                                 eax += (ebx >> 7) & 0x180;
258                                 break;
259                 }
260
261                 pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
262                 if (!(edi & 0x800)) pixel >>= 4;
263                 else pixel &= 0x0f;
264
265 Pixel_Out:
266                 if (!pixel && (func & 0x18)) goto Next_Pixel;
267                 esi = Buffer_Adr + ((XD>>1)^1);                         // pixel addr
268                 eax = *(Pico_mcd->word_ram2M + esi);                    // old pixel
269                 if (XD & 1)
270                 {
271                         if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
272                         *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
273                 }
274                 else
275                 {
276                         if ((eax & 0xf0) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
277                         *(Pico_mcd->word_ram2M + esi) = (pixel << 4) | (eax & 0xf);
278                 }
279
280
281 Next_Pixel:
282                 ecx += (DYXS << 16) >> 16;      // _rot_comp.DXS;
283                 edx +=  DYXS >> 16;             // _rot_comp.DYS;
284                 XD++;
285                 if (XD >= 8)
286                 {
287                         Buffer_Adr += ((_rot_comp.Reg_5C & 0x1f) + 1) << 5;
288                         XD = 0;
289                 }
290                 H_Dot--;
291         }
292         // end while
293
294
295 // nothing_to_draw:
296         _rot_comp.YD++;
297         // _rot_comp.V_Dot--; // will be done by caller
298 }
299
300
301 PICO_INTERNAL_ASM void gfx_cd_write16(unsigned int a, unsigned int d)
302 {
303         dprintf("gfx_cd_write16(%x, %04x)", a, d);
304
305         if (_rot_comp.Reg_58 & 0x8000)
306                 elprintf(EL_CD|EL_ANOMALY, "cd: busy gfx reg write %02x %04x", a, d);
307
308         switch (a) {
309                 case 0x58: // .Reg_Stamp_Size
310                         _rot_comp.Reg_58 = d & 7;
311                         return;
312
313                 case 0x5A: // .Reg_Stamp_Adr
314                         _rot_comp.Reg_5A = d & 0xffe0;
315                         return;
316
317                 case 0x5C: // .Reg_IM_VCell_Size
318                         _rot_comp.Reg_5C = d & 0x1f;
319                         return;
320
321                 case 0x5E: // .Reg_IM_Adr
322                         _rot_comp.Reg_5E = d & 0xFFF8;
323                         return;
324
325                 case 0x60: // .Reg_IM_Offset
326                         _rot_comp.Reg_60 = d & 0x3f;
327                         return;
328
329                 case 0x62: // .Reg_IM_HDot_Size
330                         _rot_comp.Reg_62 = d & 0x1ff;
331                         return;
332
333                 case 0x64: // .Reg_IM_VDot_Size
334                         _rot_comp.Reg_64 = d & 0xff;    // V_Dot, must be 32bit?
335                         return;
336
337                 case 0x66: // .Reg_Vector_Adr
338                         _rot_comp.Reg_66 = d & 0xfffe;
339                         if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
340                         gfx_cd_start();
341                         return;
342
343                 default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
344         }
345 }
346
347
348 PICO_INTERNAL void gfx_cd_reset(void)
349 {
350         memset(&_rot_comp.Reg_58, 0, sizeof(_rot_comp));
351 }
352
353
354 // --------------------------------
355
356 #include "cell_map.c"
357
358 #ifndef UTYPES_DEFINED
359 typedef unsigned short u16;
360 #endif
361
362 // check: Heart of the alien, jaguar xj 220
363 PICO_INTERNAL void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
364 {
365   unsigned char *base;
366   unsigned int asrc, a2;
367   u16 *r;
368
369   base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
370
371   switch (Pico.video.type)
372   {
373     case 1: // vram
374       r = Pico.vram;
375       for(; len; len--)
376       {
377         asrc = cell_map(source >> 2) << 2;
378         asrc |= source & 2;
379         // if(a&1) d=(d<<8)|(d>>8); // ??
380         r[a>>1] = *(u16 *)(base + asrc);
381         source += 2;
382         // AutoIncrement
383         a=(u16)(a+inc);
384       }
385       rendstatus |= PDRAW_SPRITES_MOVED;
386       break;
387
388     case 3: // cram
389       Pico.m.dirtyPal = 1;
390       r = Pico.cram;
391       for(a2=a&0x7f; len; len--)
392       {
393         asrc = cell_map(source >> 2) << 2;
394         asrc |= source & 2;
395         r[a2>>1] = *(u16 *)(base + asrc);
396         source += 2;
397         // AutoIncrement
398         a2+=inc;
399         // good dest?
400         if(a2 >= 0x80) break;
401       }
402       a=(a&0xff00)|a2;
403       break;
404
405     case 5: // vsram[a&0x003f]=d;
406       r = Pico.vsram;
407       for(a2=a&0x7f; len; len--)
408       {
409         asrc = cell_map(source >> 2) << 2;
410         asrc |= source & 2;
411         r[a2>>1] = *(u16 *)(base + asrc);
412         source += 2;
413         // AutoIncrement
414         a2+=inc;
415         // good dest?
416         if(a2 >= 0x80) break;
417       }
418       a=(a&0xff00)|a2;
419       break;
420   }
421   // remember addr
422   Pico.video.addr=(u16)a;
423 }
424