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/arm.h"
12#include "core/log.h"
13#include "util/vector.h"
14
15#define MAX_ROM_PATCHES 4
16
17enum GBACheatType {
18 CHEAT_ASSIGN,
19 CHEAT_ASSIGN_INDIRECT,
20 CHEAT_AND,
21 CHEAT_ADD,
22 CHEAT_OR,
23 CHEAT_IF_EQ,
24 CHEAT_IF_NE,
25 CHEAT_IF_LT,
26 CHEAT_IF_GT,
27 CHEAT_IF_ULT,
28 CHEAT_IF_UGT,
29 CHEAT_IF_AND,
30 CHEAT_IF_LAND
31};
32
33enum GBACodeBreakerType {
34 CB_GAME_ID = 0x0,
35 CB_HOOK = 0x1,
36 CB_OR_2 = 0x2,
37 CB_ASSIGN_1 = 0x3,
38 CB_FILL = 0x4,
39 CB_FILL_8 = 0x5,
40 CB_AND_2 = 0x6,
41 CB_IF_EQ = 0x7,
42 CB_ASSIGN_2 = 0x8,
43 CB_ENCRYPT = 0x9,
44 CB_IF_NE = 0xA,
45 CB_IF_GT = 0xB,
46 CB_IF_LT = 0xC,
47 CB_IF_SPECIAL = 0xD,
48 CB_ADD_2 = 0xE,
49 CB_IF_AND = 0xF,
50};
51
52enum GBAGameSharkType {
53 GSA_ASSIGN_1 = 0x0,
54 GSA_ASSIGN_2 = 0x1,
55 GSA_ASSIGN_4 = 0x2,
56 GSA_ASSIGN_LIST = 0x3,
57 GSA_PATCH = 0x6,
58 GSA_BUTTON = 0x8,
59 GSA_IF_EQ = 0xD,
60 GSA_IF_EQ_RANGE = 0xE,
61 GSA_HOOK = 0xF
62};
63
64enum GBAActionReplay3Condition {
65 PAR3_COND_OTHER = 0x00000000,
66 PAR3_COND_EQ = 0x08000000,
67 PAR3_COND_NE = 0x10000000,
68 PAR3_COND_LT = 0x18000000,
69 PAR3_COND_GT = 0x20000000,
70 PAR3_COND_ULT = 0x28000000,
71 PAR3_COND_UGT = 0x30000000,
72 PAR3_COND_LAND = 0x38000000,
73};
74
75enum GBAActionReplay3Width {
76 PAR3_WIDTH_1 = 0x00000000,
77 PAR3_WIDTH_2 = 0x02000000,
78 PAR3_WIDTH_4 = 0x04000000,
79 PAR3_WIDTH_FALSE = 0x06000000,
80};
81
82enum GBAActionReplay3Action {
83 PAR3_ACTION_NEXT = 0x00000000,
84 PAR3_ACTION_NEXT_TWO = 0x40000000,
85 PAR3_ACTION_BLOCK = 0x80000000,
86 PAR3_ACTION_DISABLE = 0xC0000000,
87};
88
89enum GBAActionReplay3Base {
90 PAR3_BASE_ASSIGN = 0x00000000,
91 PAR3_BASE_INDIRECT = 0x40000000,
92 PAR3_BASE_ADD = 0x80000000,
93 PAR3_BASE_OTHER = 0xC0000000,
94
95 PAR3_BASE_ASSIGN_1 = 0x00000000,
96 PAR3_BASE_ASSIGN_2 = 0x02000000,
97 PAR3_BASE_ASSIGN_4 = 0x04000000,
98 PAR3_BASE_INDIRECT_1 = 0x40000000,
99 PAR3_BASE_INDIRECT_2 = 0x42000000,
100 PAR3_BASE_INDIRECT_4 = 0x44000000,
101 PAR3_BASE_ADD_1 = 0x80000000,
102 PAR3_BASE_ADD_2 = 0x82000000,
103 PAR3_BASE_ADD_4 = 0x84000000,
104 PAR3_BASE_HOOK = 0xC4000000,
105 PAR3_BASE_IO_2 = 0xC6000000,
106 PAR3_BASE_IO_3 = 0xC7000000,
107};
108
109enum GBAActionReplay3Other {
110 PAR3_OTHER_END = 0x00000000,
111 PAR3_OTHER_SLOWDOWN = 0x08000000,
112 PAR3_OTHER_BUTTON_1 = 0x10000000,
113 PAR3_OTHER_BUTTON_2 = 0x12000000,
114 PAR3_OTHER_BUTTON_4 = 0x14000000,
115 PAR3_OTHER_PATCH_1 = 0x18000000,
116 PAR3_OTHER_PATCH_2 = 0x1A000000,
117 PAR3_OTHER_PATCH_3 = 0x1C000000,
118 PAR3_OTHER_PATCH_4 = 0x1E000000,
119 PAR3_OTHER_ENDIF = 0x40000000,
120 PAR3_OTHER_ELSE = 0x60000000,
121 PAR3_OTHER_FILL_1 = 0x80000000,
122 PAR3_OTHER_FILL_2 = 0x82000000,
123 PAR3_OTHER_FILL_4 = 0x84000000,
124};
125
126enum {
127 PAR3_COND = 0x38000000,
128 PAR3_WIDTH = 0x06000000,
129 PAR3_ACTION = 0xC0000000,
130 PAR3_BASE = 0xC0000000,
131
132 PAR3_WIDTH_BASE = 25
133};
134
135struct GBACheat {
136 enum GBACheatType type;
137 int width;
138 uint32_t address;
139 uint32_t operand;
140 uint32_t repeat;
141 uint32_t negativeRepeat;
142
143 int32_t addressOffset;
144 int32_t operandOffset;
145};
146
147struct GBACheatHook {
148 uint32_t address;
149 enum ExecutionMode mode;
150 uint32_t patchedOpcode;
151 size_t refs;
152 size_t reentries;
153};
154
155mLOG_DECLARE_CATEGORY(CHEATS);
156
157DECLARE_VECTOR(GBACheatList, struct GBACheat);
158DECLARE_VECTOR(StringList, char*);
159
160struct GBACheatSet {
161 struct GBACheatHook* hook;
162 struct GBACheatList list;
163
164 struct GBACheatPatch {
165 uint32_t address;
166 int16_t newValue;
167 int16_t oldValue;
168 bool applied;
169 bool exists;
170 } romPatches[MAX_ROM_PATCHES];
171
172 struct GBACheat* incompleteCheat;
173 struct GBACheatPatch* incompletePatch;
174 struct GBACheat* currentBlock;
175
176 int gsaVersion;
177 uint32_t gsaSeeds[4];
178 int remainingAddresses;
179
180 char* name;
181 bool enabled;
182 struct StringList lines;
183};
184
185DECLARE_VECTOR(GBACheatSets, struct GBACheatSet*);
186
187struct GBACheatDevice {
188 struct mCPUComponent d;
189 struct GBA* p;
190
191 struct GBACheatSets cheats;
192};
193
194struct VFile;
195
196void GBACheatDeviceCreate(struct GBACheatDevice*);
197void GBACheatDeviceDestroy(struct GBACheatDevice*);
198void GBACheatDeviceClear(struct GBACheatDevice*);
199
200void GBACheatSetInit(struct GBACheatSet*, const char* name);
201void GBACheatSetDeinit(struct GBACheatSet*);
202
203void GBACheatAttachDevice(struct GBA* gba, struct GBACheatDevice*);
204
205void GBACheatAddSet(struct GBACheatDevice*, struct GBACheatSet*);
206void GBACheatRemoveSet(struct GBACheatDevice*, struct GBACheatSet*);
207void GBACheatSetCopyProperties(struct GBACheatSet* newSet, struct GBACheatSet* set);
208
209bool GBACheatAddCodeBreaker(struct GBACheatSet*, uint32_t op1, uint16_t op2);
210bool GBACheatAddCodeBreakerLine(struct GBACheatSet*, const char* line);
211
212bool GBACheatAddGameShark(struct GBACheatSet*, uint32_t op1, uint32_t op2);
213bool GBACheatAddGameSharkLine(struct GBACheatSet*, const char* line);
214
215bool GBACheatAddProActionReplay(struct GBACheatSet*, uint32_t op1, uint32_t op2);
216bool GBACheatAddProActionReplayLine(struct GBACheatSet*, const char* line);
217
218bool GBACheatAddVBALine(struct GBACheatSet*, const char* line);
219
220bool GBACheatAddAutodetect(struct GBACheatSet*, uint32_t op1, uint32_t op2);
221
222bool GBACheatParseFile(struct GBACheatDevice*, struct VFile*);
223bool GBACheatSaveFile(struct GBACheatDevice*, struct VFile*);
224
225bool GBACheatAddLine(struct GBACheatSet*, const char* line);
226
227void GBACheatRefresh(struct GBACheatDevice*, struct GBACheatSet*);
228
229#endif