mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-22 18:59:04 +02:00
Sysreg (0xBC100000) gets the ME's reset bit (0x4C bit 2), the soft interrupt (0x44, which interrupts the other CPU: the ME's controller from the main CPU, PSP_MECODEC_INTR from the ME), the hardware mutex (0x48, each CPU sets its own bit only while the other's is clear) and the clock and config registers it writes. The ME's interrupt controller (0xBC300000, write-1-to-clear, mask at 8) answers the ME only. All of it is the ME's state and stays unhandled without an ME. On the ME: interrupts to EBase with EPC and EXL, halt sleeping until an unmasked interrupt, and Count, which doesn't run: it stays 0 across 10ms with ERL set after reset and with Status cleared, and reads back what was written. Reset keeps the GPRs and sets Status ERL/BEV/SR, which homebrew relies on: mcidclan's samples run C on the stack Sony's ME code left. The values are from a kernel probe on a PSP-1000 (sp 0x801FFFE0, COP0 22 = 1, Status 0x20500404, mfic 0, which doesn't gate interrupts). The main CPU runs base interrupt handlers the way interruptman calls them (a0 intno, a1 arg, a2 interrupted pc; -2 disables), and interrupt 31 has a stand-in for me_wrapper's handler, which homebrew releases first. While the ME is emulated, any main CPU code may access hardware registers: homebrew calls its own code in kernel mode through a kcall module. This is a stopgap (g_kernelModeAnywhere, marked HACK): kernel mode should be part of the CPU's state, switched on while a kernel module's export runs. The memcpy/memmove replacements copy through MMIO when an end is a register address, for code copied to the boot page. mcidclan's me-soft-interrupt sample now runs, counting each interrupt in both directions. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
149 lines
4.5 KiB
C++
149 lines
4.5 KiB
C++
// 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 <cstring>
|
|
#include <vector>
|
|
|
|
#include "Common/Log.h"
|
|
#include "Common/Serialize/Serializer.h"
|
|
#include "Common/Serialize/SerializeFuncs.h"
|
|
#include "Core/HW/GpioMMIO.h"
|
|
#include "Core/HW/MMIO.h"
|
|
#include "Core/HW/MeMMIO.h"
|
|
#include "Core/MemMap.h"
|
|
#include "Core/MIPS/MIPS.h"
|
|
|
|
namespace MMIO {
|
|
|
|
// The boot page (see Memory::GetBootPage) is RAM, which the main CPU reaches through here.
|
|
class BootPageDevice : public MMIODevice {
|
|
public:
|
|
BootPageDevice() : MMIODevice("boot page", Memory::BOOT_PAGE_ADDRESS, Memory::BOOT_PAGE_SIZE) {}
|
|
|
|
bool Read(MIPSState *mips, u32 offset, int size, u32 *value) override {
|
|
if (offset + size > Memory::BOOT_PAGE_SIZE)
|
|
return false;
|
|
const u8 *ptr = Memory::GetBootPage() + offset;
|
|
switch (size) {
|
|
case 1: *value = *ptr; break;
|
|
case 2: *value = *(const u16_le *)ptr; break;
|
|
default: *value = *(const u32_le *)ptr; break;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Write(MIPSState *mips, u32 offset, int size, u32 value) override {
|
|
if (offset + size > Memory::BOOT_PAGE_SIZE)
|
|
return false;
|
|
u8 *ptr = Memory::GetBootPage() + offset;
|
|
switch (size) {
|
|
case 1: *ptr = (u8)value; break;
|
|
case 2: *(u16_le *)ptr = (u16)value; break;
|
|
default: *(u32_le *)ptr = value; break;
|
|
}
|
|
// The ME runs code from here, uncached, so it sees new code at once (MIST patches the
|
|
// bootstrap, me/vram puts it back after its own stub). Reaches the ME's JIT too.
|
|
if (mips)
|
|
mips->InvalidateICacheRangeDeferred(Memory::BOOT_PAGE_ADDRESS + offset, size);
|
|
return true;
|
|
}
|
|
|
|
// Both CPUs reset to the start of the page. The firmware leaves code there that sends the ME
|
|
// (whose COP0 register 22 is nonzero) on to 0x40, where homebrew puts its ME code (read on a
|
|
// PSP-1000 under PSPLink). The main CPU's half isn't needed, it never resets under HLE.
|
|
void Reset() override {
|
|
u8 *page = Memory::GetBootPage();
|
|
memset(page, 0, Memory::BOOT_PAGE_SIZE);
|
|
const u32 code[] = {
|
|
0x4002B000, // mfc0 v0, $22
|
|
0x1440000E, // bnez v0, 0xBFC00040
|
|
0x00000000, // nop
|
|
0x1000FFFF, // b .
|
|
0x00000000, // nop
|
|
};
|
|
for (size_t i = 0; i < ARRAY_SIZE(code); ++i) {
|
|
((u32_le *)page)[i] = code[i];
|
|
}
|
|
}
|
|
|
|
void DoState(PointerWrap &p) override {
|
|
DoArray(p, Memory::GetBootPage(), Memory::BOOT_PAGE_SIZE);
|
|
}
|
|
};
|
|
|
|
static BootPageDevice g_bootPage;
|
|
|
|
static std::vector<MMIODevice *> g_devices;
|
|
|
|
void Init() {
|
|
g_devices = {
|
|
GpioMMIO::Device(),
|
|
SysconSerialMMIO::Device(),
|
|
&g_bootPage,
|
|
MeMMIO::SysregDevice(),
|
|
MeMMIO::InterruptDevice(),
|
|
MeMMIO::MemProtectDevice(),
|
|
};
|
|
for (MMIODevice *device : g_devices) {
|
|
device->Reset();
|
|
}
|
|
}
|
|
|
|
void Shutdown() {
|
|
g_devices.clear();
|
|
}
|
|
|
|
void DoState(PointerWrap &p) {
|
|
auto s = p.Section("MMIO", 1, 2);
|
|
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);
|
|
if (s >= 2) {
|
|
g_bootPage.DoState(p);
|
|
} else if (p.mode == PointerWrap::MODE_READ) {
|
|
g_bootPage.Reset();
|
|
}
|
|
}
|
|
|
|
static MMIODevice *FindDevice(u32 address) {
|
|
for (MMIODevice *device : g_devices) {
|
|
if (device->Contains(address))
|
|
return device;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
u32 Read(MIPSState *mips, u32 address, int size) {
|
|
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 UNHANDLED_READ;
|
|
}
|
|
|
|
void Write(MIPSState *mips, u32 address, int size, u32 value) {
|
|
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
|