]> Shamusworld >> Repos - thunder/blobdiff - src/thunder.cpp
Added save states; updated application icon.
[thunder] / src / thunder.cpp
index ace26f29716faf7b51b9f8a977d4b8abf637e077..cb91ed1a76e2ebb1062a2a5ff06c3777c71797a8 100644 (file)
@@ -2,7 +2,7 @@
 // Thunder: A Rolling Thunder Emulator
 //
 // by James Hammons
-// (C) 2004, 2014 Underground Software
+// (C) 2004, 2023 Underground Software
 //
 // JLH = James Hammons <jlhamm@acm.org>
 //
 // JLH  08/12/2009  Stabilized emulation so that it works
 // JLH  04/04/2014  Converted to SDL 2
 // JLH  04/17/2014  Removed a metric fuck-tonne of cruft, added YM2151 & MCU
+// JLH  01/13/2023  Finally fixed the sprite lag problem  :-D
+// JLH  01/13/2023  Added save states
 //
 
-#define THUNDER_VERSION                "1.1.0"
+#define THUNDER_VERSION                "1.2.2"
 
 #include <SDL2/SDL.h>
-#include <iostream>
-#include <iomanip>
-#include <fstream>
 #include <string>
-#include <new>
-#include <stdio.h>
 #include <stdlib.h>
-#include <stdint.h>
 #include <time.h>
+#include "fileio.h"
 #include "gui.h"
 #include "log.h"
 #include "psg.h"
 #include "video.h"
 #include "ym2151.h"
 
-
-using namespace std;
-
-
 #define ROM1   "rt3-1b.9c"
 #define ROM2   "rt3-2b.12c"
 #define ROM3   "rt3-3.12d"
@@ -69,51 +62,35 @@ using namespace std;
 #define PROM5  "mb7112e.6u"
 #define MCUROM "rt1-mcu.bin"
 
-
 // Global defines
 
-SDL_Surface * screen;
-
-uint8_t * gram, * grom;                        // Allocate RAM & ROM pointers
-// Actual memory
-uint8_t gram1[0x10000], gram2[0x10000], grom1[0x10000], grom2[0x10000];
+uint8_t gram1[0x10000], grom1[0x10000], grom2[0x10000];
 uint8_t grom3[0x8000], data_rom[0x40000], spr_rom[0x80000], voice_rom[0x20000];
-uint8_t chr_rom[0x60000];              // Character ROM pointer
+uint8_t charROM[0x60000];              // Character ROM
 uint8_t mcuMem[0x10000];               // 64K for MCU
 
-V6809REGS cpu1, cpu2;                  // CPU execution contexts
+// CPU execution contexts
+V6809REGS cpu1, cpu2;
 V63701REGS mcu;
 
-bool trace1 = false;                   // ditto...
-bool looking_at_rom = true;            // true = R1, false = R2
-uint32_t banksw1, banksw2;             // Bank switch addresses
-uint16_t game_over_switch;             // Game over delay
-uint16_t dpc;                                  // Debug pc reg...
-bool show_scr = true;                  // Whether or not to show background
-bool enable_cpu = true;                        // Whether or not to enable CPUs
-bool irqGoA = true;                            // IRQ switch for CPU #1
-bool irqGoB = true;                            // IRQ switch for CPU #2
-
-uint16_t refresh_ = 0;                 // Crappy global screen stuff...
-bool refresh2 = true;
-
-uint32_t psg_lens[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-uint8_t * psg_adrs[16];
+// Bank switch addresses
+uint32_t banksw1, banksw2;
 
+// MCU inputs
 Byte input1, input2, input3, input4, input5;
 
-fstream tr;                                                    // Tracelog hook
-uint16_t pcx;                                          // Where we at?
+// Miscellaneous stuff
+bool copySprites = false;
+const uint8_t stateHeader[20] = "THUNDERSAVESTATE1.0";
 
 // Function prototypes
 uint8_t MCUReadMemory(uint16_t address);
 void MCUWriteMemory(uint16_t address, uint8_t data);
 
-
 //
 // Read a byte from memory
 //
-uint8_t RdMem(uint16_t addr)
+uint8_t MainReadMemory(uint16_t addr)
 {
        uint8_t b;
 
@@ -122,90 +99,48 @@ uint8_t RdMem(uint16_t addr)
                // Memory shared with MCU (CPU #1 only! CPU #2 does not)
                if ((addr >= 0x4000) && (addr <= 0x43FF))
                        return mcuMem[addr - 0x3000];
-//             if ((addr >= 0x4000) && (addr <= 0x41FF))
-//                     return MCUReadMemory(addr - 0x3000);
-//             if ((addr >= 0x4200) && (addr <= 0x43FF))
-//                     return mcuMem[addr - 0x3000];
 
                if (addr >= 0x6000)
                        return data_rom[banksw1 + (addr - 0x6000)];     // Get char data
-               else
-                       return gram1[addr];
+
+               return gram1[addr];
        }
 
        return grom1[addr];
 }
 
-
 //
 // Write a byte to memory
 //
