diff src/gb/GB.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children 44974c3e093b
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gb/GB.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,3918 @@
     1.4 +#include <cstdio>
     1.5 +#include <cstdlib>
     1.6 +#include <cstring>
     1.7 +#include <cassert>
     1.8 +
     1.9 +#include "../Port.h"
    1.10 +#include "../NLS.h"
    1.11 +#include "GB.h"
    1.12 +#include "gbCheats.h"
    1.13 +#include "gbGlobals.h"
    1.14 +#include "gbMemory.h"
    1.15 +#include "gbSGB.h"
    1.16 +#include "gbSound.h"
    1.17 +#include "../common/unzip.h"
    1.18 +#include "../common/Util.h"
    1.19 +#include "../common/System.h"
    1.20 +#include "../common/movie.h"
    1.21 +#include "../common/vbalua.h"
    1.22 +
    1.23 +#ifdef __GNUC__
    1.24 +#define _stricmp strcasecmp
    1.25 +#endif
    1.26 +
    1.27 +// FIXME: constant (GB) or boolean (GBA)?!
    1.28 +#define C_FLAG 0x10
    1.29 +#define H_FLAG 0x20
    1.30 +#define N_FLAG 0x40
    1.31 +#define Z_FLAG 0x80
    1.32 +extern soundtick_t GB_USE_TICKS_AS;
    1.33 +
    1.34 +u8 *		 origPix = NULL;
    1.35 +extern u8 *	 pix;
    1.36 +extern u32	 extButtons;
    1.37 +extern bool8 capturePrevious;
    1.38 +extern int32 captureNumber;
    1.39 +extern bool8 speedup;
    1.40 +
    1.41 +bool gbUpdateSizes();
    1.42 +
    1.43 +// debugging
    1.44 +bool memorydebug = false;
    1.45 +char gbBuffer[2048];
    1.46 +
    1.47 +extern u16 gbLineMix[160];
    1.48 +
    1.49 +// mappers
    1.50 +void (*mapper)(u16, u8)	   = NULL;
    1.51 +void (*mapperRAM)(u16, u8) = NULL;
    1.52 +u8	 (*mapperReadRAM)(u16) = NULL;
    1.53 +
    1.54 +// registers
    1.55 +gbRegister PC;
    1.56 +gbRegister SP;
    1.57 +gbRegister AF;
    1.58 +gbRegister BC;
    1.59 +gbRegister DE;
    1.60 +gbRegister HL;
    1.61 +u16		   IFF;
    1.62 +// 0xff04
    1.63 +u8 register_DIV = 0;
    1.64 +// 0xff05
    1.65 +u8 register_TIMA = 0;
    1.66 +// 0xff06
    1.67 +u8 register_TMA = 0;
    1.68 +// 0xff07
    1.69 +u8 register_TAC = 0;
    1.70 +// 0xff0f
    1.71 +u8 register_IF = 0;
    1.72 +// 0xff40
    1.73 +u8 register_LCDC = 0;
    1.74 +// 0xff41
    1.75 +u8 register_STAT = 0;
    1.76 +// 0xff42
    1.77 +u8 register_SCY = 0;
    1.78 +// 0xff43
    1.79 +u8 register_SCX = 0;
    1.80 +// 0xff44
    1.81 +u8 register_LY = 0;
    1.82 +// 0xff45
    1.83 +u8 register_LYC = 0;
    1.84 +// 0xff46
    1.85 +u8 register_DMA = 0;
    1.86 +// 0xff4a
    1.87 +u8 register_WY = 0;
    1.88 +// 0xff4b
    1.89 +u8 register_WX = 0;
    1.90 +// 0xff4f
    1.91 +u8 register_VBK = 0;
    1.92 +// 0xff51
    1.93 +u8 register_HDMA1 = 0;
    1.94 +// 0xff52
    1.95 +u8 register_HDMA2 = 0;
    1.96 +// 0xff53
    1.97 +u8 register_HDMA3 = 0;
    1.98 +// 0xff54
    1.99 +u8 register_HDMA4 = 0;
   1.100 +// 0xff55
   1.101 +u8 register_HDMA5 = 0;
   1.102 +// 0xff70
   1.103 +u8 register_SVBK = 0;
   1.104 +// 0xffff
   1.105 +u8 register_IE = 0;
   1.106 +
   1.107 +// ticks definition
   1.108 +int32 GBDIV_CLOCK_TICKS = 64;
   1.109 +int32 GBLCD_MODE_0_CLOCK_TICKS	 = 51;
   1.110 +int32 GBLCD_MODE_1_CLOCK_TICKS	 = 1140;
   1.111 +int32 GBLCD_MODE_2_CLOCK_TICKS	 = 20;
   1.112 +int32 GBLCD_MODE_3_CLOCK_TICKS	 = 43;
   1.113 +int32 GBLY_INCREMENT_CLOCK_TICKS = 114;
   1.114 +int32 GBTIMER_MODE_0_CLOCK_TICKS = 256;
   1.115 +int32 GBTIMER_MODE_1_CLOCK_TICKS = 4;
   1.116 +int32 GBTIMER_MODE_2_CLOCK_TICKS = 16;
   1.117 +int32 GBTIMER_MODE_3_CLOCK_TICKS = 64;
   1.118 +int32 GBSERIAL_CLOCK_TICKS		 = 128;
   1.119 +int32 GBSYNCHRONIZE_CLOCK_TICKS	 = 52920;
   1.120 +
   1.121 +// state variables
   1.122 +
   1.123 +// interrupt
   1.124 +int32 gbInterrupt	  = 0;
   1.125 +int32 gbInterruptWait = 0;
   1.126 +// serial
   1.127 +int32 gbSerialOn	= 0;
   1.128 +int32 gbSerialTicks = 0;
   1.129 +int32 gbSerialBits	= 0;
   1.130 +// timer
   1.131 +int32 gbTimerOn			= 0;
   1.132 +int32 gbTimerTicks		= 0;
   1.133 +int32 gbTimerClockTicks = 0;
   1.134 +int32 gbTimerMode		= 0;
   1.135 +// lcd
   1.136 +int32 gbLcdMode	 = 2;
   1.137 +int32 gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS;
   1.138 +int32 gbLcdLYIncrementTicks = 0;
   1.139 +// div
   1.140 +int32 gbDivTicks = GBDIV_CLOCK_TICKS;
   1.141 +// cgb
   1.142 +int32 gbVramBank		= 0;
   1.143 +int32 gbWramBank		= 1;
   1.144 +int32 gbHdmaSource		= 0x0000;
   1.145 +int32 gbHdmaDestination = 0x8000;
   1.146 +int32 gbHdmaBytes		= 0x0000;
   1.147 +int32 gbHdmaOn = 0;
   1.148 +int32 gbSpeed  = 0;
   1.149 +// frame counting
   1.150 +int32 gbFrameCount	   = 0;
   1.151 +int32 gbFrameSkip	   = 0;
   1.152 +int32 gbFrameSkipCount = 0;
   1.153 +// timing
   1.154 +u32	  gbLastTime		 = 0;
   1.155 +u32	  gbElapsedTime		 = 0;
   1.156 +u32	  gbTimeNow			 = 0;
   1.157 +int32 gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS;
   1.158 +int32 gbDMASpeedVersion	 = 1;
   1.159 +// emulator features
   1.160 +int32 gbBattery	   = 0;
   1.161 +int32 gbJoymask[4] = { 0, 0, 0, 0 };
   1.162 +
   1.163 +int32 gbEchoRAMFixOn = 1;
   1.164 +
   1.165 +static bool newFrame = true;
   1.166 +static bool pauseAfterFrameAdvance = false;
   1.167 +
   1.168 +int32 gbRomSizes[] = { 0x00008000, // 32K
   1.169 +	                   0x00010000, // 64K
   1.170 +	                   0x00020000, // 128K
   1.171 +	                   0x00040000, // 256K
   1.172 +	                   0x00080000, // 512K
   1.173 +	                   0x00100000, // 1024K
   1.174 +	                   0x00200000, // 2048K
   1.175 +	                   0x00400000, // 4096K
   1.176 +	                   0x00800000 // 8192K
   1.177 +};
   1.178 +int32 gbRomSizesMasks[] = { 0x00007fff,
   1.179 +	                        0x0000ffff,
   1.180 +	                        0x0001ffff,
   1.181 +	                        0x0003ffff,
   1.182 +	                        0x0007ffff,
   1.183 +	                        0x000fffff,
   1.184 +	                        0x001fffff,
   1.185 +	                        0x003fffff,
   1.186 +	                        0x007fffff };
   1.187 +
   1.188 +int32 gbRamSizes[6] = { 0x00000000, // 0K
   1.189 +	                    0x00000800, // 2K
   1.190 +	                    0x00002000, // 8K
   1.191 +	                    0x00008000, // 32K
   1.192 +	                    0x00020000, // 128K
   1.193 +	                    0x00010000 // 64K
   1.194 +};
   1.195 +
   1.196 +int32 gbRamSizesMasks[6] = { 0x00000000,
   1.197 +	                         0x000007ff,
   1.198 +	                         0x00001fff,
   1.199 +	                         0x00007fff,
   1.200 +	                         0x0001ffff,
   1.201 +	                         0x0000ffff };
   1.202 +
   1.203 +int32 gbCycles[] =
   1.204 +{
   1.205 +//  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
   1.206 +	1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1,  // 0
   1.207 +	1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1,  // 1
   1.208 +	2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1,  // 2
   1.209 +	2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1,  // 3
   1.210 +	1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 4
   1.211 +	1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 5
   1.212 +	1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 6
   1.213 +	2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1,  // 7
   1.214 +	1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 8
   1.215 +	1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // 9
   1.216 +	1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // a
   1.217 +	1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,  // b
   1.218 +	2, 3, 3, 4, 3, 4, 2, 4, 2, 4, 3, 2, 3, 6, 2, 4,  // c
   1.219 +	2, 3, 3, 0, 3, 4, 2, 4, 2, 4, 3, 0, 3, 0, 2, 4,  // d
   1.220 +	3, 3, 2, 0, 0, 4, 2, 4, 4, 1, 4, 0, 0, 0, 2, 4,  // e
   1.221 +	3, 3, 2, 1, 0, 4, 2, 4, 3, 2, 4, 1, 0, 0, 2, 4   // f
   1.222 +};
   1.223 +
   1.224 +int32 gbCyclesCB[] =
   1.225 +{
   1.226 +//  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
   1.227 +	2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 0
   1.228 +	2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 1
   1.229 +	2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 2
   1.230 +	2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2,  // 3
   1.231 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 4
   1.232 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 5
   1.233 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 6
   1.234 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 7
   1.235 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 8
   1.236 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // 9
   1.237 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // a
   1.238 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // b
   1.239 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // c
   1.240 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // d
   1.241 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2,  // e
   1.242 +	2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2   // f
   1.243 +};
   1.244 +
   1.245 +u16 DAATable[] =
   1.246 +{
   1.247 +	0x0080, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700,
   1.248 +	0x0800, 0x0900, 0x1020, 0x1120, 0x1220, 0x1320, 0x1420, 0x1520,
   1.249 +	0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500, 0x1600, 0x1700,
   1.250 +	0x1800, 0x1900, 0x2020, 0x2120, 0x2220, 0x2320, 0x2420, 0x2520,
   1.251 +	0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2600, 0x2700,
   1.252 +	0x2800, 0x2900, 0x3020, 0x3120, 0x3220, 0x3320, 0x3420, 0x3520,
   1.253 +	0x3000, 0x3100, 0x3200, 0x3300, 0x3400, 0x3500, 0x3600, 0x3700,
   1.254 +	0x3800, 0x3900, 0x4020, 0x4120, 0x4220, 0x4320, 0x4420, 0x4520,
   1.255 +	0x4000, 0x4100, 0x4200, 0x4300, 0x4400, 0x4500, 0x4600, 0x4700,
   1.256 +	0x4800, 0x4900, 0x5020, 0x5120, 0x5220, 0x5320, 0x5420, 0x5520,
   1.257 +	0x5000, 0x5100, 0x5200, 0x5300, 0x5400, 0x5500, 0x5600, 0x5700,
   1.258 +	0x5800, 0x5900, 0x6020, 0x6120, 0x6220, 0x6320, 0x6420, 0x6520,
   1.259 +	0x6000, 0x6100, 0x6200, 0x6300, 0x6400, 0x6500, 0x6600, 0x6700,
   1.260 +	0x6800, 0x6900, 0x7020, 0x7120, 0x7220, 0x7320, 0x7420, 0x7520,
   1.261 +	0x7000, 0x7100, 0x7200, 0x7300, 0x7400, 0x7500, 0x7600, 0x7700,
   1.262 +	0x7800, 0x7900, 0x8020, 0x8120, 0x8220, 0x8320, 0x8420, 0x8520,
   1.263 +	0x8000, 0x8100, 0x8200, 0x8300, 0x8400, 0x8500, 0x8600, 0x8700,
   1.264 +	0x8800, 0x8900, 0x9020, 0x9120, 0x9220, 0x9320, 0x9420, 0x9520,
   1.265 +	0x9000, 0x9100, 0x9200, 0x9300, 0x9400, 0x9500, 0x9600, 0x9700,
   1.266 +	0x9800, 0x9900, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530,
   1.267 +	0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510, 0x0610, 0x0710,
   1.268 +	0x0810, 0x0910, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530,
   1.269 +	0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510, 0x1610, 0x1710,
   1.270 +	0x1810, 0x1910, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530,
   1.271 +	0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510, 0x2610, 0x2710,
   1.272 +	0x2810, 0x2910, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530,
   1.273 +	0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510, 0x3610, 0x3710,
   1.274 +	0x3810, 0x3910, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530,
   1.275 +	0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510, 0x4610, 0x4710,
   1.276 +	0x4810, 0x4910, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530,
   1.277 +	0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510, 0x5610, 0x5710,
   1.278 +	0x5810, 0x5910, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530,
   1.279 +	0x6010, 0x6110, 0x6210, 0x6310, 0x6410, 0x6510, 0x6610, 0x6710,
   1.280 +	0x6810, 0x6910, 0x7030, 0x7130, 0x7230, 0x7330, 0x7430, 0x7530,
   1.281 +	0x7010, 0x7110, 0x7210, 0x7310, 0x7410, 0x7510, 0x7610, 0x7710,
   1.282 +	0x7810, 0x7910, 0x8030, 0x8130, 0x8230, 0x8330, 0x8430, 0x8530,
   1.283 +	0x8010, 0x8110, 0x8210, 0x8310, 0x8410, 0x8510, 0x8610, 0x8710,
   1.284 +	0x8810, 0x8910, 0x9030, 0x9130, 0x9230, 0x9330, 0x9430, 0x9530,
   1.285 +	0x9010, 0x9110, 0x9210, 0x9310, 0x9410, 0x9510, 0x9610, 0x9710,
   1.286 +	0x9810, 0x9910, 0xA030, 0xA130, 0xA230, 0xA330, 0xA430, 0xA530,
   1.287 +	0xA010, 0xA110, 0xA210, 0xA310, 0xA410, 0xA510, 0xA610, 0xA710,
   1.288 +	0xA810, 0xA910, 0xB030, 0xB130, 0xB230, 0xB330, 0xB430, 0xB530,
   1.289 +	0xB010, 0xB110, 0xB210, 0xB310, 0xB410, 0xB510, 0xB610, 0xB710,
   1.290 +	0xB810, 0xB910, 0xC030, 0xC130, 0xC230, 0xC330, 0xC430, 0xC530,
   1.291 +	0xC010, 0xC110, 0xC210, 0xC310, 0xC410, 0xC510, 0xC610, 0xC710,
   1.292 +	0xC810, 0xC910, 0xD030, 0xD130, 0xD230, 0xD330, 0xD430, 0xD530,
   1.293 +	0xD010, 0xD110, 0xD210, 0xD310, 0xD410, 0xD510, 0xD610, 0xD710,
   1.294 +	0xD810, 0xD910, 0xE030, 0xE130, 0xE230, 0xE330, 0xE430, 0xE530,
   1.295 +	0xE010, 0xE110, 0xE210, 0xE310, 0xE410, 0xE510, 0xE610, 0xE710,
   1.296 +	0xE810, 0xE910, 0xF030, 0xF130, 0xF230, 0xF330, 0xF430, 0xF530,
   1.297 +	0xF010, 0xF110, 0xF210, 0xF310, 0xF410, 0xF510, 0xF610, 0xF710,
   1.298 +	0xF810, 0xF910, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530,
   1.299 +	0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510, 0x0610, 0x0710,
   1.300 +	0x0810, 0x0910, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530,
   1.301 +	0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510, 0x1610, 0x1710,
   1.302 +	0x1810, 0x1910, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530,
   1.303 +	0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510, 0x2610, 0x2710,
   1.304 +	0x2810, 0x2910, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530,
   1.305 +	0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510, 0x3610, 0x3710,
   1.306 +	0x3810, 0x3910, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530,
   1.307 +	0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510, 0x4610, 0x4710,
   1.308 +	0x4810, 0x4910, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530,
   1.309 +	0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510, 0x5610, 0x5710,
   1.310 +	0x5810, 0x5910, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530,
   1.311 +	0x0600, 0x0700, 0x0800, 0x0900, 0x0A00, 0x0B00, 0x0C00, 0x0D00,
   1.312 +	0x0E00, 0x0F00, 0x1020, 0x1120, 0x1220, 0x1320, 0x1420, 0x1520,
   1.313 +	0x1600, 0x1700, 0x1800, 0x1900, 0x1A00, 0x1B00, 0x1C00, 0x1D00,
   1.314 +	0x1E00, 0x1F00, 0x2020, 0x2120, 0x2220, 0x2320, 0x2420, 0x2520,
   1.315 +	0x2600, 0x2700, 0x2800, 0x2900, 0x2A00, 0x2B00, 0x2C00, 0x2D00,
   1.316 +	0x2E00, 0x2F00, 0x3020, 0x3120, 0x3220, 0x3320, 0x3420, 0x3520,
   1.317 +	0x3600, 0x3700, 0x3800, 0x3900, 0x3A00, 0x3B00, 0x3C00, 0x3D00,
   1.318 +	0x3E00, 0x3F00, 0x4020, 0x4120, 0x4220, 0x4320, 0x4420, 0x4520,
   1.319 +	0x4600, 0x4700, 0x4800, 0x4900, 0x4A00, 0x4B00, 0x4C00, 0x4D00,
   1.320 +	0x4E00, 0x4F00, 0x5020, 0x5120, 0x5220, 0x5320, 0x5420, 0x5520,
   1.321 +	0x5600, 0x5700, 0x5800, 0x5900, 0x5A00, 0x5B00, 0x5C00, 0x5D00,
   1.322 +	0x5E00, 0x5F00, 0x6020, 0x6120, 0x6220, 0x6320, 0x6420, 0x6520,
   1.323 +	0x6600, 0x6700, 0x6800, 0x6900, 0x6A00, 0x6B00, 0x6C00, 0x6D00,
   1.324 +	0x6E00, 0x6F00, 0x7020, 0x7120, 0x7220, 0x7320, 0x7420, 0x7520,
   1.325 +	0x7600, 0x7700, 0x7800, 0x7900, 0x7A00, 0x7B00, 0x7C00, 0x7D00,
   1.326 +	0x7E00, 0x7F00, 0x8020, 0x8120, 0x8220, 0x8320, 0x8420, 0x8520,
   1.327 +	0x8600, 0x8700, 0x8800, 0x8900, 0x8A00, 0x8B00, 0x8C00, 0x8D00,
   1.328 +	0x8E00, 0x8F00, 0x9020, 0x9120, 0x9220, 0x9320, 0x9420, 0x9520,
   1.329 +	0x9600, 0x9700, 0x9800, 0x9900, 0x9A00, 0x9B00, 0x9C00, 0x9D00,
   1.330 +	0x9E00, 0x9F00, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530,
   1.331 +	0x0610, 0x0710, 0x0810, 0x0910, 0x0A10, 0x0B10, 0x0C10, 0x0D10,
   1.332 +	0x0E10, 0x0F10, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530,
   1.333 +	0x1610, 0x1710, 0x1810, 0x1910, 0x1A10, 0x1B10, 0x1C10, 0x1D10,
   1.334 +	0x1E10, 0x1F10, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530,
   1.335 +	0x2610, 0x2710, 0x2810, 0x2910, 0x2A10, 0x2B10, 0x2C10, 0x2D10,
   1.336 +	0x2E10, 0x2F10, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530,
   1.337 +	0x3610, 0x3710, 0x3810, 0x3910, 0x3A10, 0x3B10, 0x3C10, 0x3D10,
   1.338 +	0x3E10, 0x3F10, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530,
   1.339 +	0x4610, 0x4710, 0x4810, 0x4910, 0x4A10, 0x4B10, 0x4C10, 0x4D10,
   1.340 +	0x4E10, 0x4F10, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530,
   1.341 +	0x5610, 0x5710, 0x5810, 0x5910, 0x5A10, 0x5B10, 0x5C10, 0x5D10,
   1.342 +	0x5E10, 0x5F10, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530,
   1.343 +	0x6610, 0x6710, 0x6810, 0x6910, 0x6A10, 0x6B10, 0x6C10, 0x6D10,
   1.344 +	0x6E10, 0x6F10, 0x7030, 0x7130, 0x7230, 0x7330, 0x7430, 0x7530,
   1.345 +	0x7610, 0x7710, 0x7810, 0x7910, 0x7A10, 0x7B10, 0x7C10, 0x7D10,
   1.346 +	0x7E10, 0x7F10, 0x8030, 0x8130, 0x8230, 0x8330, 0x8430, 0x8530,
   1.347 +	0x8610, 0x8710, 0x8810, 0x8910, 0x8A10, 0x8B10, 0x8C10, 0x8D10,
   1.348 +	0x8E10, 0x8F10, 0x9030, 0x9130, 0x9230, 0x9330, 0x9430, 0x9530,
   1.349 +	0x9610, 0x9710, 0x9810, 0x9910, 0x9A10, 0x9B10, 0x9C10, 0x9D10,
   1.350 +	0x9E10, 0x9F10, 0xA030, 0xA130, 0xA230, 0xA330, 0xA430, 0xA530,
   1.351 +	0xA610, 0xA710, 0xA810, 0xA910, 0xAA10, 0xAB10, 0xAC10, 0xAD10,
   1.352 +	0xAE10, 0xAF10, 0xB030, 0xB130, 0xB230, 0xB330, 0xB430, 0xB530,
   1.353 +	0xB610, 0xB710, 0xB810, 0xB910, 0xBA10, 0xBB10, 0xBC10, 0xBD10,
   1.354 +	0xBE10, 0xBF10, 0xC030, 0xC130, 0xC230, 0xC330, 0xC430, 0xC530,
   1.355 +	0xC610, 0xC710, 0xC810, 0xC910, 0xCA10, 0xCB10, 0xCC10, 0xCD10,
   1.356 +	0xCE10, 0xCF10, 0xD030, 0xD130, 0xD230, 0xD330, 0xD430, 0xD530,
   1.357 +	0xD610, 0xD710, 0xD810, 0xD910, 0xDA10, 0xDB10, 0xDC10, 0xDD10,
   1.358 +	0xDE10, 0xDF10, 0xE030, 0xE130, 0xE230, 0xE330, 0xE430, 0xE530,
   1.359 +	0xE610, 0xE710, 0xE810, 0xE910, 0xEA10, 0xEB10, 0xEC10, 0xED10,
   1.360 +	0xEE10, 0xEF10, 0xF030, 0xF130, 0xF230, 0xF330, 0xF430, 0xF530,
   1.361 +	0xF610, 0xF710, 0xF810, 0xF910, 0xFA10, 0xFB10, 0xFC10, 0xFD10,
   1.362 +	0xFE10, 0xFF10, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530,
   1.363 +	0x0610, 0x0710, 0x0810, 0x0910, 0x0A10, 0x0B10, 0x0C10, 0x0D10,
   1.364 +	0x0E10, 0x0F10, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530,
   1.365 +	0x1610, 0x1710, 0x1810, 0x1910, 0x1A10, 0x1B10, 0x1C10, 0x1D10,
   1.366 +	0x1E10, 0x1F10, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530,
   1.367 +	0x2610, 0x2710, 0x2810, 0x2910, 0x2A10, 0x2B10, 0x2C10, 0x2D10,
   1.368 +	0x2E10, 0x2F10, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530,
   1.369 +	0x3610, 0x3710, 0x3810, 0x3910, 0x3A10, 0x3B10, 0x3C10, 0x3D10,
   1.370 +	0x3E10, 0x3F10, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530,
   1.371 +	0x4610, 0x4710, 0x4810, 0x4910, 0x4A10, 0x4B10, 0x4C10, 0x4D10,
   1.372 +	0x4E10, 0x4F10, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530,
   1.373 +	0x5610, 0x5710, 0x5810, 0x5910, 0x5A10, 0x5B10, 0x5C10, 0x5D10,
   1.374 +	0x5E10, 0x5F10, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530,
   1.375 +	0x00C0, 0x0140, 0x0240, 0x0340, 0x0440, 0x0540, 0x0640, 0x0740,
   1.376 +	0x0840, 0x0940, 0x0440, 0x0540, 0x0640, 0x0740, 0x0840, 0x0940,
   1.377 +	0x1040, 0x1140, 0x1240, 0x1340, 0x1440, 0x1540, 0x1640, 0x1740,
   1.378 +	0x1840, 0x1940, 0x1440, 0x1540, 0x1640, 0x1740, 0x1840, 0x1940,
   1.379 +	0x2040, 0x2140, 0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740,
   1.380 +	0x2840, 0x2940, 0x2440, 0x2540, 0x2640, 0x2740, 0x2840, 0x2940,
   1.381 +	0x3040, 0x3140, 0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740,
   1.382 +	0x3840, 0x3940, 0x3440, 0x3540, 0x3640, 0x3740, 0x3840, 0x3940,
   1.383 +	0x4040, 0x4140, 0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740,
   1.384 +	0x4840, 0x4940, 0x4440, 0x4540, 0x4640, 0x4740, 0x4840, 0x4940,
   1.385 +	0x5040, 0x5140, 0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740,
   1.386 +	0x5840, 0x5940, 0x5440, 0x5540, 0x5640, 0x5740, 0x5840, 0x5940,
   1.387 +	0x6040, 0x6140, 0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740,
   1.388 +	0x6840, 0x6940, 0x6440, 0x6540, 0x6640, 0x6740, 0x6840, 0x6940,
   1.389 +	0x7040, 0x7140, 0x7240, 0x7340, 0x7440, 0x7540, 0x7640, 0x7740,
   1.390 +	0x7840, 0x7940, 0x7440, 0x7540, 0x7640, 0x7740, 0x7840, 0x7940,
   1.391 +	0x8040, 0x8140, 0x8240, 0x8340, 0x8440, 0x8540, 0x8640, 0x8740,
   1.392 +	0x8840, 0x8940, 0x8440, 0x8540, 0x8640, 0x8740, 0x8840, 0x8940,
   1.393 +	0x9040, 0x9140, 0x9240, 0x9340, 0x9440, 0x9540, 0x9640, 0x9740,
   1.394 +	0x9840, 0x9940, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
   1.395 +	0x4050, 0x4150, 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750,
   1.396 +	0x4850, 0x4950, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
   1.397 +	0x5050, 0x5150, 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750,
   1.398 +	0x5850, 0x5950, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
   1.399 +	0x6050, 0x6150, 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750,
   1.400 +	0x6850, 0x6950, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
   1.401 +	0x7050, 0x7150, 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750,
   1.402 +	0x7850, 0x7950, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
   1.403 +	0x8050, 0x8150, 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750,
   1.404 +	0x8850, 0x8950, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
   1.405 +	0x9050, 0x9150, 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750,
   1.406 +	0x9850, 0x9950, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
   1.407 +	0xA050, 0xA150, 0xA250, 0xA350, 0xA450, 0xA550, 0xA650, 0xA750,
   1.408 +	0xA850, 0xA950, 0xA450, 0xA550, 0xA650, 0xA750, 0xA850, 0xA950,
   1.409 +	0xB050, 0xB150, 0xB250, 0xB350, 0xB450, 0xB550, 0xB650, 0xB750,
   1.410 +	0xB850, 0xB950, 0xB450, 0xB550, 0xB650, 0xB750, 0xB850, 0xB950,
   1.411 +	0xC050, 0xC150, 0xC250, 0xC350, 0xC450, 0xC550, 0xC650, 0xC750,
   1.412 +	0xC850, 0xC950, 0xC450, 0xC550, 0xC650, 0xC750, 0xC850, 0xC950,
   1.413 +	0xD050, 0xD150, 0xD250, 0xD350, 0xD450, 0xD550, 0xD650, 0xD750,
   1.414 +	0xD850, 0xD950, 0xD450, 0xD550, 0xD650, 0xD750, 0xD850, 0xD950,
   1.415 +	0xE050, 0xE150, 0xE250, 0xE350, 0xE450, 0xE550, 0xE650, 0xE750,
   1.416 +	0xE850, 0xE950, 0xE450, 0xE550, 0xE650, 0xE750, 0xE850, 0xE950,
   1.417 +	0xF050, 0xF150, 0xF250, 0xF350, 0xF450, 0xF550, 0xF650, 0xF750,
   1.418 +	0xF850, 0xF950, 0xF450, 0xF550, 0xF650, 0xF750, 0xF850, 0xF950,
   1.419 +	0x00D0, 0x0150, 0x0250, 0x0350, 0x0450, 0x0550, 0x0650, 0x0750,
   1.420 +	0x0850, 0x0950, 0x0450, 0x0550, 0x0650, 0x0750, 0x0850, 0x0950,
   1.421 +	0x1050, 0x1150, 0x1250, 0x1350, 0x1450, 0x1550, 0x1650, 0x1750,
   1.422 +	0x1850, 0x1950, 0x1450, 0x1550, 0x1650, 0x1750, 0x1850, 0x1950,
   1.423 +	0x2050, 0x2150, 0x2250, 0x2350, 0x2450, 0x2550, 0x2650, 0x2750,
   1.424 +	0x2850, 0x2950, 0x2450, 0x2550, 0x2650, 0x2750, 0x2850, 0x2950,
   1.425 +	0x3050, 0x3150, 0x3250, 0x3350, 0x3450, 0x3550, 0x3650, 0x3750,
   1.426 +	0x3850, 0x3950, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
   1.427 +	0x4050, 0x4150, 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750,
   1.428 +	0x4850, 0x4950, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
   1.429 +	0x5050, 0x5150, 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750,
   1.430 +	0x5850, 0x5950, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
   1.431 +	0x6050, 0x6150, 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750,
   1.432 +	0x6850, 0x6950, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
   1.433 +	0x7050, 0x7150, 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750,
   1.434 +	0x7850, 0x7950, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
   1.435 +	0x8050, 0x8150, 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750,
   1.436 +	0x8850, 0x8950, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
   1.437 +	0x9050, 0x9150, 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750,
   1.438 +	0x9850, 0x9950, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
   1.439 +	0xFA60, 0xFB60, 0xFC60, 0xFD60, 0xFE60, 0xFF60, 0x00C0, 0x0140,
   1.440 +	0x0240, 0x0340, 0x0440, 0x0540, 0x0640, 0x0740, 0x0840, 0x0940,
   1.441 +	0x0A60, 0x0B60, 0x0C60, 0x0D60, 0x0E60, 0x0F60, 0x1040, 0x1140,
   1.442 +	0x1240, 0x1340, 0x1440, 0x1540, 0x1640, 0x1740, 0x1840, 0x1940,
   1.443 +	0x1A60, 0x1B60, 0x1C60, 0x1D60, 0x1E60, 0x1F60, 0x2040, 0x2140,
   1.444 +	0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740, 0x2840, 0x2940,
   1.445 +	0x2A60, 0x2B60, 0x2C60, 0x2D60, 0x2E60, 0x2F60, 0x3040, 0x3140,
   1.446 +	0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740, 0x3840, 0x3940,
   1.447 +	0x3A60, 0x3B60, 0x3C60, 0x3D60, 0x3E60, 0x3F60, 0x4040, 0x4140,
   1.448 +	0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740, 0x4840, 0x4940,
   1.449 +	0x4A60, 0x4B60, 0x4C60, 0x4D60, 0x4E60, 0x4F60, 0x5040, 0x5140,
   1.450 +	0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740, 0x5840, 0x5940,
   1.451 +	0x5A60, 0x5B60, 0x5C60, 0x5D60, 0x5E60, 0x5F60, 0x6040, 0x6140,
   1.452 +	0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740, 0x6840, 0x6940,
   1.453 +	0x6A60, 0x6B60, 0x6C60, 0x6D60, 0x6E60, 0x6F60, 0x7040, 0x7140,
   1.454 +	0x7240, 0x7340, 0x7440, 0x7540, 0x7640, 0x7740, 0x7840, 0x7940,
   1.455 +	0x7A60, 0x7B60, 0x7C60, 0x7D60, 0x7E60, 0x7F60, 0x8040, 0x8140,
   1.456 +	0x8240, 0x8340, 0x8440, 0x8540, 0x8640, 0x8740, 0x8840, 0x8940,
   1.457 +	0x8A60, 0x8B60, 0x8C60, 0x8D60, 0x8E60, 0x8F60, 0x9040, 0x9140,
   1.458 +	0x9240, 0x9340, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
   1.459 +	0x3A70, 0x3B70, 0x3C70, 0x3D70, 0x3E70, 0x3F70, 0x4050, 0x4150,
   1.460 +	0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
   1.461 +	0x4A70, 0x4B70, 0x4C70, 0x4D70, 0x4E70, 0x4F70, 0x5050, 0x5150,
   1.462 +	0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
   1.463 +	0x5A70, 0x5B70, 0x5C70, 0x5D70, 0x5E70, 0x5F70, 0x6050, 0x6150,
   1.464 +	0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
   1.465 +	0x6A70, 0x6B70, 0x6C70, 0x6D70, 0x6E70, 0x6F70, 0x7050, 0x7150,
   1.466 +	0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
   1.467 +	0x7A70, 0x7B70, 0x7C70, 0x7D70, 0x7E70, 0x7F70, 0x8050, 0x8150,
   1.468 +	0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
   1.469 +	0x8A70, 0x8B70, 0x8C70, 0x8D70, 0x8E70, 0x8F70, 0x9050, 0x9150,
   1.470 +	0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
   1.471 +	0x9A70, 0x9B70, 0x9C70, 0x9D70, 0x9E70, 0x9F70, 0xA050, 0xA150,
   1.472 +	0xA250, 0xA350, 0xA450, 0xA550, 0xA650, 0xA750, 0xA850, 0xA950,
   1.473 +	0xAA70, 0xAB70, 0xAC70, 0xAD70, 0xAE70, 0xAF70, 0xB050, 0xB150,
   1.474 +	0xB250, 0xB350, 0xB450, 0xB550, 0xB650, 0xB750, 0xB850, 0xB950,
   1.475 +	0xBA70, 0xBB70, 0xBC70, 0xBD70, 0xBE70, 0xBF70, 0xC050, 0xC150,
   1.476 +	0xC250, 0xC350, 0xC450, 0xC550, 0xC650, 0xC750, 0xC850, 0xC950,
   1.477 +	0xCA70, 0xCB70, 0xCC70, 0xCD70, 0xCE70, 0xCF70, 0xD050, 0xD150,
   1.478 +	0xD250, 0xD350, 0xD450, 0xD550, 0xD650, 0xD750, 0xD850, 0xD950,
   1.479 +	0xDA70, 0xDB70, 0xDC70, 0xDD70, 0xDE70, 0xDF70, 0xE050, 0xE150,
   1.480 +	0xE250, 0xE350, 0xE450, 0xE550, 0xE650, 0xE750, 0xE850, 0xE950,
   1.481 +	0xEA70, 0xEB70, 0xEC70, 0xED70, 0xEE70, 0xEF70, 0xF050, 0xF150,
   1.482 +	0xF250, 0xF350, 0xF450, 0xF550, 0xF650, 0xF750, 0xF850, 0xF950,
   1.483 +	0xFA70, 0xFB70, 0xFC70, 0xFD70, 0xFE70, 0xFF70, 0x00D0, 0x0150,
   1.484 +	0x0250, 0x0350, 0x0450, 0x0550, 0x0650, 0x0750, 0x0850, 0x0950,
   1.485 +	0x0A70, 0x0B70, 0x0C70, 0x0D70, 0x0E70, 0x0F70, 0x1050, 0x1150,
   1.486 +	0x1250, 0x1350, 0x1450, 0x1550, 0x1650, 0x1750, 0x1850, 0x1950,
   1.487 +	0x1A70, 0x1B70, 0x1C70, 0x1D70, 0x1E70, 0x1F70, 0x2050, 0x2150,
   1.488 +	0x2250, 0x2350, 0x2450, 0x2550, 0x2650, 0x2750, 0x2850, 0x2950,
   1.489 +	0x2A70, 0x2B70, 0x2C70, 0x2D70, 0x2E70, 0x2F70, 0x3050, 0x3150,
   1.490 +	0x3250, 0x3350, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950,
   1.491 +	0x3A70, 0x3B70, 0x3C70, 0x3D70, 0x3E70, 0x3F70, 0x4050, 0x4150,
   1.492 +	0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950,
   1.493 +	0x4A70, 0x4B70, 0x4C70, 0x4D70, 0x4E70, 0x4F70, 0x5050, 0x5150,
   1.494 +	0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950,
   1.495 +	0x5A70, 0x5B70, 0x5C70, 0x5D70, 0x5E70, 0x5F70, 0x6050, 0x6150,
   1.496 +	0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950,
   1.497 +	0x6A70, 0x6B70, 0x6C70, 0x6D70, 0x6E70, 0x6F70, 0x7050, 0x7150,
   1.498 +	0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950,
   1.499 +	0x7A70, 0x7B70, 0x7C70, 0x7D70, 0x7E70, 0x7F70, 0x8050, 0x8150,
   1.500 +	0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950,
   1.501 +	0x8A70, 0x8B70, 0x8C70, 0x8D70, 0x8E70, 0x8F70, 0x9050, 0x9150,
   1.502 +	0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950,
   1.503 +};
   1.504 +
   1.505 +u8 ZeroTable[] =
   1.506 +{
   1.507 +	0x80, 0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.508 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.509 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.510 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.511 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.512 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.513 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.514 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.515 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.516 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.517 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.518 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.519 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.520 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.521 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,
   1.522 +	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0,	0,	  0
   1.523 +};
   1.524 +
   1.525 +#define GBSAVE_GAME_VERSION_1 1
   1.526 +#define GBSAVE_GAME_VERSION_2 2
   1.527 +#define GBSAVE_GAME_VERSION_3 3
   1.528 +#define GBSAVE_GAME_VERSION_4 4
   1.529 +#define GBSAVE_GAME_VERSION_5 5
   1.530 +#define GBSAVE_GAME_VERSION_6 6
   1.531 +#define GBSAVE_GAME_VERSION_7 7
   1.532 +#define GBSAVE_GAME_VERSION_8 8
   1.533 +#define GBSAVE_GAME_VERSION_9 9
   1.534 +#define GBSAVE_GAME_VERSION_10 10
   1.535 +#define GBSAVE_GAME_VERSION_11 11
   1.536 +#define GBSAVE_GAME_VERSION_12 12
   1.537 +#define GBSAVE_GAME_VERSION_13 13
   1.538 +#define GBSAVE_GAME_VERSION GBSAVE_GAME_VERSION_13
   1.539 +
   1.540 +int inline gbGetValue(int min, int max, int v)
   1.541 +{
   1.542 +	return (int)(min + (float)(max - min) * (2.0 * (v / 31.0) - (v / 31.0) * (v / 31.0)));
   1.543 +}
   1.544 +
   1.545 +void gbGenFilter()
   1.546 +{
   1.547 +	for (int r = 0; r < 32; r++)
   1.548 +	{
   1.549 +		for (int g = 0; g < 32; g++)
   1.550 +		{
   1.551 +			for (int b = 0; b < 32; b++)
   1.552 +			{
   1.553 +				int nr = gbGetValue(gbGetValue(4, 14, g),
   1.554 +				                    gbGetValue(24, 29, g), r) - 4;
   1.555 +				int ng = gbGetValue(gbGetValue(4 + gbGetValue(0, 5, r),
   1.556 +				                               14 + gbGetValue(0, 3, r), b),
   1.557 +				                    gbGetValue(24 + gbGetValue(0, 3, r),
   1.558 +				                               29 + gbGetValue(0, 1, r), b), g) - 4;
   1.559 +				int nb = gbGetValue(gbGetValue(4 + gbGetValue(0, 5, r),
   1.560 +				                               14 + gbGetValue(0, 3, r), g),
   1.561 +				                    gbGetValue(24 + gbGetValue(0, 3, r),
   1.562 +				                               29 + gbGetValue(0, 1, r), g), b) - 4;
   1.563 +				gbColorFilter[(b << 10) | (g << 5) | r] = (nb << 10) | (ng << 5) | nr;
   1.564 +			}
   1.565 +		}
   1.566 +	}
   1.567 +}
   1.568 +
   1.569 +void gbCopyMemory(u16 d, u16 s, int count)
   1.570 +{
   1.571 +	while (count)
   1.572 +	{
   1.573 +		gbWriteMemoryQuick(d, gbReadMemoryQuick(s));
   1.574 +		s++;
   1.575 +		d++;
   1.576 +		count--;
   1.577 +	}
   1.578 +}
   1.579 +
   1.580 +void gbDoHdma()
   1.581 +{
   1.582 +	gbCopyMemory(gbHdmaDestination, gbHdmaSource, 0x10);
   1.583 +
   1.584 +	gbHdmaDestination += 0x10;
   1.585 +	gbHdmaSource	  += 0x10;
   1.586 +
   1.587 +	register_HDMA2 = (register_HDMA2 + 0x10) & 0xFF;
   1.588 +	if (register_HDMA2 == 0x00)
   1.589 +		register_HDMA1++;
   1.590 +
   1.591 +	register_HDMA4 = (register_HDMA4 + 0x10) & 0xFF;
   1.592 +	if (register_HDMA4 == 0x00)
   1.593 +		register_HDMA3++;
   1.594 +
   1.595 +	if (gbHdmaDestination == 0x96b0)
   1.596 +		gbHdmaBytes = gbHdmaBytes;
   1.597 +	gbHdmaBytes -= 0x10;
   1.598 +	register_HDMA5--;
   1.599 +	if (register_HDMA5 == 0xff)
   1.600 +		gbHdmaOn = 0;
   1.601 +}
   1.602 +
   1.603 +// fix for Harley and Lego Racers
   1.604 +void gbCompareLYToLYC()
   1.605 +{
   1.606 +	if (register_LY == register_LYC)
   1.607 +	{
   1.608 +		// mark that we have a match
   1.609 +		register_STAT |= 4;
   1.610 +
   1.611 +		// check if we need an interrupt
   1.612 +		if ((register_STAT & 0x40) && (register_IE & 2))
   1.613 +			gbInterrupt |= 2;
   1.614 +	}
   1.615 +	else   // no match
   1.616 +		register_STAT &= 0xfb;
   1.617 +}
   1.618 +
   1.619 +// FIXME: horrible kludge to workaround the frame timing bug
   1.620 +static int32 s_gbJoymask[4] = { 0, 0, 0, 0 };
   1.621 +
   1.622 +void gbWriteMemoryWrapped(register u16 address, register u8 value)
   1.623 +{
   1.624 +	if (address < 0x8000)
   1.625 +	{
   1.626 +#ifndef FINAL_VERSION
   1.627 +		if (memorydebug && (address > 0x3fff || address < 0x2000))
   1.628 +		{
   1.629 +			log("Memory register write %04x=%02x PC=%04x\n",
   1.630 +			    address,
   1.631 +			    value,
   1.632 +			    PC.W);
   1.633 +		}
   1.634 +#endif
   1.635 +		if (mapper)
   1.636 +			(*mapper)(address, value);
   1.637 +		return;
   1.638 +	}
   1.639 +
   1.640 +	if (address < 0xa000)
   1.641 +	{
   1.642 +		gbWriteMemoryQuick(address, value);
   1.643 +		return;
   1.644 +	}
   1.645 +
   1.646 +	if (address < 0xc000)
   1.647 +	{
   1.648 +#ifndef FINAL_VERSION
   1.649 +		if (memorydebug)
   1.650 +		{
   1.651 +			log("Memory register write %04x=%02x PC=%04x\n",
   1.652 +			    address,
   1.653 +			    value,
   1.654 +			    PC.W);
   1.655 +		}
   1.656 +#endif
   1.657 +
   1.658 +		if (mapper)
   1.659 +			(*mapperRAM)(address, value);
   1.660 +		return;
   1.661 +	}
   1.662 +
   1.663 +	if (address < 0xfe00)
   1.664 +	{
   1.665 +		gbWriteMemoryQuick(address, value);
   1.666 +		return;
   1.667 +	}
   1.668 +
   1.669 +	if (address < 0xff00)
   1.670 +	{
   1.671 +		gbMemory[address] = value;
   1.672 +		return;
   1.673 +	}
   1.674 +
   1.675 +	switch (address & 0x00ff)
   1.676 +	{
   1.677 +	case 0x00:
   1.678 +	{
   1.679 +		gbMemory[0xff00] = ((gbMemory[0xff00] & 0xcf) |
   1.680 +		                    (value & 0x30));
   1.681 +		if (gbSgbMode)
   1.682 +		{
   1.683 +			gbSgbDoBitTransfer(value);
   1.684 +		}
   1.685 +
   1.686 +		return;
   1.687 +	}
   1.688 +
   1.689 +	case 0x01:
   1.690 +	{
   1.691 +		gbMemory[0xff01] = value;
   1.692 +		return;
   1.693 +	}
   1.694 +
   1.695 +	// serial control
   1.696 +	case 0x02:
   1.697 +	{
   1.698 +		gbSerialOn		 = (value & 0x80);
   1.699 +		gbMemory[0xff02] = value;
   1.700 +		if (gbSerialOn)
   1.701 +		{
   1.702 +			gbSerialTicks = GBSERIAL_CLOCK_TICKS;
   1.703 +#ifdef LINK_EMULATION
   1.704 +			if (linkConnected)
   1.705 +			{
   1.706 +				if (value & 1)
   1.707 +				{
   1.708 +					linkSendByte(0x100 | gbMemory[0xFF01]);
   1.709 +					Sleep(5);
   1.710 +				}
   1.711 +			}
   1.712 +#endif
   1.713 +		}
   1.714 +
   1.715 +		gbSerialBits = 0;
   1.716 +		return;
   1.717 +	}
   1.718 +
   1.719 +	// DIV register resets on any write
   1.720 +	case 0x04:
   1.721 +	{
   1.722 +		register_DIV = 0;
   1.723 +		return;
   1.724 +	}
   1.725 +	case 0x05:
   1.726 +		register_TIMA = value;
   1.727 +		return;
   1.728 +
   1.729 +	case 0x06:
   1.730 +		register_TMA = value;
   1.731 +		return;
   1.732 +
   1.733 +	// TIMER control
   1.734 +	case 0x07:
   1.735 +	{
   1.736 +		register_TAC = value;
   1.737 +
   1.738 +		gbTimerOn	= (value & 4);
   1.739 +		gbTimerMode = value & 3;
   1.740 +		//    register_TIMA = register_TMA;
   1.741 +		switch (gbTimerMode)
   1.742 +		{
   1.743 +		case 0:
   1.744 +			gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_0_CLOCK_TICKS;
   1.745 +			break;
   1.746 +		case 1:
   1.747 +			gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_1_CLOCK_TICKS;
   1.748 +			break;
   1.749 +		case 2:
   1.750 +			gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_2_CLOCK_TICKS;
   1.751 +			break;
   1.752 +		case 3:
   1.753 +			gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_3_CLOCK_TICKS;
   1.754 +			break;
   1.755 +		}
   1.756 +		return;
   1.757 +	}
   1.758 +
   1.759 +	case 0x0f:
   1.760 +	{
   1.761 +		register_IF = value;
   1.762 +		gbInterrupt = value;
   1.763 +		return;
   1.764 +	}
   1.765 +
   1.766 +	case 0x10:
   1.767 +	case 0x11:
   1.768 +	case 0x12:
   1.769 +	case 0x13:
   1.770 +	case 0x14:
   1.771 +	case 0x15:
   1.772 +	case 0x16:
   1.773 +	case 0x17:
   1.774 +	case 0x18:
   1.775 +	case 0x19:
   1.776 +	case 0x1a:
   1.777 +	case 0x1b:
   1.778 +	case 0x1c:
   1.779 +	case 0x1d:
   1.780 +	case 0x1e:
   1.781 +	case 0x1f:
   1.782 +	case 0x20:
   1.783 +	case 0x21:
   1.784 +	case 0x22:
   1.785 +	case 0x23:
   1.786 +	case 0x24:
   1.787 +	case 0x25:
   1.788 +	case 0x26:
   1.789 +	{
   1.790 +		SOUND_EVENT(address, value);
   1.791 +		return;
   1.792 +	}
   1.793 +	case 0x40:
   1.794 +	{
   1.795 +		int lcdChange = (register_LCDC & 0x80) ^ (value & 0x80);
   1.796 +
   1.797 +		if (lcdChange)
   1.798 +		{
   1.799 +			if (value & 0x80)
   1.800 +			{
   1.801 +				gbLcdTicks	   = GBLCD_MODE_1_CLOCK_TICKS;
   1.802 +				gbLcdMode	   = 0;
   1.803 +				register_STAT &= 0xfc;
   1.804 +				register_LY	   = 0x00;
   1.805 +				// FIXME: horrible workaround
   1.806 +				if (gbNullInputHackTempEnabled && !useOldFrameTiming)
   1.807 +					memcpy(gbJoymask, s_gbJoymask, sizeof(gbJoymask));
   1.808 +			}
   1.809 +			else
   1.810 +			{
   1.811 +				gbLcdTicks	   = 0;
   1.812 +				gbLcdMode	   = 0;
   1.813 +				register_STAT &= 0xfc;
   1.814 +				register_LY	   = 0x00;
   1.815 +				// FIXME: horrible workaround
   1.816 +				memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask));
   1.817 +				if (gbNullInputHackTempEnabled && !useOldFrameTiming)
   1.818 +					memset(gbJoymask, 0, sizeof(gbJoymask));
   1.819 +			}
   1.820 +			//      compareLYToLYC();
   1.821 +		}
   1.822 +		// don't draw the window if it was not enabled and not being drawn before
   1.823 +		if (!(register_LCDC & 0x20) && (value & 0x20) && gbWindowLine == -1 &&
   1.824 +		    register_LY > register_WY)
   1.825 +			gbWindowLine = 144;
   1.826 +
   1.827 +		register_LCDC = value;
   1.828 +
   1.829 +		return;
   1.830 +	}
   1.831 +
   1.832 +	// STAT
   1.833 +	case 0x41:
   1.834 +	{
   1.835 +		//register_STAT = (register_STAT & 0x87) |
   1.836 +		//      (value & 0x7c);
   1.837 +		register_STAT = (value & 0xf8) | (register_STAT & 0x07);     // fix ?
   1.838 +		// GB bug from Devrs FAQ
   1.839 +		if (!gbCgbMode && (register_LCDC & 0x80) && gbLcdMode < 2)
   1.840 +			gbInterrupt |= 2;
   1.841 +		return;
   1.842 +	}
   1.843 +
   1.844 +	// SCY
   1.845 +	case 0x42:
   1.846 +	{
   1.847 +		register_SCY = value;
   1.848 +		return;
   1.849 +	}
   1.850 +
   1.851 +	// SCX
   1.852 +	case 0x43:
   1.853 +	{
   1.854 +		register_SCX = value;
   1.855 +		return;
   1.856 +	}
   1.857 +
   1.858 +	// LY
   1.859 +	case 0x44:
   1.860 +	{
   1.861 +		// read only
   1.862 +		return;
   1.863 +	}
   1.864 +
   1.865 +	// LYC
   1.866 +	case 0x45:
   1.867 +	{
   1.868 +		register_LYC = value;
   1.869 +		if ((register_LCDC & 0x80))
   1.870 +		{
   1.871 +			gbCompareLYToLYC();
   1.872 +		}
   1.873 +		return;
   1.874 +	}
   1.875 +
   1.876 +	// DMA!
   1.877 +	case 0x46:
   1.878 +	{
   1.879 +		int source = value * 0x0100;
   1.880 +
   1.881 +		gbCopyMemory(0xfe00,
   1.882 +		             source,
   1.883 +		             0xa0);
   1.884 +		register_DMA = value;
   1.885 +		return;
   1.886 +	}
   1.887 +
   1.888 +	// BGP
   1.889 +	case 0x47:
   1.890 +	{
   1.891 +		gbBgp[0] = value & 0x03;
   1.892 +		gbBgp[1] = (value & 0x0c) >> 2;
   1.893 +		gbBgp[2] = (value & 0x30) >> 4;
   1.894 +		gbBgp[3] = (value & 0xc0) >> 6;
   1.895 +		break;
   1.896 +	}
   1.897 +
   1.898 +	// OBP0
   1.899 +	case 0x48:
   1.900 +	{
   1.901 +		gbObp0[0] = value & 0x03;
   1.902 +		gbObp0[1] = (value & 0x0c) >> 2;
   1.903 +		gbObp0[2] = (value & 0x30) >> 4;
   1.904 +		gbObp0[3] = (value & 0xc0) >> 6;
   1.905 +		break;
   1.906 +	}
   1.907 +
   1.908 +	// OBP1
   1.909 +	case 0x49:
   1.910 +	{
   1.911 +		gbObp1[0] = value & 0x03;
   1.912 +		gbObp1[1] = (value & 0x0c) >> 2;
   1.913 +		gbObp1[2] = (value & 0x30) >> 4;
   1.914 +		gbObp1[3] = (value & 0xc0) >> 6;
   1.915 +		break;
   1.916 +	}
   1.917 +
   1.918 +	case 0x4a:
   1.919 +		register_WY = value;
   1.920 +		return;
   1.921 +
   1.922 +	case 0x4b:
   1.923 +		register_WX = value;
   1.924 +		return;
   1.925 +
   1.926 +	// KEY1
   1.927 +	case 0x4d:
   1.928 +	{
   1.929 +		if (gbCgbMode)
   1.930 +		{
   1.931 +			gbMemory[0xff4d] = (gbMemory[0xff4d] & 0x80) | (value & 1);
   1.932 +			return;
   1.933 +		}
   1.934 +		break;
   1.935 +	}
   1.936 +
   1.937 +	// VBK
   1.938 +	case 0x4f:
   1.939 +	{
   1.940 +		if (gbCgbMode)
   1.941 +		{
   1.942 +			value = value & 1;
   1.943 +			if (value == gbVramBank)
   1.944 +				return;
   1.945 +
   1.946 +			int vramAddress = value * 0x2000;
   1.947 +			gbMemoryMap[0x08] = &gbVram[vramAddress];
   1.948 +			gbMemoryMap[0x09] = &gbVram[vramAddress + 0x1000];
   1.949 +
   1.950 +			gbVramBank	 = value;
   1.951 +			register_VBK = value;
   1.952 +		}
   1.953 +		return;
   1.954 +		break;
   1.955 +	}
   1.956 +
   1.957 +	// HDMA1
   1.958 +	case 0x51:
   1.959 +	{
   1.960 +		if (gbCgbMode)
   1.961 +		{
   1.962 +			if (value > 0x7f && value < 0xa0)
   1.963 +				value = 0;
   1.964 +
   1.965 +			gbHdmaSource = (value << 8) | (register_HDMA2 & 0xf0);
   1.966 +
   1.967 +			register_HDMA1 = value;
   1.968 +			return;
   1.969 +		}
   1.970 +		break;
   1.971 +	}
   1.972 +
   1.973 +	// HDMA2
   1.974 +	case 0x52:
   1.975 +	{
   1.976 +		if (gbCgbMode)
   1.977 +		{
   1.978 +			value = value & 0xf0;
   1.979 +
   1.980 +			gbHdmaSource = (register_HDMA1 << 8) | (value);
   1.981 +
   1.982 +			register_HDMA2 = value;
   1.983 +			return;
   1.984 +		}
   1.985 +		break;
   1.986 +	}
   1.987 +
   1.988 +	// HDMA3
   1.989 +	case 0x53:
   1.990 +	{
   1.991 +		if (gbCgbMode)
   1.992 +		{
   1.993 +			value = value & 0x1f;
   1.994 +			gbHdmaDestination  = (value << 8) | (register_HDMA4 & 0xf0);
   1.995 +			gbHdmaDestination += 0x8000;
   1.996 +			register_HDMA3	   = value;
   1.997 +			return;
   1.998 +		}
   1.999 +		break;
  1.1000 +	}
  1.1001 +
  1.1002 +	// HDMA4
  1.1003 +	case 0x54:
  1.1004 +	{
  1.1005 +		if (gbCgbMode)
  1.1006 +		{
  1.1007 +			value = value & 0xf0;
  1.1008 +			gbHdmaDestination  = ((register_HDMA3 & 0x1f) << 8) | value;
  1.1009 +			gbHdmaDestination += 0x8000;
  1.1010 +			register_HDMA4	   = value;
  1.1011 +			return;
  1.1012 +		}
  1.1013 +		break;
  1.1014 +	}
  1.1015 +
  1.1016 +	// HDMA5
  1.1017 +	case 0x55:
  1.1018 +	{
  1.1019 +		if (gbCgbMode)
  1.1020 +		{
  1.1021 +			gbHdmaBytes = 16 + (value & 0x7f) * 16;
  1.1022 +			if (gbHdmaOn)
  1.1023 +			{
  1.1024 +				if (value & 0x80)
  1.1025 +				{
  1.1026 +					register_HDMA5 = (value & 0x7f);
  1.1027 +				}
  1.1028 +				else
  1.1029 +				{
  1.1030 +					register_HDMA5 = 0xff;
  1.1031 +					gbHdmaOn	   = 0;
  1.1032 +				}
  1.1033 +			}
  1.1034 +			else
  1.1035 +			{
  1.1036 +				if (value & 0x80)
  1.1037 +				{
  1.1038 +					gbHdmaOn	   = 1;
  1.1039 +					register_HDMA5 = value & 0x7f;
  1.1040 +					if (gbLcdMode == 0)
  1.1041 +						gbDoHdma();
  1.1042 +				}
  1.1043 +				else
  1.1044 +				{
  1.1045 +					// we need to take the time it takes to complete the transfer into
  1.1046 +					// account... according to GB DEV FAQs, the setup time is the same
  1.1047 +					// for single and double speed, but the actual transfer takes the
  1.1048 +					// same time // (is that a typo?)
  1.1049 +					switch (gbDMASpeedVersion)
  1.1050 +					{
  1.1051 +					case 1:     // I believe this is more correct
  1.1052 +						// the lower 7 bits of FF55 specify the Transfer Length (divided by 16, minus 1)
  1.1053 +						// and we make gbDmaTicks twice as many cycles at double speed to make the transfer take the same time
  1.1054 +						if (gbSpeed)
  1.1055 +							gbDmaTicks = 16 * ((value & 0x7f) + 1);
  1.1056 +						else
  1.1057 +							gbDmaTicks = 8 * ((value & 0x7f) + 1);
  1.1058 +						break;
  1.1059 +					case 0:     // here for backward compatibility
  1.1060 +						// I think this was a guess that approximates the above in most but not all games
  1.1061 +						if (gbSpeed)
  1.1062 +							gbDmaTicks = 231 + 16 * (value & 0x7f);
  1.1063 +						else
  1.1064 +							gbDmaTicks = 231 + 8 * (value & 0x7f);
  1.1065 +						break;
  1.1066 +					default:     // shouldn't happen
  1.1067 +						//assert(0);
  1.1068 +						break;
  1.1069 +					}
  1.1070 +					gbCopyMemory(gbHdmaDestination, gbHdmaSource, gbHdmaBytes);
  1.1071 +					gbHdmaDestination += gbHdmaBytes;
  1.1072 +					gbHdmaSource	  += gbHdmaBytes;
  1.1073 +
  1.1074 +					register_HDMA3 = ((gbHdmaDestination - 0x8000) >> 8) & 0x1f;
  1.1075 +					register_HDMA4 = gbHdmaDestination & 0xf0;
  1.1076 +					register_HDMA1 = (gbHdmaSource >> 8) & 0xff;
  1.1077 +					register_HDMA2 = gbHdmaSource & 0xf0;
  1.1078 +				}
  1.1079 +			}
  1.1080 +			return;
  1.1081 +		}
  1.1082 +		break;
  1.1083 +	}
  1.1084 +
  1.1085 +	// BCPS
  1.1086 +	case 0x68:
  1.1087 +	{
  1.1088 +		if (gbCgbMode)
  1.1089 +		{
  1.1090 +			int paletteIndex = (value & 0x3f) >> 1;
  1.1091 +			int paletteHiLo	 = (value & 0x01);
  1.1092 +
  1.1093 +			gbMemory[0xff68] = value;
  1.1094 +			gbMemory[0xff69] = (paletteHiLo ?
  1.1095 +			                    (gbPalette[paletteIndex] >> 8) :
  1.1096 +			                    (gbPalette[paletteIndex] & 0x00ff));
  1.1097 +			return;
  1.1098 +		}
  1.1099 +		break;
  1.1100 +	}
  1.1101 +
  1.1102 +	// BCPD
  1.1103 +	case 0x69:
  1.1104 +	{
  1.1105 +		if (gbCgbMode)
  1.1106 +		{
  1.1107 +			int v = gbMemory[0xff68];
  1.1108 +			int paletteIndex = (v & 0x3f) >> 1;
  1.1109 +			int paletteHiLo	 = (v & 0x01);
  1.1110 +			gbMemory[0xff69]		= value;
  1.1111 +			gbPalette[paletteIndex] = (paletteHiLo ?
  1.1112 +			                           ((value << 8) | (gbPalette[paletteIndex] & 0xff)) :
  1.1113 +			                           ((gbPalette[paletteIndex] & 0xff00) | (value))) & 0x7fff;
  1.1114 +
  1.1115 +			if (gbMemory[0xff68] & 0x80)
  1.1116 +			{
  1.1117 +				int index = ((gbMemory[0xff68] & 0x3f) + 1) & 0x3f;
  1.1118 +
  1.1119 +				gbMemory[0xff68] = (gbMemory[0xff68] & 0x80) | index;
  1.1120 +
  1.1121 +				gbMemory[0xff69] = (index & 1 ?
  1.1122 +				                    (gbPalette[index >> 1] >> 8) :
  1.1123 +				                    (gbPalette[index >> 1] & 0x00ff));
  1.1124 +			}
  1.1125 +			return;
  1.1126 +		}
  1.1127 +		break;
  1.1128 +	}
  1.1129 +
  1.1130 +	// OCPS
  1.1131 +	case 0x6a:
  1.1132 +	{
  1.1133 +		if (gbCgbMode)
  1.1134 +		{
  1.1135 +			int paletteIndex = (value & 0x3f) >> 1;
  1.1136 +			int paletteHiLo	 = (value & 0x01);
  1.1137 +
  1.1138 +			paletteIndex += 32;
  1.1139 +
  1.1140 +			gbMemory[0xff6a] = value;
  1.1141 +			gbMemory[0xff6b] = (paletteHiLo ?
  1.1142 +			                    (gbPalette[paletteIndex] >> 8) :
  1.1143 +			                    (gbPalette[paletteIndex] & 0x00ff));
  1.1144 +			return;
  1.1145 +		}
  1.1146 +		break;
  1.1147 +	}
  1.1148 +
  1.1149 +	// OCPD
  1.1150 +	case 0x6b:
  1.1151 +	{
  1.1152 +		if (gbCgbMode)
  1.1153 +		{
  1.1154 +			int v = gbMemory[0xff6a];
  1.1155 +			int paletteIndex = (v & 0x3f) >> 1;
  1.1156 +			int paletteHiLo	 = (v & 0x01);
  1.1157 +
  1.1158 +			paletteIndex += 32;
  1.1159 +
  1.1160 +			gbMemory[0xff6b]		= value;
  1.1161 +			gbPalette[paletteIndex] = (paletteHiLo ?
  1.1162 +			                           ((value << 8) | (gbPalette[paletteIndex] & 0xff)) :
  1.1163 +			                           ((gbPalette[paletteIndex] & 0xff00) | (value))) & 0x7fff;
  1.1164 +			if (gbMemory[0xff6a] & 0x80)
  1.1165 +			{
  1.1166 +				int index = ((gbMemory[0xff6a] & 0x3f) + 1) & 0x3f;
  1.1167 +
  1.1168 +				gbMemory[0xff6a] = (gbMemory[0xff6a] & 0x80) | index;
  1.1169 +
  1.1170 +				gbMemory[0xff6b] = (index & 1 ?
  1.1171 +				                    (gbPalette[(index >> 1) + 32] >> 8) :
  1.1172 +				                    (gbPalette[(index >> 1) + 32] & 0x00ff));
  1.1173 +			}
  1.1174 +			return;
  1.1175 +		}
  1.1176 +		break;
  1.1177 +	}
  1.1178 +
  1.1179 +	// SVBK
  1.1180 +	case 0x70:
  1.1181 +	{
  1.1182 +		if (gbCgbMode)
  1.1183 +		{
  1.1184 +			value = value & 7;
  1.1185 +
  1.1186 +			int bank = value;
  1.1187 +			if (value == 0)
  1.1188 +				bank = 1;
  1.1189 +
  1.1190 +			if (bank == gbWramBank)
  1.1191 +				return;
  1.1192 +
  1.1193 +			int wramAddress = bank * 0x1000;
  1.1194 +			gbMemoryMap[0x0d] = &gbWram[wramAddress];
  1.1195 +
  1.1196 +			gbWramBank	  = bank;
  1.1197 +			register_SVBK = value;
  1.1198 +			return;
  1.1199 +		}
  1.1200 +		break;
  1.1201 +	}
  1.1202 +
  1.1203 +	case 0xff:
  1.1204 +	{
  1.1205 +		register_IE	 = value;
  1.1206 +		register_IF &= value;
  1.1207 +		return;
  1.1208 +	}
  1.1209 +	}
  1.1210 +
  1.1211 +	gbWriteMemoryQuick(address, value);
  1.1212 +}
  1.1213 +
  1.1214 +u8 gbReadOpcode(register u16 address)
  1.1215 +{
  1.1216 +	if (gbCheatMap[address])
  1.1217 +		return gbCheatRead(address);
  1.1218 +
  1.1219 +	// the following fix does more than Echo RAM fix, anyway...
  1.1220 +	switch (gbEchoRAMFixOn ? (address >> 12) & 0x000f : address & 0xf000)
  1.1221 +	{
  1.1222 +	case 0x0a:
  1.1223 +	case 0x0b:
  1.1224 +		if (mapperReadRAM)
  1.1225 +			return mapperReadRAM(address);
  1.1226 +		break;
  1.1227 +	case 0x0f:
  1.1228 +		if (address > 0xff00)
  1.1229 +		{
  1.1230 +			switch (address & 0x00ff)
  1.1231 +			{
  1.1232 +			case 0x04:
  1.1233 +				return register_DIV;
  1.1234 +			case 0x05:
  1.1235 +				return register_TIMA;
  1.1236 +			case 0x06:
  1.1237 +				return register_TMA;
  1.1238 +			case 0x07:
  1.1239 +				return (0xf8 | register_TAC);
  1.1240 +			case 0x0f:
  1.1241 +				return (0xe0 | register_IF);
  1.1242 +			case 0x40:
  1.1243 +				return register_LCDC;
  1.1244 +			case 0x41:
  1.1245 +				return (0x80 | register_STAT);
  1.1246 +			case 0x42:
  1.1247 +				return register_SCY;
  1.1248 +			case 0x43:
  1.1249 +				return register_SCX;
  1.1250 +			case 0x44:
  1.1251 +				return register_LY;
  1.1252 +			case 0x45:
  1.1253 +				return register_LYC;
  1.1254 +			case 0x46:
  1.1255 +				return register_DMA;
  1.1256 +			case 0x4a:
  1.1257 +				return register_WY;
  1.1258 +			case 0x4b:
  1.1259 +				return register_WX;
  1.1260 +			case 0x4f:
  1.1261 +				return (0xfe | register_VBK);
  1.1262 +			case 0x51:
  1.1263 +				return register_HDMA1;
  1.1264 +			case 0x52:
  1.1265 +				return register_HDMA2;
  1.1266 +			case 0x53:
  1.1267 +				return register_HDMA3;
  1.1268 +			case 0x54:
  1.1269 +				return register_HDMA4;
  1.1270 +			case 0x55:
  1.1271 +				return register_HDMA5;
  1.1272 +			case 0x70:
  1.1273 +				return (0xf8 | register_SVBK);
  1.1274 +			case 0xff:
  1.1275 +				return register_IE;
  1.1276 +			}
  1.1277 +		}
  1.1278 +		break;
  1.1279 +	}
  1.1280 +	return gbReadMemoryQuick(address);
  1.1281 +}
  1.1282 +
  1.1283 +void gbWriteMemory(register u16 address, register u8 value)
  1.1284 +{
  1.1285 +	gbWriteMemoryWrapped(address, value);
  1.1286 +	CallRegisteredLuaMemHook(address, 1, value, LUAMEMHOOK_WRITE);
  1.1287 +}
  1.1288 +
  1.1289 +u8 gbReadMemory(register u16 address)
  1.1290 +{
  1.1291 +	if (gbCheatMap[address])
  1.1292 +		return gbCheatRead(address);
  1.1293 +
  1.1294 +	if (address < 0xa000)
  1.1295 +		return gbReadMemoryQuick(address);
  1.1296 +
  1.1297 +	if (address < 0xc000)
  1.1298 +	{
  1.1299 +#ifndef FINAL_VERSION
  1.1300 +		if (memorydebug)
  1.1301 +		{
  1.1302 +			log("Memory register read %04x PC=%04x\n",
  1.1303 +			    address,
  1.1304 +			    PC.W);
  1.1305 +		}
  1.1306 +#endif
  1.1307 +
  1.1308 +		if (mapperReadRAM)
  1.1309 +			return mapperReadRAM(address);
  1.1310 +		return gbReadMemoryQuick(address);
  1.1311 +	}
  1.1312 +
  1.1313 +	if (address >= 0xff00)
  1.1314 +	{
  1.1315 +		switch (address & 0x00ff)
  1.1316 +		{
  1.1317 +		case 0x00:
  1.1318 +		{
  1.1319 +			if (gbSgbMode)
  1.1320 +			{
  1.1321 +				gbSgbReadingController |= 4;
  1.1322 +				gbSgbResetPacketState();
  1.1323 +			}
  1.1324 +
  1.1325 +			int b = gbMemory[0xff00];
  1.1326 +
  1.1327 +			if ((b & 0x30) == 0x20)
  1.1328 +			{
  1.1329 +				b &= 0xf0;
  1.1330 +
  1.1331 +				int joy = 0;
  1.1332 +				if (gbSgbMode && gbSgbMultiplayer)
  1.1333 +				{
  1.1334 +					switch (gbSgbNextController)
  1.1335 +					{
  1.1336 +					case 0x0f:
  1.1337 +						joy = 0;
  1.1338 +						break;
  1.1339 +					case 0x0e:
  1.1340 +						joy = 1;
  1.1341 +						break;
  1.1342 +					case 0x0d:
  1.1343 +						joy = 2;
  1.1344 +						break;
  1.1345 +					case 0x0c:
  1.1346 +						joy = 3;
  1.1347 +						break;
  1.1348 +					default:
  1.1349 +						joy = 0;
  1.1350 +					}
  1.1351 +				}
  1.1352 +				int joystate = gbJoymask[joy];
  1.1353 +				if (!(joystate & 128))
  1.1354 +					b |= 0x08;
  1.1355 +				if (!(joystate & 64))
  1.1356 +					b |= 0x04;
  1.1357 +				if (!(joystate & 32))
  1.1358 +					b |= 0x02;
  1.1359 +				if (!(joystate & 16))
  1.1360 +					b |= 0x01;
  1.1361 +
  1.1362 +				gbMemory[0xff00] = b;
  1.1363 +			}
  1.1364 +			else if ((b & 0x30) == 0x10)
  1.1365 +			{
  1.1366 +				b &= 0xf0;
  1.1367 +
  1.1368 +				int joy = 0;
  1.1369 +				if (gbSgbMode && gbSgbMultiplayer)
  1.1370 +				{
  1.1371 +					switch (gbSgbNextController)
  1.1372 +					{
  1.1373 +					case 0x0f:
  1.1374 +						joy = 0;
  1.1375 +						break;
  1.1376 +					case 0x0e:
  1.1377 +						joy = 1;
  1.1378 +						break;
  1.1379 +					case 0x0d:
  1.1380 +						joy = 2;
  1.1381 +						break;
  1.1382 +					case 0x0c:
  1.1383 +						joy = 3;
  1.1384 +						break;
  1.1385 +					default:
  1.1386 +						joy = 0;
  1.1387 +					}
  1.1388 +				}
  1.1389 +				int joystate = gbJoymask[joy];
  1.1390 +				if (!(joystate & 8))
  1.1391 +					b |= 0x08;
  1.1392 +				if (!(joystate & 4))
  1.1393 +					b |= 0x04;
  1.1394 +				if (!(joystate & 2))
  1.1395 +					b |= 0x02;
  1.1396 +				if (!(joystate & 1))
  1.1397 +					b |= 0x01;
  1.1398 +
  1.1399 +				gbMemory[0xff00] = b;
  1.1400 +			}
  1.1401 +			else
  1.1402 +			{
  1.1403 +				if (gbSgbMode && gbSgbMultiplayer)
  1.1404 +				{
  1.1405 +					gbMemory[0xff00] = 0xf0 | gbSgbNextController;
  1.1406 +				}
  1.1407 +				else
  1.1408 +				{
  1.1409 +					gbMemory[0xff00] = 0xff;
  1.1410 +				}
  1.1411 +			}
  1.1412 +		}
  1.1413 +			GBSystemCounters.lagged = false;
  1.1414 +			return gbMemory[0xff00];
  1.1415 +			break;
  1.1416 +		case 0x01:
  1.1417 +			return gbMemory[0xff01];
  1.1418 +		case 0x04:
  1.1419 +			return register_DIV;
  1.1420 +		case 0x05:
  1.1421 +			return register_TIMA;
  1.1422 +		case 0x06:
  1.1423 +			return register_TMA;
  1.1424 +		case 0x07:
  1.1425 +			return (0xf8 | register_TAC);
  1.1426 +		case 0x0f:
  1.1427 +			return (0xe0 | register_IF);
  1.1428 +		case 0x40:
  1.1429 +			return register_LCDC;
  1.1430 +		case 0x41:
  1.1431 +			return (0x80 | register_STAT);
  1.1432 +		case 0x42:
  1.1433 +			return register_SCY;
  1.1434 +		case 0x43:
  1.1435 +			return register_SCX;
  1.1436 +		case 0x44:
  1.1437 +			return register_LY;
  1.1438 +		case 0x45:
  1.1439 +			return register_LYC;
  1.1440 +		case 0x46:
  1.1441 +			return register_DMA;
  1.1442 +		case 0x4a:
  1.1443 +			return register_WY;
  1.1444 +		case 0x4b:
  1.1445 +			return register_WX;
  1.1446 +		case 0x4f:
  1.1447 +			return (0xfe | register_VBK);
  1.1448 +		case 0x51:
  1.1449 +			return register_HDMA1;
  1.1450 +		case 0x52:
  1.1451 +			return register_HDMA2;
  1.1452 +		case 0x53:
  1.1453 +			return register_HDMA3;
  1.1454 +		case 0x54:
  1.1455 +			return register_HDMA4;
  1.1456 +		case 0x55:
  1.1457 +			return register_HDMA5;
  1.1458 +		case 0x70:
  1.1459 +			return (0xf8 | register_SVBK);
  1.1460 +		case 0xff:
  1.1461 +			return register_IE;
  1.1462 +		}
  1.1463 +	}
  1.1464 +
  1.1465 +	return gbReadMemoryQuick(address);
  1.1466 +}
  1.1467 +
  1.1468 +void gbVblank_interrupt()
  1.1469 +{
  1.1470 +	if (IFF & 0x80)
  1.1471 +	{
  1.1472 +		PC.W++;
  1.1473 +		IFF &= 0x7f;
  1.1474 +	}
  1.1475 +	gbInterrupt &= 0xfe;
  1.1476 +
  1.1477 +	IFF			&= 0x7e;
  1.1478 +	register_IF &= 0xfe;
  1.1479 +
  1.1480 +	gbWriteMemory(--SP.W, PC.B.B1);
  1.1481 +	gbWriteMemory(--SP.W, PC.B.B0);
  1.1482 +	PC.W = 0x40;
  1.1483 +}
  1.1484 +
  1.1485 +void gbLcd_interrupt()
  1.1486 +{
  1.1487 +	if (IFF & 0x80)
  1.1488 +	{
  1.1489 +		PC.W++;
  1.1490 +		IFF &= 0x7f;
  1.1491 +	}
  1.1492 +	gbInterrupt &= 0xfd;
  1.1493 +	IFF			&= 0x7e;
  1.1494 +	register_IF &= 0xfd;
  1.1495 +
  1.1496 +	gbWriteMemory(--SP.W, PC.B.B1);
  1.1497 +	gbWriteMemory(--SP.W, PC.B.B0);
  1.1498 +
  1.1499 +	PC.W = 0x48;
  1.1500 +}
  1.1501 +
  1.1502 +void gbTimer_interrupt()
  1.1503 +{
  1.1504 +	if (IFF & 0x80)
  1.1505 +	{
  1.1506 +		PC.W++;
  1.1507 +		IFF &= 0x7f;
  1.1508 +	}
  1.1509 +	IFF			&= 0x7e;
  1.1510 +	gbInterrupt &= 0xfb;
  1.1511 +	register_IF &= 0xfb;
  1.1512 +
  1.1513 +	gbWriteMemory(--SP.W, PC.B.B1);
  1.1514 +	gbWriteMemory(--SP.W, PC.B.B0);
  1.1515 +
  1.1516 +	PC.W = 0x50;
  1.1517 +}
  1.1518 +
  1.1519 +void gbSerial_interrupt()
  1.1520 +{
  1.1521 +	if (IFF & 0x80)
  1.1522 +	{
  1.1523 +		PC.W++;
  1.1524 +		IFF &= 0x7f;
  1.1525 +	}
  1.1526 +	IFF			&= 0x7e;
  1.1527 +	gbInterrupt &= 0xf7;
  1.1528 +	register_IF &= 0xf7;
  1.1529 +
  1.1530 +	gbWriteMemory(--SP.W, PC.B.B1);
  1.1531 +	gbWriteMemory(--SP.W, PC.B.B0);
  1.1532 +
  1.1533 +	PC.W = 0x58;
  1.1534 +}
  1.1535 +
  1.1536 +void gbJoypad_interrupt()
  1.1537 +{
  1.1538 +	if (IFF & 0x80)
  1.1539 +	{
  1.1540 +		PC.W++;
  1.1541 +		IFF &= 0x7f;
  1.1542 +	}
  1.1543 +	IFF			&= 0x7e;
  1.1544 +	gbInterrupt &= 0xef;
  1.1545 +	register_IF &= 0xef;
  1.1546 +
  1.1547 +	gbWriteMemory(--SP.W, PC.B.B1);
  1.1548 +	gbWriteMemory(--SP.W, PC.B.B0);
  1.1549 +
  1.1550 +	PC.W = 0x60;
  1.1551 +}
  1.1552 +
  1.1553 +void gbSpeedSwitch()
  1.1554 +{
  1.1555 +	if (gbSpeed == 0)
  1.1556 +	{
  1.1557 +		gbSpeed = 1;
  1.1558 +		GBLCD_MODE_0_CLOCK_TICKS   = 51 * 2; //127; //51 * 2;
  1.1559 +		GBLCD_MODE_1_CLOCK_TICKS   = 1140 * 2;
  1.1560 +		GBLCD_MODE_2_CLOCK_TICKS   = 20 * 2; //52; //20 * 2;
  1.1561 +		GBLCD_MODE_3_CLOCK_TICKS   = 43 * 2; //99; //43 * 2;
  1.1562 +		GBDIV_CLOCK_TICKS		   = 64 * 2;
  1.1563 +		GBLY_INCREMENT_CLOCK_TICKS = 114 * 2;
  1.1564 +		GBTIMER_MODE_0_CLOCK_TICKS = 256; //256*2;
  1.1565 +		GBTIMER_MODE_1_CLOCK_TICKS = 4; //4*2;
  1.1566 +		GBTIMER_MODE_2_CLOCK_TICKS = 16; //16*2;
  1.1567 +		GBTIMER_MODE_3_CLOCK_TICKS = 64; //64*2;
  1.1568 +		GBSERIAL_CLOCK_TICKS	   = 128 * 2;
  1.1569 +		gbDivTicks *= 2;
  1.1570 +		gbLcdTicks *= 2;
  1.1571 +		gbLcdLYIncrementTicks *= 2;
  1.1572 +		//    timerTicks *= 2;
  1.1573 +		//    timerClockTicks *= 2;
  1.1574 +		gbSerialTicks	 *= 2;
  1.1575 +		SOUND_CLOCK_TICKS = soundQuality * GB_USE_TICKS_AS * 2;
  1.1576 +		soundTicks		 *= 2;
  1.1577 +		//    synchronizeTicks *= 2;
  1.1578 +		//    SYNCHRONIZE_CLOCK_TICKS *= 2;
  1.1579 +	}
  1.1580 +	else
  1.1581 +	{
  1.1582 +		gbSpeed = 0;
  1.1583 +		GBLCD_MODE_0_CLOCK_TICKS   = 51;
  1.1584 +		GBLCD_MODE_1_CLOCK_TICKS   = 1140;
  1.1585 +		GBLCD_MODE_2_CLOCK_TICKS   = 20;
  1.1586 +		GBLCD_MODE_3_CLOCK_TICKS   = 43;
  1.1587 +		GBDIV_CLOCK_TICKS		   = 64;
  1.1588 +		GBLY_INCREMENT_CLOCK_TICKS = 114;
  1.1589 +		GBTIMER_MODE_0_CLOCK_TICKS = 256;
  1.1590 +		GBTIMER_MODE_1_CLOCK_TICKS = 4;
  1.1591 +		GBTIMER_MODE_2_CLOCK_TICKS = 16;
  1.1592 +		GBTIMER_MODE_3_CLOCK_TICKS = 64;
  1.1593 +		GBSERIAL_CLOCK_TICKS	   = 128;
  1.1594 +		gbDivTicks /= 2;
  1.1595 +		gbLcdTicks /= 2;
  1.1596 +		gbLcdLYIncrementTicks /= 2;
  1.1597 +		//    timerTicks /= 2;
  1.1598 +		//    timerClockTicks /= 2;
  1.1599 +		gbSerialTicks	 /= 2;
  1.1600 +		SOUND_CLOCK_TICKS = soundQuality * GB_USE_TICKS_AS;
  1.1601 +		soundTicks		 /= 2;
  1.1602 +		//    synchronizeTicks /= 2;
  1.1603 +		//    SYNCHRONIZE_CLOCK_TICKS /= 2;
  1.1604 +	}
  1.1605 +}
  1.1606 +
  1.1607 +void gbGetHardwareType()
  1.1608 +{
  1.1609 +	gbCgbMode = 0;
  1.1610 +	if (gbRom[0x143] & 0x80)
  1.1611 +	{
  1.1612 +		if (gbEmulatorType == 0 ||
  1.1613 +		    gbEmulatorType == 1 ||
  1.1614 +		    gbEmulatorType == 4 ||
  1.1615 +		    gbEmulatorType == 5 ||
  1.1616 +		    (gbRom[0x146] != 0x03 && (gbEmulatorType == 2)))
  1.1617 +		{
  1.1618 +			gbCgbMode = 1;
  1.1619 +		}
  1.1620 +	}
  1.1621 +
  1.1622 +	if (gbSgbMode == 2)
  1.1623 +	{
  1.1624 +		gbSgbMode = 0;
  1.1625 +		return;
  1.1626 +	}
  1.1627 +
  1.1628 +	gbSgbMode = 0;
  1.1629 +	if (gbRom[0x146] == 0x03)
  1.1630 +	{
  1.1631 +		if (gbEmulatorType == 0 ||
  1.1632 +		    gbEmulatorType == 2 ||
  1.1633 +		    gbEmulatorType == 5 ||
  1.1634 +		    (!(gbRom[0x143] & 0x80) && (gbEmulatorType == 1 || gbEmulatorType == 4)))
  1.1635 +			gbSgbMode = 1;
  1.1636 +	}
  1.1637 +}
  1.1638 +
  1.1639 +void gbReset(bool userReset)
  1.1640 +{
  1.1641 +	// movie must be closed while opening/creating a movie
  1.1642 +	if (userReset && VBAMovieRecording())
  1.1643 +	{
  1.1644 +		VBAMovieSignalReset();
  1.1645 +		return;
  1.1646 +	}
  1.1647 +
  1.1648 +	if (!VBAMovieActive())
  1.1649 +	{
  1.1650 +		GBSystemCounters.frameCount = 0;
  1.1651 +		GBSystemCounters.lagCount	= 0;
  1.1652 +		GBSystemCounters.extraCount = 0;
  1.1653 +		GBSystemCounters.lagged		= true;
  1.1654 +		GBSystemCounters.laggedLast = true;
  1.1655 +	}
  1.1656 +
  1.1657 +	SP.W = 0xfffe;
  1.1658 +	AF.W = 0x01b0;
  1.1659 +	BC.W = 0x0013;
  1.1660 +	DE.W = 0x00d8;
  1.1661 +	HL.W = 0x014d;
  1.1662 +	PC.W = 0x0100;
  1.1663 +	IFF	 = 0;
  1.1664 +	gbInterrupt		= 1;
  1.1665 +	gbInterruptWait = 0;
  1.1666 +
  1.1667 +	register_DIV   = 0;
  1.1668 +	register_TIMA  = 0;
  1.1669 +	register_TMA   = 0;
  1.1670 +	register_TAC   = 0;
  1.1671 +	register_IF	   = 1;
  1.1672 +	register_LCDC  = 0x91;
  1.1673 +	register_STAT  = 0;
  1.1674 +	register_SCY   = 0;
  1.1675 +	register_SCX   = 0;
  1.1676 +	register_LY	   = 0;
  1.1677 +	register_LYC   = 0;
  1.1678 +	register_DMA   = 0;
  1.1679 +	register_WY	   = 0;
  1.1680 +	register_WX	   = 0;
  1.1681 +	register_VBK   = 0;
  1.1682 +	register_HDMA1 = 0;
  1.1683 +	register_HDMA2 = 0;
  1.1684 +	register_HDMA3 = 0;
  1.1685 +	register_HDMA4 = 0;
  1.1686 +	register_HDMA5 = 0;
  1.1687 +	register_SVBK  = 0;
  1.1688 +	register_IE	   = 0;
  1.1689 +
  1.1690 +	gbGetHardwareType();
  1.1691 +	if (gbCgbMode)
  1.1692 +	{
  1.1693 +		if (!gbVram)
  1.1694 +			gbVram = (u8 *)malloc(0x4000 + 4);
  1.1695 +		if (!gbWram)
  1.1696 +			gbWram = (u8 *)malloc(0x8000 + 4);
  1.1697 +		memset(gbVram, 0, 0x4000 + 4);
  1.1698 +		memset(gbWram, 0, 0x8000 + 4);
  1.1699 +	}
  1.1700 +	else
  1.1701 +	{
  1.1702 +		if (gbVram)
  1.1703 +		{
  1.1704 +			free(gbVram);
  1.1705 +			gbVram = NULL;
  1.1706 +		}
  1.1707 +		if (gbWram)
  1.1708 +		{
  1.1709 +			free(gbWram);
  1.1710 +			gbWram = NULL;
  1.1711 +		}
  1.1712 +	}
  1.1713 +
  1.1714 +	// clean LineBuffer
  1.1715 +	if (gbLineBuffer)
  1.1716 +		memset(gbLineBuffer, 0, 160 * sizeof(u16));
  1.1717 +	// clean Pix
  1.1718 +	if (pix)
  1.1719 +		memset(pix, 0, 4 * 257 * 226);
  1.1720 +
  1.1721 +	if (gbCgbMode)
  1.1722 +	{
  1.1723 +		if (gbSgbMode)
  1.1724 +		{
  1.1725 +			if (gbEmulatorType == 5)
  1.1726 +				AF.W = 0xffb0;
  1.1727 +			else
  1.1728 +				AF.W = 0x01b0;
  1.1729 +			BC.W = 0x0013;
  1.1730 +			DE.W = 0x00d8;
  1.1731 +			HL.W = 0x014d;
  1.1732 +		}
  1.1733 +		else
  1.1734 +		{
  1.1735 +			AF.W = 0x11b0;
  1.1736 +			BC.W = 0x0000;
  1.1737 +			DE.W = 0xff56;
  1.1738 +			HL.W = 0x000d;
  1.1739 +		}
  1.1740 +		if (gbEmulatorType == 4)
  1.1741 +			BC.B.B1 |= 0x01;
  1.1742 +
  1.1743 +		register_HDMA5	 = 0xff;
  1.1744 +		gbMemory[0xff68] = 0xc0;
  1.1745 +		gbMemory[0xff6a] = 0xc0;
  1.1746 +
  1.1747 +		for (int i = 0; i < 64; i++)
  1.1748 +			gbPalette[i] = 0x7fff;
  1.1749 +	}
  1.1750 +	else
  1.1751 +	{
  1.1752 +		for (int i = 0; i < 8; i++)
  1.1753 +			gbPalette[i] = systemGbPalette[gbPaletteOption * 8 + i];
  1.1754 +	}
  1.1755 +
  1.1756 +	if (gbSpeed)
  1.1757 +	{
  1.1758 +		gbSpeedSwitch();
  1.1759 +		gbMemory[0xff4d] = 0;
  1.1760 +	}
  1.1761 +
  1.1762 +	gbDivTicks = GBDIV_CLOCK_TICKS;
  1.1763 +	gbLcdMode  = 2;
  1.1764 +	gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS;
  1.1765 +	gbLcdLYIncrementTicks = 0;
  1.1766 +	gbTimerTicks		  = 0;
  1.1767 +	gbTimerClockTicks	  = 0;
  1.1768 +	gbSerialTicks		  = 0;
  1.1769 +	gbSerialBits		  = 0;
  1.1770 +	gbSerialOn			  = 0;
  1.1771 +	gbWindowLine		  = -1;
  1.1772 +	gbTimerOn			  = 0;
  1.1773 +	gbTimerMode			  = 0;
  1.1774 +	//  gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS;
  1.1775 +	gbSpeed		 = 0;
  1.1776 +	gbJoymask[0] = gbJoymask[1] = gbJoymask[2] = gbJoymask[3] = 0;
  1.1777 +
  1.1778 +	// FIXME: horrible kludge
  1.1779 +	memset(s_gbJoymask, 0, sizeof(s_gbJoymask));
  1.1780 +
  1.1781 +	if (gbCgbMode)
  1.1782 +	{
  1.1783 +		gbSpeed	 = 0;
  1.1784 +		gbHdmaOn = 0;
  1.1785 +		gbHdmaSource	  = 0x0000;
  1.1786 +		gbHdmaDestination = 0x8000;
  1.1787 +		gbVramBank		  = 0;
  1.1788 +		gbWramBank		  = 1;
  1.1789 +		register_LY		  = 0x90;
  1.1790 +		gbLcdMode		  = 1;
  1.1791 +	}
  1.1792 +
  1.1793 +	if (gbSgbMode)
  1.1794 +	{
  1.1795 +		gbSgbReset();
  1.1796 +	}
  1.1797 +
  1.1798 +	for (int i = 0; i < 4; i++)
  1.1799 +		gbBgp[i] = gbObp0[i] = gbObp1[i] = i;
  1.1800 +
  1.1801 +	memset(&gbDataMBC1, 0, sizeof(gbDataMBC1));
  1.1802 +	gbDataMBC1.mapperROMBank = 1;
  1.1803 +
  1.1804 +	gbDataMBC2.mapperRAMEnable = 0;
  1.1805 +	gbDataMBC2.mapperROMBank   = 1;
  1.1806 +
  1.1807 +	memset(&gbDataMBC3, 0, 6 * sizeof(int32));
  1.1808 +	gbDataMBC3.mapperROMBank = 1;
  1.1809 +
  1.1810 +	memset(&gbDataMBC5, 0, sizeof(gbDataMBC5));
  1.1811 +	gbDataMBC5.mapperROMBank = 1;
  1.1812 +	switch (gbRom[0x147])
  1.1813 +	{
  1.1814 +	case 0x1c:
  1.1815 +	case 0x1d:
  1.1816 +	case 0x1e:
  1.1817 +		gbDataMBC5.isRumbleCartridge = 1;
  1.1818 +	}
  1.1819 +
  1.1820 +	memset(&gbDataHuC1, 0, sizeof(gbDataHuC1));
  1.1821 +	gbDataHuC1.mapperROMBank = 1;
  1.1822 +
  1.1823 +	memset(&gbDataHuC3, 0, sizeof(gbDataHuC3));
  1.1824 +	gbDataHuC3.mapperROMBank = 1;
  1.1825 +
  1.1826 +	gbMemoryMap[0x00] = &gbRom[0x0000];
  1.1827 +	gbMemoryMap[0x01] = &gbRom[0x1000];
  1.1828 +	gbMemoryMap[0x02] = &gbRom[0x2000];
  1.1829 +	gbMemoryMap[0x03] = &gbRom[0x3000];
  1.1830 +	gbMemoryMap[0x04] = &gbRom[0x4000];
  1.1831 +	gbMemoryMap[0x05] = &gbRom[0x5000];
  1.1832 +	gbMemoryMap[0x06] = &gbRom[0x6000];
  1.1833 +	gbMemoryMap[0x07] = &gbRom[0x7000];
  1.1834 +	if (gbCgbMode)
  1.1835 +	{
  1.1836 +		gbMemoryMap[0x08] = &gbVram[0x0000];
  1.1837 +		gbMemoryMap[0x09] = &gbVram[0x1000];
  1.1838 +		gbMemoryMap[0x0a] = &gbMemory[0xa000];
  1.1839 +		gbMemoryMap[0x0b] = &gbMemory[0xb000];
  1.1840 +		gbMemoryMap[0x0c] = &gbMemory[0xc000];
  1.1841 +		gbMemoryMap[0x0d] = &gbWram[0x1000];
  1.1842 +		gbMemoryMap[0x0e] = &gbMemory[0xe000];
  1.1843 +		gbMemoryMap[0x0f] = &gbMemory[0xf000];
  1.1844 +	}
  1.1845 +	else
  1.1846 +	{
  1.1847 +		gbMemoryMap[0x08] = &gbMemory[0x8000];
  1.1848 +		gbMemoryMap[0x09] = &gbMemory[0x9000];
  1.1849 +		gbMemoryMap[0x0a] = &gbMemory[0xa000];
  1.1850 +		gbMemoryMap[0x0b] = &gbMemory[0xb000];
  1.1851 +		gbMemoryMap[0x0c] = &gbMemory[0xc000];
  1.1852 +		gbMemoryMap[0x0d] = &gbMemory[0xd000];
  1.1853 +		gbMemoryMap[0x0e] = &gbMemory[0xe000];
  1.1854 +		gbMemoryMap[0x0f] = &gbMemory[0xf000];
  1.1855 +	}
  1.1856 +
  1.1857 +	if (gbRam)
  1.1858 +	{
  1.1859 +		gbMemoryMap[0x0a] = &gbRam[0x0000];
  1.1860 +		gbMemoryMap[0x0b] = &gbRam[0x1000];
  1.1861 +	}
  1.1862 +
  1.1863 +	gbSoundReset();
  1.1864 +
  1.1865 +	systemResetSensor();
  1.1866 +
  1.1867 +	systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
  1.1868 +
  1.1869 +	gbLastTime	 = systemGetClock();
  1.1870 +	gbFrameCount = 0;
  1.1871 +
  1.1872 +	systemRefreshScreen();
  1.1873 +}
  1.1874 +
  1.1875 +void gbWriteSaveMBC1(const char *name)
  1.1876 +{
  1.1877 +	FILE *gzFile = fopen(name, "wb");
  1.1878 +
  1.1879 +	if (gzFile == NULL)
  1.1880 +	{
  1.1881 +		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
  1.1882 +		return;
  1.1883 +	}
  1.1884 +
  1.1885 +	fwrite(gbRam,
  1.1886 +	       1,
  1.1887 +	       gbRamSize,
  1.1888 +	       gzFile);
  1.1889 +
  1.1890 +	fclose(gzFile);
  1.1891 +}
  1.1892 +
  1.1893 +void gbWriteSaveMBC2(const char *name)
  1.1894 +{
  1.1895 +	FILE *file = fopen(name, "wb");
  1.1896 +
  1.1897 +	if (file == NULL)
  1.1898 +	{
  1.1899 +		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
  1.1900 +		return;
  1.1901 +	}
  1.1902 +
  1.1903 +	fwrite(&gbMemory[0xa000],
  1.1904 +	       1,
  1.1905 +	       256,
  1.1906 +	       file);
  1.1907 +
  1.1908 +	fclose(file);
  1.1909 +}
  1.1910 +
  1.1911 +void gbWriteSaveMBC3(const char *name, bool extendedSave)
  1.1912 +{
  1.1913 +	FILE *gzFile = fopen(name, "wb");
  1.1914 +
  1.1915 +	if (gzFile == NULL)
  1.1916 +	{
  1.1917 +		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
  1.1918 +		return;
  1.1919 +	}
  1.1920 +
  1.1921 +	fwrite(gbRam,
  1.1922 +	       1,
  1.1923 +	       gbRamSize,
  1.1924 +	       gzFile);
  1.1925 +
  1.1926 +	if (extendedSave)
  1.1927 +	{
  1.1928 +		//assert(sizeof(time_t) == 4);
  1.1929 +		fwrite(&gbDataMBC3.mapperSeconds,
  1.1930 +		       1,
  1.1931 +		       10 * sizeof(int32) + /*sizeof(time_t)*/4,
  1.1932 +		       gzFile);
  1.1933 +	}
  1.1934 +
  1.1935 +	fclose(gzFile);
  1.1936 +}
  1.1937 +
  1.1938 +void gbWriteSaveMBC5(const char *name)
  1.1939 +{
  1.1940 +	FILE *gzFile = fopen(name, "wb");
  1.1941 +
  1.1942 +	if (gzFile == NULL)
  1.1943 +	{
  1.1944 +		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
  1.1945 +		return;
  1.1946 +	}
  1.1947 +
  1.1948 +	fwrite(gbRam,
  1.1949 +	       1,
  1.1950 +	       gbRamSize,
  1.1951 +	       gzFile);
  1.1952 +
  1.1953 +	fclose(gzFile);
  1.1954 +}
  1.1955 +
  1.1956 +void gbWriteSaveMBC7(const char *name)
  1.1957 +{
  1.1958 +	FILE *file = fopen(name, "wb");
  1.1959 +
  1.1960 +	if (file == NULL)
  1.1961 +	{
  1.1962 +		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name);
  1.1963 +		return;
  1.1964 +	}
  1.1965 +
  1.1966 +	fwrite(&gbMemory[0xa000],
  1.1967 +	       1,
  1.1968 +	       256,
  1.1969 +	       file);
  1.1970 +
  1.1971 +	fclose(file);
  1.1972 +}
  1.1973 +
  1.1974 +bool gbReadSaveMBC1(const char *name)
  1.1975 +{
  1.1976 +	gzFile gzFile = gzopen(name, "rb");
  1.1977 +
  1.1978 +	if (gzFile == NULL)
  1.1979 +	{
  1.1980 +		return false;
  1.1981 +	}
  1.1982 +
  1.1983 +	int read = gzread(gzFile,
  1.1984 +	                  gbRam,
  1.1985 +	                  gbRamSize);
  1.1986 +
  1.1987 +	if (read != gbRamSize)
  1.1988 +	{
  1.1989 +		systemMessage(MSG_FAILED_TO_READ_SGM, N_("Failed to read complete save game %s (%d)"), name, read);
  1.1990 +		gzclose(gzFile);
  1.1991 +		return false;
  1.1992 +	}
  1.1993 +
  1.1994 +	gzclose(gzFile);
  1.1995 +	return true;
  1.1996 +}
  1.1997 +
  1.1998 +bool gbReadSaveMBC2(const char *name)
  1.1999 +{
  1.2000 +	FILE *file = fopen(name, "rb");
  1.2001 +
  1.2002 +	if (file == NULL)
  1.2003 +	{
  1.2004 +		return false;
  1.2005 +	}
  1.2006 +
  1.2007 +	int read = fread(&gbMemory[0xa000],
  1.2008 +	                 1,
  1.2009 +	                 256,
  1.2010 +	                 file);
  1.2011 +
  1.2012 +	if (read != 256)
  1.2013 +	{
  1.2014 +		systemMessage(MSG_FAILED_TO_READ_SGM,
  1.2015 +		              N_("Failed to read complete save game %s (%d)"), name, read);
  1.2016 +		fclose(file);
  1.2017 +		return false;
  1.2018 +	}
  1.2019 +
  1.2020 +	fclose(file);
  1.2021 +	return true;
  1.2022 +}
  1.2023 +
  1.2024 +bool gbReadSaveMBC3(const char *name)
  1.2025 +{
  1.2026 +	gzFile gzFile = gzopen(name, "rb");
  1.2027 +
  1.2028 +	if (gzFile == NULL)
  1.2029 +	{
  1.2030 +		return false;
  1.2031 +	}
  1.2032 +
  1.2033 +	int read = gzread(gzFile,
  1.2034 +	                  gbRam,
  1.2035 +	                  gbRamSize);
  1.2036 +
  1.2037 +	bool res = true;
  1.2038 +
  1.2039 +	if (read != gbRamSize)
  1.2040 +	{
  1.2041 +		systemMessage(MSG_FAILED_TO_READ_SGM,
  1.2042 +		              N_("Failed to read complete save game %s (%d)"), name, read);
  1.2043 +	}
  1.2044 +	else
  1.2045 +	{
  1.2046 +		//assert(sizeof(time_t) == 4);
  1.2047 +		read = gzread(gzFile,
  1.2048 +		              &gbDataMBC3.mapperSeconds,
  1.2049 +		              sizeof(int32) * 10 + /*sizeof(time_t)*/4);
  1.2050 +
  1.2051 +		if (read != (sizeof(int32) * 10 + /*sizeof(time_t)*/4) && read != 0)
  1.2052 +		{
  1.2053 +			systemMessage(MSG_FAILED_TO_READ_RTC,
  1.2054 +			              N_("Failed to read RTC from save game %s (continuing)"),
  1.2055 +			              name);
  1.2056 +			res = false;
  1.2057 +		}
  1.2058 +	}
  1.2059 +
  1.2060 +	gzclose(gzFile);
  1.2061 +	return res;
  1.2062 +}
  1.2063 +
  1.2064 +bool gbReadSaveMBC5(const char *name)
  1.2065 +{
  1.2066 +	gzFile gzFile = gzopen(name, "rb");
  1.2067 +
  1.2068 +	if (gzFile == NULL)
  1.2069 +	{
  1.2070 +		return false;
  1.2071 +	}
  1.2072 +
  1.2073 +	int read = gzread(gzFile,
  1.2074 +	                  gbRam,
  1.2075 +	                  gbRamSize);
  1.2076 +
  1.2077 +	if (read != gbRamSize)
  1.2078 +	{
  1.2079 +		systemMessage(MSG_FAILED_TO_READ_SGM,
  1.2080 +		              N_("Failed to read complete save game %s (%d)"), name, read);
  1.2081 +		gzclose(gzFile);
  1.2082 +		return false;
  1.2083 +	}
  1.2084 +
  1.2085 +	gzclose(gzFile);
  1.2086 +	return true;
  1.2087 +}
  1.2088 +
  1.2089 +bool gbReadSaveMBC7(const char *name)
  1.2090 +{
  1.2091 +	FILE *file = fopen(name, "rb");
  1.2092 +
  1.2093 +	if (file == NULL)
  1.2094 +	{
  1.2095 +		return false;
  1.2096 +	}
  1.2097 +
  1.2098 +	int read = fread(&gbMemory[0xa000],
  1.2099 +	                 1,
  1.2100 +	                 256,
  1.2101 +	                 file);
  1.2102 +
  1.2103 +	if (read != 256)
  1.2104 +	{
  1.2105 +		systemMessage(MSG_FAILED_TO_READ_SGM,
  1.2106 +		              N_("Failed to read complete save game %s (%d)"), name, read);
  1.2107 +		fclose(file);
  1.2108 +		return false;
  1.2109 +	}
  1.2110 +
  1.2111 +	fclose(file);
  1.2112 +	return true;
  1.2113 +}
  1.2114 +
  1.2115 +#if 0
  1.2116 +bool gbLoadBIOS(const char *biosFileName, bool useBiosFile)
  1.2117 +{
  1.2118 +	useBios = false;
  1.2119 +	if (useBiosFile)
  1.2120 +	{
  1.2121 +		useBios = utilLoadBIOS(bios, biosFileName, gbEmulatorType);
  1.2122 +		if (!useBios)
  1.2123 +		{
  1.2124 +			systemMessage(MSG_INVALID_BIOS_FILE_SIZE, N_("Invalid BOOTROM file"));
  1.2125 +		}
  1.2126 +	}
  1.2127 +	return useBios;
  1.2128 +}
  1.2129 +#endif
  1.2130 +
  1.2131 +void gbInit()
  1.2132 +{
  1.2133 +	gbGenFilter();
  1.2134 +	gbSgbInit();    // calls gbSgbReset()... whatever
  1.2135 +
  1.2136 +	gbMemory = (u8 *)malloc(65536 + 4);
  1.2137 +	memset(gbMemory, 0, 65536 + 4);
  1.2138 +	memset(gbPalette, 0, 2 * 128);
  1.2139 +
  1.2140 +	// HACK: +4 at start to accomodate the 2xSaI filter reading out of bounds of the leftmost pixel
  1.2141 +	origPix = (u8 *)calloc(1, 4 * 257 * 226 + 4);
  1.2142 +	pix		= origPix + 4;
  1.2143 +
  1.2144 +	gbLineBuffer = (u16 *)malloc(160 * sizeof(u16));
  1.2145 +}
  1.2146 +
  1.2147 +bool gbWriteBatteryFile(const char *file, bool extendedSave)
  1.2148 +{
  1.2149 +	if (gbBattery)
  1.2150 +	{
  1.2151 +		int type = gbRom[0x147];
  1.2152 +
  1.2153 +		switch (type)
  1.2154 +		{
  1.2155 +		case 0x03:
  1.2156 +			gbWriteSaveMBC1(file);
  1.2157 +			break;
  1.2158 +		case 0x06:
  1.2159 +			gbWriteSaveMBC2(file);
  1.2160 +			break;
  1.2161 +		case 0x0f:
  1.2162 +		case 0x10:
  1.2163 +		case 0x13:
  1.2164 +			gbWriteSaveMBC3(file, extendedSave);
  1.2165 +			break;
  1.2166 +		case 0x1b:
  1.2167 +		case 0x1e:
  1.2168 +			gbWriteSaveMBC5(file);
  1.2169 +			break;
  1.2170 +		case 0x22:
  1.2171 +			gbWriteSaveMBC7(file);
  1.2172 +			break;
  1.2173 +		case 0xff:
  1.2174 +			gbWriteSaveMBC1(file);
  1.2175 +			break;
  1.2176 +		}
  1.2177 +	}
  1.2178 +	return true;
  1.2179 +}
  1.2180 +
  1.2181 +bool gbWriteBatteryFile(const char *file)
  1.2182 +{
  1.2183 +	gbWriteBatteryFile(file, true);
  1.2184 +	return true;
  1.2185 +}
  1.2186 +
  1.2187 +bool gbWriteBatteryToStream(gzFile gzfile)
  1.2188 +{
  1.2189 +	// the GB save code is ugly, so rather than convert it all to use gzFiles, just save it to a temp file...
  1.2190 +#define TEMP_SAVE_FNAME ("tempvbawrite.sav")
  1.2191 +	bool retVal = gbWriteBatteryFile(TEMP_SAVE_FNAME, true);
  1.2192 +
  1.2193 +	// ...open the temp file and figure out its size...
  1.2194 +	FILE *fileTemp = fopen(TEMP_SAVE_FNAME, "rb");
  1.2195 +	if (fileTemp == NULL)
  1.2196 +		return false;
  1.2197 +	fseek(fileTemp, 0, SEEK_END);
  1.2198 +	int len = (int) ftell(fileTemp);
  1.2199 +
  1.2200 +	// ...copy over the temp file...
  1.2201 +	char *temp = new char [len];
  1.2202 +	fseek(fileTemp, 0, SEEK_SET);
  1.2203 +	if (fread(temp, len, 1, fileTemp) != 1)
  1.2204 +	{
  1.2205 +		delete [] temp;
  1.2206 +		fclose(fileTemp);
  1.2207 +		return false;
  1.2208 +	}
  1.2209 +	fclose(fileTemp);
  1.2210 +	utilGzWrite(gzfile, temp, len);
  1.2211 +	delete [] temp;
  1.2212 +
  1.2213 +	// ... and delete the temp file
  1.2214 +	remove(TEMP_SAVE_FNAME);
  1.2215 +#undef TEMP_SAVE_FNAME
  1.2216 +
  1.2217 +	return retVal;
  1.2218 +}
  1.2219 +
  1.2220 +bool gbReadBatteryFile(const char *file)
  1.2221 +{
  1.2222 +	bool res = false;
  1.2223 +	if (gbBattery)
  1.2224 +	{
  1.2225 +		int type = gbRom[0x147];
  1.2226 +
  1.2227 +		switch (type)
  1.2228 +		{
  1.2229 +		case 0x03:
  1.2230 +			res = gbReadSaveMBC1(file);
  1.2231 +			break;
  1.2232 +		case 0x06:
  1.2233 +			res = gbReadSaveMBC2(file);
  1.2234 +			break;
  1.2235 +		case 0x0f:
  1.2236 +		case 0x10:
  1.2237 +		case 0x13:
  1.2238 +			if (!gbReadSaveMBC3(file))
  1.2239 +			{
  1.2240 +				struct tm *lt;
  1.2241 +				time_t tmp; //Small kludge to get it working on some systems where time_t has size 8.
  1.2242 +
  1.2243 +				if (VBAMovieActive() || VBAMovieLoading())
  1.2244 +				{
  1.2245 +					gbDataMBC3.mapperLastTime = VBAMovieGetId() + VBAMovieGetFrameCounter() / 60;
  1.2246 +					lt = gmtime(&tmp);
  1.2247 +					gbDataMBC3.mapperLastTime=(u32)tmp;
  1.2248 +				}
  1.2249 +				else
  1.2250 +				{
  1.2251 +					time(&tmp);
  1.2252 +					gbDataMBC3.mapperLastTime=(u32)tmp;
  1.2253 +					lt = localtime(&tmp);
  1.2254 +				}
  1.2255 +				systemScreenMessage(ctime(&tmp), 4);
  1.2256 +				gbDataMBC3.mapperLastTime=(u32)tmp;
  1.2257 +
  1.2258 +				gbDataMBC3.mapperSeconds = lt->tm_sec;
  1.2259 +				gbDataMBC3.mapperMinutes = lt->tm_min;
  1.2260 +				gbDataMBC3.mapperHours	 = lt->tm_hour;
  1.2261 +				gbDataMBC3.mapperDays	 = lt->tm_yday & 255;
  1.2262 +				gbDataMBC3.mapperControl = (gbDataMBC3.mapperControl & 0xfe) |
  1.2263 +				                           (lt->tm_yday > 255 ? 1 : 0);
  1.2264 +				res = false;
  1.2265 +				break;
  1.2266 +			}
  1.2267 +			time_t tmp;
  1.2268 +			systemScreenMessage(ctime(&tmp), 4);
  1.2269 +			gbDataMBC3.mapperLastTime=(u32)tmp;
  1.2270 +			res = true;
  1.2271 +			break;
  1.2272 +		case 0x1b:
  1.2273 +		case 0x1e:
  1.2274 +			res = gbReadSaveMBC5(file);
  1.2275 +			break;
  1.2276 +		case 0x22:
  1.2277 +			res = gbReadSaveMBC7(file);
  1.2278 +		case 0xff:
  1.2279 +			res = gbReadSaveMBC1(file);
  1.2280 +			break;
  1.2281 +		}
  1.2282 +	}
  1.2283 +	systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
  1.2284 +	return res;
  1.2285 +}
  1.2286 +
  1.2287 +bool gbReadBatteryFromStream(gzFile gzfile)
  1.2288 +{
  1.2289 +	// the GB save code is ugly, so rather than convert it all to use gzFiles, just copy it to temp RAM...
  1.2290 +#define TEMP_SAVE_FNAME ("tempvbaread.sav")
  1.2291 +	int	  pos	 = gztell(gzfile);
  1.2292 +	int	  buflen = 1024;
  1.2293 +	// ...make a temp file and write it there...
  1.2294 +	FILE *fileTemp = fopen(TEMP_SAVE_FNAME, "wb");
  1.2295 +	if (fileTemp == NULL)
  1.2296 +		return false;
  1.2297 +	int gzDeflated;
  1.2298 +	char *temp	 = new char [buflen];
  1.2299 +	while ((gzDeflated = utilGzRead(gzfile, temp, buflen)) != 0)
  1.2300 +	{
  1.2301 +		if (gzDeflated == -1 || fwrite(temp, gzDeflated, 1, fileTemp) != 1)
  1.2302 +		{
  1.2303 +			delete [] temp;
  1.2304 +			fclose(fileTemp);
  1.2305 +			gzseek(gzfile, pos, SEEK_SET); /// FIXME: leaves pos in gzfile before save instead of after it (everything that
  1.2306 +			                               // calls this right now does a seek afterwards so it doesn't matter for now, but it's
  1.2307 +			                               // still bad)
  1.2308 +			return false;
  1.2309 +		}
  1.2310 +	}
  1.2311 +	gzseek(gzfile, pos, SEEK_SET); /// FIXME: leaves pos in gzfile before save instead of after it (everything that calls this
  1.2312 +	                               // right now does a seek afterwards so it doesn't matter for now, but it's still bad)
  1.2313 +	fclose(fileTemp);
  1.2314 +	delete [] temp;
  1.2315 +
  1.2316 +	// ... load from the temp file...
  1.2317 +	bool retVal = gbReadBatteryFile(TEMP_SAVE_FNAME);
  1.2318 +
  1.2319 +	// ... and delete the temp file
  1.2320 +	remove(TEMP_SAVE_FNAME);
  1.2321 +#undef TEMP_SAVE_FNAME
  1.2322 +
  1.2323 +	return retVal;
  1.2324 +}
  1.2325 +
  1.2326 +bool gbReadGSASnapshot(const char *fileName)
  1.2327 +{
  1.2328 +	FILE *file = fopen(fileName, "rb");
  1.2329 +
  1.2330 +	if (!file)
  1.2331 +	{
  1.2332 +		systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName);
  1.2333 +		return false;
  1.2334 +	}
  1.2335 +
  1.2336 +	//  long size = ftell(file);
  1.2337 +	fseek(file, 0x4, SEEK_SET);
  1.2338 +	char buffer[16];
  1.2339 +	char buffer2[16];
  1.2340 +	fread(buffer, 1, 15, file);
  1.2341 +	buffer[15] = 0;
  1.2342 +	memcpy(buffer2, &gbRom[0x134], 15);
  1.2343 +	buffer2[15] = 0;
  1.2344 +	if (memcmp(buffer, buffer2, 15))
  1.2345 +	{
  1.2346 +		systemMessage(MSG_CANNOT_IMPORT_SNAPSHOT_FOR,
  1.2347 +		              N_("Cannot import snapshot for %s. Current game is %s"),
  1.2348 +		              buffer,
  1.2349 +		              buffer2);
  1.2350 +		fclose(file);
  1.2351 +		return false;
  1.2352 +	}
  1.2353 +	fseek(file, 0x13, SEEK_SET);
  1.2354 +	int read   = 0;
  1.2355 +	int toRead = 0;
  1.2356 +	switch (gbRom[0x147])
  1.2357 +	{
  1.2358 +	case 0x03:
  1.2359 +	case 0x0f:
  1.2360 +	case 0x10:
  1.2361 +	case 0x13:
  1.2362 +	case 0x1b:
  1.2363 +	case 0x1e:
  1.2364 +	case 0xff:
  1.2365 +		read   = fread(gbRam, 1, gbRamSize, file);
  1.2366 +		toRead = gbRamSize;
  1.2367 +		break;
  1.2368 +	case 0x06:
  1.2369 +	case 0x22:
  1.2370 +		read   = fread(&gbMemory[0xa000], 1, 256, file);
  1.2371 +		toRead = 256;
  1.2372 +		break;
  1.2373 +	default:
  1.2374 +		systemMessage(MSG_UNSUPPORTED_SNAPSHOT_FILE,
  1.2375 +		              N_("Unsupported snapshot file %s"),
  1.2376 +		              fileName);
  1.2377 +		fclose(file);
  1.2378 +		return false;
  1.2379 +	}
  1.2380 +	fclose(file);
  1.2381 +	gbReset();
  1.2382 +	return true;
  1.2383 +}
  1.2384 +
  1.2385 +variable_desc gbSaveGameStruct[] =
  1.2386 +{
  1.2387 +	{ &PC.W,					   sizeof(u16)						 },
  1.2388 +	{ &SP.W,					   sizeof(u16)						 },
  1.2389 +	{ &AF.W,					   sizeof(u16)						 },
  1.2390 +	{ &BC.W,					   sizeof(u16)						 },
  1.2391 +	{ &DE.W,					   sizeof(u16)						 },
  1.2392 +	{ &HL.W,					   sizeof(u16)						 },
  1.2393 +	{ &IFF,						   sizeof(u8)						 },
  1.2394 +	{ &GBLCD_MODE_0_CLOCK_TICKS,   sizeof(int32)					 },
  1.2395 +	{ &GBLCD_MODE_1_CLOCK_TICKS,   sizeof(int32)					 },
  1.2396 +	{ &GBLCD_MODE_2_CLOCK_TICKS,   sizeof(int32)					 },
  1.2397 +	{ &GBLCD_MODE_3_CLOCK_TICKS,   sizeof(int32)					 },
  1.2398 +	{ &GBDIV_CLOCK_TICKS,		   sizeof(int32)					 },
  1.2399 +	{ &GBLY_INCREMENT_CLOCK_TICKS, sizeof(int32)					 },
  1.2400 +	{ &GBTIMER_MODE_0_CLOCK_TICKS, sizeof(int32)					 },
  1.2401 +	{ &GBTIMER_MODE_1_CLOCK_TICKS, sizeof(int32)					 },
  1.2402 +	{ &GBTIMER_MODE_2_CLOCK_TICKS, sizeof(int32)					 },
  1.2403 +	{ &GBTIMER_MODE_3_CLOCK_TICKS, sizeof(int32)					 },
  1.2404 +	{ &GBSERIAL_CLOCK_TICKS,	   sizeof(int32)					 },
  1.2405 +	{ &GBSYNCHRONIZE_CLOCK_TICKS,  sizeof(int32)					 },
  1.2406 +	{ &gbDivTicks,				   sizeof(int32)					 },
  1.2407 +	{ &gbLcdMode,				   sizeof(int32)					 },
  1.2408 +	{ &gbLcdTicks,				   sizeof(int32)					 },
  1.2409 +	{ &gbLcdLYIncrementTicks,	   sizeof(int32)					 },
  1.2410 +	{ &gbTimerTicks,			   sizeof(int32)					 },
  1.2411 +	{ &gbTimerClockTicks,		   sizeof(int32)					 },
  1.2412 +	{ &gbSerialTicks,			   sizeof(int32)					 },
  1.2413 +	{ &gbSerialBits,			   sizeof(int32)					 },
  1.2414 +	{ &gbInterrupt,				   sizeof(int32)					 },
  1.2415 +	{ &gbInterruptWait,			   sizeof(int32)					 },
  1.2416 +	{ &gbSynchronizeTicks,		   sizeof(int32)					 },
  1.2417 +	{ &gbTimerOn,				   sizeof(int32)					 },
  1.2418 +	{ &gbTimerMode,				   sizeof(int32)					 },
  1.2419 +	{ &gbSerialOn,				   sizeof(int32)					 },
  1.2420 +	{ &gbWindowLine,			   sizeof(int32)					 },
  1.2421 +	{ &gbCgbMode,				   sizeof(int32)					 },
  1.2422 +	{ &gbVramBank,				   sizeof(int32)					 },
  1.2423 +	{ &gbWramBank,				   sizeof(int32)					 },
  1.2424 +	{ &gbHdmaSource,			   sizeof(int32)					 },
  1.2425 +	{ &gbHdmaDestination,		   sizeof(int32)					 },
  1.2426 +	{ &gbHdmaBytes,				   sizeof(int32)					 },
  1.2427 +	{ &gbHdmaOn,				   sizeof(int32)					 },
  1.2428 +	{ &gbSpeed,					   sizeof(int32)					 },
  1.2429 +	{ &gbSgbMode,				   sizeof(int32)					 },
  1.2430 +	{ &register_DIV,			   sizeof(u8)						 },
  1.2431 +	{ &register_TIMA,			   sizeof(u8)						 },
  1.2432 +	{ &register_TMA,			   sizeof(u8)						 },
  1.2433 +	{ &register_TAC,			   sizeof(u8)						 },
  1.2434 +	{ &register_IF,				   sizeof(u8)						 },
  1.2435 +	{ &register_LCDC,			   sizeof(u8)						 },
  1.2436 +	{ &register_STAT,			   sizeof(u8)						 },
  1.2437 +	{ &register_SCY,			   sizeof(u8)						 },
  1.2438 +	{ &register_SCX,			   sizeof(u8)						 },
  1.2439 +	{ &register_LY,				   sizeof(u8)						 },
  1.2440 +	{ &register_LYC,			   sizeof(u8)						 },
  1.2441 +	{ &register_DMA,			   sizeof(u8)						 },
  1.2442 +	{ &register_WY,				   sizeof(u8)						 },
  1.2443 +	{ &register_WX,				   sizeof(u8)						 },
  1.2444 +	{ &register_VBK,			   sizeof(u8)						 },
  1.2445 +	{ &register_HDMA1,			   sizeof(u8)						 },
  1.2446 +	{ &register_HDMA2,			   sizeof(u8)						 },
  1.2447 +	{ &register_HDMA3,			   sizeof(u8)						 },
  1.2448 +	{ &register_HDMA4,			   sizeof(u8)						 },
  1.2449 +	{ &register_HDMA5,			   sizeof(u8)						 },
  1.2450 +	{ &register_SVBK,			   sizeof(u8)						 },
  1.2451 +	{ &register_IE,				   sizeof(u8)						 },
  1.2452 +	{ &gbBgp[0],				   sizeof(u8)						 },
  1.2453 +	{ &gbBgp[1],				   sizeof(u8)						 },
  1.2454 +	{ &gbBgp[2],				   sizeof(u8)						 },
  1.2455 +	{ &gbBgp[3],				   sizeof(u8)						 },
  1.2456 +	{ &gbObp0[0],				   sizeof(u8)						 },
  1.2457 +	{ &gbObp0[1],				   sizeof(u8)						 },
  1.2458 +	{ &gbObp0[2],				   sizeof(u8)						 },
  1.2459 +	{ &gbObp0[3],				   sizeof(u8)						 },
  1.2460 +	{ &gbObp1[0],				   sizeof(u8)						 },
  1.2461 +	{ &gbObp1[1],				   sizeof(u8)						 },
  1.2462 +	{ &gbObp1[2],				   sizeof(u8)						 },
  1.2463 +	{ &gbObp1[3],				   sizeof(u8)						 },
  1.2464 +	{ NULL,						   0								 }
  1.2465 +};
  1.2466 +
  1.2467 +bool gbWriteSaveStateToStream(gzFile gzFile)
  1.2468 +{
  1.2469 +	utilWriteInt(gzFile, GBSAVE_GAME_VERSION);
  1.2470 +
  1.2471 +	utilGzWrite(gzFile, &gbRom[0x134], 15);
  1.2472 +
  1.2473 +	utilWriteData(gzFile, gbSaveGameStruct);
  1.2474 +
  1.2475 +	utilGzWrite(gzFile, &IFF, 2);
  1.2476 +
  1.2477 +	if (gbSgbMode)
  1.2478 +	{
  1.2479 +		gbSgbSaveGame(gzFile);
  1.2480 +	}
  1.2481 +
  1.2482 +	utilGzWrite(gzFile, &gbDataMBC1, sizeof(gbDataMBC1));
  1.2483 +	utilGzWrite(gzFile, &gbDataMBC2, sizeof(gbDataMBC2));
  1.2484 +	//assert(sizeof(time_t) == 4);
  1.2485 +	utilGzWrite(gzFile, &gbDataMBC3, sizeof(gbDataMBC3));
  1.2486 +	utilGzWrite(gzFile, &gbDataMBC5, sizeof(gbDataMBC5));
  1.2487 +	utilGzWrite(gzFile, &gbDataHuC1, sizeof(gbDataHuC1));
  1.2488 +	utilGzWrite(gzFile, &gbDataHuC3, sizeof(gbDataHuC3));
  1.2489 +
  1.2490 +	// yes, this definitely needs to be saved, or loading paused games will show a black screen
  1.2491 +	// this is also necessary to be consistent with what the GBA saving does
  1.2492 +	utilGzWrite(gzFile, pix, 4 * 257 * 226);
  1.2493 +
  1.2494 +	utilGzWrite(gzFile, gbPalette, 128 * sizeof(u16));
  1.2495 +	// todo: remove
  1.2496 +	utilGzWrite(gzFile, gbPalette, 128 * sizeof(u16));
  1.2497 +
  1.2498 +	utilGzWrite(gzFile, &gbMemory[0x8000], 0x8000);
  1.2499 +
  1.2500 +	if (gbRamSize && gbRam)
  1.2501 +	{
  1.2502 +		utilGzWrite(gzFile, gbRam, gbRamSize);
  1.2503 +	}
  1.2504 +
  1.2505 +	if (gbCgbMode)
  1.2506 +	{
  1.2507 +		utilGzWrite(gzFile, gbVram, 0x4000);
  1.2508 +		utilGzWrite(gzFile, gbWram, 0x8000);
  1.2509 +	}
  1.2510 +
  1.2511 +	gbSoundSaveGame(gzFile);
  1.2512 +
  1.2513 +	gbCheatsSaveGame(gzFile);
  1.2514 +
  1.2515 +	// new to re-recording version:
  1.2516 +	{
  1.2517 +		extern int32 sensorX, sensorY;
  1.2518 +		utilGzWrite(gzFile, &sensorX, sizeof(sensorX));
  1.2519 +		utilGzWrite(gzFile, &sensorY, sizeof(sensorY));
  1.2520 +		utilGzWrite(gzFile, gbJoymask, 4 * sizeof(*gbJoymask)); // this has to be saved or old input will incorrectly get
  1.2521 +		                                                        // carried
  1.2522 +		                                                        // back on loading a snapshot!
  1.2523 +
  1.2524 +		bool8 movieActive = VBAMovieActive();
  1.2525 +		utilGzWrite(gzFile, &movieActive, sizeof(movieActive));
  1.2526 +		if (movieActive)
  1.2527 +		{
  1.2528 +			uint8 *movie_freeze_buf	 = NULL;
  1.2529 +			uint32 movie_freeze_size = 0;
  1.2530 +
  1.2531 +			VBAMovieFreeze(&movie_freeze_buf, &movie_freeze_size);
  1.2532 +			if (movie_freeze_buf)
  1.2533 +			{
  1.2534 +				utilGzWrite(gzFile, &movie_freeze_size, sizeof(movie_freeze_size));
  1.2535 +				utilGzWrite(gzFile, movie_freeze_buf, movie_freeze_size);
  1.2536 +				delete [] movie_freeze_buf;
  1.2537 +			}
  1.2538 +			else
  1.2539 +			{
  1.2540 +				systemMessage(0, N_("Failed to save movie snapshot."));
  1.2541 +				return false;
  1.2542 +			}
  1.2543 +		}
  1.2544 +		utilGzWrite(gzFile, &GBSystemCounters.frameCount, sizeof(GBSystemCounters.frameCount));
  1.2545 +	}
  1.2546 +
  1.2547 +	// new to rerecording 19.4 wip (svn r22+):
  1.2548 +	{
  1.2549 +		utilGzWrite(gzFile, &GBSystemCounters.lagCount, sizeof(GBSystemCounters.lagCount));
  1.2550 +		utilGzWrite(gzFile, &GBSystemCounters.lagged, sizeof(GBSystemCounters.lagged));
  1.2551 +		utilGzWrite(gzFile, &GBSystemCounters.laggedLast, sizeof(GBSystemCounters.laggedLast));
  1.2552 +	}
  1.2553 +
  1.2554 +	return true;
  1.2555 +}
  1.2556 +
  1.2557 +bool gbWriteMemSaveState(char *memory, int available)
  1.2558 +{
  1.2559 +	gzFile gzFile = utilMemGzOpen(memory, available, "w");
  1.2560 +
  1.2561 +	if (gzFile == NULL)
  1.2562 +	{
  1.2563 +		return false;
  1.2564 +	}
  1.2565 +
  1.2566 +	bool res = gbWriteSaveStateToStream(gzFile);
  1.2567 +
  1.2568 +	long pos = utilGzTell(gzFile) + 8;
  1.2569 +
  1.2570 +	if (pos >= (available))
  1.2571 +		res = false;
  1.2572 +
  1.2573 +	utilGzClose(gzFile);
  1.2574 +
  1.2575 +	return res;
  1.2576 +}
  1.2577 +
  1.2578 +bool gbWriteSaveState(const char *name)
  1.2579 +{
  1.2580 +	gzFile gzFile = utilGzOpen(name, "wb");
  1.2581 +
  1.2582 +	if (gzFile == NULL)
  1.2583 +		return false;
  1.2584 +
  1.2585 +	bool res = gbWriteSaveStateToStream(gzFile);
  1.2586 +
  1.2587 +	utilGzClose(gzFile);
  1.2588 +	return res;
  1.2589 +}
  1.2590 +
  1.2591 +static int	tempStateID	  = 0;
  1.2592 +static int	tempFailCount = 0;
  1.2593 +static bool backupSafe	  = true;
  1.2594 +
  1.2595 +bool gbReadSaveStateFromStream(gzFile gzFile)
  1.2596 +{
  1.2597 +	int	 type;
  1.2598 +	char tempBackupName [128];
  1.2599 +	if (backupSafe)
  1.2600 +	{
  1.2601 +		sprintf(tempBackupName, "gbatempsave%d.sav", tempStateID++);
  1.2602 +		gbWriteSaveState(tempBackupName);
  1.2603 +	}
  1.2604 +
  1.2605 +	int version = utilReadInt(gzFile);
  1.2606 +
  1.2607 +	if (version > GBSAVE_GAME_VERSION || version < 0)
  1.2608 +	{
  1.2609 +		systemMessage(MSG_UNSUPPORTED_VB_SGM,
  1.2610 +		              N_("Unsupported VisualBoy save game version %d"), version);
  1.2611 +		goto failedLoadGB;
  1.2612 +	}
  1.2613 +
  1.2614 +	u8 romname[20];
  1.2615 +
  1.2616 +	utilGzRead(gzFile, romname, 15);
  1.2617 +
  1.2618 +	if (memcmp(&gbRom[0x134], romname, 15) != 0)
  1.2619 +	{
  1.2620 +		systemMessage(MSG_CANNOT_LOAD_SGM_FOR,
  1.2621 +		              N_("Cannot load save game for %s. Playing %s"),
  1.2622 +		              romname, &gbRom[0x134]);
  1.2623 +		goto failedLoadGB;
  1.2624 +	}
  1.2625 +
  1.2626 +	utilReadData(gzFile, gbSaveGameStruct);
  1.2627 +
  1.2628 +	if (version >= GBSAVE_GAME_VERSION_7)
  1.2629 +	{
  1.2630 +		utilGzRead(gzFile, &IFF, 2);
  1.2631 +	}
  1.2632 +
  1.2633 +	if (gbSgbMode)
  1.2634 +	{
  1.2635 +		gbSgbReadGame(gzFile, version);
  1.2636 +	}
  1.2637 +	else
  1.2638 +	{
  1.2639 +		gbSgbMask = 0; // loading a game at the wrong time causes no display
  1.2640 +	}
  1.2641 +
  1.2642 +	utilGzRead(gzFile, &gbDataMBC1, sizeof(gbDataMBC1));
  1.2643 +	utilGzRead(gzFile, &gbDataMBC2, sizeof(gbDataMBC2));
  1.2644 +	if (version < GBSAVE_GAME_VERSION_4)
  1.2645 +		// prior to version 4, there was no adjustment for the time the game
  1.2646 +		// was last played, so we have less to read. This needs update if the
  1.2647 +		// structure changes again.
  1.2648 +		utilGzRead(gzFile, &gbDataMBC3, sizeof(int32) * 10);
  1.2649 +	else
  1.2650 +	{
  1.2651 +		//assert(sizeof(time_t) == 4);
  1.2652 +		utilGzRead(gzFile, &gbDataMBC3, sizeof(gbDataMBC3));
  1.2653 +	}
  1.2654 +	utilGzRead(gzFile, &gbDataMBC5, sizeof(gbDataMBC5));
  1.2655 +	utilGzRead(gzFile, &gbDataHuC1, sizeof(gbDataHuC1));
  1.2656 +	utilGzRead(gzFile, &gbDataHuC3, sizeof(gbDataHuC3));
  1.2657 +
  1.2658 +	if (version >= GBSAVE_GAME_VERSION_12)
  1.2659 +	{
  1.2660 +		utilGzRead(gzFile, pix, 4 * 257 * 226);
  1.2661 +	}
  1.2662 +	else
  1.2663 +	{
  1.2664 +		memset(pix, 0, 257 * 226 * sizeof(u32));
  1.2665 +//		if(version < GBSAVE_GAME_VERSION_5)
  1.2666 +//			utilGzRead(gzFile, pix, 256*224*sizeof(u16));
  1.2667 +	}
  1.2668 +
  1.2669 +	if (version < GBSAVE_GAME_VERSION_6)
  1.2670 +	{
  1.2671 +		utilGzRead(gzFile, gbPalette, 64 * sizeof(u16));
  1.2672 +	}
  1.2673 +	else
  1.2674 +		utilGzRead(gzFile, gbPalette, 128 * sizeof(u16));
  1.2675 +
  1.2676 +	// todo: remove
  1.2677 +	utilGzRead(gzFile, gbPalette, 128 * sizeof(u16));
  1.2678 +
  1.2679 +	if (version < GBSAVE_GAME_VERSION_10)
  1.2680 +	{
  1.2681 +		if (!gbCgbMode && !gbSgbMode)
  1.2682 +		{
  1.2683 +			for (int i = 0; i < 8; i++)
  1.2684 +				gbPalette[i] = systemGbPalette[gbPaletteOption * 8 + i];
  1.2685 +		}
  1.2686 +	}
  1.2687 +
  1.2688 +	utilGzRead(gzFile, &gbMemory[0x8000], 0x8000);
  1.2689 +
  1.2690 +	if (gbRamSize && gbRam)
  1.2691 +	{
  1.2692 +		utilGzRead(gzFile, gbRam, gbRamSize);
  1.2693 +	}
  1.2694 +
  1.2695 +	gbMemoryMap[0x00] = &gbRom[0x0000];
  1.2696 +	gbMemoryMap[0x01] = &gbRom[0x1000];
  1.2697 +	gbMemoryMap[0x02] = &gbRom[0x2000];
  1.2698 +	gbMemoryMap[0x03] = &gbRom[0x3000];
  1.2699 +	gbMemoryMap[0x04] = &gbRom[0x4000];
  1.2700 +	gbMemoryMap[0x05] = &gbRom[0x5000];
  1.2701 +	gbMemoryMap[0x06] = &gbRom[0x6000];
  1.2702 +	gbMemoryMap[0x07] = &gbRom[0x7000];
  1.2703 +	gbMemoryMap[0x08] = &gbMemory[0x8000];
  1.2704 +	gbMemoryMap[0x09] = &gbMemory[0x9000];
  1.2705 +	gbMemoryMap[0x0a] = &gbMemory[0xa000];
  1.2706 +	gbMemoryMap[0x0b] = &gbMemory[0xb000];
  1.2707 +	gbMemoryMap[0x0c] = &gbMemory[0xc000];
  1.2708 +	gbMemoryMap[0x0d] = &gbMemory[0xd000];
  1.2709 +	gbMemoryMap[0x0e] = &gbMemory[0xe000];
  1.2710 +	gbMemoryMap[0x0f] = &gbMemory[0xf000];
  1.2711 +
  1.2712 +	type = gbRom[0x147];
  1.2713 +
  1.2714 +	switch (type)
  1.2715 +	{
  1.2716 +	case 0x00:
  1.2717 +	case 0x01:
  1.2718 +	case 0x02:
  1.2719 +	case 0x03:
  1.2720 +		// MBC 1
  1.2721 +		memoryUpdateMapMBC1();
  1.2722 +		break;
  1.2723 +	case 0x05:
  1.2724 +	case 0x06:
  1.2725 +		// MBC2
  1.2726 +		memoryUpdateMapMBC2();
  1.2727 +		break;
  1.2728 +	case 0x0f:
  1.2729 +	case 0x10:
  1.2730 +	case 0x11:
  1.2731 +	case 0x12:
  1.2732 +	case 0x13:
  1.2733 +		// MBC 3
  1.2734 +		memoryUpdateMapMBC3();
  1.2735 +		break;
  1.2736 +	case 0x19:
  1.2737 +	case 0x1a:
  1.2738 +	case 0x1b:
  1.2739 +		// MBC5
  1.2740 +		memoryUpdateMapMBC5();
  1.2741 +		break;
  1.2742 +	case 0x1c:
  1.2743 +	case 0x1d:
  1.2744 +	case 0x1e:
  1.2745 +		// MBC 5 Rumble
  1.2746 +		memoryUpdateMapMBC5();
  1.2747 +		break;
  1.2748 +	case 0x22:
  1.2749 +		// MBC 7
  1.2750 +		memoryUpdateMapMBC7();
  1.2751 +		break;
  1.2752 +	case 0xfe:
  1.2753 +		// HuC3
  1.2754 +		memoryUpdateMapHuC3();
  1.2755 +		break;
  1.2756 +	case 0xff:
  1.2757 +		// HuC1
  1.2758 +		memoryUpdateMapHuC1();
  1.2759 +		break;
  1.2760 +	}
  1.2761 +
  1.2762 +	if (gbCgbMode)
  1.2763 +	{
  1.2764 +		if (!gbVram)
  1.2765 +			gbVram = (u8 *)malloc(0x4000 + 4);
  1.2766 +		if (!gbWram)
  1.2767 +			gbWram = (u8 *)malloc(0x8000 + 4);
  1.2768 +		utilGzRead(gzFile, gbVram, 0x4000);
  1.2769 +		utilGzRead(gzFile, gbWram, 0x8000);
  1.2770 +
  1.2771 +		int value = register_SVBK;
  1.2772 +		if (value == 0)
  1.2773 +			value = 1;
  1.2774 +
  1.2775 +		gbMemoryMap[0x08] = &gbVram[register_VBK * 0x2000];
  1.2776 +		gbMemoryMap[0x09] = &gbVram[register_VBK * 0x2000 + 0x1000];
  1.2777 +		gbMemoryMap[0x0d] = &gbWram[value * 0x1000];
  1.2778 +	}
  1.2779 +	else
  1.2780 +	{
  1.2781 +		if (gbVram)
  1.2782 +		{
  1.2783 +			free(gbVram);
  1.2784 +			gbVram = NULL;
  1.2785 +		}
  1.2786 +		if (gbWram)
  1.2787 +		{
  1.2788 +			free(gbWram);
  1.2789 +			gbWram = NULL;
  1.2790 +		}
  1.2791 +	}
  1.2792 +
  1.2793 +	gbSoundReadGame(version, gzFile);
  1.2794 +
  1.2795 +#if 0
  1.2796 +	if (gbBorderOn)
  1.2797 +	{
  1.2798 +		gbSgbRenderBorder();
  1.2799 +	}
  1.2800 +
  1.2801 +	systemRefreshScreen();
  1.2802 +#endif
  1.2803 +
  1.2804 +	if (version > GBSAVE_GAME_VERSION_1)
  1.2805 +		gbCheatsReadGame(gzFile, version);
  1.2806 +
  1.2807 +	systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
  1.2808 +
  1.2809 +	if (version >= GBSAVE_GAME_VERSION_11) // new to re-recording version:
  1.2810 +	{
  1.2811 +		extern int32 sensorX, sensorY; // from SDL.cpp
  1.2812 +		utilGzRead(gzFile, &sensorX, sizeof(sensorX));
  1.2813 +		utilGzRead(gzFile, &sensorY, sizeof(sensorY));
  1.2814 +		utilGzRead(gzFile, gbJoymask, 4 * sizeof(*gbJoymask)); // this has to be saved or old input will incorrectly get carried
  1.2815 +		                                                       // back on loading a snapshot!
  1.2816 +
  1.2817 +		bool8 movieSnapshot;
  1.2818 +		utilGzRead(gzFile, &movieSnapshot, sizeof(movieSnapshot));
  1.2819 +		if (VBAMovieActive() && !movieSnapshot)
  1.2820 +		{
  1.2821 +			systemMessage(0, N_("Can't load a non-movie snapshot while a movie is active."));
  1.2822 +			goto failedLoadGB;
  1.2823 +		}
  1.2824 +
  1.2825 +		if (movieSnapshot) // even if a movie isn't active we still want to parse through this in case other stuff is added
  1.2826 +		                   // later on in the save format
  1.2827 +		{
  1.2828 +			uint32 movieInputDataSize = 0;
  1.2829 +			utilGzRead(gzFile, &movieInputDataSize, sizeof(movieInputDataSize));
  1.2830 +			uint8 *local_movie_data = new uint8 [movieInputDataSize];
  1.2831 +			int	   readBytes		= utilGzRead(gzFile, local_movie_data, movieInputDataSize);
  1.2832 +			if (readBytes != movieInputDataSize)
  1.2833 +			{
  1.2834 +				systemMessage(0, N_("Corrupt movie snapshot."));
  1.2835 +				if (local_movie_data)
  1.2836 +					delete [] local_movie_data;
  1.2837 +				goto failedLoadGB;
  1.2838 +			}
  1.2839 +			int code = VBAMovieUnfreeze(local_movie_data, movieInputDataSize);
  1.2840 +			if (local_movie_data)
  1.2841 +				delete [] local_movie_data;
  1.2842 +			if (code != MOVIE_SUCCESS && VBAMovieActive())
  1.2843 +			{
  1.2844 +				char errStr [1024];
  1.2845 +				strcpy(errStr, "Failed to load movie snapshot");
  1.2846 +				switch (code)
  1.2847 +				{
  1.2848 +				case MOVIE_NOT_FROM_THIS_MOVIE:
  1.2849 +					strcat(errStr, ";\nSnapshot not from this movie"); break;
  1.2850 +				case MOVIE_NOT_FROM_A_MOVIE:
  1.2851 +					strcat(errStr, ";\nNot a movie snapshot"); break;                    // shouldn't get here...
  1.2852 +				case MOVIE_SNAPSHOT_INCONSISTENT:
  1.2853 +					strcat(errStr, ";\nSnapshot inconsistent with movie"); break;
  1.2854 +				case MOVIE_WRONG_FORMAT:
  1.2855 +					strcat(errStr, ";\nWrong format"); break;
  1.2856 +				}
  1.2857 +				strcat(errStr, ".");
  1.2858 +				systemMessage(0, N_(errStr));
  1.2859 +				goto failedLoadGB;
  1.2860 +			}
  1.2861 +		}
  1.2862 +		utilGzRead(gzFile, &GBSystemCounters.frameCount, sizeof(GBSystemCounters.frameCount));
  1.2863 +	}
  1.2864 +
  1.2865 +	if (version >= GBSAVE_GAME_VERSION_13)   // new to rerecording 19.4 wip (svn r22+):
  1.2866 +	{
  1.2867 +		utilGzRead(gzFile, &GBSystemCounters.lagCount, sizeof(GBSystemCounters.lagCount));
  1.2868 +		utilGzRead(gzFile, &GBSystemCounters.lagged, sizeof(GBSystemCounters.lagged));
  1.2869 +		utilGzRead(gzFile, &GBSystemCounters.laggedLast, sizeof(GBSystemCounters.laggedLast));
  1.2870 +	}
  1.2871 +
  1.2872 +	if (backupSafe)
  1.2873 +	{
  1.2874 +		remove(tempBackupName);
  1.2875 +		tempFailCount = 0;
  1.2876 +	}
  1.2877 +
  1.2878 +	for (int i = 0; i < 4; ++i)
  1.2879 +		systemSetJoypad(i, gbJoymask[i] & 0xFFFF);
  1.2880 +
  1.2881 +	// FIXME: horrible kludge
  1.2882 +	memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask));
  1.2883 +
  1.2884 +	VBAUpdateButtonPressDisplay();
  1.2885 +	VBAUpdateFrameCountDisplay();
  1.2886 +	systemRefreshScreen();
  1.2887 +	return true;
  1.2888 +
  1.2889 +failedLoadGB:
  1.2890 +	if (backupSafe)
  1.2891 +	{
  1.2892 +		tempFailCount++;
  1.2893 +		if (tempFailCount < 3) // fail no more than 2 times in a row
  1.2894 +			gbReadSaveState(tempBackupName);
  1.2895 +		remove(tempBackupName);
  1.2896 +	}
  1.2897 +	return false;
  1.2898 +}
  1.2899 +
  1.2900 +bool gbReadMemSaveState(char *memory, int available)
  1.2901 +{
  1.2902 +	gzFile gzFile = utilMemGzOpen(memory, available, "r");
  1.2903 +
  1.2904 +	backupSafe = false;
  1.2905 +	bool res = gbReadSaveStateFromStream(gzFile);
  1.2906 +	backupSafe = true;
  1.2907 +
  1.2908 +	utilGzClose(gzFile);
  1.2909 +
  1.2910 +	return res;
  1.2911 +}
  1.2912 +
  1.2913 +bool gbReadSaveState(const char *name)
  1.2914 +{
  1.2915 +	gzFile gzFile = utilGzOpen(name, "rb");
  1.2916 +
  1.2917 +	if (gzFile == NULL)
  1.2918 +	{
  1.2919 +		return false;
  1.2920 +	}
  1.2921 +
  1.2922 +	bool res = gbReadSaveStateFromStream(gzFile);
  1.2923 +
  1.2924 +	utilGzClose(gzFile);
  1.2925 +
  1.2926 +	return res;
  1.2927 +}
  1.2928 +
  1.2929 +bool gbWritePNGFile(const char *fileName)
  1.2930 +{
  1.2931 +	if (gbBorderOn)
  1.2932 +		return utilWritePNGFile(fileName, 256, 224, pix);
  1.2933 +	return utilWritePNGFile(fileName, 160, 144, pix);
  1.2934 +}
  1.2935 +
  1.2936 +bool gbWriteBMPFile(const char *fileName)
  1.2937 +{
  1.2938 +	if (gbBorderOn)
  1.2939 +		return utilWriteBMPFile(fileName, 256, 224, pix);
  1.2940 +	return utilWriteBMPFile(fileName, 160, 144, pix);
  1.2941 +}
  1.2942 +
  1.2943 +void gbCleanUp()
  1.2944 +{
  1.2945 +	newFrame	  = true;
  1.2946 +
  1.2947 +	GBSystemCounters.frameCount = 0;
  1.2948 +	GBSystemCounters.lagCount	= 0;
  1.2949 +	GBSystemCounters.extraCount = 0;
  1.2950 +	GBSystemCounters.lagged		= true;
  1.2951 +	GBSystemCounters.laggedLast = true;
  1.2952 +
  1.2953 +	if (gbRam != NULL)
  1.2954 +	{
  1.2955 +		free(gbRam);
  1.2956 +		gbRam = NULL;
  1.2957 +	}
  1.2958 +
  1.2959 +	if (gbRom != NULL)
  1.2960 +	{
  1.2961 +		free(gbRom);
  1.2962 +		gbRom = NULL;
  1.2963 +	}
  1.2964 +
  1.2965 +	if (gbMemory != NULL)
  1.2966 +	{
  1.2967 +		free(gbMemory);
  1.2968 +		gbMemory = NULL;
  1.2969 +	}
  1.2970 +
  1.2971 +	if (gbLineBuffer != NULL)
  1.2972 +	{
  1.2973 +		free(gbLineBuffer);
  1.2974 +		gbLineBuffer = NULL;
  1.2975 +	}
  1.2976 +
  1.2977 +	if (origPix != NULL)
  1.2978 +	{
  1.2979 +		free(origPix);
  1.2980 +		origPix = NULL;
  1.2981 +	}
  1.2982 +	pix = NULL;
  1.2983 +
  1.2984 +	gbSgbShutdown();
  1.2985 +
  1.2986 +	if (gbVram != NULL)
  1.2987 +	{
  1.2988 +		free(gbVram);
  1.2989 +		gbVram = NULL;
  1.2990 +	}
  1.2991 +
  1.2992 +	if (gbWram != NULL)
  1.2993 +	{
  1.2994 +		free(gbWram);
  1.2995 +		gbWram = NULL;
  1.2996 +	}
  1.2997 +
  1.2998 +	systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
  1.2999 +
  1.3000 +	memset(gbJoymask, 0, sizeof(gbJoymask));
  1.3001 +	// FIXME: horrible kludge
  1.3002 +	memset(s_gbJoymask, 0, sizeof(s_gbJoymask));
  1.3003 +
  1.3004 +	systemClearJoypads();
  1.3005 +	systemResetSensor();
  1.3006 +
  1.3007 +//	gbLastTime = gbFrameCount = 0;
  1.3008 +	systemRefreshScreen();
  1.3009 +}
  1.3010 +
  1.3011 +bool gbLoadRom(const char *szFile)
  1.3012 +{
  1.3013 +	int size = 0;
  1.3014 +
  1.3015 +	if (gbRom != NULL)
  1.3016 +	{
  1.3017 +		gbCleanUp();
  1.3018 +	}
  1.3019 +
  1.3020 +	systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
  1.3021 +
  1.3022 +	gbRom = utilLoad(szFile,
  1.3023 +	                 utilIsGBImage,
  1.3024 +	                 NULL,
  1.3025 +	                 size);
  1.3026 +	if (!gbRom)
  1.3027 +		return false;
  1.3028 +
  1.3029 +	gbRomSize = size;
  1.3030 +
  1.3031 +	return gbUpdateSizes();
  1.3032 +}
  1.3033 +
  1.3034 +bool gbUpdateSizes()
  1.3035 +{
  1.3036 +	if (gbRom[0x148] > 8)
  1.3037 +	{
  1.3038 +		systemMessage(MSG_UNSUPPORTED_ROM_SIZE,
  1.3039 +		              N_("Unsupported rom size %02x"), gbRom[0x148]);
  1.3040 +		return false;
  1.3041 +	}
  1.3042 +
  1.3043 +	if (gbRomSize < gbRomSizes[gbRom[0x148]])
  1.3044 +	{
  1.3045 +		gbRom = (u8 *)realloc(gbRom, gbRomSizes[gbRom[0x148]]);
  1.3046 +	}
  1.3047 +	gbRomSize	  = gbRomSizes[gbRom[0x148]];
  1.3048 +	gbRomSizeMask = gbRomSizesMasks[gbRom[0x148]];
  1.3049 +
  1.3050 +	if (gbRom[0x149] > 5)
  1.3051 +	{
  1.3052 +		systemMessage(MSG_UNSUPPORTED_RAM_SIZE,
  1.3053 +		              N_("Unsupported ram size %02x"), gbRom[0x149]);
  1.3054 +		return false;
  1.3055 +	}
  1.3056 +
  1.3057 +	gbRamSize	  = gbRamSizes[gbRom[0x149]];
  1.3058 +	gbRamSizeMask = gbRamSizesMasks[gbRom[0x149]];
  1.3059 +
  1.3060 +	if (gbRamSize)
  1.3061 +	{
  1.3062 +		gbRam = (u8 *)malloc(gbRamSize + 4);
  1.3063 +		memset(gbRam, 0xFF, gbRamSize + 4);
  1.3064 +	}
  1.3065 +
  1.3066 +	int type = gbRom[0x147];
  1.3067 +
  1.3068 +	mapperReadRAM = NULL;
  1.3069 +
  1.3070 +	switch (type)
  1.3071 +	{
  1.3072 +	case 0x00:
  1.3073 +	case 0x01:
  1.3074 +	case 0x02:
  1.3075 +	case 0x03:
  1.3076 +		// MBC 1
  1.3077 +		mapper	  = mapperMBC1ROM;
  1.3078 +		mapperRAM = mapperMBC1RAM;
  1.3079 +		break;
  1.3080 +	case 0x05:
  1.3081 +	case 0x06:
  1.3082 +		// MBC2
  1.3083 +		mapper		  = mapperMBC2ROM;
  1.3084 +		mapperRAM	  = mapperMBC2RAM;
  1.3085 +		gbRamSize	  = 0x200;
  1.3086 +		gbRamSizeMask = 0x1ff;
  1.3087 +		break;
  1.3088 +	case 0x0f:
  1.3089 +	case 0x10:
  1.3090 +	case 0x11:
  1.3091 +	case 0x12:
  1.3092 +	case 0x13:
  1.3093 +		// MBC 3
  1.3094 +		mapper		  = mapperMBC3ROM;
  1.3095 +		mapperRAM	  = mapperMBC3RAM;
  1.3096 +		mapperReadRAM = mapperMBC3ReadRAM;
  1.3097 +		break;
  1.3098 +	case 0x19:
  1.3099 +	case 0x1a:
  1.3100 +	case 0x1b:
  1.3101 +		// MBC5
  1.3102 +		mapper	  = mapperMBC5ROM;
  1.3103 +		mapperRAM = mapperMBC5RAM;
  1.3104 +		break;
  1.3105 +	case 0x1c:
  1.3106 +	case 0x1d:
  1.3107 +	case 0x1e:
  1.3108 +		// MBC 5 Rumble
  1.3109 +		mapper	  = mapperMBC5ROM;
  1.3110 +		mapperRAM = mapperMBC5RAM;
  1.3111 +		break;
  1.3112 +	case 0x22:
  1.3113 +		// MBC 7
  1.3114 +		mapper		  = mapperMBC7ROM;
  1.3115 +		mapperRAM	  = mapperMBC7RAM;
  1.3116 +		mapperReadRAM = mapperMBC7ReadRAM;
  1.3117 +		break;
  1.3118 +	case 0xfe:
  1.3119 +		// HuC3
  1.3120 +		mapper		  = mapperHuC3ROM;
  1.3121 +		mapperRAM	  = mapperHuC3RAM;
  1.3122 +		mapperReadRAM = mapperHuC3ReadRAM;
  1.3123 +		break;
  1.3124 +	case 0xff:
  1.3125 +		// HuC1
  1.3126 +		mapper	  = mapperHuC1ROM;
  1.3127 +		mapperRAM = mapperHuC1RAM;
  1.3128 +		break;
  1.3129 +	default:
  1.3130 +		systemMessage(MSG_UNKNOWN_CARTRIDGE_TYPE,
  1.3131 +		              N_("Unknown cartridge type %02x"), type);
  1.3132 +		return false;
  1.3133 +	}
  1.3134 +
  1.3135 +	switch (type)
  1.3136 +	{
  1.3137 +	case 0x03:
  1.3138 +	case 0x06:
  1.3139 +	case 0x0f:
  1.3140 +	case 0x10:
  1.3141 +	case 0x13:
  1.3142 +	case 0x1b:
  1.3143 +	case 0x1d:
  1.3144 +	case 0x1e:
  1.3145 +	case 0x22:
  1.3146 +	case 0xff:
  1.3147 +		gbBattery = 1;
  1.3148 +		break;
  1.3149 +	}
  1.3150 +
  1.3151 +	gbInit();
  1.3152 +	gbReset();
  1.3153 +
  1.3154 +	return true;
  1.3155 +}
  1.3156 +
  1.3157 +void gbEmulate(int ticksToStop)
  1.3158 +{
  1.3159 +	gbRegister tempRegister;
  1.3160 +	u8		   tempValue;
  1.3161 +	s8		   offset;
  1.3162 +
  1.3163 +	int clockTicks = 0;
  1.3164 +	gbDmaTicks = 0;
  1.3165 +
  1.3166 +	register int opcode = 0;
  1.3167 +
  1.3168 +	u32 newmask = 0;
  1.3169 +	if (newFrame)
  1.3170 +	{
  1.3171 +		extern void VBAOnExitingFrameBoundary();
  1.3172 +		VBAOnExitingFrameBoundary();
  1.3173 +
  1.3174 +		// update joystick information
  1.3175 +		systemReadJoypads();
  1.3176 +
  1.3177 +		bool sensor = (gbRom[0x147] == 0x22);
  1.3178 +
  1.3179 +		// read joystick
  1.3180 +		if (gbSgbMode && gbSgbMultiplayer)
  1.3181 +		{
  1.3182 +			if (gbSgbFourPlayers)
  1.3183 +			{
  1.3184 +				gbJoymask[0] = systemGetJoypad(0, sensor);
  1.3185 +				gbJoymask[1] = systemGetJoypad(1, false);
  1.3186 +				gbJoymask[2] = systemGetJoypad(2, false);
  1.3187 +				gbJoymask[3] = systemGetJoypad(3, false);
  1.3188 +			}
  1.3189 +			else
  1.3190 +			{
  1.3191 +				gbJoymask[0] = systemGetJoypad(0, sensor);
  1.3192 +				gbJoymask[1] = systemGetJoypad(1, false);
  1.3193 +			}
  1.3194 +		}
  1.3195 +		else
  1.3196 +		{
  1.3197 +			gbJoymask[0] = systemGetJoypad(0, sensor);
  1.3198 +		}
  1.3199 +
  1.3200 +		// FIXME: horrible kludge
  1.3201 +		memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask));
  1.3202 +
  1.3203 +//		if (sensor)
  1.3204 +//			systemUpdateMotionSensor(0);
  1.3205 +
  1.3206 +		newmask = gbJoymask[0];
  1.3207 +		if (newmask & 0xFF)
  1.3208 +		{
  1.3209 +			gbInterrupt |= 16;
  1.3210 +		}
  1.3211 +
  1.3212 +		extButtons = (newmask >> 18);
  1.3213 +		speedup	   = (extButtons & 1) != 0;
  1.3214 +
  1.3215 +		VBAMovieResetIfRequested();
  1.3216 +
  1.3217 +		CallRegisteredLuaFunctions(LUACALL_BEFOREEMULATION);
  1.3218 +
  1.3219 +		newFrame = false;
  1.3220 +	}
  1.3221 +
  1.3222 +	for (;; )
  1.3223 +	{
  1.3224 +#ifndef FINAL_VERSION
  1.3225 +		if (systemDebug)
  1.3226 +		{
  1.3227 +			if (!(IFF & 0x80))
  1.3228 +			{
  1.3229 +				if (systemDebug > 1)
  1.3230 +				{
  1.3231 +					sprintf(gbBuffer, "PC=%04x AF=%04x BC=%04x DE=%04x HL=%04x SP=%04x I=%04x\n",
  1.3232 +					        PC.W, AF.W, BC.W, DE.W, HL.W, SP.W, IFF);
  1.3233 +				}
  1.3234 +				else
  1.3235 +				{
  1.3236 +					sprintf(gbBuffer, "PC=%04x I=%02x\n", PC.W, IFF);
  1.3237 +				}
  1.3238 +				log(gbBuffer);
  1.3239 +			}
  1.3240 +		}
  1.3241 +#endif
  1.3242 +		if (IFF & 0x80)
  1.3243 +		{
  1.3244 +			if (register_LCDC & 0x80)
  1.3245 +			{
  1.3246 +				clockTicks = gbLcdTicks;
  1.3247 +			}
  1.3248 +			else
  1.3249 +				clockTicks = 100;
  1.3250 +
  1.3251 +			if (gbLcdMode == 1 && (gbLcdLYIncrementTicks < clockTicks))
  1.3252 +				clockTicks = gbLcdLYIncrementTicks;
  1.3253 +
  1.3254 +			if (gbSerialOn && (gbSerialTicks < clockTicks))
  1.3255 +				clockTicks = gbSerialTicks;
  1.3256 +
  1.3257 +			if (gbTimerOn && (gbTimerTicks < clockTicks))
  1.3258 +				clockTicks = gbTimerTicks;
  1.3259 +
  1.3260 +			if (soundTicks && (soundTicks < clockTicks))
  1.3261 +				clockTicks = soundTicks;
  1.3262 +		}
  1.3263 +		else
  1.3264 +		{
  1.3265 +			opcode = gbReadOpcode(PC.W);
  1.3266 +			CallRegisteredLuaMemHook(PC.W, 1, opcode, LUAMEMHOOK_EXEC);
  1.3267 +			PC.W++;
  1.3268 +
  1.3269 +			if (IFF & 0x100)
  1.3270 +			{
  1.3271 +				IFF &= 0xff;
  1.3272 +				PC.W--;
  1.3273 +			}
  1.3274 +
  1.3275 +			clockTicks = gbCycles[opcode];
  1.3276 +
  1.3277 +			switch (opcode)
  1.3278 +			{
  1.3279 +			case 0xCB:
  1.3280 +				// extended opcode
  1.3281 +				//CallRegisteredLuaMemHook(PC.W, 1, opcode, LUAMEMHOOK_EXEC);	// is this desired?
  1.3282 +				opcode	   = gbReadOpcode(PC.W++);
  1.3283 +				clockTicks = gbCyclesCB[opcode];
  1.3284 +				switch (opcode)
  1.3285 +				{
  1.3286 +#include "gbCodesCB.h"
  1.3287 +				}
  1.3288 +				break;
  1.3289 +#include "gbCodes.h"
  1.3290 +			}
  1.3291 +		}
  1.3292 +
  1.3293 +		if (!emulating)
  1.3294 +			return;
  1.3295 +
  1.3296 +		if (gbDmaTicks)
  1.3297 +		{
  1.3298 +			clockTicks += gbDmaTicks;
  1.3299 +			gbDmaTicks	= 0;
  1.3300 +		}
  1.3301 +
  1.3302 +		if (gbSgbMode)
  1.3303 +		{
  1.3304 +			if (gbSgbPacketTimeout)
  1.3305 +			{
  1.3306 +				gbSgbPacketTimeout -= clockTicks;
  1.3307 +
  1.3308 +				if (gbSgbPacketTimeout <= 0)
  1.3309 +					gbSgbResetPacketState();
  1.3310 +			}
  1.3311 +		}
  1.3312 +
  1.3313 +		ticksToStop -= clockTicks;
  1.3314 +
  1.3315 +		// DIV register emulation
  1.3316 +		gbDivTicks -= clockTicks;
  1.3317 +		while (gbDivTicks <= 0)
  1.3318 +		{
  1.3319 +			register_DIV++;
  1.3320 +			gbDivTicks += GBDIV_CLOCK_TICKS;
  1.3321 +		}
  1.3322 +
  1.3323 +		if (register_LCDC & 0x80)
  1.3324 +		{
  1.3325 +			// LCD stuff
  1.3326 +			gbLcdTicks -= clockTicks;
  1.3327 +			if (gbLcdMode == 1)
  1.3328 +			{
  1.3329 +				// during V-BLANK,we need to increment LY at the same rate!
  1.3330 +				gbLcdLYIncrementTicks -= clockTicks;
  1.3331 +				while (gbLcdLYIncrementTicks <= 0)
  1.3332 +				{
  1.3333 +					gbLcdLYIncrementTicks += GBLY_INCREMENT_CLOCK_TICKS;
  1.3334 +
  1.3335 +					if (register_LY < 153)
  1.3336 +					{
  1.3337 +						register_LY++;
  1.3338 +
  1.3339 +						gbCompareLYToLYC();
  1.3340 +
  1.3341 +						if (register_LY >= 153)
  1.3342 +							gbLcdLYIncrementTicks = 6;
  1.3343 +					}
  1.3344 +					else
  1.3345 +					{
  1.3346 +						register_LY = 0x00;
  1.3347 +						// reset the window line
  1.3348 +						gbWindowLine = -1;
  1.3349 +						gbLcdLYIncrementTicks = GBLY_INCREMENT_CLOCK_TICKS * 2;
  1.3350 +						gbCompareLYToLYC();
  1.3351 +					}
  1.3352 +				}
  1.3353 +			}
  1.3354 +
  1.3355 +			// our counter is off, see what we need to do
  1.3356 +			while (gbLcdTicks <= 0)
  1.3357 +			{
  1.3358 +				int framesToSkip = systemFramesToSkip();
  1.3359 +
  1.3360 +				switch (gbLcdMode)
  1.3361 +				{
  1.3362 +				case 0:
  1.3363 +					// H-Blank
  1.3364 +					register_LY++;
  1.3365 +
  1.3366 +					gbCompareLYToLYC();
  1.3367 +
  1.3368 +					// check if we reached the V-Blank period
  1.3369 +					if (register_LY == 144)
  1.3370 +					{
  1.3371 +						// Yes, V-Blank
  1.3372 +						// set the LY increment counter
  1.3373 +						gbLcdLYIncrementTicks = gbLcdTicks + GBLY_INCREMENT_CLOCK_TICKS;
  1.3374 +						gbLcdTicks += GBLCD_MODE_1_CLOCK_TICKS;
  1.3375 +						gbLcdMode	= 1;
  1.3376 +						if (register_LCDC & 0x80)
  1.3377 +						{
  1.3378 +							gbInterrupt	   |= 1; // V-Blank interrupt
  1.3379 +							gbInterruptWait = 6;
  1.3380 +							if (register_STAT & 0x10)
  1.3381 +								gbInterrupt |= 2;
  1.3382 +						}
  1.3383 +
  1.3384 +						systemFrame();
  1.3385 +
  1.3386 +						++gbFrameCount;
  1.3387 +						u32 currentTime = systemGetClock();
  1.3388 +						if (currentTime - gbLastTime >= 1000)
  1.3389 +						{
  1.3390 +							systemShowSpeed(int(float(gbFrameCount) * 100000 / (float(currentTime - gbLastTime) * 60) + .5f));
  1.3391 +							gbLastTime	 = currentTime;
  1.3392 +							gbFrameCount = 0;
  1.3393 +						}
  1.3394 +
  1.3395 +						++GBSystemCounters.frameCount;
  1.3396 +						if (GBSystemCounters.lagged)
  1.3397 +						{
  1.3398 +							++GBSystemCounters.lagCount;
  1.3399 +						}
  1.3400 +						GBSystemCounters.laggedLast = GBSystemCounters.lagged;
  1.3401 +						GBSystemCounters.lagged		= true;
  1.3402 +
  1.3403 +						extern void VBAOnEnteringFrameBoundary();
  1.3404 +						VBAOnEnteringFrameBoundary();
  1.3405 +
  1.3406 +						newFrame = true;
  1.3407 +
  1.3408 +						pauseAfterFrameAdvance = systemPauseOnFrame();
  1.3409 +
  1.3410 +						if (gbFrameSkipCount >= framesToSkip || pauseAfterFrameAdvance)
  1.3411 +						{
  1.3412 +							if (gbBorderOn)
  1.3413 +								gbSgbRenderBorder();  // clear unnecessary things on border (e.g. in-game text message)
  1.3414 +
  1.3415 +							systemRenderFrame();
  1.3416 +							gbFrameSkipCount = 0;
  1.3417 +
  1.3418 +							bool capturePressed = (extButtons & 2) != 0;
  1.3419 +							if (capturePressed && !capturePrevious)
  1.3420 +							{
  1.3421 +								captureNumber = systemScreenCapture(captureNumber);
  1.3422 +							}
  1.3423 +							capturePrevious = capturePressed && !pauseAfterFrameAdvance;
  1.3424 +						}
  1.3425 +						else
  1.3426 +						{
  1.3427 +							++gbFrameSkipCount;
  1.3428 +						}
  1.3429 +
  1.3430 +						if (pauseAfterFrameAdvance)
  1.3431 +						{
  1.3432 +							systemSetPause(true);
  1.3433 +						}
  1.3434 +					}
  1.3435 +					else
  1.3436 +					{
  1.3437 +						// go the the OAM being accessed mode
  1.3438 +						gbLcdTicks += GBLCD_MODE_2_CLOCK_TICKS;
  1.3439 +						gbLcdMode	= 2;
  1.3440 +
  1.3441 +						// only one LCD interrupt per line. may need to generalize...
  1.3442 +						if (!(register_STAT & 0x40) ||
  1.3443 +						    (register_LY != register_LYC))
  1.3444 +						{
  1.3445 +							if ((register_STAT & 0x28) == 0x20)
  1.3446 +								gbInterrupt |= 2;
  1.3447 +						}
  1.3448 +					}
  1.3449 +
  1.3450 +					break;
  1.3451 +				case 1:
  1.3452 +					// V-Blank
  1.3453 +					// next mode is OAM being accessed mode
  1.3454 +					gbLcdTicks += GBLCD_MODE_2_CLOCK_TICKS;
  1.3455 +					gbLcdMode	= 2;
  1.3456 +					if (!(register_STAT & 0x40) ||
  1.3457 +					    (register_LY != register_LYC))
  1.3458 +					{
  1.3459 +						if ((register_STAT & 0x28) == 0x20)
  1.3460 +							gbInterrupt |= 2;
  1.3461 +					}
  1.3462 +					break;
  1.3463 +				case 2:
  1.3464 +					// OAM being accessed mode
  1.3465 +
  1.3466 +					// next mode is OAM and VRAM in use
  1.3467 +					gbLcdTicks += GBLCD_MODE_3_CLOCK_TICKS;
  1.3468 +					gbLcdMode	= 3;
  1.3469 +					break;
  1.3470 +				case 3:
  1.3471 +					// OAM and VRAM in use
  1.3472 +					// next mode is H-Blank
  1.3473 +					if (register_LY < 144)
  1.3474 +					{
  1.3475 +						if (!gbSgbMask)
  1.3476 +						{
  1.3477 +							if (gbFrameSkipCount >= framesToSkip || pauseAfterFrameAdvance)
  1.3478 +							{
  1.3479 +								gbRenderLine();
  1.3480 +								gbDrawSprites();
  1.3481 +
  1.3482 +								switch (systemColorDepth)
  1.3483 +								{
  1.3484 +								case 16:
  1.3485 +
  1.3486 +								{
  1.3487 +									u16 *dest = (u16 *)pix +
  1.3488 +									            (gbBorderLineSkip + 2) * (register_LY + gbBorderRowSkip + 1)
  1.3489 +									            + gbBorderColumnSkip;
  1.3490 +									for (int x = 0; x < 160; )
  1.3491 +									{
  1.3492 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3493 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3494 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3495 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3496 +
  1.3497 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3498 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3499 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3500 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3501 +
  1.3502 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3503 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3504 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3505 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3506 +
  1.3507 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3508 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3509 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3510 +										*dest++ = systemColorMap16[gbLineMix[x++]];
  1.3511 +									}
  1.3512 +									if (gbBorderOn)
  1.3513 +										dest += gbBorderColumnSkip;
  1.3514 +									*dest++ = 0;     // for filters that read one pixel more
  1.3515 +									break;
  1.3516 +								}
  1.3517 +								case 24:
  1.3518 +
  1.3519 +								{
  1.3520 +									u8 *dest = (u8 *)pix +
  1.3521 +									           3 * (gbBorderLineSkip * (register_LY + gbBorderRowSkip) +
  1.3522 +									                gbBorderColumnSkip);
  1.3523 +									for (int x = 0; x < 160; )
  1.3524 +									{
  1.3525 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3526 +										dest += 3;
  1.3527 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3528 +										dest += 3;
  1.3529 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3530 +										dest += 3;
  1.3531 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3532 +										dest += 3;
  1.3533 +
  1.3534 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3535 +										dest += 3;
  1.3536 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3537 +										dest += 3;
  1.3538 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3539 +										dest += 3;
  1.3540 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3541 +										dest += 3;
  1.3542 +
  1.3543 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3544 +										dest += 3;
  1.3545 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3546 +										dest += 3;
  1.3547 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3548 +										dest += 3;
  1.3549 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3550 +										dest += 3;
  1.3551 +
  1.3552 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3553 +										dest += 3;
  1.3554 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3555 +										dest += 3;
  1.3556 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3557 +										dest += 3;
  1.3558 +										*((u32 *)dest) = systemColorMap32[gbLineMix[x++]];
  1.3559 +										dest += 3;
  1.3560 +									}
  1.3561 +									break;
  1.3562 +								}
  1.3563 +								case 32:
  1.3564 +
  1.3565 +								{
  1.3566 +									u32 *dest = (u32 *)pix +
  1.3567 +									            (gbBorderLineSkip + 1) * (register_LY + gbBorderRowSkip + 1)
  1.3568 +									            + gbBorderColumnSkip;
  1.3569 +									for (int x = 0; x < 160; )
  1.3570 +									{
  1.3571 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3572 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3573 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3574 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3575 +
  1.3576 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3577 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3578 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3579 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3580 +
  1.3581 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3582 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3583 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3584 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3585 +
  1.3586 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3587 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3588 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3589 +										*dest++ = systemColorMap32[gbLineMix[x++]];
  1.3590 +									}
  1.3591 +									break;
  1.3592 +								}
  1.3593 +								}
  1.3594 +							}
  1.3595 +						}
  1.3596 +					}
  1.3597 +					gbLcdTicks += GBLCD_MODE_0_CLOCK_TICKS;
  1.3598 +					gbLcdMode	= 0;
  1.3599 +					// only one LCD interrupt per line. may need to generalize...
  1.3600 +					if (!(register_STAT & 0x40) ||
  1.3601 +					    (register_LY != register_LYC))
  1.3602 +					{
  1.3603 +						if (register_STAT & 0x08)
  1.3604 +							gbInterrupt |= 2;
  1.3605 +					}
  1.3606 +					if (gbHdmaOn)
  1.3607 +					{
  1.3608 +						gbDoHdma();
  1.3609 +					}
  1.3610 +					break;
  1.3611 +				}
  1.3612 +				// mark the correct lcd mode on STAT register
  1.3613 +				register_STAT = (register_STAT & 0xfc) | gbLcdMode;
  1.3614 +			}
  1.3615 +		}
  1.3616 +
  1.3617 +		// serial emulation
  1.3618 +		if (gbSerialOn)
  1.3619 +		{
  1.3620 +#ifdef LINK_EMULATION
  1.3621 +			if (linkConnected)
  1.3622 +			{
  1.3623 +				gbSerialTicks -= clockTicks;
  1.3624 +
  1.3625 +				while (gbSerialTicks <= 0)
  1.3626 +				{
  1.3627 +					// increment number of shifted bits
  1.3628 +					gbSerialBits++;
  1.3629 +					linkProc();
  1.3630 +					if (gbSerialOn && (gbMemory[0xff02] & 1))
  1.3631 +					{
  1.3632 +						if (gbSerialBits == 8)
  1.3633 +						{
  1.3634 +							gbSerialBits	  = 0;
  1.3635 +							gbMemory[0xff01]  = 0xff;
  1.3636 +							gbMemory[0xff02] &= 0x7f;
  1.3637 +							gbSerialOn		  = 0;
  1.3638 +							gbInterrupt		 |= 8;
  1.3639 +							gbSerialTicks	  = 0;
  1.3640 +						}
  1.3641 +					}
  1.3642 +					gbSerialTicks += GBSERIAL_CLOCK_TICKS;
  1.3643 +				}
  1.3644 +			}
  1.3645 +			else
  1.3646 +			{
  1.3647 +#endif
  1.3648 +			if (gbMemory[0xff02] & 1)
  1.3649 +			{
  1.3650 +				gbSerialTicks -= clockTicks;
  1.3651 +
  1.3652 +				// overflow
  1.3653 +				while (gbSerialTicks <= 0)
  1.3654 +				{
  1.3655 +					// shift serial byte to right and put a 1 bit in its place
  1.3656 +					//      gbMemory[0xff01] = 0x80 | (gbMemory[0xff01]>>1);
  1.3657 +					// increment number of shifted bits
  1.3658 +					gbSerialBits++;
  1.3659 +					if (gbSerialBits == 8)
  1.3660 +					{
  1.3661 +						// end of transmission
  1.3662 +						if (gbSerialFunction)    // external device
  1.3663 +							gbMemory[0xff01] = gbSerialFunction(gbMemory[0xff01]);
  1.3664 +						else
  1.3665 +							gbMemory[0xff01] = 0xff;
  1.3666 +						gbSerialTicks	  = 0;
  1.3667 +						gbMemory[0xff02] &= 0x7f;
  1.3668 +						gbSerialOn		  = 0;
  1.3669 +						gbInterrupt		 |= 8;
  1.3670 +						gbSerialBits	  = 0;
  1.3671 +					}
  1.3672 +					else
  1.3673 +						gbSerialTicks += GBSERIAL_CLOCK_TICKS;
  1.3674 +				}
  1.3675 +			}
  1.3676 +#ifdef LINK_EMULATION
  1.3677 +		}
  1.3678 +#endif
  1.3679 +		}
  1.3680 +
  1.3681 +		// timer emulation
  1.3682 +		if (gbTimerOn)
  1.3683 +		{
  1.3684 +			gbTimerTicks -= clockTicks;
  1.3685 +
  1.3686 +			while (gbTimerTicks <= 0)
  1.3687 +			{
  1.3688 +				register_TIMA++;
  1.3689 +
  1.3690 +				if (register_TIMA == 0)
  1.3691 +				{
  1.3692 +					// timer overflow!
  1.3693 +
  1.3694 +					// reload timer modulo
  1.3695 +					register_TIMA = register_TMA;
  1.3696 +
  1.3697 +					// flag interrupt
  1.3698 +					gbInterrupt |= 4;
  1.3699 +				}
  1.3700 +
  1.3701 +				gbTimerTicks += gbTimerClockTicks;
  1.3702 +			}
  1.3703 +		}
  1.3704 +
  1.3705 +		/*
  1.3706 +		   if(soundOffFlag)
  1.3707 +		   {
  1.3708 +		   if(synchronize && !speedup)
  1.3709 +		   {
  1.3710 +		   synchronizeTicks -= clockTicks;
  1.3711 +
  1.3712 +		   while(synchronizeTicks < 0)
  1.3713 +		   {
  1.3714 +		   synchronizeTicks += SYNCHRONIZE_CLOCK_TICKS;
  1.3715 +
  1.3716 +		   DWORD now = timeGetTime();
  1.3717 +		   gbElapsedTime += (now - timeNow);
  1.3718 +
  1.3719 +		   if(gbElapsedTime < 50)
  1.3720 +		   {
  1.3721 +		   DWORD diff = 50 - gbElapsedTime;
  1.3722 +		   Sleep(diff);
  1.3723 +		   timeNow = timeGetTime();
  1.3724 +		   elapsedTime = timeNow - now - diff;
  1.3725 +		   if((int)elapsedTime < 0)
  1.3726 +		   elapsedTime = 0;
  1.3727 +		   } else
  1.3728 +		   {
  1.3729 +		   timeNow = timeGetTime();
  1.3730 +		   elapsedTime = 0;
  1.3731 +		   }
  1.3732 +		   }
  1.3733 +		   }
  1.3734 +		   }
  1.3735 +		 */
  1.3736 +
  1.3737 +		soundTicks -= clockTicks;
  1.3738 +		while (soundTicks < 0) // must be < 1 when soundtick_t is real data type
  1.3739 +		{
  1.3740 +			soundTicks += SOUND_CLOCK_TICKS;
  1.3741 +
  1.3742 +			gbSoundTick();
  1.3743 +		}
  1.3744 +
  1.3745 +		register_IF = gbInterrupt;
  1.3746 +
  1.3747 +		if (IFF & 0x20)
  1.3748 +		{
  1.3749 +			IFF &= 0xdf;
  1.3750 +			IFF |= 0x01;
  1.3751 +			gbInterruptWait = 0;
  1.3752 +		}
  1.3753 +		else if (gbInterrupt)
  1.3754 +		{
  1.3755 +			if (gbInterruptWait == 0)
  1.3756 +			{
  1.3757 +				//        gbInterruptWait = 0;
  1.3758 +
  1.3759 +				if (IFF & 0x01)
  1.3760 +				{
  1.3761 +					if ((gbInterrupt & 1) && (register_IE & 1))
  1.3762 +					{
  1.3763 +						gbVblank_interrupt();
  1.3764 +						continue;
  1.3765 +					}
  1.3766 +
  1.3767 +					if ((gbInterrupt & 2) && (register_IE & 2))
  1.3768 +					{
  1.3769 +						gbLcd_interrupt();
  1.3770 +						continue;
  1.3771 +					}
  1.3772 +
  1.3773 +					if ((gbInterrupt & 4) && (register_IE & 4))
  1.3774 +					{
  1.3775 +						gbTimer_interrupt();
  1.3776 +						continue;
  1.3777 +					}
  1.3778 +
  1.3779 +					if ((gbInterrupt & 8) && (register_IE & 8))
  1.3780 +					{
  1.3781 +						gbSerial_interrupt();
  1.3782 +						continue;
  1.3783 +					}
  1.3784 +
  1.3785 +					if ((gbInterrupt & 16) && (register_IE & 16))
  1.3786 +					{
  1.3787 +						gbJoypad_interrupt();
  1.3788 +						continue;
  1.3789 +					}
  1.3790 +				}
  1.3791 +			}
  1.3792 +			else
  1.3793 +			{
  1.3794 +				gbInterruptWait -= clockTicks;
  1.3795 +				if (gbInterruptWait < 0)
  1.3796 +					gbInterruptWait = 0;
  1.3797 +			}
  1.3798 +		}
  1.3799 +
  1.3800 +		if (useOldFrameTiming)
  1.3801 +		{
  1.3802 +			// old timing code
  1.3803 +			if (ticksToStop > 0)
  1.3804 +				continue;
  1.3805 +		}
  1.3806 +		else
  1.3807 +		{
  1.3808 +			if (!newFrame && (register_LCDC & 0x80) != 0)
  1.3809 +				continue;
  1.3810 +		}
  1.3811 +
  1.3812 +		if (!(register_LCDC & 0x80))
  1.3813 +		{
  1.3814 +			if (!useOldFrameTiming)
  1.3815 +			{
  1.3816 +				// FIXME: since register_LY can be reset to 0 by some games, frame length is variable
  1.3817 +				// and infinite loops can occurr
  1.3818 +				// for now, it IS necessary to do something on this condition or games like
  1.3819 +				// Megaman would freeze upon low-level restart interrupt sequence (Start+Select+A+B).
  1.3820 +				// the only sensible way to fix this issue is to implement the RIGHT frame timing
  1.3821 +#ifdef WANTS_INCOMPLETE_WORKAROUND
  1.3822 +				if (systemReadJoypads())
  1.3823 +				{
  1.3824 +					if (gbSgbMode && gbSgbMultiplayer)
  1.3825 +					{
  1.3826 +						if (gbSgbFourPlayers)
  1.3827 +						{
  1.3828 +							gbJoymask[0] = systemGetJoypad(0, false);
  1.3829 +							gbJoymask[1] = systemGetJoypad(1, false);
  1.3830 +							gbJoymask[2] = systemGetJoypad(2, false);
  1.3831 +							gbJoymask[3] = systemGetJoypad(3, false);
  1.3832 +						}
  1.3833 +						else
  1.3834 +						{
  1.3835 +							gbJoymask[0] = systemGetJoypad(0, false);
  1.3836 +							gbJoymask[1] = systemGetJoypad(1, false);
  1.3837 +						}
  1.3838 +					}
  1.3839 +					else
  1.3840 +					{
  1.3841 +						gbJoymask[0] = systemGetJoypad(0, false);
  1.3842 +					}
  1.3843 +				}
  1.3844 +				else
  1.3845 +					gbJoymask[0] = gbJoymask[1] = gbJoymask[2] = gbJoymask[3] = 0;
  1.3846 +#else
  1.3847 +#endif
  1.3848 +			}
  1.3849 +		}
  1.3850 +
  1.3851 +		// makes sure frames are really divided across input sampling boundaries which occur at a constant rate
  1.3852 +		if (newFrame || useOldFrameTiming)
  1.3853 +		{
  1.3854 +///			extern void VBAOnEnteringFrameBoundary();
  1.3855 +///			VBAOnEnteringFrameBoundary();
  1.3856 +
  1.3857 +			break;
  1.3858 +		}
  1.3859 +	}
  1.3860 +}
  1.3861 +
  1.3862 +struct EmulatedSystem GBSystem =
  1.3863 +{
  1.3864 +	// emuMain
  1.3865 +	gbEmulate,
  1.3866 +	// emuReset
  1.3867 +	gbReset,
  1.3868 +	// emuCleanUp
  1.3869 +	gbCleanUp,
  1.3870 +	// emuReadBattery
  1.3871 +	gbReadBatteryFile,
  1.3872 +	// emuWriteBattery
  1.3873 +	gbWriteBatteryFile,
  1.3874 +	// emuReadBatteryFromStream
  1.3875 +	gbReadBatteryFromStream,
  1.3876 +	// emuWriteBatteryToStream
  1.3877 +	gbWriteBatteryToStream,
  1.3878 +	// emuReadState
  1.3879 +	gbReadSaveState,
  1.3880 +	// emuWriteState
  1.3881 +	gbWriteSaveState,
  1.3882 +	// emuReadStateFromStream
  1.3883 +	gbReadSaveStateFromStream,
  1.3884 +	// emuWriteStateToStream
  1.3885 +	gbWriteSaveStateToStream,
  1.3886 +	// emuReadMemState
  1.3887 +	gbReadMemSaveState,
  1.3888 +	// emuWriteMemState
  1.3889 +	gbWriteMemSaveState,
  1.3890 +	// emuWritePNG
  1.3891 +	gbWritePNGFile,
  1.3892 +	// emuWriteBMP
  1.3893 +	gbWriteBMPFile,
  1.3894 +	// emuUpdateCPSR
  1.3895 +	NULL,
  1.3896 +	// emuHasDebugger
  1.3897 +	false,
  1.3898 +	// emuCount
  1.3899 +#ifdef FINAL_VERSION
  1.3900 +	70000 / 4,
  1.3901 +#else
  1.3902 +	1000,
  1.3903 +#endif
  1.3904 +};
  1.3905 +
  1.3906 +// is there a reason to use more than one set of counters?
  1.3907 +EmulatedSystemCounters &GBSystemCounters = systemCounters;
  1.3908 +
  1.3909 +/*
  1.3910 +   EmulatedSystemCounters GBSystemCounters =
  1.3911 +   {
  1.3912 +    // frameCount
  1.3913 +    0,
  1.3914 +    // lagCount
  1.3915 +    0,
  1.3916 +    // lagged
  1.3917 +    true,
  1.3918 +    // laggedLast
  1.3919 +    true,
  1.3920 +   };
  1.3921 + */