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 L. 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 priority);
29 int FindPCXName(void);
31 // Private global variables
33 uint8 my_scr[0x14000]; // Screen buffer...
34 uint8 palette[768]; // Screen palette
35 uint8 ccolor[256][8]; // Character colors
36 uint8 scolor[128][16]; // Sprite colors
37 bool charbase; // Character base pointer...
38 uint8 hScrollOffset; // Horizontal scroll offset
39 uint8 vScrollOffset; // Vertical scroll offset
40 uint8 spr_color_index; // Sprite color index
41 uint32 offsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; // Scroll offsets...
42 uint32 voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
44 extern bool show_text; // Whether or not to show text
45 extern bool show_scr; // Whether or not to show screen
48 // Render the NAMCO screen
50 void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
55 uint32 sc_base = ((ram[0x9000] << 8) | ram[0x9001]) + 4; // Adjust hscroll val
56 hScrollOffset = sc_base & 0x07; // Horiz. scroll offset
57 sc_base = (sc_base & 0xFFF8) >> 2; // Skip odds..
58 uint8 vsc_base = ((ram[0x9002] + 1) & 0xF8) >> 3;// Vertical scroll addr adjust
59 vScrollOffset = ((ram[0x9002] + 1) & 0x07); // Vertical fine scroll amount
60 uint32 scp1 = 0x0180 | ((sc_base + 0x04) & 0x7F); /*0x0188;*/
61 uint32 scp2 = 0x1180 | ((sc_base + 0x04) & 0x7F); /*0x1188;*/
62 uint32 scp3 = 0x2180 | ((sc_base + 0x04) & 0x7F); /*0x2188;*/
64 scp1 += vsc_base * 0x80;
65 scp1 &= 0x0FFF; // Set vertical scroll addr
66 scp2 += vsc_base * 0x80;
67 scp2 = 0x1000 | (scp2 & 0x0FFF); // Set vertical scroll addr
68 scp3 += vsc_base * 0x80;
69 scp3 = 0x2000 | (scp3 & 0x0FFF); // Set vertical scroll addr
71 uint32 chBaseOffset = (charbase ? 0x20000 : 0x00000);
73 for(sy=0; sy<29; sy++)
75 for(sx=0; sx<37; sx++)
77 uint8 scp_lo = (scp1 + (sx << 1)) & 0x7F;// Let LO byte wrap only...
78 uint16 sp2 = (scp1 & 0xFF80) | scp_lo;
79 uint8 tile = ram[sp2++];
80 uint8 index = ram[sp2] & 0x03;
81 uint8 color = ram[sp2];
82 uint32 chind = chBaseOffset + (((index << 8) + tile) * 64);
83 uint32 sc_addr = (sx * 8) + (sy * 2560); // Start addr in my_scr[]
85 for(int y=0; y<8; y++)
87 for(int x=0; x<8; x++)
88 my_scr[sc_addr++] = ccolor[color][chr[chind++]];
90 sc_addr += 312; // Do next line...
95 scp1 = 0x0000 | (scp1 & 0x0FFF);
98 DrawSprites(0x40); // Draw sprites at lowest layer...
100 chBaseOffset = (charbase ? 0x30000 : 0x10000);
102 for(sy=0; sy<29; sy++)
104 for(sx=0; sx<37; sx++)
106 uint8 scp_lo = (scp2 + (sx << 1)) & 0x7F; // Let LO byte wrap only...
107 uint16 sp2 = (scp2 & 0xFF80) | scp_lo;
108 uint8 tile = ram[sp2++];
109 uint8 index = ram[sp2] & 0x03;
110 uint8 color = ram[sp2];
111 uint32 chind = chBaseOffset + (((index << 8) + tile) * 64);
112 uint32 sc_addr = (sx * 8) + (sy * 2560); // Start addr in my_scr[]
114 for(int y=0; y<8; y++)
116 for(int x=0; x<8; x++)
119 my_scr[sc_addr] = ccolor[color][chr[chind]];
125 sc_addr += 312; // Do next line...
130 scp2 = 0x1000 | (scp2 & 0x0FFF);
133 DrawSprites(0x80); // Draw sprites under layer #2...
135 for(sy=0; sy<29; sy++)
137 for(sx=0; sx<37; sx++)
139 uint8 scp_lo = (scp3 + (sx << 1)) & 0x7F; // Let LO byte wrap only...
140 uint16 sp2 = (scp3 & 0xFF80) | scp_lo;
141 uint8 tile = ram[sp2++];
142 uint8 index = ram[sp2] & 0x03;
143 uint8 color = ram[sp2];
144 uint32 chind = 0x40000 + (((index << 8) + tile) * 64);
145 uint32 sc_addr = (sx * 8) + (sy * 2560); // Start addr in my_scr[]
147 for(int y=0; y<8; y++)
149 for(int x=0; x<8; x++)
152 my_scr[sc_addr] = ccolor[color][chr[chind]];
158 sc_addr += 312; // Do next line...
162 scp3 += 0x80; scp3 = 0x2000|(scp3&0x0FFF);
165 DrawSprites(0xC0); // Draw highest priority sprites...
167 for(sy=0; sy<28; sy++)
169 for(sx=0; sx<36; sx++)
171 uint16 sp2 = scp + (sx << 1);
172 uint8 tile = ram[sp2++];
173 uint8 index = ram[sp2] & 0x03;
174 uint8 color = ram[sp2];
175 uint32 chind = 0x50000 + (((index << 8) + tile) * 64);
176 uint32 sc_addr = (sx * 8) + (sy * 2560) + hScrollOffset + voffsets[vScrollOffset]; // Start addr in my_scr[]
178 for(int y=0; y<8; y++)
180 for(int x=0; x<8; x++)
183 my_scr[sc_addr] = ccolor[color][chr[chind]];
189 sc_addr += 312; // Do next line of char...
198 DrawText(); // Draw a msg if needed...
201 DrawGUI(); // Show GUI if active...
203 if (SDL_LockSurface(scr) < 0)
205 // fprintf(stderr, "Couldn't lock the display surface: %s\n", SDL_GetError());
209 // Screen size is 288 x 224
212 /* for(int i=0; i<224; i++)
214 memcpy((char *)scr->pixels + scr->pitch * i,
215 my_scr + (uint32)(offsets[hScrollOffset]+voffsets[vScrollOffset]) + i * 320, 320);
218 // Doubled pixel blit (should be faster now!)
219 uint8 * pMem = (uint8 *)scr->pixels + ((scr->pitch * 8 + 16) * 2);
220 uint32 src = (uint32)(offsets[hScrollOffset] + voffsets[vScrollOffset]),
221 dst1 = 0, dst2 = scr->pitch;
222 uint32 srcAdd = 320 - 288, dstAdd = (scr->pitch * 2) - (288 * 2);
224 for(int i=0; i<224; i++)
226 for (int j=0; j<288; j++)
228 pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = pMem[dst2 + 1] = my_scr[src++];
238 // Scanlined pixel blit
239 /* uint8 * pMem = (uint8 *)scr->pixels + ((scr->pitch * 8 + 16) * 2);
240 uint32 src = (uint32)(offsets[hScrollOffset] + voffsets[vScrollOffset]),
241 dst1 = 0, dst2 = scr->pitch;
242 uint32 srcAdd = 320 - 288, dstAdd = (scr->pitch * 2) - (288 * 2);
244 for(int i=0; i<224; i++)
246 for (int j=0; j<288; j++)
248 pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = my_scr[src++];
258 SDL_UnlockSurface(scr);
259 SDL_UpdateRect(scr, 0, 0, 0, 0);
263 // Draw sprites at priority level (new code)
265 void DrawSprites(uint8 priority)
267 extern uint8 gram1[]; // Game RAM space
269 for(uint16 i=0x5800; i<0x6000; i+=0x10)
271 if ((gram1[i + 8] & 0xC0) == priority) // Check for correct layer...
273 spr_color_index = gram1[i + 6] >> 1; // Set color...
274 uint16 x = ((gram1[i + 6] & 0x01) << 8) | gram1[i + 7];
277 x -= 512; // Handle neg x values
279 uint16 y = 192 - gram1[i + 9];
280 uint16 hdr = (gram1[i + 4] & 0x90) << 8 | (gram1[i + 8] & 0x14);
281 uint8 flip = gram1[i + 4] & 0x20; // Horizontal flip
282 uint32 spr_num = ((gram1[i + 4] & 0x07) << 9) | ((gram1[i + 5] & 0x7F) << 2);
284 Sprite(spr_num, x, y, flip, hdr); // Draw sprite...
292 void Sprite(uint32 sprnum, uint16 x, uint16 y, uint8 flip, uint16 spr_id)
294 extern uint8 spr_rom[];
295 // To show or not to show a 16x16 block in the 4x4 grid...
296 bool horiz_bl = false, vert_bl = false;
299 x += hScrollOffset; // Adjust x-coord
300 y += vScrollOffset; // Adjust y-coord
302 if (spr_id == 0x8004)
304 horiz_bl = true; vert_bl = true;
306 if (spr_id == 0x8000)
308 horiz_bl = true; y += 16;
310 if (spr_id == 0x8010)
312 horiz_bl = true; y += 16; sprnum += 2;
314 if (spr_id == 0x0004)
318 if (spr_id == 0x1004)
320 vert_bl = true; sprnum++;
322 if (spr_id == 0x0000)
326 if (spr_id == 0x1000)
330 if (spr_id == 0x0010)
332 y += 16; sprnum += 2;
334 if (spr_id == 0x1010)
336 y += 16; sprnum += 3;
339 sprnum <<= 7; // 128 bytes per sprite
343 for(uint16 sy=0; sy<16; sy++)
345 for(uint16 sx=0; sx<16; sx+=2)
347 uint8 b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
348 uint16 spy = y + sy, spx = x + sx; // Need to optimize this clipping!
350 if (spy > 223 || spx > 299)
353 sc_addr = spx + spy * 320;
356 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
361 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
367 for(uint16 sy=0; sy<16; sy++)
369 for(uint16 sx=16; sx<32; sx+=2)
371 uint8 b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
372 uint16 spy = y + sy, spx = x + sx;
374 if (spy > 223 || spx > 299)
377 sc_addr = spx + spy * 320;
380 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
385 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
390 sprnum += 128; // Advance to next...
394 y += 16; // Do next row...
396 for(uint16 sy=0; sy<16; sy++)
398 for(uint16 sx=0; sx<16; sx+=2)
400 uint8 b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
401 uint16 spy = y + sy, spx = x + sx;
403 if (spy > 223 || spx > 299)
406 sc_addr = spx + spy * 320;
409 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
414 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
420 for(uint16 sy=0; sy<16; sy++)
422 for(uint16 sx=16; sx<32; sx+=2)
424 uint8 b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
425 uint16 spy = y + sy, spx = x + sx;
427 if (spy > 223 || spx > 299)
430 sc_addr = spx + spy * 320;
433 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
438 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
448 for(uint16 sy=0; sy<16; sy++)
450 for(uint16 sx=30; sx!=14; sx-=2)
452 uint8 b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
453 uint16 spy = y+sy, spx = x+sx;
454 if ((spy>223) || (spx>299))
457 sc_addr = spx+spy*320;
459 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
462 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
466 for(uint16 sy=0; sy<16; sy++)
468 for(uint16 sx=14; sx!=0xFFFE; sx-=2)
470 uint8 b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
471 uint16 spy = y+sy, spx = x+sx;
472 if ((spy>223) || (spx>299))
475 sc_addr = spx+spy*320;
477 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
480 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
483 if (!horiz_bl) sprnum += 128; // If single, skip sprite...
486 y += 16; // Adjust Y coord...
489 for(uint16 sy=0; sy<16; sy++)
491 for(uint16 sx=30; sx!=14; sx-=2)
493 uint8 b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
494 uint16 spy = y+sy, spx = x+sx;
495 if ((spy>223) || (spx>299))
498 sc_addr = spx+spy*320;
500 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
503 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
507 for(uint16 sy=0; sy<16; sy++)
509 for(uint16 sx=14; sx!=0xFFFE; sx-=2)
511 uint8 b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
512 uint16 spy = y+sy, spx = x+sx;
513 if ((spy>223) || (spx>299))
516 sc_addr = spx+spy*320;
518 my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
521 my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
530 static int pcxNum = -1; // This needs to go elsewhere... (or does it?)
536 while (pcxNum < 10000)
538 sprintf(filename, "thnd%04i.pcx", pcxNum);
540 if ((fr = fopen(filename, "r")) == NULL)
541 return pcxNum; // file does not exist - we can create it
549 void SnapPCX(SDL_Surface * scr)
554 int XMax = 319; // Need to adjust this to handle 288 bytes per line
556 int bytesPerLine = 320;
558 if ((i = FindPCXName()) < 0)
561 sprintf(filename, "thnd%04i.pcx", i);
563 if ((fw = fopen(filename, "wb")) == NULL)
568 fputc(0xA, fw); // pcx signature
569 fputc(0x5, fw); // version 5
570 fputc(0x1, fw); // RLE encoding
571 fputc(0x8, fw); // bits per pixel
572 fputc(0, fw); fputc(0, fw); fputc(0, fw); fputc(0, fw); // XMin=0,YMin=0
573 fputc(XMax&0xFF, fw); fputc(XMax>>8, fw);
574 fputc(YMax&0xFF, fw); fputc(YMax>>8, fw);
575 fputc(0, fw); fputc(0, fw); fputc(0, fw); fputc(0,fw); // unknown DPI
576 for (i=0; i<48; i++) fputc(0, fw); // EGA color palette
577 fputc(0, fw); // reserved
578 fputc(1, fw); // number of bit planes
579 fputc(bytesPerLine&0xFF, fw); fputc(bytesPerLine>>8, fw);
580 fputc(1, fw); fputc(0, fw); // palette info - unused
581 fputc((XMax+1)&0xFF, fw); fputc((XMax+1)>>8, fw);
582 fputc((YMax+1)&0xFF, fw); fputc((YMax+1)>>8, fw); // screen resolution
583 for (i=0; i<54; i++) fputc(0, fw); // unused
585 // Instead of using the screen, we should use our internal buffer...
586 SDL_LockSurface(scr);
588 uint32 mem = scr->pitch * 8; // Skip first line... WAS:320*8;
589 for (line=0; line<=YMax; line++)
594 uint8 * pMem = (uint8 *)scr->pixels;
597 while (xpos < bytesPerLine)
602 while (pMem[mem] == last && xpos < bytesPerLine && count < 63)
604 mem++; count++; xpos++;
607 if (count > 1 || (last&0xC0) == 0xC0)
609 fputc(0xC0 | (count & 0x3F), fw); fputc(last & 0xFF, fw);
611 else fputc(last & 0xFF, fw);
613 mem += (scr->pitch - 320); // Handle non-standard line lengths...
616 SDL_UnlockSurface(scr);
621 for (i=0; i<768; i++)
622 fputc(palette[i], fw);
624 fclose(fw); // success!