lowercasing filenames, part1; makefile adjustments
[picodrive.git] / Pico / cd / gfx_cd.c
index 64b52dd..69d4a00 100644 (file)
@@ -1,9 +1,13 @@
-// TODO...
+// This is a direct rewrite of gfx_cd.asm (x86 asm to C).
+// You can even find some x86 register names :)
+// Original code (c) 2002 by Stéphane Dallongeville
 
-// #include <string.h>
-#include "../PicoInt.h"
+// (c) Copyright 2007, Grazvydas "notaz" Ignotas
 
-#define rot_comp Pico_mcd->rot_comp
+
+#include "../pico_int.h"
+
+#define _rot_comp Pico_mcd->rot_comp
 
 static const int Table_Rot_Time[] =
 {
@@ -33,121 +37,113 @@ static void gfx_cd_start(void)
 {
        int upd_len;
 
-       dprintf("gfx_cd_start()");
+       // _rot_comp.XD_Mul = ((_rot_comp.Reg_5C & 0x1f) + 1) * 4; // unused
+       _rot_comp.Function = (_rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18);  // Jmp_Adr
+       // _rot_comp.Buffer_Adr = (_rot_comp.Reg_5E & 0xfff8) << 2; // unused?
+       _rot_comp.YD = (_rot_comp.Reg_60 >> 3) & 7;
+       _rot_comp.Vector_Adr = (_rot_comp.Reg_66 & 0xfffe) << 2;
 
-       rot_comp.XD_Mul = ((rot_comp.Reg_5C & 0x1f) + 1) * 4;
-       rot_comp.Function = (rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18);    // Jmp_Adr
-       rot_comp.Buffer_Adr = (rot_comp.Reg_5E & 0xfff8) << 2;
-       rot_comp.YD = (rot_comp.Reg_60 >> 3) & 7;
-       rot_comp.Vector_Adr = (rot_comp.Reg_66 & 0xfffe) << 2;
-
-       upd_len = (rot_comp.Reg_62 >> 3) & 0x3f;
+       upd_len = (_rot_comp.Reg_62 >> 3) & 0x3f;
        upd_len = Table_Rot_Time[upd_len];
-       rot_comp.Draw_Speed = rot_comp.Float_Part = upd_len;
+       _rot_comp.Draw_Speed = _rot_comp.Float_Part = upd_len;
 
-       rot_comp.Reg_58 |= 0x8000;      // Stamp_Size,  we start a new GFX operation
+       _rot_comp.Reg_58 |= 0x8000;     // Stamp_Size,  we start a new GFX operation
 
-       switch (rot_comp.Reg_58 & 6)    // Scr_16?
+       switch (_rot_comp.Reg_58 & 6)   // Scr_16?
        {
                case 0: // ?
-                       rot_comp.Stamp_Map_Adr = (rot_comp.Reg_5A & 0xff80) << 2;
+                       _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xff80) << 2;
                        break;
                case 2: // .Dot_32
-                       rot_comp.Stamp_Map_Adr = (rot_comp.Reg_5A & 0xffe0) << 2;
+                       _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xffe0) << 2;
                        break;
                case 4: // .Scr_16
-                       rot_comp.Stamp_Map_Adr = 0x20000;
+                       _rot_comp.Stamp_Map_Adr = 0x20000;
                        break;
                case 6: // .Scr_16_Dot_32
-                       rot_comp.Stamp_Map_Adr = (rot_comp.Reg_5A & 0xe000) << 2;
+                       _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xe000) << 2;
                        break;
        }
 
+       dprintf("gfx_cd_start, stamp_map_addr=%06x", _rot_comp.Stamp_Map_Adr);
+
        gfx_cd_update();
 }
 
 
 static void gfx_completed(void)
 {
-       rot_comp.Reg_58 &= 0x7fff;      // Stamp_Size
-       rot_comp.Reg_64  = 0;
+       _rot_comp.Reg_58 &= 0x7fff;     // Stamp_Size
+       _rot_comp.Reg_64  = 0;
        if (Pico_mcd->s68k_regs[0x33] & (1<<1))
        {
-               dprintf("gfx_cd irq 1");
+               elprintf(EL_INTS, "gfx_cd irq 1");
                SekInterruptS68k(1);
        }
 }
 
 
