]> Shamusworld >> Repos - thunder/blobdiff - src/screen.cpp
Added DIP switch fungibility, misc. code cleanups.
[thunder] / src / screen.cpp
index dda0f6e665eb42f349723213b4b688e370e8384c..d3f5672c214db805ef7779b2e120f6a7c34e933e 100644 (file)
@@ -1,12 +1,9 @@
 //
 // Screen Handler
 //
-// This sets screen to VESA 320x240 LFB so we can use the WHOLE laptop screen
-// Now with VESA2 support!
-// Also, support routines for video hardware emulation are included
+// This emulates the NAMCO tile/sprite hardware
 //
 // by James Hammons
-//
 // (C) 2003 Underground Software
 //
 // JLH = James Hammons <jlhamm@acm.org>
 
 void DrawSprites(uint8_t priority);
 int FindPCXName(void);
-//static void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t scrollOffset, bool transparent = true);
-static void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t xScroll = 0, uint32_t yScroll = 0, bool transparent = true);
+static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t baseAddr, bool transparent = true);
+static void DrawChar(uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t xScroll, uint32_t yScroll, bool transparent = true);
 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip, uint8_t horiz_bl, uint8_t vert_bl);
 
 // Private global variables
 
-uint8_t my_scr[0x14000];                       // Screen buffer...
-//uint8_t palette[768];                                // Screen palette
-uint32_t palette[256];                         // Screen palette
-uint8_t ccolor[256][8];                                // Character colors
-uint8_t scolor[128][16];                       // Sprite colors
-bool charbase;                                         // Character base pointer...
-uint8_t hScrollOffset;                         // Horizontal scroll offset
-uint8_t vScrollOffset;                         // Vertical scroll offset
-uint8_t spr_color_index;                       // Sprite color index
-//uint32_t hoffsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };// Scroll offsets...
-//uint32_t voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
-uint32_t voffsets[8] = { 288*0, 288*1, 288*2, 288*3, 288*4, 288*5, 288*6, 288*7 };
+uint8_t my_scr[0x14000];               // Screen buffer...
+uint32_t palette[256];                 // Screen palette
+uint8_t ccolor[256][8];                        // Character colors
+uint8_t scolor[128][16];               // Sprite colors
+bool charBankSwitch;                   // Character bank switch
+uint8_t spr_color_index;               // Sprite color index
 
-extern bool show_text;                         // Whether or not to show text
-extern bool show_scr;                          // Whether or not to show screen
+extern bool show_text;                 // Whether or not to show text
 
 
 //
 // Render the NAMCO screen
 //