-void WrMem(uint16_t address, uint8_t data)
+void MainWriteMemory(uint16_t address, uint8_t data)
 {
-       extern bool disasm;
-       extern bool charbase;   // Needed for screen. Extern it in it??
-#if 0
-       if (address == 0x4182)
-       {
-               WriteLog("\nWriteMem: CPU #1 writing $%02X to $4182!\n\n", b);
-       }
-#endif
-#if 0
-if (((address >= 0x4180) && (address <= 0x4191)) || (address == 0x4380))
-       printf("WriteMem: CPU #1 writing $%02X to $%04X...\n", b, address);
-#endif
-
        if (address == 0x6000)
-{
-//printf("Spawning VOICE channel #1: $6000=$%02X, $6200=$%02X\n", b, gram1[0x6200]);
                SpawnSound(GAMESOUND, gram1[0x6200], 0);        // Do voice chan 1
-}
        if (address == 0x6400)
-{
-//printf("Spawning VOICE channel #2: $6400=$%02X, $6600=$%02X\n", b, gram1[0x6600]);
                SpawnSound(GAMESOUND, gram1[0x6600], 1);        // Do voice chan 2
-}
        if (address == 0x6800)
                banksw1 = (uint32_t)data << 13;                         // Set char data bankswitch base address
-       if (address > 0x4284 && address < 0x42A5 && data)
-               SpawnSound(PSGSOUND, address - 0x4285);         // Do PSG sound on chans 2, 3
 
-//if ((address >= 0x4284) && (address < 0x42A5))
-//{
-//     printf("WrMem: Wrote $%02X to PSG address $%04X...\n", data, address);
-//}
        // Memory shared with MCU (CPU #1 only! CPU #2 does not)
        if ((address >= 0x4000) && (address <= 0x43FF))
                mcuMem[address - 0x3000] = data;
-//     if ((address >= 0x4000) && (address <= 0x41FF))
-//             MCUWriteMemory(address - 0x3000, data);
-//     if ((address >= 0x4200) && (address <= 0x43FF))
-//             mcuMem[address - 0x3000] = data;
        else
                gram1[address] = data;
 
+       if (address == 0x5FF2)
+               copySprites = true;
        if (address == 0x8800)
-               charbase = false;                                                       // Char banksw1
+               charBankSwitch = false;         // Char banksw1
        if (address == 0x8C00)
-               charbase = true;                                                        // Char banksw2
-       if (address == 0x8400)                                                          // Frame go strobe? VBlank acknowledge?
-       {
-               BlitChar(screen, chr_rom, gram1);
-
-               // IRQ Ack (may also be frame go...
-               ClearLineOfCurrentV6809(V6809_ASSERT_LINE_IRQ);
-#if 1
-       if (disasm)
-               WriteLog("WriteMem: CPU #1 Acknowledging IRQ...\n", data);
-#endif
-       }
+               charBankSwitch = true;          // Char banksw2
+       if (address == 0x8400)                  // Frame go strobe? VBlank acknowledge?
+               ClearLineOfCurrentV6809(V6809_LINE_IRQ); // IRQ Ack
 }
 
-
 //
 // Read a byte from memory (2nd processor)
 //
-uint8_t RdMemB(uint16_t address)
+uint8_t SubReadMemory(uint16_t address)
 {
        if (address < 0x8000)
        {
@@ -220,15 +155,11 @@ uint8_t RdMemB(uint16_t address)
        return grom2[address];
 }
 
-
 //
 // Write a byte to memory (2nd processor)
 //
-void WrMemB(uint16_t address, uint8_t data)
+void SubWriteMemory(uint16_t address, uint8_t data)
 {
-       extern bool disasm;
-       extern bool charbase;
-
        // Set sprite data bank switch
        if (address == 0xD803)
                banksw2 = (uint32_t)(data & 0x03) << 13;
@@ -239,24 +170,18 @@ void WrMemB(uint16_t address, uint8_t data)
        if ((address >= 0x2000) && (address < 0x6000))
                gram1[address - 0x2000] = data;
 
+       if (address == 0x1FF2)
+               copySprites = true;
+
        if (address == 0x8800)
-       {
-               // IRQ Ack (may also be frame go...)
-               ClearLineOfCurrentV6809(V6809_ASSERT_LINE_IRQ);
-#if 1
-       if (disasm)
-               WriteLog("WriteMem: CPU #2 Acknowledging IRQ...\n", data);
-#endif
-       }
+               ClearLineOfCurrentV6809(V6809_LINE_IRQ); // IRQ Ack
 }
 
-
 uint8_t MCUReadMemory(uint16_t address)
 {
        if (address < 0x20)
                return InternalRegisterRead(address);
        else if ((address >= 0x1000) && (address <= 0x113F))
-//     else if ((address >= 0x1000) && (address <= 0x11FF))
                return ReadPSG(address - 0x1000);
        else if ((address >= 0x2000) && (address <= 0x2001))
                return YMReadReg(0);
@@ -274,7 +199,6 @@ uint8_t MCUReadMemory(uint16_t address)
        return mcuMem[address];
 }
 
-
 void MCUWriteMemory(uint16_t address, uint8_t data)
 {
        static uint8_t ymRegister;
@@ -288,7 +212,6 @@ void MCUWriteMemory(uint16_t address, uint8_t data)
                || (address >= 0xF000))
                return;
        else if ((address >= 0x1000) && (address <= 0x113F))
-//     else if ((address >= 0x1000) && (address <= 0x11FF))
        {
                WritePSG(address - 0x1000, data);
                return;
@@ -308,32 +231,27 @@ void MCUWriteMemory(uint16_t address, uint8_t data)
        mcuMem[address] = data;
 }
 
-
 uint8_t V63701ReadPort1(void)
 {
 //     printf("V63701ReadPort1: Read $%02X...\n", input3.byte);
        return input3.byte;
 }
 
-
 uint8_t V63701ReadPort2(void)
 {
        return 0xFF;
 }
 
-
 void V63701WritePort1(uint8_t data)
 {
 //     printf("V63701WritePort1: Wrote $%02X...\n", data);
 }
 
-
 void V63701WritePort2(uint8_t data)
 {
 //     printf("V63701WritePort2: Wrote $%02X...\n", data);
 }
 
-
 //
 // Generic Load file into image space
 // (No error checking performed!  Responsibility of caller!)
@@ -352,151 +270,239 @@ bool LoadImg(const char * filename, uint8_t * mem, uint32_t address, uint32_t le
                return false;
        }
 
-       fread(&mem[address], 1, length, file);
+       size_t ignored = fread(&mem[address], 1, length, file);
        fclose(file);
 
        return true;
 }
 
