GB.cpp 187 KB
Newer Older
1
//#include "../win32/stdafx.h" // would fix LNK2005 linker errors for MSVC
Zach Bacon's avatar
Zach Bacon committed
2
3
#include <assert.h>
#include <memory.h>
4
5
6
7
8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../NLS.h"
Zach Bacon's avatar
Zach Bacon committed
9
10
11
12
#include "../System.h"
#include "../Util.h"
#include "../common/ConfigManager.h"
#include "../gba/GBALink.h"
13
#include "../gba/Sound.h"
14
15
16
17
18
19
20
21
22
23
24
#include "gb.h"
#include "gbCheats.h"
#include "gbGlobals.h"
#include "gbMemory.h"
#include "gbSGB.h"
#include "gbSound.h"

#ifdef __GNUC__
#define _stricmp strcasecmp
#endif

Zach Bacon's avatar
Zach Bacon committed
25
extern uint8_t* pix;
26
bool gbUpdateSizes();
27
28
29
30
31
32
bool inBios = false;

// debugging
bool memorydebug = false;
char gbBuffer[2048];

33
extern uint16_t gbLineMix[160];
34
35

// mappers
Zach Bacon's avatar
Zach Bacon committed
36
37
void (*mapper)(uint16_t, uint8_t) = NULL;
void (*mapperRAM)(uint16_t, uint8_t) = NULL;
38
uint8_t (*mapperReadRAM)(uint16_t) = NULL;
39
40
41
42
43
44
45
46
47
void (*mapperUpdateClock)() = NULL;

// registers
gbRegister PC;
gbRegister SP;
gbRegister AF;
gbRegister BC;
gbRegister DE;
gbRegister HL;
48
uint16_t IFF = 0;
49
// 0xff04
Zach Bacon's avatar
Zach Bacon committed
50
uint8_t register_DIV = 0;
51
// 0xff05
Zach Bacon's avatar
Zach Bacon committed
52
uint8_t register_TIMA = 0;
53
// 0xff06
Zach Bacon's avatar
Zach Bacon committed
54
uint8_t register_TMA = 0;
55
// 0xff07
Zach Bacon's avatar
Zach Bacon committed
56
uint8_t register_TAC = 0;
57
// 0xff0f
Zach Bacon's avatar
Zach Bacon committed
58
uint8_t register_IF = 0;
59
// 0xff40
Zach Bacon's avatar
Zach Bacon committed
60
uint8_t register_LCDC = 0;
61
// 0xff41
Zach Bacon's avatar
Zach Bacon committed
62
uint8_t register_STAT = 0;
63
// 0xff42
Zach Bacon's avatar
Zach Bacon committed
64
uint8_t register_SCY = 0;
65
// 0xff43
Zach Bacon's avatar
Zach Bacon committed
66
uint8_t register_SCX = 0;
67
// 0xff44
Zach Bacon's avatar
Zach Bacon committed
68
uint8_t register_LY = 0;
69
// 0xff45
Zach Bacon's avatar
Zach Bacon committed
70
uint8_t register_LYC = 0;
71
// 0xff46
Zach Bacon's avatar
Zach Bacon committed
72
uint8_t register_DMA = 0;
73
// 0xff4a
Zach Bacon's avatar
Zach Bacon committed
74
uint8_t register_WY = 0;
75
// 0xff4b
Zach Bacon's avatar
Zach Bacon committed
76
uint8_t register_WX = 0;
77
// 0xff4f
Zach Bacon's avatar
Zach Bacon committed
78
uint8_t register_VBK = 0;
79
// 0xff51
80
uint8_t register_HDMA1 = 0;
81
// 0xff52
82
uint8_t register_HDMA2 = 0;
83
// 0xff53
84
uint8_t register_HDMA3 = 0;
85
// 0xff54
86
uint8_t register_HDMA4 = 0;
87
// 0xff55
88
uint8_t register_HDMA5 = 0;
89
// 0xff70
Zach Bacon's avatar
Zach Bacon committed
90
uint8_t register_SVBK = 0;
91
// 0xffff
Zach Bacon's avatar
Zach Bacon committed
92
uint8_t register_IE = 0;
93
94

// ticks definition
Zach Bacon's avatar
Zach Bacon committed
95
96
97
98
99
int GBDIV_CLOCK_TICKS = 64;
int GBLCD_MODE_0_CLOCK_TICKS = 51;
int GBLCD_MODE_1_CLOCK_TICKS = 1140;
int GBLCD_MODE_2_CLOCK_TICKS = 20;
int GBLCD_MODE_3_CLOCK_TICKS = 43;
100
101
102
103
104
int GBLY_INCREMENT_CLOCK_TICKS = 114;
int GBTIMER_MODE_0_CLOCK_TICKS = 256;
int GBTIMER_MODE_1_CLOCK_TICKS = 4;
int GBTIMER_MODE_2_CLOCK_TICKS = 16;
int GBTIMER_MODE_3_CLOCK_TICKS = 64;
Zach Bacon's avatar
Zach Bacon committed
105
106
int GBSERIAL_CLOCK_TICKS = 128;
int GBSYNCHRONIZE_CLOCK_TICKS = 52920;
107
108
109
110
111
112
113
114
115
116
117
118
119

// state variables

// general
int clockTicks = 0;
bool gbSystemMessage = false;
int gbGBCColorType = 0;
int gbHardware = 0;
int gbRomType = 0;
int gbRemainingClockTicks = 0;
int gbOldClockTicks = 0;
int gbIntBreak = 0;
int gbInterruptLaunched = 0;
120
uint8_t gbCheatingDevice = 0; // 1 = GS, 2 = GG
121
122
123
124
125
126
127
128
129
130
// breakpoint
bool breakpoint = false;
// interrupt
int gbInt48Signal = 0;
int gbInterruptWait = 0;
// serial
int gbSerialOn = 0;
int gbSerialTicks = 0;
int gbSerialBits = 0;
// timer
131
int gbTimerOn = 0;
132
133
134
135
136
137
138
139
int gbTimerTicks = GBTIMER_MODE_0_CLOCK_TICKS;
int gbTimerClockTicks = GBTIMER_MODE_0_CLOCK_TICKS;
int gbTimerMode = 0;
bool gbIncreased = false;
// The internal timer is always active, and it is
// not reset by writing to register_TIMA/TMA, but by
// writing to register_DIV...
int gbInternalTimer = 0x55;
Zach Bacon's avatar
Zach Bacon committed
140
141
const uint8_t gbTimerMask[4] = { 0xff, 0x3, 0xf, 0x3f };
const uint8_t gbTimerBug[8] = { 0x80, 0x80, 0x02, 0x02, 0x0, 0xff, 0x0, 0xff };
142
143
144
145
146
147
bool gbTimerModeChange = false;
bool gbTimerOnChange = false;
// lcd
bool gbScreenOn = true;
int gbLcdMode = 2;
int gbLcdModeDelayed = 2;
Zach Bacon's avatar
Zach Bacon committed
148
int gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS - 1;
149
150
151
152
int gbLcdTicksDelayed = GBLCD_MODE_2_CLOCK_TICKS;
int gbLcdLYIncrementTicks = 114;
int gbLcdLYIncrementTicksDelayed = 115;
int gbScreenTicks = 0;
153
154
155
uint8_t gbSCYLine[300];
uint8_t gbSCXLine[300];
uint8_t gbBgpLine[300];
Zach Bacon's avatar
Zach Bacon committed
156
157
158
uint8_t gbObp0Line[300];
uint8_t gbObp1Line[300];
uint8_t gbSpritesTicks[300];
159
uint8_t oldRegister_WY;
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
bool gbLYChangeHappened = false;
bool gbLCDChangeHappened = false;
int gbLine99Ticks = 1;
int gbRegisterLYLCDCOffOn = 0;
int inUseRegister_WY = 0;

// Used to keep track of the line that ellapse
// when screen is off
int gbWhiteScreen = 0;
bool gbBlackScreen = false;
int register_LCDCBusy = 0;

// div
int gbDivTicks = GBDIV_CLOCK_TICKS;
// cgb
int gbVramBank = 0;
int gbWramBank = 1;
//sgb
bool gbSgbResetFlag = false;
// gbHdmaDestination is 0x99d0 on startup (tested on HW)
// but I'm not sure what gbHdmaSource is...
int gbHdmaSource = 0x99d0;
int gbHdmaDestination = 0x99d0;
int gbHdmaBytes = 0x0000;
int gbHdmaOn = 0;
int gbSpeed = 0;
// frame counting
int gbFrameCount = 0;
int gbFrameSkip = 0;
int gbFrameSkipCount = 0;
// timing
191
192
193
uint32_t gbLastTime = 0;
uint32_t gbElapsedTime = 0;
uint32_t gbTimeNow = 0;
194
195
196
int gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS;
// emulator features
int gbBattery = 0;
retro-wertz's avatar
retro-wertz committed
197
int gbRumble = 0;
198
199
200
201
202
203
bool gbBatteryError = false;
int gbCaptureNumber = 0;
bool gbCapture = false;
bool gbCapturePrevious = false;
int gbJoymask[4] = { 0, 0, 0, 0 };

