// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include <cstdint>20#include <string>21#include <string_view>2223#include "Common/CommonTypes.h"2425class GraphicsContext;2627// For platforms that don't call Run28void Core_SetGraphicsContext(GraphicsContext *ctx);2930// Returns false when an UI exit state is detected.31void Core_Stop();3233// X11, sigh.34#ifdef None35#undef None36#endif3738enum class CPUStepType {39None,40Into,41Over,42Out,43Frame,44};4546// Must be set when breaking.47enum class BreakReason {48None,49AssertChoice,50DebugBreak,51DebugStep,52DebugStepInto,53UIFocus,54AfterFrame,55MemoryException,56CpuException,57BreakInstruction,58SavestateLoad,59SavestateSave,60SavestateRewind,61SavestateCrash,62MemoryBreakpoint,63CpuBreakpoint,64MemoryAccess, // ???65JitBranchDebug,66BreakOnBoot,67RABreak,68AddBreakpoint,69FrameAdvance,70UIPause,71HLEDebugBreak,72};73const char *BreakReasonToString(BreakReason reason);7475// Async, called from gui76void Core_Break(BreakReason reason, u32 relatedAddress = 0);7778// Resumes execution. Works both when stepping the CPU and the GE.79void Core_Resume();8081BreakReason Core_BreakReason();8283// This should be called externally.84// Can fail if another step type was requested this frame.85bool Core_RequestCPUStep(CPUStepType stepType, int stepSize);8687bool Core_NextFrame();88void Core_SwitchToGe(); // Switches from CPU emulation to GE display list execution.8990// Changes every time we enter stepping.91int Core_GetSteppingCounter();92struct SteppingReason {93BreakReason reason;94u32 relatedAddress = 0;95};96SteppingReason Core_GetSteppingReason();9798enum class CoreLifecycle {99STARTING,100// Note: includes failure cases. Guaranteed call after STARTING.101START_COMPLETE,102STOPPING,103// Guaranteed call after STOPPING.104STOPPED,105106// Sometimes called for save states. Guaranteed sequence, and never during STARTING or STOPPING.107MEMORY_REINITING,108MEMORY_REINITED,109};110111// RUNNING must be at 0, NEXTFRAME must be at 1.112enum CoreState {113// Emulation is running normally.114CORE_RUNNING_CPU = 0,115// Emulation was running normally, just reached the end of a frame.116CORE_NEXTFRAME = 1,117// Emulation is paused, CPU thread is sleeping.118CORE_STEPPING_CPU, // Can be used for recoverable runtime errors (ignored memory exceptions)119// Core is not running.120CORE_POWERDOWN,121// Unrecoverable runtime error. Recoverable errors should use CORE_STEPPING.122CORE_RUNTIME_ERROR,123// Stepping the GPU. When done, will switch over to STEPPING_CPU.124CORE_STEPPING_GE,125// Running the GPU. When done, will switch over to RUNNING_CPU.126CORE_RUNNING_GE,127};128const char *CoreStateToString(CoreState state);129130// Callback is called on the Emu thread.131typedef void (* CoreLifecycleFunc)(CoreLifecycle stage);132void Core_ListenLifecycle(CoreLifecycleFunc func);133void Core_NotifyLifecycle(CoreLifecycle stage);134135bool Core_IsStepping();136137bool Core_IsActive();138bool Core_IsInactive();139140// Warning: these three are only used on Windows - debugger integration.141void Core_StateProcessed();142void Core_WaitInactive();143144void Core_SetPowerSaving(bool mode);145bool Core_GetPowerSaving();146147void Core_RunLoopUntil(u64 globalticks);148149extern volatile CoreState coreState;150extern volatile bool coreStatePending;151152void Core_UpdateState(CoreState newState);153154enum class MemoryExceptionType {155NONE,156UNKNOWN,157READ_WORD,158WRITE_WORD,159READ_BLOCK,160WRITE_BLOCK,161ALIGNMENT,162};163enum class ExecExceptionType {164JUMP,165THREAD,166};167168// Separate one for without info, to avoid having to allocate a string169void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type);170171void Core_MemoryExceptionInfo(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type, std::string_view additionalInfo, bool forceReport);172173void Core_ExecException(u32 address, u32 pc, ExecExceptionType type);174void Core_BreakException(u32 pc);175// Call when loading save states, etc.176void Core_ResetException();177178enum class MIPSExceptionType {179NONE,180MEMORY,181BREAK,182BAD_EXEC_ADDR,183};184185struct MIPSExceptionInfo {186MIPSExceptionType type;187std::string info;188std::string stackTrace; // if available.189190// Memory exception info191MemoryExceptionType memory_type;192uint32_t pc;193uint32_t address;194uint32_t accessSize;195uint32_t ra = 0;196197// Reuses pc and address from memory type, where address is the failed destination.198ExecExceptionType exec_type;199};200201const MIPSExceptionInfo &Core_GetExceptionInfo();202203const char *ExceptionTypeAsString(MIPSExceptionType type);204const char *MemoryExceptionTypeAsString(MemoryExceptionType type);205const char *ExecExceptionTypeAsString(ExecExceptionType type);206207208