-void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
+void BlitChar(uint8_t * chr, uint8_t * ram)
 {
        // Screen structure:
+       //
+       // Each screen RAM is 4K in size, from lowest to highest priority:
+       // $0000-$0FFF, $1000-$1FFF, $2000-$2FFF, $3000-$3FFF
+       //
        // Screen is 288 x 224 pixels, with character and independent sprites.
-       // Tiles are 36 x 28. There are four tile planes, three of them affected by
-       // the h/vscroll values, the last one is a static display (also has highest
-       // priority). Screens are 128 bytes wide by 32 bytes high.
-
-       if (show_scr)
-       {
-               uint32_t sc_base = ((ram[0x9000] << 8) | ram[0x9001]) + 4;      // Adjust hscroll val
-               hScrollOffset = sc_base & 0x07;         // Horiz. fine scroll offset
-               sc_base = (sc_base & 0xFFF8) >> 2;      // Skip odds..
-               uint8_t vsc_base = ((ram[0x9002] + 1) & 0xF8) >> 3;// Vertical scroll addr adjust
-               vScrollOffset = ((ram[0x9002] + 1) & 0x07);     // Vertical fine scroll amount
-
-               uint32_t scp0 = 0x0180 | ((sc_base + 0x04) & 0x7F);     /*0x0188;*/
-               uint32_t scp1 = 0x1180 | ((sc_base + 0x04) & 0x7F);     /*0x1188;*/
-               uint32_t scp2 = 0x2180 | ((sc_base + 0x04) & 0x7F);     /*0x2188;*/
-               uint32_t scp3 = 0x3208;
-
-               scp0 += vsc_base * 0x80;
-               scp0 &= 0x0FFF;                                         // Set vertical scroll addr
-               scp1 += vsc_base * 0x80;
-               scp1 = 0x1000 | (scp1 & 0x0FFF);        // Set vertical scroll addr
-               scp2 += vsc_base * 0x80;
-               scp2 = 0x2000 | (scp2 & 0x0FFF);        // Set vertical scroll addr
-
-               // Layer 0 (bottom layer)
-               for(uint8_t sy=0; sy<29; sy++)
-               {
-                       for(uint8_t sx=0; sx<37; sx++)
-                               DrawChar(chr, ram, sx, sy, scp0, (charbase ? 0x20000 : 0x00000), hScrollOffset, vScrollOffset, false);
-
-                       scp0 += 0x80;
-                       scp0 = 0x0000 | (scp0 & 0x0FFF);
-               }
-
-               // Draw sprites at lowest layer...
-               DrawSprites(0x40);
-
-               // Layer 1
-               for(uint8_t sy=0; sy<29; sy++)
-               {
-                       for(uint8_t sx=0; sx<37; sx++)
-                               DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x30000 : 0x10000), hScrollOffset, vScrollOffset);
-
-                       scp1 += 0x80;
-                       scp1 = 0x1000 | (scp1 & 0x0FFF);
-               }
-
-               // Draw sprites under layer #2...
-               DrawSprites(0x80);
-
-               // Layer 2
-               for(uint8_t sy=0; sy<29; sy++)
-               {
-                       for(uint8_t sx=0; sx<37; sx++)
-                               DrawChar(chr, ram, sx, sy, scp2, 0x40000, hScrollOffset, vScrollOffset);
-
-                       scp2 += 0x80;
-                       scp2 = 0x2000 | (scp2 & 0x0FFF);
-               }
-
-               // Draw highest priority sprites...
-               DrawSprites(0xC0);
-
-               // Layer 3 (top layer)
-               for(uint8_t sy=0; sy<28; sy++)
-               {
-                       for(uint8_t sx=0; sx<36; sx++)
-                               DrawChar(chr, ram, sx, sy, scp3, 0x50000);
-
-                       scp3 += 0x80;
-               }
-       }
+       // Tiles are 36 x 28. There are four tile planes, all of them affected by
+       // their own h/vscroll values. Screens are 128 bytes wide by 32 bytes high.
+       //
+       // Also note that tiles are 16-bits wide, meaning the screen is really
+       // only 64 characters wide!
+
+       // Base address is $9000-1 (top 13 bits) + $9002 >> 3 << 7 (top 5 bits)
+/*
+$9000-2: $02 $0C $FF  0000 0010  0000 1100  1111 1111
+$9004-6: $06 $0E $FF  0000 0110  0000 1110  1111 1111
+$9400-2: $0A $0B $FF  0000 1010  0000 1011  1111 1111
+$9404-6: $0E $0D $07  0000 1110  0000 1101  0000 0111
+                      ---- PP?H  HHHH Hhhh  VVVV Vvvv
+? = refresh layer bit?
+PP = which 4K block to write to?
+*/
+       uint16_t screenX0 = (((ram[0x9000] << 8) | ram[0x9001]) + 20) & 0x1FF;
+       uint16_t screenX1 = (((ram[0x9004] << 8) | ram[0x9005]) + 18) & 0x1FF;
+       uint16_t screenX2 = (((ram[0x9400] << 8) | ram[0x9401]) + 21) & 0x1FF;
+       uint16_t screenX3 = (((ram[0x9404] << 8) | ram[0x9405]) + 19) & 0x1FF;
+
+       uint16_t screenY0 = ram[0x9002] + 25;
+       uint16_t screenY1 = ram[0x9006] + 25;
+       uint16_t screenY2 = ram[0x9402] + 25;
+       uint16_t screenY3 = ram[0x9406] + 25;
+
+       uint16_t ramBlock0 = (ram[0x9000] >> 2) & 0x03;
+       uint16_t ramBlock1 = (ram[0x9004] >> 2) & 0x03;
+       uint16_t ramBlock2 = (ram[0x9400] >> 2) & 0x03;
+       uint16_t ramBlock3 = (ram[0x9404] >> 2) & 0x03;
+
+       DrawScreen(ramBlock0, screenX0, screenY0, (charBankSwitch ? 2 : 0), false);
+       DrawSprites(0x40);
+       DrawScreen(ramBlock1, screenX1, screenY1, (charBankSwitch ? 3 : 1));
+       DrawSprites(0x80);
+       DrawScreen(ramBlock2, screenX2, screenY2, 4);
+       DrawSprites(0xC0);
+       DrawScreen(ramBlock3, screenX3, screenY3, 5);
 
        // Draw a msg if needed...
        if (show_text)
