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
46 // Render the NAMCO screen
48 void BlitChar(uint8_t * chr, uint8_t * ram)
52 // Each screen RAM is 4K in size, from lowest to highest priority:
53 // $0000-$0FFF, $1000-$1FFF, $2000-$2FFF, $3000-$3FFF
55 // Screen is 288 x 224 pixels, with character and independent sprites.
56 // Tiles are 36 x 28. There are four tile planes, all of them affected by
57 // their own h/vscroll values. Screens are 128 bytes wide by 32 bytes high.
59 // Also note that tiles are 16-bits wide, meaning the screen is really
60 // only 64 characters wide!
62 // Base address is $9000-1 (top 13 bits) + $9002 >> 3 << 7 (top 5 bits)
64 $9000-2: $02 $0C $FF 0000 0010 0000 1100 1111 1111
65 $9004-6: $06 $0E $FF 0000 0110 0000 1110 1111 1111
66 $9400-2: $0A $0B $FF 0000 1010 0000 1011 1111 1111
67 $9404-6: $0E $0D $07 0000 1110 0000 1101 0000 0111
68 ---- PP?H HHHH Hhhh VVVV Vvvv
69 ? = refresh layer bit?
70 PP = which 4K block to write to?
72 uint16_t screenX0 = (((ram[0x9000] << 8) | ram[0x9001]) + 20) & 0x1FF;
73 uint16_t screenX1 = (((ram[0x9004] << 8) | ram[0x9005]) + 18) & 0x1FF;
74 uint16_t screenX2 = (((ram[0x9400] << 8) | ram[0x9401]) + 21) & 0x1FF;
75 uint16_t screenX3 = (((ram[0x9404] << 8) | ram[0x9405]) + 19) & 0x1FF;
77 uint16_t screenY0 = ram[0x9002] + 25;
78 uint16_t screenY1 = ram[0x9006] + 25;
79 uint16_t screenY2 = ram[0x9402] + 25;
80 uint16_t screenY3 = ram[0x9406] + 25;
82 uint16_t ramBlock0 = (ram[0x9000] >> 2) & 0x03;
83 uint16_t ramBlock1 = (ram[0x9004] >> 2) & 0x03;
84 uint16_t ramBlock2 = (ram[0x9400] >> 2) & 0x03;
85 uint16_t ramBlock3 = (ram[0x9404] >> 2) & 0x03;
87 DrawScreen(ramBlock0, screenX0, screenY0, (charBankSwitch ? 2 : 0), false);
89 DrawScreen(ramBlock1, screenX1, screenY1, (charBankSwitch ? 3 : 1));
91 DrawScreen(ramBlock2, screenX2, screenY2, 4);
93 DrawScreen(ramBlock3, screenX3, screenY3, 5);
95 // Draw a msg if needed...
99 // Show GUI if active...
103 // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not
104 // be... (and we don't have to care about that, the OpenGL backend takes
106 for(uint32_t i=0; i<VIRTUAL_SCREEN_WIDTH*VIRTUAL_SCREEN_HEIGHT; i++)
107 scrBuffer[i] = palette[my_scr[i]];
109 RenderScreenBuffer();
116 static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t tileBase, bool transparent/*= true*/)
118 uint16_t ramBase = (ramBlock << 12) | ((yAddress << 4) & 0xF80)
119 | ((xAddress >> 2) & 0x7E);
121 for(uint8_t sy=0; sy<29; sy++)
123 for(uint8_t sx=0; sx<37; sx++)
126 DrawChar(sx, sy, ramBase, tileBase << 16, xAddress & 0x07, yAddress & 0x07, transparent);
133 // Draw character on screen
135 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*/)
137 extern uint8_t charROM[];
138 extern uint8_t gram1[];
140 // Calculate address in RAM of tile to draw
141 uint16_t addr = (ramBase & 0xF000) | ((ramBase + (sy << 7)) & 0x0F80)
142 | ((ramBase + (sx << 1)) & 0x7F);
143 uint16_t tile = ((gram1[addr + 1] << 8) | gram1[addr]) & 0x03FF;
144 // Yes, it really requires all 8 bits... even though the bottom 3 are used
145 // as a tile address!
146 uint8_t color = gram1[addr + 1];
147 uint32_t chind = tileBase + (tile << 6);
149 int xStart = (int)(sx * 8) - xScroll;
150 int yStart = (int)(sy * 8) - yScroll;
151 int32_t sc_addr = xStart + (yStart * 288);
153 for(int y=0; y<8; y++)
155 for(int x=0; x<8; x++)
158 if (((xStart + x) < 0) || ((xStart + x) >= 288)
159 || ((yStart + y) < 0) || ((yStart + y) >= 224))
166 if (!(transparent && (charROM[chind] == 7)))
167 my_scr[sc_addr] = ccolor[color][charROM[chind]];
173 sc_addr += (288 - 8); // Do next line of char...
179 // Copy sprites in sprite RAM from positions 4-9 to 10-15
180 // (N.B.: This still doesn't solve the shifting signs on the walls problem...)
182 void CopySprites(void)
184 extern uint8_t gram1[];
186 for(uint16_t i=0x5800; i<0x6000; i+=0x10)
188 for(uint16_t j=4; j<=9; j++)
190 gram1[i + j + 6] = gram1[i + j];
197 // Draw sprites at priority level (new code)
199 void DrawSprites(uint8_t priority)
204 // ------ --------------------------------------------------------------------
205 // 4 h.fb .nnn (f = horz. flip, h = horz. expand, b = sprite offset lo
206 // bit, nnn = upper bits of sprite #)
207 // 5 Lower 7 bits of sprite #
208 // 6 Sprite color index (top 7 bits only), bottom bit is bit 8 of X
210 // 7 Sprite X position (bits 0-7)
211 // 8 Top two bits are sprite priority, bits 4 & 2 are sprite offset hi
213 // 9 Sprite Y position (192 - value)
215 extern uint8_t gram1[]; // Game RAM space
217 for(uint16_t i=0x5800; i<0x6000; i+=0x10)
219 if ((gram1[i + 8 + 6] & 0xC0) == priority) // Check for correct layer...
221 spr_color_index = gram1[i + 6 + 6] >> 1; // Set color...
222 uint16_t x = ((gram1[i + 6 + 6] & 0x01) << 8) | gram1[i + 7 + 6];
225 x -= 512; // Handle neg x values
227 uint16_t y = 192 - gram1[i + 9 + 6];
228 uint8_t flip = gram1[i + 4 + 6] & 0x20; // Horizontal flip
229 uint32_t spr_num = ((gram1[i + 4 + 6] & 0x07) << 9)
230 | ((gram1[i + 5 + 6] & 0x7F) << 2)
231 | ((gram1[i + 4 + 6] & 0x10) >> 4)
232 | ((gram1[i + 8 + 6] & 0x10) >> 3);
235 Sprite(spr_num, x, y, flip, gram1[i + 4 + 6] & 0x80, gram1[i + 8 + 6] & 0x04);
241 static inline void DrawSpriteBlock(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
243 extern uint8_t spr_rom[];
245 for(uint16_t sy=0; sy<16; sy++)
247 for(uint16_t sx=xStart; sx<xEnd; sx+=xInc)
249 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
250 uint16_t spy = y + sy, spx = x + sx; // Need to optimize this clipping! [eh?]
252 // This handles negative values, by casting as unsigned
253 uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
256 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
261 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
267 static inline void DrawSpriteBlock2(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
269 extern uint8_t spr_rom[];
271 for(uint16_t sy=0; sy<16; sy++)
273 for(uint16_t sx=xStart; sx!=xEnd; sx+=xInc)
275 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
276 uint16_t spy = y + sy, spx = x + sx; // Need to optimize this clipping! [eh?]
278 // This handles negative values, by casting as unsigned
279 uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
282 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
287 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
296 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip,
297 uint8_t horiz_bl, uint8_t vert_bl)
299 // 128 bytes per sprite (16 x 16 chunks, 4 bits per pixel)
307 DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
310 DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
312 sprnum += 128; // Advance to next...
316 y += 16; // Do next row...
318 DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
321 DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
327 DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
329 DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
332 sprnum += 128; // If single, skip sprite...
336 y += 16; // Adjust Y coord...
339 DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
341 DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
347 int FindPCXName(void)
349 static int pcxNum = -1; // This needs to go elsewhere... (or does it?)
355 while (pcxNum < 10000)
357 sprintf(filename, "thnd%04i.pcx", pcxNum);
359 // file does not exist - we can create it
360 if ((fr = fopen(filename, "r")) == NULL)
370 void SavePCXSnapshot(void)
375 int bytesPerLine = 288;
376 int i = FindPCXName();
381 sprintf(filename, "thnd%04i.pcx", i);
382 FILE * fw = fopen(filename, "wb");
389 fputc(0x0A, fw); // PCX signature
390 fputc(0x05, fw); // Version 5
391 fputc(0x01, fw); // RLE encoding
392 fputc(0x08, fw); // Bits per pixel
396 fputc(0, fw); // XMin=0,YMin=0
397 fputc(xMax & 0xFF, fw);
398 fputc(xMax >> 8, fw);
399 fputc(yMax & 0xFF, fw);
400 fputc(yMax >> 8, fw);
404 fputc(0, fw); // Unknown DPI
410 fputc(0, fw); // Reserved
411 fputc(1, fw); // Number of bit planes
412 fputc(bytesPerLine & 0xFF, fw);
413 fputc(bytesPerLine >> 8, fw);
415 fputc(0, fw); // Palette info - unused
416 fputc((xMax + 1) & 0xFF, fw);
417 fputc((xMax + 1) >> 8, fw);
418 fputc((yMax + 1) & 0xFF, fw);
419 fputc((yMax + 1) >> 8, fw); // Screen resolution
425 uint8_t * mem = my_scr;
427 for(int line=0; line<=yMax; line++)
431 while (xpos < bytesPerLine)
438 while ((*mem == last) && (xpos < bytesPerLine) && (count < 63))
445 if ((count > 1) || ((last & 0xC0) == 0xC0))
447 fputc(0xC0 | (count & 0x3F), fw);
448 fputc(last & 0xFF, fw);
451 fputc(last & 0xFF, fw);
455 // Write out the palette
458 for(int i=0; i<256; i++)
460 fputc(palette[i] & 0xFF, fw);
461 fputc((palette[i] >> 8) & 0xFF, fw);
462 fputc((palette[i] >> 16) & 0xFF, fw);