4 // This sets screen to VESA 320x240 LFB so we can use the WHOLE laptop screen
5 // Now with VESA2 support!
6 // Also, support routines for video hardware emulation are included
10 // (C) 2003 Underground Software
12 // JLH = James Hammons <jlhamm@acm.org>
15 // --- ---------- -----------------------------------------------------------
16 // JLH 03/12/2003 Ported this steaming pile of crap from VESA to SDL
23 #include <string> // For memset()
26 // Private function prototypes
28 void DrawSprites(uint8_t priority);
29 int FindPCXName(void);
30 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);
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 uint8_t palette[768]; // Screen palette
37 uint8_t ccolor[256][8]; // Character colors
38 uint8_t scolor[128][16]; // Sprite colors
39 bool charbase; // Character base pointer...
40 uint8_t hScrollOffset; // Horizontal scroll offset
41 uint8_t vScrollOffset; // Vertical scroll offset
42 uint8_t spr_color_index; // Sprite color index
43 uint32_t hoffsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };// Scroll offsets...
44 uint32_t voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
46 extern bool show_text; // Whether or not to show text
47 extern bool show_scr; // Whether or not to show screen
51 // Render the NAMCO screen
53 void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
56 // Screen is 288 x 224 pixels, with character and independent sprites. Tiles are 36 x 28.
57 // There are four tile planes, three of them affected by the h/vscroll values, the last
58 // one is a static display (also has highest priority). Screens are 128 bytes wide by
63 uint32_t sc_base = ((ram[0x9000] << 8) | ram[0x9001]) + 4; // Adjust hscroll val
64 hScrollOffset = sc_base & 0x07; // Horiz. fine scroll offset
65 sc_base = (sc_base & 0xFFF8) >> 2; // Skip odds..
66 uint8_t vsc_base = ((ram[0x9002] + 1) & 0xF8) >> 3;// Vertical scroll addr adjust
67 vScrollOffset = ((ram[0x9002] + 1) & 0x07); // Vertical fine scroll amount
69 uint32_t scp0 = 0x0180 | ((sc_base + 0x04) & 0x7F); /*0x0188;*/
70 uint32_t scp1 = 0x1180 | ((sc_base + 0x04) & 0x7F); /*0x1188;*/
71 uint32_t scp2 = 0x2180 | ((sc_base + 0x04) & 0x7F); /*0x2188;*/
72 uint32_t scp3 = 0x3208;
74 scp0 += vsc_base * 0x80;
75 scp0 &= 0x0FFF; // Set vertical scroll addr
76 scp1 += vsc_base * 0x80;
77 scp1 = 0x1000 | (scp1 & 0x0FFF); // Set vertical scroll addr
78 scp2 += vsc_base * 0x80;
79 scp2 = 0x2000 | (scp2 & 0x0FFF); // Set vertical scroll addr
81 // Layer 0 (bottom layer)
82 for(uint8_t sy=0; sy<29; sy++)
84 for(uint8_t sx=0; sx<37; sx++)
85 DrawChar(chr, ram, sx, sy, scp0, (charbase ? 0x20000 : 0x00000), 0, false);
88 scp0 = 0x0000 | (scp0 & 0x0FFF);
91 DrawSprites(0x40); // Draw sprites at lowest layer...
94 for(uint8_t sy=0; sy<29; sy++)
96 for(uint8_t sx=0; sx<37; sx++)
97 DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x30000 : 0x10000), 0);
100 scp1 = 0x1000 | (scp1 & 0x0FFF);
103 DrawSprites(0x80); // Draw sprites under layer #2...
106 for(uint8_t sy=0; sy<29; sy++)
108 for(uint8_t sx=0; sx<37; sx++)
109 DrawChar(chr, ram, sx, sy, scp2, 0x40000, 0);
112 scp2 = 0x2000 | (scp2 & 0x0FFF);
115 DrawSprites(0xC0); // Draw highest priority sprites...
117 // Layer 3 (top layer)
118 for(uint8_t sy=0; sy<28; sy++)
120 for(uint8_t sx=0; sx<36; sx++)
121 DrawChar(chr, ram, sx, sy, scp3, 0x50000, hScrollOffset + voffsets[vScrollOffset]);
128 DrawText(); // Draw a msg if needed...
131 DrawGUI(); // Show GUI if active...
133 if (SDL_LockSurface(scr) < 0)
135 // fprintf(stderr, "Couldn't lock the display surface: %s\n", SDL_GetError());
139 // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not be...
141 // Doubled pixel blit (should be faster now!)
142 uint8_t * pMem = (uint8_t *)scr->pixels;
143 uint32_t src = (uint32_t)(hoffsets[hScrollOffset] + voffsets[vScrollOffset]),
144 dst1 = 0, dst2 = scr->pitch;
145 uint32_t srcAdd = 320 - VIRTUAL_SCREEN_WIDTH, dstAdd = (scr->pitch * 2) - (VIRTUAL_SCREEN_WIDTH * 2);
147 for(int i=0; i<VIRTUAL_SCREEN_HEIGHT; i++)
149 for (int j=0; j<VIRTUAL_SCREEN_WIDTH; j++)
151 pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = pMem[dst2 + 1] = my_scr[src++];
161 SDL_UnlockSurface(scr);
162 SDL_UpdateRect(scr, 0, 0, 0, 0);
167 // Draw character on screen
169 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*/)
171 uint8_t scp_lo = (scp + (sx << 1)) & 0x7F;// Let LO byte wrap only...
172 uint16_t sp2 = (scp & 0xFF80) | scp_lo;
173 uint8_t tile = ram[sp2++];
174 uint8_t index = ram[sp2] & 0x03;
175 uint8_t color = ram[sp2];
176 uint32_t chind = baseAddr + (((index << 8) + tile) * 64);
177 uint32_t sc_addr = (sx * 8) + (sy * 2560) + scrollOffset; // Start addr in my_scr[]
179 for(int y=0; y<8; y++)
181 for(int x=0; x<8; x++)
186 my_scr[sc_addr] = ccolor[color][chr[chind]];
189 my_scr[sc_addr] = ccolor[color][chr[chind]];
195 sc_addr += 312; // Do next line of char...
201 // Draw sprites at priority level (new code)
203 void DrawSprites(uint8_t priority)
208 // ------ -----------------------------------------------------------------------------------
209 // 4 h.fb .nnn (f = horz. flip, h = horz. expand, b = sprite offset lo bit, nnn = upper bits of sprite #)
210 // 5 Lower 7 bits of sprite #
211 // 6 Sprite color index (top 7 bits only), bottom bit is bit 8 of X position
212 // 7 Sprite X position (bits 0-7)
213 // 8 Top two bits are sprite priority, bits 4 & 2 are sprite offset hi bit, vert. expand
214 // 9 Sprite Y position (192 - value)
216 extern uint8_t gram1[]; // Game RAM space
218 for(uint16_t i=0x5800; i<0x6000; i+=0x10)
220 if ((gram1[i + 8] & 0xC0) == priority) // Check for correct layer...
222 spr_color_index = gram1[i + 6] >> 1; // Set color...
223 uint16_t x = ((gram1[i + 6] & 0x01) << 8) | gram1[i + 7];
226 x -= 512; // Handle neg x values
228 uint16_t y = 192 - gram1[i + 9];
229 uint8_t flip = gram1[i + 4] & 0x20; // Horizontal flip
230 uint32_t spr_num = ((gram1[i + 4] & 0x07) << 9) | ((gram1[i + 5] & 0x7F) << 2)
231 | ((gram1[i + 4] & 0x10) >> 4) | ((gram1[i + 8] & 0x10) >> 3);
234 Sprite(spr_num, x, y, flip, gram1[i + 4] & 0x80, gram1[i + 8] & 0x04);
243 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip, uint8_t horiz_bl, uint8_t vert_bl)
245 extern uint8_t spr_rom[];
248 x += hScrollOffset; // Adjust x-coord
249 y += vScrollOffset; // Adjust y-coord
251 sprnum <<= 7; // 128 bytes per sprite
258 for(uint16_t sy=0; sy<16; sy++)
260 for(uint16_t sx=0; sx<16; sx+=2)
262 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
263 uint16_t spy = y + sy, spx = x + sx; // Need to optimize this clipping!
265 if (spy > 223 || spx > 299)
268 sc_addr = spx + spy * 320;
271 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
276 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
282 for(uint16_t sy=0; sy<16; sy++)
284 for(uint16_t sx=16; sx<32; sx+=2)
286 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
287 uint16_t spy = y + sy, spx = x + sx;
289 if (spy > 223 || spx > 299)
292 sc_addr = spx + spy * 320;
295 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
300 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
305 sprnum += 128; // Advance to next...
309 y += 16; // Do next row...
311 for(uint16_t sy=0; sy<16; sy++)
313 for(uint16_t sx=0; sx<16; sx+=2)
315 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
316 uint16_t spy = y + sy, spx = x + sx;
318 if (spy > 223 || spx > 299)
321 sc_addr = spx + spy * 320;
324 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
329 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
335 for(uint16_t sy=0; sy<16; sy++)
337 for(uint16_t sx=16; sx<32; sx+=2)
339 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
340 uint16_t spy = y + sy, spx = x + sx;
342 if (spy > 223 || spx > 299)
345 sc_addr = spx + spy * 320;
348 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
353 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
363 for(uint16_t sy=0; sy<16; sy++)
365 for(uint16_t sx=30; sx!=14; sx-=2)
367 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
368 uint16_t spy = y + sy, spx = x + sx;
370 if ((spy > 223) || (spx > 299))
373 sc_addr = spx + spy * 320;
376 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
381 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
386 for(uint16_t sy=0; sy<16; sy++)
388 for(uint16_t sx=14; sx!=0xFFFE; sx-=2)
390 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
391 uint16_t spy = y + sy, spx = x + sx;
393 if ((spy > 223) || (spx > 299))
396 sc_addr = spx+spy*320;
399 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
404 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
408 if (!horiz_bl) sprnum += 128; // If single, skip sprite...
412 y += 16; // Adjust Y coord...
416 for(uint16_t sy=0; sy<16; sy++)
418 for(uint16_t sx=30; sx!=14; sx-=2)
420 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
421 uint16_t spy = y + sy, spx = x + sx;
423 if ((spy > 223) || (spx > 299))
426 sc_addr = spx + spy * 320;
429 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
434 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
438 for(uint16_t sy=0; sy<16; sy++)
440 for(uint16_t sx=14; sx!=0xFFFE; sx-=2)
442 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
443 uint16_t spy = y + sy, spx = x + sx;
445 if ((spy > 223) || (spx > 299))
448 sc_addr = spx + spy * 320;
451 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
456 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
466 static int pcxNum = -1; // This needs to go elsewhere... (or does it?)
472 while (pcxNum < 10000)
474 sprintf(filename, "thnd%04i.pcx", pcxNum);
476 if ((fr = fopen(filename, "r")) == NULL)
477 return pcxNum; // file does not exist - we can create it
486 void SnapPCX(SDL_Surface * scr)
491 int XMax = 319; // Need to adjust this to handle 288 bytes per line
493 int bytesPerLine = 320;
495 // Return if failed...
496 if ((i = FindPCXName()) < 0)
499 sprintf(filename, "thnd%04i.pcx", i);
501 if ((fw = fopen(filename, "wb")) == NULL)
506 fputc(0xA, fw); // pcx signature
507 fputc(0x5, fw); // version 5
508 fputc(0x1, fw); // RLE encoding
509 fputc(0x8, fw); // bits per pixel
513 fputc(0, fw); // XMin=0,YMin=0
514 fputc(XMax & 0xFF, fw);
515 fputc(XMax >> 8, fw);
516 fputc(YMax & 0xFF, fw);
517 fputc(YMax >> 8, fw);
521 fputc(0,fw); // unknown DPI
524 fputc(0, fw); // EGA color palette
526 fputc(0, fw); // reserved
527 fputc(1, fw); // number of bit planes
528 fputc(bytesPerLine & 0xFF, fw);
529 fputc(bytesPerLine >> 8, fw);
531 fputc(0, fw); // palette info - unused
532 fputc((XMax + 1) & 0xFF, fw);
533 fputc((XMax + 1) >> 8, fw);
534 fputc((YMax + 1) & 0xFF, fw);
535 fputc((YMax + 1) >> 8, fw); // screen resolution
538 fputc(0, fw); // unused
540 // Instead of using the screen, we should use our internal buffer...
541 SDL_LockSurface(scr);
543 uint32_t mem = scr->pitch * 8; // Skip first line... WAS:320*8;
545 for (line=0; line<=YMax; line++)
550 uint8_t * pMem = (uint8_t *)scr->pixels;
554 while (xpos < bytesPerLine)
560 while (pMem[mem] == last && xpos < bytesPerLine && count < 63)
562 mem++; count++; xpos++;
565 if (count > 1 || (last & 0xC0) == 0xC0)
567 fputc(0xC0 | (count & 0x3F), fw);
568 fputc(last & 0xFF, fw);
571 fputc(last & 0xFF, fw);
573 mem += (scr->pitch - 320); // Handle non-standard line lengths...
576 SDL_UnlockSurface(scr);
581 for (i=0; i<768; i++)
582 fputc(palette[i], fw);
584 fclose(fw); // success!