Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libsnes/bsnes/gameboy/cpu/core/registers.hpp
2 views
1
enum {
2
A, F, AF,
3
B, C, BC,
4
D, E, DE,
5
H, L, HL,
6
SP, PC,
7
};
8
9
enum {
10
ZF, NF, HF, CF,
11
};
12
13
//register base class
14
//the idea here is to have all registers derive from a single base class.
15
//this allows construction of opcodes that can take any register as input or output,
16
//despite the fact that behind-the-scenes, special handling is done for eg: F, AF, HL, etc.
17
//registers can also be chained together: eg af = 0x0000 writes both a and f.
18
struct Register {
19
virtual operator unsigned() const = 0;
20
virtual unsigned operator=(unsigned x) = 0;
21
Register& operator=(const Register &x) { operator=((unsigned)x); return *this; }
22
23
unsigned operator++(int) { unsigned r = *this; operator=(*this + 1); return r; }
24
unsigned operator--(int) { unsigned r = *this; operator=(*this - 1); return r; }
25
unsigned operator++() { return operator=(*this + 1); }
26
unsigned operator--() { return operator=(*this - 1); }
27
28
unsigned operator |=(unsigned x) { return operator=(*this | x); }
29
unsigned operator ^=(unsigned x) { return operator=(*this ^ x); }
30
unsigned operator &=(unsigned x) { return operator=(*this & x); }
31
32
unsigned operator<<=(unsigned x) { return operator=(*this << x); }
33
unsigned operator>>=(unsigned x) { return operator=(*this >> x); }
34
35
unsigned operator +=(unsigned x) { return operator=(*this + x); }
36
unsigned operator -=(unsigned x) { return operator=(*this - x); }
37
unsigned operator *=(unsigned x) { return operator=(*this * x); }
38
unsigned operator /=(unsigned x) { return operator=(*this / x); }
39
unsigned operator %=(unsigned x) { return operator=(*this % x); }
40
};
41
42
struct Register8 : Register {
43
uint8 data;
44
operator unsigned() const { return data; }
45
unsigned operator=(unsigned x) { return data = x; }
46
};
47
48
struct RegisterF : Register {
49
bool z, n, h, c;
50
operator unsigned() const { return (z << 7) | (n << 6) | (h << 5) | (c << 4); }
51
unsigned operator=(unsigned x) { z = x & 0x80; n = x & 0x40; h = x & 0x20; c = x & 0x10; return *this; }
52
bool& operator[](unsigned r) {
53
static bool* table[] = { &z, &n, &h, &c };
54
return *table[r];
55
}
56
};
57
58
struct Register16 : Register {
59
uint16 data;
60
operator unsigned() const { return data; }
61
unsigned operator=(unsigned x) { return data = x; }
62
};
63
64
struct RegisterAF : Register {
65
Register8 &hi;
66
RegisterF &lo;
67
operator unsigned() const { return (hi << 8) | (lo << 0); }
68
unsigned operator=(unsigned x) { hi = x >> 8; lo = x >> 0; return *this; }
69
RegisterAF(Register8 &hi, RegisterF &lo) : hi(hi), lo(lo) {}
70
};
71
72
struct RegisterW : Register {
73
Register8 &hi, &lo;
74
operator unsigned() const { return (hi << 8) | (lo << 0); }
75
unsigned operator=(unsigned x) { hi = x >> 8; lo = x >> 0; return *this; }
76
RegisterW(Register8 &hi, Register8 &lo) : hi(hi), lo(lo) {}
77
};
78
79
struct Registers {
80
Register8 a;
81
RegisterF f;
82
RegisterAF af;
83
Register8 b;
84
Register8 c;
85
RegisterW bc;
86
Register8 d;
87
Register8 e;
88
RegisterW de;
89
Register8 h;
90
Register8 l;
91
RegisterW hl;
92
Register16 sp;
93
Register16 pc;
94
95
Register& operator[](unsigned r) {
96
static Register* table[] = { &a, &f, &af, &b, &c, &bc, &d, &e, &de, &h, &l, &hl, &sp, &pc };
97
return *table[r];
98
}
99
100
Registers() : af(a, f), bc(b, c), de(d, e), hl(h, l) {}
101
} r;
102
103