204
uint8_t gbRamFill = 0xff;
205

Zach Bacon's avatar
Zach Bacon committed
206
207
208
209
210
211
212
213
214
215
int gbRomSizes[] = {
    0x00008000, // 32K
    0x00010000, // 64K
    0x00020000, // 128K
    0x00040000, // 256K
    0x00080000, // 512K
    0x00100000, // 1024K
    0x00200000, // 2048K
    0x00400000, // 4096K
    0x00800000 // 8192K
216
217
};
int gbRomSizesMasks[] = { 0x00007fff,
Zach Bacon's avatar
Zach Bacon committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    0x0000ffff,
    0x0001ffff,
    0x0003ffff,
    0x0007ffff,
    0x000fffff,
    0x001fffff,
    0x003fffff,
    0x007fffff };

int gbRamSizes[6] = {
    0x00000000, // 0K
    0x00002000, // 2K  // Changed to 2000 to avoid problems with gbMemoryMap...
    0x00002000, // 8K
    0x00008000, // 32K
    0x00020000, // 128K
    0x00010000 // 64K
234
235
236
};

int gbRamSizesMasks[6] = { 0x00000000,
Zach Bacon's avatar
Zach Bacon committed
237
238
239
240
241
    0x000007ff,
    0x00001fff,
    0x00007fff,
    0x0001ffff,
    0x0000ffff };
242
243

int gbCycles[] = {
Zach Bacon's avatar
Zach Bacon committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    //  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, // 0
    1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, // 1
    2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, // 2
    2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, // 3
    1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 4
    1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 5
    1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 6
    2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, // 7
    1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 8
    1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 9
    1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // a
    1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // b
    2, 3, 3, 4, 3, 4, 2, 4, 2, 4, 3, 2, 3, 6, 2, 4, // c
    2, 3, 3, 1, 3, 4, 2, 4, 2, 4, 3, 1, 3, 1, 2, 4, // d
    3, 3, 2, 1, 1, 4, 2, 4, 4, 1, 4, 1, 1, 1, 2, 4, // e
    3, 3, 2, 1, 1, 4, 2, 4, 3, 2, 4, 1, 0, 1, 2, 4 // f
261
262
263
};

int gbCyclesCB[] = {
Zach Bacon's avatar
Zach Bacon committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    //  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 1
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 2
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 3
    2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 4
    2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 5
    2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 6
    2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 7
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 8
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 9
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // a
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // b
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // c
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // d
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // e
    2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2 // f
281
282
};