@@ -143,34 +103,49 @@ void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
        // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not
        // be... (and we don't have to care about that, the OpenGL backend takes
        // care of it.)
+       for(uint32_t i=0; i<VIRTUAL_SCREEN_WIDTH*VIRTUAL_SCREEN_HEIGHT; i++)
+               scrBuffer[i] = palette[my_scr[i]];
+
+       RenderScreenBuffer();
+}
+
 
-       uint32_t src = 0;
+//
+// Render tilemap
+//
+static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t tileBase, bool transparent/*= true*/)
+{
+       uint16_t ramBase = (ramBlock << 12) | ((yAddress << 4) & 0xF80)
+               | ((xAddress >> 2) & 0x7E);
 
-       for(int i=0; i<VIRTUAL_SCREEN_HEIGHT; i++)
+       for(uint8_t sy=0; sy<29; sy++)
        {
-               for (int j=0; j<VIRTUAL_SCREEN_WIDTH; j++)
+               for(uint8_t sx=0; sx<37; sx++)
                {
-                       scrBuffer[src] = palette[my_scr[src]];
-                       src++;
+
+                       DrawChar(sx, sy, ramBase, tileBase << 16, xAddress & 0x07, yAddress & 0x07, transparent);
                }
        }
-
-       RenderScreenBuffer();
 }
 
 
 //
 // Draw character on screen
 //
