+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;
+}
+