Files
ppsspp/Core/MIPS/MeCore.h
T
Henrik RydgårdandClaude Opus 5.5 caed1bc5f2 ME: Skip the rest of a slice the ME spends polling for the main CPU
Most homebrew ME code waits in a loop like lw t0, flag; beq t0, zero,
loop; nop: Daedalus/MElib/NooDS's mediaengine.prx, snes9xTYL, PMP Mod,
Yabause, FBA, the Touhou ports (with a few nops in it). Such a loop stores
nothing, has only plain loads, integer ALU ops, nops and branches, and
writes every register it reads before reading it in a pass, so every
pass goes the same way while memory stays the same - which it does while
the ME runs, since the main CPU doesn't. At the top of one, between
parts of a slice, the ME interprets a pass, and if it comes back to the
top, waits out the rest of the slice.

Loops that also store (a heartbeat counter) or span several blocks
aren't recognized, nor are delay loops counting down a register.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 11:07:37 +02:00

98 lines
3.4 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/.
// The Media Engine, the PSP's second Allegrex. The firmware runs its codecs on it, and homebrew
// whatever it likes. It runs in its own address space (Memory::CreateMESpace()), on the
// interpreter, a slice behind the main CPU: CoreTiming runs it for the cycles the main CPU has just
// used. Only emulated for homebrew, when enabled; commercial games get the HLE codecs.
#pragma once
#include <unordered_map>
#include "Common/CommonTypes.h"
#include "Core/MIPS/MIPS.h"
class PointerWrap;
namespace MeCore {
// The ME's interrupt controller's source for the main CPU's soft interrupt (0xBC100044).
enum : u32 {
ME_INTR_SOFT = 0x80000000,
};
struct MEState {
MIPSState cpu;
// Held in reset until the main CPU releases it.
bool inReset = true;
// Stopped by halt, until an interrupt it doesn't mask is raised.
bool halted = false;
// The ME's clock is tickBase - cpu.downcount.
s64 tickBase = 0;
// mfic/mtic's interrupt enable, which is per CPU.
u32 interruptEnable = 0;
// Its interrupt controller (0xBC300000): raised sources, and the ones that reach the CPU (IP2).
u32 intcRaised = 0;
u32 intcEnabled = 0;
// System registers (0xBC100000): those the ME code we run uses.
u32 resetFlags = 0; // 0x4C. Bit 2 holds the ME in reset.
u32 busClock = 0; // 0x50
u32 nmiFlags = 0; // 0x04
u32 ramConfig = 0; // 0x40. Bits 0-1: RAM size, 16 << n MB.
u32 mutex = 0; // 0x48. Bit 0 is the main CPU's, bit 1 the ME's.
u32 memProtect[20]{}; // 0xBC000000-0xBC00004C
// Not saved: whether the code at an address is a wait loop (see IsWaitLoop), and where it ends.
// 0 for not one. Cleared when the ME's code changes.
std::unordered_map<u32, u32> waitLoopEnds;
// Waiting for the main CPU, for the rest of this RunFor.
bool waiting = false;
};
// Null when the ME isn't emulated.
extern MEState *g_meState;
// Whether the config and this boot (homebrew) want the ME.
bool Wanted(bool homebrew);
// Creates the ME, in reset. Fails on platforms without an ME address space.
bool Init();
void Shutdown();
// Creates or removes the ME to match the state.
void DoState(PointerWrap &p);
// Runs the ME for the cycles the main CPU has just run.
void RunFor(int cycles);
// Asserting reset stops the ME. Releasing it starts the ME at the reset vector.
void SetReset(bool asserted);
// The ME's clock.
u64 GetTicks();
// COP0 registers with behaviour on the ME: Compare (11), Cause (13).
u32 ReadCop0(int reg);
void WriteCop0(int reg, u32 value);
void Halt();
// Raises sources in the ME's interrupt controller.
void RaiseInterrupt(u32 sources);
// The ME interrupts the main CPU (it wrote 0xBC100044).
void InterruptMainCPU();
} // namespace MeCore