-//static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t scrollOffset, bool transparent/*= true*/)
-static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t xScroll/*= 0*/, uint32_t yScroll/*= 0*/, bool transparent/*= true*/)
+static inline void DrawChar(uint8_t sx, uint8_t sy, uint16_t ramBase, uint32_t tileBase, uint32_t xScroll, uint32_t yScroll, bool transparent/*= true*/)
 {
-       uint8_t scp_lo = (scp + (sx << 1)) & 0x7F;// Let LO byte wrap only...
-       uint16_t sp2 = (scp & 0xFF80) | scp_lo;
-       uint8_t tile  = ram[sp2++];
-       uint8_t index = ram[sp2] & 0x03;
-       uint8_t color = ram[sp2];
-       uint32_t chind = baseAddr + (((index << 8) + tile) * 64);
+       extern uint8_t charROM[];
+       extern uint8_t gram1[];
+
+       // Calculate address in RAM of tile to draw
+       uint16_t addr = (ramBase & 0xF000) | ((ramBase + (sy << 7)) & 0x0F80)
+               | ((ramBase + (sx << 1)) & 0x7F);
+       uint16_t tile  = ((gram1[addr + 1] << 8) | gram1[addr]) & 0x03FF;
+       // Yes, it really requires all 8 bits... even though the bottom 3 are used
+       // as a tile address!
+       uint8_t color = gram1[addr + 1];
+       uint32_t chind = tileBase + (tile << 6);
+
        int xStart = (int)(sx * 8) - xScroll;
        int yStart = (int)(sy * 8) - yScroll;
        int32_t sc_addr = xStart + (yStart * 288);
@@ -179,6 +154,7 @@ static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy
        {
                for(int x=0; x<8; x++)
                {
+                       // Clipping...
                        if (((xStart + x) < 0) || ((xStart + x) >= 288)
                                || ((yStart + y) < 0) || ((yStart + y) >= 224))
                        {
@@ -187,13 +163,8 @@ static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy
                                continue;
                        }
 
-                       if (transparent)
-                       {
-                               if (chr[chind] != 7)
-                                       my_scr[sc_addr] = ccolor[color][chr[chind]];
-                       }
-                       else
-                               my_scr[sc_addr] = ccolor[color][chr[chind]];
+                       if (!(transparent && (charROM[chind] == 7)))
+                               my_scr[sc_addr] = ccolor[color][charROM[chind]];
 
                        sc_addr++;
                        chind++;
@@ -204,6 +175,24 @@ static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy
 }
 
 
+//
+// Copy sprites in sprite RAM from positions 4-9 to 10-15
+// (N.B.: This still doesn't solve the shifting signs on the walls problem...)
+//
+void CopySprites(void)
+{
+       extern uint8_t gram1[];
+
+       for(uint16_t i=0x5800; i<0x6000; i+=0x10)
+       {
+               for(uint16_t j=4; j<=9; j++)
+               {
+                       gram1[i + j + 6] = gram1[i + j];
+               }
+       }
+}
+
+
 //
 // Draw sprites at priority level (new code)
 //
@@ -227,23 +216,23 @@ void DrawSprites(uint8_t priority)
 
        for(uint16_t i=0x5800; i<0x6000; i+=0x10)
        {
-               if ((gram1[i + 8] & 0xC0) == priority)          // Check for correct layer...
+               if ((gram1[i + 8 + 6] & 0xC0) == priority)              // Check for correct layer...
                {
-                       spr_color_index = gram1[i + 6] >> 1;    // Set color...
-                       uint16_t x = ((gram1[i + 6] & 0x01) << 8) | gram1[i + 7];
+                       spr_color_index = gram1[i + 6 + 6] >> 1;        // Set color...
+                       uint16_t x = ((gram1[i + 6 + 6] & 0x01) << 8) | gram1[i + 7 + 6];
 
                        if (x > 512 - 32)
                                x -= 512;                                                       // Handle neg x values
 
-                       uint16_t y = 192 - gram1[i + 9];
-                       uint8_t flip = gram1[i + 4] & 0x20;             // Horizontal flip
-                       uint32_t spr_num = ((gram1[i + 4] & 0x07) << 9)
-                               | ((gram1[i + 5] & 0x7F) << 2)
-                               | ((gram1[i + 4] & 0x10) >> 4)
-                               | ((gram1[i + 8] & 0x10) >> 3);
+                       uint16_t y = 192 - gram1[i + 9 + 6];
+                       uint8_t flip = gram1[i + 4 + 6] & 0x20;         // Horizontal flip
+                       uint32_t spr_num = ((gram1[i + 4 + 6] & 0x07) << 9)
+                               | ((gram1[i + 5 + 6] & 0x7F) << 2)
+                               | ((gram1[i + 4 + 6] & 0x10) >> 4)
+                               | ((gram1[i + 8 + 6] & 0x10) >> 3);
 
                        // Draw sprite...
-                       Sprite(spr_num, x, y, flip, gram1[i + 4] & 0x80, gram1[i + 8] & 0x04);
+                       Sprite(spr_num, x, y, flip, gram1[i + 4 + 6] & 0x80, gram1[i + 8 + 6] & 0x04);
                }
        }
 }