-
 //
 // Read color PROMs
 //
 bool ReadColorPROMs(void)
 {
-       fstream ff1, ff2;
-       //  uint8_t ch;
-       char ch;
-       extern uint32_t palette[256];     // Screen physical palette
-       extern uint8_t ccolor[256][8];   // Character color PROM values
-       extern uint8_t scolor[128][16];  // Sprite color PROM values
+       FILE * file1 = fopen("./ROMs/" PROM3, "rb");
 
-       ff1.open("./ROMs/"PROM3, ios::binary | ios::in);
-
-       if (ff1)
+       if (file1)
        {
                for(int i=0; i<256; i++) // Load char pallete with PROM values
-               {
                        for(int j=0; j<8; j++)
-                       {
-                               ff1.get(ch);
-                               ccolor[i][j] = (uint8_t)ch;
-                       }
-               }
+                               ccolor[i][j] = (uint8_t)fgetc(file1);
 
-               ff1.close();
+               fclose(file1);
        }
 
-       ff1.open("./ROMs/"PROM4, ios::binary | ios::in);
+       file1 = fopen("./ROMs/" PROM4, "rb");
 
-       if (ff1)
+       if (file1)
        {
                for(int i=0; i<128; i++) // Load sprite pallete with PROM values
-               {
                        for(int j=0; j<16; j++)
-                       {
-                               ff1.get(ch);
-                               scolor[i][j] = (uint8_t)ch;
-                       }
-               }
+                               scolor[i][j] = (uint8_t)fgetc(file1);
 
-               ff1.close();
+               fclose(file1);
        }
 
-       ff1.open("./ROMs/"PROM1, ios::binary | ios::in);
-       ff2.open("./ROMs/"PROM2, ios::binary | ios::in);
+       file1 = fopen("./ROMs/" PROM1, "rb");
+       FILE * file2 = fopen("./ROMs/" PROM2, "rb");
 
        // If open was successful...
-       if (ff1 && ff2)
+       if (file1 && file2)
        {
                // Palette is 12-bit RGB, we stretch it to 24-bit
                for(int i=0; i<256; i++)
                {
-                       char c1, c2;
-                       ff1.get(c1);
-                       ff2.get(c2);
+                       uint8_t c1 = fgetc(file1);
+                       uint8_t c2 = fgetc(file2);
                        uint8_t r = (uint8_t)c1 & 0x0F;
                        uint8_t g = (uint8_t)c1 >> 4;
                        uint8_t b = (uint8_t)c2;
                        palette[i] = 0xFF000000 | (b << 20) | (b << 16) | (g << 12) | (g << 8) | (r << 4) | r;
                }
 
-               ff1.close();
-               ff2.close();
+               fclose(file1);
+               fclose(file2);
        }
 
        // PROM5 has the following in it (tile address decoder):
        // 00:  00 20 40 60  02 22 42 62  04 24 44 64  06 26 46 66
-       // 10:  88 A8 C8 E8  8A AA CA EA  8C AC CC EC  8E AE CE EE 
+       // 10:  88 A8 C8 E8  8A AA CA EA  8C AC CC EC  8E AE CE EE
 
-       if (!ff1)
+       if (!file1)
+       {
                printf("Could not open PROM files!\n");
+               return false;
+       }
 
-       return ff1;
+       return true;
 }
 