283
uint16_t DAATable[] = {
Zach Bacon's avatar
Zach Bacon committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
    0x0080, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700,
    0x0800, 0x0900, 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500,
    0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500, 0x1600, 0x1700,
    0x1800, 0x1900, 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500,
    0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2600, 0x2700,
    0x2800, 0x2900, 0x3000, 0x3100, 0x3200, 0x3300, 0x3400, 0x3500,
    0x3000, 0x3100, 0x3200, 0x3300, 0x3400, 0x3500, 0x3600, 0x3700,
    0x3800, 0x3900, 0x4000, 0x4100, 0x4200, 0x4300, 0x4400, 0x4500,
    0x4000, 0x4100, 0x4200, 0x4300, 0x4400, 0x4500, 0x4600, 0x4700,
    0x4800, 0x4900, 0x5000, 0x5100, 0x5200, 0x5300, 0x5400, 0x5500,
    0x5000, 0x5100, 0x5200, 0x5300, 0x5400, 0x5500, 0x5600, 0x5700,
    0x5800, 0x5900, 0x6000, 0x6100, 0x6200, 0x6300, 0x6400, 0x6500,
    0x6000, 0x6100, 0x6200, 0x6300, 0x6400, 0x6500, 0x6600, 0x6700,
    0x6800, 0x6900, 0x7000, 0x7100, 0x7200, 0x7300, 0x7400, 0x7500,
    0x7000, 0x7100, 0x7200, 0x7300, 0x7400, 0x7500, 0x7600, 0x7700,
    0x7800, 0x7900, 0x8000, 0x8100, 0x8200, 0x8300, 0x8400, 0x8500,
    0x8000, 0x8100, 0x8200, 0x8300, 0x8400, 0x8500, 0x8600, 0x8700,
    0x8800, 0x8900, 0x9000, 0x9100, 0x9200, 0x9300, 0x9400, 0x9500,
    0x9000, 0x9100, 0x9200, 0x9300, 0x9400, 0x9500, 0x9600, 0x9700,
    0x9800, 0x9900, 0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510,
    0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510, 0x0610, 0x0710,
    0x0810, 0x0910, 0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510,
    0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510, 0x1610, 0x1710,
    0x1810, 0x1910, 0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510,
    0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510, 0x2610, 0x2710,
    0x2810, 0x2910, 0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510,
    0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510, 0x3610, 0x3710,
    0x3810, 0x3910, 0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510,
    0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510, 0x4610, 0x4710,
    0x4810, 0x4910, 0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510,
    0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510, 0x5610, 0x5710,
    0x5810, 0x5910, 0x6010, 0x6110, 0x6210, 0x6310, 0x6410, 0x6510,
    0x6010, 0x6110, 0x6210, 0x6310, 0x6410, 0x6510, 0x6610, 0x6710,
    0x6810, 0x6910, 0x7010, 0x7110, 0x7210, 0x7310, 0x7410, 0x7510,
    0x7010, 0x7110, 0x7210, 0x7310, 0x7410, 0x7510, 0x7610, 0x7710,
    0x7810, 0x7910, 0x8010, 0x8110, 0x8210, 0x8310, 0x8410, 0x8510,
    0x8010, 0x8110, 0x8210, 0x8310, 0x8410, 0x8510, 0x8610, 0x8710,
    0x8810, 0x8910, 0x9010, 0x9110, 0x9210, 0x9310, 0x9410, 0x9510,
    0x9010, 0x9110, 0x9210, 0x9310, 0x9410, 0x9510, 0x9610, 0x9710,
    0x9810, 0x9910, 0xA010, 0xA110, 0xA210, 0xA310, 0xA410, 0xA510,
    0xA010, 0xA110, 0xA210, 0xA310, 0xA410, 0xA510, 0xA610, 0xA710,
    0xA810, 0xA910, 0xB010, 0xB110, 0xB210, 0xB310, 0xB410, 0xB510,
    0xB010, 0xB110, 0xB210, 0xB310, 0xB410, 0xB510, 0xB610, 0xB710,
    0xB810, 0xB910, 0xC010, 0xC110, 0xC210, 0xC310, 0xC410, 0xC510,
    0xC010, 0xC110, 0xC210, 0xC310, 0xC410, 0xC510, 0xC610, 0xC710,
    0xC810, 0xC910, 0xD010, 0xD110, 0xD210, 0xD310, 0xD410, 0xD510,
    0xD010, 0xD110, 0xD210, 0xD310, 0xD410, 0xD510, 0xD610, 0xD710,
    0xD810, 0xD910, 0xE010, 0xE110, 0xE210, 0xE310, 0xE410, 0xE510,
    0xE010, 0xE110, 0xE210, 0xE310, 0xE410, 0xE510, 0xE610, 0xE710,
    0xE810, 0xE910, 0xF010, 0xF110, 0xF210, 0xF310, 0xF410, 0xF510,
    0xF010, 0xF110, 0xF210, 0xF310, 0xF410, 0xF510, 0xF610, 0xF710,
    0xF810, 0xF910, 0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510,
    0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510, 0x0610, 0x0710,
    0x0810, 0x0910, 0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510,
    0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510, 0x1610, 0x1710,
    0x1810, 0x1910, 0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510,
    0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510, 0x2610, 0x2710,
    0x2810, 0x2910, 0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510,
    0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510, 0x3610, 0x3710,
    0x3810, 0x3910, 0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510,
    0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510, 0x4610, 0x4710,
    0x4810, 0x4910, 0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510,
    0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510, 0x5610, 0x5710,
    0x5810, 0x5910, 0x6010, 0x6110, 0x6210, 0x6310, 0x6410, 0x6510,
    0x0600, 0x0700, 0x0800, 0x0900, 0x0A00, 0x0B00, 0x0C00, 0x0D00,
    0x0E00, 0x0F00, 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500,
    0x1600, 0x1700, 0x1800, 0x1900, 0x1A00, 0x1B00, 0x1C00, 0x1D00,
    0x1E00, 0x1F00, 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500,
    0x2600, 0x2700, 0x2800, 0x2900, 0x2A00, 0x2B00, 0x2C00, 0x2D00,
    0x2E00, 0x2F00, 0x3000, 0x3100, 0x3200, 0x3300, 0x3400, 0x3500,
    0x3600, 0x3700, 0x3800, 0x3900, 0x3A00, 0x3B00, 0x3C00, 0x3D00,
    0x3E00, 0x3F00, 0x4000, 0x4100, 0x4200, 0x4300, 0x4400, 0x4500,
    0x4600, 0x4700, 0x4800, 0x4900, 0x4A00, 0x4B00, 0x4C00, 0x4D00,
    0x4E00, 0x4F00, 0x5000, 0x5100, 0x5200, 0x5300, 0x5400, 0x5500,
    0x5600, 0x5700, 0x5800, 0x5900, 0x5A00, 0x5B00, 0x5C00, 0x5D00,
    0x5E00, 0x5F00, 0x6000, 0x6100, 0x6200, 0x6300, 0x6400, 0x6500,
    0x6600, 0x6700, 0x6800, 0x6900, 0x6A00, 0x6B00, 0x6C00, 0x6D00,
    0x6E00, 0x6F00, 0x7000, 0x7100, 0x7200, 0x7300, 0x7400, 0x7500,
    0x7600, 0x7700, 0x7800, 0x7900, 0x7A00, 0x7B00, 0x7C00, 0x7D00,
    0x7E00, 0x7F00, 0x8000, 0x8100, 0x8200, 0x8300, 0x8400, 0x8500,
    0x8600, 0x8700, 0x8800, 0x8900, 0x8A00, 0x8B00, 0x8C00, 0x8D00,
    0x8E00, 0x8F00, 0x9000, 0x9100, 0x9200, 0x9300, 0x9400, 0x9500,
    0x9600, 0x9700, 0x9800, 0x9900, 0x9A00, 0x9B00, 0x9C00, 0x9D00,
    0x9E00, 0x9F00, 0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510,
    0x0610, 0x0710, 0x0810, 0x0910, 0x0A10, 0x0B10, 0x0C10, 0x0D10,
    0x0E10, 0x0F10, 0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510,
    0x1610, 0x1710, 0x1810, 0x1910, 0x1A10, 0x1B10, 0x1C10, 0x1D10,
    0x1E10, 0x1F10, 0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510,
    0x2610, 0x2710, 0x2810, 0x2910, 0x2A10, 0x2B10, 0x2C10, 0x2D10,
    0x2E10, 0x2F10, 0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510,
    0x3610, 0x3710, 0x3810, 0x3910, 0x3A10, 0x3B10, 0x3C10, 0x3D10,
    0x3E10, 0x3F10, 0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510,
    0x4610, 0x4710, 0x4810, 0x4910, 0x4A10, 0x4B10, 0x4C10, 0x4D10,
    0x4E10, 0x4F10, 0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510,
    0x5610, 0x5710, 0x5810, 0x5910, 0x5A10, 0x5B10, 0x5C10, 0x5D10,
    0x5E10, 0x5F10, 0x6010, 0x6110, 0x6210, 0x6310, 0x6410, 0x6510,
    0x6610, 0x6710, 0x6810, 0x6910, 0x6A10, 0x6B10, 0x6C10, 0x6D10,
    0x6E10, 0x6F10, 0x7010, 0x7110, 0x7210, 0x7310, 0x7410, 0x7510,
    0x7610, 0x7710, 0x7810, 0x7910, 0x7A10, 0x7B10, 0x7C10, 0x7D10,
    0x7E10, 0x7F10, 0x8010, 0x8110, 0x8210, 0x8310, 0x8410, 0x8510,
    0x8610, 0x8710, 0x8810, 0x8910, 0x8A10, 0x8B10, 0x8C10, 0x8D10,
    0x8E10, 0x8F10, 0x9010, 0x9110, 0x9210, 0x9310, 0x9410, 0x9510,
    0x9610, 0x9710, 0x9810, 0x9910, 0x9A10, 0x9B10, 0x9C10, 0x9D10,
    0x9E10, 0x9F10, 0xA010, 0xA110, 0xA210, 0xA310, 0xA410, 0xA510,
    0xA610, 0xA710, 0xA810, 0xA910, 0xAA10, 0xAB10, 0xAC10, 0xAD10,
    0xAE10, 0xAF10, 0xB010, 0xB110, 0xB210, 0xB310, 0xB410, 0xB510,
    0xB610, 0xB710, 0xB810, 0xB910, 0xBA10, 0xBB10, 0xBC10, 0xBD10,
    0xBE10, 0xBF10, 0xC010, 0xC110, 0xC210, 0xC310, 0xC410, 0xC510,
    0xC610, 0xC710, 0xC810, 0xC910, 0xCA10, 0xCB10, 0xCC10, 0xCD10,
    0xCE10, 0xCF10, 0xD010, 0xD110, 0xD210, 0xD310, 0xD410, 0xD510,
    0xD610, 0xD710, 0xD810, 0xD910, 0xDA10, 0xDB10, 0xDC10, 0xDD10,
    0xDE10, 0xDF10, 0xE010, 0xE110, 0xE210, 0xE310, 0xE410, 0xE510,
    0xE610, 0xE710, 0xE810, 0xE910, 0xEA10, 0xEB10, 0xEC10, 0xED10,
    0xEE10, 0xEF10, 0xF010, 0xF110, 0xF210, 0xF310, 0xF410, 0xF510,
    0xF610, 0xF710, 0xF810, 0xF910, 0xFA10, 0xFB10, 0xFC10, 0xFD10,
    0xFE10, 0xFF10, 0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510,
    0x0610, 0x0710, 0x0810, 0x0910, 0x0A10, 0x0B10, 0x0C10, 0x0D10,
    0x0E10, 0x0F10, 0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510,
    0x1610, 0x1710, 0x1810, 0x1910, 0x1A10, 0x1B10, 0x1C10, 0x1D10,
    0x1E10, 0x1F10, 0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510,
    0x2610, 0x2710, 0x2810, 0x2910, 0x2A10, 0x2B10, 0x2C10, 0x2D10,
    0x2E10, 0x2F10, 0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510,
    0x3610, 0x3710, 0x3810, 0x3910, 0x3A10, 0x3B10, 0x3C10, 0x3D10,
    0x3E10, 0x3F10, 0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510,
    0x4610, 0x4710, 0x4810, 0x4910, 0x4A10, 0x4B10, 0x4C10, 0x4D10,
    0x4E10, 0x4F10, 0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510,
    0x5610, 0x5710, 0x5810, 0x5910, 0x5A10, 0x5B10, 0x5C10, 0x5D10,
    0x5E10, 0x5F10, 0x6010, 0x6110, 0x6210, 0x6310, 0x6410, 0x6510,
    0x00C0, 0x0140, 0x0240, 0x0340, 0x0440, 0x0540, 0x0640, 0x0740,
    0x0840, 0x0940, 0x0A40, 0x0B40, 0x0C40, 0x0D40, 0x0E40, 0x0F40,
    0x1040, 0x1140, 0x1240, 0x1340, 0x1440, 0x1540, 0x1640, 0x1740,
    0x1840, 0x1940, 0x1A40, 0x1B40, 0x1C40, 0x1D40, 0x1E40, 0x1F40,
    0x2040, 0x2140, 0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740,
    0x2840, 0x2940, 0x2A40, 0x2B40, 0x2C40, 0x2D40, 0x2E40, 0x2F40,
    0x3040, 0x3140, 0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740,
    0x3840, 0x3940, 0x3A40, 0x3B40, 0x3C40, 0x3D40, 0x3E40, 0x3F40,
    0x4040, 0x4140, 0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740,
    0x4840, 0x4940, 0x4A40, 0x4B40, 0x4C40, 0x4D40, 0x4E40, 0x4F40,
    0x5040, 0x5140, 0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740,
    0x5840, 0x5940, 0x5A40, 0x5B40, 0x5C40, 0x5D40, 0x5E40, 0x5F40,
    0x6040, 0x6140, 0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740,
    0x6840, 0x6940, 0x6A40, 0x6B40, 0x6C40, 0x6D40, 0x6E40, 0x6F40,
    0x7040, 0x7140, 0x7240, 0x7340, 0x7440, 0x7540, 0x7640, 0x7740,
    0x7840, 0x7940, 0x7A40, 0x7B40, 0x7C40, 0x7D40, 0x7E40, 0x7F40,
    0x8040, 0x8140, 0x8240, 0x8340, 0x8440, 0x8540, 0x8640, 0x8740,
    0x8840, 0x8940, 0x8A40, 0x8B40, 0x8C40, 0x8D40, 0x8E40, 0x8F40,
    0x9040, 0x9140, 0x9240, 0x9340, 0x9440, 0x9540, 0x9640, 0x9740,
    0x9840, 0x9940, 0x9A40, 0x9B40, 0x9C40, 0x9D40, 0x9E40, 0x9F40,
    0xA040, 0xA140, 0xA240, 0xA340, 0xA440, 0xA540, 0xA640, 0xA740,
    0xA840, 0xA940, 0xAA40, 0xAB40, 0xAC40, 0xAD40, 0xAE40, 0xAF40,
    0xB040, 0xB140, 0xB240, 0xB340, 0xB440, 0xB540, 0xB640, 0xB740,
    0xB840, 0xB940, 0xBA40, 0xBB40, 0xBC40, 0xBD40, 0xBE40, 0xBF40,
    0xC040, 0xC140, 0xC240, 0xC340, 0xC440, 0xC540, 0xC640, 0xC740,
    0xC840, 0xC940, 0xCA40, 0xCB40, 0xCC40, 0xCD40, 0xCE40, 0xCF40,
    0xD040, 0xD140, 0xD240, 0xD340, 0xD440, 0xD540, 0xD640, 0xD740,
    0xD840, 0xD940, 0xDA40, 0xDB40, 0xDC40, 0xDD40, 0xDE40, 0xDF40,
    0xE040, 0xE140, 0xE240, 0xE340, 0xE440, 0xE540, 0xE640, 0xE740,
    0xE840, 0xE940, 0xEA40, 0xEB40, 0xEC40, 0xED40, 0xEE40, 0xEF40,
    0xF040, 0xF140, 0xF240, 0xF340, 0xF440, 0xF540, 0xF640, 0xF740,
    0xF840, 0xF940, 0xFA40, 0xFB40, 0xFC40, 0xFD40, 0xFE40, 0xFF40,
    0xA050, 0xA150, 0xA250, 0xA350, 0xA450, 0xA550, 0xA650, 0xA750,
    0xA850, 0xA950, 0xAA50, 0xAB50, 0xAC50, 0xAD50, 0xAE50, 0xAF50,
    0xB050, 0xB150, 0xB250, 0xB350, 0xB450, 0xB550, 0xB650, 0xB750,
    0xB850, 0xB950, 0xBA50, 0xBB50, 0xBC50, 0xBD50, 0xBE50, 0xBF50,
    0xC050, 0xC150, 0xC250, 0xC350, 0xC450, 0xC550, 0xC650, 0xC750,
    0xC850, 0xC950, 0xCA50, 0xCB50, 0xCC50, 0xCD50, 0xCE50, 0xCF50,
    0xD050, 0xD150, 0xD250, 0xD350, 0xD450, 0xD550, 0xD650, 0xD750,
    0xD850, 0xD950, 0xDA50, 0xDB50, 0xDC50, 0xDD50, 0xDE50, 0xDF50,
    0xE050, 0xE150, 0xE250, 0xE350, 0xE450, 0xE550, 0xE650, 0xE750,
    0xE850, 0xE950, 0xEA50, 0xEB50, 0xEC50, 0xED50, 0xEE50, 0xEF50,
    0xF050, 0xF150, 0xF250, 0xF350, 0xF450, 0xF550, 0xF650, 0xF750,
    0xF850, 0xF950, 0xFA50, 0xFB50, 0xFC50, 0xFD50, 0xFE50, 0xFF50,
    0x00D0, 0x0150, 0x0250, 0x0350, 0x0450, 0x0550, 0x0650, 0x0750,
    0x0850, 0x0950, 0x0A50, 0x0B50, 0x0C50, 0x0D50, 0x0E50, 0x0F50,
    0x1050, 0x1150, 0x1250, 0x1350, 0x1450, 0x1550, 0x1650, 0x1750,
    0x1850, 0x1950, 0x1A50, 0x1B50, 0x1C50, 0x1D50, 0x1E50, 0x1F50,
    0x2050, 0x2150, 0x2250, 0x2350, 0x2450, 0x2550, 0x2650, 0x2750,
    0x2850, 0x2950, 0x2A50, 0x2B50, 0x2C50, 0x2D50, 0x2E50, 0x2F50,
    0x3050, 0x3150, 0x3250, 0x3350, 0x3450, 0x3550, 0x3650, 0x3750,
    0x3850, 0x3950, 0x3A50, 0x3B50, 0x3C50, 0x3D50, 0x3E50, 0x3F50,
    0x4050, 0x4150, 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750,
    0x4850, 0x4950, 0x4A50, 0x4B50, 0x4C50, 0x4D50, 0x4E50, 0x4F50,
    0x5050, 0x5150, 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750,
    0x5850, 0x5950, 0x5A50, 0x5B50, 0x5C50, 0x5D50, 0x5E50, 0x5F50,
    0x6050, 0x6150, 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750,
    0x6850, 0x6950, 0x6A50, 0x6B50, 0x6C50, 0x6D50, 0x6E50, 0x6F50,
    0x7050, 0x7150, 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750,
    0x7850, 0x7950, 0x7A50, 0x7B50, 0x7C50, 0x7D50, 0x7E50, 0x7F50,
    0x8050, 0x8150, 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750,
    0x8850, 0x8950, 0x8A50, 0x8B50, 0x8C50, 0x8D50, 0x8E50, 0x8F50,
    0x9050, 0x9150, 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750,
    0x9850, 0x9950, 0x9A50, 0x9B50, 0x9C50, 0x9D50, 0x9E50, 0x9F50,
    0xFA40, 0xFB40, 0xFC40, 0xFD40, 0xFE40, 0xFF40, 0x00C0, 0x0140,
    0x0240, 0x0340, 0x0440, 0x0540, 0x0640, 0x0740, 0x0840, 0x0940,
    0x0A40, 0x0B40, 0x0C40, 0x0D40, 0x0E40, 0x0F40, 0x1040, 0x1140,
    0x1240, 0x1340, 0x1440, 0x1540, 0x1640, 0x1740, 0x1840, 0x1940,
    0x1A40, 0x1B40, 0x1C40, 0x1D40, 0x1E40, 0x1F40, 0x2040, 0x2140,
    0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740, 0x2840, 0x2940,
    0x2A40, 0x2B40, 0x2C40, 0x2D40, 0x2E40, 0x2F40, 0x3040, 0x3140,
    0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740, 0x3840, 0x3940,
    0x3A40, 0x3B40, 0x3C40, 0x3D40, 0x3E40, 0x3F40, 0x4040, 0x4140,
    0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740, 0x4840, 0x4940,
    0x4A40, 0x4B40, 0x4C40, 0x4D40, 0x4E40, 0x4F40, 0x5040, 0x5140,
    0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740, 0x5840, 0x5940,
    0x5A40, 0x5B40, 0x5C40, 0x5D40, 0x5E40, 0x5F40, 0x6040, 0x6140,
    0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740, 0x6840, 0x6940,
    0x6A40, 0x6B40, 0x6C40, 0x6D40, 0x6E40, 0x6F40, 0x7040, 0x7140,
    0x7240, 0x7340, 0x7440, 0x7540, 0x7640, 0x7740, 0x7840, 0x7940,
    0x7A40, 0x7B40, 0x7C40, 0x7D40, 0x7E40, 0x7F40, 0x8040, 0x8140,
    0x8240, 0x8340, 0x8440, 0x8540, 0x8640, 0x8740, 0x8840, 0x8940,
    0x8A40, 0x8B40, 0x8C40, 0x8D40, 0x8E40, 0x8F40, 0x9040, 0x9140,
    0x9240, 0x9340, 0x9440, 0x9540, 0x9640, 0x9740, 0x9840, 0x9940,
    0x9A40, 0x9B40, 0x9C40, 0x9D40, 0x9E40, 0x9F40, 0xA040, 0xA140,
    0xA240, 0xA340, 0xA440, 0xA540, 0xA640, 0xA740, 0xA840, 0xA940,
    0xAA40, 0xAB40, 0xAC40, 0xAD40, 0xAE40, 0xAF40, 0xB040, 0xB140,
    0xB240, 0xB340, 0xB440, 0xB540, 0xB640, 0xB740, 0xB840, 0xB940,
    0xBA40, 0xBB40, 0xBC40, 0xBD40, 0xBE40, 0xBF40, 0xC040, 0xC140,
    0xC240, 0xC340, 0xC440, 0xC540, 0xC640, 0xC740, 0xC840, 0xC940,
    0xCA40, 0xCB40, 0xCC40, 0xCD40, 0xCE40, 0xCF40, 0xD040, 0xD140,
    0xD240, 0xD340, 0xD440, 0xD540, 0xD640, 0xD740, 0xD840, 0xD940,
    0xDA40, 0xDB40, 0xDC40, 0xDD40, 0xDE40, 0xDF40, 0xE040, 0xE140,
    0xE240, 0xE340, 0xE440, 0xE540, 0xE640, 0xE740, 0xE840, 0xE940,
    0xEA40, 0xEB40, 0xEC40, 0xED40, 0xEE40, 0xEF40, 0xF040, 0xF140,
    0xF240, 0xF340, 0xF440, 0xF540, 0xF640, 0xF740, 0xF840, 0xF940,
    0x9A50, 0x9B50, 0x9C50, 0x9D50, 0x9E50, 0x9F50, 0xA050, 0xA150,
    0xA250, 0xA350, 0xA450, 0xA550, 0xA650, 0xA750, 0xA850, 0xA950,
    0xAA50, 0xAB50, 0xAC50, 0xAD50, 0xAE50, 0xAF50, 0xB050, 0xB150,
    0xB250, 0xB350, 0xB450, 0xB550, 0xB650, 0xB750, 0xB850, 0xB950,
    0xBA50, 0xBB50, 0xBC50, 0xBD50, 0xBE50, 0xBF50, 0xC050, 0xC150,
    0xC250, 0xC350, 0xC450, 0xC550, 0xC650, 0xC750, 0xC850, 0xC950,
    0xCA50, 0xCB50, 0xCC50, 0xCD50, 0xCE50, 0xCF50, 0xD050, 0xD150,
    0xD250, 0xD350, 0xD450, 0xD550, 0xD650, 0xD750, 0xD850, 0xD950,
    0xDA50, 0xDB50, 0xDC50, 0xDD50, 0xDE50, 0xDF50, 0xE050, 0xE150,
    0xE250, 0xE350, 0xE450, 0xE550, 0xE650, 0xE750, 0xE850, 0xE950,
    0xEA50, 0xEB50, 0xEC50, 0xED50, 0xEE50, 0xEF50, 0xF050, 0xF150,
    0xF250, 0xF350, 0xF450, 0xF550, 0xF650, 0xF750, 0xF850, 0xF950,
    0xFA50, 0xFB50, 0xFC50, 0xFD50, 0xFE50, 0xFF50, 0x00D0, 0x0150,
    0x0250, 0x0350, 0x0450, 0x0550, 0x0650, 0x0750, 0x0850, 0x0950,
    0x0A50, 0x0B50, 0x0C50, 0x0D50, 0x0E50, 0x0F50, 0x1050, 0x1150,
    0x1250, 0x1350, 0x1450, 0x1550, 0x1650, 0x1750, 0x1850, 0x1950,
    0x1A50, 0x1B50, 0x1C50, 0x1D50, 0x1E50, 0x1F50, 0x2050, 0x2150,
    0x2250, 0x2350, 0x2450, 0x2550, 0x2650, 0x2750, 0x2850, 0x2950,
    0x2A50, 0x2B50, 0x2C50, 0x2D50, 0x2E50, 0x2F50, 0x3050, 0x3150,
    0x3250, 0x3350, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
    0x3A50, 0x3B50, 0x3C50, 0x3D50, 0x3E50, 0x3F50, 0x4050, 0x4150,
    0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
    0x4A50, 0x4B50, 0x4C50, 0x4D50, 0x4E50, 0x4F50, 0x5050, 0x5150,
    0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
    0x5A50, 0x5B50, 0x5C50, 0x5D50, 0x5E50, 0x5F50, 0x6050, 0x6150,
    0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
    0x6A50, 0x6B50, 0x6C50, 0x6D50, 0x6E50, 0x6F50, 0x7050, 0x7150,
    0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
    0x7A50, 0x7B50, 0x7C50, 0x7D50, 0x7E50, 0x7F50, 0x8050, 0x8150,
    0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
    0x8A50, 0x8B50, 0x8C50, 0x8D50, 0x8E50, 0x8F50, 0x9050, 0x9150,
    0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
540
541
};

