Files
ppsspp/Core/HLE/sceKernelInterrupt.h
T
Henrik RydgårdandClaude Opus 5.5 e52cc38866 ME: Reset, interrupts and the other registers homebrew runs the ME with
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>
2026-10-21 09:33:26 +02:00

186 lines
5.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/.
#pragma once
#include <map>
class PointerWrap;
class MIPSState;
enum PSPInterrupt {
PSP_GPIO_INTR = 4,
PSP_ATA_INTR = 5,
PSP_UMD_INTR = 6,
PSP_MSCM0_INTR = 7,
PSP_WLAN_INTR = 8,
PSP_AUDIO_INTR = 10,
PSP_I2C_INTR = 12,
PSP_SIRS_INTR = 14,
PSP_SYSTIMER0_INTR = 15,
PSP_SYSTIMER1_INTR = 16,
PSP_SYSTIMER2_INTR = 17,
PSP_SYSTIMER3_INTR = 18,
PSP_THREAD0_INTR = 19,
PSP_NAND_INTR = 20,
PSP_DMACPLUS_INTR = 21,
PSP_DMA0_INTR = 22,
PSP_DMA1_INTR = 23,
PSP_MEMLMD_INTR = 24,
PSP_GE_INTR = 25,
PSP_VBLANK_INTR = 30,
PSP_MECODEC_INTR = 31,
PSP_HPREMOTE_INTR = 36,
PSP_MSCM1_INTR = 60,
PSP_MSCM2_INTR = 61,
PSP_THREAD1_INTR = 65,
PSP_INTERRUPT_INTR = 66,
PSP_NUMBER_INTERRUPTS = 67
};
// These are invented for PPSSPP:
enum PSPGeSubInterrupts {
PSP_GE_SUBINTR_SIGNAL = 0,
PSP_GE_SUBINTR_FINISH = 1,
};
enum PSPInterruptTriggerType {
// Trigger immediately, for CoreTiming events.
PSP_INTR_IMMEDIATE = 0x0,
// Trigger after the HLE syscall finishes.
PSP_INTR_HLE = 0x1,
// Only trigger (as above) if interrupts are not suspended.
PSP_INTR_ONLY_IF_ENABLED = 0x2,
// Always reschedule, even if there's no handler registered.
// TODO: Maybe this should just always do this? Not sure.
PSP_INTR_ALWAYS_RESCHED = 0x4,
};
enum PSPSubInterruptTriggerType {
// Trigger all sub intr
PSP_INTR_SUB_ALL = -2,
// Trigger code at the interrupt handler level
PSP_INTR_SUB_NONE = -1,
// Trigger specific sub interrupt
PSP_INTR_SUB_NUMBER = 0,
};
struct PendingInterrupt {
PendingInterrupt(int intr_, int subintr_)
: intr(intr_), subintr(subintr_) {}
void DoState(PointerWrap &p);
int intr;
int subintr;
};
struct SubIntrHandler
{
bool enabled;
int intrNumber;
int subIntrNumber;
u32 handlerAddress;
u32 handlerArg;
};
class IntrHandler
{
public:
IntrHandler(int intrNumber_)
: intrNumber(intrNumber_)
{
}
virtual ~IntrHandler() {}
virtual bool run(PendingInterrupt& pend);
virtual void copyArgsToCPU(PendingInterrupt& pend);
virtual void handleResult(PendingInterrupt& pend);
// What the kernel spends around a handler: before it runs, and after the last one queued for
// this interrupt returns.
int entryCycles() const { return entryCycles_; }
int exitCycles() const { return exitCycles_; }
void setCosts(int entryCycles, int exitCycles) {
entryCycles_ = entryCycles;
exitCycles_ = exitCycles;
}
// Returns how many handlers were queued.
int queueUp(int subintr);
SubIntrHandler* add(int subIntrNum);
void remove(int subIntrNum);
bool has(int subIntrNum) const;
void enable(int subIntrNum);
void disable(int subIntrNum);
SubIntrHandler *get(int subIntrNum);
void clear();
// The handler a kernel driver registered for the whole interrupt (sceKernelRegisterIntrHandler),
// raised with PSP_INTR_SUB_NONE. Only the Media Engine raises one so far.
bool hasBaseHandler() const { return baseHandler_ != 0; }
void setBaseHandler(u32 handler, u32 arg) {
baseHandler_ = handler;
baseArg_ = arg;
baseEnabled_ = false;
}
void enableBase(bool enable) { baseEnabled_ = enable; }
// Stands in for a firmware driver's handler, which homebrew releases to register its own.
static constexpr u32 FIRMWARE_HANDLER = 1;
void DoState(PointerWrap &p);
private:
int intrNumber;
std::map<int, SubIntrHandler> subIntrHandlers;
u32 baseHandler_ = 0;
u32 baseArg_ = 0;
bool baseEnabled_ = false;
int entryCycles_ = 0;
int exitCycles_ = 0;
};
void __DisableInterrupts();
void __EnableInterrupts();
bool __InterruptsEnabled();
bool __IsInInterrupt();
void __InterruptsInit();
void __InterruptsDoState(PointerWrap &p);
void __InterruptsDoStateLate(PointerWrap &p);
void __InterruptsShutdown();
// Returns how many handlers were queued.
int __TriggerInterrupt(int type, PSPInterrupt intno, int subInterrupts = -1);
// Forgets interrupts that have been raised but not taken, except for one that's being handled. Returns how many.
int __CancelRaisedInterrupts(PSPInterrupt intno);
bool __RunOnePendingInterrupt();
void __KernelReturnFromInterrupt(MIPSState *mips);
void __RegisterIntrHandler(u32 intrNumber, IntrHandler* handler);
void __SetIntrHandlerCosts(PSPInterrupt intno, int entryCycles, int exitCycles);
SubIntrHandler *__RegisterSubIntrHandler(u32 intrNumber, u32 subIntrNumber, u32 handler, u32 handlerArg, u32 &error);
int __ReleaseSubIntrHandler(int intrNumber, int subIntrNumber);
u32 sceKernelRegisterSubIntrHandler(u32 intrNumber, u32 subIntrNumber, u32 handler, u32 handlerArg);
u32 sceKernelReleaseSubIntrHandler(u32 intrNumber, u32 subIntrNumber);
u32 sceKernelEnableSubIntr(u32 intrNumber, u32 subIntrNumber);
void Register_Kernel_Library();
void Register_InterruptManager();
void Register_SysclibForKernel();
void Register_InterruptManagerForKernel();