-static void gfx_do(void)
+static void gfx_do(unsigned int func, unsigned short *stamp_base, unsigned int H_Dot)
 {
-       unsigned int eax, ebx, ecx, edx, esi, edi, pixel = 0;
-       unsigned int func = rot_comp.Function;
-       unsigned short *ptrs;
-
-       rot_comp.XD = rot_comp.Reg_60 & 7;
-       rot_comp.Buffer_Adr = ((rot_comp.Reg_5E & 0xfff8) + rot_comp.YD) << 2;
-       rot_comp.H_Dot = rot_comp.Reg_62 & 0x1ff;
-       ecx = *(unsigned int *)(Pico_mcd->word_ram2M + rot_comp.Vector_Adr);
+       unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
+       unsigned int XD, Buffer_Adr;
+       int DYXS;
+
+       XD = _rot_comp.Reg_60 & 7;
+       Buffer_Adr = ((_rot_comp.Reg_5E & 0xfff8) + _rot_comp.YD) << 2;
+       ecx = *(unsigned int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr);
        edx = ecx >> 16;
        ecx = (ecx & 0xffff) << 8;
        edx <<= 8;
-       rot_comp.DXS = *(int *)(Pico_mcd->word_ram2M + rot_comp.Vector_Adr + 4);
-       rot_comp.DYS =  rot_comp.DXS >> 16;
-       rot_comp.DXS = (rot_comp.DXS << 16) >> 16; // sign extend
-       rot_comp.Vector_Adr += 8;
+       DYXS = *(int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr + 4);
+       _rot_comp.Vector_Adr += 8;
 
        // MAKE_IMAGE_LINE
-       while (rot_comp.H_Dot)
+       while (H_Dot)
        {
-               if (func & 2)           // mode 32x32 dot
+               // MAKE_IMAGE_PIXEL
+               if (!(func & 1))        // NOT TILED
                {
-                       if (func & 4)   // 16x16 screen
+                       int mask = (func & 4) ? 0x00800000 : 0x00f80000;
+                       if ((ecx | edx) & mask)
                        {
-                               eax = (ecx >> (11+5)) & 0x007f;
-                               ebx = (edx >> (11-2)) & 0x3f80;
-                       }
-                       else            // 1x1 screen
-                       {
-                               eax = (ecx >> (11+5)) & 0x07;
-                               ebx = (edx >> (11+2)) & 0x38;
+                               if (func & 0x18) goto Next_Pixel;
+                               pixel = 0;
+                               goto Pixel_Out;
                        }
                }
-               else                    // mode 16x16 dot
+
+               if (func & 2)           // mode 32x32 dot
                {
                        if (func & 4)   // 16x16 screen
                        {
-                               eax = (ecx >> (11+4)) & 0x00ff;
-                               ebx = (edx >> (11-4)) & 0xff00;
+                               ebx = ((ecx >> (11+5)) & 0x007f) |
+                                     ((edx >> (11-2)) & 0x3f80);
                        }
                        else            // 1x1 screen
                        {
-                               eax = (ecx >> (11+4)) & 0x0f;
-                               ebx = (edx >> (11+0)) & 0xf0;
+                               ebx = ((ecx >> (11+5)) & 0x07) |
+                                     ((edx >> (11+2)) & 0x38);
                        }
                }
-               ebx += eax;
-
-               // esi = rot_comp.Stamp_Map_Adr;
-               ptrs = (unsigned short *) (Pico_mcd->word_ram2M + rot_comp.Stamp_Map_Adr + ebx*2);
-               edi = ptrs[0] | (ptrs[1] << 16);
-
-               // MAKE_IMAGE_PIXEL
-               if (!(func & 1))        // NOT TILED
+               else                    // mode 16x16 dot
                {
                        if (func & 4)   // 16x16 screen
                        {
-                               if ((edx & 0x00800000) || (ecx & 0x00800000)) goto Pixel_Out;
+                               ebx = ((ecx >> (11+4)) & 0x00ff) |
+                                     ((edx >> (11-4)) & 0xff00);
                        }
                        else            // 1x1 screen
                        {
-                               if ((edx & 0x00f80000) || (ecx & 0x00f80000)) goto Pixel_Out;
+                               ebx = ((ecx >> (11+4)) & 0x0f) |
+                                     ((edx >> (11+0)) & 0xf0);
                        }
                }
-               esi = edi;
+
+               edi = stamp_base[ebx];
+               esi = (edi & 0x7ff) << 7;
+               if (!esi) { pixel = 0; goto Pixel_Out; }
                edi >>= (11+1);
-               esi = (esi & 0x7ff) << 7;
-               if (!esi) goto Pixel_Out;
                edi &= (0x1c>>1);
                eax = ecx;
                ebx = edx;
@@ -254,15 +250,13 @@ static void gfx_do(void)
 
                pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
                if (!(edi & 0x800)) pixel >>= 4;
+               else pixel &= 0x0f;
 
 Pixel_Out:
-//             pixel = 0;
-//Finish:
-               pixel &= 0x0f;
                if (!pixel && (func & 0x18)) goto Next_Pixel;
-               esi = rot_comp.Buffer_Adr + ((rot_comp.XD>>1)^1);       // pixel addr
+               esi = Buffer_Adr + ((XD>>1)^1);                         // pixel addr
                eax = *(Pico_mcd->word_ram2M + esi);                    // old pixel
-               if (rot_comp.XD & 1)
+               if (XD & 1)
                {
                        if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
                        *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
@@ -275,121 +269,138 @@ Pixel_Out:
 
 
 Next_Pixel:
-               ecx += rot_comp.DXS;
-               edx += rot_comp.DYS;
-               rot_comp.XD++;
-               if (rot_comp.XD >= 8)
+               ecx += (DYXS << 16) >> 16;      // _rot_comp.DXS;
+               edx +=  DYXS >> 16;             // _rot_comp.DYS;
+               XD++;
+               if (XD >= 8)
                {
-                       rot_comp.Buffer_Adr += ((rot_comp.Reg_5C & 0x1f) + 1) << 5;
-                       rot_comp.XD = 0;
+                       Buffer_Adr += ((_rot_comp.Reg_5C & 0x1f) + 1) << 5;
+                       XD = 0;
                }
-               rot_comp.H_Dot--;
+               H_Dot--;
        }
+       // end while
 
 
-//nothing_to_draw:
-       rot_comp.YD++;
-       // rot_comp.V_Dot--; // will be done by caller
+// nothing_to_draw:
+       _rot_comp.YD++;
+       // _rot_comp.V_Dot--; // will be done by caller
 }
 
 
-void gfx_cd_update(void)
+PICO_INTERNAL void gfx_cd_update(void)
 {
-       unsigned char *V_Dot = (unsigned char *) &rot_comp.Reg_64;
+       int V_Dot = _rot_comp.Reg_64 & 0xff;
        int jobs;
 
-       dprintf("gfx_cd_update, Reg_64 = %04x", rot_comp.Reg_64);
+       dprintf("gfx_cd_update, Reg_64 = %04x", _rot_comp.Reg_64);
 
-       if (!*V_Dot)
+       if (!V_Dot)
        {
                gfx_completed();
                return;
        }
 
-       jobs = rot_comp.Float_Part >> 16;
+       jobs = _rot_comp.Float_Part >> 16;
 
        if (!jobs)
        {
-               rot_comp.Float_Part += rot_comp.Draw_Speed;
+               _rot_comp.Float_Part += _rot_comp.Draw_Speed;
                return;
        }
 
-       rot_comp.Float_Part &= 0xffff;
-       rot_comp.Float_Part += rot_comp.Draw_Speed;
+       _rot_comp.Float_Part &= 0xffff;
+       _rot_comp.Float_Part += _rot_comp.Draw_Speed;
 
-       while (jobs--)
+       if (PicoOpt & POPT_EN_MCD_GFX)
        {
-               if (PicoOpt & 0x1000)
-                       gfx_do();       // jmp [Jmp_Adr]:
+               unsigned int func = _rot_comp.Function;
+               unsigned int H_Dot = _rot_comp.Reg_62 & 0x1ff;
+               unsigned short *stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + _rot_comp.Stamp_Map_Adr);
 
-               (*V_Dot)--;             // dec byte [V_Dot]
+               while (jobs--)
+               {
+                       gfx_do(func, stamp_base, H_Dot);        // jmp [Jmp_Adr]:
 
-               if (!*V_Dot)
+                       V_Dot--;                                // dec byte [V_Dot]
+                       if (V_Dot == 0)
+                       {
+                               // GFX_Completed:
+                               gfx_completed();
+                               return;
+                       }
+               }
+       }
+       else
+       {
+               if (jobs >= V_Dot)
                {
-                       // GFX_Completed:
                        gfx_completed();
                        return;
                }
+               V_Dot -= jobs;
        }
+
+       _rot_comp.Reg_64 = V_Dot;
 }
 
 
