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()
27 // Private function prototypes
29 void DrawSprites(uint8_t priority);
30 int FindPCXName(void);
31 //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);
32 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);
33 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip, uint8_t horiz_bl, uint8_t vert_bl);
35 // Private global variables
37 uint8_t my_scr[0x14000]; // Screen buffer...
38 //uint8_t palette[768]; // Screen palette
39 uint32_t palette[256]; // Screen palette
40 uint8_t ccolor[256][8]; // Character colors
41 uint8_t scolor[128][16]; // Sprite colors
42 bool charbase; // Character base pointer...
43 uint8_t hScrollOffset; // Horizontal scroll offset
44 uint8_t vScrollOffset; // Vertical scroll offset
45 uint8_t spr_color_index; // Sprite color index
46 uint32_t hoffsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };// Scroll offsets...
47 //uint32_t voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
48 uint32_t voffsets[8] = { 288*0, 288*1, 288*2, 288*3, 288*4, 288*5, 288*6, 288*7 };
50 // $000, $140, $280, $3C0, $500, $640, $780, $8C0
52 extern bool show_text; // Whether or not to show text
53 extern bool show_scr; // Whether or not to show screen
57 // Render the NAMCO screen
59 void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
62 // Screen is 288 x 224 pixels, with character and independent sprites.
63 // Tiles are 36 x 28. There are four tile planes, three of them affected by
64 // the h/vscroll values, the last one is a static display (also has highest
65 // priority). Screens are 128 bytes wide by 32 bytes high.
69 uint32_t sc_base = ((ram[0x9000] << 8) | ram[0x9001]) + 4; // Adjust hscroll val
70 hScrollOffset = sc_base & 0x07; // Horiz. fine scroll offset
71 sc_base = (sc_base & 0xFFF8) >> 2; // Skip odds..
72 uint8_t vsc_base = ((ram[0x9002] + 1) & 0xF8) >> 3;// Vertical scroll addr adjust
73 vScrollOffset = ((ram[0x9002] + 1) & 0x07); // Vertical fine scroll amount
75 uint32_t scp0 = 0x0180 | ((sc_base + 0x04) & 0x7F); /*0x0188;*/
76 uint32_t scp1 = 0x1180 | ((sc_base + 0x04) & 0x7F); /*0x1188;*/
77 uint32_t scp2 = 0x2180 | ((sc_base + 0x04) & 0x7F); /*0x2188;*/
78 uint32_t scp3 = 0x3208;
80 scp0 += vsc_base * 0x80;
81 scp0 &= 0x0FFF; // Set vertical scroll addr
82 scp1 += vsc_base * 0x80;
83 scp1 = 0x1000 | (scp1 & 0x0FFF); // Set vertical scroll addr
84 scp2 += vsc_base * 0x80;
85 scp2 = 0x2000 | (scp2 & 0x0FFF); // Set vertical scroll addr
87 // Layer 0 (bottom layer)
88 for(uint8_t sy=0; sy<29; sy++)
90 for(uint8_t sx=0; sx<37; sx++)
92 DrawChar(chr, ram, sx, sy, scp0, (charbase ? 0x20000 : 0x00000), 0, false);
94 DrawChar(chr, ram, sx, sy, scp0, (charbase ? 0x20000 : 0x00000), hScrollOffset, vScrollOffset, false);
98 scp0 = 0x0000 | (scp0 & 0x0FFF);
101 // Draw sprites at lowest layer...
105 for(uint8_t sy=0; sy<29; sy++)
107 for(uint8_t sx=0; sx<37; sx++)
109 DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x30000 : 0x10000), 0);
111 DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x30000 : 0x10000), hScrollOffset, vScrollOffset);
115 scp1 = 0x1000 | (scp1 & 0x0FFF);
118 // Draw sprites under layer #2...
122 for(uint8_t sy=0; sy<29; sy++)
124 for(uint8_t sx=0; sx<37; sx++)
126 DrawChar(chr, ram, sx, sy, scp2, 0x40000, 0);
128 DrawChar(chr, ram, sx, sy, scp2, 0x40000, hScrollOffset, vScrollOffset);
132 scp2 = 0x2000 | (scp2 & 0x0FFF);
135 // Draw highest priority sprites...
138 // Layer 3 (top layer)
139 for(uint8_t sy=0; sy<28; sy++)
141 for(uint8_t sx=0; sx<36; sx++)
143 DrawChar(chr, ram, sx, sy, scp3, 0x50000, hScrollOffset + voffsets[vScrollOffset]);
145 DrawChar(chr, ram, sx, sy, scp3, 0x50000);
152 // Draw a msg if needed...
156 // Show GUI if active...
161 if (SDL_LockSurface(scr) < 0)
163 // fprintf(stderr, "Couldn't lock the display surface: %s\n", SDL_GetError());
168 // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not be...
170 uint32_t src = 0;//(uint32_t)(hoffsets[hScrollOffset] + voffsets[vScrollOffset]);
171 // uint32_t srcAdd = 320 - VIRTUAL_SCREEN_WIDTH;
173 for(int i=0; i<VIRTUAL_SCREEN_HEIGHT; i++)
175 for (int j=0; j<VIRTUAL_SCREEN_WIDTH; j++)
177 scrBuffer[(i * VIRTUAL_SCREEN_WIDTH) + j] = palette[my_scr[src++]];
183 RenderScreenBuffer();
188 // Draw character on screen
190 //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*/)
191 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*/)
193 uint8_t scp_lo = (scp + (sx << 1)) & 0x7F;// Let LO byte wrap only...
194 uint16_t sp2 = (scp & 0xFF80) | scp_lo;
195 uint8_t tile = ram[sp2++];
196 uint8_t index = ram[sp2] & 0x03;
197 uint8_t color = ram[sp2];
198 uint32_t chind = baseAddr + (((index << 8) + tile) * 64);
199 // uint32_t sc_addr = (sx * 8) + (sy * 2560) + scrollOffset; // Start addr in my_scr[]
200 // uint32_t sc_addr = (sx * 8) + (sy * 288 * 8);// + scrollOffset;
202 int xStart = (int)(sx * 8) - xScroll;
203 int yStart = (int)(sy * 8) - yScroll;
205 // int32_t sc_addr = ((sx * 8) - xScroll) + ((sy * 8 * 288) - (yScroll * 288));
206 int32_t sc_addr = xStart + (yStart * 288);
208 for(int y=0; y<8; y++)
210 for(int x=0; x<8; x++)
212 if (((xStart + x) < 0) || ((xStart + x) >= 288)
213 || ((yStart + y) < 0) || ((yStart + y) >= 224))
223 my_scr[sc_addr] = ccolor[color][chr[chind]];
226 my_scr[sc_addr] = ccolor[color][chr[chind]];
232 sc_addr += (288 - 8); // Do next line of char...
238 // Draw sprites at priority level (new code)
240 void DrawSprites(uint8_t priority)
245 // ------ --------------------------------------------------------------------
246 // 4 h.fb .nnn (f = horz. flip, h = horz. expand, b = sprite offset lo
247 // bit, nnn = upper bits of sprite #)
248 // 5 Lower 7 bits of sprite #
249 // 6 Sprite color index (top 7 bits only), bottom bit is bit 8 of X
251 // 7 Sprite X position (bits 0-7)
252 // 8 Top two bits are sprite priority, bits 4 & 2 are sprite offset hi
254 // 9 Sprite Y position (192 - value)
256 extern uint8_t gram1[]; // Game RAM space
258 for(uint16_t i=0x5800; i<0x6000; i+=0x10)
260 if ((gram1[i + 8] & 0xC0) == priority) // Check for correct layer...
262 spr_color_index = gram1[i + 6] >> 1; // Set color...
263 uint16_t x = ((gram1[i + 6] & 0x01) << 8) | gram1[i + 7];
266 x -= 512; // Handle neg x values
268 uint16_t y = 192 - gram1[i + 9];
269 uint8_t flip = gram1[i + 4] & 0x20; // Horizontal flip
270 uint32_t spr_num = ((gram1[i + 4] & 0x07) << 9)
271 | ((gram1[i + 5] & 0x7F) << 2)
272 | ((gram1[i + 4] & 0x10) >> 4)
273 | ((gram1[i + 8] & 0x10) >> 3);
276 Sprite(spr_num, x, y, flip, gram1[i + 4] & 0x80, gram1[i + 8] & 0x04);
282 static inline void DrawSpriteBlock(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
284 extern uint8_t spr_rom[];
287 for(uint16_t sy=0; sy<16; sy++)
289 // for(uint16_t sx=0; sx<16; sx+=2)
290 for(uint16_t sx=xStart; sx<xEnd; sx+=xInc)
292 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
293 uint16_t spy = y + sy, spx = x + sx; // Need to optimize this clipping! [eh?]
295 // This handles negative values, by casting as unsigned
296 uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
299 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
304 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
310 static inline void DrawSpriteBlock2(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
312 extern uint8_t spr_rom[];
315 for(uint16_t sy=0; sy<16; sy++)
317 // for(uint16_t sx=0; sx<16; sx+=2)
318 for(uint16_t sx=xStart; sx!=xEnd; sx+=xInc)
320 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
321 uint16_t spy = y + sy, spx = x + sx; // Need to optimize this clipping! [eh?]
323 // This handles negative values, by casting as unsigned
324 uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
327 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
332 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
341 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip,
342 uint8_t horiz_bl, uint8_t vert_bl)
344 extern uint8_t spr_rom[];
346 sprnum <<= 7; // 128 bytes per sprite
354 for(uint16_t sy=0; sy<16; sy++)
356 for(uint16_t sx=0; sx<16; sx+=2)
358 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
359 uint16_t spy = y + sy, spx = x + sx; // Need to optimize this clipping!
361 // This handles negative values, by casting as unsigned
363 // if (spy > 223 || spx > 299)
364 if (spy >= 224 || spx >= 288)
367 // sc_addr = spx + spy * 320;
368 sc_addr = spx + (spy * 288);
370 sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
373 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
378 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
382 DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
388 for(uint16_t sy=0; sy<16; sy++)
390 for(uint16_t sx=16; sx<32; sx+=2)
392 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
393 uint16_t spy = y + sy, spx = x + sx;
395 // if (spy > 223 || spx > 299)
396 if (spy > 223 || spx > 287)
399 // sc_addr = spx + spy * 320;
400 sc_addr = spx + (spy * 288);
403 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
408 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
412 DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
416 sprnum += 128; // Advance to next...
420 y += 16; // Do next row...
423 for(uint16_t sy=0; sy<16; sy++)
425 for(uint16_t sx=0; sx<16; sx+=2)
427 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
428 uint16_t spy = y + sy, spx = x + sx;
430 // if (spy > 223 || spx > 299)
431 if (spy > 223 || spx > 287)
434 // sc_addr = spx + spy * 320;
435 sc_addr = spx + (spy * 288);
438 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
443 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
447 DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
453 for(uint16_t sy=0; sy<16; sy++)
455 for(uint16_t sx=16; sx<32; sx+=2)
457 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
458 uint16_t spy = y + sy, spx = x + sx;
460 // if (spy > 223 || spx > 299)
461 if (spy > 223 || spx > 287)
464 // sc_addr = spx + spy * 320;
465 sc_addr = spx + (spy * 288);
468 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
473 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
477 DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
487 for(uint16_t sy=0; sy<16; sy++)
489 for(uint16_t sx=30; sx!=14; sx-=2)
491 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
492 uint16_t spy = y + sy, spx = x + sx;
494 // if ((spy > 223) || (spx > 299))
495 if ((spy > 223) || (spx > 287))
498 // sc_addr = spx + spy * 320;
499 sc_addr = spx + (spy * 288);
502 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
507 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
511 DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
516 for(uint16_t sy=0; sy<16; sy++)
518 for(uint16_t sx=14; sx!=0xFFFE; sx-=2)
520 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
521 uint16_t spy = y + sy, spx = x + sx;
523 // if ((spy > 223) || (spx > 299))
524 if ((spy > 223) || (spx > 287))
527 // sc_addr = spx + spy * 320;
528 sc_addr = spx + (spy * 288);
531 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
536 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
540 DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
544 sprnum += 128; // If single, skip sprite...
548 y += 16; // Adjust Y coord...
553 for(uint16_t sy=0; sy<16; sy++)
555 for(uint16_t sx=30; sx!=14; sx-=2)
557 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
558 uint16_t spy = y + sy, spx = x + sx;
560 // if ((spy > 223) || (spx > 299))
561 if ((spy > 223) || (spx > 287))
564 // sc_addr = spx + spy * 320;
565 sc_addr = spx + (spy * 288);
568 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
573 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
577 DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
582 for(uint16_t sy=0; sy<16; sy++)
584 for(uint16_t sx=14; sx!=0xFFFE; sx-=2)
586 uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
587 uint16_t spy = y + sy, spx = x + sx;
589 // if ((spy > 223) || (spx > 299))
590 if ((spy > 223) || (spx > 287))
593 // sc_addr = spx + spy * 320;
594 sc_addr = spx + (spy * 288);
597 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
602 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
606 DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
613 int FindPCXName(void)
615 static int pcxNum = -1; // This needs to go elsewhere... (or does it?)
621 while (pcxNum < 10000)
623 sprintf(filename, "thnd%04i.pcx", pcxNum);
625 // file does not exist - we can create it
626 if ((fr = fopen(filename, "r")) == NULL)
636 void SnapPCX(SDL_Surface * scr)
641 int XMax = 319; // Need to adjust this to handle 288 bytes per line
643 int bytesPerLine = 320;
645 // Return if failed...
646 if ((i = FindPCXName()) < 0)
649 sprintf(filename, "thnd%04i.pcx", i);
651 if ((fw = fopen(filename, "wb")) == NULL)
656 fputc(0xA, fw); // pcx signature
657 fputc(0x5, fw); // version 5
658 fputc(0x1, fw); // RLE encoding
659 fputc(0x8, fw); // bits per pixel
663 fputc(0, fw); // XMin=0,YMin=0
664 fputc(XMax & 0xFF, fw);
665 fputc(XMax >> 8, fw);
666 fputc(YMax & 0xFF, fw);
667 fputc(YMax >> 8, fw);
671 fputc(0,fw); // unknown DPI
674 fputc(0, fw); // EGA color palette
676 fputc(0, fw); // reserved
677 fputc(1, fw); // number of bit planes
678 fputc(bytesPerLine & 0xFF, fw);
679 fputc(bytesPerLine >> 8, fw);
681 fputc(0, fw); // palette info - unused
682 fputc((XMax + 1) & 0xFF, fw);
683 fputc((XMax + 1) >> 8, fw);
684 fputc((YMax + 1) & 0xFF, fw);
685 fputc((YMax + 1) >> 8, fw); // screen resolution
688 fputc(0, fw); // unused
690 // Instead of using the screen, we should use our internal buffer...
691 SDL_LockSurface(scr);
693 uint32_t mem = scr->pitch * 8; // Skip first line... WAS:320*8;
695 for (line=0; line<=YMax; line++)
700 uint8_t * pMem = (uint8_t *)scr->pixels;
704 while (xpos < bytesPerLine)
710 while (pMem[mem] == last && xpos < bytesPerLine && count < 63)
717 if (count > 1 || (last & 0xC0) == 0xC0)
719 fputc(0xC0 | (count & 0x3F), fw);
720 fputc(last & 0xFF, fw);
723 fputc(last & 0xFF, fw);
726 mem += (scr->pitch - 320); // Handle non-standard line lengths...
729 SDL_UnlockSurface(scr);
734 for (i=0; i<768; i++)
735 fputc(palette[i], fw);
737 fclose(fw); // success!