view src/gb/GB.cpp @ 59:3ce48d803e74

added functions to get rom/ram sizes
author Robert McIntyre <rlm@mit.edu>
date Wed, 07 Mar 2012 17:40:00 -0600
parents 3e36553d0cbf
children ef59aee6d715
line wrap: on
line source
1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <cassert>
6 #include "../Port.h"
7 #include "../NLS.h"
8 #include "GB.h"
9 #include "gbCheats.h"
10 #include "gbGlobals.h"
11 #include "gbMemory.h"
12 #include "gbSGB.h"
13 #include "gbSound.h"
14 #include "../common/unzip.h"
15 #include "../common/Util.h"
16 #include "../common/System.h"
17 #include "../common/movie.h"
18 #include "../common/vbalua.h"
20 #ifdef __GNUC__
21 #define _stricmp strcasecmp
22 #endif
24 // FIXME: constant (GB) or boolean (GBA)?!
25 #define C_FLAG 0x10
26 #define H_FLAG 0x20
27 #define N_FLAG 0x40
28 #define Z_FLAG 0x80
29 extern soundtick_t GB_USE_TICKS_AS;
31 u8 * origPix = NULL;
32 extern u8 * pix;
33 extern u32 extButtons;
34 extern bool8 capturePrevious;
35 extern int32 captureNumber;
36 extern bool8 speedup;
38 bool gbUpdateSizes();
40 // debugging
41 bool memorydebug = false;
42 char gbBuffer[2048];
44 extern u16 gbLineMix[160];
46 // mappers
47 void (*mapper)(u16, u8) = NULL;
48 void (*mapperRAM)(u16, u8) = NULL;
49 u8 (*mapperReadRAM)(u16) = NULL;
51 // registers
52 gbRegister PC;
53 gbRegister SP;
54 gbRegister AF;
55 gbRegister BC;
56 gbRegister DE;
57 gbRegister HL;
58 u16 IFF;
59 // 0xff04
60 u8 register_DIV = 0;
61 // 0xff05
62 u8 register_TIMA = 0;
63 // 0xff06
64 u8 register_TMA = 0;
65 // 0xff07
66 u8 register_TAC = 0;
67 // 0xff0f
68 u8 register_IF = 0;
69 // 0xff40
70 u8 register_LCDC = 0;
71 // 0xff41
72 u8 register_STAT = 0;
73 // 0xff42
74 u8 register_SCY = 0;
75 // 0xff43
76 u8 register_SCX = 0;
77 // 0xff44
78 u8 register_LY = 0;
79 // 0xff45
80 u8 register_LYC = 0;
81 // 0xff46
82 u8 register_DMA = 0;
83 // 0xff4a
84 u8 register_WY = 0;
85 // 0xff4b
86 u8 register_WX = 0;
87 // 0xff4f
88 u8 register_VBK = 0;
89 // 0xff51
90 u8 register_HDMA1 = 0;
91 // 0xff52
92 u8 register_HDMA2 = 0;
93 // 0xff53
94 u8 register_HDMA3 = 0;
95 // 0xff54
96 u8 register_HDMA4 = 0;
97 // 0xff55
98 u8 register_HDMA5 = 0;
99 // 0xff70
100 u8 register_SVBK = 0;
101 // 0xffff
102 u8 register_IE = 0;
104 // ticks definition
105 int32 GBDIV_CLOCK_TICKS = 64;
106 int32 GBLCD_MODE_0_CLOCK_TICKS = 51;
107 int32 GBLCD_MODE_1_CLOCK_TICKS = 1140;
108 int32 GBLCD_MODE_2_CLOCK_TICKS = 20;
109 int32 GBLCD_MODE_3_CLOCK_TICKS = 43;
110 int32 GBLY_INCREMENT_CLOCK_TICKS = 114;
111 int32 GBTIMER_MODE_0_CLOCK_TICKS = 256;
112 int32 GBTIMER_MODE_1_CLOCK_TICKS = 4;
113 int32 GBTIMER_MODE_2_CLOCK_TICKS = 16;
114 int32 GBTIMER_MODE_3_CLOCK_TICKS = 64;
115 int32 GBSERIAL_CLOCK_TICKS = 128;
116 int32 GBSYNCHRONIZE_CLOCK_TICKS = 52920;
118 // state variables
120 // interrupt
121 int32 gbInterrupt = 0;
122 int32 gbInterruptWait = 0;
123 // serial
124 int32 gbSerialOn = 0;
125 int32 gbSerialTicks = 0;
126 int32 gbSerialBits = 0;
127 // timer
128 int32 gbTimerOn = 0;
129 int32 gbTimerTicks = 0;
130 int32 gbTimerClockTicks = 0;
131 int32 gbTimerMode = 0;
132 // lcd
133 int32 gbLcdMode = 2;
134 int32 gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS;
135 int32 gbLcdLYIncrementTicks = 0;
136 // div
137 int32 gbDivTicks = GBDIV_CLOCK_TICKS;
138 // cgb
139 int32 gbVramBank = 0;
140 int32 gbWramBank = 1;
141 int32 gbHdmaSource = 0x0000;
142 int32 gbHdmaDestination = 0x8000;
143 int32 gbHdmaBytes = 0x0000;
144 int32 gbHdmaOn = 0;
145 int32 gbSpeed = 0;
146 // frame counting
147 int32 gbFrameCount = 0;
148 int32 gbFrameSkip = 0;
149 int32 gbFrameSkipCount = 0;
150 // timing
151 u32 gbLastTime = 0;
152 u32 gbElapsedTime = 0;
153 u32 gbTimeNow = 0;
154 int32 gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS;
155 int32 gbDMASpeedVersion = 1;
156 // emulator features
157 int32 gbBattery = 0;
158 int32 gbJoymask[4] = { 0, 0, 0, 0 };
160 int32 gbEchoRAMFixOn = 1;
162 static bool newFrame = true;
163 static bool pauseAfterFrameAdvance = false;
165 int32 gbRomSizes[] = { 0x00008000, // 32K
166 0x00010000, // 64K
167 0x00020000, // 128K
168 0x00040000, // 256K
169 0x00080000, // 512K
170 0x00100000, // 1024K
171 0x00200000, // 2048K
172 0x00400000, // 4096K
173 0x00800000 // 8192K
174 };
175 int32 gbRomSizesMasks[] = { 0x00007fff,
176 0x0000ffff,
177 0x0001ffff,
178 0x0003ffff,
179 0x0007ffff,
180 0x000fffff,
181 0x001fffff,
182 0x003fffff,
183 0x007fffff };
185 int32 gbRamSizes[6] = { 0x00000000, // 0K
186 0x00000800, // 2K
187 0x00002000, // 8K
188 0x00008000, // 32K
189 0x00020000, // 128K
190 0x00010000 // 64K
191 };
193 int32 gbRamSizesMasks[6] = { 0x00000000,
194 0x000007ff,
195 0x00001fff,
196 0x00007fff,
197 0x0001ffff,
198 0x0000ffff };
200 int32 gbCycles[] =
201 {
202 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
203 1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, // 0
204 1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, // 1
205 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, // 2
206 2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, // 3
207 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 4
208 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 5
209 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 6
210 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, // 7
211 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 8
212 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 9
213 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // a
214 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // b
215 2, 3, 3, 4, 3, 4, 2, 4, 2, 4, 3, 2, 3, 6, 2, 4, // c
216 2, 3, 3, 0, 3, 4, 2, 4, 2, 4, 3, 0, 3, 0, 2, 4, // d
217 3, 3, 2, 0, 0, 4, 2, 4, 4, 1, 4, 0, 0, 0, 2, 4, // e
218 3, 3, 2, 1, 0, 4, 2, 4, 3, 2, 4, 1, 0, 0, 2, 4 // f
219 };
221 int32 gbCyclesCB[] =
222 {
223 // 0 1 2 3 4 5 6 7 8 9 a b c d e f
224 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0
225 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 1
226 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 2
227 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 3
228 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 4
229 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 5
230 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 6
231 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 7
232 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 8
233 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 9
234 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // a
235 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // b
236 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // c
237 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // d
238 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // e
239 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2 // f
240 };
242 u16 DAATable[] =
243 {
244 0x0080, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700,
245 0x0800, 0x0900, 0x1020, 0x1120, 0x1220, 0x1320, 0x1420, 0x1520,
246 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500, 0x1600, 0x1700,
247 0x1800, 0x1900, 0x2020, 0x2120, 0x2220, 0x2320, 0x2420, 0x2520,
248 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2600, 0x2700,
249 0x2800, 0x2900, 0x3020, 0x3120, 0x3220, 0x3320, 0x3420, 0x3520,
250 0x3000, 0x3100, 0x3200, 0x3300, 0x3400, 0x3500, 0x3600, 0x3700,
251 0x3800, 0x3900, 0x4020, 0x4120, 0x4220, 0x4320, 0x4420, 0x4520,
252 0x4000, 0x4100, 0x4200, 0x4300, 0x4400, 0x4500, 0x4600, 0x4700,
253 0x4800, 0x4900, 0x5020, 0x5120, 0x5220, 0x5320, 0x5420, 0x5520,
254 0x5000, 0x5100, 0x5200, 0x5300, 0x5400, 0x5500, 0x5600, 0x5700,
255 0x5800, 0x5900, 0x6020, 0x6120, 0x6220, 0x6320, 0x6420, 0x6520,
256 0x6000, 0x6100, 0x6200, 0x6300, 0x6400, 0x6500, 0x6600, 0x6700,
257 0x6800, 0x6900, 0x7020, 0x7120, 0x7220, 0x7320, 0x7420, 0x7520,
258 0x7000, 0x7100, 0x7200, 0x7300, 0x7400, 0x7500, 0x7600, 0x7700,
259 0x7800, 0x7900, 0x8020, 0x8120, 0x8220, 0x8320, 0x8420, 0x8520,
260 0x8000, 0x8100, 0x8200, 0x8300, 0x8400, 0x8500, 0x8600, 0x8700,
261 0x8800, 0x8900, 0x9020, 0x9120, 0x9220, 0x9320, 0x9420, 0x9520,
262 0x9000, 0x9100, 0x9200, 0x9300, 0x9400, 0x9500, 0x9600, 0x9700,
263 0x9800, 0x9900, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530,
264 0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510, 0x0610, 0x0710,
265 0x0810, 0x0910, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530,
266 0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510, 0x1610, 0x1710,
267 0x1810, 0x1910, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530,
268 0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510, 0x2610, 0x2710,
269 0x2810, 0x2910, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530,
270 0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510, 0x3610, 0x3710,
271 0x3810, 0x3910, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530,
272 0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510, 0x4610, 0x4710,
273 0x4810, 0x4910, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530,
274 0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510, 0x5610, 0x5710,
275 0x5810, 0x5910, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530,
276 0x6010, 0x6110, 0x6210, 0x6310, 0x6410, 0x6510, 0x6610, 0x6710,
277 0x6810, 0x6910, 0x7030, 0x7130, 0x7230, 0x7330, 0x7430, 0x7530,
278 0x7010, 0x7110, 0x7210, 0x7310, 0x7410, 0x7510, 0x7610, 0x7710,
279 0x7810, 0x7910, 0x8030, 0x8130, 0x8230, 0x8330, 0x8430, 0x8530,
280 0x8010, 0x8110, 0x8210, 0x8310, 0x8410, 0x8510, 0x8610, 0x8710,
281 0x8810, 0x8910, 0x9030, 0x9130, 0x9230, 0x9330, 0x9430, 0x9530,
282 0x9010, 0x9110, 0x9210, 0x9310, 0x9410, 0x9510, 0x9610, 0x9710,
283 0x9810, 0x9910, 0xA030, 0xA130, 0xA230, 0xA330, 0xA430, 0xA530,
284 0xA010, 0xA110, 0xA210, 0xA310, 0xA410, 0xA510, 0xA610, 0xA710,
285 0xA810, 0xA910, 0xB030, 0xB130, 0xB230, 0xB330, 0xB430, 0xB530,
286 0xB010, 0xB110, 0xB210, 0xB310, 0xB410, 0xB510, 0xB610, 0xB710,
287 0xB810, 0xB910, 0xC030, 0xC130, 0xC230, 0xC330, 0xC430, 0xC530,
288 0xC010, 0xC110, 0xC210, 0xC310, 0xC410, 0xC510, 0xC610, 0xC710,
289 0xC810, 0xC910, 0xD030, 0xD130, 0xD230, 0xD330, 0xD430, 0xD530,
290 0xD010, 0xD110, 0xD210, 0xD310, 0xD410, 0xD510, 0xD610, 0xD710,
291 0xD810, 0xD910, 0xE030, 0xE130, 0xE230, 0xE330, 0xE430, 0xE530,
292 0xE010, 0xE110, 0xE210, 0xE310, 0xE410, 0xE510, 0xE610, 0xE710,
293 0xE810, 0xE910, 0xF030, 0xF130, 0xF230, 0xF330, 0xF430, 0xF530,
294 0xF010, 0xF110, 0xF210, 0xF310, 0xF410, 0xF510, 0xF610, 0xF710,
295 0xF810, 0xF910, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530,
296 0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510, 0x0610, 0x0710,
297 0x0810, 0x0910, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530,
298 0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510, 0x1610, 0x1710,
299 0x1810, 0x1910, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530,
300 0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510, 0x2610, 0x2710,
301 0x2810, 0x2910, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530,
302 0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510, 0x3610, 0x3710,
303 0x3810, 0x3910, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530,
304 0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510, 0x4610, 0x4710,
305 0x4810, 0x4910, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530,
306 0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510, 0x5610, 0x5710,
307 0x5810, 0x5910, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530,
308 0x0600, 0x0700, 0x0800, 0x0900, 0x0A00, 0x0B00, 0x0C00, 0x0D00,
309 0x0E00, 0x0F00, 0x1020, 0x1120, 0x1220, 0x1320, 0x1420, 0x1520,
310 0x1600, 0x1700, 0x1800, 0x1900, 0x1A00, 0x1B00, 0x1C00, 0x1D00,
311 0x1E00, 0x1F00, 0x2020, 0x2120, 0x2220, 0x2320, 0x2420, 0x2520,
312 0x2600, 0x2700, 0x2800, 0x2900, 0x2A00, 0x2B00, 0x2C00, 0x2D00,
313 0x2E00, 0x2F00, 0x3020, 0x3120, 0x3220, 0x3320, 0x3420, 0x3520,
314 0x3600, 0x3700, 0x3800, 0x3900, 0x3A00, 0x3B00, 0x3C00, 0x3D00,
315 0x3E00, 0x3F00, 0x4020, 0x4120, 0x4220, 0x4320, 0x4420, 0x4520,
316 0x4600, 0x4700, 0x4800, 0x4900, 0x4A00, 0x4B00, 0x4C00, 0x4D00,
317 0x4E00, 0x4F00, 0x5020, 0x5120, 0x5220, 0x5320, 0x5420, 0x5520,
318 0x5600, 0x5700, 0x5800, 0x5900, 0x5A00, 0x5B00, 0x5C00, 0x5D00,
319 0x5E00, 0x5F00, 0x6020, 0x6120, 0x6220, 0x6320, 0x6420, 0x6520,
320 0x6600, 0x6700, 0x6800, 0x6900, 0x6A00, 0x6B00, 0x6C00, 0x6D00,
321 0x6E00, 0x6F00, 0x7020, 0x7120, 0x7220, 0x7320, 0x7420, 0x7520,
322 0x7600, 0x7700, 0x7800, 0x7900, 0x7A00, 0x7B00, 0x7C00, 0x7D00,
323 0x7E00, 0x7F00, 0x8020, 0x8120, 0x8220, 0x8320, 0x8420, 0x8520,
324 0x8600, 0x8700, 0x8800, 0x8900, 0x8A00, 0x8B00, 0x8C00, 0x8D00,
325 0x8E00, 0x8F00, 0x9020, 0x9120, 0x9220, 0x9320, 0x9420, 0x9520,
326 0x9600, 0x9700, 0x9800, 0x9900, 0x9A00, 0x9B00, 0x9C00, 0x9D00,
327 0x9E00, 0x9F00, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530,
328 0x0610, 0x0710, 0x0810, 0x0910, 0x0A10, 0x0B10, 0x0C10, 0x0D10,
329 0x0E10, 0x0F10, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530,
330 0x1610, 0x1710, 0x1810, 0x1910, 0x1A10, 0x1B10, 0x1C10, 0x1D10,
331 0x1E10, 0x1F10, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530,
332 0x2610, 0x2710, 0x2810, 0x2910, 0x2A10, 0x2B10, 0x2C10, 0x2D10,
333 0x2E10, 0x2F10, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530,
334 0x3610, 0x3710, 0x3810, 0x3910, 0x3A10, 0x3B10, 0x3C10, 0x3D10,
335 0x3E10, 0x3F10, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530,
336 0x4610, 0x4710, 0x4810, 0x4910, 0x4A10, 0x4B10, 0x4C10, 0x4D10,
337 0x4E10, 0x4F10, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530,
338 0x5610, 0x5710, 0x5810, 0x5910, 0x5A10, 0x5B10, 0x5C10, 0x5D10,
339 0x5E10, 0x5F10, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530,
340 0x6610, 0x6710, 0x6810, 0x6910, 0x6A10, 0x6B10, 0x6C10, 0x6D10,
341 0x6E10, 0x6F10, 0x7030, 0x7130, 0x7230, 0x7330, 0x7430, 0x7530,
342 0x7610, 0x7710, 0x7810, 0x7910, 0x7A10, 0x7B10, 0x7C10, 0x7D10,
343 0x7E10, 0x7F10, 0x8030, 0x8130, 0x8230, 0x8330, 0x8430, 0x8530,
344 0x8610, 0x8710, 0x8810, 0x8910, 0x8A10, 0x8B10, 0x8C10, 0x8D10,
345 0x8E10, 0x8F10, 0x9030, 0x9130, 0x9230, 0x9330, 0x9430, 0x9530,
346 0x9610, 0x9710, 0x9810, 0x9910, 0x9A10, 0x9B10, 0x9C10, 0x9D10,
347 0x9E10, 0x9F10, 0xA030, 0xA130, 0xA230, 0xA330, 0xA430, 0xA530,
348 0xA610, 0xA710, 0xA810, 0xA910, 0xAA10, 0xAB10, 0xAC10, 0xAD10,
349 0xAE10, 0xAF10, 0xB030, 0xB130, 0xB230, 0xB330, 0xB430, 0xB530,
350 0xB610, 0xB710, 0xB810, 0xB910, 0xBA10, 0xBB10, 0xBC10, 0xBD10,
351 0xBE10, 0xBF10, 0xC030, 0xC130, 0xC230, 0xC330, 0xC430, 0xC530,
352 0xC610, 0xC710, 0xC810, 0xC910, 0xCA10, 0xCB10, 0xCC10, 0xCD10,
353 0xCE10, 0xCF10, 0xD030, 0xD130, 0xD230, 0xD330, 0xD430, 0xD530,
354 0xD610, 0xD710, 0xD810, 0xD910, 0xDA10, 0xDB10, 0xDC10, 0xDD10,
355 0xDE10, 0xDF10, 0xE030, 0xE130, 0xE230, 0xE330, 0xE430, 0xE530,
356 0xE610, 0xE710, 0xE810, 0xE910, 0xEA10, 0xEB10, 0xEC10, 0xED10,
357 0xEE10, 0xEF10, 0xF030, 0xF130, 0xF230, 0xF330, 0xF430, 0xF530,
358 0xF610, 0xF710, 0xF810, 0xF910, 0xFA10, 0xFB10, 0xFC10, 0xFD10,
359 0xFE10, 0xFF10, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530,
360 0x0610, 0x0710, 0x0810, 0x0910, 0x0A10, 0x0B10, 0x0C10, 0x0D10,
361 0x0E10, 0x0F10, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530,
362 0x1610, 0x1710, 0x1810, 0x1910, 0x1A10, 0x1B10, 0x1C10, 0x1D10,
363 0x1E10, 0x1F10, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530,
364 0x2610, 0x2710, 0x2810, 0x2910, 0x2A10, 0x2B10, 0x2C10, 0x2D10,
365 0x2E10, 0x2F10, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530,
366 0x3610, 0x3710, 0x3810, 0x3910, 0x3A10, 0x3B10, 0x3C10, 0x3D10,
367 0x3E10, 0x3F10, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530,
368 0x4610, 0x4710, 0x4810, 0x4910, 0x4A10, 0x4B10, 0x4C10, 0x4D10,
369 0x4E10, 0x4F10, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530,
370 0x5610, 0x5710, 0x5810, 0x5910, 0x5A10, 0x5B10, 0x5C10, 0x5D10,
371 0x5E10, 0x5F10, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530,
372 0x00C0, 0x0140, 0x0240, 0x0340, 0x0440, 0x0540, 0x0640, 0x0740,
373 0x0840, 0x0940, 0x0440, 0x0540, 0x0640, 0x0740, 0x0840, 0x0940,
374 0x1040, 0x1140, 0x1240, 0x1340, 0x1440, 0x1540, 0x1640, 0x1740,
375 0x1840, 0x1940, 0x1440, 0x1540, 0x1640, 0x1740, 0x1840, 0x1940,
376 0x2040, 0x2140, 0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740,
377 0x2840, 0x2940, 0x2440, 0x2540, 0x2640, 0x2740, 0x2840, 0x2940,
378 0x3040, 0x3140, 0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740,
379 0x3840, 0x3940, 0x3440, 0x3540, 0x3640, 0x3740, 0x3840, 0x3940,
380 0x4040, 0x4140, 0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740,
381 0x4840, 0x4940, 0x4440, 0x4540, 0x4640, 0x4740, 0x4840, 0x4940,
382 0x5040, 0x5140, 0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740,
383 0x5840, 0x5940, 0x5440, 0x5540, 0x5640, 0x5740, 0x5840, 0x5940,
384 0x6040, 0x6140, 0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740,
385 0x6840, 0x6940, 0x6440, 0x6540, 0x6640, 0x6740, 0x6840, 0x6940,
386 0x7040, 0x7140, 0x7240, 0x7340, 0x7440, 0x7540, 0x7640, 0x7740,
387 0x7840, 0x7940, 0x7440, 0x7540, 0x7640, 0x7740, 0x7840, 0x7940,
388 0x8040, 0x8140, 0x8240, 0x8340, 0x8440, 0x8540, 0x8640, 0x8740,
389 0x8840, 0x8940, 0x8440, 0x8540, 0x8640, 0x8740, 0x8840, 0x8940,
390 0x9040, 0x9140, 0x9240, 0x9340, 0x9440, 0x9540, 0x9640, 0x9740,
391 0x9840, 0x9940, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
392 0x4050, 0x4150, 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750,
393 0x4850, 0x4950, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
394 0x5050, 0x5150, 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750,
395 0x5850, 0x5950, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
396 0x6050, 0x6150, 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750,
397 0x6850, 0x6950, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
398 0x7050, 0x7150, 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750,
399 0x7850, 0x7950, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
400 0x8050, 0x8150, 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750,
401 0x8850, 0x8950, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
402 0x9050, 0x9150, 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750,
403 0x9850, 0x9950, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
404 0xA050, 0xA150, 0xA250, 0xA350, 0xA450, 0xA550, 0xA650, 0xA750,
405 0xA850, 0xA950, 0xA450, 0xA550, 0xA650, 0xA750, 0xA850, 0xA950,
406 0xB050, 0xB150, 0xB250, 0xB350, 0xB450, 0xB550, 0xB650, 0xB750,
407 0xB850, 0xB950, 0xB450, 0xB550, 0xB650, 0xB750, 0xB850, 0xB950,
408 0xC050, 0xC150, 0xC250, 0xC350, 0xC450, 0xC550, 0xC650, 0xC750,
409 0xC850, 0xC950, 0xC450, 0xC550, 0xC650, 0xC750, 0xC850, 0xC950,
410 0xD050, 0xD150, 0xD250, 0xD350, 0xD450, 0xD550, 0xD650, 0xD750,
411 0xD850, 0xD950, 0xD450, 0xD550, 0xD650, 0xD750, 0xD850, 0xD950,
412 0xE050, 0xE150, 0xE250, 0xE350, 0xE450, 0xE550, 0xE650, 0xE750,
413 0xE850, 0xE950, 0xE450, 0xE550, 0xE650, 0xE750, 0xE850, 0xE950,
414 0xF050, 0xF150, 0xF250, 0xF350, 0xF450, 0xF550, 0xF650, 0xF750,
415 0xF850, 0xF950, 0xF450, 0xF550, 0xF650, 0xF750, 0xF850, 0xF950,
416 0x00D0, 0x0150, 0x0250, 0x0350, 0x0450, 0x0550, 0x0650, 0x0750,
417 0x0850, 0x0950, 0x0450, 0x0550, 0x0650, 0x0750, 0x0850, 0x0950,
418 0x1050, 0x1150, 0x1250, 0x1350, 0x1450, 0x1550, 0x1650, 0x1750,
419 0x1850, 0x1950, 0x1450, 0x1550, 0x1650, 0x1750, 0x1850, 0x1950,
420 0x2050, 0x2150, 0x2250, 0x2350, 0x2450, 0x2550, 0x2650, 0x2750,
421 0x2850, 0x2950, 0x2450, 0x2550, 0x2650, 0x2750, 0x2850, 0x2950,
422 0x3050, 0x3150, 0x3250, 0x3350, 0x3450, 0x3550, 0x3650, 0x3750,
423 0x3850, 0x3950, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
424 0x4050, 0x4150, 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750,
425 0x4850, 0x4950, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
426 0x5050, 0x5150, 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750,
427 0x5850, 0x5950, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
428 0x6050, 0x6150, 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750,
429 0x6850, 0x6950, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
430 0x7050, 0x7150, 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750,
431 0x7850, 0x7950, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
432 0x8050, 0x8150, 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750,
433 0x8850, 0x8950, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
434 0x9050, 0x9150, 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750,
435 0x9850, 0x9950, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
436 0xFA60, 0xFB60, 0xFC60, 0xFD60, 0xFE60, 0xFF60, 0x00C0, 0x0140,
437 0x0240, 0x0340, 0x0440, 0x0540, 0x0640, 0x0740, 0x0840, 0x0940,
438 0x0A60, 0x0B60, 0x0C60, 0x0D60, 0x0E60, 0x0F60, 0x1040, 0x1140,
439 0x1240, 0x1340, 0x1440, 0x1540, 0x1640, 0x1740, 0x1840, 0x1940,
440 0x1A60, 0x1B60, 0x1C60, 0x1D60, 0x1E60, 0x1F60, 0x2040, 0x2140,
441 0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740, 0x2840, 0x2940,
442 0x2A60, 0x2B60, 0x2C60, 0x2D60, 0x2E60, 0x2F60, 0x3040, 0x3140,
443 0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740, 0x3840, 0x3940,
444 0x3A60, 0x3B60, 0x3C60, 0x3D60, 0x3E60, 0x3F60, 0x4040, 0x4140,
445 0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740, 0x4840, 0x4940,
446 0x4A60, 0x4B60, 0x4C60, 0x4D60, 0x4E60, 0x4F60, 0x5040, 0x5140,
447 0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740, 0x5840, 0x5940,
448 0x5A60, 0x5B60, 0x5C60, 0x5D60, 0x5E60, 0x5F60, 0x6040, 0x6140,
449 0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740, 0x6840, 0x6940,
450 0x6A60, 0x6B60, 0x6C60, 0x6D60, 0x6E60, 0x6F60, 0x7040, 0x7140,
451 0x7240, 0x7340, 0x7440, 0x7540, 0x7640, 0x7740, 0x7840, 0x7940,
452 0x7A60, 0x7B60, 0x7C60, 0x7D60, 0x7E60, 0x7F60, 0x8040, 0x8140,
453 0x8240, 0x8340, 0x8440, 0x8540, 0x8640, 0x8740, 0x8840, 0x8940,
454 0x8A60, 0x8B60, 0x8C60, 0x8D60, 0x8E60, 0x8F60, 0x9040, 0x9140,
455 0x9240, 0x9340, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
456 0x3A70, 0x3B70, 0x3C70, 0x3D70, 0x3E70, 0x3F70, 0x4050, 0x4150,
457 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
458 0x4A70, 0x4B70, 0x4C70, 0x4D70, 0x4E70, 0x4F70, 0x5050, 0x5150,
459 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
460 0x5A70, 0x5B70, 0x5C70, 0x5D70, 0x5E70, 0x5F70, 0x6050, 0x6150,
461 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
462 0x6A70, 0x6B70, 0x6C70, 0x6D70, 0x6E70, 0x6F70, 0x7050, 0x7150,
463 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
464 0x7A70, 0x7B70, 0x7C70, 0x7D70, 0x7E70, 0x7F70, 0x8050, 0x8150,
465 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
466 0x8A70, 0x8B70, 0x8C70, 0x8D70, 0x8E70, 0x8F70, 0x9050, 0x9150,
467 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
468 0x9A70, 0x9B70, 0x9C70, 0x9D70, 0x9E70, 0x9F70, 0xA050, 0xA150,
469 0xA250, 0xA350, 0xA450, 0xA550, 0xA650, 0xA750, 0xA850, 0xA950,
470 0xAA70, 0xAB70, 0xAC70, 0xAD70, 0xAE70, 0xAF70, 0xB050, 0xB150,
471 0xB250, 0xB350, 0xB450, 0xB550, 0xB650, 0xB750, 0xB850, 0xB950,
472 0xBA70, 0xBB70, 0xBC70, 0xBD70, 0xBE70, 0xBF70, 0xC050, 0xC150,
473 0xC250, 0xC350, 0xC450, 0xC550, 0xC650, 0xC750, 0xC850, 0xC950,
474 0xCA70, 0xCB70, 0xCC70, 0xCD70, 0xCE70, 0xCF70, 0xD050, 0xD150,
475 0xD250, 0xD350, 0xD450, 0xD550, 0xD650, 0xD750, 0xD850, 0xD950,
476 0xDA70, 0xDB70, 0xDC70, 0xDD70, 0xDE70, 0xDF70, 0xE050, 0xE150,
477 0xE250, 0xE350, 0xE450, 0xE550, 0xE650, 0xE750, 0xE850, 0xE950,
478 0xEA70, 0xEB70, 0xEC70, 0xED70, 0xEE70, 0xEF70, 0xF050, 0xF150,
479 0xF250, 0xF350, 0xF450, 0xF550, 0xF650, 0xF750, 0xF850, 0xF950,
480 0xFA70, 0xFB70, 0xFC70, 0xFD70, 0xFE70, 0xFF70, 0x00D0, 0x0150,
481 0x0250, 0x0350, 0x0450, 0x0550, 0x0650, 0x0750, 0x0850, 0x0950,
482 0x0A70, 0x0B70, 0x0C70, 0x0D70, 0x0E70, 0x0F70, 0x1050, 0x1150,
483 0x1250, 0x1350, 0x1450, 0x1550, 0x1650, 0x1750, 0x1850, 0x1950,
484 0x1A70, 0x1B70, 0x1C70, 0x1D70, 0x1E70, 0x1F70, 0x2050, 0x2150,
485 0x2250, 0x2350, 0x2450, 0x2550, 0x2650, 0x2750, 0x2850, 0x2950,
486 0x2A70, 0x2B70, 0x2C70, 0x2D70, 0x2E70, 0x2F70, 0x3050, 0x3150,
487 0x3250, 0x3350, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
488 0x3A70, 0x3B70, 0x3C70, 0x3D70, 0x3E70, 0x3F70, 0x4050, 0x4150,
489 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
490 0x4A70, 0x4B70, 0x4C70, 0x4D70, 0x4E70, 0x4F70, 0x5050, 0x5150,
491 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
492 0x5A70, 0x5B70, 0x5C70, 0x5D70, 0x5E70, 0x5F70, 0x6050, 0x6150,
493 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
494 0x6A70, 0x6B70, 0x6C70, 0x6D70, 0x6E70, 0x6F70, 0x7050, 0x7150,
495 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
496 0x7A70, 0x7B70, 0x7C70, 0x7D70, 0x7E70, 0x7F70, 0x8050, 0x8150,
497 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
498 0x8A70, 0x8B70, 0x8C70, 0x8D70, 0x8E70, 0x8F70, 0x9050, 0x9150,
499 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
500 };
502 u8 ZeroTable[] =
503 {
504 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
505 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
506 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
507 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
508 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
509 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
510 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
511 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
512 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
513 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
514 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
515 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
516 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
517 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
518 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
519 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
520 };
522 #define GBSAVE_GAME_VERSION_1 1
523 #define GBSAVE_GAME_VERSION_2 2
524 #define GBSAVE_GAME_VERSION_3 3
525 #define GBSAVE_GAME_VERSION_4 4
526 #define GBSAVE_GAME_VERSION_5 5
527 #define GBSAVE_GAME_VERSION_6 6
528 #define GBSAVE_GAME_VERSION_7 7
529 #define GBSAVE_GAME_VERSION_8 8
530 #define GBSAVE_GAME_VERSION_9 9
531 #define GBSAVE_GAME_VERSION_10 10
532 #define GBSAVE_GAME_VERSION_11 11
533 #define GBSAVE_GAME_VERSION_12 12
534 #define GBSAVE_GAME_VERSION_13 13
535 #define GBSAVE_GAME_VERSION GBSAVE_GAME_VERSION_13
537 int inline gbGetValue(int min, int max, int v)
538 {
539 return (int)(min + (float)(max - min) * (2.0 * (v / 31.0) - (v / 31.0) * (v / 31.0)));
540 }
542 void gbGenFilter()
543 {
544 for (int r = 0; r < 32; r++)
545 {
546 for (int g = 0; g < 32; g++)
547 {
548 for (int b = 0; b < 32; b++)
549 {
550 int nr = gbGetValue(gbGetValue(4, 14, g),
551 gbGetValue(24, 29, g), r) - 4;
552 int ng = gbGetValue(gbGetValue(4 + gbGetValue(0, 5, r),
553 14 + gbGetValue(0, 3, r), b),
554 gbGetValue(24 + gbGetValue(0, 3, r),
555 29 + gbGetValue(0, 1, r), b), g) - 4;
556 int nb = gbGetValue(gbGetValue(4 + gbGetValue(0, 5, r),
557 14 + gbGetValue(0, 3, r), g),
558 gbGetValue(24 + gbGetValue(0, 3, r),
559 29 + gbGetValue(0, 1, r), g), b) - 4;
560 gbColorFilter[(b << 10) | (g << 5) | r] = (nb << 10) | (ng << 5) | nr;
561 }
562 }
563 }
564 }
566 void gbCopyMemory(u16 d, u16 s, int count)
567 {
568 while (count)
569 {
570 gbWriteMemoryQuick(d, gbReadMemoryQuick(s));
571 s++;
572 d++;
573 count--;
574 }
575 }
577 void gbDoHdma()
578 {
579 gbCopyMemory(gbHdmaDestination, gbHdmaSource, 0x10);
581 gbHdmaDestination += 0x10;
582 gbHdmaSource += 0x10;
584 register_HDMA2 = (register_HDMA2 + 0x10) & 0xFF;
585 if (register_HDMA2 == 0x00)
586 register_HDMA1++;
588 register_HDMA4 = (register_HDMA4 + 0x10) & 0xFF;
589 if (register_HDMA4 == 0x00)
590 register_HDMA3++;
592 if (gbHdmaDestination == 0x96b0)
593 gbHdmaBytes = gbHdmaBytes;
594 gbHdmaBytes -= 0x10;
595 register_HDMA5--;
596 if (register_HDMA5 == 0xff)
597 gbHdmaOn = 0;
598 }
600 // fix for Harley and Lego Racers
601 void gbCompareLYToLYC()
602 {
603 if (register_LY == register_LYC)
604 {
605 // mark that we have a match
606 register_STAT |= 4;
608 // check if we need an interrupt
609 if ((register_STAT & 0x40) && (register_IE & 2))
610 gbInterrupt |= 2;
611 }
612 else // no match
613 register_STAT &= 0xfb;
614 }
616 // FIXME: horrible kludge to workaround the frame timing bug
617 static int32 s_gbJoymask[4] = { 0, 0, 0, 0 };
619 void gbWriteMemoryWrapped(register u16 address, register u8 value)
620 {
621 if (address < 0x8000)
622 {
623 #ifndef FINAL_VERSION
624 if (memorydebug && (address > 0x3fff || address < 0x2000))
625 {
626 log("Memory register write %04x=%02x PC=%04x\n",
627 address,
628 value,
629 PC.W);
630 }
631 #endif
632 if (mapper)
633 (*mapper)(address, value);
634 return;
635 }
637 if (address < 0xa000)
638 {
639 gbWriteMemoryQuick(address, value);
640 return;
641 }
643 if (address < 0xc000)
644 {
645 #ifndef FINAL_VERSION
646 if (memorydebug)
647 {
648 log("Memory register write %04x=%02x PC=%04x\n",
649 address,
650 value,
651 PC.W);
652 }
653 #endif
655 if (mapper)
656 (*mapperRAM)(address, value);
657 return;
658 }
660 if (address < 0xfe00)
661 {
662 gbWriteMemoryQuick(address, value);
663 return;
664 }
666 if (address < 0xff00)
667 {
668 gbMemory[address] = value;
669 return;
670 }
672 switch (address & 0x00ff)
673 {
674 case 0x00:
675 {
676 gbMemory[0xff00] = ((gbMemory[0xff00] & 0xcf) |
677 (value & 0x30));
678 if (gbSgbMode)
679 {
680 gbSgbDoBitTransfer(value);
681 }
683 return;
684 }
686 case 0x01:
687 {
688 gbMemory[0xff01] = value;
689 return;
690 }
692 // serial control
693 case 0x02:
694 {
695 gbSerialOn = (value & 0x80);
696 gbMemory[0xff02] = value;
697 if (gbSerialOn)
698 {
699 gbSerialTicks = GBSERIAL_CLOCK_TICKS;
700 #ifdef LINK_EMULATION
701 if (linkConnected)
702 {
703 if (value & 1)
704 {
705 linkSendByte(0x100 | gbMemory[0xFF01]);
706 Sleep(5);
707 }
708 }
709 #endif
710 }
712 gbSerialBits = 0;
713 return;
714 }
716 // DIV register resets on any write
717 case 0x04:
718 {
719 register_DIV = 0;
720 return;
721 }
722 case 0x05:
723 register_TIMA = value;
724 return;
726 case 0x06:
727 register_TMA = value;
728 return;
730 // TIMER control
731 case 0x07:
732 {
733 register_TAC = value;
735 gbTimerOn = (value & 4);
736 gbTimerMode = value & 3;
737 // register_TIMA = register_TMA;
738 switch (gbTimerMode)
739 {
740 case 0:
741 gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_0_CLOCK_TICKS;
742 break;
743 case 1:
744 gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_1_CLOCK_TICKS;
745 break;
746 case 2:
747 gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_2_CLOCK_TICKS;
748 break;
749 case 3:
750 gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_3_CLOCK_TICKS;
751 break;
752 }
753 return;
754 }
756 case 0x0f:
757 {
758 register_IF = value;
759 gbInterrupt = value;
760 return;
761 }
763 case 0x10:
764 case 0x11:
765 case 0x12:
766 case 0x13:
767 case 0x14:
768 case 0x15:
769 case 0x16:
770 case 0x17:
771 case 0x18:
772 case 0x19:
773 case 0x1a:
774 case 0x1b:
775 case 0x1c:
776 case 0x1d:
777 case 0x1e:
778 case 0x1f:
779 case 0x20:
780 case 0x21:
781 case 0x22:
782 case 0x23:
783 case 0x24:
784 case 0x25:
785 case 0x26:
786 {
787 SOUND_EVENT(address, value);
788 return;
789 }
790 case 0x40:
791 {
792 int lcdChange = (register_LCDC & 0x80) ^ (value & 0x80);
794 if (lcdChange)
795 {
796 if (value & 0x80)
797 {
798 gbLcdTicks = GBLCD_MODE_1_CLOCK_TICKS;
799 gbLcdMode = 0;
800 register_STAT &= 0xfc;
801 register_LY = 0x00;
802 // FIXME: horrible workaround
803 if (gbNullInputHackTempEnabled && !useOldFrameTiming)
804 memcpy(gbJoymask, s_gbJoymask, sizeof(gbJoymask));
805 }
806 else
807 {
808 gbLcdTicks = 0;
809 gbLcdMode = 0;
810 register_STAT &= 0xfc;
811 register_LY = 0x00;
812 // FIXME: horrible workaround
813 memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask));
814 if (gbNullInputHackTempEnabled && !useOldFrameTiming)
815 memset(gbJoymask, 0, sizeof(gbJoymask));
816 }
817 // compareLYToLYC();
818 }
819 // don't draw the window if it was not enabled and not being drawn before
820 if (!(register_LCDC & 0x20) && (value & 0x20) && gbWindowLine == -1 &&
821 register_LY > register_WY)
822 gbWindowLine = 144;
824 register_LCDC = value;
826 return;
827 }
829 // STAT
830 case 0x41:
831 {
832 //register_STAT = (register_STAT & 0x87) |
833 // (value & 0x7c);
834 register_STAT = (value & 0xf8) | (register_STAT & 0x07); // fix ?
835 // GB bug from Devrs FAQ
836 if (!gbCgbMode && (register_LCDC & 0x80) && gbLcdMode < 2)
837 gbInterrupt |= 2;
838 return;
839 }
841 // SCY
842 case 0x42:
843 {
844 register_SCY = value;
845 return;
846 }
848 // SCX
849 case 0x43:
850 {
851 register_SCX = value;
852 return;
853 }
855 // LY
856 case 0x44:
857 {
858 // read only
859 return;
860 }
862 // LYC
863 case 0x45:
864 {
865 register_LYC = value;
866 if ((register_LCDC & 0x80))
867 {
868 gbCompareLYToLYC();
869 }
870 return;
871 }
873 // DMA!
874 case 0x46:
875 {
876 int source = value * 0x0100;
878 gbCopyMemory(0xfe00,
879 source,
880 0xa0);
881 register_DMA = value;
882 return;
883 }
885 // BGP
886 case 0x47:
887 {
888 gbBgp[0] = value & 0x03;
889 gbBgp[1] = (value & 0x0c) >> 2;
890 gbBgp[2] = (value & 0x30) >> 4;
891 gbBgp[3] = (value & 0xc0) >> 6;
892 break;
893 }
895 // OBP0
896 case 0x48:
897 {
898 gbObp0[0] = value & 0x03;
899 gbObp0[1] = (value & 0x0c) >> 2;
900 gbObp0[2] = (value & 0x30) >> 4;
901 gbObp0[3] = (value & 0xc0) >> 6;
902 break;
903 }
905 // OBP1
906 case 0x49:
907 {
908 gbObp1[0] = value & 0x03;
909 gbObp1[1] = (value & 0x0c) >> 2;
910 gbObp1[2] = (value & 0x30) >> 4;
911 gbObp1[3] = (value & 0xc0) >> 6;
912 break;
913 }
915 case 0x4a:
916 register_WY = value;
917 return;
919 case 0x4b:
920 register_WX = value;
921 return;
923 // KEY1
924 case 0x4d:
925 {
926 if (gbCgbMode)
927 {
928 gbMemory[0xff4d] = (gbMemory[0xff4d] & 0x80) | (value & 1);
929 return;
930 }
931 break;
932 }
934 // VBK
935 case 0x4f:
936 {
937 if (gbCgbMode)
938 {
939 value = value & 1;
940 if (value == gbVramBank)
941 return;
943 int vramAddress = value * 0x2000;
944 gbMemoryMap[0x08] = &gbVram[vramAddress];
945 gbMemoryMap[0x09] = &gbVram[vramAddress + 0x1000];
947 gbVramBank = value;
948 register_VBK = value;
949 }
950 return;
951 break;
952 }
954 // HDMA1
955 case 0x51:
956 {
957 if (gbCgbMode)
958 {
959 if (value > 0x7f && value < 0xa0)
960 value = 0;
962 gbHdmaSource = (value << 8) | (register_HDMA2 & 0xf0);
964 register_HDMA1 = value;
965 return;
966 }
967 break;
968 }
970 // HDMA2
971 case 0x52:
972 {
973 if (gbCgbMode)
974 {
975 value = value & 0xf0;
977 gbHdmaSource = (register_HDMA1 << 8) | (value);
979 register_HDMA2 = value;
980 return;
981 }
982 break;
983 }
985 // HDMA3
986 case 0x53:
987 {
988 if (gbCgbMode)
989 {
990 value = value & 0x1f;
991 gbHdmaDestination = (value << 8) | (register_HDMA4 & 0xf0);
992 gbHdmaDestination += 0x8000;
993 register_HDMA3 = value;
994 return;
995 }
996 break;
997 }
999 // HDMA4
1000 case 0x54:
1002 if (gbCgbMode)
1004 value = value & 0xf0;
1005 gbHdmaDestination = ((register_HDMA3 & 0x1f) << 8) | value;
1006 gbHdmaDestination += 0x8000;
1007 register_HDMA4 = value;
1008 return;
1010 break;
1013 // HDMA5
1014 case 0x55:
1016 if (gbCgbMode)
1018 gbHdmaBytes = 16 + (value & 0x7f) * 16;
1019 if (gbHdmaOn)
1021 if (value & 0x80)
1023 register_HDMA5 = (value & 0x7f);
1025 else
1027 register_HDMA5 = 0xff;
1028 gbHdmaOn = 0;
1031 else
1033 if (value & 0x80)
1035 gbHdmaOn = 1;
1036 register_HDMA5 = value & 0x7f;
1037 if (gbLcdMode == 0)
1038 gbDoHdma();
1040 else
1042 // we need to take the time it takes to complete the transfer into
1043 // account... according to GB DEV FAQs, the setup time is the same
1044 // for single and double speed, but the actual transfer takes the
1045 // same time // (is that a typo?)
1046 switch (gbDMASpeedVersion)
1048 case 1: // I believe this is more correct
1049 // the lower 7 bits of FF55 specify the Transfer Length (divided by 16, minus 1)
1050 // and we make gbDmaTicks twice as many cycles at double speed to make the transfer take the same time
1051 if (gbSpeed)
1052 gbDmaTicks = 16 * ((value & 0x7f) + 1);
1053 else
1054 gbDmaTicks = 8 * ((value & 0x7f) + 1);
1055 break;
1056 case 0: // here for backward compatibility
1057 // I think this was a guess that approximates the above in most but not all games
1058 if (gbSpeed)
1059 gbDmaTicks = 231 + 16 * (value & 0x7f);
1060 else
1061 gbDmaTicks = 231 + 8 * (value & 0x7f);
1062 break;
1063 default: // shouldn't happen
1064 //assert(0);
1065 break;
1067 gbCopyMemory(gbHdmaDestination, gbHdmaSource, gbHdmaBytes);
1068 gbHdmaDestination += gbHdmaBytes;
1069 gbHdmaSource += gbHdmaBytes;
1071 register_HDMA3 = ((gbHdmaDestination - 0x8000) >> 8) & 0x1f;
1072 register_HDMA4 = gbHdmaDestination & 0xf0;
1073 register_HDMA1 = (gbHdmaSource >> 8) & 0xff;
1074 register_HDMA2 = gbHdmaSource & 0xf0;
1077 return;
1079 break;
1082 // BCPS
1083 case 0x68:
1085 if (gbCgbMode)
1087 int paletteIndex = (value & 0x3f) >> 1;
1088 int paletteHiLo = (value & 0x01);
1090 gbMemory[0xff68] = value;
1091 gbMemory[0xff69] = (paletteHiLo ?
1092 (gbPalette[paletteIndex] >> 8) :
1093 (gbPalette[paletteIndex] & 0x00ff));
1094 return;
1096 break;
1099 // BCPD
1100 case 0x69:
1102 if (gbCgbMode)
1104 int v = gbMemory[0xff68];
1105 int paletteIndex = (v & 0x3f) >> 1;
1106 int paletteHiLo = (v & 0x01);
1107 gbMemory[0xff69] = value;
1108 gbPalette[paletteIndex] = (paletteHiLo ?
1109 ((value << 8) | (gbPalette[paletteIndex] & 0xff)) :
1110 ((gbPalette[paletteIndex] & 0xff00) | (value))) & 0x7fff;
1112 if (gbMemory[0xff68] & 0x80)
1114 int index = ((gbMemory[0xff68] & 0x3f) + 1) & 0x3f;
1116 gbMemory[0xff68] = (gbMemory[0xff68] & 0x80) | index;
1118 gbMemory[0xff69] = (index & 1 ?
1119 (gbPalette[index >> 1] >> 8) :
1120 (gbPalette[index >> 1] & 0x00ff));
1122 return;
1124 break;
1127 // OCPS
1128 case 0x6a:
1130 if (gbCgbMode)
1132 int paletteIndex = (value & 0x3f) >> 1;
1133 int paletteHiLo = (value & 0x01);
1135 paletteIndex += 32;
1137 gbMemory[0xff6a] = value;
1138 gbMemory[0xff6b] = (paletteHiLo ?
1139 (gbPalette[paletteIndex] >> 8) :
1140 (gbPalette[paletteIndex] & 0x00ff));
1141 return;
1143 break;
1146 // OCPD
1147 case 0x6b:
1149 if (gbCgbMode)
1151 int v = gbMemory[0xff6a];
1152 int paletteIndex = (v & 0x3f) >> 1;
1153 int paletteHiLo = (v & 0x01);
1155 paletteIndex += 32;
1157 gbMemory[0xff6b] = value;
1158 gbPalette[paletteIndex] = (paletteHiLo ?
1159 ((value << 8) | (gbPalette[paletteIndex] & 0xff)) :
1160 ((gbPalette[paletteIndex] & 0xff00) | (value))) & 0x7fff;
1161 if (gbMemory[0xff6a] & 0x80)
1163 int index = ((gbMemory[0xff6a] & 0x3f) + 1) & 0x3f;
1165 gbMemory[0xff6a] = (gbMemory[0xff6a] & 0x80) | index;
1167 gbMemory[0xff6b] = (index & 1 ?
1168 (gbPalette[(index >> 1) + 32] >> 8) :
1169 (gbPalette[(index >> 1) + 32] & 0x00ff));
1171 return;
1173 break;
1176 // SVBK
1177 case 0x70:
1179 if (gbCgbMode)
1181 value = value & 7;
1183 int bank = value;
1184 if (value == 0)
1185 bank = 1;
1187 if (bank == gbWramBank)
1188 return;
1190 int wramAddress = bank * 0x1000;
1191 gbMemoryMap[0x0d] = &gbWram[wramAddress];
1193 gbWramBank = bank;
1194 register_SVBK = value;
1195 return;
1197 break;
1200 case 0xff:
1202 register_IE = value;
1203 register_IF &= value;
1204 return;
1208 gbWriteMemoryQuick(address, value);
1211 u8 gbReadOpcode(register u16 address)
1213 if (gbCheatMap[address])
1214 return gbCheatRead(address);
1216 // the following fix does more than Echo RAM fix, anyway...
1217 switch (gbEchoRAMFixOn ? (address >> 12) & 0x000f : address & 0xf000)
1219 case 0x0a:
1220 case 0x0b:
1221 if (mapperReadRAM)
1222 return mapperReadRAM(address);
1223 break;
1224 case 0x0f:
1225 if (address > 0xff00)
1227 switch (address & 0x00ff)
1229 case 0x04:
1230 return register_DIV;
1231 case 0x05:
1232 return register_TIMA;
1233 case 0x06:
1234 return register_TMA;
1235 case 0x07:
1236 return (0xf8 | register_TAC);
1237 case 0x0f:
1238 return (0xe0 | register_IF);
1239 case 0x40:
1240 return register_LCDC;
1241 case 0x41:
1242 return (0x80 | register_STAT);
1243 case 0x42:
1244 return register_SCY;
1245 case 0x43:
1246 return register_SCX;
1247 case 0x44:
1248 return register_LY;
1249 case 0x45:
1250 return register_LYC;
1251 case 0x46:
1252 return register_DMA;
1253 case 0x4a:
1254 return register_WY;
1255 case 0x4b:
1256 return register_WX;
1257 case 0x4f:
1258 return (0xfe | register_VBK);
1259 case 0x51:
1260 return register_HDMA1;
1261 case 0x52:
1262 return register_HDMA2;
1263 case 0x53:
1264 return register_HDMA3;
1265 case 0x54:
1266 return register_HDMA4;
1267 case 0x55:
1268 return register_HDMA5;
1269 case 0x70:
1270 return (0xf8 | register_SVBK);
1271 case 0xff:
1272 return register_IE;
1275 break;
1277 return gbReadMemoryQuick(address);
1280 void gbWriteMemory(register u16 address, register u8 value)
1282 gbWriteMemoryWrapped(address, value);
1283 CallRegisteredLuaMemHook(address, 1, value, LUAMEMHOOK_WRITE);
1286 u8 gbReadMemory(register u16 address)
1288 if (gbCheatMap[address])
1289 return gbCheatRead(address);
1291 if (address < 0xa000)
1292 return gbReadMemoryQuick(address);
1294 if (address < 0xc000)
1296 #ifndef FINAL_VERSION
1297 if (memorydebug)
1299 log("Memory register read %04x PC=%04x\n",
1300 address,
1301 PC.W);
1303 #endif
1305 if (mapperReadRAM)
1306 return mapperReadRAM(address);
1307 return gbReadMemoryQuick(address);
1310 if (address >= 0xff00)
1312 switch (address & 0x00ff)
1314 case 0x00:
1316 if (gbSgbMode)
1318 gbSgbReadingController |= 4;
1319 gbSgbResetPacketState();
1322 int b = gbMemory[0xff00];
1324 if ((b & 0x30) == 0x20)
1326 b &= 0xf0;
1328 int joy = 0;
1329 if (gbSgbMode && gbSgbMultiplayer)
1331 switch (gbSgbNextController)
1333 case 0x0f:
1334 joy = 0;
1335 break;
1336 case 0x0e:
1337 joy = 1;
1338 break;
1339 case 0x0d:
1340 joy = 2;
1341 break;
1342 case 0x0c:
1343 joy = 3;
1344 break;
1345 default:
1346 joy = 0;
1349 int joystate = gbJoymask[joy];
1350 if (!(joystate & 128))
1351 b |= 0x08;
1352 if (!(joystate & 64))
1353 b |= 0x04;
1354 if (!(joystate & 32))
1355 b |= 0x02;
1356 if (!(joystate & 16))
1357 b |= 0x01;
1359 gbMemory[0xff00] = b;
1361 else if ((b & 0x30) == 0x10)
1363 b &= 0xf0;
1365 int joy = 0;
1366 if (gbSgbMode && gbSgbMultiplayer)
1368 switch (gbSgbNextController)
1370 case 0x0f:
1371 joy = 0;
1372 break;
1373 case 0x0e:
1374 joy = 1;
1375 break;
1376 case 0x0d:
1377 joy = 2;
1378 break;
1379 case 0x0c:
1380 joy = 3;
1381 break;
1382 default:
1383 joy = 0;
1386 int joystate = gbJoymask[joy];
1387 if (!(joystate & 8))
1388 b |= 0x08;
1389 if (!(joystate & 4))
1390 b |= 0x04;
1391 if (!(joystate & 2))
1392 b |= 0x02;
1393 if (!(joystate & 1))
1394 b |= 0x01;
1396 gbMemory[0xff00] = b;
1398 else
1400 if (gbSgbMode && gbSgbMultiplayer)
1402 gbMemory[0xff00] = 0xf0 | gbSgbNextController;
1404 else
1406 gbMemory[0xff00] = 0xff;
1410 GBSystemCounters.lagged = false;
1411 return gbMemory[0xff00];
1412 break;
1413 case 0x01:
1414 return gbMemory[0xff01];
1415 case 0x04:
1416 return register_DIV;
1417 case 0x05:
1418 return register_TIMA;
1419 case 0x06:
1420 return register_TMA;
1421 case 0x07:
1422 return (0xf8 | register_TAC);
1423 case 0x0f:
1424 return (0xe0 | register_IF);
1425 case 0x40:
1426 return register_LCDC;
1427 case 0x41:
1428 return (0x80 | register_STAT);
1429 case 0x42:
1430 return register_SCY;
1431 case 0x43:
1432 return register_SCX;
1433 case 0x44:
1434 return register_LY;
1435 case 0x45:
1436 return register_LYC;
1437 case 0x46:
1438 return register_DMA;
1439 case 0x4a:
1440 return register_WY;
1441 case 0x4b:
1442 return register_WX;
1443 case 0x4f:
1444 return (0xfe | register_VBK);
1445 case 0x51:
1446 return register_HDMA1;
1447 case 0x52:
1448 return register_HDMA2;
1449 case 0x53:
1450 return register_HDMA3;
1451 case 0x54:
1452 return register_HDMA4;
1453 case 0x55:
1454 return register_HDMA5;
1455 case 0x70:
1456 return (0xf8 | register_SVBK);
1457 case 0xff:
1458 return register_IE;
1462 return gbReadMemoryQuick(address);
1465 void gbVblank_interrupt()
1467 if (IFF & 0x80)
1469 PC.W++;
1470 IFF &= 0x7f;
1472 gbInterrupt &= 0xfe;
1474 IFF &= 0x7e;
1475 register_IF &= 0xfe;
1477 gbWriteMemory(--SP.W, PC.B.B1);
1478 gbWriteMemory(--SP.W, PC.B.B0);
1479 PC.W = 0x40;
1482 void gbLcd_interrupt()
1484 if (IFF & 0x80)
1486 PC.W++;
1487 IFF &= 0x7f;
1489 gbInterrupt &= 0xfd;
1490 IFF &= 0x7e;
1491 register_IF &= 0xfd;
1493 gbWriteMemory(--SP.W, PC.B.B1);
1494 gbWriteMemory(--SP.W, PC.B.B0);
1496 PC.W = 0x48;
1499 void gbTimer_interrupt()
1501 if (IFF & 0x80)
1503 PC.W++;
1504 IFF &= 0x7f;
1506 IFF &= 0x7e;
1507 gbInterrupt &= 0xfb;
1508 register_IF &= 0xfb;
1510 gbWriteMemory(--SP.W, PC.B.B1);
1511 gbWriteMemory(--SP.W, PC.B.B0);
1513 PC.W = 0x50;
1516 void gbSerial_interrupt()
1518 if (IFF & 0x80)
1520 PC.W++;
1521 IFF &= 0x7f;
1523 IFF &= 0x7e;
1524 gbInterrupt &= 0xf7;
1525 register_IF &= 0xf7;
1527 gbWriteMemory(--SP.W, PC.B.B1);
1528 gbWriteMemory(--SP.W, PC.B.B0);
1530 PC.W = 0x58;
1533 void gbJoypad_interrupt()
1535 if (IFF & 0x80)
1537 PC.W++;
1538 IFF &= 0x7f;
1540 IFF &= 0x7e;
1541 gbInterrupt &= 0xef;
1542 register_IF &= 0xef;
1544 gbWriteMemory(--SP.W, PC.B.B1);
1545 gbWriteMemory(--SP.W, PC.B.B0);
1547 PC.W = 0x60;
1550 void gbSpeedSwitch()
1552 if (gbSpeed == 0)
1554 gbSpeed = 1;
1555 GBLCD_MODE_0_CLOCK_TICKS = 51 * 2; //127; //51 * 2;
1556 GBLCD_MODE_1_CLOCK_TICKS = 1140 * 2;
1557 GBLCD_MODE_2_CLOCK_TICKS = 20 * 2; //52; //20 * 2;
1558 GBLCD_MODE_3_CLOCK_TICKS = 43 * 2; //99; //43 * 2;
1559 GBDIV_CLOCK_TICKS = 64 * 2;
1560 GBLY_INCREMENT_CLOCK_TICKS = 114 * 2;
1561 GBTIMER_MODE_0_CLOCK_TICKS = 256; //256*2;
1562 GBTIMER_MODE_1_CLOCK_TICKS = 4; //4*2;
1563 GBTIMER_MODE_2_CLOCK_TICKS = 16; //16*2;
1564 GBTIMER_MODE_3_CLOCK_TICKS = 64; //64*2;
1565 GBSERIAL_CLOCK_TICKS = 128 * 2;
1566 gbDivTicks *= 2;
1567 gbLcdTicks *= 2;
1568 gbLcdLYIncrementTicks *= 2;
1569 // timerTicks *= 2;
1570 // timerClockTicks *= 2;
1571 gbSerialTicks *= 2;
1572 SOUND_CLOCK_TICKS = soundQuality * GB_USE_TICKS_AS * 2;
1573 soundTicks *= 2;
1574 // synchronizeTicks *= 2;
1575 // SYNCHRONIZE_CLOCK_TICKS *= 2;
1577 else
1579 gbSpeed = 0;
1580 GBLCD_MODE_0_CLOCK_TICKS = 51;
1581 GBLCD_MODE_1_CLOCK_TICKS = 1140;
1582 GBLCD_MODE_2_CLOCK_TICKS = 20;
1583 GBLCD_MODE_3_CLOCK_TICKS = 43;
1584 GBDIV_CLOCK_TICKS = 64;
1585 GBLY_INCREMENT_CLOCK_TICKS = 114;
1586 GBTIMER_MODE_0_CLOCK_TICKS = 256;
1587 GBTIMER_MODE_1_CLOCK_TICKS = 4;
1588 GBTIMER_MODE_2_CLOCK_TICKS = 16;
1589 GBTIMER_MODE_3_CLOCK_TICKS = 64;
1590 GBSERIAL_CLOCK_TICKS = 128;
1591 gbDivTicks /= 2;
1592 gbLcdTicks /= 2;
1593 gbLcdLYIncrementTicks /= 2;
1594 // timerTicks /= 2;
1595 // timerClockTicks /= 2;
1596 gbSerialTicks /= 2;
1597 SOUND_CLOCK_TICKS = soundQuality * GB_USE_TICKS_AS;
1598 soundTicks /= 2;
1599 // synchronizeTicks /= 2;
1600 // SYNCHRONIZE_CLOCK_TICKS /= 2;
1604 void gbGetHardwareType()
1606 gbCgbMode = 0;
1607 if (gbRom[0x143] & 0x80)
1609 if (gbEmulatorType == 0 ||
1610 gbEmulatorType == 1 ||
1611 gbEmulatorType == 4 ||
1612 gbEmulatorType == 5 ||
1613 (gbRom[0x146] != 0x03 && (gbEmulatorType == 2)))
1615 gbCgbMode = 1;
1619 if (gbSgbMode == 2)
1621 gbSgbMode = 0;
1622 return;
1625 gbSgbMode = 0;
1626 if (gbRom[0x146] == 0x03)
1628 if (gbEmulatorType == 0 ||
1629 gbEmulatorType == 2 ||
1630 gbEmulatorType == 5 ||
1631 (!(gbRom[0x143] & 0x80) && (gbEmulatorType == 1 || gbEmulatorType == 4)))
1632 gbSgbMode = 1;
1636 void gbReset(bool userReset)
1638 // movie must be closed while opening/creating a movie
1639 if (userReset && VBAMovieRecording())
1641 VBAMovieSignalReset();
1642 return;
1645 if (!VBAMovieActive())
1647 GBSystemCounters.frameCount = 0;
1648 GBSystemCounters.lagCount = 0;
1649 GBSystemCounters.extraCount = 0;
1650 GBSystemCounters.lagged = true;
1651 GBSystemCounters.laggedLast = true;
1654 SP.W = 0xfffe;
1655 AF.W = 0x01b0;
1656 BC.W = 0x0013;
1657 DE.W = 0x00d8;
1658 HL.W = 0x014d;
1659 PC.W = 0x0100;
1660 IFF = 0;
1661 gbInterrupt = 1;
1662 gbInterruptWait = 0;
1664 register_DIV = 0;
1665 register_TIMA = 0;
1666 register_TMA = 0;
1667 register_TAC = 0;
1668 register_IF = 1;
1669 register_LCDC = 0x91;
1670 register_STAT = 0;
1671 register_SCY = 0;
1672 register_SCX = 0;
1673 register_LY = 0;
1674 register_LYC = 0;
1675 register_DMA = 0;
1676 register_WY = 0;
1677 register_WX = 0;
1678 register_VBK = 0;
1679 register_HDMA1 = 0;
1680 register_HDMA2 = 0;
1681 register_HDMA3 = 0;
1682 register_HDMA4 = 0;
1683 register_HDMA5 = 0;
1684 register_SVBK = 0;
1685 register_IE = 0;
1687 gbGetHardwareType();
1688 if (gbCgbMode)
1690 if (!gbVram)
1691 gbVram = (u8 *)malloc(0x4000 + 4);
1692 if (!gbWram)
1693 gbWram = (u8 *)malloc(0x8000 + 4);
1694 memset(gbVram, 0, 0x4000 + 4);
1695 memset(gbWram, 0, 0x8000 + 4);
1697 else
1699 if (gbVram)
1701 free(gbVram);
1702 gbVram = NULL;
1704 if (gbWram)
1706 free(gbWram);
1707 gbWram = NULL;
1711 // clean LineBuffer
1712 if (gbLineBuffer)
1713 memset(gbLineBuffer, 0, 160 * sizeof(u16));
1714 // clean Pix
1715 if (pix)
1716 memset(pix, 0, 4 * 257 * 226);
1718 if (gbCgbMode)
1720 if (gbSgbMode)
1722 if (gbEmulatorType == 5)
1723 AF.W = 0xffb0;
1724 else
1725 AF.W = 0x01b0;
1726 BC.W = 0x0013;
1727 DE.W = 0x00d8;
1728 HL.W = 0x014d;
1730 else
1732 AF.W = 0x11b0;
1733 BC.W = 0x0000;
1734 DE.W = 0xff56;
1735 HL.W = 0x000d;
1737 if (gbEmulatorType == 4)
1738 BC.B.B1 |= 0x01;
1740 register_HDMA5 = 0xff;
1741 gbMemory[0xff68] = 0xc0;
1742 gbMemory[0xff6a] = 0xc0;
1744 for (int i = 0; i < 64; i++)
1745 gbPalette[i] = 0x7fff;
1747 else
1749 for (int i = 0; i < 8; i++)
1750 gbPalette[i] = systemGbPalette[gbPaletteOption * 8 + i];
1753 if (gbSpeed)
1755 gbSpeedSwitch();
1756 gbMemory[0xff4d] = 0;
1759 gbDivTicks = GBDIV_CLOCK_TICKS;
1760 gbLcdMode = 2;
1761 gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS;
1762 gbLcdLYIncrementTicks = 0;
1763 gbTimerTicks = 0;
1764 gbTimerClockTicks = 0;
1765 gbSerialTicks = 0;
1766 gbSerialBits = 0;
1767 gbSerialOn = 0;
1768 gbWindowLine = -1;
1769 gbTimerOn = 0;
1770 gbTimerMode = 0;
1771 // gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS;
1772 gbSpeed = 0;
1773 gbJoymask[0] = gbJoymask[1] = gbJoymask[2] = gbJoymask[3] = 0;
1775 // FIXME: horrible kludge
1776 memset(s_gbJoymask, 0, sizeof(s_gbJoymask));
1778 if (gbCgbMode)
1780 gbSpeed = 0;
1781 gbHdmaOn = 0;
1782 gbHdmaSource = 0x0000;
1783 gbHdmaDestination = 0x8000;
1784 gbVramBank = 0;
1785 gbWramBank = 1;
1786 register_LY = 0x90;
1787 gbLcdMode = 1;
1790 if (gbSgbMode)
1792 gbSgbReset();
1795 for (int i = 0; i < 4; i++)
1796 gbBgp[i] = gbObp0[i] = gbObp1[i] = i;
1798 memset(&gbDataMBC1, 0, sizeof(gbDataMBC1));
1799 gbDataMBC1.mapperROMBank = 1;
1801 gbDataMBC2.mapperRAMEnable = 0;
1802 gbDataMBC2.mapperROMBank = 1;
1804 memset(&gbDataMBC3, 0, 6 * sizeof(int32));
1805 gbDataMBC3.mapperROMBank = 1;
1807 memset(&gbDataMBC5, 0, sizeof(gbDataMBC5));
1808 gbDataMBC5.mapperROMBank = 1;
1809 switch (gbRom[0x147])
1811 case 0x1c:
1812 case 0x1d:
1813 case 0x1e:
1814 gbDataMBC5.isRumbleCartridge = 1;
1817 memset(&gbDataHuC1, 0, sizeof(gbDataHuC1));
1818 gbDataHuC1.mapperROMBank = 1;
1820 memset(&gbDataHuC3, 0, sizeof(gbDataHuC3));
1821 gbDataHuC3.mapperROMBank = 1;
1823 gbMemoryMap[0x00] = &gbRom[0x0000];
1824 gbMemoryMap[0x01] = &gbRom[0x1000];
1825 gbMemoryMap[0x02] = &gbRom[0x2000];
1826 gbMemoryMap[0x03] = &gbRom[0x3000];
1827 gbMemoryMap[0x04] = &gbRom[0x4000];
1828 gbMemoryMap[0x05] = &gbRom[0x5000];
1829 gbMemoryMap[0x06] = &gbRom[0x6000];
1830 gbMemoryMap[0x07] = &gbRom[0x7000];
1831 if (gbCgbMode)
1833 gbMemoryMap[0x08] = &gbVram[0x0000];
1834 gbMemoryMap[0x09] = &gbVram[0x1000];
1835 gbMemoryMap[0x0a] = &gbMemory[0xa000];
1836 gbMemoryMap[0x0b] = &gbMemory[0xb000];
1837 gbMemoryMap[0x0c] = &gbMemory[0xc000];
1838 gbMemoryMap[0x0d] = &gbWram[0x1000];
1839 gbMemoryMap[0x0e] = &gbMemory[0xe000];
1840 gbMemoryMap[0x0f] = &gbMemory[0xf000];
1842 else
1844 gbMemoryMap[0x08] = &gbMemory[0x8000];
1845 gbMemoryMap[0x09] = &gbMemory[0x9000];
1846 gbMemoryMap[0x0a] = &gbMemory[0xa000];
1847 gbMemoryMap[0x0b] = &gbMemory[0xb000];
1848 gbMemoryMap[0x0c] = &gbMemory[0xc000];
1849 gbMemoryMap[0x0d] = &gbMemory[0xd000];
1850 gbMemoryMap[0x0e] = &gbMemory[0xe000];
1851 gbMemoryMap[0x0f] = &gbMemory[0xf000];
1854 if (gbRam)
1856 gbMemoryMap[0x0a] = &gbRam[0x0000];
1857 gbMemoryMap[0x0b] = &gbRam[0x1000];
1860 gbSoundReset();
1862 systemResetSensor();
1864 systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
1866 gbLastTime = systemGetClock();
1867 gbFrameCount = 0;
1869 systemRefreshScreen();
1872 void gbWriteSaveMBC1(const char *name)
1874 FILE *gzFile = fopen(name, "wb");
1876 if (gzFile == NULL)
1878 systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
1879 return;
1882 fwrite(gbRam,
1883 1,
1884 gbRamSize,
1885 gzFile);
1887 fclose(gzFile);
1890 void gbWriteSaveMBC2(const char *name)
1892 FILE *file = fopen(name, "wb");
1894 if (file == NULL)
1896 systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
1897 return;
1900 fwrite(&gbMemory[0xa000],
1901 1,
1902 256,
1903 file);
1905 fclose(file);
1908 void gbWriteSaveMBC3(const char *name, bool extendedSave)
1910 FILE *gzFile = fopen(name, "wb");
1912 if (gzFile == NULL)
1914 systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
1915 return;
1918 fwrite(gbRam,
1919 1,
1920 gbRamSize,
1921 gzFile);
1923 if (extendedSave)
1925 //assert(sizeof(time_t) == 4);
1926 fwrite(&gbDataMBC3.mapperSeconds,
1927 1,
1928 10 * sizeof(int32) + /*sizeof(time_t)*/4,
1929 gzFile);
1932 fclose(gzFile);
1935 void gbWriteSaveMBC5(const char *name)
1937 FILE *gzFile = fopen(name, "wb");
1939 if (gzFile == NULL)
1941 systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
1942 return;
1945 fwrite(gbRam,
1946 1,
1947 gbRamSize,
1948 gzFile);
1950 fclose(gzFile);
1953 void gbWriteSaveMBC7(const char *name)
1955 FILE *file = fopen(name, "wb");
1957 if (file == NULL)
1959 systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
1960 return;
1963 fwrite(&gbMemory[0xa000],
1964 1,
1965 256,
1966 file);
1968 fclose(file);
1971 bool gbReadSaveMBC1(const char *name)
1973 gzFile gzFile = gzopen(name, "rb");
1975 if (gzFile == NULL)
1977 return false;
1980 int read = gzread(gzFile,
1981 gbRam,
1982 gbRamSize);
1984 if (read != gbRamSize)
1986 systemMessage(MSG_FAILED_TO_READ_SGM, N_("Failed to read complete save game %s (%d)"), name, read);
1987 gzclose(gzFile);
1988 return false;
1991 gzclose(gzFile);
1992 return true;
1995 bool gbReadSaveMBC2(const char *name)
1997 FILE *file = fopen(name, "rb");
1999 if (file == NULL)
2001 return false;
2004 int read = fread(&gbMemory[0xa000],
2005 1,
2006 256,
2007 file);
2009 if (read != 256)
2011 systemMessage(MSG_FAILED_TO_READ_SGM,
2012 N_("Failed to read complete save game %s (%d)"), name, read);
2013 fclose(file);
2014 return false;
2017 fclose(file);
2018 return true;
2021 bool gbReadSaveMBC3(const char *name)
2023 gzFile gzFile = gzopen(name, "rb");
2025 if (gzFile == NULL)
2027 return false;
2030 int read = gzread(gzFile,
2031 gbRam,
2032 gbRamSize);
2034 bool res = true;
2036 if (read != gbRamSize)
2038 systemMessage(MSG_FAILED_TO_READ_SGM,
2039 N_("Failed to read complete save game %s (%d)"), name, read);
2041 else
2043 //assert(sizeof(time_t) == 4);
2044 read = gzread(gzFile,
2045 &gbDataMBC3.mapperSeconds,
2046 sizeof(int32) * 10 + /*sizeof(time_t)*/4);
2048 if (read != (sizeof(int32) * 10 + /*sizeof(time_t)*/4) && read != 0)
2050 systemMessage(MSG_FAILED_TO_READ_RTC,
2051 N_("Failed to read RTC from save game %s (continuing)"),
2052 name);
2053 res = false;
2057 gzclose(gzFile);
2058 return res;
2061 bool gbReadSaveMBC5(const char *name)
2063 gzFile gzFile = gzopen(name, "rb");
2065 if (gzFile == NULL)
2067 return false;
2070 int read = gzread(gzFile,
2071 gbRam,
2072 gbRamSize);
2074 if (read != gbRamSize)
2076 systemMessage(MSG_FAILED_TO_READ_SGM,
2077 N_("Failed to read complete save game %s (%d)"), name, read);
2078 gzclose(gzFile);
2079 return false;
2082 gzclose(gzFile);
2083 return true;
2086 bool gbReadSaveMBC7(const char *name)
2088 FILE *file = fopen(name, "rb");
2090 if (file == NULL)
2092 return false;
2095 int read = fread(&gbMemory[0xa000],
2096 1,
2097 256,
2098 file);
2100 if (read != 256)
2102 systemMessage(MSG_FAILED_TO_READ_SGM,
2103 N_("Failed to read complete save game %s (%d)"), name, read);
2104 fclose(file);
2105 return false;
2108 fclose(file);
2109 return true;
2112 #if 0
2113 bool gbLoadBIOS(const char *biosFileName, bool useBiosFile)
2115 useBios = false;
2116 if (useBiosFile)
2118 useBios = utilLoadBIOS(bios, biosFileName, gbEmulatorType);
2119 if (!useBios)
2121 systemMessage(MSG_INVALID_BIOS_FILE_SIZE, N_("Invalid BOOTROM file"));
2124 return useBios;
2126 #endif
2128 void gbInit()
2130 gbGenFilter();
2131 gbSgbInit(); // calls gbSgbReset()... whatever
2133 gbMemory = (u8 *)malloc(65536 + 4);
2134 memset(gbMemory, 0, 65536 + 4);
2135 memset(gbPalette, 0, 2 * 128);
2137 // HACK: +4 at start to accomodate the 2xSaI filter reading out of bounds of the leftmost pixel
2138 origPix = (u8 *)calloc(1, 4 * 257 * 226 + 4);
2139 pix = origPix + 4;
2141 gbLineBuffer = (u16 *)malloc(160 * sizeof(u16));
2144 bool gbWriteBatteryFile(const char *file, bool extendedSave)
2146 if (gbBattery)
2148 int type = gbRom[0x147];
2150 switch (type)
2152 case 0x03:
2153 gbWriteSaveMBC1(file);
2154 break;
2155 case 0x06:
2156 gbWriteSaveMBC2(file);
2157 break;
2158 case 0x0f:
2159 case 0x10:
2160 case 0x13:
2161 gbWriteSaveMBC3(file, extendedSave);
2162 break;
2163 case 0x1b:
2164 case 0x1e:
2165 gbWriteSaveMBC5(file);
2166 break;
2167 case 0x22:
2168 gbWriteSaveMBC7(file);
2169 break;
2170 case 0xff:
2171 gbWriteSaveMBC1(file);
2172 break;
2175 return true;
2178 bool gbWriteBatteryFile(const char *file)
2180 gbWriteBatteryFile(file, true);
2181 return true;
2184 bool gbWriteBatteryToStream(gzFile gzfile)
2186 // the GB save code is ugly, so rather than convert it all to use gzFiles, just save it to a temp file...
2187 #define TEMP_SAVE_FNAME ("tempvbawrite.sav")
2188 bool retVal = gbWriteBatteryFile(TEMP_SAVE_FNAME, true);
2190 // ...open the temp file and figure out its size...
2191 FILE *fileTemp = fopen(TEMP_SAVE_FNAME, "rb");
2192 if (fileTemp == NULL)
2193 return false;
2194 fseek(fileTemp, 0, SEEK_END);
2195 int len = (int) ftell(fileTemp);
2197 // ...copy over the temp file...
2198 char *temp = new char [len];
2199 fseek(fileTemp, 0, SEEK_SET);
2200 if (fread(temp, len, 1, fileTemp) != 1)
2202 delete [] temp;
2203 fclose(fileTemp);
2204 return false;
2206 fclose(fileTemp);
2207 utilGzWrite(gzfile, temp, len);
2208 delete [] temp;
2210 // ... and delete the temp file
2211 remove(TEMP_SAVE_FNAME);
2212 #undef TEMP_SAVE_FNAME
2214 return retVal;
2217 bool gbReadBatteryFile(const char *file)
2219 bool res = false;
2220 if (gbBattery)
2222 int type = gbRom[0x147];
2224 switch (type)
2226 case 0x03:
2227 res = gbReadSaveMBC1(file);
2228 break;
2229 case 0x06:
2230 res = gbReadSaveMBC2(file);
2231 break;
2232 case 0x0f:
2233 case 0x10:
2234 case 0x13:
2235 if (!gbReadSaveMBC3(file))
2237 struct tm *lt;
2238 time_t tmp; //Small kludge to get it working on some systems where time_t has size 8.
2240 if (VBAMovieActive() || VBAMovieLoading())
2242 gbDataMBC3.mapperLastTime = VBAMovieGetId() + VBAMovieGetFrameCounter() / 60;
2243 lt = gmtime(&tmp);
2244 gbDataMBC3.mapperLastTime=(u32)tmp;
2246 else
2248 time(&tmp);
2249 gbDataMBC3.mapperLastTime=(u32)tmp;
2250 lt = localtime(&tmp);
2252 systemScreenMessage(ctime(&tmp), 4);
2253 gbDataMBC3.mapperLastTime=(u32)tmp;
2255 gbDataMBC3.mapperSeconds = lt->tm_sec;
2256 gbDataMBC3.mapperMinutes = lt->tm_min;
2257 gbDataMBC3.mapperHours = lt->tm_hour;
2258 gbDataMBC3.mapperDays = lt->tm_yday & 255;
2259 gbDataMBC3.mapperControl = (gbDataMBC3.mapperControl & 0xfe) |
2260 (lt->tm_yday > 255 ? 1 : 0);
2261 res = false;
2262 break;
2264 time_t tmp;
2265 systemScreenMessage(ctime(&tmp), 4);
2266 gbDataMBC3.mapperLastTime=(u32)tmp;
2267 res = true;
2268 break;
2269 case 0x1b:
2270 case 0x1e:
2271 res = gbReadSaveMBC5(file);
2272 break;
2273 case 0x22:
2274 res = gbReadSaveMBC7(file);
2275 case 0xff:
2276 res = gbReadSaveMBC1(file);
2277 break;
2280 systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
2281 return res;
2284 bool gbReadBatteryFromStream(gzFile gzfile)
2286 // the GB save code is ugly, so rather than convert it all to use gzFiles, just copy it to temp RAM...
2287 #define TEMP_SAVE_FNAME ("tempvbaread.sav")
2288 int pos = gztell(gzfile);
2289 int buflen = 1024;
2290 // ...make a temp file and write it there...
2291 FILE *fileTemp = fopen(TEMP_SAVE_FNAME, "wb");
2292 if (fileTemp == NULL)
2293 return false;
2294 int gzDeflated;
2295 char *temp = new char [buflen];
2296 while ((gzDeflated = utilGzRead(gzfile, temp, buflen)) != 0)
2298 if (gzDeflated == -1 || fwrite(temp, gzDeflated, 1, fileTemp) != 1)
2300 delete [] temp;
2301 fclose(fileTemp);
2302 gzseek(gzfile, pos, SEEK_SET); /// FIXME: leaves pos in gzfile before save instead of after it (everything that
2303 // calls this right now does a seek afterwards so it doesn't matter for now, but it's
2304 // still bad)
2305 return false;
2308 gzseek(gzfile, pos, SEEK_SET); /// FIXME: leaves pos in gzfile before save instead of after it (everything that calls this
2309 // right now does a seek afterwards so it doesn't matter for now, but it's still bad)
2310 fclose(fileTemp);
2311 delete [] temp;
2313 // ... load from the temp file...
2314 bool retVal = gbReadBatteryFile(TEMP_SAVE_FNAME);
2316 // ... and delete the temp file
2317 remove(TEMP_SAVE_FNAME);
2318 #undef TEMP_SAVE_FNAME
2320 return retVal;
2323 bool gbReadGSASnapshot(const char *fileName)
2325 FILE *file = fopen(fileName, "rb");
2327 if (!file)
2329 systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName);
2330 return false;
2333 // long size = ftell(file);
2334 fseek(file, 0x4, SEEK_SET);
2335 char buffer[16];
2336 char buffer2[16];
2337 fread(buffer, 1, 15, file);
2338 buffer[15] = 0;
2339 memcpy(buffer2, &gbRom[0x134], 15);
2340 buffer2[15] = 0;
2341 if (memcmp(buffer, buffer2, 15))
2343 systemMessage(MSG_CANNOT_IMPORT_SNAPSHOT_FOR,
2344 N_("Cannot import snapshot for %s. Current game is %s"),
2345 buffer,
2346 buffer2);
2347 fclose(file);
2348 return false;
2350 fseek(file, 0x13, SEEK_SET);
2351 int read = 0;
2352 int toRead = 0;
2353 switch (gbRom[0x147])
2355 case 0x03:
2356 case 0x0f:
2357 case 0x10:
2358 case 0x13:
2359 case 0x1b:
2360 case 0x1e:
2361 case 0xff:
2362 read = fread(gbRam, 1, gbRamSize, file);
2363 toRead = gbRamSize;
2364 break;
2365 case 0x06:
2366 case 0x22:
2367 read = fread(&gbMemory[0xa000], 1, 256, file);
2368 toRead = 256;
2369 break;
2370 default:
2371 systemMessage(MSG_UNSUPPORTED_SNAPSHOT_FILE,
2372 N_("Unsupported snapshot file %s"),
2373 fileName);
2374 fclose(file);
2375 return false;
2377 fclose(file);
2378 gbReset();
2379 return true;
2382 variable_desc gbSaveGameStruct[] =
2384 { &PC.W, sizeof(u16) },
2385 { &SP.W, sizeof(u16) },
2386 { &AF.W, sizeof(u16) },
2387 { &BC.W, sizeof(u16) },
2388 { &DE.W, sizeof(u16) },
2389 { &HL.W, sizeof(u16) },
2390 { &IFF, sizeof(u8) },
2391 { &GBLCD_MODE_0_CLOCK_TICKS, sizeof(int32) },
2392 { &GBLCD_MODE_1_CLOCK_TICKS, sizeof(int32) },
2393 { &GBLCD_MODE_2_CLOCK_TICKS, sizeof(int32) },
2394 { &GBLCD_MODE_3_CLOCK_TICKS, sizeof(int32) },
2395 { &GBDIV_CLOCK_TICKS, sizeof(int32) },
2396 { &GBLY_INCREMENT_CLOCK_TICKS, sizeof(int32) },
2397 { &GBTIMER_MODE_0_CLOCK_TICKS, sizeof(int32) },
2398 { &GBTIMER_MODE_1_CLOCK_TICKS, sizeof(int32) },
2399 { &GBTIMER_MODE_2_CLOCK_TICKS, sizeof(int32) },
2400 { &GBTIMER_MODE_3_CLOCK_TICKS, sizeof(int32) },
2401 { &GBSERIAL_CLOCK_TICKS, sizeof(int32) },
2402 { &GBSYNCHRONIZE_CLOCK_TICKS, sizeof(int32) },
2403 { &gbDivTicks, sizeof(int32) },
2404 { &gbLcdMode, sizeof(int32) },
2405 { &gbLcdTicks, sizeof(int32) },
2406 { &gbLcdLYIncrementTicks, sizeof(int32) },
2407 { &gbTimerTicks, sizeof(int32) },
2408 { &gbTimerClockTicks, sizeof(int32) },
2409 { &gbSerialTicks, sizeof(int32) },
2410 { &gbSerialBits, sizeof(int32) },
2411 { &gbInterrupt, sizeof(int32) },
2412 { &gbInterruptWait, sizeof(int32) },
2413 { &gbSynchronizeTicks, sizeof(int32) },
2414 { &gbTimerOn, sizeof(int32) },
2415 { &gbTimerMode, sizeof(int32) },
2416 { &gbSerialOn, sizeof(int32) },
2417 { &gbWindowLine, sizeof(int32) },
2418 { &gbCgbMode, sizeof(int32) },
2419 { &gbVramBank, sizeof(int32) },
2420 { &gbWramBank, sizeof(int32) },
2421 { &gbHdmaSource, sizeof(int32) },
2422 { &gbHdmaDestination, sizeof(int32) },
2423 { &gbHdmaBytes, sizeof(int32) },
2424 { &gbHdmaOn, sizeof(int32) },
2425 { &gbSpeed, sizeof(int32) },
2426 { &gbSgbMode, sizeof(int32) },
2427 { &register_DIV, sizeof(u8) },
2428 { &register_TIMA, sizeof(u8) },
2429 { &register_TMA, sizeof(u8) },
2430 { &register_TAC, sizeof(u8) },
2431 { &register_IF, sizeof(u8) },
2432 { &register_LCDC, sizeof(u8) },
2433 { &register_STAT, sizeof(u8) },
2434 { &register_SCY, sizeof(u8) },
2435 { &register_SCX, sizeof(u8) },
2436 { &register_LY, sizeof(u8) },
2437 { &register_LYC, sizeof(u8) },
2438 { &register_DMA, sizeof(u8) },
2439 { &register_WY, sizeof(u8) },
2440 { &register_WX, sizeof(u8) },
2441 { &register_VBK, sizeof(u8) },
2442 { &register_HDMA1, sizeof(u8) },
2443 { &register_HDMA2, sizeof(u8) },
2444 { &register_HDMA3, sizeof(u8) },
2445 { &register_HDMA4, sizeof(u8) },
2446 { &register_HDMA5, sizeof(u8) },
2447 { &register_SVBK, sizeof(u8) },
2448 { &register_IE, sizeof(u8) },
2449 { &gbBgp[0], sizeof(u8) },
2450 { &gbBgp[1], sizeof(u8) },
2451 { &gbBgp[2], sizeof(u8) },
2452 { &gbBgp[3], sizeof(u8) },
2453 { &gbObp0[0], sizeof(u8) },
2454 { &gbObp0[1], sizeof(u8) },
2455 { &gbObp0[2], sizeof(u8) },
2456 { &gbObp0[3], sizeof(u8) },
2457 { &gbObp1[0], sizeof(u8) },
2458 { &gbObp1[1], sizeof(u8) },
2459 { &gbObp1[2], sizeof(u8) },
2460 { &gbObp1[3], sizeof(u8) },
2461 { NULL, 0 }
2462 };
2464 bool gbWriteSaveStateToStream(gzFile gzFile)
2466 utilWriteInt(gzFile, GBSAVE_GAME_VERSION);
2468 utilGzWrite(gzFile, &gbRom[0x134], 15);
2470 utilWriteData(gzFile, gbSaveGameStruct);
2472 utilGzWrite(gzFile, &IFF, 2);
2474 if (gbSgbMode)
2476 gbSgbSaveGame(gzFile);
2479 utilGzWrite(gzFile, &gbDataMBC1, sizeof(gbDataMBC1));
2480 utilGzWrite(gzFile, &gbDataMBC2, sizeof(gbDataMBC2));
2481 //assert(sizeof(time_t) == 4);
2482 utilGzWrite(gzFile, &gbDataMBC3, sizeof(gbDataMBC3));
2483 utilGzWrite(gzFile, &gbDataMBC5, sizeof(gbDataMBC5));
2484 utilGzWrite(gzFile, &gbDataHuC1, sizeof(gbDataHuC1));
2485 utilGzWrite(gzFile, &gbDataHuC3, sizeof(gbDataHuC3));
2487 // yes, this definitely needs to be saved, or loading paused games will show a black screen
2488 // this is also necessary to be consistent with what the GBA saving does
2489 utilGzWrite(gzFile, pix, 4 * 257 * 226);
2491 utilGzWrite(gzFile, gbPalette, 128 * sizeof(u16));
2492 // todo: remove
2493 utilGzWrite(gzFile, gbPalette, 128 * sizeof(u16));
2495 utilGzWrite(gzFile, &gbMemory[0x8000], 0x8000);
2498 if (gbRamSize && gbRam)
2500 utilGzWrite(gzFile, gbRam, gbRamSize);
2503 if (gbCgbMode)
2505 utilGzWrite(gzFile, gbVram, 0x4000);
2506 utilGzWrite(gzFile, gbWram, 0x8000);
2509 gbSoundSaveGame(gzFile);
2511 gbCheatsSaveGame(gzFile);
2513 // new to re-recording version:
2515 extern int32 sensorX, sensorY;
2516 utilGzWrite(gzFile, &sensorX, sizeof(sensorX));
2517 utilGzWrite(gzFile, &sensorY, sizeof(sensorY));
2518 utilGzWrite(gzFile, gbJoymask, 4 * sizeof(*gbJoymask)); // this has to be saved or old input will incorrectly get
2519 // carried
2520 // back on loading a snapshot!
2522 bool8 movieActive = VBAMovieActive();
2523 utilGzWrite(gzFile, &movieActive, sizeof(movieActive));
2524 if (movieActive)
2526 uint8 *movie_freeze_buf = NULL;
2527 uint32 movie_freeze_size = 0;
2529 VBAMovieFreeze(&movie_freeze_buf, &movie_freeze_size);
2530 if (movie_freeze_buf)
2532 utilGzWrite(gzFile, &movie_freeze_size, sizeof(movie_freeze_size));
2533 utilGzWrite(gzFile, movie_freeze_buf, movie_freeze_size);
2534 delete [] movie_freeze_buf;
2536 else
2538 systemMessage(0, N_("Failed to save movie snapshot."));
2539 return false;
2542 utilGzWrite(gzFile, &GBSystemCounters.frameCount, sizeof(GBSystemCounters.frameCount));
2545 // new to rerecording 19.4 wip (svn r22+):
2547 utilGzWrite(gzFile, &GBSystemCounters.lagCount, sizeof(GBSystemCounters.lagCount));
2548 utilGzWrite(gzFile, &GBSystemCounters.lagged, sizeof(GBSystemCounters.lagged));
2549 utilGzWrite(gzFile, &GBSystemCounters.laggedLast, sizeof(GBSystemCounters.laggedLast));
2552 return true;
2555 bool gbWriteMemSaveState(char *memory, int available)
2557 gzFile gzFile = utilMemGzOpen(memory, available, "w");
2559 if (gzFile == NULL)
2561 return false;
2564 bool res = gbWriteSaveStateToStream(gzFile);
2566 long pos = utilGzTell(gzFile) + 8;
2568 if (pos >= (available))
2569 res = false;
2571 utilGzClose(gzFile);
2573 return res;
2576 bool gbWriteSaveState(const char *name)
2578 gzFile gzFile = utilGzOpen(name, "wb");
2580 if (gzFile == NULL)
2581 return false;
2583 bool res = gbWriteSaveStateToStream(gzFile);
2585 utilGzClose(gzFile);
2586 return res;
2589 static int tempStateID = 0;
2590 static int tempFailCount = 0;
2591 static bool backupSafe = true;
2593 bool gbReadSaveStateFromStream(gzFile gzFile)
2595 int type;
2596 char tempBackupName [128];
2597 if (backupSafe)
2599 sprintf(tempBackupName, "gbatempsave%d.sav", tempStateID++);
2600 gbWriteSaveState(tempBackupName);
2603 int version = utilReadInt(gzFile);
2605 if (version > GBSAVE_GAME_VERSION || version < 0)
2607 systemMessage(MSG_UNSUPPORTED_VB_SGM,
2608 N_("Unsupported VisualBoy save game version %d"), version);
2609 goto failedLoadGB;
2612 u8 romname[20];
2614 utilGzRead(gzFile, romname, 15);
2616 if (memcmp(&gbRom[0x134], romname, 15) != 0)
2618 systemMessage(MSG_CANNOT_LOAD_SGM_FOR,
2619 N_("Cannot load save game for %s. Playing %s"),
2620 romname, &gbRom[0x134]);
2621 goto failedLoadGB;
2624 utilReadData(gzFile, gbSaveGameStruct);
2626 if (version >= GBSAVE_GAME_VERSION_7)
2628 utilGzRead(gzFile, &IFF, 2);
2631 if (gbSgbMode)
2633 gbSgbReadGame(gzFile, version);
2635 else
2637 gbSgbMask = 0; // loading a game at the wrong time causes no display
2640 utilGzRead(gzFile, &gbDataMBC1, sizeof(gbDataMBC1));
2641 utilGzRead(gzFile, &gbDataMBC2, sizeof(gbDataMBC2));
2642 if (version < GBSAVE_GAME_VERSION_4)
2643 // prior to version 4, there was no adjustment for the time the game
2644 // was last played, so we have less to read. This needs update if the
2645 // structure changes again.
2646 utilGzRead(gzFile, &gbDataMBC3, sizeof(int32) * 10);
2647 else
2649 //assert(sizeof(time_t) == 4);
2650 utilGzRead(gzFile, &gbDataMBC3, sizeof(gbDataMBC3));
2652 utilGzRead(gzFile, &gbDataMBC5, sizeof(gbDataMBC5));
2653 utilGzRead(gzFile, &gbDataHuC1, sizeof(gbDataHuC1));
2654 utilGzRead(gzFile, &gbDataHuC3, sizeof(gbDataHuC3));
2656 if (version >= GBSAVE_GAME_VERSION_12)
2658 utilGzRead(gzFile, pix, 4 * 257 * 226);
2660 else
2662 memset(pix, 0, 257 * 226 * sizeof(u32));
2663 // if(version < GBSAVE_GAME_VERSION_5)
2664 // utilGzRead(gzFile, pix, 256*224*sizeof(u16));
2667 if (version < GBSAVE_GAME_VERSION_6)
2669 utilGzRead(gzFile, gbPalette, 64 * sizeof(u16));
2671 else
2672 utilGzRead(gzFile, gbPalette, 128 * sizeof(u16));
2674 // todo: remove
2675 utilGzRead(gzFile, gbPalette, 128 * sizeof(u16));
2677 if (version < GBSAVE_GAME_VERSION_10)
2679 if (!gbCgbMode && !gbSgbMode)
2681 for (int i = 0; i < 8; i++)
2682 gbPalette[i] = systemGbPalette[gbPaletteOption * 8 + i];
2686 utilGzRead(gzFile, &gbMemory[0x8000], 0x8000);
2688 if (gbRamSize && gbRam)
2690 utilGzRead(gzFile, gbRam, gbRamSize);
2693 gbMemoryMap[0x00] = &gbRom[0x0000];
2694 gbMemoryMap[0x01] = &gbRom[0x1000];
2695 gbMemoryMap[0x02] = &gbRom[0x2000];
2696 gbMemoryMap[0x03] = &gbRom[0x3000];
2697 gbMemoryMap[0x04] = &gbRom[0x4000];
2698 gbMemoryMap[0x05] = &gbRom[0x5000];
2699 gbMemoryMap[0x06] = &gbRom[0x6000];
2700 gbMemoryMap[0x07] = &gbRom[0x7000];
2701 gbMemoryMap[0x08] = &gbMemory[0x8000];
2702 gbMemoryMap[0x09] = &gbMemory[0x9000];
2703 gbMemoryMap[0x0a] = &gbMemory[0xa000];
2704 gbMemoryMap[0x0b] = &gbMemory[0xb000];
2705 gbMemoryMap[0x0c] = &gbMemory[0xc000];
2706 gbMemoryMap[0x0d] = &gbMemory[0xd000];
2707 gbMemoryMap[0x0e] = &gbMemory[0xe000];
2708 gbMemoryMap[0x0f] = &gbMemory[0xf000];
2710 type = gbRom[0x147];
2712 switch (type)
2714 case 0x00:
2715 case 0x01:
2716 case 0x02:
2717 case 0x03:
2718 // MBC 1
2719 memoryUpdateMapMBC1();
2720 break;
2721 case 0x05:
2722 case 0x06:
2723 // MBC2
2724 memoryUpdateMapMBC2();
2725 break;
2726 case 0x0f:
2727 case 0x10:
2728 case 0x11:
2729 case 0x12:
2730 case 0x13:
2731 // MBC 3
2732 memoryUpdateMapMBC3();
2733 break;
2734 case 0x19:
2735 case 0x1a:
2736 case 0x1b:
2737 // MBC5
2738 memoryUpdateMapMBC5();
2739 break;
2740 case 0x1c:
2741 case 0x1d:
2742 case 0x1e:
2743 // MBC 5 Rumble
2744 memoryUpdateMapMBC5();
2745 break;
2746 case 0x22:
2747 // MBC 7
2748 memoryUpdateMapMBC7();
2749 break;
2750 case 0xfe:
2751 // HuC3
2752 memoryUpdateMapHuC3();
2753 break;
2754 case 0xff:
2755 // HuC1
2756 memoryUpdateMapHuC1();
2757 break;
2760 if (gbCgbMode)
2762 if (!gbVram)
2763 gbVram = (u8 *)malloc(0x4000 + 4);
2764 if (!gbWram)
2765 gbWram = (u8 *)malloc(0x8000 + 4);
2766 utilGzRead(gzFile, gbVram, 0x4000);
2767 utilGzRead(gzFile, gbWram, 0x8000);
2769 int value = register_SVBK;
2770 if (value == 0)
2771 value = 1;
2773 gbMemoryMap[0x08] = &gbVram[register_VBK * 0x2000];
2774 gbMemoryMap[0x09] = &gbVram[register_VBK * 0x2000 + 0x1000];
2775 gbMemoryMap[0x0d] = &gbWram[value * 0x1000];
2777 else
2779 if (gbVram)
2781 free(gbVram);
2782 gbVram = NULL;
2784 if (gbWram)
2786 free(gbWram);
2787 gbWram = NULL;
2791 gbSoundReadGame(version, gzFile);
2793 #if 0
2794 if (gbBorderOn)
2796 gbSgbRenderBorder();
2799 systemRefreshScreen();
2800 #endif
2802 if (version > GBSAVE_GAME_VERSION_1)
2803 gbCheatsReadGame(gzFile, version);
2805 systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
2807 if (version >= GBSAVE_GAME_VERSION_11) // new to re-recording version:
2809 extern int32 sensorX, sensorY; // from SDL.cpp
2810 utilGzRead(gzFile, &sensorX, sizeof(sensorX));
2811 utilGzRead(gzFile, &sensorY, sizeof(sensorY));
2812 utilGzRead(gzFile, gbJoymask, 4 * sizeof(*gbJoymask)); // this has to be saved or old input will incorrectly get carried
2813 // back on loading a snapshot!
2815 bool8 movieSnapshot;
2816 utilGzRead(gzFile, &movieSnapshot, sizeof(movieSnapshot));
2817 if (VBAMovieActive() && !movieSnapshot)
2819 systemMessage(0, N_("Can't load a non-movie snapshot while a movie is active."));
2820 goto failedLoadGB;
2823 if (movieSnapshot) // even if a movie isn't active we still want to parse through this in case other stuff is added
2824 // later on in the save format
2826 uint32 movieInputDataSize = 0;
2827 utilGzRead(gzFile, &movieInputDataSize, sizeof(movieInputDataSize));
2828 uint8 *local_movie_data = new uint8 [movieInputDataSize];
2829 int readBytes = utilGzRead(gzFile, local_movie_data, movieInputDataSize);
2830 if (readBytes != movieInputDataSize)
2832 systemMessage(0, N_("Corrupt movie snapshot."));
2833 if (local_movie_data)
2834 delete [] local_movie_data;
2835 goto failedLoadGB;
2837 int code = VBAMovieUnfreeze(local_movie_data, movieInputDataSize);
2838 if (local_movie_data)
2839 delete [] local_movie_data;
2840 if (code != MOVIE_SUCCESS && VBAMovieActive())
2842 char errStr [1024];
2843 strcpy(errStr, "Failed to load movie snapshot");
2844 switch (code)
2846 case MOVIE_NOT_FROM_THIS_MOVIE:
2847 strcat(errStr, ";\nSnapshot not from this movie"); break;
2848 case MOVIE_NOT_FROM_A_MOVIE:
2849 strcat(errStr, ";\nNot a movie snapshot"); break; // shouldn't get here...
2850 case MOVIE_SNAPSHOT_INCONSISTENT:
2851 strcat(errStr, ";\nSnapshot inconsistent with movie"); break;
2852 case MOVIE_WRONG_FORMAT:
2853 strcat(errStr, ";\nWrong format"); break;
2855 strcat(errStr, ".");
2856 systemMessage(0, N_(errStr));
2857 goto failedLoadGB;
2860 utilGzRead(gzFile, &GBSystemCounters.frameCount, sizeof(GBSystemCounters.frameCount));
2863 if (version >= GBSAVE_GAME_VERSION_13) // new to rerecording 19.4 wip (svn r22+):
2865 utilGzRead(gzFile, &GBSystemCounters.lagCount, sizeof(GBSystemCounters.lagCount));
2866 utilGzRead(gzFile, &GBSystemCounters.lagged, sizeof(GBSystemCounters.lagged));
2867 utilGzRead(gzFile, &GBSystemCounters.laggedLast, sizeof(GBSystemCounters.laggedLast));
2870 if (backupSafe)
2872 remove(tempBackupName);
2873 tempFailCount = 0;
2876 for (int i = 0; i < 4; ++i)
2877 systemSetJoypad(i, gbJoymask[i] & 0xFFFF);
2879 // FIXME: horrible kludge
2880 memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask));
2882 VBAUpdateButtonPressDisplay();
2883 VBAUpdateFrameCountDisplay();
2884 systemRefreshScreen();
2885 return true;
2887 failedLoadGB:
2888 if (backupSafe)
2890 tempFailCount++;
2891 if (tempFailCount < 3) // fail no more than 2 times in a row
2892 gbReadSaveState(tempBackupName);
2893 remove(tempBackupName);
2895 return false;
2898 bool gbReadMemSaveState(char *memory, int available)
2900 gzFile gzFile = utilMemGzOpen(memory, available, "r");
2902 backupSafe = false;
2903 bool res = gbReadSaveStateFromStream(gzFile);
2904 backupSafe = true;
2906 utilGzClose(gzFile);
2908 return res;
2911 bool gbReadSaveState(const char *name)
2913 gzFile gzFile = utilGzOpen(name, "rb");
2915 if (gzFile == NULL)
2917 return false;
2920 bool res = gbReadSaveStateFromStream(gzFile);
2922 utilGzClose(gzFile);
2924 return res;
2927 bool gbWritePNGFile(const char *fileName)
2929 if (gbBorderOn)
2930 return utilWritePNGFile(fileName, 256, 224, pix);
2931 return utilWritePNGFile(fileName, 160, 144, pix);
2934 bool gbWriteBMPFile(const char *fileName)
2936 if (gbBorderOn)
2937 return utilWriteBMPFile(fileName, 256, 224, pix);
2938 return utilWriteBMPFile(fileName, 160, 144, pix);
2941 void gbCleanUp()
2943 newFrame = true;
2945 GBSystemCounters.frameCount = 0;
2946 GBSystemCounters.lagCount = 0;
2947 GBSystemCounters.extraCount = 0;
2948 GBSystemCounters.lagged = true;
2949 GBSystemCounters.laggedLast = true;
2951 if (gbRam != NULL)
2953 free(gbRam);
2954 gbRam = NULL;
2957 if (gbRom != NULL)
2959 free(gbRom);
2960 gbRom = NULL;
2963 if (gbMemory != NULL)
2965 free(gbMemory);
2966 gbMemory = NULL;
2969 if (gbLineBuffer != NULL)
2971 free(gbLineBuffer);
2972 gbLineBuffer = NULL;
2975 if (origPix != NULL)
2977 free(origPix);
2978 origPix = NULL;
2980 pix = NULL;
2982 gbSgbShutdown();
2984 if (gbVram != NULL)
2986 free(gbVram);
2987 gbVram = NULL;
2990 if (gbWram != NULL)
2992 free(gbWram);
2993 gbWram = NULL;
2996 systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
2998 memset(gbJoymask, 0, sizeof(gbJoymask));
2999 // FIXME: horrible kludge
3000 memset(s_gbJoymask, 0, sizeof(s_gbJoymask));
3002 systemClearJoypads();
3003 systemResetSensor();
3005 // gbLastTime = gbFrameCount = 0;
3006 systemRefreshScreen();
3009 bool gbLoadRom(const char *szFile)
3011 int size = 0;
3013 if (gbRom != NULL)
3015 gbCleanUp();
3018 systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
3020 gbRom = utilLoad(szFile,
3021 utilIsGBImage,
3022 NULL,
3023 size);
3024 if (!gbRom)
3025 return false;
3027 gbRomSize = size;
3029 return gbUpdateSizes();
3032 bool gbUpdateSizes()
3034 if (gbRom[0x148] > 8)
3036 systemMessage(MSG_UNSUPPORTED_ROM_SIZE,
3037 N_("Unsupported rom size %02x"), gbRom[0x148]);
3038 return false;
3041 if (gbRomSize < gbRomSizes[gbRom[0x148]])
3043 gbRom = (u8 *)realloc(gbRom, gbRomSizes[gbRom[0x148]]);
3045 gbRomSize = gbRomSizes[gbRom[0x148]];
3046 gbRomSizeMask = gbRomSizesMasks[gbRom[0x148]];
3048 if (gbRom[0x149] > 5)
3050 systemMessage(MSG_UNSUPPORTED_RAM_SIZE,
3051 N_("Unsupported ram size %02x"), gbRom[0x149]);
3052 return false;
3055 gbRamSize = gbRamSizes[gbRom[0x149]];
3056 gbRamSizeMask = gbRamSizesMasks[gbRom[0x149]];
3058 if (gbRamSize)
3060 gbRam = (u8 *)malloc(gbRamSize + 4);
3061 memset(gbRam, 0xFF, gbRamSize + 4);
3064 int type = gbRom[0x147];
3066 mapperReadRAM = NULL;
3068 switch (type)
3070 case 0x00:
3071 case 0x01:
3072 case 0x02:
3073 case 0x03:
3074 // MBC 1
3075 mapper = mapperMBC1ROM;
3076 mapperRAM = mapperMBC1RAM;
3077 break;
3078 case 0x05:
3079 case 0x06:
3080 // MBC2
3081 mapper = mapperMBC2ROM;
3082 mapperRAM = mapperMBC2RAM;
3083 gbRamSize = 0x200;
3084 gbRamSizeMask = 0x1ff;
3085 break;
3086 case 0x0f:
3087 case 0x10:
3088 case 0x11:
3089 case 0x12:
3090 case 0x13:
3091 // MBC 3
3092 mapper = mapperMBC3ROM;
3093 mapperRAM = mapperMBC3RAM;
3094 mapperReadRAM = mapperMBC3ReadRAM;
3095 break;
3096 case 0x19:
3097 case 0x1a:
3098 case 0x1b:
3099 // MBC5
3100 mapper = mapperMBC5ROM;
3101 mapperRAM = mapperMBC5RAM;
3102 break;
3103 case 0x1c:
3104 case 0x1d:
3105 case 0x1e:
3106 // MBC 5 Rumble
3107 mapper = mapperMBC5ROM;
3108 mapperRAM = mapperMBC5RAM;
3109 break;
3110 case 0x22:
3111 // MBC 7
3112 mapper = mapperMBC7ROM;
3113 mapperRAM = mapperMBC7RAM;
3114 mapperReadRAM = mapperMBC7ReadRAM;
3115 break;
3116 case 0xfe:
3117 // HuC3
3118 mapper = mapperHuC3ROM;
3119 mapperRAM = mapperHuC3RAM;
3120 mapperReadRAM = mapperHuC3ReadRAM;
3121 break;
3122 case 0xff:
3123 // HuC1
3124 mapper = mapperHuC1ROM;
3125 mapperRAM = mapperHuC1RAM;
3126 break;
3127 default:
3128 systemMessage(MSG_UNKNOWN_CARTRIDGE_TYPE,
3129 N_("Unknown cartridge type %02x"), type);
3130 return false;
3133 switch (type)
3135 case 0x03:
3136 case 0x06:
3137 case 0x0f:
3138 case 0x10:
3139 case 0x13:
3140 case 0x1b:
3141 case 0x1d:
3142 case 0x1e:
3143 case 0x22:
3144 case 0xff:
3145 gbBattery = 1;
3146 break;
3149 gbInit();
3150 gbReset();
3152 return true;
3155 void gbEmulate(int ticksToStop)
3157 //printf("RLM: Inside the GB!\n");
3158 gbRegister tempRegister;
3159 u8 tempValue;
3160 s8 offset;
3162 int clockTicks = 0;
3163 gbDmaTicks = 0;
3165 register int opcode = 0;
3167 u32 newmask = 0;
3168 //printf("RLM: newframe = %d\n", newFrame);
3169 if (newFrame)
3171 extern void VBAOnExitingFrameBoundary();
3172 VBAOnExitingFrameBoundary();
3173 //printf("RLM: exiting frame boundary?\n");
3174 // update joystick information
3175 systemReadJoypads();
3177 bool sensor = (gbRom[0x147] == 0x22);
3179 // read joystick
3180 if (gbSgbMode && gbSgbMultiplayer)
3182 if (gbSgbFourPlayers)
3184 gbJoymask[0] = systemGetJoypad(0, sensor);
3185 gbJoymask[1] = systemGetJoypad(1, false);
3186 gbJoymask[2] = systemGetJoypad(2, false);
3187 gbJoymask[3] = systemGetJoypad(3, false);
3189 else
3191 gbJoymask[0] = systemGetJoypad(0, sensor);
3192 gbJoymask[1] = systemGetJoypad(1, false);
3195 else
3197 gbJoymask[0] = systemGetJoypad(0, sensor);
3200 // FIXME: horrible kludge
3201 memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask));
3203 // if (sensor)
3204 // systemUpdateMotionSensor(0);
3206 newmask = gbJoymask[0];
3207 if (newmask & 0xFF)
3209 gbInterrupt |= 16;
3212 extButtons = (newmask >> 18);
3213 speedup = (extButtons & 1) != 0;
3215 VBAMovieResetIfRequested();
3216 //printf("RLM: before Lua functions\n");
3217 //CallRegisteredLuaFunctions(LUACALL_BEFOREEMULATION);
3218 //printf("RLM: after Lua functions\n");
3219 newFrame = false;
3223 for (;; )
3225 #ifndef FINAL_VERSION
3226 if (systemDebug)
3228 if (!(IFF & 0x80))
3230 if (systemDebug > 1)
3232 sprintf(gbBuffer, "PC=%04x AF=%04x BC=%04x DE=%04x HL=%04x SP=%04x I=%04x\n",
3233 PC.W, AF.W, BC.W, DE.W, HL.W, SP.W, IFF);
3235 else
3237 sprintf(gbBuffer, "PC=%04x I=%02x\n", PC.W, IFF);
3239 log(gbBuffer);
3242 #endif
3243 if (IFF & 0x80)
3245 if (register_LCDC & 0x80)
3247 clockTicks = gbLcdTicks;
3249 else
3250 clockTicks = 100;
3252 if (gbLcdMode == 1 && (gbLcdLYIncrementTicks < clockTicks))
3253 clockTicks = gbLcdLYIncrementTicks;
3255 if (gbSerialOn && (gbSerialTicks < clockTicks))
3256 clockTicks = gbSerialTicks;
3258 if (gbTimerOn && (gbTimerTicks < clockTicks))
3259 clockTicks = gbTimerTicks;
3261 if (soundTicks && (soundTicks < clockTicks))
3262 clockTicks = soundTicks;
3264 else
3266 opcode = gbReadOpcode(PC.W);
3267 CallRegisteredLuaMemHook(PC.W, 1, opcode, LUAMEMHOOK_EXEC);
3268 PC.W++;
3270 if (IFF & 0x100)
3272 IFF &= 0xff;
3273 PC.W--;
3276 clockTicks = gbCycles[opcode];
3278 switch (opcode)
3280 case 0xCB:
3281 // extended opcode
3282 //CallRegisteredLuaMemHook(PC.W, 1, opcode, LUAMEMHOOK_EXEC); // is this desired?
3283 opcode = gbReadOpcode(PC.W++);
3284 clockTicks = gbCyclesCB[opcode];
3285 switch (opcode)
3287 #include "gbCodesCB.h"
3289 break;
3290 #include "gbCodes.h"
3294 if (!emulating)
3295 return;
3297 if (gbDmaTicks)
3299 clockTicks += gbDmaTicks;
3300 gbDmaTicks = 0;
3303 if (gbSgbMode)
3305 if (gbSgbPacketTimeout)
3307 gbSgbPacketTimeout -= clockTicks;
3309 if (gbSgbPacketTimeout <= 0)
3310 gbSgbResetPacketState();
3314 ticksToStop -= clockTicks;
3316 // DIV register emulation
3317 gbDivTicks -= clockTicks;
3318 while (gbDivTicks <= 0)
3320 register_DIV++;
3321 gbDivTicks += GBDIV_CLOCK_TICKS;
3324 if (register_LCDC & 0x80)
3326 // LCD stuff
3327 gbLcdTicks -= clockTicks;
3328 if (gbLcdMode == 1)
3330 // during V-BLANK,we need to increment LY at the same rate!
3331 gbLcdLYIncrementTicks -= clockTicks;
3332 while (gbLcdLYIncrementTicks <= 0)
3334 gbLcdLYIncrementTicks += GBLY_INCREMENT_CLOCK_TICKS;
3336 if (register_LY < 153)
3338 register_LY++;
3340 gbCompareLYToLYC();
3342 if (register_LY >= 153)
3343 gbLcdLYIncrementTicks = 6;
3345 else
3347 register_LY = 0x00;
3348 // reset the window line
3349 gbWindowLine = -1;
3350 gbLcdLYIncrementTicks = GBLY_INCREMENT_CLOCK_TICKS * 2;
3351 gbCompareLYToLYC();
3356 // our counter is off, see what we need to do
3357 while (gbLcdTicks <= 0)
3359 int framesToSkip = systemFramesToSkip();
3361 switch (gbLcdMode)
3363 case 0:
3364 // H-Blank
3365 register_LY++;
3367 gbCompareLYToLYC();
3369 // check if we reached the V-Blank period
3370 if (register_LY == 144)
3372 // Yes, V-Blank
3373 // set the LY increment counter
3374 gbLcdLYIncrementTicks = gbLcdTicks + GBLY_INCREMENT_CLOCK_TICKS;
3375 gbLcdTicks += GBLCD_MODE_1_CLOCK_TICKS;
3376 gbLcdMode = 1;
3377 if (register_LCDC & 0x80)
3379 gbInterrupt |= 1; // V-Blank interrupt
3380 gbInterruptWait = 6;
3381 if (register_STAT & 0x10)
3382 gbInterrupt |= 2;
3385 systemFrame();
3387 ++gbFrameCount;
3388 u32 currentTime = systemGetClock();
3389 if (currentTime - gbLastTime >= 1000)
3391 systemShowSpeed(int(float(gbFrameCount) * 100000 / (float(currentTime - gbLastTime) * 60) + .5f));
3392 gbLastTime = currentTime;
3393 gbFrameCount = 0;
3396 ++GBSystemCounters.frameCount;
3397 if (GBSystemCounters.lagged)
3399 ++GBSystemCounters.lagCount;
3401 GBSystemCounters.laggedLast = GBSystemCounters.lagged;
3402 GBSystemCounters.lagged = true;
3404 extern void VBAOnEnteringFrameBoundary();
3405 VBAOnEnteringFrameBoundary();
3407 newFrame = true;
3409 pauseAfterFrameAdvance = systemPauseOnFrame();
3411 if (gbFrameSkipCount >= framesToSkip || pauseAfterFrameAdvance)
3413 if (gbBorderOn)
3414 gbSgbRenderBorder(); // clear unnecessary things on border (e.g. in-game text message)
3416 systemRenderFrame();
3417 gbFrameSkipCount = 0;
3419 bool capturePressed = (extButtons & 2) != 0;
3420 if (capturePressed && !capturePrevious)
3422 captureNumber = systemScreenCapture(captureNumber);
3424 capturePrevious = capturePressed && !pauseAfterFrameAdvance;
3426 else
3428 ++gbFrameSkipCount;
3431 if (pauseAfterFrameAdvance)
3433 systemSetPause(true);
3436 else
3438 // go the the OAM being accessed mode
3439 gbLcdTicks += GBLCD_MODE_2_CLOCK_TICKS;
3440 gbLcdMode = 2;
3442 // only one LCD interrupt per line. may need to generalize...
3443 if (!(register_STAT & 0x40) ||
3444 (register_LY != register_LYC))
3446 if ((register_STAT & 0x28) == 0x20)
3447 gbInterrupt |= 2;
3451 break;
3452 case 1:
3453 // V-Blank
3454 // next mode is OAM being accessed mode
3455 gbLcdTicks += GBLCD_MODE_2_CLOCK_TICKS;
3456 gbLcdMode = 2;
3457 if (!(register_STAT & 0x40) ||
3458 (register_LY != register_LYC))
3460 if ((register_STAT & 0x28) == 0x20)
3461 gbInterrupt |= 2;
3463 break;
3464 case 2:
3465 // OAM being accessed mode
3467 // next mode is OAM and VRAM in use
3468 gbLcdTicks += GBLCD_MODE_3_CLOCK_TICKS;
3469 gbLcdMode = 3;
3470 break;
3471 case 3:
3472 // OAM and VRAM in use
3473 // next mode is H-Blank
3474 if (register_LY < 144)
3476 if (!gbSgbMask)
3478 if (gbFrameSkipCount >= framesToSkip || pauseAfterFrameAdvance)
3480 gbRenderLine();
3481 gbDrawSprites();
3483 switch (systemColorDepth)
3485 case 16:
3488 u16 *dest = (u16 *)pix +
3489 (gbBorderLineSkip + 2) * (register_LY + gbBorderRowSkip + 1)
3490 + gbBorderColumnSkip;
3491 for (int x = 0; x < 160; )
3493 *dest++ = systemColorMap16[gbLineMix[x++]];
3494 *dest++ = systemColorMap16[gbLineMix[x++]];
3495 *dest++ = systemColorMap16[gbLineMix[x++]];
3496 *dest++ = systemColorMap16[gbLineMix[x++]];
3498 *dest++ = systemColorMap16[gbLineMix[x++]];
3499 *dest++ = systemColorMap16[gbLineMix[x++]];
3500 *dest++ = systemColorMap16[gbLineMix[x++]];
3501 *dest++ = systemColorMap16[gbLineMix[x++]];
3503 *dest++ = systemColorMap16[gbLineMix[x++]];
3504 *dest++ = systemColorMap16[gbLineMix[x++]];
3505 *dest++ = systemColorMap16[gbLineMix[x++]];
3506 *dest++ = systemColorMap16[gbLineMix[x++]];
3508 *dest++ = systemColorMap16[gbLineMix[x++]];
3509 *dest++ = systemColorMap16[gbLineMix[x++]];
3510 *dest++ = systemColorMap16[gbLineMix[x++]];
3511 *dest++ = systemColorMap16[gbLineMix[x++]];
3513 if (gbBorderOn)
3514 dest += gbBorderColumnSkip;
3515 *dest++ = 0; // for filters that read one pixel more
3516 break;
3518 case 24:
3521 u8 *dest = (u8 *)pix +
3522 3 * (gbBorderLineSkip * (register_LY + gbBorderRowSkip) +
3523 gbBorderColumnSkip);
3524 for (int x = 0; x < 160; )
3526 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3527 dest += 3;
3528 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3529 dest += 3;
3530 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3531 dest += 3;
3532 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3533 dest += 3;
3535 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3536 dest += 3;
3537 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3538 dest += 3;
3539 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3540 dest += 3;
3541 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3542 dest += 3;
3544 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3545 dest += 3;
3546 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3547 dest += 3;
3548 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3549 dest += 3;
3550 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3551 dest += 3;
3553 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3554 dest += 3;
3555 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3556 dest += 3;
3557 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3558 dest += 3;
3559 *((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
3560 dest += 3;
3562 break;
3564 case 32:
3567 u32 *dest = (u32 *)pix +
3568 (gbBorderLineSkip + 1) * (register_LY + gbBorderRowSkip + 1)
3569 + gbBorderColumnSkip;
3570 for (int x = 0; x < 160; )
3572 *dest++ = systemColorMap32[gbLineMix[x++]];
3573 *dest++ = systemColorMap32[gbLineMix[x++]];
3574 *dest++ = systemColorMap32[gbLineMix[x++]];
3575 *dest++ = systemColorMap32[gbLineMix[x++]];
3577 *dest++ = systemColorMap32[gbLineMix[x++]];
3578 *dest++ = systemColorMap32[gbLineMix[x++]];
3579 *dest++ = systemColorMap32[gbLineMix[x++]];
3580 *dest++ = systemColorMap32[gbLineMix[x++]];
3582 *dest++ = systemColorMap32[gbLineMix[x++]];
3583 *dest++ = systemColorMap32[gbLineMix[x++]];
3584 *dest++ = systemColorMap32[gbLineMix[x++]];
3585 *dest++ = systemColorMap32[gbLineMix[x++]];
3587 *dest++ = systemColorMap32[gbLineMix[x++]];
3588 *dest++ = systemColorMap32[gbLineMix[x++]];
3589 *dest++ = systemColorMap32[gbLineMix[x++]];
3590 *dest++ = systemColorMap32[gbLineMix[x++]];
3592 break;
3598 gbLcdTicks += GBLCD_MODE_0_CLOCK_TICKS;
3599 gbLcdMode = 0;
3600 // only one LCD interrupt per line. may need to generalize...
3601 if (!(register_STAT & 0x40) ||
3602 (register_LY != register_LYC))
3604 if (register_STAT & 0x08)
3605 gbInterrupt |= 2;
3607 if (gbHdmaOn)
3609 gbDoHdma();
3611 break;
3613 // mark the correct lcd mode on STAT register
3614 register_STAT = (register_STAT & 0xfc) | gbLcdMode;
3618 // serial emulation
3619 if (gbSerialOn)
3621 #ifdef LINK_EMULATION
3622 if (linkConnected)
3624 gbSerialTicks -= clockTicks;
3626 while (gbSerialTicks <= 0)
3628 // increment number of shifted bits
3629 gbSerialBits++;
3630 linkProc();
3631 if (gbSerialOn && (gbMemory[0xff02] & 1))
3633 if (gbSerialBits == 8)
3635 gbSerialBits = 0;
3636 gbMemory[0xff01] = 0xff;
3637 gbMemory[0xff02] &= 0x7f;
3638 gbSerialOn = 0;
3639 gbInterrupt |= 8;
3640 gbSerialTicks = 0;
3643 gbSerialTicks += GBSERIAL_CLOCK_TICKS;
3646 else
3648 #endif
3649 if (gbMemory[0xff02] & 1)
3651 gbSerialTicks -= clockTicks;
3653 // overflow
3654 while (gbSerialTicks <= 0)
3656 // shift serial byte to right and put a 1 bit in its place
3657 // gbMemory[0xff01] = 0x80 | (gbMemory[0xff01]>>1);
3658 // increment number of shifted bits
3659 gbSerialBits++;
3660 if (gbSerialBits == 8)
3662 // end of transmission
3663 if (gbSerialFunction) // external device
3664 gbMemory[0xff01] = gbSerialFunction(gbMemory[0xff01]);
3665 else
3666 gbMemory[0xff01] = 0xff;
3667 gbSerialTicks = 0;
3668 gbMemory[0xff02] &= 0x7f;
3669 gbSerialOn = 0;
3670 gbInterrupt |= 8;
3671 gbSerialBits = 0;
3673 else
3674 gbSerialTicks += GBSERIAL_CLOCK_TICKS;
3677 #ifdef LINK_EMULATION
3679 #endif
3682 // timer emulation
3683 if (gbTimerOn)
3685 gbTimerTicks -= clockTicks;
3687 while (gbTimerTicks <= 0)
3689 register_TIMA++;
3691 if (register_TIMA == 0)
3693 // timer overflow!
3695 // reload timer modulo
3696 register_TIMA = register_TMA;
3698 // flag interrupt
3699 gbInterrupt |= 4;
3702 gbTimerTicks += gbTimerClockTicks;
3706 /*
3707 if(soundOffFlag)
3709 if(synchronize && !speedup)
3711 synchronizeTicks -= clockTicks;
3713 while(synchronizeTicks < 0)
3715 synchronizeTicks += SYNCHRONIZE_CLOCK_TICKS;
3717 DWORD now = timeGetTime();
3718 gbElapsedTime += (now - timeNow);
3720 if(gbElapsedTime < 50)
3722 DWORD diff = 50 - gbElapsedTime;
3723 Sleep(diff);
3724 timeNow = timeGetTime();
3725 elapsedTime = timeNow - now - diff;
3726 if((int)elapsedTime < 0)
3727 elapsedTime = 0;
3728 } else
3730 timeNow = timeGetTime();
3731 elapsedTime = 0;
3736 */
3738 soundTicks -= clockTicks;
3739 while (soundTicks < 0) // must be < 1 when soundtick_t is real data type
3741 soundTicks += SOUND_CLOCK_TICKS;
3743 gbSoundTick();
3746 register_IF = gbInterrupt;
3748 if (IFF & 0x20)
3750 IFF &= 0xdf;
3751 IFF |= 0x01;
3752 gbInterruptWait = 0;
3754 else if (gbInterrupt)
3756 if (gbInterruptWait == 0)
3758 // gbInterruptWait = 0;
3760 if (IFF & 0x01)
3762 if ((gbInterrupt & 1) && (register_IE & 1))
3764 gbVblank_interrupt();
3765 continue;
3768 if ((gbInterrupt & 2) && (register_IE & 2))
3770 gbLcd_interrupt();
3771 continue;
3774 if ((gbInterrupt & 4) && (register_IE & 4))
3776 gbTimer_interrupt();
3777 continue;
3780 if ((gbInterrupt & 8) && (register_IE & 8))
3782 gbSerial_interrupt();
3783 continue;
3786 if ((gbInterrupt & 16) && (register_IE & 16))
3788 gbJoypad_interrupt();
3789 continue;
3793 else
3795 gbInterruptWait -= clockTicks;
3796 if (gbInterruptWait < 0)
3797 gbInterruptWait = 0;
3801 if (useOldFrameTiming)
3803 // old timing code
3804 if (ticksToStop > 0)
3805 continue;
3807 else
3809 if (!newFrame && (register_LCDC & 0x80) != 0)
3810 continue;
3813 if (!(register_LCDC & 0x80))
3815 if (!useOldFrameTiming)
3817 // FIXME: since register_LY can be reset to 0 by some games, frame length is variable
3818 // and infinite loops can occurr
3819 // for now, it IS necessary to do something on this condition or games like
3820 // Megaman would freeze upon low-level restart interrupt sequence (Start+Select+A+B).
3821 // the only sensible way to fix this issue is to implement the RIGHT frame timing
3822 #ifdef WANTS_INCOMPLETE_WORKAROUND
3823 if (systemReadJoypads())
3825 if (gbSgbMode && gbSgbMultiplayer)
3827 if (gbSgbFourPlayers)
3829 gbJoymask[0] = systemGetJoypad(0, false);
3830 gbJoymask[1] = systemGetJoypad(1, false);
3831 gbJoymask[2] = systemGetJoypad(2, false);
3832 gbJoymask[3] = systemGetJoypad(3, false);
3834 else
3836 gbJoymask[0] = systemGetJoypad(0, false);
3837 gbJoymask[1] = systemGetJoypad(1, false);
3840 else
3842 gbJoymask[0] = systemGetJoypad(0, false);
3845 else
3846 gbJoymask[0] = gbJoymask[1] = gbJoymask[2] = gbJoymask[3] = 0;
3847 #else
3848 #endif
3852 // makes sure frames are really divided across input sampling boundaries which occur at a constant rate
3853 if (newFrame || useOldFrameTiming)
3855 /// extern void VBAOnEnteringFrameBoundary();
3856 /// VBAOnEnteringFrameBoundary();
3858 break;
3865 //RLM:
3866 int getRamSize(){
3867 return gbRamSize;
3870 int getRomSize(){
3871 return gbRomSize;
3875 struct EmulatedSystem GBSystem =
3877 // emuMain
3878 gbEmulate,
3879 // emuReset
3880 gbReset,
3881 // emuCleanUp
3882 gbCleanUp,
3883 // emuReadBattery
3884 gbReadBatteryFile,
3885 // emuWriteBattery
3886 gbWriteBatteryFile,
3887 // emuReadBatteryFromStream
3888 gbReadBatteryFromStream,
3889 // emuWriteBatteryToStream
3890 gbWriteBatteryToStream,
3891 // emuReadState
3892 gbReadSaveState,
3893 // emuWriteState
3894 gbWriteSaveState,
3895 // emuReadStateFromStream
3896 gbReadSaveStateFromStream,
3897 // emuWriteStateToStream
3898 gbWriteSaveStateToStream,
3899 // emuReadMemState
3900 gbReadMemSaveState,
3901 // emuWriteMemState
3902 gbWriteMemSaveState,
3903 // emuWritePNG
3904 gbWritePNGFile,
3905 // emuWriteBMP
3906 gbWriteBMPFile,
3907 // emuUpdateCPSR
3908 NULL,
3909 // emuHasDebugger
3910 false,
3911 // emuCount
3912 #ifdef FINAL_VERSION
3913 70000 / 4,
3914 #else
3915 1000,
3916 #endif
3917 };
3919 // is there a reason to use more than one set of counters?
3920 EmulatedSystemCounters &GBSystemCounters = systemCounters;
3922 /*
3923 EmulatedSystemCounters GBSystemCounters =
3925 // frameCount
3926 0,
3927 // lagCount
3928 0,
3929 // lagged
3930 true,
3931 // laggedLast
3932 true,
3933 };
3934 */