Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/gameboy/cheat/cheat.cpp
2 views
1
#include <gameboy/gameboy.hpp>
2
3
namespace GameBoy {
4
5
Cheat cheat;
6
7
bool Cheat::decode(const string &code_, unsigned &addr, unsigned &data, unsigned &comp) {
8
static bool initialize = false;
9
static uint8 mapProActionReplay[256], mapGameGenie[256];
10
11
if(initialize == false) {
12
initialize = true;
13
14
for(auto &n : mapProActionReplay) n = ~0;
15
mapProActionReplay['0'] = 0; mapProActionReplay['1'] = 1; mapProActionReplay['2'] = 2; mapProActionReplay['3'] = 3;
16
mapProActionReplay['4'] = 4; mapProActionReplay['5'] = 5; mapProActionReplay['6'] = 6; mapProActionReplay['7'] = 7;
17
mapProActionReplay['8'] = 8; mapProActionReplay['9'] = 9; mapProActionReplay['A'] = 10; mapProActionReplay['B'] = 11;
18
mapProActionReplay['C'] = 12; mapProActionReplay['D'] = 13; mapProActionReplay['E'] = 14; mapProActionReplay['F'] = 15;
19
20
for(auto &n : mapGameGenie) n = ~0;
21
mapGameGenie['0'] = 0; mapGameGenie['1'] = 1; mapGameGenie['2'] = 2; mapGameGenie['3'] = 3;
22
mapGameGenie['4'] = 4; mapGameGenie['5'] = 5; mapGameGenie['6'] = 6; mapGameGenie['7'] = 7;
23
mapGameGenie['8'] = 8; mapGameGenie['9'] = 9; mapGameGenie['A'] = 10; mapGameGenie['B'] = 11;
24
mapGameGenie['C'] = 12; mapGameGenie['D'] = 13; mapGameGenie['E'] = 14; mapGameGenie['F'] = 15;
25
}
26
27
string code = code_;
28
code.upper();
29
unsigned length = code.length(), bits = 0;
30
31
if(code.wildcard("????:??")) {
32
code = { substr(code, 0, 4), substr(code, 5, 2) };
33
for(unsigned n = 0; n < 6; n++) if(mapProActionReplay[code[n]] > 15) return false;
34
bits = hex(code);
35
addr = (bits >> 8) & 0xffff;
36
data = (bits >> 0) & 0xff;
37
comp = ~0;
38
return true;
39
}
40
41
if(code.wildcard("????:??:??")) {
42
code = { substr(code, 0, 4), substr(code, 5, 2), substr(code, 8, 2) };
43
for(unsigned n = 0; n < 8; n++) if(mapProActionReplay[code[n]] > 15) return false;
44
bits = hex(code);
45
addr = (bits >> 16) & 0xffff;
46
data = (bits >> 8) & 0xff;
47
comp = (bits >> 0) & 0xff;
48
return true;
49
}
50
51
if(code.wildcard("???" "-" "???")) {
52
code = { substr(code, 0, 3), substr(code, 4, 3) };
53
for(unsigned n = 0; n < 6; n++) if(mapGameGenie[code[n]] > 15) return false;
54
for(unsigned n = 0; n < 6; n++) bits |= mapGameGenie[code[n]] << (20 - n * 4);
55
56
addr = (bits >> 0) & 0xffff;
57
data = (bits >> 16) & 0xff;
58
comp = ~0;
59
60
addr = (((addr >> 4) | (addr << 12)) & 0xffff) ^ 0xf000;
61
62
return true;
63
}
64
65
if(code.wildcard("???" "-" "???" "-" "???")) {
66
code = { substr(code, 0, 3), substr(code, 4, 3), substr(code, 8, 1), substr(code, 10, 1) };
67
for(unsigned n = 0; n < 8; n++) if(mapGameGenie[code[n]] > 15) return false;
68
for(unsigned n = 0; n < 8; n++) bits |= mapGameGenie[code[n]] << (28 - n * 4);
69
70
addr = (bits >> 8) & 0xffff;
71
data = (bits >> 24) & 0xff;
72
comp = (bits >> 0) & 0xff;
73
74
addr = (((addr >> 4) | (addr << 12)) & 0xffff) ^ 0xf000;
75
comp = (((comp >> 2) | (comp << 6)) & 0xff) ^ 0xba;
76
77
return true;
78
}
79
80
return false;
81
}
82
83
void Cheat::synchronize() {
84
for(auto &n : override) n = false;
85
86
for(unsigned n = 0; n < size(); n++) {
87
override[operator[](n).addr] = true;
88
}
89
}
90
91
}
92
93