4 // This emulates the NAMCO tile/sprite hardware
7 // (C) 2003 Underground Software
9 // JLH = James Hammons <jlhamm@acm.org>
12 // --- ---------- -----------------------------------------------------------
13 // JLH 03/12/2003 Ported this steaming pile of crap from VESA to SDL
14 // JLH 04/04/2014 Ported to SDL 2. Much less crappy.
21 #include <string> // For memset()
25 // Private function prototypes
27 void DrawSprites(uint8_t priority);
28 int FindPCXName(void);
29 static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t baseAddr, bool transparent = true);
30 static void DrawChar(uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t xScroll, uint32_t yScroll, bool transparent = true);
31 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip, uint8_t horiz_bl, uint8_t vert_bl);
33 // Private global variables
35 uint8_t my_scr[0x14000]; // Screen buffer...
36 uint32_t palette[256]; // Screen palette
37 uint8_t ccolor[256][8]; // Character colors
38 uint8_t scolor[128][16]; // Sprite colors
39 bool charBankSwitch; // Character bank switch
40 uint8_t spr_color_index; // Sprite color index
42 extern bool show_text; // Whether or not to show text
45 // Render the NAMCO screen
47 void BlitChar(uint8_t * chr, uint8_t * ram)
51 // Each screen RAM is 4K in size, from lowest to highest priority:
52 // $0000-$0FFF, $1000-$1FFF, $2000-$2FFF, $3000-$3FFF
54 // Screen is 288 x 224 pixels, with character and independent sprites.
55 // Tiles are 36 x 28. There are four tile planes, all of them affected by
56 // their own h/vscroll values. Screens are 128 bytes wide by 32 bytes high.
58 // Also note that tiles are 16-bits wide, meaning the screen is really
59 // only 64 characters wide!
61 // Base address is $9000-1 (top 13 bits) + $9002 >> 3 << 7 (top 5 bits)
63 $9000-2: $02 $0C $FF 0000 0010 0000 1100 1111 1111
64 $9004-6: $06 $0E $FF 0000 0110 0000 1110 1111 1111
65 $9400-2: $0A $0B $FF 0000 1010 0000 1011 1111 1111
66 $9404-6: $0E $0D $07 0000 1110 0000 1101 0000 0111
67 ---- PP?H HHHH Hhhh VVVV Vvvv
68 ? = refresh layer bit?
69 PP = which 4K block to write to?
71 uint16_t screenX0 = (((ram[0x9000] << 8) | ram[0x9001]) + 20) & 0x1FF;
72 uint16_t screenX1 = (((ram[0x9004] << 8) | ram[0x9005]) + 18) & 0x1FF;
73 uint16_t screenX2 = (((ram[0x9400] << 8) | ram[0x9401]) + 21) & 0x1FF;
74 uint16_t screenX3 = (((ram[0x9404] << 8) | ram[0x9405]) + 19) & 0x1FF;
76 uint16_t screenY0 = ram[0x9002] + 25;
77 uint16_t screenY1 = ram[0x9006] + 25;
78 uint16_t screenY2 = ram[0x9402] + 25;
79 uint16_t screenY3 = ram[0x9406] + 25;
81 uint16_t ramBlock0 = (ram[0x9000] >> 2) & 0x03;
82 uint16_t ramBlock1 = (ram[0x9004] >> 2) & 0x03;
83 uint16_t ramBlock2 = (ram[0x9400] >> 2) & 0x03;
84 uint16_t ramBlock3 = (ram[0x9404] >> 2) & 0x03;
86 DrawScreen(ramBlock0, screenX0, screenY0, (charBankSwitch ? 2 : 0), false);
88 DrawScreen(ramBlock1, screenX1, screenY1, (charBankSwitch ? 3 : 1));
90 DrawScreen(ramBlock2, screenX2, screenY2, 4);
92 DrawScreen(ramBlock3, screenX3, screenY3, 5);
94 // Draw a msg if needed...
98 // Show GUI if active...
102 // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not
103 // be... (and we don't have to care about that, the OpenGL backend takes
105 for(uint32_t i=0; i<VIRTUAL_SCREEN_WIDTH*VIRTUAL_SCREEN_HEIGHT; i++)
106 scrBuffer[i] = palette[my_scr[i]];
108 RenderScreenBuffer();
114 static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t tileBase, bool transparent/*= true*/)
116 uint16_t ramBase = (ramBlock << 12) | ((yAddress << 4) & 0xF80)
117 | ((xAddress >> 2) & 0x7E);
119 for(uint8_t sy=0; sy<29; sy++)
121 for(uint8_t sx=0; sx<37; sx++)
122 DrawChar(sx, sy, ramBase, tileBase << 16, xAddress & 0x07, yAddress & 0x07, transparent);
127 // Draw character on screen
129 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*/)
131 extern uint8_t charROM[];
132 extern uint8_t gram1[];
134 // Calculate address in RAM of tile to draw
135 uint16_t addr = (ramBase & 0xF000) | ((ramBase + (sy << 7)) & 0x0F80)
136 | ((ramBase + (sx << 1)) & 0x7F);
137 uint16_t tile = ((gram1[addr + 1] << 8) | gram1[addr]) & 0x03FF;
138 // Yes, it really requires all 8 bits... even though the bottom 3 are used
139 // as a tile address!
140 uint8_t color = gram1[addr + 1];
141 uint32_t chind = tileBase + (tile << 6);
143 int xStart = (int)(sx * 8) - xScroll;
144 int yStart = (int)(sy * 8) - yScroll;
145 int32_t sc_addr = xStart + (yStart * 288);
147 for(int y=0; y<8; y++)
149 for(int x=0; x<8; x++)
152 if (((xStart + x) < 0) || ((xStart + x) >= 288)
153 || ((yStart + y) < 0) || ((yStart + y) >= 224))
160 if (!(transparent && (charROM[chind] == 7)))
161 my_scr[sc_addr] = ccolor[color][charROM[chind]];
167 sc_addr += (288 - 8); // Do next line of char...
172 // Copy sprites in sprite RAM from positions 4-9 to 10-15
174 void CopySprites(void)
176 extern uint8_t gram1[];
178 for(uint16_t i=0x5800; i<0x6000; i+=0x10)
180 for(uint16_t j=4; j<=9; j++)
182 gram1[i + j + 6] = gram1[i + j];
188 // Draw sprites at priority level
189 // We read from the sprite copy RAM to render sprites, as that seems to be the
190 // way the real H/W does it.
192 void DrawSprites(uint8_t priority)
197 // ------ --------------------------------------------------------------------
198 // 4 (10) h.fb .nnn (f = horz. flip, h = horz. expand, b = sprite offset lo
199 // bit, nnn = upper bits of sprite #)
200 // 5 (11) Lower 7 bits of sprite #
201 // 6 (12) Sprite color index (top 7 bits only), bottom bit is bit 8 of X
203 // 7 (13) Sprite X position (bits 0-7)
204 // 8 (14) Top two bits are sprite priority, bits 4 & 2 are sprite offset hi
206 // 9 (15) Sprite Y position (192 - value)
208 extern uint8_t gram1[]; // Game RAM space
210 for(uint16_t i=0x5800; i<0x6000; i+=0x10)
212 uint8_t * sprRAM = &gram1[i];
214 // Skip sprite if it's not on the correct layer...
215 if ((sprRAM[14] & 0xC0) != priority)
218 spr_color_index = sprRAM[12] >> 1; // Set color...
219 uint16_t x = ((sprRAM[12] & 0x01) << 8) | sprRAM[13];
222 x -= 512; // Handle neg x values
224 uint16_t y = 192 - sprRAM[15];
225 uint8_t horzFlip = sprRAM[10] & 0x20;
226 uint32_t spr_num = ((sprRAM[10] & 0x07) << 9)
227 | ((sprRAM[11] & 0x7F) << 2)
228 | ((sprRAM[10] & 0x10) >> 4)
229 | ((sprRAM[14] & 0x10) >> 3);
231 // Draw the sprite...
232 Sprite(spr_num, x, y, horzFlip, sprRAM[10] & 0x80, sprRAM[14] & 0x04);
236 static inline void DrawSpriteBlock(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
238 extern uint8_t spr_rom[];
240 for(uint16_t sy=0; sy<16; sy++)
242 for(uint16_t sx=xStart; sx<xEnd; sx+=xInc)
244 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
245 uint16_t spy = y + sy, spx = x + sx; // Need to optimize this clipping! [eh?]
247 // This handles negative values, by casting as unsigned
248 uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
251 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
256 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
261 static inline void DrawSpriteBlock2(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
263 extern uint8_t spr_rom[];
265 for(uint16_t sy=0; sy<16; sy++)
267 for(uint16_t sx=xStart; sx!=xEnd; sx+=xInc)
269 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
270 uint16_t spy = y + sy, spx = x + sx; // Need to optimize this clipping! [eh?]
272 // This handles negative values, by casting as unsigned
273 uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
276 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
281 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
289 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip,
290 uint8_t horiz_bl, uint8_t vert_bl)
292 // 128 bytes per sprite (16 x 16 chunks, 4 bits per pixel)
300 DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
303 DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
305 sprnum += 128; // Advance to next...
309 y += 16; // Do next row...
311 DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
314 DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
320 DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
322 DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
325 sprnum += 128; // If single, skip sprite...
329 y += 16; // Adjust Y coord...
332 DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
334 DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
339 int FindPCXName(void)
341 static int pcxNum = -1; // This needs to go elsewhere... (or does it?)
347 while (pcxNum < 10000)
349 sprintf(filename, "thnd%04i.pcx", pcxNum);
351 // file does not exist - we can create it
352 if ((fr = fopen(filename, "r")) == NULL)
361 void SavePCXSnapshot(void)
366 int bytesPerLine = 288;
367 int i = FindPCXName();
372 sprintf(filename, "thnd%04i.pcx", i);
373 FILE * fw = fopen(filename, "wb");
380 fputc(0x0A, fw); // PCX signature
381 fputc(0x05, fw); // Version 5
382 fputc(0x01, fw); // RLE encoding
383 fputc(0x08, fw); // Bits per pixel
387 fputc(0, fw); // XMin=0,YMin=0
388 fputc(xMax & 0xFF, fw);
389 fputc(xMax >> 8, fw);
390 fputc(yMax & 0xFF, fw);
391 fputc(yMax >> 8, fw);
395 fputc(0, fw); // Unknown DPI
401 fputc(0, fw); // Reserved
402 fputc(1, fw); // Number of bit planes
403 fputc(bytesPerLine & 0xFF, fw);
404 fputc(bytesPerLine >> 8, fw);
406 fputc(0, fw); // Palette info - unused
407 fputc((xMax + 1) & 0xFF, fw);
408 fputc((xMax + 1) >> 8, fw);
409 fputc((yMax + 1) & 0xFF, fw);
410 fputc((yMax + 1) >> 8, fw); // Screen resolution
416 uint8_t * mem = my_scr;
418 for(int line=0; line<=yMax; line++)
422 while (xpos < bytesPerLine)
429 while ((*mem == last) && (xpos < bytesPerLine) && (count < 63))
436 if ((count > 1) || ((last & 0xC0) == 0xC0))
438 fputc(0xC0 | (count & 0x3F), fw);
439 fputc(last & 0xFF, fw);
442 fputc(last & 0xFF, fw);
446 // Write out the palette
449 for(int i=0; i<256; i++)
451 fputc(palette[i] & 0xFF, fw);
452 fputc((palette[i] >> 8) & 0xFF, fw);
453 fputc((palette[i] >> 16) & 0xFF, fw);