-unsigned int gfx_cd_read(unsigned int a)
+PICO_INTERNAL_ASM unsigned int gfx_cd_read(unsigned int a)
 {
        unsigned int d = 0;
 
        switch (a) {
-               case 0x58: d = rot_comp.Reg_58; break;
-               case 0x5A: d = rot_comp.Reg_5A; break;
-               case 0x5C: d = rot_comp.Reg_5C; break;
-               case 0x5E: d = rot_comp.Reg_5E; break;
-               case 0x60: d = rot_comp.Reg_60; break;
-               case 0x62: d = rot_comp.Reg_62; break;
-               case 0x64: d = rot_comp.Reg_64; break;
+               case 0x58: d = _rot_comp.Reg_58; break;
+               case 0x5A: d = _rot_comp.Reg_5A; break;
+               case 0x5C: d = _rot_comp.Reg_5C; break;
+               case 0x5E: d = _rot_comp.Reg_5E; break;
+               case 0x60: d = _rot_comp.Reg_60; break;
+               case 0x62: d = _rot_comp.Reg_62; break;
+               case 0x64: d = _rot_comp.Reg_64; break;
                case 0x66: break;
                default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
        }
 
        dprintf("gfx_cd_read(%02x) = %04x", a, d);
 
-       return 0;
+       return d;
 }
 