542
uint8_t ZeroTable[256] = {
Zach Bacon's avatar
Zach Bacon committed
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0
567
568
};

skidau's avatar
skidau committed
569
// Title checksums that are treated specially by the CGB boot ROM
570
static const uint8_t gbColorizationChecksums[79] = {
Zach Bacon's avatar
Zach Bacon committed
571
572
573
574
575
576
577
    0x00, 0x88, 0x16, 0x36, 0xD1, 0xDB, 0xF2, 0x3C, 0x8C, 0x92, 0x3D, 0x5C,
    0x58, 0xC9, 0x3E, 0x70, 0x1D, 0x59, 0x69, 0x19, 0x35, 0xA8, 0x14, 0xAA,
    0x75, 0x95, 0x99, 0x34, 0x6F, 0x15, 0xFF, 0x97, 0x4B, 0x90, 0x17, 0x10,
    0x39, 0xF7, 0xF6, 0xA2, 0x49, 0x4E, 0x43, 0x68, 0xE0, 0x8B, 0xF0, 0xCE,
    0x0C, 0x29, 0xE8, 0xB7, 0x86, 0x9A, 0x52, 0x01, 0x9D, 0x71, 0x9C, 0xBD,
    0x5D, 0x6D, 0x67, 0x3F, 0x6B, 0xB3, 0x46, 0x28, 0xA5, 0xC6, 0xD3, 0x27,
    0x61, 0x18, 0x66, 0x6A, 0xBF, 0x0D, 0xF4
skidau's avatar
skidau committed
578
579
580
581
};

