Merge pull request #129 from wheremyfoodat/cheats

Add preliminary cheat support
This commit is contained in:
wheremyfoodat 2023-07-20 16:28:22 +03:00 committed by GitHub
commit 6dad0f05d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 310 additions and 16 deletions

43
include/action_replay.hpp Normal file
View file

@ -0,0 +1,43 @@
#pragma once
#include <array>
#include <vector>
#include "helpers.hpp"
#include "memory.hpp"
class ActionReplay {
using Cheat = std::vector<u32>; // A cheat is really just a bunch of 64-bit opcodes neatly encoded into 32-bit chunks
u32 offset1, offset2; // Memory offset registers. Non-persistent.
u32 data1, data2; // Data offset registers. Non-persistent.
u32 storage1, storage2; // Storage registers. Persistent.
// When an instruction does not specify which offset or data register to use, we use the "active" one
// Which is by default #1 and may be changed by certain AR operations
u32 *activeOffset, *activeData, *activeStorage;
// Program counter
u32 pc = 0;
Memory& mem;
// Has the cheat ended?
bool running = false;
// Run 1 AR instruction
void runInstruction(const Cheat& cheat, u32 instruction);
// Action Replay has a billion D-type opcodes so this handles all of them
void executeDType(const Cheat& cheat, u32 instruction);
u8 read8(u32 addr);
u16 read16(u32 addr);
u32 read32(u32 addr);
void write8(u32 addr, u8 value);
void write16(u32 addr, u16 value);
void write32(u32 addr, u32 value);
public:
ActionReplay(Memory& mem);
void runCheat(const Cheat& cheat);
void reset();
};

31
include/cheats.hpp Normal file
View file

@ -0,0 +1,31 @@
#pragma once
#include <array>
#include <vector>
#include "action_replay.hpp"
#include "helpers.hpp"
// Forward-declare this since it's just passed and we don't want to include memory.hpp and increase compile time
class Memory;
class Cheats {
public:
enum class CheatType {
ActionReplay, // CTRPF cheats
Gateway,
};
struct Cheat {
CheatType type;
std::vector<u32> instructions;
};
Cheats(Memory& mem);
void addCheat(const Cheat& cheat);
void reset();
void run();
private:
ActionReplay ar; // An ActionReplay cheat machine for executing CTRPF codes
std::vector<Cheat> cheats;
};

View file

@ -7,6 +7,7 @@
#include <optional>
#include "PICA/gpu.hpp"
#include "cheats.hpp"
#include "config.hpp"
#include "cpu.hpp"
#include "crypto/aes_engine.hpp"
@ -31,6 +32,7 @@ class Emulator {
Memory memory;
Kernel kernel;
Crypto::AESEngine aesEngine;
Cheats cheats;
SDL_Window* window;
@ -41,6 +43,9 @@ class Emulator {
SDL_GameController* gameController = nullptr;
int gameControllerID;
// Shows whether we've loaded any action replay codes
bool haveCheats = false;
// Variables to keep track of whether the user is controlling the 3DS analog stick with their keyboard
// This is done so when a gamepad is connected, we won't automatically override the 3DS analog stick settings with the gamepad's state
// And so the user can still use the keyboard to control the analog