// 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/.161718// Regular replacement funcs are just C functions. These take care of their19// own parameter parsing using the old school PARAM macros.20// The return value is the number of cycles to eat.2122// Replacement functions are replaced by function hash, also checking the size to reduce23// collisions. This is not really super safe, and we probably should restrict them by24// game ID, really...2526// JIT replacefuncs can be for inline or "outline" replacement.27// With inline replacement, we recognize the call to the functions28// at jal time already. With outline replacement, we just replace the29// implementation, which gets jumped to from other functions.3031// In both cases the jit needs to know how much to subtract downcount.32//33// If the replacement func returned a positive number, this will be treated34// as the number of cycles to subtract.35// If the replacement func returns -1, it will be assumed that the subtraction36// was done by the replacement func.3738#pragma once3940#include <map>4142#include "Common/CommonTypes.h"43#include "Core/MIPS/JitCommon/JitCommon.h"4445typedef int (* ReplaceFunc)();4647enum {48REPFLAG_ALLOWINLINE = 0x01,49// Used to keep things around but disable them.50REPFLAG_DISABLED = 0x02,51// Note that this will re-execute in a function that loops at start.52REPFLAG_HOOKENTER = 0x04,53// Only hooks jr ra, so only use on funcs that have that.54REPFLAG_HOOKEXIT = 0x08,55// Function may take a lot of time and execute in slices (executed multiple times.)56REPFLAG_SLICED = 0x10,57};5859// Kind of similar to HLE functions but with different data.60struct ReplacementTableEntry {61const char *name;62ReplaceFunc replaceFunc;63MIPSComp::MIPSReplaceFunc jitReplaceFunc;64int flags;65s32 hookOffset;66};6768void Replacement_Init();69void Replacement_Shutdown();7071int GetNumReplacementFuncs();72std::vector<int> GetReplacementFuncIndexes(u64 hash, int funcSize);73const ReplacementTableEntry *GetReplacementFunc(size_t index);7475void WriteReplaceInstructions(u32 address, u64 hash, int size);76void RestoreReplacedInstruction(u32 address);77void RestoreReplacedInstructions(u32 startAddr, u32 endAddr);78bool GetReplacedOpAt(u32 address, u32 *op);79bool CanReplaceJalTo(u32 dest, const ReplacementTableEntry **entry, u32 *funcSize);8081// For savestates. If you call SaveAndClearReplacements(), you must call RestoreSavedReplacements().82std::map<u32, u32> SaveAndClearReplacements();83void RestoreSavedReplacements(const std::map<u32, u32> &saved);848586