// For titles with colliding checksums, the fourth character of the game title
// for disambiguation.
582
static const uint8_t gbColorizationDisambigChars[29] = {
Zach Bacon's avatar
Zach Bacon committed
583
584
585
586
    'B', 'E', 'F', 'A', 'A', 'R', 'B', 'E',
    'K', 'E', 'K', ' ', 'R', '-', 'U', 'R',
    'A', 'R', ' ', 'I', 'N', 'A', 'I', 'L',
    'I', 'C', 'E', ' ', 'R'
skidau's avatar
skidau committed
587
588
589
};

// Palette ID | (Flags << 5)
590
static const uint8_t gbColorizationPaletteInfo[94] = {
Zach Bacon's avatar
Zach Bacon committed
591
592
593
594
595
596
597
598
    0x7C, 0x08, 0x12, 0xA3, 0xA2, 0x07, 0x87, 0x4B, 0x20, 0x12, 0x65, 0xA8,
    0x16, 0xA9, 0x86, 0xB1, 0x68, 0xA0, 0x87, 0x66, 0x12, 0xA1, 0x30, 0x3C,
    0x12, 0x85, 0x12, 0x64, 0x1B, 0x07, 0x06, 0x6F, 0x6E, 0x6E, 0xAE, 0xAF,
    0x6F, 0xB2, 0xAF, 0xB2, 0xA8, 0xAB, 0x6F, 0xAF, 0x86, 0xAE, 0xA2, 0xA2,
    0x12, 0xAF, 0x13, 0x12, 0xA1, 0x6E, 0xAF, 0xAF, 0xAD, 0x06, 0x4C, 0x6E,
    0xAF, 0xAF, 0x12, 0x7C, 0xAC, 0xA8, 0x6A, 0x6E, 0x13, 0xA0, 0x2D, 0xA8,
    0x2B, 0xAC, 0x64, 0xAC, 0x6D, 0x87, 0xBC, 0x60, 0xB4, 0x13, 0x72, 0x7C,
    0xB5, 0xAE, 0xAE, 0x7C, 0x7C, 0x65, 0xA2, 0x6C, 0x64, 0x85
skidau's avatar
skidau committed
599
600
601
};

// Uncompressed palette data from the CGB boot ROM
602
static const uint16_t gbColorizationPaletteData[32][3][4] = {
Zach Bacon's avatar
Zach Bacon committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
    {
        { 0x7FFF, 0x01DF, 0x0112, 0x0000 },
        { 0x7FFF, 0x7EEB, 0x001F, 0x7C00 },
        { 0x7FFF, 0x42B5, 0x3DC8, 0x0000 },
    },
    {
        { 0x231F, 0x035F, 0x00F2, 0x0009 },
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x4FFF, 0x7ED2, 0x3A4C, 0x1CE0 },
    },
    {
        { 0x7FFF, 0x7FFF, 0x7E8C, 0x7C00 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x03ED, 0x7FFF, 0x255F, 0x0000 },
    },
    {
        { 0x7FFF, 0x7FFF, 0x7E8C, 0x7C00 },
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x036A, 0x021F, 0x03FF, 0x7FFF },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x03EF, 0x01D6, 0x0000 },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x7EEB, 0x001F, 0x7C00 },
        { 0x7FFF, 0x03EA, 0x011F, 0x0000 },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x7EEB, 0x001F, 0x7C00 },
        { 0x7FFF, 0x027F, 0x001F, 0x0000 },
    },
    {
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x7EEB, 0x001F, 0x7C00 },
        { 0x7FFF, 0x03FF, 0x001F, 0x0000 },
    },
    {
        { 0x299F, 0x001A, 0x000C, 0x0000 },
        { 0x7C00, 0x7FFF, 0x3FFF, 0x7E00 },
        { 0x7E74, 0x03FF, 0x0180, 0x0000 },
    },
    {
        { 0x7FFF, 0x01DF, 0x0112, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x67FF, 0x77AC, 0x1A13, 0x2D6B },
    },
    {
        { 0x0000, 0x7FFF, 0x421F, 0x1CF2 },
        { 0x0000, 0x7FFF, 0x421F, 0x1CF2 },
        { 0x7ED6, 0x4BFF, 0x2175, 0x0000 },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x3FFF, 0x7E00, 0x001F },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
    },
    {
        { 0x231F, 0x035F, 0x00F2, 0x0009 },
        { 0x7FFF, 0x7EEB, 0x001F, 0x7C00 },
        { 0x7FFF, 0x6E31, 0x454A, 0x0000 },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x7FFF, 0x6E31, 0x454A, 0x0000 },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
    },
    {
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
    },
    {
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
    },
    {
        { 0x7FFF, 0x03E0, 0x0206, 0x0120 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
    },
    {
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
        { 0x0000, 0x4200, 0x037F, 0x7FFF },
    },
    {
        { 0x03FF, 0x001F, 0x000C, 0x0000 },
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
    },
    {
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x42B5, 0x3DC8, 0x0000 },
    },
    {
        { 0x7FFF, 0x5294, 0x294A, 0x0000 },
        { 0x7FFF, 0x5294, 0x294A, 0x0000 },
        { 0x7FFF, 0x5294, 0x294A, 0x0000 },
    },
    {
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x53FF, 0x4A5F, 0x7E52, 0x0000 },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
    },
    {
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x639F, 0x4279, 0x15B0, 0x04CB },
    },
    {
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x1BEF, 0x0200, 0x0000 },
        { 0x7FFF, 0x03FF, 0x012F, 0x0000 },
    },
    {
        { 0x7FFF, 0x033F, 0x0193, 0x0000 },
        { 0x7FFF, 0x033F, 0x0193, 0x0000 },
        { 0x7FFF, 0x033F, 0x0193, 0x0000 },
    },
    {
        { 0x7FFF, 0x421F, 0x1CF2, 0x0000 },
        { 0x7FFF, 0x7E8C, 0x7C00, 0x0000 },
        { 0x7FFF, 0x1BEF, 0x6180, 0x0000 },
    },
    {
        { 0x2120, 0x8022, 0x8281, 0x1110 },
        { 0xFF7F, 0xDF7F, 0x1201, 0x0001 },
        { 0xFF00, 0xFF7F, 0x1F03, 0x0000 },
    },
    {
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
    },
    {
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
        { 0x7FFF, 0x32BF, 0x00D0, 0x0000 },
    }
