diff --git a/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.cpp b/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.cpp index a39a769a0..b4b295e4a 100644 --- a/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.cpp +++ b/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.cpp @@ -1,14 +1,21 @@ #include "stdafx.h" #include "PifRamHandler.h" -#include -#include +#include +#include +#include +#include +#include +#include -PifRamHandler::PifRamHandler(CMipsMemoryVM & MMU, CRegisters & Reg) : - m_MMU(MMU), - m_PifRam(MMU.PifRam()), - m_PC(Reg.m_PROGRAM_COUNTER) +PifRamHandler::PifRamHandler(CN64System & System, bool SavesReadOnly) : + m_MMU(System.m_MMU_VM), + m_PC(System.m_Reg.m_PROGRAM_COUNTER), + m_Eeprom(SavesReadOnly) { + SystemReset(); + + System.RegisterCallBack(CN64SystemCB_Reset, this, (CN64System::CallBackFunction)stSystemReset); } bool PifRamHandler::Read32(uint32_t Address, uint32_t & Value) @@ -66,12 +73,704 @@ bool PifRamHandler::Write32(uint32_t Address, uint32_t Value, uint32_t Mask) *(uint32_t *)(&m_PifRam[Address - 0x1FC007C0]) = Value; if (Address == 0x1FC007FC) { - m_MMU.PifRamWrite(); + ControlWrite(); } } return true; } +void PifRamHandler::DMA_READ() +{ + uint8_t * PifRamPos = m_PifRam; + uint8_t * RDRAM = g_MMU->Rdram(); + + uint32_t & SI_DRAM_ADDR_REG = (uint32_t &)g_Reg->SI_DRAM_ADDR_REG; + if ((int32_t)SI_DRAM_ADDR_REG > (int32_t)g_System->RdramSize()) + { + if (bShowPifRamErrors()) + { + g_Notify->DisplayError(stdstr_f("%s\nSI_DRAM_ADDR_REG not in RDRAM space", __FUNCTION__).c_str()); + } + return; + } + + ControlRead(); + + if (CDebugSettings::HaveDebugger()) + { + g_Debugger->PIFReadStarted(); + } + + SI_DRAM_ADDR_REG &= 0xFFFFFFF8; + if ((int32_t)SI_DRAM_ADDR_REG < 0) + { + int32_t count, RdramPos; + + RdramPos = (int32_t)SI_DRAM_ADDR_REG; + for (count = 0; count < 0x40; count++, RdramPos++) + { + if (RdramPos < 0) + { + continue; + } + RDRAM[RdramPos ^ 3] = m_PifRam[count]; + } + } + else + { + for (size_t i = 0; i < 64; i++) + { + RDRAM[(SI_DRAM_ADDR_REG + i) ^ 3] = PifRamPos[i]; + } + } + + if (LogPRDMAMemStores()) + { + int32_t count; + char HexData[100], AsciiData[100], Addon[20]; + LogMessage("\tData DMAed to RDRAM:"); + LogMessage("\t--------------------"); + for (count = 0; count < 16; count++) + { + if ((count % 4) == 0) + { + HexData[0] = '\0'; + AsciiData[0] = '\0'; + } + sprintf(Addon, "%02X %02X %02X %02X", + m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], + m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); + strcat(HexData, Addon); + if (((count + 1) % 4) != 0) + { + sprintf(Addon, "-"); + strcat(HexData, Addon); + } + + sprintf(Addon, "%c%c%c%c", + m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], + m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); + strcat(AsciiData, Addon); + + if (((count + 1) % 4) == 0) + { + LogMessage("\t%s %s", HexData, AsciiData); + } + } + LogMessage(""); + } + + if (g_System->bRandomizeSIPIInterrupts()) + { + if (g_System->bDelaySI()) + { + g_SystemTimer->SetTimer(CSystemTimer::SiTimer, 0x900 + (g_Random->next() % 0x40), false); + } + else + { + g_SystemTimer->SetTimer(CSystemTimer::SiTimer, g_Random->next() % 0x40, false); + } + } + else + { + if (g_System->bDelaySI()) + { + g_SystemTimer->SetTimer(CSystemTimer::SiTimer, 0x900, false); + } + else + { + g_Reg->MI_INTR_REG |= MI_INTR_SI; + g_Reg->SI_STATUS_REG |= SI_STATUS_INTERRUPT; + g_Reg->CheckInterrupts(); + } + } +} + +void PifRamHandler::DMA_WRITE() +{ + uint8_t * PifRamPos = m_PifRam; + + uint32_t & SI_DRAM_ADDR_REG = (uint32_t &)g_Reg->SI_DRAM_ADDR_REG; + if ((int32_t)SI_DRAM_ADDR_REG > (int32_t)g_System->RdramSize()) + { + if (bShowPifRamErrors()) + { + g_Notify->DisplayError("SI DMA\nSI_DRAM_ADDR_REG not in RDRAM space"); + } + return; + } + + SI_DRAM_ADDR_REG &= 0xFFFFFFF8; + uint8_t * RDRAM = g_MMU->Rdram(); + + if ((int32_t)SI_DRAM_ADDR_REG < 0) + { + int32_t RdramPos = (int32_t)SI_DRAM_ADDR_REG; + + for (int32_t count = 0; count < 0x40; count++, RdramPos++) + { + if (RdramPos < 0) + { + m_PifRam[count] = 0; + continue; + } + m_PifRam[count] = RDRAM[RdramPos ^ 3]; + } + } + else + { + for (size_t i = 0; i < 64; i++) + { + PifRamPos[i] = RDRAM[(SI_DRAM_ADDR_REG + i) ^ 3]; + } + } + + if (LogPRDMAMemLoads()) + { + int32_t count; + char HexData[100], AsciiData[100], Addon[20]; + LogMessage(""); + LogMessage("\tData DMAed to the PIF RAM:"); + LogMessage("\t--------------------------"); + for (count = 0; count < 16; count++) + { + if ((count % 4) == 0) + { + HexData[0] = '\0'; + AsciiData[0] = '\0'; + } + sprintf(Addon, "%02X %02X %02X %02X", + m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], + m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); + strcat(HexData, Addon); + if (((count + 1) % 4) != 0) + { + sprintf(Addon, "-"); + strcat(HexData, Addon); + } + + sprintf(Addon, "%c%c%c%c", + m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], + m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); + strcat(AsciiData, Addon); + + if (((count + 1) % 4) == 0) + { + LogMessage("\t%s %s", HexData, AsciiData); + } + } + LogMessage(""); + } + + ControlWrite(); + + if (g_System->bDelaySI()) + { + g_SystemTimer->SetTimer(CSystemTimer::SiTimer, 0x900, false); + } + else + { + g_Reg->MI_INTR_REG |= MI_INTR_SI; + g_Reg->SI_STATUS_REG |= SI_STATUS_INTERRUPT; + g_Reg->CheckInterrupts(); + } +} + +void PifRamHandler::CicNus6105(const char * Challenge, char * Respone, int32_t Length) +{ + // clang-format off + static const char lut0[0x10] = { + 0x4, 0x7, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, + 0xC, 0xF, 0x8, 0xF, 0x6, 0x3, 0x6, 0x9, + }; + static const char lut1[0x10] = { + 0x4, 0x1, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, + 0xC, 0x9, 0x8, 0x5, 0x6, 0x3, 0xC, 0x9, + }; + // clang-format on + const char * lut = lut0; + char Key = 0xB; + + for (int32_t i = 0; i < Length; i++) + { + Respone[i] = (Key + 5 * Challenge[i]) & 0xF; + Key = lut[Respone[i]]; + int32_t Sgn = (Respone[i] >> 3) & 0x1; + int32_t Mag = ((Sgn == 1) ? ~Respone[i] : Respone[i]) & 0x7; + int32_t Mod = (Mag % 3 == 1) ? Sgn : 1 - Sgn; + if (lut == lut1 && (Respone[i] == 0x1 || Respone[i] == 0x9)) + { + Mod = 1; + } + if (lut == lut1 && (Respone[i] == 0xB || Respone[i] == 0xE)) + { + Mod = 0; + } + lut = (Mod == 1) ? lut1 : lut0; + } +} + +void PifRamHandler::ControlRead() +{ + if (m_PifRam[0x3F] == 0x2) + { + return; + } + + CONTROL * Controllers = g_Plugins->Control()->PluginControllers(); + + int32_t Channel = 0; + for (int32_t CurPos = 0; CurPos < 0x40; CurPos++) + { + switch (m_PifRam[CurPos]) + { + case 0x00: + Channel += 1; + if (Channel > 6) + { + CurPos = 0x40; + } + break; + case 0xFD: CurPos = 0x40; break; + case 0xFE: CurPos = 0x40; break; + case 0xFF: + case 0xB4: + case 0x56: + case 0xB8: + break; + default: + if ((m_PifRam[CurPos] & 0xC0) == 0) + { + if (Channel < 4) + { + if (Controllers[Channel].Present && Controllers[Channel].RawData) + { + if (g_Plugins->Control()->ReadController) + { + g_Plugins->Control()->ReadController(Channel, &m_PifRam[CurPos]); + } + } + else + { + ReadControllerCommand(Channel, &m_PifRam[CurPos]); + } + } + CurPos += m_PifRam[CurPos] + (m_PifRam[CurPos + 1] & 0x3F) + 1; + Channel += 1; + } + else + { + if (CurPos != 0x27 && bShowPifRamErrors()) + { + g_Notify->DisplayError(stdstr_f("Unknown command in PifRamRead(%X)", m_PifRam[CurPos]).c_str()); + } + CurPos = 0x40; + } + break; + } + } + if (g_Plugins->Control()->ReadController) + { + g_Plugins->Control()->ReadController(-1, nullptr); + } +} + +void PifRamHandler::ControlWrite() +{ + enum + { + CHALLENGE_LENGTH = 0x20 + }; + + CONTROL * Controllers = g_Plugins->Control()->PluginControllers(); + int32_t Channel = 0, CurPos; + + if (m_PifRam[0x3F] > 0x1) + { + switch (m_PifRam[0x3F]) + { + case 0x02: + // Format the 'challenge' message into 30 nibbles for X-Scale's CIC code + { + char Challenge[30], Response[30]; + for (int32_t i = 0; i < 15; i++) + { + Challenge[i * 2] = (m_PifRam[48 + i] >> 4) & 0x0f; + Challenge[i * 2 + 1] = m_PifRam[48 + i] & 0x0f; + } + CicNus6105(Challenge, Response, CHALLENGE_LENGTH - 2); + uint64_t ResponseValue = 0; + m_PifRam[46] = m_PifRam[47] = 0x00; + for (int32_t z = 8; z > 0; z--) + { + ResponseValue = (ResponseValue << 8) | ((Response[(z - 1) * 2] << 4) + Response[(z - 1) * 2 + 1]); + } + memcpy(&m_PifRam[48], &ResponseValue, sizeof(uint64_t)); + ResponseValue = 0; + for (int32_t z = 7; z > 0; z--) + { + ResponseValue = (ResponseValue << 8) | ((Response[((z + 8) - 1) * 2] << 4) + Response[((z + 8) - 1) * 2 + 1]); + } + memcpy(&m_PifRam[56], &ResponseValue, sizeof(uint64_t)); + } + break; + case 0x08: + m_PifRam[0x3F] = 0; + g_Reg->MI_INTR_REG |= MI_INTR_SI; + g_Reg->SI_STATUS_REG |= SI_STATUS_INTERRUPT; + g_Reg->CheckInterrupts(); + break; + case 0x10: + memset(m_PifRom, 0, 0x7C0); + break; + case 0x30: + m_PifRam[0x3F] = 0x80; + break; + case 0xC0: + memset(m_PifRam, 0, 0x40); + break; + default: + if (bShowPifRamErrors()) + { + g_Notify->DisplayError(stdstr_f("Unknown PifRam control: %d", m_PifRam[0x3F]).c_str()); + } + } + return; + } + + for (CurPos = 0; CurPos < 0x40; CurPos++) + { + switch (m_PifRam[CurPos]) + { + case 0x00: + Channel += 1; + if (Channel > 6) + { + CurPos = 0x40; + } + break; + case 0xFD: CurPos = 0x40; break; + case 0xFE: CurPos = 0x40; break; + case 0xFF: + case 0xB4: + case 0x56: + case 0xB8: + break; + default: + if ((m_PifRam[CurPos] & 0xC0) == 0) + { + if (Channel < 4) + { + if (Controllers[Channel].Present && Controllers[Channel].RawData) + { + if (g_Plugins->Control()->ControllerCommand) + { + g_Plugins->Control()->ControllerCommand(Channel, &m_PifRam[CurPos]); + } + } + else + { + ProcessControllerCommand(Channel, &m_PifRam[CurPos]); + } + } + else if (Channel == 4) + { + m_Eeprom.EepromCommand(&m_PifRam[CurPos]); + } + else + { + if (bShowPifRamErrors()) + { + g_Notify->DisplayError("Command on channel 5?"); + } + } + CurPos += m_PifRam[CurPos] + (m_PifRam[CurPos + 1] & 0x3F) + 1; + Channel += 1; + } + else + { + if (CurPos != 0x27 && bShowPifRamErrors()) + { + g_Notify->DisplayError(stdstr_f("Unknown Command in PifRamWrite(%X)", m_PifRam[CurPos]).c_str()); + } + CurPos = 0x40; + } + break; + } + } + m_PifRam[0x3F] = 0; + if (g_Plugins->Control()->ControllerCommand) + { + g_Plugins->Control()->ControllerCommand(-1, nullptr); + } +} + +void PifRamHandler::LogControllerPakData(const char * Description) +{ + int32_t count, count2; + char HexData[100], AsciiData[100], Addon[20]; + LogMessage("\t%s:", Description); + LogMessage("\t------------------------------"); + for (count = 0; count < 16; count++) + { + if ((count % 4) == 0) + { + HexData[0] = '\0'; + AsciiData[0] = '\0'; + } + sprintf(Addon, "%02X %02X %02X %02X", + m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], + m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); + strcat(HexData, Addon); + if (((count + 1) % 4) != 0) + { + sprintf(Addon, "-"); + strcat(HexData, Addon); + } + + Addon[0] = 0; + for (count2 = 0; count2 < 4; count2++) + { + if (m_PifRam[(count << 2) + count2] < 30) + { + strcat(Addon, "."); + } + else + { + char tmp[2]; + sprintf(tmp, "%c", m_PifRam[(count << 2) + count2]); + strcat(Addon, tmp); + } + } + strcat(AsciiData, Addon); + + if (((count + 1) % 4) == 0) + { + LogMessage("\t%s %s", HexData, AsciiData); + } + } + LogMessage(""); +} + +void PifRamHandler::ReadControllerCommand(int32_t Control, uint8_t * Command) +{ + CONTROL * Controllers = g_Plugins->Control()->PluginControllers(); + + switch (Command[2]) + { + case 0x01: // Read controller + if (Controllers[Control].Present != PRESENT_NONE) + { + if (bShowPifRamErrors()) + { + if (Command[0] != 1 || Command[1] != 4) + { + g_Notify->DisplayError("What am I meant to do with this controller command?"); + } + } + + const uint32_t buttons = g_BaseSystem->GetButtons(Control); + memcpy(&Command[3], &buttons, sizeof(uint32_t)); + } + break; + case 0x02: // Read from controller pak + if (Controllers[Control].Present != PRESENT_NONE) + { + switch (Controllers[Control].Plugin) + { + case PLUGIN_RAW: + if (g_Plugins->Control()->ReadController) + { + g_Plugins->Control()->ReadController(Control, Command); + } + break; + } + } + break; + case 0x03: // Write controller pak + if (Controllers[Control].Present != PRESENT_NONE) + { + switch (Controllers[Control].Plugin) + { + case PLUGIN_RAW: + if (g_Plugins->Control()->ReadController) + { + g_Plugins->Control()->ReadController(Control, Command); + } + break; + } + } + break; + } +} + +void PifRamHandler::ProcessControllerCommand(int32_t Control, uint8_t * Command) +{ + CONTROL * Controllers = g_Plugins->Control()->PluginControllers(); + + switch (Command[2]) + { + case 0x00: // Check + case 0xFF: // Reset and check? + if ((Command[1] & 0x80) != 0) + { + break; + } + if (bShowPifRamErrors()) + { + if (Command[0] != 1 || Command[1] != 3) + { + g_Notify->DisplayError("What am I meant to do with this controller command?"); + } + } + if (Controllers[Control].Present != PRESENT_NONE) + { + if (Controllers[Control].Present != PRESENT_MOUSE) + { + //N64 Controller + Command[3] = 0x05; + Command[4] = 0x00; + switch (Controllers[Control].Plugin) + { + case PLUGIN_TRANSFER_PAK: + case PLUGIN_RUMBLE_PAK: + case PLUGIN_MEMPAK: + case PLUGIN_RAW: + Command[5] = 1; + break; + default: Command[5] = 0; break; + } + } + else //if (Controllers[Control].Present == PRESENT_MOUSE) + { + //N64 Mouse + Command[3] = 0x02; + Command[4] = 0x00; + Command[5] = 0x00; + } + } + else + { + Command[1] |= 0x80; + } + break; + case 0x01: // Read controller + if (bShowPifRamErrors()) + { + if (Command[0] != 1 || Command[1] != 4) + { + g_Notify->DisplayError("What am I meant to do with this controller command?"); + } + } + if (Controllers[Control].Present == PRESENT_NONE) + { + Command[1] |= 0x80; + } + break; + case 0x02: // Read from controller pak + if (LogControllerPak()) + { + LogControllerPakData("Read: before getting results"); + } + if (bShowPifRamErrors()) + { + if (Command[0] != 3 || Command[1] != 33) + { + g_Notify->DisplayError("What am I meant to do with this controller command?"); + } + } + if (Controllers[Control].Present != PRESENT_NONE) + { + uint32_t address = (Command[3] << 8) | (Command[4] & 0xE0); + uint8_t * data = &Command[5]; + + switch (Controllers[Control].Plugin) + { + case PLUGIN_RUMBLE_PAK: Rumblepak::ReadFrom(address, data); break; + case PLUGIN_MEMPAK: g_Mempak->ReadFrom(Control, address, data); break; + case PLUGIN_TRANSFER_PAK: Transferpak::ReadFrom((uint16_t)address, data); break; + case PLUGIN_RAW: + if (g_Plugins->Control()->ControllerCommand) + { + g_Plugins->Control()->ControllerCommand(Control, Command); + } + break; + default: + memset(&Command[5], 0, 0x20); + } + + if (Controllers[Control].Plugin != PLUGIN_RAW) + { + Command[0x25] = CMempak::CalculateCrc(data); + } + } + else + { + Command[1] |= 0x80; + } + if (LogControllerPak()) + { + LogControllerPakData("Read: after getting results"); + } + break; + case 0x03: // Write controller pak + if (LogControllerPak()) + { + LogControllerPakData("Write: before processing"); + } + if (bShowPifRamErrors()) + { + if (Command[0] != 35 || Command[1] != 1) + { + g_Notify->DisplayError("What am I meant to do with this controller command?"); + } + } + if (Controllers[Control].Present != PRESENT_NONE) + { + uint32_t address = (Command[3] << 8) | (Command[4] & 0xE0); + uint8_t * data = &Command[5]; + + switch (Controllers[Control].Plugin) + { + case PLUGIN_MEMPAK: g_Mempak->WriteTo(Control, address, data); break; + case PLUGIN_RUMBLE_PAK: Rumblepak::WriteTo(Control, address, data); break; + case PLUGIN_TRANSFER_PAK: Transferpak::WriteTo((uint16_t)address, data); break; + case PLUGIN_RAW: + if (g_Plugins->Control()->ControllerCommand) + { + g_Plugins->Control()->ControllerCommand(Control, Command); + } + break; + } + + if (Controllers[Control].Plugin != PLUGIN_RAW) + { + Command[0x25] = CMempak::CalculateCrc(data); + } + } + else + { + Command[1] |= 0x80; + } + if (LogControllerPak()) + { + LogControllerPakData("Write: after processing"); + } + break; + default: + if (bShowPifRamErrors()) + { + g_Notify->DisplayError(stdstr_f("Unknown ControllerCommand %d", Command[2]).c_str()); + } + } +} + +void PifRamHandler::SystemReset(void) +{ + memset(m_PifRam, 0, sizeof(m_PifRam)); + memset(m_PifRom, 0, sizeof(m_PifRom)); +} + uint32_t PifRamHandler::swap32by8(uint32_t word) { const uint32_t swapped = diff --git a/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.h b/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.h index b5dd530fb..111173153 100644 --- a/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.h +++ b/Source/Project64-core/N64System/MemoryHandler/PifRamHandler.h @@ -1,9 +1,11 @@ #pragma once #include "MemoryHandler.h" #include +#include #include #include +class CN64System; class CMipsMemoryVM; class CRegisters; @@ -13,18 +15,41 @@ class PifRamHandler : private CLogging { public: - PifRamHandler(CMipsMemoryVM & MMU, CRegisters & Reg); + PifRamHandler(CN64System & System, bool SavesReadOnly); bool Read32(uint32_t Address, uint32_t & Value); bool Write32(uint32_t Address, uint32_t Value, uint32_t Mask); + void DMA_READ(); + void DMA_WRITE(); + + uint8_t * PifRam(void) + { + return m_PifRam; + } + private: PifRamHandler(); PifRamHandler(const PifRamHandler &); PifRamHandler & operator=(const PifRamHandler &); + void CicNus6105(const char * Challenge, char response[], int32_t length); + void ControlRead(); + void ControlWrite(void); + void LogControllerPakData(const char * Description); + void ReadControllerCommand(int32_t Control, uint8_t * Command); + void ProcessControllerCommand(int32_t Control, uint8_t * Command); + void SystemReset(void); + static uint32_t swap32by8(uint32_t word); + static void stSystemReset(PifRamHandler * _this) + { + _this->SystemReset(); + } + CMipsMemoryVM & m_MMU; - uint8_t * m_PifRam; + uint8_t m_PifRom[0x7C0]; + uint8_t m_PifRam[0x40]; uint32_t & m_PC; + CEeprom m_Eeprom; }; \ No newline at end of file diff --git a/Source/Project64-core/N64System/MemoryHandler/SerialInterfaceHandler.cpp b/Source/Project64-core/N64System/MemoryHandler/SerialInterfaceHandler.cpp index 879c9ef9b..b3568b156 100644 --- a/Source/Project64-core/N64System/MemoryHandler/SerialInterfaceHandler.cpp +++ b/Source/Project64-core/N64System/MemoryHandler/SerialInterfaceHandler.cpp @@ -16,6 +16,7 @@ SerialInterfaceReg::SerialInterfaceReg(uint32_t * Interface) : SerialInterfaceHandler::SerialInterfaceHandler(CMipsMemoryVM & MMU, CRegisters & Reg) : SerialInterfaceReg(Reg.m_SerialInterface), MIPSInterfaceReg(Reg.m_Mips_Interface), + m_PifRamHandler(MMU.PifRam()), m_MMU(MMU), m_Reg(Reg), m_PC(Reg.m_PROGRAM_COUNTER) @@ -88,11 +89,11 @@ bool SerialInterfaceHandler::Write32(uint32_t Address, uint32_t Value, uint32_t case 0x04800000: SI_DRAM_ADDR_REG = (SI_DRAM_ADDR_REG & ~Mask) | (MaskedValue); break; case 0x04800004: SI_PIF_ADDR_RD64B_REG = (SI_PIF_ADDR_RD64B_REG & ~Mask) | (MaskedValue); - m_MMU.SI_DMA_READ(); + m_PifRamHandler.DMA_READ(); break; case 0x04800010: SI_PIF_ADDR_WR64B_REG = (SI_PIF_ADDR_WR64B_REG & ~Mask) | (MaskedValue); - m_MMU.SI_DMA_WRITE(); + m_PifRamHandler.DMA_WRITE(); break; case 0x04800018: MI_INTR_REG &= ~MI_INTR_SI; diff --git a/Source/Project64-core/N64System/MemoryHandler/SerialInterfaceHandler.h b/Source/Project64-core/N64System/MemoryHandler/SerialInterfaceHandler.h index 1e033ee4b..ae5cd5af2 100644 --- a/Source/Project64-core/N64System/MemoryHandler/SerialInterfaceHandler.h +++ b/Source/Project64-core/N64System/MemoryHandler/SerialInterfaceHandler.h @@ -13,6 +13,8 @@ enum SI_STATUS_INTERRUPT = 0x1000, }; +class PifRamHandler; + class SerialInterfaceReg { protected: @@ -51,6 +53,7 @@ private: SerialInterfaceHandler(const SerialInterfaceHandler &); SerialInterfaceHandler & operator=(const SerialInterfaceHandler &); + PifRamHandler & m_PifRamHandler; CMipsMemoryVM & m_MMU; CRegisters & m_Reg; uint32_t & m_PC; diff --git a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp index 59837fc0b..ad294ec05 100755 --- a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp +++ b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.cpp @@ -17,7 +17,6 @@ uint32_t CMipsMemoryVM::RegModValue; #pragma warning(disable : 4355) // Disable 'this' : used in base member initializer list CMipsMemoryVM::CMipsMemoryVM(CN64System & System, bool SavesReadOnly) : - CPifRam(SavesReadOnly), m_System(System), m_Reg(System.m_Reg), m_AudioInterfaceHandler(System, System.m_Reg), @@ -29,7 +28,7 @@ CMipsMemoryVM::CMipsMemoryVM(CN64System & System, bool SavesReadOnly) : m_ISViewerHandler(System), m_MIPSInterfaceHandler(System.m_Reg), m_PeripheralInterfaceHandler(System, *this, System.m_Reg, m_CartridgeDomain2Address2Handler), - m_PifRamHandler(*this, System.m_Reg), + m_PifRamHandler(System, SavesReadOnly), m_RDRAMInterfaceHandler(System.m_Reg), m_RomMemoryHandler(System, System.m_Reg, *g_Rom), m_SerialInterfaceHandler(*this, System.m_Reg), @@ -183,7 +182,6 @@ bool CMipsMemoryVM::Initialize(bool SyncSystem) m_DMEM = (uint8_t *)(m_RDRAM + 0x04000000); m_IMEM = (uint8_t *)(m_RDRAM + 0x04001000); - CPifRam::Reset(); m_MemoryReadMap = new size_t[0x100000]; if (m_MemoryReadMap == nullptr) @@ -266,7 +264,6 @@ void CMipsMemoryVM::FreeMemory() delete[] m_MemoryWriteMap; m_MemoryWriteMap = nullptr; } - CPifRam::Reset(); } uint8_t * CMipsMemoryVM::MemoryPtr(uint32_t VAddr, uint32_t Size, bool Read) diff --git a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h index 61de1226e..e65d913ee 100644 --- a/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h +++ b/Source/Project64-core/N64System/Mips/MemoryVirtualMem.h @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -50,7 +49,6 @@ class CArmRecompilerOps; class CMipsMemoryVM : private R4300iOp, - public CPifRam, private CGameSettings { public: @@ -79,10 +77,6 @@ public: { return m_IMEM; } - uint8_t * PifRam() - { - return &m_PifRam[0]; - } CSram & GetSram() { @@ -149,6 +143,11 @@ public: { return m_RomMemoryHandler; }; + PifRamHandler & PifRam(void) + { + return m_PifRamHandler; + }; + private: CMipsMemoryVM(); diff --git a/Source/Project64-core/N64System/Mips/PifRam.cpp b/Source/Project64-core/N64System/Mips/PifRam.cpp deleted file mode 100644 index 065cdcccb..000000000 --- a/Source/Project64-core/N64System/Mips/PifRam.cpp +++ /dev/null @@ -1,714 +0,0 @@ -#include "stdafx.h" -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -CPifRam::CPifRam(bool SavesReadOnly) : - CEeprom(SavesReadOnly) -{ - Reset(); -} - -CPifRam::~CPifRam() -{ -} - -void CPifRam::Reset() -{ - memset(m_PifRam, 0, sizeof(m_PifRam)); - memset(m_PifRom, 0, sizeof(m_PifRom)); -} - -void CPifRam::n64_cic_nus_6105(char challenge[], char respone[], int32_t length) -{ - // clang-format off - static char lut0[0x10] = { - 0x4, 0x7, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, - 0xC, 0xF, 0x8, 0xF, 0x6, 0x3, 0x6, 0x9, - }; - static char lut1[0x10] = { - 0x4, 0x1, 0xA, 0x7, 0xE, 0x5, 0xE, 0x1, - 0xC, 0x9, 0x8, 0x5, 0x6, 0x3, 0xC, 0x9, - }; - // clang-format on - char key, *lut; - int32_t i, sgn, mag, mod; - - for (key = 0xB, lut = lut0, i = 0; i < length; i++) - { - respone[i] = (key + 5 * challenge[i]) & 0xF; - key = lut[respone[i]]; - sgn = (respone[i] >> 3) & 0x1; - mag = ((sgn == 1) ? ~respone[i] : respone[i]) & 0x7; - mod = (mag % 3 == 1) ? sgn : 1 - sgn; - if (lut == lut1 && (respone[i] == 0x1 || respone[i] == 0x9)) - { - mod = 1; - } - if (lut == lut1 && (respone[i] == 0xB || respone[i] == 0xE)) - { - mod = 0; - } - lut = (mod == 1) ? lut1 : lut0; - } -} - -void CPifRam::PifRamRead() -{ - if (m_PifRam[0x3F] == 0x2) - { - return; - } - - CONTROL * Controllers = g_Plugins->Control()->PluginControllers(); - - int32_t Channel = 0; - for (int32_t CurPos = 0; CurPos < 0x40; CurPos++) - { - switch (m_PifRam[CurPos]) - { - case 0x00: - Channel += 1; - if (Channel > 6) - { - CurPos = 0x40; - } - break; - case 0xFD: CurPos = 0x40; break; - case 0xFE: CurPos = 0x40; break; - case 0xFF: - case 0xB4: - case 0x56: - case 0xB8: - break; - default: - if ((m_PifRam[CurPos] & 0xC0) == 0) - { - if (Channel < 4) - { - if (Controllers[Channel].Present && Controllers[Channel].RawData) - { - if (g_Plugins->Control()->ReadController) - { - g_Plugins->Control()->ReadController(Channel, &m_PifRam[CurPos]); - } - } - else - { - ReadControllerCommand(Channel, &m_PifRam[CurPos]); - } - } - CurPos += m_PifRam[CurPos] + (m_PifRam[CurPos + 1] & 0x3F) + 1; - Channel += 1; - } - else - { - if (CurPos != 0x27 && bShowPifRamErrors()) - { - g_Notify->DisplayError(stdstr_f("Unknown command in PifRamRead(%X)", m_PifRam[CurPos]).c_str()); - } - CurPos = 0x40; - } - break; - } - } - if (g_Plugins->Control()->ReadController) - { - g_Plugins->Control()->ReadController(-1, nullptr); - } -} - -void CPifRam::PifRamWrite() -{ - CONTROL * Controllers = g_Plugins->Control()->PluginControllers(); - int32_t Channel = 0, CurPos; - - if (m_PifRam[0x3F] > 0x1) - { - switch (m_PifRam[0x3F]) - { - case 0x02: - // Format the 'challenge' message into 30 nibbles for X-Scale's CIC code - { - char Challenge[30], Response[30]; - for (int32_t i = 0; i < 15; i++) - { - Challenge[i * 2] = (m_PifRam[48 + i] >> 4) & 0x0f; - Challenge[i * 2 + 1] = m_PifRam[48 + i] & 0x0f; - } - n64_cic_nus_6105(Challenge, Response, CHALLENGE_LENGTH - 2); - uint64_t ResponseValue = 0; - m_PifRam[46] = m_PifRam[47] = 0x00; - for (int32_t z = 8; z > 0; z--) - { - ResponseValue = (ResponseValue << 8) | ((Response[(z - 1) * 2] << 4) + Response[(z - 1) * 2 + 1]); - } - memcpy(&m_PifRam[48], &ResponseValue, sizeof(uint64_t)); - ResponseValue = 0; - for (int32_t z = 7; z > 0; z--) - { - ResponseValue = (ResponseValue << 8) | ((Response[((z + 8) - 1) * 2] << 4) + Response[((z + 8) - 1) * 2 + 1]); - } - memcpy(&m_PifRam[56], &ResponseValue, sizeof(uint64_t)); - } - break; - case 0x08: - m_PifRam[0x3F] = 0; - g_Reg->MI_INTR_REG |= MI_INTR_SI; - g_Reg->SI_STATUS_REG |= SI_STATUS_INTERRUPT; - g_Reg->CheckInterrupts(); - break; - case 0x10: - memset(m_PifRom, 0, 0x7C0); - break; - case 0x30: - m_PifRam[0x3F] = 0x80; - break; - case 0xC0: - memset(m_PifRam, 0, 0x40); - break; - default: - if (bShowPifRamErrors()) - { - g_Notify->DisplayError(stdstr_f("Unknown PifRam control: %d", m_PifRam[0x3F]).c_str()); - } - } - return; - } - - for (CurPos = 0; CurPos < 0x40; CurPos++) - { - switch (m_PifRam[CurPos]) - { - case 0x00: - Channel += 1; - if (Channel > 6) - { - CurPos = 0x40; - } - break; - case 0xFD: CurPos = 0x40; break; - case 0xFE: CurPos = 0x40; break; - case 0xFF: - case 0xB4: - case 0x56: - case 0xB8: - break; - default: - if ((m_PifRam[CurPos] & 0xC0) == 0) - { - if (Channel < 4) - { - if (Controllers[Channel].Present && Controllers[Channel].RawData) - { - if (g_Plugins->Control()->ControllerCommand) - { - g_Plugins->Control()->ControllerCommand(Channel, &m_PifRam[CurPos]); - } - } - else - { - ProcessControllerCommand(Channel, &m_PifRam[CurPos]); - } - } - else if (Channel == 4) - { - EepromCommand(&m_PifRam[CurPos]); - } - else - { - if (bShowPifRamErrors()) - { - g_Notify->DisplayError("Command on channel 5?"); - } - } - CurPos += m_PifRam[CurPos] + (m_PifRam[CurPos + 1] & 0x3F) + 1; - Channel += 1; - } - else - { - if (CurPos != 0x27 && bShowPifRamErrors()) - { - g_Notify->DisplayError(stdstr_f("Unknown Command in PifRamWrite(%X)", m_PifRam[CurPos]).c_str()); - } - CurPos = 0x40; - } - break; - } - } - m_PifRam[0x3F] = 0; - if (g_Plugins->Control()->ControllerCommand) - { - g_Plugins->Control()->ControllerCommand(-1, nullptr); - } -} - -void CPifRam::SI_DMA_READ() -{ - uint8_t * PifRamPos = m_PifRam; - uint8_t * RDRAM = g_MMU->Rdram(); - - uint32_t & SI_DRAM_ADDR_REG = (uint32_t &)g_Reg->SI_DRAM_ADDR_REG; - if ((int32_t)SI_DRAM_ADDR_REG > (int32_t)g_System->RdramSize()) - { - if (bShowPifRamErrors()) - { - g_Notify->DisplayError(stdstr_f("%s\nSI_DRAM_ADDR_REG not in RDRAM space", __FUNCTION__).c_str()); - } - return; - } - - PifRamRead(); - - if (CDebugSettings::HaveDebugger()) - { - g_Debugger->PIFReadStarted(); - } - - SI_DRAM_ADDR_REG &= 0xFFFFFFF8; - if ((int32_t)SI_DRAM_ADDR_REG < 0) - { - int32_t count, RdramPos; - - RdramPos = (int32_t)SI_DRAM_ADDR_REG; - for (count = 0; count < 0x40; count++, RdramPos++) - { - if (RdramPos < 0) - { - continue; - } - RDRAM[RdramPos ^ 3] = m_PifRam[count]; - } - } - else - { - for (size_t i = 0; i < 64; i++) - { - RDRAM[(SI_DRAM_ADDR_REG + i) ^ 3] = PifRamPos[i]; - } - } - - if (LogPRDMAMemStores()) - { - int32_t count; - char HexData[100], AsciiData[100], Addon[20]; - LogMessage("\tData DMAed to RDRAM:"); - LogMessage("\t--------------------"); - for (count = 0; count < 16; count++) - { - if ((count % 4) == 0) - { - HexData[0] = '\0'; - AsciiData[0] = '\0'; - } - sprintf(Addon, "%02X %02X %02X %02X", - m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], - m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); - strcat(HexData, Addon); - if (((count + 1) % 4) != 0) - { - sprintf(Addon, "-"); - strcat(HexData, Addon); - } - - sprintf(Addon, "%c%c%c%c", - m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], - m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); - strcat(AsciiData, Addon); - - if (((count + 1) % 4) == 0) - { - LogMessage("\t%s %s", HexData, AsciiData); - } - } - LogMessage(""); - } - - if (g_System->bRandomizeSIPIInterrupts()) - { - if (g_System->bDelaySI()) - { - g_SystemTimer->SetTimer(CSystemTimer::SiTimer, 0x900 + (g_Random->next() % 0x40), false); - } - else - { - g_SystemTimer->SetTimer(CSystemTimer::SiTimer, g_Random->next() % 0x40, false); - } - } - else - { - if (g_System->bDelaySI()) - { - g_SystemTimer->SetTimer(CSystemTimer::SiTimer, 0x900, false); - } - else - { - g_Reg->MI_INTR_REG |= MI_INTR_SI; - g_Reg->SI_STATUS_REG |= SI_STATUS_INTERRUPT; - g_Reg->CheckInterrupts(); - } - } -} - -void CPifRam::SI_DMA_WRITE() -{ - uint8_t * PifRamPos = m_PifRam; - - uint32_t & SI_DRAM_ADDR_REG = (uint32_t &)g_Reg->SI_DRAM_ADDR_REG; - if ((int32_t)SI_DRAM_ADDR_REG > (int32_t)g_System->RdramSize()) - { - if (bShowPifRamErrors()) - { - g_Notify->DisplayError("SI DMA\nSI_DRAM_ADDR_REG not in RDRAM space"); - } - return; - } - - SI_DRAM_ADDR_REG &= 0xFFFFFFF8; - uint8_t * RDRAM = g_MMU->Rdram(); - - if ((int32_t)SI_DRAM_ADDR_REG < 0) - { - int32_t RdramPos = (int32_t)SI_DRAM_ADDR_REG; - - for (int32_t count = 0; count < 0x40; count++, RdramPos++) - { - if (RdramPos < 0) - { - m_PifRam[count] = 0; - continue; - } - m_PifRam[count] = RDRAM[RdramPos ^ 3]; - } - } - else - { - for (size_t i = 0; i < 64; i++) - { - PifRamPos[i] = RDRAM[(SI_DRAM_ADDR_REG + i) ^ 3]; - } - } - - if (LogPRDMAMemLoads()) - { - int32_t count; - char HexData[100], AsciiData[100], Addon[20]; - LogMessage(""); - LogMessage("\tData DMAed to the PIF RAM:"); - LogMessage("\t--------------------------"); - for (count = 0; count < 16; count++) - { - if ((count % 4) == 0) - { - HexData[0] = '\0'; - AsciiData[0] = '\0'; - } - sprintf(Addon, "%02X %02X %02X %02X", - m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], - m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); - strcat(HexData, Addon); - if (((count + 1) % 4) != 0) - { - sprintf(Addon, "-"); - strcat(HexData, Addon); - } - - sprintf(Addon, "%c%c%c%c", - m_PifRam[(count << 2) + 0], m_PifRam[(count << 2) + 1], - m_PifRam[(count << 2) + 2], m_PifRam[(count << 2) + 3]); - strcat(AsciiData, Addon); - - if (((count + 1) % 4) == 0) - { - LogMessage("\t%s %s", HexData, AsciiData); - } - } - LogMessage(""); - } - - PifRamWrite(); - - if (g_System->bDelaySI()) - { - g_SystemTimer->SetTimer(CSystemTimer::SiTimer, 0x900, false); - } - else - { - g_Reg->MI_INTR_REG |= MI_INTR_SI; - g_Reg->SI_STATUS_REG |= SI_STATUS_INTERRUPT; - g_Reg->CheckInterrupts(); - } -} - -void CPifRam::ProcessControllerCommand(int32_t Control, uint8_t * Command) -{ - CONTROL * Controllers = g_Plugins->Control()->PluginControllers(); - - switch (Command[2]) - { - case 0x00: // Check - case 0xFF: // Reset and check? - if ((Command[1] & 0x80) != 0) - { - break; - } - if (bShowPifRamErrors()) - { - if (Command[0] != 1 || Command[1] != 3) - { - g_Notify->DisplayError("What am I meant to do with this controller command?"); - } - } - if (Controllers[Control].Present != PRESENT_NONE) - { - if (Controllers[Control].Present != PRESENT_MOUSE) - { - //N64 Controller - Command[3] = 0x05; - Command[4] = 0x00; - switch (Controllers[Control].Plugin) - { - case PLUGIN_TRANSFER_PAK: - case PLUGIN_RUMBLE_PAK: - case PLUGIN_MEMPAK: - case PLUGIN_RAW: - Command[5] = 1; - break; - default: Command[5] = 0; break; - } - } - else //if (Controllers[Control].Present == PRESENT_MOUSE) - { - //N64 Mouse - Command[3] = 0x02; - Command[4] = 0x00; - Command[5] = 0x00; - } - } - else - { - Command[1] |= 0x80; - } - break; - case 0x01: // Read controller - if (bShowPifRamErrors()) - { - if (Command[0] != 1 || Command[1] != 4) - { - g_Notify->DisplayError("What am I meant to do with this controller command?"); - } - } - if (Controllers[Control].Present == PRESENT_NONE) - { - Command[1] |= 0x80; - } - break; - case 0x02: // Read from controller pak - if (LogControllerPak()) - { - LogControllerPakData("Read: before getting results"); - } - if (bShowPifRamErrors()) - { - if (Command[0] != 3 || Command[1] != 33) - { - g_Notify->DisplayError("What am I meant to do with this controller command?"); - } - } - if (Controllers[Control].Present != PRESENT_NONE) - { - uint32_t address = (Command[3] << 8) | (Command[4] & 0xE0); - uint8_t * data = &Command[5]; - - switch (Controllers[Control].Plugin) - { - case PLUGIN_RUMBLE_PAK: Rumblepak::ReadFrom(address, data); break; - case PLUGIN_MEMPAK: g_Mempak->ReadFrom(Control, address, data); break; - case PLUGIN_TRANSFER_PAK: Transferpak::ReadFrom((uint16_t)address, data); break; - case PLUGIN_RAW: - if (g_Plugins->Control()->ControllerCommand) - { - g_Plugins->Control()->ControllerCommand(Control, Command); - } - break; - default: - memset(&Command[5], 0, 0x20); - } - - if (Controllers[Control].Plugin != PLUGIN_RAW) - { - Command[0x25] = CMempak::CalculateCrc(data); - } - } - else - { - Command[1] |= 0x80; - } - if (LogControllerPak()) - { - LogControllerPakData("Read: after getting results"); - } - break; - case 0x03: // Write controller pak - if (LogControllerPak()) - { - LogControllerPakData("Write: before processing"); - } - if (bShowPifRamErrors()) - { - if (Command[0] != 35 || Command[1] != 1) - { - g_Notify->DisplayError("What am I meant to do with this controller command?"); - } - } - if (Controllers[Control].Present != PRESENT_NONE) - { - uint32_t address = (Command[3] << 8) | (Command[4] & 0xE0); - uint8_t * data = &Command[5]; - - switch (Controllers[Control].Plugin) - { - case PLUGIN_MEMPAK: g_Mempak->WriteTo(Control, address, data); break; - case PLUGIN_RUMBLE_PAK: Rumblepak::WriteTo(Control, address, data); break; - case PLUGIN_TRANSFER_PAK: Transferpak::WriteTo((uint16_t)address, data); break; - case PLUGIN_RAW: - if (g_Plugins->Control()->ControllerCommand) - { - g_Plugins->Control()->ControllerCommand(Control, Command); - } - break; - } - - if (Controllers[Control].Plugin != PLUGIN_RAW) - { - Command[0x25] = CMempak::CalculateCrc(data); - } - } - else - { - Command[1] |= 0x80; - } - if (LogControllerPak()) - { - LogControllerPakData("Write: after processing"); - } - break; - default: - if (bShowPifRamErrors()) - { - g_Notify->DisplayError(stdstr_f("Unknown ControllerCommand %d", Command[2]).c_str()); - } - } -} - -void CPifRam::ReadControllerCommand(int32_t Control, uint8_t * Command) -{ - CONTROL * Controllers = g_Plugins->Control()->PluginControllers(); - - switch (Command[2]) - { - case 0x01: // Read controller - if (Controllers[Control].Present != PRESENT_NONE) - { - if (bShowPifRamErrors()) - { - if (Command[0] != 1 || Command[1] != 4) - { - g_Notify->DisplayError("What am I meant to do with this controller command?"); - } - } - - const uint32_t buttons = g_BaseSystem->GetButtons(Control); - memcpy(&Command[3], &buttons, sizeof(uint32_t)); - } - break; - case 0x02: // Read from controller pak - if (Controllers[Control].Present != PRESENT_NONE) - { - switch (Controllers[Control].Plugin) - { - case PLUGIN_RAW: - if (g_Plugins->Control()->ReadController) - { - g_Plugins->Control()->ReadController(Control, Command); - } - break; - } - } - break; - case 0x03: // Write controller pak - if (Controllers[Control].Present != PRESENT_NONE) - { - switch (Controllers[Control].Plugin) - { - case PLUGIN_RAW: - if (g_Plugins->Control()->ReadController) - { - g_Plugins->Control()->ReadController(Control, Command); - } - break; - } - } - break; - } -} - -void CPifRam::LogControllerPakData(const char * Description) -{ - uint8_t * PIF_Ram = g_MMU->PifRam(); - - int32_t count, count2; - char HexData[100], AsciiData[100], Addon[20]; - LogMessage("\t%s:", Description); - LogMessage("\t------------------------------"); - for (count = 0; count < 16; count++) - { - if ((count % 4) == 0) - { - HexData[0] = '\0'; - AsciiData[0] = '\0'; - } - sprintf(Addon, "%02X %02X %02X %02X", - PIF_Ram[(count << 2) + 0], PIF_Ram[(count << 2) + 1], - PIF_Ram[(count << 2) + 2], PIF_Ram[(count << 2) + 3]); - strcat(HexData, Addon); - if (((count + 1) % 4) != 0) - { - sprintf(Addon, "-"); - strcat(HexData, Addon); - } - - Addon[0] = 0; - for (count2 = 0; count2 < 4; count2++) - { - if (PIF_Ram[(count << 2) + count2] < 30) - { - strcat(Addon, "."); - } - else - { - char tmp[2]; - sprintf(tmp, "%c", PIF_Ram[(count << 2) + count2]); - strcat(Addon, tmp); - } - } - strcat(AsciiData, Addon); - - if (((count + 1) % 4) == 0) - { - LogMessage("\t%s %s", HexData, AsciiData); - } - } - LogMessage(""); -} \ No newline at end of file diff --git a/Source/Project64-core/N64System/Mips/PifRam.h b/Source/Project64-core/N64System/Mips/PifRam.h deleted file mode 100644 index 1e715c4ea..000000000 --- a/Source/Project64-core/N64System/Mips/PifRam.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include -#include - -class CPifRam : - public CLogging, - private CEeprom -{ -public: - CPifRam(bool SavesReadOnly); - ~CPifRam(); - - void Reset(); - - void PifRamWrite(); - void PifRamRead(); - - void SI_DMA_READ(); - void SI_DMA_WRITE(); - -protected: - uint8_t m_PifRom[0x7C0]; - uint8_t m_PifRam[0x40]; - -private: - CPifRam(); - CPifRam(const CPifRam &); - CPifRam & operator=(const CPifRam &); - - enum - { - CHALLENGE_LENGTH = 0x20 - }; - void ProcessControllerCommand(int32_t Control, uint8_t * Command); - void ReadControllerCommand(int32_t Control, uint8_t * Command); - void LogControllerPakData(const char * Description); - void n64_cic_nus_6105(char challenge[], char response[], int32_t length); -}; diff --git a/Source/Project64-core/N64System/N64System.cpp b/Source/Project64-core/N64System/N64System.cpp index c3ba6536a..7c1b75af5 100644 --- a/Source/Project64-core/N64System/N64System.cpp +++ b/Source/Project64-core/N64System/N64System.cpp @@ -1896,7 +1896,7 @@ bool CN64System::SaveState() zipWriteInFileInZip(file, m_Reg.m_RDRAM_Interface, sizeof(uint32_t) * 8); zipWriteInFileInZip(file, m_Reg.m_SerialInterface, sizeof(uint32_t) * 4); zipWriteInFileInZip(file, (void * const)&m_TLB.TlbEntry(0), sizeof(CTLB::TLB_ENTRY) * 32); - zipWriteInFileInZip(file, m_MMU_VM.PifRam(), 0x40); + zipWriteInFileInZip(file, m_MMU_VM.PifRam().PifRam(), 0x40); zipWriteInFileInZip(file, m_MMU_VM.Rdram(), RdramSize); zipWriteInFileInZip(file, m_MMU_VM.Dmem(), 0x1000); zipWriteInFileInZip(file, m_MMU_VM.Imem(), 0x1000); @@ -1966,7 +1966,7 @@ bool CN64System::SaveState() hSaveFile.Write(m_Reg.m_RDRAM_Interface, sizeof(uint32_t) * 8); hSaveFile.Write(m_Reg.m_SerialInterface, sizeof(uint32_t) * 4); hSaveFile.Write(&m_TLB.TlbEntry(0), sizeof(CTLB::TLB_ENTRY) * 32); - hSaveFile.Write(g_MMU->PifRam(), 0x40); + hSaveFile.Write(g_MMU->PifRam().PifRam(), 0x40); hSaveFile.Write(g_MMU->Rdram(), RdramSize); hSaveFile.Write(g_MMU->Dmem(), 0x1000); hSaveFile.Write(g_MMU->Imem(), 0x1000); @@ -2161,7 +2161,7 @@ bool CN64System::LoadState(const char * FileName) unzReadCurrentFile(file, m_Reg.m_RDRAM_Interface, sizeof(uint32_t) * 8); unzReadCurrentFile(file, m_Reg.m_SerialInterface, sizeof(uint32_t) * 4); unzReadCurrentFile(file, (void * const)&m_TLB.TlbEntry(0), sizeof(CTLB::TLB_ENTRY) * 32); - unzReadCurrentFile(file, m_MMU_VM.PifRam(), 0x40); + unzReadCurrentFile(file, m_MMU_VM.PifRam().PifRam(), 0x40); unzReadCurrentFile(file, m_MMU_VM.Rdram(), SaveRDRAMSize); unzReadCurrentFile(file, m_MMU_VM.Dmem(), 0x1000); unzReadCurrentFile(file, m_MMU_VM.Imem(), 0x1000); @@ -2256,7 +2256,7 @@ bool CN64System::LoadState(const char * FileName) hSaveFile.Read(m_Reg.m_RDRAM_Interface, sizeof(uint32_t) * 8); hSaveFile.Read(m_Reg.m_SerialInterface, sizeof(uint32_t) * 4); hSaveFile.Read((void * const)&m_TLB.TlbEntry(0), sizeof(CTLB::TLB_ENTRY) * 32); - hSaveFile.Read(m_MMU_VM.PifRam(), 0x40); + hSaveFile.Read(m_MMU_VM.PifRam().PifRam(), 0x40); hSaveFile.Read(m_MMU_VM.Rdram(), SaveRDRAMSize); hSaveFile.Read(m_MMU_VM.Dmem(), 0x1000); hSaveFile.Read(m_MMU_VM.Imem(), 0x1000); diff --git a/Source/Project64-core/N64System/N64System.h b/Source/Project64-core/N64System/N64System.h index d81efd845..1d703e766 100644 --- a/Source/Project64-core/N64System/N64System.h +++ b/Source/Project64-core/N64System/N64System.h @@ -148,6 +148,7 @@ private: friend class R4300iOp; friend class VideoInterfaceHandler; + friend class PifRamHandler; // Used for loading and potentially executing the CPU in its own thread static void StartEmulationThread(CThread * thread); diff --git a/Source/Project64-core/N64System/Recompiler/x86/x86RecompilerOps.cpp b/Source/Project64-core/N64System/Recompiler/x86/x86RecompilerOps.cpp index 11025ba3f..68f190cf6 100644 --- a/Source/Project64-core/N64System/Recompiler/x86/x86RecompilerOps.cpp +++ b/Source/Project64-core/N64System/Recompiler/x86/x86RecompilerOps.cpp @@ -10737,14 +10737,14 @@ void CX86RecompilerOps::SW_Const(uint32_t Value, uint32_t VAddr) UpdateCounters(m_RegWorkingSet, false, true, false); m_Assembler.MoveConstToVariable(Value, &g_Reg->SI_PIF_ADDR_RD64B_REG, "SI_PIF_ADDR_RD64B_REG"); m_RegWorkingSet.BeforeCallDirect(); - m_Assembler.CallThis((uint32_t)((CPifRam *)g_MMU), AddressOf(&CPifRam::SI_DMA_READ), "CPifRam::SI_DMA_READ", 4); + m_Assembler.CallThis((uint32_t)(&g_MMU->m_PifRamHandler), AddressOf(&PifRamHandler::DMA_READ), "PifRamHandler::DMA_READ", 4); m_RegWorkingSet.AfterCallDirect(); break; case 0x04800010: UpdateCounters(m_RegWorkingSet, false, true, false); m_Assembler.MoveConstToVariable(Value, &g_Reg->SI_PIF_ADDR_WR64B_REG, "SI_PIF_ADDR_WR64B_REG"); m_RegWorkingSet.BeforeCallDirect(); - m_Assembler.CallThis((uint32_t)((CPifRam *)g_MMU), AddressOf(&CPifRam::SI_DMA_WRITE), "CPifRam::SI_DMA_WRITE", 4); + m_Assembler.CallThis((uint32_t)(&g_MMU->m_PifRamHandler), AddressOf(&PifRamHandler::DMA_WRITE), "PifRamHandler::DMA_WRITE", 4); m_RegWorkingSet.AfterCallDirect(); break; case 0x04800018: @@ -11118,13 +11118,13 @@ void CX86RecompilerOps::SW_Register(CX86Ops::x86Reg Reg, uint32_t VAddr) case 0x04800004: m_Assembler.MoveX86regToVariable(Reg, &g_Reg->SI_PIF_ADDR_RD64B_REG, "SI_PIF_ADDR_RD64B_REG"); m_RegWorkingSet.BeforeCallDirect(); - m_Assembler.CallThis((uint32_t)((CPifRam *)g_MMU), AddressOf(&CPifRam::SI_DMA_READ), "CPifRam::SI_DMA_READ", 4); + m_Assembler.CallThis((uint32_t)(&g_MMU->m_PifRamHandler), AddressOf(&PifRamHandler::DMA_READ), "PifRamHandler::DMA_READ", 4); m_RegWorkingSet.AfterCallDirect(); break; case 0x04800010: m_Assembler.MoveX86regToVariable(Reg, &g_Reg->SI_PIF_ADDR_WR64B_REG, "SI_PIF_ADDR_WR64B_REG"); m_RegWorkingSet.BeforeCallDirect(); - m_Assembler.CallThis((uint32_t)((CPifRam *)g_MMU), AddressOf(&CPifRam::SI_DMA_WRITE), "CPifRam::SI_DMA_WRITE", 4); + m_Assembler.CallThis((uint32_t)(&g_MMU->m_PifRamHandler), AddressOf(&PifRamHandler::DMA_WRITE), "PifRamHandler::DMA_WRITE", 4); m_RegWorkingSet.AfterCallDirect(); break; case 0x04800018: diff --git a/Source/Project64-core/Project64-core.vcxproj b/Source/Project64-core/Project64-core.vcxproj index 515e3d46c..49155b696 100644 --- a/Source/Project64-core/Project64-core.vcxproj +++ b/Source/Project64-core/Project64-core.vcxproj @@ -76,7 +76,6 @@ - @@ -199,7 +198,6 @@ - diff --git a/Source/Project64-core/Project64-core.vcxproj.filters b/Source/Project64-core/Project64-core.vcxproj.filters index 909f55f34..7f3390d8b 100644 --- a/Source/Project64-core/Project64-core.vcxproj.filters +++ b/Source/Project64-core/Project64-core.vcxproj.filters @@ -267,9 +267,6 @@ Source Files\N64 System\Mips - - Source Files\N64 System\Mips - Source Files\N64 System\Mips @@ -635,9 +632,6 @@ Header Files\N64 System\Mips - - Header Files\N64 System\Mips - Header Files\N64 System\Mips diff --git a/Source/Project64/UserInterface/Debugger/DebugMMU.cpp b/Source/Project64/UserInterface/Debugger/DebugMMU.cpp index 8038fa0dd..a22c344fd 100644 --- a/Source/Project64/UserInterface/Debugger/DebugMMU.cpp +++ b/Source/Project64/UserInterface/Debugger/DebugMMU.cpp @@ -57,7 +57,7 @@ uint8_t * CDebugMMU::GetPhysicalPtr(uint32_t paddr, WORD * flags) else if (paddr >= 0x1FC007C0 && paddr <= 0x1FC007FF) // PIF RAM { uint32_t pifRamOffset = paddr - 0x1FC007C0; - ptr = (uint8_t *)(g_MMU->PifRam() + pifRamOffset); + ptr = (uint8_t *)(g_MMU->PifRam().PifRam() + pifRamOffset); bBigEndian = true; } else