-
 //
 // Unpack font data
 //
 bool UnpackFonts(void)
 {
-//  uint8_t b1, b2, b3;
-       char b1, b2, b3;
-       fstream f1, f2;
-       //0x4000 $800 chars
-       f1.open("./ROMs/"ROM7, ios::binary | ios::in);
-       f2.open("./ROMs/"ROM8, ios::binary | ios::in);
+       // 0x4000 $800 chars
+       FILE * file1 = fopen("./ROMs/" ROM7, "rb");
+       FILE * file2 = fopen("./ROMs/" ROM8, "rb");
 
-       if ((!f1) || (!f2))
-               return false;  // Return if not found...
+       if (!file1 || !file2)
+       {
+               printf("Could not open either " ROM7 " or " ROM8 "!\n");
+               return false;
+       }
 
        for(long i=0; i<0x40000; i+=64)
        {
                for(int j=0; j<64; j+=8)
                {
-                       f1.get(b1);  f1.get(b2);  f2.get(b3);
-                       b3 ^= 0xFF; // Invert top data...
-                       chr_rom[i+j] = ((b3 & 0x80) >> 5) | ((b1 & 0x80) >> 6) | ((b1 & 0x08) >> 3);
-                       chr_rom[i+j+1] = ((b3 & 0x40) >> 4) | ((b1 & 0x40) >> 5) | ((b1 & 0x04) >> 2);
-                       chr_rom[i+j+2] = ((b3 & 0x20) >> 3) | ((b1 & 0x20) >> 4) | ((b1 & 0x02) >> 1);
-                       chr_rom[i+j+3] = ((b3 & 0x10) >> 2) | ((b1 & 0x10) >> 3) | (b1 & 0x01);
-                       chr_rom[i+j+4] = ((b3 & 0x08) >> 1) | ((b2 & 0x80) >> 6) | ((b2 & 0x08) >> 3);
-                       chr_rom[i+j+5] = (b3 & 0x04)        | ((b2 & 0x40) >> 5) | ((b2 & 0x04) >> 2);
-                       chr_rom[i+j+6] = ((b3 & 0x02) << 1) | ((b2 & 0x20) >> 4) | ((b2 & 0x02) >> 1);
-                       chr_rom[i+j+7] = ((b3 & 0x01) << 2) | ((b2 & 0x10) >> 3) | (b2 & 0x01);
+                       uint8_t b1 = (uint8_t)fgetc(file1);
+                       uint8_t b2 = (uint8_t)fgetc(file1);
+                       uint8_t b3 = (uint8_t)fgetc(file2) ^ 0xFF;
+                       charROM[i + j + 0] = ((b3 & 0x80) >> 5) | ((b1 & 0x80) >> 6) | ((b1 & 0x08) >> 3);
+                       charROM[i + j + 1] = ((b3 & 0x40) >> 4) | ((b1 & 0x40) >> 5) | ((b1 & 0x04) >> 2);
+                       charROM[i + j + 2] = ((b3 & 0x20) >> 3) | ((b1 & 0x20) >> 4) | ((b1 & 0x02) >> 1);
+                       charROM[i + j + 3] = ((b3 & 0x10) >> 2) | ((b1 & 0x10) >> 3) | (b1 & 0x01);
+                       charROM[i + j + 4] = ((b3 & 0x08) >> 1) | ((b2 & 0x80) >> 6) | ((b2 & 0x08) >> 3);
+                       charROM[i + j + 5] = (b3 & 0x04)        | ((b2 & 0x40) >> 5) | ((b2 & 0x04) >> 2);
+                       charROM[i + j + 6] = ((b3 & 0x02) << 1) | ((b2 & 0x20) >> 4) | ((b2 & 0x02) >> 1);
+                       charROM[i + j + 7] = ((b3 & 0x01) << 2) | ((b2 & 0x10) >> 3) | (b2 & 0x01);
                }
        }
 
-       f1.close();
-       f2.close();
+       fclose(file1);
+       fclose(file2);
 
-       f1.open("./ROMs/"ROM5, ios::binary | ios::in);
-       f2.open("./ROMs/"ROM6, ios::binary | ios::in);
+       file1 = fopen("./ROMs/" ROM5, "rb");
+       file2 = fopen("./ROMs/" ROM6, "rb");
+
+       if (!file1 || !file2)
+       {
+               printf("Could not open either " ROM5 " or " ROM6 "!\n");
+               return false;
+       }
 
        for(long i=0x40000; i<0x60000; i+=64)
        {
                for(int j=0; j<64; j+=8)
                {
-                       f1.get(b1);  f1.get(b2);  f2.get(b3);
-                       b3 ^= 0xFF;                             // Invert top data
-                       chr_rom[i+j] = ((b3 & 0x80) >> 5) | ((b1 & 0x80) >> 6) | ((b1 & 0x08) >> 3);
-                       chr_rom[i+j+1] = ((b3 & 0x40) >> 4) | ((b1 & 0x40) >> 5) | ((b1 & 0x04) >> 2);
-                       chr_rom[i+j+2] = ((b3 & 0x20) >> 3) | ((b1 & 0x20) >> 4) | ((b1 & 0x02) >> 1);
-                       chr_rom[i+j+3] = ((b3 & 0x10) >> 2) | ((b1 & 0x10) >> 3) | (b1 & 0x01);
-                       chr_rom[i+j+4] = ((b3 & 0x08) >> 1) | ((b2 & 0x80) >> 6) | ((b2 & 0x08) >> 3);
-                       chr_rom[i+j+5] = (b3 & 0x04)        | ((b2 & 0x40) >> 5) | ((b2 & 0x04) >> 2);
-                       chr_rom[i+j+6] = ((b3 & 0x02) << 1) | ((b2 & 0x20) >> 4) | ((b2 & 0x02) >> 1);
-                       chr_rom[i+j+7] = ((b3 & 0x01) << 2) | ((b2 & 0x10) >> 3) | (b2 & 0x01);
+                       uint8_t b1 = (uint8_t)fgetc(file1);
+                       uint8_t b2 = (uint8_t)fgetc(file1);
+                       uint8_t b3 = (uint8_t)fgetc(file2) ^ 0xFF;
+                       charROM[i + j + 0] = ((b3 & 0x80) >> 5) | ((b1 & 0x80) >> 6) | ((b1 & 0x08) >> 3);
+                       charROM[i + j + 1] = ((b3 & 0x40) >> 4) | ((b1 & 0x40) >> 5) | ((b1 & 0x04) >> 2);
+                       charROM[i + j + 2] = ((b3 & 0x20) >> 3) | ((b1 & 0x20) >> 4) | ((b1 & 0x02) >> 1);
+                       charROM[i + j + 3] = ((b3 & 0x10) >> 2) | ((b1 & 0x10) >> 3) | (b1 & 0x01);
+                       charROM[i + j + 4] = ((b3 & 0x08) >> 1) | ((b2 & 0x80) >> 6) | ((b2 & 0x08) >> 3);
+                       charROM[i + j + 5] = (b3 & 0x04)        | ((b2 & 0x40) >> 5) | ((b2 & 0x04) >> 2);
+                       charROM[i + j + 6] = ((b3 & 0x02) << 1) | ((b2 & 0x20) >> 4) | ((b2 & 0x02) >> 1);
+                       charROM[i + j + 7] = ((b3 & 0x01) << 2) | ((b2 & 0x10) >> 3) | (b2 & 0x01);
                }
        }
 
-       f1.close();
-       f2.close();
+       fclose(file1);
+       fclose(file2);
 
        return true;
 }
 
