diff --git a/Core/CMakeLists.txt b/Core/CMakeLists.txt index 1c9a19417f..a32134aefc 100644 --- a/Core/CMakeLists.txt +++ b/Core/CMakeLists.txt @@ -578,6 +578,8 @@ add_library(Core STATIC HW/Display.h HW/GpioMMIO.cpp HW/GpioMMIO.h + HW/MMIO.cpp + HW/MMIO.h HW/AvcDecoder.cpp HW/AvcDecoder.h HW/MediaEngine.cpp diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index c413dd7ed7..37493b9955 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -493,6 +493,7 @@ + @@ -864,6 +865,7 @@ + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index 1ec7c1115c..468cdc841f 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -1171,6 +1171,9 @@ HW + + HW + HLE\Libraries @@ -2172,6 +2175,9 @@ HW + + HW + HLE\Libraries diff --git a/Core/HW/GpioMMIO.cpp b/Core/HW/GpioMMIO.cpp index 5f4349000d..c2568890c7 100644 --- a/Core/HW/GpioMMIO.cpp +++ b/Core/HW/GpioMMIO.cpp @@ -17,38 +17,73 @@ #include +#include "Common/Common.h" +#include "Common/Serialize/Serializer.h" +#include "Common/Serialize/SerializeFuncs.h" #include "Core/HW/GpioMMIO.h" +#include "Core/HW/MMIO.h" namespace GpioMMIO { namespace { -u32 g_regs[SIZE / 4]; - constexpr u32 REG_INTR_ACK = 0x024; +class GpioDevice : public MMIODevice { +public: + GpioDevice() : MMIODevice("GPIO", BASE_ADDRESS, SIZE) {} + + bool Read(MIPSState *mips, u32 offset, int size, u32 *value) override { + if (size != 4) + return false; + *value = regs_[offset / 4]; + return true; + } + + bool Write(MIPSState *mips, u32 offset, int size, u32 value) override { + if (size != 4) + return false; + if (offset == REG_INTR_ACK) { + // Acknowledging an interrupt clears the corresponding bit(s) in the triggered-status + // register, rather than storing the acknowledge value itself - matches uofw's usage + // (GpioAcquireIntr writes the same bit it's acknowledging) and JPCSP's + // acknowledgeInterrupt(). + regs_[REG_INTR_STATUS / 4] &= ~value; + return true; + } + regs_[offset / 4] = value; + return true; + } + + void Reset() override { + memset(regs_, 0, sizeof(regs_)); + } + + void DoState(PointerWrap &p) override { + auto s = p.Section("GpioMMIO", 1, 1); + if (!s) + return; + DoArray(p, regs_, ARRAY_SIZE(regs_)); + } + + void SetInterruptStatusBits(u32 bits) { + regs_[REG_INTR_STATUS / 4] |= bits; + } + +private: + u32 regs_[SIZE / 4]{}; +}; + +GpioDevice g_device; + } // namespace -u32 Read32(u32 address) { - u32 offset = address - BASE_ADDRESS; - return g_regs[offset / 4]; -} - -void Write32(u32 address, u32 value) { - u32 offset = address - BASE_ADDRESS; - if (offset == REG_INTR_ACK) { - // Acknowledging an interrupt clears the corresponding bit(s) in the triggered-status - // register, rather than storing the acknowledge value itself - matches uofw's usage - // (GpioAcquireIntr writes the same bit it's acknowledging) and JPCSP's - // acknowledgeInterrupt(). - g_regs[REG_INTR_STATUS / 4] &= ~value; - return; - } - g_regs[offset / 4] = value; +MMIODevice *Device() { + return &g_device; } void SetInterruptStatusBits(u32 bits) { - g_regs[REG_INTR_STATUS / 4] |= bits; + g_device.SetInterruptStatusBits(bits); } } // namespace GpioMMIO @@ -72,23 +107,103 @@ enum SysconCmd : u8 { // (including these first two), bytes 2.. = payload, byte [length] = checksum // (~sum(bytes[0..length-1]) & 0xFF). constexpr int DATA_SIZE = 16; -u8 g_data[DATA_SIZE]; -int g_dataIndex = 0; -int g_responseLen = 0; -bool g_dataExhausted = true; // AC_SUPPLY | WLAN_POWER | POWER_SWITCH - matches JPCSP's MMIOHandlerSyscon.getBaryonStatus(), // used as the RX status byte for every response (real hardware would report genuine baryon // status bits here; this is just a plausible constant since PPSSPP doesn't model those). constexpr u8 kBaryonStatus = 0x13; -// Persistent alarm value across sceSysconReadAlarm/WriteAlarm calls - mirrors JPCSP's -// sceSyscon.java readAlarm()/writeAlarm(), which are just as simple (a stored int, no real -// hardware backing). -u32 g_alarm = 0; +class SysconSerialDevice : public MMIODevice { +public: + SysconSerialDevice() : MMIODevice("syscon serial", BASE_ADDRESS, SIZE) {} -void ProcessCommand() { - u8 cmd = g_data[0]; + bool Read(MIPSState *mips, u32 offset, int size, u32 *value) override { + if (size != 4) + return false; + switch (offset) { + case 0x008: + { + *value = (data_[dataIndex_] << 8) | data_[dataIndex_ + 1]; + dataIndex_ += 2; + if (dataIndex_ > responseLen_) { + dataIndex_ = 0; + dataExhausted_ = true; + } + return true; + } + case 0x00C: + { + u32 flags = 1; // bit 0: no error. + if (!dataExhausted_) + flags |= 4; // bit 2: more response data available. + *value = flags; + return true; + } + default: + *value = 0; + return true; + } + } + + bool Write(MIPSState *mips, u32 offset, int size, u32 value) override { + if (size != 4) + return false; + switch (offset) { + case 0x004: + if (value & 4) { + dataIndex_ = 0; + } + if (value & 2) { + ProcessCommand(); + } + break; + case 0x008: + if (dataIndex_ + 1 < DATA_SIZE) { + data_[dataIndex_] = (value >> 8) & 0xFF; + data_[dataIndex_ + 1] = value & 0xFF; + } + dataIndex_ += 2; + break; + default: + break; + } + return true; + } + + void Reset() override { + memset(data_, 0, sizeof(data_)); + dataIndex_ = 0; + responseLen_ = 0; + dataExhausted_ = true; + alarm_ = 0; + } + + void DoState(PointerWrap &p) override { + auto s = p.Section("SysconSerialMMIO", 1, 1); + if (!s) + return; + DoArray(p, data_, DATA_SIZE); + Do(p, dataIndex_); + Do(p, responseLen_); + Do(p, dataExhausted_); + Do(p, alarm_); + } + +private: + void ProcessCommand(); + + u8 data_[DATA_SIZE]{}; + int dataIndex_ = 0; + int responseLen_ = 0; + bool dataExhausted_ = true; + // Persistent alarm value across sceSysconReadAlarm/WriteAlarm calls - mirrors JPCSP's + // sceSyscon.java readAlarm()/writeAlarm(), which are just as simple (a stored int, no real + // hardware backing). + u32 alarm_ = 0; +}; + +void SysconSerialDevice::ProcessCommand() { + u8 cmd = data_[0]; // Sub-status byte: must not be 0x80 or 0x81 (uofw's Syscon_cmd() treats those as // SYSCON_RES_80/81 and retries the whole command) - 0x82 is what JPCSP's own fallback @@ -103,89 +218,43 @@ void ProcessCommand() { payloadLen += 4; break; case CMD_READ_ALARM: - memcpy(&payload[payloadLen], &g_alarm, 4); + memcpy(&payload[payloadLen], &alarm_, 4); payloadLen += 4; break; case CMD_WRITE_CLOCK: // Not modeled - accept and ignore, same as JPCSP's writeClock(). break; case CMD_WRITE_ALARM: - memcpy(&g_alarm, &g_data[2], 4); + memcpy(&alarm_, &data_[2], 4); break; case CMD_NOP: default: break; } - g_data[0] = kBaryonStatus; - g_data[1] = (u8)(payloadLen + 2); - memcpy(&g_data[2], payload, payloadLen); + data_[0] = kBaryonStatus; + data_[1] = (u8)(payloadLen + 2); + memcpy(&data_[2], payload, payloadLen); u8 sum = 0; - for (int i = 0; i < g_data[1]; i++) - sum += g_data[i]; - g_data[g_data[1]] = (u8)~sum; + for (int i = 0; i < data_[1]; i++) + sum += data_[i]; + data_[data_[1]] = (u8)~sum; - g_dataIndex = 0; - g_responseLen = g_data[1]; - g_dataExhausted = false; + dataIndex_ = 0; + responseLen_ = data_[1]; + dataExhausted_ = false; // Signals "response ready" the way real hardware raising GPIO4 would - see // uofw's Syscon_cmd(), which polls exactly this bit after triggering a command. GpioMMIO::SetInterruptStatusBits(0x10); } +SysconSerialDevice g_device; + } // namespace -u32 Read32(u32 address) { - u32 offset = address - BASE_ADDRESS; - switch (offset) { - case 0x004: - return 0; - case 0x008: - { - u32 value = (g_data[g_dataIndex] << 8) | g_data[g_dataIndex + 1]; - g_dataIndex += 2; - if (g_dataIndex > g_responseLen) { - g_dataIndex = 0; - g_dataExhausted = true; - } - return value; - } - case 0x00C: - { - u32 flags = 1; // bit 0: no error. - if (!g_dataExhausted) - flags |= 4; // bit 2: more response data available. - return flags; - } - case 0x018: - return 0; - default: - return 0; - } -} - -void Write32(u32 address, u32 value) { - u32 offset = address - BASE_ADDRESS; - switch (offset) { - case 0x004: - if (value & 4) { - g_dataIndex = 0; - } - if (value & 2) { - ProcessCommand(); - } - break; - case 0x008: - if (g_dataIndex + 1 < DATA_SIZE) { - g_data[g_dataIndex] = (value >> 8) & 0xFF; - g_data[g_dataIndex + 1] = value & 0xFF; - } - g_dataIndex += 2; - break; - default: - break; - } +MMIODevice *Device() { + return &g_device; } } // namespace SysconSerialMMIO diff --git a/Core/HW/GpioMMIO.h b/Core/HW/GpioMMIO.h index 1b145b7da2..89a7c2ea75 100644 --- a/Core/HW/GpioMMIO.h +++ b/Core/HW/GpioMMIO.h @@ -48,6 +48,8 @@ #include "Common/CommonTypes.h" +class MMIODevice; + namespace GpioMMIO { constexpr u32 BASE_ADDRESS = 0xBE240000; @@ -58,12 +60,7 @@ constexpr u32 SIZE = 0x100; // line, is ever actually driven here since that's the only one this MMIO model needs). constexpr u32 REG_INTR_STATUS = 0x020; -inline bool IsGpioAddress(u32 address) { - return address >= BASE_ADDRESS && address < BASE_ADDRESS + SIZE; -} - -u32 Read32(u32 address); -void Write32(u32 address, u32 value); +MMIODevice *Device(); // Sets bits in the interrupt-triggered status register (offset 0x020) - used by // SysconSerialMMIO to signal "response ready" on GPIO pin 4 the same way real Syscon @@ -77,11 +74,6 @@ namespace SysconSerialMMIO { constexpr u32 BASE_ADDRESS = 0xBE580000; constexpr u32 SIZE = 0x100; -inline bool IsSysconSerialAddress(u32 address) { - return address >= BASE_ADDRESS && address < BASE_ADDRESS + SIZE; -} - -u32 Read32(u32 address); -void Write32(u32 address, u32 value); +MMIODevice *Device(); } // namespace SysconSerialMMIO diff --git a/Core/HW/MMIO.cpp b/Core/HW/MMIO.cpp new file mode 100644 index 0000000000..ffc3ba85f3 --- /dev/null +++ b/Core/HW/MMIO.cpp @@ -0,0 +1,95 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +#include + +#include "Common/Log.h" +#include "Common/Serialize/Serializer.h" +#include "Core/Core.h" +#include "Core/HW/GpioMMIO.h" +#include "Core/HW/MMIO.h" +#include "Core/MemMap.h" +#include "Core/MIPS/MIPS.h" + +namespace MMIO { + +// Unhandled reads return a distinctive poison value (not 0) so it's obvious in a live trace when +// some later computation's input traces back to an unimplemented register, rather than looking +// like an ordinary, legitimate zero. +constexpr u32 UNKNOWN_MMIO_POISON = 0x1337BEEF; + +static std::vector g_devices; + +void Init() { + g_devices = { + GpioMMIO::Device(), + SysconSerialMMIO::Device(), + }; + for (MMIODevice *device : g_devices) { + device->Reset(); + } +} + +void Shutdown() { + g_devices.clear(); +} + +void DoState(PointerWrap &p) { + auto s = p.Section("MMIO", 1, 1); + if (!s) + return; + // Every device, whether or not this boot registered it, so the layout doesn't depend on that. + GpioMMIO::Device()->DoState(p); + SysconSerialMMIO::Device()->DoState(p); +} + +static MMIODevice *FindDevice(u32 address) { + for (MMIODevice *device : g_devices) { + if (device->Contains(address)) + return device; + } + return nullptr; +} + +static bool CheckKernelMode(MIPSState *mips, u32 address, int size, MemoryExceptionType type) { + if (Memory::IsKernelCodeAddress(mips->pc)) + return true; + Core_MemoryException(address, size, mips->pc, type, "Kernel mode only"); + return false; +} + +u32 Read(MIPSState *mips, u32 address, int size) { + if (!CheckKernelMode(mips, address, size, MemoryExceptionType::READ_WORD)) + return UNKNOWN_MMIO_POISON; + MMIODevice *device = FindDevice(address); + u32 value; + if (device && device->Read(mips, address - device->Base(), size, &value)) + return value; + WARN_LOG(Log::CPU, "Unhandled MMIO Read%d at %08x%s%s", size * 8, address, device ? " in " : "", device ? device->Name() : ""); + return UNKNOWN_MMIO_POISON; +} + +void Write(MIPSState *mips, u32 address, int size, u32 value) { + if (!CheckKernelMode(mips, address, size, MemoryExceptionType::WRITE_WORD)) + return; + MMIODevice *device = FindDevice(address); + if (device && device->Write(mips, address - device->Base(), size, value)) + return; + WARN_LOG(Log::CPU, "Unhandled MMIO Write%d at %08x = %08x%s%s", size * 8, address, value, device ? " in " : "", device ? device->Name() : ""); +} + +} // namespace MMIO diff --git a/Core/HW/MMIO.h b/Core/HW/MMIO.h new file mode 100644 index 0000000000..fba78d7275 --- /dev/null +++ b/Core/HW/MMIO.h @@ -0,0 +1,64 @@ +// Copyright (c) 2012- PPSSPP Project. + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 2.0 or later versions. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License 2.0 for more details. + +// A copy of the GPL 2.0 should have been included with the program. +// If not, see http://www.gnu.org/licenses/ + +// Official git repository and contact information can be found at +// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. + +// Memory-mapped hardware registers, for kernel code that pokes the hardware directly (the firmware +// modules the VSH boot runs). Each block of registers is a device covering an address range; the +// interpreter sends an access there (Memory::IsMMIOAccess) to the device, or logs it as unhandled. + +#pragma once + +#include "Common/CommonTypes.h" + +class MIPSState; +class PointerWrap; + +class MMIODevice { +public: + MMIODevice(const char *name, u32 base, u32 size) : name_(name), base_(base), size_(size) {} + virtual ~MMIODevice() {} + + // offset is from the base. size is 1, 2 or 4. The CPU doing the access is passed for registers + // that answer each CPU differently. Returns false for an access the device doesn't handle. + virtual bool Read(MIPSState *mips, u32 offset, int size, u32 *value) = 0; + virtual bool Write(MIPSState *mips, u32 offset, int size, u32 value) = 0; + // To the power-on state. + virtual void Reset() = 0; + virtual void DoState(PointerWrap &p) = 0; + + const char *Name() const { return name_; } + bool Contains(u32 address) const { return address - base_ < size_; } + u32 Base() const { return base_; } + +private: + const char *name_; + u32 base_; + u32 size_; +}; + +namespace MMIO { + +// Registers the devices, in their power-on state. +void Init(); +void Shutdown(); +void DoState(PointerWrap &p); + +// For an address Memory::IsMMIOAccess() accepts. size is 1, 2 or 4. An access no device handles is +// logged, and reads as poison. +u32 Read(MIPSState *mips, u32 address, int size); +void Write(MIPSState *mips, u32 address, int size, u32 value); + +} // namespace MMIO diff --git a/Core/MIPS/IR/IRCompFPU.cpp b/Core/MIPS/IR/IRCompFPU.cpp index df91c8fcdb..990ce8efb2 100644 --- a/Core/MIPS/IR/IRCompFPU.cpp +++ b/Core/MIPS/IR/IRCompFPU.cpp @@ -80,6 +80,12 @@ void IRFrontend::Comp_FPULS(MIPSOpcode op) { CheckMemoryBreakpoint(rs, offset); + if (js.kernelMode) { + // Kernel code may reach hardware registers (MMIO), which only the interpreter handles. + DISABLE; + return; + } + switch (op >> 26) { case 49: // lwc1 ir.Write(IROp::LoadFloat, ft, rs, 0, offset); diff --git a/Core/MIPS/Interpreter.cpp b/Core/MIPS/Interpreter.cpp index 3ae1232190..4779cc0de2 100644 --- a/Core/MIPS/Interpreter.cpp +++ b/Core/MIPS/Interpreter.cpp @@ -36,7 +36,7 @@ #include "Core/HLE/HLETables.h" #include "Core/HLE/ReplaceTables.h" #include "Core/HLE/sceKernelInterrupt.h" -#include "Core/HW/GpioMMIO.h" +#include "Core/HW/MMIO.h" #define R(i) (mips->r[i]) #define F(i) (mips->f[i]) @@ -99,78 +99,6 @@ int MIPS_InterpretSingleStep(MIPSState *mips) { return 1; } -// MMIO handling, for limited LLE, just enough for VSH. -// -// Unregistered reads return a distinctive poison value (not 0) so it's obvious in a live trace -// when some later computation's input traces back to an unimplemented MMIO register, rather -// than looking like an ordinary, legitimate zero. -constexpr u32 UNKNOWN_MMIO_POISON = 0x1337BEEF; - -static u8 ReadMMIO_U8(MIPSState *mips, u32 addr) { - if (!Memory::IsKernelCodeAddress(mips->pc)) { - Core_MemoryException(addr, 1, mips->pc, MemoryExceptionType::READ_WORD, "Kernel mode only"); - return (u8)UNKNOWN_MMIO_POISON; - } - WARN_LOG(Log::CPU, "Unhandled MMIO Read8 at %08x", addr); - return (u8)UNKNOWN_MMIO_POISON; -} - -static u16 ReadMMIO_U16(MIPSState *mips, u32 addr) { - if (!Memory::IsKernelCodeAddress(mips->pc)) { - Core_MemoryException(addr, 2, mips->pc, MemoryExceptionType::READ_WORD, "Kernel mode only"); - return (u16)UNKNOWN_MMIO_POISON; - } - WARN_LOG(Log::CPU, "Unhandled MMIO Read16 at %08x", addr); - return (u16)UNKNOWN_MMIO_POISON; -} - -static u32 ReadMMIO_U32(MIPSState *mips, u32 addr) { - if (!Memory::IsKernelCodeAddress(mips->pc)) { - Core_MemoryException(addr, 4, mips->pc, MemoryExceptionType::READ_WORD, "Kernel mode only"); - return UNKNOWN_MMIO_POISON; - } - if (GpioMMIO::IsGpioAddress(addr)) { - return GpioMMIO::Read32(addr); - } - if (SysconSerialMMIO::IsSysconSerialAddress(addr)) { - return SysconSerialMMIO::Read32(addr); - } - WARN_LOG(Log::CPU, "Unhandled MMIO Read32 at %08x", addr); - return UNKNOWN_MMIO_POISON; -} - -void WriteMMIO_U8(MIPSState *mips, u32 addr, u8 value) { - if (!Memory::IsKernelCodeAddress(mips->pc)) { - Core_MemoryException(addr, 1, mips->pc, MemoryExceptionType::WRITE_WORD, "Kernel mode only"); - return; - } - WARN_LOG(Log::CPU, "Unhandled MMIO Write8 at %08x = %02x", addr, value); -} - -void WriteMMIO_U16(MIPSState *mips, u32 addr, u16 value) { - if (!Memory::IsKernelCodeAddress(mips->pc)) { - Core_MemoryException(addr, 2, mips->pc, MemoryExceptionType::WRITE_WORD, "Kernel mode only"); - return; - } - WARN_LOG(Log::CPU, "Unhandled MMIO Write16 at %08x = %04x", addr, value); -} - -void WriteMMIO_U32(MIPSState *mips, u32 addr, u32 value) { - if (!Memory::IsKernelCodeAddress(mips->pc)) { - Core_MemoryException(addr, 4, mips->pc, MemoryExceptionType::WRITE_WORD, "Kernel mode only"); - return; - } - if (GpioMMIO::IsGpioAddress(addr)) { - GpioMMIO::Write32(addr, value); - return; - } - if (SysconSerialMMIO::IsSysconSerialAddress(addr)) { - SysconSerialMMIO::Write32(addr, value); - return; - } - WARN_LOG(Log::CPU, "Unhandled MMIO Write32 at %08x = %08x", addr, value); -} - namespace MIPSInt { void Int_Cache(MIPSState *mips, MIPSOpcode op) { int imm = SignExtend16ToS32(op); @@ -427,8 +355,12 @@ namespace MIPSInt { case 48: // ll if (rt != 0) { if (!Memory::IsValid4AlignedAddress(addr)) { - Core_MemoryException(addr, 4, PC, MemoryExceptionType::READ_WORD, "ll"); - R(rt) = 0; + if (Memory::IsMMIOAccess(addr)) { + R(rt) = MMIO::Read(mips, addr, 4); + } else { + Core_MemoryException(addr, 4, PC, MemoryExceptionType::READ_WORD, "ll"); + R(rt) = 0; + } } else { R(rt) = Memory::ReadUnchecked_U32(addr); } @@ -438,7 +370,10 @@ namespace MIPSInt { case 56: // sc if (mips->llBit) { if (!Memory::IsValid4AlignedAddress(addr)) { - Core_MemoryException(addr, 4, PC, MemoryExceptionType::WRITE_WORD, "sc"); + if (Memory::IsMMIOAccess(addr)) + MMIO::Write(mips, addr, 4, R(rt)); + else + Core_MemoryException(addr, 4, PC, MemoryExceptionType::WRITE_WORD, "sc"); } else { Memory::WriteUnchecked_U32(R(rt), addr); } @@ -513,7 +448,7 @@ namespace MIPSInt { if (!Memory::IsValidAddress(addr)) { if (Memory::IsMMIOAccess(addr)) { // This is a read from MMIO, so we can handle it. - R(rt) = SignExtend8ToU32(ReadMMIO_U8(mips, addr)); + R(rt) = SignExtend8ToU32(MMIO::Read(mips, addr, 1)); break; } Core_MemoryException(addr, 1, PC, MemoryExceptionType::READ_WORD, "lb"); @@ -526,7 +461,7 @@ namespace MIPSInt { if (!Memory::IsValid2AlignedAddress(addr)) { if (Memory::IsMMIOAccess(addr)) { // This is a read from MMIO, so we can handle it. - R(rt) = SignExtend16ToU32(ReadMMIO_U16(mips, addr)); + R(rt) = SignExtend16ToU32(MMIO::Read(mips, addr, 2)); break; } Core_MemoryException(addr, 2, PC, MemoryExceptionType::READ_WORD, "lh"); @@ -539,7 +474,7 @@ namespace MIPSInt { if (!Memory::IsValid4AlignedAddress(addr)) { if (Memory::IsMMIOAccess(addr)) { // This is a read from MMIO, so we can handle it. - R(rt) = ReadMMIO_U32(mips, addr); + R(rt) = MMIO::Read(mips, addr, 4); break; } Core_MemoryException(addr, 4, PC, MemoryExceptionType::READ_WORD, "lw"); @@ -552,7 +487,7 @@ namespace MIPSInt { if (!Memory::IsValidAddress(addr)) { if (Memory::IsMMIOAccess(addr)) { // This is a read from MMIO, so we can handle it. - R(rt) = ReadMMIO_U8(mips, addr); + R(rt) = MMIO::Read(mips, addr, 1); break; } Core_MemoryException(addr, 1, PC, MemoryExceptionType::READ_WORD, "lbu"); @@ -565,7 +500,7 @@ namespace MIPSInt { if (!Memory::IsValid2AlignedAddress(addr)) { if (Memory::IsMMIOAccess(addr)) { // This is a read from MMIO, so we can handle it. - R(rt) = ReadMMIO_U16(mips, addr); + R(rt) = MMIO::Read(mips, addr, 2); break; } Core_MemoryException(addr, 2, PC, MemoryExceptionType::READ_WORD, "lhu"); @@ -578,7 +513,7 @@ namespace MIPSInt { if (!Memory::IsValidAddress(addr)) { if (Memory::IsMMIOAccess(addr)) { // This is a write to MMIO, so we can handle it. - WriteMMIO_U8(mips, addr, (u8)(R(rt))); + MMIO::Write(mips, addr, 1, (u8)R(rt)); break; } Core_MemoryException(addr, 1, PC, MemoryExceptionType::WRITE_WORD, "sb"); @@ -590,7 +525,7 @@ namespace MIPSInt { if (!Memory::IsValid2AlignedAddress(addr)) { if (Memory::IsMMIOAccess(addr)) { // This is a write to MMIO, so we can handle it. - WriteMMIO_U16(mips, addr, (u16)(R(rt))); + MMIO::Write(mips, addr, 2, (u16)R(rt)); break; } Core_MemoryException(addr, 2, PC, MemoryExceptionType::WRITE_WORD, "sh"); @@ -602,7 +537,7 @@ namespace MIPSInt { if (!Memory::IsValid4AlignedAddress(addr)) { if (Memory::IsMMIOAccess(addr)) { // This is a write to MMIO, so we can handle it. - WriteMMIO_U32(mips, addr, R(rt)); + MMIO::Write(mips, addr, 4, R(rt)); break; } Core_MemoryException(addr, 4, PC, MemoryExceptionType::WRITE_WORD, "sw"); @@ -619,6 +554,8 @@ namespace MIPSInt { u32 mem = 0; if (Memory::IsValidAddress(addr)) { mem = Memory::ReadUnchecked_U32(addr & 0xfffffffc); + } else if (Memory::IsMMIOAccess(addr)) { + mem = MMIO::Read(mips, addr & 0xfffffffc, 4); } else { Core_MemoryException(addr, 4, PC, MemoryExceptionType::READ_WORD, "lwl"); } @@ -634,6 +571,8 @@ namespace MIPSInt { u32 mem = 0; if (Memory::IsValidAddress(addr)) { mem = Memory::ReadUnchecked_U32(addr & 0xfffffffc); + } else if (Memory::IsMMIOAccess(addr)) { + mem = MMIO::Read(mips, addr & 0xfffffffc, 4); } else { Core_MemoryException(addr, 4, PC, MemoryExceptionType::READ_WORD, "lwr"); } @@ -688,6 +627,10 @@ namespace MIPSInt { switch (op >> 26) { case 49: if (!Memory::IsValid4AlignedAddress(addr)) { + if (Memory::IsMMIOAccess(addr)) { + FI(ft) = MMIO::Read(mips, addr, 4); + break; + } Core_MemoryException(addr, 4, PC, MemoryExceptionType::READ_WORD, "lwc1"); FI(ft) = 0; break; @@ -696,6 +639,10 @@ namespace MIPSInt { break; //lwc1 case 57: if (!Memory::IsValid4AlignedAddress(addr)) { + if (Memory::IsMMIOAccess(addr)) { + MMIO::Write(mips, addr, 4, FI(ft)); + break; + } Core_MemoryException(addr, 4, PC, MemoryExceptionType::WRITE_WORD, "swc1"); break; } diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index b19b7ccd99..57662ed013 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -64,6 +64,7 @@ #include "Core/MIPS/JitCommon/JitCommon.h" #include "Core/RetroAchievements.h" #include "HW/MemoryStick.h" +#include "Core/HW/MMIO.h" #ifndef MOBILE_DEVICE #include "Core/AVIDump.h" @@ -210,7 +211,7 @@ int g_screenshotFailures; if (gpu) gpu->FlushPendingDrawing(); - auto s = p.Section("SaveStart", 1, 3); + auto s = p.Section("SaveStart", 1, 4); if (!s) return; @@ -268,6 +269,11 @@ int g_screenshotFailures; Achievements::DoState(p); // Kernel object destructors might close open files, so do the filesystem last. pspFileSystem.DoState(p); + if (s >= 4) { + MMIO::DoState(p); + } else if (p.mode == p.MODE_READ) { + MMIO::Init(); + } if (p.mode == p.MODE_READ && p.error == PointerWrap::ERROR_FAILURE) { // RAM may be loaded and the JIT not told, or the other way round. Keep nothing. diff --git a/Core/System.cpp b/Core/System.cpp index c1f90a635b..1590da74b5 100644 --- a/Core/System.cpp +++ b/Core/System.cpp @@ -60,6 +60,7 @@ #include "Core/HLE/sceKernel.h" #include "Core/HLE/sceUtility.h" #include "Core/HW/Display.h" +#include "Core/HW/MMIO.h" #include "Core/Config.h" #include "Core/Core.h" #include "Core/Util/PathUtil.h" @@ -552,6 +553,7 @@ static bool CPU_Init(FileLoader *fileLoader, IdentifiedFileType type, std::strin mipsr4k.Reset(); CoreTiming::Init(&mipsr4k); + MMIO::Init(); DisplayHWInit(); @@ -676,6 +678,7 @@ void CPU_Shutdown(bool success) { Replacement_Shutdown(); CoreTiming::Shutdown(); + MMIO::Shutdown(); __KernelShutdown(); HLEShutdown(); diff --git a/UWP/CoreUWP/CoreUWP.vcxproj b/UWP/CoreUWP/CoreUWP.vcxproj index 4fafc73aac..cc57a3adcf 100644 --- a/UWP/CoreUWP/CoreUWP.vcxproj +++ b/UWP/CoreUWP/CoreUWP.vcxproj @@ -261,6 +261,7 @@ + @@ -556,6 +557,7 @@ + diff --git a/UWP/CoreUWP/CoreUWP.vcxproj.filters b/UWP/CoreUWP/CoreUWP.vcxproj.filters index bef5fe63c4..382b5f7d77 100644 --- a/UWP/CoreUWP/CoreUWP.vcxproj.filters +++ b/UWP/CoreUWP/CoreUWP.vcxproj.filters @@ -168,6 +168,7 @@ + @@ -584,6 +585,7 @@ + diff --git a/android/jni/Android.mk b/android/jni/Android.mk index c0dd81c0b8..d843963663 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -655,6 +655,7 @@ EXEC_AND_LIB_FILES := \ $(SRC)/Core/HW/Camera.cpp \ $(SRC)/Core/HW/Display.cpp \ $(SRC)/Core/HW/GpioMMIO.cpp \ + $(SRC)/Core/HW/MMIO.cpp \ $(SRC)/Core/HW/MemoryStick.cpp \ $(SRC)/Core/HW/MpegDemux.cpp.arm \ $(SRC)/Core/HW/AvcDecoder.cpp.arm \ diff --git a/libretro/Makefile.common b/libretro/Makefile.common index 8d58d198cd..ab5fc9171d 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -848,6 +848,7 @@ SOURCES_CXX += \ $(COREDIR)/HW/Camera.cpp \ $(COREDIR)/HW/Display.cpp \ $(COREDIR)/HW/GpioMMIO.cpp \ + $(COREDIR)/HW/MMIO.cpp \ $(COREDIR)/HW/SimpleAudioDec.cpp \ $(COREDIR)/HW/Atrac3Standalone.cpp \ $(COREDIR)/HW/AsyncIOManager.cpp \ diff --git a/unittest/JitHarness.cpp b/unittest/JitHarness.cpp index 4aa0d7af14..277bf0a0eb 100644 --- a/unittest/JitHarness.cpp +++ b/unittest/JitHarness.cpp @@ -44,8 +44,10 @@ #include "Core/Config.h" #include "Core/HLE/HLE.h" #include "Core/HLE/ReplaceTables.h" +#include "Core/HW/MMIO.h" #include "Core/MIPS/MIPSAnalyst.h" #include "Core/MIPS/JitCommon/JitState.h" +#include "Common/Serialize/Serializer.h" #if PPSSPP_ARCH(RISCV64) #include "Core/MIPS/RiscV/RiscVVfpuDot.h" #elif PPSSPP_ARCH(LOONGARCH64) @@ -510,6 +512,74 @@ bool TestCPUEret() { return success; } +// Kernel code reaching the GPIO block's registers on every core: a write, an acknowledge clearing a +// status bit, reads back through lw and lwc1, and poison from a register nothing handles. Then the +// registers through a savestate. +bool TestMMIO() { + SetupJitHarness(); + MMIO::Init(); + + // Kernel code, which may touch hardware registers. + const u32 base = 0x88000100; + auto sw = [](int rt, int rs, int imm) { return (0x2Bu << 26) | (rs << 21) | (rt << 16) | (imm & 0xFFFF); }; + auto lwc1 = [](int ft, int rs, int imm) { return (0x31u << 26) | (rs << 21) | (ft << 16) | (imm & 0xFFFF); }; + const u32 code[] = { + MIPS_MAKE_LUI(MIPS_REG_T0, 0xBE24), // GPIO + MIPS_MAKE_ORI(MIPS_REG_T1, MIPS_REG_ZERO, 0x30), + sw(MIPS_REG_T1, MIPS_REG_T0, 0x20), // Interrupt status. + MIPS_MAKE_ORI(MIPS_REG_T1, MIPS_REG_ZERO, 0x10), + sw(MIPS_REG_T1, MIPS_REG_T0, 0x24), // Acknowledge, clears the bit. + MIPS_MAKE_LW(MIPS_REG_V0, MIPS_REG_T0, 0x20), + lwc1(0, MIPS_REG_T0, 0x20), + MIPS_MAKE_LUI(MIPS_REG_T2, 0xBC10), + MIPS_MAKE_LW(MIPS_REG_V1, MIPS_REG_T2, 0), + MIPS_MAKE_SYSCALL("UnitTestFakeSyscalls", "UnitTestTerminator"), + MIPS_MAKE_BREAK(1), + }; + for (size_t i = 0; i < ARRAY_SIZE(code); ++i) + Memory::WriteUnchecked_U32(code[i], base + (u32)i * 4); + + static const CPUCore cores[] = { + CPUCore::INTERPRETER, + CPUCore::IR_INTERPRETER, + CPUCore::JIT, + }; + bool success = true; + for (CPUCore core : cores) { + mipsr4k.UpdateCore(core); + MMIO::Init(); + currentMIPS->r[MIPS_REG_V0] = 0; + currentMIPS->r[MIPS_REG_V1] = 0; + currentMIPS->fi[0] = 0; + currentMIPS->pc = base; + coreState = CORE_RUNNING_CPU; + while (coreState == CORE_RUNNING_CPU) + mipsr4k.RunLoopUntil(CoreTiming::GetTicks(currentMIPS) + 1000000); + + if (currentMIPS->r[MIPS_REG_V0] != 0x20 || currentMIPS->fi[0] != 0x20 || currentMIPS->r[MIPS_REG_V1] != 0x1337BEEF) { + printf("MMIO on core %d: v0=%08x f0=%08x v1=%08x, expected 00000020, 00000020, 1337beef\n", (int)core, + currentMIPS->r[MIPS_REG_V0], currentMIPS->fi[0], currentMIPS->r[MIPS_REG_V1]); + success = false; + } + } + mipsr4k.UpdateCore(CPUCore::INTERPRETER); + + struct MMIOState { + void DoState(PointerWrap &p) { MMIO::DoState(p); } + } state; + std::vector saved; + EXPECT_EQ_INT(CChunkFileReader::MeasureAndSavePtr(state, &saved), CChunkFileReader::ERROR_NONE); + MMIO::Init(); + std::string error; + EXPECT_EQ_INT(CChunkFileReader::LoadPtr(saved.data(), saved.size(), state, &error), CChunkFileReader::ERROR_NONE); + currentMIPS->pc = base; + EXPECT_EQ_HEX(MMIO::Read(currentMIPS, 0xBE240020, 4), 0x20); + + MMIO::Shutdown(); + DestroyJitHarness(); + return success; +} + // Differential fuzzing of the CPU cores. Random straight-line code (ALU, FPU, VFPU, loads and // stores) runs from a random register state on each core, and every core has to end up with the // interpreter's state, NaN payloads aside. diff --git a/unittest/JitHarness.h b/unittest/JitHarness.h index f17427396b..15df5a9b22 100644 --- a/unittest/JitHarness.h +++ b/unittest/JitHarness.h @@ -21,6 +21,7 @@ bool TestJit(); bool TestJitDotBench(); bool TestJitReplacementHooks(); bool TestCPUEret(); +bool TestMMIO(); bool TestCPUEquivalence(); bool TestCPURoundingMode(); bool TestVFPUDotJit(); diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index 050858e7ef..99fa1d92c3 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -3734,6 +3734,7 @@ TestItem availableTests[] = { TEST_ITEM(JitDotBench), TEST_ITEM(JitReplacementHooks), TEST_ITEM(CPUEret), + TEST_ITEM(MMIO), TEST_ITEM(CPUEquivalence), TEST_ITEM(CPURoundingMode), TEST_ITEM(VFPUDotJit),