skidau's avatar
skidau committed
763
764
};

765
766
767
768
769
770
771
772
773
774
775
776
777
778
#define GBSAVE_GAME_VERSION_1 1
#define GBSAVE_GAME_VERSION_2 2
#define GBSAVE_GAME_VERSION_3 3
#define GBSAVE_GAME_VERSION_4 4
#define GBSAVE_GAME_VERSION_5 5
#define GBSAVE_GAME_VERSION_6 6
#define GBSAVE_GAME_VERSION_7 7
#define GBSAVE_GAME_VERSION_8 8
#define GBSAVE_GAME_VERSION_9 9
#define GBSAVE_GAME_VERSION_10 10
#define GBSAVE_GAME_VERSION_11 11
#define GBSAVE_GAME_VERSION_12 12
#define GBSAVE_GAME_VERSION GBSAVE_GAME_VERSION_12

Zach Bacon's avatar
Zach Bacon committed
779
int inline gbGetValue(int min, int max, int v)
780
{
Zach Bacon's avatar
Zach Bacon committed
781
    return (int)(min + (float)(max - min) * (2.0 * (v / 31.0) - (v / 31.0) * (v / 31.0)));
782
783
784
785
}

void gbGenFilter()
{
Zach Bacon's avatar
Zach Bacon committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
    for (int r = 0; r < 32; r++) {
        for (int g = 0; g < 32; g++) {
            for (int b = 0; b < 32; b++) {
                int nr = gbGetValue(gbGetValue(4, 14, g),
                             gbGetValue(24, 29, g), r)
                    - 4;
                int ng = gbGetValue(gbGetValue(4 + gbGetValue(0, 5, r),
                                        14 + gbGetValue(0, 3, r), b),
                             gbGetValue(24 + gbGetValue(0, 3, r),
                                        29 + gbGetValue(0, 1, r), b),
                             g)
                    - 4;
                int nb = gbGetValue(gbGetValue(4 + gbGetValue(0, 5, r),
                                        14 + gbGetValue(0, 3, r), g),
                             gbGetValue(24 + gbGetValue(0, 3, r),
                                        29 + gbGetValue(0, 1, r), g),
                             b)
                    - 4;
                gbColorFilter[(b << 10) | (g << 5) | r] = (nb << 10) | (ng << 5) | nr;
            }
        }
807
808
809
    }
}

Zach Bacon's avatar
Zach Bacon committed
810
bool gbIsGameboyRom(char* file)
811
{
Zach Bacon's avatar
Zach Bacon committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
    if (strlen(file) > 4) {
        char* p = strrchr(file, '.');

        if (p != NULL) {
            if (_stricmp(p, ".gb") == 0)
                return true;
            if (_stricmp(p, ".dmg") == 0)
                return true;
            if (_stricmp(p, ".gbc") == 0)
                return true;
            if (_stricmp(p, ".cgb") == 0)
                return true;
            if (_stricmp(p, ".sgb") == 0)
                return true;
        }
827
828
    }

Zach Bacon's avatar
Zach Bacon committed
829
    return false;
830
831
}

832
void gbCopyMemory(uint16_t d, uint16_t s, int count)
833
{
Zach Bacon's avatar
Zach Bacon committed
834
835
836
837
838
839
    while (count) {
        gbMemoryMap[d >> 12][d & 0x0fff] = gbMemoryMap[s >> 12][s & 0x0fff];
        s++;
        d++;
        count--;
    }
840
841
842
843
844
}

void gbDoHdma()
{

Zach Bacon's avatar
Zach Bacon committed
845
846
847
    gbCopyMemory((gbHdmaDestination & 0x1ff0) | 0x8000,
        gbHdmaSource & 0xfff0,
        0x10);
848

Zach Bacon's avatar
Zach Bacon committed
849
850
851
    gbHdmaDestination += 0x10;
    if (gbHdmaDestination == 0xa000)
        gbHdmaDestination = 0x8000;
852

Zach Bacon's avatar
Zach Bacon committed
853
854
855
    gbHdmaSource += 0x10;
    if (gbHdmaSource == 0x8000)
        gbHdmaSource = 0xa000;
856

Zach Bacon's avatar
Zach Bacon committed
857
858
    register_HDMA2 = gbHdmaSource & 0xff;
    register_HDMA1 = gbHdmaSource >> 8;
859

Zach Bacon's avatar
Zach Bacon committed
860
861
    register_HDMA4 = gbHdmaDestination & 0xff;
    register_HDMA3 = gbHdmaDestination >> 8;
862

Zach Bacon's avatar
Zach Bacon committed
863
864
865
866
    gbHdmaBytes -= 0x10;
    gbMemory[0xff55] = --register_HDMA5;
    if (register_HDMA5 == 0xff)
        gbHdmaOn = 0;
867

Zach Bacon's avatar
Zach Bacon committed
868
869
870
871
872
    // We need to add the dmaClockticks for HDMA !
    if (gbSpeed)
        gbDmaTicks = 17;
    else
        gbDmaTicks = 9;
873

Zach Bacon's avatar
Zach Bacon committed
874
875
    if (IFF & 0x80)
        gbDmaTicks++;
876
877
878
879
880
}

// fix for Harley and Lego Racers
void gbCompareLYToLYC()
{
Zach Bacon's avatar
Zach Bacon committed
881
882
    if (register_LCDC & 0x80) {
        if (register_LY == register_LYC) {
883

Zach Bacon's avatar
Zach Bacon committed
884
885
            // mark that we have a match
            register_STAT |= 4;
886

Zach Bacon's avatar
Zach Bacon committed
887
888
889
890
891
892
893
894
895
            // check if we need an interrupt
            if (register_STAT & 0x40) {
                // send LCD interrupt only if no interrupt 48h signal...
                if (!gbInt48Signal) {
                    register_IF |= 2;
                }
                gbInt48Signal |= 8;
            }
        } else // no match
896
        {
Zach Bacon's avatar
Zach Bacon committed
897
898
            register_STAT &= 0xfb;
            gbInt48Signal &= ~8;
899
900
901
902
        }
    }
}

