-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgb.h
252 lines (224 loc) · 3.24 KB
/
gb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdint.h>
#define nelem(x) (sizeof(x)/sizeof(x[0]))
#define VAR(a) {&a, sizeof(a), 1}
#define ARR(a) {a, sizeof(*a), nelem(a)}
#define nil ((void*)0)
#define JOYPAD_CYCLE (30)
enum
{
JOYP = 0x00,
SB = 0x01,
SC = 0x02,
DIV = 0x04,
TIMA = 0x05,
TMA = 0x06,
TAC = 0x07,
IF = 0x0F,
NR10 = 0x10,
NR11 = 0x11,
NR12 = 0x12,
NR13 = 0x13,
NR14 = 0x14,
NR21 = 0x16,
NR22 = 0x17,
NR23 = 0x18,
NR24 = 0x19,
NR30 = 0x1A,
NR31 = 0x1B,
NR32 = 0x1C,
NR33 = 0x1D,
NR34 = 0x1E,
NR41 = 0x20,
NR42 = 0x21,
NR43 = 0x22,
NR44 = 0x23,
NR50 = 0x24,
NR51 = 0x25,
NR52 = 0x26,
WAVE = 0x30,
LCDC = 0x40,
STAT = 0x41,
SCY = 0x42,
SCX = 0x43,
LY = 0x44,
LYC = 0x45,
DMA = 0x46,
BGP = 0x47,
OBP0 = 0x48,
OBP1 = 0x49,
WY = 0x4A,
WX = 0x4B,
KEY1 = 0x4D,
VBK = 0x4F,
RP = 0x56,
HDMASH = 0x51,
HDMASL = 0x52,
HDMADH = 0x53,
HDMADL = 0x54,
HDMAC = 0x55,
BCPS = 0x68,
BCPD = 0x69,
OCPS = 0x6A,
OCPD = 0x6B,
SVBK = 0x70,
IE = 0xFF
};
enum
{
LCDEN = 0x80,
WINMAP = 0x40,
WINEN = 0x20,
BGTILE = 0x10,
BGMAP = 0x08,
SPR16 = 0x04,
SPREN = 0x02,
BGEN = 0x01,
BGPRI = 0x01,
IRQLYC = 0x40,
IRQM2 = 0x20,
IRQM1 = 0x10,
IRQM0 = 0x08,
IRQVBL = 1,
IRQLCDS = 2,
IRQTIM = 4,
IRQSER = 8,
IRQJOY = 16,
};
enum
{
CGB = 1,
COL = 2,
TURBO = 4,
FORCEDMG = 8,
FEATRAM = 1,
FEATBAT = 2,
FEATTIM = 4,
DMAREADY = 1,
DMAHBLANK = 2,
INIT = -1,
SAVE = -2,
RSTR = -3,
READ = -4,
};
enum
{
TIMERSIZ = 18,
PICW = 160,
PICH = 144,
FREQ = 1 << 23
};
enum
{
GB_KEY_RIGHT = 0x01,
GB_KEY_LEFT = 0x02,
GB_KEY_UP = 0x04,
GB_KEY_DOWN = 0x08,
GB_KEY_A = 0x10,
GB_KEY_B = 0x20,
GB_KEY_SELECT = 0x40,
GB_KEY_START = 0x80
};
enum { NEVENT = 2 };
enum
{
REG_RIP = 7,
REG_RSP = 6,
};
typedef uint8_t u8;
typedef uint16_t u16;
typedef int8_t i8;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint64_t u64;
typedef int64_t i64;
typedef struct Event Event;
typedef struct Var Var;
struct Event
{
int time;
void (*f)(void*);
Event* next;
void* aux;
};
struct Var
{
void *a;
int s, n;
};
#define VAR(a) {&a, sizeof(a), 1}
#define ARR(a) {a, sizeof(*a), nelem(a)}
extern u8 *rom, *back, reg[256], oam[256];
extern u8 vram[16384];
extern int nrom, nback, nbackbank;
extern u32 pal[64];
extern u8 dma;
extern u32 divclock;
extern u64 clock;
extern u8 ppuy, ppustate;
extern u8 keys;
extern int prish;
extern u8 mode;
extern u8 mbc;
extern Event* elist;
extern Event evhblank, evjoypad;
extern u32 moncols[4];
extern u8* pic;
extern int (*mapper)(int, int);
extern Event *events[NEVENT];
extern int savereq, loadreq;
/* joypad */
void
joypadevent(void*);
/* memory */
void
memwrite(u16 a, u8 v);
u8
memread(u16 a);
void
meminit(void);
/* events */
void
initevent(void);
void
addevent(Event*, int);
void
delevent(Event*);
void
popevent(void);
/* ppu */
void
hblanktick(void*);
void
ppusync(void);
void
ppuinit(void);
/* cpu */
int
step(void);
void
reset(void);
/* graphic */
void
initwindow(int scale);
void
render();
/* save */
void
putvars(Var *v);
void
getvars(Var *v);
void
flush();
int
savestate(const char *fname);
int
loadstate(const char *fname);
void
writeback(void);
/* error */
void
error(char*, ...);
void
panic(char*, ...);
void*
xalloc(long);