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