diff src/common/Util.cpp @ 19:5e8e5083da94

brought in common and gba, fixed problems with outdated Makefile.am files in both of these packages
author Robert McIntyre <rlm@mit.edu>
date Sun, 04 Mar 2012 14:33:52 -0600
parents f9f4f1b99eed
children f94fa48624d0
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/common/Util.cpp	Sun Mar 04 14:33:52 2012 -0600
     1.3 @@ -0,0 +1,1400 @@
     1.4 +#include <cstdio>
     1.5 +#include <cstdlib>
     1.6 +#include <cstring>
     1.7 +#include <zlib.h>
     1.8 +
     1.9 +extern "C" {
    1.10 +#include <png.h>
    1.11 +}
    1.12 +
    1.13 +#if 0
    1.14 +#include "unrarlib.h"
    1.15 +#endif
    1.16 +
    1.17 +#include "unzip.h"
    1.18 +
    1.19 +#include "../NLS.h"
    1.20 +#include "System.h"
    1.21 +#include "Util.h"
    1.22 +#include "../gba/Flash.h"
    1.23 +#include "../gba/RTC.h"
    1.24 +
    1.25 +extern "C" {
    1.26 +#include "memgzio.h"
    1.27 +}
    1.28 +
    1.29 +#ifndef _MSC_VER
    1.30 +#define _stricmp strcasecmp
    1.31 +#endif // ! _MSC_VER
    1.32 +
    1.33 +extern int32 cpuSaveType;
    1.34 +
    1.35 +extern int systemColorDepth;
    1.36 +extern int systemRedShift;
    1.37 +extern int systemGreenShift;
    1.38 +extern int systemBlueShift;
    1.39 +
    1.40 +extern u16 systemColorMap16[0x10000];
    1.41 +extern u32 systemColorMap32[0x10000];
    1.42 +
    1.43 +static int	   (ZEXPORT *utilGzWriteFunc)(gzFile, voidp, unsigned int) = NULL;
    1.44 +static int	   (ZEXPORT *utilGzReadFunc)(gzFile, voidp, unsigned int)  = NULL;
    1.45 +static int	   (ZEXPORT *utilGzCloseFunc)(gzFile) = NULL;
    1.46 +static z_off_t (ZEXPORT *utilGzSeekFunc)(gzFile, z_off_t, int) = NULL;
    1.47 +static z_off_t (ZEXPORT *utilGzTellFunc)(gzFile) = NULL;
    1.48 +
    1.49 +//Kludge to get it to compile in Linux, GCC cannot convert
    1.50 +//gzwrite function pointer to the type of utilGzWriteFunc
    1.51 +//due to void* and const void* differences
    1.52 +//--Felipe 
    1.53 +int gzWrite(gzFile file, void* buf, unsigned len){
    1.54 +	return gzwrite(file,buf,len);
    1.55 +}
    1.56 +
    1.57 +void utilPutDword(u8 *p, u32 value)
    1.58 +{
    1.59 +	*p++ = value & 255;
    1.60 +	*p++ = (value >> 8) & 255;
    1.61 +	*p++ = (value >> 16) & 255;
    1.62 +	*p	 = (value >> 24) & 255;
    1.63 +}
    1.64 +
    1.65 +void utilPutWord(u8 *p, u16 value)
    1.66 +{
    1.67 +	*p++ = value & 255;
    1.68 +	*p	 = (value >> 8) & 255;
    1.69 +}
    1.70 +
    1.71 +void utilWriteBMP(u8 *b, int w, int h, int dstDepth, u8 *pix)
    1.72 +{
    1.73 +	int sizeX = w;
    1.74 +	int sizeY = h;
    1.75 +
    1.76 +	switch (dstDepth > 0 ? dstDepth : systemColorDepth)
    1.77 +	{
    1.78 +	case 16:
    1.79 +	{
    1.80 +		u16 *p = (u16 *)(pix + (w + 2) * (h) * 2); // skip first black line
    1.81 +		for (int y = 0; y < sizeY; y++)
    1.82 +		{
    1.83 +			for (int x = 0; x < sizeX; x++)
    1.84 +			{
    1.85 +				u16 v = *p++;
    1.86 +
    1.87 +				*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
    1.88 +				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
    1.89 +				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
    1.90 +			}
    1.91 +			p++; // skip black pixel for filters
    1.92 +			p++; // skip black pixel for filters
    1.93 +			p -= 2 * (w + 2);
    1.94 +		}
    1.95 +		break;
    1.96 +	}
    1.97 +	case 24:
    1.98 +	{
    1.99 +		u8 *pixU8 = (u8 *)pix + 3 * w * (h - 1);
   1.100 +		for (int y = 0; y < sizeY; y++)
   1.101 +		{
   1.102 +			for (int x = 0; x < sizeX; x++)
   1.103 +			{
   1.104 +				if (systemRedShift > systemBlueShift)
   1.105 +				{
   1.106 +					*b++ = *pixU8++; // B
   1.107 +					*b++ = *pixU8++; // G
   1.108 +					*b++ = *pixU8++; // R
   1.109 +				}
   1.110 +				else
   1.111 +				{
   1.112 +					int red	  = *pixU8++;
   1.113 +					int green = *pixU8++;
   1.114 +					int blue  = *pixU8++;
   1.115 +
   1.116 +					*b++ = blue;
   1.117 +					*b++ = green;
   1.118 +					*b++ = red;
   1.119 +				}
   1.120 +			}
   1.121 +			pixU8 -= 2 * 3 * w;
   1.122 +		}
   1.123 +		break;
   1.124 +	}
   1.125 +	case 32:
   1.126 +	{
   1.127 +		u32 *pixU32 = (u32 *)(pix + 4 * (w + 1) * (h));
   1.128 +		for (int y = 0; y < sizeY; y++)
   1.129 +		{
   1.130 +			for (int x = 0; x < sizeX; x++)
   1.131 +			{
   1.132 +				u32 v = *pixU32++;
   1.133 +
   1.134 +				*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.135 +				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.136 +				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.137 +			}
   1.138 +			pixU32++;
   1.139 +			pixU32 -= 2 * (w + 1);
   1.140 +		}
   1.141 +		break;
   1.142 +	}
   1.143 +	}
   1.144 +}
   1.145 +
   1.146 +bool utilWriteBMPFile(const char *fileName, int w, int h, u8 *pix)
   1.147 +{
   1.148 +	u8 writeBuffer[256 * 3];
   1.149 +
   1.150 +	FILE *fp = fopen(fileName, "wb");
   1.151 +
   1.152 +	if (!fp)
   1.153 +	{
   1.154 +		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName);
   1.155 +		return false;
   1.156 +	}
   1.157 +
   1.158 +	struct
   1.159 +	{
   1.160 +		u8 ident[2];
   1.161 +		u8 filesize[4];
   1.162 +		u8 reserved[4];
   1.163 +		u8 dataoffset[4];
   1.164 +		u8 headersize[4];
   1.165 +		u8 width[4];
   1.166 +		u8 height[4];
   1.167 +		u8 planes[2];
   1.168 +		u8 bitsperpixel[2];
   1.169 +		u8 compression[4];
   1.170 +		u8 datasize[4];
   1.171 +		u8 hres[4];
   1.172 +		u8 vres[4];
   1.173 +		u8 colors[4];
   1.174 +		u8 importantcolors[4];
   1.175 +		//    u8 pad[2];
   1.176 +	} bmpheader;
   1.177 +	memset(&bmpheader, 0, sizeof(bmpheader));
   1.178 +
   1.179 +	bmpheader.ident[0] = 'B';
   1.180 +	bmpheader.ident[1] = 'M';
   1.181 +
   1.182 +	u32 fsz = sizeof(bmpheader) + w * h * 3;
   1.183 +	utilPutDword(bmpheader.filesize, fsz);
   1.184 +	utilPutDword(bmpheader.dataoffset, 0x36);
   1.185 +	utilPutDword(bmpheader.headersize, 0x28);
   1.186 +	utilPutDword(bmpheader.width, w);
   1.187 +	utilPutDword(bmpheader.height, h);
   1.188 +	utilPutDword(bmpheader.planes, 1);
   1.189 +	utilPutDword(bmpheader.bitsperpixel, 24);
   1.190 +	utilPutDword(bmpheader.datasize, 3 * w * h);
   1.191 +
   1.192 +	fwrite(&bmpheader, 1, sizeof(bmpheader), fp);
   1.193 +
   1.194 +#if 0
   1.195 +	// FIXME: need sufficient buffer
   1.196 +	utilWriteBMP(writeBuffer, w, h, systemColorDepth, pix);
   1.197 +#else
   1.198 +	u8 *b = writeBuffer;
   1.199 +
   1.200 +	int sizeX = w;
   1.201 +	int sizeY = h;
   1.202 +
   1.203 +	switch (systemColorDepth)
   1.204 +	{
   1.205 +	case 16:
   1.206 +	{
   1.207 +		u16 *p = (u16 *)(pix + (w + 2) * (h) * 2); // skip first black line
   1.208 +		for (int y = 0; y < sizeY; y++)
   1.209 +		{
   1.210 +			for (int x = 0; x < sizeX; x++)
   1.211 +			{
   1.212 +				u16 v = *p++;
   1.213 +
   1.214 +				*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
   1.215 +				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.216 +				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.217 +			}
   1.218 +			p++; // skip black pixel for filters
   1.219 +			p++; // skip black pixel for filters
   1.220 +			p -= 2 * (w + 2);
   1.221 +			fwrite(writeBuffer, 1, 3 * w, fp);
   1.222 +
   1.223 +			b = writeBuffer;
   1.224 +		}
   1.225 +		break;
   1.226 +	}
   1.227 +	case 24:
   1.228 +	{
   1.229 +		u8 *pixU8 = (u8 *)pix + 3 * w * (h - 1);
   1.230 +		for (int y = 0; y < sizeY; y++)
   1.231 +		{
   1.232 +			for (int x = 0; x < sizeX; x++)
   1.233 +			{
   1.234 +				if (systemRedShift > systemBlueShift)
   1.235 +				{
   1.236 +					*b++ = *pixU8++; // B
   1.237 +					*b++ = *pixU8++; // G
   1.238 +					*b++ = *pixU8++; // R
   1.239 +				}
   1.240 +				else
   1.241 +				{
   1.242 +					int red	  = *pixU8++;
   1.243 +					int green = *pixU8++;
   1.244 +					int blue  = *pixU8++;
   1.245 +
   1.246 +					*b++ = blue;
   1.247 +					*b++ = green;
   1.248 +					*b++ = red;
   1.249 +				}
   1.250 +			}
   1.251 +			pixU8 -= 2 * 3 * w;
   1.252 +			fwrite(writeBuffer, 1, 3 * w, fp);
   1.253 +
   1.254 +			b = writeBuffer;
   1.255 +		}
   1.256 +		break;
   1.257 +	}
   1.258 +	case 32:
   1.259 +	{
   1.260 +		u32 *pixU32 = (u32 *)(pix + 4 * (w + 1) * (h));
   1.261 +		for (int y = 0; y < sizeY; y++)
   1.262 +		{
   1.263 +			for (int x = 0; x < sizeX; x++)
   1.264 +			{
   1.265 +				u32 v = *pixU32++;
   1.266 +
   1.267 +				*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.268 +				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.269 +				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.270 +			}
   1.271 +			pixU32++;
   1.272 +			pixU32 -= 2 * (w + 1);
   1.273 +
   1.274 +			fwrite(writeBuffer, 1, 3 * w, fp);
   1.275 +
   1.276 +			b = writeBuffer;
   1.277 +		}
   1.278 +		break;
   1.279 +	}
   1.280 +	}
   1.281 +#endif
   1.282 +
   1.283 +	fclose(fp);
   1.284 +
   1.285 +	return true;
   1.286 +}
   1.287 +
   1.288 +bool utilWritePNGFile(const char *fileName, int w, int h, u8 *pix)
   1.289 +{
   1.290 +	u8 writeBuffer[256 * 3];
   1.291 +
   1.292 +	FILE *fp = fopen(fileName, "wb");
   1.293 +
   1.294 +	if (!fp)
   1.295 +	{
   1.296 +		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName);
   1.297 +		return false;
   1.298 +	}
   1.299 +
   1.300 +	png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
   1.301 +	                                              NULL,
   1.302 +	                                              NULL,
   1.303 +	                                              NULL);
   1.304 +	if (!png_ptr)
   1.305 +	{
   1.306 +		fclose(fp);
   1.307 +		return false;
   1.308 +	}
   1.309 +
   1.310 +	png_infop info_ptr = png_create_info_struct(png_ptr);
   1.311 +
   1.312 +	if (!info_ptr)
   1.313 +	{
   1.314 +		png_destroy_write_struct(&png_ptr, NULL);
   1.315 +		fclose(fp);
   1.316 +		return false;
   1.317 +	}
   1.318 +
   1.319 +	if (setjmp(png_ptr->jmpbuf))
   1.320 +	{
   1.321 +		png_destroy_write_struct(&png_ptr, NULL);
   1.322 +		fclose(fp);
   1.323 +		return false;
   1.324 +	}
   1.325 +
   1.326 +	png_init_io(png_ptr, fp);
   1.327 +
   1.328 +	png_set_IHDR(png_ptr,
   1.329 +	             info_ptr,
   1.330 +	             w,
   1.331 +	             h,
   1.332 +	             8,
   1.333 +	             PNG_COLOR_TYPE_RGB,
   1.334 +	             PNG_INTERLACE_NONE,
   1.335 +	             PNG_COMPRESSION_TYPE_DEFAULT,
   1.336 +	             PNG_FILTER_TYPE_DEFAULT);
   1.337 +
   1.338 +	png_write_info(png_ptr, info_ptr);
   1.339 +
   1.340 +	u8 *b = writeBuffer;
   1.341 +
   1.342 +	int sizeX = w;
   1.343 +	int sizeY = h;
   1.344 +
   1.345 +	switch (systemColorDepth)
   1.346 +	{
   1.347 +	case 16:
   1.348 +	{
   1.349 +		u16 *p = (u16 *)(pix + (w + 2) * 2); // skip first black line
   1.350 +		for (int y = 0; y < sizeY; y++)
   1.351 +		{
   1.352 +			for (int x = 0; x < sizeX; x++)
   1.353 +			{
   1.354 +				u16 v = *p++;
   1.355 +
   1.356 +				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.357 +				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.358 +				*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
   1.359 +			}
   1.360 +			p++; // skip black pixel for filters
   1.361 +			p++; // skip black pixel for filters
   1.362 +			png_write_row(png_ptr, writeBuffer);
   1.363 +
   1.364 +			b = writeBuffer;
   1.365 +		}
   1.366 +		break;
   1.367 +	}
   1.368 +	case 24:
   1.369 +	{
   1.370 +		u8 *pixU8 = (u8 *)pix;
   1.371 +		for (int y = 0; y < sizeY; y++)
   1.372 +		{
   1.373 +			for (int x = 0; x < sizeX; x++)
   1.374 +			{
   1.375 +				if (systemRedShift < systemBlueShift)
   1.376 +				{
   1.377 +					*b++ = *pixU8++; // R
   1.378 +					*b++ = *pixU8++; // G
   1.379 +					*b++ = *pixU8++; // B
   1.380 +				}
   1.381 +				else
   1.382 +				{
   1.383 +					int blue  = *pixU8++;
   1.384 +					int green = *pixU8++;
   1.385 +					int red	  = *pixU8++;
   1.386 +
   1.387 +					*b++ = red;
   1.388 +					*b++ = green;
   1.389 +					*b++ = blue;
   1.390 +				}
   1.391 +			}
   1.392 +			png_write_row(png_ptr, writeBuffer);
   1.393 +
   1.394 +			b = writeBuffer;
   1.395 +		}
   1.396 +		break;
   1.397 +	}
   1.398 +	case 32:
   1.399 +	{
   1.400 +		u32 *pixU32 = (u32 *)(pix + 4 * (w + 1));
   1.401 +		for (int y = 0; y < sizeY; y++)
   1.402 +		{
   1.403 +			for (int x = 0; x < sizeX; x++)
   1.404 +			{
   1.405 +				u32 v = *pixU32++;
   1.406 +
   1.407 +				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.408 +				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.409 +				*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.410 +			}
   1.411 +			pixU32++;
   1.412 +
   1.413 +			png_write_row(png_ptr, writeBuffer);
   1.414 +
   1.415 +			b = writeBuffer;
   1.416 +		}
   1.417 +		break;
   1.418 +	}
   1.419 +	}
   1.420 +
   1.421 +	png_write_end(png_ptr, info_ptr);
   1.422 +
   1.423 +	png_destroy_write_struct(&png_ptr, &info_ptr);
   1.424 +
   1.425 +	fclose(fp);
   1.426 +
   1.427 +	return true;
   1.428 +}
   1.429 +
   1.430 +static int utilReadInt2(FILE *f)
   1.431 +{
   1.432 +	int res = 0;
   1.433 +	int c	= fgetc(f);
   1.434 +	if (c == EOF)
   1.435 +		return -1;
   1.436 +	res = c;
   1.437 +	c	= fgetc(f);
   1.438 +	if (c == EOF)
   1.439 +		return -1;
   1.440 +	return c + (res << 8);
   1.441 +}
   1.442 +
   1.443 +static int utilReadInt3(FILE *f)
   1.444 +{
   1.445 +	int res = 0;
   1.446 +	int c	= fgetc(f);
   1.447 +	if (c == EOF)
   1.448 +		return -1;
   1.449 +	res = c;
   1.450 +	c	= fgetc(f);
   1.451 +	if (c == EOF)
   1.452 +		return -1;
   1.453 +	res = c + (res << 8);
   1.454 +	c	= fgetc(f);
   1.455 +	if (c == EOF)
   1.456 +		return -1;
   1.457 +	return c + (res << 8);
   1.458 +}
   1.459 +
   1.460 +void utilApplyIPS(const char *ips, u8 * *r, int *s)
   1.461 +{
   1.462 +	// from the IPS spec at http://zerosoft.zophar.net/ips.htm
   1.463 +	FILE *f = fopen(ips, "rb");
   1.464 +	if (!f)
   1.465 +		return;
   1.466 +	u8 *rom	 = *r;
   1.467 +	int size = *s;
   1.468 +	if (fgetc(f) == 'P' &&
   1.469 +	    fgetc(f) == 'A' &&
   1.470 +	    fgetc(f) == 'T' &&
   1.471 +	    fgetc(f) == 'C' &&
   1.472 +	    fgetc(f) == 'H')
   1.473 +	{
   1.474 +		int b;
   1.475 +		int offset;
   1.476 +		int len;
   1.477 +		for (;; )
   1.478 +		{
   1.479 +			// read offset
   1.480 +			offset = utilReadInt3(f);
   1.481 +			// if offset == EOF, end of patch
   1.482 +			if (offset == 0x454f46)
   1.483 +				break;
   1.484 +			// read length
   1.485 +			len = utilReadInt2(f);
   1.486 +			if (!len)
   1.487 +			{
   1.488 +				// len == 0, RLE block
   1.489 +				len = utilReadInt2(f);
   1.490 +				// byte to fill
   1.491 +				int c = fgetc(f);
   1.492 +				if (c == -1)
   1.493 +					break;
   1.494 +				b = (u8)c;
   1.495 +			}
   1.496 +			else
   1.497 +				b = -1;
   1.498 +			// check if we need to reallocate our ROM
   1.499 +			if ((offset + len) >= size)
   1.500 +			{
   1.501 +				size *= 2;
   1.502 +				rom	  = (u8 *)realloc(rom, size);
   1.503 +				*r	  = rom;
   1.504 +				*s	  = size;
   1.505 +			}
   1.506 +			if (b == -1)
   1.507 +			{
   1.508 +				// normal block, just read the data
   1.509 +				if (fread(&rom[offset], 1, len, f) != (size_t)len)
   1.510 +					break;
   1.511 +			}
   1.512 +			else
   1.513 +			{
   1.514 +				// fill the region with the given byte
   1.515 +				while (len--)
   1.516 +				{
   1.517 +					rom[offset++] = b;
   1.518 +				}
   1.519 +			}
   1.520 +		}
   1.521 +	}
   1.522 +	// close the file
   1.523 +	fclose(f);
   1.524 +}
   1.525 +
   1.526 +extern bool8 cpuIsMultiBoot;
   1.527 +
   1.528 +bool utilIsGBAImage(const char *file)
   1.529 +{
   1.530 +	cpuIsMultiBoot = false;
   1.531 +	if (strlen(file) > 4)
   1.532 +	{
   1.533 +		const char *p = strrchr(file, '.');
   1.534 +
   1.535 +		if (p != NULL)
   1.536 +		{
   1.537 +			if (_stricmp(p, ".gba") == 0)
   1.538 +				return true;
   1.539 +			if (_stricmp(p, ".agb") == 0)
   1.540 +				return true;
   1.541 +			if (_stricmp(p, ".bin") == 0)
   1.542 +				return true;
   1.543 +			if (_stricmp(p, ".elf") == 0)
   1.544 +				return true;
   1.545 +			if (_stricmp(p, ".mb") == 0)
   1.546 +			{
   1.547 +				cpuIsMultiBoot = true;
   1.548 +				return true;
   1.549 +			}
   1.550 +		}
   1.551 +	}
   1.552 +
   1.553 +	return false;
   1.554 +}
   1.555 +
   1.556 +bool utilIsGBImage(const char *file)
   1.557 +{
   1.558 +	if (strlen(file) > 4)
   1.559 +	{
   1.560 +		const char *p = strrchr(file, '.');
   1.561 +
   1.562 +		if (p != NULL)
   1.563 +		{
   1.564 +			if (_stricmp(p, ".gb") == 0)
   1.565 +				return true;
   1.566 +			if (_stricmp(p, ".gbc") == 0)
   1.567 +				return true;
   1.568 +			if (_stricmp(p, ".cgb") == 0)
   1.569 +				return true;
   1.570 +			if (_stricmp(p, ".sgb") == 0)
   1.571 +				return true;
   1.572 +		}
   1.573 +	}
   1.574 +
   1.575 +	return false;
   1.576 +}
   1.577 +
   1.578 +bool utilIsGBABios(const char *file)
   1.579 +{
   1.580 +	if (strlen(file) > 4)
   1.581 +	{
   1.582 +		const char *p = strrchr(file, '.');
   1.583 +
   1.584 +		if (p != NULL)
   1.585 +		{
   1.586 +			if (_stricmp(p, ".gba") == 0)
   1.587 +				return true;
   1.588 +			if (_stricmp(p, ".agb") == 0)
   1.589 +				return true;
   1.590 +			if (_stricmp(p, ".bin") == 0)
   1.591 +				return true;
   1.592 +			if (_stricmp(p, ".bios") == 0)
   1.593 +				return true;
   1.594 +			if (_stricmp(p, ".rom") == 0)
   1.595 +				return true;
   1.596 +		}
   1.597 +	}
   1.598 +
   1.599 +	return false;
   1.600 +}
   1.601 +
   1.602 +bool utilIsGBBios(const char *file)
   1.603 +{
   1.604 +	if (strlen(file) > 4)
   1.605 +	{
   1.606 +		const char *p = strrchr(file, '.');
   1.607 +
   1.608 +		if (p != NULL)
   1.609 +		{
   1.610 +			if (_stricmp(p, ".gb") == 0)
   1.611 +				return true;
   1.612 +			if (_stricmp(p, ".bin") == 0)
   1.613 +				return true;
   1.614 +			if (_stricmp(p, ".bios") == 0)
   1.615 +				return true;
   1.616 +			if (_stricmp(p, ".rom") == 0)
   1.617 +				return true;
   1.618 +		}
   1.619 +	}
   1.620 +
   1.621 +	return false;
   1.622 +}
   1.623 +
   1.624 +bool utilIsELF(const char *file)
   1.625 +{
   1.626 +	if (strlen(file) > 4)
   1.627 +	{
   1.628 +		const char *p = strrchr(file, '.');
   1.629 +
   1.630 +		if (p != NULL)
   1.631 +		{
   1.632 +			if (_stricmp(p, ".elf") == 0)
   1.633 +				return true;
   1.634 +		}
   1.635 +	}
   1.636 +	return false;
   1.637 +}
   1.638 +
   1.639 +bool utilIsZipFile(const char *file)
   1.640 +{
   1.641 +	if (strlen(file) > 4)
   1.642 +	{
   1.643 +		const char *p = strrchr(file, '.');
   1.644 +
   1.645 +		if (p != NULL)
   1.646 +		{
   1.647 +			if (_stricmp(p, ".zip") == 0)
   1.648 +				return true;
   1.649 +		}
   1.650 +	}
   1.651 +
   1.652 +	return false;
   1.653 +}
   1.654 +
   1.655 +#if 0
   1.656 +bool utilIsRarFile(const char *file)
   1.657 +{
   1.658 +	if (strlen(file) > 4)
   1.659 +	{
   1.660 +		char *p = strrchr(file, '.');
   1.661 +
   1.662 +		if (p != NULL)
   1.663 +		{
   1.664 +			if (_stricmp(p, ".rar") == 0)
   1.665 +				return true;
   1.666 +		}
   1.667 +	}
   1.668 +
   1.669 +	return false;
   1.670 +}
   1.671 +
   1.672 +#endif
   1.673 +
   1.674 +bool utilIsGzipFile(const char *file)
   1.675 +{
   1.676 +	if (strlen(file) > 3)
   1.677 +	{
   1.678 +		const char *p = strrchr(file, '.');
   1.679 +
   1.680 +		if (p != NULL)
   1.681 +		{
   1.682 +			if (_stricmp(p, ".gz") == 0)
   1.683 +				return true;
   1.684 +			if (_stricmp(p, ".z") == 0)
   1.685 +				return true;
   1.686 +		}
   1.687 +	}
   1.688 +
   1.689 +	return false;
   1.690 +}
   1.691 +
   1.692 +void utilGetBaseName(const char *file, char *buffer)
   1.693 +{
   1.694 +	strcpy(buffer, file);
   1.695 +
   1.696 +	if (utilIsGzipFile(file))
   1.697 +	{
   1.698 +		char *p = strrchr(buffer, '.');
   1.699 +
   1.700 +		if (p)
   1.701 +			*p = 0;
   1.702 +	}
   1.703 +}
   1.704 +
   1.705 +IMAGE_TYPE utilFindType(const char *file)
   1.706 +{
   1.707 +	char buffer[2048];
   1.708 +
   1.709 +	if (utilIsZipFile(file))
   1.710 +	{
   1.711 +		unzFile unz = unzOpen(file);
   1.712 +
   1.713 +		if (unz == NULL)
   1.714 +		{
   1.715 +			systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file);
   1.716 +			return IMAGE_UNKNOWN;
   1.717 +		}
   1.718 +
   1.719 +		int r = unzGoToFirstFile(unz);
   1.720 +
   1.721 +		if (r != UNZ_OK)
   1.722 +		{
   1.723 +			unzClose(unz);
   1.724 +			systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
   1.725 +			return IMAGE_UNKNOWN;
   1.726 +		}
   1.727 +
   1.728 +		IMAGE_TYPE found = IMAGE_UNKNOWN;
   1.729 +
   1.730 +		unz_file_info info;
   1.731 +
   1.732 +		while (true)
   1.733 +		{
   1.734 +			r = unzGetCurrentFileInfo(unz,
   1.735 +			                          &info,
   1.736 +			                          buffer,
   1.737 +			                          sizeof(buffer),
   1.738 +			                          NULL,
   1.739 +			                          0,
   1.740 +			                          NULL,
   1.741 +			                          0);
   1.742 +
   1.743 +			if (r != UNZ_OK)
   1.744 +			{
   1.745 +				unzClose(unz);
   1.746 +				systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
   1.747 +				return IMAGE_UNKNOWN;
   1.748 +			}
   1.749 +
   1.750 +			if (utilIsGBAImage(buffer))
   1.751 +			{
   1.752 +				found = IMAGE_GBA;
   1.753 +				break;
   1.754 +			}
   1.755 +
   1.756 +			if (utilIsGBImage(buffer))
   1.757 +			{
   1.758 +				found = IMAGE_GB;
   1.759 +				break;
   1.760 +			}
   1.761 +
   1.762 +			r = unzGoToNextFile(unz);
   1.763 +
   1.764 +			if (r != UNZ_OK)
   1.765 +				break;
   1.766 +		}
   1.767 +		unzClose(unz);
   1.768 +
   1.769 +		if (found == IMAGE_UNKNOWN)
   1.770 +		{
   1.771 +			systemMessage(MSG_NO_IMAGE_ON_ZIP,
   1.772 +			              N_("No image found on ZIP file %s"), file);
   1.773 +			return found;
   1.774 +		}
   1.775 +		return found;
   1.776 +#if 0
   1.777 +	}
   1.778 +	else if (utilIsRarFile(file))
   1.779 +	{
   1.780 +		IMAGE_TYPE found = IMAGE_UNKNOWN;
   1.781 +
   1.782 +		ArchiveList_struct *rarList = NULL;
   1.783 +		if (urarlib_list((void *)file, (ArchiveList_struct *)&rarList))
   1.784 +		{
   1.785 +			ArchiveList_struct *p = rarList;
   1.786 +
   1.787 +			while (p)
   1.788 +			{
   1.789 +				if (utilIsGBAImage(p->item.Name))
   1.790 +				{
   1.791 +					found = IMAGE_GBA;
   1.792 +					break;
   1.793 +				}
   1.794 +
   1.795 +				if (utilIsGBImage(p->item.Name))
   1.796 +				{
   1.797 +					found = IMAGE_GB;
   1.798 +					break;
   1.799 +				}
   1.800 +				p = p->next;
   1.801 +			}
   1.802 +
   1.803 +			urarlib_freelist(rarList);
   1.804 +		}
   1.805 +		return found;
   1.806 +#endif
   1.807 +	}
   1.808 +	else
   1.809 +	{
   1.810 +		if (utilIsGzipFile(file))
   1.811 +			utilGetBaseName(file, buffer);
   1.812 +		else
   1.813 +			strcpy(buffer, file);
   1.814 +
   1.815 +		if (utilIsGBAImage(buffer))
   1.816 +			return IMAGE_GBA;
   1.817 +		if (utilIsGBImage(buffer))
   1.818 +			return IMAGE_GB;
   1.819 +	}
   1.820 +	return IMAGE_UNKNOWN;
   1.821 +}
   1.822 +
   1.823 +static int utilGetSize(int size)
   1.824 +{
   1.825 +	int res = 1;
   1.826 +	while (res < size)
   1.827 +		res <<= 1;
   1.828 +	return res;
   1.829 +}
   1.830 +
   1.831 +static u8 *utilLoadFromZip(const char *file,
   1.832 +                           bool (*accept)(const char *),
   1.833 +                           u8 *data,
   1.834 +                           int &size)
   1.835 +{
   1.836 +	char buffer[2048];
   1.837 +
   1.838 +	unzFile unz = unzOpen(file);
   1.839 +
   1.840 +	if (unz == NULL)
   1.841 +	{
   1.842 +		systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file);
   1.843 +		return NULL;
   1.844 +	}
   1.845 +	int r = unzGoToFirstFile(unz);
   1.846 +
   1.847 +	if (r != UNZ_OK)
   1.848 +	{
   1.849 +		unzClose(unz);
   1.850 +		systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
   1.851 +		return NULL;
   1.852 +	}
   1.853 +
   1.854 +	bool found = false;
   1.855 +
   1.856 +	unz_file_info info;
   1.857 +
   1.858 +	while (true)
   1.859 +	{
   1.860 +		r = unzGetCurrentFileInfo(unz,
   1.861 +		                          &info,
   1.862 +		                          buffer,
   1.863 +		                          sizeof(buffer),
   1.864 +		                          NULL,
   1.865 +		                          0,
   1.866 +		                          NULL,
   1.867 +		                          0);
   1.868 +
   1.869 +		if (r != UNZ_OK)
   1.870 +		{
   1.871 +			unzClose(unz);
   1.872 +			systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file);
   1.873 +			return NULL;
   1.874 +		}
   1.875 +
   1.876 +		if (accept(buffer))
   1.877 +		{
   1.878 +			found = true;
   1.879 +			break;
   1.880 +		}
   1.881 +
   1.882 +		r = unzGoToNextFile(unz);
   1.883 +
   1.884 +		if (r != UNZ_OK)
   1.885 +			break;
   1.886 +	}
   1.887 +
   1.888 +	if (!found)
   1.889 +	{
   1.890 +		unzClose(unz);
   1.891 +		systemMessage(MSG_NO_IMAGE_ON_ZIP,
   1.892 +		              N_("No image found on ZIP file %s"), file);
   1.893 +		return NULL;
   1.894 +	}
   1.895 +
   1.896 +	int fileSize = info.uncompressed_size;
   1.897 +	if (size == 0)
   1.898 +		size = fileSize;
   1.899 +	r = unzOpenCurrentFile(unz);
   1.900 +
   1.901 +	if (r != UNZ_OK)
   1.902 +	{
   1.903 +		unzClose(unz);
   1.904 +		systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), buffer);
   1.905 +		return NULL;
   1.906 +	}
   1.907 +
   1.908 +	u8 *image = data;
   1.909 +
   1.910 +	if (image == NULL)
   1.911 +	{
   1.912 +		image = (u8 *)malloc(utilGetSize(size));
   1.913 +		if (image == NULL)
   1.914 +		{
   1.915 +			unzCloseCurrentFile(unz);
   1.916 +			unzClose(unz);
   1.917 +			systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
   1.918 +			              "data");
   1.919 +			return NULL;
   1.920 +		}
   1.921 +		size = fileSize;
   1.922 +	}
   1.923 +	int read = fileSize <= size ? fileSize : size;
   1.924 +	r = unzReadCurrentFile(unz,
   1.925 +	                       image,
   1.926 +	                       read);
   1.927 +
   1.928 +	unzCloseCurrentFile(unz);
   1.929 +	unzClose(unz);
   1.930 +
   1.931 +	if (r != (int)read)
   1.932 +	{
   1.933 +		systemMessage(MSG_ERROR_READING_IMAGE,
   1.934 +		              N_("Error reading image %s"), buffer);
   1.935 +		if (data == NULL)
   1.936 +			free(image);
   1.937 +		return NULL;
   1.938 +	}
   1.939 +
   1.940 +	size = fileSize;
   1.941 +
   1.942 +	return image;
   1.943 +}
   1.944 +
   1.945 +static u8 *utilLoadGzipFile(const char *file,
   1.946 +                            bool (*accept)(const char *),
   1.947 +                            u8 *data,
   1.948 +                            int &size)
   1.949 +{
   1.950 +	FILE *f = fopen(file, "rb");
   1.951 +
   1.952 +	if (f == NULL)
   1.953 +	{
   1.954 +		systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
   1.955 +		return NULL;
   1.956 +	}
   1.957 +
   1.958 +	fseek(f, -4, SEEK_END);
   1.959 +	int fileSize = fgetc(f) | (fgetc(f) << 8) | (fgetc(f) << 16) | (fgetc(f) << 24);
   1.960 +	fclose(f);
   1.961 +	if (size == 0)
   1.962 +		size = fileSize;
   1.963 +
   1.964 +	gzFile gz = gzopen(file, "rb");
   1.965 +
   1.966 +	if (gz == NULL)
   1.967 +	{
   1.968 +		// should not happen, but who knows?
   1.969 +		systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
   1.970 +		return NULL;
   1.971 +	}
   1.972 +
   1.973 +	u8 *image = data;
   1.974 +
   1.975 +	if (image == NULL)
   1.976 +	{
   1.977 +		image = (u8 *)malloc(utilGetSize(size));
   1.978 +		if (image == NULL)
   1.979 +		{
   1.980 +			systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
   1.981 +			              "data");
   1.982 +			fclose(f);
   1.983 +			return NULL;
   1.984 +		}
   1.985 +		size = fileSize;
   1.986 +	}
   1.987 +	int read = fileSize <= size ? fileSize : size;
   1.988 +	int r	 = gzread(gz, image, read);
   1.989 +	gzclose(gz);
   1.990 +
   1.991 +	if (r != (int)read)
   1.992 +	{
   1.993 +		systemMessage(MSG_ERROR_READING_IMAGE,
   1.994 +		              N_("Error reading image %s"), file);
   1.995 +		if (data == NULL)
   1.996 +			free(image);
   1.997 +		return NULL;
   1.998 +	}
   1.999 +
  1.1000 +	size = fileSize;
  1.1001 +
  1.1002 +	return image;
  1.1003 +}
  1.1004 +
  1.1005 +#if 0
  1.1006 +static u8 *utilLoadRarFile(const char *file,
  1.1007 +                           bool (*accept)(const char *),
  1.1008 +                           u8 *data,
  1.1009 +                           int &size)
  1.1010 +{
  1.1011 +	char buffer[2048];
  1.1012 +
  1.1013 +	ArchiveList_struct *rarList = NULL;
  1.1014 +	if (urarlib_list((void *)file, (ArchiveList_struct *)&rarList))
  1.1015 +	{
  1.1016 +		ArchiveList_struct *p = rarList;
  1.1017 +
  1.1018 +		bool found = false;
  1.1019 +		while (p)
  1.1020 +		{
  1.1021 +			if (accept(p->item.Name))
  1.1022 +			{
  1.1023 +				strcpy(buffer, p->item.Name);
  1.1024 +				found = true;
  1.1025 +				break;
  1.1026 +			}
  1.1027 +			p = p->next;
  1.1028 +		}
  1.1029 +		if (found)
  1.1030 +		{
  1.1031 +			void *memory		= NULL;
  1.1032 +			unsigned long lsize = 0;
  1.1033 +			size = p->item.UnpSize;
  1.1034 +			int r = urarlib_get((void *)&memory, &lsize, buffer, (void *)file, "");
  1.1035 +			if (!r)
  1.1036 +			{
  1.1037 +				systemMessage(MSG_ERROR_READING_IMAGE,
  1.1038 +				              N_("Error reading image %s"), buffer);
  1.1039 +				urarlib_freelist(rarList);
  1.1040 +				return NULL;
  1.1041 +			}
  1.1042 +			u8 *image = (u8 *)memory;
  1.1043 +			if (data != NULL)
  1.1044 +			{
  1.1045 +				memcpy(image, data, size);
  1.1046 +			}
  1.1047 +			urarlib_freelist(rarList);
  1.1048 +			return image;
  1.1049 +		}
  1.1050 +		systemMessage(MSG_NO_IMAGE_ON_ZIP,
  1.1051 +		              N_("No image found on RAR file %s"), file);
  1.1052 +		urarlib_freelist(rarList);
  1.1053 +		return NULL;
  1.1054 +	}
  1.1055 +	// nothing found
  1.1056 +	return NULL;
  1.1057 +}
  1.1058 +
  1.1059 +#endif
  1.1060 +
  1.1061 +// the caller is responsible for caling free(return value) to release the memory
  1.1062 +u8 *utilLoad(const char *file,
  1.1063 +             bool (*accept)(const char *),
  1.1064 +             u8 *data,
  1.1065 +             int &size)
  1.1066 +{
  1.1067 +	if (utilIsZipFile(file))
  1.1068 +	{
  1.1069 +		return utilLoadFromZip(file, accept, data, size);
  1.1070 +	}
  1.1071 +	if (utilIsGzipFile(file))
  1.1072 +	{
  1.1073 +		return utilLoadGzipFile(file, accept, data, size);
  1.1074 +	}
  1.1075 +#if 0
  1.1076 +	if (utilIsRarFile(file))
  1.1077 +	{
  1.1078 +		return utilLoadRarFile(file, accept, data, size);
  1.1079 +	}
  1.1080 +#endif
  1.1081 +
  1.1082 +	u8 *image = data;
  1.1083 +
  1.1084 +	FILE *f = fopen(file, "rb");
  1.1085 +
  1.1086 +	if (!f)
  1.1087 +	{
  1.1088 +		systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file);
  1.1089 +		return NULL;
  1.1090 +	}
  1.1091 +
  1.1092 +	fseek(f, 0, SEEK_END);
  1.1093 +	int fileSize = ftell(f);
  1.1094 +	fseek(f, 0, SEEK_SET);
  1.1095 +	if (size == 0)
  1.1096 +		size = fileSize;
  1.1097 +
  1.1098 +	if (image == NULL)
  1.1099 +	{
  1.1100 +		image = (u8 *)malloc(utilGetSize(size));
  1.1101 +		if (image == NULL)
  1.1102 +		{
  1.1103 +			systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"),
  1.1104 +			              "data");
  1.1105 +			fclose(f);
  1.1106 +			return NULL;
  1.1107 +		}
  1.1108 +		size = fileSize;
  1.1109 +	}
  1.1110 +	int read = fileSize <= size ? fileSize : size;
  1.1111 +	int r	 = fread(image, 1, read, f);
  1.1112 +	fclose(f);
  1.1113 +
  1.1114 +	if (r != (int)read)
  1.1115 +	{
  1.1116 +		systemMessage(MSG_ERROR_READING_IMAGE,
  1.1117 +		              N_("Error reading image %s"), file);
  1.1118 +		if (data == NULL)
  1.1119 +			free(image);
  1.1120 +		return NULL;
  1.1121 +	}
  1.1122 +
  1.1123 +	size = fileSize;
  1.1124 +
  1.1125 +	return image;
  1.1126 +}
  1.1127 +
  1.1128 +void utilWriteInt(gzFile gzFile, int32 i)
  1.1129 +{
  1.1130 +	utilGzWrite(gzFile, &i, sizeof(int32));
  1.1131 +}
  1.1132 +
  1.1133 +int32 utilReadInt(gzFile gzFile)
  1.1134 +{
  1.1135 +	int32 i = 0;
  1.1136 +	utilGzRead(gzFile, &i, sizeof(int32));
  1.1137 +	return i;
  1.1138 +}
  1.1139 +
  1.1140 +void utilReadData(gzFile gzFile, variable_desc *data)
  1.1141 +{
  1.1142 +	while (data->address)
  1.1143 +	{
  1.1144 +		utilGzRead(gzFile, data->address, data->size);
  1.1145 +		data++;
  1.1146 +	}
  1.1147 +}
  1.1148 +
  1.1149 +void utilWriteData(gzFile gzFile, variable_desc *data)
  1.1150 +{
  1.1151 +	while (data->address)
  1.1152 +	{
  1.1153 +		utilGzWrite(gzFile, data->address, data->size);
  1.1154 +		data++;
  1.1155 +	}
  1.1156 +}
  1.1157 +
  1.1158 +gzFile utilGzOpen(const char *file, const char *mode)
  1.1159 +{
  1.1160 +	utilGzWriteFunc = gzWrite;
  1.1161 +	utilGzReadFunc	= gzread;
  1.1162 +	utilGzCloseFunc = gzclose;
  1.1163 +	utilGzSeekFunc	= gzseek;
  1.1164 +	utilGzTellFunc	= gztell;
  1.1165 +
  1.1166 +	return gzopen(file, mode);
  1.1167 +}
  1.1168 +
  1.1169 +gzFile utilGzReopen(int id, const char *mode)
  1.1170 +{
  1.1171 +	utilGzWriteFunc = gzWrite;
  1.1172 +	utilGzReadFunc	= gzread;
  1.1173 +	utilGzCloseFunc = gzclose;
  1.1174 +	utilGzSeekFunc	= gzseek;
  1.1175 +	utilGzTellFunc	= gztell;
  1.1176 +
  1.1177 +	return gzdopen(id, mode);
  1.1178 +}
  1.1179 +
  1.1180 +gzFile utilMemGzOpen(char *memory, int available, char *mode)
  1.1181 +{
  1.1182 +	utilGzWriteFunc = memgzwrite;
  1.1183 +	utilGzReadFunc	= memgzread;
  1.1184 +	utilGzCloseFunc = memgzclose;
  1.1185 +	utilGzSeekFunc	= NULL;	// FIXME: not implemented...
  1.1186 +	utilGzTellFunc	= memtell;
  1.1187 +
  1.1188 +	return memgzopen(memory, available, mode);
  1.1189 +}
  1.1190 +
  1.1191 +int utilGzWrite(gzFile file, voidp buffer, unsigned int len)
  1.1192 +{
  1.1193 +	return utilGzWriteFunc(file, buffer, len);
  1.1194 +}
  1.1195 +
  1.1196 +int utilGzRead(gzFile file, voidp buffer, unsigned int len)
  1.1197 +{
  1.1198 +	return utilGzReadFunc(file, buffer, len);
  1.1199 +}
  1.1200 +
  1.1201 +int utilGzClose(gzFile file)
  1.1202 +{
  1.1203 +	return utilGzCloseFunc(file);
  1.1204 +}
  1.1205 +
  1.1206 +z_off_t utilGzSeek(gzFile file, z_off_t offset, int whence)
  1.1207 +{
  1.1208 +	return utilGzSeekFunc(file, offset, whence);
  1.1209 +}
  1.1210 +
  1.1211 +z_off_t utilGzTell(gzFile file)
  1.1212 +{
  1.1213 +	return utilGzTellFunc(file);
  1.1214 +}
  1.1215 +
  1.1216 +void utilGBAFindSave(const u8 *data, const int size)
  1.1217 +{
  1.1218 +	u32 *p		   = (u32 *)data;
  1.1219 +	u32 *end	   = (u32 *)(data + size);
  1.1220 +	int	 saveType  = 0;
  1.1221 +	int	 flashSize = 0x10000;
  1.1222 +	bool rtcFound  = false;
  1.1223 +
  1.1224 +	while (p  < end)
  1.1225 +	{
  1.1226 +		u32 d = READ32LE(p);
  1.1227 +
  1.1228 +		if (d == 0x52504545)
  1.1229 +		{
  1.1230 +			if (memcmp(p, "EEPROM_", 7) == 0)
  1.1231 +			{
  1.1232 +				if (saveType == 0)
  1.1233 +					saveType = 1;
  1.1234 +			}
  1.1235 +		}
  1.1236 +		else if (d == 0x4D415253)
  1.1237 +		{
  1.1238 +			if (memcmp(p, "SRAM_", 5) == 0)
  1.1239 +			{
  1.1240 +				if (saveType == 0)
  1.1241 +					saveType = 2;
  1.1242 +			}
  1.1243 +		}
  1.1244 +		else if (d == 0x53414C46)
  1.1245 +		{
  1.1246 +			if (memcmp(p, "FLASH1M_", 8) == 0)
  1.1247 +			{
  1.1248 +				if (saveType == 0)
  1.1249 +				{
  1.1250 +					saveType  = 3;
  1.1251 +					flashSize = 0x20000;
  1.1252 +				}
  1.1253 +			}
  1.1254 +			else if (memcmp(p, "FLASH", 5) == 0)
  1.1255 +			{
  1.1256 +				if (saveType == 0)
  1.1257 +				{
  1.1258 +					saveType  = 3;
  1.1259 +					flashSize = 0x10000;
  1.1260 +				}
  1.1261 +			}
  1.1262 +		}
  1.1263 +		else if (d == 0x52494953)
  1.1264 +		{
  1.1265 +			if (memcmp(p, "SIIRTC_V", 8) == 0)
  1.1266 +				rtcFound = true;
  1.1267 +		}
  1.1268 +		p++;
  1.1269 +	}
  1.1270 +	// if no matches found, then set it to NONE
  1.1271 +	if (saveType == 0)
  1.1272 +	{
  1.1273 +		saveType = 5;
  1.1274 +	}
  1.1275 +	rtcEnable(rtcFound);
  1.1276 +	cpuSaveType = saveType;
  1.1277 +	flashSetSize(flashSize);
  1.1278 +}
  1.1279 +
  1.1280 +void utilUpdateSystemColorMaps()
  1.1281 +{
  1.1282 +	switch (systemColorDepth)
  1.1283 +	{
  1.1284 +	case 16:
  1.1285 +	{
  1.1286 +		for (int i = 0; i < 0x10000; i++)
  1.1287 +		{
  1.1288 +			systemColorMap16[i] = ((i & 0x1f) << systemRedShift) |
  1.1289 +			                      (((i & 0x3e0) >> 5) << systemGreenShift) |
  1.1290 +			                      (((i & 0x7c00) >> 10) << systemBlueShift);
  1.1291 +		}
  1.1292 +		break;
  1.1293 +	}
  1.1294 +	case 24:
  1.1295 +	case 32:
  1.1296 +	{
  1.1297 +		for (int i = 0; i < 0x10000; i++)
  1.1298 +		{
  1.1299 +			systemColorMap32[i] = ((i & 0x1f) << systemRedShift) |
  1.1300 +			                      (((i & 0x3e0) >> 5) << systemGreenShift) |
  1.1301 +			                      (((i & 0x7c00) >> 10) << systemBlueShift);
  1.1302 +		}
  1.1303 +		break;
  1.1304 +	}
  1.1305 +	}
  1.1306 +}
  1.1307 +
  1.1308 +//// BIOS stuff
  1.1309 +// systemType uses the same enum values as gbEmulatorType does
  1.1310 +
  1.1311 +bool utilLoadBIOS(u8 *bios, const char *biosFileName, int systemType)
  1.1312 +{
  1.1313 +	if (bios == NULL || strlen(biosFileName) == 0)
  1.1314 +		return false;
  1.1315 +
  1.1316 +	if (systemType == 4)
  1.1317 +	{
  1.1318 +		int biosSize = 0x4000;
  1.1319 +		if (utilLoad(biosFileName, utilIsGBABios, bios, biosSize))
  1.1320 +		{
  1.1321 +			if (biosSize == 0x4000)
  1.1322 +				return true;
  1.1323 +		}
  1.1324 +	}
  1.1325 +	else
  1.1326 +	{
  1.1327 +		int biosSize = 0x100;
  1.1328 +		if (utilLoad(biosFileName, utilIsGBBios, bios, biosSize))
  1.1329 +		{
  1.1330 +			if (biosSize == 0x100)
  1.1331 +				return true;
  1.1332 +		}
  1.1333 +	}
  1.1334 +
  1.1335 +	return false;
  1.1336 +}
  1.1337 +
  1.1338 +bool utilCheckBIOS(const char *biosFileName, int systemType)
  1.1339 +{
  1.1340 +	if (strlen(biosFileName) == 0)
  1.1341 +		return false;
  1.1342 +
  1.1343 +	u8 * tempBIOS = (u8 *)malloc(systemType == 4 ? 0x4000 : 0x100);
  1.1344 +	bool result	  = utilLoadBIOS(tempBIOS, biosFileName, systemType);
  1.1345 +	free(tempBIOS);
  1.1346 +
  1.1347 +	return result;
  1.1348 +}
  1.1349 +
  1.1350 +#if 0
  1.1351 +// returns the checksum of the BIOS that will be loaded after the next restart
  1.1352 +u16 utilCalcBIOSChecksum(const u8 *bios, int systemType)
  1.1353 +{
  1.1354 +	u32	biosChecksum = 0;
  1.1355 +	if (bios)
  1.1356 +	{
  1.1357 +		int biosSize	= (systemType == 4 ? 0x4000 : 0x100);
  1.1358 +		const u16 *data = reinterpret_cast<const u16 *>(bios);
  1.1359 +		for (int i = biosSize; i > 0; i -= 2)
  1.1360 +			biosChecksum += *data++;
  1.1361 +	}
  1.1362 +
  1.1363 +	while ((biosChecksum >> 16) & 0xFFFF)
  1.1364 +		biosChecksum = (biosChecksum &0xFFFF) + ((biosChecksum >> 16) & 0xFFFF);
  1.1365 +
  1.1366 +	return biosChecksum & 0xFFFF;
  1.1367 +}
  1.1368 +#else
  1.1369 +// returns the checksum of the BIOS that will be loaded after the next restart
  1.1370 +u16 utilCalcBIOSChecksum(const u8 *bios, int systemType)
  1.1371 +{
  1.1372 +	u32	biosChecksum = 0;
  1.1373 +	if (bios)
  1.1374 +	{
  1.1375 +		int biosSize	= (systemType == 4 ? 0x4000 : 0x100);
  1.1376 +		const u32 *data = reinterpret_cast<const u32 *>(bios);
  1.1377 +		for (int i = biosSize; i > 0; i -= 4)
  1.1378 +			biosChecksum += *data++;
  1.1379 +	}
  1.1380 +
  1.1381 +	return biosChecksum & 0xFFFF;
  1.1382 +}
  1.1383 +#endif
  1.1384 +
  1.1385 +// returns the checksum of the BIOS file
  1.1386 +u16 utilCalcBIOSFileChecksum(const char *biosFileName, int systemType)
  1.1387 +{
  1.1388 +	if (strlen(biosFileName) == 0)
  1.1389 +		return 0;
  1.1390 +
  1.1391 +	u16		  biosChecksum = 0;
  1.1392 +	const int biosSize	   = (systemType == 4 ? 0x4000 : 0x100);
  1.1393 +	u8 *	  tempBIOS	   = (u8 *)malloc(biosSize);
  1.1394 +	bool	  hasBIOS	   = utilLoadBIOS(tempBIOS, biosFileName, systemType);
  1.1395 +	if (hasBIOS)
  1.1396 +	{
  1.1397 +		biosChecksum = utilCalcBIOSChecksum(tempBIOS, systemType);
  1.1398 +	}
  1.1399 +	free(tempBIOS);
  1.1400 +
  1.1401 +	return biosChecksum;
  1.1402 +}
  1.1403 +