-void gfx_cd_write16(unsigned int a, unsigned int d)
+PICO_INTERNAL_ASM void gfx_cd_write16(unsigned int a, unsigned int d)
 {
        dprintf("gfx_cd_write16(%x, %04x)", a, d);
 
        switch (a) {
                case 0x58: // .Reg_Stamp_Size
-                       rot_comp.Reg_58 = d & 7;
+                       _rot_comp.Reg_58 = d & 7;
                        return;
 
                case 0x5A: // .Reg_Stamp_Adr
-                       rot_comp.Reg_5A = d & 0xffe0;
+                       _rot_comp.Reg_5A = d & 0xffe0;
                        return;
 
                case 0x5C: // .Reg_IM_VCell_Size
-                       rot_comp.Reg_5C = d & 0x1f;
+                       _rot_comp.Reg_5C = d & 0x1f;
                        return;
 
                case 0x5E: // .Reg_IM_Adr
-                       rot_comp.Reg_5E = d & 0xFFF8;
+                       _rot_comp.Reg_5E = d & 0xFFF8;
                        return;
 
                case 0x60: // .Reg_IM_Offset
-                       rot_comp.Reg_60 = d & 0x3f;
+                       _rot_comp.Reg_60 = d & 0x3f;
                        return;
 
                case 0x62: // .Reg_IM_HDot_Size
-                       rot_comp.Reg_62 = d & 0x1ff;
+                       _rot_comp.Reg_62 = d & 0x1ff;
                        return;
 
                case 0x64: // .Reg_IM_VDot_Size
-                       rot_comp.Reg_64 = d & 0xff;     // V_Dot, must be 32bit?
+                       _rot_comp.Reg_64 = d & 0xff;    // V_Dot, must be 32bit?
                        return;
 
                case 0x66: // .Reg_Vector_Adr
-                       rot_comp.Reg_66 = d & 0xfffe;
+                       _rot_comp.Reg_66 = d & 0xfffe;
                        if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
                        gfx_cd_start();
                        return;
@@ -399,9 +410,9 @@ void gfx_cd_write16(unsigned int a, unsigned int d)
 }
 
 
-void gfx_cd_reset(void)
+PICO_INTERNAL void gfx_cd_reset(void)
 {
-       memset(&rot_comp.Reg_58, 0, sizeof(rot_comp));
+       memset(&_rot_comp.Reg_58, 0, sizeof(_rot_comp));
 }
 
 
@@ -409,10 +420,12 @@ void gfx_cd_reset(void)
 
 #include "cell_map.c"
 
+#ifndef UTYPES_DEFINED
 typedef unsigned short u16;
+#endif
 
 // check: Heart of the alien, jaguar xj 220
-void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
+PICO_INTERNAL void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
 {
   unsigned char *base;
   unsigned int asrc, a2;
@@ -434,7 +447,7 @@ void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc
         // AutoIncrement
         a=(u16)(a+inc);
       }
-      rendstatus|=0x10;
+      rendstatus |= PDRAW_SPRITES_MOVED;
       break;
 
     case 3: // cram