Zach Bacon's avatar
Zach Bacon committed
903
void gbWriteMemory(register uint16_t address, register uint8_t value)
904
905
{

Zach Bacon's avatar
Zach Bacon committed
906
    if (address < 0x8000) {
907
#ifndef FINAL_VERSION
Zach Bacon's avatar
Zach Bacon committed
908
909
910
911
912
913
        if (memorydebug && (address > 0x3fff || address < 0x2000)) {
            log("Memory register write %04x=%02x PC=%04x\n",
                address,
                value,
                PC.W);
        }
914
915

#endif
Zach Bacon's avatar
Zach Bacon committed
916
917
918
919
920
921
922
923
924
925
926
927
928
929
        if (mapper)
            (*mapper)(address, value);
        return;
    }

    if (address < 0xa000) {
        // No access to Vram during mode 3
        // (used to emulate the gfx differences between GB & GBC-GBA/SP in Stunt Racer)
        if ((gbLcdModeDelayed != 3) ||
            // This part is used to emulate a small difference between hardwares
            // (check 8-in-1's arrow on GBA/GBC to verify it)
            ((register_LY == 0) && ((gbHardware & 0xa) && (gbScreenOn == false) && (register_LCDC & 0x80)) && (gbLcdLYIncrementTicksDelayed == (GBLY_INCREMENT_CLOCK_TICKS - GBLCD_MODE_2_CLOCK_TICKS))))
            gbMemoryMap[address >> 12][address & 0x0fff] = value;
        return;
930
    }
Zach Bacon's avatar
Zach Bacon committed
931
932
933
934
935
936
937
938
939
940
941
942
943

    // Used for the mirroring of 0xC000 in 0xE000
    if ((address >= 0xe000) && (address < 0xfe00))
        address &= ~0x2000;

    if (address < 0xc000) {
#ifndef FINAL_VERSION
        if (memorydebug) {
            log("Memory register write %04x=%02x PC=%04x\n",
                address,
                value,
                PC.W);
        }
944
945
#endif

Zach Bacon's avatar
Zach Bacon committed
946
947
948
949
        // Is that a correct fix ??? (it used to be 'if (mapper)')...
        if (mapperRAM)
            (*mapperRAM)(address, value);
        return;
950
951
    }

Zach Bacon's avatar
Zach Bacon committed
952
953
954
955
    if (address < 0xfe00) {
        gbMemoryMap[address >> 12][address & 0x0fff] = value;
        return;
    }
956

Zach Bacon's avatar
Zach Bacon committed
957
958
959
960
961
962
963
964
965
    // OAM not accessible during mode 2 & 3.
    if (address < 0xfea0) {
        if (((gbHardware & 0xa) && ((gbLcdMode | gbLcdModeDelayed) & 2)) || ((gbHardware & 5) && (((gbLcdModeDelayed == 2) && (gbLcdTicksDelayed <= GBLCD_MODE_2_CLOCK_TICKS)) || (gbLcdModeDelayed == 3))))
            return;
        else {
            gbMemory[address] = value;
            return;
        }
    }
966

Zach Bacon's avatar
Zach Bacon committed
967
968
969
970
    if ((address > 0xfea0) && (address < 0xff00)) { // GBC allows reading/writing to that area
        gbMemory[address] = value;
        return;
    }
971

Zach Bacon's avatar
Zach Bacon committed
972
    switch (address & 0x00ff) {
973
974

    case 0x00: {
Zach Bacon's avatar
Zach Bacon committed
975
976
977
978
979
        gbMemory[0xff00] = ((gbMemory[0xff00] & 0xcf) | (value & 0x30) | 0xc0);
        if (gbSgbMode) {
            gbSgbDoBitTransfer(value);
        }
        return;
980
981
982
    }

    case 0x01: {
Zach Bacon's avatar
Zach Bacon committed
983
984
        gbMemory[0xff01] = value;
        return;
985
986
    }

Zach Bacon's avatar
Zach Bacon committed
987
988
989
    // serial control
    case 0x02: {
        gbSerialOn = (value & 0x80);
990
#ifndef NO_LINK
Zach Bacon's avatar
Zach Bacon committed
991
992
993
994
995
996
        //trying to detect whether the game has exited multiplay mode, pokemon blue start w/ 0x7e while pocket racing start w/ 0x7c
        if (EmuReseted || (gbMemory[0xff02] & 0x7c) || (value & 0x7c) || (!(value & 0x81))) {
            LinkFirstTime = true;
        }
        EmuReseted = false;
        gbMemory[0xff02] = value;
retro-wertz's avatar
retro-wertz committed
997
998
999
        if (gbSerialOn && (GetLinkMode() == LINK_GAMEBOY_IPC || GetLinkMode() == LINK_GAMEBOY_SOCKET
        || GetLinkMode() == LINK_DISCONNECTED || winGbPrinterEnabled)) {

Zach Bacon's avatar
Zach Bacon committed
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
            gbSerialTicks = GBSERIAL_CLOCK_TICKS;

            LinkIsWaiting = true;

            //Do data exchange, master initiate the transfer
            //may cause visual artifact if not processed immediately, is it due to IRQ stuff or invalid data being exchanged?
            if ((value & 1)) { //internal clock
                if (gbSerialFunction) {
                    gbSIO_SC = value;
                    gbMemory[0xff01] = gbSerialFunction(gbMemory[0xff01]); //gbSerialFunction/gbStartLink/gbPrinter
                } else
                    gbMemory[0xff01] = 0xff;
                gbMemory[0xff02] &= 0x7f;
                gbSerialOn = 0;
                gbMemory[0xff0f] = register_IF |= 8;
            }
1016
#ifdef OLD_GB_LINK
Zach Bacon's avatar
Zach Bacon committed
1017
1018
1019
1020
1021
1022
            if (linkConnected) {
                if (value & 1) {
                    linkSendByte(0x100 | gbMemory[0xFF01]);
                    Sleep(5);
                }
            }
1023
#endif
Zach Bacon's avatar
Zach Bacon committed
1024
        }
1025

Zach Bacon's avatar
Zach Bacon committed
1026
1027
        gbSerialBits = 0;
        return;
1028
#endif
Zach Bacon's avatar
Zach Bacon committed
1029
    }
1030
1031

    case 0x04: {
Zach Bacon's avatar
Zach Bacon committed
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
        // DIV register resets on any write
        // (not totally perfect, but better than nothing)
        gbMemory[0xff04] = register_DIV = 0;
        gbDivTicks = GBDIV_CLOCK_TICKS;
        // Another weird timer 'bug' :
        // Writing to DIV register resets the internal timer,
        // and can also increase TIMA/trigger an interrupt
        // in some cases...
        if (gbTimerOn && !(gbInternalTimer & (gbTimerClockTicks >> 1))) {
            gbMemory[0xff05] = ++register_TIMA;
            if (register_TIMA == 0) {
                // timer overflow!

                // reload timer modulo
                gbMemory[0xff05] = register_TIMA = register_TMA;

                // flag interrupt
                gbMemory[0xff0f] = register_IF |= 4;
            }
1051
        }
Zach Bacon's avatar
Zach Bacon committed
1052
1053
        gbInternalTimer = 0xff;
        return;
1054
1055
    }
    case 0x05:
Zach Bacon's avatar
Zach Bacon committed
1056
1057
        gbMemory[0xff05] = register_TIMA = value;
        return;
1058
1059

    case 0x06:
Zach Bacon's avatar
Zach Bacon committed
1060
1061
        gbMemory[0xff06] = register_TMA = value;
        return;
1062

Zach Bacon's avatar
Zach Bacon committed
1063
    // TIMER control
1064
1065
    case 0x07: {

Zach Bacon's avatar
Zach Bacon committed
1066
1067
        gbTimerModeChange = (((value & 3) != (register_TAC & 3)) && (value & register_TAC & 4)) ? true : false;
        gbTimerOnChange = (((value ^ register_TAC) & 4) == 4) ? true : false;
1068

1069
        gbTimerOn = (value & 4);
1070

Zach Bacon's avatar
Zach Bacon committed
1071
1072
        if (gbTimerOnChange || gbTimerModeChange) {
            gbTimerMode = value & 3;
1073

Zach Bacon's avatar
Zach Bacon committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
            switch (gbTimerMode) {
            case 0:
                gbTimerClockTicks = GBTIMER_MODE_0_CLOCK_TICKS;
                break;
            case 1:
                gbTimerClockTicks = GBTIMER_MODE_1_CLOCK_TICKS;
                break;
            case 2:
                gbTimerClockTicks = GBTIMER_MODE_2_CLOCK_TICKS;
                break;
            case 3:
                gbTimerClockTicks = GBTIMER_MODE_3_CLOCK_TICKS;
                break;
            }
1088
1089
        }

Zach Bacon's avatar
Zach Bacon committed
1090
1091
1092
        // This looks weird, but this emulates a bug in which register_TIMA
        // is increased when writing to register_TAC
        // (This fixes Korodice's long-delay between menus bug).
1093

Zach Bacon's avatar
Zach Bacon committed
1094
1095
        if (gbTimerOnChange || gbTimerModeChange) {
            bool temp = false;
1096

Zach Bacon's avatar
Zach Bacon committed
1097
            if ((gbTimerOn && !gbTimerModeChange) && (gbTimerMode & 2) && !(gbInternalTimer & 0x80) && (gbInternalTimer & (gbTimerClockTicks >> 1)) && !(gbInternalTimer & (gbTimerClockTicks >> 5)))
1098
                temp = true;
Zach Bacon's avatar
Zach Bacon committed
1099
            else if ((!gbTimerOn && !gbTimerModeChange && gbTimerOnChange) && ((gbTimerTicks - 1) < (gbTimerClockTicks >> 1)))
1100
                temp = true;
Zach Bacon's avatar
Zach Bacon committed
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
            else if (gbTimerOn && gbTimerModeChange && !gbTimerOnChange) {
                switch (gbTimerMode & 3) {
                case 0x00:
                    temp = false;
                    break;
                case 0x01:
                    if (((gbInternalTimer & 0x82) == 2) && (gbTimerTicks > (clockTicks + 1)))
                        temp = true;
                    break;
                case 0x02:
                    if (((gbInternalTimer & 0x88) == 0x8) && (gbTimerTicks > (clockTicks + 1)))
                        temp = true;
                    break;
                case 0x03:
                    if (((gbInternalTimer & 0xA0) == 0x20) && (gbTimerTicks > (clockTicks + 1)))
                        temp = true;
                    break;
                }
            }
1120

Zach Bacon's avatar
Zach Bacon committed
1121
1122
1123
1124
            if (temp) {
                gbMemory[0xff05] = ++register_TIMA;
                if ((register_TIMA & 0xff) == 0) {
                    // timer overflow!
1125

Zach Bacon's avatar
Zach Bacon committed
1126
1127
                    // reload timer modulo
                    gbMemory[0xff05] = register_TIMA = register_TMA;
1128

Zach Bacon's avatar
Zach Bacon committed
1129
1130
1131
1132
                    // flag interrupt
                    gbMemory[0xff0f] = register_IF |= 4;
                }
            }
1133
        }
Zach Bacon's avatar
Zach Bacon committed
1134
1135
        gbMemory[0xff07] = register_TAC = value;
        return;
1136
1137
1138
    }

    case 0x0f: {
Zach Bacon's avatar
Zach Bacon committed
1139
1140
1141
        gbMemory[0xff0f] = register_IF = value;
        //gbMemory[0xff0f] = register_IE |= value;
        return;
1142
1143
1144
1145
1146
1147
1148
    }

    case 0x10:
    case 0x11:
    case 0x12:
    case 0x13:
    case 0x14:
retro-wertz's avatar
retro-wertz committed
1149
    case 0x15:
1150
1151
1152
1153
1154
1155
1156
1157
1158
    case 0x16:
    case 0x17:
    case 0x18:
    case 0x19:
    case 0x1a:
    case 0x1b:
    case 0x1c:
    case 0x1d:
    case 0x1e:
retro-wertz's avatar
retro-wertz committed
1159
    case 0x1f:
1160
1161
1162
1163
1164
    case 0x20:
    case 0x21:
    case 0x22:
    case 0x23:
    case 0x24:
retro-wertz's avatar
retro-wertz committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
    case 0x25:
    case 0x26:
    case 0x27:
    case 0x28:
    case 0x29:
    case 0x2a:
    case 0x2b:
    case 0x2c:
    case 0x2d:
    case 0x2e:
    case 0x2f:
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
    case 0x30:
    case 0x31:
    case 0x32:
    case 0x33:
    case 0x34:
    case 0x35:
    case 0x36:
    case 0x37:
    case 0x38:
    case 0x39:
    case 0x3a:
    case 0x3b:
    case 0x3c:
    case 0x3d:
    case 0x3e:
retro-wertz's avatar
retro-wertz committed
1191
1192
1193
    case 0x3f:
        // Sound registers handled by blargg
        gbSoundEvent(address, value);
Zach Bacon's avatar
Zach Bacon committed
1194
1195
        //gbMemory[address] = value;
        return;
1196
1197

    case 0x40: {
Zach Bacon's avatar
Zach Bacon committed
1198
        int lcdChange = (register_LCDC & 0x80) ^ (value & 0x80);
1199

Zach Bacon's avatar
Zach Bacon committed
1200
1201
1202
1203
1204
1205
1206
1207
        // don't draw the window if it was not enabled and not being drawn before
        if (!(register_LCDC & 0x20) && (value & 0x20) && gbWindowLine == -1 && register_LY > register_WY)
            gbWindowLine = 146;
        // 007 fix : don't draw the first window's 1st line if it's enable 'too late'
        // (ie. if register_LY == register_WY when enabling it)
        // and move it to the next line
        else if (!(register_LCDC & 0x20) && (value & 0x20) && (register_LY == register_WY))
            gbWindowLine = -2;
1208

Zach Bacon's avatar
Zach Bacon committed
1209
        gbMemory[0xff40] = register_LCDC = value;
1210

Zach Bacon's avatar
Zach Bacon committed
1211
1212
        if (lcdChange) {
            if ((value & 0x80) && (!register_LCDCBusy)) {
1213

Zach Bacon's avatar
Zach Bacon committed
1214
                //  if (!gbWhiteScreen && !gbSgbMask)
1215

Zach Bacon's avatar
Zach Bacon committed
1216
                //  systemDrawScreen();
1217

Zach Bacon's avatar
Zach Bacon committed
1218
                gbRegisterLYLCDCOffOn = (register_LY + 144) % 154;
1219

Zach Bacon's avatar
Zach Bacon committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
                gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS - (gbSpeed ? 2 : 1);
                gbLcdTicksDelayed = GBLCD_MODE_2_CLOCK_TICKS - (gbSpeed ? 1 : 0);
                gbLcdLYIncrementTicks = GBLY_INCREMENT_CLOCK_TICKS - (gbSpeed ? 2 : 1);
                gbLcdLYIncrementTicksDelayed = GBLY_INCREMENT_CLOCK_TICKS - (gbSpeed ? 1 : 0);
                gbLcdMode = 2;
                gbLcdModeDelayed = 2;
                gbMemory[0xff41] = register_STAT = (register_STAT & 0xfc) | 2;
                gbMemory[0xff44] = register_LY = 0x00;
                gbInt48Signal = 0;
                gbLYChangeHappened = false;
                gbLCDChangeHappened = false;
                gbWindowLine = 146;
                oldRegister_WY = 146;

                // Fix for Namco Gallery Vol.2
                // (along with updating register_LCDC at the start of 'case 0x40') :
                if (register_STAT & 0x20) {
                    // send LCD interrupt only if no interrupt 48h signal...
                    if (!gbInt48Signal) {
                        gbMemory[0xff0f] = register_IF |= 2;
                    }
                    gbInt48Signal |= 4;
                }
                gbCompareLYToLYC();
1244

Zach Bacon's avatar
Zach Bacon committed
1245
            } else {
1246

Zach Bacon's avatar
Zach Bacon committed
1247
                register_LCDCBusy = clockTicks + 6;
1248

Zach Bacon's avatar
Zach Bacon committed
1249
1250
                //used to update the screen with white lines when it's off.
                //(it looks strange, but it's pretty accurate)
1251

Zach Bacon's avatar
Zach Bacon committed
1252
                gbWhiteScreen = 0;
1253

Zach Bacon's avatar
Zach Bacon committed
1254
                gbScreenTicks = ((150 - register_LY) * GBLY_INCREMENT_CLOCK_TICKS + (49 << (gbSpeed ? 1 : 0)));
1255

Zach Bacon's avatar
Zach Bacon committed
1256
1257
1258
1259
1260
1261
1262
1263
                // disable the screen rendering
                gbScreenOn = false;
                gbLcdTicks = 0;
                gbLcdMode = 0;
                gbLcdModeDelayed = 0;
                gbMemory[0xff41] = register_STAT &= 0xfc;
                gbInt48Signal = 0;
            }
1264
        }
Zach Bacon's avatar
Zach Bacon committed
1265
        return;
1266
1267
1268
1269
    }

    // STAT
    case 0x41: {
Zach Bacon's avatar
Zach Bacon committed
1270
1271
1272
1273
        //register_STAT = (register_STAT & 0x87) |
        //      (value & 0x7c);
        gbMemory[0xff41] = register_STAT = (value & 0xf8) | (register_STAT & 0x07); // fix ?
        // GB bug from Devrs FAQ
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
        // http://www.devrs.com/gb/files/faqs.html#GBBugs
        // 2018-7-26 Backported STAT register bug behavior
        // Corrected : it happens if Lcd Mode < 2, but also if LY == LYC whatever
        // Lcd Mode is, and if !gbInt48Signal in all cases. The screen being off
        // doesn't matter (the bug will still happen).
        // That fixes 'Satoru Nakajima - F-1 Hero' crash bug.
        // Games below relies on this bug, , and are incompatible with the GBC.
        // - Road Rash: crash after player screen
        // - Zerg no Densetsu: crash right after showing a small portion of intro

        if ((gbHardware & 5)
            && (((!gbInt48Signal) && (gbLcdMode < 2) && (register_LCDC & 0x80))
            || (register_LY == register_LYC))) {
1287

1288
1289
1290
            gbMemory[0xff0f] = register_IF |=2;
        }

Zach Bacon's avatar
Zach Bacon committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
        gbInt48Signal &= ((register_STAT >> 3) & 0xF);

        if ((register_LCDC & 0x80)) {
            if ((register_STAT & 0x08) && (gbLcdMode == 0)) {
                if (!gbInt48Signal) {
                    gbMemory[0xff0f] = register_IF |= 2;
                }
                gbInt48Signal |= 1;
            }
            if ((register_STAT & 0x10) && (gbLcdMode == 1)) {
                if (!gbInt48Signal) {
                    gbMemory[0xff0f] = register_IF |= 2;
                }
                gbInt48Signal |= 2;
            }
            if ((register_STAT & 0x20) && (gbLcdMode == 2)) {
                if (!gbInt48Signal) {
                    gbMemory[0xff0f] = register_IF |= 2;
                }
                gbInt48Signal |= 4;
            }
            gbCompareLYToLYC();
1313