+void SaveThunderState(const char * filename)
+{
+       WriteLog("Main: Saving Thunder state...\n");
+       FILE * file = fopen(filename, "wb");
+
+       if (!file)
+       {
+               WriteLog("Could not open file \"%s\" for writing!\n", filename);
+               return;
+       }
+
+       // Write out header
+       size_t ignored = fwrite(stateHeader, 1, 19, file);
+
+       // Write out CPUs' state
+       ignored = fwrite(&cpu1, 1, sizeof(cpu1), file);
+       ignored = fwrite(&cpu2, 1, sizeof(cpu2), file);
+       ignored = fwrite(&mcu, 1, sizeof(mcu), file);
+
+       // Write out main memory
+       ignored = fwrite(gram1, 1, 0x10000, file);
+       ignored = fwrite(mcuMem, 1, 0x10000, file);
+
+       // Write out state variables
+       WriteLong(file, banksw1);
+       WriteLong(file, banksw2);
+       fputc((uint8_t)copySprites, file);
+       fputc(input1.byte, file);
+       fputc(input2.byte, file);
+       fputc(input3.byte, file);
+       fputc(input4.byte, file);
+       fputc(input5.byte, file);
+
+       PSGSaveState(file);
+       YMSaveState(file);
+
+       fclose(file);
+}
+
+bool LoadThunderState(const char * filename)
+{
+       WriteLog("Main: Loading Thunder state...\n");
+       FILE * file = fopen(filename, "rb");
+
+       if (!file)
+       {
+               WriteLog("Could not open file \"%s\" for reading!\n", filename);
+               return false;
+       }
+
+       uint8_t buffer[19];
+       size_t ignored = fread(buffer, 1, 19, file);
+
+       // Sanity check...
+       if (memcmp(buffer, stateHeader, 19) != 0)
+       {
+               fclose(file);
+               WriteLog("File \"%s\" is not a valid Thunder save state file!\n", filename);
+               return false;
+       }
+
+       // Read CPUs' state
+       ignored = fread(&cpu1, 1, sizeof(cpu1), file);
+       ignored = fread(&cpu2, 1, sizeof(cpu2), file);
+       ignored = fread(&mcu, 1, sizeof(mcu), file);
+
+       // Read main memory
+       ignored = fread(gram1, 1, 0x10000, file);
+       ignored = fread(mcuMem, 1, 0x10000, file);
+
+       // Read in state variables
+       banksw1 = ReadLong(file);
+       banksw2 = ReadLong(file);
+       copySprites = (bool)fgetc(file);
+       input1.byte = fgetc(file);
+       input2.byte = fgetc(file);
+       input3.byte = fgetc(file);
+       input4.byte = fgetc(file);
+       input5.byte = fgetc(file);
+
+       PSGLoadState(file);
+       YMLoadState(file);
+
+       fclose(file);
+
+       // Make sure things are in a sane state before execution :-P
+       cpu1.RdMem = MainReadMemory;
+       cpu1.WrMem = MainWriteMemory;
+
+       cpu2.RdMem = SubReadMemory;
+       cpu2.WrMem = SubWriteMemory;
+
+       mcu.RdMem = MCUReadMemory;
+       mcu.WrMem = MCUWriteMemory;
+
+       return true;
+}
 
 //
 // Main loop
