diff --git a/Source/Project64-core/N64System/N64Class.cpp b/Source/Project64-core/N64System/N64Class.cpp index 5969b123b..6521299d5 100644 --- a/Source/Project64-core/N64System/N64Class.cpp +++ b/Source/Project64-core/N64System/N64Class.cpp @@ -385,6 +385,13 @@ bool CN64System::EmulationStarting(CThread * thread) WriteTrace(TraceN64System, TraceDebug, "Game starting"); g_BaseSystem->StartEmulation2(false); WriteTrace(TraceN64System, TraceDebug, "Game Done"); + //PLACE TO ADD 64DD SAVING CODE + if (g_Disk != NULL) + { + g_Disk->SaveDiskImage(); + //g_Notify->DisplayError(g_Disk->GetError()); + WriteTrace(TraceN64System, TraceDebug, "64DD Save Done"); + } } catch (...) { @@ -411,11 +418,11 @@ void CN64System::StartEmulation2(bool NewThread) StartLog(); } - WriteTrace(TraceN64System, TraceDebug, "Setting up system"); + WriteTrace(TraceN64System, TraceDebug, "Setting up system"); CInterpreterCPU::BuildCPU(); uint32_t CpuType = g_Settings->LoadDword(Game_CpuType); - WriteTrace(TraceN64System, TraceDebug, "CpuType = %d",CpuType); + WriteTrace(TraceN64System, TraceDebug, "CpuType = %d",CpuType); if (CpuType == CPU_SyncCores && !g_Settings->LoadBool(Debugger_Enabled)) { g_Settings->SaveDword(Game_CpuType, CPU_Recompiler); @@ -440,7 +447,7 @@ void CN64System::StartEmulation2(bool NewThread) m_Recomp = new CRecompiler(m_Reg, m_Profile, m_EndEmulation); } - WriteTrace(TraceN64System, TraceDebug, "Setting system as active"); + WriteTrace(TraceN64System, TraceDebug, "Setting system as active"); bool bSetActive = true; if (m_SyncCPU) { @@ -454,13 +461,13 @@ void CN64System::StartEmulation2(bool NewThread) if (!bSetActive) { - WriteTrace(TraceN64System, TraceWarning, "Failed to set system as active"); + WriteTrace(TraceN64System, TraceWarning, "Failed to set system as active"); g_Settings->SaveBool(GameRunning_LoadingInProgress, false); g_Notify->DisplayError(MSG_PLUGIN_NOT_INIT); } else { - WriteTrace(TraceN64System, TraceDebug, "Starting emulation thread"); + WriteTrace(TraceN64System, TraceDebug, "Starting emulation thread"); StartEmulationThead(); } } @@ -468,10 +475,10 @@ void CN64System::StartEmulation2(bool NewThread) { //mark the emulation as starting and fix up menus g_Notify->DisplayMessage(5, MSG_EMULATION_STARTED); - WriteTrace(TraceN64System, TraceDebug, "Start Executing CPU"); + WriteTrace(TraceN64System, TraceDebug, "Start Executing CPU"); ExecuteCPU(); } - WriteTrace(TraceN64System, TraceDebug, "Done"); + WriteTrace(TraceN64System, TraceDebug, "Done"); } void CN64System::StartEmulation(bool NewThread) diff --git a/Source/Project64-core/N64System/N64DiskClass.cpp b/Source/Project64-core/N64System/N64DiskClass.cpp index cecc83c5c..0eaa55819 100644 --- a/Source/Project64-core/N64System/N64DiskClass.cpp +++ b/Source/Project64-core/N64System/N64DiskClass.cpp @@ -41,6 +41,40 @@ bool CN64Disk::LoadDiskImage(const char * FileLoc) g_Settings->SaveBool(GameRunning_LoadingInProgress, false); } + m_FileName = FileLoc; + return true; +} + +bool CN64Disk::SaveDiskImage() +{ + ForceByteSwapDisk(); + + if (m_DiskFormat == DiskFormatMAME) + { + //If original file was MAME format, just copy + //SDK format protection + WriteTrace(TraceN64System, TraceDebug, "Trying to open %s", m_FileName); + m_DiskFile.Close(); + if (!m_DiskFile.Open(m_FileName.c_str(), CFileBase::modeWrite)) + { + WriteTrace(TraceN64System, TraceError, "Failed to open %s", m_FileName); + return false; + } + + m_DiskFile.SeekToBegin(); + if (!m_DiskFile.Write(m_DiskImage, MameFormatSize)) + { + m_DiskFile.Close(); + WriteTrace(TraceN64System, TraceError, "Failed to write file"); + return false; + } + } + else if (m_DiskFormat == DiskFormatSDK) + { + //If original file was SDK format, we need to convert it back + } + + m_DiskFile.Close(); return true; } @@ -101,6 +135,7 @@ bool CN64Disk::AllocateAndLoadDiskImage(const char * FileLoc) if (DiskFileSize == MameFormatSize) { //If Disk is MAME Format (size is constant, it should be the same for every file), then continue + m_DiskFormat = DiskFormatMAME; WriteTrace(TraceN64System, TraceDebug, "Disk File is MAME Format"); if (!AllocateDiskImage(DiskFileSize)) @@ -144,6 +179,8 @@ bool CN64Disk::AllocateAndLoadDiskImage(const char * FileLoc) { //If Disk is SDK format (made with SDK based dumpers like LuigiBlood's, or Nintendo's, size is also constant) //We need to convert it. + m_DiskFormat = DiskFormatSDK; + g_Notify->DisplayMessage(5, MSG_LOADING); //Allocate supported size @@ -193,6 +230,21 @@ void CN64Disk::ByteSwapDisk() } } +void CN64Disk::ForceByteSwapDisk() +{ + uint32_t count; + + for (count = 0; count < m_DiskFileSize; count += 4) + { + m_DiskImage[count] ^= m_DiskImage[count + 3]; + m_DiskImage[count + 3] ^= m_DiskImage[count]; + m_DiskImage[count] ^= m_DiskImage[count + 3]; + m_DiskImage[count + 1] ^= m_DiskImage[count + 2]; + m_DiskImage[count + 2] ^= m_DiskImage[count + 1]; + m_DiskImage[count + 1] ^= m_DiskImage[count + 2]; + } +} + void CN64Disk::SetError(LanguageStringID ErrorMsg) { m_ErrorMsg = ErrorMsg; diff --git a/Source/Project64-core/N64System/N64DiskClass.h b/Source/Project64-core/N64System/N64DiskClass.h index 2b3938966..991523767 100644 --- a/Source/Project64-core/N64System/N64DiskClass.h +++ b/Source/Project64-core/N64System/N64DiskClass.h @@ -18,6 +18,7 @@ public: ~CN64Disk(); bool LoadDiskImage(const char * FileLoc); + bool SaveDiskImage(); static bool IsValidDiskImage(uint8_t Test[4]); uint8_t * GetDiskAddress() { return m_DiskImage; } uint8_t * GetDiskAddressBuffer() { return m_DiskImage + m_DiskBufAddress; } @@ -30,11 +31,14 @@ private: bool AllocateDiskImage(uint32_t DiskFileSize); bool AllocateAndLoadDiskImage(const char * FileLoc); void ByteSwapDisk(); + void ForceByteSwapDisk(); void SetError(LanguageStringID ErrorMsg); void ConvertDiskFormat(); + void ConvertDiskFormatBack(); //constant values - enum { ReadFromRomSection = 0x400000, MameFormatSize = 0x0435B0C0, SDKFormatSize = 0x03DEC800 }; + enum { ReadFromRomSection = 0x400000, MameFormatSize = 0x0435B0C0, SDKFormatSize = 0x03DEC800, + DiskFormatMAME = 0x0, DiskFormatSDK = 0x1 }; //class variables CFile m_DiskFile; @@ -44,6 +48,7 @@ private: uint32_t m_DiskBufAddress; LanguageStringID m_ErrorMsg; stdstr m_FileName, m_DiskIdent; + uint8_t m_DiskFormat; //0 = MAME, 1 = SDK //disk convert #define SECTORS_PER_BLOCK 85