2019-02-28 02:33:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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 for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
2019-03-04 09:41:01 +00:00
|
|
|
#include <stdarg.h>
|
2019-02-28 02:33:07 +00:00
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
#include "dmnt_cheat_types.hpp"
|
|
|
|
|
|
|
|
enum CheatVmOpcodeType : u32 {
|
|
|
|
CheatVmOpcodeType_StoreStatic = 0,
|
|
|
|
CheatVmOpcodeType_BeginConditionalBlock = 1,
|
|
|
|
CheatVmOpcodeType_EndConditionalBlock = 2,
|
|
|
|
CheatVmOpcodeType_ControlLoop = 3,
|
|
|
|
CheatVmOpcodeType_LoadRegisterStatic = 4,
|
|
|
|
CheatVmOpcodeType_LoadRegisterMemory = 5,
|
2019-03-05 09:20:18 +00:00
|
|
|
CheatVmOpcodeType_StoreStaticToAddress = 6,
|
2019-02-28 02:46:53 +00:00
|
|
|
CheatVmOpcodeType_PerformArithmeticStatic = 7,
|
2019-02-28 02:33:07 +00:00
|
|
|
CheatVmOpcodeType_BeginKeypressConditionalBlock = 8,
|
2019-02-28 03:30:29 +00:00
|
|
|
|
|
|
|
/* These are not implemented by Gateway's VM. */
|
|
|
|
CheatVmOpcodeType_PerformArithmeticRegister = 9,
|
2019-03-05 09:20:18 +00:00
|
|
|
CheatVmOpcodeType_StoreRegisterToAddress = 10,
|
2019-03-15 20:45:35 +00:00
|
|
|
CheatVmOpcodeType_Reserved11 = 11,
|
2019-03-05 08:53:45 +00:00
|
|
|
|
|
|
|
/* This is a meta entry, and not a real opcode. */
|
2019-03-15 20:45:35 +00:00
|
|
|
/* This is to facilitate multi-nybble instruction decoding. */
|
2019-03-05 08:53:45 +00:00
|
|
|
CheatVmOpcodeType_ExtendedWidth = 12,
|
2019-03-15 20:45:35 +00:00
|
|
|
|
|
|
|
/* Extended width opcodes. */
|
|
|
|
CheatVmOpcodeType_BeginRegisterConditionalBlock = 0xC0,
|
2019-02-28 02:33:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum MemoryAccessType : u32 {
|
|
|
|
MemoryAccessType_MainNso = 0,
|
|
|
|
MemoryAccessType_Heap = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ConditionalComparisonType : u32 {
|
|
|
|
ConditionalComparisonType_GT = 1,
|
|
|
|
ConditionalComparisonType_GE = 2,
|
|
|
|
ConditionalComparisonType_LT = 3,
|
|
|
|
ConditionalComparisonType_LE = 4,
|
|
|
|
ConditionalComparisonType_EQ = 5,
|
|
|
|
ConditionalComparisonType_NE = 6,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum RegisterArithmeticType : u32 {
|
|
|
|
RegisterArithmeticType_Addition = 0,
|
|
|
|
RegisterArithmeticType_Subtraction = 1,
|
|
|
|
RegisterArithmeticType_Multiplication = 2,
|
|
|
|
RegisterArithmeticType_LeftShift = 3,
|
|
|
|
RegisterArithmeticType_RightShift = 4,
|
2019-02-28 03:30:29 +00:00
|
|
|
|
|
|
|
/* These are not supported by Gateway's VM. */
|
|
|
|
RegisterArithmeticType_LogicalAnd = 5,
|
|
|
|
RegisterArithmeticType_LogicalOr = 6,
|
|
|
|
RegisterArithmeticType_LogicalNot = 7,
|
|
|
|
RegisterArithmeticType_LogicalXor = 8,
|
|
|
|
|
|
|
|
RegisterArithmeticType_None = 9,
|
2019-02-28 02:33:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-05 09:20:18 +00:00
|
|
|
enum StoreRegisterOffsetType : u32 {
|
|
|
|
StoreRegisterOffsetType_None = 0,
|
|
|
|
StoreRegisterOffsetType_Reg = 1,
|
|
|
|
StoreRegisterOffsetType_Imm = 2,
|
|
|
|
};
|
|
|
|
|
2019-03-15 20:45:35 +00:00
|
|
|
enum CompareRegisterValueType : u32 {
|
|
|
|
CompareRegisterValueType_MemoryRelAddr = 0,
|
|
|
|
CompareRegisterValueType_MemoryOfsReg = 1,
|
|
|
|
CompareRegisterValueType_RegisterRelAddr = 2,
|
|
|
|
CompareRegisterValueType_RegisterOfsReg = 3,
|
|
|
|
CompareRegisterValueType_StaticValue = 4,
|
|
|
|
};
|
|
|
|
|
2019-02-28 02:33:07 +00:00
|
|
|
union VmInt {
|
|
|
|
u8 bit8;
|
|
|
|
u16 bit16;
|
|
|
|
u32 bit32;
|
|
|
|
u64 bit64;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StoreStaticOpcode {
|
|
|
|
u32 bit_width;
|
|
|
|
MemoryAccessType mem_type;
|
|
|
|
u32 offset_register;
|
2019-03-03 14:29:48 +00:00
|
|
|
u64 rel_address;
|
2019-02-28 02:33:07 +00:00
|
|
|
VmInt value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BeginConditionalOpcode {
|
|
|
|
u32 bit_width;
|
|
|
|
MemoryAccessType mem_type;
|
|
|
|
ConditionalComparisonType cond_type;
|
2019-03-03 14:29:48 +00:00
|
|
|
u64 rel_address;
|
2019-02-28 02:33:07 +00:00
|
|
|
VmInt value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EndConditionalOpcode {};
|
|
|
|
|
|
|
|
struct ControlLoopOpcode {
|
|
|
|
bool start_loop;
|
2019-02-28 02:46:53 +00:00
|
|
|
u32 reg_index;
|
2019-02-28 02:33:07 +00:00
|
|
|
u32 num_iters;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LoadRegisterStaticOpcode {
|
2019-02-28 02:46:53 +00:00
|
|
|
u32 reg_index;
|
2019-02-28 02:33:07 +00:00
|
|
|
u64 value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LoadRegisterMemoryOpcode {
|
|
|
|
u32 bit_width;
|
|
|
|
MemoryAccessType mem_type;
|
|
|
|
u32 reg_index;
|
|
|
|
bool load_from_reg;
|
2019-03-03 14:29:48 +00:00
|
|
|
u64 rel_address;
|
2019-02-28 02:33:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-05 09:20:18 +00:00
|
|
|
struct StoreStaticToAddressOpcode {
|
2019-02-28 02:33:07 +00:00
|
|
|
u32 bit_width;
|
|
|
|
u32 reg_index;
|
|
|
|
bool increment_reg;
|
|
|
|
bool add_offset_reg;
|
|
|
|
u32 offset_reg_index;
|
|
|
|
u64 value;
|
|
|
|
};
|
|
|
|
|
2019-02-28 02:46:53 +00:00
|
|
|
struct PerformArithmeticStaticOpcode {
|
2019-02-28 02:33:07 +00:00
|
|
|
u32 bit_width;
|
|
|
|
u32 reg_index;
|
|
|
|
RegisterArithmeticType math_type;
|
2019-02-28 02:46:53 +00:00
|
|
|
u32 value;
|
2019-02-28 02:33:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BeginKeypressConditionalOpcode {
|
|
|
|
u32 key_mask;
|
|
|
|
};
|
|
|
|
|
2019-02-28 03:30:29 +00:00
|
|
|
struct PerformArithmeticRegisterOpcode {
|
|
|
|
u32 bit_width;
|
|
|
|
RegisterArithmeticType math_type;
|
|
|
|
u32 dst_reg_index;
|
|
|
|
u32 src_reg_1_index;
|
|
|
|
u32 src_reg_2_index;
|
|
|
|
bool has_immediate;
|
|
|
|
VmInt value;
|
|
|
|
};
|
|
|
|
|
2019-03-05 09:20:18 +00:00
|
|
|
struct StoreRegisterToAddressOpcode {
|
|
|
|
u32 bit_width;
|
|
|
|
u32 str_reg_index;
|
|
|
|
u32 addr_reg_index;
|
|
|
|
bool increment_reg;
|
|
|
|
StoreRegisterOffsetType ofs_type;
|
|
|
|
u32 ofs_reg_index;
|
|
|
|
u64 rel_address;
|
|
|
|
};
|
|
|
|
|
2019-03-15 20:45:35 +00:00
|
|
|
struct BeginRegisterConditionalOpcode {
|
|
|
|
u32 bit_width;
|
|
|
|
ConditionalComparisonType cond_type;
|
|
|
|
u32 val_reg_index;
|
|
|
|
CompareRegisterValueType comp_type;
|
|
|
|
MemoryAccessType mem_type;
|
|
|
|
u32 addr_reg_index;
|
|
|
|
u32 ofs_reg_index;
|
|
|
|
u64 rel_address;
|
|
|
|
VmInt value;
|
|
|
|
};
|
|
|
|
|
2019-03-05 09:20:18 +00:00
|
|
|
|
2019-02-28 02:33:07 +00:00
|
|
|
struct CheatVmOpcode {
|
|
|
|
CheatVmOpcodeType opcode;
|
2019-03-05 09:39:20 +00:00
|
|
|
bool begin_conditional_block;
|
2019-02-28 02:33:07 +00:00
|
|
|
union {
|
|
|
|
StoreStaticOpcode store_static;
|
|
|
|
BeginConditionalOpcode begin_cond;
|
|
|
|
EndConditionalOpcode end_cond;
|
|
|
|
ControlLoopOpcode ctrl_loop;
|
|
|
|
LoadRegisterStaticOpcode ldr_static;
|
|
|
|
LoadRegisterMemoryOpcode ldr_memory;
|
2019-03-05 09:20:18 +00:00
|
|
|
StoreStaticToAddressOpcode str_static;
|
2019-02-28 02:46:53 +00:00
|
|
|
PerformArithmeticStaticOpcode perform_math_static;
|
2019-02-28 02:33:07 +00:00
|
|
|
BeginKeypressConditionalOpcode begin_keypress_cond;
|
2019-02-28 03:30:29 +00:00
|
|
|
PerformArithmeticRegisterOpcode perform_math_reg;
|
2019-03-05 09:20:18 +00:00
|
|
|
StoreRegisterToAddressOpcode str_register;
|
2019-03-15 20:45:35 +00:00
|
|
|
BeginRegisterConditionalOpcode begin_reg_cond;
|
2019-02-28 02:33:07 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
class DmntCheatVm {
|
|
|
|
public:
|
|
|
|
constexpr static size_t MaximumProgramOpcodeCount = 0x400;
|
|
|
|
constexpr static size_t NumRegisters = 0x10;
|
|
|
|
private:
|
|
|
|
size_t num_opcodes = 0;
|
|
|
|
size_t instruction_ptr = 0;
|
2019-03-05 09:39:20 +00:00
|
|
|
size_t condition_depth = 0;
|
2019-03-03 14:44:21 +00:00
|
|
|
bool decode_success = false;
|
2019-02-28 02:33:07 +00:00
|
|
|
u32 program[MaximumProgramOpcodeCount] = {0};
|
|
|
|
u64 registers[NumRegisters] = {0};
|
|
|
|
size_t loop_tops[NumRegisters] = {0};
|
|
|
|
private:
|
|
|
|
bool DecodeNextOpcode(CheatVmOpcode *out);
|
|
|
|
void SkipConditionalBlock();
|
2019-03-03 14:44:21 +00:00
|
|
|
void ResetState();
|
2019-02-28 03:30:29 +00:00
|
|
|
|
2019-03-04 09:41:01 +00:00
|
|
|
/* For debugging. These will be IFDEF'd out normally. */
|
|
|
|
void OpenDebugLogFile();
|
|
|
|
void CloseDebugLogFile();
|
|
|
|
void LogToDebugFile(const char *format, ...);
|
|
|
|
void LogOpcode(const CheatVmOpcode *opcode);
|
|
|
|
|
2019-02-28 03:30:29 +00:00
|
|
|
static u64 GetVmInt(VmInt value, u32 bit_width);
|
2019-03-03 14:29:48 +00:00
|
|
|
static u64 GetCheatProcessAddress(const CheatProcessMetadata* metadata, MemoryAccessType mem_type, u64 rel_address);
|
2019-02-28 02:33:07 +00:00
|
|
|
public:
|
|
|
|
DmntCheatVm() { }
|
|
|
|
|
|
|
|
size_t GetProgramSize() {
|
|
|
|
return this->num_opcodes;
|
|
|
|
}
|
|
|
|
|
2019-03-05 04:13:52 +00:00
|
|
|
bool LoadProgram(const CheatEntry *cheats, size_t num_cheats);
|
2019-02-28 02:33:07 +00:00
|
|
|
void Execute(const CheatProcessMetadata *metadata);
|
2019-03-04 09:41:01 +00:00
|
|
|
#ifdef DMNT_CHEAT_VM_DEBUG_LOG
|
|
|
|
private:
|
|
|
|
FILE *debug_log_file = NULL;
|
|
|
|
#endif
|
2019-02-28 02:33:07 +00:00
|
|
|
};
|