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