@@ -505,31 +511,20 @@ int main(int argc, char * argv[])
 {
        InitLog("thunder.log");
 
-       extern bool disasm;                             // From 'V6809.CPP'
-       extern bool charbase;                   // From 'SCREEN.CPP'
-       charbase = false;
-
        bool running;                                   // CPU running state flag...
        SDL_Event event;                                // SDL "event"
-       extern uint8_t palette[768];    // Screen physical palette
        uint32_t ticks, oldTicks;
+       uint8_t frameTickCount = 0;
 
-       printf("THUNDER v"THUNDER_VERSION" by James Hammons\n");
-       printf("Serial #20149417 / Prerelease\n");
-       printf("© 2003, 2014 Underground Software\n\n");
+       printf("THUNDER v" THUNDER_VERSION " by James Hammons\n");
+       printf("Serial #20230113 / Prerelease\n");
+       printf("© 2003, 2023 Underground Software\n\n");
        printf("This emulator is free software. If you paid for it you were RIPPED OFF\n\n");
 
 //     SDL_WM_SetCaption("Thunder v"THUNDER_VERSION" ", "Thunder");
 
-       // Needed only for debugger
-       gram = gram1;  grom = grom1;
+       printf("Loading ROMs...\n");
 
-       memset(gram, 0, 0x10000);
-       memset(grom, 0, 0x10000);
-       memset(gram2, 0, 0x10000);
-       memset(grom2, 0, 0x10000);
-
-       cout << "Loading ROMs..." << endl;
        if (!ReadColorPROMs())
                return -1;
 
@@ -589,10 +584,7 @@ int main(int argc, char * argv[])
 
        // Load 5, 6, 7, 8th ROMs
        if (!UnpackFonts())
-       {
-               cout << "Could not open font files!" << endl;
                return -1;
-       }
 
        // Load MCU program + data
        if (!LoadImg(MCUROM, mcuMem, 0xF000, 0x1000))
@@ -602,30 +594,48 @@ int main(int argc, char * argv[])
                return -1;
 
        // Set up V6809, V63701 execution contexts
-
        memset(&cpu1, 0, sizeof(V6809REGS));
-       cpu1.RdMem = RdMem;
-       cpu1.WrMem = WrMem;
-       cpu1.cpuFlags |= V6809_ASSERT_LINE_RESET;
+       cpu1.RdMem = MainReadMemory;
+       cpu1.WrMem = MainWriteMemory;
+       cpu1.cpuFlags |= V6809_LINE_RESET;
 
        memset(&cpu2, 0, sizeof(V6809REGS));
-       cpu2.RdMem = RdMemB;
-       cpu2.WrMem = WrMemB;
-       cpu2.cpuFlags |= V6809_ASSERT_LINE_RESET;
+       cpu2.RdMem = SubReadMemory;
+       cpu2.WrMem = SubWriteMemory;
+       cpu2.cpuFlags |= V6809_LINE_RESET;
 
        memset(&mcu, 0, sizeof(V63701REGS));
        mcu.RdMem = MCUReadMemory;
        mcu.WrMem = MCUWriteMemory;
-       mcu.cpuFlags |= V63701_ASSERT_LINE_RESET;
+       mcu.cpuFlags |= V63701_LINE_RESET;
 
-       running = true;                                                         // Set running status...
+       running = true;
 
        // Set all inputs to inactive...
-       input1.byte = input2.byte = input3.byte = input4.byte = input5.byte = 0xFF;
-       banksw1 = 0;                   // Will this work?
+       input1.byte = input2.byte = input3.byte = 0xFF;
+       banksw1 = 0;
        banksw2 = 0;
        InitGUI();                 // Reset # of coins
 
+       // Set up DIP switches...
+       input4.bit.b0 = 1;              // DSW B-0: Continues (1 = 6, 0 = 3)
+       input4.bit.b1 = 1;              // DSW B-2: ???
+       input4.bit.b2 = 1;              // DSW B-4: Difficulty (1 = normal, 0 = easy)
+       input4.bit.b3 = 1;              // DSW B-6: Bonus lives (70K/200K = 1, 100K/300K = 0)
+       input4.bit.b4 = 1;              // DSW A-0: Coin #2 credit
+       input4.bit.b5 = 1;              // DSW A-2: Freeze mode (0 = freeze)
+       input4.bit.b6 = 1;              // DSW A-4: Attract mode sound (1 = on)
+       input4.bit.b7 = 1;              // DSW A-6: Coin #1 credit
+
+       input5.bit.b0 = 1;              // DSW B-1: Cabinet type (1 = A, 0 = B)
+       input5.bit.b1 = 0;              // DSW B-3: Level select (0 = on)
+       input5.bit.b2 = 0;              // DSW B-5: Time (1 = 120, 0 = 150)
+       input5.bit.b3 = 0;              // DSW B-7: Lives (1 = 3, 0 = 5)
+       input5.bit.b4 = 1;              // DSW A-1: Coin #2 credit
+       input5.bit.b5 = 1;              // DSW A-3: Invulnerability (0 = on)
+       input5.bit.b6 = 1;              // DSW A-5: Coin #1 credit
+       input5.bit.b7 = 1;              // DSW A-7: Service mode
+
 WriteLog("About to set up screen...\n");
        InitVideo();
 
@@ -634,22 +644,14 @@ WriteLog("About to set up screen...\n");
 WriteLog("About to set up audio...\n");
        InitSound();
 
-memset(scrBuffer, 0xFF, VIRTUAL_SCREEN_WIDTH*VIRTUAL_SCREEN_HEIGHT*sizeof(uint32_t));
-RenderScreenBuffer();
-
 WriteLog("About to enter main loop...\n");
        while (running)
        {
-               HandleGUIDebounce();                                    // Debounce GUI keys
+//             HandleGUIDebounce();                                    // Debounce GUI keys
 
 // Dipswitches are presented to the main CPUs as 0 or 1 at locations
 // $423D - $425B by the MCU
 
-//testing... (works)
-//gram1[0x423D] = 1;
-               //gram1[0x423D] = self_test;                    // Reset DSW1-1
-//             gram1[0x4268] = 0;                                              // Reset Video test
-
                // SDL key handling...
 
                SDL_Event event;
@@ -672,18 +674,47 @@ WriteLog("About to enter main loop...\n");
                                        input1.bit.b5 = 0;
                                else if (event.key.keysym.sym == SDLK_1)
                                        input1.bit.b6 = 0;
-                               else if (event.key.keysym.sym == SDLK_RIGHT)
+                               else if (event.key.keysym.sym == SDLK_2)
+                                       input2.bit.b6 = 0;
+                               else if (event.key.keysym.sym == SDLK_c)        // Left
                                        input3.bit.b5 = 0;
-                               else if (event.key.keysym.sym == SDLK_LEFT)
+                               else if (event.key.keysym.sym == SDLK_z)        // Right
                                        input3.bit.b4 = 0;
-                               else if (event.key.keysym.sym == SDLK_UP)
+                               else if (event.key.keysym.sym == SDLK_s)        // Up
                                        input2.bit.b2 = 0;
-                               else if (event.key.keysym.sym == SDLK_DOWN)
+                               else if (event.key.keysym.sym == SDLK_x)        // Down
                                        input1.bit.b2 = 0;
-                               else if (event.key.keysym.sym == SDLK_q)        // (Q)  Jump
+                               else if (event.key.keysym.sym == SDLK_k)        // Jump
                                        input1.bit.b1 = 0;
-                               else if (event.key.keysym.sym == SDLK_e)        // (E) Fire
+                               else if (event.key.keysym.sym == SDLK_l)        // Fire
                                        input3.bit.b3 = 0;
+
+                               else if (event.key.keysym.sym == SDLK_F1)       // Service mode
+                                       input5.bit.b7 = !input5.bit.b7;
+
+                               else if (event.key.keysym.sym == SDLK_q)
+                                       input4.bit.b7 = !input4.bit.b7;
+                               else if (event.key.keysym.sym == SDLK_w)
+                                       input5.bit.b6 = !input5.bit.b6;
+                               else if (event.key.keysym.sym == SDLK_e)
+                                       input5.bit.b5 = !input5.bit.b5;
+                               else if (event.key.keysym.sym == SDLK_r)
+                                       input5.bit.b4 = !input5.bit.b4;
+                               else if (event.key.keysym.sym == SDLK_t)
+                                       input5.bit.b3 = !input5.bit.b3;
+                               else if (event.key.keysym.sym == SDLK_y)
+                                       input5.bit.b2 = !input5.bit.b2;
+                               else if (event.key.keysym.sym == SDLK_u)
+                                       input5.bit.b1 = !input5.bit.b1;
+                               else if (event.key.keysym.sym == SDLK_i)
+                                       input5.bit.b0 = !input5.bit.b0;
+
+                               // Quick'n'Dirty save state handling...  :-P
+                               else if (event.key.keysym.sym == SDLK_F8)
+                                       SaveThunderState("thun0001.state");
+                               else if (event.key.keysym.sym == SDLK_F9)
+                                       LoadThunderState("thun0001.state");
+
 #else
                                else if (event.key.keysym.sym == SDLK_1)
                                        input1.bit.b0 = 0;
@@ -738,17 +769,19 @@ WriteLog("About to enter main loop...\n");
                                        input1.bit.b5 = 1;
                                else if (event.key.keysym.sym == SDLK_1)
                                        input1.bit.b6 = 1;
-                               else if (event.key.keysym.sym == SDLK_RIGHT)
+                               else if (event.key.keysym.sym == SDLK_2)
+                                       input2.bit.b6 = 1;
+                               else if (event.key.keysym.sym == SDLK_c)
                                        input3.bit.b5 = 1;
-                               else if (event.key.keysym.sym == SDLK_LEFT)
+                               else if (event.key.keysym.sym == SDLK_z)
                                        input3.bit.b4 = 1;
-                               else if (event.key.keysym.sym == SDLK_UP)
+                               else if (event.key.keysym.sym == SDLK_s)
                                        input2.bit.b2 = 1;
-                               else if (event.key.keysym.sym == SDLK_DOWN)
+                               else if (event.key.keysym.sym == SDLK_x)
                                        input1.bit.b2 = 1;
-                               else if (event.key.keysym.sym == SDLK_q)        // (Q)  Jump
+                               else if (event.key.keysym.sym == SDLK_k)        // (Q)  Jump
                                        input1.bit.b1 = 1;
-                               else if (event.key.keysym.sym == SDLK_e)        // (E) Fire
+                               else if (event.key.keysym.sym == SDLK_l)        // (E) Fire
                                        input3.bit.b3 = 1;
 #else
                                if (event.key.keysym.sym == SDLK_1)
@@ -978,9 +1011,9 @@ WriteLog("About to enter main loop...\n");
 #endif
 
                // We can do this here because we're not executing the cores yet.
-               cpu1.cpuFlags |= V6809_ASSERT_LINE_IRQ;
-               cpu2.cpuFlags |= V6809_ASSERT_LINE_IRQ;
-               mcu.cpuFlags |= V63701_ASSERT_LINE_IRQ;
+               cpu1.cpuFlags |= V6809_LINE_IRQ;
+               cpu2.cpuFlags |= V6809_LINE_IRQ;
+               mcu.cpuFlags |= V63701_LINE_IRQ;
 //                                     while (cpu1.clock < 25000)
 // 1.538 MHz = 25633.333... cycles per frame (1/60 s)
 // 25600 cycles/frame
@@ -989,22 +1022,41 @@ WriteLog("About to enter main loop...\n");
 // 40 works, until it doesn't... :-P
 // 640 * 40
 // 800 * 32
+// 20 seems to work...  :-)
 // Interesting, putting IRQs at 30 Hz makes it run at the correct speed. Still hangs in the demo, though.
-               for(int i=0; i<640; i++)
+//             for(int i=0; i<640; i++)
+               for(int i=0; i<1280; i++)
                {
                        // Gay, but what are ya gonna do?
                        // There's better ways, such as keeping track of when slave writes to master, etc...
-                       Execute6809(&cpu1, 40);
-                       Execute6809(&cpu2, 40);
+//                     Execute6809(&cpu1, 40);
+//                     Execute6809(&cpu2, 40);
+                       Execute6809(&cpu1, 20);
+                       Execute6809(&cpu2, 20);
 
                        // MCU runs at 1,536,000 Hz
                        // 1536000 / 60 / 640 == 40
-                       Execute63701(&mcu, 40);
+//                     Execute63701(&mcu, 40);
+                       Execute63701(&mcu, 20);
                }
 
+               BlitChar(charROM, gram1);
+
+               // Make sure that the sprite RAM lags by one frame...  :-)
+               if (copySprites)
+               {
+                       CopySprites();
+                       copySprites = false;
+               }
+
+               frameTickCount++;
+
+               if (frameTickCount == 3)
+                       frameTickCount = 0;
+
                // Speed throttling happens here...
-               // Actually, it's 16.66... Need to account for that somehow
-               while (SDL_GetTicks() - oldTicks < 16)
+               // Actually, it's 16.66... Need to account for that somehow [DONE]
+               while (SDL_GetTicks() - oldTicks < (16 + (frameTickCount == 2 ? 1 : 0)))
                        SDL_Delay(1);                           // Release our timeslice...
 
                oldTicks = SDL_GetTicks();
@@ -1015,126 +1067,3 @@ WriteLog("About to enter main loop...\n");
 
        return 1;
 }
-
-#if 0
-/*
-Hitachi uC runs at 6.144 MHz
-YM2151 runs at 3.579580 MHz
-
-
-Rolling Thunder Memory map
---------------------------
-Most of the decoding is done by custom chips (CUS47 and CUS41), so the memory
-map is inferred by program behaviour. The customs also handle internally irq
-and watchdog.
-
-The main CPU memory map is the same in all games because CUS47 is used by all
-games. The sub CPU and sound CPU, on the other hand, change because CUS41 is
-replaced by other chips.
-
-All RAM is shared between main and sub CPU, except for sound RAM which is
-shared between main and sound CPU; the portion of object RAM that is overlapped
-by sound RAM is used exclusively by the sub CPU.
-
-MAIN CPU:
-
-Address             Dir Data     Name      Description
-------------------- --- -------- --------- -----------------------
-000x xxxx xxxx xxxx R/W xxxxxxxx SCROLL0   tilemap 0/1 RAM (shared with sub CPU)
-001x xxxx xxxx xxxx R/W xxxxxxxx SCROLL1   tilemap 2/3 RAM (shared with sub CPU)
-0100 00xx xxxx xxxx R/W xxxxxxxx SOUND     sound RAM (through CUS30, shared with MCU)
-0100 0000 xxxx xxxx R/W xxxxxxxx           portion holding the sound wave data
-0100 0001 00xx xxxx R/W xxxxxxxx           portion holding the sound registers
-010x xxxx xxxx xxxx R/W xxxxxxxx OBJECT    work RAM (shared with sub CPU) [1]
-0101 1xxx xxxx xxxx R/W xxxxxxxx           portion holding sprite registers
-011x xxxx xxxx xxxx R   xxxxxxxx ROM 9D    program ROM (banked) [2]
-1xxx xxxx xxxx xxxx R   xxxxxxxx ROM 9C    program ROM
-1000 00-- ---- ----   W --------           watchdog reset (RES generated by CUS47)
-1000 01-- ---- ----   W --------           main CPU irq acknowledge (IRQ generated by CUS47)
-1000 1x-- ---- ----   W -------- BANK      tile gfx bank select (data is in A10) (latch in CUS47)
-1001 00-- ---- -x0x   W xxxxxxxx LATCH0    tilemap 0/1 X scroll + priority
-1001 00-- ---- -x10   W xxxxxxxx LATCH0    tilemap 0/1 Y scroll
-1001 00-- ---- --11   W ------xx BAMNKM    ROM 9D bank select
-1001 01-- ---- -x0x   W xxxxxxxx LATCH1    tilemap 2/3 X scroll + priority
-1001 01-- ---- -x10   W xxxxxxxx LATCH1    tilemap 2/3 Y scroll
-1001 01-- ---- --11   W ------xx BAMNKS    ROM 12D bank select
-1100 00-- ---- ----   W xxxxxxxx BACKCOLOR background color
-
-[1] Note that this is partially overlapped by sound RAM
-[2] In Rolling Thunder and others, replaced by the ROM/voice expansion board
-
-
-SUB CPU:
-
-Address             Dir Data     Name      Description
-------------------- --- -------- --------- -----------------------
-000x xxxx xxxx xxxx R/W xxxxxxxx SUBOBJ    work RAM (shared with main CPU)
-0001 1xxx xxxx xxxx R/W xxxxxxxx           portion holding sprite registers
-001x xxxx xxxx xxxx R/W xxxxxxxx SUBSCR0   tilemap 0/1 RAM (shared with main CPU)
-010x xxxx xxxx xxxx R/W xxxxxxxx SUBSCR1   tilemap 2/3 RAM (shared with main CPU)
-011x xxxx xxxx xxxx R   xxxxxxxx ROM 12D   program ROM (banked) [1]
-1xxx xxxx xxxx xxxx R   xxxxxxxx ROM 12C   program ROM
-1000 0--- ---- ----   W --------           watchdog reset (MRESET generated by CUS41)
-1000 1--- ---- ----   W --------           main CPU irq acknowledge (generated by CUS41)
-1101 0--- ---- -x0x   W xxxxxxxx LATCH0    tilemap 0/1 X scroll + priority
-1101 0--- ---- -x10   W xxxxxxxx LATCH0    tilemap 0/1 Y scroll
-1101 0--- ---- --11   W ------xx BAMNKM    ROM 9D bank select
-1101 1--- ---- -x0x   W xxxxxxxx LATCH1    tilemap 2/3 X scroll + priority
-1101 1--- ---- -x10   W xxxxxxxx LATCH1    tilemap 2/3 Y scroll
-1101 1--- ---- --11   W ------xx BAMNKS    ROM 12D bank select
-
-[1] Only used by Rolling Thunder
-
-
-MCU:
-
-Address             Dir Data     Name      Description
-------------------- --- -------- --------- -----------------------
-0000 0000 xxxx xxxx                        MCU internal registers, timers, ports and RAM
-0001 xxxx xxxx xxxx R/W xxxxxxxx RAM 3F    sound RAM (through CUS30, partially shared with main CPU)
-0001 0000 xxxx xxxx R/W xxxxxxxx           portion holding the sound wave data
-0001 0001 00xx xxxx R/W xxxxxxxx           portion holding the sound registers
-0010 0--- --00 ---x R/W xxxxxxxx YMCS      YM2151
-0010 0--- --01 ----                        n.c.
-0010 0--- --10 ---- R   xxxxxxxx PORTA     switch inputs
-0010 0--- --11 ---- R   xxxxxxxx PORTB     dip switches
-01xx xxxx xxxx xxxx R   xxxxxxxx ROM 6B    program ROM (lower half)
-10xx xxxx xxxx xxxx R   xxxxxxxx ROM 6B    program ROM (upper half)
-1011 0--- ---- ----   W                    unknown (CUS41)
-1011 1--- ---- ----   W                    unknown (CUS41)
-1111 xxxx xxxx xxxx R   xxxxxxxx           MCU internal ROM
-
-
-Notes:
------
-- There are two watchdogs, one per CPU (or maybe three). Handling them
-  separately is necessary to allow entering service mode without manually
-  resetting in rthunder and genpeitd: only one of the CPUs stops writing to
-  the watchdog.
-
-- The sprite hardware buffers spriteram: the program writes the sprite list to
-  offsets 4-9 of every 16-byte block, then at the end writes to offset 0x1ff2
-  of sprite RAM to signal the chip that the list is complete. The chip will
-  copy the list from 4-9 to 10-15 and use it from there. This has not been
-  verified on the real hardware, but it is the most logical way of doing it.
-  Emulating this behaviour and not using an external buffer is important in
-  rthunder: when you insert a coin, the whole sprite RAM is cleared, but 0x1ff2
-  is not written to. If we buffered spriteram to an external buffer, this would
-  cause dangling sprites because the buffer would not be updated.
-
-- spriteram buffering fixes sprite lag, but causes a glitch in rthunder when
-  entering a door. The *closed* door is made of tiles, but the *moving* door is
-  made of sprites. Since sprites are delayed by 1 frame, when you enter a door
-  there is one frame where neither the tile-based closed door nor the
-  sprite-based moving door is shown, so it flickers. This behavior has been
-  confirmed on a real PCB.
-
-TODO:
-----
-- The two unknown writes for the MCU are probably watchdog reset and irq acknowledge,
-  but they don't seem to work as expected. During the first few frames they are
-  written out of order and hooking them up in the usual way causes the MCU to
-  stop receiving interrupts.
-*/
-#endif
-