src/gba/cheats.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef GBA_CHEATS_H
7#define GBA_CHEATS_H
8
9#include "util/common.h"
10
11#include "arm.h"
12#include "util/vector.h"
13
14#define MAX_ROM_PATCHES 4
15
16enum GBACheatType {
17 CHEAT_ASSIGN,
18 CHEAT_AND,
19 CHEAT_ADD,
20 CHEAT_OR,
21 CHEAT_IF_EQ,
22 CHEAT_IF_NE,
23 CHEAT_IF_LT,
24 CHEAT_IF_GT,
25 CHEAT_IF_ULT,
26 CHEAT_IF_UGT,
27 CHEAT_IF_AND,
28 CHEAT_IF_LAND
29};
30
31enum GBACodeBreakerType {
32 CB_GAME_ID = 0x0,
33 CB_HOOK = 0x1,
34 CB_OR_2 = 0x2,
35 CB_ASSIGN_1 = 0x3,
36 CB_FILL = 0x4,
37 CB_FILL_8 = 0x5,
38 CB_AND_2 = 0x6,
39 CB_IF_EQ = 0x7,
40 CB_ASSIGN_2 = 0x8,
41 CB_ENCRYPT = 0x9,
42 CB_IF_NE = 0xA,
43 CB_IF_GT = 0xB,
44 CB_IF_LT = 0xC,
45 CB_IF_SPECIAL = 0xD,
46 CB_ADD_2 = 0xE,
47 CB_IF_AND = 0xF,
48};
49
50enum GBAGameSharkType {
51 GSA_ASSIGN_1 = 0x0,
52 GSA_ASSIGN_2 = 0x1,
53 GSA_ASSIGN_4 = 0x2,
54 GSA_ASSIGN_LIST = 0x3,
55 GSA_PATCH = 0x6,
56 GSA_BUTTON = 0x8,
57 GSA_IF_EQ = 0xD,
58 GSA_IF_EQ_RANGE = 0xE,
59 GSA_HOOK = 0xF
60};
61
62enum GBAActionReplay3Condition {
63 PAR3_COND_OTHER = 0x00000000,
64 PAR3_COND_EQ = 0x08000000,
65 PAR3_COND_NE = 0x10000000,
66 PAR3_COND_LT = 0x18000000,
67 PAR3_COND_GT = 0x20000000,
68 PAR3_COND_ULT = 0x28000000,
69 PAR3_COND_UGT = 0x30000000,
70 PAR3_COND_LAND = 0x38000000,
71};
72
73enum GBAActionReplay3Width {
74 PAR3_WIDTH_1 = 0x00000000,
75 PAR3_WIDTH_2 = 0x02000000,
76 PAR3_WIDTH_4 = 0x04000000,
77 PAR3_WIDTH_FALSE = 0x06000000,
78};
79
80enum GBAActionReplay3Action {
81 PAR3_ACTION_NEXT = 0x00000000,
82 PAR3_ACTION_NEXT_TWO = 0x40000000,
83 PAR3_ACTION_BLOCK = 0x80000000,
84 PAR3_ACTION_DISABLE = 0xC0000000,
85};
86
87enum GBAActionReplay3Base {
88 PAR3_BASE_ASSIGN_1 = 0x00000000,
89 PAR3_BASE_ASSIGN_2 = 0x02000000,
90 PAR3_BASE_ASSIGN_4 = 0x04000000,
91 PAR3_BASE_INDIRECT_1 = 0x40000000,
92 PAR3_BASE_INDIRECT_2 = 0x42000000,
93 PAR3_BASE_INDIRECT_4 = 0x44000000,
94 PAR3_BASE_ADD_1 = 0x80000000,
95 PAR3_BASE_ADD_2 = 0x82000000,
96 PAR3_BASE_ADD_4 = 0x84000000,
97 PAR3_BASE_HOOK = 0xC4000000,
98 PAR3_BASE_IO_2 = 0xC6000000,
99 PAR3_BASE_IO_3 = 0xC7000000,
100};
101
102enum GBAActionReplay3Other {
103 PAR3_OTHER_END = 0x00000000,
104 PAR3_OTHER_SLOWDOWN = 0x08000000,
105 PAR3_OTHER_BUTTON_1 = 0x10000000,
106 PAR3_OTHER_BUTTON_2 = 0x12000000,
107 PAR3_OTHER_BUTTON_4 = 0x14000000,
108 PAR3_OTHER_PATCH_1 = 0x18000000,
109 PAR3_OTHER_PATCH_2 = 0x1A000000,
110 PAR3_OTHER_PATCH_3 = 0x1C000000,
111 PAR3_OTHER_PATCH_4 = 0x1E000000,
112 PAR3_OTHER_ENDIF = 0x40000000,
113 PAR3_OTHER_ELSE = 0x60000000,
114 PAR3_OTHER_FILL_1 = 0x80000000,
115 PAR3_OTHER_FILL_2 = 0x82000000,
116 PAR3_OTHER_FILL_4 = 0x84000000,
117};
118
119enum {
120 PAR3_COND = 0x38000000,
121 PAR3_WIDTH = 0x06000000,
122 PAR3_ACTION = 0xC0000000
123};
124
125struct GBACheat {
126 enum GBACheatType type;
127 int width;
128 uint32_t address;
129 uint32_t operand;
130 uint32_t repeat;
131
132 int32_t addressOffset;
133 int32_t operandOffset;
134};
135
136struct GBACheatHook {
137 uint32_t address;
138 enum ExecutionMode mode;
139 uint32_t patchedOpcode;
140 size_t refs;
141 size_t reentries;
142};
143
144DECLARE_VECTOR(GBACheatList, struct GBACheat);
145
146struct GBACheatSet {
147 struct GBACheatHook* hook;
148 struct GBACheatList list;
149
150 struct GBACheat* incompleteCheat;
151
152 struct GBACheatPatch {
153 uint32_t address;
154 int16_t newValue;
155 int16_t oldValue;
156 bool applied;
157 bool exists;
158 } romPatches[MAX_ROM_PATCHES];
159
160 int gsaVersion;
161 uint32_t gsaSeeds[4];
162 int remainingAddresses;
163
164 char* name;
165};
166
167DECLARE_VECTOR(GBACheatSets, struct GBACheatSet*);
168
169struct GBACheatDevice {
170 struct ARMComponent d;
171 struct GBA* p;
172
173 struct GBACheatSets cheats;
174};
175
176struct VFile;
177
178void GBACheatDeviceCreate(struct GBACheatDevice*);
179void GBACheatDeviceDestroy(struct GBACheatDevice*);
180
181void GBACheatSetInit(struct GBACheatSet*, const char* name);
182void GBACheatSetDeinit(struct GBACheatSet*);
183
184void GBACheatAttachDevice(struct GBA* gba, struct GBACheatDevice*);
185
186void GBACheatAddSet(struct GBACheatDevice*, struct GBACheatSet*);
187void GBACheatRemoveSet(struct GBACheatDevice*, struct GBACheatSet*);
188
189bool GBACheatAddCodeBreaker(struct GBACheatSet*, uint32_t op1, uint16_t op2);
190bool GBACheatAddCodeBreakerLine(struct GBACheatSet*, const char* line);
191
192bool GBACheatAddGameShark(struct GBACheatSet*, uint32_t op1, uint32_t op2);
193bool GBACheatAddGameSharkLine(struct GBACheatSet*, const char* line);
194
195bool GBACheatAddAutodetect(struct GBACheatSet*, uint32_t op1, uint32_t op2);
196bool GBACheatAddAutodetectLine(struct GBACheatSet*, const char* line);
197
198bool GBACheatParseFile(struct GBACheatDevice*, struct VFile*);
199bool GBACheatAddLine(struct GBACheatSet*, const char* line);
200
201void GBACheatRefresh(struct GBACheatDevice*, struct GBACheatSet*);
202
203#endif