# HG changeset patch # User Robert McIntyre # Date 1330792287 21600 # Node ID f9f4f1b99eed56d476ae15474329ae8d2e879e0e # Parent 8ced16adf2e167b0abdad8df489d4267e2ee8eb9 importing src directory diff -r 8ced16adf2e1 -r f9f4f1b99eed src/AutoBuild.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/AutoBuild.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __AUTOBUILD_H__ +#define __AUTOBUILD_H__ +#include "version.h" +//change the FALSE to TRUE for autoincrement of build number +#define INCREMENT_VERSION FALSE +#define FILEVER 1,8,0,600 +#define PRODUCTVER 1,8,0,600 +#define STRFILEVER "1, 8, 0, 600\0" +#define STRPRODUCTVER "1, 8, 0, 600\0" +#endif //__AUTOBUILD_H__ + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,15 @@ +CORE_SUBDIRS = gba gb common filters + +EXTRA_SUBDIRS = prof sdl gtk lua + +SUBDIRS = $(CORE_SUBDIRS) @VBA_SRC_EXTRA@ + +DIST_SUBDIRS = $(CORE_SUBDIRS) $(EXTRA_SUBDIRS) + +dist_sysconf_DATA = VisualBoyAdvance.cfg + +EXTRA_DIST = \ + win32 + +dist-hook: + rm -rf `find $(distdir)/win32 -name CVS` diff -r 8ced16adf2e1 -r f9f4f1b99eed src/NLS.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/NLS.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,52 @@ +#ifndef VBS_NLS_H +#define VBA_NLS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#define N_(String) (String) + +#define MSG_UNSUPPORTED_VBA_SGM 1 +#define MSG_CANNOT_LOAD_SGM 2 +#define MSG_SAVE_GAME_NOT_USING_BIOS 3 +#define MSG_SAVE_GAME_USING_BIOS 4 +#define MSG_UNSUPPORTED_SAVE_TYPE 5 +#define MSG_CANNOT_OPEN_FILE 6 +#define MSG_BAD_ZIP_FILE 7 +#define MSG_NO_IMAGE_ON_ZIP 8 +#define MSG_ERROR_OPENING_IMAGE 9 +#define MSG_ERROR_READING_IMAGE 10 +#define MSG_UNSUPPORTED_BIOS_FUNCTION 11 +#define MSG_INVALID_BIOS_FILE_SIZE 12 +#define MSG_INVALID_CHEAT_CODE 13 +#define MSG_UNKNOWN_ARM_OPCODE 14 +#define MSG_UNKNOWN_THUMB_OPCODE 15 +#define MSG_ERROR_CREATING_FILE 16 +#define MSG_FAILED_TO_READ_SGM 17 +#define MSG_FAILED_TO_READ_RTC 18 +#define MSG_UNSUPPORTED_VB_SGM 19 +#define MSG_CANNOT_LOAD_SGM_FOR 20 +#define MSG_ERROR_OPENING_IMAGE_FROM 21 +#define MSG_ERROR_READING_IMAGE_FROM 22 +#define MSG_UNSUPPORTED_ROM_SIZE 23 +#define MSG_UNSUPPORTED_RAM_SIZE 24 +#define MSG_UNKNOWN_CARTRIDGE_TYPE 25 +#define MSG_MAXIMUM_NUMBER_OF_CHEATS 26 +#define MSG_INVALID_GAMESHARK_CODE 27 +#define MSG_INVALID_GAMEGENIE_CODE 28 +#define MSG_INVALID_CHEAT_TO_REMOVE 29 +#define MSG_INVALID_CHEAT_CODE_ADDRESS 30 +#define MSG_UNSUPPORTED_CHEAT_LIST_VERSION 31 +#define MSG_UNSUPPORTED_CHEAT_LIST_TYPE 32 +#define MSG_INVALID_GSA_CODE 33 +#define MSG_CANNOT_IMPORT_SNAPSHOT_FOR 34 +#define MSG_UNSUPPORTED_SNAPSHOT_FILE 35 +#define MSG_UNSUPPORTED_ARM_MODE 36 +#define MSG_UNSUPPORTED_CODE_FILE 37 +#define MSG_GBA_CODE_WARNING 38 +#define MSG_INVALID_CBA_CODE 39 +#define MSG_CBA_CODE_WARNING 40 +#define MSG_OUT_OF_MEMORY 41 + +#endif // VBA_NLS_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/Port.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Port.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,178 @@ +#ifndef VBA_PORT_H +#define VBA_PORT_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include + +#ifndef NULL +#define NULL 0 +#endif + +typedef unsigned char bool8; + +#ifdef HAVE_STDINT_H +#include + +typedef int8_t int8; +typedef uint8_t uint8; +typedef int16_t int16; +typedef uint16_t uint16; +typedef int32_t int32; +typedef uint32_t uint32; +typedef int64_t int64; +typedef uint64_t uint64; +typedef intptr_t pint; + +#else /* Don't have stdint.h */ + +#ifdef PTR_NOT_INT +typedef long pint; +#else /* pointer is int */ +typedef int pint; +#endif /* PTR_NOT_INT */ + +/* FIXME: Refactor this by moving out the BORLAND part and unifying typedefs */ +#ifndef WIN32 +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef signed char int8; +typedef short int16; +typedef int int32; +typedef unsigned int uint32; +# ifdef __GNUC__ /* long long is not part of ISO C++ */ +__extension__ typedef long long int64; +__extension__ typedef unsigned long long uint64; +# else +typedef long long int64; +typedef unsigned long long uint64; +# endif +#else /* WIN32 */ + +# ifdef __BORLANDC__ +# include +# else + +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef signed char int8; +typedef short int16; + +# ifndef WSAAPI +/* winsock2.h typedefs int32 as well. */ +typedef long int32; +# endif + +typedef unsigned int uint32; + +# endif /* __BORLANDC__ */ + +typedef __int64 int64; +typedef unsigned __int64 uint64; + +#endif /* WIN32 */ +#endif /* HAVE_STDINT_H */ + +#ifndef WIN32 + +#ifndef PATH_MAX +#define PATH_MAX 1024 +#endif + +#define _MAX_DIR PATH_MAX +#define _MAX_DRIVE 1 +#define _MAX_FNAME PATH_MAX +#define _MAX_EXT PATH_MAX +#define _MAX_PATH PATH_MAX + +#define ZeroMemory(a, b) memset((a), 0, (b)) + +void _makepath(char *path, const char *drive, const char *dir, + const char *fname, const char *ext); +void _splitpath(const char *path, char *drive, char *dir, char *fname, + char *ext); +#else /* WIN32 */ +#define strcasecmp stricmp +#define strncasecmp strnicmp +#endif + +typedef uint8 u8; +typedef uint16 u16; +typedef uint32 u32; +typedef uint64 u64; +typedef int8 s8; +typedef int16 s16; +typedef int32 s32; +typedef int64 s64; + +// for consistency +static inline u8 swap8(u8 v) +{ + return v; +} + +// swaps a 16-bit value +static inline u16 swap16(u16 v) +{ + return (v<<8)|(v>>8); +} + +// swaps a 32-bit value +static inline u32 swap32(u32 v) +{ + return (v<<24)|((v<<8)&0xff0000)|((v>>8)&0xff00)|(v>>24); +} + +#define READ8LE(x) \ + *((u8 *)x) + +#define WRITE8LE(x, v) \ + *((u8 *)x) = (v) + +#ifdef WORDS_BIGENDIAN +#if defined(__GNUC__) && defined(__ppc__) + +#define READ16LE(base) \ + ({ unsigned short lhbrxResult; \ + __asm__("lhbrx %0, 0, %1" : "=r" (lhbrxResult) : "r" (base) : "memory"); \ + lhbrxResult; }) + +#define READ32LE(base) \ + ({ unsigned long lwbrxResult; \ + __asm__("lwbrx %0, 0, %1" : "=r" (lwbrxResult) : "r" (base) : "memory"); \ + lwbrxResult; }) + +#define WRITE16LE(base, value) \ + __asm__("sthbrx %0, 0, %1" : : "r" (value), "r" (base) : "memory") + +#define WRITE32LE(base, value) \ + __asm__("stwbrx %0, 0, %1" : : "r" (value), "r" (base) : "memory") + +#else +#define READ16LE(x) \ + swap16(*((u16 *)(x))) +#define READ32LE(x) \ + swap32(*((u32 *)(x))) +#define WRITE16LE(x, v) \ + *((u16 *)x) = swap16((v)) +#define WRITE32LE(x, v) \ + *((u32 *)x) = swap32((v)) +#endif +#else +#define READ16LE(x) \ + *((u16 *)x) +#define READ32LE(x) \ + *((u32 *)x) +#define WRITE16LE(x, v) \ + *((u16 *)x) = (v) +#define WRITE32LE(x, v) \ + *((u32 *)x) = (v) +#endif + +#ifndef CTASSERT +#define CTASSERT(x) typedef char __assert ## y[(x) ? 1 : -1]; +#endif + +#endif // VBA_PORT_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/CHANGE-LOG.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/CHANGE-LOG.txt Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,55 @@ +ver 1.3.3 +------- +change condition compile of do_recursion in SFMT.c + +ver 1.3.2 +------- +bug fix to_res53_mix and genrand_res53_mix. + +ver 1.3.1 +------- +gcc compile option changed form -O9 to -O3. +add functions genrand_res53_mix and to_res53_mix. +bug fix about definition of ALWAYS_INLINE. +add new definition PRE_ALWAYS for MSC. + +ver 1.3 +------- +bug fixed: -DONLY64 without -DBIG_ENIAN64 had been generating +wrong sequence. +bug fixed: There is no documentation about BIG_ENDIAN64. +add automatic endian check by __BIG_ENDIAN__ predefined macro. +bug fixed: change == in check.sh to = +add SFMT-params216091.h +add AltiVec parameter format for systems which are not osx. +change Makefile for systems which are not osx and support AltiVec. +change sample2 of howto-compile for Free BSD. +change source files for BORLANDC and Visual Studio. +change period certification code more smart. +add params directory. + +ver 1.2.1 +------- +Fix typo in SFMT-alti.c SFMT-sse2.c +marge SFMT-alti.c and SFMT-alti.h into SFMT-alti.h +marge SFMT-sse2.c and SFMT-sse2.h into SFMT-sse2.h +This version is not released. + +ver 1.2 +------- +Support many periods: 2^{607}, 2^{1279}, 2^{2281}, 2^{4253}, 2^{11213}, +2^{19937}, 2^{44497}, 2^{86243}, 2^{132049} +Fix typo in LICENSE.txt. +Add cast to vec_perm for SFMT-alti.c, SFMT-alti64.c. +combine source codes. + +ver 1.1 +------- +The period certification method is changed from constant to function. +The convert functions from 32-bit and 64-bit integer to double are added. +The documentation is changed. +Sample programs are added. + +ver 1.0 +------- +The first version. diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/LICENSE.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/LICENSE.txt Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,29 @@ +Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima +University. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the Hiroshima University nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/README.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/README.txt Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,22 @@ + ================================================================= + SFMT ver. 1.3.3 + SIMD oriented Fast Mersenne Twister(SFMT) + + Mutsuo Saito (Hiroshima University) and + Makoto Matsumoto (Hiroshima University) + + Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima + University. All rights reserved. + + The (modified) BSD License is applied to this software, see LICENSE.txt + ================================================================= + + To see documents, see html/index.html. + + To make test program, see html/howto-compile.html + + If you want to redistribute and/or change source files, see LICENSE.txt. + + When you change these files and redistribute them, PLEASE write your + e-mail address in redistribution and write to contact YOU first if + users of your changed source encounter troubles. diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-alti.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-alti.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,156 @@ +/** + * @file SFMT-alti.h + * + * @brief SIMD oriented Fast Mersenne Twister(SFMT) + * pseudorandom number generator + * + * @author Mutsuo Saito (Hiroshima University) + * @author Makoto Matsumoto (Hiroshima University) + * + * Copyright (C) 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima + * University. All rights reserved. + * + * The new BSD License is applied to this software. + * see LICENSE.txt + */ + +#ifndef SFMT_ALTI_H +#define SFMT_ALTI_H + +inline static vector unsigned int vec_recursion(vector unsigned int a, + vector unsigned int b, + vector unsigned int c, + vector unsigned int d) + ALWAYSINLINE; + +/** + * This function represents the recursion formula in AltiVec and BIG ENDIAN. + * @param a a 128-bit part of the interal state array + * @param b a 128-bit part of the interal state array + * @param c a 128-bit part of the interal state array + * @param d a 128-bit part of the interal state array + * @return output + */ +inline static vector unsigned int vec_recursion(vector unsigned int a, + vector unsigned int b, + vector unsigned int c, + vector unsigned int d) { + + const vector unsigned int sl1 = ALTI_SL1; + const vector unsigned int sr1 = ALTI_SR1; +#ifdef ONLY64 + const vector unsigned int mask = ALTI_MSK64; + const vector unsigned char perm_sl = ALTI_SL2_PERM64; + const vector unsigned char perm_sr = ALTI_SR2_PERM64; +#else + const vector unsigned int mask = ALTI_MSK; + const vector unsigned char perm_sl = ALTI_SL2_PERM; + const vector unsigned char perm_sr = ALTI_SR2_PERM; +#endif + vector unsigned int v, w, x, y, z; + x = vec_perm(a, (vector unsigned int)perm_sl, perm_sl); + v = a; + y = vec_sr(b, sr1); + z = vec_perm(c, (vector unsigned int)perm_sr, perm_sr); + w = vec_sl(d, sl1); + z = vec_xor(z, w); + y = vec_and(y, mask); + v = vec_xor(v, x); + z = vec_xor(z, y); + z = vec_xor(z, v); + return z; +} + +/** + * This function fills the internal state array with pseudorandom + * integers. + */ +inline static void gen_rand_all(void) { + int i; + vector unsigned int r, r1, r2; + + r1 = sfmt[N - 2].s; + r2 = sfmt[N - 1].s; + for (i = 0; i < N - POS1; i++) { + r = vec_recursion(sfmt[i].s, sfmt[i + POS1].s, r1, r2); + sfmt[i].s = r; + r1 = r2; + r2 = r; + } + for (; i < N; i++) { + r = vec_recursion(sfmt[i].s, sfmt[i + POS1 - N].s, r1, r2); + sfmt[i].s = r; + r1 = r2; + r2 = r; + } +} + +/** + * This function fills the user-specified array with pseudorandom + * integers. + * + * @param array an 128-bit array to be filled by pseudorandom numbers. + * @param size number of 128-bit pesudorandom numbers to be generated. + */ +inline static void gen_rand_array(w128_t *array, int size) { + int i, j; + vector unsigned int r, r1, r2; + + r1 = sfmt[N - 2].s; + r2 = sfmt[N - 1].s; + for (i = 0; i < N - POS1; i++) { + r = vec_recursion(sfmt[i].s, sfmt[i + POS1].s, r1, r2); + array[i].s = r; + r1 = r2; + r2 = r; + } + for (; i < N; i++) { + r = vec_recursion(sfmt[i].s, array[i + POS1 - N].s, r1, r2); + array[i].s = r; + r1 = r2; + r2 = r; + } + /* main loop */ + for (; i < size - N; i++) { + r = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2); + array[i].s = r; + r1 = r2; + r2 = r; + } + for (j = 0; j < 2 * N - size; j++) { + sfmt[j].s = array[j + size - N].s; + } + for (; i < size; i++) { + r = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2); + array[i].s = r; + sfmt[j++].s = r; + r1 = r2; + r2 = r; + } +} + +#ifndef ONLY64 +#if defined(__APPLE__) +#define ALTI_SWAP (vector unsigned char) \ + (4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11) +#else +#define ALTI_SWAP {4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11} +#endif +/** + * This function swaps high and low 32-bit of 64-bit integers in user + * specified array. + * + * @param array an 128-bit array to be swaped. + * @param size size of 128-bit array. + */ +inline static void swap(w128_t *array, int size) { + int i; + const vector unsigned char perm = ALTI_SWAP; + + for (i = 0; i < size; i++) { + array[i].s = vec_perm(array[i].s, (vector unsigned int)perm, perm); + } +} +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,97 @@ +#ifndef SFMT_PARAMS_H +#define SFMT_PARAMS_H + +#if !defined(MEXP) +#ifdef __GNUC__ + #warning "MEXP is not defined. I assume MEXP is 19937." +#endif + #define MEXP 19937 +#endif +/*----------------- + BASIC DEFINITIONS + -----------------*/ +/** Mersenne Exponent. The period of the sequence + * is a multiple of 2^MEXP-1. + * #define MEXP 19937 */ +/** SFMT generator has an internal state array of 128-bit integers, + * and N is its size. */ +#define N (MEXP / 128 + 1) +/** N32 is the size of internal state array when regarded as an array + * of 32-bit integers.*/ +#define N32 (N * 4) +/** N64 is the size of internal state array when regarded as an array + * of 64-bit integers.*/ +#define N64 (N * 2) + +/*---------------------- + the parameters of SFMT + following definitions are in paramsXXXX.h file. + ----------------------*/ +/** the pick up position of the array. +#define POS1 122 +*/ + +/** the parameter of shift left as four 32-bit registers. +#define SL1 18 + */ + +/** the parameter of shift left as one 128-bit register. + * The 128-bit integer is shifted by (SL2 * 8) bits. +#define SL2 1 +*/ + +/** the parameter of shift right as four 32-bit registers. +#define SR1 11 +*/ + +/** the parameter of shift right as one 128-bit register. + * The 128-bit integer is shifted by (SL2 * 8) bits. +#define SR2 1 +*/ + +/** A bitmask, used in the recursion. These parameters are introduced + * to break symmetry of SIMD. +#define MSK1 0xdfffffefU +#define MSK2 0xddfecb7fU +#define MSK3 0xbffaffffU +#define MSK4 0xbffffff6U +*/ + +/** These definitions are part of a 128-bit period certification vector. +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0xc98e126aU +*/ + +#if MEXP == 607 + #include "SFMT-params607.h" +#elif MEXP == 1279 + #include "SFMT-params1279.h" +#elif MEXP == 2281 + #include "SFMT-params2281.h" +#elif MEXP == 4253 + #include "SFMT-params4253.h" +#elif MEXP == 11213 + #include "SFMT-params11213.h" +#elif MEXP == 19937 + #include "SFMT-params19937.h" +#elif MEXP == 44497 + #include "SFMT-params44497.h" +#elif MEXP == 86243 + #include "SFMT-params86243.h" +#elif MEXP == 132049 + #include "SFMT-params132049.h" +#elif MEXP == 216091 + #include "SFMT-params216091.h" +#else +#ifdef __GNUC__ + #error "MEXP is not valid." + #undef MEXP +#else + #undef MEXP +#endif + +#endif + +#endif /* SFMT_PARAMS_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params11213.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params11213.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS11213_H +#define SFMT_PARAMS11213_H + +#define POS1 68 +#define SL1 14 +#define SL2 3 +#define SR1 7 +#define SR2 3 +#define MSK1 0xeffff7fbU +#define MSK2 0xffffffefU +#define MSK3 0xdfdfbfffU +#define MSK4 0x7fffdbfdU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0xe8148000U +#define PARITY4 0xd0c7afa3U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12} + #define ALTI_SR2_PERM64 {13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12} +#endif /* For OSX */ +#define IDSTR "SFMT-11213:68-14-3-7-3:effff7fb-ffffffef-dfdfbfff-7fffdbfd" + +#endif /* SFMT_PARAMS11213_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params1279.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params1279.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS1279_H +#define SFMT_PARAMS1279_H + +#define POS1 7 +#define SL1 14 +#define SL2 3 +#define SR1 5 +#define SR2 1 +#define MSK1 0xf7fefffdU +#define MSK2 0x7fefcfffU +#define MSK3 0xaff3ef3fU +#define MSK4 0xb5ffff7fU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0x20000000U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-1279:7-14-3-5-1:f7fefffd-7fefcfff-aff3ef3f-b5ffff7f" + +#endif /* SFMT_PARAMS1279_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params132049.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params132049.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS132049_H +#define SFMT_PARAMS132049_H + +#define POS1 110 +#define SL1 19 +#define SL2 1 +#define SR1 21 +#define SR2 1 +#define MSK1 0xffffbb5fU +#define MSK2 0xfb6ebf95U +#define MSK3 0xfffefffaU +#define MSK4 0xcff77fffU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0xcb520000U +#define PARITY4 0xc7e91c7dU + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} + #define ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-132049:110-19-1-21-1:ffffbb5f-fb6ebf95-fffefffa-cff77fff" + +#endif /* SFMT_PARAMS132049_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params19937.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params19937.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS19937_H +#define SFMT_PARAMS19937_H + +#define POS1 122 +#define SL1 18 +#define SL2 1 +#define SR1 11 +#define SR2 1 +#define MSK1 0xdfffffefU +#define MSK2 0xddfecb7fU +#define MSK3 0xbffaffffU +#define MSK4 0xbffffff6U +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0x13c9e684U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} + #define ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-19937:122-18-1-11-1:dfffffef-ddfecb7f-bffaffff-bffffff6" + +#endif /* SFMT_PARAMS19937_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params216091.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params216091.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS216091_H +#define SFMT_PARAMS216091_H + +#define POS1 627 +#define SL1 11 +#define SL2 3 +#define SR1 10 +#define SR2 1 +#define MSK1 0xbff7bff7U +#define MSK2 0xbfffffffU +#define MSK3 0xbffffa7fU +#define MSK4 0xffddfbfbU +#define PARITY1 0xf8000001U +#define PARITY2 0x89e80709U +#define PARITY3 0x3bd2b64bU +#define PARITY4 0x0c64b1e4U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-216091:627-11-3-10-1:bff7bff7-bfffffff-bffffa7f-ffddfbfb" + +#endif /* SFMT_PARAMS216091_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params2281.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params2281.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS2281_H +#define SFMT_PARAMS2281_H + +#define POS1 12 +#define SL1 19 +#define SL2 1 +#define SR1 5 +#define SR2 1 +#define MSK1 0xbff7ffbfU +#define MSK2 0xfdfffffeU +#define MSK3 0xf7ffef7fU +#define MSK4 0xf2f7cbbfU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0x41dfa600U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} + #define ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-2281:12-19-1-5-1:bff7ffbf-fdfffffe-f7ffef7f-f2f7cbbf" + +#endif /* SFMT_PARAMS2281_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params4253.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params4253.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS4253_H +#define SFMT_PARAMS4253_H + +#define POS1 17 +#define SL1 20 +#define SL2 1 +#define SR1 7 +#define SR2 1 +#define MSK1 0x9f7bffffU +#define MSK2 0x9fffff5fU +#define MSK3 0x3efffffbU +#define MSK4 0xfffff7bbU +#define PARITY1 0xa8000001U +#define PARITY2 0xaf5390a3U +#define PARITY3 0xb740b3f8U +#define PARITY4 0x6c11486dU + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8} + #define ALTI_SL2_PERM64 {1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-4253:17-20-1-7-1:9f7bffff-9fffff5f-3efffffb-fffff7bb" + +#endif /* SFMT_PARAMS4253_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params44497.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params44497.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS44497_H +#define SFMT_PARAMS44497_H + +#define POS1 330 +#define SL1 5 +#define SL2 3 +#define SR1 9 +#define SR2 3 +#define MSK1 0xeffffffbU +#define MSK2 0xdfbebfffU +#define MSK3 0xbfbf7befU +#define MSK4 0x9ffd7bffU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0xa3ac4000U +#define PARITY4 0xecc1327aU + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12} + #define ALTI_SR2_PERM64 {13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12} +#endif /* For OSX */ +#define IDSTR "SFMT-44497:330-5-3-9-3:effffffb-dfbebfff-bfbf7bef-9ffd7bff" + +#endif /* SFMT_PARAMS44497_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params607.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params607.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS607_H +#define SFMT_PARAMS607_H + +#define POS1 2 +#define SL1 15 +#define SL2 3 +#define SR1 13 +#define SR2 3 +#define MSK1 0xfdff37ffU +#define MSK2 0xef7f3f7dU +#define MSK3 0xff777b7dU +#define MSK4 0x7ff7fb2fU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0x5986f054U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2) + #define ALTI_SR2_PERM \ + (vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10} + #define ALTI_SL2_PERM64 {3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2} + #define ALTI_SR2_PERM {5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12} + #define ALTI_SR2_PERM64 {13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12} +#endif /* For OSX */ +#define IDSTR "SFMT-607:2-15-3-13-3:fdff37ff-ef7f3f7d-ff777b7d-7ff7fb2f" + +#endif /* SFMT_PARAMS607_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-params86243.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-params86243.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef SFMT_PARAMS86243_H +#define SFMT_PARAMS86243_H + +#define POS1 366 +#define SL1 6 +#define SL2 7 +#define SR1 19 +#define SR2 1 +#define MSK1 0xfdbffbffU +#define MSK2 0xbff7ff3fU +#define MSK3 0xfd77efffU +#define MSK4 0xbf9ff3ffU +#define PARITY1 0x00000001U +#define PARITY2 0x00000000U +#define PARITY3 0x00000000U +#define PARITY4 0xe9528d85U + + +/* PARAMETERS FOR ALTIVEC */ +#if defined(__APPLE__) /* For OSX */ + #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1) + #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1) + #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4) + #define ALTI_MSK64 \ + (vector unsigned int)(MSK2, MSK1, MSK4, MSK3) + #define ALTI_SL2_PERM \ + (vector unsigned char)(25,25,25,25,3,25,25,25,7,0,1,2,11,4,5,6) + #define ALTI_SL2_PERM64 \ + (vector unsigned char)(7,25,25,25,25,25,25,25,15,0,1,2,3,4,5,6) + #define ALTI_SR2_PERM \ + (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14) + #define ALTI_SR2_PERM64 \ + (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14) +#else /* For OTHER OSs(Linux?) */ + #define ALTI_SL1 {SL1, SL1, SL1, SL1} + #define ALTI_SR1 {SR1, SR1, SR1, SR1} + #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4} + #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3} + #define ALTI_SL2_PERM {25,25,25,25,3,25,25,25,7,0,1,2,11,4,5,6} + #define ALTI_SL2_PERM64 {7,25,25,25,25,25,25,25,15,0,1,2,3,4,5,6} + #define ALTI_SR2_PERM {7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14} + #define ALTI_SR2_PERM64 {15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14} +#endif /* For OSX */ +#define IDSTR "SFMT-86243:366-6-7-19-1:fdbffbff-bff7ff3f-fd77efff-bf9ff3ff" + +#endif /* SFMT_PARAMS86243_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT-sse2.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT-sse2.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,121 @@ +/** + * @file SFMT-sse2.h + * @brief SIMD oriented Fast Mersenne Twister(SFMT) for Intel SSE2 + * + * @author Mutsuo Saito (Hiroshima University) + * @author Makoto Matsumoto (Hiroshima University) + * + * @note We assume LITTLE ENDIAN in this file + * + * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima + * University. All rights reserved. + * + * The new BSD License is applied to this software, see LICENSE.txt + */ + +#ifndef SFMT_SSE2_H +#define SFMT_SSE2_H + +PRE_ALWAYS static __m128i mm_recursion(__m128i *a, __m128i *b, __m128i c, + __m128i d, __m128i mask) ALWAYSINLINE; + +/** + * This function represents the recursion formula. + * @param a a 128-bit part of the interal state array + * @param b a 128-bit part of the interal state array + * @param c a 128-bit part of the interal state array + * @param d a 128-bit part of the interal state array + * @param mask 128-bit mask + * @return output + */ +PRE_ALWAYS static __m128i mm_recursion(__m128i *a, __m128i *b, + __m128i c, __m128i d, __m128i mask) { + __m128i v, x, y, z; + + x = _mm_load_si128(a); + y = _mm_srli_epi32(*b, SR1); + z = _mm_srli_si128(c, SR2); + v = _mm_slli_epi32(d, SL1); + z = _mm_xor_si128(z, x); + z = _mm_xor_si128(z, v); + x = _mm_slli_si128(x, SL2); + y = _mm_and_si128(y, mask); + z = _mm_xor_si128(z, x); + z = _mm_xor_si128(z, y); + return z; +} + +/** + * This function fills the internal state array with pseudorandom + * integers. + */ +inline static void gen_rand_all(void) { + int i; + __m128i r, r1, r2, mask; + mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1); + + r1 = _mm_load_si128(&sfmt[N - 2].si); + r2 = _mm_load_si128(&sfmt[N - 1].si); + for (i = 0; i < N - POS1; i++) { + r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1].si, r1, r2, mask); + _mm_store_si128(&sfmt[i].si, r); + r1 = r2; + r2 = r; + } + for (; i < N; i++) { + r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1 - N].si, r1, r2, mask); + _mm_store_si128(&sfmt[i].si, r); + r1 = r2; + r2 = r; + } +} + +/** + * This function fills the user-specified array with pseudorandom + * integers. + * + * @param array an 128-bit array to be filled by pseudorandom numbers. + * @param size number of 128-bit pesudorandom numbers to be generated. + */ +inline static void gen_rand_array(w128_t *array, int size) { + int i, j; + __m128i r, r1, r2, mask; + mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1); + + r1 = _mm_load_si128(&sfmt[N - 2].si); + r2 = _mm_load_si128(&sfmt[N - 1].si); + for (i = 0; i < N - POS1; i++) { + r = mm_recursion(&sfmt[i].si, &sfmt[i + POS1].si, r1, r2, mask); + _mm_store_si128(&array[i].si, r); + r1 = r2; + r2 = r; + } + for (; i < N; i++) { + r = mm_recursion(&sfmt[i].si, &array[i + POS1 - N].si, r1, r2, mask); + _mm_store_si128(&array[i].si, r); + r1 = r2; + r2 = r; + } + /* main loop */ + for (; i < size - N; i++) { + r = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2, + mask); + _mm_store_si128(&array[i].si, r); + r1 = r2; + r2 = r; + } + for (j = 0; j < 2 * N - size; j++) { + r = _mm_load_si128(&array[j + size - N].si); + _mm_store_si128(&sfmt[j].si, r); + } + for (; i < size; i++) { + r = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2, + mask); + _mm_store_si128(&array[i].si, r); + _mm_store_si128(&sfmt[j++].si, r); + r1 = r2; + r2 = r; + } +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,620 @@ +/** + * @file SFMT.c + * @brief SIMD oriented Fast Mersenne Twister(SFMT) + * + * @author Mutsuo Saito (Hiroshima University) + * @author Makoto Matsumoto (Hiroshima University) + * + * Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima + * University. All rights reserved. + * + * The new BSD License is applied to this software, see LICENSE.txt + */ +#include +#include +#include "SFMT.h" +#include "SFMT-params.h" + +#if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64) +#define BIG_ENDIAN64 1 +#endif +#if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64) +#define BIG_ENDIAN64 1 +#endif +#if defined(ONLY64) && !defined(BIG_ENDIAN64) + #if defined(__GNUC__) + #error "-DONLY64 must be specified with -DBIG_ENDIAN64" + #endif +#undef ONLY64 +#endif +/*------------------------------------------------------ + 128-bit SIMD data type for Altivec, SSE2 or standard C + ------------------------------------------------------*/ +#if defined(HAVE_ALTIVEC) + #if !defined(__APPLE__) + #include + #endif +/** 128-bit data structure */ +union W128_T { + vector unsigned int s; + uint32_t u[4]; +}; +/** 128-bit data type */ +typedef union W128_T w128_t; + +#elif defined(HAVE_SSE2) + #include + +/** 128-bit data structure */ +union W128_T { + __m128i si; + uint32_t u[4]; +}; +/** 128-bit data type */ +typedef union W128_T w128_t; + +#else + +/** 128-bit data structure */ +struct W128_T { + uint32_t u[4]; +}; +/** 128-bit data type */ +typedef struct W128_T w128_t; + +#endif + +/*-------------------------------------- + FILE GLOBAL VARIABLES + internal state, index counter and flag + --------------------------------------*/ +/** the 128-bit internal state array */ +static w128_t sfmt[N]; +/** the 32bit integer pointer to the 128-bit internal state array */ +static uint32_t *psfmt32 = &sfmt[0].u[0]; +#if !defined(BIG_ENDIAN64) || defined(ONLY64) +/** the 64bit integer pointer to the 128-bit internal state array */ +static uint64_t *psfmt64 = (uint64_t *)&sfmt[0].u[0]; +#endif +/** index counter to the 32-bit internal state array */ +static int idx; +/** a flag: it is 0 if and only if the internal state is not yet + * initialized. */ +static int initialized = 0; +/** a parity check vector which certificate the period of 2^{MEXP} */ +static uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4}; + +/*---------------- + STATIC FUNCTIONS + ----------------*/ +inline static int idxof(int i); +inline static void rshift128(w128_t *out, w128_t const *in, int shift); +inline static void lshift128(w128_t *out, w128_t const *in, int shift); +inline static void gen_rand_all(void); +inline static void gen_rand_array(w128_t *array, int size); +inline static uint32_t func1(uint32_t x); +inline static uint32_t func2(uint32_t x); +static void period_certification(void); +#if defined(BIG_ENDIAN64) && !defined(ONLY64) +inline static void swap(w128_t *array, int size); +#endif + +#if defined(HAVE_ALTIVEC) + #include "SFMT-alti.h" +#elif defined(HAVE_SSE2) + #include "SFMT-sse2.h" +#endif + +/** + * This function simulate a 64-bit index of LITTLE ENDIAN + * in BIG ENDIAN machine. + */ +#ifdef ONLY64 +inline static int idxof(int i) { + return i ^ 1; +} +#else +inline static int idxof(int i) { + return i; +} +#endif +/** + * This function simulates SIMD 128-bit right shift by the standard C. + * The 128-bit integer given in in is shifted by (shift * 8) bits. + * This function simulates the LITTLE ENDIAN SIMD. + * @param out the output of this function + * @param in the 128-bit data to be shifted + * @param shift the shift value + */ +#ifdef ONLY64 +inline static void rshift128(w128_t *out, w128_t const *in, int shift) { + uint64_t th, tl, oh, ol; + + th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]); + tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]); + + oh = th >> (shift * 8); + ol = tl >> (shift * 8); + ol |= th << (64 - shift * 8); + out->u[0] = (uint32_t)(ol >> 32); + out->u[1] = (uint32_t)(ol & 0xffffffff); + out->u[2] = (uint32_t)(oh >> 32); + out->u[3] = (uint32_t)(oh & 0xffffffff); +} +#else +inline static void rshift128(w128_t *out, w128_t const *in, int shift) { + uint64_t th, tl, oh, ol; + + th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]); + tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]); + + oh = th >> (shift * 8); + ol = tl >> (shift * 8); + ol |= th << (64 - shift * 8); + out->u[1] = (uint32_t)(ol >> 32); + out->u[0] = (uint32_t)(ol & 0xffffffff); + out->u[3] = (uint32_t)(oh >> 32); + out->u[2] = (uint32_t)(oh & 0xffffffff); +} +#endif +/** + * This function simulates SIMD 128-bit left shift by the standard C. + * The 128-bit integer given in in is shifted by (shift * 8) bits. + * This function simulates the LITTLE ENDIAN SIMD. + * @param out the output of this function + * @param in the 128-bit data to be shifted + * @param shift the shift value + */ +#ifdef ONLY64 +inline static void lshift128(w128_t *out, w128_t const *in, int shift) { + uint64_t th, tl, oh, ol; + + th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]); + tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]); + + oh = th << (shift * 8); + ol = tl << (shift * 8); + oh |= tl >> (64 - shift * 8); + out->u[0] = (uint32_t)(ol >> 32); + out->u[1] = (uint32_t)(ol & 0xffffffff); + out->u[2] = (uint32_t)(oh >> 32); + out->u[3] = (uint32_t)(oh & 0xffffffff); +} +#else +inline static void lshift128(w128_t *out, w128_t const *in, int shift) { + uint64_t th, tl, oh, ol; + + th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]); + tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]); + + oh = th << (shift * 8); + ol = tl << (shift * 8); + oh |= tl >> (64 - shift * 8); + out->u[1] = (uint32_t)(ol >> 32); + out->u[0] = (uint32_t)(ol & 0xffffffff); + out->u[3] = (uint32_t)(oh >> 32); + out->u[2] = (uint32_t)(oh & 0xffffffff); +} +#endif + +/** + * This function represents the recursion formula. + * @param r output + * @param a a 128-bit part of the internal state array + * @param b a 128-bit part of the internal state array + * @param c a 128-bit part of the internal state array + * @param d a 128-bit part of the internal state array + */ +#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2)) +#ifdef ONLY64 +inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c, + w128_t *d) { + w128_t x; + w128_t y; + + lshift128(&x, a, SL2); + rshift128(&y, c, SR2); + r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0] + ^ (d->u[0] << SL1); + r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1] + ^ (d->u[1] << SL1); + r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2] + ^ (d->u[2] << SL1); + r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3] + ^ (d->u[3] << SL1); +} +#else +inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c, + w128_t *d) { + w128_t x; + w128_t y; + + lshift128(&x, a, SL2); + rshift128(&y, c, SR2); + r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0] + ^ (d->u[0] << SL1); + r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1] + ^ (d->u[1] << SL1); + r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2] + ^ (d->u[2] << SL1); + r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3] + ^ (d->u[3] << SL1); +} +#endif +#endif + +#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2)) +/** + * This function fills the internal state array with pseudorandom + * integers. + */ +inline static void gen_rand_all(void) { + int i; + w128_t *r1, *r2; + + r1 = &sfmt[N - 2]; + r2 = &sfmt[N - 1]; + for (i = 0; i < N - POS1; i++) { + do_recursion(&sfmt[i], &sfmt[i], &sfmt[i + POS1], r1, r2); + r1 = r2; + r2 = &sfmt[i]; + } + for (; i < N; i++) { + do_recursion(&sfmt[i], &sfmt[i], &sfmt[i + POS1 - N], r1, r2); + r1 = r2; + r2 = &sfmt[i]; + } +} + +/** + * This function fills the user-specified array with pseudorandom + * integers. + * + * @param array an 128-bit array to be filled by pseudorandom numbers. + * @param size number of 128-bit pseudorandom numbers to be generated. + */ +inline static void gen_rand_array(w128_t *array, int size) { + int i, j; + w128_t *r1, *r2; + + r1 = &sfmt[N - 2]; + r2 = &sfmt[N - 1]; + for (i = 0; i < N - POS1; i++) { + do_recursion(&array[i], &sfmt[i], &sfmt[i + POS1], r1, r2); + r1 = r2; + r2 = &array[i]; + } + for (; i < N; i++) { + do_recursion(&array[i], &sfmt[i], &array[i + POS1 - N], r1, r2); + r1 = r2; + r2 = &array[i]; + } + for (; i < size - N; i++) { + do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2); + r1 = r2; + r2 = &array[i]; + } + for (j = 0; j < 2 * N - size; j++) { + sfmt[j] = array[j + size - N]; + } + for (; i < size; i++, j++) { + do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2); + r1 = r2; + r2 = &array[i]; + sfmt[j] = array[i]; + } +} +#endif + +#if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC) +inline static void swap(w128_t *array, int size) { + int i; + uint32_t x, y; + + for (i = 0; i < size; i++) { + x = array[i].u[0]; + y = array[i].u[2]; + array[i].u[0] = array[i].u[1]; + array[i].u[2] = array[i].u[3]; + array[i].u[1] = x; + array[i].u[3] = y; + } +} +#endif +/** + * This function represents a function used in the initialization + * by init_by_array + * @param x 32-bit integer + * @return 32-bit integer + */ +static uint32_t func1(uint32_t x) { + return (x ^ (x >> 27)) * (uint32_t)1664525UL; +} + +/** + * This function represents a function used in the initialization + * by init_by_array + * @param x 32-bit integer + * @return 32-bit integer + */ +static uint32_t func2(uint32_t x) { + return (x ^ (x >> 27)) * (uint32_t)1566083941UL; +} + +/** + * This function certificate the period of 2^{MEXP} + */ +static void period_certification(void) { + int inner = 0; + int i, j; + uint32_t work; + + for (i = 0; i < 4; i++) + inner ^= psfmt32[idxof(i)] & parity[i]; + for (i = 16; i > 0; i >>= 1) + inner ^= inner >> i; + inner &= 1; + /* check OK */ + if (inner == 1) { + return; + } + /* check NG, and modification */ + for (i = 0; i < 4; i++) { + work = 1; + for (j = 0; j < 32; j++) { + if ((work & parity[i]) != 0) { + psfmt32[idxof(i)] ^= work; + return; + } + work = work << 1; + } + } +} + +/*---------------- + PUBLIC FUNCTIONS + ----------------*/ +/** + * This function returns the identification string. + * The string shows the word size, the Mersenne exponent, + * and all parameters of this generator. + */ +const char *get_idstring(void) { + return IDSTR; +} + +/** + * This function returns the minimum size of array used for \b + * fill_array32() function. + * @return minimum size of array used for fill_array32() function. + */ +int get_min_array_size32(void) { + return N32; +} + +/** + * This function returns the minimum size of array used for \b + * fill_array64() function. + * @return minimum size of array used for fill_array64() function. + */ +int get_min_array_size64(void) { + return N64; +} + +#ifndef ONLY64 +/** + * This function generates and returns 32-bit pseudorandom number. + * init_gen_rand or init_by_array must be called before this function. + * @return 32-bit pseudorandom number + */ +uint32_t gen_rand32(void) { + uint32_t r; + + assert(initialized); + if (idx >= N32) { + gen_rand_all(); + idx = 0; + } + r = psfmt32[idx++]; + return r; +} +#endif +/** + * This function generates and returns 64-bit pseudorandom number. + * init_gen_rand or init_by_array must be called before this function. + * The function gen_rand64 should not be called after gen_rand32, + * unless an initialization is again executed. + * @return 64-bit pseudorandom number + */ +uint64_t gen_rand64(void) { +#if defined(BIG_ENDIAN64) && !defined(ONLY64) + uint32_t r1, r2; +#else + uint64_t r; +#endif + + assert(initialized); + assert(idx % 2 == 0); + + if (idx >= N32) { + gen_rand_all(); + idx = 0; + } +#if defined(BIG_ENDIAN64) && !defined(ONLY64) + r1 = psfmt32[idx]; + r2 = psfmt32[idx + 1]; + idx += 2; + return ((uint64_t)r2 << 32) | r1; +#else + r = psfmt64[idx / 2]; + idx += 2; + return r; +#endif +} + +#ifndef ONLY64 +/** + * This function generates pseudorandom 32-bit integers in the + * specified array[] by one call. The number of pseudorandom integers + * is specified by the argument size, which must be at least 624 and a + * multiple of four. The generation by this function is much faster + * than the following gen_rand function. + * + * For initialization, init_gen_rand or init_by_array must be called + * before the first call of this function. This function can not be + * used after calling gen_rand function, without initialization. + * + * @param array an array where pseudorandom 32-bit integers are filled + * by this function. The pointer to the array must be \b "aligned" + * (namely, must be a multiple of 16) in the SIMD version, since it + * refers to the address of a 128-bit integer. In the standard C + * version, the pointer is arbitrary. + * + * @param size the number of 32-bit pseudorandom integers to be + * generated. size must be a multiple of 4, and greater than or equal + * to (MEXP / 128 + 1) * 4. + * + * @note \b memalign or \b posix_memalign is available to get aligned + * memory. Mac OSX doesn't have these functions, but \b malloc of OSX + * returns the pointer to the aligned memory block. + */ +void fill_array32(uint32_t *array, int size) { + assert(initialized); + assert(idx == N32); + assert(size % 4 == 0); + assert(size >= N32); + + gen_rand_array((w128_t *)array, size / 4); + idx = N32; +} +#endif + +/** + * This function generates pseudorandom 64-bit integers in the + * specified array[] by one call. The number of pseudorandom integers + * is specified by the argument size, which must be at least 312 and a + * multiple of two. The generation by this function is much faster + * than the following gen_rand function. + * + * For initialization, init_gen_rand or init_by_array must be called + * before the first call of this function. This function can not be + * used after calling gen_rand function, without initialization. + * + * @param array an array where pseudorandom 64-bit integers are filled + * by this function. The pointer to the array must be "aligned" + * (namely, must be a multiple of 16) in the SIMD version, since it + * refers to the address of a 128-bit integer. In the standard C + * version, the pointer is arbitrary. + * + * @param size the number of 64-bit pseudorandom integers to be + * generated. size must be a multiple of 2, and greater than or equal + * to (MEXP / 128 + 1) * 2 + * + * @note \b memalign or \b posix_memalign is available to get aligned + * memory. Mac OSX doesn't have these functions, but \b malloc of OSX + * returns the pointer to the aligned memory block. + */ +void fill_array64(uint64_t *array, int size) { + assert(initialized); + assert(idx == N32); + assert(size % 2 == 0); + assert(size >= N64); + + gen_rand_array((w128_t *)array, size / 2); + idx = N32; + +#if defined(BIG_ENDIAN64) && !defined(ONLY64) + swap((w128_t *)array, size /2); +#endif +} + +/** + * This function initializes the internal state array with a 32-bit + * integer seed. + * + * @param seed a 32-bit integer used as the seed. + */ +void init_gen_rand(uint32_t seed) { + int i; + + psfmt32[idxof(0)] = seed; + for (i = 1; i < N32; i++) { + psfmt32[idxof(i)] = 1812433253UL * (psfmt32[idxof(i - 1)] + ^ (psfmt32[idxof(i - 1)] >> 30)) + + i; + } + idx = N32; + period_certification(); + initialized = 1; +} + +/** + * This function initializes the internal state array, + * with an array of 32-bit integers used as the seeds + * @param init_key the array of 32-bit integers, used as a seed. + * @param key_length the length of init_key. + */ +void init_by_array(uint32_t *init_key, int key_length) { + int i, j, count; + uint32_t r; + int lag; + int mid; + int size = N * 4; + + if (size >= 623) { + lag = 11; + } else if (size >= 68) { + lag = 7; + } else if (size >= 39) { + lag = 5; + } else { + lag = 3; + } + mid = (size - lag) / 2; + + memset(sfmt, 0x8b, sizeof(sfmt)); + if (key_length + 1 > N32) { + count = key_length + 1; + } else { + count = N32; + } + r = func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid)] + ^ psfmt32[idxof(N32 - 1)]); + psfmt32[idxof(mid)] += r; + r += key_length; + psfmt32[idxof(mid + lag)] += r; + psfmt32[idxof(0)] = r; + + count--; + for (i = 1, j = 0; (j < count) && (j < key_length); j++) { + r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)] + ^ psfmt32[idxof((i + N32 - 1) % N32)]); + psfmt32[idxof((i + mid) % N32)] += r; + r += init_key[j] + i; + psfmt32[idxof((i + mid + lag) % N32)] += r; + psfmt32[idxof(i)] = r; + i = (i + 1) % N32; + } + for (; j < count; j++) { + r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)] + ^ psfmt32[idxof((i + N32 - 1) % N32)]); + psfmt32[idxof((i + mid) % N32)] += r; + r += i; + psfmt32[idxof((i + mid + lag) % N32)] += r; + psfmt32[idxof(i)] = r; + i = (i + 1) % N32; + } + for (j = 0; j < N32; j++) { + r = func2(psfmt32[idxof(i)] + psfmt32[idxof((i + mid) % N32)] + + psfmt32[idxof((i + N32 - 1) % N32)]); + psfmt32[idxof((i + mid) % N32)] ^= r; + r -= i; + psfmt32[idxof((i + mid + lag) % N32)] ^= r; + psfmt32[idxof(i)] = r; + i = (i + 1) % N32; + } + + idx = N32; + period_certification(); + initialized = 1; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/SFMT/SFMT.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SFMT/SFMT.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,165 @@ +/** + * @file SFMT.h + * + * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom + * number generator + * + * @author Mutsuo Saito (Hiroshima University) + * @author Makoto Matsumoto (Hiroshima University) + * + * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima + * University. All rights reserved. + * + * The new BSD License is applied to this software. + * see LICENSE.txt + * + * @note We assume that your system has inttypes.h. If your system + * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t, + * and you have to define PRIu64 and PRIx64 in this file as follows: + * @verbatim + typedef unsigned int uint32_t + typedef unsigned long long uint64_t + #define PRIu64 "llu" + #define PRIx64 "llx" +@endverbatim + * uint32_t must be exactly 32-bit unsigned integer type (no more, no + * less), and uint64_t must be exactly 64-bit unsigned integer type. + * PRIu64 and PRIx64 are used for printf function to print 64-bit + * unsigned int and 64-bit unsigned int in hexadecimal format. + */ + +#ifndef SFMT_H +#define SFMT_H + +#include + +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) + #include +#elif defined(_MSC_VER) || defined(__BORLANDC__) + typedef unsigned int uint32_t; + typedef unsigned __int64 uint64_t; + #define inline __inline +#else + #include + #if defined(__GNUC__) + #define inline __inline__ + #endif +#endif + +#ifndef PRIu64 + #if defined(_MSC_VER) || defined(__BORLANDC__) + #define PRIu64 "I64u" + #define PRIx64 "I64x" + #else + #define PRIu64 "llu" + #define PRIx64 "llx" + #endif +#endif + +#if defined(__GNUC__) +#define ALWAYSINLINE __attribute__((always_inline)) +#else +#define ALWAYSINLINE +#endif + +#if defined(_MSC_VER) + #if _MSC_VER >= 1200 + #define PRE_ALWAYS __forceinline + #else + #define PRE_ALWAYS inline + #endif +#else + #define PRE_ALWAYS inline +#endif + +#if defined(__cplusplus) || defined(c_plusplus) +//extern "C" { +#endif + +uint32_t gen_rand32(void); +uint64_t gen_rand64(void); +void fill_array32(uint32_t *array, int size); +void fill_array64(uint64_t *array, int size); +void init_gen_rand(uint32_t seed); +void init_by_array(uint32_t *init_key, int key_length); +const char *get_idstring(void); +int get_min_array_size32(void); +int get_min_array_size64(void); + +#if defined(__cplusplus) || defined(c_plusplus) +//} // end extern +#endif + +/* These real versions are due to Isaku Wada */ +/** generates a random number on [0,1]-real-interval */ +inline static double to_real1(uint32_t v) +{ + return v * (1.0/4294967295.0); + /* divided by 2^32-1 */ +} + +/** generates a random number on [0,1]-real-interval */ +inline static double genrand_real1(void) +{ + return to_real1(gen_rand32()); +} + +/** generates a random number on [0,1)-real-interval */ +inline static double to_real2(uint32_t v) +{ + return v * (1.0/4294967296.0); + /* divided by 2^32 */ +} + +/** generates a random number on [0,1)-real-interval */ +inline static double genrand_real2(void) +{ + return to_real2(gen_rand32()); +} + +/** generates a random number on (0,1)-real-interval */ +inline static double to_real3(uint32_t v) +{ + return (((double)v) + 0.5)*(1.0/4294967296.0); + /* divided by 2^32 */ +} + +/** generates a random number on (0,1)-real-interval */ +inline static double genrand_real3(void) +{ + return to_real3(gen_rand32()); +} +/** These real versions are due to Isaku Wada */ + +/** generates a random number on [0,1) with 53-bit resolution*/ +inline static double to_res53(uint64_t v) +{ + return v * (1.0/18446744073709551616.0L); +} + +/** generates a random number on [0,1) with 53-bit resolution from two + * 32 bit integers */ +inline static double to_res53_mix(uint32_t x, uint32_t y) +{ + return to_res53(x | ((uint64_t)y << 32)); +} + +/** generates a random number on [0,1) with 53-bit resolution + */ +inline static double genrand_res53(void) +{ + return to_res53(gen_rand64()); +} + +/** generates a random number on [0,1) with 53-bit resolution + using 32bit integer. + */ +inline static double genrand_res53_mix(void) +{ + uint32_t x, y; + + x = gen_rand32(); + y = gen_rand32(); + return to_res53_mix(x, y); +} +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/VisualBoyAdvance.cfg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/VisualBoyAdvance.cfg Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,213 @@ +# All numeric values are in hexadecimal +# Use TAB or EQUAL sign to separate name from value + +# +# Key configuration (all numbers are in hexadecimal!) +# +# Keys values are in the format YXXX where Y is the device number. 0 means +# keyborad and XXX is the SDL define for the desired key (read SDL_keysym.h). +# +# If Y is greater than 0, it means joystick number Y-1 and it uses the +# following format for XXX: +# +# - if XXX < 20, XXX is the axis number multiplied by 2. An even number means +# movement to the negative side (on the X axis, it means left). An odd +# number means movement to the positive side (on the X axis, it mean +# right). For the Y axis, negative means up and positive means down. +# X axis is usally axis number 0 and Y is axis number 1. +# - if 20 >= XXX > 30, then XXX is the HAT number multiplied by 4 plus the +# direction: 0 for up, 1 for down, 2 for right and 3 for left. Example: +# 0021 is HAT 0 down, 0026 is HAT 1 right. +# - if 80 >= XXX > 100, XXX is the joystick button number (XXX-080). +# +# Default key configuration is (value in parenthesis): +# +# Left Left Arrow (0114) +# Right Right Arrow (0113) +# Up Up Arrow (0111) +# Down Down Arrow (0112) +# A Z (007a) +# B X (0078) +# L A (0061) +# R S (0073) +# Start ENTER (000d) +# Select BACKSPACE (0008) +# Speed up SPACE (0020) +# Capture F12 (0125) +# +Joy0_Left=0114 +Joy0_Right=0113 +Joy0_Up=0111 +Joy0_Down=0112 +Joy0_A=007a +Joy0_B=0078 +Joy0_L=0061 +Joy0_R=0073 +Joy0_Start=000d +Joy0_Select=0008 +Joy0_Speed=0020 +Joy0_Capture=0125 + +# Motion support keys. Same format as above +# +# Default keys are (value in parenthesis): +# +# Left Numeric Pad 4 (0104) +# Right Numeric Pad 6 (0106) +# Up Numeric Pad 8 (0108) +# Down Numeric Pad 2 (0102) +# +Motion_Left=0104 +Motion_Right=0106 +Motion_Up=0108 +Motion_Down=0102 + +# Frame skip setting. Allowed values are from 0 to 5 only. +frameSkip=2 + +# Gameboy Frame skip setting. Allowed values are from 0 to 5 only. +gbFrameSkip=0 + +# Video setting. 0=1x, 1=2x, 2=3x, 3=4x. +video=1 + +# Use fullscreen mode. 0=false, any other value means true +fullScreen=0 + +# Disables MMX support +disableMMX=1 + +# Use bios file. 0=false, any other value means true +useBios=0 + +# Bios file full path and name (ZIP not supported) +biosFile=none + +# Filter to use. 0=no filter, 1=TV Mode, 2=2xSaI, 3=Super 2xSaI, 4=Super Eagle +# 5=Pixelate, 6=Motion Blur, 7=AdvanceMAME Scale2x, 8=Simple 2x, +# 9=Bilinear, A=Bilinear Plus, B=hq2x, C=lq2x +filter=0 + +# Disable status messages. 0=false, any other value means true +disableStatus=0 + +# Enable Gameboy border. 0=false, any other value means true +borderOn=0 + +# Gameboy emulator type. 0=automatic, 1=CGB/GBC, 2=SGB, 3=GB, 4=GBA, 5=SGB2 +emulatorType=1 + +# Enable washed colors. 0=false, any other value means true +colorOption=1 + +# Directories. Not setting one them makes the file go the rom directory. + +# Save state directory +#saveDir= + +# Screen shot Capture directory +#captureDir= + +# Battery directory +#batteryDir= + +# Screen capture format +# 0=PNG, anything else for BMP +captureFormat=0 + +# Sound quality +# 1=44 Khz, 2=22Khz, 4=11Khz +soundQuality=1 + +# Sound Echo +# 0=false, anything else for true +soundEcho=0 + +# Sound Low pass filter +# 0=false, anything else for true +soundLowPass=0 + +# Sound reverse stereo +# 0=false, anything else for true +soundReverse=0 + +# Remove GBA intros (not supported anymore) +# 0=false, anything else for true +# removeIntros=0 + +# Save Type +# 0=automatic, 1=EEPROM, 2=SRAM, 3=Flash, 4=EEPROM+Sensor, 5=NONE +saveType=0 + +# Flash size +# 0=64K Flash, 1=128K Flash + +flashSize=0 + +# Sound volume +# 0=1x, 1=2x, 2=3x, 3=4x +soundVolume=0 + +# Interframe blending +# 0=none, 1=motion blur, 2=smart +ifbType=0 + +# Show emulation speed +# 0=none, 1=percentage, 2=detailed +showSpeed=1 + +# Show speed in transparent mode +# 0=normal, anything else for transparent +showSpeedTransparent=1 + +# Enable/Disable auto frameskip +# 0=disable, anything else to enable +autoFrameSkip=0 + +# Sets the desired throttle +# 0=disable, 5...1000 valid throttle speeds +throttle=0 + +# Pauses the emulator when the window is inactive +# 0=disable, anything else to enable +pauseWhenInactive=0 + +# Enables AGBPrint support +# 0=disable, anything else to enable +agbPrint=0 + +# Enables GBA RTC support +# 0=disable, anything else to enable +rtcEnabled=0 + +# Sound OFF flag +# 0=sound on, anything else turns off sound +soundOff=0 + +# Sound Enable +# Controls which channels are enabled: (add values) +# 1 - Channel 1 +# 2 - Channel 2 +# 4 - Channel 3 +# 8 - Channel 4 +# 100 - DirectSound A +# 200 - DirectSound B +# 30f=all enabled, 0=mute all +soundEnable=30f + +# Controls automatic SGB border +# 0=disable, anything else enables automatic SGB border display +borderAutomatic=0 + +# Skip bios code +# 0=disable, anything else skips BIOS code +skipBios=0 + +# The interval between the rewind saves +# Minimum of 0 seconds to disable rewind support, +# Maximum of 60 minutes. Value in seconds (hexadecimal numbers) +rewindTimer=0 + +# Enable enhanced save type detection +# 0=disable, anything else to enable (no longer used) +#enhancedDetection=1 diff -r 8ced16adf2e1 -r f9f4f1b99eed src/apu/Blip_Buffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/apu/Blip_Buffer.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,465 @@ +// Blip_Buffer 0.4.1. http://www.slack.net/~ant/ + +#include "Blip_Buffer.h" + +#include +#include +#include +#include +#include + +/* Copyright (C) 2003-2007 Shay Green. This module is free software; you +can redistribute it and/or modify it under the terms of the GNU Lesser +General Public License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. This +module is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +details. You should have received a copy of the GNU Lesser General Public +License along with this module; if not, write to the Free Software Foundation, +Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +// TODO: use scoped for variables in treble_eq() + +#ifdef BLARGG_ENABLE_OPTIMIZER + #include BLARGG_ENABLE_OPTIMIZER +#endif + +int const silent_buf_size = 1; // size used for Silent_Blip_Buffer + +Blip_Buffer::Blip_Buffer() +{ + factor_ = LONG_MAX; + buffer_ = 0; + buffer_size_ = 0; + sample_rate_ = 0; + bass_shift_ = 0; + clock_rate_ = 0; + bass_freq_ = 16; + length_ = 0; + + // assumptions code makes about implementation-defined features + #ifndef NDEBUG + // right shift of negative value preserves sign + buf_t_ i = -0x7FFFFFFE; + assert( (i >> 1) == -0x3FFFFFFF ); + + // casting to short truncates to 16 bits and sign-extends + i = 0x18000; + assert( (short) i == -0x8000 ); + #endif + + clear(); +} + +Blip_Buffer::~Blip_Buffer() +{ + if ( buffer_size_ != silent_buf_size ) + free( buffer_ ); +} + +Silent_Blip_Buffer::Silent_Blip_Buffer() +{ + factor_ = 0; + buffer_ = buf; + buffer_size_ = silent_buf_size; + clear(); +} + +void Blip_Buffer::clear( int entire_buffer ) +{ + offset_ = 0; + reader_accum_ = 0; + modified_ = 0; + if ( buffer_ ) + { + long count = (entire_buffer ? buffer_size_ : samples_avail()); + memset( buffer_, 0, (count + blip_buffer_extra_) * sizeof (buf_t_) ); + } +} + +Blip_Buffer::blargg_err_t Blip_Buffer::set_sample_rate( long new_rate, int msec ) +{ + if ( buffer_size_ == silent_buf_size ) + { + assert( 0 ); + return "Internal (tried to resize Silent_Blip_Buffer)"; + } + + // start with maximum length that resampled time can represent + long new_size = (ULONG_MAX >> BLIP_BUFFER_ACCURACY) - blip_buffer_extra_ - 64; + if ( msec != blip_max_length ) + { + long s = (new_rate * (msec + 1) + 999) / 1000; + if ( s < new_size ) + new_size = s; + else + assert( 0 ); // fails if requested buffer length exceeds limit + } + + if ( buffer_size_ != new_size ) + { + void* p = realloc( buffer_, (new_size + blip_buffer_extra_) * sizeof *buffer_ ); + if ( !p ) + return "Out of memory"; + buffer_ = (buf_t_*) p; + } + + buffer_size_ = new_size; + assert( buffer_size_ != silent_buf_size ); // size should never happen to match this + + // update things based on the sample rate + sample_rate_ = new_rate; + length_ = new_size * 1000 / new_rate - 1; + if ( msec ) + assert( length_ == msec ); // ensure length is same as that passed in + + // update these since they depend on sample rate + if ( clock_rate_ ) + clock_rate( clock_rate_ ); + bass_freq( bass_freq_ ); + + clear(); + + return 0; // success +} + +blip_resampled_time_t Blip_Buffer::clock_rate_factor( long rate ) const +{ + double ratio = (double) sample_rate_ / rate; + blip_long factor = (blip_long) floor( ratio * (1L << BLIP_BUFFER_ACCURACY) + 0.5 ); + assert( factor > 0 || !sample_rate_ ); // fails if clock/output ratio is too large + return (blip_resampled_time_t) factor; +} + +void Blip_Buffer::bass_freq( int freq ) +{ + bass_freq_ = freq; + int shift = 31; + if ( freq > 0 ) + { + shift = 13; + long f = (freq << 16) / sample_rate_; + while ( (f >>= 1) && --shift ) { } + } + bass_shift_ = shift; +} + +void Blip_Buffer::end_frame( blip_time_t t ) +{ + offset_ += t * factor_; + assert( samples_avail() <= (long) buffer_size_ ); // fails if time is past end of buffer +} + +long Blip_Buffer::count_samples( blip_time_t t ) const +{ + blip_resampled_time_t last_sample = resampled_time( t ) >> BLIP_BUFFER_ACCURACY; + blip_resampled_time_t first_sample = offset_ >> BLIP_BUFFER_ACCURACY; + return long (last_sample - first_sample); +} + +blip_time_t Blip_Buffer::count_clocks( long count ) const +{ + if ( !factor_ ) + { + assert( 0 ); // sample rate and clock rates must be set first + return 0; + } + + if ( count > buffer_size_ ) + count = buffer_size_; + blip_resampled_time_t time = (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; + return (blip_time_t) ((time - offset_ + factor_ - 1) / factor_); +} + +void Blip_Buffer::remove_samples( long count ) +{ + if ( count ) + { + remove_silence( count ); + + // copy remaining samples to beginning and clear old samples + long remain = samples_avail() + blip_buffer_extra_; + memmove( buffer_, buffer_ + count, remain * sizeof *buffer_ ); + memset( buffer_ + remain, 0, count * sizeof *buffer_ ); + } +} + +// Blip_Synth_ + +Blip_Synth_Fast_::Blip_Synth_Fast_() +{ + buf = 0; + last_amp = 0; + delta_factor = 0; +} + +void Blip_Synth_Fast_::volume_unit( double new_unit ) +{ + delta_factor = int (new_unit * (1L << blip_sample_bits) + 0.5); +} + +#if !BLIP_BUFFER_FAST + +Blip_Synth_::Blip_Synth_( short* p, int w ) : + impulses( p ), + width( w ) +{ + volume_unit_ = 0.0; + kernel_unit = 0; + buf = 0; + last_amp = 0; + delta_factor = 0; +} + +#undef PI +#define PI 3.1415926535897932384626433832795029 + +static void gen_sinc( float* out, int count, double oversample, double treble, double cutoff ) +{ + if ( cutoff >= 0.999 ) + cutoff = 0.999; + + if ( treble < -300.0 ) + treble = -300.0; + if ( treble > 5.0 ) + treble = 5.0; + + double const maxh = 4096.0; + double const rolloff = pow( 10.0, 1.0 / (maxh * 20.0) * treble / (1.0 - cutoff) ); + double const pow_a_n = pow( rolloff, maxh - maxh * cutoff ); + double const to_angle = PI / 2 / maxh / oversample; + for ( int i = 0; i < count; i++ ) + { + double angle = ((i - count) * 2 + 1) * to_angle; + double c = rolloff * cos( (maxh - 1.0) * angle ) - cos( maxh * angle ); + double cos_nc_angle = cos( maxh * cutoff * angle ); + double cos_nc1_angle = cos( (maxh * cutoff - 1.0) * angle ); + double cos_angle = cos( angle ); + + c = c * pow_a_n - rolloff * cos_nc1_angle + cos_nc_angle; + double d = 1.0 + rolloff * (rolloff - cos_angle - cos_angle); + double b = 2.0 - cos_angle - cos_angle; + double a = 1.0 - cos_angle - cos_nc_angle + cos_nc1_angle; + + out [i] = (float) ((a * d + c * b) / (b * d)); // a / b + c / d + } +} + +void blip_eq_t::generate( float* out, int count ) const +{ + // lower cutoff freq for narrow kernels with their wider transition band + // (8 points->1.49, 16 points->1.15) + double oversample = blip_res * 2.25 / count + 0.85; + double half_rate = sample_rate * 0.5; + if ( cutoff_freq ) + oversample = half_rate / cutoff_freq; + double cutoff = rolloff_freq * oversample / half_rate; + + gen_sinc( out, count, blip_res * oversample, treble, cutoff ); + + // apply (half of) hamming window + double to_fraction = PI / (count - 1); + for ( int i = count; i--; ) + out [i] *= 0.54f - 0.46f * (float) cos( i * to_fraction ); +} + +void Blip_Synth_::adjust_impulse() +{ + // sum pairs for each phase and add error correction to end of first half + int const size = impulses_size(); + for ( int p = blip_res; p-- >= blip_res / 2; ) + { + int p2 = blip_res - 2 - p; + long error = kernel_unit; + for ( int i = 1; i < size; i += blip_res ) + { + error -= impulses [i + p ]; + error -= impulses [i + p2]; + } + if ( p == p2 ) + error /= 2; // phase = 0.5 impulse uses same half for both sides + impulses [size - blip_res + p] += (short) error; + //printf( "error: %ld\n", error ); + } + + //for ( int i = blip_res; i--; printf( "\n" ) ) + // for ( int j = 0; j < width / 2; j++ ) + // printf( "%5ld,", impulses [j * blip_res + i + 1] ); +} + +void Blip_Synth_::treble_eq( blip_eq_t const& eq ) +{ + float fimpulse [blip_res / 2 * (blip_widest_impulse_ - 1) + blip_res * 2]; + + int const half_size = blip_res / 2 * (width - 1); + eq.generate( &fimpulse [blip_res], half_size ); + + int i; + + // need mirror slightly past center for calculation + for ( i = blip_res; i--; ) + fimpulse [blip_res + half_size + i] = fimpulse [blip_res + half_size - 1 - i]; + + // starts at 0 + for ( i = 0; i < blip_res; i++ ) + fimpulse [i] = 0.0f; + + // find rescale factor + double total = 0.0; + for ( i = 0; i < half_size; i++ ) + total += fimpulse [blip_res + i]; + + //double const base_unit = 44800.0 - 128 * 18; // allows treble up to +0 dB + //double const base_unit = 37888.0; // allows treble to +5 dB + double const base_unit = 32768.0; // necessary for blip_unscaled to work + double rescale = base_unit / 2 / total; + kernel_unit = (long) base_unit; + + // integrate, first difference, rescale, convert to int + double sum = 0.0; + double next = 0.0; + int const size = this->impulses_size(); + for ( i = 0; i < size; i++ ) + { + impulses [i] = (short) (int) floor( (next - sum) * rescale + 0.5 ); + sum += fimpulse [i]; + next += fimpulse [i + blip_res]; + } + adjust_impulse(); + + // volume might require rescaling + double vol = volume_unit_; + if ( vol ) + { + volume_unit_ = 0.0; + volume_unit( vol ); + } +} + +void Blip_Synth_::volume_unit( double new_unit ) +{ + if ( new_unit != volume_unit_ ) + { + // use default eq if it hasn't been set yet + if ( !kernel_unit ) + treble_eq( -8.0 ); + + volume_unit_ = new_unit; + double factor = new_unit * (1L << blip_sample_bits) / kernel_unit; + + if ( factor > 0.0 ) + { + int shift = 0; + + // if unit is really small, might need to attenuate kernel + while ( factor < 2.0 ) + { + shift++; + factor *= 2.0; + } + + if ( shift ) + { + kernel_unit >>= shift; + assert( kernel_unit > 0 ); // fails if volume unit is too low + + // keep values positive to avoid round-towards-zero of sign-preserving + // right shift for negative values + long offset = 0x8000 + (1 << (shift - 1)); + long offset2 = 0x8000 >> shift; + for ( int i = impulses_size(); i--; ) + impulses [i] = (short) (int) (((impulses [i] + offset) >> shift) - offset2); + adjust_impulse(); + } + } + delta_factor = (int) floor( factor + 0.5 ); + //printf( "delta_factor: %d, kernel_unit: %d\n", delta_factor, kernel_unit ); + } +} +#endif + +long Blip_Buffer::read_samples( blip_sample_t* out_, long max_samples, int stereo ) +{ + long count = samples_avail(); + if ( count > max_samples ) + count = max_samples; + + if ( count ) + { + int const bass = BLIP_READER_BASS( *this ); + BLIP_READER_BEGIN( reader, *this ); + BLIP_READER_ADJ_( reader, count ); + blip_sample_t* BLIP_RESTRICT out = out_ + count; + blip_long offset = (blip_long) -count; + + if ( !stereo ) + { + do + { + blip_long s = BLIP_READER_READ( reader ); + BLIP_READER_NEXT_IDX_( reader, bass, offset ); + BLIP_CLAMP( s, s ); + out [offset] = (blip_sample_t) s; + } + while ( ++offset ); + } + else + { + do + { + blip_long s = BLIP_READER_READ( reader ); + BLIP_READER_NEXT_IDX_( reader, bass, offset ); + BLIP_CLAMP( s, s ); + out [offset * 2] = (blip_sample_t) s; + } + while ( ++offset ); + } + + BLIP_READER_END( reader, *this ); + + remove_samples( count ); + } + return count; +} + +void Blip_Buffer::mix_samples( blip_sample_t const* in, long count ) +{ + if ( buffer_size_ == silent_buf_size ) + { + assert( 0 ); + return; + } + + buf_t_* out = buffer_ + (offset_ >> BLIP_BUFFER_ACCURACY) + blip_widest_impulse_ / 2; + + int const sample_shift = blip_sample_bits - 16; + int prev = 0; + while ( count-- ) + { + blip_long s = (blip_long) *in++ << sample_shift; + *out += s - prev; + prev = s; + ++out; + } + *out -= prev; +} + +blip_ulong const subsample_mask = (1L << BLIP_BUFFER_ACCURACY) - 1; + +void Blip_Buffer::save_state( blip_buffer_state_t* out ) +{ + assert( samples_avail() == 0 ); + out->offset_ = offset_; + out->reader_accum_ = reader_accum_; + memcpy( out->buf, &buffer_ [offset_ >> BLIP_BUFFER_ACCURACY], sizeof out->buf ); +} + +void Blip_Buffer::load_state( blip_buffer_state_t const& in ) +{ + clear( false ); + + offset_ = in.offset_; + reader_accum_ = in.reader_accum_; + memcpy( buffer_, in.buf, sizeof in.buf ); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/apu/Blip_Buffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/apu/Blip_Buffer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,556 @@ +// Band-limited sound synthesis buffer + +// Blip_Buffer 0.4.1 +#ifndef BLIP_BUFFER_H +#define BLIP_BUFFER_H + + // internal + #include + #if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF + typedef long blip_long; + typedef unsigned long blip_ulong; + #else + typedef int blip_long; + typedef unsigned blip_ulong; + #endif + +// Time unit at source clock rate +typedef blip_long blip_time_t; + +// Output samples are 16-bit signed, with a range of -32768 to 32767 +typedef short blip_sample_t; +enum { blip_sample_max = 32767 }; + +struct blip_buffer_state_t; + +class Blip_Buffer { +public: + typedef const char* blargg_err_t; + + // Sets output sample rate and buffer length in milliseconds (1/1000 sec, defaults + // to 1/4 second) and clears buffer. If there isn't enough memory, leaves buffer + // untouched and returns "Out of memory", otherwise returns NULL. + blargg_err_t set_sample_rate( long samples_per_sec, int msec_length = 1000 / 4 ); + + // Sets number of source time units per second + void clock_rate( long clocks_per_sec ); + + // Ends current time frame of specified duration and makes its samples available + // (along with any still-unread samples) for reading with read_samples(). Begins + // a new time frame at the end of the current frame. + void end_frame( blip_time_t time ); + + // Reads at most 'max_samples' out of buffer into 'dest', removing them from from + // the buffer. Returns number of samples actually read and removed. If stereo is + // true, increments 'dest' one extra time after writing each sample, to allow + // easy interleving of two channels into a stereo output buffer. + long read_samples( blip_sample_t* dest, long max_samples, int stereo = 0 ); + +// Additional features + + // Removes all available samples and clear buffer to silence. If 'entire_buffer' is + // false, just clears out any samples waiting rather than the entire buffer. + void clear( int entire_buffer = 1 ); + + // Number of samples available for reading with read_samples() + long samples_avail() const; + + // Removes 'count' samples from those waiting to be read + void remove_samples( long count ); + + // Sets frequency high-pass filter frequency, where higher values reduce bass more + void bass_freq( int frequency ); + + // Current output sample rate + long sample_rate() const; + + // Length of buffer in milliseconds + int length() const; + + // Number of source time units per second + long clock_rate() const; + +// Experimental features + + // Saves state, including high-pass filter and tails of last deltas. + // All samples must have been read from buffer before calling this. + void save_state( blip_buffer_state_t* out ); + + // Loads state. State must have been saved from Blip_Buffer with same + // settings during same run of program. States can NOT be stored on disk. + // Clears buffer before loading state. + void load_state( blip_buffer_state_t const& in ); + + // Number of samples delay from synthesis to samples read out + int output_latency() const; + + // Counts number of clocks needed until 'count' samples will be available. + // If buffer can't even hold 'count' samples, returns number of clocks until + // buffer becomes full. + blip_time_t count_clocks( long count ) const; + + // Number of raw samples that can be mixed within frame of specified duration. + long count_samples( blip_time_t duration ) const; + + // Mixes in 'count' samples from 'buf_in' + void mix_samples( blip_sample_t const* buf_in, long count ); + + + // Signals that sound has been added to buffer. Could be done automatically in + // Blip_Synth, but that would affect performance more, as you can arrange that + // this is called only once per time frame rather than for every delta. + void set_modified() { modified_ = this; } + + // not documented yet + blip_ulong unsettled() const; + Blip_Buffer* clear_modified() { Blip_Buffer* b = modified_; modified_ = 0; return b; } + void remove_silence( long count ); + typedef blip_ulong blip_resampled_time_t; + blip_resampled_time_t resampled_duration( int t ) const { return t * factor_; } + blip_resampled_time_t resampled_time( blip_time_t t ) const { return t * factor_ + offset_; } + blip_resampled_time_t clock_rate_factor( long clock_rate ) const; +public: + Blip_Buffer(); + ~Blip_Buffer(); + + // Deprecated + typedef blip_resampled_time_t resampled_time_t; + blargg_err_t sample_rate( long r ) { return set_sample_rate( r ); } + blargg_err_t sample_rate( long r, int msec ) { return set_sample_rate( r, msec ); } +private: + // noncopyable + Blip_Buffer( const Blip_Buffer& ); + Blip_Buffer& operator = ( const Blip_Buffer& ); +public: + typedef blip_long buf_t_; + blip_ulong factor_; + blip_resampled_time_t offset_; + buf_t_* buffer_; + blip_long buffer_size_; + blip_long reader_accum_; + int bass_shift_; +private: + long sample_rate_; + long clock_rate_; + int bass_freq_; + int length_; + Blip_Buffer* modified_; // non-zero = true (more optimal than using bool, heh) + friend class Blip_Reader; +}; + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +// Number of bits in resample ratio fraction. Higher values give a more accurate ratio +// but reduce maximum buffer size. +#ifndef BLIP_BUFFER_ACCURACY + #define BLIP_BUFFER_ACCURACY 16 +#endif + +// Number bits in phase offset. Fewer than 6 bits (64 phase offsets) results in +// noticeable broadband noise when synthesizing high frequency square waves. +// Affects size of Blip_Synth objects since they store the waveform directly. +#ifndef BLIP_PHASE_BITS + #if BLIP_BUFFER_FAST + #define BLIP_PHASE_BITS 8 + #else + #define BLIP_PHASE_BITS 6 + #endif +#endif + + // Internal + typedef blip_ulong blip_resampled_time_t; + int const blip_widest_impulse_ = 16; + int const blip_buffer_extra_ = blip_widest_impulse_ + 2; + int const blip_res = 1 << BLIP_PHASE_BITS; + class blip_eq_t; + + class Blip_Synth_Fast_ { + public: + Blip_Buffer* buf; + int last_amp; + int delta_factor; + + void volume_unit( double ); + Blip_Synth_Fast_(); + void treble_eq( blip_eq_t const& ) { } + }; + + class Blip_Synth_ { + public: + Blip_Buffer* buf; + int last_amp; + int delta_factor; + + void volume_unit( double ); + Blip_Synth_( short* impulses, int width ); + void treble_eq( blip_eq_t const& ); + private: + double volume_unit_; + short* const impulses; + int const width; + blip_long kernel_unit; + int impulses_size() const { return blip_res / 2 * width + 1; } + void adjust_impulse(); + }; + +// Quality level, better = slower. In general, use blip_good_quality. +const int blip_med_quality = 8; +const int blip_good_quality = 12; +const int blip_high_quality = 16; + +// Range specifies the greatest expected change in amplitude. Calculate it +// by finding the difference between the maximum and minimum expected +// amplitudes (max - min). +template +class Blip_Synth { +public: + // Sets overall volume of waveform + void volume( double v ) { impl.volume_unit( v * (1.0 / (range < 0 ? -range : range)) ); } + + // Configures low-pass filter (see blip_buffer.txt) + void treble_eq( blip_eq_t const& eq ) { impl.treble_eq( eq ); } + + // Gets/sets Blip_Buffer used for output + Blip_Buffer* output() const { return impl.buf; } + void output( Blip_Buffer* b ) { impl.buf = b; impl.last_amp = 0; } + + // Updates amplitude of waveform at given time. Using this requires a separate + // Blip_Synth for each waveform. + void update( blip_time_t time, int amplitude ); + +// Low-level interface + + // Adds an amplitude transition of specified delta, optionally into specified buffer + // rather than the one set with output(). Delta can be positive or negative. + // The actual change in amplitude is delta * (volume / range) + void offset( blip_time_t, int delta, Blip_Buffer* ) const; + void offset( blip_time_t t, int delta ) const { offset( t, delta, impl.buf ); } + + // Works directly in terms of fractional output samples. Contact author for more info. + void offset_resampled( blip_resampled_time_t, int delta, Blip_Buffer* ) const; + + // Same as offset(), except code is inlined for higher performance + void offset_inline( blip_time_t t, int delta, Blip_Buffer* buf ) const { + offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); + } + void offset_inline( blip_time_t t, int delta ) const { + offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); + } + +private: +#if BLIP_BUFFER_FAST + Blip_Synth_Fast_ impl; +#else + Blip_Synth_ impl; + typedef short imp_t; + imp_t impulses [blip_res * (quality / 2) + 1]; +public: + Blip_Synth() : impl( impulses, quality ) { } +#endif +}; + +// Low-pass equalization parameters +class blip_eq_t { +public: + // Logarithmic rolloff to treble dB at half sampling rate. Negative values reduce + // treble, small positive values (0 to 5.0) increase treble. + blip_eq_t( double treble_db = 0 ); + + // See blip_buffer.txt + blip_eq_t( double treble, long rolloff_freq, long sample_rate, long cutoff_freq = 0 ); + +private: + double treble; + long rolloff_freq; + long sample_rate; + long cutoff_freq; + void generate( float* out, int count ) const; + friend class Blip_Synth_; +}; + +int const blip_sample_bits = 30; + +// Dummy Blip_Buffer to direct sound output to, for easy muting without +// having to stop sound code. +class Silent_Blip_Buffer : public Blip_Buffer { + buf_t_ buf [blip_buffer_extra_ + 1]; +public: + // The following cannot be used (an assertion will fail if attempted): + blargg_err_t set_sample_rate( long samples_per_sec, int msec_length ); + blip_time_t count_clocks( long count ) const; + void mix_samples( blip_sample_t const* buf, long count ); + + Silent_Blip_Buffer(); +}; + + #if __GNUC__ >= 3 || _MSC_VER >= 1400 + #define BLIP_RESTRICT __restrict + #else + #define BLIP_RESTRICT + #endif + +// Optimized reading from Blip_Buffer, for use in custom sample output + +// Begins reading from buffer. Name should be unique to the current block. +#define BLIP_READER_BEGIN( name, blip_buffer ) \ + const Blip_Buffer::buf_t_* BLIP_RESTRICT name##_reader_buf = (blip_buffer).buffer_;\ + blip_long name##_reader_accum = (blip_buffer).reader_accum_ + +// Gets value to pass to BLIP_READER_NEXT() +#define BLIP_READER_BASS( blip_buffer ) ((blip_buffer).bass_shift_) + +// Constant value to use instead of BLIP_READER_BASS(), for slightly more optimal +// code at the cost of having no bass control +int const blip_reader_default_bass = 9; + +// Current sample +#define BLIP_READER_READ( name ) (name##_reader_accum >> (blip_sample_bits - 16)) + +// Current raw sample in full internal resolution +#define BLIP_READER_READ_RAW( name ) (name##_reader_accum) + +// Advances to next sample +#define BLIP_READER_NEXT( name, bass ) \ + (void) (name##_reader_accum += *name##_reader_buf++ - (name##_reader_accum >> (bass))) + +// Ends reading samples from buffer. The number of samples read must now be removed +// using Blip_Buffer::remove_samples(). +#define BLIP_READER_END( name, blip_buffer ) \ + (void) ((blip_buffer).reader_accum_ = name##_reader_accum) + + +// experimental +#define BLIP_READER_ADJ_( name, offset ) (name##_reader_buf += offset) + +blip_long const blip_reader_idx_factor = sizeof (Blip_Buffer::buf_t_); + +#define BLIP_READER_NEXT_IDX_( name, bass, idx ) {\ + name##_reader_accum -= name##_reader_accum >> (bass);\ + name##_reader_accum += name##_reader_buf [(idx)];\ +} + +#define BLIP_READER_NEXT_RAW_IDX_( name, bass, idx ) {\ + name##_reader_accum -= name##_reader_accum >> (bass);\ + name##_reader_accum +=\ + *(Blip_Buffer::buf_t_ const*) ((char const*) name##_reader_buf + (idx));\ +} + +// Compatibility with older version +const long blip_unscaled = 65535; +const int blip_low_quality = blip_med_quality; +const int blip_best_quality = blip_high_quality; + +// Deprecated; use BLIP_READER macros as follows: +// Blip_Reader r; r.begin( buf ); -> BLIP_READER_BEGIN( r, buf ); +// int bass = r.begin( buf ) -> BLIP_READER_BEGIN( r, buf ); int bass = BLIP_READER_BASS( buf ); +// r.read() -> BLIP_READER_READ( r ) +// r.read_raw() -> BLIP_READER_READ_RAW( r ) +// r.next( bass ) -> BLIP_READER_NEXT( r, bass ) +// r.next() -> BLIP_READER_NEXT( r, blip_reader_default_bass ) +// r.end( buf ) -> BLIP_READER_END( r, buf ) +class Blip_Reader { +public: + int begin( Blip_Buffer& ); + blip_long read() const { return accum >> (blip_sample_bits - 16); } + blip_long read_raw() const { return accum; } + void next( int bass_shift = 9 ) { accum += *buf++ - (accum >> bass_shift); } + void end( Blip_Buffer& b ) { b.reader_accum_ = accum; } +private: + const Blip_Buffer::buf_t_* buf; + blip_long accum; +}; + +#if defined (_M_IX86) || defined (_M_IA64) || defined (__i486__) || \ + defined (__x86_64__) || defined (__ia64__) || defined (__i386__) + #define BLIP_CLAMP_( in ) in < -0x8000 || 0x7FFF < in +#else + #define BLIP_CLAMP_( in ) (blip_sample_t) in != in +#endif + +// Clamp sample to blip_sample_t range +#define BLIP_CLAMP( sample, out )\ + { if ( BLIP_CLAMP_( (sample) ) ) (out) = ((sample) >> 24) ^ 0x7FFF; } + +struct blip_buffer_state_t +{ + blip_resampled_time_t offset_; + blip_long reader_accum_; + blip_long buf [blip_buffer_extra_]; +}; + +// End of public interface + +#ifndef assert + #include +#endif + +template +inline void Blip_Synth::offset_resampled( blip_resampled_time_t time, + int delta, Blip_Buffer* blip_buf ) const +{ + // If this assertion fails, it means that an attempt was made to add a delta + // at a negative time or past the end of the buffer. + assert( (blip_long) (time >> BLIP_BUFFER_ACCURACY) < blip_buf->buffer_size_ ); + + delta *= impl.delta_factor; + blip_long* BLIP_RESTRICT buf = blip_buf->buffer_ + (time >> BLIP_BUFFER_ACCURACY); + int phase = (int) (time >> (BLIP_BUFFER_ACCURACY - BLIP_PHASE_BITS) & (blip_res - 1)); + +#if BLIP_BUFFER_FAST + blip_long left = buf [0] + delta; + + // Kind of crappy, but doing shift after multiply results in overflow. + // Alternate way of delaying multiply by delta_factor results in worse + // sub-sample resolution. + blip_long right = (delta >> BLIP_PHASE_BITS) * phase; + left -= right; + right += buf [1]; + + buf [0] = left; + buf [1] = right; +#else + + int const fwd = (blip_widest_impulse_ - quality) / 2; + int const rev = fwd + quality - 2; + int const mid = quality / 2 - 1; + + imp_t const* BLIP_RESTRICT imp = impulses + blip_res - phase; + + #if defined (_M_IX86) || defined (_M_IA64) || defined (__i486__) || \ + defined (__x86_64__) || defined (__ia64__) || defined (__i386__) + + // this straight forward version gave in better code on GCC for x86 + + #define ADD_IMP( out, in ) \ + buf [out] += (blip_long) imp [blip_res * (in)] * delta + + #define BLIP_FWD( i ) {\ + ADD_IMP( fwd + i, i );\ + ADD_IMP( fwd + 1 + i, i + 1 );\ + } + #define BLIP_REV( r ) {\ + ADD_IMP( rev - r, r + 1 );\ + ADD_IMP( rev + 1 - r, r );\ + } + + BLIP_FWD( 0 ) + if ( quality > 8 ) BLIP_FWD( 2 ) + if ( quality > 12 ) BLIP_FWD( 4 ) + { + ADD_IMP( fwd + mid - 1, mid - 1 ); + ADD_IMP( fwd + mid , mid ); + imp = impulses + phase; + } + if ( quality > 12 ) BLIP_REV( 6 ) + if ( quality > 8 ) BLIP_REV( 4 ) + BLIP_REV( 2 ) + + ADD_IMP( rev , 1 ); + ADD_IMP( rev + 1, 0 ); + + #undef ADD_IMP + + #else + + // for RISC processors, help compiler by reading ahead of writes + + #define BLIP_FWD( i ) {\ + blip_long t0 = i0 * delta + buf [fwd + i];\ + blip_long t1 = imp [blip_res * (i + 1)] * delta + buf [fwd + 1 + i];\ + i0 = imp [blip_res * (i + 2)];\ + buf [fwd + i] = t0;\ + buf [fwd + 1 + i] = t1;\ + } + #define BLIP_REV( r ) {\ + blip_long t0 = i0 * delta + buf [rev - r];\ + blip_long t1 = imp [blip_res * r] * delta + buf [rev + 1 - r];\ + i0 = imp [blip_res * (r - 1)];\ + buf [rev - r] = t0;\ + buf [rev + 1 - r] = t1;\ + } + + blip_long i0 = *imp; + BLIP_FWD( 0 ) + if ( quality > 8 ) BLIP_FWD( 2 ) + if ( quality > 12 ) BLIP_FWD( 4 ) + { + blip_long t0 = i0 * delta + buf [fwd + mid - 1]; + blip_long t1 = imp [blip_res * mid] * delta + buf [fwd + mid ]; + imp = impulses + phase; + i0 = imp [blip_res * mid]; + buf [fwd + mid - 1] = t0; + buf [fwd + mid ] = t1; + } + if ( quality > 12 ) BLIP_REV( 6 ) + if ( quality > 8 ) BLIP_REV( 4 ) + BLIP_REV( 2 ) + + blip_long t0 = i0 * delta + buf [rev ]; + blip_long t1 = *imp * delta + buf [rev + 1]; + buf [rev ] = t0; + buf [rev + 1] = t1; + #endif + +#endif +} + +#undef BLIP_FWD +#undef BLIP_REV + +template +#if BLIP_BUFFER_FAST + inline +#endif +void Blip_Synth::offset( blip_time_t t, int delta, Blip_Buffer* buf ) const +{ + offset_resampled( t * buf->factor_ + buf->offset_, delta, buf ); +} + +template +#if BLIP_BUFFER_FAST + inline +#endif +void Blip_Synth::update( blip_time_t t, int amp ) +{ + int delta = amp - impl.last_amp; + impl.last_amp = amp; + offset_resampled( t * impl.buf->factor_ + impl.buf->offset_, delta, impl.buf ); +} + +inline blip_eq_t::blip_eq_t( double t ) : + treble( t ), rolloff_freq( 0 ), sample_rate( 44100 ), cutoff_freq( 0 ) { } +inline blip_eq_t::blip_eq_t( double t, long rf, long sr, long cf ) : + treble( t ), rolloff_freq( rf ), sample_rate( sr ), cutoff_freq( cf ) { } + +inline int Blip_Buffer::length() const { return length_; } +inline long Blip_Buffer::samples_avail() const { return (long) (offset_ >> BLIP_BUFFER_ACCURACY); } +inline long Blip_Buffer::sample_rate() const { return sample_rate_; } +inline int Blip_Buffer::output_latency() const { return blip_widest_impulse_ / 2; } +inline long Blip_Buffer::clock_rate() const { return clock_rate_; } +inline void Blip_Buffer::clock_rate( long cps ) { factor_ = clock_rate_factor( clock_rate_ = cps ); } + +inline int Blip_Reader::begin( Blip_Buffer& blip_buf ) +{ + buf = blip_buf.buffer_; + accum = blip_buf.reader_accum_; + return blip_buf.bass_shift_; +} + +inline void Blip_Buffer::remove_silence( long count ) +{ + // fails if you try to remove more samples than available + assert( count <= samples_avail() ); + offset_ -= (blip_resampled_time_t) count << BLIP_BUFFER_ACCURACY; +} + +inline blip_ulong Blip_Buffer::unsettled() const +{ + return reader_accum_ >> (blip_sample_bits - 16); +} + +int const blip_max_length = 0; +int const blip_default_length = 250; // 1/4 second + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/CheatSearch.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/CheatSearch.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,371 @@ +#include +#include + +#include "CheatSearch.h" + +CheatSearchBlock cheatSearchBlocks[4]; + +CheatSearchData cheatSearchData = { + 0, + cheatSearchBlocks +}; + +static bool cheatSearchEQ(u32 a, u32 b) +{ + return a == b; +} + +static bool cheatSearchNE(u32 a, u32 b) +{ + return a != b; +} + +static bool cheatSearchLT(u32 a, u32 b) +{ + return a < b; +} + +static bool cheatSearchLE(u32 a, u32 b) +{ + return a <= b; +} + +static bool cheatSearchGT(u32 a, u32 b) +{ + return a > b; +} + +static bool cheatSearchGE(u32 a, u32 b) +{ + return a >= b; +} + +static bool cheatSearchSignedEQ(s32 a, s32 b) +{ + return a == b; +} + +static bool cheatSearchSignedNE(s32 a, s32 b) +{ + return a != b; +} + +static bool cheatSearchSignedLT(s32 a, s32 b) +{ + return a < b; +} + +static bool cheatSearchSignedLE(s32 a, s32 b) +{ + return a <= b; +} + +static bool cheatSearchSignedGT(s32 a, s32 b) +{ + return a > b; +} + +static bool cheatSearchSignedGE(s32 a, s32 b) +{ + return a >= b; +} + +static bool (*cheatSearchFunc[])(u32, u32) = { + cheatSearchEQ, + cheatSearchNE, + cheatSearchLT, + cheatSearchLE, + cheatSearchGT, + cheatSearchGE +}; + +static bool (*cheatSearchSignedFunc[])(s32, s32) = { + cheatSearchSignedEQ, + cheatSearchSignedNE, + cheatSearchSignedLT, + cheatSearchSignedLE, + cheatSearchSignedGT, + cheatSearchSignedGE +}; + +void cheatSearchSetSavedAndBits(CheatSearchBlock *block) +{ + if (!block->saved) + { + block->saved = (u8 *)malloc(block->size); + memcpy(block->saved, block->data, block->size); + } + if (!block->bits) + { + block->bits = (u8 *)malloc(block->size >> 3); + memset(block->bits, 0xff, block->size >> 3); + } +} + +void cheatSearchZeroBlock(CheatSearchBlock *block) +{ + block->data = 0; + block->offset = 0; + block->size = 0; + free(block->saved); + free(block->bits); + block->saved = 0; + block->bits = 0; +} + +void cheatSearchCleanup(CheatSearchData *cs) +{ + int count = cs->count; + + for (int i = 0; i < count; i++) + { + CheatSearchBlock &block = cs->blocks[i]; + free(block.saved); + free(block.bits); + block.saved = 0; + block.bits = 0; + } + cs->count = 0; +} + +void cheatSearchStart(const CheatSearchData *cs) +{ + int count = cs->count; + + for (int i = 0; i < count; i++) + { + CheatSearchBlock *block = &cs->blocks[i]; + + memset(block->bits, 0xff, block->size >> 3); + memcpy(block->saved, block->data, block->size); + } +} + +s32 cheatSearchSignedRead(u8 *data, int off, int size) +{ + u32 res = data[off++]; + + switch (size) + { + case BITS_8: + res <<= 24; + return ((s32)res) >> 24; + case BITS_16: + res |= ((u32)data[off++])<<8; + res <<= 16; + return ((s32)res) >> 16; + case BITS_32: + res |= ((u32)data[off++])<<8; + res |= ((u32)data[off++])<<16; + res |= ((u32)data[off++])<<24; + return (s32)res; + } + return (s32)res; +} + +u32 cheatSearchRead(u8 *data, int off, int size) +{ + u32 res = data[off++]; + if (size == BITS_16) + res |= ((u32)data[off++])<<8; + else if (size == BITS_32) + { + res |= ((u32)data[off++])<<8; + res |= ((u32)data[off++])<<16; + res |= ((u32)data[off++])<<24; + } + return res; +} + +void cheatSearch(const CheatSearchData *cs, int compare, int size, + bool isSigned) +{ + if (compare < 0 || compare > SEARCH_GE) + return; + int inc = 1; + if (size == BITS_16) + inc = 2; + else if (size == BITS_32) + inc = 4; + + if (isSigned) + { + bool (*func)(s32, s32) = cheatSearchSignedFunc[compare]; + + for (int i = 0; i < cs->count; i++) + { + CheatSearchBlock *block = &cs->blocks[i]; + int size2 = block->size; + u8 *bits = block->bits; + u8 *data = block->data; + u8 *saved = block->saved; + + for (int j = 0; j < size2; j += inc) + { + if (IS_BIT_SET(bits, j)) + { + s32 a = cheatSearchSignedRead(data, j, size); + s32 b = cheatSearchSignedRead(saved, j, size); + + if (!func(a, b)) + { + CLEAR_BIT(bits, j); + if (size == BITS_16) + CLEAR_BIT(bits, j+1); + if (size == BITS_32) + { + CLEAR_BIT(bits, j+2); + CLEAR_BIT(bits, j+3); + } + } + } + } + } + } + else + { + bool (*func)(u32, u32) = cheatSearchFunc[compare]; + + for (int i = 0; i < cs->count; i++) + { + CheatSearchBlock *block = &cs->blocks[i]; + int size2 = block->size; + u8 *bits = block->bits; + u8 *data = block->data; + u8 *saved = block->saved; + + for (int j = 0; j < size2; j += inc) + { + if (IS_BIT_SET(bits, j)) + { + u32 a = cheatSearchRead(data, j, size); + u32 b = cheatSearchRead(saved, j, size); + + if (!func(a, b)) + { + CLEAR_BIT(bits, j); + if (size == BITS_16) + CLEAR_BIT(bits, j+1); + if (size == BITS_32) + { + CLEAR_BIT(bits, j+2); + CLEAR_BIT(bits, j+3); + } + } + } + } + } + } +} + +void cheatSearchValue(const CheatSearchData *cs, int compare, int size, + bool isSigned, u32 value) +{ + if (compare < 0 || compare > SEARCH_GE) + return; + int inc = 1; + if (size == BITS_16) + inc = 2; + else if (size == BITS_32) + inc = 4; + + if (isSigned) + { + bool (*func)(s32, s32) = cheatSearchSignedFunc[compare]; + + for (int i = 0; i < cs->count; i++) + { + CheatSearchBlock *block = &cs->blocks[i]; + int size2 = block->size; + u8 *bits = block->bits; + u8 *data = block->data; + + for (int j = 0; j < size2; j += inc) + { + if (IS_BIT_SET(bits, j)) + { + s32 a = cheatSearchSignedRead(data, j, size); + s32 b = (s32)value; + + if (!func(a, b)) + { + CLEAR_BIT(bits, j); + if (size == BITS_16) + CLEAR_BIT(bits, j+1); + if (size == BITS_32) + { + CLEAR_BIT(bits, j+2); + CLEAR_BIT(bits, j+3); + } + } + } + } + } + } + else + { + bool (*func)(u32, u32) = cheatSearchFunc[compare]; + + for (int i = 0; i < cs->count; i++) + { + CheatSearchBlock *block = &cs->blocks[i]; + int size2 = block->size; + u8 *bits = block->bits; + u8 *data = block->data; + + for (int j = 0; j < size2; j += inc) + { + if (IS_BIT_SET(bits, j)) + { + u32 a = cheatSearchRead(data, j, size); + + if (!func(a, value)) + { + CLEAR_BIT(bits, j); + if (size == BITS_16) + CLEAR_BIT(bits, j+1); + if (size == BITS_32) + { + CLEAR_BIT(bits, j+2); + CLEAR_BIT(bits, j+3); + } + } + } + } + } + } +} + +int cheatSearchGetCount(const CheatSearchData *cs, int size) +{ + int res = 0; + int inc = 1; + if (size == BITS_16) + inc = 2; + else if (size == BITS_32) + inc = 4; + + for (int i = 0; i < cs->count; i++) + { + CheatSearchBlock *block = &cs->blocks[i]; + + int size2 = block->size; + u8 *bits = block->bits; + for (int j = 0; j < size2; j += inc) + { + if (IS_BIT_SET(bits, j)) + res++; + } + } + return res; +} + +void cheatSearchUpdateValues(const CheatSearchData *cs) +{ + for (int i = 0; i < cs->count; i++) + { + CheatSearchBlock *block = &cs->blocks[i]; + + memcpy(block->saved, block->data, block->size); + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/CheatSearch.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/CheatSearch.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,65 @@ +#ifndef VBA_CHEATSEARCH_H +#define VBA_CHEATSEARCH_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../Port.h" + +struct CheatSearchBlock +{ + u8 *data; + int size; + u32 offset; + u8 *saved; + u8 *bits; +}; + +struct CheatSearchData +{ + int count; + CheatSearchBlock *blocks; +}; + +enum +{ + SEARCH_EQ, + SEARCH_NE, + SEARCH_LT, + SEARCH_LE, + SEARCH_GT, + SEARCH_GE +}; + +enum +{ + BITS_8, + BITS_16, + BITS_32 +}; + +#define SET_BIT(bits, off) \ + (bits)[(off) >> 3] |= (1 << ((off) & 7)) + +#define CLEAR_BIT(bits, off) \ + (bits)[(off) >> 3] &= ~(1 << ((off) & 7)) + +#define IS_BIT_SET(bits, off) \ + (bits)[(off) >> 3] & (1 << ((off) & 7)) + +extern CheatSearchData cheatSearchData; +extern void cheatSearchSetSavedAndBits(CheatSearchBlock *block); +extern void cheatSearchZeroBlock(CheatSearchBlock *block); +extern void cheatSearchCleanup(CheatSearchData *cs); +extern void cheatSearchStart(const CheatSearchData *cs); +extern void cheatSearch(const CheatSearchData *cs, int compare, int size, + bool isSigned); +extern void cheatSearchValue(const CheatSearchData *cs, int compare, int size, + bool isSigned, u32 value); +extern int cheatSearchGetCount(const CheatSearchData *cs, int size); +extern void cheatSearchUpdateValues(const CheatSearchData *cs); +extern s32 cheatSearchSignedRead(u8 *data, int off, int size); +extern u32 cheatSearchRead(u8 *data, int off, int size); + +#endif // VBA_CHEATSEARCH_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,23 @@ +noinst_LIBRARIES = libgbcom.a + +libgbcom_a_SOURCES = \ + lua-engine.cpp \ + memgzio.c \ + memgzio.h \ + movie.cpp \ + movie.h \ + System.h \ + Text.cpp \ + Text.h \ + unzip.cpp \ + unzip.h \ + Util.cpp \ + Util.h \ + vbalua.h + +AM_CPPFLAGS = \ + -I$(top_srcdir)/src \ + -DSDL \ + -DSYSCONFDIR=\"$(sysconfdir)\" + +AM_CXXFLAGS = -fno-exceptions @SDL_CFLAGS@ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/System.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/System.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,137 @@ +#ifndef VBA_SYSTEM_H +#define VBA_SYSTEM_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +// c++ lacks a way to implement Smart Referrences or Delphi-Style Properties +// in order to maintain consistency, value-copied things should not be modified too often +struct EmulatedSystem +{ + // main emulation function + void (*emuMain)(int); + // reset emulator + void (*emuReset)(bool); + // clean up memory + void (*emuCleanUp)(); + // load battery file + bool (*emuReadBattery)(const char *); + // write battery file + bool (*emuWriteBattery)(const char *); + // load battery file from stream + bool (*emuReadBatteryFromStream)(gzFile); + // write battery file to stream + bool (*emuWriteBatteryToStream)(gzFile); + // load state + bool (*emuReadState)(const char *); + // save state + bool (*emuWriteState)(const char *); + // load state from stream + bool (*emuReadStateFromStream)(gzFile); + // save state to stream + bool (*emuWriteStateToStream)(gzFile); + // load memory state (rewind) + bool (*emuReadMemState)(char *, int); + // write memory state (rewind) + bool (*emuWriteMemState)(char *, int); + // write PNG file + bool (*emuWritePNG)(const char *); + // write BMP file + bool (*emuWriteBMP)(const char *); + // emulator update CPSR (ARM only) + void (*emuUpdateCPSR)(); + // emulator has debugger + bool emuHasDebugger; + // clock ticks to emulate + int emuCount; +}; + +// why not convert the value type only when doing I/O? +struct EmulatedSystemCounters +{ + int32 frameCount; + int32 lagCount; + int32 extraCount; + bool8 lagged; + bool8 laggedLast; +}; + +extern struct EmulatedSystem theEmulator; +extern struct EmulatedSystemCounters systemCounters; + +extern void log(const char *, ...); + +extern void systemGbPrint(u8 *, int, int, int, int); +extern int systemScreenCapture(int); +extern void systemRefreshScreen(); +extern void systemRenderFrame(); +extern void systemRedrawScreen(); +extern void systemUpdateListeners(); +// updates the joystick data +extern void systemSetSensorX(int32); +extern void systemSetSensorY(int32); +extern void systemResetSensor(); +extern int32 systemGetSensorX(); +extern int32 systemGetSensorY(); +extern void systemUpdateMotionSensor(int); +extern int systemGetDefaultJoypad(); +extern void systemSetDefaultJoypad(int); +extern bool systemReadJoypads(); +// return information about the given joystick, -1 for default joystick... the bool is for if motion sensor should be handled +// too +extern u32 systemGetOriginalJoypad(int, bool); +extern u32 systemGetJoypad(int, bool); +extern void systemSetJoypad(int, u32); +extern void systemClearJoypads(); +extern void systemMessage(int, const char *, ...); +extern void systemScreenMessage(const char *msg, int slot = 0, int duration = 3000, const char *colorList = NULL); +extern bool systemSoundInit(); +extern void systemSoundShutdown(); +extern void systemSoundPause(); +extern void systemSoundResume(); +extern bool systemSoundIsPaused(); +extern void systemSoundReset(); +extern void systemSoundWriteToBuffer(); +extern void systemSoundClearBuffer(); +extern bool systemSoundCanChangeQuality(); +extern bool systemSoundSetQuality(int quality); +extern u32 systemGetClock(); +extern void systemSetTitle(const char *); +extern void systemShowSpeed(int); +extern void systemIncreaseThrottle(); +extern void systemDecreaseThrottle(); +extern void systemSetThrottle(int); +extern int systemGetThrottle(); +extern void systemFrame(); +extern int systemFramesToSkip(); +extern bool systemIsEmulating(); +extern void systemGbBorderOn(); +extern bool systemIsRunningGBA(); +extern bool systemIsSpedUp(); +extern bool systemIsPaused(); +extern void systemSetPause(bool pause); +extern bool systemPauseOnFrame(); + +extern int systemCartridgeType; +extern int systemSpeed; +extern bool systemSoundOn; +extern u16 systemColorMap16[0x10000]; +extern u32 systemColorMap32[0x10000]; +extern u16 systemGbPalette[24]; +extern int systemRedShift; +extern int systemGreenShift; +extern int systemBlueShift; +extern int systemColorDepth; +extern int systemDebug; +extern int systemVerbose; +extern int systemFrameSkip; +extern int systemSaveUpdateCounter; + +#define SYSTEM_SAVE_UPDATED 30 +#define SYSTEM_SAVE_NOT_UPDATED 0 + +#endif // VBA_SYSTEM_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/Text.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/Text.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,496 @@ +/* FCE Ultra - NES/Famicom Emulator + * + * Copyright notice for this file: + * Copyright (C) 2002 Ben Parnell + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* Code originally from fceu/drawing.h file, adapted by Forgotten + */ +#include "System.h" + +bool outlinedText = true, transparentText = false; +int textColor = 0, textMethod = 1; + +extern u32 RGB_LOW_BITS_MASK; + +static const u8 fontdata2[2048] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x81, 0xa5, 0x81, 0xbd, 0x99, 0x81, 0x7e, 0x7e, 0xff, 0xdb, 0xff, + 0xc3, 0xe7, 0xff, 0x7e, 0x36, 0x7f, 0x7f, 0x7f, 0x3e, 0x1c, 0x08, 0x00, 0x08, 0x1c, 0x3e, 0x7f, 0x3e, 0x1c, 0x08, 0x00, + 0x1c, + 0x3e, 0x1c, 0x7f, 0x7f, 0x3e, 0x1c, 0x3e, 0x08, 0x08, 0x1c, 0x3e, 0x7f, 0x3e, 0x1c, 0x3e, 0x00, 0x00, 0x18, 0x3c, 0x3c, + 0x18, + 0x00, 0x00, 0xff, 0xff, 0xe7, 0xc3, 0xc3, 0xe7, 0xff, 0xff, 0x00, 0x3c, 0x66, 0x42, 0x42, 0x66, 0x3c, 0x00, 0xff, 0xc3, + 0x99, 0xbd, 0xbd, 0x99, 0xc3, 0xff, 0xf0, 0xe0, 0xf0, 0xbe, 0x33, 0x33, 0x33, 0x1e, 0x3c, 0x66, 0x66, 0x66, 0x3c, 0x18, + 0x7e, + 0x18, 0xfc, 0xcc, 0xfc, 0x0c, 0x0c, 0x0e, 0x0f, 0x07, 0xfe, 0xc6, 0xfe, 0xc6, 0xc6, 0xe6, 0x67, 0x03, 0x99, 0x5a, 0x3c, + 0xe7, + 0xe7, 0x3c, 0x5a, 0x99, 0x01, 0x07, 0x1f, 0x7f, 0x1f, 0x07, 0x01, 0x00, 0x40, 0x70, 0x7c, 0x7f, 0x7c, 0x70, 0x40, 0x00, + 0x18, + 0x3c, 0x7e, 0x18, 0x18, 0x7e, 0x3c, 0x18, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x00, 0xfe, 0xdb, 0xdb, 0xde, 0xd8, + 0xd8, + 0xd8, 0x00, 0x7c, 0xc6, 0x1c, 0x36, 0x36, 0x1c, 0x33, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x7e, 0x00, 0x18, 0x3c, + 0x7e, + 0x18, 0x7e, 0x3c, 0x18, 0xff, 0x18, 0x3c, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x3c, 0x18, + 0x00, + 0x00, 0x18, 0x30, 0x7f, 0x30, 0x18, 0x00, 0x00, 0x00, 0x0c, 0x06, 0x7f, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, + 0x03, + 0x7f, 0x00, 0x00, 0x00, 0x24, 0x66, 0xff, 0x66, 0x24, 0x00, 0x00, 0x00, 0x18, 0x3c, 0x7e, 0xff, 0xff, 0x00, 0x00, 0x00, + 0xff, + 0xff, 0x7e, 0x3c, 0x18, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x1e, 0x1e, 0x0c, 0x0c, 0x00, 0x0c, 0x00, 0x36, 0x36, 0x36, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x36, 0x36, 0x7f, 0x36, 0x7f, 0x36, 0x36, 0x00, 0x0c, 0x3e, 0x03, 0x1e, 0x30, 0x1f, 0x0c, 0x00, + 0x00, + 0x63, 0x33, 0x18, 0x0c, 0x66, 0x63, 0x00, 0x1c, 0x36, 0x1c, 0x6e, 0x3b, 0x33, 0x6e, 0x00, 0x06, 0x06, 0x03, 0x00, 0x00, + 0x00, + 0x00, 0x00, 0x18, 0x0c, 0x06, 0x06, 0x06, 0x0c, 0x18, 0x00, 0x06, 0x0c, 0x18, 0x18, 0x18, 0x0c, 0x06, 0x00, 0x00, 0x66, + 0x3c, 0xff, 0x3c, 0x66, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x3f, 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, + 0x0c, + 0x06, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x00, 0x60, 0x30, 0x18, + 0x0c, + 0x06, 0x03, 0x01, 0x00, 0x3e, 0x63, 0x73, 0x7b, 0x6f, 0x67, 0x3e, 0x00, 0x0c, 0x0e, 0x0c, 0x0c, 0x0c, 0x0c, 0x3f, 0x00, + 0x1e, + 0x33, 0x30, 0x1c, 0x06, 0x33, 0x3f, 0x00, 0x1e, 0x33, 0x30, 0x1c, 0x30, 0x33, 0x1e, 0x00, 0x38, 0x3c, 0x36, 0x33, 0x7f, + 0x30, + 0x78, 0x00, 0x3f, 0x03, 0x1f, 0x30, 0x30, 0x33, 0x1e, 0x00, 0x1c, 0x06, 0x03, 0x1f, 0x33, 0x33, 0x1e, 0x00, 0x3f, 0x33, + 0x30, + 0x18, 0x0c, 0x0c, 0x0c, 0x00, 0x1e, 0x33, 0x33, 0x1e, 0x33, 0x33, 0x1e, 0x00, 0x1e, 0x33, 0x33, 0x3e, 0x30, 0x18, 0x0e, + 0x00, + 0x00, 0x0c, 0x0c, 0x00, 0x00, 0x0c, 0x0c, 0x00, 0x00, 0x0c, 0x0c, 0x00, 0x00, 0x0c, 0x0c, 0x06, 0x18, 0x0c, 0x06, 0x03, + 0x06, + 0x0c, 0x18, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x06, 0x0c, 0x18, 0x30, 0x18, 0x0c, 0x06, 0x00, 0x1e, + 0x33, + 0x30, 0x18, 0x0c, 0x00, 0x0c, 0x00, + 0x3e, 0x63, 0x7b, 0x7b, 0x7b, 0x03, 0x1e, 0x00, 0x0c, 0x1e, 0x33, 0x33, 0x3f, 0x33, 0x33, 0x00, 0x3f, 0x66, 0x66, 0x3e, + 0x66, 0x66, 0x3f, 0x00, 0x3c, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3c, 0x00, 0x1f, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1f, 0x00, + 0x7f, + 0x46, 0x16, 0x1e, 0x16, 0x46, 0x7f, 0x00, 0x7f, 0x46, 0x16, 0x1e, 0x16, 0x06, 0x0f, 0x00, 0x3c, 0x66, 0x03, 0x03, 0x73, + 0x66, + 0x7c, 0x00, 0x33, 0x33, 0x33, 0x3f, 0x33, 0x33, 0x33, 0x00, 0x1e, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x00, 0x78, 0x30, + 0x30, 0x30, 0x33, 0x33, 0x1e, 0x00, 0x67, 0x66, 0x36, 0x1e, 0x36, 0x66, 0x67, 0x00, 0x0f, 0x06, 0x06, 0x06, 0x46, 0x66, + 0x7f, + 0x00, 0x63, 0x77, 0x7f, 0x7f, 0x6b, 0x63, 0x63, 0x00, 0x63, 0x67, 0x6f, 0x7b, 0x73, 0x63, 0x63, 0x00, 0x1c, 0x36, 0x63, + 0x63, + 0x63, 0x36, 0x1c, 0x00, 0x3f, 0x66, 0x66, 0x3e, 0x06, 0x06, 0x0f, 0x00, 0x1e, 0x33, 0x33, 0x33, 0x3b, 0x1e, 0x38, 0x00, + 0x3f, + 0x66, 0x66, 0x3e, 0x36, 0x66, 0x67, 0x00, 0x1e, 0x33, 0x07, 0x0e, 0x38, 0x33, 0x1e, 0x00, 0x3f, 0x2d, 0x0c, 0x0c, 0x0c, + 0x0c, + 0x1e, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3f, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x1e, 0x0c, 0x00, 0x63, 0x63, + 0x63, + 0x6b, 0x7f, 0x77, 0x63, 0x00, 0x63, 0x63, 0x36, 0x1c, 0x1c, 0x36, 0x63, 0x00, 0x33, 0x33, 0x33, 0x1e, 0x0c, 0x0c, 0x1e, + 0x00, + 0x7f, 0x63, 0x31, 0x18, 0x4c, 0x66, 0x7f, 0x00, 0x1e, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1e, 0x00, 0x03, 0x06, 0x0c, 0x18, + 0x30, + 0x60, 0x40, 0x00, 0x1e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1e, 0x00, 0x08, 0x1c, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, + 0x0c, 0x0c, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x30, 0x3e, 0x33, 0x6e, 0x00, 0x07, 0x06, 0x06, 0x3e, + 0x66, 0x66, 0x3b, 0x00, 0x00, 0x00, 0x1e, 0x33, 0x03, 0x33, 0x1e, 0x00, 0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6e, 0x00, + 0x00, + 0x00, 0x1e, 0x33, 0x3f, 0x03, 0x1e, 0x00, 0x1c, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x6e, 0x33, 0x33, + 0x3e, + 0x30, 0x1f, 0x07, 0x06, 0x36, 0x6e, 0x66, 0x66, 0x67, 0x00, 0x0c, 0x00, 0x0e, 0x0c, 0x0c, 0x0c, 0x1e, 0x00, 0x30, 0x00, + 0x30, 0x30, 0x30, 0x33, 0x33, 0x1e, 0x07, 0x06, 0x66, 0x36, 0x1e, 0x36, 0x67, 0x00, 0x0e, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x1e, + 0x00, 0x00, 0x00, 0x33, 0x7f, 0x7f, 0x6b, 0x63, 0x00, 0x00, 0x00, 0x1f, 0x33, 0x33, 0x33, 0x33, 0x00, 0x00, 0x00, 0x1e, + 0x33, + 0x33, 0x33, 0x1e, 0x00, 0x00, 0x00, 0x3b, 0x66, 0x66, 0x3e, 0x06, 0x0f, 0x00, 0x00, 0x6e, 0x33, 0x33, 0x3e, 0x30, 0x78, + 0x00, + 0x00, 0x3b, 0x6e, 0x66, 0x06, 0x0f, 0x00, 0x00, 0x00, 0x3e, 0x03, 0x1e, 0x30, 0x1f, 0x00, 0x08, 0x0c, 0x3e, 0x0c, 0x0c, + 0x2c, + 0x18, 0x00, 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6e, 0x00, 0x00, 0x00, 0x33, 0x33, 0x33, 0x1e, 0x0c, 0x00, 0x00, 0x00, + 0x63, + 0x6b, 0x7f, 0x7f, 0x36, 0x00, 0x00, 0x00, 0x63, 0x36, 0x1c, 0x36, 0x63, 0x00, 0x00, 0x00, 0x33, 0x33, 0x33, 0x3e, 0x30, + 0x1f, + 0x00, 0x00, 0x3f, 0x19, 0x0c, 0x26, 0x3f, 0x00, 0x38, 0x0c, 0x0c, 0x07, 0x0c, 0x0c, 0x38, 0x00, 0x18, 0x18, 0x18, 0x00, + 0x18, + 0x18, 0x18, 0x00, 0x07, 0x0c, 0x0c, 0x38, 0x0c, 0x0c, 0x07, 0x00, 0x6e, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x08, + 0x1c, 0x36, 0x63, 0x63, 0x7f, 0x00, + 0x1e, 0x33, 0x03, 0x33, 0x1e, 0x18, 0x30, 0x1e, 0x00, 0x33, 0x00, 0x33, 0x33, 0x33, 0x7e, 0x00, 0x38, 0x00, 0x1e, 0x33, + 0x3f, 0x03, 0x1e, 0x00, 0x7e, 0xc3, 0x3c, 0x60, 0x7c, 0x66, 0xfc, 0x00, 0x33, 0x00, 0x1e, 0x30, 0x3e, 0x33, 0x7e, 0x00, + 0x07, + 0x00, 0x1e, 0x30, 0x3e, 0x33, 0x7e, 0x00, 0x0c, 0x0c, 0x1e, 0x30, 0x3e, 0x33, 0x7e, 0x00, 0x00, 0x00, 0x1e, 0x03, 0x03, + 0x1e, + 0x30, 0x1c, 0x7e, 0xc3, 0x3c, 0x66, 0x7e, 0x06, 0x3c, 0x00, 0x33, 0x00, 0x1e, 0x33, 0x3f, 0x03, 0x1e, 0x00, 0x07, 0x00, + 0x1e, 0x33, 0x3f, 0x03, 0x1e, 0x00, 0x33, 0x00, 0x0e, 0x0c, 0x0c, 0x0c, 0x1e, 0x00, 0x3e, 0x63, 0x1c, 0x18, 0x18, 0x18, + 0x3c, + 0x00, 0x07, 0x00, 0x0e, 0x0c, 0x0c, 0x0c, 0x1e, 0x00, 0x63, 0x1c, 0x36, 0x63, 0x7f, 0x63, 0x63, 0x00, 0x0c, 0x0c, 0x00, + 0x1e, + 0x33, 0x3f, 0x33, 0x00, 0x38, 0x00, 0x3f, 0x06, 0x1e, 0x06, 0x3f, 0x00, 0x00, 0x00, 0xfe, 0x30, 0xfe, 0x33, 0xfe, 0x00, + 0x7c, + 0x36, 0x33, 0x7f, 0x33, 0x33, 0x73, 0x00, 0x1e, 0x33, 0x00, 0x1e, 0x33, 0x33, 0x1e, 0x00, 0x00, 0x33, 0x00, 0x1e, 0x33, + 0x33, + 0x1e, 0x00, 0x00, 0x07, 0x00, 0x1e, 0x33, 0x33, 0x1e, 0x00, 0x1e, 0x33, 0x00, 0x33, 0x33, 0x33, 0x7e, 0x00, 0x00, 0x07, + 0x00, + 0x33, 0x33, 0x33, 0x7e, 0x00, 0x00, 0x33, 0x00, 0x33, 0x33, 0x3e, 0x30, 0x1f, 0xc3, 0x18, 0x3c, 0x66, 0x66, 0x3c, 0x18, + 0x00, + 0x33, 0x00, 0x33, 0x33, 0x33, 0x33, 0x1e, 0x00, 0x18, 0x18, 0x7e, 0x03, 0x03, 0x7e, 0x18, 0x18, 0x1c, 0x36, 0x26, 0x0f, + 0x06, + 0x67, 0x3f, 0x00, 0x33, 0x33, 0x1e, 0x3f, 0x0c, 0x3f, 0x0c, 0x0c, 0x1f, 0x33, 0x33, 0x5f, 0x63, 0xf3, 0x63, 0xe3, 0x70, + 0xd8, + 0x18, 0x3c, 0x18, 0x18, 0x1b, 0x0e, + 0x38, 0x00, 0x1e, 0x30, 0x3e, 0x33, 0x7e, 0x00, 0x1c, 0x00, 0x0e, 0x0c, 0x0c, 0x0c, 0x1e, 0x00, 0x00, 0x38, 0x00, 0x1e, + 0x33, 0x33, 0x1e, 0x00, 0x00, 0x38, 0x00, 0x33, 0x33, 0x33, 0x7e, 0x00, 0x00, 0x1f, 0x00, 0x1f, 0x33, 0x33, 0x33, 0x00, + 0x3f, + 0x00, 0x33, 0x37, 0x3f, 0x3b, 0x33, 0x00, 0x3c, 0x36, 0x36, 0x7c, 0x00, 0x7e, 0x00, 0x00, 0x1c, 0x36, 0x36, 0x1c, 0x00, + 0x3e, + 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x06, 0x03, 0x33, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x3f, 0x30, 0x30, 0x00, 0x00, 0xc3, 0x63, 0x33, 0x7b, 0xcc, 0x66, 0x33, 0xf0, 0xc3, 0x63, 0x33, 0xdb, 0xec, 0xf6, + 0xf3, + 0xc0, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0xcc, 0x66, 0x33, 0x66, 0xcc, 0x00, 0x00, 0x00, 0x33, 0x66, + 0xcc, + 0x66, 0x33, 0x00, 0x00, 0x44, 0x11, 0x44, 0x11, 0x44, 0x11, 0x44, 0x11, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, + 0xdb, + 0xee, 0xdb, 0x77, 0xdb, 0xee, 0xdb, 0x77, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1f, + 0x18, + 0x18, 0x18, 0x18, 0x18, 0x1f, 0x18, 0x1f, 0x18, 0x18, 0x18, 0x6c, 0x6c, 0x6c, 0x6c, 0x6f, 0x6c, 0x6c, 0x6c, 0x00, 0x00, + 0x00, + 0x00, 0x7f, 0x6c, 0x6c, 0x6c, 0x00, 0x00, 0x1f, 0x18, 0x1f, 0x18, 0x18, 0x18, 0x6c, 0x6c, 0x6f, 0x60, 0x6f, 0x6c, 0x6c, + 0x6c, + 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0x00, 0x00, 0x7f, 0x60, 0x6f, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0x6f, 0x60, + 0x7f, + 0x00, 0x00, 0x00, 0x6c, 0x6c, 0x6c, 0x6c, 0x7f, 0x00, 0x00, 0x00, 0x18, 0x18, 0x1f, 0x18, 0x1f, 0x00, 0x00, 0x00, 0x00, + 0x00, + 0x00, 0x00, 0x1f, 0x18, 0x18, 0x18, + 0x18, 0x18, 0x18, 0x18, 0xf8, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xff, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xf8, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, + 0x18, + 0x18, 0x18, 0x18, 0xff, 0x18, 0x18, 0x18, 0x18, 0x18, 0xf8, 0x18, 0xf8, 0x18, 0x18, 0x18, 0x6c, 0x6c, 0x6c, 0x6c, 0xec, + 0x6c, + 0x6c, 0x6c, 0x6c, 0x6c, 0xec, 0x0c, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x0c, 0xec, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, + 0xef, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xef, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0xec, 0x0c, 0xec, 0x6c, + 0x6c, + 0x6c, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x6c, 0x6c, 0xef, 0x00, 0xef, 0x6c, 0x6c, 0x6c, 0x18, 0x18, 0xff, + 0x00, + 0xff, 0x00, 0x00, 0x00, 0x6c, 0x6c, 0x6c, 0x6c, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x18, 0x18, 0x18, + 0x00, + 0x00, 0x00, 0x00, 0xff, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, 0xfc, 0x00, 0x00, 0x00, 0x18, 0x18, 0xf8, 0x18, 0xf8, + 0x00, + 0x00, 0x00, 0x00, 0x00, 0xf8, 0x18, 0xf8, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x6c, 0x6c, 0x6c, 0x6c, 0x6c, + 0x6c, + 0x6c, 0xff, 0x6c, 0x6c, 0x6c, 0x18, 0x18, 0xff, 0x18, 0xff, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1f, 0x00, 0x00, + 0x00, + 0x00, 0x00, 0x00, 0x00, 0xf8, 0x18, 0x18, 0x18, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, + 0xff, + 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, + 0xff, + 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x6e, 0x3b, 0x13, 0x3b, 0x6e, 0x00, 0x00, 0x1e, 0x33, 0x1f, 0x33, 0x1f, 0x03, 0x03, 0x00, 0x3f, 0x33, 0x03, + 0x03, 0x03, 0x03, 0x00, 0x00, 0x7f, 0x36, 0x36, 0x36, 0x36, 0x36, 0x00, 0x3f, 0x33, 0x06, 0x0c, 0x06, 0x33, 0x3f, 0x00, + 0x00, + 0x00, 0x7e, 0x1b, 0x1b, 0x1b, 0x0e, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x3e, 0x06, 0x03, 0x00, 0x6e, 0x3b, 0x18, 0x18, + 0x18, + 0x18, 0x00, 0x3f, 0x0c, 0x1e, 0x33, 0x33, 0x1e, 0x0c, 0x3f, 0x1c, 0x36, 0x63, 0x7f, 0x63, 0x36, 0x1c, 0x00, 0x1c, 0x36, + 0x63, 0x63, 0x36, 0x36, 0x77, 0x00, 0x38, 0x0c, 0x18, 0x3e, 0x33, 0x33, 0x1e, 0x00, 0x00, 0x00, 0x7e, 0xdb, 0xdb, 0x7e, + 0x00, + 0x00, 0x60, 0x30, 0x7e, 0xdb, 0xdb, 0x7e, 0x06, 0x03, 0x1c, 0x06, 0x03, 0x1f, 0x03, 0x06, 0x1c, 0x00, 0x1e, 0x33, 0x33, + 0x33, + 0x33, 0x33, 0x33, 0x00, 0x00, 0x3f, 0x00, 0x3f, 0x00, 0x3f, 0x00, 0x00, 0x0c, 0x0c, 0x3f, 0x0c, 0x0c, 0x00, 0x3f, 0x00, + 0x06, + 0x0c, 0x18, 0x0c, 0x06, 0x00, 0x3f, 0x00, 0x18, 0x0c, 0x06, 0x0c, 0x18, 0x00, 0x3f, 0x00, 0x70, 0xd8, 0xd8, 0x18, 0x18, + 0x18, + 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1b, 0x1b, 0x0e, 0x0c, 0x0c, 0x00, 0x3f, 0x00, 0x0c, 0x0c, 0x00, 0x00, 0x6e, + 0x3b, + 0x00, 0x6e, 0x3b, 0x00, 0x00, 0x1c, 0x36, 0x36, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, + 0x00, + 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0xf0, 0x30, 0x30, 0x30, 0x37, 0x36, 0x3c, 0x38, 0x1e, 0x36, 0x36, 0x36, + 0x36, + 0x00, 0x00, 0x00, 0x0e, 0x18, 0x0c, 0x06, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x3c, 0x3c, 0x3c, 0x00, 0x00, 0x00, + 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +static void calcColors(const int colorNum, int & lo, int & hi, int & out) +{ + int redLo, redHi, greenLo, greenHi, blueLo, blueHi; + + if (colorNum == 0 || colorNum == 1 || colorNum == 2 || colorNum == 6) // white, red, yellow, or magenta + redLo = (0xf) << systemRedShift, redHi = (0x1f) << systemRedShift; + else + redLo = redHi = 0; + + if (colorNum == 0 || colorNum == 2 || colorNum == 3 || colorNum == 4) // white, yellow, green, or cyan + greenLo = (0xf) << systemGreenShift, greenHi = (0x1f) << systemGreenShift; + else + greenLo = greenHi = 0; + + if (colorNum == 0 || colorNum == 4 || colorNum == 5 || colorNum == 6) // white, cyan, blue, or magenta + blueLo = (0xf) << systemBlueShift, blueHi = (0x1f) << systemBlueShift; + else + blueLo = blueHi = 0; + + lo = redLo + greenLo + blueLo; + hi = redHi + greenHi + blueHi; + + if (colorNum == 7) // black + out = 0xffffffff; // white border + else + out = 0; // black border +} + +int lastColID = 0; +static void progressColorList(const char *& colorList, int & lo, int & hi, int & out) +{ + if (*colorList) + { + if (*colorList != lastColID) + { + calcColors((int)(*colorList)-1, lo, hi, out); + lastColID = *colorList; + } + colorList++; + } + else + { + colorList = NULL; + } +} + +static void drawTextInternal(u8 *screen, int pitch, int x, int y, + const char *string, bool trans, const char *colorList = NULL) +{ + if (colorList && !*colorList) + colorList = NULL; + + int loCol, hiCol, outCol; + calcColors(textColor, loCol, hiCol, outCol); + + lastColID = 0; + + const static int xd [8] = {-1, 0, 1, 1, 1, 0, -1, -1}; + const static int yd [8] = {-1, -1, -1, 0, 1, 1, 1, 0}; + + screen += y*pitch; + int inc = 2; + switch (systemColorDepth) + { + case 24: + inc = 3; + break; + case 32: + inc = 4; + break; + } + screen += x*inc; + + int xpos = x; + switch (systemColorDepth) + { + case 16: + { + while (*string) + { + char c = *string++; + u8 * scr = screen; + + if (colorList) + progressColorList(colorList, loCol, hiCol, outCol); + + u16 mask = u16(~RGB_LOW_BITS_MASK); + u16 *s = (u16 *)scr; + for (int h = 0-1; h < 8+1; h++) + { + for (int w = 0-1; w < 8+1; w++, s++) + { + int on = (h < 0 || w < 0 || h >= 8 || w >= 8) ? 0 : (fontdata2[(c<<3)+h]>>w)&1; + + int border = 0; + if (outlinedText) + for (int i = 0; i < 8; i++) + { + int h2 = h+yd[i], w2 = w+xd[i]; + border = (h2 < 0 || w2 < 0 || h2 >= 8 || w2 >= 8) ? 0 : (fontdata2[(c<<3)+h2]>>w2)&1; + if (border) + break; + } + + if (trans) + { + if (on) + *s = loCol + + ((*s & mask) >>1); + else if (border) + { + *s = outCol + + ((*s & mask) >>1); + } + } + else + { + if (on) + *s = hiCol; + else if (border) + *s = outCol; + } + } + scr += pitch; + s = (u16 *)scr; + } + screen += inc*8; + + xpos += 8; + if (xpos+8 > pitch>>1) // cut off text at right boundary + break; + } + break; + } + case 24: // TODO: verify this code's correctness + { + while (*string) + { + char c = *string++; + u8 * scr = screen; + + if (colorList) + progressColorList(colorList, loCol, hiCol, outCol); + + int h, w; + u8 *s = (u8 *)scr; + for (h = 0-1; h < 8+1; h++) + { + for (w = 0-1; w < 8+1; w++, s++) + { + int on = (h < 0 || w < 0 || h >= 8 || w >= 8) ? 0 : (fontdata2[(c<<3)+h]>>w)&1; + + int border = 0; + if (outlinedText) + for (int i = 0; i < 8; i++) + { + int h2 = h+yd[i], w2 = w+xd[i]; + border = (h2 < 0 || w2 < 0 || h2 >= 8 || w2 >= 8) ? 0 : (fontdata2[(c<<3)+h2]>>w2)&1; + if (border) + break; + } + + if (trans) + { + if (on) + { + u32 color = hiCol; + *s = ((color & 255)>>1)+(*s>>1); + *(s+1) = (((color >> 8) & 255)>>1)+(*(s+1)>>1); + *(s+2) = (((color >> 16) & 255)>>1)+(*(s+2)>>1); + } + else if (border) + { + u32 color = outCol; + *s = ((color & 255)>>1)+(*s>>1); + *(s+1) = (((color >> 8) & 255)>>1)+(*(s+1)>>1); + *(s+2) = (((color >> 16) & 255)>>1)+(*(s+2)>>1); + } + } + else + { + if (on) + { + u32 color = hiCol; + *s = (color & 255); + *(s+1) = (color >> 8) & 255; + *(s+2) = (color >> 16) & 255; + } + else if (border) + { + u32 color = outCol; + *s = (color & 255); + *(s+1) = (color >> 8) & 255; + *(s+2) = (color >> 16) & 255; + } + } + } + scr += pitch; + s = (u8 *)scr; + } + screen += inc*8; + + xpos += 8; + if (xpos+8 > pitch/3) // cut off text at right boundary + break; + } + break; + } + case 32: + { + while (*string) + { + char c = *string++; + u8 * scr = screen; + + if (colorList) + progressColorList(colorList, loCol, hiCol, outCol); + + int h, w; + u32 mask = 0xfefefe; + u32 *s = (u32 *)scr; + for (h = 0-1; h < 8+1; h++) + { + for (w = 0-1; w < 8+1; w++, s++) + { + int on = (h < 0 || w < 0 || h >= 8 || w >= 8) ? 0 : (fontdata2[(c<<3)+h]>>w)&1; + + int border = 0; + if (outlinedText) + for (int i = 0; i < 8; i++) + { + int h2 = h+yd[i], w2 = w+xd[i]; + border = (h2 < 0 || w2 < 0 || h2 >= 8 || w2 >= 8) ? 0 : (fontdata2[(c<<3)+h2]>>w2)&1; + if (border) + break; + } + + if (trans) + { + if (on) + *s = loCol + + ((*s & mask)>>1); + else if (border) + { + *s = outCol + + ((*s & mask)>>1); + } + } + else + { + if (on) + *s = hiCol; + else if (border) + *s = outCol; + } + } + scr += pitch; + s = (u32 *)scr; + } + screen += inc*8; + + xpos += 8; + if (xpos+8 > pitch>>2) // cut off text at right boundary + break; + } + break; + } + } +} + +void drawText(u8 *screen, int pitch, int x, int y, const char *string, const char *colorList) +{ + drawTextInternal(screen, pitch, x, y, string, transparentText, colorList); +} + +void drawTextTransp(u8 *screen, int pitch, int x, int y, const char *string, const char *colorList) +{ + drawTextInternal(screen, pitch, x, y, string, true, colorList); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/Text.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/Text.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +#ifndef VBA_TEXT_H +#define VBA_TEXT_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +extern void drawText(u8 *, int, int, int, const char *, const char*cl = NULL); +extern void drawTextTransp(u8 *, int, int, int, const char *, const char*cl = NULL); + +extern bool outlinedText, transparentText; +extern int textColor, textMethod; + +#endif // VBA_TEXT_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/Util.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/Util.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1400 @@ +#include +#include +#include +#include + +extern "C" { +#include +} + +#if 0 +#include "unrarlib.h" +#endif + +#include "unzip.h" + +#include "../NLS.h" +#include "System.h" +#include "Util.h" +#include "../gba/Flash.h" +#include "../gba/RTC.h" + +extern "C" { +#include "memgzio.h" +} + +#ifndef _MSC_VER +#define _stricmp strcasecmp +#endif // ! _MSC_VER + +extern int32 cpuSaveType; + +extern int systemColorDepth; +extern int systemRedShift; +extern int systemGreenShift; +extern int systemBlueShift; + +extern u16 systemColorMap16[0x10000]; +extern u32 systemColorMap32[0x10000]; + +static int (ZEXPORT *utilGzWriteFunc)(gzFile, voidp, unsigned int) = NULL; +static int (ZEXPORT *utilGzReadFunc)(gzFile, voidp, unsigned int) = NULL; +static int (ZEXPORT *utilGzCloseFunc)(gzFile) = NULL; +static z_off_t (ZEXPORT *utilGzSeekFunc)(gzFile, z_off_t, int) = NULL; +static z_off_t (ZEXPORT *utilGzTellFunc)(gzFile) = NULL; + +//Kludge to get it to compile in Linux, GCC cannot convert +//gzwrite function pointer to the type of utilGzWriteFunc +//due to void* and const void* differences +//--Felipe +int gzWrite(gzFile file, void* buf, unsigned len){ + return gzwrite(file,buf,len); +} + +void utilPutDword(u8 *p, u32 value) +{ + *p++ = value & 255; + *p++ = (value >> 8) & 255; + *p++ = (value >> 16) & 255; + *p = (value >> 24) & 255; +} + +void utilPutWord(u8 *p, u16 value) +{ + *p++ = value & 255; + *p = (value >> 8) & 255; +} + +void utilWriteBMP(u8 *b, int w, int h, int dstDepth, u8 *pix) +{ + int sizeX = w; + int sizeY = h; + + switch (dstDepth > 0 ? dstDepth : systemColorDepth) + { + case 16: + { + u16 *p = (u16 *)(pix + (w + 2) * (h) * 2); // skip first black line + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u16 v = *p++; + + *b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + } + p++; // skip black pixel for filters + p++; // skip black pixel for filters + p -= 2 * (w + 2); + } + break; + } + case 24: + { + u8 *pixU8 = (u8 *)pix + 3 * w * (h - 1); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + if (systemRedShift > systemBlueShift) + { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + else + { + int red = *pixU8++; + int green = *pixU8++; + int blue = *pixU8++; + + *b++ = blue; + *b++ = green; + *b++ = red; + } + } + pixU8 -= 2 * 3 * w; + } + break; + } + case 32: + { + u32 *pixU32 = (u32 *)(pix + 4 * (w + 1) * (h)); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u32 v = *pixU32++; + + *b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + } + pixU32++; + pixU32 -= 2 * (w + 1); + } + break; + } + } +} + +bool utilWriteBMPFile(const char *fileName, int w, int h, u8 *pix) +{ + u8 writeBuffer[256 * 3]; + + FILE *fp = fopen(fileName, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName); + return false; + } + + struct + { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + // u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w * h * 3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x36); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3 * w * h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + +#if 0 + // FIXME: need sufficient buffer + utilWriteBMP(writeBuffer, w, h, systemColorDepth, pix); +#else + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + switch (systemColorDepth) + { + case 16: + { + u16 *p = (u16 *)(pix + (w + 2) * (h) * 2); // skip first black line + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u16 v = *p++; + + *b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + } + p++; // skip black pixel for filters + p++; // skip black pixel for filters + p -= 2 * (w + 2); + fwrite(writeBuffer, 1, 3 * w, fp); + + b = writeBuffer; + } + break; + } + case 24: + { + u8 *pixU8 = (u8 *)pix + 3 * w * (h - 1); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + if (systemRedShift > systemBlueShift) + { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + else + { + int red = *pixU8++; + int green = *pixU8++; + int blue = *pixU8++; + + *b++ = blue; + *b++ = green; + *b++ = red; + } + } + pixU8 -= 2 * 3 * w; + fwrite(writeBuffer, 1, 3 * w, fp); + + b = writeBuffer; + } + break; + } + case 32: + { + u32 *pixU32 = (u32 *)(pix + 4 * (w + 1) * (h)); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u32 v = *pixU32++; + + *b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + } + pixU32++; + pixU32 -= 2 * (w + 1); + + fwrite(writeBuffer, 1, 3 * w, fp); + + b = writeBuffer; + } + break; + } + } +#endif + + fclose(fp); + + return true; +} + +bool utilWritePNGFile(const char *fileName, int w, int h, u8 *pix) +{ + u8 writeBuffer[256 * 3]; + + FILE *fp = fopen(fileName, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName); + return false; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if (!png_ptr) + { + fclose(fp); + return false; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return false; + } + + if (setjmp(png_ptr->jmpbuf)) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return false; + } + + png_init_io(png_ptr, fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + switch (systemColorDepth) + { + case 16: + { + u16 *p = (u16 *)(pix + (w + 2) * 2); // skip first black line + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u16 v = *p++; + + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B + } + p++; // skip black pixel for filters + p++; // skip black pixel for filters + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + break; + } + case 24: + { + u8 *pixU8 = (u8 *)pix; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + if (systemRedShift < systemBlueShift) + { + *b++ = *pixU8++; // R + *b++ = *pixU8++; // G + *b++ = *pixU8++; // B + } + else + { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + } + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + break; + } + case 32: + { + u32 *pixU32 = (u32 *)(pix + 4 * (w + 1)); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u32 v = *pixU32++; + + *b++ = ((v >> systemRedShift) & 0x001f) << 3; // R + *b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G + *b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B + } + pixU32++; + + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + break; + } + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); + + return true; +} + +static int utilReadInt2(FILE *f) +{ + int res = 0; + int c = fgetc(f); + if (c == EOF) + return -1; + res = c; + c = fgetc(f); + if (c == EOF) + return -1; + return c + (res << 8); +} + +static int utilReadInt3(FILE *f) +{ + int res = 0; + int c = fgetc(f); + if (c == EOF) + return -1; + res = c; + c = fgetc(f); + if (c == EOF) + return -1; + res = c + (res << 8); + c = fgetc(f); + if (c == EOF) + return -1; + return c + (res << 8); +} + +void utilApplyIPS(const char *ips, u8 * *r, int *s) +{ + // from the IPS spec at http://zerosoft.zophar.net/ips.htm + FILE *f = fopen(ips, "rb"); + if (!f) + return; + u8 *rom = *r; + int size = *s; + if (fgetc(f) == 'P' && + fgetc(f) == 'A' && + fgetc(f) == 'T' && + fgetc(f) == 'C' && + fgetc(f) == 'H') + { + int b; + int offset; + int len; + for (;; ) + { + // read offset + offset = utilReadInt3(f); + // if offset == EOF, end of patch + if (offset == 0x454f46) + break; + // read length + len = utilReadInt2(f); + if (!len) + { + // len == 0, RLE block + len = utilReadInt2(f); + // byte to fill + int c = fgetc(f); + if (c == -1) + break; + b = (u8)c; + } + else + b = -1; + // check if we need to reallocate our ROM + if ((offset + len) >= size) + { + size *= 2; + rom = (u8 *)realloc(rom, size); + *r = rom; + *s = size; + } + if (b == -1) + { + // normal block, just read the data + if (fread(&rom[offset], 1, len, f) != (size_t)len) + break; + } + else + { + // fill the region with the given byte + while (len--) + { + rom[offset++] = b; + } + } + } + } + // close the file + fclose(f); +} + +extern bool8 cpuIsMultiBoot; + +bool utilIsGBAImage(const char *file) +{ + cpuIsMultiBoot = false; + if (strlen(file) > 4) + { + const char *p = strrchr(file, '.'); + + if (p != NULL) + { + if (_stricmp(p, ".gba") == 0) + return true; + if (_stricmp(p, ".agb") == 0) + return true; + if (_stricmp(p, ".bin") == 0) + return true; + if (_stricmp(p, ".elf") == 0) + return true; + if (_stricmp(p, ".mb") == 0) + { + cpuIsMultiBoot = true; + return true; + } + } + } + + return false; +} + +bool utilIsGBImage(const char *file) +{ + if (strlen(file) > 4) + { + const char *p = strrchr(file, '.'); + + if (p != NULL) + { + if (_stricmp(p, ".gb") == 0) + return true; + if (_stricmp(p, ".gbc") == 0) + return true; + if (_stricmp(p, ".cgb") == 0) + return true; + if (_stricmp(p, ".sgb") == 0) + return true; + } + } + + return false; +} + +bool utilIsGBABios(const char *file) +{ + if (strlen(file) > 4) + { + const char *p = strrchr(file, '.'); + + if (p != NULL) + { + if (_stricmp(p, ".gba") == 0) + return true; + if (_stricmp(p, ".agb") == 0) + return true; + if (_stricmp(p, ".bin") == 0) + return true; + if (_stricmp(p, ".bios") == 0) + return true; + if (_stricmp(p, ".rom") == 0) + return true; + } + } + + return false; +} + +bool utilIsGBBios(const char *file) +{ + if (strlen(file) > 4) + { + const char *p = strrchr(file, '.'); + + if (p != NULL) + { + if (_stricmp(p, ".gb") == 0) + return true; + if (_stricmp(p, ".bin") == 0) + return true; + if (_stricmp(p, ".bios") == 0) + return true; + if (_stricmp(p, ".rom") == 0) + return true; + } + } + + return false; +} + +bool utilIsELF(const char *file) +{ + if (strlen(file) > 4) + { + const char *p = strrchr(file, '.'); + + if (p != NULL) + { + if (_stricmp(p, ".elf") == 0) + return true; + } + } + return false; +} + +bool utilIsZipFile(const char *file) +{ + if (strlen(file) > 4) + { + const char *p = strrchr(file, '.'); + + if (p != NULL) + { + if (_stricmp(p, ".zip") == 0) + return true; + } + } + + return false; +} + +#if 0 +bool utilIsRarFile(const char *file) +{ + if (strlen(file) > 4) + { + char *p = strrchr(file, '.'); + + if (p != NULL) + { + if (_stricmp(p, ".rar") == 0) + return true; + } + } + + return false; +} + +#endif + +bool utilIsGzipFile(const char *file) +{ + if (strlen(file) > 3) + { + const char *p = strrchr(file, '.'); + + if (p != NULL) + { + if (_stricmp(p, ".gz") == 0) + return true; + if (_stricmp(p, ".z") == 0) + return true; + } + } + + return false; +} + +void utilGetBaseName(const char *file, char *buffer) +{ + strcpy(buffer, file); + + if (utilIsGzipFile(file)) + { + char *p = strrchr(buffer, '.'); + + if (p) + *p = 0; + } +} + +IMAGE_TYPE utilFindType(const char *file) +{ + char buffer[2048]; + + if (utilIsZipFile(file)) + { + unzFile unz = unzOpen(file); + + if (unz == NULL) + { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file); + return IMAGE_UNKNOWN; + } + + int r = unzGoToFirstFile(unz); + + if (r != UNZ_OK) + { + unzClose(unz); + systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file); + return IMAGE_UNKNOWN; + } + + IMAGE_TYPE found = IMAGE_UNKNOWN; + + unz_file_info info; + + while (true) + { + r = unzGetCurrentFileInfo(unz, + &info, + buffer, + sizeof(buffer), + NULL, + 0, + NULL, + 0); + + if (r != UNZ_OK) + { + unzClose(unz); + systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file); + return IMAGE_UNKNOWN; + } + + if (utilIsGBAImage(buffer)) + { + found = IMAGE_GBA; + break; + } + + if (utilIsGBImage(buffer)) + { + found = IMAGE_GB; + break; + } + + r = unzGoToNextFile(unz); + + if (r != UNZ_OK) + break; + } + unzClose(unz); + + if (found == IMAGE_UNKNOWN) + { + systemMessage(MSG_NO_IMAGE_ON_ZIP, + N_("No image found on ZIP file %s"), file); + return found; + } + return found; +#if 0 + } + else if (utilIsRarFile(file)) + { + IMAGE_TYPE found = IMAGE_UNKNOWN; + + ArchiveList_struct *rarList = NULL; + if (urarlib_list((void *)file, (ArchiveList_struct *)&rarList)) + { + ArchiveList_struct *p = rarList; + + while (p) + { + if (utilIsGBAImage(p->item.Name)) + { + found = IMAGE_GBA; + break; + } + + if (utilIsGBImage(p->item.Name)) + { + found = IMAGE_GB; + break; + } + p = p->next; + } + + urarlib_freelist(rarList); + } + return found; +#endif + } + else + { + if (utilIsGzipFile(file)) + utilGetBaseName(file, buffer); + else + strcpy(buffer, file); + + if (utilIsGBAImage(buffer)) + return IMAGE_GBA; + if (utilIsGBImage(buffer)) + return IMAGE_GB; + } + return IMAGE_UNKNOWN; +} + +static int utilGetSize(int size) +{ + int res = 1; + while (res < size) + res <<= 1; + return res; +} + +static u8 *utilLoadFromZip(const char *file, + bool (*accept)(const char *), + u8 *data, + int &size) +{ + char buffer[2048]; + + unzFile unz = unzOpen(file); + + if (unz == NULL) + { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), file); + return NULL; + } + int r = unzGoToFirstFile(unz); + + if (r != UNZ_OK) + { + unzClose(unz); + systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file); + return NULL; + } + + bool found = false; + + unz_file_info info; + + while (true) + { + r = unzGetCurrentFileInfo(unz, + &info, + buffer, + sizeof(buffer), + NULL, + 0, + NULL, + 0); + + if (r != UNZ_OK) + { + unzClose(unz); + systemMessage(MSG_BAD_ZIP_FILE, N_("Bad ZIP file %s"), file); + return NULL; + } + + if (accept(buffer)) + { + found = true; + break; + } + + r = unzGoToNextFile(unz); + + if (r != UNZ_OK) + break; + } + + if (!found) + { + unzClose(unz); + systemMessage(MSG_NO_IMAGE_ON_ZIP, + N_("No image found on ZIP file %s"), file); + return NULL; + } + + int fileSize = info.uncompressed_size; + if (size == 0) + size = fileSize; + r = unzOpenCurrentFile(unz); + + if (r != UNZ_OK) + { + unzClose(unz); + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), buffer); + return NULL; + } + + u8 *image = data; + + if (image == NULL) + { + image = (u8 *)malloc(utilGetSize(size)); + if (image == NULL) + { + unzCloseCurrentFile(unz); + unzClose(unz); + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "data"); + return NULL; + } + size = fileSize; + } + int read = fileSize <= size ? fileSize : size; + r = unzReadCurrentFile(unz, + image, + read); + + unzCloseCurrentFile(unz); + unzClose(unz); + + if (r != (int)read) + { + systemMessage(MSG_ERROR_READING_IMAGE, + N_("Error reading image %s"), buffer); + if (data == NULL) + free(image); + return NULL; + } + + size = fileSize; + + return image; +} + +static u8 *utilLoadGzipFile(const char *file, + bool (*accept)(const char *), + u8 *data, + int &size) +{ + FILE *f = fopen(file, "rb"); + + if (f == NULL) + { + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file); + return NULL; + } + + fseek(f, -4, SEEK_END); + int fileSize = fgetc(f) | (fgetc(f) << 8) | (fgetc(f) << 16) | (fgetc(f) << 24); + fclose(f); + if (size == 0) + size = fileSize; + + gzFile gz = gzopen(file, "rb"); + + if (gz == NULL) + { + // should not happen, but who knows? + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file); + return NULL; + } + + u8 *image = data; + + if (image == NULL) + { + image = (u8 *)malloc(utilGetSize(size)); + if (image == NULL) + { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "data"); + fclose(f); + return NULL; + } + size = fileSize; + } + int read = fileSize <= size ? fileSize : size; + int r = gzread(gz, image, read); + gzclose(gz); + + if (r != (int)read) + { + systemMessage(MSG_ERROR_READING_IMAGE, + N_("Error reading image %s"), file); + if (data == NULL) + free(image); + return NULL; + } + + size = fileSize; + + return image; +} + +#if 0 +static u8 *utilLoadRarFile(const char *file, + bool (*accept)(const char *), + u8 *data, + int &size) +{ + char buffer[2048]; + + ArchiveList_struct *rarList = NULL; + if (urarlib_list((void *)file, (ArchiveList_struct *)&rarList)) + { + ArchiveList_struct *p = rarList; + + bool found = false; + while (p) + { + if (accept(p->item.Name)) + { + strcpy(buffer, p->item.Name); + found = true; + break; + } + p = p->next; + } + if (found) + { + void *memory = NULL; + unsigned long lsize = 0; + size = p->item.UnpSize; + int r = urarlib_get((void *)&memory, &lsize, buffer, (void *)file, ""); + if (!r) + { + systemMessage(MSG_ERROR_READING_IMAGE, + N_("Error reading image %s"), buffer); + urarlib_freelist(rarList); + return NULL; + } + u8 *image = (u8 *)memory; + if (data != NULL) + { + memcpy(image, data, size); + } + urarlib_freelist(rarList); + return image; + } + systemMessage(MSG_NO_IMAGE_ON_ZIP, + N_("No image found on RAR file %s"), file); + urarlib_freelist(rarList); + return NULL; + } + // nothing found + return NULL; +} + +#endif + +// the caller is responsible for caling free(return value) to release the memory +u8 *utilLoad(const char *file, + bool (*accept)(const char *), + u8 *data, + int &size) +{ + if (utilIsZipFile(file)) + { + return utilLoadFromZip(file, accept, data, size); + } + if (utilIsGzipFile(file)) + { + return utilLoadGzipFile(file, accept, data, size); + } +#if 0 + if (utilIsRarFile(file)) + { + return utilLoadRarFile(file, accept, data, size); + } +#endif + + u8 *image = data; + + FILE *f = fopen(file, "rb"); + + if (!f) + { + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), file); + return NULL; + } + + fseek(f, 0, SEEK_END); + int fileSize = ftell(f); + fseek(f, 0, SEEK_SET); + if (size == 0) + size = fileSize; + + if (image == NULL) + { + image = (u8 *)malloc(utilGetSize(size)); + if (image == NULL) + { + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), + "data"); + fclose(f); + return NULL; + } + size = fileSize; + } + int read = fileSize <= size ? fileSize : size; + int r = fread(image, 1, read, f); + fclose(f); + + if (r != (int)read) + { + systemMessage(MSG_ERROR_READING_IMAGE, + N_("Error reading image %s"), file); + if (data == NULL) + free(image); + return NULL; + } + + size = fileSize; + + return image; +} + +void utilWriteInt(gzFile gzFile, int32 i) +{ + utilGzWrite(gzFile, &i, sizeof(int32)); +} + +int32 utilReadInt(gzFile gzFile) +{ + int32 i = 0; + utilGzRead(gzFile, &i, sizeof(int32)); + return i; +} + +void utilReadData(gzFile gzFile, variable_desc *data) +{ + while (data->address) + { + utilGzRead(gzFile, data->address, data->size); + data++; + } +} + +void utilWriteData(gzFile gzFile, variable_desc *data) +{ + while (data->address) + { + utilGzWrite(gzFile, data->address, data->size); + data++; + } +} + +gzFile utilGzOpen(const char *file, const char *mode) +{ + utilGzWriteFunc = gzWrite; + utilGzReadFunc = gzread; + utilGzCloseFunc = gzclose; + utilGzSeekFunc = gzseek; + utilGzTellFunc = gztell; + + return gzopen(file, mode); +} + +gzFile utilGzReopen(int id, const char *mode) +{ + utilGzWriteFunc = gzWrite; + utilGzReadFunc = gzread; + utilGzCloseFunc = gzclose; + utilGzSeekFunc = gzseek; + utilGzTellFunc = gztell; + + return gzdopen(id, mode); +} + +gzFile utilMemGzOpen(char *memory, int available, char *mode) +{ + utilGzWriteFunc = memgzwrite; + utilGzReadFunc = memgzread; + utilGzCloseFunc = memgzclose; + utilGzSeekFunc = NULL; // FIXME: not implemented... + utilGzTellFunc = memtell; + + return memgzopen(memory, available, mode); +} + +int utilGzWrite(gzFile file, voidp buffer, unsigned int len) +{ + return utilGzWriteFunc(file, buffer, len); +} + +int utilGzRead(gzFile file, voidp buffer, unsigned int len) +{ + return utilGzReadFunc(file, buffer, len); +} + +int utilGzClose(gzFile file) +{ + return utilGzCloseFunc(file); +} + +z_off_t utilGzSeek(gzFile file, z_off_t offset, int whence) +{ + return utilGzSeekFunc(file, offset, whence); +} + +z_off_t utilGzTell(gzFile file) +{ + return utilGzTellFunc(file); +} + +void utilGBAFindSave(const u8 *data, const int size) +{ + u32 *p = (u32 *)data; + u32 *end = (u32 *)(data + size); + int saveType = 0; + int flashSize = 0x10000; + bool rtcFound = false; + + while (p < end) + { + u32 d = READ32LE(p); + + if (d == 0x52504545) + { + if (memcmp(p, "EEPROM_", 7) == 0) + { + if (saveType == 0) + saveType = 1; + } + } + else if (d == 0x4D415253) + { + if (memcmp(p, "SRAM_", 5) == 0) + { + if (saveType == 0) + saveType = 2; + } + } + else if (d == 0x53414C46) + { + if (memcmp(p, "FLASH1M_", 8) == 0) + { + if (saveType == 0) + { + saveType = 3; + flashSize = 0x20000; + } + } + else if (memcmp(p, "FLASH", 5) == 0) + { + if (saveType == 0) + { + saveType = 3; + flashSize = 0x10000; + } + } + } + else if (d == 0x52494953) + { + if (memcmp(p, "SIIRTC_V", 8) == 0) + rtcFound = true; + } + p++; + } + // if no matches found, then set it to NONE + if (saveType == 0) + { + saveType = 5; + } + rtcEnable(rtcFound); + cpuSaveType = saveType; + flashSetSize(flashSize); +} + +void utilUpdateSystemColorMaps() +{ + switch (systemColorDepth) + { + case 16: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + case 24: + case 32: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + } +} + +//// BIOS stuff +// systemType uses the same enum values as gbEmulatorType does + +bool utilLoadBIOS(u8 *bios, const char *biosFileName, int systemType) +{ + if (bios == NULL || strlen(biosFileName) == 0) + return false; + + if (systemType == 4) + { + int biosSize = 0x4000; + if (utilLoad(biosFileName, utilIsGBABios, bios, biosSize)) + { + if (biosSize == 0x4000) + return true; + } + } + else + { + int biosSize = 0x100; + if (utilLoad(biosFileName, utilIsGBBios, bios, biosSize)) + { + if (biosSize == 0x100) + return true; + } + } + + return false; +} + +bool utilCheckBIOS(const char *biosFileName, int systemType) +{ + if (strlen(biosFileName) == 0) + return false; + + u8 * tempBIOS = (u8 *)malloc(systemType == 4 ? 0x4000 : 0x100); + bool result = utilLoadBIOS(tempBIOS, biosFileName, systemType); + free(tempBIOS); + + return result; +} + +#if 0 +// returns the checksum of the BIOS that will be loaded after the next restart +u16 utilCalcBIOSChecksum(const u8 *bios, int systemType) +{ + u32 biosChecksum = 0; + if (bios) + { + int biosSize = (systemType == 4 ? 0x4000 : 0x100); + const u16 *data = reinterpret_cast(bios); + for (int i = biosSize; i > 0; i -= 2) + biosChecksum += *data++; + } + + while ((biosChecksum >> 16) & 0xFFFF) + biosChecksum = (biosChecksum &0xFFFF) + ((biosChecksum >> 16) & 0xFFFF); + + return biosChecksum & 0xFFFF; +} +#else +// returns the checksum of the BIOS that will be loaded after the next restart +u16 utilCalcBIOSChecksum(const u8 *bios, int systemType) +{ + u32 biosChecksum = 0; + if (bios) + { + int biosSize = (systemType == 4 ? 0x4000 : 0x100); + const u32 *data = reinterpret_cast(bios); + for (int i = biosSize; i > 0; i -= 4) + biosChecksum += *data++; + } + + return biosChecksum & 0xFFFF; +} +#endif + +// returns the checksum of the BIOS file +u16 utilCalcBIOSFileChecksum(const char *biosFileName, int systemType) +{ + if (strlen(biosFileName) == 0) + return 0; + + u16 biosChecksum = 0; + const int biosSize = (systemType == 4 ? 0x4000 : 0x100); + u8 * tempBIOS = (u8 *)malloc(biosSize); + bool hasBIOS = utilLoadBIOS(tempBIOS, biosFileName, systemType); + if (hasBIOS) + { + biosChecksum = utilCalcBIOSChecksum(tempBIOS, systemType); + } + free(tempBIOS); + + return biosChecksum; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/Util.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/Util.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,64 @@ +#ifndef VBA_UTIL_H +#define VBA_UTIL_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +enum IMAGE_TYPE +{ + IMAGE_UNKNOWN = -1, + IMAGE_GBA = 0, + IMAGE_GB = 1 +}; + +// save game + +typedef struct +{ + void *address; + int size; +} variable_desc; + +extern void utilWriteBMP(u8 *out, int w, int h, int dstDepth, u8 *in); +extern bool utilWriteBMPFile(const char *, int, int, u8 *); +extern bool utilWritePNGFile(const char *, int, int, u8 *); +extern void utilApplyIPS(const char *ips, u8 * *rom, int *size); +extern bool utilIsGBAImage(const char *); +extern bool utilIsGBABios(const char *file); +extern bool utilIsELF(const char *file); +extern bool utilIsGBImage(const char *); +extern bool utilIsGBBios(const char *file); +extern bool utilIsZipFile(const char *); +extern bool utilIsGzipFile(const char *); +extern bool utilIsRarFile(const char *); +extern void utilGetBaseName(const char *, char *); +extern IMAGE_TYPE utilFindType(const char *); +extern u8 * utilLoad(const char *, bool (*)(const char *), u8 *, int &); +extern void utilPutDword(u8 *, u32); +extern void utilPutWord(u8 *, u16); +extern void utilWriteData(gzFile, variable_desc *); +extern void utilReadData(gzFile, variable_desc *); +extern int32 utilReadInt(gzFile); +extern void utilWriteInt(gzFile, int32); +extern gzFile utilGzOpen(const char *file, const char *mode); +extern gzFile utilGzReopen(int id, const char *mode); +extern gzFile utilMemGzOpen(char *memory, int available, char *mode); +extern int utilGzWrite(gzFile file, voidp buffer, unsigned int len); +extern int utilGzRead(gzFile file, voidp buffer, unsigned int len); +extern int utilGzClose(gzFile file); +extern z_off_t utilGzSeek(gzFile file, z_off_t offset, int whence); +extern z_off_t utilGzTell(gzFile file); +extern void utilGBAFindSave(const u8 *, const int); +extern void utilUpdateSystemColorMaps(); +extern bool utilLoadBIOS(u8 *bios, const char *biosFileName, int systemType); +extern bool utilCheckBIOS(const char *biosFileName, int systemType); +extern u16 utilCalcBIOSChecksum(const u8 *bios, int systemType); +extern u16 utilCalcBIOSFileChecksum(const char *biosFileName, int systemType); + +extern int gzWrite(gzFile file, void* buf, unsigned len); + +#endif // VBA_UTIL_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/inputGlobal.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/inputGlobal.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,48 @@ +#ifndef VBA_INPUT_GLOBAL_H +#define VBA_INPUT_GLOBAL_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +enum +{ + KEY_BUTTON_A, KEY_BUTTON_B, + KEY_BUTTON_SELECT, KEY_BUTTON_START, + KEY_RIGHT, KEY_LEFT, + KEY_UP, KEY_DOWN, + KEY_BUTTON_R, KEY_BUTTON_L, + KEY_BUTTON_SPEED, KEY_BUTTON_CAPTURE, + KEY_BUTTON_GS +}; + +#define BUTTON_MASK_A (0x0001) +#define BUTTON_MASK_B (0x0002) +#define BUTTON_MASK_SELECT (0x0004) +#define BUTTON_MASK_START (0x0008) +#define BUTTON_MASK_RIGHT (0x0010) +#define BUTTON_MASK_LEFT (0x0020) +#define BUTTON_MASK_UP (0x0040) +#define BUTTON_MASK_DOWN (0x0080) +#define BUTTON_MASK_R (0x0100) +#define BUTTON_MASK_L (0x0200) +#define BUTTON_GB_MASK (BUTTON_MASK_A|BUTTON_MASK_B|BUTTON_MASK_SELECT|BUTTON_MASK_START| \ + BUTTON_MASK_RIGHT|BUTTON_MASK_LEFT|BUTTON_MASK_UP|BUTTON_MASK_DOWN) +#define BUTTON_GBA_ONLY (BUTTON_MASK_R|BUTTON_MASK_L) +#define BUTTON_REGULAR_MASK (BUTTON_GB_MASK|BUTTON_GBA_ONLY) +#define BUTTON_MASK_OLD_RESET (0x0400) +#define BUTTON_MASK_NEW_RESET (0x0800) +#define BUTTON_MASK_LEFT_MOTION (0x1000) +#define BUTTON_MASK_RIGHT_MOTION (0x2000) +#define BUTTON_MASK_DOWN_MOTION (0x4000) +#define BUTTON_MASK_UP_MOTION (0x8000) +#define BUTTON_MOTION_MASK (BUTTON_MASK_LEFT_MOTION|BUTTON_MASK_RIGHT_MOTION|BUTTON_MASK_DOWN_MOTION| \ + BUTTON_MASK_UP_MOTION) +#define BUTTON_RECORDINGONLY_MASK (BUTTON_MASK_OLD_RESET|BUTTON_MASK_NEW_RESET|BUTTON_MOTION_MASK) +#define BUTTON_REGULAR_RECORDING_MASK (BUTTON_REGULAR_MASK|BUTTON_RECORDINGONLY_MASK) +#define BUTTON_MASK_SPEED (0x040000) +#define BUTTON_MASK_CAPTURE (0x080000) +#define BUTTON_MASK_GAMESHARK (0x100000) +#define BUTTON_NONRECORDINGONLY_MASK (BUTTON_MASK_SPEED|BUTTON_MASK_CAPTURE|BUTTON_MASK_GAMESHARK) + +#endif // VBA_INPUT_GLOBAL_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/lua-engine.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/lua-engine.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,5102 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; + +#ifdef __linux + #include // for unlink + #include + #include +#endif +#if (defined(WIN32) && !defined(SDL)) + #include + #include "../win32/stdafx.h" + #include "../win32/Input.h" + #include "../win32/MainWnd.h" + #include "../win32/VBA.h" + #include "../win32/LuaOpenDialog.h" +#else + #define stricmp strcasecmp + #define strnicmp strncasecmp +#endif + +#include "../Port.h" +#include "System.h" +#include "movie.h" +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +#include "../gba/GBASound.h" + +#ifdef _WIN32 +#include "../win32/Sound.h" +//#include "../win32/WinMiscUtil.h" +extern CString winGetSavestateFilename(const CString &LogicalRomName, int nID); +#else +#endif + +extern "C" +{ +#include "../lua/src/lua.h" +#include "../lua/src/lauxlib.h" +#include "../lua/src/lualib.h" +#include "../lua/src/lstate.h" +} +#include "vbalua.h" + +#include "../SFMT/SFMT.c" + +static void (*info_print)(int uid, const char *str); +static void (*info_onstart)(int uid); +static void (*info_onstop)(int uid); +static int info_uid; + +#ifndef countof + #define countof(a) (sizeof(a) / sizeof(a[0])) +#endif + +static lua_State *LUA; + +// Are we running any code right now? +static char *luaScriptName = NULL; + +// Are we running any code right now? +static bool8 luaRunning = false; + +// True at the frame boundary, false otherwise. +static bool8 frameBoundary = false; + +// The execution speed we're running at. +static enum { SPEED_NORMAL, SPEED_NOTHROTTLE, SPEED_TURBO, SPEED_MAXIMUM } speedmode = SPEED_NORMAL; + +// Rerecord count skip mode +static bool8 skipRerecords = false; + +// Used by the registry to find our functions +static const char *frameAdvanceThread = "VBA.FrameAdvance"; +static const char *guiCallbackTable = "VBA.GUI"; + +// True if there's a thread waiting to run after a run of frame-advance. +static bool8 frameAdvanceWaiting = false; + +// We save our pause status in the case of a natural death. +//static bool8 wasPaused = false; + +// Transparency strength. 255=opaque, 0=so transparent it's invisible +static int transparencyModifier = 255; + +// Our joypads. +static uint32 lua_joypads[4]; +static uint8 lua_joypads_used = 0; + +static bool8 gui_used = false; +static uint8 *gui_data = NULL; // BGRA + +// Protects Lua calls from going nuts. +// We set this to a big number like 1000 and decrement it +// over time. The script gets knifed once this reaches zero. +static int numTries; + +// number of registered memory functions (1 per hooked byte) +static unsigned int numMemHooks; + +// Look in inputglobal.h for macros named like BUTTON_MASK_UP to determine the order. +static const char *button_mappings[] = { + "A", "B", "select", "start", "right", "left", "up", "down", "R", "L" +}; + +#ifdef _MSC_VER + #define snprintf _snprintf + #define vscprintf _vscprintf +#else + #define stricmp strcasecmp + #define strnicmp strncasecmp + #define __forceinline __attribute__((always_inline)) +#endif + +static const char *luaCallIDStrings[] = +{ + "CALL_BEFOREEMULATION", + "CALL_AFTEREMULATION", + "CALL_BEFOREEXIT" +}; + +//make sure we have the right number of strings +CTASSERT(sizeof(luaCallIDStrings) / sizeof(*luaCallIDStrings) == LUACALL_COUNT) + +static const char *luaMemHookTypeStrings [] = +{ + "MEMHOOK_WRITE", + "MEMHOOK_READ", + "MEMHOOK_EXEC", + + "MEMHOOK_WRITE_SUB", + "MEMHOOK_READ_SUB", + "MEMHOOK_EXEC_SUB", +}; + +//make sure we have the right number of strings +CTASSERT(sizeof(luaMemHookTypeStrings) / sizeof(*luaMemHookTypeStrings) == LUAMEMHOOK_COUNT) + +static char *rawToCString(lua_State * L, int idx = 0); +static const char *toCString(lua_State *L, int idx = 0); + +// GBA memory I/O functions copied from win32/MemoryViewerDlg.cpp +static inline u8 CPUReadByteQuick(u32 addr) +{ + return ::map[addr >> 24].address[addr & ::map[addr >> 24].mask]; +} + +static inline void CPUWriteByteQuick(u32 addr, u8 b) +{ + ::map[addr >> 24].address[addr & ::map[addr >> 24].mask] = b; +} + +static inline u16 CPUReadHalfWordQuick(u32 addr) +{ + return *((u16 *) &::map[addr >> 24].address[addr & ::map[addr >> 24].mask]); +} + +static inline void CPUWriteHalfWordQuick(u32 addr, u16 b) +{ + *((u16 *) &::map[addr >> 24].address[addr & ::map[addr >> 24].mask]) = b; +} + +static inline u32 CPUReadMemoryQuick(u32 addr) +{ + return *((u32 *) &::map[addr >> 24].address[addr & ::map[addr >> 24].mask]); +} + +static inline void CPUWriteMemoryQuick(u32 addr, u32 b) +{ + *((u32 *) &::map[addr >> 24].address[addr & ::map[addr >> 24].mask]) = b; +} + +// GB +static inline u8 gbReadMemoryQuick8(u16 addr) +{ + return gbReadMemoryQuick(addr); +} + +static inline void gbWriteMemoryQuick8(u16 addr, u8 b) +{ + gbWriteMemoryQuick(addr, b); +} + +static inline u16 gbReadMemoryQuick16(u16 addr) +{ + return (gbReadMemoryQuick(addr + 1) << 8) | gbReadMemoryQuick(addr); +} + +static inline void gbWriteMemoryQuick16(u16 addr, u16 b) +{ + gbWriteMemoryQuick(addr, b & 0xff); + gbWriteMemoryQuick(addr + 1, (b >> 8) & 0xff); +} + +static inline u32 gbReadMemoryQuick32(u16 addr) +{ + return (gbReadMemoryQuick(addr + 3) << 24) | + (gbReadMemoryQuick(addr + 2) << 16) | + (gbReadMemoryQuick(addr + 1) << 8) | + gbReadMemoryQuick(addr); +} + +static inline void gbWriteMemoryQuick32(u16 addr, u32 b) +{ + gbWriteMemoryQuick(addr, b & 0xff); + gbWriteMemoryQuick(addr + 1, (b >> 8) & 0xff); + gbWriteMemoryQuick(addr + 2, (b >> 16) & 0xff); + gbWriteMemoryQuick(addr + 1, (b >> 24) & 0xff); +} + +static inline u8 gbReadROMQuick8(u32 addr) +{ + return gbReadROMQuick(addr & gbRomSizeMask); +} + +static inline u8 gbReadROMQuick16(u32 addr) +{ + return (gbReadROMQuick(addr+1 & gbRomSizeMask) << 8) | gbReadROMQuick(addr & gbRomSizeMask); +} + +static inline u8 gbReadROMQuick32(u32 addr) +{ + return (gbReadROMQuick(addr+3 & gbRomSizeMask) << 24) | + (gbReadROMQuick(addr+2 & gbRomSizeMask) << 16) | + (gbReadROMQuick(addr+1 & gbRomSizeMask) << 8) | + gbReadROMQuick(addr & gbRomSizeMask); +} + +typedef void (*GetColorFunc)(const uint8 *, uint8 *, uint8 *, uint8 *); +typedef void (*SetColorFunc)(uint8 *, uint8, uint8, uint8); + +static void getColor16(const uint8 *s, uint8 *r, uint8 *g, uint8 *b) +{ + u16 v = *(const uint16 *)s; + *r = ((v >> systemBlueShift) & 0x001f) << 3; + *g = ((v >> systemGreenShift) & 0x001f) << 3; + *b = ((v >> systemRedShift) & 0x001f) << 3; +} + +static void getColor24(const uint8 *s, uint8 *r, uint8 *g, uint8 *b) +{ + if (systemRedShift > systemBlueShift) + *b = s[0], *g = s[1], *r = s[2]; + else + *r = s[0], *g = s[1], *b = s[2]; +} + +static void getColor32(const uint8 *s, uint8 *r, uint8 *g, uint8 *b) +{ + u32 v = *(const uint32 *)s; + *b = ((v >> systemBlueShift) & 0x001f) << 3; + *g = ((v >> systemGreenShift) & 0x001f) << 3; + *r = ((v >> systemRedShift) & 0x001f) << 3; +} + +static void setColor16(uint8 *s, uint8 r, uint8 g, uint8 b) +{ + *(uint16 *)s = ((b >> 3) & 0x01f) << + systemBlueShift | + ((g >> 3) & 0x01f) << + systemGreenShift | + ((r >> 3) & 0x01f) << + systemRedShift; +} + +static void setColor24(uint8 *s, uint8 r, uint8 g, uint8 b) +{ + if (systemRedShift > systemBlueShift) + s[0] = b, s[1] = g, s[2] = r; + else + s[0] = r, s[1] = g, s[2] = b; +} + +static void setColor32(uint8 *s, uint8 r, uint8 g, uint8 b) +{ + *(uint32 *)s = ((b >> 3) & 0x01f) << + systemBlueShift | + ((g >> 3) & 0x01f) << + systemGreenShift | + ((r >> 3) & 0x01f) << + systemRedShift; +} + +static bool getColorIOFunc(int depth, GetColorFunc *getColor, SetColorFunc *setColor) +{ + switch (depth) + { + case 16: + if (getColor) + *getColor = getColor16; + if (setColor) + *setColor = setColor16; + return true; + case 24: + if (getColor) + *getColor = getColor24; + if (setColor) + *setColor = setColor24; + return true; + case 32: + if (getColor) + *getColor = getColor32; + if (setColor) + *setColor = setColor32; + return true; + default: + return false; + } +} + +/** + * Resets emulator speed / pause states after script exit. + */ +static void VBALuaOnStop(void) +{ + luaRunning = false; + lua_joypads_used = 0; + gui_used = false; + //if (wasPaused) + // systemSetPause(true); +} + +/** + * Asks Lua if it wants control of the emulator's speed. + * Returns 0 if no, 1 if yes. If yes, we also tamper with the + * IPPU's settings for speed ourselves, so the calling code + * need not do anything. + */ +int VBALuaSpeed(void) +{ + if (!LUA || !luaRunning) + return 0; + + //printf("%d\n", speedmode); + switch (speedmode) + { + /* + case SPEED_NORMAL: + return 0; + case SPEED_NOTHROTTLE: + IPPU.RenderThisFrame = true; + return 1; + + case SPEED_TURBO: + IPPU.SkippedFrames++; + if (IPPU.SkippedFrames >= 40) { + IPPU.SkippedFrames = 0; + IPPU.RenderThisFrame = true; + } + else + IPPU.RenderThisFrame = false; + return 1; + + // In mode 3, SkippedFrames is set to zero so that the frame + // skipping code doesn't try anything funny. + case SPEED_MAXIMUM: + IPPU.SkippedFrames=0; + IPPU.RenderThisFrame = false; + return 1; + */ + case 0: // FIXME: to get rid of the warning + default: + assert(false); + return 0; + } +} + +/////////////////////////// +// vba.speedmode(string mode) +// +// Takes control of the emulation speed +// of the system. Normal is normal speed (60fps, 50 for PAL), +// nothrottle disables speed control but renders every frame, +// turbo renders only a few frames in order to speed up emulation, + +// maximum renders no frames +static int vba_speedmode(lua_State *L) +{ + const char *mode = luaL_checkstring(L, 1); + + if (strcasecmp(mode, "normal") == 0) + { + speedmode = SPEED_NORMAL; + } + else if (strcasecmp(mode, "nothrottle") == 0) + { + speedmode = SPEED_NOTHROTTLE; + } + else if (strcasecmp(mode, "turbo") == 0) + { + speedmode = SPEED_TURBO; + } + else if (strcasecmp(mode, "maximum") == 0) + { + speedmode = SPEED_MAXIMUM; + } + else + luaL_error(L, "Invalid mode %s to vba.speedmode", mode); + + //printf("new speed mode: %d\n", speedmode); + return 0; +} + +// vba.frameadvnace() +// +// Executes a frame advance. Occurs by yielding the coroutine, then re-running + +// when we break out. +static int vba_frameadvance(lua_State *L) +{ + // We're going to sleep for a frame-advance. Take notes. + if (frameAdvanceWaiting) + return luaL_error(L, "can't call vba.frameadvance() from here"); + + frameAdvanceWaiting = true; + + // Don't do this! The user won't like us sending their emulator out of control! + // Settings.FrameAdvance = true; + // Now we can yield to the main + return lua_yield(L, 0); + + // It's actually rather disappointing... +} + +// vba.pause() +// +// Pauses the emulator, function "waits" until the user unpauses. +// This function MAY be called from a non-frame boundary, but the frame + +// finishes executing anwyays. In this case, the function returns immediately. +static int vba_pause(lua_State *L) +{ + systemSetPause(true); + speedmode = SPEED_NORMAL; + + // Return control if we're midway through a frame. We can't pause here. + if (frameAdvanceWaiting) + { + return 0; + } + + // If it's on a frame boundary, we also yield. + frameAdvanceWaiting = true; + return lua_yield(L, 0); +} + +static int vba_registerbefore(lua_State *L) +{ + if (!lua_isnil(L, 1)) + luaL_checktype(L, 1, LUA_TFUNCTION); + lua_settop(L, 1); + lua_getfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_BEFOREEMULATION]); + lua_insert(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_BEFOREEMULATION]); + + //StopScriptIfFinished(luaStateToUIDMap[L]); + return 1; +} + +static int vba_registerafter(lua_State *L) +{ + if (!lua_isnil(L, 1)) + luaL_checktype(L, 1, LUA_TFUNCTION); + lua_settop(L, 1); + lua_getfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_AFTEREMULATION]); + lua_insert(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_AFTEREMULATION]); + + //StopScriptIfFinished(luaStateToUIDMap[L]); + return 1; +} + +static int vba_registerexit(lua_State *L) +{ + if (!lua_isnil(L, 1)) + luaL_checktype(L, 1, LUA_TFUNCTION); + lua_settop(L, 1); + lua_getfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_BEFOREEXIT]); + lua_insert(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_BEFOREEXIT]); + + //StopScriptIfFinished(luaStateToUIDMap[L]); + return 1; +} + +static inline bool isalphaorunderscore(char c) +{ + return isalpha(c) || c == '_'; +} + +static std::vector s_tableAddressStack; // prevents infinite recursion of a table within a table (when cycle is + // found, print something like table:parent) +static std::vector s_metacallStack; // prevents infinite recursion if something's __tostring returns another table + // that contains that something (when cycle is found, print the inner result + // without using __tostring) + +#define APPENDPRINT { int _n = snprintf(ptr, remaining, +#define END ); if (_n >= 0) { ptr += _n; remaining -= _n; } else { remaining = 0; } } +static void toCStringConverter(lua_State *L, int i, char * &ptr, int &remaining) +{ + if (remaining <= 0) + return; + + const char *str = ptr; // for debugging + + // if there is a __tostring metamethod then call it + int usedMeta = luaL_callmeta(L, i, "__tostring"); + if (usedMeta) + { + std::vector::const_iterator foundCycleIter = std::find(s_metacallStack.begin(), s_metacallStack.end(), lua_topointer(L, i)); + if (foundCycleIter != s_metacallStack.end()) + { + lua_pop(L, 1); + usedMeta = false; + } + else + { + s_metacallStack.push_back(lua_topointer(L, i)); + i = lua_gettop(L); + } + } + + switch (lua_type(L, i)) + { + case LUA_TNONE: + break; + case LUA_TNIL: + APPENDPRINT "nil" END break; + case LUA_TBOOLEAN: + APPENDPRINT lua_toboolean(L, i) ? "true" : "false" END break; + case LUA_TSTRING : APPENDPRINT "%s", lua_tostring(L, i) END break; + case LUA_TNUMBER: + APPENDPRINT "%.12Lg", lua_tonumber(L, i) END break; + case LUA_TFUNCTION: + if ((L->base + i - 1)->value.gc->cl.c.isC) + { + //lua_CFunction func = lua_tocfunction(L, i); + //std::map::iterator iter = s_cFuncInfoMap.find(func); + //if(iter == s_cFuncInfoMap.end()) + goto defcase; + //APPENDPRINT "function(%s)", iter->second END + } + else + { + APPENDPRINT "function(" END + Proto * p = (L->base + i - 1)->value.gc->cl.l.p; + int numParams = p->numparams + (p->is_vararg ? 1 : 0); + for (int n = 0; n < p->numparams; n++) + { + APPENDPRINT "%s", getstr(p->locvars[n].varname) END + if (n != numParams - 1) + APPENDPRINT "," END + } + if (p->is_vararg) + APPENDPRINT "..." END + APPENDPRINT ")" END + } + break; +defcase: default: + APPENDPRINT "%s:%p", luaL_typename(L, i), lua_topointer(L, i) END break; + case LUA_TTABLE: + { + // first make sure there's enough stack space + if (!lua_checkstack(L, 4)) + { + // note that even if lua_checkstack never returns false, + // that doesn't mean we didn't need to call it, + // because calling it retrieves stack space past LUA_MINSTACK + goto defcase; + } + + std::vector::const_iterator foundCycleIter = + std::find(s_tableAddressStack.begin(), s_tableAddressStack.end(), lua_topointer(L, i)); + if (foundCycleIter != s_tableAddressStack.end()) + { + int parentNum = s_tableAddressStack.end() - foundCycleIter; + if (parentNum > 1) + APPENDPRINT "%s:parent^%d", luaL_typename(L, i), parentNum END + else + APPENDPRINT "%s:parent", luaL_typename(L, i) END + } + else + { + s_tableAddressStack.push_back(lua_topointer(L, i)); + struct Scope { ~Scope(){ s_tableAddressStack. pop_back(); } } scope; + + APPENDPRINT "{" END + + lua_pushnil(L); // first key + int keyIndex = lua_gettop(L); + int valueIndex = keyIndex + 1; + bool first = true; + bool skipKey = true; // true if we're still in the "array part" of the table + lua_Number arrayIndex = (lua_Number)0; + while (lua_next(L, i)) + { + if (first) + first = false; + else + APPENDPRINT ", " END + if (skipKey) + { + arrayIndex += (lua_Number)1; + bool keyIsNumber = (lua_type(L, keyIndex) == LUA_TNUMBER); + skipKey = keyIsNumber && (lua_tonumber(L, keyIndex) == arrayIndex); + } + if (!skipKey) + { + bool keyIsString = (lua_type(L, keyIndex) == LUA_TSTRING); + bool invalidLuaIdentifier = (!keyIsString || !isalphaorunderscore(*lua_tostring(L, keyIndex))); + if (invalidLuaIdentifier) + if (keyIsString) + APPENDPRINT "['" END + else + APPENDPRINT "[" END + + toCStringConverter(L, keyIndex, ptr, remaining); + // key + + if (invalidLuaIdentifier) + if (keyIsString) + APPENDPRINT "']=" END + else + APPENDPRINT "]=" END + else + APPENDPRINT "=" END + } + + bool valueIsString = (lua_type(L, valueIndex) == LUA_TSTRING); + if (valueIsString) + APPENDPRINT "'" END + + toCStringConverter(L, valueIndex, ptr, remaining); // value + + if (valueIsString) + APPENDPRINT "'" END + + lua_pop(L, 1); + + if (remaining <= 0) + { + lua_settop(L, keyIndex - 1); // stack might not be clean yet if we're breaking + // early + break; + } + } + APPENDPRINT "}" END + } + } + break; + } + + if (usedMeta) + { + s_metacallStack.pop_back(); + lua_pop(L, 1); + } + } + + static const int s_tempStrMaxLen = 64 * 1024; + static char s_tempStr [s_tempStrMaxLen]; + + static char *rawToCString(lua_State *L, int idx) + { + int a = idx > 0 ? idx : 1; + int n = idx > 0 ? idx : lua_gettop(L); + + char *ptr = s_tempStr; + *ptr = 0; + + int remaining = s_tempStrMaxLen; + for (int i = a; i <= n; i++) + { + toCStringConverter(L, i, ptr, remaining); + if (i != n) + APPENDPRINT " " END + } + + if (remaining < 3) + { + while (remaining < 6) + remaining++, ptr--; + APPENDPRINT "..." END + } + APPENDPRINT "\r\n" END + // the trailing newline is so print() can avoid having to do wasteful things to print its newline + // (string copying would be wasteful and calling info.print() twice can be extremely slow) + // at the cost of functions that don't want the newline needing to trim off the last two characters + // (which is a very fast operation and thus acceptable in this case) + + return s_tempStr; + } +#undef APPENDPRINT +#undef END + +// replacement for luaB_tostring() that is able to show the contents of tables (and formats numbers better, and show function +// prototypes) +// can be called directly from lua via tostring(), assuming tostring hasn't been reassigned + static int tostring(lua_State *L) + { + char *str = rawToCString(L); + str[strlen(str) - 2] = 0; // hack: trim off the \r\n (which is there to simplify the print function's + // task) + lua_pushstring(L, str); + return 1; + } + +// like rawToCString, but will check if the global Lua function tostring() +// has been replaced with a custom function, and call that instead if so + static const char *toCString(lua_State *L, int idx) + { + int a = idx > 0 ? idx : 1; + int n = idx > 0 ? idx : lua_gettop(L); + lua_getglobal(L, "tostring"); + lua_CFunction cf = lua_tocfunction(L, -1); + if (cf == tostring || lua_isnil(L, -1)) // optimization: if using our own C tostring function, we can + // bypass the call through Lua and all the string object + // allocation that would entail + { + lua_pop(L, 1); + return rawToCString(L, idx); + } + else // if the user overrided the tostring function, we have to actually call it and store the + // temporarily allocated string it returns + { + lua_pushstring(L, ""); + for (int i = a; i <= n; i++) + { + lua_pushvalue(L, -2); // function to be called + lua_pushvalue(L, i); // value to print + lua_call(L, 1, 1); + if (lua_tostring(L, -1) == NULL) + luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print")); + lua_pushstring(L, (i < n) ? " " : "\r\n"); + lua_concat(L, 3); + } + const char *str = lua_tostring(L, -1); + strncpy(s_tempStr, str, s_tempStrMaxLen); + s_tempStr[s_tempStrMaxLen - 1] = 0; + lua_pop(L, 2); + return s_tempStr; + } + } + +// replacement for luaB_print() that goes to the appropriate textbox instead of stdout + static int print(lua_State *L) + { + const char *str = toCString(L); + + int uid = info_uid; //luaStateToUIDMap[L->l_G->mainthread]; + //LuaContextInfo& info = GetCurrentInfo(); + + if (info_print) + info_print(uid, str); + else + puts(str); + + //worry(L, 100); + return 0; + } + + static int printerror(lua_State *L, int idx) + { + lua_checkstack(L, lua_gettop(L) + 4); + + if (idx < 0) + idx = lua_gettop(L) + 1 + idx; + + const char *str = rawToCString(L, idx); + + int uid = info_uid; //luaStateToUIDMap[L->l_G->mainthread]; + //LuaContextInfo& info = GetCurrentInfo(); + + if (info_print) + info_print(uid, str); + else + fputs(str, stderr); + + //worry(L, 100); + return 0; + } + +// vba.message(string msg) +// +// Displays the given message on the screen. + static int vba_message(lua_State *L) + { + const char *msg = luaL_checkstring(L, 1); + systemScreenMessage(msg); + + return 0; + } + +// provides an easy way to copy a table from Lua +// (simple assignment only makes an alias, but sometimes an independent table is desired) +// currently this function only performs a shallow copy, +// but I think it should be changed to do a deep copy (possibly of configurable depth?) +// that maintains the internal table reference structure + static int copytable(lua_State *L) + { + int origIndex = 1; // we only care about the first argument + int origType = lua_type(L, origIndex); + if (origType == LUA_TNIL) + { + lua_pushnil(L); + return 1; + } + if (origType != LUA_TTABLE) + { + luaL_typerror(L, 1, lua_typename(L, LUA_TTABLE)); + lua_pushnil(L); + return 1; + } + + lua_createtable(L, lua_objlen(L, 1), 0); + int copyIndex = lua_gettop(L); + + lua_pushnil(L); // first key + int keyIndex = lua_gettop(L); + int valueIndex = keyIndex + 1; + + while (lua_next(L, origIndex)) + { + lua_pushvalue(L, keyIndex); + lua_pushvalue(L, valueIndex); + lua_rawset(L, copyIndex); // copytable[key] = value + lua_pop(L, 1); + } + + // copy the reference to the metatable as well, if any + if (lua_getmetatable(L, origIndex)) + lua_setmetatable(L, copyIndex); + + return 1; // return the new table + } + +// because print traditionally shows the address of tables, +// and the print function I provide instead shows the contents of tables, +// I also provide this function +// (otherwise there would be no way to see a table's address, AFAICT) + static int addressof(lua_State *L) + { + const void *ptr = lua_topointer(L, -1); + lua_pushinteger(L, (lua_Integer)ptr); + return 1; + } + + struct registerPointerMap + { + const char * registerName; + unsigned int *pointer; + int dataSize; + }; + +#define RPM_ENTRY(name, var) \ + { name, (unsigned int *)&var, sizeof(var) \ + } \ + , + + extern gbRegister AF; + extern gbRegister BC; + extern gbRegister DE; + extern gbRegister HL; + extern gbRegister SP; + extern gbRegister PC; + extern u16 IFF; + + registerPointerMap regPointerMap [] = { + // gba registers + RPM_ENTRY("r0", reg[0].I) + RPM_ENTRY("r1", reg[1].I) + RPM_ENTRY("r2", reg[2].I) + RPM_ENTRY("r3", reg[3].I) + RPM_ENTRY("r4", reg[4].I) + RPM_ENTRY("r5", reg[5].I) + RPM_ENTRY("r6", reg[6].I) + RPM_ENTRY("r7", reg[7].I) + RPM_ENTRY("r8", reg[8].I) + RPM_ENTRY("r9", reg[9].I) + RPM_ENTRY("r10", reg[10].I) + RPM_ENTRY("r11", reg[11].I) + RPM_ENTRY("r12", reg[12].I) + RPM_ENTRY("r13", reg[13].I) + RPM_ENTRY("r14", reg[14].I) + RPM_ENTRY("r15", reg[15].I) + RPM_ENTRY("cpsr", reg[16].I) + RPM_ENTRY("spsr", reg[17].I) + // gb registers + RPM_ENTRY("a", AF.B.B1) + RPM_ENTRY("f", AF.B.B0) + RPM_ENTRY("b", BC.B.B1) + RPM_ENTRY("c", BC.B.B0) + RPM_ENTRY("d", DE.B.B1) + RPM_ENTRY("e", DE.B.B0) + RPM_ENTRY("h", HL.B.B1) + RPM_ENTRY("l", HL.B.B0) + RPM_ENTRY("af", AF.W) + RPM_ENTRY("bc", BC.W) + RPM_ENTRY("de", DE.W) + RPM_ENTRY("hl", HL.W) + RPM_ENTRY("sp", SP.W) + RPM_ENTRY("pc", PC.W) + {} + }; + + struct cpuToRegisterMap + { + const char *cpuName; + registerPointerMap *rpmap; + } + cpuToRegisterMaps [] = + { + { "", regPointerMap }, + }; + +//DEFINE_LUA_FUNCTION(memory_getregister, "cpu_dot_registername_string") + static int memory_getregister(lua_State *L) + { + const char *qualifiedRegisterName = luaL_checkstring(L, 1); + lua_settop(L, 0); + for (int cpu = 0; cpu < sizeof(cpuToRegisterMaps) / sizeof(*cpuToRegisterMaps); cpu++) + { + cpuToRegisterMap ctrm = cpuToRegisterMaps[cpu]; + int cpuNameLen = strlen(ctrm.cpuName); + if (!strnicmp(qualifiedRegisterName, ctrm.cpuName, cpuNameLen)) + { + qualifiedRegisterName += cpuNameLen; + for (int reg = 0; ctrm.rpmap[reg].dataSize; reg++) + { + registerPointerMap rpm = ctrm.rpmap[reg]; + if (!stricmp(qualifiedRegisterName, rpm.registerName)) + { + switch (rpm.dataSize) + { + default: + case 1: + lua_pushinteger(L, *(unsigned char *)rpm.pointer); break; + case 2: + lua_pushinteger(L, *(unsigned short *)rpm.pointer); break; + case 4: + lua_pushinteger(L, *(unsigned long *)rpm.pointer); break; + } + return 1; + } + } + lua_pushnil(L); + return 1; + } + } + lua_pushnil(L); + return 1; + } + +//DEFINE_LUA_FUNCTION(memory_setregister, "cpu_dot_registername_string,value") + static int memory_setregister(lua_State *L) + { + const char * qualifiedRegisterName = luaL_checkstring(L, 1); + unsigned long value = (unsigned long)(luaL_checkinteger(L, 2)); + lua_settop(L, 0); + for (int cpu = 0; cpu < sizeof(cpuToRegisterMaps) / sizeof(*cpuToRegisterMaps); cpu++) + { + cpuToRegisterMap ctrm = cpuToRegisterMaps[cpu]; + int cpuNameLen = strlen(ctrm.cpuName); + if (!strnicmp(qualifiedRegisterName, ctrm.cpuName, cpuNameLen)) + { + qualifiedRegisterName += cpuNameLen; + for (int reg = 0; ctrm.rpmap[reg].dataSize; reg++) + { + registerPointerMap rpm = ctrm.rpmap[reg]; + if (!stricmp(qualifiedRegisterName, rpm.registerName)) + { + switch (rpm.dataSize) + { + default: + case 1: + *(unsigned char *)rpm.pointer = (unsigned char)(value & 0xFF); break; + case 2: + *(unsigned short *)rpm.pointer = (unsigned short)(value & 0xFFFF); break; + case 4: + *(unsigned long *)rpm.pointer = value; break; + } + return 0; + } + } + return 0; + } + } + return 0; + } + + void HandleCallbackError(lua_State *L) + { + if (L->errfunc || L->errorJmp) + luaL_error(L, "%s", lua_tostring(L, -1)); + else + { + lua_pushnil(LUA); + lua_setfield(LUA, LUA_REGISTRYINDEX, guiCallbackTable); + + // Error? +//#if (defined(WIN32) && !defined(SDL)) +// info_print(info_uid, lua_tostring(LUA, -1)); //Clear_Sound_Buffer(); +// AfxGetApp()->m_pMainWnd->MessageBox(lua_tostring(LUA, -1), "Lua run error", MB_OK | MB_ICONSTOP); +//#else +// fprintf(stderr, "Lua thread bombed out: %s\n", lua_tostring(LUA, -1)); +//#endif + printerror(LUA, -1); + VBALuaStop(); + } + } + + void CallRegisteredLuaFunctions(LuaCallID calltype) + { + assert((unsigned int)calltype < (unsigned int)LUACALL_COUNT); + + const char *idstring = luaCallIDStrings[calltype]; + + if (!LUA) + return; + + lua_settop(LUA, 0); + lua_getfield(LUA, LUA_REGISTRYINDEX, idstring); + + int errorcode = 0; + if (lua_isfunction(LUA, -1)) + { + errorcode = lua_pcall(LUA, 0, 0, 0); + if (errorcode) + HandleCallbackError(LUA); + } + else + { + lua_pop(LUA, 1); + } + } + +// the purpose of this structure is to provide a way of +// QUICKLY determining whether a memory address range has a hook associated with it, +// with a bias toward fast rejection because the majority of addresses will not be hooked. +// (it must not use any part of Lua or perform any per-script operations, +// otherwise it would definitely be too slow.) +// calculating the regions when a hook is added/removed may be slow, +// but this is an intentional tradeoff to obtain a high speed of checking during later execution + struct TieredRegion + { + template + struct Region + { + struct Island + { + unsigned int start; + unsigned int end; + __forceinline bool Contains(unsigned int address, int size) const { return address < end && address + size > start; } + }; + std::vector islands; + + void Calculate(const std::vector &bytes) + { + islands. clear(); + + unsigned int lastEnd = ~0; + + std::vector::const_iterator iter = bytes.begin(); + std::vector::const_iterator end = bytes.end(); + for (; iter != end; ++iter) + { + unsigned int addr = *iter; + if (addr < lastEnd || addr > lastEnd + (long long)maxGap) + { + islands. push_back(Island()); + islands. back().start = addr; + } + islands.back(). end = addr + 1; + lastEnd = addr + 1; + } + } + + bool Contains(unsigned int address, int size) const + { + for (size_t i = 0; i != islands.size(); ++i) + { + if (islands[i].Contains(address, size)) + return true; + } + return false; + } + }; + + Region<0xFFFFFFFF> broad; + Region<0x1000> mid; + Region<0> narrow; + + void Calculate(std::vector &bytes) + { + std:: sort(bytes.begin(), bytes.end()); + + broad. Calculate(bytes); + mid. Calculate(bytes); + narrow. Calculate(bytes); + } + + TieredRegion() + { + std::vector temp; + Calculate(temp); + } + + __forceinline int NotEmpty() + { + return broad.islands.size(); + } + + // note: it is illegal to call this if NotEmpty() returns 0 + __forceinline bool Contains(unsigned int address, int size) + { + return broad.islands[0].Contains(address, size) && + mid.Contains(address, size) && + narrow.Contains(address, size); + } + }; + TieredRegion hookedRegions [LUAMEMHOOK_COUNT]; + + static void CalculateMemHookRegions(LuaMemHookType hookType) + { + std::vector hookedBytes; +// std::map::iterator iter = luaContextInfo.begin(); +// std::map::iterator end = luaContextInfo.end(); +// while(iter != end) +// { +// LuaContextInfo& info = *iter->second; + if (/*info.*/ numMemHooks) + { + lua_State *L = LUA /*info.L*/; + if (L) + { + lua_settop(L, 0); + lua_getfield(L, LUA_REGISTRYINDEX, luaMemHookTypeStrings[hookType]); + lua_pushnil(L); + while (lua_next(L, -2)) + { + if (lua_isfunction(L, -1)) + { + unsigned int addr = lua_tointeger(L, -2); + hookedBytes.push_back(addr); + } + lua_pop(L, 1); + } + lua_settop(L, 0); + } + } +// ++iter; +// } + hookedRegions[hookType].Calculate(hookedBytes); + } + + static void CallRegisteredLuaMemHook_LuaMatch(unsigned int address, int size, unsigned int value, LuaMemHookType hookType) + { +// std::map::iterator iter = luaContextInfo.begin(); +// std::map::iterator end = luaContextInfo.end(); +// while(iter != end) +// { +// LuaContextInfo& info = *iter->second; + if (/*info.*/ numMemHooks) + { + lua_State *L = LUA /*info.L*/; + if (L /* && !info.panic*/) + { +#ifdef USE_INFO_STACK + infoStack.insert(infoStack.begin(), &info); + struct Scope { ~Scope(){ infoStack. erase(infoStack.begin()); } } scope; +#endif + lua_settop(L, 0); + lua_getfield(L, LUA_REGISTRYINDEX, luaMemHookTypeStrings[hookType]); + for (int i = address; i != address + size; i++) + { + lua_rawgeti(L, -1, i); + if (lua_isfunction(L, -1)) + { + bool wasRunning = (luaRunning != 0) /*info.running*/; + luaRunning /*info.running*/ = true; + //RefreshScriptSpeedStatus(); + lua_pushinteger(L, address); + lua_pushinteger(L, size); + int errorcode = lua_pcall(L, 2, 0, 0); + luaRunning /*info.running*/ = wasRunning; + //RefreshScriptSpeedStatus(); + if (errorcode) + { + HandleCallbackError(L); + //int uid = iter->first; + //HandleCallbackError(L,info,uid,true); + } + break; + } + else + { + lua_pop(L, 1); + } + } + lua_settop(L, 0); + } + } +// ++iter; +// } + } + + void CallRegisteredLuaMemHook(unsigned int address, int size, unsigned int value, LuaMemHookType hookType) + { + // performance critical! (called VERY frequently) + // I suggest timing a large number of calls to this function in Release if you change anything in here, + // before and after, because even the most innocent change can make it become 30% to 400% slower. + // a good amount to test is: 100000000 calls with no hook set, and another 100000000 with a hook set. + // (on my system that consistently took 200 ms total in the former case and 350 ms total in the latter + // case) + if (hookedRegions[hookType].NotEmpty()) + { + //if((hookType <= LUAMEMHOOK_EXEC) && (address >= 0xE00000)) + // address |= 0xFF0000; // account for mirroring of RAM + if (hookedRegions[hookType].Contains(address, size)) + CallRegisteredLuaMemHook_LuaMatch(address, size, value, hookType); // something has hooked this + // specific address + } + } + + static int memory_registerHook(lua_State *L, LuaMemHookType hookType, int defaultSize) + { + // get first argument: address + unsigned int addr = luaL_checkinteger(L, 1); + //if((addr & ~0xFFFFFF) == ~0xFFFFFF) + // addr &= 0xFFFFFF; + + // get optional second argument: size + int size = defaultSize; + int funcIdx = 2; + if (lua_isnumber(L, 2)) + { + size = luaL_checkinteger(L, 2); + if (size < 0) + { + size = -size; + addr -= size; + } + funcIdx++; + } + + // check last argument: callback function + bool clearing = lua_isnil(L, funcIdx); + if (!clearing) + luaL_checktype(L, funcIdx, LUA_TFUNCTION); + lua_settop(L, funcIdx); + + // get the address-to-callback table for this hook type of the current script + lua_getfield(L, LUA_REGISTRYINDEX, luaMemHookTypeStrings[hookType]); + + // count how many callback functions we'll be displacing + int numFuncsAfter = clearing ? 0 : size; + int numFuncsBefore = 0; + for (unsigned int i = addr; i != addr + size; i++) + { + lua_rawgeti(L, -1, i); + if (lua_isfunction(L, -1)) + numFuncsBefore++; + lua_pop(L, 1); + } + + // put the callback function in the address slots + for (unsigned int i = addr; i != addr + size; i++) + { + lua_pushvalue(L, -2); + lua_rawseti(L, -2, i); + } + + // adjust the count of active hooks + //LuaContextInfo& info = GetCurrentInfo(); + /*info.*/ numMemHooks += numFuncsAfter - numFuncsBefore; + + // re-cache regions of hooked memory across all scripts + CalculateMemHookRegions(hookType); + + //StopScriptIfFinished(luaStateToUIDMap[L]); + return 0; + } + + LuaMemHookType MatchHookTypeToCPU(lua_State *L, LuaMemHookType hookType) + { + int cpuID = 0; + + int cpunameIndex = 0; + if (lua_type(L, 2) == LUA_TSTRING) + cpunameIndex = 2; + else if (lua_type(L, 3) == LUA_TSTRING) + cpunameIndex = 3; + + if (cpunameIndex) + { + const char *cpuName = lua_tostring(L, cpunameIndex); + if (!stricmp(cpuName, "sub")) + cpuID = 1; + lua_remove(L, cpunameIndex); + } + + switch (cpuID) + { + case 0: + return hookType; + + case 1: + switch (hookType) + { + case LUAMEMHOOK_WRITE: + return LUAMEMHOOK_WRITE_SUB; + case LUAMEMHOOK_READ: + return LUAMEMHOOK_READ_SUB; + case LUAMEMHOOK_EXEC: + return LUAMEMHOOK_EXEC_SUB; + } + } + return hookType; + } + + static int memory_registerwrite(lua_State *L) + { + return memory_registerHook(L, MatchHookTypeToCPU(L, LUAMEMHOOK_WRITE), 1); + } + + static int memory_registerread(lua_State *L) + { + return memory_registerHook(L, MatchHookTypeToCPU(L, LUAMEMHOOK_READ), 1); + } + + static int memory_registerexec(lua_State *L) + { + return memory_registerHook(L, MatchHookTypeToCPU(L, LUAMEMHOOK_EXEC), 1); + } + +//int vba.lagcount +// + +//Returns the lagcounter variable + static int vba_getlagcount(lua_State *L) + { + lua_pushinteger(L, systemCounters.lagCount); + return 1; + } + +//int vba.lagged +// +//Returns true if the current frame is a lag frame + static int vba_lagged(lua_State *L) + { + lua_pushboolean(L, systemCounters.laggedLast); + return 1; + } + +// boolean vba.emulating() + int vba_emulating(lua_State *L) + { + lua_pushboolean(L, systemIsEmulating()); + return 1; + } + + int movie_isactive(lua_State *L) + { + lua_pushboolean(L, VBAMovieActive()); + return 1; + } + + int movie_isrecording(lua_State *L) + { + lua_pushboolean(L, VBAMovieRecording()); + return 1; + } + + int movie_isplaying(lua_State *L) + { + lua_pushboolean(L, VBAMoviePlaying()); + return 1; + } + + int movie_getlength(lua_State *L) + { + if (VBAMovieActive()) + lua_pushinteger(L, VBAMovieGetLength()); + else + lua_pushinteger(L, 0); + return 1; + } + + static int memory_readbyte(lua_State *L) + { + u32 addr; + u8 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + val = CPUReadByteQuick(addr); + } + else + { + val = gbReadMemoryQuick8(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_readbytesigned(lua_State *L) + { + u32 addr; + s8 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + val = (s8) CPUReadByteQuick(addr); + } + else + { + val = (s8) gbReadMemoryQuick8(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_readword(lua_State *L) + { + u32 addr; + u16 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + val = CPUReadHalfWordQuick(addr); + } + else + { + val = gbReadMemoryQuick16(addr & 0x0000FFFF); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_readwordsigned(lua_State *L) + { + u32 addr; + s16 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + val = (s16) CPUReadHalfWordQuick(addr); + } + else + { + val = (s16) gbReadMemoryQuick16(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_readdword(lua_State *L) + { + u32 addr; + u32 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + val = CPUReadMemoryQuick(addr); + } + else + { + val = gbReadMemoryQuick32(addr & 0x0000FFFF); + } + + // lua_pushinteger doesn't work properly for 32bit system, does it? + if (val >= 0x80000000 && sizeof(int) <= 4) + lua_pushnumber(L, val); + else + lua_pushinteger(L, val); + return 1; + } + + static int memory_readdwordsigned(lua_State *L) + { + u32 addr; + s32 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + val = (s32) CPUReadMemoryQuick(addr); + } + else + { + val = (s32) gbReadMemoryQuick32(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_readbyterange(lua_State *L) + { + uint32 address = luaL_checkinteger(L, 1); + int length = luaL_checkinteger(L, 2); + + if (length < 0) + { + address += length; + length = -length; + } + + // push the array + lua_createtable(L, abs(length), 0); + + // put all the values into the (1-based) array + for (int a = address, n = 1; n <= length; a++, n++) + { + unsigned char value; + + if (systemIsRunningGBA()) + { + value = CPUReadByteQuick(a); + } + else + { + value = gbReadMemoryQuick8(a); + } + + lua_pushinteger(L, value); + lua_rawseti(L, -2, n); + } + + return 1; + } + + static int memory_writebyte(lua_State *L) + { + u32 addr; + int val; + + addr = luaL_checkinteger(L, 1); + val = luaL_checkinteger(L, 2); + if (systemIsRunningGBA()) + { + CPUWriteByteQuick(addr, val); + } + else + { + gbWriteMemoryQuick8(addr, val); + } + + CallRegisteredLuaMemHook(addr, 1, val, LUAMEMHOOK_WRITE); + return 0; + } + + static int memory_writeword(lua_State *L) + { + u32 addr; + int val; + + addr = luaL_checkinteger(L, 1); + val = luaL_checkinteger(L, 2); + if (systemIsRunningGBA()) + { + CPUWriteHalfWordQuick(addr, val); + } + else + { + gbWriteMemoryQuick16(addr, val); + } + + CallRegisteredLuaMemHook(addr, 2, val, LUAMEMHOOK_WRITE); + return 0; + } + + static int memory_writedword(lua_State *L) + { + u32 addr; + int val; + + addr = luaL_checkinteger(L, 1); + val = luaL_checkinteger(L, 2); + if (systemIsRunningGBA()) + { + CPUWriteMemoryQuick(addr, val); + } + else + { + gbWriteMemoryQuick32(addr, val); + } + + CallRegisteredLuaMemHook(addr, 4, val, LUAMEMHOOK_WRITE); + return 0; + } + + static int memory_gbromreadbyte(lua_State *L) + { + u32 addr; + u8 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + lua_pushnil(L); + return 1; + } + else + { + val = gbReadROMQuick8(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_gbromreadbytesigned(lua_State *L) + { + u32 addr; + s8 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + lua_pushnil(L); + return 1; + } + else + { + val = (s8) gbReadROMQuick8(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_gbromreadword(lua_State *L) + { + u32 addr; + u16 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + lua_pushnil(L); + return 1; + } + else + { + val = gbReadROMQuick16(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_gbromreadwordsigned(lua_State *L) + { + u32 addr; + s16 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + lua_pushnil(L); + return 1; + } + else + { + val = (s16) gbReadROMQuick16(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_gbromreaddword(lua_State *L) + { + u32 addr; + u32 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + lua_pushnil(L); + return 1; + } + else + { + val = gbReadROMQuick32(addr); + } + + // lua_pushinteger doesn't work properly for 32bit system, does it? + if (val >= 0x80000000 && sizeof(int) <= 4) + lua_pushnumber(L, val); + else + lua_pushinteger(L, val); + return 1; + } + + static int memory_gbromreaddwordsigned(lua_State *L) + { + u32 addr; + s32 val; + + addr = luaL_checkinteger(L, 1); + if (systemIsRunningGBA()) + { + lua_pushnil(L); + return 1; + } + else + { + val = (s32) gbReadROMQuick32(addr); + } + + lua_pushinteger(L, val); + return 1; + } + + static int memory_gbromreadbyterange(lua_State *L) + { + uint32 address = luaL_checkinteger(L, 1); + int length = luaL_checkinteger(L, 2); + + if (length < 0) + { + address += length; + length = -length; + } + + // push the array + lua_createtable(L, abs(length), 0); + + // put all the values into the (1-based) array + for (int a = address, n = 1; n <= length; a++, n++) + { + unsigned char value; + + if (systemIsRunningGBA()) + { + lua_pushnil(L); + return 1; + } + else + { + value = gbReadROMQuick8(a); + } + + lua_pushinteger(L, value); + lua_rawseti(L, -2, n); + } + + return 1; + } + +// table joypad.get(int which = 1) +// +// Reads the joypads as inputted by the user. + static int joy_get_internal(lua_State *L, bool reportUp, bool reportDown) + { + // Reads the joypads as inputted by the user + int which = luaL_checkinteger(L, 1); + + if (which < 0 || which > 4) + { + luaL_error(L, "Invalid input port (valid range 0-4, specified %d)", which); + } + + uint32 buttons = systemGetOriginalJoypad(which - 1, false); + + lua_newtable(L); + + int i; + for (i = 0; i < 10; i++) + { + bool pressed = (buttons & (1 << i)) != 0; + if ((pressed && reportDown) || (!pressed && reportUp)) + { + lua_pushboolean(L, pressed); + lua_setfield(L, -2, button_mappings[i]); + } + } + + return 1; + } + +// joypad.get(which) +// returns a table of every game button, +// true meaning currently-held and false meaning not-currently-held +// (as of last frame boundary) +// this WILL read input from a currently-playing movie + static int joypad_get(lua_State *L) + { + return joy_get_internal(L, true, true); + } + +// joypad.getdown(which) +// returns a table of every game button that is currently held + static int joypad_getdown(lua_State *L) + { + return joy_get_internal(L, false, true); + } + +// joypad.getup(which) +// returns a table of every game button that is not currently held + static int joypad_getup(lua_State *L) + { + return joy_get_internal(L, true, false); + } + +// joypad.set(int which, table buttons) +// +// Sets the given buttons to be pressed during the next +// frame advance. The table should have the right + +// keys (no pun intended) set. + static int joypad_set(lua_State *L) + { + // Which joypad we're tampering with + int which = luaL_checkinteger(L, 1); + if (which < 0 || which > 4) + { + luaL_error(L, "Invalid output port (valid range 0-4, specified %d)", which); + } + + if (which == 0) + which = systemGetDefaultJoypad(); + + // And the table of buttons. + luaL_checktype(L, 2, LUA_TTABLE); + + // Set up for taking control of the indicated controller + lua_joypads_used |= 1 << (which - 1); + lua_joypads[which - 1] = 0; + + for (int i = 0; i < 10; i++) + { + const char *name = button_mappings[i]; + lua_getfield(L, 2, name); + if (!lua_isnil(L, -1)) + { + bool pressed = lua_toboolean(L, -1) != 0; + if (pressed) + lua_joypads[which - 1] |= 1 << i; + else + lua_joypads[which - 1] &= ~(1 << i); + } + lua_pop(L, 1); + } + + return 0; + } + +// Helper function to convert a savestate object to the filename it represents. + static const char *savestateobj2filename(lua_State *L, int offset) + { + // First we get the metatable of the indicated object + int result = lua_getmetatable(L, offset); + + if (!result) + luaL_error(L, "object not a savestate object"); + + // Also check that the type entry is set + lua_getfield(L, -1, "__metatable"); + if (strcmp(lua_tostring(L, -1), "vba Savestate") != 0) + luaL_error(L, "object not a savestate object"); + lua_pop(L, 1); + + // Now, get the field we want + lua_getfield(L, -1, "filename"); + + // Return it + return lua_tostring(L, -1); + } + +// Helper function for garbage collection. + static int savestate_gc(lua_State *L) + { + // The object we're collecting is on top of the stack + lua_getmetatable(L, 1); + + // Get the filename + const char *filename; + lua_getfield(L, -1, "filename"); + filename = lua_tostring(L, -1); + + // Delete the file + remove(filename); + + // We exit, and the garbage collector takes care of the rest. + // Edit: Visual Studio needs a return value anyway, so returns 0. + return 0; + } + +// object savestate.create(int which = nil) +// +// Creates an object used for savestates. +// The object can be associated with a player-accessible savestate + +// ("which" between 1 and 12) or not (which == nil). + static int savestate_create(lua_State *L) + { + int which = -1; + if (lua_gettop(L) >= 1) + { + which = luaL_checkinteger(L, 1); + if (which < 1 || which > 12) + { + luaL_error(L, "invalid player's savestate %d", which); + } + } + + char stateName[2048]; + + if (which > 0) + { + // Find an appropriate filename. This is OS specific, unfortunately. +#if (defined(WIN32) && !defined(SDL)) + CString stateName = winGetSavestateFilename(theApp.gameFilename, which); +#else + extern char saveDir[2048]; + extern char filename[2048]; + extern char *sdlGetFilename(char *name); + + if (saveDir[0]) + sprintf(stateName, "%s/%s%d.sgm", saveDir, sdlGetFilename(filename), which); + else + sprintf(stateName, "%s%d.sgm", filename, which); +#endif + } + else + { + char *stateNameTemp = tempnam(NULL, "snlua"); + strcpy(stateName, stateNameTemp); + if (stateNameTemp) + free(stateNameTemp); + } + + // Our "object". We don't care about the type, we just need the memory and GC services. + lua_newuserdata(L, 1); + + // The metatable we use, protected from Lua and contains garbage collection info and stuff. + lua_newtable(L); + + // First, we must protect it + lua_pushstring(L, "vba Savestate"); + lua_setfield(L, -2, "__metatable"); + + // Now we need to save the file itself. + lua_pushstring(L, stateName); + lua_setfield(L, -2, "filename"); + + // If it's an anonymous savestate, we must delete the file from disk should it be gargage collected + if (which < 0) + { + lua_pushcfunction(L, savestate_gc); + lua_setfield(L, -2, "__gc"); + } + + // Set the metatable + lua_setmetatable(L, -2); + + // Awesome. Return the object + return 1; + } + +// savestate.save(object state) +// + +// Saves a state to the given object. + static int savestate_save(lua_State *L) + { + const char *filename = savestateobj2filename(L, 1); + + // printf("saving %s\n", filename); + // Save states are very expensive. They take time. + numTries--; + + bool8 retvalue = theEmulator.emuWriteState ? theEmulator.emuWriteState(filename) : false; + if (!retvalue) + { + // Uh oh + luaL_error(L, "savestate failed"); + } + + return 0; + } + +// savestate.load(object state) +// + +// Loads the given state + static int savestate_load(lua_State *L) + { + const char *filename = savestateobj2filename(L, 1); + + numTries--; + + // printf("loading %s\n", filename); + bool8 retvalue = theEmulator.emuReadState ? theEmulator.emuReadState(filename) : false; + if (!retvalue) + { + // Uh oh + luaL_error(L, "loadstate failed"); + } + + return 0; + } + +// int vba.framecount() +// + +// Gets the frame counter for the movie, or the number of frames since last reset. + int vba_framecount(lua_State *L) + { + if (!VBAMovieActive()) + { + lua_pushinteger(L, systemCounters.frameCount); + } + else + { + lua_pushinteger(L, VBAMovieGetFrameCounter()); + } + + return 1; + } + +//string movie.getauthor +// + +// returns author info field of .vbm file + int movie_getauthor(lua_State *L) + { + if (!VBAMovieActive()) + { + //lua_pushnil(L); + lua_pushstring(L, ""); + return 1; + } + + lua_pushstring(L, VBAMovieGetAuthorInfo().c_str()); + return 1; + } + +//string movie.filename + int movie_getfilename(lua_State *L) + { + if (!VBAMovieActive()) + { + //lua_pushnil(L); + lua_pushstring(L, ""); + return 1; + } + + lua_pushstring(L, VBAMovieGetFilename().c_str()); + return 1; + } + +// string movie.mode() +// + +// "record", "playback" or nil + int movie_getmode(lua_State *L) + { + assert(!VBAMovieLoading()); + if (!VBAMovieActive()) + { + lua_pushnil(L); + return 1; + } + + if (VBAMovieRecording()) + lua_pushstring(L, "record"); + else + lua_pushstring(L, "playback"); + return 1; + } + + static int movie_rerecordcount(lua_State *L) + { + if (VBAMovieActive()) + lua_pushinteger(L, VBAMovieGetRerecordCount()); + else + lua_pushinteger(L, 0); + return 1; + } + + static int movie_setrerecordcount(lua_State *L) + { + if (VBAMovieActive()) + VBAMovieSetRerecordCount(luaL_checkinteger(L, 1)); + return 0; + } + + static int movie_rerecordcounting(lua_State *L) + { + if (lua_gettop(L) == 0) + luaL_error(L, "no parameters specified"); + + skipRerecords = lua_toboolean(L, 1); + return 0; + } + +// movie.stop() +// + +// Stops movie playback/recording. Bombs out if movie is not running. + static int movie_stop(lua_State *L) + { + if (!VBAMovieActive()) + luaL_error(L, "no movie"); + + VBAMovieStop(false); + return 0; + } + +#define LUA_SCREEN_WIDTH 256 +#define LUA_SCREEN_HEIGHT 239 + +// Common code by the gui library: make sure the screen array is ready + static void gui_prepare(void) + { + if (!gui_data) + gui_data = (uint8 *)malloc(LUA_SCREEN_WIDTH * LUA_SCREEN_HEIGHT * 4); + if (!gui_used) + memset(gui_data, 0, LUA_SCREEN_WIDTH * LUA_SCREEN_HEIGHT * 4); + gui_used = true; + } + +// pixform for lua graphics +#define BUILD_PIXEL_ARGB8888(A, R, G, B) (((int)(A) << 24) | ((int)(R) << 16) | ((int)(G) << 8) | (int)(B)) +#define DECOMPOSE_PIXEL_ARGB8888(PIX, A, R, G, B) \ + { \ + (A) = ((PIX) >> 24) & 0xff; \ + (R) = ((PIX) >> 16) & 0xff; \ + (G) = ((PIX) >> 8) & 0xff; \ + (B) = (PIX) & 0xff; \ + } +#define LUA_BUILD_PIXEL BUILD_PIXEL_ARGB8888 +#define LUA_DECOMPOSE_PIXEL DECOMPOSE_PIXEL_ARGB8888 +#define LUA_PIXEL_A(PIX) (((PIX) >> 24) & 0xff) +#define LUA_PIXEL_R(PIX) (((PIX) >> 16) & 0xff) +#define LUA_PIXEL_G(PIX) (((PIX) >> 8) & 0xff) +#define LUA_PIXEL_B(PIX) ((PIX) & 0xff) + + template + static void swap(T &one, T &two) + { + T temp = one; + one = two; + two = temp; + } + +// write a pixel to buffer + static inline void blend32(uint32 *dstPixel, uint32 colour) + { + uint8 *dst = (uint8 *)dstPixel; + int a, r, g, b; + LUA_DECOMPOSE_PIXEL(colour, a, r, g, b); + + if (a == 255 || dst[3] == 0) + { + // direct copy + *(uint32 *) (dst) = colour; + } + else if (a == 0) + { + // do not copy + } + else + { + // alpha-blending + int a_dst = ((255 - a) * dst[3] + 128) / 255; + int a_new = a + a_dst; + + dst[0] = (uint8) (((dst[0] * a_dst + b * a) + (a_new / 2)) / a_new); + dst[1] = (uint8) (((dst[1] * a_dst + g * a) + (a_new / 2)) / a_new); + dst[2] = (uint8) (((dst[2] * a_dst + r * a) + (a_new / 2)) / a_new); + dst[3] = (uint8) a_new; + } + } + +// check if a pixel is in the lua canvas + static inline bool gui_check_boundary(int x, int y) + { + return !(x < 0 || x >= LUA_SCREEN_WIDTH || y < 0 || y >= LUA_SCREEN_HEIGHT); + } + +// check if any part of a box is in the lua canvas + static inline bool gui_checkbox(int x1, int y1, int x2, int y2) + { + if ((x1 < 0 && x2 < 0) + || (x1 >= LUA_SCREEN_WIDTH && x2 >= LUA_SCREEN_WIDTH) + || (y1 < 0 && y2 < 0) + || (y1 >= LUA_SCREEN_HEIGHT && y2 >= LUA_SCREEN_HEIGHT)) + return false; + return true; + } + +// write a pixel to gui_data (do not check boundaries for speedup) + static inline void gui_drawpixel_fast(int x, int y, uint32 colour) + { + //gui_prepare(); + blend32((uint32 *) &gui_data[(y * LUA_SCREEN_WIDTH + x) * 4], colour); + } + +// write a pixel to gui_data (check boundaries) + static inline void gui_drawpixel_internal(int x, int y, uint32 colour) + { + //gui_prepare(); + if (gui_check_boundary(x, y)) + gui_drawpixel_fast(x, y, colour); + } + +// draw a line on gui_data (checks boundaries) + static void gui_drawline_internal(int x1, int y1, int x2, int y2, bool lastPixel, uint32 colour) + { + //gui_prepare(); + // Note: New version of Bresenham's Line Algorithm + // + // + // http://groups.google.co.jp/group/rec.games.roguelike.development/browse_thread/thread/345f4c42c3b25858/29e07a3af3a450e6?show_docid=29e07a3af3a450e6 + int swappedx = 0; + int swappedy = 0; + + int xtemp = x1 - x2; + int ytemp = y1 - y2; + if (xtemp == 0 && ytemp == 0) + { + gui_drawpixel_internal(x1, y1, colour); + return; + } + + if (xtemp < 0) + { + xtemp = -xtemp; + swappedx = 1; + } + + if (ytemp < 0) + { + ytemp = -ytemp; + swappedy = 1; + } + + int delta_x = xtemp << 1; + int delta_y = ytemp << 1; + + signed char ix = x1 > x2 ? 1 : -1; + signed char iy = y1 > y2 ? 1 : -1; + + if (lastPixel) + gui_drawpixel_internal(x2, y2, colour); + + if (delta_x >= delta_y) + { + int error = delta_y - (delta_x >> 1); + + while (x2 != x1) + { + if (error == 0 && !swappedx) + gui_drawpixel_internal(x2 + ix, y2, colour); + if (error >= 0) + { + if (error || (ix > 0)) + { + y2 += iy; + error -= delta_x; + } + } + + x2 += ix; + gui_drawpixel_internal(x2, y2, colour); + if (error == 0 && swappedx) + gui_drawpixel_internal(x2, y2 + iy, colour); + error += delta_y; + } + } + else + { + int error = delta_x - (delta_y >> 1); + + while (y2 != y1) + { + if (error == 0 && !swappedy) + gui_drawpixel_internal(x2, y2 + iy, colour); + if (error >= 0) + { + if (error || (iy > 0)) + { + x2 += ix; + error -= delta_y; + } + } + + y2 += iy; + gui_drawpixel_internal(x2, y2, colour); + if (error == 0 && swappedy) + gui_drawpixel_internal(x2 + ix, y2, colour); + error += delta_x; + } + } + } + +// draw a rect on gui_data + static void gui_drawbox_internal(int x1, int y1, int x2, int y2, uint32 colour) + { + if (x1 > x2) + std::swap(x1, x2); + if (y1 > y2) + std::swap(y1, y2); + if (x1 < 0) + x1 = -1; + if (y1 < 0) + y1 = -1; + if (x2 >= LUA_SCREEN_WIDTH) + x2 = LUA_SCREEN_WIDTH; + if (y2 >= LUA_SCREEN_HEIGHT) + y2 = LUA_SCREEN_HEIGHT; + + if (!gui_checkbox(x1, y1, x2, y2)) + return; + + //gui_prepare(); + gui_drawline_internal(x1, y1, x2, y1, true, colour); + gui_drawline_internal(x1, y2, x2, y2, true, colour); + gui_drawline_internal(x1, y1, x1, y2, true, colour); + gui_drawline_internal(x2, y1, x2, y2, true, colour); + } + +// draw a circle on gui_data + static void gui_drawcircle_internal(int x0, int y0, int radius, uint32 colour) + { + //gui_prepare(); + if (radius < 0) + radius = -radius; + if (radius == 0) + return; + if (radius == 1) + { + gui_drawpixel_internal(x0, y0, colour); + return; + } + + // http://en.wikipedia.org/wiki/Midpoint_circle_algorithm + int f = 1 - radius; + int ddF_x = 1; + int ddF_y = -2 * radius; + int x = 0; + int y = radius; + + if (!gui_checkbox(x0 - radius, y0 - radius, x0 + radius, y0 + radius)) + return; + + gui_drawpixel_internal(x0, y0 + radius, colour); + gui_drawpixel_internal(x0, y0 - radius, colour); + gui_drawpixel_internal(x0 + radius, y0, colour); + gui_drawpixel_internal(x0 - radius, y0, colour); + + // same pixel shouldn't be drawed twice, + // because each pixel has opacity. + // so now the routine gets ugly. + while (true) + { + assert(ddF_x == 2 * x + 1); + assert(ddF_y == -2 * y); + assert(f == x * x + y * y - radius * radius + 2 * x - y + 1); + if (f >= 0) + { + y--; + ddF_y += 2; + f += ddF_y; + } + + x++; + ddF_x += 2; + f += ddF_x; + if (x < y) + { + gui_drawpixel_internal(x0 + x, y0 + y, colour); + gui_drawpixel_internal(x0 - x, y0 + y, colour); + gui_drawpixel_internal(x0 + x, y0 - y, colour); + gui_drawpixel_internal(x0 - x, y0 - y, colour); + gui_drawpixel_internal(x0 + y, y0 + x, colour); + gui_drawpixel_internal(x0 - y, y0 + x, colour); + gui_drawpixel_internal(x0 + y, y0 - x, colour); + gui_drawpixel_internal(x0 - y, y0 - x, colour); + } + else if (x == y) + { + gui_drawpixel_internal(x0 + x, y0 + y, colour); + gui_drawpixel_internal(x0 - x, y0 + y, colour); + gui_drawpixel_internal(x0 + x, y0 - y, colour); + gui_drawpixel_internal(x0 - x, y0 - y, colour); + break; + } + else + break; + } + } + +// draw fill rect on gui_data + static void gui_fillbox_internal(int x1, int y1, int x2, int y2, uint32 colour) + { + if (x1 > x2) + std::swap(x1, x2); + if (y1 > y2) + std::swap(y1, y2); + if (x1 < 0) + x1 = 0; + if (y1 < 0) + y1 = 0; + if (x2 >= LUA_SCREEN_WIDTH) + x2 = LUA_SCREEN_WIDTH - 1; + if (y2 >= LUA_SCREEN_HEIGHT) + y2 = LUA_SCREEN_HEIGHT - 1; + + //gui_prepare(); + int ix, iy; + for (iy = y1; iy <= y2; iy++) + { + for (ix = x1; ix <= x2; ix++) + { + gui_drawpixel_fast(ix, iy, colour); + } + } + } + +// fill a circle on gui_data + static void gui_fillcircle_internal(int x0, int y0, int radius, uint32 colour) + { + //gui_prepare(); + if (radius < 0) + radius = -radius; + if (radius == 0) + return; + if (radius == 1) + { + gui_drawpixel_internal(x0, y0, colour); + return; + } + + // http://en.wikipedia.org/wiki/Midpoint_circle_algorithm + int f = 1 - radius; + int ddF_x = 1; + int ddF_y = -2 * radius; + int x = 0; + int y = radius; + + if (!gui_checkbox(x0 - radius, y0 - radius, x0 + radius, y0 + radius)) + return; + + gui_drawline_internal(x0, y0 - radius, x0, y0 + radius, true, colour); + + while (true) + { + assert(ddF_x == 2 * x + 1); + assert(ddF_y == -2 * y); + assert(f == x * x + y * y - radius * radius + 2 * x - y + 1); + if (f >= 0) + { + y--; + ddF_y += 2; + f += ddF_y; + } + + x++; + ddF_x += 2; + f += ddF_x; + + if (x < y) + { + gui_drawline_internal(x0 + x, y0 - y, x0 + x, y0 + y, true, colour); + gui_drawline_internal(x0 - x, y0 - y, x0 - x, y0 + y, true, colour); + if (f >= 0) + { + gui_drawline_internal(x0 + y, y0 - x, x0 + y, y0 + x, true, colour); + gui_drawline_internal(x0 - y, y0 - x, x0 - y, y0 + x, true, colour); + } + } + else if (x == y) + { + gui_drawline_internal(x0 + x, y0 - y, x0 + x, y0 + y, true, colour); + gui_drawline_internal(x0 - x, y0 - y, x0 - x, y0 + y, true, colour); + break; + } + else + break; + } + } + +// Helper for a simple hex parser + static int hex2int(lua_State *L, char c) + { + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return luaL_error(L, "invalid hex in colour"); + } + + static const struct ColorMapping + { + const char *name; + int value; + } + s_colorMapping[] = + { + { "white", 0xFFFFFFFF }, + { "black", 0x000000FF }, + { "clear", 0x00000000 }, + { "gray", 0x7F7F7FFF }, + { "grey", 0x7F7F7FFF }, + { "red", 0xFF0000FF }, + { "orange", 0xFF7F00FF }, + { "yellow", 0xFFFF00FF }, + { "chartreuse", 0x7FFF00FF }, + { "green", 0x00FF00FF }, + { "teal", 0x00FF7FFF }, + { "cyan", 0x00FFFFFF }, + { "blue", 0x0000FFFF }, + { "purple", 0x7F00FFFF }, + { "magenta", 0xFF00FFFF }, + }; + +/** + * Converts an integer or a string on the stack at the given + * offset to a RGB32 colour. Several encodings are supported. + * The user may construct their own RGB value, given a simple colour name, + * or an HTML-style "#09abcd" colour. 16 bit reduction doesn't occur at this time. + */ + static inline bool str2colour(uint32 *colour, lua_State *L, const char *str) + { + if (str[0] == '#') + { + int color; + sscanf(str + 1, "%X", &color); + + int len = strlen(str + 1); + int missing = max(0, 8 - len); + color <<= missing << 2; + if (missing >= 2) + color |= 0xFF; + *colour = color; + return true; + } + else + { + if (!strnicmp(str, "rand", 4)) + { + *colour = gen_rand32() | 0xFF; //((rand()*255/RAND_MAX) << 8) | ((rand()*255/RAND_MAX) << 16) | + // ((rand()*255/RAND_MAX) << 24) | 0xFF; + return true; + } + + for (int i = 0; i < sizeof(s_colorMapping) / sizeof(*s_colorMapping); i++) + { + if (!stricmp(str, s_colorMapping[i].name)) + { + *colour = s_colorMapping[i].value; + return true; + } + } + } + + return false; + } + + static inline uint32 gui_getcolour_wrapped(lua_State *L, int offset, bool hasDefaultValue, uint32 defaultColour) + { + switch (lua_type(L, offset)) + { + case LUA_TSTRING: + { + const char *str = lua_tostring(L, offset); + uint32 colour; + + if (str2colour(&colour, L, str)) + return colour; + else + { + if (hasDefaultValue) + return defaultColour; + else + return luaL_error(L, "unknown colour %s", str); + } + } + + case LUA_TNUMBER: + { + uint32 colour = (uint32) lua_tointeger(L, offset); + return colour; + } + + case LUA_TTABLE: + { + int color = 0xFF; + lua_pushnil(L); // first key + int keyIndex = lua_gettop(L); + int valueIndex = keyIndex + 1; + bool first = true; + while (lua_next(L, offset)) + { + bool keyIsString = (lua_type(L, keyIndex) == LUA_TSTRING); + bool keyIsNumber = (lua_type(L, keyIndex) == LUA_TNUMBER); + int key = keyIsString ? tolower(*lua_tostring(L, keyIndex)) : (keyIsNumber ? lua_tointeger(L, keyIndex) : 0); + int value = lua_tointeger(L, valueIndex); + if (value < 0) value = 0; + if (value > 255) value = 255; + switch (key) + { + case 1: + case 'r': + color |= value << 24; break; + case 2: + case 'g': + color |= value << 16; break; + case 3: + case 'b': + color |= value << 8; break; + case 4: + case 'a': + color = (color & ~0xFF) | value; break; + } + lua_pop(L, 1); + } + return color; + } break; + + case LUA_TFUNCTION: + luaL_error(L, "invalid colour"); // NYI + return 0; + + default: + if (hasDefaultValue) + return defaultColour; + else + return luaL_error(L, "invalid colour"); + } + } + + static uint32 gui_getcolour(lua_State *L, int offset) + { + uint32 colour; + int a, r, g, b; + + colour = gui_getcolour_wrapped(L, offset, false, 0); + a = ((colour & 0xff) * transparencyModifier) / 255; + if (a > 255) + a = 255; + b = (colour >> 8) & 0xff; + g = (colour >> 16) & 0xff; + r = (colour >> 24) & 0xff; + return LUA_BUILD_PIXEL(a, r, g, b); + } + + static uint32 gui_optcolour(lua_State *L, int offset, uint32 defaultColour) + { + uint32 colour; + int a, r, g, b; + uint8 defA, defB, defG, defR; + + LUA_DECOMPOSE_PIXEL(defaultColour, defA, defR, defG, defB); + defaultColour = (defR << 24) | (defG << 16) | (defB << 8) | defA; + + colour = gui_getcolour_wrapped(L, offset, true, defaultColour); + a = ((colour & 0xff) * transparencyModifier) / 255; + if (a > 255) + a = 255; + b = (colour >> 8) & 0xff; + g = (colour >> 16) & 0xff; + r = (colour >> 24) & 0xff; + return LUA_BUILD_PIXEL(a, r, g, b); + } + +// gui.drawpixel(x,y,colour) + static int gui_drawpixel(lua_State *L) + { + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); + + uint32 colour = gui_getcolour(L, 3); + + // if (!gui_check_boundary(x, y)) + // luaL_error(L,"bad coordinates"); + gui_prepare(); + + gui_drawpixel_internal(x, y, colour); + + return 0; + } + +// gui.drawline(x1,y1,x2,y2,color,skipFirst) + static int gui_drawline(lua_State *L) + { + int x1, y1, x2, y2; + uint32 color; + x1 = luaL_checkinteger(L, 1); + y1 = luaL_checkinteger(L, 2); + x2 = luaL_checkinteger(L, 3); + y2 = luaL_checkinteger(L, 4); + color = gui_optcolour(L, 5, LUA_BUILD_PIXEL(255, 255, 255, 255)); + int skipFirst = lua_toboolean(L, 6); + + gui_prepare(); + + gui_drawline_internal(x2, y2, x1, y1, !skipFirst, color); + + return 0; + } + +// gui.drawbox(x1, y1, x2, y2, fillcolor, outlinecolor) + static int gui_drawbox(lua_State *L) + { + int x1, y1, x2, y2; + uint32 fillcolor; + uint32 outlinecolor; + + x1 = luaL_checkinteger(L, 1); + y1 = luaL_checkinteger(L, 2); + x2 = luaL_checkinteger(L, 3); + y2 = luaL_checkinteger(L, 4); + fillcolor = gui_optcolour(L, 5, LUA_BUILD_PIXEL(63, 255, 255, 255)); + outlinecolor = gui_optcolour(L, 6, LUA_BUILD_PIXEL(255, LUA_PIXEL_R(fillcolor), LUA_PIXEL_G(fillcolor), LUA_PIXEL_B(fillcolor))); + + if (x1 > x2) + std::swap(x1, x2); + if (y1 > y2) + std::swap(y1, y2); + + gui_prepare(); + + gui_drawbox_internal(x1, y1, x2, y2, outlinecolor); + if ((x2 - x1) >= 2 && (y2 - y1) >= 2) + gui_fillbox_internal(x1 + 1, y1 + 1, x2 - 1, y2 - 1, fillcolor); + + return 0; + } + +// gui.drawcircle(x0, y0, radius, colour) + static int gui_drawcircle(lua_State *L) + { + int x, y, r; + uint32 colour; + + x = luaL_checkinteger(L, 1); + y = luaL_checkinteger(L, 2); + r = luaL_checkinteger(L, 3); + colour = gui_getcolour(L, 4); + + gui_prepare(); + + gui_drawcircle_internal(x, y, r, colour); + + return 0; + } + +// gui.fillbox(x1, y1, x2, y2, colour) + static int gui_fillbox(lua_State *L) + { + int x1, y1, x2, y2; + uint32 colour; + + x1 = luaL_checkinteger(L, 1); + y1 = luaL_checkinteger(L, 2); + x2 = luaL_checkinteger(L, 3); + y2 = luaL_checkinteger(L, 4); + colour = gui_getcolour(L, 5); + + // if (!gui_check_boundary(x1, y1)) + // luaL_error(L,"bad coordinates"); + // + // if (!gui_check_boundary(x2, y2)) + // luaL_error(L,"bad coordinates"); + gui_prepare(); + + if (!gui_checkbox(x1, y1, x2, y2)) + return 0; + + gui_fillbox_internal(x1, y1, x2, y2, colour); + + return 0; + } + +// gui.fillcircle(x0, y0, radius, colour) + static int gui_fillcircle(lua_State *L) + { + int x, y, r; + uint32 colour; + + x = luaL_checkinteger(L, 1); + y = luaL_checkinteger(L, 2); + r = luaL_checkinteger(L, 3); + colour = gui_getcolour(L, 4); + + gui_prepare(); + + gui_fillcircle_internal(x, y, r, colour); + + return 0; + } + + static int gui_getpixel(lua_State *L) + { + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); + + int pixWidth = 240, pixHeight = 160; + int scrWidth = 240, scrHeight = 160; + int scrOffsetX = 0, scrOffsetY = 0; + int pitch; + if (!systemIsRunningGBA()) + { + if (gbBorderOn) + { + pixWidth = 256, pixHeight = 224; + scrOffsetX = 48, scrOffsetY = 40; + } + else + { + pixWidth = 160, pixHeight = 144; + } + scrWidth = 160, scrHeight = 144; + } + pitch = pixWidth * (systemColorDepth / 8) + (systemColorDepth == 24 ? 0 : 4); + scrOffsetY++; // don't know why it's needed + + if (!(x >= 0 && y >= 0 && x < scrWidth && y < scrHeight) /*!gui_check_boundary(x,y)*/) + { + lua_pushinteger(L, 0); + lua_pushinteger(L, 0); + lua_pushinteger(L, 0); + } + else + { + switch (systemColorDepth) + { + case 16: + { + uint16 *screen = (uint16 *) (&pix[scrOffsetY * pitch + scrOffsetX * 2]); + uint16 pixColor = screen[y * pitch / 2 + x]; + lua_pushinteger(L, (pixColor >> 8) & 0xF8); // red + lua_pushinteger(L, (pixColor >> 3) & 0xFC); // green + lua_pushinteger(L, (pixColor << 3) & 0xF8); // blue + } + break; + case 24: + { + uint8 *screen = &pix[scrOffsetY * pitch + scrOffsetX * 3]; + lua_pushinteger(L, screen[y * pitch + x * 3 + 2]); // red + lua_pushinteger(L, screen[y * pitch + x * 3 + 1]); // green + lua_pushinteger(L, screen[y * pitch + x * 3 + 0]); // blue + } + break; + case 32: + { + uint8 *screen = &pix[scrOffsetY * pitch + scrOffsetX * 4]; + lua_pushinteger(L, screen[y * pitch + x * 4 + 2]); // red + lua_pushinteger(L, screen[y * pitch + x * 4 + 1]); // green + lua_pushinteger(L, screen[y * pitch + x * 4 + 0]); // blue + } + break; + default: + lua_pushinteger(L, 0); + lua_pushinteger(L, 0); + lua_pushinteger(L, 0); + break; + } + } + return 3; + } + + static int gui_parsecolor(lua_State *L) + { + int r, g, b, a; + uint32 color = gui_getcolour(L, 1); + LUA_DECOMPOSE_PIXEL(color, a, r, g, b); + lua_pushinteger(L, r); + lua_pushinteger(L, g); + lua_pushinteger(L, b); + lua_pushinteger(L, a); + return 4; + } + +// gui.gdscreenshot() +// +// Returns a screen shot as a string in gd's v1 file format. +// This allows us to make screen shots available without gd installed locally. +// Users can also just grab pixels via substring selection. +// +// I think... Does lua support grabbing byte values from a string? // yes, string.byte(str,offset) +// Well, either way, just install gd and do what you like with it. +// It really is easier that way. + +// example: gd.createFromGdStr(gui.gdscreenshot()):png("outputimage.png") + static int gui_gdscreenshot(lua_State *L) + { + int xofs = 0, yofs = 0, ppl = 240, width = 240, height = 160; + if (!systemIsRunningGBA()) + { + if (gbBorderOn) + xofs = 48, yofs = 40, ppl = 256; + else + ppl = 160; + width = 160, height = 144; + } + + yofs++; + + //int pitch = (((ppl * systemColorDepth + 7)>>3)+3)&~3; + int pitch = ppl * (systemColorDepth / 8) + (systemColorDepth == 24 ? 0 : 4); + uint8 *screen = &pix[yofs * pitch + xofs * (systemColorDepth / 8)]; + + int size = 11 + width * height * 4; + char *str = new char[size + 1]; + str[size] = 0; + + unsigned char *ptr = (unsigned char *)str; + + // GD format header for truecolor image (11 bytes) + *ptr++ = (65534 >> 8) & 0xFF; + *ptr++ = (65534) & 0xFF; + *ptr++ = (width >> 8) & 0xFF; + *ptr++ = (width) & 0xFF; + *ptr++ = (height >> 8) & 0xFF; + *ptr++ = (height) & 0xFF; + *ptr++ = 1; + *ptr++ = 255; + *ptr++ = 255; + *ptr++ = 255; + *ptr++ = 255; + + GetColorFunc getColor; + getColorIOFunc(systemColorDepth, &getColor, NULL); + + int x, y; + for (y = 0; y < height; y++) + { + uint8 *s = &screen[y * pitch]; + for (x = 0; x < width; x++, s += systemColorDepth / 8) + { + uint8 r, g, b; + getColor(s, &r, &g, &b); + + *ptr++ = 0; + *ptr++ = r; + *ptr++ = g; + *ptr++ = b; + } + } + + lua_pushlstring(L, str, size); + delete[] str; + return 1; + } + +// gui.opacity(number alphaValue) +// sets the transparency of subsequent draw calls +// 0.0 is completely transparent, 1.0 is completely opaque +// non-integer values are supported and meaningful, as are values greater than 1.0 +// it is not necessary to use this function to get transparency (or the less-recommended gui.transparency() either), +// because you can provide an alpha value in the color argument of each draw call. + +// however, it can be convenient to be able to globally modify the drawing transparency + static int gui_setopacity(lua_State *L) + { + double opacF = luaL_checknumber(L, 1); + transparencyModifier = (int)(opacF * 255); + if (transparencyModifier < 0) + transparencyModifier = 0; + return 0; + } + +// gui.transparency(int strength) +// + +// 0 = solid, + static int gui_transparency(lua_State *L) + { + double trans = luaL_checknumber(L, 1); + transparencyModifier = (int)((4.0 - trans) / 4.0 * 255); + if (transparencyModifier < 0) + transparencyModifier = 0; + return 0; + } + + static const uint32 Small_Font_Data[] = + { + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, // 32 + 0x00000000, 0x00000300, 0x00000400, 0x00000500, 0x00000000, 0x00000700, 0x00000000, // 33 ! + 0x00000000, 0x00040002, 0x00050003, 0x00000000, 0x00000000, 0x00000000, 0x00000000, // 34 " + 0x00000000, 0x00040002, 0x00050403, 0x00060004, 0x00070605, 0x00080006, 0x00000000, // 35 # + 0x00000000, 0x00040300, 0x00000403, 0x00000500, 0x00070600, 0x00000706, 0x00000000, // 36 $ + 0x00000000, 0x00000002, 0x00050000, 0x00000500, 0x00000005, 0x00080000, 0x00000000, // 37 % + 0x00000000, 0x00000300, 0x00050003, 0x00000500, 0x00070005, 0x00080700, 0x00000000, // 38 & + 0x00000000, 0x00000300, 0x00000400, 0x00000000, 0x00000000, 0x00000000, 0x00000000, // 39 ' + 0x00000000, 0x00000300, 0x00000003, 0x00000004, 0x00000005, 0x00000700, 0x00000000, // 40 ( + 0x00000000, 0x00000300, 0x00050000, 0x00060000, 0x00070000, 0x00000700, 0x00000000, // 41 ) + 0x00000000, 0x00000000, 0x00000400, 0x00060504, 0x00000600, 0x00080006, 0x00000000, // 42 * + 0x00000000, 0x00000000, 0x00000400, 0x00060504, 0x00000600, 0x00000000, 0x00000000, // 43 + + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000600, 0x00000700, 0x00000007, // 44 , + 0x00000000, 0x00000000, 0x00000000, 0x00060504, 0x00000000, 0x00000000, 0x00000000, // 45 - + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000700, 0x00000000, // 46 . + 0x00030000, 0x00040000, 0x00000400, 0x00000500, 0x00000005, 0x00000006, 0x00000000, // 47 / + 0x00000000, 0x00000300, 0x00050003, 0x00060004, 0x00070005, 0x00000700, 0x00000000, // 48 0 + 0x00000000, 0x00000300, 0x00000403, 0x00000500, 0x00000600, 0x00000700, 0x00000000, // 49 1 + 0x00000000, 0x00000302, 0x00050000, 0x00000500, 0x00000005, 0x00080706, 0x00000000, // 50 2 + 0x00000000, 0x00000302, 0x00050000, 0x00000504, 0x00070000, 0x00000706, 0x00000000, // 51 3 + 0x00000000, 0x00000300, 0x00000003, 0x00060004, 0x00070605, 0x00080000, 0x00000000, // 52 4 + 0x00000000, 0x00040302, 0x00000003, 0x00000504, 0x00070000, 0x00000706, 0x00000000, // 53 5 + 0x00000000, 0x00000300, 0x00000003, 0x00000504, 0x00070005, 0x00000700, 0x00000000, // 54 6 + 0x00000000, 0x00040302, 0x00050000, 0x00000500, 0x00000600, 0x00000700, 0x00000000, // 55 7 + 0x00000000, 0x00000300, 0x00050003, 0x00000500, 0x00070005, 0x00000700, 0x00000000, // 56 8 + 0x00000000, 0x00000300, 0x00050003, 0x00060500, 0x00070000, 0x00000700, 0x00000000, // 57 9 + 0x00000000, 0x00000000, 0x00000400, 0x00000000, 0x00000000, 0x00000700, 0x00000000, // 58 : + 0x00000000, 0x00000000, 0x00000000, 0x00000500, 0x00000000, 0x00000700, 0x00000007, // 59 ; + 0x00000000, 0x00040000, 0x00000400, 0x00000004, 0x00000600, 0x00080000, 0x00000000, // 60 < + 0x00000000, 0x00000000, 0x00050403, 0x00000000, 0x00070605, 0x00000000, 0x00000000, // 61 = + 0x00000000, 0x00000002, 0x00000400, 0x00060000, 0x00000600, 0x00000006, 0x00000000, // 62 > + 0x00000000, 0x00000302, 0x00050000, 0x00000500, 0x00000000, 0x00000700, 0x00000000, // 63 ? + 0x00000000, 0x00000300, 0x00050400, 0x00060004, 0x00070600, 0x00000000, 0x00000000, // 64 @ + 0x00000000, 0x00000300, 0x00050003, 0x00060504, 0x00070005, 0x00080006, 0x00000000, // 65 A + 0x00000000, 0x00000302, 0x00050003, 0x00000504, 0x00070005, 0x00000706, 0x00000000, // 66 B + 0x00000000, 0x00040300, 0x00000003, 0x00000004, 0x00000005, 0x00080700, 0x00000000, // 67 C + 0x00000000, 0x00000302, 0x00050003, 0x00060004, 0x00070005, 0x00000706, 0x00000000, // 68 D + 0x00000000, 0x00040302, 0x00000003, 0x00000504, 0x00000005, 0x00080706, 0x00000000, // 69 E + 0x00000000, 0x00040302, 0x00000003, 0x00000504, 0x00000005, 0x00000006, 0x00000000, // 70 F + 0x00000000, 0x00040300, 0x00000003, 0x00060004, 0x00070005, 0x00080700, 0x00000000, // 71 G + 0x00000000, 0x00040002, 0x00050003, 0x00060504, 0x00070005, 0x00080006, 0x00000000, // 72 H + 0x00000000, 0x00000300, 0x00000400, 0x00000500, 0x00000600, 0x00000700, 0x00000000, // 73 I + 0x00000000, 0x00040000, 0x00050000, 0x00060000, 0x00070005, 0x00000700, 0x00000000, // 74 J + 0x00000000, 0x00040002, 0x00050003, 0x00000504, 0x00070005, 0x00080006, 0x00000000, // 75 K + 0x00000000, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00080706, 0x00000000, // 76 l + 0x00000000, 0x00040002, 0x00050403, 0x00060004, 0x00070005, 0x00080006, 0x00000000, // 77 M + 0x00000000, 0x00000302, 0x00050003, 0x00060004, 0x00070005, 0x00080006, 0x00000000, // 78 N + 0x00000000, 0x00040302, 0x00050003, 0x00060004, 0x00070005, 0x00080706, 0x00000000, // 79 O + 0x00000000, 0x00000302, 0x00050003, 0x00000504, 0x00000005, 0x00000006, 0x00000000, // 80 P + 0x00000000, 0x00040302, 0x00050003, 0x00060004, 0x00070005, 0x00080706, 0x00090000, // 81 Q + 0x00000000, 0x00000302, 0x00050003, 0x00000504, 0x00070005, 0x00080006, 0x00000000, // 82 R + 0x00000000, 0x00040300, 0x00000003, 0x00000500, 0x00070000, 0x00000706, 0x00000000, // 83 S + 0x00000000, 0x00040302, 0x00000400, 0x00000500, 0x00000600, 0x00000700, 0x00000000, // 84 T + 0x00000000, 0x00040002, 0x00050003, 0x00060004, 0x00070005, 0x00080706, 0x00000000, // 85 U + 0x00000000, 0x00040002, 0x00050003, 0x00060004, 0x00000600, 0x00000700, 0x00000000, // 86 V + 0x00000000, 0x00040002, 0x00050003, 0x00060004, 0x00070605, 0x00080006, 0x00000000, // 87 W + 0x00000000, 0x00040002, 0x00050003, 0x00000500, 0x00070005, 0x00080006, 0x00000000, // 88 X + 0x00000000, 0x00040002, 0x00050003, 0x00000500, 0x00000600, 0x00000700, 0x00000000, // 89 Y + 0x00000000, 0x00040302, 0x00050000, 0x00000500, 0x00000005, 0x00080706, 0x00000000, // 90 Z + 0x00000000, 0x00040300, 0x00000400, 0x00000500, 0x00000600, 0x00080700, 0x00000000, // 91 [ + 0x00000000, 0x00000002, 0x00000400, 0x00000500, 0x00070000, 0x00080000, 0x00000000, // 92 '\' + 0x00000000, 0x00000302, 0x00000400, 0x00000500, 0x00000600, 0x00000706, 0x00000000, // 93 ] + 0x00000000, 0x00000300, 0x00050003, 0x00000000, 0x00000000, 0x00000000, 0x00000000, // 94 ^ + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00080706, 0x00000000, // 95 _ + 0x00000000, 0x00000002, 0x00000400, 0x00000000, 0x00000000, 0x00000000, 0x00000000, // 96 ` + 0x00000000, 0x00000000, 0x00050400, 0x00060004, 0x00070005, 0x00080700, 0x00000000, // 97 a + 0x00000000, 0x00000002, 0x00000003, 0x00000504, 0x00070005, 0x00000706, 0x00000000, // 98 b + 0x00000000, 0x00000000, 0x00050400, 0x00000004, 0x00000005, 0x00080700, 0x00000000, // 99 c + 0x00000000, 0x00040000, 0x00050000, 0x00060500, 0x00070005, 0x00080700, 0x00000000, // 100 d + 0x00000000, 0x00000000, 0x00050400, 0x00060504, 0x00000005, 0x00080700, 0x00000000, // 101 e + 0x00000000, 0x00040300, 0x00000003, 0x00000504, 0x00000005, 0x00000006, 0x00000000, // 102 f + 0x00000000, 0x00000000, 0x00050400, 0x00060004, 0x00070600, 0x00080000, 0x00000807, // 103 g + 0x00000000, 0x00000002, 0x00000003, 0x00000504, 0x00070005, 0x00080006, 0x00000000, // 104 h + 0x00000000, 0x00000300, 0x00000000, 0x00000500, 0x00000600, 0x00000700, 0x00000000, // 105 i + 0x00000000, 0x00000300, 0x00000000, 0x00000500, 0x00000600, 0x00000700, 0x00000007, // 106 j + 0x00000000, 0x00000002, 0x00000003, 0x00060004, 0x00000605, 0x00080006, 0x00000000, // 107 k + 0x00000000, 0x00000300, 0x00000400, 0x00000500, 0x00000600, 0x00080000, 0x00000000, // 108 l + 0x00000000, 0x00000000, 0x00050003, 0x00060504, 0x00070005, 0x00080006, 0x00000000, // 109 m + 0x00000000, 0x00000000, 0x00000403, 0x00060004, 0x00070005, 0x00080006, 0x00000000, // 110 n + 0x00000000, 0x00000000, 0x00000400, 0x00060004, 0x00070005, 0x00000700, 0x00000000, // 111 o + 0x00000000, 0x00000000, 0x00000400, 0x00060004, 0x00000605, 0x00000006, 0x00000007, // 112 p + 0x00000000, 0x00000000, 0x00000400, 0x00060004, 0x00070600, 0x00080000, 0x00090000, // 113 q + 0x00000000, 0x00000000, 0x00050003, 0x00000504, 0x00000005, 0x00000006, 0x00000000, // 114 r + 0x00000000, 0x00000000, 0x00050400, 0x00000004, 0x00070600, 0x00000706, 0x00000000, // 115 s + 0x00000000, 0x00000300, 0x00050403, 0x00000500, 0x00000600, 0x00080000, 0x00000000, // 116 t + 0x00000000, 0x00000000, 0x00050003, 0x00060004, 0x00070005, 0x00080700, 0x00000000, // 117 u + 0x00000000, 0x00000000, 0x00050003, 0x00060004, 0x00070005, 0x00000700, 0x00000000, // 118 v + 0x00000000, 0x00000000, 0x00050003, 0x00060004, 0x00070605, 0x00080006, 0x00000000, // 119 w + 0x00000000, 0x00000000, 0x00050003, 0x00000500, 0x00070005, 0x00080006, 0x00000000, // 120 x + 0x00000000, 0x00000000, 0x00050003, 0x00060004, 0x00000600, 0x00000700, 0x00000007, // 121 y + 0x00000000, 0x00000000, 0x00050403, 0x00000500, 0x00000005, 0x00080706, 0x00000000, // 122 z + 0x00000000, 0x00040300, 0x00000400, 0x00000504, 0x00000600, 0x00080700, 0x00000000, // 123 { + 0x00000000, 0x00000300, 0x00000400, 0x00000000, 0x00000600, 0x00000700, 0x00000000, // 124 | + 0x00000000, 0x00000302, 0x00000400, 0x00060500, 0x00000600, 0x00000706, 0x00000000, // 125 } + 0x00000000, 0x00000302, 0x00050000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, // 126 ~ + 0x00000000, 0x00000000, 0x00000400, 0x00060004, 0x00070605, 0x00000000, 0x00000000, // 127  + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + }; + + static void PutTextInternal(const char *str, int len, short x, short y, int color, int backcolor) + { + int Opac = (color >> 24) & 0xFF; + int backOpac = (backcolor >> 24) & 0xFF; + int origX = x; + + if (!Opac && !backOpac) + return; + + while (*str && len && y < LUA_SCREEN_HEIGHT) + { + int c = *str++; + while (x > LUA_SCREEN_WIDTH && c != '\n') + { + c = *str; + if (c == '\0') + break; + str++; + } + + if (c == '\n') + { + x = origX; + y += 8; + continue; + } + else if (c == '\t') // just in case + { + const int tabSpace = 8; + x += (tabSpace - (((x - origX) / 4) % tabSpace)) * 4; + continue; + } + + if ((unsigned int)(c - 32) >= 96) + continue; + + const unsigned char *Cur_Glyph = (const unsigned char *) &Small_Font_Data + (c - 32) * 7 * 4; + + for (int y2 = 0; y2 < 8; y2++) + { + unsigned int glyphLine = *((unsigned int *)Cur_Glyph + y2); + for (int x2 = -1; x2 < 4; x2++) + { + int shift = x2 << 3; + int mask = 0xFF << shift; + int intensity = (glyphLine & mask) >> shift; + + if (intensity && x2 >= 0 && y2 < 7) + { + //int xdraw = max(0,min(LUA_SCREEN_WIDTH - 1,x+x2)); + //int ydraw = max(0,min(LUA_SCREEN_HEIGHT - 1,y+y2)); + //gui_drawpixel_fast(xdraw, ydraw, color); + gui_drawpixel_internal(x + x2, y + y2, color); + } + else if (backOpac) + { + for (int y3 = max(0, y2 - 1); y3 <= min(6, y2 + 1); y3++) + { + unsigned int glyphLine = *((unsigned int *)Cur_Glyph + y3); + for (int x3 = max(0, x2 - 1); x3 <= min(3, x2 + 1); x3++) + { + int shift = x3 << 3; + int mask = 0xFF << shift; + intensity |= (glyphLine & mask) >> shift; + if (intensity) + goto draw_outline; // speedup? + } + } + +draw_outline: + if (intensity) + { + //int xdraw = max(0,min(LUA_SCREEN_WIDTH - 1,x+x2)); + //int ydraw = max(0,min(LUA_SCREEN_HEIGHT - 1,y+y2)); + //gui_drawpixel_fast(xdraw, ydraw, backcolor); + gui_drawpixel_internal(x + x2, y + y2, backcolor); + } + } + } + } + + x += 4; + len--; + } + } + + static int strlinelen(const char *string) + { + const char *s = string; + while (*s && *s != '\n') + s++; + if (*s) + s++; + return s - string; + } + + static void LuaDisplayString(const char *string, int y, int x, uint32 color, uint32 outlineColor) + { + if (!string) + return; + + gui_prepare(); + + PutTextInternal(string, strlen(string), x, y, color, outlineColor); + + /* + const char* ptr = string; + while(*ptr && y < LUA_SCREEN_HEIGHT) + { + int len = strlinelen(ptr); + int skip = 0; + if(len < 1) len = 1; + + // break up the line if it's too long to display otherwise + if(len > 63) + { + len = 63; + const char* ptr2 = ptr + len-1; + for(int j = len-1; j; j--, ptr2--) + { + if(*ptr2 == ' ' || *ptr2 == '\t') + { + len = j; + skip = 1; + break; + } + } + } + + int xl = 0; + int yl = 0; + int xh = (LUA_SCREEN_WIDTH - 1 - 1) - 4*len; + int yh = LUA_SCREEN_HEIGHT - 1; + int x2 = min(max(x,xl),xh); + int y2 = min(max(y,yl),yh); + + PutTextInternal(ptr,len,x2,y2,color,outlineColor); + + ptr += len + skip; + y += 8; + } + */ + } + +// gui.text(int x, int y, string msg) +// +// Displays the given text on the screen, using the same font and techniques as the + +// main HUD. + static int gui_text(lua_State *L) + { + //extern int font_height; + const char *msg; + int x, y; + uint32 colour, borderColour; + + x = luaL_checkinteger(L, 1); + y = luaL_checkinteger(L, 2); + //msg = luaL_checkstring(L, 3); + msg = toCString(L, 3); + + // if (x < 0 || x >= LUA_SCREEN_WIDTH || y < 0 || y >= (LUA_SCREEN_HEIGHT - font_height)) + // luaL_error(L,"bad coordinates"); + colour = gui_optcolour(L, 4, LUA_BUILD_PIXEL(255, 255, 255, 255)); + borderColour = gui_optcolour(L, 5, LUA_BUILD_PIXEL(255, 0, 0, 0)); + + gui_prepare(); + + LuaDisplayString(msg, y, x, colour, borderColour); + + return 0; + } + +// gui.gdoverlay([int dx=0, int dy=0,] string str [, sx=0, sy=0, sw, sh] [, float alphamul=1.0]) +// +// Overlays the given image on the screen. + +// example: gui.gdoverlay(gd.createFromPng("myimage.png"):gdStr()) + static int gui_gdoverlay(lua_State *L) + { + int argCount = lua_gettop(L); + + int xStartDst = 0; + int yStartDst = 0; + int xStartSrc = 0; + int yStartSrc = 0; + + int index = 1; + if (lua_type(L, index) == LUA_TNUMBER) + { + xStartDst = lua_tointeger(L, index++); + if (lua_type(L, index) == LUA_TNUMBER) + yStartDst = lua_tointeger(L, index++); + } + + luaL_checktype(L, index, LUA_TSTRING); + + const unsigned char *ptr = (const unsigned char *)lua_tostring(L, index++); + + if (ptr[0] != 255 || (ptr[1] != 254 && ptr[1] != 255)) + luaL_error(L, "bad image data"); + + bool trueColor = (ptr[1] == 254); + ptr += 2; + + int imgwidth = *ptr++ << 8; + imgwidth |= *ptr++; + + int width = imgwidth; + int imgheight = *ptr++ << 8; + imgheight |= *ptr++; + + int height = imgheight; + if ((!trueColor && *ptr) || (trueColor && !*ptr)) + luaL_error(L, "bad image data"); + ptr++; + + int pitch = imgwidth * (trueColor ? 4 : 1); + + if ((argCount - index + 1) >= 4) + { + xStartSrc = luaL_checkinteger(L, index++); + yStartSrc = luaL_checkinteger(L, index++); + width = luaL_checkinteger(L, index++); + height = luaL_checkinteger(L, index++); + } + + int alphaMul = transparencyModifier; + if (lua_isnumber(L, index)) + alphaMul = (int)(alphaMul * lua_tonumber(L, index++)); + if (alphaMul <= 0) + return 0; + + // since there aren't that many possible opacity levels, + // do the opacity modification calculations beforehand instead of per pixel + int opacMap[256]; + for (int i = 0; i < 128; i++) + { + int opac = 255 - ((i << 1) | (i & 1)); // gdAlphaMax = 127, not 255 + opac = (opac * alphaMul) / 255; + if (opac < 0) + opac = 0; + if (opac > 255) + opac = 255; + opacMap[i] = opac; + } + + for (int i = 128; i < 256; i++) + opacMap[i] = 0; // what should we do for them, actually? + int colorsTotal = 0; + if (!trueColor) + { + colorsTotal = *ptr++ << 8; + colorsTotal |= *ptr++; + } + + int transparent = *ptr++ << 24; + transparent |= *ptr++ << 16; + transparent |= *ptr++ << 8; + transparent |= *ptr++; + struct + { + uint8 r, g, b, a; + } pal[256]; + if (!trueColor) + for (int i = 0; i < 256; i++) + { + pal[i].r = *ptr++; + pal[i].g = *ptr++; + pal[i].b = *ptr++; + pal[i].a = opacMap[*ptr++]; + } + + // some of clippings + if (xStartSrc < 0) + { + width += xStartSrc; + xStartDst -= xStartSrc; + xStartSrc = 0; + } + + if (yStartSrc < 0) + { + height += yStartSrc; + yStartDst -= yStartSrc; + yStartSrc = 0; + } + + if (xStartSrc + width >= imgwidth) + width = imgwidth - xStartSrc; + if (yStartSrc + height >= imgheight) + height = imgheight - yStartSrc; + if (xStartDst < 0) + { + width += xStartDst; + if (width <= 0) + return 0; + xStartSrc = -xStartDst; + xStartDst = 0; + } + + if (yStartDst < 0) + { + height += yStartDst; + if (height <= 0) + return 0; + yStartSrc = -yStartDst; + yStartDst = 0; + } + + if (xStartDst + width >= LUA_SCREEN_WIDTH) + width = LUA_SCREEN_WIDTH - xStartDst; + if (yStartDst + height >= LUA_SCREEN_HEIGHT) + height = LUA_SCREEN_HEIGHT - yStartDst; + if (width <= 0 || height <= 0) + return 0; // out of screen or invalid size + gui_prepare(); + + const uint8 *pix = (const uint8 *)(&ptr[yStartSrc * pitch + (xStartSrc * (trueColor ? 4 : 1))]); + int bytesToNextLine = pitch - (width * (trueColor ? 4 : 1)); + if (trueColor) + { + for (int y = yStartDst; y < height + yStartDst && y < LUA_SCREEN_HEIGHT; y++, pix += bytesToNextLine) + { + for (int x = xStartDst; x < width + xStartDst && x < LUA_SCREEN_WIDTH; x++, pix += 4) + { + gui_drawpixel_fast(x, y, LUA_BUILD_PIXEL(opacMap[pix[0]], pix[1], pix[2], pix[3])); + } + } + } + else + { + for (int y = yStartDst; y < height + yStartDst && y < LUA_SCREEN_HEIGHT; y++, pix += bytesToNextLine) + { + for (int x = xStartDst; x < width + xStartDst && x < LUA_SCREEN_WIDTH; x++, pix++) + { + gui_drawpixel_fast(x, y, LUA_BUILD_PIXEL(pal[*pix].a, pal[*pix].r, pal[*pix].g, pal[*pix].b)); + } + } + } + + return 0; + } + +// function gui.register(function f) +// +// This function will be called just before a graphical update. +// More complicated, but doesn't suffer any frame delays. +// Nil will be accepted in place of a function to erase +// a previously registered function, and the previous function + +// (if any) is returned, or nil if none. + static int gui_register(lua_State *L) + { + // We'll do this straight up. + // First set up the stack. + lua_settop(L, 1); + + // Verify the validity of the entry + if (!lua_isnil(L, 1)) + luaL_checktype(L, 1, LUA_TFUNCTION); + + // Get the old value + lua_getfield(L, LUA_REGISTRYINDEX, guiCallbackTable); + + // Save the new value + lua_pushvalue(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, guiCallbackTable); + + // The old value is on top of the stack. Return it. + return 1; + } + +// string gui.popup(string message, [string type = "ok"]) +// + +// Popup dialog! + int gui_popup(lua_State *L) + { + const char *message = luaL_checkstring(L, 1); + const char *type = luaL_optstring(L, 2, "ok"); + +#if (defined(WIN32) && !defined(SDL)) + int t; + if (strcmp(type, "ok") == 0) + t = MB_OK; + else if (strcmp(type, "yesno") == 0) + t = MB_YESNO; + else if (strcmp(type, "yesnocancel") == 0) + t = MB_YESNOCANCEL; + else + return luaL_error(L, "invalid popup type \"%s\"", type); + + theApp.winCheckFullscreen(); + systemSoundClearBuffer(); + int result = AfxGetApp()->m_pMainWnd->MessageBox(message, "Lua Script Pop-up", t); + + lua_settop(L, 1); + + if (t != MB_OK) + { + if (result == IDYES) + lua_pushstring(L, "yes"); + else if (result == IDNO) + lua_pushstring(L, "no"); + else if (result == IDCANCEL) + lua_pushstring(L, "cancel"); + else + luaL_error(L, "win32 unrecognized return value %d", result); + return 1; + } + + // else, we don't care. + return 0; +#else + char *t; + #ifdef __linux + // The Linux backend has a "FromPause" variable. + // If set to 1, assume some known external event has screwed with the flow of time. + // Since this pauses the emulator waiting for a response, we set it to 1. +// FIXME: Well, actually it doesn't +// extern int FromPause; +// FromPause = 1; + + int pid; // appease compiler + + // Before doing any work, verify the correctness of the parameters. + if (strcmp(type, "ok") == 0) + t = "OK:100"; + else if (strcmp(type, "yesno") == 0) + t = "Yes:100,No:101"; + else if (strcmp(type, "yesnocancel") == 0) + t = "Yes:100,No:101,Cancel:102"; + else + return luaL_error(L, "invalid popup type \"%s\"", type); + + // Can we find a copy of xmessage? Search the path. + char *path = strdup(getenv("PATH")); + + char *current = path; + + char *colon; + + int found = 0; + + while (current) + { + colon = strchr(current, ':'); + + // Clip off the colon. + *colon++ = 0; + + int len = strlen(current); + char *filename = (char *)malloc(len + 12); // always give excess + snprintf(filename, len + 12, "%s/xmessage", current); + + if (access(filename, X_OK) == 0) + { + free(filename); + found = 1; + break; + } + + // Failed, move on. + current = colon; + free(filename); + } + + free(path); + + // We've found it? + if (!found) + goto use_console; + + pid = fork(); + if (pid == 0) + { // I'm the virgin sacrifice + // I'm gonna be dead in a matter of microseconds anyways, so wasted memory doesn't matter to me. + // Go ahead and abuse strdup. + char *parameters[] = { "xmessage", "-buttons", t, strdup(message), NULL }; + + execvp("xmessage", parameters); + + // Aw shitty + perror("exec xmessage"); + exit(1); + } + else if (pid < 0) // something went wrong!!! Oh hell... use the console + goto use_console; + else + { + // We're the parent. Watch for the child. + int r; + int res = waitpid(pid, &r, 0); + if (res < 0) // wtf? + goto use_console; + + // The return value gets copmlicated... + if (!WIFEXITED(r)) + { + luaL_error(L, "don't screw with my xmessage process!"); + } + + r = WEXITSTATUS(r); + + // We assume it's worked. + if (r == 0) + { + return 0; // no parameters for an OK + } + + if (r == 100) + { + lua_pushstring(L, "yes"); + return 1; + } + + if (r == 101) + { + lua_pushstring(L, "no"); + return 1; + } + + if (r == 102) + { + lua_pushstring(L, "cancel"); + return 1; + } + + // Wtf? + return luaL_error(L, "popup failed due to unknown results involving xmessage (%d)", r); + } + +use_console: + #endif + + // All else has failed + if (strcmp(type, "ok") == 0) + t = ""; + else if (strcmp(type, "yesno") == 0) + t = "yn"; + else if (strcmp(type, "yesnocancel") == 0) + t = "ync"; + else + return luaL_error(L, "invalid popup type \"%s\"", type); + + fprintf(stderr, "Lua Message: %s\n", message); + + while (true) + { + char buffer[64]; + + // We don't want parameters + if (!t[0]) + { + fprintf(stderr, "[Press Enter]"); + fgets(buffer, sizeof(buffer), stdin); + + // We're done + return 0; + } + + fprintf(stderr, "(%s): ", t); + fgets(buffer, sizeof(buffer), stdin); + + // Check if the option is in the list + if (strchr(t, tolower(buffer[0]))) + { + switch (tolower(buffer[0])) + { + case 'y': + lua_pushstring(L, "yes"); + return 1; + case 'n': + lua_pushstring(L, "no"); + return 1; + case 'c': + lua_pushstring(L, "cancel"); + return 1; + default: + luaL_error(L, "internal logic error in console based prompts for gui.popup"); + } + } + + // We fell through, so we assume the user answered wrong and prompt again. + } + + // Nothing here, since the only way out is in the loop. +#endif + } + +#if (defined(WIN32) && !defined(SDL)) + const char *s_keyToName[256] = + { + NULL, + "leftclick", + "rightclick", + NULL, + "middleclick", + NULL, + NULL, + NULL, + "backspace", + "tab", + NULL, + NULL, + NULL, + "enter", + NULL, + NULL, + "shift", // 0x10 + "control", + "alt", + "pause", + "capslock", + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + "escape", + NULL, + NULL, + NULL, + NULL, + "space", // 0x20 + "pageup", + "pagedown", + "end", + "home", + "left", + "up", + "right", + "down", + NULL, + NULL, + NULL, + NULL, + "insert", + "delete", + NULL, + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", + NULL, NULL, NULL, NULL, NULL, NULL, NULL, + "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", + "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", + "U", "V", "W", "X", "Y", "Z", + NULL, + NULL, + NULL, + NULL, + NULL, + "numpad0", "numpad1", "numpad2", "numpad3", "numpad4", "numpad5", "numpad6", "numpad7", "numpad8", "numpad9", + "numpad*", "numpad+", + NULL, + "numpad-", "numpad.", "numpad/", + "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", + "F12", + "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", + "F24", + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + "numlock", + "scrolllock", + NULL, // 0x92 + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, // 0xB9 + "semicolon", + "plus", + "comma", + "minus", + "period", + "slash", + "tilde", + NULL, // 0xC1 + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, // 0xDA + "leftbracket", + "backslash", + "rightbracket", + "quote", + }; +#endif + +// input.get() +// takes no input, returns a lua table of entries representing the current input state, +// independent of the joypad buttons the emulated game thinks are pressed +// for example: +// if the user is holding the W key and the left mouse button +// and has the mouse at the bottom-right corner of the game screen, + +// then this would return {W=true, leftclick=true, xmouse=255, ymouse=223} + static int input_getcurrentinputstatus(lua_State *L) + { + lua_newtable(L); + +#if (defined(WIN32) && !defined(SDL)) + // keyboard and mouse button status + { + unsigned char keys[256]; + if (true /*!GUI.BackgroundInput*/) // TODO: background input + { + if (GetKeyboardState(keys)) + { + for (int i = 1; i < 255; i++) + { + int mask = (i == VK_CAPITAL || i == VK_NUMLOCK || i == VK_SCROLL) ? 0x01 : 0x80; + if (keys[i] & mask) + { + const char *name = s_keyToName[i]; + if (name) + { + lua_pushboolean(L, true); + lua_setfield(L, -2, name); + } + } + } + } + } + else // use a slightly different method that will detect background input: + { + for (int i = 1; i < 255; i++) + { + const char *name = s_keyToName[i]; + if (name) + { + int active; + if (i == VK_CAPITAL || i == VK_NUMLOCK || i == VK_SCROLL) + active = GetKeyState(i) & 0x01; + else + active = GetAsyncKeyState(i) & 0x8000; + if (active) + { + lua_pushboolean(L, true); + lua_setfield(L, -2, name); + } + } + } + } + } + + // mouse position in game screen pixel coordinates + { + POINT mouse; + + int xofs = 0, yofs = 0, width = 240, height = 160; + if (!systemIsRunningGBA()) + { + if (gbBorderOn) + width = 256, height = 224, xofs = 48, yofs = 40; + else + width = 160, height = 144; + } + + GetCursorPos(&mouse); + AfxGetApp()->m_pMainWnd->ScreenToClient(&mouse); + + // game screen is always fully stretched to window size, + // with no aspect rate correction, or something like that. + RECT clientRect; + AfxGetApp()->m_pMainWnd->GetClientRect(&clientRect); + + int wndWidth = clientRect.right - clientRect.left; + int wndHeight = clientRect.bottom - clientRect.top; + mouse.x = (LONG) (mouse.x * ((float)width / wndWidth)) - xofs; + mouse.y = (LONG) (mouse.y * ((float)height / wndHeight)) - yofs; + + lua_pushinteger(L, mouse.x); + lua_setfield(L, -2, "xmouse"); + lua_pushinteger(L, mouse.y); + lua_setfield(L, -2, "ymouse"); + } + +#else + // NYI (well, return an empty table) +#endif + return 1; + } + + static int avi_framecount(lua_State *L) + { + #ifdef WIN32 + if (theApp.aviRecorder != NULL) + { + lua_pushinteger(L, theApp.aviRecorder->videoFrames()); + } + else + #endif + { + lua_pushinteger(L, 0); + } + return 1; + } + + static int avi_pause(lua_State *L) + { + #ifdef WIN32 + if (theApp.aviRecorder != NULL) + theApp.aviRecorder->Pause(true); + #endif + return 1; + } + + static int avi_resume(lua_State *L) + { + #ifdef WIN32 + if (theApp.aviRecorder != NULL) + theApp.aviRecorder->Pause(false); + #endif + return 1; + } + + static int sound_get(lua_State *L) + { + extern int32 soundLevel1; + extern int32 soundLevel2; + extern int32 soundBalance; + extern int32 soundMasterOn; + extern int32 soundVIN; + extern int32 sound1On; + extern int32 sound1EnvelopeVolume; + extern int32 sound2On; + extern int32 sound2EnvelopeVolume; + extern int32 sound3On; + extern int32 sound3OutputLevel; + extern int32 sound3Bank; + extern int32 sound3DataSize; + extern int32 sound3ForcedOutput; + extern int32 sound4On; + extern int32 sound4EnvelopeVolume; + extern u8 sound3WaveRam[0x20]; + + int freqReg; + double freq; + double leftvolscale; + double rightvolscale; + double panpot; + bool gba = systemIsRunningGBA(); + u8* gbMem = gba ? ioMem : gbMemory; + const int rNR10 = gba ? 0x60 : 0xff10; + const int rNR11 = gba ? 0x62 : 0xff11; + const int rNR12 = gba ? 0x63 : 0xff12; + const int rNR13 = gba ? 0x64 : 0xff13; + const int rNR14 = gba ? 0x65 : 0xff14; + const int rNR21 = gba ? 0x68 : 0xff16; + const int rNR22 = gba ? 0x69 : 0xff17; + const int rNR23 = gba ? 0x6c : 0xff18; + const int rNR24 = gba ? 0x6d : 0xff19; + const int rNR30 = gba ? 0x70 : 0xff1a; + const int rNR31 = gba ? 0x72 : 0xff1b; + const int rNR32 = gba ? 0x73 : 0xff1c; + const int rNR33 = gba ? 0x74 : 0xff1d; + const int rNR34 = gba ? 0x75 : 0xff1e; + const int rNR41 = gba ? 0x78 : 0xff20; + const int rNR42 = gba ? 0x79 : 0xff21; + const int rNR43 = gba ? 0x7c : 0xff22; + const int rNR44 = gba ? 0x7d : 0xff23; + const int rNR50 = gba ? 0x80 : 0xff24; + const int rNR51 = gba ? 0x81 : 0xff25; + const int rNR52 = gba ? 0x84 : 0xff26; + const int rWAVE_RAM = gba ? 0x90 : 0xff30; + + const int32 _soundVIN = 0x88; // gba ? 0x88 : soundVIN; + const bool soundVINLeft = ((_soundVIN & 0x80) != 0); + const bool soundVINRight = ((_soundVIN & 0x08) != 0); + + lua_newtable(L); + + // square1 + lua_newtable(L); + if(sound1On == 0 || soundMasterOn == 0) + { + lua_pushnumber(L, 0.0); + panpot = 0.5; + } + else + { + double envVolume = sound1EnvelopeVolume / 15.0; + if (soundVINLeft && (soundBalance & 0x10) != 0) + leftvolscale = ((soundLevel2 / 7.0) * envVolume); + else + leftvolscale = 0.0; + if (soundVINRight && (soundBalance & 0x01) != 0) + rightvolscale = ((soundLevel1 / 7.0) * envVolume); + else + rightvolscale = 0.0; + if ((leftvolscale + rightvolscale) != 0) + panpot = rightvolscale / (leftvolscale + rightvolscale); + else + panpot = 0.5; + lua_pushnumber(L, (leftvolscale + rightvolscale) / 2.0); + } + lua_setfield(L, -2, "volume"); + lua_pushnumber(L, panpot); + lua_setfield(L, -2, "panpot"); + freqReg = (((int)(gbMem[rNR14] & 7) << 8) | gbMem[rNR13]); + freq = 131072.0 / (2048 - freqReg); + lua_pushnumber(L, freq); + lua_setfield(L, -2, "frequency"); + lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69); + lua_setfield(L, -2, "midikey"); + lua_pushinteger(L, (gbMem[rNR11] & 0xC0) >> 6); + lua_setfield(L, -2, "duty"); + lua_newtable(L); + lua_pushinteger(L, freqReg); + lua_setfield(L, -2, "frequency"); + lua_setfield(L, -2, "regs"); + lua_setfield(L, -2, "square1"); + // square2 + lua_newtable(L); + if(sound2On == 0 || soundMasterOn == 0) + { + lua_pushnumber(L, 0.0); + panpot = 0.5; + } + else + { + double envVolume = sound2EnvelopeVolume / 15.0; + if (soundVINLeft && (soundBalance & 0x20) != 0) + leftvolscale = ((soundLevel2 / 7.0) * envVolume); + else + leftvolscale = 0.0; + if (soundVINRight && (soundBalance & 0x02) != 0) + rightvolscale = ((soundLevel1 / 7.0) * envVolume); + else + rightvolscale = 0.0; + if ((leftvolscale + rightvolscale) != 0) + panpot = rightvolscale / (leftvolscale + rightvolscale); + else + panpot = 0.5; + lua_pushnumber(L, (leftvolscale + rightvolscale) / 2.0); + } + lua_setfield(L, -2, "volume"); + lua_pushnumber(L, panpot); + lua_setfield(L, -2, "panpot"); + freqReg = (((int)(gbMem[rNR24] & 7) << 8) | gbMem[rNR23]); + freq = 131072.0 / (2048 - freqReg); + lua_pushnumber(L, freq); + lua_setfield(L, -2, "frequency"); + lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69); + lua_setfield(L, -2, "midikey"); + lua_pushinteger(L, (gbMem[rNR21] & 0xC0) >> 6); + lua_setfield(L, -2, "duty"); + lua_newtable(L); + lua_pushinteger(L, freqReg); + lua_setfield(L, -2, "frequency"); + lua_setfield(L, -2, "regs"); + lua_setfield(L, -2, "square2"); + // wavememory + lua_newtable(L); + if(sound3On == 0 || soundMasterOn == 0) + { + lua_pushnumber(L, 0.0); + panpot = 0.5; + } + else + { + double envVolume; + if (gba && sound3ForcedOutput != 0) + envVolume = 0.75; + else + { + double volTable[4] = { 0.0, 1.0, 0.5, 0.25 }; + envVolume = volTable[sound3OutputLevel & 3]; + } + + if (soundVINLeft && (soundBalance & 0x40) != 0) + leftvolscale = ((soundLevel2 / 7.0) * envVolume); + else + leftvolscale = 0.0; + if (soundVINRight && (soundBalance & 0x04) != 0) + rightvolscale = ((soundLevel1 / 7.0) * envVolume); + else + rightvolscale = 0.0; + if ((leftvolscale + rightvolscale) != 0) + panpot = rightvolscale / (leftvolscale + rightvolscale); + else + panpot = 0.5; + lua_pushnumber(L, (leftvolscale + rightvolscale) / 2.0); + } + lua_setfield(L, -2, "volume"); + lua_pushnumber(L, panpot); + lua_setfield(L, -2, "panpot"); + int waveMemSamples = 32; + if (gba) + { + lua_pushlstring(L, (const char *) &sound3WaveRam[sound3Bank * 0x10], sound3DataSize ? 0x20 : 0x10); + waveMemSamples = sound3DataSize ? 64 : 32; + } + else + { + lua_pushlstring(L, (const char *) &gbMem[rWAVE_RAM], 0x10); + } + lua_setfield(L, -2, "waveform"); + freqReg = (((int)(gbMem[rNR34] & 7) << 8) | gbMem[rNR33]); + freq = 2097152.0 / (waveMemSamples * (2048 - freqReg)); + lua_pushnumber(L, freq); + lua_setfield(L, -2, "frequency"); + lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69); + lua_setfield(L, -2, "midikey"); + lua_newtable(L); + lua_pushinteger(L, freqReg); + lua_setfield(L, -2, "frequency"); + lua_setfield(L, -2, "regs"); + lua_setfield(L, -2, "wavememory"); + // noise + lua_newtable(L); + if(sound4On == 0 || soundMasterOn == 0) + { + lua_pushnumber(L, 0.0); + panpot = 0.5; + } + else + { + double envVolume = sound4EnvelopeVolume / 15.0; + if (soundVINLeft && (soundBalance & 0x80) != 0) + leftvolscale = ((soundLevel2 / 7.0) * envVolume); + else + leftvolscale = 0.0; + if (soundVINRight && (soundBalance & 0x08) != 0) + rightvolscale = ((soundLevel1 / 7.0) * envVolume); + else + rightvolscale = 0.0; + if ((leftvolscale + rightvolscale) != 0) + panpot = rightvolscale / (leftvolscale + rightvolscale); + else + panpot = 0.5; + lua_pushnumber(L, (leftvolscale + rightvolscale) / 2.0); + } + lua_setfield(L, -2, "volume"); + lua_pushnumber(L, panpot); + lua_setfield(L, -2, "panpot"); + const int gbNoiseFreqTable[8] = { 1, 2, 4, 6, 8, 10, 12, 14 }; + freqReg = gbNoiseFreqTable[gbMem[rNR43] & 7] << (1 + (gbMem[rNR43] >> 4)); + lua_pushboolean(L, (gbMem[rNR43] & 8) != 0); + lua_setfield(L, -2, "short"); + freq = 1048576.0 / freqReg; + lua_pushnumber(L, freq); + lua_setfield(L, -2, "frequency"); + lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69); + lua_setfield(L, -2, "midikey"); + lua_newtable(L); + lua_pushinteger(L, freqReg); + lua_setfield(L, -2, "frequency"); + lua_setfield(L, -2, "regs"); + lua_setfield(L, -2, "noise"); + + return 1; + } + +// same as math.random, but uses SFMT instead of C rand() +// FIXME: this function doesn't care multi-instance, + +// original math.random either though (Lua 5.1) + static int sfmt_random(lua_State *L) + { + lua_Number r = (lua_Number) genrand_real2(); + switch (lua_gettop(L)) + { // check number of arguments + case 0: + { // no arguments + lua_pushnumber(L, r); // Number between 0 and 1 + break; + } + + case 1: + { // only upper limit + int u = luaL_checkint(L, 1); + luaL_argcheck(L, 1 <= u, 1, "interval is empty"); + lua_pushnumber(L, floor(r * u) + 1); // int between 1 and `u' + break; + } + + case 2: + { // lower and upper limits + int l = luaL_checkint(L, 1); + int u = luaL_checkint(L, 2); + luaL_argcheck(L, l <= u, 2, "interval is empty"); + lua_pushnumber(L, floor(r * (u - l + 1)) + l); // int between `l' and `u' + break; + } + + default: + return luaL_error(L, "wrong number of arguments"); + } + + return 1; + } + +// same as math.randomseed, but uses SFMT instead of C srand() +// FIXME: this function doesn't care multi-instance, + +// original math.randomseed either though (Lua 5.1) + static int sfmt_randomseed(lua_State *L) + { + init_gen_rand(luaL_checkint(L, 1)); + return 0; + } + +// the following bit operations are ported from LuaBitOp 1.0.1, +// because it can handle the sign bit (bit 31) correctly. + +/* +** Lua BitOp -- a bit operations library for Lua 5.1. +** http://bitop.luajit.org/ +** +** Copyright (C) 2008-2009 Mike Pall. All rights reserved. +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] +*/ + +#ifdef _MSC_VER +/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */ + typedef __int32 int32_t; + typedef unsigned __int32 uint32_t; + typedef unsigned __int64 uint64_t; +#else +#include +#endif + + typedef int32_t SBits; + typedef uint32_t UBits; + + typedef union + { + lua_Number n; +#ifdef LUA_NUMBER_DOUBLE + uint64_t b; +#else + UBits b; +#endif + } BitNum; + +/* Convert argument to bit type. */ + static UBits barg(lua_State *L, int idx) + { + BitNum bn; + UBits b; + bn.n = lua_tonumber(L, idx); +#if defined(LUA_NUMBER_DOUBLE) + bn.n += 6755399441055744.0; /* 2^52+2^51 */ +#ifdef SWAPPED_DOUBLE + b = (UBits)(bn.b >> 32); +#else + b = (UBits)(bn.b & 0xffffffff); +#endif +#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \ + defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \ + defined(LUA_NUMBER_LLONG) + if (sizeof(UBits) == sizeof(lua_Number)) + b = bn.b; + else + b = (UBits)(SBits)bn.n; +#elif defined(LUA_NUMBER_FLOAT) +#error "A 'float' lua_Number type is incompatible with this library" +#else +#error "Unknown number type, check LUA_NUMBER_* in luaconf.h" +#endif + if (b == 0 && !lua_isnumber(L, idx)) + luaL_typerror(L, idx, "number"); + return b; + } + +/* Return bit type. */ +#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; + + static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) } + static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) } + +#define BIT_OP(func, opr) \ + static int func(lua_State * L) { int i; UBits b = barg(L, 1); \ + for (i = lua_gettop(L); i > 1; i--) \ + b opr barg(L, i); BRET(b) } + BIT_OP(bit_band, &= ) + BIT_OP(bit_bor, |= ) + BIT_OP(bit_bxor, ^= ) + +#define bshl(b, n) (b << n) +#define bshr(b, n) (b >> n) +#define bsar(b, n) ((SBits)b >> n) +#define brol(b, n) ((b << n) | (b >> (32 - n))) +#define bror(b, n) ((b << (32 - n)) | (b >> n)) +#define BIT_SH(func, fn) \ + static int func(lua_State * L) { \ + UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) } + BIT_SH(bit_lshift, bshl) + BIT_SH(bit_rshift, bshr) + BIT_SH(bit_arshift, bsar) + BIT_SH(bit_rol, brol) + BIT_SH(bit_ror, bror) + + static int bit_bswap(lua_State *L) + { + UBits b = barg(L, 1); + b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24); + BRET(b) + } + + static int bit_tohex(lua_State *L) + { + UBits b = barg(L, 1); + SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2); + const char *hexdigits = "0123456789abcdef"; + char buf[8]; + int i; + if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } + if (n > 8) n = 8; + for (i = (int)n; --i >= 0; ) + { + buf[i] = hexdigits[b & 15]; b >>= 4; + } + lua_pushlstring(L, buf, (size_t)n); + return 1; + } + + static const struct luaL_Reg bit_funcs[] = { + { "tobit", bit_tobit }, + { "bnot", bit_bnot }, + { "band", bit_band }, + { "bor", bit_bor }, + { "bxor", bit_bxor }, + { "lshift", bit_lshift }, + { "rshift", bit_rshift }, + { "arshift", bit_arshift }, + { "rol", bit_rol }, + { "ror", bit_ror }, + { "bswap", bit_bswap }, + { "tohex", bit_tohex }, + { NULL, NULL } + }; + +/* Signed right-shifts are implementation-defined per C89/C99. +** But the de facto standard are arithmetic right-shifts on two's +** complement CPUs. This behaviour is required here, so test for it. +*/ +#define BAD_SAR (bsar(-8, 2) != (SBits) - 2) + + bool luabitop_validate(lua_State *L) // originally named as luaopen_bit + { + UBits b; + lua_pushnumber(L, (lua_Number)1437217655L); + b = barg(L, -1); + if (b != (UBits)1437217655L || BAD_SAR) /* Perform a simple self-test. */ + { + const char *msg = "compiled with incompatible luaconf.h"; +#ifdef LUA_NUMBER_DOUBLE +#ifdef WIN32 + if (b == (UBits)1610612736L) + msg = "use D3DCREATE_FPU_PRESERVE with DirectX"; +#endif + if (b == (UBits)1127743488L) + msg = "not compiled with SWAPPED_DOUBLE"; +#endif + if (BAD_SAR) + msg = "arithmetic right-shift broken"; + luaL_error(L, "bit library self-test failed (%s)", msg); + return false; + } + return true; + } + +// LuaBitOp ends here + + static int bit_bshift_emulua(lua_State *L) + { + int shift = luaL_checkinteger(L, 2); + if (shift < 0) + { + lua_pushinteger(L, -shift); + lua_replace(L, 2); + return bit_lshift(L); + } + else + return bit_rshift(L); + } + + static int bitbit(lua_State *L) + { + int rv = 0; + int numArgs = lua_gettop(L); + for (int i = 1; i <= numArgs; i++) + { + int where = luaL_checkinteger(L, i); + if (where >= 0 && where < 32) + rv |= (1 << where); + } + lua_settop(L, 0); + BRET(rv); + } + +// The function called periodically to ensure Lua doesn't run amok. + static void VBALuaHookFunction(lua_State *L, lua_Debug *dbg) + { + if (numTries-- == 0) + { + int kill = 0; + +#if (defined(WIN32) && !defined(SDL)) + // Uh oh + theApp.winCheckFullscreen(); + systemSoundClearBuffer(); + int ret = AfxGetApp()->m_pMainWnd->MessageBox( + "The Lua script running has been running a long time. It may have gone crazy. Kill it?\n\n(No = don't check anymore either)", + "Lua Script Gone Nuts?", + MB_YESNO); + + if (ret == IDYES) + { + kill = 1; + } + +#else + fprintf( + stderr, + "The Lua script running has been running a long time.\nIt may have gone crazy. Kill it? (I won't ask again if you say No)\n"); + + char buffer[64]; + while (true) + { + fprintf(stderr, "(y/n): "); + fgets(buffer, sizeof(buffer), stdin); + if (buffer[0] == 'y' || buffer[0] == 'Y') + { + kill = 1; + break; + } + + if (buffer[0] == 'n' || buffer[0] == 'N') + break; + } +#endif + if (kill) + { + luaL_error(L, "Killed by user request."); + VBALuaOnStop(); + } + + // else, kill the debug hook. + lua_sethook(L, NULL, 0, 0); + } + } + + static const struct luaL_reg vbalib[] = { + // {"speedmode", vba_speedmode}, // TODO: NYI + { "frameadvance", vba_frameadvance }, + { "pause", vba_pause }, + { "framecount", vba_framecount }, + { "lagcount", vba_getlagcount }, + { "lagged", vba_lagged }, + { "emulating", vba_emulating }, + { "registerbefore", vba_registerbefore }, + { "registerafter", vba_registerafter }, + { "registerexit", vba_registerexit }, + { "message", vba_message }, + { "print", print }, // sure, why not + { NULL, NULL } + }; + + static const struct luaL_reg memorylib[] = { + { "readbyte", memory_readbyte }, + { "readbytesigned", memory_readbytesigned }, + { "readword", memory_readword }, + { "readwordsigned", memory_readwordsigned }, + { "readdword", memory_readdword }, + { "readdwordsigned", memory_readdwordsigned }, + { "readbyterange", memory_readbyterange }, + { "writebyte", memory_writebyte }, + { "writeword", memory_writeword }, + { "writedword", memory_writedword }, + { "getregister", memory_getregister }, + { "setregister", memory_setregister }, + { "gbromreadbyte", memory_gbromreadbyte }, + { "gbromreadbytesigned", memory_gbromreadbytesigned }, + { "gbromreadword", memory_gbromreadword }, + { "gbromreadwordsigned", memory_gbromreadwordsigned }, + { "gbromreaddword", memory_gbromreaddword }, + { "gbromreaddwordsigned", memory_gbromreaddwordsigned }, + { "gbromreadbyterange", memory_gbromreadbyterange }, + + // alternate naming scheme for word and double-word and unsigned + { "readbyteunsigned", memory_readbyte }, + { "readwordunsigned", memory_readword }, + { "readdwordunsigned", memory_readdword }, + { "readshort", memory_readword }, + { "readshortunsigned", memory_readword }, + { "readshortsigned", memory_readwordsigned }, + { "readlong", memory_readdword }, + { "readlongunsigned", memory_readdword }, + { "readlongsigned", memory_readdwordsigned }, + { "writeshort", memory_writeword }, + { "writelong", memory_writedword }, + { "gbromreadbyteunsigned", memory_gbromreadbyte }, + { "gbromreadwordunsigned", memory_gbromreadword }, + { "gbromreaddwordunsigned", memory_gbromreaddword }, + { "gbromreadshort", memory_gbromreadword }, + { "gbromreadshortunsigned", memory_gbromreadword }, + { "gbromreadshortsigned", memory_gbromreadwordsigned }, + { "gbromreadlong", memory_gbromreaddword }, + { "gbromreadlongunsigned", memory_gbromreaddword }, + { "gbromreadlongsigned", memory_gbromreaddwordsigned }, + + // memory hooks + { "registerwrite", memory_registerwrite }, + //{"registerread", memory_registerread}, + { "registerexec", memory_registerexec }, + // alternate names + { "register", memory_registerwrite }, + { "registerrun", memory_registerexec }, + { "registerexecute", memory_registerexec }, + + { NULL, NULL } + }; + + static const struct luaL_reg joypadlib[] = { + { "get", joypad_get }, + { "getdown", joypad_getdown }, + { "getup", joypad_getup }, + { "set", joypad_set }, + + // alternative names + { "read", joypad_get }, + { "write", joypad_set }, + { "readdown", joypad_getdown }, + { "readup", joypad_getup }, + { NULL, NULL } + }; + + static const struct luaL_reg savestatelib[] = { + { "create", savestate_create }, + { "save", savestate_save }, + { "load", savestate_load }, + + { NULL, NULL } + }; + + static const struct luaL_reg movielib[] = { + { "active", movie_isactive }, + { "recording", movie_isrecording }, + { "playing", movie_isplaying }, + { "mode", movie_getmode }, + + { "length", movie_getlength }, + { "author", movie_getauthor }, + { "name", movie_getfilename }, + { "rerecordcount", movie_rerecordcount }, + { "setrerecordcount", movie_setrerecordcount }, + + { "rerecordcounting", movie_rerecordcounting }, + { "framecount", vba_framecount }, // for those familiar with + // other emulators that have + // movie.framecount() + // instead of + // emulatorname.framecount() + + { "stop", movie_stop }, + + // alternative names + { "close", movie_stop }, + { "getauthor", movie_getauthor }, + { "getname", movie_getfilename }, + { NULL, NULL } + }; + + static const struct luaL_reg guilib[] = { + { "register", gui_register }, + { "text", gui_text }, + { "box", gui_drawbox }, + { "line", gui_drawline }, + { "pixel", gui_drawpixel }, + { "opacity", gui_setopacity }, + { "transparency", gui_transparency }, + { "popup", gui_popup }, + { "parsecolor", gui_parsecolor }, + { "gdscreenshot", gui_gdscreenshot }, + { "gdoverlay", gui_gdoverlay }, + { "getpixel", gui_getpixel }, + + // alternative names + { "drawtext", gui_text }, + { "drawbox", gui_drawbox }, + { "drawline", gui_drawline }, + { "drawpixel", gui_drawpixel }, + { "setpixel", gui_drawpixel }, + { "writepixel", gui_drawpixel }, + { "rect", gui_drawbox }, + { "drawrect", gui_drawbox }, + { "drawimage", gui_gdoverlay }, + { "image", gui_gdoverlay }, + { "readpixel", gui_getpixel }, + { NULL, NULL } + }; + + static const struct luaL_reg inputlib[] = { + { "get", input_getcurrentinputstatus }, + + // alternative names + { "read", input_getcurrentinputstatus }, + { NULL, NULL } + }; + + static const struct luaL_reg soundlib[] = { + { "get", sound_get }, + + // alternative names + { NULL, NULL } + }; + +// gocha: since vba dumps avi so badly, +// I add avilib as a workaround for enhanced video encoding. + static const struct luaL_reg avilib[] = { + { "framecount", avi_framecount }, + { "pause", avi_pause }, + { "resume", avi_resume }, + { NULL, NULL } + }; + + void CallExitFunction(void) + { + if (!LUA) + return; + + lua_settop(LUA, 0); + lua_getfield(LUA, LUA_REGISTRYINDEX, luaCallIDStrings[LUACALL_BEFOREEXIT]); + + int errorcode = 0; + if (lua_isfunction(LUA, -1)) + { + errorcode = lua_pcall(LUA, 0, 0, 0); + } + + if (errorcode) + HandleCallbackError(LUA); + } + + void VBALuaFrameBoundary(void) + { + // printf("Lua Frame\n"); + + lua_joypads_used = 0; + + // HA! + if (!LUA || !luaRunning) + return; + + // Our function needs calling + lua_settop(LUA, 0); + lua_getfield(LUA, LUA_REGISTRYINDEX, frameAdvanceThread); + + lua_State *thread = lua_tothread(LUA, 1); + + // Lua calling C must know that we're busy inside a frame boundary + frameBoundary = true; + frameAdvanceWaiting = false; + + numTries = 1000; + + int result = lua_resume(thread, 0); + + if (result == LUA_YIELD) + { + // Okay, we're fine with that. + } + else if (result != 0) + { + // Done execution by bad causes + VBALuaOnStop(); + lua_pushnil(LUA); + lua_setfield(LUA, LUA_REGISTRYINDEX, frameAdvanceThread); + lua_pushnil(LUA); + lua_setfield(LUA, LUA_REGISTRYINDEX, guiCallbackTable); + + // Error? +//#if (defined(WIN32) && !defined(SDL)) +// info_print(info_uid, lua_tostring(thread, -1)); //Clear_Sound_Buffer(); +// AfxGetApp()->m_pMainWnd->MessageBox(lua_tostring(thread, -1), "Lua run error", MB_OK | MB_ICONSTOP); +//#else +// fprintf(stderr, "Lua thread bombed out: %s\n", lua_tostring(thread, -1)); +//#endif + printerror(thread, -1); + } + else + { + VBALuaOnStop(); + printf("Script died of natural causes.\n"); + } + + // Past here, VBA actually runs, so any Lua code is called mid-frame. We must + // not do anything too stupid, so let ourselves know. + frameBoundary = false; + + if (!frameAdvanceWaiting) + { + VBALuaOnStop(); + } + } + +/** + * Loads and runs the given Lua script. + * The emulator MUST be paused for this function to be + * called. Otherwise, all frame boundary assumptions go out the window. + * + * Returns true on success, false on failure. + */ + int VBALoadLuaCode(const char *filename) + { + static bool sfmtInitialized = false; + if (!sfmtInitialized) + { + init_gen_rand((unsigned)time(NULL)); + sfmtInitialized = true; + } + + if (filename != luaScriptName) + { + if (luaScriptName) + free(luaScriptName); + luaScriptName = strdup(filename); + } + + //stop any lua we might already have had running + VBALuaStop(); + + // Set current directory from filename (for dofile) + char dir[_MAX_PATH]; + char *slash, *backslash; + strcpy(dir, filename); + slash = strrchr(dir, '/'); + backslash = strrchr(dir, '\\'); + if (!slash || (backslash && backslash < slash)) + slash = backslash; + if (slash) + { + slash[1] = '\0'; // keep slash itself for some reasons + chdir(dir); + } + + if (!LUA) + { + LUA = lua_open(); + luaL_openlibs(LUA); + + luaL_register(LUA, "emu", vbalib); // added for better cross-emulator compatibility + luaL_register(LUA, "vba", vbalib); // kept for backward compatibility + luaL_register(LUA, "memory", memorylib); + luaL_register(LUA, "joypad", joypadlib); + luaL_register(LUA, "savestate", savestatelib); + luaL_register(LUA, "movie", movielib); + luaL_register(LUA, "gui", guilib); + luaL_register(LUA, "input", inputlib); + luaL_register(LUA, "sound", soundlib); + luaL_register(LUA, "bit", bit_funcs); // LuaBitOp library + luaL_register(LUA, "avi", avilib); // workaround for enhanced video encoding + lua_settop(LUA, 0); // clean the stack, because each call to luaL_register leaves a table on top + + // register a few utility functions outside of libraries (in the global namespace) + lua_register(LUA, "print", print); + lua_register(LUA, "tostring", tostring); + lua_register(LUA, "addressof", addressof); + lua_register(LUA, "copytable", copytable); + + // old bit operation functions + lua_register(LUA, "AND", bit_band); + lua_register(LUA, "OR", bit_bor); + lua_register(LUA, "XOR", bit_bxor); + lua_register(LUA, "SHIFT", bit_bshift_emulua); + lua_register(LUA, "BIT", bitbit); + + luabitop_validate(LUA); + + lua_pushstring(LUA, "math"); + lua_gettable(LUA, LUA_GLOBALSINDEX); + lua_pushcfunction(LUA, sfmt_random); + lua_setfield(LUA, -2, "random"); + lua_pushcfunction(LUA, sfmt_randomseed); + lua_setfield(LUA, -2, "randomseed"); + lua_settop(LUA, 0); + + // push arrays for storing hook functions in + for (int i = 0; i < LUAMEMHOOK_COUNT; i++) + { + lua_newtable(LUA); + lua_setfield(LUA, LUA_REGISTRYINDEX, luaMemHookTypeStrings[i]); + } + } + + // We make our thread NOW because we want it at the bottom of the stack. + // If all goes wrong, we let the garbage collector remove it. + lua_State *thread = lua_newthread(LUA); + + // Load the data + int result = luaL_loadfile(LUA, filename); + + if (result) + { +//#if (defined(WIN32) && !defined(SDL)) +// info_print(info_uid, lua_tostring(LUA, -1)); //Clear_Sound_Buffer(); +// AfxGetApp()->m_pMainWnd->MessageBox(lua_tostring(LUA, -1), "Lua load error", MB_OK | MB_ICONSTOP); +//#else +// fprintf(stderr, "Failed to compile file: %s\n", lua_tostring(LUA, -1)); +//#endif + printerror(LUA, -1); + + // Wipe the stack. Our thread + lua_settop(LUA, 0); + return 0; // Oh shit. + } + + // Get our function into it + lua_xmove(LUA, thread, 1); + + // Save the thread to the registry. This is why I make the thread FIRST. + lua_setfield(LUA, LUA_REGISTRYINDEX, frameAdvanceThread); + + // Initialize settings + luaRunning = true; + skipRerecords = false; + numMemHooks = 0; + transparencyModifier = 255; // opaque + lua_joypads_used = 0; // not used + //wasPaused = systemIsPaused(); + //systemSetPause(false); + + // Set up our protection hook to be executed once every 10,000 bytecode instructions. + lua_sethook(thread, VBALuaHookFunction, LUA_MASKCOUNT, 10000); + +#ifdef WIN32 + info_print = PrintToWindowConsole; + info_onstart = WinLuaOnStart; + info_onstop = WinLuaOnStop; + if (!LuaConsoleHWnd) + LuaConsoleHWnd = CreateDialog(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD_LUA), + AfxGetMainWnd()->GetSafeHwnd(), (DLGPROC) DlgLuaScriptDialog); + info_uid = (int)LuaConsoleHWnd; +#else + info_print = NULL; + info_onstart = NULL; + info_onstop = NULL; +#endif + if (info_onstart) + info_onstart(info_uid); + + // And run it right now. :) + VBALuaFrameBoundary(); + systemRenderFrame(); + + // We're done. + return 1; + } + +/** + * Equivalent to repeating the last VBALoadLuaCode() call. + */ + int VBAReloadLuaCode(void) + { + if (!luaScriptName) + { + systemScreenMessage("There's no script to reload."); + return 0; + } + else + return VBALoadLuaCode(luaScriptName); + } + +/** + * Terminates a running Lua script by killing the whole Lua engine. + * + * Always safe to call, except from within a lua call itself (duh). + * + */ + void VBALuaStop(void) + { + //already killed + if (!LUA) + return; + + //execute the user's shutdown callbacks + CallExitFunction(); + + /*info.*/ numMemHooks = 0; + for (int i = 0; i < LUAMEMHOOK_COUNT; i++) + CalculateMemHookRegions((LuaMemHookType)i); + + //sometimes iup uninitializes com + //MBG TODO - test whether this is really necessary. i dont think it is +#if (defined(WIN32) && !defined(SDL)) + CoInitialize(0); +#endif + + if (info_onstop) + info_onstop(info_uid); + + //lua_gc(LUA,LUA_GCCOLLECT,0); + lua_close(LUA); // this invokes our garbage collectors for us + LUA = NULL; + VBALuaOnStop(); + } + +/** + * Returns true if there is a Lua script running. + * + */ + int VBALuaRunning(void) + { + // FIXME: return false when no callback functions are registered. + return (int) (LUA != NULL); // should return true if callback functions are active. + } + +/** + * Returns true if Lua would like to steal the given joypad control. + * + * Range is 0 through 3 + */ + int VBALuaUsingJoypad(int which) + { + if (which < 0 || which > 3) + which = systemGetDefaultJoypad(); + return lua_joypads_used & (1 << which); + } + +/** + * Reads the buttons Lua is feeding for the given joypad, in the same + * format as the OS-specific code. + * + * This function must not be called more than once per frame. Ideally exactly once + * per frame (if VBALuaUsingJoypad says it's safe to do so) + */ + int VBALuaReadJoypad(int which) + { + if (which < 0 || which > 3) + which = systemGetDefaultJoypad(); + + //lua_joypads_used &= ~(1 << which); + return lua_joypads[which]; + } + +/** + * If this function returns true, the movie code should NOT increment + * the rerecord count for a load-state. + * + * This function will not return true if a script is not running. + */ + bool8 VBALuaRerecordCountSkip(void) + { + // FIXME: return true if (there are any active callback functions && skipRerecords) + return LUA && luaRunning && skipRerecords; + } + +/** + * Given a screen with the indicated resolution, + * draw the current GUI onto it. + */ + void VBALuaGui(uint8 *screen, int ppl, int width, int height) + { + if (!LUA /* || !luaRunning*/) + return; + + // First, check if we're being called by anybody + lua_getfield(LUA, LUA_REGISTRYINDEX, guiCallbackTable); + + if (lua_isfunction(LUA, -1)) + { + // We call it now + numTries = 1000; + + int ret = lua_pcall(LUA, 0, 0, 0); + if (ret != 0) + { + // This is grounds for trashing the function + // Note: This must be done before the messagebox pops up, + // otherwise the messagebox will cause a paint event which causes a weird + // infinite call sequence that makes Snes9x silently exit with error code 3, + // if a Lua GUI function crashes. (nitsuja) + lua_pushnil(LUA); + lua_setfield(LUA, LUA_REGISTRYINDEX, guiCallbackTable); + +//#if (defined(WIN32) && !defined(SDL)) +// info_print(info_uid, lua_tostring(LUA, -1)); //AfxGetApp()->m_pMainWnd->MessageBox(lua_tostring(LUA, -1), "Lua Error +// in GUI function", MB_OK); +//#else +// fprintf(stderr, "Lua error in gui.register function: %s\n", lua_tostring(LUA, -1)); +//#endif + printerror(LUA, -1); + } + } + + // And wreak the stack + lua_settop(LUA, 0); + + if (!gui_used) + return; + + gui_used = false; + + int x, y; + + //int pitch = (((ppl * systemColorDepth + 7)>>3)+3)&~3; + int pitch = ppl * (systemColorDepth / 8) + (systemColorDepth == 24 ? 0 : 4); + + if (width > LUA_SCREEN_WIDTH) + width = LUA_SCREEN_WIDTH; + if (height > LUA_SCREEN_HEIGHT) + height = LUA_SCREEN_HEIGHT; + + GetColorFunc getColor; + SetColorFunc setColor; + getColorIOFunc(systemColorDepth, &getColor, &setColor); + + for (y = 0; y < height; y++) + { + uint8 *scr = &screen[y * pitch]; + for (x = 0; x < width; x++, scr += systemColorDepth / 8) + { + const uint8 gui_alpha = gui_data[(y * LUA_SCREEN_WIDTH + x) * 4 + 3]; + if (gui_alpha == 0) + { + // do nothing + continue; + } + + const uint8 gui_red = gui_data[(y * LUA_SCREEN_WIDTH + x) * 4 + 2]; + const uint8 gui_green = gui_data[(y * LUA_SCREEN_WIDTH + x) * 4 + 1]; + const uint8 gui_blue = gui_data[(y * LUA_SCREEN_WIDTH + x) * 4]; + int red, green, blue; + + if (gui_alpha == 255) + { + // direct copy + red = gui_red; + green = gui_green; + blue = gui_blue; + } + else + { + // alpha-blending + uint8 scr_red, scr_green, scr_blue; + getColor(scr, &scr_red, &scr_green, &scr_blue); + red = (((int)gui_red - scr_red) * gui_alpha / 255 + scr_red) & 255; + green = (((int)gui_green - scr_green) * gui_alpha / 255 + scr_green) & 255; + blue = (((int)gui_blue - scr_blue) * gui_alpha / 255 + scr_blue) & 255; + } + + setColor(scr, (uint8) red, (uint8) green, (uint8) blue); + } + } + + return; + } + + void VBALuaClearGui(void) + { + gui_used = false; + } + + lua_State *VBAGetLuaState() + { + return LUA; + } + + char *VBAGetLuaScriptName() + { + return luaScriptName; + } + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/memgzio.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/memgzio.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,791 @@ +/* gzio.c -- IO on .gz files + * Copyright (C) 1995-2002 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + * + * Compile this file with -DNO_DEFLATE to avoid the compression code. + */ + +/* memgzio.c - IO on .gz files in memory + * Adapted from original gzio.c from zlib library by Forgotten + */ + +/* @(#) $Id: memgzio.c,v 1.5 2006/06/06 21:04:20 spacy51 Exp $ */ + +#include +#include +#include +#include +#include + +#include "memgzio.h" + +#ifndef local +#define local static +#endif + +#ifndef DEF_MEM_LEVEL +# define DEF_MEM_LEVEL 8 +#endif + +#ifndef OS_CODE +#define OS_CODE 3 +#endif + +#ifndef zmemcpy +#define zmemcpy memcpy +#endif + +/*struct internal_state {int dummy;};*/ /* for buggy compilers */ + +#ifndef Z_BUFSIZE +# ifdef MAXSEG_64K +# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */ +# else +# define Z_BUFSIZE 16384 +# endif +#endif +#ifndef Z_PRINTF_BUFSIZE +# define Z_PRINTF_BUFSIZE 4096 +#endif + +#define ALLOC(size) malloc(size) +#define TRYFREE(p) \ + {if (p) \ + free(p);} + +static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ + +/* gzip flag byte */ +#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ +#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */ +#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ +#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ +#define COMMENT 0x10 /* bit 4 set: file comment present */ +#define RESERVED 0xE0 /* bits 5..7: reserved */ + +typedef struct _MemFile +{ + char *memory; + char *next; + int available; + int error; + char mode; +} MEMFILE; + +typedef struct mem_stream +{ + z_stream stream; + int z_err; /* error code for last stream operation */ + int z_eof; /* set if end of input file */ + MEMFILE *file; /* memoru file */ + Byte * inbuf; /* input buffer */ + Byte * outbuf; /* output buffer */ + uLong crc; /* crc32 of uncompressed data */ + char * msg; /* error message */ + int transparent; /* 1 if input file is not a .gz file */ + char mode; /* 'w' or 'r' */ + long startpos; /* start of compressed data in file (header skipped) */ +} mem_stream; + +local gzFile gz_open OF((char *memory, const int available, const char *mode)); +local int do_flush OF((gzFile file, int flush)); +local int get_byte OF((mem_stream *s)); +local void check_header OF((mem_stream *s)); +local int destroy OF((mem_stream *s)); +local void putLong OF((MEMFILE *file, uLong x)); +local uLong getLong OF((mem_stream *s)); + +local MEMFILE *memOpen(char *memory, int available, char mode) +{ + MEMFILE *f; + + if (available <= 8) + return NULL; + + if (mode != 'w' && mode != 'r') + return NULL; + + f = (MEMFILE *)malloc(sizeof(MEMFILE)); + + f->memory = memory; + f->mode = mode; + f->error = 0; + + if (mode == 'w') + { + f->available = available - 8; + f->next = memory + 8; + memory[0] = 'V'; + memory[1] = 'B'; + memory[2] = 'A'; + memory[3] = ' '; + *((int *)(memory+4)) = 0; + } + else + { + if (memory[0] != 'V' || memory[1] != 'B' || memory[2] != 'A' || + memory[3] != ' ') + { + free(f); + return NULL; + } + f->available = *((int *)(memory+4)); + f->next = memory+8; + } + + return f; +} + +local size_t memWrite(const void *buffer, size_t size, size_t count, + MEMFILE *file) +{ + size_t total = size*count; + + if (file->mode != 'w') + { + file->error = 1; + return 0; + } + + if (total > (size_t)file->available) + { + total = file->available; + } + memcpy(file->next, buffer, total); + file->available -= (int)total; + file->next += total; + return total; +} + +local size_t memRead(void *buffer, size_t size, size_t count, + MEMFILE *file) +{ + size_t total = size*count; + + if (file->mode != 'r') + { + file->error = 1; + return 0; + } + + if (file->available == 0) + return -1; + + if (total > (size_t)file->available) + { + total = file->available; + } + memcpy(buffer, file->next, total); + file->available -= (int)total; + file->next += total; + return total; +} + +local int memPutc(int c, MEMFILE *file) +{ + if (file->mode != 'w') + { + file->error = 1; + return -1; + } + + if (file->available >= 1) + { + *file->next++ = c; + file->available--; + } + else + return -1; + + return c; +} + +local long memTell(MEMFILE *f) +{ + return (long)(f->next - f->memory) - 8; +} + +local int memError(MEMFILE *f) +{ + return f->error; +} + +local int memClose(MEMFILE *f) +{ + if (f->mode == 'w') + { + *((int *)(f->memory+4)) = memTell(f); + } + free(f); + return 0; +} + +local int memPrintf(MEMFILE *f, const char *format, ...) +{ + char buffer[80]; + va_list list; + int len; + + va_start(list, format); + len = vsprintf(buffer, format, list); + va_end(list); + + return (int)memWrite(buffer, 1, len, f); +} + +/* =========================================================================== + Opens a gzip (.gz) file for reading or writing. The mode parameter + is as in fopen ("rb" or "wb"). The file is given either by file descriptor + or path name (if fd == -1). + gz_open return NULL if the file could not be opened or if there was + insufficient memory to allocate the (de)compression state; errno + can be checked to distinguish the two cases (if errno is zero, the + zlib error is Z_MEM_ERROR). + */ +local gzFile gz_open(memory, available, mode) +char *memory; +const int available; +const char *mode; +{ + int err; + int level = Z_DEFAULT_COMPRESSION; /* compression level */ + int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */ + char * p = (char *)mode; + mem_stream *s; + char fmode[80]; /* copy of mode, without the compression level */ + char * m = fmode; + + s = (mem_stream *)ALLOC(sizeof(mem_stream)); + if (!s) + return Z_NULL; + + s->stream.zalloc = (alloc_func)0; + s->stream.zfree = (free_func)0; + s->stream.opaque = (voidpf)0; + s->stream.next_in = s->inbuf = Z_NULL; + s->stream.next_out = s->outbuf = Z_NULL; + s->stream.avail_in = s->stream.avail_out = 0; + s->z_err = Z_OK; + s->z_eof = 0; + s->crc = crc32(0L, Z_NULL, 0); + s->msg = NULL; + s->transparent = 0; + s->file = NULL; + + s->mode = '\0'; + do + { + if (*p == 'r') + s->mode = 'r'; + if (*p == 'w' || *p == 'a') + s->mode = 'w'; + if (*p >= '0' && *p <= '9') + { + level = *p - '0'; + } + else if (*p == 'f') + { + strategy = Z_FILTERED; + } + else if (*p == 'h') + { + strategy = Z_HUFFMAN_ONLY; + } + else + { + *m++ = *p; /* copy the mode */ + } + } + while (*p++ && m != fmode + sizeof(fmode)); + if (s->mode == '\0') + return destroy(s), (gzFile)Z_NULL; + + if (s->mode == 'w') + { +#ifdef NO_DEFLATE + err = Z_STREAM_ERROR; +#else + err = deflateInit2(&(s->stream), level, + Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy); + /* windowBits is passed < 0 to suppress zlib header */ + + s->stream.next_out = s->outbuf = (Byte *)ALLOC(Z_BUFSIZE); +#endif + if (err != Z_OK || s->outbuf == Z_NULL) + { + return destroy(s), (gzFile)Z_NULL; + } + } + else + { + s->stream.next_in = s->inbuf = (Byte *)ALLOC(Z_BUFSIZE); + + err = inflateInit2(&(s->stream), -MAX_WBITS); + /* windowBits is passed < 0 to tell that there is no zlib header. + * Note that in this case inflate *requires* an extra "dummy" byte + * after the compressed stream in order to complete decompression and + * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are + * present after the compressed stream. + */ + if (err != Z_OK || s->inbuf == Z_NULL) + { + return destroy(s), (gzFile)Z_NULL; + } + } + s->stream.avail_out = Z_BUFSIZE; + + errno = 0; + s->file = memOpen(memory, available, s->mode); + + if (s->file == NULL) + { + return destroy(s), (gzFile)Z_NULL; + } + + if (s->mode == 'w') + { + /* Write a very simple .gz header: + */ + memPrintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1], + Z_DEFLATED, 0 /*flags*/, 0, 0, 0, 0 /*time*/, 0 /*xflags*/, OS_CODE); + s->startpos = 10L; + /* We use 10L instead of ftell(s->file) to because ftell causes an + * fflush on some systems. This version of the library doesn't use + * startpos anyway in write mode, so this initialization is not + * necessary. + */ + } + else + { + check_header(s); /* skip the .gz header */ + s->startpos = (memTell(s->file) - s->stream.avail_in); + } + + return (gzFile)s; +} + +/* =========================================================================== + Opens a gzip (.gz) file for reading or writing. + */ +gzFile ZEXPORT memgzopen(memory, available, mode) +char *memory; +int available; +const char *mode; +{ + return gz_open(memory, available, mode); +} + +/* =========================================================================== + Read a byte from a mem_stream; update next_in and avail_in. Return EOF + for end of file. + IN assertion: the stream s has been sucessfully opened for reading. + */ +local int get_byte(s) +mem_stream *s; +{ + if (s->z_eof) + return EOF; + if (s->stream.avail_in == 0) + { + errno = 0; + s->stream.avail_in = (uInt)memRead(s->inbuf, 1, Z_BUFSIZE, s->file); + if (s->stream.avail_in == 0) + { + s->z_eof = 1; + if (memError(s->file)) + s->z_err = Z_ERRNO; + return EOF; + } + s->stream.next_in = s->inbuf; + } + s->stream.avail_in--; + return *(s->stream.next_in)++; +} + +/* =========================================================================== + Check the gzip header of a mem_stream opened for reading. Set the stream + mode to transparent if the gzip magic header is not present; set s->err + to Z_DATA_ERROR if the magic header is present but the rest of the header + is incorrect. + IN assertion: the stream s has already been created sucessfully; + s->stream.avail_in is zero for the first time, but may be non-zero + for concatenated .gz files. + */ +local void check_header(s) +mem_stream *s; +{ + int method; /* method byte */ + int flags; /* flags byte */ + uInt len; + int c; + + /* Check the gzip magic header */ + for (len = 0; len < 2; len++) + { + c = get_byte(s); + if (c != gz_magic[len]) + { + if (len != 0) + s->stream.avail_in++, s->stream.next_in--; + if (c != EOF) + { + s->stream.avail_in++, s->stream.next_in--; + s->transparent = 1; + } + s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END; + return; + } + } + method = get_byte(s); + flags = get_byte(s); + if (method != Z_DEFLATED || (flags & RESERVED) != 0) + { + s->z_err = Z_DATA_ERROR; + return; + } + + /* Discard time, xflags and OS code: */ + for (len = 0; len < 6; len++) + (void)get_byte(s); + + if ((flags & EXTRA_FIELD) != 0) /* skip the extra field */ + { + len = (uInt)get_byte(s); + len += ((uInt)get_byte(s))<<8; + /* len is garbage if EOF but the loop below will quit anyway */ + while (len-- != 0 && get_byte(s) != EOF) + ; + } + if ((flags & ORIG_NAME) != 0) /* skip the original file name */ + { + while ((c = get_byte(s)) != 0 && c != EOF) + ; + } + if ((flags & COMMENT) != 0) /* skip the .gz file comment */ + { + while ((c = get_byte(s)) != 0 && c != EOF) + ; + } + if ((flags & HEAD_CRC) != 0) /* skip the header crc */ + { + for (len = 0; len < 2; len++) + (void)get_byte(s); + } + s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK; +} + +/* =========================================================================== + * Cleanup then free the given mem_stream. Return a zlib error code. + Try freeing in the reverse order of allocations. + */ +local int destroy(s) +mem_stream *s; +{ + int err = Z_OK; + + if (!s) + return Z_STREAM_ERROR; + + TRYFREE(s->msg); + + if (s->stream.state != NULL) + { + if (s->mode == 'w') + { +#ifdef NO_DEFLATE + err = Z_STREAM_ERROR; +#else + err = deflateEnd(&(s->stream)); +#endif + } + else if (s->mode == 'r') + { + err = inflateEnd(&(s->stream)); + } + } + if (s->file != NULL && memClose(s->file)) + { +#ifdef ESPIPE + if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */ +#endif + err = Z_ERRNO; + } + if (s->z_err < 0) + err = s->z_err; + + TRYFREE(s->inbuf); + TRYFREE(s->outbuf); + TRYFREE(s); + return err; +} + +/* =========================================================================== + Reads the given number of uncompressed bytes from the compressed file. + gzread returns the number of bytes actually read (0 for end of file). + */ +int ZEXPORT memgzread(file, buf, len) +gzFile file; +voidp buf; +unsigned len; +{ + mem_stream *s = (mem_stream *)file; + Bytef * start = (Bytef *)buf; /* starting point for crc computation */ + Byte * next_out; /* == stream.next_out but not forced far (for MSDOS) */ + + if (s == NULL || s->mode != 'r') + return Z_STREAM_ERROR; + + if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) + return -1; + if (s->z_err == Z_STREAM_END) + return 0; /* EOF */ + + next_out = (Byte *)buf; + s->stream.next_out = (Bytef *)buf; + s->stream.avail_out = len; + + while (s->stream.avail_out != 0) + { + if (s->transparent) + { + /* Copy first the lookahead bytes: */ + uInt n = s->stream.avail_in; + if (n > s->stream.avail_out) + n = s->stream.avail_out; + if (n > 0) + { + zmemcpy(s->stream.next_out, s->stream.next_in, n); + next_out += n; + s->stream.next_out = next_out; + s->stream.next_in += n; + s->stream.avail_out -= n; + s->stream.avail_in -= n; + } + if (s->stream.avail_out > 0) + { + s->stream.avail_out -= (uInt)memRead(next_out, 1, s->stream.avail_out, s->file); + } + len -= s->stream.avail_out; + s->stream.total_in += (uLong)len; + s->stream.total_out += (uLong)len; + if (len == 0) + s->z_eof = 1; + return (int)len; + } + if (s->stream.avail_in == 0 && !s->z_eof) + { + errno = 0; + s->stream.avail_in = (uInt)memRead(s->inbuf, 1, Z_BUFSIZE, s->file); + if (s->stream.avail_in == 0) + { + s->z_eof = 1; + if (memError(s->file)) + { + s->z_err = Z_ERRNO; + break; + } + } + s->stream.next_in = s->inbuf; + } + s->z_err = inflate(&(s->stream), Z_NO_FLUSH); + + if (s->z_err == Z_STREAM_END) + { + /* Check CRC and original size */ + s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); + start = s->stream.next_out; + + if (getLong(s) != s->crc) + { + s->z_err = Z_DATA_ERROR; + } + else + { + (void)getLong(s); + /* The uncompressed length returned by above getlong() may + * be different from s->stream.total_out) in case of + * concatenated .gz files. Check for such files: + */ + check_header(s); + if (s->z_err == Z_OK) + { + uLong total_in = s->stream.total_in; + uLong total_out = s->stream.total_out; + + inflateReset(&(s->stream)); + s->stream.total_in = total_in; + s->stream.total_out = total_out; + s->crc = crc32(0L, Z_NULL, 0); + } + } + } + if (s->z_err != Z_OK || s->z_eof) + break; + } + s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); + + return (int)(len - s->stream.avail_out); +} + +#ifndef NO_DEFLATE +/* =========================================================================== + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of bytes actually written (0 in case of error). + */ +int ZEXPORT memgzwrite(file, buf, len) +gzFile file; +const voidp buf; +unsigned len; +{ + mem_stream *s = (mem_stream *)file; + + if (s == NULL || s->mode != 'w') + return Z_STREAM_ERROR; + + s->stream.next_in = (Bytef *)buf; + s->stream.avail_in = len; + + while (s->stream.avail_in != 0) + { + if (s->stream.avail_out == 0) + { + s->stream.next_out = s->outbuf; + if (memWrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) + { + s->z_err = Z_ERRNO; + break; + } + s->stream.avail_out = Z_BUFSIZE; + } + s->z_err = deflate(&(s->stream), Z_NO_FLUSH); + if (s->z_err != Z_OK) + break; + } + s->crc = crc32(s->crc, (const Bytef *)buf, len); + + return (int)(len - s->stream.avail_in); +} +#endif +/* =========================================================================== + Flushes all pending output into the compressed file. The parameter + flush is as in the deflate() function. + */ +local int do_flush(file, flush) +gzFile file; +int flush; +{ + uInt len; + int done = 0; + mem_stream *s = (mem_stream *)file; + + if (s == NULL || s->mode != 'w') + return Z_STREAM_ERROR; + + s->stream.avail_in = 0; /* should be zero already anyway */ + + for (;;) + { + len = Z_BUFSIZE - s->stream.avail_out; + + if (len != 0) + { + if ((uInt)memWrite(s->outbuf, 1, len, s->file) != len) + { + s->z_err = Z_ERRNO; + return Z_ERRNO; + } + s->stream.next_out = s->outbuf; + s->stream.avail_out = Z_BUFSIZE; + } + if (done) + break; + s->z_err = deflate(&(s->stream), flush); + + /* Ignore the second of two consecutive flushes: */ + if (len == 0 && s->z_err == Z_BUF_ERROR) + s->z_err = Z_OK; + + /* deflate has finished flushing only when it hasn't used up + * all the available space in the output buffer: + */ + done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END); + + if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) + break; + } + return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; +} + +/* =========================================================================== + Outputs a long in LSB order to the given file + */ +local void putLong(file, x) +MEMFILE *file; +uLong x; +{ + int n; + for (n = 0; n < 4; n++) + { + memPutc((int)(x & 0xff), file); + x >>= 8; + } +} + +/* =========================================================================== + Reads a long in LSB order from the given mem_stream. Sets z_err in case + of error. + */ +local uLong getLong(s) +mem_stream *s; +{ + uLong x = (uLong)get_byte(s); + int c; + + x += ((uLong)get_byte(s))<<8; + x += ((uLong)get_byte(s))<<16; + c = get_byte(s); + if (c == EOF) + s->z_err = Z_DATA_ERROR; + x += ((uLong)c)<<24; + return x; +} + +/* =========================================================================== + Flushes all pending output if necessary, closes the compressed file + and deallocates all the (de)compression state. + */ +int ZEXPORT memgzclose(file) +gzFile file; +{ + int err; + mem_stream *s = (mem_stream *)file; + + if (s == NULL) + return Z_STREAM_ERROR; + + if (s->mode == 'w') + { +#ifdef NO_DEFLATE + return Z_STREAM_ERROR; +#else + err = do_flush(file, Z_FINISH); + if (err != Z_OK) + return destroy((mem_stream *)file); + + putLong(s->file, s->crc); + putLong(s->file, s->stream.total_in); +#endif + } + return destroy((mem_stream *)file); +} + +long ZEXPORT memtell(file) +gzFile file; +{ + mem_stream *s = (mem_stream *)file; + + if (s == NULL) + return Z_STREAM_ERROR; + + return memTell(s->file); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/memgzio.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/memgzio.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,23 @@ +#ifndef MEMGZIO_H +#define MEMGZIO_H + +/* gzio.c -- IO on .gz files + * Copyright (C) 1995-2002 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + * + * Compile this file with -DNO_DEFLATE to avoid the compression code. + */ + +/* memgzio.c - IO on .gz files in memory + * Adapted from original gzio.c from zlib library by Forgotten + */ + +#include + +gzFile ZEXPORT memgzopen(char *memory, int available, const char *mode); +int ZEXPORT memgzread(gzFile file, voidp buf, unsigned len); +int ZEXPORT memgzwrite(gzFile file, const voidp buf, unsigned len); +int ZEXPORT memgzclose(gzFile file); +long ZEXPORT memtell(gzFile file); + +#endif // MEMGZIO_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/movie.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/movie.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1772 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#ifdef HAVE_STRINGS_H +# include +#endif + +#if defined(__unix) || defined(__linux) || defined(__sun) || defined(__DJGPP) +# include +# include +# include +# include +# define stricmp strcasecmp +// FIXME: this is wrong, but we don't want buffer overflow +# if defined _MAX_PATH +# undef _MAX_PATH +//# define _MAX_PATH 128 +# define _MAX_PATH 260 +# endif +#endif + +#ifdef WIN32 +# include +# ifndef W_OK +# define W_OK 2 +# endif +# define ftruncate chsize +#endif + +#include "movie.h" +#include "System.h" +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gba/RTC.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +#include "inputGlobal.h" +#include "unzip.h" +#include "Util.h" + +#include "vbalua.h" + +#if (defined(WIN32) && !defined(SDL)) +# include "../win32/stdafx.h" +# include "../win32/MainWnd.h" +# include "../win32/VBA.h" +# include "../win32/WinMiscUtil.h" +#endif + +extern int emulating; // from system.cpp +extern u16 currentButtons[4]; // from System.cpp +extern u16 lastKeys; + +SMovie Movie; +bool loadingMovie = false; + +// probably bad idea to have so many global variables, but I hate to recompile almost everything after editing VBA.h +bool autoConvertMovieWhenPlaying = false; + +static u16 initialInputs[4] = { 0 }; + +static bool resetSignaled = false; +static bool resetSignaledLast = false; + +static int prevEmulatorType, prevBorder, prevWinBorder, prevBorderAuto; + +// little-endian integer pop/push functions: +static inline uint32 Pop32(const uint8 * &ptr) +{ + uint32 v = (ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24)); + ptr += 4; + return v; +} + +static inline uint16 Pop16(const uint8 * &ptr) /* const version */ +{ + uint16 v = (ptr[0] | (ptr[1] << 8)); + ptr += 2; + return v; +} + +static inline uint16 Pop16(uint8 * &ptr) /* non-const version */ +{ + uint16 v = (ptr[0] | (ptr[1] << 8)); + ptr += 2; + return v; +} + +static inline uint8 Pop8(const uint8 * &ptr) +{ + return *(ptr)++; +} + +static inline void Push32(uint32 v, uint8 * &ptr) +{ + ptr[0] = (uint8)(v & 0xff); + ptr[1] = (uint8)((v >> 8) & 0xff); + ptr[2] = (uint8)((v >> 16) & 0xff); + ptr[3] = (uint8)((v >> 24) & 0xff); + ptr += 4; +} + +static inline void Push16(uint16 v, uint8 * &ptr) +{ + ptr[0] = (uint8)(v & 0xff); + ptr[1] = (uint8)((v >> 8) & 0xff); + ptr += 2; +} + +static inline void Push8(uint8 v, uint8 * &ptr) +{ + *ptr++ = v; +} + +// little-endian integer read/write functions: +static inline uint16 Read16(const uint8 *ptr) +{ + return ptr[0] | (ptr[1] << 8); +} + +static inline void Write16(uint16 v, uint8 *ptr) +{ + ptr[0] = uint8(v & 0xff); + ptr[1] = uint8((v >> 8) & 0xff); +} + +static long file_length(FILE *fp) +{ + long cur_pos = ftell(fp); + fseek(fp, 0, SEEK_END); + long length = ftell(fp); + fseek(fp, cur_pos, SEEK_SET); + return length; +} + +static int bytes_per_frame(SMovie &mov) +{ + int num_controllers = 0; + + for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i) + if (mov.header.controllerFlags & MOVIE_CONTROLLER(i)) + ++num_controllers; + + return CONTROLLER_DATA_SIZE * num_controllers; +} + +static void reserve_buffer_space(uint32 space_needed) +{ + if (space_needed > Movie.inputBufferSize) + { + uint32 ptr_offset = Movie.inputBufferPtr - Movie.inputBuffer; + uint32 alloc_chunks = (space_needed - 1) / BUFFER_GROWTH_SIZE + 1; + uint32 old_size = Movie.inputBufferSize; + Movie.inputBufferSize = BUFFER_GROWTH_SIZE * alloc_chunks; + Movie.inputBuffer = (uint8 *)realloc(Movie.inputBuffer, Movie.inputBufferSize); + // FIXME: this only fixes the random input problem during dma-frame-skip, but not the skip + memset(Movie.inputBuffer + old_size, 0, Movie.inputBufferSize - old_size); + Movie.inputBufferPtr = Movie.inputBuffer + ptr_offset; + } +} + +static int read_movie_header(FILE *file, SMovie &movie) +{ + assert(file != NULL); + assert(VBM_HEADER_SIZE == sizeof(SMovieFileHeader)); // sanity check on the header type definition + + uint8 headerData [VBM_HEADER_SIZE]; + + if (fread(headerData, 1, VBM_HEADER_SIZE, file) != VBM_HEADER_SIZE) + return MOVIE_WRONG_FORMAT; // if we failed to read in all VBM_HEADER_SIZE bytes of the header + + const uint8 * ptr = headerData; + SMovieFileHeader &header = movie.header; + + header.magic = Pop32(ptr); + if (header.magic != VBM_MAGIC) + return MOVIE_WRONG_FORMAT; + + header.version = Pop32(ptr); + if (header.version != VBM_VERSION) + return MOVIE_WRONG_VERSION; + + header.uid = Pop32(ptr); + header.length_frames = Pop32(ptr) + 1; // HACK: add 1 to the length for compatibility + header.rerecord_count = Pop32(ptr); + + header.startFlags = Pop8(ptr); + header.controllerFlags = Pop8(ptr); + header.typeFlags = Pop8(ptr); + header.optionFlags = Pop8(ptr); + + header.saveType = Pop32(ptr); + header.flashSize = Pop32(ptr); + header.gbEmulatorType = Pop32(ptr); + + for (int i = 0; i < 12; i++) + header.romTitle[i] = Pop8(ptr); + + header.minorVersion = Pop8(ptr); + + header.romCRC = Pop8(ptr); + header.romOrBiosChecksum = Pop16(ptr); + header.romGameCode = Pop32(ptr); + + header.offset_to_savestate = Pop32(ptr); + header.offset_to_controller_data = Pop32(ptr); + + return MOVIE_SUCCESS; +} + +static void write_movie_header(FILE *file, const SMovie &movie) +{ + assert(ftell(file) == 0); // we assume file points to beginning of movie file + + uint8 headerData [VBM_HEADER_SIZE]; + uint8 *ptr = headerData; + const SMovieFileHeader &header = movie.header; + + Push32(header.magic, ptr); + Push32(header.version, ptr); + + Push32(header.uid, ptr); + Push32(header.length_frames - 1, ptr); // HACK: reduce the length by 1 for compatibility with certain faulty old tools + // like TME + Push32(header.rerecord_count, ptr); + + Push8(header.startFlags, ptr); + Push8(header.controllerFlags, ptr); + Push8(header.typeFlags, ptr); + Push8(header.optionFlags, ptr); + + Push32(header.saveType, ptr); + Push32(header.flashSize, ptr); + Push32(header.gbEmulatorType, ptr); + + for (int i = 0; i < 12; ++i) + Push8(header.romTitle[i], ptr); + + Push8(header.minorVersion, ptr); + + Push8(header.romCRC, ptr); + Push16(header.romOrBiosChecksum, ptr); + Push32(header.romGameCode, ptr); + + Push32(header.offset_to_savestate, ptr); + Push32(header.offset_to_controller_data, ptr); + + fwrite(headerData, 1, VBM_HEADER_SIZE, file); +} + +static void flush_movie_header() +{ + assert(Movie.file != 0 && "logical error!"); + if (!Movie.file) + return; + + long originalPos = ftell(Movie.file); + + // (over-)write the header + fseek(Movie.file, 0, SEEK_SET); + write_movie_header(Movie.file, Movie); + + fflush(Movie.file); + + fseek(Movie.file, originalPos, SEEK_SET); +} + +static void flush_movie_frames() +{ + assert(Movie.file && "logical error!"); + if (!Movie.file) + return; + + long originalPos = ftell(Movie.file); + + // overwrite the controller data + fseek(Movie.file, Movie.header.offset_to_controller_data, SEEK_SET); + fwrite(Movie.inputBuffer, 1, Movie.bytesPerFrame * Movie.header.length_frames, Movie.file); + + fflush(Movie.file); + + fseek(Movie.file, originalPos, SEEK_SET); +} + +static void truncate_movie(long length) +{ + // truncate movie to length + // NOTE: it's certain that the savestate block is never after the + // controller data block, because the VBM format decrees it. + + assert(Movie.file && length >= 0); + if (!Movie.file || length < 0) + return; + + assert(Movie.header.offset_to_savestate <= Movie.header.offset_to_controller_data); + if (Movie.header.offset_to_savestate > Movie.header.offset_to_controller_data) + return; + + Movie.header.length_frames = length; + flush_movie_header(); + const long truncLen = long(Movie.header.offset_to_controller_data + Movie.bytesPerFrame * length); + if (file_length(Movie.file) != truncLen) + { + ftruncate(fileno(Movie.file), truncLen); + } +} + +static void remember_input_state() +{ + for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i) + { + if (systemCartridgeType == 0) + { + initialInputs[i] = u16(~P1 & 0x03FF); + } + else + { + extern int32 gbJoymask[4]; + for (int i = 0; i < 4; ++i) + initialInputs[i] = u16(gbJoymask[i] & 0xFFFF); + } + } +} + +static void change_state(MovieState new_state) +{ +#if (defined(WIN32) && !defined(SDL)) + theApp.frameSearching = false; + theApp.frameSearchSkipping = false; +#endif + + if (new_state == MOVIE_STATE_NONE) + { + Movie.pauseFrame = -1; + + if (Movie.state == MOVIE_STATE_NONE) + return; + + truncate_movie(Movie.header.length_frames); + + fclose(Movie.file); + Movie.file = NULL; + Movie.currentFrame = 0; +#if (defined(WIN32) && !defined(SDL)) + // undo changes to border settings + { + gbBorderOn = prevBorder; + theApp.winGbBorderOn = prevWinBorder; + gbBorderAutomatic = prevBorderAuto; + systemGbBorderOn(); + } +#endif + gbEmulatorType = prevEmulatorType; + + extern int32 gbDMASpeedVersion; + gbDMASpeedVersion = 1; + + extern int32 gbEchoRAMFixOn; + gbEchoRAMFixOn = 1; + + gbNullInputHackTempEnabled = gbNullInputHackEnabled; + + if (Movie.inputBuffer) + { + free(Movie.inputBuffer); + Movie.inputBuffer = NULL; + } + } + else if (new_state == MOVIE_STATE_PLAY) + { + assert(Movie.file); + + // this would cause problems if not dealt with + if (Movie.currentFrame >= Movie.header.length_frames) + { + new_state = MOVIE_STATE_END; + Movie.inputBufferPtr = Movie.inputBuffer + Movie.bytesPerFrame * Movie.header.length_frames; + } + } + else if (new_state == MOVIE_STATE_RECORD) + { + assert(Movie.file); + + // this would cause problems if not dealt with + if (Movie.currentFrame > Movie.header.length_frames) + { + new_state = MOVIE_STATE_END; + Movie.inputBufferPtr = Movie.inputBuffer + Movie.bytesPerFrame * Movie.header.length_frames; + } + + fseek(Movie.file, Movie.header.offset_to_controller_data + Movie.bytesPerFrame * Movie.currentFrame, SEEK_SET); + } + + if (new_state == MOVIE_STATE_END && Movie.state != MOVIE_STATE_END) + { +#if defined(SDL) + systemClearJoypads(); +#endif + systemScreenMessage("Movie end"); + } + + Movie.state = new_state; + + // checking for movie end + bool willPause = false; + + // if the movie's been set to pause at a certain frame + if (Movie.state != MOVIE_STATE_NONE && Movie.pauseFrame >= 0 && Movie.currentFrame == (uint32)Movie.pauseFrame) + { + Movie.pauseFrame = -1; + willPause = true; + } + + if (Movie.state == MOVIE_STATE_END) + { + if (Movie.currentFrame == Movie.header.length_frames) + { +#if (defined(WIN32) && !defined(SDL)) + if (theApp.movieOnEndPause) + { + willPause = true; + } +#else + // SDL FIXME +#endif + +#if (defined(WIN32) && !defined(SDL)) + switch (theApp.movieOnEndBehavior) + { + case 1: + // the old behavior + //VBAMovieRestart(); + break; + case 2: +#else + // SDL FIXME +#endif + if (Movie.RecordedThisSession) + { + // if user has been recording this movie since the last time it started playing, + // they probably don't want the movie to end now during playback, + // so switch back to recording when it reaches the end + VBAMovieSwitchToRecording(); + systemScreenMessage("Recording resumed"); + willPause = true; + } +#if (defined(WIN32) && !defined(SDL)) + break; + case 3: + // keep open + break; + case 0: + // fall through + default: + // close movie + //VBAMovieStop(false); + break; + } +#else + // SDL FIXME +#endif + } +#if 1 + else if (Movie.currentFrame > Movie.header.length_frames) + { +#if (defined(WIN32) && !defined(SDL)) + switch (theApp.movieOnEndBehavior) + { + case 1: + // FIXME: this should be delayed till the current frame ends + VBAMovieRestart(); + break; + case 2: + // nothing + break; + case 3: + // keep open + break; + case 0: + // fall through + default: + // close movie + VBAMovieStop(false); + break; + } +#else + // SDLFIXME +#endif + } +#endif + } // end if (Movie.state == MOVIE_STATE_END) + + if (willPause) + { + systemSetPause(true); + } +} + +void VBAMovieInit() +{ + memset(&Movie, 0, sizeof(Movie)); + Movie.state = MOVIE_STATE_NONE; + Movie.pauseFrame = -1; + + resetSignaled = false; + resetSignaledLast = false; +} + +void VBAMovieGetRomInfo(const SMovie &movieInfo, char romTitle [12], uint32 &romGameCode, uint16 &checksum, uint8 &crc) +{ + if (systemCartridgeType == 0) // GBA + { + extern u8 *bios, *rom; + memcpy(romTitle, &rom[0xa0], 12); // GBA TITLE + memcpy(&romGameCode, &rom[0xac], 4); // GBA ROM GAME CODE + if ((movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0) + checksum = utilCalcBIOSChecksum(bios, 4); // GBA BIOS CHECKSUM + else + checksum = 0; + crc = rom[0xbd]; // GBA ROM CRC + } + else // non-GBA + { + extern u8 *gbRom; + memcpy(romTitle, &gbRom[0x134], 12); // GB TITLE (note this can be 15 but is truncated to 12) + romGameCode = (uint32)gbRom[0x146]; // GB ROM UNIT CODE + + checksum = (gbRom[0x14e] << 8) | gbRom[0x14f]; // GB ROM CHECKSUM, read from big-endian + crc = gbRom[0x14d]; // GB ROM CRC + } +} + +#ifdef SDL +static void GetBatterySaveName(char *buffer) +{ + extern char batteryDir[2048], filename[2048]; // from SDL.cpp + extern char *sdlGetFilename(char *name); // from SDL.cpp + if (batteryDir[0]) + sprintf(buffer, "%s/%s.sav", batteryDir, sdlGetFilename(filename)); + else + sprintf(buffer, "%s.sav", filename); +} + +#endif + +static void SetPlayEmuSettings() +{ + prevEmulatorType = gbEmulatorType; + gbEmulatorType = Movie.header.gbEmulatorType; + +#if (defined(WIN32) && !defined(SDL)) +// theApp.removeIntros = false; + theApp.skipBiosFile = (Movie.header.optionFlags & MOVIE_SETTING_SKIPBIOSFILE) != 0; + theApp.useBiosFile = (Movie.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0; +#else + extern int saveType, sdlRtcEnable, sdlFlashSize; // from SDL.cpp + extern bool8 useBios, skipBios, removeIntros; // from SDL.cpp + useBios = (Movie.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0; + skipBios = (Movie.header.optionFlags & MOVIE_SETTING_SKIPBIOSFILE) != 0; + removeIntros = false /*(Movie.header.optionFlags & MOVIE_SETTING_REMOVEINTROS) != 0*/; +#endif + + extern void SetPrefetchHack(bool); + if (systemCartridgeType == 0) // lag disablement applies only to GBA + SetPrefetchHack((Movie.header.optionFlags & MOVIE_SETTING_LAGHACK) != 0); + + gbNullInputHackTempEnabled = ((Movie.header.optionFlags & MOVIE_SETTING_GBINPUTHACK) != 0); + + // some GB/GBC games depend on the sound rate, so just use the highest one + systemSoundSetQuality(1); + useOldFrameTiming = false; + + extern int32 gbDMASpeedVersion; + if ((Movie.header.optionFlags & MOVIE_SETTING_GBCFF55FIX) != 0) + gbDMASpeedVersion = 1; + else + gbDMASpeedVersion = 0; // old CGB HDMA5 timing was used + + extern int32 gbEchoRAMFixOn; + if ((Movie.header.optionFlags & MOVIE_SETTING_GBECHORAMFIX) != 0) + gbEchoRAMFixOn = 1; + else + gbEchoRAMFixOn = 0; + +#if (defined(WIN32) && !defined(SDL)) + rtcEnable((Movie.header.optionFlags & MOVIE_SETTING_RTCENABLE) != 0); + theApp.winSaveType = Movie.header.saveType; + theApp.winFlashSize = Movie.header.flashSize; + + prevBorder = gbBorderOn; + prevWinBorder = theApp.winGbBorderOn; + prevBorderAuto = gbBorderAutomatic; + if ((gbEmulatorType == 2 || gbEmulatorType == 5) + && !theApp.hideMovieBorder) // games played in SGB mode can have a border + { + gbBorderOn = true; + theApp.winGbBorderOn = true; + gbBorderAutomatic = false; + } + else + { + gbBorderOn = false; + theApp.winGbBorderOn = false; + gbBorderAutomatic = false; + if (theApp.hideMovieBorder) + { + theApp.hideMovieBorder = false; + prevBorder = false; // it might be expected behaviour that it stays hidden after the movie + } + } + systemGbBorderOn(); +#else + sdlRtcEnable = (Movie.header.optionFlags & MOVIE_SETTING_RTCENABLE) != 0; + saveType = Movie.header.saveType; + sdlFlashSize = Movie.header.flashSize; +#endif +} + +static void HardResetAndSRAMClear() +{ +#if (defined(WIN32) && !defined(SDL)) + winEraseBatteryFile(); // delete the damn SRAM file and keep it from being resurrected from RAM + MainWnd *temp = ((MainWnd *)theApp.m_pMainWnd); + if (!temp->winFileRun(true)) // restart running the game + { + temp->winFileClose(); + } +#else + char fname [1024]; + GetBatterySaveName(fname); + remove(fname); // delete the damn SRAM file + + // Henceforth, emuCleanUp means "clear out SRAM" + //theEmulator.emuCleanUp(); // keep it from being resurrected from RAM <--This is wrong, it'll deallocate all variables --Felipe + + /// FIXME the correct SDL code to call for a full restart isn't in a function yet + theEmulator.emuReset(false); +#endif +} + +int VBAMovieOpen(const char *filename, bool8 read_only) +{ + loadingMovie = true; + uint8 movieReadOnly = read_only ? 1 : 0; + + FILE * file; + STREAM stream; + int result; + int fn; + + char movie_filename[_MAX_PATH]; +#ifdef WIN32 + _fullpath(movie_filename, filename, _MAX_PATH); +#else + // SDL FIXME: convert to fullpath + strncpy(movie_filename, filename, _MAX_PATH); + movie_filename[_MAX_PATH - 1] = '\0'; +#endif + + if (movie_filename[0] == '\0') + { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; } + + if (!emulating) + { loadingMovie = false; return MOVIE_UNKNOWN_ERROR; } + +// bool alreadyOpen = (Movie.file != NULL && _stricmp(movie_filename, Movie.filename) == 0); + +// if (alreadyOpen) + change_state(MOVIE_STATE_NONE); // have to stop current movie before trying to re-open it + + if (!(file = fopen(movie_filename, "rb+"))) + if (!(file = fopen(movie_filename, "rb"))) + { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; } + //else + // movieReadOnly = 2; // we have to open the movie twice, no need to do this both times + +// if (!alreadyOpen) +// change_state(MOVIE_STATE_NONE); // stop current movie when we're able to open the other one +// +// if (!(file = fopen(movie_filename, "rb+"))) +// if(!(file = fopen(movie_filename, "rb"))) +// {loadingMovie = false; return MOVIE_FILE_NOT_FOUND;} +// else +// movieReadOnly = 2; + + // clear out the current movie + VBAMovieInit(); + + // read header + if ((result = read_movie_header(file, Movie)) != MOVIE_SUCCESS) + { + fclose(file); + { loadingMovie = false; return result; } + } + + // set emulator settings that make the movie more likely to stay synchronized + SetPlayEmuSettings(); + +// extern bool systemLoadBIOS(); +// if (!systemLoadBIOS()) +// { loadingMovie = false; return MOVIE_UNKNOWN_ERROR; } + + // read the metadata / author info from file + fread(Movie.authorInfo, 1, MOVIE_METADATA_SIZE, file); + fn = dup(fileno(file)); // XXX: why does this fail?? it returns -1 but errno == 0 + fclose(file); + + // apparently this lseek is necessary + lseek(fn, Movie.header.offset_to_savestate, SEEK_SET); + if (!(stream = utilGzReopen(fn, "rb"))) + if (!(stream = utilGzOpen(movie_filename, "rb"))) + { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; } + else + fn = dup(fileno(file)); + // in case the above dup failed but opening the file normally doesn't fail + + if (Movie.header.startFlags & MOVIE_START_FROM_SNAPSHOT) + { + // load the snapshot + result = theEmulator.emuReadStateFromStream(stream) ? MOVIE_SUCCESS : MOVIE_WRONG_FORMAT; + + // FIXME: Kludge for conversion + remember_input_state(); + } + else if (Movie.header.startFlags & MOVIE_START_FROM_SRAM) + { + // 'soft' reset: + theEmulator.emuReset(false); + + // load the SRAM + result = theEmulator.emuReadBatteryFromStream(stream) ? MOVIE_SUCCESS : MOVIE_WRONG_FORMAT; + } + else + { + HardResetAndSRAMClear(); + } + + utilGzClose(stream); + + if (result != MOVIE_SUCCESS) + { loadingMovie = false; return result; } + +// if (!(file = fopen(movie_filename, /*read_only ? "rb" :*/ "rb+"))) // want to be able to switch out of read-only later +// { +// if(!Movie.readOnly || !(file = fopen(movie_filename, "rb"))) // try read-only if failed +// return MOVIE_FILE_NOT_FOUND; +// } + if (!(file = fopen(movie_filename, "rb+"))) + if (!(file = fopen(movie_filename, "rb"))) + { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; } + else + movieReadOnly = 2; + + // recalculate length of movie from the file size + Movie.bytesPerFrame = bytes_per_frame(Movie); + fseek(file, 0, SEEK_END); + long fileSize = ftell(file); + Movie.header.length_frames = (fileSize - Movie.header.offset_to_controller_data) / Movie.bytesPerFrame; + + if (fseek(file, Movie.header.offset_to_controller_data, SEEK_SET)) + { fclose(file); loadingMovie = false; return MOVIE_WRONG_FORMAT; } + + strcpy(Movie.filename, movie_filename); + Movie.file = file; + Movie.inputBufferPtr = Movie.inputBuffer; + Movie.currentFrame = 0; + Movie.readOnly = movieReadOnly; + Movie.RecordedThisSession = false; + + // read controller data + uint32 to_read = Movie.bytesPerFrame * Movie.header.length_frames; + reserve_buffer_space(to_read); + fread(Movie.inputBuffer, 1, to_read, file); + + change_state(MOVIE_STATE_PLAY); + + char messageString[64] = "Movie "; + bool converted = false; + if (autoConvertMovieWhenPlaying) + { + int result = VBAMovieConvertCurrent(); + if (result == MOVIE_SUCCESS) + strcat(messageString, "converted and "); + else if (result == MOVIE_WRONG_VERSION) + strcat(messageString, "higher revision "); + } + + if (Movie.state == MOVIE_STATE_PLAY) + strcat(messageString, "replaying "); + else + strcat(messageString, "finished "); + if (Movie.readOnly) + strcat(messageString, "(read)"); + else + strcat(messageString, "(edit)"); + systemScreenMessage(messageString); + + VBAUpdateButtonPressDisplay(); + VBAUpdateFrameCountDisplay(); + systemRefreshScreen(); + + { loadingMovie = false; return MOVIE_SUCCESS; } +} + +static void SetRecordEmuSettings() +{ + Movie.header.optionFlags = 0; +#if (defined(WIN32) && !defined(SDL)) + if (theApp.useBiosFile) + Movie.header.optionFlags |= MOVIE_SETTING_USEBIOSFILE; + if (theApp.skipBiosFile) + Movie.header.optionFlags |= MOVIE_SETTING_SKIPBIOSFILE; + if (rtcIsEnabled()) + Movie.header.optionFlags |= MOVIE_SETTING_RTCENABLE; + Movie.header.saveType = theApp.winSaveType; + Movie.header.flashSize = theApp.winFlashSize; +#else + extern int saveType, sdlRtcEnable, sdlFlashSize; // from SDL.cpp + extern bool8 useBios, skipBios; // from SDL.cpp + if (useBios) + Movie.header.optionFlags |= MOVIE_SETTING_USEBIOSFILE; + if (skipBios) + Movie.header.optionFlags |= MOVIE_SETTING_SKIPBIOSFILE; + if (sdlRtcEnable) + Movie.header.optionFlags |= MOVIE_SETTING_RTCENABLE; + Movie.header.saveType = saveType; + Movie.header.flashSize = sdlFlashSize; +#endif + prevEmulatorType = Movie.header.gbEmulatorType = gbEmulatorType; + + if (!memLagTempEnabled) + Movie.header.optionFlags |= MOVIE_SETTING_LAGHACK; + + if (gbNullInputHackTempEnabled) + Movie.header.optionFlags |= MOVIE_SETTING_GBINPUTHACK; + + Movie.header.optionFlags |= MOVIE_SETTING_GBCFF55FIX; + extern int32 gbDMASpeedVersion; + gbDMASpeedVersion = 1; + + Movie.header.optionFlags |= MOVIE_SETTING_GBECHORAMFIX; + extern int32 gbEchoRAMFixOn; + gbEchoRAMFixOn = 1; + + // some GB/GBC games depend on the sound rate, so just use the highest one + systemSoundSetQuality(1); + + useOldFrameTiming = false; + +#if (defined(WIN32) && !defined(SDL)) +// theApp.removeIntros = false; + + prevBorder = gbBorderOn; + prevWinBorder = theApp.winGbBorderOn; + prevBorderAuto = gbBorderAutomatic; + if (gbEmulatorType == 2 || gbEmulatorType == 5) // only games played in SGB mode will have a border + { + gbBorderOn = true; + theApp.winGbBorderOn = true; + gbBorderAutomatic = false; + } + else + { + gbBorderOn = false; + theApp.winGbBorderOn = false; + gbBorderAutomatic = false; + } + systemGbBorderOn(); +#else + /// SDLFIXME +#endif +} + +uint16 VBAMovieGetCurrentInputOf(int controllerNum, bool normalOnly) +{ + if (controllerNum < 0 || controllerNum >= MOVIE_NUM_OF_POSSIBLE_CONTROLLERS) + return 0; + + return normalOnly ? (currentButtons[controllerNum] & BUTTON_REGULAR_MASK) : currentButtons[controllerNum]; +} + +int VBAMovieCreate(const char *filename, const char *authorInfo, uint8 startFlags, uint8 controllerFlags, uint8 typeFlags) +{ + // make sure at least one controller is enabled + if ((controllerFlags & MOVIE_CONTROLLERS_ANY_MASK) == 0) + return MOVIE_WRONG_FORMAT; + + if (!emulating) + return MOVIE_UNKNOWN_ERROR; + + loadingMovie = true; + + FILE * file; + STREAM stream; + int fn; + + char movie_filename [_MAX_PATH]; +#ifdef WIN32 + _fullpath(movie_filename, filename, _MAX_PATH); +#else + // FIXME: convert to fullpath + strncpy(movie_filename, filename, _MAX_PATH); + movie_filename[_MAX_PATH - 1] = '\0'; +#endif + + bool alreadyOpen = (Movie.file != NULL && stricmp(movie_filename, Movie.filename) == 0); + + if (alreadyOpen) + change_state(MOVIE_STATE_NONE); // have to stop current movie before trying to re-open it + + if (movie_filename[0] == '\0') + { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; } + + if (!(file = fopen(movie_filename, "wb"))) + { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; } + + if (!alreadyOpen) + change_state(MOVIE_STATE_NONE); // stop current movie when we're able to open the other one + + // clear out the current movie + VBAMovieInit(); + + // fill in the movie's header + Movie.header.uid = (uint32)time(NULL); + Movie.header.magic = VBM_MAGIC; + Movie.header.version = VBM_VERSION; + Movie.header.rerecord_count = 0; + Movie.header.length_frames = 0; + Movie.header.startFlags = startFlags; + Movie.header.controllerFlags = controllerFlags; + Movie.header.typeFlags = typeFlags; + Movie.header.minorVersion = VBM_REVISION; + + // set emulator settings that make the movie more likely to stay synchronized when it's later played back + SetRecordEmuSettings(); + + // set ROM and BIOS checksums and stuff + VBAMovieGetRomInfo(Movie, Movie.header.romTitle, Movie.header.romGameCode, Movie.header.romOrBiosChecksum, Movie.header.romCRC); + + // write the header to file + write_movie_header(file, Movie); + + // copy over the metadata / author info + VBAMovieSetMetadata(authorInfo); + + // write the metadata / author info to file + fwrite(Movie.authorInfo, 1, sizeof(char) * MOVIE_METADATA_SIZE, file); + + // write snapshot or SRAM if applicable + if (Movie.header.startFlags & MOVIE_START_FROM_SNAPSHOT + || Movie.header.startFlags & MOVIE_START_FROM_SRAM) + { + Movie.header.offset_to_savestate = (uint32)ftell(file); + + // close the file and reopen it as a stream: + + fn = dup(fileno(file)); + fclose(file); + + if (!(stream = utilGzReopen(fn, "ab"))) // append mode to start at end, no seek necessary + { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; } + + // write the save data: + if (Movie.header.startFlags & MOVIE_START_FROM_SNAPSHOT) + { + // save snapshot + if (!theEmulator.emuWriteStateToStream(stream)) + { + utilGzClose(stream); + { loadingMovie = false; return MOVIE_UNKNOWN_ERROR; } + } + } + else if (Movie.header.startFlags & MOVIE_START_FROM_SRAM) + { + // save SRAM + if (!theEmulator.emuWriteBatteryToStream(stream)) + { + utilGzClose(stream); + { loadingMovie = false; return MOVIE_UNKNOWN_ERROR; } + } + + // 'soft' reset: + theEmulator.emuReset(false); + } + + utilGzClose(stream); + + // reopen the file and seek back to the end + + if (!(file = fopen(movie_filename, "rb+"))) + { loadingMovie = false; return MOVIE_FILE_NOT_FOUND; } + + fseek(file, 0, SEEK_END); + } + else // no snapshot or SRAM + { + HardResetAndSRAMClear(); + } + + Movie.header.offset_to_controller_data = (uint32)ftell(file); + + strcpy(Movie.filename, movie_filename); + Movie.file = file; + Movie.bytesPerFrame = bytes_per_frame(Movie); + Movie.inputBufferPtr = Movie.inputBuffer; + Movie.currentFrame = 0; + Movie.readOnly = false; + Movie.RecordedThisSession = true; + + change_state(MOVIE_STATE_RECORD); + + systemScreenMessage("Recording movie..."); + { loadingMovie = false; return MOVIE_SUCCESS; } +} + +void VBAUpdateButtonPressDisplay() +{ + uint32 keys = currentButtons[0] & BUTTON_REGULAR_RECORDING_MASK; + + const static char KeyMap[] = { 'A', 'B', 's', 'S', '>', '<', '^', 'v', 'R', 'L', '!', '?', '{', '}', 'v', '^' }; + const static int KeyOrder[] = { 5, 6, 4, 7, 0, 1, 9, 8, 3, 2, 12, 15, 13, 14, 11, 10 }; // < ^ > v A B L R S s { = } _ + // ? ! + char buffer[256]; + sprintf(buffer, " "); + +#ifndef WIN32 + // don't bother color-coding autofire and such + int i; + for (i = 0; i < 15; i++) + { + int j = KeyOrder[i]; + int mask = (1 << (j)); + buffer[strlen(" ") + i] = ((keys & mask) != 0) ? KeyMap[j] : ' '; + } + + systemScreenMessage(buffer, 2, -1); +#else + const bool eraseAll = !theApp.inputDisplay; + uint32 autoHeldKeys = eraseAll ? 0 : theApp.autoHold & BUTTON_REGULAR_RECORDING_MASK; + uint32 autoFireKeys = eraseAll ? 0 : (theApp.autoFire | theApp.autoFire2) & BUTTON_REGULAR_RECORDING_MASK; + uint32 pressedKeys = eraseAll ? 0 : keys; + + char colorList[64]; + memset(colorList, 1, strlen(buffer)); + + if (!eraseAll) + { + for (int i = 0; i < 15; i++) + { + const int j = KeyOrder[i]; + const int mask = (1 << (j)); + bool pressed = (pressedKeys & mask) != 0; + const bool autoHeld = (autoHeldKeys & mask) != 0; + const bool autoFired = (autoFireKeys & mask) != 0; + const bool erased = (lastKeys & mask) != 0 && (!pressed && !autoHeld && !autoFired); + extern int textMethod; + if (textMethod != 2 && (autoHeld || (autoFired && !pressed) || erased)) + { + int colorNum = 1; // default is white + if (autoHeld) + colorNum += (pressed ? 2 : 1); // yellow if pressed, red if not + else if (autoFired) + colorNum += 5; // blue if autofired and not currently pressed + else if (erased) + colorNum += 8; // black on black + + colorList[strlen(" ") + i] = colorNum; + pressed = true; + } + buffer[strlen(" ") + i] = pressed ? KeyMap[j] : ' '; + } + } + + lastKeys = currentButtons[0]; + lastKeys |= theApp.autoHold & BUTTON_REGULAR_RECORDING_MASK; + lastKeys |= (theApp.autoFire | theApp.autoFire2) & BUTTON_REGULAR_RECORDING_MASK; + + systemScreenMessage(buffer, 2, -1, colorList); +#endif +} + +void VBAUpdateFrameCountDisplay() +{ + const int MAGICAL_NUMBER = 64; // FIXME: this won't do any better, but only to remind you of sz issues + char frameDisplayString[MAGICAL_NUMBER]; + char lagFrameDisplayString[MAGICAL_NUMBER]; + char extraCountDisplayString[MAGICAL_NUMBER]; + +#if (defined(WIN32) && !defined(SDL)) + if (theApp.frameCounter) +#else + /// SDL FIXME +#endif + { + switch (Movie.state) + { + case MOVIE_STATE_PLAY: + case MOVIE_STATE_END: + { + sprintf(frameDisplayString, "%d / %d", Movie.currentFrame, Movie.header.length_frames); + if (!Movie.readOnly) + strcat(frameDisplayString, " (edit)"); + break; + } + case MOVIE_STATE_RECORD: + { + sprintf(frameDisplayString, "%d (record)", Movie.currentFrame); + break; + } + default: + { + sprintf(frameDisplayString, "%d (no movie)", systemCounters.frameCount); + break; + } + } + +#if (defined(WIN32) && !defined(SDL)) + if (theApp.lagCounter) +#else + /// SDL FIXME +#endif + { +// sprintf(lagFrameDisplayString, " %c %d", systemCounters.laggedLast ? '*' : '|', systemCounters.lagCount); + sprintf(lagFrameDisplayString, " | %d%s", systemCounters.lagCount, systemCounters.laggedLast ? " *" : ""); + strcat(frameDisplayString, lagFrameDisplayString); + } + +#if (defined(WIN32) && !defined(SDL)) + if (theApp.extraCounter) +#else + /// SDL FIXME +#endif + { + sprintf(extraCountDisplayString, " | %d", systemCounters.frameCount - systemCounters.extraCount); + strcat(frameDisplayString, extraCountDisplayString); + } + } +#if (defined(WIN32) && !defined(SDL)) + else + { + frameDisplayString[0] = '\0'; + } +#else + /// SDL FIXME +#endif + systemScreenMessage(frameDisplayString, 1, -1); +} + +// this function should only be called once every frame +void VBAMovieUpdateState() +{ + ++Movie.currentFrame; + + if (Movie.state == MOVIE_STATE_PLAY) + { + Movie.inputBufferPtr += Movie.bytesPerFrame; + if (Movie.currentFrame >= Movie.header.length_frames) + { + // the movie ends anyway; what to do next depends on the settings + change_state(MOVIE_STATE_END); + } + } + else if (Movie.state == MOVIE_STATE_RECORD) + { + // use first fseek? + fwrite(Movie.inputBufferPtr, 1, Movie.bytesPerFrame, Movie.file); + Movie.header.length_frames = Movie.currentFrame; + Movie.inputBufferPtr += Movie.bytesPerFrame; + Movie.RecordedThisSession = true; + flush_movie_header(); + } + else if (Movie.state == MOVIE_STATE_END) + { + change_state(MOVIE_STATE_END); + } +} + +void VBAMovieRead(int i, bool /*sensor*/) +{ + if (Movie.state != MOVIE_STATE_PLAY) + return; + + if (i < 0 || i >= MOVIE_NUM_OF_POSSIBLE_CONTROLLERS) + return; // not a controller we're recognizing + + if (Movie.header.controllerFlags & MOVIE_CONTROLLER(i)) + { + currentButtons[i] = Read16(Movie.inputBufferPtr + CONTROLLER_DATA_SIZE * i); + } + else + { + currentButtons[i] = 0; // pretend the controller is disconnected + } + + if ((currentButtons[i] & BUTTON_MASK_NEW_RESET) != 0) + resetSignaled = true; +} + +void VBAMovieWrite(int i, bool /*sensor*/) +{ + if (Movie.state != MOVIE_STATE_RECORD) + return; + + if (i < 0 || i >= MOVIE_NUM_OF_POSSIBLE_CONTROLLERS) + return; // not a controller we're recognizing + + reserve_buffer_space((uint32)((Movie.inputBufferPtr - Movie.inputBuffer) + Movie.bytesPerFrame)); + + if (Movie.header.controllerFlags & MOVIE_CONTROLLER(i)) + { + // get the current controller data + uint16 buttonData = currentButtons[i]; + + // mask away the irrelevent bits + buttonData &= BUTTON_REGULAR_MASK | BUTTON_MOTION_MASK; + + // soft-reset "button" for 1 frame if the game is reset while recording + if (resetSignaled) + { + buttonData |= BUTTON_MASK_NEW_RESET; + } + + // backward compatibility kludge + if (resetSignaledLast) + { + buttonData |= BUTTON_MASK_OLD_RESET; + } + + Write16(buttonData, Movie.inputBufferPtr + CONTROLLER_DATA_SIZE * i); + + // and for display + currentButtons[i] = buttonData; + } + else + { + // pretend the controller is disconnected (otherwise input it gives could cause desync since we're not writing it to the + // movie) + currentButtons[i] = 0; + } +} + +void VBAMovieStop(bool8 suppress_message) +{ + if (Movie.state != MOVIE_STATE_NONE) + { + change_state(MOVIE_STATE_NONE); + if (!suppress_message) + systemScreenMessage("Movie stop"); + } +} + +int VBAMovieGetInfo(const char *filename, SMovie *info) +{ + assert(info != NULL); + if (info == NULL) + return -1; + + FILE * file; + int result; + SMovie &local_movie = *info; + + memset(info, 0, sizeof(*info)); + if (filename[0] == '\0') + return MOVIE_FILE_NOT_FOUND; + if (!(file = fopen(filename, "rb"))) + return MOVIE_FILE_NOT_FOUND; + + // read header + if ((result = (read_movie_header(file, local_movie))) != MOVIE_SUCCESS) + { + fclose(file); + return result; + } + + // read the metadata / author info from file + fread(local_movie.authorInfo, 1, sizeof(char) * MOVIE_METADATA_SIZE, file); + + strncpy(local_movie.filename, filename, _MAX_PATH); + local_movie.filename[_MAX_PATH - 1] = '\0'; + + if (Movie.file != NULL && stricmp(local_movie.filename, Movie.filename) == 0) // alreadyOpen + { + local_movie.bytesPerFrame = Movie.bytesPerFrame; + local_movie.header.length_frames = Movie.header.length_frames; + } + else + { + // recalculate length of movie from the file size + local_movie.bytesPerFrame = bytes_per_frame(local_movie); + fseek(file, 0, SEEK_END); + int fileSize = ftell(file); + local_movie.header.length_frames = + (fileSize - local_movie.header.offset_to_controller_data) / local_movie.bytesPerFrame; + } + + fclose(file); + + if (access(filename, W_OK)) + info->readOnly = true; + + return MOVIE_SUCCESS; +} + +bool8 VBAMovieActive() +{ + return (Movie.state != MOVIE_STATE_NONE); +} + +bool8 VBAMovieLoading() +{ + return loadingMovie; +} + +bool8 VBAMoviePlaying() +{ + return (Movie.state == MOVIE_STATE_PLAY); +} + +bool8 VBAMovieRecording() +{ + return (Movie.state == MOVIE_STATE_RECORD); +} + +bool8 VBAMovieReadOnly() +{ + if (!VBAMovieActive()) + return false; + + return Movie.readOnly; +} + +void VBAMovieToggleReadOnly() +{ + if (!VBAMovieActive()) + return; + + if (Movie.readOnly != 2) + { + Movie.readOnly = !Movie.readOnly; + + systemScreenMessage(Movie.readOnly ? "Movie now read-only" : "Movie now editable"); + } + else + { + systemScreenMessage("Can't toggle read-only movie"); + } +} + +uint32 VBAMovieGetVersion() +{ + if (!VBAMovieActive()) + return 0; + + return Movie.header.version; +} + +uint32 VBAMovieGetMinorVersion() +{ + if (!VBAMovieActive()) + return 0; + + return Movie.header.minorVersion; +} + +uint32 VBAMovieGetId() +{ + if (!VBAMovieActive()) + return 0; + + return Movie.header.uid; +} + +uint32 VBAMovieGetLength() +{ + if (!VBAMovieActive()) + return 0; + + return Movie.header.length_frames; +} + +uint32 VBAMovieGetFrameCounter() +{ + if (!VBAMovieActive()) + return 0; + + return Movie.currentFrame; +} + +uint32 VBAMovieGetRerecordCount() +{ + if (!VBAMovieActive()) + return 0; + + return Movie.header.rerecord_count; +} + +uint32 VBAMovieSetRerecordCount(uint32 newRerecordCount) +{ + uint32 oldRerecordCount = 0; + if (!VBAMovieActive()) + return 0; + + oldRerecordCount = Movie.header.rerecord_count; + Movie.header.rerecord_count = newRerecordCount; + return oldRerecordCount; +} + +std::string VBAMovieGetAuthorInfo() +{ + if (!VBAMovieActive()) + return ""; + + return Movie.authorInfo; +} + +std::string VBAMovieGetFilename() +{ + if (!VBAMovieActive()) + return ""; + + return Movie.filename; +} + +void VBAMovieFreeze(uint8 * *buf, uint32 *size) +{ + // sanity check + if (!VBAMovieActive()) + { + return; + } + + *buf = NULL; + *size = 0; + + // compute size needed for the buffer + // room for header.uid, currentFrame, and header.length_frames + uint32 size_needed = sizeof(Movie.header.uid) + sizeof(Movie.currentFrame) + sizeof(Movie.header.length_frames); + size_needed += (uint32)(Movie.bytesPerFrame * Movie.header.length_frames); + *buf = new uint8[size_needed]; + *size = size_needed; + + uint8 *ptr = *buf; + if (!ptr) + { + return; + } + + Push32(Movie.header.uid, ptr); + Push32(Movie.currentFrame, ptr); + Push32(Movie.header.length_frames - 1, ptr); // HACK: shorten the length by 1 for backward compatibility + + memcpy(ptr, Movie.inputBuffer, Movie.bytesPerFrame * Movie.header.length_frames); +} + +int VBAMovieUnfreeze(const uint8 *buf, uint32 size) +{ + // sanity check + if (!VBAMovieActive()) + { + return MOVIE_NOT_FROM_A_MOVIE; + } + + const uint8 *ptr = buf; + if (size < sizeof(Movie.header.uid) + sizeof(Movie.currentFrame) + sizeof(Movie.header.length_frames)) + { + return MOVIE_WRONG_FORMAT; + } + + uint32 movie_id = Pop32(ptr); + uint32 current_frame = Pop32(ptr); + uint32 end_frame = Pop32(ptr) + 1; // HACK: restore the length for backward compatibility + uint32 space_needed = Movie.bytesPerFrame * end_frame; + + if (movie_id != Movie.header.uid) + return MOVIE_NOT_FROM_THIS_MOVIE; + + if (space_needed > size) + return MOVIE_WRONG_FORMAT; + + if (Movie.readOnly) + { + // here, we are going to keep the input data from the movie file + // and simply rewind to the currentFrame pointer + // this will cause a desync if the savestate is not in sync // <-- NOT ANYMORE + // with the on-disk recording data, but it's easily solved + // by loading another savestate or playing the movie from the beginning + + // don't allow loading a state inconsistent with the current movie + uint32 length_history = min(current_frame, Movie.header.length_frames); + if (end_frame < length_history) + return MOVIE_SNAPSHOT_INCONSISTENT; + + uint32 space_shared = Movie.bytesPerFrame * length_history; + if (memcmp(Movie.inputBuffer, ptr, space_shared)) + return MOVIE_SNAPSHOT_INCONSISTENT; + + Movie.currentFrame = current_frame; + Movie.inputBufferPtr = Movie.inputBuffer + Movie.bytesPerFrame * min(current_frame, Movie.header.length_frames); + } + else + { + // here, we are going to take the input data from the savestate + // and make it the input data for the current movie, then continue + // writing new input data at the currentFrame pointer + Movie.currentFrame = current_frame; + Movie.header.length_frames = end_frame; + if (!VBALuaRerecordCountSkip()) + ++Movie.header.rerecord_count; + + Movie.RecordedThisSession = true; + + // do this before calling reserve_buffer_space() + Movie.inputBufferPtr = Movie.inputBuffer + Movie.bytesPerFrame * min(current_frame, Movie.header.length_frames); + reserve_buffer_space(space_needed); + memcpy(Movie.inputBuffer, ptr, space_needed); + + // for consistency, no auto movie conversion here since we don't auto convert the corresponding savestate + flush_movie_header(); + flush_movie_frames(); + } + + change_state(MOVIE_STATE_PLAY); // check for movie end + + // necessary! + resetSignaled = false; + resetSignaledLast = false; + + // necessary to check if there's a reset signal at the previous frame + if (current_frame > 0) + { + const u8 NEW_RESET = u8(BUTTON_MASK_NEW_RESET >> 8); + for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i) + { + if ((Movie.header.controllerFlags & MOVIE_CONTROLLER(i)) && (*(Movie.inputBufferPtr+1- Movie.bytesPerFrame) & NEW_RESET)) + { + resetSignaledLast = true; + break; + } + } + } + + return MOVIE_SUCCESS; +} + +bool VBAMovieEnded() +{ + return (Movie.state == MOVIE_STATE_END); +// return (Movie.state != MOVIE_STATE_NONE && Movie.currentFrame >= Movie.header.length_frames); +} + +bool VBAMovieAllowsRerecording() +{ + bool allows = (Movie.state != MOVIE_STATE_NONE) && (Movie.currentFrame <= Movie.header.length_frames); + return /*!VBAMovieReadOnly() &&*/ allows; +} + +bool VBAMovieSwitchToPlaying() +{ + if (!VBAMovieActive()) + return false; + + if (!Movie.readOnly) + { + VBAMovieToggleReadOnly(); + } + + change_state(MOVIE_STATE_PLAY); + if (Movie.state == MOVIE_STATE_PLAY) + systemScreenMessage("Movie replay (continue)"); + else + systemScreenMessage("Movie end"); + + return true; +} + +bool VBAMovieSwitchToRecording() +{ + if (!VBAMovieAllowsRerecording()) + return false; + + if (Movie.readOnly) + { + VBAMovieToggleReadOnly(); + } + + if (!VBALuaRerecordCountSkip()) + ++Movie.header.rerecord_count; + + change_state(MOVIE_STATE_RECORD); + systemScreenMessage("Movie re-record"); + + //truncate_movie(Movie.currentFrame); + + return true; +} + +uint32 VBAMovieGetState() +{ + // ? + if (!VBAMovieActive()) + return MOVIE_STATE_NONE; + + return Movie.state; +} + +void VBAMovieSignalReset() +{ + if (VBAMovieActive()) + resetSignaled = true; +} + +void VBAMovieResetIfRequested() +{ + if (resetSignaled) + { + theEmulator.emuReset(false); + resetSignaled = false; + resetSignaledLast = true; + } + else + { + resetSignaledLast = false; + } +} + +void VBAMovieSetMetadata(const char *info) +{ + if (!memcmp(Movie.authorInfo, info, MOVIE_METADATA_SIZE)) + return; + + memcpy(Movie.authorInfo, info, MOVIE_METADATA_SIZE); // strncpy would omit post-0 bytes + Movie.authorInfo[MOVIE_METADATA_SIZE - 1] = '\0'; + + if (Movie.file) + { + // (over-)write the header + fseek(Movie.file, 0, SEEK_SET); + write_movie_header(Movie.file, Movie); + + // write the metadata / author info to file + fwrite(Movie.authorInfo, 1, sizeof(char) * MOVIE_METADATA_SIZE, Movie.file); + + fflush(Movie.file); + } +} + +void VBAMovieRestart() +{ + if (VBAMovieActive()) + { + systemSoundClearBuffer(); + + bool8 modified = Movie.RecordedThisSession; + + VBAMovieStop(true); + + char movieName [_MAX_PATH]; + strncpy(movieName, Movie.filename, _MAX_PATH); + movieName[_MAX_PATH - 1] = '\0'; + VBAMovieOpen(movieName, Movie.readOnly); // can't just pass in Movie.filename, since VBAMovieOpen clears out Movie's + // variables + + Movie.RecordedThisSession = modified; + + systemScreenMessage("Movie replay (restart)"); + } +} + +int VBAMovieGetPauseAt() +{ + return Movie.pauseFrame; +} + +void VBAMovieSetPauseAt(int at) +{ + Movie.pauseFrame = at; +} + +/////////////////////// +// movie tools + +// FIXME: is it safe to convert/flush a movie while recording it (considering fseek() problem)? +int VBAMovieConvertCurrent() +{ + if (!VBAMovieActive()) + { + return MOVIE_NOTHING; + } + + if (Movie.header.minorVersion > VBM_REVISION) + { + return MOVIE_WRONG_VERSION; + } + + if (Movie.header.minorVersion == VBM_REVISION) + { + return MOVIE_NOTHING; + } + + Movie.header.minorVersion = VBM_REVISION; + + if (Movie.header.length_frames == 0) // this could happen + { + truncate_movie(0); + return MOVIE_SUCCESS; + } + + // fix movies recorded from snapshots + if (Movie.header.startFlags & MOVIE_START_FROM_SNAPSHOT) + { + uint8 *firstFramePtr = Movie.inputBuffer; + for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i) + { + if (Movie.header.controllerFlags & MOVIE_CONTROLLER(i)) + { + Push16(initialInputs[i], firstFramePtr); + // note: this is correct since Push16 advances the dest pointer by sizeof u16 + } + } + } + + // convert old resets to new ones + const u8 OLD_RESET = u8(BUTTON_MASK_OLD_RESET >> 8); + const u8 NEW_RESET = u8(BUTTON_MASK_NEW_RESET >> 8); + for (int i = 0; i < MOVIE_NUM_OF_POSSIBLE_CONTROLLERS; ++i) + { + if (Movie.header.controllerFlags & MOVIE_CONTROLLER(i)) + { + uint8 *startPtr = Movie.inputBuffer + sizeof(u16) * i + 1; + uint8 *endPtr = Movie.inputBuffer + Movie.bytesPerFrame * (Movie.header.length_frames - 1); + for (; startPtr < endPtr; startPtr += Movie.bytesPerFrame) + { + if (startPtr[Movie.bytesPerFrame] & OLD_RESET) + { + startPtr[0] |= NEW_RESET; + } + } + } + } + + flush_movie_header(); + flush_movie_frames(); + return MOVIE_SUCCESS; +} + +bool VBAMovieTuncateAtCurrentFrame() +{ + if (!VBAMovieActive()) + return false; + + truncate_movie(Movie.currentFrame); + change_state(MOVIE_STATE_END); + systemScreenMessage("Movie truncated"); + + return true; +} + +bool VBAMovieFixHeader() +{ + if (!VBAMovieActive()) + return false; + + flush_movie_header(); + systemScreenMessage("Movie header fixed"); + return true; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/movie.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/movie.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,190 @@ +#ifndef VBA_MOVIE_H +#define VBA_MOVIE_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include +#include + +#include "../Port.h" + +#define ZLIB +///#ifdef ZLIB +#ifndef WIN32 +#include "zlib.h" +#endif + +#ifndef MOVIE_SUCCESS +# define MOVIE_SUCCESS 1 +# define MOVIE_NOTHING 0 +# define MOVIE_WRONG_FORMAT (-1) +# define MOVIE_WRONG_VERSION (-2) +# define MOVIE_FILE_NOT_FOUND (-3) +# define MOVIE_NOT_FROM_THIS_MOVIE (-4) +# define MOVIE_NOT_FROM_A_MOVIE (-5) +# define MOVIE_SNAPSHOT_INCONSISTENT (-6) +# define MOVIE_UNKNOWN_ERROR (-7) +#endif + +#define VBM_MAGIC (0x1a4D4256) // VBM0x1a +#define VBM_VERSION (1) +#define VBM_HEADER_SIZE (64) +#define CONTROLLER_DATA_SIZE (2) +#define BUFFER_GROWTH_SIZE (4096) +#define MOVIE_METADATA_SIZE (192) +#define MOVIE_METADATA_AUTHOR_SIZE (64) + +// revision 1 uses (?) insted of (!) as reset +#define VBM_REVISION (1) + +#define MOVIE_START_FROM_SNAPSHOT (1<<0) +#define MOVIE_START_FROM_SRAM (1<<1) + +#define MOVIE_CONTROLLER(i) (1<<(i)) +#define MOVIE_CONTROLLERS_ANY_MASK (MOVIE_CONTROLLER(0)|MOVIE_CONTROLLER(1)|MOVIE_CONTROLLER(2)|MOVIE_CONTROLLER(3)) +#define MOVIE_NUM_OF_POSSIBLE_CONTROLLERS (4) + +#define MOVIE_TYPE_GBA (1<<0) +#define MOVIE_TYPE_GBC (1<<1) +#define MOVIE_TYPE_SGB (1<<2) + +#define MOVIE_SETTING_USEBIOSFILE (1<<0) +#define MOVIE_SETTING_SKIPBIOSFILE (1<<1) +#define MOVIE_SETTING_RTCENABLE (1<<2) +#define MOVIE_SETTING_GBINPUTHACK (1<<3) +#define MOVIE_SETTING_LAGHACK (1<<4) +#define MOVIE_SETTING_GBCFF55FIX (1<<5) +#define MOVIE_SETTING_GBECHORAMFIX (1<<6) + +#define STREAM gzFile +/*#define READ_STREAM(p,l,s) gzread (s,p,l) + #define WRITE_STREAM(p,l,s) gzwrite (s,p,l) + #define OPEN_STREAM(f,m) gzopen (f,m) + #define REOPEN_STREAM(f,m) gzdopen (f,m) + #define FIND_STREAM(f) gztell(f) + #define REVERT_STREAM(f,o,s) gzseek(f,o,s) + #define CLOSE_STREAM(s) gzclose (s) + #else + #define STREAM FILE * + #define READ_STREAM(p,l,s) fread (p,1,l,s) + #define WRITE_STREAM(p,l,s) fwrite (p,1,l,s) + #define OPEN_STREAM(f,m) fopen (f,m) + #define REOPEN_STREAM(f,m) fdopen (f,m) + #define FIND_STREAM(f) ftell(f) + #define REVERT_STREAM(f,o,s) fseek(f,o,s) + #define CLOSE_STREAM(s) fclose (s) + #endif*/ + +enum MovieState +{ + MOVIE_STATE_NONE = 0, + MOVIE_STATE_PLAY, + MOVIE_STATE_RECORD, + MOVIE_STATE_END +}; + +struct SMovieFileHeader +{ + uint32 magic; // VBM0x1a + uint32 version; // 1 + int32 uid; // used to match savestates to a particular movie + uint32 length_frames; + uint32 rerecord_count; + uint8 startFlags; + uint8 controllerFlags; + uint8 typeFlags; + uint8 optionFlags; + uint32 saveType; // emulator setting value + uint32 flashSize; // emulator setting value + uint32 gbEmulatorType; // emulator setting value + char romTitle [12]; + uint8 minorVersion; // minor version/revision of the current movie version + uint8 romCRC; // the CRC of the ROM used while recording + uint16 romOrBiosChecksum; // the Checksum of the ROM used while recording, or a CRC of the BIOS if GBA + uint32 romGameCode; // the Game Code of the ROM used while recording, or "\0\0\0\0" if not GBA + uint32 offset_to_savestate; // offset to the savestate or SRAM inside file, set to 0 if unused + uint32 offset_to_controller_data; // offset to the controller data inside file +}; + +struct SMovie +{ + enum MovieState state; + char filename[/*_MAX_PATH*/ 260]; // FIXME: should use a string instead + FILE* file; + uint8 readOnly; + int32 pauseFrame; // FIXME: byte size + + SMovieFileHeader header; + char authorInfo[MOVIE_METADATA_SIZE]; + + uint32 currentFrame; // should == length_frame when recording, and be < length_frames when playing + uint32 bytesPerFrame; + uint8* inputBuffer; + uint32 inputBufferSize; + uint8* inputBufferPtr; + + // bool8 doesn't make much sense if it is meant to solve any portability problem, + // because there's no guarantee that true == 1 and false == 0 (or TRUE == 1 and FALSE == 0) on all platforms. + // while using user-defined boolean types might impact on performance. + // the more reliable (and faster!) way to maintain cross-platform I/O compatibility is + // to manually map from/to built-in boolean types to/from fixed-sized types value by value ONLY when doing I/O + // e.g. bool(true) <-> u8(1) and <-> bool(false) <-> u8(0), BOOL(TRUE) <-> s32(-1) and BOOL(FALSE) <-> s32(0) etc. + bool8 RecordedThisSession; +}; + +// methods used by the user-interface code +int VBAMovieOpen(const char *filename, bool8 read_only); +int VBAMovieCreate(const char *filename, const char *authorInfo, uint8 startFlags, uint8 controllerFlags, uint8 typeFlags); +int VBAMovieGetInfo(const char *filename, SMovie*info); +void VBAMovieGetRomInfo(const SMovie &movieInfo, char romTitle[12], uint32 &romGameCode, uint16 &checksum, uint8 &crc); +void VBAMovieStop(bool8 suppress_message); +const char *VBAChooseMovieFilename(bool8 read_only); + +// methods used by the emulation +void VBAMovieInit(); +void VBAMovieUpdateState(); +void VBAMovieRead(int controllerNum = 0, bool sensor = false); +void VBAMovieWrite(int controllerNum = 0, bool sensor = false); +void VBAUpdateButtonPressDisplay(); +void VBAUpdateFrameCountDisplay(); +//bool8 VBAMovieRewind (uint32 at_frame); +void VBAMovieFreeze(uint8 **buf, uint32 *size); +int VBAMovieUnfreeze(const uint8 *buf, uint32 size); +void VBAMovieRestart(); + +// accessor functions +bool8 VBAMovieActive(); +bool8 VBAMovieLoading(); +bool8 VBAMoviePlaying(); +bool8 VBAMovieRecording(); +// the following accessors return 0/false if !VBAMovieActive() +uint8 VBAMovieReadOnly(); +uint32 VBAMovieGetVersion(); +uint32 VBAMovieGetMinorVersion(); +uint32 VBAMovieGetId(); +uint32 VBAMovieGetLength(); +uint32 VBAMovieGetFrameCounter(); +uint32 VBAMovieGetState(); +uint32 VBAMovieGetRerecordCount (); +uint32 VBAMovieSetRerecordCount (uint32 newRerecordCount); +std::string VBAMovieGetAuthorInfo(); +std::string VBAMovieGetFilename(); + +uint16 VBAMovieGetCurrentInputOf(int controllerNum, bool normalOnly = true); +void VBAMovieSignalReset(); +void VBAMovieResetIfRequested(); +void VBAMovieSetMetadata(const char *info); +void VBAMovieToggleReadOnly(); +bool VBAMovieEnded(); +bool VBAMovieAllowsRerecording(); +bool VBAMovieSwitchToPlaying(); +bool VBAMovieSwitchToRecording(); +int VBAMovieGetPauseAt(); +void VBAMovieSetPauseAt(int at); +int VBAMovieConvertCurrent(); +bool VBAMovieTuncateAtCurrentFrame(); +bool VBAMovieFixHeader(); + +#endif // VBA_MOVIE_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/nesvideos-piece.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/nesvideos-piece.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,525 @@ +#include +#include +#include +#include + +/* Note: This module assumes everyone uses RGB15 as display depth */ + +static std::string VIDEO_CMD = + "mencoder - -o test0.avi" + " -noskip -mc 0" + " -ovc lavc" + " -oac mp3lame" + " -lameopts preset=256:aq=2:mode=3" + " -lavcopts vcodec=ffv1:context=0:format=BGR32:coder=0:vstrict=-1" + " >& mencoder.log"; + +static void FlushWrite(FILE* fp, const unsigned char*buf, unsigned length); + +#define BGR24 (0x42475218) // BGR24 fourcc +#define BGR16 (0x42475210) // BGR16 fourcc +#define BGR15 (0x4247520F) // BGR15 fourcc + +static FILE* (*openFunc) (const char*, const char*) = NULL; +static int (*closeFunc) (FILE*) = NULL; + +#if (defined(WIN32) || defined(win32)) // capital is standard, but check for either + #include + #define popen _popen; + #define pclose _pclose; +#endif + +#define u32(n) (n)&255,((n)>>8)&255,((n)>>16)&255,((n)>>24)&255 +#define u16(n) (n)&255,((n)>>8)&255 +#define s4(s) s[0],s[1],s[2],s[3] + +static const unsigned FPS_SCALE = (0x1000000); + +// general-purpose A/V sync debugging, ignored unless explicitly enabled with NESVideoEnableDebugging +static void (*debugVideoMessageFunc)(const char *msg) = NULL; +static void (*debugAudioMessageFunc)(const char *msg) = NULL; +// logo adds 1 "frame" to audio, so offset that (A/V frames shouldn't necessarily match up depending on the rates, but should at least make them start out matching in case they do) +static unsigned audioFramesWritten=0, videoFramesWritten=1; +static double audioSecondsWritten=0, videoSecondsWritten=0; + + +static class AVI +{ + FILE* avifp; + + bool KnowVideo; + unsigned width; + unsigned height; + unsigned fps_scaled; + std::vector VideoBuffer; + + bool KnowAudio; + unsigned rate; + unsigned chans; + unsigned bits; + std::vector AudioBuffer; + +public: + AVI() : + avifp(NULL), + KnowVideo(false), + KnowAudio(false) + { + } + ~AVI() + { + if(avifp) closeFunc(avifp); + } + + void Audio(unsigned r,unsigned b,unsigned c, + const unsigned char*d, unsigned nsamples) + { + if(!KnowAudio) + { + rate = r; + chans = c; + bits = b; + KnowAudio = true; + CheckFlushing(); + } + unsigned bytes = nsamples*chans*(bits/8); + + if(debugAudioMessageFunc) + { + audioFramesWritten++; + audioSecondsWritten += (double)nsamples / (double)rate; // += bytes times seconds per byte + char temp [64]; + sprintf(temp, "A: %.2lf s, %d f", audioSecondsWritten, audioFramesWritten); + debugAudioMessageFunc(temp); + } + + if(KnowVideo) + SendAudioFrame(d, bytes); + else + { + AudioBuffer.insert(AudioBuffer.end(), d, d+bytes); + fprintf(stderr, "Buffering %u bytes of audio\n", bytes); + } + } + void Video(unsigned w,unsigned h,unsigned f, const unsigned char*d) + { + if(!KnowVideo) + { + width=w; + height=h; + fps_scaled=f; + KnowVideo = true; + CheckFlushing(); + } + + unsigned bytes = width*height*2; + + //std::vector tmp(bytes, 'k'); + //d = &tmp[0]; + + if(debugVideoMessageFunc) + { + videoFramesWritten++; + videoSecondsWritten += (double)FPS_SCALE / (double)fps_scaled; // += seconds per frame + char temp [64]; + sprintf(temp, "V: %.2lf s, %d f", videoSecondsWritten, videoFramesWritten); + debugVideoMessageFunc(temp); + } + + if(KnowAudio) + SendVideoFrame(d, bytes); + else + { + VideoBuffer.insert(VideoBuffer.end(), d, d+bytes); + fprintf(stderr, "Buffering %u bytes of video\n", bytes); + } + } + +private: + void CheckFlushing() + { + //AudioBuffer.clear(); + //VideoBuffer.clear(); + + if(KnowAudio && KnowVideo) + { + unsigned last_offs; + + // Flush Audio + + last_offs = 0; + while(last_offs < AudioBuffer.size()) + { + unsigned bytes = rate / (fps_scaled / FPS_SCALE); + bytes *= chans*(bits/8); + + unsigned remain = AudioBuffer.size() - last_offs; + if(bytes > remain) bytes = remain; + if(!bytes) break; + + unsigned begin = last_offs; + last_offs += bytes; + SendAudioFrame(&AudioBuffer[begin], bytes); + } + AudioBuffer.erase(AudioBuffer.begin(), AudioBuffer.begin()+last_offs); + + // Flush Video + + last_offs = 0; + while(last_offs < VideoBuffer.size()) + { + unsigned bytes = width*height*2; + unsigned remain = VideoBuffer.size() - last_offs; + if(bytes > remain) bytes = remain; + if(!bytes)break; + + unsigned begin = last_offs; + last_offs += bytes; + SendVideoFrame(&VideoBuffer[begin], bytes); + } + VideoBuffer.erase(VideoBuffer.begin(), VideoBuffer.begin()+last_offs); + } + } + + void SendVideoFrame(const unsigned char* vidbuf, unsigned framesize) + { + CheckBegin(); + + //fprintf(stderr, "Writing 00dc of %u bytes\n", framesize); + + const unsigned char header[] = { s4("00dc"), u32(framesize) }; + FlushWrite(avifp, header, sizeof(header)); + FlushWrite(avifp, vidbuf, framesize); + } + + void SendAudioFrame(const unsigned char* audbuf, unsigned framesize) + { + CheckBegin(); + + //fprintf(stderr, "Writing 01wb of %u bytes\n", framesize); + + const unsigned char header[] = { s4("01wb"), u32(framesize) }; + FlushWrite(avifp, header, sizeof(header)); + FlushWrite(avifp, audbuf, framesize); + } + + void CheckBegin() + { + if(avifp) return; + + if(!openFunc) openFunc = popen; // default + if(!closeFunc) closeFunc = pclose; // default + + avifp = openFunc(VIDEO_CMD.c_str(), "wb"); + if(!avifp) return; + + const unsigned fourcc = BGR16; + const unsigned framesize = width*height*2; + + const unsigned aud_rate = rate; + const unsigned aud_chans = chans; + const unsigned aud_bits = bits; + + const unsigned nframes = 0; //unknown + const unsigned scale = FPS_SCALE; + const unsigned scaled_fps = fps_scaled; + + const unsigned SIZE_strh_vids = 4 + 4*2 + 2*2 + 8*4 + 2*4; + const unsigned SIZE_strf_vids = 4*3 + 2*2 + 4*6; + const unsigned SIZE_strl_vids = 4+ 4+(4+SIZE_strh_vids) + 4+(4+SIZE_strf_vids); + + const unsigned SIZE_strh_auds = 4 + 4*3 + 2*2 + 4*8 + 2*4; + const unsigned SIZE_strf_auds = 2*2 + 4*2 + 2*3; + const unsigned SIZE_strl_auds = 4+ 4+(4+SIZE_strh_auds) + 4+(4+SIZE_strf_auds); + + const unsigned SIZE_avih = 4*12; + const unsigned SIZE_hdrl = 4+4+ (4+SIZE_avih) + 4 + (4+SIZE_strl_vids) + 4 + (4+SIZE_strl_auds); + const unsigned SIZE_movi = 4 + nframes*(4+4+framesize); + const unsigned SIZE_avi = 4+4+ (4+SIZE_hdrl) + 4 + (4+SIZE_movi); + + const unsigned char AVIheader[] = + { + s4("RIFF"), + u32(SIZE_avi), + s4("AVI "), + + // HEADER + + s4("LIST"), + u32(SIZE_hdrl), + s4("hdrl"), + + s4("avih"), + u32(SIZE_avih), + u32(0), + u32(0), + u32(0), + u32(0), + u32(nframes), + u32(0), + u32(2), // two streams + u32(0), + u32(0), + u32(0), + u32(0), + u32(0), + + // VIDEO HEADER + + s4("LIST"), + u32(SIZE_strl_vids), + s4("strl"), + + s4("strh"), + u32(SIZE_strh_vids), + s4("vids"), + u32(0), + u32(0), + u16(0), + u16(0), + u32(0), + u32(scale), + u32(scaled_fps), + u32(0), + u32(0), + u32(0), + u32(0), + u32(0), + u16(0), + u16(0), + u16(0), + u16(0), + + s4("strf"), + u32(SIZE_strf_vids), + u32(0), + u32(width), + u32(height), + u16(0), + u16(0), + u32(fourcc), + u32(0), + u32(0), + u32(0), + u32(0), + u32(0), + + // AUDIO HEADER + + s4("LIST"), + u32(SIZE_strl_auds), + s4("strl"), + + s4("strh"), + u32(SIZE_strh_auds), + s4("auds"), + u32(0), //fourcc + u32(0), //handler + u32(0), //flags + u16(0), //prio + u16(0), //lang + u32(0), //init frames + u32(1), //scale + u32(aud_rate), + u32(0), //start + u32(0), //rate*length + u32(1048576), //suggested bufsize + u32(0), //quality + u32(aud_chans * (aud_bits / 8)), //sample size + u16(0), //frame size + u16(0), + u16(0), + u16(0), + + s4("strf"), + u32(SIZE_strf_auds), + u16(1), // pcm format + u16(aud_chans), + u32(aud_rate), + u32(aud_rate * aud_chans * (aud_bits/8)), // samples per second + u16(aud_chans * (aud_bits/8)), //block align + u16(aud_bits), //bits + u16(0), //cbSize + + // MOVIE + + s4("LIST"), + u32(SIZE_movi), + s4("movi") + }; + + FlushWrite(avifp, AVIheader, sizeof(AVIheader)); + } +} AVI; + +extern "C" +{ + int LoggingEnabled = 0; /* 0=no, 1=yes, 2=recording! */ + + const char* NESVideoGetVideoCmd() + { + return VIDEO_CMD.c_str(); + } + void NESVideoSetVideoCmd(const char *cmd) + { + VIDEO_CMD = cmd; + } + void NESVideoEnableDebugging( void videoMessageFunc(const char *msg), void audioMessageFunc(const char *msg) ) + { + debugVideoMessageFunc = videoMessageFunc; + debugAudioMessageFunc = audioMessageFunc; + } + void NESVideoSetFileFuncs( FILE* open(const char *,const char *), int close(FILE*) ) + { + openFunc = open; + closeFunc = close; + } + + void NESVideoLoggingVideo + (const void*data, unsigned width,unsigned height, + unsigned fps_scaled + ) + { + if(LoggingEnabled < 2) return; + + unsigned LogoFrames = fps_scaled >> 24; + + static bool First = true; + if(First) + { + First=false; + /* Bisqwit's logo addition routine. */ + /* If you don't have his files, this function does nothing + * and it does not matter at all. + */ + + const char *background = + width==320 ? "logo320_240" + : width==160 ? "logo160_144" + : width==240 ? "logo240_160" + : height>224 ? "logo256_240" + : "logo256_224"; + + /* Note: This should be 1 second long. */ + for(unsigned frame = 0; frame < LogoFrames; ++frame) + { + char Buf[4096]; + sprintf(Buf, "/shares/home/bisqwit/povray/nesvlogo/%s_f%u.tga", + background, frame); + + FILE*fp = fopen(Buf, "rb"); + if(!fp) // write blackness when missing frames to keep the intro 1 second long: + { + unsigned bytes = width*height*2; + unsigned char* buf = (unsigned char*)malloc(bytes); + if(buf) + { + memset(buf,0,bytes); + AVI.Video(width,height,fps_scaled, buf); + if(debugVideoMessageFunc) videoFramesWritten--; + free(buf); + } + } + else // write 1 frame of the logo: + { + int idlen = fgetc(fp); + /* Silently ignore all other header data. + * These files are assumed to be uncompressed BGR24 tga files with Y swapped. + * Even their geometry is assumed to match perfectly. + */ + fseek(fp, 1+1+2+2+1+ /*org*/2+2+ /*geo*/2+2+ 1+1+idlen, SEEK_CUR); + + bool yflip=true; + std::vector data(width*height*3); + for(unsigned y=height; y-->0; ) + fread(&data[y*width*3], 1, width*3, fp); + fclose(fp); + + std::vector result(width*height); + for(unsigned pos=0, max=result.size(); pos 0) + { + unsigned bytes = n*chans*(bits/8); + unsigned char* buf = (unsigned char*)malloc(bytes); + if(buf) + { + memset(buf,0,bytes); + AVI.Audio(rate,bits,chans, buf, n); + free(buf); + } + } + } + + AVI.Audio(rate,bits,chans, (const unsigned char*) data, nsamples); + } +} /* extern "C" */ + + + +static void FlushWrite(FILE* fp, const unsigned char*buf, unsigned length) +{ +/// unsigned failures = 0; +/// const static int FAILURE_THRESH = 8092; // don't want to loop infinitely if we keep failing to make progress - actually maybe you would want this, so the checking is disabled + while(length > 0 /*&& failures < FAILURE_THRESH*/) + { + unsigned written = fwrite(buf, 1, length, fp); +/// if(written == 0) +/// failures++; +/// else +/// { + length -= written; + buf += written; +/// failures = 0; +/// } + } +/// if(failures >= FAILURE_THRESH) +/// { +/// fprintf(stderr, "FlushWrite() failed to write %d bytes %d times - giving up.", length, failures); +/// LoggingEnabled = 0; +/// } +} + +// for the UB tech +#undef BGR24 +#undef BGR16 +#undef BGR15 + +#undef u32 +#undef u16 +#undef s4 diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/nesvideos-piece.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/nesvideos-piece.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,48 @@ +#ifndef NESVPIECEhh +#define NESVPIECEhh + +#define NESVIDEOS_LOGGING 1 + +#ifdef __cplusplus +extern "C" { +#endif + +/* Is video logging enabled? 0=no, 1=yes, 2=active. Default value: 0 */ +extern int LoggingEnabled; + +/* Get and set the video recording command (shell command) */ +extern const char* NESVideoGetVideoCmd(); +extern void NESVideoSetVideoCmd(const char *cmd); + +/* Tells to use these functions for obtaining/releasing FILE pointers for writing - if not specified, popen/pclose are used. */ +extern void NESVideoSetFileFuncs( FILE* openFunc(const char *,const char *), int closeFunc(FILE*) ); + +/* Tells to call these functions per frame with amounts (seconds and frames) of video and audio progress */ +extern void NESVideoEnableDebugging( void videoMessageFunc(const char *msg), void audioMessageFunc(const char *msg) ); + +/* Save 1 frame of video. (Assumed to be 16-bit RGB) */ +/* FPS is scaled by 24 bits (*0x1000000) */ +/* Does not do anything if LoggingEnabled<2. */ +extern void NESVideoLoggingVideo + (const void*data, unsigned width, unsigned height, + unsigned fps_scaled); + +/* Save N bytes of audio. bytes_per_second is required on the first call. */ +/* Does not do anything if LoggingEnabled<2. */ +/* The interval of calling this function is not important, as long as all the audio + * data is eventually written without too big delay (5 seconds is too big) + * This function may be called multiple times per video frame, or once per a few video + * frames, or anything in between. Just that all audio data must be written exactly once, + * and in order. */ +extern void NESVideoLoggingAudio + (const void*data, + unsigned rate, unsigned bits, unsigned chans, + unsigned nsamples); +/* nsamples*chans*(bits/8) = bytes in *data. */ +/* rate*chans*(bits/8) = bytes per second. */ + +#ifdef __cplusplus +} +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/unzip.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/unzip.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1208 @@ +/* unzip.c -- IO on .zip files using zlib + Version 0.15 beta, Mar 19th, 1998, + + Read unzip.h for more info + */ + +#include +#include +#include +#include "zlib.h" +#include "unzip.h" + +#ifdef NO_ERRNO_H +extern int errno; +#else +# include +#endif + +#ifndef local +# define local static +#endif +/* compile with -Dlocal if your debugger can't find static symbols */ + +#if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \ + !defined(CASESENSITIVITYDEFAULT_NO) +#define CASESENSITIVITYDEFAULT_NO +#endif + +#ifndef UNZ_BUFSIZE +#define UNZ_BUFSIZE (16384) +#endif + +#ifndef UNZ_MAXFILENAMEINZIP +#define UNZ_MAXFILENAMEINZIP (256) +#endif + +#ifndef ALLOC +# define ALLOC(size) (malloc(size)) +#endif +#ifndef TRYFREE +# define TRYFREE(p) {if (p) \ + free(p);} +#endif + +#define SIZECENTRALDIRITEM (0x2e) +#define SIZEZIPLOCALHEADER (0x1e) + +/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ + +#ifndef SEEK_CUR +#define SEEK_CUR 1 +#endif + +#ifndef SEEK_END +#define SEEK_END 2 +#endif + +#ifndef SEEK_SET +#define SEEK_SET 0 +#endif + +const char unz_copyright[] = + " unzip 0.15 Copyright 1998 Gilles Vollant "; + +/* unz_file_info_interntal contain internal info about a file in zipfile*/ +typedef struct unz_file_info_internal_s +{ + uLong offset_curfile; /* relative offset of local header 4 bytes */ +} unz_file_info_internal; + +/* file_in_zip_read_info_s contain internal information about a file in zipfile, + when reading and decompress it */ +typedef struct +{ + char * read_buffer; /* internal buffer for compressed data */ + z_stream stream; /* zLib stream structure for inflate */ + + uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/ + uLong stream_initialised; /* flag set if stream structure is initialised*/ + + uLong offset_local_extrafield; /* offset of the local extra field */ + uInt size_local_extrafield; /* size of the local extra field */ + uLong pos_local_extrafield; /* position in the local extra field in read*/ + + uLong crc32; /* crc32 of all data uncompressed */ + uLong crc32_wait; /* crc32 we must obtain after decompress all */ + uLong rest_read_compressed; /* number of byte to be decompressed */ + uLong rest_read_uncompressed; /*number of byte to be obtained after decomp*/ + FILE* file; /* io structore of the zipfile */ + uLong compression_method; /* compression method (0==store) */ + uLong byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx)*/ +} file_in_zip_read_info_s; + +/* unz_s contain internal information about the zipfile + */ +typedef struct +{ + FILE*file; /* io structore of the zipfile */ + unz_global_info gi; /* public global information */ + uLong byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx)*/ + uLong num_file; /* number of the current file in the zipfile*/ + uLong pos_in_central_dir; /* pos of the current file in the central dir*/ + uLong current_file_ok; /* flag about the usability of the current file*/ + uLong central_pos; /* position of the beginning of the central dir*/ + + uLong size_central_dir; /* size of the central directory */ + uLong offset_central_dir; /* offset of start of central directory with + respect to the starting disk number */ + + unz_file_info cur_file_info; /* public info about the current file in zip*/ + unz_file_info_internal cur_file_info_internal; /* private info about it*/ + file_in_zip_read_info_s*pfile_in_zip_read; /* structure about the current + file if we are decompressing it */ +} unz_s; + +/* =========================================================================== + Read a byte from a gz_stream; update next_in and avail_in. Return EOF + for end of file. + IN assertion: the stream s has been sucessfully opened for reading. + */ + +local int unzlocal_getByte(FILE *fin, int *pi) +{ + unsigned char c; + int err = fread(&c, 1, 1, fin); + if (err == 1) + { + *pi = (int)c; + return UNZ_OK; + } + else + { + if (ferror(fin)) + return UNZ_ERRNO; + else + return UNZ_EOF; + } +} + +/* =========================================================================== + Reads a long in LSB order from the given gz_stream. Sets + */ +local int unzlocal_getShort(FILE *fin, uLong *pX) +{ + uLong x ; + int i; + int err; + + err = unzlocal_getByte(fin, &i); + x = (uLong)i; + + if (err == UNZ_OK) + err = unzlocal_getByte(fin, &i); + x += ((uLong)i)<<8; + + if (err == UNZ_OK) + *pX = x; + else + *pX = 0; + return err; +} + +local int unzlocal_getLong(FILE *fin, uLong *pX) +{ + uLong x ; + int i; + int err; + + err = unzlocal_getByte(fin, &i); + x = (uLong)i; + + if (err == UNZ_OK) + err = unzlocal_getByte(fin, &i); + x += ((uLong)i)<<8; + + if (err == UNZ_OK) + err = unzlocal_getByte(fin, &i); + x += ((uLong)i)<<16; + + if (err == UNZ_OK) + err = unzlocal_getByte(fin, &i); + x += ((uLong)i)<<24; + + if (err == UNZ_OK) + *pX = x; + else + *pX = 0; + return err; +} + +/* My own strcmpi / strcasecmp */ +local int strcmpcasenosensitive_internal(const char *fileName1, + const char *fileName2) +{ + for (;;) + { + char c1 = *(fileName1++); + char c2 = *(fileName2++); + if ((c1 >= 'a') && (c1 <= 'z')) + c1 -= 0x20; + if ((c2 >= 'a') && (c2 <= 'z')) + c2 -= 0x20; + if (c1 == '\0') + return ((c2 == '\0') ? 0 : -1); + if (c2 == '\0') + return 1; + if (c1 < c2) + return -1; + if (c1 > c2) + return 1; + } +} + +#ifdef CASESENSITIVITYDEFAULT_NO +#define CASESENSITIVITYDEFAULTVALUE 2 +#else +#define CASESENSITIVITYDEFAULTVALUE 1 +#endif + +#ifndef STRCMPCASENOSENTIVEFUNCTION +#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal +#endif + +/* + Compare two filename (fileName1,fileName2). + If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) + If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi + or strcasecmp) + If iCaseSenisivity = 0, case sensitivity is defaut of your operating system + (like 1 on Unix, 2 on Windows) + + */ +extern int ZEXPORT unzStringFileNameCompare(const char *fileName1, + const char *fileName2, + int iCaseSensitivity) +{ + if (iCaseSensitivity == 0) + iCaseSensitivity = CASESENSITIVITYDEFAULTVALUE; + + if (iCaseSensitivity == 1) + return strcmp(fileName1, fileName2); + + return STRCMPCASENOSENTIVEFUNCTION(fileName1, fileName2); +} + +#define BUFREADCOMMENT (0x400) + +/* + Locate the Central directory of a zipfile (at the end, just before + the global comment) + */ +local uLong unzlocal_SearchCentralDir(FILE *fin) +{ + unsigned char*buf; + uLong uSizeFile; + uLong uBackRead; + uLong uMaxBack = 0xffff; /* maximum size of global comment */ + uLong uPosFound = 0; + + if (fseek(fin, 0, SEEK_END) != 0) + return 0; + + uSizeFile = ftell(fin); + + if (uMaxBack > uSizeFile) + uMaxBack = uSizeFile; + + buf = (unsigned char *)ALLOC(BUFREADCOMMENT+4); + if (buf == NULL) + return 0; + + uBackRead = 4; + while (uBackRead < uMaxBack) + { + uLong uReadSize, uReadPos ; + int i; + if (uBackRead+BUFREADCOMMENT > uMaxBack) + uBackRead = uMaxBack; + else + uBackRead += BUFREADCOMMENT; + uReadPos = uSizeFile-uBackRead ; + + uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? + (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); + if (fseek(fin, uReadPos, SEEK_SET) != 0) + break; + + if (fread(buf, (uInt)uReadSize, 1, fin) != 1) + break; + + for (i = (int)uReadSize-3; (i--) > 0;) + if (((*(buf+i)) == 0x50) && ((*(buf+i+1)) == 0x4b) && + ((*(buf+i+2)) == 0x05) && ((*(buf+i+3)) == 0x06)) + { + uPosFound = uReadPos+i; + break; + } + + if (uPosFound != 0) + break; + } + TRYFREE(buf); + return uPosFound; +} + +/* + Open a Zip file. path contain the full pathname (by example, + on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer + "zlib/zlib109.zip". + If the zipfile cannot be opened (file don't exist or in not valid), the + return value is NULL. + Else, the return value is a unzFile Handle, usable with other function + of this unzip package. + */ +extern unzFile ZEXPORT unzOpen(const char *path) +{ + unz_s us; + unz_s *s; + uLong central_pos, uL; + FILE * fin ; + + uLong number_disk; /* number of the current dist, used for + spaning ZIP, unsupported, always 0*/ + uLong number_disk_with_CD; /* number the the disk with central dir, used + for spaning ZIP, unsupported, always 0*/ + uLong number_entry_CD; /* total number of entries in + the central dir + (same than number_entry on nospan) */ + + int err = UNZ_OK; + + if (unz_copyright[0] != ' ') + return NULL; + + fin = fopen(path, "rb"); + if (fin == NULL) + return NULL; + + central_pos = unzlocal_SearchCentralDir(fin); + if (central_pos == 0) + err = UNZ_ERRNO; + + if (fseek(fin, central_pos, SEEK_SET) != 0) + err = UNZ_ERRNO; + + /* the signature, already checked */ + if (unzlocal_getLong(fin, &uL) != UNZ_OK) + err = UNZ_ERRNO; + + /* number of this disk */ + if (unzlocal_getShort(fin, &number_disk) != UNZ_OK) + err = UNZ_ERRNO; + + /* number of the disk with the start of the central directory */ + if (unzlocal_getShort(fin, &number_disk_with_CD) != UNZ_OK) + err = UNZ_ERRNO; + + /* total number of entries in the central dir on this disk */ + if (unzlocal_getShort(fin, &us.gi.number_entry) != UNZ_OK) + err = UNZ_ERRNO; + + /* total number of entries in the central dir */ + if (unzlocal_getShort(fin, &number_entry_CD) != UNZ_OK) + err = UNZ_ERRNO; + + if ((number_entry_CD != us.gi.number_entry) || + (number_disk_with_CD != 0) || + (number_disk != 0)) + err = UNZ_BADZIPFILE; + + /* size of the central directory */ + if (unzlocal_getLong(fin, &us.size_central_dir) != UNZ_OK) + err = UNZ_ERRNO; + + /* offset of start of central directory with respect to the + starting disk number */ + if (unzlocal_getLong(fin, &us.offset_central_dir) != UNZ_OK) + err = UNZ_ERRNO; + + /* zipfile comment length */ + if (unzlocal_getShort(fin, &us.gi.size_comment) != UNZ_OK) + err = UNZ_ERRNO; + + if ((central_pos < us.offset_central_dir+us.size_central_dir) && + (err == UNZ_OK)) + err = UNZ_BADZIPFILE; + + if (err != UNZ_OK) + { + fclose(fin); + return NULL; + } + + us.file = fin; + us.byte_before_the_zipfile = central_pos - + (us.offset_central_dir+us.size_central_dir); + us.central_pos = central_pos; + us.pfile_in_zip_read = NULL; + + s = (unz_s *)ALLOC(sizeof(unz_s)); + *s = us; + unzGoToFirstFile((unzFile)s); + return (unzFile)s; +} + +/* + Close a ZipFile opened with unzipOpen. + If there is files inside the .Zip opened with unzipOpenCurrentFile (see later), + these files MUST be closed with unzipCloseCurrentFile before call unzipClose. + return UNZ_OK if there is no problem. */ +extern int ZEXPORT unzClose(unzFile file) +{ + unz_s*s; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + + if (s->pfile_in_zip_read != NULL) + unzCloseCurrentFile(file); + + fclose(s->file); + TRYFREE(s); + return UNZ_OK; +} + +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. */ +extern int ZEXPORT unzGetGlobalInfo(unzFile file, + unz_global_info *pglobal_info) +{ + unz_s*s; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + *pglobal_info = s->gi; + return UNZ_OK; +} + +/* + Translate date/time from Dos format to tm_unz (readable more easilty) + */ +local void unzlocal_DosDateToTmuDate(uLong ulDosDate, tm_unz *ptm) +{ + uLong uDate; + uDate = (uLong)(ulDosDate>>16); + ptm->tm_mday = (uInt)(uDate&0x1f) ; + ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; + ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; + + ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); + ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; + ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; +} + +/* + Get Info about the current file in the zipfile, with internal only info + */ +local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file, + unz_file_info *pfile_info, + unz_file_info_internal + *pfile_info_internal, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize)); + +local int unzlocal_GetCurrentFileInfoInternal(unzFile file, + unz_file_info *pfile_info, + unz_file_info_internal *pfile_info_internal, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize) +{ + unz_s* s; + unz_file_info file_info; + unz_file_info_internal file_info_internal; + int err = UNZ_OK; + uLong uMagic; + long lSeek = 0; + + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + if (fseek(s->file, s->pos_in_central_dir+s->byte_before_the_zipfile, SEEK_SET) != 0) + err = UNZ_ERRNO; + + /* we check the magic */ + if (err == UNZ_OK) + if (unzlocal_getLong(s->file, &uMagic) != UNZ_OK) + err = UNZ_ERRNO; + else if (uMagic != 0x02014b50) + err = UNZ_BADZIPFILE; + + if (unzlocal_getShort(s->file, &file_info.version) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &file_info.version_needed) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &file_info.flag) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &file_info.compression_method) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getLong(s->file, &file_info.dosDate) != UNZ_OK) + err = UNZ_ERRNO; + + unzlocal_DosDateToTmuDate(file_info.dosDate, &file_info.tmu_date); + + if (unzlocal_getLong(s->file, &file_info.crc) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getLong(s->file, &file_info.compressed_size) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getLong(s->file, &file_info.uncompressed_size) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &file_info.size_filename) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &file_info.size_file_extra) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &file_info.size_file_comment) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &file_info.disk_num_start) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &file_info.internal_fa) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getLong(s->file, &file_info.external_fa) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getLong(s->file, &file_info_internal.offset_curfile) != UNZ_OK) + err = UNZ_ERRNO; + + lSeek += file_info.size_filename; + if ((err == UNZ_OK) && (szFileName != NULL)) + { + uLong uSizeRead ; + if (file_info.size_filename < fileNameBufferSize) + { + *(szFileName+file_info.size_filename) = '\0'; + uSizeRead = file_info.size_filename; + } + else + uSizeRead = fileNameBufferSize; + + if ((file_info.size_filename > 0) && (fileNameBufferSize > 0)) + if (fread(szFileName, (uInt)uSizeRead, 1, s->file) != 1) + err = UNZ_ERRNO; + lSeek -= uSizeRead; + } + + if ((err == UNZ_OK) && (extraField != NULL)) + { + uLong uSizeRead ; + if (file_info.size_file_extra < extraFieldBufferSize) + uSizeRead = file_info.size_file_extra; + else + uSizeRead = extraFieldBufferSize; + + if (lSeek != 0) + if (fseek(s->file, lSeek, SEEK_CUR) == 0) + lSeek = 0; + else + err = UNZ_ERRNO; + if ((file_info.size_file_extra > 0) && (extraFieldBufferSize > 0)) + if (fread(extraField, (uInt)uSizeRead, 1, s->file) != 1) + err = UNZ_ERRNO; + lSeek += file_info.size_file_extra - uSizeRead; + } + else + lSeek += file_info.size_file_extra; + + if ((err == UNZ_OK) && (szComment != NULL)) + { + uLong uSizeRead ; + if (file_info.size_file_comment < commentBufferSize) + { + *(szComment+file_info.size_file_comment) = '\0'; + uSizeRead = file_info.size_file_comment; + } + else + uSizeRead = commentBufferSize; + + if (lSeek != 0) + if (fseek(s->file, lSeek, SEEK_CUR) == 0) + lSeek = 0; + else + err = UNZ_ERRNO; + if ((file_info.size_file_comment > 0) && (commentBufferSize > 0)) + if (fread(szComment, (uInt)uSizeRead, 1, s->file) != 1) + err = UNZ_ERRNO; + lSeek += file_info.size_file_comment - uSizeRead; + } + else + lSeek += file_info.size_file_comment; + + if ((err == UNZ_OK) && (pfile_info != NULL)) + *pfile_info = file_info; + + if ((err == UNZ_OK) && (pfile_info_internal != NULL)) + *pfile_info_internal = file_info_internal; + + return err; +} + +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. + */ +extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, + unz_file_info *pfile_info, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize) +{ + return unzlocal_GetCurrentFileInfoInternal(file, pfile_info, NULL, + szFileName, fileNameBufferSize, + extraField, extraFieldBufferSize, + szComment, commentBufferSize); +} + +/* + Set the current file of the zipfile to the first file. + return UNZ_OK if there is no problem + */ +extern int ZEXPORT unzGoToFirstFile(unzFile file) +{ + int err = UNZ_OK; + unz_s*s; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + s->pos_in_central_dir = s->offset_central_dir; + s->num_file = 0; + err = unzlocal_GetCurrentFileInfoInternal(file, &s->cur_file_info, + &s->cur_file_info_internal, + NULL, 0, NULL, 0, NULL, 0); + s->current_file_ok = (err == UNZ_OK); + return err; +} + +/* + Set the current file of the zipfile to the next file. + return UNZ_OK if there is no problem + return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. + */ +extern int ZEXPORT unzGoToNextFile(unzFile file) +{ + unz_s*s; + int err; + + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + if (!s->current_file_ok) + return UNZ_END_OF_LIST_OF_FILE; + if (s->num_file+1 == s->gi.number_entry) + return UNZ_END_OF_LIST_OF_FILE; + + s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + + s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; + s->num_file++; + err = unzlocal_GetCurrentFileInfoInternal(file, &s->cur_file_info, + &s->cur_file_info_internal, + NULL, 0, NULL, 0, NULL, 0); + s->current_file_ok = (err == UNZ_OK); + return err; +} + +/* + Try locate the file szFileName in the zipfile. + For the iCaseSensitivity signification, see unzipStringFileNameCompare + + return value : + UNZ_OK if the file is found. It becomes the current file. + UNZ_END_OF_LIST_OF_FILE if the file is not found + */ +extern int ZEXPORT unzLocateFile(unzFile file, + const char *szFileName, + int iCaseSensitivity) +{ + unz_s*s; + int err; + + uLong num_fileSaved; + uLong pos_in_central_dirSaved; + + if (file == NULL) + return UNZ_PARAMERROR; + + if (strlen(szFileName) >= UNZ_MAXFILENAMEINZIP) + return UNZ_PARAMERROR; + + s = (unz_s *)file; + if (!s->current_file_ok) + return UNZ_END_OF_LIST_OF_FILE; + + num_fileSaved = s->num_file; + pos_in_central_dirSaved = s->pos_in_central_dir; + + err = unzGoToFirstFile(file); + + while (err == UNZ_OK) + { + char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; + unzGetCurrentFileInfo(file, NULL, + szCurrentFileName, sizeof(szCurrentFileName)-1, + NULL, 0, NULL, 0); + if (unzStringFileNameCompare(szCurrentFileName, + szFileName, iCaseSensitivity) == 0) + return UNZ_OK; + err = unzGoToNextFile(file); + } + + s->num_file = num_fileSaved ; + s->pos_in_central_dir = pos_in_central_dirSaved ; + return err; +} + +/* + Read the local header of the current zipfile + Check the coherency of the local header and info in the end of central + directory about this file + store in *piSizeVar the size of extra info in local header + (filename and size of extra field data) + */ +local int unzlocal_CheckCurrentFileCoherencyHeader(unz_s *s, + uInt *piSizeVar, + uLong *poffset_local_extrafield, + uInt *psize_local_extrafield) +{ + uLong uMagic, uData, uFlags; + uLong size_filename; + uLong size_extra_field; + int err = UNZ_OK; + + *piSizeVar = 0; + *poffset_local_extrafield = 0; + *psize_local_extrafield = 0; + + if (fseek(s->file, s->cur_file_info_internal.offset_curfile + + s->byte_before_the_zipfile, SEEK_SET) != 0) + return UNZ_ERRNO; + + if (err == UNZ_OK) + if (unzlocal_getLong(s->file, &uMagic) != UNZ_OK) + err = UNZ_ERRNO; + else if (uMagic != 0x04034b50) + err = UNZ_BADZIPFILE; + + if (unzlocal_getShort(s->file, &uData) != UNZ_OK) + err = UNZ_ERRNO; +/* + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) + err=UNZ_BADZIPFILE; + */ + if (unzlocal_getShort(s->file, &uFlags) != UNZ_OK) + err = UNZ_ERRNO; + + if (unzlocal_getShort(s->file, &uData) != UNZ_OK) + err = UNZ_ERRNO; + else if ((err == UNZ_OK) && (uData != s->cur_file_info.compression_method)) + err = UNZ_BADZIPFILE; + + if ((err == UNZ_OK) && (s->cur_file_info.compression_method != 0) && + (s->cur_file_info.compression_method != Z_DEFLATED)) + err = UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file, &uData) != UNZ_OK) /* date/time */ + err = UNZ_ERRNO; + + if (unzlocal_getLong(s->file, &uData) != UNZ_OK) /* crc */ + err = UNZ_ERRNO; + else if ((err == UNZ_OK) && (uData != s->cur_file_info.crc) && + ((uFlags & 8) == 0)) + err = UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file, &uData) != UNZ_OK) /* size compr */ + err = UNZ_ERRNO; + else if ((err == UNZ_OK) && (uData != s->cur_file_info.compressed_size) && + ((uFlags & 8) == 0)) + err = UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file, &uData) != UNZ_OK) /* size uncompr */ + err = UNZ_ERRNO; + else if ((err == UNZ_OK) && (uData != s->cur_file_info.uncompressed_size) && + ((uFlags & 8) == 0)) + err = UNZ_BADZIPFILE; + + if (unzlocal_getShort(s->file, &size_filename) != UNZ_OK) + err = UNZ_ERRNO; + else if ((err == UNZ_OK) && (size_filename != s->cur_file_info.size_filename)) + err = UNZ_BADZIPFILE; + + *piSizeVar += (uInt)size_filename; + + if (unzlocal_getShort(s->file, &size_extra_field) != UNZ_OK) + err = UNZ_ERRNO; + *poffset_local_extrafield = s->cur_file_info_internal.offset_curfile + + SIZEZIPLOCALHEADER + size_filename; + *psize_local_extrafield = (uInt)size_extra_field; + + *piSizeVar += (uInt)size_extra_field; + + return err; +} + +/* + Open for reading data the current file in the zipfile. + If there is no error and the file is opened, the return value is UNZ_OK. + */ +extern int ZEXPORT unzOpenCurrentFile(unzFile file) +{ + int err = UNZ_OK; + int Store; + uInt iSizeVar; + unz_s*s; + file_in_zip_read_info_s*pfile_in_zip_read_info; + uLong offset_local_extrafield; /* offset of the local extra field */ + uInt size_local_extrafield; /* size of the local extra field */ + + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + if (!s->current_file_ok) + return UNZ_PARAMERROR; + + if (s->pfile_in_zip_read != NULL) + unzCloseCurrentFile(file); + + if (unzlocal_CheckCurrentFileCoherencyHeader(s, &iSizeVar, + &offset_local_extrafield, &size_local_extrafield) != UNZ_OK) + return UNZ_BADZIPFILE; + + pfile_in_zip_read_info = (file_in_zip_read_info_s *) + ALLOC(sizeof(file_in_zip_read_info_s)); + if (pfile_in_zip_read_info == NULL) + return UNZ_INTERNALERROR; + + pfile_in_zip_read_info->read_buffer = (char *)ALLOC(UNZ_BUFSIZE); + pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; + pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; + pfile_in_zip_read_info->pos_local_extrafield = 0; + + if (pfile_in_zip_read_info->read_buffer == NULL) + { + TRYFREE(pfile_in_zip_read_info); + return UNZ_INTERNALERROR; + } + + pfile_in_zip_read_info->stream_initialised = 0; + + if ((s->cur_file_info.compression_method != 0) && + (s->cur_file_info.compression_method != Z_DEFLATED)) + err = UNZ_BADZIPFILE; + Store = s->cur_file_info.compression_method == 0; + + pfile_in_zip_read_info->crc32_wait = s->cur_file_info.crc; + pfile_in_zip_read_info->crc32 = 0; + pfile_in_zip_read_info->compression_method = + s->cur_file_info.compression_method; + pfile_in_zip_read_info->file = s->file; + pfile_in_zip_read_info->byte_before_the_zipfile = s->byte_before_the_zipfile; + + pfile_in_zip_read_info->stream.total_out = 0; + + if (!Store) + { + pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; + pfile_in_zip_read_info->stream.zfree = (free_func)0; + pfile_in_zip_read_info->stream.opaque = (voidpf)0; + + err = inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); + if (err == Z_OK) + pfile_in_zip_read_info->stream_initialised = 1; + /* windowBits is passed < 0 to tell that there is no zlib header. + * Note that in this case inflate *requires* an extra "dummy" byte + * after the compressed stream in order to complete decompression and + * return Z_STREAM_END. + * In unzip, i don't wait absolutely Z_STREAM_END because I known the + * size of both compressed and uncompressed data + */ + } + pfile_in_zip_read_info->rest_read_compressed = + s->cur_file_info.compressed_size ; + pfile_in_zip_read_info->rest_read_uncompressed = + s->cur_file_info.uncompressed_size ; + + pfile_in_zip_read_info->pos_in_zipfile = + s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + + iSizeVar; + + pfile_in_zip_read_info->stream.avail_in = (uInt)0; + + s->pfile_in_zip_read = pfile_in_zip_read_info; + return UNZ_OK; +} + +/* + Read bytes from the current file. + buf contain buffer where data must be copied + len the size of buf. + + return the number of byte copied if somes bytes are copied + return 0 if the end of file was reached + return <0 with error code if there is an error + (UNZ_ERRNO for IO error, or zLib error for uncompress error) + */ +extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len) +{ + int err = UNZ_OK; + uInt iRead = 0; + unz_s*s; + file_in_zip_read_info_s*pfile_in_zip_read_info; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + pfile_in_zip_read_info = s->pfile_in_zip_read; + + if (pfile_in_zip_read_info == NULL) + return UNZ_PARAMERROR; + + if ((pfile_in_zip_read_info->read_buffer == NULL)) + return UNZ_END_OF_LIST_OF_FILE; + if (len == 0) + return 0; + + pfile_in_zip_read_info->stream.next_out = (Bytef *)buf; + + pfile_in_zip_read_info->stream.avail_out = (uInt)len; + + if (len > pfile_in_zip_read_info->rest_read_uncompressed) + pfile_in_zip_read_info->stream.avail_out = + (uInt)pfile_in_zip_read_info->rest_read_uncompressed; + + while (pfile_in_zip_read_info->stream.avail_out > 0) + { + if ((pfile_in_zip_read_info->stream.avail_in == 0) && + (pfile_in_zip_read_info->rest_read_compressed > 0)) + { + uInt uReadThis = UNZ_BUFSIZE; + if (pfile_in_zip_read_info->rest_read_compressed < uReadThis) + uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed; + if (uReadThis == 0) + return UNZ_EOF; + if (fseek(pfile_in_zip_read_info->file, + pfile_in_zip_read_info->pos_in_zipfile + + pfile_in_zip_read_info->byte_before_the_zipfile, SEEK_SET) != 0) + return UNZ_ERRNO; + if (fread(pfile_in_zip_read_info->read_buffer, uReadThis, 1, + pfile_in_zip_read_info->file) != 1) + return UNZ_ERRNO; + pfile_in_zip_read_info->pos_in_zipfile += uReadThis; + + pfile_in_zip_read_info->rest_read_compressed -= uReadThis; + + pfile_in_zip_read_info->stream.next_in = + (Bytef *)pfile_in_zip_read_info->read_buffer; + pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; + } + + if (pfile_in_zip_read_info->compression_method == 0) + { + uInt uDoCopy, i ; + if (pfile_in_zip_read_info->stream.avail_out < + pfile_in_zip_read_info->stream.avail_in) + uDoCopy = pfile_in_zip_read_info->stream.avail_out ; + else + uDoCopy = pfile_in_zip_read_info->stream.avail_in ; + + for (i = 0; i < uDoCopy; i++) + *(pfile_in_zip_read_info->stream.next_out+i) = + *(pfile_in_zip_read_info->stream.next_in+i); + + pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, + pfile_in_zip_read_info->stream.next_out, + uDoCopy); + pfile_in_zip_read_info->rest_read_uncompressed -= uDoCopy; + pfile_in_zip_read_info->stream.avail_in -= uDoCopy; + pfile_in_zip_read_info->stream.avail_out -= uDoCopy; + pfile_in_zip_read_info->stream.next_out += uDoCopy; + pfile_in_zip_read_info->stream.next_in += uDoCopy; + pfile_in_zip_read_info->stream.total_out += uDoCopy; + iRead += uDoCopy; + } + else + { + uLong uTotalOutBefore, uTotalOutAfter; + const Bytef *bufBefore; + uLong uOutThis; + int flush = Z_SYNC_FLUSH; + + uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; + bufBefore = pfile_in_zip_read_info->stream.next_out; + + /* + if ((pfile_in_zip_read_info->rest_read_uncompressed == + pfile_in_zip_read_info->stream.avail_out) && + (pfile_in_zip_read_info->rest_read_compressed == 0)) + flush = Z_FINISH; + */ + err = inflate(&pfile_in_zip_read_info->stream, flush); + + uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; + uOutThis = uTotalOutAfter-uTotalOutBefore; + + pfile_in_zip_read_info->crc32 = + crc32(pfile_in_zip_read_info->crc32, bufBefore, + (uInt)(uOutThis)); + + pfile_in_zip_read_info->rest_read_uncompressed -= + uOutThis; + + iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); + + if (err == Z_STREAM_END) + return (iRead == 0) ? UNZ_EOF : iRead; + if (err != Z_OK) + break; + } + } + + if (err == Z_OK) + return iRead; + return err; +} + +/* + Give the current position in uncompressed data + */ +extern z_off_t ZEXPORT unztell(unzFile file) +{ + unz_s*s; + file_in_zip_read_info_s*pfile_in_zip_read_info; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + pfile_in_zip_read_info = s->pfile_in_zip_read; + + if (pfile_in_zip_read_info == NULL) + return UNZ_PARAMERROR; + + return (z_off_t)pfile_in_zip_read_info->stream.total_out; +} + +/* + return 1 if the end of file was reached, 0 elsewhere + */ +extern int ZEXPORT unzeof(unzFile file) +{ + unz_s*s; + file_in_zip_read_info_s*pfile_in_zip_read_info; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + pfile_in_zip_read_info = s->pfile_in_zip_read; + + if (pfile_in_zip_read_info == NULL) + return UNZ_PARAMERROR; + + if (pfile_in_zip_read_info->rest_read_uncompressed == 0) + return 1; + else + return 0; +} + +/* + Read extra field from the current file (opened by unzOpenCurrentFile) + This is the local-header version of the extra field (sometimes, there is + more info in the local-header version than in the central-header) + + if buf==NULL, it return the size of the local extra field that can be read + + if buf!=NULL, len is the size of the buffer, the extra header is copied in + buf. + the return value is the number of bytes copied in buf, or (if <0) + the error code + */ +extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len) +{ + unz_s*s; + file_in_zip_read_info_s*pfile_in_zip_read_info; + uInt read_now; + uLong size_to_read; + + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + pfile_in_zip_read_info = s->pfile_in_zip_read; + + if (pfile_in_zip_read_info == NULL) + return UNZ_PARAMERROR; + + size_to_read = (pfile_in_zip_read_info->size_local_extrafield - + pfile_in_zip_read_info->pos_local_extrafield); + + if (buf == NULL) + return (int)size_to_read; + + if (len > size_to_read) + read_now = (uInt)size_to_read; + else + read_now = (uInt)len ; + + if (read_now == 0) + return 0; + + if (fseek(pfile_in_zip_read_info->file, + pfile_in_zip_read_info->offset_local_extrafield + + pfile_in_zip_read_info->pos_local_extrafield, SEEK_SET) != 0) + return UNZ_ERRNO; + + if (fread(buf, (uInt)size_to_read, 1, pfile_in_zip_read_info->file) != 1) + return UNZ_ERRNO; + + return (int)read_now; +} + +/* + Close the file in zip opened with unzipOpenCurrentFile + Return UNZ_CRCERROR if all the file was read but the CRC is not good + */ +extern int ZEXPORT unzCloseCurrentFile(unzFile file) +{ + int err = UNZ_OK; + + unz_s*s; + file_in_zip_read_info_s*pfile_in_zip_read_info; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + pfile_in_zip_read_info = s->pfile_in_zip_read; + + if (pfile_in_zip_read_info == NULL) + return UNZ_PARAMERROR; + + if (pfile_in_zip_read_info->rest_read_uncompressed == 0) + { + if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) + err = UNZ_CRCERROR; + } + + TRYFREE(pfile_in_zip_read_info->read_buffer); + pfile_in_zip_read_info->read_buffer = NULL; + if (pfile_in_zip_read_info->stream_initialised) + inflateEnd(&pfile_in_zip_read_info->stream); + + pfile_in_zip_read_info->stream_initialised = 0; + TRYFREE(pfile_in_zip_read_info); + + s->pfile_in_zip_read = NULL; + + return err; +} + +/* + Get the global comment string of the ZipFile, in the szComment buffer. + uSizeBuf is the size of the szComment buffer. + return the number of byte copied or an error code <0 + */ +extern int ZEXPORT unzGetGlobalComment(unzFile file, + char *szComment, + uLong uSizeBuf) +{ + //int err=UNZ_OK; + unz_s*s; + uLong uReadThis ; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz_s *)file; + + uReadThis = uSizeBuf; + if (uReadThis > s->gi.size_comment) + uReadThis = s->gi.size_comment; + + if (fseek(s->file, s->central_pos+22, SEEK_SET) != 0) + return UNZ_ERRNO; + + if (uReadThis > 0) + { + *szComment = '\0'; + if (fread(szComment, (uInt)uReadThis, 1, s->file) != 1) + return UNZ_ERRNO; + } + + if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment)) + *(szComment+s->gi.size_comment) = '\0'; + return (int)uReadThis; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/unzip.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/unzip.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,275 @@ +/* unzip.h -- IO for uncompress .zip files using zlib + Version 0.15 beta, Mar 19th, 1998, + + Copyright (C) 1998 Gilles Vollant + + This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g + WinZip, InfoZip tools and compatible. + Encryption and multi volume ZipFile (span) are not supported. + Old compressions used by old PKZip 1.x are not supported + + THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE + CAN CHANGE IN FUTURE VERSION !! + I WAIT FEEDBACK at mail info@winimage.com + Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution + + Condition of use and distribution are the same than zlib : + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + +*/ +/* for more info about .ZIP format, see + ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip + PkWare has also a specification at : + ftp://ftp.pkware.com/probdesc.zip */ + +#ifndef _unz_H +#define _unz_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _ZLIB_H +#include "zlib.h" +#endif + +#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP) +/* like the STRICT of WIN32, we define a pointer that cannot be converted + from (void*) without cast */ +typedef struct TagunzFile__ { int unused; } unzFile__; +typedef unzFile__ *unzFile; +#else +typedef voidp unzFile; +#endif + + +#define UNZ_OK (0) +#define UNZ_END_OF_LIST_OF_FILE (-100) +#define UNZ_ERRNO (Z_ERRNO) +#define UNZ_EOF (0) +#define UNZ_PARAMERROR (-102) +#define UNZ_BADZIPFILE (-103) +#define UNZ_INTERNALERROR (-104) +#define UNZ_CRCERROR (-105) + +/* tm_unz contain date/time info */ +typedef struct tm_unz_s +{ + uInt tm_sec; /* seconds after the minute - [0,59] */ + uInt tm_min; /* minutes after the hour - [0,59] */ + uInt tm_hour; /* hours since midnight - [0,23] */ + uInt tm_mday; /* day of the month - [1,31] */ + uInt tm_mon; /* months since January - [0,11] */ + uInt tm_year; /* years - [1980..2044] */ +} tm_unz; + +/* unz_global_info structure contain global data about the ZIPfile + These data comes from the end of central dir */ +typedef struct unz_global_info_s +{ + uLong number_entry; /* total number of entries in + the central dir on this disk */ + uLong size_comment; /* size of the global comment of the zipfile */ +} unz_global_info; + + +/* unz_file_info contain information about a file in the zipfile */ +typedef struct unz_file_info_s +{ + uLong version; /* version made by 2 bytes */ + uLong version_needed; /* version needed to extract 2 bytes */ + uLong flag; /* general purpose bit flag 2 bytes */ + uLong compression_method; /* compression method 2 bytes */ + uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ + uLong crc; /* crc-32 4 bytes */ + uLong compressed_size; /* compressed size 4 bytes */ + uLong uncompressed_size; /* uncompressed size 4 bytes */ + uLong size_filename; /* filename length 2 bytes */ + uLong size_file_extra; /* extra field length 2 bytes */ + uLong size_file_comment; /* file comment length 2 bytes */ + + uLong disk_num_start; /* disk number start 2 bytes */ + uLong internal_fa; /* internal file attributes 2 bytes */ + uLong external_fa; /* external file attributes 4 bytes */ + + tm_unz tmu_date; +} unz_file_info; + +extern int ZEXPORT unzStringFileNameCompare OF ((const char* fileName1, + const char* fileName2, + int iCaseSensitivity)); +/* + Compare two filename (fileName1,fileName2). + If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) + If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi + or strcasecmp) + If iCaseSenisivity = 0, case sensitivity is defaut of your operating system + (like 1 on Unix, 2 on Windows) +*/ + + +extern unzFile ZEXPORT unzOpen OF((const char *path)); +/* + Open a Zip file. path contain the full pathname (by example, + on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer + "zlib/zlib111.zip". + If the zipfile cannot be opened (file don't exist or in not valid), the + return value is NULL. + Else, the return value is a unzFile Handle, usable with other function + of this unzip package. +*/ + +extern int ZEXPORT unzClose OF((unzFile file)); +/* + Close a ZipFile opened with unzipOpen. + If there is files inside the .Zip opened with unzOpenCurrentFile (see later), + these files MUST be closed with unzipCloseCurrentFile before call unzipClose. + return UNZ_OK if there is no problem. */ + +extern int ZEXPORT unzGetGlobalInfo OF((unzFile file, + unz_global_info *pglobal_info)); +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. */ + + +extern int ZEXPORT unzGetGlobalComment OF((unzFile file, + char *szComment, + uLong uSizeBuf)); +/* + Get the global comment string of the ZipFile, in the szComment buffer. + uSizeBuf is the size of the szComment buffer. + return the number of byte copied or an error code <0 +*/ + + +/***************************************************************************/ +/* Unzip package allow you browse the directory of the zipfile */ + +extern int ZEXPORT unzGoToFirstFile OF((unzFile file)); +/* + Set the current file of the zipfile to the first file. + return UNZ_OK if there is no problem +*/ + +extern int ZEXPORT unzGoToNextFile OF((unzFile file)); +/* + Set the current file of the zipfile to the next file. + return UNZ_OK if there is no problem + return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. +*/ + +extern int ZEXPORT unzLocateFile OF((unzFile file, + const char *szFileName, + int iCaseSensitivity)); +/* + Try locate the file szFileName in the zipfile. + For the iCaseSensitivity signification, see unzStringFileNameCompare + + return value : + UNZ_OK if the file is found. It becomes the current file. + UNZ_END_OF_LIST_OF_FILE if the file is not found +*/ + + +extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file, + unz_file_info *pfile_info, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize)); +/* + Get Info about the current file + if pfile_info!=NULL, the *pfile_info structure will contain somes info about + the current file + if szFileName!=NULL, the filemane string will be copied in szFileName + (fileNameBufferSize is the size of the buffer) + if extraField!=NULL, the extra field information will be copied in extraField + (extraFieldBufferSize is the size of the buffer). + This is the Central-header version of the extra field + if szComment!=NULL, the comment string of the file will be copied in szComment + (commentBufferSize is the size of the buffer) +*/ + +/***************************************************************************/ +/* for reading the content of the current zipfile, you can open it, read data + from it, and close it (you can close it before reading all the file) + */ + +extern int ZEXPORT unzOpenCurrentFile OF((unzFile file)); +/* + Open for reading data the current file in the zipfile. + If there is no error, the return value is UNZ_OK. +*/ + +extern int ZEXPORT unzCloseCurrentFile OF((unzFile file)); +/* + Close the file in zip opened with unzOpenCurrentFile + Return UNZ_CRCERROR if all the file was read but the CRC is not good +*/ + + +extern int ZEXPORT unzReadCurrentFile OF((unzFile file, + voidp buf, + unsigned len)); +/* + Read bytes from the current file (opened by unzOpenCurrentFile) + buf contain buffer where data must be copied + len the size of buf. + + return the number of byte copied if somes bytes are copied + return 0 if the end of file was reached + return <0 with error code if there is an error + (UNZ_ERRNO for IO error, or zLib error for uncompress error) +*/ + +extern z_off_t ZEXPORT unztell OF((unzFile file)); +/* + Give the current position in uncompressed data +*/ + +extern int ZEXPORT unzeof OF((unzFile file)); +/* + return 1 if the end of file was reached, 0 elsewhere +*/ + +extern int ZEXPORT unzGetLocalExtrafield OF((unzFile file, + voidp buf, + unsigned len)); +/* + Read extra field from the current file (opened by unzOpenCurrentFile) + This is the local-header version of the extra field (sometimes, there is + more info in the local-header version than in the central-header) + + if buf==NULL, it return the size of the local extra field + + if buf!=NULL, len is the size of the buffer, the extra header is copied in + buf. + the return value is the number of bytes copied in buf, or (if <0) + the error code +*/ + +#ifdef __cplusplus +} +#endif + +#endif /* _unz_H */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/common/vbalua.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/vbalua.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,52 @@ +#ifndef VBA_LUA_H +#define VBA_LUA_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +enum LuaCallID +{ + LUACALL_BEFOREEMULATION, + LUACALL_AFTEREMULATION, + LUACALL_BEFOREEXIT, + + LUACALL_COUNT +}; +void CallRegisteredLuaFunctions(LuaCallID calltype); + +enum LuaMemHookType +{ + LUAMEMHOOK_WRITE, + LUAMEMHOOK_READ, + LUAMEMHOOK_EXEC, + LUAMEMHOOK_WRITE_SUB, + LUAMEMHOOK_READ_SUB, + LUAMEMHOOK_EXEC_SUB, + + LUAMEMHOOK_COUNT +}; +void CallRegisteredLuaMemHook(unsigned int address, int size, unsigned int value, LuaMemHookType hookType); + +// Just forward function declarations + +void VBALuaFrameBoundary(); +int VBALoadLuaCode(const char *filename); +int VBAReloadLuaCode(); +void VBALuaStop(); +int VBALuaRunning(); + +int VBALuaUsingJoypad(int); +int VBALuaReadJoypad(int); +int VBALuaSpeed(); +bool8 VBALuaRerecordCountSkip(); + +void VBALuaGui(uint8 *screen, int ppl, int width, int height); +void VBALuaClearGui(); + +char* VBAGetLuaScriptName(); + +// And some interesting REVERSE declarations! +char *VBAGetFreezeFilename(int slot); + +#endif // VBA_LUA_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/2xSaI.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/2xSaI.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1434 @@ +#include "../common/System.h" + +extern "C" +{ +#ifdef MMX + void _2xSaILine(u8 *srcPtr, u8 *deltaPtr, u32 srcPitch, + u32 width, u8 *dstPtr, u32 dstPitch); + void _2xSaISuperEagleLine(u8 *srcPtr, u8 *deltaPtr, + u32 srcPitch, u32 width, + u8 *dstPtr, u32 dstPitch); + void _2xSaISuper2xSaILine(u8 *srcPtr, u8 *deltaPtr, + u32 srcPitch, u32 width, + u8 *dstPtr, u32 dstPitch); + void Init_2xSaIMMX(u32 BitFormat); + void BilinearMMX(u16 *A, u16 *B, u16 *C, u16 *D, + u16 *dx, u16 *dy, u8 *dP); + void BilinearMMXGrid0(u16 *A, u16 *B, u16 *C, u16 *D, + u16 *dx, u16 *dy, u8 *dP); + void BilinearMMXGrid1(u16 *A, u16 *B, u16 *C, u16 *D, + u16 *dx, u16 *dy, u8 *dP); + void EndMMX(); + + bool cpu_mmx = 1; +#endif +} +static u32 colorMask = 0xF7DEF7DE; +static u32 lowPixelMask = 0x08210821; +static u32 qcolorMask = 0xE79CE79C; +static u32 qlowpixelMask = 0x18631863; +static u32 redblueMask = 0xF81F; +static u32 greenMask = 0x7E0; + +u32 qRGB_COLOR_MASK[2] = { 0xF7DEF7DE, 0xF7DEF7DE }; + +extern void hq2x_init(unsigned); + +int Init_2xSaI(u32 BitFormat) +{ + if (systemColorDepth == 16) + { + if (BitFormat == 565) + { + colorMask = 0xF7DEF7DE; + lowPixelMask = 0x08210821; + qcolorMask = 0xE79CE79C; + qlowpixelMask = 0x18631863; + redblueMask = 0xF81F; + greenMask = 0x7E0; + qRGB_COLOR_MASK[0] = qRGB_COLOR_MASK[1] = 0xF7DEF7DE; + hq2x_init(16); + } + else if (BitFormat == 555) + { + colorMask = 0x7BDE7BDE; + lowPixelMask = 0x04210421; + qcolorMask = 0x739C739C; + qlowpixelMask = 0x0C630C63; + redblueMask = 0x7C1F; + greenMask = 0x3E0; + qRGB_COLOR_MASK[0] = qRGB_COLOR_MASK[1] = 0x7BDE7BDE; + hq2x_init(15); + } + else + { + return 0; + } + } + else if (systemColorDepth == 32) + { + colorMask = 0xfefefe; + lowPixelMask = 0x010101; + qcolorMask = 0xfcfcfc; + qlowpixelMask = 0x030303; + qRGB_COLOR_MASK[0] = qRGB_COLOR_MASK[1] = 0xfefefe; + hq2x_init(32); + } + else + return 0; + +#ifdef MMX + Init_2xSaIMMX(BitFormat); +#endif + + return 1; +} + +static inline int GetResult1(u32 A, u32 B, u32 C, u32 D, + u32 /* E */) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r += 1; + if (y <= 1) + r -= 1; + return r; +} + +static inline int GetResult2(u32 A, u32 B, u32 C, u32 D, + u32 /* E */) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r -= 1; + if (y <= 1) + r += 1; + return r; +} + +static inline int GetResult(u32 A, u32 B, u32 C, u32 D) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r += 1; + if (y <= 1) + r -= 1; + return r; +} + +static inline u32 INTERPOLATE(u32 A, u32 B) +{ + if (A != B) + { + return (((A & colorMask) >> 1) + ((B & colorMask) >> 1) + + (A & B & lowPixelMask)); + } + else + return A; +} + +static inline u32 Q_INTERPOLATE(u32 A, u32 B, u32 C, u32 D) +{ + register u32 x = ((A & qcolorMask) >> 2) + + ((B & qcolorMask) >> 2) + + ((C & qcolorMask) >> 2) + ((D & qcolorMask) >> 2); + register u32 y = (A & qlowpixelMask) + + (B & qlowpixelMask) + (C & qlowpixelMask) + (D & qlowpixelMask); + + y = (y >> 2) & qlowpixelMask; + return x + y; +} + +static inline int GetResult1_32(u32 A, u32 B, u32 C, u32 D, + u32 /* E */) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r += 1; + if (y <= 1) + r -= 1; + return r; +} + +static inline int GetResult2_32(u32 A, u32 B, u32 C, u32 D, + u32 /* E */) +{ + int x = 0; + int y = 0; + int r = 0; + + if (A == C) + x += 1; + else if (B == C) + y += 1; + if (A == D) + x += 1; + else if (B == D) + y += 1; + if (x <= 1) + r -= 1; + if (y <= 1) + r += 1; + return r; +} + +#define BLUE_MASK565 0x001F001F +#define RED_MASK565 0xF800F800 +#define GREEN_MASK565 0x07E007E0 + +#define BLUE_MASK555 0x001F001F +#define RED_MASK555 0x7C007C00 +#define GREEN_MASK555 0x03E003E0 + +void Super2xSaI(u8 *srcPtr, u32 srcPitch, + u8 *deltaPtr, u8 *dstPtr, u32 dstPitch, + int width, int height) +{ + u16 *bP; + u8 * dP; + u32 inc_bP; + u32 Nextline = srcPitch >> 1; +#ifdef MMX + if (cpu_mmx) + { + for (; height; height--) + { + _2xSaISuper2xSaILine(srcPtr, deltaPtr, srcPitch, width, + dstPtr, dstPitch); + srcPtr += srcPitch; + dstPtr += dstPitch * 2; + deltaPtr += srcPitch; + } + } + else +#endif + { + inc_bP = 1; + + for (; height; height--) + { + bP = (u16 *) srcPtr; + dP = (u8 *) dstPtr; + + for (u32 finish = width; finish; finish -= inc_bP) + { + u32 color4, color5, color6; + u32 color1, color2, color3; + u32 colorA0, colorA1, colorA2, colorA3, + colorB0, colorB1, colorB2, colorB3, colorS1, colorS2; + u32 product1a, product1b, product2a, product2b; + + //--------------------------------------- B1 B2 + // 4 5 6 S2 + // 1 2 3 S1 + // A1 A2 + + colorB0 = *(bP - Nextline - 1); + colorB1 = *(bP - Nextline); + colorB2 = *(bP - Nextline + 1); + colorB3 = *(bP - Nextline + 2); + + color4 = *(bP - 1); + color5 = *(bP); + color6 = *(bP + 1); + colorS2 = *(bP + 2); + + color1 = *(bP + Nextline - 1); + color2 = *(bP + Nextline); + color3 = *(bP + Nextline + 1); + colorS1 = *(bP + Nextline + 2); + + colorA0 = *(bP + Nextline + Nextline - 1); + colorA1 = *(bP + Nextline + Nextline); + colorA2 = *(bP + Nextline + Nextline + 1); + colorA3 = *(bP + Nextline + Nextline + 2); + + //-------------------------------------- + if (color2 == color6 && color5 != color3) + { + product2b = product1b = color2; + } + else if (color5 == color3 && color2 != color6) + { + product2b = product1b = color5; + } + else if (color5 == color3 && color2 == color6) + { + register int r = 0; + + r += GetResult(color6, color5, color1, colorA1); + r += GetResult(color6, color5, color4, colorB1); + r += GetResult(color6, color5, colorA2, colorS1); + r += GetResult(color6, color5, colorB2, colorS2); + + if (r > 0) + product2b = product1b = color6; + else if (r < 0) + product2b = product1b = color5; + else + { + product2b = product1b = INTERPOLATE(color5, color6); + } + } + else + { + if (color6 == color3 && color3 == colorA1 + && color2 != colorA2 && color3 != colorA0) + product2b = + Q_INTERPOLATE(color3, color3, color3, color2); + else if (color5 == color2 && color2 == colorA2 + && colorA1 != color3 && color2 != colorA3) + product2b = + Q_INTERPOLATE(color2, color2, color2, color3); + else + product2b = INTERPOLATE(color2, color3); + + if (color6 == color3 && color6 == colorB1 + && color5 != colorB2 && color6 != colorB0) + product1b = + Q_INTERPOLATE(color6, color6, color6, color5); + else if (color5 == color2 && color5 == colorB2 + && colorB1 != color6 && color5 != colorB3) + product1b = + Q_INTERPOLATE(color6, color5, color5, color5); + else + product1b = INTERPOLATE(color5, color6); + } + + if (color5 == color3 && color2 != color6 && color4 == color5 + && color5 != colorA2) + product2a = INTERPOLATE(color2, color5); + else + if (color5 == color1 && color6 == color5 + && color4 != color2 && color5 != colorA0) + product2a = INTERPOLATE(color2, color5); + else + product2a = color2; + + if (color2 == color6 && color5 != color3 && color1 == color2 + && color2 != colorB2) + product1a = INTERPOLATE(color2, color5); + else + if (color4 == color2 && color3 == color2 + && color1 != color5 && color2 != colorB0) + product1a = INTERPOLATE(color2, color5); + else + product1a = color5; + +#ifdef WORDS_BIGENDIAN + product1a = (product1a << 16) | product1b; + product2a = (product2a << 16) | product2b; +#else + product1a = product1a | (product1b << 16); + product2a = product2a | (product2b << 16); +#endif + + *((u32 *) dP) = product1a; + *((u32 *) (dP + dstPitch)) = product2a; + + bP += inc_bP; + dP += sizeof(u32); + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + deltaPtr += srcPitch; + } // endof: for (; height; height--) + } +} + +void Super2xSaI32(u8 *srcPtr, u32 srcPitch, + u8 * /* deltaPtr */, u8 *dstPtr, u32 dstPitch, + int width, int height) +{ + u32 *bP; + u32 *dP; + u32 inc_bP; + u32 Nextline = srcPitch >> 2; + inc_bP = 1; + + for (; height; height--) + { + bP = (u32 *) srcPtr; + dP = (u32 *) dstPtr; + + for (u32 finish = width; finish; finish -= inc_bP) + { + u32 color4, color5, color6; + u32 color1, color2, color3; + u32 colorA0, colorA1, colorA2, colorA3, + colorB0, colorB1, colorB2, colorB3, colorS1, colorS2; + u32 product1a, product1b, product2a, product2b; + + //--------------------------------------- B1 B2 + // 4 5 6 S2 + // 1 2 3 S1 + // A1 A2 + + colorB0 = *(bP - Nextline - 1); + colorB1 = *(bP - Nextline); + colorB2 = *(bP - Nextline + 1); + colorB3 = *(bP - Nextline + 2); + + color4 = *(bP - 1); + color5 = *(bP); + color6 = *(bP + 1); + colorS2 = *(bP + 2); + + color1 = *(bP + Nextline - 1); + color2 = *(bP + Nextline); + color3 = *(bP + Nextline + 1); + colorS1 = *(bP + Nextline + 2); + + colorA0 = *(bP + Nextline + Nextline - 1); + colorA1 = *(bP + Nextline + Nextline); + colorA2 = *(bP + Nextline + Nextline + 1); + colorA3 = *(bP + Nextline + Nextline + 2); + + //-------------------------------------- + if (color2 == color6 && color5 != color3) + { + product2b = product1b = color2; + } + else if (color5 == color3 && color2 != color6) + { + product2b = product1b = color5; + } + else if (color5 == color3 && color2 == color6) + { + register int r = 0; + + r += GetResult(color6, color5, color1, colorA1); + r += GetResult(color6, color5, color4, colorB1); + r += GetResult(color6, color5, colorA2, colorS1); + r += GetResult(color6, color5, colorB2, colorS2); + + if (r > 0) + product2b = product1b = color6; + else if (r < 0) + product2b = product1b = color5; + else + { + product2b = product1b = INTERPOLATE(color5, color6); + } + } + else + { + if (color6 == color3 && color3 == colorA1 + && color2 != colorA2 && color3 != colorA0) + product2b = + Q_INTERPOLATE(color3, color3, color3, color2); + else if (color5 == color2 && color2 == colorA2 + && colorA1 != color3 && color2 != colorA3) + product2b = + Q_INTERPOLATE(color2, color2, color2, color3); + else + product2b = INTERPOLATE(color2, color3); + + if (color6 == color3 && color6 == colorB1 + && color5 != colorB2 && color6 != colorB0) + product1b = + Q_INTERPOLATE(color6, color6, color6, color5); + else if (color5 == color2 && color5 == colorB2 + && colorB1 != color6 && color5 != colorB3) + product1b = + Q_INTERPOLATE(color6, color5, color5, color5); + else + product1b = INTERPOLATE(color5, color6); + } + + if (color5 == color3 && color2 != color6 && color4 == color5 + && color5 != colorA2) + product2a = INTERPOLATE(color2, color5); + else + if (color5 == color1 && color6 == color5 + && color4 != color2 && color5 != colorA0) + product2a = INTERPOLATE(color2, color5); + else + product2a = color2; + + if (color2 == color6 && color5 != color3 && color1 == color2 + && color2 != colorB2) + product1a = INTERPOLATE(color2, color5); + else + if (color4 == color2 && color3 == color2 + && color1 != color5 && color2 != colorB0) + product1a = INTERPOLATE(color2, color5); + else + product1a = color5; + *(dP) = product1a; + *(dP + 1) = product1b; + *(dP + (dstPitch >> 2)) = product2a; + *(dP + (dstPitch >> 2) + 1) = product2b; + + bP += inc_bP; + dP += 2; + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + // deltaPtr += srcPitch; + } // endof: for (; height; height--) +} + +void SuperEagle(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 * dP; + u16 *bP; + u16 *xP; + u32 inc_bP; + +#ifdef MMX + if (cpu_mmx) + { + for (; height; height--) + { + _2xSaISuperEagleLine(srcPtr, deltaPtr, srcPitch, width, + dstPtr, dstPitch); + srcPtr += srcPitch; + dstPtr += dstPitch * 2; + deltaPtr += srcPitch; + } + } + else +#endif + { + inc_bP = 1; + + u32 Nextline = srcPitch >> 1; + + for (; height; height--) + { + bP = (u16 *) srcPtr; + xP = (u16 *) deltaPtr; + dP = dstPtr; + for (u32 finish = width; finish; finish -= inc_bP) + { + u32 color4, color5, color6; + u32 color1, color2, color3; + u32 colorA1, colorA2, colorB1, colorB2, colorS1, colorS2; + u32 product1a, product1b, product2a, product2b; + + colorB1 = *(bP - Nextline); + colorB2 = *(bP - Nextline + 1); + + color4 = *(bP - 1); + color5 = *(bP); + color6 = *(bP + 1); + colorS2 = *(bP + 2); + + color1 = *(bP + Nextline - 1); + color2 = *(bP + Nextline); + color3 = *(bP + Nextline + 1); + colorS1 = *(bP + Nextline + 2); + + colorA1 = *(bP + Nextline + Nextline); + colorA2 = *(bP + Nextline + Nextline + 1); + + // -------------------------------------- + if (color2 == color6 && color5 != color3) + { + product1b = product2a = color2; + if ((color1 == color2) || (color6 == colorB2)) + { + product1a = INTERPOLATE(color2, color5); + product1a = INTERPOLATE(color2, product1a); + // product1a = color2; + } + else + { + product1a = INTERPOLATE(color5, color6); + } + + if ((color6 == colorS2) || (color2 == colorA1)) + { + product2b = INTERPOLATE(color2, color3); + product2b = INTERPOLATE(color2, product2b); + // product2b = color2; + } + else + { + product2b = INTERPOLATE(color2, color3); + } + } + else if (color5 == color3 && color2 != color6) + { + product2b = product1a = color5; + + if ((colorB1 == color5) || (color3 == colorS1)) + { + product1b = INTERPOLATE(color5, color6); + product1b = INTERPOLATE(color5, product1b); + // product1b = color5; + } + else + { + product1b = INTERPOLATE(color5, color6); + } + + if ((color3 == colorA2) || (color4 == color5)) + { + product2a = INTERPOLATE(color5, color2); + product2a = INTERPOLATE(color5, product2a); + // product2a = color5; + } + else + { + product2a = INTERPOLATE(color2, color3); + } + } + else if (color5 == color3 && color2 == color6) + { + register int r = 0; + + r += GetResult(color6, color5, color1, colorA1); + r += GetResult(color6, color5, color4, colorB1); + r += GetResult(color6, color5, colorA2, colorS1); + r += GetResult(color6, color5, colorB2, colorS2); + + if (r > 0) + { + product1b = product2a = color2; + product1a = product2b = INTERPOLATE(color5, color6); + } + else if (r < 0) + { + product2b = product1a = color5; + product1b = product2a = INTERPOLATE(color5, color6); + } + else + { + product2b = product1a = color5; + product1b = product2a = color2; + } + } + else + { + product2b = product1a = INTERPOLATE(color2, color6); + product2b = + Q_INTERPOLATE(color3, color3, color3, product2b); + product1a = + Q_INTERPOLATE(color5, color5, color5, product1a); + + product2a = product1b = INTERPOLATE(color5, color3); + product2a = + Q_INTERPOLATE(color2, color2, color2, product2a); + product1b = + Q_INTERPOLATE(color6, color6, color6, product1b); + + // product1a = color5; + // product1b = color6; + // product2a = color2; + // product2b = color3; + } +#ifdef WORDS_BIGENDIAN + product1a = (product1a << 16) | product1b; + product2a = (product2a << 16) | product2b; +#else + product1a = product1a | (product1b << 16); + product2a = product2a | (product2b << 16); +#endif + + *((u32 *) dP) = product1a; + *((u32 *) (dP + dstPitch)) = product2a; + *xP = color5; + + bP += inc_bP; + xP += inc_bP; + dP += sizeof(u32); + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + deltaPtr += srcPitch; + } // endof: for (height; height; height--) + } +} + +void SuperEagle32(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dP; + u32 *bP; + u32 *xP; + u32 inc_bP; + + inc_bP = 1; + + u32 Nextline = srcPitch >> 2; + + for (; height; height--) + { + bP = (u32 *) srcPtr; + xP = (u32 *) deltaPtr; + dP = (u32 *)dstPtr; + for (u32 finish = width; finish; finish -= inc_bP) + { + u32 color4, color5, color6; + u32 color1, color2, color3; + u32 colorA1, colorA2, colorB1, colorB2, colorS1, colorS2; + u32 product1a, product1b, product2a, product2b; + + colorB1 = *(bP - Nextline); + colorB2 = *(bP - Nextline + 1); + + color4 = *(bP - 1); + color5 = *(bP); + color6 = *(bP + 1); + colorS2 = *(bP + 2); + + color1 = *(bP + Nextline - 1); + color2 = *(bP + Nextline); + color3 = *(bP + Nextline + 1); + colorS1 = *(bP + Nextline + 2); + + colorA1 = *(bP + Nextline + Nextline); + colorA2 = *(bP + Nextline + Nextline + 1); + + // -------------------------------------- + if (color2 == color6 && color5 != color3) + { + product1b = product2a = color2; + if ((color1 == color2) || (color6 == colorB2)) + { + product1a = INTERPOLATE(color2, color5); + product1a = INTERPOLATE(color2, product1a); + // product1a = color2; + } + else + { + product1a = INTERPOLATE(color5, color6); + } + + if ((color6 == colorS2) || (color2 == colorA1)) + { + product2b = INTERPOLATE(color2, color3); + product2b = INTERPOLATE(color2, product2b); + // product2b = color2; + } + else + { + product2b = INTERPOLATE(color2, color3); + } + } + else if (color5 == color3 && color2 != color6) + { + product2b = product1a = color5; + + if ((colorB1 == color5) || (color3 == colorS1)) + { + product1b = INTERPOLATE(color5, color6); + product1b = INTERPOLATE(color5, product1b); + // product1b = color5; + } + else + { + product1b = INTERPOLATE(color5, color6); + } + + if ((color3 == colorA2) || (color4 == color5)) + { + product2a = INTERPOLATE(color5, color2); + product2a = INTERPOLATE(color5, product2a); + // product2a = color5; + } + else + { + product2a = INTERPOLATE(color2, color3); + } + } + else if (color5 == color3 && color2 == color6) + { + register int r = 0; + + r += GetResult(color6, color5, color1, colorA1); + r += GetResult(color6, color5, color4, colorB1); + r += GetResult(color6, color5, colorA2, colorS1); + r += GetResult(color6, color5, colorB2, colorS2); + + if (r > 0) + { + product1b = product2a = color2; + product1a = product2b = INTERPOLATE(color5, color6); + } + else if (r < 0) + { + product2b = product1a = color5; + product1b = product2a = INTERPOLATE(color5, color6); + } + else + { + product2b = product1a = color5; + product1b = product2a = color2; + } + } + else + { + product2b = product1a = INTERPOLATE(color2, color6); + product2b = + Q_INTERPOLATE(color3, color3, color3, product2b); + product1a = + Q_INTERPOLATE(color5, color5, color5, product1a); + + product2a = product1b = INTERPOLATE(color5, color3); + product2a = + Q_INTERPOLATE(color2, color2, color2, product2a); + product1b = + Q_INTERPOLATE(color6, color6, color6, product1b); + + // product1a = color5; + // product1b = color6; + // product2a = color2; + // product2b = color3; + } + *(dP) = product1a; + *(dP + 1) = product1b; + *(dP + (dstPitch >> 2)) = product2a; + *(dP + (dstPitch >> 2) + 1) = product2b; + *xP = color5; + + bP += inc_bP; + xP += inc_bP; + dP += 2; + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + deltaPtr += srcPitch; + } // endof: for (height; height; height--) +} + +void _2xSaI(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 * dP; + u16 *bP; + u32 inc_bP; + +#ifdef MMX + if (cpu_mmx) + { + for (; height; height -= 1) + { + _2xSaILine(srcPtr, deltaPtr, srcPitch, width, dstPtr, dstPitch); + srcPtr += srcPitch; + dstPtr += dstPitch * 2; + deltaPtr += srcPitch; + } + } + else +#endif + { + inc_bP = 1; + + u32 Nextline = srcPitch >> 1; + + for (; height; height--) + { + bP = (u16 *) srcPtr; + dP = dstPtr; + + for (u32 finish = width; finish; finish -= inc_bP) + { + register u32 colorA, colorB; + u32 colorC, colorD, + colorE, colorF, colorG, colorH, + colorI, colorJ, colorK, colorL, + + colorM, colorN, colorO, colorP; + u32 product, product1, product2; + + //--------------------------------------- + // Map of the pixels: I|E F|J + // G|A B|K + // H|C D|L + // M|N O|P + colorI = *(bP - Nextline - 1); + colorE = *(bP - Nextline); + colorF = *(bP - Nextline + 1); + colorJ = *(bP - Nextline + 2); + + colorG = *(bP - 1); + colorA = *(bP); + colorB = *(bP + 1); + colorK = *(bP + 2); + + colorH = *(bP + Nextline - 1); + colorC = *(bP + Nextline); + colorD = *(bP + Nextline + 1); + colorL = *(bP + Nextline + 2); + + colorM = *(bP + Nextline + Nextline - 1); + colorN = *(bP + Nextline + Nextline); + colorO = *(bP + Nextline + Nextline + 1); + colorP = *(bP + Nextline + Nextline + 2); + + if ((colorA == colorD) && (colorB != colorC)) + { + if (((colorA == colorE) && (colorB == colorL)) || + ((colorA == colorC) && (colorA == colorF) + && (colorB != colorE) && (colorB == colorJ))) + { + product = colorA; + } + else + { + product = INTERPOLATE(colorA, colorB); + } + + if (((colorA == colorG) && (colorC == colorO)) || + ((colorA == colorB) && (colorA == colorH) + && (colorG != colorC) && (colorC == colorM))) + { + product1 = colorA; + } + else + { + product1 = INTERPOLATE(colorA, colorC); + } + product2 = colorA; + } + else if ((colorB == colorC) && (colorA != colorD)) + { + if (((colorB == colorF) && (colorA == colorH)) || + ((colorB == colorE) && (colorB == colorD) + && (colorA != colorF) && (colorA == colorI))) + { + product = colorB; + } + else + { + product = INTERPOLATE(colorA, colorB); + } + + if (((colorC == colorH) && (colorA == colorF)) || + ((colorC == colorG) && (colorC == colorD) + && (colorA != colorH) && (colorA == colorI))) + { + product1 = colorC; + } + else + { + product1 = INTERPOLATE(colorA, colorC); + } + product2 = colorB; + } + else if ((colorA == colorD) && (colorB == colorC)) + { + if (colorA == colorB) + { + product = colorA; + product1 = colorA; + product2 = colorA; + } + else + { + register int r = 0; + + product1 = INTERPOLATE(colorA, colorC); + product = INTERPOLATE(colorA, colorB); + + r += + GetResult1(colorA, colorB, colorG, colorE, + colorI); + r += + GetResult2(colorB, colorA, colorK, colorF, + colorJ); + r += + GetResult2(colorB, colorA, colorH, colorN, + colorM); + r += + GetResult1(colorA, colorB, colorL, colorO, + colorP); + + if (r > 0) + product2 = colorA; + else if (r < 0) + product2 = colorB; + else + { + product2 = + Q_INTERPOLATE(colorA, colorB, colorC, + colorD); + } + } + } + else + { + product2 = Q_INTERPOLATE(colorA, colorB, colorC, colorD); + + if ((colorA == colorC) && (colorA == colorF) + && (colorB != colorE) && (colorB == colorJ)) + { + product = colorA; + } + else if ((colorB == colorE) && (colorB == colorD) + && (colorA != colorF) && (colorA == colorI)) + { + product = colorB; + } + else + { + product = INTERPOLATE(colorA, colorB); + } + + if ((colorA == colorB) && (colorA == colorH) + && (colorG != colorC) && (colorC == colorM)) + { + product1 = colorA; + } + else if ((colorC == colorG) && (colorC == colorD) + && (colorA != colorH) && (colorA == colorI)) + { + product1 = colorC; + } + else + { + product1 = INTERPOLATE(colorA, colorC); + } + } + +#ifdef WORDS_BIGENDIAN + product = (colorA << 16) | product; + product1 = (product1 << 16) | product2; +#else + product = colorA | (product << 16); + product1 = product1 | (product2 << 16); +#endif + *((s32 *) dP) = product; + *((u32 *) (dP + dstPitch)) = product1; + + bP += inc_bP; + dP += sizeof(u32); + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + deltaPtr += srcPitch; + } // endof: for (height; height; height--) + } +} + +void _2xSaI32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dP; + u32 *bP; + u32 inc_bP = 1; + + u32 Nextline = srcPitch >> 2; + + for (; height; height--) + { + bP = (u32 *) srcPtr; + dP = (u32 *) dstPtr; + + for (u32 finish = width; finish; finish -= inc_bP) + { + register u32 colorA, colorB; + u32 colorC, colorD, + colorE, colorF, colorG, colorH, + colorI, colorJ, colorK, colorL, + + colorM, colorN, colorO, colorP; + u32 product, product1, product2; + + //--------------------------------------- + // Map of the pixels: I|E F|J + // G|A B|K + // H|C D|L + // M|N O|P + colorI = *(bP - Nextline - 1); + colorE = *(bP - Nextline); + colorF = *(bP - Nextline + 1); + colorJ = *(bP - Nextline + 2); + + colorG = *(bP - 1); + colorA = *(bP); + colorB = *(bP + 1); + colorK = *(bP + 2); + + colorH = *(bP + Nextline - 1); + colorC = *(bP + Nextline); + colorD = *(bP + Nextline + 1); + colorL = *(bP + Nextline + 2); + + colorM = *(bP + Nextline + Nextline - 1); + colorN = *(bP + Nextline + Nextline); + colorO = *(bP + Nextline + Nextline + 1); + colorP = *(bP + Nextline + Nextline + 2); + + if ((colorA == colorD) && (colorB != colorC)) + { + if (((colorA == colorE) && (colorB == colorL)) || + ((colorA == colorC) && (colorA == colorF) + && (colorB != colorE) && (colorB == colorJ))) + { + product = colorA; + } + else + { + product = INTERPOLATE(colorA, colorB); + } + + if (((colorA == colorG) && (colorC == colorO)) || + ((colorA == colorB) && (colorA == colorH) + && (colorG != colorC) && (colorC == colorM))) + { + product1 = colorA; + } + else + { + product1 = INTERPOLATE(colorA, colorC); + } + product2 = colorA; + } + else if ((colorB == colorC) && (colorA != colorD)) + { + if (((colorB == colorF) && (colorA == colorH)) || + ((colorB == colorE) && (colorB == colorD) + && (colorA != colorF) && (colorA == colorI))) + { + product = colorB; + } + else + { + product = INTERPOLATE(colorA, colorB); + } + + if (((colorC == colorH) && (colorA == colorF)) || + ((colorC == colorG) && (colorC == colorD) + && (colorA != colorH) && (colorA == colorI))) + { + product1 = colorC; + } + else + { + product1 = INTERPOLATE(colorA, colorC); + } + product2 = colorB; + } + else if ((colorA == colorD) && (colorB == colorC)) + { + if (colorA == colorB) + { + product = colorA; + product1 = colorA; + product2 = colorA; + } + else + { + register int r = 0; + + product1 = INTERPOLATE(colorA, colorC); + product = INTERPOLATE(colorA, colorB); + + r += + GetResult1(colorA, colorB, colorG, colorE, + colorI); + r += + GetResult2(colorB, colorA, colorK, colorF, + colorJ); + r += + GetResult2(colorB, colorA, colorH, colorN, + colorM); + r += + GetResult1(colorA, colorB, colorL, colorO, + colorP); + + if (r > 0) + product2 = colorA; + else if (r < 0) + product2 = colorB; + else + { + product2 = + Q_INTERPOLATE(colorA, colorB, colorC, + colorD); + } + } + } + else + { + product2 = Q_INTERPOLATE(colorA, colorB, colorC, colorD); + + if ((colorA == colorC) && (colorA == colorF) + && (colorB != colorE) && (colorB == colorJ)) + { + product = colorA; + } + else if ((colorB == colorE) && (colorB == colorD) + && (colorA != colorF) && (colorA == colorI)) + { + product = colorB; + } + else + { + product = INTERPOLATE(colorA, colorB); + } + + if ((colorA == colorB) && (colorA == colorH) + && (colorG != colorC) && (colorC == colorM)) + { + product1 = colorA; + } + else if ((colorC == colorG) && (colorC == colorD) + && (colorA != colorH) && (colorA == colorI)) + { + product1 = colorC; + } + else + { + product1 = INTERPOLATE(colorA, colorC); + } + } + *(dP) = colorA; + *(dP + 1) = product; + *(dP + (dstPitch >> 2)) = product1; + *(dP + (dstPitch >> 2) + 1) = product2; + + bP += inc_bP; + dP += 2; + } // end of for ( finish= width etc..) + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + // deltaPtr += srcPitch; + } // endof: for (height; height; height--) +} + +static u32 Bilinear(u32 A, u32 B, u32 x) +{ + unsigned long areaA, areaB; + unsigned long result; + + if (A == B) + return A; + + areaB = (x >> 11) & 0x1f; // reduce 16 bit fraction to 5 bits + areaA = 0x20 - areaB; + + A = (A & redblueMask) | ((A & greenMask) << 16); + B = (B & redblueMask) | ((B & greenMask) << 16); + + result = ((areaA * A) + (areaB * B)) >> 5; + + return (result & redblueMask) | ((result >> 16) & greenMask); +} + +static u32 Bilinear4(u32 A, u32 B, u32 C, u32 D, u32 x, + u32 y) +{ + unsigned long areaA, areaB, areaC, areaD; + unsigned long result, xy; + + x = (x >> 11) & 0x1f; + y = (y >> 11) & 0x1f; + xy = (x * y) >> 5; + + A = (A & redblueMask) | ((A & greenMask) << 16); + B = (B & redblueMask) | ((B & greenMask) << 16); + C = (C & redblueMask) | ((C & greenMask) << 16); + D = (D & redblueMask) | ((D & greenMask) << 16); + + areaA = 0x20 + xy - x - y; + areaB = x - xy; + areaC = y - xy; + areaD = xy; + + result = ((areaA * A) + (areaB * B) + (areaC * C) + (areaD * D)) >> 5; + + return (result & redblueMask) | ((result >> 16) & greenMask); +} + +void Scale_2xSaI(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, + u32 dstWidth, u32 dstHeight, int width, int height) +{ + u8 * dP; + u16 *bP; + + u32 w; + u32 h; + u32 dw; + u32 dh; + u32 hfinish; + u32 wfinish; + + u32 Nextline = srcPitch >> 1; + + wfinish = (width - 1) << 16; // convert to fixed point + dw = wfinish / (dstWidth - 1); + hfinish = (height - 1) << 16; // convert to fixed point + dh = hfinish / (dstHeight - 1); + + for (h = 0; h < hfinish; h += dh) + { + u32 y1, y2; + + y1 = h & 0xffff; // fraction part of fixed point + bP = (u16 *) (srcPtr + ((h >> 16) * srcPitch)); + dP = dstPtr; + y2 = 0x10000 - y1; + + w = 0; + + for (; w < wfinish; ) + { + u32 A, B, C, D; + u32 E, F, G, H; + u32 I, J, K, L; + u32 x1, x2, a1, f1, f2; + u32 position, product1; + + position = w >> 16; + A = bP[position]; // current pixel + B = bP[position + 1]; // next pixel + C = bP[position + Nextline]; + D = bP[position + Nextline + 1]; + E = bP[position - Nextline]; + F = bP[position - Nextline + 1]; + G = bP[position - 1]; + H = bP[position + Nextline - 1]; + I = bP[position + 2]; + J = bP[position + Nextline + 2]; + K = bP[position + Nextline + Nextline]; + L = bP[position + Nextline + Nextline + 1]; + + x1 = w & 0xffff; // fraction part of fixed point + x2 = 0x10000 - x1; + + /*0*/ + if (A == B && C == D && A == C) + product1 = A; + else /*1*/ if (A == D && B != C) + { + f1 = (x1 >> 1) + (0x10000 >> 2); + f2 = (y1 >> 1) + (0x10000 >> 2); + if (y1 <= f1 && A == J && A != E) // close to B + { + a1 = f1 - y1; + product1 = Bilinear(A, B, a1); + } + else if (y1 >= f1 && A == G && A != L) // close to C + { + a1 = y1 - f1; + product1 = Bilinear(A, C, a1); + } + else if (x1 >= f2 && A == E && A != J) // close to B + { + a1 = x1 - f2; + product1 = Bilinear(A, B, a1); + } + else if (x1 <= f2 && A == L && A != G) // close to C + { + a1 = f2 - x1; + product1 = Bilinear(A, C, a1); + } + else if (y1 >= x1) // close to C + { + a1 = y1 - x1; + product1 = Bilinear(A, C, a1); + } + else if (y1 <= x1) // close to B + { + a1 = x1 - y1; + product1 = Bilinear(A, B, a1); + } + } + else + /*2*/ + if (B == C && A != D) + { + f1 = (x1 >> 1) + (0x10000 >> 2); + f2 = (y1 >> 1) + (0x10000 >> 2); + if (y2 >= f1 && B == H && B != F) // close to A + { + a1 = y2 - f1; + product1 = Bilinear(B, A, a1); + } + else if (y2 <= f1 && B == I && B != K) // close to D + { + a1 = f1 - y2; + product1 = Bilinear(B, D, a1); + } + else if (x2 >= f2 && B == F && B != H) // close to A + { + a1 = x2 - f2; + product1 = Bilinear(B, A, a1); + } + else if (x2 <= f2 && B == K && B != I) // close to D + { + a1 = f2 - x2; + product1 = Bilinear(B, D, a1); + } + else if (y2 >= x1) // close to A + { + a1 = y2 - x1; + product1 = Bilinear(B, A, a1); + } + else if (y2 <= x1) // close to D + { + a1 = x1 - y2; + product1 = Bilinear(B, D, a1); + } + } + /*3*/ + else + { + product1 = Bilinear4(A, B, C, D, x1, y1); + } + + //end First Pixel + *(u32 *) dP = product1; + dP += 2; + w += dw; + } + dstPtr += dstPitch; + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/2xSaImmx.asm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/2xSaImmx.asm Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,2109 @@ +;/*---------------------------------------------------------------------* +; * The following (piece of) code, (part of) the 2xSaI engine, * +; * copyright (c) 1999 - 2001 by Derek Liauw Kie Fa. * +; * Non-Commercial use of this software is allowed and is encouraged, * +; * provided that appropriate credit be given. * +; * You may freely modify this code, but I request * +; * that any improvements to the engine be submitted to me, so * +; * that I can implement these improvements in newer versions of * +; * the software. * +; * If you need more information, have any comments or suggestions, * +; * you can e-mail me. My e-mail: derek-liauw@usa.net. * +; *---------------------------------------------------------------------*/ + +;---------------------- +; 2xSaI version 0.59 WIP, soon to become version 0.60 +;---------------------- + +;%define FAR_POINTER + + + + BITS 32 +%ifdef __DJGPP__ + GLOBAL __2xSaILine + GLOBAL __2xSaISuperEagleLine + GLOBAL __2xSaISuper2xSaILine + GLOBAL _Init_2xSaIMMX +%else + GLOBAL _2xSaILine + GLOBAL _2xSaISuperEagleLine + GLOBAL _2xSaISuper2xSaILine + GLOBAL Init_2xSaIMMX +%endif + SECTION .text ALIGN = 32 + +%ifdef FAR_POINTER +;EXTERN_C void _2xSaILine (uint8 *srcPtr, uint32 srcPitch, uint32 width, +; uint8 *dstPtr, uint32 dstPitch, uint16 dstSegment); +%else +;EXTERN_C void _2xSaILine (uint8 *srcPtr, uint32 srcPitch, uint32 width, +; uint8 *dstPtr, uint32 dstPitch); +%endif + +srcPtr equ 8 +deltaPtr equ 12 +srcPitch equ 16 +width equ 20 +dstOffset equ 24 +dstPitch equ 28 +dstSegment equ 32 + + + + +colorB0 equ -2 +colorB1 equ 0 +colorB2 equ 2 +colorB3 equ 4 + +color7 equ -2 +color8 equ 0 +color9 equ 2 + +color4 equ -2 +color5 equ 0 +color6 equ 2 +colorS2 equ 4 + +color1 equ -2 +color2 equ 0 +color3 equ 2 +colorS1 equ 4 + +colorA0 equ -2 +colorA1 equ 0 +colorA2 equ 2 +colorA3 equ 4 + + + + +%ifdef __DJGPP__ +__2xSaISuper2xSaILine: +%else +_2xSaISuper2xSaILine: +%endif +; Store some stuff + push ebp + mov ebp, esp + pushad + +; Prepare the destination +%ifdef FAR_POINTER + ; Set the selector + mov eax, [ebp+dstSegment] + mov fs, ax +%endif + mov edx, [ebp+dstOffset] ; edx points to the screen +; Prepare the source + ; eax points to colorA + mov eax, [ebp+srcPtr] ;eax points to colorA + mov ebx, [ebp+srcPitch] ;ebx contains the source pitch + mov ecx, [ebp+width] ;ecx contains the number of pixels to process + ; eax now points to colorB1 + sub eax, ebx ;eax points to B1 which is the base + +; Main Loop +.Loop: push ecx + + ;-----Check Delta------------------ + mov ecx, [ebp+deltaPtr] + + + ;load source img + movq mm0, [eax+colorB0] + movq mm1, [eax+colorB3] + movq mm2, [eax+ebx+color4] + movq mm3, [eax+ebx+colorS2] + movq mm4, [eax+ebx+ebx+color1] + movq mm5, [eax+ebx+ebx+colorS1] + push eax + add eax, ebx + movq mm6, [eax+ebx+ebx+colorA0] + movq mm7, [eax+ebx+ebx+colorA3] + pop eax + + ;compare to delta + pcmpeqw mm0, [ecx+2+colorB0] + pcmpeqw mm1, [ecx+2+colorB3] + pcmpeqw mm2, [ecx+ebx+2+color4] + pcmpeqw mm3, [ecx+ebx+2+colorS2] + pcmpeqw mm4, [ecx+ebx+ebx+2+color1] + pcmpeqw mm5, [ecx+ebx+ebx+2+colorS1] + add ecx, ebx + pcmpeqw mm6, [ecx+ebx+ebx+2+colorA0] + pcmpeqw mm7, [ecx+ebx+ebx+2+colorA3] + sub ecx, ebx + + + ;compose results + pand mm0, mm1 + pand mm2, mm3 + pand mm4, mm5 + pand mm6, mm7 + pand mm0, mm2 + pand mm4, mm6 + pxor mm7, mm7 + pand mm0, mm4 + movq mm6, [eax+colorB0] + pcmpeqw mm7, mm0 ;did any compare give us a zero ? + + movq [ecx+2+colorB0], mm6 + + packsswb mm7, mm7 + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_PROCESS ;no, so we can skip + + ;End Delta + + ;--------------------------------- + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+color6] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + movq [I56Pixel], mm0 + movq mm7, mm0 + + ;------------------- + movq mm0, mm7 + movq mm1, mm4 ;5,5,5,6 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + movq [I5556Pixel], mm0 + ;-------------------- + + movq mm0, mm7 + movq mm1, mm5 ;6,6,6,5 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I5666Pixel], mm0 + + ;------------------------- + ;------------------------- + movq mm0, [eax+ebx+ebx+color2] + movq mm1, [eax+ebx+ebx+color3] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I23Pixel], mm0 + movq mm7, mm0 + + ;--------------------- + movq mm0, mm7 + movq mm1, mm4 ;2,2,2,3 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I2223Pixel], mm0 + + ;---------------------- + movq mm0, mm7 + movq mm1, mm5 ;3,3,3,2 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I2333Pixel], mm0 + + + ;-------------------- +;//////////////////////////////// +; Decide which "branch" to take +;-------------------------------- + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+color6] + movq mm6, mm0 + movq mm7, mm1 + pcmpeqw mm0, [eax+ebx+ebx+color3] + pcmpeqw mm1, [eax+ebx+ebx+color2] + pcmpeqw mm6, mm7 + + movq mm2, mm0 + movq mm3, mm0 + + pand mm0, mm1 ;colorA == colorD && colorB == colorC + pxor mm7, mm7 + + pcmpeqw mm2, mm7 + pand mm6, mm0 + pand mm2, mm1 ;colorA != colorD && colorB == colorC + + pcmpeqw mm1, mm7 + + pand mm1, mm3 ;colorA == colorD && colorB != colorC + pxor mm0, mm6 + por mm1, mm6 + movq mm7, mm0 + movq [Mask26], mm2 + packsswb mm7, mm7 + movq [Mask35], mm1 + + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_GUESS + +;--------------------------------------------- + movq mm6, mm0 + movq mm4, [eax+ebx+colorA] + movq mm5, [eax+ebx+colorB] + pxor mm7, mm7 + pand mm6, [ONE] + + movq mm0, [eax+colorE] + movq mm1, [eax+ebx+colorG] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+colorF] + movq mm1, [eax+ebx+colorK] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + push eax + add eax, ebx + movq mm0, [eax+ebx+colorH] + movq mm1, [eax+ebx+ebx+colorN] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+ebx+colorL] + movq mm1, [eax+ebx+ebx+colorO] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + pop eax + movq mm1, mm7 + pxor mm0, mm0 + pcmpgtw mm7, mm0 + pcmpgtw mm0, mm1 + + por mm7, [Mask35] + por mm0, [Mask26] + movq [Mask35], mm7 + movq [Mask26], mm0 + +.SKIP_GUESS: + + ;Start the ASSEMBLY !!! eh... compose all the results together to form the final image... + + + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+ebx+color2] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + ;--------------------------- + + + +%ifdef dfhsdfhsdahdsfhdsfh + + if (color5 == color3 && color2 != color6 && color4 == color5 && color5 != colorA2) + product2a = INTERPOLATE (color2, color5); + else + if (color5 == color1 && color6 == color5 && color4 != color2 && color5 != colorA0) + product2a = INTERPOLATE(color2, color5); + else + product2a = color2; + + if (color2 == color6 && color5 != color3 && color1 == color2 && color2 != colorB2) + product1a = INTERPOLATE (color2, color5); + else + if (color4 == color2 && color3 == color2 && color1 != color5 && color2 != colorB0) + product1a = INTERPOLATE(color2, color5); + else + product1a = color5; + +%endif + + + movq mm7, [Mask26] + movq mm6, [eax+colorB2] + movq mm5, [eax+ebx+ebx+color2] + movq mm4, [eax+ebx+ebx+color1] + pcmpeqw mm4, mm5 + pcmpeqw mm6, mm5 + pxor mm5, mm5 + pand mm7, mm4 + pcmpeqw mm6, mm5 + pand mm7, mm6 + + + + movq mm6, [eax+ebx+ebx+color3] + movq mm5, [eax+ebx+ebx+color2] + movq mm4, [eax+ebx+ebx+color1] + movq mm2, [eax+ebx+color5] + movq mm1, [eax+ebx+color4] + movq mm3, [eax+colorB0] + + pcmpeqw mm2, mm4 + pcmpeqw mm6, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm3, mm5 + pxor mm5, mm5 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm6, mm1 + pand mm2, mm3 + pand mm6, mm2 + por mm7, mm6 + + + movq mm6, mm7 + pcmpeqw mm6, mm5 + pand mm7, mm0 + + movq mm1, [eax+ebx+color5] + pand mm6, mm1 + por mm7, mm6 + movq [final1a], mm7 ;finished 1a + + + + ;-------------------------------- + + movq mm7, [Mask35] + push eax + add eax, ebx + movq mm6, [eax+ebx+ebx+colorA2] + pop eax + movq mm5, [eax+ebx+color5] + movq mm4, [eax+ebx+color4] + pcmpeqw mm4, mm5 + pcmpeqw mm6, mm5 + pxor mm5, mm5 + pand mm7, mm4 + pcmpeqw mm6, mm5 + pand mm7, mm6 + + + + movq mm6, [eax+ebx+color6] + movq mm5, [eax+ebx+color5] + movq mm4, [eax+ebx+color4] + movq mm2, [eax+ebx+ebx+color2] + movq mm1, [eax+ebx+ebx+color1] + push eax + add eax, ebx + movq mm3, [eax+ebx+ebx+colorA0] + pop eax + + pcmpeqw mm2, mm4 + pcmpeqw mm6, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm3, mm5 + pxor mm5, mm5 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm6, mm1 + pand mm2, mm3 + pand mm6, mm2 + por mm7, mm6 + + + movq mm6, mm7 + pcmpeqw mm6, mm5 + pand mm7, mm0 + + movq mm1, [eax+ebx+ebx+color2] + pand mm6, mm1 + por mm7, mm6 + movq [final2a], mm7 ;finished 2a + + + ;-------------------------------------------- + + +%ifdef dfhsdfhsdahdsfhdsfh + if (color6 == color3 && color3 == colorA1 && color2 != colorA2 && color3 != colorA0) + product2b = Q_INTERPOLATE (color3, color3, color3, color2); + else + if (color5 == color2 && color2 == colorA2 && colorA1 != color3 && color2 != colorA3) + product2b = Q_INTERPOLATE (color2, color2, color2, color3); + else + product2b = INTERPOLATE (color2, color3); + + if (color6 == color3 && color6 == colorB1 && color5 != colorB2 && color6 != colorB0) + product1b = Q_INTERPOLATE (color6, color6, color6, color5); + else + if (color5 == color2 && color5 == colorB2 && colorB1 != color6 && color5 != colorB3) + product1b = Q_INTERPOLATE (color6, color5, color5, color5); + else + product1b = INTERPOLATE (color5, color6); +%endif + + push eax + add eax, ebx + pxor mm7, mm7 + movq mm0, [eax+ebx+ebx+colorA0] + movq mm1, [eax+ebx+ebx+colorA1] + movq mm2, [eax+ebx+ebx+colorA2] + movq mm3, [eax+ebx+ebx+colorA3] + pop eax + movq mm4, [eax+ebx+ebx+color2] + movq mm5, [eax+ebx+ebx+color3] + movq mm6, [eax+ebx+color6] + + pcmpeqw mm6, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm4, mm2 + pcmpeqw mm0, mm5 + pcmpeqw mm4, mm7 + pcmpeqw mm0, mm7 + pand mm0, mm4 + pand mm6, mm1 + pand mm0, mm6 + + + push eax + add eax, ebx + movq mm1, [eax+ebx+ebx+colorA1] + pop eax + movq mm4, [eax+ebx+ebx+color2] + movq mm5, [eax+ebx+color5] + movq mm6, [eax+ebx+ebx+color3] + + pcmpeqw mm5, mm4 + pcmpeqw mm2, mm4 + pcmpeqw mm1, mm6 + pcmpeqw mm3, mm4 + pcmpeqw mm1, mm7 + pcmpeqw mm3, mm7 + pand mm2, mm5 + pand mm1, mm3 + pand mm1, mm2 + + + movq mm7, mm0 + por mm7, mm1 + + movq mm4, [Mask35] + movq mm3, [Mask26] + + movq mm6, mm4 + pand mm6, mm7 + pxor mm4, mm6 + + movq mm6, mm3 + pand mm6, mm7 + pxor mm3, mm6 + + movq mm2, mm0 + movq mm7, [I2333Pixel] + movq mm6, [I2223Pixel] + movq mm5, [I23Pixel] + + + por mm2, mm4 + pand mm4, [eax+ebx+ebx+color3] + por mm2, mm3 + pand mm3, [eax+ebx+ebx+color2] + por mm2, mm1 + pand mm0, mm7 + pand mm1, mm6 + pxor mm7, mm7 + pcmpeqw mm2, mm7 + por mm0, mm1 + por mm3, mm4 + pand mm2, mm5 + por mm0, mm3 + por mm0, mm2 + movq [final2b], mm0 + + ;----------------------------------- + + + pxor mm7, mm7 + movq mm0, [eax+colorB0] + movq mm1, [eax+colorB1] + movq mm2, [eax+colorB2] + movq mm3, [eax+colorB3] + movq mm4, [eax+ebx+color5] + movq mm5, [eax+ebx+color6] + movq mm6, [eax+ebx+ebx+color3] + + pcmpeqw mm6, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm4, mm2 + pcmpeqw mm0, mm5 + pcmpeqw mm4, mm7 + pcmpeqw mm0, mm7 + pand mm0, mm4 + pand mm6, mm1 + pand mm0, mm6 + + movq mm1, [eax+colorB1] + movq mm4, [eax+ebx+color5] + movq mm5, [eax+ebx+ebx+color2] + movq mm6, [eax+ebx+color6] + + pcmpeqw mm5, mm4 + pcmpeqw mm2, mm4 + pcmpeqw mm1, mm6 + pcmpeqw mm3, mm4 + pcmpeqw mm1, mm7 + pcmpeqw mm3, mm7 + pand mm2, mm5 + pand mm1, mm3 + pand mm1, mm2 + + + movq mm7, mm0 + por mm7, mm1 + + movq mm4, [Mask35] + movq mm3, [Mask26] + + movq mm6, mm4 + pand mm6, mm7 + pxor mm4, mm6 + + movq mm6, mm3 + pand mm6, mm7 + pxor mm3, mm6 + + movq mm2, mm0 + movq mm7, [I5666Pixel] + movq mm6, [I5556Pixel] + movq mm5, [I56Pixel] + + + por mm2, mm4 + pand mm4, [eax+ebx+color5] + por mm2, mm3 + pand mm3, [eax+ebx+color6] + por mm2, mm1 + pand mm0, mm7 + pand mm1, mm6 + pxor mm7, mm7 + pcmpeqw mm2, mm7 + por mm0, mm1 + por mm3, mm4 + pand mm2, mm5 + por mm0, mm3 + por mm0, mm2 + movq [final1b], mm0 + + ;--------- + + movq mm0, [final1a] + movq mm4, [final2a] + movq mm2, [final1b] + movq mm6, [final2b] + + + movq mm1, mm0 + movq mm5, mm4 + + + punpcklwd mm0, mm2 + punpckhwd mm1, mm2 + + punpcklwd mm4, mm6 + punpckhwd mm5, mm6 + + +%ifdef FAR_POINTER + movq [fs:edx], mm0 + movq [fs:edx+8], mm1 + push edx + add edx, [ebp+dstPitch] + movq [fs:edx], mm4 + movq [fs:edx+8], mm5 + pop edx +%else + movq [edx], mm0 + movq [edx+8], mm1 + push edx + add edx, [ebp+dstPitch] + movq [edx], mm4 + movq [edx+8], mm5 + pop edx +%endif +.SKIP_PROCESS: + mov ecx, [ebp+deltaPtr] + add ecx, 8 + mov [ebp+deltaPtr], ecx + add edx, 16 + add eax, 8 + + pop ecx + sub ecx, 4 + cmp ecx, 0 + jg near .Loop + +; Restore some stuff + popad + mov esp, ebp + pop ebp + emms + ret + + +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- + + + +%ifdef __DJGPP__ +__2xSaISuperEagleLine: +%else +_2xSaISuperEagleLine: +%endif +; Store some stuff + push ebp + mov ebp, esp + pushad + +; Prepare the destination +%ifdef FAR_POINTER + ; Set the selector + mov eax, [ebp+dstSegment] + mov fs, ax +%endif + mov edx, [ebp+dstOffset] ; edx points to the screen +; Prepare the source + ; eax points to colorA + mov eax, [ebp+srcPtr] + mov ebx, [ebp+srcPitch] + mov ecx, [ebp+width] + ; eax now points to colorB1 + sub eax, ebx + +; Main Loop +.Loop: push ecx + + ;-----Check Delta------------------ + mov ecx, [ebp+deltaPtr] + + movq mm0, [eax+colorB0] + movq mm1, [eax+colorB3] + movq mm2, [eax+ebx+color4] + movq mm3, [eax+ebx+colorS2] + movq mm4, [eax+ebx+ebx+color1] + movq mm5, [eax+ebx+ebx+colorS1] + push eax + add eax, ebx + movq mm6, [eax+ebx+ebx+colorA0] + movq mm7, [eax+ebx+ebx+colorA3] + pop eax + + pcmpeqw mm0, [ecx+2+colorB0] + pcmpeqw mm1, [ecx+2+colorB3] + pcmpeqw mm2, [ecx+ebx+2+color4] + pcmpeqw mm3, [ecx+ebx+2+colorS2] + pcmpeqw mm4, [ecx+ebx+ebx+2+color1] + pcmpeqw mm5, [ecx+ebx+ebx+2+colorS1] + add ecx, ebx + pcmpeqw mm6, [ecx+ebx+ebx+2+colorA0] + pcmpeqw mm7, [ecx+ebx+ebx+2+colorA3] + sub ecx, ebx + + + pand mm0, mm1 + pand mm2, mm3 + pand mm4, mm5 + pand mm6, mm7 + pand mm0, mm2 + pand mm4, mm6 + pxor mm7, mm7 + pand mm0, mm4 + movq mm6, [eax+colorB0] + pcmpeqw mm7, mm0 + + movq [ecx+2+colorB0], mm6 + + packsswb mm7, mm7 + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_PROCESS + + ;End Delta + + ;--------------------------------- + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+color6] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + movq [I56Pixel], mm0 + movq mm7, mm0 + + ;------------------- + movq mm0, mm7 + movq mm1, mm4 ;5,5,5,6 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + movq [product1a], mm0 + ;-------------------- + + movq mm0, mm7 + movq mm1, mm5 ;6,6,6,5 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [product1b], mm0 + + ;------------------------- + ;------------------------- + movq mm0, [eax+ebx+ebx+color2] + movq mm1, [eax+ebx+ebx+color3] + movq mm2, mm0 + movq mm3, mm1 + movq mm4, mm0 + movq mm5, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [I23Pixel], mm0 + movq mm7, mm0 + + ;--------------------- + movq mm0, mm7 + movq mm1, mm4 ;2,2,2,3 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [product2a], mm0 + + ;---------------------- + movq mm0, mm7 + movq mm1, mm5 ;3,3,3,2 + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 + movq [product2b], mm0 + + + ;//////////////////////////////// + ; Decide which "branch" to take + ;-------------------------------- + movq mm4, [eax+ebx+color5] + movq mm5, [eax+ebx+color6] + movq mm6, [eax+ebx+ebx+color3] + movq mm7, [eax+ebx+ebx+color2] + + pxor mm3, mm3 + movq mm0, mm4 + movq mm1, mm5 + + pcmpeqw mm0, mm6 + pcmpeqw mm1, mm7 + pcmpeqw mm1, mm3 + pand mm0, mm1 + movq [Mask35], mm0 + + movq mm0, [eax+ebx+ebx+colorS1] + movq mm1, [eax+ebx+color4] + push eax + add eax, ebx + movq mm2, [eax+ebx+ebx+colorA2] + pop eax + movq mm3, [eax+colorB1] + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm4 + pcmpeqw mm3, mm4 + pand mm0, mm1 + pand mm2, mm3 + por mm0, mm2 + pand mm0, [Mask35] + movq [Mask35b], mm0 + + ;----------- + pxor mm3, mm3 + movq mm0, mm4 + movq mm1, mm5 + + pcmpeqw mm0, mm6 + pcmpeqw mm1, mm7 + pcmpeqw mm0, mm3 + pand mm0, mm1 + movq [Mask26], mm0 + + movq mm0, [eax+ebx+ebx+color1] + movq mm1, [eax+ebx+colorS2] + push eax + add eax, ebx + movq mm2, [eax+ebx+ebx+colorA1] + pop eax + movq mm3, [eax+colorB2] + pcmpeqw mm0, mm5 + pcmpeqw mm1, mm5 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm1 + pand mm2, mm3 + por mm0, mm2 + pand mm0, [Mask26] + movq [Mask26b], mm0 + + ;-------------------- + movq mm0, mm4 + movq mm1, mm5 + movq mm2, mm0 + + pcmpeqw mm2, mm1 + pcmpeqw mm0, mm6 + pcmpeqw mm1, mm7 + pand mm0, mm1 + pand mm2, mm0 + pxor mm0, mm2 + movq mm7, mm0 + + ;------------------ + packsswb mm7, mm7 + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_GUESS + +;--------------------------------------------- +; Map of the pixels: I|E F|J +; G|A B|K +; H|C D|L +; M|N O|P + movq mm6, mm0 + movq mm4, [eax+ebx+color5] + movq mm5, [eax+ebx+color6] + pxor mm7, mm7 + pand mm6, [ONE] + + movq mm0, [eax+colorB1] + movq mm1, [eax+ebx+color4] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+colorB2] + movq mm1, [eax+ebx+colorS2] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + push eax + add eax, ebx + movq mm0, [eax+ebx+color1] + movq mm1, [eax+ebx+ebx+colorA1] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+ebx+colorS1] + movq mm1, [eax+ebx+ebx+colorA2] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + pop eax + movq mm1, mm7 + pxor mm0, mm0 + pcmpgtw mm7, mm0 + pcmpgtw mm0, mm1 + + por mm7, [Mask35] + por mm0, [Mask26] + movq [Mask35], mm7 + movq [Mask26], mm0 + +.SKIP_GUESS: + ;Start the ASSEMBLY !!! + + movq mm4, [Mask35] + movq mm5, [Mask26] + movq mm6, [Mask35b] + movq mm7, [Mask26b] + + movq mm0, [eax+ebx+color5] + movq mm1, [eax+ebx+color6] + movq mm2, [eax+ebx+ebx+color2] + movq mm3, [eax+ebx+ebx+color3] + pcmpeqw mm0, mm2 + pcmpeqw mm1, mm3 + movq mm2, mm4 + movq mm3, mm5 + por mm0, mm1 + por mm2, mm3 + pand mm2, mm0 + pxor mm0, mm2 + movq mm3, mm0 + + movq mm2, mm0 + pxor mm0, mm0 + por mm2, mm4 + pxor mm4, mm6 + por mm2, mm5 + pxor mm5, mm7 + pcmpeqw mm2, mm0 + ;---------------- + + movq mm0, [eax+ebx+color5] + movq mm1, mm3 + por mm1, mm4 + por mm1, mm6 + pand mm0, mm1 + movq mm1, mm5 + pand mm1, [I56Pixel] + por mm0, mm1 + movq mm1, mm7 + pand mm1, [product1b] + por mm0, mm1 + movq mm1, mm2 + pand mm1, [product1a] + por mm0, mm1 + movq [final1a], mm0 + + movq mm0, [eax+ebx+color6] + movq mm1, mm3 + por mm1, mm5 + por mm1, mm7 + pand mm0, mm1 + movq mm1, mm4 + pand mm1, [I56Pixel] + por mm0, mm1 + movq mm1, mm6 + pand mm1, [product1a] + por mm0, mm1 + movq mm1, mm2 + pand mm1, [product1b] + por mm0, mm1 + movq [final1b], mm0 + + movq mm0, [eax+ebx+ebx+color2] + movq mm1, mm3 + por mm1, mm5 + por mm1, mm7 + pand mm0, mm1 + movq mm1, mm4 + pand mm1, [I23Pixel] + por mm0, mm1 + movq mm1, mm6 + pand mm1, [product2b] + por mm0, mm1 + movq mm1, mm2 + pand mm1, [product2a] + por mm0, mm1 + movq [final2a], mm0 + + movq mm0, [eax+ebx+ebx+color3] + movq mm1, mm3 + por mm1, mm4 + por mm1, mm6 + pand mm0, mm1 + movq mm1, mm5 + pand mm1, [I23Pixel] + por mm0, mm1 + movq mm1, mm7 + pand mm1, [product2a] + por mm0, mm1 + movq mm1, mm2 + pand mm1, [product2b] + por mm0, mm1 + movq [final2b], mm0 + + + movq mm0, [final1a] + movq mm2, [final1b] + movq mm1, mm0 + movq mm4, [final2a] + movq mm6, [final2b] + movq mm5, mm4 + punpcklwd mm0, mm2 + punpckhwd mm1, mm2 + punpcklwd mm4, mm6 + punpckhwd mm5, mm6 + + + + +%ifdef FAR_POINTER + movq [fs:edx], mm0 + movq [fs:edx+8], mm1 + push edx + add edx, [ebp+dstPitch] + movq [fs:edx], mm4 + movq [fs:edx+8], mm5 + pop edx +%else + movq [edx], mm0 + movq [edx+8], mm1 + push edx + add edx, [ebp+dstPitch] + movq [edx], mm4 + movq [edx+8], mm5 + pop edx +%endif +.SKIP_PROCESS: + mov ecx, [ebp+deltaPtr] + add ecx, 8 + mov [ebp+deltaPtr], ecx + add edx, 16 + add eax, 8 + + pop ecx + sub ecx, 4 + cmp ecx, 0 + jg near .Loop + +; Restore some stuff + popad + mov esp, ebp + pop ebp + emms + ret + + +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- + + +;This is version 0.50 +colorI equ -2 +colorE equ 0 +colorF equ 2 +colorJ equ 4 + +colorG equ -2 +colorA equ 0 +colorB equ 2 +colorK equ 4 + +colorH equ -2 +colorC equ 0 +colorD equ 2 +colorL equ 4 + +colorM equ -2 +colorN equ 0 +colorO equ 2 +colorP equ 4 + +%ifdef __DJGPP__ +__2xSaILine: +%else +_2xSaILine: +%endif +; Store some stuff + push ebp + mov ebp, esp + pushad + +; Prepare the destination +%ifdef FAR_POINTER + ; Set the selector + mov eax, [ebp+dstSegment] + mov fs, ax +%endif + mov edx, [ebp+dstOffset] ; edx points to the screen +; Prepare the source + ; eax points to colorA + mov eax, [ebp+srcPtr] + mov ebx, [ebp+srcPitch] + mov ecx, [ebp+width] + ; eax now points to colorE + sub eax, ebx + + +; Main Loop +.Loop: push ecx + + ;-----Check Delta------------------ + mov ecx, [ebp+deltaPtr] + + movq mm0, [eax+colorI] + movq mm1, [eax+colorJ] + movq mm2, [eax+ebx+colorG] + movq mm3, [eax+ebx+colorK] + movq mm4, [eax+ebx+ebx+colorH] + movq mm5, [eax+ebx+ebx+colorL] + push eax + add eax, ebx + movq mm6, [eax+ebx+ebx+colorM] + movq mm7, [eax+ebx+ebx+colorP] + pop eax + + pcmpeqw mm0, [ecx+2+colorI] + pcmpeqw mm1, [ecx+2+colorK] + pcmpeqw mm2, [ecx+ebx+2+colorG] + pcmpeqw mm3, [ecx+ebx+2+colorK] + pcmpeqw mm4, [ecx+ebx+ebx+2+colorH] + pcmpeqw mm5, [ecx+ebx+ebx+2+colorL] + add ecx, ebx + pcmpeqw mm6, [ecx+ebx+ebx+2+colorM] + pcmpeqw mm7, [ecx+ebx+ebx+2+colorP] + sub ecx, ebx + + + pand mm0, mm1 + pand mm2, mm3 + pand mm4, mm5 + pand mm6, mm7 + pand mm0, mm2 + pand mm4, mm6 + pxor mm7, mm7 + pand mm0, mm4 + movq mm6, [eax+colorI] + pcmpeqw mm7, mm0 + + movq [ecx+2+colorI], mm6 + + packsswb mm7, mm7 + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_PROCESS + + ;End Delta + + ;--------------------------------- + + +;1 + ;if ((colorA == colorD) && (colorB != colorC) && (colorA == colorE) && (colorB == colorL) + movq mm0, [eax+ebx+colorA] ;mm0 and mm1 contain colorA + movq mm2, [eax+ebx+colorB] ;mm2 and mm3 contain colorB + + movq mm1, mm0 + movq mm3, mm2 + + pcmpeqw mm0, [eax+ebx+ebx+colorD] + pcmpeqw mm1, [eax+colorE] + pcmpeqw mm2, [eax+ebx+ebx+colorL] + pcmpeqw mm3, [eax+ebx+ebx+colorC] + + pand mm0, mm1 + pxor mm1, mm1 + pand mm0, mm2 + pcmpeqw mm3, mm1 + pand mm0, mm3 ;result in mm0 + + ;if ((colorA == colorC) && (colorB != colorE) && (colorA == colorF) && (colorB == colorJ) + movq mm4, [eax+ebx+colorA] ;mm4 and mm5 contain colorA + movq mm6, [eax+ebx+colorB] ;mm6 and mm7 contain colorB + movq mm5, mm4 + movq mm7, mm6 + + pcmpeqw mm4, [eax+ebx+ebx+colorC] + pcmpeqw mm5, [eax+colorF] + pcmpeqw mm6, [eax+colorJ] + pcmpeqw mm7, [eax+colorE] + + pand mm4, mm5 + pxor mm5, mm5 + pand mm4, mm6 + pcmpeqw mm7, mm5 + pand mm4, mm7 ;result in mm4 + + por mm0, mm4 ;combine the masks + movq [Mask1], mm0 + + ;-------------------------------------------- + +;2 + ;if ((colorB == colorC) && (colorA != colorD) && (colorB == colorF) && (colorA == colorH) + movq mm0, [eax+ebx+colorB] ;mm0 and mm1 contain colorB + movq mm2, [eax+ebx+colorA] ;mm2 and mm3 contain colorA + movq mm1, mm0 + movq mm3, mm2 + + pcmpeqw mm0, [eax+ebx+ebx+colorC] + pcmpeqw mm1, [eax+colorF] + pcmpeqw mm2, [eax+ebx+ebx+colorH] + pcmpeqw mm3, [eax+ebx+ebx+colorD] + + pand mm0, mm1 + pxor mm1, mm1 + pand mm0, mm2 + pcmpeqw mm3, mm1 + pand mm0, mm3 ;result in mm0 + + ;if ((colorB == colorE) && (colorB == colorD) && (colorA != colorF) && (colorA == colorI) + movq mm4, [eax+ebx+colorB] ;mm4 and mm5 contain colorB + movq mm6, [eax+ebx+colorA] ;mm6 and mm7 contain colorA + movq mm5, mm4 + movq mm7, mm6 + + pcmpeqw mm4, [eax+ebx+ebx+colorD] + pcmpeqw mm5, [eax+colorE] + pcmpeqw mm6, [eax+colorI] + pcmpeqw mm7, [eax+colorF] + + pand mm4, mm5 + pxor mm5, mm5 + pand mm4, mm6 + pcmpeqw mm7, mm5 + pand mm4, mm7 ;result in mm4 + + por mm0, mm4 ;combine the masks + movq [Mask2], mm0 + + +;interpolate colorA and colorB + movq mm0, [eax+ebx+colorA] + movq mm1, [eax+ebx+colorB] + + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + + ;assemble the pixels + movq mm1, [eax+ebx+colorA] + movq mm2, [eax+ebx+colorB] + + movq mm3, [Mask1] + movq mm5, mm1 + movq mm4, [Mask2] + movq mm6, mm1 + + pand mm1, mm3 + por mm3, mm4 + pxor mm7, mm7 + pand mm2, mm4 + + pcmpeqw mm3, mm7 + por mm1, mm2 + pand mm0, mm3 + + por mm0, mm1 + + punpcklwd mm5, mm0 + punpckhwd mm6, mm0 + +%ifdef FAR_POINTER + movq [fs:edx], mm5 + movq [fs:edx+8], mm6 +%else + movq [edx], mm5 + movq [edx+8], mm6 +%endif + +;------------------------------------------------ +; Create the Nextline +;------------------------------------------------ +;3 ;if ((colorA == colorD) && (colorB != colorC) && (colorA == colorG) && (colorC == colorO) + movq mm0, [eax+ebx+colorA] ;mm0 and mm1 contain colorA + movq mm2, [eax+ebx+ebx+colorC] ;mm2 and mm3 contain colorC + movq mm1, mm0 + movq mm3, mm2 + + push eax + add eax, ebx + pcmpeqw mm0, [eax+ebx+colorD] + pcmpeqw mm1, [eax+colorG] + pcmpeqw mm2, [eax+ebx+ebx+colorO] + pcmpeqw mm3, [eax+colorB] + pop eax + + pand mm0, mm1 + pxor mm1, mm1 + pand mm0, mm2 + pcmpeqw mm3, mm1 + pand mm0, mm3 ;result in mm0 + + ;if ((colorA == colorB) && (colorG != colorC) && (colorA == colorH) && (colorC == colorM) + movq mm4, [eax+ebx+colorA] ;mm4 and mm5 contain colorA + movq mm6, [eax+ebx+ebx+colorC] ;mm6 and mm7 contain colorC + movq mm5, mm4 + movq mm7, mm6 + + push eax + add eax, ebx + pcmpeqw mm4, [eax+ebx+colorH] + pcmpeqw mm5, [eax+colorB] + pcmpeqw mm6, [eax+ebx+ebx+colorM] + pcmpeqw mm7, [eax+colorG] + pop eax + + pand mm4, mm5 + pxor mm5, mm5 + pand mm4, mm6 + pcmpeqw mm7, mm5 + pand mm4, mm7 ;result in mm4 + + por mm0, mm4 ;combine the masks + movq [Mask1], mm0 + ;-------------------------------------------- + +;4 + ;if ((colorB == colorC) && (colorA != colorD) && (colorC == colorH) && (colorA == colorF) + movq mm0, [eax+ebx+ebx+colorC] ;mm0 and mm1 contain colorC + movq mm2, [eax+ebx+colorA] ;mm2 and mm3 contain colorA + movq mm1, mm0 + movq mm3, mm2 + + pcmpeqw mm0, [eax+ebx+colorB] + pcmpeqw mm1, [eax+ebx+ebx+colorH] + pcmpeqw mm2, [eax+colorF] + pcmpeqw mm3, [eax+ebx+ebx+colorD] + + pand mm0, mm1 + pxor mm1, mm1 + pand mm0, mm2 + pcmpeqw mm3, mm1 + pand mm0, mm3 ;result in mm0 + + ;if ((colorC == colorG) && (colorC == colorD) && (colorA != colorH) && (colorA == colorI) + movq mm4, [eax+ebx+ebx+colorC] ;mm4 and mm5 contain colorC + movq mm6, [eax+ebx+colorA] ;mm6 and mm7 contain colorA + movq mm5, mm4 + movq mm7, mm6 + + pcmpeqw mm4, [eax+ebx+ebx+colorD] + pcmpeqw mm5, [eax+ebx+colorG] + pcmpeqw mm6, [eax+colorI] + pcmpeqw mm7, [eax+ebx+ebx+colorH] + + pand mm4, mm5 + pxor mm5, mm5 + pand mm4, mm6 + pcmpeqw mm7, mm5 + pand mm4, mm7 ;result in mm4 + + por mm0, mm4 ;combine the masks + movq [Mask2], mm0 + ;---------------------------------------------- + +;interpolate colorA and colorC + movq mm0, [eax+ebx+colorA] + movq mm1, [eax+ebx+ebx+colorC] + + movq mm2, mm0 + movq mm3, mm1 + + pand mm0, [colorMask] + pand mm1, [colorMask] + + psrlw mm0, 1 + psrlw mm1, 1 + + pand mm3, [lowPixelMask] + paddw mm0, mm1 + + pand mm3, mm2 + paddw mm0, mm3 ;mm0 contains the interpolated values + ;------------- + + ;assemble the pixels + movq mm1, [eax+ebx+colorA] + movq mm2, [eax+ebx+ebx+colorC] + + movq mm3, [Mask1] + movq mm4, [Mask2] + + pand mm1, mm3 + pand mm2, mm4 + + por mm3, mm4 + pxor mm7, mm7 + por mm1, mm2 + + pcmpeqw mm3, mm7 + pand mm0, mm3 + por mm0, mm1 + movq [ACPixel], mm0 + +;//////////////////////////////// +; Decide which "branch" to take +;-------------------------------- + movq mm0, [eax+ebx+colorA] + movq mm1, [eax+ebx+colorB] + movq mm6, mm0 + movq mm7, mm1 + pcmpeqw mm0, [eax+ebx+ebx+colorD] + pcmpeqw mm1, [eax+ebx+ebx+colorC] + pcmpeqw mm6, mm7 + + movq mm2, mm0 + movq mm3, mm0 + + pand mm0, mm1 ;colorA == colorD && colorB == colorC + pxor mm7, mm7 + + pcmpeqw mm2, mm7 + pand mm6, mm0 + pand mm2, mm1 ;colorA != colorD && colorB == colorC + + pcmpeqw mm1, mm7 + + pand mm1, mm3 ;colorA == colorD && colorB != colorC + pxor mm0, mm6 + por mm1, mm6 + movq mm7, mm0 + movq [Mask2], mm2 + packsswb mm7, mm7 + movq [Mask1], mm1 + + movd ecx, mm7 + test ecx, ecx + jz near .SKIP_GUESS + +;--------------------------------------------- +; Map of the pixels: I|E F|J +; G|A B|K +; H|C D|L +; M|N O|P + movq mm6, mm0 + movq mm4, [eax+ebx+colorA] + movq mm5, [eax+ebx+colorB] + pxor mm7, mm7 + pand mm6, [ONE] + + movq mm0, [eax+colorE] + movq mm1, [eax+ebx+colorG] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+colorF] + movq mm1, [eax+ebx+colorK] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + push eax + add eax, ebx + movq mm0, [eax+ebx+colorH] + movq mm1, [eax+ebx+ebx+colorN] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + movq mm0, [eax+ebx+colorL] + movq mm1, [eax+ebx+ebx+colorO] + movq mm2, mm0 + movq mm3, mm1 + pcmpeqw mm0, mm4 + pcmpeqw mm1, mm4 + pcmpeqw mm2, mm5 + pcmpeqw mm3, mm5 + pand mm0, mm6 + pand mm1, mm6 + pand mm2, mm6 + pand mm3, mm6 + paddw mm0, mm1 + paddw mm2, mm3 + + pxor mm3, mm3 + pcmpgtw mm0, mm6 + pcmpgtw mm2, mm6 + pcmpeqw mm0, mm3 + pcmpeqw mm2, mm3 + pand mm0, mm6 + pand mm2, mm6 + paddw mm7, mm0 + psubw mm7, mm2 + + pop eax + movq mm1, mm7 + pxor mm0, mm0 + pcmpgtw mm7, mm0 + pcmpgtw mm0, mm1 + + por mm7, [Mask1] + por mm0, [Mask2] + movq [Mask1], mm7 + movq [Mask2], mm0 + +.SKIP_GUESS: + ;---------------------------- + ;interpolate A, B, C and D + movq mm0, [eax+ebx+colorA] + movq mm1, [eax+ebx+colorB] + movq mm4, mm0 + movq mm2, [eax+ebx+ebx+colorC] + movq mm5, mm1 + movq mm3, [qcolorMask] + movq mm6, mm2 + movq mm7, [qlowpixelMask] + + pand mm0, mm3 + pand mm1, mm3 + pand mm2, mm3 + pand mm3, [eax+ebx+ebx+colorD] + + psrlw mm0, 2 + pand mm4, mm7 + psrlw mm1, 2 + pand mm5, mm7 + psrlw mm2, 2 + pand mm6, mm7 + psrlw mm3, 2 + pand mm7, [eax+ebx+ebx+colorD] + + paddw mm0, mm1 + paddw mm2, mm3 + + paddw mm4, mm5 + paddw mm6, mm7 + + paddw mm4, mm6 + paddw mm0, mm2 + psrlw mm4, 2 + pand mm4, [qlowpixelMask] + paddw mm0, mm4 ;mm0 contains the interpolated value of A, B, C and D + +;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ + ;assemble the pixels + movq mm1, [Mask1] + movq mm2, [Mask2] + movq mm4, [eax+ebx+colorA] + movq mm5, [eax+ebx+colorB] + pand mm4, mm1 + pand mm5, mm2 + + pxor mm7, mm7 + por mm1, mm2 + por mm4, mm5 + pcmpeqw mm1, mm7 + pand mm0, mm1 + por mm4, mm0 ;mm4 contains the diagonal pixels + + movq mm0, [ACPixel] + movq mm1, mm0 + punpcklwd mm0, mm4 + punpckhwd mm1, mm4 + + push edx + add edx, [ebp+dstPitch] + +%ifdef FAR_POINTER + movq [fs:edx], mm0 + movq [fs:edx+8], mm1 +%else + movq [edx], mm0 + movq [edx+8], mm1 +%endif + pop edx + +.SKIP_PROCESS: + mov ecx, [ebp+deltaPtr] + add ecx, 8 + mov [ebp+deltaPtr], ecx + add edx, 16 + add eax, 8 + + pop ecx + sub ecx, 4 + cmp ecx, 0 + jg near .Loop + +; Restore some stuff + popad + mov esp, ebp + pop ebp + emms + ret + +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- + +%ifdef __DJGPP__ +_Init_2xSaIMMX: +%else +Init_2xSaIMMX: +%endif +; Store some stuff + push ebp + mov ebp, esp + push edx + + +;Damn thing doesn't work +; mov eax,1 +; cpuid +; test edx, 0x00800000 ;test bit 23 +; jz end2 ;bit not set => no MMX detected + + mov eax, [ebp+8] ;PixelFormat + cmp eax, 555 + jz Bits555 + cmp eax, 565 + jz Bits565 +end2: + mov eax, 1 + jmp end3 +Bits555: + mov edx, 0x7BDE7BDE + mov eax, colorMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x04210421 + mov eax, lowPixelMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x739C739C + mov eax, qcolorMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x0C630C63 + mov eax, qlowpixelMask + mov [eax], edx + mov [eax+4], edx + mov eax, 0 + jmp end3 +Bits565: + mov edx, 0xF7DEF7DE + mov eax, colorMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x08210821 + mov eax, lowPixelMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0xE79CE79C + mov eax, qcolorMask + mov [eax], edx + mov [eax+4], edx + mov edx, 0x18631863 + mov eax, qlowpixelMask + mov [eax], edx + mov [eax+4], edx + mov eax, 0 + jmp end3 +end3: + pop edx + mov esp, ebp + pop ebp + ret + + +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- +;------------------------------------------------------------------------- + + SECTION .data ALIGN = 32 +;Some constants +colorMask dd 0xF7DEF7DE,0xF7DEF7DE +lowPixelMask dd 0x08210821,0x08210821 + +qcolorMask dd 0xE79CE79C,0xE79CE79C +qlowpixelMask dd 0x18631863,0x18631863 + +darkenMask dd 0xC718C718,0xC718C718 +GreenMask dd 0x07E007E0,0x07E007E0 +RedBlueMask dd 0xF81FF81F,0xF81FF81F + +FALSE dd 0x00000000,0x00000000 +TRUE dd 0xffffffff,0xffffffff +ONE dd 0x00010001,0x00010001 + + + SECTION .bss ALIGN = 32 +ACPixel resb 8 +Mask1 resb 8 +Mask2 resb 8 + +I56Pixel resb 8 +I23Pixel resb 8 +I5556Pixel resb 8 +I2223Pixel resb 8 +I5666Pixel resb 8 +I2333Pixel resb 8 +Mask26 resb 8 +Mask35 resb 8 +Mask26b resb 8 +Mask35b resb 8 +product1a resb 8 +product1b resb 8 +product2a resb 8 +product2b resb 8 +final1a resb 8 +final1b resb 8 +final2a resb 8 +final2b resb 8 diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,22 @@ +SUFFIXES = .asm + +noinst_LIBRARIES = lib386.a libfilter.a + +lib386_a_SOURCES = 2xSaImmx.asm + +.asm.o: + $(NASM) -f elf -o $@ $< + +libfilter_a_SOURCES = \ + 2xSaI.cpp \ + admame.cpp \ + bilinear.cpp \ + hq2x.cpp \ + hq2x.h \ + interframe.cpp \ + interp.h \ + lq2x.h \ + motionblur.cpp \ + pixel.cpp \ + scanline.cpp \ + simple2x.cpp diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/admame.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/admame.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1036 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 1999-2002 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +/* + * Alternatively at the previous license terms, you are allowed to use this + * code in your program with these conditions: + * - the program is not used in commercial activities. + * - the whole source code of the program is released with the binary. + */ + +#include "../Port.h" + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif + +static void internal_scale2x_16_def(u16 *dst, const u16 *src0, const u16 *src1, const u16 *src2, unsigned count) +{ + /* first pixel */ + dst[0] = src1[0]; + if (src1[1] == src0[0] && src2[0] != src0[0]) + dst[1] = src0[0]; + else + dst[1] = src1[0]; + ++src0; + ++src1; + ++src2; + dst += 2; + + /* central pixels */ + count -= 2; + while (count) + { + if (src0[0] != src2[0] && src1[-1] != src1[1]) + { + dst[0] = src1[-1] == src0[0] ? src0[0] : src1[0]; + dst[1] = src1[1] == src0[0] ? src0[0] : src1[0]; + } + else + { + dst[0] = src1[0]; + dst[1] = src1[0]; + } + + ++src0; + ++src1; + ++src2; + dst += 2; + --count; + } + + /* last pixel */ + if (src1[-1] == src0[0] && src2[0] != src0[0]) + dst[0] = src0[0]; + else + dst[0] = src1[0]; + dst[1] = src1[0]; +} + +static void internal_scale2x_32_def(u32 *dst, + const u32 *src0, + const u32 *src1, + const u32 *src2, + unsigned count) +{ + /* first pixel */ + dst[0] = src1[0]; + if (src1[1] == src0[0] && src2[0] != src0[0]) + dst[1] = src0[0]; + else + dst[1] = src1[0]; + ++src0; + ++src1; + ++src2; + dst += 2; + + /* central pixels */ + count -= 2; + while (count) + { + if (src0[0] != src2[0] && src1[-1] != src1[1]) + { + dst[0] = src1[-1] == src0[0] ? src0[0] : src1[0]; + dst[1] = src1[1] == src0[0] ? src0[0] : src1[0]; + } + else + { + dst[0] = src1[0]; + dst[1] = src1[0]; + } + + ++src0; + ++src1; + ++src2; + dst += 2; + --count; + } + + /* last pixel */ + if (src1[-1] == src0[0] && src2[0] != src0[0]) + dst[0] = src0[0]; + else + dst[0] = src1[0]; + dst[1] = src1[0]; +} + +#ifdef MMX +static void internal_scale2x_16_mmx_single(u16 *dst, const u16 *src0, const u16 *src1, const u16 *src2, unsigned count) +{ + /* always do the first and last run */ + count -= 2 * 4; + +#ifdef __GNUC__ + __asm__ __volatile__ ( + /* first run */ + /* set the current, current_pre, current_next registers */ + "movq 0(%1), %%mm0\n" + "movq 0(%1),%%mm7\n" + "movq 8(%1),%%mm1\n" + "psllq $48,%%mm0\n" + "psllq $48,%%mm1\n" + "psrlq $48, %%mm0\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $16,%%mm2\n" + "psrlq $16,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqw %%mm6,%%mm2\n" + "pcmpeqw %%mm6,%%mm4\n" + "pcmpeqw (%2),%%mm3\n" + "pcmpeqw (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqw %%mm1,%%mm2\n" + "pcmpeqw %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpcklwd %%mm4,%%mm2\n" + "punpckhwd %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + + /* next */ + "addl $8,%0\n" + "addl $8,%1\n" + "addl $8,%2\n" + "addl $16,%3\n" + + /* central runs */ + "shrl $2,%4\n" + "jz 1f\n" + + "0:\n" + + /* set the current, current_pre, current_next registers */ + "movq -8(%1),%%mm0\n" + "movq (%1),%%mm7\n" + "movq 8(%1),%%mm1\n" + "psrlq $48,%%mm0\n" + "psllq $48,%%mm1\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $16,%%mm2\n" + "psrlq $16,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqw %%mm6,%%mm2\n" + "pcmpeqw %%mm6,%%mm4\n" + "pcmpeqw (%2),%%mm3\n" + "pcmpeqw (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqw %%mm1,%%mm2\n" + "pcmpeqw %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpcklwd %%mm4,%%mm2\n" + "punpckhwd %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + + /* next */ + "addl $8,%0\n" + "addl $8,%1\n" + "addl $8,%2\n" + "addl $16,%3\n" + + "decl %4\n" + "jnz 0b\n" + "1:\n" + + /* final run */ + /* set the current, current_pre, current_next registers */ + "movq (%1),%%mm1\n" + "movq (%1),%%mm7\n" + "movq -8(%1),%%mm0\n" + "psrlq $48,%%mm1\n" + "psrlq $48,%%mm0\n" + "psllq $48,%%mm1\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $16,%%mm2\n" + "psrlq $16,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqw %%mm6,%%mm2\n" + "pcmpeqw %%mm6,%%mm4\n" + "pcmpeqw (%2),%%mm3\n" + "pcmpeqw (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqw %%mm1,%%mm2\n" + "pcmpeqw %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpcklwd %%mm4,%%mm2\n" + "punpckhwd %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + "emms\n" + + : "+r" (src0), "+r" (src1), "+r" (src2), "+r" (dst), "+r" (count) + : + : "cc" + ); +#else + __asm { + mov eax, src0; + mov ebx, src1; + mov ecx, src2; + mov edx, dst; + mov esi, count; + + /* first run */ + /* set the current, current_pre, current_next registers */ + movq mm0, qword ptr [ebx]; + movq mm7, qword ptr [ebx]; + movq mm1, qword ptr [ebx + 8]; + psllq mm0, 48; + psllq mm1, 48; + psrlq mm0, 48; + movq mm2, mm7; + movq mm3, mm7; + psllq mm2, 16; + psrlq mm3, 16; + por mm0, mm2; + por mm1, mm3; + + /* current_upper */ + movq mm6, qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2, mm0; + movq mm4, mm1; + movq mm3, mm0; + movq mm5, mm1; + pcmpeqw mm2, mm6; + pcmpeqw mm4, mm6; + pcmpeqw mm3, qword ptr [ecx]; + pcmpeqw mm5, qword ptr [ecx]; + pandn mm3, mm2; + pandn mm5, mm4; + movq mm2, mm0; + movq mm4, mm1; + pcmpeqw mm2, mm1; + pcmpeqw mm4, mm0; + pandn mm2, mm3; + pandn mm4, mm5; + movq mm3, mm2; + movq mm5, mm4; + pand mm2, mm6; + pand mm4, mm6; + pandn mm3, mm7; + pandn mm5, mm7; + por mm2, mm3; + por mm4, mm5; + + /* set *dst0 */ + movq mm3, mm2; + punpcklwd mm2, mm4; + punpckhwd mm3, mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx + 8], mm3; + + /* next */ + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 16; + + /* central runs */ + shr esi, 2; + jz label1; + align 4; +label0: + + /* set the current, current_pre, current_next registers */ + movq mm0, qword ptr [ebx - 8]; + movq mm7, qword ptr [ebx]; + movq mm1, qword ptr [ebx + 8]; + psrlq mm0, 48; + psllq mm1, 48; + movq mm2, mm7; + movq mm3, mm7; + psllq mm2, 16; + psrlq mm3, 16; + por mm0, mm2; + por mm1, mm3; + + /* current_upper */ + movq mm6, qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2, mm0; + movq mm4, mm1; + movq mm3, mm0; + movq mm5, mm1; + pcmpeqw mm2, mm6; + pcmpeqw mm4, mm6; + pcmpeqw mm3, qword ptr [ecx]; + pcmpeqw mm5, qword ptr [ecx]; + pandn mm3, mm2; + pandn mm5, mm4; + movq mm2, mm0; + movq mm4, mm1; + pcmpeqw mm2, mm1; + pcmpeqw mm4, mm0; + pandn mm2, mm3; + pandn mm4, mm5; + movq mm3, mm2; + movq mm5, mm4; + pand mm2, mm6; + pand mm4, mm6; + pandn mm3, mm7; + pandn mm5, mm7; + por mm2, mm3; + por mm4, mm5; + + /* set *dst */ + movq mm3, mm2; + punpcklwd mm2, mm4; + punpckhwd mm3, mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx + 8], mm3; + + /* next */ + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 16; + + dec esi; + jnz label0; +label1: + + /* final run */ + /* set the current, current_pre, current_next registers */ + movq mm1, qword ptr [ebx]; + movq mm7, qword ptr [ebx]; + movq mm0, qword ptr [ebx - 8]; + psrlq mm1, 48; + psrlq mm0, 48; + psllq mm1, 48; + movq mm2, mm7; + movq mm3, mm7; + psllq mm2, 16; + psrlq mm3, 16; + por mm0, mm2; + por mm1, mm3; + + /* current_upper */ + movq mm6, qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2, mm0; + movq mm4, mm1; + movq mm3, mm0; + movq mm5, mm1; + pcmpeqw mm2, mm6; + pcmpeqw mm4, mm6; + pcmpeqw mm3, qword ptr [ecx]; + pcmpeqw mm5, qword ptr [ecx]; + pandn mm3, mm2; + pandn mm5, mm4; + movq mm2, mm0; + movq mm4, mm1; + pcmpeqw mm2, mm1; + pcmpeqw mm4, mm0; + pandn mm2, mm3; + pandn mm4, mm5; + movq mm3, mm2; + movq mm5, mm4; + pand mm2, mm6; + pand mm4, mm6; + pandn mm3, mm7; + pandn mm5, mm7; + por mm2, mm3; + por mm4, mm5; + + /* set *dst */ + movq mm3, mm2; + punpcklwd mm2, mm4; + punpckhwd mm3, mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx + 8], mm3; + + mov src0, eax; + mov src1, ebx; + mov src2, ecx; + mov dst, edx; + mov count, esi; + + emms; + } +#endif +} + +static void internal_scale2x_32_mmx_single(u32 *dst, const u32 *src0, const u32 *src1, const u32 *src2, unsigned count) +{ + /* always do the first and last run */ + count -= 2 * 2; + +#ifdef __GNUC__ + __asm__ __volatile__ ( + /* first run */ + /* set the current, current_pre, current_next registers */ + "movq 0(%1),%%mm0\n" + "movq 0(%1),%%mm7\n" + "movq 8(%1),%%mm1\n" + "psllq $32,%%mm0\n" + "psllq $32,%%mm1\n" + "psrlq $32,%%mm0\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $32,%%mm2\n" + "psrlq $32,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqd %%mm6,%%mm2\n" + "pcmpeqd %%mm6,%%mm4\n" + "pcmpeqd (%2),%%mm3\n" + "pcmpeqd (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqd %%mm1,%%mm2\n" + "pcmpeqd %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpckldq %%mm4,%%mm2\n" + "punpckhdq %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3, 8(%3)\n" + + /* next */ + "addl $8,%0\n" + "addl $8,%1\n" + "addl $8,%2\n" + "addl $16,%3\n" + + /* central runs */ + "shrl $1,%4\n" + "jz 1f\n" + + "0:\n" + + /* set the current, current_pre, current_next registers */ + "movq -8(%1),%%mm0\n" + "movq (%1),%%mm7\n" + "movq 8(%1),%%mm1\n" + "psrlq $32,%%mm0\n" + "psllq $32,%%mm1\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $32,%%mm2\n" + "psrlq $32,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqd %%mm6,%%mm2\n" + "pcmpeqd %%mm6,%%mm4\n" + "pcmpeqd (%2),%%mm3\n" + "pcmpeqd (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqd %%mm1,%%mm2\n" + "pcmpeqd %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpckldq %%mm4,%%mm2\n" + "punpckhdq %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + + /* next */ + "addl $8,%0\n" + "addl $8,%1\n" + "addl $8,%2\n" + "addl $16,%3\n" + + "decl %4\n" + "jnz 0b\n" + "1:\n" + + /* final run */ + /* set the current, current_pre, current_next registers */ + "movq (%1),%%mm1\n" + "movq (%1),%%mm7\n" + "movq -8(%1), %%mm0\n" + "psrlq $32,%%mm1\n" + "psrlq $32,%%mm0\n" + "psllq $32,%%mm1\n" + "movq %%mm7,%%mm2\n" + "movq %%mm7,%%mm3\n" + "psllq $32,%%mm2\n" + "psrlq $32,%%mm3\n" + "por %%mm2,%%mm0\n" + "por %%mm3,%%mm1\n" + + /* current_upper */ + "movq (%0),%%mm6\n" + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "movq %%mm0,%%mm3\n" + "movq %%mm1,%%mm5\n" + "pcmpeqd %%mm6,%%mm2\n" + "pcmpeqd %%mm6,%%mm4\n" + "pcmpeqd (%2),%%mm3\n" + "pcmpeqd (%2),%%mm5\n" + "pandn %%mm2,%%mm3\n" + "pandn %%mm4,%%mm5\n" + "movq %%mm0,%%mm2\n" + "movq %%mm1,%%mm4\n" + "pcmpeqd %%mm1,%%mm2\n" + "pcmpeqd %%mm0,%%mm4\n" + "pandn %%mm3,%%mm2\n" + "pandn %%mm5,%%mm4\n" + "movq %%mm2,%%mm3\n" + "movq %%mm4,%%mm5\n" + "pand %%mm6,%%mm2\n" + "pand %%mm6,%%mm4\n" + "pandn %%mm7,%%mm3\n" + "pandn %%mm7,%%mm5\n" + "por %%mm3,%%mm2\n" + "por %%mm5,%%mm4\n" + + /* set *dst */ + "movq %%mm2,%%mm3\n" + "punpckldq %%mm4,%%mm2\n" + "punpckhdq %%mm4,%%mm3\n" + "movq %%mm2,(%3)\n" + "movq %%mm3,8(%3)\n" + "emms\n" + + : "+r" (src0), "+r" (src1), "+r" (src2), "+r" (dst), "+r" (count) + : + : "cc" + ); +#else + __asm { + mov eax, src0; + mov ebx, src1; + mov ecx, src2; + mov edx, dst; + mov esi, count; + + /* first run */ + /* set the current, current_pre, current_next registers */ + movq mm0, qword ptr [ebx]; + movq mm7, qword ptr [ebx]; + movq mm1, qword ptr [ebx + 8]; + psllq mm0, 32; + psllq mm1, 32; + psrlq mm0, 32; + movq mm2, mm7; + movq mm3, mm7; + psllq mm2, 32; + psrlq mm3, 32; + por mm0, mm2; + por mm1, mm3; + + /* current_upper */ + movq mm6, qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2, mm0; + movq mm4, mm1; + movq mm3, mm0; + movq mm5, mm1; + pcmpeqd mm2, mm6; + pcmpeqd mm4, mm6; + pcmpeqd mm3, qword ptr [ecx]; + pcmpeqd mm5, qword ptr [ecx]; + pandn mm3, mm2; + pandn mm5, mm4; + movq mm2, mm0; + movq mm4, mm1; + pcmpeqd mm2, mm1; + pcmpeqd mm4, mm0; + pandn mm2, mm3; + pandn mm4, mm5; + movq mm3, mm2; + movq mm5, mm4; + pand mm2, mm6; + pand mm4, mm6; + pandn mm3, mm7; + pandn mm5, mm7; + por mm2, mm3; + por mm4, mm5; + + /* set *dst */ + movq mm3, mm2; + punpckldq mm2, mm4; + punpckhdq mm3, mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx + 8], mm3; + + /* next */ + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 16; + + /* central runs */ + shr esi, 1; + jz label1; +label0: + + /* set the current, current_pre, current_next registers */ + movq mm0, qword ptr [ebx - 8]; + movq mm7, qword ptr [ebx]; + movq mm1, qword ptr [ebx + 8]; + psrlq mm0, 32; + psllq mm1, 32; + movq mm2, mm7; + movq mm3, mm7; + psllq mm2, 32; + psrlq mm3, 32; + por mm0, mm2; + por mm1, mm3; + + /* current_upper */ + movq mm6, qword ptr[eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2, mm0; + movq mm4, mm1; + movq mm3, mm0; + movq mm5, mm1; + pcmpeqd mm2, mm6; + pcmpeqd mm4, mm6; + pcmpeqd mm3, qword ptr[ecx]; + pcmpeqd mm5, qword ptr[ecx]; + pandn mm3, mm2; + pandn mm5, mm4; + movq mm2, mm0; + movq mm4, mm1; + pcmpeqd mm2, mm1; + pcmpeqd mm4, mm0; + pandn mm2, mm3; + pandn mm4, mm5; + movq mm3, mm2; + movq mm5, mm4; + pand mm2, mm6; + pand mm4, mm6; + pandn mm3, mm7; + pandn mm5, mm7; + por mm2, mm3; + por mm4, mm5; + + /* set *dst */ + movq mm3, mm2; + punpckldq mm2, mm4; + punpckhdq mm3, mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx + 8], mm3; + + /* next */ + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 16; + + dec esi; + jnz label0; +label1: + + /* final run */ + /* set the current, current_pre, current_next registers */ + movq mm1, qword ptr [ebx]; + movq mm7, qword ptr [ebx]; + movq mm0, qword ptr [ebx - 8]; + psrlq mm1, 32; + psrlq mm0, 32; + psllq mm1, 32; + movq mm2, mm7; + movq mm3, mm7; + psllq mm2, 32; + psrlq mm3, 32; + por mm0, mm2; + por mm1, mm3; + + /* current_upper */ + movq mm6, qword ptr [eax]; + + /* compute the upper-left pixel for dst on %%mm2 */ + /* compute the upper-right pixel for dst on %%mm4 */ + movq mm2, mm0; + movq mm4, mm1; + movq mm3, mm0; + movq mm5, mm1; + pcmpeqd mm2, mm6; + pcmpeqd mm4, mm6; + pcmpeqd mm3, qword ptr [ecx]; + pcmpeqd mm5, qword ptr [ecx]; + pandn mm3, mm2; + pandn mm5, mm4; + movq mm2, mm0; + movq mm4, mm1; + pcmpeqd mm2, mm1; + pcmpeqd mm4, mm0; + pandn mm2, mm3; + pandn mm4, mm5; + movq mm3, mm2; + movq mm5, mm4; + pand mm2, mm6; + pand mm4, mm6; + pandn mm3, mm7; + pandn mm5, mm7; + por mm2, mm3; + por mm4, mm5; + + /* set *dst */ + movq mm3, mm2; + punpckldq mm2, mm4; + punpckhdq mm3, mm4; + movq qword ptr [edx], mm2; + movq qword ptr [edx + 8], mm3; + + mov src0, eax; + mov src1, ebx; + mov src2, ecx; + mov dst, edx; + mov count, esi; + + emms; + } +#endif +} + +static void internal_scale2x_16_mmx(u16 *dst0, u16 *dst1, const u16 *src0, const u16 *src1, const u16 *src2, unsigned count) +{ + // assert( count >= 2*4 ); + internal_scale2x_16_mmx_single(dst0, src0, src1, src2, count); + internal_scale2x_16_mmx_single(dst1, src2, src1, src0, count); +} + +static void internal_scale2x_32_mmx(u32 *dst0, u32 *dst1, const u32 *src0, const u32 *src1, const u32 *src2, unsigned count) +{ + // assert( count >= 2*2 ); + internal_scale2x_32_mmx_single(dst0, src0, src1, src2, count); + internal_scale2x_32_mmx_single(dst1, src2, src1, src0, count); +} + +#endif + +void AdMame2x(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *dst0 = (u16 *)dstPtr; + u16 *dst1 = dst0 + (dstPitch >> 1); + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = src0 + (srcPitch >> 1); + u16 *src2 = src1 + (srcPitch >> 1); +#ifdef MMX + if (cpu_mmx) + { + internal_scale2x_16_mmx(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch; + dst1 += dstPitch; + internal_scale2x_16_mmx(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + internal_scale2x_16_mmx(dst0, dst1, src0, src1, src1, width); + } + else + { +#endif + internal_scale2x_16_def(dst0, src0, src0, src1, width); + internal_scale2x_16_def(dst1, src1, src0, src0, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch; + dst1 += dstPitch; + internal_scale2x_16_def(dst0, src0, src1, src2, width); + internal_scale2x_16_def(dst1, src2, src1, src0, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + internal_scale2x_16_def(dst0, src0, src1, src1, width); + internal_scale2x_16_def(dst1, src1, src1, src0, width); +#ifdef MMX +} + +#endif +} + +void AdMame2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dst0 = (u32 *)dstPtr; + u32 *dst1 = dst0 + (dstPitch >> 2); + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = src0 + (srcPitch >> 2); + u32 *src2 = src1 + (srcPitch >> 2); +#ifdef MMX + if (cpu_mmx) + { + internal_scale2x_32_mmx(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + internal_scale2x_32_mmx(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + internal_scale2x_32_mmx(dst0, dst1, src0, src1, src1, width); + } + else + { +#endif + internal_scale2x_32_def(dst0, src0, src0, src1, width); + internal_scale2x_32_def(dst1, src1, src0, src0, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + internal_scale2x_32_def(dst0, src0, src1, src2, width); + internal_scale2x_32_def(dst1, src2, src1, src0, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + internal_scale2x_32_def(dst0, src0, src1, src1, width); + internal_scale2x_32_def(dst1, src1, src1, src0, width); +#ifdef MMX +} + +#endif +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/bilinear.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/bilinear.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,418 @@ +/** Code adapted from Exult source code by Forgotten +** Scale.cc - Trying to scale with bilinear interpolation. +** +** Written: 6/14/00 - JSF +**/ + +#include "../common/System.h" + +static u8 row_cur[3 * 322]; +static u8 row_next[3 * 322]; + +static u8 *rgb_row_cur = row_cur; +static u8 *rgb_row_next = row_next; + +#ifdef RGB +#undef RGB // wingdi.h has it +#endif +#define RGB(r, g, b) \ + ((r) >> 3) << systemRedShift | \ + ((g) >> 3) << systemGreenShift | \ + ((b) >> 3) << systemBlueShift \ + +static void fill_rgb_row_16(u16 *from, int src_width, u8 *row, int width) +{ + u8 *copy_start = row + src_width * 3; + u8 *all_stop = row + width * 3; + while (row < copy_start) + { + u16 color = *from++; + *row++ = ((color >> systemRedShift) & 0x1f) << 3; + *row++ = ((color >> systemGreenShift) & 0x1f) << 3; + *row++ = ((color >> systemBlueShift) & 0x1f) << 3; + } + // any remaining elements to be written to 'row' are a replica of the + // preceding pixel + u8 *p = row - 3; + while (row < all_stop) + { + // we're guaranteed three elements per pixel; could unroll the loop + // further, especially with a Duff's Device, but the gains would be + // probably limited (judging by profiler output) + *row++ = *p++; + *row++ = *p++; + *row++ = *p++; + } +} + +static void fill_rgb_row_32(u32 *from, int src_width, u8 *row, int width) +{ + u8 *copy_start = row + src_width * 3; + u8 *all_stop = row + width * 3; + while (row < copy_start) + { + u32 color = *from++; + *row++ = ((color >> systemRedShift) & 0x1f) << 3; + *row++ = ((color >> systemGreenShift) & 0x1f) << 3; + *row++ = ((color >> systemBlueShift) & 0x1f) << 3; + } + // any remaining elements to be written to 'row' are a replica of the + // preceding pixel + u8 *p = row - 3; + while (row < all_stop) + { + // we're guaranteed three elements per pixel; could unroll the loop + // further, especially with a Duff's Device, but the gains would be + // probably limited (judging by profiler output) + *row++ = *p++; + *row++ = *p++; + *row++ = *p++; + } +} + +void Bilinear(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *to = (u16 *)dstPtr; + u16 *to_odd = (u16 *)(dstPtr + dstPitch); + + int from_width = width; + u16 *from = (u16 *)srcPtr; + fill_rgb_row_16(from, from_width, rgb_row_cur, width + 1); + + for (int y = 0; y < height; y++) + { + u16 *from_orig = from; + u16 *to_orig = to; + + if (y + 1 < height) + fill_rgb_row_16(from + width + 2, from_width, rgb_row_next, + width + 1); + else + fill_rgb_row_16(from, from_width, rgb_row_next, width + 1); + + // every pixel in the src region, is extended to 4 pixels in the + // destination, arranged in a square 'quad'; if the current src + // pixel is 'a', then in what follows 'b' is the src pixel to the + // right, 'c' is the src pixel below, and 'd' is the src pixel to + // the right and down + u8 *cur_row = rgb_row_cur; + u8 *next_row = rgb_row_next; + u8 *ar = cur_row++; + u8 *ag = cur_row++; + u8 *ab = cur_row++; + u8 *cr = next_row++; + u8 *cg = next_row++; + u8 *cb = next_row++; + for (int x = 0; x < width; x++) + { + u8 *br = cur_row++; + u8 *bg = cur_row++; + u8 *bb = cur_row++; + u8 *dr = next_row++; + u8 *dg = next_row++; + u8 *db = next_row++; + + // upper left pixel in quad: just copy it in + *to++ = RGB(*ar, *ag, *ab); + + // upper right + *to++ = RGB((*ar + *br) >> 1, (*ag + *bg) >> 1, (*ab + *bb) >> 1); + + // lower left + *to_odd++ = RGB((*ar + *cr) >> 1, (*ag + *cg) >> 1, (*ab + *cb) >> 1); + + // lower right + *to_odd++ = RGB((*ar + *br + *cr + *dr) >> 2, + (*ag + *bg + *cg + *dg) >> 2, + (*ab + *bb + *cb + *db) >> 2); + + // 'b' becomes 'a', 'd' becomes 'c' + ar = br; + ag = bg; + ab = bb; + cr = dr; + cg = dg; + cb = db; + } + + // the "next" rgb row becomes the current; the old current rgb row is + // recycled and serves as the new "next" row + u8 *temp; + temp = rgb_row_cur; + rgb_row_cur = rgb_row_next; + rgb_row_next = temp; + + // update the pointers for start of next pair of lines + from = (u16 *)((u8 *)from_orig + srcPitch); + to = (u16 *)((u8 *)to_orig + (dstPitch << 1)); + to_odd = (u16 *)((u8 *)to + dstPitch); + } +} + +void BilinearPlus(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *to = (u16 *)dstPtr; + u16 *to_odd = (u16 *)(dstPtr + dstPitch); + + int from_width = width; + u16 *from = (u16 *)srcPtr; + fill_rgb_row_16(from, from_width, rgb_row_cur, width + 1); + + for (int y = 0; y < height; y++) + { + u16 *from_orig = from; + u16 *to_orig = to; + + if (y + 1 < height) + fill_rgb_row_16(from + width + 2, from_width, rgb_row_next, + width + 1); + else + fill_rgb_row_16(from, from_width, rgb_row_next, width + 1); + + // every pixel in the src region, is extended to 4 pixels in the + // destination, arranged in a square 'quad'; if the current src + // pixel is 'a', then in what follows 'b' is the src pixel to the + // right, 'c' is the src pixel below, and 'd' is the src pixel to + // the right and down + u8 *cur_row = rgb_row_cur; + u8 *next_row = rgb_row_next; + u8 *ar = cur_row++; + u8 *ag = cur_row++; + u8 *ab = cur_row++; + u8 *cr = next_row++; + u8 *cg = next_row++; + u8 *cb = next_row++; + for (int x = 0; x < width; x++) + { + u8 *br = cur_row++; + u8 *bg = cur_row++; + u8 *bb = cur_row++; + u8 *dr = next_row++; + u8 *dg = next_row++; + u8 *db = next_row++; + + // upper left pixel in quad: just copy it in + //*to++ = manip.rgb(*ar, *ag, *ab); +#ifdef USE_ORIGINAL_BILINEAR_PLUS + *to++ = RGB( + (((*ar) << 2) + ((*ar)) + (*cr + *br + *br)) >> 3, + (((*ag) << 2) + ((*ag)) + (*cg + *bg + *bg)) >> 3, + (((*ab) << 2) + ((*ab)) + (*cb + *bb + *bb)) >> 3); +#else + *to++ = RGB( + (((*ar) << 3) + ((*ar) << 1) + (*cr + *br + *br + *cr)) >> 4, + (((*ag) << 3) + ((*ag) << 1) + (*cg + *bg + *bg + *cg)) >> 4, + (((*ab) << 3) + ((*ab) << 1) + (*cb + *bb + *bb + *cb)) >> 4); +#endif + + // upper right + *to++ = RGB((*ar + *br) >> 1, (*ag + *bg) >> 1, (*ab + *bb) >> 1); + + // lower left + *to_odd++ = RGB((*ar + *cr) >> 1, (*ag + *cg) >> 1, (*ab + *cb) >> 1); + + // lower right + *to_odd++ = RGB((*ar + *br + *cr + *dr) >> 2, + (*ag + *bg + *cg + *dg) >> 2, + (*ab + *bb + *cb + *db) >> 2); + + // 'b' becomes 'a', 'd' becomes 'c' + ar = br; + ag = bg; + ab = bb; + cr = dr; + cg = dg; + cb = db; + } + + // the "next" rgb row becomes the current; the old current rgb row is + // recycled and serves as the new "next" row + u8 *temp; + temp = rgb_row_cur; + rgb_row_cur = rgb_row_next; + rgb_row_next = temp; + + // update the pointers for start of next pair of lines + from = (u16 *)((u8 *)from_orig + srcPitch); + to = (u16 *)((u8 *)to_orig + (dstPitch << 1)); + to_odd = (u16 *)((u8 *)to + dstPitch); + } +} + +void Bilinear32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *to = (u32 *)dstPtr; + u32 *to_odd = (u32 *)(dstPtr + dstPitch); + + int from_width = width; + if (width + 1 < from_width) + from_width = width + 1; + u32 *from = (u32 *)srcPtr; + fill_rgb_row_32(from, from_width, rgb_row_cur, width + 1); + + for (int y = 0; y < height; y++) + { + u32 *from_orig = from; + u32 *to_orig = to; + + if (y + 1 < height) + fill_rgb_row_32(from + width + 1, from_width, rgb_row_next, + width + 1); + else + fill_rgb_row_32(from, from_width, rgb_row_next, width + 1); + + // every pixel in the src region, is extended to 4 pixels in the + // destination, arranged in a square 'quad'; if the current src + // pixel is 'a', then in what follows 'b' is the src pixel to the + // right, 'c' is the src pixel below, and 'd' is the src pixel to + // the right and down + u8 *cur_row = rgb_row_cur; + u8 *next_row = rgb_row_next; + u8 *ar = cur_row++; + u8 *ag = cur_row++; + u8 *ab = cur_row++; + u8 *cr = next_row++; + u8 *cg = next_row++; + u8 *cb = next_row++; + for (int x = 0; x < width; x++) + { + u8 *br = cur_row++; + u8 *bg = cur_row++; + u8 *bb = cur_row++; + u8 *dr = next_row++; + u8 *dg = next_row++; + u8 *db = next_row++; + + // upper left pixel in quad: just copy it in + *to++ = RGB(*ar, *ag, *ab); + + // upper right + *to++ = RGB((*ar + *br) >> 1, (*ag + *bg) >> 1, (*ab + *bb) >> 1); + + // lower left + *to_odd++ = RGB((*ar + *cr) >> 1, (*ag + *cg) >> 1, (*ab + *cb) >> 1); + + // lower right + *to_odd++ = RGB((*ar + *br + *cr + *dr) >> 2, + (*ag + *bg + *cg + *dg) >> 2, + (*ab + *bb + *cb + *db) >> 2); + + // 'b' becomes 'a', 'd' becomes 'c' + ar = br; + ag = bg; + ab = bb; + cr = dr; + cg = dg; + cb = db; + } + + // the "next" rgb row becomes the current; the old current rgb row is + // recycled and serves as the new "next" row + u8 *temp; + temp = rgb_row_cur; + rgb_row_cur = rgb_row_next; + rgb_row_next = temp; + + // update the pointers for start of next pair of lines + from = (u32 *)((u8 *)from_orig + srcPitch); + to = (u32 *)((u8 *)to_orig + (dstPitch << 1)); + to_odd = (u32 *)((u8 *)to + dstPitch); + } +} + +void BilinearPlus32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *to = (u32 *)dstPtr; + u32 *to_odd = (u32 *)(dstPtr + dstPitch); + + int from_width = width; + if (width + 1 < from_width) + from_width = width + 1; + u32 *from = (u32 *)srcPtr; + fill_rgb_row_32(from, from_width, rgb_row_cur, width + 1); + + for (int y = 0; y < height; y++) + { + u32 *from_orig = from; + u32 *to_orig = to; + + if (y + 1 < height) + fill_rgb_row_32(from + width + 1, from_width, rgb_row_next, + width + 1); + else + fill_rgb_row_32(from, from_width, rgb_row_next, width + 1); + + // every pixel in the src region, is extended to 4 pixels in the + // destination, arranged in a square 'quad'; if the current src + // pixel is 'a', then in what follows 'b' is the src pixel to the + // right, 'c' is the src pixel below, and 'd' is the src pixel to + // the right and down + u8 *cur_row = rgb_row_cur; + u8 *next_row = rgb_row_next; + u8 *ar = cur_row++; + u8 *ag = cur_row++; + u8 *ab = cur_row++; + u8 *cr = next_row++; + u8 *cg = next_row++; + u8 *cb = next_row++; + for (int x = 0; x < width; x++) + { + u8 *br = cur_row++; + u8 *bg = cur_row++; + u8 *bb = cur_row++; + u8 *dr = next_row++; + u8 *dg = next_row++; + u8 *db = next_row++; + + // upper left pixel in quad: just copy it in + //*to++ = manip.rgb(*ar, *ag, *ab); +#ifdef USE_ORIGINAL_BILINEAR_PLUS + *to++ = RGB( + (((*ar) << 2) + ((*ar)) + (*cr + *br + *br)) >> 3, + (((*ag) << 2) + ((*ag)) + (*cg + *bg + *bg)) >> 3, + (((*ab) << 2) + ((*ab)) + (*cb + *bb + *bb)) >> 3); +#else + *to++ = RGB( + (((*ar) << 3) + ((*ar) << 1) + (*cr + *br + *br + *cr)) >> 4, + (((*ag) << 3) + ((*ag) << 1) + (*cg + *bg + *bg + *cg)) >> 4, + (((*ab) << 3) + ((*ab) << 1) + (*cb + *bb + *bb + *cb)) >> 4); +#endif + + // upper right + *to++ = RGB((*ar + *br) >> 1, (*ag + *bg) >> 1, (*ab + *bb) >> 1); + + // lower left + *to_odd++ = RGB((*ar + *cr) >> 1, (*ag + *cg) >> 1, (*ab + *cb) >> 1); + + // lower right + *to_odd++ = RGB((*ar + *br + *cr + *dr) >> 2, + (*ag + *bg + *cg + *dg) >> 2, + (*ab + *bb + *cb + *db) >> 2); + + // 'b' becomes 'a', 'd' becomes 'c' + ar = br; + ag = bg; + ab = bb; + cr = dr; + cg = dg; + cb = db; + } + + // the "next" rgb row becomes the current; the old current rgb row is + // recycled and serves as the new "next" row + u8 *temp; + temp = rgb_row_cur; + rgb_row_cur = rgb_row_next; + rgb_row_next = temp; + + // update the pointers for start of next pair of lines + from = (u32 *)((u8 *)from_orig + srcPitch); + to = (u32 *)((u8 *)to_orig + (dstPitch << 1)); + to_odd = (u32 *)((u8 *)to + dstPitch); + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/filters.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/filters.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,58 @@ + +#ifndef VBA_FILTERS_H +#define VBA_FILTERS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +extern void Pixelate2x16(u8*, u32, u8*, u8*, u32, int, int); +extern void Pixelate2x32(u8*, u32, u8*, u8*, u32, int, int); +extern void (*Pixelate3x16)(u8*, u32, u8*, u8*, u32, int, int); +extern void (*Pixelate3x32)(u8*, u32, u8*, u8*, u32, int, int); +extern void (*Pixelate4x16)(u8*, u32, u8*, u8*, u32, int, int); +extern void (*Pixelate4x32)(u8*, u32, u8*, u8*, u32, int, int); +extern void MotionBlur(u8*, u32, u8*, u8*, u32, int, int); +extern void MotionBlur32(u8*, u32, u8*, u8*, u32, int, int); +extern void _2xSaI(u8*, u32, u8*, u8*, u32, int, int); +extern void _2xSaI32(u8*, u32, u8*, u8*, u32, int, int); +extern void Super2xSaI(u8*, u32, u8*, u8*, u32, int, int); +extern void Super2xSaI32(u8*, u32, u8*, u8*, u32, int, int); +extern void SuperEagle(u8*, u32, u8*, u8*, u32, int, int); +extern void SuperEagle32(u8*, u32, u8*, u8*, u32, int, int); +extern void AdMame2x(u8*, u32, u8*, u8*, u32, int, int); +extern void AdMame2x32(u8*, u32, u8*, u8*, u32, int, int); +extern void Simple2x16(u8*, u32, u8*, u8*, u32, int, int); +extern void Simple2x32(u8*, u32, u8*, u8*, u32, int, int); +extern void (*Simple3x16)(u8*, u32, u8*, u8*, u32, int, int); +extern void (*Simple3x32)(u8*, u32, u8*, u8*, u32, int, int); +extern void (*Simple4x16)(u8*, u32, u8*, u8*, u32, int, int); +extern void (*Simple4x32)(u8*, u32, u8*, u8*, u32, int, int); +extern void Bilinear(u8*, u32, u8*, u8*, u32, int, int); +extern void Bilinear32(u8*, u32, u8*, u8*, u32, int, int); +extern void BilinearPlus(u8*, u32, u8*, u8*, u32, int, int); +extern void BilinearPlus32(u8*, u32, u8*, u8*, u32, int, int); +extern void Scanlines(u8*, u32, u8*, u8*, u32, int, int); +extern void Scanlines32(u8*, u32, u8*, u8*, u32, int, int); +extern void ScanlinesTV(u8*, u32, u8*, u8*, u32, int, int); +extern void ScanlinesTV32(u8*, u32, u8*, u8*, u32, int, int); +extern void hq2x(u8*, u32, u8*, u8*, u32, int, int); +extern void hq2x32(u8*, u32, u8*, u8*, u32, int, int); +extern void hq2xS(u8*, u32, u8*, u8*, u32, int, int); +extern void hq2xS32(u8*, u32, u8*, u8*, u32, int, int); +extern void lq2x(u8*, u32, u8*, u8*, u32, int, int); +extern void lq2x32(u8*, u32, u8*, u8*, u32, int, int); +extern void hq3x(u8*, u32, u8*, u8*, u32, int, int); +extern void hq3x32(u8*, u32, u8*, u8*, u32, int, int); +extern void hq3xS(u8*, u32, u8*, u8*, u32, int, int); +extern void hq3xS32(u8*, u32, u8*, u8*, u32, int, int); + +extern void SmartIB(u8*, u32, int, int); +extern void SmartIB32(u8*, u32, int, int); +extern void MotionBlurIB(u8*, u32, int, int); +extern void InterlaceIB(u8*, u32, int, int); +extern void MotionBlurIB32(u8*, u32, int, int); + +extern void InterframeCleanup(); + +#endif // VBA_FILTERS_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/hq2x.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/hq2x.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,966 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2003 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ +#include "../Port.h" +#include "interp.h" + +unsigned interp_mask[2]; +unsigned interp_bits_per_pixel; + +/***************************************************************************/ +/* HQ2x C implementation */ + +/* + * This effect is a rewritten implementation of the hq2x effect made by Maxim Stepin + */ + +static void hq2x_16_def(u16 *dst0, u16 *dst1, const u16 *src0, const u16 *src1, const u16 *src2, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; ++i) + { + unsigned char mask; + + u16 c[9]; + + c[1] = src0[0]; + c[4] = src1[0]; + c[7] = src2[0]; + + if (i > 0) + { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } + else + { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i < count - 1) + { + c[2] = src0[1]; + c[5] = src1[1]; + c[8] = src2[1]; + } + else + { + c[2] = c[1]; + c[5] = c[4]; + c[8] = c[7]; + } + + mask = 0; + + if (interp_16_diff(c[0], c[4])) + mask |= 1 << 0; + if (interp_16_diff(c[1], c[4])) + mask |= 1 << 1; + if (interp_16_diff(c[2], c[4])) + mask |= 1 << 2; + if (interp_16_diff(c[3], c[4])) + mask |= 1 << 3; + if (interp_16_diff(c[5], c[4])) + mask |= 1 << 4; + if (interp_16_diff(c[6], c[4])) + mask |= 1 << 5; + if (interp_16_diff(c[7], c[4])) + mask |= 1 << 6; + if (interp_16_diff(c[8], c[4])) + mask |= 1 << 7; + +#define P0 dst0[0] +#define P1 dst0[1] +#define P2 dst1[0] +#define P3 dst1[1] +#define MUR interp_16_diff(c[1], c[5]) // top-right +#define MDR interp_16_diff(c[5], c[7]) // bottom-right +#define MDL interp_16_diff(c[7], c[3]) // bottom-left +#define MUL interp_16_diff(c[3], c[1]) // top-left +#define IC(p0) c[p0] +#define I11(p0, p1) interp_16_11(c[p0], c[p1]) +#define I211(p0, p1, p2) interp_16_211(c[p0], c[p1], c[p2]) +#define I31(p0, p1) interp_16_31(c[p0], c[p1]) +#define I332(p0, p1, p2) interp_16_332(c[p0], c[p1], c[p2]) +#define I431(p0, p1, p2) interp_16_431(c[p0], c[p1], c[p2]) +#define I521(p0, p1, p2) interp_16_521(c[p0], c[p1], c[p2]) +#define I53(p0, p1) interp_16_53(c[p0], c[p1]) +#define I611(p0, p1, p2) interp_16_611(c[p0], c[p1], c[p2]) +#define I71(p0, p1) interp_16_71(c[p0], c[p1]) +#define I772(p0, p1, p2) interp_16_772(c[p0], c[p1], c[p2]) +#define I97(p0, p1) interp_16_97(c[p0], c[p1]) +#define I1411(p0, p1, p2) interp_16_1411(c[p0], c[p1], c[p2]) +#define I151(p0, p1) interp_16_151(c[p0], c[p1]) + + switch (mask) + { +#include "hq2x.h" + } + +#undef P0 +#undef P1 +#undef P2 +#undef P3 +#undef MUR +#undef MDR +#undef MDL +#undef MUL +#undef IC +#undef I11 +#undef I211 +#undef I31 +#undef I332 +#undef I431 +#undef I521 +#undef I53 +#undef I611 +#undef I71 +#undef I772 +#undef I97 +#undef I1411 +#undef I151 + + src0 += 1; + src1 += 1; + src2 += 1; + dst0 += 2; + dst1 += 2; + } +} + +static void hq2x_32_def(u32 *dst0, u32 *dst1, const u32 *src0, const u32 *src1, const u32 *src2, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; ++i) + { + unsigned char mask; + + u32 c[9]; + + c[1] = src0[0]; + c[4] = src1[0]; + c[7] = src2[0]; + + if (i > 0) + { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } + else + { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i < count - 1) + { + c[2] = src0[1]; + c[5] = src1[1]; + c[8] = src2[1]; + } + else + { + c[2] = c[1]; + c[5] = c[4]; + c[8] = c[7]; + } + + mask = 0; + + if (interp_32_diff(c[0], c[4])) + mask |= 1 << 0; + if (interp_32_diff(c[1], c[4])) + mask |= 1 << 1; + if (interp_32_diff(c[2], c[4])) + mask |= 1 << 2; + if (interp_32_diff(c[3], c[4])) + mask |= 1 << 3; + if (interp_32_diff(c[5], c[4])) + mask |= 1 << 4; + if (interp_32_diff(c[6], c[4])) + mask |= 1 << 5; + if (interp_32_diff(c[7], c[4])) + mask |= 1 << 6; + if (interp_32_diff(c[8], c[4])) + mask |= 1 << 7; + +#define P0 dst0[0] +#define P1 dst0[1] +#define P2 dst1[0] +#define P3 dst1[1] +#define MUR interp_32_diff(c[1], c[5]) // top-right +#define MDR interp_32_diff(c[5], c[7]) // bottom-right +#define MDL interp_32_diff(c[7], c[3]) // bottom-left +#define MUL interp_32_diff(c[3], c[1]) // top-left +#define IC(p0) c[p0] +#define I11(p0, p1) interp_32_11(c[p0], c[p1]) +#define I211(p0, p1, p2) interp_32_211(c[p0], c[p1], c[p2]) +#define I31(p0, p1) interp_32_31(c[p0], c[p1]) +#define I332(p0, p1, p2) interp_32_332(c[p0], c[p1], c[p2]) +#define I431(p0, p1, p2) interp_32_431(c[p0], c[p1], c[p2]) +#define I521(p0, p1, p2) interp_32_521(c[p0], c[p1], c[p2]) +#define I53(p0, p1) interp_32_53(c[p0], c[p1]) +#define I611(p0, p1, p2) interp_32_611(c[p0], c[p1], c[p2]) +#define I71(p0, p1) interp_32_71(c[p0], c[p1]) +#define I772(p0, p1, p2) interp_32_772(c[p0], c[p1], c[p2]) +#define I97(p0, p1) interp_32_97(c[p0], c[p1]) +#define I1411(p0, p1, p2) interp_32_1411(c[p0], c[p1], c[p2]) +#define I151(p0, p1) interp_32_151(c[p0], c[p1]) + + switch (mask) + { +#include "hq2x.h" + } + +#undef P0 +#undef P1 +#undef P2 +#undef P3 +#undef MUR +#undef MDR +#undef MDL +#undef MUL +#undef IC +#undef I11 +#undef I211 +#undef I31 +#undef I332 +#undef I431 +#undef I521 +#undef I53 +#undef I611 +#undef I71 +#undef I772 +#undef I97 +#undef I1411 +#undef I151 + + src0 += 1; + src1 += 1; + src2 += 1; + dst0 += 2; + dst1 += 2; + } +} + +/***************************************************************************/ +/* HQ2xS C implementation */ + +/* + * This effect is derived from the hq2x effect made by Maxim Stepin + */ + +static void hq2xS_16_def(u16 *dst0, u16 *dst1, const u16 *src0, const u16 *src1, const u16 *src2, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; ++i) + { + unsigned char mask; + + u16 c[9]; + + c[1] = src0[0]; + c[4] = src1[0]; + c[7] = src2[0]; + + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + + c[2] = src0[1]; + c[5] = src1[1]; + c[8] = src2[1]; + + mask = 0; + + // hq2xS dynamic edge detection: + // simply comparing the center color against its surroundings will give bad results in many cases, + // so, instead, compare the center color relative to the max difference in brightness of this 3x3 block + int brightArray[9]; + int maxBright = 0, minBright = 999999; + for (int j = 0; j < 9; j++) + { + int r, g, b; + if (interp_bits_per_pixel == 16) + { + b = (int)((c[j] & 0x1F)) << 3; + g = (int)((c[j] & 0x7E0)) >> 3; + r = (int)((c[j] & 0xF800)) >> 8; + } + else + { + b = (int)((c[j] & 0x1F)) << 3; + g = (int)((c[j] & 0x3E0)) >> 2; + r = (int)((c[j] & 0x7C00)) >> 7; + } + const int bright = r + r + r + g + g + g + b + b; + if (bright > maxBright) maxBright = bright; + if (bright < minBright) minBright = bright; + + brightArray[j] = bright; + } + int diffBright = ((maxBright - minBright) * 7) >> 4; + if (diffBright > 7) + { + #define ABS(x) ((x) < 0 ? -(x) : (x)) + + const int centerBright = brightArray[4]; + if (ABS(brightArray[0] - centerBright) > diffBright) + mask |= 1 << 0; + if (ABS(brightArray[1] - centerBright) > diffBright) + mask |= 1 << 1; + if (ABS(brightArray[2] - centerBright) > diffBright) + mask |= 1 << 2; + if (ABS(brightArray[3] - centerBright) > diffBright) + mask |= 1 << 3; + if (ABS(brightArray[5] - centerBright) > diffBright) + mask |= 1 << 4; + if (ABS(brightArray[6] - centerBright) > diffBright) + mask |= 1 << 5; + if (ABS(brightArray[7] - centerBright) > diffBright) + mask |= 1 << 6; + if (ABS(brightArray[8] - centerBright) > diffBright) + mask |= 1 << 7; + } + +#define P0 dst0[0] +#define P1 dst0[1] +#define P2 dst1[0] +#define P3 dst1[1] +#define MUR false //(ABS(brightArray[1] - brightArray[5]) > diffBright) // top-right +#define MDR false //(ABS(brightArray[5] - brightArray[7]) > diffBright) // bottom-right +#define MDL false //(ABS(brightArray[7] - brightArray[3]) > diffBright) // bottom-left +#define MUL false //(ABS(brightArray[3] - brightArray[1]) > diffBright) // top-left +#define IC(p0) c[p0] +#define I11(p0, p1) interp_16_11(c[p0], c[p1]) +#define I211(p0, p1, p2) interp_16_211(c[p0], c[p1], c[p2]) +#define I31(p0, p1) interp_16_31(c[p0], c[p1]) +#define I332(p0, p1, p2) interp_16_332(c[p0], c[p1], c[p2]) +#define I431(p0, p1, p2) interp_16_431(c[p0], c[p1], c[p2]) +#define I521(p0, p1, p2) interp_16_521(c[p0], c[p1], c[p2]) +#define I53(p0, p1) interp_16_53(c[p0], c[p1]) +#define I611(p0, p1, p2) interp_16_611(c[p0], c[p1], c[p2]) +#define I71(p0, p1) interp_16_71(c[p0], c[p1]) +#define I772(p0, p1, p2) interp_16_772(c[p0], c[p1], c[p2]) +#define I97(p0, p1) interp_16_97(c[p0], c[p1]) +#define I1411(p0, p1, p2) interp_16_1411(c[p0], c[p1], c[p2]) +#define I151(p0, p1) interp_16_151(c[p0], c[p1]) + + switch (mask) + { +#include "hq2x.h" + } + +#undef P0 +#undef P1 +#undef P2 +#undef P3 +#undef MUR +#undef MDR +#undef MDL +#undef MUL +#undef IC +#undef I11 +#undef I211 +#undef I31 +#undef I332 +#undef I431 +#undef I521 +#undef I53 +#undef I611 +#undef I71 +#undef I772 +#undef I97 +#undef I1411 +#undef I151 + + src0 += 1; + src1 += 1; + src2 += 1; + dst0 += 2; + dst1 += 2; + } +} + +static void hq2xS_32_def(u32 *dst0, u32 *dst1, const u32 *src0, const u32 *src1, const u32 *src2, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; ++i) + { + unsigned char mask; + + u32 c[9]; + + c[1] = src0[0]; + c[4] = src1[0]; + c[7] = src2[0]; + + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + + c[2] = src0[1]; + c[5] = src1[1]; + c[8] = src2[1]; + + mask = 0; + + // hq2xS dynamic edge detection: + // simply comparing the center color against its surroundings will give bad results in many cases, + // so, instead, compare the center color relative to the max difference in brightness of this 3x3 block + int brightArray[9]; + int maxBright = 0, minBright = 999999; + for (int j = 0; j < 9; j++) + { + const int b = (int)((c[j] & 0xF8)); + const int g = (int)((c[j] & 0xF800)) >> 8; + const int r = (int)((c[j] & 0xF80000)) >> 16; + const int bright = r + r + r + g + g + g + b + b; + if (bright > maxBright) maxBright = bright; + if (bright < minBright) minBright = bright; + + brightArray[j] = bright; + } + int diffBright = ((maxBright - minBright) * 7) >> 4; + if (diffBright > 7) + { + #define ABS(x) ((x) < 0 ? -(x) : (x)) + + const int centerBright = brightArray[4]; + if (ABS(brightArray[0] - centerBright) > diffBright) + mask |= 1 << 0; + if (ABS(brightArray[1] - centerBright) > diffBright) + mask |= 1 << 1; + if (ABS(brightArray[2] - centerBright) > diffBright) + mask |= 1 << 2; + if (ABS(brightArray[3] - centerBright) > diffBright) + mask |= 1 << 3; + if (ABS(brightArray[5] - centerBright) > diffBright) + mask |= 1 << 4; + if (ABS(brightArray[6] - centerBright) > diffBright) + mask |= 1 << 5; + if (ABS(brightArray[7] - centerBright) > diffBright) + mask |= 1 << 6; + if (ABS(brightArray[8] - centerBright) > diffBright) + mask |= 1 << 7; + } + +#define P0 dst0[0] +#define P1 dst0[1] +#define P2 dst1[0] +#define P3 dst1[1] +#define MUR false //(ABS(brightArray[1] - brightArray[5]) > diffBright) // top-right +#define MDR false //(ABS(brightArray[5] - brightArray[7]) > diffBright) // bottom-right +#define MDL false //(ABS(brightArray[7] - brightArray[3]) > diffBright) // bottom-left +#define MUL false //(ABS(brightArray[3] - brightArray[1]) > diffBright) // top-left +#define IC(p0) c[p0] +#define I11(p0, p1) interp_32_11(c[p0], c[p1]) +#define I211(p0, p1, p2) interp_32_211(c[p0], c[p1], c[p2]) +#define I31(p0, p1) interp_32_31(c[p0], c[p1]) +#define I332(p0, p1, p2) interp_32_332(c[p0], c[p1], c[p2]) +#define I431(p0, p1, p2) interp_32_431(c[p0], c[p1], c[p2]) +#define I521(p0, p1, p2) interp_32_521(c[p0], c[p1], c[p2]) +#define I53(p0, p1) interp_32_53(c[p0], c[p1]) +#define I611(p0, p1, p2) interp_32_611(c[p0], c[p1], c[p2]) +#define I71(p0, p1) interp_32_71(c[p0], c[p1]) +#define I772(p0, p1, p2) interp_32_772(c[p0], c[p1], c[p2]) +#define I97(p0, p1) interp_32_97(c[p0], c[p1]) +#define I1411(p0, p1, p2) interp_32_1411(c[p0], c[p1], c[p2]) +#define I151(p0, p1) interp_32_151(c[p0], c[p1]) + + switch (mask) + { +#include "hq2x.h" + } + +#undef P0 +#undef P1 +#undef P2 +#undef P3 +#undef MUR +#undef MDR +#undef MDL +#undef MUL +#undef IC +#undef I11 +#undef I211 +#undef I31 +#undef I332 +#undef I431 +#undef I521 +#undef I53 +#undef I611 +#undef I71 +#undef I772 +#undef I97 +#undef I1411 +#undef I151 + + src0 += 1; + src1 += 1; + src2 += 1; + dst0 += 2; + dst1 += 2; + } +} + +/***************************************************************************/ +/* LQ2x C implementation */ + +/* + * This effect is derived from the hq2x effect made by Maxim Stepin + */ + +static void lq2x_16_def(u16 *dst0, u16 *dst1, const u16 *src0, const u16 *src1, const u16 *src2, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; ++i) + { + unsigned char mask; + + u16 c[9]; + + c[1] = src0[0]; + c[4] = src1[0]; + c[7] = src2[0]; + + if (i > 0) + { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } + else + { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i < count - 1) + { + c[2] = src0[1]; + c[5] = src1[1]; + c[8] = src2[1]; + } + else + { + c[2] = c[1]; + c[5] = c[4]; + c[8] = c[7]; + } + + mask = 0; + + if (c[0] != c[4]) + mask |= 1 << 0; + if (c[1] != c[4]) + mask |= 1 << 1; + if (c[2] != c[4]) + mask |= 1 << 2; + if (c[3] != c[4]) + mask |= 1 << 3; + if (c[5] != c[4]) + mask |= 1 << 4; + if (c[6] != c[4]) + mask |= 1 << 5; + if (c[7] != c[4]) + mask |= 1 << 6; + if (c[8] != c[4]) + mask |= 1 << 7; + +#define P0 dst0[0] +#define P1 dst0[1] +#define P2 dst1[0] +#define P3 dst1[1] +#define MUR (c[1] != c[5]) +#define MDR (c[5] != c[7]) +#define MDL (c[7] != c[3]) +#define MUL (c[3] != c[1]) +#define IC(p0) c[p0] +#define I11(p0, p1) interp_16_11(c[p0], c[p1]) +#define I211(p0, p1, p2) interp_16_211(c[p0], c[p1], c[p2]) +#define I31(p0, p1) interp_16_31(c[p0], c[p1]) +#define I332(p0, p1, p2) interp_16_332(c[p0], c[p1], c[p2]) +#define I431(p0, p1, p2) interp_16_431(c[p0], c[p1], c[p2]) +#define I521(p0, p1, p2) interp_16_521(c[p0], c[p1], c[p2]) +#define I53(p0, p1) interp_16_53(c[p0], c[p1]) +#define I611(p0, p1, p2) interp_16_611(c[p0], c[p1], c[p2]) +#define I71(p0, p1) interp_16_71(c[p0], c[p1]) +#define I772(p0, p1, p2) interp_16_772(c[p0], c[p1], c[p2]) +#define I97(p0, p1) interp_16_97(c[p0], c[p1]) +#define I1411(p0, p1, p2) interp_16_1411(c[p0], c[p1], c[p2]) +#define I151(p0, p1) interp_16_151(c[p0], c[p1]) + + switch (mask) + { +#include "lq2x.h" + } + +#undef P0 +#undef P1 +#undef P2 +#undef P3 +#undef MUR +#undef MDR +#undef MDL +#undef MUL +#undef IC +#undef I11 +#undef I211 +#undef I31 +#undef I332 +#undef I431 +#undef I521 +#undef I53 +#undef I611 +#undef I71 +#undef I772 +#undef I97 +#undef I1411 +#undef I151 + + src0 += 1; + src1 += 1; + src2 += 1; + dst0 += 2; + dst1 += 2; + } +} + +static void lq2x_32_def(u32 *dst0, u32 *dst1, const u32 *src0, const u32 *src1, const u32 *src2, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; ++i) + { + unsigned char mask; + + u32 c[9]; + + c[1] = src0[0]; + c[4] = src1[0]; + c[7] = src2[0]; + + if (i > 0) + { + c[0] = src0[-1]; + c[3] = src1[-1]; + c[6] = src2[-1]; + } + else + { + c[0] = c[1]; + c[3] = c[4]; + c[6] = c[7]; + } + + if (i < count - 1) + { + c[2] = src0[1]; + c[5] = src1[1]; + c[8] = src2[1]; + } + else + { + c[2] = c[1]; + c[5] = c[4]; + c[8] = c[7]; + } + + mask = 0; + + if (c[0] != c[4]) + mask |= 1 << 0; + if (c[1] != c[4]) + mask |= 1 << 1; + if (c[2] != c[4]) + mask |= 1 << 2; + if (c[3] != c[4]) + mask |= 1 << 3; + if (c[5] != c[4]) + mask |= 1 << 4; + if (c[6] != c[4]) + mask |= 1 << 5; + if (c[7] != c[4]) + mask |= 1 << 6; + if (c[8] != c[4]) + mask |= 1 << 7; + +#define P0 dst0[0] +#define P1 dst0[1] +#define P2 dst1[0] +#define P3 dst1[1] +#define MUR (c[1] != c[5]) +#define MDR (c[5] != c[7]) +#define MDL (c[7] != c[3]) +#define MUL (c[3] != c[1]) +#define IC(p0) c[p0] +#define I11(p0, p1) interp_32_11(c[p0], c[p1]) +#define I211(p0, p1, p2) interp_32_211(c[p0], c[p1], c[p2]) +#define I31(p0, p1) interp_32_31(c[p0], c[p1]) +#define I332(p0, p1, p2) interp_32_332(c[p0], c[p1], c[p2]) +#define I431(p0, p1, p2) interp_32_431(c[p0], c[p1], c[p2]) +#define I521(p0, p1, p2) interp_32_521(c[p0], c[p1], c[p2]) +#define I53(p0, p1) interp_32_53(c[p0], c[p1]) +#define I611(p0, p1, p2) interp_32_611(c[p0], c[p1], c[p2]) +#define I71(p0, p1) interp_32_71(c[p0], c[p1]) +#define I772(p0, p1, p2) interp_32_772(c[p0], c[p1], c[p2]) +#define I97(p0, p1) interp_32_97(c[p0], c[p1]) +#define I1411(p0, p1, p2) interp_32_1411(c[p0], c[p1], c[p2]) +#define I151(p0, p1) interp_32_151(c[p0], c[p1]) + + switch (mask) + { +#include "lq2x.h" + } + +#undef P0 +#undef P1 +#undef P2 +#undef P3 +#undef MUR +#undef MDR +#undef MDL +#undef MUL +#undef IC +#undef I11 +#undef I211 +#undef I31 +#undef I332 +#undef I431 +#undef I521 +#undef I53 +#undef I611 +#undef I71 +#undef I772 +#undef I97 +#undef I1411 +#undef I151 + + src0 += 1; + src1 += 1; + src2 += 1; + dst0 += 2; + dst1 += 2; + } +} + +void hq2x(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *dst0 = (u16 *)dstPtr; + u16 *dst1 = dst0 + (dstPitch >> 1); + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = src0 + (srcPitch >> 1); + u16 *src2 = src1 + (srcPitch >> 1); + + hq2x_16_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch; + dst1 += dstPitch; + hq2x_16_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + hq2x_16_def(dst0, dst1, src0, src1, src1, width); +} + +void hq2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dst0 = (u32 *)dstPtr; + u32 *dst1 = dst0 + (dstPitch >> 2); + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = src0 + (srcPitch >> 2); + u32 *src2 = src1 + (srcPitch >> 2); + hq2x_32_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + hq2x_32_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + hq2x_32_def(dst0, dst1, src0, src1, src1, width); +} + +void hq2xS(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *dst0 = (u16 *)dstPtr; + u16 *dst1 = dst0 + (dstPitch >> 1); + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = src0 + (srcPitch >> 1); + u16 *src2 = src1 + (srcPitch >> 1); + + hq2xS_16_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch; + dst1 += dstPitch; + hq2xS_16_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + hq2xS_16_def(dst0, dst1, src0, src1, src1, width); +} + +void hq2xS32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dst0 = (u32 *)dstPtr; + u32 *dst1 = dst0 + (dstPitch >> 2); + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = src0 + (srcPitch >> 2); + u32 *src2 = src1 + (srcPitch >> 2); + hq2xS_32_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + hq2xS_32_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + hq2xS_32_def(dst0, dst1, src0, src1, src1, width); +} + +void lq2x(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u16 *dst0 = (u16 *)dstPtr; + u16 *dst1 = dst0 + (dstPitch >> 1); + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = src0 + (srcPitch >> 1); + u16 *src2 = src1 + (srcPitch >> 1); + + lq2x_16_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch; + dst1 += dstPitch; + lq2x_16_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 1; + --count; + } + dst0 += dstPitch; + dst1 += dstPitch; + lq2x_16_def(dst0, dst1, src0, src1, src1, width); +} + +void lq2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u32 *dst0 = (u32 *)dstPtr; + u32 *dst1 = dst0 + (dstPitch >> 2); + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = src0 + (srcPitch >> 2); + u32 *src2 = src1 + (srcPitch >> 2); + lq2x_32_def(dst0, dst1, src0, src0, src1, width); + + int count = height; + + count -= 2; + while (count) + { + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + lq2x_32_def(dst0, dst1, src0, src1, src2, width); + src0 = src1; + src1 = src2; + src2 += srcPitch >> 2; + --count; + } + dst0 += dstPitch >> 1; + dst1 += dstPitch >> 1; + lq2x_32_def(dst0, dst1, src0, src1, src1, width); +} + +void hq2x_init(unsigned bits_per_pixel) +{ + interp_set(bits_per_pixel); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/hq2x.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/hq2x.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1824 @@ +case 0 : +case 1 : +case 4 : +case 5 : +case 32 : +case 33 : +case 36 : +case 37 : +case 128 : +case 129 : +case 132 : +case 133 : +case 160 : +case 161 : +case 164 : +case 165 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 2 : +case 34 : +case 130 : +case 162 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 3 : +case 35 : +case 131 : +case 163 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 6 : +case 38 : +case 134 : +case 166 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 7 : +case 39 : +case 135 : +case 167 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I211(4, 3, 7); + P3 = I211(4, 5, 7); +} break; +case 8 : +case 12 : +case 136 : +case 140 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + P2 = I31(4, 6); + P3 = I211(4, 5, 7); +} break; +case 9 : +case 13 : +case 137 : +case 141 : +{ + P0 = I31(4, 1); + P1 = I211(4, 1, 5); + P2 = I31(4, 6); + P3 = I211(4, 5, 7); +} break; +case 10 : +case 138 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 11 : +case 139 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 14 : +case 142 : +{ + P2 = I31(4, 6); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = I31(4, 0); + P1 = I31(4, 5); + } else { + P0 = I332(1, 3, 4); + P1 = I521(4, 1, 5); + } +} break; +case 15 : +case 143 : +{ + P2 = I31(4, 6); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = IC(4); + P1 = I31(4, 5); + } else { + P0 = I332(1, 3, 4); + P1 = I521(4, 1, 5); + } +} break; +case 16 : +case 17 : +case 48 : +case 49 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + P2 = I211(4, 3, 7); + P3 = I31(4, 8); +} break; +case 18 : +case 50 : +{ + P0 = I31(4, 0); + P2 = I211(4, 3, 7); + P3 = I31(4, 8); + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 19 : +case 51 : +{ + P2 = I211(4, 3, 7); + P3 = I31(4, 8); + if (MUR) { + P0 = I31(4, 3); + P1 = I31(4, 2); + } else { + P0 = I521(4, 1, 3); + P1 = I332(1, 5, 4); + } +} break; +case 20 : +case 21 : +case 52 : +case 53 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 1); + P2 = I211(4, 3, 7); + P3 = I31(4, 8); +} break; +case 22 : +case 54 : +{ + P0 = I31(4, 0); + P2 = I211(4, 3, 7); + P3 = I31(4, 8); + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 23 : +case 55 : +{ + P2 = I211(4, 3, 7); + P3 = I31(4, 8); + if (MUR) { + P0 = I31(4, 3); + P1 = IC(4); + } else { + P0 = I521(4, 1, 3); + P1 = I332(1, 5, 4); + } +} break; +case 24 : +case 66 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 25 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 26 : +case 31 : +case 95 : +{ + P2 = I31(4, 6); + P3 = I31(4, 8); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 27 : +case 75 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 8); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 28 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 29 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 30 : +case 86 : +{ + P0 = I31(4, 0); + P2 = I31(4, 6); + P3 = I31(4, 8); + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 40 : +case 44 : +case 168 : +case 172 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + P2 = I31(4, 7); + P3 = I211(4, 5, 7); +} break; +case 41 : +case 45 : +case 169 : +case 173 : +{ + P0 = I31(4, 1); + P1 = I211(4, 1, 5); + P2 = I31(4, 7); + P3 = I211(4, 5, 7); +} break; +case 42 : +case 170 : +{ + P1 = I31(4, 2); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = I31(4, 0); + P2 = I31(4, 7); + } else { + P0 = I332(1, 3, 4); + P2 = I521(4, 3, 7); + } +} break; +case 43 : +case 171 : +{ + P1 = I31(4, 2); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = IC(4); + P2 = I31(4, 7); + } else { + P0 = I332(1, 3, 4); + P2 = I521(4, 3, 7); + } +} break; +case 46 : +case 174 : +{ + P1 = I31(4, 5); + P2 = I31(4, 7); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 47 : +case 175 : +{ + P1 = I31(4, 5); + P2 = I31(4, 7); + P3 = I211(4, 5, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 56 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 7); + P3 = I31(4, 8); +} break; +case 57 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 7); + P3 = I31(4, 8); +} break; +case 58 : +{ + P2 = I31(4, 7); + P3 = I31(4, 8); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 59 : +{ + P2 = I31(4, 7); + P3 = I31(4, 8); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 60 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P2 = I31(4, 7); + P3 = I31(4, 8); +} break; +case 61 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + P2 = I31(4, 7); + P3 = I31(4, 8); +} break; +case 62 : +{ + P0 = I31(4, 0); + P2 = I31(4, 7); + P3 = I31(4, 8); + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 63 : +{ + P2 = I31(4, 7); + P3 = I31(4, 8); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 64 : +case 65 : +case 68 : +case 69 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 67 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 70 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 71 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I31(4, 6); + P3 = I31(4, 8); +} break; +case 72 : +case 76 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 73 : +case 77 : +{ + P1 = I211(4, 1, 5); + P3 = I31(4, 8); + if (MDL) { + P0 = I31(4, 1); + P2 = I31(4, 6); + } else { + P0 = I521(4, 3, 1); + P2 = I332(3, 7, 4); + } +} break; +case 74 : +case 107 : +case 123 : +{ + P1 = I31(4, 2); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 78 : +{ + P1 = I31(4, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 79 : +{ + P1 = I31(4, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 80 : +case 81 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 82 : +case 214 : +case 222 : +{ + P0 = I31(4, 0); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 83 : +{ + P0 = I31(4, 3); + P2 = I31(4, 6); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 84 : +case 85 : +{ + P0 = I211(4, 1, 3); + P2 = I31(4, 6); + if (MDR) { + P1 = I31(4, 1); + P3 = I31(4, 8); + } else { + P1 = I521(4, 5, 1); + P3 = I332(5, 7, 4); + } +} break; +case 87 : +{ + P0 = I31(4, 3); + P2 = I31(4, 6); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 88 : +case 248 : +case 250 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 89 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 90 : +{ + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 91 : +{ + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 92 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 93 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 94 : +{ + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 96 : +case 97 : +case 100 : +case 101 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 98 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 99 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 102 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 103 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I31(4, 3); + P3 = I31(4, 8); +} break; +case 104 : +case 108 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 105 : +case 109 : +{ + P1 = I211(4, 1, 5); + P3 = I31(4, 8); + if (MDL) { + P0 = I31(4, 1); + P2 = IC(4); + } else { + P0 = I521(4, 3, 1); + P2 = I332(3, 7, 4); + } +} break; +case 106 : +case 120 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 110 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 111 : +{ + P1 = I31(4, 5); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 112 : +case 113 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + if (MDR) { + P2 = I31(4, 3); + P3 = I31(4, 8); + } else { + P2 = I521(4, 7, 3); + P3 = I332(5, 7, 4); + } +} break; +case 114 : +{ + P0 = I31(4, 0); + P2 = I31(4, 3); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 115 : +{ + P0 = I31(4, 3); + P2 = I31(4, 3); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 116 : +case 117 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 1); + P2 = I31(4, 3); + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 118 : +{ + P0 = I31(4, 0); + P2 = I31(4, 3); + P3 = I31(4, 8); + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 119 : +{ + P2 = I31(4, 3); + P3 = I31(4, 8); + if (MUR) { + P0 = I31(4, 3); + P1 = IC(4); + } else { + P0 = I521(4, 1, 3); + P1 = I332(1, 5, 4); + } +} break; +case 121 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } +} break; +case 122 : +{ + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = I31(4, 8); + } else { + P3 = I611(4, 5, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 124 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } +} break; +case 125 : +{ + P1 = I31(4, 1); + P3 = I31(4, 8); + if (MDL) { + P0 = I31(4, 1); + P2 = IC(4); + } else { + P0 = I521(4, 3, 1); + P2 = I332(3, 7, 4); + } +} break; +case 126 : +{ + P0 = I31(4, 0); + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 127 : +{ + P3 = I31(4, 8); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 144 : +case 145 : +case 176 : +case 177 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + P2 = I211(4, 3, 7); + P3 = I31(4, 7); +} break; +case 146 : +case 178 : +{ + P0 = I31(4, 0); + P2 = I211(4, 3, 7); + if (MUR) { + P1 = I31(4, 2); + P3 = I31(4, 7); + } else { + P1 = I332(1, 5, 4); + P3 = I521(4, 5, 7); + } +} break; +case 147 : +case 179 : +{ + P0 = I31(4, 3); + P2 = I211(4, 3, 7); + P3 = I31(4, 7); + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 148 : +case 149 : +case 180 : +case 181 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 1); + P2 = I211(4, 3, 7); + P3 = I31(4, 7); +} break; +case 150 : +case 182 : +{ + P0 = I31(4, 0); + P2 = I211(4, 3, 7); + if (MUR) { + P1 = IC(4); + P3 = I31(4, 7); + } else { + P1 = I332(1, 5, 4); + P3 = I521(4, 5, 7); + } +} break; +case 151 : +case 183 : +{ + P0 = I31(4, 3); + P2 = I211(4, 3, 7); + P3 = I31(4, 7); + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 152 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 7); +} break; +case 153 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 7); +} break; +case 154 : +{ + P2 = I31(4, 6); + P3 = I31(4, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 155 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 156 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P2 = I31(4, 6); + P3 = I31(4, 7); +} break; +case 157 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + P2 = I31(4, 6); + P3 = I31(4, 7); +} break; +case 158 : +{ + P2 = I31(4, 6); + P3 = I31(4, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 159 : +{ + P2 = I31(4, 6); + P3 = I31(4, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 184 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 7); + P3 = I31(4, 7); +} break; +case 185 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 7); + P3 = I31(4, 7); +} break; +case 186 : +{ + P2 = I31(4, 7); + P3 = I31(4, 7); + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 187 : +{ + P1 = I31(4, 2); + P3 = I31(4, 7); + if (MUL) { + P0 = IC(4); + P2 = I31(4, 7); + } else { + P0 = I332(1, 3, 4); + P2 = I521(4, 3, 7); + } +} break; +case 188 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + P2 = I31(4, 7); + P3 = I31(4, 7); +} break; +case 189 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + P2 = I31(4, 7); + P3 = I31(4, 7); +} break; +case 190 : +{ + P0 = I31(4, 0); + P2 = I31(4, 7); + if (MUR) { + P1 = IC(4); + P3 = I31(4, 7); + } else { + P1 = I332(1, 5, 4); + P3 = I521(4, 5, 7); + } +} break; +case 191 : +{ + P2 = I31(4, 7); + P3 = I31(4, 7); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 192 : +case 193 : +case 196 : +case 197 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 194 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 195 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 198 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 199 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I31(4, 6); + P3 = I31(4, 5); +} break; +case 200 : +case 204 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + if (MDL) { + P2 = I31(4, 6); + P3 = I31(4, 5); + } else { + P2 = I332(3, 7, 4); + P3 = I521(4, 7, 5); + } +} break; +case 201 : +case 205 : +{ + P0 = I31(4, 1); + P1 = I211(4, 1, 5); + P3 = I31(4, 5); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } +} break; +case 202 : +{ + P1 = I31(4, 2); + P3 = I31(4, 5); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 203 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + P3 = I31(4, 5); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 206 : +{ + P1 = I31(4, 5); + P3 = I31(4, 5); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 207 : +{ + P2 = I31(4, 6); + P3 = I31(4, 5); + if (MUL) { + P0 = IC(4); + P1 = I31(4, 5); + } else { + P0 = I332(1, 3, 4); + P1 = I521(4, 1, 5); + } +} break; +case 208 : +case 209 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 210 : +case 216 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 211 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 212 : +case 213 : +{ + P0 = I211(4, 1, 3); + P2 = I31(4, 6); + if (MDR) { + P1 = I31(4, 1); + P3 = IC(4); + } else { + P1 = I521(4, 5, 1); + P3 = I332(5, 7, 4); + } +} break; +case 215 : +{ + P0 = I31(4, 3); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 217 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 218 : +{ + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 219 : +{ + P1 = I31(4, 2); + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 220 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + if (MDL) { + P2 = I31(4, 6); + } else { + P2 = I611(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 221 : +{ + P0 = I31(4, 1); + P2 = I31(4, 6); + if (MDR) { + P1 = I31(4, 1); + P3 = IC(4); + } else { + P1 = I521(4, 5, 1); + P3 = I332(5, 7, 4); + } +} break; +case 223 : +{ + P2 = I31(4, 6); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 224 : +case 225 : +case 228 : +case 229 : +{ + P0 = I211(4, 1, 3); + P1 = I211(4, 1, 5); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 226 : +{ + P0 = I31(4, 0); + P1 = I31(4, 2); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 227 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 230 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 231 : +{ + P0 = I31(4, 3); + P1 = I31(4, 5); + P2 = I31(4, 3); + P3 = I31(4, 5); +} break; +case 232 : +case 236 : +{ + P0 = I31(4, 0); + P1 = I211(4, 1, 5); + if (MDL) { + P2 = IC(4); + P3 = I31(4, 5); + } else { + P2 = I332(3, 7, 4); + P3 = I521(4, 7, 5); + } +} break; +case 233 : +case 237 : +{ + P0 = I31(4, 1); + P1 = I211(4, 1, 5); + P3 = I31(4, 5); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } +} break; +case 234 : +{ + P1 = I31(4, 2); + P3 = I31(4, 5); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = I31(4, 0); + } else { + P0 = I611(4, 1, 3); + } +} break; +case 235 : +{ + P1 = I31(4, 2); + P3 = I31(4, 5); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 238 : +{ + P0 = I31(4, 0); + P1 = I31(4, 5); + if (MDL) { + P2 = IC(4); + P3 = I31(4, 5); + } else { + P2 = I332(3, 7, 4); + P3 = I521(4, 7, 5); + } +} break; +case 239 : +{ + P1 = I31(4, 5); + P3 = I31(4, 5); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 240 : +case 241 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 2); + if (MDR) { + P2 = I31(4, 3); + P3 = IC(4); + } else { + P2 = I521(4, 7, 3); + P3 = I332(5, 7, 4); + } +} break; +case 242 : +{ + P0 = I31(4, 0); + P2 = I31(4, 3); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUR) { + P1 = I31(4, 2); + } else { + P1 = I611(4, 1, 5); + } +} break; +case 243 : +{ + P0 = I31(4, 3); + P1 = I31(4, 2); + if (MDR) { + P2 = I31(4, 3); + P3 = IC(4); + } else { + P2 = I521(4, 7, 3); + P3 = I332(5, 7, 4); + } +} break; +case 244 : +case 245 : +{ + P0 = I211(4, 1, 3); + P1 = I31(4, 1); + P2 = I31(4, 3); + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } +} break; +case 246 : +{ + P0 = I31(4, 0); + P2 = I31(4, 3); + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 247 : +{ + P0 = I31(4, 3); + P2 = I31(4, 3); + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 249 : +{ + P0 = I31(4, 1); + P1 = I31(4, 2); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } +} break; +case 251 : +{ + P1 = I31(4, 2); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 252 : +{ + P0 = I31(4, 0); + P1 = I31(4, 1); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } +} break; +case 253 : +{ + P0 = I31(4, 1); + P1 = I31(4, 1); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } +} break; +case 254 : +{ + P0 = I31(4, 0); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 255 : +{ + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/hq3x32.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/hq3x32.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,445 @@ +#include "../Port.h" +#include "hq_shared32.h" +#include "interp.h" + +#define SIZE_PIXEL 2 // 16bit = 2 bytes +#define PIXELTYPE unsigned short +#define Interp1 Interp1_16 +#define Interp2 Interp2_16 +#define Interp3 Interp3_16 +#define Interp4 Interp4_16 +#define Interp5 Interp5_16 + +void hq3x(unsigned char *pIn, unsigned int srcPitch, + unsigned char *, + unsigned char *pOut, unsigned int dstPitch, + int Xres, int Yres) +{ + int i, j; + unsigned int line; + PIXELTYPE c[10]; + + // +----+----+----+ + // | | | | + // | c1 | c2 | c3 | + // +----+----+----+ + // | | | | + // | c4 | c5 | c6 | + // +----+----+----+ + // | | | | + // | c7 | c8 | c9 | + // +----+----+----+ + + for (j = 0; j < Yres; j++) + { + if ((j > 0) || (j < Yres - 1)) + line = srcPitch; + else + line = 0; + + for (i = 0; i < Xres; i++) + { + c[2] = *((PIXELTYPE *)(pIn - line)); + c[5] = *((PIXELTYPE *)(pIn)); + c[8] = *((PIXELTYPE *)(pIn + line)); + + if (i > 0) + { + c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL)); + c[4] = *((PIXELTYPE *)(pIn - SIZE_PIXEL)); + c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL)); + } + else + { + c[1] = c[2]; + c[4] = c[5]; + c[7] = c[8]; + } + + if (i < Xres - 1) + { + c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL)); + c[6] = *((PIXELTYPE *)(pIn + SIZE_PIXEL)); + c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL)); + } + else + { + c[3] = c[2]; + c[6] = c[5]; + c[9] = c[8]; + } + + int pattern = 0; + + if (interp_16_diff(c[1], c[5])) + pattern |= 1 << 0; + if (interp_16_diff(c[2], c[5])) + pattern |= 1 << 1; + if (interp_16_diff(c[3], c[5])) + pattern |= 1 << 2; + if (interp_16_diff(c[4], c[5])) + pattern |= 1 << 3; + if (interp_16_diff(c[6], c[5])) + pattern |= 1 << 4; + if (interp_16_diff(c[7], c[5])) + pattern |= 1 << 5; + if (interp_16_diff(c[8], c[5])) + pattern |= 1 << 6; + if (interp_16_diff(c[9], c[5])) + pattern |= 1 << 7; + +#define Diff interp_16_diff +#include "hq3x32.h" +#undef Diff + pIn += SIZE_PIXEL; + pOut += 3 << 1; + } + pIn += srcPitch - (Xres << 1); + pOut += dstPitch - (3 * Xres << 1); + pOut += dstPitch << 1; + // pIn+=SIZE_PIXEL; + // pOut+=3*SIZE_PIXEL; + //} + //pIn+=srcPitch-(4*Xres); + //pOut+=dstPitch-(3*Xres*SIZE_PIXEL); + //pOut+=2*dstPitch; + } +} + +void hq3xS(unsigned char *pIn, unsigned int srcPitch, + unsigned char *, + unsigned char *pOut, unsigned int dstPitch, + int Xres, int Yres) +{ + int i, j; + PIXELTYPE c[10]; + + // +----+----+----+ + // | | | | + // | c1 | c2 | c3 | + // +----+----+----+ + // | | | | + // | c4 | c5 | c6 | + // +----+----+----+ + // | | | | + // | c7 | c8 | c9 | + // +----+----+----+ + + for (j = 0; j < Yres; j++) + { + for (i = 0; i < Xres; i++) + { + c[2] = *((PIXELTYPE *)(pIn - srcPitch)); + c[5] = *((PIXELTYPE *)(pIn)); + c[8] = *((PIXELTYPE *)(pIn + srcPitch)); + + c[1] = *((PIXELTYPE *)(pIn - srcPitch - SIZE_PIXEL)); + c[4] = *((PIXELTYPE *)(pIn - SIZE_PIXEL)); + c[7] = *((PIXELTYPE *)(pIn + srcPitch - SIZE_PIXEL)); + + c[3] = *((PIXELTYPE *)(pIn - srcPitch + SIZE_PIXEL)); + c[6] = *((PIXELTYPE *)(pIn + SIZE_PIXEL)); + c[9] = *((PIXELTYPE *)(pIn + srcPitch + SIZE_PIXEL)); + + int pattern = 0; + + // hq3xS dynamic edge detection: + // simply comparing the center color against its surroundings will give bad results in many cases, + // so, instead, compare the center color relative to the max difference in brightness of this 3x3 block + int brightArray[10]; + int maxBright = 0, minBright = 999999; + for (int j = 1; j < 10; j++) + { + int r, g, b; + if (interp_bits_per_pixel == 16) + { + b = (int)((c[j] & 0x1F)) << 3; + g = (int)((c[j] & 0x7E0)) >> 3; + r = (int)((c[j] & 0xF800)) >> 8; + } + else + { + b = (int)((c[j] & 0x1F)) << 3; + g = (int)((c[j] & 0x3E0)) >> 2; + r = (int)((c[j] & 0x7C00)) >> 7; + } + const int bright = r + r + r + g + g + g + b + b; + if (bright > maxBright) maxBright = bright; + if (bright < minBright) minBright = bright; + + brightArray[j] = bright; + } + const int diffBright = ((maxBright - minBright) * 7) >> 4; + if (diffBright > 7) + { + #define ABS(x) ((x) < 0 ? -(x) : (x)) + + const int centerBright = brightArray[5]; + if (ABS(brightArray[1] - centerBright) > diffBright) + pattern |= 1 << 0; + if (ABS(brightArray[2] - centerBright) > diffBright) + pattern |= 1 << 1; + if (ABS(brightArray[3] - centerBright) > diffBright) + pattern |= 1 << 2; + if (ABS(brightArray[4] - centerBright) > diffBright) + pattern |= 1 << 3; + if (ABS(brightArray[6] - centerBright) > diffBright) + pattern |= 1 << 4; + if (ABS(brightArray[7] - centerBright) > diffBright) + pattern |= 1 << 5; + if (ABS(brightArray[8] - centerBright) > diffBright) + pattern |= 1 << 6; + if (ABS(brightArray[9] - centerBright) > diffBright) + pattern |= 1 << 7; + } + +#define Diff(x, y) false //(ABS((x) - (y)) > diffBright) +#undef cget +#define cget(x) brightArray[x] +#include "hq3x32.h" +#undef cget +#undef Diff + pIn += SIZE_PIXEL; + pOut += 3 << 1; + } + pIn += srcPitch - (Xres << 1); + pOut += dstPitch - (3 * Xres << 1); + pOut += dstPitch << 1; + // pIn+=SIZE_PIXEL; + // pOut+=3*SIZE_PIXEL; + //} + //pIn+=srcPitch-(4*Xres); + //pOut+=dstPitch-(3*Xres*SIZE_PIXEL); + //pOut+=2*dstPitch; + } +} + +#undef Interp1 +#undef Interp2 +#undef Interp3 +#undef Interp4 +#undef Interp5 +#undef SIZE_PIXEL +#undef PIXELTYPE +#define SIZE_PIXEL 4 // 32bit = 4 bytes +#define PIXELTYPE unsigned int + +void hq3x32(unsigned char *pIn, unsigned int srcPitch, + unsigned char *, + unsigned char *pOut, unsigned int dstPitch, + int Xres, int Yres) +{ + unsigned int YUV1, YUV2; + int i, j, k; + unsigned int line; + PIXELTYPE c[10]; + + // +----+----+----+ + // | | | | + // | c1 | c2 | c3 | + // +----+----+----+ + // | | | | + // | c4 | c5 | c6 | + // +----+----+----+ + // | | | | + // | c7 | c8 | c9 | + // +----+----+----+ + + for (j = 0; j < Yres; j++) + { + if ((j > 0) && (j < Yres - 1)) + line = srcPitch; + else + line = 0; + + for (i = 0; i < Xres; i++) + { + c[2] = *((PIXELTYPE *)(pIn - line)); + c[5] = *((PIXELTYPE *)(pIn)); + c[8] = *((PIXELTYPE *)(pIn + line)); + + if (i > 0) + { + c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL)); + c[4] = *((PIXELTYPE *)(pIn - SIZE_PIXEL)); + c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL)); + } + else + { + c[1] = c[2]; + c[4] = c[5]; + c[7] = c[8]; + } + + if (i < Xres - 1) + { + c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL)); + c[6] = *((PIXELTYPE *)(pIn + SIZE_PIXEL)); + c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL)); + } + else + { + c[3] = c[2]; + c[6] = c[5]; + c[9] = c[8]; + } + + int pattern = 0; + int flag = 1; + + YUV1 = RGBtoYUV(c[5]); + + for (k = 1; k <= 9; k++) + { + if (k == 5) continue; + + if (c[k] != c[5]) + { + YUV2 = RGBtoYUV(c[k]); + if ( + (abs32((YUV1 & Ymask) - (YUV2 & Ymask)) > trY) || + (abs32((YUV1 & Umask) - (YUV2 & Umask)) > trU) || + (abs32((YUV1 & Vmask) - (YUV2 & Vmask)) > trV) + ) + pattern |= flag; + } + flag <<= 1; + } + +#include "hq3x32.h" + pIn += SIZE_PIXEL; + pOut += 3 << 2; + } + pIn += srcPitch - (Xres << 2); + pOut += dstPitch - (3 * Xres << 2); + pOut += dstPitch << 1; + // pIn+=SIZE_PIXEL; + // pOut+=3*SIZE_PIXEL; + //} + //pIn+=srcPitch-(4*Xres); + //pOut+=dstPitch-(3*Xres*SIZE_PIXEL); + //pOut+=2*dstPitch; + } +} + +void hq3xS32(unsigned char *pIn, unsigned int srcPitch, + unsigned char *, + unsigned char *pOut, unsigned int dstPitch, + int Xres, int Yres) +{ + int i, j; + unsigned int line; + PIXELTYPE c[10]; + + // +----+----+----+ + // | | | | + // | c1 | c2 | c3 | + // +----+----+----+ + // | | | | + // | c4 | c5 | c6 | + // +----+----+----+ + // | | | | + // | c7 | c8 | c9 | + // +----+----+----+ + + for (j = 0; j < Yres; j++) + { + if ((j > 0) && (j < Yres - 1)) + line = srcPitch; + else + line = 0; + + for (i = 0; i < Xres; i++) + { + c[2] = *((PIXELTYPE *)(pIn - line)); + c[5] = *((PIXELTYPE *)(pIn)); + c[8] = *((PIXELTYPE *)(pIn + line)); + + if (i > 0) + { + c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL)); + c[4] = *((PIXELTYPE *)(pIn - SIZE_PIXEL)); + c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL)); + } + else + { + c[1] = c[2]; + c[4] = c[5]; + c[7] = c[8]; + } + + if (i < Xres - 1) + { + c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL)); + c[6] = *((PIXELTYPE *)(pIn + SIZE_PIXEL)); + c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL)); + } + else + { + c[3] = c[2]; + c[6] = c[5]; + c[9] = c[8]; + } + + int pattern = 0; + + // hq3xS dynamic edge detection: + // simply comparing the center color against its surroundings will give bad results in many cases, + // so, instead, compare the center color relative to the max difference in brightness of this 3x3 block + int brightArray[10]; + int maxBright = 0, minBright = 999999; + for (int j = 1; j < 10; j++) + { + const int b = (int)((c[j] & 0xF8)); + const int g = (int)((c[j] & 0xF800)) >> 8; + const int r = (int)((c[j] & 0xF80000)) >> 16; + const int bright = r + r + r + g + g + g + b + b; + if (bright > maxBright) maxBright = bright; + if (bright < minBright) minBright = bright; + + brightArray[j] = bright; + } + int diffBright = ((maxBright - minBright) * 7) >> 4; + if (diffBright > 7) + { + #define ABS(x) ((x) < 0 ? -(x) : (x)) + + const int centerBright = brightArray[5]; + if (ABS(brightArray[1] - centerBright) > diffBright) + pattern |= 1 << 0; + if (ABS(brightArray[2] - centerBright) > diffBright) + pattern |= 1 << 1; + if (ABS(brightArray[3] - centerBright) > diffBright) + pattern |= 1 << 2; + if (ABS(brightArray[4] - centerBright) > diffBright) + pattern |= 1 << 3; + if (ABS(brightArray[6] - centerBright) > diffBright) + pattern |= 1 << 4; + if (ABS(brightArray[7] - centerBright) > diffBright) + pattern |= 1 << 5; + if (ABS(brightArray[8] - centerBright) > diffBright) + pattern |= 1 << 6; + if (ABS(brightArray[9] - centerBright) > diffBright) + pattern |= 1 << 7; + } + +#define Diff(x, y) false //(ABS((x) - (y)) > diffBright) +#undef cget +#define cget(x) brightArray[x] +#include "hq3x32.h" +#undef cget +#undef Diff + pIn += SIZE_PIXEL; + pOut += 3 << 2; + } + pIn += srcPitch - (Xres << 2); + pOut += dstPitch - (3 * Xres << 2); + pOut += dstPitch << 1; + // pIn+=SIZE_PIXEL; + // pOut+=3*SIZE_PIXEL; + //} + //pIn+=srcPitch-(4*Xres); + //pOut+=dstPitch-(3*Xres*SIZE_PIXEL); + //pOut+=2*dstPitch; + } +} \ No newline at end of file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/hq3x32.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/hq3x32.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,3674 @@ +#define PIXEL00_1M Interp1( pOut, c[5], c[1] ); +#define PIXEL00_1U Interp1( pOut, c[5], c[2] ); +#define PIXEL00_1L Interp1( pOut, c[5], c[4] ); +#define PIXEL00_2 Interp2( pOut, c[5], c[4], c[2] ); +#define PIXEL00_4 Interp4( pOut, c[5], c[4], c[2] ); +#define PIXEL00_5 Interp5( pOut, c[4], c[2] ); +#define PIXEL00_C *((PIXELTYPE*)(pOut)) = c[5]; + +#define PIXEL01_1 Interp1( pOut+SIZE_PIXEL, c[5], c[2] ); +#define PIXEL01_3 Interp3( pOut+SIZE_PIXEL, c[5], c[2] ); +#define PIXEL01_6 Interp1( pOut+SIZE_PIXEL, c[2], c[5] ); +#define PIXEL01_C *((PIXELTYPE*)(pOut+SIZE_PIXEL)) = c[5]; + +#define PIXEL02_1M Interp1( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[3] ); +#define PIXEL02_1U Interp1( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[2] ); +#define PIXEL02_1R Interp1( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6] ); +#define PIXEL02_2 Interp2( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[2], c[6] ); +#define PIXEL02_4 Interp4( pOut+SIZE_PIXEL+SIZE_PIXEL, c[5], c[2], c[6] ); +#define PIXEL02_5 Interp5( pOut+SIZE_PIXEL+SIZE_PIXEL, c[2], c[6] ); +#define PIXEL02_C *((PIXELTYPE*)(pOut+SIZE_PIXEL+SIZE_PIXEL)) = c[5]; + +#define PIXEL10_1 Interp1( pOut+dstPitch, c[5], c[4] ); +#define PIXEL10_3 Interp3( pOut+dstPitch, c[5], c[4] ); +#define PIXEL10_6 Interp1( pOut+dstPitch, c[4], c[5] ); +#define PIXEL10_C *((PIXELTYPE*)(pOut+dstPitch)) = c[5]; + +#define PIXEL11 *((PIXELTYPE*)(pOut+dstPitch+SIZE_PIXEL)) = c[5]; + +#define PIXEL12_1 Interp1( pOut+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6] ); +#define PIXEL12_3 Interp3( pOut+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6] ); +#define PIXEL12_6 Interp1( pOut+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[6], c[5] ); +#define PIXEL12_C *((PIXELTYPE*)(pOut+dstPitch+SIZE_PIXEL+SIZE_PIXEL)) = c[5]; + +#define PIXEL20_1M Interp1( pOut+dstPitch+dstPitch, c[5], c[7] ); +#define PIXEL20_1D Interp1( pOut+dstPitch+dstPitch, c[5], c[8] ); +#define PIXEL20_1L Interp1( pOut+dstPitch+dstPitch, c[5], c[4] ); +#define PIXEL20_2 Interp2( pOut+dstPitch+dstPitch, c[5], c[8], c[4] ); +#define PIXEL20_4 Interp4( pOut+dstPitch+dstPitch, c[5], c[8], c[4] ); +#define PIXEL20_5 Interp5( pOut+dstPitch+dstPitch, c[8], c[4] ); +#define PIXEL20_C *((PIXELTYPE*)(pOut+dstPitch+dstPitch)) = c[5]; + +#define PIXEL21_1 Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL, c[5], c[8] ); +#define PIXEL21_3 Interp3( pOut+dstPitch+dstPitch+SIZE_PIXEL, c[5], c[8] ); +#define PIXEL21_6 Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL, c[8], c[5] ); +#define PIXEL21_C *((PIXELTYPE*)(pOut+dstPitch+dstPitch+SIZE_PIXEL)) = c[5]; + +#define PIXEL22_1M Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[9] ); +#define PIXEL22_1D Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[8] ); +#define PIXEL22_1R Interp1( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6] ); +#define PIXEL22_2 Interp2( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6], c[8] ); +#define PIXEL22_4 Interp4( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[5], c[6], c[8] ); +#define PIXEL22_5 Interp5( pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL, c[6], c[8] ); +#define PIXEL22_C *((PIXELTYPE*)(pOut+dstPitch+dstPitch+SIZE_PIXEL+SIZE_PIXEL)) = c[5]; + +#ifndef cget +#define cget(x) c[x] +#endif + + switch (pattern) + { + case 0: + case 1: + case 4: + case 32: + case 128: + case 5: + case 132: + case 160: + case 33: + case 129: + case 36: + case 133: + case 164: + case 161: + case 37: + case 165: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 2: + case 34: + case 130: + case 162: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 16: + case 17: + case 48: + case 49: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 64: + case 65: + case 68: + case 69: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 8: + case 12: + case 136: + case 140: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 3: + case 35: + case 131: + case 163: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 6: + case 38: + case 134: + case 166: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 20: + case 21: + case 52: + case 53: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 144: + case 145: + case 176: + case 177: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 192: + case 193: + case 196: + case 197: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 96: + case 97: + case 100: + case 101: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 40: + case 44: + case 168: + case 172: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 9: + case 13: + case 137: + case 141: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 18: + case 50: + { + PIXEL00_1M + + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_1M + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 80: + case 81: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_1M + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 72: + case 76: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_1M + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 10: + case 138: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 66: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 24: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 7: + case 39: + case 135: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 148: + case 149: + case 180: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 224: + case 228: + case 225: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 41: + case 169: + case 45: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 22: + case 54: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 208: + case 209: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 104: + case 108: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 11: + case 139: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 19: + case 51: + { + if (Diff(cget(2), cget(6))) + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL12_C + } + else + { + PIXEL00_2 + PIXEL01_6 + PIXEL02_5 + PIXEL12_1 + } + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 146: + case 178: + { + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_1M + PIXEL12_C + PIXEL22_1D + } + else + { + PIXEL01_1 + PIXEL02_5 + PIXEL12_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + break; + } + case 84: + case 85: + { + if (Diff(cget(6), cget(8))) + { + PIXEL02_1U + PIXEL12_C + PIXEL21_C + PIXEL22_1M + } + else + { + PIXEL02_2 + PIXEL12_6 + PIXEL21_1 + PIXEL22_5 + } + PIXEL00_2 + PIXEL01_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + break; + } + case 112: + case 113: + { + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + } + else + { + PIXEL12_1 + PIXEL20_2 + PIXEL21_6 + PIXEL22_5 + } + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + break; + } + case 200: + case 204: + { + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + } + else + { + PIXEL10_1 + PIXEL20_5 + PIXEL21_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + break; + } + case 73: + case 77: + { + if (Diff(cget(8), cget(4))) + { + PIXEL00_1U + PIXEL10_C + PIXEL20_1M + PIXEL21_C + } + else + { + PIXEL00_2 + PIXEL10_6 + PIXEL20_5 + PIXEL21_1 + } + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + PIXEL22_1M + break; + } + case 42: + case 170: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + PIXEL01_C + PIXEL10_C + PIXEL20_1D + } + else + { + PIXEL00_5 + PIXEL01_1 + PIXEL10_6 + PIXEL20_2 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL21_1 + PIXEL22_2 + break; + } + case 14: + case 142: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_C + } + else + { + PIXEL00_5 + PIXEL01_6 + PIXEL02_2 + PIXEL10_1 + } + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 67: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 70: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 28: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 152: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 194: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 98: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 56: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 25: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 26: + case 31: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL10_3 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL02_4 + PIXEL12_3 + } + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 82: + case 214: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + } + else + { + PIXEL01_3 + PIXEL02_4 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 88: + case 248: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + } + else + { + PIXEL10_3 + PIXEL20_4 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL22_4 + } + break; + } + case 74: + case 107: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + } + else + { + PIXEL00_4 + PIXEL01_3 + } + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 27: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 86: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 216: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 106: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 30: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 210: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 120: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 75: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 29: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1M + break; + } + case 198: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 184: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 99: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 57: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 71: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 156: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 226: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 60: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 195: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 102: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 153: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 58: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 83: + { + PIXEL00_1L + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 92: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 202: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 78: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1M + break; + } + case 154: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 114: + { + PIXEL00_1M + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 89: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 90: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 55: + case 23: + { + if (Diff(cget(2), cget(6))) + { + PIXEL00_1L + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL00_2 + PIXEL01_6 + PIXEL02_5 + PIXEL12_1 + } + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + PIXEL22_1M + break; + } + case 182: + case 150: + { + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + PIXEL22_1D + } + else + { + PIXEL01_1 + PIXEL02_5 + PIXEL12_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL10_1 + PIXEL11 + PIXEL20_2 + PIXEL21_1 + break; + } + case 213: + case 212: + { + if (Diff(cget(6), cget(8))) + { + PIXEL02_1U + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL02_2 + PIXEL12_6 + PIXEL21_1 + PIXEL22_5 + } + PIXEL00_2 + PIXEL01_1 + PIXEL10_1 + PIXEL11 + PIXEL20_1M + break; + } + case 241: + case 240: + { + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL20_1L + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_1 + PIXEL20_2 + PIXEL21_6 + PIXEL22_5 + } + PIXEL00_2 + PIXEL01_1 + PIXEL02_1M + PIXEL10_1 + PIXEL11 + break; + } + case 236: + case 232: + { + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + PIXEL22_1R + } + else + { + PIXEL10_1 + PIXEL20_5 + PIXEL21_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + break; + } + case 109: + case 105: + { + if (Diff(cget(8), cget(4))) + { + PIXEL00_1U + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL00_2 + PIXEL10_6 + PIXEL20_5 + PIXEL21_1 + } + PIXEL01_1 + PIXEL02_2 + PIXEL11 + PIXEL12_1 + PIXEL22_1M + break; + } + case 171: + case 43: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + PIXEL20_1D + } + else + { + PIXEL00_5 + PIXEL01_1 + PIXEL10_6 + PIXEL20_2 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL21_1 + PIXEL22_2 + break; + } + case 143: + case 15: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL02_1R + PIXEL10_C + } + else + { + PIXEL00_5 + PIXEL01_6 + PIXEL02_2 + PIXEL10_1 + } + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_1 + PIXEL22_2 + break; + } + case 124: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 203: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 62: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 211: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 118: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 217: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 110: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 155: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 188: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 185: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 61: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 157: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 103: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 227: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 230: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 199: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 220: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 158: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 234: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1M + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1R + break; + } + case 242: + { + PIXEL00_1M + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1L + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 59: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 121: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 87: + { + PIXEL00_1L + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1M + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 79: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1R + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1M + break; + } + case 122: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 94: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 218: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 91: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 229: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_2 + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 167: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_2 + PIXEL21_1 + PIXEL22_2 + break; + } + case 173: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 181: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 186: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 115: + { + PIXEL00_1L + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 93: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 206: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 205: + case 201: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_1M + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 174: + case 46: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_1M + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 179: + case 147: + { + PIXEL00_1L + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_1M + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 117: + case 116: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_1M + } + else + { + PIXEL22_2 + } + break; + } + case 189: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 231: + { + PIXEL00_1L + PIXEL01_C + PIXEL02_1R + PIXEL10_1 + PIXEL11 + PIXEL12_1 + PIXEL20_1L + PIXEL21_C + PIXEL22_1R + break; + } + case 126: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_4 + PIXEL12_3 + } + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 219: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL01_3 + PIXEL10_3 + } + PIXEL02_1M + PIXEL11 + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 125: + { + if (Diff(cget(8), cget(4))) + { + PIXEL00_1U + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL00_2 + PIXEL10_6 + PIXEL20_5 + PIXEL21_1 + } + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + PIXEL22_1M + break; + } + case 221: + { + if (Diff(cget(6), cget(8))) + { + PIXEL02_1U + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL02_2 + PIXEL12_6 + PIXEL21_1 + PIXEL22_5 + } + PIXEL00_1U + PIXEL01_1 + PIXEL10_C + PIXEL11 + PIXEL20_1M + break; + } + case 207: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL02_1R + PIXEL10_C + } + else + { + PIXEL00_5 + PIXEL01_6 + PIXEL02_2 + PIXEL10_1 + } + PIXEL11 + PIXEL12_1 + PIXEL20_1M + PIXEL21_C + PIXEL22_1R + break; + } + case 238: + { + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + PIXEL22_1R + } + else + { + PIXEL10_1 + PIXEL20_5 + PIXEL21_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL01_C + PIXEL02_1R + PIXEL11 + PIXEL12_1 + break; + } + case 190: + { + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + PIXEL22_1D + } + else + { + PIXEL01_1 + PIXEL02_5 + PIXEL12_6 + PIXEL22_2 + } + PIXEL00_1M + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + break; + } + case 187: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + PIXEL20_1D + } + else + { + PIXEL00_5 + PIXEL01_1 + PIXEL10_6 + PIXEL20_2 + } + PIXEL02_1M + PIXEL11 + PIXEL12_C + PIXEL21_1 + PIXEL22_1D + break; + } + case 243: + { + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL20_1L + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_1 + PIXEL20_2 + PIXEL21_6 + PIXEL22_5 + } + PIXEL00_1L + PIXEL01_C + PIXEL02_1M + PIXEL10_1 + PIXEL11 + break; + } + case 119: + { + if (Diff(cget(2), cget(6))) + { + PIXEL00_1L + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL00_2 + PIXEL01_6 + PIXEL02_5 + PIXEL12_1 + } + PIXEL10_1 + PIXEL11 + PIXEL20_1L + PIXEL21_C + PIXEL22_1M + break; + } + case 237: + case 233: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_2 + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 175: + case 47: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + PIXEL20_1D + PIXEL21_1 + PIXEL22_2 + break; + } + case 183: + case 151: + { + PIXEL00_1L + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_2 + PIXEL21_1 + PIXEL22_1D + break; + } + case 245: + case 244: + { + PIXEL00_2 + PIXEL01_1 + PIXEL02_1U + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 250: + { + PIXEL00_1M + PIXEL01_C + PIXEL02_1M + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + } + else + { + PIXEL10_3 + PIXEL20_4 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL22_4 + } + break; + } + case 123: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + } + else + { + PIXEL00_4 + PIXEL01_3 + } + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 95: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL10_3 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL02_4 + PIXEL12_3 + } + PIXEL11 + PIXEL20_1M + PIXEL21_C + PIXEL22_1M + break; + } + case 222: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + } + else + { + PIXEL01_3 + PIXEL02_4 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 252: + { + PIXEL00_1M + PIXEL01_1 + PIXEL02_1U + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + } + else + { + PIXEL10_3 + PIXEL20_4 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 249: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1M + PIXEL10_C + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL22_4 + } + break; + } + case 235: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + } + else + { + PIXEL00_4 + PIXEL01_3 + } + PIXEL02_1M + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 111: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 63: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL02_4 + PIXEL12_3 + } + PIXEL10_C + PIXEL11 + PIXEL20_1D + PIXEL21_1 + PIXEL22_1M + break; + } + case 159: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL10_3 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL11 + PIXEL12_C + PIXEL20_1M + PIXEL21_1 + PIXEL22_1D + break; + } + case 215: + { + PIXEL00_1L + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 246: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + } + else + { + PIXEL01_3 + PIXEL02_4 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 254: + { + PIXEL00_1M + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + } + else + { + PIXEL01_3 + PIXEL02_4 + } + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + } + else + { + PIXEL10_3 + PIXEL20_4 + } + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL21_3 + PIXEL22_2 + } + break; + } + case 253: + { + PIXEL00_1U + PIXEL01_1 + PIXEL02_1U + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 251: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + } + else + { + PIXEL00_4 + PIXEL01_3 + } + PIXEL02_1M + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL10_C + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL10_3 + PIXEL20_2 + PIXEL21_3 + } + if (Diff(cget(6), cget(8))) + { + PIXEL12_C + PIXEL22_C + } + else + { + PIXEL12_3 + PIXEL22_4 + } + break; + } + case 239: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + PIXEL02_1R + PIXEL10_C + PIXEL11 + PIXEL12_1 + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + PIXEL22_1R + break; + } + case 127: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL01_C + PIXEL10_C + } + else + { + PIXEL00_2 + PIXEL01_3 + PIXEL10_3 + } + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL02_4 + PIXEL12_3 + } + PIXEL11 + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + PIXEL21_C + } + else + { + PIXEL20_4 + PIXEL21_3 + } + PIXEL22_1M + break; + } + case 191: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + PIXEL20_1D + PIXEL21_1 + PIXEL22_1D + break; + } + case 223: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + PIXEL10_C + } + else + { + PIXEL00_4 + PIXEL10_3 + } + if (Diff(cget(2), cget(6))) + { + PIXEL01_C + PIXEL02_C + PIXEL12_C + } + else + { + PIXEL01_3 + PIXEL02_2 + PIXEL12_3 + } + PIXEL11 + PIXEL20_1M + if (Diff(cget(6), cget(8))) + { + PIXEL21_C + PIXEL22_C + } + else + { + PIXEL21_3 + PIXEL22_4 + } + break; + } + case 247: + { + PIXEL00_1L + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_1 + PIXEL11 + PIXEL12_C + PIXEL20_1L + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + case 255: + { + if (Diff(cget(4), cget(2))) + { + PIXEL00_C + } + else + { + PIXEL00_2 + } + PIXEL01_C + if (Diff(cget(2), cget(6))) + { + PIXEL02_C + } + else + { + PIXEL02_2 + } + PIXEL10_C + PIXEL11 + PIXEL12_C + if (Diff(cget(8), cget(4))) + { + PIXEL20_C + } + else + { + PIXEL20_2 + } + PIXEL21_C + if (Diff(cget(6), cget(8))) + { + PIXEL22_C + } + else + { + PIXEL22_2 + } + break; + } + } diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/hq_shared32.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/hq_shared32.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,389 @@ +#include "../Port.h" +#include "hq_shared32.h" +#include "interp.h" + +const unsigned __int64 reg_blank = 0x0000000000000000; +const unsigned __int64 const7 = 0x0000000700070007; +const unsigned __int64 treshold = 0x0000000000300706; + +void Interp1(unsigned char *pc, unsigned int c1, unsigned int c2) +{ + //*((int*)pc) = (c1*3+c2)/4; + +#ifdef MMX + __asm + { + mov eax, pc + movd mm1, c1 + movd mm2, c2 + movq mm0, mm1 + pslld mm0, 2 + psubd mm0, mm1 + paddd mm0, mm2 + psrld mm0, 2 + movd [eax], mm0 + EMMS + } +#else + __asm + { + mov eax, pc + mov edx, c1 + shl edx, 2 + add edx, c2 + sub edx, c1 + shr edx, 2 + mov [eax], edx + } +#endif +} + +void Interp2(unsigned char *pc, unsigned int c1, unsigned int c2, unsigned int c3) +{ + //*((int*)pc) = (c1*2+c2+c3)/4; + +#ifdef MMX + __asm + { + mov eax, pc + movd mm0, c1 + movd mm1, c2 + movd mm2, c3 + pslld mm0, 1 + paddd mm0, mm1 + paddd mm0, mm2 + psrad mm0, 2 + movd [eax], mm0 + EMMS + } +#else + __asm + { + mov eax, pc + mov edx, c1 + shl edx, 1 + add edx, c2 + add edx, c3 + shr edx, 2 + mov [eax], edx + } +#endif +} + +void Interp3(unsigned char *pc, unsigned int c1, unsigned int c2) +{ + //*((int*)pc) = (c1*7+c2)/8; + //*((int*)pc) = ((((c1 & 0x00FF00)*7 + (c2 & 0x00FF00) ) & 0x0007F800) + + // (((c1 & 0xFF00FF)*7 + (c2 & 0xFF00FF) ) & 0x07F807F8)) >> 3; + +#ifdef MMX + __asm + { + mov eax, pc + movd mm1, c1 + movd mm2, c2 + punpcklbw mm1, reg_blank + punpcklbw mm2, reg_blank + pmullw mm1, const7 + paddw mm1, mm2 + psrlw mm1, 3 + packuswb mm1, reg_blank + movd [eax], mm1 + EMMS + } +#else + __asm + { + mov eax, c1 + mov ebx, c2 + mov ecx, eax + shl ecx, 3 + sub ecx, eax + add ecx, ebx + shr ecx, 3 + mov eax, pc + mov [eax], ecx + } +#endif +} + +void Interp4(unsigned char *pc, unsigned int c1, unsigned int c2, unsigned int c3) +{ + //*((int*)pc) = (c1*2+(c2+c3)*7)/16; + //*((int*)pc) = ((((c1 & 0x00FF00)*2 + ((c2 & 0x00FF00) + (c3 & 0x00FF00))*7 ) & 0x000FF000) + + // (((c1 & 0xFF00FF)*2 + ((c2 & 0xFF00FF) + (c3 & 0xFF00FF))*7 ) & 0x0FF00FF0)) >> 4; + +#ifdef MMX + __asm + { + mov eax, pc + movd mm1, c1 + movd mm2, c2 + movd mm3, c3 + punpcklbw mm1, reg_blank + punpcklbw mm2, reg_blank + punpcklbw mm3, reg_blank + psllw mm1, 1 + paddw mm2, mm3 + pmullw mm2, const7 + paddw mm1, mm2 + psrlw mm1, 4 + packuswb mm1, reg_blank + movd [eax], mm1 + EMMS + } +#else + + __asm + { + mov eax, [c1] + and eax, 0FF00h + shl eax, 1 + mov ecx, [c2] + and ecx, 0FF00h + mov edx, [c3] + and edx, 0FF00h + add ecx, edx + imul ecx, ecx, 7 + add eax, ecx + and eax, 0FF000h + + mov ebx, [c1] + and ebx, 0FF00FFh + shl ebx, 1 + mov ecx, [c2] + and ecx, 0FF00FFh + mov edx, [c3] + and edx, 0FF00FFh + add ecx, edx + imul ecx, ecx, 7 + add ebx, ecx + and ebx, 0FF00FF0h + + add eax, ebx + shr eax, 4 + + mov ebx, pc + mov [ebx], eax + } +#endif +} + +void Interp5(unsigned char *pc, unsigned int c1, unsigned int c2) +{ + //*((int*)pc) = (c1+c2)/2; + +#ifdef MMX + __asm + { + mov eax, pc + movd mm0, c1 + movd mm1, c2 + paddd mm0, mm1 + psrad mm0, 1 + movd [eax], mm0 + EMMS + } +#else + __asm + { + mov eax, pc + mov edx, c1 + add edx, c2 + shr edx, 1 + mov [eax], edx + } +#endif +} + +void Interp1_16(unsigned char *pc, unsigned short c1, unsigned short c2) +{ + *((unsigned short *)pc) = interp_16_31(c1, c2); + //*((int*)pc) = (c1*3+c2)/4; +} + +void Interp2_16(unsigned char *pc, unsigned short c1, unsigned short c2, unsigned short c3) +{ + *((unsigned short *)pc) = interp_16_211(c1, c2, c3); + //*((int*)pc) = (c1*2+c2+c3)/4; +} + +void Interp3_16(unsigned char *pc, unsigned short c1, unsigned short c2) +{ + *((unsigned short *)pc) = interp_16_71(c1, c2); +// *((unsigned short*)pc) = (c1*7+c2)/8; +// *((unsigned short*)pc) = ((((c1 & 0x00FF00)*7 + (c2 & 0x00FF00) ) & 0x0007F800) + +// (((c1 & 0xFF00FF)*7 + (c2 & 0xFF00FF) ) & 0x07F807F8)) >> 3; +} + +void Interp4_16(unsigned char *pc, unsigned short c1, unsigned short c2, unsigned short c3) +{ + *((unsigned short *)pc) = interp_16_772(c2, c3, c1); +// *((unsigned short*)pc) = (c1*2+(c2+c3)*7)/16; +// *((unsigned short*)pc) = ((((c1 & 0x00FF00)*2 + ((c2 & 0x00FF00) + (c3 & 0x00FF00))*7 ) & 0x000FF000) + +// (((c1 & 0xFF00FF)*2 + ((c2 & 0xFF00FF) + (c3 & 0xFF00FF))*7 ) & 0x0FF00FF0)) >> 4; +} + +void Interp5_16(unsigned char *pc, unsigned short c1, unsigned short c2) +{ + *((unsigned short *)pc) = interp_16_11(c1, c2); +} + +bool Diff(unsigned int c1, unsigned int c2) +{ + unsigned int + YUV1 = RGBtoYUV(c1), + YUV2 = RGBtoYUV(c2); + + if (YUV1 == YUV2) return false; // Save some processing power + +#ifdef MMX + unsigned int retval; + __asm + { + mov eax, 0x7FFFFFFF + movd mm7, eax; mm7 = ABS_MASK = 0x7FFFFFFF + + ; Copy source colors in first reg + movd mm0, YUV1 + movd mm1, YUV2 + + mov eax, 0x00FF0000 + movd mm6, eax; mm6 = Ymask = 0x00FF0000 + + ; Calculate color Y difference + movq mm2, mm0 + movq mm3, mm1 + pand mm2, mm6 + pand mm3, mm6 + psubd mm2, mm3 + pand mm2, mm7 + + mov eax, 0x0000FF00 + movd mm6, eax; mm6 = Umask = 0x0000FF00 + + ; Calculate color U difference + movq mm3, mm0 + movq mm4, mm1 + pand mm3, mm6 + pand mm4, mm6 + psubd mm3, mm4 + pand mm3, mm7 + + mov eax, 0x000000FF + movd mm6, eax; mm6 = Vmask = 0x000000FF + + ; Calculate color V difference + movq mm4, mm0 + movq mm5, mm1 + pand mm4, mm6 + pand mm5, mm6 + psubd mm4, mm5 + pand mm4, mm7 + + mov eax, 0x00300000 + movd mm5, eax; mm5 = trY = 0x00300000 + mov eax, 0x00000700 + movd mm6, eax; mm6 = trU = 0x00000700 + mov eax, 0x00000006 + movd mm7, eax; mm7 = trV = 0x00000006 + + ; Compare the results + pcmpgtd mm2, trY + pcmpgtd mm3, trU + pcmpgtd mm4, trV + por mm2, mm3 + por mm2, mm4 + + movd retval, mm2 + + EMMS + } + return (retval != 0); +#else + return + (abs32((YUV1 & Ymask) - (YUV2 & Ymask)) > trY) || + (abs32((YUV1 & Umask) - (YUV2 & Umask)) > trU) || + (abs32((YUV1 & Vmask) - (YUV2 & Vmask)) > trV); +#endif +} + +unsigned int RGBtoYUV(unsigned int c) +{ // Division through 3 slows down the emulation about 10% !!! +#ifdef MMX + unsigned int retval; + __asm + { + movd mm0, c + movq mm1, mm0 + movq mm2, mm0; mm0 = mm1 = mm2 = c + + mov eax, 0x000000FF + movd mm5, eax; mm5 = REDMASK = 0x000000FF + mov eax, 0x0000FF00 + movd mm6, eax; mm6 = GREENMASK = 0x0000FF00 + mov eax, 0x00FF0000 + movd mm7, eax; mm7 = BLUEMASK = 0x00FF0000 + + pand mm0, mm5 + pand mm1, mm6 + pand mm2, mm7; mm0 = R mm1 = G mm2 = B + + movq mm3, mm0 + paddd mm3, mm1 + paddd mm3, mm2 + ; psrld mm3, 2; mm3 = Y + ; pslld mm3, 16 + pslld mm3, 14; mm3 = Y << 16 + + mov eax, 512 + movd mm7, eax; mm7 = 128 << 2 = 512 + + movq mm4, mm0 + psubd mm4, mm2 + ; psrld mm4, 2 + ; paddd mm4, mm7; mm4 = U + ; pslld mm4, 8; mm4 = U << 8 + paddd mm4, mm7 + pslld mm4, 6 + + mov eax, 128 + movd mm7, eax; mm7 = 128 + + movq mm5, mm1 + pslld mm5, 1 + psubd mm5, mm0 + psubd mm5, mm2 + psrld mm5, 3 + paddd mm5, mm7; mm5 = V + + paddd mm5, mm4 + paddd mm5, mm3 + + movd retval, mm5 + + EMMS + } + return retval; +#else + unsigned char r, g, b, Y, u, v; + r = (c & 0x000000FF); + g = (c & 0x0000FF00) >> 8; + b = (c & 0x00FF0000) >> 16; + Y = (r + g + b) >> 2; + u = 128 + ((r - b) >> 2); + v = 128 + ((-r + 2 * g - b) >> 3); + return (Y << 16) + (u << 8) + v; + + // Extremely High Quality Code + //unsigned char r, g, b; + //r = c & 0xFF; + //g = (c >> 8) & 0xFF; + //b = (c >> 16) & 0xFF; + //unsigned char y, u, v; + //y = (0.256788 * r + 0.504129 * g + 0.097906 * b) + 16; + //u = (-0.148223 * r - 0.290993 * g + 0.439216 * b) + 128; + //v = (0.439216 * r - 0.367788 * g - 0.071427 * b) + 128; + //return (y << 16) + (u << 8) + v; +#endif +} \ No newline at end of file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/hq_shared32.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/hq_shared32.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,31 @@ +#ifndef VBA_HQ_SHARED32_H +#define VBA_HQ_SHARED32_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#define abs32(value) (value & 0x7FFFFFFF) +#define abs16(value) (value & 0x7FFF) + +const int Ymask = 0x00FF0000; +const int Umask = 0x0000FF00; +const int Vmask = 0x000000FF; +const int trY = 0x00300000; +const int trU = 0x00000700; +const int trV = 0x00000006; + +void Interp1(unsigned char *pc, unsigned int c1, unsigned int c2); +void Interp2(unsigned char *pc, unsigned int c1, unsigned int c2, unsigned int c3); +void Interp3(unsigned char *pc, unsigned int c1, unsigned int c2); +void Interp4(unsigned char *pc, unsigned int c1, unsigned int c2, unsigned int c3); +void Interp5(unsigned char *pc, unsigned int c1, unsigned int c2); +void Interp1_16(unsigned char *pc, unsigned short c1, unsigned short c2); +void Interp2_16(unsigned char *pc, unsigned short c1, unsigned short c2, unsigned short c3); +void Interp3_16(unsigned char *pc, unsigned short c1, unsigned short c2); +void Interp4_16(unsigned char *pc, unsigned short c1, unsigned short c2, unsigned short c3); +void Interp5_16(unsigned char *pc, unsigned short c1, unsigned short c2); +bool Diff(unsigned int c1, unsigned int c2); +unsigned int RGBtoYUV(unsigned int c); + +#endif // VBA_HQ_SHARED32_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/interframe.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/interframe.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,630 @@ +#include +#include +#include "../Port.h" + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif + +/* + * Thanks to Kawaks' Mr. K for the code + + Incorporated into vba by Anthony Di Franco + */ + +static u8 *frm1 = NULL; +static u8 *frm2 = NULL; +static u8 *frm3 = NULL; + +extern u32 RGB_LOW_BITS_MASK; +extern u32 qRGB_COLOR_MASK[2]; + +static void Init() +{ + frm1 = (u8 *)calloc(322 * 242, 4); + // 1 frame ago + frm2 = (u8 *)calloc(322 * 242, 4); + // 2 frames ago + frm3 = (u8 *)calloc(322 * 242, 4); + // 3 frames ago +} + +void InterframeCleanup() +{ + if (frm1) + free(frm1); + if (frm2) + free(frm2); + if (frm3) + free(frm3); + frm1 = frm2 = frm3 = NULL; +} + +#ifdef MMX +static void SmartIB_MMX(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + u16 *src2 = (u16 *)frm2; + u16 *src3 = (u16 *)frm3; + + int count = width >> 2; + + for (int i = 0; i < height; i++) + { +#ifdef __GNUC__ + asm volatile ( + "push %4\n" + "movq 0(%5), %%mm7\n" // colorMask + "0:\n" + "movq 0(%0), %%mm0\n" // src0 + "movq 0(%1), %%mm1\n" // src1 + "movq 0(%2), %%mm2\n" // src2 + "movq 0(%3), %%mm3\n" // src3 + "movq %%mm0, 0(%3)\n" // src3 = src0 + "movq %%mm0, %%mm4\n" + "movq %%mm1, %%mm5\n" + "pcmpeqw %%mm2, %%mm5\n" // src1 == src2 (A) + "pcmpeqw %%mm3, %%mm4\n" // src3 == src0 (B) + "por %%mm5, %%mm4\n" // A | B + "movq %%mm2, %%mm5\n" + "pcmpeqw %%mm0, %%mm5\n" // src0 == src2 (C) + "pcmpeqw %%mm1, %%mm3\n" // src1 == src3 (D) + "por %%mm3, %%mm5\n" // C|D + "pandn %%mm5, %%mm4\n" // (!(A|B))&(C|D) + "movq %%mm0, %%mm2\n" + "pand %%mm7, %%mm2\n" // color & colorMask + "pand %%mm7, %%mm1\n" // src1 & colorMask + "psrlw $1, %%mm2\n" // (color & colorMask) >> 1 (E) + "psrlw $1, %%mm1\n" // (src & colorMask) >> 1 (F) + "paddw %%mm2, %%mm1\n" // E+F + "pand %%mm4, %%mm1\n" // (E+F) & res + "pandn %%mm0, %%mm4\n" // color& !res + + "por %%mm1, %%mm4\n" + "movq %%mm4, 0(%0)\n" // src0 = res + + "addl $8, %0\n" + "addl $8, %1\n" + "addl $8, %2\n" + "addl $8, %3\n" + + "decl %4\n" + "jnz 0b\n" + "pop %4\n" + "emms\n" + : "+r" (src0), "+r" (src1), "+r" (src2), "+r" (src3) + : "r" (count), "r" (qRGB_COLOR_MASK) + ); +#else + __asm { + movq mm7, qword ptr [qRGB_COLOR_MASK]; + mov eax, src0; + mov ebx, src1; + mov ecx, src2; + mov edx, src3; + mov edi, count; +label0: + movq mm0, qword ptr [eax]; // src0 + movq mm1, qword ptr [ebx]; // src1 + movq mm2, qword ptr [ecx]; // src2 + movq mm3, qword ptr [edx]; // src3 + movq qword ptr [edx], mm0; // src3 = src0 + movq mm4, mm0; + movq mm5, mm1; + pcmpeqw mm5, mm2; // src1 == src2 (A) + pcmpeqw mm4, mm3; // src3 == src0 (B) + por mm4, mm5; // A | B + movq mm5, mm2; + pcmpeqw mm5, mm0; // src0 == src2 (C) + pcmpeqw mm3, mm1; // src1 == src3 (D) + por mm5, mm3; // C|D + pandn mm4, mm5; // (!(A|B))&(C|D) + movq mm2, mm0; + pand mm2, mm7; // color & colorMask + pand mm1, mm7; // src1 & colorMask + psrlw mm2, 1; // (color & colorMask) >> 1 (E) + psrlw mm1, 1; // (src & colorMask) >> 1 (F) + paddw mm1, mm2; // E+F + pand mm1, mm4; // (E+F) & res + pandn mm4, mm0; // color & !res + + por mm4, mm1; + movq qword ptr [eax], mm4; // src0 = res + + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 8; + + dec edi; + jnz label0; + mov src0, eax; + mov src1, ebx; + mov src2, ecx; + mov src3, edx; + emms; + } +#endif + src0 += 2; + src1 += 2; + src2 += 2; + src3 += 2; + } + + /* Swap buffers around */ + u8 *temp = frm1; + frm1 = frm3; + frm3 = frm2; + frm2 = temp; +} + +#endif + +void SmartIB(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if (frm1 == NULL) + { + Init(); + } +#ifdef MMX + if (cpu_mmx) + { + SmartIB_MMX(srcPtr, srcPitch, width, height); + return; + } +#endif + + u16 colorMask = ~RGB_LOW_BITS_MASK; + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + u16 *src2 = (u16 *)frm2; + u16 *src3 = (u16 *)frm3; + + int sPitch = srcPitch >> 1; + + int pos = 0; + for (int j = 0; j < height; j++) + for (int i = 0; i < sPitch; i++) + { + u16 color = src0[pos]; + src0[pos] = + (src1[pos] != src2[pos]) && + (src3[pos] != color) && + ((color == src2[pos]) || (src1[pos] == src3[pos])) + ? (((color & colorMask) >> 1) + ((src1[pos] & colorMask) >> 1)) : + color; + src3[pos] = color; /* oldest buffer now holds newest frame */ + pos++; + } + + /* Swap buffers around */ + u8 *temp = frm1; + frm1 = frm3; + frm3 = frm2; + frm2 = temp; +} + +#ifdef MMX +static void SmartIB32_MMX(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = (u32 *)frm1; + u32 *src2 = (u32 *)frm2; + u32 *src3 = (u32 *)frm3; + + int count = width >> 1; + + for (int i = 0; i < height; i++) + { +#ifdef __GNUC__ + asm volatile ( + "push %4\n" + "movq 0(%5), %%mm7\n" // colorMask + "0:\n" + "movq 0(%0), %%mm0\n" // src0 + "movq 0(%1), %%mm1\n" // src1 + "movq 0(%2), %%mm2\n" // src2 + "movq 0(%3), %%mm3\n" // src3 + "movq %%mm0, 0(%3)\n" // src3 = src0 + "movq %%mm0, %%mm4\n" + "movq %%mm1, %%mm5\n" + "pcmpeqd %%mm2, %%mm5\n" // src1 == src2 (A) + "pcmpeqd %%mm3, %%mm4\n" // src3 == src0 (B) + "por %%mm5, %%mm4\n" // A | B + "movq %%mm2, %%mm5\n" + "pcmpeqd %%mm0, %%mm5\n" // src0 == src2 (C) + "pcmpeqd %%mm1, %%mm3\n" // src1 == src3 (D) + "por %%mm3, %%mm5\n" // C|D + "pandn %%mm5, %%mm4\n" // (!(A|B))&(C|D) + "movq %%mm0, %%mm2\n" + "pand %%mm7, %%mm2\n" // color & colorMask + "pand %%mm7, %%mm1\n" // src1 & colorMask + "psrld $1, %%mm2\n" // (color & colorMask) >> 1 (E) + "psrld $1, %%mm1\n" // (src & colorMask) >> 1 (F) + "paddd %%mm2, %%mm1\n" // E+F + "pand %%mm4, %%mm1\n" // (E+F) & res + "pandn %%mm0, %%mm4\n" // color& !res + + "por %%mm1, %%mm4\n" + "movq %%mm4, 0(%0)\n" // src0 = res + + "addl $8, %0\n" + "addl $8, %1\n" + "addl $8, %2\n" + "addl $8, %3\n" + + "decl %4\n" + "jnz 0b\n" + "pop %4\n" + "emms\n" + : "+r" (src0), "+r" (src1), "+r" (src2), "+r" (src3) + : "r" (count), "r" (qRGB_COLOR_MASK) + ); +#else + __asm { + movq mm7, qword ptr [qRGB_COLOR_MASK]; + mov eax, src0; + mov ebx, src1; + mov ecx, src2; + mov edx, src3; + mov edi, count; +label0: + movq mm0, qword ptr [eax]; // src0 + movq mm1, qword ptr [ebx]; // src1 + movq mm2, qword ptr [ecx]; // src2 + movq mm3, qword ptr [edx]; // src3 + movq qword ptr [edx], mm0; // src3 = src0 + movq mm4, mm0; + movq mm5, mm1; + pcmpeqd mm5, mm2; // src1 == src2 (A) + pcmpeqd mm4, mm3; // src3 == src0 (B) + por mm4, mm5; // A | B + movq mm5, mm2; + pcmpeqd mm5, mm0; // src0 == src2 (C) + pcmpeqd mm3, mm1; // src1 == src3 (D) + por mm5, mm3; // C|D + pandn mm4, mm5; // (!(A|B))&(C|D) + movq mm2, mm0; + pand mm2, mm7; // color & colorMask + pand mm1, mm7; // src1 & colorMask + psrld mm2, 1; // (color & colorMask) >> 1 (E) + psrld mm1, 1; // (src & colorMask) >> 1 (F) + paddd mm1, mm2; // E+F + pand mm1, mm4; // (E+F) & res + pandn mm4, mm0; // color & !res + + por mm4, mm1; + movq qword ptr [eax], mm4; // src0 = res + + add eax, 8; + add ebx, 8; + add ecx, 8; + add edx, 8; + + dec edi; + jnz label0; + mov src0, eax; + mov src1, ebx; + mov src2, ecx; + mov src3, edx; + emms; + } +#endif + + src0++; + src1++; + src2++; + src3++; + } + /* Swap buffers around */ + u8 *temp = frm1; + frm1 = frm3; + frm3 = frm2; + frm2 = temp; +} + +#endif + +void SmartIB32(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if (frm1 == NULL) + { + Init(); + } +#ifdef MMX + if (cpu_mmx) + { + SmartIB32_MMX(srcPtr, srcPitch, width, height); + return; + } +#endif + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = (u32 *)frm1; + u32 *src2 = (u32 *)frm2; + u32 *src3 = (u32 *)frm3; + + u32 colorMask = 0xfefefe; + + int sPitch = srcPitch >> 2; + int pos = 0; + + for (int j = 0; j < height; j++) + for (int i = 0; i < sPitch; i++) + { + u32 color = src0[pos]; + src0[pos] = + (src1[pos] != src2[pos]) && + (src3[pos] != color) && + ((color == src2[pos]) || (src1[pos] == src3[pos])) + ? (((color & colorMask) >> 1) + ((src1[pos] & colorMask) >> 1)) : + color; + src3[pos] = color; /* oldest buffer now holds newest frame */ + pos++; + } + + /* Swap buffers around */ + u8 *temp = frm1; + frm1 = frm3; + frm3 = frm2; + frm2 = temp; +} + +#ifdef MMX +static void MotionBlurIB_MMX(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + + int count = width >> 2; + + for (int i = 0; i < height; i++) + { +#ifdef __GNUC__ + asm volatile ( + "push %2\n" + "movq 0(%3), %%mm7\n" // colorMask + "0:\n" + "movq 0(%0), %%mm0\n" // src0 + "movq 0(%1), %%mm1\n" // src1 + "movq %%mm0, 0(%1)\n" // src1 = src0 + "pand %%mm7, %%mm0\n" // color & colorMask + "pand %%mm7, %%mm1\n" // src1 & colorMask + "psrlw $1, %%mm0\n" // (color & colorMask) >> 1 (E) + "psrlw $1, %%mm1\n" // (src & colorMask) >> 1 (F) + "paddw %%mm1, %%mm0\n" // E+F + + "movq %%mm0, 0(%0)\n" // src0 = res + + "addl $8, %0\n" + "addl $8, %1\n" + + "decl %2\n" + "jnz 0b\n" + "pop %2\n" + "emms\n" + : "+r" (src0), "+r" (src1) + : "r" (count), "r" (qRGB_COLOR_MASK) + ); +#else + __asm { + movq mm7, qword ptr [qRGB_COLOR_MASK]; + mov eax, src0; + mov ebx, src1; + mov edi, count; +label0: + movq mm0, qword ptr [eax]; // src0 + movq mm1, qword ptr [ebx]; // src1 + movq qword ptr [ebx], mm0; // src1 = src0 + pand mm0, mm7; // color & colorMask + pand mm1, mm7; // src1 & colorMask + psrlw mm0, 1; // (color & colorMask) >> 1 (E) + psrlw mm1, 1; // (src & colorMask) >> 1 (F) + paddw mm0, mm1; // E+F + + movq qword ptr [eax], mm0; // src0 = res + + add eax, 8; + add ebx, 8; + + dec edi; + jnz label0; + mov src0, eax; + mov src1, ebx; + emms; + } +#endif + src0 += 2; + src1 += 2; + } +} + +#endif + +void MotionBlurIB(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if (frm1 == NULL) + { + Init(); + } + +#ifdef MMX + if (cpu_mmx) + { + MotionBlurIB_MMX(srcPtr, srcPitch, width, height); + return; + } +#endif + + u16 colorMask = ~RGB_LOW_BITS_MASK; + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + + int sPitch = srcPitch >> 1; + + int pos = 0; + for (int j = 0; j < height; j++) + for (int i = 0; i < sPitch; i++) + { + u16 color = src0[pos]; + src0[pos] = + (((color & colorMask) >> 1) + ((src1[pos] & colorMask) >> 1)); + src1[pos] = color; + pos++; + } +} + +#ifdef MMX +static void MotionBlurIB32_MMX(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = (u32 *)frm1; + + int count = width >> 1; + + for (int i = 0; i < height; i++) + { +#ifdef __GNUC__ + asm volatile ( + "push %2\n" + "movq 0(%3), %%mm7\n" // colorMask + "0:\n" + "movq 0(%0), %%mm0\n" // src0 + "movq 0(%1), %%mm1\n" // src1 + "movq %%mm0, 0(%1)\n" // src1 = src0 + "pand %%mm7, %%mm0\n" // color & colorMask + "pand %%mm7, %%mm1\n" // src1 & colorMask + "psrld $1, %%mm0\n" // (color & colorMask) >> 1 (E) + "psrld $1, %%mm1\n" // (src & colorMask) >> 1 (F) + "paddd %%mm1, %%mm0\n" // E+F + + "movq %%mm0, 0(%0)\n" // src0 = res + + "addl $8, %0\n" + "addl $8, %1\n" + + "decl %2\n" + "jnz 0b\n" + "pop %2\n" + "emms\n" + : "+r" (src0), "+r" (src1) + : "r" (count), "r" (qRGB_COLOR_MASK) + ); +#else + __asm { + movq mm7, qword ptr [qRGB_COLOR_MASK]; + mov eax, src0; + mov ebx, src1; + mov edi, count; +label0: + movq mm0, qword ptr [eax]; // src0 + movq mm1, qword ptr [ebx]; // src1 + movq qword ptr [ebx], mm0; // src1 = src0 + pand mm0, mm7; // color & colorMask + pand mm1, mm7; // src1 & colorMask + psrld mm0, 1; // (color & colorMask) >> 1 (E) + psrld mm1, 1; // (src & colorMask) >> 1 (F) + paddd mm0, mm1; // E+F + + movq qword ptr [eax], mm0; // src0 = res + + add eax, 8; + add ebx, 8; + + dec edi; + jnz label0; + mov src0, eax; + mov src1, ebx; + emms; + } +#endif + src0++; + src1++; + } +} + +#endif + +void MotionBlurIB32(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if (frm1 == NULL) + { + Init(); + } + +#ifdef MMX + if (cpu_mmx) + { + MotionBlurIB32_MMX(srcPtr, srcPitch, width, height); + return; + } +#endif + + u32 *src0 = (u32 *)srcPtr; + u32 *src1 = (u32 *)frm1; + + u32 colorMask = 0xfefefe; + + int sPitch = srcPitch >> 2; + int pos = 0; + + for (int j = 0; j < height; j++) + for (int i = 0; i < sPitch; i++) + { + u32 color = src0[pos]; + src0[pos] = (((color & colorMask) >> 1) + + ((src1[pos] & colorMask) >> 1)); + src1[pos] = color; + pos++; + } +} + +static int count = 0; + +void InterlaceIB(u8 *srcPtr, u32 srcPitch, int width, int height) +{ + if (frm1 == NULL) + { + Init(); + } + + u16 colorMask = ~RGB_LOW_BITS_MASK; + + u16 *src0 = (u16 *)srcPtr; + u16 *src1 = (u16 *)frm1; + + int sPitch = srcPitch >> 1; + + int pos = 0; + for (int j = 0; j < height; j++) + { + bool render = count ? (j & 1) != 0 : (j & 1) == 0; + if (render) + { + for (int i = 0; i < sPitch; i++) + { + u16 color = src0[pos]; + src0[pos] = + (((color & colorMask) >> 1) + ((((src1[pos] & colorMask) >> 1) & colorMask) >> 1)); + src1[pos] = color; + pos++; + } + } + else + { + for (int i = 0; i < sPitch; i++) + { + u16 color = src0[pos]; + src0[pos] = + (((((color & colorMask) >> 1) & colorMask) >> 1) + ((src1[pos] & colorMask) >> 1)); + src1[pos] = color; + pos++; + } + } + } + count = count ^ 1; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/interp.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/interp.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,351 @@ +/* + * This file is part of the Advance project. + * + * Copyright (C) 2003 Andrea Mazzoleni + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * In addition, as a special exception, Andrea Mazzoleni + * gives permission to link the code of this program with + * the MAME library (or with modified versions of MAME that use the + * same license as MAME), and distribute linked combinations including + * the two. You must obey the GNU General Public License in all + * respects for all of the code used other than MAME. If you modify + * this file, you may extend this exception to your version of the + * file, but you are not obligated to do so. If you do not wish to + * do so, delete this exception statement from your version. + */ + +#ifndef __INTERP_H +#define __INTERP_H + +/***************************************************************************/ +/* Basic types */ + +/***************************************************************************/ +/* interpolation */ + +extern unsigned interp_mask[2]; +extern unsigned interp_bits_per_pixel; + +#define INTERP_16_MASK_1(v) (v & interp_mask[0]) +#define INTERP_16_MASK_2(v) (v & interp_mask[1]) + +static inline u16 interp_16_521(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*5 + INTERP_16_MASK_1(p2)*2 + INTERP_16_MASK_1(p3)*1) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*5 + INTERP_16_MASK_2(p2)*2 + INTERP_16_MASK_2(p3)*1) / 8); +} + +static inline u16 interp_16_332(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*3 + INTERP_16_MASK_1(p2)*3 + INTERP_16_MASK_1(p3)*2) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*3 + INTERP_16_MASK_2(p2)*3 + INTERP_16_MASK_2(p3)*2) / 8); +} + +static inline u16 interp_16_611(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*6 + INTERP_16_MASK_1(p2) + INTERP_16_MASK_1(p3)) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*6 + INTERP_16_MASK_2(p2) + INTERP_16_MASK_2(p3)) / 8); +} + +static inline u16 interp_16_71(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*7 + INTERP_16_MASK_1(p2)) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*7 + INTERP_16_MASK_2(p2)) / 8); +} + +static inline u16 interp_16_211(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*2 + INTERP_16_MASK_1(p2) + INTERP_16_MASK_1(p3)) / 4) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*2 + INTERP_16_MASK_2(p2) + INTERP_16_MASK_2(p3)) / 4); +} + +static inline u16 interp_16_772(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1(((INTERP_16_MASK_1(p1) + INTERP_16_MASK_1(p2))*7 + INTERP_16_MASK_1(p3)*2) / 16) + | INTERP_16_MASK_2(((INTERP_16_MASK_2(p1) + INTERP_16_MASK_2(p2))*7 + INTERP_16_MASK_2(p3)*2) / 16); +} + +static inline u16 interp_16_11(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1) + INTERP_16_MASK_1(p2)) / 2) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1) + INTERP_16_MASK_2(p2)) / 2); +} + +static inline u16 interp_16_31(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*3 + INTERP_16_MASK_1(p2)) / 4) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*3 + INTERP_16_MASK_2(p2)) / 4); +} + +static inline u16 interp_16_1411(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*14 + INTERP_16_MASK_1(p2) + INTERP_16_MASK_1(p3)) / 16) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*14 + INTERP_16_MASK_2(p2) + INTERP_16_MASK_2(p3)) / 16); +} + +static inline u16 interp_16_431(u16 p1, u16 p2, u16 p3) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*4 + INTERP_16_MASK_1(p2)*3 + INTERP_16_MASK_1(p3)) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*4 + INTERP_16_MASK_2(p2)*3 + INTERP_16_MASK_2(p3)) / 8); +} + +static inline u16 interp_16_53(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*5 + INTERP_16_MASK_1(p2)*3) / 8) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*5 + INTERP_16_MASK_2(p2)*3) / 8); +} + +static inline u16 interp_16_151(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*15 + INTERP_16_MASK_1(p2)) / 16) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*15 + INTERP_16_MASK_2(p2)) / 16); +} + +static inline u16 interp_16_97(u16 p1, u16 p2) +{ + return INTERP_16_MASK_1((INTERP_16_MASK_1(p1)*9 + INTERP_16_MASK_1(p2)*7) / 16) + | INTERP_16_MASK_2((INTERP_16_MASK_2(p1)*9 + INTERP_16_MASK_2(p2)*7) / 16); +} + +#define INTERP_32_MASK_1(v) (v & 0xFF00FF) +#define INTERP_32_MASK_2(v) (v & 0x00FF00) + +static inline u32 interp_32_521(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*5 + INTERP_32_MASK_1(p2)*2 + INTERP_32_MASK_1(p3)*1) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*5 + INTERP_32_MASK_2(p2)*2 + INTERP_32_MASK_2(p3)*1) / 8); +} + +static inline u32 interp_32_332(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*3 + INTERP_32_MASK_1(p2)*3 + INTERP_32_MASK_1(p3)*2) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*3 + INTERP_32_MASK_2(p2)*3 + INTERP_32_MASK_2(p3)*2) / 8); +} + +static inline u32 interp_32_211(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*2 + INTERP_32_MASK_1(p2) + INTERP_32_MASK_1(p3)) / 4) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*2 + INTERP_32_MASK_2(p2) + INTERP_32_MASK_2(p3)) / 4); +} + +static inline u32 interp_32_611(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*6 + INTERP_32_MASK_1(p2) + INTERP_32_MASK_1(p3)) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*6 + INTERP_32_MASK_2(p2) + INTERP_32_MASK_2(p3)) / 8); +} + +static inline u32 interp_32_71(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*7 + INTERP_32_MASK_1(p2)) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*7 + INTERP_32_MASK_2(p2)) / 8); +} + +static inline u32 interp_32_772(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1(((INTERP_32_MASK_1(p1) + INTERP_32_MASK_1(p2))*7 + INTERP_32_MASK_1(p3)*2) / 16) + | INTERP_32_MASK_2(((INTERP_32_MASK_2(p1) + INTERP_32_MASK_2(p2))*7 + INTERP_32_MASK_2(p3)*2) / 16); +} + +static inline u32 interp_32_11(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1) + INTERP_32_MASK_1(p2)) / 2) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1) + INTERP_32_MASK_2(p2)) / 2); +} + +static inline u32 interp_32_31(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*3 + INTERP_32_MASK_1(p2)) / 4) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*3 + INTERP_32_MASK_2(p2)) / 4); +} + +static inline u32 interp_32_1411(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*14 + INTERP_32_MASK_1(p2) + INTERP_32_MASK_1(p3)) / 16) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*14 + INTERP_32_MASK_2(p2) + INTERP_32_MASK_2(p3)) / 16); +} + +static inline u32 interp_32_431(u32 p1, u32 p2, u32 p3) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*4 + INTERP_32_MASK_1(p2)*3 + INTERP_32_MASK_1(p3)) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*4 + INTERP_32_MASK_2(p2)*3 + INTERP_32_MASK_2(p3)) / 8); +} + +static inline u32 interp_32_53(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*5 + INTERP_32_MASK_1(p2)*3) / 8) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*5 + INTERP_32_MASK_2(p2)*3) / 8); +} + +static inline u32 interp_32_151(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*15 + INTERP_32_MASK_1(p2)) / 16) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*15 + INTERP_32_MASK_2(p2)) / 16); +} + +static inline u32 interp_32_97(u32 p1, u32 p2) +{ + return INTERP_32_MASK_1((INTERP_32_MASK_1(p1)*9 + INTERP_32_MASK_1(p2)*7) / 16) + | INTERP_32_MASK_2((INTERP_32_MASK_2(p1)*9 + INTERP_32_MASK_2(p2)*7) / 16); +} + +/***************************************************************************/ +/* diff */ + +#define INTERP_Y_LIMIT (0x30*4) +#define INTERP_U_LIMIT (0x07*4) +#define INTERP_V_LIMIT (0x06*8) + +static int interp_16_diff(u16 p1, u16 p2) +{ + int r, g, b; + int y, u, v; + + if (p1 == p2) + return 0; + + if (interp_bits_per_pixel == 16) { + b = (int)((p1 & 0x1F) - (p2 & 0x1F)) << 3; + g = (int)((p1 & 0x7E0) - (p2 & 0x7E0)) >> 3; + r = (int)((p1 & 0xF800) - (p2 & 0xF800)) >> 8; + } else { + b = (int)((p1 & 0x1F) - (p2 & 0x1F)) << 3; + g = (int)((p1 & 0x3E0) - (p2 & 0x3E0)) >> 2; + r = (int)((p1 & 0x7C00) - (p2 & 0x7C00)) >> 7; + } + + y = r + g + b; + u = r - b; + v = -r + 2*g - b; + + if (y < -INTERP_Y_LIMIT || y > INTERP_Y_LIMIT) + return 1; + + if (u < -INTERP_U_LIMIT || u > INTERP_U_LIMIT) + return 1; + + if (v < -INTERP_V_LIMIT || v > INTERP_V_LIMIT) + return 1; + +return 0; +} + +static int interp_32_diff(u32 p1, u32 p2) +{ + int r, g, b; + int y, u, v; + + if ((p1 & 0xF8F8F8) == (p2 & 0xF8F8F8)) + return 0; + + b = (int)((p1 & 0xFF) - (p2 & 0xFF)); + g = (int)((p1 & 0xFF00) - (p2 & 0xFF00)) >> 8; + r = (int)((p1 & 0xFF0000) - (p2 & 0xFF0000)) >> 16; + + y = r + g + b; + u = r - b; + v = -r + 2*g - b; + + if (y < -INTERP_Y_LIMIT || y > INTERP_Y_LIMIT) + return 1; + + if (u < -INTERP_U_LIMIT || u > INTERP_U_LIMIT) + return 1; + + if (v < -INTERP_V_LIMIT || v > INTERP_V_LIMIT) + return 1; + + return 0; +} + + +#define INTERP_LIMIT2 (96000) +#define ABS(x) ((x) < 0 ? -(x) : (x)) +#define MAX(x,y) ((x) > (y) ? (x) : (y)) +#define MIN(x,y) ((x) < (y) ? (x) : (y)) + +static int interp_16_diff2(u16 p1, u16 p2) +{ + int r, g, b; + int y, u, v; + + if ((p1 & 0xF79E) == (p2 & 0xF79E)) + return 0; + + if (interp_bits_per_pixel == 16) { + b = (int)((p1 & 0x1F) - (p2 & 0x1F)) << 3; + g = (int)((p1 & 0x7E0) - (p2 & 0x7E0)) >> 3; + r = (int)((p1 & 0xF800) - (p2 & 0xF800)) >> 8; + } else { + b = (int)((p1 & 0x1F) - (p2 & 0x1F)) << 3; + g = (int)((p1 & 0x3E0) - (p2 & 0x3E0)) >> 2; + r = (int)((p1 & 0x7C00) - (p2 & 0x7C00)) >> 7; + } + +// yb = 30*r + 58*g + 12*b; + y = 33*r + 36*g + 31*b; + u = -14*r - 29*g + 44*b; + v = 62*r - 51*g - 10*b; + + if (11*ABS(y) + 8*ABS(u) + 6*ABS(v) > INTERP_LIMIT2) + return 1; + return 0; +} + +static int interp_32_diff2(u32 p1, u32 p2) +{ + int r, g, b; + int y, u, v; + + if ((p1 & 0xF0F0F0) == (p2 & 0xF0F0F0)) + return 0; + + b = (int)((p1 & 0xF8) - (p2 & 0xF8)); + g = (int)((p1 & 0xF800) - (p2 & 0xF800)) >> 8; + r = (int)((p1 & 0xF80000) - (p2 & 0xF80000)) >> 16; + +// y = 30*r + 58*g + 12*b; + y = 33*r + 36*g + 31*b; + u = -14*r - 29*g + 44*b; + v = 62*r - 51*g - 10*b; + + if (11*ABS(y) + 8*ABS(u) + 6*ABS(v) > INTERP_LIMIT2) + return 1; + + return 0; +} + +static void interp_set(unsigned bits_per_pixel) +{ + interp_bits_per_pixel = bits_per_pixel; + + switch (bits_per_pixel) { + case 15 : + interp_mask[0] = 0x7C1F; + interp_mask[1] = 0x03E0; + break; + case 16 : + interp_mask[0] = 0xF81F; + interp_mask[1] = 0x07E0; + break; + case 32 : + interp_mask[0] = 0xFF00FF; + interp_mask[1] = 0x00FF00; + break; + } +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/lq2x.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/lq2x.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1284 @@ +case 0 : +case 2 : +case 4 : +case 6 : +case 8 : +case 12 : +case 16 : +case 20 : +case 24 : +case 28 : +case 32 : +case 34 : +case 36 : +case 38 : +case 40 : +case 44 : +case 48 : +case 52 : +case 56 : +case 60 : +case 64 : +case 66 : +case 68 : +case 70 : +case 96 : +case 98 : +case 100 : +case 102 : +case 128 : +case 130 : +case 132 : +case 134 : +case 136 : +case 140 : +case 144 : +case 148 : +case 152 : +case 156 : +case 160 : +case 162 : +case 164 : +case 166 : +case 168 : +case 172 : +case 176 : +case 180 : +case 184 : +case 188 : +case 192 : +case 194 : +case 196 : +case 198 : +case 224 : +case 226 : +case 228 : +case 230 : +{ + P0 = IC(0); + P1 = IC(0); + P2 = IC(0); + P3 = IC(0); +} break; +case 1 : +case 5 : +case 9 : +case 13 : +case 17 : +case 21 : +case 25 : +case 29 : +case 33 : +case 37 : +case 41 : +case 45 : +case 49 : +case 53 : +case 57 : +case 61 : +case 65 : +case 69 : +case 97 : +case 101 : +case 129 : +case 133 : +case 137 : +case 141 : +case 145 : +case 149 : +case 153 : +case 157 : +case 161 : +case 165 : +case 169 : +case 173 : +case 177 : +case 181 : +case 185 : +case 189 : +case 193 : +case 197 : +case 225 : +case 229 : +{ + P0 = IC(1); + P1 = IC(1); + P2 = IC(1); + P3 = IC(1); +} break; +case 3 : +case 35 : +case 67 : +case 99 : +case 131 : +case 163 : +case 195 : +case 227 : +{ + P0 = IC(2); + P1 = IC(2); + P2 = IC(2); + P3 = IC(2); +} break; +case 7 : +case 39 : +case 71 : +case 103 : +case 135 : +case 167 : +case 199 : +case 231 : +{ + P0 = IC(3); + P1 = IC(3); + P2 = IC(3); + P3 = IC(3); +} break; +case 10 : +case 138 : +{ + P1 = IC(0); + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I211(0, 1, 3); + } +} break; +case 11 : +case 27 : +case 75 : +case 139 : +case 155 : +case 203 : +{ + P1 = IC(2); + P2 = IC(2); + P3 = IC(2); + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 14 : +case 142 : +{ + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + P1 = IC(0); + } else { + P0 = I332(1, 3, 0); + P1 = I31(0, 1); + } +} break; +case 15 : +case 143 : +case 207 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + P1 = IC(4); + } else { + P0 = I332(1, 3, 4); + P1 = I31(4, 1); + } +} break; +case 18 : +case 22 : +case 30 : +case 50 : +case 54 : +case 62 : +case 86 : +case 118 : +{ + P0 = IC(0); + P2 = IC(0); + P3 = IC(0); + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 19 : +case 51 : +{ + P2 = IC(2); + P3 = IC(2); + if (MUR) { + P0 = IC(2); + P1 = IC(2); + } else { + P0 = I31(2, 1); + P1 = I332(1, 5, 2); + } +} break; +case 23 : +case 55 : +case 119 : +{ + P2 = IC(3); + P3 = IC(3); + if (MUR) { + P0 = IC(3); + P1 = IC(3); + } else { + P0 = I31(3, 1); + P1 = I332(1, 5, 3); + } +} break; +case 26 : +{ + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I211(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 31 : +case 95 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 42 : +case 170 : +{ + P1 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + P2 = IC(0); + } else { + P0 = I332(1, 3, 0); + P2 = I31(0, 3); + } +} break; +case 43 : +case 171 : +case 187 : +{ + P1 = IC(2); + P3 = IC(2); + if (MUL) { + P0 = IC(2); + P2 = IC(2); + } else { + P0 = I332(1, 3, 2); + P2 = I31(2, 3); + } +} break; +case 46 : +case 174 : +{ + P1 = IC(0); + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } +} break; +case 47 : +case 175 : +{ + P1 = IC(4); + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 58 : +case 154 : +case 186 : +{ + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 59 : +{ + P2 = IC(2); + P3 = IC(2); + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } + if (MUR) { + P1 = IC(2); + } else { + P1 = I611(2, 1, 5); + } +} break; +case 63 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 72 : +case 76 : +case 104 : +case 106 : +case 108 : +case 110 : +case 120 : +case 124 : +{ + P0 = IC(0); + P1 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } +} break; +case 73 : +case 77 : +case 105 : +case 109 : +case 125 : +{ + P1 = IC(1); + P3 = IC(1); + if (MDL) { + P0 = IC(1); + P2 = IC(1); + } else { + P0 = I31(1, 3); + P2 = I332(3, 7, 1); + } +} break; +case 74 : +{ + P1 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I211(0, 1, 3); + } +} break; +case 78 : +case 202 : +case 206 : +{ + P1 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } +} break; +case 79 : +{ + P1 = IC(4); + P3 = IC(4); + if (MDL) { + P2 = IC(4); + } else { + P2 = I611(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } +} break; +case 80 : +case 208 : +case 210 : +case 216 : +{ + P0 = IC(0); + P1 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } +} break; +case 81 : +case 209 : +case 217 : +{ + P0 = IC(1); + P1 = IC(1); + P2 = IC(1); + if (MDR) { + P3 = IC(1); + } else { + P3 = I211(1, 5, 7); + } +} break; +case 82 : +case 214 : +case 222 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 83 : +case 115 : +{ + P0 = IC(2); + P2 = IC(2); + if (MDR) { + P3 = IC(2); + } else { + P3 = I611(2, 5, 7); + } + if (MUR) { + P1 = IC(2); + } else { + P1 = I611(2, 1, 5); + } +} break; +case 84 : +case 212 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P1 = IC(0); + P3 = IC(0); + } else { + P1 = I31(0, 5); + P3 = I332(5, 7, 0); + } +} break; +case 85 : +case 213 : +case 221 : +{ + P0 = IC(1); + P2 = IC(1); + if (MDR) { + P1 = IC(1); + P3 = IC(1); + } else { + P1 = I31(1, 5); + P3 = I332(5, 7, 1); + } +} break; +case 87 : +{ + P0 = IC(3); + P2 = IC(3); + if (MDR) { + P3 = IC(3); + } else { + P3 = I611(3, 5, 7); + } + if (MUR) { + P1 = IC(3); + } else { + P1 = I211(3, 1, 5); + } +} break; +case 88 : +case 248 : +case 250 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } +} break; +case 89 : +case 93 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I611(1, 3, 7); + } + if (MDR) { + P3 = IC(1); + } else { + P3 = I611(1, 5, 7); + } +} break; +case 90 : +{ + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 91 : +{ + if (MDL) { + P2 = IC(2); + } else { + P2 = I611(2, 3, 7); + } + if (MDR) { + P3 = IC(2); + } else { + P3 = I611(2, 5, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } + if (MUR) { + P1 = IC(2); + } else { + P1 = I611(2, 1, 5); + } +} break; +case 92 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } +} break; +case 94 : +{ + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 107 : +case 123 : +{ + P1 = IC(2); + P3 = IC(2); + if (MDL) { + P2 = IC(2); + } else { + P2 = I211(2, 3, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 111 : +{ + P1 = IC(4); + P3 = IC(4); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 112 : +case 240 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDR) { + P2 = IC(0); + P3 = IC(0); + } else { + P2 = I31(0, 7); + P3 = I332(5, 7, 0); + } +} break; +case 113 : +case 241 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDR) { + P2 = IC(1); + P3 = IC(1); + } else { + P2 = I31(1, 7); + P3 = I332(5, 7, 1); + } +} break; +case 114 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 116 : +{ + P0 = IC(0); + P1 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } +} break; +case 117 : +{ + P0 = IC(1); + P1 = IC(1); + P2 = IC(1); + if (MDR) { + P3 = IC(1); + } else { + P3 = I611(1, 5, 7); + } +} break; +case 121 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I211(1, 3, 7); + } + if (MDR) { + P3 = IC(1); + } else { + P3 = I611(1, 5, 7); + } +} break; +case 122 : +{ + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I611(0, 5, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 126 : +{ + P0 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 127 : +{ + P3 = IC(4); + if (MDL) { + P2 = IC(4); + } else { + P2 = I211(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I211(4, 1, 5); + } +} break; +case 146 : +case 150 : +case 178 : +case 182 : +case 190 : +{ + P0 = IC(0); + P2 = IC(0); + if (MUR) { + P1 = IC(0); + P3 = IC(0); + } else { + P1 = I332(1, 5, 0); + P3 = I31(0, 5); + } +} break; +case 147 : +case 179 : +{ + P0 = IC(2); + P2 = IC(2); + P3 = IC(2); + if (MUR) { + P1 = IC(2); + } else { + P1 = I611(2, 1, 5); + } +} break; +case 151 : +case 183 : +{ + P0 = IC(3); + P2 = IC(3); + P3 = IC(3); + if (MUR) { + P1 = IC(3); + } else { + P1 = I1411(3, 1, 5); + } +} break; +case 158 : +{ + P2 = IC(0); + P3 = IC(0); + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 159 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 191 : +{ + P2 = IC(4); + P3 = IC(4); + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 200 : +case 204 : +case 232 : +case 236 : +case 238 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + P3 = IC(0); + } else { + P2 = I332(3, 7, 0); + P3 = I31(0, 7); + } +} break; +case 201 : +case 205 : +{ + P0 = IC(1); + P1 = IC(1); + P3 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I611(1, 3, 7); + } +} break; +case 211 : +{ + P0 = IC(2); + P1 = IC(2); + P2 = IC(2); + if (MDR) { + P3 = IC(2); + } else { + P3 = I211(2, 5, 7); + } +} break; +case 215 : +{ + P0 = IC(3); + P2 = IC(3); + if (MDR) { + P3 = IC(3); + } else { + P3 = I211(3, 5, 7); + } + if (MUR) { + P1 = IC(3); + } else { + P1 = I1411(3, 1, 5); + } +} break; +case 218 : +{ + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 219 : +{ + P1 = IC(2); + P2 = IC(2); + if (MDR) { + P3 = IC(2); + } else { + P3 = I211(2, 5, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 220 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I611(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } +} break; +case 223 : +{ + P2 = IC(4); + if (MDR) { + P3 = IC(4); + } else { + P3 = I211(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I211(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; +case 233 : +case 237 : +{ + P0 = IC(1); + P1 = IC(1); + P3 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I1411(1, 3, 7); + } +} break; +case 234 : +{ + P1 = IC(0); + P3 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MUL) { + P0 = IC(0); + } else { + P0 = I611(0, 1, 3); + } +} break; +case 235 : +{ + P1 = IC(2); + P3 = IC(2); + if (MDL) { + P2 = IC(2); + } else { + P2 = I1411(2, 3, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 239 : +{ + P1 = IC(4); + P3 = IC(4); + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } +} break; +case 242 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I211(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I611(0, 1, 5); + } +} break; +case 243 : +{ + P0 = IC(2); + P1 = IC(2); + if (MDR) { + P2 = IC(2); + P3 = IC(2); + } else { + P2 = I31(2, 7); + P3 = I332(5, 7, 2); + } +} break; +case 244 : +{ + P0 = IC(0); + P1 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I1411(0, 5, 7); + } +} break; +case 245 : +{ + P0 = IC(1); + P1 = IC(1); + P2 = IC(1); + if (MDR) { + P3 = IC(1); + } else { + P3 = I1411(1, 5, 7); + } +} break; +case 246 : +{ + P0 = IC(0); + P2 = IC(0); + if (MDR) { + P3 = IC(0); + } else { + P3 = I1411(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 247 : +{ + P0 = IC(3); + P2 = IC(3); + if (MDR) { + P3 = IC(3); + } else { + P3 = I1411(3, 5, 7); + } + if (MUR) { + P1 = IC(3); + } else { + P1 = I1411(3, 1, 5); + } +} break; +case 249 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I1411(1, 3, 7); + } + if (MDR) { + P3 = IC(1); + } else { + P3 = I211(1, 5, 7); + } +} break; +case 251 : +{ + P1 = IC(2); + if (MDL) { + P2 = IC(2); + } else { + P2 = I1411(2, 3, 7); + } + if (MDR) { + P3 = IC(2); + } else { + P3 = I211(2, 5, 7); + } + if (MUL) { + P0 = IC(2); + } else { + P0 = I211(2, 1, 3); + } +} break; +case 252 : +{ + P0 = IC(0); + P1 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I1411(0, 5, 7); + } +} break; +case 253 : +{ + P0 = IC(1); + P1 = IC(1); + if (MDL) { + P2 = IC(1); + } else { + P2 = I1411(1, 3, 7); + } + if (MDR) { + P3 = IC(1); + } else { + P3 = I1411(1, 5, 7); + } +} break; +case 254 : +{ + P0 = IC(0); + if (MDL) { + P2 = IC(0); + } else { + P2 = I211(0, 3, 7); + } + if (MDR) { + P3 = IC(0); + } else { + P3 = I1411(0, 5, 7); + } + if (MUR) { + P1 = IC(0); + } else { + P1 = I211(0, 1, 5); + } +} break; +case 255 : +{ + if (MDL) { + P2 = IC(4); + } else { + P2 = I1411(4, 3, 7); + } + if (MDR) { + P3 = IC(4); + } else { + P3 = I1411(4, 5, 7); + } + if (MUL) { + P0 = IC(4); + } else { + P0 = I1411(4, 1, 3); + } + if (MUR) { + P1 = IC(4); + } else { + P1 = I1411(4, 1, 5); + } +} break; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/motionblur.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/motionblur.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,183 @@ +#include "../Port.h" + +extern u32 RGB_LOW_BITS_MASK; + +void MotionBlur(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~(RGB_LOW_BITS_MASK | (RGB_LOW_BITS_MASK << 16)); + u32 lowPixelMask = RGB_LOW_BITS_MASK; + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *xP = (u32 *) deltaPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + u32 currentDelta; + u32 nextDelta; + + finish = (u8 *) bP + ((width + 2) << 1); + nextPixel = *bP++; + nextDelta = *xP++; + + do + { + currentPixel = nextPixel; + currentDelta = nextDelta; + nextPixel = *bP++; + nextDelta = *xP++; + + if (currentPixel != currentDelta) + { + u32 colorA, product, colorB; + + *(xP - 2) = currentPixel; +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; + colorB = currentDelta >> 16; +#else + colorA = currentPixel & 0xffff; + colorB = currentDelta & 0xffff; +#endif + + product = ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1) + + (colorA & colorB & lowPixelMask))); + + *(dP) = product | product << 16; + *(nL) = product | product << 16; + +#ifdef WORDS_BIGENDIAN + colorA = (currentPixel & 0xffff); + colorB = (currentDelta & 0xffff); +#else + colorA = currentPixel >> 16; + colorB = currentDelta >> 16; +#endif + product = ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1) + + (colorA & colorB & lowPixelMask))); + + *(dP + 1) = product | product << 16; + *(nL + 1) = product | product << 16; + } + else + { + u32 colorA, product; + + *(xP - 2) = currentPixel; +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; +#else + colorA = currentPixel & 0xffff; +#endif + + product = colorA; + + *(dP) = product | product << 16; + *(nL) = product | product << 16; +#ifdef WORDS_BIGENDIAN + colorA = (currentPixel & 0xffff); +#else + colorA = currentPixel >> 16; +#endif + product = colorA; + + *(dP + 1) = product | product << 16; + *(nL + 1) = product | product << 16; + } + + dP += 2; + nL += 2; + } + while ((u8 *) bP < finish); + + deltaPtr += srcPitch; + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void MotionBlur32(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~RGB_LOW_BITS_MASK; + u32 lowPixelMask = RGB_LOW_BITS_MASK; + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *xP = (u32 *) deltaPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + u32 currentDelta; + u32 nextDelta; + + finish = (u8 *) bP + ((width + 1) << 2); + nextPixel = *bP++; + nextDelta = *xP++; + + do + { + currentPixel = nextPixel; + currentDelta = nextDelta; + nextPixel = *bP++; + nextDelta = *xP++; + + u32 colorA, product, colorB; + + *(xP - 2) = currentPixel; + colorA = currentPixel; + colorB = currentDelta; + + product = ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1) + + (colorA & colorB & lowPixelMask))); + + *(dP) = product; + *(dP + 1) = product; + *(nL) = product; + *(nL + 1) = product; + + *(xP - 1) = nextPixel; + + colorA = nextPixel; + colorB = nextDelta; + + product = ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1) + + (colorA & colorB & lowPixelMask))); + + *(dP + 2) = product; + *(dP + 3) = product; + *(nL + 2) = product; + *(nL + 3) = product; + + nextPixel = *bP++; + nextDelta = *xP++; + + dP += 4; + nL += 4; + } + while ((u8 *) bP < finish); + + deltaPtr += srcPitch; + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/pixel.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/pixel.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,196 @@ +#include "../Port.h" + +extern u32 RGB_LOW_BITS_MASK; + +void Pixelate2x16(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~(RGB_LOW_BITS_MASK | (RGB_LOW_BITS_MASK << 16)); + colorMask = (colorMask >> 2) & (colorMask >> 1); + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *xP = (u32 *) deltaPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + u32 currentDelta; + u32 nextDelta; + + finish = (u8 *) bP + ((width+2) << 1); + nextPixel = *bP++; + nextDelta = *xP++; + + do + { + currentPixel = nextPixel; + currentDelta = nextDelta; + nextPixel = *bP++; + nextDelta = *xP++; + + if ((nextPixel != nextDelta) || (currentPixel != currentDelta)) + { + u32 colorA, colorB, product; + + *(xP - 2) = currentPixel; +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; + colorB = currentPixel & 0xffff; +#else + colorA = currentPixel & 0xffff; + colorB = currentPixel >> 16; +#endif + product = (colorA >> 2) & colorMask; + +#ifdef WORDS_BIGENDIAN + *(nL) = (product << 16) | (product); + *(dP) = (colorA << 16) | product; +#else + *(nL) = product | (product << 16); + *(dP) = colorA | (product << 16); +#endif + +#ifdef WORDS_BIGENDIAN + colorA = nextPixel >> 16; +#else + colorA = nextPixel & 0xffff; +#endif + product = (colorB >> 2) & colorMask; +#ifdef WORDS_BIGENDIAN + *(nL + 1) = (product << 16) | (product); + *(dP + 1) = (colorB << 16) | (product); +#else + *(nL + 1) = (product) | (product << 16); + *(dP + 1) = (colorB) | (product << 16); +#endif + } + + dP += 2; + nL += 2; + } + while ((u8 *) bP < finish); + + deltaPtr += srcPitch; + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void Pixelate2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ((u32)~RGB_LOW_BITS_MASK >> 2) & ((u32)~RGB_LOW_BITS_MASK >> 1); + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + // u32 *xP = (u32 *) deltaPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width+1) << 2); + nextPixel = *bP++; + + do + { + u32 product; + + currentPixel = nextPixel; + nextPixel = *bP++; + product = (currentPixel >> 2) & colorMask; + *(nL) = product; + *(nL+1) = product; + *(dP) = currentPixel; + *(dP+1) = product; + + currentPixel = nextPixel; + nextPixel = *bP++; + product = (currentPixel >> 2) & colorMask; + *(nL + 2) = product; + *(nL + 3) = product; + *(dP + 2) = currentPixel; + *(dP + 3) = product; + + dP += 4; + nL += 4; + } + while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +// generic Pixelate Nx magnification filter +template +void PixelateNx(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + ColorType colorMask = ((ColorType)~RGB_LOW_BITS_MASK >> 2) & ((ColorType)~RGB_LOW_BITS_MASK >> 1); + + srcPitch = srcPitch / sizeof(ColorType) - width; + u32 dstNextP = dstPitch / sizeof(ColorType); + u32 dstNextL = (dstNextP - width) * magnification; // skip to the next magnificated 'line' + dstNextP -= magnification; + + u32 offset = (dstPitch + sizeof(ColorType)) * magnification - dstPitch; + + ColorType *src = (ColorType *)srcPtr; + ColorType *dst = (ColorType *)dstPtr; + + do // per src line + { + u8 *finishP = (u8 *)dst + offset; + for (int x = 0; x < width; ++x) // per pixel in line + { + ColorType col = *src; + ColorType *dst2 = dst; + u8 *finishM = (u8 *)(dst + magnification); + + ColorType product = (col >> 2) & colorMask; + do + { + *dst2 = product; + } while ((u8 *)++dst2 < finishM); + dst2 += dstNextP; + finishM += dstPitch; + do // dst magnificated pixel + { + *dst2++ = product; + do + { + *dst2 = col; + } while ((u8 *)++dst2 < finishM); + dst2 += dstNextP; + finishM += dstPitch; + } while ((u8 *)dst2 < finishP); + + ++src; + dst += magnification; + finishP += magnification * sizeof(ColorType); + } + src += srcPitch; + dst += dstNextL; + } while (--height); +} + +typedef void (*PixelateNxFP)(u8*, u32, u8*, u8*, u32, int, int); + +PixelateNxFP Pixelate3x16 = PixelateNx<3, u16>; +PixelateNxFP Pixelate3x32 = PixelateNx<3, u32>; +PixelateNxFP Pixelate4x16 = PixelateNx<4, u16>; +PixelateNxFP Pixelate4x32 = PixelateNx<4, u32>; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/scanline.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/scanline.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,225 @@ +#include "../Port.h" + +extern u32 RGB_LOW_BITS_MASK; + +void Scanlines(u8 *srcPtr, u32 srcPitch, u8 *, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width + 2) << 1); + nextPixel = *bP++; + + do + { + currentPixel = nextPixel; + nextPixel = *bP++; + u32 colorA, colorB; + +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; + colorB = currentPixel & 0xffff; +#else + colorA = currentPixel & 0xffff; + colorB = currentPixel >> 16; +#endif + + *(dP) = colorA | colorA << 16; + *(nL) = 0; + +#ifdef WORDS_BIGENDIAN + colorA = nextPixel >> 16; +#else + colorA = nextPixel & 0xffff; +#endif + + *(dP + 1) = colorB | (colorB << 16); + *(nL + 1) = 0; + + dP += 2; + nL += 2; + } + while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void Scanlines32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width + 1) << 2); + nextPixel = *bP++; + + do + { + currentPixel = nextPixel; + nextPixel = *bP++; + + u32 colorA, colorB; + + colorA = currentPixel; + colorB = nextPixel; + + *(dP) = colorA; + *(dP + 1) = colorA; + *(nL) = 0; + *(nL + 1) = 0; + + *(dP + 2) = colorB; + *(dP + 3) = colorB; + *(nL + 2) = 0; + *(nL + 3) = 0; + + nextPixel = *bP++; + + dP += 4; + nL += 4; + } + while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void ScanlinesTV(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~(RGB_LOW_BITS_MASK | (RGB_LOW_BITS_MASK << 16)); + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width + 2) << 1); + nextPixel = *bP++; + + do + { + currentPixel = nextPixel; + nextPixel = *bP++; + + u32 colorA, colorB; + +#ifdef WORDS_BIGENDIAN + colorA = currentPixel >> 16; + colorB = currentPixel & 0xFFFF; +#else + colorA = currentPixel & 0xFFFF; + colorB = currentPixel >> 16; +#endif + + *(dP) = colorA = colorA | ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1))) << 16; + colorA = ((colorA & colorMask) >> 1); + colorA += ((colorA & colorMask) >> 1); + *(nL) = colorA; + + colorA = nextPixel & 0xFFFF; + + *(dP + 1) = colorB = colorB | ((((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1))) << 16; + colorB = ((colorB & colorMask) >> 1); + colorB += ((colorB & colorMask) >> 1); + + *(nL + 1) = colorB; + + dP += 2; + nL += 2; + } + while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void ScanlinesTV32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + u32 colorMask = ~RGB_LOW_BITS_MASK; + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + u32 nextPixel; + + finish = (u8 *) bP + ((width + 1) << 2); + nextPixel = *bP++; + + do + { + currentPixel = nextPixel; + nextPixel = *bP++; + + u32 colorA, colorB, temp; + + colorA = currentPixel; + colorB = nextPixel; + + *(dP) = colorA; + *(dP + 1) = temp = ((colorA & colorMask) >> 1) + + ((colorB & colorMask) >> 1); + temp = ((temp & colorMask) >> 1); + temp += ((temp & colorMask) >> 1); + colorA = ((colorA & colorMask) >> 1); + colorA += ((colorA & colorMask) >> 1); + + *(nL) = colorA; + *(nL + 1) = temp; + + dP += 2; + nL += 2; + } + while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/filters/simple2x.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/filters/simple2x.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,189 @@ +#include "../Port.h" + +void Simple2x16(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + + finish = (u8 *) bP + ((width + 2) << 1); + currentPixel = *bP++; + + do + { +#ifdef WORDS_BIGENDIAN + u32 color = currentPixel >> 16; +#else + u32 color = currentPixel & 0xffff; +#endif + + color = color | (color << 16); + + *(dP) = color; + *(nL) = color; + +#ifdef WORDS_BIGENDIAN + color = currentPixel & 0xffff; +#else + color = currentPixel >> 16; +#endif + color = color | (color << 16); + *(dP + 1) = color; + *(nL + 1) = color; + + currentPixel = *bP++; + + dP += 2; + nL += 2; + } + while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +void Simple2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + u8 *nextLine, *finish; + + nextLine = dstPtr + dstPitch; + + do + { + u32 *bP = (u32 *) srcPtr; + u32 *dP = (u32 *) dstPtr; + u32 *nL = (u32 *) nextLine; + u32 currentPixel; + + finish = (u8 *) bP + ((width + 1) << 2); + currentPixel = *bP++; + + do + { + u32 color = currentPixel; + + *(dP) = color; + *(dP + 1) = color; + *(nL) = color; + *(nL + 1) = color; + + currentPixel = *bP++; + + dP += 2; + nL += 2; + } + while ((u8 *) bP < finish); + + srcPtr += srcPitch; + dstPtr += dstPitch << 1; + nextLine += dstPitch << 1; + } + while (--height); +} + +#if 0 +// generic Simple Nx magnification filter +template +void SimpleNx(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + srcPitch = srcPitch / sizeof(ColorType) - width; + u32 dstNextP = dstPitch / sizeof(ColorType); + u32 dstNextL = (dstNextP - width) * magnification; // skip to the next magnificated 'line' + dstNextP -= magnification; + + u32 offset = (dstPitch + sizeof(ColorType)) * magnification - dstPitch; + + ColorType *src = (ColorType *)srcPtr; + ColorType *dst = (ColorType *)dstPtr; + + do // per src line + { + u8 *finishP = (u8 *)dst + offset; + for (int x = 0; x < width; ++x) // per pixel in line + { + ColorType col = *src; + ColorType *dst2 = dst; + u8 * finishM = (u8 *)(dst + magnification); + do // dst magnificated pixel + { + do + { + *dst2 = col; + } + while ((u8 *)++dst2 < finishM); + dst2 += dstNextP; + finishM += dstPitch; + } + while ((u8 *)dst2 < finishP); + + ++src; + dst += magnification; + finishP += magnification * sizeof(ColorType); + } + src += srcPitch; + dst += dstNextL; + } + while (--height); +} + +#else + +// generic Simple Nx magnification filter +template +void SimpleNx(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, + u8 *dstPtr, u32 dstPitch, int width, int height) +{ + srcPitch = srcPitch / sizeof(ColorType) - width; + dstPitch /= sizeof(ColorType); + u32 dstBlank = (dstPitch - width) * magnification; // skip to the next magnificated 'line' + dstPitch -= magnification; + + ColorType *src = (ColorType *)srcPtr; + ColorType *dst = (ColorType *)dstPtr; + + do // per src line + { + for (int x = 0; x < width; ++x) // per pixel in src line + { + ColorType col = *src; + ColorType *dst2 = dst; + for (int dy = 0; dy < magnification; ++dy) // dst magnificated pixel + { + for (int dx = 0; dx < magnification; ++dx) + { + *dst2 = col; + ++dst2; + } + dst2 += dstPitch; + } + + ++src; + dst += magnification; + } + src += srcPitch; + dst += dstBlank; + } + while (--height); +} + +#endif + +typedef void (*SimpleNxFP)(u8 *, u32, u8 *, u8 *, u32, int, int); + +SimpleNxFP Simple3x16 = SimpleNx<3, u16>; +SimpleNxFP Simple3x32 = SimpleNx<3, u32>; +SimpleNxFP Simple4x16 = SimpleNx<4, u16>; +SimpleNxFP Simple4x32 = SimpleNx<4, u32>; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/GB.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/GB.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,3918 @@ +#include +#include +#include +#include + +#include "../Port.h" +#include "../NLS.h" +#include "GB.h" +#include "gbCheats.h" +#include "gbGlobals.h" +#include "gbMemory.h" +#include "gbSGB.h" +#include "gbSound.h" +#include "../common/unzip.h" +#include "../common/Util.h" +#include "../common/System.h" +#include "../common/movie.h" +#include "../common/vbalua.h" + +#ifdef __GNUC__ +#define _stricmp strcasecmp +#endif + +// FIXME: constant (GB) or boolean (GBA)?! +#define C_FLAG 0x10 +#define H_FLAG 0x20 +#define N_FLAG 0x40 +#define Z_FLAG 0x80 +extern soundtick_t GB_USE_TICKS_AS; + +u8 * origPix = NULL; +extern u8 * pix; +extern u32 extButtons; +extern bool8 capturePrevious; +extern int32 captureNumber; +extern bool8 speedup; + +bool gbUpdateSizes(); + +// debugging +bool memorydebug = false; +char gbBuffer[2048]; + +extern u16 gbLineMix[160]; + +// mappers +void (*mapper)(u16, u8) = NULL; +void (*mapperRAM)(u16, u8) = NULL; +u8 (*mapperReadRAM)(u16) = NULL; + +// registers +gbRegister PC; +gbRegister SP; +gbRegister AF; +gbRegister BC; +gbRegister DE; +gbRegister HL; +u16 IFF; +// 0xff04 +u8 register_DIV = 0; +// 0xff05 +u8 register_TIMA = 0; +// 0xff06 +u8 register_TMA = 0; +// 0xff07 +u8 register_TAC = 0; +// 0xff0f +u8 register_IF = 0; +// 0xff40 +u8 register_LCDC = 0; +// 0xff41 +u8 register_STAT = 0; +// 0xff42 +u8 register_SCY = 0; +// 0xff43 +u8 register_SCX = 0; +// 0xff44 +u8 register_LY = 0; +// 0xff45 +u8 register_LYC = 0; +// 0xff46 +u8 register_DMA = 0; +// 0xff4a +u8 register_WY = 0; +// 0xff4b +u8 register_WX = 0; +// 0xff4f +u8 register_VBK = 0; +// 0xff51 +u8 register_HDMA1 = 0; +// 0xff52 +u8 register_HDMA2 = 0; +// 0xff53 +u8 register_HDMA3 = 0; +// 0xff54 +u8 register_HDMA4 = 0; +// 0xff55 +u8 register_HDMA5 = 0; +// 0xff70 +u8 register_SVBK = 0; +// 0xffff +u8 register_IE = 0; + +// ticks definition +int32 GBDIV_CLOCK_TICKS = 64; +int32 GBLCD_MODE_0_CLOCK_TICKS = 51; +int32 GBLCD_MODE_1_CLOCK_TICKS = 1140; +int32 GBLCD_MODE_2_CLOCK_TICKS = 20; +int32 GBLCD_MODE_3_CLOCK_TICKS = 43; +int32 GBLY_INCREMENT_CLOCK_TICKS = 114; +int32 GBTIMER_MODE_0_CLOCK_TICKS = 256; +int32 GBTIMER_MODE_1_CLOCK_TICKS = 4; +int32 GBTIMER_MODE_2_CLOCK_TICKS = 16; +int32 GBTIMER_MODE_3_CLOCK_TICKS = 64; +int32 GBSERIAL_CLOCK_TICKS = 128; +int32 GBSYNCHRONIZE_CLOCK_TICKS = 52920; + +// state variables + +// interrupt +int32 gbInterrupt = 0; +int32 gbInterruptWait = 0; +// serial +int32 gbSerialOn = 0; +int32 gbSerialTicks = 0; +int32 gbSerialBits = 0; +// timer +int32 gbTimerOn = 0; +int32 gbTimerTicks = 0; +int32 gbTimerClockTicks = 0; +int32 gbTimerMode = 0; +// lcd +int32 gbLcdMode = 2; +int32 gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS; +int32 gbLcdLYIncrementTicks = 0; +// div +int32 gbDivTicks = GBDIV_CLOCK_TICKS; +// cgb +int32 gbVramBank = 0; +int32 gbWramBank = 1; +int32 gbHdmaSource = 0x0000; +int32 gbHdmaDestination = 0x8000; +int32 gbHdmaBytes = 0x0000; +int32 gbHdmaOn = 0; +int32 gbSpeed = 0; +// frame counting +int32 gbFrameCount = 0; +int32 gbFrameSkip = 0; +int32 gbFrameSkipCount = 0; +// timing +u32 gbLastTime = 0; +u32 gbElapsedTime = 0; +u32 gbTimeNow = 0; +int32 gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS; +int32 gbDMASpeedVersion = 1; +// emulator features +int32 gbBattery = 0; +int32 gbJoymask[4] = { 0, 0, 0, 0 }; + +int32 gbEchoRAMFixOn = 1; + +static bool newFrame = true; +static bool pauseAfterFrameAdvance = false; + +int32 gbRomSizes[] = { 0x00008000, // 32K + 0x00010000, // 64K + 0x00020000, // 128K + 0x00040000, // 256K + 0x00080000, // 512K + 0x00100000, // 1024K + 0x00200000, // 2048K + 0x00400000, // 4096K + 0x00800000 // 8192K +}; +int32 gbRomSizesMasks[] = { 0x00007fff, + 0x0000ffff, + 0x0001ffff, + 0x0003ffff, + 0x0007ffff, + 0x000fffff, + 0x001fffff, + 0x003fffff, + 0x007fffff }; + +int32 gbRamSizes[6] = { 0x00000000, // 0K + 0x00000800, // 2K + 0x00002000, // 8K + 0x00008000, // 32K + 0x00020000, // 128K + 0x00010000 // 64K +}; + +int32 gbRamSizesMasks[6] = { 0x00000000, + 0x000007ff, + 0x00001fff, + 0x00007fff, + 0x0001ffff, + 0x0000ffff }; + +int32 gbCycles[] = +{ +// 0 1 2 3 4 5 6 7 8 9 a b c d e f + 1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, // 0 + 1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, // 1 + 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, // 2 + 2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, // 3 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 4 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 5 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 6 + 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, // 7 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 8 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // 9 + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // a + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, // b + 2, 3, 3, 4, 3, 4, 2, 4, 2, 4, 3, 2, 3, 6, 2, 4, // c + 2, 3, 3, 0, 3, 4, 2, 4, 2, 4, 3, 0, 3, 0, 2, 4, // d + 3, 3, 2, 0, 0, 4, 2, 4, 4, 1, 4, 0, 0, 0, 2, 4, // e + 3, 3, 2, 1, 0, 4, 2, 4, 3, 2, 4, 1, 0, 0, 2, 4 // f +}; + +int32 gbCyclesCB[] = +{ +// 0 1 2 3 4 5 6 7 8 9 a b c d e f + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0 + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 1 + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 2 + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 3 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 4 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 5 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 6 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 7 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 8 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // 9 + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // a + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // b + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // c + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // d + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, // e + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2 // f +}; + +u16 DAATable[] = +{ + 0x0080, 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, + 0x0800, 0x0900, 0x1020, 0x1120, 0x1220, 0x1320, 0x1420, 0x1520, + 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500, 0x1600, 0x1700, + 0x1800, 0x1900, 0x2020, 0x2120, 0x2220, 0x2320, 0x2420, 0x2520, + 0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2600, 0x2700, + 0x2800, 0x2900, 0x3020, 0x3120, 0x3220, 0x3320, 0x3420, 0x3520, + 0x3000, 0x3100, 0x3200, 0x3300, 0x3400, 0x3500, 0x3600, 0x3700, + 0x3800, 0x3900, 0x4020, 0x4120, 0x4220, 0x4320, 0x4420, 0x4520, + 0x4000, 0x4100, 0x4200, 0x4300, 0x4400, 0x4500, 0x4600, 0x4700, + 0x4800, 0x4900, 0x5020, 0x5120, 0x5220, 0x5320, 0x5420, 0x5520, + 0x5000, 0x5100, 0x5200, 0x5300, 0x5400, 0x5500, 0x5600, 0x5700, + 0x5800, 0x5900, 0x6020, 0x6120, 0x6220, 0x6320, 0x6420, 0x6520, + 0x6000, 0x6100, 0x6200, 0x6300, 0x6400, 0x6500, 0x6600, 0x6700, + 0x6800, 0x6900, 0x7020, 0x7120, 0x7220, 0x7320, 0x7420, 0x7520, + 0x7000, 0x7100, 0x7200, 0x7300, 0x7400, 0x7500, 0x7600, 0x7700, + 0x7800, 0x7900, 0x8020, 0x8120, 0x8220, 0x8320, 0x8420, 0x8520, + 0x8000, 0x8100, 0x8200, 0x8300, 0x8400, 0x8500, 0x8600, 0x8700, + 0x8800, 0x8900, 0x9020, 0x9120, 0x9220, 0x9320, 0x9420, 0x9520, + 0x9000, 0x9100, 0x9200, 0x9300, 0x9400, 0x9500, 0x9600, 0x9700, + 0x9800, 0x9900, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530, + 0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510, 0x0610, 0x0710, + 0x0810, 0x0910, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530, + 0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510, 0x1610, 0x1710, + 0x1810, 0x1910, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530, + 0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510, 0x2610, 0x2710, + 0x2810, 0x2910, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, + 0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510, 0x3610, 0x3710, + 0x3810, 0x3910, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530, + 0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510, 0x4610, 0x4710, + 0x4810, 0x4910, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530, + 0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510, 0x5610, 0x5710, + 0x5810, 0x5910, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530, + 0x6010, 0x6110, 0x6210, 0x6310, 0x6410, 0x6510, 0x6610, 0x6710, + 0x6810, 0x6910, 0x7030, 0x7130, 0x7230, 0x7330, 0x7430, 0x7530, + 0x7010, 0x7110, 0x7210, 0x7310, 0x7410, 0x7510, 0x7610, 0x7710, + 0x7810, 0x7910, 0x8030, 0x8130, 0x8230, 0x8330, 0x8430, 0x8530, + 0x8010, 0x8110, 0x8210, 0x8310, 0x8410, 0x8510, 0x8610, 0x8710, + 0x8810, 0x8910, 0x9030, 0x9130, 0x9230, 0x9330, 0x9430, 0x9530, + 0x9010, 0x9110, 0x9210, 0x9310, 0x9410, 0x9510, 0x9610, 0x9710, + 0x9810, 0x9910, 0xA030, 0xA130, 0xA230, 0xA330, 0xA430, 0xA530, + 0xA010, 0xA110, 0xA210, 0xA310, 0xA410, 0xA510, 0xA610, 0xA710, + 0xA810, 0xA910, 0xB030, 0xB130, 0xB230, 0xB330, 0xB430, 0xB530, + 0xB010, 0xB110, 0xB210, 0xB310, 0xB410, 0xB510, 0xB610, 0xB710, + 0xB810, 0xB910, 0xC030, 0xC130, 0xC230, 0xC330, 0xC430, 0xC530, + 0xC010, 0xC110, 0xC210, 0xC310, 0xC410, 0xC510, 0xC610, 0xC710, + 0xC810, 0xC910, 0xD030, 0xD130, 0xD230, 0xD330, 0xD430, 0xD530, + 0xD010, 0xD110, 0xD210, 0xD310, 0xD410, 0xD510, 0xD610, 0xD710, + 0xD810, 0xD910, 0xE030, 0xE130, 0xE230, 0xE330, 0xE430, 0xE530, + 0xE010, 0xE110, 0xE210, 0xE310, 0xE410, 0xE510, 0xE610, 0xE710, + 0xE810, 0xE910, 0xF030, 0xF130, 0xF230, 0xF330, 0xF430, 0xF530, + 0xF010, 0xF110, 0xF210, 0xF310, 0xF410, 0xF510, 0xF610, 0xF710, + 0xF810, 0xF910, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530, + 0x0090, 0x0110, 0x0210, 0x0310, 0x0410, 0x0510, 0x0610, 0x0710, + 0x0810, 0x0910, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530, + 0x1010, 0x1110, 0x1210, 0x1310, 0x1410, 0x1510, 0x1610, 0x1710, + 0x1810, 0x1910, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530, + 0x2010, 0x2110, 0x2210, 0x2310, 0x2410, 0x2510, 0x2610, 0x2710, + 0x2810, 0x2910, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, + 0x3010, 0x3110, 0x3210, 0x3310, 0x3410, 0x3510, 0x3610, 0x3710, + 0x3810, 0x3910, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530, + 0x4010, 0x4110, 0x4210, 0x4310, 0x4410, 0x4510, 0x4610, 0x4710, + 0x4810, 0x4910, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530, + 0x5010, 0x5110, 0x5210, 0x5310, 0x5410, 0x5510, 0x5610, 0x5710, + 0x5810, 0x5910, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530, + 0x0600, 0x0700, 0x0800, 0x0900, 0x0A00, 0x0B00, 0x0C00, 0x0D00, + 0x0E00, 0x0F00, 0x1020, 0x1120, 0x1220, 0x1320, 0x1420, 0x1520, + 0x1600, 0x1700, 0x1800, 0x1900, 0x1A00, 0x1B00, 0x1C00, 0x1D00, + 0x1E00, 0x1F00, 0x2020, 0x2120, 0x2220, 0x2320, 0x2420, 0x2520, + 0x2600, 0x2700, 0x2800, 0x2900, 0x2A00, 0x2B00, 0x2C00, 0x2D00, + 0x2E00, 0x2F00, 0x3020, 0x3120, 0x3220, 0x3320, 0x3420, 0x3520, + 0x3600, 0x3700, 0x3800, 0x3900, 0x3A00, 0x3B00, 0x3C00, 0x3D00, + 0x3E00, 0x3F00, 0x4020, 0x4120, 0x4220, 0x4320, 0x4420, 0x4520, + 0x4600, 0x4700, 0x4800, 0x4900, 0x4A00, 0x4B00, 0x4C00, 0x4D00, + 0x4E00, 0x4F00, 0x5020, 0x5120, 0x5220, 0x5320, 0x5420, 0x5520, + 0x5600, 0x5700, 0x5800, 0x5900, 0x5A00, 0x5B00, 0x5C00, 0x5D00, + 0x5E00, 0x5F00, 0x6020, 0x6120, 0x6220, 0x6320, 0x6420, 0x6520, + 0x6600, 0x6700, 0x6800, 0x6900, 0x6A00, 0x6B00, 0x6C00, 0x6D00, + 0x6E00, 0x6F00, 0x7020, 0x7120, 0x7220, 0x7320, 0x7420, 0x7520, + 0x7600, 0x7700, 0x7800, 0x7900, 0x7A00, 0x7B00, 0x7C00, 0x7D00, + 0x7E00, 0x7F00, 0x8020, 0x8120, 0x8220, 0x8320, 0x8420, 0x8520, + 0x8600, 0x8700, 0x8800, 0x8900, 0x8A00, 0x8B00, 0x8C00, 0x8D00, + 0x8E00, 0x8F00, 0x9020, 0x9120, 0x9220, 0x9320, 0x9420, 0x9520, + 0x9600, 0x9700, 0x9800, 0x9900, 0x9A00, 0x9B00, 0x9C00, 0x9D00, + 0x9E00, 0x9F00, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530, + 0x0610, 0x0710, 0x0810, 0x0910, 0x0A10, 0x0B10, 0x0C10, 0x0D10, + 0x0E10, 0x0F10, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530, + 0x1610, 0x1710, 0x1810, 0x1910, 0x1A10, 0x1B10, 0x1C10, 0x1D10, + 0x1E10, 0x1F10, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530, + 0x2610, 0x2710, 0x2810, 0x2910, 0x2A10, 0x2B10, 0x2C10, 0x2D10, + 0x2E10, 0x2F10, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, + 0x3610, 0x3710, 0x3810, 0x3910, 0x3A10, 0x3B10, 0x3C10, 0x3D10, + 0x3E10, 0x3F10, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530, + 0x4610, 0x4710, 0x4810, 0x4910, 0x4A10, 0x4B10, 0x4C10, 0x4D10, + 0x4E10, 0x4F10, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530, + 0x5610, 0x5710, 0x5810, 0x5910, 0x5A10, 0x5B10, 0x5C10, 0x5D10, + 0x5E10, 0x5F10, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530, + 0x6610, 0x6710, 0x6810, 0x6910, 0x6A10, 0x6B10, 0x6C10, 0x6D10, + 0x6E10, 0x6F10, 0x7030, 0x7130, 0x7230, 0x7330, 0x7430, 0x7530, + 0x7610, 0x7710, 0x7810, 0x7910, 0x7A10, 0x7B10, 0x7C10, 0x7D10, + 0x7E10, 0x7F10, 0x8030, 0x8130, 0x8230, 0x8330, 0x8430, 0x8530, + 0x8610, 0x8710, 0x8810, 0x8910, 0x8A10, 0x8B10, 0x8C10, 0x8D10, + 0x8E10, 0x8F10, 0x9030, 0x9130, 0x9230, 0x9330, 0x9430, 0x9530, + 0x9610, 0x9710, 0x9810, 0x9910, 0x9A10, 0x9B10, 0x9C10, 0x9D10, + 0x9E10, 0x9F10, 0xA030, 0xA130, 0xA230, 0xA330, 0xA430, 0xA530, + 0xA610, 0xA710, 0xA810, 0xA910, 0xAA10, 0xAB10, 0xAC10, 0xAD10, + 0xAE10, 0xAF10, 0xB030, 0xB130, 0xB230, 0xB330, 0xB430, 0xB530, + 0xB610, 0xB710, 0xB810, 0xB910, 0xBA10, 0xBB10, 0xBC10, 0xBD10, + 0xBE10, 0xBF10, 0xC030, 0xC130, 0xC230, 0xC330, 0xC430, 0xC530, + 0xC610, 0xC710, 0xC810, 0xC910, 0xCA10, 0xCB10, 0xCC10, 0xCD10, + 0xCE10, 0xCF10, 0xD030, 0xD130, 0xD230, 0xD330, 0xD430, 0xD530, + 0xD610, 0xD710, 0xD810, 0xD910, 0xDA10, 0xDB10, 0xDC10, 0xDD10, + 0xDE10, 0xDF10, 0xE030, 0xE130, 0xE230, 0xE330, 0xE430, 0xE530, + 0xE610, 0xE710, 0xE810, 0xE910, 0xEA10, 0xEB10, 0xEC10, 0xED10, + 0xEE10, 0xEF10, 0xF030, 0xF130, 0xF230, 0xF330, 0xF430, 0xF530, + 0xF610, 0xF710, 0xF810, 0xF910, 0xFA10, 0xFB10, 0xFC10, 0xFD10, + 0xFE10, 0xFF10, 0x00B0, 0x0130, 0x0230, 0x0330, 0x0430, 0x0530, + 0x0610, 0x0710, 0x0810, 0x0910, 0x0A10, 0x0B10, 0x0C10, 0x0D10, + 0x0E10, 0x0F10, 0x1030, 0x1130, 0x1230, 0x1330, 0x1430, 0x1530, + 0x1610, 0x1710, 0x1810, 0x1910, 0x1A10, 0x1B10, 0x1C10, 0x1D10, + 0x1E10, 0x1F10, 0x2030, 0x2130, 0x2230, 0x2330, 0x2430, 0x2530, + 0x2610, 0x2710, 0x2810, 0x2910, 0x2A10, 0x2B10, 0x2C10, 0x2D10, + 0x2E10, 0x2F10, 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, + 0x3610, 0x3710, 0x3810, 0x3910, 0x3A10, 0x3B10, 0x3C10, 0x3D10, + 0x3E10, 0x3F10, 0x4030, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530, + 0x4610, 0x4710, 0x4810, 0x4910, 0x4A10, 0x4B10, 0x4C10, 0x4D10, + 0x4E10, 0x4F10, 0x5030, 0x5130, 0x5230, 0x5330, 0x5430, 0x5530, + 0x5610, 0x5710, 0x5810, 0x5910, 0x5A10, 0x5B10, 0x5C10, 0x5D10, + 0x5E10, 0x5F10, 0x6030, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530, + 0x00C0, 0x0140, 0x0240, 0x0340, 0x0440, 0x0540, 0x0640, 0x0740, + 0x0840, 0x0940, 0x0440, 0x0540, 0x0640, 0x0740, 0x0840, 0x0940, + 0x1040, 0x1140, 0x1240, 0x1340, 0x1440, 0x1540, 0x1640, 0x1740, + 0x1840, 0x1940, 0x1440, 0x1540, 0x1640, 0x1740, 0x1840, 0x1940, + 0x2040, 0x2140, 0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740, + 0x2840, 0x2940, 0x2440, 0x2540, 0x2640, 0x2740, 0x2840, 0x2940, + 0x3040, 0x3140, 0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740, + 0x3840, 0x3940, 0x3440, 0x3540, 0x3640, 0x3740, 0x3840, 0x3940, + 0x4040, 0x4140, 0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740, + 0x4840, 0x4940, 0x4440, 0x4540, 0x4640, 0x4740, 0x4840, 0x4940, + 0x5040, 0x5140, 0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740, + 0x5840, 0x5940, 0x5440, 0x5540, 0x5640, 0x5740, 0x5840, 0x5940, + 0x6040, 0x6140, 0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740, + 0x6840, 0x6940, 0x6440, 0x6540, 0x6640, 0x6740, 0x6840, 0x6940, + 0x7040, 0x7140, 0x7240, 0x7340, 0x7440, 0x7540, 0x7640, 0x7740, + 0x7840, 0x7940, 0x7440, 0x7540, 0x7640, 0x7740, 0x7840, 0x7940, + 0x8040, 0x8140, 0x8240, 0x8340, 0x8440, 0x8540, 0x8640, 0x8740, + 0x8840, 0x8940, 0x8440, 0x8540, 0x8640, 0x8740, 0x8840, 0x8940, + 0x9040, 0x9140, 0x9240, 0x9340, 0x9440, 0x9540, 0x9640, 0x9740, + 0x9840, 0x9940, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950, + 0x4050, 0x4150, 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, + 0x4850, 0x4950, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950, + 0x5050, 0x5150, 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, + 0x5850, 0x5950, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950, + 0x6050, 0x6150, 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, + 0x6850, 0x6950, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950, + 0x7050, 0x7150, 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, + 0x7850, 0x7950, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950, + 0x8050, 0x8150, 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, + 0x8850, 0x8950, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950, + 0x9050, 0x9150, 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, + 0x9850, 0x9950, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950, + 0xA050, 0xA150, 0xA250, 0xA350, 0xA450, 0xA550, 0xA650, 0xA750, + 0xA850, 0xA950, 0xA450, 0xA550, 0xA650, 0xA750, 0xA850, 0xA950, + 0xB050, 0xB150, 0xB250, 0xB350, 0xB450, 0xB550, 0xB650, 0xB750, + 0xB850, 0xB950, 0xB450, 0xB550, 0xB650, 0xB750, 0xB850, 0xB950, + 0xC050, 0xC150, 0xC250, 0xC350, 0xC450, 0xC550, 0xC650, 0xC750, + 0xC850, 0xC950, 0xC450, 0xC550, 0xC650, 0xC750, 0xC850, 0xC950, + 0xD050, 0xD150, 0xD250, 0xD350, 0xD450, 0xD550, 0xD650, 0xD750, + 0xD850, 0xD950, 0xD450, 0xD550, 0xD650, 0xD750, 0xD850, 0xD950, + 0xE050, 0xE150, 0xE250, 0xE350, 0xE450, 0xE550, 0xE650, 0xE750, + 0xE850, 0xE950, 0xE450, 0xE550, 0xE650, 0xE750, 0xE850, 0xE950, + 0xF050, 0xF150, 0xF250, 0xF350, 0xF450, 0xF550, 0xF650, 0xF750, + 0xF850, 0xF950, 0xF450, 0xF550, 0xF650, 0xF750, 0xF850, 0xF950, + 0x00D0, 0x0150, 0x0250, 0x0350, 0x0450, 0x0550, 0x0650, 0x0750, + 0x0850, 0x0950, 0x0450, 0x0550, 0x0650, 0x0750, 0x0850, 0x0950, + 0x1050, 0x1150, 0x1250, 0x1350, 0x1450, 0x1550, 0x1650, 0x1750, + 0x1850, 0x1950, 0x1450, 0x1550, 0x1650, 0x1750, 0x1850, 0x1950, + 0x2050, 0x2150, 0x2250, 0x2350, 0x2450, 0x2550, 0x2650, 0x2750, + 0x2850, 0x2950, 0x2450, 0x2550, 0x2650, 0x2750, 0x2850, 0x2950, + 0x3050, 0x3150, 0x3250, 0x3350, 0x3450, 0x3550, 0x3650, 0x3750, + 0x3850, 0x3950, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950, + 0x4050, 0x4150, 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, + 0x4850, 0x4950, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950, + 0x5050, 0x5150, 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, + 0x5850, 0x5950, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950, + 0x6050, 0x6150, 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, + 0x6850, 0x6950, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950, + 0x7050, 0x7150, 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, + 0x7850, 0x7950, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950, + 0x8050, 0x8150, 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, + 0x8850, 0x8950, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950, + 0x9050, 0x9150, 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, + 0x9850, 0x9950, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950, + 0xFA60, 0xFB60, 0xFC60, 0xFD60, 0xFE60, 0xFF60, 0x00C0, 0x0140, + 0x0240, 0x0340, 0x0440, 0x0540, 0x0640, 0x0740, 0x0840, 0x0940, + 0x0A60, 0x0B60, 0x0C60, 0x0D60, 0x0E60, 0x0F60, 0x1040, 0x1140, + 0x1240, 0x1340, 0x1440, 0x1540, 0x1640, 0x1740, 0x1840, 0x1940, + 0x1A60, 0x1B60, 0x1C60, 0x1D60, 0x1E60, 0x1F60, 0x2040, 0x2140, + 0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740, 0x2840, 0x2940, + 0x2A60, 0x2B60, 0x2C60, 0x2D60, 0x2E60, 0x2F60, 0x3040, 0x3140, + 0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740, 0x3840, 0x3940, + 0x3A60, 0x3B60, 0x3C60, 0x3D60, 0x3E60, 0x3F60, 0x4040, 0x4140, + 0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740, 0x4840, 0x4940, + 0x4A60, 0x4B60, 0x4C60, 0x4D60, 0x4E60, 0x4F60, 0x5040, 0x5140, + 0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740, 0x5840, 0x5940, + 0x5A60, 0x5B60, 0x5C60, 0x5D60, 0x5E60, 0x5F60, 0x6040, 0x6140, + 0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740, 0x6840, 0x6940, + 0x6A60, 0x6B60, 0x6C60, 0x6D60, 0x6E60, 0x6F60, 0x7040, 0x7140, + 0x7240, 0x7340, 0x7440, 0x7540, 0x7640, 0x7740, 0x7840, 0x7940, + 0x7A60, 0x7B60, 0x7C60, 0x7D60, 0x7E60, 0x7F60, 0x8040, 0x8140, + 0x8240, 0x8340, 0x8440, 0x8540, 0x8640, 0x8740, 0x8840, 0x8940, + 0x8A60, 0x8B60, 0x8C60, 0x8D60, 0x8E60, 0x8F60, 0x9040, 0x9140, + 0x9240, 0x9340, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950, + 0x3A70, 0x3B70, 0x3C70, 0x3D70, 0x3E70, 0x3F70, 0x4050, 0x4150, + 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950, + 0x4A70, 0x4B70, 0x4C70, 0x4D70, 0x4E70, 0x4F70, 0x5050, 0x5150, + 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950, + 0x5A70, 0x5B70, 0x5C70, 0x5D70, 0x5E70, 0x5F70, 0x6050, 0x6150, + 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950, + 0x6A70, 0x6B70, 0x6C70, 0x6D70, 0x6E70, 0x6F70, 0x7050, 0x7150, + 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950, + 0x7A70, 0x7B70, 0x7C70, 0x7D70, 0x7E70, 0x7F70, 0x8050, 0x8150, + 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950, + 0x8A70, 0x8B70, 0x8C70, 0x8D70, 0x8E70, 0x8F70, 0x9050, 0x9150, + 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950, + 0x9A70, 0x9B70, 0x9C70, 0x9D70, 0x9E70, 0x9F70, 0xA050, 0xA150, + 0xA250, 0xA350, 0xA450, 0xA550, 0xA650, 0xA750, 0xA850, 0xA950, + 0xAA70, 0xAB70, 0xAC70, 0xAD70, 0xAE70, 0xAF70, 0xB050, 0xB150, + 0xB250, 0xB350, 0xB450, 0xB550, 0xB650, 0xB750, 0xB850, 0xB950, + 0xBA70, 0xBB70, 0xBC70, 0xBD70, 0xBE70, 0xBF70, 0xC050, 0xC150, + 0xC250, 0xC350, 0xC450, 0xC550, 0xC650, 0xC750, 0xC850, 0xC950, + 0xCA70, 0xCB70, 0xCC70, 0xCD70, 0xCE70, 0xCF70, 0xD050, 0xD150, + 0xD250, 0xD350, 0xD450, 0xD550, 0xD650, 0xD750, 0xD850, 0xD950, + 0xDA70, 0xDB70, 0xDC70, 0xDD70, 0xDE70, 0xDF70, 0xE050, 0xE150, + 0xE250, 0xE350, 0xE450, 0xE550, 0xE650, 0xE750, 0xE850, 0xE950, + 0xEA70, 0xEB70, 0xEC70, 0xED70, 0xEE70, 0xEF70, 0xF050, 0xF150, + 0xF250, 0xF350, 0xF450, 0xF550, 0xF650, 0xF750, 0xF850, 0xF950, + 0xFA70, 0xFB70, 0xFC70, 0xFD70, 0xFE70, 0xFF70, 0x00D0, 0x0150, + 0x0250, 0x0350, 0x0450, 0x0550, 0x0650, 0x0750, 0x0850, 0x0950, + 0x0A70, 0x0B70, 0x0C70, 0x0D70, 0x0E70, 0x0F70, 0x1050, 0x1150, + 0x1250, 0x1350, 0x1450, 0x1550, 0x1650, 0x1750, 0x1850, 0x1950, + 0x1A70, 0x1B70, 0x1C70, 0x1D70, 0x1E70, 0x1F70, 0x2050, 0x2150, + 0x2250, 0x2350, 0x2450, 0x2550, 0x2650, 0x2750, 0x2850, 0x2950, + 0x2A70, 0x2B70, 0x2C70, 0x2D70, 0x2E70, 0x2F70, 0x3050, 0x3150, + 0x3250, 0x3350, 0x3450, 0x3550, 0x3650, 0x3750, 0x3850, 0x3950, + 0x3A70, 0x3B70, 0x3C70, 0x3D70, 0x3E70, 0x3F70, 0x4050, 0x4150, + 0x4250, 0x4350, 0x4450, 0x4550, 0x4650, 0x4750, 0x4850, 0x4950, + 0x4A70, 0x4B70, 0x4C70, 0x4D70, 0x4E70, 0x4F70, 0x5050, 0x5150, + 0x5250, 0x5350, 0x5450, 0x5550, 0x5650, 0x5750, 0x5850, 0x5950, + 0x5A70, 0x5B70, 0x5C70, 0x5D70, 0x5E70, 0x5F70, 0x6050, 0x6150, + 0x6250, 0x6350, 0x6450, 0x6550, 0x6650, 0x6750, 0x6850, 0x6950, + 0x6A70, 0x6B70, 0x6C70, 0x6D70, 0x6E70, 0x6F70, 0x7050, 0x7150, + 0x7250, 0x7350, 0x7450, 0x7550, 0x7650, 0x7750, 0x7850, 0x7950, + 0x7A70, 0x7B70, 0x7C70, 0x7D70, 0x7E70, 0x7F70, 0x8050, 0x8150, + 0x8250, 0x8350, 0x8450, 0x8550, 0x8650, 0x8750, 0x8850, 0x8950, + 0x8A70, 0x8B70, 0x8C70, 0x8D70, 0x8E70, 0x8F70, 0x9050, 0x9150, + 0x9250, 0x9350, 0x9450, 0x9550, 0x9650, 0x9750, 0x9850, 0x9950, +}; + +u8 ZeroTable[] = +{ + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +#define GBSAVE_GAME_VERSION_1 1 +#define GBSAVE_GAME_VERSION_2 2 +#define GBSAVE_GAME_VERSION_3 3 +#define GBSAVE_GAME_VERSION_4 4 +#define GBSAVE_GAME_VERSION_5 5 +#define GBSAVE_GAME_VERSION_6 6 +#define GBSAVE_GAME_VERSION_7 7 +#define GBSAVE_GAME_VERSION_8 8 +#define GBSAVE_GAME_VERSION_9 9 +#define GBSAVE_GAME_VERSION_10 10 +#define GBSAVE_GAME_VERSION_11 11 +#define GBSAVE_GAME_VERSION_12 12 +#define GBSAVE_GAME_VERSION_13 13 +#define GBSAVE_GAME_VERSION GBSAVE_GAME_VERSION_13 + +int inline gbGetValue(int min, int max, int v) +{ + return (int)(min + (float)(max - min) * (2.0 * (v / 31.0) - (v / 31.0) * (v / 31.0))); +} + +void gbGenFilter() +{ + for (int r = 0; r < 32; r++) + { + for (int g = 0; g < 32; g++) + { + for (int b = 0; b < 32; b++) + { + int nr = gbGetValue(gbGetValue(4, 14, g), + gbGetValue(24, 29, g), r) - 4; + int ng = gbGetValue(gbGetValue(4 + gbGetValue(0, 5, r), + 14 + gbGetValue(0, 3, r), b), + gbGetValue(24 + gbGetValue(0, 3, r), + 29 + gbGetValue(0, 1, r), b), g) - 4; + int nb = gbGetValue(gbGetValue(4 + gbGetValue(0, 5, r), + 14 + gbGetValue(0, 3, r), g), + gbGetValue(24 + gbGetValue(0, 3, r), + 29 + gbGetValue(0, 1, r), g), b) - 4; + gbColorFilter[(b << 10) | (g << 5) | r] = (nb << 10) | (ng << 5) | nr; + } + } + } +} + +void gbCopyMemory(u16 d, u16 s, int count) +{ + while (count) + { + gbWriteMemoryQuick(d, gbReadMemoryQuick(s)); + s++; + d++; + count--; + } +} + +void gbDoHdma() +{ + gbCopyMemory(gbHdmaDestination, gbHdmaSource, 0x10); + + gbHdmaDestination += 0x10; + gbHdmaSource += 0x10; + + register_HDMA2 = (register_HDMA2 + 0x10) & 0xFF; + if (register_HDMA2 == 0x00) + register_HDMA1++; + + register_HDMA4 = (register_HDMA4 + 0x10) & 0xFF; + if (register_HDMA4 == 0x00) + register_HDMA3++; + + if (gbHdmaDestination == 0x96b0) + gbHdmaBytes = gbHdmaBytes; + gbHdmaBytes -= 0x10; + register_HDMA5--; + if (register_HDMA5 == 0xff) + gbHdmaOn = 0; +} + +// fix for Harley and Lego Racers +void gbCompareLYToLYC() +{ + if (register_LY == register_LYC) + { + // mark that we have a match + register_STAT |= 4; + + // check if we need an interrupt + if ((register_STAT & 0x40) && (register_IE & 2)) + gbInterrupt |= 2; + } + else // no match + register_STAT &= 0xfb; +} + +// FIXME: horrible kludge to workaround the frame timing bug +static int32 s_gbJoymask[4] = { 0, 0, 0, 0 }; + +void gbWriteMemoryWrapped(register u16 address, register u8 value) +{ + if (address < 0x8000) + { +#ifndef FINAL_VERSION + if (memorydebug && (address > 0x3fff || address < 0x2000)) + { + log("Memory register write %04x=%02x PC=%04x\n", + address, + value, + PC.W); + } +#endif + if (mapper) + (*mapper)(address, value); + return; + } + + if (address < 0xa000) + { + gbWriteMemoryQuick(address, value); + return; + } + + if (address < 0xc000) + { +#ifndef FINAL_VERSION + if (memorydebug) + { + log("Memory register write %04x=%02x PC=%04x\n", + address, + value, + PC.W); + } +#endif + + if (mapper) + (*mapperRAM)(address, value); + return; + } + + if (address < 0xfe00) + { + gbWriteMemoryQuick(address, value); + return; + } + + if (address < 0xff00) + { + gbMemory[address] = value; + return; + } + + switch (address & 0x00ff) + { + case 0x00: + { + gbMemory[0xff00] = ((gbMemory[0xff00] & 0xcf) | + (value & 0x30)); + if (gbSgbMode) + { + gbSgbDoBitTransfer(value); + } + + return; + } + + case 0x01: + { + gbMemory[0xff01] = value; + return; + } + + // serial control + case 0x02: + { + gbSerialOn = (value & 0x80); + gbMemory[0xff02] = value; + if (gbSerialOn) + { + gbSerialTicks = GBSERIAL_CLOCK_TICKS; +#ifdef LINK_EMULATION + if (linkConnected) + { + if (value & 1) + { + linkSendByte(0x100 | gbMemory[0xFF01]); + Sleep(5); + } + } +#endif + } + + gbSerialBits = 0; + return; + } + + // DIV register resets on any write + case 0x04: + { + register_DIV = 0; + return; + } + case 0x05: + register_TIMA = value; + return; + + case 0x06: + register_TMA = value; + return; + + // TIMER control + case 0x07: + { + register_TAC = value; + + gbTimerOn = (value & 4); + gbTimerMode = value & 3; + // register_TIMA = register_TMA; + switch (gbTimerMode) + { + case 0: + gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_0_CLOCK_TICKS; + break; + case 1: + gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_1_CLOCK_TICKS; + break; + case 2: + gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_2_CLOCK_TICKS; + break; + case 3: + gbTimerClockTicks = gbTimerTicks = GBTIMER_MODE_3_CLOCK_TICKS; + break; + } + return; + } + + case 0x0f: + { + register_IF = value; + gbInterrupt = value; + return; + } + + case 0x10: + case 0x11: + case 0x12: + case 0x13: + case 0x14: + case 0x15: + case 0x16: + case 0x17: + case 0x18: + case 0x19: + case 0x1a: + case 0x1b: + case 0x1c: + case 0x1d: + case 0x1e: + case 0x1f: + case 0x20: + case 0x21: + case 0x22: + case 0x23: + case 0x24: + case 0x25: + case 0x26: + { + SOUND_EVENT(address, value); + return; + } + case 0x40: + { + int lcdChange = (register_LCDC & 0x80) ^ (value & 0x80); + + if (lcdChange) + { + if (value & 0x80) + { + gbLcdTicks = GBLCD_MODE_1_CLOCK_TICKS; + gbLcdMode = 0; + register_STAT &= 0xfc; + register_LY = 0x00; + // FIXME: horrible workaround + if (gbNullInputHackTempEnabled && !useOldFrameTiming) + memcpy(gbJoymask, s_gbJoymask, sizeof(gbJoymask)); + } + else + { + gbLcdTicks = 0; + gbLcdMode = 0; + register_STAT &= 0xfc; + register_LY = 0x00; + // FIXME: horrible workaround + memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask)); + if (gbNullInputHackTempEnabled && !useOldFrameTiming) + memset(gbJoymask, 0, sizeof(gbJoymask)); + } + // compareLYToLYC(); + } + // don't draw the window if it was not enabled and not being drawn before + if (!(register_LCDC & 0x20) && (value & 0x20) && gbWindowLine == -1 && + register_LY > register_WY) + gbWindowLine = 144; + + register_LCDC = value; + + return; + } + + // STAT + case 0x41: + { + //register_STAT = (register_STAT & 0x87) | + // (value & 0x7c); + register_STAT = (value & 0xf8) | (register_STAT & 0x07); // fix ? + // GB bug from Devrs FAQ + if (!gbCgbMode && (register_LCDC & 0x80) && gbLcdMode < 2) + gbInterrupt |= 2; + return; + } + + // SCY + case 0x42: + { + register_SCY = value; + return; + } + + // SCX + case 0x43: + { + register_SCX = value; + return; + } + + // LY + case 0x44: + { + // read only + return; + } + + // LYC + case 0x45: + { + register_LYC = value; + if ((register_LCDC & 0x80)) + { + gbCompareLYToLYC(); + } + return; + } + + // DMA! + case 0x46: + { + int source = value * 0x0100; + + gbCopyMemory(0xfe00, + source, + 0xa0); + register_DMA = value; + return; + } + + // BGP + case 0x47: + { + gbBgp[0] = value & 0x03; + gbBgp[1] = (value & 0x0c) >> 2; + gbBgp[2] = (value & 0x30) >> 4; + gbBgp[3] = (value & 0xc0) >> 6; + break; + } + + // OBP0 + case 0x48: + { + gbObp0[0] = value & 0x03; + gbObp0[1] = (value & 0x0c) >> 2; + gbObp0[2] = (value & 0x30) >> 4; + gbObp0[3] = (value & 0xc0) >> 6; + break; + } + + // OBP1 + case 0x49: + { + gbObp1[0] = value & 0x03; + gbObp1[1] = (value & 0x0c) >> 2; + gbObp1[2] = (value & 0x30) >> 4; + gbObp1[3] = (value & 0xc0) >> 6; + break; + } + + case 0x4a: + register_WY = value; + return; + + case 0x4b: + register_WX = value; + return; + + // KEY1 + case 0x4d: + { + if (gbCgbMode) + { + gbMemory[0xff4d] = (gbMemory[0xff4d] & 0x80) | (value & 1); + return; + } + break; + } + + // VBK + case 0x4f: + { + if (gbCgbMode) + { + value = value & 1; + if (value == gbVramBank) + return; + + int vramAddress = value * 0x2000; + gbMemoryMap[0x08] = &gbVram[vramAddress]; + gbMemoryMap[0x09] = &gbVram[vramAddress + 0x1000]; + + gbVramBank = value; + register_VBK = value; + } + return; + break; + } + + // HDMA1 + case 0x51: + { + if (gbCgbMode) + { + if (value > 0x7f && value < 0xa0) + value = 0; + + gbHdmaSource = (value << 8) | (register_HDMA2 & 0xf0); + + register_HDMA1 = value; + return; + } + break; + } + + // HDMA2 + case 0x52: + { + if (gbCgbMode) + { + value = value & 0xf0; + + gbHdmaSource = (register_HDMA1 << 8) | (value); + + register_HDMA2 = value; + return; + } + break; + } + + // HDMA3 + case 0x53: + { + if (gbCgbMode) + { + value = value & 0x1f; + gbHdmaDestination = (value << 8) | (register_HDMA4 & 0xf0); + gbHdmaDestination += 0x8000; + register_HDMA3 = value; + return; + } + break; + } + + // HDMA4 + case 0x54: + { + if (gbCgbMode) + { + value = value & 0xf0; + gbHdmaDestination = ((register_HDMA3 & 0x1f) << 8) | value; + gbHdmaDestination += 0x8000; + register_HDMA4 = value; + return; + } + break; + } + + // HDMA5 + case 0x55: + { + if (gbCgbMode) + { + gbHdmaBytes = 16 + (value & 0x7f) * 16; + if (gbHdmaOn) + { + if (value & 0x80) + { + register_HDMA5 = (value & 0x7f); + } + else + { + register_HDMA5 = 0xff; + gbHdmaOn = 0; + } + } + else + { + if (value & 0x80) + { + gbHdmaOn = 1; + register_HDMA5 = value & 0x7f; + if (gbLcdMode == 0) + gbDoHdma(); + } + else + { + // we need to take the time it takes to complete the transfer into + // account... according to GB DEV FAQs, the setup time is the same + // for single and double speed, but the actual transfer takes the + // same time // (is that a typo?) + switch (gbDMASpeedVersion) + { + case 1: // I believe this is more correct + // the lower 7 bits of FF55 specify the Transfer Length (divided by 16, minus 1) + // and we make gbDmaTicks twice as many cycles at double speed to make the transfer take the same time + if (gbSpeed) + gbDmaTicks = 16 * ((value & 0x7f) + 1); + else + gbDmaTicks = 8 * ((value & 0x7f) + 1); + break; + case 0: // here for backward compatibility + // I think this was a guess that approximates the above in most but not all games + if (gbSpeed) + gbDmaTicks = 231 + 16 * (value & 0x7f); + else + gbDmaTicks = 231 + 8 * (value & 0x7f); + break; + default: // shouldn't happen + //assert(0); + break; + } + gbCopyMemory(gbHdmaDestination, gbHdmaSource, gbHdmaBytes); + gbHdmaDestination += gbHdmaBytes; + gbHdmaSource += gbHdmaBytes; + + register_HDMA3 = ((gbHdmaDestination - 0x8000) >> 8) & 0x1f; + register_HDMA4 = gbHdmaDestination & 0xf0; + register_HDMA1 = (gbHdmaSource >> 8) & 0xff; + register_HDMA2 = gbHdmaSource & 0xf0; + } + } + return; + } + break; + } + + // BCPS + case 0x68: + { + if (gbCgbMode) + { + int paletteIndex = (value & 0x3f) >> 1; + int paletteHiLo = (value & 0x01); + + gbMemory[0xff68] = value; + gbMemory[0xff69] = (paletteHiLo ? + (gbPalette[paletteIndex] >> 8) : + (gbPalette[paletteIndex] & 0x00ff)); + return; + } + break; + } + + // BCPD + case 0x69: + { + if (gbCgbMode) + { + int v = gbMemory[0xff68]; + int paletteIndex = (v & 0x3f) >> 1; + int paletteHiLo = (v & 0x01); + gbMemory[0xff69] = value; + gbPalette[paletteIndex] = (paletteHiLo ? + ((value << 8) | (gbPalette[paletteIndex] & 0xff)) : + ((gbPalette[paletteIndex] & 0xff00) | (value))) & 0x7fff; + + if (gbMemory[0xff68] & 0x80) + { + int index = ((gbMemory[0xff68] & 0x3f) + 1) & 0x3f; + + gbMemory[0xff68] = (gbMemory[0xff68] & 0x80) | index; + + gbMemory[0xff69] = (index & 1 ? + (gbPalette[index >> 1] >> 8) : + (gbPalette[index >> 1] & 0x00ff)); + } + return; + } + break; + } + + // OCPS + case 0x6a: + { + if (gbCgbMode) + { + int paletteIndex = (value & 0x3f) >> 1; + int paletteHiLo = (value & 0x01); + + paletteIndex += 32; + + gbMemory[0xff6a] = value; + gbMemory[0xff6b] = (paletteHiLo ? + (gbPalette[paletteIndex] >> 8) : + (gbPalette[paletteIndex] & 0x00ff)); + return; + } + break; + } + + // OCPD + case 0x6b: + { + if (gbCgbMode) + { + int v = gbMemory[0xff6a]; + int paletteIndex = (v & 0x3f) >> 1; + int paletteHiLo = (v & 0x01); + + paletteIndex += 32; + + gbMemory[0xff6b] = value; + gbPalette[paletteIndex] = (paletteHiLo ? + ((value << 8) | (gbPalette[paletteIndex] & 0xff)) : + ((gbPalette[paletteIndex] & 0xff00) | (value))) & 0x7fff; + if (gbMemory[0xff6a] & 0x80) + { + int index = ((gbMemory[0xff6a] & 0x3f) + 1) & 0x3f; + + gbMemory[0xff6a] = (gbMemory[0xff6a] & 0x80) | index; + + gbMemory[0xff6b] = (index & 1 ? + (gbPalette[(index >> 1) + 32] >> 8) : + (gbPalette[(index >> 1) + 32] & 0x00ff)); + } + return; + } + break; + } + + // SVBK + case 0x70: + { + if (gbCgbMode) + { + value = value & 7; + + int bank = value; + if (value == 0) + bank = 1; + + if (bank == gbWramBank) + return; + + int wramAddress = bank * 0x1000; + gbMemoryMap[0x0d] = &gbWram[wramAddress]; + + gbWramBank = bank; + register_SVBK = value; + return; + } + break; + } + + case 0xff: + { + register_IE = value; + register_IF &= value; + return; + } + } + + gbWriteMemoryQuick(address, value); +} + +u8 gbReadOpcode(register u16 address) +{ + if (gbCheatMap[address]) + return gbCheatRead(address); + + // the following fix does more than Echo RAM fix, anyway... + switch (gbEchoRAMFixOn ? (address >> 12) & 0x000f : address & 0xf000) + { + case 0x0a: + case 0x0b: + if (mapperReadRAM) + return mapperReadRAM(address); + break; + case 0x0f: + if (address > 0xff00) + { + switch (address & 0x00ff) + { + case 0x04: + return register_DIV; + case 0x05: + return register_TIMA; + case 0x06: + return register_TMA; + case 0x07: + return (0xf8 | register_TAC); + case 0x0f: + return (0xe0 | register_IF); + case 0x40: + return register_LCDC; + case 0x41: + return (0x80 | register_STAT); + case 0x42: + return register_SCY; + case 0x43: + return register_SCX; + case 0x44: + return register_LY; + case 0x45: + return register_LYC; + case 0x46: + return register_DMA; + case 0x4a: + return register_WY; + case 0x4b: + return register_WX; + case 0x4f: + return (0xfe | register_VBK); + case 0x51: + return register_HDMA1; + case 0x52: + return register_HDMA2; + case 0x53: + return register_HDMA3; + case 0x54: + return register_HDMA4; + case 0x55: + return register_HDMA5; + case 0x70: + return (0xf8 | register_SVBK); + case 0xff: + return register_IE; + } + } + break; + } + return gbReadMemoryQuick(address); +} + +void gbWriteMemory(register u16 address, register u8 value) +{ + gbWriteMemoryWrapped(address, value); + CallRegisteredLuaMemHook(address, 1, value, LUAMEMHOOK_WRITE); +} + +u8 gbReadMemory(register u16 address) +{ + if (gbCheatMap[address]) + return gbCheatRead(address); + + if (address < 0xa000) + return gbReadMemoryQuick(address); + + if (address < 0xc000) + { +#ifndef FINAL_VERSION + if (memorydebug) + { + log("Memory register read %04x PC=%04x\n", + address, + PC.W); + } +#endif + + if (mapperReadRAM) + return mapperReadRAM(address); + return gbReadMemoryQuick(address); + } + + if (address >= 0xff00) + { + switch (address & 0x00ff) + { + case 0x00: + { + if (gbSgbMode) + { + gbSgbReadingController |= 4; + gbSgbResetPacketState(); + } + + int b = gbMemory[0xff00]; + + if ((b & 0x30) == 0x20) + { + b &= 0xf0; + + int joy = 0; + if (gbSgbMode && gbSgbMultiplayer) + { + switch (gbSgbNextController) + { + case 0x0f: + joy = 0; + break; + case 0x0e: + joy = 1; + break; + case 0x0d: + joy = 2; + break; + case 0x0c: + joy = 3; + break; + default: + joy = 0; + } + } + int joystate = gbJoymask[joy]; + if (!(joystate & 128)) + b |= 0x08; + if (!(joystate & 64)) + b |= 0x04; + if (!(joystate & 32)) + b |= 0x02; + if (!(joystate & 16)) + b |= 0x01; + + gbMemory[0xff00] = b; + } + else if ((b & 0x30) == 0x10) + { + b &= 0xf0; + + int joy = 0; + if (gbSgbMode && gbSgbMultiplayer) + { + switch (gbSgbNextController) + { + case 0x0f: + joy = 0; + break; + case 0x0e: + joy = 1; + break; + case 0x0d: + joy = 2; + break; + case 0x0c: + joy = 3; + break; + default: + joy = 0; + } + } + int joystate = gbJoymask[joy]; + if (!(joystate & 8)) + b |= 0x08; + if (!(joystate & 4)) + b |= 0x04; + if (!(joystate & 2)) + b |= 0x02; + if (!(joystate & 1)) + b |= 0x01; + + gbMemory[0xff00] = b; + } + else + { + if (gbSgbMode && gbSgbMultiplayer) + { + gbMemory[0xff00] = 0xf0 | gbSgbNextController; + } + else + { + gbMemory[0xff00] = 0xff; + } + } + } + GBSystemCounters.lagged = false; + return gbMemory[0xff00]; + break; + case 0x01: + return gbMemory[0xff01]; + case 0x04: + return register_DIV; + case 0x05: + return register_TIMA; + case 0x06: + return register_TMA; + case 0x07: + return (0xf8 | register_TAC); + case 0x0f: + return (0xe0 | register_IF); + case 0x40: + return register_LCDC; + case 0x41: + return (0x80 | register_STAT); + case 0x42: + return register_SCY; + case 0x43: + return register_SCX; + case 0x44: + return register_LY; + case 0x45: + return register_LYC; + case 0x46: + return register_DMA; + case 0x4a: + return register_WY; + case 0x4b: + return register_WX; + case 0x4f: + return (0xfe | register_VBK); + case 0x51: + return register_HDMA1; + case 0x52: + return register_HDMA2; + case 0x53: + return register_HDMA3; + case 0x54: + return register_HDMA4; + case 0x55: + return register_HDMA5; + case 0x70: + return (0xf8 | register_SVBK); + case 0xff: + return register_IE; + } + } + + return gbReadMemoryQuick(address); +} + +void gbVblank_interrupt() +{ + if (IFF & 0x80) + { + PC.W++; + IFF &= 0x7f; + } + gbInterrupt &= 0xfe; + + IFF &= 0x7e; + register_IF &= 0xfe; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + PC.W = 0x40; +} + +void gbLcd_interrupt() +{ + if (IFF & 0x80) + { + PC.W++; + IFF &= 0x7f; + } + gbInterrupt &= 0xfd; + IFF &= 0x7e; + register_IF &= 0xfd; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + + PC.W = 0x48; +} + +void gbTimer_interrupt() +{ + if (IFF & 0x80) + { + PC.W++; + IFF &= 0x7f; + } + IFF &= 0x7e; + gbInterrupt &= 0xfb; + register_IF &= 0xfb; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + + PC.W = 0x50; +} + +void gbSerial_interrupt() +{ + if (IFF & 0x80) + { + PC.W++; + IFF &= 0x7f; + } + IFF &= 0x7e; + gbInterrupt &= 0xf7; + register_IF &= 0xf7; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + + PC.W = 0x58; +} + +void gbJoypad_interrupt() +{ + if (IFF & 0x80) + { + PC.W++; + IFF &= 0x7f; + } + IFF &= 0x7e; + gbInterrupt &= 0xef; + register_IF &= 0xef; + + gbWriteMemory(--SP.W, PC.B.B1); + gbWriteMemory(--SP.W, PC.B.B0); + + PC.W = 0x60; +} + +void gbSpeedSwitch() +{ + if (gbSpeed == 0) + { + gbSpeed = 1; + GBLCD_MODE_0_CLOCK_TICKS = 51 * 2; //127; //51 * 2; + GBLCD_MODE_1_CLOCK_TICKS = 1140 * 2; + GBLCD_MODE_2_CLOCK_TICKS = 20 * 2; //52; //20 * 2; + GBLCD_MODE_3_CLOCK_TICKS = 43 * 2; //99; //43 * 2; + GBDIV_CLOCK_TICKS = 64 * 2; + GBLY_INCREMENT_CLOCK_TICKS = 114 * 2; + GBTIMER_MODE_0_CLOCK_TICKS = 256; //256*2; + GBTIMER_MODE_1_CLOCK_TICKS = 4; //4*2; + GBTIMER_MODE_2_CLOCK_TICKS = 16; //16*2; + GBTIMER_MODE_3_CLOCK_TICKS = 64; //64*2; + GBSERIAL_CLOCK_TICKS = 128 * 2; + gbDivTicks *= 2; + gbLcdTicks *= 2; + gbLcdLYIncrementTicks *= 2; + // timerTicks *= 2; + // timerClockTicks *= 2; + gbSerialTicks *= 2; + SOUND_CLOCK_TICKS = soundQuality * GB_USE_TICKS_AS * 2; + soundTicks *= 2; + // synchronizeTicks *= 2; + // SYNCHRONIZE_CLOCK_TICKS *= 2; + } + else + { + gbSpeed = 0; + GBLCD_MODE_0_CLOCK_TICKS = 51; + GBLCD_MODE_1_CLOCK_TICKS = 1140; + GBLCD_MODE_2_CLOCK_TICKS = 20; + GBLCD_MODE_3_CLOCK_TICKS = 43; + GBDIV_CLOCK_TICKS = 64; + GBLY_INCREMENT_CLOCK_TICKS = 114; + GBTIMER_MODE_0_CLOCK_TICKS = 256; + GBTIMER_MODE_1_CLOCK_TICKS = 4; + GBTIMER_MODE_2_CLOCK_TICKS = 16; + GBTIMER_MODE_3_CLOCK_TICKS = 64; + GBSERIAL_CLOCK_TICKS = 128; + gbDivTicks /= 2; + gbLcdTicks /= 2; + gbLcdLYIncrementTicks /= 2; + // timerTicks /= 2; + // timerClockTicks /= 2; + gbSerialTicks /= 2; + SOUND_CLOCK_TICKS = soundQuality * GB_USE_TICKS_AS; + soundTicks /= 2; + // synchronizeTicks /= 2; + // SYNCHRONIZE_CLOCK_TICKS /= 2; + } +} + +void gbGetHardwareType() +{ + gbCgbMode = 0; + if (gbRom[0x143] & 0x80) + { + if (gbEmulatorType == 0 || + gbEmulatorType == 1 || + gbEmulatorType == 4 || + gbEmulatorType == 5 || + (gbRom[0x146] != 0x03 && (gbEmulatorType == 2))) + { + gbCgbMode = 1; + } + } + + if (gbSgbMode == 2) + { + gbSgbMode = 0; + return; + } + + gbSgbMode = 0; + if (gbRom[0x146] == 0x03) + { + if (gbEmulatorType == 0 || + gbEmulatorType == 2 || + gbEmulatorType == 5 || + (!(gbRom[0x143] & 0x80) && (gbEmulatorType == 1 || gbEmulatorType == 4))) + gbSgbMode = 1; + } +} + +void gbReset(bool userReset) +{ + // movie must be closed while opening/creating a movie + if (userReset && VBAMovieRecording()) + { + VBAMovieSignalReset(); + return; + } + + if (!VBAMovieActive()) + { + GBSystemCounters.frameCount = 0; + GBSystemCounters.lagCount = 0; + GBSystemCounters.extraCount = 0; + GBSystemCounters.lagged = true; + GBSystemCounters.laggedLast = true; + } + + SP.W = 0xfffe; + AF.W = 0x01b0; + BC.W = 0x0013; + DE.W = 0x00d8; + HL.W = 0x014d; + PC.W = 0x0100; + IFF = 0; + gbInterrupt = 1; + gbInterruptWait = 0; + + register_DIV = 0; + register_TIMA = 0; + register_TMA = 0; + register_TAC = 0; + register_IF = 1; + register_LCDC = 0x91; + register_STAT = 0; + register_SCY = 0; + register_SCX = 0; + register_LY = 0; + register_LYC = 0; + register_DMA = 0; + register_WY = 0; + register_WX = 0; + register_VBK = 0; + register_HDMA1 = 0; + register_HDMA2 = 0; + register_HDMA3 = 0; + register_HDMA4 = 0; + register_HDMA5 = 0; + register_SVBK = 0; + register_IE = 0; + + gbGetHardwareType(); + if (gbCgbMode) + { + if (!gbVram) + gbVram = (u8 *)malloc(0x4000 + 4); + if (!gbWram) + gbWram = (u8 *)malloc(0x8000 + 4); + memset(gbVram, 0, 0x4000 + 4); + memset(gbWram, 0, 0x8000 + 4); + } + else + { + if (gbVram) + { + free(gbVram); + gbVram = NULL; + } + if (gbWram) + { + free(gbWram); + gbWram = NULL; + } + } + + // clean LineBuffer + if (gbLineBuffer) + memset(gbLineBuffer, 0, 160 * sizeof(u16)); + // clean Pix + if (pix) + memset(pix, 0, 4 * 257 * 226); + + if (gbCgbMode) + { + if (gbSgbMode) + { + if (gbEmulatorType == 5) + AF.W = 0xffb0; + else + AF.W = 0x01b0; + BC.W = 0x0013; + DE.W = 0x00d8; + HL.W = 0x014d; + } + else + { + AF.W = 0x11b0; + BC.W = 0x0000; + DE.W = 0xff56; + HL.W = 0x000d; + } + if (gbEmulatorType == 4) + BC.B.B1 |= 0x01; + + register_HDMA5 = 0xff; + gbMemory[0xff68] = 0xc0; + gbMemory[0xff6a] = 0xc0; + + for (int i = 0; i < 64; i++) + gbPalette[i] = 0x7fff; + } + else + { + for (int i = 0; i < 8; i++) + gbPalette[i] = systemGbPalette[gbPaletteOption * 8 + i]; + } + + if (gbSpeed) + { + gbSpeedSwitch(); + gbMemory[0xff4d] = 0; + } + + gbDivTicks = GBDIV_CLOCK_TICKS; + gbLcdMode = 2; + gbLcdTicks = GBLCD_MODE_2_CLOCK_TICKS; + gbLcdLYIncrementTicks = 0; + gbTimerTicks = 0; + gbTimerClockTicks = 0; + gbSerialTicks = 0; + gbSerialBits = 0; + gbSerialOn = 0; + gbWindowLine = -1; + gbTimerOn = 0; + gbTimerMode = 0; + // gbSynchronizeTicks = GBSYNCHRONIZE_CLOCK_TICKS; + gbSpeed = 0; + gbJoymask[0] = gbJoymask[1] = gbJoymask[2] = gbJoymask[3] = 0; + + // FIXME: horrible kludge + memset(s_gbJoymask, 0, sizeof(s_gbJoymask)); + + if (gbCgbMode) + { + gbSpeed = 0; + gbHdmaOn = 0; + gbHdmaSource = 0x0000; + gbHdmaDestination = 0x8000; + gbVramBank = 0; + gbWramBank = 1; + register_LY = 0x90; + gbLcdMode = 1; + } + + if (gbSgbMode) + { + gbSgbReset(); + } + + for (int i = 0; i < 4; i++) + gbBgp[i] = gbObp0[i] = gbObp1[i] = i; + + memset(&gbDataMBC1, 0, sizeof(gbDataMBC1)); + gbDataMBC1.mapperROMBank = 1; + + gbDataMBC2.mapperRAMEnable = 0; + gbDataMBC2.mapperROMBank = 1; + + memset(&gbDataMBC3, 0, 6 * sizeof(int32)); + gbDataMBC3.mapperROMBank = 1; + + memset(&gbDataMBC5, 0, sizeof(gbDataMBC5)); + gbDataMBC5.mapperROMBank = 1; + switch (gbRom[0x147]) + { + case 0x1c: + case 0x1d: + case 0x1e: + gbDataMBC5.isRumbleCartridge = 1; + } + + memset(&gbDataHuC1, 0, sizeof(gbDataHuC1)); + gbDataHuC1.mapperROMBank = 1; + + memset(&gbDataHuC3, 0, sizeof(gbDataHuC3)); + gbDataHuC3.mapperROMBank = 1; + + gbMemoryMap[0x00] = &gbRom[0x0000]; + gbMemoryMap[0x01] = &gbRom[0x1000]; + gbMemoryMap[0x02] = &gbRom[0x2000]; + gbMemoryMap[0x03] = &gbRom[0x3000]; + gbMemoryMap[0x04] = &gbRom[0x4000]; + gbMemoryMap[0x05] = &gbRom[0x5000]; + gbMemoryMap[0x06] = &gbRom[0x6000]; + gbMemoryMap[0x07] = &gbRom[0x7000]; + if (gbCgbMode) + { + gbMemoryMap[0x08] = &gbVram[0x0000]; + gbMemoryMap[0x09] = &gbVram[0x1000]; + gbMemoryMap[0x0a] = &gbMemory[0xa000]; + gbMemoryMap[0x0b] = &gbMemory[0xb000]; + gbMemoryMap[0x0c] = &gbMemory[0xc000]; + gbMemoryMap[0x0d] = &gbWram[0x1000]; + gbMemoryMap[0x0e] = &gbMemory[0xe000]; + gbMemoryMap[0x0f] = &gbMemory[0xf000]; + } + else + { + gbMemoryMap[0x08] = &gbMemory[0x8000]; + gbMemoryMap[0x09] = &gbMemory[0x9000]; + gbMemoryMap[0x0a] = &gbMemory[0xa000]; + gbMemoryMap[0x0b] = &gbMemory[0xb000]; + gbMemoryMap[0x0c] = &gbMemory[0xc000]; + gbMemoryMap[0x0d] = &gbMemory[0xd000]; + gbMemoryMap[0x0e] = &gbMemory[0xe000]; + gbMemoryMap[0x0f] = &gbMemory[0xf000]; + } + + if (gbRam) + { + gbMemoryMap[0x0a] = &gbRam[0x0000]; + gbMemoryMap[0x0b] = &gbRam[0x1000]; + } + + gbSoundReset(); + + systemResetSensor(); + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + gbLastTime = systemGetClock(); + gbFrameCount = 0; + + systemRefreshScreen(); +} + +void gbWriteSaveMBC1(const char *name) +{ + FILE *gzFile = fopen(name, "wb"); + + if (gzFile == NULL) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(gbRam, + 1, + gbRamSize, + gzFile); + + fclose(gzFile); +} + +void gbWriteSaveMBC2(const char *name) +{ + FILE *file = fopen(name, "wb"); + + if (file == NULL) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(&gbMemory[0xa000], + 1, + 256, + file); + + fclose(file); +} + +void gbWriteSaveMBC3(const char *name, bool extendedSave) +{ + FILE *gzFile = fopen(name, "wb"); + + if (gzFile == NULL) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(gbRam, + 1, + gbRamSize, + gzFile); + + if (extendedSave) + { + //assert(sizeof(time_t) == 4); + fwrite(&gbDataMBC3.mapperSeconds, + 1, + 10 * sizeof(int32) + /*sizeof(time_t)*/4, + gzFile); + } + + fclose(gzFile); +} + +void gbWriteSaveMBC5(const char *name) +{ + FILE *gzFile = fopen(name, "wb"); + + if (gzFile == NULL) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(gbRam, + 1, + gbRamSize, + gzFile); + + fclose(gzFile); +} + +void gbWriteSaveMBC7(const char *name) +{ + FILE *file = fopen(name, "wb"); + + if (file == NULL) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), name); + return; + } + + fwrite(&gbMemory[0xa000], + 1, + 256, + file); + + fclose(file); +} + +bool gbReadSaveMBC1(const char *name) +{ + gzFile gzFile = gzopen(name, "rb"); + + if (gzFile == NULL) + { + return false; + } + + int read = gzread(gzFile, + gbRam, + gbRamSize); + + if (read != gbRamSize) + { + systemMessage(MSG_FAILED_TO_READ_SGM, N_("Failed to read complete save game %s (%d)"), name, read); + gzclose(gzFile); + return false; + } + + gzclose(gzFile); + return true; +} + +bool gbReadSaveMBC2(const char *name) +{ + FILE *file = fopen(name, "rb"); + + if (file == NULL) + { + return false; + } + + int read = fread(&gbMemory[0xa000], + 1, + 256, + file); + + if (read != 256) + { + systemMessage(MSG_FAILED_TO_READ_SGM, + N_("Failed to read complete save game %s (%d)"), name, read); + fclose(file); + return false; + } + + fclose(file); + return true; +} + +bool gbReadSaveMBC3(const char *name) +{ + gzFile gzFile = gzopen(name, "rb"); + + if (gzFile == NULL) + { + return false; + } + + int read = gzread(gzFile, + gbRam, + gbRamSize); + + bool res = true; + + if (read != gbRamSize) + { + systemMessage(MSG_FAILED_TO_READ_SGM, + N_("Failed to read complete save game %s (%d)"), name, read); + } + else + { + //assert(sizeof(time_t) == 4); + read = gzread(gzFile, + &gbDataMBC3.mapperSeconds, + sizeof(int32) * 10 + /*sizeof(time_t)*/4); + + if (read != (sizeof(int32) * 10 + /*sizeof(time_t)*/4) && read != 0) + { + systemMessage(MSG_FAILED_TO_READ_RTC, + N_("Failed to read RTC from save game %s (continuing)"), + name); + res = false; + } + } + + gzclose(gzFile); + return res; +} + +bool gbReadSaveMBC5(const char *name) +{ + gzFile gzFile = gzopen(name, "rb"); + + if (gzFile == NULL) + { + return false; + } + + int read = gzread(gzFile, + gbRam, + gbRamSize); + + if (read != gbRamSize) + { + systemMessage(MSG_FAILED_TO_READ_SGM, + N_("Failed to read complete save game %s (%d)"), name, read); + gzclose(gzFile); + return false; + } + + gzclose(gzFile); + return true; +} + +bool gbReadSaveMBC7(const char *name) +{ + FILE *file = fopen(name, "rb"); + + if (file == NULL) + { + return false; + } + + int read = fread(&gbMemory[0xa000], + 1, + 256, + file); + + if (read != 256) + { + systemMessage(MSG_FAILED_TO_READ_SGM, + N_("Failed to read complete save game %s (%d)"), name, read); + fclose(file); + return false; + } + + fclose(file); + return true; +} + +#if 0 +bool gbLoadBIOS(const char *biosFileName, bool useBiosFile) +{ + useBios = false; + if (useBiosFile) + { + useBios = utilLoadBIOS(bios, biosFileName, gbEmulatorType); + if (!useBios) + { + systemMessage(MSG_INVALID_BIOS_FILE_SIZE, N_("Invalid BOOTROM file")); + } + } + return useBios; +} +#endif + +void gbInit() +{ + gbGenFilter(); + gbSgbInit(); // calls gbSgbReset()... whatever + + gbMemory = (u8 *)malloc(65536 + 4); + memset(gbMemory, 0, 65536 + 4); + memset(gbPalette, 0, 2 * 128); + + // HACK: +4 at start to accomodate the 2xSaI filter reading out of bounds of the leftmost pixel + origPix = (u8 *)calloc(1, 4 * 257 * 226 + 4); + pix = origPix + 4; + + gbLineBuffer = (u16 *)malloc(160 * sizeof(u16)); +} + +bool gbWriteBatteryFile(const char *file, bool extendedSave) +{ + if (gbBattery) + { + int type = gbRom[0x147]; + + switch (type) + { + case 0x03: + gbWriteSaveMBC1(file); + break; + case 0x06: + gbWriteSaveMBC2(file); + break; + case 0x0f: + case 0x10: + case 0x13: + gbWriteSaveMBC3(file, extendedSave); + break; + case 0x1b: + case 0x1e: + gbWriteSaveMBC5(file); + break; + case 0x22: + gbWriteSaveMBC7(file); + break; + case 0xff: + gbWriteSaveMBC1(file); + break; + } + } + return true; +} + +bool gbWriteBatteryFile(const char *file) +{ + gbWriteBatteryFile(file, true); + return true; +} + +bool gbWriteBatteryToStream(gzFile gzfile) +{ + // the GB save code is ugly, so rather than convert it all to use gzFiles, just save it to a temp file... +#define TEMP_SAVE_FNAME ("tempvbawrite.sav") + bool retVal = gbWriteBatteryFile(TEMP_SAVE_FNAME, true); + + // ...open the temp file and figure out its size... + FILE *fileTemp = fopen(TEMP_SAVE_FNAME, "rb"); + if (fileTemp == NULL) + return false; + fseek(fileTemp, 0, SEEK_END); + int len = (int) ftell(fileTemp); + + // ...copy over the temp file... + char *temp = new char [len]; + fseek(fileTemp, 0, SEEK_SET); + if (fread(temp, len, 1, fileTemp) != 1) + { + delete [] temp; + fclose(fileTemp); + return false; + } + fclose(fileTemp); + utilGzWrite(gzfile, temp, len); + delete [] temp; + + // ... and delete the temp file + remove(TEMP_SAVE_FNAME); +#undef TEMP_SAVE_FNAME + + return retVal; +} + +bool gbReadBatteryFile(const char *file) +{ + bool res = false; + if (gbBattery) + { + int type = gbRom[0x147]; + + switch (type) + { + case 0x03: + res = gbReadSaveMBC1(file); + break; + case 0x06: + res = gbReadSaveMBC2(file); + break; + case 0x0f: + case 0x10: + case 0x13: + if (!gbReadSaveMBC3(file)) + { + struct tm *lt; + time_t tmp; //Small kludge to get it working on some systems where time_t has size 8. + + if (VBAMovieActive() || VBAMovieLoading()) + { + gbDataMBC3.mapperLastTime = VBAMovieGetId() + VBAMovieGetFrameCounter() / 60; + lt = gmtime(&tmp); + gbDataMBC3.mapperLastTime=(u32)tmp; + } + else + { + time(&tmp); + gbDataMBC3.mapperLastTime=(u32)tmp; + lt = localtime(&tmp); + } + systemScreenMessage(ctime(&tmp), 4); + gbDataMBC3.mapperLastTime=(u32)tmp; + + gbDataMBC3.mapperSeconds = lt->tm_sec; + gbDataMBC3.mapperMinutes = lt->tm_min; + gbDataMBC3.mapperHours = lt->tm_hour; + gbDataMBC3.mapperDays = lt->tm_yday & 255; + gbDataMBC3.mapperControl = (gbDataMBC3.mapperControl & 0xfe) | + (lt->tm_yday > 255 ? 1 : 0); + res = false; + break; + } + time_t tmp; + systemScreenMessage(ctime(&tmp), 4); + gbDataMBC3.mapperLastTime=(u32)tmp; + res = true; + break; + case 0x1b: + case 0x1e: + res = gbReadSaveMBC5(file); + break; + case 0x22: + res = gbReadSaveMBC7(file); + case 0xff: + res = gbReadSaveMBC1(file); + break; + } + } + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + return res; +} + +bool gbReadBatteryFromStream(gzFile gzfile) +{ + // the GB save code is ugly, so rather than convert it all to use gzFiles, just copy it to temp RAM... +#define TEMP_SAVE_FNAME ("tempvbaread.sav") + int pos = gztell(gzfile); + int buflen = 1024; + // ...make a temp file and write it there... + FILE *fileTemp = fopen(TEMP_SAVE_FNAME, "wb"); + if (fileTemp == NULL) + return false; + int gzDeflated; + char *temp = new char [buflen]; + while ((gzDeflated = utilGzRead(gzfile, temp, buflen)) != 0) + { + if (gzDeflated == -1 || fwrite(temp, gzDeflated, 1, fileTemp) != 1) + { + delete [] temp; + fclose(fileTemp); + gzseek(gzfile, pos, SEEK_SET); /// FIXME: leaves pos in gzfile before save instead of after it (everything that + // calls this right now does a seek afterwards so it doesn't matter for now, but it's + // still bad) + return false; + } + } + gzseek(gzfile, pos, SEEK_SET); /// FIXME: leaves pos in gzfile before save instead of after it (everything that calls this + // right now does a seek afterwards so it doesn't matter for now, but it's still bad) + fclose(fileTemp); + delete [] temp; + + // ... load from the temp file... + bool retVal = gbReadBatteryFile(TEMP_SAVE_FNAME); + + // ... and delete the temp file + remove(TEMP_SAVE_FNAME); +#undef TEMP_SAVE_FNAME + + return retVal; +} + +bool gbReadGSASnapshot(const char *fileName) +{ + FILE *file = fopen(fileName, "rb"); + + if (!file) + { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName); + return false; + } + + // long size = ftell(file); + fseek(file, 0x4, SEEK_SET); + char buffer[16]; + char buffer2[16]; + fread(buffer, 1, 15, file); + buffer[15] = 0; + memcpy(buffer2, &gbRom[0x134], 15); + buffer2[15] = 0; + if (memcmp(buffer, buffer2, 15)) + { + systemMessage(MSG_CANNOT_IMPORT_SNAPSHOT_FOR, + N_("Cannot import snapshot for %s. Current game is %s"), + buffer, + buffer2); + fclose(file); + return false; + } + fseek(file, 0x13, SEEK_SET); + int read = 0; + int toRead = 0; + switch (gbRom[0x147]) + { + case 0x03: + case 0x0f: + case 0x10: + case 0x13: + case 0x1b: + case 0x1e: + case 0xff: + read = fread(gbRam, 1, gbRamSize, file); + toRead = gbRamSize; + break; + case 0x06: + case 0x22: + read = fread(&gbMemory[0xa000], 1, 256, file); + toRead = 256; + break; + default: + systemMessage(MSG_UNSUPPORTED_SNAPSHOT_FILE, + N_("Unsupported snapshot file %s"), + fileName); + fclose(file); + return false; + } + fclose(file); + gbReset(); + return true; +} + +variable_desc gbSaveGameStruct[] = +{ + { &PC.W, sizeof(u16) }, + { &SP.W, sizeof(u16) }, + { &AF.W, sizeof(u16) }, + { &BC.W, sizeof(u16) }, + { &DE.W, sizeof(u16) }, + { &HL.W, sizeof(u16) }, + { &IFF, sizeof(u8) }, + { &GBLCD_MODE_0_CLOCK_TICKS, sizeof(int32) }, + { &GBLCD_MODE_1_CLOCK_TICKS, sizeof(int32) }, + { &GBLCD_MODE_2_CLOCK_TICKS, sizeof(int32) }, + { &GBLCD_MODE_3_CLOCK_TICKS, sizeof(int32) }, + { &GBDIV_CLOCK_TICKS, sizeof(int32) }, + { &GBLY_INCREMENT_CLOCK_TICKS, sizeof(int32) }, + { &GBTIMER_MODE_0_CLOCK_TICKS, sizeof(int32) }, + { &GBTIMER_MODE_1_CLOCK_TICKS, sizeof(int32) }, + { &GBTIMER_MODE_2_CLOCK_TICKS, sizeof(int32) }, + { &GBTIMER_MODE_3_CLOCK_TICKS, sizeof(int32) }, + { &GBSERIAL_CLOCK_TICKS, sizeof(int32) }, + { &GBSYNCHRONIZE_CLOCK_TICKS, sizeof(int32) }, + { &gbDivTicks, sizeof(int32) }, + { &gbLcdMode, sizeof(int32) }, + { &gbLcdTicks, sizeof(int32) }, + { &gbLcdLYIncrementTicks, sizeof(int32) }, + { &gbTimerTicks, sizeof(int32) }, + { &gbTimerClockTicks, sizeof(int32) }, + { &gbSerialTicks, sizeof(int32) }, + { &gbSerialBits, sizeof(int32) }, + { &gbInterrupt, sizeof(int32) }, + { &gbInterruptWait, sizeof(int32) }, + { &gbSynchronizeTicks, sizeof(int32) }, + { &gbTimerOn, sizeof(int32) }, + { &gbTimerMode, sizeof(int32) }, + { &gbSerialOn, sizeof(int32) }, + { &gbWindowLine, sizeof(int32) }, + { &gbCgbMode, sizeof(int32) }, + { &gbVramBank, sizeof(int32) }, + { &gbWramBank, sizeof(int32) }, + { &gbHdmaSource, sizeof(int32) }, + { &gbHdmaDestination, sizeof(int32) }, + { &gbHdmaBytes, sizeof(int32) }, + { &gbHdmaOn, sizeof(int32) }, + { &gbSpeed, sizeof(int32) }, + { &gbSgbMode, sizeof(int32) }, + { ®ister_DIV, sizeof(u8) }, + { ®ister_TIMA, sizeof(u8) }, + { ®ister_TMA, sizeof(u8) }, + { ®ister_TAC, sizeof(u8) }, + { ®ister_IF, sizeof(u8) }, + { ®ister_LCDC, sizeof(u8) }, + { ®ister_STAT, sizeof(u8) }, + { ®ister_SCY, sizeof(u8) }, + { ®ister_SCX, sizeof(u8) }, + { ®ister_LY, sizeof(u8) }, + { ®ister_LYC, sizeof(u8) }, + { ®ister_DMA, sizeof(u8) }, + { ®ister_WY, sizeof(u8) }, + { ®ister_WX, sizeof(u8) }, + { ®ister_VBK, sizeof(u8) }, + { ®ister_HDMA1, sizeof(u8) }, + { ®ister_HDMA2, sizeof(u8) }, + { ®ister_HDMA3, sizeof(u8) }, + { ®ister_HDMA4, sizeof(u8) }, + { ®ister_HDMA5, sizeof(u8) }, + { ®ister_SVBK, sizeof(u8) }, + { ®ister_IE, sizeof(u8) }, + { &gbBgp[0], sizeof(u8) }, + { &gbBgp[1], sizeof(u8) }, + { &gbBgp[2], sizeof(u8) }, + { &gbBgp[3], sizeof(u8) }, + { &gbObp0[0], sizeof(u8) }, + { &gbObp0[1], sizeof(u8) }, + { &gbObp0[2], sizeof(u8) }, + { &gbObp0[3], sizeof(u8) }, + { &gbObp1[0], sizeof(u8) }, + { &gbObp1[1], sizeof(u8) }, + { &gbObp1[2], sizeof(u8) }, + { &gbObp1[3], sizeof(u8) }, + { NULL, 0 } +}; + +bool gbWriteSaveStateToStream(gzFile gzFile) +{ + utilWriteInt(gzFile, GBSAVE_GAME_VERSION); + + utilGzWrite(gzFile, &gbRom[0x134], 15); + + utilWriteData(gzFile, gbSaveGameStruct); + + utilGzWrite(gzFile, &IFF, 2); + + if (gbSgbMode) + { + gbSgbSaveGame(gzFile); + } + + utilGzWrite(gzFile, &gbDataMBC1, sizeof(gbDataMBC1)); + utilGzWrite(gzFile, &gbDataMBC2, sizeof(gbDataMBC2)); + //assert(sizeof(time_t) == 4); + utilGzWrite(gzFile, &gbDataMBC3, sizeof(gbDataMBC3)); + utilGzWrite(gzFile, &gbDataMBC5, sizeof(gbDataMBC5)); + utilGzWrite(gzFile, &gbDataHuC1, sizeof(gbDataHuC1)); + utilGzWrite(gzFile, &gbDataHuC3, sizeof(gbDataHuC3)); + + // yes, this definitely needs to be saved, or loading paused games will show a black screen + // this is also necessary to be consistent with what the GBA saving does + utilGzWrite(gzFile, pix, 4 * 257 * 226); + + utilGzWrite(gzFile, gbPalette, 128 * sizeof(u16)); + // todo: remove + utilGzWrite(gzFile, gbPalette, 128 * sizeof(u16)); + + utilGzWrite(gzFile, &gbMemory[0x8000], 0x8000); + + if (gbRamSize && gbRam) + { + utilGzWrite(gzFile, gbRam, gbRamSize); + } + + if (gbCgbMode) + { + utilGzWrite(gzFile, gbVram, 0x4000); + utilGzWrite(gzFile, gbWram, 0x8000); + } + + gbSoundSaveGame(gzFile); + + gbCheatsSaveGame(gzFile); + + // new to re-recording version: + { + extern int32 sensorX, sensorY; + utilGzWrite(gzFile, &sensorX, sizeof(sensorX)); + utilGzWrite(gzFile, &sensorY, sizeof(sensorY)); + utilGzWrite(gzFile, gbJoymask, 4 * sizeof(*gbJoymask)); // this has to be saved or old input will incorrectly get + // carried + // back on loading a snapshot! + + bool8 movieActive = VBAMovieActive(); + utilGzWrite(gzFile, &movieActive, sizeof(movieActive)); + if (movieActive) + { + uint8 *movie_freeze_buf = NULL; + uint32 movie_freeze_size = 0; + + VBAMovieFreeze(&movie_freeze_buf, &movie_freeze_size); + if (movie_freeze_buf) + { + utilGzWrite(gzFile, &movie_freeze_size, sizeof(movie_freeze_size)); + utilGzWrite(gzFile, movie_freeze_buf, movie_freeze_size); + delete [] movie_freeze_buf; + } + else + { + systemMessage(0, N_("Failed to save movie snapshot.")); + return false; + } + } + utilGzWrite(gzFile, &GBSystemCounters.frameCount, sizeof(GBSystemCounters.frameCount)); + } + + // new to rerecording 19.4 wip (svn r22+): + { + utilGzWrite(gzFile, &GBSystemCounters.lagCount, sizeof(GBSystemCounters.lagCount)); + utilGzWrite(gzFile, &GBSystemCounters.lagged, sizeof(GBSystemCounters.lagged)); + utilGzWrite(gzFile, &GBSystemCounters.laggedLast, sizeof(GBSystemCounters.laggedLast)); + } + + return true; +} + +bool gbWriteMemSaveState(char *memory, int available) +{ + gzFile gzFile = utilMemGzOpen(memory, available, "w"); + + if (gzFile == NULL) + { + return false; + } + + bool res = gbWriteSaveStateToStream(gzFile); + + long pos = utilGzTell(gzFile) + 8; + + if (pos >= (available)) + res = false; + + utilGzClose(gzFile); + + return res; +} + +bool gbWriteSaveState(const char *name) +{ + gzFile gzFile = utilGzOpen(name, "wb"); + + if (gzFile == NULL) + return false; + + bool res = gbWriteSaveStateToStream(gzFile); + + utilGzClose(gzFile); + return res; +} + +static int tempStateID = 0; +static int tempFailCount = 0; +static bool backupSafe = true; + +bool gbReadSaveStateFromStream(gzFile gzFile) +{ + int type; + char tempBackupName [128]; + if (backupSafe) + { + sprintf(tempBackupName, "gbatempsave%d.sav", tempStateID++); + gbWriteSaveState(tempBackupName); + } + + int version = utilReadInt(gzFile); + + if (version > GBSAVE_GAME_VERSION || version < 0) + { + systemMessage(MSG_UNSUPPORTED_VB_SGM, + N_("Unsupported VisualBoy save game version %d"), version); + goto failedLoadGB; + } + + u8 romname[20]; + + utilGzRead(gzFile, romname, 15); + + if (memcmp(&gbRom[0x134], romname, 15) != 0) + { + systemMessage(MSG_CANNOT_LOAD_SGM_FOR, + N_("Cannot load save game for %s. Playing %s"), + romname, &gbRom[0x134]); + goto failedLoadGB; + } + + utilReadData(gzFile, gbSaveGameStruct); + + if (version >= GBSAVE_GAME_VERSION_7) + { + utilGzRead(gzFile, &IFF, 2); + } + + if (gbSgbMode) + { + gbSgbReadGame(gzFile, version); + } + else + { + gbSgbMask = 0; // loading a game at the wrong time causes no display + } + + utilGzRead(gzFile, &gbDataMBC1, sizeof(gbDataMBC1)); + utilGzRead(gzFile, &gbDataMBC2, sizeof(gbDataMBC2)); + if (version < GBSAVE_GAME_VERSION_4) + // prior to version 4, there was no adjustment for the time the game + // was last played, so we have less to read. This needs update if the + // structure changes again. + utilGzRead(gzFile, &gbDataMBC3, sizeof(int32) * 10); + else + { + //assert(sizeof(time_t) == 4); + utilGzRead(gzFile, &gbDataMBC3, sizeof(gbDataMBC3)); + } + utilGzRead(gzFile, &gbDataMBC5, sizeof(gbDataMBC5)); + utilGzRead(gzFile, &gbDataHuC1, sizeof(gbDataHuC1)); + utilGzRead(gzFile, &gbDataHuC3, sizeof(gbDataHuC3)); + + if (version >= GBSAVE_GAME_VERSION_12) + { + utilGzRead(gzFile, pix, 4 * 257 * 226); + } + else + { + memset(pix, 0, 257 * 226 * sizeof(u32)); +// if(version < GBSAVE_GAME_VERSION_5) +// utilGzRead(gzFile, pix, 256*224*sizeof(u16)); + } + + if (version < GBSAVE_GAME_VERSION_6) + { + utilGzRead(gzFile, gbPalette, 64 * sizeof(u16)); + } + else + utilGzRead(gzFile, gbPalette, 128 * sizeof(u16)); + + // todo: remove + utilGzRead(gzFile, gbPalette, 128 * sizeof(u16)); + + if (version < GBSAVE_GAME_VERSION_10) + { + if (!gbCgbMode && !gbSgbMode) + { + for (int i = 0; i < 8; i++) + gbPalette[i] = systemGbPalette[gbPaletteOption * 8 + i]; + } + } + + utilGzRead(gzFile, &gbMemory[0x8000], 0x8000); + + if (gbRamSize && gbRam) + { + utilGzRead(gzFile, gbRam, gbRamSize); + } + + gbMemoryMap[0x00] = &gbRom[0x0000]; + gbMemoryMap[0x01] = &gbRom[0x1000]; + gbMemoryMap[0x02] = &gbRom[0x2000]; + gbMemoryMap[0x03] = &gbRom[0x3000]; + gbMemoryMap[0x04] = &gbRom[0x4000]; + gbMemoryMap[0x05] = &gbRom[0x5000]; + gbMemoryMap[0x06] = &gbRom[0x6000]; + gbMemoryMap[0x07] = &gbRom[0x7000]; + gbMemoryMap[0x08] = &gbMemory[0x8000]; + gbMemoryMap[0x09] = &gbMemory[0x9000]; + gbMemoryMap[0x0a] = &gbMemory[0xa000]; + gbMemoryMap[0x0b] = &gbMemory[0xb000]; + gbMemoryMap[0x0c] = &gbMemory[0xc000]; + gbMemoryMap[0x0d] = &gbMemory[0xd000]; + gbMemoryMap[0x0e] = &gbMemory[0xe000]; + gbMemoryMap[0x0f] = &gbMemory[0xf000]; + + type = gbRom[0x147]; + + switch (type) + { + case 0x00: + case 0x01: + case 0x02: + case 0x03: + // MBC 1 + memoryUpdateMapMBC1(); + break; + case 0x05: + case 0x06: + // MBC2 + memoryUpdateMapMBC2(); + break; + case 0x0f: + case 0x10: + case 0x11: + case 0x12: + case 0x13: + // MBC 3 + memoryUpdateMapMBC3(); + break; + case 0x19: + case 0x1a: + case 0x1b: + // MBC5 + memoryUpdateMapMBC5(); + break; + case 0x1c: + case 0x1d: + case 0x1e: + // MBC 5 Rumble + memoryUpdateMapMBC5(); + break; + case 0x22: + // MBC 7 + memoryUpdateMapMBC7(); + break; + case 0xfe: + // HuC3 + memoryUpdateMapHuC3(); + break; + case 0xff: + // HuC1 + memoryUpdateMapHuC1(); + break; + } + + if (gbCgbMode) + { + if (!gbVram) + gbVram = (u8 *)malloc(0x4000 + 4); + if (!gbWram) + gbWram = (u8 *)malloc(0x8000 + 4); + utilGzRead(gzFile, gbVram, 0x4000); + utilGzRead(gzFile, gbWram, 0x8000); + + int value = register_SVBK; + if (value == 0) + value = 1; + + gbMemoryMap[0x08] = &gbVram[register_VBK * 0x2000]; + gbMemoryMap[0x09] = &gbVram[register_VBK * 0x2000 + 0x1000]; + gbMemoryMap[0x0d] = &gbWram[value * 0x1000]; + } + else + { + if (gbVram) + { + free(gbVram); + gbVram = NULL; + } + if (gbWram) + { + free(gbWram); + gbWram = NULL; + } + } + + gbSoundReadGame(version, gzFile); + +#if 0 + if (gbBorderOn) + { + gbSgbRenderBorder(); + } + + systemRefreshScreen(); +#endif + + if (version > GBSAVE_GAME_VERSION_1) + gbCheatsReadGame(gzFile, version); + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + if (version >= GBSAVE_GAME_VERSION_11) // new to re-recording version: + { + extern int32 sensorX, sensorY; // from SDL.cpp + utilGzRead(gzFile, &sensorX, sizeof(sensorX)); + utilGzRead(gzFile, &sensorY, sizeof(sensorY)); + utilGzRead(gzFile, gbJoymask, 4 * sizeof(*gbJoymask)); // this has to be saved or old input will incorrectly get carried + // back on loading a snapshot! + + bool8 movieSnapshot; + utilGzRead(gzFile, &movieSnapshot, sizeof(movieSnapshot)); + if (VBAMovieActive() && !movieSnapshot) + { + systemMessage(0, N_("Can't load a non-movie snapshot while a movie is active.")); + goto failedLoadGB; + } + + if (movieSnapshot) // even if a movie isn't active we still want to parse through this in case other stuff is added + // later on in the save format + { + uint32 movieInputDataSize = 0; + utilGzRead(gzFile, &movieInputDataSize, sizeof(movieInputDataSize)); + uint8 *local_movie_data = new uint8 [movieInputDataSize]; + int readBytes = utilGzRead(gzFile, local_movie_data, movieInputDataSize); + if (readBytes != movieInputDataSize) + { + systemMessage(0, N_("Corrupt movie snapshot.")); + if (local_movie_data) + delete [] local_movie_data; + goto failedLoadGB; + } + int code = VBAMovieUnfreeze(local_movie_data, movieInputDataSize); + if (local_movie_data) + delete [] local_movie_data; + if (code != MOVIE_SUCCESS && VBAMovieActive()) + { + char errStr [1024]; + strcpy(errStr, "Failed to load movie snapshot"); + switch (code) + { + case MOVIE_NOT_FROM_THIS_MOVIE: + strcat(errStr, ";\nSnapshot not from this movie"); break; + case MOVIE_NOT_FROM_A_MOVIE: + strcat(errStr, ";\nNot a movie snapshot"); break; // shouldn't get here... + case MOVIE_SNAPSHOT_INCONSISTENT: + strcat(errStr, ";\nSnapshot inconsistent with movie"); break; + case MOVIE_WRONG_FORMAT: + strcat(errStr, ";\nWrong format"); break; + } + strcat(errStr, "."); + systemMessage(0, N_(errStr)); + goto failedLoadGB; + } + } + utilGzRead(gzFile, &GBSystemCounters.frameCount, sizeof(GBSystemCounters.frameCount)); + } + + if (version >= GBSAVE_GAME_VERSION_13) // new to rerecording 19.4 wip (svn r22+): + { + utilGzRead(gzFile, &GBSystemCounters.lagCount, sizeof(GBSystemCounters.lagCount)); + utilGzRead(gzFile, &GBSystemCounters.lagged, sizeof(GBSystemCounters.lagged)); + utilGzRead(gzFile, &GBSystemCounters.laggedLast, sizeof(GBSystemCounters.laggedLast)); + } + + if (backupSafe) + { + remove(tempBackupName); + tempFailCount = 0; + } + + for (int i = 0; i < 4; ++i) + systemSetJoypad(i, gbJoymask[i] & 0xFFFF); + + // FIXME: horrible kludge + memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask)); + + VBAUpdateButtonPressDisplay(); + VBAUpdateFrameCountDisplay(); + systemRefreshScreen(); + return true; + +failedLoadGB: + if (backupSafe) + { + tempFailCount++; + if (tempFailCount < 3) // fail no more than 2 times in a row + gbReadSaveState(tempBackupName); + remove(tempBackupName); + } + return false; +} + +bool gbReadMemSaveState(char *memory, int available) +{ + gzFile gzFile = utilMemGzOpen(memory, available, "r"); + + backupSafe = false; + bool res = gbReadSaveStateFromStream(gzFile); + backupSafe = true; + + utilGzClose(gzFile); + + return res; +} + +bool gbReadSaveState(const char *name) +{ + gzFile gzFile = utilGzOpen(name, "rb"); + + if (gzFile == NULL) + { + return false; + } + + bool res = gbReadSaveStateFromStream(gzFile); + + utilGzClose(gzFile); + + return res; +} + +bool gbWritePNGFile(const char *fileName) +{ + if (gbBorderOn) + return utilWritePNGFile(fileName, 256, 224, pix); + return utilWritePNGFile(fileName, 160, 144, pix); +} + +bool gbWriteBMPFile(const char *fileName) +{ + if (gbBorderOn) + return utilWriteBMPFile(fileName, 256, 224, pix); + return utilWriteBMPFile(fileName, 160, 144, pix); +} + +void gbCleanUp() +{ + newFrame = true; + + GBSystemCounters.frameCount = 0; + GBSystemCounters.lagCount = 0; + GBSystemCounters.extraCount = 0; + GBSystemCounters.lagged = true; + GBSystemCounters.laggedLast = true; + + if (gbRam != NULL) + { + free(gbRam); + gbRam = NULL; + } + + if (gbRom != NULL) + { + free(gbRom); + gbRom = NULL; + } + + if (gbMemory != NULL) + { + free(gbMemory); + gbMemory = NULL; + } + + if (gbLineBuffer != NULL) + { + free(gbLineBuffer); + gbLineBuffer = NULL; + } + + if (origPix != NULL) + { + free(origPix); + origPix = NULL; + } + pix = NULL; + + gbSgbShutdown(); + + if (gbVram != NULL) + { + free(gbVram); + gbVram = NULL; + } + + if (gbWram != NULL) + { + free(gbWram); + gbWram = NULL; + } + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + memset(gbJoymask, 0, sizeof(gbJoymask)); + // FIXME: horrible kludge + memset(s_gbJoymask, 0, sizeof(s_gbJoymask)); + + systemClearJoypads(); + systemResetSensor(); + +// gbLastTime = gbFrameCount = 0; + systemRefreshScreen(); +} + +bool gbLoadRom(const char *szFile) +{ + int size = 0; + + if (gbRom != NULL) + { + gbCleanUp(); + } + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + gbRom = utilLoad(szFile, + utilIsGBImage, + NULL, + size); + if (!gbRom) + return false; + + gbRomSize = size; + + return gbUpdateSizes(); +} + +bool gbUpdateSizes() +{ + if (gbRom[0x148] > 8) + { + systemMessage(MSG_UNSUPPORTED_ROM_SIZE, + N_("Unsupported rom size %02x"), gbRom[0x148]); + return false; + } + + if (gbRomSize < gbRomSizes[gbRom[0x148]]) + { + gbRom = (u8 *)realloc(gbRom, gbRomSizes[gbRom[0x148]]); + } + gbRomSize = gbRomSizes[gbRom[0x148]]; + gbRomSizeMask = gbRomSizesMasks[gbRom[0x148]]; + + if (gbRom[0x149] > 5) + { + systemMessage(MSG_UNSUPPORTED_RAM_SIZE, + N_("Unsupported ram size %02x"), gbRom[0x149]); + return false; + } + + gbRamSize = gbRamSizes[gbRom[0x149]]; + gbRamSizeMask = gbRamSizesMasks[gbRom[0x149]]; + + if (gbRamSize) + { + gbRam = (u8 *)malloc(gbRamSize + 4); + memset(gbRam, 0xFF, gbRamSize + 4); + } + + int type = gbRom[0x147]; + + mapperReadRAM = NULL; + + switch (type) + { + case 0x00: + case 0x01: + case 0x02: + case 0x03: + // MBC 1 + mapper = mapperMBC1ROM; + mapperRAM = mapperMBC1RAM; + break; + case 0x05: + case 0x06: + // MBC2 + mapper = mapperMBC2ROM; + mapperRAM = mapperMBC2RAM; + gbRamSize = 0x200; + gbRamSizeMask = 0x1ff; + break; + case 0x0f: + case 0x10: + case 0x11: + case 0x12: + case 0x13: + // MBC 3 + mapper = mapperMBC3ROM; + mapperRAM = mapperMBC3RAM; + mapperReadRAM = mapperMBC3ReadRAM; + break; + case 0x19: + case 0x1a: + case 0x1b: + // MBC5 + mapper = mapperMBC5ROM; + mapperRAM = mapperMBC5RAM; + break; + case 0x1c: + case 0x1d: + case 0x1e: + // MBC 5 Rumble + mapper = mapperMBC5ROM; + mapperRAM = mapperMBC5RAM; + break; + case 0x22: + // MBC 7 + mapper = mapperMBC7ROM; + mapperRAM = mapperMBC7RAM; + mapperReadRAM = mapperMBC7ReadRAM; + break; + case 0xfe: + // HuC3 + mapper = mapperHuC3ROM; + mapperRAM = mapperHuC3RAM; + mapperReadRAM = mapperHuC3ReadRAM; + break; + case 0xff: + // HuC1 + mapper = mapperHuC1ROM; + mapperRAM = mapperHuC1RAM; + break; + default: + systemMessage(MSG_UNKNOWN_CARTRIDGE_TYPE, + N_("Unknown cartridge type %02x"), type); + return false; + } + + switch (type) + { + case 0x03: + case 0x06: + case 0x0f: + case 0x10: + case 0x13: + case 0x1b: + case 0x1d: + case 0x1e: + case 0x22: + case 0xff: + gbBattery = 1; + break; + } + + gbInit(); + gbReset(); + + return true; +} + +void gbEmulate(int ticksToStop) +{ + gbRegister tempRegister; + u8 tempValue; + s8 offset; + + int clockTicks = 0; + gbDmaTicks = 0; + + register int opcode = 0; + + u32 newmask = 0; + if (newFrame) + { + extern void VBAOnExitingFrameBoundary(); + VBAOnExitingFrameBoundary(); + + // update joystick information + systemReadJoypads(); + + bool sensor = (gbRom[0x147] == 0x22); + + // read joystick + if (gbSgbMode && gbSgbMultiplayer) + { + if (gbSgbFourPlayers) + { + gbJoymask[0] = systemGetJoypad(0, sensor); + gbJoymask[1] = systemGetJoypad(1, false); + gbJoymask[2] = systemGetJoypad(2, false); + gbJoymask[3] = systemGetJoypad(3, false); + } + else + { + gbJoymask[0] = systemGetJoypad(0, sensor); + gbJoymask[1] = systemGetJoypad(1, false); + } + } + else + { + gbJoymask[0] = systemGetJoypad(0, sensor); + } + + // FIXME: horrible kludge + memcpy(s_gbJoymask, gbJoymask, sizeof(gbJoymask)); + +// if (sensor) +// systemUpdateMotionSensor(0); + + newmask = gbJoymask[0]; + if (newmask & 0xFF) + { + gbInterrupt |= 16; + } + + extButtons = (newmask >> 18); + speedup = (extButtons & 1) != 0; + + VBAMovieResetIfRequested(); + + CallRegisteredLuaFunctions(LUACALL_BEFOREEMULATION); + + newFrame = false; + } + + for (;; ) + { +#ifndef FINAL_VERSION + if (systemDebug) + { + if (!(IFF & 0x80)) + { + if (systemDebug > 1) + { + sprintf(gbBuffer, "PC=%04x AF=%04x BC=%04x DE=%04x HL=%04x SP=%04x I=%04x\n", + PC.W, AF.W, BC.W, DE.W, HL.W, SP.W, IFF); + } + else + { + sprintf(gbBuffer, "PC=%04x I=%02x\n", PC.W, IFF); + } + log(gbBuffer); + } + } +#endif + if (IFF & 0x80) + { + if (register_LCDC & 0x80) + { + clockTicks = gbLcdTicks; + } + else + clockTicks = 100; + + if (gbLcdMode == 1 && (gbLcdLYIncrementTicks < clockTicks)) + clockTicks = gbLcdLYIncrementTicks; + + if (gbSerialOn && (gbSerialTicks < clockTicks)) + clockTicks = gbSerialTicks; + + if (gbTimerOn && (gbTimerTicks < clockTicks)) + clockTicks = gbTimerTicks; + + if (soundTicks && (soundTicks < clockTicks)) + clockTicks = soundTicks; + } + else + { + opcode = gbReadOpcode(PC.W); + CallRegisteredLuaMemHook(PC.W, 1, opcode, LUAMEMHOOK_EXEC); + PC.W++; + + if (IFF & 0x100) + { + IFF &= 0xff; + PC.W--; + } + + clockTicks = gbCycles[opcode]; + + switch (opcode) + { + case 0xCB: + // extended opcode + //CallRegisteredLuaMemHook(PC.W, 1, opcode, LUAMEMHOOK_EXEC); // is this desired? + opcode = gbReadOpcode(PC.W++); + clockTicks = gbCyclesCB[opcode]; + switch (opcode) + { +#include "gbCodesCB.h" + } + break; +#include "gbCodes.h" + } + } + + if (!emulating) + return; + + if (gbDmaTicks) + { + clockTicks += gbDmaTicks; + gbDmaTicks = 0; + } + + if (gbSgbMode) + { + if (gbSgbPacketTimeout) + { + gbSgbPacketTimeout -= clockTicks; + + if (gbSgbPacketTimeout <= 0) + gbSgbResetPacketState(); + } + } + + ticksToStop -= clockTicks; + + // DIV register emulation + gbDivTicks -= clockTicks; + while (gbDivTicks <= 0) + { + register_DIV++; + gbDivTicks += GBDIV_CLOCK_TICKS; + } + + if (register_LCDC & 0x80) + { + // LCD stuff + gbLcdTicks -= clockTicks; + if (gbLcdMode == 1) + { + // during V-BLANK,we need to increment LY at the same rate! + gbLcdLYIncrementTicks -= clockTicks; + while (gbLcdLYIncrementTicks <= 0) + { + gbLcdLYIncrementTicks += GBLY_INCREMENT_CLOCK_TICKS; + + if (register_LY < 153) + { + register_LY++; + + gbCompareLYToLYC(); + + if (register_LY >= 153) + gbLcdLYIncrementTicks = 6; + } + else + { + register_LY = 0x00; + // reset the window line + gbWindowLine = -1; + gbLcdLYIncrementTicks = GBLY_INCREMENT_CLOCK_TICKS * 2; + gbCompareLYToLYC(); + } + } + } + + // our counter is off, see what we need to do + while (gbLcdTicks <= 0) + { + int framesToSkip = systemFramesToSkip(); + + switch (gbLcdMode) + { + case 0: + // H-Blank + register_LY++; + + gbCompareLYToLYC(); + + // check if we reached the V-Blank period + if (register_LY == 144) + { + // Yes, V-Blank + // set the LY increment counter + gbLcdLYIncrementTicks = gbLcdTicks + GBLY_INCREMENT_CLOCK_TICKS; + gbLcdTicks += GBLCD_MODE_1_CLOCK_TICKS; + gbLcdMode = 1; + if (register_LCDC & 0x80) + { + gbInterrupt |= 1; // V-Blank interrupt + gbInterruptWait = 6; + if (register_STAT & 0x10) + gbInterrupt |= 2; + } + + systemFrame(); + + ++gbFrameCount; + u32 currentTime = systemGetClock(); + if (currentTime - gbLastTime >= 1000) + { + systemShowSpeed(int(float(gbFrameCount) * 100000 / (float(currentTime - gbLastTime) * 60) + .5f)); + gbLastTime = currentTime; + gbFrameCount = 0; + } + + ++GBSystemCounters.frameCount; + if (GBSystemCounters.lagged) + { + ++GBSystemCounters.lagCount; + } + GBSystemCounters.laggedLast = GBSystemCounters.lagged; + GBSystemCounters.lagged = true; + + extern void VBAOnEnteringFrameBoundary(); + VBAOnEnteringFrameBoundary(); + + newFrame = true; + + pauseAfterFrameAdvance = systemPauseOnFrame(); + + if (gbFrameSkipCount >= framesToSkip || pauseAfterFrameAdvance) + { + if (gbBorderOn) + gbSgbRenderBorder(); // clear unnecessary things on border (e.g. in-game text message) + + systemRenderFrame(); + gbFrameSkipCount = 0; + + bool capturePressed = (extButtons & 2) != 0; + if (capturePressed && !capturePrevious) + { + captureNumber = systemScreenCapture(captureNumber); + } + capturePrevious = capturePressed && !pauseAfterFrameAdvance; + } + else + { + ++gbFrameSkipCount; + } + + if (pauseAfterFrameAdvance) + { + systemSetPause(true); + } + } + else + { + // go the the OAM being accessed mode + gbLcdTicks += GBLCD_MODE_2_CLOCK_TICKS; + gbLcdMode = 2; + + // only one LCD interrupt per line. may need to generalize... + if (!(register_STAT & 0x40) || + (register_LY != register_LYC)) + { + if ((register_STAT & 0x28) == 0x20) + gbInterrupt |= 2; + } + } + + break; + case 1: + // V-Blank + // next mode is OAM being accessed mode + gbLcdTicks += GBLCD_MODE_2_CLOCK_TICKS; + gbLcdMode = 2; + if (!(register_STAT & 0x40) || + (register_LY != register_LYC)) + { + if ((register_STAT & 0x28) == 0x20) + gbInterrupt |= 2; + } + break; + case 2: + // OAM being accessed mode + + // next mode is OAM and VRAM in use + gbLcdTicks += GBLCD_MODE_3_CLOCK_TICKS; + gbLcdMode = 3; + break; + case 3: + // OAM and VRAM in use + // next mode is H-Blank + if (register_LY < 144) + { + if (!gbSgbMask) + { + if (gbFrameSkipCount >= framesToSkip || pauseAfterFrameAdvance) + { + gbRenderLine(); + gbDrawSprites(); + + switch (systemColorDepth) + { + case 16: + + { + u16 *dest = (u16 *)pix + + (gbBorderLineSkip + 2) * (register_LY + gbBorderRowSkip + 1) + + gbBorderColumnSkip; + for (int x = 0; x < 160; ) + { + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + *dest++ = systemColorMap16[gbLineMix[x++]]; + } + if (gbBorderOn) + dest += gbBorderColumnSkip; + *dest++ = 0; // for filters that read one pixel more + break; + } + case 24: + + { + u8 *dest = (u8 *)pix + + 3 * (gbBorderLineSkip * (register_LY + gbBorderRowSkip) + + gbBorderColumnSkip); + for (int x = 0; x < 160; ) + { + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + *((u32 *)dest) = systemColorMap32[gbLineMix[x++]]; + dest += 3; + } + break; + } + case 32: + + { + u32 *dest = (u32 *)pix + + (gbBorderLineSkip + 1) * (register_LY + gbBorderRowSkip + 1) + + gbBorderColumnSkip; + for (int x = 0; x < 160; ) + { + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + *dest++ = systemColorMap32[gbLineMix[x++]]; + } + break; + } + } + } + } + } + gbLcdTicks += GBLCD_MODE_0_CLOCK_TICKS; + gbLcdMode = 0; + // only one LCD interrupt per line. may need to generalize... + if (!(register_STAT & 0x40) || + (register_LY != register_LYC)) + { + if (register_STAT & 0x08) + gbInterrupt |= 2; + } + if (gbHdmaOn) + { + gbDoHdma(); + } + break; + } + // mark the correct lcd mode on STAT register + register_STAT = (register_STAT & 0xfc) | gbLcdMode; + } + } + + // serial emulation + if (gbSerialOn) + { +#ifdef LINK_EMULATION + if (linkConnected) + { + gbSerialTicks -= clockTicks; + + while (gbSerialTicks <= 0) + { + // increment number of shifted bits + gbSerialBits++; + linkProc(); + if (gbSerialOn && (gbMemory[0xff02] & 1)) + { + if (gbSerialBits == 8) + { + gbSerialBits = 0; + gbMemory[0xff01] = 0xff; + gbMemory[0xff02] &= 0x7f; + gbSerialOn = 0; + gbInterrupt |= 8; + gbSerialTicks = 0; + } + } + gbSerialTicks += GBSERIAL_CLOCK_TICKS; + } + } + else + { +#endif + if (gbMemory[0xff02] & 1) + { + gbSerialTicks -= clockTicks; + + // overflow + while (gbSerialTicks <= 0) + { + // shift serial byte to right and put a 1 bit in its place + // gbMemory[0xff01] = 0x80 | (gbMemory[0xff01]>>1); + // increment number of shifted bits + gbSerialBits++; + if (gbSerialBits == 8) + { + // end of transmission + if (gbSerialFunction) // external device + gbMemory[0xff01] = gbSerialFunction(gbMemory[0xff01]); + else + gbMemory[0xff01] = 0xff; + gbSerialTicks = 0; + gbMemory[0xff02] &= 0x7f; + gbSerialOn = 0; + gbInterrupt |= 8; + gbSerialBits = 0; + } + else + gbSerialTicks += GBSERIAL_CLOCK_TICKS; + } + } +#ifdef LINK_EMULATION + } +#endif + } + + // timer emulation + if (gbTimerOn) + { + gbTimerTicks -= clockTicks; + + while (gbTimerTicks <= 0) + { + register_TIMA++; + + if (register_TIMA == 0) + { + // timer overflow! + + // reload timer modulo + register_TIMA = register_TMA; + + // flag interrupt + gbInterrupt |= 4; + } + + gbTimerTicks += gbTimerClockTicks; + } + } + + /* + if(soundOffFlag) + { + if(synchronize && !speedup) + { + synchronizeTicks -= clockTicks; + + while(synchronizeTicks < 0) + { + synchronizeTicks += SYNCHRONIZE_CLOCK_TICKS; + + DWORD now = timeGetTime(); + gbElapsedTime += (now - timeNow); + + if(gbElapsedTime < 50) + { + DWORD diff = 50 - gbElapsedTime; + Sleep(diff); + timeNow = timeGetTime(); + elapsedTime = timeNow - now - diff; + if((int)elapsedTime < 0) + elapsedTime = 0; + } else + { + timeNow = timeGetTime(); + elapsedTime = 0; + } + } + } + } + */ + + soundTicks -= clockTicks; + while (soundTicks < 0) // must be < 1 when soundtick_t is real data type + { + soundTicks += SOUND_CLOCK_TICKS; + + gbSoundTick(); + } + + register_IF = gbInterrupt; + + if (IFF & 0x20) + { + IFF &= 0xdf; + IFF |= 0x01; + gbInterruptWait = 0; + } + else if (gbInterrupt) + { + if (gbInterruptWait == 0) + { + // gbInterruptWait = 0; + + if (IFF & 0x01) + { + if ((gbInterrupt & 1) && (register_IE & 1)) + { + gbVblank_interrupt(); + continue; + } + + if ((gbInterrupt & 2) && (register_IE & 2)) + { + gbLcd_interrupt(); + continue; + } + + if ((gbInterrupt & 4) && (register_IE & 4)) + { + gbTimer_interrupt(); + continue; + } + + if ((gbInterrupt & 8) && (register_IE & 8)) + { + gbSerial_interrupt(); + continue; + } + + if ((gbInterrupt & 16) && (register_IE & 16)) + { + gbJoypad_interrupt(); + continue; + } + } + } + else + { + gbInterruptWait -= clockTicks; + if (gbInterruptWait < 0) + gbInterruptWait = 0; + } + } + + if (useOldFrameTiming) + { + // old timing code + if (ticksToStop > 0) + continue; + } + else + { + if (!newFrame && (register_LCDC & 0x80) != 0) + continue; + } + + if (!(register_LCDC & 0x80)) + { + if (!useOldFrameTiming) + { + // FIXME: since register_LY can be reset to 0 by some games, frame length is variable + // and infinite loops can occurr + // for now, it IS necessary to do something on this condition or games like + // Megaman would freeze upon low-level restart interrupt sequence (Start+Select+A+B). + // the only sensible way to fix this issue is to implement the RIGHT frame timing +#ifdef WANTS_INCOMPLETE_WORKAROUND + if (systemReadJoypads()) + { + if (gbSgbMode && gbSgbMultiplayer) + { + if (gbSgbFourPlayers) + { + gbJoymask[0] = systemGetJoypad(0, false); + gbJoymask[1] = systemGetJoypad(1, false); + gbJoymask[2] = systemGetJoypad(2, false); + gbJoymask[3] = systemGetJoypad(3, false); + } + else + { + gbJoymask[0] = systemGetJoypad(0, false); + gbJoymask[1] = systemGetJoypad(1, false); + } + } + else + { + gbJoymask[0] = systemGetJoypad(0, false); + } + } + else + gbJoymask[0] = gbJoymask[1] = gbJoymask[2] = gbJoymask[3] = 0; +#else +#endif + } + } + + // makes sure frames are really divided across input sampling boundaries which occur at a constant rate + if (newFrame || useOldFrameTiming) + { +/// extern void VBAOnEnteringFrameBoundary(); +/// VBAOnEnteringFrameBoundary(); + + break; + } + } +} + +struct EmulatedSystem GBSystem = +{ + // emuMain + gbEmulate, + // emuReset + gbReset, + // emuCleanUp + gbCleanUp, + // emuReadBattery + gbReadBatteryFile, + // emuWriteBattery + gbWriteBatteryFile, + // emuReadBatteryFromStream + gbReadBatteryFromStream, + // emuWriteBatteryToStream + gbWriteBatteryToStream, + // emuReadState + gbReadSaveState, + // emuWriteState + gbWriteSaveState, + // emuReadStateFromStream + gbReadSaveStateFromStream, + // emuWriteStateToStream + gbWriteSaveStateToStream, + // emuReadMemState + gbReadMemSaveState, + // emuWriteMemState + gbWriteMemSaveState, + // emuWritePNG + gbWritePNGFile, + // emuWriteBMP + gbWriteBMPFile, + // emuUpdateCPSR + NULL, + // emuHasDebugger + false, + // emuCount +#ifdef FINAL_VERSION + 70000 / 4, +#else + 1000, +#endif +}; + +// is there a reason to use more than one set of counters? +EmulatedSystemCounters &GBSystemCounters = systemCounters; + +/* + EmulatedSystemCounters GBSystemCounters = + { + // frameCount + 0, + // lagCount + 0, + // lagged + true, + // laggedLast + true, + }; + */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/GB.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/GB.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,50 @@ +#ifndef VBA_GB_H +#define VBA_GB_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +typedef union +{ + struct + { +#ifdef WORDS_BIGENDIAN + u8 B1, B0; +#else + u8 B0, B1; +#endif + } B; + u16 W; +} gbRegister; + +extern bool gbLoadRom(const char *); +extern void gbEmulate(int); +extern bool gbIsGameboyRom(const char *); +extern void gbSoundReset(); +extern void gbSoundSetQuality(int); +extern void gbReset(bool userReset = false); +extern void gbCleanUp(); +extern bool gbWriteBatteryFile(const char *); +extern bool gbWriteBatteryFile(const char *, bool); +extern bool gbWriteBatteryToStream(gzFile); +extern bool gbReadBatteryFile(const char *); +extern bool gbReadBatteryFromStream(gzFile); +extern bool gbWriteSaveState(const char *); +extern bool gbWriteMemSaveState(char *, int); +extern bool gbReadSaveState(const char *); +extern bool gbReadMemSaveState(char *, int); +extern bool gbReadSaveStateFromStream(gzFile); +extern bool gbWriteSaveStateToStream(gzFile); +extern void gbSgbRenderBorder(); +extern bool gbWritePNGFile(const char *); +extern bool gbWriteBMPFile(const char *); +extern bool gbReadGSASnapshot(const char *); + +extern struct EmulatedSystem GBSystem; +extern struct EmulatedSystemCounters &GBSystemCounters; + +#endif // VBA_GB_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,21 @@ +noinst_LIBRARIES = libgb.a + +libgb_a_SOURCES = \ + gbCheats.cpp \ + gbCheats.h \ + gbCodesCB.h \ + gbCodes.h \ + GB.cpp \ + GB.h \ + gbDis.cpp \ + gbGfx.cpp \ + gbGlobals.cpp \ + gbGlobals.h \ + gbMemory.cpp \ + gbMemory.h \ + gbPrinter.cpp \ + gbPrinter.h \ + gbSGB.cpp \ + gbSGB.h \ + gbSound.cpp \ + gbSound.h diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbCheats.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbCheats.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,477 @@ +#include +#include +#include +#include + +#include "../NLS.h" +#include "../common/System.h" +#include "../common/Util.h" + +#include "gbCheats.h" +#include "gbGlobals.h" + +gbCheat gbCheatList[100]; +int gbCheatNumber = 0; +bool gbCheatMap[0x10000]; + +extern bool8 cheatsEnabled; + +#define GBCHEAT_IS_HEX(a) (((a) >= 'A' && (a) <= 'F') || ((a) >= '0' && (a) <= '9')) +#define GBCHEAT_HEX_VALUE(a) ((a) >= 'A' ? (a) - 'A' + 10 : (a) - '0') + +void gbCheatUpdateMap() +{ + memset(gbCheatMap, 0, 0x10000); + + for (int i = 0; i < gbCheatNumber; i++) + { + if (gbCheatList[i].enabled) + gbCheatMap[gbCheatList[i].address] = true; + } +} + +void gbCheatsSaveGame(gzFile gzFile) +{ + utilWriteInt(gzFile, gbCheatNumber); + if (gbCheatNumber) + utilGzWrite(gzFile, &gbCheatList[0], sizeof(gbCheat)*gbCheatNumber); +} + +void gbCheatsReadGame(gzFile gzFile, int version) +{ + if (version <= 8) + { + int gbGgOn = utilReadInt(gzFile); + + if (gbGgOn) + { + int n = utilReadInt(gzFile); + gbXxCheat tmpCheat; + for (int i = 0; i < n; i++) + { + utilGzRead(gzFile, &tmpCheat, sizeof(gbXxCheat)); + gbAddGgCheat(tmpCheat.cheatCode, tmpCheat.cheatDesc); + } + } + + int gbGsOn = utilReadInt(gzFile); + + if (gbGsOn) + { + int n = utilReadInt(gzFile); + gbXxCheat tmpCheat; + for (int i = 0; i < n; i++) + { + utilGzRead(gzFile, &tmpCheat, sizeof(gbXxCheat)); + gbAddGsCheat(tmpCheat.cheatCode, tmpCheat.cheatDesc); + } + } + } + else + { + gbCheatNumber = utilReadInt(gzFile); + + if (gbCheatNumber) + { + utilGzRead(gzFile, &gbCheatList[0], sizeof(gbCheat)*gbCheatNumber); + } + } + + gbCheatUpdateMap(); +} + +void gbCheatsSaveCheatList(const char *file) +{ + if (gbCheatNumber == 0) + return; + FILE *f = fopen(file, "wb"); + if (f == NULL) + return; + int version = 1; + fwrite(&version, 1, sizeof(version), f); + int type = 1; + fwrite(&type, 1, sizeof(type), f); + fwrite(&gbCheatNumber, 1, sizeof(gbCheatNumber), f); + fwrite(gbCheatList, 1, sizeof(gbCheatList), f); + fclose(f); +} + +bool gbCheatsLoadCheatList(const char *file) +{ + gbCheatNumber = 0; + + gbCheatUpdateMap(); + + int count = 0; + + FILE *f = fopen(file, "rb"); + + if (f == NULL) + return false; + + int version = 0; + + if (fread(&version, 1, sizeof(version), f) != sizeof(version)) + { + fclose(f); + return false; + } + + if (version != 1) + { + systemMessage(MSG_UNSUPPORTED_CHEAT_LIST_VERSION, + N_("Unsupported cheat list version %d"), version); + fclose(f); + return false; + } + + int type = 0; + if (fread(&type, 1, sizeof(type), f) != sizeof(type)) + { + fclose(f); + return false; + } + + if (type != 1) + { + systemMessage(MSG_UNSUPPORTED_CHEAT_LIST_TYPE, + N_("Unsupported cheat list type %d"), type); + fclose(f); + return false; + } + + if (fread(&count, 1, sizeof(count), f) != sizeof(count)) + { + fclose(f); + return false; + } + + if (fread(gbCheatList, 1, sizeof(gbCheatList), f) != sizeof(gbCheatList)) + { + fclose(f); + return false; + } + + fclose(f); + gbCheatNumber = count; + gbCheatUpdateMap(); + + return true; +} + +bool gbVerifyGsCode(const char *code) +{ + int len = strlen(code); + + if (len == 0) + return true; + + if (len != 8) + return false; + + for (int i = 0; i < 8; i++) + if (!GBCHEAT_IS_HEX(code[i])) + return false; + + int address = GBCHEAT_HEX_VALUE(code[6]) << 12 | + GBCHEAT_HEX_VALUE(code[7]) << 8 | + GBCHEAT_HEX_VALUE(code[4]) << 4 | + GBCHEAT_HEX_VALUE(code[5]); + + if (address < 0xa000 || + address > 0xdfff) + return false; + + return true; +} + +void gbAddGsCheat(const char *code, const char *desc) +{ + if (gbCheatNumber > 99) + { + systemMessage(MSG_MAXIMUM_NUMBER_OF_CHEATS, + N_("Maximum number of cheats reached.")); + return; + } + + if (!gbVerifyGsCode(code)) + { + systemMessage(MSG_INVALID_GAMESHARK_CODE, + N_("Invalid GameShark code: %s"), code); + return; + } + + int i = gbCheatNumber; + + strcpy(gbCheatList[i].cheatCode, code); + strcpy(gbCheatList[i].cheatDesc, desc); + + gbCheatList[i].code = GBCHEAT_HEX_VALUE(code[0]) << 4 | + GBCHEAT_HEX_VALUE(code[1]); + + gbCheatList[i].value = GBCHEAT_HEX_VALUE(code[2]) << 4 | + GBCHEAT_HEX_VALUE(code[3]); + + gbCheatList[i].address = GBCHEAT_HEX_VALUE(code[6]) << 12 | + GBCHEAT_HEX_VALUE(code[7]) << 8 | + GBCHEAT_HEX_VALUE(code[4]) << 4 | + GBCHEAT_HEX_VALUE(code[5]); + + gbCheatList[i].compare = 0; + + gbCheatList[i].enabled = true; + + gbCheatMap[gbCheatList[i].address] = true; + + gbCheatNumber++; +} + +bool gbVerifyGgCode(const char *code) +{ + int len = strlen(code); + + if (len != 11 && + len != 7 && + len != 6 && + len != 0) + return false; + + if (len == 0) + return true; + + if (!GBCHEAT_IS_HEX(code[0])) + return false; + if (!GBCHEAT_IS_HEX(code[1])) + return false; + if (!GBCHEAT_IS_HEX(code[2])) + return false; + if (code[3] != '-') + return false; + if (!GBCHEAT_IS_HEX(code[4])) + return false; + if (!GBCHEAT_IS_HEX(code[5])) + return false; + if (!GBCHEAT_IS_HEX(code[6])) + return false; + if (code[7] != 0) + { + if (code[7] != '-') + return false; + if (code[8] != 0) + { + if (!GBCHEAT_IS_HEX(code[8])) + return false; + if (!GBCHEAT_IS_HEX(code[9])) + return false; + if (!GBCHEAT_IS_HEX(code[10])) + return false; + } + } + + // int replace = (GBCHEAT_HEX_VALUE(code[0]) << 4) + + // GBCHEAT_HEX_VALUE(code[1]); + + int address = (GBCHEAT_HEX_VALUE(code[2]) << 8) + + (GBCHEAT_HEX_VALUE(code[4]) << 4) + + (GBCHEAT_HEX_VALUE(code[5])) + + ((GBCHEAT_HEX_VALUE(code[6]) ^ 0x0f) << 12); + + if (address >= 0x8000 && address <= 0x9fff) + return false; + + if (address >= 0xc000) + return false; + + if (code[7] == 0 || code[8] == '0') + return true; + + int compare = (GBCHEAT_HEX_VALUE(code[8]) << 4) + + (GBCHEAT_HEX_VALUE(code[10])); + compare = compare ^ 0xff; + compare = (compare >> 2) | ((compare << 6) & 0xc0); + compare ^= 0x45; + + int cloak = (GBCHEAT_HEX_VALUE(code[8])) ^ (GBCHEAT_HEX_VALUE(code[9])); + + if (cloak >= 1 && cloak <= 7) + return false; + + return true; +} + +void gbAddGgCheat(const char *code, const char *desc) +{ + if (gbCheatNumber > 99) + { + systemMessage(MSG_MAXIMUM_NUMBER_OF_CHEATS, + N_("Maximum number of cheats reached.")); + return; + } + + if (!gbVerifyGgCode(code)) + { + systemMessage(MSG_INVALID_GAMEGENIE_CODE, + N_("Invalid GameGenie code: %s"), code); + return; + } + + int i = gbCheatNumber; + + int len = strlen(code); + + strcpy(gbCheatList[i].cheatCode, code); + strcpy(gbCheatList[i].cheatDesc, desc); + + gbCheatList[i].code = 1; + gbCheatList[i].value = (GBCHEAT_HEX_VALUE(code[0]) << 4) + + GBCHEAT_HEX_VALUE(code[1]); + + gbCheatList[i].address = (GBCHEAT_HEX_VALUE(code[2]) << 8) + + (GBCHEAT_HEX_VALUE(code[4]) << 4) + + (GBCHEAT_HEX_VALUE(code[5])) + + ((GBCHEAT_HEX_VALUE(code[6]) ^ 0x0f) << 12); + + gbCheatList[i].compare = 0; + + if (len != 7 && len != 8) + { + int compare = (GBCHEAT_HEX_VALUE(code[8]) << 4) + + (GBCHEAT_HEX_VALUE(code[10])); + compare = compare ^ 0xff; + compare = (compare >> 2) | ((compare << 6) & 0xc0); + compare ^= 0x45; + + gbCheatList[i].compare = compare; + gbCheatList[i].code = 0; + } + + gbCheatList[i].enabled = true; + + gbCheatMap[gbCheatList[i].address] = true; + + gbCheatNumber++; +} + +void gbCheatRemove(int i) +{ + if (i < 0 || i >= gbCheatNumber) + { + systemMessage(MSG_INVALID_CHEAT_TO_REMOVE, + N_("Invalid cheat to remove %d"), i); + return; + } + + if ((i+1) < gbCheatNumber) + { + memcpy(&gbCheatList[i], &gbCheatList[i+1], sizeof(gbCheat)* + (gbCheatNumber-i-1)); + } + + gbCheatNumber--; + + gbCheatUpdateMap(); +} + +void gbCheatRemoveAll() +{ + gbCheatNumber = 0; + gbCheatUpdateMap(); +} + +void gbCheatEnable(int i) +{ + if (i >= 0 && i < gbCheatNumber) + { + if (!gbCheatList[i].enabled) + { + gbCheatList[i].enabled = true; + gbCheatUpdateMap(); + } + } +} + +void gbCheatDisable(int i) +{ + if (i >= 0 && i < gbCheatNumber) + { + if (gbCheatList[i].enabled) + { + gbCheatList[i].enabled = false; + gbCheatUpdateMap(); + } + } +} + +bool gbCheatReadGSCodeFile(const char *fileName) +{ + FILE *file = fopen(fileName, "rb"); + + if (!file) + return false; + + fseek(file, 0x18, SEEK_SET); + int count = 0; + fread(&count, 1, 2, file); + int dummy = 0; + gbCheatRemoveAll(); + char desc[13]; + char code[9]; + int i; + for (i = 0; i < count; i++) + { + fread(&dummy, 1, 2, file); + fread(desc, 1, 12, file); + desc[12] = 0; + fread(code, 1, 8, file); + code[8] = 0; + gbAddGsCheat(code, desc); + } + + for (i = 0; i < gbCheatNumber; i++) + gbCheatDisable(i); + + fclose(file); + return true; +} + +u8 gbCheatRead(u16 address) +{ + if (!cheatsEnabled) + return gbReadMemoryQuick(address); + + for (int i = 0; i < gbCheatNumber; i++) + { + if (gbCheatList[i].enabled && gbCheatList[i].address == address) + { + switch (gbCheatList[i].code) + { + case 0x100: // GameGenie support + if (gbReadMemoryQuick(address) == gbCheatList[i].compare) + return gbCheatList[i].value; + break; + case 0x00: + case 0x01: + case 0x80: + return gbCheatList[i].value; + case 0x90: + case 0x91: + case 0x92: + case 0x93: + case 0x94: + case 0x95: + case 0x96: + case 0x97: + if (address >= 0xd000 && address < 0xe000) + { + if (((gbMemoryMap[0x0d] - gbWram)/0x1000) == + (gbCheatList[i].code - 0x90)) + return gbCheatList[i].value; + } + else + return gbCheatList[i].value; + } + } + } + return gbReadMemoryQuick(address); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbCheats.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbCheats.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,44 @@ +#ifndef VBA_GB_CHEATS_H +#define VBA_GB_CHEATS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +struct gbXxCheat +{ + char cheatDesc[100]; + char cheatCode[20]; +}; + +struct gbCheat +{ + char cheatCode[20]; + char cheatDesc[32]; + u16 address; + int code; + u8 compare; + u8 value; + bool enabled; +}; + +extern void gbCheatsSaveGame(gzFile); +extern void gbCheatsReadGame(gzFile, int); +extern void gbCheatsSaveCheatList(const char *); +extern bool gbCheatsLoadCheatList(const char *); +extern bool gbCheatReadGSCodeFile(const char *); + +extern void gbAddGsCheat(const char *, const char *); +extern void gbAddGgCheat(const char *, const char *); +extern void gbCheatRemove(int); +extern void gbCheatRemoveAll(); +extern void gbCheatEnable(int); +extern void gbCheatDisable(int); +extern u8 gbCheatRead(u16); + +extern int gbCheatNumber; +extern gbCheat gbCheatList[100]; +extern bool gbCheatMap[0x10000]; + +#endif // VBA_GB_CHEATS_H + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbCodes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbCodes.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1388 @@ + case 0x00: + // NOP + break; + case 0x01: + // LD BC, NNNN + BC.B.B0=gbReadMemory(PC.W++); + BC.B.B1=gbReadMemory(PC.W++); + break; + case 0x02: + // LD (BC),A + gbWriteMemory(BC.W,AF.B.B1); + break; + case 0x03: + // INC BC + BC.W++; + break; + case 0x04: + // INC B + BC.B.B1++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[BC.B.B1]| (BC.B.B1&0x0F? 0:H_FLAG); + break; + case 0x05: + // DEC B + BC.B.B1--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[BC.B.B1]| + ((BC.B.B1&0x0F)==0x0F? H_FLAG:0); + break; + case 0x06: + // LD B, NN + BC.B.B1=gbReadOpcode(PC.W++); + break; + case 0x07: + // RLCA + tempValue=AF.B.B1&0x80? C_FLAG:0; + AF.B.B1=((AF.B.B1<<1)|(AF.B.B1>>7)) & 0xFF; + AF.B.B0=tempValue; + break; + case 0x08: + // LD (NNNN), SP + tempRegister.B.B0=gbReadOpcode(PC.W++); + tempRegister.B.B1=gbReadOpcode(PC.W++); + gbWriteMemory(tempRegister.W++,SP.B.B0); + gbWriteMemory(tempRegister.W,SP.B.B1); + break; + case 0x09: + // ADD HL,BC + tempRegister.W=(HL.W+BC.W)&0xFFFF; + AF.B.B0= (AF.B.B0 & Z_FLAG)| ((HL.W^BC.W^tempRegister.W)&0x1000? H_FLAG:0)| + (((long)HL.W+(long)BC.W)&0x10000? C_FLAG:0); + HL.W=tempRegister.W; + break; + case 0x0a: + // LD A,(BC) + AF.B.B1=gbReadMemory(BC.W); + break; + case 0x0b: + // DEC BC + BC.W--; + break; + case 0x0c: + // INC C + BC.B.B0++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[BC.B.B0]| (BC.B.B0&0x0F? 0:H_FLAG); + break; + case 0x0d: + // DEC C + BC.B.B0--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[BC.B.B0]| + ((BC.B.B0&0x0F)==0x0F? H_FLAG:0); + break; + case 0x0e: + // LD C, NN + BC.B.B0=gbReadOpcode(PC.W++); + break; + case 0x0f: + // RRCA + tempValue=AF.B.B1&0x01; + AF.B.B1=(AF.B.B1>>1)|(tempValue? 0x80:0); + AF.B.B0=(tempValue<<4); + break; + case 0x10: + // STOP + opcode = gbReadOpcode(PC.W++); + if(gbCgbMode) { + if(gbReadMemoryQuick(0xff4d) & 1) { + gbSpeedSwitch(); + + if(gbSpeed == 0) + gbWriteMemoryQuick(0xff4d, 0x00); + else + gbWriteMemoryQuick(0xff4d, 0x80); + } + } + break; + case 0x11: + // LD DE, NNNN + DE.B.B0=gbReadMemory(PC.W++); + DE.B.B1=gbReadMemory(PC.W++); + break; + case 0x12: + // LD (DE),A + gbWriteMemory(DE.W,AF.B.B1); + break; + case 0x13: + // INC DE + DE.W++; + break; + case 0x14: + // INC D + DE.B.B1++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[DE.B.B1]| (DE.B.B1&0x0F? 0:H_FLAG); + break; + case 0x15: + // DEC D + DE.B.B1--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[DE.B.B1]| + ((DE.B.B1&0x0F)==0x0F? H_FLAG:0); + break; + case 0x16: + // LD D,NN + DE.B.B1=gbReadOpcode(PC.W++); + break; + case 0x17: + // RLA + tempValue=AF.B.B1&0x80? C_FLAG:0; + AF.B.B1=((AF.B.B1<<1)|((AF.B.B0&C_FLAG)>>4)) & 0xFF; + AF.B.B0=tempValue; + break; + case 0x18: + // JR NN + PC.W+=(s8)gbReadMemory(PC.W)+1; + break; + case 0x19: + // ADD HL,DE + tempRegister.W=(HL.W+DE.W)&0xFFFF; + AF.B.B0= (AF.B.B0 & Z_FLAG)| ((HL.W^DE.W^tempRegister.W)&0x1000? H_FLAG:0)| + (((long)HL.W+(long)DE.W)&0x10000? C_FLAG:0); + HL.W=tempRegister.W; + break; + case 0x1a: + // LD A,(DE) + AF.B.B1=gbReadMemory(DE.W); + break; + case 0x1b: + // DEC DE + DE.W--; + break; + case 0x1c: + // INC E + DE.B.B0++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[DE.B.B0]| (DE.B.B0&0x0F? 0:H_FLAG); + break; + case 0x1d: + // DEC E + DE.B.B0--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[DE.B.B0]| + ((DE.B.B0&0x0F)==0x0F? H_FLAG:0); + break; + case 0x1e: + // LD E,NN + DE.B.B0=gbReadOpcode(PC.W++); + break; + case 0x1f: + // RRA + tempValue=AF.B.B1&0x01; + AF.B.B1=(AF.B.B1>>1)|(AF.B.B0&C_FLAG? 0x80:0); + AF.B.B0=(tempValue<<4); + break; + case 0x20: + // JR NZ,NN + if(AF.B.B0&Z_FLAG) + PC.W++; + else { + PC.W+=(s8)gbReadMemory(PC.W)+1; + clockTicks++; + } + break; + case 0x21: + // LD HL,NNNN + HL.B.B0=gbReadMemory(PC.W++); + HL.B.B1=gbReadMemory(PC.W++); + break; + case 0x22: + // LDI (HL),A + gbWriteMemory(HL.W++,AF.B.B1); + break; + case 0x23: + // INC HL + HL.W++; + break; + case 0x24: + // INC H + HL.B.B1++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[HL.B.B1]| (HL.B.B1&0x0F? 0:H_FLAG); + break; + case 0x25: + // DEC H + HL.B.B1--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[HL.B.B1]| + ((HL.B.B1&0x0F)==0x0F? H_FLAG:0); + break; + case 0x26: + // LD H,NN + HL.B.B1=gbReadOpcode(PC.W++); + break; + case 0x27: + // DAA + tempRegister.W=AF.B.B1; + if(AF.B.B0&C_FLAG) tempRegister.W|=256; + if(AF.B.B0&H_FLAG) tempRegister.W|=512; + if(AF.B.B0&N_FLAG) tempRegister.W|=1024; + AF.W=DAATable[tempRegister.W]; + break; + case 0x28: + // JR Z,NN + if(AF.B.B0&Z_FLAG) { + PC.W+=(s8)gbReadMemory(PC.W)+1; + clockTicks++; + } else + PC.W++; + break; + case 0x29: + // ADD HL,HL + tempRegister.W=(HL.W+HL.W)&0xFFFF; AF.B.B0= (AF.B.B0 & Z_FLAG)| + ((HL.W^HL.W^tempRegister.W)&0x1000? H_FLAG:0)| + (((long)HL.W+(long)HL.W)&0x10000? C_FLAG:0); + HL.W=tempRegister.W; + break; + case 0x2a: + // LDI A,(HL) + AF.B.B1 = gbReadMemory(HL.W++); + break; + case 0x2b: + // DEC HL + HL.W--; + break; + case 0x2c: + // INC L + HL.B.B0++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[HL.B.B0]| (HL.B.B0&0x0F? 0:H_FLAG); + break; + case 0x2d: + // DEC L + HL.B.B0--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[HL.B.B0]| + ((HL.B.B0&0x0F)==0x0F? H_FLAG:0); + break; + case 0x2e: + // LD L,NN + HL.B.B0=gbReadOpcode(PC.W++); + break; + case 0x2f: + // CPL + AF.B.B1 ^= 255; + AF.B.B0|=N_FLAG|H_FLAG; + break; + case 0x30: + // JR NC,NN + if(AF.B.B0&C_FLAG) + PC.W++; + else { + PC.W+=(s8)gbReadMemory(PC.W)+1; + clockTicks++; + } + break; + case 0x31: + // LD SP,NNNN + SP.B.B0=gbReadMemory(PC.W++); + SP.B.B1=gbReadMemory(PC.W++); + break; + case 0x32: + // LDD (HL),A + gbWriteMemory(HL.W--,AF.B.B1); + break; + case 0x33: + // INC SP + SP.W++; + break; + case 0x34: + // INC (HL) + tempValue=(gbReadMemory(HL.W)+1) & 0xFF; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[tempValue]| (tempValue&0x0F? 0:H_FLAG); + gbWriteMemory(HL.W,tempValue); + break; + case 0x35: + // DEC (HL) + tempValue=(gbReadMemory(HL.W)-1) & 0xFF; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[tempValue]| + ((tempValue&0x0F)==0x0F? H_FLAG:0);gbWriteMemory(HL.W,tempValue); + break; + case 0x36: + // LD (HL),NN + gbWriteMemory(HL.W,gbReadOpcode(PC.W++)); + break; + case 0x37: + // SCF + AF.B.B0 = AF.B.B0 & Z_FLAG | C_FLAG; + break; +case 0x38: + // JR C,NN + if(AF.B.B0&C_FLAG) { + PC.W+=(s8)gbReadMemory(PC.W)+1; + clockTicks ++; + } else + PC.W++; + break; + case 0x39: + // ADD HL,SP + tempRegister.W=(HL.W+SP.W)&0xFFFF; + AF.B.B0= (AF.B.B0 & Z_FLAG)| ((HL.W^SP.W^tempRegister.W)&0x1000? H_FLAG:0)| + (((long)HL.W+(long)SP.W)&0x10000? C_FLAG:0); + HL.W=tempRegister.W; + break; + case 0x3a: + // LDD A,(HL) + AF.B.B1 = gbReadMemory(HL.W--); + break; + case 0x3b: + // DEC SP + SP.W--; + break; + case 0x3c: + // INC A + AF.B.B1++; + AF.B.B0= (AF.B.B0 & C_FLAG)|ZeroTable[AF.B.B1]| (AF.B.B1&0x0F? 0:H_FLAG); + break; + case 0x3d: + // DEC A + AF.B.B1--; + AF.B.B0= N_FLAG|(AF.B.B0 & C_FLAG)|ZeroTable[AF.B.B1]| + ((AF.B.B1&0x0F)==0x0F? H_FLAG:0); + break; + case 0x3e: + // LD A,NN + AF.B.B1=gbReadOpcode(PC.W++); + break; + case 0x3f: + // CCF + AF.B.B0^=C_FLAG;AF.B.B0&=~(N_FLAG|H_FLAG); + break; + case 0x40: + // LD B,B + BC.B.B1=BC.B.B1; + break; + case 0x41: + // LD B,C + BC.B.B1=BC.B.B0; + break; + case 0x42: + // LD B,D + BC.B.B1=DE.B.B1; + break; + case 0x43: + // LD B,E + BC.B.B1=DE.B.B0; + break; + case 0x44: + // LD B,H + BC.B.B1=HL.B.B1; + break; + case 0x45: + // LD B,L + BC.B.B1=HL.B.B0; + break; + case 0x46: + // LD B,(HL) + BC.B.B1=gbReadMemory(HL.W); + break; + case 0x47: + // LD B,A + BC.B.B1=AF.B.B1; + break; + case 0x48: + // LD C,B + BC.B.B0=BC.B.B1; + break; + case 0x49: + // LD C,C + BC.B.B0=BC.B.B0; + break; + case 0x4a: + // LD C,D + BC.B.B0=DE.B.B1; + break; + case 0x4b: + // LD C,E + BC.B.B0=DE.B.B0; + break; + case 0x4c: + // LD C,H + BC.B.B0=HL.B.B1; + break; + case 0x4d: + // LD C,L + BC.B.B0=HL.B.B0; + break; + case 0x4e: + // LD C,(HL) + BC.B.B0=gbReadMemory(HL.W); + break; + case 0x4f: + // LD C,A + BC.B.B0=AF.B.B1; + break; + case 0x50: + // LD D,B + DE.B.B1=BC.B.B1; + break; + case 0x51: + // LD D,C + DE.B.B1=BC.B.B0; + break; + case 0x52: + // LD D,D + DE.B.B1=DE.B.B1; + break; + case 0x53: + // LD D,E + DE.B.B1=DE.B.B0; + break; + case 0x54: + // LD D,H + DE.B.B1=HL.B.B1; + break; + case 0x55: + // LD D,L + DE.B.B1=HL.B.B0; + break; + case 0x56: + // LD D,(HL) + DE.B.B1=gbReadMemory(HL.W); + break; + case 0x57: + // LD D,A + DE.B.B1=AF.B.B1; + break; + case 0x58: + // LD E,B + DE.B.B0=BC.B.B1; + break; + case 0x59: + // LD E,C + DE.B.B0=BC.B.B0; + break; + case 0x5a: + // LD E,D + DE.B.B0=DE.B.B1; + break; + case 0x5b: + // LD E,E + DE.B.B0=DE.B.B0; + break; + case 0x5c: + // LD E,H + DE.B.B0=HL.B.B1; + break; + case 0x5d: + // LD E,L + DE.B.B0=HL.B.B0; + break; + case 0x5e: + // LD E,(HL) + DE.B.B0=gbReadMemory(HL.W); + break; + case 0x5f: + // LD E,A + DE.B.B0=AF.B.B1; + break; + case 0x60: + // LD H,B + HL.B.B1=BC.B.B1; + break; + case 0x61: + // LD H,C + HL.B.B1=BC.B.B0; + break; + case 0x62: + // LD H,D + HL.B.B1=DE.B.B1; + break; + case 0x63: + // LD H,E + HL.B.B1=DE.B.B0; + break; + case 0x64: + // LD H,H + HL.B.B1=HL.B.B1; + break; + case 0x65: + // LD H,L + HL.B.B1=HL.B.B0; + break; + case 0x66: + // LD H,(HL) + HL.B.B1=gbReadMemory(HL.W); + break; + case 0x67: + // LD H,A + HL.B.B1=AF.B.B1; + break; + case 0x68: + // LD L,B + HL.B.B0=BC.B.B1; + break; + case 0x69: + // LD L,C + HL.B.B0=BC.B.B0; + break; + case 0x6a: + // LD L,D + HL.B.B0=DE.B.B1; + break; + case 0x6b: + // LD L,E + HL.B.B0=DE.B.B0; + break; + case 0x6c: + // LD L,H + HL.B.B0=HL.B.B1; + break; + case 0x6d: + // LD L,L + HL.B.B0=HL.B.B0; + break; + case 0x6e: + // LD L,(HL) + HL.B.B0=gbReadMemory(HL.W); + break; + case 0x6f: + // LD L,A + HL.B.B0=AF.B.B1; + break; + case 0x70: + // LD (HL),B + gbWriteMemory(HL.W,BC.B.B1); + break; + case 0x71: + // LD (HL),C + gbWriteMemory(HL.W,BC.B.B0); + break; + case 0x72: + // LD (HL),D + gbWriteMemory(HL.W,DE.B.B1); + break; + case 0x73: + // LD (HL),E + gbWriteMemory(HL.W,DE.B.B0); + break; + case 0x74: + // LD (HL),H + gbWriteMemory(HL.W,HL.B.B1); + break; + case 0x75: + // LD (HL),L + gbWriteMemory(HL.W,HL.B.B0); + break; + case 0x76: + // HALT + if(IFF & 1) { + PC.W--; + IFF |= 0x80; + } else { + if((register_IE & register_IF) > 0) + IFF |= 0x100; + else { + PC.W--; + IFF |= 0x81; + } + } + break; + case 0x77: + // LD (HL),A + gbWriteMemory(HL.W,AF.B.B1); + break; + case 0x78: + // LD A,B + AF.B.B1=BC.B.B1; + break; + case 0x79: + // LD A,C + AF.B.B1=BC.B.B0; + break; + case 0x7a: + // LD A,D + AF.B.B1=DE.B.B1; + break; + case 0x7b: + // LD A,E + AF.B.B1=DE.B.B0; + break; + case 0x7c: + // LD A,H + AF.B.B1=HL.B.B1; + break; + case 0x7d: + // LD A,L + AF.B.B1=HL.B.B0; + break; + case 0x7e: + // LD A,(HL) + AF.B.B1=gbReadMemory(HL.W); + break; + case 0x7f: + // LD A,A + AF.B.B1=AF.B.B1; + break; + case 0x80: + // ADD B + tempRegister.W=AF.B.B1+BC.B.B1; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x81: + // ADD C + tempRegister.W=AF.B.B1+BC.B.B0; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x82: + // ADD D + tempRegister.W=AF.B.B1+DE.B.B1; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x83: + // ADD E + tempRegister.W=AF.B.B1+DE.B.B0; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x84: + // ADD H + tempRegister.W=AF.B.B1+HL.B.B1; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x85: + // ADD L + tempRegister.W=AF.B.B1+HL.B.B0; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x86: + // ADD (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1+tempValue; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x87: + // ADD A + tempRegister.W=AF.B.B1+AF.B.B1; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^AF.B.B1^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x88: + // ADC B: + tempRegister.W=AF.B.B1+BC.B.B1+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x89: + // ADC C + tempRegister.W=AF.B.B1+BC.B.B0+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8a: + // ADC D + tempRegister.W=AF.B.B1+DE.B.B1+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8b: + // ADC E + tempRegister.W=AF.B.B1+DE.B.B0+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8c: + // ADC H + tempRegister.W=AF.B.B1+HL.B.B1+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); AF.B.B1=tempRegister.B.B0; + break; + case 0x8d: + // ADC L + tempRegister.W=AF.B.B1+HL.B.B0+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8e: + // ADC (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1+tempValue+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x8f: + // ADC A + tempRegister.W=AF.B.B1+AF.B.B1+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^AF.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x90: + // SUB B + tempRegister.W=AF.B.B1-BC.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x91: + // SUB C + tempRegister.W=AF.B.B1-BC.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x92: + // SUB D + tempRegister.W=AF.B.B1-DE.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x93: + // SUB E + tempRegister.W=AF.B.B1-DE.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x94: + // SUB H + tempRegister.W=AF.B.B1-HL.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x95: + // SUB L + tempRegister.W=AF.B.B1-HL.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x96: + // SUB (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1-tempValue; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x97: + // SUB A + AF.B.B1=0; + AF.B.B0=N_FLAG|Z_FLAG; + break; + case 0x98: + // SBC B + tempRegister.W=AF.B.B1-BC.B.B1-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x99: + // SBC C + tempRegister.W=AF.B.B1-BC.B.B0-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9a: + // SBC D + tempRegister.W=AF.B.B1-DE.B.B1-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9b: + // SBC E + tempRegister.W=AF.B.B1-DE.B.B0-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9c: + // SBC H + tempRegister.W=AF.B.B1-HL.B.B1-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9d: + // SBC L + tempRegister.W=AF.B.B1-HL.B.B0-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9e: + // SBC (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1-tempValue-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0x9f: + // SBC A + tempRegister.W=AF.B.B1-AF.B.B1-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^AF.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xa0: + // AND B + AF.B.B1&=BC.B.B1; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa1: + // AND C + AF.B.B1&=BC.B.B0; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa2: + // AND_D + AF.B.B1&=DE.B.B1; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa3: + // AND E + AF.B.B1&=DE.B.B0; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa4: + // AND H + AF.B.B1&=HL.B.B1; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa5: + // AND L + AF.B.B1&=HL.B.B0; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa6: + // AND (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B1&=tempValue; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa7: + // AND A + AF.B.B1&=AF.B.B1; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xa8: + // XOR B + AF.B.B1^=BC.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xa9: + // XOR C + AF.B.B1^=BC.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xaa: + // XOR D + AF.B.B1^=DE.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xab: + // XOR E + AF.B.B1^=DE.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xac: + // XOR H + AF.B.B1^=HL.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xad: + // XOR L + AF.B.B1^=HL.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xae: + // XOR (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B1^=tempValue; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xaf: + // XOR A + AF.B.B1=0; + AF.B.B0=Z_FLAG; + break; + case 0xb0: + // OR B + AF.B.B1|=BC.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb1: + // OR C + AF.B.B1|=BC.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb2: + // OR D + AF.B.B1|=DE.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb3: + // OR E + AF.B.B1|=DE.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb4: + // OR H + AF.B.B1|=HL.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb5: + // OR L + AF.B.B1|=HL.B.B0; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb6: + // OR (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B1|=tempValue; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb7: + // OR A + AF.B.B1|=AF.B.B1; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xb8: + // CP B: + tempRegister.W=AF.B.B1-BC.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xb9: + // CP C + tempRegister.W=AF.B.B1-BC.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^BC.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xba: + // CP D + tempRegister.W=AF.B.B1-DE.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbb: + // CP E + tempRegister.W=AF.B.B1-DE.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^DE.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbc: + // CP H + tempRegister.W=AF.B.B1-HL.B.B1; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B1^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbd: + // CP L + tempRegister.W=AF.B.B1-HL.B.B0; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^HL.B.B0^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbe: + // CP (HL) + tempValue=gbReadMemory(HL.W); + tempRegister.W=AF.B.B1-tempValue; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xbf: + // CP A + AF.B.B0=N_FLAG|Z_FLAG; + break; + case 0xc0: + // RET NZ + if(!(AF.B.B0&Z_FLAG)) { + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + clockTicks += 3; + } + break; + case 0xc1: + // POP BC + BC.B.B0=gbReadMemory(SP.W++); + BC.B.B1=gbReadMemory(SP.W++); + break; + case 0xc2: + // JP NZ,NNNN + if(AF.B.B0&Z_FLAG) + PC.W+=2; + else { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + clockTicks++; + } + break; + case 0xc3: + // JP NNNN + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + break; + case 0xc4: + // CALL NZ,NNNN + if(AF.B.B0&Z_FLAG) + PC.W+=2; + else { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + clockTicks += 3; + } + break; + case 0xc5: + // PUSH BC + gbWriteMemory(--SP.W,BC.B.B1); + gbWriteMemory(--SP.W,BC.B.B0); + break; + case 0xc6: + // ADD NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1+tempValue; + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10 ? H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xc7: + // RST 00 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0000; + break; + case 0xc8: + // RET Z + if(AF.B.B0&Z_FLAG) { + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + clockTicks += 3; + } + break; + case 0xc9: + // RET + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + break; + case 0xca: + // JP Z,NNNN + if(AF.B.B0&Z_FLAG) { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + clockTicks++; + } else + PC.W+=2; + break; + // CB done outside + case 0xcc: + // CALL Z,NNNN + if(AF.B.B0&Z_FLAG) { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + clockTicks += 3; + } else + PC.W+=2; + break; + case 0xcd: + // CALL NNNN + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + break; + case 0xce: + // ADC NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1+tempValue+(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= (tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xcf: + // RST 08 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0008; + break; + case 0xd0: + // RET NC + if(!(AF.B.B0&C_FLAG)) { + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + clockTicks += 3; + } + break; + case 0xd1: + // POP DE + DE.B.B0=gbReadMemory(SP.W++); + DE.B.B1=gbReadMemory(SP.W++); + break; + case 0xd2: + // JP NC,NNNN + if(AF.B.B0&C_FLAG) + PC.W+=2; + else { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + clockTicks++; + } + break; + // D3 illegal + case 0xd4: + // CALL NC,NNNN + if(AF.B.B0&C_FLAG) + PC.W+=2; + else { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + clockTicks += 3; + } + break; + case 0xd5: + // PUSH DE + gbWriteMemory(--SP.W,DE.B.B1); + gbWriteMemory(--SP.W,DE.B.B0); + break; + case 0xd6: + // SUB NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1-tempValue; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xd7: + // RST 10 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0010; + break; + case 0xd8: + // RET C + if(AF.B.B0&C_FLAG) { + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + clockTicks += 4; + } + break; + case 0xd9: + // RETI + PC.B.B0=gbReadMemory(SP.W++); + PC.B.B1=gbReadMemory(SP.W++); + IFF |= 0x01; + break; + case 0xda: + // JP C,NNNN + if(AF.B.B0&C_FLAG) { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W); + PC.W=tempRegister.W; + clockTicks++; + } else + PC.W+=2; + break; + // DB illegal + case 0xdc: + // CALL C,NNNN + if(AF.B.B0&C_FLAG) { + tempRegister.B.B0=gbReadMemory(PC.W++); + tempRegister.B.B1=gbReadMemory(PC.W++); + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=tempRegister.W; + clockTicks += 3; + } else + PC.W+=2; + break; + // DD illegal + case 0xde: + // SBC NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1-tempValue-(AF.B.B0&C_FLAG ? 1 : 0); + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + AF.B.B1=tempRegister.B.B0; + break; + case 0xdf: + // RST 18 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0018; + break; + case 0xe0: + // LD (FF00+NN),A + gbWriteMemory(0xff00 + gbReadOpcode(PC.W++),AF.B.B1); + break; + case 0xe1: + // POP HL + HL.B.B0=gbReadMemory(SP.W++); + HL.B.B1=gbReadMemory(SP.W++); + break; + case 0xe2: + // LD (FF00+C),A + gbWriteMemory(0xff00 + BC.B.B0,AF.B.B1); + break; + // E3 illegal + // E4 illegal + case 0xe5: + // PUSH HL + gbWriteMemory(--SP.W,HL.B.B1); + gbWriteMemory(--SP.W,HL.B.B0); + break; + case 0xe6: + // AND NN + tempValue=gbReadOpcode(PC.W++); + AF.B.B1&=tempValue; + AF.B.B0=H_FLAG|ZeroTable[AF.B.B1]; + break; + case 0xe7: + // RST 20 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0020; + break; + case 0xe8: + // ADD SP,NN + offset = (s8)gbReadOpcode(PC.W++); + + if(offset >= 0) { + tempRegister.W = SP.W + offset; + AF.B.B0 = (SP.W > tempRegister.W ? C_FLAG : 0) | + ((SP.W^offset^tempRegister.W)&0x1000? H_FLAG:0); + SP.W = tempRegister.W; + } else { + tempRegister.W = SP.W + offset; + AF.B.B0 = (SP.W < tempRegister.W ? C_FLAG : 0) | + ((SP.W^offset^tempRegister.W)&0x1000?H_FLAG:0); + SP.W = tempRegister.W; + } + break; + case 0xe9: + // LD PC,HL + PC.W=HL.W; + break; + case 0xea: + // LD (NNNN),A + tempRegister.B.B0=gbReadOpcode(PC.W++); + tempRegister.B.B1=gbReadOpcode(PC.W++); + gbWriteMemory(tempRegister.W,AF.B.B1); + break; + // EB illegal + // EC illegal + // ED illegal + case 0xee: + // XOR NN + tempValue=gbReadOpcode(PC.W++); + AF.B.B1^=tempValue; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xef: + // RST 28 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0028; + break; + case 0xf0: + // LD A,(FF00+NN) + AF.B.B1 = gbReadMemory(0xff00+gbReadOpcode(PC.W++)); + break; + case 0xf1: + // POP AF + AF.B.B0=gbReadMemory(SP.W++); + AF.B.B1=gbReadMemory(SP.W++); + break; + case 0xf2: + // LD A,(FF00+C) + AF.B.B1 = gbReadMemory(0xff00+BC.B.B0); + break; + case 0xf3: + // DI + // IFF&=0xFE; + IFF&=(~0x21); + break; + // F4 illegal + case 0xf5: + // PUSH AF + gbWriteMemory(--SP.W,AF.B.B1); + gbWriteMemory(--SP.W,AF.B.B0); + break; + case 0xf6: + // OR NN + tempValue=gbReadOpcode(PC.W++); + AF.B.B1|=tempValue; + AF.B.B0=ZeroTable[AF.B.B1]; + break; + case 0xf7: + // RST 30 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0030; + break; + case 0xf8: + // LD HL,SP+NN + offset = (s8)gbReadOpcode(PC.W++); + if(offset >= 0) { + tempRegister.W = SP.W + offset; + AF.B.B0 = (SP.W > tempRegister.W ? C_FLAG : 0) | + ((SP.W^offset^tempRegister.W)&0x1000? H_FLAG:0); + HL.W = tempRegister.W; + } else { + tempRegister.W = SP.W + offset; + AF.B.B0 = (SP.W < tempRegister.W ? C_FLAG : 0) | + ((SP.W^offset^tempRegister.W)&0x1000?H_FLAG:0); + HL.W = tempRegister.W; + } + break; + case 0xf9: + // LD SP,HL + SP.W=HL.W; + break; + case 0xfa: + // LD A,(NNNN) + tempRegister.B.B0=gbReadOpcode(PC.W++); + tempRegister.B.B1=gbReadOpcode(PC.W++); + AF.B.B1=gbReadMemory(tempRegister.W); + break; + case 0xfb: + // EI + IFF|=0x20; + break; + // FC illegal + // FD illegal + case 0xfe: + // CP NN + tempValue=gbReadOpcode(PC.W++); + tempRegister.W=AF.B.B1-tempValue; + AF.B.B0= N_FLAG|(tempRegister.B.B1?C_FLAG:0)|ZeroTable[tempRegister.B.B0]| + ((AF.B.B1^tempValue^tempRegister.B.B0)&0x10?H_FLAG:0); + break; + case 0xff: + // RST 38 + gbWriteMemory(--SP.W,PC.B.B1); + gbWriteMemory(--SP.W,PC.B.B0); + PC.W=0x0038; + break; + default: + systemMessage(0, N_("Unknown opcode %02x at %04x"), + gbReadOpcode(PC.W-1),PC.W-1); + emulating = false; + return; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbCodesCB.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbCodesCB.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1269 @@ + case 0x00: + // RLC B + AF.B.B0 = (BC.B.B1 & 0x80)?C_FLAG:0; + BC.B.B1 = ((BC.B.B1<<1) | (BC.B.B1>>7)) & 0xFF; + AF.B.B0 |= ZeroTable[BC.B.B1]; + break; + case 0x01: + // RLC C + AF.B.B0 = (BC.B.B0 & 0x80)?C_FLAG:0; + BC.B.B0 = ((BC.B.B0<<1) | (BC.B.B0>>7)) & 0xFF; + AF.B.B0 |= ZeroTable[BC.B.B0]; + break; + case 0x02: + // RLC D + AF.B.B0 = (DE.B.B1 & 0x80)?C_FLAG:0; + DE.B.B1 = ((DE.B.B1<<1) | (DE.B.B1>>7)) & 0xFF; + AF.B.B0 |= ZeroTable[DE.B.B1]; + break; + case 0x03: + // RLC E + AF.B.B0 = (DE.B.B0 & 0x80)?C_FLAG:0; + DE.B.B0 = ((DE.B.B0<<1) | (DE.B.B0>>7)) & 0xFF; + AF.B.B0 |= ZeroTable[DE.B.B0]; + break; + case 0x04: + // RLC H + AF.B.B0 = (HL.B.B1 & 0x80)?C_FLAG:0; + HL.B.B1 = ((HL.B.B1<<1) | (HL.B.B1>>7)) & 0xFF; + AF.B.B0 |= ZeroTable[HL.B.B1]; + break; + case 0x05: + // RLC L + AF.B.B0 = (HL.B.B0 & 0x80)?C_FLAG:0; + HL.B.B0 = ((HL.B.B0<<1) | (HL.B.B0>>7)) & 0xFF; + AF.B.B0 |= ZeroTable[HL.B.B0]; + break; + case 0x06: + // RLC (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0 = (tempValue & 0x80)?C_FLAG:0; + tempValue = ((tempValue<<1) | (tempValue>>7)) & 0xFF; + AF.B.B0 |= ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x07: + // RLC A + AF.B.B0 = (AF.B.B1 & 0x80)?C_FLAG:0; + AF.B.B1 = ((AF.B.B1<<1) | (AF.B.B1>>7)) & 0xFF; + AF.B.B0 |= ZeroTable[AF.B.B1]; + break; + case 0x08: + // RRC B + AF.B.B0=(BC.B.B1&0x01 ? C_FLAG : 0); + BC.B.B1=((BC.B.B1>>1)|(BC.B.B1<<7)) & 0xFF; + AF.B.B0|=ZeroTable[BC.B.B1]; + break; + case 0x09: + // RRC C + AF.B.B0=(BC.B.B0&0x01 ? C_FLAG : 0); + BC.B.B0=((BC.B.B0>>1)|(BC.B.B0<<7)) & 0xFF; + AF.B.B0|=ZeroTable[BC.B.B0]; + break; + case 0x0a: + // RRC D + AF.B.B0=(DE.B.B1&0x01 ? C_FLAG : 0); + DE.B.B1=((DE.B.B1>>1)|(DE.B.B1<<7)) & 0xFF; + AF.B.B0|=ZeroTable[DE.B.B1]; + break; + case 0x0b: + // RRC E + AF.B.B0=(DE.B.B0&0x01 ? C_FLAG : 0); + DE.B.B0=((DE.B.B0>>1)|(DE.B.B0<<7)) & 0xFF; + AF.B.B0|=ZeroTable[DE.B.B0]; + break; + case 0x0c: + // RRC H + AF.B.B0=(HL.B.B1&0x01 ? C_FLAG : 0); + HL.B.B1=((HL.B.B1>>1)|(HL.B.B1<<7)) & 0xFF; + AF.B.B0|=ZeroTable[HL.B.B1]; + break; + case 0x0d: + // RRC L + AF.B.B0=(HL.B.B0&0x01 ? C_FLAG : 0); + HL.B.B0=((HL.B.B0>>1)|(HL.B.B0<<7)) & 0xFF; + AF.B.B0|=ZeroTable[HL.B.B0]; + break; + case 0x0e: + // RRC (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(tempValue&0x01 ? C_FLAG : 0); + tempValue=((tempValue>>1)|(tempValue<<7)) & 0xFF; + AF.B.B0|=ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x0f: + // RRC A + AF.B.B0=(AF.B.B1&0x01 ? C_FLAG : 0); + AF.B.B1=((AF.B.B1>>1)|(AF.B.B1<<7)) & 0xFF; + AF.B.B0|=ZeroTable[AF.B.B1]; + break; + case 0x10: + // RL B + if(BC.B.B1&0x80) { + BC.B.B1=((BC.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[BC.B.B1]|C_FLAG; + } else { + BC.B.B1=((BC.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[BC.B.B1]; + } + break; + case 0x11: + // RL C + if(BC.B.B0&0x80) { + BC.B.B0=((BC.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[BC.B.B0]|C_FLAG; + } else { + BC.B.B0=((BC.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[BC.B.B0]; + } + break; + case 0x12: + // RL D + if(DE.B.B1&0x80) { + DE.B.B1=((DE.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[DE.B.B1]|C_FLAG; + } else { + DE.B.B1=((DE.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[DE.B.B1]; + } + break; + case 0x13: + // RL E + if(DE.B.B0&0x80) { + DE.B.B0=((DE.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[DE.B.B0]|C_FLAG; + } else { + DE.B.B0=((DE.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[DE.B.B0]; + } + break; + case 0x14: + // RL H + if(HL.B.B1&0x80) { + HL.B.B1=((HL.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[HL.B.B1]|C_FLAG; + } else { + HL.B.B1=((HL.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[HL.B.B1]; + } + break; + case 0x15: + // RL L + if(HL.B.B0&0x80) { + HL.B.B0=((HL.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[HL.B.B0]|C_FLAG; + } else { + HL.B.B0=((HL.B.B0<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[HL.B.B0]; + } + break; + case 0x16: + // RL (HL) + tempValue=gbReadMemory(HL.W); + if(tempValue&0x80) { + tempValue=((tempValue<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[tempValue]|C_FLAG; + } else { + tempValue=((tempValue<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[tempValue]; + } + gbWriteMemory(HL.W,tempValue); + break; + case 0x17: + // RL A + if(AF.B.B1&0x80) { + AF.B.B1=((AF.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[AF.B.B1]|C_FLAG; + } else { + AF.B.B1=((AF.B.B1<<1)|(AF.B.B0&C_FLAG ? 1 : 0)) & 0xFF; + AF.B.B0=ZeroTable[AF.B.B1]; + } + break; + case 0x18: + // RR B + if(BC.B.B1&0x01) { + BC.B.B1=(BC.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[BC.B.B1]|C_FLAG; + } else { + BC.B.B1=(BC.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[BC.B.B1]; + } + break; + case 0x19: + // RR C + if(BC.B.B0&0x01) { + BC.B.B0=(BC.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[BC.B.B0]|C_FLAG; + } else { + BC.B.B0=(BC.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[BC.B.B0]; + } + break; + case 0x1a: + // RR D + if(DE.B.B1&0x01) { + DE.B.B1=(DE.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[DE.B.B1]|C_FLAG; + } else { + DE.B.B1=(DE.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[DE.B.B1]; + } + break; + case 0x1b: + // RR E + if(DE.B.B0&0x01) { + DE.B.B0=(DE.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[DE.B.B0]|C_FLAG; + } else { + DE.B.B0=(DE.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[DE.B.B0]; + } + break; + case 0x1c: + // RR H + if(HL.B.B1&0x01) { + HL.B.B1=(HL.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[HL.B.B1]|C_FLAG; + } else { + HL.B.B1=(HL.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[HL.B.B1]; + } + break; + case 0x1d: + // RR L + if(HL.B.B0&0x01) { + HL.B.B0=(HL.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[HL.B.B0]|C_FLAG; + } else { + HL.B.B0=(HL.B.B0>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[HL.B.B0]; + } + break; + case 0x1e: + // RR (HL) + tempValue=gbReadMemory(HL.W); + if(tempValue&0x01) { + tempValue=(tempValue>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[tempValue]|C_FLAG; + } else { + tempValue=(tempValue>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[tempValue]; + } + gbWriteMemory(HL.W,tempValue); + break; + case 0x1f: + // RR A + if(AF.B.B1&0x01) { + AF.B.B1=(AF.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[AF.B.B1]|C_FLAG; + } else { + AF.B.B1=(AF.B.B1>>1)|(AF.B.B0 & C_FLAG ? 0x80:0); + AF.B.B0=ZeroTable[AF.B.B1]; + } + break; + case 0x20: + // SLA B + AF.B.B0=(BC.B.B1&0x80?C_FLAG : 0); + BC.B.B1<<=1; + AF.B.B0|=ZeroTable[BC.B.B1]; + break; + case 0x21: + // SLA C + AF.B.B0=(BC.B.B0&0x80?C_FLAG : 0); + BC.B.B0<<=1; + AF.B.B0|=ZeroTable[BC.B.B0]; + break; + case 0x22: + // SLA D + AF.B.B0=(DE.B.B1&0x80?C_FLAG : 0); + DE.B.B1<<=1; + AF.B.B0|=ZeroTable[DE.B.B1]; + break; + case 0x23: + // SLA E + AF.B.B0=(DE.B.B0&0x80?C_FLAG : 0); + DE.B.B0<<=1; + AF.B.B0|=ZeroTable[DE.B.B0]; + break; + case 0x24: + // SLA H + AF.B.B0=(HL.B.B1&0x80?C_FLAG : 0); + HL.B.B1<<=1; + AF.B.B0|=ZeroTable[HL.B.B1]; + break; + case 0x25: + // SLA L + AF.B.B0=(HL.B.B0&0x80?C_FLAG : 0); + HL.B.B0<<=1; + AF.B.B0|=ZeroTable[HL.B.B0]; + break; + case 0x26: + // SLA (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(tempValue&0x80?C_FLAG : 0); + tempValue<<=1; + AF.B.B0|=ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x27: + // SLA A + AF.B.B0=(AF.B.B1&0x80?C_FLAG : 0); + AF.B.B1<<=1; + AF.B.B0|=ZeroTable[AF.B.B1]; + break; + case 0x28: + // SRA B + AF.B.B0=(BC.B.B1&0x01 ? C_FLAG: 0); + BC.B.B1=(BC.B.B1>>1)|(BC.B.B1&0x80); + AF.B.B0|=ZeroTable[BC.B.B1]; + break; + case 0x29: + // SRA C + AF.B.B0=(BC.B.B0&0x01 ? C_FLAG: 0); + BC.B.B0=(BC.B.B0>>1)|(BC.B.B0&0x80); + AF.B.B0|=ZeroTable[BC.B.B0]; + break; + case 0x2a: + // SRA D + AF.B.B0=(DE.B.B1&0x01 ? C_FLAG: 0); + DE.B.B1=(DE.B.B1>>1)|(DE.B.B1&0x80); + AF.B.B0|=ZeroTable[DE.B.B1]; + break; + case 0x2b: + // SRA E + AF.B.B0=(DE.B.B0&0x01 ? C_FLAG: 0); + DE.B.B0=(DE.B.B0>>1)|(DE.B.B0&0x80); + AF.B.B0|=ZeroTable[DE.B.B0]; + break; + case 0x2c: + // SRA H + AF.B.B0=(HL.B.B1&0x01 ? C_FLAG: 0); + HL.B.B1=(HL.B.B1>>1)|(HL.B.B1&0x80); + AF.B.B0|=ZeroTable[HL.B.B1]; + break; + case 0x2d: + // SRA L + AF.B.B0=(HL.B.B0&0x01 ? C_FLAG: 0); + HL.B.B0=(HL.B.B0>>1)|(HL.B.B0&0x80); + AF.B.B0|=ZeroTable[HL.B.B0]; + break; + case 0x2e: + // SRA (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(tempValue&0x01 ? C_FLAG: 0); + tempValue=(tempValue>>1)|(tempValue&0x80); + AF.B.B0|=ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x2f: + // SRA A + AF.B.B0=(AF.B.B1&0x01 ? C_FLAG: 0); + AF.B.B1=(AF.B.B1>>1)|(AF.B.B1&0x80); + AF.B.B0|=ZeroTable[AF.B.B1]; + break; + case 0x30: + // SWAP B + BC.B.B1 = (BC.B.B1&0xf0)>>4 | (BC.B.B1&0x0f)<<4; + AF.B.B0 = ZeroTable[BC.B.B1]; + break; + case 0x31: + // SWAP C + BC.B.B0 = (BC.B.B0&0xf0)>>4 | (BC.B.B0&0x0f)<<4; + AF.B.B0 = ZeroTable[BC.B.B0]; + break; + case 0x32: + // SWAP D + DE.B.B1 = (DE.B.B1&0xf0)>>4 | (DE.B.B1&0x0f)<<4; + AF.B.B0 = ZeroTable[DE.B.B1]; + break; + case 0x33: + // SWAP E + DE.B.B0 = (DE.B.B0&0xf0)>>4 | (DE.B.B0&0x0f)<<4; + AF.B.B0 = ZeroTable[DE.B.B0]; + break; + case 0x34: + // SWAP H + HL.B.B1 = (HL.B.B1&0xf0)>>4 | (HL.B.B1&0x0f)<<4; + AF.B.B0 = ZeroTable[HL.B.B1]; + break; + case 0x35: + // SWAP L + HL.B.B0 = (HL.B.B0&0xf0)>>4 | (HL.B.B0&0x0f)<<4; + AF.B.B0 = ZeroTable[HL.B.B0]; + break; + case 0x36: + // SWAP (HL) + tempValue=gbReadMemory(HL.W); + tempValue = (tempValue&0xf0)>>4 | (tempValue&0x0f)<<4; + AF.B.B0 = ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x37: + // SWAP A + AF.B.B1 = (AF.B.B1&0xf0)>>4 | (AF.B.B1&0x0f)<<4; + AF.B.B0 = ZeroTable[AF.B.B1]; + break; + case 0x38: + // SRL B + AF.B.B0=(BC.B.B1&0x01)?C_FLAG:0; + BC.B.B1>>=1; + AF.B.B0|=ZeroTable[BC.B.B1]; + break; + case 0x39: + // SRL C + AF.B.B0=(BC.B.B0&0x01)?C_FLAG:0; + BC.B.B0>>=1; + AF.B.B0|=ZeroTable[BC.B.B0]; + break; + case 0x3a: + // SRL D + AF.B.B0=(DE.B.B1&0x01)?C_FLAG:0; + DE.B.B1>>=1; + AF.B.B0|=ZeroTable[DE.B.B1]; + break; + case 0x3b: + // SRL E + AF.B.B0=(DE.B.B0&0x01)?C_FLAG:0; + DE.B.B0>>=1; + AF.B.B0|=ZeroTable[DE.B.B0]; + break; + case 0x3c: + // SRL H + AF.B.B0=(HL.B.B1&0x01)?C_FLAG:0; + HL.B.B1>>=1; + AF.B.B0|=ZeroTable[HL.B.B1]; + break; + case 0x3d: + // SRL L + AF.B.B0=(HL.B.B0&0x01)?C_FLAG:0; + HL.B.B0>>=1; + AF.B.B0|=ZeroTable[HL.B.B0]; + break; + case 0x3e: + // SRL (HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(tempValue&0x01)?C_FLAG:0; + tempValue>>=1; + AF.B.B0|=ZeroTable[tempValue]; + gbWriteMemory(HL.W,tempValue); + break; + case 0x3f: + // SRL A + AF.B.B0=(AF.B.B1&0x01)?C_FLAG:0; + AF.B.B1>>=1; + AF.B.B0|=ZeroTable[AF.B.B1]; + break; + case 0x40: + // BIT 0,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<0)? 0:Z_FLAG); + break; + case 0x41: + // BIT 0,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<0)? 0:Z_FLAG); + break; + case 0x42: + // BIT 0,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<0)? 0:Z_FLAG); + break; + case 0x43: + // BIT 0,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<0)? 0:Z_FLAG); + break; + case 0x44: + // BIT 0,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<0)? 0:Z_FLAG); + break; + case 0x45: + // BIT 0,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<0)? 0:Z_FLAG); + break; + case 0x46: + // BIT 0,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<0)? 0:Z_FLAG); + break; + case 0x47: + // BIT 0,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<0)? 0:Z_FLAG); + break; + case 0x48: + // BIT 1,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<1)? 0:Z_FLAG); + break; + case 0x49: + // BIT 1,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<1)? 0:Z_FLAG); + break; + case 0x4a: + // BIT 1,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<1)? 0:Z_FLAG); + break; + case 0x4b: + // BIT 1,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<1)? 0:Z_FLAG); + break; + case 0x4c: + // BIT 1,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<1)? 0:Z_FLAG); + break; + case 0x4d: + // BIT 1,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<1)? 0:Z_FLAG); + break; + case 0x4e: + // BIT 1,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<1)? 0:Z_FLAG); + break; + case 0x4f: + // BIT 1,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<1)? 0:Z_FLAG); + break; + case 0x50: + // BIT 2,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<2)? 0:Z_FLAG); + break; + case 0x51: + // BIT 2,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<2)? 0:Z_FLAG); + break; + case 0x52: + // BIT 2,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<2)? 0:Z_FLAG); + break; + case 0x53: + // BIT 2,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<2)? 0:Z_FLAG); + break; + case 0x54: + // BIT 2,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<2)? 0:Z_FLAG); + break; + case 0x55: + // BIT 2,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<2)? 0:Z_FLAG); + break; + case 0x56: + // BIT 2,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<2)? 0:Z_FLAG); + break; + case 0x57: + // BIT 2,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<2)? 0:Z_FLAG); + break; + case 0x58: + // BIT 3,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<3)? 0:Z_FLAG); + break; + case 0x59: + // BIT 3,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<3)? 0:Z_FLAG); + break; + case 0x5a: + // BIT 3,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<3)? 0:Z_FLAG); + break; + case 0x5b: + // BIT 3,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<3)? 0:Z_FLAG); + break; + case 0x5c: + // BIT 3,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<3)? 0:Z_FLAG); + break; + case 0x5d: + // BIT 3,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<3)? 0:Z_FLAG); + break; + case 0x5e: + // BIT 3,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<3)? 0:Z_FLAG); + break; + case 0x5f: + // BIT 3,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<3)? 0:Z_FLAG); + break; + case 0x60: + // BIT 4,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<4)? 0:Z_FLAG); + break; + case 0x61: + // BIT 4,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<4)? 0:Z_FLAG); + break; + case 0x62: + // BIT 4,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<4)? 0:Z_FLAG); + break; + case 0x63: + // BIT 4,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<4)? 0:Z_FLAG); + break; + case 0x64: + // BIT 4,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<4)? 0:Z_FLAG); + break; + case 0x65: + // BIT 4,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<4)? 0:Z_FLAG); + break; + case 0x66: + // BIT 4,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<4)? 0:Z_FLAG); + break; + case 0x67: + // BIT 4,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<4)? 0:Z_FLAG); + break; + case 0x68: + // BIT 5,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<5)? 0:Z_FLAG); + break; + case 0x69: + // BIT 5,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<5)? 0:Z_FLAG); + break; + case 0x6a: + // BIT 5,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<5)? 0:Z_FLAG); + break; + case 0x6b: + // BIT 5,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<5)? 0:Z_FLAG); + break; + case 0x6c: + // BIT 5,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<5)? 0:Z_FLAG); + break; + case 0x6d: + // BIT 5,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<5)? 0:Z_FLAG); + break; + case 0x6e: + // BIT 5,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<5)? 0:Z_FLAG); + break; + case 0x6f: + // BIT 5,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<5)? 0:Z_FLAG); + break; + case 0x70: + // BIT 6,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<6)? 0:Z_FLAG); + break; + case 0x71: + // BIT 6,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<6)? 0:Z_FLAG); + break; + case 0x72: + // BIT 6,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<6)? 0:Z_FLAG); + break; + case 0x73: + // BIT 6,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<6)? 0:Z_FLAG); + break; + case 0x74: + // BIT 6,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<6)? 0:Z_FLAG); + break; + case 0x75: + // BIT 6,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<6)? 0:Z_FLAG); + break; + case 0x76: + // BIT 6,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<6)? 0:Z_FLAG); + break; + case 0x77: + // BIT 6,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<6)? 0:Z_FLAG); + break; + case 0x78: + // BIT 7,B + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B1&(1<<7)? 0:Z_FLAG); + break; + case 0x79: + // BIT 7,C + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(BC.B.B0&(1<<7)? 0:Z_FLAG); + break; + case 0x7a: + // BIT 7,D + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B1&(1<<7)? 0:Z_FLAG); + break; + case 0x7b: + // BIT 7,E + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(DE.B.B0&(1<<7)? 0:Z_FLAG); + break; + case 0x7c: + // BIT 7,H + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B1&(1<<7)? 0:Z_FLAG); + break; + case 0x7d: + // BIT 7,L + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(HL.B.B0&(1<<7)? 0:Z_FLAG); + break; + case 0x7e: + // BIT 7,(HL) + tempValue=gbReadMemory(HL.W); + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(tempValue&(1<<7)? 0:Z_FLAG); + break; + case 0x7f: + // BIT 7,A + AF.B.B0=(AF.B.B0&C_FLAG)|H_FLAG|(AF.B.B1&(1<<7)? 0:Z_FLAG); + break; + case 0x80: + // RES 0,B + BC.B.B1&=~(1<<0); + break; + case 0x81: + // RES 0,C + BC.B.B0&=~(1<<0); + break; + case 0x82: + // RES 0,D + DE.B.B1&=~(1<<0); + break; + case 0x83: + // RES 0,E + DE.B.B0&=~(1<<0); + break; + case 0x84: + // RES 0,H + HL.B.B1&=~(1<<0); + break; + case 0x85: + // RES 0,L + HL.B.B0&=~(1<<0); + break; + case 0x86: + // RES 0,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<0); + gbWriteMemory(HL.W,tempValue); + break; + case 0x87: + // RES 0,A + AF.B.B1&=~(1<<0); + break; + case 0x88: + // RES 1,B + BC.B.B1&=~(1<<1); + break; + case 0x89: + // RES 1,C + BC.B.B0&=~(1<<1); + break; + case 0x8a: + // RES 1,D + DE.B.B1&=~(1<<1); + break; + case 0x8b: + // RES 1,E + DE.B.B0&=~(1<<1); + break; + case 0x8c: + // RES 1,H + HL.B.B1&=~(1<<1); + break; + case 0x8d: + // RES 1,L + HL.B.B0&=~(1<<1); + break; + case 0x8e: + // RES 1,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<1); + gbWriteMemory(HL.W,tempValue); + break; + case 0x8f: + // RES 1,A + AF.B.B1&=~(1<<1); + break; + case 0x90: + // RES 2,B + BC.B.B1&=~(1<<2); + break; + case 0x91: + // RES 2,C + BC.B.B0&=~(1<<2); + break; + case 0x92: + // RES 2,D + DE.B.B1&=~(1<<2); + break; + case 0x93: + // RES 2,E + DE.B.B0&=~(1<<2); + break; + case 0x94: + // RES 2,H + HL.B.B1&=~(1<<2); + break; + case 0x95: + // RES 2,L + HL.B.B0&=~(1<<2); + break; + case 0x96: + // RES 2,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<2); + gbWriteMemory(HL.W,tempValue); + break; + case 0x97: + // RES 2,A + AF.B.B1&=~(1<<2); + break; + case 0x98: + // RES 3,B + BC.B.B1&=~(1<<3); + break; + case 0x99: + // RES 3,C + BC.B.B0&=~(1<<3); + break; + case 0x9a: + // RES 3,D + DE.B.B1&=~(1<<3); + break; + case 0x9b: + // RES 3,E + DE.B.B0&=~(1<<3); + break; + case 0x9c: + // RES 3,H + HL.B.B1&=~(1<<3); + break; + case 0x9d: + // RES 3,L + HL.B.B0&=~(1<<3); + break; + case 0x9e: + // RES 3,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<3); + gbWriteMemory(HL.W,tempValue); + break; + case 0x9f: + // RES 3,A + AF.B.B1&=~(1<<3); + break; + case 0xa0: + // RES 4,B + BC.B.B1&=~(1<<4); + break; + case 0xa1: + // RES 4,C + BC.B.B0&=~(1<<4); + break; + case 0xa2: + // RES 4,D + DE.B.B1&=~(1<<4); + break; + case 0xa3: + // RES 4,E + DE.B.B0&=~(1<<4); + break; + case 0xa4: + // RES 4,H + HL.B.B1&=~(1<<4); + break; + case 0xa5: + // RES 4,L + HL.B.B0&=~(1<<4); + break; + case 0xa6: + // RES 4,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<4); + gbWriteMemory(HL.W,tempValue); + break; + case 0xa7: + // RES 4,A + AF.B.B1&=~(1<<4); + break; + case 0xa8: + // RES 5,B + BC.B.B1&=~(1<<5); + break; + case 0xa9: + // RES 5,C + BC.B.B0&=~(1<<5); + break; + case 0xaa: + // RES 5,D + DE.B.B1&=~(1<<5); + break; + case 0xab: + // RES 5,E + DE.B.B0&=~(1<<5); + break; + case 0xac: + // RES 5,H + HL.B.B1&=~(1<<5); + break; + case 0xad: + // RES 5,L + HL.B.B0&=~(1<<5); + break; + case 0xae: + // RES 5,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<5); + gbWriteMemory(HL.W,tempValue); + break; + case 0xaf: + // RES 5,A + AF.B.B1&=~(1<<5); + break; + case 0xb0: + // RES 6,B + BC.B.B1&=~(1<<6); + break; + case 0xb1: + // RES 6,C + BC.B.B0&=~(1<<6); + break; + case 0xb2: + // RES 6,D + DE.B.B1&=~(1<<6); + break; + case 0xb3: + // RES 6,E + DE.B.B0&=~(1<<6); + break; + case 0xb4: + // RES 6,H + HL.B.B1&=~(1<<6); + break; + case 0xb5: + // RES 6,L + HL.B.B0&=~(1<<6); + break; + case 0xb6: + // RES 6,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<6); + gbWriteMemory(HL.W,tempValue); + break; + case 0xb7: + // RES 6,A + AF.B.B1&=~(1<<6); + break; + case 0xb8: + // RES 7,B + BC.B.B1&=~(1<<7); + break; + case 0xb9: + // RES 7,C + BC.B.B0&=~(1<<7); + break; + case 0xba: + // RES 7,D + DE.B.B1&=~(1<<7); + break; + case 0xbb: + // RES 7,E + DE.B.B0&=~(1<<7); + break; + case 0xbc: + // RES 7,H + HL.B.B1&=~(1<<7); + break; + case 0xbd: + // RES 7,L + HL.B.B0&=~(1<<7); + break; + case 0xbe: + // RES 7,(HL) + tempValue=gbReadMemory(HL.W); + tempValue&=~(1<<7); + gbWriteMemory(HL.W,tempValue); + break; + case 0xbf: + // RES 7,A + AF.B.B1&=~(1<<7); + break; + case 0xc0: + // SET 0,B + BC.B.B1|=1<<0; + break; + case 0xc1: + // SET 0,C + BC.B.B0|=1<<0; + break; + case 0xc2: + // SET 0,D + DE.B.B1|=1<<0; + break; + case 0xc3: + // SET 0,E + DE.B.B0|=1<<0; + break; + case 0xc4: + // SET 0,H + HL.B.B1|=1<<0; + break; + case 0xc5: + // SET 0,L + HL.B.B0|=1<<0; + break; + case 0xc6: + // SET 0,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<0; + gbWriteMemory(HL.W,tempValue); + break; + case 0xc7: + // SET 0,A + AF.B.B1|=1<<0; + break; + case 0xc8: + // SET 1,B + BC.B.B1|=1<<1; + break; + case 0xc9: + // SET 1,C + BC.B.B0|=1<<1; + break; + case 0xca: + // SET 1,D + DE.B.B1|=1<<1; + break; + case 0xcb: + // SET 1,E + DE.B.B0|=1<<1; + break; + case 0xcc: + // SET 1,H + HL.B.B1|=1<<1; + break; + case 0xcd: + // SET 1,L + HL.B.B0|=1<<1; + break; + case 0xce: + // SET 1,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<1; + gbWriteMemory(HL.W,tempValue); + break; + case 0xcf: + // SET 1,A + AF.B.B1|=1<<1; + break; + case 0xd0: + // SET 2,B + BC.B.B1|=1<<2; + break; + case 0xd1: + // SET 2,C + BC.B.B0|=1<<2; + break; + case 0xd2: + // SET 2,D + DE.B.B1|=1<<2; + break; + case 0xd3: + // SET 2,E + DE.B.B0|=1<<2; + break; + case 0xd4: + // SET 2,H + HL.B.B1|=1<<2; + break; + case 0xd5: + // SET 2,L + HL.B.B0|=1<<2; + break; + case 0xd6: + // SET 2,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<2; + gbWriteMemory(HL.W,tempValue); + break; + case 0xd7: + // SET 2,A + AF.B.B1|=1<<2; + break; + case 0xd8: + // SET 3,B + BC.B.B1|=1<<3; + break; + case 0xd9: + // SET 3,C + BC.B.B0|=1<<3; + break; + case 0xda: + // SET 3,D + DE.B.B1|=1<<3; + break; + case 0xdb: + // SET 3,E + DE.B.B0|=1<<3; + break; + case 0xdc: + // SET 3,H + HL.B.B1|=1<<3; + break; + case 0xdd: + // SET 3,L + HL.B.B0|=1<<3; + break; + case 0xde: + // SET 3,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<3; + gbWriteMemory(HL.W,tempValue); + break; + case 0xdf: + // SET 3,A + AF.B.B1|=1<<3; + break; + case 0xe0: + // SET 4,B + BC.B.B1|=1<<4; + break; + case 0xe1: + // SET 4,C + BC.B.B0|=1<<4; + break; + case 0xe2: + // SET 4,D + DE.B.B1|=1<<4; + break; + case 0xe3: + // SET 4,E + DE.B.B0|=1<<4; + break; + case 0xe4: + // SET 4,H + HL.B.B1|=1<<4; + break; + case 0xe5: + // SET 4,L + HL.B.B0|=1<<4; + break; + case 0xe6: + // SET 4,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<4; + gbWriteMemory(HL.W,tempValue); + break; + case 0xe7: + // SET 4,A + AF.B.B1|=1<<4; + break; + case 0xe8: + // SET 5,B + BC.B.B1|=1<<5; + break; + case 0xe9: + // SET 5,C + BC.B.B0|=1<<5; + break; + case 0xea: + // SET 5,D + DE.B.B1|=1<<5; + break; + case 0xeb: + // SET 5,E + DE.B.B0|=1<<5; + break; + case 0xec: + // SET 5,H + HL.B.B1|=1<<5; + break; + case 0xed: + // SET 5,L + HL.B.B0|=1<<5; + break; + case 0xee: + // SET 5,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<5; + gbWriteMemory(HL.W,tempValue); + break; + case 0xef: + // SET 5,A + AF.B.B1|=1<<5; + break; + case 0xf0: + // SET 6,B + BC.B.B1|=1<<6; + break; + case 0xf1: + // SET 6,C + BC.B.B0|=1<<6; + break; + case 0xf2: + // SET 6,D + DE.B.B1|=1<<6; + break; + case 0xf3: + // SET 6,E + DE.B.B0|=1<<6; + break; + case 0xf4: + // SET 6,H + HL.B.B1|=1<<6; + break; + case 0xf5: + // SET 6,L + HL.B.B0|=1<<6; + break; + case 0xf6: + // SET 6,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<6; + gbWriteMemory(HL.W,tempValue); + break; + case 0xf7: + // SET 6,A + AF.B.B1|=1<<6; + break; + case 0xf8: + // SET 7,B + BC.B.B1|=1<<7; + break; + case 0xf9: + // SET 7,C + BC.B.B0|=1<<7; + break; + case 0xfa: + // SET 7,D + DE.B.B1|=1<<7; + break; + case 0xfb: + // SET 7,E + DE.B.B0|=1<<7; + break; + case 0xfc: + // SET 7,H + HL.B.B1|=1<<7; + break; + case 0xfd: + // SET 7,L + HL.B.B0|=1<<7; + break; + case 0xfe: + // SET 7,(HL) + tempValue=gbReadMemory(HL.W); + tempValue|=1<<7; + gbWriteMemory(HL.W,tempValue); + break; + case 0xff: + // SET 7,A + AF.B.B1|=1<<7; + break; + default: + systemMessage(0, N_("Unknown opcode %02x at %04x"), + gbReadOpcode(PC.W-1),PC.W-1); + emulating = false; + return; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbDis.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbDis.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,239 @@ +#include +#include + +#include "gbGlobals.h" + +typedef struct +{ + u8 mask; + u8 value; + char *mnen; +} GBOPCODE; + +static char *registers[] = +{ "B", "C", "D", "E", "H", "L", "(HL)", "A" }; + +static char *registers16[] = +{ "BC", "DE", "HL", "SP", // for some operations + "BC", "DE", "HL", "AF" }; // for push/pop + +static char *cond[] = +{ "NZ", "Z", "NC", "C" }; + +static char hexDigits[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' +}; + +static GBOPCODE opcodes[] = { + { 0xff, 0x00, "NOP" }, + { 0xcf, 0x01, "LD %R4,%W" }, + { 0xff, 0x02, "LD (BC),A" }, + { 0xcf, 0x03, "INC %R4" }, + { 0xc7, 0x04, "INC %r3" }, + { 0xc7, 0x05, "DEC %r3" }, + { 0xc7, 0x06, "LD %r3,%B" }, + { 0xff, 0x07, "RLCA" }, + { 0xff, 0x08, "LD (%W),SP" }, + { 0xcf, 0x09, "ADD HL,%R4" }, + { 0xff, 0x0a, "LD A,(BC)" }, + { 0xcf, 0x0b, "DEC %R4" }, + { 0xff, 0x0f, "RRCA" }, + { 0xff, 0x10, "STOP" }, + { 0xff, 0x12, "LD (DE),A" }, + { 0xff, 0x17, "RLA" }, + { 0xff, 0x18, "JR %d" }, + { 0xff, 0x1a, "LD A,(DE)" }, + { 0xff, 0x1f, "RRA" }, + { 0xe7, 0x20, "JR %c3,%d" }, + { 0xff, 0x22, "LDI (HL),A" }, + { 0xff, 0x27, "DAA" }, + { 0xff, 0x2a, "LDI A,(HL)" }, + { 0xff, 0x2f, "CPL" }, + { 0xff, 0x32, "LDD (HL),A" }, + { 0xff, 0x37, "SCF" }, + { 0xff, 0x3a, "LDD A,(HL)" }, + { 0xff, 0x3f, "CCF" }, + { 0xff, 0x76, "HALT" }, + { 0xc0, 0x40, "LD %r3,%r0" }, + { 0xf8, 0x80, "ADD A,%r0" }, + { 0xf8, 0x88, "ADC A,%r0" }, + { 0xf8, 0x90, "SUB %r0" }, + { 0xf8, 0x98, "SBC A,%r0" }, + { 0xf8, 0xa0, "AND %r0" }, + { 0xf8, 0xa8, "XOR %r0" }, + { 0xf8, 0xb0, "OR %r0" }, + { 0xf8, 0xb8, "CP %r0" }, + { 0xe7, 0xc0, "RET %c3" }, + { 0xcf, 0xc1, "POP %t4" }, + { 0xe7, 0xc2, "JP %c3,%W" }, + { 0xff, 0xc3, "JP %W" }, + { 0xe7, 0xc4, "CALL %c3,%W" }, + { 0xcf, 0xc5, "PUSH %t4" }, + { 0xff, 0xc6, "ADD A,%B" }, + { 0xc7, 0xc7, "RST %P" }, + { 0xff, 0xc9, "RET" }, + { 0xff, 0xcd, "CALL %W" }, + { 0xff, 0xce, "ADC %B" }, + { 0xff, 0xd6, "SUB %B" }, + { 0xff, 0xd9, "RETI" }, + { 0xff, 0xde, "SBC %B" }, + { 0xff, 0xe0, "LD (FF%B),A" }, + { 0xff, 0xe2, "LD (FF00h+C),A" }, + { 0xff, 0xe6, "AND %B" }, + { 0xff, 0xe8, "ADD SP,%D" }, + { 0xff, 0xe9, "LD PC,HL" }, + { 0xff, 0xea, "LD (%W),A" }, + { 0xff, 0xee, "XOR %B" }, + { 0xff, 0xf0, "LD A,(FF%B)" }, + { 0xff, 0xf2, "LD A,(FF00h+C)" }, + { 0xff, 0xf3, "DI" }, + { 0xff, 0xf6, "OR %B" }, + { 0xff, 0xf8, "LD HL,SP%D" }, + { 0xff, 0xf9, "LD SP,HL" }, + { 0xff, 0xfa, "LD A,(%W)" }, + { 0xff, 0xfb, "EI" }, + { 0xff, 0xfe, "CP %B" }, + { 0x00, 0x00, "DB %B" } +}; + +static GBOPCODE cbOpcodes[] = { + { 0xf8, 0x00, "RLC %r0" }, + { 0xf8, 0x08, "RRC %r0" }, + { 0xf8, 0x10, "RL %r0" }, + { 0xf8, 0x18, "RR %r0" }, + { 0xf8, 0x20, "SLA %r0" }, + { 0xf8, 0x28, "SRA %r0" }, + { 0xf8, 0x30, "SWAP %r0" }, + { 0xf8, 0x38, "SRL %r0" }, + { 0xc0, 0x40, "BIT %b,%r0" }, + { 0xc0, 0x80, "RES %b,%r0" }, + { 0xc0, 0xc0, "SET %b,%r0" }, + { 0x00, 0x00, "DB CBh,%B" } +}; + +static char *addHex(char *p, u8 value) +{ + *p++ = hexDigits[value >> 4]; + *p++ = hexDigits[value & 15]; + return p; +} + +static char *addHex16(char *p, u16 value) +{ + p = addHex(p, value>>8); + return addHex(p, value & 255); +} + +static char *addStr(char *p, char *s) +{ + while (*s) + { + *p++ = *s++; + } + return p; +} + +int gbDis(char *buffer, u16 address) +{ + char *p = buffer; + int instr = 1; + u16 addr = address; + sprintf(p, "%04x ", address); + p += 12; + + u8 opcode = gbReadMemoryQuick(address); + address++; + char * mnen; + GBOPCODE *op; + if (opcode == 0xcb) + { + opcode = gbReadMemoryQuick(address); + address++; + instr++; + op = cbOpcodes; + } + else + { + op = opcodes; + } + while (op->value != (opcode & op->mask)) + op++; + mnen = op->mnen; + + u8 b0, b1; + s8 disp; + int shift; + + while (*mnen) + { + if (*mnen == '%') + { + mnen++; + switch (*mnen++) + { + case 'W': + b0 = gbReadMemoryQuick(address); + address++; + b1 = gbReadMemoryQuick(address); + address++; + p = addHex16(p, b0|b1<<8); + instr += 2; + *p++ = 'h'; + break; + case 'B': + p = addHex(p, gbReadMemoryQuick(address)); + *p++ = 'h'; + address++; + instr++; + break; + case 'D': + disp = gbReadMemoryQuick(address); + if (disp >= 0) + *p++ = '+'; + p += sprintf(p, "%d", disp); + instr++; + break; + case 'd': + disp = gbReadMemoryQuick(address); + address++; + p = addHex16(p, address+disp); + *p++ = 'h'; + instr++; + break; + case 'b': + // kind of a hack, but it works :-) + *p++ = hexDigits[(opcode >> 3) & 7]; + break; + case 'r': + shift = *mnen++ - '0'; + p = addStr(p, registers[(opcode >> shift) & 7]); + break; + case 'R': + shift = *mnen++ - '0'; + p = addStr(p, registers16[(opcode >> shift) & 3]); + break; + case 't': + shift = *mnen++ - '0'; + p = addStr(p, registers16[4+((opcode >> shift) & 3)]); + break; + case 'P': + p = addHex(p, ((opcode >> 3) & 7) * 8); + break; + case 'c': + shift = *mnen++ - '0'; + p = addStr(p, cond[(opcode >> shift) & 3]); + break; + } + } + else + *p++ = *mnen++; + } + for (int i = 0; i < instr; i++) + { + u16 a = addr + i; + addHex(buffer+5+i*2, gbReadMemoryQuick(a)); + } + *p = 0; + return instr; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbGfx.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbGfx.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,552 @@ +#include + +#include "gbGlobals.h" +#include "gbSGB.h" + +extern int32 layerSettings; + +u8 gbInvertTab[256] = { + 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, + 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, + 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, + 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, + 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, + 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, + 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, + 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, + 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, + 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, + 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, + 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, + 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, + 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, + 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, + 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, + 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, + 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, + 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, + 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, + 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, + 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, + 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, + 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, + 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, + 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, + 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, + 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, + 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, + 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, + 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, + 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff +}; + +u16 gbLineMix[160]; + +void gbRenderLine() +{ + u8 *bank0; + u8 *bank1; + if (gbCgbMode) + { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } + else + { + bank0 = &gbMemory[0x8000]; + bank1 = NULL; + } + + int tile_map = 0x1800; + if ((register_LCDC & 8) != 0) + tile_map = 0x1c00; + + int tile_pattern = 0x0800; + + if ((register_LCDC & 16) != 0) + tile_pattern = 0x0000; + + int x = 0; + int y = register_LY; + + if (y >= 144) + return; + + // int yLine = (y + gbBorderRowSkip) * gbBorderLineSkip; + + int sx = register_SCX; + int sy = register_SCY; + + sy += y; + + sy &= 255; + + int tx = sx >> 3; + int ty = sy >> 3; + + int bx = 1 << (7 - (sx & 7)); + int by = sy & 7; + + int tile_map_line_y = tile_map + ty * 32; + + int tile_map_address = tile_map_line_y + tx; + + u8 attrs = 0; + if (bank1 != NULL) + attrs = bank1[tile_map_address]; + + u8 tile = bank0[tile_map_address]; + + tile_map_address++; + + if ((register_LCDC & 16) == 0) + { + if (tile < 128) + tile += 128; + else + tile -= 128; + } + + int tile_pattern_address = tile_pattern + tile * 16 + by*2; + + if (register_LCDC & 0x80) + { + if ((register_LCDC & 0x01 || gbCgbMode) && (layerSettings & 0x0100)) + { + while (x < 160) + { + u8 tile_a = 0; + u8 tile_b = 0; + + if (attrs & 0x40) + { + tile_pattern_address = tile_pattern + tile * 16 + (7-by)*2; + } + + if (attrs & 0x08) + { + tile_a = bank1[tile_pattern_address++]; + tile_b = bank1[tile_pattern_address]; + } + else + { + tile_a = bank0[tile_pattern_address++]; + tile_b = bank0[tile_pattern_address]; + } + + if (attrs & 0x20) + { + tile_a = gbInvertTab[tile_a]; + tile_b = gbInvertTab[tile_b]; + } + + while (bx > 0) + { + u8 c = (tile_a & bx) ? 1 : 0; + c += ((tile_b & bx) ? 2 : 0); + + gbLineBuffer[x] = c; // mark the gbLineBuffer color + + if (attrs & 0x80) + gbLineBuffer[x] |= 0x300; + + if (gbCgbMode) + { + c = c + (attrs & 7)*4; + } + else + { + c = gbBgp[c]; + if (gbSgbMode && !gbCgbMode) + { + int dx = x >> 3; + int dy = y >> 3; + + int palette = gbSgbATF[dy * 20 + dx]; + + if (c == 0) + palette = 0; + + c = c + 4*palette; + } + } + gbLineMix[x] = gbColorOption ? gbColorFilter[gbPalette[c]] : + gbPalette[c]; + x++; + if (x >= 160) + break; + bx >>= 1; + } + tx++; + if (tx == 32) + tx = 0; + bx = 128; + + if (bank1) + attrs = bank1[tile_map_line_y + tx]; + + tile = bank0[tile_map_line_y + tx]; + + if ((register_LCDC & 16) == 0) + { + if (tile < 128) + tile += 128; + else + tile -= 128; + } + tile_pattern_address = tile_pattern + tile * 16 + by * 2; + } + } + else + { + for (int i = 0; i < 160; i++) + { + gbLineMix[i] = gbPalette[0]; + gbLineBuffer[i] = 0; + } + } + + // do the window display + if ((register_LCDC & 0x20) && (layerSettings & 0x2000)) + { + int wy = register_WY; + + if (y >= wy) + { + int wx = register_WX; + wx -= 7; + + if (wx <= 159 && gbWindowLine <= 143) + { + tile_map = 0x1800; + + if ((register_LCDC & 0x40) != 0) + tile_map = 0x1c00; + + if (gbWindowLine == -1) + { + gbWindowLine = 0; + } + + tx = 0; + ty = gbWindowLine >> 3; + + bx = 128; + by = gbWindowLine & 7; + + if (wx < 0) + { + bx >>= (-wx); + wx = 0; + } + + tile_map_line_y = tile_map + ty * 32; + + tile_map_address = tile_map_line_y + tx; + + x = wx; + + tile = bank0[tile_map_address]; + u8 attrs = 0; + if (bank1) + attrs = bank1[tile_map_address]; + tile_map_address++; + + if ((register_LCDC & 16) == 0) + { + if (tile < 128) + tile += 128; + else + tile -= 128; + } + + tile_pattern_address = tile_pattern + tile * 16 + by*2; + + while (x < 160) + { + u8 tile_a = 0; + u8 tile_b = 0; + + if (attrs & 0x40) + { + tile_pattern_address = tile_pattern + tile * 16 + (7-by)*2; + } + + if (attrs & 0x08) + { + tile_a = bank1[tile_pattern_address++]; + tile_b = bank1[tile_pattern_address]; + } + else + { + tile_a = bank0[tile_pattern_address++]; + tile_b = bank0[tile_pattern_address]; + } + + if (attrs & 0x20) + { + tile_a = gbInvertTab[tile_a]; + tile_b = gbInvertTab[tile_b]; + } + + while (bx > 0) + { + u8 c = (tile_a & bx) != 0 ? 1 : 0; + c += ((tile_b & bx) != 0 ? 2 : 0); + + if (attrs & 0x80) + gbLineBuffer[x] = 0x300 + c; + else + gbLineBuffer[x] = 0x100 + c; + + if (gbCgbMode) + { + c = c + (attrs & 7) * 4; + } + else + { + c = gbBgp[c]; + if (gbSgbMode && !gbCgbMode) + { + int dx = x >> 3; + int dy = y >> 3; + + int palette = gbSgbATF[dy * 20 + dx]; + + if (c == 0) + palette = 0; + + c = c + 4*palette; + } + } + gbLineMix[x] = gbColorOption ? gbColorFilter[gbPalette[c]] : + gbPalette[c]; + x++; + if (x >= 160) + break; + bx >>= 1; + } + tx++; + if (tx == 32) + tx = 0; + bx = 128; + tile = bank0[tile_map_line_y + tx]; + if (bank1) + attrs = bank1[tile_map_line_y + tx]; + + if ((register_LCDC & 16) == 0) + { + if (tile < 128) + tile += 128; + else + tile -= 128; + } + tile_pattern_address = tile_pattern + tile * 16 + by * 2; + } + gbWindowLine++; + } + } + } + } + else + { + for (int i = 0; i < 160; i++) + { + gbLineMix[i] = gbPalette[0]; + gbLineBuffer[i] = 0; + } + } +} + +void gbDrawSpriteTile(int tile, int x, int y, int t, int flags, + int size, int spriteNumber) +{ + u8 *bank0; + u8 *bank1; + if (gbCgbMode) + { + if (register_VBK & 1) + { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } + else + { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } + } + else + { + bank0 = &gbMemory[0x8000]; + bank1 = NULL; + } + + int init = 0x0000; + + // int yLine = (y+gbBorderRowSkip) * gbBorderLineSkip; + + u8 *pal = gbObp0; + + int flipx = (flags & 0x20); + int flipy = (flags & 0x40); + + if ((flags & 0x10)) + pal = gbObp1; + + if (flipy) + { + t = (size ? 15 : 7) - t; + } + + int prio = flags & 0x80; + + int address = init + tile * 16 + 2*t; + int a = 0; + int b = 0; + + if (gbCgbMode && flags & 0x08) + { + a = bank1[address++]; + b = bank1[address++]; + } + else + { + a = bank0[address++]; + b = bank0[address++]; + } + + for (int xx = 0; xx < 8; xx++) + { + u8 mask = 1 << (7-xx); + u8 c = 0; + if ((a & mask)) + c++; + if ((b & mask)) + c += 2; + + if (c == 0) + continue; + + int xxx = xx+x; + if (flipx) + xxx = (7-xx+x); + + if (xxx < 0 || xxx > 159) + continue; + + u16 color = gbLineBuffer[xxx]; + + if (prio) + { + if (color < 0x200 && ((color & 0xFF) != 0)) + continue; + } + if (color >= 0x300 && color != 0x300) + continue; + else if (color >= 0x200 && color < 0x300) + { + int sprite = color & 0xff; + + int spriteX = gbMemory[0xfe00 + 4 * sprite + 1] - 8; + + if (spriteX == x) + { + if (sprite < spriteNumber) + continue; + } + else + { + if (gbCgbMode) + { + if (sprite < spriteNumber) + continue; + } + else + { + if (spriteX < x+8) + continue; + } + } + } + + gbLineBuffer[xxx] = 0x200 + spriteNumber; + + // make sure that sprites will work even in CGB mode + if (gbCgbMode) + { + c = c + (flags & 0x07)*4 + 32; + } + else + { + c = pal[c]; + + if (gbSgbMode && !gbCgbMode) + { + int dx = xxx >> 3; + int dy = y >> 3; + + int palette = gbSgbATF[dy * 20 + dx]; + + if (c == 0) + palette = 0; + + c = c + 4*palette; + } + else + { + c += 4; + } + } + + gbLineMix[xxx] = gbColorOption ? gbColorFilter[gbPalette[c]] : + gbPalette[c]; + } +} + +void gbDrawSprites() +{ + int x = 0; + int y = 0; + int count = 0; + + int size = (register_LCDC & 4); + + if (!(register_LCDC & 0x80)) + return; + + if ((register_LCDC & 2) && (layerSettings & 0x1000)) + { + int yc = register_LY; + + int address = 0xfe00; + for (int i = 0; i < 40; i++) + { + y = gbMemory[address++]; + x = gbMemory[address++]; + int tile = gbMemory[address++]; + if (size) + tile &= 254; + int flags = gbMemory[address++]; + + if (x > 0 && y > 0 && x < 168 && y < 160) + { + // check if sprite intersects current line + int t = yc -y + 16; + if (size && t >= 0 && t < 16) + { + gbDrawSpriteTile(tile, x-8, yc, t, flags, size, i); + count++; + } + else if (!size && t >= 0 && t < 8) + { + gbDrawSpriteTile(tile, x-8, yc, t, flags, size, i); + count++; + } + } + // sprite limit reached! + if (count >= 10) + break; + } + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbGlobals.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbGlobals.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,39 @@ +#include "../Port.h" +#include "GB.h" + +u8 *gbMemoryMap[16]; + +int32 gbRomSizeMask = 0; +int32 gbRomSize = 0; +int32 gbRamSizeMask = 0; +int32 gbRamSize = 0; + +u8 * gbMemory = NULL; +u8 * gbVram = NULL; +u8 * gbRom = NULL; +u8 * gbRam = NULL; +u8 * gbWram = NULL; +u16 *gbLineBuffer = NULL; + +u16 gbPalette[128]; +u8 gbBgp[4] = { 0, 1, 2, 3}; +u8 gbObp0[4] = { 0, 1, 2, 3}; +u8 gbObp1[4] = { 0, 1, 2, 3}; +int32 gbWindowLine = -1; + +int32 gbCgbMode = 0; + +u16 gbColorFilter[32768]; +int32 gbColorOption = 0; +int32 gbPaletteOption = 0; +int32 gbEmulatorType = 0; +int32 gbBorderOn = 1; +int32 gbBorderAutomatic = 0; +int32 gbBorderLineSkip = 160; +int32 gbBorderRowSkip = 0; +int32 gbBorderColumnSkip = 0; +int32 gbDmaTicks = 0; +bool8 gbNullInputHackEnabled = false; +bool8 gbNullInputHackTempEnabled = false; + +u8 (*gbSerialFunction)(u8) = NULL; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbGlobals.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbGlobals.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,95 @@ +#ifndef VBA_GB_GLOBALS_H +#define VBA_GB_GLOBALS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../Port.h" + +extern int32 gbRomSizeMask; +extern int32 gbRomSize; +extern int32 gbRamSize; +extern int32 gbRamSizeMask; + +extern u8 * gbRom; +extern u8 * gbRam; +extern u8 * gbVram; +extern u8 * gbWram; +extern u8 * gbMemory; +extern u16 *gbLineBuffer; + +extern u8 *gbMemoryMap[16]; + +inline u8 gbReadMemoryQuick(u16 address) +{ + extern int32 gbEchoRAMFixOn; + if (gbEchoRAMFixOn) + { + if (address >= 0xe000 && address < 0xfe00) + { + address -= 0x2000; + } + } + return gbMemoryMap[address>>12][address&0xfff]; +} + +inline void gbWriteMemoryQuick(u16 address, u8 value) +{ + extern int32 gbEchoRAMFixOn; + if (gbEchoRAMFixOn) + { + if (address >= 0xe000 && address < 0xfe00) + { + address -= 0x2000; + } + } + gbMemoryMap[address>>12][address&0xfff] = value; +} + +inline u8 gbReadROMQuick(u32 address) +{ + return gbRom[address]; +} + +extern int32 gbFrameSkip; +extern u16 gbColorFilter[32768]; +extern int32 gbColorOption; +extern int32 gbPaletteOption; +extern int32 gbEmulatorType; +extern int32 gbBorderOn; +extern int32 gbBorderAutomatic; +extern int32 gbCgbMode; +extern int32 gbSgbMode; +extern int32 gbWindowLine; +extern int32 gbSpeed; +extern u8 gbBgp[4]; +extern u8 gbObp0[4]; +extern u8 gbObp1[4]; +extern u16 gbPalette[128]; + +extern u8 register_LCDC; +extern u8 register_LY; +extern u8 register_SCY; +extern u8 register_SCX; +extern u8 register_WY; +extern u8 register_WX; +extern u8 register_VBK; + +extern int emulating; + +extern int32 gbBorderLineSkip; +extern int32 gbBorderRowSkip; +extern int32 gbBorderColumnSkip; +extern int32 gbDmaTicks; + +extern bool8 useOldFrameTiming; +extern bool8 gbNullInputHackEnabled; +extern bool8 gbNullInputHackTempEnabled; + +extern void gbRenderLine(); +extern void gbDrawSprites(); + +extern u8 (*gbSerialFunction)(u8); + +#endif // VBA_GB_GLOBALS_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbMemory.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbMemory.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1070 @@ +#include "gbGlobals.h" +#include "gbMemory.h" +#include "../common/System.h" +#include "../common/movie.h" + +mapperMBC1 gbDataMBC1 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // memory model + 0, // ROM high address + 0 // RAM address +}; + +// MBC1 ROM write registers +void mapperMBC1ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch (address & 0x6000) + { + case 0x0000: // RAM enable register + gbDataMBC1.mapperRAMEnable = ((value & 0x0a) == 0x0a ? 1 : 0); + break; + case 0x2000: // ROM bank select + // value = value & 0x1f; + if (value == 0) + value = 1; + if (value == gbDataMBC1.mapperROMBank) + break; + + tmpAddress = value << 14; + + // check current model + if (gbDataMBC1.mapperMemoryModel == 0) + { + // model is 16/8, so we have a high address in use + tmpAddress |= (gbDataMBC1.mapperROMHighAddress) << 19; + } + + tmpAddress &= gbRomSizeMask; + gbDataMBC1.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + break; + case 0x4000: // RAM bank select + if (gbDataMBC1.mapperMemoryModel == 1) + { + // 4/32 model, RAM bank switching provided + value = value & 0x03; + if (value == gbDataMBC1.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + gbDataMBC1.mapperRAMBank = value; + gbDataMBC1.mapperRAMAddress = tmpAddress; + } + else + { + // 16/8, set the high address + gbDataMBC1.mapperROMHighAddress = value & 0x03; + tmpAddress = gbDataMBC1.mapperROMBank << 14; + tmpAddress |= (gbDataMBC1.mapperROMHighAddress) << 19; + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + break; + case 0x6000: // memory model select + gbDataMBC1.mapperMemoryModel = value & 1; + break; + } +} + +// MBC1 RAM write +void mapperMBC1RAM(u16 address, u8 value) +{ + if (gbDataMBC1.mapperRAMEnable) + { + if (gbRamSize) + { + gbWriteMemoryQuick(address, value); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } +} + +void memoryUpdateMapMBC1() +{ + int tmpAddress = gbDataMBC1.mapperROMBank << 14; + + // check current model + if (gbDataMBC1.mapperMemoryModel == 1) + { + // model is 16/8, so we have a high address in use + tmpAddress |= (gbDataMBC1.mapperROMHighAddress) << 19; + } + + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if (gbRamSize) + { + gbMemoryMap[0x0a] = &gbRam[gbDataMBC1.mapperRAMAddress]; + gbMemoryMap[0x0b] = &gbRam[gbDataMBC1.mapperRAMAddress + 0x1000]; + } +} + +mapperMBC2 gbDataMBC2 = { + 0, // RAM enable + 1 // ROM bank +}; + +// MBC2 ROM write registers +void mapperMBC2ROM(u16 address, u8 value) +{ + switch (address & 0x6000) + { + case 0x0000: // RAM enable + if (!(address & 0x0100)) + { + gbDataMBC2.mapperRAMEnable = (value & 0x0f) == 0x0a; + } + break; + case 0x2000: // ROM bank select + if (address & 0x0100) + { + value &= 0x0f; + + if (value == 0) + value = 1; + if (gbDataMBC2.mapperROMBank != value) + { + gbDataMBC2.mapperROMBank = value; + + int tmpAddress = value << 14; + + tmpAddress &= gbRomSizeMask; + + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + } + break; + } +} + +// MBC2 RAM write +void mapperMBC2RAM(u16 address, u8 value) +{ + if (gbDataMBC2.mapperRAMEnable) + { + if (gbRamSize && address < 0xa200) + { + gbWriteMemoryQuick(address, value); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } +} + +void memoryUpdateMapMBC2() +{ + int tmpAddress = gbDataMBC2.mapperROMBank << 14; + + tmpAddress &= gbRomSizeMask; + + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; +} + +mapperMBC3 gbDataMBC3 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // RAM address + 0, // timer clock latch + 0, // timer clock register + 0, // timer seconds + 0, // timer minutes + 0, // timer hours + 0, // timer days + 0, // timer control + 0, // timer latched seconds + 0, // timer latched minutes + 0, // timer latched hours + 0, // timer latched days + 0, // timer latched control + (time_t)-1 // last time +}; + +void memoryUpdateMBC3Clock() +{ + time_t now; + + if (VBAMovieActive() || VBAMovieLoading()) + now = (time_t)(VBAMovieGetId() + VBAMovieGetFrameCounter()/60); /// FIXME: is /60 the right factor? + else + now = time(NULL); + + time_t diff = now - gbDataMBC3.mapperLastTime; + if (diff > 0) + { + // update the clock according to the last update time + gbDataMBC3.mapperSeconds += (int)(diff % 60); + if (gbDataMBC3.mapperSeconds > 59) + { + gbDataMBC3.mapperSeconds -= 60; + gbDataMBC3.mapperMinutes++; + } + + diff /= 60; + + gbDataMBC3.mapperMinutes += (int)(diff % 60); + if (gbDataMBC3.mapperMinutes > 60) + { + gbDataMBC3.mapperMinutes -= 60; + gbDataMBC3.mapperHours++; + } + + diff /= 60; + + gbDataMBC3.mapperHours += (int)(diff % 24); + if (gbDataMBC3.mapperHours > 24) + { + gbDataMBC3.mapperHours -= 24; + gbDataMBC3.mapperDays++; + } + diff /= 24; + + gbDataMBC3.mapperDays += (int)diff; + if (gbDataMBC3.mapperDays > 255) + { + if (gbDataMBC3.mapperDays > 511) + { + gbDataMBC3.mapperDays %= 512; + gbDataMBC3.mapperControl |= 0x80; + } + gbDataMBC3.mapperControl = (gbDataMBC3.mapperControl & 0xfe) | + (gbDataMBC3.mapperDays > 255 ? 1 : 0); + } + } + gbDataMBC3.mapperLastTime = now; +} + +// MBC3 ROM write registers +void mapperMBC3ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch (address & 0x6000) + { + case 0x0000: // RAM enable register + gbDataMBC3.mapperRAMEnable = ((value & 0x0a) == 0x0a ? 1 : 0); + break; + case 0x2000: // ROM bank select + value = value & 0x7f; + if (value == 0) + value = 1; + if (value == gbDataMBC3.mapperROMBank) + break; + + tmpAddress = value << 14; + + tmpAddress &= gbRomSizeMask; + gbDataMBC3.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + break; + case 0x4000: // RAM bank select + if (value < 8) + { + if (value == gbDataMBC3.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + gbDataMBC3.mapperRAMBank = value; + gbDataMBC3.mapperRAMAddress = tmpAddress; + } + else + { + if (gbDataMBC3.mapperRAMEnable) + { + gbDataMBC3.mapperRAMBank = -1; + + gbDataMBC3.mapperClockRegister = value; + } + } + break; + case 0x6000: // clock latch + if (gbDataMBC3.mapperClockLatch == 0 && value == 1) + { + memoryUpdateMBC3Clock(); + gbDataMBC3.mapperLSeconds = gbDataMBC3.mapperSeconds; + gbDataMBC3.mapperLMinutes = gbDataMBC3.mapperMinutes; + gbDataMBC3.mapperLHours = gbDataMBC3.mapperHours; + gbDataMBC3.mapperLDays = gbDataMBC3.mapperDays; + gbDataMBC3.mapperLControl = gbDataMBC3.mapperControl; + } + if (value == 0x00 || value == 0x01) + gbDataMBC3.mapperClockLatch = value; + break; + } +} + +// MBC3 RAM write +void mapperMBC3RAM(u16 address, u8 value) +{ + if (gbDataMBC3.mapperRAMEnable) + { + if (gbDataMBC3.mapperRAMBank != -1) + { + if (gbRamSize) + { + gbWriteMemoryQuick(address, value); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } + else + { + time_t tmp; //Small kludge to get it working on some 64 bit systems. + if (VBAMovieActive() || VBAMovieLoading()) + gbDataMBC3.mapperLastTime = VBAMovieGetId() + VBAMovieGetFrameCounter()/60; + else { + time(&tmp); + gbDataMBC3.mapperLastTime=(u32)tmp; + } + systemScreenMessage(ctime(&tmp), 4); + gbDataMBC3.mapperLastTime=(u32)tmp; + + switch (gbDataMBC3.mapperClockRegister) + { + case 0x08: + gbDataMBC3.mapperSeconds = value; + break; + case 0x09: + gbDataMBC3.mapperMinutes = value; + break; + case 0x0a: + gbDataMBC3.mapperHours = value; + break; + case 0x0b: + gbDataMBC3.mapperDays = value; + break; + case 0x0c: + if (gbDataMBC3.mapperControl & 0x80) + gbDataMBC3.mapperControl = 0x80 | value; + else + gbDataMBC3.mapperControl = value; + break; + } + } + } +} + +// MBC3 read RAM +u8 mapperMBC3ReadRAM(u16 address) +{ + if (gbDataMBC3.mapperRAMEnable) + { + if (gbDataMBC3.mapperRAMBank != -1) + { + return gbReadMemoryQuick(address); + } + + switch (gbDataMBC3.mapperClockRegister) + { + case 0x08: + return gbDataMBC3.mapperLSeconds; + break; + case 0x09: + return gbDataMBC3.mapperLMinutes; + break; + case 0x0a: + return gbDataMBC3.mapperLHours; + break; + case 0x0b: + return gbDataMBC3.mapperLDays; + break; + case 0x0c: + return gbDataMBC3.mapperLControl; + } + } + return 0; +} + +void memoryUpdateMapMBC3() +{ + int tmpAddress = gbDataMBC3.mapperROMBank << 14; + + tmpAddress &= gbRomSizeMask; + + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if (gbDataMBC3.mapperRAMBank >= 0 && gbRamSize) + { + tmpAddress = gbDataMBC3.mapperRAMBank << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + } +} + +mapperMBC5 gbDataMBC5 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // ROM high address + 0, // RAM address + 0 // is rumble cartridge? +}; + +// MBC5 ROM write registers +void mapperMBC5ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch (address & 0x6000) + { + case 0x0000: // RAM enable register + gbDataMBC5.mapperRAMEnable = ((value & 0x0a) == 0x0a ? 1 : 0); + break; + case 0x2000: // ROM bank select + if (address < 0x3000) + { + value = value & 0xff; + if (value == gbDataMBC5.mapperROMBank) + break; + + tmpAddress = (value << 14) | (gbDataMBC5.mapperROMHighAddress << 22) ; + + tmpAddress &= gbRomSizeMask; + gbDataMBC5.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + else + { + value = value & 1; + if (value == gbDataMBC5.mapperROMHighAddress) + break; + + tmpAddress = (gbDataMBC5.mapperROMBank << 14) | (value << 22); + + tmpAddress &= gbRomSizeMask; + gbDataMBC5.mapperROMHighAddress = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + break; + case 0x4000: // RAM bank select + if (gbDataMBC5.isRumbleCartridge) + value &= 0x07; + else + value &= 0x0f; + if (value == gbDataMBC5.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + if (gbRamSize) + { + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + + gbDataMBC5.mapperRAMBank = value; + gbDataMBC5.mapperRAMAddress = tmpAddress; + } + break; + } +} + +// MBC5 RAM write +void mapperMBC5RAM(u16 address, u8 value) +{ + if (gbDataMBC5.mapperRAMEnable) + { + if (gbRamSize) + { + gbWriteMemoryQuick(address, value); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } +} + +void memoryUpdateMapMBC5() +{ + int tmpAddress = (gbDataMBC5.mapperROMBank << 14) | + (gbDataMBC5.mapperROMHighAddress << 22) ; + + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if (gbRamSize) + { + tmpAddress = gbDataMBC5.mapperRAMBank << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + } +} + +mapperMBC7 gbDataMBC7 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // RAM address + 0, // chip select + 0, // ?? + 0, // mapper state + 0, // buffer for receiving serial data + 0, // idle state + 0, // count of bits received + 0, // command received + 0, // address received + 0, // write enable + 0, // value to return on ram +}; + +// MBC7 ROM write registers +void mapperMBC7ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch (address & 0x6000) + { + case 0x0000: + break; + case 0x2000: // ROM bank select + value = value & 0x7f; + if (value == 0) + value = 1; + + if (value == gbDataMBC7.mapperROMBank) + break; + + tmpAddress = (value << 14); + + tmpAddress &= gbRomSizeMask; + gbDataMBC7.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + break; + case 0x4000: // RAM bank select/enable + if (value < 8) + { + tmpAddress = (value&3) << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbMemory[0xa000]; + gbMemoryMap[0x0b] = &gbMemory[0xb000]; + + gbDataMBC7.mapperRAMBank = value; + gbDataMBC7.mapperRAMAddress = tmpAddress; + gbDataMBC7.mapperRAMEnable = 0; + } + else + { + gbDataMBC7.mapperRAMEnable = 0; + } + break; + } +} + +// MBC7 read RAM +u8 mapperMBC7ReadRAM(u16 address) +{ + switch (address & 0xa0f0) + { + case 0xa000: + case 0xa010: + case 0xa060: + case 0xa070: + return 0; + case 0xa020: + // sensor X low byte + return systemGetSensorX() & 255; + case 0xa030: + // sensor X high byte + return systemGetSensorX() >> 8; + case 0xa040: + // sensor Y low byte + return systemGetSensorY() & 255; + case 0xa050: + // sensor Y high byte + return systemGetSensorY() >> 8; + case 0xa080: + return gbDataMBC7.value; + } + return 0xff; +} + +// MBC7 RAM write +void mapperMBC7RAM(u16 address, u8 value) +{ + if (address == 0xa080) + { + // special processing needed + int oldCs = gbDataMBC7.cs, oldSk = gbDataMBC7.sk; + + gbDataMBC7.cs = value>>7; + gbDataMBC7.sk = (value>>6)&1; + + if (!oldCs && gbDataMBC7.cs) + { + if (gbDataMBC7.state == 5) + { + if (gbDataMBC7.writeEnable) + { + gbWriteMemoryQuick(0xa000+gbDataMBC7.address*2, gbDataMBC7.buffer>>8); + gbWriteMemoryQuick(0xa000+gbDataMBC7.address*2+1, gbDataMBC7.buffer&0xff); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + gbDataMBC7.state = 0; + gbDataMBC7.value = 1; + } + else + { + gbDataMBC7.idle = true; + gbDataMBC7.state = 0; + } + } + + if (!oldSk && gbDataMBC7.sk) + { + if (gbDataMBC7.idle) + { + if (value & 0x02) + { + gbDataMBC7.idle = false; + gbDataMBC7.count = 0; + gbDataMBC7.state = 1; + } + } + else + { + switch (gbDataMBC7.state) + { + case 1: + // receiving command + gbDataMBC7.buffer <<= 1; + gbDataMBC7.buffer |= (value & 0x02) ? 1 : 0; + gbDataMBC7.count++; + if (gbDataMBC7.count == 2) + { + // finished receiving command + gbDataMBC7.state = 2; + gbDataMBC7.count = 0; + gbDataMBC7.code = gbDataMBC7.buffer & 3; + } + break; + case 2: + // receive address + gbDataMBC7.buffer <<= 1; + gbDataMBC7.buffer |= (value&0x02) ? 1 : 0; + gbDataMBC7.count++; + if (gbDataMBC7.count == 8) + { + // finish receiving + gbDataMBC7.state = 3; + gbDataMBC7.count = 0; + gbDataMBC7.address = gbDataMBC7.buffer&0xff; + if (gbDataMBC7.code == 0) + { + if ((gbDataMBC7.address>>6) == 0) + { + gbDataMBC7.writeEnable = 0; + gbDataMBC7.state = 0; + } + else if ((gbDataMBC7.address>>6) == 3) + { + gbDataMBC7.writeEnable = 1; + gbDataMBC7.state = 0; + } + } + } + break; + case 3: + gbDataMBC7.buffer <<= 1; + gbDataMBC7.buffer |= (value&0x02) ? 1 : 0; + gbDataMBC7.count++; + + switch (gbDataMBC7.code) + { + case 0: + if (gbDataMBC7.count == 16) + { + if ((gbDataMBC7.address>>6) == 0) + { + gbDataMBC7.writeEnable = 0; + gbDataMBC7.state = 0; + } + else if ((gbDataMBC7.address>>6) == 1) + { + if (gbDataMBC7.writeEnable) + { + for (int i = 0; i < 256; i++) + { + gbWriteMemoryQuick(0xa000+i*2, gbDataMBC7.buffer >> 8); + gbWriteMemoryQuick(0xa000+i*2+1, gbDataMBC7.buffer & 0xff); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } + gbDataMBC7.state = 5; + } + else if ((gbDataMBC7.address>>6) == 2) + { + if (gbDataMBC7.writeEnable) + { + for (int i = 0; i < 256; i++) + WRITE16LE((u16 *)&gbMemory[0xa000+i*2], 0xffff); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + gbDataMBC7.state = 5; + } + else if ((gbDataMBC7.address>>6) == 3) + { + gbDataMBC7.writeEnable = 1; + gbDataMBC7.state = 0; + } + gbDataMBC7.count = 0; + } + break; + case 1: + if (gbDataMBC7.count == 16) + { + gbDataMBC7.count = 0; + gbDataMBC7.state = 5; + gbDataMBC7.value = 0; + } + break; + case 2: + if (gbDataMBC7.count == 1) + { + gbDataMBC7.state = 4; + gbDataMBC7.count = 0; + gbDataMBC7.buffer = (gbReadMemoryQuick(0xa000+gbDataMBC7.address*2)<<8)| + (gbReadMemoryQuick(0xa000+gbDataMBC7.address*2+1)); + } + break; + case 3: + if (gbDataMBC7.count == 16) + { + gbDataMBC7.count = 0; + gbDataMBC7.state = 5; + gbDataMBC7.value = 0; + gbDataMBC7.buffer = 0xffff; + } + break; + } + break; + } + } + } + + if (oldSk && !gbDataMBC7.sk) + { + if (gbDataMBC7.state == 4) + { + gbDataMBC7.value = (gbDataMBC7.buffer & 0x8000) ? 1 : 0; + gbDataMBC7.buffer <<= 1; + gbDataMBC7.count++; + if (gbDataMBC7.count == 16) + { + gbDataMBC7.count = 0; + gbDataMBC7.state = 0; + } + } + } + } +} + +void memoryUpdateMapMBC7() +{ + int tmpAddress = (gbDataMBC5.mapperROMBank << 14); + + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; +} + +mapperHuC1 gbDataHuC1 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // memory model + 0, // ROM high address + 0 // RAM address +}; + +// HuC1 ROM write registers +void mapperHuC1ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch (address & 0x6000) + { + case 0x0000: // RAM enable register + gbDataHuC1.mapperRAMEnable = ((value & 0x0a) == 0x0a ? 1 : 0); + break; + case 0x2000: // ROM bank select + value = value & 0x3f; + if (value == 0) + value = 1; + if (value == gbDataHuC1.mapperROMBank) + break; + + tmpAddress = value << 14; + + tmpAddress &= gbRomSizeMask; + gbDataHuC1.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + break; + case 0x4000: // RAM bank select + if (gbDataHuC1.mapperMemoryModel == 1) + { + // 4/32 model, RAM bank switching provided + value = value & 0x03; + if (value == gbDataHuC1.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + gbDataHuC1.mapperRAMBank = value; + gbDataHuC1.mapperRAMAddress = tmpAddress; + } + else + { + // 16/8, set the high address + gbDataHuC1.mapperROMHighAddress = value & 0x03; + tmpAddress = gbDataHuC1.mapperROMBank << 14; + tmpAddress |= (gbDataHuC1.mapperROMHighAddress) << 19; + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + } + break; + case 0x6000: // memory model select + gbDataHuC1.mapperMemoryModel = value & 1; + break; + } +} + +// HuC1 RAM write +void mapperHuC1RAM(u16 address, u8 value) +{ + if (gbDataHuC1.mapperRAMEnable) + { + if (gbRamSize) + { + gbWriteMemoryQuick(address, value); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } +} + +void memoryUpdateMapHuC1() +{ + int tmpAddress = gbDataHuC1.mapperROMBank << 14; + + tmpAddress &= gbRomSizeMask; + + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if (gbRamSize) + { + tmpAddress = gbDataHuC1.mapperRAMBank << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + } +} + +mapperHuC3 gbDataHuC3 = { + 0, // RAM enable + 1, // ROM bank + 0, // RAM bank + 0, // RAM address + 0, // RAM flag + 0 // RAM read value +}; + +// HuC3 ROM write registers +void mapperHuC3ROM(u16 address, u8 value) +{ + int tmpAddress = 0; + + switch (address & 0x6000) + { + case 0x0000: // RAM enable register + gbDataHuC3.mapperRAMEnable = (value == 0x0a ? 1 : 0); + gbDataHuC3.mapperRAMFlag = value; + if (gbDataHuC3.mapperRAMFlag != 0x0a) + gbDataHuC3.mapperRAMBank = -1; + break; + case 0x2000: // ROM bank select + value = value & 0x7f; + if (value == 0) + value = 1; + if (value == gbDataHuC3.mapperROMBank) + break; + + tmpAddress = value << 14; + + tmpAddress &= gbRomSizeMask; + gbDataHuC3.mapperROMBank = value; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + break; + case 0x4000: // RAM bank select + value = value & 0x03; + if (value == gbDataHuC3.mapperRAMBank) + break; + tmpAddress = value << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + gbDataHuC3.mapperRAMBank = value; + gbDataHuC3.mapperRAMAddress = tmpAddress; + break; + case 0x6000: // nothing to do! + break; + } +} + +// HuC3 read RAM +u8 mapperHuC3ReadRAM(u16 address) +{ + if (gbDataHuC3.mapperRAMFlag > 0x0b && + gbDataHuC3.mapperRAMFlag < 0x0e) + { + if (gbDataHuC3.mapperRAMFlag != 0x0c) + return 1; + return gbDataHuC3.mapperRAMValue; + } + else + return gbReadMemoryQuick(address); +} + +// HuC3 RAM write +void mapperHuC3RAM(u16 address, u8 value) +{ + int32 *p; + + if (gbDataHuC3.mapperRAMFlag < 0x0b || + gbDataHuC3.mapperRAMFlag > 0x0e) + { + if (gbDataHuC3.mapperRAMEnable) + { + if (gbRamSize) + { + gbWriteMemoryQuick(address, value); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + } + } + else + { + if (gbDataHuC3.mapperRAMFlag == 0x0b) + { + if (value == 0x62) + { + gbDataHuC3.mapperRAMValue = 1; + } + else + { + switch (value & 0xf0) + { + case 0x10: + p = &gbDataHuC3.mapperRegister2; + gbDataHuC3.mapperRAMValue = *(p+gbDataHuC3.mapperRegister1++); + if (gbDataHuC3.mapperRegister1 > 6) + gbDataHuC3.mapperRegister1 = 0; + break; + case 0x30: + p = &gbDataHuC3.mapperRegister2; + *(p+gbDataHuC3.mapperRegister1++) = value & 0x0f; + if (gbDataHuC3.mapperRegister1 > 6) + gbDataHuC3.mapperRegister1 = 0; + gbDataHuC3.mapperAddress = + (gbDataHuC3.mapperRegister6 << 24) | + (gbDataHuC3.mapperRegister5 << 16) | + (gbDataHuC3.mapperRegister4 << 8) | + (gbDataHuC3.mapperRegister3 << 4) | + (gbDataHuC3.mapperRegister2); + break; + case 0x40: + gbDataHuC3.mapperRegister1 = (gbDataHuC3.mapperRegister1 & 0xf0) | + (value & 0x0f); + gbDataHuC3.mapperRegister2 = (gbDataHuC3.mapperAddress & 0x0f); + gbDataHuC3.mapperRegister3 = ((gbDataHuC3.mapperAddress>>4)&0x0f); + gbDataHuC3.mapperRegister4 = ((gbDataHuC3.mapperAddress>>8)&0x0f); + gbDataHuC3.mapperRegister5 = ((gbDataHuC3.mapperAddress>>16)&0x0f); + gbDataHuC3.mapperRegister6 = ((gbDataHuC3.mapperAddress>>24)&0x0f); + gbDataHuC3.mapperRegister7 = 0; + gbDataHuC3.mapperRegister8 = 0; + gbDataHuC3.mapperRAMValue = 0; + break; + case 0x50: + gbDataHuC3.mapperRegister1 = (gbDataHuC3.mapperRegister1 & 0x0f) | + ((value << 4)&0x0f); + break; + default: + gbDataHuC3.mapperRAMValue = 1; + break; + } + } + } + } +} + +void memoryUpdateMapHuC3() +{ + int tmpAddress = gbDataHuC3.mapperROMBank << 14; + + tmpAddress &= gbRomSizeMask; + gbMemoryMap[0x04] = &gbRom[tmpAddress]; + gbMemoryMap[0x05] = &gbRom[tmpAddress + 0x1000]; + gbMemoryMap[0x06] = &gbRom[tmpAddress + 0x2000]; + gbMemoryMap[0x07] = &gbRom[tmpAddress + 0x3000]; + + if (gbRamSize) + { + tmpAddress = gbDataHuC3.mapperRAMBank << 13; + tmpAddress &= gbRamSizeMask; + gbMemoryMap[0x0a] = &gbRam[tmpAddress]; + gbMemoryMap[0x0b] = &gbRam[tmpAddress + 0x1000]; + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbMemory.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbMemory.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,142 @@ +#ifndef VBA_GB_MEMORY_H +#define VBA_GB_MEMORY_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../Port.h" + +struct mapperMBC1 +{ + int32 mapperRAMEnable; + int32 mapperROMBank; + int32 mapperRAMBank; + int32 mapperMemoryModel; + int32 mapperROMHighAddress; + int32 mapperRAMAddress; +}; + +struct mapperMBC2 +{ + int32 mapperRAMEnable; + int32 mapperROMBank; +}; + +struct mapperMBC3 +{ + int32 mapperRAMEnable; + int32 mapperROMBank; + int32 mapperRAMBank; + int32 mapperRAMAddress; + int32 mapperClockLatch; + int32 mapperClockRegister; + int32 mapperSeconds; + int32 mapperMinutes; + int32 mapperHours; + int32 mapperDays; + int32 mapperControl; + int32 mapperLSeconds; + int32 mapperLMinutes; + int32 mapperLHours; + int32 mapperLDays; + int32 mapperLControl; + //time_t mapperLastTime; + u32 mapperLastTime; +}; + +struct mapperMBC5 +{ + int32 mapperRAMEnable; + int32 mapperROMBank; + int32 mapperRAMBank; + int32 mapperROMHighAddress; + int32 mapperRAMAddress; + int32 isRumbleCartridge; +}; + +struct mapperMBC7 +{ + int32 mapperRAMEnable; + int32 mapperROMBank; + int32 mapperRAMBank; + int32 mapperRAMAddress; + int32 cs; + int32 sk; + int32 state; + int32 buffer; + int32 idle; + int32 count; + int32 code; + int32 address; + int32 writeEnable; + int32 value; +}; + +struct mapperHuC1 +{ + int32 mapperRAMEnable; + int32 mapperROMBank; + int32 mapperRAMBank; + int32 mapperMemoryModel; + int32 mapperROMHighAddress; + int32 mapperRAMAddress; +}; + +struct mapperHuC3 +{ + int32 mapperRAMEnable; + int32 mapperROMBank; + int32 mapperRAMBank; + int32 mapperRAMAddress; + int32 mapperAddress; + int32 mapperRAMFlag; + int32 mapperRAMValue; + int32 mapperRegister1; + int32 mapperRegister2; + int32 mapperRegister3; + int32 mapperRegister4; + int32 mapperRegister5; + int32 mapperRegister6; + int32 mapperRegister7; + int32 mapperRegister8; +}; + +extern mapperMBC1 gbDataMBC1; +extern mapperMBC2 gbDataMBC2; +extern mapperMBC3 gbDataMBC3; +extern mapperMBC5 gbDataMBC5; +extern mapperHuC1 gbDataHuC1; +extern mapperHuC3 gbDataHuC3; + +void mapperMBC1ROM(u16, u8); +void mapperMBC1RAM(u16, u8); +void mapperMBC2ROM(u16, u8); +void mapperMBC2RAM(u16, u8); +void mapperMBC3ROM(u16, u8); +void mapperMBC3RAM(u16, u8); +u8 mapperMBC3ReadRAM(u16); +void mapperMBC5ROM(u16, u8); +void mapperMBC5RAM(u16, u8); +void mapperMBC7ROM(u16, u8); +void mapperMBC7RAM(u16, u8); +u8 mapperMBC7ReadRAM(u16); +void mapperHuC1ROM(u16, u8); +void mapperHuC1RAM(u16, u8); +void mapperHuC3ROM(u16, u8); +void mapperHuC3RAM(u16, u8); +u8 mapperHuC3ReadRAM(u16); + +//extern void (*mapper)(u16,u8); +//extern void (*mapperRAM)(u16,u8); +//extern u8 (*mapperReadRAM)(u16); + +extern void memoryUpdateMapMBC1(); +extern void memoryUpdateMapMBC2(); +extern void memoryUpdateMapMBC3(); +extern void memoryUpdateMapMBC5(); +extern void memoryUpdateMapMBC7(); +extern void memoryUpdateMapHuC1(); +extern void memoryUpdateMapHuC3(); + +#endif // VBA_GB_MEMORY diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbPrinter.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbPrinter.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,234 @@ +#include +#include + +#include "../common/System.h" +#include "gbPrinter.h" + +u8 gbPrinterStatus = 0; +int gbPrinterState = 0; +u8 gbPrinterData[0x280*9]; +u8 gbPrinterPacket[0x400]; +int gbPrinterCount = 0; +int gbPrinterDataCount = 0; +int gbPrinterDataSize = 0; +int gbPrinterResult = 0; + +bool gbPrinterCheckCRC() +{ + u16 crc = 0; + + for (int i = 2; i < (6+gbPrinterDataSize); i++) + { + crc += gbPrinterPacket[i]; + } + + int msgCrc = gbPrinterPacket[6+gbPrinterDataSize] + + (gbPrinterPacket[7+gbPrinterDataSize]<<8); + + return msgCrc == crc; +} + +void gbPrinterReset() +{ + gbPrinterState = 0; + gbPrinterDataSize = 0; + gbPrinterDataCount = 0; + gbPrinterCount = 0; + gbPrinterStatus = 0; + gbPrinterResult = 0; +} + +void gbPrinterShowData() +{ + systemGbPrint(gbPrinterData, + gbPrinterPacket[6], + gbPrinterPacket[7], + gbPrinterPacket[8], + gbPrinterPacket[9]); + /* + allegro_init(); + install_keyboard(); + set_gfx_mode(GFX_AUTODETECT, 160, 144, 0, 0); + PALETTE pal; + pal[0].r = 255; + pal[0].g = 255; + pal[0].b = 255; + pal[1].r = 168; + pal[1].g = 168; + pal[1].b = 168; + pal[2].r = 96; + pal[2].g = 96; + pal[2].b = 96; + pal[3].r = 0; + pal[3].g = 0; + pal[3].b = 0; + set_palette(pal); + acquire_screen(); + u8 *data = gbPrinterData; + for(int y = 0; y < 0x12; y++) { + for(int x = 0; x < 0x14; x++) { + for(int k = 0; k < 8; k++) { + int a = *data++; + int b = *data++; + for(int j = 0; j < 8; j++) { + int mask = 1 << (7-j); + int c = 0; + if(a & mask) + c++; + if(b & mask) + c+=2; + putpixel(screen, x*8+j, y*8+k, c); + } + } + } + } + release_screen(); + while(!keypressed()) { + } + */ +} + +void gbPrinterReceiveData() +{ + if (gbPrinterPacket[3]) // compressed + { + u8 *data = &gbPrinterPacket[6]; + u8 *dest = &gbPrinterData[gbPrinterDataCount]; + int len = 0; + while (len < gbPrinterDataSize) + { + u8 control = *data++; + if (control & 0x80) // repeated data + { + control &= 0x7f; + control += 2; + memset(dest, *data++, control); + len += control; + dest += control; + } + else // raw data + { + control++; + memcpy(dest, data, control); + dest += control; + data += control; + len += control; + } + } + } + else + { + memcpy(&gbPrinterData[gbPrinterDataCount], + &gbPrinterPacket[6], + gbPrinterDataSize); + gbPrinterDataCount += gbPrinterDataSize; + } +} + +void gbPrinterCommand() +{ + switch (gbPrinterPacket[2]) + { + case 0x01: + // reset/initialize packet + gbPrinterDataCount = 0; + gbPrinterStatus = 0; + break; + case 0x02: + // print packet + gbPrinterShowData(); + break; + case 0x04: + // data packet + gbPrinterReceiveData(); + break; + case 0x0f: + // NUL packet + break; + } +} + +u8 gbPrinterSend(u8 byte) +{ + switch (gbPrinterState) + { + case 0: + gbPrinterCount = 0; + // receiving preamble + if (byte == 0x88) + { + gbPrinterPacket[gbPrinterCount++] = byte; + gbPrinterState++; + } + else + { + // todo: handle failure + gbPrinterReset(); + } + break; + case 1: + // receiving preamble + if (byte == 0x33) + { + gbPrinterPacket[gbPrinterCount++] = byte; + gbPrinterState++; + } + else + { + // todo: handle failure + gbPrinterReset(); + } + break; + case 2: + // receiving header + gbPrinterPacket[gbPrinterCount++] = byte; + if (gbPrinterCount == 6) + { + gbPrinterState++; + gbPrinterDataSize = gbPrinterPacket[4] + (gbPrinterPacket[5]<<8); + } + break; + case 3: + // receiving data + if (gbPrinterDataSize) + { + gbPrinterPacket[gbPrinterCount++] = byte; + if (gbPrinterCount == (6+gbPrinterDataSize)) + { + gbPrinterState++; + } + break; + } + gbPrinterState++; + // intentionally move to next if no data to receive + case 4: + // receiving CRC + gbPrinterPacket[gbPrinterCount++] = byte; + gbPrinterState++; + break; + case 5: + // receiving CRC-2 + gbPrinterPacket[gbPrinterCount++] = byte; + if (gbPrinterCheckCRC()) + { + gbPrinterCommand(); + } + gbPrinterState++; + break; + case 6: + // receiving dummy 1 + gbPrinterPacket[gbPrinterCount++] = byte; + gbPrinterResult = 0x81; + gbPrinterState++; + break; + case 7: + // receiving dummy 2 + gbPrinterPacket[gbPrinterCount++] = byte; + gbPrinterResult = gbPrinterStatus; + gbPrinterState = 0; + gbPrinterCount = 0; + break; + } + return gbPrinterResult; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbPrinter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbPrinter.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,10 @@ +#ifndef VBA_GB_PRINTER_H +#define VBA_GB_PRINTER_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +extern u8 gbPrinterSend(u8 byte); + +#endif // VBA_GB_PRINTER_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbSGB.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbSGB.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,997 @@ +#include +#include + +#include "../common/System.h" +#include "../common/Util.h" +#include "GB.h" +#include "gbGlobals.h" +#include "../common/movie.h" + +extern u8 * pix; + +#define GBSGB_NONE 0 +#define GBSGB_RESET 1 +#define GBSGB_PACKET_TRANSMIT 2 + +u8 gbSgbBorderChar [32 * 256]; +u8 gbSgbBorder [2048]; + +int32 gbSgbCGBSupport = 0; +int32 gbSgbMask = 0; +int32 gbSgbMode = 0; +int32 gbSgbPacketState = GBSGB_NONE; +int32 gbSgbBit = 0; +int32 gbSgbPacketTimeout = 0; +int32 GBSGB_PACKET_TIMEOUT = 66666; +u8 gbSgbPacket[16 * 7]; +int32 gbSgbPacketNBits = 0; +int32 gbSgbPacketByte = 0; +int32 gbSgbPacketNumber = 0; +int32 gbSgbMultiplayer = 0; +int32 gbSgbFourPlayers = 0; +u8 gbSgbNextController = 0x0f; +u8 gbSgbReadingController = 0; +u16 gbSgbSCPPalette[4 * 512]; +u8 gbSgbATF[20 * 18]; +u8 gbSgbATFList[45 * 20 * 18]; +u8 gbSgbScreenBuffer[4160]; + +inline void gbSgbDraw24Bit(u8 *p, u16 v) +{ + *((u32 *) p) = systemColorMap32[v]; +} + +inline void gbSgbDraw32Bit(u32 *p, u16 v) +{ + *p = systemColorMap32[v]; +} + +inline void gbSgbDraw16Bit(u16 *p, u16 v) +{ + *p = systemColorMap16[v]; +} + +void gbSgbReset() +{ + gbSgbPacketTimeout = 0; + gbSgbCGBSupport = 0; + gbSgbMask = 0; + gbSgbPacketState = GBSGB_NONE; + gbSgbBit = 0; + gbSgbPacketNBits = 0; + gbSgbPacketNumber = 0; + gbSgbMultiplayer = 0; + gbSgbFourPlayers = 0; + gbSgbNextController = 0x0f; + gbSgbReadingController = 0; + + memset(gbSgbSCPPalette, 0, 512 * 4); + memset(gbSgbATF, 0, 20 * 18); + memset(gbSgbATFList, 0, 45 * 20 * 18); + memset(gbSgbPacket, 0, 16 * 7); + memset(gbSgbBorderChar, 0, 32 * 256); + memset(gbSgbBorder, 0, 2048); + + int i; + for (i = 1; i < 2048; i += 2) + { + gbSgbBorder[i] = 1 << 2; + } + + for (i = 0; i < 4; i++) + { + gbPalette[i * 4] = (0x1f) | (0x1f << 5) | (0x1f << 10); + gbPalette[i * 4 + 1] = (0x15) | (0x15 << 5) | (0x15 << 10); + gbPalette[i * 4 + 2] = (0x0c) | (0x0c << 5) | (0x0c << 10); + gbPalette[i * 4 + 3] = 0; + } +} + +void gbSgbInit() +{ + gbSgbReset(); +} + +void gbSgbShutdown() +{ + memset(gbSgbBorderChar, 0, 32 * 256); + memset(gbSgbBorder, 0, 2048); +} + +void gbSgbFillScreen(u16 color) +{ + switch (systemColorDepth) + { + case 16: + { + for (int y = 0; y < 144; y++) + { + int yLine = (y + gbBorderRowSkip + 1) * (gbBorderLineSkip + 2) + + gbBorderColumnSkip; + u16 *dest = (u16 *)pix + yLine; + for (register int x = 0; x < 160; x++) + gbSgbDraw16Bit(dest++, color); + } + } + break; + case 24: + { + for (int y = 0; y < 144; y++) + { + int yLine = (y + gbBorderRowSkip) * gbBorderLineSkip + gbBorderColumnSkip; + u8 *dest = (u8 *)pix + yLine * 3; + for (register int x = 0; x < 160; x++) + { + gbSgbDraw24Bit(dest, color); + dest += 3; + } + } + } + break; + case 32: + { + for (int y = 0; y < 144; y++) + { + int yLine = (y + gbBorderRowSkip + 1) * (gbBorderLineSkip + 1) + gbBorderColumnSkip; + u32 *dest = (u32 *)pix + yLine; + for (register int x = 0; x < 160; x++) + { + gbSgbDraw32Bit(dest++, color); + } + } + } + break; + } +} + +void gbSgbRenderScreenToBuffer() +{ + u16 mapAddress = 0x9800; + + if (register_LCDC & 0x08) + mapAddress = 0x9c00; + + u16 patternAddress = 0x8800; + + int flag = 1; + + if (register_LCDC & 0x10) + { + patternAddress = 0x8000; + flag = 0; + } + + u8 *toAddress = gbSgbScreenBuffer; + + for (int i = 0; i < 13; i++) + { + for (int j = 0; j < 20; j++) + { + int tile = gbReadMemoryQuick(mapAddress); + mapAddress++; + + if (flag) + { + if (tile > 127) + tile -= 128; + else + tile += 128; + } + for (int k = 0; k < 16; k++) + *toAddress++ = gbReadMemoryQuick(patternAddress + tile * 16 + k); + } + mapAddress += 12; + } +} + +void gbSgbDrawBorderTile(int x, int y, int tile, int attr) +{ + u16 *dest = (u16 *)pix + ((y + 1) * (256 + 2)) + x; + u8 * dest8 = (u8 *)pix + ((y * 256) + x) * 3; + u32 *dest32 = (u32 *)pix + ((y + 1) * 257) + x; + + u8 *tileAddress = &gbSgbBorderChar[tile * 32]; + u8 *tileAddress2 = &gbSgbBorderChar[tile * 32 + 16]; + + u8 l = 8; + + u8 palette = ((attr >> 2) & 7); + + if (palette < 4) + palette += 4; + + palette *= 16; + + u8 xx = 0; + u8 yy = 0; + + int flipX = attr & 0x40; + int flipY = attr & 0x80; + + while (l > 0) + { + u8 mask = 0x80; + u8 a = *tileAddress++; + u8 b = *tileAddress++; + u8 c = *tileAddress2++; + u8 d = *tileAddress2++; + + while (mask > 0) + { + u8 color = 0; + if (a & mask) + color++; + if (b & mask) + color += 2; + if (c & mask) + color += 4; + if (d & mask) + color += 8; + + u8 xxx = xx; + u8 yyy = yy; + + if (flipX) + xxx = 7 - xx; + if (flipY) + yyy = 7 - yy; + + u8 realx = x + xxx; + u8 realy = y + yyy; + + u16 c = gbPalette[palette + color]; + if (!color) + c = gbPalette[0]; + if ((realy < 40 || realy >= 184) || (realx < 48 || realx >= 208)) + { + switch (systemColorDepth) + { + case 16: + gbSgbDraw16Bit(dest + yyy * (256 + 2) + xxx, c); + break; + case 24: + gbSgbDraw24Bit(dest8 + (yyy * 256 + xxx) * 3, c); + break; + case 32: + gbSgbDraw32Bit(dest32 + yyy * (256 + 1) + xxx, c); + break; + } + } + + mask >>= 1; + + xx++; + } + yy++; + xx = 0; + l--; + mask = 0x80; + } +} + +void gbSgbRenderBorder() +{ + if (gbBorderOn) + { + u8 *fromAddress = gbSgbBorder; + + for (u8 y = 0; y < 28; y++) + { + for (u8 x = 0; x < 32; x++) + { + u8 tile = *fromAddress++; + u8 attr = *fromAddress++; + + gbSgbDrawBorderTile(x * 8, y * 8, tile, attr); + } + } + } +} + +void gbSgbPicture() +{ + gbSgbRenderScreenToBuffer(); + + memcpy(gbSgbBorder, gbSgbScreenBuffer, 2048); + + u16 *paletteAddr = (u16 *)&gbSgbScreenBuffer[2048]; + + for (int i = 64; i < 128; i++) + { + gbPalette[i] = READ16LE(paletteAddr++); + } + + gbSgbCGBSupport |= 4; + + if (gbBorderAutomatic && !gbBorderOn && gbSgbCGBSupport > 4) + { + gbBorderOn = 1; + systemGbBorderOn(); + } + + if (gbBorderOn && !gbSgbMask) + gbSgbRenderBorder(); + + if (gbSgbMode && gbCgbMode && gbSgbCGBSupport > 4) + { + gbSgbCGBSupport = 0; + gbSgbMode = 2; + gbSgbMask = 0; + gbSgbRenderBorder(); + gbReset(); + } + + if (gbSgbCGBSupport > 4) + gbSgbCGBSupport = 0; +} + +void gbSgbSetPalette(int a, int b, u16 *p) +{ + u16 bit00 = READ16LE(p++); + int i; + + for (i = 1; i < 4; i++) + { + gbPalette[a * 4 + i] = READ16LE(p++); + } + + for (i = 1; i < 4; i++) + { + gbPalette[b * 4 + i] = READ16LE(p++); + } + + gbPalette[0] = gbPalette[4] = gbPalette[8] = gbPalette[12] = bit00; + if (gbBorderOn && !gbSgbMask) + gbSgbRenderBorder(); +} + +void gbSgbScpPalette() +{ + gbSgbRenderScreenToBuffer(); + + u16 *fromAddress = (u16 *)gbSgbScreenBuffer; + + for (int i = 0; i < 512 * 4; i++) + { + gbSgbSCPPalette[i] = READ16LE(fromAddress++); + } +} + +void gbSgbSetATF(int n) +{ + if (n < 0) + n = 0; + if (n > 44) + n = 44; + memcpy(gbSgbATF, &gbSgbATFList[n * 20 * 18], 20 * 18); + + if (gbSgbPacket[1] & 0x40) + { + gbSgbMask = 0; + if (gbBorderOn) + gbSgbRenderBorder(); + } +} + +void gbSgbSetPalette() +{ + u16 pal = READ16LE((((u16 *)&gbSgbPacket[1]))) & 511; + memcpy(&gbPalette[0], &gbSgbSCPPalette[pal * 4], 4 * sizeof(u16)); + + pal = READ16LE((((u16 *)&gbSgbPacket[3]))) & 511; + memcpy(&gbPalette[4], &gbSgbSCPPalette[pal * 4], 4 * sizeof(u16)); + + pal = READ16LE((((u16 *)&gbSgbPacket[5]))) & 511; + memcpy(&gbPalette[8], &gbSgbSCPPalette[pal * 4], 4 * sizeof(u16)); + + pal = READ16LE((((u16 *)&gbSgbPacket[7]))) & 511; + memcpy(&gbPalette[12], &gbSgbSCPPalette[pal * 4], 4 * sizeof(u16)); + + u8 atf = gbSgbPacket[9]; + + if (atf & 0x80) + { + gbSgbSetATF(atf & 0x3f); + } + + if (atf & 0x40) + { + gbSgbMask = 0; + if (gbBorderOn) + gbSgbRenderBorder(); + } +} + +void gbSgbAttributeBlock() +{ + u8 *fromAddress = &gbSgbPacket[1]; + + u8 nDataSet = *fromAddress++; + if (nDataSet > 12) + nDataSet = 12; + if (nDataSet == 0) + nDataSet = 1; + + while (nDataSet) + { + u8 controlCode = (*fromAddress++) & 7; + u8 paletteDesignation = (*fromAddress++) & 0x3f; + u8 startH = (*fromAddress++) & 0x1f; + u8 startV = (*fromAddress++) & 0x1f; + u8 endH = (*fromAddress++) & 0x1f; + u8 endV = (*fromAddress++) & 0x1f; + + u8 *toAddress = gbSgbATF; + + for (u8 y = 0; y < 18; y++) + { + for (u8 x = 0; x < 20; x++) + { + if (x < startH || y < startV || + x > endH || y > endV) + { + // outside + if (controlCode & 0x04) + *toAddress = (paletteDesignation >> 4) & 0x03; + } + else if (x > startH && x < endH && + y > startV && y < endV) + { + // inside + if (controlCode & 0x01) + *toAddress = paletteDesignation & 0x03; + } + else + { + // surrounding line + if (controlCode & 0x02) + *toAddress = (paletteDesignation >> 2) & 0x03; + else if (controlCode == 0x01) + *toAddress = paletteDesignation & 0x03; + } + toAddress++; + } + } + nDataSet--; + } +} + +void gbSgbSetColumnPalette(u8 col, u8 p) +{ + // if(col < 0) + // col = 0; + if (col > 19) + col = 19; + + p &= 3; + + u8 *toAddress = &gbSgbATF[col]; + + for (u8 y = 0; y < 18; y++) + { + *toAddress = p; + toAddress += 20; + } +} + +void gbSgbSetRowPalette(u8 row, u8 p) +{ + // if(row < 0) + // row = 0; + if (row > 17) + row = 17; + + p &= 3; + + u8 *toAddress = &gbSgbATF[row * 20]; + + for (u8 x = 0; x < 20; x++) + { + *toAddress++ = p; + } +} + +void gbSgbAttributeDivide() +{ + u8 control = gbSgbPacket[1]; + u8 coord = gbSgbPacket[2]; + u8 colorBR = control & 3; + u8 colorAL = (control >> 2) & 3; + u8 colorOL = (control >> 4) & 3; + + if (control & 0x40) + { + if (coord > 17) + coord = 17; + + for (u8 i = 0; i < 18; i++) + { + if (i < coord) + gbSgbSetRowPalette(i, colorAL); + else if (i > coord) + gbSgbSetRowPalette(i, colorBR); + else + gbSgbSetRowPalette(i, colorOL); + } + } + else + { + if (coord > 19) + coord = 19; + + for (u8 i = 0; i < 20; i++) + { + if (i < coord) + gbSgbSetColumnPalette(i, colorAL); + else if (i > coord) + gbSgbSetColumnPalette(i, colorBR); + else + gbSgbSetColumnPalette(i, colorOL); + } + } +} + +void gbSgbAttributeLine() +{ + u8 *fromAddress = &gbSgbPacket[1]; + + u8 nDataSet = *fromAddress++; + + if (nDataSet > 0x6e) + nDataSet = 0x6e; + + while (nDataSet) + { + u8 line = *fromAddress++; + u8 num = line & 0x1f; + u8 pal = (line >> 5) & 0x03; + if (line & 0x80) + { + if (num > 17) + num = 17; + gbSgbSetRowPalette(num, pal); + } + else + { + if (num > 19) + num = 19; + gbSgbSetColumnPalette(num, pal); + } + nDataSet--; + } +} + +void gbSgbAttributeCharacter() +{ + u8 startH = gbSgbPacket[1] & 0x1f; + u8 startV = gbSgbPacket[2] & 0x1f; + int nDataSet = READ16LE(((u16 *)&gbSgbPacket[3])); + int style = gbSgbPacket[5] & 1; + if (startH > 19) + startH = 19; + if (startV > 17) + startV = 17; + + u8 s = 6; + u8 *fromAddress = &gbSgbPacket[6]; + u8 v = *fromAddress++; + + if (style) + { + while (nDataSet) + { + u8 p = (v >> s) & 3; + gbSgbATF[startV * 20 + startH] = p; + startV++; + if (startV == 18) + { + startV = 0; + startH++; + if (startH == 20) + break; + } + + if (s) + s -= 2; + else + { + s = 6; + v = *fromAddress++; + nDataSet--; + } + } + } + else + { + while (nDataSet) + { + u8 p = (v >> s) & 3; + gbSgbATF[startV * 20 + startH] = p; + startH++; + if (startH == 20) + { + startH = 0; + startV++; + if (startV == 18) + break; + } + + if (s) + s -= 2; + else + { + s = 6; + v = *fromAddress++; + nDataSet--; + } + } + } +} + +void gbSgbSetATFList() +{ + gbSgbRenderScreenToBuffer(); + + u8 *fromAddress = gbSgbScreenBuffer; + u8 *toAddress = gbSgbATFList; + + for (int i = 0; i < 45; i++) + { + for (int j = 0; j < 90; j++) + { + u8 v = *fromAddress++; + u8 s = 6; + if (i == 2) + s = 6; + for (int k = 0; k < 4; k++) + { + *toAddress++ = (v >> s) & 0x03; + s -= 2; + } + } + } +} + +void gbSgbMaskEnable() +{ + int gbSgbMaskFlag = gbSgbPacket[1] & 3; + + gbSgbMask = gbSgbMaskFlag; + + switch (gbSgbMaskFlag) + { + case 1: + break; + case 2: + gbSgbFillScreen(0x0000); + // memset(&gbPalette[0], 0, 128*sizeof(u16)); + break; + case 3: + gbSgbFillScreen(gbPalette[0]); + break; + } + if (!gbSgbMask) + { + if (gbBorderOn) + gbSgbRenderBorder(); + } +} + +void gbSgbChrTransfer() +{ + gbSgbRenderScreenToBuffer(); + + int address = (gbSgbPacket[1] & 1) * (128 * 32); + + if (gbSgbPacket[1] & 1) + gbSgbCGBSupport |= 2; + else + gbSgbCGBSupport |= 1; + + memcpy(&gbSgbBorderChar[address], gbSgbScreenBuffer, 128 * 32); + + if (gbBorderAutomatic && !gbBorderOn && gbSgbCGBSupport > 4) + { + gbBorderOn = 1; + systemGbBorderOn(); + } + + if (gbBorderOn && !gbSgbMask) + gbSgbRenderBorder(); + + if (gbSgbMode && gbCgbMode && gbSgbCGBSupport == 7) + { + gbSgbCGBSupport = 0; + gbSgbMode = 2; + gbSgbMask = 0; + gbSgbRenderBorder(); + gbReset(); + } + + if (gbSgbCGBSupport > 4) + gbSgbCGBSupport = 0; +} + +void gbSgbMultiRequest() +{ + if (gbSgbPacket[1] & 1) + { + gbSgbMultiplayer = 1; + if (gbSgbPacket[1] & 2) + gbSgbFourPlayers = 1; + else + gbSgbFourPlayers = 0; + gbSgbNextController = 0x0e; + } + else + { + gbSgbFourPlayers = 0; + gbSgbMultiplayer = 0; + gbSgbNextController = 0x0f; + } +} + +void gbSgbCommand() +{ + int command = gbSgbPacket[0] >> 3; + // int nPacket = gbSgbPacket[0] & 7; + + switch (command) + { + case 0x00: + gbSgbSetPalette(0, 1, (u16 *)&gbSgbPacket[1]); + break; + case 0x01: + gbSgbSetPalette(2, 3, (u16 *)&gbSgbPacket[1]); + break; + case 0x02: + gbSgbSetPalette(0, 3, (u16 *)&gbSgbPacket[1]); + break; + case 0x03: + gbSgbSetPalette(1, 2, (u16 *)&gbSgbPacket[1]); + break; + case 0x04: + gbSgbAttributeBlock(); + break; + case 0x05: + gbSgbAttributeLine(); + break; + case 0x06: + gbSgbAttributeDivide(); + break; + case 0x07: + gbSgbAttributeCharacter(); + break; + case 0x0a: + gbSgbSetPalette(); + break; + case 0x0b: + gbSgbScpPalette(); + break; + case 0x11: + gbSgbMultiRequest(); + break; + case 0x13: + gbSgbChrTransfer(); + break; + case 0x14: + gbSgbPicture(); + break; + case 0x15: + gbSgbSetATFList(); + break; + case 0x16: + gbSgbSetATF(gbSgbPacket[1] & 0x3f); + break; + case 0x17: + gbSgbMaskEnable(); + break; + } +} + +void gbSgbResetPacketState() +{ + gbSgbPacketState = GBSGB_NONE; + gbSgbPacketTimeout = 0; +} + +void gbSgbDoBitTransfer(u8 value) +{ + value = value & 0x30; + switch (gbSgbPacketState) + { + case GBSGB_NONE: + if (value == 0) + { + gbSgbPacketState = GBSGB_RESET; + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + } + else if (value == 0x30) + { + if (gbSgbMultiplayer) + { + if ((gbSgbReadingController & 7) == 7) + { + gbSgbReadingController = 0; + if (gbSgbMultiplayer) + { + gbSgbNextController--; + if (gbSgbFourPlayers) + { + if (gbSgbNextController == 0x0b) + gbSgbNextController = 0x0f; + } + else + { + if (gbSgbNextController == 0x0d) + gbSgbNextController = 0x0f; + } + } + } + else + { + gbSgbReadingController &= 3; + } + } + gbSgbPacketTimeout = 0; + } + else + { + if (value == 0x10) + gbSgbReadingController |= 0x2; + else if (value == 0x20) + gbSgbReadingController |= 0x01; + gbSgbPacketTimeout = 0; + } + gbSgbPacketTimeout = 0; + break; + case GBSGB_RESET: + if (value == 0x30) + { + gbSgbPacketState = GBSGB_PACKET_TRANSMIT; + gbSgbPacketByte = 0; + gbSgbPacketNBits = 0; + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + } + else if (value == 0x00) + { + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + gbSgbPacketState = GBSGB_RESET; + } + else + { + gbSgbPacketState = GBSGB_NONE; + gbSgbPacketTimeout = 0; + } + break; + case GBSGB_PACKET_TRANSMIT: + if (value == 0) + { + gbSgbPacketState = GBSGB_RESET; + gbSgbPacketTimeout = 0; + } + else if (value == 0x30) + { + if (gbSgbPacketNBits == 128) + { + gbSgbPacketNBits = 0; + gbSgbPacketByte = 0; + gbSgbPacketNumber++; + gbSgbPacketTimeout = 0; + if (gbSgbPacketNumber == (gbSgbPacket[0] & 7)) + { + gbSgbCommand(); + gbSgbPacketNumber = 0; + gbSgbPacketState = GBSGB_NONE; + gbSgbPacketTimeout = 0; + } + } + else + { + if (gbSgbPacketNBits < 128) + { + gbSgbPacket[gbSgbPacketNumber * 16 + gbSgbPacketByte] >>= 1; + gbSgbPacket[gbSgbPacketNumber * 16 + gbSgbPacketByte] |= gbSgbBit; + gbSgbPacketNBits++; + if (!(gbSgbPacketNBits & 7)) + { + gbSgbPacketByte++; + } + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + } + } + } + else + { + if (value == 0x20) + gbSgbBit = 0x00; + else + gbSgbBit = 0x80; + gbSgbPacketTimeout = GBSGB_PACKET_TIMEOUT; + } + gbSgbReadingController = 0; + break; + default: + gbSgbPacketState = GBSGB_NONE; + gbSgbPacketTimeout = 0; + break; + } +} + +variable_desc gbSgbSaveStruct[] = { + { &gbSgbMask, sizeof(int32) }, + { &gbSgbPacketState, sizeof(int32) }, + { &gbSgbBit, sizeof(int32) }, + { &gbSgbPacketNBits, sizeof(int32) }, + { &gbSgbPacketByte, sizeof(int32) }, + { &gbSgbPacketNumber, sizeof(int32) }, + { &gbSgbMultiplayer, sizeof(int32) }, + { &gbSgbNextController, sizeof(u8) }, + { &gbSgbReadingController, sizeof(u8) }, + { NULL, 0 } +}; + +variable_desc gbSgbSaveStructV3[] = { + { &gbSgbMask, sizeof(int32) }, + { &gbSgbPacketState, sizeof(int32) }, + { &gbSgbBit, sizeof(int32) }, + { &gbSgbPacketNBits, sizeof(int32) }, + { &gbSgbPacketByte, sizeof(int32) }, + { &gbSgbPacketNumber, sizeof(int32) }, + { &gbSgbMultiplayer, sizeof(int32) }, + { &gbSgbNextController, sizeof(u8) }, + { &gbSgbReadingController, sizeof(u8) }, + { &gbSgbFourPlayers, sizeof(int32) }, + { NULL, 0 } +}; + +void gbSgbSaveGame(gzFile gzFile) +{ + utilWriteData(gzFile, gbSgbSaveStructV3); + + utilGzWrite(gzFile, gbSgbBorder, 2048); + utilGzWrite(gzFile, gbSgbBorderChar, 32 * 256); + + utilGzWrite(gzFile, gbSgbPacket, 16 * 7); + + utilGzWrite(gzFile, gbSgbSCPPalette, 4 * 512 * sizeof(u16)); + utilGzWrite(gzFile, gbSgbATF, 20 * 18); + utilGzWrite(gzFile, gbSgbATFList, 45 * 20 * 18); + + utilGzWrite(gzFile, gbSgbScreenBuffer, 4160); + utilGzWrite(gzFile, &gbSgbMode, sizeof(gbSgbMode)); + utilGzWrite(gzFile, &gbSgbCGBSupport, sizeof(gbSgbCGBSupport)); + utilGzWrite(gzFile, &gbSgbPacketTimeout, sizeof(gbSgbPacketTimeout)); +} + +void gbSgbReadGame(gzFile gzFile, int version) +{ + if (version >= 3) + utilReadData(gzFile, gbSgbSaveStructV3); + else + { + utilReadData(gzFile, gbSgbSaveStruct); + gbSgbFourPlayers = 0; + } + + if (version >= 8) + { + utilGzRead(gzFile, gbSgbBorder, 2048); + utilGzRead(gzFile, gbSgbBorderChar, 32 * 256); + } + + utilGzRead(gzFile, gbSgbPacket, 16 * 7); + + utilGzRead(gzFile, gbSgbSCPPalette, 4 * 512 * sizeof(u16)); + utilGzRead(gzFile, gbSgbATF, 20 * 18); + utilGzRead(gzFile, gbSgbATFList, 45 * 20 * 18); + + if (version >= 11) + { + utilGzRead(gzFile, gbSgbScreenBuffer, 4160); + utilGzRead(gzFile, &gbSgbMode, sizeof(gbSgbMode)); + utilGzRead(gzFile, &gbSgbCGBSupport, sizeof(gbSgbCGBSupport)); + utilGzRead(gzFile, &gbSgbPacketTimeout, sizeof(gbSgbPacketTimeout)); + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbSGB.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbSGB.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +#ifndef VBA_GB_SGB_H +#define VBA_GB_SGB_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +void gbSgbInit(); +void gbSgbShutdown(); +void gbSgbCommand(); +void gbSgbResetPacketState(); +void gbSgbReset(); +void gbSgbDoBitTransfer(u8); +void gbSgbSaveGame(gzFile); +void gbSgbReadGame(gzFile, int version); +void gbSgbRenderBorder(); + +extern u8 gbSgbATF[20*18]; +extern int32 gbSgbMode; +extern int32 gbSgbMask; +extern int32 gbSgbMultiplayer; +extern u8 gbSgbNextController; +extern int32 gbSgbPacketTimeout; +extern u8 gbSgbReadingController; +extern int32 gbSgbFourPlayers; + +#endif // VBA_GB_SGB_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbSound.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbSound.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1139 @@ +#ifdef WIN32 +# include "../win32/stdafx.h" +# include "../win32/VBA.h" +#endif + +#include +#include + +#include "../common/System.h" +#include "../common/Util.h" +//#include "../Blip_Buffer.h" +#include "gbGlobals.h" +#include "gbSound.h" + +#ifndef countof +#define countof(a) (sizeof(a) / sizeof(a[0])) +#endif + +extern u8 soundBuffer[6][735]; +extern u16 soundFinalWave[1470]; +extern u16 soundFrameSound[735 * 30 * 2]; +extern int32 soundVolume; + +soundtick_t GB_USE_TICKS_AS = 24; // (1048576.0/44100.0); // FIXME: (4194304.0/70224.0)(fps) vs 60.0fps? + +#define SOUND_MAGIC 0x60000000 +#define SOUND_MAGIC_2 0x30000000 +#define NOISE_MAGIC (2097152.0 / 44100.0) + +extern int32 speed; + +extern u8 soundWavePattern[4][32]; + +extern u32 soundBufferLen; +extern u32 soundBufferTotalLen; +extern int32 soundQuality; +extern int32 soundPaused; +extern int32 soundPlay; +extern soundtick_t soundTicks; +extern soundtick_t SOUND_CLOCK_TICKS; +extern u32 soundNextPosition; + +extern int32 soundLevel1; +extern int32 soundLevel2; +extern int32 soundBalance; +extern int32 soundMasterOn; +extern u32 soundIndex; +extern u32 soundBufferIndex; +extern int32 soundFrameSoundWritten; +int32 soundVIN = 0; +extern int32 soundDebug; + +extern int32 sound1On; +extern int32 sound1ATL; +extern int32 sound1Skip; +extern int32 sound1Index; +extern int32 sound1Continue; +extern int32 sound1EnvelopeVolume; +extern int32 sound1EnvelopeATL; +extern int32 sound1EnvelopeUpDown; +extern int32 sound1EnvelopeATLReload; +extern int32 sound1SweepATL; +extern int32 sound1SweepATLReload; +extern int32 sound1SweepSteps; +extern int32 sound1SweepUpDown; +extern int32 sound1SweepStep; +extern u8 * sound1Wave; + +extern int32 sound2On; +extern int32 sound2ATL; +extern int32 sound2Skip; +extern int32 sound2Index; +extern int32 sound2Continue; +extern int32 sound2EnvelopeVolume; +extern int32 sound2EnvelopeATL; +extern int32 sound2EnvelopeUpDown; +extern int32 sound2EnvelopeATLReload; +extern u8 * sound2Wave; + +extern int32 sound3On; +extern int32 sound3ATL; +extern int32 sound3Skip; +extern int32 sound3Index; +extern int32 sound3Continue; +extern int32 sound3OutputLevel; +extern int32 sound3Last; + +extern int32 sound4On; +extern int32 sound4Clock; +extern int32 sound4ATL; +extern int32 sound4Skip; +extern int32 sound4Index; +extern int32 sound4ShiftRight; +extern int32 sound4ShiftSkip; +extern int32 sound4ShiftIndex; +extern int32 sound4NSteps; +extern int32 sound4CountDown; +extern int32 sound4Continue; +extern int32 sound4EnvelopeVolume; +extern int32 sound4EnvelopeATL; +extern int32 sound4EnvelopeUpDown; +extern int32 sound4EnvelopeATLReload; + +extern int32 soundEnableFlag; +extern int32 soundMutedFlag; + +extern int32 soundFreqRatio[8]; +extern int32 soundShiftClock[16]; + +extern s16 soundFilter[4000]; +extern s16 soundLeft[5]; +extern s16 soundRight[5]; +extern int32 soundEchoIndex; +extern bool8 soundEcho; +extern bool8 soundLowPass; +extern bool8 soundReverse; +extern bool8 soundOffFlag; + +bool8 gbDigitalSound = false; + +void gbSoundEvent(register u16 address, register int data) +{ + int freq = 0; + + gbMemory[address] = data; + +#ifndef FINAL_VERSION + if (soundDebug) + { + // don't translate. debug only + log("Sound event: %08lx %02x\n", address, data); + } +#endif + switch (address) + { + case NR10: + sound1SweepATL = sound1SweepATLReload = 344 * ((data >> 4) & 7); + sound1SweepSteps = data & 7; + sound1SweepUpDown = data & 0x08; + sound1SweepStep = 0; + break; + case NR11: + sound1Wave = soundWavePattern[data >> 6]; + sound1ATL = 172 * (64 - (data & 0x3f)); + break; + case NR12: + sound1EnvelopeVolume = data >> 4; + sound1EnvelopeUpDown = data & 0x08; + sound1EnvelopeATLReload = sound1EnvelopeATL = 689 * (data & 7); + break; + case NR13: + freq = (((int)(gbMemory[NR14] & 7)) << 8) | data; + sound1ATL = 172 * (64 - (gbMemory[NR11] & 0x3f)); + freq = 2048 - freq; + if (freq) + { + sound1Skip = SOUND_MAGIC / freq; + } + else + sound1Skip = 0; + break; + case NR14: + freq = (((int)(data & 7) << 8) | gbMemory[NR13]); + freq = 2048 - freq; + sound1ATL = 172 * (64 - (gbMemory[NR11] & 0x3f)); + sound1Continue = data & 0x40; + if (freq) + { + sound1Skip = SOUND_MAGIC / freq; + } + else + sound1Skip = 0; + if (data & 0x80) + { + gbMemory[NR52] |= 1; + sound1EnvelopeVolume = gbMemory[NR12] >> 4; + sound1EnvelopeUpDown = gbMemory[NR12] & 0x08; + sound1ATL = 172 * (64 - (gbMemory[NR11] & 0x3f)); + sound1EnvelopeATLReload = sound1EnvelopeATL = 689 * (gbMemory[NR12] & 7); + sound1SweepATL = sound1SweepATLReload = 344 * ((gbMemory[NR10] >> 4) & 7); + sound1SweepSteps = gbMemory[NR10] & 7; + sound1SweepUpDown = gbMemory[NR10] & 0x08; + sound1SweepStep = 0; + + sound1Index = 0; + sound1On = 1; + } + break; + case NR21: + sound2Wave = soundWavePattern[data >> 6]; + sound2ATL = 172 * (64 - (data & 0x3f)); + break; + case NR22: + sound2EnvelopeVolume = data >> 4; + sound2EnvelopeUpDown = data & 0x08; + sound2EnvelopeATLReload = sound2EnvelopeATL = 689 * (data & 7); + break; + case NR23: + freq = (((int)(gbMemory[NR24] & 7)) << 8) | data; + sound2ATL = 172 * (64 - (gbMemory[NR21] & 0x3f)); + freq = 2048 - freq; + if (freq) + { + sound2Skip = SOUND_MAGIC / freq; + } + else + sound2Skip = 0; + break; + case NR24: + freq = (((int)(data & 7) << 8) | gbMemory[NR23]); + freq = 2048 - freq; + sound2ATL = 172 * (64 - (gbMemory[NR21] & 0x3f)); + sound2Continue = data & 0x40; + if (freq) + { + sound2Skip = SOUND_MAGIC / freq; + } + else + sound2Skip = 0; + if (data & 0x80) + { + gbMemory[NR52] |= 2; + sound2EnvelopeVolume = gbMemory[NR22] >> 4; + sound2EnvelopeUpDown = gbMemory[NR22] & 0x08; + sound2ATL = 172 * (64 - (gbMemory[NR21] & 0x3f)); + sound2EnvelopeATLReload = sound2EnvelopeATL = 689 * (gbMemory[NR22] & 7); + + sound2Index = 0; + sound2On = 1; + } + break; + case NR30: + if (!(data & 0x80)) + { + gbMemory[NR52] &= 0xfb; + sound3On = 0; + } + break; + case NR31: + sound3ATL = 172 * (256 - data); + break; + case NR32: + sound3OutputLevel = (data >> 5) & 3; + break; + case NR33: + freq = 2048 - (((int)(gbMemory[NR34] & 7) << 8) | data); + if (freq) + { + sound3Skip = SOUND_MAGIC_2 / freq; + } + else + sound3Skip = 0; + break; + case NR34: + freq = 2048 - (((data & 7) << 8) | (int)gbMemory[NR33]); + if (freq) + { + sound3Skip = SOUND_MAGIC_2 / freq; + } + else + { + sound3Skip = 0; + } + sound3Continue = data & 0x40; + if ((data & 0x80) && (gbMemory[NR30] & 0x80)) + { + gbMemory[NR52] |= 4; + sound3ATL = 172 * (256 - gbMemory[NR31]); + sound3Index = 0; + sound3On = 1; + } + break; + case NR41: + sound4ATL = 172 * (64 - (data & 0x3f)); + break; + case NR42: + sound4EnvelopeVolume = data >> 4; + sound4EnvelopeUpDown = data & 0x08; + sound4EnvelopeATLReload = sound4EnvelopeATL = 689 * (data & 7); + break; + case NR43: + freq = soundFreqRatio[data & 7]; + sound4NSteps = data & 0x08; + + sound4Skip = freq * NOISE_MAGIC; + + sound4Clock = data >> 4; + + freq = freq / soundShiftClock[sound4Clock]; + + sound4ShiftSkip = freq * NOISE_MAGIC; + + break; + case NR44: + sound4Continue = data & 0x40; + if (data & 0x80) + { + gbMemory[NR52] |= 8; + sound4EnvelopeVolume = gbMemory[NR42] >> 4; + sound4EnvelopeUpDown = gbMemory[NR42] & 0x08; + sound4ATL = 172 * (64 - (gbMemory[NR41] & 0x3f)); + sound4EnvelopeATLReload = sound4EnvelopeATL = 689 * (gbMemory[NR42] & 7); + + sound4On = 1; + + sound4Index = 0; + sound4ShiftIndex = 0; + + freq = soundFreqRatio[gbMemory[NR43] & 7]; + + sound4Skip = freq * NOISE_MAGIC; + + sound4NSteps = gbMemory[NR43] & 0x08; + + freq = freq / soundShiftClock[gbMemory[NR43] >> 4]; + + sound4ShiftSkip = freq * NOISE_MAGIC; + if (sound4NSteps) + sound4ShiftRight = 0x7f; + else + sound4ShiftRight = 0x7fff; + } + break; + case NR50: + soundVIN = data & 0x88; + soundLevel1 = data & 7; + soundLevel2 = (data >> 4) & 7; + break; + case NR51: + soundBalance = (data & soundEnableFlag); + gbMemory[address] = data; + break; + case NR52: + soundMasterOn = data & 0x80; + if (!(data & 0x80)) + { + sound1On = 0; + sound2On = 0; + sound3On = 0; + sound4On = 0; + } + break; + } + + gbDigitalSound = true; + + if (sound1On && sound1EnvelopeVolume != 0) + gbDigitalSound = false; + if (sound2On && sound2EnvelopeVolume != 0) + gbDigitalSound = false; + if (sound3On && sound3OutputLevel != 0) + gbDigitalSound = false; + if (sound4On && sound4EnvelopeVolume != 0) + gbDigitalSound = false; +} + +void gbSoundChannel1() +{ + int vol = sound1EnvelopeVolume; + + int freq = 0; + + int value = 0; + + if (sound1On && (sound1ATL || !sound1Continue)) + { + sound1Index += soundQuality * sound1Skip; + sound1Index &= 0x1fffffff; + + value = ((s8)sound1Wave[sound1Index >> 24]) * vol; + } + + soundBuffer[0][soundIndex] = value; + + if (sound1On) + { + if (sound1ATL) + { + sound1ATL -= soundQuality; + + if (sound1ATL <= 0 && sound1Continue) + { + gbMemory[NR52] &= 0xfe; + sound1On = 0; + } + } + + if (sound1EnvelopeATL) + { + sound1EnvelopeATL -= soundQuality; + + if (sound1EnvelopeATL <= 0) + { + if (sound1EnvelopeUpDown) + { + if (sound1EnvelopeVolume < 15) + sound1EnvelopeVolume++; + } + else + { + if (sound1EnvelopeVolume) + sound1EnvelopeVolume--; + } + + sound1EnvelopeATL += sound1EnvelopeATLReload; + } + } + + if (sound1SweepATL) + { + sound1SweepATL -= soundQuality; + + if (sound1SweepATL <= 0) + { + freq = (((int)(gbMemory[NR14] & 7) << 8) | gbMemory[NR13]); + + int updown = 1; + + if (sound1SweepUpDown) + updown = -1; + + int newfreq = 0; + if (sound1SweepSteps) + { + newfreq = freq + updown * freq / (1 << sound1SweepSteps); + if (newfreq == freq) + newfreq = 0; + } + else + newfreq = freq; + + if (newfreq < 0) + { + sound1SweepATL += sound1SweepATLReload; + } + else if (newfreq > 2047) + { + sound1SweepATL = 0; + sound1On = 0; + gbMemory[NR52] &= 0xfe; + } + else + { + sound1SweepATL += sound1SweepATLReload; + sound1Skip = SOUND_MAGIC / (2048 - newfreq); + + gbMemory[NR13] = newfreq & 0xff; + gbMemory[NR14] = (gbMemory[NR14] & 0xf8) | ((newfreq >> 8) & 7); + } + } + } + } +} + +void gbSoundChannel2() +{ + // int freq = 0; + int vol = sound2EnvelopeVolume; + + int value = 0; + + if (sound2On && (sound2ATL || !sound2Continue)) + { + sound2Index += soundQuality * sound2Skip; + sound2Index &= 0x1fffffff; + + value = ((s8)sound2Wave[sound2Index >> 24]) * vol; + } + + soundBuffer[1][soundIndex] = value; + + if (sound2On) + { + if (sound2ATL) + { + sound2ATL -= soundQuality; + + if (sound2ATL <= 0 && sound2Continue) + { + gbMemory[NR52] &= 0xfd; + sound2On = 0; + } + } + + if (sound2EnvelopeATL) + { + sound2EnvelopeATL -= soundQuality; + + if (sound2EnvelopeATL <= 0) + { + if (sound2EnvelopeUpDown) + { + if (sound2EnvelopeVolume < 15) + sound2EnvelopeVolume++; + } + else + { + if (sound2EnvelopeVolume) + sound2EnvelopeVolume--; + } + sound2EnvelopeATL += sound2EnvelopeATLReload; + } + } + } +} + +void gbSoundChannel3() +{ + int value = sound3Last; + + if (sound3On && (sound3ATL || !sound3Continue)) + { + sound3Index += soundQuality * sound3Skip; + sound3Index &= 0x1fffffff; + + value = gbMemory[0xff30 + (sound3Index >> 25)]; + + if ((sound3Index & 0x01000000)) + { + value &= 0x0f; + } + else + { + value >>= 4; + } + + value -= 8; + value *= 2; + + switch (sound3OutputLevel) + { + case 0: + value = 0; + break; + case 1: + break; + case 2: + value = (value >> 1); + break; + case 3: + value = (value >> 2); + break; + } + //value += 1; + sound3Last = value; + } + + soundBuffer[2][soundIndex] = value; + + if (sound3On) + { + if (sound3ATL) + { + sound3ATL -= soundQuality; + + if (sound3ATL <= 0 && sound3Continue) + { + gbMemory[NR52] &= 0xfb; + sound3On = 0; + } + } + } +} + +void gbSoundChannel4() +{ + int vol = sound4EnvelopeVolume; + + int value = 0; + + if (sound4Clock <= 0x0c) + { + if (sound4On && (sound4ATL || !sound4Continue)) + { + #define NOISE_ONE_SAMP_SCALE 0x200000 + + sound4Index += soundQuality * sound4Skip; + sound4ShiftIndex += soundQuality * sound4ShiftSkip; + + if (sound4NSteps) + { + while (sound4ShiftIndex >= NOISE_ONE_SAMP_SCALE) + { + sound4ShiftRight = (((sound4ShiftRight << 6) ^ + (sound4ShiftRight << 5)) & 0x40) | + (sound4ShiftRight >> 1); + sound4ShiftIndex -= NOISE_ONE_SAMP_SCALE; + } + } + else + { + while (sound4ShiftIndex >= NOISE_ONE_SAMP_SCALE) + { + sound4ShiftRight = (((sound4ShiftRight << 14) ^ + (sound4ShiftRight << 13)) & 0x4000) | + (sound4ShiftRight >> 1); + + sound4ShiftIndex -= NOISE_ONE_SAMP_SCALE; + } + } + + sound4Index %= NOISE_ONE_SAMP_SCALE; + sound4ShiftIndex %= NOISE_ONE_SAMP_SCALE; + + value = ((sound4ShiftRight & 1) * 2 - 1) * vol; + } + else + { + value = 0; + } + } + + soundBuffer[3][soundIndex] = value; + + if (sound4On) + { + if (sound4ATL) + { + sound4ATL -= soundQuality; + + if (sound4ATL <= 0 && sound4Continue) + { + gbMemory[NR52] &= 0xfd; + sound4On = 0; + } + } + + if (sound4EnvelopeATL) + { + sound4EnvelopeATL -= soundQuality; + + if (sound4EnvelopeATL <= 0) + { + if (sound4EnvelopeUpDown) + { + if (sound4EnvelopeVolume < 15) + sound4EnvelopeVolume++; + } + else + { + if (sound4EnvelopeVolume) + sound4EnvelopeVolume--; + } + sound4EnvelopeATL += sound4EnvelopeATLReload; + } + } + } +} + +void gbSoundMix() +{ + int res = 0; + + if (gbMemory) + soundBalance = (gbMemory[NR51] & soundEnableFlag & ~soundMutedFlag); + + if (soundBalance & 16) + { + res += ((s8)soundBuffer[0][soundIndex]); + } + if (soundBalance & 32) + { + res += ((s8)soundBuffer[1][soundIndex]); + } + if (soundBalance & 64) + { + res += ((s8)soundBuffer[2][soundIndex]); + } + if (soundBalance & 128) + { + res += ((s8)soundBuffer[3][soundIndex]); + } + + if (gbDigitalSound) + res = soundLevel1 * 256; + else + res *= soundLevel1 * 60; + + if (soundEcho) + { + res *= 2; + res += soundFilter[soundEchoIndex]; + res /= 2; + soundFilter[soundEchoIndex++] = res; + } + + if (soundLowPass) + { + soundLeft[4] = soundLeft[3]; + soundLeft[3] = soundLeft[2]; + soundLeft[2] = soundLeft[1]; + soundLeft[1] = soundLeft[0]; + soundLeft[0] = res; + res = (soundLeft[4] + 2 * soundLeft[3] + 8 * soundLeft[2] + 2 * soundLeft[1] + + soundLeft[0]) / 14; + } + + bool noSpecialEffects = false; +#if (defined(WIN32) && !defined(SDL)) + if (theApp.soundRecording || theApp.aviRecording || theApp.nvAudioLog) + noSpecialEffects = true; +#endif + + if (!noSpecialEffects) + { + switch (soundVolume) + { + case 0: + case 1: + case 2: + case 3: + res *= (soundVolume + 1); + break; + case 4: + res >>= 2; + break; + case 5: + res >>= 1; + break; + } + } + + if (res > 32767) + res = 32767; + if (res < -32768) + res = -32768; + + if (soundReverse && !noSpecialEffects) + { + soundFinalWave[++soundBufferIndex] = res; + if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) + /*assert(false)*/; + else + soundFrameSound[++soundFrameSoundWritten] = res; + } + else + { + soundFinalWave[soundBufferIndex++] = res; + if (soundFrameSoundWritten >= countof(soundFrameSound)) + /*assert(false)*/; + else + soundFrameSound[soundFrameSoundWritten++] = res; + } + + res = 0; + + if (soundBalance & 1) + { + res += ((s8)soundBuffer[0][soundIndex]); + } + if (soundBalance & 2) + { + res += ((s8)soundBuffer[1][soundIndex]); + } + if (soundBalance & 4) + { + res += ((s8)soundBuffer[2][soundIndex]); + } + if (soundBalance & 8) + { + res += ((s8)soundBuffer[3][soundIndex]); + } + + if (gbDigitalSound) + res = soundLevel2 * 256; + else + res *= soundLevel2 * 60; + + if (soundEcho) + { + res *= 2; + res += soundFilter[soundEchoIndex]; + res /= 2; + soundFilter[soundEchoIndex++] = res; + + if (soundEchoIndex >= 4000) + soundEchoIndex = 0; + } + + if (soundLowPass) + { + soundRight[4] = soundRight[3]; + soundRight[3] = soundRight[2]; + soundRight[2] = soundRight[1]; + soundRight[1] = soundRight[0]; + soundRight[0] = res; + res = (soundRight[4] + 2 * soundRight[3] + 8 * soundRight[2] + 2 * soundRight[1] + + soundRight[0]) / 14; + } + + if (!noSpecialEffects) + { + switch (soundVolume) + { + case 0: + case 1: + case 2: + case 3: + res *= (soundVolume + 1); + break; + case 4: + res >>= 2; + break; + case 5: + res >>= 1; + break; + } + } + + if (res > 32767) + res = 32767; + if (res < -32768) + res = -32768; + + if (soundReverse && !noSpecialEffects) + { + soundFinalWave[-1 + soundBufferIndex++] = res; + if ((soundFrameSoundWritten) >= countof(soundFrameSound)) + /*assert(false)*/; + else + soundFrameSound[-1 + soundFrameSoundWritten++] = res; + } + else + { + soundFinalWave[soundBufferIndex++] = res; + if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) + /*assert(false)*/; + else + soundFrameSound[soundFrameSoundWritten++] = res; + } +} + +void gbSoundTick() +{ + if (systemSoundOn) + { + if (soundMasterOn) + { + gbSoundChannel1(); + gbSoundChannel2(); + gbSoundChannel3(); + gbSoundChannel4(); + + gbSoundMix(); + } + else + { + soundFinalWave[soundBufferIndex++] = 0; + soundFinalWave[soundBufferIndex++] = 0; + if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) + /*assert(false)*/; + else + { + soundFrameSound[soundFrameSoundWritten++] = 0; + soundFrameSound[soundFrameSoundWritten++] = 0; + } + } + + soundIndex++; + + if (2 * soundBufferIndex >= soundBufferLen) + { + if (systemSoundOn) + { + if (soundPaused) + { + extern void soundResume(); + soundResume(); + } + + systemSoundWriteToBuffer(); + } + soundIndex = 0; + soundBufferIndex = 0; + } + } +} + +void gbSoundReset() +{ + soundPaused = 1; + soundPlay = 0; + SOUND_CLOCK_TICKS = soundQuality * GB_USE_TICKS_AS; +// soundTicks = SOUND_CLOCK_TICKS; + soundTicks = 0; + soundNextPosition = 0; + soundMasterOn = 1; + soundIndex = 0; + soundBufferIndex = 0; + soundLevel1 = 7; + soundLevel2 = 7; + soundVIN = 0; + + sound1On = 0; + sound1ATL = 0; + sound1Skip = 0; + sound1Index = 0; + sound1Continue = 0; + sound1EnvelopeVolume = 0; + sound1EnvelopeATL = 0; + sound1EnvelopeUpDown = 0; + sound1EnvelopeATLReload = 0; + sound1SweepATL = 0; + sound1SweepATLReload = 0; + sound1SweepSteps = 0; + sound1SweepUpDown = 0; + sound1SweepStep = 0; + sound1Wave = soundWavePattern[2]; + + sound2On = 0; + sound2ATL = 0; + sound2Skip = 0; + sound2Index = 0; + sound2Continue = 0; + sound2EnvelopeVolume = 0; + sound2EnvelopeATL = 0; + sound2EnvelopeUpDown = 0; + sound2EnvelopeATLReload = 0; + sound2Wave = soundWavePattern[2]; + + sound3On = 0; + sound3ATL = 0; + sound3Skip = 0; + sound3Index = 0; + sound3Continue = 0; + sound3OutputLevel = 0; + + sound4On = 0; + sound4Clock = 0; + sound4ATL = 0; + sound4Skip = 0; + sound4Index = 0; + sound4ShiftRight = 0x7f; + sound4NSteps = 0; + sound4CountDown = 0; + sound4Continue = 0; + sound4EnvelopeVolume = 0; + sound4EnvelopeATL = 0; + sound4EnvelopeUpDown = 0; + sound4EnvelopeATLReload = 0; + + // don't translate + if (soundDebug) + { + log("*** Sound Init ***\n"); + } + + gbSoundEvent(0xff10, 0x80); + gbSoundEvent(0xff11, 0xbf); + gbSoundEvent(0xff12, 0xf3); + gbSoundEvent(0xff14, 0xbf); + gbSoundEvent(0xff16, 0x3f); + gbSoundEvent(0xff17, 0x00); + gbSoundEvent(0xff19, 0xbf); + + gbSoundEvent(0xff1a, 0x7f); + gbSoundEvent(0xff1b, 0xff); + gbSoundEvent(0xff1c, 0xbf); + gbSoundEvent(0xff1e, 0xbf); + + gbSoundEvent(0xff20, 0xff); + gbSoundEvent(0xff21, 0x00); + gbSoundEvent(0xff22, 0x00); + gbSoundEvent(0xff23, 0xbf); + gbSoundEvent(0xff24, 0x77); + gbSoundEvent(0xff25, 0xf3); + + gbSoundEvent(0xff26, 0xf0); + + // don't translate + if (soundDebug) + { + log("*** Sound Init Complete ***\n"); + } + + sound1On = 0; + sound2On = 0; + sound3On = 0; + sound4On = 0; + + int addr = 0xff30; + + while (addr < 0xff40) + { + gbMemory[addr++] = 0x00; + gbMemory[addr++] = 0xff; + } + + memset(soundFinalWave, 0x00, soundBufferLen); + + memset(soundFilter, 0, sizeof(soundFilter)); + soundEchoIndex = 0; +} + +extern bool soundInit(); +extern void soundShutdown(); + +void gbSoundSetQuality(int quality) +{ + if (soundQuality != quality && systemSoundCanChangeQuality()) + { + if (!soundOffFlag) + soundShutdown(); + soundQuality = quality; + soundNextPosition = 0; + if (!soundOffFlag) + soundInit(); + SOUND_CLOCK_TICKS = (gbSpeed ? 2 : 1) * GB_USE_TICKS_AS * soundQuality; + soundIndex = 0; + soundBufferIndex = 0; + } + else + { + soundNextPosition = 0; + SOUND_CLOCK_TICKS = (gbSpeed ? 2 : 1) * GB_USE_TICKS_AS * soundQuality; + soundIndex = 0; + soundBufferIndex = 0; + } +} + +static int32 soundTicks_int32; +static int32 SOUND_CLOCK_TICKS_int32; +variable_desc gbSoundSaveStruct[] = { + { &soundPaused, sizeof(int32) }, + { &soundPlay, sizeof(int32) }, + { &soundTicks_int32, sizeof(int32) }, + { &SOUND_CLOCK_TICKS_int32, sizeof(int32) }, + { &soundLevel1, sizeof(int32) }, + { &soundLevel2, sizeof(int32) }, + { &soundBalance, sizeof(int32) }, + { &soundMasterOn, sizeof(int32) }, + { &soundIndex, sizeof(int32) }, + { &soundVIN, sizeof(int32) }, + { &sound1On, sizeof(int32) }, + { &sound1ATL, sizeof(int32) }, + { &sound1Skip, sizeof(int32) }, + { &sound1Index, sizeof(int32) }, + { &sound1Continue, sizeof(int32) }, + { &sound1EnvelopeVolume, sizeof(int32) }, + { &sound1EnvelopeATL, sizeof(int32) }, + { &sound1EnvelopeATLReload, sizeof(int32) }, + { &sound1EnvelopeUpDown, sizeof(int32) }, + { &sound1SweepATL, sizeof(int32) }, + { &sound1SweepATLReload, sizeof(int32) }, + { &sound1SweepSteps, sizeof(int32) }, + { &sound1SweepUpDown, sizeof(int32) }, + { &sound1SweepStep, sizeof(int32) }, + { &sound2On, sizeof(int32) }, + { &sound2ATL, sizeof(int32) }, + { &sound2Skip, sizeof(int32) }, + { &sound2Index, sizeof(int32) }, + { &sound2Continue, sizeof(int32) }, + { &sound2EnvelopeVolume, sizeof(int32) }, + { &sound2EnvelopeATL, sizeof(int32) }, + { &sound2EnvelopeATLReload, sizeof(int32) }, + { &sound2EnvelopeUpDown, sizeof(int32) }, + { &sound3On, sizeof(int32) }, + { &sound3ATL, sizeof(int32) }, + { &sound3Skip, sizeof(int32) }, + { &sound3Index, sizeof(int32) }, + { &sound3Continue, sizeof(int32) }, + { &sound3OutputLevel, sizeof(int32) }, + { &sound4On, sizeof(int32) }, + { &sound4ATL, sizeof(int32) }, + { &sound4Skip, sizeof(int32) }, + { &sound4Index, sizeof(int32) }, + { &sound4Clock, sizeof(int32) }, + { &sound4ShiftRight, sizeof(int32) }, + { &sound4ShiftSkip, sizeof(int32) }, + { &sound4ShiftIndex, sizeof(int32) }, + { &sound4NSteps, sizeof(int32) }, + { &sound4CountDown, sizeof(int32) }, + { &sound4Continue, sizeof(int32) }, + { &sound4EnvelopeVolume, sizeof(int32) }, + { &sound4EnvelopeATL, sizeof(int32) }, + { &sound4EnvelopeATLReload, sizeof(int32) }, + { &sound4EnvelopeUpDown, sizeof(int32) }, + { &soundEnableFlag, sizeof(int32) }, + { NULL, 0 } +}; + +//variable_desc gbSoundSaveStructV2[] = { +// { &soundTicks, sizeof(soundtick_t) }, +// { &SOUND_CLOCK_TICKS, sizeof(soundtick_t) }, +// { &GB_USE_TICKS_AS, sizeof(soundtick_t) }, +// { NULL, 0 } +//}; + +void gbSoundSaveGame(gzFile gzFile) +{ + soundTicks_int32 = (int32) soundTicks; + SOUND_CLOCK_TICKS_int32 = (int32) SOUND_CLOCK_TICKS; + + utilWriteData(gzFile, gbSoundSaveStruct); + + utilGzWrite(gzFile, soundBuffer, 4 * 735); + utilGzWrite(gzFile, soundFinalWave, 2 * 735); + utilGzWrite(gzFile, &soundQuality, sizeof(int32)); + + //utilWriteData(gzFile, gbSoundSaveStructV2); +} + +void gbSoundReadGame(int version, gzFile gzFile) +{ + int32 oldSoundPaused = soundPaused; + int32 oldSoundEnableFlag = soundEnableFlag; + utilReadData(gzFile, gbSoundSaveStruct); + soundPaused = oldSoundPaused; + soundEnableFlag = oldSoundEnableFlag; + + soundBufferIndex = soundIndex * 2; + + utilGzRead(gzFile, soundBuffer, 4 * 735); + utilGzRead(gzFile, soundFinalWave, 2 * 735); + + if (version >= 7) + { + int quality = 1; + utilGzRead(gzFile, &quality, sizeof(int32)); + gbSoundSetQuality(quality); + } + else + { + soundQuality = -1; + gbSoundSetQuality(1); + } + + sound1Wave = soundWavePattern[gbMemory[NR11] >> 6]; + sound2Wave = soundWavePattern[gbMemory[NR21] >> 6]; + + //if(version >= 14) { + // utilReadData(gzFile, gbSoundSaveStructV2); + //} + //else { + soundTicks = (soundtick_t) soundTicks_int32; + SOUND_CLOCK_TICKS = (soundtick_t) SOUND_CLOCK_TICKS_int32; + //} +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gb/gbSound.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gb/gbSound.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,51 @@ +#ifndef VBA_GB_SOUND_H +#define VBA_GB_SOUND_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#define NR10 0xff10 +#define NR11 0xff11 +#define NR12 0xff12 +#define NR13 0xff13 +#define NR14 0xff14 +#define NR21 0xff16 +#define NR22 0xff17 +#define NR23 0xff18 +#define NR24 0xff19 +#define NR30 0xff1a +#define NR31 0xff1b +#define NR32 0xff1c +#define NR33 0xff1d +#define NR34 0xff1e +#define NR41 0xff20 +#define NR42 0xff21 +#define NR43 0xff22 +#define NR44 0xff23 +#define NR50 0xff24 +#define NR51 0xff25 +#define NR52 0xff26 + +#define SOUND_EVENT(address, value) \ + gbSoundEvent(address, value) + +extern void gbSoundTick(); +extern void gbSoundPause(); +extern void gbSoundResume(); +extern void gbSoundEnable(int); +extern void gbSoundDisable(int); +extern int gbSoundGetEnable(); +extern void gbSoundReset(); +extern void gbSoundSaveGame(gzFile); +extern void gbSoundReadGame(int, gzFile); +extern void gbSoundEvent(register u16, register int); +extern void gbSoundSetQuality(int); + +typedef int32 soundtick_t; + +extern soundtick_t soundTicks; +extern int32 soundQuality; +extern soundtick_t SOUND_CLOCK_TICKS; + +#endif // VBA_GB_SOUND_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/EEprom.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/EEprom.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,210 @@ +#include + +#include "GBA.h" // for SAVE_GAME_VERSION_3 +#include "EEprom.h" +#include "../common/System.h" +#include "../common/Util.h" + +extern int32 cpuDmaCount; + +int32 eepromMode = EEPROM_IDLE; +int32 eepromByte = 0; +int32 eepromBits = 0; +int32 eepromAddress = 0; +u8 eepromData[0x2000]; +u8 eepromBuffer[16]; +bool8 eepromInUse = false; +int32 eepromSize = 512; + +variable_desc eepromSaveData[] = { + { &eepromMode, sizeof(int32) }, + { &eepromByte, sizeof(int32) }, + { &eepromBits, sizeof(int32) }, + { &eepromAddress, sizeof(int32) }, + { &eepromInUse, sizeof(bool8) }, + { &eepromData[0], 512 }, + { &eepromBuffer[0], 16 }, + { NULL, 0 } +}; + +void eepromReset() +{ + eepromMode = EEPROM_IDLE; + eepromByte = 0; + eepromBits = 0; + eepromAddress = 0; + eepromInUse = false; + eepromSize = 512; +} + +void eepromErase() +{ + memset(eepromData, 0, 0x2000*sizeof(u8)); + eepromMode = EEPROM_IDLE; + eepromByte = 0; + eepromBits = 0; + eepromAddress = 0; + memset(eepromBuffer, 0, 16*sizeof(u8)); + eepromInUse = false; + eepromSize = 512; +} + +void eepromSaveGame(gzFile gzFile) +{ + utilWriteData(gzFile, eepromSaveData); + utilWriteInt(gzFile, eepromSize); + utilGzWrite(gzFile, eepromData, 0x2000); +} + +void eepromReadGame(gzFile gzFile, int version) +{ + utilReadData(gzFile, eepromSaveData); + if (version >= SAVE_GAME_VERSION_3) + { + eepromSize = utilReadInt(gzFile); + utilGzRead(gzFile, eepromData, 0x2000); + } + else + { + // prior to 0.7.1, only 4K EEPROM was supported + eepromSize = 512; + } +} + +int eepromRead(u32 /* address */) +{ + switch (eepromMode) + { + case EEPROM_IDLE: + case EEPROM_READADDRESS: + case EEPROM_WRITEDATA: + return 1; + case EEPROM_READDATA: + { + eepromBits++; + if (eepromBits == 4) + { + eepromMode = EEPROM_READDATA2; + eepromBits = 0; + eepromByte = 0; + } + return 0; + } + case EEPROM_READDATA2: + { + int data = 0; + int address = eepromAddress << 3; + int mask = 1 << (7 - (eepromBits & 7)); + data = (eepromData[address+eepromByte] & mask) ? 1 : 0; + eepromBits++; + if ((eepromBits & 7) == 0) + eepromByte++; + if (eepromBits == 0x40) + eepromMode = EEPROM_IDLE; + return data; + } + default: + return 0; + } + return 1; +} + +void eepromWrite(u32 /* address */, u8 value) +{ + if (cpuDmaCount == 0) + return; + int bit = value & 1; + switch (eepromMode) + { + case EEPROM_IDLE: + eepromByte = 0; + eepromBits = 1; + eepromBuffer[eepromByte] = bit; + eepromMode = EEPROM_READADDRESS; + break; + case EEPROM_READADDRESS: + eepromBuffer[eepromByte] <<= 1; + eepromBuffer[eepromByte] |= bit; + eepromBits++; + if ((eepromBits & 7) == 0) + { + eepromByte++; + } + if (cpuDmaCount == 0x11 || cpuDmaCount == 0x51) + { + if (eepromBits == 0x11) + { + eepromInUse = true; + eepromSize = 0x2000; + eepromAddress = ((eepromBuffer[0] & 0x3F) << 8) | + ((eepromBuffer[1] & 0xFF)); + if (!(eepromBuffer[0] & 0x40)) + { + eepromBuffer[0] = bit; + eepromBits = 1; + eepromByte = 0; + eepromMode = EEPROM_WRITEDATA; + } + else + { + eepromMode = EEPROM_READDATA; + eepromByte = 0; + eepromBits = 0; + } + } + } + else + { + if (eepromBits == 9) + { + eepromInUse = true; + eepromAddress = (eepromBuffer[0] & 0x3F); + if (!(eepromBuffer[0] & 0x40)) + { + eepromBuffer[0] = bit; + eepromBits = 1; + eepromByte = 0; + eepromMode = EEPROM_WRITEDATA; + } + else + { + eepromMode = EEPROM_READDATA; + eepromByte = 0; + eepromBits = 0; + } + } + } + break; + case EEPROM_READDATA: + case EEPROM_READDATA2: + // should we reset here? + eepromMode = EEPROM_IDLE; + break; + case EEPROM_WRITEDATA: + eepromBuffer[eepromByte] <<= 1; + eepromBuffer[eepromByte] |= bit; + eepromBits++; + if ((eepromBits & 7) == 0) + { + eepromByte++; + } + if (eepromBits == 0x40) + { + eepromInUse = true; + // write data; + for (int i = 0; i < 8; i++) + { + eepromData[(eepromAddress << 3) + i] = eepromBuffer[i]; + } + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + } + else if (eepromBits == 0x41) + { + eepromMode = EEPROM_IDLE; + eepromByte = 0; + eepromBits = 0; + } + break; + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/EEprom.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/EEprom.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,27 @@ +#ifndef VBA_EEPROM_H +#define VBA_EEPROM_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +extern void eepromSaveGame(gzFile gzFile); +extern void eepromReadGame(gzFile gzFile, int version); +extern int eepromRead(u32 address); +extern void eepromWrite(u32 address, u8 value); +extern void eepromReset(); +extern void eepromErase(); +extern u8 eepromData[0x2000]; +extern bool8 eepromInUse; +extern int32 eepromSize; + +#define EEPROM_IDLE 0 +#define EEPROM_READADDRESS 1 +#define EEPROM_READDATA 2 +#define EEPROM_READDATA2 3 +#define EEPROM_WRITEDATA 4 + +#endif // VBA_EEPROM_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Flash.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Flash.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,285 @@ +#include +#include + +#include "Flash.h" +#include "GBA.h" +#include "GBAGlobals.h" +#include "Sram.h" +#include "../common/System.h" +#include "../common/Util.h" + +#define FLASH_READ_ARRAY 0 +#define FLASH_CMD_1 1 +#define FLASH_CMD_2 2 +#define FLASH_AUTOSELECT 3 +#define FLASH_CMD_3 4 +#define FLASH_CMD_4 5 +#define FLASH_CMD_5 6 +#define FLASH_ERASE_COMPLETE 7 +#define FLASH_PROGRAM 8 +#define FLASH_SETBANK 9 + +u8 flashSaveMemory[0x20000 + 4]; +int32 flashState = FLASH_READ_ARRAY; +int32 flashReadState = FLASH_READ_ARRAY; +int32 flashSize = 0x10000; +int32 flashDeviceID = 0x1b; +int32 flashManufacturerID = 0x32; +int32 flashBank = 0; + +static variable_desc flashSaveData[] = { + { &flashState, sizeof(int32) }, + { &flashReadState, sizeof(int32) }, + { &flashSaveMemory[0], 0x10000 }, + { NULL, 0 } +}; + +static variable_desc flashSaveData2[] = { + { &flashState, sizeof(int32) }, + { &flashReadState, sizeof(int32) }, + { &flashSize, sizeof(int32) }, + { &flashSaveMemory[0], 0x20000 }, + { NULL, 0 } +}; + +static variable_desc flashSaveData3[] = { + { &flashState, sizeof(int32) }, + { &flashReadState, sizeof(int32) }, + { &flashSize, sizeof(int32) }, + { &flashBank, sizeof(int32) }, + { &flashSaveMemory[0], 0x20000 }, + { NULL, 0 } +}; + +void flashReset() +{ + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + flashBank = 0; +} + +void flashErase() +{ + memset(flashSaveMemory, 0, 0x20000*sizeof(u8)); + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + flashSize = 0x10000; + flashDeviceID = 0x1b; + flashManufacturerID = 0x32; + flashBank = 0; +} + +void flashSaveGame(gzFile gzFile) +{ + utilWriteData(gzFile, flashSaveData3); +} + +void flashReadGame(gzFile gzFile, int version) +{ + if (version < SAVE_GAME_VERSION_5) + utilReadData(gzFile, flashSaveData); + else if (version < SAVE_GAME_VERSION_7) + { + utilReadData(gzFile, flashSaveData2); + flashBank = 0; + flashSetSize(flashSize); + } + else + { + utilReadData(gzFile, flashSaveData3); + } +} + +void flashSetSize(int size) +{ + // log("Setting flash size to %d\n", size); + flashSize = size; + if (size == 0x10000) + { + flashDeviceID = 0x1b; + flashManufacturerID = 0x32; + } + else + { + flashDeviceID = 0x13; //0x09; + flashManufacturerID = 0x62; //0xc2; + } +} + +u8 flashRead(u32 address) +{ + // log("Reading %08x from %08x\n", address, reg[15].I); + // log("Current read state is %d\n", flashReadState); + address &= 0xFFFF; + + switch (flashReadState) + { + case FLASH_READ_ARRAY: + return flashSaveMemory[(flashBank << 16) + address]; + case FLASH_AUTOSELECT: + switch (address & 0xFF) + { + case 0: + // manufacturer ID + return u8(flashManufacturerID); + case 1: + // device ID + return u8(flashDeviceID); + } + break; + case FLASH_ERASE_COMPLETE: + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + return 0xFF; + } + ; + return 0; +} + +void flashSaveDecide(u32 address, u8 byte) +{ + // log("Deciding save type %08x\n", address); + if (address == 0x0e005555) + { + saveType = 2; + cpuSaveGameFunc = flashWrite; + } + else + { + saveType = 1; + cpuSaveGameFunc = sramWrite; + } + + (*cpuSaveGameFunc)(address, byte); +} + +void flashWrite(u32 address, u8 byte) +{ + // log("Writing %02x at %08x\n", byte, address); + // log("Current state is %d\n", flashState); + address &= 0xFFFF; + switch (flashState) + { + case FLASH_READ_ARRAY: + if (address == 0x5555 && byte == 0xAA) + flashState = FLASH_CMD_1; + break; + case FLASH_CMD_1: + if (address == 0x2AAA && byte == 0x55) + flashState = FLASH_CMD_2; + else + flashState = FLASH_READ_ARRAY; + break; + case FLASH_CMD_2: + if (address == 0x5555) + { + if (byte == 0x90) + { + flashState = FLASH_AUTOSELECT; + flashReadState = FLASH_AUTOSELECT; + } + else if (byte == 0x80) + { + flashState = FLASH_CMD_3; + } + else if (byte == 0xF0) + { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + else if (byte == 0xA0) + { + flashState = FLASH_PROGRAM; + } + else if (byte == 0xB0 && flashSize == 0x20000) + { + flashState = FLASH_SETBANK; + } + else + { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + } + else + { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_CMD_3: + if (address == 0x5555 && byte == 0xAA) + { + flashState = FLASH_CMD_4; + } + else + { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_CMD_4: + if (address == 0x2AAA && byte == 0x55) + { + flashState = FLASH_CMD_5; + } + else + { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_CMD_5: + if (byte == 0x30) + { + // SECTOR ERASE + memset(&flashSaveMemory[(flashBank << 16) + (address & 0xF000)], + 0, + 0x1000); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + flashReadState = FLASH_ERASE_COMPLETE; + } + else if (byte == 0x10) + { + // CHIP ERASE + memset(flashSaveMemory, 0, flashSize); + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + flashReadState = FLASH_ERASE_COMPLETE; + } + else + { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_AUTOSELECT: + if (byte == 0xF0) + { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + else if (address == 0x5555 && byte == 0xAA) + flashState = FLASH_CMD_1; + else + { + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + } + break; + case FLASH_PROGRAM: + flashSaveMemory[(flashBank<<16)+address] = byte; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + break; + case FLASH_SETBANK: + if (address == 0) + { + flashBank = (byte & 1); + } + flashState = FLASH_READ_ARRAY; + flashReadState = FLASH_READ_ARRAY; + break; + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Flash.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Flash.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,23 @@ +#ifndef VBA_FLASH_H +#define VBA_FLASH_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +extern void flashSaveGame(gzFile gzFile); +extern void flashReadGame(gzFile gzFile, int version); +extern u8 flashRead(u32 address); +extern void flashWrite(u32 address, u8 byte); +extern u8 flashSaveMemory[0x20000 + 4]; +extern void flashSaveDecide(u32 address, u8 byte); +extern void flashReset(); +extern void flashErase(); +extern void flashSetSize(int size); + +extern int32 flashSize; + +#endif // VBA_FLASH_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBA.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBA.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,4567 @@ +#include +#include +#include +#include + +#include "../Port.h" +#include "../NLS.h" +#include "GBA.h" +//#include "GBAGlobals.h" +#include "GBACheats.h" // FIXME: SDL requires this included before "GBAinline.h" +#include "GBAinline.h" +#include "GBAGfx.h" +#include "GBASound.h" +#include "EEprom.h" +#include "Flash.h" +#include "Sram.h" +#include "bios.h" +#include "elf.h" +#include "agbprint.h" +#include "../common/unzip.h" +#include "../common/Util.h" +#include "../common/movie.h" +#include "../common/vbalua.h" + +#ifdef PROFILING +#include "../prof/prof.h" +#endif + +#define UPDATE_REG(address, value) WRITE16LE(((u16 *)&ioMem[address]), value) + +#ifdef __GNUC__ +#define _stricmp strcasecmp +#endif + +#define CPU_BREAK_LOOP \ + cpuSavedTicks = cpuSavedTicks - *extCpuLoopTicks; \ + *extCpuLoopTicks = *extClockTicks; + +#define CPU_BREAK_LOOP_2 \ + cpuSavedTicks = cpuSavedTicks - *extCpuLoopTicks; \ + *extCpuLoopTicks = *extClockTicks; \ + *extTicks = *extClockTicks; + +int32 cpuDmaTicksToUpdate = 0; +int32 cpuDmaCount = 0; +bool8 cpuDmaHack = 0; +u32 cpuDmaLast = 0; +int32 dummyAddress = 0; + +int32 *extCpuLoopTicks = NULL; +int32 *extClockTicks = NULL; +int32 *extTicks = NULL; + +#if (defined(WIN32) && !defined(SDL)) +HANDLE mapROM; // shared memory handles +HANDLE mapWORKRAM; +HANDLE mapBIOS; +HANDLE mapIRAM; +HANDLE mapPALETTERAM; +HANDLE mapVRAM; +HANDLE mapOAM; +HANDLE mapPIX; +HANDLE mapIOMEM; +#endif + +int32 gbaSaveType = 0; // used to remember the save type on reset +bool8 intState = false; +bool8 stopState = false; +bool8 holdState = false; +int32 holdType = 0; +bool8 cpuSramEnabled = true; +bool8 cpuFlashEnabled = true; +bool8 cpuEEPROMEnabled = true; +bool8 cpuEEPROMSensorEnabled = false; + +#ifdef PROFILING +int profilingTicks = 0; +int profilingTicksReload = 0; +static char *profilBuffer = NULL; +static int profilSize = 0; +static u32 profilLowPC = 0; +static int profilScale = 0; +#endif +bool8 freezeWorkRAM[0x40000]; +bool8 freezeInternalRAM[0x8000]; +int32 lcdTicks = 960; +bool8 timer0On = false; +int32 timer0Ticks = 0; +int32 timer0Reload = 0; +int32 timer0ClockReload = 0; +bool8 timer1On = false; +int32 timer1Ticks = 0; +int32 timer1Reload = 0; +int32 timer1ClockReload = 0; +bool8 timer2On = false; +int32 timer2Ticks = 0; +int32 timer2Reload = 0; +int32 timer2ClockReload = 0; +bool8 timer3On = false; +int32 timer3Ticks = 0; +int32 timer3Reload = 0; +int32 timer3ClockReload = 0; +u32 dma0Source = 0; +u32 dma0Dest = 0; +u32 dma1Source = 0; +u32 dma1Dest = 0; +u32 dma2Source = 0; +u32 dma2Dest = 0; +u32 dma3Source = 0; +u32 dma3Dest = 0; +void (*cpuSaveGameFunc)(u32, u8) = flashSaveDecide; +void (*renderLine)() = mode0RenderLine; +bool8 fxOn = false; +bool8 windowOn = false; +int32 frameSkipCount = 0; +u32 gbaLastTime = 0; +int32 gbaFrameCount = 0; +bool8 prefetchActive = false, prefetchPrevActive = false, prefetchApplies = false; +char buffer[1024]; +FILE *out = NULL; + +static bool newFrame = true; +static bool pauseAfterFrameAdvance = false; + +const int32 TIMER_TICKS[4] = { + 1, + 64, + 256, + 1024 +}; + +const int32 thumbCycles[] = { +// 0 1 2 3 4 5 6 7 8 9 a b c d e f + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 1 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 2 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 3 + 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 4 + 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 5 + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 6 + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 7 + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 8 + 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, // 9 + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // a + 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 4, 1, 1, // b + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, // d + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // e + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 // f +}; + +const int32 gamepakRamWaitState[4] = { 4, 3, 2, 8 }; +const int32 gamepakWaitState[8] = { 4, 3, 2, 8, 4, 3, 2, 8 }; +const int32 gamepakWaitState0[8] = { 2, 2, 2, 2, 1, 1, 1, 1 }; +const int32 gamepakWaitState1[8] = { 4, 4, 4, 4, 1, 1, 1, 1 }; +const int32 gamepakWaitState2[8] = { 8, 8, 8, 8, 1, 1, 1, 1 }; + +int32 memoryWait[16] = +{ 0, 0, 2, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 0 }; +int32 memoryWait32[16] = +{ 0, 0, 9, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 0 }; +int32 memoryWaitSeq[16] = +{ 0, 0, 2, 0, 0, 0, 0, 0, 2, 2, 4, 4, 8, 8, 4, 0 }; +int32 memoryWaitSeq32[16] = +{ 2, 0, 3, 0, 0, 2, 2, 0, 4, 4, 8, 8, 16, 16, 8, 0 }; +int32 memoryWaitFetch[16] = +{ 3, 0, 3, 0, 0, 1, 1, 0, 4, 4, 4, 4, 4, 4, 4, 0 }; +int32 memoryWaitFetch32[16] = +{ 6, 0, 6, 0, 0, 2, 2, 0, 8, 8, 8, 8, 8, 8, 8, 0 }; + +const int32 cpuMemoryWait[16] = { + 0, 0, 2, 0, 0, 0, 0, 0, + 2, 2, 2, 2, 2, 2, 0, 0 +}; +const int32 cpuMemoryWait32[16] = { + 0, 0, 3, 0, 0, 0, 0, 0, + 3, 3, 3, 3, 3, 3, 0, 0 +}; + +const bool8 memory32[16] = { + true, false, false, true, true, false, false, true, false, false, false, false, false, false, true, false +}; + +u8 biosProtected[4]; + +u8 cpuBitsSet[256]; +u8 cpuLowestBitSet[256]; + +#ifdef WORDS_BIGENDIAN +bool8 cpuBiosSwapped = false; +#endif + +u32 myROM[] = { + 0xEA000006, + 0xEA000093, + 0xEA000006, + 0x00000000, + 0x00000000, + 0x00000000, + 0xEA000088, + 0x00000000, + 0xE3A00302, + 0xE1A0F000, + 0xE92D5800, + 0xE55EC002, + 0xE28FB03C, + 0xE79BC10C, + 0xE14FB000, + 0xE92D0800, + 0xE20BB080, + 0xE38BB01F, + 0xE129F00B, + 0xE92D4004, + 0xE1A0E00F, + 0xE12FFF1C, + 0xE8BD4004, + 0xE3A0C0D3, + 0xE129F00C, + 0xE8BD0800, + 0xE169F00B, + 0xE8BD5800, + 0xE1B0F00E, + 0x0000009C, + 0x0000009C, + 0x0000009C, + 0x0000009C, + 0x000001F8, + 0x000001F0, + 0x000000AC, + 0x000000A0, + 0x000000FC, + 0x00000168, + 0xE12FFF1E, + 0xE1A03000, + 0xE1A00001, + 0xE1A01003, + 0xE2113102, + 0x42611000, + 0xE033C040, + 0x22600000, + 0xE1B02001, + 0xE15200A0, + 0x91A02082, + 0x3AFFFFFC, + 0xE1500002, + 0xE0A33003, + 0x20400002, + 0xE1320001, + 0x11A020A2, + 0x1AFFFFF9, + 0xE1A01000, + 0xE1A00003, + 0xE1B0C08C, + 0x22600000, + 0x42611000, + 0xE12FFF1E, + 0xE92D0010, + 0xE1A0C000, + 0xE3A01001, + 0xE1500001, + 0x81A000A0, + 0x81A01081, + 0x8AFFFFFB, + 0xE1A0000C, + 0xE1A04001, + 0xE3A03000, + 0xE1A02001, + 0xE15200A0, + 0x91A02082, + 0x3AFFFFFC, + 0xE1500002, + 0xE0A33003, + 0x20400002, + 0xE1320001, + 0x11A020A2, + 0x1AFFFFF9, + 0xE0811003, + 0xE1B010A1, + 0xE1510004, + 0x3AFFFFEE, + 0xE1A00004, + 0xE8BD0010, + 0xE12FFF1E, + 0xE0010090, + 0xE1A01741, + 0xE2611000, + 0xE3A030A9, + 0xE0030391, + 0xE1A03743, + 0xE2833E39, + 0xE0030391, + 0xE1A03743, + 0xE2833C09, + 0xE283301C, + 0xE0030391, + 0xE1A03743, + 0xE2833C0F, + 0xE28330B6, + 0xE0030391, + 0xE1A03743, + 0xE2833C16, + 0xE28330AA, + 0xE0030391, + 0xE1A03743, + 0xE2833A02, + 0xE2833081, + 0xE0030391, + 0xE1A03743, + 0xE2833C36, + 0xE2833051, + 0xE0030391, + 0xE1A03743, + 0xE2833CA2, + 0xE28330F9, + 0xE0000093, + 0xE1A00840, + 0xE12FFF1E, + 0xE3A00001, + 0xE3A01001, + 0xE92D4010, + 0xE3A0C301, + 0xE3A03000, + 0xE3A04001, + 0xE3500000, + 0x1B000004, + 0xE5CC3301, + 0xEB000002, + 0x0AFFFFFC, + 0xE8BD4010, + 0xE12FFF1E, + 0xE5CC3208, + 0xE15C20B8, + 0xE0110002, + 0x10200002, + 0x114C00B8, + 0xE5CC4208, + 0xE12FFF1E, + 0xE92D500F, + 0xE3A00301, + 0xE1A0E00F, + 0xE510F004, + 0xE8BD500F, + 0xE25EF004, + 0xE59FD044, + 0xE92D5000, + 0xE14FC000, + 0xE10FE000, + 0xE92D5000, + 0xE3A0C302, + 0xE5DCE09C, + 0xE35E00A5, + 0x1A000004, + 0x05DCE0B4, + 0x021EE080, + 0xE28FE004, + 0x159FF018, + 0x059FF018, + 0xE59FD018, + 0xE8BD5000, + 0xE169F00C, + 0xE8BD5000, + 0xE25EF004, + 0x03007FF0, + 0x09FE2000, + 0x09FFC000, + 0x03007FE0 +}; + +variable_desc saveGameStruct[] = { + { &DISPCNT, sizeof(u16) }, + { &DISPSTAT, sizeof(u16) }, + { &VCOUNT, sizeof(u16) }, + { &BG0CNT, sizeof(u16) }, + { &BG1CNT, sizeof(u16) }, + { &BG2CNT, sizeof(u16) }, + { &BG3CNT, sizeof(u16) }, + { &BG0HOFS, sizeof(u16) }, + { &BG0VOFS, sizeof(u16) }, + { &BG1HOFS, sizeof(u16) }, + { &BG1VOFS, sizeof(u16) }, + { &BG2HOFS, sizeof(u16) }, + { &BG2VOFS, sizeof(u16) }, + { &BG3HOFS, sizeof(u16) }, + { &BG3VOFS, sizeof(u16) }, + { &BG2PA, sizeof(u16) }, + { &BG2PB, sizeof(u16) }, + { &BG2PC, sizeof(u16) }, + { &BG2PD, sizeof(u16) }, + { &BG2X_L, sizeof(u16) }, + { &BG2X_H, sizeof(u16) }, + { &BG2Y_L, sizeof(u16) }, + { &BG2Y_H, sizeof(u16) }, + { &BG3PA, sizeof(u16) }, + { &BG3PB, sizeof(u16) }, + { &BG3PC, sizeof(u16) }, + { &BG3PD, sizeof(u16) }, + { &BG3X_L, sizeof(u16) }, + { &BG3X_H, sizeof(u16) }, + { &BG3Y_L, sizeof(u16) }, + { &BG3Y_H, sizeof(u16) }, + { &WIN0H, sizeof(u16) }, + { &WIN1H, sizeof(u16) }, + { &WIN0V, sizeof(u16) }, + { &WIN1V, sizeof(u16) }, + { &WININ, sizeof(u16) }, + { &WINOUT, sizeof(u16) }, + { &MOSAIC, sizeof(u16) }, + { &BLDMOD, sizeof(u16) }, + { &COLEV, sizeof(u16) }, + { &COLY, sizeof(u16) }, + { &DM0SAD_L, sizeof(u16) }, + { &DM0SAD_H, sizeof(u16) }, + { &DM0DAD_L, sizeof(u16) }, + { &DM0DAD_H, sizeof(u16) }, + { &DM0CNT_L, sizeof(u16) }, + { &DM0CNT_H, sizeof(u16) }, + { &DM1SAD_L, sizeof(u16) }, + { &DM1SAD_H, sizeof(u16) }, + { &DM1DAD_L, sizeof(u16) }, + { &DM1DAD_H, sizeof(u16) }, + { &DM1CNT_L, sizeof(u16) }, + { &DM1CNT_H, sizeof(u16) }, + { &DM2SAD_L, sizeof(u16) }, + { &DM2SAD_H, sizeof(u16) }, + { &DM2DAD_L, sizeof(u16) }, + { &DM2DAD_H, sizeof(u16) }, + { &DM2CNT_L, sizeof(u16) }, + { &DM2CNT_H, sizeof(u16) }, + { &DM3SAD_L, sizeof(u16) }, + { &DM3SAD_H, sizeof(u16) }, + { &DM3DAD_L, sizeof(u16) }, + { &DM3DAD_H, sizeof(u16) }, + { &DM3CNT_L, sizeof(u16) }, + { &DM3CNT_H, sizeof(u16) }, + { &TM0D, sizeof(u16) }, + { &TM0CNT, sizeof(u16) }, + { &TM1D, sizeof(u16) }, + { &TM1CNT, sizeof(u16) }, + { &TM2D, sizeof(u16) }, + { &TM2CNT, sizeof(u16) }, + { &TM3D, sizeof(u16) }, + { &TM3CNT, sizeof(u16) }, + { &P1, sizeof(u16) }, + { &IE, sizeof(u16) }, + { &IF, sizeof(u16) }, + { &IME, sizeof(u16) }, + { &holdState, sizeof(bool8) }, + { &holdType, sizeof(int32) }, + { &lcdTicks, sizeof(int32) }, + { &timer0On, sizeof(bool8) }, + { &timer0Ticks, sizeof(int32) }, + { &timer0Reload, sizeof(int32) }, + { &timer0ClockReload, sizeof(int32) }, + { &timer1On, sizeof(bool8) }, + { &timer1Ticks, sizeof(int32) }, + { &timer1Reload, sizeof(int32) }, + { &timer1ClockReload, sizeof(int32) }, + { &timer2On, sizeof(bool8) }, + { &timer2Ticks, sizeof(int32) }, + { &timer2Reload, sizeof(int32) }, + { &timer2ClockReload, sizeof(int32) }, + { &timer3On, sizeof(bool8) }, + { &timer3Ticks, sizeof(int32) }, + { &timer3Reload, sizeof(int32) }, + { &timer3ClockReload, sizeof(int32) }, + { &dma0Source, sizeof(u32) }, + { &dma0Dest, sizeof(u32) }, + { &dma1Source, sizeof(u32) }, + { &dma1Dest, sizeof(u32) }, + { &dma2Source, sizeof(u32) }, + { &dma2Dest, sizeof(u32) }, + { &dma3Source, sizeof(u32) }, + { &dma3Dest, sizeof(u32) }, + { &fxOn, sizeof(bool8) }, + { &windowOn, sizeof(bool8) }, + { &N_FLAG, sizeof(bool8) }, + { &C_FLAG, sizeof(bool8) }, + { &Z_FLAG, sizeof(bool8) }, + { &V_FLAG, sizeof(bool8) }, + { &armState, sizeof(bool8) }, + { &armIrqEnable, sizeof(bool8) }, + { &armNextPC, sizeof(u32) }, + { &armMode, sizeof(int32) }, + { &saveType, sizeof(int32) }, + { NULL, 0 } +}; + +//int cpuLoopTicks = 0; +int cpuSavedTicks = 0; + +#ifdef PROFILING +void cpuProfil(char *buf, int size, u32 lowPC, int scale) +{ + profilBuffer = buf; + profilSize = size; + profilLowPC = lowPC; + profilScale = scale; +} + +void cpuEnableProfiling(int hz) +{ + if (hz == 0) + hz = 100; + profilingTicks = profilingTicksReload = 16777216 / hz; + profSetHertz(hz); +} + +#endif + +inline int CPUUpdateTicksAccess32(u32 address) +{ + return memoryWait32[(address >> 24) & 15]; +} + +inline int CPUUpdateTicksAccess16(u32 address) +{ + return memoryWait[(address >> 24) & 15]; +} + +inline int CPUUpdateTicksAccessSeq32(u32 address) +{ + return memoryWaitSeq32[(address >> 24) & 15]; +} + +inline int CPUUpdateTicksAccessSeq16(u32 address) +{ + return memoryWaitSeq[(address >> 24) & 15]; +} + +inline int CPUUpdateTicks() +{ + int cpuLoopTicks = lcdTicks; + + if (soundTicks < cpuLoopTicks) + cpuLoopTicks = soundTicks; + + if (timer0On && !(TM0CNT & 4) && (timer0Ticks < cpuLoopTicks)) + { + cpuLoopTicks = timer0Ticks; + } + if (timer1On && !(TM1CNT & 4) && (timer1Ticks < cpuLoopTicks)) + { + cpuLoopTicks = timer1Ticks; + } + if (timer2On && !(TM2CNT & 4) && (timer2Ticks < cpuLoopTicks)) + { + cpuLoopTicks = timer2Ticks; + } + if (timer3On && !(TM3CNT & 4) && (timer3Ticks < cpuLoopTicks)) + { + cpuLoopTicks = timer3Ticks; + } +#ifdef PROFILING + if (profilingTicksReload != 0) + { + if (profilingTicks < cpuLoopTicks) + { + cpuLoopTicks = profilingTicks; + } + } +#endif + cpuSavedTicks = cpuLoopTicks; + return cpuLoopTicks; +} + +void CPUUpdateWindow0() +{ + int x00 = WIN0H >> 8; + int x01 = WIN0H & 255; + + if (x00 <= x01) + { + for (int i = 0; i < 240; i++) + { + gfxInWin0[i] = (i >= x00 && i < x01); + } + } + else + { + for (int i = 0; i < 240; i++) + { + gfxInWin0[i] = (i >= x00 || i < x01); + } + } +} + +void CPUUpdateWindow1() +{ + int x00 = WIN1H >> 8; + int x01 = WIN1H & 255; + + if (x00 <= x01) + { + for (int i = 0; i < 240; i++) + { + gfxInWin1[i] = (i >= x00 && i < x01); + } + } + else + { + for (int i = 0; i < 240; i++) + { + gfxInWin1[i] = (i >= x00 || i < x01); + } + } +} + +#define CLEAR_ARRAY(a) \ + { \ + u32 *array = (a); \ + for (int i = 0; i < 240; i++) { \ + *array++ = 0x80000000; \ + } \ + } \ + +void CPUUpdateRenderBuffers(bool force) +{ + if (!(layerEnable & 0x0100) || force) + { + CLEAR_ARRAY(line0); + } + if (!(layerEnable & 0x0200) || force) + { + CLEAR_ARRAY(line1); + } + if (!(layerEnable & 0x0400) || force) + { + CLEAR_ARRAY(line2); + } + if (!(layerEnable & 0x0800) || force) + { + CLEAR_ARRAY(line3); + } +} + +bool CPUWriteStateToStream(gzFile gzFile) +{ + utilWriteInt(gzFile, SAVE_GAME_VERSION); + + utilGzWrite(gzFile, &rom[0xa0], 16); + + utilWriteInt(gzFile, useBios); + + utilGzWrite(gzFile, ®[0], sizeof(reg)); + + utilWriteData(gzFile, saveGameStruct); + + // new to version 0.7.1 + utilWriteInt(gzFile, stopState); + // new to version 0.8 + utilWriteInt(gzFile, intState); + + utilGzWrite(gzFile, internalRAM, 0x8000); + utilGzWrite(gzFile, paletteRAM, 0x400); + utilGzWrite(gzFile, workRAM, 0x40000); + utilGzWrite(gzFile, vram, 0x20000); + utilGzWrite(gzFile, oam, 0x400); + utilGzWrite(gzFile, pix, 4 * 241 * 162); + utilGzWrite(gzFile, ioMem, 0x400); + + eepromSaveGame(gzFile); + flashSaveGame(gzFile); + soundSaveGame(gzFile); + + cheatsSaveGame(gzFile); + + // version 1.5 + rtcSaveGame(gzFile); + + // SAVE_GAME_VERSION_9 (new to re-recording version which is based on 1.72) + { + extern int32 sensorX, sensorY; // from SDL.cpp + utilGzWrite(gzFile, &sensorX, sizeof(sensorX)); + utilGzWrite(gzFile, &sensorY, sizeof(sensorY)); + bool8 movieActive = VBAMovieActive(); + utilGzWrite(gzFile, &movieActive, sizeof(movieActive)); + if (movieActive) + { + uint8 *movie_freeze_buf = NULL; + uint32 movie_freeze_size = 0; + + VBAMovieFreeze(&movie_freeze_buf, &movie_freeze_size); + if (movie_freeze_buf) + { + utilGzWrite(gzFile, &movie_freeze_size, sizeof(movie_freeze_size)); + utilGzWrite(gzFile, movie_freeze_buf, movie_freeze_size); + delete [] movie_freeze_buf; + } + else + { + systemMessage(0, N_("Failed to save movie snapshot.")); + return false; + } + } + utilGzWrite(gzFile, &GBASystemCounters.frameCount, sizeof(GBASystemCounters.frameCount)); + } + + // SAVE_GAME_VERSION_10 + { + utilGzWrite(gzFile, memoryWait, 16 * sizeof(int32)); + utilGzWrite(gzFile, memoryWait32, 16 * sizeof(int32)); + utilGzWrite(gzFile, memoryWaitSeq, 16 * sizeof(int32)); + utilGzWrite(gzFile, memoryWaitSeq32, 16 * sizeof(int32)); + utilGzWrite(gzFile, memoryWaitFetch, 16 * sizeof(int32)); + utilGzWrite(gzFile, memoryWaitFetch32, 16 * sizeof(int32)); + } + + // SAVE_GAME_VERSION_11 + { + utilGzWrite(gzFile, &prefetchActive, sizeof(bool8)); + utilGzWrite(gzFile, &prefetchPrevActive, sizeof(bool8)); + utilGzWrite(gzFile, &prefetchApplies, sizeof(bool8)); + } + + // SAVE_GAME_VERSION_12 + { + utilGzWrite(gzFile, &memLagTempEnabled, sizeof(bool8)); // necessary + utilGzWrite(gzFile, &speedHack, sizeof(bool8)); // just in case it's ever used... + } + + // SAVE_GAME_VERSION_13 + { + utilGzWrite(gzFile, &GBASystemCounters.lagCount, sizeof(GBASystemCounters.lagCount)); + utilGzWrite(gzFile, &GBASystemCounters.lagged, sizeof(GBASystemCounters.lagged)); + utilGzWrite(gzFile, &GBASystemCounters.laggedLast, sizeof(GBASystemCounters.laggedLast)); + } + + return true; +} + +bool CPUWriteState(const char *file) +{ + gzFile gzFile = utilGzOpen(file, "wb"); + + if (gzFile == NULL) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), file); + return false; + } + + bool res = CPUWriteStateToStream(gzFile); + + utilGzClose(gzFile); + + return res; +} + +bool CPUWriteMemState(char *memory, int available) +{ + gzFile gzFile = utilMemGzOpen(memory, available, "w"); + + if (gzFile == NULL) + { + return false; + } + + bool res = CPUWriteStateToStream(gzFile); + + long pos = utilGzTell(gzFile) + 8; + + if (pos >= (available)) + res = false; + + utilGzClose(gzFile); + + return res; +} + +static int tempStateID = 0; +static int tempFailCount = 0; +static bool backupSafe = true; + +bool CPUReadStateFromStream(gzFile gzFile) +{ + bool8 ub; + char tempBackupName [128]; + if (backupSafe) + { + sprintf(tempBackupName, "gbatempsave%d.sav", tempStateID++); + CPUWriteState(tempBackupName); + } + + int version = utilReadInt(gzFile); + + if (version > SAVE_GAME_VERSION || version < SAVE_GAME_VERSION_1) + { + systemMessage(MSG_UNSUPPORTED_VBA_SGM, + N_("Unsupported VisualBoyAdvance save game version %d"), + version); + goto failedLoad; + } + + u8 romname[17]; + + utilGzRead(gzFile, romname, 16); + + if (memcmp(&rom[0xa0], romname, 16) != 0) + { + romname[16] = 0; + for (int i = 0; i < 16; i++) + if (romname[i] < 32) + romname[i] = 32; + systemMessage(MSG_CANNOT_LOAD_SGM, N_("Cannot load save game for %s"), romname); + goto failedLoad; + } + + ub = utilReadInt(gzFile) ? true : false; + + if (ub != useBios) + { + if (useBios) + systemMessage(MSG_SAVE_GAME_NOT_USING_BIOS, + N_("Save game is not using the BIOS files")); + else + systemMessage(MSG_SAVE_GAME_USING_BIOS, + N_("Save game is using the BIOS file")); + goto failedLoad; + } + + utilGzRead(gzFile, ®[0], sizeof(reg)); + + utilReadData(gzFile, saveGameStruct); + + if (version < SAVE_GAME_VERSION_3) + stopState = false; + else + stopState = utilReadInt(gzFile) ? true : false; + + if (version < SAVE_GAME_VERSION_4) + intState = false; + else + intState = utilReadInt(gzFile) ? true : false; + + utilGzRead(gzFile, internalRAM, 0x8000); + utilGzRead(gzFile, paletteRAM, 0x400); + utilGzRead(gzFile, workRAM, 0x40000); + utilGzRead(gzFile, vram, 0x20000); + utilGzRead(gzFile, oam, 0x400); + if (version < SAVE_GAME_VERSION_6) + utilGzRead(gzFile, pix, 4 * 240 * 160); + else + utilGzRead(gzFile, pix, 4 * 241 * 162); + utilGzRead(gzFile, ioMem, 0x400); + + eepromReadGame(gzFile, version); + flashReadGame(gzFile, version); + soundReadGame(gzFile, version); + + if (version > SAVE_GAME_VERSION_1) + { + cheatsReadGame(gzFile); + } + if (version > SAVE_GAME_VERSION_6) + { + rtcReadGame(gzFile); + } + + if (version <= SAVE_GAME_VERSION_7) + { + u32 temp; +#define SWAP(a, b, c) \ + temp = (a); \ + (a) = (b) << 16 | (c); \ + (b) = (temp) >> 16; \ + (c) = (temp) & 0xFFFF; + + SWAP(dma0Source, DM0SAD_H, DM0SAD_L); + SWAP(dma0Dest, DM0DAD_H, DM0DAD_L); + SWAP(dma1Source, DM1SAD_H, DM1SAD_L); + SWAP(dma1Dest, DM1DAD_H, DM1DAD_L); + SWAP(dma2Source, DM2SAD_H, DM2SAD_L); + SWAP(dma2Dest, DM2DAD_H, DM2DAD_L); + SWAP(dma3Source, DM3SAD_H, DM3SAD_L); + SWAP(dma3Dest, DM3DAD_H, DM3DAD_L); + } + + // set pointers! + layerEnable = layerSettings & DISPCNT; + + CPUUpdateRender(); + CPUUpdateRenderBuffers(true); + CPUUpdateWindow0(); + CPUUpdateWindow1(); + gbaSaveType = 0; + switch (saveType) + { + case 0: + cpuSaveGameFunc = flashSaveDecide; + break; + case 1: + cpuSaveGameFunc = sramWrite; + gbaSaveType = 1; + break; + case 2: + cpuSaveGameFunc = flashWrite; + gbaSaveType = 2; + break; + default: + systemMessage(MSG_UNSUPPORTED_SAVE_TYPE, + N_("Unsupported save type %d"), saveType); + break; + } + if (eepromInUse) + gbaSaveType = 3; + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + if (version >= SAVE_GAME_VERSION_9) // new to re-recording version: + { + extern int32 sensorX, sensorY; // from SDL.cpp + utilGzRead(gzFile, &sensorX, sizeof(sensorX)); + utilGzRead(gzFile, &sensorY, sizeof(sensorY)); + + bool8 movieSnapshot; + utilGzRead(gzFile, &movieSnapshot, sizeof(movieSnapshot)); + if (VBAMovieActive() && !movieSnapshot) + { + systemMessage(0, N_("Can't load a non-movie snapshot while a movie is active.")); + goto failedLoad; + } + + if (movieSnapshot) // even if a movie isn't active we still want to parse through this in case other stuff is added + // later on in the save format + { + uint32 movieInputDataSize = 0; + utilGzRead(gzFile, &movieInputDataSize, sizeof(movieInputDataSize)); + uint8 *local_movie_data = new uint8[movieInputDataSize]; + int readBytes = utilGzRead(gzFile, local_movie_data, movieInputDataSize); + if (readBytes != movieInputDataSize) + { + systemMessage(0, N_("Corrupt movie snapshot.")); + if (local_movie_data) + delete [] local_movie_data; + goto failedLoad; + } + int code = VBAMovieUnfreeze(local_movie_data, movieInputDataSize); + if (local_movie_data) + delete [] local_movie_data; + if (code != MOVIE_SUCCESS && VBAMovieActive()) + { + char errStr [1024]; + strcpy(errStr, "Failed to load movie snapshot"); + switch (code) + { + case MOVIE_NOT_FROM_THIS_MOVIE: + strcat(errStr, ";\nSnapshot not from this movie"); break; + case MOVIE_NOT_FROM_A_MOVIE: + strcat(errStr, ";\nNot a movie snapshot"); break; // shouldn't get here... + case MOVIE_SNAPSHOT_INCONSISTENT: + strcat(errStr, ";\nSnapshot inconsistent with movie"); break; + case MOVIE_WRONG_FORMAT: + strcat(errStr, ";\nWrong format"); break; + } + strcat(errStr, "."); + systemMessage(0, N_(errStr)); + goto failedLoad; + } + } + utilGzRead(gzFile, &GBASystemCounters.frameCount, sizeof(GBASystemCounters.frameCount)); + } + if (version >= SAVE_GAME_VERSION_10) + { + utilGzRead(gzFile, memoryWait, 16 * sizeof(int32)); + utilGzRead(gzFile, memoryWait32, 16 * sizeof(int32)); + utilGzRead(gzFile, memoryWaitSeq, 16 * sizeof(int32)); + utilGzRead(gzFile, memoryWaitSeq32, 16 * sizeof(int32)); + utilGzRead(gzFile, memoryWaitFetch, 16 * sizeof(int32)); + utilGzRead(gzFile, memoryWaitFetch32, 16 * sizeof(int32)); + } + if (version >= SAVE_GAME_VERSION_11) + { + utilGzRead(gzFile, &prefetchActive, sizeof(bool8)); + //if(prefetchActive && !prefetchPrevActive) systemScreenMessage("pre-fetch enabled",3,600); + //if(!prefetchActive && prefetchPrevActive) systemScreenMessage("pre-fetch disabled",3,600); + utilGzRead(gzFile, &prefetchPrevActive, sizeof(bool8)); + utilGzRead(gzFile, &prefetchApplies, sizeof(bool8)); + } + if (version >= SAVE_GAME_VERSION_12) + { + utilGzRead(gzFile, &memLagTempEnabled, sizeof(bool8)); // necessary + utilGzRead(gzFile, &speedHack, sizeof(bool8)); // just in case it's ever used... + } + if (version >= SAVE_GAME_VERSION_13) + { + utilGzRead(gzFile, &GBASystemCounters.lagCount, sizeof(GBASystemCounters.lagCount)); + utilGzRead(gzFile, &GBASystemCounters.lagged, sizeof(GBASystemCounters.lagged)); + utilGzRead(gzFile, &GBASystemCounters.laggedLast, sizeof(GBASystemCounters.laggedLast)); + } + + if (backupSafe) + { + remove(tempBackupName); + tempFailCount = 0; + } + systemSetJoypad(0, ~P1 & 0x3FF); + VBAUpdateButtonPressDisplay(); + VBAUpdateFrameCountDisplay(); + systemRefreshScreen(); + return true; + +failedLoad: + if (backupSafe) + { + tempFailCount++; + if (tempFailCount < 3) // fail no more than 2 times in a row + CPUReadState(tempBackupName); + remove(tempBackupName); + } + return false; +} + +bool CPUReadMemState(char *memory, int available) +{ + gzFile gzFile = utilMemGzOpen(memory, available, "r"); + + backupSafe = false; + bool res = CPUReadStateFromStream(gzFile); + backupSafe = true; + + utilGzClose(gzFile); + + return res; +} + +bool CPUReadState(const char *file) +{ + gzFile gzFile = utilGzOpen(file, "rb"); + + if (gzFile == NULL) + return false; + + bool res = CPUReadStateFromStream(gzFile); + + utilGzClose(gzFile); + + return res; +} + +bool CPUExportEepromFile(const char *fileName) +{ + if (eepromInUse) + { + FILE *file = fopen(fileName, "wb"); + + if (!file) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), + fileName); + return false; + } + + for (int i = 0; i < eepromSize; ) + { + for (int j = 0; j < 8; j++) + { + if (fwrite(&eepromData[i + 7 - j], 1, 1, file) != 1) + { + fclose(file); + return false; + } + } + i += 8; + } + fclose(file); + } + return true; +} + +bool CPUWriteBatteryToStream(gzFile gzFile) +{ + if (!gzFile) + return false; + + utilWriteInt(gzFile, SAVE_GAME_VERSION); + + // for simplicity, we put both types of battery files should be in the stream, even if one's empty + eepromSaveGame(gzFile); + flashSaveGame(gzFile); + + return true; +} + +bool CPUWriteBatteryFile(const char *fileName) +{ + if (gbaSaveType == 0) + { + if (eepromInUse) + gbaSaveType = 3; + else + switch (saveType) + { + case 1: + gbaSaveType = 1; + break; + case 2: + gbaSaveType = 2; + break; + } + } + + if (gbaSaveType) + { + FILE *file = fopen(fileName, "wb"); + + if (!file) + { + systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), + fileName); + return false; + } + + // only save if Flash/Sram in use or EEprom in use + if (gbaSaveType != 3) + { + if (gbaSaveType == 2) + { + if (fwrite(flashSaveMemory, 1, flashSize, file) != (size_t)flashSize) + { + fclose(file); + return false; + } + } + else + { + if (fwrite(flashSaveMemory, 1, 0x10000, file) != 0x10000) + { + fclose(file); + return false; + } + } + } + else + { + if (fwrite(eepromData, 1, eepromSize, file) != (size_t)eepromSize) + { + fclose(file); + return false; + } + } + fclose(file); + } + + return true; +} + +bool CPUReadGSASnapshot(const char *fileName) +{ + int i; + FILE *file = fopen(fileName, "rb"); + + if (!file) + { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName); + return false; + } + + // check file size to know what we should read + fseek(file, 0, SEEK_END); + + // long size = ftell(file); + fseek(file, 0x0, SEEK_SET); + fread(&i, 1, 4, file); + fseek(file, i, SEEK_CUR); // Skip SharkPortSave + fseek(file, 4, SEEK_CUR); // skip some sort of flag + fread(&i, 1, 4, file); // name length + fseek(file, i, SEEK_CUR); // skip name + fread(&i, 1, 4, file); // desc length + fseek(file, i, SEEK_CUR); // skip desc + fread(&i, 1, 4, file); // notes length + fseek(file, i, SEEK_CUR); // skip notes + int saveSize; + fread(&saveSize, 1, 4, file); // read length + saveSize -= 0x1c; // remove header size + char buffer[17]; + char buffer2[17]; + fread(buffer, 1, 16, file); + buffer[16] = 0; + for (i = 0; i < 16; i++) + if (buffer[i] < 32) + buffer[i] = 32; + memcpy(buffer2, &rom[0xa0], 16); + buffer2[16] = 0; + for (i = 0; i < 16; i++) + if (buffer2[i] < 32) + buffer2[i] = 32; + if (memcmp(buffer, buffer2, 16)) + { + systemMessage(MSG_CANNOT_IMPORT_SNAPSHOT_FOR, + N_("Cannot import snapshot for %s. Current game is %s"), + buffer, + buffer2); + fclose(file); + return false; + } + fseek(file, 12, SEEK_CUR); // skip some flags + if (saveSize >= 65536) + { + if (fread(flashSaveMemory, 1, saveSize, file) != (size_t)saveSize) + { + fclose(file); + return false; + } + } + else + { + systemMessage(MSG_UNSUPPORTED_SNAPSHOT_FILE, + N_("Unsupported snapshot file %s"), + fileName); + fclose(file); + return false; + } + fclose(file); + CPUReset(); + return true; +} + +bool CPUWriteGSASnapshot(const char *fileName, + const char *title, + const char *desc, + const char *notes) +{ + FILE *file = fopen(fileName, "wb"); + + if (!file) + { + systemMessage(MSG_CANNOT_OPEN_FILE, N_("Cannot open file %s"), fileName); + return false; + } + + u8 buffer[17]; + + utilPutDword(buffer, 0x0d); // SharkPortSave length + fwrite(buffer, 1, 4, file); + fwrite("SharkPortSave", 1, 0x0d, file); + utilPutDword(buffer, 0x000f0000); + fwrite(buffer, 1, 4, file); // save type 0x000f0000 = GBA save + utilPutDword(buffer, strlen(title)); + fwrite(buffer, 1, 4, file); // title length + fwrite(title, 1, strlen(title), file); + utilPutDword(buffer, strlen(desc)); + fwrite(buffer, 1, 4, file); // desc length + fwrite(desc, 1, strlen(desc), file); + utilPutDword(buffer, strlen(notes)); + fwrite(buffer, 1, 4, file); // notes length + fwrite(notes, 1, strlen(notes), file); + int saveSize = 0x10000; + if (gbaSaveType == 2) + saveSize = flashSize; + int totalSize = saveSize + 0x1c; + + utilPutDword(buffer, totalSize); // length of remainder of save - CRC + fwrite(buffer, 1, 4, file); + + char temp[0x2001c]; + memset(temp, 0, 28); + memcpy(temp, &rom[0xa0], 16); // copy internal name + temp[0x10] = rom[0xbe]; // reserved area (old checksum) + temp[0x11] = rom[0xbf]; // reserved area (old checksum) + temp[0x12] = rom[0xbd]; // complement check + temp[0x13] = rom[0xb0]; // maker + temp[0x14] = 1; // 1 save ? + memcpy(&temp[0x1c], flashSaveMemory, saveSize); // copy save + fwrite(temp, 1, totalSize, file); // write save + header + u32 crc = 0; + + for (int i = 0; i < totalSize; i++) + { + crc += ((u32)temp[i] << (crc % 0x18)); + } + + utilPutDword(buffer, crc); + fwrite(buffer, 1, 4, file); // CRC? + + fclose(file); + return true; +} + +bool CPUImportEepromFile(const char *fileName) +{ + FILE *file = fopen(fileName, "rb"); + + if (!file) + return false; + + // check file size to know what we should read + fseek(file, 0, SEEK_END); + + long size = ftell(file); + fseek(file, 0, SEEK_SET); + if (size == 512 || size == 0x2000) + { + if (fread(eepromData, 1, size, file) != (size_t)size) + { + fclose(file); + return false; + } + for (int i = 0; i < size; ) + { + u8 tmp = eepromData[i]; + eepromData[i] = eepromData[7 - i]; + eepromData[7 - i] = tmp; + i++; + tmp = eepromData[i]; + eepromData[i] = eepromData[7 - i]; + eepromData[7 - i] = tmp; + i++; + tmp = eepromData[i]; + eepromData[i] = eepromData[7 - i]; + eepromData[7 - i] = tmp; + i++; + tmp = eepromData[i]; + eepromData[i] = eepromData[7 - i]; + eepromData[7 - i] = tmp; + i++; + i += 4; + } + } + else + { + fclose(file); + return false; + } + fclose(file); + return true; +} + +bool CPUReadBatteryFromStream(gzFile gzFile) +{ + if (!gzFile) + return false; + + int version = utilReadInt(gzFile); + + // for simplicity, we put both types of battery files should be in the stream, even if one's empty + eepromReadGame(gzFile, version); + flashReadGame(gzFile, version); + + return true; +} + +bool CPUReadBatteryFile(const char *fileName) +{ + FILE *file = fopen(fileName, "rb"); + + if (!file) + return false; + + // check file size to know what we should read + fseek(file, 0, SEEK_END); + + long size = ftell(file); + fseek(file, 0, SEEK_SET); + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + if (size == 512 || size == 0x2000) + { + if (fread(eepromData, 1, size, file) != (size_t)size) + { + fclose(file); + return false; + } + } + else + { + if (size == 0x20000) + { + if (fread(flashSaveMemory, 1, 0x20000, file) != 0x20000) + { + fclose(file); + return false; + } + flashSetSize(0x20000); + } + else + { + if (fread(flashSaveMemory, 1, 0x10000, file) != 0x10000) + { + fclose(file); + return false; + } + flashSetSize(0x10000); + } + } + fclose(file); + return true; +} + +bool CPUWritePNGFile(const char *fileName) +{ + return utilWritePNGFile(fileName, 240, 160, pix); +} + +bool CPUWriteBMPFile(const char *fileName) +{ + return utilWriteBMPFile(fileName, 240, 160, pix); +} + +void CPUCleanUp() +{ + newFrame = true; + + GBASystemCounters.frameCount = 0; + GBASystemCounters.lagCount = 0; + GBASystemCounters.extraCount = 0; + GBASystemCounters.lagged = true; + GBASystemCounters.laggedLast = true; + +#ifdef PROFILING + if (profilingTicksReload) + { + profCleanup(); + } +#endif + +#if (defined(WIN32) && !defined(SDL)) + #define FreeMappedMem(name, mapName, offset) \ + if (name != NULL) { \ + UnmapViewOfFile((name) - (offset)); \ + name = NULL; \ + CloseHandle(mapName); \ + } +#else + #define FreeMappedMem(name, mapName, offset) \ + if (name != NULL) { \ + free(name); \ + name = NULL; \ + } +#endif + + FreeMappedMem(rom, mapROM, 0); + FreeMappedMem(vram, mapVRAM, 0); + FreeMappedMem(paletteRAM, mapPALETTERAM, 0); + FreeMappedMem(internalRAM, mapIRAM, 0); + FreeMappedMem(workRAM, mapWORKRAM, 0); + FreeMappedMem(bios, mapBIOS, 0); + FreeMappedMem(pix, mapPIX, 4); + FreeMappedMem(oam, mapOAM, 0); + FreeMappedMem(ioMem, mapIOMEM, 0); + + eepromErase(); + flashErase(); + + elfCleanUp(); + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + systemClearJoypads(); + systemResetSensor(); + +// gbaLastTime = gbaFrameCount = 0; + systemRefreshScreen(); +} + +int CPULoadRom(const char *szFile) +{ + int size = 0x2000000; + + if (rom != NULL) + { + CPUCleanUp(); + } + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + // size+4 is so RAM search and watch are safe to read any byte in the allocated region as a 4-byte int +#if (defined(WIN32) && !defined(SDL)) + #define AllocMappedMem(name, mapName, nameStr, size, useCalloc, offset) \ + mapName = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (size) + (offset) + (4), nameStr); \ + if ((mapName) && GetLastError() == ERROR_ALREADY_EXISTS) { \ + CloseHandle(mapName); \ + mapName = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (size) + (offset) + (4), NULL); \ + } \ + name = (u8 *)MapViewOfFile(mapName, FILE_MAP_WRITE, 0, 0, 0) + (offset); \ + if ((name) == NULL) { \ + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), nameStr); \ + CPUCleanUp(); \ + return 0; \ + } \ + memset(name, 0, size + 4); +#else + #define AllocMappedMem(name, mapName, nameStr, size, useCalloc, offset) \ + name = (u8 *)(useCalloc ? calloc(1, size + 4) : malloc(size + 4)); \ + if ((name) == NULL) { \ + systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), nameStr); \ + CPUCleanUp(); \ + return 0; \ + } \ + memset(name, 0, size + 4); +#endif + + AllocMappedMem(rom, mapROM, "vbaROM", 0x2000000, false, 0); + AllocMappedMem(workRAM, mapWORKRAM, "vbaWORKRAM", 0x40000, true, 0); + + u8 *whereToLoad = rom; + if (cpuIsMultiBoot) + whereToLoad = workRAM; + + if (utilIsELF(szFile)) + { + FILE *f = fopen(szFile, "rb"); + if (!f) + { + systemMessage(MSG_ERROR_OPENING_IMAGE, N_("Error opening image %s"), + szFile); + FreeMappedMem(rom, mapROM, 0); + FreeMappedMem(workRAM, mapWORKRAM, 0); + return 0; + } + bool res = elfRead(szFile, size, f); + if (!res || size == 0) + { + FreeMappedMem(rom, mapROM, 0); + FreeMappedMem(workRAM, mapWORKRAM, 0); + elfCleanUp(); + return 0; + } + } + else if (!utilLoad(szFile, + utilIsGBAImage, + whereToLoad, + size)) + { + FreeMappedMem(rom, mapROM, 0); + FreeMappedMem(workRAM, mapWORKRAM, 0); + return 0; + } + + u16 *temp = (u16 *)(rom + ((size + 1) & ~1)); + int i; + for (i = (size + 1) & ~1; i < 0x2000000; i += 2) + { + WRITE16LE(temp, (i >> 1) & 0xFFFF); + temp++; + } + + AllocMappedMem(bios, mapBIOS, "vbaBIOS", 0x4000, true, 0); + AllocMappedMem(internalRAM, mapIRAM, "vbaIRAM", 0x8000, true, 0); + AllocMappedMem(paletteRAM, mapPALETTERAM, "vbaPALETTERAM", 0x400, true, 0); + AllocMappedMem(vram, mapVRAM, "vbaVRAM", 0x20000, true, 0); + AllocMappedMem(oam, mapOAM, "vbaOAM", 0x400, true, 0); + + // HACK: +4 at start to accomodate the 2xSaI filter reading out of bounds of the leftmost pixel + AllocMappedMem(pix, mapPIX, "vbaPIX", 4 * 241 * 162, true, 4); + AllocMappedMem(ioMem, mapIOMEM, "vbaIOMEM", 0x400, true, 0); + + CPUUpdateRenderBuffers(true); + + return size; +} + +void CPUUpdateRender() +{ + switch (DISPCNT & 7) + { + case 0: + if ((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode0RenderLine; + else if (fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode0RenderLineNoWindow; + else + renderLine = mode0RenderLineAll; + break; + case 1: + if ((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode1RenderLine; + else if (fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode1RenderLineNoWindow; + else + renderLine = mode1RenderLineAll; + break; + case 2: + if ((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode2RenderLine; + else if (fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode2RenderLineNoWindow; + else + renderLine = mode2RenderLineAll; + break; + case 3: + if ((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode3RenderLine; + else if (fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode3RenderLineNoWindow; + else + renderLine = mode3RenderLineAll; + break; + case 4: + if ((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode4RenderLine; + else if (fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode4RenderLineNoWindow; + else + renderLine = mode4RenderLineAll; + break; + case 5: + if ((!fxOn && !windowOn && !(layerEnable & 0x8000)) || + cpuDisableSfx) + renderLine = mode5RenderLine; + else if (fxOn && !windowOn && !(layerEnable & 0x8000)) + renderLine = mode5RenderLineNoWindow; + else + renderLine = mode5RenderLineAll; + default: + break; + } +} + +void CPUUpdateCPSR() +{ + u32 CPSR = reg[16].I & 0x40; + if (N_FLAG) + CPSR |= 0x80000000; + if (Z_FLAG) + CPSR |= 0x40000000; + if (C_FLAG) + CPSR |= 0x20000000; + if (V_FLAG) + CPSR |= 0x10000000; + if (!armState) + CPSR |= 0x00000020; + if (!armIrqEnable) + CPSR |= 0x80; + CPSR |= (armMode & 0x1F); + reg[16].I = CPSR; +} + +void CPUUpdateFlags(bool breakLoop) +{ + u32 CPSR = reg[16].I; + + N_FLAG = (CPSR & 0x80000000) ? true : false; + Z_FLAG = (CPSR & 0x40000000) ? true : false; + C_FLAG = (CPSR & 0x20000000) ? true : false; + V_FLAG = (CPSR & 0x10000000) ? true : false; + armState = (CPSR & 0x20) ? false : true; + armIrqEnable = (CPSR & 0x80) ? false : true; + if (breakLoop) + { + if (armIrqEnable && (IF & IE) && (IME & 1)) + { + CPU_BREAK_LOOP_2; + } + } +} + +void CPUUpdateFlags() +{ + CPUUpdateFlags(true); +} + +#ifdef WORDS_BIGENDIAN +static void CPUSwap(volatile u32 *a, volatile u32 *b) +{ + volatile u32 c = *b; + *b = *a; + *a = c; +} + +#else +static void CPUSwap(u32 *a, u32 *b) +{ + u32 c = *b; + *b = *a; + *a = c; +} + +#endif + +void CPUSwitchMode(int mode, bool saveState, bool breakLoop) +{ + // if(armMode == mode) + // return; + + CPUUpdateCPSR(); + + switch (armMode) + { + case 0x10: + case 0x1F: + reg[R13_USR].I = reg[13].I; + reg[R14_USR].I = reg[14].I; + reg[17].I = reg[16].I; + break; + case 0x11: + CPUSwap(®[R8_FIQ].I, ®[8].I); + CPUSwap(®[R9_FIQ].I, ®[9].I); + CPUSwap(®[R10_FIQ].I, ®[10].I); + CPUSwap(®[R11_FIQ].I, ®[11].I); + CPUSwap(®[R12_FIQ].I, ®[12].I); + reg[R13_FIQ].I = reg[13].I; + reg[R14_FIQ].I = reg[14].I; + reg[SPSR_FIQ].I = reg[17].I; + break; + case 0x12: + reg[R13_IRQ].I = reg[13].I; + reg[R14_IRQ].I = reg[14].I; + reg[SPSR_IRQ].I = reg[17].I; + break; + case 0x13: + reg[R13_SVC].I = reg[13].I; + reg[R14_SVC].I = reg[14].I; + reg[SPSR_SVC].I = reg[17].I; + break; + case 0x17: + reg[R13_ABT].I = reg[13].I; + reg[R14_ABT].I = reg[14].I; + reg[SPSR_ABT].I = reg[17].I; + break; + case 0x1b: + reg[R13_UND].I = reg[13].I; + reg[R14_UND].I = reg[14].I; + reg[SPSR_UND].I = reg[17].I; + break; + } + + u32 CPSR = reg[16].I; + u32 SPSR = reg[17].I; + + switch (mode) + { + case 0x10: + case 0x1F: + reg[13].I = reg[R13_USR].I; + reg[14].I = reg[R14_USR].I; + reg[16].I = SPSR; + break; + case 0x11: + CPUSwap(®[8].I, ®[R8_FIQ].I); + CPUSwap(®[9].I, ®[R9_FIQ].I); + CPUSwap(®[10].I, ®[R10_FIQ].I); + CPUSwap(®[11].I, ®[R11_FIQ].I); + CPUSwap(®[12].I, ®[R12_FIQ].I); + reg[13].I = reg[R13_FIQ].I; + reg[14].I = reg[R14_FIQ].I; + if (saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_FIQ].I; + break; + case 0x12: + reg[13].I = reg[R13_IRQ].I; + reg[14].I = reg[R14_IRQ].I; + reg[16].I = SPSR; + if (saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_IRQ].I; + break; + case 0x13: + reg[13].I = reg[R13_SVC].I; + reg[14].I = reg[R14_SVC].I; + reg[16].I = SPSR; + if (saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_SVC].I; + break; + case 0x17: + reg[13].I = reg[R13_ABT].I; + reg[14].I = reg[R14_ABT].I; + reg[16].I = SPSR; + if (saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_ABT].I; + break; + case 0x1b: + reg[13].I = reg[R13_UND].I; + reg[14].I = reg[R14_UND].I; + reg[16].I = SPSR; + if (saveState) + reg[17].I = CPSR; + else + reg[17].I = reg[SPSR_UND].I; + break; + default: + systemMessage(MSG_UNSUPPORTED_ARM_MODE, N_("Unsupported ARM mode %02x"), mode); + break; + } + armMode = mode; + CPUUpdateFlags(breakLoop); + CPUUpdateCPSR(); +} + +void CPUSwitchMode(int mode, bool saveState) +{ + CPUSwitchMode(mode, saveState, true); +} + +void CPUUndefinedException() +{ + u32 PC = reg[15].I; + bool savedArmState = armState; + CPUSwitchMode(0x1b, true, false); + reg[14].I = PC - (savedArmState ? 4 : 2); + reg[15].I = 0x04; + armState = true; + armIrqEnable = false; + armNextPC = 0x04; + reg[15].I += 4; +} + +void CPUSoftwareInterrupt() +{ + u32 PC = reg[15].I; + bool savedArmState = armState; + CPUSwitchMode(0x13, true, false); + reg[14].I = PC - (savedArmState ? 4 : 2); + reg[15].I = 0x08; + armState = true; + armIrqEnable = false; + armNextPC = 0x08; + reg[15].I += 4; +} + +void CPUSoftwareInterrupt(int comment) +{ + static bool disableMessage = false; + if (armState) + comment >>= 16; +#ifdef BKPT_SUPPORT + if (comment == 0xff) + { + extern void (*dbgOutput)(char *, u32); + dbgOutput(NULL, reg[0].I); + return; + } +#endif +#ifdef PROFILING + if (comment == 0xfe) + { + profStartup(reg[0].I, reg[1].I); + return; + } + if (comment == 0xfd) + { + profControl(reg[0].I); + return; + } + if (comment == 0xfc) + { + profCleanup(); + return; + } + if (comment == 0xfb) + { + profCount(); + return; + } +#endif + if (comment == 0xfa) + { + agbPrintFlush(); + return; + } +#ifdef SDL + if (comment == 0xf9) + { + emulating = 0; + CPU_BREAK_LOOP_2; + return; + } +#endif + if (useBios) + { +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("SWI: %08x at %08x (0x%08x,0x%08x,0x%08x,VCOUNT = %2d)\n", comment, + armState ? armNextPC - 4 : armNextPC - 2, + reg[0].I, + reg[1].I, + reg[2].I, + VCOUNT); + } +#endif + CPUSoftwareInterrupt(); + return; + } + // This would be correct, but it causes problems if uncommented + // else { + // biosProtected = 0xe3a02004; + // } + + switch (comment) + { + case 0x00: + BIOS_SoftReset(); + break; + case 0x01: + BIOS_RegisterRamReset(); + break; + case 0x02: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Halt: (VCOUNT = %2d)\n", + VCOUNT); + } +#endif + holdState = true; + holdType = -1; + break; + case 0x03: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Stop: (VCOUNT = %2d)\n", + VCOUNT); + } +#endif + holdState = true; + holdType = -1; + stopState = true; + break; + case 0x04: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("IntrWait: 0x%08x,0x%08x (VCOUNT = %2d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + CPUSoftwareInterrupt(); + break; + case 0x05: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("VBlankIntrWait: (VCOUNT = %2d)\n", + VCOUNT); + } +#endif + CPUSoftwareInterrupt(); + break; + case 0x06: + CPUSoftwareInterrupt(); + break; + case 0x07: + CPUSoftwareInterrupt(); + break; + case 0x08: + BIOS_Sqrt(); + break; + case 0x09: + BIOS_ArcTan(); + break; + case 0x0A: + BIOS_ArcTan2(); + break; + case 0x0B: + BIOS_CpuSet(); + break; + case 0x0C: + BIOS_CpuFastSet(); + break; + case 0x0E: + BIOS_BgAffineSet(); + break; + case 0x0F: + BIOS_ObjAffineSet(); + break; + case 0x10: + BIOS_BitUnPack(); + break; + case 0x11: + BIOS_LZ77UnCompWram(); + break; + case 0x12: + BIOS_LZ77UnCompVram(); + break; + case 0x13: + BIOS_HuffUnComp(); + break; + case 0x14: + BIOS_RLUnCompWram(); + break; + case 0x15: + BIOS_RLUnCompVram(); + break; + case 0x16: + BIOS_Diff8bitUnFilterWram(); + break; + case 0x17: + BIOS_Diff8bitUnFilterVram(); + break; + case 0x18: + BIOS_Diff16bitUnFilter(); + break; + case 0x19: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("SoundBiasSet: 0x%08x (VCOUNT = %2d)\n", + reg[0].I, + VCOUNT); + } +#endif + if (reg[0].I) + soundPause(); + else + soundResume(); + break; + case 0x1F: + BIOS_MidiKey2Freq(); + break; + case 0x2A: + BIOS_SndDriverJmpTableCopy(); + // let it go, because we don't really emulate this function // FIXME (?) + default: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("SWI: %08x at %08x (0x%08x,0x%08x,0x%08x,VCOUNT = %2d)\n", comment, + armState ? armNextPC - 4 : armNextPC - 2, + reg[0].I, + reg[1].I, + reg[2].I, + VCOUNT); + } +#endif + + if (!disableMessage) + { + systemMessage(MSG_UNSUPPORTED_BIOS_FUNCTION, + N_( + "Unsupported BIOS function %02x called from %08x. A BIOS file is needed in order to get correct behaviour."), + comment, + armMode ? armNextPC - 4 : armNextPC - 2); + disableMessage = true; + } + break; + } +} + +void CPUCompareVCOUNT() +{ + if (VCOUNT == (DISPSTAT >> 8)) + { + DISPSTAT |= 4; + UPDATE_REG(0x04, DISPSTAT); + + if (DISPSTAT & 0x20) + { + IF |= 4; + UPDATE_REG(0x202, IF); + } + } + else + { + DISPSTAT &= 0xFFFB; + UPDATE_REG(0x4, DISPSTAT); + } +} + +void doDMA(u32 &s, u32 &d, u32 si, u32 di, u32 c, int transfer32) +{ + int sm = s >> 24; + int dm = d >> 24; + + int sc = c; + + cpuDmaCount = c; + + if (transfer32) + { + s &= 0xFFFFFFFC; + if (s < 0x02000000 && (reg[15].I >> 24)) + { + while (c != 0) + { + CPUWriteMemory(d, 0); + d += di; + c--; + } + } + else + { + while (c != 0) + { + CPUWriteMemory(d, CPUReadMemory(s)); + d += di; + s += si; + c--; + } + } + } + else + { + s &= 0xFFFFFFFE; + si = (int)si >> 1; + di = (int)di >> 1; + if (s < 0x02000000 && (reg[15].I >> 24)) + { + while (c != 0) + { + CPUWriteHalfWord(d, 0); + d += di; + c--; + } + } + else + { + while (c != 0) + { + cpuDmaLast = CPUReadHalfWord(s); + CPUWriteHalfWord(d, cpuDmaLast); + d += di; + s += si; + c--; + } + } + } + + cpuDmaCount = 0; + + int sw = 1 + memoryWaitSeq[sm & 15]; + int dw = 1 + memoryWaitSeq[dm & 15]; + + int totalTicks = 0; + + if (transfer32) + { + if (!memory32[sm & 15]) + sw <<= 1; + if (!memory32[dm & 15]) + dw <<= 1; + } + + totalTicks = (sw + dw) * sc; + + cpuDmaTicksToUpdate += totalTicks; + + if (*extCpuLoopTicks >= 0) + { + CPU_BREAK_LOOP; + } +} + +void CPUCheckDMA(int reason, int dmamask) +{ + cpuDmaHack = 0; + // DMA 0 + if ((DM0CNT_H & 0x8000) && (dmamask & 1)) + { + if (((DM0CNT_H >> 12) & 3) == reason) + { + u32 sourceIncrement = 4; + u32 destIncrement = 4; + switch ((DM0CNT_H >> 7) & 3) + { + case 0: + break; + case 1: + sourceIncrement = (u32) - 4; + break; + case 2: + sourceIncrement = 0; + break; + } + switch ((DM0CNT_H >> 5) & 3) + { + case 0: + break; + case 1: + destIncrement = (u32) - 4; + break; + case 2: + destIncrement = 0; + break; + } +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_DMA0) + { + int count = (DM0CNT_L ? DM0CNT_L : 0x4000) << 1; + if (DM0CNT_H & 0x0400) + count <<= 1; + log("DMA0: s=%08x d=%08x c=%04x count=%08x\n", dma0Source, dma0Dest, + DM0CNT_H, + count); + } +#endif + doDMA(dma0Source, dma0Dest, sourceIncrement, destIncrement, + DM0CNT_L ? DM0CNT_L : 0x4000, + DM0CNT_H & 0x0400); + cpuDmaHack = 1; + if (DM0CNT_H & 0x4000) + { + IF |= 0x0100; + UPDATE_REG(0x202, IF); + } + + if (((DM0CNT_H >> 5) & 3) == 3) + { + dma0Dest = DM0DAD_L | (DM0DAD_H << 16); + } + + if (!(DM0CNT_H & 0x0200) || (reason == 0)) + { + DM0CNT_H &= 0x7FFF; + UPDATE_REG(0xBA, DM0CNT_H); + } + } + } + + // DMA 1 + if ((DM1CNT_H & 0x8000) && (dmamask & 2)) + { + if (((DM1CNT_H >> 12) & 3) == reason) + { + u32 sourceIncrement = 4; + u32 destIncrement = 4; + switch ((DM1CNT_H >> 7) & 3) + { + case 0: + break; + case 1: + sourceIncrement = (u32) - 4; + break; + case 2: + sourceIncrement = 0; + break; + } + switch ((DM1CNT_H >> 5) & 3) + { + case 0: + break; + case 1: + destIncrement = (u32) - 4; + break; + case 2: + destIncrement = 0; + break; + } + if (reason == 3) + { +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_DMA1) + { + log("DMA1: s=%08x d=%08x c=%04x count=%08x\n", dma1Source, dma1Dest, + DM1CNT_H, + 16); + } +#endif + doDMA(dma1Source, dma1Dest, sourceIncrement, 0, 4, + 0x0400); + } + else + { +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_DMA1) + { + int count = (DM1CNT_L ? DM1CNT_L : 0x4000) << 1; + if (DM1CNT_H & 0x0400) + count <<= 1; + log("DMA1: s=%08x d=%08x c=%04x count=%08x\n", dma1Source, dma1Dest, + DM1CNT_H, + count); + } +#endif + doDMA(dma1Source, dma1Dest, sourceIncrement, destIncrement, + DM1CNT_L ? DM1CNT_L : 0x4000, + DM1CNT_H & 0x0400); + } + cpuDmaHack = 1; + + if (DM1CNT_H & 0x4000) + { + IF |= 0x0200; + UPDATE_REG(0x202, IF); + } + + if (((DM1CNT_H >> 5) & 3) == 3) + { + dma1Dest = DM1DAD_L | (DM1DAD_H << 16); + } + + if (!(DM1CNT_H & 0x0200) || (reason == 0)) + { + DM1CNT_H &= 0x7FFF; + UPDATE_REG(0xC6, DM1CNT_H); + } + } + } + + // DMA 2 + if ((DM2CNT_H & 0x8000) && (dmamask & 4)) + { + if (((DM2CNT_H >> 12) & 3) == reason) + { + u32 sourceIncrement = 4; + u32 destIncrement = 4; + switch ((DM2CNT_H >> 7) & 3) + { + case 0: + break; + case 1: + sourceIncrement = (u32) - 4; + break; + case 2: + sourceIncrement = 0; + break; + } + switch ((DM2CNT_H >> 5) & 3) + { + case 0: + break; + case 1: + destIncrement = (u32) - 4; + break; + case 2: + destIncrement = 0; + break; + } + if (reason == 3) + { +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_DMA2) + { + int count = (4) << 2; + log("DMA2: s=%08x d=%08x c=%04x count=%08x\n", dma2Source, dma2Dest, + DM2CNT_H, + count); + } +#endif + doDMA(dma2Source, dma2Dest, sourceIncrement, 0, 4, + 0x0400); + } + else + { +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_DMA2) + { + int count = (DM2CNT_L ? DM2CNT_L : 0x4000) << 1; + if (DM2CNT_H & 0x0400) + count <<= 1; + log("DMA2: s=%08x d=%08x c=%04x count=%08x\n", dma2Source, dma2Dest, + DM2CNT_H, + count); + } +#endif + doDMA(dma2Source, dma2Dest, sourceIncrement, destIncrement, + DM2CNT_L ? DM2CNT_L : 0x4000, + DM2CNT_H & 0x0400); + } + cpuDmaHack = 1; + if (DM2CNT_H & 0x4000) + { + IF |= 0x0400; + UPDATE_REG(0x202, IF); + } + + if (((DM2CNT_H >> 5) & 3) == 3) + { + dma2Dest = DM2DAD_L | (DM2DAD_H << 16); + } + + if (!(DM2CNT_H & 0x0200) || (reason == 0)) + { + DM2CNT_H &= 0x7FFF; + UPDATE_REG(0xD2, DM2CNT_H); + } + } + } + + // DMA 3 + if ((DM3CNT_H & 0x8000) && (dmamask & 8)) + { + if (((DM3CNT_H >> 12) & 3) == reason) + { + u32 sourceIncrement = 4; + u32 destIncrement = 4; + switch ((DM3CNT_H >> 7) & 3) + { + case 0: + break; + case 1: + sourceIncrement = (u32) - 4; + break; + case 2: + sourceIncrement = 0; + break; + } + switch ((DM3CNT_H >> 5) & 3) + { + case 0: + break; + case 1: + destIncrement = (u32) - 4; + break; + case 2: + destIncrement = 0; + break; + } +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_DMA3) + { + int count = (DM3CNT_L ? DM3CNT_L : 0x10000) << 1; + if (DM3CNT_H & 0x0400) + count <<= 1; + log("DMA3: s=%08x d=%08x c=%04x count=%08x\n", dma3Source, dma3Dest, + DM3CNT_H, + count); + } +#endif + doDMA(dma3Source, dma3Dest, sourceIncrement, destIncrement, + DM3CNT_L ? DM3CNT_L : 0x10000, + DM3CNT_H & 0x0400); + if (DM3CNT_H & 0x4000) + { + IF |= 0x0800; + UPDATE_REG(0x202, IF); + } + + if (((DM3CNT_H >> 5) & 3) == 3) + { + dma3Dest = DM3DAD_L | (DM3DAD_H << 16); + } + + if (!(DM3CNT_H & 0x0200) || (reason == 0)) + { + DM3CNT_H &= 0x7FFF; + UPDATE_REG(0xDE, DM3CNT_H); + } + } + } + cpuDmaHack = 0; +} + +void CPUUpdateRegister(u32 address, u16 value) +{ + switch (address) + { + case 0x00: + { + bool change = ((DISPCNT ^ value) & 0x80) ? true : false; + bool changeBG = ((DISPCNT ^ value) & 0x0F00) ? true : false; + DISPCNT = (value & 0xFFF7); + UPDATE_REG(0x00, DISPCNT); + layerEnable = layerSettings & value; + windowOn = (layerEnable & 0x6000) ? true : false; + if (change && !((value & 0x80))) + { + if (!(DISPSTAT & 1)) + { + lcdTicks = 960; + // VCOUNT = 0; + // UPDATE_REG(0x06, VCOUNT); + DISPSTAT &= 0xFFFC; + UPDATE_REG(0x04, DISPSTAT); + CPUCompareVCOUNT(); + } + // (*renderLine)(); + } + CPUUpdateRender(); + // we only care about changes in BG0-BG3 + if (changeBG) + CPUUpdateRenderBuffers(false); + // CPUUpdateTicks(); + break; + } + case 0x04: + DISPSTAT = (value & 0xFF38) | (DISPSTAT & 7); + UPDATE_REG(0x04, DISPSTAT); + break; + case 0x06: + // not writable + break; + case 0x08: + BG0CNT = (value & 0xDFCF); + UPDATE_REG(0x08, BG0CNT); + break; + case 0x0A: + BG1CNT = (value & 0xDFCF); + UPDATE_REG(0x0A, BG1CNT); + break; + case 0x0C: + BG2CNT = (value & 0xFFCF); + UPDATE_REG(0x0C, BG2CNT); + break; + case 0x0E: + BG3CNT = (value & 0xFFCF); + UPDATE_REG(0x0E, BG3CNT); + break; + case 0x10: + BG0HOFS = value & 511; + UPDATE_REG(0x10, BG0HOFS); + break; + case 0x12: + BG0VOFS = value & 511; + UPDATE_REG(0x12, BG0VOFS); + break; + case 0x14: + BG1HOFS = value & 511; + UPDATE_REG(0x14, BG1HOFS); + break; + case 0x16: + BG1VOFS = value & 511; + UPDATE_REG(0x16, BG1VOFS); + break; + case 0x18: + BG2HOFS = value & 511; + UPDATE_REG(0x18, BG2HOFS); + break; + case 0x1A: + BG2VOFS = value & 511; + UPDATE_REG(0x1A, BG2VOFS); + break; + case 0x1C: + BG3HOFS = value & 511; + UPDATE_REG(0x1C, BG3HOFS); + break; + case 0x1E: + BG3VOFS = value & 511; + UPDATE_REG(0x1E, BG3VOFS); + break; + case 0x20: + BG2PA = value; + UPDATE_REG(0x20, BG2PA); + break; + case 0x22: + BG2PB = value; + UPDATE_REG(0x22, BG2PB); + break; + case 0x24: + BG2PC = value; + UPDATE_REG(0x24, BG2PC); + break; + case 0x26: + BG2PD = value; + UPDATE_REG(0x26, BG2PD); + break; + case 0x28: + BG2X_L = value; + UPDATE_REG(0x28, BG2X_L); + gfxBG2Changed |= 1; + break; + case 0x2A: + BG2X_H = (value & 0xFFF); + UPDATE_REG(0x2A, BG2X_H); + gfxBG2Changed |= 1; + break; + case 0x2C: + BG2Y_L = value; + UPDATE_REG(0x2C, BG2Y_L); + gfxBG2Changed |= 2; + break; + case 0x2E: + BG2Y_H = value & 0xFFF; + UPDATE_REG(0x2E, BG2Y_H); + gfxBG2Changed |= 2; + break; + case 0x30: + BG3PA = value; + UPDATE_REG(0x30, BG3PA); + break; + case 0x32: + BG3PB = value; + UPDATE_REG(0x32, BG3PB); + break; + case 0x34: + BG3PC = value; + UPDATE_REG(0x34, BG3PC); + break; + case 0x36: + BG3PD = value; + UPDATE_REG(0x36, BG3PD); + break; + case 0x38: + BG3X_L = value; + UPDATE_REG(0x38, BG3X_L); + gfxBG3Changed |= 1; + break; + case 0x3A: + BG3X_H = value & 0xFFF; + UPDATE_REG(0x3A, BG3X_H); + gfxBG3Changed |= 1; + break; + case 0x3C: + BG3Y_L = value; + UPDATE_REG(0x3C, BG3Y_L); + gfxBG3Changed |= 2; + break; + case 0x3E: + BG3Y_H = value & 0xFFF; + UPDATE_REG(0x3E, BG3Y_H); + gfxBG3Changed |= 2; + break; + case 0x40: + WIN0H = value; + UPDATE_REG(0x40, WIN0H); + CPUUpdateWindow0(); + break; + case 0x42: + WIN1H = value; + UPDATE_REG(0x42, WIN1H); + CPUUpdateWindow1(); + break; + case 0x44: + WIN0V = value; + UPDATE_REG(0x44, WIN0V); + break; + case 0x46: + WIN1V = value; + UPDATE_REG(0x46, WIN1V); + break; + case 0x48: + WININ = value & 0x3F3F; + UPDATE_REG(0x48, WININ); + break; + case 0x4A: + WINOUT = value & 0x3F3F; + UPDATE_REG(0x4A, WINOUT); + break; + case 0x4C: + MOSAIC = value; + UPDATE_REG(0x4C, MOSAIC); + break; + case 0x50: + BLDMOD = value & 0x3FFF; + UPDATE_REG(0x50, BLDMOD); + fxOn = ((BLDMOD >> 6) & 3) != 0; + CPUUpdateRender(); + break; + case 0x52: + COLEV = value & 0x1F1F; + UPDATE_REG(0x52, COLEV); + break; + case 0x54: + COLY = value & 0x1F; + UPDATE_REG(0x54, COLY); + break; + case 0x60: + case 0x62: + case 0x64: + case 0x68: + case 0x6c: + case 0x70: + case 0x72: + case 0x74: + case 0x78: + case 0x7c: + case 0x80: + case 0x84: + soundEvent(address & 0xFF, (u8)(value & 0xFF)); + soundEvent((address & 0xFF) + 1, (u8)(value >> 8)); + break; + case 0x82: + case 0x88: + case 0xa0: + case 0xa2: + case 0xa4: + case 0xa6: + case 0x90: + case 0x92: + case 0x94: + case 0x96: + case 0x98: + case 0x9a: + case 0x9c: + case 0x9e: + soundEvent(address & 0xFF, value); + break; + case 0xB0: + DM0SAD_L = value; + UPDATE_REG(0xB0, DM0SAD_L); + break; + case 0xB2: + DM0SAD_H = value & 0x07FF; + UPDATE_REG(0xB2, DM0SAD_H); + break; + case 0xB4: + DM0DAD_L = value; + UPDATE_REG(0xB4, DM0DAD_L); + break; + case 0xB6: + DM0DAD_H = value & 0x07FF; + UPDATE_REG(0xB6, DM0DAD_H); + break; + case 0xB8: + DM0CNT_L = value & 0x3FFF; + UPDATE_REG(0xB8, 0); + break; + case 0xBA: + { + bool start = ((DM0CNT_H ^ value) & 0x8000) ? true : false; + value &= 0xF7E0; + + DM0CNT_H = value; + UPDATE_REG(0xBA, DM0CNT_H); + + if (start && (value & 0x8000)) + { + dma0Source = DM0SAD_L | (DM0SAD_H << 16); + dma0Dest = DM0DAD_L | (DM0DAD_H << 16); + CPUCheckDMA(0, 1); + } + break; + } + case 0xBC: + DM1SAD_L = value; + UPDATE_REG(0xBC, DM1SAD_L); + break; + case 0xBE: + DM1SAD_H = value & 0x0FFF; + UPDATE_REG(0xBE, DM1SAD_H); + break; + case 0xC0: + DM1DAD_L = value; + UPDATE_REG(0xC0, DM1DAD_L); + break; + case 0xC2: + DM1DAD_H = value & 0x07FF; + UPDATE_REG(0xC2, DM1DAD_H); + break; + case 0xC4: + DM1CNT_L = value & 0x3FFF; + UPDATE_REG(0xC4, 0); + break; + case 0xC6: + { + bool start = ((DM1CNT_H ^ value) & 0x8000) ? true : false; + value &= 0xF7E0; + + DM1CNT_H = value; + UPDATE_REG(0xC6, DM1CNT_H); + + if (start && (value & 0x8000)) + { + dma1Source = DM1SAD_L | (DM1SAD_H << 16); + dma1Dest = DM1DAD_L | (DM1DAD_H << 16); + CPUCheckDMA(0, 2); + } + break; + } + case 0xC8: + DM2SAD_L = value; + UPDATE_REG(0xC8, DM2SAD_L); + break; + case 0xCA: + DM2SAD_H = value & 0x0FFF; + UPDATE_REG(0xCA, DM2SAD_H); + break; + case 0xCC: + DM2DAD_L = value; + UPDATE_REG(0xCC, DM2DAD_L); + break; + case 0xCE: + DM2DAD_H = value & 0x07FF; + UPDATE_REG(0xCE, DM2DAD_H); + break; + case 0xD0: + DM2CNT_L = value & 0x3FFF; + UPDATE_REG(0xD0, 0); + break; + case 0xD2: + { + bool start = ((DM2CNT_H ^ value) & 0x8000) ? true : false; + + value &= 0xF7E0; + + DM2CNT_H = value; + UPDATE_REG(0xD2, DM2CNT_H); + + if (start && (value & 0x8000)) + { + dma2Source = DM2SAD_L | (DM2SAD_H << 16); + dma2Dest = DM2DAD_L | (DM2DAD_H << 16); + + CPUCheckDMA(0, 4); + } + break; + } + case 0xD4: + DM3SAD_L = value; + UPDATE_REG(0xD4, DM3SAD_L); + break; + case 0xD6: + DM3SAD_H = value & 0x0FFF; + UPDATE_REG(0xD6, DM3SAD_H); + break; + case 0xD8: + DM3DAD_L = value; + UPDATE_REG(0xD8, DM3DAD_L); + break; + case 0xDA: + DM3DAD_H = value & 0x0FFF; + UPDATE_REG(0xDA, DM3DAD_H); + break; + case 0xDC: + DM3CNT_L = value; + UPDATE_REG(0xDC, 0); + break; + case 0xDE: + { + bool start = ((DM3CNT_H ^ value) & 0x8000) ? true : false; + + value &= 0xFFE0; + + DM3CNT_H = value; + UPDATE_REG(0xDE, DM3CNT_H); + + if (start && (value & 0x8000)) + { + dma3Source = DM3SAD_L | (DM3SAD_H << 16); + dma3Dest = DM3DAD_L | (DM3DAD_H << 16); + CPUCheckDMA(0, 8); + } + break; + } + case 0x100: + timer0Reload = value; + break; + case 0x102: + timer0Ticks = timer0ClockReload = TIMER_TICKS[value & 3]; + if (!timer0On && (value & 0x80)) + { + // reload the counter + TM0D = timer0Reload; + if (timer0ClockReload == 1) + timer0Ticks = 0x10000 - TM0D; + UPDATE_REG(0x100, TM0D); + } + timer0On = value & 0x80 ? true : false; + TM0CNT = value & 0xC7; + UPDATE_REG(0x102, TM0CNT); + // CPUUpdateTicks(); + break; + case 0x104: + timer1Reload = value; + break; + case 0x106: + timer1Ticks = timer1ClockReload = TIMER_TICKS[value & 3]; + if (!timer1On && (value & 0x80)) + { + // reload the counter + TM1D = timer1Reload; + if (timer1ClockReload == 1) + timer1Ticks = 0x10000 - TM1D; + UPDATE_REG(0x104, TM1D); + } + timer1On = value & 0x80 ? true : false; + TM1CNT = value & 0xC7; + UPDATE_REG(0x106, TM1CNT); + break; + case 0x108: + timer2Reload = value; + break; + case 0x10A: + timer2Ticks = timer2ClockReload = TIMER_TICKS[value & 3]; + if (!timer2On && (value & 0x80)) + { + // reload the counter + TM2D = timer2Reload; + if (timer2ClockReload == 1) + timer2Ticks = 0x10000 - TM2D; + UPDATE_REG(0x108, TM2D); + } + timer2On = value & 0x80 ? true : false; + TM2CNT = value & 0xC7; + UPDATE_REG(0x10A, TM2CNT); + break; + case 0x10C: + timer3Reload = value; + break; + case 0x10E: + timer3Ticks = timer3ClockReload = TIMER_TICKS[value & 3]; + if (!timer3On && (value & 0x80)) + { + // reload the counter + TM3D = timer3Reload; + if (timer3ClockReload == 1) + timer3Ticks = 0x10000 - TM3D; + UPDATE_REG(0x10C, TM3D); + } + timer3On = value & 0x80 ? true : false; + TM3CNT = value & 0xC7; + UPDATE_REG(0x10E, TM3CNT); + break; + case 0x128: + if (value & 0x80) + { + value &= 0xff7f; + if (value & 1 && (value & 0x4000)) + { + UPDATE_REG(0x12a, 0xFF); + IF |= 0x80; + UPDATE_REG(0x202, IF); + value &= 0x7f7f; + } + } + UPDATE_REG(0x128, value); + break; + case 0x130: + P1 |= (value & 0x3FF); + UPDATE_REG(0x130, P1); + break; + case 0x132: + UPDATE_REG(0x132, value & 0xC3FF); + break; + case 0x200: + IE = value & 0x3FFF; + UPDATE_REG(0x200, IE); + if ((IME & 1) && (IF & IE) && armIrqEnable) + { + CPU_BREAK_LOOP_2; + } + break; + case 0x202: + IF ^= (value & IF); + UPDATE_REG(0x202, IF); + break; + case 0x204: + { + int i; + memoryWait[0x0e] = memoryWaitSeq[0x0e] = gamepakRamWaitState[value & 3]; + + if (!speedHack) + { + memoryWait[0x08] = memoryWait[0x09] = gamepakWaitState[(value >> 2) & 7]; + memoryWaitSeq[0x08] = memoryWaitSeq[0x09] = + gamepakWaitState0[(value >> 2) & 7]; + + memoryWait[0x0a] = memoryWait[0x0b] = gamepakWaitState[(value >> 5) & 7]; + memoryWaitSeq[0x0a] = memoryWaitSeq[0x0b] = + gamepakWaitState1[(value >> 5) & 7]; + + memoryWait[0x0c] = memoryWait[0x0d] = gamepakWaitState[(value >> 8) & 7]; + memoryWaitSeq[0x0c] = memoryWaitSeq[0x0d] = + gamepakWaitState2[(value >> 8) & 7]; + } + else + { + memoryWait[0x08] = memoryWait[0x09] = 4; + memoryWaitSeq[0x08] = memoryWaitSeq[0x09] = 2; + + memoryWait[0x0a] = memoryWait[0x0b] = 4; + memoryWaitSeq[0x0a] = memoryWaitSeq[0x0b] = 4; + + memoryWait[0x0c] = memoryWait[0x0d] = 4; + memoryWaitSeq[0x0c] = memoryWaitSeq[0x0d] = 8; + } + for (i = 0; i < 16; i++) + { + memoryWaitFetch32[i] = memoryWait32[i] = memoryWait[i] * + (memory32[i] ? 1 : 2); + memoryWaitFetch[i] = memoryWait[i]; + } + memoryWaitFetch32[3] += 1; + memoryWaitFetch32[2] += 3; + + prefetchActive = false; + prefetchApplies = false; + if (value & 0x4000) + { + for (i = 8; i < 16; i++) + { + memoryWaitFetch32[i] = 2 * cpuMemoryWait[i]; + memoryWaitFetch[i] = cpuMemoryWait[i]; + } + if (((value & 3) == 3)) + { + if (!memLagTempEnabled) + { + memoryWaitFetch[8]--; // hack to prevent inaccurately extreme lag at some points of many games (possibly + // from no pre-fetch emulation) + /// FIXME: how correct is this? Should it set the fetch to 0 or change fetch32 or + // anything else? + + prefetchActive = true; + } + prefetchApplies = true; + } + } + //if(prefetchActive && !prefetchPrevActive) systemScreenMessage("pre-fetch enabled",3,600); + //if(!prefetchActive && prefetchPrevActive) systemScreenMessage("pre-fetch disabled",3,600); + prefetchPrevActive = prefetchActive; + + UPDATE_REG(0x204, value); + break; + } + case 0x208: + IME = value & 1; + UPDATE_REG(0x208, IME); + if ((IME & 1) && (IF & IE) && armIrqEnable) + { + CPU_BREAK_LOOP_2; + } + break; + case 0x300: + if (value != 0) + value &= 0xFFFE; + UPDATE_REG(0x300, value); + break; + default: + UPDATE_REG(address & 0x3FE, value); + break; + } +} + +void CPUWriteHalfWordWrapped(u32 address, u16 value) +{ +#ifdef GBA_LOGGING + if (address & 1) + { + if (systemVerbose & VERBOSE_UNALIGNED_MEMORY) + { + log("Unaligned halfword write: %04x to %08x from %08x\n", + value, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } + } +#endif + + switch (address >> 24) + { + case 2: +#ifdef SDL + if (*((u16 *)&freezeWorkRAM[address & 0x3FFFE])) + cheatsWriteHalfWord((u16 *)&workRAM[address & 0x3FFFE], + value, + *((u16 *)&freezeWorkRAM[address & 0x3FFFE])); + else +#endif + WRITE16LE(((u16 *)&workRAM[address & 0x3FFFE]), value); + break; + case 3: +#ifdef SDL + if (*((u16 *)&freezeInternalRAM[address & 0x7ffe])) + cheatsWriteHalfWord((u16 *)&internalRAM[address & 0x7ffe], + value, + *((u16 *)&freezeInternalRAM[address & 0x7ffe])); + else +#endif + WRITE16LE(((u16 *)&internalRAM[address & 0x7ffe]), value); + break; + case 4: + CPUUpdateRegister(address & 0x3fe, value); + break; + case 5: + WRITE16LE(((u16 *)&paletteRAM[address & 0x3fe]), value); + break; + case 6: + if (address & 0x10000) + WRITE16LE(((u16 *)&vram[address & 0x17ffe]), value); + else + WRITE16LE(((u16 *)&vram[address & 0x1fffe]), value); + break; + case 7: + WRITE16LE(((u16 *)&oam[address & 0x3fe]), value); + break; + case 8: + case 9: + if (address == 0x80000c4 || address == 0x80000c6 || address == 0x80000c8) + { + if (!rtcWrite(address, value)) + goto unwritable; + } + else if (!agbPrintWrite(address, value)) + goto unwritable; + break; + case 13: + if (cpuEEPROMEnabled) + { + eepromWrite(address, (u8)(value & 0xFF)); + break; + } + goto unwritable; + case 14: + if (!eepromInUse | cpuSramEnabled | cpuFlashEnabled) + { + (*cpuSaveGameFunc)(address, (u8)(value & 0xFF)); + break; + } + goto unwritable; + default: +unwritable: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_WRITE) + { + log("Illegal halfword write: %04x to %08x from %08x\n", + value, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } +#endif + break; + } +} + +void CPUWriteHalfWord(u32 address, u16 value) +{ + CPUWriteHalfWordWrapped(address, value); + CallRegisteredLuaMemHook(address, 2, value, LUAMEMHOOK_WRITE); +} + +void CPUWriteByteWrapped(u32 address, u8 b) +{ + switch (address >> 24) + { + case 2: +#ifdef SDL + if (freezeWorkRAM[address & 0x3FFFF]) + cheatsWriteByte(&workRAM[address & 0x3FFFF], b); + else +#endif + workRAM[address & 0x3FFFF] = b; + break; + case 3: +#ifdef SDL + if (freezeInternalRAM[address & 0x7fff]) + cheatsWriteByte(&internalRAM[address & 0x7fff], b); + else +#endif + internalRAM[address & 0x7fff] = b; + break; + case 4: + switch (address & 0x3FF) + { + case 0x301: + if (b == 0x80) + stopState = true; + holdState = 1; + holdType = -1; + break; + case 0x60: + case 0x61: + case 0x62: + case 0x63: + case 0x64: + case 0x65: + case 0x68: + case 0x69: + case 0x6c: + case 0x6d: + case 0x70: + case 0x71: + case 0x72: + case 0x73: + case 0x74: + case 0x75: + case 0x78: + case 0x79: + case 0x7c: + case 0x7d: + case 0x80: + case 0x81: + case 0x84: + case 0x85: + case 0x90: + case 0x91: + case 0x92: + case 0x93: + case 0x94: + case 0x95: + case 0x96: + case 0x97: + case 0x98: + case 0x99: + case 0x9a: + case 0x9b: + case 0x9c: + case 0x9d: + case 0x9e: + case 0x9f: + soundEvent(address & 0xFF, b); + break; + default: + // if(address & 1) { + // CPUWriteHalfWord(address-1, (CPUReadHalfWord(address-1)&0x00FF)|((int)b<<8)); + // } else + if (address & 1) + CPUUpdateRegister(address & 0x3fe, + ((READ16LE(((u16 *)&ioMem[address & 0x3fe]))) + & 0x00FF) | + b << 8); + else + CPUUpdateRegister(address & 0x3fe, + ((READ16LE(((u16 *)&ioMem[address & 0x3fe])) & 0xFF00) | b)); + } + break; + case 5: + // no need to switch + *((u16 *)&paletteRAM[address & 0x3FE]) = (b << 8) | b; + break; + case 6: + // no need to switch + if (address & 0x10000) + *((u16 *)&vram[address & 0x17FFE]) = (b << 8) | b; + else + *((u16 *)&vram[address & 0x1FFFE]) = (b << 8) | b; + break; + case 7: + // no need to switch + *((u16 *)&oam[address & 0x3FE]) = (b << 8) | b; + break; + case 13: + if (cpuEEPROMEnabled) + { + eepromWrite(address, b); + break; + } + goto unwritable; + case 14: + if (!eepromInUse | cpuSramEnabled | cpuFlashEnabled) + { + (*cpuSaveGameFunc)(address, b); + break; + } + // default + default: +unwritable: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_WRITE) + { + log("Illegal byte write: %02x to %08x from %08x\n", + b, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } +#endif + break; + } +} + +void CPUWriteByte(u32 address, u8 b) +{ + CPUWriteByteWrapped(address, b); + CallRegisteredLuaMemHook(address, 1, b, LUAMEMHOOK_WRITE); +} + +bool CPULoadBios(const char *biosFileName, bool useBiosFile) +{ + useBios = false; + if (useBiosFile) + { + useBios = utilLoadBIOS(bios, biosFileName, 4); + if (!useBios) + { + systemMessage(MSG_INVALID_BIOS_FILE_SIZE, N_("Invalid GBA BIOS file")); + } + } + + if (!useBios) + { + // load internal BIOS + memcpy(bios, myROM, sizeof(myROM)); + } + + return useBios; +} + +void CPUInit() +{ +#ifdef WORDS_BIGENDIAN + if (!cpuBiosSwapped) + { + for (unsigned int i = 0; i < sizeof(myROM) / 4; i++) + { + WRITE32LE(&myROM[i], myROM[i]); + } + cpuBiosSwapped = true; + } +#endif + gbaSaveType = 0; + eepromInUse = 0; + saveType = 0; + + if (!useBios) + { + // load internal BIOS + memcpy(bios, myROM, sizeof(myROM)); + } + + biosProtected[0] = 0x00; + biosProtected[1] = 0xf0; + biosProtected[2] = 0x29; + biosProtected[3] = 0xe1; + + int i = 0; + for (i = 0; i < 256; i++) + { + int cpuBitSetCount = 0; + int j; + for (j = 0; j < 8; j++) + if (i & (1 << j)) + cpuBitSetCount++; + cpuBitsSet[i] = cpuBitSetCount; + + for (j = 0; j < 8; j++) + if (i & (1 << j)) + break; + cpuLowestBitSet[i] = j; + } + + for (i = 0; i < 0x400; i++) + ioReadable[i] = true; + for (i = 0x10; i < 0x48; i++) + ioReadable[i] = false; + for (i = 0x4c; i < 0x50; i++) + ioReadable[i] = false; + for (i = 0x54; i < 0x60; i++) + ioReadable[i] = false; + for (i = 0x8c; i < 0x90; i++) + ioReadable[i] = false; + for (i = 0xa0; i < 0xb8; i++) + ioReadable[i] = false; + for (i = 0xbc; i < 0xc4; i++) + ioReadable[i] = false; + for (i = 0xc8; i < 0xd0; i++) + ioReadable[i] = false; + for (i = 0xd4; i < 0xdc; i++) + ioReadable[i] = false; + for (i = 0xe0; i < 0x100; i++) + ioReadable[i] = false; + for (i = 0x110; i < 0x120; i++) + ioReadable[i] = false; + for (i = 0x12c; i < 0x130; i++) + ioReadable[i] = false; + for (i = 0x138; i < 0x140; i++) + ioReadable[i] = false; + for (i = 0x144; i < 0x150; i++) + ioReadable[i] = false; + for (i = 0x15c; i < 0x200; i++) + ioReadable[i] = false; + for (i = 0x20c; i < 0x300; i++) + ioReadable[i] = false; + for (i = 0x304; i < 0x400; i++) + ioReadable[i] = false; + + *((u16 *)&rom[0x1fe209c]) = 0xdffa; // SWI 0xFA + *((u16 *)&rom[0x1fe209e]) = 0x4770; // BX LR + + { + int32 origMemoryWaitFetch[16] = { 3, 0, 3, 0, 0, 1, 1, 0, 4, 4, 4, 4, 4, 4, 4, 0 }; + int32 origMemoryWaitFetch32[16] = { 6, 0, 6, 0, 0, 2, 2, 0, 8, 8, 8, 8, 8, 8, 8, 0 }; + memcpy(memoryWaitFetch, origMemoryWaitFetch, 16 * sizeof(int32)); + memcpy(memoryWaitFetch32, origMemoryWaitFetch32, 16 * sizeof(int32)); + } +} + +void CPUReset(bool userReset) +{ + // movie must be closed while opening/creating a movie + if (userReset && VBAMovieRecording()) + { + VBAMovieSignalReset(); + return; + } + + if (!VBAMovieActive()) + { + GBASystemCounters.frameCount = 0; + GBASystemCounters.lagCount = 0; + GBASystemCounters.extraCount = 0; + GBASystemCounters.lagged = true; + GBASystemCounters.laggedLast = true; + } + + if (gbaSaveType == 0) + { + if (eepromInUse) + gbaSaveType = 3; + else + switch (saveType) + { + case 1: + gbaSaveType = 1; + break; + case 2: + gbaSaveType = 2; + break; + } + } + + rtcReset(); + // clean registers + memset(®[0], 0, sizeof(reg)); + // clean OAM + memset(oam, 0, 0x400); + // clean palette + memset(paletteRAM, 0, 0x400); + // clean picture + memset(pix, 0, 4 * 241 * 162); + // clean vram + memset(vram, 0, 0x20000); + // clean io memory + memset(ioMem, 0, 0x400); + // clean RAM + memset(internalRAM, 0, 0x8000); /// FIXME: is it unsafe to erase ALL of this? Even the init code doesn't. + memset(workRAM, 0, 0x40000); /// ditto + + DISPCNT = 0x0080; + DISPSTAT = 0x0000; + VCOUNT = 0x0000; + BG0CNT = 0x0000; + BG1CNT = 0x0000; + BG2CNT = 0x0000; + BG3CNT = 0x0000; + BG0HOFS = 0x0000; + BG0VOFS = 0x0000; + BG1HOFS = 0x0000; + BG1VOFS = 0x0000; + BG2HOFS = 0x0000; + BG2VOFS = 0x0000; + BG3HOFS = 0x0000; + BG3VOFS = 0x0000; + BG2PA = 0x0100; + BG2PB = 0x0000; + BG2PC = 0x0000; + BG2PD = 0x0100; + BG2X_L = 0x0000; + BG2X_H = 0x0000; + BG2Y_L = 0x0000; + BG2Y_H = 0x0000; + BG3PA = 0x0100; + BG3PB = 0x0000; + BG3PC = 0x0000; + BG3PD = 0x0100; + BG3X_L = 0x0000; + BG3X_H = 0x0000; + BG3Y_L = 0x0000; + BG3Y_H = 0x0000; + WIN0H = 0x0000; + WIN1H = 0x0000; + WIN0V = 0x0000; + WIN1V = 0x0000; + WININ = 0x0000; + WINOUT = 0x0000; + MOSAIC = 0x0000; + BLDMOD = 0x0000; + COLEV = 0x0000; + COLY = 0x0000; + DM0SAD_L = 0x0000; + DM0SAD_H = 0x0000; + DM0DAD_L = 0x0000; + DM0DAD_H = 0x0000; + DM0CNT_L = 0x0000; + DM0CNT_H = 0x0000; + DM1SAD_L = 0x0000; + DM1SAD_H = 0x0000; + DM1DAD_L = 0x0000; + DM1DAD_H = 0x0000; + DM1CNT_L = 0x0000; + DM1CNT_H = 0x0000; + DM2SAD_L = 0x0000; + DM2SAD_H = 0x0000; + DM2DAD_L = 0x0000; + DM2DAD_H = 0x0000; + DM2CNT_L = 0x0000; + DM2CNT_H = 0x0000; + DM3SAD_L = 0x0000; + DM3SAD_H = 0x0000; + DM3DAD_L = 0x0000; + DM3DAD_H = 0x0000; + DM3CNT_L = 0x0000; + DM3CNT_H = 0x0000; + TM0D = 0x0000; + TM0CNT = 0x0000; + TM1D = 0x0000; + TM1CNT = 0x0000; + TM2D = 0x0000; + TM2CNT = 0x0000; + TM3D = 0x0000; + TM3CNT = 0x0000; + P1 = 0x03FF; + IE = 0x0000; + IF = 0x0000; + IME = 0x0000; + + armMode = 0x1F; + + if (cpuIsMultiBoot) + { + reg[13].I = 0x03007F00; + reg[15].I = 0x02000000; + reg[16].I = 0x00000000; + reg[R13_IRQ].I = 0x03007FA0; + reg[R13_SVC].I = 0x03007FE0; + armIrqEnable = true; + } + else + { + if (useBios && !skipBios) + { + reg[15].I = 0x00000000; + armMode = 0x13; + armIrqEnable = false; + } + else + { + reg[13].I = 0x03007F00; + reg[15].I = 0x08000000; + reg[16].I = 0x00000000; + reg[R13_IRQ].I = 0x03007FA0; + reg[R13_SVC].I = 0x03007FE0; + armIrqEnable = true; + } + } + armState = true; + C_FLAG = V_FLAG = N_FLAG = Z_FLAG = false; + UPDATE_REG(0x00, DISPCNT); + UPDATE_REG(0x20, BG2PA); + UPDATE_REG(0x26, BG2PD); + UPDATE_REG(0x30, BG3PA); + UPDATE_REG(0x36, BG3PD); + UPDATE_REG(0x130, P1); + UPDATE_REG(0x88, 0x200); + + // disable FIQ + reg[16].I |= 0x40; + CPUUpdateCPSR(); + + armNextPC = reg[15].I; + reg[15].I += 4; + + // reset internal state + holdState = false; + holdType = 0; + + biosProtected[0] = 0x00; + biosProtected[1] = 0xf0; + biosProtected[2] = 0x29; + biosProtected[3] = 0xe1; + + BIOS_RegisterRamReset(); + + lcdTicks = 960; + timer0On = false; + timer0Ticks = 0; + timer0Reload = 0; + timer0ClockReload = 0; + timer1On = false; + timer1Ticks = 0; + timer1Reload = 0; + timer1ClockReload = 0; + timer2On = false; + timer2Ticks = 0; + timer2Reload = 0; + timer2ClockReload = 0; + timer3On = false; + timer3Ticks = 0; + timer3Reload = 0; + timer3ClockReload = 0; + dma0Source = 0; + dma0Dest = 0; + dma1Source = 0; + dma1Dest = 0; + dma2Source = 0; + dma2Dest = 0; + dma3Source = 0; + dma3Dest = 0; + cpuSaveGameFunc = flashSaveDecide; + renderLine = mode0RenderLine; + fxOn = false; + windowOn = false; + frameSkipCount = 0; + saveType = 0; + layerEnable = DISPCNT & layerSettings; + + CPUUpdateRenderBuffers(true); + + for (int i = 0; i < 256; i++) + { + map[i].address = (u8 *)&dummyAddress; + map[i].mask = 0; + } + + map[0].address = bios; + map[0].mask = 0x3FFF; + map[2].address = workRAM; + map[2].mask = 0x3FFFF; + map[3].address = internalRAM; + map[3].mask = 0x7FFF; + map[4].address = ioMem; + map[4].mask = 0x3FF; + map[5].address = paletteRAM; + map[5].mask = 0x3FF; + map[6].address = vram; + map[6].mask = 0x1FFFF; + map[7].address = oam; + map[7].mask = 0x3FF; + map[8].address = rom; + map[8].mask = 0x1FFFFFF; + map[9].address = rom; + map[9].mask = 0x1FFFFFF; + map[10].address = rom; + map[10].mask = 0x1FFFFFF; + map[12].address = rom; + map[12].mask = 0x1FFFFFF; + map[14].address = flashSaveMemory; + map[14].mask = 0xFFFF; + + eepromReset(); + flashReset(); + + soundReset(); + + CPUUpdateWindow0(); + CPUUpdateWindow1(); + + // make sure registers are correctly initialized if not using BIOS + if (!useBios) + { + if (cpuIsMultiBoot) + BIOS_RegisterRamReset(0xfe); + else + BIOS_RegisterRamReset(0xff); + } + else + { + if (cpuIsMultiBoot) + BIOS_RegisterRamReset(0xfe); + } + + switch (cpuSaveType) + { + case 0: // automatic + cpuSramEnabled = true; + cpuFlashEnabled = true; + cpuEEPROMEnabled = true; + cpuEEPROMSensorEnabled = false; + break; + case 1: // EEPROM + cpuSramEnabled = false; + cpuFlashEnabled = false; + cpuEEPROMEnabled = true; + cpuEEPROMSensorEnabled = false; + break; + case 2: // SRAM + cpuSramEnabled = true; + cpuFlashEnabled = false; + cpuEEPROMEnabled = false; + cpuEEPROMSensorEnabled = false; + cpuSaveGameFunc = sramWrite; + break; + case 3: // FLASH + cpuSramEnabled = false; + cpuFlashEnabled = true; + cpuEEPROMEnabled = false; + cpuEEPROMSensorEnabled = false; + cpuSaveGameFunc = flashWrite; + break; + case 4: // EEPROM+Sensor + cpuSramEnabled = false; + cpuFlashEnabled = false; + cpuEEPROMEnabled = true; + cpuEEPROMSensorEnabled = true; + break; + case 5: // NONE + cpuSramEnabled = false; + cpuFlashEnabled = false; + cpuEEPROMEnabled = false; + cpuEEPROMSensorEnabled = false; + break; + } + + systemResetSensor(); + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + gbaLastTime = systemGetClock(); + gbaFrameCount = 0; + + systemRefreshScreen(); +} + +void CPUInterrupt() +{ + u32 PC = reg[15].I; + bool savedState = armState; + CPUSwitchMode(0x12, true, false); + reg[14].I = PC; + if (!savedState) + reg[14].I += 2; + reg[15].I = 0x18; + armState = true; + armIrqEnable = false; + + armNextPC = reg[15].I; + reg[15].I += 4; + + // if(!holdState) + biosProtected[0] = 0x02; + biosProtected[1] = 0xc0; + biosProtected[2] = 0x5e; + biosProtected[3] = 0xe5; +} + +void TogglePrefetchHack() +{ + memLagTempEnabled = !memLagTempEnabled; + + if (emulating) + { + extern bool8 prefetchActive, prefetchPrevActive, prefetchApplies; + if (prefetchApplies && prefetchActive == memLagTempEnabled) + { + prefetchActive = !prefetchActive; + //if(prefetchActive && !prefetchPrevActive) systemScreenMessage("pre-fetch enabled",3,600); + //if(!prefetchActive && prefetchPrevActive) systemScreenMessage("pre-fetch disabled",3,600); + extern int32 memoryWaitFetch [16]; + if (prefetchActive) + memoryWaitFetch[8]--; + else + memoryWaitFetch[8]++; + prefetchPrevActive = prefetchActive; + } + } +} + +void SetPrefetchHack(bool set) +{ + if ((bool)memLagTempEnabled == set) + TogglePrefetchHack(); +} + +#ifdef SDL +void log(const char *defaultMsg, ...) +{ + char buffer[2048]; + va_list valist; + + va_start(valist, defaultMsg); + vsprintf(buffer, defaultMsg, valist); + + if (out == NULL) + { + out = fopen("trace.log", "w"); + } + + fputs(buffer, out); + + va_end(valist); +} + +#else +extern void winlog(const char *, ...); +#endif + +void CPULoop(int _ticks) +{ + int32 ticks = _ticks; + int32 clockTicks; + int32 cpuLoopTicks = 0; + int32 timerOverflow = 0; + // variables used by the CPU core + + extCpuLoopTicks = &cpuLoopTicks; + extClockTicks = &clockTicks; + extTicks = &ticks; + + cpuLoopTicks = CPUUpdateTicks(); + if (cpuLoopTicks > ticks) + { + cpuLoopTicks = ticks; + cpuSavedTicks = ticks; + } + + if (intState) + { + cpuLoopTicks = 5; + cpuSavedTicks = 5; + } + + if (newFrame) + { + extern void VBAOnExitingFrameBoundary(); + VBAOnExitingFrameBoundary(); + + // update joystick information + systemReadJoypads(); + + u32 joy = systemGetJoypad(0, cpuEEPROMSensorEnabled); + +// if (cpuEEPROMSensorEnabled) +// systemUpdateMotionSensor(0); + + P1 = 0x03FF ^ (joy & 0x3FF); + UPDATE_REG(0x130, P1); + u16 P1CNT = READ16LE(((u16 *)&ioMem[0x132])); + // this seems wrong, but there are cases where the game + // can enter the stop state without requesting an IRQ from + // the joypad. + if ((P1CNT & 0x4000) || stopState) + { + u16 p1 = (0x3FF ^ P1) & 0x3FF; + if (P1CNT & 0x8000) + { + if (p1 == (P1CNT & 0x3FF)) + { + IF |= 0x1000; + UPDATE_REG(0x202, IF); + } + } + else + { + if (p1 & P1CNT) + { + IF |= 0x1000; + UPDATE_REG(0x202, IF); + } + } + } + + // HACK: some special "buttons" + extButtons = (joy >> 18); + speedup = (extButtons & 1) != 0; + + VBAMovieResetIfRequested(); + + CallRegisteredLuaFunctions(LUACALL_BEFOREEMULATION); + + newFrame = false; + } + + for (;; ) + { +#ifndef FINAL_VERSION + if (systemDebug) + { + if (systemDebug >= 10 && !holdState) + { + CPUUpdateCPSR(); + sprintf( + buffer, + "R00=%08x R01=%08x R02=%08x R03=%08x R04=%08x R05=%08x R06=%08x R07=%08x R08=%08x" + "R09=%08x R10=%08x R11=%08x R12=%08x R13=%08x R14=%08x R15=%08x R16=%08x R17=%08x\n", + reg[0].I, + reg[1].I, + reg[2].I, + reg[3].I, + reg[4].I, + reg[5].I, + reg[6].I, + reg[7].I, + reg[8].I, + reg[9].I, + reg[10].I, + reg[11].I, + reg[12].I, + reg[13].I, + reg[14].I, + reg[15].I, + reg[16].I, + reg[17].I); +#ifdef SDL + log(buffer); +#else + winlog(buffer); +#endif + } + else if (!holdState) + { + sprintf(buffer, "PC=%08x\n", armNextPC); +#ifdef SDL + log(buffer); +#else + winlog(buffer); +#endif + } + } +#endif + + if (!holdState) + { + if (armState) + { + CallRegisteredLuaMemHook(armNextPC, 4, CPUReadMemoryQuick(armNextPC), LUAMEMHOOK_EXEC); +#include "arm-new.h" + } + else + { + CallRegisteredLuaMemHook(armNextPC, 2, CPUReadHalfWordQuick(armNextPC), LUAMEMHOOK_EXEC); +#include "thumb.h" + } + } + else + { + clockTicks = lcdTicks; + + if (soundTicks < clockTicks) + clockTicks = soundTicks; + + if (timer0On && (timer0Ticks < clockTicks)) + { + clockTicks = timer0Ticks; + } + if (timer1On && (timer1Ticks < clockTicks)) + { + clockTicks = timer1Ticks; + } + if (timer2On && (timer2Ticks < clockTicks)) + { + clockTicks = timer2Ticks; + } + if (timer3On && (timer3Ticks < clockTicks)) + { + clockTicks = timer3Ticks; + } +#ifdef PROFILING + if (profilingTicksReload != 0) + { + if (profilingTicks < clockTicks) + { + clockTicks = profilingTicks; + } + } +#endif + } + + cpuLoopTicks -= clockTicks; + if ((cpuLoopTicks <= 0)) + { + if (cpuSavedTicks) + { + clockTicks = cpuSavedTicks; // + cpuLoopTicks; + } + cpuDmaTicksToUpdate = -cpuLoopTicks; + +updateLoop: + lcdTicks -= clockTicks; + + if (lcdTicks <= 0) + { + if (DISPSTAT & 1) // V-BLANK + { // if in V-Blank mode, keep computing... + if (DISPSTAT & 2) + { + lcdTicks += 960; + VCOUNT++; + UPDATE_REG(0x06, VCOUNT); + DISPSTAT &= 0xFFFD; + UPDATE_REG(0x04, DISPSTAT); + CPUCompareVCOUNT(); + } + else + { + lcdTicks += 272; + DISPSTAT |= 2; + UPDATE_REG(0x04, DISPSTAT); + if (DISPSTAT & 16) + { + IF |= 2; + UPDATE_REG(0x202, IF); + } + } + + if (VCOUNT >= 228) + { + DISPSTAT &= 0xFFFC; + UPDATE_REG(0x04, DISPSTAT); + VCOUNT = 0; + UPDATE_REG(0x06, VCOUNT); + CPUCompareVCOUNT(); + } + } + else + { + int framesToSkip = systemFramesToSkip(); + + if (DISPSTAT & 2) + { + // if in H-Blank, leave it and move to drawing mode + VCOUNT++; + UPDATE_REG(0x06, VCOUNT); + + lcdTicks += 960; + DISPSTAT &= 0xFFFD; + if (VCOUNT == 160) + { + DISPSTAT |= 1; + DISPSTAT &= 0xFFFD; + UPDATE_REG(0x04, DISPSTAT); + if (DISPSTAT & 0x0008) + { + IF |= 1; + UPDATE_REG(0x202, IF); + } + CPUCheckDMA(1, 0x0f); + + systemFrame(); + + ++gbaFrameCount; + u32 gbaCurrentTime = systemGetClock(); + if (gbaCurrentTime - gbaLastTime >= 1000) + { + systemShowSpeed(int(float(gbaFrameCount) * 100000 / (float(gbaCurrentTime - gbaLastTime) * 60) + .5f)); + gbaLastTime = gbaCurrentTime; + gbaFrameCount = 0; + } + + ++GBASystemCounters.frameCount; + if (GBASystemCounters.lagged) + { + ++GBASystemCounters.lagCount; + } + GBASystemCounters.laggedLast = GBASystemCounters.lagged; + GBASystemCounters.lagged = true; + + if (cheatsEnabled) + cheatsCheckKeys(P1 ^ 0x3FF, extButtons); + + extern void VBAOnEnteringFrameBoundary(); + VBAOnEnteringFrameBoundary(); + + newFrame = true; + + pauseAfterFrameAdvance = systemPauseOnFrame(); + + if (frameSkipCount >= framesToSkip || pauseAfterFrameAdvance) + { + systemRenderFrame(); + frameSkipCount = 0; + + bool capturePressed = (extButtons & 2) != 0; + if (capturePressed && !capturePrevious) + { + captureNumber = systemScreenCapture(captureNumber); + } + capturePrevious = capturePressed && !pauseAfterFrameAdvance; + } + else + { + ++frameSkipCount; + } + + if (pauseAfterFrameAdvance) + { + systemSetPause(true); + } + } + + UPDATE_REG(0x04, DISPSTAT); + CPUCompareVCOUNT(); + } + else + { + if (frameSkipCount >= framesToSkip || pauseAfterFrameAdvance) + { + (*renderLine)(); + + switch (systemColorDepth) + { + case 16: + { + u16 *dest = (u16 *)pix + 242 * (VCOUNT + 1); + for (int x = 0; x < 240; ) + { + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap16[lineMix[x++] & 0xFFFF]; + } + // for filters that read past the screen + *dest++ = 0; + break; + } + case 24: + { + u8 *dest = (u8 *)pix + 240 * VCOUNT * 3; + for (int x = 0; x < 240; ) + { + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + *((u32 *)dest) = systemColorMap32[lineMix[x++] & 0xFFFF]; + dest += 3; + } + break; + } + case 32: + { + u32 *dest = (u32 *)pix + 241 * (VCOUNT + 1); + for (int x = 0; x < 240; ) + { + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + *dest++ = systemColorMap32[lineMix[x++] & 0xFFFF]; + } + break; + } + } + } + // entering H-Blank + DISPSTAT |= 2; + UPDATE_REG(0x04, DISPSTAT); + lcdTicks += 272; + CPUCheckDMA(2, 0x0f); + if (DISPSTAT & 16) + { + IF |= 2; + UPDATE_REG(0x202, IF); + } + } + } + } + + if (!stopState) + { + if (timer0On) + { + if (timer0ClockReload == 1) + { + u32 tm0d = TM0D + clockTicks; + if (tm0d > 0xffff) + { + tm0d += timer0Reload; + timerOverflow |= 1; + soundTimerOverflow(0); + if (TM0CNT & 0x40) + { + IF |= 0x08; + UPDATE_REG(0x202, IF); + } + } + TM0D = tm0d & 0xFFFF; + timer0Ticks = 0x10000 - TM0D; + UPDATE_REG(0x100, TM0D); + } + else + { + timer0Ticks -= clockTicks; + if (timer0Ticks <= 0) + { + timer0Ticks += timer0ClockReload; + TM0D++; + if (TM0D == 0) + { + TM0D = timer0Reload; + timerOverflow |= 1; + soundTimerOverflow(0); + if (TM0CNT & 0x40) + { + IF |= 0x08; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x100, TM0D); + } + } + } + + if (timer1On) + { + if (TM1CNT & 4) + { + if (timerOverflow & 1) + { + TM1D++; + if (TM1D == 0) + { + TM1D += timer1Reload; + timerOverflow |= 2; + soundTimerOverflow(1); + if (TM1CNT & 0x40) + { + IF |= 0x10; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x104, TM1D); + } + } + else + { + if (timer1ClockReload == 1) + { + u32 tm1d = TM1D + clockTicks; + if (tm1d > 0xffff) + { + tm1d += timer1Reload; + timerOverflow |= 2; + soundTimerOverflow(1); + if (TM1CNT & 0x40) + { + IF |= 0x10; + UPDATE_REG(0x202, IF); + } + } + TM1D = tm1d & 0xFFFF; + timer1Ticks = 0x10000 - TM1D; + UPDATE_REG(0x104, TM1D); + } + else + { + timer1Ticks -= clockTicks; + if (timer1Ticks <= 0) + { + timer1Ticks += timer1ClockReload; + TM1D++; + + if (TM1D == 0) + { + TM1D = timer1Reload; + timerOverflow |= 2; + soundTimerOverflow(1); + if (TM1CNT & 0x40) + { + IF |= 0x10; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x104, TM1D); + } + } + } + } + + if (timer2On) + { + if (TM2CNT & 4) + { + if (timerOverflow & 2) + { + TM2D++; + if (TM2D == 0) + { + TM2D += timer2Reload; + timerOverflow |= 4; + if (TM2CNT & 0x40) + { + IF |= 0x20; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x108, TM2D); + } + } + else + { + if (timer2ClockReload == 1) + { + u32 tm2d = TM2D + clockTicks; + if (tm2d > 0xffff) + { + tm2d += timer2Reload; + timerOverflow |= 4; + if (TM2CNT & 0x40) + { + IF |= 0x20; + UPDATE_REG(0x202, IF); + } + } + TM2D = tm2d & 0xFFFF; + timer2Ticks = 0x10000 - TM2D; + UPDATE_REG(0x108, TM2D); + } + else + { + timer2Ticks -= clockTicks; + if (timer2Ticks <= 0) + { + timer2Ticks += timer2ClockReload; + TM2D++; + + if (TM2D == 0) + { + TM2D = timer2Reload; + timerOverflow |= 4; + if (TM2CNT & 0x40) + { + IF |= 0x20; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x108, TM2D); + } + } + } + } + + if (timer3On) + { + if (TM3CNT & 4) + { + if (timerOverflow & 4) + { + TM3D++; + if (TM3D == 0) + { + TM3D += timer3Reload; + if (TM3CNT & 0x40) + { + IF |= 0x40; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x10c, TM3D); + } + } + else + { + if (timer3ClockReload == 1) + { + u32 tm3d = TM3D + clockTicks; + if (tm3d > 0xffff) + { + tm3d += timer3Reload; + if (TM3CNT & 0x40) + { + IF |= 0x40; + UPDATE_REG(0x202, IF); + } + } + TM3D = tm3d & 0xFFFF; + timer3Ticks = 0x10000 - TM3D; + UPDATE_REG(0x10C, TM3D); + } + else + { + timer3Ticks -= clockTicks; + if (timer3Ticks <= 0) + { + timer3Ticks += timer3ClockReload; + TM3D++; + + if (TM3D == 0) + { + TM3D = timer3Reload; + if (TM3CNT & 0x40) + { + IF |= 0x40; + UPDATE_REG(0x202, IF); + } + } + UPDATE_REG(0x10C, TM3D); + } + } + } + } + } + // we shouldn't be doing sound in stop state, but we lose synchronization + // if sound is disabled, so in stop state, soundTick will just produce + // mute sound + soundTicks -= clockTicks; + if (soundTicks < 1) + { + soundTick(); + soundTicks += SOUND_CLOCK_TICKS; + } + timerOverflow = 0; + +#ifdef PROFILING + profilingTicks -= clockTicks; + if (profilingTicks <= 0) + { + profilingTicks += profilingTicksReload; + if (profilBuffer && profilSize) + { + u16 *b = (u16 *)profilBuffer; + int pc = ((reg[15].I - profilLowPC) * profilScale) / 0x10000; + if (pc >= 0 && pc < profilSize) + { + b[pc]++; + } + } + } +#endif + + ticks -= clockTicks; + cpuLoopTicks = CPUUpdateTicks(); + + // FIXME: it is too bad that it is still not determined whether the loop can be exited at this point + if (cpuDmaTicksToUpdate > 0) + { + clockTicks = cpuSavedTicks; + if (clockTicks > cpuDmaTicksToUpdate) + clockTicks = cpuDmaTicksToUpdate; + cpuDmaTicksToUpdate -= clockTicks; + if (cpuDmaTicksToUpdate < 0) + cpuDmaTicksToUpdate = 0; + goto updateLoop; // this is evil + } + + if (IF && (IME & 1) && armIrqEnable) + { + int res = IF & IE; + if (stopState) + res &= 0x3080; + if (res) + { + if (intState) + { + CPUInterrupt(); + intState = false; + if (holdState) + { + holdState = false; + stopState = false; + } + } + else + { + if (!holdState) + { + intState = true; + cpuLoopTicks = 5; + cpuSavedTicks = 5; + } + else + { + CPUInterrupt(); + if (holdState) + { + holdState = false; + stopState = false; + } + } + } + } + } + + if (useOldFrameTiming) + { + if (ticks <= 0) + { + newFrame = true; + break; + } + } + else if (newFrame) + { + // FIXME: it should be enough to use frameBoundary only if there were no need for supporting the old timing + // but is there still any GBA .vbm that uses the old timing? +/// extern void VBAOnEnteringFrameBoundary(); +/// VBAOnEnteringFrameBoundary(); + + break; + } + } + } +} + +struct EmulatedSystem GBASystem = +{ + // emuMain + CPULoop, + // emuReset + CPUReset, + // emuCleanUp + CPUCleanUp, + // emuReadBattery + CPUReadBatteryFile, + // emuWriteBattery + CPUWriteBatteryFile, + // emuReadBatteryFromStream + CPUReadBatteryFromStream, + // emuWriteBatteryToStream + CPUWriteBatteryToStream, + // emuReadState + CPUReadState, + // emuWriteState + CPUWriteState, + // emuReadStateFromStream + CPUReadStateFromStream, + // emuWriteStateToStream + CPUWriteStateToStream, + // emuReadMemState + CPUReadMemState, + // emuWriteMemState + CPUWriteMemState, + // emuWritePNG + CPUWritePNGFile, + // emuWriteBMP + CPUWriteBMPFile, + // emuUpdateCPSR + CPUUpdateCPSR, + // emuHasDebugger + true, + // emuCount +#ifdef FINAL_VERSION + 250000, +#else + 5000, +#endif +}; + +// is there a reason to use more than one set of counters? +EmulatedSystemCounters &GBASystemCounters = systemCounters; + +/* + EmulatedSystemCounters GBASystemCounters = + { + // frameCount + 0, + // lagCount + 0, + // lagged + true, + // laggedLast + true, + }; + */ + + +#undef CPU_BREAK_LOOP +#undef CPU_BREAK_LOOP2 diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBA.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBA.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,122 @@ +#ifndef VBA_GBA_H +#define VBA_GBA_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +#if (defined(WIN32) && !defined(SDL)) +#include "../win32/stdafx.h" // for HANDLE +//#include // for HANDLE +// NOTE: if you get this error: +// #error WINDOWS.H already included. MFC apps must not #include +// it is probably because stdafx.h is getting included at the wrong place +// (i.e. after anything else) in a file, or your precompiled headers are otherwise wrong +#endif + +#define SAVE_GAME_VERSION_1 1 +#define SAVE_GAME_VERSION_2 2 +#define SAVE_GAME_VERSION_3 3 +#define SAVE_GAME_VERSION_4 4 +#define SAVE_GAME_VERSION_5 5 +#define SAVE_GAME_VERSION_6 6 +#define SAVE_GAME_VERSION_7 7 +#define SAVE_GAME_VERSION_8 8 +#define SAVE_GAME_VERSION_9 9 +#define SAVE_GAME_VERSION_10 10 +#define SAVE_GAME_VERSION_11 11 +#define SAVE_GAME_VERSION_12 12 +#define SAVE_GAME_VERSION_13 13 +#define SAVE_GAME_VERSION SAVE_GAME_VERSION_13 + +#if (defined(WIN32) && !defined(SDL)) +extern HANDLE mapROM; // shared memory handles +extern HANDLE mapWORKRAM; +extern HANDLE mapBIOS; +extern HANDLE mapIRAM; +extern HANDLE mapPALETTERAM; +extern HANDLE mapVRAM; +extern HANDLE mapOAM; +extern HANDLE mapPIX; +extern HANDLE mapIOMEM; +#endif + +/* +extern reg_pair reg[45]; +extern u8 biosProtected[4]; + +extern bool8 N_FLAG; +extern bool8 Z_FLAG; +extern bool8 C_FLAG; +extern bool8 V_FLAG; +extern bool8 armIrqEnable; +extern bool8 armState; +extern int32 armMode; +*/ +extern void (*cpuSaveGameFunc)(u32, u8); + +extern bool8 freezeWorkRAM[0x40000]; +extern bool8 freezeInternalRAM[0x8000]; +extern bool CPUReadGSASnapshot(const char *); +extern bool CPUWriteGSASnapshot(const char *, const char *, const char *, const char *); +extern bool CPUWriteBatteryFile(const char *); +extern bool CPUReadBatteryFile(const char *); +extern bool CPUWriteBatteryToStream(gzFile); +extern bool CPUReadBatteryFromStream(gzFile); +extern bool CPUExportEepromFile(const char *); +extern bool CPUImportEepromFile(const char *); +extern bool CPUWritePNGFile(const char *); +extern bool CPUWriteBMPFile(const char *); +extern void CPUCleanUp(); +extern void CPUUpdateRender(); +extern void CPUUpdateRenderBuffers(bool force); +extern bool CPUReadMemState(char *, int); +extern bool CPUReadState(const char *); +extern bool CPUWriteMemState(char *, int); +extern bool CPUWriteState(const char *); +extern bool CPUReadStateFromStream(gzFile); +extern bool CPUWriteStateToStream(gzFile); +extern int CPULoadRom(const char *); +extern void CPUUpdateRegister(u32, u16); +extern void CPUWriteHalfWord(u32, u16); +extern void CPUWriteByte(u32, u8); +extern bool CPULoadBios(const char *, bool); +extern void CPUInit(); +extern void CPUReset(bool userReset = false); +extern void CPULoop(int); +extern void CPUCheckDMA(int, int); +#ifdef PROFILING +extern void cpuProfil(char *buffer, int, u32, int); +extern void cpuEnableProfiling(int hz); +#endif + +extern struct EmulatedSystem GBASystem; +extern struct EmulatedSystemCounters &GBASystemCounters; + +#define R13_IRQ 18 +#define R14_IRQ 19 +#define SPSR_IRQ 20 +#define R13_USR 26 +#define R14_USR 27 +#define R13_SVC 28 +#define R14_SVC 29 +#define SPSR_SVC 30 +#define R13_ABT 31 +#define R14_ABT 32 +#define SPSR_ABT 33 +#define R13_UND 34 +#define R14_UND 35 +#define SPSR_UND 36 +#define R8_FIQ 37 +#define R9_FIQ 38 +#define R10_FIQ 39 +#define R11_FIQ 40 +#define R12_FIQ 41 +#define R13_FIQ 42 +#define R14_FIQ 43 +#define SPSR_FIQ 44 + +#endif // VBA_GBA_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBACheats.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBACheats.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1822 @@ +#include +#include +#include + +#include "../common/System.h" +#include "../common/Util.h" +#include "../NLS.h" +#include "GBACheats.h" +#include "GBA.h" +#include "GBAinline.h" +#include "GBAGlobals.h" + +/** + * Gameshark code types: + * + * NNNNNNNN 001DC0DE - ID code for the game (game 4 character name) from ROM + * DEADFACE XXXXXXXX - changes decryption seeds + * 0AAAAAAA 000000YY - 8-bit constant write + * 1AAAAAAA 0000YYYY - 16-bit constant write + * 2AAAAAAA YYYYYYYY - 32-bit constant write + * 3AAAAAAA YYYYYYYY - ?? + * 6AAAAAAA 0000YYYY - 16-bit ROM Patch (address >> 1) + * 6AAAAAAA 1000YYYY - 16-bit ROM Patch ? (address >> 1) + * 6AAAAAAA 2000YYYY - 16-bit ROM Patch ? (address >> 1) + * 8A1AAAAA 000000YY - 8-bit button write + * 8A2AAAAA 0000YYYY - 16-bit button write + * 8A3AAAAA YYYYYYYY - 32-bit button write + * 80F00000 0000YYYY - button slow motion + * DAAAAAAA 0000YYYY - if address contains 16-bit value enable next code + * FAAAAAAA 0000YYYY - Master code function + * + * CodeBreaker codes types: + * + * 0000AAAA 000Y - Game CRC (Y are flags: 8 - CRC, 2 - DI) + * 1AAAAAAA YYYY - Master Code function (store address at ((YYYY << 0x16) + * + 0x08000100)) + * 2AAAAAAA YYYY - 16-bit or + * 3AAAAAAA YYYY - 8-bit constant write + * 4AAAAAAA YYYY - Slide code + * XXXXCCCC IIII (C is count and I is address increment, X is value incr.) + * 5AAAAAAA CCCC - Super code (Write bytes to address, CCCC is count) + * BBBBBBBB BBBB + * 6AAAAAAA YYYY - 16-bit and + * 7AAAAAAA YYYY - if address contains 16-bit value enable next code + * 8AAAAAAA YYYY - 16-bit constant write + * 9AAAAAAA YYYY - change decryption (when first code only?) + * AAAAAAAA YYYY - if address does not contain 16-bit value enable next code + * BAAAAAAA YYYY - if 16-bit < YYYY + * CAAAAAAA YYYY - if 16-bit > YYYY + * D0000020 YYYY - if button keys equal value enable next code + * EAAAAAAA YYYY - increase value stored in address + */ +#define UNKNOWN_CODE -1 +#define INT_8_BIT_WRITE 0 +#define INT_16_BIT_WRITE 1 +#define INT_32_BIT_WRITE 2 +#define GSA_16_BIT_ROM_PATCH 3 +#define GSA_8_BIT_GS_WRITE 4 +#define GSA_16_BIT_GS_WRITE 5 +#define GSA_32_BIT_GS_WRITE 6 +#define CBA_IF_KEYS_PRESSED 7 +#define CBA_IF_TRUE 8 +#define CBA_SLIDE_CODE 9 +#define CBA_IF_FALSE 10 +#define CBA_AND 11 +#define GSA_8_BIT_GS_WRITE2 12 +#define GSA_16_BIT_GS_WRITE2 13 +#define GSA_32_BIT_GS_WRITE2 14 +#define GSA_16_BIT_ROM_PATCH2 15 +#define GSA_8_BIT_SLIDE 16 +#define GSA_16_BIT_SLIDE 17 +#define GSA_32_BIT_SLIDE 18 +#define GSA_8_BIT_IF_TRUE 19 +#define GSA_32_BIT_IF_TRUE 20 +#define GSA_8_BIT_IF_FALSE 21 +#define GSA_32_BIT_IF_FALSE 22 +#define GSA_8_BIT_FILL 23 +#define GSA_16_BIT_FILL 24 +#define GSA_8_BIT_IF_TRUE2 25 +#define GSA_16_BIT_IF_TRUE2 26 +#define GSA_32_BIT_IF_TRUE2 27 +#define GSA_8_BIT_IF_FALSE2 28 +#define GSA_16_BIT_IF_FALSE2 29 +#define GSA_32_BIT_IF_FALSE2 30 +#define GSA_SLOWDOWN 31 +#define CBA_ADD 32 +#define CBA_OR 33 +#define CBA_LT 34 +#define CBA_GT 35 +#define CBA_SUPER 36 + +CheatsData cheatsList[100]; +int cheatsNumber = 0; + +u8 cheatsCBASeedBuffer[0x30]; +u32 cheatsCBASeed[4]; +u32 cheatsCBATemporaryValue = 0; +u16 cheatsCBATable[256]; +bool cheatsCBATableGenerated = false; + +u8 cheatsCBACurrentSeed[12] = { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 +}; + +#define CHEAT_IS_HEX(a) (((a) >= 'A' && (a) <= 'F') || ((a) >= '0' && (a) <= '9')) + +#define CHEAT_PATCH_ROM_16BIT(a, v) \ + WRITE16LE(((u16 *)&rom[(a) & 0x1ffffff]), v); + +static bool isMultilineWithData(int i) +{ + // we consider it a multiline code if it has more than one line of data + // otherwise, it can still be considered a single code + if (i < cheatsNumber && i >= 0) + switch (cheatsList[i].size) + { + case INT_8_BIT_WRITE: + case INT_16_BIT_WRITE: + case INT_32_BIT_WRITE: + case GSA_16_BIT_ROM_PATCH: + case GSA_8_BIT_GS_WRITE: + case GSA_16_BIT_GS_WRITE: + case GSA_32_BIT_GS_WRITE: + case CBA_AND: + case CBA_IF_KEYS_PRESSED: + case CBA_IF_TRUE: + case CBA_IF_FALSE: + case GSA_8_BIT_IF_TRUE: + case GSA_32_BIT_IF_TRUE: + case GSA_8_BIT_IF_FALSE: + case GSA_32_BIT_IF_FALSE: + case GSA_8_BIT_FILL: + case GSA_16_BIT_FILL: + case GSA_8_BIT_IF_TRUE2: + case GSA_16_BIT_IF_TRUE2: + case GSA_32_BIT_IF_TRUE2: + case GSA_8_BIT_IF_FALSE2: + case GSA_16_BIT_IF_FALSE2: + case GSA_32_BIT_IF_FALSE2: + case GSA_SLOWDOWN: + case CBA_ADD: + case CBA_OR: + return false; + // the codes below have two lines of data + case CBA_SLIDE_CODE: + case GSA_8_BIT_GS_WRITE2: + case GSA_16_BIT_GS_WRITE2: + case GSA_32_BIT_GS_WRITE2: + case GSA_16_BIT_ROM_PATCH2: + case GSA_8_BIT_SLIDE: + case GSA_16_BIT_SLIDE: + case GSA_32_BIT_SLIDE: + case CBA_LT: + case CBA_GT: + case CBA_SUPER: + return true; + } + return false; +} + +static int getCodeLength(int num) +{ + if (num >= cheatsNumber || num < 0) + return 1; + + // this is for all the codes that are true multiline + switch (cheatsList[num].size) + { + case INT_8_BIT_WRITE: + case INT_16_BIT_WRITE: + case INT_32_BIT_WRITE: + case GSA_16_BIT_ROM_PATCH: + case GSA_8_BIT_GS_WRITE: + case GSA_16_BIT_GS_WRITE: + case GSA_32_BIT_GS_WRITE: + case CBA_AND: + case GSA_8_BIT_FILL: + case GSA_16_BIT_FILL: + case GSA_SLOWDOWN: + case CBA_ADD: + case CBA_OR: + return 1; + case CBA_IF_KEYS_PRESSED: + case CBA_IF_TRUE: + case CBA_IF_FALSE: + case CBA_SLIDE_CODE: + case GSA_8_BIT_GS_WRITE2: + case GSA_16_BIT_GS_WRITE2: + case GSA_32_BIT_GS_WRITE2: + case GSA_16_BIT_ROM_PATCH2: + case GSA_8_BIT_SLIDE: + case GSA_16_BIT_SLIDE: + case GSA_32_BIT_SLIDE: + case GSA_8_BIT_IF_TRUE: + case GSA_32_BIT_IF_TRUE: + case GSA_8_BIT_IF_FALSE: + case GSA_32_BIT_IF_FALSE: + case CBA_LT: + case CBA_GT: + return 2; + case GSA_8_BIT_IF_TRUE2: + case GSA_16_BIT_IF_TRUE2: + case GSA_32_BIT_IF_TRUE2: + case GSA_8_BIT_IF_FALSE2: + case GSA_16_BIT_IF_FALSE2: + case GSA_32_BIT_IF_FALSE2: + return 3; + case CBA_SUPER: + return (cheatsList[num].value+5)/6; + } + return 1; +} + +int cheatsCheckKeys(u32 keys, u32 extended) +{ + int ticks = 0; + for (int i = 0; i < cheatsNumber; i++) + { + if (!cheatsList[i].enabled) + { + // make sure we skip other lines in this code + i += getCodeLength(i)-1; + continue; + } + switch (cheatsList[i].size) + { + case INT_8_BIT_WRITE: + CPUWriteByte(cheatsList[i].address, cheatsList[i].value); + break; + case INT_16_BIT_WRITE: + CPUWriteHalfWord(cheatsList[i].address, cheatsList[i].value); + break; + case INT_32_BIT_WRITE: + CPUWriteMemory(cheatsList[i].address, cheatsList[i].value); + break; + case GSA_16_BIT_ROM_PATCH: + if ((cheatsList[i].status & 1) == 0) + { + if (CPUReadHalfWord(cheatsList[i].address) != cheatsList[i].value) + { + cheatsList[i].oldValue = CPUReadHalfWord(cheatsList[i].address); + cheatsList[i].status |= 1; + CHEAT_PATCH_ROM_16BIT(cheatsList[i].address, cheatsList[i].value); + } + } + break; + case GSA_8_BIT_GS_WRITE: + if (extended & 4) + { + CPUWriteByte(cheatsList[i].address, cheatsList[i].value); + } + break; + case GSA_16_BIT_GS_WRITE: + if (extended & 4) + { + CPUWriteHalfWord(cheatsList[i].address, cheatsList[i].value); + } + break; + case GSA_32_BIT_GS_WRITE: + if (extended & 4) + { + CPUWriteMemory(cheatsList[i].address, cheatsList[i].value); + } + break; + case CBA_IF_KEYS_PRESSED: + { + u16 value = cheatsList[i].value; + u32 addr = cheatsList[i].address; + if ((addr & 0x30) == 0x20) + { + if ((keys & value) != value) + { + i++; + } + } + else if ((addr & 0x30) == 0x10) + { + if ((keys & value) == value) + { + i++; + } + } + break; + } + case CBA_IF_TRUE: + if (CPUReadHalfWord(cheatsList[i].address) != cheatsList[i].value) + { + i++; + } + break; + case CBA_SLIDE_CODE: + { + u32 address = cheatsList[i].address; + u16 value = cheatsList[i].value; + i++; + if (i < cheatsNumber) + { + int count = (cheatsList[i].address & 0xFFFF); + u16 vinc = (cheatsList[i].address >> 16) & 0xFFFF; + int inc = cheatsList[i].value; + + for (int x = 0; x < count; x++) + { + CPUWriteHalfWord(address, value); + address += inc; + value += vinc; + } + } + break; + } + case CBA_IF_FALSE: + if (CPUReadHalfWord(cheatsList[i].address) == cheatsList[i].value) + { + i++; + } + break; + case CBA_AND: + CPUWriteHalfWord(cheatsList[i].address, + CPUReadHalfWord(cheatsList[i].address) & + cheatsList[i].value); + break; + case GSA_8_BIT_GS_WRITE2: + i++; + if (i < cheatsNumber) + { + if (extended & 4) + { + CPUWriteByte(cheatsList[i-1].value, cheatsList[i].address); + } + } + break; + case GSA_16_BIT_GS_WRITE2: + i++; + if (i < cheatsNumber) + { + if (extended & 4) + { + CPUWriteHalfWord(cheatsList[i-1].value, cheatsList[i].address); + } + } + break; + case GSA_32_BIT_GS_WRITE2: + i++; + if (i < cheatsNumber) + { + if (extended & 4) + { + CPUWriteMemory(cheatsList[i-1].value, cheatsList[i].address); + } + } + break; + case GSA_16_BIT_ROM_PATCH2: + i++; + if (i < cheatsNumber) + { + if ((cheatsList[i-1].status & 1) == 0) + { + u32 addr = ((cheatsList[i-1].value & 0x00FFFFFF) << 1) + 0x8000000; + if (CPUReadHalfWord(addr) != (cheatsList[i].address & 0xFFFF)) + { + cheatsList[i-1].oldValue = CPUReadHalfWord(addr); + cheatsList[i-1].status |= 1; + CHEAT_PATCH_ROM_16BIT(addr, cheatsList[i].address & 0xFFFF); + } + } + } + break; + case GSA_8_BIT_SLIDE: + i++; + if (i < cheatsNumber) + { + u32 addr = cheatsList[i-1].value; + u8 value = cheatsList[i].address; + int vinc = (cheatsList[i].value >> 24) & 255; + int count = (cheatsList[i].value >> 16) & 255; + int ainc = (cheatsList[i].value & 0xffff); + while (count > 0) + { + CPUWriteByte(addr, value); + value += vinc; + addr += ainc; + count--; + } + } + break; + case GSA_16_BIT_SLIDE: + i++; + if (i < cheatsNumber) + { + u32 addr = cheatsList[i-1].value; + u16 value = cheatsList[i].address; + int vinc = (cheatsList[i].value >> 24) & 255; + int count = (cheatsList[i].value >> 16) & 255; + int ainc = (cheatsList[i].value & 0xffff)*2; + while (count > 0) + { + CPUWriteHalfWord(addr, value); + value += vinc; + addr += ainc; + count--; + } + } + break; + case GSA_32_BIT_SLIDE: + i++; + if (i < cheatsNumber) + { + u32 addr = cheatsList[i-1].value; + u32 value = cheatsList[i].address; + int vinc = (cheatsList[i].value >> 24) & 255; + int count = (cheatsList[i].value >> 16) & 255; + int ainc = (cheatsList[i].value & 0xffff)*4; + while (count > 0) + { + CPUWriteMemory(addr, value); + value += vinc; + addr += ainc; + count--; + } + } + break; + case GSA_8_BIT_IF_TRUE: + if (CPUReadByte(cheatsList[i].address) != cheatsList[i].value) + { + i++; + } + break; + case GSA_32_BIT_IF_TRUE: + if (CPUReadMemory(cheatsList[i].address) != cheatsList[i].value) + { + i++; + } + break; + case GSA_8_BIT_IF_FALSE: + if (CPUReadByte(cheatsList[i].address) == cheatsList[i].value) + { + i++; + } + break; + case GSA_32_BIT_IF_FALSE: + if (CPUReadMemory(cheatsList[i].address) == cheatsList[i].value) + { + i++; + } + break; + case GSA_8_BIT_FILL: + { + u32 addr = cheatsList[i].address; + u8 v = cheatsList[i].value & 0xff; + u32 end = addr + (cheatsList[i].value >> 8); + do + { + CPUWriteByte(addr, v); + addr++; + } + while (addr <= end); + break; + } + case GSA_16_BIT_FILL: + { + u32 addr = cheatsList[i].address; + u16 v = cheatsList[i].value & 0xffff; + u32 end = addr + ((cheatsList[i].value >> 16) << 1); + do + { + CPUWriteHalfWord(addr, v); + addr += 2; + } + while (addr <= end); + break; + } + case GSA_8_BIT_IF_TRUE2: + if (CPUReadByte(cheatsList[i].address) != cheatsList[i].value) + { + i += 2; + } + break; + case GSA_16_BIT_IF_TRUE2: + if (CPUReadHalfWord(cheatsList[i].address) != cheatsList[i].value) + { + i += 2; + } + break; + case GSA_32_BIT_IF_TRUE2: + if (CPUReadMemory(cheatsList[i].address) != cheatsList[i].value) + { + i += 2; + } + break; + case GSA_8_BIT_IF_FALSE2: + if (CPUReadByte(cheatsList[i].address) == cheatsList[i].value) + { + i += 2; + } + break; + case GSA_16_BIT_IF_FALSE2: + if (CPUReadHalfWord(cheatsList[i].address) == cheatsList[i].value) + { + i += 2; + } + break; + case GSA_32_BIT_IF_FALSE2: + if (CPUReadMemory(cheatsList[i].address) == cheatsList[i].value) + { + i += 2; + } + break; + case GSA_SLOWDOWN: + // check if button was pressed and released, if so toggle our state + if ((cheatsList[i].status & 4) && !(extended & 4)) + cheatsList[i].status ^= 1; + if (extended & 4) + cheatsList[i].status |= 4; + else + cheatsList[i].status &= ~4; + + if (cheatsList[i].status & 1) + ticks += 2*256*((cheatsList[i].value >> 8) & 255); + break; + case CBA_ADD: + CPUWriteHalfWord(cheatsList[i].address, + CPUReadHalfWord(cheatsList[i].address) + + (u16)cheatsList[i].value); + break; + case CBA_OR: + CPUWriteHalfWord(cheatsList[i].address, + CPUReadHalfWord(cheatsList[i].address) | + cheatsList[i].value); + break; + case CBA_LT: + if (CPUReadHalfWord(cheatsList[i].address) >= cheatsList[i].value) + i++; + break; + case CBA_GT: + if (CPUReadHalfWord(cheatsList[i].address) <= cheatsList[i].value) + i++; + break; + case CBA_SUPER: + { + int count = 2*cheatsList[i].value; + u32 address = cheatsList[i].address; + for (int x = 0; x < count; x++) + { + u8 b; + int res = x % 6; + if (res < 4) + b = (cheatsList[i].address >> (24-8*res)) & 0xFF; + else + b = (cheatsList[i].value >> (8 - 8*(res-4))) & 0x0FF; + CPUWriteByte(address, b); + address++; + if (x && !res) + i++; + } + if (count % 6) + i++; + break; + } + } + } + return ticks; +} + +void cheatsAdd(const char *codeStr, + const char *desc, + u32 address, + u32 value, + int code, + int size) +{ + if (cheatsNumber < 100) + { + int x = cheatsNumber; + cheatsList[x].code = code; + cheatsList[x].size = size; + cheatsList[x].address = address; + cheatsList[x].value = value; + strcpy(cheatsList[x].codestring, codeStr); + strcpy(cheatsList[x].desc, desc); + cheatsList[x].enabled = true; + cheatsList[x].status = 0; + + // we only store the old value for this simple codes. ROM patching + // is taken care when it actually patches the ROM + switch (cheatsList[x].size) + { + case INT_8_BIT_WRITE: + cheatsList[x].oldValue = CPUReadByte(address); + break; + case INT_16_BIT_WRITE: + cheatsList[x].oldValue = CPUReadHalfWord(address); + break; + case INT_32_BIT_WRITE: + cheatsList[x].oldValue = CPUReadMemory(address); + break; + } + cheatsNumber++; + } +} + +void cheatsDelete(int number, bool restore) +{ + if (number < cheatsNumber && number >= 0) + { + int x = number; + + if (restore) + { + switch (cheatsList[x].size) + { + case INT_8_BIT_WRITE: + CPUWriteByte(cheatsList[x].address, (u8)cheatsList[x].oldValue); + break; + case INT_16_BIT_WRITE: + CPUWriteHalfWord(cheatsList[x].address, (u16)cheatsList[x].oldValue); + break; + case INT_32_BIT_WRITE: + CPUWriteMemory(cheatsList[x].address, cheatsList[x].oldValue); + break; + case GSA_16_BIT_ROM_PATCH: + if (cheatsList[x].status & 1) + { + cheatsList[x].status &= ~1; + CHEAT_PATCH_ROM_16BIT(cheatsList[x].address, + cheatsList[x].oldValue); + } + break; + case GSA_16_BIT_ROM_PATCH2: + if (cheatsList[x].status & 1) + { + cheatsList[x].status &= ~1; + CHEAT_PATCH_ROM_16BIT(((cheatsList[x].value & 0x00FFFFFF) << 1)+ + 0x8000000, + cheatsList[x].oldValue); + } + break; + } + } + if ((x+1) < cheatsNumber) + { + memcpy(&cheatsList[x], &cheatsList[x+1], sizeof(CheatsData)* + (cheatsNumber-x-1)); + } + cheatsNumber--; + } +} + +void cheatsDeleteAll(bool restore) +{ + for (int i = cheatsNumber-1; i >= 0; i--) + { + cheatsDelete(i, restore); + } +} + +void cheatsEnable(int i) +{ + if (i >= 0 && i < cheatsNumber) + { + cheatsList[i].enabled = true; + } +} + +void cheatsDisable(int i) +{ + if (i >= 0 && i < cheatsNumber) + { + switch (cheatsList[i].size) + { + case GSA_16_BIT_ROM_PATCH: + if (cheatsList[i].status & 1) + { + cheatsList[i].status &= ~1; + CHEAT_PATCH_ROM_16BIT(cheatsList[i].address, + cheatsList[i].oldValue); + } + break; + case GSA_16_BIT_ROM_PATCH2: + if (cheatsList[i].status & 1) + { + cheatsList[i].status &= ~1; + CHEAT_PATCH_ROM_16BIT(((cheatsList[i].value & 0x00FFFFFF) << 1)+ + 0x8000000, + cheatsList[i].oldValue); + } + break; + } + cheatsList[i].enabled = false; + } +} + +bool cheatsVerifyCheatCode(const char *code, const char *desc) +{ + int len = strlen(code); + if (len != 11 && len != 13 && len != 17) + { + systemMessage(MSG_INVALID_CHEAT_CODE, N_("Invalid cheat code '%s'"), code); + return false; + } + + if (code[8] != ':') + { + systemMessage(MSG_INVALID_CHEAT_CODE, N_("Invalid cheat code '%s'"), code); + return false; + } + + int i; + for (i = 0; i < 8; i++) + { + if (!CHEAT_IS_HEX(code[i])) + { + // wrong cheat + systemMessage(MSG_INVALID_CHEAT_CODE, + N_("Invalid cheat code '%s'"), code); + return false; + } + } + for (i = 9; i < len; i++) + { + if (!CHEAT_IS_HEX(code[i])) + { + // wrong cheat + systemMessage(MSG_INVALID_CHEAT_CODE, + N_("Invalid cheat code '%s'"), code); + return false; + } + } + + u32 address = 0; + u32 value = 0; + + char buffer[10]; + strncpy(buffer, code, 8); + buffer[8] = 0; + sscanf(buffer, "%x", &address); + + switch (address >> 24) + { + case 2: + case 3: + break; + default: + systemMessage(MSG_INVALID_CHEAT_CODE_ADDRESS, + N_("Invalid cheat code address: %08x"), + address); + return false; + } + + strncpy(buffer, &code[9], 8); + sscanf(buffer, "%x", &value); + int type = 0; + if (len == 13) + type = 1; + if (len == 17) + type = 2; + cheatsAdd(code, desc, address, value, type, type); + return true; +} + +void cheatsAddCheatCode(const char *code, const char *desc) +{ + cheatsVerifyCheatCode(code, desc); +} + +void cheatsDecryptGSACode(u32& address, u32& value, bool v3) +{ + u32 rollingseed = 0xC6EF3720; + u32 seeds_v1[] = { 0x09F4FBBD, 0x9681884A, 0x352027E9, 0xF3DEE5A7 }; + u32 seeds_v3[] = { 0x7AA9648F, 0x7FAE6994, 0xC0EFAAD5, 0x42712C57 }; + u32 *seeds = v3 ? seeds_v3 : seeds_v1; + + int bitsleft = 32; + while (bitsleft > 0) + { + value -= ((((address << 4) + seeds[2]) ^ (address + rollingseed)) ^ + ((address >> 5) + seeds[3])); + address -= ((((value << 4) + seeds[0]) ^ (value + rollingseed)) ^ + ((value >> 5) + seeds[1])); + rollingseed -= 0x9E3779B9; + bitsleft--; + } +} + +void cheatsAddGSACode(const char *code, const char *desc, bool v3) +{ + if (strlen(code) != 16) + { + // wrong cheat + systemMessage(MSG_INVALID_GSA_CODE, + N_("Invalid GSA code. Format is XXXXXXXXYYYYYYYY")); + return; + } + + int i; + for (i = 0; i < 16; i++) + { + if (!CHEAT_IS_HEX(code[i])) + { + // wrong cheat + systemMessage(MSG_INVALID_GSA_CODE, + N_("Invalid GSA code. Format is XXXXXXXXYYYYYYYY")); + return; + } + } + + char buffer[10]; + strncpy(buffer, code, 8); + buffer[8] = 0; + u32 address; + sscanf(buffer, "%x", &address); + strncpy(buffer, &code[8], 8); + buffer[8] = 0; + u32 value; + sscanf(buffer, "%x", &value); + + cheatsDecryptGSACode(address, value, v3); + + if (value == 0x1DC0DE) + { + u32 gamecode = READ32LE(((u32 *)&rom[0xac])); + if (gamecode != address) + { + char buffer[5]; + *((u32 *)buffer) = address; + buffer[4] = 0; + char buffer2[5]; + *((u32 *)buffer2) = READ32LE(((u32 *)&rom[0xac])); + buffer2[4] = 0; + systemMessage(MSG_GBA_CODE_WARNING, + N_("Warning: cheats are for game %s. Current game is %s.\nCodes may not work correctly."), + buffer, buffer2); + } + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, v3 ? 257 : 256, + UNKNOWN_CODE); + return; + } + if (isMultilineWithData(cheatsNumber-1)) + { + cheatsAdd(code, desc, address, value, v3 ? 257 : 256, UNKNOWN_CODE); + return; + } + if (v3) + { + int type = (address >> 25) & 127; + u32 addr = (address & 0x00F00000) << 4 | (address & 0x0003FFFF); + switch (type) + { + case 0x00: + if (address == 0) + { + type = (value >> 25) & 127; + addr = (value & 0x00F00000) << 4 | (value & 0x0003FFFF); + switch (type) + { + case 0x04: + cheatsAdd(code, desc, 0, value & 0x00FFFFFF, 257, GSA_SLOWDOWN); + break; + case 0x08: + cheatsAdd(code, desc, 0, addr, 257, GSA_8_BIT_GS_WRITE2); + break; + case 0x09: + cheatsAdd(code, desc, 0, addr, 257, GSA_16_BIT_GS_WRITE2); + break; + case 0x0a: + cheatsAdd(code, desc, 0, addr, 257, GSA_32_BIT_GS_WRITE2); + break; + case 0x0c: + case 0x0d: + case 0x0e: + case 0x0f: + cheatsAdd(code, desc, 0, value & 0x00FFFFFF, 257, GSA_16_BIT_ROM_PATCH2); + break; + case 0x40: + cheatsAdd(code, desc, 0, addr, 257, GSA_8_BIT_SLIDE); + break; + case 0x41: + cheatsAdd(code, desc, 0, addr, 257, GSA_16_BIT_SLIDE); + break; + case 0x42: + cheatsAdd(code, desc, 0, addr, 257, GSA_32_BIT_SLIDE); + break; + default: + cheatsAdd(code, desc, address, value, 257, UNKNOWN_CODE); + break; + } + } + else + cheatsAdd(code, desc, addr, value, 257, GSA_8_BIT_FILL); + break; + case 0x01: + cheatsAdd(code, desc, addr, value, 257, GSA_16_BIT_FILL); + break; + case 0x02: + cheatsAdd(code, desc, addr, value, 257, INT_32_BIT_WRITE); + break; + case 0x04: + cheatsAdd(code, desc, addr, value, 257, GSA_8_BIT_IF_TRUE); + break; + case 0x05: + cheatsAdd(code, desc, addr, value, 257, CBA_IF_TRUE); + break; + case 0x06: + cheatsAdd(code, desc, addr, value, 257, GSA_32_BIT_IF_TRUE); + break; + case 0x08: + cheatsAdd(code, desc, addr, value, 257, GSA_8_BIT_IF_FALSE); + break; + case 0x09: + cheatsAdd(code, desc, addr, value, 257, CBA_IF_FALSE); + break; + case 0x0a: + cheatsAdd(code, desc, addr, value, 257, GSA_32_BIT_IF_FALSE); + break; + case 0x24: + cheatsAdd(code, desc, addr, value, 257, GSA_8_BIT_IF_TRUE2); + break; + case 0x25: + cheatsAdd(code, desc, addr, value, 257, GSA_16_BIT_IF_TRUE2); + break; + case 0x26: + cheatsAdd(code, desc, addr, value, 257, GSA_32_BIT_IF_TRUE2); + break; + case 0x28: + cheatsAdd(code, desc, addr, value, 257, GSA_8_BIT_IF_FALSE2); + break; + case 0x29: + cheatsAdd(code, desc, addr, value, 257, GSA_16_BIT_IF_FALSE2); + break; + case 0x2a: + cheatsAdd(code, desc, addr, value, 257, GSA_32_BIT_IF_FALSE2); + break; + default: + cheatsAdd(code, desc, address, value, 257, UNKNOWN_CODE); + break; + } + } + else + { + int type = (address >> 28) & 15; + switch (type) + { + case 0: + case 1: + case 2: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 256, type); + break; + case 6: + address <<= 1; + type = (address >> 28) & 15; + if (type == 0x0c) + { + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 256, + GSA_16_BIT_ROM_PATCH); + break; + } + // unsupported code + cheatsAdd(code, desc, address, value, 256, + UNKNOWN_CODE); + break; + case 8: + switch ((address >> 20) & 15) + { + case 1: + cheatsAdd(code, desc, address & 0x0F0FFFFF, value, 256, + GSA_8_BIT_GS_WRITE); + break; + case 2: + cheatsAdd(code, desc, address & 0x0F0FFFFF, value, 256, + GSA_16_BIT_GS_WRITE); + break; + case 3: + cheatsAdd(code, desc, address & 0x0F0FFFFF, value, 256, + GSA_32_BIT_GS_WRITE); + case 15: + cheatsAdd(code, desc, 0, value & 0xFF00, 256, GSA_SLOWDOWN); + break; + default: + // unsupported code + cheatsAdd(code, desc, address, value, 256, + UNKNOWN_CODE); + break; + } + break; + case 0x0d: + if (address != 0xDEADFACE) + { + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 256, + CBA_IF_TRUE); + } + else + cheatsAdd(code, desc, address, value, 256, + UNKNOWN_CODE); + break; + default: + // unsupported code + cheatsAdd(code, desc, address, value, 256, + UNKNOWN_CODE); + break; + } + } +} + +bool cheatsImportGSACodeFile(const char *name, int game, bool v3) +{ + FILE *f = fopen(name, "rb"); + if (!f) + return false; + + int games = 0; + int len = 0; + fseek(f, 0x1e, SEEK_CUR); + fread(&games, 1, 4, f); + bool found = false; + int g = 0; + while (games > 0) + { + if (g == game) + { + found = true; + break; + } + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + int codes = 0; + fread(&codes, 1, 4, f); + while (codes > 0) + { + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + fseek(f, 8, SEEK_CUR); + fread(&len, 1, 4, f); + fseek(f, len*12, SEEK_CUR); + codes--; + } + games--; + g++; + } + if (found) + { + char desc[256]; + char code[17]; + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + int codes = 0; + fread(&codes, 1, 4, f); + while (codes > 0) + { + fread(&len, 1, 4, f); + fread(desc, 1, len, f); + desc[len] = 0; + desc[31] = 0; + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + fseek(f, 4, SEEK_CUR); + fread(&len, 1, 4, f); + while (len) + { + fseek(f, 4, SEEK_CUR); + fread(code, 1, 8, f); + fseek(f, 4, SEEK_CUR); + fread(&code[8], 1, 8, f); + code[16] = 0; + cheatsAddGSACode(code, desc, v3); + len -= 2; + } + codes--; + } + } + fclose(f); + return false; +} + +void cheatsCBAReverseArray(u8 *array, u8 *dest) +{ + dest[0] = array[3]; + dest[1] = array[2]; + dest[2] = array[1]; + dest[3] = array[0]; + dest[4] = array[5]; + dest[5] = array[4]; +} + +void chatsCBAScramble(u8 *array, int count, u8 b) +{ + u8 *x = array + (count >> 3); + u8 *y = array + (b >> 3); + u32 z = *x & (1 << (count & 7)); + u32 x0 = (*x & (~(1 << (count & 7)))); + if (z != 0) + z = 1; + if ((*y & (1 << (b & 7))) != 0) + x0 |= (1 << (count & 7)); + *x = x0; + u32 temp = *y & (~(1 << (b & 7))); + if (z != 0) + temp |= (1 << (b & 7)); + *y = temp; +} + +u32 cheatsCBAGetValue(u8 *array) +{ + return array[0] | array[1]<<8 | array[2] << 16 | array[3]<<24; +} + +u16 cheatsCBAGetData(u8 *array) +{ + return array[4] | array[5]<<8; +} + +void cheatsCBAArrayToValue(u8 *array, u8 *dest) +{ + dest[0] = array[3]; + dest[1] = array[2]; + dest[2] = array[1]; + dest[3] = array[0]; + dest[4] = array[5]; + dest[5] = array[4]; +} + +void cheatsCBAParseSeedCode(u32 address, u32 value, u32 *array) +{ + array[0] = 1; + array[1] = value & 0xFF; + array[2] = (address >> 0x10) & 0xFF; + array[3] = (value >> 8) & 0xFF; + array[4] = (address >> 0x18) & 0x0F; + array[5] = address & 0xFFFF; + array[6] = address; + array[7] = value; +} + +u32 cheatsCBAEncWorker() +{ + u32 x = (cheatsCBATemporaryValue * 0x41c64e6d) + 0x3039; + u32 y = (x * 0x41c64e6d) + 0x3039; + u32 z = x >> 0x10; + x = ((y >> 0x10) & 0x7fff) << 0x0f; + z = (z << 0x1e) | x; + x = (y * 0x41c64e6d) + 0x3039; + cheatsCBATemporaryValue = x; + return z | ((x >> 0x10) & 0x7fff); +} + +#define ROR(v, s) \ + (((v) >> (s)) | (((v) & ((1 << (s))-1)) << (32 - (s)))) + +u32 cheatsCBACalcIndex(u32 x, u32 y) +{ + if (y != 0) + { + if (y == 1) + x = 0; + else if (x == y) + x = 0; + if (y < 1) + return x; + else if (x < y) + return x; + u32 x0 = 1; + + while (y < 0x10000000) + { + if (y < x) + { + y = y << 4; + x0 = x0 << 4; + } + else + break; + } + + while (y < 0x80000000) + { + if (y < x) + { + y = y << 1; + x0 = x0 << 1; + } + else + break; + } + +loop: + u32 z = 0; + if (x >= y) + x -= y; + if (x >= (y >> 1)) + { + x -= (y >> 1); + z |= ROR(x0, 1); + } + if (x >= (y >> 2)) + { + x -= (y >> 2); + z |= ROR(x0, 2); + } + if (x >= (y >> 3)) + { + x -= (y >> 3); + z |= ROR(x0, 3); + } + + u32 temp = x0; + + if (x != 0) + { + x0 = x0 >> 4; + if (x0 != 0) + { + y = y >> 4; + goto loop; + } + } + + z = z & 0xe0000000; + + if (z != 0) + { + if ((temp & 7) == 0) + return x; + } + else + return x; + + if ((z & ROR(temp, 3)) != 0) + x += y >> 3; + if ((z & ROR(temp, 2)) != 0) + x += y >> 2; + if ((z & ROR(temp, 1)) != 0) + x += y >> 1; + return x; + } + else + {} + // should not happen in the current code + return 0; +} + +void cheatsCBAUpdateSeedBuffer(u32 a, u8 *buffer, int count) +{ + int i; + for (i = 0; i < count; i++) + buffer[i] = i; + for (i = 0; (u32)i < a; i++) + { + u32 a = cheatsCBACalcIndex(cheatsCBAEncWorker(), count); + u32 b = cheatsCBACalcIndex(cheatsCBAEncWorker(), count); + u32 t = buffer[a]; + buffer[a] = buffer[b]; + buffer[b] = t; + } +} + +void cheatsCBAChangeEncryption(u32 *seed) +{ + int i; + + cheatsCBATemporaryValue = (seed[1] ^ 0x1111); + cheatsCBAUpdateSeedBuffer(0x50, cheatsCBASeedBuffer, 0x30); + cheatsCBATemporaryValue = 0x4efad1c3; + + for (i = 0; (u32)i < seed[4]; i++) + { + cheatsCBATemporaryValue = cheatsCBAEncWorker(); + } + cheatsCBASeed[2] = cheatsCBAEncWorker(); + cheatsCBASeed[3] = cheatsCBAEncWorker(); + + cheatsCBATemporaryValue = seed[3] ^ 0xf254; + + for (i = 0; (u32)i < seed[3]; i++) + { + cheatsCBATemporaryValue = cheatsCBAEncWorker(); + } + + cheatsCBASeed[0] = cheatsCBAEncWorker(); + cheatsCBASeed[1] = cheatsCBAEncWorker(); + + *((u32 *)&cheatsCBACurrentSeed[0]) = seed[6]; + *((u32 *)&cheatsCBACurrentSeed[4]) = seed[7]; + *((u32 *)&cheatsCBACurrentSeed[8]) = 0; +} + +u16 cheatsCBAGenValue(u32 x, u32 y, u32 z) +{ + y <<= 0x10; + z <<= 0x10; + x <<= 0x18; + u32 x0 = (int)y >> 0x10; + z = (int)z >> 0x10; + x = (int)x >> 0x10; + for (int i = 0; i < 8; i++) + { + u32 temp = z ^ x; + if ((int)temp >= 0) + { + temp = z << 0x11; + } + else + { + temp = z << 0x01; + temp ^= x0; + temp = temp << 0x10; + } + z = (int)temp >> 0x10; + temp = x << 0x11; + x = (int)temp >> 0x10; + } + return z & 0xffff; +} + +void cheatsCBAGenTable() +{ + for (int i = 0; i < 0x100; i++) + { + cheatsCBATable[i] = cheatsCBAGenValue(i, 0x1021, 0); + } + cheatsCBATableGenerated = true; +} + +u16 cheatsCBACalcCRC(u8 *rom, int count) +{ + u32 crc = 0xffffffff; + + if (count & 3) + { + // 0x08000EAE + } + else + { + count = (count >> 2) - 1; + if (count != -1) + { + while (count != -1) + { + crc = (((crc << 0x08) ^ cheatsCBATable[(((u32)crc << 0x10) >> 0x18) + ^ *rom++]) << 0x10) >> 0x10; + crc = (((crc << 0x08) ^ cheatsCBATable[(((u32)crc << 0x10) >> 0x18) + ^ *rom++]) << 0x10) >> 0x10; + crc = (((crc << 0x08) ^ cheatsCBATable[(((u32)crc << 0x10) >> 0x18) + ^ *rom++]) << 0x10) >> 0x10; + crc = (((crc << 0x08) ^ cheatsCBATable[(((u32)crc << 0x10) >> 0x18) + ^ *rom++]) << 0x10) >> 0x10; + count--; + } + } + } + return crc & 0xffff; +} + +void cheatsCBADecrypt(u8 *decrypt) +{ + u8 buffer[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + u8 *array = &buffer[1]; + + cheatsCBAReverseArray(decrypt, array); + + for (int count = 0x2f; count >= 0; count--) + { + chatsCBAScramble(array, count, cheatsCBASeedBuffer[count]); + } + cheatsCBAArrayToValue(array, decrypt); + *((u32 *)decrypt) = cheatsCBAGetValue(decrypt) ^ + cheatsCBASeed[0]; + *((u16 *)(decrypt+4)) = (cheatsCBAGetData(decrypt) ^ + cheatsCBASeed[1]) & 0xffff; + + cheatsCBAReverseArray(decrypt, array); + + u32 cs = cheatsCBAGetValue(cheatsCBACurrentSeed); + for (int i = 0; i <= 4; i++) + { + array[i] = ((cs >> 8) ^ array[i+1]) ^ array[i] ; + } + + array[5] = (cs >> 8) ^ array[5]; + + for (int j = 5; j >= 0; j--) + { + array[j] = (cs ^ array[j-1]) ^ array[j]; + } + + cheatsCBAArrayToValue(array, decrypt); + + *((u32 *)decrypt) = cheatsCBAGetValue(decrypt) + ^ cheatsCBASeed[2]; + *((u16 *)(decrypt+4)) = (cheatsCBAGetData(decrypt) + ^ cheatsCBASeed[3]) & 0xffff; +} + +int cheatsCBAGetCount() +{ + int count = 0; + for (int i = 0; i < cheatsNumber; i++) + { + if (cheatsList[i].code == 512) + count++; + } + return count; +} + +bool cheatsCBAShouldDecrypt() +{ + for (int i = 0; i < cheatsNumber; i++) + { + if (cheatsList[i].code == 512) + { + return (cheatsList[i].codestring[0] == '9'); + } + } + return false; +} + +void cheatsAddCBACode(const char *code, const char *desc) +{ + if (strlen(code) != 13) + { + // wrong cheat + systemMessage(MSG_INVALID_CBA_CODE, + N_("Invalid CBA code. Format is XXXXXXXX YYYY.")); + return; + } + + int i; + for (i = 0; i < 8; i++) + { + if (!CHEAT_IS_HEX(code[i])) + { + // wrong cheat + systemMessage(MSG_INVALID_CBA_CODE, + N_("Invalid CBA code. Format is XXXXXXXX YYYY.")); + return; + } + } + + if (code[8] != ' ') + { + systemMessage(MSG_INVALID_CBA_CODE, + N_("Invalid CBA code. Format is XXXXXXXX YYYY.")); + return; + } + + for (i = 9; i < 13; i++) + { + if (!CHEAT_IS_HEX(code[i])) + { + // wrong cheat + systemMessage(MSG_INVALID_CBA_CODE, + N_("Invalid CBA code. Format is XXXXXXXX YYYY.")); + return; + } + } + + char buffer[10]; + strncpy(buffer, code, 8); + buffer[8] = 0; + u32 address; + sscanf(buffer, "%x", &address); + strncpy(buffer, &code[9], 4); + buffer[4] = 0; + u32 value; + sscanf(buffer, "%x", &value); + + u8 array[8] = { + address &255, + (address >> 8) & 255, + (address >> 16) & 255, + (address >> 24) & 255, + (value & 255), + (value >> 8) & 255, + 0, + 0 + }; + + if (cheatsCBAGetCount() == 0 && + (address >> 28) == 9) + { + u32 seed[8]; + cheatsCBAParseSeedCode(address, value, seed); + cheatsCBAChangeEncryption(seed); + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, UNKNOWN_CODE); + } + else + { + if (cheatsCBAShouldDecrypt()) + cheatsCBADecrypt(array); + + address = READ32LE(((u32 *)array)); + value = READ16LE(((u16 *)&array[4])); + + int type = (address >> 28) & 15; + + if (isMultilineWithData(cheatsNumber-1)) + { + cheatsAdd(code, desc, address, value, 512, UNKNOWN_CODE); + return; + } + + switch (type) + { + case 0x00: + { + if (!cheatsCBATableGenerated) + cheatsCBAGenTable(); + u32 crc = cheatsCBACalcCRC(rom, 0x10000); + if (crc != address) + { + systemMessage(MSG_CBA_CODE_WARNING, + N_("Warning: Codes seem to be for a different game.\nCodes may not work correctly.")); + } + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + UNKNOWN_CODE); + break; + } + case 0x02: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_OR); + break; + case 0x03: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + INT_8_BIT_WRITE); + break; + case 0x04: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_SLIDE_CODE); + break; + case 0x05: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_SUPER); + break; + case 0x06: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_AND); + break; + case 0x07: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_IF_TRUE); + break; + case 0x08: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + INT_16_BIT_WRITE); + break; + case 0x0a: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_IF_FALSE); + break; + case 0x0b: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_LT); + break; + case 0x0c: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_GT); + break; + case 0x0d: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_IF_KEYS_PRESSED); + break; + case 0x0e: + cheatsAdd(code, desc, address & 0x0FFFFFFF, value, 512, + CBA_ADD); + break; + default: + // unsupported code + cheatsAdd(code, desc, address & 0xFFFFFFFF, value, 512, + UNKNOWN_CODE); + break; + } + } +} + +void cheatsSaveGame(gzFile file) +{ + utilWriteInt(file, cheatsNumber); + + utilGzWrite(file, cheatsList, sizeof(cheatsList)); +} + +void cheatsReadGame(gzFile file) +{ + cheatsNumber = 0; + + cheatsNumber = utilReadInt(file); + + utilGzRead(file, cheatsList, sizeof(cheatsList)); + + bool firstCodeBreaker = true; + + for (int i = 0; i < cheatsNumber; i++) + { + cheatsList[i].status = 0; + if (!cheatsList[i].codestring[0]) + { + switch (cheatsList[i].size) + { + case 0: + sprintf(cheatsList[i].codestring, "%08x:%02x", cheatsList[i].address, + cheatsList[i].value); + break; + case 1: + sprintf(cheatsList[i].codestring, "%08x:%04x", cheatsList[i].address, + cheatsList[i].value); + break; + case 2: + sprintf(cheatsList[i].codestring, "%08x:%08x", cheatsList[i].address, + cheatsList[i].value); + break; + } + } + + if (cheatsList[i].enabled) + { + cheatsEnable(i); + } + + if (cheatsList[i].code == 512 && firstCodeBreaker) + { + firstCodeBreaker = false; + char buffer[10]; + strncpy(buffer, cheatsList[i].codestring, 8); + buffer[8] = 0; + u32 address; + sscanf(buffer, "%x", &address); + if ((address >> 28) == 9) + { + strncpy(buffer, &cheatsList[i].codestring[9], 4); + buffer[4] = 0; + u32 value; + sscanf(buffer, "%x", &value); + + u32 seed[8]; + cheatsCBAParseSeedCode(address, value, seed); + cheatsCBAChangeEncryption(seed); + } + } + } +} + +void cheatsSaveCheatList(const char *file) +{ + if (cheatsNumber == 0) + return; + FILE *f = fopen(file, "wb"); + if (f == NULL) + return; + int version = 1; + fwrite(&version, 1, sizeof(version), f); + int type = 0; + fwrite(&type, 1, sizeof(type), f); + fwrite(&cheatsNumber, 1, sizeof(cheatsNumber), f); + fwrite(cheatsList, 1, sizeof(cheatsList), f); + fclose(f); +} + +bool cheatsLoadCheatList(const char *file) +{ + cheatsNumber = 0; + + int count = 0; + + FILE *f = fopen(file, "rb"); + + if (f == NULL) + return false; + + int version = 0; + + if (fread(&version, 1, sizeof(version), f) != sizeof(version)) + { + fclose(f); + return false; + } + + if (version != 1) + { + systemMessage(MSG_UNSUPPORTED_CHEAT_LIST_VERSION, + N_("Unsupported cheat list version %d"), version); + fclose(f); + return false; + } + + int type = 0; + if (fread(&type, 1, sizeof(type), f) != sizeof(type)) + { + fclose(f); + return false; + } + + if (type != 0) + { + systemMessage(MSG_UNSUPPORTED_CHEAT_LIST_TYPE, + N_("Unsupported cheat list type %d"), type); + fclose(f); + return false; + } + + if (fread(&count, 1, sizeof(count), f) != sizeof(count)) + { + fclose(f); + return false; + } + + if (fread(cheatsList, 1, sizeof(cheatsList), f) != sizeof(cheatsList)) + { + fclose(f); + return false; + } + + bool firstCodeBreaker = true; + + for (int i = 0; i < count; i++) + { + cheatsList[i].status = 0; // remove old status as it is not used + if (!cheatsList[i].codestring[0]) + { + switch (cheatsList[i].size) + { + case 0: + sprintf(cheatsList[i].codestring, "%08x:%02x", cheatsList[i].address, + cheatsList[i].value); + break; + case 1: + sprintf(cheatsList[i].codestring, "%08x:%04x", cheatsList[i].address, + cheatsList[i].value); + break; + case 2: + sprintf(cheatsList[i].codestring, "%08x:%08x", cheatsList[i].address, + cheatsList[i].value); + break; + } + } + + if (cheatsList[i].code == 512 && firstCodeBreaker) + { + firstCodeBreaker = false; + char buffer[10]; + strncpy(buffer, cheatsList[i].codestring, 8); + buffer[8] = 0; + u32 address; + sscanf(buffer, "%x", &address); + if ((address >> 28) == 9) + { + strncpy(buffer, &cheatsList[i].codestring[9], 4); + buffer[4] = 0; + u32 value; + sscanf(buffer, "%x", &value); + + u32 seed[8]; + cheatsCBAParseSeedCode(address, value, seed); + cheatsCBAChangeEncryption(seed); + } + } + } + cheatsNumber = count; + fclose(f); + return true; +} + +extern int *extCpuLoopTicks; +extern int *extClockTicks; +extern int *extTicks; +extern int cpuSavedTicks; + +extern void debuggerBreakOnWrite(u32 *, u32, u32, int); + +#define CPU_BREAK_LOOP2 \ + cpuSavedTicks = cpuSavedTicks - *extCpuLoopTicks; \ + *extCpuLoopTicks = *extClockTicks; \ + *extTicks = *extClockTicks; + +void cheatsWriteMemory(u32 *address, u32 value, u32 mask) +{ +#ifdef BKPT_SUPPORT +#ifdef SDL + if (cheatsNumber == 0) + { + debuggerBreakOnWrite(address, *address, value, 2); + CPU_BREAK_LOOP2; + *address = value; + return; + } +#endif +#endif +} + +void cheatsWriteHalfWord(u16 *address, u16 value, u16 mask) +{ +#ifdef BKPT_SUPPORT +#ifdef SDL + if (cheatsNumber == 0) + { + debuggerBreakOnWrite((u32 *)address, *address, value, 1); + CPU_BREAK_LOOP2; + *address = value; + return; + } +#endif +#endif +} + +#if defined BKPT_SUPPORT && defined SDL +void cheatsWriteByte(u8 *address, u8 value) +#else +void cheatsWriteByte(u8 *, u8) +#endif +{ +#ifdef BKPT_SUPPORT +#ifdef SDL + if (cheatsNumber == 0) + { + debuggerBreakOnWrite((u32 *)address, *address, value, 0); + CPU_BREAK_LOOP2; + *address = value; + return; + } +#endif +#endif +} + +#undef CPU_BREAK_LOOP2 diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBACheats.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBACheats.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,44 @@ +#ifndef VBA_GBA_CHEATS_H +#define VBA_GBA_CHEATS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +struct CheatsData +{ + int code; + int size; + int status; + bool enabled; + u32 address; + u32 value; + u32 oldValue; + char codestring[20]; + char desc[32]; +}; + +extern void cheatsAdd(const char *, const char *, u32, u32, int, int); +extern void cheatsAddCheatCode(const char *code, const char *desc); +extern void cheatsAddGSACode(const char *code, const char *desc, bool v3); +extern void cheatsAddCBACode(const char *code, const char *desc); +extern bool cheatsImportGSACodeFile(const char *name, int game, bool v3); +extern void cheatsDelete(int number, bool restore); +extern void cheatsDeleteAll(bool restore); +extern void cheatsEnable(int number); +extern void cheatsDisable(int number); +extern void cheatsSaveGame(gzFile file); +extern void cheatsReadGame(gzFile file); +extern void cheatsSaveCheatList(const char *file); +extern bool cheatsLoadCheatList(const char *file); +extern void cheatsWriteMemory(u32 *, u32, u32); +extern void cheatsWriteHalfWord(u16 *, u16, u16); +extern void cheatsWriteByte(u8 *, u8); +extern int cheatsCheckKeys(u32, u32); +extern int cheatsNumber; +extern CheatsData cheatsList[100]; + +#endif // GBA_CHEATS_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBAGfx.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBAGfx.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,35 @@ +#include "../Port.h" +#include "GBAGfx.h" + +int coeff[32] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 +}; + +// some of the rendering code in gfx.h (such as mode 0 line 1298) +// renders outside the given buffer (past 239) which corrupts other memory, +// so rather than find all places in that code that need to be fixed, +// just give it enough extra scratch space to use + +u32 line0[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +u32 line1[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +u32 line2[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +u32 line3[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +u32 lineOBJ[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +u32 lineOBJWin[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +u32 lineMix[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +bool gfxInWin0[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +bool gfxInWin1[240+LINE_BUFFER_OVERFLOW_LEEWAY]; + +int gfxBG2Changed = 0; +int gfxBG3Changed = 0; + +int gfxBG2X = 0; +int gfxBG2Y = 0; +int gfxBG2LastX = 0; +int gfxBG2LastY = 0; +int gfxBG3X = 0; +int gfxBG3Y = 0; +int gfxBG3LastX = 0; +int gfxBG3LastY = 0; +int gfxLastVCOUNT = 0; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBAGfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBAGfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1791 @@ +#ifndef VBA_GBA_GFX_H +#define VBA_GBA_GFX_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../Port.h" +#include "GBA.h" +#include "GBAGlobals.h" + +//#define SPRITE_DEBUG + +void gfxDrawTextScreen(u16, u16, u16, u32 *); +void gfxDrawRotScreen(u16, + u16, u16, + u16, u16, + u16, u16, + u16, u16, + int&, int&, + int, + u32 *); +void gfxDrawRotScreen16Bit(u16, + u16, u16, + u16, u16, + u16, u16, + u16, u16, + int&, int&, + int, + u32 *); +void gfxDrawRotScreen256(u16, + u16, u16, + u16, u16, + u16, u16, + u16, u16, + int&, int&, + int, + u32 *); +void gfxDrawRotScreen16Bit160(u16, + u16, u16, + u16, u16, + u16, u16, + u16, u16, + int&, int&, + int, + u32 *); +void gfxDrawSprites(u32 *); +void gfxIncreaseBrightness(u32 *line, int coeff); +void gfxDecreaseBrightness(u32 *line, int coeff); +void gfxAlphaBlend(u32 *ta, u32 *tb, int ca, int cb); + +void mode0RenderLine(); +void mode0RenderLineNoWindow(); +void mode0RenderLineAll(); + +void mode1RenderLine(); +void mode1RenderLineNoWindow(); +void mode1RenderLineAll(); + +void mode2RenderLine(); +void mode2RenderLineNoWindow(); +void mode2RenderLineAll(); + +void mode3RenderLine(); +void mode3RenderLineNoWindow(); +void mode3RenderLineAll(); + +void mode4RenderLine(); +void mode4RenderLineNoWindow(); +void mode4RenderLineAll(); + +void mode5RenderLine(); +void mode5RenderLineNoWindow(); +void mode5RenderLineAll(); + +extern int coeff[32]; + +#define LINE_BUFFER_OVERFLOW_LEEWAY (512-240) + +extern u32 line0[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +extern u32 line1[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +extern u32 line2[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +extern u32 line3[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +extern u32 lineOBJ[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +extern u32 lineOBJWin[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +extern u32 lineMix[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +extern bool gfxInWin0[240+LINE_BUFFER_OVERFLOW_LEEWAY]; +extern bool gfxInWin1[240+LINE_BUFFER_OVERFLOW_LEEWAY]; + +extern int gfxBG2Changed; +extern int gfxBG3Changed; + +extern int gfxBG2X; +extern int gfxBG2Y; +extern int gfxBG2LastX; +extern int gfxBG2LastY; +extern int gfxBG3X; +extern int gfxBG3Y; +extern int gfxBG3LastX; +extern int gfxBG3LastY; +extern int gfxLastVCOUNT; + +inline void gfxClearArray(u32 *array) +{ + for (int i = 0; i < 240; i++) + { + *array++ = 0x80000000; + } +} + +inline void gfxDrawTextScreen(u16 control, u16 hofs, u16 vofs, + u32 *line) +{ + u16 *palette = (u16 *)paletteRAM; + u8 * charBase = &vram[((control >> 2) & 0x03) * 0x4000]; + u16 *screenBase = (u16 *)&vram[((control >> 8) & 0x1f) * 0x800]; + u32 prio = ((control & 3)<<25) + 0x1000000; + int sizeX = 256; + int sizeY = 256; + switch ((control >> 14) & 3) + { + case 0: + break; + case 1: + sizeX = 512; + break; + case 2: + sizeY = 512; + break; + case 3: + sizeX = 512; + sizeY = 512; + break; + } + + int maskX = sizeX-1; + int maskY = sizeY-1; + + bool mosaicOn = (control & 0x40) ? true : false; + + int xxx = hofs & maskX; + int yyy = (vofs + VCOUNT) & maskY; + int mosaicX = (MOSAIC & 0x000F)+1; + int mosaicY = ((MOSAIC & 0x00F0)>>4)+1; + + if (mosaicOn) + { + if ((VCOUNT % mosaicY) != 0) + { + mosaicY = (VCOUNT / mosaicY) * mosaicY; + yyy = (vofs + mosaicY) & maskY; + } + } + + if (yyy > 255 && sizeY > 256) + { + yyy &= 255; + screenBase += 0x400; + if (sizeX > 256) + screenBase += 0x400; + } + + int yshift = ((yyy>>3)<<5); + if ((control) & 0x80) + { + u16 *screenSource = screenBase + 0x400 * (xxx>>8) + ((xxx & 255)>>3) + yshift; + for (int x = 0; x < 240; x++) + { + u16 data = READ16LE(screenSource); + + int tile = data & 0x3FF; + int tileX = (xxx & 7); + int tileY = yyy & 7; + + if (data & 0x0400) + tileX = 7 - tileX; + if (data & 0x0800) + tileY = 7 - tileY; + + u8 color = charBase[tile * 64 + tileY * 8 + tileX]; + + line[x] = color ? (READ16LE(&palette[color]) | prio) : 0x80000000; + + if (data & 0x0400) + { + if (tileX == 0) + screenSource++; + } + else if (tileX == 7) + screenSource++; + xxx++; + if (xxx == 256) + { + if (sizeX > 256) + screenSource = screenBase + 0x400 + yshift; + else + { + screenSource = screenBase + yshift; + xxx = 0; + } + } + else if (xxx >= sizeX) + { + xxx = 0; + screenSource = screenBase + yshift; + } + } + } + else + { + u16 *screenSource = screenBase + 0x400*(xxx>>8)+((xxx&255)>>3) + + yshift; + for (int x = 0; x < 240; x++) + { + u16 data = READ16LE(screenSource); + + int tile = data & 0x3FF; + int tileX = (xxx & 7); + int tileY = yyy & 7; + + if (data & 0x0400) + tileX = 7 - tileX; + if (data & 0x0800) + tileY = 7 - tileY; + + u8 color = charBase[(tile<<5) + (tileY<<2) + (tileX>>1)]; + + if (tileX & 1) + { + color = (color >> 4); + } + else + { + color &= 0x0F; + } + + int pal = (READ16LE(screenSource)>>8) & 0xF0; + line[x] = color ? (READ16LE(&palette[pal + color])|prio) : 0x80000000; + + if (data & 0x0400) + { + if (tileX == 0) + screenSource++; + } + else if (tileX == 7) + screenSource++; + xxx++; + if (xxx == 256) + { + if (sizeX > 256) + screenSource = screenBase + 0x400 + yshift; + else + { + screenSource = screenBase + yshift; + xxx = 0; + } + } + else if (xxx >= sizeX) + { + xxx = 0; + screenSource = screenBase + yshift; + } + } + } + if (mosaicOn) + { + if (mosaicX > 1) + { + int m = 1; + for (int i = 0; i < 239; i++) + { + line[i+1] = line[i]; + m++; + if (m == mosaicX) + { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawRotScreen(u16 control, + u16 x_l, u16 x_h, + u16 y_l, u16 y_h, + u16 pa, u16 pb, + u16 pc, u16 pd, + int& currentX, int& currentY, + int changed, + u32 *line) +{ + u16 *palette = (u16 *)paletteRAM; + u8 * charBase = &vram[((control >> 2) & 0x03) * 0x4000]; + u8 * screenBase = (u8 *)&vram[((control >> 8) & 0x1f) * 0x800]; + int prio = ((control & 3) << 25) + 0x1000000; + + int sizeX = 128; + int sizeY = 128; + switch ((control >> 14) & 3) + { + case 0: + break; + case 1: + sizeX = sizeY = 256; + break; + case 2: + sizeX = sizeY = 512; + break; + case 3: + sizeX = sizeY = 1024; + break; + } + + int dx = pa & 0x7FFF; + if (pa & 0x8000) + dx |= 0xFFFF8000; + int dmx = pb & 0x7FFF; + if (pb & 0x8000) + dmx |= 0xFFFF8000; + int dy = pc & 0x7FFF; + if (pc & 0x8000) + dy |= 0xFFFF8000; + int dmy = pd & 0x7FFFF; + if (pd & 0x8000) + dmy |= 0xFFFF8000; + + if (VCOUNT == 0) + changed = 3; + + if (changed & 1) + { + currentX = (x_l) | ((x_h & 0x07FF)<<16); + if (x_h & 0x0800) + currentX |= 0xF8000000; + } + else + { + currentX += dmx; + } + + if (changed & 2) + { + currentY = (y_l) | ((y_h & 0x07FF)<<16); + if (y_h & 0x0800) + currentY |= 0xF8000000; + } + else + { + currentY += dmy; + } + + int realX = currentX; + int realY = currentY; + + if (control & 0x40) + { + int mosaicY = ((MOSAIC & 0xF0)>>4) + 1; + int y = (VCOUNT % mosaicY); + realX -= y*dmx; + realY -= y*dmy; + } + + int xxx = (realX >> 8); + int yyy = (realY >> 8); + + if (control & 0x2000) + { + xxx %= sizeX; + yyy %= sizeY; + if (xxx < 0) + xxx += sizeX; + if (yyy < 0) + yyy += sizeY; + } + + if (control & 0x80) + { + for (int x = 0; x < 240; x++) + { + if (xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) + { + line[x] = 0x80000000; + } + else + { + int tile = screenBase[(xxx>>3) + (yyy>>3)*(sizeX>>3)]; + + int tileX = (xxx & 7); + int tileY = yyy & 7; + + u8 color = charBase[(tile<<6) + (tileY<<3) + tileX]; + + line[x] = color ? (READ16LE(&palette[color])|prio) : 0x80000000; + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + + if (control & 0x2000) + { + xxx %= sizeX; + yyy %= sizeY; + if (xxx < 0) + xxx += sizeX; + if (yyy < 0) + yyy += sizeY; + } + } + } + else + { + for (int x = 0; x < 240; x++) + { + if (xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) + { + line[x] = 0x80000000; + } + else + { + int tile = screenBase[(xxx>>3) + (yyy>>3)*(sizeX>>3)]; + + int tileX = (xxx & 7); + int tileY = yyy & 7; + + u8 color = charBase[(tile<<6) + (tileY<<3) + tileX]; + + line[x] = color ? (READ16LE(&palette[color])|prio) : 0x80000000; + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + + if (control & 0x2000) + { + xxx %= sizeX; + yyy %= sizeY; + if (xxx < 0) + xxx += sizeX; + if (yyy < 0) + yyy += sizeY; + } + } + } + + if (control & 0x40) + { + int mosaicX = (MOSAIC & 0xF) + 1; + if (mosaicX > 1) + { + int m = 1; + for (int i = 0; i < 239; i++) + { + line[i+1] = line[i]; + m++; + if (m == mosaicX) + { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawRotScreen16Bit(u16 control, + u16 x_l, u16 x_h, + u16 y_l, u16 y_h, + u16 pa, u16 pb, + u16 pc, u16 pd, + int& currentX, int& currentY, + int changed, + u32 *line) +{ + u16 *screenBase = (u16 *)&vram[0]; + int prio = ((control & 3) << 25) + 0x1000000; + int sizeX = 240; + int sizeY = 160; + + int startX = (x_l) | ((x_h & 0x07FF)<<16); + if (x_h & 0x0800) + startX |= 0xF8000000; + int startY = (y_l) | ((y_h & 0x07FF)<<16); + if (y_h & 0x0800) + startY |= 0xF8000000; + + int dx = pa & 0x7FFF; + if (pa & 0x8000) + dx |= 0xFFFF8000; + int dmx = pb & 0x7FFF; + if (pb & 0x8000) + dmx |= 0xFFFF8000; + int dy = pc & 0x7FFF; + if (pc & 0x8000) + dy |= 0xFFFF8000; + int dmy = pd & 0x7FFFF; + if (pd & 0x8000) + dmy |= 0xFFFF8000; + + if (VCOUNT == 0) + changed = 3; + + if (changed & 1) + { + currentX = (x_l) | ((x_h & 0x07FF)<<16); + if (x_h & 0x0800) + currentX |= 0xF8000000; + } + else + currentX += dmx; + + if (changed & 2) + { + currentY = (y_l) | ((y_h & 0x07FF)<<16); + if (y_h & 0x0800) + currentY |= 0xF8000000; + } + else + { + currentY += dmy; + } + + int realX = currentX; + int realY = currentY; + + if (control & 0x40) + { + int mosaicY = ((MOSAIC & 0xF0)>>4) + 1; + int y = (VCOUNT % mosaicY); + realX -= y*dmx; + realY -= y*dmy; + } + + int xxx = (realX >> 8); + int yyy = (realY >> 8); + + for (int x = 0; x < 240; x++) + { + if (xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) + { + line[x] = 0x80000000; + } + else + { + line[x] = (READ16LE(&screenBase[yyy * sizeX + xxx]) | prio); + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + } + + if (control & 0x40) + { + int mosaicX = (MOSAIC & 0xF) + 1; + if (mosaicX > 1) + { + int m = 1; + for (int i = 0; i < 239; i++) + { + line[i+1] = line[i]; + m++; + if (m == mosaicX) + { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawRotScreen256(u16 control, + u16 x_l, u16 x_h, + u16 y_l, u16 y_h, + u16 pa, u16 pb, + u16 pc, u16 pd, + int ¤tX, int& currentY, + int changed, + u32 *line) +{ + u16 *palette = (u16 *)paletteRAM; + u8 * screenBase = (DISPCNT & 0x0010) ? &vram[0xA000] : &vram[0x0000]; + int prio = ((control & 3) << 25) + 0x1000000; + int sizeX = 240; + int sizeY = 160; + + int startX = (x_l) | ((x_h & 0x07FF)<<16); + if (x_h & 0x0800) + startX |= 0xF8000000; + int startY = (y_l) | ((y_h & 0x07FF)<<16); + if (y_h & 0x0800) + startY |= 0xF8000000; + + int dx = pa & 0x7FFF; + if (pa & 0x8000) + dx |= 0xFFFF8000; + int dmx = pb & 0x7FFF; + if (pb & 0x8000) + dmx |= 0xFFFF8000; + int dy = pc & 0x7FFF; + if (pc & 0x8000) + dy |= 0xFFFF8000; + int dmy = pd & 0x7FFFF; + if (pd & 0x8000) + dmy |= 0xFFFF8000; + + if (VCOUNT == 0) + changed = 3; + + if (changed & 1) + { + currentX = (x_l) | ((x_h & 0x07FF)<<16); + if (x_h & 0x0800) + currentX |= 0xF8000000; + } + else + { + currentX += dmx; + } + + if (changed & 2) + { + currentY = (y_l) | ((y_h & 0x07FF)<<16); + if (y_h & 0x0800) + currentY |= 0xF8000000; + } + else + { + currentY += dmy; + } + + int realX = currentX; + int realY = currentY; + + if (control & 0x40) + { + int mosaicY = ((MOSAIC & 0xF0)>>4) + 1; + int y = (VCOUNT / mosaicY) * mosaicY; + realX = startX + y*dmx; + realY = startY + y*dmy; + } + + int xxx = (realX >> 8); + int yyy = (realY >> 8); + + for (int x = 0; x < 240; x++) + { + if (xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) + { + line[x] = 0x80000000; + } + else + { + u8 color = screenBase[yyy * 240 + xxx]; + + line[x] = color ? (READ16LE(&palette[color])|prio) : 0x80000000; + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + } + + if (control & 0x40) + { + int mosaicX = (MOSAIC & 0xF) + 1; + if (mosaicX > 1) + { + int m = 1; + for (int i = 0; i < 239; i++) + { + line[i+1] = line[i]; + m++; + if (m == mosaicX) + { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawRotScreen16Bit160(u16 control, + u16 x_l, u16 x_h, + u16 y_l, u16 y_h, + u16 pa, u16 pb, + u16 pc, u16 pd, + int& currentX, int& currentY, + int changed, + u32 *line) +{ + u16 *screenBase = (DISPCNT & 0x0010) ? (u16 *)&vram[0xa000] : + (u16 *)&vram[0]; + int prio = ((control & 3) << 25) + 0x1000000; + int sizeX = 160; + int sizeY = 128; + + int startX = (x_l) | ((x_h & 0x07FF)<<16); + if (x_h & 0x0800) + startX |= 0xF8000000; + int startY = (y_l) | ((y_h & 0x07FF)<<16); + if (y_h & 0x0800) + startY |= 0xF8000000; + + int dx = pa & 0x7FFF; + if (pa & 0x8000) + dx |= 0xFFFF8000; + int dmx = pb & 0x7FFF; + if (pb & 0x8000) + dmx |= 0xFFFF8000; + int dy = pc & 0x7FFF; + if (pc & 0x8000) + dy |= 0xFFFF8000; + int dmy = pd & 0x7FFFF; + if (pd & 0x8000) + dmy |= 0xFFFF8000; + + if (VCOUNT == 0) + changed = 3; + + if (changed & 1) + { + currentX = (x_l) | ((x_h & 0x07FF)<<16); + if (x_h & 0x0800) + currentX |= 0xF8000000; + } + else + { + currentX += dmx; + } + + if (changed & 2) + { + currentY = (y_l) | ((y_h & 0x07FF)<<16); + if (y_h & 0x0800) + currentY |= 0xF8000000; + } + else + { + currentY += dmy; + } + + int realX = currentX; + int realY = currentY; + + if (control & 0x40) + { + int mosaicY = ((MOSAIC & 0xF0)>>4) + 1; + int y = (VCOUNT / mosaicY) * mosaicY; + realX = startX + y*dmx; + realY = startY + y*dmy; + } + + int xxx = (realX >> 8); + int yyy = (realY >> 8); + + for (int x = 0; x < 240; x++) + { + if (xxx < 0 || + yyy < 0 || + xxx >= sizeX || + yyy >= sizeY) + { + line[x] = 0x80000000; + } + else + { + line[x] = (READ16LE(&screenBase[yyy * sizeX + xxx]) | prio); + } + realX += dx; + realY += dy; + + xxx = (realX >> 8); + yyy = (realY >> 8); + } + + if (control & 0x40) + { + int mosaicX = (MOSAIC & 0xF) + 1; + if (mosaicX > 1) + { + int m = 1; + for (int i = 0; i < 239; i++) + { + line[i+1] = line[i]; + m++; + if (m == mosaicX) + { + m = 1; + i++; + } + } + } + } +} + +inline void gfxDrawSprites(u32 *lineOBJ) +{ + int m = 0; + gfxClearArray(lineOBJ); + if (layerEnable & 0x1000) + { + u16 *sprites = (u16 *)oam; + u16 *spritePalette = &((u16 *)paletteRAM)[256]; + int mosaicY = ((MOSAIC & 0xF000)>>12) + 1; + int mosaicX = ((MOSAIC & 0xF00)>>8) + 1; + for (int x = 0; x < 128; x++) + { + u16 a0 = READ16LE(sprites++); + u16 a1 = READ16LE(sprites++); + u16 a2 = READ16LE(sprites++); + sprites++; + + // ignore OBJ-WIN + if ((a0 & 0x0c00) == 0x0800) + continue; + + int sizeY = 8; + int sizeX = 8; + + switch (((a0 >>12) & 0x0c)|(a1>>14)) + { + case 0: + break; + case 1: + sizeX = sizeY = 16; + break; + case 2: + sizeX = sizeY = 32; + break; + case 3: + sizeX = sizeY = 64; + break; + case 4: + sizeX = 16; + break; + case 5: + sizeX = 32; + break; + case 6: + sizeX = 32; + sizeY = 16; + break; + case 7: + sizeX = 64; + sizeY = 32; + break; + case 8: + sizeY = 16; + break; + case 9: + sizeY = 32; + break; + case 10: + sizeX = 16; + sizeY = 32; + break; + case 11: + sizeX = 32; + sizeY = 64; + break; + default: + continue; + } + +#ifdef SPRITE_DEBUG + int maskX = sizeX-1; + int maskY = sizeY-1; +#endif + + int sy = (a0 & 255); + + if (sy > 160) + sy -= 256; + + if (a0 & 0x0100) + { + int fieldX = sizeX; + int fieldY = sizeY; + if (a0 & 0x0200) + { + fieldX <<= 1; + fieldY <<= 1; + } + + int t = VCOUNT - sy; + if ((t >= 0) && (t < fieldY)) + { + int sx = (a1 & 0x1FF); + if ((sx < 240) || (((sx + fieldX) & 511) < 240)) + { + // int t2 = t - (fieldY >> 1); + int rot = (a1 >> 9) & 0x1F; + u16 *OAM = (u16 *)oam; + int dx = READ16LE(&OAM[3 + (rot << 4)]); + if (dx & 0x8000) + dx |= 0xFFFF8000; + int dmx = READ16LE(&OAM[7 + (rot << 4)]); + if (dmx & 0x8000) + dmx |= 0xFFFF8000; + int dy = READ16LE(&OAM[11 + (rot << 4)]); + if (dy & 0x8000) + dy |= 0xFFFF8000; + int dmy = READ16LE(&OAM[15 + (rot << 4)]); + if (dmy & 0x8000) + dmy |= 0xFFFF8000; + + if (a0 & 0x1000) + { + t -= (t % mosaicY); + } + + int realX = ((sizeX) << 7) - (fieldX >> 1)*dx - (fieldY>>1)*dmx + + t * dmx; + int realY = ((sizeY) << 7) - (fieldX >> 1)*dy - (fieldY>>1)*dmy + + t * dmy; + + u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + + if (a0 & 0x2000) + { + int c = (a2 & 0x3FF); + if ((DISPCNT & 7) > 2 && (c < 512)) + continue; + int inc = 32; + if (DISPCNT & 0x40) + inc = sizeX >> 2; + else + c &= 0x3FE; + for (int x = 0; x < fieldX; x++) + { + int xxx = realX >> 8; + int yyy = realY >> 8; + + if (xxx < 0 || xxx >= sizeX || + yyy < 0 || yyy >= sizeY || + sx >= 240) + ; + else + { + u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5) + + ((yyy & 7)<<3) + ((xxx >> 3)<<6) + + (xxx & 7))&0x7FFF)]; + if ((color == 0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) + { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + else if ((color) && (prio < (lineOBJ[sx]&0xFF000000))) + { + lineOBJ[sx] = READ16LE(&spritePalette[color]) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + + if (a0 & 0x1000) + { + m++; + if (m == mosaicX) + m = 0; + } +#ifdef SPRITE_DEBUG + if (t == 0 || t == maskY || x == 0 || x == maskX) + lineOBJ[sx] = 0x001F; +#endif + } + sx = (sx+1)&511;; + realX += dx; + realY += dy; + } + } + else + { + int c = (a2 & 0x3FF); + if ((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if (DISPCNT & 0x40) + inc = sizeX >> 3; + int palette = (a2 >> 8) & 0xF0; + for (int x = 0; x < fieldX; x++) + { + int xxx = realX >> 8; + int yyy = realY >> 8; + if (xxx < 0 || xxx >= sizeX || + yyy < 0 || yyy >= sizeY || + sx >= 240) + ; + else + { + u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5) + + ((yyy & 7)<<2) + ((xxx >> 3)<<5) + + ((xxx & 7)>>1))&0x7FFF)]; + if (xxx & 1) + color >>= 4; + else + color &= 0x0F; + + if ((color == 0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) + { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + else if ((color) && (prio < (lineOBJ[sx]&0xFF000000))) + { + lineOBJ[sx] = READ16LE(&spritePalette[palette+color]) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + } + if ((a0 & 0x1000) && m) + { + m++; + if (m == mosaicX) + m = 0; + } + +#ifdef SPRITE_DEBUG + if (t == 0 || t == maskY || x == 0 || x == maskX) + lineOBJ[sx] = 0x001F; +#endif + sx = (sx+1)&511;; + realX += dx; + realY += dy; + } + } + } + } + } + else + { + int t = VCOUNT - sy; + if ((t >= 0) && (t < sizeY)) + { + int sx = (a1 & 0x1FF); + if (((sx < 240) || (((sx+sizeX)&511) < 240)) && !(a0 & 0x0200)) + { + if (a0 & 0x2000) + { + if (a1 & 0x2000) + t = sizeY - t - 1; + int c = (a2 & 0x3FF); + if ((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if (DISPCNT & 0x40) + { + inc = sizeX >> 2; + } + else + { + c &= 0x3FE; + } + int xxx = 0; + if (a1 & 0x1000) + xxx = sizeX-1; + + if (a0 & 0x1000) + { + t -= (t % mosaicY); + } + + int address = 0x10000 + ((((c+ (t>>3) * inc) << 5) + + ((t & 7) << 3) + ((xxx>>3)<<6) + (xxx & 7)) & 0x7FFF); + + if (a1 & 0x1000) + xxx = 7; + u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + + for (int xx = 0; xx < sizeX; xx++) + { + if (sx < 240) + { + u8 color = vram[address]; + if ((color == 0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) + { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + else if ((color) && (prio < (lineOBJ[sx]&0xFF000000))) + { + lineOBJ[sx] = READ16LE(&spritePalette[color]) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + + if (a0 & 0x1000) + { + m++; + if (m == mosaicX) + m = 0; + } + +#ifdef SPRITE_DEBUG + if (t == 0 || t == maskY || xx == 0 || xx == maskX) + lineOBJ[sx] = 0x001F; +#endif + } + + sx = (sx+1) & 511; + if (a1 & 0x1000) + { + xxx--; + address--; + if (xxx == -1) + { + address -= 56; + xxx = 7; + } + if (address < 0x10000) + address += 0x8000; + } + else + { + xxx++; + address++; + if (xxx == 8) + { + address += 56; + xxx = 0; + } + if (address > 0x17fff) + address -= 0x8000; + } + } + } + else + { + if (a1 & 0x2000) + t = sizeY - t - 1; + int c = (a2 & 0x3FF); + if ((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if (DISPCNT & 0x40) + { + inc = sizeX >> 3; + } + int xxx = 0; + if (a1 & 0x1000) + xxx = sizeX - 1; + + if (a0 & 0x1000) + { + t -= (t % mosaicY); + } + + int address = 0x10000 + ((((c + (t>>3) * inc)<<5) + + ((t & 7)<<2) + ((xxx>>3)<<5) + ((xxx & 7) >> 1))&0x7FFF); + u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + int palette = (a2 >> 8) & 0xF0; + if (a1 & 0x1000) + { + xxx = 7; + for (int xx = sizeX - 1; xx >= 0; xx--) + { + if (sx < 240) + { + u8 color = vram[address]; + if (xx & 1) + { + color = (color >> 4); + } + else + color &= 0x0F; + + if ((color == 0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) + { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + else if ((color) && (prio < (lineOBJ[sx]&0xFF000000))) + { + lineOBJ[sx] = READ16LE(&spritePalette[palette + color]) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + } + if (a0 & 0x1000) + { + m++; + if (m == mosaicX) + m = 0; + } +#ifdef SPRITE_DEBUG + if (t == 0 || t == maskY || xx == 0 || xx == maskX) + lineOBJ[sx] = 0x001F; +#endif + sx = (sx+1) & 511; + xxx--; + if (!(xx & 1)) + address--; + if (xxx == -1) + { + xxx = 7; + address -= 28; + } + if (address < 0x10000) + address += 0x8000; + } + } + else + { + for (int xx = 0; xx < sizeX; xx++) + { + if (sx < 240) + { + u8 color = vram[address]; + if (xx & 1) + { + color = (color >> 4); + } + else + color &= 0x0F; + + if ((color == 0) && (((prio >> 25)&3) < + ((lineOBJ[sx]>>25)&3))) + { + lineOBJ[sx] = (lineOBJ[sx] & 0xF9FFFFFF) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + else if ((color) && (prio < (lineOBJ[sx]&0xFF000000))) + { + lineOBJ[sx] = READ16LE(&spritePalette[palette + color]) | prio; + if ((a0 & 0x1000) && m) + lineOBJ[sx] = (lineOBJ[sx-1] & 0xF9FFFFFF) | prio; + } + } + if (a0 & 0x1000) + { + m++; + if (m == mosaicX) + m = 0; + } +#ifdef SPRITE_DEBUG + if (t == 0 || t == maskY || xx == 0 || xx == maskX) + lineOBJ[sx] = 0x001F; +#endif + sx = (sx+1) & 511; + xxx++; + if (xx & 1) + address++; + if (xxx == 8) + { + address += 28; + xxx = 0; + } + if (address > 0x17fff) + address -= 0x8000; + } + } + } + } + } + } + } + } +} + +inline void gfxDrawOBJWin(u32 *lineOBJWin) +{ + gfxClearArray(lineOBJWin); + if (layerEnable & 0x8000) + { + u16 *sprites = (u16 *)oam; + // u16 *spritePalette = &((u16 *)paletteRAM)[256]; + for (int x = 0; x < 128; x++) + { + u16 a0 = READ16LE(sprites++); + u16 a1 = READ16LE(sprites++); + u16 a2 = READ16LE(sprites++); + sprites++; + + // ignore non OBJ-WIN + if ((a0 & 0x0c00) != 0x0800) + continue; + + int sizeY = 8; + int sizeX = 8; + + switch (((a0 >>12) & 0x0c)|(a1>>14)) + { + case 0: + break; + case 1: + sizeX = sizeY = 16; + break; + case 2: + sizeX = sizeY = 32; + break; + case 3: + sizeX = sizeY = 64; + break; + case 4: + sizeX = 16; + break; + case 5: + sizeX = 32; + break; + case 6: + sizeX = 32; + sizeY = 16; + break; + case 7: + sizeX = 64; + sizeY = 32; + break; + case 8: + sizeY = 16; + break; + case 9: + sizeY = 32; + break; + case 10: + sizeX = 16; + sizeY = 32; + break; + case 11: + sizeX = 32; + sizeY = 64; + break; + default: + continue; + } + + int sy = (a0 & 255); + + if (sy > 160) + sy -= 256; + + if (a0 & 0x0100) + { + int fieldX = sizeX; + int fieldY = sizeY; + if (a0 & 0x0200) + { + fieldX <<= 1; + fieldY <<= 1; + } + + int t = VCOUNT - sy; + if ((t >= 0) && (t < fieldY)) + { + int sx = (a1 & 0x1FF); + if ((sx < 240) || (((sx + fieldX) & 511) < 240)) + { + // int t2 = t - (fieldY >> 1); + int rot = (a1 >> 9) & 0x1F; + u16 *OAM = (u16 *)oam; + int dx = READ16LE(&OAM[3 + (rot << 4)]); + if (dx & 0x8000) + dx |= 0xFFFF8000; + int dmx = READ16LE(&OAM[7 + (rot << 4)]); + if (dmx & 0x8000) + dmx |= 0xFFFF8000; + int dy = READ16LE(&OAM[11 + (rot << 4)]); + if (dy & 0x8000) + dy |= 0xFFFF8000; + int dmy = READ16LE(&OAM[15 + (rot << 4)]); + if (dmy & 0x8000) + dmy |= 0xFFFF8000; + + int realX = ((sizeX) << 7) - (fieldX >> 1)*dx - (fieldY>>1)*dmx + + t * dmx; + int realY = ((sizeY) << 7) - (fieldX >> 1)*dy - (fieldY>>1)*dmy + + t * dmy; + + // u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + + if (a0 & 0x2000) + { + int c = (a2 & 0x3FF); + if ((DISPCNT & 7) > 2 && (c < 512)) + continue; + int inc = 32; + if (DISPCNT & 0x40) + inc = sizeX >> 2; + else + c &= 0x3FE; + for (int x = 0; x < fieldX; x++) + { + int xxx = realX >> 8; + int yyy = realY >> 8; + + if (xxx < 0 || xxx >= sizeX || + yyy < 0 || yyy >= sizeY) + {} + else + { + u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5) + + ((yyy & 7)<<3) + ((xxx >> 3)<<6) + + (xxx & 7))&0x7fff)]; + if (color) + { + lineOBJWin[sx] = 1; + } + } + sx = (sx+1)&511;; + realX += dx; + realY += dy; + } + } + else + { + int c = (a2 & 0x3FF); + if ((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if (DISPCNT & 0x40) + inc = sizeX >> 3; + // int palette = (a2 >> 8) & 0xF0; + for (int x = 0; x < fieldX; x++) + { + int xxx = realX >> 8; + int yyy = realY >> 8; + + // if(x == 0 || x == (sizeX-1) || + // t == 0 || t == (sizeY-1)) { + // lineOBJ[sx] = 0x001F | prio; + // } else { + if (xxx < 0 || xxx >= sizeX || + yyy < 0 || yyy >= sizeY) + {} + else + { + u32 color = vram[0x10000 + ((((c + (yyy>>3) * inc)<<5) + + ((yyy & 7)<<2) + ((xxx >> 3)<<5) + + ((xxx & 7)>>1))&0x7fff)]; + if (xxx & 1) + color >>= 4; + else + color &= 0x0F; + + if (color) + { + lineOBJWin[sx] = 1; + } + } + // } + sx = (sx+1)&511;; + realX += dx; + realY += dy; + } + } + } + } + } + else + { + int t = VCOUNT - sy; + if ((t >= 0) && (t < sizeY)) + { + int sx = (a1 & 0x1FF); + if (((sx < 240) || (((sx+sizeX)&511) < 240)) && !(a0 & 0x0200)) + { + if (a0 & 0x2000) + { + if (a1 & 0x2000) + t = sizeY - t - 1; + int c = (a2 & 0x3FF); + if ((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if (DISPCNT & 0x40) + { + inc = sizeX >> 2; + } + else + { + c &= 0x3FE; + } + int xxx = 0; + if (a1 & 0x1000) + xxx = sizeX-1; + int address = 0x10000 + ((((c+ (t>>3) * inc) << 5) + + ((t & 7) << 3) + ((xxx>>3)<<6) + (xxx & 7))&0x7fff); + if (a1 & 0x1000) + xxx = 7; + // u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + for (int xx = 0; xx < sizeX; xx++) + { + if (sx < 240) + { + u8 color = vram[address]; + if (color) + { + lineOBJWin[sx] = 1; + } + } + + sx = (sx+1) & 511; + if (a1 & 0x1000) + { + xxx--; + address--; + if (xxx == -1) + { + address -= 56; + xxx = 7; + } + if (address < 0x10000) + address += 0x8000; + } + else + { + xxx++; + address++; + if (xxx == 8) + { + address += 56; + xxx = 0; + } + if (address > 0x17fff) + address -= 0x8000; + } + } + } + else + { + if (a1 & 0x2000) + t = sizeY - t - 1; + int c = (a2 & 0x3FF); + if ((DISPCNT & 7) > 2 && (c < 512)) + continue; + + int inc = 32; + if (DISPCNT & 0x40) + { + inc = sizeX >> 3; + } + int xxx = 0; + if (a1 & 0x1000) + xxx = sizeX - 1; + int address = 0x10000 + ((((c + (t>>3) * inc)<<5) + + ((t & 7)<<2) + ((xxx>>3)<<5) + ((xxx & 7) >> 1))&0x7fff); + // u32 prio = (((a2 >> 10) & 3) << 25) | ((a0 & 0x0c00)<<6); + // int palette = (a2 >> 8) & 0xF0; + if (a1 & 0x1000) + { + xxx = 7; + for (int xx = sizeX - 1; xx >= 0; xx--) + { + if (sx < 240) + { + u8 color = vram[address]; + if (xx & 1) + { + color = (color >> 4); + } + else + color &= 0x0F; + + if (color) + { + lineOBJWin[sx] = 1; + } + } + sx = (sx+1) & 511; + xxx--; + if (!(xx & 1)) + address--; + if (xxx == -1) + { + xxx = 7; + address -= 28; + } + if (address < 0x10000) + address += 0x8000; + } + } + else + { + for (int xx = 0; xx < sizeX; xx++) + { + if (sx < 240) + { + u8 color = vram[address]; + if (xx & 1) + { + color = (color >> 4); + } + else + color &= 0x0F; + + if (color) + { + lineOBJWin[sx] = 1; + } + } + sx = (sx+1) & 511; + xxx++; + if (xx & 1) + address++; + if (xxx == 8) + { + address += 28; + xxx = 0; + } + if (address > 0x17fff) + address -= 0x8000; + } + } + } + } + } + } + } + } +} + +inline u32 gfxIncreaseBrightness(u32 color, int coeff) +{ + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + + r = r + (((31 - r) * coeff) >> 4); + g = g + (((31 - g) * coeff) >> 4); + b = b + (((31 - b) * coeff) >> 4); + if (r > 31) + r = 31; + if (g > 31) + g = 31; + if (b > 31) + b = 31; + color = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + return color; +} + +inline void gfxIncreaseBrightness(u32 *line, int coeff) +{ + for (int x = 0; x < 240; x++) + { + u32 color = *line; + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + + r = r + (((31 - r) * coeff) >> 4); + g = g + (((31 - g) * coeff) >> 4); + b = b + (((31 - b) * coeff) >> 4); + if (r > 31) + r = 31; + if (g > 31) + g = 31; + if (b > 31) + b = 31; + *line++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + } +} + +inline u32 gfxDecreaseBrightness(u32 color, int coeff) +{ + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + + r = r - ((r * coeff) >> 4); + g = g - ((g * coeff) >> 4); + b = b - ((b * coeff) >> 4); + if (r < 0) + r = 0; + if (g < 0) + g = 0; + if (b < 0) + b = 0; + color = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + + return color; +} + +inline void gfxDecreaseBrightness(u32 *line, int coeff) +{ + for (int x = 0; x < 240; x++) + { + u32 color = *line; + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + + r = r - ((r * coeff) >> 4); + g = g - ((g * coeff) >> 4); + b = b - ((b * coeff) >> 4); + if (r < 0) + r = 0; + if (g < 0) + g = 0; + if (b < 0) + b = 0; + *line++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + } +} + +inline u32 gfxAlphaBlend(u32 color, u32 color2, int ca, int cb) +{ + if (color < 0x80000000) + { + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + int r0 = (color2 & 0x1F); + int g0 = ((color2 >> 5) & 0x1F); + int b0 = ((color2 >> 10) & 0x1F); + + r = ((r * ca) >> 4) + ((r0 * cb) >> 4); + g = ((g * ca) >> 4) + ((g0 * cb) >> 4); + b = ((b * ca) >> 4) + ((b0 * cb) >> 4); + + if (r > 31) + r = 31; + if (g > 31) + g = 31; + if (b > 31) + b = 31; + + return (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + } + return color; +} + +inline void gfxAlphaBlend(u32 *ta, u32 *tb, int ca, int cb) +{ + for (int x = 0; x < 240; x++) + { + u32 color = *ta; + if (color < 0x80000000) + { + int r = (color & 0x1F); + int g = ((color >> 5) & 0x1F); + int b = ((color >> 10) & 0x1F); + u32 color2 = (*tb++); + int r0 = (color2 & 0x1F); + int g0 = ((color2 >> 5) & 0x1F); + int b0 = ((color2 >> 10) & 0x1F); + + r = ((r * ca) >> 4) + ((r0 * cb) >> 4); + g = ((g * ca) >> 4) + ((g0 * cb) >> 4); + b = ((b * ca) >> 4) + ((b0 * cb) >> 4); + + if (r > 31) + r = 31; + if (g > 31) + g = 31; + if (b > 31) + b = 31; + + *ta++ = (color & 0xFFFF0000) | (b << 10) | (g << 5) | r; + } + else + { + ta++; + tb++; + } + } +} + +#endif // VBA_GBA_GFX_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBAGlobals.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBAGlobals.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,123 @@ +#include "GBAGlobals.h" + +reg_pair reg[45]; +memoryMap map[256]; +bool8 ioReadable[0x400]; +bool8 N_FLAG = 0; +bool8 C_FLAG = 0; +bool8 Z_FLAG = 0; +bool8 V_FLAG = 0; +bool8 armState = true; +bool8 armIrqEnable = true; +u32 armNextPC = 0x00000000; +int32 armMode = 0x1f; +u32 stop = 0x08000568; +int32 saveType = 0; +bool8 useBios = false; +bool8 skipBios = false; +int32 frameSkip = 1; +u32 extButtons = 0; +bool8 capturePrevious = false; +int32 captureNumber = 0; +bool8 speedup = false; +bool8 synchronize = true; +bool8 cpuDisableSfx = false; +bool8 cpuIsMultiBoot = false; +bool8 parseDebug = true; +int32 layerSettings = 0xff00; +int32 layerEnable = 0xff00; +bool8 speedHack = false; +bool8 memLagEnabled = false; +bool8 memLagTempEnabled = false; +bool8 useOldFrameTiming = false; +int32 cpuSaveType = 0; +bool8 cpuEnhancedDetection = true; +bool8 cheatsEnabled = true; + +u8 *bios = NULL; +u8 *rom = NULL; +u8 *internalRAM = NULL; +u8 *workRAM = NULL; +u8 *paletteRAM = NULL; +u8 *vram = NULL; +u8 *pix = NULL; +u8 *oam = NULL; +u8 *ioMem = NULL; + +u16 DISPCNT = 0x0080; +u16 DISPSTAT = 0x0000; +u16 VCOUNT = 0x0000; +u16 BG0CNT = 0x0000; +u16 BG1CNT = 0x0000; +u16 BG2CNT = 0x0000; +u16 BG3CNT = 0x0000; +u16 BG0HOFS = 0x0000; +u16 BG0VOFS = 0x0000; +u16 BG1HOFS = 0x0000; +u16 BG1VOFS = 0x0000; +u16 BG2HOFS = 0x0000; +u16 BG2VOFS = 0x0000; +u16 BG3HOFS = 0x0000; +u16 BG3VOFS = 0x0000; +u16 BG2PA = 0x0100; +u16 BG2PB = 0x0000; +u16 BG2PC = 0x0000; +u16 BG2PD = 0x0100; +u16 BG2X_L = 0x0000; +u16 BG2X_H = 0x0000; +u16 BG2Y_L = 0x0000; +u16 BG2Y_H = 0x0000; +u16 BG3PA = 0x0100; +u16 BG3PB = 0x0000; +u16 BG3PC = 0x0000; +u16 BG3PD = 0x0100; +u16 BG3X_L = 0x0000; +u16 BG3X_H = 0x0000; +u16 BG3Y_L = 0x0000; +u16 BG3Y_H = 0x0000; +u16 WIN0H = 0x0000; +u16 WIN1H = 0x0000; +u16 WIN0V = 0x0000; +u16 WIN1V = 0x0000; +u16 WININ = 0x0000; +u16 WINOUT = 0x0000; +u16 MOSAIC = 0x0000; +u16 BLDMOD = 0x0000; +u16 COLEV = 0x0000; +u16 COLY = 0x0000; +u16 DM0SAD_L = 0x0000; +u16 DM0SAD_H = 0x0000; +u16 DM0DAD_L = 0x0000; +u16 DM0DAD_H = 0x0000; +u16 DM0CNT_L = 0x0000; +u16 DM0CNT_H = 0x0000; +u16 DM1SAD_L = 0x0000; +u16 DM1SAD_H = 0x0000; +u16 DM1DAD_L = 0x0000; +u16 DM1DAD_H = 0x0000; +u16 DM1CNT_L = 0x0000; +u16 DM1CNT_H = 0x0000; +u16 DM2SAD_L = 0x0000; +u16 DM2SAD_H = 0x0000; +u16 DM2DAD_L = 0x0000; +u16 DM2DAD_H = 0x0000; +u16 DM2CNT_L = 0x0000; +u16 DM2CNT_H = 0x0000; +u16 DM3SAD_L = 0x0000; +u16 DM3SAD_H = 0x0000; +u16 DM3DAD_L = 0x0000; +u16 DM3DAD_H = 0x0000; +u16 DM3CNT_L = 0x0000; +u16 DM3CNT_H = 0x0000; +u16 TM0D = 0x0000; +u16 TM0CNT = 0x0000; +u16 TM1D = 0x0000; +u16 TM1CNT = 0x0000; +u16 TM2D = 0x0000; +u16 TM2CNT = 0x0000; +u16 TM3D = 0x0000; +u16 TM3CNT = 0x0000; +u16 P1 = 0xFFFF; +u16 IE = 0x0000; +u16 IF = 0x0000; +u16 IME = 0x0000; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBAGlobals.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBAGlobals.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,208 @@ +#ifndef VBA_GBA_GLOBALS_H +#define VBA_GBA_GLOBALS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../Port.h" + +#define VERBOSE_SWI 1 +#define VERBOSE_UNALIGNED_MEMORY 2 +#define VERBOSE_ILLEGAL_WRITE 4 +#define VERBOSE_ILLEGAL_READ 8 +#define VERBOSE_DMA0 16 +#define VERBOSE_DMA1 32 +#define VERBOSE_DMA2 64 +#define VERBOSE_DMA3 128 +#define VERBOSE_UNDEFINED 256 +#define VERBOSE_AGBPRINT 512 + +// moved from armdis.cpp +#define debuggerReadMemory(addr) \ + READ32LE(&map[(addr) >> 24].address[(addr) & map[(addr) >> 24].mask]) + +#define debuggerReadHalfWord(addr) \ + READ16LE(&map[(addr) >> 24].address[(addr) & map[(addr) >> 24].mask]) + +#define debuggerReadByte(addr) \ + READ8LE(&map[(addr) >> 24].address[(addr) & map[(addr) >> 24].mask]) + +#define debuggerWriteMemory(addr, value) \ + WRITE32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask]), (value)) + +#define debuggerWriteHalfWord(addr, value) \ + WRITE16LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask]), (value)) + +#define debuggerWriteByte(addr, value) \ + WRITE8LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask]), (value)) + +// moved from GBA.h +typedef struct +{ + u8 *address; + u32 mask; +} memoryMap; + +#ifndef NO_GBA_MAP +extern memoryMap map[256]; +#endif + +// moved from GBA.h +typedef union +{ + struct + { +#ifdef WORDS_BIGENDIAN + u8 B3; + u8 B2; + u8 B1; + u8 B0; +#else + u8 B0; + u8 B1; + u8 B2; + u8 B3; +#endif + } B; + struct + { +#ifdef WORDS_BIGENDIAN + u16 W1; + u16 W0; +#else + u16 W0; + u16 W1; +#endif + } W; +#ifdef WORDS_BIGENDIAN + volatile u32 I; +#else + u32 I; +#endif +} reg_pair; + +extern reg_pair reg[45]; +extern u8 biosProtected[4]; +extern bool8 ioReadable[0x400]; +extern bool8 N_FLAG; +extern bool8 C_FLAG; +extern bool8 Z_FLAG; +extern bool8 V_FLAG; +extern bool8 armState; +extern bool8 armIrqEnable; +extern u32 armNextPC; +extern int32 armMode; +extern u32 stop; +extern int32 saveType; +extern bool8 useBios; +extern bool8 skipBios; +extern int32 frameSkip; +extern u32 extButtons; +extern bool8 capturePrevious; +extern int32 captureNumber; +extern bool8 speedup; +extern bool8 synchronize; +extern bool8 cpuDisableSfx; +extern bool8 cpuIsMultiBoot; +extern bool8 parseDebug; +extern int32 layerSettings; +extern int32 layerEnable; +extern bool8 speedHack; +extern bool8 memLagEnabled, memLagTempEnabled; +extern bool8 useOldFrameTiming; +extern int32 cpuSaveType; +extern bool8 cpuEnhancedDetection; +extern bool8 cheatsEnabled; + +extern int emulating; + +extern u8 *bios; +extern u8 *rom; +extern u8 *internalRAM; +extern u8 *workRAM; +extern u8 *paletteRAM; +extern u8 *vram; +extern u8 *pix; +extern u8 *oam; +extern u8 *ioMem; + +extern u16 DISPCNT; +extern u16 DISPSTAT; +extern u16 VCOUNT; +extern u16 BG0CNT; +extern u16 BG1CNT; +extern u16 BG2CNT; +extern u16 BG3CNT; +extern u16 BG0HOFS; +extern u16 BG0VOFS; +extern u16 BG1HOFS; +extern u16 BG1VOFS; +extern u16 BG2HOFS; +extern u16 BG2VOFS; +extern u16 BG3HOFS; +extern u16 BG3VOFS; +extern u16 BG2PA; +extern u16 BG2PB; +extern u16 BG2PC; +extern u16 BG2PD; +extern u16 BG2X_L; +extern u16 BG2X_H; +extern u16 BG2Y_L; +extern u16 BG2Y_H; +extern u16 BG3PA; +extern u16 BG3PB; +extern u16 BG3PC; +extern u16 BG3PD; +extern u16 BG3X_L; +extern u16 BG3X_H; +extern u16 BG3Y_L; +extern u16 BG3Y_H; +extern u16 WIN0H; +extern u16 WIN1H; +extern u16 WIN0V; +extern u16 WIN1V; +extern u16 WININ; +extern u16 WINOUT; +extern u16 MOSAIC; +extern u16 BLDMOD; +extern u16 COLEV; +extern u16 COLY; +extern u16 DM0SAD_L; +extern u16 DM0SAD_H; +extern u16 DM0DAD_L; +extern u16 DM0DAD_H; +extern u16 DM0CNT_L; +extern u16 DM0CNT_H; +extern u16 DM1SAD_L; +extern u16 DM1SAD_H; +extern u16 DM1DAD_L; +extern u16 DM1DAD_H; +extern u16 DM1CNT_L; +extern u16 DM1CNT_H; +extern u16 DM2SAD_L; +extern u16 DM2SAD_H; +extern u16 DM2DAD_L; +extern u16 DM2DAD_H; +extern u16 DM2CNT_L; +extern u16 DM2CNT_H; +extern u16 DM3SAD_L; +extern u16 DM3SAD_H; +extern u16 DM3DAD_L; +extern u16 DM3DAD_H; +extern u16 DM3CNT_L; +extern u16 DM3CNT_H; +extern u16 TM0D; +extern u16 TM0CNT; +extern u16 TM1D; +extern u16 TM1CNT; +extern u16 TM2D; +extern u16 TM2CNT; +extern u16 TM3D; +extern u16 TM3CNT; +extern u16 P1; +extern u16 IE; +extern u16 IF; +extern u16 IME; + +#endif // VBA_GBA_GLOBALS_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBASound.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBASound.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1542 @@ +#if (defined(WIN32) && !defined(SDL)) +# include "../win32/stdafx.h" +# include "../win32/VBA.h" +#endif + +#include +#include + +#include "GBASound.h" +#include "../common/System.h" // SDL build needs this +#include "../common/Util.h" +#include "GBA.h" +#include "GBAGlobals.h" + +#ifndef countof +#define countof(a) (sizeof(a) / sizeof(a[0])) +#endif + +soundtick_t USE_TICKS_AS = 380; // (16777216.0/44100.0); // FIXME: (16777216.0/280896.0)(fps) vs 60.0fps? + +#define SOUND_MAGIC 0x60000000 +#define SOUND_MAGIC_2 0x30000000 +#define NOISE_MAGIC (2097152.0 / 44100.0) + +extern bool8 stopState; + +u8 soundWavePattern[4][32] = { + { 0x01, 0x01, 0x01, 0x01, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff }, + { 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff }, + { 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff }, + { 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff } +}; + +int32 soundFreqRatio[8] = { + 1048576, // 0 + 524288, // 1 + 262144, // 2 + 174763, // 3 + 131072, // 4 + 104858, // 5 + 87381, // 6 + 74898 // 7 +}; + +int32 soundShiftClock[16] = { + 2, // 0 + 4, // 1 + 8, // 2 + 16, // 3 + 32, // 4 + 64, // 5 + 128, // 6 + 256, // 7 + 512, // 8 + 1024, // 9 + 2048, // 10 + 4096, // 11 + 8192, // 12 + 16384, // 13 + 1, // 14 + 1 // 15 +}; + +int32 soundVolume = 0; + +u8 soundBuffer[6][735]; +u16 soundFinalWave[1470]; +u16 soundFrameSound[735 * 30 * 2]; // for avi logging + +u32 soundBufferLen = 1470; +u32 soundBufferTotalLen = 14700; +int32 soundQuality = 2; +int32 soundPaused = 1; +int32 soundPlay = 0; +soundtick_t soundTicks = soundQuality * USE_TICKS_AS; +soundtick_t SOUND_CLOCK_TICKS = soundQuality * USE_TICKS_AS; +u32 soundNextPosition = 0; + +int32 soundLevel1 = 0; +int32 soundLevel2 = 0; +int32 soundBalance = 0; +int32 soundMasterOn = 0; +u32 soundIndex = 0; +u32 soundBufferIndex = 0; +int32 soundFrameSoundWritten = 0; +int32 soundDebug = 0; +bool8 soundOffFlag = false; + +int32 sound1On = 0; +int32 sound1ATL = 0; +int32 sound1Skip = 0; +int32 sound1Index = 0; +int32 sound1Continue = 0; +int32 sound1EnvelopeVolume = 0; +int32 sound1EnvelopeATL = 0; +int32 sound1EnvelopeUpDown = 0; +int32 sound1EnvelopeATLReload = 0; +int32 sound1SweepATL = 0; +int32 sound1SweepATLReload = 0; +int32 sound1SweepSteps = 0; +int32 sound1SweepUpDown = 0; +int32 sound1SweepStep = 0; +u8 * sound1Wave = soundWavePattern[2]; + +int32 sound2On = 0; +int32 sound2ATL = 0; +int32 sound2Skip = 0; +int32 sound2Index = 0; +int32 sound2Continue = 0; +int32 sound2EnvelopeVolume = 0; +int32 sound2EnvelopeATL = 0; +int32 sound2EnvelopeUpDown = 0; +int32 sound2EnvelopeATLReload = 0; +u8 * sound2Wave = soundWavePattern[2]; + +int32 sound3On = 0; +int32 sound3ATL = 0; +int32 sound3Skip = 0; +int32 sound3Index = 0; +int32 sound3Continue = 0; +int32 sound3OutputLevel = 0; +int32 sound3Last = 0; +u8 sound3WaveRam[0x20]; +int32 sound3Bank = 0; +int32 sound3DataSize = 0; +int32 sound3ForcedOutput = 0; + +int32 sound4On = 0; +int32 sound4Clock = 0; +int32 sound4ATL = 0; +int32 sound4Skip = 0; +int32 sound4Index = 0; +int32 sound4ShiftRight = 0x7f; +int32 sound4ShiftSkip = 0; +int32 sound4ShiftIndex = 0; +int32 sound4NSteps = 0; +int32 sound4CountDown = 0; +int32 sound4Continue = 0; +int32 sound4EnvelopeVolume = 0; +int32 sound4EnvelopeATL = 0; +int32 sound4EnvelopeUpDown = 0; +int32 sound4EnvelopeATLReload = 0; + +int32 soundControl = 0; + +int32 soundDSFifoAIndex = 0; +int32 soundDSFifoACount = 0; +int32 soundDSFifoAWriteIndex = 0; +bool8 soundDSAEnabled = false; +int32 soundDSATimer = 0; +u8 soundDSFifoA[32]; +u8 soundDSAValue = 0; + +int32 soundDSFifoBIndex = 0; +int32 soundDSFifoBCount = 0; +int32 soundDSFifoBWriteIndex = 0; +bool8 soundDSBEnabled = false; +int32 soundDSBTimer = 0; +u8 soundDSFifoB[32]; +u8 soundDSBValue = 0; + +int32 soundEnableFlag = 0x3ff; +int32 soundMutedFlag = 0; + +s16 soundFilter[4000]; +s16 soundRight[5] = { 0, 0, 0, 0, 0 }; +s16 soundLeft[5] = { 0, 0, 0, 0, 0 }; +int32 soundEchoIndex = 0; +bool8 soundEcho = false; +bool8 soundLowPass = false; +bool8 soundReverse = false; + +static int32 soundTicks_int32; +static int32 SOUND_CLOCK_TICKS_int32; +static int32 soundDSBValue_int32; +variable_desc soundSaveStruct[] = { + { &soundPaused, sizeof(int32) }, + { &soundPlay, sizeof(int32) }, + { &soundTicks_int32, sizeof(int32) }, + { &SOUND_CLOCK_TICKS_int32, sizeof(int32) }, + { &soundLevel1, sizeof(int32) }, + { &soundLevel2, sizeof(int32) }, + { &soundBalance, sizeof(int32) }, + { &soundMasterOn, sizeof(int32) }, + { &soundIndex, sizeof(int32) }, + { &sound1On, sizeof(int32) }, + { &sound1ATL, sizeof(int32) }, + { &sound1Skip, sizeof(int32) }, + { &sound1Index, sizeof(int32) }, + { &sound1Continue, sizeof(int32) }, + { &sound1EnvelopeVolume, sizeof(int32) }, + { &sound1EnvelopeATL, sizeof(int32) }, + { &sound1EnvelopeATLReload, sizeof(int32) }, + { &sound1EnvelopeUpDown, sizeof(int32) }, + { &sound1SweepATL, sizeof(int32) }, + { &sound1SweepATLReload, sizeof(int32) }, + { &sound1SweepSteps, sizeof(int32) }, + { &sound1SweepUpDown, sizeof(int32) }, + { &sound1SweepStep, sizeof(int32) }, + { &sound2On, sizeof(int32) }, + { &sound2ATL, sizeof(int32) }, + { &sound2Skip, sizeof(int32) }, + { &sound2Index, sizeof(int32) }, + { &sound2Continue, sizeof(int32) }, + { &sound2EnvelopeVolume, sizeof(int32) }, + { &sound2EnvelopeATL, sizeof(int32) }, + { &sound2EnvelopeATLReload, sizeof(int32) }, + { &sound2EnvelopeUpDown, sizeof(int32) }, + { &sound3On, sizeof(int32) }, + { &sound3ATL, sizeof(int32) }, + { &sound3Skip, sizeof(int32) }, + { &sound3Index, sizeof(int32) }, + { &sound3Continue, sizeof(int32) }, + { &sound3OutputLevel, sizeof(int32) }, + { &sound4On, sizeof(int32) }, + { &sound4ATL, sizeof(int32) }, + { &sound4Skip, sizeof(int32) }, + { &sound4Index, sizeof(int32) }, + { &sound4Clock, sizeof(int32) }, + { &sound4ShiftRight, sizeof(int32) }, + { &sound4ShiftSkip, sizeof(int32) }, + { &sound4ShiftIndex, sizeof(int32) }, + { &sound4NSteps, sizeof(int32) }, + { &sound4CountDown, sizeof(int32) }, + { &sound4Continue, sizeof(int32) }, + { &sound4EnvelopeVolume, sizeof(int32) }, + { &sound4EnvelopeATL, sizeof(int32) }, + { &sound4EnvelopeATLReload, sizeof(int32) }, + { &sound4EnvelopeUpDown, sizeof(int32) }, + { &soundEnableFlag, sizeof(int32) }, + { &soundControl, sizeof(int32) }, + { &soundDSFifoAIndex, sizeof(int32) }, + { &soundDSFifoACount, sizeof(int32) }, + { &soundDSFifoAWriteIndex, sizeof(int32) }, + { &soundDSAEnabled, sizeof(bool8) }, + { &soundDSATimer, sizeof(int32) }, + { &soundDSFifoA[0], 32 }, + { &soundDSAValue, sizeof(u8) }, + { &soundDSFifoBIndex, sizeof(int32) }, + { &soundDSFifoBCount, sizeof(int32) }, + { &soundDSFifoBWriteIndex, sizeof(int32) }, + { &soundDSBEnabled, sizeof(int32) }, + { &soundDSBTimer, sizeof(int32) }, + { &soundDSFifoB[0], 32 }, + { &soundDSBValue_int32, sizeof(int32) }, // save as int32 because of a mistake of the past. + { &soundBuffer[0][0], 6 * 735 }, + { &soundFinalWave[0], 2 * 735 }, + { NULL, 0 } +}; + +variable_desc soundSaveStructV2[] = { + { &sound3WaveRam[0], 0x20 }, + { &sound3Bank, sizeof(int32) }, + { &sound3DataSize, sizeof(int32) }, + { &sound3ForcedOutput, sizeof(int32) }, + { NULL, 0 } +}; + +//variable_desc soundSaveStructV3[] = { +// { &soundTicks, sizeof(soundtick_t) }, +// { &SOUND_CLOCK_TICKS, sizeof(soundtick_t) }, +// { &USE_TICKS_AS, sizeof(soundtick_t) }, +// { NULL, 0 } +//}; + +void soundEvent(u32 address, u8 data) +{ + int freq = 0; + + switch (address) + { + case NR10: + data &= 0x7f; + sound1SweepATL = sound1SweepATLReload = 344 * ((data >> 4) & 7); + sound1SweepSteps = data & 7; + sound1SweepUpDown = data & 0x08; + sound1SweepStep = 0; + ioMem[address] = data; + break; + case NR11: + sound1Wave = soundWavePattern[data >> 6]; + sound1ATL = 172 * (64 - (data & 0x3f)); + ioMem[address] = data; + break; + case NR12: + sound1EnvelopeUpDown = data & 0x08; + sound1EnvelopeATLReload = 689 * (data & 7); + if ((data & 0xF8) == 0) + sound1EnvelopeVolume = 0; + ioMem[address] = data; + break; + case NR13: + freq = (((int)(ioMem[NR14] & 7)) << 8) | data; + sound1ATL = 172 * (64 - (ioMem[NR11] & 0x3f)); + freq = 2048 - freq; + if (freq) + { + sound1Skip = SOUND_MAGIC / freq; + } + else + sound1Skip = 0; + ioMem[address] = data; + break; + case NR14: + data &= 0xC7; + freq = (((int)(data & 7) << 8) | ioMem[NR13]); + freq = 2048 - freq; + sound1ATL = 172 * (64 - (ioMem[NR11] & 0x3f)); + sound1Continue = data & 0x40; + if (freq) + { + sound1Skip = SOUND_MAGIC / freq; + } + else + sound1Skip = 0; + if (data & 0x80) + { + ioMem[NR52] |= 1; + sound1EnvelopeVolume = ioMem[NR12] >> 4; + sound1EnvelopeUpDown = ioMem[NR12] & 0x08; + sound1ATL = 172 * (64 - (ioMem[NR11] & 0x3f)); + sound1EnvelopeATLReload = sound1EnvelopeATL = 689 * (ioMem[NR12] & 7); + sound1SweepATL = sound1SweepATLReload = 344 * ((ioMem[NR10] >> 4) & 7); + sound1SweepSteps = ioMem[NR10] & 7; + sound1SweepUpDown = ioMem[NR10] & 0x08; + sound1SweepStep = 0; + + sound1Index = 0; + sound1On = 1; + } + ioMem[address] = data; + break; + case NR21: + sound2Wave = soundWavePattern[data >> 6]; + sound2ATL = 172 * (64 - (data & 0x3f)); + ioMem[address] = data; + break; + case NR22: + sound2EnvelopeUpDown = data & 0x08; + sound2EnvelopeATLReload = 689 * (data & 7); + if ((data & 0xF8) == 0) + sound2EnvelopeVolume = 0; + ioMem[address] = data; + break; + case NR23: + freq = (((int)(ioMem[NR24] & 7)) << 8) | data; + sound2ATL = 172 * (64 - (ioMem[NR21] & 0x3f)); + freq = 2048 - freq; + if (freq) + { + sound2Skip = SOUND_MAGIC / freq; + } + else + sound2Skip = 0; + ioMem[address] = data; + break; + case NR24: + data &= 0xC7; + freq = (((int)(data & 7) << 8) | ioMem[NR23]); + freq = 2048 - freq; + sound2ATL = 172 * (64 - (ioMem[NR21] & 0x3f)); + sound2Continue = data & 0x40; + if (freq) + { + sound2Skip = SOUND_MAGIC / freq; + } + else + sound2Skip = 0; + if (data & 0x80) + { + ioMem[NR52] |= 2; + sound2EnvelopeVolume = ioMem[NR22] >> 4; + sound2EnvelopeUpDown = ioMem[NR22] & 0x08; + sound2ATL = 172 * (64 - (ioMem[NR21] & 0x3f)); + sound2EnvelopeATLReload = sound2EnvelopeATL = 689 * (ioMem[NR22] & 7); + + sound2Index = 0; + sound2On = 1; + } + ioMem[address] = data; + break; + case NR30: + data &= 0xe0; + if (!(data & 0x80)) + { + ioMem[NR52] &= 0xfb; + sound3On = 0; + } + if (((data >> 6) & 1) != sound3Bank) + memcpy(&ioMem[0x90], &sound3WaveRam[(((data >> 6) & 1) * 0x10) ^ 0x10], + 0x10); + sound3Bank = (data >> 6) & 1; + sound3DataSize = (data >> 5) & 1; + ioMem[address] = data; + break; + case NR31: + sound3ATL = 172 * (256 - data); + ioMem[address] = data; + break; + case NR32: + data &= 0xe0; + sound3OutputLevel = (data >> 5) & 3; + sound3ForcedOutput = (data >> 7) & 1; + ioMem[address] = data; + break; + case NR33: + freq = 2048 - (((int)(ioMem[NR34] & 7) << 8) | data); + if (freq) + { + sound3Skip = SOUND_MAGIC_2 / freq; + } + else + sound3Skip = 0; + ioMem[address] = data; + break; + case NR34: + data &= 0xc7; + freq = 2048 - (((data & 7) << 8) | (int)ioMem[NR33]); + if (freq) + { + sound3Skip = SOUND_MAGIC_2 / freq; + } + else + { + sound3Skip = 0; + } + sound3Continue = data & 0x40; + if ((data & 0x80) && (ioMem[NR30] & 0x80)) + { + ioMem[NR52] |= 4; + sound3ATL = 172 * (256 - ioMem[NR31]); + sound3Index = 0; + sound3On = 1; + } + ioMem[address] = data; + break; + case NR41: + data &= 0x3f; + sound4ATL = 172 * (64 - (data & 0x3f)); + ioMem[address] = data; + break; + case NR42: + sound4EnvelopeUpDown = data & 0x08; + sound4EnvelopeATLReload = 689 * (data & 7); + if ((data & 0xF8) == 0) + sound4EnvelopeVolume = 0; + ioMem[address] = data; + break; + case NR43: + freq = soundFreqRatio[data & 7]; + sound4NSteps = data & 0x08; + + sound4Skip = freq * NOISE_MAGIC; + + sound4Clock = data >> 4; + + freq = freq / soundShiftClock[sound4Clock]; + + sound4ShiftSkip = freq * NOISE_MAGIC; + ioMem[address] = data; + break; + case NR44: + data &= 0xc0; + sound4Continue = data & 0x40; + if (data & 0x80) + { + ioMem[NR52] |= 8; + sound4EnvelopeVolume = ioMem[NR42] >> 4; + sound4EnvelopeUpDown = ioMem[NR42] & 0x08; + sound4ATL = 172 * (64 - (ioMem[NR41] & 0x3f)); + sound4EnvelopeATLReload = sound4EnvelopeATL = 689 * (ioMem[NR42] & 7); + + sound4On = 1; + + sound4Index = 0; + sound4ShiftIndex = 0; + + freq = soundFreqRatio[ioMem[NR43] & 7]; + + sound4Skip = freq * NOISE_MAGIC; + + sound4NSteps = ioMem[NR43] & 0x08; + + freq = freq / soundShiftClock[ioMem[NR43] >> 4]; + + sound4ShiftSkip = freq * NOISE_MAGIC; + if (sound4NSteps) + sound4ShiftRight = 0x7f; + else + sound4ShiftRight = 0x7fff; + } + ioMem[address] = data; + break; + case NR50: + data &= 0x77; + soundLevel1 = data & 7; + soundLevel2 = (data >> 4) & 7; + ioMem[address] = data; + break; + case NR51: + soundBalance = (data & soundEnableFlag); + ioMem[address] = data; + break; + case NR52: + data &= 0x80; + data |= ioMem[NR52] & 15; + soundMasterOn = data & 0x80; + if (!(data & 0x80)) + { + sound1On = 0; + sound2On = 0; + sound3On = 0; + sound4On = 0; + } + ioMem[address] = data; + break; + case 0x90: + case 0x91: + case 0x92: + case 0x93: + case 0x94: + case 0x95: + case 0x96: + case 0x97: + case 0x98: + case 0x99: + case 0x9a: + case 0x9b: + case 0x9c: + case 0x9d: + case 0x9e: + case 0x9f: + sound3WaveRam[(sound3Bank * 0x10) ^ 0x10 + (address & 15)] = data; + break; + } +} + +void soundEvent(u32 address, u16 data) +{ + switch (address) + { + case SGCNT0_H: + data &= 0xFF0F; + soundControl = data & 0x770F;; + if (data & 0x0800) + { + soundDSFifoAWriteIndex = 0; + soundDSFifoAIndex = 0; + soundDSFifoACount = 0; + soundDSAValue = 0; + memset(soundDSFifoA, 0, 32); + } + soundDSAEnabled = (data & 0x0300) ? true : false; + soundDSATimer = (data & 0x0400) ? 1 : 0; + if (data & 0x8000) + { + soundDSFifoBWriteIndex = 0; + soundDSFifoBIndex = 0; + soundDSFifoBCount = 0; + soundDSBValue = 0; + memset(soundDSFifoB, 0, 32); + } + soundDSBEnabled = (data & 0x3000) ? true : false; + soundDSBTimer = (data & 0x4000) ? 1 : 0; + *((u16 *)&ioMem[address]) = data; + break; + case FIFOA_L: + case FIFOA_H: + soundDSFifoA[soundDSFifoAWriteIndex++] = data & 0xFF; + soundDSFifoA[soundDSFifoAWriteIndex++] = data >> 8; + soundDSFifoACount += 2; + soundDSFifoAWriteIndex &= 31; + *((u16 *)&ioMem[address]) = data; + break; + case FIFOB_L: + case FIFOB_H: + soundDSFifoB[soundDSFifoBWriteIndex++] = data & 0xFF; + soundDSFifoB[soundDSFifoBWriteIndex++] = data >> 8; + soundDSFifoBCount += 2; + soundDSFifoBWriteIndex &= 31; + *((u16 *)&ioMem[address]) = data; + break; + case 0x88: + data &= 0xC3FF; + *((u16 *)&ioMem[address]) = data; + break; + case 0x90: + case 0x92: + case 0x94: + case 0x96: + case 0x98: + case 0x9a: + case 0x9c: + case 0x9e: + *((u16 *)&sound3WaveRam[(sound3Bank * 0x10) ^ 0x10 + (address & 14)]) = data; + *((u16 *)&ioMem[address]) = data; + break; + } +} + +void soundChannel1() +{ + int vol = sound1EnvelopeVolume; + + int freq = 0; + int value = 0; + + if (sound1On && (sound1ATL || !sound1Continue)) + { + sound1Index += soundQuality * sound1Skip; + sound1Index &= 0x1fffffff; + + value = ((s8)sound1Wave[sound1Index >> 24]) * vol; + } + + soundBuffer[0][soundIndex] = value; + + if (sound1On) + { + if (sound1ATL) + { + sound1ATL -= soundQuality; + + if (sound1ATL <= 0 && sound1Continue) + { + ioMem[NR52] &= 0xfe; + sound1On = 0; + } + } + + if (sound1EnvelopeATL) + { + sound1EnvelopeATL -= soundQuality; + + if (sound1EnvelopeATL <= 0) + { + if (sound1EnvelopeUpDown) + { + if (sound1EnvelopeVolume < 15) + sound1EnvelopeVolume++; + } + else + { + if (sound1EnvelopeVolume) + sound1EnvelopeVolume--; + } + + sound1EnvelopeATL += sound1EnvelopeATLReload; + } + } + + if (sound1SweepATL) + { + sound1SweepATL -= soundQuality; + + if (sound1SweepATL <= 0) + { + freq = (((int)(ioMem[NR14] & 7) << 8) | ioMem[NR13]); + + int updown = 1; + + if (sound1SweepUpDown) + updown = -1; + + int newfreq = 0; + if (sound1SweepSteps) + { + newfreq = freq + updown * freq / (1 << sound1SweepSteps); + if (newfreq == freq) + newfreq = 0; + } + else + newfreq = freq; + + if (newfreq < 0) + { + sound1SweepATL += sound1SweepATLReload; + } + else if (newfreq > 2047) + { + sound1SweepATL = 0; + sound1On = 0; + ioMem[NR52] &= 0xfe; + } + else + { + sound1SweepATL += sound1SweepATLReload; + sound1Skip = SOUND_MAGIC / (2048 - newfreq); + + ioMem[NR13] = newfreq & 0xff; + ioMem[NR14] = (ioMem[NR14] & 0xf8) | ((newfreq >> 8) & 7); + } + } + } + } +} + +void soundChannel2() +{ + // int freq = 0; + int vol = sound2EnvelopeVolume; + + int value = 0; + + if (sound2On && (sound2ATL || !sound2Continue)) + { + sound2Index += soundQuality * sound2Skip; + sound2Index &= 0x1fffffff; + + value = ((s8)sound2Wave[sound2Index >> 24]) * vol; + } + + soundBuffer[1][soundIndex] = value; + + if (sound2On) + { + if (sound2ATL) + { + sound2ATL -= soundQuality; + + if (sound2ATL <= 0 && sound2Continue) + { + ioMem[NR52] &= 0xfd; + sound2On = 0; + } + } + + if (sound2EnvelopeATL) + { + sound2EnvelopeATL -= soundQuality; + + if (sound2EnvelopeATL <= 0) + { + if (sound2EnvelopeUpDown) + { + if (sound2EnvelopeVolume < 15) + sound2EnvelopeVolume++; + } + else + { + if (sound2EnvelopeVolume) + sound2EnvelopeVolume--; + } + sound2EnvelopeATL += sound2EnvelopeATLReload; + } + } + } +} + +void soundChannel3() +{ + int value = sound3Last; + + if (sound3On && (sound3ATL || !sound3Continue)) + { + sound3Index += soundQuality * sound3Skip; + if (sound3DataSize) + { + sound3Index &= 0x3fffffff; + value = sound3WaveRam[sound3Index >> 25]; + } + else + { + sound3Index &= 0x1fffffff; + value = sound3WaveRam[sound3Bank * 0x10 + (sound3Index >> 25)]; + } + + if ((sound3Index & 0x01000000)) + { + value &= 0x0f; + } + else + { + value >>= 4; + } + + value -= 8; + value *= 2; + + if (sound3ForcedOutput) + { + value = ((value >> 1) + value) >> 1; + } + else + { + switch (sound3OutputLevel) + { + case 0: + value = 0; + break; + case 1: + break; + case 2: + value = (value >> 1); + break; + case 3: + value = (value >> 2); + break; + } + } + //value += 1; + sound3Last = value; + } + + soundBuffer[2][soundIndex] = value; + + if (sound3On) + { + if (sound3ATL) + { + sound3ATL -= soundQuality; + + if (sound3ATL <= 0 && sound3Continue) + { + ioMem[NR52] &= 0xfb; + sound3On = 0; + } + } + } +} + +void soundChannel4() +{ + int vol = sound4EnvelopeVolume; + + int value = 0; + + if (sound4Clock <= 0x0c) + { + if (sound4On && (sound4ATL || !sound4Continue)) + { + #define NOISE_ONE_SAMP_SCALE 0x200000 + + sound4Index += soundQuality * sound4Skip; + sound4ShiftIndex += soundQuality * sound4ShiftSkip; + + if (sound4NSteps) + { + while (sound4ShiftIndex >= NOISE_ONE_SAMP_SCALE) + { + sound4ShiftRight = (((sound4ShiftRight << 6) ^ + (sound4ShiftRight << 5)) & 0x40) | + (sound4ShiftRight >> 1); + sound4ShiftIndex -= NOISE_ONE_SAMP_SCALE; + } + } + else + { + while (sound4ShiftIndex >= NOISE_ONE_SAMP_SCALE) + { + sound4ShiftRight = (((sound4ShiftRight << 14) ^ + (sound4ShiftRight << 13)) & 0x4000) | + (sound4ShiftRight >> 1); + + sound4ShiftIndex -= NOISE_ONE_SAMP_SCALE; + } + } + + sound4Index %= NOISE_ONE_SAMP_SCALE; + sound4ShiftIndex %= NOISE_ONE_SAMP_SCALE; + + value = ((sound4ShiftRight & 1) * 2 - 1) * vol; + } + else + { + value = 0; + } + } + + soundBuffer[3][soundIndex] = value; + + if (sound4On) + { + if (sound4ATL) + { + sound4ATL -= soundQuality; + + if (sound4ATL <= 0 && sound4Continue) + { + ioMem[NR52] &= 0xfd; + sound4On = 0; + } + } + + if (sound4EnvelopeATL) + { + sound4EnvelopeATL -= soundQuality; + + if (sound4EnvelopeATL <= 0) + { + if (sound4EnvelopeUpDown) + { + if (sound4EnvelopeVolume < 15) + sound4EnvelopeVolume++; + } + else + { + if (sound4EnvelopeVolume) + sound4EnvelopeVolume--; + } + sound4EnvelopeATL += sound4EnvelopeATLReload; + } + } + } +} + +void soundDirectSoundA() +{ + soundBuffer[4][soundIndex] = soundDSAValue; +} + +void soundDirectSoundATimer() +{ + if (soundDSAEnabled) + { + if (soundDSFifoACount <= 16) + { + CPUCheckDMA(3, 2); + if (soundDSFifoACount <= 16) + { + soundEvent(FIFOA_L, (u16)0); + soundEvent(FIFOA_H, (u16)0); + soundEvent(FIFOA_L, (u16)0); + soundEvent(FIFOA_H, (u16)0); + soundEvent(FIFOA_L, (u16)0); + soundEvent(FIFOA_H, (u16)0); + soundEvent(FIFOA_L, (u16)0); + soundEvent(FIFOA_H, (u16)0); + } + } + + soundDSAValue = (soundDSFifoA[soundDSFifoAIndex]); + soundDSFifoAIndex = (++soundDSFifoAIndex) & 31; + soundDSFifoACount--; + } + else + soundDSAValue = 0; +} + +void soundDirectSoundB() +{ + soundBuffer[5][soundIndex] = soundDSBValue; +} + +void soundDirectSoundBTimer() +{ + if (soundDSBEnabled) + { + if (soundDSFifoBCount <= 16) + { + CPUCheckDMA(3, 4); + if (soundDSFifoBCount <= 16) + { + soundEvent(FIFOB_L, (u16)0); + soundEvent(FIFOB_H, (u16)0); + soundEvent(FIFOB_L, (u16)0); + soundEvent(FIFOB_H, (u16)0); + soundEvent(FIFOB_L, (u16)0); + soundEvent(FIFOB_H, (u16)0); + soundEvent(FIFOB_L, (u16)0); + soundEvent(FIFOB_H, (u16)0); + } + } + + soundDSBValue = (soundDSFifoB[soundDSFifoBIndex]); + soundDSFifoBIndex = (++soundDSFifoBIndex) & 31; + soundDSFifoBCount--; + } + else + { + soundDSBValue = 0; + } +} + +void soundTimerOverflow(int timer) +{ + if (soundDSAEnabled && (soundDSATimer == timer)) + { + soundDirectSoundATimer(); + } + if (soundDSBEnabled && (soundDSBTimer == timer)) + { + soundDirectSoundBTimer(); + } +} + +void soundMix() +{ + int res = 0; + int cgbRes = 0; + int ratio = ioMem[0x82] & 3; + int dsaRatio = ioMem[0x82] & 4; + int dsbRatio = ioMem[0x82] & 8; + + if (ioMem) + soundBalance = (ioMem[NR51] & soundEnableFlag & ~soundMutedFlag); + + if (soundBalance & 16) + { + cgbRes = ((s8)soundBuffer[0][soundIndex]); + } + if (soundBalance & 32) + { + cgbRes += ((s8)soundBuffer[1][soundIndex]); + } + if (soundBalance & 64) + { + cgbRes += ((s8)soundBuffer[2][soundIndex]); + } + if (soundBalance & 128) + { + cgbRes += ((s8)soundBuffer[3][soundIndex]); + } + + if ((soundControl & 0x0200) && (soundEnableFlag & 0x100)) + { + if (!dsaRatio) + res = ((s8)soundBuffer[4][soundIndex]) >> 1; + else + res = ((s8)soundBuffer[4][soundIndex]); + } + + if ((soundControl & 0x2000) && (soundEnableFlag & 0x200)) + { + if (!dsbRatio) + res += ((s8)soundBuffer[5][soundIndex]) >> 1; + else + res += ((s8)soundBuffer[5][soundIndex]); + } + + res = (res * 170); + cgbRes = (cgbRes * 52 * soundLevel1); + + switch (ratio) + { + case 0: + case 3: // prohibited, but 25% + cgbRes >>= 2; + break; + case 1: + cgbRes >>= 1; + break; + case 2: + break; + } + + res += cgbRes; + + if (soundEcho) + { + res *= 2; + res += soundFilter[soundEchoIndex]; + res /= 2; + soundFilter[soundEchoIndex++] = res; + } + + if (soundLowPass) + { + soundLeft[4] = soundLeft[3]; + soundLeft[3] = soundLeft[2]; + soundLeft[2] = soundLeft[1]; + soundLeft[1] = soundLeft[0]; + soundLeft[0] = res; + res = (soundLeft[4] + 2 * soundLeft[3] + 8 * soundLeft[2] + 2 * soundLeft[1] + + soundLeft[0]) / 14; + } + + bool noSpecialEffects = false; +#if (defined(WIN32) && !defined(SDL)) + if (theApp.soundRecording || theApp.aviRecording || theApp.nvAudioLog) + noSpecialEffects = true; +#endif + + if (!noSpecialEffects) + { + switch (soundVolume) + { + case 0: + case 1: + case 2: + case 3: + res *= (soundVolume + 1); + break; + case 4: + res >>= 2; + break; + case 5: + res >>= 1; + break; + } + } + + if (res > 32767) + res = 32767; + if (res < -32768) + res = -32768; + + if (soundReverse && !noSpecialEffects) + { + soundFinalWave[++soundBufferIndex] = res; + if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) + /*assert(false)*/; + else + soundFrameSound[++soundFrameSoundWritten] = res; + } + else + { + soundFinalWave[soundBufferIndex++] = res; + if (soundFrameSoundWritten >= countof(soundFrameSound)) + /*assert(false)*/; + else + soundFrameSound[soundFrameSoundWritten++] = res; + } + + res = 0; + cgbRes = 0; + + if (soundBalance & 1) + { + cgbRes = ((s8)soundBuffer[0][soundIndex]); + } + if (soundBalance & 2) + { + cgbRes += ((s8)soundBuffer[1][soundIndex]); + } + if (soundBalance & 4) + { + cgbRes += ((s8)soundBuffer[2][soundIndex]); + } + if (soundBalance & 8) + { + cgbRes += ((s8)soundBuffer[3][soundIndex]); + } + + if ((soundControl & 0x0100) && (soundEnableFlag & 0x100)) + { + if (!dsaRatio) + res = ((s8)soundBuffer[4][soundIndex]) >> 1; + else + res = ((s8)soundBuffer[4][soundIndex]); + } + + if ((soundControl & 0x1000) && (soundEnableFlag & 0x200)) + { + if (!dsbRatio) + res += ((s8)soundBuffer[5][soundIndex]) >> 1; + else + res += ((s8)soundBuffer[5][soundIndex]); + } + + res = (res * 170); + cgbRes = (cgbRes * 52 * soundLevel1); + + switch (ratio) + { + case 0: + case 3: // prohibited, but 25% + cgbRes >>= 2; + break; + case 1: + cgbRes >>= 1; + break; + case 2: + break; + } + + res += cgbRes; + + if (soundEcho) + { + res *= 2; + res += soundFilter[soundEchoIndex]; + res /= 2; + soundFilter[soundEchoIndex++] = res; + + if (soundEchoIndex >= 4000) + soundEchoIndex = 0; + } + + if (soundLowPass) + { + soundRight[4] = soundRight[3]; + soundRight[3] = soundRight[2]; + soundRight[2] = soundRight[1]; + soundRight[1] = soundRight[0]; + soundRight[0] = res; + res = (soundRight[4] + 2 * soundRight[3] + 8 * soundRight[2] + 2 * soundRight[1] + + soundRight[0]) / 14; + } + + if (!noSpecialEffects) + { + switch (soundVolume) + { + case 0: + case 1: + case 2: + case 3: + res *= (soundVolume + 1); + break; + case 4: + res >>= 2; + break; + case 5: + res >>= 1; + break; + } + } + + if (res > 32767) + res = 32767; + if (res < -32768) + res = -32768; + + if (soundReverse && !noSpecialEffects) + { + soundFinalWave[-1 + soundBufferIndex++] = res; + if ((soundFrameSoundWritten) >= countof(soundFrameSound)) + /*assert(false)*/; + else + soundFrameSound[-1 + soundFrameSoundWritten++] = res; + } + else + { + soundFinalWave[soundBufferIndex++] = res; + if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) + /*assert(false)*/; + else + soundFrameSound[soundFrameSoundWritten++] = res; + } +} + +void soundTick() +{ + if (systemSoundOn) + { + if (soundMasterOn && !stopState) + { + soundChannel1(); + soundChannel2(); + soundChannel3(); + soundChannel4(); + soundDirectSoundA(); + soundDirectSoundB(); + soundMix(); + } + else + { + soundFinalWave[soundBufferIndex++] = 0; + soundFinalWave[soundBufferIndex++] = 0; + if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) + /*assert(false)*/; + else + { + soundFrameSound[soundFrameSoundWritten++] = 0; + soundFrameSound[soundFrameSoundWritten++] = 0; + } + } + + soundIndex++; + + if (2 * soundBufferIndex >= soundBufferLen) + { + if (systemSoundOn) + { + if (soundPaused) + { + soundResume(); + } + + systemSoundWriteToBuffer(); + } + soundIndex = 0; + soundBufferIndex = 0; + } + } +} + +void soundShutdown() +{ + systemSoundShutdown(); +} + +void soundPause() +{ + systemSoundPause(); +} + +void soundResume() +{ + systemSoundResume(); +} + +void soundEnableChannels(int channels) +{ + int c = (channels & 0x0f) << 4; + soundEnableFlag |= ((channels & 0x30f) | c); +} + +void soundDisableChannels(int channels) +{ + int c = (channels & 0x0f) << 4; + soundEnableFlag &= ~((channels & 0x30f) | c); +} + +int soundGetEnabledChannels() +{ + return (soundEnableFlag & 0x30f); +} + +#if 0 +// unused +void soundMuteChannels(int channels) +{ + soundMutedFlag |= channels & 0x30f; +} + +void soundUnmuteChannels(int channels) +{ + soundMutedFlag &= ~(channels & 0x30f); +} + +int soundGetMutedChannels() +{ + return (soundMutedFlag & 0x30f); +} +#endif + +void soundReset() +{ + systemSoundReset(); + + soundPaused = 1; + soundPlay = 0; + SOUND_CLOCK_TICKS = soundQuality * USE_TICKS_AS; + soundTicks = SOUND_CLOCK_TICKS; + soundNextPosition = 0; + soundMasterOn = 1; + soundIndex = 0; + soundBufferIndex = 0; + soundLevel1 = 7; + soundLevel2 = 7; + + sound1On = 0; + sound1ATL = 0; + sound1Skip = 0; + sound1Index = 0; + sound1Continue = 0; + sound1EnvelopeVolume = 0; + sound1EnvelopeATL = 0; + sound1EnvelopeUpDown = 0; + sound1EnvelopeATLReload = 0; + sound1SweepATL = 0; + sound1SweepATLReload = 0; + sound1SweepSteps = 0; + sound1SweepUpDown = 0; + sound1SweepStep = 0; + sound1Wave = soundWavePattern[2]; + + sound2On = 0; + sound2ATL = 0; + sound2Skip = 0; + sound2Index = 0; + sound2Continue = 0; + sound2EnvelopeVolume = 0; + sound2EnvelopeATL = 0; + sound2EnvelopeUpDown = 0; + sound2EnvelopeATLReload = 0; + sound2Wave = soundWavePattern[2]; + + sound3On = 0; + sound3ATL = 0; + sound3Skip = 0; + sound3Index = 0; + sound3Continue = 0; + sound3OutputLevel = 0; + sound3Last = 0; + sound3Bank = 0; + sound3DataSize = 0; + sound3ForcedOutput = 0; + + sound4On = 0; + sound4Clock = 0; + sound4ATL = 0; + sound4Skip = 0; + sound4Index = 0; + sound4ShiftRight = 0x7f; + sound4NSteps = 0; + sound4CountDown = 0; + sound4Continue = 0; + sound4EnvelopeVolume = 0; + sound4EnvelopeATL = 0; + sound4EnvelopeUpDown = 0; + sound4EnvelopeATLReload = 0; + + sound1On = 0; + sound2On = 0; + sound3On = 0; + sound4On = 0; + + int addr = 0x90; + + while (addr < 0xA0) + { + ioMem[addr++] = 0x00; + ioMem[addr++] = 0xff; + } + + addr = 0; + while (addr < 0x20) + { + sound3WaveRam[addr++] = 0x00; + sound3WaveRam[addr++] = 0xff; + } + + memset(soundFinalWave, 0, soundBufferLen); + + memset(soundFilter, 0, sizeof(soundFilter)); + soundEchoIndex = 0; +} + +bool soundInit() +{ + if (systemSoundInit()) + { + memset(soundBuffer[0], 0, 735 * 2); + memset(soundBuffer[1], 0, 735 * 2); + memset(soundBuffer[2], 0, 735 * 2); + memset(soundBuffer[3], 0, 735 * 2); + + memset(soundFinalWave, 0, soundBufferLen); + + soundPaused = 1; + return true; + } + return false; +} + +void soundSetQuality(int quality) +{ + if (soundQuality != quality && systemSoundCanChangeQuality()) + { + if (!soundOffFlag) + soundShutdown(); + soundQuality = quality; + soundNextPosition = 0; + if (!soundOffFlag) + soundInit(); + SOUND_CLOCK_TICKS = USE_TICKS_AS * soundQuality; + soundIndex = 0; + soundBufferIndex = 0; + } + else if (soundQuality != quality) + { + soundNextPosition = 0; + SOUND_CLOCK_TICKS = USE_TICKS_AS * soundQuality; + soundIndex = 0; + soundBufferIndex = 0; + } +} + +void soundSaveGame(gzFile gzFile) +{ + soundTicks_int32 = (int32) soundTicks; + SOUND_CLOCK_TICKS_int32 = (int32) SOUND_CLOCK_TICKS; + soundDSBValue_int32 = (int32) soundDSBValue; + + utilWriteData(gzFile, soundSaveStruct); + utilWriteData(gzFile, soundSaveStructV2); + + utilGzWrite(gzFile, &soundQuality, sizeof(int32)); + //utilWriteData(gzFile, soundSaveStructV3); +} + +void soundReadGame(gzFile gzFile, int version) +{ + int32 oldSoundPaused = soundPaused; + int32 oldSoundEnableFlag = soundEnableFlag; + utilReadData(gzFile, soundSaveStruct); + soundPaused = oldSoundPaused; + soundEnableFlag = oldSoundEnableFlag; + + if (version >= SAVE_GAME_VERSION_3) + { + utilReadData(gzFile, soundSaveStructV2); + } + else + { + sound3Bank = (ioMem[NR30] >> 6) & 1; + sound3DataSize = (ioMem[NR30] >> 5) & 1; + sound3ForcedOutput = (ioMem[NR32] >> 7) & 1; + // nothing better to do here... + memcpy(&sound3WaveRam[0x00], &ioMem[0x90], 0x10); + memcpy(&sound3WaveRam[0x10], &ioMem[0x90], 0x10); + } + soundBufferIndex = soundIndex * 2; + + int quality = 1; + utilGzRead(gzFile, &quality, sizeof(int32)); + soundSetQuality(quality); + + sound1Wave = soundWavePattern[ioMem[NR11] >> 6]; + sound2Wave = soundWavePattern[ioMem[NR21] >> 6]; + + //if(version >= SAVE_GAME_VERSION_14) { + // utilReadData(gzFile, soundSaveStructV3); + //} + //else { + soundTicks = (soundtick_t) soundTicks_int32; + SOUND_CLOCK_TICKS = (soundtick_t) SOUND_CLOCK_TICKS_int32; + //} + soundDSBValue = (u8) (soundDSBValue_int32 & 0xff); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBASound.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBASound.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,73 @@ +#ifndef VBA_GBA_SOUND_H +#define VBA_GBA_SOUND_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "zlib.h" +#include "../Port.h" + +#define NR10 0x60 +#define NR11 0x62 +#define NR12 0x63 +#define NR13 0x64 +#define NR14 0x65 +#define NR21 0x68 +#define NR22 0x69 +#define NR23 0x6c +#define NR24 0x6d +#define NR30 0x70 +#define NR31 0x72 +#define NR32 0x73 +#define NR33 0x74 +#define NR34 0x75 +#define NR41 0x78 +#define NR42 0x79 +#define NR43 0x7c +#define NR44 0x7d +#define NR50 0x80 +#define NR51 0x81 +#define NR52 0x84 +#define SGCNT0_H 0x82 +#define FIFOA_L 0xa0 +#define FIFOA_H 0xa2 +#define FIFOB_L 0xa4 +#define FIFOB_H 0xa6 + +extern void soundTick(); +extern void soundShutdown(); +extern bool soundInit(); +extern void soundPause(); +extern void soundResume(); +extern void soundEnableChannels(int); +extern void soundDisableChannels(int); +extern int soundGetEnabledChannels(); +extern void soundReset(); +extern void soundSaveGame(gzFile); +extern void soundReadGame(gzFile, int); +extern void soundEvent(u32, u8); +extern void soundEvent(u32, u16); +extern void soundTimerOverflow(int); +extern void soundSetQuality(int); + +typedef int32 soundtick_t; + +extern soundtick_t SOUND_CLOCK_TICKS; +extern soundtick_t soundTicks; +extern int32 soundPaused; +extern bool8 soundOffFlag; +extern int32 soundQuality; +extern u32 soundBufferLen; +extern u32 soundBufferTotalLen; +extern u32 soundNextPosition; +extern u16 soundFinalWave[1470]; +extern u16 soundFrameSound[735*30*2]; +extern int32 soundFrameSoundWritten; +extern int32 soundVolume; + +extern bool8 soundEcho; +extern bool8 soundLowPass; +extern bool8 soundReverse; + +#endif // VBA_GBA_SOUND_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/GBAinline.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/GBAinline.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,490 @@ +#ifndef VBA_GBAINLINE_H +#define VBA_GBAINLINE_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../Port.h" +#include "../common/System.h" +#include "../common/vbalua.h" +#include "GBAGlobals.h" +#include "EEprom.h" +#include "Flash.h" +#include "RTC.h" + +extern bool8 cpuSramEnabled; +extern bool8 cpuFlashEnabled; +extern bool8 cpuEEPROMEnabled; +extern bool8 cpuEEPROMSensorEnabled; + +#define CPUReadByteQuick(addr) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] + +#define CPUReadHalfWordQuick(addr) \ + READ16LE(((u16 *)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define CPUReadMemoryQuick(addr) \ + READ32LE(((u32 *)&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +//inline u32 CPUReadMemoryQuick(u32 addr) +//{ +// u32 addrShift = (addr)>>24; +// u32 rt = (addr) & map[addrShift].mask; +// return READ32LE(((u32*)&map[addrShift].address[rt])); +//} + +inline u32 CPUReadMemory(u32 address) +{ +#ifdef GBA_LOGGING + if (address & 3) + { + if (systemVerbose & VERBOSE_UNALIGNED_MEMORY) + { + log("Unaligned word read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } + } +#endif + + u32 value; + switch (address >> 24) + { + case 0: + if (reg[15].I >> 24) + { + if (address < 0x4000) + { +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_READ) + { + log("Illegal word read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + + value = READ32LE(((u32 *)&biosProtected)); + } + else + goto unreadable; + } + else + value = READ32LE(((u32 *)&bios[address & 0x3FFC])); + break; + case 2: + value = READ32LE(((u32 *)&workRAM[address & 0x3FFFC])); + break; + case 3: + value = READ32LE(((u32 *)&internalRAM[address & 0x7ffC])); + break; + case 4: + if ((address < 0x4000400) && ioReadable[address & 0x3fc]) + { + if (ioReadable[(address & 0x3fc) + 2]) + { + if (address >= 0x400012d && address <= 0x4000131) + GBASystemCounters.lagged = false; + value = READ32LE(((u32 *)&ioMem[address & 0x3fC])); + } + else + { + if (address >= 0x400012f && address <= 0x4000131) + GBASystemCounters.lagged = false; + value = READ16LE(((u16 *)&ioMem[address & 0x3fc])); + } + } + else + goto unreadable; + break; + case 5: + value = READ32LE(((u32 *)&paletteRAM[address & 0x3fC])); + break; + case 6: + value = READ32LE(((u32 *)&vram[address & 0x1fffc])); + break; + case 7: + value = READ32LE(((u32 *)&oam[address & 0x3FC])); + break; + case 8: + case 9: + case 10: + case 11: + case 12: + value = READ32LE(((u32 *)&rom[address&0x1FFFFFC])); + break; + case 13: + if (cpuEEPROMEnabled) + // no need to swap this + return eepromRead(address); + goto unreadable; + case 14: + if (cpuFlashEnabled | cpuSramEnabled) + // no need to swap this + return flashRead(address); + // default + default: +unreadable: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_READ) + { + log("Illegal word read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + + // if(ioMem[0x205] & 0x40) { + if (armState) + { + value = CPUReadMemoryQuick(reg[15].I); + } + else + { + value = CPUReadHalfWordQuick(reg[15].I) | + CPUReadHalfWordQuick(reg[15].I) << 16; + } + // } else { + // value = *((u32 *)&bios[address & 0x3ffc]); + // } + // return 0xFFFFFFFF; + } + + if (address & 3) + { +#ifdef C_CORE + int shift = (address & 3) << 3; + value = (value >> shift) | (value << (32 - shift)); +#else +#ifdef __GNUC__ + asm ("and $3, %%ecx;" + "shl $3 ,%%ecx;" + "ror %%cl, %0" + : "=r" (value) + : "r" (value), "c" (address)); +#else + __asm { + mov ecx, address; + and ecx, 3; + shl ecx, 3; + ror [dword ptr value], cl; + } +#endif +#endif + } + return value; +} + +extern u32 myROM[]; + +inline u32 CPUReadHalfWord(u32 address) +{ +#ifdef GBA_LOGGING + if (address & 1) + { + if (systemVerbose & VERBOSE_UNALIGNED_MEMORY) + { + log("Unaligned halfword read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } + } +#endif + + u32 value; + + switch (address >> 24) + { + case 0: + if (reg[15].I >> 24) + { + if (address < 0x4000) + { +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_READ) + { + log("Illegal halfword read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + value = READ16LE(((u16 *)&biosProtected[address&2])); + } + else + goto unreadable; + } + else + value = READ16LE(((u16 *)&bios[address & 0x3FFE])); + break; + case 2: + value = READ16LE(((u16 *)&workRAM[address & 0x3FFFE])); + break; + case 3: + value = READ16LE(((u16 *)&internalRAM[address & 0x7ffe])); + break; + case 4: + if ((address < 0x4000400) && ioReadable[address & 0x3fe]) + { + if (address >= 0x400012f && address <= 0x4000131) + GBASystemCounters.lagged = false; + value = READ16LE(((u16 *)&ioMem[address & 0x3fe])); + } + else + goto unreadable; + break; + case 5: + value = READ16LE(((u16 *)&paletteRAM[address & 0x3fe])); + break; + case 6: + value = READ16LE(((u16 *)&vram[address & 0x1fffe])); + break; + case 7: + value = READ16LE(((u16 *)&oam[address & 0x3fe])); + break; + case 8: + case 9: + case 10: + case 11: + case 12: + if (address == 0x80000c4 || address == 0x80000c6 || address == 0x80000c8) + value = rtcRead(address); + else + value = READ16LE(((u16 *)&rom[address & 0x1FFFFFE])); + break; + case 13: + if (cpuEEPROMEnabled) + // no need to swap this + return eepromRead(address); + goto unreadable; + case 14: + if (cpuFlashEnabled | cpuSramEnabled) + // no need to swap this + return flashRead(address); + // default + default: +unreadable: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_READ) + { + log("Illegal halfword read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + extern bool8 cpuDmaHack; + extern u32 cpuDmaLast; + extern int32 cpuDmaCount; + if (cpuDmaHack && cpuDmaCount) + { + value = (u16)cpuDmaLast; + } + else + { + if (armState) + { + value = CPUReadHalfWordQuick(reg[15].I + (address & 2)); + } + else + { + value = CPUReadHalfWordQuick(reg[15].I); + } + } + // return value; + // if(address & 1) + // value = (value >> 8) | ((value & 0xFF) << 24); + // return 0xFFFF; + break; + } + + if (address & 1) + { + value = (value >> 8) | (value << 24); + } + + return value; +} + +inline u16 CPUReadHalfWordSigned(u32 address) +{ + u16 value = CPUReadHalfWord(address); + if ((address & 1)) + value = (s8)value; + return value; +} + +inline u8 CPUReadByte(u32 address) +{ + switch (address >> 24) + { + case 0: + if (reg[15].I >> 24) + { + if (address < 0x4000) + { +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_READ) + { + log("Illegal byte read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + return biosProtected[address & 3]; + } + else + goto unreadable; + } + return bios[address & 0x3FFF]; + case 2: + return workRAM[address & 0x3FFFF]; + case 3: + return internalRAM[address & 0x7fff]; + case 4: + if ((address < 0x4000400) && ioReadable[address & 0x3ff]) + { + if (address == 0x4000130 || address == 0x4000131) + GBASystemCounters.lagged = false; + return ioMem[address & 0x3ff]; + } + else + goto unreadable; + case 5: + return paletteRAM[address & 0x3ff]; + case 6: + return vram[address & 0x1ffff]; + case 7: + return oam[address & 0x3ff]; + case 8: + case 9: + case 10: + case 11: + case 12: + return rom[address & 0x1FFFFFF]; + case 13: + if (cpuEEPROMEnabled) + return eepromRead(address); + goto unreadable; + case 14: + if (cpuSramEnabled | cpuFlashEnabled) + return flashRead(address); + if (cpuEEPROMSensorEnabled) + { + switch (address & 0x00008f00) + { + case 0x8200: + return systemGetSensorX() & 255; + case 0x8300: + return (systemGetSensorX() >> 8)|0x80; + case 0x8400: + return systemGetSensorY() & 255; + case 0x8500: + return systemGetSensorY() >> 8; + } + } + // default + default: +unreadable: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_READ) + { + log("Illegal byte read: %08x at %08x\n", address, armMode ? + armNextPC - 4 : armNextPC - 2); + } +#endif + + if (armState) + { + return CPUReadByteQuick(reg[15].I+(address & 3)); + } + else + { + return CPUReadByteQuick(reg[15].I+(address & 1)); + } + // return 0xFF; + break; + } +} + +inline void CPUWriteMemoryWrapped(u32 address, u32 value) +{ +#ifdef GBA_LOGGING + if (address & 3) + { + if (systemVerbose & VERBOSE_UNALIGNED_MEMORY) + { + log("Unaliagned word write: %08x to %08x from %08x\n", + value, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } + } +#endif + + switch (address >> 24) + { + case 0x02: +#ifdef SDL + if (*((u32 *)&freezeWorkRAM[address & 0x3FFFC])) + cheatsWriteMemory((u32 *)&workRAM[address & 0x3FFFC], + value, + *((u32 *)&freezeWorkRAM[address & 0x3FFFC])); + else +#endif + WRITE32LE(((u32 *)&workRAM[address & 0x3FFFC]), value); + break; + case 0x03: +#ifdef SDL + if (*((u32 *)&freezeInternalRAM[address & 0x7ffc])) + cheatsWriteMemory((u32 *)&internalRAM[address & 0x7FFC], + value, + *((u32 *)&freezeInternalRAM[address & 0x7ffc])); + else +#endif + WRITE32LE(((u32 *)&internalRAM[address & 0x7ffC]), value); + break; + case 0x04: + CPUUpdateRegister((address & 0x3FC), value & 0xFFFF); + CPUUpdateRegister((address & 0x3FC) + 2, (value >> 16)); + break; + case 0x05: + WRITE32LE(((u32 *)&paletteRAM[address & 0x3FC]), value); + break; + case 0x06: + if (address & 0x10000) + WRITE32LE(((u32 *)&vram[address & 0x17ffc]), value); + else + WRITE32LE(((u32 *)&vram[address & 0x1fffc]), value); + break; + case 0x07: + WRITE32LE(((u32 *)&oam[address & 0x3fc]), value); + break; + case 0x0D: + if (cpuEEPROMEnabled) + { + eepromWrite(address, value); + break; + } + goto unwritable; + case 0x0E: + if (!eepromInUse | cpuSramEnabled | cpuFlashEnabled) + { + (*cpuSaveGameFunc)(address, (u8)value); + break; + } + // default + default: +unwritable: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_ILLEGAL_WRITE) + { + log("Illegal word write: %08x to %08x from %08x\n", + value, + address, + armMode ? armNextPC - 4 : armNextPC - 2); + } +#endif + break; + } +} + +inline void CPUWriteMemory(u32 address, u32 value) +{ + CPUWriteMemoryWrapped(address, value); + CallRegisteredLuaMemHook(address, 4, value, LUAMEMHOOK_WRITE); +} + +#endif // VBA_GBAINLINE_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +noinst_LIBRARIES = libgba.a + +libgba_a_SOURCES = \ + agbprint.cpp \ + agbprint.h \ + arm-new.h \ + armdis.cpp \ + armdis.h \ + bios.cpp \ + bios.h \ + elf.cpp \ + elf.h \ + Cheats.cpp \ + Cheats.h \ + EEprom.cpp \ + EEprom.h \ + Flash.cpp \ + Flash.h \ + GBA.cpp \ + GBA.h \ + GBAinline.h \ + GBAGfx.cpp \ + GBAGfx.h \ + Globals.cpp \ + Globals.h \ + Mode0.cpp \ + Mode1.cpp \ + Mode2.cpp \ + Mode3.cpp \ + Mode4.cpp \ + Mode5.cpp \ + remote.cpp \ + RTC.cpp \ + RTC.h \ + Sound.cpp \ + Sound.h \ + Sram.cpp \ + Sram.h \ + thumb.h + +AM_CPPFLAGS = \ + -I$(top_srcdir)/src \ + -DSDL \ + -DSYSCONFDIR=\"$(sysconfdir)\" + +AM_CXXFLAGS = -fno-exceptions @SDL_CFLAGS@ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Mode0.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Mode0.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,633 @@ +#include "GBAGfx.h" +#include "GBAGlobals.h" + +void mode0RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + return; + } + + if (layerEnable & 0x0100) + { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if (layerEnable & 0x0200) + { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if (layerEnable & 0x0400) + { + gfxDrawTextScreen(BG2CNT, BG2HOFS, BG2VOFS, line2); + } + + if (layerEnable & 0x0800) + { + gfxDrawTextScreen(BG3CNT, BG3HOFS, BG3VOFS, line3); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + + if (line0[x] < color) + { + color = line0[x]; + top = 0x01; + } + + if ((u8)(line1[x]>>24) < (u8)(color >> 24)) + { + color = line1[x]; + top = 0x02; + } + + if ((u8)(line2[x]>>24) < (u8)(color >> 24)) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(line3[x]>>24) < (u8)(color >> 24)) + { + color = line3[x]; + top = 0x08; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if ((top & 0x10) && (color & 0x00010000)) + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((u8)(line0[x]>>24) < (u8)(back >> 24)) + { + back = line0[x]; + top2 = 0x01; + } + + if ((u8)(line1[x]>>24) < (u8)(back >> 24)) + { + back = line1[x]; + top2 = 0x02; + } + + if ((u8)(line2[x]>>24) < (u8)(back >> 24)) + { + back = line2[x]; + top2 = 0x04; + } + + if ((u8)(line3[x]>>24) < (u8)(back >> 24)) + { + back = line3[x]; + top2 = 0x08; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } +} + +void mode0RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + return; + } + + if (layerEnable & 0x0100) + { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if (layerEnable & 0x0200) + { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if (layerEnable & 0x0400) + { + gfxDrawTextScreen(BG2CNT, BG2HOFS, BG2VOFS, line2); + } + + if (layerEnable & 0x0800) + { + gfxDrawTextScreen(BG3CNT, BG3HOFS, BG3VOFS, line3); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + int effect = (BLDMOD >> 6) & 3; + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + + if (line0[x] < color) + { + color = line0[x]; + top = 0x01; + } + + if (line1[x] < (color & 0xFF000000)) + { + color = line1[x]; + top = 0x02; + } + + if (line2[x] < (color & 0xFF000000)) + { + color = line2[x]; + top = 0x04; + } + + if (line3[x] < (color & 0xFF000000)) + { + color = line3[x]; + top = 0x08; + } + + if (lineOBJ[x] < (color & 0xFF000000)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (!(color & 0x00010000)) + { + switch (effect) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = backdrop; + u8 top2 = 0x20; + if (line0[x] < back) + { + if (top != 0x01) + { + back = line0[x]; + top2 = 0x01; + } + } + + if (line1[x] < (back & 0xFF000000)) + { + if (top != 0x02) + { + back = line1[x]; + top2 = 0x02; + } + } + + if (line2[x] < (back & 0xFF000000)) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if (line3[x] < (back & 0xFF000000)) + { + if (top != 0x08) + { + back = line3[x]; + top2 = 0x08; + } + } + + if (lineOBJ[x] < (back & 0xFF000000)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if (line0[x] < back) + { + back = line0[x]; + top2 = 0x01; + } + + if (line1[x] < (back & 0xFF000000)) + { + back = line1[x]; + top2 = 0x02; + } + + if (line2[x] < (back & 0xFF000000)) + { + back = line2[x]; + top2 = 0x04; + } + + if (line3[x] < (back & 0xFF000000)) + { + back = line3[x]; + top2 = 0x08; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } +} + +void mode0RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if (layerEnable & 0x2000) + { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if (layerEnable & 0x4000) + { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if ((layerEnable & 0x0100)) + { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if ((layerEnable & 0x0200)) + { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if ((layerEnable & 0x0400)) + { + gfxDrawTextScreen(BG2CNT, BG2HOFS, BG2VOFS, line2); + } + + if ((layerEnable & 0x0800)) + { + gfxDrawTextScreen(BG3CNT, BG3HOFS, BG3VOFS, line3); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + u8 mask = outMask; + + if (!(lineOBJWin[x] & 0x80000000)) + { + mask = WINOUT >> 8; + } + + if (inWindow1) + { + if (gfxInWin1[x]) + mask = inWin1Mask; + } + + if (inWindow0) + { + if (gfxInWin0[x]) + { + mask = inWin0Mask; + } + } + + if ((mask & 1) && (line0[x] < color)) + { + color = line0[x]; + top = 0x01; + } + + if ((mask & 2) && ((u8)(line1[x]>>24) < (u8)(color >> 24))) + { + color = line1[x]; + top = 0x02; + } + + if ((mask & 4) && ((u8)(line2[x]>>24) < (u8)(color >> 24))) + { + color = line2[x]; + top = 0x04; + } + + if ((mask & 8) && ((u8)(line3[x]>>24) < (u8)(color >> 24))) + { + color = line3[x]; + top = 0x08; + } + + if ((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24))) + { + color = lineOBJ[x]; + top = 0x10; + } + + // special FX on in the window + if (mask & 32) + { + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = backdrop; + u8 top2 = 0x20; + if ((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x01) + { + back = line0[x]; + top2 = 0x01; + } + } + + if ((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x02) + { + back = line1[x]; + top2 = 0x02; + } + } + + if ((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((mask & 8) && (u8)(line3[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x08) + { + back = line3[x]; + top2 = 0x08; + } + } + + if ((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 1) && ((u8)(line0[x]>>24) < (u8)(back >> 24))) + { + back = line0[x]; + top2 = 0x01; + } + + if ((mask & 2) && ((u8)(line1[x]>>24) < (u8)(back >> 24))) + { + back = line1[x]; + top2 = 0x02; + } + + if ((mask & 4) && ((u8)(line2[x]>>24) < (u8)(back >> 24))) + { + back = line2[x]; + top2 = 0x04; + } + + if ((mask & 8) && ((u8)(line3[x]>>24) < (u8)(back >> 24))) + { + back = line3[x]; + top2 = 0x08; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } + else if (color & 0x00010000) + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 1) && ((u8)(line0[x]>>24) < (u8)(back >> 24))) + { + back = line0[x]; + top2 = 0x01; + } + + if ((mask & 2) && ((u8)(line1[x]>>24) < (u8)(back >> 24))) + { + back = line1[x]; + top2 = 0x02; + } + + if ((mask & 4) && ((u8)(line2[x]>>24) < (u8)(back >> 24))) + { + back = line2[x]; + top2 = 0x04; + } + + if ((mask & 8) && ((u8)(line3[x]>>24) < (u8)(back >> 24))) + { + back = line3[x]; + top2 = 0x08; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Mode1.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Mode1.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,580 @@ +#include "GBAGfx.h" +#include "GBAGlobals.h" + +void mode1RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if (layerEnable & 0x0100) + { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if (layerEnable & 0x0200) + { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, line2); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + + if (line0[x] < color) + { + color = line0[x]; + top = 0x01; + } + + if ((u8)(line1[x]>>24) < (u8)(color >> 24)) + { + color = line1[x]; + top = 0x02; + } + + if ((u8)(line2[x]>>24) < (u8)(color >> 24)) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if ((top & 0x10) && (color & 0x00010000)) + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((u8)(line0[x]>>24) < (u8)(back >> 24)) + { + back = line0[x]; + top2 = 0x01; + } + + if ((u8)(line1[x]>>24) < (u8)(back >> 24)) + { + back = line1[x]; + top2 = 0x02; + } + + if ((u8)(line2[x]>>24) < (u8)(back >> 24)) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode1RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if (layerEnable & 0x0100) + { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if (layerEnable & 0x0200) + { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, line2); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + + if (line0[x] < color) + { + color = line0[x]; + top = 0x01; + } + + if ((u8)(line1[x]>>24) < (u8)(color >> 24)) + { + color = line1[x]; + top = 0x02; + } + + if ((u8)(line2[x]>>24) < (u8)(color >> 24)) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = backdrop; + u8 top2 = 0x20; + if ((u8)(line0[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x01) + { + back = line0[x]; + top2 = 0x01; + } + } + + if ((u8)(line1[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x02) + { + back = line1[x]; + top2 = 0x02; + } + } + + if ((u8)(line2[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((u8)(line0[x]>>24) < (u8)(back >> 24)) + { + back = line0[x]; + top2 = 0x01; + } + + if ((u8)(line1[x]>>24) < (u8)(back >> 24)) + { + back = line1[x]; + top2 = 0x02; + } + + if ((u8)(line2[x]>>24) < (u8)(back >> 24)) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode1RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if (layerEnable & 0x2000) + { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if (layerEnable & 0x4000) + { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if (layerEnable & 0x0100) + { + gfxDrawTextScreen(BG0CNT, BG0HOFS, BG0VOFS, line0); + } + + if (layerEnable & 0x0200) + { + gfxDrawTextScreen(BG1CNT, BG1HOFS, BG1VOFS, line1); + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, line2); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + u8 mask = outMask; + + if (!(lineOBJWin[x] & 0x80000000)) + { + mask = WINOUT >> 8; + } + + if (inWindow1) + { + if (gfxInWin1[x]) + mask = inWin1Mask; + } + + if (inWindow0) + { + if (gfxInWin0[x]) + { + mask = inWin0Mask; + } + } + + if (line0[x] < color && (mask & 1)) + { + color = line0[x]; + top = 0x01; + } + + if ((u8)(line1[x]>>24) < (u8)(color >> 24) && (mask & 2)) + { + color = line1[x]; + top = 0x02; + } + + if ((u8)(line2[x]>>24) < (u8)(color >> 24) && (mask & 4)) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24) && (mask & 16)) + { + color = lineOBJ[x]; + top = 0x10; + } + + // special FX on the window + if (mask & 32) + { + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = backdrop; + u8 top2 = 0x20; + if ((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x01) + { + back = line0[x]; + top2 = 0x01; + } + } + + if ((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x02) + { + back = line1[x]; + top2 = 0x02; + } + } + + if ((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24)) + { + back = line0[x]; + top2 = 0x01; + } + + if ((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24)) + { + back = line1[x]; + top2 = 0x02; + } + + if ((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24)) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } + else if (color & 0x00010000) + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 1) && (u8)(line0[x]>>24) < (u8)(back >> 24)) + { + back = line0[x]; + top2 = 0x01; + } + + if ((mask & 2) && (u8)(line1[x]>>24) < (u8)(back >> 24)) + { + back = line1[x]; + top2 = 0x02; + } + + if ((mask & 4) && (u8)(line2[x]>>24) < (u8)(back >> 24)) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Mode2.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Mode2.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,530 @@ +#include "GBAGfx.h" +#include "GBAGlobals.h" + +void mode2RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, gfxBG2X, gfxBG2Y, + changed, line2); + } + + if (layerEnable & 0x0800) + { + int changed = gfxBG3Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG3CNT, BG3X_L, BG3X_H, BG3Y_L, BG3Y_H, + BG3PA, BG3PB, BG3PC, BG3PD, gfxBG3X, gfxBG3Y, + changed, line3); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + + if ((u8)(line2[x]>>24) < (u8)(color >> 24)) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(line3[x]>>24) < (u8)(color >> 24)) + { + color = line3[x]; + top = 0x08; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if ((top & 0x10) && (color & 0x00010000)) + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((u8)(line2[x]>>24) < (u8)(back >> 24)) + { + back = line2[x]; + top2 = 0x04; + } + + if ((u8)(line3[x]>>24) < (u8)(back >> 24)) + { + back = line3[x]; + top2 = 0x08; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxBG3Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode2RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, gfxBG2X, gfxBG2Y, + changed, line2); + } + + if (layerEnable & 0x0800) + { + int changed = gfxBG3Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG3CNT, BG3X_L, BG3X_H, BG3Y_L, BG3Y_H, + BG3PA, BG3PB, BG3PC, BG3PD, gfxBG3X, gfxBG3Y, + changed, line3); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + + if ((u8)(line2[x]>>24) < (u8)(color >> 24)) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(line3[x]>>24) < (u8)(color >> 24)) + { + color = line3[x]; + top = 0x08; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = backdrop; + u8 top2 = 0x20; + + if ((u8)(line2[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((u8)(line3[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x08) + { + back = line3[x]; + top2 = 0x08; + } + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((u8)(line2[x]>>24) < (u8)(back >> 24)) + { + back = line2[x]; + top2 = 0x04; + } + + if ((u8)(line3[x]>>24) < (u8)(back >> 24)) + { + back = line3[x]; + top2 = 0x08; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxBG3Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode2RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if (layerEnable & 0x2000) + { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if (layerEnable & 0x4000) + { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, gfxBG2X, gfxBG2Y, + changed, line2); + } + + if (layerEnable & 0x0800) + { + int changed = gfxBG3Changed; + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen(BG3CNT, BG3X_L, BG3X_H, BG3Y_L, BG3Y_H, + BG3PA, BG3PB, BG3PC, BG3PD, gfxBG3X, gfxBG3Y, + changed, line3); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + u8 mask = outMask; + + if (!(lineOBJWin[x] & 0x80000000)) + { + mask = WINOUT >> 8; + } + + if (inWindow1) + { + if (gfxInWin1[x]) + mask = inWin1Mask; + } + + if (inWindow0) + { + if (gfxInWin0[x]) + { + mask = inWin0Mask; + } + } + + if (line2[x] < color && (mask & 4)) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(line3[x]>>24) < (u8)(color >> 24) && (mask & 8)) + { + color = line3[x]; + top = 0x08; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24) && (mask & 16)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (mask & 32) + { + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((mask & 8) && (u8)(line3[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x08) + { + back = line3[x]; + top2 = 0x08; + } + } + + if ((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if ((mask & 8) && (u8)(line3[x]>>24) < (u8)(back >> 24)) + { + back = line3[x]; + top2 = 0x08; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } + else if (color & 0x00010000) + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if ((mask & 8) && (u8)(line3[x]>>24) < (u8)(back >> 24)) + { + back = line3[x]; + top2 = 0x08; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxBG3Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Mode3.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Mode3.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,443 @@ +#include "GBAGfx.h" +#include "GBAGlobals.h" + +void mode3RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = background; + u8 top = 0x20; + + if (line2[x] < color) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >>24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if ((top & 0x10) && (color & 0x00010000)) + { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if (line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode3RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = background; + u8 top = 0x20; + + if (line2[x] < color) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >>24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = background; + u8 top2 = 0x20; + + if (line2[x] < back) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if (line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode3RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x80) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if (layerEnable & 0x2000) + { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if (layerEnable & 0x4000) + { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = background; + u8 top = 0x20; + u8 mask = outMask; + + if (!(lineOBJWin[x] & 0x80000000)) + { + mask = WINOUT >> 8; + } + + if (inWindow1) + { + if (gfxInWin1[x]) + mask = inWin1Mask; + } + + if (inWindow0) + { + if (gfxInWin0[x]) + { + mask = inWin0Mask; + } + } + + if ((mask & 4) && (line2[x] < color)) + { + color = line2[x]; + top = 0x04; + } + + if ((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >>24))) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (mask & 32) + { + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = background; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } + else if (color & 0x00010000) + { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Mode4.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Mode4.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,440 @@ +#include "GBAGfx.h" +#include "GBAGlobals.h" + +void mode4RenderLine() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x0080) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if (layerEnable & 0x400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen256(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + + if (line2[x] < color) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if ((top & 0x10) && (color & 0x00010000)) + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if (line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode4RenderLineNoWindow() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x0080) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + if (layerEnable & 0x400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen256(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + + if (line2[x] < color) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >> 24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = backdrop; + u8 top2 = 0x20; + + if (line2[x] < back) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if (line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode4RenderLineAll() +{ + u16 *palette = (u16 *)paletteRAM; + + if (DISPCNT & 0x0080) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + bool inWindow0 = false; + bool inWindow1 = false; + + if (layerEnable & 0x2000) + { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if (layerEnable & 0x4000) + { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + if (layerEnable & 0x400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen256(BG2CNT, BG2X_L, BG2X_H, BG2Y_L, BG2Y_H, + BG2PA, BG2PB, BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + u32 backdrop = (READ16LE(&palette[0]) | 0x30000000); + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + for (int x = 0; x < 240; x++) + { + u32 color = backdrop; + u8 top = 0x20; + u8 mask = outMask; + + if (!(lineOBJWin[x] & 0x80000000)) + { + mask = WINOUT >> 8; + } + + if (inWindow1) + { + if (gfxInWin1[x]) + mask = inWin1Mask; + } + + if (inWindow0) + { + if (gfxInWin0[x]) + { + mask = inWin0Mask; + } + } + + if ((mask & 4) && (line2[x] < color)) + { + color = line2[x]; + top = 0x04; + } + + if ((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >>24))) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (mask & 32) + { + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } + else if (color & 0x00010000) + { + // semi-transparent OBJ + u32 back = backdrop; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Mode5.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Mode5.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,443 @@ +#include "GBAGfx.h" +#include "GBAGlobals.h" + +void mode5RenderLine() +{ + if (DISPCNT & 0x0080) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + u16 *palette = (u16 *)paletteRAM; + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit160(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = background; + u8 top = 0x20; + + if (line2[x] < color) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >>24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if ((top & 0x10) && (color & 0x00010000)) + { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if (line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode5RenderLineNoWindow() +{ + if (DISPCNT & 0x0080) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + u16 *palette = (u16 *)paletteRAM; + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit160(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = background; + u8 top = 0x20; + + if (line2[x] < color) + { + color = line2[x]; + top = 0x04; + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(color >>24)) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = background; + u8 top2 = 0x20; + + if (line2[x] < back) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if (line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + +void mode5RenderLineAll() +{ + if (DISPCNT & 0x0080) + { + for (int x = 0; x < 240; x++) + { + lineMix[x] = 0x7fff; + } + gfxLastVCOUNT = VCOUNT; + return; + } + + u16 *palette = (u16 *)paletteRAM; + + if (layerEnable & 0x0400) + { + int changed = gfxBG2Changed; + + if (gfxLastVCOUNT > VCOUNT) + changed = 3; + + gfxDrawRotScreen16Bit160(BG2CNT, BG2X_L, BG2X_H, + BG2Y_L, BG2Y_H, BG2PA, BG2PB, + BG2PC, BG2PD, + gfxBG2X, gfxBG2Y, changed, + line2); + } + + gfxDrawSprites(lineOBJ); + gfxDrawOBJWin(lineOBJWin); + + bool inWindow0 = false; + bool inWindow1 = false; + + if (layerEnable & 0x2000) + { + u8 v0 = WIN0V >> 8; + u8 v1 = WIN0V & 255; + inWindow0 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow0 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow0 |= (VCOUNT >= v0 || VCOUNT < v1); + } + if (layerEnable & 0x4000) + { + u8 v0 = WIN1V >> 8; + u8 v1 = WIN1V & 255; + inWindow1 = ((v0 == v1) && (v0 >= 0xe8)); + if (v1 >= v0) + inWindow1 |= (VCOUNT >= v0 && VCOUNT < v1); + else + inWindow1 |= (VCOUNT >= v0 || VCOUNT < v1); + } + + u8 inWin0Mask = WININ & 0xFF; + u8 inWin1Mask = WININ >> 8; + u8 outMask = WINOUT & 0xFF; + + u32 background = (READ16LE(&palette[0]) | 0x30000000); + + for (int x = 0; x < 240; x++) + { + u32 color = background; + u8 top = 0x20; + u8 mask = outMask; + + if (!(lineOBJWin[x] & 0x80000000)) + { + mask = WINOUT >> 8; + } + + if (inWindow1) + { + if (gfxInWin1[x]) + mask = inWin1Mask; + } + + if (inWindow0) + { + if (gfxInWin0[x]) + { + mask = inWin0Mask; + } + } + + if ((mask & 4) && (line2[x] < color)) + { + color = line2[x]; + top = 0x04; + } + + if ((mask & 16) && ((u8)(lineOBJ[x]>>24) < (u8)(color >>24))) + { + color = lineOBJ[x]; + top = 0x10; + } + + if (mask & 32) + { + if (!(color & 0x00010000)) + { + switch ((BLDMOD >> 6) & 3) + { + case 0: + break; + case 1: + { + if (top & BLDMOD) + { + u32 back = background; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + if (top != 0x04) + { + back = line2[x]; + top2 = 0x04; + } + } + + if ((mask & 16) && (u8)(lineOBJ[x]>>24) < (u8)(back >> 24)) + { + if (top != 0x10) + { + back = lineOBJ[x]; + top2 = 0x10; + } + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + } + break; + } + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + else + { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + } + else if (color & 0x00010000) + { + // semi-transparent OBJ + u32 back = background; + u8 top2 = 0x20; + + if ((mask & 4) && line2[x] < back) + { + back = line2[x]; + top2 = 0x04; + } + + if (top2 & (BLDMOD>>8)) + color = gfxAlphaBlend(color, back, + coeff[COLEV & 0x1F], + coeff[(COLEV >> 8) & 0x1F]); + else + { + switch ((BLDMOD >> 6) & 3) + { + case 2: + if (BLDMOD & top) + color = gfxIncreaseBrightness(color, coeff[COLY & 0x1F]); + break; + case 3: + if (BLDMOD & top) + color = gfxDecreaseBrightness(color, coeff[COLY & 0x1F]); + break; + } + } + } + + lineMix[x] = color; + } + gfxBG2Changed = 0; + gfxLastVCOUNT = VCOUNT; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/RTC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/RTC.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,240 @@ +#include + +#include "../Port.h" +#include "../NLS.h" +#include "../common/System.h" // systemMessage +#include "../common/Util.h" +#include "../common/movie.h" +#include "GBAGlobals.h" + +enum RTCSTATE { IDLE, COMMAND, DATA, READDATA }; + +typedef struct +{ + u8 byte0; + u8 byte1; + u8 byte2; + u8 command; + int dataLen; + int bits; + RTCSTATE state; + u8 data[12]; + // reserved variables for future + u8 reserved[12]; + bool reserved2; + u32 reserved3; +} RTCCLOCKDATA; + +static RTCCLOCKDATA rtcClockData; +static bool rtcEnabled = false; + +void rtcEnable(bool enable) +{ + rtcEnabled = enable; +} + +bool rtcIsEnabled() +{ + return rtcEnabled; +} + +u16 rtcRead(u32 address) +{ + if (rtcEnabled) + { + if (address == 0x80000c8) + return rtcClockData.byte2; + else if (address == 0x80000c6) + return rtcClockData.byte1; + else if (address == 0x80000c4) + { + return rtcClockData.byte0; + } + } + + return READ16LE((&rom[address & 0x1FFFFFE])); +} + +static u8 toBCD(u8 value) +{ + value = value % 100; + int l = value % 10; + int h = value / 10; + return h * 16 + l; +} + +bool rtcWrite(u32 address, u16 value) +{ + if (!rtcEnabled) + return false; + + if (address == 0x80000c8) + { + rtcClockData.byte2 = (u8)value; // enable ? + } + else if (address == 0x80000c6) + { + rtcClockData.byte1 = (u8)value; // read/write + } + else if (address == 0x80000c4) + { + if (rtcClockData.byte2 & 1) + { + if (rtcClockData.state == IDLE && rtcClockData.byte0 == 1 && value == 5) + { + rtcClockData.state = COMMAND; + rtcClockData.bits = 0; + rtcClockData.command = 0; + } + else if (!(rtcClockData.byte0 & 1) && (value & 1)) // bit transfer + { + rtcClockData.byte0 = (u8)value; + switch (rtcClockData.state) + { + case COMMAND: + rtcClockData.command |= ((value & 2) >> 1) << (7-rtcClockData.bits); + rtcClockData.bits++; + if (rtcClockData.bits == 8) + { + rtcClockData.bits = 0; + switch (rtcClockData.command) + { + case 0x60: + // not sure what this command does but it doesn't take parameters + // maybe it is a reset or stop + rtcClockData.state = IDLE; + rtcClockData.bits = 0; + break; + case 0x62: + // this sets the control state but not sure what those values are + rtcClockData.state = READDATA; + rtcClockData.dataLen = 1; + break; + case 0x63: + rtcClockData.dataLen = 1; + rtcClockData.data[0] = 0x40; + rtcClockData.state = DATA; + break; + case 0x65: + { + struct tm *newtime; + time_t long_time; + + if (VBAMovieActive() || VBAMovieLoading()) + { + long_time = VBAMovieGetId() + VBAMovieGetFrameCounter()/60; + newtime = gmtime(&long_time); + } + else + { + time(&long_time); /* Get time as long integer. */ + newtime = localtime(&long_time); /* Convert to local time. */ + } + + rtcClockData.dataLen = 7; + rtcClockData.data[0] = toBCD(newtime->tm_year); + rtcClockData.data[1] = toBCD(newtime->tm_mon+1); + rtcClockData.data[2] = toBCD(newtime->tm_mday); + rtcClockData.data[3] = 0; + rtcClockData.data[4] = toBCD(newtime->tm_hour); + rtcClockData.data[5] = toBCD(newtime->tm_min); + rtcClockData.data[6] = toBCD(newtime->tm_sec); + rtcClockData.state = DATA; + break; + } + case 0x67: + { + struct tm *newtime; + time_t long_time; + + if (VBAMovieActive() || VBAMovieLoading()) + { + long_time = VBAMovieGetId() + VBAMovieGetFrameCounter()/60; + newtime = gmtime(&long_time); + } + else + { + time(&long_time); /* Get time as long integer. */ + newtime = localtime(&long_time); /* Convert to local time. */ + } + + rtcClockData.dataLen = 3; + rtcClockData.data[0] = toBCD(newtime->tm_hour); + rtcClockData.data[1] = toBCD(newtime->tm_min); + rtcClockData.data[2] = toBCD(newtime->tm_sec); + rtcClockData.state = DATA; + break; + } + default: + systemMessage(0, N_("Unknown RTC command %02x"), rtcClockData.command); + rtcClockData.state = IDLE; + break; + } + } + break; + case DATA: + if (rtcClockData.byte1 & 2) + {} + else + { + rtcClockData.byte0 = (rtcClockData.byte0 & ~2) | + ((rtcClockData.data[rtcClockData.bits >> 3] >> + (rtcClockData.bits & 7)) & 1)*2; + rtcClockData.bits++; + if (rtcClockData.bits == 8*rtcClockData.dataLen) + { + rtcClockData.bits = 0; + rtcClockData.state = IDLE; + } + } + break; + case READDATA: + if (!(rtcClockData.byte1 & 2)) + {} + else + { + rtcClockData.data[rtcClockData.bits >> 3] = + (rtcClockData.data[rtcClockData.bits >> 3] >> 1) | + ((value << 6) & 128); + rtcClockData.bits++; + if (rtcClockData.bits == 8*rtcClockData.dataLen) + { + rtcClockData.bits = 0; + rtcClockData.state = IDLE; + } + } + break; + default: + break; + } + } + else + rtcClockData.byte0 = (u8)value; + } + } + return true; +} + +void rtcReset() +{ + memset(&rtcClockData, 0, sizeof(rtcClockData)); + + rtcClockData.byte0 = 0; + rtcClockData.byte1 = 0; + rtcClockData.byte2 = 0; + rtcClockData.command = 0; + rtcClockData.dataLen = 0; + rtcClockData.bits = 0; + rtcClockData.state = IDLE; +} + +void rtcSaveGame(gzFile gzFile) +{ + utilGzWrite(gzFile, &rtcClockData, sizeof(rtcClockData)); +} + +void rtcReadGame(gzFile gzFile) +{ + utilGzRead(gzFile, &rtcClockData, sizeof(rtcClockData)); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/RTC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/RTC.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,17 @@ +#ifndef VBA_RTC_H +#define VBA_RTC_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +extern u16 rtcRead(u32 address); +extern bool rtcWrite(u32 address, u16 value); +extern void rtcEnable(bool); +extern bool rtcIsEnabled(); +extern void rtcReset(); + +extern void rtcReadGame(gzFile gzFile); +extern void rtcSaveGame(gzFile gzFile); + +#endif // VBA_RTC_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Sram.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Sram.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,15 @@ +#include "../common/System.h" +#include "Flash.h" +#include "Sram.h" + +u8 sramRead(u32 address) +{ + return flashSaveMemory[address & 0xFFFF]; +} + +void sramWrite(u32 address, u8 byte) +{ + flashSaveMemory[address & 0xFFFF] = byte; + systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/Sram.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/Sram.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,11 @@ +#ifndef VBA_SRAM_H +#define VBA_SRAM_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +extern u8 sramRead(u32 address); +extern void sramWrite(u32 address, u8 byte); + +#endif // VBA_SRAM_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/agbprint.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/agbprint.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,82 @@ +#include +#include + +#include "GBAGlobals.h" + +extern void (*dbgOutput)(char *, u32); +extern int systemVerbose; + +static bool agbPrintEnabled = false; +static bool agbPrintProtect = false; + +bool agbPrintWrite(u32 address, u16 value) +{ + if (agbPrintEnabled) + { + if (address == 0x9fe2ffe) // protect + { + agbPrintProtect = (value != 0); + debuggerWriteHalfWord(address, value); + return true; + } + else + { + if (agbPrintProtect && + ((address >= 0x9fe20f8 && address <= 0x9fe20ff) // control structure + || (address >= 0x8fd0000 && address <= 0x8fdffff) + || (address >= 0x9fd0000 && address <= 0x9fdffff))) + { + debuggerWriteHalfWord(address, value); + return true; + } + } + } + return false; +} + +void agbPrintReset() +{ + agbPrintProtect = false; +} + +void agbPrintEnable(bool enable) +{ + agbPrintEnabled = enable; +} + +bool agbPrintIsEnabled() +{ + return agbPrintEnabled; +} + +void agbPrintFlush() +{ + u16 get = debuggerReadHalfWord(0x9fe20fc); + u16 put = debuggerReadHalfWord(0x9fe20fe); + + u32 address = (debuggerReadHalfWord(0x9fe20fa) << 16); + if (address != 0xfd0000 && address != 0x1fd0000) + { + dbgOutput("Did you forget to call AGBPrintInit?\n", 0); + // get rid of the text otherwise we will continue to be called + debuggerWriteHalfWord(0x9fe20fc, put); + return; + } + + u8 *data = &rom[address]; + + while (get != put) + { + char c = data[get++]; + char s[2]; + s[0] = c; + s[1] = 0; + + if (systemVerbose & VERBOSE_AGBPRINT) + dbgOutput(s, 0); + if (c == '\n') + break; + } + debuggerWriteHalfWord(0x9fe20fc, get); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/agbprint.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/agbprint.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +#ifndef VBA_AGBPRINT_H +#define VBA_AGBPRINT_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +extern void agbPrintEnable(bool); +extern bool agbPrintIsEnabled(); +extern void agbPrintReset(); +extern bool agbPrintWrite(u32, u16); +extern void agbPrintFlush(); + +#endif // VBA_AGBPRINT_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/arm-new.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/arm-new.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,7453 @@ +#ifdef BKPT_SUPPORT +#define CONSOLE_OUTPUT(a, b) \ + extern void (*dbgOutput)(char *, u32); \ + if ((opcode == 0xe0000000) && (reg[0].I == 0xC0DED00D)) { \ + dbgOutput((a), (b)); \ + } +#else +#define CONSOLE_OUTPUT(a, b) +#endif + +#define OP_AND \ + reg[dest].I = reg[(opcode >> 16) & 15].I & value; \ + CONSOLE_OUTPUT(NULL, reg[2].I); + +#define OP_ANDS \ + reg[dest].I = reg[(opcode >> 16) & 15].I & value; \ + \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; \ + Z_FLAG = (reg[dest].I) ? false : true; \ + C_FLAG = C_OUT; + +#define OP_EOR \ + reg[dest].I = reg[(opcode >> 16) & 15].I ^ value; + +#define OP_EORS \ + reg[dest].I = reg[(opcode >> 16) & 15].I ^ value; \ + \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; \ + Z_FLAG = (reg[dest].I) ? false : true; \ + C_FLAG = C_OUT; +#ifdef C_CORE +#define NEG(i) ((i) >> 31) +#define POS(i) ((~(i)) >> 31) +#define ADDCARRY(a, b, c) \ + C_FLAG = ((NEG(a) & NEG(b)) | \ + (NEG(a) & POS(c)) | \ + (NEG(b) & POS(c))) ? true : false; +#define ADDOVERFLOW(a, b, c) \ + V_FLAG = ((NEG(a) & NEG(b) & POS(c)) | \ + (POS(a) & POS(b) & NEG(c))) ? true : false; +#define SUBCARRY(a, b, c) \ + C_FLAG = ((NEG(a) & POS(b)) | \ + (NEG(a) & POS(c)) | \ + (POS(b) & POS(c))) ? true : false; +#define SUBOVERFLOW(a, b, c) \ + V_FLAG = ((NEG(a) & POS(b) & POS(c)) | \ + (POS(a) & NEG(b) & NEG(c))) ? true : false; +#define OP_SUB \ + { \ + reg[dest].I = reg[base].I - value; \ + } +#define OP_SUBS \ + { \ + u32 lhs = reg[base].I; \ + u32 rhs = value; \ + u32 res = lhs - rhs; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#define OP_RSB \ + { \ + reg[dest].I = value - reg[base].I; \ + } +#define OP_RSBS \ + { \ + u32 lhs = reg[base].I; \ + u32 rhs = value; \ + u32 res = rhs - lhs; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(rhs, lhs, res); \ + SUBOVERFLOW(rhs, lhs, res); \ + } +#define OP_ADD \ + { \ + reg[dest].I = reg[base].I + value; \ + } +#define OP_ADDS \ + { \ + u32 lhs = reg[base].I; \ + u32 rhs = value; \ + u32 res = lhs + rhs; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + ADDCARRY(lhs, rhs, res); \ + ADDOVERFLOW(lhs, rhs, res); \ + } +#define OP_ADC \ + { \ + reg[dest].I = reg[base].I + value + (u32)C_FLAG; \ + } +#define OP_ADCS \ + { \ + u32 lhs = reg[base].I; \ + u32 rhs = value; \ + u32 res = lhs + rhs + (u32)C_FLAG; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + ADDCARRY(lhs, rhs, res); \ + ADDOVERFLOW(lhs, rhs, res); \ + } +#define OP_SBC \ + { \ + reg[dest].I = reg[base].I - value - !((u32)C_FLAG); \ + } +#define OP_SBCS \ + { \ + u32 lhs = reg[base].I; \ + u32 rhs = value; \ + u32 res = lhs - rhs - !((u32)C_FLAG); \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#define OP_RSC \ + { \ + reg[dest].I = value - reg[base].I - !((u32)C_FLAG); \ + } +#define OP_RSCS \ + { \ + u32 lhs = reg[base].I; \ + u32 rhs = value; \ + u32 res = rhs - lhs - !((u32)C_FLAG); \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(rhs, lhs, res); \ + SUBOVERFLOW(rhs, lhs, res); \ + } +#define OP_CMP \ + { \ + u32 lhs = reg[base].I; \ + u32 rhs = value; \ + u32 res = lhs - rhs; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#define OP_CMN \ + { \ + u32 lhs = reg[base].I; \ + u32 rhs = value; \ + u32 res = lhs + rhs; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + ADDCARRY(lhs, rhs, res); \ + ADDOVERFLOW(lhs, rhs, res); \ + } + +#define LOGICAL_LSL_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + C_OUT = (v >> (32 - shift)) & 1 ? true : false; \ + value = v << shift; \ + } +#define LOGICAL_LSR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + C_OUT = (v >> (shift - 1)) & 1 ? true : false; \ + value = v >> shift; \ + } +#define LOGICAL_ASR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + C_OUT = ((s32)v >> (int)(shift - 1)) & 1 ? true : false; \ + value = (s32)v >> (int)shift; \ + } +#define LOGICAL_ROR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + C_OUT = (v >> (shift - 1)) & 1 ? true : false; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } +#define LOGICAL_RRX_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + shift = (int)C_FLAG; \ + C_OUT = (v & 1) ? true : false; \ + value = ((v >> 1) | \ + (shift << 31)); \ + } +#define LOGICAL_ROR_IMM \ + { \ + u32 v = opcode & 0xff; \ + C_OUT = (v >> (shift - 1)) & 1 ? true : false; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } +#define ARITHMETIC_LSL_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + value = v << shift; \ + } +#define ARITHMETIC_LSR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + value = v >> shift; \ + } +#define ARITHMETIC_ASR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + value = (s32)v >> (int)shift; \ + } +#define ARITHMETIC_ROR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } +#define ARITHMETIC_RRX_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + shift = (int)C_FLAG; \ + value = ((v >> 1) | \ + (shift << 31)); \ + } +#define ARITHMETIC_ROR_IMM \ + { \ + u32 v = opcode & 0xff; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } +#define ROR_IMM_MSR \ + { \ + u32 v = opcode & 0xff; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } +#define ROR_VALUE \ + { \ + value = ((value << (32 - shift)) | \ + (value >> shift)); \ + } +#define RCR_VALUE \ + { \ + shift = (int)C_FLAG; \ + value = ((value >> 1) | \ + (shift << 31)); \ + } +#else +#ifdef __GNUC__ + #ifdef __POWERPC__ + #define OP_SUB \ + { \ + reg[dest].I = reg[base].I - value; \ + } + #define OP_SUBS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_RSB \ + { \ + reg[dest].I = value - reg[base].I; \ + } + #define OP_RSBS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("subfco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_ADD \ + { \ + reg[dest].I = reg[base].I + value; \ + } + + #define OP_ADDS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_ADC \ + { \ + reg[dest].I = reg[base].I + value + (u32)C_FLAG; \ + } + #define OP_ADCS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("mtspr xer, %4\n" \ + "addeo. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_SBC \ + { \ + reg[dest].I = reg[base].I - value - (C_FLAG ^ 1); \ + } + #define OP_SBCS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("mtspr xer, %4\n" \ + "subfeo. %0, %3, %2\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_RSC \ + { \ + reg[dest].I = value - reg[base].I - (C_FLAG ^ 1); \ + } + #define OP_RSCS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("mtspr xer, %4\n" \ + "subfeo. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_CMP \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define OP_CMN \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[base].I), \ + "r" (value) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + + #define LOGICAL_LSL_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + C_OUT = (v >> (32 - shift)) & 1 ? true : false; \ + value = v << shift; \ + } + #define LOGICAL_LSR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + C_OUT = (v >> (shift - 1)) & 1 ? true : false; \ + value = v >> shift; \ + } + #define LOGICAL_ASR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + C_OUT = ((s32)v >> (int)(shift - 1)) & 1 ? true : false; \ + value = (s32)v >> (int)shift; \ + } + #define LOGICAL_ROR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + C_OUT = (v >> (shift - 1)) & 1 ? true : false; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } + #define LOGICAL_RRX_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + shift = (int)C_FLAG; \ + C_OUT = (v & 1) ? true : false; \ + value = ((v >> 1) | \ + (shift << 31)); \ + } + #define LOGICAL_ROR_IMM \ + { \ + u32 v = opcode & 0xff; \ + C_OUT = (v >> (shift - 1)) & 1 ? true : false; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } + #define ARITHMETIC_LSL_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + value = v << shift; \ + } + #define ARITHMETIC_LSR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + value = v >> shift; \ + } + #define ARITHMETIC_ASR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + value = (s32)v >> (int)shift; \ + } + #define ARITHMETIC_ROR_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } + #define ARITHMETIC_RRX_REG \ + { \ + u32 v = reg[opcode & 0x0f].I; \ + shift = (int)C_FLAG; \ + value = ((v >> 1) | \ + (shift << 31)); \ + } + #define ARITHMETIC_ROR_IMM \ + { \ + u32 v = opcode & 0xff; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } + #define ROR_IMM_MSR \ + { \ + u32 v = opcode & 0xff; \ + value = ((v << (32 - shift)) | \ + (v >> shift)); \ + } + #define ROR_VALUE \ + { \ + value = ((value << (32 - shift)) | \ + (value >> shift)); \ + } + #define RCR_VALUE \ + { \ + shift = (int)C_FLAG; \ + value = ((value >> 1) | \ + (shift << 31)); \ + } +#else +#define OP_SUB \ + asm ("sub %1, %%ebx;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[base].I)); + +#define OP_SUBS \ + asm ("sub %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[base].I)); + +#define OP_RSB \ + asm ("sub %1, %%ebx;" \ + : "=b" (reg[dest].I) \ + : "r" (reg[base].I), "b" (value)); + +#define OP_RSBS \ + asm ("sub %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (reg[base].I), "b" (value)); + +#define OP_ADD \ + asm ("add %1, %%ebx;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[base].I)); + +#define OP_ADDS \ + asm ("add %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setcb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[base].I)); + +#define OP_ADC \ + asm ("bt $0, C_FLAG;" \ + "adc %1, %%ebx;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[base].I)); + +#define OP_ADCS \ + asm ("bt $0, C_FLAG;" \ + "adc %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setcb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[base].I)); + +#define OP_SBC \ + asm ("bt $0, C_FLAG;" \ + "cmc;" \ + "sbb %1, %%ebx;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[base].I)); + +#define OP_SBCS \ + asm ("bt $0, C_FLAG;" \ + "cmc;" \ + "sbb %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[base].I)); +#define OP_RSC \ + asm ("bt $0, C_FLAG;" \ + "cmc;" \ + "sbb %1, %%ebx;" \ + : "=b" (reg[dest].I) \ + : "r" (reg[base].I), "b" (value)); + +#define OP_RSCS \ + asm ("bt $0, C_FLAG;" \ + "cmc;" \ + "sbb %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (reg[base].I), "b" (value)); +#define OP_CMP \ + asm ("sub %0, %1;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : \ + : "r" (value), "r" (reg[base].I)); + +#define OP_CMN \ + asm ("add %0, %1;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setcb C_FLAG;" \ + "setob V_FLAG;" \ + : \ + : "r" (value), "r" (reg[base].I)); +#define LOGICAL_LSL_REG \ + asm ("shl %%cl, %%eax;" \ + "setcb %%cl;" \ + : "=a" (value), "=c" (C_OUT) \ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define LOGICAL_LSR_REG \ + asm ("shr %%cl, %%eax;" \ + "setcb %%cl;" \ + : "=a" (value), "=c" (C_OUT) \ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define LOGICAL_ASR_REG \ + asm ("sar %%cl, %%eax;" \ + "setcb %%cl;" \ + : "=a" (value), "=c" (C_OUT) \ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define LOGICAL_ROR_REG \ + asm ("ror %%cl, %%eax;" \ + "setcb %%cl;" \ + : "=a" (value), "=c" (C_OUT) \ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define LOGICAL_RRX_REG \ + asm ("bt $0, C_FLAG;" \ + "rcr $1, %%eax;" \ + "setcb %%cl;" \ + : "=a" (value), "=c" (C_OUT) \ + : "a" (reg[opcode & 0x0f].I)); + +#define LOGICAL_ROR_IMM \ + asm ("ror %%cl, %%eax;" \ + "setcb %%cl;" \ + : "=a" (value), "=c" (C_OUT) \ + : "a" (opcode & 0xff), "c" (shift)); +#define ARITHMETIC_LSL_REG \ + asm ("\ + shl %%cl, %%eax;" \ + : "=a" (value) \ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define ARITHMETIC_LSR_REG \ + asm ("\ + shr %%cl, %%eax;" \ + : "=a" (value) \ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define ARITHMETIC_ASR_REG \ + asm ("\ + sar %%cl, %%eax;" \ + : "=a" (value) \ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define ARITHMETIC_ROR_REG \ + asm ("\ + ror %%cl, %%eax;" \ + : "=a" (value) \ + : "a" (reg[opcode & 0x0f].I), "c" (shift)); + +#define ARITHMETIC_RRX_REG \ + asm ("\ + bt $0, C_FLAG;\ + rcr $1, %%eax;" \ + : "=a" (value) \ + : "a" (reg[opcode & 0x0f].I)); + +#define ARITHMETIC_ROR_IMM \ + asm ("\ + ror %%cl, %%eax;" \ + : "=a" (value) \ + : "a" (opcode & 0xff), "c" (shift)); +#define ROR_IMM_MSR \ + asm ("ror %%cl, %%eax;" \ + : "=a" (value) \ + : "a" (opcode & 0xFF), "c" (shift)); +#define ROR_VALUE \ + asm ("ror %%cl, %0" \ + : "=r" (value) \ + : "r" (value), "c" (shift)); +#define RCR_VALUE \ + asm ("bt $0, C_FLAG;" \ + "rcr $1, %0" \ + : "=r" (value) \ + : "r" (value)); +#endif +#else +#define OP_SUB \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm sub ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + } + +#define OP_SUBS \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm sub ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } + +#define OP_RSB \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm mov eax, value \ + __asm sub eax, ebx \ + __asm mov ebx, dest \ + __asm mov dword ptr [OFFSET reg + 4 * ebx], eax \ + } + +#define OP_RSBS \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm mov eax, value \ + __asm sub eax, ebx \ + __asm mov ebx, dest \ + __asm mov dword ptr [OFFSET reg + 4 * ebx], eax \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } + +#define OP_ADD \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm add ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + } + +#define OP_ADDS \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm add ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } + +#define OP_ADC \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm bt word ptr C_FLAG, 0 \ + __asm adc ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + } + +#define OP_ADCS \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm bt word ptr C_FLAG, 0 \ + __asm adc ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } + +#define OP_SBC \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm mov eax, value \ + __asm bt word ptr C_FLAG, 0 \ + __asm cmc \ + __asm sbb ebx, eax \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + } + +#define OP_SBCS \ + { \ + __asm mov ebx, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm mov eax, value \ + __asm bt word ptr C_FLAG, 0 \ + __asm cmc \ + __asm sbb ebx, eax \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define OP_RSC \ + { \ + __asm mov ebx, value \ + __asm mov eax, base \ + __asm mov eax, dword ptr[OFFSET reg + 4 * eax] \ + __asm bt word ptr C_FLAG, 0 \ + __asm cmc \ + __asm sbb ebx, eax \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + } + +#define OP_RSCS \ + { \ + __asm mov ebx, value \ + __asm mov eax, base \ + __asm mov eax, dword ptr[OFFSET reg + 4 * eax] \ + __asm bt word ptr C_FLAG, 0 \ + __asm cmc \ + __asm sbb ebx, eax \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define OP_CMP \ + { \ + __asm mov eax, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * eax] \ + __asm sub ebx, value \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } + +#define OP_CMN \ + { \ + __asm mov eax, base \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * eax] \ + __asm add ebx, value \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define LOGICAL_LSL_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0f \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm shl eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_OUT + +#define LOGICAL_LSR_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0f \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm shr eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_OUT + +#define LOGICAL_ASR_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0f \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm sar eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_OUT + +#define LOGICAL_ROR_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0F \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm ror eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_OUT + +#define LOGICAL_RRX_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0F \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm bt word ptr C_OUT, 0 \ + __asm rcr eax, 1 \ + __asm mov value, eax \ + __asm setc byte ptr C_OUT + +#define LOGICAL_ROR_IMM \ + __asm mov eax, opcode \ + __asm and eax, 0xff \ + __asm mov cl, byte ptr shift \ + __asm ror eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_OUT +#define ARITHMETIC_LSL_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0f \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm shl eax, cl \ + __asm mov value, eax + +#define ARITHMETIC_LSR_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0f \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm shr eax, cl \ + __asm mov value, eax + +#define ARITHMETIC_ASR_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0f \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm sar eax, cl \ + __asm mov value, eax + +#define ARITHMETIC_ROR_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0F \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm ror eax, cl \ + __asm mov value, eax + +#define ARITHMETIC_RRX_REG \ + __asm mov eax, opcode \ + __asm and eax, 0x0F \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm bt word ptr C_FLAG, 0 \ + __asm rcr eax, 1 \ + __asm mov value, eax + +#define ARITHMETIC_ROR_IMM \ + __asm mov eax, opcode \ + __asm and eax, 0xff \ + __asm mov cl, byte ptr shift \ + __asm ror eax, cl \ + __asm mov value, eax +#define ROR_IMM_MSR \ + { \ + __asm mov eax, opcode \ + __asm and eax, 0xff \ + __asm mov cl, byte ptr shift \ + __asm ror eax, CL \ + __asm mov value, eax \ + } +#define ROR_VALUE \ + { \ + __asm mov cl, byte ptr shift \ + __asm ror dword ptr value, cl \ + } +#define RCR_VALUE \ + { \ + __asm mov cl, byte ptr shift \ + __asm bt word ptr C_FLAG, 0 \ + __asm rcr dword ptr value, 1 \ + } +#endif +#endif + +#define OP_TST \ + u32 res = reg[base].I & value; \ + N_FLAG = (res & 0x80000000) ? true : false; \ + Z_FLAG = (res) ? false : true; \ + C_FLAG = C_OUT; + +#define OP_TEQ \ + u32 res = reg[base].I ^ value; \ + N_FLAG = (res & 0x80000000) ? true : false; \ + Z_FLAG = (res) ? false : true; \ + C_FLAG = C_OUT; + +#define OP_ORR \ + reg[dest].I = reg[base].I | value; + +#define OP_ORRS \ + reg[dest].I = reg[base].I | value; \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; \ + Z_FLAG = (reg[dest].I) ? false : true; \ + C_FLAG = C_OUT; + +#define OP_MOV \ + reg[dest].I = value; + +#define OP_MOVS \ + reg[dest].I = value; \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; \ + Z_FLAG = (reg[dest].I) ? false : true; \ + C_FLAG = C_OUT; + +#define OP_BIC \ + reg[dest].I = reg[base].I & (~value); + +#define OP_BICS \ + reg[dest].I = reg[base].I & (~value); \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; \ + Z_FLAG = (reg[dest].I) ? false : true; \ + C_FLAG = C_OUT; + +#define OP_MVN \ + reg[dest].I = ~value; + +#define OP_MVNS \ + reg[dest].I = ~value; \ + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; \ + Z_FLAG = (reg[dest].I) ? false : true; \ + C_FLAG = C_OUT; + +#define CASE_16(BASE) \ +case BASE: \ +case BASE + 1: \ +case BASE + 2: \ +case BASE + 3: \ +case BASE + 4: \ +case BASE + 5: \ +case BASE + 6: \ +case BASE + 7: \ +case BASE + 8: \ +case BASE + 9: \ +case BASE + 10: \ +case BASE + 11: \ +case BASE + 12: \ +case BASE + 13: \ +case BASE + 14: \ +case BASE + 15: + +#define CASE_256(BASE) \ + CASE_16(BASE) \ + CASE_16(BASE + 0x10) \ + CASE_16(BASE + 0x20) \ + CASE_16(BASE + 0x30) \ + CASE_16(BASE + 0x40) \ + CASE_16(BASE + 0x50) \ + CASE_16(BASE + 0x60) \ + CASE_16(BASE + 0x70) \ + CASE_16(BASE + 0x80) \ + CASE_16(BASE + 0x90) \ + CASE_16(BASE + 0xa0) \ + CASE_16(BASE + 0xb0) \ + CASE_16(BASE + 0xc0) \ + CASE_16(BASE + 0xd0) \ + CASE_16(BASE + 0xe0) \ + CASE_16(BASE + 0xf0) + +#define LOGICAL_DATA_OPCODE(OPCODE, OPCODE2, BASE) \ +case BASE: \ +case BASE + 8: \ +{ \ + /* OP Rd,Rb,Rm LSL # */ \ + int base = (opcode >> 16) & 0x0F; \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + \ + if (shift) { \ + LOGICAL_LSL_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 2: \ +case BASE + 10: \ +{ \ + /* OP Rd,Rb,Rm LSR # */ \ + int base = (opcode >> 16) & 0x0F; \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + LOGICAL_LSR_REG \ + } else { \ + value = 0; \ + C_OUT = (reg[opcode & 0x0F].I & 0x80000000) ? true : false; \ + } \ + \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 4: \ +case BASE + 12: \ +{ \ + /* OP Rd,Rb,Rm ASR # */ \ + int base = (opcode >> 16) & 0x0F; \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + LOGICAL_ASR_REG \ + } else { \ + if (reg[opcode & 0x0F].I & 0x80000000) { \ + value = 0xFFFFFFFF; \ + C_OUT = true; \ + } else { \ + value = 0; \ + C_OUT = false; \ + } \ + } \ + \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 6: \ +case BASE + 14: \ +{ \ + /* OP Rd,Rb,Rm ROR # */ \ + int base = (opcode >> 16) & 0x0F; \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + LOGICAL_ROR_REG \ + } else { \ + LOGICAL_RRX_REG \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 1: \ +{ \ + /* OP Rd,Rb,Rm LSL Rs */ \ + clockTicks++; \ + int base = (opcode >> 16) & 0x0F; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + if (shift == 32) { \ + value = 0; \ + C_OUT = (reg[opcode & 0x0F].I & 1 ? true : false); \ + } else if (shift < 32) { \ + LOGICAL_LSL_REG \ + } else { \ + value = 0; \ + C_OUT = false; \ + } \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 3: \ +{ \ + /* OP Rd,Rb,Rm LSR Rs */ \ + clockTicks++; \ + int base = (opcode >> 16) & 0x0F; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + if (shift == 32) { \ + value = 0; \ + C_OUT = (reg[opcode & 0x0F].I & 0x80000000 ? true : false); \ + } else if (shift < 32) { \ + LOGICAL_LSR_REG \ + } else { \ + value = 0; \ + C_OUT = false; \ + } \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 5: \ +{ \ + /* OP Rd,Rb,Rm ASR Rs */ \ + clockTicks++; \ + int base = (opcode >> 16) & 0x0F; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift < 32) { \ + if (shift) { \ + LOGICAL_ASR_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + } else { \ + if (reg[opcode & 0x0F].I & 0x80000000) { \ + value = 0xFFFFFFFF; \ + C_OUT = true; \ + } else { \ + value = 0; \ + C_OUT = false; \ + } \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 7: \ +{ \ + /* OP Rd,Rb,Rm ROR Rs */ \ + clockTicks++; \ + int base = (opcode >> 16) & 0x0F; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + shift &= 0x1f; \ + if (shift) { \ + LOGICAL_ROR_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + C_OUT = (value & 0x80000000 ? true : false); \ + } \ + } else { \ + value = reg[opcode & 0x0F].I; \ + C_OUT = (value & 0x80000000 ? true : false); \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 0x200: \ +case BASE + 0x201: \ +case BASE + 0x202: \ +case BASE + 0x203: \ +case BASE + 0x204: \ +case BASE + 0x205: \ +case BASE + 0x206: \ +case BASE + 0x207: \ +case BASE + 0x208: \ +case BASE + 0x209: \ +case BASE + 0x20a: \ +case BASE + 0x20b: \ +case BASE + 0x20c: \ +case BASE + 0x20d: \ +case BASE + 0x20e: \ +case BASE + 0x20f: \ +{ \ + int shift = (opcode & 0xF00) >> 7; \ + int base = (opcode >> 16) & 0x0F; \ + int dest = (opcode >> 12) & 0x0F; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + LOGICAL_ROR_IMM \ + } else { \ + value = opcode & 0xff; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; + +#define LOGICAL_DATA_OPCODE_WITHOUT_base(OPCODE, OPCODE2, BASE) \ +case BASE: \ +case BASE + 8: \ +{ \ + /* OP Rd,Rb,Rm LSL # */ \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + \ + if (shift) { \ + LOGICAL_LSL_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 2: \ +case BASE + 10: \ +{ \ + /* OP Rd,Rb,Rm LSR # */ \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + LOGICAL_LSR_REG \ + } else { \ + value = 0; \ + C_OUT = (reg[opcode & 0x0F].I & 0x80000000) ? true : false; \ + } \ + \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 4: \ +case BASE + 12: \ +{ \ + /* OP Rd,Rb,Rm ASR # */ \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + LOGICAL_ASR_REG \ + } else { \ + if (reg[opcode & 0x0F].I & 0x80000000) { \ + value = 0xFFFFFFFF; \ + C_OUT = true; \ + } else { \ + value = 0; \ + C_OUT = false; \ + } \ + } \ + \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 6: \ +case BASE + 14: \ +{ \ + /* OP Rd,Rb,Rm ROR # */ \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + LOGICAL_ROR_REG \ + } else { \ + LOGICAL_RRX_REG \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 1: \ +{ \ + /* OP Rd,Rb,Rm LSL Rs */ \ + clockTicks++; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + if (shift == 32) { \ + value = 0; \ + C_OUT = (reg[opcode & 0x0F].I & 1 ? true : false); \ + } else if (shift < 32) { \ + LOGICAL_LSL_REG \ + } else { \ + value = 0; \ + C_OUT = false; \ + } \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 3: \ +{ \ + /* OP Rd,Rb,Rm LSR Rs */ \ + clockTicks++; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + if (shift == 32) { \ + value = 0; \ + C_OUT = (reg[opcode & 0x0F].I & 0x80000000 ? true : false); \ + } else if (shift < 32) { \ + LOGICAL_LSR_REG \ + } else { \ + value = 0; \ + C_OUT = false; \ + } \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 5: \ +{ \ + /* OP Rd,Rb,Rm ASR Rs */ \ + clockTicks++; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift < 32) { \ + if (shift) { \ + LOGICAL_ASR_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + } else { \ + if (reg[opcode & 0x0F].I & 0x80000000) { \ + value = 0xFFFFFFFF; \ + C_OUT = true; \ + } else { \ + value = 0; \ + C_OUT = false; \ + } \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 7: \ +{ \ + /* OP Rd,Rb,Rm ROR Rs */ \ + clockTicks++; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + shift &= 0x1f; \ + if (shift) { \ + LOGICAL_ROR_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + C_OUT = (value & 0x80000000 ? true : false); \ + } \ + } else { \ + value = reg[opcode & 0x0F].I; \ + C_OUT = (value & 0x80000000 ? true : false); \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 0x200: \ +case BASE + 0x201: \ +case BASE + 0x202: \ +case BASE + 0x203: \ +case BASE + 0x204: \ +case BASE + 0x205: \ +case BASE + 0x206: \ +case BASE + 0x207: \ +case BASE + 0x208: \ +case BASE + 0x209: \ +case BASE + 0x20a: \ +case BASE + 0x20b: \ +case BASE + 0x20c: \ +case BASE + 0x20d: \ +case BASE + 0x20e: \ +case BASE + 0x20f: \ +{ \ + int shift = (opcode & 0xF00) >> 7; \ + int dest = (opcode >> 12) & 0x0F; \ + bool C_OUT = C_FLAG; \ + u32 value; \ + if (shift) { \ + LOGICAL_ROR_IMM \ + } else { \ + value = opcode & 0xff; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; + +#define ARITHMETIC_DATA_OPCODE(OPCODE, OPCODE2, BASE) \ +case BASE: \ +case BASE + 8: \ +{ \ + /* OP Rd,Rb,Rm LSL # */ \ + int base = (opcode >> 16) & 0x0F; \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + u32 value; \ + if (shift) { \ + ARITHMETIC_LSL_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 2: \ +case BASE + 10: \ +{ \ + /* OP Rd,Rb,Rm LSR # */ \ + int base = (opcode >> 16) & 0x0F; \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + u32 value; \ + if (shift) { \ + ARITHMETIC_LSR_REG \ + } else { \ + value = 0; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 4: \ +case BASE + 12: \ +{ \ + /* OP Rd,Rb,Rm ASR # */ \ + int base = (opcode >> 16) & 0x0F; \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + u32 value; \ + if (shift) { \ + ARITHMETIC_ASR_REG \ + } else { \ + if (reg[opcode & 0x0F].I & 0x80000000) { \ + value = 0xFFFFFFFF; \ + } else value = 0; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 6: \ +case BASE + 14: \ +{ \ + /* OP Rd,Rb,Rm ROR # */ \ + int base = (opcode >> 16) & 0x0F; \ + int shift = (opcode >> 7) & 0x1F; \ + int dest = (opcode >> 12) & 15; \ + u32 value; \ + if (shift) { \ + ARITHMETIC_ROR_REG \ + } else { \ + ARITHMETIC_RRX_REG \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 1: \ +{ \ + /* OP Rd,Rb,Rm LSL Rs */ \ + clockTicks++; \ + int base = (opcode >> 16) & 0x0F; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + u32 value; \ + if (shift) { \ + if (shift == 32) { \ + value = 0; \ + } else if (shift < 32) { \ + ARITHMETIC_LSL_REG \ + } else value = 0; \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 3: \ +{ \ + /* OP Rd,Rb,Rm LSR Rs */ \ + clockTicks++; \ + int base = (opcode >> 16) & 0x0F; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + u32 value; \ + if (shift) { \ + if (shift == 32) { \ + value = 0; \ + } else if (shift < 32) { \ + ARITHMETIC_LSR_REG \ + } else value = 0; \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 5: \ +{ \ + /* OP Rd,Rb,Rm ASR Rs */ \ + clockTicks++; \ + int base = (opcode >> 16) & 0x0F; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + u32 value; \ + if (shift < 32) { \ + if (shift) { \ + ARITHMETIC_ASR_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + } else { \ + if (reg[opcode & 0x0F].I & 0x80000000) { \ + value = 0xFFFFFFFF; \ + } else value = 0; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 7: \ +{ \ + /* OP Rd,Rb,Rm ROR Rs */ \ + clockTicks++; \ + int base = (opcode >> 16) & 0x0F; \ + int shift = reg[(opcode >> 8) & 15].B.B0; \ + int dest = (opcode >> 12) & 15; \ + u32 value; \ + if (shift) { \ + shift &= 0x1f; \ + if (shift) { \ + ARITHMETIC_ROR_REG \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + } else { \ + value = reg[opcode & 0x0F].I; \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; \ +case BASE + 0x200: \ +case BASE + 0x201: \ +case BASE + 0x202: \ +case BASE + 0x203: \ +case BASE + 0x204: \ +case BASE + 0x205: \ +case BASE + 0x206: \ +case BASE + 0x207: \ +case BASE + 0x208: \ +case BASE + 0x209: \ +case BASE + 0x20a: \ +case BASE + 0x20b: \ +case BASE + 0x20c: \ +case BASE + 0x20d: \ +case BASE + 0x20e: \ +case BASE + 0x20f: \ +{ \ + int shift = (opcode & 0xF00) >> 7; \ + int base = (opcode >> 16) & 0x0F; \ + int dest = (opcode >> 12) & 0x0F; \ + u32 value; \ + { \ + ARITHMETIC_ROR_IMM \ + } \ + if (dest == 15) { \ + OPCODE2 \ + /* todo */ \ + if (opcode & 0x00100000) { \ + clockTicks++; \ + CPUSwitchMode(reg[17].I & 0x1f, false); \ + } \ + if (armState) { \ + reg[15].I &= 0xFFFFFFFC; \ + armNextPC = reg[15].I; \ + reg[15].I += 4; \ + } else { \ + reg[15].I &= 0xFFFFFFFE; \ + armNextPC = reg[15].I; \ + reg[15].I += 2; \ + } \ + } else { \ + OPCODE \ + } \ +} \ +break; + +u32 opcode = CPUReadMemoryQuick(armNextPC); + +clockTicks = memoryWaitFetch32[(armNextPC >> 24) & 15]; + +#ifndef FINAL_VERSION +if (armNextPC == stop) +{ + armNextPC++; +} +#endif + +armNextPC = reg[15].I; +reg[15].I += 4; +int cond = opcode >> 28; +// suggested optimization for frequent cases +bool cond_res; +if (cond == 0x0e) +{ + cond_res = true; +} +else +{ + switch (cond) + { + case 0x00: // EQ + cond_res = Z_FLAG; + break; + case 0x01: // NE + cond_res = !Z_FLAG; + break; + case 0x02: // CS + cond_res = C_FLAG; + break; + case 0x03: // CC + cond_res = !C_FLAG; + break; + case 0x04: // MI + cond_res = N_FLAG; + break; + case 0x05: // PL + cond_res = !N_FLAG; + break; + case 0x06: // VS + cond_res = V_FLAG; + break; + case 0x07: // VC + cond_res = !V_FLAG; + break; + case 0x08: // HI + cond_res = C_FLAG && !Z_FLAG; + break; + case 0x09: // LS + cond_res = !C_FLAG || Z_FLAG; + break; + case 0x0A: // GE + cond_res = N_FLAG == V_FLAG; + break; + case 0x0B: // LT + cond_res = N_FLAG != V_FLAG; + break; + case 0x0C: // GT + cond_res = !Z_FLAG && (N_FLAG == V_FLAG); + break; + case 0x0D: // LE + cond_res = Z_FLAG || (N_FLAG != V_FLAG); + break; + case 0x0E: + cond_res = true; + break; + case 0x0F: + default: + // ??? + cond_res = false; + break; + } +} + +if (cond_res) +{ + switch (((opcode >> 16) & 0xFF0) | ((opcode >> 4) & 0x0F)) + { + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_AND, OP_AND, 0x000); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_ANDS, OP_AND, 0x010); + case 0x009: + { + // MUL Rd, Rm, Rs + int dest = (opcode >> 16) & 0x0F; + int mult = (opcode & 0x0F); + u32 rs = reg[(opcode >> 8) & 0x0F].I; + reg[dest].I = reg[mult].I * rs; + if (((s32)rs) < 0) + rs = ~rs; + if ((rs & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((rs & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + case 0x019: + { + // MULS Rd, Rm, Rs + int dest = (opcode >> 16) & 0x0F; + int mult = (opcode & 0x0F); + u32 rs = reg[(opcode >> 8) & 0x0F].I; + reg[dest].I = reg[mult].I * rs; + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; + Z_FLAG = (reg[dest].I) ? false : true; + if (((s32)rs) < 0) + rs = ~rs; + if ((rs & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((rs & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + case 0x00b: + case 0x02b: + { + // STRH Rd, [Rn], -Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + address -= offset; + reg[base].I = address; + } + break; + case 0x04b: + case 0x06b: + { + // STRH Rd, [Rn], #-offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + address -= offset; + reg[base].I = address; + } + break; + case 0x08b: + case 0x0ab: + { + // STRH Rd, [Rn], Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + address += offset; + reg[base].I = address; + } + break; + case 0x0cb: + case 0x0eb: + { + // STRH Rd, [Rn], #offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + address += offset; + reg[base].I = address; + } + break; + case 0x10b: + { + // STRH Rd, [Rn, -Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + } + break; + case 0x12b: + { + // STRH Rd, [Rn, -Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + reg[base].I = address; + } + break; + case 0x14b: + { + // STRH Rd, [Rn, -#offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + } + break; + case 0x16b: + { + // STRH Rd, [Rn, -#offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + reg[base].I = address; + } + break; + case 0x18b: + { + // STRH Rd, [Rn, Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + } + break; + case 0x1ab: + { + // STRH Rd, [Rn, Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + reg[base].I = address; + } + break; + case 0x1cb: + { + // STRH Rd, [Rn, #offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + } + break; + case 0x1eb: + { + // STRH Rd, [Rn, #offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 4 + CPUUpdateTicksAccess16(address); + CPUWriteHalfWord(address, reg[dest].W.W0); + reg[base].I = address; + } + break; + case 0x01b: + case 0x03b: + { + // LDRH Rd, [Rn], -Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if (dest != base) + { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x05b: + case 0x07b: + { + // LDRH Rd, [Rn], #-offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if (dest != base) + { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x09b: + case 0x0bb: + { + // LDRH Rd, [Rn], Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if (dest != base) + { + address += offset; + reg[base].I = address; + } + } + break; + case 0x0db: + case 0x0fb: + { + // LDRH Rd, [Rn], #offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if (dest != base) + { + address += offset; + reg[base].I = address; + } + } + break; + case 0x11b: + { + // LDRH Rd, [Rn, -Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + } + break; + case 0x13b: + { + // LDRH Rd, [Rn, -Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x15b: + { + // LDRH Rd, [Rn, -#offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + } + break; + case 0x17b: + { + // LDRH Rd, [Rn, -#offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x19b: + { + // LDRH Rd, [Rn, Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + } + break; + case 0x1bb: + { + // LDRH Rd, [Rn, Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x1db: + { + // LDRH Rd, [Rn, #offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + } + break; + case 0x1fb: + { + // LDRH Rd, [Rn, #offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = CPUReadHalfWord(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x01d: + case 0x03d: + { + // LDRSB Rd, [Rn], -Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if (dest != base) + { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x05d: + case 0x07d: + { + // LDRSB Rd, [Rn], #-offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if (dest != base) + { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x09d: + case 0x0bd: + { + // LDRSB Rd, [Rn], Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if (dest != base) + { + address += offset; + reg[base].I = address; + } + } + break; + case 0x0dd: + case 0x0fd: + { + // LDRSB Rd, [Rn], #offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if (dest != base) + { + address += offset; + reg[base].I = address; + } + } + break; + case 0x11d: + { + // LDRSB Rd, [Rn, -Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + } + break; + case 0x13d: + { + // LDRSB Rd, [Rn, -Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x15d: + { + // LDRSB Rd, [Rn, -#offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + } + break; + case 0x17d: + { + // LDRSB Rd, [Rn, -#offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x19d: + { + // LDRSB Rd, [Rn, Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + } + break; + case 0x1bd: + { + // LDRSB Rd, [Rn, Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x1dd: + { + // LDRSB Rd, [Rn, #offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + } + break; + case 0x1fd: + { + // LDRSB Rd, [Rn, #offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s8)CPUReadByte(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x01f: + case 0x03f: + { + // LDRSH Rd, [Rn], -Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if (dest != base) + { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x05f: + case 0x07f: + { + // LDRSH Rd, [Rn], #-offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if (dest != base) + { + address -= offset; + reg[base].I = address; + } + } + break; + case 0x09f: + case 0x0bf: + { + // LDRSH Rd, [Rn], Rm + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if (dest != base) + { + address += offset; + reg[base].I = address; + } + } + break; + case 0x0df: + case 0x0ff: + { + // LDRSH Rd, [Rn], #offset + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I; + int offset = (opcode & 0x0F) | ((opcode >> 4) & 0xF0); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if (dest != base) + { + address += offset; + reg[base].I = address; + } + } + break; + case 0x11f: + { + // LDRSH Rd, [Rn, -Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + } + break; + case 0x13f: + { + // LDRSH Rd, [Rn, -Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x15f: + { + // LDRSH Rd, [Rn, -#offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + } + break; + case 0x17f: + { + // LDRSH Rd, [Rn, -#offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I - ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x19f: + { + // LDRSH Rd, [Rn, Rm] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + } + break; + case 0x1bf: + { + // LDRSH Rd, [Rn, Rm]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + reg[opcode & 0x0F].I; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if (dest != base) + reg[base].I = address; + } + break; + case 0x1df: + { + // LDRSH Rd, [Rn, #offset] + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + } + break; + case 0x1ff: + { + // LDRSH Rd, [Rn, #offset]! + int base = (opcode >> 16) & 0x0F; + int dest = (opcode >> 12) & 0x0F; + u32 address = reg[base].I + ((opcode & 0x0F) | ((opcode >> 4) & 0xF0)); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + reg[dest].I = (s16)CPUReadHalfWordSigned(address); + if (dest != base) + reg[base].I = address; + } + break; + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_EOR, OP_EOR, 0x020); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_EORS, OP_EOR, 0x030); + case 0x029: + { + // MLA Rd, Rm, Rs, Rn + int dest = (opcode >> 16) & 0x0F; + int mult = (opcode & 0x0F); + u32 rs = reg[(opcode >> 8) & 0x0F].I; + reg[dest].I = reg[mult].I * rs + reg[(opcode >> 12) & 0x0f].I; + if (((s32)rs) < 0) + rs = ~rs; + if ((rs & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((rs & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + case 0x039: + { + // MLAS Rd, Rm, Rs, Rn + int dest = (opcode >> 16) & 0x0F; + int mult = (opcode & 0x0F); + u32 rs = reg[(opcode >> 8) & 0x0F].I; + reg[dest].I = reg[mult].I * rs + reg[(opcode >> 12) & 0x0f].I; + N_FLAG = (reg[dest].I & 0x80000000) ? true : false; + Z_FLAG = (reg[dest].I) ? false : true; + if (((s32)rs) < 0) + rs = ~rs; + if ((rs & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((rs & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + ARITHMETIC_DATA_OPCODE(OP_SUB, OP_SUB, 0x040); + ARITHMETIC_DATA_OPCODE(OP_SUBS, OP_SUB, 0x050); + ARITHMETIC_DATA_OPCODE(OP_RSB, OP_RSB, 0x060); + ARITHMETIC_DATA_OPCODE(OP_RSBS, OP_RSB, 0x070); + ARITHMETIC_DATA_OPCODE(OP_ADD, OP_ADD, 0x080); + ARITHMETIC_DATA_OPCODE(OP_ADDS, OP_ADD, 0x090); + case 0x089: + { + // UMULL RdLo, RdHi, Rn, Rs + u32 umult = reg[(opcode & 0x0F)].I; + u32 usource = reg[(opcode >> 8) & 0x0F].I; + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u64 uTemp = ((u64)umult) * ((u64)usource); + reg[destLo].I = (u32)(uTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(uTemp >> 32); + if ((usource & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((usource & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((usource & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + case 0x099: + { + // UMULLS RdLo, RdHi, Rn, Rs + u32 umult = reg[(opcode & 0x0F)].I; + u32 usource = reg[(opcode >> 8) & 0x0F].I; + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u64 uTemp = ((u64)umult) * ((u64)usource); + reg[destLo].I = (u32)(uTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(uTemp >> 32); + Z_FLAG = (uTemp) ? false : true; + N_FLAG = (reg[destHi].I & 0x80000000) ? true : false; + if ((usource & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((usource & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((usource & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + ARITHMETIC_DATA_OPCODE(OP_ADC, OP_ADC, 0x0a0); + ARITHMETIC_DATA_OPCODE(OP_ADCS, OP_ADC, 0x0b0); + case 0x0a9: + { + // UMLAL RdLo, RdHi, Rn, Rs + u32 umult = reg[(opcode & 0x0F)].I; + u32 usource = reg[(opcode >> 8) & 0x0F].I; + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u64 uTemp = (u64)reg[destHi].I; + uTemp <<= 32; + uTemp |= (u64)reg[destLo].I; + uTemp += ((u64)umult) * ((u64)usource); + reg[destLo].I = (u32)(uTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(uTemp >> 32); + if ((usource & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((usource & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((usource & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + case 0x0b9: + { + // UMLALS RdLo, RdHi, Rn, Rs + u32 umult = reg[(opcode & 0x0F)].I; + u32 usource = reg[(opcode >> 8) & 0x0F].I; + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u64 uTemp = (u64)reg[destHi].I; + uTemp <<= 32; + uTemp |= (u64)reg[destLo].I; + uTemp += ((u64)umult) * ((u64)usource); + reg[destLo].I = (u32)(uTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(uTemp >> 32); + Z_FLAG = (uTemp) ? false : true; + N_FLAG = (reg[destHi].I & 0x80000000) ? true : false; + if ((usource & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((usource & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((usource & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + ARITHMETIC_DATA_OPCODE(OP_SBC, OP_SBC, 0x0c0); + ARITHMETIC_DATA_OPCODE(OP_SBCS, OP_SBC, 0x0d0); + case 0x0c9: + { + // SMULL RdLo, RdHi, Rm, Rs + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u32 rs = reg[(opcode >> 8) & 0x0F].I; + s64 m = (s32)reg[(opcode & 0x0F)].I; + s64 s = (s32)rs; + s64 sTemp = m * s; + reg[destLo].I = (u32)(sTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(sTemp >> 32); + if (((s32)rs) < 0) + rs = ~rs; + if ((rs & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((rs & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + case 0x0d9: + { + // SMULLS RdLo, RdHi, Rm, Rs + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u32 rs = reg[(opcode >> 8) & 0x0F].I; + s64 m = (s32)reg[(opcode & 0x0F)].I; + s64 s = (s32)rs; + s64 sTemp = m * s; + reg[destLo].I = (u32)(sTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(sTemp >> 32); + Z_FLAG = (sTemp) ? false : true; + N_FLAG = (sTemp < 0) ? true : false; + if (((s32)rs) < 0) + rs = ~rs; + if ((rs & 0xFFFFFF00) == 0) + clockTicks += 2; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 3; + else if ((rs & 0xFF000000) == 0) + clockTicks += 4; + else + clockTicks += 5; + } + break; + ARITHMETIC_DATA_OPCODE(OP_RSC, OP_RSC, 0x0e0); + ARITHMETIC_DATA_OPCODE(OP_RSCS, OP_RSC, 0x0f0); + case 0x0e9: + { + // SMLAL RdLo, RdHi, Rm, Rs + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u32 rs = reg[(opcode >> 8) & 0x0F].I; + s64 m = (s32)reg[(opcode & 0x0F)].I; + s64 s = (s32)rs; + s64 sTemp = (u64)reg[destHi].I; + sTemp <<= 32; + sTemp |= (u64)reg[destLo].I; + sTemp += m * s; + reg[destLo].I = (u32)(sTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(sTemp >> 32); + if (((s32)rs) < 0) + rs = ~rs; + if ((rs & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((rs & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + case 0x0f9: + { + // SMLALS RdLo, RdHi, Rm, Rs + int destLo = (opcode >> 12) & 0x0F; + int destHi = (opcode >> 16) & 0x0F; + u32 rs = reg[(opcode >> 8) & 0x0F].I; + s64 m = (s32)reg[(opcode & 0x0F)].I; + s64 s = (s32)rs; + s64 sTemp = (u64)reg[destHi].I; + sTemp <<= 32; + sTemp |= (u64)reg[destLo].I; + sTemp += m * s; + reg[destLo].I = (u32)(sTemp & 0xFFFFFFFF); + reg[destHi].I = (u32)(sTemp >> 32); + Z_FLAG = (sTemp) ? false : true; + N_FLAG = (sTemp < 0) ? true : false; + if (((s32)rs) < 0) + rs = ~rs; + if ((rs & 0xFFFFFF00) == 0) + clockTicks += 3; + else if ((rs & 0xFFFF0000) == 0) + clockTicks += 4; + else if ((rs & 0xFF000000) == 0) + clockTicks += 5; + else + clockTicks += 6; + } + break; + LOGICAL_DATA_OPCODE(OP_TST, OP_TST, 0x110); + case 0x100: + // MRS Rd, CPSR + // TODO: check if right instruction.... + CPUUpdateCPSR(); + reg[(opcode >> 12) & 0x0F].I = reg[16].I; + break; + case 0x109: + { + // SWP Rd, Rm, [Rn] + u32 address = reg[(opcode >> 16) & 15].I; + u32 temp = CPUReadMemory(address); + CPUWriteMemory(address, reg[opcode & 15].I); + reg[(opcode >> 12) & 15].I = temp; + } + break; + LOGICAL_DATA_OPCODE(OP_TEQ, OP_TEQ, 0x130); + case 0x120: + { + // MSR CPSR_fields, Rm + CPUUpdateCPSR(); + u32 value = reg[opcode & 15].I; + u32 newValue = reg[16].I; + if (armMode > 0x10) + { + if (opcode & 0x00010000) + newValue = (newValue & 0xFFFFFF00) | (value & 0x000000FF); + if (opcode & 0x00020000) + newValue = (newValue & 0xFFFF00FF) | (value & 0x0000FF00); + if (opcode & 0x00040000) + newValue = (newValue & 0xFF00FFFF) | (value & 0x00FF0000); + } + if (opcode & 0x00080000) + newValue = (newValue & 0x00FFFFFF) | (value & 0xFF000000); + newValue |= 0x10; + CPUSwitchMode(newValue & 0x1f, false); + reg[16].I = newValue; + CPUUpdateFlags(); + } + break; + case 0x121: + { + // BX Rm + // TODO: check if right instruction... + clockTicks += 3; + int base = opcode & 0x0F; + armState = reg[base].I & 1 ? false : true; + if (armState) + { + reg[15].I = reg[base].I & 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + else + { + reg[15].I = reg[base].I & 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + } + } + break; + ARITHMETIC_DATA_OPCODE(OP_CMP, OP_CMP, 0x150); + case 0x140: + // MRS Rd, SPSR + // TODO: check if right instruction... + reg[(opcode >> 12) & 0x0F].I = reg[17].I; + break; + case 0x149: + { + // SWPB Rd, Rm, [Rn] + u32 address = reg[(opcode >> 16) & 15].I; + u32 temp = CPUReadByte(address); + CPUWriteByte(address, reg[opcode & 15].B.B0); + reg[(opcode >> 12) & 15].I = temp; + } + break; + ARITHMETIC_DATA_OPCODE(OP_CMN, OP_CMN, 0x170); + case 0x160: + { + // MSR SPSR_fields, Rm + u32 value = reg[opcode & 15].I; + if (armMode > 0x10 && armMode < 0x1f) + { + if (opcode & 0x00010000) + reg[17].I = (reg[17].I & 0xFFFFFF00) | (value & 0x000000FF); + if (opcode & 0x00020000) + reg[17].I = (reg[17].I & 0xFFFF00FF) | (value & 0x0000FF00); + if (opcode & 0x00040000) + reg[17].I = (reg[17].I & 0xFF00FFFF) | (value & 0x00FF0000); + if (opcode & 0x00080000) + reg[17].I = (reg[17].I & 0x00FFFFFF) | (value & 0xFF000000); + } + } + break; + LOGICAL_DATA_OPCODE(OP_ORR, OP_ORR, 0x180); + LOGICAL_DATA_OPCODE(OP_ORRS, OP_ORR, 0x190); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_MOV, OP_MOV, 0x1a0); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_MOVS, OP_MOV, 0x1b0); + LOGICAL_DATA_OPCODE(OP_BIC, OP_BIC, 0x1c0); + LOGICAL_DATA_OPCODE(OP_BICS, OP_BIC, 0x1d0); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_MVN, OP_MVN, 0x1e0); + LOGICAL_DATA_OPCODE_WITHOUT_base(OP_MVNS, OP_MVN, 0x1f0); +#ifdef BKPT_SUPPORT + case 0x127: + case 0x7ff: // for GDB support + extern void (*dbgSignal)(int, int); + reg[15].I -= 4; + armNextPC -= 4; + dbgSignal(5, (opcode & 0x0f) | ((opcode >> 4) & 0xfff0)); + return; +#endif + case 0x320: + case 0x321: + case 0x322: + case 0x323: + case 0x324: + case 0x325: + case 0x326: + case 0x327: + case 0x328: + case 0x329: + case 0x32a: + case 0x32b: + case 0x32c: + case 0x32d: + case 0x32e: + case 0x32f: + { + // MSR CPSR_fields, # + CPUUpdateCPSR(); + u32 value = opcode & 0xFF; + int shift = (opcode & 0xF00) >> 7; + if (shift) + { + ROR_IMM_MSR; + } + u32 newValue = reg[16].I; + if (armMode > 0x10) + { + if (opcode & 0x00010000) + newValue = (newValue & 0xFFFFFF00) | (value & 0x000000FF); + if (opcode & 0x00020000) + newValue = (newValue & 0xFFFF00FF) | (value & 0x0000FF00); + if (opcode & 0x00040000) + newValue = (newValue & 0xFF00FFFF) | (value & 0x00FF0000); + } + if (opcode & 0x00080000) + newValue = (newValue & 0x00FFFFFF) | (value & 0xFF000000); + + newValue |= 0x10; + + CPUSwitchMode(newValue & 0x1f, false); + reg[16].I = newValue; + CPUUpdateFlags(); + } + break; + case 0x360: + case 0x361: + case 0x362: + case 0x363: + case 0x364: + case 0x365: + case 0x366: + case 0x367: + case 0x368: + case 0x369: + case 0x36a: + case 0x36b: + case 0x36c: + case 0x36d: + case 0x36e: + case 0x36f: + { + // MSR SPSR_fields, # + if (armMode > 0x10 && armMode < 0x1f) + { + u32 value = opcode & 0xFF; + int shift = (opcode & 0xF00) >> 7; + if (shift) + { + ROR_IMM_MSR; + } + if (opcode & 0x00010000) + reg[17].I = (reg[17].I & 0xFFFFFF00) | (value & 0x000000FF); + if (opcode & 0x00020000) + reg[17].I = (reg[17].I & 0xFFFF00FF) | (value & 0x0000FF00); + if (opcode & 0x00040000) + reg[17].I = (reg[17].I & 0xFF00FFFF) | (value & 0x00FF0000); + if (opcode & 0x00080000) + reg[17].I = (reg[17].I & 0x00FFFFFF) | (value & 0xFF000000); + } + } + break; + CASE_16(0x400) + // T versions shouldn't be different on GBA + CASE_16(0x420) + { + // STR Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x480) + // T versions shouldn't be different on GBA + CASE_16(0x4a0) + { + // STR Rd, [Rn], # + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x500) + { + // STR Rd, [Rn, -#] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x520) + { + // STR Rd, [Rn, -#]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x580) + { + // STR Rd, [Rn, #] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x5a0) + { + // STR Rd, [Rn, #]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x410) + { + // LDR Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I -= offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x430) + { + // LDRT Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I -= offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x490) + { + // LDR Rd, [Rn], # + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I += offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x4b0) + { + // LDRT Rd, [Rn], # + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I += offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + } + break; + CASE_16(0x510) + { + // LDR Rd, [Rn, -#] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x530) + { + // LDR Rd, [Rn, -#]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x590) + { + // LDR Rd, [Rn, #] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x5b0) + { + // LDR Rd, [Rn, #]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x440) + // T versions shouldn't be different on GBA + CASE_16(0x460) + { + // STRB Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x4c0) + // T versions shouldn't be different on GBA + CASE_16(0x4e0) + // STRB Rd, [Rn], # + { + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x540) + { + // STRB Rd, [Rn, -#] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x560) + { + // STRB Rd, [Rn, -#]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x5c0) + { + // STRB Rd, [Rn, #] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x5e0) + { + // STRB Rd, [Rn, #]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x450) + // T versions shouldn't be different + CASE_16(0x470) + { + // LDRB Rd, [Rn], -# + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I -= offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x4d0) + CASE_16(0x4f0) // T versions should not be different + { + // LDRB Rd, [Rn], # + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I += offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x550) + { + // LDRB Rd, [Rn, -#] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x570) + { + // LDRB Rd, [Rn, -#]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x5d0) + { + // LDRB Rd, [Rn, #] + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + CASE_16(0x5f0) + { + // LDRB Rd, [Rn, #]! + int offset = opcode & 0xFFF; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x600: + case 0x608: + // T versions are the same + case 0x620: + case 0x628: + { + // STR Rd, [Rn], -Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x602: + case 0x60a: + // T versions are the same + case 0x622: + case 0x62a: + { + // STR Rd, [Rn], -Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x604: + case 0x60c: + // T versions are the same + case 0x624: + case 0x62c: + { + // STR Rd, [Rn], -Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x606: + case 0x60e: + // T versions are the same + case 0x626: + case 0x62e: + { + // STR Rd, [Rn], -Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address - value; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x680: + case 0x688: + // T versions are the same + case 0x6a0: + case 0x6a8: + { + // STR Rd, [Rn], Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x682: + case 0x68a: + // T versions are the same + case 0x6a2: + case 0x6aa: + { + // STR Rd, [Rn], Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x684: + case 0x68c: + // T versions are the same + case 0x6a4: + case 0x6ac: + { + // STR Rd, [Rn], Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x686: + case 0x68e: + // T versions are the same + case 0x6a6: + case 0x6ae: + { + // STR Rd, [Rn], Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteMemory(address, reg[dest].I); + reg[base].I = address + value; + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x700: + case 0x708: + { + // STR Rd, [Rn, -Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x702: + case 0x70a: + { + // STR Rd, [Rn, -Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x704: + case 0x70c: + { + // STR Rd, [Rn, -Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x706: + case 0x70e: + { + // STR Rd, [Rn, -Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x720: + case 0x728: + { + // STR Rd, [Rn, -Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x722: + case 0x72a: + { + // STR Rd, [Rn, -Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x724: + case 0x72c: + { + // STR Rd, [Rn, -Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x726: + case 0x72e: + { + // STR Rd, [Rn, -Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x780: + case 0x788: + { + // STR Rd, [Rn, Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x782: + case 0x78a: + { + // STR Rd, [Rn, Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x784: + case 0x78c: + { + // STR Rd, [Rn, Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x786: + case 0x78e: + { + // STR Rd, [Rn, Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x7a0: + case 0x7a8: + { + // STR Rd, [Rn, Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x7a2: + case 0x7aa: + { + // STR Rd, [Rn, Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x7a4: + case 0x7ac: + { + // STR Rd, [Rn, Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x7a6: + case 0x7ae: + { + // STR Rd, [Rn, Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[base].I = address; + CPUWriteMemory(address, reg[dest].I); + clockTicks += 2 + CPUUpdateTicksAccess32(address); + } + break; + case 0x610: + case 0x618: + // T versions are the same + case 0x630: + case 0x638: + { + // LDR Rd, [Rn], -Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x612: + case 0x61a: + // T versions are the same + case 0x632: + case 0x63a: + { + // LDR Rd, [Rn], -Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x614: + case 0x61c: + // T versions are the same + case 0x634: + case 0x63c: + { + // LDR Rd, [Rn], -Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x616: + case 0x61e: + // T versions are the same + case 0x636: + case 0x63e: + { + // LDR Rd, [Rn], -Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address - value; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x690: + case 0x698: + // T versions are the same + case 0x6b0: + case 0x6b8: + { + // LDR Rd, [Rn], Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x692: + case 0x69a: + // T versions are the same + case 0x6b2: + case 0x6ba: + { + // LDR Rd, [Rn], Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x694: + case 0x69c: + // T versions are the same + case 0x6b4: + case 0x6bc: + { + // LDR Rd, [Rn], Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x696: + case 0x69e: + // T versions are the same + case 0x6b6: + case 0x6be: + { + // LDR Rd, [Rn], Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address + value; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x710: + case 0x718: + { + // LDR Rd, [Rn, -Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x712: + case 0x71a: + { + // LDR Rd, [Rn, -Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x714: + case 0x71c: + { + // LDR Rd, [Rn, -Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x716: + case 0x71e: + { + // LDR Rd, [Rn, -Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x730: + case 0x738: + { + // LDR Rd, [Rn, -Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x732: + case 0x73a: + { + // LDR Rd, [Rn, -Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x734: + case 0x73c: + { + // LDR Rd, [Rn, -Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x736: + case 0x73e: + { + // LDR Rd, [Rn, -Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x790: + case 0x798: + { + // LDR Rd, [Rn, Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x792: + case 0x79a: + { + // LDR Rd, [Rn, Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x794: + case 0x79c: + { + // LDR Rd, [Rn, Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x796: + case 0x79e: + { + // LDR Rd, [Rn, Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[dest].I = CPUReadMemory(address); + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x7b0: + case 0x7b8: + { + // LDR Rd, [Rn, Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x7b2: + case 0x7ba: + { + // LDR Rd, [Rn, Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x7b4: + case 0x7bc: + { + // LDR Rd, [Rn, Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x7b6: + case 0x7be: + { + // LDR Rd, [Rn, Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[dest].I = CPUReadMemory(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess32(address); + if (dest == 15) + { + clockTicks += 2; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + case 0x640: + case 0x648: + // T versions are the same + case 0x660: + case 0x668: + { + // STRB Rd, [Rn], -Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x642: + case 0x64a: + // T versions are the same + case 0x662: + case 0x66a: + { + // STRB Rd, [Rn], -Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x644: + case 0x64c: + // T versions are the same + case 0x664: + case 0x66c: + { + // STRB Rd, [Rn], -Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x646: + case 0x64e: + // T versions are the same + case 0x666: + case 0x66e: + { + // STRB Rd, [Rn], -Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address - value; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6c0: + case 0x6c8: + // T versions are the same + case 0x6e0: + case 0x6e8: + { + // STRB Rd, [Rn], Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6c2: + case 0x6ca: + // T versions are the same + case 0x6e2: + case 0x6ea: + { + // STRB Rd, [Rn], Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6c4: + case 0x6cc: + // T versions are the same + case 0x6e4: + case 0x6ec: + { + // STR Rd, [Rn], Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + offset; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6c6: + case 0x6ce: + // T versions are the same + case 0x6e6: + case 0x6ee: + { + // STRB Rd, [Rn], Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + CPUWriteByte(address, reg[dest].B.B0); + reg[base].I = address + value; + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x740: + case 0x748: + { + // STRB Rd, [Rn, -Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x742: + case 0x74a: + { + // STRB Rd, [Rn, -Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x744: + case 0x74c: + { + // STRB Rd, [Rn, -Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x746: + case 0x74e: + { + // STRB Rd, [Rn, -Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x760: + case 0x768: + { + // STRB Rd, [Rn, -Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x762: + case 0x76a: + { + // STRB Rd, [Rn, -Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x764: + case 0x76c: + { + // STRB Rd, [Rn, -Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x766: + case 0x76e: + { + // STRB Rd, [Rn, -Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7c0: + case 0x7c8: + { + // STRB Rd, [Rn, Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7c2: + case 0x7ca: + { + // STRB Rd, [Rn, Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7c4: + case 0x7cc: + { + // STRB Rd, [Rn, Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7c6: + case 0x7ce: + { + // STRB Rd, [Rn, Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7e0: + case 0x7e8: + { + // STRB Rd, [Rn, Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7e2: + case 0x7ea: + { + // STRB Rd, [Rn, Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7e4: + case 0x7ec: + { + // STRB Rd, [Rn, Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7e6: + case 0x7ee: + { + // STRB Rd, [Rn, Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[base].I = address; + CPUWriteByte(address, reg[dest].B.B0); + clockTicks += 2 + CPUUpdateTicksAccess16(address); + } + break; + case 0x650: + case 0x658: + // T versions are the same + case 0x670: + case 0x678: + { + // LDRB Rd, [Rn], -Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x652: + case 0x65a: + // T versions are the same + case 0x672: + case 0x67a: + { + // LDRB Rd, [Rn], -Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x654: + case 0x65c: + // T versions are the same + case 0x674: + case 0x67c: + { + // LDRB Rd, [Rn], -Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address - offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x656: + case 0x65e: + // T versions are the same + case 0x676: + case 0x67e: + { + // LDRB Rd, [Rn], -Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address - value; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6d0: + case 0x6d8: + // T versions are the same + case 0x6f0: + case 0x6f8: + { + // LDRB Rd, [Rn], Rm, LSL # + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6d2: + case 0x6da: + // T versions are the same + case 0x6f2: + case 0x6fa: + { + // LDRB Rd, [Rn], Rm, LSR # + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6d4: + case 0x6dc: + // T versions are the same + case 0x6f4: + case 0x6fc: + { + // LDRB Rd, [Rn], Rm, ASR # + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address + offset; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x6d6: + case 0x6de: + // T versions are the same + case 0x6f6: + case 0x6fe: + { + // LDRB Rd, [Rn], Rm, ROR # + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address + value; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x750: + case 0x758: + { + // LDRB Rd, [Rn, -Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x752: + case 0x75a: + { + // LDRB Rd, [Rn, -Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x754: + case 0x75c: + { + // LDRB Rd, [Rn, -Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x756: + case 0x75e: + { + // LDRB Rd, [Rn, -Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x770: + case 0x778: + { + // LDRB Rd, [Rn, -Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x772: + case 0x77a: + { + // LDRB Rd, [Rn, -Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x774: + case 0x77c: + { + // LDRB Rd, [Rn, -Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - offset; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x776: + case 0x77e: + { + // LDRB Rd, [Rn, -Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I - value; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7d0: + case 0x7d8: + { + // LDRB Rd, [Rn, Rm, LSL #] + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7d2: + case 0x7da: + { + // LDRB Rd, [Rn, Rm, LSR #] + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7d4: + case 0x7dc: + { + // LDRB Rd, [Rn, Rm, ASR #] + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7d6: + case 0x7de: + { + // LDRB Rd, [Rn, Rm, ROR #] + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[dest].I = CPUReadByte(address); + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7f0: + case 0x7f8: + { + // LDRB Rd, [Rn, Rm, LSL #]! + int offset = reg[opcode & 15].I << ((opcode >> 7) & 31); + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7f2: + case 0x7fa: + { + // LDRB Rd, [Rn, Rm, LSR #]! + int shift = (opcode >> 7) & 31; + int offset = shift ? reg[opcode & 15].I >> shift : 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7f4: + case 0x7fc: + { + // LDRB Rd, [Rn, Rm, ASR #]! + int shift = (opcode >> 7) & 31; + int offset; + if (shift) + offset = (int)((s32)reg[opcode & 15].I >> shift); + else if (reg[opcode & 15].I & 0x80000000) + offset = 0xFFFFFFFF; + else + offset = 0; + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + offset; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; + case 0x7f6: + case 0x7fe: + { + // LDRB Rd, [Rn, Rm, ROR #]! + int shift = (opcode >> 7) & 31; + u32 value = reg[opcode & 15].I; + if (shift) + { + ROR_VALUE; + } + else + { + RCR_VALUE; + } + int dest = (opcode >> 12) & 15; + int base = (opcode >> 16) & 15; + u32 address = reg[base].I + value; + reg[dest].I = CPUReadByte(address); + if (dest != base) + reg[base].I = address; + clockTicks += 3 + CPUUpdateTicksAccess16(address); + } + break; +#define STMW_REG(val, num) \ + if (opcode & (val)) { \ + CPUWriteMemory(address, reg[(num)].I); \ + if (!offset) { \ + reg[base].I = temp; \ + clockTicks += 1 + CPUUpdateTicksAccess32(address); \ + offset = 1; \ + } else { \ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); \ + } \ + address += 4; \ + } +#define STM_REG(val, num) \ + if (opcode & (val)) { \ + CPUWriteMemory(address, reg[(num)].I); \ + if (!offset) { \ + clockTicks += 1 + CPUUpdateTicksAccess32(address); \ + offset = 1; \ + } else { \ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); \ + } \ + address += 4; \ + } + + CASE_16(0x800) + // STMDA Rn, {Rlist} + { + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + STM_REG(8192, 13); + STM_REG(16384, 14); + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x820) + { + // STMDA Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + STMW_REG(8192, 13); + STMW_REG(16384, 14); + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + reg[base].I = temp; + } + } + break; + CASE_16(0x840) + { + // STMDA Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + + if (armMode == 0x11) + { + STM_REG(256, R8_FIQ); + STM_REG(512, R9_FIQ); + STM_REG(1024, R10_FIQ); + STM_REG(2048, R11_FIQ); + STM_REG(4096, R12_FIQ); + } + else + { + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + STM_REG(8192, R13_USR); + STM_REG(16384, R14_USR); + } + else + { + STM_REG(8192, 13); + STM_REG(16384, 14); + } + + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x860) + { + // STMDA Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + + if (armMode == 0x11) + { + STMW_REG(256, R8_FIQ); + STMW_REG(512, R9_FIQ); + STMW_REG(1024, R10_FIQ); + STMW_REG(2048, R11_FIQ); + STMW_REG(4096, R12_FIQ); + } + else + { + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + STMW_REG(8192, R13_USR); + STMW_REG(16384, R14_USR); + } + else + { + STMW_REG(8192, 13); + STMW_REG(16384, 14); + } + + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + reg[base].I = temp; + } + } + break; + + CASE_16(0x880) + { + // STMIA Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + STM_REG(8192, 13); + STM_REG(16384, 14); + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x8a0) + { + // STMIA Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + u32 temp = reg[base].I + 4 * (cpuBitsSet[opcode & 0xFF] + + cpuBitsSet[(opcode >> 8) & 255]); + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + STMW_REG(8192, 13); + STMW_REG(16384, 14); + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + { + reg[base].I = temp; + clockTicks += 1 + CPUUpdateTicksAccess32(address); + } + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x8c0) + { + // STMIA Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + if (armMode == 0x11) + { + STM_REG(256, R8_FIQ); + STM_REG(512, R9_FIQ); + STM_REG(1024, R10_FIQ); + STM_REG(2048, R11_FIQ); + STM_REG(4096, R12_FIQ); + } + else + { + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + } + if (armMode != 0x10 && armMode != 0x1f) + { + STM_REG(8192, R13_USR); + STM_REG(16384, R14_USR); + } + else + { + STM_REG(8192, 13); + STM_REG(16384, 14); + } + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x8e0) + { + // STMIA Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + u32 temp = reg[base].I + 4 * (cpuBitsSet[opcode & 0xFF] + + cpuBitsSet[(opcode >> 8) & 255]); + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + if (armMode == 0x11) + { + STMW_REG(256, R8_FIQ); + STMW_REG(512, R9_FIQ); + STMW_REG(1024, R10_FIQ); + STMW_REG(2048, R11_FIQ); + STMW_REG(4096, R12_FIQ); + } + else + { + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + } + if (armMode != 0x10 && armMode != 0x1f) + { + STMW_REG(8192, R13_USR); + STMW_REG(16384, R14_USR); + } + else + { + STMW_REG(8192, 13); + STMW_REG(16384, 14); + } + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + { + reg[base].I = temp; + clockTicks += 1 + CPUUpdateTicksAccess32(address); + } + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + + CASE_16(0x900) + { + // STMDB Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + STM_REG(8192, 13); + STM_REG(16384, 14); + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x920) + { + // STMDB Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + STMW_REG(8192, 13); + STMW_REG(16384, 14); + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + reg[base].I = temp; + } + } + break; + CASE_16(0x940) + { + // STMDB Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + + if (armMode == 0x11) + { + STM_REG(256, R8_FIQ); + STM_REG(512, R9_FIQ); + STM_REG(1024, R10_FIQ); + STM_REG(2048, R11_FIQ); + STM_REG(4096, R12_FIQ); + } + else + { + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + STM_REG(8192, R13_USR); + STM_REG(16384, R14_USR); + } + else + { + STM_REG(8192, 13); + STM_REG(16384, 14); + } + + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x960) + { + // STMDB Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + + if (armMode == 0x11) + { + STMW_REG(256, R8_FIQ); + STMW_REG(512, R9_FIQ); + STMW_REG(1024, R10_FIQ); + STMW_REG(2048, R11_FIQ); + STMW_REG(4096, R12_FIQ); + } + else + { + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + STMW_REG(8192, R13_USR); + STMW_REG(16384, R14_USR); + } + else + { + STMW_REG(8192, 13); + STMW_REG(16384, 14); + } + + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + reg[base].I = temp; + } + } + break; + + CASE_16(0x980) + // STMIB Rn, {Rlist} + { + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + STM_REG(8192, 13); + STM_REG(16384, 14); + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x9a0) + { + // STMIB Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + u32 temp = reg[base].I + 4 * (cpuBitsSet[opcode & 0xFF] + + cpuBitsSet[(opcode >> 8) & 255]); + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + STMW_REG(8192, 13); + STMW_REG(16384, 14); + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + { + reg[base].I = temp; + clockTicks += 1 + CPUUpdateTicksAccess32(address); + } + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x9c0) + { + // STMIB Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + STM_REG(1, 0); + STM_REG(2, 1); + STM_REG(4, 2); + STM_REG(8, 3); + STM_REG(16, 4); + STM_REG(32, 5); + STM_REG(64, 6); + STM_REG(128, 7); + if (armMode == 0x11) + { + STM_REG(256, R8_FIQ); + STM_REG(512, R9_FIQ); + STM_REG(1024, R10_FIQ); + STM_REG(2048, R11_FIQ); + STM_REG(4096, R12_FIQ); + } + else + { + STM_REG(256, 8); + STM_REG(512, 9); + STM_REG(1024, 10); + STM_REG(2048, 11); + STM_REG(4096, 12); + } + if (armMode != 0x10 && armMode != 0x1f) + { + STM_REG(8192, R13_USR); + STM_REG(16384, R14_USR); + } + else + { + STM_REG(8192, 13); + STM_REG(16384, 14); + } + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + clockTicks += 1 + CPUUpdateTicksAccess32(address); + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + CASE_16(0x9e0) + { + // STMIB Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + u32 temp = reg[base].I + 4 * (cpuBitsSet[opcode & 0xFF] + + cpuBitsSet[(opcode >> 8) & 255]); + STMW_REG(1, 0); + STMW_REG(2, 1); + STMW_REG(4, 2); + STMW_REG(8, 3); + STMW_REG(16, 4); + STMW_REG(32, 5); + STMW_REG(64, 6); + STMW_REG(128, 7); + if (armMode == 0x11) + { + STMW_REG(256, R8_FIQ); + STMW_REG(512, R9_FIQ); + STMW_REG(1024, R10_FIQ); + STMW_REG(2048, R11_FIQ); + STMW_REG(4096, R12_FIQ); + } + else + { + STMW_REG(256, 8); + STMW_REG(512, 9); + STMW_REG(1024, 10); + STMW_REG(2048, 11); + STMW_REG(4096, 12); + } + if (armMode != 0x10 && armMode != 0x1f) + { + STMW_REG(8192, R13_USR); + STMW_REG(16384, R14_USR); + } + else + { + STMW_REG(8192, 13); + STMW_REG(16384, 14); + } + if (opcode & 32768) + { + CPUWriteMemory(address, reg[15].I + 4); + if (!offset) + { + reg[base].I = temp; + clockTicks += 1 + CPUUpdateTicksAccess32(address); + } + else + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); + } + } + break; + +#define LDM_REG(val, num) \ + if (opcode & (val)) { \ + reg[(num)].I = CPUReadMemory(address); \ + if (offset) \ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); \ + else { \ + clockTicks += 1 + CPUUpdateTicksAccess32(address); \ + offset = 1; \ + } \ + address += 4; \ + } + + CASE_16(0x810) + { + // LDMDA Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if (opcode & 32768) + { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x830) + { + // LDMDA Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if (opcode & 32768) + { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + if (!(opcode & (1 << base))) + reg[base].I = temp; + } + break; + CASE_16(0x850) + { + // LDMDA Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if (opcode & 0x8000) + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + CPUSwitchMode(reg[17].I & 0x1f, false); + if (armState) + { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } + else + { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } + else + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if (armMode == 0x11) + { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } + else + { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } + else + { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + } + } + break; + CASE_16(0x870) + { + // LDMDA Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (temp + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if (opcode & 0x8000) + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + if (!(opcode & (1 << base))) + reg[base].I = temp; + + CPUSwitchMode(reg[17].I & 0x1f, false); + if (armState) + { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } + else + { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } + else + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if (armMode == 0x11) + { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } + else + { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } + else + { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + + if (!(opcode & (1 << base))) + reg[base].I = temp; + } + } + break; + + CASE_16(0x890) + { + // LDMIA Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if (opcode & 32768) + { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x8b0) + { + // LDMIA Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I + + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if (opcode & 32768) + { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + if (!(opcode & (1 << base))) + reg[base].I = temp; + } + break; + CASE_16(0x8d0) + { + // LDMIA Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if (opcode & 0x8000) + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + CPUSwitchMode(reg[17].I & 0x1f, false); + if (armState) + { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } + else + { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } + else + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if (armMode == 0x11) + { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } + else + { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } + else + { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + } + } + break; + CASE_16(0x8f0) + { + // LDMIA Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I + + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = reg[base].I & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if (opcode & 0x8000) + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + if (!(opcode & (1 << base))) + reg[base].I = temp; + + CPUSwitchMode(reg[17].I & 0x1f, false); + if (armState) + { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } + else + { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } + else + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if (armMode == 0x11) + { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } + else + { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } + else + { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + + if (!(opcode & (1 << base))) + reg[base].I = temp; + } + } + break; + + CASE_16(0x910) + { + // LDMDB Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if (opcode & 32768) + { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x930) + { + // LDMDB Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if (opcode & 32768) + { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + if (!(opcode & (1 << base))) + reg[base].I = temp; + } + break; + CASE_16(0x950) + { + // LDMDB Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if (opcode & 0x8000) + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + CPUSwitchMode(reg[17].I & 0x1f, false); + if (armState) + { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } + else + { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } + else + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if (armMode == 0x11) + { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } + else + { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } + else + { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + } + } + break; + CASE_16(0x970) + { + // LDMDB Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I - + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = temp & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if (opcode & 0x8000) + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + if (!(opcode & (1 << base))) + reg[base].I = temp; + + CPUSwitchMode(reg[17].I & 0x1f, false); + if (armState) + { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } + else + { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } + else + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if (armMode == 0x11) + { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } + else + { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } + else + { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + + if (!(opcode & (1 << base))) + reg[base].I = temp; + } + } + break; + + CASE_16(0x990) + { + // LDMIB Rn, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if (opcode & 32768) + { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + } + break; + CASE_16(0x9b0) + { + // LDMIB Rn!, {Rlist} + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I + + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (reg[base].I + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + if (opcode & 32768) + { + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + armNextPC = reg[15].I; + reg[15].I += 4; + } + if (!(opcode & (1 << base))) + reg[base].I = temp; + } + break; + CASE_16(0x9d0) + { + // LDMIB Rn, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 address = (reg[base].I + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if (opcode & 0x8000) + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + CPUSwitchMode(reg[17].I & 0x1f, false); + if (armState) + { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } + else + { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } + else + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if (armMode == 0x11) + { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } + else + { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } + else + { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + } + } + break; + CASE_16(0x9f0) + { + // LDMIB Rn!, {Rlist}^ + int base = (opcode & 0x000F0000) >> 16; + u32 temp = reg[base].I + + 4 * (cpuBitsSet[opcode & 255] + cpuBitsSet[(opcode >> 8) & 255]); + u32 address = (reg[base].I + 4) & 0xFFFFFFFC; + clockTicks += 2; + int offset = 0; + if (opcode & 0x8000) + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + LDM_REG(8192, 13); + LDM_REG(16384, 14); + + reg[15].I = CPUReadMemory(address); + if (!offset) + clockTicks += 2 + CPUUpdateTicksAccess32(address); + else + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); + + if (!(opcode & (1 << base))) + reg[base].I = temp; + + CPUSwitchMode(reg[17].I & 0x1f, false); + if (armState) + { + armNextPC = reg[15].I & 0xFFFFFFFC; + reg[15].I = armNextPC + 4; + } + else + { + armNextPC = reg[15].I & 0xFFFFFFFE; + reg[15].I = armNextPC + 2; + } + } + else + { + LDM_REG(1, 0); + LDM_REG(2, 1); + LDM_REG(4, 2); + LDM_REG(8, 3); + LDM_REG(16, 4); + LDM_REG(32, 5); + LDM_REG(64, 6); + LDM_REG(128, 7); + + if (armMode == 0x11) + { + LDM_REG(256, R8_FIQ); + LDM_REG(512, R9_FIQ); + LDM_REG(1024, R10_FIQ); + LDM_REG(2048, R11_FIQ); + LDM_REG(4096, R12_FIQ); + } + else + { + LDM_REG(256, 8); + LDM_REG(512, 9); + LDM_REG(1024, 10); + LDM_REG(2048, 11); + LDM_REG(4096, 12); + } + + if (armMode != 0x10 && armMode != 0x1f) + { + LDM_REG(8192, R13_USR); + LDM_REG(16384, R14_USR); + } + else + { + LDM_REG(8192, 13); + LDM_REG(16384, 14); + } + + if (!(opcode & (1 << base))) + reg[base].I = temp; + } + } + break; + CASE_256(0xa00) + { + // B + clockTicks += 3; + int offset = opcode & 0x00FFFFFF; + if (offset & 0x00800000) + { + offset |= 0xFF000000; + } + offset <<= 2; + reg[15].I += offset; + armNextPC = reg[15].I; + reg[15].I += 4; + } + break; + CASE_256(0xb00) + { + // BL + clockTicks += 3; + int offset = opcode & 0x00FFFFFF; + if (offset & 0x00800000) + { + offset |= 0xFF000000; + } + offset <<= 2; + reg[14].I = reg[15].I - 4; + reg[15].I += offset; + armNextPC = reg[15].I; + reg[15].I += 4; + } + break; + CASE_256(0xf00) + // SWI + clockTicks += 3; + CPUSoftwareInterrupt(opcode & 0x00FFFFFF); + break; +#ifdef GP_SUPPORT + case 0xe11: + case 0xe13: + case 0xe15: + case 0xe17: + case 0xe19: + case 0xe1b: + case 0xe1d: + case 0xe1f: + // MRC + break; + case 0xe01: + case 0xe03: + case 0xe05: + case 0xe07: + case 0xe09: + case 0xe0b: + case 0xe0d: + case 0xe0f: + // MRC + break; +#endif + default: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_UNDEFINED) + log("Undefined ARM instruction %08x at %08x\n", opcode, + armNextPC - 4); +#endif + CPUUndefinedException(); + break; + // END + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/armdis.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/armdis.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,774 @@ +/************************************************************************/ +/* Arm/Thumb command set disassembler */ +/************************************************************************/ +#include + +#include "GBAGlobals.h" +#include "armdis.h" +#include "elf.h" + +struct Opcodes +{ + u32 mask; + u32 cval; + const char *mnemonic; +}; + +const char hdig[] = "0123456789abcdef"; + +const char *decVals[16] = { + "0", "1", "2", "3", "4", "5", "6", "7", "8", + "9", "10", "11", "12", "13", "14", "15" +}; + +const char *regs[16] = { + "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", + "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc" +}; + +const char *conditions[16] = { + "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", + "hi", "ls", "ge", "lt", "gt", "le", "", "nv" +}; + +const char *shifts[5] = { + "lsl", "lsr", "asr", "ror", "rrx" +}; + +const char *armMultLoadStore[12] = { + // non-stack + "da", "ia", "db", "ib", + // stack store + "ed", "ea", "fd", "fa", + // stack load + "fa", "fd", "ea", "ed" +}; + +const Opcodes thumbOpcodes[] = { + // Format 1 + { 0xf800, 0x0000, "lsl %r0, %r3, %o" }, + { 0xf800, 0x0800, "lsr %r0, %r3, %o" }, + { 0xf800, 0x1000, "asr %r0, %r3, %o" }, + // Format 2 + { 0xfe00, 0x1800, "add %r0, %r3, %r6" }, + { 0xfe00, 0x1a00, "sub %r0, %r3, %r6" }, + { 0xfe00, 0x1c00, "add %r0, %r3, %i" }, + { 0xfe00, 0x1e00, "sub %r0, %r3, %i" }, + // Format 3 + { 0xf800, 0x2000, "mov %r8, %O" }, + { 0xf800, 0x2800, "cmp %r8, %O" }, + { 0xf800, 0x3000, "add %r8, %O" }, + { 0xf800, 0x3800, "sub %r8, %O" }, + // Format 4 + { 0xffc0, 0x4000, "and %r0, %r3" }, + { 0xffc0, 0x4040, "eor %r0, %r3" }, + { 0xffc0, 0x4080, "lsl %r0, %r3" }, + { 0xffc0, 0x40c0, "lsr %r0, %r3" }, + { 0xffc0, 0x4100, "asr %r0, %r3" }, + { 0xffc0, 0x4140, "adc %r0, %r3" }, + { 0xffc0, 0x4180, "sbc %r0, %r3" }, + { 0xffc0, 0x41c0, "ror %r0, %r3" }, + { 0xffc0, 0x4200, "tst %r0, %r3" }, + { 0xffc0, 0x4240, "neg %r0, %r3" }, + { 0xffc0, 0x4280, "cmp %r0, %r3" }, + { 0xffc0, 0x42c0, "cmn %r0, %r3" }, + { 0xffc0, 0x4300, "orr %r0, %r3" }, + { 0xffc0, 0x4340, "mul %r0, %r3" }, + { 0xffc0, 0x4380, "bic %r0, %r3" }, + { 0xffc0, 0x43c0, "mvn %r0, %r3" }, + // Format 5 + { 0xff80, 0x4700, "bx %h36" }, + { 0xfcc0, 0x4400, "[ ??? ]" }, + { 0xff00, 0x4400, "add %h07, %h36" }, + { 0xff00, 0x4500, "cmp %h07, %h36" }, + { 0xff00, 0x4600, "mov %h07, %h36" }, + // Format 6 + { 0xf800, 0x4800, "ldr %r8, [%I] (=%J)" }, + // Format 7 + { 0xfa00, 0x5000, "str%b %r0, [%r3, %r6]" }, + { 0xfa00, 0x5800, "ldr%b %r0, [%r3, %r6]" }, + // Format 8 + { 0xfe00, 0x5200, "strh %r0, [%r3, %r6]" }, + { 0xfe00, 0x5600, "ldsb %r0, [%r3, %r6]" }, + { 0xfe00, 0x5a00, "ldrh %r0, [%r3, %r6]" }, + { 0xfe00, 0x5e00, "ldsh %r0, [%r3, %r6]" }, + // Format 9 + { 0xe800, 0x6000, "str%B %r0, [%r3, %p]" }, + { 0xe800, 0x6800, "ldr%B %r0, [%r3, %p]" }, + // Format 10 + { 0xf800, 0x8000, "strh %r0, [%r3, %e]" }, + { 0xf800, 0x8800, "ldrh %r0, [%r3, %e]" }, + // Format 11 + { 0xf800, 0x9000, "str %r8, [sp, %w]" }, + { 0xf800, 0x9800, "ldr %r8, [sp, %w]" }, + // Format 12 + { 0xf800, 0xa000, "add %r8, pc, %w (=%K)" }, + { 0xf800, 0xa800, "add %r8, sp, %w" }, + // Format 13 + { 0xff00, 0xb000, "add sp, %s" }, + // Format 14 + { 0xffff, 0xb500, "push {lr}" }, + { 0xff00, 0xb400, "push {%l}" }, + { 0xff00, 0xb500, "push {%l,lr}" }, + { 0xffff, 0xbd00, "pop {pc}" }, + { 0xff00, 0xbd00, "pop {%l,pc}" }, + { 0xff00, 0xbc00, "pop {%l}" }, + // Format 15 + { 0xf800, 0xc000, "stmia %r8!, {%l}" }, + { 0xf800, 0xc800, "ldmia %r8!, {%l}" }, + // Format 17 + { 0xff00, 0xdf00, "swi %m" }, + // Format 16 + { 0xf000, 0xd000, "b%c %W" }, + // Format 18 + { 0xf800, 0xe000, "b %a" }, + // Format 19 + { 0xf800, 0xf000, "bl %A" }, + { 0xf800, 0xf800, "blh %Z" }, + { 0xff00, 0xbe00, "bkpt %O" }, + // Unknown + { 0x0000, 0x0000, "[ ??? ]" } +}; + +const Opcodes armOpcodes[] = { + // Undefined + { 0x0e000010, 0x06000010, "[ undefined ]" }, + // Branch instructions + { 0x0ff000f0, 0x01200010, "bx%c %r0" }, + { 0x0f000000, 0x0a000000, "b%c %o" }, + { 0x0f000000, 0x0b000000, "bl%c %o" }, + { 0x0f000000, 0x0f000000, "swi%c %q" }, + // PSR transfer + { 0x0fbf0fff, 0x010f0000, "mrs%c %r3, %p" }, + { 0x0db0f000, 0x0120f000, "msr%c %p, %i" }, + // Multiply instructions + { 0x0fe000f0, 0x00000090, "mul%c%s %r4, %r0, %r2" }, + { 0x0fe000f0, 0x00200090, "mla%c%s %r4, %r0, %r2, %r3" }, + { 0x0fa000f0, 0x00800090, "%umull%c%s %r3, %r4, %r0, %r2" }, + { 0x0fa000f0, 0x00a00090, "%umlal%c%s %r3, %r4, %r0, %r2" }, + // Load/Store instructions + { 0x0fb00ff0, 0x01000090, "swp%c%b %r3, %r0, [%r4]" }, + { 0x0fb000f0, 0x01000090, "[ ??? ]" }, + { 0x0c100000, 0x04000000, "str%c%b%t %r3, %a" }, + { 0x0c100000, 0x04100000, "ldr%c%b%t %r3, %a" }, + { 0x0e100090, 0x00000090, "str%c%h %r3, %a" }, + { 0x0e100090, 0x00100090, "ldr%c%h %r3, %a" }, + { 0x0e100000, 0x08000000, "stm%c%m %r4%l" }, + { 0x0e100000, 0x08100000, "ldm%c%m %r4%l" }, + // Data processing + { 0x0de00000, 0x00000000, "and%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x00200000, "eor%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x00400000, "sub%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x00600000, "rsb%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x00800000, "add%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x00a00000, "adc%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x00c00000, "sbc%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x00e00000, "rsc%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x01000000, "tst%c%s %r4, %i" }, + { 0x0de00000, 0x01200000, "teq%c%s %r4, %i" }, + { 0x0de00000, 0x01400000, "cmp%c%s %r4, %i" }, + { 0x0de00000, 0x01600000, "cmn%c%s %r4, %i" }, + { 0x0de00000, 0x01800000, "orr%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x01a00000, "mov%c%s %r3, %i" }, + { 0x0de00000, 0x01c00000, "bic%c%s %r3, %r4, %i" }, + { 0x0de00000, 0x01e00000, "mvn%c%s %r3, %i" }, + // Coprocessor operations + { 0x0f000010, 0x0e000000, "cdp%c %P, %N, %r3, %R4, %R0%V" }, + { 0x0e100000, 0x0c000000, "stc%c%L %P, %r3, %A" }, + { 0x0f100010, 0x0e000010, "mcr%c %P, %N, %r3, %R4, %R0%V" }, + { 0x0f100010, 0x0e100010, "mrc%c %P, %N, %r3, %R4, %R0%V" }, + // Unknown + { 0x00000000, 0x00000000, "[ ??? ]" } +}; + +char *addStr(char *dest, const char *src) +{ + while (*src) + { + *dest++ = *src++; + } + return dest; +} + +char *addHex(char *dest, int siz, u32 val) +{ + if (siz == 0) + { + siz = 28; + while ((((val >> siz) & 15) == 0) && (siz >= 4)) + siz -= 4; + siz += 4; + } + while (siz > 0) + { + siz -= 4; + *dest++ = hdig[(val >> siz) & 15]; + } + return dest; +} + +int disArm(u32 offset, char *dest, int flags) +{ + u32 opcode = debuggerReadMemory(offset); + + const Opcodes *sp = armOpcodes; + while (sp->cval != (opcode & sp->mask)) + sp++; + + if (flags & DIS_VIEW_ADDRESS) + { + dest = addHex(dest, 32, offset); + *dest++ = ' '; + } + if (flags & DIS_VIEW_CODE) + { + dest = addHex(dest, 32, opcode); + *dest++ = ' '; + } + + const char *src = sp->mnemonic; + while (*src) + { + if (*src != '%') + *dest++ = *src++; + else + { + src++; + switch (*src) + { + case 'c': + dest = addStr(dest, conditions[opcode >> 28]); + break; + case 'r': + dest = addStr(dest, regs[(opcode >> ((*(++src) - '0') * 4)) & 15]); + break; + case 'o': + { + *dest++ = '$'; + int off = opcode & 0xffffff; + if (off & 0x800000) + off |= 0xff000000; + off <<= 2; + dest = addHex(dest, 32, offset + 8 + off); + } + break; + case 'i': + if (opcode & (1 << 25)) + { + dest = addStr(dest, "#0x"); + int imm = opcode & 0xff; + int rot = (opcode & 0xf00) >> 7; + int val = (imm << (32 - rot)) | (imm >> rot); + dest = addHex(dest, 0, val); + } + else + { + dest = addStr(dest, regs[opcode & 0x0f]); + int shi = (opcode >> 5) & 3; + int sdw = (opcode >> 7) & 0x1f; + if ((sdw == 0) && (shi == 3)) + shi = 4; + if ((sdw) || (opcode & 0x10) || (shi)) + { + dest = addStr(dest, ", "); + dest = addStr(dest, shifts[shi]); + if (opcode & 0x10) + { + *dest++ = ' '; + dest = addStr(dest, regs[(opcode >> 8) & 15]); + } + else + { + if (sdw == 0 && ((shi == 1) || (shi == 2))) + sdw = 32; + if (shi != 4) + { + dest = addStr(dest, " #0x"); + dest = addHex(dest, 8, sdw); + } + } + } + } + break; + case 'p': + if (opcode & (1 << 22)) + dest = addStr(dest, "spsr"); + else + dest = addStr(dest, "cpsr"); + if (opcode & 0x00F00000) + { + *dest++ = '_'; + if (opcode & 0x00080000) + *dest++ = 'f'; + if (opcode & 0x00040000) + *dest++ = 's'; + if (opcode & 0x00020000) + *dest++ = 'x'; + if (opcode & 0x00010000) + *dest++ = 'c'; + } + break; + case 's': + if (opcode & (1 << 20)) + *dest++ = 's'; + break; + case 'S': + if (opcode & (1 << 22)) + *dest++ = 's'; + break; + case 'u': + if (opcode & (1 << 22)) + *dest++ = 's'; + else + *dest++ = 'u'; + break; + case 'b': + if (opcode & (1 << 22)) + *dest++ = 'b'; + break; + case 'a': + if ((opcode & 0x076f0000) == 0x004f0000) + { + *dest++ = '['; + *dest++ = '$'; + int adr = offset + 8; + int add = (opcode & 15) | ((opcode >> 8) & 0xf0); + if (opcode & (1 << 23)) + adr += add; + else + adr -= add; + dest = addHex(dest, 32, adr); + *dest++ = ']'; + dest = addStr(dest, " (="); + *dest++ = '$'; + dest = addHex(dest, 32, debuggerReadMemory(adr)); + *dest++ = ')'; + } + if ((opcode & 0x072f0000) == 0x050f0000) + { + *dest++ = '['; + *dest++ = '$'; + int adr = offset + 8; + if (opcode & (1 << 23)) + adr += opcode & 0xfff; + else + adr -= opcode & 0xfff; + dest = addHex(dest, 32, adr); + *dest++ = ']'; + dest = addStr(dest, " (="); + *dest++ = '$'; + dest = addHex(dest, 32, debuggerReadMemory(adr)); + *dest++ = ')'; + } + else + { + int reg = (opcode >> 16) & 15; + *dest++ = '['; + dest = addStr(dest, regs[reg]); + if (!(opcode & (1 << 24))) + *dest++ = ']'; + if (((opcode & (1 << 25)) && (opcode & (1 << 26))) || (!(opcode & (1 << 22)) && !(opcode & (1 << 26)))) + { + dest = addStr(dest, ", "); + if (!(opcode & (1 << 23))) + *dest++ = '-'; + dest = addStr(dest, regs[opcode & 0x0f]); + int shi = (opcode >> 5) & 3; + if (opcode & (1 << 26)) + { + if (((opcode >> 7) & 0x1f) || (opcode & 0x10) || (shi == 1) || (shi == 2)) + { + dest = addStr(dest, ", "); + dest = addStr(dest, shifts[shi]); + if (opcode & 0x10) + { + *dest++ = ' '; + dest = addStr(dest, regs[(opcode >> 8) & 15]); + } + else + { + int sdw = (opcode >> 7) & 0x1f; + if (sdw == 0 && ((shi == 1) || (shi == 2))) + sdw = 32; + dest = addStr(dest, " #0x"); + dest = addHex(dest, 8, sdw); + } + } + } + } + else + { + int off; + if (opcode & (1 << 26)) + off = opcode & 0xfff; + else + off = (opcode & 15) | ((opcode >> 4) & 0xf0); + if (off) + { + dest = addStr(dest, ", "); + if (!(opcode & (1 << 23))) + *dest++ = '-'; + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, off); + } + } + if (opcode & (1 << 24)) + { + *dest++ = ']'; + if (opcode & (1 << 21)) + *dest++ = '!'; + } + } + break; + case 't': + if ((opcode & 0x01200000) == 0x01200000) + *dest++ = 't'; + break; + case 'h': + if (opcode & (1 << 6)) + *dest++ = 's'; + if (opcode & (1 << 5)) + *dest++ = 'h'; + else + *dest++ = 'b'; + break; + case 'm': + if (((opcode >> 16) & 15) == 13) + { + if (opcode & 0x00100000) + dest = addStr(dest, armMultLoadStore[8 + ((opcode >> 23) & 3)]); + else + dest = addStr(dest, armMultLoadStore[4 + ((opcode >> 23) & 3)]); + } + else + dest = addStr(dest, armMultLoadStore[(opcode >> 23) & 3]); + break; + case 'l': + if (opcode & (1 << 21)) + *dest++ = '!'; + dest = addStr(dest, ", {"); + { + int rlst = opcode & 0xffff; + int msk = 0; + int not_first = 0; + while (msk < 16) + { + if (rlst & (1 << msk)) + { + int fr = msk; + while (rlst & (1 << msk)) + msk++; + int to = msk - 1; + if (not_first) + //dest = addStr(dest, ", "); + *dest++ = ','; + dest = addStr(dest, regs[fr]); + if (fr != to) + { + if (fr == to - 1) + //dest = addStr(", "); + *dest++ = ','; + else + *dest++ = '-'; + dest = addStr(dest, regs[to]); + } + not_first = 1; + } + else + msk++; + } + *dest++ = '}'; + if (opcode & (1 << 22)) + *dest++ = '^'; + } + break; + case 'q': + *dest++ = '$'; + dest = addHex(dest, 24, opcode & 0xffffff); + break; + case 'P': + *dest++ = 'p'; + dest = addStr(dest, decVals[(opcode >> 8) & 15]); + break; + case 'N': + if (opcode & 0x10) + dest = addStr(dest, decVals[(opcode >> 21) & 7]); + else + dest = addStr(dest, decVals[(opcode >> 20) & 15]); + break; + case 'R': + { + src++; + int reg = 4 * (*src - '0'); + *dest++ = 'c'; + dest = addStr(dest, decVals[(opcode >> reg) & 15]); + } + break; + case 'V': + { + int val = (opcode >> 5) & 7; + if (val) + { + dest = addStr(dest, ", "); + dest = addStr(dest, decVals[val]); + } + } + break; + case 'L': + if (opcode & (1 << 22)) + *dest++ = 'l'; + break; + case 'A': + if ((opcode & 0x012f0000) == 0x010f0000) + { + int adr = offset + 8; + int add = (opcode & 0xff) << 2; + if (opcode & (1 << 23)) + adr += add; + else + adr -= add; + *dest++ = '$'; + addHex(dest, 32, adr); + } + else + { + *dest++ = '['; + dest = addStr(dest, regs[(opcode >> 16) & 15]); + if (!(opcode & (1 << 24))) + *dest++ = ']'; + int off = (opcode & 0xff) << 2; + if (off) + { + dest = addStr(dest, ", "); + if (!(opcode & (1 << 23))) + *dest++ = '-'; + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, off); + } + if (opcode & (1 << 24)) + { + *dest++ = ']'; + if (opcode & (1 << 21)) + *dest++ = '!'; + } + } + break; + } + src++; + } + } + *dest++ = 0; + + return 4; +} + +int disThumb(u32 offset, char *dest, int flags) +{ + u32 opcode = debuggerReadHalfWord(offset); + + const Opcodes *sp = thumbOpcodes; + int ret = 2; + while (sp->cval != (opcode & sp->mask)) + sp++; + + if (flags & DIS_VIEW_ADDRESS) + { + dest = addHex(dest, 32, offset); + *dest++ = ' '; + } + if (flags & DIS_VIEW_CODE) + { + dest = addHex(dest, 16, opcode); + *dest++ = ' '; + } + + const char *src = sp->mnemonic; + while (*src) + { + if (*src != '%') + *dest++ = *src++; + else + { + src++; + switch (*src) + { + case 'r': + src++; + dest = addStr(dest, regs[(opcode >> (*src - '0')) & 7]); + break; + case 'o': + dest = addStr(dest, "#0x"); + { + int val = (opcode >> 6) & 0x1f; + dest = addHex(dest, 8, val); + } + break; + case 'p': + dest = addStr(dest, "#0x"); + { + int val = (opcode >> 6) & 0x1f; + if (!(opcode & (1 << 12))) + val <<= 2; + dest = addHex(dest, 0, val); + } + break; + case 'e': + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, ((opcode >> 6) & 0x1f) << 1); + break; + case 'i': + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, (opcode >> 6) & 7); + break; + case 'h': + { + src++; + int reg = (opcode >> (*src - '0')) & 7; + src++; + if (opcode & (1 << (*src - '0'))) + reg += 8; + dest = addStr(dest, regs[reg]); + } + break; + case 'O': + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, (opcode & 0xff)); + break; + case 'I': + *dest++ = '$'; + dest = addHex(dest, 32, (offset & 0xfffffffc) + 4 + ((opcode & 0xff) << 2)); + break; + case 'J': + { + u32 value = debuggerReadMemory((offset & 0xfffffffc) + 4 + + ((opcode & 0xff) << 2)); + *dest++ = '$'; + dest = addHex(dest, 32, value); + const char *s = elfGetAddressSymbol(value); + if (*s) + { + *dest++ = ' '; + dest = addStr(dest, s); + } + } + break; + case 'K': + { + u32 value = (offset & 0xfffffffc) + 4 + ((opcode & 0xff) << 2); + *dest++ = '$'; + dest = addHex(dest, 32, value); + const char *s = elfGetAddressSymbol(value); + if (*s) + { + *dest++ = ' '; + dest = addStr(dest, s); + } + } + break; + case 'b': + if (opcode & (1 << 10)) + *dest++ = 'b'; + break; + case 'B': + if (opcode & (1 << 12)) + *dest++ = 'b'; + break; + case 'w': + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, (opcode & 0xff) << 2); + break; + case 'W': + *dest++ = '$'; + { + int add = opcode & 0xff; + if (add & 0x80) + add |= 0xffffff00; + dest = addHex(dest, 32, (offset & 0xfffffffe) + 4 + (add << 1)); + } + break; + case 'c': + dest = addStr(dest, conditions[(opcode >> 8) & 15]); + break; + case 's': + if (opcode & (1 << 7)) + *dest++ = '-'; + dest = addStr(dest, "#0x"); + dest = addHex(dest, 0, (opcode & 0x7f) << 2); + break; + case 'l': + { + int rlst = opcode & 0xff; + int msk = 0; + int not_first = 0; + while (msk < 8) + { + if (rlst & (1 << msk)) + { + int fr = msk; + while (rlst & (1 << msk)) + msk++; + int to = msk - 1; + if (not_first) + *dest++ = ','; + dest = addStr(dest, regs[fr]); + if (fr != to) + { + if (fr == to - 1) + *dest++ = ','; + else + *dest++ = '-'; + dest = addStr(dest, regs[to]); + } + not_first = 1; + } + else + msk++; + } + } + break; + case 'm': + *dest++ = '$'; + dest = addHex(dest, 8, opcode & 0xff); + break; + case 'Z': + *dest++ = '$'; + dest = addHex(dest, 16, (opcode & 0x7ff) << 1); + break; + case 'a': + *dest++ = '$'; + { + int add = opcode & 0x07ff; + if (add & 0x400) + add |= 0xfffff800; + add <<= 1; + dest = addHex(dest, 32, offset + 4 + add); + } + break; + case 'A': + { + int nopcode = debuggerReadHalfWord(offset + 2); + int add = opcode & 0x7ff; + if (add & 0x400) + add |= 0xfff800; + add = (add << 12) | ((nopcode & 0x7ff) << 1); + *dest++ = '$'; + dest = addHex(dest, 32, offset + 4 + add); + const char *s = elfGetAddressSymbol(offset + 4 + add); + if (*s) + { + *dest++ = ' '; + *dest++ = '('; + dest = addStr(dest, s); + *dest++ = ')'; + } + ret = 4; + } + break; + } + src++; + } + } + *dest++ = 0; + return ret; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/armdis.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/armdis.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +#ifndef VBA_GBA_ARMDIS_H +#define VBA_GBA_ARMDIS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#define DIS_VIEW_ADDRESS 1 +#define DIS_VIEW_CODE 2 + +int disThumb(u32 offset, char *dest, int flags); +int disArm(u32 offset, char *dest, int flags); + +#endif // VBA_GBA_ARMDIS_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/bios.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/bios.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1278 @@ +#include +#include +#include + +#include "bios.h" +#include "../common/System.h" +#include "GBA.h" +#include "GBACheats.h" // FIXME: SDL build requires this +#include "GBAinline.h" +#include "GBAGlobals.h" + +s16 sineTable[256] = { + (s16)0x0000, (s16)0x0192, (s16)0x0323, (s16)0x04B5, (s16)0x0645, (s16)0x07D5, (s16)0x0964, (s16)0x0AF1, + (s16)0x0C7C, (s16)0x0E05, (s16)0x0F8C, (s16)0x1111, (s16)0x1294, (s16)0x1413, (s16)0x158F, (s16)0x1708, + (s16)0x187D, (s16)0x19EF, (s16)0x1B5D, (s16)0x1CC6, (s16)0x1E2B, (s16)0x1F8B, (s16)0x20E7, (s16)0x223D, + (s16)0x238E, (s16)0x24DA, (s16)0x261F, (s16)0x275F, (s16)0x2899, (s16)0x29CD, (s16)0x2AFA, (s16)0x2C21, + (s16)0x2D41, (s16)0x2E5A, (s16)0x2F6B, (s16)0x3076, (s16)0x3179, (s16)0x3274, (s16)0x3367, (s16)0x3453, + (s16)0x3536, (s16)0x3612, (s16)0x36E5, (s16)0x37AF, (s16)0x3871, (s16)0x392A, (s16)0x39DA, (s16)0x3A82, + (s16)0x3B20, (s16)0x3BB6, (s16)0x3C42, (s16)0x3CC5, (s16)0x3D3E, (s16)0x3DAE, (s16)0x3E14, (s16)0x3E71, + (s16)0x3EC5, (s16)0x3F0E, (s16)0x3F4E, (s16)0x3F84, (s16)0x3FB1, (s16)0x3FD3, (s16)0x3FEC, (s16)0x3FFB, + (s16)0x4000, (s16)0x3FFB, (s16)0x3FEC, (s16)0x3FD3, (s16)0x3FB1, (s16)0x3F84, (s16)0x3F4E, (s16)0x3F0E, + (s16)0x3EC5, (s16)0x3E71, (s16)0x3E14, (s16)0x3DAE, (s16)0x3D3E, (s16)0x3CC5, (s16)0x3C42, (s16)0x3BB6, + (s16)0x3B20, (s16)0x3A82, (s16)0x39DA, (s16)0x392A, (s16)0x3871, (s16)0x37AF, (s16)0x36E5, (s16)0x3612, + (s16)0x3536, (s16)0x3453, (s16)0x3367, (s16)0x3274, (s16)0x3179, (s16)0x3076, (s16)0x2F6B, (s16)0x2E5A, + (s16)0x2D41, (s16)0x2C21, (s16)0x2AFA, (s16)0x29CD, (s16)0x2899, (s16)0x275F, (s16)0x261F, (s16)0x24DA, + (s16)0x238E, (s16)0x223D, (s16)0x20E7, (s16)0x1F8B, (s16)0x1E2B, (s16)0x1CC6, (s16)0x1B5D, (s16)0x19EF, + (s16)0x187D, (s16)0x1708, (s16)0x158F, (s16)0x1413, (s16)0x1294, (s16)0x1111, (s16)0x0F8C, (s16)0x0E05, + (s16)0x0C7C, (s16)0x0AF1, (s16)0x0964, (s16)0x07D5, (s16)0x0645, (s16)0x04B5, (s16)0x0323, (s16)0x0192, + (s16)0x0000, (s16)0xFE6E, (s16)0xFCDD, (s16)0xFB4B, (s16)0xF9BB, (s16)0xF82B, (s16)0xF69C, (s16)0xF50F, + (s16)0xF384, (s16)0xF1FB, (s16)0xF074, (s16)0xEEEF, (s16)0xED6C, (s16)0xEBED, (s16)0xEA71, (s16)0xE8F8, + (s16)0xE783, (s16)0xE611, (s16)0xE4A3, (s16)0xE33A, (s16)0xE1D5, (s16)0xE075, (s16)0xDF19, (s16)0xDDC3, + (s16)0xDC72, (s16)0xDB26, (s16)0xD9E1, (s16)0xD8A1, (s16)0xD767, (s16)0xD633, (s16)0xD506, (s16)0xD3DF, + (s16)0xD2BF, (s16)0xD1A6, (s16)0xD095, (s16)0xCF8A, (s16)0xCE87, (s16)0xCD8C, (s16)0xCC99, (s16)0xCBAD, + (s16)0xCACA, (s16)0xC9EE, (s16)0xC91B, (s16)0xC851, (s16)0xC78F, (s16)0xC6D6, (s16)0xC626, (s16)0xC57E, + (s16)0xC4E0, (s16)0xC44A, (s16)0xC3BE, (s16)0xC33B, (s16)0xC2C2, (s16)0xC252, (s16)0xC1EC, (s16)0xC18F, + (s16)0xC13B, (s16)0xC0F2, (s16)0xC0B2, (s16)0xC07C, (s16)0xC04F, (s16)0xC02D, (s16)0xC014, (s16)0xC005, + (s16)0xC000, (s16)0xC005, (s16)0xC014, (s16)0xC02D, (s16)0xC04F, (s16)0xC07C, (s16)0xC0B2, (s16)0xC0F2, + (s16)0xC13B, (s16)0xC18F, (s16)0xC1EC, (s16)0xC252, (s16)0xC2C2, (s16)0xC33B, (s16)0xC3BE, (s16)0xC44A, + (s16)0xC4E0, (s16)0xC57E, (s16)0xC626, (s16)0xC6D6, (s16)0xC78F, (s16)0xC851, (s16)0xC91B, (s16)0xC9EE, + (s16)0xCACA, (s16)0xCBAD, (s16)0xCC99, (s16)0xCD8C, (s16)0xCE87, (s16)0xCF8A, (s16)0xD095, (s16)0xD1A6, + (s16)0xD2BF, (s16)0xD3DF, (s16)0xD506, (s16)0xD633, (s16)0xD767, (s16)0xD8A1, (s16)0xD9E1, (s16)0xDB26, + (s16)0xDC72, (s16)0xDDC3, (s16)0xDF19, (s16)0xE075, (s16)0xE1D5, (s16)0xE33A, (s16)0xE4A3, (s16)0xE611, + (s16)0xE783, (s16)0xE8F8, (s16)0xEA71, (s16)0xEBED, (s16)0xED6C, (s16)0xEEEF, (s16)0xF074, (s16)0xF1FB, + (s16)0xF384, (s16)0xF50F, (s16)0xF69C, (s16)0xF82B, (s16)0xF9BB, (s16)0xFB4B, (s16)0xFCDD, (s16)0xFE6E +}; + +void BIOS_ArcTan() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("ArcTan: %08x (VCOUNT=%2d)\n", + reg[0].I, + VCOUNT); + } +#endif + + s32 a = -((s32)(reg[0].I * reg[0].I)) >> 14; + s32 b = ((0xA9 * a) >> 14) + 0x390; + b = ((b * a) >> 14) + 0x91C; + b = ((b * a) >> 14) + 0xFB6; + b = ((b * a) >> 14) + 0x16AA; + b = ((b * a) >> 14) + 0x2081; + b = ((b * a) >> 14) + 0x3651; + b = ((b * a) >> 14) + 0xA2F9; + reg[0].I = (reg[0].I * b) >> 16; + +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("ArcTan: return=%08x\n", + reg[0].I); + } +#endif +} + +void BIOS_ArcTan2() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("ArcTan2: %08x,%08x (VCOUNT=%2d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + s16 x = reg[0].I; + s16 y = reg[1].I; + + if (y == 0) + { + reg[0].I = 0x8000 & x; + reg[3].I = 0x170; + } + else + { + if (x == 0) + { + reg[0].I = (0x8000 & y) + 0x4000; + reg[3].I = 0x170; + } + else + { + if (abs(x) > abs(y)) + { + reg[1].I = x; + reg[0].I = y << 14; + BIOS_Div(); + BIOS_ArcTan(); + if (x < 0) + reg[0].I = 0x8000 + reg[0].I; + else + reg[0].I = ((y & 0x8000) << 1) + reg[0].I; + reg[3].I = 0x170; + } + else + { + reg[0].I = x << 14; + BIOS_Div(); + BIOS_ArcTan(); + reg[0].I = (0x4000 + (y & 0x8000)) - reg[0].I; + reg[3].I = 0x170; + } + } + } + +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("ArcTan2: return=%08x\n", + reg[0].I); + } +#endif +} + +void BIOS_BitUnPack() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("BitUnPack: %08x,%08x,%08x (VCOUNT=%2d)\n", + reg[0].I, + reg[1].I, + reg[2].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + u32 header = reg[2].I; + + int len = CPUReadHalfWord(header); + // check address + int bits = CPUReadByte(header+2); + int revbits = 8 - bits; + // u32 value = 0; + u32 base = CPUReadMemory(header+4); + bool addBase = (base & 0x80000000) ? true : false; + base &= 0x7fffffff; + int dataSize = CPUReadByte(header+3); + + int data = 0; + int bitwritecount = 0; + while (1) + { + len -= 1; + if (len < 0) + break; + int mask = 0xff >> revbits; + u8 b = CPUReadByte(source); + source++; + int bitcount = 0; + while (1) + { + if (bitcount >= 8) + break; + u32 d = b & mask; + u32 temp = d >> bitcount; + if (!temp && addBase) + { + temp += base; + } + data |= temp << bitwritecount; + bitwritecount += dataSize; + if (bitwritecount >= 32) + { + CPUWriteMemory(dest, data); + dest += 4; + data = 0; + bitwritecount = 0; + } + mask <<= bits; + bitcount += bits; + } + } +} + +void BIOS_BgAffineSet() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("BgAffineSet: %08x,%08x,%08x (VCOUNT=%2d)\n", + reg[0].I, + reg[1].I, + reg[2].I, + VCOUNT); + } +#endif + + u32 src = reg[0].I; + u32 dest = reg[1].I; + int num = reg[2].I; + + for (int i = 0; i < num; i++) + { + s32 cx = CPUReadMemory(src); + src += 4; + s32 cy = CPUReadMemory(src); + src += 4; + s16 dispx = CPUReadHalfWord(src); + src += 2; + s16 dispy = CPUReadHalfWord(src); + src += 2; + s16 rx = CPUReadHalfWord(src); + src += 2; + s16 ry = CPUReadHalfWord(src); + src += 2; + u16 theta = CPUReadHalfWord(src)>>8; + src += 4; // keep structure alignment + s32 a = (s32)sineTable[(theta+0x40)&255]; + s32 b = (s32)sineTable[theta]; + + s16 dx = (s16)((rx * a)>>14); + s16 dmx = (s16)((rx * b)>>14); + s16 dy = (s16)((ry * b)>>14); + s16 dmy = (s16)((ry * a)>>14); + + CPUWriteHalfWord(dest, dx); + dest += 2; + CPUWriteHalfWord(dest, -dmx); + dest += 2; + CPUWriteHalfWord(dest, dy); + dest += 2; + CPUWriteHalfWord(dest, dmy); + dest += 2; + + s32 startx = cx - dx * dispx + dmx * dispy; + s32 starty = cy - dy * dispx - dmy * dispy; + + CPUWriteMemory(dest, startx); + dest += 4; + CPUWriteMemory(dest, starty); + dest += 4; + } +} + +void BIOS_CpuSet() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("CpuSet: 0x%08x,0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, reg[1].I, + reg[2].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + u32 cnt = reg[2].I; + + if (((source & 0xe000000) == 0) || + ((source + (((cnt << 11)>>9) & 0x1fffff)) & 0xe000000) == 0) + return; + + int count = cnt & 0x1FFFFF; + + // 32-bit ? + if ((cnt >> 26) & 1) + { + // needed for 32-bit mode! + source &= 0xFFFFFFFC; + dest &= 0xFFFFFFFC; + // fill ? + if ((cnt >> 24) & 1) + { + u32 value = CPUReadMemory(source); + while (count) + { + CPUWriteMemory(dest, value); + dest += 4; + count--; + } + } + else + { + // copy + while (count) + { + CPUWriteMemory(dest, CPUReadMemory(source)); + source += 4; + dest += 4; + count--; + } + } + } + else + { + // 16-bit fill? + if ((cnt >> 24) & 1) + { + u16 value = CPUReadHalfWord(source); + while (count) + { + CPUWriteHalfWord(dest, value); + dest += 2; + count--; + } + } + else + { + // copy + while (count) + { + CPUWriteHalfWord(dest, CPUReadHalfWord(source)); + source += 2; + dest += 2; + count--; + } + } + } +} + +void BIOS_CpuFastSet() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("CpuFastSet: 0x%08x,0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, reg[1].I, + reg[2].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + u32 cnt = reg[2].I; + + if (((source & 0xe000000) == 0) || + ((source + (((cnt << 11)>>9) & 0x1fffff)) & 0xe000000) == 0) + return; + + // needed for 32-bit mode! + source &= 0xFFFFFFFC; + dest &= 0xFFFFFFFC; + + int count = cnt & 0x1FFFFF; + + // fill? + if ((cnt >> 24) & 1) + { + while (count > 0) + { + // BIOS always transfers 32 bytes at a time + u32 value = CPUReadMemory(source); + for (int i = 0; i < 8; i++) + { + CPUWriteMemory(dest, value); + dest += 4; + } + count -= 8; + } + } + else + { + // copy + while (count > 0) + { + // BIOS always transfers 32 bytes at a time + for (int i = 0; i < 8; i++) + { + CPUWriteMemory(dest, CPUReadMemory(source)); + source += 4; + dest += 4; + } + count -= 8; + } + } +} + +void BIOS_Diff8bitUnFilterWram() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Diff8bitUnFilterWram: 0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, + reg[1].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if (((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff) & 0xe000000) == 0)) + return; + + int len = header >> 8; + + u8 data = CPUReadByte(source++); + CPUWriteByte(dest++, data); + len--; + + while (len > 0) + { + u8 diff = CPUReadByte(source++); + data += diff; + CPUWriteByte(dest++, data); + len--; + } +} + +void BIOS_Diff8bitUnFilterVram() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Diff8bitUnFilterVram: 0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, + reg[1].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if (((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + + u8 data = CPUReadByte(source++); + u16 writeData = data; + int shift = 8; + int bytes = 1; + + while (len >= 2) + { + u8 diff = CPUReadByte(source++); + data += diff; + writeData |= (data << shift); + bytes++; + shift += 8; + if (bytes == 2) + { + CPUWriteHalfWord(dest, writeData); + dest += 2; + len -= 2; + bytes = 0; + writeData = 0; + shift = 0; + } + } +} + +void BIOS_Diff16bitUnFilter() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Diff16bitUnFilter: 0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, + reg[1].I, VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if (((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + + u16 data = CPUReadHalfWord(source); + source += 2; + CPUWriteHalfWord(dest, data); + dest += 2; + len -= 2; + + while (len >= 2) + { + u16 diff = CPUReadHalfWord(source); + source += 2; + data += diff; + CPUWriteHalfWord(dest, data); + dest += 2; + len -= 2; + } +} + +void BIOS_Div() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Div: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + int number = reg[0].I; + int denom = reg[1].I; + + if (denom != 0) + { + reg[0].I = number / denom; + reg[1].I = number % denom; + s32 temp = (s32)reg[0].I; + reg[3].I = temp < 0 ? (u32)-temp : (u32)temp; + } +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Div: return=0x%08x,0x%08x,0x%08x\n", + reg[0].I, + reg[1].I, + reg[3].I); + } +#endif +} + +void BIOS_DivARM() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("DivARM: 0x%08x, (VCOUNT=%d)\n", + reg[0].I, + VCOUNT); + } +#endif + + u32 temp = reg[0].I; + reg[0].I = reg[1].I; + reg[1].I = temp; + BIOS_Div(); +} + +void BIOS_HuffUnComp() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("HuffUnComp: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if (((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + u8 treeSize = CPUReadByte(source++); + + u32 treeStart = source; + + source += (treeSize<<1) + 1; + + int len = header >> 8; + + u32 mask = 0x80000000; + u32 data = CPUReadMemory(source); + source += 4; + + int pos = 0; + u8 rootNode = CPUReadByte(treeStart); + u8 currentNode = rootNode; + bool writeData = false; + int byteShift = 0; + int byteCount = 0; + u32 writeValue = 0; + + if ((header & 0x0F) == 8) + { + while (len > 0) + { + // take left + if (pos == 0) + pos++; + else + pos += (((currentNode & 0x3F)+1)<<1); + + if (data & mask) + { + // right + if (currentNode & 0x40) + writeData = true; + currentNode = CPUReadByte(treeStart+pos+1); + } + else + { + // left + if (currentNode & 0x80) + writeData = true; + currentNode = CPUReadByte(treeStart+pos); + } + + if (writeData) + { + writeValue |= (currentNode << byteShift); + byteCount++; + byteShift += 8; + + pos = 0; + currentNode = rootNode; + writeData = false; + + if (byteCount == 4) + { + byteCount = 0; + byteShift = 0; + CPUWriteMemory(dest, writeValue); + writeValue = 0; + dest += 4; + len -= 4; + } + } + mask >>= 1; + if (mask == 0) + { + mask = 0x80000000; + data = CPUReadMemory(source); + source += 4; + } + } + } + else + { + int halfLen = 0; + int value = 0; + while (len > 0) + { + // take left + if (pos == 0) + pos++; + else + pos += (((currentNode & 0x3F)+1)<<1); + + if ((data & mask)) + { + // right + if (currentNode & 0x40) + writeData = true; + currentNode = CPUReadByte(treeStart+pos+1); + } + else + { + // left + if (currentNode & 0x80) + writeData = true; + currentNode = CPUReadByte(treeStart+pos); + } + + if (writeData) + { + if (halfLen == 0) + value |= currentNode; + else + value |= (currentNode<<4); + + halfLen += 4; + if (halfLen == 8) + { + writeValue |= (value << byteShift); + byteCount++; + byteShift += 8; + + halfLen = 0; + value = 0; + + if (byteCount == 4) + { + byteCount = 0; + byteShift = 0; + CPUWriteMemory(dest, writeValue); + dest += 4; + writeValue = 0; + len -= 4; + } + } + pos = 0; + currentNode = rootNode; + writeData = false; + } + mask >>= 1; + if (mask == 0) + { + mask = 0x80000000; + data = CPUReadMemory(source); + source += 4; + } + } + } +} + +void BIOS_LZ77UnCompVram() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("LZ77UnCompVram: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if (((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int byteCount = 0; + int byteShift = 0; + u32 writeValue = 0; + + int len = header >> 8; + + while (len > 0) + { + u8 d = CPUReadByte(source++); + + if (d) + { + for (int i = 0; i < 8; i++) + { + if (d & 0x80) + { + u16 data = CPUReadByte(source++) << 8; + data |= CPUReadByte(source++); + int length = (data >> 12) + 3; + int offset = (data & 0x0FFF); + u32 windowOffset = dest + byteCount - offset - 1; + for (int i = 0; i < length; i++) + { + writeValue |= (CPUReadByte(windowOffset++) << byteShift); + byteShift += 8; + byteCount++; + + if (byteCount == 2) + { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteCount = 0; + byteShift = 0; + writeValue = 0; + } + len--; + if (len == 0) + return; + } + } + else + { + writeValue |= (CPUReadByte(source++) << byteShift); + byteShift += 8; + byteCount++; + if (byteCount == 2) + { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteCount = 0; + byteShift = 0; + writeValue = 0; + } + len--; + if (len == 0) + return; + } + d <<= 1; + } + } + else + { + for (int i = 0; i < 8; i++) + { + writeValue |= (CPUReadByte(source++) << byteShift); + byteShift += 8; + byteCount++; + if (byteCount == 2) + { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteShift = 0; + byteCount = 0; + writeValue = 0; + } + len--; + if (len == 0) + return; + } + } + } +} + +void BIOS_LZ77UnCompWram() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("LZ77UnCompWram: 0x%08x,0x%08x (VCOUNT=%d)\n", reg[0].I, reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if (((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + + while (len > 0) + { + u8 d = CPUReadByte(source++); + + if (d) + { + for (int i = 0; i < 8; i++) + { + if (d & 0x80) + { + u16 data = CPUReadByte(source++) << 8; + data |= CPUReadByte(source++); + int length = (data >> 12) + 3; + int offset = (data & 0x0FFF); + u32 windowOffset = dest - offset - 1; + for (int i = 0; i < length; i++) + { + CPUWriteByte(dest++, CPUReadByte(windowOffset++)); + len--; + if (len == 0) + return; + } + } + else + { + CPUWriteByte(dest++, CPUReadByte(source++)); + len--; + if (len == 0) + return; + } + d <<= 1; + } + } + else + { + for (int i = 0; i < 8; i++) + { + CPUWriteByte(dest++, CPUReadByte(source++)); + len--; + if (len == 0) + return; + } + } + } +} + +void BIOS_ObjAffineSet() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("ObjAffineSet: 0x%08x,0x%08x,0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + reg[2].I, + reg[3].I, + VCOUNT); + } +#endif + + u32 src = reg[0].I; + u32 dest = reg[1].I; + int num = reg[2].I; + int offset = reg[3].I; + + for (int i = 0; i < num; i++) + { + s16 rx = CPUReadHalfWord(src); + src += 2; + s16 ry = CPUReadHalfWord(src); + src += 2; + u16 theta = CPUReadHalfWord(src)>>8; + src += 4; // keep structure alignment + + s32 a = (s32)sineTable[(theta+0x40)&255]; + s32 b = (s32)sineTable[theta]; + + s16 dx = (s16)((rx * a)>>14); + s16 dmx = (s16)((rx * b)>>14); + s16 dy = (s16)((ry * b)>>14); + s16 dmy = (s16)((ry * a)>>14); + + CPUWriteHalfWord(dest, dx); + dest += offset; + CPUWriteHalfWord(dest, -dmx); + dest += offset; + CPUWriteHalfWord(dest, dy); + dest += offset; + CPUWriteHalfWord(dest, dmy); + dest += offset; + } +} + +void BIOS_RegisterRamReset(u32 flags) +{ + // no need to trace here. this is only called directly from GBA.cpp + // to emulate bios initialization + + if (flags) + { + if (flags & 0x01) + { + // clear work RAM + memset(workRAM, 0, 0x40000); + } + if (flags & 0x02) + { + // clear internal RAM + memset(internalRAM, 0, 0x7e00); // don't clear 0x7e00-0x7fff + } + if (flags & 0x04) + { + // clear palette RAM + memset(paletteRAM, 0, 0x400); + } + if (flags & 0x08) + { + // clear VRAM + memset(vram, 0, 0x18000); + } + if (flags & 0x10) + { + // clean OAM + memset(oam, 0, 0x400); + } + + if (flags & 0x80) + { + int i; + for (i = 0; i < 8; i++) + CPUUpdateRegister(0x200+i*2, 0); + + CPUUpdateRegister(0x202, 0xFFFF); + + for (i = 0; i < 8; i++) + CPUUpdateRegister(0x4+i*2, 0); + + for (i = 0; i < 16; i++) + CPUUpdateRegister(0x20+i*2, 0); + + for (i = 0; i < 24; i++) + CPUUpdateRegister(0xb0+i*2, 0); + + CPUUpdateRegister(0x130, 0); + CPUUpdateRegister(0x20, 0x100); + CPUUpdateRegister(0x30, 0x100); + CPUUpdateRegister(0x26, 0x100); + CPUUpdateRegister(0x36, 0x100); + } + + if (flags & 0x20) + { + int i; + for (i = 0; i < 8; i++) + CPUUpdateRegister(0x110+i*2, 0); + CPUUpdateRegister(0x134, 0x8000); + for (i = 0; i < 7; i++) + CPUUpdateRegister(0x140+i*2, 0); + } + + if (flags & 0x40) + { + int i; + CPUWriteByte(0x4000084, 0); + CPUWriteByte(0x4000084, 0x80); + CPUWriteMemory(0x4000080, 0x880e0000); + CPUUpdateRegister(0x88, CPUReadHalfWord(0x4000088)&0x3ff); + CPUWriteByte(0x4000070, 0x70); + for (i = 0; i < 8; i++) + CPUUpdateRegister(0x90+i*2, 0); + CPUWriteByte(0x4000070, 0); + for (i = 0; i < 8; i++) + CPUUpdateRegister(0x90+i*2, 0); + CPUWriteByte(0x4000084, 0); + } + } +} + +void BIOS_RegisterRamReset() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("RegisterRamReset: 0x%08x (VCOUNT=%d)\n", + reg[0].I, + VCOUNT); + } +#endif + + BIOS_RegisterRamReset(reg[0].I); +} + +void BIOS_RLUnCompVram() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("RLUnCompVram: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if (((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + int byteCount = 0; + int byteShift = 0; + u32 writeValue = 0; + + while (len > 0) + { + u8 d = CPUReadByte(source++); + int l = d & 0x7F; + if (d & 0x80) + { + u8 data = CPUReadByte(source++); + l += 3; + for (int i = 0; i < l; i++) + { + writeValue |= (data << byteShift); + byteShift += 8; + byteCount++; + + if (byteCount == 2) + { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteCount = 0; + byteShift = 0; + writeValue = 0; + } + len--; + if (len == 0) + return; + } + } + else + { + l++; + for (int i = 0; i < l; i++) + { + writeValue |= (CPUReadByte(source++) << byteShift); + byteShift += 8; + byteCount++; + if (byteCount == 2) + { + CPUWriteHalfWord(dest, writeValue); + dest += 2; + byteCount = 0; + byteShift = 0; + writeValue = 0; + } + len--; + if (len == 0) + return; + } + } + } +} + +void BIOS_RLUnCompWram() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("RLUnCompWram: 0x%08x,0x%08x (VCOUNT=%d)\n", + reg[0].I, + reg[1].I, + VCOUNT); + } +#endif + + u32 source = reg[0].I; + u32 dest = reg[1].I; + + u32 header = CPUReadMemory(source); + source += 4; + + if (((source & 0xe000000) == 0) || + ((source + ((header >> 8) & 0x1fffff)) & 0xe000000) == 0) + return; + + int len = header >> 8; + + while (len > 0) + { + u8 d = CPUReadByte(source++); + int l = d & 0x7F; + if (d & 0x80) + { + u8 data = CPUReadByte(source++); + l += 3; + for (int i = 0; i < l; i++) + { + CPUWriteByte(dest++, data); + len--; + if (len == 0) + return; + } + } + else + { + l++; + for (int i = 0; i < l; i++) + { + CPUWriteByte(dest++, CPUReadByte(source++)); + len--; + if (len == 0) + return; + } + } + } +} + +void BIOS_SoftReset() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("SoftReset: (VCOUNT=%d)\n", VCOUNT); + } +#endif + + armState = true; + armMode = 0x1F; + armIrqEnable = false; + C_FLAG = V_FLAG = N_FLAG = Z_FLAG = false; + reg[13].I = 0x03007F00; + reg[14].I = 0x00000000; + reg[16].I = 0x00000000; + reg[R13_IRQ].I = 0x03007FA0; + reg[R14_IRQ].I = 0x00000000; + reg[SPSR_IRQ].I = 0x00000000; + reg[R13_SVC].I = 0x03007FE0; + reg[R14_SVC].I = 0x00000000; + reg[SPSR_SVC].I = 0x00000000; + u8 b = internalRAM[0x7ffa]; + + memset(&internalRAM[0x7e00], 0, 0x200); + + if (b) + { + armNextPC = 0x02000000; + reg[15].I = 0x02000004; + } + else + { + armNextPC = 0x08000000; + reg[15].I = 0x08000004; + } +} + +void BIOS_Sqrt() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Sqrt: %08x (VCOUNT=%2d)\n", + reg[0].I, + VCOUNT); + } +#endif + reg[0].I = (u32)sqrt((double)reg[0].I); +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("Sqrt: return=%08x\n", + reg[0].I); + } +#endif +} + +void BIOS_MidiKey2Freq() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("MidiKey2Freq: WaveData=%08x mk=%08x fp=%08x\n", + reg[0].I, + reg[1].I, + reg[2].I); + } +#endif + int freq = CPUReadMemory(reg[0].I+4); + double tmp; + tmp = ((double)(180 - reg[1].I)) - ((double)reg[2].I / 256.f); + tmp = pow((double)2.f, tmp / 12.f); + reg[0].I = (int)((double)freq / tmp); + +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("MidiKey2Freq: return %08x\n", + reg[0].I); + } +#endif +} + +void BIOS_SndDriverJmpTableCopy() +{ +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_SWI) + { + log("SndDriverJmpTableCopy: dest=%08x\n", + reg[0].I); + } +#endif + for (int i = 0; i < 0x24; i++) + { + CPUWriteMemory(reg[0].I, 0x9c); + reg[0].I += 4; + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/bios.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/bios.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,34 @@ +#ifndef VBA_BIOS_H +#define VBA_BIOS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../Port.h" + +extern void BIOS_ArcTan(); +extern void BIOS_ArcTan2(); +extern void BIOS_BitUnPack(); +extern void BIOS_BgAffineSet(); +extern void BIOS_CpuSet(); +extern void BIOS_CpuFastSet(); +extern void BIOS_Diff8bitUnFilterWram(); +extern void BIOS_Diff8bitUnFilterVram(); +extern void BIOS_Diff16bitUnFilter(); +extern void BIOS_Div(); +extern void BIOS_DivARM(); +extern void BIOS_HuffUnComp(); +extern void BIOS_LZ77UnCompVram(); +extern void BIOS_LZ77UnCompWram(); +extern void BIOS_ObjAffineSet(); +extern void BIOS_RegisterRamReset(); +extern void BIOS_RegisterRamReset(u32); +extern void BIOS_RLUnCompVram(); +extern void BIOS_RLUnCompWram(); +extern void BIOS_SoftReset(); +extern void BIOS_Sqrt(); +extern void BIOS_MidiKey2Freq(); +extern void BIOS_SndDriverJmpTableCopy(); + +#endif // VBA_BIOS_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/elf.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/elf.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,3229 @@ +#include +#include +#include + +#include "../Port.h" +#include "../NLS.h" +#include "../common/System.h" // systemMessage +#include "GBAGlobals.h" +#include "elf.h" + +#define elfReadMemory(addr) \ + READ32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define DW_TAG_array_type 0x01 +#define DW_TAG_enumeration_type 0x04 +#define DW_TAG_formal_parameter 0x05 +#define DW_TAG_label 0x0a +#define DW_TAG_lexical_block 0x0b +#define DW_TAG_member 0x0d +#define DW_TAG_pointer_type 0x0f +#define DW_TAG_reference_type 0x10 +#define DW_TAG_compile_unit 0x11 +#define DW_TAG_structure_type 0x13 +#define DW_TAG_subroutine_type 0x15 +#define DW_TAG_typedef 0x16 +#define DW_TAG_union_type 0x17 +#define DW_TAG_unspecified_parameters 0x18 +#define DW_TAG_inheritance 0x1c +#define DW_TAG_inlined_subroutine 0x1d +#define DW_TAG_subrange_type 0x21 +#define DW_TAG_base_type 0x24 +#define DW_TAG_const_type 0x26 +#define DW_TAG_enumerator 0x28 +#define DW_TAG_subprogram 0x2e +#define DW_TAG_variable 0x34 +#define DW_TAG_volatile_type 0x35 + +#define DW_AT_sibling 0x01 +#define DW_AT_location 0x02 +#define DW_AT_name 0x03 +#define DW_AT_byte_size 0x0b +#define DW_AT_bit_offset 0x0c +#define DW_AT_bit_size 0x0d +#define DW_AT_stmt_list 0x10 +#define DW_AT_low_pc 0x11 +#define DW_AT_high_pc 0x12 +#define DW_AT_language 0x13 +#define DW_AT_compdir 0x1b +#define DW_AT_const_value 0x1c +#define DW_AT_containing_type 0x1d +#define DW_AT_inline 0x20 +#define DW_AT_producer 0x25 +#define DW_AT_prototyped 0x27 +#define DW_AT_upper_bound 0x2f +#define DW_AT_abstract_origin 0x31 +#define DW_AT_accessibility 0x32 +#define DW_AT_artificial 0x34 +#define DW_AT_data_member_location 0x38 +#define DW_AT_decl_file 0x3a +#define DW_AT_decl_line 0x3b +#define DW_AT_declaration 0x3c +#define DW_AT_encoding 0x3e +#define DW_AT_external 0x3f +#define DW_AT_frame_base 0x40 +#define DW_AT_macro_info 0x43 +#define DW_AT_specification 0x47 +#define DW_AT_type 0x49 +#define DW_AT_virtuality 0x4c +#define DW_AT_vtable_elem_location 0x4d +// DWARF 2.1/3.0 extensions +#define DW_AT_entry_pc 0x52 +#define DW_AT_ranges 0x55 +// ARM Compiler extensions +#define DW_AT_proc_body 0x2000 +#define DW_AT_save_offset 0x2001 +#define DW_AT_user_2002 0x2002 +// MIPS extensions +#define DW_AT_MIPS_linkage_name 0x2007 + +#define DW_FORM_addr 0x01 +#define DW_FORM_data2 0x05 +#define DW_FORM_data4 0x06 +#define DW_FORM_string 0x08 +#define DW_FORM_block 0x09 +#define DW_FORM_block1 0x0a +#define DW_FORM_data1 0x0b +#define DW_FORM_flag 0x0c +#define DW_FORM_sdata 0x0d +#define DW_FORM_strp 0x0e +#define DW_FORM_udata 0x0f +#define DW_FORM_ref_addr 0x10 +#define DW_FORM_ref4 0x13 +#define DW_FORM_ref_udata 0x15 +#define DW_FORM_indirect 0x16 + +#define DW_OP_addr 0x03 +#define DW_OP_plus_uconst 0x23 +#define DW_OP_reg0 0x50 +#define DW_OP_reg1 0x51 +#define DW_OP_reg2 0x52 +#define DW_OP_reg3 0x53 +#define DW_OP_reg4 0x54 +#define DW_OP_reg5 0x55 +#define DW_OP_reg6 0x56 +#define DW_OP_reg7 0x57 +#define DW_OP_reg8 0x58 +#define DW_OP_reg9 0x59 +#define DW_OP_reg10 0x5a +#define DW_OP_reg11 0x5b +#define DW_OP_reg12 0x5c +#define DW_OP_reg13 0x5d +#define DW_OP_reg14 0x5e +#define DW_OP_reg15 0x5f +#define DW_OP_fbreg 0x91 + +#define DW_LNS_extended_op 0x00 +#define DW_LNS_copy 0x01 +#define DW_LNS_advance_pc 0x02 +#define DW_LNS_advance_line 0x03 +#define DW_LNS_set_file 0x04 +#define DW_LNS_set_column 0x05 +#define DW_LNS_negate_stmt 0x06 +#define DW_LNS_set_basic_block 0x07 +#define DW_LNS_const_add_pc 0x08 +#define DW_LNS_fixed_advance_pc 0x09 + +#define DW_LNE_end_sequence 0x01 +#define DW_LNE_set_address 0x02 +#define DW_LNE_define_file 0x03 + +#define DW_CFA_advance_loc 0x01 +#define DW_CFA_offset 0x02 +#define DW_CFA_restore 0x03 +#define DW_CFA_set_loc 0x01 +#define DW_CFA_advance_loc1 0x02 +#define DW_CFA_advance_loc2 0x03 +#define DW_CFA_advance_loc4 0x04 +#define DW_CFA_offset_extended 0x05 +#define DW_CFA_restore_extended 0x06 +#define DW_CFA_undefined 0x07 +#define DW_CFA_same_value 0x08 +#define DW_CFA_register 0x09 +#define DW_CFA_remember_state 0x0a +#define DW_CFA_restore_state 0x0b +#define DW_CFA_def_cfa 0x0c +#define DW_CFA_def_cfa_register 0x0d +#define DW_CFA_def_cfa_offset 0x0e +#define DW_CFA_nop 0x00 + +#define CASE_TYPE_TAG \ +case DW_TAG_const_type: \ +case DW_TAG_volatile_type: \ +case DW_TAG_pointer_type: \ +case DW_TAG_base_type: \ +case DW_TAG_array_type: \ +case DW_TAG_structure_type: \ +case DW_TAG_union_type: \ +case DW_TAG_typedef: \ +case DW_TAG_subroutine_type: \ +case DW_TAG_enumeration_type: \ +case DW_TAG_enumerator: \ +case DW_TAG_reference_type + +struct ELFcie +{ + ELFcie *next; + u32 offset; + u8 * augmentation; + u32 codeAlign; + s32 dataAlign; + int returnAddress; + u8 * data; + u32 dataLen; +}; + +struct ELFfde +{ + ELFcie *cie; + u32 address; + u32 end; + u8 * data; + u32 dataLen; +}; + +enum ELFRegMode +{ + REG_NOT_SET, + REG_OFFSET, + REG_REGISTER +}; + +struct ELFFrameStateRegister +{ + ELFRegMode mode; + int reg; + s32 offset; +}; + +struct ELFFrameStateRegisters +{ + ELFFrameStateRegister regs[16]; + ELFFrameStateRegisters *previous; +}; + +enum ELFCfaMode +{ + CFA_NOT_SET, + CFA_REG_OFFSET +}; + +struct ELFFrameState +{ + ELFFrameStateRegisters registers; + + ELFCfaMode cfaMode; + int cfaRegister; + s32 cfaOffset; + + u32 pc; + + int dataAlign; + int codeAlign; + int returnAddress; +}; + +extern bool8 cpuIsMultiBoot; + +Symbol *elfSymbols = NULL; +char * elfSymbolsStrTab = NULL; +int elfSymbolsCount = 0; + +ELFSectionHeader **elfSectionHeaders = NULL; +char *elfSectionHeadersStringTable = NULL; +int elfSectionHeadersCount = 0; +u8 * elfFileData = NULL; + +CompileUnit *elfCompileUnits = NULL; +DebugInfo * elfDebugInfo = NULL; +char * elfDebugStrings = NULL; + +ELFcie * elfCies = NULL; +ELFfde **elfFdes = NULL; +int elfFdeCount = 0; + +CompileUnit *elfCurrentUnit = NULL; + +u32 elfRead4Bytes(u8 *); +u16 elfRead2Bytes(u8 *); + +CompileUnit *elfGetCompileUnit(u32 addr) +{ + if (elfCompileUnits) + { + CompileUnit *unit = elfCompileUnits; + while (unit) + { + if (unit->lowPC) + { + if (addr >= unit->lowPC && addr < unit->highPC) + return unit; + } + else + { + ARanges *r = unit->ranges; + if (r) + { + int count = r->count; + for (int j = 0; j < count; j++) + { + if (addr >= r->ranges[j].lowPC && addr < r->ranges[j].highPC) + return unit; + } + } + } + unit = unit->next; + } + } + return NULL; +} + +char *elfGetAddressSymbol(u32 addr) +{ + static char buffer[256]; + + CompileUnit *unit = elfGetCompileUnit(addr); + // found unit, need to find function + if (unit) + { + Function *func = unit->functions; + while (func) + { + if (addr >= func->lowPC && addr < func->highPC) + { + int offset = addr - func->lowPC; + char *name = func->name; + if (!name) + name = ""; + if (offset) + sprintf(buffer, "%s+%d", name, offset); + else + strcpy(buffer, name); + return buffer; + } + func = func->next; + } + } + + if (elfSymbolsCount) + { + for (int i = 0; i < elfSymbolsCount; i++) + { + Symbol *s = &elfSymbols[i]; + if ((addr >= s->value) && addr < (s->value+s->size)) + { + int offset = addr-s->value; + char *name = s->name; + if (name == NULL) + name = ""; + if (offset) + sprintf(buffer, "%s+%d", name, addr-s->value); + else + strcpy(buffer, name); + return buffer; + } + else if (addr == s->value) + { + if (s->name) + strcpy(buffer, s->name); + else + strcpy(buffer, ""); + return buffer; + } + } + } + + return ""; +} + +bool elfFindLineInModule(u32 *addr, char *name, int line) +{ + CompileUnit *unit = elfCompileUnits; + + while (unit) + { + if (unit->lineInfoTable) + { + int i; + int count = unit->lineInfoTable->fileCount; + char *found = NULL; + for (i = 0; i < count; i++) + { + if (strcmp(name, unit->lineInfoTable->files[i]) == 0) + { + found = unit->lineInfoTable->files[i]; + break; + } + } + // found a matching filename... try to find line now + if (found) + { + LineInfoItem *table = unit->lineInfoTable->lines; + count = unit->lineInfoTable->number; + for (i = 0; i < count; i++) + { + if (table[i].file == found && table[i].line == line) + { + *addr = table[i].address; + return true; + } + } + // we can only find a single match + return false; + } + } + unit = unit->next; + } + return false; +} + +int elfFindLine(CompileUnit *unit, Function * /* func */, u32 addr, char **f) +{ + int currentLine = -1; + if (unit->hasLineInfo) + { + int count = unit->lineInfoTable->number; + LineInfoItem *table = unit->lineInfoTable->lines; + int i; + for (i = 0; i < count; i++) + { + if (addr <= table[i].address) + break; + } + if (i == count) + i--; + *f = table[i].file; + currentLine = table[i].line; + } + return currentLine; +} + +bool elfFindLineInUnit(u32 *addr, CompileUnit *unit, int line) +{ + if (unit->hasLineInfo) + { + int count = unit->lineInfoTable->number; + LineInfoItem *table = unit->lineInfoTable->lines; + int i; + for (i = 0; i < count; i++) + { + if (line == table[i].line) + { + *addr = table[i].address; + return true; + } + } + } + return false; +} + +bool elfGetCurrentFunction(u32 addr, Function **f, CompileUnit **u) +{ + CompileUnit *unit = elfGetCompileUnit(addr); + // found unit, need to find function + if (unit) + { + Function *func = unit->functions; + while (func) + { + if (addr >= func->lowPC && addr < func->highPC) + { + *f = func; + *u = unit; + return true; + } + func = func->next; + } + } + return false; +} + +bool elfGetObject(char *name, Function *f, CompileUnit *u, Object **o) +{ + if (f && u) + { + Object *v = f->variables; + + while (v) + { + if (strcmp(name, v->name) == 0) + { + *o = v; + return true; + } + v = v->next; + } + v = f->parameters; + while (v) + { + if (strcmp(name, v->name) == 0) + { + *o = v; + return true; + } + v = v->next; + } + v = u->variables; + while (v) + { + if (strcmp(name, v->name) == 0) + { + *o = v; + return true; + } + v = v->next; + } + } + + CompileUnit *c = elfCompileUnits; + + while (c) + { + if (c != u) + { + Object *v = c->variables; + while (v) + { + if (strcmp(name, v->name) == 0) + { + *o = v; + return true; + } + v = v->next; + } + } + c = c->next; + } + + return false; +} + +char *elfGetSymbol(int i, u32 *value, u32 *size, int *type) +{ + if (i < elfSymbolsCount) + { + Symbol *s = &elfSymbols[i]; + *value = s->value; + *size = s->size; + *type = s->type; + return s->name; + } + return NULL; +} + +bool elfGetSymbolAddress(char *sym, u32 *addr, u32 *size, int *type) +{ + if (elfSymbolsCount) + { + for (int i = 0; i < elfSymbolsCount; i++) + { + Symbol *s = &elfSymbols[i]; + if (strcmp(sym, s->name) == 0) + { + *addr = s->value; + *size = s->size; + *type = s->type; + return true; + } + } + } + return false; +} + +ELFfde *elfGetFde(u32 address) +{ + if (elfFdes) + { + int i; + for (i = 0; i < elfFdeCount; i++) + { + if (address >= elfFdes[i]->address && + address < elfFdes[i]->end) + { + return elfFdes[i]; + } + } + } + + return NULL; +} + +void elfExecuteCFAInstructions(ELFFrameState *state, u8 *data, u32 len, + u32 pc) +{ + u8 *end = data + len; + int bytes; + int reg; + ELFFrameStateRegisters *fs; + + while (data < end && state->pc < pc) + { + u8 op = *data++; + + switch (op >> 6) + { + case DW_CFA_advance_loc: + state->pc += (op & 0x3f) * state->codeAlign; + break; + case DW_CFA_offset: + reg = op & 0x3f; + state->registers.regs[reg].mode = REG_OFFSET; + state->registers.regs[reg].offset = state->dataAlign * + (s32)elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_CFA_restore: + // we don't care much about the other possible settings, + // so just setting to unset is enough for now + state->registers.regs[op & 0x3f].mode = REG_NOT_SET; + break; + case 0: + switch (op & 0x3f) + { + case DW_CFA_nop: + break; + case DW_CFA_advance_loc1: + state->pc += state->codeAlign * (*data++); + break; + case DW_CFA_advance_loc2: + state->pc += state->codeAlign * elfRead2Bytes(data); + data += 2; + break; + case DW_CFA_advance_loc4: + state->pc += state->codeAlign * elfRead4Bytes(data); + data += 4; + break; + case DW_CFA_offset_extended: + reg = elfReadLEB128(data, &bytes); + data += bytes; + state->registers.regs[reg].mode = REG_OFFSET; + state->registers.regs[reg].offset = state->dataAlign * + (s32)elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_CFA_restore_extended: + case DW_CFA_undefined: + case DW_CFA_same_value: + reg = elfReadLEB128(data, &bytes); + data += bytes; + state->registers.regs[reg].mode = REG_NOT_SET; + break; + case DW_CFA_register: + reg = elfReadLEB128(data, &bytes); + data += bytes; + state->registers.regs[reg].mode = REG_REGISTER; + state->registers.regs[reg].reg = elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_CFA_remember_state: + fs = (ELFFrameStateRegisters *)calloc(1, + sizeof(ELFFrameStateRegisters)); + memcpy(fs, &state->registers, sizeof(ELFFrameStateRegisters)); + state->registers.previous = fs; + break; + case DW_CFA_restore_state: + if (state->registers.previous == NULL) + { + printf("Error: previous frame state is NULL.\n"); + return; + } + fs = state->registers.previous; + memcpy(&state->registers, fs, sizeof(ELFFrameStateRegisters)); + free(fs); + break; + case DW_CFA_def_cfa: + state->cfaRegister = elfReadLEB128(data, &bytes); + data += bytes; + state->cfaOffset = (s32)elfReadLEB128(data, &bytes); + data += bytes; + state->cfaMode = CFA_REG_OFFSET; + break; + case DW_CFA_def_cfa_register: + state->cfaRegister = elfReadLEB128(data, &bytes); + data += bytes; + state->cfaMode = CFA_REG_OFFSET; + break; + case DW_CFA_def_cfa_offset: + state->cfaOffset = (s32)elfReadLEB128(data, &bytes); + data += bytes; + state->cfaMode = CFA_REG_OFFSET; + break; + default: + printf("Unknown CFA opcode %08x\n", op); + return; + } + break; + default: + printf("Unknown CFA opcode %08x\n", op); + return; + } + } +} + +ELFFrameState *elfGetFrameState(ELFfde *fde, u32 address) +{ + ELFFrameState *state = (ELFFrameState *)calloc(1, sizeof(ELFFrameState)); + state->pc = fde->address; + state->dataAlign = fde->cie->dataAlign; + state->codeAlign = fde->cie->codeAlign; + state->returnAddress = fde->cie->returnAddress; + + elfExecuteCFAInstructions(state, + fde->cie->data, + fde->cie->dataLen, + 0xffffffff); + elfExecuteCFAInstructions(state, + fde->data, + fde->dataLen, + address); + + return state; +} + +void elfPrintCallChain(u32 address) +{ + int count = 1; + + reg_pair regs[15]; + reg_pair newRegs[15]; + + memcpy(®s[0], ®[0], sizeof(reg_pair) * 15); + + while (count < 20) + { + char *addr = elfGetAddressSymbol(address); + if (*addr == 0) + addr = "???"; + + printf("%08x %s\n", address, addr); + + ELFfde *fde = elfGetFde(address); + + if (fde == NULL) + { + break; + } + + ELFFrameState *state = elfGetFrameState(fde, address); + + if (!state) + { + break; + } + + if (state->cfaMode == CFA_REG_OFFSET) + { + memcpy(&newRegs[0], ®s[0], sizeof(reg_pair) * 15); + u32 addr = 0; + for (int i = 0; i < 15; i++) + { + ELFFrameStateRegister *r = &state->registers. + regs[i]; + + switch (r->mode) + { + case REG_NOT_SET: + newRegs[i].I = regs[i].I; + break; + case REG_OFFSET: + newRegs[i].I = elfReadMemory(regs[state->cfaRegister].I + + state->cfaOffset + + r->offset); + break; + case REG_REGISTER: + newRegs[i].I = regs[r->reg].I; + break; + default: + printf("Unknown register mode: %d\n", r->mode); + break; + } + } + memcpy(regs, newRegs, sizeof(reg_pair)*15); + addr = newRegs[14].I; + addr &= 0xfffffffe; + address = addr; + count++; + } + else + { + printf("CFA not set\n"); + break; + } + if (state->registers.previous) + { + ELFFrameStateRegisters *prev = state->registers.previous; + + while (prev) + { + ELFFrameStateRegisters *p = prev->previous; + free(prev); + prev = p; + } + } + free(state); + } +} + +u32 elfDecodeLocation(Function *f, ELFBlock *o, LocationType *type, u32 base) +{ + u32 framebase = 0; + if (f && f->frameBase) + { + ELFBlock *b = f->frameBase; + switch (*b->data) + { + case DW_OP_reg0: + case DW_OP_reg1: + case DW_OP_reg2: + case DW_OP_reg3: + case DW_OP_reg4: + case DW_OP_reg5: + case DW_OP_reg6: + case DW_OP_reg7: + case DW_OP_reg8: + case DW_OP_reg9: + case DW_OP_reg10: + case DW_OP_reg11: + case DW_OP_reg12: + case DW_OP_reg13: + case DW_OP_reg14: + case DW_OP_reg15: + framebase = reg[*b->data-0x50].I; + break; + default: + fprintf(stderr, "Unknown frameBase %02x\n", *b->data); + break; + } + } + + ELFBlock *loc = o; + u32 location = 0; + int bytes = 0; + if (loc) + { + switch (*loc->data) + { + case DW_OP_addr: + location = elfRead4Bytes(loc->data+1); + *type = LOCATION_memory; + break; + case DW_OP_plus_uconst: + location = base + elfReadLEB128(loc->data+1, &bytes); + *type = LOCATION_memory; + break; + case DW_OP_reg0: + case DW_OP_reg1: + case DW_OP_reg2: + case DW_OP_reg3: + case DW_OP_reg4: + case DW_OP_reg5: + case DW_OP_reg6: + case DW_OP_reg7: + case DW_OP_reg8: + case DW_OP_reg9: + case DW_OP_reg10: + case DW_OP_reg11: + case DW_OP_reg12: + case DW_OP_reg13: + case DW_OP_reg14: + case DW_OP_reg15: + location = *loc->data - 0x50; + *type = LOCATION_register; + break; + case DW_OP_fbreg: + { + int bytes; + s32 off = elfReadSignedLEB128(loc->data+1, &bytes); + location = framebase + off; + *type = LOCATION_memory; + break; + } + default: + fprintf(stderr, "Unknown location %02x\n", *loc->data); + break; + } + } + return location; +} + +u32 elfDecodeLocation(Function *f, ELFBlock *o, LocationType *type) +{ + return elfDecodeLocation(f, o, type, 0); +} + +// reading function + +u32 elfRead4Bytes(u8 *data) +{ + u32 value = *data++; + value |= (*data++ << 8); + value |= (*data++ << 16); + value |= (*data << 24); + return value; +} + +u16 elfRead2Bytes(u8 *data) +{ + u16 value = *data++; + value |= (*data << 8); + return value; +} + +char *elfReadString(u8 *data, int *bytesRead) +{ + if (*data == 0) + { + *bytesRead = 1; + return NULL; + } + *bytesRead = strlen((char *)data) + 1; + return (char *)data; +} + +s32 elfReadSignedLEB128(u8 *data, int *bytesRead) +{ + s32 result = 0; + int shift = 0; + int count = 0; + + u8 byte; + do + { + byte = *data++; + count++; + result |= (byte & 0x7f) << shift; + shift += 7; + } + while (byte & 0x80); + if ((shift < 32) && (byte & 0x40)) + result |= -(1 << shift); + *bytesRead = count; + return result; +} + +u32 elfReadLEB128(u8 *data, int *bytesRead) +{ + u32 result = 0; + int shift = 0; + int count = 0; + u8 byte; + do + { + byte = *data++; + count++; + result |= (byte & 0x7f) << shift; + shift += 7; + } + while (byte & 0x80); + *bytesRead = count; + return result; +} + +u8 *elfReadSection(u8 *data, ELFSectionHeader *sh) +{ + return data + READ32LE(&sh->offset); +} + +ELFSectionHeader *elfGetSectionByName(char *name) +{ + for (int i = 0; i < elfSectionHeadersCount; i++) + { + if (strcmp(name, + &elfSectionHeadersStringTable[READ32LE(&elfSectionHeaders[i]-> + name)]) == 0) + { + return elfSectionHeaders[i]; + } + } + return NULL; +} + +ELFSectionHeader *elfGetSectionByNumber(int number) +{ + if (number < elfSectionHeadersCount) + { + return elfSectionHeaders[number]; + } + return NULL; +} + +CompileUnit *elfGetCompileUnitForData(u8 *data) +{ + u8 *end = elfCurrentUnit->top + 4 + elfCurrentUnit->length; + + if (data >= elfCurrentUnit->top && data < end) + return elfCurrentUnit; + + CompileUnit *unit = elfCompileUnits; + + while (unit) + { + end = unit->top + 4 + unit->length; + + if (data >= unit->top && data < end) + return unit; + + unit = unit->next; + } + + printf("Error: cannot find reference to compile unit at offset %08x\n", + (int)(data - elfDebugInfo->infodata)); + exit(-1); +} + +u8 *elfReadAttribute(u8 *data, ELFAttr *attr) +{ + int bytes; + int form = attr->form; +start: + switch (form) + { + case DW_FORM_addr: + attr->value = elfRead4Bytes(data); + data += 4; + break; + case DW_FORM_data2: + attr->value = elfRead2Bytes(data); + data += 2; + break; + case DW_FORM_data4: + attr->value = elfRead4Bytes(data); + data += 4; + break; + case DW_FORM_string: + attr->string = (char *)data; + data += strlen(attr->string)+1; + break; + case DW_FORM_strp: + attr->string = elfDebugStrings + elfRead4Bytes(data); + data += 4; + break; + case DW_FORM_block: + attr->block = (ELFBlock *)malloc(sizeof(ELFBlock)); + attr->block->length = elfReadLEB128(data, &bytes); + data += bytes; + attr->block->data = data; + data += attr->block->length; + break; + case DW_FORM_block1: + attr->block = (ELFBlock *)malloc(sizeof(ELFBlock)); + attr->block->length = *data++; + attr->block->data = data; + data += attr->block->length; + break; + case DW_FORM_data1: + attr->value = *data++; + break; + case DW_FORM_flag: + attr->flag = (*data++) ? true : false; + break; + case DW_FORM_sdata: + attr->value = elfReadSignedLEB128(data, &bytes); + data += bytes; + break; + case DW_FORM_udata: + attr->value = elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_FORM_ref_addr: + attr->value = (elfDebugInfo->infodata + elfRead4Bytes(data)) - + elfGetCompileUnitForData(data)->top; + data += 4; + break; + case DW_FORM_ref4: + attr->value = elfRead4Bytes(data); + data += 4; + break; + case DW_FORM_ref_udata: + attr->value = (elfDebugInfo->infodata + + (elfGetCompileUnitForData(data)->top - + elfDebugInfo->infodata) + + elfReadLEB128(data, &bytes)) - + elfCurrentUnit->top; + data += bytes; + break; + case DW_FORM_indirect: + form = elfReadLEB128(data, &bytes); + data += bytes; + goto start; + default: + fprintf(stderr, "Unsupported FORM %02x\n", form); + exit(-1); + } + return data; +} + +ELFAbbrev *elfGetAbbrev(ELFAbbrev **table, u32 number) +{ + int hash = number % 121; + + ELFAbbrev *abbrev = table[hash]; + + while (abbrev) + { + if (abbrev->number == number) + return abbrev; + abbrev = abbrev->next; + } + return NULL; +} + +ELFAbbrev * *elfReadAbbrevs(u8 *data, u32 offset) +{ + data += offset; + ELFAbbrev **abbrevs = (ELFAbbrev * *)calloc(sizeof(ELFAbbrev *)*121, 1); + int bytes = 0; + u32 number = elfReadLEB128(data, &bytes); + data += bytes; + while (number) + { + ELFAbbrev *abbrev = (ELFAbbrev *)calloc(sizeof(ELFAbbrev), 1); + + // read tag information + abbrev->number = number; + abbrev->tag = elfReadLEB128(data, &bytes); + data += bytes; + abbrev->hasChildren = *data++ ? true : false; + + // read attributes + int name = elfReadLEB128(data, &bytes); + data += bytes; + int form = elfReadLEB128(data, &bytes); + data += bytes; + + while (name) + { + if ((abbrev->numAttrs % 4) == 0) + { + abbrev->attrs = (ELFAttr *)realloc(abbrev->attrs, + (abbrev->numAttrs + 4) * + sizeof(ELFAttr)); + } + abbrev->attrs[abbrev->numAttrs].name = name; + abbrev->attrs[abbrev->numAttrs++].form = form; + + name = elfReadLEB128(data, &bytes); + data += bytes; + form = elfReadLEB128(data, &bytes); + data += bytes; + } + + int hash = number % 121; + abbrev->next = abbrevs[hash]; + abbrevs[hash] = abbrev; + + number = elfReadLEB128(data, &bytes); + data += bytes; + + if (elfGetAbbrev(abbrevs, number) != NULL) + break; + } + + return abbrevs; +} + +void elfParseCFA(u8 *top) +{ + ELFSectionHeader *h = elfGetSectionByName(".debug_frame"); + + if (h == NULL) + { + return; + } + + u8 *data = elfReadSection(top, h); + + u8 *topOffset = data; + + u8 *end = data + READ32LE(&h->size); + + ELFcie *cies = NULL; + + while (data < end) + { + u32 offset = data - topOffset; + u32 len = elfRead4Bytes(data); + data += 4; + + u8 *dataEnd = data + len; + + u32 id = elfRead4Bytes(data); + data += 4; + + if (id == 0xffffffff) + { + // skip version + *data++; + + ELFcie *cie = (ELFcie *)calloc(1, sizeof(ELFcie)); + + cie->next = cies; + cies = cie; + + cie->offset = offset; + + cie->augmentation = data; + while (*data) + data++; + data++; + + if (*cie->augmentation) + { + fprintf(stderr, "Error: augmentation not supported\n"); + exit(-1); + } + + int bytes; + cie->codeAlign = elfReadLEB128(data, &bytes); + data += bytes; + + cie->dataAlign = elfReadSignedLEB128(data, &bytes); + data += bytes; + + cie->returnAddress = *data++; + + cie->data = data; + cie->dataLen = dataEnd - data; + } + else + { + ELFfde *fde = (ELFfde *)calloc(1, sizeof(ELFfde)); + + ELFcie *cie = cies; + + while (cie != NULL) + { + if (cie->offset == id) + break; + cie = cie->next; + } + + if (!cie) + { + fprintf(stderr, "Cannot find CIE %08x\n", id); + exit(-1); + } + + fde->cie = cie; + + fde->address = elfRead4Bytes(data); + data += 4; + + fde->end = fde->address + elfRead4Bytes(data); + data += 4; + + fde->data = data; + fde->dataLen = dataEnd - data; + + if ((elfFdeCount %10) == 0) + { + elfFdes = (ELFfde * *)realloc(elfFdes, (elfFdeCount+10) * + sizeof(ELFfde *)); + } + elfFdes[elfFdeCount++] = fde; + } + data = dataEnd; + } + + elfCies = cies; +} + +void elfAddLine(LineInfo *l, u32 a, int file, int line, int *max) +{ + if (l->number == *max) + { + *max += 1000; + l->lines = (LineInfoItem *)realloc(l->lines, *max*sizeof(LineInfoItem)); + } + LineInfoItem *li = &l->lines[l->number]; + li->file = l->files[file-1]; + li->address = a; + li->line = line; + l->number++; +} + +void elfParseLineInfo(CompileUnit *unit, u8 *top) +{ + ELFSectionHeader *h = elfGetSectionByName(".debug_line"); + if (h == NULL) + { + fprintf(stderr, "No line information found\n"); + return; + } + LineInfo *l = unit->lineInfoTable = (LineInfo *)calloc(1, sizeof(LineInfo)); + l->number = 0; + int max = 1000; + l->lines = (LineInfoItem *)malloc(1000*sizeof(LineInfoItem)); + + u8 *data = elfReadSection(top, h); + data += unit->lineInfo; + u32 totalLen = elfRead4Bytes(data); + data += 4; + u8 *end = data + totalLen; + // u16 version = elfRead2Bytes(data); + data += 2; + // u32 offset = elfRead4Bytes(data); + data += 4; + int minInstrSize = *data++; + int defaultIsStmt = *data++; + int lineBase = (s8)*data++; + int lineRange = *data++; + int opcodeBase = *data++; + u8 *stdOpLen = (u8 *)malloc(opcodeBase * sizeof(u8)); + stdOpLen[0] = 1; + int i; + for (i = 1; i < opcodeBase; i++) + stdOpLen[i] = *data++; + + free(stdOpLen); // todo + int bytes = 0; + + char *s; + while ((s = elfReadString(data, &bytes)) != NULL) + { + data += bytes; + // fprintf(stderr, "Directory is %s\n", s); + } + data += bytes; + int count = 4; + int index = 0; + l->files = (char * *)malloc(sizeof(char *)*count); + + while ((s = elfReadString(data, &bytes)) != NULL) + { + l->files[index++] = s; + + data += bytes; + // directory + elfReadLEB128(data, &bytes); + data += bytes; + // time + elfReadLEB128(data, &bytes); + data += bytes; + // size + elfReadLEB128(data, &bytes); + data += bytes; + // fprintf(stderr, "File is %s\n", s); + if (index == count) + { + count += 4; + l->files = (char * *)realloc(l->files, sizeof(char *)*count); + } + } + l->fileCount = index; + data += bytes; + + while (data < end) + { + u32 address = 0; + int file = 1; + int line = 1; + int col = 0; + int isStmt = defaultIsStmt; + int basicBlock = 0; + int endSeq = 0; + + while (!endSeq) + { + int op = *data++; + switch (op) + { + case DW_LNS_extended_op: + { + data++; + op = *data++; + switch (op) + { + case DW_LNE_end_sequence: + endSeq = 1; + break; + case DW_LNE_set_address: + address = elfRead4Bytes(data); + data += 4; + break; + default: + fprintf(stderr, "Unknown extended LINE opcode %02x\n", op); + exit(-1); + } + break; + } + case DW_LNS_copy: + // fprintf(stderr, "Address %08x line %d (%d)\n", address, line, file); + elfAddLine(l, address, file, line, &max); + basicBlock = 0; + break; + case DW_LNS_advance_pc: + address += minInstrSize * elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_LNS_advance_line: + line += elfReadSignedLEB128(data, &bytes); + data += bytes; + break; + case DW_LNS_set_file: + file = elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_LNS_set_column: + col = elfReadLEB128(data, &bytes); + data += bytes; + break; + case DW_LNS_negate_stmt: + isStmt = !isStmt; + break; + case DW_LNS_set_basic_block: + basicBlock = 1; + break; + case DW_LNS_const_add_pc: + address += (minInstrSize *((255 - opcodeBase)/lineRange)); + break; + case DW_LNS_fixed_advance_pc: + address += elfRead2Bytes(data); + data += 2; + break; + default: + op = op - opcodeBase; + address += (op / lineRange) * minInstrSize; + line += lineBase + (op % lineRange); + elfAddLine(l, address, file, line, &max); + // fprintf(stderr, "Address %08x line %d (%d)\n", address, line,file); + basicBlock = 1; + break; + } + } + } + l->lines = (LineInfoItem *)realloc(l->lines, l->number*sizeof(LineInfoItem)); +} + +u8 *elfSkipData(u8 *data, ELFAbbrev *abbrev, ELFAbbrev **abbrevs) +{ + int i; + int bytes; + + for (i = 0; i < abbrev->numAttrs; i++) + { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if (abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if (abbrev->hasChildren) + { + int nesting = 1; + while (nesting) + { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if (!abbrevNum) + { + nesting--; + continue; + } + + abbrev = elfGetAbbrev(abbrevs, abbrevNum); + + for (i = 0; i < abbrev->numAttrs; i++) + { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if (abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if (abbrev->hasChildren) + { + nesting++; + } + } + } + return data; +} + +Type *elfParseType(CompileUnit *unit, u32); +u8 *elfParseObject(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Object **object); +u8 *elfParseFunction(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Function **function); +void elfCleanUp(Function *); + +void elfAddType(Type *type, CompileUnit *unit, u32 offset) +{ + if (type->next == NULL) + { + if (unit->types != type && type->offset == 0) + { + type->offset = offset; + type->next = unit->types; + unit->types = type; + } + } +} + +void elfParseType(u8 *data, u32 offset, ELFAbbrev *abbrev, CompileUnit *unit, + Type **type) +{ + switch (abbrev->tag) + { + case DW_TAG_typedef: + { + u32 typeref = 0; + char *name = NULL; + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_name: + name = attr->string; + break; + case DW_AT_type: + typeref = attr->value; + break; + case DW_AT_decl_file: + case DW_AT_decl_line: + break; + default: + fprintf(stderr, "Unknown attribute for typedef %02x\n", attr->name); + break; + } + } + if (abbrev->hasChildren) + fprintf(stderr, "Unexpected children for typedef\n"); + *type = elfParseType(unit, typeref); + if (name) + (*type)->name = name; + return; + break; + } + case DW_TAG_union_type: + case DW_TAG_structure_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + if (abbrev->tag == DW_TAG_structure_type) + t->type = TYPE_struct; + else + t->type = TYPE_union; + + Struct *s = (Struct *)calloc(sizeof(Struct), 1); + t->structure = s; + elfAddType(t, unit, offset); + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_name: + t->name = attr->string; + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + case DW_AT_decl_file: + case DW_AT_decl_line: + case DW_AT_sibling: + case DW_AT_containing_type: // todo? + case DW_AT_declaration: + case DW_AT_specification: // TODO: + break; + default: + fprintf(stderr, "Unknown attribute for struct %02x\n", attr->name); + break; + } + } + if (abbrev->hasChildren) + { + int bytes; + u32 num = elfReadLEB128(data, &bytes); + data += bytes; + int index = 0; + while (num) + { + ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num); + + switch (abbr->tag) + { + case DW_TAG_member: + { + if ((index % 4) == 0) + s->members = (Member *)realloc(s->members, + sizeof(Member)*(index+4)); + Member *m = &s->members[index]; + m->location = NULL; + m->bitOffset = 0; + m->bitSize = 0; + m->byteSize = 0; + for (int i = 0; i < abbr->numAttrs; i++) + { + ELFAttr *attr = &abbr->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_name: + m->name = attr->string; + break; + case DW_AT_type: + m->type = elfParseType(unit, attr->value); + break; + case DW_AT_data_member_location: + m->location = attr->block; + break; + case DW_AT_byte_size: + m->byteSize = attr->value; + break; + case DW_AT_bit_offset: + m->bitOffset = attr->value; + break; + case DW_AT_bit_size: + m->bitSize = attr->value; + break; + case DW_AT_decl_file: + case DW_AT_decl_line: + case DW_AT_accessibility: + case DW_AT_artificial: // todo? + break; + default: + fprintf(stderr, "Unknown member attribute %02x\n", + attr->name); + } + } + index++; + break; + } + case DW_TAG_subprogram: + { + Function *fnc = NULL; + data = elfParseFunction(data, abbr, unit, &fnc); + if (fnc != NULL) + { + if (unit->lastFunction) + unit->lastFunction->next = fnc; + else + unit->functions = fnc; + unit->lastFunction = fnc; + } + break; + } + case DW_TAG_inheritance: + // TODO: add support + data = elfSkipData(data, abbr, unit->abbrevs); + break; +CASE_TYPE_TAG: + // skip types... parsed only when used + data = elfSkipData(data, abbr, unit->abbrevs); + break; + case DW_TAG_variable: + data = elfSkipData(data, abbr, unit->abbrevs); + break; + default: + fprintf(stderr, "Unknown struct tag %02x %s\n", abbr->tag, t->name); + data = elfSkipData(data, abbr, unit->abbrevs); + break; + } + num = elfReadLEB128(data, &bytes); + data += bytes; + } + s->memberCount = index; + } + *type = t; + return; + break; + } + case DW_TAG_base_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + + t->type = TYPE_base; + elfAddType(t, unit, offset); + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_name: + t->name = attr->string; + break; + case DW_AT_encoding: + t->encoding = attr->value; + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + case DW_AT_bit_size: + t->bitSize = attr->value; + break; + default: + fprintf(stderr, "Unknown attribute for base type %02x\n", + attr->name); + break; + } + } + if (abbrev->hasChildren) + fprintf(stderr, "Unexpected children for base type\n"); + *type = t; + return; + break; + } + case DW_TAG_pointer_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + + t->type = TYPE_pointer; + + elfAddType(t, unit, offset); + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_type: + t->pointer = elfParseType(unit, attr->value); + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + default: + fprintf(stderr, "Unknown pointer type attribute %02x\n", attr->name); + break; + } + } + if (abbrev->hasChildren) + fprintf(stderr, "Unexpected children for pointer type\n"); + *type = t; + return; + break; + } + case DW_TAG_reference_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + + t->type = TYPE_reference; + + elfAddType(t, unit, offset); + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_type: + t->pointer = elfParseType(unit, attr->value); + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + default: + fprintf(stderr, "Unknown ref type attribute %02x\n", attr->name); + break; + } + } + if (abbrev->hasChildren) + fprintf(stderr, "Unexpected children for ref type\n"); + *type = t; + return; + break; + } + case DW_TAG_volatile_type: + { + u32 typeref = 0; + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_type: + typeref = attr->value; + break; + default: + fprintf(stderr, "Unknown volatile attribute for type %02x\n", + attr->name); + break; + } + } + if (abbrev->hasChildren) + fprintf(stderr, "Unexpected children for volatile type\n"); + *type = elfParseType(unit, typeref); + return; + break; + } + case DW_TAG_const_type: + { + u32 typeref = 0; + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_type: + typeref = attr->value; + break; + default: + fprintf(stderr, "Unknown const attribute for type %02x\n", + attr->name); + break; + } + } + if (abbrev->hasChildren) + fprintf(stderr, "Unexpected children for const type\n"); + *type = elfParseType(unit, typeref); + return; + break; + } + case DW_TAG_enumeration_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + t->type = TYPE_enum; + Enum *e = (Enum *)calloc(sizeof(Enum), 1); + t->enumeration = e; + elfAddType(t, unit, offset); + int count = 0; + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_name: + t->name = attr->string; + break; + case DW_AT_byte_size: + t->size = attr->value; + break; + case DW_AT_sibling: + case DW_AT_decl_file: + case DW_AT_decl_line: + break; + default: + fprintf(stderr, "Unknown enum attribute %02x\n", attr->name); + } + } + if (abbrev->hasChildren) + { + int bytes; + u32 num = elfReadLEB128(data, &bytes); + data += bytes; + while (num) + { + ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num); + + switch (abbr->tag) + { + case DW_TAG_enumerator: + { + count++; + e->members = (EnumMember *)realloc(e->members, + count*sizeof(EnumMember)); + EnumMember *m = &e->members[count-1]; + for (int i = 0; i < abbr->numAttrs; i++) + { + ELFAttr *attr = &abbr->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_name: + m->name = attr->string; + break; + case DW_AT_const_value: + m->value = attr->value; + break; + default: + fprintf(stderr, "Unknown sub param attribute %02x\n", + attr->name); + } + } + break; + } + default: + fprintf(stderr, "Unknown enum tag %02x\n", abbr->tag); + data = elfSkipData(data, abbr, unit->abbrevs); + break; + } + num = elfReadLEB128(data, &bytes); + data += bytes; + } + } + e->count = count; + *type = t; + return; + break; + } + case DW_TAG_subroutine_type: + { + Type *t = (Type *)calloc(sizeof(Type), 1); + t->type = TYPE_function; + FunctionType *f = (FunctionType *)calloc(sizeof(FunctionType), 1); + t->function = f; + elfAddType(t, unit, offset); + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_prototyped: + case DW_AT_sibling: + break; + case DW_AT_type: + f->returnType = elfParseType(unit, attr->value); + break; + default: + fprintf(stderr, "Unknown subroutine attribute %02x\n", attr->name); + } + } + if (abbrev->hasChildren) + { + int bytes; + u32 num = elfReadLEB128(data, &bytes); + data += bytes; + Object *lastVar = NULL; + while (num) + { + ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num); + + switch (abbr->tag) + { + case DW_TAG_formal_parameter: + { + Object *o; + data = elfParseObject(data, abbr, unit, &o); + if (f->args) + lastVar->next = o; + else + f->args = o; + lastVar = o; + break; + } + case DW_TAG_unspecified_parameters: + // no use in the debugger yet + data = elfSkipData(data, abbr, unit->abbrevs); + break; +CASE_TYPE_TAG: + // skip types... parsed only when used + data = elfSkipData(data, abbr, unit->abbrevs); + break; + default: + fprintf(stderr, "Unknown subroutine tag %02x\n", abbr->tag); + data = elfSkipData(data, abbr, unit->abbrevs); + break; + } + num = elfReadLEB128(data, &bytes); + data += bytes; + } + } + *type = t; + return; + break; + } + case DW_TAG_array_type: + { + u32 typeref = 0; + int i; + Array *array = (Array *)calloc(sizeof(Array), 1); + Type * t = (Type *)calloc(sizeof(Type), 1); + t->type = TYPE_array; + elfAddType(t, unit, offset); + + for (i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_sibling: + break; + case DW_AT_type: + typeref = attr->value; + array->type = elfParseType(unit, typeref); + break; + default: + fprintf(stderr, "Unknown array attribute %02x\n", attr->name); + } + } + if (abbrev->hasChildren) + { + int bytes; + u32 num = elfReadLEB128(data, &bytes); + data += bytes; + int index = 0; + int maxBounds = 0; + while (num) + { + ELFAbbrev *abbr = elfGetAbbrev(unit->abbrevs, num); + + switch (abbr->tag) + { + case DW_TAG_subrange_type: + { + if (maxBounds == index) + { + maxBounds += 4; + array->bounds = (int *)realloc(array->bounds, + sizeof(int)*maxBounds); + } + for (int i = 0; i < abbr->numAttrs; i++) + { + ELFAttr *attr = &abbr->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_upper_bound: + array->bounds[index] = attr->value+1; + break; + case DW_AT_type: // ignore + break; + default: + fprintf(stderr, "Unknown subrange attribute %02x\n", + attr->name); + } + } + index++; + break; + } + default: + fprintf(stderr, "Unknown array tag %02x\n", abbr->tag); + data = elfSkipData(data, abbr, unit->abbrevs); + break; + } + num = elfReadLEB128(data, &bytes); + data += bytes; + } + array->maxBounds = index; + } + t->size = array->type->size; + for (i = 0; i < array->maxBounds; i++) + t->size *= array->bounds[i]; + t->array = array; + *type = t; + return; + break; + } + default: + fprintf(stderr, "Unknown type TAG %02x\n", abbrev->tag); + exit(-1); + } +} + +Type *elfParseType(CompileUnit *unit, u32 offset) +{ + Type *t = unit->types; + + while (t) + { + if (t->offset == offset) + return t; + t = t->next; + } + if (offset == 0) + { + Type *t = (Type *)calloc(sizeof(Type), 1); + t->type = TYPE_void; + t->offset = 0; + elfAddType(t, unit, 0); + return t; + } + u8 *data = unit->top + offset; + int bytes; + int abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + Type *type = NULL; + + ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + elfParseType(data, offset, abbrev, unit, &type); + return type; +} + +void elfGetObjectAttributes(CompileUnit *unit, u32 offset, Object *o) +{ + u8 *data = unit->top + offset; + int bytes; + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if (!abbrevNum) + { + return; + } + + ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_location: + o->location = attr->block; + break; + case DW_AT_name: + if (o->name == NULL) + o->name = attr->string; + break; + case DW_AT_MIPS_linkage_name: + o->name = attr->string; + break; + case DW_AT_decl_file: + o->file = attr->value; + break; + case DW_AT_decl_line: + o->line = attr->value; + break; + case DW_AT_type: + o->type = elfParseType(unit, attr->value); + break; + case DW_AT_external: + o->external = attr->flag; + break; + case DW_AT_const_value: + case DW_AT_abstract_origin: + case DW_AT_declaration: + case DW_AT_artificial: + // todo + break; + case DW_AT_specification: + // TODO: + break; + default: + fprintf(stderr, "Unknown object attribute %02x\n", attr->name); + break; + } + } +} + +u8 *elfParseObject(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Object **object) +{ + Object *o = (Object *)calloc(sizeof(Object), 1); + + o->next = NULL; + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_location: + o->location = attr->block; + break; + case DW_AT_name: + if (o->name == NULL) + o->name = attr->string; + break; + case DW_AT_MIPS_linkage_name: + o->name = attr->string; + break; + case DW_AT_decl_file: + o->file = attr->value; + break; + case DW_AT_decl_line: + o->line = attr->value; + break; + case DW_AT_type: + o->type = elfParseType(unit, attr->value); + break; + case DW_AT_external: + o->external = attr->flag; + break; + case DW_AT_abstract_origin: + elfGetObjectAttributes(unit, attr->value, o); + break; + case DW_AT_const_value: + case DW_AT_declaration: + case DW_AT_artificial: + break; + case DW_AT_specification: + // TODO: + break; + default: + fprintf(stderr, "Unknown object attribute %02x\n", attr->name); + break; + } + } + *object = o; + return data; +} + +u8 *elfParseBlock(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Function *func, Object **lastVar) +{ + int bytes; + u32 start = func->lowPC; + u32 end = func->highPC; + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_sibling: + break; + case DW_AT_low_pc: + start = attr->value; + break; + case DW_AT_high_pc: + end = attr->value; + break; + case DW_AT_ranges: // ignore for now + break; + default: + fprintf(stderr, "Unknown block attribute %02x\n", attr->name); + break; + } + } + + if (abbrev->hasChildren) + { + int nesting = 1; + + while (nesting) + { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if (!abbrevNum) + { + nesting--; + continue; + } + + abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + switch (abbrev->tag) + { +CASE_TYPE_TAG: // types only parsed when used + case DW_TAG_label: // not needed + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + case DW_TAG_lexical_block: + data = elfParseBlock(data, abbrev, unit, func, lastVar); + break; + case DW_TAG_subprogram: + { + Function *f = NULL; + data = elfParseFunction(data, abbrev, unit, &f); + if (f != NULL) + { + if (unit->lastFunction) + unit->lastFunction->next = f; + else + unit->functions = f; + unit->lastFunction = f; + } + break; + } + case DW_TAG_variable: + { + Object *o; + data = elfParseObject(data, abbrev, unit, &o); + if (o->startScope == 0) + o->startScope = start; + if (o->endScope == 0) + o->endScope = 0; + if (func->variables) + (*lastVar)->next = o; + else + func->variables = o; + *lastVar = o; + break; + } + case DW_TAG_inlined_subroutine: + // TODO: + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + default: + { + fprintf(stderr, "Unknown block TAG %02x\n", abbrev->tag); + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + } + } + } + } + return data; +} + +void elfGetFunctionAttributes(CompileUnit *unit, u32 offset, Function *func) +{ + u8 *data = unit->top + offset; + int bytes; + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if (!abbrevNum) + { + return; + } + + ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + + switch (attr->name) + { + case DW_AT_sibling: + break; + case DW_AT_name: + if (func->name == NULL) + func->name = attr->string; + break; + case DW_AT_MIPS_linkage_name: + func->name = attr->string; + break; + case DW_AT_low_pc: + func->lowPC = attr->value; + break; + case DW_AT_high_pc: + func->highPC = attr->value; + break; + case DW_AT_decl_file: + func->file = attr->value; + break; + case DW_AT_decl_line: + func->line = attr->value; + break; + case DW_AT_external: + func->external = attr->flag; + break; + case DW_AT_frame_base: + func->frameBase = attr->block; + break; + case DW_AT_type: + func->returnType = elfParseType(unit, attr->value); + break; + case DW_AT_inline: + case DW_AT_specification: + case DW_AT_declaration: + case DW_AT_artificial: + case DW_AT_prototyped: + case DW_AT_proc_body: + case DW_AT_save_offset: + case DW_AT_user_2002: + case DW_AT_virtuality: + case DW_AT_containing_type: + case DW_AT_accessibility: + // todo; + break; + case DW_AT_vtable_elem_location: + free(attr->block); + break; + default: + fprintf(stderr, "Unknown function attribute %02x\n", attr->name); + break; + } + } + + return; +} + +u8 *elfParseFunction(u8 *data, ELFAbbrev *abbrev, CompileUnit *unit, + Function **f) +{ + Function *func = (Function *)calloc(sizeof(Function), 1); + *f = func; + + int bytes; + bool mangled = false; + bool declaration = false; + for (int i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + switch (attr->name) + { + case DW_AT_sibling: + break; + case DW_AT_name: + if (func->name == NULL) + func->name = attr->string; + break; + case DW_AT_MIPS_linkage_name: + func->name = attr->string; + mangled = true; + break; + case DW_AT_low_pc: + func->lowPC = attr->value; + break; + case DW_AT_high_pc: + func->highPC = attr->value; + break; + case DW_AT_prototyped: + break; + case DW_AT_decl_file: + func->file = attr->value; + break; + case DW_AT_decl_line: + func->line = attr->value; + break; + case DW_AT_external: + func->external = attr->flag; + break; + case DW_AT_frame_base: + func->frameBase = attr->block; + break; + case DW_AT_type: + func->returnType = elfParseType(unit, attr->value); + break; + case DW_AT_abstract_origin: + elfGetFunctionAttributes(unit, attr->value, func); + break; + case DW_AT_declaration: + declaration = attr->flag; + break; + case DW_AT_inline: + case DW_AT_specification: + case DW_AT_artificial: + case DW_AT_proc_body: + case DW_AT_save_offset: + case DW_AT_user_2002: + case DW_AT_virtuality: + case DW_AT_containing_type: + case DW_AT_accessibility: + // todo; + break; + case DW_AT_vtable_elem_location: + free(attr->block); + break; + default: + fprintf(stderr, "Unknown function attribute %02x\n", attr->name); + break; + } + } + + if (declaration) + { + elfCleanUp(func); + free(func); + *f = NULL; + + while (1) + { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if (!abbrevNum) + { + return data; + } + + abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + data = elfSkipData(data, abbrev, unit->abbrevs); + } + } + + if (abbrev->hasChildren) + { + int nesting = 1; + Object *lastParam = NULL; + Object *lastVar = NULL; + + while (nesting) + { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if (!abbrevNum) + { + nesting--; + continue; + } + + abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + + switch (abbrev->tag) + { +CASE_TYPE_TAG: // no need to parse types. only parsed when used + case DW_TAG_label: // not needed + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + case DW_TAG_subprogram: + { + Function *fnc = NULL; + data = elfParseFunction(data, abbrev, unit, &fnc); + if (fnc != NULL) + { + if (unit->lastFunction == NULL) + unit->functions = fnc; + else + unit->lastFunction->next = fnc; + unit->lastFunction = fnc; + } + break; + } + case DW_TAG_lexical_block: + { + data = elfParseBlock(data, abbrev, unit, func, &lastVar); + break; + } + case DW_TAG_formal_parameter: + { + Object *o; + data = elfParseObject(data, abbrev, unit, &o); + if (func->parameters) + lastParam->next = o; + else + func->parameters = o; + lastParam = o; + break; + } + case DW_TAG_variable: + { + Object *o; + data = elfParseObject(data, abbrev, unit, &o); + if (func->variables) + lastVar->next = o; + else + func->variables = o; + lastVar = o; + break; + } + case DW_TAG_unspecified_parameters: + case DW_TAG_inlined_subroutine: + { + // todo + for (int i = 0; i < abbrev->numAttrs; i++) + { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if (abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if (abbrev->hasChildren) + nesting++; + break; + } + default: + { + fprintf(stderr, "Unknown function TAG %02x\n", abbrev->tag); + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + } + } + } + } + return data; +} + +u8 *elfParseUnknownData(u8 *data, ELFAbbrev *abbrev, ELFAbbrev **abbrevs) +{ + int i; + int bytes; + // switch(abbrev->tag) { + // default: + fprintf(stderr, "Unknown TAG %02x\n", abbrev->tag); + + for (i = 0; i < abbrev->numAttrs; i++) + { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if (abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if (abbrev->hasChildren) + { + int nesting = 1; + while (nesting) + { + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + if (!abbrevNum) + { + nesting--; + continue; + } + + abbrev = elfGetAbbrev(abbrevs, abbrevNum); + + fprintf(stderr, "Unknown TAG %02x\n", abbrev->tag); + + for (i = 0; i < abbrev->numAttrs; i++) + { + data = elfReadAttribute(data, &abbrev->attrs[i]); + if (abbrev->attrs[i].form == DW_FORM_block1) + free(abbrev->attrs[i].block); + } + + if (abbrev->hasChildren) + { + nesting++; + } + } + } + // } + return data; +} + +u8 *elfParseCompileUnitChildren(u8 *data, CompileUnit *unit) +{ + int bytes; + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + Object *lastObj = NULL; + while (abbrevNum) + { + ELFAbbrev *abbrev = elfGetAbbrev(unit->abbrevs, abbrevNum); + switch (abbrev->tag) + { + case DW_TAG_subprogram: + { + Function *func = NULL; + data = elfParseFunction(data, abbrev, unit, &func); + if (func != NULL) + { + if (unit->lastFunction) + unit->lastFunction->next = func; + else + unit->functions = func; + unit->lastFunction = func; + } + break; + } +CASE_TYPE_TAG: + data = elfSkipData(data, abbrev, unit->abbrevs); + break; + case DW_TAG_variable: + { + Object *var = NULL; + data = elfParseObject(data, abbrev, unit, &var); + if (lastObj) + lastObj->next = var; + else + unit->variables = var; + lastObj = var; + break; + } + default: + data = elfParseUnknownData(data, abbrev, unit->abbrevs); + break; + } + + abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + } + return data; +} + +CompileUnit *elfParseCompUnit(u8 *data, u8 *abbrevData) +{ + int bytes; + u8 *top = data; + + u32 length = elfRead4Bytes(data); + data += 4; + + u16 version = elfRead2Bytes(data); + data += 2; + + u32 offset = elfRead4Bytes(data); + data += 4; + + u8 addrSize = *data++; + + if (version != 2) + { + fprintf(stderr, "Unsupported debugging information version %d\n", version); + return NULL; + } + + if (addrSize != 4) + { + fprintf(stderr, "Unsupported address size %d\n", addrSize); + return NULL; + } + + ELFAbbrev **abbrevs = elfReadAbbrevs(abbrevData, offset); + + u32 abbrevNum = elfReadLEB128(data, &bytes); + data += bytes; + + ELFAbbrev *abbrev = elfGetAbbrev(abbrevs, abbrevNum); + + CompileUnit *unit = (CompileUnit *)calloc(sizeof(CompileUnit), 1); + unit->top = top; + unit->length = length; + unit->abbrevs = abbrevs; + unit->next = NULL; + + elfCurrentUnit = unit; + + int i; + + for (i = 0; i < abbrev->numAttrs; i++) + { + ELFAttr *attr = &abbrev->attrs[i]; + data = elfReadAttribute(data, attr); + + switch (attr->name) + { + case DW_AT_name: + unit->name = attr->string; + break; + case DW_AT_stmt_list: + unit->hasLineInfo = true; + unit->lineInfo = attr->value; + break; + case DW_AT_low_pc: + unit->lowPC = attr->value; + break; + case DW_AT_high_pc: + unit->highPC = attr->value; + break; + case DW_AT_compdir: + unit->compdir = attr->string; + break; + // ignore + case DW_AT_language: + case DW_AT_producer: + case DW_AT_macro_info: + case DW_AT_entry_pc: + break; + default: + fprintf(stderr, "Unknown attribute %02x\n", attr->name); + break; + } + } + + if (abbrev->hasChildren) + elfParseCompileUnitChildren(data, unit); + + return unit; +} + +void elfParseAranges(u8 *data) +{ + ELFSectionHeader *sh = elfGetSectionByName(".debug_aranges"); + if (sh == NULL) + { + fprintf(stderr, "No aranges found\n"); + return; + } + + data = elfReadSection(data, sh); + u8 *end = data + READ32LE(&sh->size); + + int max = 4; + ARanges *ranges = (ARanges *)calloc(sizeof(ARanges), 4); + + int index = 0; + + while (data < end) + { + u32 len = elfRead4Bytes(data); + data += 4; + // u16 version = elfRead2Bytes(data); + data += 2; + u32 offset = elfRead4Bytes(data); + data += 4; + // u8 addrSize = *data++; + // u8 segSize = *data++; + data += 2; // remove if uncommenting above + data += 4; + ranges[index].count = (len-20)/8; + ranges[index].offset = offset; + ranges[index].ranges = (ARange *)calloc(sizeof(ARange), (len-20)/8); + int i = 0; + while (true) + { + u32 addr = elfRead4Bytes(data); + data += 4; + u32 len = elfRead4Bytes(data); + data += 4; + if (addr == 0 && len == 0) + break; + ranges[index].ranges[i].lowPC = addr; + ranges[index].ranges[i].highPC = addr+len; + i++; + } + index++; + if (index == max) + { + max += 4; + ranges = (ARanges *)realloc(ranges, max*sizeof(ARanges)); + } + } + elfDebugInfo->numRanges = index; + elfDebugInfo->ranges = ranges; +} + +void elfReadSymtab(u8 *data) +{ + ELFSectionHeader *sh = elfGetSectionByName(".symtab"); + int table = READ32LE(&sh->link); + + char *strtable = (char *)elfReadSection(data, elfGetSectionByNumber(table)); + + ELFSymbol *symtab = (ELFSymbol *)elfReadSection(data, sh); + + int count = READ32LE(&sh->size) / sizeof(ELFSymbol); + elfSymbolsCount = 0; + + elfSymbols = (Symbol *)malloc(sizeof(Symbol)*count); + + int i; + + for (i = 0; i < count; i++) + { + ELFSymbol *s = &symtab[i]; + int type = s->info & 15; + int binding = s->info >> 4; + + if (binding) + { + Symbol *sym = &elfSymbols[elfSymbolsCount]; + sym->name = &strtable[READ32LE(&s->name)]; + sym->binding = binding; + sym->type = type; + sym->value = READ32LE(&s->value); + sym->size = READ32LE(&s->size); + elfSymbolsCount++; + } + } + for (i = 0; i < count; i++) + { + ELFSymbol *s = &symtab[i]; + int bind = s->info>>4; + int type = s->info & 15; + + if (!bind) + { + Symbol *sym = &elfSymbols[elfSymbolsCount]; + sym->name = &strtable[READ32LE(&s->name)]; + sym->binding = (s->info >> 4); + sym->type = type; + sym->value = READ32LE(&s->value); + sym->size = READ32LE(&s->size); + elfSymbolsCount++; + } + } + elfSymbolsStrTab = strtable; + // free(symtab); +} + +bool elfReadProgram(ELFHeader *eh, u8 *data, int& size, bool parseDebug) +{ + int count = READ16LE(&eh->e_phnum); + int i; + + if (READ32LE(&eh->e_entry) == 0x2000000) + cpuIsMultiBoot = true; + + // read program headers... should probably move this code down + u8 *p = data + READ32LE(&eh->e_phoff); + size = 0; + for (i = 0; i < count; i++) + { + ELFProgramHeader *ph = (ELFProgramHeader *)p; + p += sizeof(ELFProgramHeader); + if (READ16LE(&eh->e_phentsize) != sizeof(ELFProgramHeader)) + { + p += READ16LE(&eh->e_phentsize) - sizeof(ELFProgramHeader); + } + + // printf("PH %d %08x %08x %08x %08x %08x %08x %08x %08x\n", + // i, ph->type, ph->offset, ph->vaddr, ph->paddr, + // ph->filesz, ph->memsz, ph->flags, ph->align); + if (cpuIsMultiBoot) + { + if (READ32LE(&ph->paddr) >= 0x2000000 && + READ32LE(&ph->paddr) <= 0x203ffff) + { + memcpy(&workRAM[READ32LE(&ph->paddr) & 0x3ffff], + data + READ32LE(&ph->offset), + READ32LE(&ph->filesz)); + } + } + else + { + if (READ32LE(&ph->paddr) >= 0x8000000 && + READ32LE(&ph->paddr) <= 0x9ffffff) + { + memcpy(&rom[READ32LE(&ph->paddr) & 0x1ffffff], + data + READ32LE(&ph->offset), + READ32LE(&ph->filesz)); + size += READ32LE(&ph->filesz); + } + } + } + + char *stringTable = NULL; + + // read section headers + p = data + READ32LE(&eh->e_shoff); + count = READ16LE(&eh->e_shnum); + + ELFSectionHeader **sh = (ELFSectionHeader * *) + malloc(sizeof(ELFSectionHeader *) * count); + + for (i = 0; i < count; i++) + { + sh[i] = (ELFSectionHeader *)p; + p += sizeof(ELFSectionHeader); + if (READ16LE(&eh->e_shentsize) != sizeof(ELFSectionHeader)) + p += READ16LE(&eh->e_shentsize) - sizeof(ELFSectionHeader); + } + + if (READ16LE(&eh->e_shstrndx) != 0) + { + stringTable = (char *)elfReadSection(data, + sh[READ16LE(&eh->e_shstrndx)]); + } + + elfSectionHeaders = sh; + elfSectionHeadersStringTable = stringTable; + elfSectionHeadersCount = count; + + for (i = 0; i < count; i++) + { + // printf("SH %d %-20s %08x %08x %08x %08x %08x %08x %08x %08x\n", + // i, &stringTable[sh[i]->name], sh[i]->name, sh[i]->type, + // sh[i]->flags, sh[i]->addr, sh[i]->offset, sh[i]->size, + // sh[i]->link, sh[i]->info); + if (READ32LE(&sh[i]->flags) & 2) // load section + { + if (cpuIsMultiBoot) + { + if (READ32LE(&sh[i]->addr) >= 0x2000000 && + READ32LE(&sh[i]->addr) <= 0x203ffff) + { + memcpy(&workRAM[READ32LE(&sh[i]->addr) & 0x3ffff], data + + READ32LE(&sh[i]->offset), + READ32LE(&sh[i]->size)); + } + } + else + { + if (READ32LE(&sh[i]->addr) >= 0x8000000 && + READ32LE(&sh[i]->addr) <= 0x9ffffff) + { + memcpy(&rom[READ32LE(&sh[i]->addr) & 0x1ffffff], + data + READ32LE(&sh[i]->offset), + READ32LE(&sh[i]->size)); + size += READ32LE(&sh[i]->size); + } + } + } + } + + if (parseDebug) + { + fprintf(stderr, "Parsing debug info\n"); + + ELFSectionHeader *dbgHeader = elfGetSectionByName(".debug_info"); + if (dbgHeader == NULL) + { + fprintf(stderr, "Cannot find debug information\n"); + goto end; + } + + ELFSectionHeader *h = elfGetSectionByName(".debug_abbrev"); + if (h == NULL) + { + fprintf(stderr, "Cannot find abbreviation table\n"); + goto end; + } + + elfDebugInfo = (DebugInfo *)calloc(sizeof(DebugInfo), 1); + u8 *abbrevdata = elfReadSection(data, h); + + h = elfGetSectionByName(".debug_str"); + + if (h == NULL) + elfDebugStrings = NULL; + else + elfDebugStrings = (char *)elfReadSection(data, h); + + u8 *debugdata = elfReadSection(data, dbgHeader); + + elfDebugInfo->debugdata = data; + elfDebugInfo->infodata = debugdata; + + u32 total = READ32LE(&dbgHeader->size); + u8 *end = debugdata + total; + u8 *ddata = debugdata; + + CompileUnit *last = NULL; + CompileUnit *unit = NULL; + + while (ddata < end) + { + unit = elfParseCompUnit(ddata, abbrevdata); + unit->offset = ddata-debugdata; + elfParseLineInfo(unit, data); + if (last == NULL) + elfCompileUnits = unit; + else + last->next = unit; + last = unit; + ddata += 4 + unit->length; + } + elfParseAranges(data); + CompileUnit *comp = elfCompileUnits; + while (comp) + { + ARanges *r = elfDebugInfo->ranges; + for (int i = 0; i < elfDebugInfo->numRanges; i++) + if (r[i].offset == comp->offset) + { + comp->ranges = &r[i]; + break; + } + comp = comp->next; + } + elfParseCFA(data); + elfReadSymtab(data); + } +end: + if (sh) + { + free(sh); + } + + elfSectionHeaders = NULL; + elfSectionHeadersStringTable = NULL; + elfSectionHeadersCount = 0; + + return true; +} + +extern bool8 parseDebug; + +bool elfRead(const char *name, int& siz, FILE *f) +{ + fseek(f, 0, SEEK_END); + long size = ftell(f); + elfFileData = (u8 *)malloc(size); + fseek(f, 0, SEEK_SET); + fread(elfFileData, 1, size, f); + fclose(f); + + ELFHeader *header = (ELFHeader *)elfFileData; + + if (READ32LE(&header->magic) != 0x464C457F || + READ16LE(&header->e_machine) != 40 || + header->clazz != 1) + { + systemMessage(0, N_("Not a valid ELF file %s"), name); + free(elfFileData); + elfFileData = NULL; + return false; + } + + if (!elfReadProgram(header, elfFileData, siz, parseDebug)) + { + free(elfFileData); + elfFileData = NULL; + return false; + } + + return true; +} + +void elfCleanUp(Object *o) +{ + free(o->location); +} + +void elfCleanUp(Function *func) +{ + Object *o = func->parameters; + while (o) + { + elfCleanUp(o); + Object *next = o->next; + free(o); + o = next; + } + + o = func->variables; + while (o) + { + elfCleanUp(o); + Object *next = o->next; + free(o); + o = next; + } + free(func->frameBase); +} + +void elfCleanUp(ELFAbbrev **abbrevs) +{ + for (int i = 0; i < 121; i++) + { + ELFAbbrev *abbrev = abbrevs[i]; + + while (abbrev) + { + free(abbrev->attrs); + ELFAbbrev *next = abbrev->next; + free(abbrev); + + abbrev = next; + } + } +} + +void elfCleanUp(Type *t) +{ + switch (t->type) + { + case TYPE_function: + if (t->function) + { + Object *o = t->function->args; + while (o) + { + elfCleanUp(o); + Object *next = o->next; + free(o); + o = next; + } + free(t->function); + } + break; + case TYPE_array: + if (t->array) + { + free(t->array->bounds); + free(t->array); + } + break; + case TYPE_struct: + case TYPE_union: + if (t->structure) + { + for (int i = 0; i < t->structure->memberCount; i++) + { + free(t->structure->members[i].location); + } + free(t->structure->members); + free(t->structure); + } + break; + case TYPE_enum: + if (t->enumeration) + { + free(t->enumeration->members); + free(t->enumeration); + } + break; + case TYPE_base: + case TYPE_pointer: + case TYPE_void: + case TYPE_reference: + break; // nothing to do + } +} + +void elfCleanUp(CompileUnit *comp) +{ + elfCleanUp(comp->abbrevs); + free(comp->abbrevs); + Function *func = comp->functions; + while (func) + { + elfCleanUp(func); + Function *next = func->next; + free(func); + func = next; + } + Type *t = comp->types; + while (t) + { + elfCleanUp(t); + Type *next = t->next; + free(t); + t = next; + } + Object *o = comp->variables; + while (o) + { + elfCleanUp(o); + Object *next = o->next; + free(o); + o = next; + } + if (comp->lineInfoTable) + { + free(comp->lineInfoTable->lines); + free(comp->lineInfoTable->files); + free(comp->lineInfoTable); + } +} + +void elfCleanUp() +{ + CompileUnit *comp = elfCompileUnits; + + while (comp) + { + elfCleanUp(comp); + CompileUnit *next = comp->next; + free(comp); + comp = next; + } + elfCompileUnits = NULL; + free(elfSymbols); + elfSymbols = NULL; + // free(elfSymbolsStrTab); + elfSymbolsStrTab = NULL; + + elfDebugStrings = NULL; + if (elfDebugInfo) + { + int num = elfDebugInfo->numRanges; + int i; + for (i = 0; i < num; i++) + { + free(elfDebugInfo->ranges[i].ranges); + } + free(elfDebugInfo->ranges); + free(elfDebugInfo); + elfDebugInfo = NULL; + } + + if (elfFdes) + { + if (elfFdeCount) + { + for (int i = 0; i < elfFdeCount; i++) + free(elfFdes[i]); + } + free(elfFdes); + + elfFdes = NULL; + elfFdeCount = 0; + } + + ELFcie *cie = elfCies; + while (cie) + { + ELFcie *next = cie->next; + free(cie); + cie = next; + } + elfCies = NULL; + + if (elfFileData) + { + free(elfFileData); + elfFileData = NULL; + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/elf.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/elf.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,296 @@ +#ifndef VBA_ELF_H +#define VBA_ELF_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +enum LocationType +{ + LOCATION_register, + LOCATION_memory, + LOCATION_value +}; + +#define DW_ATE_boolean 0x02 +#define DW_ATE_signed 0x05 +#define DW_ATE_unsigned 0x07 +#define DW_ATE_unsigned_char 0x08 + +struct ELFHeader +{ + u32 magic; + u8 clazz; + u8 data; + u8 version; + u8 pad[9]; + u16 e_type; + u16 e_machine; + u32 e_version; + u32 e_entry; + u32 e_phoff; + u32 e_shoff; + u32 e_flags; + u16 e_ehsize; + u16 e_phentsize; + u16 e_phnum; + u16 e_shentsize; + u16 e_shnum; + u16 e_shstrndx; +}; + +struct ELFProgramHeader +{ + u32 type; + u32 offset; + u32 vaddr; + u32 paddr; + u32 filesz; + u32 memsz; + u32 flags; + u32 align; +}; + +struct ELFSectionHeader +{ + u32 name; + u32 type; + u32 flags; + u32 addr; + u32 offset; + u32 size; + u32 link; + u32 info; + u32 addralign; + u32 entsize; +}; + +struct ELFSymbol +{ + u32 name; + u32 value; + u32 size; + u8 info; + u8 other; + u16 shndx; +}; + +struct ELFBlock +{ + int length; + u8 *data; +}; + +struct ELFAttr +{ + u32 name; + u32 form; + union + { + u32 value; + char * string; + u8 * data; + bool flag; + ELFBlock *block; + }; +}; + +struct ELFAbbrev +{ + u32 number; + u32 tag; + bool hasChildren; + int numAttrs; + ELFAttr * attrs; + ELFAbbrev *next; +}; + +enum TypeEnum +{ + TYPE_base, + TYPE_pointer, + TYPE_function, + TYPE_void, + TYPE_array, + TYPE_struct, + TYPE_reference, + TYPE_enum, + TYPE_union +}; + +struct Type; +struct Object; + +struct FunctionType +{ + Type * returnType; + Object *args; +}; + +struct Member +{ + char * name; + Type * type; + int bitSize; + int bitOffset; + int byteSize; + ELFBlock *location; +}; + +struct Struct +{ + int memberCount; + Member *members; +}; + +struct Array +{ + Type *type; + int maxBounds; + int * bounds; +}; + +struct EnumMember +{ + char *name; + u32 value; +}; + +struct Enum +{ + int count; + EnumMember *members; +}; + +struct Type +{ + u32 offset; + TypeEnum type; + char * name; + int encoding; + int size; + int bitSize; + union + { + Type * pointer; + FunctionType *function; + Array * array; + Struct * structure; + Enum * enumeration; + }; + Type *next; +}; + +struct Object +{ + char * name; + int file; + int line; + bool external; + Type * type; + ELFBlock *location; + u32 startScope; + u32 endScope; + Object * next; +}; + +struct Function +{ + char * name; + u32 lowPC; + u32 highPC; + int file; + int line; + bool external; + Type * returnType; + Object * parameters; + Object * variables; + ELFBlock *frameBase; + Function *next; +}; + +struct LineInfoItem +{ + u32 address; + char *file; + int line; +}; + +struct LineInfo +{ + int fileCount; + char ** files; + int number; + LineInfoItem *lines; +}; + +struct ARange +{ + u32 lowPC; + u32 highPC; +}; + +struct ARanges +{ + u32 offset; + int count; + ARange *ranges; +}; + +struct CompileUnit +{ + u32 length; + u8 * top; + u32 offset; + ELFAbbrev ** abbrevs; + ARanges * ranges; + char * name; + char * compdir; + u32 lowPC; + u32 highPC; + bool hasLineInfo; + u32 lineInfo; + LineInfo * lineInfoTable; + Function * functions; + Function * lastFunction; + Object * variables; + Type * types; + CompileUnit *next; +}; + +struct DebugInfo +{ + u8 * debugfile; + u8 * abbrevdata; + u8 * debugdata; + u8 * infodata; + int numRanges; + ARanges *ranges; +}; + +struct Symbol +{ + char *name; + int type; + int binding; + u32 address; + u32 value; + u32 size; +}; + +extern u32 elfReadLEB128(u8 *, int *); +extern s32 elfReadSignedLEB128(u8 *, int *); +extern bool elfRead(const char *, int &, FILE *f); +extern bool elfGetSymbolAddress(char *, u32 *, u32 *, int *); +extern char *elfGetAddressSymbol(u32); +extern char *elfGetSymbol(int, u32 *, u32 *, int *); +extern void elfCleanUp(); +extern bool elfGetCurrentFunction(u32, Function **, CompileUnit **c); +extern bool elfGetObject(char *, Function *, CompileUnit *, Object * *); +extern bool elfFindLineInUnit(u32 *, CompileUnit *, int); +extern bool elfFindLineInModule(u32 *, char *, int); +u32 elfDecodeLocation(Function *, ELFBlock *, LocationType *); +u32 elfDecodeLocation(Function *, ELFBlock *, LocationType *, u32); +int elfFindLine(CompileUnit *unit, Function *func, u32 addr, char * *); + +#endif // VBA_ELF_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/remote.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/remote.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,712 @@ +#include +#include +#include + +#ifndef WIN32 +# include +# include +# include +# ifdef HAVE_NETINET_IN_H +# include +# endif // HAVE_NETINET_IN_H +# ifdef HAVE_ARPA_INET_H +# include +# else // ! HAVE_ARPA_INET_H +# define socklen_t int +# endif // ! HAVE_ARPA_INET_H +#else // WIN32 +# include "../win32/stdafx.h" +# include +# include +# define socklen_t int +# define close closesocket +# define read _read +# define write _write +#endif // WIN32 + +#include "GBA.h" +#include "GBAGlobals.h" + +extern bool debugger; +extern void CPUUpdateCPSR(); +#ifdef SDL +extern void (*dbgMain)(); +extern void (*dbgSignal)(int, int); +extern void debuggerMain(); +extern void debuggerSignal(int, int); +#endif + +int remotePort = 55555; +int remoteSignal = 5; +int remoteSocket = -1; +int remoteListenSocket = -1; +bool remoteConnected = false; +bool remoteResumed = false; + +int (*remoteSendFnc)(char *, int) = NULL; +int (*remoteRecvFnc)(char *, int) = NULL; +bool (*remoteInitFnc)() = NULL; +void (*remoteCleanUpFnc)() = NULL; + +#if (defined WIN32 && !defined SDL) +void remoteSetSockets(SOCKET l, SOCKET r) +{ + remoteSocket = r; + remoteListenSocket = l; +} + +#endif + +int remoteTcpSend(char *data, int len) +{ + return send(remoteSocket, data, len, 0); +} + +int remoteTcpRecv(char *data, int len) +{ + return recv(remoteSocket, data, len, 0); +} + +bool remoteTcpInit() +{ + if (remoteSocket == -1) + { +#ifdef WIN32 + WSADATA wsaData; + int error = WSAStartup(MAKEWORD(1, 1), &wsaData); +#endif // WIN32 + int s = socket(PF_INET, SOCK_STREAM, 0); + + remoteListenSocket = s; + + if (s < 0) + { + fprintf(stderr, "Error opening socket\n"); + exit(-1); + } + int tmp = 1; + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof(tmp)); + + // char hostname[256]; + // gethostname(hostname, 256); + + // hostent *ent = gethostbyname(hostname); + // unsigned long a = *((unsigned long *)ent->h_addr); + + sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(remotePort); + addr.sin_addr.s_addr = htonl(0); + int count = 0; + while (count < 3) + { + if (bind(s, (sockaddr *)&addr, sizeof(addr))) + { + addr.sin_port = htons(ntohs(addr.sin_port)+1); + } + else + break; + } + if (count == 3) + { + fprintf(stderr, "Error binding \n"); + exit(-1); + } + + fprintf(stderr, "Listening for a connection at port %d\n", + ntohs(addr.sin_port)); + + if (listen(s, 1)) + { + fprintf(stderr, "Error listening\n"); + exit(-1); + } + socklen_t len = sizeof(addr); + +#ifdef WIN32 + int flag = 0; + ioctlsocket(s, FIONBIO, (unsigned long *)&flag); +#endif // WIN32 + int s2 = accept(s, (sockaddr *)&addr, &len); + if (s2 > 0) + { + fprintf(stderr, "Got a connection from %s %d\n", + inet_ntoa((in_addr)addr.sin_addr), + ntohs(addr.sin_port)); + } + else + { +#ifdef WIN32 + int error = WSAGetLastError(); +#endif // WIN32 + } + char dummy; + recv(s2, &dummy, 1, 0); + if (dummy != '+') + { + fprintf(stderr, "ACK not received\n"); + exit(-1); + } + remoteSocket = s2; + // close(s); + } + return true; +} + +void remoteTcpCleanUp() +{ + if (remoteSocket > 0) + { + fprintf(stderr, "Closing remote socket\n"); + close(remoteSocket); + remoteSocket = -1; + } + if (remoteListenSocket > 0) + { + fprintf(stderr, "Closing listen socket\n"); + close(remoteListenSocket); + remoteListenSocket = -1; + } +} + +int remotePipeSend(char *data, int len) +{ + int res = write(1, data, len); + return res; +} + +int remotePipeRecv(char *data, int len) +{ + int res = read(0, data, len); + return res; +} + +bool remotePipeInit() +{ + char dummy; + read(0, &dummy, 1); + if (dummy != '+') + { + fprintf(stderr, "ACK not received\n"); + exit(-1); + } + + return true; +} + +void remotePipeCleanUp() +{} + +void remoteSetPort(int port) +{ + remotePort = port; +} + +void remoteSetProtocol(int p) +{ + if (p == 0) + { + remoteSendFnc = remoteTcpSend; + remoteRecvFnc = remoteTcpRecv; + remoteInitFnc = remoteTcpInit; + remoteCleanUpFnc = remoteTcpCleanUp; + } + else + { + remoteSendFnc = remotePipeSend; + remoteRecvFnc = remotePipeRecv; + remoteInitFnc = remotePipeInit; + remoteCleanUpFnc = remotePipeCleanUp; + } +} + +void remoteInit() +{ + if (remoteInitFnc) + remoteInitFnc(); +} + +void remotePutPacket(char *packet) +{ + char *hex = "0123456789abcdef"; + char buffer[1024]; + + int count = strlen(packet); + + unsigned char csum = 0; + + char *p = buffer; + *p++ = '$'; + + for (int i = 0; i < count; i++) + { + csum += packet[i]; + *p++ = packet[i]; + } + *p++ = '#'; + *p++ = hex[csum>>4]; + *p++ = hex[csum & 15]; + *p++ = 0; + // printf("Sending %s\n", buffer); + remoteSendFnc(buffer, count + 4); + + char c = 0; + remoteRecvFnc(&c, 1); + /* + if(c == '+') + printf("ACK\n"); + else if(c=='-') + printf("NACK\n"); + */ +} + +void remoteOutput(char *s, u32 addr) +{ + char buffer[16384]; + + char *d = buffer; + *d++ = 'O'; + + if (s) + { + char c = *s++; + while (c) + { + sprintf(d, "%02x", c); + d += 2; + c = *s++; + } + } + else + { + char c = debuggerReadByte(addr); + addr++; + while (c) + { + sprintf(d, "%02x", c); + d += 2; + c = debuggerReadByte(addr); + addr++; + } + } + remotePutPacket(buffer); + // fprintf(stderr, "Output sent %s\n", buffer); +} + +void remoteSendSignal() +{ + char buffer[1024]; + sprintf(buffer, "S%02x", remoteSignal); + remotePutPacket(buffer); +} + +void remoteSendStatus() +{ + char buffer[1024]; + sprintf(buffer, "T%02x", remoteSignal); + char *s = buffer; + s += 3; + for (int i = 0; i < 15; i++) + { + u32 v = reg[i].I; + sprintf(s, "%02x:%02x%02x%02x%02x;", i, + (v & 255), + (v >> 8) & 255, + (v >> 16) & 255, + (v >> 24) & 255); + s += 12; + } + u32 v = armNextPC; + sprintf(s, "0f:%02x%02x%02x%02x;", (v & 255), + (v >> 8) & 255, + (v >> 16) & 255, + (v >> 24) & 255); + s += 12; + CPUUpdateCPSR(); + v = reg[16].I; + sprintf(s, "19:%02x%02x%02x%02x;", (v & 255), + (v >> 8) & 255, + (v >> 16) & 255, + (v >> 24) & 255); + s += 12; + *s = 0; + // printf("Sending %s\n", buffer); + remotePutPacket(buffer); +} + +void remoteBinaryWrite(char *p) +{ + u32 address; + int count; + sscanf(p, "%x,%x:", &address, &count); + // printf("Binary write for %08x %d\n", address, count); + + p = strchr(p, ':'); + p++; + for (int i = 0; i < count; i++) + { + u8 b = *p++; + switch (b) + { + case 0x7d: + b = *p++; + debuggerWriteByte(address, (b^0x20)); + address++; + break; + default: + debuggerWriteByte(address, b); + address++; + break; + } + } + // printf("ROM is %08x\n", debuggerReadMemory(0x8000254)); + remotePutPacket("OK"); +} + +void remoteMemoryWrite(char *p) +{ + u32 address; + int count; + sscanf(p, "%x,%x:", &address, &count); + // printf("Memory write for %08x %d\n", address, count); + + p = strchr(p, ':'); + p++; + for (int i = 0; i < count; i++) + { + u8 v = 0; + char c = *p++; + if (c <= '9') + v = (c - '0') << 4; + else + v = (c + 10 - 'a') << 4; + c = *p++; + if (c <= '9') + v += (c - '0'); + else + v += (c + 10 - 'a'); + debuggerWriteByte(address, v); + address++; + } + // printf("ROM is %08x\n", debuggerReadMemory(0x8000254)); + remotePutPacket("OK"); +} + +void remoteMemoryRead(char *p) +{ + u32 address; + int count; + sscanf(p, "%x,%x:", &address, &count); + // printf("Memory read for %08x %d\n", address, count); + + char buffer[1024]; + + char *s = buffer; + for (int i = 0; i < count; i++) + { + u8 b = debuggerReadByte(address); + sprintf(s, "%02x", b); + address++; + s += 2; + } + *s = 0; + remotePutPacket(buffer); +} + +void remoteStepOverRange(char *p) +{ + u32 address; + u32 final; + sscanf(p, "%x,%x", &address, &final); + + remotePutPacket("OK"); + + remoteResumed = true; + do + { + CPULoop(1); + if (debugger) + break; + } + while (armNextPC >= address && armNextPC < final); + + remoteResumed = false; + + remoteSendStatus(); +} + +void remoteWriteWatch(char *p, bool active) +{ + u32 address; + int count; + sscanf(p, ",%x,%x#", &address, &count); + + fprintf(stderr, "Write watch for %08x %d\n", address, count); + + if (address < 0x2000000 || address > 0x3007fff) + { + remotePutPacket("E01"); + return; + } + + if (address > 0x203ffff && address < 0x3000000) + { + remotePutPacket("E01"); + return; + } + + u32 final = address + count; + + if (address < 0x2040000 && final > 0x2040000) + { + remotePutPacket("E01"); + return; + } + else if (address < 0x3008000 && final > 0x3008000) + { + remotePutPacket("E01"); + return; + } + + for (int i = 0; i < count; i++) + { + if ((address >> 24) == 2) + freezeWorkRAM[address & 0x3ffff] = active; + else + freezeInternalRAM[address & 0x7fff] = active; + address++; + } + + remotePutPacket("OK"); +} + +void remoteReadRegisters(char *p) +{ + char buffer[1024]; + + char *s = buffer; + int i; + // regular registers + for (i = 0; i < 15; i++) + { + u32 v = reg[i].I; + sprintf(s, "%02x%02x%02x%02x", v & 255, (v >> 8) & 255, + (v >> 16) & 255, (v >> 24) & 255); + s += 8; + } + // PC + u32 pc = armNextPC; + sprintf(s, "%02x%02x%02x%02x", pc & 255, (pc >> 8) & 255, + (pc >> 16) & 255, (pc >> 24) & 255); + s += 8; + + // floating point registers (24-bit) + for (i = 0; i < 8; i++) + { + sprintf(s, "000000000000000000000000"); + s += 24; + } + + // FP status register + sprintf(s, "00000000"); + s += 8; + // CPSR + CPUUpdateCPSR(); + u32 v = reg[16].I; + sprintf(s, "%02x%02x%02x%02x", v & 255, (v >> 8) & 255, + (v >> 16) & 255, (v >> 24) & 255); + s += 8; + *s = 0; + remotePutPacket(buffer); +} + +void remoteWriteRegister(char *p) +{ + int r; + + sscanf(p, "%x=", &r); + + p = strchr(p, '='); + p++; + + char c = *p++; + + u32 v = 0; + + u8 data[4] = {0, 0, 0, 0}; + + int i = 0; + + while (c != '#') + { + u8 b = 0; + if (c <= '9') + b = (c - '0') << 4; + else + b = (c + 10 - 'a') << 4; + c = *p++; + if (c <= '9') + b += (c - '0'); + else + b += (c + 10 - 'a'); + data[i++] = b; + c = *p++; + } + + v = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); + + // printf("Write register %d=%08x\n", r, v); + reg[r].I = v; + if (r == 15) + { + armNextPC = v; + if (armState) + reg[15].I = v + 4; + else + reg[15].I = v + 2; + } + remotePutPacket("OK"); +} + +void remoteStubMain() +{ + if (!debugger) + return; + + if (remoteResumed) + { + remoteSendStatus(); + remoteResumed = false; + } + + while (true) + { + char buffer[1024]; + int res = remoteRecvFnc(buffer, 1024); + + if (res == -1) + { + fprintf(stderr, "GDB connection lost\n"); +#ifdef SDL + dbgMain = debuggerMain; + dbgSignal = debuggerSignal; +#endif + debugger = false; + break; + } + + // fprintf(stderr, "Received %s\n", buffer); + char *p = buffer; + char c = *p++; + char pp = '+'; + remoteSendFnc(&pp, 1); + + if (c != '$') + continue; + c = *p++; + switch (c) + { + case '?': + remoteSendSignal(); + break; + case 'D': + remotePutPacket("OK"); +#ifdef SDL + dbgMain = debuggerMain; + dbgSignal = debuggerSignal; +#endif + remoteResumed = true; + debugger = false; + return; + case 'e': + remoteStepOverRange(p); + break; + case 'k': + remotePutPacket("OK"); +#ifdef SDL + dbgMain = debuggerMain; + dbgSignal = debuggerSignal; +#endif + debugger = false; + emulating = false; + return; + case 'C': + remoteResumed = true; + debugger = false; + return; + case 'c': + remoteResumed = true; + debugger = false; + return; + case 's': + remoteResumed = true; + remoteSignal = 5; + CPULoop(1); + if (remoteResumed) + { + remoteResumed = false; + remoteSendStatus(); + } + break; + case 'g': + remoteReadRegisters(p); + break; + case 'P': + remoteWriteRegister(p); + break; + case 'M': + remoteMemoryWrite(p); + break; + case 'm': + remoteMemoryRead(p); + break; + case 'X': + remoteBinaryWrite(p); + break; + case 'H': + remotePutPacket("OK"); + break; + case 'q': + remotePutPacket(""); + break; + case 'Z': + if (*p++ == '2') + { + remoteWriteWatch(p, true); + } + else + remotePutPacket(""); + break; + case 'z': + if (*p++ == '2') + { + remoteWriteWatch(p, false); + } + else + remotePutPacket(""); + break; + default: + { + *(strchr(p, '#') + 3) = 0; + fprintf(stderr, "Unknown packet %s\n", --p); + remotePutPacket(""); + break; + } + } + } +} + +void remoteStubSignal(int sig, int number) +{ + remoteSignal = sig; + remoteResumed = false; + remoteSendStatus(); + debugger = true; +} + +void remoteCleanUp() +{ + if (remoteCleanUpFnc) + remoteCleanUpFnc(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gba/thumb.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gba/thumb.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,2524 @@ +#ifdef C_CORE +#define NEG(i) ((i) >> 31) +#define POS(i) ((~(i)) >> 31) +#define ADDCARRY(a, b, c) \ + C_FLAG = ((NEG(a) & NEG(b)) | \ + (NEG(a) & POS(c)) | \ + (NEG(b) & POS(c))) ? true : false; +#define ADDOVERFLOW(a, b, c) \ + V_FLAG = ((NEG(a) & NEG(b) & POS(c)) | \ + (POS(a) & POS(b) & NEG(c))) ? true : false; +#define SUBCARRY(a, b, c) \ + C_FLAG = ((NEG(a) & POS(b)) | \ + (NEG(a) & POS(c)) | \ + (POS(b) & POS(c))) ? true : false; +#define SUBOVERFLOW(a, b, c) \ + V_FLAG = ((NEG(a) & POS(b) & POS(c)) | \ + (POS(a) & NEG(b) & NEG(c))) ? true : false; +#define ADD_RD_RS_RN \ + { \ + u32 lhs = reg[source].I; \ + u32 rhs = value; \ + u32 res = lhs + rhs; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + ADDCARRY(lhs, rhs, res); \ + ADDOVERFLOW(lhs, rhs, res); \ + } +#define ADD_RD_RS_O3 \ + { \ + u32 lhs = reg[source].I; \ + u32 rhs = value; \ + u32 res = lhs + rhs; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + ADDCARRY(lhs, rhs, res); \ + ADDOVERFLOW(lhs, rhs, res); \ + } +#define ADD_RN_O8(d) \ + { \ + u32 lhs = reg[(d)].I; \ + u32 rhs = (opcode & 255); \ + u32 res = lhs + rhs; \ + reg[(d)].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + ADDCARRY(lhs, rhs, res); \ + ADDOVERFLOW(lhs, rhs, res); \ + } +#define CMN_RD_RS \ + { \ + u32 lhs = reg[dest].I; \ + u32 rhs = value; \ + u32 res = lhs + rhs; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + ADDCARRY(lhs, rhs, res); \ + ADDOVERFLOW(lhs, rhs, res); \ + } +#define ADC_RD_RS \ + { \ + u32 lhs = reg[dest].I; \ + u32 rhs = value; \ + u32 res = lhs + rhs + (u32)C_FLAG; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + ADDCARRY(lhs, rhs, res); \ + ADDOVERFLOW(lhs, rhs, res); \ + } +#define SUB_RD_RS_RN \ + { \ + u32 lhs = reg[source].I; \ + u32 rhs = value; \ + u32 res = lhs - rhs; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#define SUB_RD_RS_O3 \ + { \ + u32 lhs = reg[source].I; \ + u32 rhs = value; \ + u32 res = lhs - rhs; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#define SUB_RN_O8(d) \ + { \ + u32 lhs = reg[(d)].I; \ + u32 rhs = (opcode & 255); \ + u32 res = lhs - rhs; \ + reg[(d)].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#define CMP_RN_O8(d) \ + { \ + u32 lhs = reg[(d)].I; \ + u32 rhs = (opcode & 255); \ + u32 res = lhs - rhs; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#define SBC_RD_RS \ + { \ + u32 lhs = reg[dest].I; \ + u32 rhs = value; \ + u32 res = lhs - rhs - !((u32)C_FLAG); \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#define LSL_RD_RM_I5 \ + { \ + C_FLAG = (reg[source].I >> (32 - shift)) & 1 ? true : false; \ + value = reg[source].I << shift; \ + } +#define LSL_RD_RS \ + { \ + C_FLAG = (reg[dest].I >> (32 - value)) & 1 ? true : false; \ + value = reg[dest].I << value; \ + } +#define LSR_RD_RM_I5 \ + { \ + C_FLAG = (reg[source].I >> (shift - 1)) & 1 ? true : false; \ + value = reg[source].I >> shift; \ + } +#define LSR_RD_RS \ + { \ + C_FLAG = (reg[dest].I >> (value - 1)) & 1 ? true : false; \ + value = reg[dest].I >> value; \ + } +#define ASR_RD_RM_I5 \ + { \ + C_FLAG = ((s32)reg[source].I >> (int)(shift - 1)) & 1 ? true : false; \ + value = (s32)reg[source].I >> (int)shift; \ + } +#define ASR_RD_RS \ + { \ + C_FLAG = ((s32)reg[dest].I >> (int)(value - 1)) & 1 ? true : false; \ + value = (s32)reg[dest].I >> (int)value; \ + } +#define ROR_RD_RS \ + { \ + C_FLAG = (reg[dest].I >> (value - 1)) & 1 ? true : false; \ + value = ((reg[dest].I << (32 - value)) | \ + (reg[dest].I >> value)); \ + } +#define NEG_RD_RS \ + { \ + u32 lhs = reg[source].I; \ + u32 rhs = 0; \ + u32 res = rhs - lhs; \ + reg[dest].I = res; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(rhs, lhs, res); \ + SUBOVERFLOW(rhs, lhs, res); \ + } +#define CMP_RD_RS \ + { \ + u32 lhs = reg[dest].I; \ + u32 rhs = value; \ + u32 res = lhs - rhs; \ + Z_FLAG = (res == 0) ? true : false; \ + N_FLAG = NEG(res) ? true : false; \ + SUBCARRY(lhs, rhs, res); \ + SUBOVERFLOW(lhs, rhs, res); \ + } +#else +#ifdef __GNUC__ +#ifdef __POWERPC__ + #define ADD_RD_RS_RN \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[source].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define ADD_RD_RS_O3 ADD_RD_RS_RN + #define ADD_RN_O8(d) \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[(d)].I), \ + "r" (opcode & 255) \ + ); \ + reg[(d)].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define CMN_RD_RS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("addco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[dest].I), \ + "r" (value) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define ADC_RD_RS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("mtspr xer, %4\n" \ + "addeo. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[dest].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define SUB_RD_RS_RN \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[source].I), \ + "r" (value) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define SUB_RD_RS_O3 SUB_RD_RS_RN + #define SUB_RN_O8(d) \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[(d)].I), \ + "r" (opcode & 255) \ + ); \ + reg[(d)].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define CMP_RN_O8(d) \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[(d)].I), \ + "r" (opcode & 255) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define SBC_RD_RS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("mtspr xer, %4\n" \ + "subfeo. %0, %3, %2\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[dest].I), \ + "r" (value), \ + "r" (C_FLAG << 29) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define LSL_RD_RM_I5 \ + { \ + C_FLAG = (reg[source].I >> (32 - shift)) & 1 ? true : false; \ + value = reg[source].I << shift; \ + } + #define LSL_RD_RS \ + { \ + C_FLAG = (reg[dest].I >> (32 - value)) & 1 ? true : false; \ + value = reg[dest].I << value; \ + } + #define LSR_RD_RM_I5 \ + { \ + C_FLAG = (reg[source].I >> (shift - 1)) & 1 ? true : false; \ + value = reg[source].I >> shift; \ + } + #define LSR_RD_RS \ + { \ + C_FLAG = (reg[dest].I >> (value - 1)) & 1 ? true : false; \ + value = reg[dest].I >> value; \ + } + #define ASR_RD_RM_I5 \ + { \ + C_FLAG = ((s32)reg[source].I >> (int)(shift - 1)) & 1 ? true : false; \ + value = (s32)reg[source].I >> (int)shift; \ + } + #define ASR_RD_RS \ + { \ + C_FLAG = ((s32)reg[dest].I >> (int)(value - 1)) & 1 ? true : false; \ + value = (s32)reg[dest].I >> (int)value; \ + } + #define ROR_RD_RS \ + { \ + C_FLAG = (reg[dest].I >> (value - 1)) & 1 ? true : false; \ + value = ((reg[dest].I << (32 - value)) | \ + (reg[dest].I >> value)); \ + } + #define NEG_RD_RS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("subfco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[source].I), \ + "r" (0) \ + ); \ + reg[dest].I = Result; \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } + #define CMP_RD_RS \ + { \ + register int Flags; \ + register int Result; \ + asm volatile ("subco. %0, %2, %3\n" \ + "mcrxr cr1\n" \ + "mfcr %1\n" \ + : "=r" (Result), \ + "=r" (Flags) \ + : "r" (reg[dest].I), \ + "r" (value) \ + ); \ + Z_FLAG = (Flags >> 29) & 1; \ + N_FLAG = (Flags >> 31) & 1; \ + C_FLAG = (Flags >> 25) & 1; \ + V_FLAG = (Flags >> 26) & 1; \ + } +#else +#define ADD_RD_RS_RN \ + asm ("add %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setcb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[source].I)); +#define ADD_RD_RS_O3 \ + asm ("add %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setcb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[source].I)); +#define ADD_RN_O8(d) \ + asm ("add %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setcb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[(d)].I) \ + : "r" (opcode & 255), "b" (reg[(d)].I)); +#define CMN_RD_RS \ + asm ("add %0, %1;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setcb C_FLAG;" \ + "setob V_FLAG;" \ + : \ + : "r" (value), "r" (reg[dest].I) : "1"); +#define ADC_RD_RS \ + asm ("bt $0, C_FLAG;" \ + "adc %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setcb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[dest].I)); +#define SUB_RD_RS_RN \ + asm ("sub %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[source].I)); +#define SUB_RD_RS_O3 \ + asm ("sub %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[source].I)); +#define SUB_RN_O8(d) \ + asm ("sub %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[(d)].I) \ + : "r" (opcode & 255), "b" (reg[(d)].I)); +#define CMP_RN_O8(d) \ + asm ("sub %0, %1;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : \ + : "r" (opcode & 255), "r" (reg[(d)].I) : "1"); +#define SBC_RD_RS \ + asm volatile ("bt $0, C_FLAG;" \ + "cmc;" \ + "sbb %1, %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "r" (value), "b" (reg[dest].I) : "cc", "memory"); +#define LSL_RD_RM_I5 \ + asm ("shl %%cl, %%eax;" \ + "setcb C_FLAG;" \ + : "=a" (value) \ + : "a" (reg[source].I), "c" (shift)); +#define LSL_RD_RS \ + asm ("shl %%cl, %%eax;" \ + "setcb C_FLAG;" \ + : "=a" (value) \ + : "a" (reg[dest].I), "c" (value)); +#define LSR_RD_RM_I5 \ + asm ("shr %%cl, %%eax;" \ + "setcb C_FLAG;" \ + : "=a" (value) \ + : "a" (reg[source].I), "c" (shift)); +#define LSR_RD_RS \ + asm ("shr %%cl, %%eax;" \ + "setcb C_FLAG;" \ + : "=a" (value) \ + : "a" (reg[dest].I), "c" (value)); +#define ASR_RD_RM_I5 \ + asm ("sar %%cl, %%eax;" \ + "setcb C_FLAG;" \ + : "=a" (value) \ + : "a" (reg[source].I), "c" (shift)); +#define ASR_RD_RS \ + asm ("sar %%cl, %%eax;" \ + "setcb C_FLAG;" \ + : "=a" (value) \ + : "a" (reg[dest].I), "c" (value)); +#define ROR_RD_RS \ + asm ("ror %%cl, %%eax;" \ + "setcb C_FLAG;" \ + : "=a" (value) \ + : "a" (reg[dest].I), "c" (value)); +#define NEG_RD_RS \ + asm ("neg %%ebx;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : "=b" (reg[dest].I) \ + : "b" (reg[source].I)); +#define CMP_RD_RS \ + asm ("sub %0, %1;" \ + "setsb N_FLAG;" \ + "setzb Z_FLAG;" \ + "setncb C_FLAG;" \ + "setob V_FLAG;" \ + : \ + : "r" (value), "r" (reg[dest].I) : "1"); +#endif +#else +#define ADD_RD_RS_RN \ + { \ + __asm mov eax, source \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * eax] \ + __asm add ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define ADD_RD_RS_O3 \ + { \ + __asm mov eax, source \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * eax] \ + __asm add ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define ADD_RN_O8(d) \ + { \ + __asm mov ebx, opcode \ + __asm and ebx, 255 \ + __asm add dword ptr [OFFSET reg + 4 * (d)], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define CMN_RD_RS \ + { \ + __asm mov eax, dest \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * eax] \ + __asm add ebx, value \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define ADC_RD_RS \ + { \ + __asm mov ebx, dest \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm bt word ptr C_FLAG, 0 \ + __asm adc ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define SUB_RD_RS_RN \ + { \ + __asm mov eax, source \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * eax] \ + __asm sub ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define SUB_RD_RS_O3 \ + { \ + __asm mov eax, source \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * eax] \ + __asm sub ebx, value \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define SUB_RN_O8(d) \ + { \ + __asm mov ebx, opcode \ + __asm and ebx, 255 \ + __asm sub dword ptr [OFFSET reg + 4 * (d)], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define CMP_RN_O8(d) \ + { \ + __asm mov eax, dword ptr [OFFSET reg + 4 * (d)] \ + __asm mov ebx, opcode \ + __asm and ebx, 255 \ + __asm sub eax, ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define SBC_RD_RS \ + { \ + __asm mov ebx, dest \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm mov eax, value \ + __asm bt word ptr C_FLAG, 0 \ + __asm cmc \ + __asm sbb ebx, eax \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define LSL_RD_RM_I5 \ + { \ + __asm mov eax, source \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm shl eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_FLAG \ + } +#define LSL_RD_RS \ + { \ + __asm mov eax, dest \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr value \ + __asm shl eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_FLAG \ + } +#define LSR_RD_RM_I5 \ + { \ + __asm mov eax, source \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm shr eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_FLAG \ + } +#define LSR_RD_RS \ + { \ + __asm mov eax, dest \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr value \ + __asm shr eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_FLAG \ + } +#define ASR_RD_RM_I5 \ + { \ + __asm mov eax, source \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr shift \ + __asm sar eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_FLAG \ + } +#define ASR_RD_RS \ + { \ + __asm mov eax, dest \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr value \ + __asm sar eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_FLAG \ + } +#define ROR_RD_RS \ + { \ + __asm mov eax, dest \ + __asm mov eax, dword ptr [OFFSET reg + 4 * eax] \ + __asm mov cl, byte ptr value \ + __asm ror eax, cl \ + __asm mov value, eax \ + __asm setc byte ptr C_FLAG \ + } +#define NEG_RD_RS \ + { \ + __asm mov ebx, source \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * ebx] \ + __asm neg ebx \ + __asm mov eax, dest \ + __asm mov dword ptr [OFFSET reg + 4 * eax], ebx \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#define CMP_RD_RS \ + { \ + __asm mov eax, dest \ + __asm mov ebx, dword ptr [OFFSET reg + 4 * eax] \ + __asm sub ebx, value \ + __asm sets byte ptr N_FLAG \ + __asm setz byte ptr Z_FLAG \ + __asm setnc byte ptr C_FLAG \ + __asm seto byte ptr V_FLAG \ + } +#endif +#endif + +u32 opcode = CPUReadHalfWordQuick(armNextPC); +clockTicks = thumbCycles[opcode >> 8] + memoryWaitFetch[(armNextPC >> 24) & 15]; +#ifndef FINAL_VERSION +if (armNextPC == stop) +{ + armNextPC = armNextPC++; +} +#endif + +armNextPC = reg[15].I; +reg[15].I += 2; + +switch (opcode >> 8) +{ +case 0x00: +case 0x01: +case 0x02: +case 0x03: +case 0x04: +case 0x05: +case 0x06: +case 0x07: +{ + // LSL Rd, Rm, #Imm 5 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + int shift = (opcode >> 6) & 0x1f; + u32 value; + + if (shift) + { + LSL_RD_RM_I5; + } + else + { + value = reg[source].I; + } + reg[dest].I = value; + // C_FLAG set above + N_FLAG = (value & 0x80000000 ? true : false); + Z_FLAG = (value ? false : true); +} +break; +case 0x08: +case 0x09: +case 0x0a: +case 0x0b: +case 0x0c: +case 0x0d: +case 0x0e: +case 0x0f: +{ + // LSR Rd, Rm, #Imm 5 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + int shift = (opcode >> 6) & 0x1f; + u32 value; + + if (shift) + { + LSR_RD_RM_I5; + } + else + { + C_FLAG = reg[source].I & 0x80000000 ? true : false; + value = 0; + } + reg[dest].I = value; + // C_FLAG set above + N_FLAG = (value & 0x80000000 ? true : false); + Z_FLAG = (value ? false : true); +} +break; +case 0x10: +case 0x11: +case 0x12: +case 0x13: +case 0x14: +case 0x15: +case 0x16: +case 0x17: +{ + // ASR Rd, Rm, #Imm 5 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + int shift = (opcode >> 6) & 0x1f; + u32 value; + + if (shift) + { + ASR_RD_RM_I5; + } + else + { + if (reg[source].I & 0x80000000) + { + value = 0xFFFFFFFF; + C_FLAG = true; + } + else + { + value = 0; + C_FLAG = false; + } + } + reg[dest].I = value; + // C_FLAG set above + N_FLAG = (value & 0x80000000 ? true : false); + Z_FLAG = (value ? false : true); +} +break; +case 0x18: +case 0x19: +{ + // ADD Rd, Rs, Rn + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + u32 value = reg[(opcode >> 6) & 0x07].I; + ADD_RD_RS_RN; +} +break; +case 0x1a: +case 0x1b: +{ + // SUB Rd, Rs, Rn + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + u32 value = reg[(opcode >> 6) & 0x07].I; + SUB_RD_RS_RN; +} +break; +case 0x1c: +case 0x1d: +{ + // ADD Rd, Rs, #Offset3 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + u32 value = (opcode >> 6) & 7; + ADD_RD_RS_O3; +} +break; +case 0x1e: +case 0x1f: +{ + // SUB Rd, Rs, #Offset3 + int dest = opcode & 0x07; + int source = (opcode >> 3) & 0x07; + u32 value = (opcode >> 6) & 7; + SUB_RD_RS_O3; +} +break; +case 0x20: + // MOV R0, #Offset8 + reg[0].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[0].I ? false : true); + break; +case 0x21: + // MOV R1, #Offset8 + reg[1].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[1].I ? false : true); + break; +case 0x22: + // MOV R2, #Offset8 + reg[2].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[2].I ? false : true); + break; +case 0x23: + // MOV R3, #Offset8 + reg[3].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[3].I ? false : true); + break; +case 0x24: + // MOV R4, #Offset8 + reg[4].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[4].I ? false : true); + break; +case 0x25: + // MOV R5, #Offset8 + reg[5].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[5].I ? false : true); + break; +case 0x26: + // MOV R6, #Offset8 + reg[6].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[6].I ? false : true); + break; +case 0x27: + // MOV R7, #Offset8 + reg[7].I = opcode & 255; + N_FLAG = false; + Z_FLAG = (reg[7].I ? false : true); + break; +case 0x28: + // CMP R0, #Offset8 + CMP_RN_O8(0); + break; +case 0x29: + // CMP R1, #Offset8 + CMP_RN_O8(1); + break; +case 0x2a: + // CMP R2, #Offset8 + CMP_RN_O8(2); + break; +case 0x2b: + // CMP R3, #Offset8 + CMP_RN_O8(3); + break; +case 0x2c: + // CMP R4, #Offset8 + CMP_RN_O8(4); + break; +case 0x2d: + // CMP R5, #Offset8 + CMP_RN_O8(5); + break; +case 0x2e: + // CMP R6, #Offset8 + CMP_RN_O8(6); + break; +case 0x2f: + // CMP R7, #Offset8 + CMP_RN_O8(7); + break; +case 0x30: + // ADD R0,#Offset8 + ADD_RN_O8(0); + break; +case 0x31: + // ADD R1,#Offset8 + ADD_RN_O8(1); + break; +case 0x32: + // ADD R2,#Offset8 + ADD_RN_O8(2); + break; +case 0x33: + // ADD R3,#Offset8 + ADD_RN_O8(3); + break; +case 0x34: + // ADD R4,#Offset8 + ADD_RN_O8(4); + break; +case 0x35: + // ADD R5,#Offset8 + ADD_RN_O8(5); + break; +case 0x36: + // ADD R6,#Offset8 + ADD_RN_O8(6); + break; +case 0x37: + // ADD R7,#Offset8 + ADD_RN_O8(7); + break; +case 0x38: + // SUB R0,#Offset8 + SUB_RN_O8(0); + break; +case 0x39: + // SUB R1,#Offset8 + SUB_RN_O8(1); + break; +case 0x3a: + // SUB R2,#Offset8 + SUB_RN_O8(2); + break; +case 0x3b: + // SUB R3,#Offset8 + SUB_RN_O8(3); + break; +case 0x3c: + // SUB R4,#Offset8 + SUB_RN_O8(4); + break; +case 0x3d: + // SUB R5,#Offset8 + SUB_RN_O8(5); + break; +case 0x3e: + // SUB R6,#Offset8 + SUB_RN_O8(6); + break; +case 0x3f: + // SUB R7,#Offset8 + SUB_RN_O8(7); + break; +case 0x40: + switch ((opcode >> 6) & 3) + { + case 0x00: + { + // AND Rd, Rs + int dest = opcode & 7; + reg[dest].I &= reg[(opcode >> 3) & 7].I; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; +#ifdef BKPT_SUPPORT +#define THUMB_CONSOLE_OUTPUT(a, b) \ + if ((opcode == 0x4000) && (reg[0].I == 0xC0DED00D)) { \ + extern void (*dbgOutput)(char *, u32); \ + dbgOutput((a), (b)); \ + } +#else +#define THUMB_CONSOLE_OUTPUT(a, b) +#endif + THUMB_CONSOLE_OUTPUT(NULL, reg[2].I); + } + break; + case 0x01: + // EOR Rd, Rs + { + int dest = opcode & 7; + reg[dest].I ^= reg[(opcode >> 3) & 7].I; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + } + break; + case 0x02: + // LSL Rd, Rs + { + int dest = opcode & 7; + u32 value = reg[(opcode >> 3) & 7].B.B0; + if (value) + { + if (value == 32) + { + value = 0; + C_FLAG = (reg[dest].I & 1 ? true : false); + } + else if (value < 32) + { + LSL_RD_RS; + } + else + { + value = 0; + C_FLAG = false; + } + reg[dest].I = value; + } + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + clockTicks++; + } + break; + case 0x03: + { + // LSR Rd, Rs + int dest = opcode & 7; + u32 value = reg[(opcode >> 3) & 7].B.B0; + if (value) + { + if (value == 32) + { + value = 0; + C_FLAG = (reg[dest].I & 0x80000000 ? true : false); + } + else if (value < 32) + { + LSR_RD_RS; + } + else + { + value = 0; + C_FLAG = false; + } + reg[dest].I = value; + } + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + clockTicks++; + } + break; + } + break; +case 0x41: + switch ((opcode >> 6) & 3) + { + case 0x00: + { + // ASR Rd, Rs + int dest = opcode & 7; + u32 value = reg[(opcode >> 3) & 7].B.B0; + // ASR + if (value) + { + if (value < 32) + { + ASR_RD_RS; + reg[dest].I = value; + } + else + { + if (reg[dest].I & 0x80000000) + { + reg[dest].I = 0xFFFFFFFF; + C_FLAG = true; + } + else + { + reg[dest].I = 0x00000000; + C_FLAG = false; + } + } + } + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + clockTicks++; + } + break; + case 0x01: + { + // ADC Rd, Rs + int dest = opcode & 0x07; + u32 value = reg[(opcode >> 3) & 7].I; + // ADC + ADC_RD_RS; + } + break; + case 0x02: + { + // SBC Rd, Rs + int dest = opcode & 0x07; + u32 value = reg[(opcode >> 3) & 7].I; + + // SBC + SBC_RD_RS; + } + break; + case 0x03: + // ROR Rd, Rs + { + int dest = opcode & 7; + u32 value = reg[(opcode >> 3) & 7].B.B0; + + if (value) + { + value = value & 0x1f; + if (value == 0) + { + C_FLAG = (reg[dest].I & 0x80000000 ? true : false); + } + else + { + ROR_RD_RS; + reg[dest].I = value; + } + } + clockTicks++; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + Z_FLAG = reg[dest].I ? false : true; + } + break; + } + break; +case 0x42: + switch ((opcode >> 6) & 3) + { + case 0x00: + { + // TST Rd, Rs + u32 value = reg[opcode & 7].I & reg[(opcode >> 3) & 7].I; + N_FLAG = value & 0x80000000 ? true : false; + Z_FLAG = value ? false : true; + } + break; + case 0x01: + { + // NEG Rd, Rs + int dest = opcode & 7; + int source = (opcode >> 3) & 7; + NEG_RD_RS; + } + break; + case 0x02: + { + // CMP Rd, Rs + int dest = opcode & 7; + u32 value = reg[(opcode >> 3) & 7].I; + CMP_RD_RS; + } + break; + case 0x03: + { + // CMN Rd, Rs + int dest = opcode & 7; + u32 value = reg[(opcode >> 3) & 7].I; + // CMN + CMN_RD_RS; + } + break; + } + break; +case 0x43: + switch ((opcode >> 6) & 3) + { + case 0x00: + { + // ORR Rd, Rs + int dest = opcode & 7; + reg[dest].I |= reg[(opcode >> 3) & 7].I; + Z_FLAG = reg[dest].I ? false : true; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + } + break; + case 0x01: + { + // MUL Rd, Rs + int dest = opcode & 7; + u32 rm = reg[(opcode >> 3) & 7].I; + reg[dest].I = reg[dest].I * rm; + if (((s32)rm) < 0) + rm = ~rm; + if ((rm & 0xFFFFFF00) == 0) + clockTicks += 1; + else if ((rm & 0xFFFF0000) == 0) + clockTicks += 2; + else if ((rm & 0xFF000000) == 0) + clockTicks += 3; + else + clockTicks += 4; + Z_FLAG = reg[dest].I ? false : true; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + } + break; + case 0x02: + { + // BIC Rd, Rs + int dest = opcode & 7; + reg[dest].I &= (~reg[(opcode >> 3) & 7].I); + Z_FLAG = reg[dest].I ? false : true; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + } + break; + case 0x03: + { + // MVN Rd, Rs + int dest = opcode & 7; + reg[dest].I = ~reg[(opcode >> 3) & 7].I; + Z_FLAG = reg[dest].I ? false : true; + N_FLAG = reg[dest].I & 0x80000000 ? true : false; + } + break; + } + break; +case 0x44: +{ + int dest = opcode & 7; + int base = (opcode >> 3) & 7; + switch ((opcode >> 6) & 3) + { + default: + goto unknown_thumb; + case 1: + // ADD Rd, Hs + reg[dest].I += reg[base + 8].I; + break; + case 2: + // ADD Hd, Rs + reg[dest + 8].I += reg[base].I; + if (dest == 7) + { + reg[15].I &= 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks++; + } + break; + case 3: + // ADD Hd, Hs + reg[dest + 8].I += reg[base + 8].I; + if (dest == 7) + { + reg[15].I &= 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks++; + } + break; + } +} +break; +case 0x45: +{ + int dest = opcode & 7; + int base = (opcode >> 3) & 7; + u32 value; + switch ((opcode >> 6) & 3) + { + case 0: + // CMP Rd, Hs + value = reg[base].I; + CMP_RD_RS; + break; + case 1: + // CMP Rd, Hs + value = reg[base + 8].I; + CMP_RD_RS; + break; + case 2: + // CMP Hd, Rs + value = reg[base].I; + dest += 8; + CMP_RD_RS; + break; + case 3: + // CMP Hd, Hs + value = reg[base + 8].I; + dest += 8; + CMP_RD_RS; + break; + } +} +break; +case 0x46: +{ + int dest = opcode & 7; + int base = (opcode >> 3) & 7; + switch ((opcode >> 6) & 3) + { + case 0: + // this form should not be used... + // MOV Rd, Rs + reg[dest].I = reg[base].I; + break; + case 1: + // MOV Rd, Hs + reg[dest].I = reg[base + 8].I; + break; + case 2: + // MOV Hd, Rs + reg[dest + 8].I = reg[base].I; + if (dest == 7) + { + reg[15].I &= 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks++; + } + break; + case 3: + // MOV Hd, Hs + reg[dest + 8].I = reg[base + 8].I; + if (dest == 7) + { + reg[15].I &= 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks++; + } + break; + } +} +break; +case 0x47: +{ + int base = (opcode >> 3) & 7; + switch ((opcode >> 6) & 3) + { + case 0: + // BX Rs + reg[15].I = (reg[base].I) & 0xFFFFFFFE; + if (reg[base].I & 1) + { + armState = false; + armNextPC = reg[15].I; + reg[15].I += 2; + } + else + { + armState = true; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + break; + case 1: + // BX Hs + reg[15].I = (reg[8 + base].I) & 0xFFFFFFFE; + if (reg[8 + base].I & 1) + { + armState = false; + armNextPC = reg[15].I; + reg[15].I += 2; + } + else + { + armState = true; + reg[15].I &= 0xFFFFFFFC; + armNextPC = reg[15].I; + reg[15].I += 4; + } + break; + default: + goto unknown_thumb; + } +} +break; +case 0x48: + // LDR R0,[PC, #Imm] +{ + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[0].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x49: + // LDR R1,[PC, #Imm] +{ + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[1].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x4a: + // LDR R2,[PC, #Imm] +{ + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[2].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x4b: + // LDR R3,[PC, #Imm] +{ + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[3].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x4c: + // LDR R4,[PC, #Imm] +{ + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[4].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x4d: + // LDR R5,[PC, #Imm] +{ + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[5].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x4e: + // LDR R6,[PC, #Imm] +{ + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[6].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x4f: + // LDR R7,[PC, #Imm] +{ + u32 address = (reg[15].I & 0xFFFFFFFC) + ((opcode & 0xFF) << 2); + reg[7].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x50: +case 0x51: + // STR Rd, [Rs, Rn] +{ + u32 + address = reg[(opcode >> 3) & 7].I + reg[(opcode >> 6) & 7].I; + CPUWriteMemory(address, + reg[opcode & 7].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x52: +case 0x53: + // STRH Rd, [Rs, Rn] +{ + u32 address = reg[(opcode >> 3) & 7].I + reg[(opcode >> 6) & 7].I; + CPUWriteHalfWord(address, + reg[opcode & 7].W.W0); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x54: +case 0x55: + // STRB Rd, [Rs, Rn] +{ + u32 address = reg[(opcode >> 3) & 7].I + reg[(opcode >> 6) & 7].I; + CPUWriteByte(address, + reg[opcode & 7].B.B0); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x56: +case 0x57: + // LDSB Rd, [Rs, Rn] +{ + u32 address = reg[(opcode >> 3) & 7].I + reg[(opcode >> 6) & 7].I; + reg[opcode & 7].I = (s8)CPUReadByte(address); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x58: +case 0x59: + // LDR Rd, [Rs, Rn] +{ + u32 address = reg[(opcode >> 3) & 7].I + reg[(opcode >> 6) & 7].I; + reg[opcode & 7].I = CPUReadMemory(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x5a: +case 0x5b: + // LDRH Rd, [Rs, Rn] +{ + u32 address = reg[(opcode >> 3) & 7].I + reg[(opcode >> 6) & 7].I; + reg[opcode & 7].I = CPUReadHalfWord(address); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x5c: +case 0x5d: + // LDRB Rd, [Rs, Rn] +{ + u32 address = reg[(opcode >> 3) & 7].I + reg[(opcode >> 6) & 7].I; + reg[opcode & 7].I = CPUReadByte(address); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x5e: +case 0x5f: + // LDSH Rd, [Rs, Rn] +{ + u32 address = reg[(opcode >> 3) & 7].I + reg[(opcode >> 6) & 7].I; + reg[opcode & 7].I = (s16)CPUReadHalfWordSigned(address); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x60: +case 0x61: +case 0x62: +case 0x63: +case 0x64: +case 0x65: +case 0x66: +case 0x67: + // STR Rd, [Rs, #Imm] +{ + u32 address = reg[(opcode >> 3) & 7].I + (((opcode >> 6) & 31) << 2); + CPUWriteMemory(address, + reg[opcode & 7].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x68: +case 0x69: +case 0x6a: +case 0x6b: +case 0x6c: +case 0x6d: +case 0x6e: +case 0x6f: + // LDR Rd, [Rs, #Imm] +{ + u32 address = reg[(opcode >> 3) & 7].I + (((opcode >> 6) & 31) << 2); + reg[opcode & 7].I = CPUReadMemory(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x70: +case 0x71: +case 0x72: +case 0x73: +case 0x74: +case 0x75: +case 0x76: +case 0x77: + // STRB Rd, [Rs, #Imm] +{ + u32 address = reg[(opcode >> 3) & 7].I + (((opcode >> 6) & 31)); + CPUWriteByte(address, + reg[opcode & 7].B.B0); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x78: +case 0x79: +case 0x7a: +case 0x7b: +case 0x7c: +case 0x7d: +case 0x7e: +case 0x7f: + // LDRB Rd, [Rs, #Imm] +{ + u32 address = reg[(opcode >> 3) & 7].I + (((opcode >> 6) & 31)); + reg[opcode & 7].I = CPUReadByte(address); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x80: +case 0x81: +case 0x82: +case 0x83: +case 0x84: +case 0x85: +case 0x86: +case 0x87: + // STRH Rd, [Rs, #Imm] +{ + u32 address = reg[(opcode >> 3) & 7].I + (((opcode >> 6) & 31) << 1); + CPUWriteHalfWord(address, + reg[opcode & 7].W.W0); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x88: +case 0x89: +case 0x8a: +case 0x8b: +case 0x8c: +case 0x8d: +case 0x8e: +case 0x8f: + // LDRH Rd, [Rs, #Imm] +{ + u32 address = reg[(opcode >> 3) & 7].I + (((opcode >> 6) & 31) << 1); + reg[opcode & 7].I = CPUReadHalfWord(address); + clockTicks += CPUUpdateTicksAccess16(address); +} +break; +case 0x90: + // STR R0, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + CPUWriteMemory(address, reg[0].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x91: + // STR R1, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + CPUWriteMemory(address, reg[1].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x92: + // STR R2, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + CPUWriteMemory(address, reg[2].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x93: + // STR R3, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + CPUWriteMemory(address, reg[3].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x94: + // STR R4, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + CPUWriteMemory(address, reg[4].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x95: + // STR R5, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + CPUWriteMemory(address, reg[5].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x96: + // STR R6, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + CPUWriteMemory(address, reg[6].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x97: + // STR R7, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + CPUWriteMemory(address, reg[7].I); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x98: + // LDR R0, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + reg[0].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x99: + // LDR R1, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + reg[1].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x9a: + // LDR R2, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + reg[2].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x9b: + // LDR R3, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + reg[3].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x9c: + // LDR R4, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + reg[4].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x9d: + // LDR R5, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + reg[5].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x9e: + // LDR R6, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + reg[6].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0x9f: + // LDR R7, [SP, #Imm] +{ + u32 address = reg[13].I + ((opcode & 255) << 2); + reg[7].I = CPUReadMemoryQuick(address); + clockTicks += CPUUpdateTicksAccess32(address); +} +break; +case 0xa0: + // ADD R0, PC, Imm + reg[0].I = (reg[15].I & 0xFFFFFFFC) + ((opcode & 255) << 2); + break; +case 0xa1: + // ADD R1, PC, Imm + reg[1].I = (reg[15].I & 0xFFFFFFFC) + ((opcode & 255) << 2); + break; +case 0xa2: + // ADD R2, PC, Imm + reg[2].I = (reg[15].I & 0xFFFFFFFC) + ((opcode & 255) << 2); + break; +case 0xa3: + // ADD R3, PC, Imm + reg[3].I = (reg[15].I & 0xFFFFFFFC) + ((opcode & 255) << 2); + break; +case 0xa4: + // ADD R4, PC, Imm + reg[4].I = (reg[15].I & 0xFFFFFFFC) + ((opcode & 255) << 2); + break; +case 0xa5: + // ADD R5, PC, Imm + reg[5].I = (reg[15].I & 0xFFFFFFFC) + ((opcode & 255) << 2); + break; +case 0xa6: + // ADD R6, PC, Imm + reg[6].I = (reg[15].I & 0xFFFFFFFC) + ((opcode & 255) << 2); + break; +case 0xa7: + // ADD R7, PC, Imm + reg[7].I = (reg[15].I & 0xFFFFFFFC) + ((opcode & 255) << 2); + break; +case 0xa8: + // ADD R0, SP, Imm + reg[0].I = reg[13].I + ((opcode & 255) << 2); + break; +case 0xa9: + // ADD R1, SP, Imm + reg[1].I = reg[13].I + ((opcode & 255) << 2); + break; +case 0xaa: + // ADD R2, SP, Imm + reg[2].I = reg[13].I + ((opcode & 255) << 2); + break; +case 0xab: + // ADD R3, SP, Imm + reg[3].I = reg[13].I + ((opcode & 255) << 2); + break; +case 0xac: + // ADD R4, SP, Imm + reg[4].I = reg[13].I + ((opcode & 255) << 2); + break; +case 0xad: + // ADD R5, SP, Imm + reg[5].I = reg[13].I + ((opcode & 255) << 2); + break; +case 0xae: + // ADD R6, SP, Imm + reg[6].I = reg[13].I + ((opcode & 255) << 2); + break; +case 0xaf: + // ADD R7, SP, Imm + reg[7].I = reg[13].I + ((opcode & 255) << 2); + break; +case 0xb0: +{ + // ADD SP, Imm + int offset = (opcode & 127) << 2; + if (opcode & 0x80) + offset = -offset; + reg[13].I += offset; +} +break; +#define PUSH_REG(val, r) \ + if (opcode & (val)) { \ + CPUWriteMemory(address, reg[(r)].I); \ + if (offset) \ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); \ + else \ + clockTicks += 1 + CPUUpdateTicksAccess32(address); \ + offset = 1; \ + address += 4; \ + } +case 0xb4: + // PUSH {Rlist} +{ + int offset = 0; + u32 temp = reg[13].I - 4 * cpuBitsSet[opcode & 0xff]; + u32 address = temp & 0xFFFFFFFC; + PUSH_REG(1, 0); + PUSH_REG(2, 1); + PUSH_REG(4, 2); + PUSH_REG(8, 3); + PUSH_REG(16, 4); + PUSH_REG(32, 5); + PUSH_REG(64, 6); + PUSH_REG(128, 7); + reg[13].I = temp; +} +break; +case 0xb5: + // PUSH {Rlist, LR} +{ + int offset = 0; + u32 temp = reg[13].I - 4 - 4 * cpuBitsSet[opcode & 0xff]; + u32 address = temp & 0xFFFFFFFC; + PUSH_REG(1, 0); + PUSH_REG(2, 1); + PUSH_REG(4, 2); + PUSH_REG(8, 3); + PUSH_REG(16, 4); + PUSH_REG(32, 5); + PUSH_REG(64, 6); + PUSH_REG(128, 7); + PUSH_REG(256, 14); + reg[13].I = temp; +} +break; +#define POP_REG(val, r) \ + if (opcode & (val)) { \ + reg[(r)].I = CPUReadMemory(address); \ + if (offset) \ + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); \ + else \ + clockTicks += 2 + CPUUpdateTicksAccess32(address); \ + offset = 1; \ + address += 4; \ + } +case 0xbc: + // POP {Rlist} +{ + int offset = 0; + u32 address = reg[13].I & 0xFFFFFFFC; + u32 temp = reg[13].I + 4 * cpuBitsSet[opcode & 0xFF]; + POP_REG(1, 0); + POP_REG(2, 1); + POP_REG(4, 2); + POP_REG(8, 3); + POP_REG(16, 4); + POP_REG(32, 5); + POP_REG(64, 6); + POP_REG(128, 7); + reg[13].I = temp; +} +break; +case 0xbd: + // POP {Rlist, PC} +{ + int offset = 0; + u32 address = reg[13].I & 0xFFFFFFFC; + u32 temp = reg[13].I + 4 + 4 * cpuBitsSet[opcode & 0xFF]; + POP_REG(1, 0); + POP_REG(2, 1); + POP_REG(4, 2); + POP_REG(8, 3); + POP_REG(16, 4); + POP_REG(32, 5); + POP_REG(64, 6); + POP_REG(128, 7); + reg[15].I = (CPUReadMemory(address) & 0xFFFFFFFE); + if (offset) + clockTicks += CPUUpdateTicksAccessSeq32(address); + else + clockTicks += CPUUpdateTicksAccess32(address); + armNextPC = reg[15].I; + reg[15].I += 2; + reg[13].I = temp; +} +break; +#define THUMB_STM_REG(val, r, b) \ + if (opcode & (val)) { \ + CPUWriteMemory(address, reg[(r)].I); \ + if (!offset) { \ + reg[(b)].I = temp; \ + clockTicks += 1 + CPUUpdateTicksAccess32(address); \ + } else \ + clockTicks += 1 + CPUUpdateTicksAccessSeq32(address); \ + offset = 1; \ + address += 4; \ + } +case 0xc0: +{ + // STM R0!, {Rlist} + u32 address = reg[0].I & 0xFFFFFFFC; + u32 temp = reg[0].I + 4 * cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 0); + THUMB_STM_REG(2, 1, 0); + THUMB_STM_REG(4, 2, 0); + THUMB_STM_REG(8, 3, 0); + THUMB_STM_REG(16, 4, 0); + THUMB_STM_REG(32, 5, 0); + THUMB_STM_REG(64, 6, 0); + THUMB_STM_REG(128, 7, 0); +} +break; +case 0xc1: +{ + // STM R1!, {Rlist} + u32 address = reg[1].I & 0xFFFFFFFC; + u32 temp = reg[1].I + 4 * cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 1); + THUMB_STM_REG(2, 1, 1); + THUMB_STM_REG(4, 2, 1); + THUMB_STM_REG(8, 3, 1); + THUMB_STM_REG(16, 4, 1); + THUMB_STM_REG(32, 5, 1); + THUMB_STM_REG(64, 6, 1); + THUMB_STM_REG(128, 7, 1); +} +break; +case 0xc2: +{ + // STM R2!, {Rlist} + u32 address = reg[2].I & 0xFFFFFFFC; + u32 temp = reg[2].I + 4 * cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 2); + THUMB_STM_REG(2, 1, 2); + THUMB_STM_REG(4, 2, 2); + THUMB_STM_REG(8, 3, 2); + THUMB_STM_REG(16, 4, 2); + THUMB_STM_REG(32, 5, 2); + THUMB_STM_REG(64, 6, 2); + THUMB_STM_REG(128, 7, 2); +} +break; +case 0xc3: +{ + // STM R3!, {Rlist} + u32 address = reg[3].I & 0xFFFFFFFC; + u32 temp = reg[3].I + 4 * cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 3); + THUMB_STM_REG(2, 1, 3); + THUMB_STM_REG(4, 2, 3); + THUMB_STM_REG(8, 3, 3); + THUMB_STM_REG(16, 4, 3); + THUMB_STM_REG(32, 5, 3); + THUMB_STM_REG(64, 6, 3); + THUMB_STM_REG(128, 7, 3); +} +break; +case 0xc4: +{ + // STM R4!, {Rlist} + u32 address = reg[4].I & 0xFFFFFFFC; + u32 temp = reg[4].I + 4 * cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 4); + THUMB_STM_REG(2, 1, 4); + THUMB_STM_REG(4, 2, 4); + THUMB_STM_REG(8, 3, 4); + THUMB_STM_REG(16, 4, 4); + THUMB_STM_REG(32, 5, 4); + THUMB_STM_REG(64, 6, 4); + THUMB_STM_REG(128, 7, 4); +} +break; +case 0xc5: +{ + // STM R5!, {Rlist} + u32 address = reg[5].I & 0xFFFFFFFC; + u32 temp = reg[5].I + 4 * cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 5); + THUMB_STM_REG(2, 1, 5); + THUMB_STM_REG(4, 2, 5); + THUMB_STM_REG(8, 3, 5); + THUMB_STM_REG(16, 4, 5); + THUMB_STM_REG(32, 5, 5); + THUMB_STM_REG(64, 6, 5); + THUMB_STM_REG(128, 7, 5); +} +break; +case 0xc6: +{ + // STM R6!, {Rlist} + u32 address = reg[6].I & 0xFFFFFFFC; + u32 temp = reg[6].I + 4 * cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 6); + THUMB_STM_REG(2, 1, 6); + THUMB_STM_REG(4, 2, 6); + THUMB_STM_REG(8, 3, 6); + THUMB_STM_REG(16, 4, 6); + THUMB_STM_REG(32, 5, 6); + THUMB_STM_REG(64, 6, 6); + THUMB_STM_REG(128, 7, 6); +} +break; +case 0xc7: +{ + // STM R7!, {Rlist} + u32 address = reg[7].I & 0xFFFFFFFC; + u32 temp = reg[7].I + 4 * cpuBitsSet[opcode & 0xff]; + int offset = 0; + // store + THUMB_STM_REG(1, 0, 7); + THUMB_STM_REG(2, 1, 7); + THUMB_STM_REG(4, 2, 7); + THUMB_STM_REG(8, 3, 7); + THUMB_STM_REG(16, 4, 7); + THUMB_STM_REG(32, 5, 7); + THUMB_STM_REG(64, 6, 7); + THUMB_STM_REG(128, 7, 7); +} +break; +#define THUMB_LDM_REG(val, r) \ + if (opcode & (val)) { \ + reg[(r)].I = CPUReadMemory(address); \ + if (offset) \ + clockTicks += 2 + CPUUpdateTicksAccessSeq32(address); \ + else \ + clockTicks += 2 + CPUUpdateTicksAccess32(address); \ + offset = 1; \ + address += 4; \ + } +case 0xc8: +{ + // LDM R0!, {Rlist} + u32 address = reg[0].I & 0xFFFFFFFC; + u32 temp = reg[0].I + 4 * cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if (!(opcode & 1)) + reg[0].I = temp; +} +break; +case 0xc9: +{ + // LDM R1!, {Rlist} + u32 address = reg[1].I & 0xFFFFFFFC; + u32 temp = reg[1].I + 4 * cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if (!(opcode & 2)) + reg[1].I = temp; +} +break; +case 0xca: +{ + // LDM R2!, {Rlist} + u32 address = reg[2].I & 0xFFFFFFFC; + u32 temp = reg[2].I + 4 * cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if (!(opcode & 4)) + reg[2].I = temp; +} +break; +case 0xcb: +{ + // LDM R3!, {Rlist} + u32 address = reg[3].I & 0xFFFFFFFC; + u32 temp = reg[3].I + 4 * cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if (!(opcode & 8)) + reg[3].I = temp; +} +break; +case 0xcc: +{ + // LDM R4!, {Rlist} + u32 address = reg[4].I & 0xFFFFFFFC; + u32 temp = reg[4].I + 4 * cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if (!(opcode & 16)) + reg[4].I = temp; +} +break; +case 0xcd: +{ + // LDM R5!, {Rlist} + u32 address = reg[5].I & 0xFFFFFFFC; + u32 temp = reg[5].I + 4 * cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if (!(opcode & 32)) + reg[5].I = temp; +} +break; +case 0xce: +{ + // LDM R6!, {Rlist} + u32 address = reg[6].I & 0xFFFFFFFC; + u32 temp = reg[6].I + 4 * cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if (!(opcode & 64)) + reg[6].I = temp; +} +break; +case 0xcf: +{ + // LDM R7!, {Rlist} + u32 address = reg[7].I & 0xFFFFFFFC; + u32 temp = reg[7].I + 4 * cpuBitsSet[opcode & 0xFF]; + int offset = 0; + // load + THUMB_LDM_REG(1, 0); + THUMB_LDM_REG(2, 1); + THUMB_LDM_REG(4, 2); + THUMB_LDM_REG(8, 3); + THUMB_LDM_REG(16, 4); + THUMB_LDM_REG(32, 5); + THUMB_LDM_REG(64, 6); + THUMB_LDM_REG(128, 7); + if (!(opcode & 128)) + reg[7].I = temp; +} +break; +case 0xd0: + // BEQ offset + if (Z_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd1: + // BNE offset + if (!Z_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd2: + // BCS offset + if (C_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd3: + // BCC offset + if (!C_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd4: + // BMI offset + if (N_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd5: + // BPL offset + if (!N_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd6: + // BVS offset + if (V_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd7: + // BVC offset + if (!V_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd8: + // BHI offset + if (C_FLAG && !Z_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xd9: + // BLS offset + if (!C_FLAG || Z_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xda: + // BGE offset + if (N_FLAG == V_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xdb: + // BLT offset + if (N_FLAG != V_FLAG) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xdc: + // BGT offset + if (!Z_FLAG && (N_FLAG == V_FLAG)) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xdd: + // BLE offset + if (Z_FLAG || (N_FLAG != V_FLAG)) + { + reg[15].I += ((s8)(opcode & 0xFF)) << 1; + armNextPC = reg[15].I; + reg[15].I += 2; + clockTicks = 3; + } + break; +case 0xdf: + // SWI #comment + CPUSoftwareInterrupt(opcode & 0xFF); + break; +case 0xe0: +case 0xe1: +case 0xe2: +case 0xe3: +case 0xe4: +case 0xe5: +case 0xe6: +case 0xe7: +{ + // B offset + int offset = (opcode & 0x3FF) << 1; + if (opcode & 0x0400) + offset |= 0xFFFFF800; + reg[15].I += offset; + armNextPC = reg[15].I; + reg[15].I += 2; +} +break; +case 0xf0: +case 0xf1: +case 0xf2: +case 0xf3: +{ + // BLL #offset + int offset = (opcode & 0x7FF); + reg[14].I = reg[15].I + (offset << 12); +} +break; +case 0xf4: +case 0xf5: +case 0xf6: +case 0xf7: +{ + // BLL #offset + int offset = (opcode & 0x7FF); + reg[14].I = reg[15].I + ((offset << 12) | 0xFF800000); +} +break; +case 0xf8: +case 0xf9: +case 0xfa: +case 0xfb: +case 0xfc: +case 0xfd: +case 0xfe: +case 0xff: +{ + // BLH #offset + int offset = (opcode & 0x7FF); + u32 temp = reg[15].I - 2; + reg[15].I = (reg[14].I + (offset << 1)) & 0xFFFFFFFE; + armNextPC = reg[15].I; + reg[15].I += 2; + reg[14].I = temp | 1; +} +break; +#ifdef BKPT_SUPPORT +case 0xbe: + // BKPT #comment + extern void (*dbgSignal)(int, int); + reg[15].I -= 2; + armNextPC -= 2; + dbgSignal(5, opcode & 255); + return; +#endif +case 0xb1: +case 0xb2: +case 0xb3: +case 0xb6: +case 0xb7: +case 0xb8: +case 0xb9: +case 0xba: +case 0xbb: +#ifndef BKPT_SUPPORT +case 0xbe: +#endif +case 0xbf: +case 0xde: +default: +unknown_thumb: +#ifdef GBA_LOGGING + if (systemVerbose & VERBOSE_UNDEFINED) + log("Undefined THUMB instruction %04x at %08x\n", opcode, armNextPC - 2); +#endif + CPUUndefinedException(); + break; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-configfile.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-configfile.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-filters.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-filters.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-input.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-input.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-joypadconfig.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-joypadconfig.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-main.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-main.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-screenarea.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-screenarea.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-system.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-system.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-tools.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-tools.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-window.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-window.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/.deps/gvba-windowcallbacks.Po --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/.deps/gvba-windowcallbacks.Po Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +# dummy diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/Makefile Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,817 @@ +# Makefile.in generated by automake 1.10.1 from Makefile.am. +# src/gtk/Makefile. Generated from Makefile.in by configure. + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + + + + + + +pkgdatadir = $(datadir)/VisualBoyAdvance +pkglibdir = $(libdir)/VisualBoyAdvance +pkgincludedir = $(includedir)/VisualBoyAdvance +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = x86_64-unknown-linux-gnu +host_triplet = x86_64-unknown-linux-gnu +target_triplet = x86_64-unknown-linux-gnu +bin_PROGRAMS = gvba$(EXEEXT) +subdir = src/gtk +DIST_COMMON = $(dist_pkgdata_DATA) $(srcdir)/Makefile.am \ + $(srcdir)/Makefile.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/gettext.m4 \ + $(top_srcdir)/m4/iconv.m4 $(top_srcdir)/m4/lib-ld.m4 \ + $(top_srcdir)/m4/lib-link.m4 $(top_srcdir)/m4/lib-prefix.m4 \ + $(top_srcdir)/m4/nls.m4 $(top_srcdir)/m4/po.m4 \ + $(top_srcdir)/m4/progtest.m4 $(top_srcdir)/acinclude.m4 \ + $(top_srcdir)/configure.in +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs +CONFIG_CLEAN_FILES = +LIBRARIES = $(noinst_LIBRARIES) +AR = ar +ARFLAGS = cru +libgvba_a_AR = $(AR) $(ARFLAGS) +libgvba_a_LIBADD = +am_libgvba_a_OBJECTS = +libgvba_a_OBJECTS = $(am_libgvba_a_OBJECTS) +am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(pkgdatadir)" +binPROGRAMS_INSTALL = $(INSTALL_PROGRAM) +PROGRAMS = $(bin_PROGRAMS) +am_gvba_OBJECTS = gvba-configfile.$(OBJEXT) gvba-filters.$(OBJEXT) \ + gvba-input.$(OBJEXT) gvba-joypadconfig.$(OBJEXT) \ + gvba-main.$(OBJEXT) gvba-screenarea.$(OBJEXT) \ + gvba-system.$(OBJEXT) gvba-tools.$(OBJEXT) \ + gvba-windowcallbacks.$(OBJEXT) gvba-window.$(OBJEXT) +gvba_OBJECTS = $(am_gvba_OBJECTS) +gvba_LINK = $(CXXLD) $(gvba_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \ + $(LDFLAGS) -o $@ +DEFAULT_INCLUDES = -I. +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__depfiles_maybe = depfiles +CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) +CXXLD = $(CXX) +CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) \ + -o $@ +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +CCLD = $(CC) +LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ +SOURCES = $(libgvba_a_SOURCES) $(gvba_SOURCES) +DIST_SOURCES = $(libgvba_a_SOURCES) $(gvba_SOURCES) +RECURSIVE_TARGETS = all-recursive check-recursive dvi-recursive \ + html-recursive info-recursive install-data-recursive \ + install-dvi-recursive install-exec-recursive \ + install-html-recursive install-info-recursive \ + install-pdf-recursive install-ps-recursive install-recursive \ + installcheck-recursive installdirs-recursive pdf-recursive \ + ps-recursive uninstall-recursive +am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; +am__vpath_adj = case $$p in \ + $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ + *) f=$$p;; \ + esac; +am__strip_dir = `echo $$p | sed -e 's|^.*/||'`; +dist_pkgdataDATA_INSTALL = $(INSTALL_DATA) +DATA = $(dist_pkgdata_DATA) +RECURSIVE_CLEAN_TARGETS = mostlyclean-recursive clean-recursive \ + distclean-recursive maintainer-clean-recursive +ETAGS = etags +CTAGS = ctags +DIST_SUBDIRS = $(SUBDIRS) +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = ${SHELL} /home/r/proj/vba/trunk/missing --run aclocal-1.10 +AMTAR = ${SHELL} /home/r/proj/vba/trunk/missing --run tar +AUTOCONF = ${SHELL} /home/r/proj/vba/trunk/missing --run autoconf +AUTOHEADER = ${SHELL} /home/r/proj/vba/trunk/missing --run autoheader +AUTOMAKE = ${SHELL} /home/r/proj/vba/trunk/missing --run automake-1.10 +AWK = gawk +CC = gcc +CCDEPMODE = depmode=gcc3 +CFLAGS = -g -O2 +CPP = gcc -E +CPPFLAGS = +CXX = g++ +CXXDEPMODE = depmode=gcc3 +CXXFLAGS = -g -O2 -DC_CORE -DPROFILING -DDEV_VERSION +CYGPATH_W = echo +DEFS = -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"VisualBoyAdvance\" -DVERSION=\"1.7.2\" -DYYTEXT_POINTER=1 -DHAVE_LIBZ=1 -DHAVE_LIBPNG=1 -DHAVE_LIBPTHREAD=1 -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_MALLOC_H=1 -DHAVE_STRINGS_H=1 -DHAVE_UNISTD_H=1 -DHAVE_ARPA_INET_H=1 -DHAVE_NETINET_IN_H=1 +DEPDIR = .deps +ECHO_C = +ECHO_N = -n +ECHO_T = +EGREP = /bin/grep -E +EXEEXT = +GETTEXT_PACKAGE = +GMSGFMT = +GREP = /bin/grep +GTKMM_CFLAGS = +GTKMM_CPPFLAGS = +GTKMM_LIBS = +INSTALL = /usr/bin/install -c +INSTALL_DATA = ${INSTALL} -m 644 +INSTALL_PROGRAM = ${INSTALL} +INSTALL_SCRIPT = ${INSTALL} +INSTALL_STRIP_PROGRAM = $(install_sh) -c -s +INTLLIBS = +LDFLAGS = +LEX = flex +LEXLIB = -lfl +LEX_OUTPUT_ROOT = lex.yy +LIBICONV = +LIBINTL = +LIBOBJS = +LIBS = -lpthread -lpng -lz +LTLIBICONV = +LTLIBINTL = +LTLIBOBJS = +MAKEINFO = ${SHELL} /home/r/proj/vba/trunk/missing --run makeinfo +MKDIR_P = /bin/mkdir -p +MKINSTALLDIRS = +MSGFMT = +MSGMERGE = +NASM = /usr/bin/nasm +OBJEXT = o +PACKAGE = VisualBoyAdvance +PACKAGE_BUGREPORT = +PACKAGE_NAME = +PACKAGE_STRING = +PACKAGE_TARNAME = +PACKAGE_VERSION = +PATH_SEPARATOR = : +PKG_CONFIG = +POSUB = +RANLIB = ranlib +SDL_CFLAGS = -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT +SDL_CONFIG = /usr/bin/sdl-config +SDL_LIBS = -L/usr/lib -Wl,-rpath,/usr/lib -lSDL -lpthread +SET_MAKE = +SHELL = /bin/sh +STRIP = +USE_NLS = +VBA_EXTRA = +VBA_LIBS = ../gba/libgba.a ../gb/libgb.a ../common/libgbcom.a ../filters/libfilter.a ../lua/libgblua.a ../prof/libprof.a +VBA_SRC_EXTRA = lua prof sdl +VERSION = 1.7.2 +XGETTEXT = +XMKMF = +YACC = bison -y +YFLAGS = +abs_builddir = /home/r/proj/vba/trunk/src/gtk +abs_srcdir = /home/r/proj/vba/trunk/src/gtk +abs_top_builddir = /home/r/proj/vba/trunk +abs_top_srcdir = /home/r/proj/vba/trunk +ac_ct_CC = gcc +ac_ct_CXX = g++ +am__include = include +am__leading_dot = . +am__quote = +am__tar = ${AMTAR} chof - "$$tardir" +am__untar = ${AMTAR} xf - +bindir = ${exec_prefix}/bin +build = x86_64-unknown-linux-gnu +build_alias = +build_cpu = x86_64 +build_os = linux-gnu +build_vendor = unknown +builddir = . +datadir = ${datarootdir} +datarootdir = ${prefix}/share +docdir = ${datarootdir}/doc/${PACKAGE} +dvidir = ${docdir} +exec_prefix = ${prefix} +host = x86_64-unknown-linux-gnu +host_alias = +host_cpu = x86_64 +host_os = linux-gnu +host_vendor = unknown +htmldir = ${docdir} +includedir = ${prefix}/include +infodir = ${datarootdir}/info +install_sh = $(SHELL) /home/r/proj/vba/trunk/install-sh +libdir = ${exec_prefix}/lib +libexecdir = ${exec_prefix}/libexec +localedir = ${datarootdir}/locale +localstatedir = ${prefix}/var +mandir = ${datarootdir}/man +mkdir_p = /bin/mkdir -p +oldincludedir = /usr/include +pdfdir = ${docdir} +prefix = /usr/local +program_transform_name = s,x,x, +psdir = ${docdir} +sbindir = ${exec_prefix}/sbin +sharedstatedir = ${prefix}/com +srcdir = . +sysconfdir = ${prefix}/etc +target = x86_64-unknown-linux-gnu +target_alias = +target_cpu = x86_64 +target_os = linux-gnu +target_vendor = unknown +top_builddir = ../.. +top_srcdir = ../.. +SUBDIRS = images +noinst_LIBRARIES = libgvba.a +gvba_SOURCES = \ + configfile.cpp \ + configfile.h \ + filters.cpp \ + filters.h \ + input.cpp \ + input.h \ + intl.h \ + joypadconfig.cpp \ + joypadconfig.h \ + main.cpp \ + menuitem.h \ + screenarea.cpp \ + screenarea.h \ + sigccompat.h \ + system.cpp \ + tools.cpp \ + tools.h \ + windowcallbacks.cpp \ + window.cpp \ + window.h + +gvba_LDADD = libgvba.a ../gba/libgba.a ../gb/libgb.a ../common/libgbcom.a ../filters/libfilter.a ../lua/libgblua.a ../prof/libprof.a -L/usr/lib -Wl,-rpath,/usr/lib -lSDL -lpthread +gvba_DEPENDENCIES = libgvba.a ../gba/libgba.a ../gb/libgb.a ../common/libgbcom.a ../filters/libfilter.a ../lua/libgblua.a ../prof/libprof.a +gvba_CPPFLAGS = \ + -DPKGDATADIR=\"$(pkgdatadir)\" \ + -DLOCALEDIR=\"$(datadir)/locale\" \ + -DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \ + $(GTKMM_CPPFLAGS) + +gvba_CXXFLAGS = -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT +libgvba_a_SOURCES = \ + ../AutoBuild.h \ + ../NLS.h \ + ../Port.h + +libgvba_a_CPPFLAGS = -DSDL +libgvba_a_CXXFLAGS = -fno-exceptions +dist_pkgdata_DATA = vba.glade +all: all-recursive + +.SUFFIXES: +.SUFFIXES: .cpp .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \ + && exit 0; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/gtk/Makefile'; \ + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu src/gtk/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +clean-noinstLIBRARIES: + -test -z "$(noinst_LIBRARIES)" || rm -f $(noinst_LIBRARIES) +libgvba.a: $(libgvba_a_OBJECTS) $(libgvba_a_DEPENDENCIES) + -rm -f libgvba.a + $(libgvba_a_AR) libgvba.a $(libgvba_a_OBJECTS) $(libgvba_a_LIBADD) + $(RANLIB) libgvba.a +install-binPROGRAMS: $(bin_PROGRAMS) + @$(NORMAL_INSTALL) + test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)" + @list='$(bin_PROGRAMS)'; for p in $$list; do \ + p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \ + if test -f $$p \ + ; then \ + f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \ + echo " $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) '$$p' '$(DESTDIR)$(bindir)/$$f'"; \ + $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) "$$p" "$(DESTDIR)$(bindir)/$$f" || exit 1; \ + else :; fi; \ + done + +uninstall-binPROGRAMS: + @$(NORMAL_UNINSTALL) + @list='$(bin_PROGRAMS)'; for p in $$list; do \ + f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \ + echo " rm -f '$(DESTDIR)$(bindir)/$$f'"; \ + rm -f "$(DESTDIR)$(bindir)/$$f"; \ + done + +clean-binPROGRAMS: + -test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS) +gvba$(EXEEXT): $(gvba_OBJECTS) $(gvba_DEPENDENCIES) + @rm -f gvba$(EXEEXT) + $(gvba_LINK) $(gvba_OBJECTS) $(gvba_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +include ./$(DEPDIR)/gvba-configfile.Po +include ./$(DEPDIR)/gvba-filters.Po +include ./$(DEPDIR)/gvba-input.Po +include ./$(DEPDIR)/gvba-joypadconfig.Po +include ./$(DEPDIR)/gvba-main.Po +include ./$(DEPDIR)/gvba-screenarea.Po +include ./$(DEPDIR)/gvba-system.Po +include ./$(DEPDIR)/gvba-tools.Po +include ./$(DEPDIR)/gvba-window.Po +include ./$(DEPDIR)/gvba-windowcallbacks.Po + +.cpp.o: + $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< + mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +# source='$<' object='$@' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXXCOMPILE) -c -o $@ $< + +.cpp.obj: + $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` + mv -f $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +# source='$<' object='$@' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +gvba-configfile.o: configfile.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-configfile.o -MD -MP -MF $(DEPDIR)/gvba-configfile.Tpo -c -o gvba-configfile.o `test -f 'configfile.cpp' || echo '$(srcdir)/'`configfile.cpp + mv -f $(DEPDIR)/gvba-configfile.Tpo $(DEPDIR)/gvba-configfile.Po +# source='configfile.cpp' object='gvba-configfile.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-configfile.o `test -f 'configfile.cpp' || echo '$(srcdir)/'`configfile.cpp + +gvba-configfile.obj: configfile.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-configfile.obj -MD -MP -MF $(DEPDIR)/gvba-configfile.Tpo -c -o gvba-configfile.obj `if test -f 'configfile.cpp'; then $(CYGPATH_W) 'configfile.cpp'; else $(CYGPATH_W) '$(srcdir)/configfile.cpp'; fi` + mv -f $(DEPDIR)/gvba-configfile.Tpo $(DEPDIR)/gvba-configfile.Po +# source='configfile.cpp' object='gvba-configfile.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-configfile.obj `if test -f 'configfile.cpp'; then $(CYGPATH_W) 'configfile.cpp'; else $(CYGPATH_W) '$(srcdir)/configfile.cpp'; fi` + +gvba-filters.o: filters.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-filters.o -MD -MP -MF $(DEPDIR)/gvba-filters.Tpo -c -o gvba-filters.o `test -f 'filters.cpp' || echo '$(srcdir)/'`filters.cpp + mv -f $(DEPDIR)/gvba-filters.Tpo $(DEPDIR)/gvba-filters.Po +# source='filters.cpp' object='gvba-filters.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-filters.o `test -f 'filters.cpp' || echo '$(srcdir)/'`filters.cpp + +gvba-filters.obj: filters.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-filters.obj -MD -MP -MF $(DEPDIR)/gvba-filters.Tpo -c -o gvba-filters.obj `if test -f 'filters.cpp'; then $(CYGPATH_W) 'filters.cpp'; else $(CYGPATH_W) '$(srcdir)/filters.cpp'; fi` + mv -f $(DEPDIR)/gvba-filters.Tpo $(DEPDIR)/gvba-filters.Po +# source='filters.cpp' object='gvba-filters.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-filters.obj `if test -f 'filters.cpp'; then $(CYGPATH_W) 'filters.cpp'; else $(CYGPATH_W) '$(srcdir)/filters.cpp'; fi` + +gvba-input.o: input.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-input.o -MD -MP -MF $(DEPDIR)/gvba-input.Tpo -c -o gvba-input.o `test -f 'input.cpp' || echo '$(srcdir)/'`input.cpp + mv -f $(DEPDIR)/gvba-input.Tpo $(DEPDIR)/gvba-input.Po +# source='input.cpp' object='gvba-input.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-input.o `test -f 'input.cpp' || echo '$(srcdir)/'`input.cpp + +gvba-input.obj: input.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-input.obj -MD -MP -MF $(DEPDIR)/gvba-input.Tpo -c -o gvba-input.obj `if test -f 'input.cpp'; then $(CYGPATH_W) 'input.cpp'; else $(CYGPATH_W) '$(srcdir)/input.cpp'; fi` + mv -f $(DEPDIR)/gvba-input.Tpo $(DEPDIR)/gvba-input.Po +# source='input.cpp' object='gvba-input.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-input.obj `if test -f 'input.cpp'; then $(CYGPATH_W) 'input.cpp'; else $(CYGPATH_W) '$(srcdir)/input.cpp'; fi` + +gvba-joypadconfig.o: joypadconfig.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-joypadconfig.o -MD -MP -MF $(DEPDIR)/gvba-joypadconfig.Tpo -c -o gvba-joypadconfig.o `test -f 'joypadconfig.cpp' || echo '$(srcdir)/'`joypadconfig.cpp + mv -f $(DEPDIR)/gvba-joypadconfig.Tpo $(DEPDIR)/gvba-joypadconfig.Po +# source='joypadconfig.cpp' object='gvba-joypadconfig.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-joypadconfig.o `test -f 'joypadconfig.cpp' || echo '$(srcdir)/'`joypadconfig.cpp + +gvba-joypadconfig.obj: joypadconfig.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-joypadconfig.obj -MD -MP -MF $(DEPDIR)/gvba-joypadconfig.Tpo -c -o gvba-joypadconfig.obj `if test -f 'joypadconfig.cpp'; then $(CYGPATH_W) 'joypadconfig.cpp'; else $(CYGPATH_W) '$(srcdir)/joypadconfig.cpp'; fi` + mv -f $(DEPDIR)/gvba-joypadconfig.Tpo $(DEPDIR)/gvba-joypadconfig.Po +# source='joypadconfig.cpp' object='gvba-joypadconfig.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-joypadconfig.obj `if test -f 'joypadconfig.cpp'; then $(CYGPATH_W) 'joypadconfig.cpp'; else $(CYGPATH_W) '$(srcdir)/joypadconfig.cpp'; fi` + +gvba-main.o: main.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-main.o -MD -MP -MF $(DEPDIR)/gvba-main.Tpo -c -o gvba-main.o `test -f 'main.cpp' || echo '$(srcdir)/'`main.cpp + mv -f $(DEPDIR)/gvba-main.Tpo $(DEPDIR)/gvba-main.Po +# source='main.cpp' object='gvba-main.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-main.o `test -f 'main.cpp' || echo '$(srcdir)/'`main.cpp + +gvba-main.obj: main.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-main.obj -MD -MP -MF $(DEPDIR)/gvba-main.Tpo -c -o gvba-main.obj `if test -f 'main.cpp'; then $(CYGPATH_W) 'main.cpp'; else $(CYGPATH_W) '$(srcdir)/main.cpp'; fi` + mv -f $(DEPDIR)/gvba-main.Tpo $(DEPDIR)/gvba-main.Po +# source='main.cpp' object='gvba-main.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-main.obj `if test -f 'main.cpp'; then $(CYGPATH_W) 'main.cpp'; else $(CYGPATH_W) '$(srcdir)/main.cpp'; fi` + +gvba-screenarea.o: screenarea.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-screenarea.o -MD -MP -MF $(DEPDIR)/gvba-screenarea.Tpo -c -o gvba-screenarea.o `test -f 'screenarea.cpp' || echo '$(srcdir)/'`screenarea.cpp + mv -f $(DEPDIR)/gvba-screenarea.Tpo $(DEPDIR)/gvba-screenarea.Po +# source='screenarea.cpp' object='gvba-screenarea.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-screenarea.o `test -f 'screenarea.cpp' || echo '$(srcdir)/'`screenarea.cpp + +gvba-screenarea.obj: screenarea.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-screenarea.obj -MD -MP -MF $(DEPDIR)/gvba-screenarea.Tpo -c -o gvba-screenarea.obj `if test -f 'screenarea.cpp'; then $(CYGPATH_W) 'screenarea.cpp'; else $(CYGPATH_W) '$(srcdir)/screenarea.cpp'; fi` + mv -f $(DEPDIR)/gvba-screenarea.Tpo $(DEPDIR)/gvba-screenarea.Po +# source='screenarea.cpp' object='gvba-screenarea.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-screenarea.obj `if test -f 'screenarea.cpp'; then $(CYGPATH_W) 'screenarea.cpp'; else $(CYGPATH_W) '$(srcdir)/screenarea.cpp'; fi` + +gvba-system.o: system.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-system.o -MD -MP -MF $(DEPDIR)/gvba-system.Tpo -c -o gvba-system.o `test -f 'system.cpp' || echo '$(srcdir)/'`system.cpp + mv -f $(DEPDIR)/gvba-system.Tpo $(DEPDIR)/gvba-system.Po +# source='system.cpp' object='gvba-system.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-system.o `test -f 'system.cpp' || echo '$(srcdir)/'`system.cpp + +gvba-system.obj: system.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-system.obj -MD -MP -MF $(DEPDIR)/gvba-system.Tpo -c -o gvba-system.obj `if test -f 'system.cpp'; then $(CYGPATH_W) 'system.cpp'; else $(CYGPATH_W) '$(srcdir)/system.cpp'; fi` + mv -f $(DEPDIR)/gvba-system.Tpo $(DEPDIR)/gvba-system.Po +# source='system.cpp' object='gvba-system.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-system.obj `if test -f 'system.cpp'; then $(CYGPATH_W) 'system.cpp'; else $(CYGPATH_W) '$(srcdir)/system.cpp'; fi` + +gvba-tools.o: tools.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-tools.o -MD -MP -MF $(DEPDIR)/gvba-tools.Tpo -c -o gvba-tools.o `test -f 'tools.cpp' || echo '$(srcdir)/'`tools.cpp + mv -f $(DEPDIR)/gvba-tools.Tpo $(DEPDIR)/gvba-tools.Po +# source='tools.cpp' object='gvba-tools.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-tools.o `test -f 'tools.cpp' || echo '$(srcdir)/'`tools.cpp + +gvba-tools.obj: tools.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-tools.obj -MD -MP -MF $(DEPDIR)/gvba-tools.Tpo -c -o gvba-tools.obj `if test -f 'tools.cpp'; then $(CYGPATH_W) 'tools.cpp'; else $(CYGPATH_W) '$(srcdir)/tools.cpp'; fi` + mv -f $(DEPDIR)/gvba-tools.Tpo $(DEPDIR)/gvba-tools.Po +# source='tools.cpp' object='gvba-tools.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-tools.obj `if test -f 'tools.cpp'; then $(CYGPATH_W) 'tools.cpp'; else $(CYGPATH_W) '$(srcdir)/tools.cpp'; fi` + +gvba-windowcallbacks.o: windowcallbacks.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-windowcallbacks.o -MD -MP -MF $(DEPDIR)/gvba-windowcallbacks.Tpo -c -o gvba-windowcallbacks.o `test -f 'windowcallbacks.cpp' || echo '$(srcdir)/'`windowcallbacks.cpp + mv -f $(DEPDIR)/gvba-windowcallbacks.Tpo $(DEPDIR)/gvba-windowcallbacks.Po +# source='windowcallbacks.cpp' object='gvba-windowcallbacks.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-windowcallbacks.o `test -f 'windowcallbacks.cpp' || echo '$(srcdir)/'`windowcallbacks.cpp + +gvba-windowcallbacks.obj: windowcallbacks.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-windowcallbacks.obj -MD -MP -MF $(DEPDIR)/gvba-windowcallbacks.Tpo -c -o gvba-windowcallbacks.obj `if test -f 'windowcallbacks.cpp'; then $(CYGPATH_W) 'windowcallbacks.cpp'; else $(CYGPATH_W) '$(srcdir)/windowcallbacks.cpp'; fi` + mv -f $(DEPDIR)/gvba-windowcallbacks.Tpo $(DEPDIR)/gvba-windowcallbacks.Po +# source='windowcallbacks.cpp' object='gvba-windowcallbacks.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-windowcallbacks.obj `if test -f 'windowcallbacks.cpp'; then $(CYGPATH_W) 'windowcallbacks.cpp'; else $(CYGPATH_W) '$(srcdir)/windowcallbacks.cpp'; fi` + +gvba-window.o: window.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-window.o -MD -MP -MF $(DEPDIR)/gvba-window.Tpo -c -o gvba-window.o `test -f 'window.cpp' || echo '$(srcdir)/'`window.cpp + mv -f $(DEPDIR)/gvba-window.Tpo $(DEPDIR)/gvba-window.Po +# source='window.cpp' object='gvba-window.o' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-window.o `test -f 'window.cpp' || echo '$(srcdir)/'`window.cpp + +gvba-window.obj: window.cpp + $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -MT gvba-window.obj -MD -MP -MF $(DEPDIR)/gvba-window.Tpo -c -o gvba-window.obj `if test -f 'window.cpp'; then $(CYGPATH_W) 'window.cpp'; else $(CYGPATH_W) '$(srcdir)/window.cpp'; fi` + mv -f $(DEPDIR)/gvba-window.Tpo $(DEPDIR)/gvba-window.Po +# source='window.cpp' object='gvba-window.obj' libtool=no \ +# DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) \ +# $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(gvba_CPPFLAGS) $(CPPFLAGS) $(gvba_CXXFLAGS) $(CXXFLAGS) -c -o gvba-window.obj `if test -f 'window.cpp'; then $(CYGPATH_W) 'window.cpp'; else $(CYGPATH_W) '$(srcdir)/window.cpp'; fi` +install-dist_pkgdataDATA: $(dist_pkgdata_DATA) + @$(NORMAL_INSTALL) + test -z "$(pkgdatadir)" || $(MKDIR_P) "$(DESTDIR)$(pkgdatadir)" + @list='$(dist_pkgdata_DATA)'; for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + f=$(am__strip_dir) \ + echo " $(dist_pkgdataDATA_INSTALL) '$$d$$p' '$(DESTDIR)$(pkgdatadir)/$$f'"; \ + $(dist_pkgdataDATA_INSTALL) "$$d$$p" "$(DESTDIR)$(pkgdatadir)/$$f"; \ + done + +uninstall-dist_pkgdataDATA: + @$(NORMAL_UNINSTALL) + @list='$(dist_pkgdata_DATA)'; for p in $$list; do \ + f=$(am__strip_dir) \ + echo " rm -f '$(DESTDIR)$(pkgdatadir)/$$f'"; \ + rm -f "$(DESTDIR)$(pkgdatadir)/$$f"; \ + done + +# This directory's subdirectories are mostly independent; you can cd +# into them and run `make' without going through this Makefile. +# To change the values of `make' variables: instead of editing Makefiles, +# (1) if the variable is set in `config.status', edit `config.status' +# (which will cause the Makefiles to be regenerated when you run `make'); +# (2) otherwise, pass the desired values on the `make' command line. +$(RECURSIVE_TARGETS): + @failcom='exit 1'; \ + for f in x $$MAKEFLAGS; do \ + case $$f in \ + *=* | --[!k]*);; \ + *k*) failcom='fail=yes';; \ + esac; \ + done; \ + dot_seen=no; \ + target=`echo $@ | sed s/-recursive//`; \ + list='$(SUBDIRS)'; for subdir in $$list; do \ + echo "Making $$target in $$subdir"; \ + if test "$$subdir" = "."; then \ + dot_seen=yes; \ + local_target="$$target-am"; \ + else \ + local_target="$$target"; \ + fi; \ + (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $$local_target) \ + || eval $$failcom; \ + done; \ + if test "$$dot_seen" = "no"; then \ + $(MAKE) $(AM_MAKEFLAGS) "$$target-am" || exit 1; \ + fi; test -z "$$fail" + +$(RECURSIVE_CLEAN_TARGETS): + @failcom='exit 1'; \ + for f in x $$MAKEFLAGS; do \ + case $$f in \ + *=* | --[!k]*);; \ + *k*) failcom='fail=yes';; \ + esac; \ + done; \ + dot_seen=no; \ + case "$@" in \ + distclean-* | maintainer-clean-*) list='$(DIST_SUBDIRS)' ;; \ + *) list='$(SUBDIRS)' ;; \ + esac; \ + rev=''; for subdir in $$list; do \ + if test "$$subdir" = "."; then :; else \ + rev="$$subdir $$rev"; \ + fi; \ + done; \ + rev="$$rev ."; \ + target=`echo $@ | sed s/-recursive//`; \ + for subdir in $$rev; do \ + echo "Making $$target in $$subdir"; \ + if test "$$subdir" = "."; then \ + local_target="$$target-am"; \ + else \ + local_target="$$target"; \ + fi; \ + (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $$local_target) \ + || eval $$failcom; \ + done && test -z "$$fail" +tags-recursive: + list='$(SUBDIRS)'; for subdir in $$list; do \ + test "$$subdir" = . || (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) tags); \ + done +ctags-recursive: + list='$(SUBDIRS)'; for subdir in $$list; do \ + test "$$subdir" = . || (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) ctags); \ + done + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonemtpy = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: tags-recursive $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + tags=; \ + here=`pwd`; \ + if ($(ETAGS) --etags-include --version) >/dev/null 2>&1; then \ + include_option=--etags-include; \ + empty_fix=.; \ + else \ + include_option=--include; \ + empty_fix=; \ + fi; \ + list='$(SUBDIRS)'; for subdir in $$list; do \ + if test "$$subdir" = .; then :; else \ + test ! -f $$subdir/TAGS || \ + tags="$$tags $$include_option=$$here/$$subdir/TAGS"; \ + fi; \ + done; \ + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + if test -z "$(ETAGS_ARGS)$$tags$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$tags $$unique; \ + fi +ctags: CTAGS +CTAGS: ctags-recursive $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + tags=; \ + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + test -z "$(CTAGS_ARGS)$$tags$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$tags $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && cd $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) $$here + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ + fi; \ + cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ + else \ + test -f $(distdir)/$$file \ + || cp -p $$d/$$file $(distdir)/$$file \ + || exit 1; \ + fi; \ + done + list='$(DIST_SUBDIRS)'; for subdir in $$list; do \ + if test "$$subdir" = .; then :; else \ + test -d "$(distdir)/$$subdir" \ + || $(MKDIR_P) "$(distdir)/$$subdir" \ + || exit 1; \ + distdir=`$(am__cd) $(distdir) && pwd`; \ + top_distdir=`$(am__cd) $(top_distdir) && pwd`; \ + (cd $$subdir && \ + $(MAKE) $(AM_MAKEFLAGS) \ + top_distdir="$$top_distdir" \ + distdir="$$distdir/$$subdir" \ + am__remove_distdir=: \ + am__skip_length_check=: \ + distdir) \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-recursive +all-am: Makefile $(LIBRARIES) $(PROGRAMS) $(DATA) +installdirs: installdirs-recursive +installdirs-am: + for dir in "$(DESTDIR)$(bindir)" "$(DESTDIR)$(pkgdatadir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-recursive +install-exec: install-exec-recursive +install-data: install-data-recursive +uninstall: uninstall-recursive + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-recursive +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-recursive + +clean-am: clean-binPROGRAMS clean-generic clean-noinstLIBRARIES \ + mostlyclean-am + +distclean: distclean-recursive + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-recursive + +dvi-am: + +html: html-recursive + +info: info-recursive + +info-am: + +install-data-am: install-dist_pkgdataDATA + +install-dvi: install-dvi-recursive + +install-exec-am: install-binPROGRAMS + +install-html: install-html-recursive + +install-info: install-info-recursive + +install-man: + +install-pdf: install-pdf-recursive + +install-ps: install-ps-recursive + +installcheck-am: + +maintainer-clean: maintainer-clean-recursive + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-recursive + +mostlyclean-am: mostlyclean-compile mostlyclean-generic + +pdf: pdf-recursive + +pdf-am: + +ps: ps-recursive + +ps-am: + +uninstall-am: uninstall-binPROGRAMS uninstall-dist_pkgdataDATA + +.MAKE: $(RECURSIVE_CLEAN_TARGETS) $(RECURSIVE_TARGETS) install-am \ + install-strip + +.PHONY: $(RECURSIVE_CLEAN_TARGETS) $(RECURSIVE_TARGETS) CTAGS GTAGS \ + all all-am check check-am clean clean-binPROGRAMS \ + clean-generic clean-noinstLIBRARIES ctags ctags-recursive \ + distclean distclean-compile distclean-generic distclean-tags \ + distdir dvi dvi-am html html-am info info-am install \ + install-am install-binPROGRAMS install-data install-data-am \ + install-dist_pkgdataDATA install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs installdirs-am \ + maintainer-clean maintainer-clean-generic mostlyclean \ + mostlyclean-compile mostlyclean-generic pdf pdf-am ps ps-am \ + tags tags-recursive uninstall uninstall-am \ + uninstall-binPROGRAMS uninstall-dist_pkgdataDATA + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,50 @@ +SUBDIRS = images + +bin_PROGRAMS = gvba + +noinst_LIBRARIES = libgvba.a + +gvba_SOURCES = \ + configfile.cpp \ + configfile.h \ + filters.cpp \ + filters.h \ + input.cpp \ + input.h \ + intl.h \ + joypadconfig.cpp \ + joypadconfig.h \ + main.cpp \ + menuitem.h \ + screenarea.cpp \ + screenarea.h \ + sigccompat.h \ + system.cpp \ + tools.cpp \ + tools.h \ + windowcallbacks.cpp \ + window.cpp \ + window.h + +gvba_LDADD = libgvba.a @VBA_LIBS@ @GTKMM_LIBS@ @LIBINTL@ @SDL_LIBS@ + +gvba_DEPENDENCIES = libgvba.a @VBA_LIBS@ + +gvba_CPPFLAGS = \ + -DPKGDATADIR=\"$(pkgdatadir)\" \ + -DLOCALEDIR=\"$(datadir)/locale\" \ + -DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \ + $(GTKMM_CPPFLAGS) + +gvba_CXXFLAGS = @GTKMM_CFLAGS@ @SDL_CFLAGS@ + +libgvba_a_SOURCES = \ + ../AutoBuild.h \ + ../NLS.h \ + ../Port.h + +libgvba_a_CPPFLAGS = -DSDL + +libgvba_a_CXXFLAGS = -fno-exceptions + +dist_pkgdata_DATA = vba.glade diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/configfile.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/configfile.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,262 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "configfile.h" + +#include +#include + +#include +#include + +namespace VBA +{ +namespace Config +{ + +using std::string; +using Glib::IOChannel; + +Line::Line(const string & _rsKey, const string & _rsValue) : + m_sKey(_rsKey), + m_sValue(_rsValue) +{ +} + +Section::Section(const string & _rsName) : + m_sName(_rsName) +{ +} + +bool Section::bKeyExists(const string & _rsKey) +{ + for (iterator it = begin(); it != end(); it++) + { + if (it->m_sKey == _rsKey) + { + return true; + } + } + return false; +} + +void Section::vSetKey(const string & _rsKey, const string & _rsValue) +{ + for (iterator it = begin(); it != end(); it++) + { + if (it->m_sKey == _rsKey) + { + it->m_sValue = _rsValue; + return; + } + } + push_back(Line(_rsKey, _rsValue)); +} + +string Section::sGetKey(const string & _rsKey) const +{ + for (const_iterator it = begin(); it != end(); it++) + { + if (it->m_sKey == _rsKey) + { + return it->m_sValue; + } + } + throw KeyNotFound(m_sName, _rsKey); +} + +void Section::vRemoveKey(const string & _rsKey) +{ + for (iterator it = begin(); it != end(); it++) + { + if (it->m_sKey == _rsKey) + { + erase(it); + return; + } + } +} + +File::File() +{ +} + +File::File(const string & _rsFile) +{ + vLoad(_rsFile); +} + +File::~File() +{ +} + +bool File::bSectionExists(const string & _rsName) +{ + for (iterator it = begin(); it != end(); it++) + { + if (it->sGetName() == _rsName) + { + return true; + } + } + return false; +} + +Section * File::poAddSection(const string & _rsName) +{ + Section * poSection = NULL; + for (iterator it = begin(); it != end(); it++) + { + if (it->sGetName() == _rsName) + { + poSection = &(*it); + } + } + if (poSection == NULL) + { + push_back(Section(_rsName)); + poSection = &back(); + } + return poSection; +} + +Section * File::poGetSection(const string & _rsName) +{ + for (iterator it = begin(); it != end(); it++) + { + if (it->sGetName() == _rsName) + { + return &(*it); + } + } + throw SectionNotFound(_rsName); +} + +void File::vRemoveSection(const string & _rsName) +{ + for (iterator it = begin(); it != end(); it++) + { + if (it->sGetName() == _rsName) + { + erase(it); + return; + } + } +} + +void File::vLoad(const string & _rsFile, + bool _bAddSection, + bool _bAddKey) +{ + string sBuffer = Glib::file_get_contents(_rsFile); + Section * poSection = NULL; + char ** lines = g_strsplit(sBuffer.c_str(), "\n", 0); + char * tmp; + int i = 0; + while (lines[i]) + { + if (lines[i][0] == '[') + { + if ((tmp = strchr(lines[i], ']'))) + { + *tmp = '\0'; + if (_bAddSection) + { + poSection = poAddSection(&lines[i][1]); + } + else + { + try + { + poSection = poGetSection(&lines[i][1]); + } + catch (...) + { + poSection = NULL; + } + } + } + } + else if (lines[i][0] != '#' && poSection != NULL) + { + if ((tmp = strchr(lines[i], '='))) + { + *tmp = '\0'; + tmp++; + if (_bAddKey || poSection->bKeyExists(lines[i])) + { + poSection->vSetKey(lines[i], tmp); + } + } + } + i++; + } + g_strfreev(lines); +} + +void File::vSave(const string & _rsFile) +{ + Glib::RefPtr poFile = IOChannel::create_from_file(_rsFile, "w"); + poFile->set_encoding(""); + + for (const_iterator poSection = begin(); + poSection != end(); + poSection++) + { + string sName = "[" + poSection->sGetName() + "]\n"; + poFile->write(sName); + + for (Section::const_iterator poLine = poSection->begin(); + poLine != poSection->end(); + poLine++) + { + string sLine = poLine->m_sKey + "=" + poLine->m_sValue + "\n"; + poFile->write(sLine); + } + poFile->write("\n"); + } +} + +void File::vClear() +{ + clear(); +} + +std::ostream & operator<<(std::ostream & _roOut, const File & _roFile) +{ + for (File::const_iterator poSection = _roFile.begin(); + poSection != _roFile.end(); + poSection++) + { + string sName = "[" + poSection->sGetName() + "]\n"; + _roOut << sName; + + for (Section::const_iterator poLine = poSection->begin(); + poLine != poSection->end(); + poLine++) + { + string sLine = poLine->m_sKey + "=" + poLine->m_sValue + "\n"; + _roOut << sLine; + } + _roOut << "\n"; + } + return _roOut; +} + +} // namespace Config +} // namespace VBA diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/configfile.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/configfile.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,204 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_CONFIGFILE_H__ +#define __VBA_CONFIGFILE_H__ + +#include +#include +#include +#include + +namespace VBA +{ +namespace Config +{ + +class NotFound +{ +public: + virtual ~NotFound() {} + +protected: + NotFound() {} +}; + +class SectionNotFound : public NotFound +{ +public: + SectionNotFound(const std::string & _rsName) : + m_sName(_rsName) + { + } + virtual ~SectionNotFound() {} + + inline std::string sGetName() const { return m_sName; } + +private: + std::string m_sName; +}; + +class KeyNotFound : public NotFound +{ +public: + KeyNotFound(const std::string & _rsSection, const std::string & _rsKey) : + m_sSection(_rsSection), + m_sKey(_rsKey) + { + } + virtual ~KeyNotFound() {} + + inline std::string sGetSection() const { return m_sSection; } + inline std::string sGetKey() const { return m_sKey; } + +private: + std::string m_sSection; + std::string m_sKey; +}; + +class Line +{ +public: + Line(const std::string & _rsKey, const std::string & _rsValue); + + std::string m_sKey; + std::string m_sValue; +}; + +class Section : private std::list +{ +public: + explicit Section(const std::string & _rsName); + + inline std::string sGetName() const { return m_sName; } + + bool bKeyExists(const std::string & _rsKey); + void vSetKey(const std::string & _rsKey, const std::string & _rsValue); + std::string sGetKey(const std::string & _rsKey) const; + void vRemoveKey(const std::string & _rsKey); + + template + void vSetKey(const std::string & _rsKey, const T & _rValue); + + template + T oGetKey(const std::string & _rsKey) const; + + // read only + typedef std::list::const_iterator const_iterator; + inline const_iterator begin() const + { + return std::list::begin(); + } + inline const_iterator end() const + { + return std::list::end(); + } + +private: + inline iterator begin() + { + return std::list::begin(); + } + inline iterator end() + { + return std::list::end(); + } + + std::string m_sName; +}; + +class File : private std::list
+{ +public: + File(); + File(const std::string & _rsFile); + virtual ~File(); + + bool bSectionExists(const std::string & _rsName); + Section * poAddSection(const std::string & _rsName); + Section * poGetSection(const std::string & _rsName); + void vRemoveSection(const std::string & _rsName); + void vLoad(const std::string & _rsFile, + bool _bAddSection = true, + bool _bAddKey = true); + void vSave(const std::string & _rsFile); + void vClear(); + + // read only + typedef std::list
::const_iterator const_iterator; + inline const_iterator begin() const + { + return std::list
::begin(); + } + inline const_iterator end() const + { + return std::list
::end(); + } + +private: + inline iterator begin() + { + return std::list
::begin(); + } + inline iterator end() + { + return std::list
::end(); + } +}; + +// debug +std::ostream & operator<<(std::ostream & _roOut, const File & _roConfig); + +template +void Section::vSetKey(const std::string & _rsKey, const T & _rValue) +{ + std::ostringstream oOut; + oOut << _rValue; + for (iterator it = begin(); it != end(); it++) + { + if (it->m_sKey == _rsKey) + { + it->m_sValue = oOut.str(); + return; + } + } + push_back(Line(_rsKey, oOut.str())); +} + +template +T Section::oGetKey(const std::string & _rsKey) const +{ + T oValue; + for (const_iterator it = begin(); it != end(); it++) + { + if (it->m_sKey == _rsKey) + { + std::istringstream oIn(it->m_sValue); + oIn >> oValue; + return oValue; + } + } + throw KeyNotFound(m_sName, _rsKey); +} + +} // namespace Config +} // namespace VBA + + +#endif // __VBA_CONFIGFILE_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/filters.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/filters.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,59 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "filters.h" + +namespace VBA +{ + +static const Filter2x apvFilters2x[][2] = +{ + { NULL, NULL }, + { _2xSaI, _2xSaI32 }, + { Super2xSaI, Super2xSaI32 }, + { SuperEagle, SuperEagle32 }, + { Pixelate2x16, Pixelate2x32 }, + { MotionBlur, MotionBlur32 }, + { AdMame2x, AdMame2x32 }, + { Simple2x16, Simple2x32 }, + { Bilinear, Bilinear32 }, + { BilinearPlus, BilinearPlus32 }, + { Scanlines, Scanlines32 }, + { ScanlinesTV, ScanlinesTV32 }, + { hq2x, hq2x32 }, + { lq2x, lq2x32 } +}; + +static const FilterIB apvFiltersIB[][2] = +{ + { NULL, NULL }, + { SmartIB, SmartIB32 }, + { MotionBlurIB, MotionBlurIB32 } +}; + +Filter2x pvGetFilter2x(EFilter2x _eFilter2x, EFilterDepth _eDepth) +{ + return apvFilters2x[_eFilter2x][_eDepth]; +} + +FilterIB pvGetFilterIB(EFilterIB _eFilterIB, EFilterDepth _eDepth) +{ + return apvFiltersIB[_eFilterIB][_eDepth]; +} + +} // namespace VBA diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/filters.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/filters.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,106 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_FILTERS_H__ +#define __VBA_FILTERS_H__ + +#include "../common/System.h" + +int Init_2xSaI(u32); + +void _2xSaI (u8 *, u32, u8 *, u8 *, u32, int, int); +void _2xSaI32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void Super2xSaI (u8 *, u32, u8 *, u8 *, u32, int, int); +void Super2xSaI32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void SuperEagle (u8 *, u32, u8 *, u8 *, u32, int, int); +void SuperEagle32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void Pixelate2x16 (u8 *, u32, u8 *, u8 *, u32, int, int); +void Pixelate2x32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void MotionBlur (u8 *, u32, u8 *, u8 *, u32, int, int); +void MotionBlur32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void AdMame2x (u8 *, u32, u8 *, u8 *, u32, int, int); +void AdMame2x32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void Simple2x16 (u8 *, u32, u8 *, u8 *, u32, int, int); +void Simple2x32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void Bilinear (u8 *, u32, u8 *, u8 *, u32, int, int); +void Bilinear32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void BilinearPlus (u8 *, u32, u8 *, u8 *, u32, int, int); +void BilinearPlus32(u8 *, u32, u8 *, u8 *, u32, int, int); +void Scanlines (u8 *, u32, u8 *, u8 *, u32, int, int); +void Scanlines32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void ScanlinesTV (u8 *, u32, u8 *, u8 *, u32, int, int); +void ScanlinesTV32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void hq2x (u8 *, u32, u8 *, u8 *, u32, int, int); +void hq2x32 (u8 *, u32, u8 *, u8 *, u32, int, int); +void lq2x (u8 *, u32, u8 *, u8 *, u32, int, int); +void lq2x32 (u8 *, u32, u8 *, u8 *, u32, int, int); + +void SmartIB (u8 *, u32, int, int); +void SmartIB32 (u8 *, u32, int, int); +void MotionBlurIB (u8 *, u32, int, int); +void MotionBlurIB32(u8 *, u32, int, int); + +namespace VBA +{ + +typedef void (*Filter2x)(u8 *, u32, u8 *, u8 *, u32, int, int); +typedef void (*FilterIB)(u8 *, u32, int, int); + +enum EFilter2x +{ + FirstFilter, + FilterNone = FirstFilter, + Filter2xSaI, + FilterSuper2xSaI, + FilterSuperEagle, + FilterPixelate, + FilterMotionBlur, + FilterAdMame2x, + FilterSimple2x, + FilterBilinear, + FilterBilinearPlus, + FilterScanlines, + FilterScanlinesTV, + FilterHq2x, + FilterLq2x, + LastFilter = FilterLq2x +}; + +enum EFilterIB +{ + FirstFilterIB, + FilterIBNone = FirstFilterIB, + FilterIBSmart, + FilterIBMotionBlur, + LastFilterIB = FilterIBMotionBlur +}; + +enum EFilterDepth +{ + FilterDepth16, + FilterDepth32 +}; + +Filter2x pvGetFilter2x(EFilter2x _eFilter2x, EFilterDepth _eDepth); +FilterIB pvGetFilterIB(EFilterIB _eFilterIB, EFilterDepth _eDepth); + +} // namespace VBA + + +#endif // __VBA_FILTERS_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/images/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/images/Makefile Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,396 @@ +# Makefile.in generated by automake 1.10.1 from Makefile.am. +# src/gtk/images/Makefile. Generated from Makefile.in by configure. + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + + + + +pkgdatadir = $(datadir)/VisualBoyAdvance +pkglibdir = $(libdir)/VisualBoyAdvance +pkgincludedir = $(includedir)/VisualBoyAdvance +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = x86_64-unknown-linux-gnu +host_triplet = x86_64-unknown-linux-gnu +target_triplet = x86_64-unknown-linux-gnu +subdir = src/gtk/images +DIST_COMMON = $(dist_pkgdata_DATA) $(srcdir)/Makefile.am \ + $(srcdir)/Makefile.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/gettext.m4 \ + $(top_srcdir)/m4/iconv.m4 $(top_srcdir)/m4/lib-ld.m4 \ + $(top_srcdir)/m4/lib-link.m4 $(top_srcdir)/m4/lib-prefix.m4 \ + $(top_srcdir)/m4/nls.m4 $(top_srcdir)/m4/po.m4 \ + $(top_srcdir)/m4/progtest.m4 $(top_srcdir)/acinclude.m4 \ + $(top_srcdir)/configure.in +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs +CONFIG_CLEAN_FILES = +SOURCES = +DIST_SOURCES = +am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; +am__vpath_adj = case $$p in \ + $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ + *) f=$$p;; \ + esac; +am__strip_dir = `echo $$p | sed -e 's|^.*/||'`; +am__installdirs = "$(DESTDIR)$(pkgdatadir)" +dist_pkgdataDATA_INSTALL = $(INSTALL_DATA) +DATA = $(dist_pkgdata_DATA) $(noinst_DATA) +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = ${SHELL} /home/r/proj/vba/trunk/missing --run aclocal-1.10 +AMTAR = ${SHELL} /home/r/proj/vba/trunk/missing --run tar +AUTOCONF = ${SHELL} /home/r/proj/vba/trunk/missing --run autoconf +AUTOHEADER = ${SHELL} /home/r/proj/vba/trunk/missing --run autoheader +AUTOMAKE = ${SHELL} /home/r/proj/vba/trunk/missing --run automake-1.10 +AWK = gawk +CC = gcc +CCDEPMODE = depmode=gcc3 +CFLAGS = -g -O2 +CPP = gcc -E +CPPFLAGS = +CXX = g++ +CXXDEPMODE = depmode=gcc3 +CXXFLAGS = -g -O2 -DC_CORE -DPROFILING -DDEV_VERSION +CYGPATH_W = echo +DEFS = -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"VisualBoyAdvance\" -DVERSION=\"1.7.2\" -DYYTEXT_POINTER=1 -DHAVE_LIBZ=1 -DHAVE_LIBPNG=1 -DHAVE_LIBPTHREAD=1 -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_MALLOC_H=1 -DHAVE_STRINGS_H=1 -DHAVE_UNISTD_H=1 -DHAVE_ARPA_INET_H=1 -DHAVE_NETINET_IN_H=1 +DEPDIR = .deps +ECHO_C = +ECHO_N = -n +ECHO_T = +EGREP = /bin/grep -E +EXEEXT = +GETTEXT_PACKAGE = +GMSGFMT = +GREP = /bin/grep +GTKMM_CFLAGS = +GTKMM_CPPFLAGS = +GTKMM_LIBS = +INSTALL = /usr/bin/install -c +INSTALL_DATA = ${INSTALL} -m 644 +INSTALL_PROGRAM = ${INSTALL} +INSTALL_SCRIPT = ${INSTALL} +INSTALL_STRIP_PROGRAM = $(install_sh) -c -s +INTLLIBS = +LDFLAGS = +LEX = flex +LEXLIB = -lfl +LEX_OUTPUT_ROOT = lex.yy +LIBICONV = +LIBINTL = +LIBOBJS = +LIBS = -lpthread -lpng -lz +LTLIBICONV = +LTLIBINTL = +LTLIBOBJS = +MAKEINFO = ${SHELL} /home/r/proj/vba/trunk/missing --run makeinfo +MKDIR_P = /bin/mkdir -p +MKINSTALLDIRS = +MSGFMT = +MSGMERGE = +NASM = /usr/bin/nasm +OBJEXT = o +PACKAGE = VisualBoyAdvance +PACKAGE_BUGREPORT = +PACKAGE_NAME = +PACKAGE_STRING = +PACKAGE_TARNAME = +PACKAGE_VERSION = +PATH_SEPARATOR = : +PKG_CONFIG = +POSUB = +RANLIB = ranlib +SDL_CFLAGS = -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT +SDL_CONFIG = /usr/bin/sdl-config +SDL_LIBS = -L/usr/lib -Wl,-rpath,/usr/lib -lSDL -lpthread +SET_MAKE = +SHELL = /bin/sh +STRIP = +USE_NLS = +VBA_EXTRA = +VBA_LIBS = ../gba/libgba.a ../gb/libgb.a ../common/libgbcom.a ../filters/libfilter.a ../lua/libgblua.a ../prof/libprof.a +VBA_SRC_EXTRA = lua prof sdl +VERSION = 1.7.2 +XGETTEXT = +XMKMF = +YACC = bison -y +YFLAGS = +abs_builddir = /home/r/proj/vba/trunk/src/gtk/images +abs_srcdir = /home/r/proj/vba/trunk/src/gtk/images +abs_top_builddir = /home/r/proj/vba/trunk +abs_top_srcdir = /home/r/proj/vba/trunk +ac_ct_CC = gcc +ac_ct_CXX = g++ +am__include = include +am__leading_dot = . +am__quote = +am__tar = ${AMTAR} chof - "$$tardir" +am__untar = ${AMTAR} xf - +bindir = ${exec_prefix}/bin +build = x86_64-unknown-linux-gnu +build_alias = +build_cpu = x86_64 +build_os = linux-gnu +build_vendor = unknown +builddir = . +datadir = ${datarootdir} +datarootdir = ${prefix}/share +docdir = ${datarootdir}/doc/${PACKAGE} +dvidir = ${docdir} +exec_prefix = ${prefix} +host = x86_64-unknown-linux-gnu +host_alias = +host_cpu = x86_64 +host_os = linux-gnu +host_vendor = unknown +htmldir = ${docdir} +includedir = ${prefix}/include +infodir = ${datarootdir}/info +install_sh = $(SHELL) /home/r/proj/vba/trunk/install-sh +libdir = ${exec_prefix}/lib +libexecdir = ${exec_prefix}/libexec +localedir = ${datarootdir}/locale +localstatedir = ${prefix}/var +mandir = ${datarootdir}/man +mkdir_p = /bin/mkdir -p +oldincludedir = /usr/include +pdfdir = ${docdir} +prefix = /usr/local +program_transform_name = s,x,x, +psdir = ${docdir} +sbindir = ${exec_prefix}/sbin +sharedstatedir = ${prefix}/com +srcdir = . +sysconfdir = ${prefix}/etc +target = x86_64-unknown-linux-gnu +target_alias = +target_cpu = x86_64 +target_os = linux-gnu +target_vendor = unknown +top_builddir = ../../.. +top_srcdir = ../../.. +VBA_WM_IMAGES = \ + stock-vba-wm-16.png \ + stock-vba-wm-32.png \ + stock-vba-wm-48.png \ + stock-vba-wm-64.png + +VBA_WM_VARIABLES = \ + stock_vba_wm_16 $(srcdir)/stock-vba-wm-16.png \ + stock_vba_wm_32 $(srcdir)/stock-vba-wm-32.png \ + stock_vba_wm_48 $(srcdir)/stock-vba-wm-48.png \ + stock_vba_wm_64 $(srcdir)/stock-vba-wm-64.png + +EXTRA_DIST = $(VBA_WM_IMAGES) +noinst_DATA = vba-wm-pixbufs.h +dist_pkgdata_DATA = vba-64.png +CLEANFILES = $(noinst_DATA) +all: all-am + +.SUFFIXES: +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \ + && exit 0; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/gtk/images/Makefile'; \ + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu src/gtk/images/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +install-dist_pkgdataDATA: $(dist_pkgdata_DATA) + @$(NORMAL_INSTALL) + test -z "$(pkgdatadir)" || $(MKDIR_P) "$(DESTDIR)$(pkgdatadir)" + @list='$(dist_pkgdata_DATA)'; for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + f=$(am__strip_dir) \ + echo " $(dist_pkgdataDATA_INSTALL) '$$d$$p' '$(DESTDIR)$(pkgdatadir)/$$f'"; \ + $(dist_pkgdataDATA_INSTALL) "$$d$$p" "$(DESTDIR)$(pkgdatadir)/$$f"; \ + done + +uninstall-dist_pkgdataDATA: + @$(NORMAL_UNINSTALL) + @list='$(dist_pkgdata_DATA)'; for p in $$list; do \ + f=$(am__strip_dir) \ + echo " rm -f '$(DESTDIR)$(pkgdatadir)/$$f'"; \ + rm -f "$(DESTDIR)$(pkgdatadir)/$$f"; \ + done +tags: TAGS +TAGS: + +ctags: CTAGS +CTAGS: + + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ + fi; \ + cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ + else \ + test -f $(distdir)/$$file \ + || cp -p $$d/$$file $(distdir)/$$file \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(DATA) +installdirs: + for dir in "$(DESTDIR)$(pkgdatadir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic mostlyclean-am + +distclean: distclean-am + -rm -f Makefile +distclean-am: clean-am distclean-generic + +dvi: dvi-am + +dvi-am: + +html: html-am + +info: info-am + +info-am: + +install-data-am: install-dist_pkgdataDATA + +install-dvi: install-dvi-am + +install-exec-am: + +install-html: install-html-am + +install-info: install-info-am + +install-man: + +install-pdf: install-pdf-am + +install-ps: install-ps-am + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-generic + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-dist_pkgdataDATA + +.MAKE: install-am install-strip + +.PHONY: all all-am check check-am clean clean-generic distclean \ + distclean-generic distdir dvi dvi-am html html-am info info-am \ + install install-am install-data install-data-am \ + install-dist_pkgdataDATA install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-generic pdf \ + pdf-am ps ps-am uninstall uninstall-am \ + uninstall-dist_pkgdataDATA + + +$(srcdir)/vba-wm-pixbufs.h: $(VBA_WM_IMAGES) Makefile.am + gdk-pixbuf-csource --raw --build-list $(VBA_WM_VARIABLES) > $(@F) +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/images/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/images/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,22 @@ +VBA_WM_IMAGES = \ + stock-vba-wm-16.png \ + stock-vba-wm-32.png \ + stock-vba-wm-48.png \ + stock-vba-wm-64.png + +VBA_WM_VARIABLES = \ + stock_vba_wm_16 $(srcdir)/stock-vba-wm-16.png \ + stock_vba_wm_32 $(srcdir)/stock-vba-wm-32.png \ + stock_vba_wm_48 $(srcdir)/stock-vba-wm-48.png \ + stock_vba_wm_64 $(srcdir)/stock-vba-wm-64.png + +EXTRA_DIST = $(VBA_WM_IMAGES) + +noinst_DATA = vba-wm-pixbufs.h + +dist_pkgdata_DATA = vba-64.png + +CLEANFILES = $(noinst_DATA) + +$(srcdir)/vba-wm-pixbufs.h: $(VBA_WM_IMAGES) Makefile.am + gdk-pixbuf-csource --raw --build-list $(VBA_WM_VARIABLES) > $(@F) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/images/stock-vba-wm-16.png Binary file src/gtk/images/stock-vba-wm-16.png has changed diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/images/stock-vba-wm-32.png Binary file src/gtk/images/stock-vba-wm-32.png has changed diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/images/stock-vba-wm-48.png Binary file src/gtk/images/stock-vba-wm-48.png has changed diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/images/stock-vba-wm-64.png Binary file src/gtk/images/stock-vba-wm-64.png has changed diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/images/vba-64.png Binary file src/gtk/images/vba-64.png has changed diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/input.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/input.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,53 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "input.h" + +#include + +namespace VBA +{ + +Keymap::Keymap() +{ + m_pstTable = g_hash_table_new(g_direct_hash, g_direct_equal); + if (m_pstTable == NULL) + { + throw std::bad_alloc(); + } +} + +Keymap::~Keymap() +{ + g_hash_table_destroy(m_pstTable); +} + +void Keymap::vRegister(guint _uiVal, EKey _eKey) +{ + g_hash_table_insert(m_pstTable, + GUINT_TO_POINTER(_uiVal), + GUINT_TO_POINTER(_eKey)); +} + +void Keymap::vClear() +{ + g_hash_table_destroy(m_pstTable); + m_pstTable = g_hash_table_new(g_direct_hash, g_direct_equal); +} + +} // namespace VBA diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/input.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/input.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,92 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_INPUT_H__ +#define __VBA_INPUT_H__ + +#include + +namespace VBA +{ + +enum EKey +{ + KeyNone, + // GBA keys + KeyA, + KeyB, + KeySelect, + KeyStart, + KeyRight, + KeyLeft, + KeyUp, + KeyDown, + KeyR, + KeyL, + // VBA extension + KeySpeed, + KeyCapture +}; + +enum EKeyFlag +{ + // GBA keys + KeyFlagA = 1 << 0, + KeyFlagB = 1 << 1, + KeyFlagSelect = 1 << 2, + KeyFlagStart = 1 << 3, + KeyFlagRight = 1 << 4, + KeyFlagLeft = 1 << 5, + KeyFlagUp = 1 << 6, + KeyFlagDown = 1 << 7, + KeyFlagR = 1 << 8, + KeyFlagL = 1 << 9, + // VBA extension + KeyFlagSpeed = 1 << 10, + KeyFlagCapture = 1 << 11, +}; + +class Keymap +{ + public: + Keymap(); + ~Keymap(); + + void vRegister(guint _uiVal, EKey _eKey); + void vClear(); + inline EKey eGetKey(guint _uiVal); + + private: + GHashTable * m_pstTable; + + // noncopyable + Keymap(const Keymap &); + Keymap & operator=(const Keymap &); +}; + +inline EKey Keymap::eGetKey(guint _uiVal) +{ + return (EKey)GPOINTER_TO_UINT(g_hash_table_lookup(m_pstTable, + GUINT_TO_POINTER(_uiVal))); +} + +} // namespace VBA + + +#endif // __VBA_INPUT_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/intl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/intl.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,42 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_INTL_H__ +#define __VBA_INTL_H__ + +#ifndef GETTEXT_PACKAGE +# error "GETTEXT_PACKAGE must be defined" +#endif + +#ifdef ENABLE_NLS +# include +# define _(String) gettext(String) +# define N_(String) (String) +#else +# define _(String) (String) +# define N_(String) (String) +# define textdomain(String) (String) +# define gettext(String) (String) +# define dgettext(Domain,String) (String) +# define dcgettext(Domain,String,Type) (String) +# define bindtextdomain(Domain,Directory) (Domain) +#endif + + +#endif // __VBA_INTL_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/joypadconfig.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/joypadconfig.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,276 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "joypadconfig.h" + +#include + +#include "intl.h" + +namespace VBA +{ + +guint * JoypadConfig::puiAt(int _iIndex) +{ + guint * puiMember; + + switch (_iIndex) + { + case 0: + puiMember = &m_uiUp; + break; + case 1: + puiMember = &m_uiDown; + break; + case 2: + puiMember = &m_uiLeft; + break; + case 3: + puiMember = &m_uiRight; + break; + case 4: + puiMember = &m_uiA; + break; + case 5: + puiMember = &m_uiB; + break; + case 6: + puiMember = &m_uiL; + break; + case 7: + puiMember = &m_uiR; + break; + case 8: + puiMember = &m_uiSelect; + break; + case 9: + puiMember = &m_uiStart; + break; + case 10: + puiMember = &m_uiSpeed; + break; + case 11: + puiMember = &m_uiCapture; + break; + default: + puiMember = NULL; + } + + return puiMember; +} + +int JoypadConfig::iFind(guint _uiKeycode) +{ + for (guint i = 0; i < 12; i++) + { + if (*puiAt(i) == _uiKeycode) + { + return i; + } + } + + return -1; +} + +void JoypadConfig::vSetDefault() +{ + guint auiKeyval[] = + { + GDK_Up, GDK_Down, GDK_Left, GDK_Right, + GDK_z, GDK_x, GDK_a, GDK_s, + GDK_BackSpace, GDK_Return, + GDK_space, GDK_F12 + }; + + for (guint i = 0; i < G_N_ELEMENTS(auiKeyval); i++) + { + GdkKeymapKey * pstKeys; + int iKeys; + + if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), + auiKeyval[i], + &pstKeys, + &iKeys)) + { + *puiAt(i) = pstKeys[0].keycode; + g_free(pstKeys); + } + else + { + *puiAt(i) = 0; + } + } +} + +Keymap * JoypadConfig::poCreateKeymap() const +{ + Keymap * poKeymap = new Keymap(); + + poKeymap->vRegister(m_uiUp, KeyUp ); + poKeymap->vRegister(m_uiDown, KeyDown ); + poKeymap->vRegister(m_uiLeft, KeyLeft ); + poKeymap->vRegister(m_uiRight, KeyRight ); + poKeymap->vRegister(m_uiA, KeyA ); + poKeymap->vRegister(m_uiB, KeyB ); + poKeymap->vRegister(m_uiL, KeyL ); + poKeymap->vRegister(m_uiR, KeyR ); + poKeymap->vRegister(m_uiSelect, KeySelect ); + poKeymap->vRegister(m_uiStart, KeyStart ); + poKeymap->vRegister(m_uiSpeed, KeySpeed ); + poKeymap->vRegister(m_uiCapture, KeyCapture ); + + return poKeymap; +} + +JoypadConfigDialog::JoypadConfigDialog(GtkDialog * _pstDialog, + const Glib::RefPtr & _poXml) : + Gtk::Dialog(_pstDialog) +{ + m_puiCurrentKeyCode = NULL; + + memset(&m_oConfig, 0, sizeof(m_oConfig)); + + m_poOkButton = dynamic_cast(_poXml->get_widget("JoypadOkButton")); + + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadUpEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadDownEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadLeftEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadRightEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadAEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadBEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadLEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadREntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadSelectEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadStartEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadSpeedEntry"))); + m_oEntries.push_back(dynamic_cast(_poXml->get_widget("JoypadCaptureEntry"))); + + for (guint i = 0; i < m_oEntries.size(); i++) + { + Gtk::Entry * poEntry = m_oEntries[i]; + + poEntry->signal_focus_in_event().connect(SigC::bind( + SigC::slot(*this, &JoypadConfigDialog::bOnEntryFocusIn), + i)); + poEntry->signal_focus_out_event().connect(SigC::slot(*this, &JoypadConfigDialog::bOnEntryFocusOut)); + } + + vUpdateEntries(); +} + +JoypadConfigDialog::~JoypadConfigDialog() +{ +} + +void JoypadConfigDialog::vSetConfig(const JoypadConfig & _roConfig) +{ + m_oConfig = _roConfig; + vUpdateEntries(); +} + +void JoypadConfigDialog::vUpdateEntries() +{ + for (guint i = 0; i < m_oEntries.size(); i++) + { + guint uiKeyval = 0; + gdk_keymap_translate_keyboard_state(gdk_keymap_get_default(), + *m_oConfig.puiAt(i), + (GdkModifierType)0, + 0, + &uiKeyval, + NULL, + NULL, + NULL); + const char * csName = gdk_keyval_name(uiKeyval); + if (csName == NULL) + { + m_oEntries[i]->set_text(_("")); + } + else + { + m_oEntries[i]->set_text(csName); + } + } +} + +bool JoypadConfigDialog::bOnEntryFocusIn(GdkEventFocus * _pstEvent, + guint _uiEntry) +{ + m_uiCurrentEntry = _uiEntry; + m_puiCurrentKeyCode = m_oConfig.puiAt(_uiEntry); + + return false; +} + +bool JoypadConfigDialog::bOnEntryFocusOut(GdkEventFocus * _pstEvent) +{ + m_puiCurrentKeyCode = NULL; + + return false; +} + +bool JoypadConfigDialog::on_key_press_event(GdkEventKey * _pstEvent) +{ + if (m_puiCurrentKeyCode == NULL) + { + return Gtk::Dialog::on_key_press_event(_pstEvent); + } + + *m_puiCurrentKeyCode = 0; + int iFound = m_oConfig.iFind(_pstEvent->hardware_keycode); + if (iFound >= 0) + { + *m_oConfig.puiAt(iFound) = 0; + m_oEntries[iFound]->set_text(_("")); + } + + *m_puiCurrentKeyCode = _pstEvent->hardware_keycode; + + guint uiKeyval = 0; + gdk_keymap_translate_keyboard_state(gdk_keymap_get_default(), + _pstEvent->hardware_keycode, + (GdkModifierType)0, + 0, + &uiKeyval, + NULL, + NULL, + NULL); + + const char * csName = gdk_keyval_name(uiKeyval); + if (csName == NULL) + { + m_oEntries[m_uiCurrentEntry]->set_text(_("")); + } + else + { + m_oEntries[m_uiCurrentEntry]->set_text(csName); + } + + if (m_uiCurrentEntry + 1 < m_oEntries.size()) + { + m_oEntries[m_uiCurrentEntry + 1]->grab_focus(); + } + else + { + m_poOkButton->grab_focus(); + } + + return true; +} + +} // namespace VBA diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/joypadconfig.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/joypadconfig.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,88 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_JOYPADCONFIG_H__ +#define __VBA_JOYPADCONFIG_H__ + +#include + +#include +#include + +#ifndef GTKMM20 +# include "sigccompat.h" +#endif // ! GTKMM20 + +#include "input.h" + +namespace VBA +{ + +class JoypadConfig +{ +public: + guint m_uiUp; + guint m_uiDown; + guint m_uiLeft; + guint m_uiRight; + guint m_uiA; + guint m_uiB; + guint m_uiL; + guint m_uiR; + guint m_uiSelect; + guint m_uiStart; + guint m_uiSpeed; + guint m_uiCapture; + + guint * puiAt(int _iIndex); + int iFind(guint _uiKeycode); + void vSetDefault(); + Keymap * poCreateKeymap() const; +}; + +class JoypadConfigDialog : public Gtk::Dialog +{ +public: + JoypadConfigDialog(GtkDialog * _pstDialog, + const Glib::RefPtr & _poXml); + virtual ~JoypadConfigDialog(); + + void vSetConfig(const JoypadConfig & _roConfig); + inline JoypadConfig stGetConfig() const { return m_oConfig; } + +protected: + bool bOnEntryFocusIn(GdkEventFocus * _pstEvent, guint _uiEntry); + bool bOnEntryFocusOut(GdkEventFocus * _pstEvent); + + bool on_key_press_event(GdkEventKey * _pstEvent); + +private: + JoypadConfig m_oConfig; + Gtk::Button * m_poOkButton; + std::vector m_oEntries; + guint * m_puiCurrentKeyCode; + guint m_uiCurrentEntry; + + void vUpdateEntries(); +}; + +} // namespace VBA + + +#endif // __VBA_JOYPADCONFIG_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/main.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,177 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include "../getopt.h" + +#include + +#include +#include +#include +#include + +#include "images/vba-wm-pixbufs.h" + +#include "window.h" +#include "intl.h" + +using Gnome::Glade::Xml; + +static const char * csProgramName; + +static int iShowHelp; +static int iShowVersion; + +// Non-characters used for long options that have no equivalent short option +enum +{ + IGNORED_OPTION = CHAR_MAX + 1 +}; + +static const char csShortOptions[] = "V"; + +static const struct option astLongOptions[] = +{ + { "help", no_argument, &iShowHelp, IGNORED_OPTION }, + { "version", no_argument, NULL, 'V' }, + { 0, 0, 0, 0 } +}; + +static void vUsage(int iStatus) +{ + if (iStatus != 0) + { + g_printerr(_("Try `%s --help' for more information.\n"), csProgramName); + } + else + { + g_print(_("Usage: %s [option ...] [file]\n"), csProgramName); + g_print(_("\ +\n\ +Options:\n\ + --help Output this help.\n\ + -V, --version Output version information.\n\ +")); + } + + exit(iStatus); +} + +static void vSetDefaultWindowIcon() +{ + const guint8 * apuiInlinePixbuf[] = + { + stock_vba_wm_16, + stock_vba_wm_32, + stock_vba_wm_48, + stock_vba_wm_64 + }; + + std::list > listPixbuf; + for (guint i = 0; i < G_N_ELEMENTS(apuiInlinePixbuf); i++) + { + listPixbuf.push_back( + Gdk::Pixbuf::create_from_inline(-1, apuiInlinePixbuf[i])); + } + + Gtk::Window::set_default_icon_list(listPixbuf); +} + +int main(int argc, char * argv[]) +{ + csProgramName = argv[0]; + +#ifdef ENABLE_NLS + setlocale(LC_ALL, ""); + bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); + textdomain(GETTEXT_PACKAGE); + bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); +#endif // ENABLE_NLS + + Gtk::Main oKit(argc, argv); + + int iOpt; + while ((iOpt = getopt_long(argc, argv, csShortOptions, astLongOptions, NULL)) + != -1) + { + switch (iOpt) + { + case 'V': + iShowVersion = 1; + break; + case 0: + // Long options + break; + default: + vUsage(1); + break; + } + } + + if (iShowVersion) + { + g_print(_("VisualBoyAdvance version %s [GTK+]\n"), VERSION); + exit(0); + } + + if (iShowHelp) + { + vUsage(0); + } + + vSetDefaultWindowIcon(); + + Glib::RefPtr poXml; + try + { + poXml = Xml::create(PKGDATADIR "/vba.glade", "MainWindow"); + } + catch (const Xml::Error & e) + { + Gtk::MessageDialog oDialog(e.what(), +#ifndef GTKMM20 + false, +#endif // ! GTKMM20 + Gtk::MESSAGE_ERROR, + Gtk::BUTTONS_OK); + oDialog.run(); + return 1; + } + + VBA::Window * poWindow = NULL; + poXml->get_widget_derived("MainWindow", poWindow); + + if (optind < argc) + { + // Display the window before loading the file + poWindow->show(); + while (Gtk::Main::events_pending()) + { + Gtk::Main::iteration(); + } + + poWindow->bLoadROM(argv[optind]); + } + + Gtk::Main::run(*poWindow); + delete poWindow; + + return 0; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/menuitem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/menuitem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,80 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_MENUITEM_H__ +#define __VBA_MENUITEM_H__ + +#include +#include + +#ifdef GTKMM20 +namespace Gtk { typedef Gtk::Menu_Helpers::AccelKey AccelKey; } +#endif // GTKMM20 + +namespace VBA +{ + +class MenuItem : public Gtk::MenuItem +{ +public: + MenuItem() + {} + + MenuItem(Gtk::Widget & _roWidget) : + Gtk::MenuItem(_roWidget) + {} + + MenuItem(const Glib::ustring & _rsLabel, bool _bMnemonic = false) : + Gtk::MenuItem(_rsLabel, _bMnemonic) + {} + + inline void set_accel_key(const Gtk::AccelKey & _roAccelKey) + { + Gtk::MenuItem::set_accel_key(_roAccelKey); + } +}; + +class ImageMenuItem : public Gtk::ImageMenuItem +{ +public: + ImageMenuItem() + {} + + ImageMenuItem(Widget & _roImage, const Glib::ustring & _rsLabel, bool _bMnemonic = false) : + Gtk::ImageMenuItem(_roImage, _rsLabel, _bMnemonic) + {} + + ImageMenuItem(const Glib::ustring & _rsLabel, bool _bMnemonic = false) : + Gtk::ImageMenuItem(_rsLabel, _bMnemonic) + {} + + ImageMenuItem(const Gtk::StockID & _roId) : + Gtk::ImageMenuItem(_roId) + {} + + inline void set_accel_key(const Gtk::AccelKey & _roAccelKey) + { + Gtk::MenuItem::set_accel_key(_roAccelKey); + } +}; + +} // namespace VBA + + +#endif // __VBA_MENUITEM_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/screenarea.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/screenarea.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,293 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "screenarea.h" + +#include + +namespace VBA +{ + +ScreenArea::ScreenArea(int _iWidth, int _iHeight, int _iScale) : + m_puiPixels(NULL), + m_puiDelta(NULL), + m_vFilter2x(NULL), + m_vFilterIB(NULL), + m_bShowCursor(true) +{ + g_assert(_iWidth >= 1 && _iHeight >= 1 && _iScale >= 1); + + m_iWidth = _iWidth; + m_iHeight = _iHeight; + m_iScale = _iScale; + vUpdateSize(); + + set_events(Gdk::EXPOSURE_MASK + | Gdk::POINTER_MOTION_MASK + | Gdk::ENTER_NOTIFY_MASK + | Gdk::LEAVE_NOTIFY_MASK); + + char aiEmptyData[8]; + memset(aiEmptyData, 0, sizeof(aiEmptyData)); + Glib::RefPtr poSource = Gdk::Bitmap::create(aiEmptyData, 8, 8); + Glib::RefPtr poMask = Gdk::Bitmap::create(aiEmptyData, 8, 8); + Gdk::Color oFg; + Gdk::Color oBg; + oFg.set_rgb(0, 0, 0); + oBg.set_rgb(0, 0, 0); + + m_poEmptyCursor = new Gdk::Cursor(poSource, poMask, oFg, oBg, 0, 0); +} + +ScreenArea::~ScreenArea() +{ + if (m_puiPixels != NULL) + { + delete[] m_puiPixels; + } + + if (m_puiDelta != NULL) + { + delete[] m_puiDelta; + } + + if (m_poEmptyCursor != NULL) + { + delete m_poEmptyCursor; + } +} + +void ScreenArea::vSetSize(int _iWidth, int _iHeight) +{ + g_return_if_fail(_iWidth >= 1 && _iHeight >= 1); + + if (_iWidth != m_iWidth || _iHeight != m_iHeight) + { + m_iWidth = _iWidth; + m_iHeight = _iHeight; + vUpdateSize(); + } +} + +void ScreenArea::vSetScale(int _iScale) +{ + g_return_if_fail(_iScale >= 1); + + if (_iScale != m_iScale) + { + m_iScale = _iScale; + vUpdateSize(); + } +} + +void ScreenArea::vSetFilter2x(EFilter2x _eFilter2x) +{ + m_vFilter2x = pvGetFilter2x(_eFilter2x, FilterDepth32); +} + +void ScreenArea::vSetFilterIB(EFilterIB _eFilterIB) +{ + m_vFilterIB = pvGetFilterIB(_eFilterIB, FilterDepth32); +} + +void ScreenArea::vDrawPixels(u8 * _puiData) +{ + if (m_vFilterIB != NULL) + { + m_vFilterIB(_puiData + m_iAreaWidth * 2 + 4, + m_iAreaWidth * 2 + 4, + m_iWidth, + m_iHeight); + } + + if (m_iScale == 1) + { + u32 * puiSrc = (u32 *)_puiData + m_iWidth + 1; + u32 * puiPixel = m_puiPixels; + for (int y = 0; y < m_iHeight; y++) + { + for (int x = 0; x < m_iWidth; x++) + { + *puiPixel++ = *puiSrc++; + } + puiSrc++; + } + } + else if (m_iScale == 2 && m_vFilter2x != NULL) + { + m_vFilter2x(_puiData + m_iAreaWidth * 2 + 4, + m_iAreaWidth * 2 + 4, + m_puiDelta, + (u8 *)m_puiPixels, + m_iRowStride, + m_iWidth, + m_iHeight); + } + else + { + u32 * puiSrc = (u32 *)_puiData + m_iWidth + 1; + u32 * puiSrc2; + u32 * puiPixel = m_puiPixels; + for (int y = 0; y < m_iHeight; y++) + { + for (int j = 0; j < m_iScale; j++) + { + puiSrc2 = puiSrc; + for (int x = 0; x < m_iWidth; x++) + { + for (int i = 0; i < m_iScale; i++) + { + *puiPixel++ = *puiSrc2; + } + puiSrc2++; + } + } + puiSrc = puiSrc2 + 1; + } + } + + queue_draw_area(0, 0, m_iAreaWidth, m_iAreaHeight); +} + +void ScreenArea::vDrawColor(u32 _uiColor) +{ + _uiColor = GUINT32_TO_BE(_uiColor) << 8; + + u32 * puiPixel = m_puiPixels; + u32 * puiEnd = m_puiPixels + m_iAreaWidth * m_iAreaHeight; + while (puiPixel != puiEnd) + { + *puiPixel++ = _uiColor; + } + + queue_draw_area(0, 0, m_iAreaWidth, m_iAreaHeight); +} + +void ScreenArea::vUpdateSize() +{ + if (m_puiPixels != NULL) + { + delete[] m_puiPixels; + } + + if (m_puiDelta != NULL) + { + delete[] m_puiDelta; + } + + m_iAreaWidth = m_iScale * m_iWidth; + m_iAreaHeight = m_iScale * m_iHeight; + m_iRowStride = m_iAreaWidth * 4; + + m_puiPixels = new u32[m_iAreaWidth * m_iAreaHeight]; + + m_puiDelta = new u8[(m_iWidth + 2) * (m_iHeight + 2) * 4]; + memset(m_puiDelta, 255, (m_iWidth + 2) * (m_iHeight + 2) * 4); + + set_size_request(m_iAreaWidth, m_iAreaHeight); +} + +void ScreenArea::vStartCursorTimeout() +{ + m_oCursorSig.disconnect(); + m_oCursorSig = Glib::signal_timeout().connect( + SigC::slot(*this, &ScreenArea::bOnCursorTimeout), + 2000); +} + +void ScreenArea::vStopCursorTimeout() +{ + m_oCursorSig.disconnect(); +} + +void ScreenArea::vHideCursor() +{ + get_window()->set_cursor(*m_poEmptyCursor); + m_bShowCursor = false; +} + +void ScreenArea::vShowCursor() +{ + get_window()->set_cursor(); + m_bShowCursor = true; +} + +bool ScreenArea::on_expose_event(GdkEventExpose * _pstEvent) +{ + if (_pstEvent->area.x + _pstEvent->area.width > m_iAreaWidth + || _pstEvent->area.y + _pstEvent->area.height > m_iAreaHeight) + { + return false; + } + + guchar * puiAreaPixels = (guchar *)m_puiPixels; + + if (_pstEvent->area.x != 0) + { + puiAreaPixels += _pstEvent->area.x << 2; + } + + if (_pstEvent->area.y != 0) + { + puiAreaPixels += _pstEvent->area.y * m_iRowStride; + } + + get_window()->draw_rgb_32_image(get_style()->get_fg_gc(get_state()), + _pstEvent->area.x, + _pstEvent->area.y, + _pstEvent->area.width, + _pstEvent->area.height, + Gdk::RGB_DITHER_MAX, + puiAreaPixels, + m_iRowStride); + return true; +} + +bool ScreenArea::on_motion_notify_event(GdkEventMotion * _pstEvent) +{ + if (! m_bShowCursor) + { + vShowCursor(); + } + vStartCursorTimeout(); + return false; +} + +bool ScreenArea::on_enter_notify_event(GdkEventCrossing * _pstEvent) +{ + vStartCursorTimeout(); + return false; +} + +bool ScreenArea::on_leave_notify_event(GdkEventCrossing * _pstEvent) +{ + vStopCursorTimeout(); + if (! m_bShowCursor) + { + vShowCursor(); + } + return false; +} + +bool ScreenArea::bOnCursorTimeout() +{ + vHideCursor(); + return false; +} + +} // namespace VBA diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/screenarea.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/screenarea.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,81 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_SCREENAREA_H__ +#define __VBA_SCREENAREA_H__ + +#include +#include + +#ifndef GTKMM20 +# include "sigccompat.h" +#endif // ! GTKMM20 + +#include "filters.h" + +namespace VBA +{ + +class ScreenArea : public Gtk::DrawingArea +{ +public: + ScreenArea(int _iWidth, int _iHeight, int _iScale = 1); + virtual ~ScreenArea(); + + void vSetSize(int _iWidth, int _iHeight); + void vSetScale(int _iScale); + void vSetFilter2x(EFilter2x _eFilter2x); + void vSetFilterIB(EFilterIB _eFilterIB); + void vDrawPixels(u8 * _puiData); + void vDrawColor(u32 _uiColor); // 0xRRGGBB + +protected: + virtual bool on_expose_event(GdkEventExpose * _pstEvent); + virtual bool on_motion_notify_event(GdkEventMotion * _pstEvent); + virtual bool on_enter_notify_event(GdkEventCrossing * _pstEvent); + virtual bool on_leave_notify_event(GdkEventCrossing * _pstEvent); + virtual bool bOnCursorTimeout(); + +private: + int m_iWidth; + int m_iHeight; + int m_iScale; + int m_iAreaWidth; + int m_iAreaHeight; + int m_iRowStride; + u32 * m_puiPixels; + u8 * m_puiDelta; + Filter2x m_vFilter2x; + FilterIB m_vFilterIB; + + bool m_bShowCursor; + Gdk::Cursor * m_poEmptyCursor; + SigC::connection m_oCursorSig; + + void vUpdateSize(); + void vStartCursorTimeout(); + void vStopCursorTimeout(); + void vHideCursor(); + void vShowCursor(); +}; + +} // namespace VBA + + +#endif // __VBA_SCREENAREA_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/sigccompat.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/sigccompat.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,57 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_SIGCCOMPAT_H__ +#define __VBA_SIGCCOMPAT_H__ + +#undef LIBSIGC_DISABLE_DEPRECATED +#include +#include + +#include +#include +#include + +namespace SigC +{ + +using namespace sigc; + +template +inline slot0 +slot( T_obj1& _A_obj, T_return (T_obj2::*_A_func)() ) +{ return ::sigc::bound_mem_functor0 + (dynamic_cast< T_obj1&>(_A_obj), _A_func); } + +template +inline slot1 +slot( T_obj1& _A_obj, T_return (T_obj2::*_A_func)(T_arg1) ) +{ return ::sigc::bound_mem_functor1 + (dynamic_cast< T_obj1&>(_A_obj), _A_func); } + +template +inline slot2 +slot( T_obj1& _A_obj, T_return (T_obj2::*_A_func)(T_arg1,T_arg2) ) +{ return ::sigc::bound_mem_functor2 + (dynamic_cast< T_obj1&>(_A_obj), _A_func); } + +} // namespace SigC + + +#endif // __VBA_SIGCCOMPAT_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/system.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/system.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,469 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include + +#include +#include + +#include "../common/System.h" +#include "../common/Util.h" +#include "../gba/GBAGlobals.h" +#include "../gb/gbGlobals.h" +#include "../gba/GBASound.h" + +#include "window.h" +#include "intl.h" + +// Required vars, used by the emulator core +// +int systemRedShift; +int systemGreenShift; +int systemBlueShift; +int systemColorDepth; +int systemDebug; +int systemVerbose; +int systemSaveUpdateCounter; +int systemFrameSkip; +u32 systemColorMap32[0x10000]; +u16 systemColorMap16[0x10000]; +u16 systemGbPalette[24]; +bool systemSoundOn; + +char filename[2048]; +char biosFileName[2048]; +//char captureDir[2048]; +char saveDir[2048]; +char batteryDir[2048]; + +int sensorX = 2047; +int sensorY = 2047; +bool sensorOn = false; + +int emulating; +bool debugger; +int RGB_LOW_BITS_MASK; + +int cartridgeType = 3; +int sizeOption = 0; +int captureFormat = 0; +int throttle = 0; +bool paused = false; +bool removeIntros = false; +int sdlFlashSize = 0; +int sdlRtcEnable = 0; + +int sdlDefaultJoypad = 0; + +SDL_Joystick **sdlDevices = NULL; + +u16 motion[4] = { + SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2 +}; + +struct EmulatedSystem emulator = { + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + false, + 0 +}; + +// Extra vars, only used for the GUI +// +int systemRenderedFrames; +int systemFPS; + +// Sound stuff +// +const int iSoundSamples = 2048; +const int iSoundTotalLen = iSoundSamples * 4; +static u8 auiSoundBuffer[iSoundTotalLen]; +static int iSoundLen; +static SDL_cond * pstSoundCond; +static SDL_mutex * pstSoundMutex; + +inline VBA::Window * GUI() +{ + return VBA::Window::poGetInstance(); +} + +void systemMessage(int _iId, const char * _csFormat, ...) +{ + va_list args; + va_start(args, _csFormat); + + GUI()->vPopupErrorV(_(_csFormat), args); + + va_end(args); +} + +void systemDrawScreen() +{ + GUI()->vDrawScreen(); + systemRenderedFrames++; +} + +bool systemReadJoypads() +{ + return true; +} + +u32 systemReadJoypad(int,bool) +{ + return GUI()->uiReadJoypad(); +} + +void systemShowSpeed(int _iSpeed) +{ + systemFPS = systemRenderedFrames; + systemRenderedFrames = 0; + + GUI()->vShowSpeed(_iSpeed); +} + +void system10Frames(int _iRate) +{ + GUI()->vComputeFrameskip(_iRate); +} + +void systemFrame(int) +{ +} + +void systemSetTitle(const char * _csTitle) +{ + GUI()->set_title(_csTitle); +} + +int systemScreenCapture(int _iNum) +{ + GUI()->vCaptureScreen(_iNum); +} + +void systemSoundWriteToBuffer() +{ + if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING) + { + SDL_PauseAudio(0); + } + + bool bWait = true; + while (bWait && ! speedup && GUI()->iGetThrottle() == 0) + { + SDL_mutexP(pstSoundMutex); + if (iSoundLen < iSoundTotalLen) + { + bWait = false; + } + SDL_mutexV(pstSoundMutex); + } + + int iLen = soundBufferLen; + int iCopied = 0; + if (iSoundLen + iLen >= iSoundTotalLen) + { + iCopied = iSoundTotalLen - iSoundLen; + memcpy(&auiSoundBuffer[iSoundLen], soundFinalWave, iCopied); + + iSoundLen = iSoundTotalLen; + SDL_CondSignal(pstSoundCond); + + bWait = true; + if (! speedup && GUI()->iGetThrottle() == 0) + { + while(bWait) + { + SDL_mutexP(pstSoundMutex); + if (iSoundLen < iSoundTotalLen) + { + bWait = false; + } + SDL_mutexV(pstSoundMutex); + } + + memcpy(auiSoundBuffer, ((u8 *)soundFinalWave) + iCopied, + soundBufferLen - iCopied); + + iSoundLen = soundBufferLen - iCopied; + } + else + { + memcpy(auiSoundBuffer, ((u8 *)soundFinalWave) + iCopied, + soundBufferLen); + } + } + else + { + memcpy(&auiSoundBuffer[iSoundLen], soundFinalWave, soundBufferLen); + iSoundLen += soundBufferLen; + } +} + +static void vSoundCallback(void * _pvUserData, u8 * _puiStream, int _iLen) +{ + if (! emulating) + { + return; + } + + SDL_mutexP(pstSoundMutex); + if (! speedup && GUI()->iGetThrottle() == 0) + { + while (iSoundLen < iSoundTotalLen && emulating) + { + SDL_CondWait(pstSoundCond, pstSoundMutex); + } + } + if (emulating) + { + memcpy(_puiStream, auiSoundBuffer, _iLen); + } + iSoundLen = 0; + SDL_mutexV(pstSoundMutex); +} + +bool systemSoundInit() +{ + SDL_AudioSpec stAudio; + + switch (soundQuality) + { + case 1: + stAudio.freq = 44100; + soundBufferLen = 1470 * 2; + break; + case 2: + stAudio.freq = 22050; + soundBufferLen = 736 * 2; + break; + case 4: + stAudio.freq = 11025; + soundBufferLen = 368 * 2; + break; + } + + stAudio.format = AUDIO_S16SYS; + stAudio.channels = 2; + stAudio.samples = iSoundSamples; + stAudio.callback = vSoundCallback; + stAudio.userdata = NULL; + + if (SDL_OpenAudio(&stAudio, NULL) < 0) + { + fprintf(stderr, "Failed to open audio: %s\n", SDL_GetError()); + return false; + } + + pstSoundCond = SDL_CreateCond(); + pstSoundMutex = SDL_CreateMutex(); + + soundBufferTotalLen = soundBufferLen * 10; + iSoundLen = 0; + systemSoundOn = true; + + return true; +} + +void systemSoundShutdown() +{ + SDL_mutexP(pstSoundMutex); + int iSave = emulating; + emulating = 0; + SDL_CondSignal(pstSoundCond); + SDL_mutexV(pstSoundMutex); + + SDL_DestroyCond(pstSoundCond); + pstSoundCond = NULL; + + SDL_DestroyMutex(pstSoundMutex); + pstSoundMutex = NULL; + + SDL_CloseAudio(); + + emulating = iSave; + systemSoundOn = false; +} + +void systemSoundPause() +{ + SDL_PauseAudio(1); +} + +void systemSoundResume() +{ + SDL_PauseAudio(0); +} + +void systemSoundReset() +{ +} + +u32 systemGetClock() +{ + return SDL_GetTicks(); +} + +void systemUpdateMotionSensor() +{ +} + +int systemGetSensorX() +{ + return 0; +} + +int systemGetSensorY() +{ + return 0; +} + +void systemGbPrint(u8 * _puiData, + int _iPages, + int _iFeed, + int _iPalette, + int _iContrast) +{ +} + +void systemScreenMessage(const char * _csMsg, int slot, int duration, const char *colorList) +{ +} + +bool systemCanChangeSoundQuality() +{ + return true; +} + +bool systemPauseOnFrame() +{ + return false; +} + +void systemGbBorderOn() +{ +} + +void debuggerMain() +{ +} + +void debuggerSignal(int, int) +{ +} + +void debuggerOutput(char *, u32) +{ +} + +char *sdlGetFilename(char *name) +{ + static char filebuffer[2048]; + + int len = strlen(name); + + char *p = name + len - 1; + + while(true) { + if(*p == '/' || + *p == '\\') { + p++; + break; + } + len--; + p--; + if(len == 0) + break; + } + + if(len == 0) + strcpy(filebuffer, name); + else + strcpy(filebuffer, p); + return filebuffer; +} + +bool sdlCheckJoyKey(int key) +{ + int dev = (key >> 12) - 1; + int what = key & 0xfff; + + if(what >= 128) { + // joystick button + int button = what - 128; + + if(button >= SDL_JoystickNumButtons(sdlDevices[dev])) + return false; + } else if (what < 0x20) { + // joystick axis + what >>= 1; + if(what >= SDL_JoystickNumAxes(sdlDevices[dev])) + return false; + } else if (what < 0x30) { + // joystick hat + what = (what & 15); + what >>= 2; + if(what >= SDL_JoystickNumHats(sdlDevices[dev])) + return false; + } + + // no problem found + return true; +} + +u16 checksumBIOS() +{ + bool hasBIOS = false; + u8 * tempBIOS; + if(useBios) + { + tempBIOS = (u8 *)malloc(0x4000); + int size = 0x4000; + if(utilLoad(biosFileName, + utilIsGBABios, + tempBIOS, + size)) { + if(size == 0x4000) + hasBIOS = true; + } + } + + u16 biosCheck = 0; + if(hasBIOS) { + for(int i = 0; i < 0x4000; i += 4) + biosCheck += *((u32 *)&tempBIOS[i]); + free(tempBIOS); + } + + return biosCheck; +} + +void (*dbgMain)() = debuggerMain; +void (*dbgSignal)(int, int) = debuggerSignal; +void (*dbgOutput)(char *, u32) = debuggerOutput; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/tools.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/tools.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,65 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "tools.h" + +namespace VBA +{ + +std::string sCutSuffix(const std::string & _rsString, + const std::string & _rsSep) +{ + return _rsString.substr(0, _rsString.find_last_of(_rsSep)); +} + +Glib::ustring sCutSuffix(const Glib::ustring & _rsString, + const Glib::ustring & _rsSep) +{ + return _rsString.substr(0, _rsString.find_last_of(_rsSep)); +} + +bool bHasSuffix(const Glib::ustring & _rsString, + const Glib::ustring & _rsSuffix, + bool _bCaseSensitive) +{ + if (_rsSuffix.size() > _rsString.size()) + { + return false; + } + + Glib::ustring sEnd = _rsString.substr(_rsString.size() - _rsSuffix.size()); + + if (_bCaseSensitive) + { + if (_rsSuffix == sEnd) + { + return true; + } + } + else + { + if (_rsSuffix.lowercase() == sEnd.lowercase()) + { + return true; + } + } + + return false; +} + +} // namespace VBA diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/tools.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/tools.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,42 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_TOOLS_H__ +#define __VBA_TOOLS_H__ + +#include +#include + +namespace VBA +{ + +std::string sCutSuffix(const std::string & _rsString, + const std::string & _rsSep = "."); + +Glib::ustring sCutSuffix(const Glib::ustring & _rsString, + const Glib::ustring & _rsSep = "."); + +bool bHasSuffix(const Glib::ustring & _rsString, + const Glib::ustring & _rsSuffix, + bool _bCaseSensitive = true); + +} + + +#endif // __VBA_TOOLS_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/vba.glade --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/vba.glade Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,3597 @@ + + + + + + + VBA + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_NONE + False + True + False + False + False + GDK_WINDOW_TYPE_HINT_NORMAL + + + + True + False + 0 + + + + True + GTK_SHADOW_OUT + GTK_POS_LEFT + GTK_POS_TOP + + + + True + + + + True + _File + True + + + + + + + True + _Open... + True + + + + + True + gtk-open + 1 + 0.5 + 0.5 + 0 + 0 + + + + + + + + True + + + + + + True + _Load... + True + + + + + + + True + _Save... + True + + + + + + + True + Loa_d game + True + + + + + + + True + Most recent + True + + + + + + True + Auto load most recent + True + False + + + + + + True + + + + + + True + Slot1 + True + + + + + + + True + Slot2 + True + + + + + + + True + Slot3 + True + + + + + + + True + Slot4 + True + + + + + + + True + Slot5 + True + + + + + + + True + Slot6 + True + + + + + + + True + Slot7 + True + + + + + + + True + Slot8 + True + + + + + + + True + Slot9 + True + + + + + + + True + Slot10 + True + + + + + + + + + + + True + S_ave game + True + + + + + + + True + Oldest slot + True + + + + + + True + + + + + + True + Slot1 + True + + + + + + + True + Slot2 + True + + + + + + + True + Slot3 + True + + + + + + + True + Slot4 + True + + + + + + + True + Slot5 + True + + + + + + + True + Slot6 + True + + + + + + + True + Slot7 + True + + + + + + + True + Slot8 + True + + + + + + + True + Slot9 + True + + + + + + + True + Slot10 + True + + + + + + + + + + + True + + + + + + True + _Pause + True + False + + + + + + + True + _Reset + True + + + + + + + True + + + + + + True + Rece_nt + True + + + + + + + True + _Reset + True + + + + + + True + _Freeze + True + False + + + + + + True + + + + + + + + + + True + + + + + + True + _Import + True + + + + + + + True + _Battery file... + True + + + + + + + + + + True + E_xport + True + + + + + + + True + _Battery file... + True + + + + + + + + + + True + + + + + + True + Screen capt_ure... + True + + + + + + True + + + + + + True + _Close + True + + + + True + gtk-close + 1 + 0.5 + 0.5 + 0 + 0 + + + + + + + + True + _Exit + True + + + + + True + gtk-quit + 1 + 0.5 + 0.5 + 0 + 0 + + + + + + + + + + + + True + _Options + True + + + + + + + True + _Frameskip + True + + + + + + + True + _Throttle + True + + + + + + + True + _No throttle + True + True + + + + + + True + 25% + True + False + ThrottleNoThrottle + + + + + + True + 50% + True + False + ThrottleNoThrottle + + + + + + True + 100% + True + False + ThrottleNoThrottle + + + + + + True + 150% + True + False + ThrottleNoThrottle + + + + + + True + 200% + True + False + ThrottleNoThrottle + + + + + + True + _Other... + True + False + ThrottleNoThrottle + + + + + + + + + + True + + + + + + True + _Automatic + True + True + + + + + + True + _0 + True + False + FrameskipAutomatic + + + + + + True + _1 + True + False + FrameskipAutomatic + + + + + + True + _2 + True + False + FrameskipAutomatic + + + + + + True + _3 + True + False + FrameskipAutomatic + + + + + + True + _4 + True + False + FrameskipAutomatic + + + + + + True + _5 + True + False + FrameskipAutomatic + + + + + + True + _6 + True + False + FrameskipAutomatic + + + + + + True + _7 + True + False + FrameskipAutomatic + + + + + + True + _8 + True + False + FrameskipAutomatic + + + + + + True + _9 + True + False + FrameskipAutomatic + + + + + + + + + + True + _Video + True + + + + + + + True + _1x + True + True + + + + + + True + _2x + True + False + Video1x + + + + + + True + _3x + True + False + Video1x + + + + + + True + _4x + True + False + Video1x + + + + + + True + _5x + True + False + Video1x + + + + + + True + _6x + True + False + Video1x + + + + + + True + + + + + + True + _Layers + True + + + + + + + True + BG0 + True + False + + + + + + + True + BG1 + True + False + + + + + + + True + BG2 + True + False + + + + + + + True + BG3 + True + False + + + + + + + True + OBJ + True + False + + + + + + + True + WIN0 + True + False + + + + + + + True + WIN1 + True + False + + + + + + + True + OBJWIN + True + False + + + + + + + + + + + + + + + True + _Emulator + True + + + + + + + True + Directories... + True + + + + + + True + Pause when inactive window + True + False + + + + + + True + Show speed + True + + + + + + + True + None + True + True + + + + + + True + Percentage + True + False + ShowSpeedNone + + + + + + True + Detailed + True + False + ShowSpeedNone + + + + + + + + + + True + Save type + True + + + + + + + True + _Automatic + True + True + + + + + + True + EEPROM + True + False + SaveTypeAutomatic + + + + + + True + SRAM + True + False + SaveTypeAutomatic + + + + + + True + Flash + True + False + SaveTypeAutomatic + + + + + + True + EEPROM+Sensor + True + False + SaveTypeAutomatic + + + + + + True + None + True + False + SaveTypeAutomatic + + + + + + True + + + + + + True + Flash 64K + True + True + + + + + + True + Flash 128K + True + False + SaveTypeFlash64K + + + + + + + + + + True + + + + + + True + _Select BIOS file... + True + + + + + + True + _Use BIOS file + True + False + + + + + + True + + + + + + True + Screenshot format + True + + + + + + + True + _PNG + True + True + + + + + + True + _BMP + True + False + ScreenshotFormatPNG + + + + + + + + + + + + + + True + _Sound + True + + + + + + + True + O_ff + True + True + + + + + + True + _Mute + True + False + SoundOff + + + + + + True + _On + True + False + SoundOff + + + + + + True + + + + + + True + Echo + True + False + + + + + + True + Low pass filter + True + False + + + + + + True + Reverse stereo + True + False + + + + + + True + + + + + + True + Channel _1 + True + False + + + + + + True + Channel _2 + True + False + + + + + + True + Channel _3 + True + False + + + + + + True + Channel _4 + True + False + + + + + + True + Channel _A + True + False + + + + + + True + Channel _B + True + False + + + + + + True + + + + + + True + 11 _Khz + True + True + + + + + + True + 22 K_hz + True + False + Sound11Khz + + + + + + True + 44 Kh_z + True + False + Sound11Khz + + + + + + True + + + + + + True + _Volume + True + + + + + + + True + 25% + True + True + + + + + + True + 50% + True + False + Volume25 + + + + + + True + 100% + True + False + Volume25 + + + + + + True + 200% + True + False + Volume25 + + + + + + True + 300% + True + False + Volume25 + + + + + + True + 400% + True + False + Volume25 + + + + + + + + + + + + + + True + _Gameboy + True + + + + + + + True + _Border + True + False + + + + + + True + _Printer + True + False + + + + + + True + + + + + + True + _Automatic + True + True + + + + + + True + _GBA + True + False + GameboyAutomatic + + + + + + True + _CGB/GBC + True + False + GameboyAutomatic + + + + + + True + _SGB + True + False + GameboyAutomatic + + + + + + True + SGB_2 + True + False + GameboyAutomatic + + + + + + True + G_B + True + False + GameboyAutomatic + + + + + + + + + + True + F_ilter + True + + + + + + + True + Interframe _blending + True + + + + + + + True + _None + True + True + + + + + + True + _Smart + True + False + IFBNone + + + + + + True + _Motion Blur + True + False + IFBNone + + + + + + + + + + True + + + + + + True + _None + True + True + + + + + + True + _TV Mode + True + False + FilterNone + + + + + + True + _2xSaI + True + False + FilterNone + + + + + + True + _Super 2xSaI + True + False + FilterNone + + + + + + True + Super _Eagle + True + False + FilterNone + + + + + + True + _Pixelate + True + False + FilterNone + + + + + + True + _Motion Blur + True + False + FilterNone + + + + + + True + _AdvanceMAME 2x + True + False + FilterNone + + + + + + True + S_imple 2x + True + False + FilterNone + + + + + + True + Bilinea_r + True + False + FilterNone + + + + + + True + Bilinear Pl_us + True + False + FilterNone + + + + + + True + S_canlines + True + False + FilterNone + + + + + + True + h_q2x + True + False + FilterNone + + + + + + True + _lq2x + True + False + FilterNone + + + + + + True + + + + + + True + _Disable MMX + True + False + + + + + + + + + + True + _Joypad + True + + + + + + + True + _Configure + True + + + + + + + True + _1... + True + + + + + + True + _2... + True + + + + + + True + _3... + True + + + + + + True + _4... + True + + + + + + + + + + True + + + + + + True + _1 + True + True + + + + + + True + _2 + True + False + Joypad1 + + + + + + True + _3 + True + False + Joypad1 + + + + + + True + _4 + True + False + Joypad1 + + + + + + True + + + + + + True + _Autofire + True + + + + + + + True + _A + True + False + + + + + + + True + _B + True + False + + + + + + + True + _L + True + False + + + + + + + True + _R + True + False + + + + + + + + + + + + + + + + + + + True + _Tools + True + + + + + + + True + _GDB + True + + + + + + + True + _Wait connection... + True + + + + + + True + _Load and wait... + True + + + + + + True + _Break + True + + + + + + True + _Disconnect + True + + + + + + + + + + + + + + True + _Help + True + + + + + + + True + _About + True + + + + + + + + + + + 0 + False + True + + + + + + True + 0.5 + 0.5 + 0 + 0 + + + + + + + 0 + True + True + + + + + + + + About VBA + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_CENTER_ON_PARENT + False + True + True + False + False + GDK_WINDOW_TYPE_HINT_DIALOG + True + + + + True + False + 0 + + + + True + GTK_BUTTONBOX_END + + + + True + True + True + gtk-close + True + GTK_RELIEF_NORMAL + -7 + + + + + 0 + False + True + GTK_PACK_END + + + + + + True + 0.5 + 0.5 + 0 + 0 + + + + + + + 0 + True + True + + + + + + True + True + + False + True + GTK_JUSTIFY_CENTER + False + True + 0.5 + 0.5 + 5 + 5 + + + 0 + True + False + + + + + + True + True + An emulator for Gameboy(TM) and GameboyAdvance(TM). + False + False + GTK_JUSTIFY_CENTER + True + True + 0.5 + 0.5 + 5 + 5 + + + 0 + True + False + + + + + + True + True + <i>Special thanks to Yann Parmentier aka "kohai" for the icons.</i> + False + True + GTK_JUSTIFY_LEFT + False + True + 0.5 + 0.5 + 5 + 5 + + + 0 + True + False + + + + + + True + True + <small>Copyright (C) 2004 Forgotten and the VBA development team</small> + False + True + GTK_JUSTIFY_CENTER + True + True + 0.5 + 0.5 + 5 + 5 + + + 0 + True + False + + + + + + + + Throttle + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_CENTER_ON_PARENT + False + True + True + False + False + GDK_WINDOW_TYPE_HINT_DIALOG + True + + + + True + False + 0 + + + + True + GTK_BUTTONBOX_END + + + + True + True + True + gtk-cancel + True + GTK_RELIEF_NORMAL + -6 + + + + + + True + True + True + gtk-ok + True + GTK_RELIEF_NORMAL + -5 + + + + + 0 + False + True + GTK_PACK_END + + + + + + True + 0.5 + 0.5 + 0 + 0 + + + + True + False + 0 + + + + True + Throttle : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + True + True + + + + + + True + True + 1 + 0 + True + GTK_UPDATE_ALWAYS + True + True + 100 5 1000 1 25 25 + + + 0 + True + True + + + + + + True + % + False + False + GTK_JUSTIFY_LEFT + False + False + 0 + 0.5 + 0 + 0 + + + 0 + True + True + + + + + + + 0 + True + True + + + + + + + + Directories + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_CENTER_ON_PARENT + False + True + True + False + False + GDK_WINDOW_TYPE_HINT_DIALOG + True + + + + True + False + 0 + + + + True + GTK_BUTTONBOX_END + + + + True + True + True + gtk-cancel + True + GTK_RELIEF_NORMAL + -6 + + + + + + True + True + True + gtk-ok + True + GTK_RELIEF_NORMAL + -5 + + + + + 0 + False + True + GTK_PACK_END + + + + + + True + 5 + 4 + False + 0 + 0 + + + + True + GBA roms : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 0 + 1 + fill + expand + + + + + + True + True + True + True + 0 + + True + * + False + + + 1 + 2 + 0 + 1 + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-cancel + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 2 + 3 + 0 + 1 + fill + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-open + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 3 + 4 + 0 + 1 + fill + expand + + + + + + True + GB roms : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 1 + 2 + fill + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-open + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 3 + 4 + 1 + 2 + fill + expand + + + + + + True + Batteries : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 2 + 3 + fill + expand + + + + + + True + True + True + True + 0 + + True + * + False + + + 1 + 2 + 2 + 3 + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-open + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 3 + 4 + 2 + 3 + fill + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-cancel + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 2 + 3 + 2 + 3 + fill + expand + + + + + + True + Saves : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 3 + 4 + fill + expand + + + + + + True + True + True + True + 0 + + True + * + False + + + 1 + 2 + 3 + 4 + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-cancel + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 2 + 3 + 3 + 4 + fill + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-open + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 3 + 4 + 3 + 4 + fill + expand + + + + + + True + Captures : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 4 + 5 + fill + expand + + + + + + True + True + True + True + 0 + + True + * + False + + + 1 + 2 + 4 + 5 + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-cancel + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 2 + 3 + 4 + 5 + fill + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-open + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 3 + 4 + 4 + 5 + fill + expand + + + + + + True + True + True + True + 0 + + True + * + False + + + 1 + 2 + 1 + 2 + expand + + + + + + True + True + GTK_RELIEF_NONE + + + + True + gtk-cancel + 4 + 0.5 + 0.5 + 0 + 0 + + + + + 2 + 3 + 1 + 2 + fill + expand + + + + + 0 + True + True + + + + + + + + Joypad config + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_CENTER_ON_PARENT + False + True + True + False + False + GDK_WINDOW_TYPE_HINT_DIALOG + True + + + + True + False + 0 + + + + True + GTK_BUTTONBOX_END + + + + True + True + True + gtk-cancel + True + GTK_RELIEF_NORMAL + -6 + + + + + + True + True + True + gtk-ok + True + GTK_RELIEF_NORMAL + -5 + + + + + 0 + False + True + GTK_PACK_END + + + + + + True + 0.5 + 0.5 + 0 + 1 + + + + True + 12 + 2 + False + 5 + 0 + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 0 + 1 + fill + expand + + + + + + True + Down : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 1 + 2 + fill + expand + + + + + + True + Left : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 2 + 3 + fill + expand + + + + + + True + Right : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 3 + 4 + fill + expand + + + + + + True + Button A : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 4 + 5 + fill + expand + + + + + + True + Button B : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 5 + 6 + fill + expand + + + + + + True + Button L : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 6 + 7 + fill + expand + + + + + + True + Button R : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 7 + 8 + fill + expand + + + + + + True + Select : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 8 + 9 + fill + expand + + + + + + True + Start : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 9 + 10 + fill + expand + + + + + + True + Speed : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 10 + 11 + fill + expand + + + + + + True + Capture : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 11 + 12 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 1 + 2 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 2 + 3 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 3 + 4 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 4 + 5 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 5 + 6 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 6 + 7 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 7 + 8 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 8 + 9 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 9 + 10 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 10 + 11 + fill + expand + + + + + + True + True + False + True + 0 + + True + * + False + + + 1 + 2 + 11 + 12 + fill + expand + + + + + + True + Up : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + 1 + 0 + 1 + fill + expand + + + + + + + 0 + True + True + + + + + + + + TCP port + GTK_WINDOW_TOPLEVEL + GTK_WIN_POS_CENTER_ON_PARENT + False + True + True + False + False + GDK_WINDOW_TYPE_HINT_DIALOG + True + + + + True + False + 0 + + + + True + GTK_BUTTONBOX_END + + + + True + True + True + gtk-cancel + True + GTK_RELIEF_NORMAL + -6 + + + + + + True + True + True + gtk-ok + True + GTK_RELIEF_NORMAL + -5 + + + + + 0 + False + True + GTK_PACK_END + + + + + + True + 0.5 + 0.5 + 0 + 0 + + + + True + False + 0 + + + + True + Port : + False + False + GTK_JUSTIFY_LEFT + False + False + 1 + 0.5 + 0 + 0 + + + 0 + True + True + + + + + + True + True + 1 + 0 + True + GTK_UPDATE_ALWAYS + True + True + 55555 1 65535 1 100 100 + + + 0 + True + True + + + + + + + 0 + True + True + + + + + + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/window.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/window.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1944 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "window.h" + +#include + +#include +#include + +#include + +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gba/GBASound.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +#include "../gb/gbPrinter.h" +#include "../common/Util.h" + +#include "menuitem.h" +#include "tools.h" +#include "intl.h" + +extern int systemRenderedFrames; +extern int systemFPS; +extern bool debugger; +extern int RGB_LOW_BITS_MASK; +extern void (*dbgMain)(); +extern void (*dbgSignal)(int, int); +extern void (*dbgOutput)(char *, u32); +extern void remoteInit(); +extern void remoteCleanUp(); +extern void remoteStubMain(); +extern void remoteStubSignal(int, int); +extern void remoteOutput(char *, u32); +extern void remoteSetProtocol(int); +extern void remoteSetPort(int); + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif // MMX + +namespace VBA +{ + +using Gnome::Glade::Xml; + +Window * Window::m_poInstance = NULL; + +Window::Window(GtkWindow * _pstWindow, const Glib::RefPtr & _poXml) : + Gtk::Window (_pstWindow), + m_iGBScreenWidth (160), + m_iGBScreenHeight (144), + m_iSGBScreenWidth (256), + m_iSGBScreenHeight(224), + m_iGBAScreenWidth (240), + m_iGBAScreenHeight(160), + m_iFrameskipMin (0), + m_iFrameskipMax (9), + m_iThrottleMin (5), + m_iThrottleMax (1000), + m_iScaleMin (1), + m_iScaleMax (6), + m_iShowSpeedMin (ShowNone), + m_iShowSpeedMax (ShowDetailed), + m_iSaveTypeMin (SaveAuto), + m_iSaveTypeMax (SaveNone), + m_iSoundQualityMin(Sound44K), + m_iSoundQualityMax(Sound11K), + m_iSoundVolumeMin (Sound100), + m_iSoundVolumeMax (Sound50), + m_iEmulatorTypeMin(EmulatorAuto), + m_iEmulatorTypeMax(EmulatorSGB2), + m_iFilter2xMin (FirstFilter), + m_iFilter2xMax (LastFilter), + m_iFilterIBMin (FirstFilterIB), + m_iFilterIBMax (LastFilterIB), + m_iJoypadMin (1), + m_iJoypadMax (4) +{ + m_poXml = _poXml; + m_poFileOpenDialog = NULL; + m_iScreenWidth = m_iGBAScreenWidth; + m_iScreenHeight = m_iGBAScreenHeight; + m_eCartridge = CartridgeNone; + m_uiJoypadState = 0; + + vInitSystem(); + vInitSDL(); + + Gtk::Container * poC; + poC = dynamic_cast(_poXml->get_widget("ScreenContainer")); + m_poScreenArea = Gtk::manage(new ScreenArea(m_iScreenWidth, m_iScreenHeight)); + poC->add(*m_poScreenArea); + vDrawDefaultScreen(); + m_poScreenArea->show(); + + // Get config + // + vInitConfig(); + + m_sUserDataDir = Glib::get_home_dir() + "/.gvba"; + m_sConfigFile = m_sUserDataDir + "/config"; + + if (! Glib::file_test(m_sUserDataDir, Glib::FILE_TEST_EXISTS)) + { + mkdir(m_sUserDataDir.c_str(), 0777); + } + if (Glib::file_test(m_sConfigFile, Glib::FILE_TEST_EXISTS)) + { + vLoadConfig(m_sConfigFile); + vCheckConfig(); + } + else + { + vSaveConfig(m_sConfigFile); + } + + vCreateFileOpenDialog(); + vLoadHistoryFromConfig(); + vLoadJoypadsFromConfig(); + + Gtk::MenuItem * poMI; + Gtk::CheckMenuItem * poCMI; + + // File menu + // + poMI = dynamic_cast(_poXml->get_widget("FileOpen")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnFileOpen)); + + poMI = dynamic_cast(_poXml->get_widget("FileLoad")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnFileLoad)); + m_listSensitiveWhenPlaying.push_back(poMI); + + poMI = dynamic_cast(_poXml->get_widget("FileSave")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnFileSave)); + m_listSensitiveWhenPlaying.push_back(poMI); + + for (int i = 0; i < 10; i++) + { + char csName[20]; + snprintf(csName, 20, "LoadGameSlot%d", i + 1); + m_apoLoadGameItem[i] = dynamic_cast(_poXml->get_widget(csName)); + snprintf(csName, 20, "SaveGameSlot%d", i + 1); + m_apoSaveGameItem[i] = dynamic_cast(_poXml->get_widget(csName)); + + m_apoLoadGameItem[i]->signal_activate().connect(SigC::bind( + SigC::slot(*this, &Window::vOnLoadGame), + i + 1)); + m_apoSaveGameItem[i]->signal_activate().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSaveGame), + i + 1)); + } + vUpdateGameSlots(); + + poMI = dynamic_cast(_poXml->get_widget("LoadGameMostRecent")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnLoadGameMostRecent)); + m_listSensitiveWhenPlaying.push_back(poMI); + + poCMI = dynamic_cast(_poXml->get_widget("LoadGameAuto")); + poCMI->set_active(m_poCoreConfig->oGetKey("load_game_auto")); + vOnLoadGameAutoToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnLoadGameAutoToggled), + poCMI)); + + poMI = dynamic_cast(_poXml->get_widget("SaveGameOldest")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnSaveGameOldest)); + m_listSensitiveWhenPlaying.push_back(poMI); + + m_poFilePauseItem = dynamic_cast(_poXml->get_widget("FilePause")); + m_poFilePauseItem->set_active(false); + vOnFilePauseToggled(m_poFilePauseItem); + m_poFilePauseItem->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnFilePauseToggled), + m_poFilePauseItem)); + m_listSensitiveWhenPlaying.push_back(m_poFilePauseItem); + + poMI = dynamic_cast(_poXml->get_widget("FileReset")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnFileReset)); + m_listSensitiveWhenPlaying.push_back(poMI); + + poMI = dynamic_cast(_poXml->get_widget("FileScreenCapture")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnFileScreenCapture)); + m_listSensitiveWhenPlaying.push_back(poMI); + + poMI = dynamic_cast(_poXml->get_widget("FileClose")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnFileClose)); + m_listSensitiveWhenPlaying.push_back(poMI); + + poMI = dynamic_cast(_poXml->get_widget("FileExit")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnFileExit)); + + // Recent menu + // + m_poRecentMenu = dynamic_cast(_poXml->get_widget("RecentMenu_menu")); + vUpdateHistoryMenu(); + + m_poRecentResetItem = dynamic_cast(_poXml->get_widget("RecentReset")); + m_poRecentResetItem->signal_activate().connect(SigC::slot(*this, &Window::vOnRecentReset)); + + poCMI = dynamic_cast(_poXml->get_widget("RecentFreeze")); + poCMI->set_active(m_poHistoryConfig->oGetKey("freeze")); + vOnRecentFreezeToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnRecentFreezeToggled), + poCMI)); + + // Import menu + // + poMI = dynamic_cast(_poXml->get_widget("ImportBatteryFile")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnImportBatteryFile)); + m_listSensitiveWhenPlaying.push_back(poMI); + + // Export menu + // + poMI = dynamic_cast(_poXml->get_widget("ExportBatteryFile")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnExportBatteryFile)); + m_listSensitiveWhenPlaying.push_back(poMI); + + // Frameskip menu + // + struct + { + const char * m_csName; + const int m_iFrameskip; + } + astFrameskip[] = + { + { "FrameskipAutomatic", -1 }, + { "Frameskip0", 0 }, + { "Frameskip1", 1 }, + { "Frameskip2", 2 }, + { "Frameskip3", 3 }, + { "Frameskip4", 4 }, + { "Frameskip5", 5 }, + { "Frameskip6", 6 }, + { "Frameskip7", 7 }, + { "Frameskip8", 8 }, + { "Frameskip9", 9 } + }; + int iDefaultFrameskip; + if (m_poCoreConfig->sGetKey("frameskip") == "auto") + { + iDefaultFrameskip = -1; + } + else + { + iDefaultFrameskip = m_poCoreConfig->oGetKey("frameskip"); + } + for (guint i = 0; i < G_N_ELEMENTS(astFrameskip); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astFrameskip[i].m_csName)); + if (astFrameskip[i].m_iFrameskip == iDefaultFrameskip) + { + poCMI->set_active(); + vOnFrameskipToggled(poCMI, iDefaultFrameskip); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnFrameskipToggled), + poCMI, astFrameskip[i].m_iFrameskip)); + } + + // Throttle menu + // + struct + { + const char * m_csName; + const int m_iThrottle; + } + astThrottle[] = + { + { "ThrottleNoThrottle", 0 }, + { "Throttle25", 25 }, + { "Throttle50", 50 }, + { "Throttle100", 100 }, + { "Throttle150", 150 }, + { "Throttle200", 200 } + }; + poCMI = dynamic_cast(_poXml->get_widget("ThrottleOther")); + poCMI->set_active(); + poCMI->signal_activate().connect(SigC::bind( + SigC::slot(*this, &Window::vOnThrottleOther), + poCMI)); + + int iDefaultThrottle = m_poCoreConfig->oGetKey("throttle"); + for (guint i = 0; i < G_N_ELEMENTS(astThrottle); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astThrottle[i].m_csName)); + if (astThrottle[i].m_iThrottle == iDefaultThrottle) + { + poCMI->set_active(); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnThrottleToggled), + poCMI, astThrottle[i].m_iThrottle)); + } + vSetThrottle(iDefaultThrottle); + + // Video menu + // + struct + { + const char * m_csName; + const int m_iScale; + } + astVideoScale[] = + { + { "Video1x", 1 }, + { "Video2x", 2 }, + { "Video3x", 3 }, + { "Video4x", 4 }, + { "Video5x", 5 }, + { "Video6x", 6 } + }; + int iDefaultScale = m_poDisplayConfig->oGetKey("scale"); + for (guint i = 0; i < G_N_ELEMENTS(astVideoScale); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astVideoScale[i].m_csName)); + if (astVideoScale[i].m_iScale == iDefaultScale) + { + poCMI->set_active(); + vOnVideoScaleToggled(poCMI, iDefaultScale); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnVideoScaleToggled), + poCMI, astVideoScale[i].m_iScale)); + } + + // Layers menu + // + struct + { + const char * m_csName; + const char * m_csKey; + const int m_iLayer; + } + astLayer[] = + { + { "LayersBg0", "layer_bg0", 0 }, + { "LayersBg1", "layer_bg1", 1 }, + { "LayersBg2", "layer_bg2", 2 }, + { "LayersBg3", "layer_bg3", 3 }, + { "LayersObj", "layer_obj", 4 }, + { "LayersWin0", "layer_win0", 5 }, + { "LayersWin1", "layer_win1", 6 }, + { "LayersObjWin", "layer_objwin", 7 } + }; + for (guint i = 0; i < G_N_ELEMENTS(astLayer); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astLayer[i].m_csName)); + poCMI->set_active(m_poCoreConfig->oGetKey(astLayer[i].m_csKey)); + vOnLayerToggled(poCMI, astLayer[i].m_iLayer); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnLayerToggled), + poCMI, astLayer[i].m_iLayer)); + } + + // Emulator menu + // + poMI = dynamic_cast(_poXml->get_widget("EmulatorDirectories")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnDirectories)); + + poCMI = dynamic_cast(_poXml->get_widget("EmulatorPauseWhenInactive")); + poCMI->set_active(m_poDisplayConfig->oGetKey("pause_when_inactive")); + vOnPauseWhenInactiveToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnPauseWhenInactiveToggled), + poCMI)); + + m_poUseBiosItem = dynamic_cast(_poXml->get_widget("EmulatorUseBios")); + m_poUseBiosItem->set_active(m_poCoreConfig->oGetKey("use_bios_file")); + if (m_poCoreConfig->sGetKey("bios_file") == "") + { + m_poUseBiosItem->set_sensitive(false); + } + m_poUseBiosItem->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnUseBiosToggled), + m_poUseBiosItem)); + + poMI = dynamic_cast(_poXml->get_widget("EmulatorSelectBios")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnSelectBios)); + + // Show speed menu + // + struct + { + const char * m_csName; + const EShowSpeed m_eShowSpeed; + } + astShowSpeed[] = + { + { "ShowSpeedNone", ShowNone }, + { "ShowSpeedPercentage", ShowPercentage }, + { "ShowSpeedDetailed", ShowDetailed } + }; + EShowSpeed eDefaultShowSpeed = (EShowSpeed)m_poDisplayConfig->oGetKey("show_speed"); + for (guint i = 0; i < G_N_ELEMENTS(astShowSpeed); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astShowSpeed[i].m_csName)); + if (astShowSpeed[i].m_eShowSpeed == eDefaultShowSpeed) + { + poCMI->set_active(); + vOnShowSpeedToggled(poCMI, eDefaultShowSpeed); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnShowSpeedToggled), + poCMI, astShowSpeed[i].m_eShowSpeed)); + } + + // Save type menu + // + struct + { + const char * m_csName; + const ESaveType m_eSaveType; + } + astSaveType[] = + { + { "SaveTypeAutomatic", SaveAuto }, + { "SaveTypeEeprom", SaveEEPROM }, + { "SaveTypeSram", SaveSRAM }, + { "SaveTypeFlash", SaveFlash }, + { "SaveTypeEepromSensor", SaveEEPROMSensor }, + { "SaveTypeNone", SaveNone } + }; + ESaveType eDefaultSaveType = (ESaveType)m_poCoreConfig->oGetKey("save_type"); + for (guint i = 0; i < G_N_ELEMENTS(astSaveType); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astSaveType[i].m_csName)); + if (astSaveType[i].m_eSaveType == eDefaultSaveType) + { + poCMI->set_active(); + vOnSaveTypeToggled(poCMI, eDefaultSaveType); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSaveTypeToggled), + poCMI, astSaveType[i].m_eSaveType)); + } + + // Flash size menu + // + struct + { + const char * m_csName; + const int m_iFlashSize; + } + astFlashSize[] = + { + { "SaveTypeFlash64K", 64 }, + { "SaveTypeFlash128K", 128 } + }; + int iDefaultFlashSize = m_poCoreConfig->oGetKey("flash_size"); + for (guint i = 0; i < G_N_ELEMENTS(astFlashSize); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astFlashSize[i].m_csName)); + if (astFlashSize[i].m_iFlashSize == iDefaultFlashSize) + { + poCMI->set_active(); + vOnFlashSizeToggled(poCMI, iDefaultFlashSize); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnFlashSizeToggled), + poCMI, astFlashSize[i].m_iFlashSize)); + } + + // Screenshot format menu + // + struct + { + const char * m_csName; + const char * m_csScreenshotFormat; + } + astScreenshotFormat[] = + { + { "ScreenshotFormatPNG", "png" }, + { "ScreenshotFormatBMP", "bmp" } + }; + std::string sDefaultScreenshotFormat = m_poCoreConfig->sGetKey("screenshot_format"); + for (guint i = 0; i < G_N_ELEMENTS(astScreenshotFormat); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astScreenshotFormat[i].m_csName)); + if (astScreenshotFormat[i].m_csScreenshotFormat == sDefaultScreenshotFormat) + { + poCMI->set_active(); + vOnScreenshotFormatToggled(poCMI, sDefaultScreenshotFormat); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnScreenshotFormatToggled), + poCMI, std::string(astScreenshotFormat[i].m_csScreenshotFormat))); + } + + // Sound menu + // + std::string sDefaultSoundStatus = m_poSoundConfig->sGetKey("status"); + + poCMI = dynamic_cast(_poXml->get_widget("SoundOff")); + if (sDefaultSoundStatus == "off") + { + poCMI->set_active(); + vOnSoundStatusToggled(poCMI, SoundOff); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundStatusToggled), + poCMI, SoundOff)); + m_poSoundOffItem = poCMI; + + poCMI = dynamic_cast(_poXml->get_widget("SoundMute")); + if (sDefaultSoundStatus == "mute") + { + poCMI->set_active(); + vOnSoundStatusToggled(poCMI, SoundMute); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundStatusToggled), + poCMI, SoundMute)); + + poCMI = dynamic_cast(_poXml->get_widget("SoundOn")); + if (sDefaultSoundStatus == "on") + { + poCMI->set_active(); + vOnSoundStatusToggled(poCMI, SoundOn); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundStatusToggled), + poCMI, SoundOn)); + + poCMI = dynamic_cast(_poXml->get_widget("SoundEcho")); + poCMI->set_active(m_poSoundConfig->oGetKey("echo")); + vOnSoundEchoToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundEchoToggled), + poCMI)); + + poCMI = dynamic_cast(_poXml->get_widget("SoundLowPass")); + poCMI->set_active(m_poSoundConfig->oGetKey("low_pass")); + vOnSoundLowPassToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundLowPassToggled), + poCMI)); + + poCMI = dynamic_cast(_poXml->get_widget("SoundReverseStereo")); + poCMI->set_active(m_poSoundConfig->oGetKey("reverse_stereo")); + vOnSoundReverseToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundReverseToggled), + poCMI)); + + struct + { + const char * m_csName; + const char * m_csKey; + const int m_iSoundChannel; + } + astSoundChannel[] = + { + { "SoundChannel1", "channel_1", 0 }, + { "SoundChannel2", "channel_2", 1 }, + { "SoundChannel3", "channel_3", 2 }, + { "SoundChannel4", "channel_4", 3 }, + { "SoundChannelA", "channel_A", 4 }, + { "SoundChannelB", "channel_B", 5 } + }; + for (guint i = 0; i < G_N_ELEMENTS(astSoundChannel); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astSoundChannel[i].m_csName)); + poCMI->set_active(m_poSoundConfig->oGetKey(astSoundChannel[i].m_csKey)); + vOnSoundChannelToggled(poCMI, astSoundChannel[i].m_iSoundChannel); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundChannelToggled), + poCMI, astSoundChannel[i].m_iSoundChannel)); + } + + struct + { + const char * m_csName; + const ESoundQuality m_eSoundQuality; + } + astSoundQuality[] = + { + { "Sound11Khz", Sound11K }, + { "Sound22Khz", Sound22K }, + { "Sound44Khz", Sound44K } + }; + ESoundQuality eDefaultSoundQuality = (ESoundQuality)m_poSoundConfig->oGetKey("quality"); + for (guint i = 0; i < G_N_ELEMENTS(astSoundQuality); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astSoundQuality[i].m_csName)); + if (astSoundQuality[i].m_eSoundQuality == eDefaultSoundQuality) + { + poCMI->set_active(); + vOnSoundQualityToggled(poCMI, eDefaultSoundQuality); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundQualityToggled), + poCMI, astSoundQuality[i].m_eSoundQuality)); + } + + // Volume menu + // + struct + { + const char * m_csName; + const ESoundVolume m_eSoundVolume; + } + astSoundVolume[] = + { + { "Volume25", Sound25 }, + { "Volume50", Sound50 }, + { "Volume100", Sound100 }, + { "Volume200", Sound200 }, + { "Volume300", Sound300 }, + { "Volume400", Sound400 } + }; + ESoundVolume eDefaultSoundVolume = (ESoundVolume)m_poSoundConfig->oGetKey("volume"); + for (guint i = 0; i < G_N_ELEMENTS(astSoundVolume); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astSoundVolume[i].m_csName)); + if (astSoundVolume[i].m_eSoundVolume == eDefaultSoundVolume) + { + poCMI->set_active(); + vOnSoundVolumeToggled(poCMI, eDefaultSoundVolume); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnSoundVolumeToggled), + poCMI, astSoundVolume[i].m_eSoundVolume)); + } + + // Gameboy menu + // + poCMI = dynamic_cast(_poXml->get_widget("GameboyBorder")); + poCMI->set_active(m_poCoreConfig->oGetKey("gb_border")); + vOnGBBorderToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnGBBorderToggled), + poCMI)); + + poCMI = dynamic_cast(_poXml->get_widget("GameboyPrinter")); + poCMI->set_active(m_poCoreConfig->oGetKey("gb_printer")); + vOnGBPrinterToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnGBPrinterToggled), + poCMI)); + + struct + { + const char * m_csName; + const EEmulatorType m_eEmulatorType; + } + astEmulatorType[] = + { + { "GameboyAutomatic", EmulatorAuto }, + { "GameboyGba", EmulatorGBA }, + { "GameboyCgb", EmulatorCGB }, + { "GameboySgb", EmulatorSGB }, + { "GameboySgb2", EmulatorSGB2 }, + { "GameboyGb", EmulatorGB } + }; + EEmulatorType eDefaultEmulatorType = (EEmulatorType)m_poCoreConfig->oGetKey("emulator_type"); + for (guint i = 0; i < G_N_ELEMENTS(astEmulatorType); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astEmulatorType[i].m_csName)); + if (astEmulatorType[i].m_eEmulatorType == eDefaultEmulatorType) + { + poCMI->set_active(); + vOnEmulatorTypeToggled(poCMI, eDefaultEmulatorType); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnEmulatorTypeToggled), + poCMI, astEmulatorType[i].m_eEmulatorType)); + } + + // Filter menu + // + struct + { + const char * m_csName; + const EFilter2x m_eFilter2x; + } + astFilter2x[] = + { + { "FilterNone", FilterNone }, + { "FilterTVMode", FilterScanlinesTV }, + { "Filter2xSaI", Filter2xSaI }, + { "FilterSuper2xSaI", FilterSuper2xSaI }, + { "FilterSuperEagle", FilterSuperEagle }, + { "FilterPixelate", FilterPixelate }, + { "FilterMotionBlur", FilterMotionBlur }, + { "FilterAdvanceMame2x", FilterAdMame2x }, + { "FilterSimple2x", FilterSimple2x }, + { "FilterBilinear", FilterBilinear }, + { "FilterBilinearPlus", FilterBilinearPlus }, + { "FilterScanlines", FilterScanlines }, + { "FilterHq2x", FilterHq2x }, + { "FilterLq2x", FilterLq2x } + }; + EFilter2x eDefaultFilter2x = (EFilter2x)m_poDisplayConfig->oGetKey("filter2x"); + for (guint i = 0; i < G_N_ELEMENTS(astFilter2x); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astFilter2x[i].m_csName)); + if (astFilter2x[i].m_eFilter2x == eDefaultFilter2x) + { + poCMI->set_active(); + vOnFilter2xToggled(poCMI, eDefaultFilter2x); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnFilter2xToggled), + poCMI, astFilter2x[i].m_eFilter2x)); + } + + poCMI = dynamic_cast(_poXml->get_widget("FilterDisableMmx")); +#ifdef MMX + poCMI->set_active(m_poDisplayConfig->oGetKey("filter_disable_mmx")); + vOnDisableMMXToggled(poCMI); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnDisableMMXToggled), + poCMI)); +#else // ! MMX + poCMI->set_active(); + poCMI->set_sensitive(false); +#endif // ! MMX + + // Interframe blending menu + // + struct + { + const char * m_csName; + const EFilterIB m_eFilterIB; + } + astFilterIB[] = + { + { "IFBNone", FilterIBNone }, + { "IFBSmart", FilterIBSmart }, + { "IFBMotionBlur", FilterIBMotionBlur } + }; + EFilterIB eDefaultFilterIB = (EFilterIB)m_poDisplayConfig->oGetKey("filterIB"); + for (guint i = 0; i < G_N_ELEMENTS(astFilterIB); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astFilterIB[i].m_csName)); + if (astFilterIB[i].m_eFilterIB == eDefaultFilterIB) + { + poCMI->set_active(); + vOnFilterIBToggled(poCMI, eDefaultFilterIB); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnFilterIBToggled), + poCMI, astFilterIB[i].m_eFilterIB)); + } + + // Joypad menu + // + poMI = dynamic_cast(_poXml->get_widget("JoypadConfigure1")); + poMI->signal_activate().connect(SigC::bind( + SigC::slot(*this, &Window::vOnJoypadConfigure), 1)); + + poMI = dynamic_cast(_poXml->get_widget("JoypadConfigure2")); + poMI->signal_activate().connect(SigC::bind( + SigC::slot(*this, &Window::vOnJoypadConfigure), 2)); + + poMI = dynamic_cast(_poXml->get_widget("JoypadConfigure3")); + poMI->signal_activate().connect(SigC::bind( + SigC::slot(*this, &Window::vOnJoypadConfigure), 3)); + + poMI = dynamic_cast(_poXml->get_widget("JoypadConfigure4")); + poMI->signal_activate().connect(SigC::bind( + SigC::slot(*this, &Window::vOnJoypadConfigure), 4)); + + int iDefaultJoypad = m_poInputConfig->oGetKey("active_joypad"); + for (int i = m_iJoypadMin; i <= m_iJoypadMax; i++) + { + char csName[20]; + snprintf(csName, sizeof(csName), "Joypad%d", i); + + poCMI = dynamic_cast(_poXml->get_widget(csName)); + if (i == iDefaultJoypad) + { + poCMI->set_active(); + vOnJoypadToggled(poCMI, iDefaultJoypad); + } + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnJoypadToggled), + poCMI, i)); + } + + // Autofire menu + // + struct + { + const char * m_csName; + const char * m_csKey; + const EKeyFlag m_eKeyFlag; + } + astAutofire[] = + { + { "AutofireA", "autofire_A", KeyFlagA }, + { "AutofireB", "autofire_B", KeyFlagB }, + { "AutofireL", "autofire_L", KeyFlagL }, + { "AutofireR", "autofire_R", KeyFlagR } + }; + for (guint i = 0; i < G_N_ELEMENTS(astAutofire); i++) + { + poCMI = dynamic_cast(_poXml->get_widget(astAutofire[i].m_csName)); + poCMI->set_active(m_poInputConfig->oGetKey(astAutofire[i].m_csKey)); + vOnAutofireToggled(poCMI, astAutofire[i].m_eKeyFlag); + poCMI->signal_toggled().connect(SigC::bind( + SigC::slot(*this, &Window::vOnAutofireToggled), + poCMI, astAutofire[i].m_eKeyFlag)); + } + + // GDB menu + // + poMI = dynamic_cast(_poXml->get_widget("GdbWait")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnGDBWait)); + + poMI = dynamic_cast(_poXml->get_widget("GdbLoadAndWait")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnGDBLoadAndWait)); + + poMI = dynamic_cast(_poXml->get_widget("GdbBreak")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnGDBBreak)); + + poMI = dynamic_cast(_poXml->get_widget("GdbDisconnect")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnGDBDisconnect)); + + // Help menu + // + poMI = dynamic_cast(_poXml->get_widget("HelpAbout")); + poMI->signal_activate().connect(SigC::slot(*this, &Window::vOnHelpAbout)); + + // Init widgets sensitivity + for (std::list::iterator it = m_listSensitiveWhenPlaying.begin(); + it != m_listSensitiveWhenPlaying.end(); + it++) + { + (*it)->set_sensitive(false); + } + + if (m_poInstance == NULL) + { + m_poInstance = this; + } + else + { + abort(); + } +} + +Window::~Window() +{ + vOnFileClose(); + vSaveHistoryToConfig(); + vSaveJoypadsToConfig(); + vSaveConfig(m_sConfigFile); + + if (m_poFileOpenDialog != NULL) + { + delete m_poFileOpenDialog; + } + + if (m_poKeymap != NULL) + { + delete m_poKeymap; + } + + m_poInstance = NULL; +} + +void Window::vInitSystem() +{ +#if G_BYTE_ORDER == G_LITTLE_ENDIAN + systemRedShift = 3; + systemGreenShift = 11; + systemBlueShift = 19; + RGB_LOW_BITS_MASK = 0x00010101; +#else + systemRedShift = 27; + systemGreenShift = 19; + systemBlueShift = 11; + RGB_LOW_BITS_MASK = 0x01010100; +#endif + + systemColorDepth = 32; + systemDebug = 0; + systemVerbose = 0; + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + systemFrameSkip = 0; + systemSoundOn = false; + soundOffFlag = true; + + systemRenderedFrames = 0; + systemFPS = 0; + + emulating = 0; + debugger = false; + + for (int i = 0; i < 0x10000; i++) + { +#if G_BYTE_ORDER == G_LITTLE_ENDIAN + systemColorMap32[i] = (((i & 0x1f) << systemRedShift) + | (((i & 0x3e0) >> 5) << systemGreenShift) + | (((i & 0x7c00) >> 10) << systemBlueShift)); +#else + systemColorMap32[i] = (((i & 0x1f) << systemRedShift) + | (((i & 0x3e0) >> 5) << systemGreenShift) + | (((i & 0x7c00) >> 10) << systemBlueShift)); +#endif + } + + gbFrameSkip = 0; + + for (int i = 0; i < 24; ) + { + systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10); + systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + systemGbPalette[i++] = 0; + } + + Init_2xSaI(32); +} + +void Window::vInitSDL() +{ + static bool bDone = false; + + if (bDone) + return; + + int iFlags = (SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE); + + if (SDL_Init(iFlags) < 0) + { + fprintf(stderr, "Failed to init SDL: %s", SDL_GetError()); + abort(); + } + + bDone = true; +} + +void Window::vInitConfig() +{ + m_oConfig.vClear(); + + // History section + // + m_poHistoryConfig = m_oConfig.poAddSection("History"); + m_poHistoryConfig->vSetKey("freeze", false ); + m_poHistoryConfig->vSetKey("0", "" ); + m_poHistoryConfig->vSetKey("1", "" ); + m_poHistoryConfig->vSetKey("2", "" ); + m_poHistoryConfig->vSetKey("3", "" ); + m_poHistoryConfig->vSetKey("4", "" ); + m_poHistoryConfig->vSetKey("5", "" ); + m_poHistoryConfig->vSetKey("6", "" ); + m_poHistoryConfig->vSetKey("7", "" ); + m_poHistoryConfig->vSetKey("8", "" ); + m_poHistoryConfig->vSetKey("9", "" ); + + // Directories section + // + m_poDirConfig = m_oConfig.poAddSection("Directories"); + m_poDirConfig->vSetKey("gb_roms", "" ); + m_poDirConfig->vSetKey("gba_roms", "" ); + m_poDirConfig->vSetKey("batteries", "" ); + m_poDirConfig->vSetKey("saves", "" ); + m_poDirConfig->vSetKey("captures", "" ); + + // Core section + // + m_poCoreConfig = m_oConfig.poAddSection("Core"); + m_poCoreConfig->vSetKey("load_game_auto", false ); + m_poCoreConfig->vSetKey("frameskip", "auto" ); + m_poCoreConfig->vSetKey("throttle", 0 ); + m_poCoreConfig->vSetKey("layer_bg0", true ); + m_poCoreConfig->vSetKey("layer_bg1", true ); + m_poCoreConfig->vSetKey("layer_bg2", true ); + m_poCoreConfig->vSetKey("layer_bg3", true ); + m_poCoreConfig->vSetKey("layer_obj", true ); + m_poCoreConfig->vSetKey("layer_win0", true ); + m_poCoreConfig->vSetKey("layer_win1", true ); + m_poCoreConfig->vSetKey("layer_objwin", true ); + m_poCoreConfig->vSetKey("use_bios_file", false ); + m_poCoreConfig->vSetKey("bios_file", "" ); + m_poCoreConfig->vSetKey("save_type", SaveAuto ); + m_poCoreConfig->vSetKey("flash_size", 64 ); + m_poCoreConfig->vSetKey("gb_border", true ); + m_poCoreConfig->vSetKey("gb_printer", false ); + m_poCoreConfig->vSetKey("emulator_type", EmulatorAuto ); + m_poCoreConfig->vSetKey("screenshot_format", "png" ); + + // Display section + // + m_poDisplayConfig = m_oConfig.poAddSection("Display"); + m_poDisplayConfig->vSetKey("scale", 1 ); + m_poDisplayConfig->vSetKey("show_speed", ShowPercentage ); + m_poDisplayConfig->vSetKey("pause_when_inactive", true ); + m_poDisplayConfig->vSetKey("filter2x", FilterNone ); + m_poDisplayConfig->vSetKey("filterIB", FilterIBNone ); +#ifdef MMX + m_poDisplayConfig->vSetKey("filter_disable_mmx", false ); +#endif // MMX + + // Sound section + // + m_poSoundConfig = m_oConfig.poAddSection("Sound"); + m_poSoundConfig->vSetKey("status", "on" ); + m_poSoundConfig->vSetKey("echo", false ); + m_poSoundConfig->vSetKey("low_pass", false ); + m_poSoundConfig->vSetKey("reverse_stereo", false ); + m_poSoundConfig->vSetKey("channel_1", true ); + m_poSoundConfig->vSetKey("channel_2", true ); + m_poSoundConfig->vSetKey("channel_3", true ); + m_poSoundConfig->vSetKey("channel_4", true ); + m_poSoundConfig->vSetKey("channel_A", true ); + m_poSoundConfig->vSetKey("channel_B", true ); + m_poSoundConfig->vSetKey("quality", Sound22K ); + m_poSoundConfig->vSetKey("volume", Sound100 ); + + // Input section + // + JoypadConfig oJoypadConfig; + oJoypadConfig.vSetDefault(); + m_poInputConfig = m_oConfig.poAddSection("Input"); + m_poInputConfig->vSetKey("active_joypad", m_iJoypadMin ); + for (int i = m_iJoypadMin; i <= m_iJoypadMax; i++) + { + char csPrefix[20]; + snprintf(csPrefix, sizeof(csPrefix), "joypad%d_", i); + std::string sPrefix(csPrefix); + m_poInputConfig->vSetKey(sPrefix + "up", oJoypadConfig.m_uiUp ); + m_poInputConfig->vSetKey(sPrefix + "down", oJoypadConfig.m_uiDown ); + m_poInputConfig->vSetKey(sPrefix + "left", oJoypadConfig.m_uiLeft ); + m_poInputConfig->vSetKey(sPrefix + "right", oJoypadConfig.m_uiRight ); + m_poInputConfig->vSetKey(sPrefix + "A", oJoypadConfig.m_uiA ); + m_poInputConfig->vSetKey(sPrefix + "B", oJoypadConfig.m_uiB ); + m_poInputConfig->vSetKey(sPrefix + "L", oJoypadConfig.m_uiL ); + m_poInputConfig->vSetKey(sPrefix + "R", oJoypadConfig.m_uiR ); + m_poInputConfig->vSetKey(sPrefix + "select", oJoypadConfig.m_uiSelect ); + m_poInputConfig->vSetKey(sPrefix + "start", oJoypadConfig.m_uiStart ); + m_poInputConfig->vSetKey(sPrefix + "speed", oJoypadConfig.m_uiSpeed ); + m_poInputConfig->vSetKey(sPrefix + "capture", oJoypadConfig.m_uiCapture ); + } + m_poInputConfig->vSetKey("autofire_A", false ); + m_poInputConfig->vSetKey("autofire_B", false ); + m_poInputConfig->vSetKey("autofire_L", false ); + m_poInputConfig->vSetKey("autofire_R", false ); +} + +void Window::vCheckConfig() +{ + int iValue; + int iAdjusted; + std::string sValue; + + // Directories section + // + sValue = m_poDirConfig->sGetKey("gb_roms"); + if (sValue != "" && ! Glib::file_test(sValue, Glib::FILE_TEST_IS_DIR)) + { + m_poDirConfig->vSetKey("gb_roms", ""); + } + sValue = m_poDirConfig->sGetKey("gba_roms"); + if (sValue != "" && ! Glib::file_test(sValue, Glib::FILE_TEST_IS_DIR)) + { + m_poDirConfig->vSetKey("gba_roms", ""); + } + sValue = m_poDirConfig->sGetKey("batteries"); + if (sValue != "" && ! Glib::file_test(sValue, Glib::FILE_TEST_IS_DIR)) + { + m_poDirConfig->vSetKey("batteries", ""); + } + sValue = m_poDirConfig->sGetKey("saves"); + if (sValue != "" && ! Glib::file_test(sValue, Glib::FILE_TEST_IS_DIR)) + { + m_poDirConfig->vSetKey("saves", ""); + } + sValue = m_poDirConfig->sGetKey("captures"); + if (sValue != "" && ! Glib::file_test(sValue, Glib::FILE_TEST_IS_DIR)) + { + m_poDirConfig->vSetKey("captures", ""); + } + + // Core section + // + if (m_poCoreConfig->sGetKey("frameskip") != "auto") + { + iValue = m_poCoreConfig->oGetKey("frameskip"); + iAdjusted = CLAMP(iValue, m_iFrameskipMin, m_iFrameskipMax); + if (iValue != iAdjusted) + { + m_poCoreConfig->vSetKey("frameskip", iAdjusted); + } + } + + iValue = m_poCoreConfig->oGetKey("throttle"); + if (iValue != 0) + { + iAdjusted = CLAMP(iValue, m_iThrottleMin, m_iThrottleMax); + if (iValue != iAdjusted) + { + m_poCoreConfig->vSetKey("throttle", iAdjusted); + } + } + + sValue = m_poCoreConfig->sGetKey("bios_file"); + if (sValue != "" && ! Glib::file_test(sValue, Glib::FILE_TEST_IS_REGULAR)) + { + m_poCoreConfig->vSetKey("bios_file", ""); + } + if (m_poCoreConfig->sGetKey("bios_file") == "") + { + m_poCoreConfig->vSetKey("use_bios_file", false); + } + + iValue = m_poCoreConfig->oGetKey("save_type"); + if (iValue != 0) + { + iAdjusted = CLAMP(iValue, m_iSaveTypeMin, m_iSaveTypeMax); + if (iValue != iAdjusted) + { + m_poCoreConfig->vSetKey("save_type", iAdjusted); + } + } + + iValue = m_poCoreConfig->oGetKey("flash_size"); + if (iValue != 64 && iValue != 128) + { + m_poCoreConfig->vSetKey("flash_size", 64); + } + + iValue = m_poCoreConfig->oGetKey("emulator_type"); + iAdjusted = CLAMP(iValue, m_iEmulatorTypeMin, m_iEmulatorTypeMax); + if (iValue != iAdjusted) + { + m_poCoreConfig->vSetKey("emulator_type", iAdjusted); + } + + sValue = m_poCoreConfig->sGetKey("screenshot_format"); + if (sValue != "png" && sValue != "bmp") + { + sValue = "png"; + } + + // Display section + // + iValue = m_poDisplayConfig->oGetKey("scale"); + iAdjusted = CLAMP(iValue, m_iScaleMin, m_iScaleMax); + if (iValue != iAdjusted) + { + m_poDisplayConfig->vSetKey("scale", iAdjusted); + } + + iValue = m_poDisplayConfig->oGetKey("show_speed"); + iAdjusted = CLAMP(iValue, m_iShowSpeedMin, m_iShowSpeedMax); + if (iValue != iAdjusted) + { + m_poDisplayConfig->vSetKey("show_speed", iAdjusted); + } + + iValue = m_poDisplayConfig->oGetKey("filter2x"); + iAdjusted = CLAMP(iValue, m_iFilter2xMin, m_iFilter2xMax); + if (iValue != iAdjusted) + { + m_poDisplayConfig->vSetKey("filter2x", iAdjusted); + } + + iValue = m_poDisplayConfig->oGetKey("filterIB"); + iAdjusted = CLAMP(iValue, m_iFilterIBMin, m_iFilterIBMax); + if (iValue != iAdjusted) + { + m_poDisplayConfig->vSetKey("filterIB", iAdjusted); + } + + // Sound section + // + sValue = m_poSoundConfig->sGetKey("status"); + if (sValue != "off" && sValue != "on" && sValue != "mute") + { + m_poSoundConfig->vSetKey("status", "on"); + } + + iValue = m_poSoundConfig->oGetKey("quality"); + iAdjusted = CLAMP(iValue, m_iSoundQualityMin, m_iSoundQualityMax); + if (iValue != iAdjusted) + { + m_poSoundConfig->vSetKey("quality", iAdjusted); + } + + iValue = m_poSoundConfig->oGetKey("volume"); + iAdjusted = CLAMP(iValue, m_iSoundVolumeMin, m_iSoundVolumeMax); + if (iValue != iAdjusted) + { + m_poSoundConfig->vSetKey("volume", iAdjusted); + } + + // Input section + // + iValue = m_poInputConfig->oGetKey("active_joypad"); + iAdjusted = CLAMP(iValue, m_iJoypadMin, m_iJoypadMax); + if (iValue != iAdjusted) + { + m_poInputConfig->vSetKey("active_joypad", iAdjusted); + } +} + +void Window::vLoadConfig(const std::string & _rsFile) +{ + try + { + m_oConfig.vLoad(_rsFile, false, false); + } + catch (const Glib::Error & e) + { + vPopupError(e.what().c_str()); + } +} + +void Window::vSaveConfig(const std::string & _rsFile) +{ + try + { + m_oConfig.vSave(_rsFile); + } + catch (const Glib::Error & e) + { + vPopupError(e.what().c_str()); + } +} + +void Window::vLoadHistoryFromConfig() +{ + char csKey[] = "0"; + for (int i = 0; i < 10; i++, csKey[0]++) + { + std::string sFile = m_poHistoryConfig->sGetKey(csKey); + if (sFile == "") + { + break; + } + m_listHistory.push_back(sFile); + } +} + +void Window::vSaveHistoryToConfig() +{ + char csKey[] = "0"; + for (std::list::const_iterator it = m_listHistory.begin(); + it != m_listHistory.end(); + it++, csKey[0]++) + { + m_poHistoryConfig->vSetKey(csKey, *it); + } +} + +void Window::vHistoryAdd(const std::string & _rsFile) +{ + if (m_poHistoryConfig->oGetKey("freeze")) + { + return; + } + + m_listHistory.remove(_rsFile); + m_listHistory.push_front(_rsFile); + if (m_listHistory.size() > 10) + { + m_listHistory.pop_back(); + } + + vUpdateHistoryMenu(); +} + +void Window::vClearHistoryMenu() +{ + Gtk::Menu_Helpers::MenuList::iterator it = m_poRecentMenu->items().begin(); + for (int i = 0; i < 3; i++, it++) + ; + + m_poRecentMenu->items().erase(it, m_poRecentMenu->items().end()); +} + +void Window::vUpdateHistoryMenu() +{ + vClearHistoryMenu(); + + guint uiAccelKey = GDK_F1; + for (std::list::const_iterator it = m_listHistory.begin(); + it != m_listHistory.end(); + it++, uiAccelKey++) + { + Gtk::Image * poImage = Gtk::manage(new Gtk::Image(Gtk::Stock::OPEN, Gtk::ICON_SIZE_MENU)); + Glib::ustring sLabel = Glib::path_get_basename(*it); + VBA::ImageMenuItem * poIMI = Gtk::manage(new VBA::ImageMenuItem(*poImage, sLabel)); + + m_oTooltips.set_tip(*poIMI, *it); + + poIMI->signal_activate().connect(SigC::bind( + SigC::slot(*this, &Window::vOnRecentFile), + *it)); + + poIMI->set_accel_key(Gtk::AccelKey(uiAccelKey, Gdk::CONTROL_MASK)); + poIMI->accelerate(*this); + + poIMI->show(); + m_poRecentMenu->items().push_back(*poIMI); + } +} + +void Window::vLoadJoypadsFromConfig() +{ + m_oJoypads.clear(); + + for (int i = m_iJoypadMin; i <= m_iJoypadMax; i++) + { + char csPrefix[20]; + snprintf(csPrefix, sizeof(csPrefix), "joypad%d_", i); + std::string sPrefix(csPrefix); + + JoypadConfig oJoypadConfig; + oJoypadConfig.m_uiUp = m_poInputConfig->oGetKey(sPrefix + "up"); + oJoypadConfig.m_uiDown = m_poInputConfig->oGetKey(sPrefix + "down"); + oJoypadConfig.m_uiLeft = m_poInputConfig->oGetKey(sPrefix + "left"); + oJoypadConfig.m_uiRight = m_poInputConfig->oGetKey(sPrefix + "right"); + oJoypadConfig.m_uiA = m_poInputConfig->oGetKey(sPrefix + "A"); + oJoypadConfig.m_uiB = m_poInputConfig->oGetKey(sPrefix + "B"); + oJoypadConfig.m_uiL = m_poInputConfig->oGetKey(sPrefix + "L"); + oJoypadConfig.m_uiR = m_poInputConfig->oGetKey(sPrefix + "R"); + oJoypadConfig.m_uiSelect = m_poInputConfig->oGetKey(sPrefix + "select"); + oJoypadConfig.m_uiStart = m_poInputConfig->oGetKey(sPrefix + "start"); + oJoypadConfig.m_uiSpeed = m_poInputConfig->oGetKey(sPrefix + "speed"); + oJoypadConfig.m_uiCapture = m_poInputConfig->oGetKey(sPrefix + "capture"); + + m_oJoypads.push_back(oJoypadConfig); + } +} + +void Window::vSaveJoypadsToConfig() +{ + for (int i = m_iJoypadMin; i <= m_iJoypadMax; i++) + { + char csPrefix[20]; + snprintf(csPrefix, sizeof(csPrefix), "joypad%d_", i); + std::string sPrefix(csPrefix); + + m_poInputConfig->vSetKey(sPrefix + "up", m_oJoypads[i - 1].m_uiUp ); + m_poInputConfig->vSetKey(sPrefix + "down", m_oJoypads[i - 1].m_uiDown ); + m_poInputConfig->vSetKey(sPrefix + "left", m_oJoypads[i - 1].m_uiLeft ); + m_poInputConfig->vSetKey(sPrefix + "right", m_oJoypads[i - 1].m_uiRight ); + m_poInputConfig->vSetKey(sPrefix + "A", m_oJoypads[i - 1].m_uiA ); + m_poInputConfig->vSetKey(sPrefix + "B", m_oJoypads[i - 1].m_uiB ); + m_poInputConfig->vSetKey(sPrefix + "L", m_oJoypads[i - 1].m_uiL ); + m_poInputConfig->vSetKey(sPrefix + "R", m_oJoypads[i - 1].m_uiR ); + m_poInputConfig->vSetKey(sPrefix + "select", m_oJoypads[i - 1].m_uiSelect ); + m_poInputConfig->vSetKey(sPrefix + "start", m_oJoypads[i - 1].m_uiStart ); + m_poInputConfig->vSetKey(sPrefix + "speed", m_oJoypads[i - 1].m_uiSpeed ); + m_poInputConfig->vSetKey(sPrefix + "capture", m_oJoypads[i - 1].m_uiCapture ); + } +} + +void Window::vUpdateScreen() +{ + if (m_eCartridge == CartridgeGB) + { + if (gbBorderOn) + { + m_iScreenWidth = m_iSGBScreenWidth; + m_iScreenHeight = m_iSGBScreenHeight; + gbBorderLineSkip = m_iSGBScreenWidth; + gbBorderColumnSkip = (m_iSGBScreenWidth - m_iGBScreenWidth) / 2; + gbBorderRowSkip = (m_iSGBScreenHeight - m_iGBScreenHeight) / 2; + } + else + { + m_iScreenWidth = m_iGBScreenWidth; + m_iScreenHeight = m_iGBScreenHeight; + gbBorderLineSkip = m_iGBScreenWidth; + gbBorderColumnSkip = 0; + gbBorderRowSkip = 0; + } + } + else if (m_eCartridge == CartridgeGBA) + { + m_iScreenWidth = m_iGBAScreenWidth; + m_iScreenHeight = m_iGBAScreenHeight; + } + + g_return_if_fail(m_iScreenWidth >= 1 && m_iScreenHeight >= 1); + + m_poScreenArea->vSetSize(m_iScreenWidth, m_iScreenHeight); + m_poScreenArea->vSetScale(m_poDisplayConfig->oGetKey("scale")); + + resize(1, 1); + + if (emulating) + { + vDrawScreen(); + } + else + { + vDrawDefaultScreen(); + } +} + +bool Window::bLoadROM(const std::string & _rsFile) +{ + vOnFileClose(); + + m_sRomFile = _rsFile; + const char * csFile = _rsFile.c_str(); + + IMAGE_TYPE eType = utilFindType(csFile); + if (eType == IMAGE_UNKNOWN) + { + vPopupError(_("Unknown file type %s"), csFile); + return false; + } + + bool bLoaded = false; + if (eType == IMAGE_GB) + { + bLoaded = gbLoadRom(csFile); + if (bLoaded) + { + m_eCartridge = CartridgeGB; + m_stEmulator = GBSystem; + } + } + else if (eType == IMAGE_GBA) + { + int iSize = CPULoadRom(csFile); + bLoaded = (iSize > 0); + if (bLoaded) + { + m_eCartridge = CartridgeGBA; + m_stEmulator = GBASystem; + + useBios = m_poCoreConfig->oGetKey("use_bios_file"); + //CPUInit(m_poCoreConfig->sGetKey("bios_file").c_str(), useBios); + CPUInit(); + CPUReset(); + + // If the bios file was rejected by CPUInit + if (m_poCoreConfig->oGetKey("use_bios_file") && ! useBios) + { + m_poUseBiosItem->set_active(false); + m_poUseBiosItem->set_sensitive(false); + m_poCoreConfig->vSetKey("bios_file", ""); + } + } + } + + if (! bLoaded) + { + return false; + } + + vLoadBattery(); + vUpdateScreen(); + + debugger = false; // May cause conflicts + emulating = 1; + m_bWasEmulating = false; + m_uiThrottleDelay = 0; + + if (m_eCartridge == CartridgeGBA) + { + soundSetQuality(m_eSoundQuality); + } + else + { + gbSoundSetQuality(m_eSoundQuality); + } + + vUpdateGameSlots(); + vHistoryAdd(_rsFile); + + for (std::list::iterator it = m_listSensitiveWhenPlaying.begin(); + it != m_listSensitiveWhenPlaying.end(); + it++) + { + (*it)->set_sensitive(); + } + + if (m_poCoreConfig->oGetKey("load_game_auto")) + { + vOnLoadGameMostRecent(); + } + + vStartEmu(); + + return true; +} + +void Window::vPopupError(const char * _csFormat, ...) +{ + va_list args; + va_start(args, _csFormat); + char * csMsg = g_strdup_vprintf(_csFormat, args); + va_end(args); + + Gtk::MessageDialog oDialog(*this, + csMsg, +#ifndef GTKMM20 + false, +#endif // ! GTKMM20 + Gtk::MESSAGE_ERROR, + Gtk::BUTTONS_OK); + oDialog.run(); + g_free(csMsg); +} + +void Window::vPopupErrorV(const char * _csFormat, va_list _args) +{ + char * csMsg = g_strdup_vprintf(_csFormat, _args); + + Gtk::MessageDialog oDialog(*this, + csMsg, +#ifndef GTKMM20 + false, +#endif // ! GTKMM20 + Gtk::MESSAGE_ERROR, + Gtk::BUTTONS_OK); + oDialog.run(); + g_free(csMsg); +} + +void Window::vDrawScreen() +{ + m_poScreenArea->vDrawPixels(pix); +} + +void Window::vDrawDefaultScreen() +{ + m_poScreenArea->vDrawColor(0x000000); // Black +} + +void Window::vSetDefaultTitle() +{ + set_title("VBA"); +} + +void Window::vShowSpeed(int _iSpeed) +{ + char csTitle[50]; + + if (m_eShowSpeed == ShowPercentage) + { + snprintf(csTitle, 50, "VBA - %d%%", _iSpeed); + set_title(csTitle); + } + else if (m_eShowSpeed == ShowDetailed) + { + snprintf(csTitle, 50, "VBA - %d%% (%d, %d fps)", + _iSpeed, systemFrameSkip, systemFPS); + set_title(csTitle); + } +} + +void Window::vComputeFrameskip(int _iRate) +{ + static u32 uiLastTime = 0; + static int iFrameskipAdjust = 0; + + u32 uiTime = SDL_GetTicks(); + + if (m_bWasEmulating) + { + int iWantedSpeed = 100; + + if (m_iThrottle > 0) + { + if (! speedup) + { + u32 uiDiff = uiTime - m_uiThrottleLastTime; + int iTarget = 1000000 / (_iRate * m_iThrottle); + int iDelay = iTarget - uiDiff; + if (iDelay > 0) + { + m_uiThrottleDelay = iDelay; + } + } + iWantedSpeed = m_iThrottle; + } + + if (m_bAutoFrameskip) + { + u32 uiDiff = uiTime - uiLastTime; + int iSpeed = iWantedSpeed; + + if (uiDiff != 0) + { + iSpeed = (1000000 / _iRate) / uiDiff; + } + + if (iSpeed >= iWantedSpeed - 2) + { + iFrameskipAdjust++; + if (iFrameskipAdjust >= 3) + { + iFrameskipAdjust = 0; + if (systemFrameSkip > 0) + { + systemFrameSkip--; + } + } + } + else + { + if (iSpeed < iWantedSpeed - 20) + { + iFrameskipAdjust -= ((iWantedSpeed - 10) - iSpeed) / 5; + } + else if (systemFrameSkip < 9) + { + iFrameskipAdjust--; + } + + if (iFrameskipAdjust <= -2) + { + iFrameskipAdjust += 2; + if (systemFrameSkip < 9) + { + systemFrameSkip++; + } + } + } + } + } + else + { + m_bWasEmulating = true; + } + + uiLastTime = uiTime; + m_uiThrottleLastTime = uiTime; +} + +void Window::vCaptureScreen(int _iNum) +{ + std::string sBaseName; + std::string sCaptureDir = m_poDirConfig->sGetKey("captures"); + if (sCaptureDir == "") + { + sBaseName = sCutSuffix(m_sRomFile); + } + else + { + sBaseName = sCaptureDir + "/" + sCutSuffix(Glib::path_get_basename(m_sRomFile)); + } + std::string sFormat = m_poCoreConfig->sGetKey("screenshot_format"); + + char * csFile = g_strdup_printf("%s_%02d.%s", + sBaseName.c_str(), + _iNum, + sFormat.c_str()); + if (sFormat == "png") + { + m_stEmulator.emuWritePNG(csFile); + } + else + { + m_stEmulator.emuWriteBMP(csFile); + } + g_free(csFile); +} + +u32 Window::uiReadJoypad() +{ + u32 uiJoypad = m_uiJoypadState; + + if (m_uiAutofireState != 0) + { + uiJoypad &= ~m_uiAutofireState; + if (m_bAutofireToggle) + { + uiJoypad |= m_uiAutofireState; + } + m_bAutofireToggle = ! m_bAutofireToggle; + } + + return uiJoypad; +} + +void Window::vCreateFileOpenDialog() +{ + if (m_poFileOpenDialog != NULL) + { + return; + } + + std::string sGBDir = m_poDirConfig->sGetKey("gb_roms"); + std::string sGBADir = m_poDirConfig->sGetKey("gba_roms"); + +#ifdef GTKMM20 + + Gtk::FileSelection * poDialog = new Gtk::FileSelection(_("Open")); + poDialog->set_transient_for(*this); + + if (sGBADir != "") + { + poDialog->set_filename(sGBADir + "/"); + } + else if (sGBDir != "") + { + poDialog->set_filename(sGBDir + "/"); + } + +#else // ! GTKMM20 + + Gtk::FileChooserDialog * poDialog = new Gtk::FileChooserDialog(*this, _("Open")); + poDialog->add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + poDialog->add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + + if (sGBDir != "") + { + poDialog->add_shortcut_folder(sGBDir); + poDialog->set_current_folder(sGBDir); + } + + if (sGBADir != "" && sGBADir != sGBDir) + { + poDialog->add_shortcut_folder(sGBADir); + poDialog->set_current_folder(sGBADir); + } + + const char * acsPattern[] = + { + // GBA + "*.[bB][iI][nN]", "*.[aA][gG][bB]", "*.[gG][bB][aA]", + // GB + "*.[gG][bB]", "*.[sS][gG][bB]", "*.[cC][gG][bB]", "*.[gG][bB][cC]", + // Both + "*.[mM][bB]", "*.[eE][lL][fF]", "*.[zZ][iI][pP]", "*.[zZ]", "*.[gG][zZ]" + }; + + Gtk::FileFilter oAllGBAFilter; + oAllGBAFilter.set_name(_("All Gameboy Advance files")); + for (guint i = 0; i < G_N_ELEMENTS(acsPattern); i++) + { + oAllGBAFilter.add_pattern(acsPattern[i]); + } + + Gtk::FileFilter oGBAFilter; + oGBAFilter.set_name(_("Gameboy Advance files")); + for (int i = 0; i < 3; i++) + { + oGBAFilter.add_pattern(acsPattern[i]); + } + + Gtk::FileFilter oGBFilter; + oGBFilter.set_name(_("Gameboy files")); + for (int i = 3; i < 7; i++) + { + oGBFilter.add_pattern(acsPattern[i]); + } + + poDialog->add_filter(oAllGBAFilter); + poDialog->add_filter(oGBAFilter); + poDialog->add_filter(oGBFilter); + +#endif // ! GTKMM20 + + m_poFileOpenDialog = poDialog; +} + +void Window::vLoadBattery() +{ + std::string sBattery; + std::string sDir = m_poDirConfig->sGetKey("batteries"); + if (sDir == "") + { + sBattery = sCutSuffix(m_sRomFile) + ".sav"; + } + else + { + sBattery = sDir + "/" + sCutSuffix(Glib::path_get_basename(m_sRomFile)) + ".sav"; + } + + if (m_stEmulator.emuReadBattery(sBattery.c_str())) + { + systemScreenMessage(_("Loaded battery")); + } +} + +void Window::vSaveBattery() +{ + std::string sBattery; + std::string sDir = m_poDirConfig->sGetKey("batteries"); + if (sDir == "") + { + sBattery = sCutSuffix(m_sRomFile) + ".sav"; + } + else + { + sBattery = sDir + "/" + sCutSuffix(Glib::path_get_basename(m_sRomFile)) + ".sav"; + } + + if (m_stEmulator.emuWriteBattery(sBattery.c_str())) + { + systemScreenMessage(_("Saved battery")); + } +} + +void Window::vStartEmu() +{ + if (m_oEmuSig.connected()) + { + return; + } + + m_oEmuSig = Glib::signal_idle().connect(SigC::slot(*this, &Window::bOnEmuIdle), + Glib::PRIORITY_DEFAULT_IDLE); +} + +void Window::vStopEmu() +{ + m_oEmuSig.disconnect(); + m_bWasEmulating = false; +} + +void Window::vSetThrottle(int _iPercent) +{ + m_iThrottle = _iPercent; + m_poCoreConfig->vSetKey("throttle", _iPercent); +} + +void Window::vSelectBestThrottleItem() +{ + struct + { + const char * m_csName; + const int m_iThrottle; + } + astThrottle[] = + { + { "ThrottleNoThrottle", 0 }, + { "Throttle25", 25 }, + { "Throttle50", 50 }, + { "Throttle100", 100 }, + { "Throttle150", 150 }, + { "Throttle200", 200 } + }; + for (guint i = 0; i < G_N_ELEMENTS(astThrottle); i++) + { + Gtk::CheckMenuItem * poCMI; + poCMI = dynamic_cast(m_poXml->get_widget(astThrottle[i].m_csName)); + if (astThrottle[i].m_iThrottle == m_iThrottle) + { + poCMI->set_active(); + } + } +} + +void Window::vUpdateGameSlots() +{ + if (m_eCartridge == CartridgeNone) + { + std::string sDateTime = _("----/--/-- --:--:--"); + + for (int i = 0; i < 10; i++) + { + char csPrefix[10]; + snprintf(csPrefix, sizeof(csPrefix), "%2d ", i + 1); + + Gtk::Label * poLabel; + poLabel = dynamic_cast(m_apoLoadGameItem[i]->get_child()); + poLabel->set_text(csPrefix + sDateTime); + m_apoLoadGameItem[i]->set_sensitive(false); + + poLabel = dynamic_cast(m_apoSaveGameItem[i]->get_child()); + poLabel->set_text(csPrefix + sDateTime); + m_apoSaveGameItem[i]->set_sensitive(false); + + m_astGameSlot[i].m_bEmpty = true; + } + } + else + { + std::string sFileBase; + std::string sDir = m_poDirConfig->sGetKey("saves"); + if (sDir == "") + { + sFileBase = sCutSuffix(m_sRomFile); + } + else + { + sFileBase = sDir + "/" + sCutSuffix(Glib::path_get_basename(m_sRomFile)); + } + + const char * csDateFormat = _("%Y/%m/%d %H:%M:%S"); + + for (int i = 0; i < 10; i++) + { + char csPrefix[10]; + snprintf(csPrefix, sizeof(csPrefix), "%2d ", i + 1); + + char csSlot[10]; + snprintf(csSlot, sizeof(csSlot), "%d", i + 1); + m_astGameSlot[i].m_sFile = sFileBase + csSlot + ".sgm"; + + std::string sDateTime; + struct stat stStat; + if (stat(m_astGameSlot[i].m_sFile.c_str(), &stStat) == -1) + { + sDateTime = _("----/--/-- --:--:--"); + m_astGameSlot[i].m_bEmpty = true; + } + else + { + char csDateTime[30]; + strftime(csDateTime, sizeof(csDateTime), csDateFormat, + localtime(&stStat.st_mtime)); + sDateTime = csDateTime; + m_astGameSlot[i].m_bEmpty = false; + m_astGameSlot[i].m_uiTime = stStat.st_mtime; + } + + Gtk::Label * poLabel; + poLabel = dynamic_cast(m_apoLoadGameItem[i]->get_child()); + poLabel->set_text(csPrefix + sDateTime); + m_apoLoadGameItem[i]->set_sensitive(! m_astGameSlot[i].m_bEmpty); + + poLabel = dynamic_cast(m_apoSaveGameItem[i]->get_child()); + poLabel->set_text(csPrefix + sDateTime); + m_apoSaveGameItem[i]->set_sensitive(); + } + } +} + +} // VBA namespace diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/window.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/window.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,322 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_WINDOW_H__ +#define __VBA_WINDOW_H__ + +#include +#include + +#include +#include + +#ifndef GTKMM20 +# include "sigccompat.h" +#endif // ! GTKMM20 + +#include +#include +#include + +#include "../common/System.h" + +#include "configfile.h" +#include "screenarea.h" +#include "filters.h" +#include "input.h" +#include "joypadconfig.h" + +namespace VBA +{ + +class Window : public Gtk::Window +{ + friend class Gnome::Glade::Xml; + +public: + virtual ~Window(); + + inline static Window * poGetInstance() { return m_poInstance; } + + enum ECartridge + { + CartridgeNone, + CartridgeGB, + CartridgeGBA + }; + + // GB/GBA screen sizes + const int m_iGBScreenWidth; + const int m_iGBScreenHeight; + const int m_iSGBScreenWidth; + const int m_iSGBScreenHeight; + const int m_iGBAScreenWidth; + const int m_iGBAScreenHeight; + + bool bLoadROM(const std::string & _rsFile); + void vPopupError(const char * _csFormat, ...); + void vPopupErrorV(const char * _csFormat, va_list _args); + void vDrawScreen(); + void vComputeFrameskip(int _iRate); + void vShowSpeed(int _iSpeed); + void vCaptureScreen(int _iNum); + u32 uiReadJoypad(); + + inline ECartridge eGetCartridge() const { return m_eCartridge; } + inline int iGetThrottle() const { return m_iThrottle; } + +protected: + Window(GtkWindow * _pstWindow, + const Glib::RefPtr & _poXml); + + enum EShowSpeed + { + ShowNone, + ShowPercentage, + ShowDetailed + }; + + enum ESaveType + { + SaveAuto, + SaveEEPROM, + SaveSRAM, + SaveFlash, + SaveEEPROMSensor, + SaveNone + }; + + enum ESoundStatus + { + SoundOff, + SoundMute, + SoundOn + }; + + enum ESoundQuality + { + Sound44K = 1, + Sound22K = 2, + Sound11K = 4 + }; + + enum ESoundVolume + { + Sound100, + Sound200, + Sound300, + Sound400, + Sound25, + Sound50 + }; + + enum EEmulatorType + { + EmulatorAuto, + EmulatorCGB, + EmulatorSGB, + EmulatorGB, + EmulatorGBA, + EmulatorSGB2 + }; + + virtual void vOnFileOpen(); + virtual void vOnFileLoad(); + virtual void vOnFileSave(); + virtual void vOnLoadGameMostRecent(); + virtual void vOnLoadGameAutoToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnLoadGame(int _iSlot); + virtual void vOnSaveGameOldest(); + virtual void vOnSaveGame(int _iSlot); + virtual void vOnFilePauseToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnFileReset(); + virtual void vOnRecentReset(); + virtual void vOnRecentFreezeToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnRecentFile(std::string _sFile); + virtual void vOnImportBatteryFile(); + virtual void vOnExportBatteryFile(); + virtual void vOnFileScreenCapture(); + virtual void vOnFileClose(); + virtual void vOnFileExit(); + virtual void vOnFrameskipToggled(Gtk::CheckMenuItem * _poCMI, int _iValue); + virtual void vOnThrottleToggled(Gtk::CheckMenuItem * _poCMI, int _iPercent); + virtual void vOnThrottleOther(Gtk::CheckMenuItem * _poCMI); + virtual void vOnVideoScaleToggled(Gtk::CheckMenuItem * _poCMI, int _iScale); + virtual void vOnLayerToggled(Gtk::CheckMenuItem * _poCMI, int _iLayer); + virtual void vOnDirectories(); + virtual void vOnDirectoryReset(Gtk::Entry * _poEntry); + virtual void vOnDirectorySelect(Gtk::Entry * _poEntry); + virtual void vOnPauseWhenInactiveToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnSelectBios(); + virtual void vOnUseBiosToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnShowSpeedToggled(Gtk::CheckMenuItem * _poCMI, int _iShowSpeed); + virtual void vOnSaveTypeToggled(Gtk::CheckMenuItem * _poCMI, int _iSaveType); + virtual void vOnFlashSizeToggled(Gtk::CheckMenuItem * _poCMI, int _iFlashSize); + virtual void vOnScreenshotFormatToggled(Gtk::CheckMenuItem * _poCMI, std::string _sFormat); + virtual void vOnSoundStatusToggled(Gtk::CheckMenuItem * _poCMI, int _iSoundStatus); + virtual void vOnSoundEchoToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnSoundLowPassToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnSoundReverseToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnSoundChannelToggled(Gtk::CheckMenuItem * _poCMI, int _iSoundChannel); + virtual void vOnSoundQualityToggled(Gtk::CheckMenuItem * _poCMI, int _iSoundQuality); + virtual void vOnSoundVolumeToggled(Gtk::CheckMenuItem * _poCMI, int _iSoundVolume); + virtual void vOnGBBorderToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnGBPrinterToggled(Gtk::CheckMenuItem * _poCMI); + virtual void vOnEmulatorTypeToggled(Gtk::CheckMenuItem * _poCMI, int _iEmulatorType); + virtual void vOnFilter2xToggled(Gtk::CheckMenuItem * _poCMI, int _iFilter2x); + virtual void vOnFilterIBToggled(Gtk::CheckMenuItem * _poCMI, int _iFilterIB); +#ifdef MMX + virtual void vOnDisableMMXToggled(Gtk::CheckMenuItem * _poCMI); +#endif // MMX + virtual void vOnJoypadConfigure(int _iJoypad); + virtual void vOnJoypadToggled(Gtk::CheckMenuItem * _poCMI, int _iJoypad); + virtual void vOnAutofireToggled(Gtk::CheckMenuItem * _poCMI, u32 _uiKeyFlag); + virtual void vOnGDBWait(); + virtual void vOnGDBLoadAndWait(); + virtual void vOnGDBBreak(); + virtual void vOnGDBDisconnect(); + virtual void vOnHelpAbout(); + virtual bool bOnEmuIdle(); + + virtual bool on_focus_in_event(GdkEventFocus * _pstEvent); + virtual bool on_focus_out_event(GdkEventFocus * _pstEvent); + virtual bool on_key_press_event(GdkEventKey * _pstEvent); + virtual bool on_key_release_event(GdkEventKey * _pstEvent); + +private: + // Config limits + const int m_iFrameskipMin; + const int m_iFrameskipMax; + const int m_iThrottleMin; + const int m_iThrottleMax; + const int m_iScaleMin; + const int m_iScaleMax; + const int m_iShowSpeedMin; + const int m_iShowSpeedMax; + const int m_iSaveTypeMin; + const int m_iSaveTypeMax; + const int m_iSoundQualityMin; + const int m_iSoundQualityMax; + const int m_iSoundVolumeMin; + const int m_iSoundVolumeMax; + const int m_iEmulatorTypeMin; + const int m_iEmulatorTypeMax; + const int m_iFilter2xMin; + const int m_iFilter2xMax; + const int m_iFilterIBMin; + const int m_iFilterIBMax; + const int m_iJoypadMin; + const int m_iJoypadMax; + + static Window * m_poInstance; + + Glib::RefPtr m_poXml; + + std::string m_sUserDataDir; + std::string m_sConfigFile; + Config::File m_oConfig; + Config::Section * m_poHistoryConfig; + Config::Section * m_poDirConfig; + Config::Section * m_poCoreConfig; + Config::Section * m_poDisplayConfig; + Config::Section * m_poSoundConfig; + Config::Section * m_poInputConfig; + +#ifdef GTKMM20 + Gtk::FileSelection * m_poFileOpenDialog; +#else // ! GTKMM20 + Gtk::FileChooserDialog * m_poFileOpenDialog; +#endif // ! GTKMM20 + ScreenArea * m_poScreenArea; + Gtk::Menu * m_poRecentMenu; + Gtk::MenuItem * m_poRecentResetItem; + Gtk::CheckMenuItem * m_poFilePauseItem; + Gtk::CheckMenuItem * m_poUseBiosItem; + Gtk::CheckMenuItem * m_poSoundOffItem; + + struct SGameSlot + { + bool m_bEmpty; + std::string m_sFile; + time_t m_uiTime; + }; + + Gtk::MenuItem * m_apoLoadGameItem[10]; + Gtk::MenuItem * m_apoSaveGameItem[10]; + SGameSlot m_astGameSlot[10]; + + std::list m_listHistory; + + std::list m_listSensitiveWhenPlaying; + + Gtk::Tooltips m_oTooltips; + + SigC::connection m_oEmuSig; + + std::vector m_oJoypads; + Keymap * m_poKeymap; + + int m_iScreenWidth; + int m_iScreenHeight; + + std::string m_sRomFile; + ECartridge m_eCartridge; + EmulatedSystem m_stEmulator; + u32 m_uiJoypadState; + u32 m_uiAutofireState; + bool m_bAutofireToggle; + bool m_bPaused; + bool m_bWasEmulating; + bool m_bAutoFrameskip; + int m_iThrottle; + u32 m_uiThrottleLastTime; + u32 m_uiThrottleDelay; + EShowSpeed m_eShowSpeed; + ESoundQuality m_eSoundQuality; + + void vInitSystem(); + void vInitSDL(); + void vInitConfig(); + void vCheckConfig(); + void vLoadConfig(const std::string & _rsFile); + void vSaveConfig(const std::string & _rsFile); + void vLoadHistoryFromConfig(); + void vSaveHistoryToConfig(); + void vHistoryAdd(const std::string & _rsFile); + void vClearHistoryMenu(); + void vUpdateHistoryMenu(); + void vLoadJoypadsFromConfig(); + void vSaveJoypadsToConfig(); + void vUpdateScreen(); + void vDrawDefaultScreen(); + void vSetDefaultTitle(); + void vCreateFileOpenDialog(); + void vLoadBattery(); + void vSaveBattery(); + void vStartEmu(); + void vStopEmu(); + void vSetThrottle(int _iPercent); + void vSelectBestThrottleItem(); + void vUpdateGameSlots(); +}; + +} // namespace VBA + + +#endif // __VBA_WINDOW_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/gtk/windowcallbacks.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gtk/windowcallbacks.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1582 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "window.h" + +#include + +#include +#include + +#include + +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gba/Flash.h" +#include "../gba/GBASound.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +#include "../gb/gbPrinter.h" +#include "../common/Util.h" + +#include "tools.h" +#include "intl.h" + +extern int systemRenderedFrames; +extern int systemFPS; +extern bool debugger; +extern int RGB_LOW_BITS_MASK; +extern void (*dbgMain)(); +extern void (*dbgSignal)(int, int); +extern void (*dbgOutput)(char *, u32); +extern void remoteInit(); +extern void remoteCleanUp(); +extern void remoteStubMain(); +extern void remoteStubSignal(int, int); +extern void remoteOutput(char *, u32); +extern void remoteSetProtocol(int); +extern void remoteSetPort(int); + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif // MMX + +namespace VBA +{ + +using Gnome::Glade::Xml; + +void Window::vOnFileOpen() +{ + while (m_poFileOpenDialog->run() == Gtk::RESPONSE_OK) + { + if (bLoadROM(m_poFileOpenDialog->get_filename())) + { + break; + } + } + m_poFileOpenDialog->hide(); +} + +void Window::vOnFileLoad() +{ + std::string sSaveDir = m_poDirConfig->sGetKey("saves"); + +#ifdef GTKMM20 + + Gtk::FileSelection oDialog(_("Load game")); + oDialog.set_transient_for(*this); + + if (sSaveDir == "") + { + oDialog.set_filename(Glib::path_get_dirname(m_sRomFile) + "/"); + } + else + { + oDialog.set_filename(sSaveDir + "/"); + } + +#else // ! GTKMM20 + + Gtk::FileChooserDialog oDialog(*this, _("Load game")); + oDialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + oDialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + + if (sSaveDir == "") + { + oDialog.set_current_folder(Glib::path_get_dirname(m_sRomFile)); + } + else + { + oDialog.set_current_folder(sSaveDir); + oDialog.add_shortcut_folder(sSaveDir); + } + + Gtk::FileFilter oSaveFilter; + oSaveFilter.set_name(_("VisualBoyAdvance save game")); + oSaveFilter.add_pattern("*.[sS][gG][mM]"); + + oDialog.add_filter(oSaveFilter); + +#endif // ! GTKMM20 + + while (oDialog.run() == Gtk::RESPONSE_OK) + { + if (m_stEmulator.emuReadState(oDialog.get_filename().c_str())) + { + break; + } + } +} + +void Window::vOnFileSave() +{ + Glib::ustring sSaveDir = m_poDirConfig->sGetKey("saves"); + +#ifdef GTKMM20 + + Gtk::FileSelection oDialog(_("Save game")); + oDialog.set_transient_for(*this); + + if (sSaveDir == "") + { + oDialog.set_filename(sCutSuffix(m_sRomFile)); + } + else + { + oDialog.set_filename(sSaveDir + "/" + + sCutSuffix(Glib::path_get_basename(m_sRomFile))); + } + +#else // ! GTKMM20 + + Gtk::FileChooserDialog oDialog(*this, _("Save game"), + Gtk::FILE_CHOOSER_ACTION_SAVE); + oDialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + oDialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); + + if (sSaveDir == "") + { + oDialog.set_current_folder(Glib::path_get_dirname(m_sRomFile)); + } + else + { + oDialog.set_current_folder(sSaveDir); + oDialog.add_shortcut_folder(sSaveDir); + } + oDialog.set_current_name(sCutSuffix(Glib::path_get_basename(m_sRomFile))); + + Gtk::FileFilter oSaveFilter; + oSaveFilter.set_name(_("VisualBoyAdvance save game")); + oSaveFilter.add_pattern("*.[sS][gG][mM]"); + + oDialog.add_filter(oSaveFilter); + +#endif // ! GTKMM20 + + while (oDialog.run() == Gtk::RESPONSE_OK) + { + Glib::ustring sFile = oDialog.get_filename(); + if (! bHasSuffix(sFile, ".sgm", false)) + { + sFile += ".sgm"; + } + + if (Glib::file_test(sFile, Glib::FILE_TEST_EXISTS)) + { + Gtk::MessageDialog oConfirmDialog(*this, + _("File already exists. Overwrite it?"), +#ifndef GTKMM20 + false, +#endif // ! GTKMM20 + Gtk::MESSAGE_QUESTION, + Gtk::BUTTONS_YES_NO); + if (oConfirmDialog.run() != Gtk::RESPONSE_YES) + { + continue; + } + } + + if (m_stEmulator.emuWriteState(sFile.c_str())) + { + break; + } + } +} + +void Window::vOnLoadGameMostRecent() +{ + int iMostRecent = -1; + time_t uiTimeMax; + + for (int i = 0; i < 10; i++) + { + if (! m_astGameSlot[i].m_bEmpty + && (iMostRecent < 0 || m_astGameSlot[i].m_uiTime > uiTimeMax)) + { + iMostRecent = i; + uiTimeMax = m_astGameSlot[i].m_uiTime; + } + } + + if (iMostRecent >= 0) + { + vOnLoadGame(iMostRecent + 1); + } +} + +void Window::vOnLoadGameAutoToggled(Gtk::CheckMenuItem * _poCMI) +{ + m_poCoreConfig->vSetKey("load_game_auto", _poCMI->get_active()); +} + +void Window::vOnLoadGame(int _iSlot) +{ + int i = _iSlot - 1; + if (! m_astGameSlot[i].m_bEmpty) + { + m_stEmulator.emuReadState(m_astGameSlot[i].m_sFile.c_str()); + m_poFilePauseItem->set_active(false); + } +} + +void Window::vOnSaveGameOldest() +{ + int iOldest = -1; + time_t uiTimeMin; + + for (int i = 0; i < 10; i++) + { + if (! m_astGameSlot[i].m_bEmpty + && (iOldest < 0 || m_astGameSlot[i].m_uiTime < uiTimeMin)) + { + iOldest = i; + uiTimeMin = m_astGameSlot[i].m_uiTime; + } + } + + if (iOldest >= 0) + { + vOnSaveGame(iOldest + 1); + } + else + { + vOnSaveGame(1); + } +} + +void Window::vOnSaveGame(int _iSlot) +{ + int i = _iSlot - 1; + m_stEmulator.emuWriteState(m_astGameSlot[i].m_sFile.c_str()); + vUpdateGameSlots(); +} + +void Window::vOnFilePauseToggled(Gtk::CheckMenuItem * _poCMI) +{ + m_bPaused = _poCMI->get_active(); + if (emulating) + { + if (m_bPaused) + { + vStopEmu(); + soundPause(); + } + else + { + vStartEmu(); + soundResume(); + } + } +} + +void Window::vOnFileReset() +{ + if (emulating) + { + m_stEmulator.emuReset(true); + m_poFilePauseItem->set_active(false); + } +} + +void Window::vOnRecentReset() +{ + m_listHistory.clear(); + vClearHistoryMenu(); +} + +void Window::vOnRecentFreezeToggled(Gtk::CheckMenuItem * _poCMI) +{ + m_poRecentResetItem->set_sensitive(! _poCMI->get_active()); + m_poHistoryConfig->vSetKey("freeze", _poCMI->get_active()); +} + +void Window::vOnRecentFile(std::string _sFile) +{ + bLoadROM(_sFile); +} + +void Window::vOnImportBatteryFile() +{ + std::string BatteryDir = m_poDirConfig->sGetKey("batteries"); + +#ifdef GTKMM20 + + Gtk::FileSelection oDialog(_("Import battery file")); + oDialog.set_transient_for(*this); + + if (BatteryDir == "") + { + oDialog.set_filename(Glib::path_get_dirname(m_sRomFile) + "/"); + } + else + { + oDialog.set_filename(BatteryDir + "/"); + } + +#else // ! GTKMM20 + + Gtk::FileChooserDialog oDialog(*this, _("Import battery file")); + oDialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + oDialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + + if (BatteryDir == "") + { + oDialog.set_current_folder(Glib::path_get_dirname(m_sRomFile)); + } + else + { + oDialog.set_current_folder(BatteryDir); + oDialog.add_shortcut_folder(BatteryDir); + } + + Gtk::FileFilter oBatteryFilter; + oBatteryFilter.set_name(_("Battery file")); + oBatteryFilter.add_pattern("*.[sS][aA][vV]"); + + Gtk::FileFilter oFlashFilter; + oFlashFilter.set_name(_("Flash save")); + oFlashFilter.add_pattern("*.[dD][aA][tT]"); + + oDialog.add_filter(oBatteryFilter); + oDialog.add_filter(oFlashFilter); + +#endif // ! GTKMM20 + + while (oDialog.run() == Gtk::RESPONSE_OK) + { + Gtk::MessageDialog oConfirmDialog(*this, + _("Importing a battery file will erase any saved games and reset the emulator. Do you want to continue?"), +#ifndef GTKMM20 + false, +#endif // ! GTKMM20 + Gtk::MESSAGE_QUESTION, + Gtk::BUTTONS_YES_NO); + if (oConfirmDialog.run() != Gtk::RESPONSE_YES) + { + continue; + } + + if (m_stEmulator.emuReadBattery(oDialog.get_filename().c_str())) + { + m_stEmulator.emuReset(false); + break; + } + else + { + vPopupError(_("Failed to import battery file %s."), + oDialog.get_filename().c_str()); + } + } +} + +void Window::vOnExportBatteryFile() +{ + std::string sBatteryDir = m_poDirConfig->sGetKey("batteries"); + +#ifdef GTKMM20 + + Gtk::FileSelection oDialog(_("Export battery file")); + oDialog.set_transient_for(*this); + + if (sBatteryDir == "") + { + oDialog.set_filename(sCutSuffix(m_sRomFile)); + } + else + { + oDialog.set_filename(sBatteryDir + "/" + + sCutSuffix(Glib::path_get_basename(m_sRomFile))); + } + +#else // ! GTKMM20 + + Gtk::FileChooserDialog oDialog(*this, _("Export battery file"), + Gtk::FILE_CHOOSER_ACTION_SAVE); + oDialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + oDialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); + + if (sBatteryDir == "") + { + oDialog.set_current_folder(Glib::path_get_dirname(m_sRomFile)); + } + else + { + oDialog.set_current_folder(sBatteryDir); + oDialog.add_shortcut_folder(sBatteryDir); + } + oDialog.set_current_name(sCutSuffix(Glib::path_get_basename(m_sRomFile))); + + Gtk::FileFilter oBatteryFilter; + oBatteryFilter.set_name(_("Battery file")); + oBatteryFilter.add_pattern("*.[sS][aA][vV]"); + + Gtk::FileFilter oFlashFilter; + oFlashFilter.set_name(_("Flash save")); + oFlashFilter.add_pattern("*.[dD][aA][tT]"); + + oDialog.add_filter(oBatteryFilter); + oDialog.add_filter(oFlashFilter); + +#endif // ! GTKMM20 + + while (oDialog.run() == Gtk::RESPONSE_OK) + { + Glib::ustring sFile = oDialog.get_filename(); + Glib::ustring sExt; + +#ifdef GTKMM20 + + sExt = ".sav"; + +#else // ! GTKMM20 + + if (oDialog.get_filter() == &oBatteryFilter) + { + sExt = ".sav"; + } + else + { + sExt = ".dat"; + } + +#endif // ! GTKMM20 + + if (! bHasSuffix(sFile, sExt, false)) + { + sFile += sExt; + } + + if (Glib::file_test(sFile, Glib::FILE_TEST_EXISTS)) + { + Gtk::MessageDialog oConfirmDialog(*this, + _("File already exists. Overwrite it?"), +#ifndef GTKMM20 + false, +#endif // ! GTKMM20 + Gtk::MESSAGE_QUESTION, + Gtk::BUTTONS_YES_NO); + if (oConfirmDialog.run() != Gtk::RESPONSE_YES) + { + continue; + } + } + + bool bResult; + if (m_eCartridge == CartridgeGB) + { + bResult = gbWriteBatteryFile(sFile.c_str(), false); + } + else + { + bResult = m_stEmulator.emuWriteBattery(sFile.c_str()); + } + + if (bResult) + { + break; + } + else + { + vPopupError(_("Failed to export battery file %s."), + sFile.c_str()); + } + } +} + +void Window::vOnFileScreenCapture() +{ + std::string sCaptureDir = m_poDirConfig->sGetKey("captures"); + +#ifdef GTKMM20 + + Gtk::FileSelection oDialog(_("Save screenshot")); + oDialog.set_transient_for(*this); + + if (sCaptureDir == "") + { + oDialog.set_filename(sCutSuffix(m_sRomFile)); + } + else + { + oDialog.set_filename(sCaptureDir + "/" + + sCutSuffix(Glib::path_get_basename(m_sRomFile))); + } + +#else // ! GTKMM20 + + Gtk::FileChooserDialog oDialog(*this, _("Save screenshot"), + Gtk::FILE_CHOOSER_ACTION_SAVE); + oDialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + oDialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); + + if (sCaptureDir == "") + { + oDialog.set_current_folder(Glib::path_get_dirname(m_sRomFile)); + } + else + { + oDialog.set_current_folder(sCaptureDir); + oDialog.add_shortcut_folder(sCaptureDir); + } + oDialog.set_current_name(sCutSuffix(Glib::path_get_basename(m_sRomFile))); + + Gtk::FileFilter oPngFilter; + oPngFilter.set_name(_("PNG image")); + oPngFilter.add_pattern("*.[pP][nN][gG]"); + + Gtk::FileFilter oBmpFilter; + oBmpFilter.set_name(_("BMP image")); + oBmpFilter.add_pattern("*.[bB][mM][pP]"); + + oDialog.add_filter(oPngFilter); + oDialog.add_filter(oBmpFilter); + + if (m_poCoreConfig->sGetKey("screenshot_format") == "bmp") + { + oDialog.set_filter(oBmpFilter); + } + +#endif // ! GTKMM20 + + while (oDialog.run() == Gtk::RESPONSE_OK) + { + Glib::ustring sFile = oDialog.get_filename(); + Glib::ustring sExt; + +#ifdef GTKMM20 + + sExt = "." + m_poCoreConfig->sGetKey("screenshot_format"); + +#else // ! GTKMM20 + + if (oDialog.get_filter() == &oPngFilter) + { + sExt = ".png"; + } + else + { + sExt = ".bmp"; + } + +#endif // ! GTKMM20 + + if (! bHasSuffix(sFile, sExt, false)) + { + sFile += sExt; + } + + if (Glib::file_test(sFile, Glib::FILE_TEST_EXISTS)) + { + Gtk::MessageDialog oConfirmDialog(*this, + _("File already exists. Overwrite it?"), +#ifndef GTKMM20 + false, +#endif // ! GTKMM20 + Gtk::MESSAGE_QUESTION, + Gtk::BUTTONS_YES_NO); + if (oConfirmDialog.run() != Gtk::RESPONSE_YES) + { + continue; + } + } + + bool bResult; + if (sExt == ".png") + { + bResult = m_stEmulator.emuWritePNG(sFile.c_str()); + } + else + { + bResult = m_stEmulator.emuWriteBMP(sFile.c_str()); + } + + if (bResult) + { + break; + } + } +} + +void Window::vOnFileClose() +{ + if (m_eCartridge != CartridgeNone) + { + soundPause(); + vStopEmu(); + vSetDefaultTitle(); + vDrawDefaultScreen(); + vSaveBattery(); + m_stEmulator.emuCleanUp(); + m_eCartridge = CartridgeNone; + emulating = 0; + + vUpdateGameSlots(); + + for (std::list::iterator it = m_listSensitiveWhenPlaying.begin(); + it != m_listSensitiveWhenPlaying.end(); + it++) + { + (*it)->set_sensitive(false); + } + + m_poFilePauseItem->set_active(false); + } +} + +void Window::vOnFileExit() +{ + hide(); +} + +void Window::vOnFrameskipToggled(Gtk::CheckMenuItem * _poCMI, int _iValue) +{ + if (! _poCMI->get_active()) + { + return; + } + + if (_iValue >= 0 && _iValue <= 9) + { + m_poCoreConfig->vSetKey("frameskip", _iValue); + gbFrameSkip = _iValue; + systemFrameSkip = _iValue; + m_bAutoFrameskip = false; + } + else + { + m_poCoreConfig->vSetKey("frameskip", "auto"); + gbFrameSkip = 0; + systemFrameSkip = 0; + m_bAutoFrameskip = true; + } +} + +void Window::vOnThrottleToggled(Gtk::CheckMenuItem * _poCMI, int _iPercent) +{ + if (! _poCMI->get_active()) + { + return; + } + + vSetThrottle(_iPercent); + + // Initialize the frameskip adjustment each time throttle is changed + if (m_bAutoFrameskip) + { + systemFrameSkip = 0; + } +} + +void Window::vOnThrottleOther(Gtk::CheckMenuItem * _poCMI) +{ + if (! _poCMI->get_active()) + { + return; + } + + Glib::RefPtr poXml; + poXml = Xml::create(PKGDATADIR "/vba.glade", "ThrottleDialog"); + + Gtk::Dialog * poDialog = dynamic_cast(poXml->get_widget("ThrottleDialog")); + Gtk::SpinButton * poSpin = dynamic_cast(poXml->get_widget("ThrottleSpin")); + + poDialog->set_transient_for(*this); + + if (m_iThrottle != 0) + { + poSpin->set_value(m_iThrottle); + } + else + { + poSpin->set_value(100); + } + + if (poDialog->run() == Gtk::RESPONSE_OK) + { + vSetThrottle(poSpin->get_value_as_int()); + } + + delete poDialog; + vSelectBestThrottleItem(); +} + +void Window::vOnVideoScaleToggled(Gtk::CheckMenuItem * _poCMI, int _iScale) +{ + if (! _poCMI->get_active()) + { + return; + } + + m_poDisplayConfig->vSetKey("scale", _iScale); + vUpdateScreen(); +} + +void Window::vOnLayerToggled(Gtk::CheckMenuItem * _poCMI, int _iLayer) +{ + int iMask = (0x0100 << _iLayer); + if (_poCMI->get_active()) + { + layerSettings |= iMask; + } + else + { + layerSettings &= ~iMask; + } + layerEnable = DISPCNT & layerSettings; + + const char * acsLayers[] = + { + "layer_bg0", + "layer_bg1", + "layer_bg2", + "layer_bg3", + "layer_obj", + "layer_win0", + "layer_win1", + "layer_objwin" + }; + m_poCoreConfig->vSetKey(acsLayers[_iLayer], _poCMI->get_active()); +} + +void Window::vOnDirectories() +{ + Glib::RefPtr poXml; + poXml = Xml::create(PKGDATADIR "/vba.glade", "DirectoriesDialog"); + + struct + { + const char * m_csKey; + const char * m_csEntry; + const char * m_csResetButton; + const char * m_csSelectButton; + } + astRow[] = + { + { "gba_roms", "GBARomsDirEntry", "GBARomsDirResetButton", "GBARomsDirSelectButton" }, + { "gb_roms", "GBRomsDirEntry", "GBRomsDirResetButton", "GBRomsDirSelectButton" }, + { "batteries", "BatteriesDirEntry", "BatteriesDirResetButton", "BatteriesDirSelectButton" }, + { "saves", "SavesDirEntry", "SavesDirResetButton", "SavesDirSelectButton" }, + { "captures", "CapturesDirEntry", "CapturesDirResetButton", "CapturesDirSelectButton" } + }; + + for (guint i = 0; i < G_N_ELEMENTS(astRow); i++) + { + Gtk::Entry * poEntry = dynamic_cast(poXml->get_widget(astRow[i].m_csEntry)); + Gtk::Button * poReset = dynamic_cast(poXml->get_widget(astRow[i].m_csResetButton)); + Gtk::Button * poSelect = dynamic_cast(poXml->get_widget(astRow[i].m_csSelectButton)); + + poEntry->set_text(m_poDirConfig->sGetKey(astRow[i].m_csKey)); + + poReset->signal_clicked().connect(SigC::bind( + SigC::slot(*this, &Window::vOnDirectoryReset), + poEntry)); + poSelect->signal_clicked().connect(SigC::bind( + SigC::slot(*this, &Window::vOnDirectorySelect), + poEntry)); + } + + Gtk::Dialog * poDialog = dynamic_cast(poXml->get_widget("DirectoriesDialog")); + poDialog->set_transient_for(*this); + + if (poDialog->run() == Gtk::RESPONSE_OK) + { + for (guint i = 0; i < G_N_ELEMENTS(astRow); i++) + { + Gtk::Entry * poEntry = dynamic_cast(poXml->get_widget(astRow[i].m_csEntry)); + Glib::ustring sDir = poEntry->get_text(); + if (! Glib::file_test(sDir, Glib::FILE_TEST_IS_DIR)) + { + sDir = ""; + } + m_poDirConfig->vSetKey(astRow[i].m_csKey, sDir); + } + + // Needed if saves dir changed + vUpdateGameSlots(); + } + + delete poDialog; +} + +void Window::vOnDirectoryReset(Gtk::Entry * _poEntry) +{ + _poEntry->set_text(""); +} + +void Window::vOnDirectorySelect(Gtk::Entry * _poEntry) +{ +#ifdef GTKMM20 + + Gtk::FileSelection oDialog(_("Select directory")); + oDialog.set_transient_for(*this); + + if (_poEntry->get_text() != "") + { + oDialog.set_filename(_poEntry->get_text() + "/"); + } + + if (oDialog.run() == Gtk::RESPONSE_OK) + { + std::string sFile = oDialog.get_filename(); + if (! Glib::file_test(sFile, Glib::FILE_TEST_IS_DIR)) + { + sFile = Glib::path_get_dirname(sFile); + } + _poEntry->set_text(sFile); + } + +#else // ! GTKMM20 + + Gtk::FileChooserDialog oDialog(*this, _("Select directory"), + Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); + oDialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + oDialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK); + + if (_poEntry->get_text() != "") + { + oDialog.add_shortcut_folder(_poEntry->get_text()); + oDialog.set_current_folder(_poEntry->get_text()); + } + + if (oDialog.run() == Gtk::RESPONSE_OK) + { + _poEntry->set_text(oDialog.get_filename()); + } + +#endif // ! GTKMM20 +} + +void Window::vOnPauseWhenInactiveToggled(Gtk::CheckMenuItem * _poCMI) +{ + m_poDisplayConfig->vSetKey("pause_when_inactive", _poCMI->get_active()); +} + +void Window::vOnSelectBios() +{ +#ifdef GTKMM20 + + Gtk::FileSelection oDialog(_("Select BIOS file")); + oDialog.set_transient_for(*this); + + if (m_poCoreConfig->sGetKey("bios_file") != "") + { + oDialog.set_filename(m_poCoreConfig->sGetKey("bios_file")); + } + +#else // ! GTKMM20 + + Gtk::FileChooserDialog oDialog(*this, _("Select BIOS file")); + oDialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + oDialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + + if (m_poCoreConfig->sGetKey("bios_file") != "") + { + oDialog.set_filename(m_poCoreConfig->sGetKey("bios_file")); + } + + const char * acsPattern[] = + { + "*.[bB][iI][nN]", "*.[aA][gG][bB]", "*.[gG][bB][aA]", + "*.[bB][iI][oO][sS]", "*.[zZ][iI][pP]", "*.[zZ]", "*.[gG][zZ]" + }; + + Gtk::FileFilter oAllFilter; + oAllFilter.set_name(_("All files")); + oAllFilter.add_pattern("*"); + + Gtk::FileFilter oBiosFilter; + oBiosFilter.set_name(_("Gameboy Advance BIOS")); + for (guint i = 0; i < G_N_ELEMENTS(acsPattern); i++) + { + oBiosFilter.add_pattern(acsPattern[i]); + } + + oDialog.add_filter(oAllFilter); + oDialog.add_filter(oBiosFilter); + + oDialog.set_filter(oBiosFilter); + +#endif // ! GTKMM20 + + while (oDialog.run() == Gtk::RESPONSE_OK) + { + if (Glib::file_test(oDialog.get_filename(), Glib::FILE_TEST_IS_REGULAR)) + { + m_poCoreConfig->vSetKey("bios_file", oDialog.get_filename()); + m_poUseBiosItem->set_sensitive(); + break; + } + } +} + +void Window::vOnUseBiosToggled(Gtk::CheckMenuItem * _poCMI) +{ + m_poCoreConfig->vSetKey("use_bios_file", _poCMI->get_active()); +} + +void Window::vOnShowSpeedToggled(Gtk::CheckMenuItem * _poCMI, int _iShowSpeed) +{ + if (! _poCMI->get_active()) + { + return; + } + + m_eShowSpeed = (EShowSpeed)_iShowSpeed; + if (m_eShowSpeed == ShowNone) + { + vSetDefaultTitle(); + } + m_poDisplayConfig->vSetKey("show_speed", _iShowSpeed); +} + +void Window::vOnSaveTypeToggled(Gtk::CheckMenuItem * _poCMI, int _iSaveType) +{ + if (! _poCMI->get_active()) + { + return; + } + + cpuSaveType = _iSaveType; + m_poCoreConfig->vSetKey("save_type", _iSaveType); +} + +void Window::vOnFlashSizeToggled(Gtk::CheckMenuItem * _poCMI, int _iFlashSize) +{ + if (! _poCMI->get_active()) + { + return; + } + + if (_iFlashSize == 64) + { + flashSetSize(0x10000); + } + else + { + flashSetSize(0x20000); + } + m_poCoreConfig->vSetKey("flash_size", _iFlashSize); +} + +void Window::vOnScreenshotFormatToggled(Gtk::CheckMenuItem * _poCMI, std::string _sFormat) +{ + if (! _poCMI->get_active()) + { + return; + } + + m_poCoreConfig->vSetKey("screenshot_format", _sFormat); +} + +void Window::vOnSoundStatusToggled(Gtk::CheckMenuItem * _poCMI, int _iSoundStatus) +{ + if (! _poCMI->get_active()) + { + return; + } + + std::string sSoundStatus; + switch (_iSoundStatus) + { + case SoundOff: + soundOffFlag = true; + if (systemSoundOn) + { + soundShutdown(); + } + sSoundStatus = "off"; + break; + case SoundMute: + soundDisableChannels(0x30f); + sSoundStatus = "mute"; + break; + case SoundOn: + if (soundOffFlag) + { + soundOffFlag = false; + if (! soundInit()) + { + m_poSoundOffItem->set_active(); + return; + } + } + soundEnableChannels(0x30f); + sSoundStatus = "on"; + break; + } + m_poSoundConfig->vSetKey("status", sSoundStatus); +} + +void Window::vOnSoundEchoToggled(Gtk::CheckMenuItem * _poCMI) +{ + soundEcho = _poCMI->get_active(); + m_poSoundConfig->vSetKey("echo", soundEcho); +} + +void Window::vOnSoundLowPassToggled(Gtk::CheckMenuItem * _poCMI) +{ + soundLowPass = _poCMI->get_active(); + m_poSoundConfig->vSetKey("low_pass", soundLowPass); +} + +void Window::vOnSoundReverseToggled(Gtk::CheckMenuItem * _poCMI) +{ + soundReverse = _poCMI->get_active(); + m_poSoundConfig->vSetKey("reverse_stereo", soundReverse); +} + +void Window::vOnSoundChannelToggled(Gtk::CheckMenuItem * _poCMI, int _iSoundChannel) +{ + int iShift = _iSoundChannel; + if (_iSoundChannel > 3) + { + iShift += 4; + } + int iFlag = 1 << iShift; + int iActive = soundGetEnabledChannels() & 0x30f; + if (_poCMI->get_active()) + { + iActive |= iFlag; + } + else + { + iActive &= ~iFlag; + } + soundEnableChannels(iActive); + soundDisableChannels(~iActive & 0x30f); + + const char * acsChannels[] = + { + "channel_1", + "channel_2", + "channel_3", + "channel_4", + "channel_A", + "channel_B" + }; + m_poSoundConfig->vSetKey(acsChannels[_iSoundChannel], _poCMI->get_active()); +} + +void Window::vOnSoundQualityToggled(Gtk::CheckMenuItem * _poCMI, int _iSoundQuality) +{ + if (! _poCMI->get_active()) + { + return; + } + + m_eSoundQuality = (ESoundQuality)_iSoundQuality; + if (m_eCartridge == CartridgeGBA) + { + soundSetQuality(_iSoundQuality); + } + else if (m_eCartridge == CartridgeGB) + { + gbSoundSetQuality(_iSoundQuality); + } + m_poSoundConfig->vSetKey("quality", _iSoundQuality); +} + +void Window::vOnSoundVolumeToggled(Gtk::CheckMenuItem * _poCMI, int _iSoundVolume) +{ + if (! _poCMI->get_active()) + { + return; + } + + soundVolume = _iSoundVolume; + m_poSoundConfig->vSetKey("volume", _iSoundVolume); +} + +void Window::vOnGBBorderToggled(Gtk::CheckMenuItem * _poCMI) +{ + gbBorderOn = _poCMI->get_active(); + if (emulating && m_eCartridge == CartridgeGB && _poCMI->get_active()) + { + gbSgbRenderBorder(); + } + vUpdateScreen(); + m_poCoreConfig->vSetKey("gb_border", _poCMI->get_active()); +} + +void Window::vOnGBPrinterToggled(Gtk::CheckMenuItem * _poCMI) +{ + if (_poCMI->get_active()) + { + gbSerialFunction = gbPrinterSend; + } + else + { + gbSerialFunction = NULL; + } + m_poCoreConfig->vSetKey("gb_printer", _poCMI->get_active()); +} + +void Window::vOnEmulatorTypeToggled(Gtk::CheckMenuItem * _poCMI, int _iEmulatorType) +{ + gbEmulatorType = _iEmulatorType; + m_poCoreConfig->vSetKey("emulator_type", _iEmulatorType); +} + +void Window::vOnFilter2xToggled(Gtk::CheckMenuItem * _poCMI, int _iFilter2x) +{ + if (! _poCMI->get_active()) + { + return; + } + + m_poScreenArea->vSetFilter2x((EFilter2x)_iFilter2x); + if (emulating) + { + vDrawScreen(); + } + m_poDisplayConfig->vSetKey("filter2x", _iFilter2x); +} + +void Window::vOnFilterIBToggled(Gtk::CheckMenuItem * _poCMI, int _iFilterIB) +{ + if (! _poCMI->get_active()) + { + return; + } + + m_poScreenArea->vSetFilterIB((EFilterIB)_iFilterIB); + if (emulating) + { + vDrawScreen(); + } + m_poDisplayConfig->vSetKey("filterIB", _iFilterIB); +} + +#ifdef MMX +void Window::vOnDisableMMXToggled(Gtk::CheckMenuItem * _poCMI) +{ + cpu_mmx = ! _poCMI->get_active(); + m_poDisplayConfig->vSetKey("filter_disable_mmx", _poCMI->get_active()); +} +#endif // MMX + +void Window::vOnJoypadConfigure(int _iJoypad) +{ + Glib::RefPtr poXml; + poXml = Xml::create(PKGDATADIR "/vba.glade", "JoypadConfigDialog"); + + JoypadConfigDialog * poDialog = NULL; + poXml->get_widget_derived("JoypadConfigDialog", poDialog); + poDialog->set_transient_for(*this); + poDialog->vSetConfig(m_oJoypads[_iJoypad - 1]); + + if (poDialog->run() == Gtk::RESPONSE_OK) + { + m_oJoypads[_iJoypad - 1] = poDialog->stGetConfig(); + if (_iJoypad == m_poInputConfig->oGetKey("active_joypad")) + { + if (m_poKeymap != NULL) + { + delete m_poKeymap; + } + m_poKeymap = m_oJoypads[_iJoypad - 1].poCreateKeymap(); + } + } + + delete poDialog; +} + +void Window::vOnJoypadToggled(Gtk::CheckMenuItem * _poCMI, int _iJoypad) +{ + if (! _poCMI->get_active()) + { + return; + } + + if (m_poKeymap != NULL) + { + delete m_poKeymap; + } + m_poKeymap = m_oJoypads[_iJoypad - 1].poCreateKeymap(); + + m_poInputConfig->vSetKey("active_joypad", _iJoypad); +} + +void Window::vOnAutofireToggled(Gtk::CheckMenuItem * _poCMI, u32 _uiKeyFlag) +{ + if (_poCMI->get_active()) + { + m_uiAutofireState |= _uiKeyFlag; + } + else + { + m_uiAutofireState &= ~_uiKeyFlag; + } + + std::string sKey; + if (_uiKeyFlag == KeyFlagA) + { + sKey = "autofire_A"; + } + else if (_uiKeyFlag == KeyFlagB) + { + sKey = "autofire_B"; + } + else if (_uiKeyFlag == KeyFlagL) + { + sKey = "autofire_L"; + } + else if (_uiKeyFlag == KeyFlagR) + { + sKey = "autofire_R"; + } + m_poInputConfig->vSetKey(sKey, _poCMI->get_active()); +} + +void Window::vOnGDBWait() +{ + Glib::RefPtr poXml; + poXml = Xml::create(PKGDATADIR "/vba.glade", "TcpPortDialog"); + + Gtk::Dialog * poDialog = dynamic_cast(poXml->get_widget("TcpPortDialog")); + Gtk::SpinButton * poSpin = dynamic_cast(poXml->get_widget("TcpPortSpin")); + + poDialog->set_transient_for(*this); + + int iPort = 55555; + poSpin->set_value(iPort); + + bool bOk = false; + if (poDialog->run() == Gtk::RESPONSE_OK) + { + bOk = true; + iPort = poSpin->get_value_as_int(); + } + delete poDialog; + + if (! bOk) + { + return; + } + + m_eCartridge = CartridgeGBA; + m_sRomFile = "gnu_stub"; + m_stEmulator = GBASystem; + + rom = (u8 *) malloc(0x2000000); + workRAM = (u8 *) calloc(1, 0x40000); + bios = (u8 *) calloc(1, 0x4000); + internalRAM = (u8 *) calloc(1, 0x8000); + paletteRAM = (u8 *) calloc(1, 0x400); + vram = (u8 *) calloc(1, 0x20000); + oam = (u8 *) calloc(1, 0x400); + pix = (u8 *) calloc(1, 4 * m_iGBAScreenWidth * m_iGBAScreenHeight); + ioMem = (u8 *) calloc(1, 0x400); + + useBios = m_poCoreConfig->oGetKey("use_bios_file"); + //CPUInit(m_poCoreConfig->sGetKey("bios_file").c_str(), useBios); + CPUInit(); + CPUReset(); + + for (std::list::iterator it = m_listSensitiveWhenPlaying.begin(); + it != m_listSensitiveWhenPlaying.end(); + it++) + { + (*it)->set_sensitive(); + } + + if (m_poCoreConfig->oGetKey("load_game_auto")) + { + vOnLoadGameMostRecent(); + } + + vStartEmu(); + + emulating = 1; + + dbgMain = remoteStubMain; + dbgSignal = remoteStubSignal; + dbgOutput = remoteOutput; + debugger = true; + + remoteSetProtocol(0); + remoteSetPort(iPort); + remoteInit(); +} + +void Window::vOnGDBLoadAndWait() +{ + bool bLoaded = false; + + while (m_poFileOpenDialog->run() == Gtk::RESPONSE_OK) + { + if (bLoadROM(m_poFileOpenDialog->get_filename())) + { + bLoaded = true; + break; + } + } + m_poFileOpenDialog->hide(); + + if (! bLoaded) + { + return; + } + + if (m_eCartridge != CartridgeGBA) + { + vPopupError(_("Only GBA images are supported.")); + vOnFileClose(); + return; + } + + Glib::RefPtr poXml; + poXml = Xml::create(PKGDATADIR "/vba.glade", "TcpPortDialog"); + + Gtk::Dialog * poDialog = dynamic_cast(poXml->get_widget("TcpPortDialog")); + Gtk::SpinButton * poSpin = dynamic_cast(poXml->get_widget("TcpPortSpin")); + + poDialog->set_transient_for(*this); + + int iPort = 55555; + poSpin->set_value(iPort); + + bool bOk = false; + if (poDialog->run() == Gtk::RESPONSE_OK) + { + bOk = true; + iPort = poSpin->get_value_as_int(); + } + delete poDialog; + + if (! bOk) + { + return; + } + + dbgMain = remoteStubMain; + dbgSignal = remoteStubSignal; + dbgOutput = remoteOutput; + debugger = true; + + remoteSetProtocol(0); + remoteSetPort(iPort); + remoteInit(); +} + +void Window::vOnGDBBreak() +{ + if (armState) + { + armNextPC -= 4; + reg[15].I -= 4; + } + else + { + armNextPC -= 2; + reg[15].I -= 2; + } + + debugger = true; +} + +void Window::vOnGDBDisconnect() +{ + remoteCleanUp(); + debugger = false; +} + +void Window::vOnHelpAbout() +{ + Glib::RefPtr poXml; + poXml = Xml::create(PKGDATADIR "/vba.glade", "AboutDialog"); + + Gtk::Dialog * poDialog = dynamic_cast(poXml->get_widget("AboutDialog")); + poDialog->set_transient_for(*this); + + Gtk::Image oIcon(PKGDATADIR "/vba-64.png"); + oIcon.show(); + Gtk::Container * poIconContainer = dynamic_cast(poXml->get_widget("AboutIconContainer")); + poIconContainer->add(oIcon); + + Gtk::Label * poLabel = dynamic_cast(poXml->get_widget("VersionLabel")); + poLabel->set_markup("" PACKAGE " " VERSION ""); + + poDialog->run(); + delete poDialog; +} + +bool Window::bOnEmuIdle() +{ + if (debugger && m_stEmulator.emuHasDebugger) + { + dbgMain(); + return true; + } + + if (m_uiThrottleDelay != 0) + { + u32 uiTime = SDL_GetTicks(); + if (uiTime - m_uiThrottleLastTime >= m_uiThrottleDelay) + { + m_uiThrottleDelay = 0; + m_uiThrottleLastTime = uiTime; + } + else + { + return true; + } + } + + m_stEmulator.emuMain(m_stEmulator.emuCount); + return true; +} + +bool Window::on_focus_in_event(GdkEventFocus * _pstEvent) +{ + if (emulating + && ! m_bPaused + && m_poDisplayConfig->oGetKey("pause_when_inactive")) + { + vStartEmu(); + soundResume(); + } + return false; +} + +bool Window::on_focus_out_event(GdkEventFocus * _pstEvent) +{ + if (emulating + && ! m_bPaused + && m_poDisplayConfig->oGetKey("pause_when_inactive")) + { + vStopEmu(); + soundPause(); + } + return false; +} + +bool Window::on_key_press_event(GdkEventKey * _pstEvent) +{ + EKey eKey; + + if ((_pstEvent->state & Gtk::AccelGroup::get_default_mod_mask()) + || (eKey = m_poKeymap->eGetKey(_pstEvent->hardware_keycode)) == KeyNone) + { + return Gtk::Window::on_key_press_event(_pstEvent); + } + + switch (eKey) + { + case KeyA: + m_uiJoypadState |= KeyFlagA; + break; + case KeyB: + m_uiJoypadState |= KeyFlagB; + break; + case KeySelect: + m_uiJoypadState |= KeyFlagSelect; + break; + case KeyStart: + m_uiJoypadState |= KeyFlagStart; + break; + case KeyRight: + m_uiJoypadState |= KeyFlagRight; + m_uiJoypadState &= ~KeyFlagLeft; + break; + case KeyLeft: + m_uiJoypadState |= KeyFlagLeft; + m_uiJoypadState &= ~KeyFlagRight; + break; + case KeyUp: + m_uiJoypadState |= KeyFlagUp; + m_uiJoypadState &= ~KeyFlagDown; + break; + case KeyDown: + m_uiJoypadState |= KeyFlagDown; + m_uiJoypadState &= ~KeyFlagUp; + break; + case KeyR: + m_uiJoypadState |= KeyFlagR; + break; + case KeyL: + m_uiJoypadState |= KeyFlagL; + break; + case KeySpeed: + m_uiJoypadState |= KeyFlagSpeed; + break; + case KeyCapture: + m_uiJoypadState |= KeyFlagCapture; + break; + case KeyNone: + break; + } + return true; +} + +bool Window::on_key_release_event(GdkEventKey * _pstEvent) +{ + EKey eKey; + + if ((_pstEvent->state & Gtk::AccelGroup::get_default_mod_mask()) + || (eKey = m_poKeymap->eGetKey(_pstEvent->hardware_keycode)) == KeyNone) + { + return Gtk::Window::on_key_release_event(_pstEvent); + } + + switch (eKey) + { + case KeyA: + m_uiJoypadState &= ~KeyFlagA; + break; + case KeyB: + m_uiJoypadState &= ~KeyFlagB; + break; + case KeySelect: + m_uiJoypadState &= ~KeyFlagSelect; + break; + case KeyStart: + m_uiJoypadState &= ~KeyFlagStart; + break; + case KeyRight: + m_uiJoypadState &= ~KeyFlagRight; + break; + case KeyLeft: + m_uiJoypadState &= ~KeyFlagLeft; + break; + case KeyUp: + m_uiJoypadState &= ~KeyFlagUp; + break; + case KeyDown: + m_uiJoypadState &= ~KeyFlagDown; + break; + case KeyR: + m_uiJoypadState &= ~KeyFlagR; + break; + case KeyL: + m_uiJoypadState &= ~KeyFlagL; + break; + case KeySpeed: + m_uiJoypadState &= ~KeyFlagSpeed; + break; + case KeyCapture: + m_uiJoypadState &= ~KeyFlagCapture; + break; + case KeyNone: + break; + } + return true; +} + +} // namespace VBA diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,56 @@ +noinst_LIBRARIES = libgblua.a + +libgblua_a_SOURCES = \ + src/lapi.c \ + src/lapi.h \ + src/lauxlib.c \ + src/lauxlib.h \ + src/lbaselib.c \ + src/lcode.c \ + src/lcode.h \ + src/ldblib.c \ + src/ldebug.c \ + src/ldebug.h \ + src/ldo.c \ + src/ldo.h \ + src/ldump.c \ + src/lfunc.c \ + src/lfunc.h \ + src/lgc.c \ + src/lgc.h \ + src/linit.c \ + src/liolib.c \ + src/llex.c \ + src/llex.h \ + src/llimit.h \ + src/lmathlib.c \ + src/lmem.c \ + src/lmem.h \ + src/loadlib.c \ + src/lobject.c \ + src/lobject.h \ + src/lopcodes.c \ + src/lopcodes.h \ + src/loslib.c \ + src/lparser.c \ + src/lparser.h \ + src/lstate.c \ + src/lstate.h \ + src/lstring.c \ + src/lstring.h \ + src/lstrlib.c \ + src/ltable.c \ + src/ltable.h \ + src/ltablib.c \ + src/ltm.c \ + src/ltm.h \ + src/lua.h \ + src/luaconf.h \ + src/lualib.h \ + src/lundump.c \ + src/lundump.h \ + src/lvm.c \ + src/lvm.h \ + src/lzio.c \ + src/lzio.h \ + src/print.c diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lapi.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lapi.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1087 @@ +/* +** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $ +** Lua API +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include + +#define lapi_c +#define LUA_CORE + +#include "lua.h" + +#include "lapi.h" +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" +#include "lundump.h" +#include "lvm.h" + + + +const char lua_ident[] = + "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n" + "$Authors: " LUA_AUTHORS " $\n" + "$URL: www.lua.org $\n"; + + + +#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) + +#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) + +#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;} + + + +static TValue *index2adr (lua_State *L, int idx) { + if (idx > 0) { + TValue *o = L->base + (idx - 1); + api_check(L, idx <= L->ci->top - L->base); + if (o >= L->top) return cast(TValue *, luaO_nilobject); + else return o; + } + else if (idx > LUA_REGISTRYINDEX) { + api_check(L, idx != 0 && -idx <= L->top - L->base); + return L->top + idx; + } + else switch (idx) { /* pseudo-indices */ + case LUA_REGISTRYINDEX: return registry(L); + case LUA_ENVIRONINDEX: { + Closure *func = curr_func(L); + sethvalue(L, &L->env, func->c.env); + return &L->env; + } + case LUA_GLOBALSINDEX: return gt(L); + default: { + Closure *func = curr_func(L); + idx = LUA_GLOBALSINDEX - idx; + return (idx <= func->c.nupvalues) + ? &func->c.upvalue[idx-1] + : cast(TValue *, luaO_nilobject); + } + } +} + + +static Table *getcurrenv (lua_State *L) { + if (L->ci == L->base_ci) /* no enclosing function? */ + return hvalue(gt(L)); /* use global table as environment */ + else { + Closure *func = curr_func(L); + return func->c.env; + } +} + + +void luaA_pushobject (lua_State *L, const TValue *o) { + setobj2s(L, L->top, o); + api_incr_top(L); +} + + +LUA_API int lua_checkstack (lua_State *L, int size) { + int res = 1; + lua_lock(L); + if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) + res = 0; /* stack overflow */ + else if (size > 0) { + luaD_checkstack(L, size); + if (L->ci->top < L->top + size) + L->ci->top = L->top + size; + } + lua_unlock(L); + return res; +} + + +LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { + int i; + if (from == to) return; + lua_lock(to); + api_checknelems(from, n); + api_check(from, G(from) == G(to)); + api_check(from, to->ci->top - to->top >= n); + from->top -= n; + for (i = 0; i < n; i++) { + setobj2s(to, to->top++, from->top + i); + } + lua_unlock(to); +} + + +LUA_API void lua_setlevel (lua_State *from, lua_State *to) { + to->nCcalls = from->nCcalls; +} + + +LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { + lua_CFunction old; + lua_lock(L); + old = G(L)->panic; + G(L)->panic = panicf; + lua_unlock(L); + return old; +} + + +LUA_API lua_State *lua_newthread (lua_State *L) { + lua_State *L1; + lua_lock(L); + luaC_checkGC(L); + L1 = luaE_newthread(L); + setthvalue(L, L->top, L1); + api_incr_top(L); + lua_unlock(L); + luai_userstatethread(L, L1); + return L1; +} + + + +/* +** basic stack manipulation +*/ + + +LUA_API int lua_gettop (lua_State *L) { + return cast_int(L->top - L->base); +} + + +LUA_API void lua_settop (lua_State *L, int idx) { + lua_lock(L); + if (idx >= 0) { + api_check(L, idx <= L->stack_last - L->base); + while (L->top < L->base + idx) + setnilvalue(L->top++); + L->top = L->base + idx; + } + else { + api_check(L, -(idx+1) <= (L->top - L->base)); + L->top += idx+1; /* `subtract' index (index is negative) */ + } + lua_unlock(L); +} + + +LUA_API void lua_remove (lua_State *L, int idx) { + StkId p; + lua_lock(L); + p = index2adr(L, idx); + api_checkvalidindex(L, p); + while (++p < L->top) setobjs2s(L, p-1, p); + L->top--; + lua_unlock(L); +} + + +LUA_API void lua_insert (lua_State *L, int idx) { + StkId p; + StkId q; + lua_lock(L); + p = index2adr(L, idx); + api_checkvalidindex(L, p); + for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); + setobjs2s(L, p, L->top); + lua_unlock(L); +} + + +LUA_API void lua_replace (lua_State *L, int idx) { + StkId o; + lua_lock(L); + /* explicit test for incompatible code */ + if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci) + luaG_runerror(L, "no calling environment"); + api_checknelems(L, 1); + o = index2adr(L, idx); + api_checkvalidindex(L, o); + if (idx == LUA_ENVIRONINDEX) { + Closure *func = curr_func(L); + api_check(L, ttistable(L->top - 1)); + func->c.env = hvalue(L->top - 1); + luaC_barrier(L, func, L->top - 1); + } + else { + setobj(L, o, L->top - 1); + if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ + luaC_barrier(L, curr_func(L), L->top - 1); + } + L->top--; + lua_unlock(L); +} + + +LUA_API void lua_pushvalue (lua_State *L, int idx) { + lua_lock(L); + setobj2s(L, L->top, index2adr(L, idx)); + api_incr_top(L); + lua_unlock(L); +} + + + +/* +** access functions (stack -> C) +*/ + + +LUA_API int lua_type (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + return (o == luaO_nilobject) ? LUA_TNONE : ttype(o); +} + + +LUA_API const char *lua_typename (lua_State *L, int t) { + UNUSED(L); + return (t == LUA_TNONE) ? "no value" : luaT_typenames[t]; +} + + +LUA_API int lua_iscfunction (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + return iscfunction(o); +} + + +LUA_API int lua_isnumber (lua_State *L, int idx) { + TValue n; + const TValue *o = index2adr(L, idx); + return tonumber(o, &n); +} + + +LUA_API int lua_isstring (lua_State *L, int idx) { + int t = lua_type(L, idx); + return (t == LUA_TSTRING || t == LUA_TNUMBER); +} + + +LUA_API int lua_isuserdata (lua_State *L, int idx) { + const TValue *o = index2adr(L, idx); + return (ttisuserdata(o) || ttislightuserdata(o)); +} + + +LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { + StkId o1 = index2adr(L, index1); + StkId o2 = index2adr(L, index2); + return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 + : luaO_rawequalObj(o1, o2); +} + + +LUA_API int lua_equal (lua_State *L, int index1, int index2) { + StkId o1, o2; + int i; + lua_lock(L); /* may call tag method */ + o1 = index2adr(L, index1); + o2 = index2adr(L, index2); + i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2); + lua_unlock(L); + return i; +} + + +LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { + StkId o1, o2; + int i; + lua_lock(L); /* may call tag method */ + o1 = index2adr(L, index1); + o2 = index2adr(L, index2); + i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 + : luaV_lessthan(L, o1, o2); + lua_unlock(L); + return i; +} + + + +LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { + TValue n; + const TValue *o = index2adr(L, idx); + if (tonumber(o, &n)) + return nvalue(o); + else + return 0; +} + + +LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { + TValue n; + const TValue *o = index2adr(L, idx); + if (tonumber(o, &n)) { + lua_Integer res; + lua_Number num = nvalue(o); + lua_number2integer(res, num); + return res; + } + else + return 0; +} + + +LUA_API int lua_toboolean (lua_State *L, int idx) { + const TValue *o = index2adr(L, idx); + return !l_isfalse(o); +} + + +LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { + StkId o = index2adr(L, idx); + if (!ttisstring(o)) { + lua_lock(L); /* `luaV_tostring' may create a new string */ + if (!luaV_tostring(L, o)) { /* conversion failed? */ + if (len != NULL) *len = 0; + lua_unlock(L); + return NULL; + } + luaC_checkGC(L); + o = index2adr(L, idx); /* previous call may reallocate the stack */ + lua_unlock(L); + } + if (len != NULL) *len = tsvalue(o)->len; + return svalue(o); +} + + +LUA_API size_t lua_objlen (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + switch (ttype(o)) { + case LUA_TSTRING: return tsvalue(o)->len; + case LUA_TUSERDATA: return uvalue(o)->len; + case LUA_TTABLE: return luaH_getn(hvalue(o)); + case LUA_TNUMBER: { + size_t l; + lua_lock(L); /* `luaV_tostring' may create a new string */ + l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0); + lua_unlock(L); + return l; + } + default: return 0; + } +} + + +LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + return (!iscfunction(o)) ? NULL : clvalue(o)->c.f; +} + + +LUA_API void *lua_touserdata (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + switch (ttype(o)) { + case LUA_TUSERDATA: return (rawuvalue(o) + 1); + case LUA_TLIGHTUSERDATA: return pvalue(o); + default: return NULL; + } +} + + +LUA_API lua_State *lua_tothread (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + return (!ttisthread(o)) ? NULL : thvalue(o); +} + + +LUA_API const void *lua_topointer (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + switch (ttype(o)) { + case LUA_TTABLE: return hvalue(o); + case LUA_TFUNCTION: return clvalue(o); + case LUA_TTHREAD: return thvalue(o); + case LUA_TUSERDATA: + case LUA_TLIGHTUSERDATA: + return lua_touserdata(L, idx); + default: return NULL; + } +} + + + +/* +** push functions (C -> stack) +*/ + + +LUA_API void lua_pushnil (lua_State *L) { + lua_lock(L); + setnilvalue(L->top); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { + lua_lock(L); + setnvalue(L->top, n); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { + lua_lock(L); + setnvalue(L->top, cast_num(n)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { + lua_lock(L); + luaC_checkGC(L); + setsvalue2s(L, L->top, luaS_newlstr(L, s, len)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushstring (lua_State *L, const char *s) { + if (s == NULL) + lua_pushnil(L); + else + lua_pushlstring(L, s, strlen(s)); +} + + +LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, + va_list argp) { + const char *ret; + lua_lock(L); + luaC_checkGC(L); + ret = luaO_pushvfstring(L, fmt, argp); + lua_unlock(L); + return ret; +} + + +LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { + const char *ret; + va_list argp; + lua_lock(L); + luaC_checkGC(L); + va_start(argp, fmt); + ret = luaO_pushvfstring(L, fmt, argp); + va_end(argp); + lua_unlock(L); + return ret; +} + + +LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { + Closure *cl; + lua_lock(L); + luaC_checkGC(L); + api_checknelems(L, n); + cl = luaF_newCclosure(L, n, getcurrenv(L)); + cl->c.f = fn; + L->top -= n; + while (n--) + setobj2n(L, &cl->c.upvalue[n], L->top+n); + setclvalue(L, L->top, cl); + lua_assert(iswhite(obj2gco(cl))); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushboolean (lua_State *L, int b) { + lua_lock(L); + setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { + lua_lock(L); + setpvalue(L->top, p); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API int lua_pushthread (lua_State *L) { + lua_lock(L); + setthvalue(L, L->top, L); + api_incr_top(L); + lua_unlock(L); + return (G(L)->mainthread == L); +} + + + +/* +** get functions (Lua -> stack) +*/ + + +LUA_API void lua_gettable (lua_State *L, int idx) { + StkId t; + lua_lock(L); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + luaV_gettable(L, t, L->top - 1, L->top - 1); + lua_unlock(L); +} + + +LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { + StkId t; + TValue key; + lua_lock(L); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + setsvalue(L, &key, luaS_new(L, k)); + luaV_gettable(L, t, &key, L->top); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_rawget (lua_State *L, int idx) { + StkId t; + lua_lock(L); + t = index2adr(L, idx); + api_check(L, ttistable(t)); + setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); + lua_unlock(L); +} + + +LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { + StkId o; + lua_lock(L); + o = index2adr(L, idx); + api_check(L, ttistable(o)); + setobj2s(L, L->top, luaH_getnum(hvalue(o), n)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { + lua_lock(L); + luaC_checkGC(L); + sethvalue(L, L->top, luaH_new(L, narray, nrec)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API int lua_getmetatable (lua_State *L, int objindex) { + const TValue *obj; + Table *mt = NULL; + int res; + lua_lock(L); + obj = index2adr(L, objindex); + switch (ttype(obj)) { + case LUA_TTABLE: + mt = hvalue(obj)->metatable; + break; + case LUA_TUSERDATA: + mt = uvalue(obj)->metatable; + break; + default: + mt = G(L)->mt[ttype(obj)]; + break; + } + if (mt == NULL) + res = 0; + else { + sethvalue(L, L->top, mt); + api_incr_top(L); + res = 1; + } + lua_unlock(L); + return res; +} + + +LUA_API void lua_getfenv (lua_State *L, int idx) { + StkId o; + lua_lock(L); + o = index2adr(L, idx); + api_checkvalidindex(L, o); + switch (ttype(o)) { + case LUA_TFUNCTION: + sethvalue(L, L->top, clvalue(o)->c.env); + break; + case LUA_TUSERDATA: + sethvalue(L, L->top, uvalue(o)->env); + break; + case LUA_TTHREAD: + setobj2s(L, L->top, gt(thvalue(o))); + break; + default: + setnilvalue(L->top); + break; + } + api_incr_top(L); + lua_unlock(L); +} + + +/* +** set functions (stack -> Lua) +*/ + + +LUA_API void lua_settable (lua_State *L, int idx) { + StkId t; + lua_lock(L); + api_checknelems(L, 2); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + luaV_settable(L, t, L->top - 2, L->top - 1); + L->top -= 2; /* pop index and value */ + lua_unlock(L); +} + + +LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { + StkId t; + TValue key; + lua_lock(L); + api_checknelems(L, 1); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + setsvalue(L, &key, luaS_new(L, k)); + luaV_settable(L, t, &key, L->top - 1); + L->top--; /* pop value */ + lua_unlock(L); +} + + +LUA_API void lua_rawset (lua_State *L, int idx) { + StkId t; + lua_lock(L); + api_checknelems(L, 2); + t = index2adr(L, idx); + api_check(L, ttistable(t)); + setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); + luaC_barriert(L, hvalue(t), L->top-1); + L->top -= 2; + lua_unlock(L); +} + + +LUA_API void lua_rawseti (lua_State *L, int idx, int n) { + StkId o; + lua_lock(L); + api_checknelems(L, 1); + o = index2adr(L, idx); + api_check(L, ttistable(o)); + setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1); + luaC_barriert(L, hvalue(o), L->top-1); + L->top--; + lua_unlock(L); +} + + +LUA_API int lua_setmetatable (lua_State *L, int objindex) { + TValue *obj; + Table *mt; + lua_lock(L); + api_checknelems(L, 1); + obj = index2adr(L, objindex); + api_checkvalidindex(L, obj); + if (ttisnil(L->top - 1)) + mt = NULL; + else { + api_check(L, ttistable(L->top - 1)); + mt = hvalue(L->top - 1); + } + switch (ttype(obj)) { + case LUA_TTABLE: { + hvalue(obj)->metatable = mt; + if (mt) + luaC_objbarriert(L, hvalue(obj), mt); + break; + } + case LUA_TUSERDATA: { + uvalue(obj)->metatable = mt; + if (mt) + luaC_objbarrier(L, rawuvalue(obj), mt); + break; + } + default: { + G(L)->mt[ttype(obj)] = mt; + break; + } + } + L->top--; + lua_unlock(L); + return 1; +} + + +LUA_API int lua_setfenv (lua_State *L, int idx) { + StkId o; + int res = 1; + lua_lock(L); + api_checknelems(L, 1); + o = index2adr(L, idx); + api_checkvalidindex(L, o); + api_check(L, ttistable(L->top - 1)); + switch (ttype(o)) { + case LUA_TFUNCTION: + clvalue(o)->c.env = hvalue(L->top - 1); + break; + case LUA_TUSERDATA: + uvalue(o)->env = hvalue(L->top - 1); + break; + case LUA_TTHREAD: + sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1)); + break; + default: + res = 0; + break; + } + if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); + L->top--; + lua_unlock(L); + return res; +} + + +/* +** `load' and `call' functions (run Lua code) +*/ + + +#define adjustresults(L,nres) \ + { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; } + + +#define checkresults(L,na,nr) \ + api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na))) + + +LUA_API void lua_call (lua_State *L, int nargs, int nresults) { + StkId func; + lua_lock(L); + api_checknelems(L, nargs+1); + checkresults(L, nargs, nresults); + func = L->top - (nargs+1); + luaD_call(L, func, nresults); + adjustresults(L, nresults); + lua_unlock(L); +} + + + +/* +** Execute a protected call. +*/ +struct CallS { /* data to `f_call' */ + StkId func; + int nresults; +}; + + +static void f_call (lua_State *L, void *ud) { + struct CallS *c = cast(struct CallS *, ud); + luaD_call(L, c->func, c->nresults); +} + + + +LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { + struct CallS c; + int status; + ptrdiff_t func; + lua_lock(L); + api_checknelems(L, nargs+1); + checkresults(L, nargs, nresults); + if (errfunc == 0) + func = 0; + else { + StkId o = index2adr(L, errfunc); + api_checkvalidindex(L, o); + func = savestack(L, o); + } + c.func = L->top - (nargs+1); /* function to be called */ + c.nresults = nresults; + status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); + adjustresults(L, nresults); + lua_unlock(L); + return status; +} + + +/* +** Execute a protected C call. +*/ +struct CCallS { /* data to `f_Ccall' */ + lua_CFunction func; + void *ud; +}; + + +static void f_Ccall (lua_State *L, void *ud) { + struct CCallS *c = cast(struct CCallS *, ud); + Closure *cl; + cl = luaF_newCclosure(L, 0, getcurrenv(L)); + cl->c.f = c->func; + setclvalue(L, L->top, cl); /* push function */ + api_incr_top(L); + setpvalue(L->top, c->ud); /* push only argument */ + api_incr_top(L); + luaD_call(L, L->top - 2, 0); +} + + +LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { + struct CCallS c; + int status; + lua_lock(L); + c.func = func; + c.ud = ud; + status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0); + lua_unlock(L); + return status; +} + + +LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, + const char *chunkname) { + ZIO z; + int status; + lua_lock(L); + if (!chunkname) chunkname = "?"; + luaZ_init(L, &z, reader, data); + status = luaD_protectedparser(L, &z, chunkname); + lua_unlock(L); + return status; +} + + +LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { + int status; + TValue *o; + lua_lock(L); + api_checknelems(L, 1); + o = L->top - 1; + if (isLfunction(o)) + status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0); + else + status = 1; + lua_unlock(L); + return status; +} + + +LUA_API int lua_status (lua_State *L) { + return L->status; +} + + +/* +** Garbage-collection function +*/ + +LUA_API int lua_gc (lua_State *L, int what, int data) { + int res = 0; + global_State *g; + lua_lock(L); + g = G(L); + switch (what) { + case LUA_GCSTOP: { + g->GCthreshold = MAX_LUMEM; + break; + } + case LUA_GCRESTART: { + g->GCthreshold = g->totalbytes; + break; + } + case LUA_GCCOLLECT: { + luaC_fullgc(L); + break; + } + case LUA_GCCOUNT: { + /* GC values are expressed in Kbytes: #bytes/2^10 */ + res = cast_int(g->totalbytes >> 10); + break; + } + case LUA_GCCOUNTB: { + res = cast_int(g->totalbytes & 0x3ff); + break; + } + case LUA_GCSTEP: { + lu_mem a = (cast(lu_mem, data) << 10); + if (a <= g->totalbytes) + g->GCthreshold = g->totalbytes - a; + else + g->GCthreshold = 0; + while (g->GCthreshold <= g->totalbytes) { + luaC_step(L); + if (g->gcstate == GCSpause) { /* end of cycle? */ + res = 1; /* signal it */ + break; + } + } + break; + } + case LUA_GCSETPAUSE: { + res = g->gcpause; + g->gcpause = data; + break; + } + case LUA_GCSETSTEPMUL: { + res = g->gcstepmul; + g->gcstepmul = data; + break; + } + default: res = -1; /* invalid option */ + } + lua_unlock(L); + return res; +} + + + +/* +** miscellaneous functions +*/ + + +LUA_API int lua_error (lua_State *L) { + lua_lock(L); + api_checknelems(L, 1); + luaG_errormsg(L); + lua_unlock(L); + return 0; /* to avoid warnings */ +} + + +LUA_API int lua_next (lua_State *L, int idx) { + StkId t; + int more; + lua_lock(L); + t = index2adr(L, idx); + api_check(L, ttistable(t)); + more = luaH_next(L, hvalue(t), L->top - 1); + if (more) { + api_incr_top(L); + } + else /* no more elements */ + L->top -= 1; /* remove key */ + lua_unlock(L); + return more; +} + + +LUA_API void lua_concat (lua_State *L, int n) { + lua_lock(L); + api_checknelems(L, n); + if (n >= 2) { + luaC_checkGC(L); + luaV_concat(L, n, cast_int(L->top - L->base) - 1); + L->top -= (n-1); + } + else if (n == 0) { /* push empty string */ + setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); + api_incr_top(L); + } + /* else n == 1; nothing to do */ + lua_unlock(L); +} + + +LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { + lua_Alloc f; + lua_lock(L); + if (ud) *ud = G(L)->ud; + f = G(L)->frealloc; + lua_unlock(L); + return f; +} + + +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { + lua_lock(L); + G(L)->ud = ud; + G(L)->frealloc = f; + lua_unlock(L); +} + + +LUA_API void *lua_newuserdata (lua_State *L, size_t size) { + Udata *u; + lua_lock(L); + luaC_checkGC(L); + u = luaS_newudata(L, size, getcurrenv(L)); + setuvalue(L, L->top, u); + api_incr_top(L); + lua_unlock(L); + return u + 1; +} + + + + +static const char *aux_upvalue (StkId fi, int n, TValue **val) { + Closure *f; + if (!ttisfunction(fi)) return NULL; + f = clvalue(fi); + if (f->c.isC) { + if (!(1 <= n && n <= f->c.nupvalues)) return NULL; + *val = &f->c.upvalue[n-1]; + return ""; + } + else { + Proto *p = f->l.p; + if (!(1 <= n && n <= p->sizeupvalues)) return NULL; + *val = f->l.upvals[n-1]->v; + return getstr(p->upvalues[n-1]); + } +} + + +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { + const char *name; + TValue *val; + lua_lock(L); + name = aux_upvalue(index2adr(L, funcindex), n, &val); + if (name) { + setobj2s(L, L->top, val); + api_incr_top(L); + } + lua_unlock(L); + return name; +} + + +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { + const char *name; + TValue *val; + StkId fi; + lua_lock(L); + fi = index2adr(L, funcindex); + api_checknelems(L, 1); + name = aux_upvalue(fi, n, &val); + if (name) { + L->top--; + setobj(L, val, L->top); + luaC_barrier(L, clvalue(fi), L->top); + } + lua_unlock(L); + return name; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lapi.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lapi.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,16 @@ +/* +** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions from Lua API +** See Copyright Notice in lua.h +*/ + +#ifndef lapi_h +#define lapi_h + + +#include "lobject.h" + + +LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lauxlib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lauxlib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,652 @@ +/* +** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include +#include +#include + + +/* This file uses only the official API of Lua. +** Any function declared here could be written as an application function. +*/ + +#define lauxlib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" + + +#define FREELIST_REF 0 /* free list of references */ + + +/* convert a stack index to positive */ +#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ + lua_gettop(L) + (i) + 1) + + +/* +** {====================================================== +** Error-report functions +** ======================================================= +*/ + + +LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { + lua_Debug ar; + if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ + return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); + lua_getinfo(L, "n", &ar); + if (strcmp(ar.namewhat, "method") == 0) { + narg--; /* do not count `self' */ + if (narg == 0) /* error is in the self argument itself? */ + return luaL_error(L, "calling " LUA_QS " on bad self (%s)", + ar.name, extramsg); + } + if (ar.name == NULL) + ar.name = "?"; + return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", + narg, ar.name, extramsg); +} + + +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { + const char *msg = lua_pushfstring(L, "%s expected, got %s", + tname, luaL_typename(L, narg)); + return luaL_argerror(L, narg, msg); +} + + +static void tag_error (lua_State *L, int narg, int tag) { + luaL_typerror(L, narg, lua_typename(L, tag)); +} + + +LUALIB_API void luaL_where (lua_State *L, int level) { + lua_Debug ar; + if (lua_getstack(L, level, &ar)) { /* check function at level */ + lua_getinfo(L, "Sl", &ar); /* get info about it */ + if (ar.currentline > 0) { /* is there info? */ + lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); + return; + } + } + lua_pushliteral(L, ""); /* else, no information available... */ +} + + +LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { + va_list argp; + va_start(argp, fmt); + luaL_where(L, 1); + lua_pushvfstring(L, fmt, argp); + va_end(argp); + lua_concat(L, 2); + return lua_error(L); +} + +/* }====================================================== */ + + +LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, + const char *const lst[]) { + const char *name = (def) ? luaL_optstring(L, narg, def) : + luaL_checkstring(L, narg); + int i; + for (i=0; lst[i]; i++) + if (strcmp(lst[i], name) == 0) + return i; + return luaL_argerror(L, narg, + lua_pushfstring(L, "invalid option " LUA_QS, name)); +} + + +LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */ + if (!lua_isnil(L, -1)) /* name already in use? */ + return 0; /* leave previous value on top, but return 0 */ + lua_pop(L, 1); + lua_newtable(L); /* create metatable */ + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ + return 1; +} + + +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { + void *p = lua_touserdata(L, ud); + if (p != NULL) { /* value is a userdata? */ + if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ + lua_pop(L, 2); /* remove both metatables */ + return p; + } + } + } + luaL_typerror(L, ud, tname); /* else error */ + return NULL; /* to avoid warnings */ +} + + +LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { + if (!lua_checkstack(L, space)) + luaL_error(L, "stack overflow (%s)", mes); +} + + +LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { + if (lua_type(L, narg) != t) + tag_error(L, narg, t); +} + + +LUALIB_API void luaL_checkany (lua_State *L, int narg) { + if (lua_type(L, narg) == LUA_TNONE) + luaL_argerror(L, narg, "value expected"); +} + + +LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { + const char *s = lua_tolstring(L, narg, len); + if (!s) tag_error(L, narg, LUA_TSTRING); + return s; +} + + +LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, + const char *def, size_t *len) { + if (lua_isnoneornil(L, narg)) { + if (len) + *len = (def ? strlen(def) : 0); + return def; + } + else return luaL_checklstring(L, narg, len); +} + + +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { + lua_Number d = lua_tonumber(L, narg); + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ + tag_error(L, narg, LUA_TNUMBER); + return d; +} + + +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { + return luaL_opt(L, luaL_checknumber, narg, def); +} + + +LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { + lua_Integer d = lua_tointeger(L, narg); + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ + tag_error(L, narg, LUA_TNUMBER); + return d; +} + + +LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, + lua_Integer def) { + return luaL_opt(L, luaL_checkinteger, narg, def); +} + + +LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { + if (!lua_getmetatable(L, obj)) /* no metatable? */ + return 0; + lua_pushstring(L, event); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { + lua_pop(L, 2); /* remove metatable and metafield */ + return 0; + } + else { + lua_remove(L, -2); /* remove only metatable */ + return 1; + } +} + + +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { + obj = abs_index(L, obj); + if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ + return 0; + lua_pushvalue(L, obj); + lua_call(L, 1, 1); + return 1; +} + + +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, + const luaL_Reg *l) { + luaI_openlib(L, libname, l, 0); +} + + +static int libsize (const luaL_Reg *l) { + int size = 0; + for (; l->name; l++) size++; + return size; +} + + +LUALIB_API void luaI_openlib (lua_State *L, const char *libname, + const luaL_Reg *l, int nup) { + if (libname) { + int size = libsize(l); + /* check whether lib already exists */ + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); + lua_getfield(L, -1, libname); /* get _LOADED[libname] */ + if (!lua_istable(L, -1)) { /* not found? */ + lua_pop(L, 1); /* remove previous result */ + /* try global variable (and create one if it does not exist) */ + if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) + luaL_error(L, "name conflict for module " LUA_QS, libname); + lua_pushvalue(L, -1); + lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ + } + lua_remove(L, -2); /* remove _LOADED table */ + lua_insert(L, -(nup+1)); /* move library table to below upvalues */ + } + for (; l->name; l++) { + int i; + for (i=0; ifunc, nup); + lua_setfield(L, -(nup+2), l->name); + } + lua_pop(L, nup); /* remove upvalues */ +} + + + +/* +** {====================================================== +** getn-setn: size for arrays +** ======================================================= +*/ + +#if defined(LUA_COMPAT_GETN) + +static int checkint (lua_State *L, int topop) { + int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1; + lua_pop(L, topop); + return n; +} + + +static void getsizes (lua_State *L) { + lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); + if (lua_isnil(L, -1)) { /* no `size' table? */ + lua_pop(L, 1); /* remove nil */ + lua_newtable(L); /* create it */ + lua_pushvalue(L, -1); /* `size' will be its own metatable */ + lua_setmetatable(L, -2); + lua_pushliteral(L, "kv"); + lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */ + } +} + + +LUALIB_API void luaL_setn (lua_State *L, int t, int n) { + t = abs_index(L, t); + lua_pushliteral(L, "n"); + lua_rawget(L, t); + if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ + lua_pushliteral(L, "n"); /* use it */ + lua_pushinteger(L, n); + lua_rawset(L, t); + } + else { /* use `sizes' */ + getsizes(L); + lua_pushvalue(L, t); + lua_pushinteger(L, n); + lua_rawset(L, -3); /* sizes[t] = n */ + lua_pop(L, 1); /* remove `sizes' */ + } +} + + +LUALIB_API int luaL_getn (lua_State *L, int t) { + int n; + t = abs_index(L, t); + lua_pushliteral(L, "n"); /* try t.n */ + lua_rawget(L, t); + if ((n = checkint(L, 1)) >= 0) return n; + getsizes(L); /* else try sizes[t] */ + lua_pushvalue(L, t); + lua_rawget(L, -2); + if ((n = checkint(L, 2)) >= 0) return n; + return (int)lua_objlen(L, t); +} + +#endif + +/* }====================================================== */ + + + +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, + const char *r) { + const char *wild; + size_t l = strlen(p); + luaL_Buffer b; + luaL_buffinit(L, &b); + while ((wild = strstr(s, p)) != NULL) { + luaL_addlstring(&b, s, wild - s); /* push prefix */ + luaL_addstring(&b, r); /* push replacement in place of pattern */ + s = wild + l; /* continue after `p' */ + } + luaL_addstring(&b, s); /* push last suffix */ + luaL_pushresult(&b); + return lua_tostring(L, -1); +} + + +LUALIB_API const char *luaL_findtable (lua_State *L, int idx, + const char *fname, int szhint) { + const char *e; + lua_pushvalue(L, idx); + do { + e = strchr(fname, '.'); + if (e == NULL) e = fname + strlen(fname); + lua_pushlstring(L, fname, e - fname); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { /* no such field? */ + lua_pop(L, 1); /* remove this nil */ + lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ + lua_pushlstring(L, fname, e - fname); + lua_pushvalue(L, -2); + lua_settable(L, -4); /* set new table into field */ + } + else if (!lua_istable(L, -1)) { /* field has a non-table value? */ + lua_pop(L, 2); /* remove table and value */ + return fname; /* return problematic part of the name */ + } + lua_remove(L, -2); /* remove previous table */ + fname = e + 1; + } while (*e == '.'); + return NULL; +} + + + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + + +#define bufflen(B) ((B)->p - (B)->buffer) +#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) + +#define LIMIT (LUA_MINSTACK/2) + + +static int emptybuffer (luaL_Buffer *B) { + size_t l = bufflen(B); + if (l == 0) return 0; /* put nothing on stack */ + else { + lua_pushlstring(B->L, B->buffer, l); + B->p = B->buffer; + B->lvl++; + return 1; + } +} + + +static void adjuststack (luaL_Buffer *B) { + if (B->lvl > 1) { + lua_State *L = B->L; + int toget = 1; /* number of levels to concat */ + size_t toplen = lua_strlen(L, -1); + do { + size_t l = lua_strlen(L, -(toget+1)); + if (B->lvl - toget + 1 >= LIMIT || toplen > l) { + toplen += l; + toget++; + } + else break; + } while (toget < B->lvl); + lua_concat(L, toget); + B->lvl = B->lvl - toget + 1; + } +} + + +LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) { + if (emptybuffer(B)) + adjuststack(B); + return B->buffer; +} + + +LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { + while (l--) + luaL_addchar(B, *s++); +} + + +LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { + luaL_addlstring(B, s, strlen(s)); +} + + +LUALIB_API void luaL_pushresult (luaL_Buffer *B) { + emptybuffer(B); + lua_concat(B->L, B->lvl); + B->lvl = 1; +} + + +LUALIB_API void luaL_addvalue (luaL_Buffer *B) { + lua_State *L = B->L; + size_t vl; + const char *s = lua_tolstring(L, -1, &vl); + if (vl <= bufffree(B)) { /* fit into buffer? */ + memcpy(B->p, s, vl); /* put it there */ + B->p += vl; + lua_pop(L, 1); /* remove from stack */ + } + else { + if (emptybuffer(B)) + lua_insert(L, -2); /* put buffer before new value */ + B->lvl++; /* add new value into B stack */ + adjuststack(B); + } +} + + +LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { + B->L = L; + B->p = B->buffer; + B->lvl = 0; +} + +/* }====================================================== */ + + +LUALIB_API int luaL_ref (lua_State *L, int t) { + int ref; + t = abs_index(L, t); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); /* remove from stack */ + return LUA_REFNIL; /* `nil' has a unique fixed reference */ + } + lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ + ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ + lua_pop(L, 1); /* remove it from stack */ + if (ref != 0) { /* any free element? */ + lua_rawgeti(L, t, ref); /* remove it from list */ + lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ + } + else { /* no free elements */ + ref = (int)lua_objlen(L, t); + ref++; /* create new reference */ + } + lua_rawseti(L, t, ref); + return ref; +} + + +LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { + if (ref >= 0) { + t = abs_index(L, t); + lua_rawgeti(L, t, FREELIST_REF); + lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ + lua_pushinteger(L, ref); + lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ + } +} + + + +/* +** {====================================================== +** Load functions +** ======================================================= +*/ + +typedef struct LoadF { + int extraline; + FILE *f; + char buff[LUAL_BUFFERSIZE]; +} LoadF; + + +static const char *getF (lua_State *L, void *ud, size_t *size) { + LoadF *lf = (LoadF *)ud; + (void)L; + if (lf->extraline) { + lf->extraline = 0; + *size = 1; + return "\n"; + } + if (feof(lf->f)) return NULL; + *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); + return (*size > 0) ? lf->buff : NULL; +} + + +static int errfile (lua_State *L, const char *what, int fnameindex) { + const char *serr = strerror(errno); + const char *filename = lua_tostring(L, fnameindex) + 1; + lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); + lua_remove(L, fnameindex); + return LUA_ERRFILE; +} + + +LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { + LoadF lf; + int status, readstatus; + int c; + int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ + lf.extraline = 0; + if (filename == NULL) { + lua_pushliteral(L, "=stdin"); + lf.f = stdin; + } + else { + lua_pushfstring(L, "@%s", filename); + lf.f = fopen(filename, "r"); + if (lf.f == NULL) return errfile(L, "open", fnameindex); + } + c = getc(lf.f); + if (c == '#') { /* Unix exec. file? */ + lf.extraline = 1; + while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ + if (c == '\n') c = getc(lf.f); + } + if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ + lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ + if (lf.f == NULL) return errfile(L, "reopen", fnameindex); + /* skip eventual `#!...' */ + while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; + lf.extraline = 0; + } + ungetc(c, lf.f); + status = lua_load(L, getF, &lf, lua_tostring(L, -1)); + readstatus = ferror(lf.f); + if (filename) fclose(lf.f); /* close file (even in case of errors) */ + if (readstatus) { + lua_settop(L, fnameindex); /* ignore results from `lua_load' */ + return errfile(L, "read", fnameindex); + } + lua_remove(L, fnameindex); + return status; +} + + +typedef struct LoadS { + const char *s; + size_t size; +} LoadS; + + +static const char *getS (lua_State *L, void *ud, size_t *size) { + LoadS *ls = (LoadS *)ud; + (void)L; + if (ls->size == 0) return NULL; + *size = ls->size; + ls->size = 0; + return ls->s; +} + + +LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, + const char *name) { + LoadS ls; + ls.s = buff; + ls.size = size; + return lua_load(L, getS, &ls, name); +} + + +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) { + return luaL_loadbuffer(L, s, strlen(s), s); +} + + + +/* }====================================================== */ + + +static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { + (void)ud; + (void)osize; + if (nsize == 0) { + free(ptr); + return NULL; + } + else + return realloc(ptr, nsize); +} + + +static int panic (lua_State *L) { + (void)L; /* to avoid warnings */ + fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", + lua_tostring(L, -1)); + return 0; +} + + +LUALIB_API lua_State *luaL_newstate (void) { + lua_State *L = lua_newstate(l_alloc, NULL); + if (L) lua_atpanic(L, &panic); + return L; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lauxlib.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lauxlib.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,174 @@ +/* +** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include +#include + +#include "lua.h" + + +#if defined(LUA_COMPAT_GETN) +LUALIB_API int (luaL_getn) (lua_State *L, int t); +LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); +#else +#define luaL_getn(L,i) ((int)lua_objlen(L, i)) +#define luaL_setn(L,i,j) ((void)0) /* no op! */ +#endif + +#if defined(LUA_COMPAT_OPENLIB) +#define luaI_openlib luaL_openlib +#endif + + +/* extra error code for `luaL_load' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + + +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; + + + +LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, + const luaL_Reg *l, int nup); +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, + const luaL_Reg *l); +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); +LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, + size_t *l); +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, + const char *def, size_t *l); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); + +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, + lua_Integer def); + +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); +LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int narg); + +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); + +LUALIB_API void (luaL_where) (lua_State *L, int lvl); +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); + +LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, + const char *const lst[]); + +LUALIB_API int (luaL_ref) (lua_State *L, int t); +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); + +LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); +LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, + const char *name); +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); + +LUALIB_API lua_State *(luaL_newstate) (void); + + +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, + const char *r); + +LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, + const char *fname, int szhint); + + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define luaL_argcheck(L, cond,numarg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +#define luaL_dofile(L, fn) \ + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_dostring(L, s) \ + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + + + +typedef struct luaL_Buffer { + char *p; /* current position in buffer */ + int lvl; /* number of strings in the stack (level) */ + lua_State *L; + char buffer[LUAL_BUFFERSIZE]; +} luaL_Buffer; + +#define luaL_addchar(B,c) \ + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ + (*(B)->p++ = (char)(c))) + +/* compatibility only */ +#define luaL_putchar(B,c) luaL_addchar(B,c) + +#define luaL_addsize(B,n) ((B)->p += (n)) + +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); +LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); + + +/* }====================================================== */ + + +/* compatibility with ref system */ + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) + +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) + +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) + + +#define luaL_reg luaL_Reg + +#endif + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lbaselib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lbaselib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,653 @@ +/* +** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $ +** Basic library +** See Copyright Notice in lua.h +*/ + + + +#include +#include +#include +#include + +#define lbaselib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + + + +/* +** If your system does not support `stdout', you can just remove this function. +** If you need, you can define your own `print' function, following this +** model but changing `fputs' to put the strings at a proper place +** (a console window or a log file, for instance). +*/ +static int luaB_print (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int i; + lua_getglobal(L, "tostring"); + for (i=1; i<=n; i++) { + const char *s; + lua_pushvalue(L, -1); /* function to be called */ + lua_pushvalue(L, i); /* value to print */ + lua_call(L, 1, 1); + s = lua_tostring(L, -1); /* get result */ + if (s == NULL) + return luaL_error(L, LUA_QL("tostring") " must return a string to " + LUA_QL("print")); + if (i>1) fputs("\t", stdout); + fputs(s, stdout); + lua_pop(L, 1); /* pop result */ + } + fputs("\n", stdout); + return 0; +} + + +static int luaB_tonumber (lua_State *L) { + int base = luaL_optint(L, 2, 10); + if (base == 10) { /* standard conversion */ + luaL_checkany(L, 1); + if (lua_isnumber(L, 1)) { + lua_pushnumber(L, lua_tonumber(L, 1)); + return 1; + } + } + else { + const char *s1 = luaL_checkstring(L, 1); + char *s2; + unsigned long n; + luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); + n = strtoul(s1, &s2, base); + if (s1 != s2) { /* at least one valid digit? */ + while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ + if (*s2 == '\0') { /* no invalid trailing characters? */ + lua_pushnumber(L, (lua_Number)n); + return 1; + } + } + } + lua_pushnil(L); /* else not a number */ + return 1; +} + + +static int luaB_error (lua_State *L) { + int level = luaL_optint(L, 2, 1); + lua_settop(L, 1); + if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ + luaL_where(L, level); + lua_pushvalue(L, 1); + lua_concat(L, 2); + } + return lua_error(L); +} + + +static int luaB_getmetatable (lua_State *L) { + luaL_checkany(L, 1); + if (!lua_getmetatable(L, 1)) { + lua_pushnil(L); + return 1; /* no metatable */ + } + luaL_getmetafield(L, 1, "__metatable"); + return 1; /* returns either __metatable field (if present) or metatable */ +} + + +static int luaB_setmetatable (lua_State *L) { + int t = lua_type(L, 2); + luaL_checktype(L, 1, LUA_TTABLE); + luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, + "nil or table expected"); + if (luaL_getmetafield(L, 1, "__metatable")) + luaL_error(L, "cannot change a protected metatable"); + lua_settop(L, 2); + lua_setmetatable(L, 1); + return 1; +} + + +static void getfunc (lua_State *L, int opt) { + if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); + else { + lua_Debug ar; + int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); + luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); + if (lua_getstack(L, level, &ar) == 0) + luaL_argerror(L, 1, "invalid level"); + lua_getinfo(L, "f", &ar); + if (lua_isnil(L, -1)) + luaL_error(L, "no function environment for tail call at level %d", + level); + } +} + + +static int luaB_getfenv (lua_State *L) { + getfunc(L, 1); + if (lua_iscfunction(L, -1)) /* is a C function? */ + lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */ + else + lua_getfenv(L, -1); + return 1; +} + + +static int luaB_setfenv (lua_State *L) { + luaL_checktype(L, 2, LUA_TTABLE); + getfunc(L, 0); + lua_pushvalue(L, 2); + if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) { + /* change environment of current thread */ + lua_pushthread(L); + lua_insert(L, -2); + lua_setfenv(L, -2); + return 0; + } + else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) + luaL_error(L, + LUA_QL("setfenv") " cannot change environment of given object"); + return 1; +} + + +static int luaB_rawequal (lua_State *L) { + luaL_checkany(L, 1); + luaL_checkany(L, 2); + lua_pushboolean(L, lua_rawequal(L, 1, 2)); + return 1; +} + + +static int luaB_rawget (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checkany(L, 2); + lua_settop(L, 2); + lua_rawget(L, 1); + return 1; +} + +static int luaB_rawset (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checkany(L, 2); + luaL_checkany(L, 3); + lua_settop(L, 3); + lua_rawset(L, 1); + return 1; +} + + +static int luaB_gcinfo (lua_State *L) { + lua_pushinteger(L, lua_getgccount(L)); + return 1; +} + + +static int luaB_collectgarbage (lua_State *L) { + static const char *const opts[] = {"stop", "restart", "collect", + "count", "step", "setpause", "setstepmul", NULL}; + static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, + LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; + int o = luaL_checkoption(L, 1, "collect", opts); + int ex = luaL_optint(L, 2, 0); + int res = lua_gc(L, optsnum[o], ex); + switch (optsnum[o]) { + case LUA_GCCOUNT: { + int b = lua_gc(L, LUA_GCCOUNTB, 0); + lua_pushnumber(L, res + ((lua_Number)b/1024)); + return 1; + } + case LUA_GCSTEP: { + lua_pushboolean(L, res); + return 1; + } + default: { + lua_pushnumber(L, res); + return 1; + } + } +} + + +static int luaB_type (lua_State *L) { + luaL_checkany(L, 1); + lua_pushstring(L, luaL_typename(L, 1)); + return 1; +} + + +static int luaB_next (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 2); /* create a 2nd argument if there isn't one */ + if (lua_next(L, 1)) + return 2; + else { + lua_pushnil(L); + return 1; + } +} + + +static int luaB_pairs (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ + lua_pushvalue(L, 1); /* state, */ + lua_pushnil(L); /* and initial value */ + return 3; +} + + +static int ipairsaux (lua_State *L) { + int i = luaL_checkint(L, 2); + luaL_checktype(L, 1, LUA_TTABLE); + i++; /* next value */ + lua_pushinteger(L, i); + lua_rawgeti(L, 1, i); + return (lua_isnil(L, -1)) ? 0 : 2; +} + + +static int luaB_ipairs (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ + lua_pushvalue(L, 1); /* state, */ + lua_pushinteger(L, 0); /* and initial value */ + return 3; +} + + +static int load_aux (lua_State *L, int status) { + if (status == 0) /* OK? */ + return 1; + else { + lua_pushnil(L); + lua_insert(L, -2); /* put before error message */ + return 2; /* return nil plus error message */ + } +} + + +static int luaB_loadstring (lua_State *L) { + size_t l; + const char *s = luaL_checklstring(L, 1, &l); + const char *chunkname = luaL_optstring(L, 2, s); + return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); +} + + +static int luaB_loadfile (lua_State *L) { + const char *fname = luaL_optstring(L, 1, NULL); + return load_aux(L, luaL_loadfile(L, fname)); +} + + +/* +** Reader for generic `load' function: `lua_load' uses the +** stack for internal stuff, so the reader cannot change the +** stack top. Instead, it keeps its resulting string in a +** reserved slot inside the stack. +*/ +static const char *generic_reader (lua_State *L, void *ud, size_t *size) { + (void)ud; /* to avoid warnings */ + luaL_checkstack(L, 2, "too many nested functions"); + lua_pushvalue(L, 1); /* get function */ + lua_call(L, 0, 1); /* call it */ + if (lua_isnil(L, -1)) { + *size = 0; + return NULL; + } + else if (lua_isstring(L, -1)) { + lua_replace(L, 3); /* save string in a reserved stack slot */ + return lua_tolstring(L, 3, size); + } + else luaL_error(L, "reader function must return a string"); + return NULL; /* to avoid warnings */ +} + + +static int luaB_load (lua_State *L) { + int status; + const char *cname = luaL_optstring(L, 2, "=(load)"); + luaL_checktype(L, 1, LUA_TFUNCTION); + lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ + status = lua_load(L, generic_reader, NULL, cname); + return load_aux(L, status); +} + + +static int luaB_dofile (lua_State *L) { + const char *fname = luaL_optstring(L, 1, NULL); + int n = lua_gettop(L); + if (luaL_loadfile(L, fname) != 0) lua_error(L); + lua_call(L, 0, LUA_MULTRET); + return lua_gettop(L) - n; +} + + +static int luaB_assert (lua_State *L) { + luaL_checkany(L, 1); + if (!lua_toboolean(L, 1)) + return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); + return lua_gettop(L); +} + + +static int luaB_unpack (lua_State *L) { + int i, e, n; + luaL_checktype(L, 1, LUA_TTABLE); + i = luaL_optint(L, 2, 1); + e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); + if (i > e) return 0; /* empty range */ + n = e - i + 1; /* number of elements */ + if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ + return luaL_error(L, "too many results to unpack"); + lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ + while (i++ < e) /* push arg[i + 1...e] */ + lua_rawgeti(L, 1, i); + return n; +} + + +static int luaB_select (lua_State *L) { + int n = lua_gettop(L); + if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { + lua_pushinteger(L, n-1); + return 1; + } + else { + int i = luaL_checkint(L, 1); + if (i < 0) i = n + i; + else if (i > n) i = n; + luaL_argcheck(L, 1 <= i, 1, "index out of range"); + return n - i; + } +} + + +static int luaB_pcall (lua_State *L) { + int status; + luaL_checkany(L, 1); + status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); + lua_pushboolean(L, (status == 0)); + lua_insert(L, 1); + return lua_gettop(L); /* return status + all results */ +} + + +static int luaB_xpcall (lua_State *L) { + int status; + luaL_checkany(L, 2); + lua_settop(L, 2); + lua_insert(L, 1); /* put error function under function to be called */ + status = lua_pcall(L, 0, LUA_MULTRET, 1); + lua_pushboolean(L, (status == 0)); + lua_replace(L, 1); + return lua_gettop(L); /* return status + all results */ +} + + +static int luaB_tostring (lua_State *L) { + luaL_checkany(L, 1); + if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ + return 1; /* use its value */ + switch (lua_type(L, 1)) { + case LUA_TNUMBER: + lua_pushstring(L, lua_tostring(L, 1)); + break; + case LUA_TSTRING: + lua_pushvalue(L, 1); + break; + case LUA_TBOOLEAN: + lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); + break; + case LUA_TNIL: + lua_pushliteral(L, "nil"); + break; + default: + lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); + break; + } + return 1; +} + + +static int luaB_newproxy (lua_State *L) { + lua_settop(L, 1); + lua_newuserdata(L, 0); /* create proxy */ + if (lua_toboolean(L, 1) == 0) + return 1; /* no metatable */ + else if (lua_isboolean(L, 1)) { + lua_newtable(L); /* create a new metatable `m' ... */ + lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ + lua_pushboolean(L, 1); + lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ + } + else { + int validproxy = 0; /* to check if weaktable[metatable(u)] == true */ + if (lua_getmetatable(L, 1)) { + lua_rawget(L, lua_upvalueindex(1)); + validproxy = lua_toboolean(L, -1); + lua_pop(L, 1); /* remove value */ + } + luaL_argcheck(L, validproxy, 1, "boolean or proxy expected"); + lua_getmetatable(L, 1); /* metatable is valid; get it */ + } + lua_setmetatable(L, 2); + return 1; +} + + +static const luaL_Reg base_funcs[] = { + {"assert", luaB_assert}, + {"collectgarbage", luaB_collectgarbage}, + {"dofile", luaB_dofile}, + {"error", luaB_error}, + {"gcinfo", luaB_gcinfo}, + {"getfenv", luaB_getfenv}, + {"getmetatable", luaB_getmetatable}, + {"loadfile", luaB_loadfile}, + {"load", luaB_load}, + {"loadstring", luaB_loadstring}, + {"next", luaB_next}, + {"pcall", luaB_pcall}, + {"print", luaB_print}, + {"rawequal", luaB_rawequal}, + {"rawget", luaB_rawget}, + {"rawset", luaB_rawset}, + {"select", luaB_select}, + {"setfenv", luaB_setfenv}, + {"setmetatable", luaB_setmetatable}, + {"tonumber", luaB_tonumber}, + {"tostring", luaB_tostring}, + {"type", luaB_type}, + {"unpack", luaB_unpack}, + {"xpcall", luaB_xpcall}, + {NULL, NULL} +}; + + +/* +** {====================================================== +** Coroutine library +** ======================================================= +*/ + +#define CO_RUN 0 /* running */ +#define CO_SUS 1 /* suspended */ +#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */ +#define CO_DEAD 3 + +static const char *const statnames[] = + {"running", "suspended", "normal", "dead"}; + +static int costatus (lua_State *L, lua_State *co) { + if (L == co) return CO_RUN; + switch (lua_status(co)) { + case LUA_YIELD: + return CO_SUS; + case 0: { + lua_Debug ar; + if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ + return CO_NOR; /* it is running */ + else if (lua_gettop(co) == 0) + return CO_DEAD; + else + return CO_SUS; /* initial state */ + } + default: /* some error occured */ + return CO_DEAD; + } +} + + +static int luaB_costatus (lua_State *L) { + lua_State *co = lua_tothread(L, 1); + luaL_argcheck(L, co, 1, "coroutine expected"); + lua_pushstring(L, statnames[costatus(L, co)]); + return 1; +} + + +static int auxresume (lua_State *L, lua_State *co, int narg) { + int status = costatus(L, co); + if (!lua_checkstack(co, narg)) + luaL_error(L, "too many arguments to resume"); + if (status != CO_SUS) { + lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]); + return -1; /* error flag */ + } + lua_xmove(L, co, narg); + lua_setlevel(L, co); + status = lua_resume(co, narg); + if (status == 0 || status == LUA_YIELD) { + int nres = lua_gettop(co); + if (!lua_checkstack(L, nres + 1)) + luaL_error(L, "too many results to resume"); + lua_xmove(co, L, nres); /* move yielded values */ + return nres; + } + else { + lua_xmove(co, L, 1); /* move error message */ + return -1; /* error flag */ + } +} + + +static int luaB_coresume (lua_State *L) { + lua_State *co = lua_tothread(L, 1); + int r; + luaL_argcheck(L, co, 1, "coroutine expected"); + r = auxresume(L, co, lua_gettop(L) - 1); + if (r < 0) { + lua_pushboolean(L, 0); + lua_insert(L, -2); + return 2; /* return false + error message */ + } + else { + lua_pushboolean(L, 1); + lua_insert(L, -(r + 1)); + return r + 1; /* return true + `resume' returns */ + } +} + + +static int luaB_auxwrap (lua_State *L) { + lua_State *co = lua_tothread(L, lua_upvalueindex(1)); + int r = auxresume(L, co, lua_gettop(L)); + if (r < 0) { + if (lua_isstring(L, -1)) { /* error object is a string? */ + luaL_where(L, 1); /* add extra info */ + lua_insert(L, -2); + lua_concat(L, 2); + } + lua_error(L); /* propagate error */ + } + return r; +} + + +static int luaB_cocreate (lua_State *L) { + lua_State *NL = lua_newthread(L); + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, + "Lua function expected"); + lua_pushvalue(L, 1); /* move function to top */ + lua_xmove(L, NL, 1); /* move function from L to NL */ + return 1; +} + + +static int luaB_cowrap (lua_State *L) { + luaB_cocreate(L); + lua_pushcclosure(L, luaB_auxwrap, 1); + return 1; +} + + +static int luaB_yield (lua_State *L) { + return lua_yield(L, lua_gettop(L)); +} + + +static int luaB_corunning (lua_State *L) { + if (lua_pushthread(L)) + lua_pushnil(L); /* main thread is not a coroutine */ + return 1; +} + + +static const luaL_Reg co_funcs[] = { + {"create", luaB_cocreate}, + {"resume", luaB_coresume}, + {"running", luaB_corunning}, + {"status", luaB_costatus}, + {"wrap", luaB_cowrap}, + {"yield", luaB_yield}, + {NULL, NULL} +}; + +/* }====================================================== */ + + +static void auxopen (lua_State *L, const char *name, + lua_CFunction f, lua_CFunction u) { + lua_pushcfunction(L, u); + lua_pushcclosure(L, f, 1); + lua_setfield(L, -2, name); +} + + +static void base_open (lua_State *L) { + /* set global _G */ + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_setglobal(L, "_G"); + /* open lib into global table */ + luaL_register(L, "_G", base_funcs); + lua_pushliteral(L, LUA_VERSION); + lua_setglobal(L, "_VERSION"); /* set global _VERSION */ + /* `ipairs' and `pairs' need auxliliary functions as upvalues */ + auxopen(L, "ipairs", luaB_ipairs, ipairsaux); + auxopen(L, "pairs", luaB_pairs, luaB_next); + /* `newproxy' needs a weaktable as upvalue */ + lua_createtable(L, 0, 1); /* new table `w' */ + lua_pushvalue(L, -1); /* `w' will be its own metatable */ + lua_setmetatable(L, -2); + lua_pushliteral(L, "kv"); + lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ + lua_pushcclosure(L, luaB_newproxy, 1); + lua_setglobal(L, "newproxy"); /* set global `newproxy' */ +} + + +LUALIB_API int luaopen_base (lua_State *L) { + base_open(L); + luaL_register(L, LUA_COLIBNAME, co_funcs); + return 2; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lcode.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lcode.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,839 @@ +/* +** $Id: lcode.c,v 2.25.1.3 2007/12/28 15:32:23 roberto Exp $ +** Code generator for Lua +** See Copyright Notice in lua.h +*/ + + +#include + +#define lcode_c +#define LUA_CORE + +#include "lua.h" + +#include "lcode.h" +#include "ldebug.h" +#include "ldo.h" +#include "lgc.h" +#include "llex.h" +#include "lmem.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lparser.h" +#include "ltable.h" + + +#define hasjumps(e) ((e)->t != (e)->f) + + +static int isnumeral(expdesc *e) { + return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); +} + + +void luaK_nil (FuncState *fs, int from, int n) { + Instruction *previous; + if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ + if (fs->pc == 0) { /* function start? */ + if (from >= fs->nactvar) + return; /* positions are already clean */ + } + else { + previous = &fs->f->code[fs->pc-1]; + if (GET_OPCODE(*previous) == OP_LOADNIL) { + int pfrom = GETARG_A(*previous); + int pto = GETARG_B(*previous); + if (pfrom <= from && from <= pto+1) { /* can connect both? */ + if (from+n-1 > pto) + SETARG_B(*previous, from+n-1); + return; + } + } + } + } + luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */ +} + + +int luaK_jump (FuncState *fs) { + int jpc = fs->jpc; /* save list of jumps to here */ + int j; + fs->jpc = NO_JUMP; + j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); + luaK_concat(fs, &j, jpc); /* keep them on hold */ + return j; +} + + +void luaK_ret (FuncState *fs, int first, int nret) { + luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); +} + + +static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { + luaK_codeABC(fs, op, A, B, C); + return luaK_jump(fs); +} + + +static void fixjump (FuncState *fs, int pc, int dest) { + Instruction *jmp = &fs->f->code[pc]; + int offset = dest-(pc+1); + lua_assert(dest != NO_JUMP); + if (abs(offset) > MAXARG_sBx) + luaX_syntaxerror(fs->ls, "control structure too long"); + SETARG_sBx(*jmp, offset); +} + + +/* +** returns current `pc' and marks it as a jump target (to avoid wrong +** optimizations with consecutive instructions not in the same basic block). +*/ +int luaK_getlabel (FuncState *fs) { + fs->lasttarget = fs->pc; + return fs->pc; +} + + +static int getjump (FuncState *fs, int pc) { + int offset = GETARG_sBx(fs->f->code[pc]); + if (offset == NO_JUMP) /* point to itself represents end of list */ + return NO_JUMP; /* end of list */ + else + return (pc+1)+offset; /* turn offset into absolute position */ +} + + +static Instruction *getjumpcontrol (FuncState *fs, int pc) { + Instruction *pi = &fs->f->code[pc]; + if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) + return pi-1; + else + return pi; +} + + +/* +** check whether list has any jump that do not produce a value +** (or produce an inverted value) +*/ +static int need_value (FuncState *fs, int list) { + for (; list != NO_JUMP; list = getjump(fs, list)) { + Instruction i = *getjumpcontrol(fs, list); + if (GET_OPCODE(i) != OP_TESTSET) return 1; + } + return 0; /* not found */ +} + + +static int patchtestreg (FuncState *fs, int node, int reg) { + Instruction *i = getjumpcontrol(fs, node); + if (GET_OPCODE(*i) != OP_TESTSET) + return 0; /* cannot patch other instructions */ + if (reg != NO_REG && reg != GETARG_B(*i)) + SETARG_A(*i, reg); + else /* no register to put value or register already has the value */ + *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); + + return 1; +} + + +static void removevalues (FuncState *fs, int list) { + for (; list != NO_JUMP; list = getjump(fs, list)) + patchtestreg(fs, list, NO_REG); +} + + +static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, + int dtarget) { + while (list != NO_JUMP) { + int next = getjump(fs, list); + if (patchtestreg(fs, list, reg)) + fixjump(fs, list, vtarget); + else + fixjump(fs, list, dtarget); /* jump to default target */ + list = next; + } +} + + +static void dischargejpc (FuncState *fs) { + patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); + fs->jpc = NO_JUMP; +} + + +void luaK_patchlist (FuncState *fs, int list, int target) { + if (target == fs->pc) + luaK_patchtohere(fs, list); + else { + lua_assert(target < fs->pc); + patchlistaux(fs, list, target, NO_REG, target); + } +} + + +void luaK_patchtohere (FuncState *fs, int list) { + luaK_getlabel(fs); + luaK_concat(fs, &fs->jpc, list); +} + + +void luaK_concat (FuncState *fs, int *l1, int l2) { + if (l2 == NO_JUMP) return; + else if (*l1 == NO_JUMP) + *l1 = l2; + else { + int list = *l1; + int next; + while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ + list = next; + fixjump(fs, list, l2); + } +} + + +void luaK_checkstack (FuncState *fs, int n) { + int newstack = fs->freereg + n; + if (newstack > fs->f->maxstacksize) { + if (newstack >= MAXSTACK) + luaX_syntaxerror(fs->ls, "function or expression too complex"); + fs->f->maxstacksize = cast_byte(newstack); + } +} + + +void luaK_reserveregs (FuncState *fs, int n) { + luaK_checkstack(fs, n); + fs->freereg += n; +} + + +static void freereg (FuncState *fs, int reg) { + if (!ISK(reg) && reg >= fs->nactvar) { + fs->freereg--; + lua_assert(reg == fs->freereg); + } +} + + +static void freeexp (FuncState *fs, expdesc *e) { + if (e->k == VNONRELOC) + freereg(fs, e->u.s.info); +} + + +static int addk (FuncState *fs, TValue *k, TValue *v) { + lua_State *L = fs->L; + TValue *idx = luaH_set(L, fs->h, k); + Proto *f = fs->f; + int oldsize = f->sizek; + if (ttisnumber(idx)) { + lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v)); + return cast_int(nvalue(idx)); + } + else { /* constant not found; create a new entry */ + setnvalue(idx, cast_num(fs->nk)); + luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, + MAXARG_Bx, "constant table overflow"); + while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); + setobj(L, &f->k[fs->nk], v); + luaC_barrier(L, f, v); + return fs->nk++; + } +} + + +int luaK_stringK (FuncState *fs, TString *s) { + TValue o; + setsvalue(fs->L, &o, s); + return addk(fs, &o, &o); +} + + +int luaK_numberK (FuncState *fs, lua_Number r) { + TValue o; + setnvalue(&o, r); + return addk(fs, &o, &o); +} + + +static int boolK (FuncState *fs, int b) { + TValue o; + setbvalue(&o, b); + return addk(fs, &o, &o); +} + + +static int nilK (FuncState *fs) { + TValue k, v; + setnilvalue(&v); + /* cannot use nil as key; instead use table itself to represent nil */ + sethvalue(fs->L, &k, fs->h); + return addk(fs, &k, &v); +} + + +void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { + if (e->k == VCALL) { /* expression is an open function call? */ + SETARG_C(getcode(fs, e), nresults+1); + } + else if (e->k == VVARARG) { + SETARG_B(getcode(fs, e), nresults+1); + SETARG_A(getcode(fs, e), fs->freereg); + luaK_reserveregs(fs, 1); + } +} + + +void luaK_setoneret (FuncState *fs, expdesc *e) { + if (e->k == VCALL) { /* expression is an open function call? */ + e->k = VNONRELOC; + e->u.s.info = GETARG_A(getcode(fs, e)); + } + else if (e->k == VVARARG) { + SETARG_B(getcode(fs, e), 2); + e->k = VRELOCABLE; /* can relocate its simple result */ + } +} + + +void luaK_dischargevars (FuncState *fs, expdesc *e) { + switch (e->k) { + case VLOCAL: { + e->k = VNONRELOC; + break; + } + case VUPVAL: { + e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0); + e->k = VRELOCABLE; + break; + } + case VGLOBAL: { + e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info); + e->k = VRELOCABLE; + break; + } + case VINDEXED: { + freereg(fs, e->u.s.aux); + freereg(fs, e->u.s.info); + e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux); + e->k = VRELOCABLE; + break; + } + case VVARARG: + case VCALL: { + luaK_setoneret(fs, e); + break; + } + default: break; /* there is one value available (somewhere) */ + } +} + + +static int code_label (FuncState *fs, int A, int b, int jump) { + luaK_getlabel(fs); /* those instructions may be jump targets */ + return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); +} + + +static void discharge2reg (FuncState *fs, expdesc *e, int reg) { + luaK_dischargevars(fs, e); + switch (e->k) { + case VNIL: { + luaK_nil(fs, reg, 1); + break; + } + case VFALSE: case VTRUE: { + luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); + break; + } + case VK: { + luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info); + break; + } + case VKNUM: { + luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval)); + break; + } + case VRELOCABLE: { + Instruction *pc = &getcode(fs, e); + SETARG_A(*pc, reg); + break; + } + case VNONRELOC: { + if (reg != e->u.s.info) + luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0); + break; + } + default: { + lua_assert(e->k == VVOID || e->k == VJMP); + return; /* nothing to do... */ + } + } + e->u.s.info = reg; + e->k = VNONRELOC; +} + + +static void discharge2anyreg (FuncState *fs, expdesc *e) { + if (e->k != VNONRELOC) { + luaK_reserveregs(fs, 1); + discharge2reg(fs, e, fs->freereg-1); + } +} + + +static void exp2reg (FuncState *fs, expdesc *e, int reg) { + discharge2reg(fs, e, reg); + if (e->k == VJMP) + luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */ + if (hasjumps(e)) { + int final; /* position after whole expression */ + int p_f = NO_JUMP; /* position of an eventual LOAD false */ + int p_t = NO_JUMP; /* position of an eventual LOAD true */ + if (need_value(fs, e->t) || need_value(fs, e->f)) { + int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); + p_f = code_label(fs, reg, 0, 1); + p_t = code_label(fs, reg, 1, 0); + luaK_patchtohere(fs, fj); + } + final = luaK_getlabel(fs); + patchlistaux(fs, e->f, final, reg, p_f); + patchlistaux(fs, e->t, final, reg, p_t); + } + e->f = e->t = NO_JUMP; + e->u.s.info = reg; + e->k = VNONRELOC; +} + + +void luaK_exp2nextreg (FuncState *fs, expdesc *e) { + luaK_dischargevars(fs, e); + freeexp(fs, e); + luaK_reserveregs(fs, 1); + exp2reg(fs, e, fs->freereg - 1); +} + + +int luaK_exp2anyreg (FuncState *fs, expdesc *e) { + luaK_dischargevars(fs, e); + if (e->k == VNONRELOC) { + if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */ + if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */ + exp2reg(fs, e, e->u.s.info); /* put value on it */ + return e->u.s.info; + } + } + luaK_exp2nextreg(fs, e); /* default */ + return e->u.s.info; +} + + +void luaK_exp2val (FuncState *fs, expdesc *e) { + if (hasjumps(e)) + luaK_exp2anyreg(fs, e); + else + luaK_dischargevars(fs, e); +} + + +int luaK_exp2RK (FuncState *fs, expdesc *e) { + luaK_exp2val(fs, e); + switch (e->k) { + case VKNUM: + case VTRUE: + case VFALSE: + case VNIL: { + if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */ + e->u.s.info = (e->k == VNIL) ? nilK(fs) : + (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) : + boolK(fs, (e->k == VTRUE)); + e->k = VK; + return RKASK(e->u.s.info); + } + else break; + } + case VK: { + if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */ + return RKASK(e->u.s.info); + else break; + } + default: break; + } + /* not a constant in the right range: put it in a register */ + return luaK_exp2anyreg(fs, e); +} + + +void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { + switch (var->k) { + case VLOCAL: { + freeexp(fs, ex); + exp2reg(fs, ex, var->u.s.info); + return; + } + case VUPVAL: { + int e = luaK_exp2anyreg(fs, ex); + luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0); + break; + } + case VGLOBAL: { + int e = luaK_exp2anyreg(fs, ex); + luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info); + break; + } + case VINDEXED: { + int e = luaK_exp2RK(fs, ex); + luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e); + break; + } + default: { + lua_assert(0); /* invalid var kind to store */ + break; + } + } + freeexp(fs, ex); +} + + +void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { + int func; + luaK_exp2anyreg(fs, e); + freeexp(fs, e); + func = fs->freereg; + luaK_reserveregs(fs, 2); + luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key)); + freeexp(fs, key); + e->u.s.info = func; + e->k = VNONRELOC; +} + + +static void invertjump (FuncState *fs, expdesc *e) { + Instruction *pc = getjumpcontrol(fs, e->u.s.info); + lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && + GET_OPCODE(*pc) != OP_TEST); + SETARG_A(*pc, !(GETARG_A(*pc))); +} + + +static int jumponcond (FuncState *fs, expdesc *e, int cond) { + if (e->k == VRELOCABLE) { + Instruction ie = getcode(fs, e); + if (GET_OPCODE(ie) == OP_NOT) { + fs->pc--; /* remove previous OP_NOT */ + return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); + } + /* else go through */ + } + discharge2anyreg(fs, e); + freeexp(fs, e); + return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond); +} + + +void luaK_goiftrue (FuncState *fs, expdesc *e) { + int pc; /* pc of last jump */ + luaK_dischargevars(fs, e); + switch (e->k) { + case VK: case VKNUM: case VTRUE: { + pc = NO_JUMP; /* always true; do nothing */ + break; + } + case VFALSE: { + pc = luaK_jump(fs); /* always jump */ + break; + } + case VJMP: { + invertjump(fs, e); + pc = e->u.s.info; + break; + } + default: { + pc = jumponcond(fs, e, 0); + break; + } + } + luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ + luaK_patchtohere(fs, e->t); + e->t = NO_JUMP; +} + + +static void luaK_goiffalse (FuncState *fs, expdesc *e) { + int pc; /* pc of last jump */ + luaK_dischargevars(fs, e); + switch (e->k) { + case VNIL: case VFALSE: { + pc = NO_JUMP; /* always false; do nothing */ + break; + } + case VTRUE: { + pc = luaK_jump(fs); /* always jump */ + break; + } + case VJMP: { + pc = e->u.s.info; + break; + } + default: { + pc = jumponcond(fs, e, 1); + break; + } + } + luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ + luaK_patchtohere(fs, e->f); + e->f = NO_JUMP; +} + + +static void codenot (FuncState *fs, expdesc *e) { + luaK_dischargevars(fs, e); + switch (e->k) { + case VNIL: case VFALSE: { + e->k = VTRUE; + break; + } + case VK: case VKNUM: case VTRUE: { + e->k = VFALSE; + break; + } + case VJMP: { + invertjump(fs, e); + break; + } + case VRELOCABLE: + case VNONRELOC: { + discharge2anyreg(fs, e); + freeexp(fs, e); + e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0); + e->k = VRELOCABLE; + break; + } + default: { + lua_assert(0); /* cannot happen */ + break; + } + } + /* interchange true and false lists */ + { int temp = e->f; e->f = e->t; e->t = temp; } + removevalues(fs, e->f); + removevalues(fs, e->t); +} + + +void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { + t->u.s.aux = luaK_exp2RK(fs, k); + t->k = VINDEXED; +} + + +static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { + lua_Number v1, v2, r; + if (!isnumeral(e1) || !isnumeral(e2)) return 0; + v1 = e1->u.nval; + v2 = e2->u.nval; + switch (op) { + case OP_ADD: r = luai_numadd(v1, v2); break; + case OP_SUB: r = luai_numsub(v1, v2); break; + case OP_MUL: r = luai_nummul(v1, v2); break; + case OP_DIV: + if (v2 == 0) return 0; /* do not attempt to divide by 0 */ + r = luai_numdiv(v1, v2); break; + case OP_MOD: + if (v2 == 0) return 0; /* do not attempt to divide by 0 */ + r = luai_nummod(v1, v2); break; + case OP_POW: r = luai_numpow(v1, v2); break; + case OP_UNM: r = luai_numunm(v1); break; + case OP_LEN: return 0; /* no constant folding for 'len' */ + default: lua_assert(0); r = 0; break; + } + if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ + e1->u.nval = r; + return 1; +} + + +static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { + if (constfolding(op, e1, e2)) + return; + else { + int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; + int o1 = luaK_exp2RK(fs, e1); + if (o1 > o2) { + freeexp(fs, e1); + freeexp(fs, e2); + } + else { + freeexp(fs, e2); + freeexp(fs, e1); + } + e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); + e1->k = VRELOCABLE; + } +} + + +static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, + expdesc *e2) { + int o1 = luaK_exp2RK(fs, e1); + int o2 = luaK_exp2RK(fs, e2); + freeexp(fs, e2); + freeexp(fs, e1); + if (cond == 0 && op != OP_EQ) { + int temp; /* exchange args to replace by `<' or `<=' */ + temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ + cond = 1; + } + e1->u.s.info = condjump(fs, op, cond, o1, o2); + e1->k = VJMP; +} + + +void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { + expdesc e2; + e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; + switch (op) { + case OPR_MINUS: { + if (!isnumeral(e)) + luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */ + codearith(fs, OP_UNM, e, &e2); + break; + } + case OPR_NOT: codenot(fs, e); break; + case OPR_LEN: { + luaK_exp2anyreg(fs, e); /* cannot operate on constants */ + codearith(fs, OP_LEN, e, &e2); + break; + } + default: lua_assert(0); + } +} + + +void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { + switch (op) { + case OPR_AND: { + luaK_goiftrue(fs, v); + break; + } + case OPR_OR: { + luaK_goiffalse(fs, v); + break; + } + case OPR_CONCAT: { + luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ + break; + } + case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: + case OPR_MOD: case OPR_POW: { + if (!isnumeral(v)) luaK_exp2RK(fs, v); + break; + } + default: { + luaK_exp2RK(fs, v); + break; + } + } +} + + +void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { + switch (op) { + case OPR_AND: { + lua_assert(e1->t == NO_JUMP); /* list must be closed */ + luaK_dischargevars(fs, e2); + luaK_concat(fs, &e2->f, e1->f); + *e1 = *e2; + break; + } + case OPR_OR: { + lua_assert(e1->f == NO_JUMP); /* list must be closed */ + luaK_dischargevars(fs, e2); + luaK_concat(fs, &e2->t, e1->t); + *e1 = *e2; + break; + } + case OPR_CONCAT: { + luaK_exp2val(fs, e2); + if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { + lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1); + freeexp(fs, e1); + SETARG_B(getcode(fs, e2), e1->u.s.info); + e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info; + } + else { + luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ + codearith(fs, OP_CONCAT, e1, e2); + } + break; + } + case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break; + case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break; + case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break; + case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break; + case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break; + case OPR_POW: codearith(fs, OP_POW, e1, e2); break; + case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break; + case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break; + case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break; + case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break; + case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break; + case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break; + default: lua_assert(0); + } +} + + +void luaK_fixline (FuncState *fs, int line) { + fs->f->lineinfo[fs->pc - 1] = line; +} + + +static int luaK_code (FuncState *fs, Instruction i, int line) { + Proto *f = fs->f; + dischargejpc(fs); /* `pc' will change */ + /* put new instruction in code array */ + luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction, + MAX_INT, "code size overflow"); + f->code[fs->pc] = i; + /* save corresponding line information */ + luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int, + MAX_INT, "code size overflow"); + f->lineinfo[fs->pc] = line; + return fs->pc++; +} + + +int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { + lua_assert(getOpMode(o) == iABC); + lua_assert(getBMode(o) != OpArgN || b == 0); + lua_assert(getCMode(o) != OpArgN || c == 0); + return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); +} + + +int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { + lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); + lua_assert(getCMode(o) == OpArgN); + return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); +} + + +void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { + int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; + int b = (tostore == LUA_MULTRET) ? 0 : tostore; + lua_assert(tostore != 0); + if (c <= MAXARG_C) + luaK_codeABC(fs, OP_SETLIST, base, b, c); + else { + luaK_codeABC(fs, OP_SETLIST, base, b, 0); + luaK_code(fs, cast(Instruction, c), fs->ls->lastline); + } + fs->freereg = base + 1; /* free registers with list values */ +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lcode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lcode.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,76 @@ +/* +** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ +** Code generator for Lua +** See Copyright Notice in lua.h +*/ + +#ifndef lcode_h +#define lcode_h + +#include "llex.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lparser.h" + + +/* +** Marks the end of a patch list. It is an invalid value both as an absolute +** address, and as a list link (would link an element to itself). +*/ +#define NO_JUMP (-1) + + +/* +** grep "ORDER OPR" if you change these enums +*/ +typedef enum BinOpr { + OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, + OPR_CONCAT, + OPR_NE, OPR_EQ, + OPR_LT, OPR_LE, OPR_GT, OPR_GE, + OPR_AND, OPR_OR, + OPR_NOBINOPR +} BinOpr; + + +typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; + + +#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) + +#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) + +#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) + +LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); +LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); +LUAI_FUNC void luaK_fixline (FuncState *fs, int line); +LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); +LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); +LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); +LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); +LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); +LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); +LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); +LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); +LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); +LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); +LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); +LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); +LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); +LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); +LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); +LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); +LUAI_FUNC int luaK_jump (FuncState *fs); +LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); +LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); +LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); +LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); +LUAI_FUNC int luaK_getlabel (FuncState *fs); +LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); +LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); +LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); +LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ldblib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ldblib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,397 @@ +/* +** $Id: ldblib.c,v 1.104.1.3 2008/01/21 13:11:21 roberto Exp $ +** Interface from Lua to its debug API +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#define ldblib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + + +static int db_getregistry (lua_State *L) { + lua_pushvalue(L, LUA_REGISTRYINDEX); + return 1; +} + + +static int db_getmetatable (lua_State *L) { + luaL_checkany(L, 1); + if (!lua_getmetatable(L, 1)) { + lua_pushnil(L); /* no metatable */ + } + return 1; +} + + +static int db_setmetatable (lua_State *L) { + int t = lua_type(L, 2); + luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, + "nil or table expected"); + lua_settop(L, 2); + lua_pushboolean(L, lua_setmetatable(L, 1)); + return 1; +} + + +static int db_getfenv (lua_State *L) { + lua_getfenv(L, 1); + return 1; +} + + +static int db_setfenv (lua_State *L) { + luaL_checktype(L, 2, LUA_TTABLE); + lua_settop(L, 2); + if (lua_setfenv(L, 1) == 0) + luaL_error(L, LUA_QL("setfenv") + " cannot change environment of given object"); + return 1; +} + + +static void settabss (lua_State *L, const char *i, const char *v) { + lua_pushstring(L, v); + lua_setfield(L, -2, i); +} + + +static void settabsi (lua_State *L, const char *i, int v) { + lua_pushinteger(L, v); + lua_setfield(L, -2, i); +} + + +static lua_State *getthread (lua_State *L, int *arg) { + if (lua_isthread(L, 1)) { + *arg = 1; + return lua_tothread(L, 1); + } + else { + *arg = 0; + return L; + } +} + + +static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { + if (L == L1) { + lua_pushvalue(L, -2); + lua_remove(L, -3); + } + else + lua_xmove(L1, L, 1); + lua_setfield(L, -2, fname); +} + + +static int db_getinfo (lua_State *L) { + lua_Debug ar; + int arg; + lua_State *L1 = getthread(L, &arg); + const char *options = luaL_optstring(L, arg+2, "flnSu"); + if (lua_isnumber(L, arg+1)) { + if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { + lua_pushnil(L); /* level out of range */ + return 1; + } + } + else if (lua_isfunction(L, arg+1)) { + lua_pushfstring(L, ">%s", options); + options = lua_tostring(L, -1); + lua_pushvalue(L, arg+1); + lua_xmove(L, L1, 1); + } + else + return luaL_argerror(L, arg+1, "function or level expected"); + if (!lua_getinfo(L1, options, &ar)) + return luaL_argerror(L, arg+2, "invalid option"); + lua_createtable(L, 0, 2); + if (strchr(options, 'S')) { + settabss(L, "source", ar.source); + settabss(L, "short_src", ar.short_src); + settabsi(L, "linedefined", ar.linedefined); + settabsi(L, "lastlinedefined", ar.lastlinedefined); + settabss(L, "what", ar.what); + } + if (strchr(options, 'l')) + settabsi(L, "currentline", ar.currentline); + if (strchr(options, 'u')) + settabsi(L, "nups", ar.nups); + if (strchr(options, 'n')) { + settabss(L, "name", ar.name); + settabss(L, "namewhat", ar.namewhat); + } + if (strchr(options, 'L')) + treatstackoption(L, L1, "activelines"); + if (strchr(options, 'f')) + treatstackoption(L, L1, "func"); + return 1; /* return table */ +} + + +static int db_getlocal (lua_State *L) { + int arg; + lua_State *L1 = getthread(L, &arg); + lua_Debug ar; + const char *name; + if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ + return luaL_argerror(L, arg+1, "level out of range"); + name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); + if (name) { + lua_xmove(L1, L, 1); + lua_pushstring(L, name); + lua_pushvalue(L, -2); + return 2; + } + else { + lua_pushnil(L); + return 1; + } +} + + +static int db_setlocal (lua_State *L) { + int arg; + lua_State *L1 = getthread(L, &arg); + lua_Debug ar; + if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ + return luaL_argerror(L, arg+1, "level out of range"); + luaL_checkany(L, arg+3); + lua_settop(L, arg+3); + lua_xmove(L, L1, 1); + lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); + return 1; +} + + +static int auxupvalue (lua_State *L, int get) { + const char *name; + int n = luaL_checkint(L, 2); + luaL_checktype(L, 1, LUA_TFUNCTION); + if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */ + name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); + if (name == NULL) return 0; + lua_pushstring(L, name); + lua_insert(L, -(get+1)); + return get + 1; +} + + +static int db_getupvalue (lua_State *L) { + return auxupvalue(L, 1); +} + + +static int db_setupvalue (lua_State *L) { + luaL_checkany(L, 3); + return auxupvalue(L, 0); +} + + + +static const char KEY_HOOK = 'h'; + + +static void hookf (lua_State *L, lua_Debug *ar) { + static const char *const hooknames[] = + {"call", "return", "line", "count", "tail return"}; + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_rawget(L, LUA_REGISTRYINDEX); + lua_pushlightuserdata(L, L); + lua_rawget(L, -2); + if (lua_isfunction(L, -1)) { + lua_pushstring(L, hooknames[(int)ar->event]); + if (ar->currentline >= 0) + lua_pushinteger(L, ar->currentline); + else lua_pushnil(L); + lua_assert(lua_getinfo(L, "lS", ar)); + lua_call(L, 2, 0); + } +} + + +static int makemask (const char *smask, int count) { + int mask = 0; + if (strchr(smask, 'c')) mask |= LUA_MASKCALL; + if (strchr(smask, 'r')) mask |= LUA_MASKRET; + if (strchr(smask, 'l')) mask |= LUA_MASKLINE; + if (count > 0) mask |= LUA_MASKCOUNT; + return mask; +} + + +static char *unmakemask (int mask, char *smask) { + int i = 0; + if (mask & LUA_MASKCALL) smask[i++] = 'c'; + if (mask & LUA_MASKRET) smask[i++] = 'r'; + if (mask & LUA_MASKLINE) smask[i++] = 'l'; + smask[i] = '\0'; + return smask; +} + + +static void gethooktable (lua_State *L) { + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_rawget(L, LUA_REGISTRYINDEX); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + lua_createtable(L, 0, 1); + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_pushvalue(L, -2); + lua_rawset(L, LUA_REGISTRYINDEX); + } +} + + +static int db_sethook (lua_State *L) { + int arg, mask, count; + lua_Hook func; + lua_State *L1 = getthread(L, &arg); + if (lua_isnoneornil(L, arg+1)) { + lua_settop(L, arg+1); + func = NULL; mask = 0; count = 0; /* turn off hooks */ + } + else { + const char *smask = luaL_checkstring(L, arg+2); + luaL_checktype(L, arg+1, LUA_TFUNCTION); + count = luaL_optint(L, arg+3, 0); + func = hookf; mask = makemask(smask, count); + } + gethooktable(L); + lua_pushlightuserdata(L, L1); + lua_pushvalue(L, arg+1); + lua_rawset(L, -3); /* set new hook */ + lua_pop(L, 1); /* remove hook table */ + lua_sethook(L1, func, mask, count); /* set hooks */ + return 0; +} + + +static int db_gethook (lua_State *L) { + int arg; + lua_State *L1 = getthread(L, &arg); + char buff[5]; + int mask = lua_gethookmask(L1); + lua_Hook hook = lua_gethook(L1); + if (hook != NULL && hook != hookf) /* external hook? */ + lua_pushliteral(L, "external hook"); + else { + gethooktable(L); + lua_pushlightuserdata(L, L1); + lua_rawget(L, -2); /* get hook */ + lua_remove(L, -2); /* remove hook table */ + } + lua_pushstring(L, unmakemask(mask, buff)); + lua_pushinteger(L, lua_gethookcount(L1)); + return 3; +} + + +static int db_debug (lua_State *L) { + for (;;) { + char buffer[250]; + fputs("lua_debug> ", stderr); + if (fgets(buffer, sizeof(buffer), stdin) == 0 || + strcmp(buffer, "cont\n") == 0) + return 0; + if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || + lua_pcall(L, 0, 0, 0)) { + fputs(lua_tostring(L, -1), stderr); + fputs("\n", stderr); + } + lua_settop(L, 0); /* remove eventual returns */ + } +} + + +#define LEVELS1 12 /* size of the first part of the stack */ +#define LEVELS2 10 /* size of the second part of the stack */ + +static int db_errorfb (lua_State *L) { + int level; + int firstpart = 1; /* still before eventual `...' */ + int arg; + lua_State *L1 = getthread(L, &arg); + lua_Debug ar; + if (lua_isnumber(L, arg+2)) { + level = (int)lua_tointeger(L, arg+2); + lua_pop(L, 1); + } + else + level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ + if (lua_gettop(L) == arg) + lua_pushliteral(L, ""); + else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ + else lua_pushliteral(L, "\n"); + lua_pushliteral(L, "stack traceback:"); + while (lua_getstack(L1, level++, &ar)) { + if (level > LEVELS1 && firstpart) { + /* no more than `LEVELS2' more levels? */ + if (!lua_getstack(L1, level+LEVELS2, &ar)) + level--; /* keep going */ + else { + lua_pushliteral(L, "\n\t..."); /* too many levels */ + while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */ + level++; + } + firstpart = 0; + continue; + } + lua_pushliteral(L, "\n\t"); + lua_getinfo(L1, "Snl", &ar); + lua_pushfstring(L, "%s:", ar.short_src); + if (ar.currentline > 0) + lua_pushfstring(L, "%d:", ar.currentline); + if (*ar.namewhat != '\0') /* is there a name? */ + lua_pushfstring(L, " in function " LUA_QS, ar.name); + else { + if (*ar.what == 'm') /* main? */ + lua_pushfstring(L, " in main chunk"); + else if (*ar.what == 'C' || *ar.what == 't') + lua_pushliteral(L, " ?"); /* C function or tail call */ + else + lua_pushfstring(L, " in function <%s:%d>", + ar.short_src, ar.linedefined); + } + lua_concat(L, lua_gettop(L) - arg); + } + lua_concat(L, lua_gettop(L) - arg); + return 1; +} + + +static const luaL_Reg dblib[] = { + {"debug", db_debug}, + {"getfenv", db_getfenv}, + {"gethook", db_gethook}, + {"getinfo", db_getinfo}, + {"getlocal", db_getlocal}, + {"getregistry", db_getregistry}, + {"getmetatable", db_getmetatable}, + {"getupvalue", db_getupvalue}, + {"setfenv", db_setfenv}, + {"sethook", db_sethook}, + {"setlocal", db_setlocal}, + {"setmetatable", db_setmetatable}, + {"setupvalue", db_setupvalue}, + {"traceback", db_errorfb}, + {NULL, NULL} +}; + + +LUALIB_API int luaopen_debug (lua_State *L) { + luaL_register(L, LUA_DBLIBNAME, dblib); + return 1; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ldebug.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ldebug.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,638 @@ +/* +** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $ +** Debug Interface +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + + +#define ldebug_c +#define LUA_CORE + +#include "lua.h" + +#include "lapi.h" +#include "lcode.h" +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" +#include "lvm.h" + + + +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); + + +static int currentpc (lua_State *L, CallInfo *ci) { + if (!isLua(ci)) return -1; /* function is not a Lua function? */ + if (ci == L->ci) + ci->savedpc = L->savedpc; + return pcRel(ci->savedpc, ci_func(ci)->l.p); +} + + +static int currentline (lua_State *L, CallInfo *ci) { + int pc = currentpc(L, ci); + if (pc < 0) + return -1; /* only active lua functions have current-line information */ + else + return getline(ci_func(ci)->l.p, pc); +} + + +/* +** this function can be called asynchronous (e.g. during a signal) +*/ +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { + if (func == NULL || mask == 0) { /* turn off hooks? */ + mask = 0; + func = NULL; + } + L->hook = func; + L->basehookcount = count; + resethookcount(L); + L->hookmask = cast_byte(mask); + return 1; +} + + +LUA_API lua_Hook lua_gethook (lua_State *L) { + return L->hook; +} + + +LUA_API int lua_gethookmask (lua_State *L) { + return L->hookmask; +} + + +LUA_API int lua_gethookcount (lua_State *L) { + return L->basehookcount; +} + + +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { + int status; + CallInfo *ci; + lua_lock(L); + for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) { + level--; + if (f_isLua(ci)) /* Lua function? */ + level -= ci->tailcalls; /* skip lost tail calls */ + } + if (level == 0 && ci > L->base_ci) { /* level found? */ + status = 1; + ar->i_ci = cast_int(ci - L->base_ci); + } + else if (level < 0) { /* level is of a lost tail call? */ + status = 1; + ar->i_ci = 0; + } + else status = 0; /* no such level */ + lua_unlock(L); + return status; +} + + +static Proto *getluaproto (CallInfo *ci) { + return (isLua(ci) ? ci_func(ci)->l.p : NULL); +} + + +static const char *findlocal (lua_State *L, CallInfo *ci, int n) { + const char *name; + Proto *fp = getluaproto(ci); + if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL) + return name; /* is a local variable in a Lua function */ + else { + StkId limit = (ci == L->ci) ? L->top : (ci+1)->func; + if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ + return "(*temporary)"; + else + return NULL; + } +} + + +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { + CallInfo *ci = L->base_ci + ar->i_ci; + const char *name = findlocal(L, ci, n); + lua_lock(L); + if (name) + luaA_pushobject(L, ci->base + (n - 1)); + lua_unlock(L); + return name; +} + + +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { + CallInfo *ci = L->base_ci + ar->i_ci; + const char *name = findlocal(L, ci, n); + lua_lock(L); + if (name) + setobjs2s(L, ci->base + (n - 1), L->top - 1); + L->top--; /* pop value */ + lua_unlock(L); + return name; +} + + +static void funcinfo (lua_Debug *ar, Closure *cl) { + if (cl->c.isC) { + ar->source = "=[C]"; + ar->linedefined = -1; + ar->lastlinedefined = -1; + ar->what = "C"; + } + else { + ar->source = getstr(cl->l.p->source); + ar->linedefined = cl->l.p->linedefined; + ar->lastlinedefined = cl->l.p->lastlinedefined; + ar->what = (ar->linedefined == 0) ? "main" : "Lua"; + } + luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); +} + + +static void info_tailcall (lua_Debug *ar) { + ar->name = ar->namewhat = ""; + ar->what = "tail"; + ar->lastlinedefined = ar->linedefined = ar->currentline = -1; + ar->source = "=(tail call)"; + luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); + ar->nups = 0; +} + + +static void collectvalidlines (lua_State *L, Closure *f) { + if (f == NULL || f->c.isC) { + setnilvalue(L->top); + } + else { + Table *t = luaH_new(L, 0, 0); + int *lineinfo = f->l.p->lineinfo; + int i; + for (i=0; il.p->sizelineinfo; i++) + setbvalue(luaH_setnum(L, t, lineinfo[i]), 1); + sethvalue(L, L->top, t); + } + incr_top(L); +} + + +static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, + Closure *f, CallInfo *ci) { + int status = 1; + if (f == NULL) { + info_tailcall(ar); + return status; + } + for (; *what; what++) { + switch (*what) { + case 'S': { + funcinfo(ar, f); + break; + } + case 'l': { + ar->currentline = (ci) ? currentline(L, ci) : -1; + break; + } + case 'u': { + ar->nups = f->c.nupvalues; + break; + } + case 'n': { + ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL; + if (ar->namewhat == NULL) { + ar->namewhat = ""; /* not found */ + ar->name = NULL; + } + break; + } + case 'L': + case 'f': /* handled by lua_getinfo */ + break; + default: status = 0; /* invalid option */ + } + } + return status; +} + + +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { + int status; + Closure *f = NULL; + CallInfo *ci = NULL; + lua_lock(L); + if (*what == '>') { + StkId func = L->top - 1; + luai_apicheck(L, ttisfunction(func)); + what++; /* skip the '>' */ + f = clvalue(func); + L->top--; /* pop function */ + } + else if (ar->i_ci != 0) { /* no tail call? */ + ci = L->base_ci + ar->i_ci; + lua_assert(ttisfunction(ci->func)); + f = clvalue(ci->func); + } + status = auxgetinfo(L, what, ar, f, ci); + if (strchr(what, 'f')) { + if (f == NULL) setnilvalue(L->top); + else setclvalue(L, L->top, f); + incr_top(L); + } + if (strchr(what, 'L')) + collectvalidlines(L, f); + lua_unlock(L); + return status; +} + + +/* +** {====================================================== +** Symbolic Execution and code checker +** ======================================================= +*/ + +#define check(x) if (!(x)) return 0; + +#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode) + +#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize) + + + +static int precheck (const Proto *pt) { + check(pt->maxstacksize <= MAXSTACK); + check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); + check(!(pt->is_vararg & VARARG_NEEDSARG) || + (pt->is_vararg & VARARG_HASARG)); + check(pt->sizeupvalues <= pt->nups); + check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); + check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); + return 1; +} + + +#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1]) + +int luaG_checkopenop (Instruction i) { + switch (GET_OPCODE(i)) { + case OP_CALL: + case OP_TAILCALL: + case OP_RETURN: + case OP_SETLIST: { + check(GETARG_B(i) == 0); + return 1; + } + default: return 0; /* invalid instruction after an open call */ + } +} + + +static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) { + switch (mode) { + case OpArgN: check(r == 0); break; + case OpArgU: break; + case OpArgR: checkreg(pt, r); break; + case OpArgK: + check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize); + break; + } + return 1; +} + + +static Instruction symbexec (const Proto *pt, int lastpc, int reg) { + int pc; + int last; /* stores position of last instruction that changed `reg' */ + last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */ + check(precheck(pt)); + for (pc = 0; pc < lastpc; pc++) { + Instruction i = pt->code[pc]; + OpCode op = GET_OPCODE(i); + int a = GETARG_A(i); + int b = 0; + int c = 0; + check(op < NUM_OPCODES); + checkreg(pt, a); + switch (getOpMode(op)) { + case iABC: { + b = GETARG_B(i); + c = GETARG_C(i); + check(checkArgMode(pt, b, getBMode(op))); + check(checkArgMode(pt, c, getCMode(op))); + break; + } + case iABx: { + b = GETARG_Bx(i); + if (getBMode(op) == OpArgK) check(b < pt->sizek); + break; + } + case iAsBx: { + b = GETARG_sBx(i); + if (getBMode(op) == OpArgR) { + int dest = pc+1+b; + check(0 <= dest && dest < pt->sizecode); + if (dest > 0) { + int j; + /* check that it does not jump to a setlist count; this + is tricky, because the count from a previous setlist may + have the same value of an invalid setlist; so, we must + go all the way back to the first of them (if any) */ + for (j = 0; j < dest; j++) { + Instruction d = pt->code[dest-1-j]; + if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; + } + /* if 'j' is even, previous value is not a setlist (even if + it looks like one) */ + check((j&1) == 0); + } + } + break; + } + } + if (testAMode(op)) { + if (a == reg) last = pc; /* change register `a' */ + } + if (testTMode(op)) { + check(pc+2 < pt->sizecode); /* check skip */ + check(GET_OPCODE(pt->code[pc+1]) == OP_JMP); + } + switch (op) { + case OP_LOADBOOL: { + if (c == 1) { /* does it jump? */ + check(pc+2 < pt->sizecode); /* check its jump */ + check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || + GETARG_C(pt->code[pc+1]) != 0); + } + break; + } + case OP_LOADNIL: { + if (a <= reg && reg <= b) + last = pc; /* set registers from `a' to `b' */ + break; + } + case OP_GETUPVAL: + case OP_SETUPVAL: { + check(b < pt->nups); + break; + } + case OP_GETGLOBAL: + case OP_SETGLOBAL: { + check(ttisstring(&pt->k[b])); + break; + } + case OP_SELF: { + checkreg(pt, a+1); + if (reg == a+1) last = pc; + break; + } + case OP_CONCAT: { + check(b < c); /* at least two operands */ + break; + } + case OP_TFORLOOP: { + check(c >= 1); /* at least one result (control variable) */ + checkreg(pt, a+2+c); /* space for results */ + if (reg >= a+2) last = pc; /* affect all regs above its base */ + break; + } + case OP_FORLOOP: + case OP_FORPREP: + checkreg(pt, a+3); + /* go through */ + case OP_JMP: { + int dest = pc+1+b; + /* not full check and jump is forward and do not skip `lastpc'? */ + if (reg != NO_REG && pc < dest && dest <= lastpc) + pc += b; /* do the jump */ + break; + } + case OP_CALL: + case OP_TAILCALL: { + if (b != 0) { + checkreg(pt, a+b-1); + } + c--; /* c = num. returns */ + if (c == LUA_MULTRET) { + check(checkopenop(pt, pc)); + } + else if (c != 0) + checkreg(pt, a+c-1); + if (reg >= a) last = pc; /* affect all registers above base */ + break; + } + case OP_RETURN: { + b--; /* b = num. returns */ + if (b > 0) checkreg(pt, a+b-1); + break; + } + case OP_SETLIST: { + if (b > 0) checkreg(pt, a + b); + if (c == 0) { + pc++; + check(pc < pt->sizecode - 1); + } + break; + } + case OP_CLOSURE: { + int nup, j; + check(b < pt->sizep); + nup = pt->p[b]->nups; + check(pc + nup < pt->sizecode); + for (j = 1; j <= nup; j++) { + OpCode op1 = GET_OPCODE(pt->code[pc + j]); + check(op1 == OP_GETUPVAL || op1 == OP_MOVE); + } + if (reg != NO_REG) /* tracing? */ + pc += nup; /* do not 'execute' these pseudo-instructions */ + break; + } + case OP_VARARG: { + check((pt->is_vararg & VARARG_ISVARARG) && + !(pt->is_vararg & VARARG_NEEDSARG)); + b--; + if (b == LUA_MULTRET) check(checkopenop(pt, pc)); + checkreg(pt, a+b-1); + break; + } + default: break; + } + } + return pt->code[last]; +} + +#undef check +#undef checkjump +#undef checkreg + +/* }====================================================== */ + + +int luaG_checkcode (const Proto *pt) { + return (symbexec(pt, pt->sizecode, NO_REG) != 0); +} + + +static const char *kname (Proto *p, int c) { + if (ISK(c) && ttisstring(&p->k[INDEXK(c)])) + return svalue(&p->k[INDEXK(c)]); + else + return "?"; +} + + +static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos, + const char **name) { + if (isLua(ci)) { /* a Lua function? */ + Proto *p = ci_func(ci)->l.p; + int pc = currentpc(L, ci); + Instruction i; + *name = luaF_getlocalname(p, stackpos+1, pc); + if (*name) /* is a local? */ + return "local"; + i = symbexec(p, pc, stackpos); /* try symbolic execution */ + lua_assert(pc != -1); + switch (GET_OPCODE(i)) { + case OP_GETGLOBAL: { + int g = GETARG_Bx(i); /* global index */ + lua_assert(ttisstring(&p->k[g])); + *name = svalue(&p->k[g]); + return "global"; + } + case OP_MOVE: { + int a = GETARG_A(i); + int b = GETARG_B(i); /* move from `b' to `a' */ + if (b < a) + return getobjname(L, ci, b, name); /* get name for `b' */ + break; + } + case OP_GETTABLE: { + int k = GETARG_C(i); /* key index */ + *name = kname(p, k); + return "field"; + } + case OP_GETUPVAL: { + int u = GETARG_B(i); /* upvalue index */ + *name = p->upvalues ? getstr(p->upvalues[u]) : "?"; + return "upvalue"; + } + case OP_SELF: { + int k = GETARG_C(i); /* key index */ + *name = kname(p, k); + return "method"; + } + default: break; + } + } + return NULL; /* no useful name found */ +} + + +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { + Instruction i; + if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1)) + return NULL; /* calling function is not Lua (or is unknown) */ + ci--; /* calling function */ + i = ci_func(ci)->l.p->code[currentpc(L, ci)]; + if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL || + GET_OPCODE(i) == OP_TFORLOOP) + return getobjname(L, ci, GETARG_A(i), name); + else + return NULL; /* no useful name can be found */ +} + + +/* only ANSI way to check whether a pointer points to an array */ +static int isinstack (CallInfo *ci, const TValue *o) { + StkId p; + for (p = ci->base; p < ci->top; p++) + if (o == p) return 1; + return 0; +} + + +void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { + const char *name = NULL; + const char *t = luaT_typenames[ttype(o)]; + const char *kind = (isinstack(L->ci, o)) ? + getobjname(L, L->ci, cast_int(o - L->base), &name) : + NULL; + if (kind) + luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", + op, kind, name, t); + else + luaG_runerror(L, "attempt to %s a %s value", op, t); +} + + +void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { + if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; + lua_assert(!ttisstring(p1) && !ttisnumber(p1)); + luaG_typeerror(L, p1, "concatenate"); +} + + +void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { + TValue temp; + if (luaV_tonumber(p1, &temp) == NULL) + p2 = p1; /* first operand is wrong */ + luaG_typeerror(L, p2, "perform arithmetic on"); +} + + +int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { + const char *t1 = luaT_typenames[ttype(p1)]; + const char *t2 = luaT_typenames[ttype(p2)]; + if (t1[2] == t2[2]) + luaG_runerror(L, "attempt to compare two %s values", t1); + else + luaG_runerror(L, "attempt to compare %s with %s", t1, t2); + return 0; +} + + +static void addinfo (lua_State *L, const char *msg) { + CallInfo *ci = L->ci; + if (isLua(ci)) { /* is Lua code? */ + char buff[LUA_IDSIZE]; /* add file:line information */ + int line = currentline(L, ci); + luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE); + luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); + } +} + + +void luaG_errormsg (lua_State *L) { + if (L->errfunc != 0) { /* is there an error handling function? */ + StkId errfunc = restorestack(L, L->errfunc); + if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); + setobjs2s(L, L->top, L->top - 1); /* move argument */ + setobjs2s(L, L->top - 1, errfunc); /* push function */ + incr_top(L); + luaD_call(L, L->top - 2, 1); /* call it */ + } + luaD_throw(L, LUA_ERRRUN); +} + + +void luaG_runerror (lua_State *L, const char *fmt, ...) { + va_list argp; + va_start(argp, fmt); + addinfo(L, luaO_pushvfstring(L, fmt, argp)); + va_end(argp); + luaG_errormsg(L); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ldebug.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ldebug.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,33 @@ +/* +** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions from Debug Interface module +** See Copyright Notice in lua.h +*/ + +#ifndef ldebug_h +#define ldebug_h + + +#include "lstate.h" + + +#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) + +#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) + +#define resethookcount(L) (L->hookcount = L->basehookcount) + + +LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, + const char *opname); +LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); +LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, + const TValue *p2); +LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, + const TValue *p2); +LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); +LUAI_FUNC void luaG_errormsg (lua_State *L); +LUAI_FUNC int luaG_checkcode (const Proto *pt); +LUAI_FUNC int luaG_checkopenop (Instruction i); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ldo.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ldo.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,518 @@ +/* +** $Id: ldo.c,v 2.38.1.3 2008/01/18 22:31:22 roberto Exp $ +** Stack and Call structure of Lua +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#define ldo_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lparser.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" +#include "lundump.h" +#include "lvm.h" +#include "lzio.h" + + + + +/* +** {====================================================== +** Error-recovery functions +** ======================================================= +*/ + + +/* chain list of long jump buffers */ +struct lua_longjmp { + struct lua_longjmp *previous; + luai_jmpbuf b; + volatile int status; /* error code */ +}; + + +void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { + switch (errcode) { + case LUA_ERRMEM: { + setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG)); + break; + } + case LUA_ERRERR: { + setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); + break; + } + case LUA_ERRSYNTAX: + case LUA_ERRRUN: { + setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ + break; + } + } + L->top = oldtop + 1; +} + + +static void restore_stack_limit (lua_State *L) { + lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); + if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */ + int inuse = cast_int(L->ci - L->base_ci); + if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */ + luaD_reallocCI(L, LUAI_MAXCALLS); + } +} + + +static void resetstack (lua_State *L, int status) { + L->ci = L->base_ci; + L->base = L->ci->base; + luaF_close(L, L->base); /* close eventual pending closures */ + luaD_seterrorobj(L, status, L->base); + L->nCcalls = L->baseCcalls; + L->allowhook = 1; + restore_stack_limit(L); + L->errfunc = 0; + L->errorJmp = NULL; +} + + +void luaD_throw (lua_State *L, int errcode) { + if (L->errorJmp) { + L->errorJmp->status = errcode; + LUAI_THROW(L, L->errorJmp); + } + else { + L->status = cast_byte(errcode); + if (G(L)->panic) { + resetstack(L, errcode); + lua_unlock(L); + G(L)->panic(L); + } + exit(EXIT_FAILURE); + } +} + + +int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { + struct lua_longjmp lj; + lj.status = 0; + lj.previous = L->errorJmp; /* chain new error handler */ + L->errorJmp = &lj; + LUAI_TRY(L, &lj, + (*f)(L, ud); + ); + L->errorJmp = lj.previous; /* restore old error handler */ + return lj.status; +} + +/* }====================================================== */ + + +static void correctstack (lua_State *L, TValue *oldstack) { + CallInfo *ci; + GCObject *up; + L->top = (L->top - oldstack) + L->stack; + for (up = L->openupval; up != NULL; up = up->gch.next) + gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; + for (ci = L->base_ci; ci <= L->ci; ci++) { + ci->top = (ci->top - oldstack) + L->stack; + ci->base = (ci->base - oldstack) + L->stack; + ci->func = (ci->func - oldstack) + L->stack; + } + L->base = (L->base - oldstack) + L->stack; +} + + +void luaD_reallocstack (lua_State *L, int newsize) { + TValue *oldstack = L->stack; + int realsize = newsize + 1 + EXTRA_STACK; + lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); + luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue); + L->stacksize = realsize; + L->stack_last = L->stack+newsize; + correctstack(L, oldstack); +} + + +void luaD_reallocCI (lua_State *L, int newsize) { + CallInfo *oldci = L->base_ci; + luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo); + L->size_ci = newsize; + L->ci = (L->ci - oldci) + L->base_ci; + L->end_ci = L->base_ci + L->size_ci - 1; +} + + +void luaD_growstack (lua_State *L, int n) { + if (n <= L->stacksize) /* double size is enough? */ + luaD_reallocstack(L, 2*L->stacksize); + else + luaD_reallocstack(L, L->stacksize + n); +} + + +static CallInfo *growCI (lua_State *L) { + if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */ + luaD_throw(L, LUA_ERRERR); + else { + luaD_reallocCI(L, 2*L->size_ci); + if (L->size_ci > LUAI_MAXCALLS) + luaG_runerror(L, "stack overflow"); + } + return ++L->ci; +} + + +void luaD_callhook (lua_State *L, int event, int line) { + lua_Hook hook = L->hook; + if (hook && L->allowhook) { + ptrdiff_t top = savestack(L, L->top); + ptrdiff_t ci_top = savestack(L, L->ci->top); + lua_Debug ar; + ar.event = event; + ar.currentline = line; + if (event == LUA_HOOKTAILRET) + ar.i_ci = 0; /* tail call; no debug information about it */ + else + ar.i_ci = cast_int(L->ci - L->base_ci); + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ + L->ci->top = L->top + LUA_MINSTACK; + lua_assert(L->ci->top <= L->stack_last); + L->allowhook = 0; /* cannot call hooks inside a hook */ + lua_unlock(L); + (*hook)(L, &ar); + lua_lock(L); + lua_assert(!L->allowhook); + L->allowhook = 1; + L->ci->top = restorestack(L, ci_top); + L->top = restorestack(L, top); + } +} + + +static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { + int i; + int nfixargs = p->numparams; + Table *htab = NULL; + StkId base, fixed; + for (; actual < nfixargs; ++actual) + setnilvalue(L->top++); +#if defined(LUA_COMPAT_VARARG) + if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */ + int nvar = actual - nfixargs; /* number of extra arguments */ + lua_assert(p->is_vararg & VARARG_HASARG); + luaC_checkGC(L); + htab = luaH_new(L, nvar, 1); /* create `arg' table */ + for (i=0; itop - nvar + i); + /* store counter in field `n' */ + setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar)); + } +#endif + /* move fixed parameters to final position */ + fixed = L->top - actual; /* first fixed argument */ + base = L->top; /* final position of first argument */ + for (i=0; itop++, fixed+i); + setnilvalue(fixed+i); + } + /* add `arg' parameter */ + if (htab) { + sethvalue(L, L->top++, htab); + lua_assert(iswhite(obj2gco(htab))); + } + return base; +} + + +static StkId tryfuncTM (lua_State *L, StkId func) { + const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); + StkId p; + ptrdiff_t funcr = savestack(L, func); + if (!ttisfunction(tm)) + luaG_typeerror(L, func, "call"); + /* Open a hole inside the stack at `func' */ + for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); + incr_top(L); + func = restorestack(L, funcr); /* previous call may change stack */ + setobj2s(L, func, tm); /* tag method is the new function to be called */ + return func; +} + + + +#define inc_ci(L) \ + ((L->ci == L->end_ci) ? growCI(L) : \ + (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci)) + + +int luaD_precall (lua_State *L, StkId func, int nresults) { + LClosure *cl; + ptrdiff_t funcr; + if (!ttisfunction(func)) /* `func' is not a function? */ + func = tryfuncTM(L, func); /* check the `function' tag method */ + funcr = savestack(L, func); + cl = &clvalue(func)->l; + L->ci->savedpc = L->savedpc; + if (!cl->isC) { /* Lua function? prepare its call */ + CallInfo *ci; + StkId st, base; + Proto *p = cl->p; + luaD_checkstack(L, p->maxstacksize); + func = restorestack(L, funcr); + if (!p->is_vararg) { /* no varargs? */ + base = func + 1; + if (L->top > base + p->numparams) + L->top = base + p->numparams; + } + else { /* vararg function */ + int nargs = cast_int(L->top - func) - 1; + base = adjust_varargs(L, p, nargs); + func = restorestack(L, funcr); /* previous call may change the stack */ + } + ci = inc_ci(L); /* now `enter' new function */ + ci->func = func; + L->base = ci->base = base; + ci->top = L->base + p->maxstacksize; + lua_assert(ci->top <= L->stack_last); + L->savedpc = p->code; /* starting point */ + ci->tailcalls = 0; + ci->nresults = nresults; + for (st = L->top; st < ci->top; st++) + setnilvalue(st); + L->top = ci->top; + if (L->hookmask & LUA_MASKCALL) { + L->savedpc++; /* hooks assume 'pc' is already incremented */ + luaD_callhook(L, LUA_HOOKCALL, -1); + L->savedpc--; /* correct 'pc' */ + } + return PCRLUA; + } + else { /* if is a C function, call it */ + CallInfo *ci; + int n; + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ + ci = inc_ci(L); /* now `enter' new function */ + ci->func = restorestack(L, funcr); + L->base = ci->base = ci->func + 1; + ci->top = L->top + LUA_MINSTACK; + lua_assert(ci->top <= L->stack_last); + ci->nresults = nresults; + if (L->hookmask & LUA_MASKCALL) + luaD_callhook(L, LUA_HOOKCALL, -1); + lua_unlock(L); + n = (*curr_func(L)->c.f)(L); /* do the actual call */ + lua_lock(L); + if (n < 0) /* yielding? */ + return PCRYIELD; + else { + luaD_poscall(L, L->top - n); + return PCRC; + } + } +} + + +static StkId callrethooks (lua_State *L, StkId firstResult) { + ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ + luaD_callhook(L, LUA_HOOKRET, -1); + if (f_isLua(L->ci)) { /* Lua function? */ + while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */ + luaD_callhook(L, LUA_HOOKTAILRET, -1); + } + return restorestack(L, fr); +} + + +int luaD_poscall (lua_State *L, StkId firstResult) { + StkId res; + int wanted, i; + CallInfo *ci; + if (L->hookmask & LUA_MASKRET) + firstResult = callrethooks(L, firstResult); + ci = L->ci--; + res = ci->func; /* res == final position of 1st result */ + wanted = ci->nresults; + L->base = (ci - 1)->base; /* restore base */ + L->savedpc = (ci - 1)->savedpc; /* restore savedpc */ + /* move results to correct place */ + for (i = wanted; i != 0 && firstResult < L->top; i--) + setobjs2s(L, res++, firstResult++); + while (i-- > 0) + setnilvalue(res++); + L->top = res; + return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ +} + + +/* +** Call a function (C or Lua). The function to be called is at *func. +** The arguments are on the stack, right after the function. +** When returns, all the results are on the stack, starting at the original +** function position. +*/ +void luaD_call (lua_State *L, StkId func, int nResults) { + if (++L->nCcalls >= LUAI_MAXCCALLS) { + if (L->nCcalls == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ + } + if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ + luaV_execute(L, 1); /* call it */ + L->nCcalls--; + luaC_checkGC(L); +} + + +static void resume (lua_State *L, void *ud) { + StkId firstArg = cast(StkId, ud); + CallInfo *ci = L->ci; + if (L->status == 0) { /* start coroutine? */ + lua_assert(ci == L->base_ci && firstArg > L->base); + if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA) + return; + } + else { /* resuming from previous yield */ + lua_assert(L->status == LUA_YIELD); + L->status = 0; + if (!f_isLua(ci)) { /* `common' yield? */ + /* finish interrupted execution of `OP_CALL' */ + lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || + GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL); + if (luaD_poscall(L, firstArg)) /* complete it... */ + L->top = L->ci->top; /* and correct top if not multiple results */ + } + else /* yielded inside a hook: just continue its execution */ + L->base = L->ci->base; + } + luaV_execute(L, cast_int(L->ci - L->base_ci)); +} + + +static int resume_error (lua_State *L, const char *msg) { + L->top = L->ci->base; + setsvalue2s(L, L->top, luaS_new(L, msg)); + incr_top(L); + lua_unlock(L); + return LUA_ERRRUN; +} + + +LUA_API int lua_resume (lua_State *L, int nargs) { + int status; + lua_lock(L); + if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci)) + return resume_error(L, "cannot resume non-suspended coroutine"); + if (L->nCcalls >= LUAI_MAXCCALLS) + return resume_error(L, "C stack overflow"); + luai_userstateresume(L, nargs); + lua_assert(L->errfunc == 0); + L->baseCcalls = ++L->nCcalls; + status = luaD_rawrunprotected(L, resume, L->top - nargs); + if (status != 0) { /* error? */ + L->status = cast_byte(status); /* mark thread as `dead' */ + luaD_seterrorobj(L, status, L->top); + L->ci->top = L->top; + } + else { + lua_assert(L->nCcalls == L->baseCcalls); + status = L->status; + } + --L->nCcalls; + lua_unlock(L); + return status; +} + + +LUA_API int lua_yield (lua_State *L, int nresults) { + luai_userstateyield(L, nresults); + lua_lock(L); + if (L->nCcalls > L->baseCcalls) + luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); + L->base = L->top - nresults; /* protect stack slots below */ + L->status = LUA_YIELD; + lua_unlock(L); + return -1; +} + + +int luaD_pcall (lua_State *L, Pfunc func, void *u, + ptrdiff_t old_top, ptrdiff_t ef) { + int status; + unsigned short oldnCcalls = L->nCcalls; + ptrdiff_t old_ci = saveci(L, L->ci); + lu_byte old_allowhooks = L->allowhook; + ptrdiff_t old_errfunc = L->errfunc; + L->errfunc = ef; + status = luaD_rawrunprotected(L, func, u); + if (status != 0) { /* an error occurred? */ + StkId oldtop = restorestack(L, old_top); + luaF_close(L, oldtop); /* close eventual pending closures */ + luaD_seterrorobj(L, status, oldtop); + L->nCcalls = oldnCcalls; + L->ci = restoreci(L, old_ci); + L->base = L->ci->base; + L->savedpc = L->ci->savedpc; + L->allowhook = old_allowhooks; + restore_stack_limit(L); + } + L->errfunc = old_errfunc; + return status; +} + + + +/* +** Execute a protected parser. +*/ +struct SParser { /* data to `f_parser' */ + ZIO *z; + Mbuffer buff; /* buffer to be used by the scanner */ + const char *name; +}; + +static void f_parser (lua_State *L, void *ud) { + int i; + Proto *tf; + Closure *cl; + struct SParser *p = cast(struct SParser *, ud); + int c = luaZ_lookahead(p->z); + luaC_checkGC(L); + tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z, + &p->buff, p->name); + cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); + cl->l.p = tf; + for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ + cl->l.upvals[i] = luaF_newupval(L); + setclvalue(L, L->top, cl); + incr_top(L); +} + + +int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { + struct SParser p; + int status; + p.z = z; p.name = name; + luaZ_initbuffer(L, &p.buff); + status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); + luaZ_freebuffer(L, &p.buff); + return status; +} + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ldo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ldo.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,57 @@ +/* +** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ +** Stack and Call structure of Lua +** See Copyright Notice in lua.h +*/ + +#ifndef ldo_h +#define ldo_h + + +#include "lobject.h" +#include "lstate.h" +#include "lzio.h" + + +#define luaD_checkstack(L,n) \ + if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ + luaD_growstack(L, n); \ + else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); + + +#define incr_top(L) {luaD_checkstack(L,1); L->top++;} + +#define savestack(L,p) ((char *)(p) - (char *)L->stack) +#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) + +#define saveci(L,p) ((char *)(p) - (char *)L->base_ci) +#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) + + +/* results from luaD_precall */ +#define PCRLUA 0 /* initiated a call to a Lua function */ +#define PCRC 1 /* did a call to a C function */ +#define PCRYIELD 2 /* C funtion yielded */ + + +/* type of protected functions, to be ran by `runprotected' */ +typedef void (*Pfunc) (lua_State *L, void *ud); + +LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); +LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); +LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); +LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); +LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, + ptrdiff_t oldtop, ptrdiff_t ef); +LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); +LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); +LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); +LUAI_FUNC void luaD_growstack (lua_State *L, int n); + +LUAI_FUNC void luaD_throw (lua_State *L, int errcode); +LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); + +LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ldump.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ldump.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,164 @@ +/* +** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ +** save precompiled Lua chunks +** See Copyright Notice in lua.h +*/ + +#include + +#define ldump_c +#define LUA_CORE + +#include "lua.h" + +#include "lobject.h" +#include "lstate.h" +#include "lundump.h" + +typedef struct { + lua_State* L; + lua_Writer writer; + void* data; + int strip; + int status; +} DumpState; + +#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) +#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) + +static void DumpBlock(const void* b, size_t size, DumpState* D) +{ + if (D->status==0) + { + lua_unlock(D->L); + D->status=(*D->writer)(D->L,b,size,D->data); + lua_lock(D->L); + } +} + +static void DumpChar(int y, DumpState* D) +{ + char x=(char)y; + DumpVar(x,D); +} + +static void DumpInt(int x, DumpState* D) +{ + DumpVar(x,D); +} + +static void DumpNumber(lua_Number x, DumpState* D) +{ + DumpVar(x,D); +} + +static void DumpVector(const void* b, int n, size_t size, DumpState* D) +{ + DumpInt(n,D); + DumpMem(b,n,size,D); +} + +static void DumpString(const TString* s, DumpState* D) +{ + if (s==NULL || getstr(s)==NULL) + { + size_t size=0; + DumpVar(size,D); + } + else + { + size_t size=s->tsv.len+1; /* include trailing '\0' */ + DumpVar(size,D); + DumpBlock(getstr(s),size,D); + } +} + +#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) + +static void DumpFunction(const Proto* f, const TString* p, DumpState* D); + +static void DumpConstants(const Proto* f, DumpState* D) +{ + int i,n=f->sizek; + DumpInt(n,D); + for (i=0; ik[i]; + DumpChar(ttype(o),D); + switch (ttype(o)) + { + case LUA_TNIL: + break; + case LUA_TBOOLEAN: + DumpChar(bvalue(o),D); + break; + case LUA_TNUMBER: + DumpNumber(nvalue(o),D); + break; + case LUA_TSTRING: + DumpString(rawtsvalue(o),D); + break; + default: + lua_assert(0); /* cannot happen */ + break; + } + } + n=f->sizep; + DumpInt(n,D); + for (i=0; ip[i],f->source,D); +} + +static void DumpDebug(const Proto* f, DumpState* D) +{ + int i,n; + n= (D->strip) ? 0 : f->sizelineinfo; + DumpVector(f->lineinfo,n,sizeof(int),D); + n= (D->strip) ? 0 : f->sizelocvars; + DumpInt(n,D); + for (i=0; ilocvars[i].varname,D); + DumpInt(f->locvars[i].startpc,D); + DumpInt(f->locvars[i].endpc,D); + } + n= (D->strip) ? 0 : f->sizeupvalues; + DumpInt(n,D); + for (i=0; iupvalues[i],D); +} + +static void DumpFunction(const Proto* f, const TString* p, DumpState* D) +{ + DumpString((f->source==p || D->strip) ? NULL : f->source,D); + DumpInt(f->linedefined,D); + DumpInt(f->lastlinedefined,D); + DumpChar(f->nups,D); + DumpChar(f->numparams,D); + DumpChar(f->is_vararg,D); + DumpChar(f->maxstacksize,D); + DumpCode(f,D); + DumpConstants(f,D); + DumpDebug(f,D); +} + +static void DumpHeader(DumpState* D) +{ + char h[LUAC_HEADERSIZE]; + luaU_header(h); + DumpBlock(h,LUAC_HEADERSIZE,D); +} + +/* +** dump Lua function as precompiled chunk +*/ +int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) +{ + DumpState D; + D.L=L; + D.writer=w; + D.data=data; + D.strip=strip; + D.status=0; + DumpHeader(&D); + DumpFunction(f,NULL,&D); + return D.status; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lfunc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lfunc.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,174 @@ +/* +** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ +** Auxiliary functions to manipulate prototypes and closures +** See Copyright Notice in lua.h +*/ + + +#include + +#define lfunc_c +#define LUA_CORE + +#include "lua.h" + +#include "lfunc.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" + + + +Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { + Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); + luaC_link(L, obj2gco(c), LUA_TFUNCTION); + c->c.isC = 1; + c->c.env = e; + c->c.nupvalues = cast_byte(nelems); + return c; +} + + +Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { + Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); + luaC_link(L, obj2gco(c), LUA_TFUNCTION); + c->l.isC = 0; + c->l.env = e; + c->l.nupvalues = cast_byte(nelems); + while (nelems--) c->l.upvals[nelems] = NULL; + return c; +} + + +UpVal *luaF_newupval (lua_State *L) { + UpVal *uv = luaM_new(L, UpVal); + luaC_link(L, obj2gco(uv), LUA_TUPVAL); + uv->v = &uv->u.value; + setnilvalue(uv->v); + return uv; +} + + +UpVal *luaF_findupval (lua_State *L, StkId level) { + global_State *g = G(L); + GCObject **pp = &L->openupval; + UpVal *p; + UpVal *uv; + while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { + lua_assert(p->v != &p->u.value); + if (p->v == level) { /* found a corresponding upvalue? */ + if (isdead(g, obj2gco(p))) /* is it dead? */ + changewhite(obj2gco(p)); /* ressurect it */ + return p; + } + pp = &p->next; + } + uv = luaM_new(L, UpVal); /* not found: create a new one */ + uv->tt = LUA_TUPVAL; + uv->marked = luaC_white(g); + uv->v = level; /* current value lives in the stack */ + uv->next = *pp; /* chain it in the proper position */ + *pp = obj2gco(uv); + uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ + uv->u.l.next = g->uvhead.u.l.next; + uv->u.l.next->u.l.prev = uv; + g->uvhead.u.l.next = uv; + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); + return uv; +} + + +static void unlinkupval (UpVal *uv) { + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); + uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ + uv->u.l.prev->u.l.next = uv->u.l.next; +} + + +void luaF_freeupval (lua_State *L, UpVal *uv) { + if (uv->v != &uv->u.value) /* is it open? */ + unlinkupval(uv); /* remove from open list */ + luaM_free(L, uv); /* free upvalue */ +} + + +void luaF_close (lua_State *L, StkId level) { + UpVal *uv; + global_State *g = G(L); + while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { + GCObject *o = obj2gco(uv); + lua_assert(!isblack(o) && uv->v != &uv->u.value); + L->openupval = uv->next; /* remove from `open' list */ + if (isdead(g, o)) + luaF_freeupval(L, uv); /* free upvalue */ + else { + unlinkupval(uv); + setobj(L, &uv->u.value, uv->v); + uv->v = &uv->u.value; /* now current value lives here */ + luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ + } + } +} + + +Proto *luaF_newproto (lua_State *L) { + Proto *f = luaM_new(L, Proto); + luaC_link(L, obj2gco(f), LUA_TPROTO); + f->k = NULL; + f->sizek = 0; + f->p = NULL; + f->sizep = 0; + f->code = NULL; + f->sizecode = 0; + f->sizelineinfo = 0; + f->sizeupvalues = 0; + f->nups = 0; + f->upvalues = NULL; + f->numparams = 0; + f->is_vararg = 0; + f->maxstacksize = 0; + f->lineinfo = NULL; + f->sizelocvars = 0; + f->locvars = NULL; + f->linedefined = 0; + f->lastlinedefined = 0; + f->source = NULL; + return f; +} + + +void luaF_freeproto (lua_State *L, Proto *f) { + luaM_freearray(L, f->code, f->sizecode, Instruction); + luaM_freearray(L, f->p, f->sizep, Proto *); + luaM_freearray(L, f->k, f->sizek, TValue); + luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); + luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); + luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); + luaM_free(L, f); +} + + +void luaF_freeclosure (lua_State *L, Closure *c) { + int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : + sizeLclosure(c->l.nupvalues); + luaM_freemem(L, c, size); +} + + +/* +** Look for n-th local variable at line `line' in function `func'. +** Returns NULL if not found. +*/ +const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { + int i; + for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { + if (pc < f->locvars[i].endpc) { /* is variable active? */ + local_number--; + if (local_number == 0) + return getstr(f->locvars[i].varname); + } + } + return NULL; /* not found */ +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lfunc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lfunc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,34 @@ +/* +** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions to manipulate prototypes and closures +** See Copyright Notice in lua.h +*/ + +#ifndef lfunc_h +#define lfunc_h + + +#include "lobject.h" + + +#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ + cast(int, sizeof(TValue)*((n)-1))) + +#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ + cast(int, sizeof(TValue *)*((n)-1))) + + +LUAI_FUNC Proto *luaF_newproto (lua_State *L); +LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); +LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); +LUAI_FUNC UpVal *luaF_newupval (lua_State *L); +LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); +LUAI_FUNC void luaF_close (lua_State *L, StkId level); +LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); +LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); +LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); +LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, + int pc); + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lgc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lgc.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,711 @@ +/* +** $Id: lgc.c,v 2.38.1.1 2007/12/27 13:02:25 roberto Exp $ +** Garbage Collector +** See Copyright Notice in lua.h +*/ + +#include + +#define lgc_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" + + +#define GCSTEPSIZE 1024u +#define GCSWEEPMAX 40 +#define GCSWEEPCOST 10 +#define GCFINALIZECOST 100 + + +#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS)) + +#define makewhite(g,x) \ + ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g))) + +#define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) +#define black2gray(x) resetbit((x)->gch.marked, BLACKBIT) + +#define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT) + + +#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) +#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT) + + +#define KEYWEAK bitmask(KEYWEAKBIT) +#define VALUEWEAK bitmask(VALUEWEAKBIT) + + + +#define markvalue(g,o) { checkconsistency(o); \ + if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); } + +#define markobject(g,t) { if (iswhite(obj2gco(t))) \ + reallymarkobject(g, obj2gco(t)); } + + +#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause) + + +static void removeentry (Node *n) { + lua_assert(ttisnil(gval(n))); + if (iscollectable(gkey(n))) + setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */ +} + + +static void reallymarkobject (global_State *g, GCObject *o) { + lua_assert(iswhite(o) && !isdead(g, o)); + white2gray(o); + switch (o->gch.tt) { + case LUA_TSTRING: { + return; + } + case LUA_TUSERDATA: { + Table *mt = gco2u(o)->metatable; + gray2black(o); /* udata are never gray */ + if (mt) markobject(g, mt); + markobject(g, gco2u(o)->env); + return; + } + case LUA_TUPVAL: { + UpVal *uv = gco2uv(o); + markvalue(g, uv->v); + if (uv->v == &uv->u.value) /* closed? */ + gray2black(o); /* open upvalues are never black */ + return; + } + case LUA_TFUNCTION: { + gco2cl(o)->c.gclist = g->gray; + g->gray = o; + break; + } + case LUA_TTABLE: { + gco2h(o)->gclist = g->gray; + g->gray = o; + break; + } + case LUA_TTHREAD: { + gco2th(o)->gclist = g->gray; + g->gray = o; + break; + } + case LUA_TPROTO: { + gco2p(o)->gclist = g->gray; + g->gray = o; + break; + } + default: lua_assert(0); + } +} + + +static void marktmu (global_State *g) { + GCObject *u = g->tmudata; + if (u) { + do { + u = u->gch.next; + makewhite(g, u); /* may be marked, if left from previous GC */ + reallymarkobject(g, u); + } while (u != g->tmudata); + } +} + + +/* move `dead' udata that need finalization to list `tmudata' */ +size_t luaC_separateudata (lua_State *L, int all) { + global_State *g = G(L); + size_t deadmem = 0; + GCObject **p = &g->mainthread->next; + GCObject *curr; + while ((curr = *p) != NULL) { + if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) + p = &curr->gch.next; /* don't bother with them */ + else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { + markfinalized(gco2u(curr)); /* don't need finalization */ + p = &curr->gch.next; + } + else { /* must call its gc method */ + deadmem += sizeudata(gco2u(curr)); + markfinalized(gco2u(curr)); + *p = curr->gch.next; + /* link `curr' at the end of `tmudata' list */ + if (g->tmudata == NULL) /* list is empty? */ + g->tmudata = curr->gch.next = curr; /* creates a circular list */ + else { + curr->gch.next = g->tmudata->gch.next; + g->tmudata->gch.next = curr; + g->tmudata = curr; + } + } + } + return deadmem; +} + + +static int traversetable (global_State *g, Table *h) { + int i; + int weakkey = 0; + int weakvalue = 0; + const TValue *mode; + if (h->metatable) + markobject(g, h->metatable); + mode = gfasttm(g, h->metatable, TM_MODE); + if (mode && ttisstring(mode)) { /* is there a weak mode? */ + weakkey = (strchr(svalue(mode), 'k') != NULL); + weakvalue = (strchr(svalue(mode), 'v') != NULL); + if (weakkey || weakvalue) { /* is really weak? */ + h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ + h->marked |= cast_byte((weakkey << KEYWEAKBIT) | + (weakvalue << VALUEWEAKBIT)); + h->gclist = g->weak; /* must be cleared after GC, ... */ + g->weak = obj2gco(h); /* ... so put in the appropriate list */ + } + } + if (weakkey && weakvalue) return 1; + if (!weakvalue) { + i = h->sizearray; + while (i--) + markvalue(g, &h->array[i]); + } + i = sizenode(h); + while (i--) { + Node *n = gnode(h, i); + lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n))); + if (ttisnil(gval(n))) + removeentry(n); /* remove empty entries */ + else { + lua_assert(!ttisnil(gkey(n))); + if (!weakkey) markvalue(g, gkey(n)); + if (!weakvalue) markvalue(g, gval(n)); + } + } + return weakkey || weakvalue; +} + + +/* +** All marks are conditional because a GC may happen while the +** prototype is still being created +*/ +static void traverseproto (global_State *g, Proto *f) { + int i; + if (f->source) stringmark(f->source); + for (i=0; isizek; i++) /* mark literals */ + markvalue(g, &f->k[i]); + for (i=0; isizeupvalues; i++) { /* mark upvalue names */ + if (f->upvalues[i]) + stringmark(f->upvalues[i]); + } + for (i=0; isizep; i++) { /* mark nested protos */ + if (f->p[i]) + markobject(g, f->p[i]); + } + for (i=0; isizelocvars; i++) { /* mark local-variable names */ + if (f->locvars[i].varname) + stringmark(f->locvars[i].varname); + } +} + + + +static void traverseclosure (global_State *g, Closure *cl) { + markobject(g, cl->c.env); + if (cl->c.isC) { + int i; + for (i=0; ic.nupvalues; i++) /* mark its upvalues */ + markvalue(g, &cl->c.upvalue[i]); + } + else { + int i; + lua_assert(cl->l.nupvalues == cl->l.p->nups); + markobject(g, cl->l.p); + for (i=0; il.nupvalues; i++) /* mark its upvalues */ + markobject(g, cl->l.upvals[i]); + } +} + + +static void checkstacksizes (lua_State *L, StkId max) { + int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */ + int s_used = cast_int(max - L->stack); /* part of stack in use */ + if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */ + return; /* do not touch the stacks */ + if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci) + luaD_reallocCI(L, L->size_ci/2); /* still big enough... */ + condhardstacktests(luaD_reallocCI(L, ci_used + 1)); + if (4*s_used < L->stacksize && + 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize) + luaD_reallocstack(L, L->stacksize/2); /* still big enough... */ + condhardstacktests(luaD_reallocstack(L, s_used)); +} + + +static void traversestack (global_State *g, lua_State *l) { + StkId o, lim; + CallInfo *ci; + markvalue(g, gt(l)); + lim = l->top; + for (ci = l->base_ci; ci <= l->ci; ci++) { + lua_assert(ci->top <= l->stack_last); + if (lim < ci->top) lim = ci->top; + } + for (o = l->stack; o < l->top; o++) + markvalue(g, o); + for (; o <= lim; o++) + setnilvalue(o); + checkstacksizes(l, lim); +} + + +/* +** traverse one gray object, turning it to black. +** Returns `quantity' traversed. +*/ +static l_mem propagatemark (global_State *g) { + GCObject *o = g->gray; + lua_assert(isgray(o)); + gray2black(o); + switch (o->gch.tt) { + case LUA_TTABLE: { + Table *h = gco2h(o); + g->gray = h->gclist; + if (traversetable(g, h)) /* table is weak? */ + black2gray(o); /* keep it gray */ + return sizeof(Table) + sizeof(TValue) * h->sizearray + + sizeof(Node) * sizenode(h); + } + case LUA_TFUNCTION: { + Closure *cl = gco2cl(o); + g->gray = cl->c.gclist; + traverseclosure(g, cl); + return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : + sizeLclosure(cl->l.nupvalues); + } + case LUA_TTHREAD: { + lua_State *th = gco2th(o); + g->gray = th->gclist; + th->gclist = g->grayagain; + g->grayagain = o; + black2gray(o); + traversestack(g, th); + return sizeof(lua_State) + sizeof(TValue) * th->stacksize + + sizeof(CallInfo) * th->size_ci; + } + case LUA_TPROTO: { + Proto *p = gco2p(o); + g->gray = p->gclist; + traverseproto(g, p); + return sizeof(Proto) + sizeof(Instruction) * p->sizecode + + sizeof(Proto *) * p->sizep + + sizeof(TValue) * p->sizek + + sizeof(int) * p->sizelineinfo + + sizeof(LocVar) * p->sizelocvars + + sizeof(TString *) * p->sizeupvalues; + } + default: lua_assert(0); return 0; + } +} + + +static size_t propagateall (global_State *g) { + size_t m = 0; + while (g->gray) m += propagatemark(g); + return m; +} + + +/* +** The next function tells whether a key or value can be cleared from +** a weak table. Non-collectable objects are never removed from weak +** tables. Strings behave as `values', so are never removed too. for +** other objects: if really collected, cannot keep them; for userdata +** being finalized, keep them in keys, but not in values +*/ +static int iscleared (const TValue *o, int iskey) { + if (!iscollectable(o)) return 0; + if (ttisstring(o)) { + stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ + return 0; + } + return iswhite(gcvalue(o)) || + (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o)))); +} + + +/* +** clear collected entries from weaktables +*/ +static void cleartable (GCObject *l) { + while (l) { + Table *h = gco2h(l); + int i = h->sizearray; + lua_assert(testbit(h->marked, VALUEWEAKBIT) || + testbit(h->marked, KEYWEAKBIT)); + if (testbit(h->marked, VALUEWEAKBIT)) { + while (i--) { + TValue *o = &h->array[i]; + if (iscleared(o, 0)) /* value was collected? */ + setnilvalue(o); /* remove value */ + } + } + i = sizenode(h); + while (i--) { + Node *n = gnode(h, i); + if (!ttisnil(gval(n)) && /* non-empty entry? */ + (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { + setnilvalue(gval(n)); /* remove value ... */ + removeentry(n); /* remove entry from table */ + } + } + l = h->gclist; + } +} + + +static void freeobj (lua_State *L, GCObject *o) { + switch (o->gch.tt) { + case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; + case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break; + case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; + case LUA_TTABLE: luaH_free(L, gco2h(o)); break; + case LUA_TTHREAD: { + lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread); + luaE_freethread(L, gco2th(o)); + break; + } + case LUA_TSTRING: { + G(L)->strt.nuse--; + luaM_freemem(L, o, sizestring(gco2ts(o))); + break; + } + case LUA_TUSERDATA: { + luaM_freemem(L, o, sizeudata(gco2u(o))); + break; + } + default: lua_assert(0); + } +} + + + +#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) + + +static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { + GCObject *curr; + global_State *g = G(L); + int deadmask = otherwhite(g); + while ((curr = *p) != NULL && count-- > 0) { + if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */ + sweepwholelist(L, &gco2th(curr)->openupval); + if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */ + lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT)); + makewhite(g, curr); /* make it white (for next cycle) */ + p = &curr->gch.next; + } + else { /* must erase `curr' */ + lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT)); + *p = curr->gch.next; + if (curr == g->rootgc) /* is the first element of the list? */ + g->rootgc = curr->gch.next; /* adjust first */ + freeobj(L, curr); + } + } + return p; +} + + +static void checkSizes (lua_State *L) { + global_State *g = G(L); + /* check size of string hash */ + if (g->strt.nuse < cast(lu_int32, g->strt.size/4) && + g->strt.size > MINSTRTABSIZE*2) + luaS_resize(L, g->strt.size/2); /* table is too big */ + /* check size of buffer */ + if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */ + size_t newsize = luaZ_sizebuffer(&g->buff) / 2; + luaZ_resizebuffer(L, &g->buff, newsize); + } +} + + +static void GCTM (lua_State *L) { + global_State *g = G(L); + GCObject *o = g->tmudata->gch.next; /* get first element */ + Udata *udata = rawgco2u(o); + const TValue *tm; + /* remove udata from `tmudata' */ + if (o == g->tmudata) /* last element? */ + g->tmudata = NULL; + else + g->tmudata->gch.next = udata->uv.next; + udata->uv.next = g->mainthread->next; /* return it to `root' list */ + g->mainthread->next = o; + makewhite(g, o); + tm = fasttm(L, udata->uv.metatable, TM_GC); + if (tm != NULL) { + lu_byte oldah = L->allowhook; + lu_mem oldt = g->GCthreshold; + L->allowhook = 0; /* stop debug hooks during GC tag method */ + g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */ + setobj2s(L, L->top, tm); + setuvalue(L, L->top+1, udata); + L->top += 2; + luaD_call(L, L->top - 2, 0); + L->allowhook = oldah; /* restore hooks */ + g->GCthreshold = oldt; /* restore threshold */ + } +} + + +/* +** Call all GC tag methods +*/ +void luaC_callGCTM (lua_State *L) { + while (G(L)->tmudata) + GCTM(L); +} + + +void luaC_freeall (lua_State *L) { + global_State *g = G(L); + int i; + g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */ + sweepwholelist(L, &g->rootgc); + for (i = 0; i < g->strt.size; i++) /* free all string lists */ + sweepwholelist(L, &g->strt.hash[i]); +} + + +static void markmt (global_State *g) { + int i; + for (i=0; imt[i]) markobject(g, g->mt[i]); +} + + +/* mark root set */ +static void markroot (lua_State *L) { + global_State *g = G(L); + g->gray = NULL; + g->grayagain = NULL; + g->weak = NULL; + markobject(g, g->mainthread); + /* make global table be traversed before main stack */ + markvalue(g, gt(g->mainthread)); + markvalue(g, registry(L)); + markmt(g); + g->gcstate = GCSpropagate; +} + + +static void remarkupvals (global_State *g) { + UpVal *uv; + for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); + if (isgray(obj2gco(uv))) + markvalue(g, uv->v); + } +} + + +static void atomic (lua_State *L) { + global_State *g = G(L); + size_t udsize; /* total size of userdata to be finalized */ + /* remark occasional upvalues of (maybe) dead threads */ + remarkupvals(g); + /* traverse objects cautch by write barrier and by 'remarkupvals' */ + propagateall(g); + /* remark weak tables */ + g->gray = g->weak; + g->weak = NULL; + lua_assert(!iswhite(obj2gco(g->mainthread))); + markobject(g, L); /* mark running thread */ + markmt(g); /* mark basic metatables (again) */ + propagateall(g); + /* remark gray again */ + g->gray = g->grayagain; + g->grayagain = NULL; + propagateall(g); + udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */ + marktmu(g); /* mark `preserved' userdata */ + udsize += propagateall(g); /* remark, to propagate `preserveness' */ + cleartable(g->weak); /* remove collected objects from weak tables */ + /* flip current white */ + g->currentwhite = cast_byte(otherwhite(g)); + g->sweepstrgc = 0; + g->sweepgc = &g->rootgc; + g->gcstate = GCSsweepstring; + g->estimate = g->totalbytes - udsize; /* first estimate */ +} + + +static l_mem singlestep (lua_State *L) { + global_State *g = G(L); + /*lua_checkmemory(L);*/ + switch (g->gcstate) { + case GCSpause: { + markroot(L); /* start a new collection */ + return 0; + } + case GCSpropagate: { + if (g->gray) + return propagatemark(g); + else { /* no more `gray' objects */ + atomic(L); /* finish mark phase */ + return 0; + } + } + case GCSsweepstring: { + lu_mem old = g->totalbytes; + sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]); + if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */ + g->gcstate = GCSsweep; /* end sweep-string phase */ + lua_assert(old >= g->totalbytes); + g->estimate -= old - g->totalbytes; + return GCSWEEPCOST; + } + case GCSsweep: { + lu_mem old = g->totalbytes; + g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); + if (*g->sweepgc == NULL) { /* nothing more to sweep? */ + checkSizes(L); + g->gcstate = GCSfinalize; /* end sweep phase */ + } + lua_assert(old >= g->totalbytes); + g->estimate -= old - g->totalbytes; + return GCSWEEPMAX*GCSWEEPCOST; + } + case GCSfinalize: { + if (g->tmudata) { + GCTM(L); + if (g->estimate > GCFINALIZECOST) + g->estimate -= GCFINALIZECOST; + return GCFINALIZECOST; + } + else { + g->gcstate = GCSpause; /* end collection */ + g->gcdept = 0; + return 0; + } + } + default: lua_assert(0); return 0; + } +} + + +void luaC_step (lua_State *L) { + global_State *g = G(L); + l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul; + if (lim == 0) + lim = (MAX_LUMEM-1)/2; /* no limit */ + g->gcdept += g->totalbytes - g->GCthreshold; + do { + lim -= singlestep(L); + if (g->gcstate == GCSpause) + break; + } while (lim > 0); + if (g->gcstate != GCSpause) { + if (g->gcdept < GCSTEPSIZE) + g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/ + else { + g->gcdept -= GCSTEPSIZE; + g->GCthreshold = g->totalbytes; + } + } + else { + lua_assert(g->totalbytes >= g->estimate); + setthreshold(g); + } +} + + +void luaC_fullgc (lua_State *L) { + global_State *g = G(L); + if (g->gcstate <= GCSpropagate) { + /* reset sweep marks to sweep all elements (returning them to white) */ + g->sweepstrgc = 0; + g->sweepgc = &g->rootgc; + /* reset other collector lists */ + g->gray = NULL; + g->grayagain = NULL; + g->weak = NULL; + g->gcstate = GCSsweepstring; + } + lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate); + /* finish any pending sweep phase */ + while (g->gcstate != GCSfinalize) { + lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep); + singlestep(L); + } + markroot(L); + while (g->gcstate != GCSpause) { + singlestep(L); + } + setthreshold(g); +} + + +void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { + global_State *g = G(L); + lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); + lua_assert(ttype(&o->gch) != LUA_TTABLE); + /* must keep invariant? */ + if (g->gcstate == GCSpropagate) + reallymarkobject(g, v); /* restore invariant */ + else /* don't mind */ + makewhite(g, o); /* mark as white just to avoid other barriers */ +} + + +void luaC_barrierback (lua_State *L, Table *t) { + global_State *g = G(L); + GCObject *o = obj2gco(t); + lua_assert(isblack(o) && !isdead(g, o)); + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); + black2gray(o); /* make table gray (again) */ + t->gclist = g->grayagain; + g->grayagain = o; +} + + +void luaC_link (lua_State *L, GCObject *o, lu_byte tt) { + global_State *g = G(L); + o->gch.next = g->rootgc; + g->rootgc = o; + o->gch.marked = luaC_white(g); + o->gch.tt = tt; +} + + +void luaC_linkupval (lua_State *L, UpVal *uv) { + global_State *g = G(L); + GCObject *o = obj2gco(uv); + o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */ + g->rootgc = o; + if (isgray(o)) { + if (g->gcstate == GCSpropagate) { + gray2black(o); /* closed upvalues need barrier */ + luaC_barrier(L, uv, uv->v); + } + else { /* sweep phase: sweep it (turning it into white) */ + makewhite(g, o); + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); + } + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lgc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lgc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,110 @@ +/* +** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ +** Garbage Collector +** See Copyright Notice in lua.h +*/ + +#ifndef lgc_h +#define lgc_h + + +#include "lobject.h" + + +/* +** Possible states of the Garbage Collector +*/ +#define GCSpause 0 +#define GCSpropagate 1 +#define GCSsweepstring 2 +#define GCSsweep 3 +#define GCSfinalize 4 + + +/* +** some userful bit tricks +*/ +#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) +#define setbits(x,m) ((x) |= (m)) +#define testbits(x,m) ((x) & (m)) +#define bitmask(b) (1<<(b)) +#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) +#define l_setbit(x,b) setbits(x, bitmask(b)) +#define resetbit(x,b) resetbits(x, bitmask(b)) +#define testbit(x,b) testbits(x, bitmask(b)) +#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) +#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) +#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) + + + +/* +** Layout for bit use in `marked' field: +** bit 0 - object is white (type 0) +** bit 1 - object is white (type 1) +** bit 2 - object is black +** bit 3 - for userdata: has been finalized +** bit 3 - for tables: has weak keys +** bit 4 - for tables: has weak values +** bit 5 - object is fixed (should not be collected) +** bit 6 - object is "super" fixed (only the main thread) +*/ + + +#define WHITE0BIT 0 +#define WHITE1BIT 1 +#define BLACKBIT 2 +#define FINALIZEDBIT 3 +#define KEYWEAKBIT 3 +#define VALUEWEAKBIT 4 +#define FIXEDBIT 5 +#define SFIXEDBIT 6 +#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) + + +#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) +#define isblack(x) testbit((x)->gch.marked, BLACKBIT) +#define isgray(x) (!isblack(x) && !iswhite(x)) + +#define otherwhite(g) (g->currentwhite ^ WHITEBITS) +#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) + +#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) +#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) + +#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) + +#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) + + +#define luaC_checkGC(L) { \ + condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ + if (G(L)->totalbytes >= G(L)->GCthreshold) \ + luaC_step(L); } + + +#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ + luaC_barrierf(L,obj2gco(p),gcvalue(v)); } + +#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ + luaC_barrierback(L,t); } + +#define luaC_objbarrier(L,p,o) \ + { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ + luaC_barrierf(L,obj2gco(p),obj2gco(o)); } + +#define luaC_objbarriert(L,t,o) \ + { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } + +LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); +LUAI_FUNC void luaC_callGCTM (lua_State *L); +LUAI_FUNC void luaC_freeall (lua_State *L); +LUAI_FUNC void luaC_step (lua_State *L); +LUAI_FUNC void luaC_fullgc (lua_State *L); +LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); +LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); +LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); +LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/linit.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/linit.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,38 @@ +/* +** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ +** Initialization of libraries for lua.c +** See Copyright Notice in lua.h +*/ + + +#define linit_c +#define LUA_LIB + +#include "lua.h" + +#include "lualib.h" +#include "lauxlib.h" + + +static const luaL_Reg lualibs[] = { + {"", luaopen_base}, + {LUA_LOADLIBNAME, luaopen_package}, + {LUA_TABLIBNAME, luaopen_table}, + {LUA_IOLIBNAME, luaopen_io}, + {LUA_OSLIBNAME, luaopen_os}, + {LUA_STRLIBNAME, luaopen_string}, + {LUA_MATHLIBNAME, luaopen_math}, + {LUA_DBLIBNAME, luaopen_debug}, + {NULL, NULL} +}; + + +LUALIB_API void luaL_openlibs (lua_State *L) { + const luaL_Reg *lib = lualibs; + for (; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_pushstring(L, lib->name); + lua_call(L, 1, 0); + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/liolib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/liolib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,553 @@ +/* +** $Id: liolib.c,v 2.73.1.3 2008/01/18 17:47:43 roberto Exp $ +** Standard I/O (and system) library +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include + +#define liolib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + + +#define IO_INPUT 1 +#define IO_OUTPUT 2 + + +static const char *const fnames[] = {"input", "output"}; + + +static int pushresult (lua_State *L, int i, const char *filename) { + int en = errno; /* calls to Lua API may change this value */ + if (i) { + lua_pushboolean(L, 1); + return 1; + } + else { + lua_pushnil(L); + if (filename) + lua_pushfstring(L, "%s: %s", filename, strerror(en)); + else + lua_pushfstring(L, "%s", strerror(en)); + lua_pushinteger(L, en); + return 3; + } +} + + +static void fileerror (lua_State *L, int arg, const char *filename) { + lua_pushfstring(L, "%s: %s", filename, strerror(errno)); + luaL_argerror(L, arg, lua_tostring(L, -1)); +} + + +#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) + + +static int io_type (lua_State *L) { + void *ud; + luaL_checkany(L, 1); + ud = lua_touserdata(L, 1); + lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); + if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) + lua_pushnil(L); /* not a file */ + else if (*((FILE **)ud) == NULL) + lua_pushliteral(L, "closed file"); + else + lua_pushliteral(L, "file"); + return 1; +} + + +static FILE *tofile (lua_State *L) { + FILE **f = tofilep(L); + if (*f == NULL) + luaL_error(L, "attempt to use a closed file"); + return *f; +} + + + +/* +** When creating file handles, always creates a `closed' file handle +** before opening the actual file; so, if there is a memory error, the +** file is not left opened. +*/ +static FILE **newfile (lua_State *L) { + FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); + *pf = NULL; /* file handle is currently `closed' */ + luaL_getmetatable(L, LUA_FILEHANDLE); + lua_setmetatable(L, -2); + return pf; +} + + +/* +** function to (not) close the standard files stdin, stdout, and stderr +*/ +static int io_noclose (lua_State *L) { + lua_pushnil(L); + lua_pushliteral(L, "cannot close standard file"); + return 2; +} + + +/* +** function to close 'popen' files +*/ +static int io_pclose (lua_State *L) { + FILE **p = tofilep(L); + int ok = lua_pclose(L, *p); + *p = NULL; + return pushresult(L, ok, NULL); +} + + +/* +** function to close regular files +*/ +static int io_fclose (lua_State *L) { + FILE **p = tofilep(L); + int ok = (fclose(*p) == 0); + *p = NULL; + return pushresult(L, ok, NULL); +} + + +static int aux_close (lua_State *L) { + lua_getfenv(L, 1); + lua_getfield(L, -1, "__close"); + return (lua_tocfunction(L, -1))(L); +} + + +static int io_close (lua_State *L) { + if (lua_isnone(L, 1)) + lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); + tofile(L); /* make sure argument is a file */ + return aux_close(L); +} + + +static int io_gc (lua_State *L) { + FILE *f = *tofilep(L); + /* ignore closed files */ + if (f != NULL) + aux_close(L); + return 0; +} + + +static int io_tostring (lua_State *L) { + FILE *f = *tofilep(L); + if (f == NULL) + lua_pushliteral(L, "file (closed)"); + else + lua_pushfstring(L, "file (%p)", f); + return 1; +} + + +static int io_open (lua_State *L) { + const char *filename = luaL_checkstring(L, 1); + const char *mode = luaL_optstring(L, 2, "r"); + FILE **pf = newfile(L); + *pf = fopen(filename, mode); + return (*pf == NULL) ? pushresult(L, 0, filename) : 1; +} + + +/* +** this function has a separated environment, which defines the +** correct __close for 'popen' files +*/ +static int io_popen (lua_State *L) { + const char *filename = luaL_checkstring(L, 1); + const char *mode = luaL_optstring(L, 2, "r"); + FILE **pf = newfile(L); + *pf = lua_popen(L, filename, mode); + return (*pf == NULL) ? pushresult(L, 0, filename) : 1; +} + + +static int io_tmpfile (lua_State *L) { + FILE **pf = newfile(L); + *pf = tmpfile(); + return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; +} + + +static FILE *getiofile (lua_State *L, int findex) { + FILE *f; + lua_rawgeti(L, LUA_ENVIRONINDEX, findex); + f = *(FILE **)lua_touserdata(L, -1); + if (f == NULL) + luaL_error(L, "standard %s file is closed", fnames[findex - 1]); + return f; +} + + +static int g_iofile (lua_State *L, int f, const char *mode) { + if (!lua_isnoneornil(L, 1)) { + const char *filename = lua_tostring(L, 1); + if (filename) { + FILE **pf = newfile(L); + *pf = fopen(filename, mode); + if (*pf == NULL) + fileerror(L, 1, filename); + } + else { + tofile(L); /* check that it's a valid file handle */ + lua_pushvalue(L, 1); + } + lua_rawseti(L, LUA_ENVIRONINDEX, f); + } + /* return current value */ + lua_rawgeti(L, LUA_ENVIRONINDEX, f); + return 1; +} + + +static int io_input (lua_State *L) { + return g_iofile(L, IO_INPUT, "r"); +} + + +static int io_output (lua_State *L) { + return g_iofile(L, IO_OUTPUT, "w"); +} + + +static int io_readline (lua_State *L); + + +static void aux_lines (lua_State *L, int idx, int toclose) { + lua_pushvalue(L, idx); + lua_pushboolean(L, toclose); /* close/not close file when finished */ + lua_pushcclosure(L, io_readline, 2); +} + + +static int f_lines (lua_State *L) { + tofile(L); /* check that it's a valid file handle */ + aux_lines(L, 1, 0); + return 1; +} + + +static int io_lines (lua_State *L) { + if (lua_isnoneornil(L, 1)) { /* no arguments? */ + /* will iterate over default input */ + lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); + return f_lines(L); + } + else { + const char *filename = luaL_checkstring(L, 1); + FILE **pf = newfile(L); + *pf = fopen(filename, "r"); + if (*pf == NULL) + fileerror(L, 1, filename); + aux_lines(L, lua_gettop(L), 1); + return 1; + } +} + + +/* +** {====================================================== +** READ +** ======================================================= +*/ + + +static int read_number (lua_State *L, FILE *f) { + lua_Number d; + if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { + lua_pushnumber(L, d); + return 1; + } + else return 0; /* read fails */ +} + + +static int test_eof (lua_State *L, FILE *f) { + int c = getc(f); + ungetc(c, f); + lua_pushlstring(L, NULL, 0); + return (c != EOF); +} + + +static int read_line (lua_State *L, FILE *f) { + luaL_Buffer b; + luaL_buffinit(L, &b); + for (;;) { + size_t l; + char *p = luaL_prepbuffer(&b); + if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ + luaL_pushresult(&b); /* close buffer */ + return (lua_objlen(L, -1) > 0); /* check whether read something */ + } + l = strlen(p); + if (l == 0 || p[l-1] != '\n') + luaL_addsize(&b, l); + else { + luaL_addsize(&b, l - 1); /* do not include `eol' */ + luaL_pushresult(&b); /* close buffer */ + return 1; /* read at least an `eol' */ + } + } +} + + +static int read_chars (lua_State *L, FILE *f, size_t n) { + size_t rlen; /* how much to read */ + size_t nr; /* number of chars actually read */ + luaL_Buffer b; + luaL_buffinit(L, &b); + rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ + do { + char *p = luaL_prepbuffer(&b); + if (rlen > n) rlen = n; /* cannot read more than asked */ + nr = fread(p, sizeof(char), rlen, f); + luaL_addsize(&b, nr); + n -= nr; /* still have to read `n' chars */ + } while (n > 0 && nr == rlen); /* until end of count or eof */ + luaL_pushresult(&b); /* close buffer */ + return (n == 0 || lua_objlen(L, -1) > 0); +} + + +static int g_read (lua_State *L, FILE *f, int first) { + int nargs = lua_gettop(L) - 1; + int success; + int n; + clearerr(f); + if (nargs == 0) { /* no arguments? */ + success = read_line(L, f); + n = first+1; /* to return 1 result */ + } + else { /* ensure stack space for all results and for auxlib's buffer */ + luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); + success = 1; + for (n = first; nargs-- && success; n++) { + if (lua_type(L, n) == LUA_TNUMBER) { + size_t l = (size_t)lua_tointeger(L, n); + success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); + } + else { + const char *p = lua_tostring(L, n); + luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); + switch (p[1]) { + case 'n': /* number */ + success = read_number(L, f); + break; + case 'l': /* line */ + success = read_line(L, f); + break; + case 'a': /* file */ + read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ + success = 1; /* always success */ + break; + default: + return luaL_argerror(L, n, "invalid format"); + } + } + } + } + if (ferror(f)) + return pushresult(L, 0, NULL); + if (!success) { + lua_pop(L, 1); /* remove last result */ + lua_pushnil(L); /* push nil instead */ + } + return n - first; +} + + +static int io_read (lua_State *L) { + return g_read(L, getiofile(L, IO_INPUT), 1); +} + + +static int f_read (lua_State *L) { + return g_read(L, tofile(L), 2); +} + + +static int io_readline (lua_State *L) { + FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); + int sucess; + if (f == NULL) /* file is already closed? */ + luaL_error(L, "file is already closed"); + sucess = read_line(L, f); + if (ferror(f)) + return luaL_error(L, "%s", strerror(errno)); + if (sucess) return 1; + else { /* EOF */ + if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ + lua_settop(L, 0); + lua_pushvalue(L, lua_upvalueindex(1)); + aux_close(L); /* close it */ + } + return 0; + } +} + +/* }====================================================== */ + + +static int g_write (lua_State *L, FILE *f, int arg) { + int nargs = lua_gettop(L) - 1; + int status = 1; + for (; nargs--; arg++) { + if (lua_type(L, arg) == LUA_TNUMBER) { + /* optimization: could be done exactly as for strings */ + status = status && + fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; + } + else { + size_t l; + const char *s = luaL_checklstring(L, arg, &l); + status = status && (fwrite(s, sizeof(char), l, f) == l); + } + } + return pushresult(L, status, NULL); +} + + +static int io_write (lua_State *L) { + return g_write(L, getiofile(L, IO_OUTPUT), 1); +} + + +static int f_write (lua_State *L) { + return g_write(L, tofile(L), 2); +} + + +static int f_seek (lua_State *L) { + static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; + static const char *const modenames[] = {"set", "cur", "end", NULL}; + FILE *f = tofile(L); + int op = luaL_checkoption(L, 2, "cur", modenames); + long offset = luaL_optlong(L, 3, 0); + op = fseek(f, offset, mode[op]); + if (op) + return pushresult(L, 0, NULL); /* error */ + else { + lua_pushinteger(L, ftell(f)); + return 1; + } +} + + +static int f_setvbuf (lua_State *L) { + static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; + static const char *const modenames[] = {"no", "full", "line", NULL}; + FILE *f = tofile(L); + int op = luaL_checkoption(L, 2, NULL, modenames); + lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); + int res = setvbuf(f, NULL, mode[op], sz); + return pushresult(L, res == 0, NULL); +} + + + +static int io_flush (lua_State *L) { + return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); +} + + +static int f_flush (lua_State *L) { + return pushresult(L, fflush(tofile(L)) == 0, NULL); +} + + +static const luaL_Reg iolib[] = { + {"close", io_close}, + {"flush", io_flush}, + {"input", io_input}, + {"lines", io_lines}, + {"open", io_open}, + {"output", io_output}, + {"popen", io_popen}, + {"read", io_read}, + {"tmpfile", io_tmpfile}, + {"type", io_type}, + {"write", io_write}, + {NULL, NULL} +}; + + +static const luaL_Reg flib[] = { + {"close", io_close}, + {"flush", f_flush}, + {"lines", f_lines}, + {"read", f_read}, + {"seek", f_seek}, + {"setvbuf", f_setvbuf}, + {"write", f_write}, + {"__gc", io_gc}, + {"__tostring", io_tostring}, + {NULL, NULL} +}; + + +static void createmeta (lua_State *L) { + luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ + lua_pushvalue(L, -1); /* push metatable */ + lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ + luaL_register(L, NULL, flib); /* file methods */ +} + + +static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { + *newfile(L) = f; + if (k > 0) { + lua_pushvalue(L, -1); + lua_rawseti(L, LUA_ENVIRONINDEX, k); + } + lua_pushvalue(L, -2); /* copy environment */ + lua_setfenv(L, -2); /* set it */ + lua_setfield(L, -3, fname); +} + + +static void newfenv (lua_State *L, lua_CFunction cls) { + lua_createtable(L, 0, 1); + lua_pushcfunction(L, cls); + lua_setfield(L, -2, "__close"); +} + + +LUALIB_API int luaopen_io (lua_State *L) { + createmeta(L); + /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ + newfenv(L, io_fclose); + lua_replace(L, LUA_ENVIRONINDEX); + /* open library */ + luaL_register(L, LUA_IOLIBNAME, iolib); + /* create (and set) default files */ + newfenv(L, io_noclose); /* close function for default files */ + createstdfile(L, stdin, IO_INPUT, "stdin"); + createstdfile(L, stdout, IO_OUTPUT, "stdout"); + createstdfile(L, stderr, 0, "stderr"); + lua_pop(L, 1); /* pop environment for default files */ + lua_getfield(L, -1, "popen"); + newfenv(L, io_pclose); /* create environment for 'popen' */ + lua_setfenv(L, -2); /* set fenv for 'popen' */ + lua_pop(L, 1); /* pop 'popen' */ + return 1; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/llex.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/llex.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,461 @@ +/* +** $Id: llex.c,v 2.20.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lexical Analyzer +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#define llex_c +#define LUA_CORE + +#include "lua.h" + +#include "ldo.h" +#include "llex.h" +#include "lobject.h" +#include "lparser.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "lzio.h" + + + +#define next(ls) (ls->current = zgetc(ls->z)) + + + + +#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') + + +/* ORDER RESERVED */ +const char *const luaX_tokens [] = { + "and", "break", "do", "else", "elseif", + "end", "false", "for", "function", "if", + "in", "local", "nil", "not", "or", "repeat", + "return", "then", "true", "until", "while", + "..", "...", "==", ">=", "<=", "~=", + "", "", "", "", + NULL +}; + + +#define save_and_next(ls) (save(ls, ls->current), next(ls)) + + +static void save (LexState *ls, int c) { + Mbuffer *b = ls->buff; + if (b->n + 1 > b->buffsize) { + size_t newsize; + if (b->buffsize >= MAX_SIZET/2) + luaX_lexerror(ls, "lexical element too long", 0); + newsize = b->buffsize * 2; + luaZ_resizebuffer(ls->L, b, newsize); + } + b->buffer[b->n++] = cast(char, c); +} + + +void luaX_init (lua_State *L) { + int i; + for (i=0; itsv.reserved = cast_byte(i+1); /* reserved word */ + } +} + + +#define MAXSRC 80 + + +const char *luaX_token2str (LexState *ls, int token) { + if (token < FIRST_RESERVED) { + lua_assert(token == cast(unsigned char, token)); + return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) : + luaO_pushfstring(ls->L, "%c", token); + } + else + return luaX_tokens[token-FIRST_RESERVED]; +} + + +static const char *txtToken (LexState *ls, int token) { + switch (token) { + case TK_NAME: + case TK_STRING: + case TK_NUMBER: + save(ls, '\0'); + return luaZ_buffer(ls->buff); + default: + return luaX_token2str(ls, token); + } +} + + +void luaX_lexerror (LexState *ls, const char *msg, int token) { + char buff[MAXSRC]; + luaO_chunkid(buff, getstr(ls->source), MAXSRC); + msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); + if (token) + luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token)); + luaD_throw(ls->L, LUA_ERRSYNTAX); +} + + +void luaX_syntaxerror (LexState *ls, const char *msg) { + luaX_lexerror(ls, msg, ls->t.token); +} + + +TString *luaX_newstring (LexState *ls, const char *str, size_t l) { + lua_State *L = ls->L; + TString *ts = luaS_newlstr(L, str, l); + TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */ + if (ttisnil(o)) + setbvalue(o, 1); /* make sure `str' will not be collected */ + return ts; +} + + +static void inclinenumber (LexState *ls) { + int old = ls->current; + lua_assert(currIsNewline(ls)); + next(ls); /* skip `\n' or `\r' */ + if (currIsNewline(ls) && ls->current != old) + next(ls); /* skip `\n\r' or `\r\n' */ + if (++ls->linenumber >= MAX_INT) + luaX_syntaxerror(ls, "chunk has too many lines"); +} + + +void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { + ls->decpoint = '.'; + ls->L = L; + ls->lookahead.token = TK_EOS; /* no look-ahead token */ + ls->z = z; + ls->fs = NULL; + ls->linenumber = 1; + ls->lastline = 1; + ls->source = source; + luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ + next(ls); /* read first char */ +} + + + +/* +** ======================================================= +** LEXICAL ANALYZER +** ======================================================= +*/ + + + +static int check_next (LexState *ls, const char *set) { + if (!strchr(set, ls->current)) + return 0; + save_and_next(ls); + return 1; +} + + +static void buffreplace (LexState *ls, char from, char to) { + size_t n = luaZ_bufflen(ls->buff); + char *p = luaZ_buffer(ls->buff); + while (n--) + if (p[n] == from) p[n] = to; +} + + +static void trydecpoint (LexState *ls, SemInfo *seminfo) { + /* format error: try to update decimal point separator */ + struct lconv *cv = localeconv(); + char old = ls->decpoint; + ls->decpoint = (cv ? cv->decimal_point[0] : '.'); + buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */ + if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) { + /* format error with correct decimal point: no more options */ + buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ + luaX_lexerror(ls, "malformed number", TK_NUMBER); + } +} + + +/* LUA_NUMBER */ +static void read_numeral (LexState *ls, SemInfo *seminfo) { + lua_assert(isdigit(ls->current)); + do { + save_and_next(ls); + } while (isdigit(ls->current) || ls->current == '.'); + if (check_next(ls, "Ee")) /* `E'? */ + check_next(ls, "+-"); /* optional exponent sign */ + while (isalnum(ls->current) || ls->current == '_') + save_and_next(ls); + save(ls, '\0'); + buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ + if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */ + trydecpoint(ls, seminfo); /* try to update decimal point separator */ +} + + +static int skip_sep (LexState *ls) { + int count = 0; + int s = ls->current; + lua_assert(s == '[' || s == ']'); + save_and_next(ls); + while (ls->current == '=') { + save_and_next(ls); + count++; + } + return (ls->current == s) ? count : (-count) - 1; +} + + +static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { + int cont = 0; + (void)(cont); /* avoid warnings when `cont' is not used */ + save_and_next(ls); /* skip 2nd `[' */ + if (currIsNewline(ls)) /* string starts with a newline? */ + inclinenumber(ls); /* skip it */ + for (;;) { + switch (ls->current) { + case EOZ: + luaX_lexerror(ls, (seminfo) ? "unfinished long string" : + "unfinished long comment", TK_EOS); + break; /* to avoid warnings */ +#if defined(LUA_COMPAT_LSTR) + case '[': { + if (skip_sep(ls) == sep) { + save_and_next(ls); /* skip 2nd `[' */ + cont++; +#if LUA_COMPAT_LSTR == 1 + if (sep == 0) + luaX_lexerror(ls, "nesting of [[...]] is deprecated", '['); +#endif + } + break; + } +#endif + case ']': { + if (skip_sep(ls) == sep) { + save_and_next(ls); /* skip 2nd `]' */ +#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2 + cont--; + if (sep == 0 && cont >= 0) break; +#endif + goto endloop; + } + break; + } + case '\n': + case '\r': { + save(ls, '\n'); + inclinenumber(ls); + if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ + break; + } + default: { + if (seminfo) save_and_next(ls); + else next(ls); + } + } + } endloop: + if (seminfo) + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), + luaZ_bufflen(ls->buff) - 2*(2 + sep)); +} + + +static void read_string (LexState *ls, int del, SemInfo *seminfo) { + save_and_next(ls); + while (ls->current != del) { + switch (ls->current) { + case EOZ: + luaX_lexerror(ls, "unfinished string", TK_EOS); + continue; /* to avoid warnings */ + case '\n': + case '\r': + luaX_lexerror(ls, "unfinished string", TK_STRING); + continue; /* to avoid warnings */ + case '\\': { + int c; + next(ls); /* do not save the `\' */ + switch (ls->current) { + case 'a': c = '\a'; break; + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'v': c = '\v'; break; + case '\n': /* go through */ + case '\r': save(ls, '\n'); inclinenumber(ls); continue; + case EOZ: continue; /* will raise an error next loop */ + default: { + if (!isdigit(ls->current)) + save_and_next(ls); /* handles \\, \", \', and \? */ + else { /* \xxx */ + int i = 0; + c = 0; + do { + c = 10*c + (ls->current-'0'); + next(ls); + } while (++i<3 && isdigit(ls->current)); + if (c > UCHAR_MAX) + luaX_lexerror(ls, "escape sequence too large", TK_STRING); + save(ls, c); + } + continue; + } + } + save(ls, c); + next(ls); + continue; + } + default: + save_and_next(ls); + } + } + save_and_next(ls); /* skip delimiter */ + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, + luaZ_bufflen(ls->buff) - 2); +} + + +static int llex (LexState *ls, SemInfo *seminfo) { + luaZ_resetbuffer(ls->buff); + for (;;) { + switch (ls->current) { + case '\n': + case '\r': { + inclinenumber(ls); + continue; + } + case '-': { + next(ls); + if (ls->current != '-') return '-'; + /* else is a comment */ + next(ls); + if (ls->current == '[') { + int sep = skip_sep(ls); + luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ + if (sep >= 0) { + read_long_string(ls, NULL, sep); /* long comment */ + luaZ_resetbuffer(ls->buff); + continue; + } + } + /* else short comment */ + while (!currIsNewline(ls) && ls->current != EOZ) + next(ls); + continue; + } + case '[': { + int sep = skip_sep(ls); + if (sep >= 0) { + read_long_string(ls, seminfo, sep); + return TK_STRING; + } + else if (sep == -1) return '['; + else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING); + } + case '=': { + next(ls); + if (ls->current != '=') return '='; + else { next(ls); return TK_EQ; } + } + case '<': { + next(ls); + if (ls->current != '=') return '<'; + else { next(ls); return TK_LE; } + } + case '>': { + next(ls); + if (ls->current != '=') return '>'; + else { next(ls); return TK_GE; } + } + case '~': { + next(ls); + if (ls->current != '=') return '~'; + else { next(ls); return TK_NE; } + } + case '"': + case '\'': { + read_string(ls, ls->current, seminfo); + return TK_STRING; + } + case '.': { + save_and_next(ls); + if (check_next(ls, ".")) { + if (check_next(ls, ".")) + return TK_DOTS; /* ... */ + else return TK_CONCAT; /* .. */ + } + else if (!isdigit(ls->current)) return '.'; + else { + read_numeral(ls, seminfo); + return TK_NUMBER; + } + } + case EOZ: { + return TK_EOS; + } + default: { + if (isspace(ls->current)) { + lua_assert(!currIsNewline(ls)); + next(ls); + continue; + } + else if (isdigit(ls->current)) { + read_numeral(ls, seminfo); + return TK_NUMBER; + } + else if (isalpha(ls->current) || ls->current == '_') { + /* identifier or reserved word */ + TString *ts; + do { + save_and_next(ls); + } while (isalnum(ls->current) || ls->current == '_'); + ts = luaX_newstring(ls, luaZ_buffer(ls->buff), + luaZ_bufflen(ls->buff)); + if (ts->tsv.reserved > 0) /* reserved word? */ + return ts->tsv.reserved - 1 + FIRST_RESERVED; + else { + seminfo->ts = ts; + return TK_NAME; + } + } + else { + int c = ls->current; + next(ls); + return c; /* single-char tokens (+ - / ...) */ + } + } + } + } +} + + +void luaX_next (LexState *ls) { + ls->lastline = ls->linenumber; + if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ + ls->t = ls->lookahead; /* use this one */ + ls->lookahead.token = TK_EOS; /* and discharge it */ + } + else + ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ +} + + +void luaX_lookahead (LexState *ls) { + lua_assert(ls->lookahead.token == TK_EOS); + ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/llex.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/llex.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,81 @@ +/* +** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lexical Analyzer +** See Copyright Notice in lua.h +*/ + +#ifndef llex_h +#define llex_h + +#include "lobject.h" +#include "lzio.h" + + +#define FIRST_RESERVED 257 + +/* maximum length of a reserved word */ +#define TOKEN_LEN (sizeof("function")/sizeof(char)) + + +/* +* WARNING: if you change the order of this enumeration, +* grep "ORDER RESERVED" +*/ +enum RESERVED { + /* terminal symbols denoted by reserved words */ + TK_AND = FIRST_RESERVED, TK_BREAK, + TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, + TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, + TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, + /* other terminal symbols */ + TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, + TK_NAME, TK_STRING, TK_EOS +}; + +/* number of reserved words */ +#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) + + +/* array with token `names' */ +LUAI_DATA const char *const luaX_tokens []; + + +typedef union { + lua_Number r; + TString *ts; +} SemInfo; /* semantics information */ + + +typedef struct Token { + int token; + SemInfo seminfo; +} Token; + + +typedef struct LexState { + int current; /* current character (charint) */ + int linenumber; /* input line counter */ + int lastline; /* line of last token `consumed' */ + Token t; /* current token */ + Token lookahead; /* look ahead token */ + struct FuncState *fs; /* `FuncState' is private to the parser */ + struct lua_State *L; + ZIO *z; /* input stream */ + Mbuffer *buff; /* buffer for tokens */ + TString *source; /* current source name */ + char decpoint; /* locale decimal point */ +} LexState; + + +LUAI_FUNC void luaX_init (lua_State *L); +LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, + TString *source); +LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); +LUAI_FUNC void luaX_next (LexState *ls); +LUAI_FUNC void luaX_lookahead (LexState *ls); +LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); +LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); +LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/llimits.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/llimits.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,128 @@ +/* +** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ +** Limits, basic types, and some other `installation-dependent' definitions +** See Copyright Notice in lua.h +*/ + +#ifndef llimits_h +#define llimits_h + + +#include +#include + + +#include "lua.h" + + +typedef LUAI_UINT32 lu_int32; + +typedef LUAI_UMEM lu_mem; + +typedef LUAI_MEM l_mem; + + + +/* chars used as small naturals (so that `char' is reserved for characters) */ +typedef unsigned char lu_byte; + + +#define MAX_SIZET ((size_t)(~(size_t)0)-2) + +#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) + + +#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ + +/* +** conversion of pointer to integer +** this is for hashing only; there is no problem if the integer +** cannot hold the whole pointer value +*/ +#define IntPoint(p) ((unsigned int)(lu_mem)(p)) + + + +/* type to ensure maximum alignment */ +typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; + + +/* result of a `usual argument conversion' over lua_Number */ +typedef LUAI_UACNUMBER l_uacNumber; + + +/* internal assertions for in-house debugging */ +#ifdef lua_assert + +#define check_exp(c,e) (lua_assert(c), (e)) +#define api_check(l,e) lua_assert(e) + +#else + +#define lua_assert(c) ((void)0) +#define check_exp(c,e) (e) +#define api_check luai_apicheck + +#endif + + +#ifndef UNUSED +#define UNUSED(x) ((void)(x)) /* to avoid warnings */ +#endif + + +#ifndef cast +#define cast(t, exp) ((t)(exp)) +#endif + +#define cast_byte(i) cast(lu_byte, (i)) +#define cast_num(i) cast(lua_Number, (i)) +#define cast_int(i) cast(int, (i)) + + + +/* +** type for virtual-machine instructions +** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) +*/ +typedef lu_int32 Instruction; + + + +/* maximum stack for a Lua function */ +#define MAXSTACK 250 + + + +/* minimum size for the string table (must be power of 2) */ +#ifndef MINSTRTABSIZE +#define MINSTRTABSIZE 32 +#endif + + +/* minimum size for string buffer */ +#ifndef LUA_MINBUFFER +#define LUA_MINBUFFER 32 +#endif + + +#ifndef lua_lock +#define lua_lock(L) ((void) 0) +#define lua_unlock(L) ((void) 0) +#endif + +#ifndef luai_threadyield +#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} +#endif + + +/* +** macro to control inclusion of some hard tests on stack reallocation +*/ +#ifndef HARDSTACKTESTS +#define condhardstacktests(x) ((void)0) +#else +#define condhardstacktests(x) x +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lmathlib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lmathlib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,263 @@ +/* +** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ +** Standard mathematical library +** See Copyright Notice in lua.h +*/ + + +#include +#include + +#define lmathlib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +#undef PI +#define PI (3.14159265358979323846) +#define RADIANS_PER_DEGREE (PI/180.0) + + + +static int math_abs (lua_State *L) { + lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); + return 1; +} + +static int math_sin (lua_State *L) { + lua_pushnumber(L, sin(luaL_checknumber(L, 1))); + return 1; +} + +static int math_sinh (lua_State *L) { + lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); + return 1; +} + +static int math_cos (lua_State *L) { + lua_pushnumber(L, cos(luaL_checknumber(L, 1))); + return 1; +} + +static int math_cosh (lua_State *L) { + lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); + return 1; +} + +static int math_tan (lua_State *L) { + lua_pushnumber(L, tan(luaL_checknumber(L, 1))); + return 1; +} + +static int math_tanh (lua_State *L) { + lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); + return 1; +} + +static int math_asin (lua_State *L) { + lua_pushnumber(L, asin(luaL_checknumber(L, 1))); + return 1; +} + +static int math_acos (lua_State *L) { + lua_pushnumber(L, acos(luaL_checknumber(L, 1))); + return 1; +} + +static int math_atan (lua_State *L) { + lua_pushnumber(L, atan(luaL_checknumber(L, 1))); + return 1; +} + +static int math_atan2 (lua_State *L) { + lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +static int math_ceil (lua_State *L) { + lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); + return 1; +} + +static int math_floor (lua_State *L) { + lua_pushnumber(L, floor(luaL_checknumber(L, 1))); + return 1; +} + +static int math_fmod (lua_State *L) { + lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +static int math_modf (lua_State *L) { + double ip; + double fp = modf(luaL_checknumber(L, 1), &ip); + lua_pushnumber(L, ip); + lua_pushnumber(L, fp); + return 2; +} + +static int math_sqrt (lua_State *L) { + lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); + return 1; +} + +static int math_pow (lua_State *L) { + lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +static int math_log (lua_State *L) { + lua_pushnumber(L, log(luaL_checknumber(L, 1))); + return 1; +} + +static int math_log10 (lua_State *L) { + lua_pushnumber(L, log10(luaL_checknumber(L, 1))); + return 1; +} + +static int math_exp (lua_State *L) { + lua_pushnumber(L, exp(luaL_checknumber(L, 1))); + return 1; +} + +static int math_deg (lua_State *L) { + lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); + return 1; +} + +static int math_rad (lua_State *L) { + lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); + return 1; +} + +static int math_frexp (lua_State *L) { + int e; + lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); + lua_pushinteger(L, e); + return 2; +} + +static int math_ldexp (lua_State *L) { + lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); + return 1; +} + + + +static int math_min (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + lua_Number dmin = luaL_checknumber(L, 1); + int i; + for (i=2; i<=n; i++) { + lua_Number d = luaL_checknumber(L, i); + if (d < dmin) + dmin = d; + } + lua_pushnumber(L, dmin); + return 1; +} + + +static int math_max (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + lua_Number dmax = luaL_checknumber(L, 1); + int i; + for (i=2; i<=n; i++) { + lua_Number d = luaL_checknumber(L, i); + if (d > dmax) + dmax = d; + } + lua_pushnumber(L, dmax); + return 1; +} + + +static int math_random (lua_State *L) { + /* the `%' avoids the (rare) case of r==1, and is needed also because on + some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ + lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; + switch (lua_gettop(L)) { /* check number of arguments */ + case 0: { /* no arguments */ + lua_pushnumber(L, r); /* Number between 0 and 1 */ + break; + } + case 1: { /* only upper limit */ + int u = luaL_checkint(L, 1); + luaL_argcheck(L, 1<=u, 1, "interval is empty"); + lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ + break; + } + case 2: { /* lower and upper limits */ + int l = luaL_checkint(L, 1); + int u = luaL_checkint(L, 2); + luaL_argcheck(L, l<=u, 2, "interval is empty"); + lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ + break; + } + default: return luaL_error(L, "wrong number of arguments"); + } + return 1; +} + + +static int math_randomseed (lua_State *L) { + srand(luaL_checkint(L, 1)); + return 0; +} + + +static const luaL_Reg mathlib[] = { + {"abs", math_abs}, + {"acos", math_acos}, + {"asin", math_asin}, + {"atan2", math_atan2}, + {"atan", math_atan}, + {"ceil", math_ceil}, + {"cosh", math_cosh}, + {"cos", math_cos}, + {"deg", math_deg}, + {"exp", math_exp}, + {"floor", math_floor}, + {"fmod", math_fmod}, + {"frexp", math_frexp}, + {"ldexp", math_ldexp}, + {"log10", math_log10}, + {"log", math_log}, + {"max", math_max}, + {"min", math_min}, + {"modf", math_modf}, + {"pow", math_pow}, + {"rad", math_rad}, + {"random", math_random}, + {"randomseed", math_randomseed}, + {"sinh", math_sinh}, + {"sin", math_sin}, + {"sqrt", math_sqrt}, + {"tanh", math_tanh}, + {"tan", math_tan}, + {NULL, NULL} +}; + + +/* +** Open math library +*/ +LUALIB_API int luaopen_math (lua_State *L) { + luaL_register(L, LUA_MATHLIBNAME, mathlib); + lua_pushnumber(L, PI); + lua_setfield(L, -2, "pi"); + lua_pushnumber(L, HUGE_VAL); + lua_setfield(L, -2, "huge"); +#if defined(LUA_COMPAT_MOD) + lua_getfield(L, -1, "fmod"); + lua_setfield(L, -2, "mod"); +#endif + return 1; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lmem.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lmem.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,86 @@ +/* +** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ +** Interface to Memory Manager +** See Copyright Notice in lua.h +*/ + + +#include + +#define lmem_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" + + + +/* +** About the realloc function: +** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); +** (`osize' is the old size, `nsize' is the new size) +** +** Lua ensures that (ptr == NULL) iff (osize == 0). +** +** * frealloc(ud, NULL, 0, x) creates a new block of size `x' +** +** * frealloc(ud, p, x, 0) frees the block `p' +** (in this specific case, frealloc must return NULL). +** particularly, frealloc(ud, NULL, 0, 0) does nothing +** (which is equivalent to free(NULL) in ANSI C) +** +** frealloc returns NULL if it cannot create or reallocate the area +** (any reallocation to an equal or smaller size cannot fail!) +*/ + + + +#define MINSIZEARRAY 4 + + +void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, + int limit, const char *errormsg) { + void *newblock; + int newsize; + if (*size >= limit/2) { /* cannot double it? */ + if (*size >= limit) /* cannot grow even a little? */ + luaG_runerror(L, errormsg); + newsize = limit; /* still have at least one free place */ + } + else { + newsize = (*size)*2; + if (newsize < MINSIZEARRAY) + newsize = MINSIZEARRAY; /* minimum size */ + } + newblock = luaM_reallocv(L, block, *size, newsize, size_elems); + *size = newsize; /* update only when everything else is OK */ + return newblock; +} + + +void *luaM_toobig (lua_State *L) { + luaG_runerror(L, "memory allocation error: block too big"); + return NULL; /* to avoid warnings */ +} + + + +/* +** generic allocation routine. +*/ +void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { + global_State *g = G(L); + lua_assert((osize == 0) == (block == NULL)); + block = (*g->frealloc)(g->ud, block, osize, nsize); + if (block == NULL && nsize > 0) + luaD_throw(L, LUA_ERRMEM); + lua_assert((nsize == 0) == (block == NULL)); + g->totalbytes = (g->totalbytes - osize) + nsize; + return block; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lmem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lmem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,49 @@ +/* +** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ +** Interface to Memory Manager +** See Copyright Notice in lua.h +*/ + +#ifndef lmem_h +#define lmem_h + + +#include + +#include "llimits.h" +#include "lua.h" + +#define MEMERRMSG "not enough memory" + + +#define luaM_reallocv(L,b,on,n,e) \ + ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ + luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ + luaM_toobig(L)) + +#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) +#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) +#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) + +#define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) +#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) +#define luaM_newvector(L,n,t) \ + cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) + +#define luaM_growvector(L,v,nelems,size,t,limit,e) \ + if ((nelems)+1 > (size)) \ + ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) + +#define luaM_reallocvector(L, v,oldn,n,t) \ + ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) + + +LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, + size_t size); +LUAI_FUNC void *luaM_toobig (lua_State *L); +LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, + size_t size_elem, int limit, + const char *errormsg); + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/loadlib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/loadlib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,666 @@ +/* +** $Id: loadlib.c,v 1.52.1.3 2008/08/06 13:29:28 roberto Exp $ +** Dynamic library loader for Lua +** See Copyright Notice in lua.h +** +** This module contains an implementation of loadlib for Unix systems +** that have dlfcn, an implementation for Darwin (Mac OS X), an +** implementation for Windows, and a stub for other systems. +*/ + + +#include +#include + + +#define loadlib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +/* prefix for open functions in C libraries */ +#define LUA_POF "luaopen_" + +/* separator for open functions in C libraries */ +#define LUA_OFSEP "_" + + +#define LIBPREFIX "LOADLIB: " + +#define POF LUA_POF +#define LIB_FAIL "open" + + +/* error codes for ll_loadfunc */ +#define ERRLIB 1 +#define ERRFUNC 2 + +#define setprogdir(L) ((void)0) + + +static void ll_unloadlib (void *lib); +static void *ll_load (lua_State *L, const char *path); +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); + + + +#if defined(LUA_DL_DLOPEN) +/* +** {======================================================================== +** This is an implementation of loadlib based on the dlfcn interface. +** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, +** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least +** as an emulation layer on top of native functions. +** ========================================================================= +*/ + +#include + +static void ll_unloadlib (void *lib) { + dlclose(lib); +} + + +static void *ll_load (lua_State *L, const char *path) { + void *lib = dlopen(path, RTLD_NOW); + if (lib == NULL) lua_pushstring(L, dlerror()); + return lib; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + lua_CFunction f = (lua_CFunction)dlsym(lib, sym); + if (f == NULL) lua_pushstring(L, dlerror()); + return f; +} + +/* }====================================================== */ + + + +#elif defined(LUA_DL_DLL) +/* +** {====================================================================== +** This is an implementation of loadlib for Windows using native functions. +** ======================================================================= +*/ + +#include + + +#undef setprogdir + +static void setprogdir (lua_State *L) { + char buff[MAX_PATH + 1]; + char *lb; + DWORD nsize = sizeof(buff)/sizeof(char); + DWORD n = GetModuleFileNameA(NULL, buff, nsize); + if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) + luaL_error(L, "unable to get ModuleFileName"); + else { + *lb = '\0'; + luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); + lua_remove(L, -2); /* remove original string */ + } +} + + +static void pusherror (lua_State *L) { + int error = GetLastError(); + char buffer[128]; + if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, error, 0, buffer, sizeof(buffer), NULL)) + lua_pushstring(L, buffer); + else + lua_pushfstring(L, "system error %d\n", error); +} + +static void ll_unloadlib (void *lib) { + FreeLibrary((HINSTANCE)lib); +} + + +static void *ll_load (lua_State *L, const char *path) { + HINSTANCE lib = LoadLibraryA(path); + if (lib == NULL) pusherror(L); + return lib; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); + if (f == NULL) pusherror(L); + return f; +} + +/* }====================================================== */ + + + +#elif defined(LUA_DL_DYLD) +/* +** {====================================================================== +** Native Mac OS X / Darwin Implementation +** ======================================================================= +*/ + +#include + + +/* Mac appends a `_' before C function names */ +#undef POF +#define POF "_" LUA_POF + + +static void pusherror (lua_State *L) { + const char *err_str; + const char *err_file; + NSLinkEditErrors err; + int err_num; + NSLinkEditError(&err, &err_num, &err_file, &err_str); + lua_pushstring(L, err_str); +} + + +static const char *errorfromcode (NSObjectFileImageReturnCode ret) { + switch (ret) { + case NSObjectFileImageInappropriateFile: + return "file is not a bundle"; + case NSObjectFileImageArch: + return "library is for wrong CPU type"; + case NSObjectFileImageFormat: + return "bad format"; + case NSObjectFileImageAccess: + return "cannot access file"; + case NSObjectFileImageFailure: + default: + return "unable to load library"; + } +} + + +static void ll_unloadlib (void *lib) { + NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); +} + + +static void *ll_load (lua_State *L, const char *path) { + NSObjectFileImage img; + NSObjectFileImageReturnCode ret; + /* this would be a rare case, but prevents crashing if it happens */ + if(!_dyld_present()) { + lua_pushliteral(L, "dyld not present"); + return NULL; + } + ret = NSCreateObjectFileImageFromFile(path, &img); + if (ret == NSObjectFileImageSuccess) { + NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | + NSLINKMODULE_OPTION_RETURN_ON_ERROR); + NSDestroyObjectFileImage(img); + if (mod == NULL) pusherror(L); + return mod; + } + lua_pushstring(L, errorfromcode(ret)); + return NULL; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym); + if (nss == NULL) { + lua_pushfstring(L, "symbol " LUA_QS " not found", sym); + return NULL; + } + return (lua_CFunction)NSAddressOfSymbol(nss); +} + +/* }====================================================== */ + + + +#else +/* +** {====================================================== +** Fallback for other systems +** ======================================================= +*/ + +#undef LIB_FAIL +#define LIB_FAIL "absent" + + +#define DLMSG "dynamic libraries not enabled; check your Lua installation" + + +static void ll_unloadlib (void *lib) { + (void)lib; /* to avoid warnings */ +} + + +static void *ll_load (lua_State *L, const char *path) { + (void)path; /* to avoid warnings */ + lua_pushliteral(L, DLMSG); + return NULL; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + (void)lib; (void)sym; /* to avoid warnings */ + lua_pushliteral(L, DLMSG); + return NULL; +} + +/* }====================================================== */ +#endif + + + +static void **ll_register (lua_State *L, const char *path) { + void **plib; + lua_pushfstring(L, "%s%s", LIBPREFIX, path); + lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ + if (!lua_isnil(L, -1)) /* is there an entry? */ + plib = (void **)lua_touserdata(L, -1); + else { /* no entry yet; create one */ + lua_pop(L, 1); + plib = (void **)lua_newuserdata(L, sizeof(const void *)); + *plib = NULL; + luaL_getmetatable(L, "_LOADLIB"); + lua_setmetatable(L, -2); + lua_pushfstring(L, "%s%s", LIBPREFIX, path); + lua_pushvalue(L, -2); + lua_settable(L, LUA_REGISTRYINDEX); + } + return plib; +} + + +/* +** __gc tag method: calls library's `ll_unloadlib' function with the lib +** handle +*/ +static int gctm (lua_State *L) { + void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); + if (*lib) ll_unloadlib(*lib); + *lib = NULL; /* mark library as closed */ + return 0; +} + + +static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { + void **reg = ll_register(L, path); + if (*reg == NULL) *reg = ll_load(L, path); + if (*reg == NULL) + return ERRLIB; /* unable to load library */ + else { + lua_CFunction f = ll_sym(L, *reg, sym); + if (f == NULL) + return ERRFUNC; /* unable to find function */ + lua_pushcfunction(L, f); + return 0; /* return function */ + } +} + + +static int ll_loadlib (lua_State *L) { + const char *path = luaL_checkstring(L, 1); + const char *init = luaL_checkstring(L, 2); + int stat = ll_loadfunc(L, path, init); + if (stat == 0) /* no errors? */ + return 1; /* return the loaded function */ + else { /* error; error message is on stack top */ + lua_pushnil(L); + lua_insert(L, -2); + lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); + return 3; /* return nil, error message, and where */ + } +} + + + +/* +** {====================================================== +** 'require' function +** ======================================================= +*/ + + +static int readable (const char *filename) { + FILE *f = fopen(filename, "r"); /* try to open file */ + if (f == NULL) return 0; /* open failed */ + fclose(f); + return 1; +} + + +static const char *pushnexttemplate (lua_State *L, const char *path) { + const char *l; + while (*path == *LUA_PATHSEP) path++; /* skip separators */ + if (*path == '\0') return NULL; /* no more templates */ + l = strchr(path, *LUA_PATHSEP); /* find next separator */ + if (l == NULL) l = path + strlen(path); + lua_pushlstring(L, path, l - path); /* template */ + return l; +} + + +static const char *findfile (lua_State *L, const char *name, + const char *pname) { + const char *path; + name = luaL_gsub(L, name, ".", LUA_DIRSEP); + lua_getfield(L, LUA_ENVIRONINDEX, pname); + path = lua_tostring(L, -1); + if (path == NULL) + luaL_error(L, LUA_QL("package.%s") " must be a string", pname); + lua_pushliteral(L, ""); /* error accumulator */ + while ((path = pushnexttemplate(L, path)) != NULL) { + const char *filename; + filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); + lua_remove(L, -2); /* remove path template */ + if (readable(filename)) /* does file exist and is readable? */ + return filename; /* return that file name */ + lua_pushfstring(L, "\n\tno file " LUA_QS, filename); + lua_remove(L, -2); /* remove file name */ + lua_concat(L, 2); /* add entry to possible error message */ + } + return NULL; /* not found */ +} + + +static void loaderror (lua_State *L, const char *filename) { + luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", + lua_tostring(L, 1), filename, lua_tostring(L, -1)); +} + + +static int loader_Lua (lua_State *L) { + const char *filename; + const char *name = luaL_checkstring(L, 1); + filename = findfile(L, name, "path"); + if (filename == NULL) return 1; /* library not found in this path */ + if (luaL_loadfile(L, filename) != 0) + loaderror(L, filename); + return 1; /* library loaded successfully */ +} + + +static const char *mkfuncname (lua_State *L, const char *modname) { + const char *funcname; + const char *mark = strchr(modname, *LUA_IGMARK); + if (mark) modname = mark + 1; + funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); + funcname = lua_pushfstring(L, POF"%s", funcname); + lua_remove(L, -2); /* remove 'gsub' result */ + return funcname; +} + + +static int loader_C (lua_State *L) { + const char *funcname; + const char *name = luaL_checkstring(L, 1); + const char *filename = findfile(L, name, "cpath"); + if (filename == NULL) return 1; /* library not found in this path */ + funcname = mkfuncname(L, name); + if (ll_loadfunc(L, filename, funcname) != 0) + loaderror(L, filename); + return 1; /* library loaded successfully */ +} + + +static int loader_Croot (lua_State *L) { + const char *funcname; + const char *filename; + const char *name = luaL_checkstring(L, 1); + const char *p = strchr(name, '.'); + int stat; + if (p == NULL) return 0; /* is root */ + lua_pushlstring(L, name, p - name); + filename = findfile(L, lua_tostring(L, -1), "cpath"); + if (filename == NULL) return 1; /* root not found */ + funcname = mkfuncname(L, name); + if ((stat = ll_loadfunc(L, filename, funcname)) != 0) { + if (stat != ERRFUNC) loaderror(L, filename); /* real error */ + lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, + name, filename); + return 1; /* function not found */ + } + return 1; +} + + +static int loader_preload (lua_State *L) { + const char *name = luaL_checkstring(L, 1); + lua_getfield(L, LUA_ENVIRONINDEX, "preload"); + if (!lua_istable(L, -1)) + luaL_error(L, LUA_QL("package.preload") " must be a table"); + lua_getfield(L, -1, name); + if (lua_isnil(L, -1)) /* not found? */ + lua_pushfstring(L, "\n\tno field package.preload['%s']", name); + return 1; +} + + +static const int sentinel_ = 0; +#define sentinel ((void *)&sentinel_) + + +static int ll_require (lua_State *L) { + const char *name = luaL_checkstring(L, 1); + int i; + lua_settop(L, 1); /* _LOADED table will be at index 2 */ + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, 2, name); + if (lua_toboolean(L, -1)) { /* is it there? */ + if (lua_touserdata(L, -1) == sentinel) /* check loops */ + luaL_error(L, "loop or previous error loading module " LUA_QS, name); + return 1; /* package is already loaded */ + } + /* else must load it; iterate over available loaders */ + lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); + if (!lua_istable(L, -1)) + luaL_error(L, LUA_QL("package.loaders") " must be a table"); + lua_pushliteral(L, ""); /* error message accumulator */ + for (i=1; ; i++) { + lua_rawgeti(L, -2, i); /* get a loader */ + if (lua_isnil(L, -1)) + luaL_error(L, "module " LUA_QS " not found:%s", + name, lua_tostring(L, -2)); + lua_pushstring(L, name); + lua_call(L, 1, 1); /* call it */ + if (lua_isfunction(L, -1)) /* did it find module? */ + break; /* module loaded successfully */ + else if (lua_isstring(L, -1)) /* loader returned error message? */ + lua_concat(L, 2); /* accumulate it */ + else + lua_pop(L, 1); + } + lua_pushlightuserdata(L, sentinel); + lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ + lua_pushstring(L, name); /* pass name as argument to module */ + lua_call(L, 1, 1); /* run loaded module */ + if (!lua_isnil(L, -1)) /* non-nil return? */ + lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ + lua_getfield(L, 2, name); + if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ + lua_pushboolean(L, 1); /* use true as result */ + lua_pushvalue(L, -1); /* extra copy to be returned */ + lua_setfield(L, 2, name); /* _LOADED[name] = true */ + } + return 1; +} + +/* }====================================================== */ + + + +/* +** {====================================================== +** 'module' function +** ======================================================= +*/ + + +static void setfenv (lua_State *L) { + lua_Debug ar; + if (lua_getstack(L, 1, &ar) == 0 || + lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ + lua_iscfunction(L, -1)) + luaL_error(L, LUA_QL("module") " not called from a Lua function"); + lua_pushvalue(L, -2); + lua_setfenv(L, -2); + lua_pop(L, 1); +} + + +static void dooptions (lua_State *L, int n) { + int i; + for (i = 2; i <= n; i++) { + lua_pushvalue(L, i); /* get option (a function) */ + lua_pushvalue(L, -2); /* module */ + lua_call(L, 1, 0); + } +} + + +static void modinit (lua_State *L, const char *modname) { + const char *dot; + lua_pushvalue(L, -1); + lua_setfield(L, -2, "_M"); /* module._M = module */ + lua_pushstring(L, modname); + lua_setfield(L, -2, "_NAME"); + dot = strrchr(modname, '.'); /* look for last dot in module name */ + if (dot == NULL) dot = modname; + else dot++; + /* set _PACKAGE as package name (full module name minus last part) */ + lua_pushlstring(L, modname, dot - modname); + lua_setfield(L, -2, "_PACKAGE"); +} + + +static int ll_module (lua_State *L) { + const char *modname = luaL_checkstring(L, 1); + int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ + if (!lua_istable(L, -1)) { /* not found? */ + lua_pop(L, 1); /* remove previous result */ + /* try global variable (and create one if it does not exist) */ + if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) + return luaL_error(L, "name conflict for module " LUA_QS, modname); + lua_pushvalue(L, -1); + lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ + } + /* check whether table already has a _NAME field */ + lua_getfield(L, -1, "_NAME"); + if (!lua_isnil(L, -1)) /* is table an initialized module? */ + lua_pop(L, 1); + else { /* no; initialize it */ + lua_pop(L, 1); + modinit(L, modname); + } + lua_pushvalue(L, -1); + setfenv(L); + dooptions(L, loaded - 1); + return 0; +} + + +static int ll_seeall (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + if (!lua_getmetatable(L, 1)) { + lua_createtable(L, 0, 1); /* create new metatable */ + lua_pushvalue(L, -1); + lua_setmetatable(L, 1); + } + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_setfield(L, -2, "__index"); /* mt.__index = _G */ + return 0; +} + + +/* }====================================================== */ + + + +/* auxiliary mark (for internal use) */ +#define AUXMARK "\1" + +static void setpath (lua_State *L, const char *fieldname, const char *envname, + const char *def) { + const char *path = getenv(envname); + if (path == NULL) /* no environment variable? */ + lua_pushstring(L, def); /* use default */ + else { + /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ + path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, + LUA_PATHSEP AUXMARK LUA_PATHSEP); + luaL_gsub(L, path, AUXMARK, def); + lua_remove(L, -2); + } + setprogdir(L); + lua_setfield(L, -2, fieldname); +} + + +static const luaL_Reg pk_funcs[] = { + {"loadlib", ll_loadlib}, + {"seeall", ll_seeall}, + {NULL, NULL} +}; + + +static const luaL_Reg ll_funcs[] = { + {"module", ll_module}, + {"require", ll_require}, + {NULL, NULL} +}; + + +static const lua_CFunction loaders[] = + {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; + + +LUALIB_API int luaopen_package (lua_State *L) { + int i; + /* create new type _LOADLIB */ + luaL_newmetatable(L, "_LOADLIB"); + lua_pushcfunction(L, gctm); + lua_setfield(L, -2, "__gc"); + /* create `package' table */ + luaL_register(L, LUA_LOADLIBNAME, pk_funcs); +#if defined(LUA_COMPAT_LOADLIB) + lua_getfield(L, -1, "loadlib"); + lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); +#endif + lua_pushvalue(L, -1); + lua_replace(L, LUA_ENVIRONINDEX); + /* create `loaders' table */ + lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1); + /* fill it with pre-defined loaders */ + for (i=0; loaders[i] != NULL; i++) { + lua_pushcfunction(L, loaders[i]); + lua_rawseti(L, -2, i+1); + } + lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ + setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ + setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ + /* store config information */ + lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" + LUA_EXECDIR "\n" LUA_IGMARK); + lua_setfield(L, -2, "config"); + /* set field `loaded' */ + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); + lua_setfield(L, -2, "loaded"); + /* set field `preload' */ + lua_newtable(L); + lua_setfield(L, -2, "preload"); + lua_pushvalue(L, LUA_GLOBALSINDEX); + luaL_register(L, NULL, ll_funcs); /* open lib into global table */ + lua_pop(L, 1); + return 1; /* return 'package' table */ +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lobject.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lobject.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,214 @@ +/* +** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ +** Some generic functions over Lua objects +** See Copyright Notice in lua.h +*/ + +#include +#include +#include +#include +#include + +#define lobject_c +#define LUA_CORE + +#include "lua.h" + +#include "ldo.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" +#include "lvm.h" + + + +const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL}; + + +/* +** converts an integer to a "floating point byte", represented as +** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if +** eeeee != 0 and (xxx) otherwise. +*/ +int luaO_int2fb (unsigned int x) { + int e = 0; /* expoent */ + while (x >= 16) { + x = (x+1) >> 1; + e++; + } + if (x < 8) return x; + else return ((e+1) << 3) | (cast_int(x) - 8); +} + + +/* converts back */ +int luaO_fb2int (int x) { + int e = (x >> 3) & 31; + if (e == 0) return x; + else return ((x & 7)+8) << (e - 1); +} + + +int luaO_log2 (unsigned int x) { + static const lu_byte log_2[256] = { + 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 + }; + int l = -1; + while (x >= 256) { l += 8; x >>= 8; } + return l + log_2[x]; + +} + + +int luaO_rawequalObj (const TValue *t1, const TValue *t2) { + if (ttype(t1) != ttype(t2)) return 0; + else switch (ttype(t1)) { + case LUA_TNIL: + return 1; + case LUA_TNUMBER: + return luai_numeq(nvalue(t1), nvalue(t2)); + case LUA_TBOOLEAN: + return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ + case LUA_TLIGHTUSERDATA: + return pvalue(t1) == pvalue(t2); + default: + lua_assert(iscollectable(t1)); + return gcvalue(t1) == gcvalue(t2); + } +} + + +int luaO_str2d (const char *s, lua_Number *result) { + char *endptr; + *result = lua_str2number(s, &endptr); + if (endptr == s) return 0; /* conversion failed */ + if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ + *result = cast_num(strtoul(s, &endptr, 16)); + if (*endptr == '\0') return 1; /* most common case */ + while (isspace(cast(unsigned char, *endptr))) endptr++; + if (*endptr != '\0') return 0; /* invalid trailing characters? */ + return 1; +} + + + +static void pushstr (lua_State *L, const char *str) { + setsvalue2s(L, L->top, luaS_new(L, str)); + incr_top(L); +} + + +/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ +const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { + int n = 1; + pushstr(L, ""); + for (;;) { + const char *e = strchr(fmt, '%'); + if (e == NULL) break; + setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); + incr_top(L); + switch (*(e+1)) { + case 's': { + const char *s = va_arg(argp, char *); + if (s == NULL) s = "(null)"; + pushstr(L, s); + break; + } + case 'c': { + char buff[2]; + buff[0] = cast(char, va_arg(argp, int)); + buff[1] = '\0'; + pushstr(L, buff); + break; + } + case 'd': { + setnvalue(L->top, cast_num(va_arg(argp, int))); + incr_top(L); + break; + } + case 'f': { + setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); + incr_top(L); + break; + } + case 'p': { + char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ + sprintf(buff, "%p", va_arg(argp, void *)); + pushstr(L, buff); + break; + } + case '%': { + pushstr(L, "%"); + break; + } + default: { + char buff[3]; + buff[0] = '%'; + buff[1] = *(e+1); + buff[2] = '\0'; + pushstr(L, buff); + break; + } + } + n += 2; + fmt = e+2; + } + pushstr(L, fmt); + luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); + L->top -= n; + return svalue(L->top - 1); +} + + +const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { + const char *msg; + va_list argp; + va_start(argp, fmt); + msg = luaO_pushvfstring(L, fmt, argp); + va_end(argp); + return msg; +} + + +void luaO_chunkid (char *out, const char *source, size_t bufflen) { + if (*source == '=') { + strncpy(out, source+1, bufflen); /* remove first char */ + out[bufflen-1] = '\0'; /* ensures null termination */ + } + else { /* out = "source", or "...source" */ + if (*source == '@') { + size_t l; + source++; /* skip the `@' */ + bufflen -= sizeof(" '...' "); + l = strlen(source); + strcpy(out, ""); + if (l > bufflen) { + source += (l-bufflen); /* get last part of file name */ + strcat(out, "..."); + } + strcat(out, source); + } + else { /* out = [string "string"] */ + size_t len = strcspn(source, "\n\r"); /* stop at first newline */ + bufflen -= sizeof(" [string \"...\"] "); + if (len > bufflen) len = bufflen; + strcpy(out, "[string \""); + if (source[len] != '\0') { /* must truncate? */ + strncat(out, source, len); + strcat(out, "..."); + } + else + strcat(out, source); + strcat(out, "\"]"); + } + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lobject.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lobject.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,381 @@ +/* +** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $ +** Type definitions for Lua objects +** See Copyright Notice in lua.h +*/ + + +#ifndef lobject_h +#define lobject_h + + +#include + + +#include "llimits.h" +#include "lua.h" + + +/* tags for values visible from Lua */ +#define LAST_TAG LUA_TTHREAD + +#define NUM_TAGS (LAST_TAG+1) + + +/* +** Extra tags for non-values +*/ +#define LUA_TPROTO (LAST_TAG+1) +#define LUA_TUPVAL (LAST_TAG+2) +#define LUA_TDEADKEY (LAST_TAG+3) + + +/* +** Union of all collectable objects +*/ +typedef union GCObject GCObject; + + +/* +** Common Header for all collectable objects (in macro form, to be +** included in other objects) +*/ +#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked + + +/* +** Common header in struct form +*/ +typedef struct GCheader { + CommonHeader; +} GCheader; + + + + +/* +** Union of all Lua values +*/ +typedef union { + GCObject *gc; + void *p; + lua_Number n; + int b; +} Value; + + +/* +** Tagged Values +*/ + +#define TValuefields Value value; int tt + +typedef struct lua_TValue { + TValuefields; +} TValue; + + +/* Macros to test type */ +#define ttisnil(o) (ttype(o) == LUA_TNIL) +#define ttisnumber(o) (ttype(o) == LUA_TNUMBER) +#define ttisstring(o) (ttype(o) == LUA_TSTRING) +#define ttistable(o) (ttype(o) == LUA_TTABLE) +#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) +#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) +#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) +#define ttisthread(o) (ttype(o) == LUA_TTHREAD) +#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) + +/* Macros to access values */ +#define ttype(o) ((o)->tt) +#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) +#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) +#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) +#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) +#define tsvalue(o) (&rawtsvalue(o)->tsv) +#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) +#define uvalue(o) (&rawuvalue(o)->uv) +#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) +#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) +#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) +#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) + +#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) + +/* +** for internal debug only +*/ +#define checkconsistency(obj) \ + lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) + +#define checkliveness(g,obj) \ + lua_assert(!iscollectable(obj) || \ + ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) + + +/* Macros to set values */ +#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) + +#define setnvalue(obj,x) \ + { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } + +#define setpvalue(obj,x) \ + { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } + +#define setbvalue(obj,x) \ + { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } + +#define setsvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ + checkliveness(G(L),i_o); } + +#define setuvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ + checkliveness(G(L),i_o); } + +#define setthvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ + checkliveness(G(L),i_o); } + +#define setclvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ + checkliveness(G(L),i_o); } + +#define sethvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ + checkliveness(G(L),i_o); } + +#define setptvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ + checkliveness(G(L),i_o); } + + + + +#define setobj(L,obj1,obj2) \ + { const TValue *o2=(obj2); TValue *o1=(obj1); \ + o1->value = o2->value; o1->tt=o2->tt; \ + checkliveness(G(L),o1); } + + +/* +** different types of sets, according to destination +*/ + +/* from stack to (same) stack */ +#define setobjs2s setobj +/* to stack (not from same stack) */ +#define setobj2s setobj +#define setsvalue2s setsvalue +#define sethvalue2s sethvalue +#define setptvalue2s setptvalue +/* from table to same table */ +#define setobjt2t setobj +/* to table */ +#define setobj2t setobj +/* to new object */ +#define setobj2n setobj +#define setsvalue2n setsvalue + +#define setttype(obj, tt) (ttype(obj) = (tt)) + + +#define iscollectable(o) (ttype(o) >= LUA_TSTRING) + + + +typedef TValue *StkId; /* index to stack elements */ + + +/* +** String headers for string table +*/ +typedef union TString { + L_Umaxalign dummy; /* ensures maximum alignment for strings */ + struct { + CommonHeader; + lu_byte reserved; + unsigned int hash; + size_t len; + } tsv; +} TString; + + +#define getstr(ts) cast(const char *, (ts) + 1) +#define svalue(o) getstr(rawtsvalue(o)) + + + +typedef union Udata { + L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ + struct { + CommonHeader; + struct Table *metatable; + struct Table *env; + size_t len; + } uv; +} Udata; + + + + +/* +** Function Prototypes +*/ +typedef struct Proto { + CommonHeader; + TValue *k; /* constants used by the function */ + Instruction *code; + struct Proto **p; /* functions defined inside the function */ + int *lineinfo; /* map from opcodes to source lines */ + struct LocVar *locvars; /* information about local variables */ + TString **upvalues; /* upvalue names */ + TString *source; + int sizeupvalues; + int sizek; /* size of `k' */ + int sizecode; + int sizelineinfo; + int sizep; /* size of `p' */ + int sizelocvars; + int linedefined; + int lastlinedefined; + GCObject *gclist; + lu_byte nups; /* number of upvalues */ + lu_byte numparams; + lu_byte is_vararg; + lu_byte maxstacksize; +} Proto; + + +/* masks for new-style vararg */ +#define VARARG_HASARG 1 +#define VARARG_ISVARARG 2 +#define VARARG_NEEDSARG 4 + + +typedef struct LocVar { + TString *varname; + int startpc; /* first point where variable is active */ + int endpc; /* first point where variable is dead */ +} LocVar; + + + +/* +** Upvalues +*/ + +typedef struct UpVal { + CommonHeader; + TValue *v; /* points to stack or to its own value */ + union { + TValue value; /* the value (when closed) */ + struct { /* double linked list (when open) */ + struct UpVal *prev; + struct UpVal *next; + } l; + } u; +} UpVal; + + +/* +** Closures +*/ + +#define ClosureHeader \ + CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ + struct Table *env + +typedef struct CClosure { + ClosureHeader; + lua_CFunction f; + TValue upvalue[1]; +} CClosure; + + +typedef struct LClosure { + ClosureHeader; + struct Proto *p; + UpVal *upvals[1]; +} LClosure; + + +typedef union Closure { + CClosure c; + LClosure l; +} Closure; + + +#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) +#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) + + +/* +** Tables +*/ + +typedef union TKey { + struct { + TValuefields; + struct Node *next; /* for chaining */ + } nk; + TValue tvk; +} TKey; + + +typedef struct Node { + TValue i_val; + TKey i_key; +} Node; + + +typedef struct Table { + CommonHeader; + lu_byte flags; /* 1<

lsizenode)) + + +#define luaO_nilobject (&luaO_nilobject_) + +LUAI_DATA const TValue luaO_nilobject_; + +#define ceillog2(x) (luaO_log2((x)-1) + 1) + +LUAI_FUNC int luaO_log2 (unsigned int x); +LUAI_FUNC int luaO_int2fb (unsigned int x); +LUAI_FUNC int luaO_fb2int (int x); +LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); +LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, + va_list argp); +LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); +LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); + + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lopcodes.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lopcodes.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,102 @@ +/* +** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ +** See Copyright Notice in lua.h +*/ + + +#define lopcodes_c +#define LUA_CORE + + +#include "lopcodes.h" + + +/* ORDER OP */ + +const char *const luaP_opnames[NUM_OPCODES+1] = { + "MOVE", + "LOADK", + "LOADBOOL", + "LOADNIL", + "GETUPVAL", + "GETGLOBAL", + "GETTABLE", + "SETGLOBAL", + "SETUPVAL", + "SETTABLE", + "NEWTABLE", + "SELF", + "ADD", + "SUB", + "MUL", + "DIV", + "MOD", + "POW", + "UNM", + "NOT", + "LEN", + "CONCAT", + "JMP", + "EQ", + "LT", + "LE", + "TEST", + "TESTSET", + "CALL", + "TAILCALL", + "RETURN", + "FORLOOP", + "FORPREP", + "TFORLOOP", + "SETLIST", + "CLOSE", + "CLOSURE", + "VARARG", + NULL +}; + + +#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) + +const lu_byte luaP_opmodes[NUM_OPCODES] = { +/* T A B C mode opcode */ + opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ + ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ + ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ + ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ + ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ + ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ + ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ + ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ + ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ + ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ + ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ + ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ + ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ + ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ + ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ + ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ + ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ + ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ + ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ + ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ +}; + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lopcodes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lopcodes.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,268 @@ +/* +** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $ +** Opcodes for Lua virtual machine +** See Copyright Notice in lua.h +*/ + +#ifndef lopcodes_h +#define lopcodes_h + +#include "llimits.h" + + +/*=========================================================================== + We assume that instructions are unsigned numbers. + All instructions have an opcode in the first 6 bits. + Instructions can have the following fields: + `A' : 8 bits + `B' : 9 bits + `C' : 9 bits + `Bx' : 18 bits (`B' and `C' together) + `sBx' : signed Bx + + A signed argument is represented in excess K; that is, the number + value is the unsigned value minus K. K is exactly the maximum value + for that argument (so that -max is represented by 0, and +max is + represented by 2*max), which is half the maximum for the corresponding + unsigned argument. +===========================================================================*/ + + +enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */ + + +/* +** size and position of opcode arguments. +*/ +#define SIZE_C 9 +#define SIZE_B 9 +#define SIZE_Bx (SIZE_C + SIZE_B) +#define SIZE_A 8 + +#define SIZE_OP 6 + +#define POS_OP 0 +#define POS_A (POS_OP + SIZE_OP) +#define POS_C (POS_A + SIZE_A) +#define POS_B (POS_C + SIZE_C) +#define POS_Bx POS_C + + +/* +** limits for opcode arguments. +** we use (signed) int to manipulate most arguments, +** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) +*/ +#if SIZE_Bx < LUAI_BITSINT-1 +#define MAXARG_Bx ((1<>1) /* `sBx' is signed */ +#else +#define MAXARG_Bx MAX_INT +#define MAXARG_sBx MAX_INT +#endif + + +#define MAXARG_A ((1<>POS_OP) & MASK1(SIZE_OP,0))) +#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ + ((cast(Instruction, o)<>POS_A) & MASK1(SIZE_A,0))) +#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ + ((cast(Instruction, u)<>POS_B) & MASK1(SIZE_B,0))) +#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ + ((cast(Instruction, b)<>POS_C) & MASK1(SIZE_C,0))) +#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ + ((cast(Instruction, b)<>POS_Bx) & MASK1(SIZE_Bx,0))) +#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ + ((cast(Instruction, b)< C) then pc++ */ +OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ + +OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ +OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ +OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ + +OP_FORLOOP,/* A sBx R(A)+=R(A+2); + if R(A) =) R(A)*/ +OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ + +OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ +} OpCode; + + +#define NUM_OPCODES (cast(int, OP_VARARG) + 1) + + + +/*=========================================================================== + Notes: + (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1, + and can be 0: OP_CALL then sets `top' to last_result+1, so + next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. + + (*) In OP_VARARG, if (B == 0) then use actual number of varargs and + set top (like in OP_CALL with C == 0). + + (*) In OP_RETURN, if (B == 0) then return up to `top' + + (*) In OP_SETLIST, if (B == 0) then B = `top'; + if (C == 0) then next `instruction' is real C + + (*) For comparisons, A specifies what condition the test should accept + (true or false). + + (*) All `skips' (pc++) assume that next instruction is a jump +===========================================================================*/ + + +/* +** masks for instruction properties. The format is: +** bits 0-1: op mode +** bits 2-3: C arg mode +** bits 4-5: B arg mode +** bit 6: instruction set register A +** bit 7: operator is a test +*/ + +enum OpArgMask { + OpArgN, /* argument is not used */ + OpArgU, /* argument is used */ + OpArgR, /* argument is a register or a jump offset */ + OpArgK /* argument is a constant or register/constant */ +}; + +LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES]; + +#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) +#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) +#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) +#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) +#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) + + +LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ + + +/* number of list items to accumulate before a SETLIST instruction */ +#define LFIELDS_PER_FLUSH 50 + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/loslib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/loslib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,243 @@ +/* +** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ +** Standard Operating System library +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include +#include + +#define loslib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +static int os_pushresult (lua_State *L, int i, const char *filename) { + int en = errno; /* calls to Lua API may change this value */ + if (i) { + lua_pushboolean(L, 1); + return 1; + } + else { + lua_pushnil(L); + lua_pushfstring(L, "%s: %s", filename, strerror(en)); + lua_pushinteger(L, en); + return 3; + } +} + + +static int os_execute (lua_State *L) { + lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); + return 1; +} + + +static int os_remove (lua_State *L) { + const char *filename = luaL_checkstring(L, 1); + return os_pushresult(L, remove(filename) == 0, filename); +} + + +static int os_rename (lua_State *L) { + const char *fromname = luaL_checkstring(L, 1); + const char *toname = luaL_checkstring(L, 2); + return os_pushresult(L, rename(fromname, toname) == 0, fromname); +} + + +static int os_tmpname (lua_State *L) { + char buff[LUA_TMPNAMBUFSIZE]; + int err; + lua_tmpnam(buff, err); + if (err) + return luaL_error(L, "unable to generate a unique filename"); + lua_pushstring(L, buff); + return 1; +} + + +static int os_getenv (lua_State *L) { + lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ + return 1; +} + + +static int os_clock (lua_State *L) { + lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); + return 1; +} + + +/* +** {====================================================== +** Time/Date operations +** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, +** wday=%w+1, yday=%j, isdst=? } +** ======================================================= +*/ + +static void setfield (lua_State *L, const char *key, int value) { + lua_pushinteger(L, value); + lua_setfield(L, -2, key); +} + +static void setboolfield (lua_State *L, const char *key, int value) { + if (value < 0) /* undefined? */ + return; /* does not set field */ + lua_pushboolean(L, value); + lua_setfield(L, -2, key); +} + +static int getboolfield (lua_State *L, const char *key) { + int res; + lua_getfield(L, -1, key); + res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); + lua_pop(L, 1); + return res; +} + + +static int getfield (lua_State *L, const char *key, int d) { + int res; + lua_getfield(L, -1, key); + if (lua_isnumber(L, -1)) + res = (int)lua_tointeger(L, -1); + else { + if (d < 0) + return luaL_error(L, "field " LUA_QS " missing in date table", key); + res = d; + } + lua_pop(L, 1); + return res; +} + + +static int os_date (lua_State *L) { + const char *s = luaL_optstring(L, 1, "%c"); + time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); + struct tm *stm; + if (*s == '!') { /* UTC? */ + stm = gmtime(&t); + s++; /* skip `!' */ + } + else + stm = localtime(&t); + if (stm == NULL) /* invalid date? */ + lua_pushnil(L); + else if (strcmp(s, "*t") == 0) { + lua_createtable(L, 0, 9); /* 9 = number of fields */ + setfield(L, "sec", stm->tm_sec); + setfield(L, "min", stm->tm_min); + setfield(L, "hour", stm->tm_hour); + setfield(L, "day", stm->tm_mday); + setfield(L, "month", stm->tm_mon+1); + setfield(L, "year", stm->tm_year+1900); + setfield(L, "wday", stm->tm_wday+1); + setfield(L, "yday", stm->tm_yday+1); + setboolfield(L, "isdst", stm->tm_isdst); + } + else { + char cc[3]; + luaL_Buffer b; + cc[0] = '%'; cc[2] = '\0'; + luaL_buffinit(L, &b); + for (; *s; s++) { + if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ + luaL_addchar(&b, *s); + else { + size_t reslen; + char buff[200]; /* should be big enough for any conversion result */ + cc[1] = *(++s); + reslen = strftime(buff, sizeof(buff), cc, stm); + luaL_addlstring(&b, buff, reslen); + } + } + luaL_pushresult(&b); + } + return 1; +} + + +static int os_time (lua_State *L) { + time_t t; + if (lua_isnoneornil(L, 1)) /* called without args? */ + t = time(NULL); /* get current time */ + else { + struct tm ts; + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 1); /* make sure table is at the top */ + ts.tm_sec = getfield(L, "sec", 0); + ts.tm_min = getfield(L, "min", 0); + ts.tm_hour = getfield(L, "hour", 12); + ts.tm_mday = getfield(L, "day", -1); + ts.tm_mon = getfield(L, "month", -1) - 1; + ts.tm_year = getfield(L, "year", -1) - 1900; + ts.tm_isdst = getboolfield(L, "isdst"); + t = mktime(&ts); + } + if (t == (time_t)(-1)) + lua_pushnil(L); + else + lua_pushnumber(L, (lua_Number)t); + return 1; +} + + +static int os_difftime (lua_State *L) { + lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), + (time_t)(luaL_optnumber(L, 2, 0)))); + return 1; +} + +/* }====================================================== */ + + +static int os_setlocale (lua_State *L) { + static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, + LC_NUMERIC, LC_TIME}; + static const char *const catnames[] = {"all", "collate", "ctype", "monetary", + "numeric", "time", NULL}; + const char *l = luaL_optstring(L, 1, NULL); + int op = luaL_checkoption(L, 2, "all", catnames); + lua_pushstring(L, setlocale(cat[op], l)); + return 1; +} + + +static int os_exit (lua_State *L) { + exit(luaL_optint(L, 1, EXIT_SUCCESS)); +} + +static const luaL_Reg syslib[] = { + {"clock", os_clock}, + {"date", os_date}, + {"difftime", os_difftime}, + {"execute", os_execute}, + {"exit", os_exit}, + {"getenv", os_getenv}, + {"remove", os_remove}, + {"rename", os_rename}, + {"setlocale", os_setlocale}, + {"time", os_time}, + {"tmpname", os_tmpname}, + {NULL, NULL} +}; + +/* }====================================================== */ + + + +LUALIB_API int luaopen_os (lua_State *L) { + luaL_register(L, LUA_OSLIBNAME, syslib); + return 1; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lparser.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lparser.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1339 @@ +/* +** $Id: lparser.c,v 2.42.1.3 2007/12/28 15:32:23 roberto Exp $ +** Lua Parser +** See Copyright Notice in lua.h +*/ + + +#include + +#define lparser_c +#define LUA_CORE + +#include "lua.h" + +#include "lcode.h" +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "llex.h" +#include "lmem.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lparser.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" + + + +#define hasmultret(k) ((k) == VCALL || (k) == VVARARG) + +#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) + +#define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) + + +/* +** nodes for block list (list of active blocks) +*/ +typedef struct BlockCnt { + struct BlockCnt *previous; /* chain */ + int breaklist; /* list of jumps out of this loop */ + lu_byte nactvar; /* # active locals outside the breakable structure */ + lu_byte upval; /* true if some variable in the block is an upvalue */ + lu_byte isbreakable; /* true if `block' is a loop */ +} BlockCnt; + + + +/* +** prototypes for recursive non-terminal functions +*/ +static void chunk (LexState *ls); +static void expr (LexState *ls, expdesc *v); + + +static void anchor_token (LexState *ls) { + if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { + TString *ts = ls->t.seminfo.ts; + luaX_newstring(ls, getstr(ts), ts->tsv.len); + } +} + + +static void error_expected (LexState *ls, int token) { + luaX_syntaxerror(ls, + luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token))); +} + + +static void errorlimit (FuncState *fs, int limit, const char *what) { + const char *msg = (fs->f->linedefined == 0) ? + luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : + luaO_pushfstring(fs->L, "function at line %d has more than %d %s", + fs->f->linedefined, limit, what); + luaX_lexerror(fs->ls, msg, 0); +} + + +static int testnext (LexState *ls, int c) { + if (ls->t.token == c) { + luaX_next(ls); + return 1; + } + else return 0; +} + + +static void check (LexState *ls, int c) { + if (ls->t.token != c) + error_expected(ls, c); +} + +static void checknext (LexState *ls, int c) { + check(ls, c); + luaX_next(ls); +} + + +#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } + + + +static void check_match (LexState *ls, int what, int who, int where) { + if (!testnext(ls, what)) { + if (where == ls->linenumber) + error_expected(ls, what); + else { + luaX_syntaxerror(ls, luaO_pushfstring(ls->L, + LUA_QS " expected (to close " LUA_QS " at line %d)", + luaX_token2str(ls, what), luaX_token2str(ls, who), where)); + } + } +} + + +static TString *str_checkname (LexState *ls) { + TString *ts; + check(ls, TK_NAME); + ts = ls->t.seminfo.ts; + luaX_next(ls); + return ts; +} + + +static void init_exp (expdesc *e, expkind k, int i) { + e->f = e->t = NO_JUMP; + e->k = k; + e->u.s.info = i; +} + + +static void codestring (LexState *ls, expdesc *e, TString *s) { + init_exp(e, VK, luaK_stringK(ls->fs, s)); +} + + +static void checkname(LexState *ls, expdesc *e) { + codestring(ls, e, str_checkname(ls)); +} + + +static int registerlocalvar (LexState *ls, TString *varname) { + FuncState *fs = ls->fs; + Proto *f = fs->f; + int oldsize = f->sizelocvars; + luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, + LocVar, SHRT_MAX, "too many local variables"); + while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; + f->locvars[fs->nlocvars].varname = varname; + luaC_objbarrier(ls->L, f, varname); + return fs->nlocvars++; +} + + +#define new_localvarliteral(ls,v,n) \ + new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) + + +static void new_localvar (LexState *ls, TString *name, int n) { + FuncState *fs = ls->fs; + luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); + fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name)); +} + + +static void adjustlocalvars (LexState *ls, int nvars) { + FuncState *fs = ls->fs; + fs->nactvar = cast_byte(fs->nactvar + nvars); + for (; nvars; nvars--) { + getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; + } +} + + +static void removevars (LexState *ls, int tolevel) { + FuncState *fs = ls->fs; + while (fs->nactvar > tolevel) + getlocvar(fs, --fs->nactvar).endpc = fs->pc; +} + + +static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { + int i; + Proto *f = fs->f; + int oldsize = f->sizeupvalues; + for (i=0; inups; i++) { + if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) { + lua_assert(f->upvalues[i] == name); + return i; + } + } + /* new one */ + luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues"); + luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, + TString *, MAX_INT, ""); + while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; + f->upvalues[f->nups] = name; + luaC_objbarrier(fs->L, f, name); + lua_assert(v->k == VLOCAL || v->k == VUPVAL); + fs->upvalues[f->nups].k = cast_byte(v->k); + fs->upvalues[f->nups].info = cast_byte(v->u.s.info); + return f->nups++; +} + + +static int searchvar (FuncState *fs, TString *n) { + int i; + for (i=fs->nactvar-1; i >= 0; i--) { + if (n == getlocvar(fs, i).varname) + return i; + } + return -1; /* not found */ +} + + +static void markupval (FuncState *fs, int level) { + BlockCnt *bl = fs->bl; + while (bl && bl->nactvar > level) bl = bl->previous; + if (bl) bl->upval = 1; +} + + +static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { + if (fs == NULL) { /* no more levels? */ + init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ + return VGLOBAL; + } + else { + int v = searchvar(fs, n); /* look up at current level */ + if (v >= 0) { + init_exp(var, VLOCAL, v); + if (!base) + markupval(fs, v); /* local will be used as an upval */ + return VLOCAL; + } + else { /* not found at current level; try upper one */ + if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) + return VGLOBAL; + var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ + var->k = VUPVAL; /* upvalue in this level */ + return VUPVAL; + } + } +} + + +static void singlevar (LexState *ls, expdesc *var) { + TString *varname = str_checkname(ls); + FuncState *fs = ls->fs; + if (singlevaraux(fs, varname, var, 1) == VGLOBAL) + var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */ +} + + +static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { + FuncState *fs = ls->fs; + int extra = nvars - nexps; + if (hasmultret(e->k)) { + extra++; /* includes call itself */ + if (extra < 0) extra = 0; + luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ + if (extra > 1) luaK_reserveregs(fs, extra-1); + } + else { + if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ + if (extra > 0) { + int reg = fs->freereg; + luaK_reserveregs(fs, extra); + luaK_nil(fs, reg, extra); + } + } +} + + +static void enterlevel (LexState *ls) { + if (++ls->L->nCcalls > LUAI_MAXCCALLS) + luaX_lexerror(ls, "chunk has too many syntax levels", 0); +} + + +#define leavelevel(ls) ((ls)->L->nCcalls--) + + +static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { + bl->breaklist = NO_JUMP; + bl->isbreakable = isbreakable; + bl->nactvar = fs->nactvar; + bl->upval = 0; + bl->previous = fs->bl; + fs->bl = bl; + lua_assert(fs->freereg == fs->nactvar); +} + + +static void leaveblock (FuncState *fs) { + BlockCnt *bl = fs->bl; + fs->bl = bl->previous; + removevars(fs->ls, bl->nactvar); + if (bl->upval) + luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); + /* a block either controls scope or breaks (never both) */ + lua_assert(!bl->isbreakable || !bl->upval); + lua_assert(bl->nactvar == fs->nactvar); + fs->freereg = fs->nactvar; /* free registers */ + luaK_patchtohere(fs, bl->breaklist); +} + + +static void pushclosure (LexState *ls, FuncState *func, expdesc *v) { + FuncState *fs = ls->fs; + Proto *f = fs->f; + int oldsize = f->sizep; + int i; + luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *, + MAXARG_Bx, "constant table overflow"); + while (oldsize < f->sizep) f->p[oldsize++] = NULL; + f->p[fs->np++] = func->f; + luaC_objbarrier(ls->L, f, func->f); + init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); + for (i=0; if->nups; i++) { + OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; + luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0); + } +} + + +static void open_func (LexState *ls, FuncState *fs) { + lua_State *L = ls->L; + Proto *f = luaF_newproto(L); + fs->f = f; + fs->prev = ls->fs; /* linked list of funcstates */ + fs->ls = ls; + fs->L = L; + ls->fs = fs; + fs->pc = 0; + fs->lasttarget = -1; + fs->jpc = NO_JUMP; + fs->freereg = 0; + fs->nk = 0; + fs->np = 0; + fs->nlocvars = 0; + fs->nactvar = 0; + fs->bl = NULL; + f->source = ls->source; + f->maxstacksize = 2; /* registers 0/1 are always valid */ + fs->h = luaH_new(L, 0, 0); + /* anchor table of constants and prototype (to avoid being collected) */ + sethvalue2s(L, L->top, fs->h); + incr_top(L); + setptvalue2s(L, L->top, f); + incr_top(L); +} + + +static void close_func (LexState *ls) { + lua_State *L = ls->L; + FuncState *fs = ls->fs; + Proto *f = fs->f; + removevars(ls, 0); + luaK_ret(fs, 0, 0); /* final return */ + luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); + f->sizecode = fs->pc; + luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); + f->sizelineinfo = fs->pc; + luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); + f->sizek = fs->nk; + luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); + f->sizep = fs->np; + luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); + f->sizelocvars = fs->nlocvars; + luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *); + f->sizeupvalues = f->nups; + lua_assert(luaG_checkcode(f)); + lua_assert(fs->bl == NULL); + ls->fs = fs->prev; + L->top -= 2; /* remove table and prototype from the stack */ + /* last token read was anchored in defunct function; must reanchor it */ + if (fs) anchor_token(ls); +} + + +Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { + struct LexState lexstate; + struct FuncState funcstate; + lexstate.buff = buff; + luaX_setinput(L, &lexstate, z, luaS_new(L, name)); + open_func(&lexstate, &funcstate); + funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ + luaX_next(&lexstate); /* read first token */ + chunk(&lexstate); + check(&lexstate, TK_EOS); + close_func(&lexstate); + lua_assert(funcstate.prev == NULL); + lua_assert(funcstate.f->nups == 0); + lua_assert(lexstate.fs == NULL); + return funcstate.f; +} + + + +/*============================================================*/ +/* GRAMMAR RULES */ +/*============================================================*/ + + +static void field (LexState *ls, expdesc *v) { + /* field -> ['.' | ':'] NAME */ + FuncState *fs = ls->fs; + expdesc key; + luaK_exp2anyreg(fs, v); + luaX_next(ls); /* skip the dot or colon */ + checkname(ls, &key); + luaK_indexed(fs, v, &key); +} + + +static void yindex (LexState *ls, expdesc *v) { + /* index -> '[' expr ']' */ + luaX_next(ls); /* skip the '[' */ + expr(ls, v); + luaK_exp2val(ls->fs, v); + checknext(ls, ']'); +} + + +/* +** {====================================================================== +** Rules for Constructors +** ======================================================================= +*/ + + +struct ConsControl { + expdesc v; /* last list item read */ + expdesc *t; /* table descriptor */ + int nh; /* total number of `record' elements */ + int na; /* total number of array elements */ + int tostore; /* number of array elements pending to be stored */ +}; + + +static void recfield (LexState *ls, struct ConsControl *cc) { + /* recfield -> (NAME | `['exp1`]') = exp1 */ + FuncState *fs = ls->fs; + int reg = ls->fs->freereg; + expdesc key, val; + int rkkey; + if (ls->t.token == TK_NAME) { + luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); + checkname(ls, &key); + } + else /* ls->t.token == '[' */ + yindex(ls, &key); + cc->nh++; + checknext(ls, '='); + rkkey = luaK_exp2RK(fs, &key); + expr(ls, &val); + luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val)); + fs->freereg = reg; /* free registers */ +} + + +static void closelistfield (FuncState *fs, struct ConsControl *cc) { + if (cc->v.k == VVOID) return; /* there is no list item */ + luaK_exp2nextreg(fs, &cc->v); + cc->v.k = VVOID; + if (cc->tostore == LFIELDS_PER_FLUSH) { + luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */ + cc->tostore = 0; /* no more items pending */ + } +} + + +static void lastlistfield (FuncState *fs, struct ConsControl *cc) { + if (cc->tostore == 0) return; + if (hasmultret(cc->v.k)) { + luaK_setmultret(fs, &cc->v); + luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET); + cc->na--; /* do not count last expression (unknown number of elements) */ + } + else { + if (cc->v.k != VVOID) + luaK_exp2nextreg(fs, &cc->v); + luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); + } +} + + +static void listfield (LexState *ls, struct ConsControl *cc) { + expr(ls, &cc->v); + luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); + cc->na++; + cc->tostore++; +} + + +static void constructor (LexState *ls, expdesc *t) { + /* constructor -> ?? */ + FuncState *fs = ls->fs; + int line = ls->linenumber; + int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); + struct ConsControl cc; + cc.na = cc.nh = cc.tostore = 0; + cc.t = t; + init_exp(t, VRELOCABLE, pc); + init_exp(&cc.v, VVOID, 0); /* no value (yet) */ + luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ + checknext(ls, '{'); + do { + lua_assert(cc.v.k == VVOID || cc.tostore > 0); + if (ls->t.token == '}') break; + closelistfield(fs, &cc); + switch(ls->t.token) { + case TK_NAME: { /* may be listfields or recfields */ + luaX_lookahead(ls); + if (ls->lookahead.token != '=') /* expression? */ + listfield(ls, &cc); + else + recfield(ls, &cc); + break; + } + case '[': { /* constructor_item -> recfield */ + recfield(ls, &cc); + break; + } + default: { /* constructor_part -> listfield */ + listfield(ls, &cc); + break; + } + } + } while (testnext(ls, ',') || testnext(ls, ';')); + check_match(ls, '}', '{', line); + lastlistfield(fs, &cc); + SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ + SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ +} + +/* }====================================================================== */ + + + +static void parlist (LexState *ls) { + /* parlist -> [ param { `,' param } ] */ + FuncState *fs = ls->fs; + Proto *f = fs->f; + int nparams = 0; + f->is_vararg = 0; + if (ls->t.token != ')') { /* is `parlist' not empty? */ + do { + switch (ls->t.token) { + case TK_NAME: { /* param -> NAME */ + new_localvar(ls, str_checkname(ls), nparams++); + break; + } + case TK_DOTS: { /* param -> `...' */ + luaX_next(ls); +#if defined(LUA_COMPAT_VARARG) + /* use `arg' as default name */ + new_localvarliteral(ls, "arg", nparams++); + f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG; +#endif + f->is_vararg |= VARARG_ISVARARG; + break; + } + default: luaX_syntaxerror(ls, " or " LUA_QL("...") " expected"); + } + } while (!f->is_vararg && testnext(ls, ',')); + } + adjustlocalvars(ls, nparams); + f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); + luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ +} + + +static void body (LexState *ls, expdesc *e, int needself, int line) { + /* body -> `(' parlist `)' chunk END */ + FuncState new_fs; + open_func(ls, &new_fs); + new_fs.f->linedefined = line; + checknext(ls, '('); + if (needself) { + new_localvarliteral(ls, "self", 0); + adjustlocalvars(ls, 1); + } + parlist(ls); + checknext(ls, ')'); + chunk(ls); + new_fs.f->lastlinedefined = ls->linenumber; + check_match(ls, TK_END, TK_FUNCTION, line); + close_func(ls); + pushclosure(ls, &new_fs, e); +} + + +static int explist1 (LexState *ls, expdesc *v) { + /* explist1 -> expr { `,' expr } */ + int n = 1; /* at least one expression */ + expr(ls, v); + while (testnext(ls, ',')) { + luaK_exp2nextreg(ls->fs, v); + expr(ls, v); + n++; + } + return n; +} + + +static void funcargs (LexState *ls, expdesc *f) { + FuncState *fs = ls->fs; + expdesc args; + int base, nparams; + int line = ls->linenumber; + switch (ls->t.token) { + case '(': { /* funcargs -> `(' [ explist1 ] `)' */ + if (line != ls->lastline) + luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); + luaX_next(ls); + if (ls->t.token == ')') /* arg list is empty? */ + args.k = VVOID; + else { + explist1(ls, &args); + luaK_setmultret(fs, &args); + } + check_match(ls, ')', '(', line); + break; + } + case '{': { /* funcargs -> constructor */ + constructor(ls, &args); + break; + } + case TK_STRING: { /* funcargs -> STRING */ + codestring(ls, &args, ls->t.seminfo.ts); + luaX_next(ls); /* must use `seminfo' before `next' */ + break; + } + default: { + luaX_syntaxerror(ls, "function arguments expected"); + return; + } + } + lua_assert(f->k == VNONRELOC); + base = f->u.s.info; /* base register for call */ + if (hasmultret(args.k)) + nparams = LUA_MULTRET; /* open call */ + else { + if (args.k != VVOID) + luaK_exp2nextreg(fs, &args); /* close last argument */ + nparams = fs->freereg - (base+1); + } + init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); + luaK_fixline(fs, line); + fs->freereg = base+1; /* call remove function and arguments and leaves + (unless changed) one result */ +} + + + + +/* +** {====================================================================== +** Expression parsing +** ======================================================================= +*/ + + +static void prefixexp (LexState *ls, expdesc *v) { + /* prefixexp -> NAME | '(' expr ')' */ + switch (ls->t.token) { + case '(': { + int line = ls->linenumber; + luaX_next(ls); + expr(ls, v); + check_match(ls, ')', '(', line); + luaK_dischargevars(ls->fs, v); + return; + } + case TK_NAME: { + singlevar(ls, v); + return; + } + default: { + luaX_syntaxerror(ls, "unexpected symbol"); + return; + } + } +} + + +static void primaryexp (LexState *ls, expdesc *v) { + /* primaryexp -> + prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ + FuncState *fs = ls->fs; + prefixexp(ls, v); + for (;;) { + switch (ls->t.token) { + case '.': { /* field */ + field(ls, v); + break; + } + case '[': { /* `[' exp1 `]' */ + expdesc key; + luaK_exp2anyreg(fs, v); + yindex(ls, &key); + luaK_indexed(fs, v, &key); + break; + } + case ':': { /* `:' NAME funcargs */ + expdesc key; + luaX_next(ls); + checkname(ls, &key); + luaK_self(fs, v, &key); + funcargs(ls, v); + break; + } + case '(': case TK_STRING: case '{': { /* funcargs */ + luaK_exp2nextreg(fs, v); + funcargs(ls, v); + break; + } + default: return; + } + } +} + + +static void simpleexp (LexState *ls, expdesc *v) { + /* simpleexp -> NUMBER | STRING | NIL | true | false | ... | + constructor | FUNCTION body | primaryexp */ + switch (ls->t.token) { + case TK_NUMBER: { + init_exp(v, VKNUM, 0); + v->u.nval = ls->t.seminfo.r; + break; + } + case TK_STRING: { + codestring(ls, v, ls->t.seminfo.ts); + break; + } + case TK_NIL: { + init_exp(v, VNIL, 0); + break; + } + case TK_TRUE: { + init_exp(v, VTRUE, 0); + break; + } + case TK_FALSE: { + init_exp(v, VFALSE, 0); + break; + } + case TK_DOTS: { /* vararg */ + FuncState *fs = ls->fs; + check_condition(ls, fs->f->is_vararg, + "cannot use " LUA_QL("...") " outside a vararg function"); + fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */ + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); + break; + } + case '{': { /* constructor */ + constructor(ls, v); + return; + } + case TK_FUNCTION: { + luaX_next(ls); + body(ls, v, 0, ls->linenumber); + return; + } + default: { + primaryexp(ls, v); + return; + } + } + luaX_next(ls); +} + + +static UnOpr getunopr (int op) { + switch (op) { + case TK_NOT: return OPR_NOT; + case '-': return OPR_MINUS; + case '#': return OPR_LEN; + default: return OPR_NOUNOPR; + } +} + + +static BinOpr getbinopr (int op) { + switch (op) { + case '+': return OPR_ADD; + case '-': return OPR_SUB; + case '*': return OPR_MUL; + case '/': return OPR_DIV; + case '%': return OPR_MOD; + case '^': return OPR_POW; + case TK_CONCAT: return OPR_CONCAT; + case TK_NE: return OPR_NE; + case TK_EQ: return OPR_EQ; + case '<': return OPR_LT; + case TK_LE: return OPR_LE; + case '>': return OPR_GT; + case TK_GE: return OPR_GE; + case TK_AND: return OPR_AND; + case TK_OR: return OPR_OR; + default: return OPR_NOBINOPR; + } +} + + +static const struct { + lu_byte left; /* left priority for each binary operator */ + lu_byte right; /* right priority */ +} priority[] = { /* ORDER OPR */ + {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ + {10, 9}, {5, 4}, /* power and concat (right associative) */ + {3, 3}, {3, 3}, /* equality and inequality */ + {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ + {2, 2}, {1, 1} /* logical (and/or) */ +}; + +#define UNARY_PRIORITY 8 /* priority for unary operators */ + + +/* +** subexpr -> (simpleexp | unop subexpr) { binop subexpr } +** where `binop' is any binary operator with a priority higher than `limit' +*/ +static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { + BinOpr op; + UnOpr uop; + enterlevel(ls); + uop = getunopr(ls->t.token); + if (uop != OPR_NOUNOPR) { + luaX_next(ls); + subexpr(ls, v, UNARY_PRIORITY); + luaK_prefix(ls->fs, uop, v); + } + else simpleexp(ls, v); + /* expand while operators have priorities higher than `limit' */ + op = getbinopr(ls->t.token); + while (op != OPR_NOBINOPR && priority[op].left > limit) { + expdesc v2; + BinOpr nextop; + luaX_next(ls); + luaK_infix(ls->fs, op, v); + /* read sub-expression with higher priority */ + nextop = subexpr(ls, &v2, priority[op].right); + luaK_posfix(ls->fs, op, v, &v2); + op = nextop; + } + leavelevel(ls); + return op; /* return first untreated operator */ +} + + +static void expr (LexState *ls, expdesc *v) { + subexpr(ls, v, 0); +} + +/* }==================================================================== */ + + + +/* +** {====================================================================== +** Rules for Statements +** ======================================================================= +*/ + + +static int block_follow (int token) { + switch (token) { + case TK_ELSE: case TK_ELSEIF: case TK_END: + case TK_UNTIL: case TK_EOS: + return 1; + default: return 0; + } +} + + +static void block (LexState *ls) { + /* block -> chunk */ + FuncState *fs = ls->fs; + BlockCnt bl; + enterblock(fs, &bl, 0); + chunk(ls); + lua_assert(bl.breaklist == NO_JUMP); + leaveblock(fs); +} + + +/* +** structure to chain all variables in the left-hand side of an +** assignment +*/ +struct LHS_assign { + struct LHS_assign *prev; + expdesc v; /* variable (global, local, upvalue, or indexed) */ +}; + + +/* +** check whether, in an assignment to a local variable, the local variable +** is needed in a previous assignment (to a table). If so, save original +** local value in a safe place and use this safe copy in the previous +** assignment. +*/ +static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { + FuncState *fs = ls->fs; + int extra = fs->freereg; /* eventual position to save local variable */ + int conflict = 0; + for (; lh; lh = lh->prev) { + if (lh->v.k == VINDEXED) { + if (lh->v.u.s.info == v->u.s.info) { /* conflict? */ + conflict = 1; + lh->v.u.s.info = extra; /* previous assignment will use safe copy */ + } + if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */ + conflict = 1; + lh->v.u.s.aux = extra; /* previous assignment will use safe copy */ + } + } + } + if (conflict) { + luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */ + luaK_reserveregs(fs, 1); + } +} + + +static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { + expdesc e; + check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, + "syntax error"); + if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ + struct LHS_assign nv; + nv.prev = lh; + primaryexp(ls, &nv.v); + if (nv.v.k == VLOCAL) + check_conflict(ls, lh, &nv.v); + luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, + "variables in assignment"); + assignment(ls, &nv, nvars+1); + } + else { /* assignment -> `=' explist1 */ + int nexps; + checknext(ls, '='); + nexps = explist1(ls, &e); + if (nexps != nvars) { + adjust_assign(ls, nvars, nexps, &e); + if (nexps > nvars) + ls->fs->freereg -= nexps - nvars; /* remove extra values */ + } + else { + luaK_setoneret(ls->fs, &e); /* close last expression */ + luaK_storevar(ls->fs, &lh->v, &e); + return; /* avoid default */ + } + } + init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ + luaK_storevar(ls->fs, &lh->v, &e); +} + + +static int cond (LexState *ls) { + /* cond -> exp */ + expdesc v; + expr(ls, &v); /* read condition */ + if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ + luaK_goiftrue(ls->fs, &v); + return v.f; +} + + +static void breakstat (LexState *ls) { + FuncState *fs = ls->fs; + BlockCnt *bl = fs->bl; + int upval = 0; + while (bl && !bl->isbreakable) { + upval |= bl->upval; + bl = bl->previous; + } + if (!bl) + luaX_syntaxerror(ls, "no loop to break"); + if (upval) + luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); + luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); +} + + +static void whilestat (LexState *ls, int line) { + /* whilestat -> WHILE cond DO block END */ + FuncState *fs = ls->fs; + int whileinit; + int condexit; + BlockCnt bl; + luaX_next(ls); /* skip WHILE */ + whileinit = luaK_getlabel(fs); + condexit = cond(ls); + enterblock(fs, &bl, 1); + checknext(ls, TK_DO); + block(ls); + luaK_patchlist(fs, luaK_jump(fs), whileinit); + check_match(ls, TK_END, TK_WHILE, line); + leaveblock(fs); + luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ +} + + +static void repeatstat (LexState *ls, int line) { + /* repeatstat -> REPEAT block UNTIL cond */ + int condexit; + FuncState *fs = ls->fs; + int repeat_init = luaK_getlabel(fs); + BlockCnt bl1, bl2; + enterblock(fs, &bl1, 1); /* loop block */ + enterblock(fs, &bl2, 0); /* scope block */ + luaX_next(ls); /* skip REPEAT */ + chunk(ls); + check_match(ls, TK_UNTIL, TK_REPEAT, line); + condexit = cond(ls); /* read condition (inside scope block) */ + if (!bl2.upval) { /* no upvalues? */ + leaveblock(fs); /* finish scope */ + luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */ + } + else { /* complete semantics when there are upvalues */ + breakstat(ls); /* if condition then break */ + luaK_patchtohere(ls->fs, condexit); /* else... */ + leaveblock(fs); /* finish scope... */ + luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */ + } + leaveblock(fs); /* finish loop */ +} + + +static int exp1 (LexState *ls) { + expdesc e; + int k; + expr(ls, &e); + k = e.k; + luaK_exp2nextreg(ls->fs, &e); + return k; +} + + +static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { + /* forbody -> DO block */ + BlockCnt bl; + FuncState *fs = ls->fs; + int prep, endfor; + adjustlocalvars(ls, 3); /* control variables */ + checknext(ls, TK_DO); + prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); + enterblock(fs, &bl, 0); /* scope for declared variables */ + adjustlocalvars(ls, nvars); + luaK_reserveregs(fs, nvars); + block(ls); + leaveblock(fs); /* end of scope for declared variables */ + luaK_patchtohere(fs, prep); + endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : + luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars); + luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */ + luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1); +} + + +static void fornum (LexState *ls, TString *varname, int line) { + /* fornum -> NAME = exp1,exp1[,exp1] forbody */ + FuncState *fs = ls->fs; + int base = fs->freereg; + new_localvarliteral(ls, "(for index)", 0); + new_localvarliteral(ls, "(for limit)", 1); + new_localvarliteral(ls, "(for step)", 2); + new_localvar(ls, varname, 3); + checknext(ls, '='); + exp1(ls); /* initial value */ + checknext(ls, ','); + exp1(ls); /* limit */ + if (testnext(ls, ',')) + exp1(ls); /* optional step */ + else { /* default step = 1 */ + luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1)); + luaK_reserveregs(fs, 1); + } + forbody(ls, base, line, 1, 1); +} + + +static void forlist (LexState *ls, TString *indexname) { + /* forlist -> NAME {,NAME} IN explist1 forbody */ + FuncState *fs = ls->fs; + expdesc e; + int nvars = 0; + int line; + int base = fs->freereg; + /* create control variables */ + new_localvarliteral(ls, "(for generator)", nvars++); + new_localvarliteral(ls, "(for state)", nvars++); + new_localvarliteral(ls, "(for control)", nvars++); + /* create declared variables */ + new_localvar(ls, indexname, nvars++); + while (testnext(ls, ',')) + new_localvar(ls, str_checkname(ls), nvars++); + checknext(ls, TK_IN); + line = ls->linenumber; + adjust_assign(ls, 3, explist1(ls, &e), &e); + luaK_checkstack(fs, 3); /* extra space to call generator */ + forbody(ls, base, line, nvars - 3, 0); +} + + +static void forstat (LexState *ls, int line) { + /* forstat -> FOR (fornum | forlist) END */ + FuncState *fs = ls->fs; + TString *varname; + BlockCnt bl; + enterblock(fs, &bl, 1); /* scope for loop and control variables */ + luaX_next(ls); /* skip `for' */ + varname = str_checkname(ls); /* first variable name */ + switch (ls->t.token) { + case '=': fornum(ls, varname, line); break; + case ',': case TK_IN: forlist(ls, varname); break; + default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); + } + check_match(ls, TK_END, TK_FOR, line); + leaveblock(fs); /* loop scope (`break' jumps to this point) */ +} + + +static int test_then_block (LexState *ls) { + /* test_then_block -> [IF | ELSEIF] cond THEN block */ + int condexit; + luaX_next(ls); /* skip IF or ELSEIF */ + condexit = cond(ls); + checknext(ls, TK_THEN); + block(ls); /* `then' part */ + return condexit; +} + + +static void ifstat (LexState *ls, int line) { + /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ + FuncState *fs = ls->fs; + int flist; + int escapelist = NO_JUMP; + flist = test_then_block(ls); /* IF cond THEN block */ + while (ls->t.token == TK_ELSEIF) { + luaK_concat(fs, &escapelist, luaK_jump(fs)); + luaK_patchtohere(fs, flist); + flist = test_then_block(ls); /* ELSEIF cond THEN block */ + } + if (ls->t.token == TK_ELSE) { + luaK_concat(fs, &escapelist, luaK_jump(fs)); + luaK_patchtohere(fs, flist); + luaX_next(ls); /* skip ELSE (after patch, for correct line info) */ + block(ls); /* `else' part */ + } + else + luaK_concat(fs, &escapelist, flist); + luaK_patchtohere(fs, escapelist); + check_match(ls, TK_END, TK_IF, line); +} + + +static void localfunc (LexState *ls) { + expdesc v, b; + FuncState *fs = ls->fs; + new_localvar(ls, str_checkname(ls), 0); + init_exp(&v, VLOCAL, fs->freereg); + luaK_reserveregs(fs, 1); + adjustlocalvars(ls, 1); + body(ls, &b, 0, ls->linenumber); + luaK_storevar(fs, &v, &b); + /* debug information will only see the variable after this point! */ + getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; +} + + +static void localstat (LexState *ls) { + /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */ + int nvars = 0; + int nexps; + expdesc e; + do { + new_localvar(ls, str_checkname(ls), nvars++); + } while (testnext(ls, ',')); + if (testnext(ls, '=')) + nexps = explist1(ls, &e); + else { + e.k = VVOID; + nexps = 0; + } + adjust_assign(ls, nvars, nexps, &e); + adjustlocalvars(ls, nvars); +} + + +static int funcname (LexState *ls, expdesc *v) { + /* funcname -> NAME {field} [`:' NAME] */ + int needself = 0; + singlevar(ls, v); + while (ls->t.token == '.') + field(ls, v); + if (ls->t.token == ':') { + needself = 1; + field(ls, v); + } + return needself; +} + + +static void funcstat (LexState *ls, int line) { + /* funcstat -> FUNCTION funcname body */ + int needself; + expdesc v, b; + luaX_next(ls); /* skip FUNCTION */ + needself = funcname(ls, &v); + body(ls, &b, needself, line); + luaK_storevar(ls->fs, &v, &b); + luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ +} + + +static void exprstat (LexState *ls) { + /* stat -> func | assignment */ + FuncState *fs = ls->fs; + struct LHS_assign v; + primaryexp(ls, &v.v); + if (v.v.k == VCALL) /* stat -> func */ + SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ + else { /* stat -> assignment */ + v.prev = NULL; + assignment(ls, &v, 1); + } +} + + +static void retstat (LexState *ls) { + /* stat -> RETURN explist */ + FuncState *fs = ls->fs; + expdesc e; + int first, nret; /* registers with returned values */ + luaX_next(ls); /* skip RETURN */ + if (block_follow(ls->t.token) || ls->t.token == ';') + first = nret = 0; /* return no values */ + else { + nret = explist1(ls, &e); /* optional return values */ + if (hasmultret(e.k)) { + luaK_setmultret(fs, &e); + if (e.k == VCALL && nret == 1) { /* tail call? */ + SET_OPCODE(getcode(fs,&e), OP_TAILCALL); + lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); + } + first = fs->nactvar; + nret = LUA_MULTRET; /* return all values */ + } + else { + if (nret == 1) /* only one single value? */ + first = luaK_exp2anyreg(fs, &e); + else { + luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ + first = fs->nactvar; /* return all `active' values */ + lua_assert(nret == fs->freereg - first); + } + } + } + luaK_ret(fs, first, nret); +} + + +static int statement (LexState *ls) { + int line = ls->linenumber; /* may be needed for error messages */ + switch (ls->t.token) { + case TK_IF: { /* stat -> ifstat */ + ifstat(ls, line); + return 0; + } + case TK_WHILE: { /* stat -> whilestat */ + whilestat(ls, line); + return 0; + } + case TK_DO: { /* stat -> DO block END */ + luaX_next(ls); /* skip DO */ + block(ls); + check_match(ls, TK_END, TK_DO, line); + return 0; + } + case TK_FOR: { /* stat -> forstat */ + forstat(ls, line); + return 0; + } + case TK_REPEAT: { /* stat -> repeatstat */ + repeatstat(ls, line); + return 0; + } + case TK_FUNCTION: { + funcstat(ls, line); /* stat -> funcstat */ + return 0; + } + case TK_LOCAL: { /* stat -> localstat */ + luaX_next(ls); /* skip LOCAL */ + if (testnext(ls, TK_FUNCTION)) /* local function? */ + localfunc(ls); + else + localstat(ls); + return 0; + } + case TK_RETURN: { /* stat -> retstat */ + retstat(ls); + return 1; /* must be last statement */ + } + case TK_BREAK: { /* stat -> breakstat */ + luaX_next(ls); /* skip BREAK */ + breakstat(ls); + return 1; /* must be last statement */ + } + default: { + exprstat(ls); + return 0; /* to avoid warnings */ + } + } +} + + +static void chunk (LexState *ls) { + /* chunk -> { stat [`;'] } */ + int islast = 0; + enterlevel(ls); + while (!islast && !block_follow(ls->t.token)) { + islast = statement(ls); + testnext(ls, ';'); + lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && + ls->fs->freereg >= ls->fs->nactvar); + ls->fs->freereg = ls->fs->nactvar; /* free registers */ + } + leavelevel(ls); +} + +/* }====================================================================== */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lparser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lparser.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,82 @@ +/* +** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua Parser +** See Copyright Notice in lua.h +*/ + +#ifndef lparser_h +#define lparser_h + +#include "llimits.h" +#include "lobject.h" +#include "lzio.h" + + +/* +** Expression descriptor +*/ + +typedef enum { + VVOID, /* no value */ + VNIL, + VTRUE, + VFALSE, + VK, /* info = index of constant in `k' */ + VKNUM, /* nval = numerical value */ + VLOCAL, /* info = local register */ + VUPVAL, /* info = index of upvalue in `upvalues' */ + VGLOBAL, /* info = index of table; aux = index of global name in `k' */ + VINDEXED, /* info = table register; aux = index register (or `k') */ + VJMP, /* info = instruction pc */ + VRELOCABLE, /* info = instruction pc */ + VNONRELOC, /* info = result register */ + VCALL, /* info = instruction pc */ + VVARARG /* info = instruction pc */ +} expkind; + +typedef struct expdesc { + expkind k; + union { + struct { int info, aux; } s; + lua_Number nval; + } u; + int t; /* patch list of `exit when true' */ + int f; /* patch list of `exit when false' */ +} expdesc; + + +typedef struct upvaldesc { + lu_byte k; + lu_byte info; +} upvaldesc; + + +struct BlockCnt; /* defined in lparser.c */ + + +/* state needed to generate code for a given function */ +typedef struct FuncState { + Proto *f; /* current function header */ + Table *h; /* table to find (and reuse) elements in `k' */ + struct FuncState *prev; /* enclosing function */ + struct LexState *ls; /* lexical state */ + struct lua_State *L; /* copy of the Lua state */ + struct BlockCnt *bl; /* chain of current blocks */ + int pc; /* next position to code (equivalent to `ncode') */ + int lasttarget; /* `pc' of last `jump target' */ + int jpc; /* list of pending jumps to `pc' */ + int freereg; /* first free register */ + int nk; /* number of elements in `k' */ + int np; /* number of elements in `p' */ + short nlocvars; /* number of elements in `locvars' */ + lu_byte nactvar; /* number of active local variables */ + upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ + unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ +} FuncState; + + +LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, + const char *name); + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lstate.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lstate.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,214 @@ +/* +** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ +** Global State +** See Copyright Notice in lua.h +*/ + + +#include + +#define lstate_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "llex.h" +#include "lmem.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" + + +#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) +#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) +#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) + + +/* +** Main thread combines a thread state and the global state +*/ +typedef struct LG { + lua_State l; + global_State g; +} LG; + + + +static void stack_init (lua_State *L1, lua_State *L) { + /* initialize CallInfo array */ + L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); + L1->ci = L1->base_ci; + L1->size_ci = BASIC_CI_SIZE; + L1->end_ci = L1->base_ci + L1->size_ci - 1; + /* initialize stack array */ + L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); + L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; + L1->top = L1->stack; + L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; + /* initialize first ci */ + L1->ci->func = L1->top; + setnilvalue(L1->top++); /* `function' entry for this `ci' */ + L1->base = L1->ci->base = L1->top; + L1->ci->top = L1->top + LUA_MINSTACK; +} + + +static void freestack (lua_State *L, lua_State *L1) { + luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); + luaM_freearray(L, L1->stack, L1->stacksize, TValue); +} + + +/* +** open parts that may cause memory-allocation errors +*/ +static void f_luaopen (lua_State *L, void *ud) { + global_State *g = G(L); + UNUSED(ud); + stack_init(L, L); /* init stack */ + sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ + sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ + luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + luaT_init(L); + luaX_init(L); + luaS_fix(luaS_newliteral(L, MEMERRMSG)); + g->GCthreshold = 4*g->totalbytes; +} + + +static void preinit_state (lua_State *L, global_State *g) { + G(L) = g; + L->stack = NULL; + L->stacksize = 0; + L->errorJmp = NULL; + L->hook = NULL; + L->hookmask = 0; + L->basehookcount = 0; + L->allowhook = 1; + resethookcount(L); + L->openupval = NULL; + L->size_ci = 0; + L->nCcalls = L->baseCcalls = 0; + L->status = 0; + L->base_ci = L->ci = NULL; + L->savedpc = NULL; + L->errfunc = 0; + setnilvalue(gt(L)); +} + + +static void close_state (lua_State *L) { + global_State *g = G(L); + luaF_close(L, L->stack); /* close all upvalues for this thread */ + luaC_freeall(L); /* collect all objects */ + lua_assert(g->rootgc == obj2gco(L)); + lua_assert(g->strt.nuse == 0); + luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); + luaZ_freebuffer(L, &g->buff); + freestack(L, L); + lua_assert(g->totalbytes == sizeof(LG)); + (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); +} + + +lua_State *luaE_newthread (lua_State *L) { + lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); + luaC_link(L, obj2gco(L1), LUA_TTHREAD); + preinit_state(L1, G(L)); + stack_init(L1, L); /* init stack */ + setobj2n(L, gt(L1), gt(L)); /* share table of globals */ + L1->hookmask = L->hookmask; + L1->basehookcount = L->basehookcount; + L1->hook = L->hook; + resethookcount(L1); + lua_assert(iswhite(obj2gco(L1))); + return L1; +} + + +void luaE_freethread (lua_State *L, lua_State *L1) { + luaF_close(L1, L1->stack); /* close all upvalues for this thread */ + lua_assert(L1->openupval == NULL); + luai_userstatefree(L1); + freestack(L, L1); + luaM_freemem(L, fromstate(L1), state_size(lua_State)); +} + + +LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { + int i; + lua_State *L; + global_State *g; + void *l = (*f)(ud, NULL, 0, state_size(LG)); + if (l == NULL) return NULL; + L = tostate(l); + g = &((LG *)L)->g; + L->next = NULL; + L->tt = LUA_TTHREAD; + g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); + L->marked = luaC_white(g); + set2bits(L->marked, FIXEDBIT, SFIXEDBIT); + preinit_state(L, g); + g->frealloc = f; + g->ud = ud; + g->mainthread = L; + g->uvhead.u.l.prev = &g->uvhead; + g->uvhead.u.l.next = &g->uvhead; + g->GCthreshold = 0; /* mark it as unfinished state */ + g->strt.size = 0; + g->strt.nuse = 0; + g->strt.hash = NULL; + setnilvalue(registry(L)); + luaZ_initbuffer(L, &g->buff); + g->panic = NULL; + g->gcstate = GCSpause; + g->rootgc = obj2gco(L); + g->sweepstrgc = 0; + g->sweepgc = &g->rootgc; + g->gray = NULL; + g->grayagain = NULL; + g->weak = NULL; + g->tmudata = NULL; + g->totalbytes = sizeof(LG); + g->gcpause = LUAI_GCPAUSE; + g->gcstepmul = LUAI_GCMUL; + g->gcdept = 0; + for (i=0; imt[i] = NULL; + if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { + /* memory allocation error: free partial state */ + close_state(L); + L = NULL; + } + else + luai_userstateopen(L); + return L; +} + + +static void callallgcTM (lua_State *L, void *ud) { + UNUSED(ud); + luaC_callGCTM(L); /* call GC metamethods for all udata */ +} + + +LUA_API void lua_close (lua_State *L) { + L = G(L)->mainthread; /* only the main thread can be closed */ + lua_lock(L); + luaF_close(L, L->stack); /* close all upvalues for this thread */ + luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ + L->errfunc = 0; /* no error function during GC metamethods */ + do { /* repeat until no more errors */ + L->ci = L->base_ci; + L->base = L->top = L->ci->base; + L->nCcalls = L->baseCcalls = 0; + } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); + lua_assert(G(L)->tmudata == NULL); + luai_userstateclose(L); + close_state(L); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lstate.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lstate.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,169 @@ +/* +** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ +** Global State +** See Copyright Notice in lua.h +*/ + +#ifndef lstate_h +#define lstate_h + +#include "lua.h" + +#include "lobject.h" +#include "ltm.h" +#include "lzio.h" + + + +struct lua_longjmp; /* defined in ldo.c */ + + +/* table of globals */ +#define gt(L) (&L->l_gt) + +/* registry */ +#define registry(L) (&G(L)->l_registry) + + +/* extra stack space to handle TM calls and some other extras */ +#define EXTRA_STACK 5 + + +#define BASIC_CI_SIZE 8 + +#define BASIC_STACK_SIZE (2*LUA_MINSTACK) + + + +typedef struct stringtable { + GCObject **hash; + lu_int32 nuse; /* number of elements */ + int size; +} stringtable; + + +/* +** informations about a call +*/ +typedef struct CallInfo { + StkId base; /* base for this function */ + StkId func; /* function index in the stack */ + StkId top; /* top for this function */ + const Instruction *savedpc; + int nresults; /* expected number of results from this function */ + int tailcalls; /* number of tail calls lost under this entry */ +} CallInfo; + + + +#define curr_func(L) (clvalue(L->ci->func)) +#define ci_func(ci) (clvalue((ci)->func)) +#define f_isLua(ci) (!ci_func(ci)->c.isC) +#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) + + +/* +** `global state', shared by all threads of this state +*/ +typedef struct global_State { + stringtable strt; /* hash table for strings */ + lua_Alloc frealloc; /* function to reallocate memory */ + void *ud; /* auxiliary data to `frealloc' */ + lu_byte currentwhite; + lu_byte gcstate; /* state of garbage collector */ + int sweepstrgc; /* position of sweep in `strt' */ + GCObject *rootgc; /* list of all collectable objects */ + GCObject **sweepgc; /* position of sweep in `rootgc' */ + GCObject *gray; /* list of gray objects */ + GCObject *grayagain; /* list of objects to be traversed atomically */ + GCObject *weak; /* list of weak tables (to be cleared) */ + GCObject *tmudata; /* last element of list of userdata to be GC */ + Mbuffer buff; /* temporary buffer for string concatentation */ + lu_mem GCthreshold; + lu_mem totalbytes; /* number of bytes currently allocated */ + lu_mem estimate; /* an estimate of number of bytes actually in use */ + lu_mem gcdept; /* how much GC is `behind schedule' */ + int gcpause; /* size of pause between successive GCs */ + int gcstepmul; /* GC `granularity' */ + lua_CFunction panic; /* to be called in unprotected errors */ + TValue l_registry; + struct lua_State *mainthread; + UpVal uvhead; /* head of double-linked list of all open upvalues */ + struct Table *mt[NUM_TAGS]; /* metatables for basic types */ + TString *tmname[TM_N]; /* array with tag-method names */ +} global_State; + + +/* +** `per thread' state +*/ +struct lua_State { + CommonHeader; + lu_byte status; + StkId top; /* first free slot in the stack */ + StkId base; /* base of current function */ + global_State *l_G; + CallInfo *ci; /* call info for current function */ + const Instruction *savedpc; /* `savedpc' of current function */ + StkId stack_last; /* last free slot in the stack */ + StkId stack; /* stack base */ + CallInfo *end_ci; /* points after end of ci array*/ + CallInfo *base_ci; /* array of CallInfo's */ + int stacksize; + int size_ci; /* size of array `base_ci' */ + unsigned short nCcalls; /* number of nested C calls */ + unsigned short baseCcalls; /* nested C calls when resuming coroutine */ + lu_byte hookmask; + lu_byte allowhook; + int basehookcount; + int hookcount; + lua_Hook hook; + TValue l_gt; /* table of globals */ + TValue env; /* temporary place for environments */ + GCObject *openupval; /* list of open upvalues in this stack */ + GCObject *gclist; + struct lua_longjmp *errorJmp; /* current error recover point */ + ptrdiff_t errfunc; /* current error handling function (stack index) */ +}; + + +#define G(L) (L->l_G) + + +/* +** Union of all collectable objects +*/ +union GCObject { + GCheader gch; + union TString ts; + union Udata u; + union Closure cl; + struct Table h; + struct Proto p; + struct UpVal uv; + struct lua_State th; /* thread */ +}; + + +/* macros to convert a GCObject into a specific value */ +#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) +#define gco2ts(o) (&rawgco2ts(o)->tsv) +#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) +#define gco2u(o) (&rawgco2u(o)->uv) +#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) +#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) +#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) +#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) +#define ngcotouv(o) \ + check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) +#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) + +/* macro to convert any Lua object into a GCObject */ +#define obj2gco(v) (cast(GCObject *, (v))) + + +LUAI_FUNC lua_State *luaE_newthread (lua_State *L); +LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lstring.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lstring.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,111 @@ +/* +** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ +** String table (keeps all strings handled by Lua) +** See Copyright Notice in lua.h +*/ + + +#include + +#define lstring_c +#define LUA_CORE + +#include "lua.h" + +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" + + + +void luaS_resize (lua_State *L, int newsize) { + GCObject **newhash; + stringtable *tb; + int i; + if (G(L)->gcstate == GCSsweepstring) + return; /* cannot resize during GC traverse */ + newhash = luaM_newvector(L, newsize, GCObject *); + tb = &G(L)->strt; + for (i=0; isize; i++) { + GCObject *p = tb->hash[i]; + while (p) { /* for each node in the list */ + GCObject *next = p->gch.next; /* save next */ + unsigned int h = gco2ts(p)->hash; + int h1 = lmod(h, newsize); /* new position */ + lua_assert(cast_int(h%newsize) == lmod(h, newsize)); + p->gch.next = newhash[h1]; /* chain it */ + newhash[h1] = p; + p = next; + } + } + luaM_freearray(L, tb->hash, tb->size, TString *); + tb->size = newsize; + tb->hash = newhash; +} + + +static TString *newlstr (lua_State *L, const char *str, size_t l, + unsigned int h) { + TString *ts; + stringtable *tb; + if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) + luaM_toobig(L); + ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); + ts->tsv.len = l; + ts->tsv.hash = h; + ts->tsv.marked = luaC_white(G(L)); + ts->tsv.tt = LUA_TSTRING; + ts->tsv.reserved = 0; + memcpy(ts+1, str, l*sizeof(char)); + ((char *)(ts+1))[l] = '\0'; /* ending 0 */ + tb = &G(L)->strt; + h = lmod(h, tb->size); + ts->tsv.next = tb->hash[h]; /* chain new entry */ + tb->hash[h] = obj2gco(ts); + tb->nuse++; + if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) + luaS_resize(L, tb->size*2); /* too crowded */ + return ts; +} + + +TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { + GCObject *o; + unsigned int h = cast(unsigned int, l); /* seed */ + size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ + size_t l1; + for (l1=l; l1>=step; l1-=step) /* compute hash */ + h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); + for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; + o != NULL; + o = o->gch.next) { + TString *ts = rawgco2ts(o); + if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { + /* string may be dead */ + if (isdead(G(L), o)) changewhite(o); + return ts; + } + } + return newlstr(L, str, l, h); /* not found */ +} + + +Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { + Udata *u; + if (s > MAX_SIZET - sizeof(Udata)) + luaM_toobig(L); + u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); + u->uv.marked = luaC_white(G(L)); /* is not finalized */ + u->uv.tt = LUA_TUSERDATA; + u->uv.len = s; + u->uv.metatable = NULL; + u->uv.env = e; + /* chain it on udata list (after main thread) */ + u->uv.next = G(L)->mainthread->next; + G(L)->mainthread->next = obj2gco(u); + return u; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lstring.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lstring.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,31 @@ +/* +** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ +** String table (keep all strings handled by Lua) +** See Copyright Notice in lua.h +*/ + +#ifndef lstring_h +#define lstring_h + + +#include "lgc.h" +#include "lobject.h" +#include "lstate.h" + + +#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) + +#define sizeudata(u) (sizeof(union Udata)+(u)->len) + +#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) +#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ + (sizeof(s)/sizeof(char))-1)) + +#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) + +LUAI_FUNC void luaS_resize (lua_State *L, int newsize); +LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); +LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lstrlib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lstrlib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,869 @@ +/* +** $Id: lstrlib.c,v 1.132.1.4 2008/07/11 17:27:21 roberto Exp $ +** Standard library for string operations and pattern-matching +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include +#include + +#define lstrlib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +/* macro to `unsign' a character */ +#define uchar(c) ((unsigned char)(c)) + + + +static int str_len (lua_State *L) { + size_t l; + luaL_checklstring(L, 1, &l); + lua_pushinteger(L, l); + return 1; +} + + +static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) { + /* relative string position: negative means back from end */ + if (pos < 0) pos += (ptrdiff_t)len + 1; + return (pos >= 0) ? pos : 0; +} + + +static int str_sub (lua_State *L) { + size_t l; + const char *s = luaL_checklstring(L, 1, &l); + ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l); + ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l); + if (start < 1) start = 1; + if (end > (ptrdiff_t)l) end = (ptrdiff_t)l; + if (start <= end) + lua_pushlstring(L, s+start-1, end-start+1); + else lua_pushliteral(L, ""); + return 1; +} + + +static int str_reverse (lua_State *L) { + size_t l; + luaL_Buffer b; + const char *s = luaL_checklstring(L, 1, &l); + luaL_buffinit(L, &b); + while (l--) luaL_addchar(&b, s[l]); + luaL_pushresult(&b); + return 1; +} + + +static int str_lower (lua_State *L) { + size_t l; + size_t i; + luaL_Buffer b; + const char *s = luaL_checklstring(L, 1, &l); + luaL_buffinit(L, &b); + for (i=0; i 0) + luaL_addlstring(&b, s, l); + luaL_pushresult(&b); + return 1; +} + + +static int str_byte (lua_State *L) { + size_t l; + const char *s = luaL_checklstring(L, 1, &l); + ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l); + ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l); + int n, i; + if (posi <= 0) posi = 1; + if ((size_t)pose > l) pose = l; + if (posi > pose) return 0; /* empty interval; return no values */ + n = (int)(pose - posi + 1); + if (posi + n <= pose) /* overflow? */ + luaL_error(L, "string slice too long"); + luaL_checkstack(L, n, "string slice too long"); + for (i=0; i= ms->level || ms->capture[l].len == CAP_UNFINISHED) + return luaL_error(ms->L, "invalid capture index"); + return l; +} + + +static int capture_to_close (MatchState *ms) { + int level = ms->level; + for (level--; level>=0; level--) + if (ms->capture[level].len == CAP_UNFINISHED) return level; + return luaL_error(ms->L, "invalid pattern capture"); +} + + +static const char *classend (MatchState *ms, const char *p) { + switch (*p++) { + case L_ESC: { + if (*p == '\0') + luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); + return p+1; + } + case '[': { + if (*p == '^') p++; + do { /* look for a `]' */ + if (*p == '\0') + luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); + if (*(p++) == L_ESC && *p != '\0') + p++; /* skip escapes (e.g. `%]') */ + } while (*p != ']'); + return p+1; + } + default: { + return p; + } + } +} + + +static int match_class (int c, int cl) { + int res; + switch (tolower(cl)) { + case 'a' : res = isalpha(c); break; + case 'c' : res = iscntrl(c); break; + case 'd' : res = isdigit(c); break; + case 'l' : res = islower(c); break; + case 'p' : res = ispunct(c); break; + case 's' : res = isspace(c); break; + case 'u' : res = isupper(c); break; + case 'w' : res = isalnum(c); break; + case 'x' : res = isxdigit(c); break; + case 'z' : res = (c == 0); break; + default: return (cl == c); + } + return (islower(cl) ? res : !res); +} + + +static int matchbracketclass (int c, const char *p, const char *ec) { + int sig = 1; + if (*(p+1) == '^') { + sig = 0; + p++; /* skip the `^' */ + } + while (++p < ec) { + if (*p == L_ESC) { + p++; + if (match_class(c, uchar(*p))) + return sig; + } + else if ((*(p+1) == '-') && (p+2 < ec)) { + p+=2; + if (uchar(*(p-2)) <= c && c <= uchar(*p)) + return sig; + } + else if (uchar(*p) == c) return sig; + } + return !sig; +} + + +static int singlematch (int c, const char *p, const char *ep) { + switch (*p) { + case '.': return 1; /* matches any char */ + case L_ESC: return match_class(c, uchar(*(p+1))); + case '[': return matchbracketclass(c, p, ep-1); + default: return (uchar(*p) == c); + } +} + + +static const char *match (MatchState *ms, const char *s, const char *p); + + +static const char *matchbalance (MatchState *ms, const char *s, + const char *p) { + if (*p == 0 || *(p+1) == 0) + luaL_error(ms->L, "unbalanced pattern"); + if (*s != *p) return NULL; + else { + int b = *p; + int e = *(p+1); + int cont = 1; + while (++s < ms->src_end) { + if (*s == e) { + if (--cont == 0) return s+1; + } + else if (*s == b) cont++; + } + } + return NULL; /* string ends out of balance */ +} + + +static const char *max_expand (MatchState *ms, const char *s, + const char *p, const char *ep) { + ptrdiff_t i = 0; /* counts maximum expand for item */ + while ((s+i)src_end && singlematch(uchar(*(s+i)), p, ep)) + i++; + /* keeps trying to match with the maximum repetitions */ + while (i>=0) { + const char *res = match(ms, (s+i), ep+1); + if (res) return res; + i--; /* else didn't match; reduce 1 repetition to try again */ + } + return NULL; +} + + +static const char *min_expand (MatchState *ms, const char *s, + const char *p, const char *ep) { + for (;;) { + const char *res = match(ms, s, ep+1); + if (res != NULL) + return res; + else if (ssrc_end && singlematch(uchar(*s), p, ep)) + s++; /* try with one more repetition */ + else return NULL; + } +} + + +static const char *start_capture (MatchState *ms, const char *s, + const char *p, int what) { + const char *res; + int level = ms->level; + if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); + ms->capture[level].init = s; + ms->capture[level].len = what; + ms->level = level+1; + if ((res=match(ms, s, p)) == NULL) /* match failed? */ + ms->level--; /* undo capture */ + return res; +} + + +static const char *end_capture (MatchState *ms, const char *s, + const char *p) { + int l = capture_to_close(ms); + const char *res; + ms->capture[l].len = s - ms->capture[l].init; /* close capture */ + if ((res = match(ms, s, p)) == NULL) /* match failed? */ + ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ + return res; +} + + +static const char *match_capture (MatchState *ms, const char *s, int l) { + size_t len; + l = check_capture(ms, l); + len = ms->capture[l].len; + if ((size_t)(ms->src_end-s) >= len && + memcmp(ms->capture[l].init, s, len) == 0) + return s+len; + else return NULL; +} + + +static const char *match (MatchState *ms, const char *s, const char *p) { + init: /* using goto's to optimize tail recursion */ + switch (*p) { + case '(': { /* start capture */ + if (*(p+1) == ')') /* position capture? */ + return start_capture(ms, s, p+2, CAP_POSITION); + else + return start_capture(ms, s, p+1, CAP_UNFINISHED); + } + case ')': { /* end capture */ + return end_capture(ms, s, p+1); + } + case L_ESC: { + switch (*(p+1)) { + case 'b': { /* balanced string? */ + s = matchbalance(ms, s, p+2); + if (s == NULL) return NULL; + p+=4; goto init; /* else return match(ms, s, p+4); */ + } + case 'f': { /* frontier? */ + const char *ep; char previous; + p += 2; + if (*p != '[') + luaL_error(ms->L, "missing " LUA_QL("[") " after " + LUA_QL("%%f") " in pattern"); + ep = classend(ms, p); /* points to what is next */ + previous = (s == ms->src_init) ? '\0' : *(s-1); + if (matchbracketclass(uchar(previous), p, ep-1) || + !matchbracketclass(uchar(*s), p, ep-1)) return NULL; + p=ep; goto init; /* else return match(ms, s, ep); */ + } + default: { + if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */ + s = match_capture(ms, s, uchar(*(p+1))); + if (s == NULL) return NULL; + p+=2; goto init; /* else return match(ms, s, p+2) */ + } + goto dflt; /* case default */ + } + } + } + case '\0': { /* end of pattern */ + return s; /* match succeeded */ + } + case '$': { + if (*(p+1) == '\0') /* is the `$' the last char in pattern? */ + return (s == ms->src_end) ? s : NULL; /* check end of string */ + else goto dflt; + } + default: dflt: { /* it is a pattern item */ + const char *ep = classend(ms, p); /* points to what is next */ + int m = ssrc_end && singlematch(uchar(*s), p, ep); + switch (*ep) { + case '?': { /* optional */ + const char *res; + if (m && ((res=match(ms, s+1, ep+1)) != NULL)) + return res; + p=ep+1; goto init; /* else return match(ms, s, ep+1); */ + } + case '*': { /* 0 or more repetitions */ + return max_expand(ms, s, p, ep); + } + case '+': { /* 1 or more repetitions */ + return (m ? max_expand(ms, s+1, p, ep) : NULL); + } + case '-': { /* 0 or more repetitions (minimum) */ + return min_expand(ms, s, p, ep); + } + default: { + if (!m) return NULL; + s++; p=ep; goto init; /* else return match(ms, s+1, ep); */ + } + } + } + } +} + + + +static const char *lmemfind (const char *s1, size_t l1, + const char *s2, size_t l2) { + if (l2 == 0) return s1; /* empty strings are everywhere */ + else if (l2 > l1) return NULL; /* avoids a negative `l1' */ + else { + const char *init; /* to search for a `*s2' inside `s1' */ + l2--; /* 1st char will be checked by `memchr' */ + l1 = l1-l2; /* `s2' cannot be found after that */ + while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { + init++; /* 1st char is already checked */ + if (memcmp(init, s2+1, l2) == 0) + return init-1; + else { /* correct `l1' and `s1' to try again */ + l1 -= init-s1; + s1 = init; + } + } + return NULL; /* not found */ + } +} + + +static void push_onecapture (MatchState *ms, int i, const char *s, + const char *e) { + if (i >= ms->level) { + if (i == 0) /* ms->level == 0, too */ + lua_pushlstring(ms->L, s, e - s); /* add whole match */ + else + luaL_error(ms->L, "invalid capture index"); + } + else { + ptrdiff_t l = ms->capture[i].len; + if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); + if (l == CAP_POSITION) + lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); + else + lua_pushlstring(ms->L, ms->capture[i].init, l); + } +} + + +static int push_captures (MatchState *ms, const char *s, const char *e) { + int i; + int nlevels = (ms->level == 0 && s) ? 1 : ms->level; + luaL_checkstack(ms->L, nlevels, "too many captures"); + for (i = 0; i < nlevels; i++) + push_onecapture(ms, i, s, e); + return nlevels; /* number of strings pushed */ +} + + +static int str_find_aux (lua_State *L, int find) { + size_t l1, l2; + const char *s = luaL_checklstring(L, 1, &l1); + const char *p = luaL_checklstring(L, 2, &l2); + ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; + if (init < 0) init = 0; + else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; + if (find && (lua_toboolean(L, 4) || /* explicit request? */ + strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ + /* do a plain search */ + const char *s2 = lmemfind(s+init, l1-init, p, l2); + if (s2) { + lua_pushinteger(L, s2-s+1); + lua_pushinteger(L, s2-s+l2); + return 2; + } + } + else { + MatchState ms; + int anchor = (*p == '^') ? (p++, 1) : 0; + const char *s1=s+init; + ms.L = L; + ms.src_init = s; + ms.src_end = s+l1; + do { + const char *res; + ms.level = 0; + if ((res=match(&ms, s1, p)) != NULL) { + if (find) { + lua_pushinteger(L, s1-s+1); /* start */ + lua_pushinteger(L, res-s); /* end */ + return push_captures(&ms, NULL, 0) + 2; + } + else + return push_captures(&ms, s1, res); + } + } while (s1++ < ms.src_end && !anchor); + } + lua_pushnil(L); /* not found */ + return 1; +} + + +static int str_find (lua_State *L) { + return str_find_aux(L, 1); +} + + +static int str_match (lua_State *L) { + return str_find_aux(L, 0); +} + + +static int gmatch_aux (lua_State *L) { + MatchState ms; + size_t ls; + const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); + const char *p = lua_tostring(L, lua_upvalueindex(2)); + const char *src; + ms.L = L; + ms.src_init = s; + ms.src_end = s+ls; + for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); + src <= ms.src_end; + src++) { + const char *e; + ms.level = 0; + if ((e = match(&ms, src, p)) != NULL) { + lua_Integer newstart = e-s; + if (e == src) newstart++; /* empty match? go at least one position */ + lua_pushinteger(L, newstart); + lua_replace(L, lua_upvalueindex(3)); + return push_captures(&ms, src, e); + } + } + return 0; /* not found */ +} + + +static int gmatch (lua_State *L) { + luaL_checkstring(L, 1); + luaL_checkstring(L, 2); + lua_settop(L, 2); + lua_pushinteger(L, 0); + lua_pushcclosure(L, gmatch_aux, 3); + return 1; +} + + +static int gfind_nodef (lua_State *L) { + return luaL_error(L, LUA_QL("string.gfind") " was renamed to " + LUA_QL("string.gmatch")); +} + + +static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, + const char *e) { + size_t l, i; + const char *news = lua_tolstring(ms->L, 3, &l); + for (i = 0; i < l; i++) { + if (news[i] != L_ESC) + luaL_addchar(b, news[i]); + else { + i++; /* skip ESC */ + if (!isdigit(uchar(news[i]))) + luaL_addchar(b, news[i]); + else if (news[i] == '0') + luaL_addlstring(b, s, e - s); + else { + push_onecapture(ms, news[i] - '1', s, e); + luaL_addvalue(b); /* add capture to accumulated result */ + } + } + } +} + + +static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, + const char *e) { + lua_State *L = ms->L; + switch (lua_type(L, 3)) { + case LUA_TNUMBER: + case LUA_TSTRING: { + add_s(ms, b, s, e); + return; + } + case LUA_TFUNCTION: { + int n; + lua_pushvalue(L, 3); + n = push_captures(ms, s, e); + lua_call(L, n, 1); + break; + } + case LUA_TTABLE: { + push_onecapture(ms, 0, s, e); + lua_gettable(L, 3); + break; + } + } + if (!lua_toboolean(L, -1)) { /* nil or false? */ + lua_pop(L, 1); + lua_pushlstring(L, s, e - s); /* keep original text */ + } + else if (!lua_isstring(L, -1)) + luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); + luaL_addvalue(b); /* add result to accumulator */ +} + + +static int str_gsub (lua_State *L) { + size_t srcl; + const char *src = luaL_checklstring(L, 1, &srcl); + const char *p = luaL_checkstring(L, 2); + int tr = lua_type(L, 3); + int max_s = luaL_optint(L, 4, srcl+1); + int anchor = (*p == '^') ? (p++, 1) : 0; + int n = 0; + MatchState ms; + luaL_Buffer b; + luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || + tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, + "string/function/table expected"); + luaL_buffinit(L, &b); + ms.L = L; + ms.src_init = src; + ms.src_end = src+srcl; + while (n < max_s) { + const char *e; + ms.level = 0; + e = match(&ms, src, p); + if (e) { + n++; + add_value(&ms, &b, src, e); + } + if (e && e>src) /* non empty match? */ + src = e; /* skip it */ + else if (src < ms.src_end) + luaL_addchar(&b, *src++); + else break; + if (anchor) break; + } + luaL_addlstring(&b, src, ms.src_end-src); + luaL_pushresult(&b); + lua_pushinteger(L, n); /* number of substitutions */ + return 2; +} + +/* }====================================================== */ + + +/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ +#define MAX_ITEM 512 +/* valid flags in a format specification */ +#define FLAGS "-+ #0" +/* +** maximum size of each format specification (such as '%-099.99d') +** (+10 accounts for %99.99x plus margin of error) +*/ +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) + + +static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { + size_t l; + const char *s = luaL_checklstring(L, arg, &l); + luaL_addchar(b, '"'); + while (l--) { + switch (*s) { + case '"': case '\\': case '\n': { + luaL_addchar(b, '\\'); + luaL_addchar(b, *s); + break; + } + case '\r': { + luaL_addlstring(b, "\\r", 2); + break; + } + case '\0': { + luaL_addlstring(b, "\\000", 4); + break; + } + default: { + luaL_addchar(b, *s); + break; + } + } + s++; + } + luaL_addchar(b, '"'); +} + +static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { + const char *p = strfrmt; + while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ + if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) + luaL_error(L, "invalid format (repeated flags)"); + if (isdigit(uchar(*p))) p++; /* skip width */ + if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ + if (*p == '.') { + p++; + if (isdigit(uchar(*p))) p++; /* skip precision */ + if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ + } + if (isdigit(uchar(*p))) + luaL_error(L, "invalid format (width or precision too long)"); + *(form++) = '%'; + strncpy(form, strfrmt, p - strfrmt + 1); + form += p - strfrmt + 1; + *form = '\0'; + return p; +} + + +static void addintlen (char *form) { + size_t l = strlen(form); + char spec = form[l - 1]; + strcpy(form + l - 1, LUA_INTFRMLEN); + form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; + form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; +} + + +static int str_format (lua_State *L) { + int arg = 1; + size_t sfl; + const char *strfrmt = luaL_checklstring(L, arg, &sfl); + const char *strfrmt_end = strfrmt+sfl; + luaL_Buffer b; + luaL_buffinit(L, &b); + while (strfrmt < strfrmt_end) { + if (*strfrmt != L_ESC) + luaL_addchar(&b, *strfrmt++); + else if (*++strfrmt == L_ESC) + luaL_addchar(&b, *strfrmt++); /* %% */ + else { /* format item */ + char form[MAX_FORMAT]; /* to store the format (`%...') */ + char buff[MAX_ITEM]; /* to store the formatted item */ + arg++; + strfrmt = scanformat(L, strfrmt, form); + switch (*strfrmt++) { + case 'c': { + sprintf(buff, form, (int)luaL_checknumber(L, arg)); + break; + } + case 'd': case 'i': { + addintlen(form); + sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); + break; + } + case 'o': case 'u': case 'x': case 'X': { + addintlen(form); + sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); + break; + } + case 'e': case 'E': case 'f': + case 'g': case 'G': { + sprintf(buff, form, (double)luaL_checknumber(L, arg)); + break; + } + case 'q': { + addquoted(L, &b, arg); + continue; /* skip the 'addsize' at the end */ + } + case 's': { + size_t l; + const char *s = luaL_checklstring(L, arg, &l); + if (!strchr(form, '.') && l >= 100) { + /* no precision and string is too long to be formatted; + keep original string */ + lua_pushvalue(L, arg); + luaL_addvalue(&b); + continue; /* skip the `addsize' at the end */ + } + else { + sprintf(buff, form, s); + break; + } + } + default: { /* also treat cases `pnLlh' */ + return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " + LUA_QL("format"), *(strfrmt - 1)); + } + } + luaL_addlstring(&b, buff, strlen(buff)); + } + } + luaL_pushresult(&b); + return 1; +} + + +static const luaL_Reg strlib[] = { + {"byte", str_byte}, + {"char", str_char}, + {"dump", str_dump}, + {"find", str_find}, + {"format", str_format}, + {"gfind", gfind_nodef}, + {"gmatch", gmatch}, + {"gsub", str_gsub}, + {"len", str_len}, + {"lower", str_lower}, + {"match", str_match}, + {"rep", str_rep}, + {"reverse", str_reverse}, + {"sub", str_sub}, + {"upper", str_upper}, + {NULL, NULL} +}; + + +static void createmetatable (lua_State *L) { + lua_createtable(L, 0, 1); /* create metatable for strings */ + lua_pushliteral(L, ""); /* dummy string */ + lua_pushvalue(L, -2); + lua_setmetatable(L, -2); /* set string metatable */ + lua_pop(L, 1); /* pop dummy string */ + lua_pushvalue(L, -2); /* string library... */ + lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */ + lua_pop(L, 1); /* pop metatable */ +} + + +/* +** Open string library +*/ +LUALIB_API int luaopen_string (lua_State *L) { + luaL_register(L, LUA_STRLIBNAME, strlib); +#if defined(LUA_COMPAT_GFIND) + lua_getfield(L, -1, "gmatch"); + lua_setfield(L, -2, "gfind"); +#endif + createmetatable(L); + return 1; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ltable.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ltable.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,588 @@ +/* +** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $ +** Lua tables (hash) +** See Copyright Notice in lua.h +*/ + + +/* +** Implementation of tables (aka arrays, objects, or hash tables). +** Tables keep its elements in two parts: an array part and a hash part. +** Non-negative integer keys are all candidates to be kept in the array +** part. The actual size of the array is the largest `n' such that at +** least half the slots between 0 and n are in use. +** Hash uses a mix of chained scatter table with Brent's variation. +** A main invariant of these tables is that, if an element is not +** in its main position (i.e. the `original' position that its hash gives +** to it), then the colliding element is in its own main position. +** Hence even when the load factor reaches 100%, performance remains good. +*/ + +#include +#include + +#define ltable_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "ltable.h" + + +/* +** max size of array part is 2^MAXBITS +*/ +#if LUAI_BITSINT > 26 +#define MAXBITS 26 +#else +#define MAXBITS (LUAI_BITSINT-2) +#endif + +#define MAXASIZE (1 << MAXBITS) + + +#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) + +#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) +#define hashboolean(t,p) hashpow2(t, p) + + +/* +** for some types, it is better to avoid modulus by power of 2, as +** they tend to have many 2 factors. +*/ +#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) + + +#define hashpointer(t,p) hashmod(t, IntPoint(p)) + + +/* +** number of ints inside a lua_Number +*/ +#define numints cast_int(sizeof(lua_Number)/sizeof(int)) + + + +#define dummynode (&dummynode_) + +static const Node dummynode_ = { + {{NULL}, LUA_TNIL}, /* value */ + {{{NULL}, LUA_TNIL, NULL}} /* key */ +}; + + +/* +** hash for lua_Numbers +*/ +static Node *hashnum (const Table *t, lua_Number n) { + unsigned int a[numints]; + int i; + if (luai_numeq(n, 0)) /* avoid problems with -0 */ + return gnode(t, 0); + memcpy(a, &n, sizeof(a)); + for (i = 1; i < numints; i++) a[0] += a[i]; + return hashmod(t, a[0]); +} + + + +/* +** returns the `main' position of an element in a table (that is, the index +** of its hash value) +*/ +static Node *mainposition (const Table *t, const TValue *key) { + switch (ttype(key)) { + case LUA_TNUMBER: + return hashnum(t, nvalue(key)); + case LUA_TSTRING: + return hashstr(t, rawtsvalue(key)); + case LUA_TBOOLEAN: + return hashboolean(t, bvalue(key)); + case LUA_TLIGHTUSERDATA: + return hashpointer(t, pvalue(key)); + default: + return hashpointer(t, gcvalue(key)); + } +} + + +/* +** returns the index for `key' if `key' is an appropriate key to live in +** the array part of the table, -1 otherwise. +*/ +static int arrayindex (const TValue *key) { + if (ttisnumber(key)) { + lua_Number n = nvalue(key); + int k; + lua_number2int(k, n); + if (luai_numeq(cast_num(k), n)) + return k; + } + return -1; /* `key' did not match some condition */ +} + + +/* +** returns the index of a `key' for table traversals. First goes all +** elements in the array part, then elements in the hash part. The +** beginning of a traversal is signalled by -1. +*/ +static int findindex (lua_State *L, Table *t, StkId key) { + int i; + if (ttisnil(key)) return -1; /* first iteration */ + i = arrayindex(key); + if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ + return i-1; /* yes; that's the index (corrected to C) */ + else { + Node *n = mainposition(t, key); + do { /* check whether `key' is somewhere in the chain */ + /* key may be dead already, but it is ok to use it in `next' */ + if (luaO_rawequalObj(key2tval(n), key) || + (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && + gcvalue(gkey(n)) == gcvalue(key))) { + i = cast_int(n - gnode(t, 0)); /* key index in hash table */ + /* hash elements are numbered after array ones */ + return i + t->sizearray; + } + else n = gnext(n); + } while (n); + luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ + return 0; /* to avoid warnings */ + } +} + + +int luaH_next (lua_State *L, Table *t, StkId key) { + int i = findindex(L, t, key); /* find original element */ + for (i++; i < t->sizearray; i++) { /* try first array part */ + if (!ttisnil(&t->array[i])) { /* a non-nil value? */ + setnvalue(key, cast_num(i+1)); + setobj2s(L, key+1, &t->array[i]); + return 1; + } + } + for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ + if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ + setobj2s(L, key, key2tval(gnode(t, i))); + setobj2s(L, key+1, gval(gnode(t, i))); + return 1; + } + } + return 0; /* no more elements */ +} + + +/* +** {============================================================= +** Rehash +** ============================================================== +*/ + + +static int computesizes (int nums[], int *narray) { + int i; + int twotoi; /* 2^i */ + int a = 0; /* number of elements smaller than 2^i */ + int na = 0; /* number of elements to go to array part */ + int n = 0; /* optimal size for array part */ + for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { + if (nums[i] > 0) { + a += nums[i]; + if (a > twotoi/2) { /* more than half elements present? */ + n = twotoi; /* optimal size (till now) */ + na = a; /* all elements smaller than n will go to array part */ + } + } + if (a == *narray) break; /* all elements already counted */ + } + *narray = n; + lua_assert(*narray/2 <= na && na <= *narray); + return na; +} + + +static int countint (const TValue *key, int *nums) { + int k = arrayindex(key); + if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ + nums[ceillog2(k)]++; /* count as such */ + return 1; + } + else + return 0; +} + + +static int numusearray (const Table *t, int *nums) { + int lg; + int ttlg; /* 2^lg */ + int ause = 0; /* summation of `nums' */ + int i = 1; /* count to traverse all array keys */ + for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ + int lc = 0; /* counter */ + int lim = ttlg; + if (lim > t->sizearray) { + lim = t->sizearray; /* adjust upper limit */ + if (i > lim) + break; /* no more elements to count */ + } + /* count elements in range (2^(lg-1), 2^lg] */ + for (; i <= lim; i++) { + if (!ttisnil(&t->array[i-1])) + lc++; + } + nums[lg] += lc; + ause += lc; + } + return ause; +} + + +static int numusehash (const Table *t, int *nums, int *pnasize) { + int totaluse = 0; /* total number of elements */ + int ause = 0; /* summation of `nums' */ + int i = sizenode(t); + while (i--) { + Node *n = &t->node[i]; + if (!ttisnil(gval(n))) { + ause += countint(key2tval(n), nums); + totaluse++; + } + } + *pnasize += ause; + return totaluse; +} + + +static void setarrayvector (lua_State *L, Table *t, int size) { + int i; + luaM_reallocvector(L, t->array, t->sizearray, size, TValue); + for (i=t->sizearray; iarray[i]); + t->sizearray = size; +} + + +static void setnodevector (lua_State *L, Table *t, int size) { + int lsize; + if (size == 0) { /* no elements to hash part? */ + t->node = cast(Node *, dummynode); /* use common `dummynode' */ + lsize = 0; + } + else { + int i; + lsize = ceillog2(size); + if (lsize > MAXBITS) + luaG_runerror(L, "table overflow"); + size = twoto(lsize); + t->node = luaM_newvector(L, size, Node); + for (i=0; ilsizenode = cast_byte(lsize); + t->lastfree = gnode(t, size); /* all positions are free */ +} + + +static void resize (lua_State *L, Table *t, int nasize, int nhsize) { + int i; + int oldasize = t->sizearray; + int oldhsize = t->lsizenode; + Node *nold = t->node; /* save old hash ... */ + if (nasize > oldasize) /* array part must grow? */ + setarrayvector(L, t, nasize); + /* create new hash part with appropriate size */ + setnodevector(L, t, nhsize); + if (nasize < oldasize) { /* array part must shrink? */ + t->sizearray = nasize; + /* re-insert elements from vanishing slice */ + for (i=nasize; iarray[i])) + setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]); + } + /* shrink array */ + luaM_reallocvector(L, t->array, oldasize, nasize, TValue); + } + /* re-insert elements from hash part */ + for (i = twoto(oldhsize) - 1; i >= 0; i--) { + Node *old = nold+i; + if (!ttisnil(gval(old))) + setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); + } + if (nold != dummynode) + luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ +} + + +void luaH_resizearray (lua_State *L, Table *t, int nasize) { + int nsize = (t->node == dummynode) ? 0 : sizenode(t); + resize(L, t, nasize, nsize); +} + + +static void rehash (lua_State *L, Table *t, const TValue *ek) { + int nasize, na; + int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ + int i; + int totaluse; + for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ + nasize = numusearray(t, nums); /* count keys in array part */ + totaluse = nasize; /* all those keys are integer keys */ + totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ + /* count extra key */ + nasize += countint(ek, nums); + totaluse++; + /* compute new size for array part */ + na = computesizes(nums, &nasize); + /* resize the table to new computed sizes */ + resize(L, t, nasize, totaluse - na); +} + + + +/* +** }============================================================= +*/ + + +Table *luaH_new (lua_State *L, int narray, int nhash) { + Table *t = luaM_new(L, Table); + luaC_link(L, obj2gco(t), LUA_TTABLE); + t->metatable = NULL; + t->flags = cast_byte(~0); + /* temporary values (kept only if some malloc fails) */ + t->array = NULL; + t->sizearray = 0; + t->lsizenode = 0; + t->node = cast(Node *, dummynode); + setarrayvector(L, t, narray); + setnodevector(L, t, nhash); + return t; +} + + +void luaH_free (lua_State *L, Table *t) { + if (t->node != dummynode) + luaM_freearray(L, t->node, sizenode(t), Node); + luaM_freearray(L, t->array, t->sizearray, TValue); + luaM_free(L, t); +} + + +static Node *getfreepos (Table *t) { + while (t->lastfree-- > t->node) { + if (ttisnil(gkey(t->lastfree))) + return t->lastfree; + } + return NULL; /* could not find a free place */ +} + + + +/* +** inserts a new key into a hash table; first, check whether key's main +** position is free. If not, check whether colliding node is in its main +** position or not: if it is not, move colliding node to an empty place and +** put new key in its main position; otherwise (colliding node is in its main +** position), new key goes to an empty position. +*/ +static TValue *newkey (lua_State *L, Table *t, const TValue *key) { + Node *mp = mainposition(t, key); + if (!ttisnil(gval(mp)) || mp == dummynode) { + Node *othern; + Node *n = getfreepos(t); /* get a free place */ + if (n == NULL) { /* cannot find a free place? */ + rehash(L, t, key); /* grow table */ + return luaH_set(L, t, key); /* re-insert key into grown table */ + } + lua_assert(n != dummynode); + othern = mainposition(t, key2tval(mp)); + if (othern != mp) { /* is colliding node out of its main position? */ + /* yes; move colliding node into free position */ + while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ + gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ + *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ + gnext(mp) = NULL; /* now `mp' is free */ + setnilvalue(gval(mp)); + } + else { /* colliding node is in its own main position */ + /* new node will go into free position */ + gnext(n) = gnext(mp); /* chain new position */ + gnext(mp) = n; + mp = n; + } + } + gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; + luaC_barriert(L, t, key); + lua_assert(ttisnil(gval(mp))); + return gval(mp); +} + + +/* +** search function for integers +*/ +const TValue *luaH_getnum (Table *t, int key) { + /* (1 <= key && key <= t->sizearray) */ + if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) + return &t->array[key-1]; + else { + lua_Number nk = cast_num(key); + Node *n = hashnum(t, nk); + do { /* check whether `key' is somewhere in the chain */ + if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) + return gval(n); /* that's it */ + else n = gnext(n); + } while (n); + return luaO_nilobject; + } +} + + +/* +** search function for strings +*/ +const TValue *luaH_getstr (Table *t, TString *key) { + Node *n = hashstr(t, key); + do { /* check whether `key' is somewhere in the chain */ + if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) + return gval(n); /* that's it */ + else n = gnext(n); + } while (n); + return luaO_nilobject; +} + + +/* +** main search function +*/ +const TValue *luaH_get (Table *t, const TValue *key) { + switch (ttype(key)) { + case LUA_TNIL: return luaO_nilobject; + case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key)); + case LUA_TNUMBER: { + int k; + lua_Number n = nvalue(key); + lua_number2int(k, n); + if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ + return luaH_getnum(t, k); /* use specialized version */ + /* else go through */ + } + default: { + Node *n = mainposition(t, key); + do { /* check whether `key' is somewhere in the chain */ + if (luaO_rawequalObj(key2tval(n), key)) + return gval(n); /* that's it */ + else n = gnext(n); + } while (n); + return luaO_nilobject; + } + } +} + + +TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { + const TValue *p = luaH_get(t, key); + t->flags = 0; + if (p != luaO_nilobject) + return cast(TValue *, p); + else { + if (ttisnil(key)) luaG_runerror(L, "table index is nil"); + else if (ttisnumber(key) && luai_numisnan(nvalue(key))) + luaG_runerror(L, "table index is NaN"); + return newkey(L, t, key); + } +} + + +TValue *luaH_setnum (lua_State *L, Table *t, int key) { + const TValue *p = luaH_getnum(t, key); + if (p != luaO_nilobject) + return cast(TValue *, p); + else { + TValue k; + setnvalue(&k, cast_num(key)); + return newkey(L, t, &k); + } +} + + +TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { + const TValue *p = luaH_getstr(t, key); + if (p != luaO_nilobject) + return cast(TValue *, p); + else { + TValue k; + setsvalue(L, &k, key); + return newkey(L, t, &k); + } +} + + +static int unbound_search (Table *t, unsigned int j) { + unsigned int i = j; /* i is zero or a present index */ + j++; + /* find `i' and `j' such that i is present and j is not */ + while (!ttisnil(luaH_getnum(t, j))) { + i = j; + j *= 2; + if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ + /* table was built with bad purposes: resort to linear search */ + i = 1; + while (!ttisnil(luaH_getnum(t, i))) i++; + return i - 1; + } + } + /* now do a binary search between them */ + while (j - i > 1) { + unsigned int m = (i+j)/2; + if (ttisnil(luaH_getnum(t, m))) j = m; + else i = m; + } + return i; +} + + +/* +** Try to find a boundary in table `t'. A `boundary' is an integer index +** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). +*/ +int luaH_getn (Table *t) { + unsigned int j = t->sizearray; + if (j > 0 && ttisnil(&t->array[j - 1])) { + /* there is a boundary in the array part: (binary) search for it */ + unsigned int i = 0; + while (j - i > 1) { + unsigned int m = (i+j)/2; + if (ttisnil(&t->array[m - 1])) j = m; + else i = m; + } + return i; + } + /* else must find a boundary in hash part */ + else if (t->node == dummynode) /* hash part is empty? */ + return j; /* that is easy... */ + else return unbound_search(t, j); +} + + + +#if defined(LUA_DEBUG) + +Node *luaH_mainposition (const Table *t, const TValue *key) { + return mainposition(t, key); +} + +int luaH_isdummy (Node *n) { return n == dummynode; } + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ltable.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ltable.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,40 @@ +/* +** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua tables (hash) +** See Copyright Notice in lua.h +*/ + +#ifndef ltable_h +#define ltable_h + +#include "lobject.h" + + +#define gnode(t,i) (&(t)->node[i]) +#define gkey(n) (&(n)->i_key.nk) +#define gval(n) (&(n)->i_val) +#define gnext(n) ((n)->i_key.nk.next) + +#define key2tval(n) (&(n)->i_key.tvk) + + +LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); +LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); +LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); +LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); +LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); +LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); +LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); +LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); +LUAI_FUNC void luaH_free (lua_State *L, Table *t); +LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); +LUAI_FUNC int luaH_getn (Table *t); + + +#if defined(LUA_DEBUG) +LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); +LUAI_FUNC int luaH_isdummy (Node *n); +#endif + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ltablib.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ltablib.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,287 @@ +/* +** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $ +** Library for Table Manipulation +** See Copyright Notice in lua.h +*/ + + +#include + +#define ltablib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) + + +static int foreachi (lua_State *L) { + int i; + int n = aux_getn(L, 1); + luaL_checktype(L, 2, LUA_TFUNCTION); + for (i=1; i <= n; i++) { + lua_pushvalue(L, 2); /* function */ + lua_pushinteger(L, i); /* 1st argument */ + lua_rawgeti(L, 1, i); /* 2nd argument */ + lua_call(L, 2, 1); + if (!lua_isnil(L, -1)) + return 1; + lua_pop(L, 1); /* remove nil result */ + } + return 0; +} + + +static int foreach (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checktype(L, 2, LUA_TFUNCTION); + lua_pushnil(L); /* first key */ + while (lua_next(L, 1)) { + lua_pushvalue(L, 2); /* function */ + lua_pushvalue(L, -3); /* key */ + lua_pushvalue(L, -3); /* value */ + lua_call(L, 2, 1); + if (!lua_isnil(L, -1)) + return 1; + lua_pop(L, 2); /* remove value and result */ + } + return 0; +} + + +static int maxn (lua_State *L) { + lua_Number max = 0; + luaL_checktype(L, 1, LUA_TTABLE); + lua_pushnil(L); /* first key */ + while (lua_next(L, 1)) { + lua_pop(L, 1); /* remove value */ + if (lua_type(L, -1) == LUA_TNUMBER) { + lua_Number v = lua_tonumber(L, -1); + if (v > max) max = v; + } + } + lua_pushnumber(L, max); + return 1; +} + + +static int getn (lua_State *L) { + lua_pushinteger(L, aux_getn(L, 1)); + return 1; +} + + +static int setn (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); +#ifndef luaL_setn + luaL_setn(L, 1, luaL_checkint(L, 2)); +#else + luaL_error(L, LUA_QL("setn") " is obsolete"); +#endif + lua_pushvalue(L, 1); + return 1; +} + + +static int tinsert (lua_State *L) { + int e = aux_getn(L, 1) + 1; /* first empty element */ + int pos; /* where to insert new element */ + switch (lua_gettop(L)) { + case 2: { /* called with only 2 arguments */ + pos = e; /* insert new element at the end */ + break; + } + case 3: { + int i; + pos = luaL_checkint(L, 2); /* 2nd argument is the position */ + if (pos > e) e = pos; /* `grow' array if necessary */ + for (i = e; i > pos; i--) { /* move up elements */ + lua_rawgeti(L, 1, i-1); + lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ + } + break; + } + default: { + return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); + } + } + luaL_setn(L, 1, e); /* new size */ + lua_rawseti(L, 1, pos); /* t[pos] = v */ + return 0; +} + + +static int tremove (lua_State *L) { + int e = aux_getn(L, 1); + int pos = luaL_optint(L, 2, e); + if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ + return 0; /* nothing to remove */ + luaL_setn(L, 1, e - 1); /* t.n = n-1 */ + lua_rawgeti(L, 1, pos); /* result = t[pos] */ + for ( ;pos= P */ + while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { + if (i>u) luaL_error(L, "invalid order function for sorting"); + lua_pop(L, 1); /* remove a[i] */ + } + /* repeat --j until a[j] <= P */ + while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { + if (j + +#define ltm_c +#define LUA_CORE + +#include "lua.h" + +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" + + + +const char *const luaT_typenames[] = { + "nil", "boolean", "userdata", "number", + "string", "table", "function", "userdata", "thread", + "proto", "upval" +}; + + +void luaT_init (lua_State *L) { + static const char *const luaT_eventname[] = { /* ORDER TM */ + "__index", "__newindex", + "__gc", "__mode", "__eq", + "__add", "__sub", "__mul", "__div", "__mod", + "__pow", "__unm", "__len", "__lt", "__le", + "__concat", "__call" + }; + int i; + for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); + luaS_fix(G(L)->tmname[i]); /* never collect these names */ + } +} + + +/* +** function to be used with macro "fasttm": optimized for absence of +** tag methods +*/ +const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { + const TValue *tm = luaH_getstr(events, ename); + lua_assert(event <= TM_EQ); + if (ttisnil(tm)) { /* no tag method? */ + events->flags |= cast_byte(1u<metatable; + break; + case LUA_TUSERDATA: + mt = uvalue(o)->metatable; + break; + default: + mt = G(L)->mt[ttype(o)]; + } + return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/ltm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/ltm.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,54 @@ +/* +** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ +** Tag methods +** See Copyright Notice in lua.h +*/ + +#ifndef ltm_h +#define ltm_h + + +#include "lobject.h" + + +/* +* WARNING: if you change the order of this enumeration, +* grep "ORDER TM" +*/ +typedef enum { + TM_INDEX, + TM_NEWINDEX, + TM_GC, + TM_MODE, + TM_EQ, /* last tag method with `fast' access */ + TM_ADD, + TM_SUB, + TM_MUL, + TM_DIV, + TM_MOD, + TM_POW, + TM_UNM, + TM_LEN, + TM_LT, + TM_LE, + TM_CONCAT, + TM_CALL, + TM_N /* number of elements in the enum */ +} TMS; + + + +#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ + ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) + +#define fasttm(l,et,e) gfasttm(G(l), et, e) + +LUAI_DATA const char *const luaT_typenames[]; + + +LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); +LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, + TMS event); +LUAI_FUNC void luaT_init (lua_State *L); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lua.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lua.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,388 @@ +/* +** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ +** Lua - An Extensible Extension Language +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include +#include + + +#include "luaconf.h" + + +#define LUA_VERSION "Lua 5.1" +#define LUA_RELEASE "Lua 5.1.4" +#define LUA_VERSION_NUM 501 +#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" + + +/* mark for precompiled code (`Lua') */ +#define LUA_SIGNATURE "\033Lua" + +/* option for multiple returns in `lua_pcall' and `lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** pseudo-indices +*/ +#define LUA_REGISTRYINDEX (-10000) +#define LUA_ENVIRONINDEX (-10001) +#define LUA_GLOBALSINDEX (-10002) +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) + + +/* thread status; 0 is OK */ +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRERR 5 + + +typedef struct lua_State lua_State; + +typedef int (*lua_CFunction) (lua_State *L); + + +/* +** functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); + + +/* +** prototype for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* +** generic extra include file +*/ +#if defined(LUA_USER_H) +#include LUA_USER_H +#endif + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + + + +/* +** state manipulation +*/ +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API void (lua_close) (lua_State *L); +LUA_API lua_State *(lua_newthread) (lua_State *L); + +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); + + +/* +** basic stack manipulation +*/ +LUA_API int (lua_gettop) (lua_State *L); +LUA_API void (lua_settop) (lua_State *L, int idx); +LUA_API void (lua_pushvalue) (lua_State *L, int idx); +LUA_API void (lua_remove) (lua_State *L, int idx); +LUA_API void (lua_insert) (lua_State *L, int idx); +LUA_API void (lua_replace) (lua_State *L, int idx); +LUA_API int (lua_checkstack) (lua_State *L, int sz); + +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int (lua_isnumber) (lua_State *L, int idx); +LUA_API int (lua_isstring) (lua_State *L, int idx); +LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isuserdata) (lua_State *L, int idx); +LUA_API int (lua_type) (lua_State *L, int idx); +LUA_API const char *(lua_typename) (lua_State *L, int tp); + +LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); + +LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); +LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); +LUA_API int (lua_toboolean) (lua_State *L, int idx); +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); +LUA_API size_t (lua_objlen) (lua_State *L, int idx); +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); +LUA_API void *(lua_touserdata) (lua_State *L, int idx); +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); +LUA_API const void *(lua_topointer) (lua_State *L, int idx); + + +/* +** push functions (C -> stack) +*/ +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API void (lua_pushstring) (lua_State *L, const char *s); +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); +LUA_API void (lua_pushboolean) (lua_State *L, int b); +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); +LUA_API int (lua_pushthread) (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API void (lua_gettable) (lua_State *L, int idx); +LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawget) (lua_State *L, int idx); +LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); +LUA_API void (lua_getfenv) (lua_State *L, int idx); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void (lua_settable) (lua_State *L, int idx); +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawset) (lua_State *L, int idx); +LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); +LUA_API int (lua_setfenv) (lua_State *L, int idx); + + +/* +** `load' and `call' functions (load and run Lua code) +*/ +LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); +LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); +LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname); + +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); + + +/* +** coroutine functions +*/ +LUA_API int (lua_yield) (lua_State *L, int nresults); +LUA_API int (lua_resume) (lua_State *L, int narg); +LUA_API int (lua_status) (lua_State *L); + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCCOUNTB 4 +#define LUA_GCSTEP 5 +#define LUA_GCSETPAUSE 6 +#define LUA_GCSETSTEPMUL 7 + +LUA_API int (lua_gc) (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API int (lua_error) (lua_State *L); + +LUA_API int (lua_next) (lua_State *L, int idx); + +LUA_API void (lua_concat) (lua_State *L, int n); + +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_strlen(L,i) lua_objlen(L, (i)) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) \ + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) + +#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) +#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) + +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) + + + +/* +** compatibility macros and functions +*/ + +#define lua_open() luaL_newstate() + +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) + +#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) + +#define lua_Chunkreader lua_Reader +#define lua_Chunkwriter lua_Writer + + +/* hack */ +LUA_API void lua_setlevel (lua_State *from, lua_State *to); + + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILRET 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + + +/* Functions to be called by the debuger in specific events */ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); + +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook lua_gethook (lua_State *L); +LUA_API int lua_gethookmask (lua_State *L); +LUA_API int lua_gethookcount (lua_State *L); + + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) `global', `local', `field', `method' */ + const char *what; /* (S) `Lua', `C', `main', `tail' */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int nups; /* (u) number of upvalues */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + int i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/luaconf.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/luaconf.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,763 @@ +/* +** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef lconfig_h +#define lconfig_h + +#include +#include + + +/* +** ================================================================== +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +@@ LUA_ANSI controls the use of non-ansi features. +** CHANGE it (define it) if you want Lua to avoid the use of any +** non-ansi feature or library. +*/ +#if defined(__STRICT_ANSI__) +#define LUA_ANSI +#endif + + +#if !defined(LUA_ANSI) && defined(_WIN32) +#define LUA_WIN +#endif + +#if defined(LUA_USE_LINUX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#define LUA_USE_READLINE /* needs some extra libraries */ +#endif + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_DL_DYLD /* does not need extra library */ +#endif + + + +/* +@@ LUA_USE_POSIX includes all functionallity listed as X/Open System +@* Interfaces Extension (XSI). +** CHANGE it (define it) if your system is XSI compatible. +*/ +#if defined(LUA_USE_POSIX) +#define LUA_USE_MKSTEMP +#define LUA_USE_ISATTY +#define LUA_USE_POPEN +#define LUA_USE_ULONGJMP +#endif + + +/* +@@ LUA_PATH and LUA_CPATH are the names of the environment variables that +@* Lua check to set its paths. +@@ LUA_INIT is the name of the environment variable that Lua +@* checks for initialization code. +** CHANGE them if you want different names. +*/ +#define LUA_PATH "LUA_PATH" +#define LUA_CPATH "LUA_CPATH" +#define LUA_INIT "LUA_INIT" + + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +@* Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +@* C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ +#if defined(_WIN32) +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_PATH_DEFAULT \ + ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua" +#define LUA_CPATH_DEFAULT \ + ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" + +#else +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/5.1/" +#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" +#define LUA_PATH_DEFAULT \ + "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua" +#define LUA_CPATH_DEFAULT \ + "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so" +#endif + + +/* +@@ LUA_DIRSEP is the directory separator (for submodules). +** CHANGE it if your machine does not use "/" as the directory separator +** and is not Windows. (On Windows Lua automatically uses "\".) +*/ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + + +/* +@@ LUA_PATHSEP is the character that separates templates in a path. +@@ LUA_PATH_MARK is the string that marks the substitution points in a +@* template. +@@ LUA_EXECDIR in a Windows path is replaced by the executable's +@* directory. +@@ LUA_IGMARK is a mark to ignore all before it when bulding the +@* luaopen_ function name. +** CHANGE them if for some reason your system cannot use those +** characters. (E.g., if one of those characters is a common character +** in file/directory names.) Probably you do not need to change them. +*/ +#define LUA_PATHSEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXECDIR "!" +#define LUA_IGMARK "-" + + +/* +@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. +** CHANGE that if ptrdiff_t is not adequate on your machine. (On most +** machines, ptrdiff_t gives a good choice between int or long.) +*/ +#define LUA_INTEGER ptrdiff_t + + +/* +@@ LUA_API is a mark for all core API functions. +@@ LUALIB_API is a mark for all standard library functions. +** CHANGE them if you need to define those functions in some special way. +** For instance, if you want to create one Windows DLL with the core and +** the libraries, you may want to use the following definition (define +** LUA_BUILD_AS_DLL to get it). +*/ +#if defined(LUA_BUILD_AS_DLL) + +#if defined(LUA_CORE) || defined(LUA_LIB) +#define LUA_API __declspec(dllexport) +#else +#define LUA_API __declspec(dllimport) +#endif + +#else + +#define LUA_API extern + +#endif + +/* more often than not the libs go together with the core */ +#define LUALIB_API LUA_API + + +/* +@@ LUAI_FUNC is a mark for all extern functions that are not to be +@* exported to outside modules. +@@ LUAI_DATA is a mark for all extern (const) variables that are not to +@* be exported to outside modules. +** CHANGE them if you need to mark them in some special way. Elf/gcc +** (versions 3.2 and later) mark them as "hidden" to optimize access +** when Lua is compiled as a shared library. +*/ +#if defined(luaall_c) +#define LUAI_FUNC static +#define LUAI_DATA /* empty */ + +#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) +#define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#define LUAI_DATA LUAI_FUNC + +#else +#define LUAI_FUNC extern +#define LUAI_DATA extern +#endif + + + +/* +@@ LUA_QL describes how error messages quote program elements. +** CHANGE it if you want a different appearance. +*/ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@* of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +** {================================================================== +** Stand-alone configuration +** =================================================================== +*/ + +#if defined(lua_c) || defined(luaall_c) + +/* +@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that +@* is, whether we're running lua interactively). +** CHANGE it if you have a better definition for non-POSIX/non-Windows +** systems. +*/ +#if defined(LUA_USE_ISATTY) +#include +#define lua_stdin_is_tty() isatty(0) +#elif defined(LUA_WIN) +#include +#include +#define lua_stdin_is_tty() _isatty(_fileno(stdin)) +#else +#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ +#endif + + +/* +@@ LUA_PROMPT is the default prompt used by stand-alone Lua. +@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua. +** CHANGE them if you want different prompts. (You can also change the +** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.) +*/ +#define LUA_PROMPT "> " +#define LUA_PROMPT2 ">> " + + +/* +@@ LUA_PROGNAME is the default name for the stand-alone Lua program. +** CHANGE it if your stand-alone interpreter has a different name and +** your system is not able to detect that name automatically. +*/ +#define LUA_PROGNAME "lua" + + +/* +@@ LUA_MAXINPUT is the maximum length for an input line in the +@* stand-alone interpreter. +** CHANGE it if you need longer lines. +*/ +#define LUA_MAXINPUT 512 + + +/* +@@ lua_readline defines how to show a prompt and then read a line from +@* the standard input. +@@ lua_saveline defines how to "save" a read line in a "history". +@@ lua_freeline defines how to free a line read by lua_readline. +** CHANGE them if you want to improve this functionality (e.g., by using +** GNU readline and history facilities). +*/ +#if defined(LUA_USE_READLINE) +#include +#include +#include +#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) +#define lua_saveline(L,idx) \ + if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ + add_history(lua_tostring(L, idx)); /* add it to history */ +#define lua_freeline(L,b) ((void)L, free(b)) +#else +#define lua_readline(L,b,p) \ + ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ + fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ +#define lua_saveline(L,idx) { (void)L; (void)idx; } +#define lua_freeline(L,b) { (void)L; (void)b; } +#endif + +#endif + +/* }================================================================== */ + + +/* +@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles +@* as a percentage. +** CHANGE it if you want the GC to run faster or slower (higher values +** mean larger pauses which mean slower collection.) You can also change +** this value dynamically. +*/ +#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */ + + +/* +@@ LUAI_GCMUL defines the default speed of garbage collection relative to +@* memory allocation as a percentage. +** CHANGE it if you want to change the granularity of the garbage +** collection. (Higher values mean coarser collections. 0 represents +** infinity, where each step performs a full collection.) You can also +** change this value dynamically. +*/ +#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ + + + +/* +@@ LUA_COMPAT_GETN controls compatibility with old getn behavior. +** CHANGE it (define it) if you want exact compatibility with the +** behavior of setn/getn in Lua 5.0. +*/ +#undef LUA_COMPAT_GETN + +/* +@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib. +** CHANGE it to undefined as soon as you do not need a global 'loadlib' +** function (the function is still available as 'package.loadlib'). +*/ +#undef LUA_COMPAT_LOADLIB + +/* +@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature. +** CHANGE it to undefined as soon as your programs use only '...' to +** access vararg parameters (instead of the old 'arg' table). +*/ +#define LUA_COMPAT_VARARG + +/* +@@ LUA_COMPAT_MOD controls compatibility with old math.mod function. +** CHANGE it to undefined as soon as your programs use 'math.fmod' or +** the new '%' operator instead of 'math.mod'. +*/ +#define LUA_COMPAT_MOD + +/* +@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting +@* facility. +** CHANGE it to 2 if you want the old behaviour, or undefine it to turn +** off the advisory error when nesting [[...]]. +*/ +#define LUA_COMPAT_LSTR 1 + +/* +@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name. +** CHANGE it to undefined as soon as you rename 'string.gfind' to +** 'string.gmatch'. +*/ +#define LUA_COMPAT_GFIND + +/* +@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib' +@* behavior. +** CHANGE it to undefined as soon as you replace to 'luaL_register' +** your uses of 'luaL_openlib' +*/ +#define LUA_COMPAT_OPENLIB + + + +/* +@@ luai_apicheck is the assert macro used by the Lua-C API. +** CHANGE luai_apicheck if you want Lua to perform some checks in the +** parameters it gets from API calls. This may slow down the interpreter +** a bit, but may be quite useful when debugging C code that interfaces +** with Lua. A useful redefinition is to use assert.h. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(L,o) { (void)L; assert(o); } +#else +#define luai_apicheck(L,o) { (void)L; } +#endif + + +/* +@@ LUAI_BITSINT defines the number of bits in an int. +** CHANGE here if Lua cannot automatically detect the number of bits of +** your machine. Probably you do not need to change this. +*/ +/* avoid overflows in comparison */ +#if INT_MAX-20 < 32760 +#define LUAI_BITSINT 16 +#elif INT_MAX > 2147483640L +/* int has at least 32 bits */ +#define LUAI_BITSINT 32 +#else +#error "you must define LUA_BITSINT with number of bits in an integer" +#endif + + +/* +@@ LUAI_UINT32 is an unsigned integer with at least 32 bits. +@@ LUAI_INT32 is an signed integer with at least 32 bits. +@@ LUAI_UMEM is an unsigned integer big enough to count the total +@* memory used by Lua. +@@ LUAI_MEM is a signed integer big enough to count the total memory +@* used by Lua. +** CHANGE here if for some weird reason the default definitions are not +** good enough for your machine. (The definitions in the 'else' +** part always works, but may waste space on machines with 64-bit +** longs.) Probably you do not need to change this. +*/ +#if LUAI_BITSINT >= 32 +#define LUAI_UINT32 unsigned int +#define LUAI_INT32 int +#define LUAI_MAXINT32 INT_MAX +#define LUAI_UMEM size_t +#define LUAI_MEM ptrdiff_t +#else +/* 16-bit ints */ +#define LUAI_UINT32 unsigned long +#define LUAI_INT32 long +#define LUAI_MAXINT32 LONG_MAX +#define LUAI_UMEM unsigned long +#define LUAI_MEM long +#endif + + +/* +@@ LUAI_MAXCALLS limits the number of nested calls. +** CHANGE it if you need really deep recursive calls. This limit is +** arbitrary; its only purpose is to stop infinite recursion before +** exhausting memory. +*/ +#define LUAI_MAXCALLS 20000 + + +/* +@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function +@* can use. +** CHANGE it if you need lots of (Lua) stack space for your C +** functions. This limit is arbitrary; its only purpose is to stop C +** functions to consume unlimited stack space. (must be smaller than +** -LUA_REGISTRYINDEX) +*/ +#define LUAI_MAXCSTACK 8000 + + + +/* +** {================================================================== +** CHANGE (to smaller values) the following definitions if your system +** has a small C stack. (Or you may want to change them to larger +** values if your system has a large C stack and these limits are +** too rigid for you.) Some of these constants control the size of +** stack-allocated arrays used by the compiler or the interpreter, while +** others limit the maximum number of recursive calls that the compiler +** or the interpreter can perform. Values too large may cause a C stack +** overflow for some forms of deep constructs. +** =================================================================== +*/ + + +/* +@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and +@* syntactical nested non-terminals in a program. +*/ +#define LUAI_MAXCCALLS 200 + + +/* +@@ LUAI_MAXVARS is the maximum number of local variables per function +@* (must be smaller than 250). +*/ +#define LUAI_MAXVARS 200 + + +/* +@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function +@* (must be smaller than 250). +*/ +#define LUAI_MAXUPVALUES 60 + + +/* +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +*/ +#define LUAL_BUFFERSIZE BUFSIZ + +/* }================================================================== */ + + + + +/* +** {================================================================== +@@ LUA_NUMBER is the type of numbers in Lua. +** CHANGE the following definitions only if you want to build Lua +** with a number type different from double. You may also need to +** change lua_number2int & lua_number2integer. +** =================================================================== +*/ + +#define LUA_NUMBER_DOUBLE +#define LUA_NUMBER double + +/* +@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' +@* over a number. +*/ +#define LUAI_UACNUMBER double + + +/* +@@ LUA_NUMBER_SCAN is the format for reading numbers. +@@ LUA_NUMBER_FMT is the format for writing numbers. +@@ lua_number2str converts a number to a string. +@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. +@@ lua_str2number converts a string to a number. +*/ +#define LUA_NUMBER_SCAN "%lf" +#define LUA_NUMBER_FMT "%.14g" +#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) +#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ +#define lua_str2number(s,p) strtod((s), (p)) + + +/* +@@ The luai_num* macros define the primitive operations over numbers. +*/ +#if defined(LUA_CORE) +#include +#define luai_numadd(a,b) ((a)+(b)) +#define luai_numsub(a,b) ((a)-(b)) +#define luai_nummul(a,b) ((a)*(b)) +#define luai_numdiv(a,b) ((a)/(b)) +#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b)) +#define luai_numpow(a,b) (pow(a,b)) +#define luai_numunm(a) (-(a)) +#define luai_numeq(a,b) ((a)==(b)) +#define luai_numlt(a,b) ((a)<(b)) +#define luai_numle(a,b) ((a)<=(b)) +#define luai_numisnan(a) (!luai_numeq((a), (a))) +#endif + + +/* +@@ lua_number2int is a macro to convert lua_Number to int. +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer. +** CHANGE them if you know a faster way to convert a lua_Number to +** int (with any rounding method and without throwing errors) in your +** system. In Pentium machines, a naive typecast from double to int +** in C is extremely slow, so any alternative is worth trying. +*/ + +/* On a Pentium, resort to a trick */ +#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \ + (defined(__i386) || defined (_M_IX86) || defined(__i386__)) + +/* On a Microsoft compiler, use assembler */ +#if 0//defined(_MSC_VER) // nitsuja: the "using assembly" method doesn't correctly handle integers between 0xFF000000 and and 0xFFFFFFFF, while the other branch does handle it correctly and it's not slower. + +#define lua_number2int(i,d) __asm fld d __asm fistp i +#define lua_number2integer(i,n) lua_number2int(i, n) + +/* the next trick should work on any Pentium, but sometimes clashes + with a DirectX idiosyncrasy */ +#else + +union luai_Cast { double l_d; long l_l; }; +#define lua_number2int(i,d) \ + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; } +#define lua_number2integer(i,n) lua_number2int(i, n) + +#endif + + +/* this option always works, but may be slow */ +#else +#define lua_number2int(i,d) ((i)=(int)(d)) +#define lua_number2integer(i,d) ((i)=(lua_Integer)(d)) + +#endif + +/* }================================================================== */ + + +/* +@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment. +** CHANGE it if your system requires alignments larger than double. (For +** instance, if your system supports long doubles and they must be +** aligned in 16-byte boundaries, then you should add long double in the +** union.) Probably you do not need to change this. +*/ +#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } + + +/* +@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling. +** CHANGE them if you prefer to use longjmp/setjmp even with C++ +** or if want/don't to use _longjmp/_setjmp instead of regular +** longjmp/setjmp. By default, Lua handles errors with exceptions when +** compiling as C++ code, with _longjmp/_setjmp when asked to use them, +** and with longjmp/setjmp otherwise. +*/ +#if defined(__cplusplus) +/* C++ exceptions */ +#define LUAI_THROW(L,c) throw(c) +#define LUAI_TRY(L,c,a) try { a } catch(...) \ + { if ((c)->status == 0) (c)->status = -1; } +#define luai_jmpbuf int /* dummy variable */ + +#elif defined(LUA_USE_ULONGJMP) +/* in Unix, try _longjmp/_setjmp (more efficient) */ +#define LUAI_THROW(L,c) _longjmp((c)->b, 1) +#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } +#define luai_jmpbuf jmp_buf + +#else +/* default handling with long jumps */ +#define LUAI_THROW(L,c) longjmp((c)->b, 1) +#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } +#define luai_jmpbuf jmp_buf + +#endif + + +/* +@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern +@* can do during pattern-matching. +** CHANGE it if you need more captures. This limit is arbitrary. +*/ +#define LUA_MAXCAPTURES 32 + + +/* +@@ lua_tmpnam is the function that the OS library uses to create a +@* temporary name. +@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam. +** CHANGE them if you have an alternative to tmpnam (which is considered +** insecure) or if you want the original tmpnam anyway. By default, Lua +** uses tmpnam except when POSIX is available, where it uses mkstemp. +*/ +#if defined(loslib_c) || defined(luaall_c) + +#if defined(LUA_USE_MKSTEMP) +#include +#define LUA_TMPNAMBUFSIZE 32 +#define lua_tmpnam(b,e) { \ + strcpy(b, "/tmp/lua_XXXXXX"); \ + e = mkstemp(b); \ + if (e != -1) close(e); \ + e = (e == -1); } + +#else +#define LUA_TMPNAMBUFSIZE L_tmpnam +#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } +#endif + +#endif + + +/* +@@ lua_popen spawns a new process connected to the current one through +@* the file streams. +** CHANGE it if you have a way to implement it in your system. +*/ +#if defined(LUA_USE_POPEN) + +#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) +#define lua_pclose(L,file) ((void)L, (pclose(file) != -1)) + +#elif defined(LUA_WIN) + +#define lua_popen(L,c,m) ((void)L, _popen(c,m)) +#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1)) + +#else + +#define lua_popen(L,c,m) ((void)((void)c, m), \ + luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) +#define lua_pclose(L,file) ((void)((void)L, file), 0) + +#endif + +/* +@@ LUA_DL_* define which dynamic-library system Lua should use. +** CHANGE here if Lua has problems choosing the appropriate +** dynamic-library system for your platform (either Windows' DLL, Mac's +** dyld, or Unix's dlopen). If your system is some kind of Unix, there +** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for +** it. To use dlopen you also need to adapt the src/Makefile (probably +** adding -ldl to the linker options), so Lua does not select it +** automatically. (When you change the makefile to add -ldl, you must +** also add -DLUA_USE_DLOPEN.) +** If you do not want any kind of dynamic library, undefine all these +** options. +** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD. +*/ +#if defined(LUA_USE_DLOPEN) +#define LUA_DL_DLOPEN +#endif + +#if defined(LUA_WIN) +#define LUA_DL_DLL +#endif + + +/* +@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State +@* (the data goes just *before* the lua_State pointer). +** CHANGE (define) this if you really need that. This value must be +** a multiple of the maximum alignment required for your machine. +*/ +#define LUAI_EXTRASPACE 0 + + +/* +@@ luai_userstate* allow user-specific actions on threads. +** CHANGE them if you defined LUAI_EXTRASPACE and need to do something +** extra when a thread is created/deleted/resumed/yielded. +*/ +#define luai_userstateopen(L) ((void)L) +#define luai_userstateclose(L) ((void)L) +#define luai_userstatethread(L,L1) ((void)L) +#define luai_userstatefree(L) ((void)L) +#define luai_userstateresume(L,n) ((void)L) +#define luai_userstateyield(L,n) ((void)L) + + +/* +@@ LUA_INTFRMLEN is the length modifier for integer conversions +@* in 'string.format'. +@@ LUA_INTFRM_T is the integer type correspoding to the previous length +@* modifier. +** CHANGE them if your system supports long long or does not support long. +*/ + +#if defined(LUA_USELONGLONG) + +#define LUA_INTFRMLEN "ll" +#define LUA_INTFRM_T long long + +#else + +#define LUA_INTFRMLEN "l" +#define LUA_INTFRM_T long + +#endif + + + +/* =================================================================== */ + +/* +** Local configuration. You can use this space to add your redefinitions +** without modifying the main part of the file. +*/ + + + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lualib.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lualib.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,53 @@ +/* +** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua standard libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lualib_h +#define lualib_h + +#include "lua.h" + + +/* Key to file-handle type */ +#define LUA_FILEHANDLE "FILE*" + + +#define LUA_COLIBNAME "coroutine" +LUALIB_API int (luaopen_base) (lua_State *L); + +#define LUA_TABLIBNAME "table" +LUALIB_API int (luaopen_table) (lua_State *L); + +#define LUA_IOLIBNAME "io" +LUALIB_API int (luaopen_io) (lua_State *L); + +#define LUA_OSLIBNAME "os" +LUALIB_API int (luaopen_os) (lua_State *L); + +#define LUA_STRLIBNAME "string" +LUALIB_API int (luaopen_string) (lua_State *L); + +#define LUA_MATHLIBNAME "math" +LUALIB_API int (luaopen_math) (lua_State *L); + +#define LUA_DBLIBNAME "debug" +LUALIB_API int (luaopen_debug) (lua_State *L); + +#define LUA_LOADLIBNAME "package" +LUALIB_API int (luaopen_package) (lua_State *L); + + +/* open all previous libraries */ +LUALIB_API void (luaL_openlibs) (lua_State *L); + + + +#ifndef lua_assert +#define lua_assert(x) ((void)0) +#endif + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lundump.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lundump.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,227 @@ +/* +** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ +** load precompiled Lua chunks +** See Copyright Notice in lua.h +*/ + +#include + +#define lundump_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstring.h" +#include "lundump.h" +#include "lzio.h" + +typedef struct { + lua_State* L; + ZIO* Z; + Mbuffer* b; + const char* name; +} LoadState; + +#ifdef LUAC_TRUST_BINARIES +#define IF(c,s) +#define error(S,s) +#else +#define IF(c,s) if (c) error(S,s) + +static void error(LoadState* S, const char* why) +{ + luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); + luaD_throw(S->L,LUA_ERRSYNTAX); +} +#endif + +#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) +#define LoadByte(S) (lu_byte)LoadChar(S) +#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) +#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) + +static void LoadBlock(LoadState* S, void* b, size_t size) +{ + size_t r=luaZ_read(S->Z,b,size); + IF (r!=0, "unexpected end"); +} + +static int LoadChar(LoadState* S) +{ + char x; + LoadVar(S,x); + return x; +} + +static int LoadInt(LoadState* S) +{ + int x; + LoadVar(S,x); + IF (x<0, "bad integer"); + return x; +} + +static lua_Number LoadNumber(LoadState* S) +{ + lua_Number x; + LoadVar(S,x); + return x; +} + +static TString* LoadString(LoadState* S) +{ + size_t size; + LoadVar(S,size); + if (size==0) + return NULL; + else + { + char* s=luaZ_openspace(S->L,S->b,size); + LoadBlock(S,s,size); + return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ + } +} + +static void LoadCode(LoadState* S, Proto* f) +{ + int n=LoadInt(S); + f->code=luaM_newvector(S->L,n,Instruction); + f->sizecode=n; + LoadVector(S,f->code,n,sizeof(Instruction)); +} + +static Proto* LoadFunction(LoadState* S, TString* p); + +static void LoadConstants(LoadState* S, Proto* f) +{ + int i,n; + n=LoadInt(S); + f->k=luaM_newvector(S->L,n,TValue); + f->sizek=n; + for (i=0; ik[i]); + for (i=0; ik[i]; + int t=LoadChar(S); + switch (t) + { + case LUA_TNIL: + setnilvalue(o); + break; + case LUA_TBOOLEAN: + setbvalue(o,LoadChar(S)!=0); + break; + case LUA_TNUMBER: + setnvalue(o,LoadNumber(S)); + break; + case LUA_TSTRING: + setsvalue2n(S->L,o,LoadString(S)); + break; + default: + error(S,"bad constant"); + break; + } + } + n=LoadInt(S); + f->p=luaM_newvector(S->L,n,Proto*); + f->sizep=n; + for (i=0; ip[i]=NULL; + for (i=0; ip[i]=LoadFunction(S,f->source); +} + +static void LoadDebug(LoadState* S, Proto* f) +{ + int i,n; + n=LoadInt(S); + f->lineinfo=luaM_newvector(S->L,n,int); + f->sizelineinfo=n; + LoadVector(S,f->lineinfo,n,sizeof(int)); + n=LoadInt(S); + f->locvars=luaM_newvector(S->L,n,LocVar); + f->sizelocvars=n; + for (i=0; ilocvars[i].varname=NULL; + for (i=0; ilocvars[i].varname=LoadString(S); + f->locvars[i].startpc=LoadInt(S); + f->locvars[i].endpc=LoadInt(S); + } + n=LoadInt(S); + f->upvalues=luaM_newvector(S->L,n,TString*); + f->sizeupvalues=n; + for (i=0; iupvalues[i]=NULL; + for (i=0; iupvalues[i]=LoadString(S); +} + +static Proto* LoadFunction(LoadState* S, TString* p) +{ + Proto* f; + if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); + f=luaF_newproto(S->L); + setptvalue2s(S->L,S->L->top,f); incr_top(S->L); + f->source=LoadString(S); if (f->source==NULL) f->source=p; + f->linedefined=LoadInt(S); + f->lastlinedefined=LoadInt(S); + f->nups=LoadByte(S); + f->numparams=LoadByte(S); + f->is_vararg=LoadByte(S); + f->maxstacksize=LoadByte(S); + LoadCode(S,f); + LoadConstants(S,f); + LoadDebug(S,f); + IF (!luaG_checkcode(f), "bad code"); + S->L->top--; + S->L->nCcalls--; + return f; +} + +static void LoadHeader(LoadState* S) +{ + char h[LUAC_HEADERSIZE]; + char s[LUAC_HEADERSIZE]; + luaU_header(h); + LoadBlock(S,s,LUAC_HEADERSIZE); + IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); +} + +/* +** load precompiled chunk +*/ +Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) +{ + LoadState S; + if (*name=='@' || *name=='=') + S.name=name+1; + else if (*name==LUA_SIGNATURE[0]) + S.name="binary string"; + else + S.name=name; + S.L=L; + S.Z=Z; + S.b=buff; + LoadHeader(&S); + return LoadFunction(&S,luaS_newliteral(L,"=?")); +} + +/* +* make header +*/ +void luaU_header (char* h) +{ + int x=1; + memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); + h+=sizeof(LUA_SIGNATURE)-1; + *h++=(char)LUAC_VERSION; + *h++=(char)LUAC_FORMAT; + *h++=(char)*(char*)&x; /* endianness */ + *h++=(char)sizeof(int); + *h++=(char)sizeof(size_t); + *h++=(char)sizeof(Instruction); + *h++=(char)sizeof(lua_Number); + *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lundump.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lundump.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,36 @@ +/* +** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ +** load precompiled Lua chunks +** See Copyright Notice in lua.h +*/ + +#ifndef lundump_h +#define lundump_h + +#include "lobject.h" +#include "lzio.h" + +/* load one chunk; from lundump.c */ +LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); + +/* make header; from lundump.c */ +LUAI_FUNC void luaU_header (char* h); + +/* dump one chunk; from ldump.c */ +LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); + +#ifdef luac_c +/* print one chunk; from print.c */ +LUAI_FUNC void luaU_print (const Proto* f, int full); +#endif + +/* for header of binary files -- this is Lua 5.1 */ +#define LUAC_VERSION 0x51 + +/* for header of binary files -- this is the official format */ +#define LUAC_FORMAT 0 + +/* size of header of binary files */ +#define LUAC_HEADERSIZE 12 + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lvm.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lvm.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,763 @@ +/* +** $Id: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $ +** Lua virtual machine +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#define lvm_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" +#include "lvm.h" + + + +/* limit for table tag-method chains (to avoid loops) */ +#define MAXTAGLOOP 100 + + +const TValue *luaV_tonumber (const TValue *obj, TValue *n) { + lua_Number num; + if (ttisnumber(obj)) return obj; + if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) { + setnvalue(n, num); + return n; + } + else + return NULL; +} + + +int luaV_tostring (lua_State *L, StkId obj) { + if (!ttisnumber(obj)) + return 0; + else { + char s[LUAI_MAXNUMBER2STR]; + lua_Number n = nvalue(obj); + lua_number2str(s, n); + setsvalue2s(L, obj, luaS_new(L, s)); + return 1; + } +} + + +static void traceexec (lua_State *L, const Instruction *pc) { + lu_byte mask = L->hookmask; + const Instruction *oldpc = L->savedpc; + L->savedpc = pc; + if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) { + resethookcount(L); + luaD_callhook(L, LUA_HOOKCOUNT, -1); + } + if (mask & LUA_MASKLINE) { + Proto *p = ci_func(L->ci)->l.p; + int npc = pcRel(pc, p); + int newline = getline(p, npc); + /* call linehook when enter a new function, when jump back (loop), + or when enter a new line */ + if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p))) + luaD_callhook(L, LUA_HOOKLINE, newline); + } +} + + +static void callTMres (lua_State *L, StkId res, const TValue *f, + const TValue *p1, const TValue *p2) { + ptrdiff_t result = savestack(L, res); + setobj2s(L, L->top, f); /* push function */ + setobj2s(L, L->top+1, p1); /* 1st argument */ + setobj2s(L, L->top+2, p2); /* 2nd argument */ + luaD_checkstack(L, 3); + L->top += 3; + luaD_call(L, L->top - 3, 1); + res = restorestack(L, result); + L->top--; + setobjs2s(L, res, L->top); +} + + + +static void callTM (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, const TValue *p3) { + setobj2s(L, L->top, f); /* push function */ + setobj2s(L, L->top+1, p1); /* 1st argument */ + setobj2s(L, L->top+2, p2); /* 2nd argument */ + setobj2s(L, L->top+3, p3); /* 3th argument */ + luaD_checkstack(L, 4); + L->top += 4; + luaD_call(L, L->top - 4, 0); +} + + +void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { + int loop; + for (loop = 0; loop < MAXTAGLOOP; loop++) { + const TValue *tm; + if (ttistable(t)) { /* `t' is a table? */ + Table *h = hvalue(t); + const TValue *res = luaH_get(h, key); /* do a primitive get */ + if (!ttisnil(res) || /* result is no nil? */ + (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ + setobj2s(L, val, res); + return; + } + /* else will try the tag method */ + } + else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) + luaG_typeerror(L, t, "index"); + if (ttisfunction(tm)) { + callTMres(L, val, tm, t, key); + return; + } + t = tm; /* else repeat with `tm' */ + } + luaG_runerror(L, "loop in gettable"); +} + + +void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { + int loop; + for (loop = 0; loop < MAXTAGLOOP; loop++) { + const TValue *tm; + if (ttistable(t)) { /* `t' is a table? */ + Table *h = hvalue(t); + TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ + if (!ttisnil(oldval) || /* result is no nil? */ + (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ + setobj2t(L, oldval, val); + luaC_barriert(L, h, val); + return; + } + /* else will try the tag method */ + } + else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) + luaG_typeerror(L, t, "index"); + if (ttisfunction(tm)) { + callTM(L, tm, t, key, val); + return; + } + t = tm; /* else repeat with `tm' */ + } + luaG_runerror(L, "loop in settable"); +} + + +static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event) { + const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ + if (ttisnil(tm)) + tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ + if (ttisnil(tm)) return 0; + callTMres(L, res, tm, p1, p2); + return 1; +} + + +static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2, + TMS event) { + const TValue *tm1 = fasttm(L, mt1, event); + const TValue *tm2; + if (tm1 == NULL) return NULL; /* no metamethod */ + if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ + tm2 = fasttm(L, mt2, event); + if (tm2 == NULL) return NULL; /* no metamethod */ + if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */ + return tm1; + return NULL; +} + + +static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, + TMS event) { + const TValue *tm1 = luaT_gettmbyobj(L, p1, event); + const TValue *tm2; + if (ttisnil(tm1)) return -1; /* no metamethod? */ + tm2 = luaT_gettmbyobj(L, p2, event); + if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */ + return -1; + callTMres(L, L->top, tm1, p1, p2); + return !l_isfalse(L->top); +} + + +static int l_strcmp (const TString *ls, const TString *rs) { + const char *l = getstr(ls); + size_t ll = ls->tsv.len; + const char *r = getstr(rs); + size_t lr = rs->tsv.len; + for (;;) { + int temp = strcoll(l, r); + if (temp != 0) return temp; + else { /* strings are equal up to a `\0' */ + size_t len = strlen(l); /* index of first `\0' in both strings */ + if (len == lr) /* r is finished? */ + return (len == ll) ? 0 : 1; + else if (len == ll) /* l is finished? */ + return -1; /* l is smaller than r (because r is not finished) */ + /* both strings longer than `len'; go on comparing (after the `\0') */ + len++; + l += len; ll -= len; r += len; lr -= len; + } + } +} + + +int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { + int res; + if (ttype(l) != ttype(r)) + return luaG_ordererror(L, l, r); + else if (ttisnumber(l)) + return luai_numlt(nvalue(l), nvalue(r)); + else if (ttisstring(l)) + return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; + else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) + return res; + return luaG_ordererror(L, l, r); +} + + +static int lessequal (lua_State *L, const TValue *l, const TValue *r) { + int res; + if (ttype(l) != ttype(r)) + return luaG_ordererror(L, l, r); + else if (ttisnumber(l)) + return luai_numle(nvalue(l), nvalue(r)); + else if (ttisstring(l)) + return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; + else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ + return res; + else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */ + return !res; + return luaG_ordererror(L, l, r); +} + + +int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) { + const TValue *tm; + lua_assert(ttype(t1) == ttype(t2)); + switch (ttype(t1)) { + case LUA_TNIL: return 1; + case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); + case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ + case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); + case LUA_TUSERDATA: { + if (uvalue(t1) == uvalue(t2)) return 1; + tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, + TM_EQ); + break; /* will try TM */ + } + case LUA_TTABLE: { + if (hvalue(t1) == hvalue(t2)) return 1; + tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); + break; /* will try TM */ + } + default: return gcvalue(t1) == gcvalue(t2); + } + if (tm == NULL) return 0; /* no TM? */ + callTMres(L, L->top, tm, t1, t2); /* call TM */ + return !l_isfalse(L->top); +} + + +void luaV_concat (lua_State *L, int total, int last) { + do { + StkId top = L->base + last + 1; + int n = 2; /* number of elements handled in this pass (at least 2) */ + if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { + if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) + luaG_concaterror(L, top-2, top-1); + } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ + (void)tostring(L, top - 2); /* result is first op (as string) */ + else { + /* at least two string values; get as many as possible */ + size_t tl = tsvalue(top-1)->len; + char *buffer; + int i; + /* collect total length */ + for (n = 1; n < total && tostring(L, top-n-1); n++) { + size_t l = tsvalue(top-n-1)->len; + if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); + tl += l; + } + buffer = luaZ_openspace(L, &G(L)->buff, tl); + tl = 0; + for (i=n; i>0; i--) { /* concat all strings */ + size_t l = tsvalue(top-i)->len; + memcpy(buffer+tl, svalue(top-i), l); + tl += l; + } + setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); + } + total -= n-1; /* got `n' strings to create 1 new */ + last -= n-1; + } while (total > 1); /* repeat until only 1 result left */ +} + + +static void Arith (lua_State *L, StkId ra, const TValue *rb, + const TValue *rc, TMS op) { + TValue tempb, tempc; + const TValue *b, *c; + if ((b = luaV_tonumber(rb, &tempb)) != NULL && + (c = luaV_tonumber(rc, &tempc)) != NULL) { + lua_Number nb = nvalue(b), nc = nvalue(c); + switch (op) { + case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; + case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; + case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; + case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; + case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; + case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; + case TM_UNM: setnvalue(ra, luai_numunm(nb)); break; + default: lua_assert(0); break; + } + } + else if (!call_binTM(L, rb, rc, ra, op)) + luaG_aritherror(L, rb, rc); +} + + + +/* +** some macros for common tasks in `luaV_execute' +*/ + +#define runtime_check(L, c) { if (!(c)) break; } + +#define RA(i) (base+GETARG_A(i)) +/* to be used after possible stack reallocation */ +#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) +#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) +#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ + ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) +#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ + ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) +#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i)) + + +#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);} + + +#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; } + + +#define arith_op(op,tm) { \ + TValue *rb = RKB(i); \ + TValue *rc = RKC(i); \ + if (ttisnumber(rb) && ttisnumber(rc)) { \ + lua_Number nb = nvalue(rb), nc = nvalue(rc); \ + setnvalue(ra, op(nb, nc)); \ + } \ + else \ + Protect(Arith(L, ra, rb, rc, tm)); \ + } + + + +void luaV_execute (lua_State *L, int nexeccalls) { + LClosure *cl; + StkId base; + TValue *k; + const Instruction *pc; + reentry: /* entry point */ + lua_assert(isLua(L->ci)); + pc = L->savedpc; + cl = &clvalue(L->ci->func)->l; + base = L->base; + k = cl->p->k; + /* main loop of interpreter */ + for (;;) { + const Instruction i = *pc++; + StkId ra; + if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && + (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { + traceexec(L, pc); + if (L->status == LUA_YIELD) { /* did hook yield? */ + L->savedpc = pc - 1; + return; + } + base = L->base; + } + /* warning!! several calls may realloc the stack and invalidate `ra' */ + ra = RA(i); + lua_assert(base == L->base && L->base == L->ci->base); + lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); + lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); + switch (GET_OPCODE(i)) { + case OP_MOVE: { + setobjs2s(L, ra, RB(i)); + continue; + } + case OP_LOADK: { + setobj2s(L, ra, KBx(i)); + continue; + } + case OP_LOADBOOL: { + setbvalue(ra, GETARG_B(i)); + if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ + continue; + } + case OP_LOADNIL: { + TValue *rb = RB(i); + do { + setnilvalue(rb--); + } while (rb >= ra); + continue; + } + case OP_GETUPVAL: { + int b = GETARG_B(i); + setobj2s(L, ra, cl->upvals[b]->v); + continue; + } + case OP_GETGLOBAL: { + TValue g; + TValue *rb = KBx(i); + sethvalue(L, &g, cl->env); + lua_assert(ttisstring(rb)); + Protect(luaV_gettable(L, &g, rb, ra)); + continue; + } + case OP_GETTABLE: { + Protect(luaV_gettable(L, RB(i), RKC(i), ra)); + continue; + } + case OP_SETGLOBAL: { + TValue g; + sethvalue(L, &g, cl->env); + lua_assert(ttisstring(KBx(i))); + Protect(luaV_settable(L, &g, KBx(i), ra)); + continue; + } + case OP_SETUPVAL: { + UpVal *uv = cl->upvals[GETARG_B(i)]; + setobj(L, uv->v, ra); + luaC_barrier(L, uv, ra); + continue; + } + case OP_SETTABLE: { + Protect(luaV_settable(L, ra, RKB(i), RKC(i))); + continue; + } + case OP_NEWTABLE: { + int b = GETARG_B(i); + int c = GETARG_C(i); + sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); + Protect(luaC_checkGC(L)); + continue; + } + case OP_SELF: { + StkId rb = RB(i); + setobjs2s(L, ra+1, rb); + Protect(luaV_gettable(L, rb, RKC(i), ra)); + continue; + } + case OP_ADD: { + arith_op(luai_numadd, TM_ADD); + continue; + } + case OP_SUB: { + arith_op(luai_numsub, TM_SUB); + continue; + } + case OP_MUL: { + arith_op(luai_nummul, TM_MUL); + continue; + } + case OP_DIV: { + arith_op(luai_numdiv, TM_DIV); + continue; + } + case OP_MOD: { + arith_op(luai_nummod, TM_MOD); + continue; + } + case OP_POW: { + arith_op(luai_numpow, TM_POW); + continue; + } + case OP_UNM: { + TValue *rb = RB(i); + if (ttisnumber(rb)) { + lua_Number nb = nvalue(rb); + setnvalue(ra, luai_numunm(nb)); + } + else { + Protect(Arith(L, ra, rb, rb, TM_UNM)); + } + continue; + } + case OP_NOT: { + int res = l_isfalse(RB(i)); /* next assignment may change this value */ + setbvalue(ra, res); + continue; + } + case OP_LEN: { + const TValue *rb = RB(i); + switch (ttype(rb)) { + case LUA_TTABLE: { + setnvalue(ra, cast_num(luaH_getn(hvalue(rb)))); + break; + } + case LUA_TSTRING: { + setnvalue(ra, cast_num(tsvalue(rb)->len)); + break; + } + default: { /* try metamethod */ + Protect( + if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN)) + luaG_typeerror(L, rb, "get length of"); + ) + } + } + continue; + } + case OP_CONCAT: { + int b = GETARG_B(i); + int c = GETARG_C(i); + Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L)); + setobjs2s(L, RA(i), base+b); + continue; + } + case OP_JMP: { + dojump(L, pc, GETARG_sBx(i)); + continue; + } + case OP_EQ: { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + Protect( + if (equalobj(L, rb, rc) == GETARG_A(i)) + dojump(L, pc, GETARG_sBx(*pc)); + ) + pc++; + continue; + } + case OP_LT: { + Protect( + if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i)) + dojump(L, pc, GETARG_sBx(*pc)); + ) + pc++; + continue; + } + case OP_LE: { + Protect( + if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i)) + dojump(L, pc, GETARG_sBx(*pc)); + ) + pc++; + continue; + } + case OP_TEST: { + if (l_isfalse(ra) != GETARG_C(i)) + dojump(L, pc, GETARG_sBx(*pc)); + pc++; + continue; + } + case OP_TESTSET: { + TValue *rb = RB(i); + if (l_isfalse(rb) != GETARG_C(i)) { + setobjs2s(L, ra, rb); + dojump(L, pc, GETARG_sBx(*pc)); + } + pc++; + continue; + } + case OP_CALL: { + int b = GETARG_B(i); + int nresults = GETARG_C(i) - 1; + if (b != 0) L->top = ra+b; /* else previous instruction set top */ + L->savedpc = pc; + switch (luaD_precall(L, ra, nresults)) { + case PCRLUA: { + nexeccalls++; + goto reentry; /* restart luaV_execute over new Lua function */ + } + case PCRC: { + /* it was a C function (`precall' called it); adjust results */ + if (nresults >= 0) L->top = L->ci->top; + base = L->base; + continue; + } + default: { + return; /* yield */ + } + } + } + case OP_TAILCALL: { + int b = GETARG_B(i); + if (b != 0) L->top = ra+b; /* else previous instruction set top */ + L->savedpc = pc; + lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); + switch (luaD_precall(L, ra, LUA_MULTRET)) { + case PCRLUA: { + /* tail call: put new frame in place of previous one */ + CallInfo *ci = L->ci - 1; /* previous frame */ + int aux; + StkId func = ci->func; + StkId pfunc = (ci+1)->func; /* previous function index */ + if (L->openupval) luaF_close(L, ci->base); + L->base = ci->base = ci->func + ((ci+1)->base - pfunc); + for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ + setobjs2s(L, func+aux, pfunc+aux); + ci->top = L->top = func+aux; /* correct top */ + lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize); + ci->savedpc = L->savedpc; + ci->tailcalls++; /* one more call lost */ + L->ci--; /* remove new frame */ + goto reentry; + } + case PCRC: { /* it was a C function (`precall' called it) */ + base = L->base; + continue; + } + default: { + return; /* yield */ + } + } + } + case OP_RETURN: { + int b = GETARG_B(i); + if (b != 0) L->top = ra+b-1; + if (L->openupval) luaF_close(L, base); + L->savedpc = pc; + b = luaD_poscall(L, ra); + if (--nexeccalls == 0) /* was previous function running `here'? */ + return; /* no: return */ + else { /* yes: continue its execution */ + if (b) L->top = L->ci->top; + lua_assert(isLua(L->ci)); + lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL); + goto reentry; + } + } + case OP_FORLOOP: { + lua_Number step = nvalue(ra+2); + lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */ + lua_Number limit = nvalue(ra+1); + if (luai_numlt(0, step) ? luai_numle(idx, limit) + : luai_numle(limit, idx)) { + dojump(L, pc, GETARG_sBx(i)); /* jump back */ + setnvalue(ra, idx); /* update internal index... */ + setnvalue(ra+3, idx); /* ...and external index */ + } + continue; + } + case OP_FORPREP: { + const TValue *init = ra; + const TValue *plimit = ra+1; + const TValue *pstep = ra+2; + L->savedpc = pc; /* next steps may throw errors */ + if (!tonumber(init, ra)) + luaG_runerror(L, LUA_QL("for") " initial value must be a number"); + else if (!tonumber(plimit, ra+1)) + luaG_runerror(L, LUA_QL("for") " limit must be a number"); + else if (!tonumber(pstep, ra+2)) + luaG_runerror(L, LUA_QL("for") " step must be a number"); + setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep))); + dojump(L, pc, GETARG_sBx(i)); + continue; + } + case OP_TFORLOOP: { + StkId cb = ra + 3; /* call base */ + setobjs2s(L, cb+2, ra+2); + setobjs2s(L, cb+1, ra+1); + setobjs2s(L, cb, ra); + L->top = cb+3; /* func. + 2 args (state and index) */ + Protect(luaD_call(L, cb, GETARG_C(i))); + L->top = L->ci->top; + cb = RA(i) + 3; /* previous call may change the stack */ + if (!ttisnil(cb)) { /* continue loop? */ + setobjs2s(L, cb-1, cb); /* save control variable */ + dojump(L, pc, GETARG_sBx(*pc)); /* jump back */ + } + pc++; + continue; + } + case OP_SETLIST: { + int n = GETARG_B(i); + int c = GETARG_C(i); + int last; + Table *h; + if (n == 0) { + n = cast_int(L->top - ra) - 1; + L->top = L->ci->top; + } + if (c == 0) c = cast_int(*pc++); + runtime_check(L, ttistable(ra)); + h = hvalue(ra); + last = ((c-1)*LFIELDS_PER_FLUSH) + n; + if (last > h->sizearray) /* needs more space? */ + luaH_resizearray(L, h, last); /* pre-alloc it at once */ + for (; n > 0; n--) { + TValue *val = ra+n; + setobj2t(L, luaH_setnum(L, h, last--), val); + luaC_barriert(L, h, val); + } + continue; + } + case OP_CLOSE: { + luaF_close(L, ra); + continue; + } + case OP_CLOSURE: { + Proto *p; + Closure *ncl; + int nup, j; + p = cl->p->p[GETARG_Bx(i)]; + nup = p->nups; + ncl = luaF_newLclosure(L, nup, cl->env); + ncl->l.p = p; + for (j=0; jl.upvals[j] = cl->upvals[GETARG_B(*pc)]; + else { + lua_assert(GET_OPCODE(*pc) == OP_MOVE); + ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc)); + } + } + setclvalue(L, ra, ncl); + Protect(luaC_checkGC(L)); + continue; + } + case OP_VARARG: { + int b = GETARG_B(i) - 1; + int j; + CallInfo *ci = L->ci; + int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1; + if (b == LUA_MULTRET) { + Protect(luaD_checkstack(L, n)); + ra = RA(i); /* previous call may change the stack */ + b = n; + L->top = ra + n; + } + for (j = 0; j < b; j++) { + if (j < n) { + setobjs2s(L, ra + j, ci->base - n + j); + } + else { + setnilvalue(ra + j); + } + } + continue; + } + } + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lvm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lvm.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,36 @@ +/* +** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua virtual machine +** See Copyright Notice in lua.h +*/ + +#ifndef lvm_h +#define lvm_h + + +#include "ldo.h" +#include "lobject.h" +#include "ltm.h" + + +#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) + +#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ + (((o) = luaV_tonumber(o,n)) != NULL)) + +#define equalobj(L,o1,o2) \ + (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) + + +LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); +LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); +LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); +LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); +LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, + StkId val); +LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, + StkId val); +LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); +LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lzio.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lzio.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,82 @@ +/* +** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ +** a generic input stream interface +** See Copyright Notice in lua.h +*/ + + +#include + +#define lzio_c +#define LUA_CORE + +#include "lua.h" + +#include "llimits.h" +#include "lmem.h" +#include "lstate.h" +#include "lzio.h" + + +int luaZ_fill (ZIO *z) { + size_t size; + lua_State *L = z->L; + const char *buff; + lua_unlock(L); + buff = z->reader(L, z->data, &size); + lua_lock(L); + if (buff == NULL || size == 0) return EOZ; + z->n = size - 1; + z->p = buff; + return char2int(*(z->p++)); +} + + +int luaZ_lookahead (ZIO *z) { + if (z->n == 0) { + if (luaZ_fill(z) == EOZ) + return EOZ; + else { + z->n++; /* luaZ_fill removed first byte; put back it */ + z->p--; + } + } + return char2int(*z->p); +} + + +void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { + z->L = L; + z->reader = reader; + z->data = data; + z->n = 0; + z->p = NULL; +} + + +/* --------------------------------------------------------------- read --- */ +size_t luaZ_read (ZIO *z, void *b, size_t n) { + while (n) { + size_t m; + if (luaZ_lookahead(z) == EOZ) + return n; /* return number of missing bytes */ + m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ + memcpy(b, z->p, m); + z->n -= m; + z->p += m; + b = (char *)b + m; + n -= m; + } + return 0; +} + +/* ------------------------------------------------------------------------ */ +char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { + if (n > buff->buffsize) { + if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; + luaZ_resizebuffer(L, buff, n); + } + return buff->buffer; +} + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/lzio.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/lzio.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,67 @@ +/* +** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ +** Buffered streams +** See Copyright Notice in lua.h +*/ + + +#ifndef lzio_h +#define lzio_h + +#include "lua.h" + +#include "lmem.h" + + +#define EOZ (-1) /* end of stream */ + +typedef struct Zio ZIO; + +#define char2int(c) cast(int, cast(unsigned char, (c))) + +#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) + +typedef struct Mbuffer { + char *buffer; + size_t n; + size_t buffsize; +} Mbuffer; + +#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) + +#define luaZ_buffer(buff) ((buff)->buffer) +#define luaZ_sizebuffer(buff) ((buff)->buffsize) +#define luaZ_bufflen(buff) ((buff)->n) + +#define luaZ_resetbuffer(buff) ((buff)->n = 0) + + +#define luaZ_resizebuffer(L, buff, size) \ + (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ + (buff)->buffsize = size) + +#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) + + +LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); +LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, + void *data); +LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ +LUAI_FUNC int luaZ_lookahead (ZIO *z); + + + +/* --------- Private Part ------------------ */ + +struct Zio { + size_t n; /* bytes still unread */ + const char *p; /* current position in buffer */ + lua_Reader reader; + void* data; /* additional data */ + lua_State *L; /* Lua state (for reader) */ +}; + + +LUAI_FUNC int luaZ_fill (ZIO *z); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/lua/src/print.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lua/src/print.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,227 @@ +/* +** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ +** print bytecodes +** See Copyright Notice in lua.h +*/ + +#include +#include + +#define luac_c +#define LUA_CORE + +#include "ldebug.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lundump.h" + +#define PrintFunction luaU_print + +#define Sizeof(x) ((int)sizeof(x)) +#define VOID(p) ((const void*)(p)) + +static void PrintString(const TString* ts) +{ + const char* s=getstr(ts); + size_t i,n=ts->tsv.len; + putchar('"'); + for (i=0; ik[i]; + switch (ttype(o)) + { + case LUA_TNIL: + printf("nil"); + break; + case LUA_TBOOLEAN: + printf(bvalue(o) ? "true" : "false"); + break; + case LUA_TNUMBER: + printf(LUA_NUMBER_FMT,nvalue(o)); + break; + case LUA_TSTRING: + PrintString(rawtsvalue(o)); + break; + default: /* cannot happen */ + printf("? type=%d",ttype(o)); + break; + } +} + +static void PrintCode(const Proto* f) +{ + const Instruction* code=f->code; + int pc,n=f->sizecode; + for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); + printf("%-9s\t",luaP_opnames[o]); + switch (getOpMode(o)) + { + case iABC: + printf("%d",a); + if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); + if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); + break; + case iABx: + if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); + break; + case iAsBx: + if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); + break; + } + switch (o) + { + case OP_LOADK: + printf("\t; "); PrintConstant(f,bx); + break; + case OP_GETUPVAL: + case OP_SETUPVAL: + printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); + break; + case OP_GETGLOBAL: + case OP_SETGLOBAL: + printf("\t; %s",svalue(&f->k[bx])); + break; + case OP_GETTABLE: + case OP_SELF: + if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } + break; + case OP_SETTABLE: + case OP_ADD: + case OP_SUB: + case OP_MUL: + case OP_DIV: + case OP_POW: + case OP_EQ: + case OP_LT: + case OP_LE: + if (ISK(b) || ISK(c)) + { + printf("\t; "); + if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); + printf(" "); + if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); + } + break; + case OP_JMP: + case OP_FORLOOP: + case OP_FORPREP: + printf("\t; to %d",sbx+pc+2); + break; + case OP_CLOSURE: + printf("\t; %p",VOID(f->p[bx])); + break; + case OP_SETLIST: + if (c==0) printf("\t; %d",(int)code[++pc]); + else printf("\t; %d",c); + break; + default: + break; + } + printf("\n"); + } +} + +#define SS(x) (x==1)?"":"s" +#define S(x) x,SS(x) + +static void PrintHeader(const Proto* f) +{ + const char* s=getstr(f->source); + if (*s=='@' || *s=='=') + s++; + else if (*s==LUA_SIGNATURE[0]) + s="(bstring)"; + else + s="(string)"; + printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", + (f->linedefined==0)?"main":"function",s, + f->linedefined,f->lastlinedefined, + S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); + printf("%d%s param%s, %d slot%s, %d upvalue%s, ", + f->numparams,f->is_vararg?"+":"",SS(f->numparams), + S(f->maxstacksize),S(f->nups)); + printf("%d local%s, %d constant%s, %d function%s\n", + S(f->sizelocvars),S(f->sizek),S(f->sizep)); +} + +static void PrintConstants(const Proto* f) +{ + int i,n=f->sizek; + printf("constants (%d) for %p:\n",n,VOID(f)); + for (i=0; isizelocvars; + printf("locals (%d) for %p:\n",n,VOID(f)); + for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); + } +} + +static void PrintUpvalues(const Proto* f) +{ + int i,n=f->sizeupvalues; + printf("upvalues (%d) for %p:\n",n,VOID(f)); + if (f->upvalues==NULL) return; + for (i=0; iupvalues[i])); + } +} + +void PrintFunction(const Proto* f, int full) +{ + int i,n=f->sizep; + PrintHeader(f); + PrintCode(f); + if (full) + { + PrintConstants(f); + PrintLocals(f); + PrintUpvalues(f); + } + for (i=0; ip[i],full); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/prof/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/prof/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,7 @@ +noinst_LIBRARIES = libprof.a + +libprof_a_SOURCES = \ + gmon.h \ + gmon_out.h \ + prof.cpp \ + prof.h diff -r 8ced16adf2e1 -r f9f4f1b99eed src/prof/gmon.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/prof/gmon.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,151 @@ +/* + * Copyright (c) 1983, 1991, 1993, 2001 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#ifndef gmon_h +#define gmon_h + +#include +typedef uint32_t u32; + +/* Size of the 4.4BSD gmon header */ +#define GMON_HDRSIZE_BSD44_32 (4 + 4 + 4 + 4 + 4 + (3 * 4)) +#define GMON_HDRSIZE_BSD44_64 (8 + 8 + 4 + 4 + 4 + (3 * 4)) + +#if 0 /* For documentation purposes only. */ + struct raw_phdr + { + char low_pc[sizeof(void *)]; /* base pc address of sample buffer */ + char high_pc[sizeof(void *)];/* max pc address of sampled buffer */ + char ncnt[4]; /* size of sample buffer (plus this + header) */ + + char version[4]; /* version number */ + char profrate[4]; /* profiling clock rate */ + char spare[3*4]; /* reserved */ + }; +#endif + +#define GMONVERSION 0x00051879 + +/* Size of the old BSD gmon header */ +#define GMON_HDRSIZE_OLDBSD_32 (4 + 4 + 4) + +/* FIXME: Checking host compiler defines here means that we can't + use a cross gprof alpha OSF. */ +#if defined(__alpha__) && defined (__osf__) +#define GMON_HDRSIZE_OLDBSD_64 (8 + 8 + 4 + 4) +#else +#define GMON_HDRSIZE_OLDBSD_64 (8 + 8 + 4) +#endif + +#if 0 /* For documentation purposes only. */ + struct old_raw_phdr + { + char low_pc[sizeof(void *)]; /* base pc address of sample buffer */ + char high_pc[sizeof(void *)];/* max pc address of sampled buffer */ + char ncnt[4]; /* size of sample buffer (plus this + header) */ +#if defined (__alpha__) && defined (__osf__) + /* + * DEC's OSF v3.0 uses 4 bytes of padding to bring the header to + * a size that is a multiple of 8. + */ + char pad[4]; +#endif + }; +#endif + +/* + * Histogram counters are unsigned shorts: + */ +#define HISTCOUNTER unsigned short + +/* + * Fraction of text space to allocate for histogram counters here, 1/2: + */ +#define HISTFRACTION 2 + +/* + * Fraction of text space to allocate for from hash buckets. The + * value of HASHFRACTION is based on the minimum number of bytes of + * separation between two subroutine call points in the object code. + * Given MIN_SUBR_SEPARATION bytes of separation the value of + * HASHFRACTION is calculated as: + * + * HASHFRACTION = MIN_SUBR_SEPARATION / (2 * sizeof(short) - 1); + * + * For the VAX, the shortest two call sequence is: + * + * calls $0,(r0) + * calls $0,(r0) + * + * which is separated by only three bytes, thus HASHFRACTION is + * calculated as: + * + * HASHFRACTION = 3 / (2 * 2 - 1) = 1 + * + * Note that the division above rounds down, thus if MIN_SUBR_FRACTION + * is less than three, this algorithm will not work! + */ +#define HASHFRACTION 1 + +/* + * Percent of text space to allocate for tostructs with a minimum: + */ +#define ARCDENSITY 2 +#define MINARCS 50 + +struct tostruct + { + u32 selfpc; + int count; + unsigned short link; + }; + +/* + * A raw arc, with pointers to the calling site and the called site + * and a count. Everything is defined in terms of characters so + * as to get a packed representation (otherwise, different compilers + * might introduce different padding): + */ +#if 0 /* For documentation purposes only. */ + struct raw_arc + { + char from_pc[sizeof(void *)]; + char self_pc[sizeof(void *)]; + char count[sizeof(long)]; + }; +#endif + +/* + * General rounding functions: + */ +#define ROUNDDOWN(x,y) (((x)/(y))*(y)) +#define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y)) + +#endif /* gmon_h */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/prof/gmon_out.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/prof/gmon_out.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,45 @@ +/* gmon_out.h + + Copyright 2000, 2001 Free Software Foundation, Inc. + +This file is part of GNU Binutils. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +/* A gmon.out file consists of a header (defined by gmon_hdr) followed + by a sequence of records. Each record starts with a one-byte tag + identifying the type of records, followed by records specific data. */ +#ifndef gmon_out_h +#define gmon_out_h + +#define GMON_MAGIC "gmon" /* magic cookie */ +#define GMON_VERSION 1 /* version number */ + +/* Raw header as it appears on file (without padding). */ +struct gmon_hdr + { + char cookie[4]; + char version[4]; + char spare[3 * 4]; + }; + +/* Types of records in this file. */ +typedef enum + { + GMON_TAG_TIME_HIST = 0, GMON_TAG_CG_ARC = 1, GMON_TAG_BB_COUNT = 2 + } +GMON_Record_Tag; + +#endif /* gmon_out_h */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/prof/prof.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/prof/prof.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,405 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +// adapted from gmon.c +/*- + * Copyright (c) 1991, 1998 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. [rescinded 22 July 1999] + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +#include "gmon.h" +#include "gmon_out.h" + +#include "../common/System.h" +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../NLS.h" + +/* + * froms is actually a bunch of unsigned shorts indexing tos + */ +static int profiling = 3; +static unsigned short *froms; +static struct tostruct *tos = 0; +static long tolimit = 0; +static u32 s_lowpc = 0; +static u32 s_highpc = 0; +static unsigned long s_textsize = 0; + +static int ssiz; +static char *sbuf; +static int s_scale; + +static int hz = 0; +static int hist_num_bins = 0; +static char hist_dimension[16] = "seconds"; +static char hist_dimension_abbrev = 's'; + +/* see profil(2) where this is describe (incorrectly) */ +#define SCALE_1_TO_1 0x10000L + +void profPut32(char *b, u32 v) +{ + b[0] = v & 255; + b[1] = (v >> 8) & 255; + b[2] = (v >> 16) & 255; + b[3] = (v >> 24) & 255; +} + +void profPut16(char *b, u16 v) +{ + b[0] = v & 255; + b[1] = (v >> 8) & 255; +} + +int profWrite8(FILE *f, u8 b) +{ + if(fwrite(&b, 1, 1, f) != 1) + return 1; + return 0; +} + +int profWrite32(FILE *f, u32 v) +{ + char buf[4]; + + profPut32(buf, v); + if(fwrite(buf, 1, 4, f) != 4) + return 1; + return 0; +} + +int profWrite(FILE *f, char *buf, unsigned int n) +{ + if(fwrite(buf, 1, n, f) != n) + return 1; + return 0; +} + +/* Control profiling; + profiling is what mcount checks to see if + all the data structures are ready. */ + +void profControl(int mode) +{ + if (mode) { + /* start */ +#ifdef PROFILING + cpuProfil(sbuf, ssiz, (u32)s_lowpc, s_scale); +#endif + profiling = 0; + } else { + /* stop */ +#ifdef PROFILING + cpuProfil(NULL, 0, 0, 0); +#endif + profiling = 3; + } +} + + +#define MSG N_("No space for profiling buffer(s)\n") + +void profStartup(u32 lowpc, u32 highpc) +{ + int monsize; + char *buffer; + int o; + + /* + * round lowpc and highpc to multiples of the density we're using + * so the rest of the scaling (here and in gprof) stays in ints. + */ + lowpc = ROUNDDOWN(lowpc, HISTFRACTION*sizeof(HISTCOUNTER)); + s_lowpc = lowpc; + highpc = ROUNDUP(highpc, HISTFRACTION*sizeof(HISTCOUNTER)); + s_highpc = highpc; + s_textsize = highpc - lowpc; + monsize = (s_textsize / HISTFRACTION); + buffer = (char *)calloc(1, 2*monsize ); + if ( buffer == NULL ) { + systemMessage(0, MSG); + return; + } + froms = (unsigned short *) calloc(1, 4*s_textsize / HASHFRACTION ); + if ( froms == NULL ) { + systemMessage(0, MSG); + free(buffer); + buffer = NULL; + return; + } + tolimit = s_textsize * ARCDENSITY / 100; + if ( tolimit < MINARCS ) { + tolimit = MINARCS; + } else if ( tolimit > 65534 ) { + tolimit = 65534; + } + tos = (struct tostruct *) calloc(1, tolimit * sizeof( struct tostruct ) ); + if ( tos == NULL ) { + systemMessage(0, MSG); + + free(buffer); + buffer = NULL; + + free(froms); + froms = NULL; + + return; + } + tos[0].link = 0; + sbuf = buffer; + ssiz = monsize; + if ( monsize <= 0 ) + return; + o = highpc - lowpc; + if( monsize < o ) + s_scale = (int)(( (float) monsize / o ) * SCALE_1_TO_1); + else + s_scale = SCALE_1_TO_1; + profControl(1); +} + +void profCleanup() +{ + FILE *fd; + int fromindex; + int endfrom; + u32 frompc; + int toindex; + struct gmon_hdr ghdr; + + profControl(0); + fd = fopen( "gmon.out" , "wb" ); + if ( fd == NULL ) { + systemMessage( 0, "mcount: gmon.out" ); + return; + } + + memcpy(&ghdr.cookie[0], GMON_MAGIC, 4); + profPut32((char *)ghdr.version, GMON_VERSION); + + if(fwrite(&ghdr, sizeof(ghdr), 1, fd) != 1) { + systemMessage(0, "mcount: gmon.out header"); + fclose(fd); + return; + } + + if(hz == 0) + hz = 100; + + hist_num_bins = ssiz; + + if(profWrite8(fd, GMON_TAG_TIME_HIST) || + profWrite32(fd, (u32)s_lowpc) || + profWrite32(fd, (u32)s_highpc) || + profWrite32(fd, hist_num_bins) || + profWrite32(fd, hz) || + profWrite(fd, hist_dimension, 15) || + profWrite(fd, &hist_dimension_abbrev, 1)) { + systemMessage(0, "mcount: gmon.out hist"); + fclose(fd); + return; + } + u16 *hist_sample = (u16 *)sbuf; + + u16 count; + int i; + + for(i = 0; i < hist_num_bins; ++i) { + profPut16((char *)&count, hist_sample[i]); + + if(fwrite(&count, sizeof(count), 1, fd) != 1) { + systemMessage(0, "mcount: gmon.out sample"); + fclose(fd); + return; + } + } + + endfrom = s_textsize / (HASHFRACTION * sizeof(*froms)); + for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) { + if ( froms[fromindex] == 0 ) { + continue; + } + frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms)); + for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) { + if(profWrite8(fd, GMON_TAG_CG_ARC) || + profWrite32(fd, (u32)frompc) || + profWrite32(fd, (u32)tos[toindex].selfpc) || + profWrite32(fd, tos[toindex].count)) { + systemMessage(0, "mcount: arc"); + fclose(fd); + return; + } + } + } + fclose(fd); +} + +void profCount() +{ + register u32 selfpc; + register unsigned short *frompcindex; + register struct tostruct *top; + register struct tostruct *prevtop; + register long toindex; + + /* + * find the return address for mcount, + * and the return address for mcount's caller. + */ + + /* selfpc = pc pushed by mcount call. + This identifies the function that was just entered. */ + selfpc = (u32) reg[14].I; + /* frompcindex = pc in preceding frame. + This identifies the caller of the function just entered. */ + frompcindex = (unsigned short *) reg[12].I; + /* + * check that we are profiling + * and that we aren't recursively invoked. + */ + if (profiling) { + goto out; + } + profiling++; + /* + * check that frompcindex is a reasonable pc value. + * for example: signal catchers get called from the stack, + * not from text space. too bad. + */ + frompcindex = (unsigned short *) ((long) frompcindex - (long) s_lowpc); + if ((unsigned long) frompcindex > s_textsize) { + goto done; + } + frompcindex = + &froms[((long) frompcindex) / (HASHFRACTION * sizeof(*froms))]; + toindex = *frompcindex; + if (toindex == 0) { + /* + * first time traversing this arc + */ + toindex = ++tos[0].link; + if (toindex >= tolimit) { + goto overflow; + } + *frompcindex = (unsigned short)toindex; + top = &tos[toindex]; + top->selfpc = selfpc; + top->count = 1; + top->link = 0; + goto done; + } + top = &tos[toindex]; + if (top->selfpc == selfpc) { + /* + * arc at front of chain; usual case. + */ + top->count++; + goto done; + } + /* + * have to go looking down chain for it. + * top points to what we are looking at, + * prevtop points to previous top. + * we know it is not at the head of the chain. + */ + for (; /* goto done */; ) { + if (top->link == 0) { + /* + * top is end of the chain and none of the chain + * had top->selfpc == selfpc. + * so we allocate a new tostruct + * and link it to the head of the chain. + */ + toindex = ++tos[0].link; + if (toindex >= tolimit) { + goto overflow; + } + top = &tos[toindex]; + top->selfpc = selfpc; + top->count = 1; + top->link = *frompcindex; + *frompcindex = (unsigned short)toindex; + goto done; + } + /* + * otherwise, check the next arc on the chain. + */ + prevtop = top; + top = &tos[top->link]; + if (top->selfpc == selfpc) { + /* + * there it is. + * increment its count + * move it to the head of the chain. + */ + top->count++; + toindex = prevtop->link; + prevtop->link = top->link; + top->link = *frompcindex; + *frompcindex = (unsigned short)toindex; + goto done; + } + + } + done: + profiling--; + /* and fall through */ + out: + return; /* normal return restores saved registers */ + + overflow: + profiling++; /* halt further profiling */ +#define TOLIMIT "mcount: tos overflow\n" + systemMessage(0, TOLIMIT); + goto out; +} + +void profSetHertz(int h) +{ + hz = h; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/prof/prof.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/prof/prof.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,34 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef VBA_PROF_PROF_H +#define VBA_PROF_PROF_H + +/* Control profiling; + profiling is what mcount checks to see if + all the data structures are ready. */ + +extern void profControl(int mode); +extern void profStartup(u32 lowpc, u32 highpc); +extern void profCleanup(); +extern void profCount(); + +extern void profSetHertz(int hertz); +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/Array.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/Array.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,40 @@ +/*************************************************************************** + * Copyright (C) 2008 by Sindre Aam�s * + * aamas@stud.ntnu.no * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License version 2 as * + * published by the Free Software Foundation. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License version 2 for more details. * + * * + * You should have received a copy of the GNU General Public License * + * version 2 along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifndef ARRAY_H +#define ARRAY_H + +#include + +template +class Array { + T *a; + std::size_t sz; + + Array(const Array &ar); + +public: + Array(const std::size_t size = 0) : a(size ? new T[size] : 0), sz(size) {} + ~Array() { delete []a; } + void reset(const std::size_t size) { delete []a; a = size ? new T[size] : 0; sz = size; } + std::size_t size() const { return sz; } + operator T*() { return a; } + operator const T*() const { return a; } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/Makefile.am Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,47 @@ +bin_PROGRAMS = VisualBoyAdvance + +noinst_PROGRAMS = TestEmu + +VisualBoyAdvance_SOURCES = \ + SDL.cpp \ + debugger.cpp \ + debugger.h \ + expr-lex.cpp \ + expr.cpp \ + expr.cpp.h \ + exprNode.cpp \ + exprNode.h \ + getopt.c \ + getopt.h \ + getopt1.c \ + ../AutoBuild.h \ + ../NLS.h \ + ../Port.h + +VisualBoyAdvance_LDADD = @VBA_LIBS@ @SDL_LIBS@ + +VisualBoyAdvance_DEPENDENCIES = @VBA_LIBS@ + +TestEmu_SOURCES = \ + TestEmu.cpp \ + debugger.cpp \ + debugger.h \ + expr-lex.cpp \ + expr.cpp \ + expr.cpp.h \ + exprNode.cpp \ + exprNode.h \ + ../AutoBuild.h \ + ../NLS.h \ + ../Port.h + +TestEmu_LDADD = @VBA_LIBS@ @SDL_LIBS@ + +TestEmu_DEPENDENCIES = @VBA_LIBS@ + +AM_CPPFLAGS = \ + -I$(top_srcdir)/src \ + -DSDL \ + -DSYSCONFDIR=\"$(sysconfdir)\" + +AM_CXXFLAGS = -fno-exceptions @SDL_CFLAGS@ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/RingBuffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/RingBuffer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,112 @@ +/*************************************************************************** + * Copyright (C) 2008 by Sindre Aamås * + * aamas@stud.ntnu.no * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License version 2 as * + * published by the Free Software Foundation. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License version 2 for more details. * + * * + * You should have received a copy of the GNU General Public License * + * version 2 along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifndef RINGBUFFER_H +#define RINGBUFFER_H + +#include "Array.h" +#include +#include +#include + +template +class RingBuffer { + Array buf; + std::size_t sz; + std::size_t rpos; + std::size_t wpos; + +public: + RingBuffer(const std::size_t sz_in = 0) : sz(0), rpos(0), wpos(0) { reset(sz_in); } + + std::size_t avail() const { + return (wpos < rpos ? 0 : sz) + rpos - wpos - 1; + } + + void clear() { + wpos = rpos = 0; + } + + void fill(T value); + + void read(T *out, std::size_t num); + + void reset(std::size_t sz_in); + + std::size_t size() const { + return sz - 1; + } + + std::size_t used() const { + return (wpos < rpos ? sz : 0) + wpos - rpos; + } + + void write(const T *in, std::size_t num); +}; + +template +void RingBuffer::fill(const T value) { + std::fill(buf + 0, buf + sz, value); + rpos = 0; + wpos = sz - 1; +} + +template +void RingBuffer::read(T *out, std::size_t num) { + if (rpos + num > sz) { + const std::size_t n = sz - rpos; + + std::memcpy(out, buf + rpos, n * sizeof(T)); + + rpos = 0; + num -= n; + out += n; + } + + std::memcpy(out, buf + rpos, num * sizeof(T)); + + if ((rpos += num) == sz) + rpos = 0; +} + +template +void RingBuffer::reset(const std::size_t sz_in) { + sz = sz_in + 1; + rpos = wpos = 0; + buf.reset(sz_in ? sz : 0); +} + +template +void RingBuffer::write(const T *in, std::size_t num) { + if (wpos + num > sz) { + const std::size_t n = sz - wpos; + + std::memcpy(buf + wpos, in, n * sizeof(T)); + + wpos = 0; + num -= n; + in += n; + } + + std::memcpy(buf + wpos, in, num * sizeof(T)); + + if ((wpos += num) == sz) + wpos = 0; +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/SDL.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/SDL.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,3623 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include +#include +#include +#include + +#include "AutoBuild.h" + +#include "Port.h" +#include "SDL.h" +#include "debugger.h" +#include "gba/GBA.h" +#include "gba/GBAGlobals.h" +#include "gba/agbprint.h" +#include "gba/Flash.h" +#include "gba/RTC.h" +#include "gba/GBASound.h" +#include "gb/GB.h" +#include "gb/gbGlobals.h" +#include "common/Text.h" +#include "common/unzip.h" +#include "common/Util.h" +#include "common/movie.h" +#include "common/System.h" +#include "common/inputGlobal.h" +#include "../common/vbalua.h" +#include "SoundSDL.h" + + +#define GBC_CAPABLE ((gbRom[0x143] & 0x80) != 0) +#define SGB_CAPABLE (gbRom[0x146] == 0x03) + +#ifndef WIN32 +# include +# define GETCWD getcwd +#else // WIN32 +# include +# define GETCWD _getcwd +#endif // WIN32 + +#ifndef __GNUC__ +# define HAVE_DECL_GETOPT 0 +# define __STDC__ 1 +# include "getopt.h" +#else // ! __GNUC__ +# define HAVE_DECL_GETOPT 1 +# include "getopt.h" +#endif // ! __GNUC__ + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif +extern bool8 soundEcho; +extern bool8 soundLowPass; +extern bool8 soundReverse; +extern int Init_2xSaI(u32); +extern void _2xSaI(u8*,u32,u8*,u8*,u32,int,int); +extern void _2xSaI32(u8*,u32,u8*,u8*,u32,int,int); +extern void Super2xSaI(u8*,u32,u8*,u8*,u32,int,int); +extern void Super2xSaI32(u8*,u32,u8*,u8*,u32,int,int); +extern void SuperEagle(u8*,u32,u8*,u8*,u32,int,int); +extern void SuperEagle32(u8*,u32,u8*,u8*,u32,int,int); +extern void Pixelate2x16(u8*,u32,u8*,u8*,u32,int,int); +extern void Pixelate2x32(u8*,u32,u8*,u8*,u32,int,int); +extern void MotionBlur(u8*,u32,u8*,u8*,u32,int,int); +extern void MotionBlur32(u8*,u32,u8*,u8*,u32,int,int); +extern void AdMame2x(u8*,u32,u8*,u8*,u32,int,int); +extern void AdMame2x32(u8*,u32,u8*,u8*,u32,int,int); +extern void Simple2x16(u8*,u32,u8*,u8*,u32,int,int); +extern void Simple2x32(u8*,u32,u8*,u8*,u32,int,int); +extern void Bilinear(u8*,u32,u8*,u8*,u32,int,int); +extern void Bilinear32(u8*,u32,u8*,u8*,u32,int,int); +extern void BilinearPlus(u8*,u32,u8*,u8*,u32,int,int); +extern void BilinearPlus32(u8*,u32,u8*,u8*,u32,int,int); +extern void Scanlines(u8*,u32,u8*,u8*,u32,int,int); +extern void Scanlines32(u8*,u32,u8*,u8*,u32,int,int); +extern void ScanlinesTV(u8*,u32,u8*,u8*,u32,int,int); +extern void ScanlinesTV32(u8*,u32,u8*,u8*,u32,int,int); +extern void hq2x(u8*,u32,u8*,u8*,u32,int,int); +extern void hq2x32(u8*,u32,u8*,u8*,u32,int,int); +extern void lq2x(u8*,u32,u8*,u8*,u32,int,int); +extern void lq2x32(u8*,u32,u8*,u8*,u32,int,int); + +extern void SmartIB(u8*,u32,int,int); +extern void SmartIB32(u8*,u32,int,int); +extern void MotionBlurIB(u8*,u32,int,int); +extern void MotionBlurIB32(u8*,u32,int,int); + +void Init_Overlay(SDL_Surface *surface, int overlaytype); +void Quit_Overlay(void); +void Draw_Overlay(SDL_Surface *surface, int size); + +extern void remoteInit(); +extern void remoteCleanUp(); +extern void remoteStubMain(); +extern void remoteStubSignal(int,int); +extern void remoteOutput(char *, u32); +extern void remoteSetProtocol(int); +extern void remoteSetPort(int); +extern void debuggerOutput(char *, u32); + +extern void CPUUpdateRenderBuffers(bool); + +struct EmulatedSystem theEmulator = { + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + false, + 0 +}; + +SDL_Surface *surface = NULL; +SDL_Overlay *overlay = NULL; +SDL_Rect overlay_rect; + +int systemSpeed = 0; +int systemRedShift = 0; +int systemBlueShift = 0; +int systemGreenShift = 0; +int systemColorDepth = 0; +int systemDebug = 0; +int systemVerbose = 0; +int systemFrameSkip = 0; +int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + +int srcPitch = 0; +int srcWidth = 0; +int srcHeight = 0; +int destWidth = 0; +int destHeight = 0; + +int sensorX = 2047; +int sensorY = 2047; +bool sensorOn = false; + +int filter = 0; +u8 *delta = NULL; + +int sdlPrintUsage = 0; +int disableMMX = 0; + +int systemCartridgeType = 3; +int sizeOption = 0; +int captureFormat = 0; +int useMovie = 0; + +int pauseWhenInactive = 0; +int active = 1; +int emulating = 0; +int RGB_LOW_BITS_MASK=0x821; +u32 systemColorMap32[0x10000]; +u16 systemColorMap16[0x10000]; +u16 systemGbPalette[24]; +void (*filterFunction)(u8*,u32,u8*,u8*,u32,int,int) = NULL; +void (*ifbFunction)(u8*,u32,int,int) = NULL; +int ifbType = 0; +char filename[2048]; +char ipsname[2048]; +char biosFileName[2048]; +char movieFileName[2048]; +char captureDir[2048]; +char saveDir[2048]; +char batteryDir[2048]; + +static char *rewindMemory = NULL; +static int rewindPos = 0; +static int rewindTopPos = 0; +static int rewindCounter = 0; +static int rewindCount = 0; +static bool rewindSaveNeeded = false; +static int rewindTimer = 0; + +#define REWIND_SIZE 400000 + +#define _stricmp strcasecmp + +/*bool sdlButtons[4][12] = { + { false, false, false, false, false, false, + false, false, false, false, false, false }, + { false, false, false, false, false, false, + false, false, false, false, false, false }, + { false, false, false, false, false, false, + false, false, false, false, false, false }, + { false, false, false, false, false, false, + false, false, false, false, false, false } +};*/ +/* + I'm changing the way the SDL GUI handles the button + input to match the one in win32, this is needed in + order to be compatible with the format required by + common/movie.cpp + --Felipe +*/ + +u16 currentButtons[4] = {0, 0, 0, 0}; + +bool sdlMotionButtons[4] = { false, false, false, false }; +const int32 INITIAL_SENSOR_VALUE = 2047; + +int sdlNumDevices = 0; +SDL_Joystick **sdlDevices = NULL; + +bool wasPaused = false; +int autoFrameSkip = 0; +int frameskipadjust = 0; +int showRenderedFrames = 0; +int renderedFrames = 0; + +int throttle = 0; +u32 throttleLastTime = 0; +u32 autoFrameSkipLastTime = 0; + +int showSpeed = 1; +int showSpeedTransparent = 1; +bool disableStatusMessages = false; +bool paused = false; +bool pauseNextFrame = false; +bool debugger = false; +bool debuggerStub = false; +int fullscreen = 0; +bool systemSoundOn = false; +bool yuv = false; +int yuvType = 0; +bool removeIntros = false; +int sdlFlashSize = 0; +int sdlAutoIPS = 1; +int sdlRtcEnable = 0; +int sdlAgbPrint = 0; + +int sdlDefaultJoypad = 0; + +extern void debuggerSignal(int,int); + +void (*dbgMain)() = debuggerMain; +void (*dbgSignal)(int,int) = debuggerSignal; +void (*dbgOutput)(char *, u32) = debuggerOutput; + +int mouseCounter = 0; +int autoFire = 0; +bool autoFireToggle = false; + +bool screenMessage[8] = {false,false,false,false,false,false,false,false}; +char screenMessageBuffer[8][21]; +u32 screenMessageTime[8] = {0,0,0,0,0,0,0,0}; +u32 screenMessageDuration[8] = {0,0,0,0,0,0,0,0}; + +SDL_cond *cond = NULL; +SDL_mutex *mutex = NULL; +u8* sdlBuffer; +int sdlSoundLen = 0; +SoundSDL* soundDriver = NULL; + +char *arg0; + +#ifndef C_CORE +u8 sdlStretcher[16384]; +int sdlStretcherPos; +#else +void (*sdlStretcher)(u8 *, u8*) = NULL; +#endif + +u16 joypad[4][12] = { + { SDLK_LEFT, SDLK_RIGHT, + SDLK_UP, SDLK_DOWN, + SDLK_z, SDLK_x, + SDLK_RETURN,SDLK_BACKSPACE, + SDLK_a, SDLK_s, + SDLK_SPACE, SDLK_F12 + }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } +}; + +u16 defaultJoypad[12] = { + SDLK_LEFT, SDLK_RIGHT, + SDLK_UP, SDLK_DOWN, + SDLK_z, SDLK_x, + SDLK_RETURN,SDLK_BACKSPACE, + SDLK_a, SDLK_s, + SDLK_SPACE, SDLK_F12 +}; + +u16 motion[4] = { + SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2 +}; + +u16 defaultMotion[4] = { + SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2 +}; + +struct option sdlOptions[] = { + { "agb-print", no_argument, &sdlAgbPrint, 1 }, + { "auto-frameskip", no_argument, &autoFrameSkip, 1 }, + { "bios", required_argument, 0, 'b' }, + { "config", required_argument, 0, 'c' }, + { "debug", no_argument, 0, 'd' }, + { "filter", required_argument, 0, 'f' }, + { "filter-normal", no_argument, &filter, 0 }, + { "filter-tv-mode", no_argument, &filter, 1 }, + { "filter-2xsai", no_argument, &filter, 2 }, + { "filter-super-2xsai", no_argument, &filter, 3 }, + { "filter-super-eagle", no_argument, &filter, 4 }, + { "filter-pixelate", no_argument, &filter, 5 }, + { "filter-motion-blur", no_argument, &filter, 6 }, + { "filter-advmame", no_argument, &filter, 7 }, + { "filter-simple2x", no_argument, &filter, 8 }, + { "filter-bilinear", no_argument, &filter, 9 }, + { "filter-bilinear+", no_argument, &filter, 10 }, + { "filter-scanlines", no_argument, &filter, 11 }, + { "filter-hq2x", no_argument, &filter, 12 }, + { "filter-lq2x", no_argument, &filter, 13 }, + { "flash-size", required_argument, 0, 'S' }, + { "flash-64k", no_argument, &sdlFlashSize, 0 }, + { "flash-128k", no_argument, &sdlFlashSize, 1 }, + { "frameskip", required_argument, 0, 's' }, + { "fullscreen", no_argument, &fullscreen, 1 }, + { "gdb", required_argument, 0, 'G' }, + { "help", no_argument, &sdlPrintUsage, 1 }, + { "ifb-none", no_argument, &ifbType, 0 }, + { "ifb-motion-blur", no_argument, &ifbType, 1 }, + { "ifb-smart", no_argument, &ifbType, 2 }, + { "ips", required_argument, 0, 'i' }, + { "no-agb-print", no_argument, &sdlAgbPrint, 0 }, + { "no-auto-frameskip", no_argument, &autoFrameSkip, 0 }, + { "no-debug", no_argument, 0, 'N' }, + { "no-ips", no_argument, &sdlAutoIPS, 0 }, + { "no-mmx", no_argument, &disableMMX, 1 }, + { "no-pause-when-inactive", no_argument, &pauseWhenInactive, 0 }, + { "no-rtc", no_argument, &sdlRtcEnable, 0 }, + { "no-show-speed", no_argument, &showSpeed, 0 }, + { "no-throttle", no_argument, &throttle, 0 }, + { "pause-when-inactive", no_argument, &pauseWhenInactive, 1 }, + { "profile", optional_argument, 0, 'P' }, + { "rtc", no_argument, &sdlRtcEnable, 1 }, + { "save-type", required_argument, 0, 't' }, + { "save-auto", no_argument, &cpuSaveType, 0 }, + { "save-eeprom", no_argument, &cpuSaveType, 1 }, + { "save-sram", no_argument, &cpuSaveType, 2 }, + { "save-flash", no_argument, &cpuSaveType, 3 }, + { "save-sensor", no_argument, &cpuSaveType, 4 }, + { "save-none", no_argument, &cpuSaveType, 5 }, + { "show-speed-normal", no_argument, &showSpeed, 1 }, + { "show-speed-detailed", no_argument, &showSpeed, 2 }, + { "throttle", required_argument, 0, 'T' }, + { "verbose", required_argument, 0, 'v' }, + { "video-1x", no_argument, &sizeOption, 0 }, + { "video-2x", no_argument, &sizeOption, 1 }, + { "video-3x", no_argument, &sizeOption, 2 }, + { "video-4x", no_argument, &sizeOption, 3 }, + { "yuv", required_argument, 0, 'Y' }, + { "recordmovie", required_argument, 0, 'r' }, + { "playmovie", required_argument, 0, 'p' }, + { "watchmovie", required_argument, 0, 'w' }, + { NULL, no_argument, NULL, 0 } +}; + +#ifndef C_CORE +#define SDL_LONG(val) \ + *((u32 *)&sdlStretcher[sdlStretcherPos]) = val;\ + sdlStretcherPos+=4; + +#define SDL_AND_EAX(val) \ + sdlStretcher[sdlStretcherPos++] = 0x25;\ + SDL_LONG(val); + +#define SDL_AND_EBX(val) \ + sdlStretcher[sdlStretcherPos++] = 0x81;\ + sdlStretcher[sdlStretcherPos++] = 0xe3;\ + SDL_LONG(val); + +#define SDL_OR_EAX_EBX \ + sdlStretcher[sdlStretcherPos++] = 0x09;\ + sdlStretcher[sdlStretcherPos++] = 0xd8; + +#define SDL_LOADL_EBX \ + sdlStretcher[sdlStretcherPos++] = 0x8b;\ + sdlStretcher[sdlStretcherPos++] = 0x1f; + +#define SDL_LOADW \ + sdlStretcher[sdlStretcherPos++] = 0x66;\ + sdlStretcher[sdlStretcherPos++] = 0x8b;\ + sdlStretcher[sdlStretcherPos++] = 0x06;\ + sdlStretcher[sdlStretcherPos++] = 0x83;\ + sdlStretcher[sdlStretcherPos++] = 0xc6;\ + sdlStretcher[sdlStretcherPos++] = 0x02; + +#define SDL_LOADL \ + sdlStretcher[sdlStretcherPos++] = 0x8b;\ + sdlStretcher[sdlStretcherPos++] = 0x06;\ + sdlStretcher[sdlStretcherPos++] = 0x83;\ + sdlStretcher[sdlStretcherPos++] = 0xc6;\ + sdlStretcher[sdlStretcherPos++] = 0x04; + +#define SDL_LOADL2 \ + sdlStretcher[sdlStretcherPos++] = 0x8b;\ + sdlStretcher[sdlStretcherPos++] = 0x06;\ + sdlStretcher[sdlStretcherPos++] = 0x83;\ + sdlStretcher[sdlStretcherPos++] = 0xc6;\ + sdlStretcher[sdlStretcherPos++] = 0x03; + +#define SDL_STOREW \ + sdlStretcher[sdlStretcherPos++] = 0x66;\ + sdlStretcher[sdlStretcherPos++] = 0x89;\ + sdlStretcher[sdlStretcherPos++] = 0x07;\ + sdlStretcher[sdlStretcherPos++] = 0x83;\ + sdlStretcher[sdlStretcherPos++] = 0xc7;\ + sdlStretcher[sdlStretcherPos++] = 0x02; + +#define SDL_STOREL \ + sdlStretcher[sdlStretcherPos++] = 0x89;\ + sdlStretcher[sdlStretcherPos++] = 0x07;\ + sdlStretcher[sdlStretcherPos++] = 0x83;\ + sdlStretcher[sdlStretcherPos++] = 0xc7;\ + sdlStretcher[sdlStretcherPos++] = 0x04; + +#define SDL_STOREL2 \ + sdlStretcher[sdlStretcherPos++] = 0x89;\ + sdlStretcher[sdlStretcherPos++] = 0x07;\ + sdlStretcher[sdlStretcherPos++] = 0x83;\ + sdlStretcher[sdlStretcherPos++] = 0xc7;\ + sdlStretcher[sdlStretcherPos++] = 0x03; + +#define SDL_RET \ + sdlStretcher[sdlStretcherPos++] = 0xc3; + +#define SDL_PUSH_EAX \ + sdlStretcher[sdlStretcherPos++] = 0x50; + +#define SDL_PUSH_ECX \ + sdlStretcher[sdlStretcherPos++] = 0x51; + +#define SDL_PUSH_EBX \ + sdlStretcher[sdlStretcherPos++] = 0x53; + +#define SDL_PUSH_ESI \ + sdlStretcher[sdlStretcherPos++] = 0x56; + +#define SDL_PUSH_EDI \ + sdlStretcher[sdlStretcherPos++] = 0x57; + +#define SDL_POP_EAX \ + sdlStretcher[sdlStretcherPos++] = 0x58; + +#define SDL_POP_ECX \ + sdlStretcher[sdlStretcherPos++] = 0x59; + +#define SDL_POP_EBX \ + sdlStretcher[sdlStretcherPos++] = 0x5b; + +#define SDL_POP_ESI \ + sdlStretcher[sdlStretcherPos++] = 0x5e; + +#define SDL_POP_EDI \ + sdlStretcher[sdlStretcherPos++] = 0x5f; + +#define SDL_MOV_ECX(val) \ + sdlStretcher[sdlStretcherPos++] = 0xb9;\ + SDL_LONG(val); + +#define SDL_REP_MOVSB \ + sdlStretcher[sdlStretcherPos++] = 0xf3;\ + sdlStretcher[sdlStretcherPos++] = 0xa4; + +#define SDL_REP_MOVSW \ + sdlStretcher[sdlStretcherPos++] = 0xf3;\ + sdlStretcher[sdlStretcherPos++] = 0x66;\ + sdlStretcher[sdlStretcherPos++] = 0xa5; + +#define SDL_REP_MOVSL \ + sdlStretcher[sdlStretcherPos++] = 0xf3;\ + sdlStretcher[sdlStretcherPos++] = 0xa5; + +void sdlMakeStretcher(int width) +{ + sdlStretcherPos = 0; + switch(systemColorDepth) { + case 16: + if(sizeOption) { + SDL_PUSH_EAX; + SDL_PUSH_ESI; + SDL_PUSH_EDI; + for(int i = 0; i < width; i++) { + SDL_LOADW; + SDL_STOREW; + SDL_STOREW; + if(sizeOption > 1) { + SDL_STOREW; + } + if(sizeOption > 2) { + SDL_STOREW; + } + } + SDL_POP_EDI; + SDL_POP_ESI; + SDL_POP_EAX; + SDL_RET; + } else { + SDL_PUSH_ESI; + SDL_PUSH_EDI; + SDL_PUSH_ECX; + SDL_MOV_ECX(width); + SDL_REP_MOVSW; + SDL_POP_ECX; + SDL_POP_EDI; + SDL_POP_ESI; + SDL_RET; + } + break; + case 24: + if(sizeOption) { + SDL_PUSH_EAX; + SDL_PUSH_ESI; + SDL_PUSH_EDI; + int w = width - 1; + for(int i = 0; i < w; i++) { + SDL_LOADL2; + SDL_STOREL2; + SDL_STOREL2; + if(sizeOption > 1) { + SDL_STOREL2; + } + if(sizeOption > 2) { + SDL_STOREL2; + } + } + // need to write the last one + SDL_LOADL2; + SDL_STOREL2; + if(sizeOption > 1) { + SDL_STOREL2; + } + if(sizeOption > 2) { + SDL_STOREL2; + } + SDL_AND_EAX(0x00ffffff); + SDL_PUSH_EBX; + SDL_LOADL_EBX; + SDL_AND_EBX(0xff000000); + SDL_OR_EAX_EBX; + SDL_POP_EBX; + SDL_STOREL2; + SDL_POP_EDI; + SDL_POP_ESI; + SDL_POP_EAX; + SDL_RET; + } else { + SDL_PUSH_ESI; + SDL_PUSH_EDI; + SDL_PUSH_ECX; + SDL_MOV_ECX(3*width); + SDL_REP_MOVSB; + SDL_POP_ECX; + SDL_POP_EDI; + SDL_POP_ESI; + SDL_RET; + } + break; + case 32: + if(sizeOption) { + SDL_PUSH_EAX; + SDL_PUSH_ESI; + SDL_PUSH_EDI; + for(int i = 0; i < width; i++) { + SDL_LOADL; + SDL_STOREL; + SDL_STOREL; + if(sizeOption > 1) { + SDL_STOREL; + } + if(sizeOption > 2) { + SDL_STOREL; + } + } + SDL_POP_EDI; + SDL_POP_ESI; + SDL_POP_EAX; + SDL_RET; + } else { + SDL_PUSH_ESI; + SDL_PUSH_EDI; + SDL_PUSH_ECX; + SDL_MOV_ECX(width); + SDL_REP_MOVSL; + SDL_POP_ECX; + SDL_POP_EDI; + SDL_POP_ESI; + SDL_RET; + } + break; + } +} + +#ifdef _MSC_VER +#define SDL_CALL_STRETCHER \ + {\ + __asm mov eax, stretcher\ + __asm mov edi, dest\ + __asm mov esi, src\ + __asm call eax\ + } +#else +#define SDL_CALL_STRETCHER \ + asm volatile("call *%%eax"::"a" (stretcher),"S" (src),"D" (dest)) +#endif +#else +#define SDL_CALL_STRETCHER \ + sdlStretcher(src, dest) + +void sdlStretch16x1(u8 *src, u8 *dest) +{ + u16 *s = (u16 *)src; + u16 *d = (u16 *)dest; + for(int i = 0; i < srcWidth; i++) + *d++ = *s++; +} + +void sdlStretch16x2(u8 *src, u8 *dest) +{ + u16 *s = (u16 *)src; + u16 *d = (u16 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *s++; + } +} + +void sdlStretch16x3(u8 *src, u8 *dest) +{ + u16 *s = (u16 *)src; + u16 *d = (u16 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *s; + *d++ = *s++; + } +} + +void sdlStretch16x4(u8 *src, u8 *dest) +{ + u16 *s = (u16 *)src; + u16 *d = (u16 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *s; + *d++ = *s; + *d++ = *s++; + } +} + +void (*sdlStretcher16[4])(u8 *, u8 *) = { + sdlStretch16x1, + sdlStretch16x2, + sdlStretch16x3, + sdlStretch16x4 +}; + +void sdlStretch32x1(u8 *src, u8 *dest) +{ + u32 *s = (u32 *)src; + u32 *d = (u32 *)dest; + for(int i = 0; i < srcWidth; i++) + *d++ = *s++; +} + +void sdlStretch32x2(u8 *src, u8 *dest) +{ + u32 *s = (u32 *)src; + u32 *d = (u32 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *s++; + } +} + +void sdlStretch32x3(u8 *src, u8 *dest) +{ + u32 *s = (u32 *)src; + u32 *d = (u32 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *s; + *d++ = *s++; + } +} + +void sdlStretch32x4(u8 *src, u8 *dest) +{ + u32 *s = (u32 *)src; + u32 *d = (u32 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *s; + *d++ = *s; + *d++ = *s++; + } +} + +void (*sdlStretcher32[4])(u8 *, u8 *) = { + sdlStretch32x1, + sdlStretch32x2, + sdlStretch32x3, + sdlStretch32x4 +}; + +void sdlStretch24x1(u8 *src, u8 *dest) +{ + u8 *s = src; + u8 *d = dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s++; + *d++ = *s++; + *d++ = *s++; + } +} + +void sdlStretch24x2(u8 *src, u8 *dest) +{ + u8 *s = (u8 *)src; + u8 *d = (u8 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + } +} + +void sdlStretch24x3(u8 *src, u8 *dest) +{ + u8 *s = (u8 *)src; + u8 *d = (u8 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + } +} + +void sdlStretch24x4(u8 *src, u8 *dest) +{ + u8 *s = (u8 *)src; + u8 *d = (u8 *)dest; + for(int i = 0; i < srcWidth; i++) { + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + *d++ = *s; + *d++ = *(s+1); + *d++ = *(s+2); + s += 3; + } +} + +void (*sdlStretcher24[4])(u8 *, u8 *) = { + sdlStretch24x1, + sdlStretch24x2, + sdlStretch24x3, + sdlStretch24x4 +}; + +#endif + +u32 sdlFromHex(char *s) +{ + u32 value; + sscanf(s, "%x", &value); + return value; +} + +#ifdef __MSC__ +#define stat _stat +#define S_IFDIR _S_IFDIR +#endif + +void sdlCheckDirectory(char *dir) +{ + struct stat buf; + + int len = strlen(dir); + + char *p = dir + len - 1; + + if(*p == '/' || + *p == '\\') + *p = 0; + + if(stat(dir, &buf) == 0) { + if(!(buf.st_mode & S_IFDIR)) { + fprintf(stderr, "Error: %s is not a directory\n", dir); + dir[0] = 0; + } + } else { + fprintf(stderr, "Error: %s does not exist\n", dir); + dir[0] = 0; + } +} + +char *sdlGetFilename(char *name) +{ + static char filebuffer[2048]; + + int len = strlen(name); + + char *p = name + len - 1; + + while(true) { + if(*p == '/' || + *p == '\\') { + p++; + break; + } + len--; + p--; + if(len == 0) + break; + } + + if(len == 0) + strcpy(filebuffer, name); + else + strcpy(filebuffer, p); + return filebuffer; +} + +FILE *sdlFindFile(const char *name) +{ + char buffer[4096]; + char path[2048]; + +#ifdef WIN32 +#define PATH_SEP ";" +#define FILE_SEP '\\' +#define EXE_NAME "VisualBoyAdvance-SDL.exe" +#else // ! WIN32 +#define PATH_SEP ":" +#define FILE_SEP '/' +#define EXE_NAME "VisualBoyAdvance" +#endif // ! WIN32 + + fprintf(stderr, "Searching for file %s\n", name); + + if(GETCWD(buffer, 2048)) { + fprintf(stderr, "Searching current directory: %s\n", buffer); + } + + FILE *f = fopen(name, "r"); + if(f != NULL) { + return f; + } + + char *home = getenv("HOME"); + + if(home != NULL) { + fprintf(stderr, "Searching home directory: %s\n", home); + sprintf(path, "%s%c%s", home, FILE_SEP, name); + f = fopen(path, "r"); + if(f != NULL) + return f; + } + +#ifdef WIN32 + home = getenv("USERPROFILE"); + if(home != NULL) { + fprintf(stderr, "Searching user profile directory: %s\n", home); + sprintf(path, "%s%c%s", home, FILE_SEP, name); + f = fopen(path, "r"); + if(f != NULL) + return f; + } +#else // ! WIN32 + fprintf(stderr, "Searching system config directory: %s\n", SYSCONFDIR); + sprintf(path, "%s%c%s", SYSCONFDIR, FILE_SEP, name); + f = fopen(path, "r"); + if(f != NULL) + return f; +#endif // ! WIN32 + + if(!strchr(arg0, '/') && + !strchr(arg0, '\\')) { + char *path = getenv("PATH"); + + if(path != NULL) { + fprintf(stderr, "Searching PATH\n"); + strncpy(buffer, path, 4096); + buffer[4095] = 0; + char *tok = strtok(buffer, PATH_SEP); + + while(tok) { + sprintf(path, "%s%c%s", tok, FILE_SEP, EXE_NAME); + f = fopen(path, "r"); + if(f != NULL) { + char path2[2048]; + fclose(f); + sprintf(path2, "%s%c%s", tok, FILE_SEP, name); + f = fopen(path2, "r"); + if(f != NULL) { + fprintf(stderr, "Found at %s\n", path2); + return f; + } + } + tok = strtok(NULL, PATH_SEP); + } + } + } else { + // executable is relative to some directory + fprintf(stderr, "Searching executable directory\n"); + strcpy(buffer, arg0); + char *p = strrchr(buffer, FILE_SEP); + if(p) { + *p = 0; + sprintf(path, "%s%c%s", buffer, FILE_SEP, name); + f = fopen(path, "r"); + if(f != NULL) + return f; + } + } + return NULL; +} + +void sdlReadPreferences(FILE *f) +{ + char buffer[2048]; + + while(1) { + char *s = fgets(buffer, 2048, f); + + if(s == NULL) + break; + + char *p = strchr(s, '#'); + + if(p) + *p = 0; + + char *token = strtok(s, " \t\n\r="); + + if(!token) + continue; + + if(strlen(token) == 0) + continue; + + char *key = token; + char *value = strtok(NULL, "\t\n\r"); + + if(value == NULL) { + fprintf(stderr, "Empty value for key %s\n", key); + continue; + } + + if(!strcmp(key,"Joy0_Left")) { + joypad[0][KEY_LEFT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_Right")) { + joypad[0][KEY_RIGHT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_Up")) { + joypad[0][KEY_UP] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_Down")) { + joypad[0][KEY_DOWN] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_A")) { + joypad[0][KEY_BUTTON_A] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_B")) { + joypad[0][KEY_BUTTON_B] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_L")) { + joypad[0][KEY_BUTTON_L] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_R")) { + joypad[0][KEY_BUTTON_R] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_Start")) { + joypad[0][KEY_BUTTON_START] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_Select")) { + joypad[0][KEY_BUTTON_SELECT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_Speed")) { + joypad[0][KEY_BUTTON_SPEED] = sdlFromHex(value); + } else if(!strcmp(key, "Joy0_Capture")) { + joypad[0][KEY_BUTTON_CAPTURE] = sdlFromHex(value); + } else if(!strcmp(key,"Joy1_Left")) { + joypad[1][KEY_LEFT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_Right")) { + joypad[1][KEY_RIGHT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_Up")) { + joypad[1][KEY_UP] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_Down")) { + joypad[1][KEY_DOWN] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_A")) { + joypad[1][KEY_BUTTON_A] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_B")) { + joypad[1][KEY_BUTTON_B] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_L")) { + joypad[1][KEY_BUTTON_L] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_R")) { + joypad[1][KEY_BUTTON_R] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_Start")) { + joypad[1][KEY_BUTTON_START] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_Select")) { + joypad[1][KEY_BUTTON_SELECT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_Speed")) { + joypad[1][KEY_BUTTON_SPEED] = sdlFromHex(value); + } else if(!strcmp(key, "Joy1_Capture")) { + joypad[1][KEY_BUTTON_CAPTURE] = sdlFromHex(value); + } else if(!strcmp(key,"Joy2_Left")) { + joypad[2][KEY_LEFT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_Right")) { + joypad[2][KEY_RIGHT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_Up")) { + joypad[2][KEY_UP] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_Down")) { + joypad[2][KEY_DOWN] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_A")) { + joypad[2][KEY_BUTTON_A] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_B")) { + joypad[2][KEY_BUTTON_B] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_L")) { + joypad[2][KEY_BUTTON_L] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_R")) { + joypad[2][KEY_BUTTON_R] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_Start")) { + joypad[2][KEY_BUTTON_START] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_Select")) { + joypad[2][KEY_BUTTON_SELECT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_Speed")) { + joypad[2][KEY_BUTTON_SPEED] = sdlFromHex(value); + } else if(!strcmp(key, "Joy2_Capture")) { + joypad[2][KEY_BUTTON_CAPTURE] = sdlFromHex(value); + } else if(!strcmp(key,"Joy4_Left")) { + joypad[4][KEY_LEFT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_Right")) { + joypad[4][KEY_RIGHT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_Up")) { + joypad[4][KEY_UP] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_Down")) { + joypad[4][KEY_DOWN] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_A")) { + joypad[4][KEY_BUTTON_A] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_B")) { + joypad[4][KEY_BUTTON_B] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_L")) { + joypad[4][KEY_BUTTON_L] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_R")) { + joypad[4][KEY_BUTTON_R] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_Start")) { + joypad[4][KEY_BUTTON_START] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_Select")) { + joypad[4][KEY_BUTTON_SELECT] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_Speed")) { + joypad[4][KEY_BUTTON_SPEED] = sdlFromHex(value); + } else if(!strcmp(key, "Joy4_Capture")) { + joypad[4][KEY_BUTTON_CAPTURE] = sdlFromHex(value); + } else if(!strcmp(key, "Motion_Left")) { + motion[KEY_LEFT] = sdlFromHex(value); + } else if(!strcmp(key, "Motion_Right")) { + motion[KEY_RIGHT] = sdlFromHex(value); + } else if(!strcmp(key, "Motion_Up")) { + motion[KEY_UP] = sdlFromHex(value); + } else if(!strcmp(key, "Motion_Down")) { + motion[KEY_DOWN] = sdlFromHex(value); + } else if(!strcmp(key, "frameSkip")) { + frameSkip = sdlFromHex(value); + if(frameSkip < 0 || frameSkip > 9) + frameSkip = 2; + } else if(!strcmp(key, "gbFrameSkip")) { + gbFrameSkip = sdlFromHex(value); + if(gbFrameSkip < 0 || gbFrameSkip > 9) + gbFrameSkip = 0; + } else if(!strcmp(key, "video")) { + sizeOption = sdlFromHex(value); + if(sizeOption < 0 || sizeOption > 3) + sizeOption = 1; + } else if(!strcmp(key, "fullScreen")) { + fullscreen = sdlFromHex(value) ? 1 : 0; + } else if(!strcmp(key, "useBios")) { + useBios = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "skipBios")) { + skipBios = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "biosFile")) { + strcpy(biosFileName, value); + } else if(!strcmp(key, "filter")) { + filter = sdlFromHex(value); + if(filter < 0 || filter > 13) + filter = 0; + } else if(!strcmp(key, "disableStatus")) { + disableStatusMessages = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "borderOn")) { + gbBorderOn = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "borderAutomatic")) { + gbBorderAutomatic = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "emulatorType")) { + gbEmulatorType = sdlFromHex(value); + if(gbEmulatorType < 0 || gbEmulatorType > 5) + gbEmulatorType = 1; + } else if(!strcmp(key, "colorOption")) { + gbColorOption = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "captureDir")) { + sdlCheckDirectory(value); + strcpy(captureDir, value); + } else if(!strcmp(key, "saveDir")) { + sdlCheckDirectory(value); + strcpy(saveDir, value); + } else if(!strcmp(key, "batteryDir")) { + sdlCheckDirectory(value); + strcpy(batteryDir, value); + } else if(!strcmp(key, "captureFormat")) { + captureFormat = sdlFromHex(value); + } else if(!strcmp(key, "soundQuality")) { + soundQuality = sdlFromHex(value); + switch(soundQuality) { + case 1: break; + default: + fprintf(stderr, "The rerecording version will run only sound at highest quality. Defaulting to 44.1 KHz\n"); + soundQuality = 1; + break; + } + } else if(!strcmp(key, "soundOff")) { + soundOffFlag = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "soundEnable")) { + int res = sdlFromHex(value) & 0x30f; + soundEnableChannels(res); + soundDisableChannels(~res); + } else if(!strcmp(key, "soundEcho")) { + soundEcho = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "soundLowPass")) { + soundLowPass = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "soundReverse")) { + soundReverse = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "soundVolume")) { + soundVolume = sdlFromHex(value); + if(soundVolume < 0 || soundVolume > 3) + soundVolume = 0; + } else if(!strcmp(key, "removeIntros")) { + removeIntros = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "saveType")) { + cpuSaveType = sdlFromHex(value); + if(cpuSaveType < 0 || cpuSaveType > 5) + cpuSaveType = 0; + } else if(!strcmp(key, "flashSize")) { + sdlFlashSize = sdlFromHex(value); + if(sdlFlashSize != 0 && sdlFlashSize != 1) + sdlFlashSize = 0; + } else if(!strcmp(key, "ifbType")) { + ifbType = sdlFromHex(value); + if(ifbType < 0 || ifbType > 2) + ifbType = 0; + } else if(!strcmp(key, "showSpeed")) { + showSpeed = sdlFromHex(value); + if(showSpeed < 0 || showSpeed > 2) + showSpeed = 1; + } else if(!strcmp(key, "showSpeedTransparent")) { + showSpeedTransparent = sdlFromHex(value); + } else if(!strcmp(key, "autoFrameSkip")) { + autoFrameSkip = sdlFromHex(value); + } else if(!strcmp(key, "throttle")) { + throttle = sdlFromHex(value); + if(throttle != 0 && (throttle < 5 || throttle > 1000)) + throttle = 0; + } else if(!strcmp(key, "disableMMX")) { +#ifdef MMX + cpu_mmx = sdlFromHex(value) ? false : true; +#endif + } else if(!strcmp(key, "pauseWhenInactive")) { + pauseWhenInactive = sdlFromHex(value) ? true : false; + } else if(!strcmp(key, "agbPrint")) { + sdlAgbPrint = sdlFromHex(value); + } else if(!strcmp(key, "rtcEnabled")) { + sdlRtcEnable = sdlFromHex(value); + } else if(!strcmp(key, "rewindTimer")) { + rewindTimer = sdlFromHex(value); + if(rewindTimer < 0 || rewindTimer > 600) + rewindTimer = 0; + rewindTimer *= 6; // convert value to 10 frames multiple + } else if(!strcmp(key, "enhancedDetection")) { + cpuEnhancedDetection = sdlFromHex(value) ? true : false; + } else { + fprintf(stderr, "Unknown configuration key %s\n", key); + } + } +} + +void sdlReadPreferences() +{ + FILE *f = sdlFindFile("VisualBoyAdvance.cfg"); + + if(f == NULL) { + fprintf(stderr, "Configuration file NOT FOUND (using defaults)\n"); + return; + } else + fprintf(stderr, "Reading configuration file.\n"); + + sdlReadPreferences(f); + + fclose(f); +} + +static void sdlApplyPerImagePreferences() +{ + FILE *f = sdlFindFile("vba-over.ini"); + if(!f) { + fprintf(stderr, "vba-over.ini NOT FOUND (using emulator settings)\n"); + return; + } else + fprintf(stderr, "Reading vba-over.ini\n"); + + char buffer[7]; + buffer[0] = '['; + buffer[1] = rom[0xac]; + buffer[2] = rom[0xad]; + buffer[3] = rom[0xae]; + buffer[4] = rom[0xaf]; + buffer[5] = ']'; + buffer[6] = 0; + + char readBuffer[2048]; + + bool found = false; + + while(1) { + char *s = fgets(readBuffer, 2048, f); + + if(s == NULL) + break; + + char *p = strchr(s, ';'); + + if(p) + *p = 0; + + char *token = strtok(s, " \t\n\r="); + + if(!token) + continue; + if(strlen(token) == 0) + continue; + + if(!strcmp(token, buffer)) { + found = true; + break; + } + } + + if(found) { + while(1) { + char *s = fgets(readBuffer, 2048, f); + + if(s == NULL) + break; + + char *p = strchr(s, ';'); + if(p) + *p = 0; + + char *token = strtok(s, " \t\n\r="); + if(!token) + continue; + if(strlen(token) == 0) + continue; + + if(token[0] == '[') // starting another image settings + break; + char *value = strtok(NULL, "\t\n\r="); + if(value == NULL) + continue; + + if(!strcmp(token, "rtcEnabled")) + rtcEnable(atoi(value) == 0 ? false : true); + else if(!strcmp(token, "flashSize")) { + int size = atoi(value); + if(size == 0x10000 || size == 0x20000) + flashSetSize(size); + } else if(!strcmp(token, "saveType")) { + int save = atoi(value); + if(save >= 0 && save <= 5) + cpuSaveType = save; + } + } + } + fclose(f); +} + +static int sdlCalculateShift(u32 mask) +{ + int m = 0; + + while(mask) { + m++; + mask >>= 1; + } + + return m-5; +} + +static int sdlCalculateMaskWidth(u32 mask) +{ + int m = 0; + int mask2 = mask; + + while(mask2) { + m++; + mask2 >>= 1; + } + + int m2 = 0; + mask2 = mask; + while(!(mask2 & 1)) { + m2++; + mask2 >>= 1; + } + + return m - m2; +} + +void sdlWriteState(int num) +{ + char stateName[2048]; + + if(saveDir[0]) + sprintf(stateName, "%s/%s%d.sgm", saveDir, sdlGetFilename(filename), + num+1); + else + sprintf(stateName,"%s%d.sgm", filename, num+1); + if(theEmulator.emuWriteState) + theEmulator.emuWriteState(stateName); + sprintf(stateName, "Wrote state %d", num+1); + systemScreenMessage(stateName); +} + +void sdlReadState(int num) +{ + char stateName[2048]; + + if(saveDir[0]) + sprintf(stateName, "%s/%s%d.sgm", saveDir, sdlGetFilename(filename), + num+1); + else + sprintf(stateName,"%s%d.sgm", filename, num+1); + + if(theEmulator.emuReadState) + theEmulator.emuReadState(stateName); + + sprintf(stateName, "Loaded state %d", num+1); + systemScreenMessage(stateName); +} + +void sdlWriteBattery() +{ + char buffer[1048]; + + if(batteryDir[0]) + sprintf(buffer, "%s/%s.sav", batteryDir, sdlGetFilename(filename)); + else + sprintf(buffer, "%s.sav", filename); + + theEmulator.emuWriteBattery(buffer); + + systemScreenMessage("Wrote battery"); +} + +void sdlReadBattery() +{ + char buffer[1048]; + + if(batteryDir[0]) + sprintf(buffer, "%s/%s.sav", batteryDir, sdlGetFilename(filename)); + else + sprintf(buffer, "%s.sav", filename); + + bool res = false; + + res = theEmulator.emuReadBattery(buffer); + + if(res) + systemScreenMessage("Loaded battery"); +} + +#define MOD_KEYS (KMOD_CTRL|KMOD_SHIFT|KMOD_ALT|KMOD_META) +#define MOD_NOCTRL (KMOD_SHIFT|KMOD_ALT|KMOD_META) +#define MOD_NOALT (KMOD_CTRL|KMOD_SHIFT|KMOD_META) +#define MOD_NOSHIFT (KMOD_CTRL|KMOD_ALT|KMOD_META) + +void sdlUpdateKey(int key, bool down) +{ + int i; + for(int j = 0; j < 4; j++) { + for(i = 0 ; i < 12; i++) { + if((joypad[j][i] & 0xf000) == 0) { + if(key == joypad[j][i]) + if (down) currentButtons[j] |= 1<> 12); + int b = joypad[j][i] & 0xfff; + if(dev) { + dev--; + + if((dev == which) && (b >= 128) && (b == (button+128))) { + if (pressed) currentButtons[j] |= 1<> 12); + int b = motion[i] & 0xfff; + if(dev) { + dev--; + + if((dev == which) && (b >= 128) && (b == (button+128))) { + sdlMotionButtons[i] = pressed; + } + } + } +} + +void sdlUpdateJoyHat(int which, + int hat, + int value) +{ + int i; + for(int j = 0; j < 4; j++) { + for(i = 0; i < 12; i++) { + int dev = (joypad[j][i] >> 12); + int a = joypad[j][i] & 0xfff; + if(dev) { + dev--; + + if((dev == which) && (a>=32) && (a < 48) && (((a&15)>>2) == hat)) { + int dir = a & 3; + int v = 0; + switch(dir) { + case 0: + v = value & SDL_HAT_UP; + break; + case 1: + v = value & SDL_HAT_DOWN; + break; + case 2: + v = value & SDL_HAT_RIGHT; + break; + case 3: + v = value & SDL_HAT_LEFT; + break; + } + if (v) currentButtons[j] |= 1<> 12); + int a = motion[i] & 0xfff; + if(dev) { + dev--; + + if((dev == which) && (a>=32) && (a < 48) && (((a&15)>>2) == hat)) { + int dir = a & 3; + int v = 0; + switch(dir) { + case 0: + v = value & SDL_HAT_UP; + break; + case 1: + v = value & SDL_HAT_DOWN; + break; + case 2: + v = value & SDL_HAT_RIGHT; + break; + case 3: + v = value & SDL_HAT_LEFT; + break; + } + sdlMotionButtons[i] = (v ? true : false); + } + } + } +} + +void sdlUpdateJoyAxis(int which, + int axis, + int value) +{ + int i; + for(int j = 0; j < 4; j++) { + for(i = 0; i < 12; i++) { + int dev = (joypad[j][i] >> 12); + int a = joypad[j][i] & 0xfff; + if(dev) { + dev--; + + if((dev == which) && (a < 32) && ((a>>1) == axis)) { + //I have no idea what this does, is this reimplementation correct? --Felipe + if (value>16384) { + if (a&1) currentButtons[j] |= 1<> 12); + int a = motion[i] & 0xfff; + if(dev) { + dev--; + + if((dev == which) && (a < 32) && ((a>>1) == axis)) { + sdlMotionButtons[i] = (a & 1) ? (value > 16384) : (value < -16384); + } + } + } +} + +bool sdlCheckJoyKey(int key) +{ + int dev = (key >> 12) - 1; + int what = key & 0xfff; + + if(what >= 128) { + // joystick button + int button = what - 128; + + if(button >= SDL_JoystickNumButtons(sdlDevices[dev])) + return false; + } else if (what < 0x20) { + // joystick axis + what >>= 1; + if(what >= SDL_JoystickNumAxes(sdlDevices[dev])) + return false; + } else if (what < 0x30) { + // joystick hat + what = (what & 15); + what >>= 2; + if(what >= SDL_JoystickNumHats(sdlDevices[dev])) + return false; + } + + // no problem found + return true; +} + +void sdlCheckKeys() +{ + sdlNumDevices = SDL_NumJoysticks(); + + if(sdlNumDevices) + sdlDevices = (SDL_Joystick **)calloc(1,sdlNumDevices * + sizeof(SDL_Joystick **)); + int i; + + bool usesJoy = false; + + for(int j = 0; j < 4; j++) { + for(i = 0; i < 12; i++) { + int dev = joypad[j][i] >> 12; + if(dev) { + dev--; + bool ok = false; + + if(sdlDevices) { + if(dev < sdlNumDevices) { + if(sdlDevices[dev] == NULL) { + sdlDevices[dev] = SDL_JoystickOpen(dev); + } + + ok = sdlCheckJoyKey(joypad[j][i]); + } else + ok = false; + } + + if(!ok) + joypad[j][i] = defaultJoypad[i]; + else + usesJoy = true; + } + } + } + + for(i = 0; i < 4; i++) { + int dev = motion[i] >> 12; + if(dev) { + dev--; + bool ok = false; + + if(sdlDevices) { + if(dev < sdlNumDevices) { + if(sdlDevices[dev] == NULL) { + sdlDevices[dev] = SDL_JoystickOpen(dev); + } + + ok = sdlCheckJoyKey(motion[i]); + } else + ok = false; + } + + if(!ok) + motion[i] = defaultMotion[i]; + else + usesJoy = true; + } + } + + if(usesJoy) + SDL_JoystickEventState(SDL_ENABLE); +} + +void sdlPollEvents() +{ + SDL_Event event; + while(SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_QUIT: + emulating = 0; + break; + case SDL_ACTIVEEVENT: + if(pauseWhenInactive && (event.active.state & SDL_APPINPUTFOCUS)) { + active = event.active.gain; + if(active) { + if(!paused) { + if(emulating) + soundResume(); + } + } else { + wasPaused = true; + if(pauseWhenInactive) { + if(emulating) + soundPause(); + } + + memset(delta,255,sizeof(delta)); + } + } + break; + case SDL_MOUSEMOTION: + case SDL_MOUSEBUTTONUP: + case SDL_MOUSEBUTTONDOWN: + if(fullscreen) { + SDL_ShowCursor(SDL_ENABLE); + mouseCounter = 120; + } + break; + case SDL_JOYHATMOTION: + sdlUpdateJoyHat(event.jhat.which, + event.jhat.hat, + event.jhat.value); + break; + case SDL_JOYBUTTONDOWN: + case SDL_JOYBUTTONUP: + sdlUpdateJoyButton(event.jbutton.which, + event.jbutton.button, + event.jbutton.state == SDL_PRESSED); + break; + case SDL_JOYAXISMOTION: + sdlUpdateJoyAxis(event.jaxis.which, + event.jaxis.axis, + event.jaxis.value); + break; + case SDL_KEYDOWN: + sdlUpdateKey(event.key.keysym.sym, true); + break; + case SDL_KEYUP: + switch(event.key.keysym.sym) { + case SDLK_r: + if(!(event.key.keysym.mod & MOD_NOCTRL) && + (event.key.keysym.mod & KMOD_CTRL)) { + if(emulating) { + theEmulator.emuReset(true); + + systemScreenMessage("Reset"); + } + } + break; + case SDLK_b: + if(!(event.key.keysym.mod & MOD_NOCTRL) && + (event.key.keysym.mod & KMOD_CTRL)) { + if(emulating && theEmulator.emuReadMemState && rewindMemory + && rewindCount) { + rewindPos = --rewindPos & 7; + theEmulator.emuReadMemState(&rewindMemory[REWIND_SIZE*rewindPos], + REWIND_SIZE); + rewindCount--; + rewindCounter = 0; + systemScreenMessage("Rewind"); + } + } + break; + case SDLK_p: + if(!(event.key.keysym.mod & MOD_NOCTRL) && + (event.key.keysym.mod & KMOD_CTRL)) { + paused = !paused; + SDL_PauseAudio(paused); + if(paused) + wasPaused = true; + } + break; + case SDLK_ESCAPE: + emulating = 0; + break; + case SDLK_f: + if(!(event.key.keysym.mod & MOD_NOCTRL) && + (event.key.keysym.mod & KMOD_CTRL)) { + int flags = 0; + fullscreen = !fullscreen; + if(fullscreen) + flags |= SDL_FULLSCREEN; + SDL_SetVideoMode(destWidth, destHeight, systemColorDepth, flags); + // if(SDL_WM_ToggleFullScreen(surface)) + // fullscreen = !fullscreen; + } + break; + case SDLK_F11: + if(dbgMain != debuggerMain) { + if(armState) { + armNextPC -= 4; + reg[15].I -= 4; + } else { + armNextPC -= 2; + reg[15].I -= 2; + } + } + debugger = true; + break; + case SDLK_F1: + case SDLK_F2: + case SDLK_F3: + case SDLK_F4: + case SDLK_F5: + case SDLK_F6: + case SDLK_F7: + case SDLK_F8: + case SDLK_F9: + case SDLK_F10: + if(!(event.key.keysym.mod & MOD_NOSHIFT) && + (event.key.keysym.mod & KMOD_SHIFT)) { + sdlWriteState(event.key.keysym.sym-SDLK_F1); + } else if(!(event.key.keysym.mod & MOD_KEYS)) { + sdlReadState(event.key.keysym.sym-SDLK_F1); + } + break; + case SDLK_1: + case SDLK_2: + case SDLK_3: + case SDLK_4: + if(!(event.key.keysym.mod & MOD_NOALT) && + (event.key.keysym.mod & KMOD_ALT)) { + char *disableMessages[4] = + { "autofire A disabled", + "autofire B disabled", + "autofire R disabled", + "autofire L disabled"}; + char *enableMessages[4] = + { "autofire A", + "autofire B", + "autofire R", + "autofire L"}; + int mask = 1 << (event.key.keysym.sym - SDLK_1); + if(event.key.keysym.sym > SDLK_2) + mask <<= 6; + if(autoFire & mask) { + autoFire &= ~mask; + systemScreenMessage(disableMessages[event.key.keysym.sym - SDLK_1]); + } else { + autoFire |= mask; + systemScreenMessage(enableMessages[event.key.keysym.sym - SDLK_1]); + } + } if(!(event.key.keysym.mod & MOD_NOCTRL) && + (event.key.keysym.mod & KMOD_CTRL)) { + int mask = 0x0100 << (event.key.keysym.sym - SDLK_1); + layerSettings ^= mask; + layerEnable = DISPCNT & layerSettings; + CPUUpdateRenderBuffers(false); + } + break; + case SDLK_5: + case SDLK_6: + case SDLK_7: + case SDLK_8: + if(!(event.key.keysym.mod & MOD_NOCTRL) && + (event.key.keysym.mod & KMOD_CTRL)) { + int mask = 0x0100 << (event.key.keysym.sym - SDLK_1); + layerSettings ^= mask; + layerEnable = DISPCNT & layerSettings; + } + break; + case SDLK_n: + if(!(event.key.keysym.mod & MOD_NOCTRL) && + (event.key.keysym.mod & KMOD_CTRL)) { + if(paused) + paused = false; + pauseNextFrame = true; + } + break; + default: + break; + } + sdlUpdateKey(event.key.keysym.sym, false); + break; + } + } +} + +void usage(char *cmd) +{ + printf("%s [option ...] file\n", cmd); + printf("\ +\n\ +Options:\n\ + -1, --video-1x 1x\n\ + -2, --video-2x 2x\n\ + -3, --video-3x 3x\n\ + -4, --video-4x 4x\n\ + -F, --fullscreen Full screen\n\ + -G, --gdb=PROTOCOL GNU Remote Stub mode:\n\ + tcp - use TCP at port 55555\n\ + tcp:PORT - use TCP at port PORT\n\ + pipe - use pipe transport\n\ + -N, --no-debug Don't parse debug information\n\ + -S, --flash-size=SIZE Set the Flash size\n\ + --flash-64k 0 - 64K Flash\n\ + --flash-128k 1 - 128K Flash\n\ + -T, --throttle=THROTTLE Set the desired throttle (5...1000)\n\ + -Y, --yuv=TYPE Use YUV overlay for drawing:\n\ + 0 - YV12\n\ + 1 - UYVY\n\ + 2 - YVYU\n\ + 3 - YUY2\n\ + 4 - IYUV\n\ + -b, --bios=BIOS Use given bios file\n\ + -c, --config=FILE Read the given configuration file\n\ + -d, --debug Enter debugger\n\ + -f, --filter=FILTER Select filter:\n\ + --filter-normal 0 - normal mode\n\ + --filter-tv-mode 1 - TV Mode\n\ + --filter-2xsai 2 - 2xSaI\n\ + --filter-super-2xsai 3 - Super 2xSaI\n\ + --filter-super-eagle 4 - Super Eagle\n\ + --filter-pixelate 5 - Pixelate\n\ + --filter-motion-blur 6 - Motion Blur\n\ + --filter-advmame 7 - AdvanceMAME Scale2x\n\ + --filter-simple2x 8 - Simple2x\n\ + --filter-bilinear 9 - Bilinear\n\ + --filter-bilinear+ 10 - Bilinear Plus\n\ + --filter-scanlines 11 - Scanlines\n\ + --filter-hq2x 12 - hq2x\n\ + --filter-lq2x 13 - lq2x\n\ + -h, --help Print this help\n\ + -i, --ips=PATCH Apply given IPS patch\n\ + -P, --profile=[HERTZ] Enable profiling\n\ + -s, --frameskip=FRAMESKIP Set frame skip (0...9)\n\ +"); + printf("\ + -t, --save-type=TYPE Set the available save type\n\ + --save-auto 0 - Automatic (EEPROM, SRAM, FLASH)\n\ + --save-eeprom 1 - EEPROM\n\ + --save-sram 2 - SRAM\n\ + --save-flash 3 - FLASH\n\ + --save-sensor 4 - EEPROM+Sensor\n\ + --save-none 5 - NONE\n\ + -v, --verbose=VERBOSE Set verbose logging (trace.log)\n\ + 1 - SWI\n\ + 2 - Unaligned memory access\n\ + 4 - Illegal memory write\n\ + 8 - Illegal memory read\n\ + 16 - DMA 0\n\ + 32 - DMA 1\n\ + 64 - DMA 2\n\ + 128 - DMA 3\n\ + 256 - Undefined instruction\n\ + 512 - AGBPrint messages\n\ +\n\ +Long options only:\n\ + --agb-print Enable AGBPrint support\n\ + --auto-frameskip Enable auto frameskipping\n\ + --ifb-none No interframe blending\n\ + --ifb-motion-blur Interframe motion blur\n\ + --ifb-smart Smart interframe blending\n\ + --no-agb-print Disable AGBPrint support\n\ + --no-auto-frameskip Disable auto frameskipping\n\ + --no-ips Do not apply IPS patch\n\ + --no-mmx Disable MMX support\n\ + --no-pause-when-inactive Don't pause when inactive\n\ + --no-rtc Disable RTC support\n\ + --no-show-speed Don't show emulation speed\n\ + --no-throttle Disable thrrotle\n\ + --pause-when-inactive Pause when inactive\n\ + --rtc Enable RTC support\n\ + --show-speed-normal Show emulation speed\n\ + --show-speed-detailed Show detailed speed data\n\ +"); + printf("\ + -r, --recordmovie=filename Start recording input movie\n\ + -p, --playmovie=filename Play input movie non-read-only\n\ + -w, --watchmovie=filename Play input movie in read-only mode\n\ +"); +} + +static char *szFile; + +void file_run() +{ + utilGetBaseName(szFile, filename); + char *p = strrchr(filename, '.'); + + if(p) + *p = 0; + + if(ipsname[0] == 0) + sprintf(ipsname, "%s.ips", filename); + + bool failed = false; + + IMAGE_TYPE type = utilFindType(szFile); + + if(type == IMAGE_UNKNOWN) { + systemMessage(0, "Unknown file type %s", szFile); + exit(-1); + } + systemCartridgeType = (int)type; + + if(type == IMAGE_GB) { + failed = !gbLoadRom(szFile); + if(!failed) { + systemCartridgeType = 1; + theEmulator = GBSystem; + if(sdlAutoIPS) { + int size = gbRomSize; + utilApplyIPS(ipsname, &gbRom, &size); + if(size != gbRomSize) { + extern bool gbUpdateSizes(); + gbUpdateSizes(); + gbReset(); + } + } + } + } else if(type == IMAGE_GBA) { + int size = CPULoadRom(szFile); + failed = (size == 0); + if(!failed) { + // if(cpuEnhancedDetection && cpuSaveType == 0) { + // utilGBAFindSave(rom, size); + // } + + sdlApplyPerImagePreferences(); + + systemCartridgeType = 0; + theEmulator = GBASystem; + + /* disabled due to problems + if(removeIntros && rom != NULL) { + WRITE32LE(&rom[0], 0xea00002e); + } + */ + + //CPUInit(biosFileName, useBios); + CPUInit(); + CPUReset(); + if(sdlAutoIPS) { + int size = 0x2000000; + utilApplyIPS(ipsname, &rom, &size); + if(size != 0x2000000) { + CPUReset(); + } + } + } + } + + if(failed) { + systemMessage(0, "Failed to load file %s", szFile); + exit(-1); + } + + emulating = 1; + renderedFrames = 0; + } + +int main(int argc, char **argv) +{ + fprintf(stderr, "VisualBoyAdvance version %s [SDL]\n", VERSION); + + arg0 = argv[0]; + + captureDir[0] = 0; + saveDir[0] = 0; + batteryDir[0] = 0; + ipsname[0] = 0; + + int op = -1; + + frameSkip = 2; + gbBorderOn = 0; + + parseDebug = true; + + sdlReadPreferences(); + + sdlPrintUsage = 0; + + while((op = getopt_long(argc, + argv, + "FNT:Y:G:D:b:c:df:hi:p::s:t:v:1234", + sdlOptions, + NULL)) != -1) { + switch(op) { + case 0: + // long option already processed by getopt_long + break; + case 'b': + useBios = true; + if(optarg == NULL) { + fprintf(stderr, "Missing BIOS file name\n"); + exit(-1); + } + strcpy(biosFileName, optarg); + break; + case 'c': + { + if(optarg == NULL) { + fprintf(stderr, "Missing config file name\n"); + exit(-1); + } + FILE *f = fopen(optarg, "r"); + if(f == NULL) { + fprintf(stderr, "File not found %s\n", optarg); + exit(-1); + } + sdlReadPreferences(f); + fclose(f); + } + break; + case 'd': + debugger = true; + break; + case 'h': + sdlPrintUsage = 1; + break; + case 'i': + if(optarg == NULL) { + fprintf(stderr, "Missing IPS name\n"); + exit(-1); + strcpy(ipsname, optarg); + } + break; + case 'Y': + yuv = true; + if(optarg) { + yuvType = atoi(optarg); + switch(yuvType) { + case 0: + yuvType = SDL_YV12_OVERLAY; + break; + case 1: + yuvType = SDL_UYVY_OVERLAY; + break; + case 2: + yuvType = SDL_YVYU_OVERLAY; + break; + case 3: + yuvType = SDL_YUY2_OVERLAY; + break; + case 4: + yuvType = SDL_IYUV_OVERLAY; + break; + default: + yuvType = SDL_YV12_OVERLAY; + } + } else + yuvType = SDL_YV12_OVERLAY; + break; + case 'G': + dbgMain = remoteStubMain; + dbgSignal = remoteStubSignal; + dbgOutput = remoteOutput; + debugger = true; + debuggerStub = true; + if(optarg) { + char *s = optarg; + if(strncmp(s,"tcp:", 4) == 0) { + s+=4; + int port = atoi(s); + remoteSetProtocol(0); + remoteSetPort(port); + } else if(strcmp(s,"tcp") == 0) { + remoteSetProtocol(0); + } else if(strcmp(s, "pipe") == 0) { + remoteSetProtocol(1); + } else { + fprintf(stderr, "Unknown protocol %s\n", s); + exit(-1); + } + } else { + remoteSetProtocol(0); + } + break; + case 'N': + parseDebug = false; + break; + case 'D': + if(optarg) { + systemDebug = atoi(optarg); + } else { + systemDebug = 1; + } + break; + case 'F': + fullscreen = 1; + mouseCounter = 120; + break; + case 'f': + if(optarg) { + filter = atoi(optarg); + } else { + filter = 0; + } + break; + + case 'r': + if(optarg == NULL) { + fprintf(stderr, "ERROR: --recordMovie ('r') needs movie filename as option\n"); + exit(-1); + } + strcpy(movieFileName, optarg); + useMovie = 1; + break; + case 'p': // play without read-only (editable) + fprintf (stderr, "-p got called!\n"); + if(optarg == NULL) { + fprintf(stderr, "ERROR: --playMovie ('p') needs movie filename as option\n"); + exit(-1); + } + strcpy(movieFileName, optarg); + useMovie = 2; + break; + case 'w': // play with read-only + fprintf (stderr, "-w got called!\n"); + if(optarg == NULL) { + fprintf(stderr, "ERROR: --watchMovie ('w') needs movie filename as option\n"); + exit(-1); + } + strcpy(movieFileName, optarg); + useMovie = 3; + break; + + case 'P': +#ifdef PROFILING + if(optarg) { + cpuEnableProfiling(atoi(optarg)); + } else + cpuEnableProfiling(100); +#endif + break; + case 'S': + sdlFlashSize = atoi(optarg); + if(sdlFlashSize < 0 || sdlFlashSize > 1) + sdlFlashSize = 0; + break; + case 's': + if(optarg) { + int a = atoi(optarg); + if(a >= 0 && a <= 9) { + gbFrameSkip = a; + frameSkip = a; + } + } else { + frameSkip = 2; + gbFrameSkip = 0; + } + break; + case 't': + if(optarg) { + int a = atoi(optarg); + if(a < 0 || a > 5) + a = 0; + cpuSaveType = a; + } + break; + case 'T': + if(optarg) { + int t = atoi(optarg); + throttle = t; + } + break; + case 'v': + if(optarg) { + systemVerbose = atoi(optarg); + } else + systemVerbose = 0; + break; + case '1': + sizeOption = 0; + break; + case '2': + sizeOption = 1; + break; + case '3': + sizeOption = 2; + break; + case '4': + sizeOption = 3; + break; + case '?': + sdlPrintUsage = 1; + break; + } + } + + if(sdlPrintUsage) { + usage(argv[0]); + exit(-1); + } + +#ifdef MMX + if(disableMMX) + cpu_mmx = 0; +#endif + + if(rewindTimer) + rewindMemory = (char *)malloc(8*REWIND_SIZE); + + if(sdlFlashSize == 0) + flashSetSize(0x10000); + else + flashSetSize(0x20000); + + rtcEnable(sdlRtcEnable ? true : false); + agbPrintEnable(sdlAgbPrint ? true : false); + + if(!debuggerStub) { + if(optind >= argc) { + systemMessage(0,"Missing image name"); + usage(argv[0]); + exit(-1); + } + } + + if(filter) { + sizeOption = 1; + } + + for(int i = 0; i < 24;) { + systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10); + systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + systemGbPalette[i++] = 0; + } + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + if(optind < argc) + { + szFile = argv[optind]; + file_run(); + } + else + { + systemCartridgeType = 0; + strcpy(filename, "gnu_stub"); + rom = (u8 *)malloc(0x2000000); + workRAM = (u8 *)calloc(1, 0x40000); + bios = (u8 *)calloc(1,0x4000); + internalRAM = (u8 *)calloc(1,0x8000); + paletteRAM = (u8 *)calloc(1,0x400); + vram = (u8 *)calloc(1, 0x20000); + oam = (u8 *)calloc(1, 0x400); + pix = (u8 *)calloc(1, 4 * 240 * 160); + ioMem = (u8 *)calloc(1, 0x400); + + theEmulator = GBASystem; + + //CPUInit(biosFileName, useBios); + CPUInit(); + CPUReset(); + } + + if(debuggerStub) + remoteInit(); + + int flags = SDL_INIT_VIDEO|SDL_INIT_AUDIO| + SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE; + + if(soundOffFlag) + flags ^= SDL_INIT_AUDIO; + + if(SDL_Init(flags)) { + systemMessage(0, "Failed to init SDL: %s", SDL_GetError()); + exit(-1); + } + + if(SDL_InitSubSystem(SDL_INIT_JOYSTICK)) { + systemMessage(0, "Failed to init joystick support: %s", SDL_GetError()); + } + + sdlCheckKeys(); + + if(systemCartridgeType == 0) { + srcWidth = 240; + srcHeight = 160; + systemFrameSkip = frameSkip; + } else if (systemCartridgeType == 1) { + if(gbBorderOn) { + srcWidth = 256; + srcHeight = 224; + gbBorderLineSkip = 256; + gbBorderColumnSkip = 48; + gbBorderRowSkip = 40; + } else { + srcWidth = 160; + srcHeight = 144; + gbBorderLineSkip = 160; + gbBorderColumnSkip = 0; + gbBorderRowSkip = 0; + } + systemFrameSkip = gbFrameSkip; + } else { + srcWidth = 320; + srcHeight = 240; + } + + destWidth = (sizeOption+1)*srcWidth; + destHeight = (sizeOption+1)*srcHeight; + + surface = SDL_SetVideoMode(destWidth, destHeight, 16, + SDL_ANYFORMAT|SDL_HWSURFACE|SDL_DOUBLEBUF| + (fullscreen ? SDL_FULLSCREEN : 0)); + + if(surface == NULL) { + systemMessage(0, "Failed to set video mode"); + SDL_Quit(); + exit(-1); + } + + systemRedShift = sdlCalculateShift(surface->format->Rmask); + systemGreenShift = sdlCalculateShift(surface->format->Gmask); + systemBlueShift = sdlCalculateShift(surface->format->Bmask); + + systemColorDepth = surface->format->BitsPerPixel; + if(systemColorDepth == 15) + systemColorDepth = 16; + + if(yuv) { + Init_Overlay(surface, yuvType); + systemColorDepth = 32; + systemRedShift = 3; + systemGreenShift = 11; + systemBlueShift = 19; + } + + if(systemColorDepth != 16 && systemColorDepth != 24 && + systemColorDepth != 32) { + fprintf(stderr,"Unsupported color depth '%d'.\nOnly 16, 24 and 32 bit color depths are supported\n", systemColorDepth); + exit(-1); + } + +#ifndef C_CORE + sdlMakeStretcher(srcWidth); +#else + switch(systemColorDepth) { + case 16: + sdlStretcher = sdlStretcher16[sizeOption]; + break; + case 24: + sdlStretcher = sdlStretcher24[sizeOption]; + break; + case 32: + sdlStretcher = sdlStretcher32[sizeOption]; + break; + default: + fprintf(stderr, "Unsupported resolution: %d\n", systemColorDepth); + exit(-1); + } +#endif + + fprintf(stderr,"Color depth: %d\n", systemColorDepth); + + if(systemColorDepth == 16) { + if(sdlCalculateMaskWidth(surface->format->Gmask) == 6) { + Init_2xSaI(565); + RGB_LOW_BITS_MASK = 0x821; + } else { + Init_2xSaI(555); + RGB_LOW_BITS_MASK = 0x421; + } + if(systemCartridgeType == 2) { + for(int i = 0; i < 0x10000; i++) { + systemColorMap16[i] = (((i >> 1) & 0x1f) << systemBlueShift) | + (((i & 0x7c0) >> 6) << systemGreenShift) | + (((i & 0xf800) >> 11) << systemRedShift); + } + } else { + for(int i = 0; i < 0x10000; i++) { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + } + srcPitch = srcWidth * 2+4; + } else { + if(systemColorDepth != 32) + filterFunction = NULL; + RGB_LOW_BITS_MASK = 0x010101; + if(systemColorDepth == 32) { + Init_2xSaI(32); + } + for(int i = 0; i < 0x10000; i++) { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + if(systemColorDepth == 32) + srcPitch = srcWidth*4 + 4; + else + srcPitch = srcWidth*3; + } + + if(systemColorDepth != 32) { + switch(filter) { + case 0: + filterFunction = NULL; + break; + case 1: + filterFunction = ScanlinesTV; + break; + case 2: + filterFunction = _2xSaI; + break; + case 3: + filterFunction = Super2xSaI; + break; + case 4: + filterFunction = SuperEagle; + break; + case 5: + filterFunction = Pixelate2x16; + break; + case 6: + filterFunction = MotionBlur; + break; + case 7: + filterFunction = AdMame2x; + break; + case 8: + filterFunction = Simple2x16; + break; + case 9: + filterFunction = Bilinear; + break; + case 10: + filterFunction = BilinearPlus; + break; + case 11: + filterFunction = Scanlines; + break; + case 12: + filterFunction = hq2x; + break; + case 13: + filterFunction = lq2x; + break; + default: + filterFunction = NULL; + break; + } + } else { + switch(filter) { + case 0: + filterFunction = NULL; + break; + case 1: + filterFunction = ScanlinesTV32; + break; + case 2: + filterFunction = _2xSaI32; + break; + case 3: + filterFunction = Super2xSaI32; + break; + case 4: + filterFunction = SuperEagle32; + break; + case 5: + filterFunction = Pixelate2x32; + break; + case 6: + filterFunction = MotionBlur32; + break; + case 7: + filterFunction = AdMame2x32; + break; + case 8: + filterFunction = Simple2x32; + break; + case 9: + filterFunction = Bilinear32; + break; + case 10: + filterFunction = BilinearPlus32; + break; + case 11: + filterFunction = Scanlines32; + break; + case 12: + filterFunction = hq2x32; + break; + case 13: + filterFunction = lq2x32; + break; + default: + filterFunction = NULL; + break; + } + } + + if(systemColorDepth == 16) { + switch(ifbType) { + case 0: + default: + ifbFunction = NULL; + break; + case 1: + ifbFunction = MotionBlurIB; + break; + case 2: + ifbFunction = SmartIB; + break; + } + } else if(systemColorDepth == 32) { + switch(ifbType) { + case 0: + default: + ifbFunction = NULL; + break; + case 1: + ifbFunction = MotionBlurIB32; + break; + case 2: + ifbFunction = SmartIB32; + break; + } + } else + ifbFunction = NULL; + + if(delta == NULL) { + delta = (u8*)malloc(322*242*4); + memset(delta, 255, 322*242*4); + } + + if(!soundOffFlag) + soundInit(); + + autoFrameSkipLastTime = throttleLastTime = systemGetClock(); + + switch(useMovie) + { + case 1: // --recordMovie + VBAMovieCreate(movieFileName, + /*authorInfo*/"", + /*startFlags*/0, + /*controllerFlags*/MOVIE_CONTROLLER(0), + /*typeFlags*/(systemCartridgeType==IMAGE_GBA)?(MOVIE_TYPE_GBA):(GBC_CAPABLE?MOVIE_TYPE_GBC:MOVIE_TYPE_SGB)); + break; + case 2: // --playMovie + VBAMovieOpen(movieFileName, false); + break; + case 3: // --watchMovie + VBAMovieOpen(movieFileName, true); + break; + default: + sdlReadBattery(); + break; + } + SDL_WM_SetCaption("VisualBoyAdvance", NULL); + + char *moviefile = getenv("AUTODEMO"); +// fprintf (stderr, "Checking for AUTODEMO...\n"); + if (moviefile) + { +// fprintf (stderr, "I got a filename OMG!\nCalling VBAMovieOpen...\n"); + VBAMovieOpen(moviefile, true); + } + + while(emulating) { + if(!paused && active) { + if(debugger && theEmulator.emuHasDebugger) + dbgMain(); + else { + theEmulator.emuMain(theEmulator.emuCount); + if(rewindSaveNeeded && rewindMemory && theEmulator.emuWriteMemState) { + rewindCount++; + if(rewindCount > 8) + rewindCount = 8; + if(theEmulator.emuWriteMemState && + theEmulator.emuWriteMemState(&rewindMemory[rewindPos*REWIND_SIZE], + REWIND_SIZE)) { + rewindPos = ++rewindPos & 7; + if(rewindCount == 8) + rewindTopPos = ++rewindTopPos & 7; + } + } + + rewindSaveNeeded = false; + } + } else { + SDL_Delay(500); + } + sdlPollEvents(); + if(mouseCounter) { + mouseCounter--; + if(mouseCounter == 0) + SDL_ShowCursor(SDL_DISABLE); + } + } + + emulating = 0; + fprintf(stderr,"Shutting down\n"); + remoteCleanUp(); + soundShutdown(); + + if(gbRom != NULL || rom != NULL) { + sdlWriteBattery(); + theEmulator.emuCleanUp(); + } + + if(delta) { + free(delta); + delta = NULL; + } + + SDL_Quit(); + return 0; +} + +void systemMessage(int num, const char *msg, ...) +{ + char buffer[2048]; + va_list valist; + + va_start(valist, msg); + vsprintf(buffer, msg, valist); + + fprintf(stderr, "%s\n", buffer); + va_end(valist); +} + +//On WIN32, this function messages requesting +//the window to be redrawn. Can this be ignored here? +void systemRefreshScreen(){} + +void systemRenderFrame() +{ + renderedFrames++; + VBAUpdateFrameCountDisplay(); + VBAUpdateButtonPressDisplay(); + + if(yuv) { + Draw_Overlay(surface, sizeOption+1); + return; + } + + SDL_LockSurface(surface); + + for(int slot = 0 ; slot < 8 ; slot++) + { + if(screenMessage[slot]) { + if(systemCartridgeType == 1 && gbBorderOn) { + gbSgbRenderBorder(); + } + if(((systemGetClock() - screenMessageTime[slot]) < screenMessageDuration[slot]) && + !disableStatusMessages) { + drawText(pix, srcPitch, 10, srcHeight - 20*(slot+1), + screenMessageBuffer[slot]); + } else { + screenMessage[slot] = false; + } + } + } + + if(ifbFunction) { + if(systemColorDepth == 16) + ifbFunction(pix+destWidth+4, destWidth+4, srcWidth, srcHeight); + else + ifbFunction(pix+destWidth*2+4, destWidth*2+4, srcWidth, srcHeight); + } + + if(filterFunction) { + if(systemColorDepth == 16) + filterFunction(pix+destWidth+4,destWidth+4, delta, + (u8*)surface->pixels,surface->pitch, + srcWidth, + srcHeight); + else + filterFunction(pix+destWidth*2+4, + destWidth*2+4, + delta, + (u8*)surface->pixels, + surface->pitch, + srcWidth, + srcHeight); + } else { + int destPitch = surface->pitch; + u8 *src = pix; + u8 *dest = (u8*)surface->pixels; + int i; + u32 *stretcher = (u32 *)sdlStretcher; + if(systemColorDepth == 16) + src += srcPitch; + int option = sizeOption; + if(yuv) + option = 0; + switch(sizeOption) { + case 0: + for(i = 0; i < srcHeight; i++) { + SDL_CALL_STRETCHER; + src += srcPitch; + dest += destPitch; + } + break; + case 1: + for(i = 0; i < srcHeight; i++) { + SDL_CALL_STRETCHER; + dest += destPitch; + SDL_CALL_STRETCHER; + src += srcPitch; + dest += destPitch; + } + break; + case 2: + for(i = 0; i < srcHeight; i++) { + SDL_CALL_STRETCHER; + dest += destPitch; + SDL_CALL_STRETCHER; + dest += destPitch; + SDL_CALL_STRETCHER; + src += srcPitch; + dest += destPitch; + } + break; + case 3: + for(i = 0; i < srcHeight; i++) { + SDL_CALL_STRETCHER; + dest += destPitch; + SDL_CALL_STRETCHER; + dest += destPitch; + SDL_CALL_STRETCHER; + dest += destPitch; + SDL_CALL_STRETCHER; + src += srcPitch; + dest += destPitch; + } + break; + } + } + + if(showSpeed && fullscreen) { + char buffer[50]; + if(showSpeed == 1) + sprintf(buffer, "%d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + showRenderedFrames); + if(showSpeedTransparent) + drawTextTransp((u8*)surface->pixels, + surface->pitch, + 10, + surface->h-20, + buffer); + else + drawText((u8*)surface->pixels, + surface->pitch, + 10, + surface->h-20, + buffer); + } + + SDL_UnlockSurface(surface); + // SDL_UpdateRect(surface, 0, 0, destWidth, destHeight); + SDL_Flip(surface); +} + +bool systemReadJoypads() +{ + return true; +} + +// Kludge to make Lua call the right function. +u32 systemGetOriginalJoypad(int which, bool sensor){ + return systemGetJoypad(which,sensor); +} + +u32 systemGetJoypad(int which, bool sensor) +{ + sensorOn = sensor; + if(which < 0 || which > 3) + which = sdlDefaultJoypad; + + //VBAMovieUpdate(which); + //VBAMovieUpdateState(); + u32 res = 0; + + //----------------------------// + if (VBAMoviePlaying()){ + // VBAMovieRead() overwrites currentButtons[i] + VBAMovieRead(which, sensor); + res = currentButtons[which]; + return res; + } + //---------------------------// + //Temporary implementation, not sure if it's correct --Felipe + + /* + if(sdlButtons[which][KEY_BUTTON_A]) + res |= BUTTON_MASK_A; + if(sdlButtons[which][KEY_BUTTON_B]) + res |= BUTTON_MASK_B; + if(sdlButtons[which][KEY_BUTTON_SELECT]) + res |= BUTTON_MASK_SELECT; + if(sdlButtons[which][KEY_BUTTON_START]) + res |= BUTTON_MASK_START; + if(sdlButtons[which][KEY_RIGHT]) + res |= BUTTON_MASK_RIGHT; + if(sdlButtons[which][KEY_LEFT]) + res |= BUTTON_MASK_LEFT; + if(sdlButtons[which][KEY_UP]) + res |= BUTTON_MASK_UP; + if(sdlButtons[which][KEY_DOWN]) + res |= BUTTON_MASK_DOWN; + if(sdlButtons[which][KEY_BUTTON_R]) + res |= BUTTON_MASK_R; + if(sdlButtons[which][KEY_BUTTON_L]) + res |= BUTTON_MASK_L; + */ +/* + // disallow L+R or U+D of being pressed at the same time + if((res & 48) == 48) + res &= ~16; + if((res & 192) == 192) + res &= ~128; +*/ +/* + if(sdlbuttons[which][KEY_BUTTON_SPEED]) + res |= 1024; + if(sdlButtons[which][KEY_BUTTON_CAPTURE]) + res |= 2048; +*/ + res = currentButtons[which]; + + if(autoFire) { + res &= (~autoFire); + if(autoFireToggle) + res |= autoFire; + autoFireToggle = !autoFireToggle; + } + + //if (res) fprintf(stdout,"%x\n",res); + + return res; +} + +void systemSetJoypad(int which, u32 buttons) +{ + if(which < 0 || which > 3) + which = sdlDefaultJoypad; +/* + sdlButtons[which][KEY_BUTTON_A] = (buttons & 1) != 0; + sdlButtons[which][KEY_BUTTON_B] = (buttons & 2) != 0; + sdlButtons[which][KEY_BUTTON_SELECT] = (buttons & 4) != 0; + sdlButtons[which][KEY_BUTTON_START] = (buttons & 8) != 0; + sdlButtons[which][KEY_RIGHT] = (buttons & 16) != 0; + sdlButtons[which][KEY_LEFT] = (buttons & 32) != 0; + sdlButtons[which][KEY_UP] = (buttons & 64) != 0; + sdlButtons[which][KEY_DOWN] = (buttons & 128) != 0; + sdlButtons[which][KEY_BUTTON_R] = (buttons & 256) != 0; + sdlButtons[which][KEY_BUTTON_L] = (buttons & 512) != 0; +*/ + currentButtons[which]= buttons & 0x3ff; +} + +void systemClearJoypads() +{ + for (int i = 0; i < 4; ++i) + currentButtons[i] = 0; + + //lastKeys = 0; +} + +void systemSetTitle(const char *title) +{ + SDL_WM_SetCaption(title, NULL); +} + +void systemShowSpeed(int speed) +{ + systemSpeed = speed; + + showRenderedFrames = renderedFrames; + renderedFrames = 0; + + if(!fullscreen && showSpeed) { + char buffer[80]; + if(showSpeed == 1) + sprintf(buffer, "VisualBoyAdvance-%3d%%", systemSpeed); + else + sprintf(buffer, "VisualBoyAdvance-%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + showRenderedFrames); + + systemSetTitle(buffer); + } +} + +// FIXME: the timing +void systemFrame(/*int rate*/) //Looking at System.cpp, it looks like rate should be 600 +{ + u32 time = systemGetClock(); + if(!wasPaused && autoFrameSkip && !throttle) { + u32 diff = time - autoFrameSkipLastTime; + int speed = 100; + + if(diff) + speed = (1000000/600)/diff; + + if(speed >= 98) { + frameskipadjust++; + + if(frameskipadjust >= 3) { + frameskipadjust=0; + if(systemFrameSkip > 0) + systemFrameSkip--; + } + } else { + if(speed < 80) + frameskipadjust -= (90 - speed)/5; + else if(systemFrameSkip < 9) + frameskipadjust--; + + if(frameskipadjust <= -2) { + frameskipadjust += 2; + if(systemFrameSkip < 9) + systemFrameSkip++; + } + } + } + if(!wasPaused && throttle) { + /*if(!speedup) { + u32 diff = time - throttleLastTime; + + int target = (1000000.0/(600*throttle)); + int d = (target - diff); + + if(d > 0) { + SDL_Delay(d); + } + } + throttleLastTime = systemGetClock(); + */ + } + if(rewindMemory) { + if(++rewindCounter >= rewindTimer) { + rewindSaveNeeded = true; + rewindCounter = 0; + } + } + + if(systemSaveUpdateCounter) { + if(--systemSaveUpdateCounter <= SYSTEM_SAVE_NOT_UPDATED) { + sdlWriteBattery(); + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + } + } + + wasPaused = false; + autoFrameSkipLastTime = time; +} + +int systemFramesToSkip(){ + return systemFrameSkip; +} + +int systemScreenCapture(int a) +{ + char buffer[2048]; + + if(captureFormat) { + if(captureDir[0]) + sprintf(buffer, "%s/%s%02d.bmp", captureDir, sdlGetFilename(filename), a); + else + sprintf(buffer, "%s%02d.bmp", filename, a); + + theEmulator.emuWriteBMP(buffer); + } else { + if(captureDir[0]) + sprintf(buffer, "%s/%s%02d.png", captureDir, sdlGetFilename(filename), a); + else + sprintf(buffer, "%s%02d.png", filename, a); + theEmulator.emuWritePNG(buffer); + } + + systemScreenMessage("Screen capture"); + return a; +} + +void soundCallback(void *,u8 *stream,int len){} + +void systemSoundWriteToBuffer(){ + soundDriver->write(soundFinalWave, soundBufferLen); +} + +void systemSoundClearBuffer() +{ + SDL_mutexP(mutex); + memset(sdlBuffer,0,soundBufferTotalLen); + sdlSoundLen=0; + printf("Hi\n"); + SDL_mutexV(mutex); +} + +bool systemSoundInit(){ + systemSoundShutdown(); + soundDriver = new SoundSDL(); + if ( !soundDriver ) + return false; + + if (!soundDriver->init()) //<-- sound sample rate + return false; + + if (!(soundDriver->setThrottle(throttle))){ + fprintf(stderr,"Failed to set desired throttle, defaulting to 100 %%.\n"); + if (!soundDriver->setThrottle(100)) return false; + } + soundPaused = true; + systemSoundOn = true; + return true; +} + +void systemSoundShutdown(){ + if (soundDriver) + { + delete soundDriver; + soundDriver = 0; + } +} + +void systemSoundPause() +{ + SDL_PauseAudio(1); +} + +void systemSoundResume() +{ + SDL_PauseAudio(0); +} + +void systemSoundReset() +{ +} + +u32 systemGetClock() +{ + return SDL_GetTicks(); +} + +void systemUpdateMotionSensor() +{ + if(sdlMotionButtons[KEY_LEFT]) { + sensorX += 3; + if(sensorX > 2197) + sensorX = 2197; + if(sensorX < 2047) + sensorX = 2057; + } else if(sdlMotionButtons[KEY_RIGHT]) { + sensorX -= 3; + if(sensorX < 1897) + sensorX = 1897; + if(sensorX > 2047) + sensorX = 2037; + } else if(sensorX > 2047) { + sensorX -= 2; + if(sensorX < 2047) + sensorX = 2047; + } else { + sensorX += 2; + if(sensorX > 2047) + sensorX = 2047; + } + + if(sdlMotionButtons[KEY_UP]) { + sensorY += 3; + if(sensorY > 2197) + sensorY = 2197; + if(sensorY < 2047) + sensorY = 2057; + } else if(sdlMotionButtons[KEY_DOWN]) { + sensorY -= 3; + if(sensorY < 1897) + sensorY = 1897; + if(sensorY > 2047) + sensorY = 2037; + } else if(sensorY > 2047) { + sensorY -= 2; + if(sensorY < 2047) + sensorY = 2047; + } else { + sensorY += 2; + if(sensorY > 2047) + sensorY = 2047; + } +} + +void systemResetSensor() +{ + sensorX = sensorY = INITIAL_SENSOR_VALUE; +} + +int systemGetSensorX() +{ + return sensorX; +} + +int systemGetSensorY() +{ + return sensorY; +} + +void systemGbPrint(u8 *data,int pages,int feed,int palette, int contrast) +{ +} + +void systemScreenMessage(const char *msg, int slot, int duration, const char *colorList) +{ + screenMessage[slot] = true; + screenMessageTime[slot] = systemGetClock(); + screenMessageDuration[slot] = duration; + if(strlen(msg) > 20) { + strncpy(screenMessageBuffer[slot], msg, 20); + screenMessageBuffer[slot][20] = 0; + } else + strcpy(screenMessageBuffer[slot], msg); +} + +bool systemSoundCanChangeQuality() +{ + return false; +} + +bool systemSoundSetQuality(int quality) +{ + if (systemCartridgeType == 0) + soundSetQuality(quality); + else + gbSoundSetQuality(quality); + + return true; +} + +bool systemPauseOnFrame() +{ + if(pauseNextFrame) { + paused = true; + pauseNextFrame = false; + return true; + } + return false; +} + +// Code donated by Niels Wagenaar (BoycottAdvance) + +// GBA screensize. +#define GBA_WIDTH 240 +#define GBA_HEIGHT 160 + +void Init_Overlay(SDL_Surface *gbascreen, int overlaytype) +{ + + overlay = SDL_CreateYUVOverlay( GBA_WIDTH, + GBA_HEIGHT, + overlaytype, gbascreen); + fprintf(stderr, "Created %dx%dx%d %s %s overlay\n", + overlay->w,overlay->h,overlay->planes, + overlay->hw_overlay?"hardware":"software", + overlay->format==SDL_YV12_OVERLAY?"YV12": + overlay->format==SDL_IYUV_OVERLAY?"IYUV": + overlay->format==SDL_YUY2_OVERLAY?"YUY2": + overlay->format==SDL_UYVY_OVERLAY?"UYVY": + overlay->format==SDL_YVYU_OVERLAY?"YVYU": + "Unknown"); +} + +void Quit_Overlay(void) +{ + + SDL_FreeYUVOverlay(overlay); +} + +/* NOTE: These RGB conversion functions are not intended for speed, + only as examples. +*/ +inline void RGBtoYUV(Uint8 *rgb, int *yuv) +{ + yuv[0] = (int)((0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16); + yuv[1] = (int)(128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2])); + yuv[2] = (int)(128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2])); +} + +inline void ConvertRGBtoYV12(SDL_Overlay *o) +{ + int x,y; + int yuv[3]; + Uint8 *p,*op[3]; + + SDL_LockYUVOverlay(o); + + /* Black initialization */ + /* + memset(o->pixels[0],0,o->pitches[0]*o->h); + memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2)); + memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2)); + */ + + /* Convert */ + for(y=0; y<160 && yh; y++) { + p=(Uint8 *)pix+srcPitch*y; + op[0]=o->pixels[0]+o->pitches[0]*y; + op[1]=o->pixels[1]+o->pitches[1]*(y/2); + op[2]=o->pixels[2]+o->pitches[2]*(y/2); + for(x=0; x<240 && xw; x++) { + RGBtoYUV(p,yuv); + *(op[0]++)=yuv[0]; + if(x%2==0 && y%2==0) { + *(op[1]++)=yuv[2]; + *(op[2]++)=yuv[1]; + } + p+=4;//s->format->BytesPerPixel; + } + } + + SDL_UnlockYUVOverlay(o); +} + +inline void ConvertRGBtoIYUV(SDL_Overlay *o) +{ + int x,y; + int yuv[3]; + Uint8 *p,*op[3]; + + SDL_LockYUVOverlay(o); + + /* Black initialization */ + /* + memset(o->pixels[0],0,o->pitches[0]*o->h); + memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2)); + memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2)); + */ + + /* Convert */ + for(y=0; y<160 && yh; y++) { + p=(Uint8 *)pix+srcPitch*y; + op[0]=o->pixels[0]+o->pitches[0]*y; + op[1]=o->pixels[1]+o->pitches[1]*(y/2); + op[2]=o->pixels[2]+o->pitches[2]*(y/2); + for(x=0; x<240 && xw; x++) { + RGBtoYUV(p,yuv); + *(op[0]++)=yuv[0]; + if(x%2==0 && y%2==0) { + *(op[1]++)=yuv[1]; + *(op[2]++)=yuv[2]; + } + p+=4; //s->format->BytesPerPixel; + } + } + + SDL_UnlockYUVOverlay(o); +} + +inline void ConvertRGBtoUYVY(SDL_Overlay *o) +{ + int x,y; + int yuv[3]; + Uint8 *p,*op; + + SDL_LockYUVOverlay(o); + + for(y=0; y<160 && yh; y++) { + p=(Uint8 *)pix+srcPitch*y; + op=o->pixels[0]+o->pitches[0]*y; + for(x=0; x<240 && xw; x++) { + RGBtoYUV(p,yuv); + if(x%2==0) { + *(op++)=yuv[1]; + *(op++)=yuv[0]; + *(op++)=yuv[2]; + } else + *(op++)=yuv[0]; + + p+=4; //s->format->BytesPerPixel; + } + } + + SDL_UnlockYUVOverlay(o); +} + +inline void ConvertRGBtoYVYU(SDL_Overlay *o) +{ + int x,y; + int yuv[3]; + Uint8 *p,*op; + + SDL_LockYUVOverlay(o); + + for(y=0; y<160 && yh; y++) { + p=(Uint8 *)pix+srcPitch*y; + op=o->pixels[0]+o->pitches[0]*y; + for(x=0; x<240 && xw; x++) { + RGBtoYUV(p,yuv); + if(x%2==0) { + *(op++)=yuv[0]; + *(op++)=yuv[2]; + op[1]=yuv[1]; + } else { + *op=yuv[0]; + op+=2; + } + + p+=4; //s->format->BytesPerPixel; + } + } + + SDL_UnlockYUVOverlay(o); +} + +inline void ConvertRGBtoYUY2(SDL_Overlay *o) +{ + int x,y; + int yuv[3]; + Uint8 *p,*op; + + SDL_LockYUVOverlay(o); + + for(y=0; y<160 && yh; y++) { + p=(Uint8 *)pix+srcPitch*y; + op=o->pixels[0]+o->pitches[0]*y; + for(x=0; x<240 && xw; x++) { + RGBtoYUV(p,yuv); + if(x%2==0) { + *(op++)=yuv[0]; + *(op++)=yuv[1]; + op[1]=yuv[2]; + } else { + *op=yuv[0]; + op+=2; + } + + p+=4; //s->format->BytesPerPixel; + } + } + + SDL_UnlockYUVOverlay(o); +} + +inline void Convert32bit(SDL_Surface *display) +{ + switch(overlay->format) { + case SDL_YV12_OVERLAY: + ConvertRGBtoYV12(overlay); + break; + case SDL_UYVY_OVERLAY: + ConvertRGBtoUYVY(overlay); + break; + case SDL_YVYU_OVERLAY: + ConvertRGBtoYVYU(overlay); + break; + case SDL_YUY2_OVERLAY: + ConvertRGBtoYUY2(overlay); + break; + case SDL_IYUV_OVERLAY: + ConvertRGBtoIYUV(overlay); + break; + default: + fprintf(stderr, "cannot convert RGB picture to obtained YUV format!\n"); + exit(1); + break; + } + +} + + +inline void Draw_Overlay(SDL_Surface *display, int size) +{ + SDL_LockYUVOverlay(overlay); + + Convert32bit(display); + + overlay_rect.x = 0; + overlay_rect.y = 0; + overlay_rect.w = GBA_WIDTH * size; + overlay_rect.h = GBA_HEIGHT * size; + + SDL_DisplayYUVOverlay(overlay, &overlay_rect); + SDL_UnlockYUVOverlay(overlay); +} + +bool systemIsEmulating() +{ + return emulating != 0; +} + +void systemGbBorderOn() +{ + srcWidth = 256; + srcHeight = 224; + gbBorderLineSkip = 256; + gbBorderColumnSkip = 48; + gbBorderRowSkip = 40; + + destWidth = (sizeOption+1)*srcWidth; + destHeight = (sizeOption+1)*srcHeight; + + surface = SDL_SetVideoMode(destWidth, destHeight, 16, + SDL_ANYFORMAT|SDL_HWSURFACE|SDL_DOUBLEBUF| + (fullscreen ? SDL_FULLSCREEN : 0)); +#ifndef C_CORE + sdlMakeStretcher(srcWidth); +#else + switch(systemColorDepth) { + case 16: + sdlStretcher = sdlStretcher16[sizeOption]; + break; + case 24: + sdlStretcher = sdlStretcher24[sizeOption]; + break; + case 32: + sdlStretcher = sdlStretcher32[sizeOption]; + break; + default: + fprintf(stderr, "Unsupported resolution: %d\n", systemColorDepth); + exit(-1); + } +#endif + + if(systemColorDepth == 16) { + if(sdlCalculateMaskWidth(surface->format->Gmask) == 6) { + Init_2xSaI(565); + RGB_LOW_BITS_MASK = 0x821; + } else { + Init_2xSaI(555); + RGB_LOW_BITS_MASK = 0x421; + } + if(systemCartridgeType == 2) { + for(int i = 0; i < 0x10000; i++) { + systemColorMap16[i] = (((i >> 1) & 0x1f) << systemBlueShift) | + (((i & 0x7c0) >> 6) << systemGreenShift) | + (((i & 0xf800) >> 11) << systemRedShift); + } + } else { + for(int i = 0; i < 0x10000; i++) { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + } + srcPitch = srcWidth * 2+4; + } else { + if(systemColorDepth != 32) + filterFunction = NULL; + RGB_LOW_BITS_MASK = 0x010101; + if(systemColorDepth == 32) { + Init_2xSaI(32); + } + for(int i = 0; i < 0x10000; i++) { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + if(systemColorDepth == 32) + srcPitch = srcWidth*4 + 4; + else + srcPitch = srcWidth*3; + } +} + +bool systemIsRunningGBA() +{ + return (rom != NULL); +} + +int systemGetDefaultJoypad() +{ + return sdlDefaultJoypad; +} + +bool systemIsPaused() +{ + return paused; +} + +void systemSetPause(bool pause) +{ + paused = pause; + if (pause) + systemSoundPause(); + else + systemSoundResume(); +} + +u16 checksumBIOS() +{ + bool hasBIOS = false; + u8 * tempBIOS; + if(useBios) + { + tempBIOS = (u8 *)malloc(0x4000); + int size = 0x4000; + if(utilLoad(biosFileName, + utilIsGBABios, + tempBIOS, + size)) { + if(size == 0x4000) + hasBIOS = true; + } + } + + u16 biosCheck = 0; + if(hasBIOS) { + for(int i = 0; i < 0x4000; i += 4) + biosCheck += *((u32 *)&tempBIOS[i]); + free(tempBIOS); + } + + return biosCheck; +} + +EmulatedSystemCounters systemCounters = { + 0, //framecount + 0, //lagcount + 0, //extracount + true, //lagged + true //laggedLast +}; + +void VBAOnEnteringFrameBoundary() +{ + CallRegisteredLuaFunctions(LUACALL_AFTEREMULATION); + + if (VBALuaRunning()) + { + VBALuaFrameBoundary(); + } + + VBAMovieUpdateState(); +} + +void VBAOnExitingFrameBoundary() +{ + ; +} + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/SoundDriver.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/SoundDriver.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,65 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 2008 VBA-M development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_SOUND_DRIVER_H__ +#define __VBA_SOUND_DRIVER_H__ + +#include "Types.h" + +/** + * Sound driver abstract interface for the core to use to output sound. + * Subclass this to implement a new sound driver. + */ +class SoundDriver +{ +public: + + /** + * Destructor. Free the resources allocated by the sound driver. + */ + virtual ~SoundDriver() { }; + + /** + * Initialize the sound driver. + * @param sampleRate In Hertz + */ + virtual bool init() = 0; + + /** + * Tell the driver that the sound stream has paused + */ + virtual void pause() = 0; + + /** + * Reset the sound driver + */ + virtual void reset() = 0; + + /** + * Tell the driver that the sound stream has resumed + */ + virtual void resume() = 0; + + /** + * Write length bytes of data from the finalWave buffer to the driver output buffer. + */ + virtual void write(u16 * finalWave, int length) = 0; + + virtual bool setThrottle(unsigned short throttle) = 0; +}; + +#endif // __VBA_SOUND_DRIVER_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/SoundSDL.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/SoundSDL.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,178 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 2008 VBA-M development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "SoundSDL.h" + +extern int emulating; +extern bool speedup; + +// Hold up to 100 ms of data in the ring buffer +const float SoundSDL::_delay = 0.1f; + +SoundSDL::SoundSDL(): + _rbuf(0), + _initialized(false) +{ + +} + +void SoundSDL::soundCallback(void *data, u8 *stream, int len) +{ + reinterpret_cast(data)->read(reinterpret_cast(stream), len); +} + +void SoundSDL::read(u16 * stream, int length) +{ + if (!_initialized || length <= 0 || !emulating) + return; + + SDL_mutexP(_mutex); + _rbuf.read(stream, std::min(static_cast(length) / 2, _rbuf.used())); + + SDL_CondSignal(_cond); + SDL_mutexV(_mutex); +} + +void SoundSDL::write(u16 * finalWave, int length) +{ + if (!_initialized) + return; + + if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING) + SDL_PauseAudio(0); + + SDL_mutexP(_mutex); + + unsigned int samples = length / 4; + + std::size_t avail; + while ((avail = _rbuf.avail() / 2) < samples) + { + _rbuf.write(finalWave, avail * 2); + + finalWave += avail * 2; + samples -= avail; + + // If emulating and not in speed up mode, synchronize to audio + // by waiting till there is enough room in the buffer + if (emulating && !speedup) + { + SDL_CondWait(_cond,_mutex); + } + else + { + // Drop the remaining of the audio data + SDL_mutexV(_mutex); + return; + } + } + + _rbuf.write(finalWave, samples * 2); + + SDL_mutexV(_mutex); +} + + +bool SoundSDL::init() +{ + SDL_AudioSpec audio; + audio.freq = SDL_SAMPLE_RATE; + audio.format = AUDIO_S16SYS; + audio.channels = 2; + audio.samples = 1024; + audio.callback = soundCallback; + audio.userdata = this; + + if(SDL_OpenAudio(&audio, NULL)) + { + fprintf(stderr,"Failed to open audio: %s\n", SDL_GetError()); + return false; + } + + _rbuf.reset(_delay * SDL_SAMPLE_RATE * 2); + + _cond = SDL_CreateCond(); + _mutex = SDL_CreateMutex(); + _initialized = true; + + return true; +} + +SoundSDL::~SoundSDL() +{ + if (!_initialized) + return; + + SDL_mutexP(_mutex); + int iSave = emulating; + emulating = 0; + SDL_CondSignal(_cond); + SDL_mutexV(_mutex); + + SDL_DestroyCond(_cond); + _cond = NULL; + + SDL_DestroyMutex(_mutex); + _mutex = NULL; + + SDL_CloseAudio(); + + emulating = iSave; +} + +void SoundSDL::pause() +{ + if (!_initialized) + return; + + SDL_PauseAudio(1); +} + +void SoundSDL::resume() +{ + if (!_initialized) + return; + + SDL_PauseAudio(0); +} + +void SoundSDL::reset() +{ +} + +bool SoundSDL::setThrottle(unsigned short throttle){ + switch(throttle){ + case 25: + case 50: + case 100: + case 200: + case 400: + break; + default: + return false; + } + SDL_CloseAudio(); + SDL_AudioSpec audio; + audio.freq = SDL_SAMPLE_RATE*throttle/100; + audio.format = AUDIO_S16SYS; + audio.channels = 2; + audio.samples = 1024; + audio.callback = soundCallback; + audio.userdata = this; + _rbuf.reset((_delay * SDL_SAMPLE_RATE * throttle * 2)/100); + return !SDL_OpenAudio(&audio,NULL); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/SoundSDL.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/SoundSDL.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,56 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 2008 VBA-M development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_SOUND_SDL_H__ +#define __VBA_SOUND_SDL_H__ + +#include "SoundDriver.h" +#include "RingBuffer.h" + +#include + +const int SDL_SAMPLE_RATE = 44100; + +class SoundSDL: public SoundDriver +{ +public: + SoundSDL(); + virtual ~SoundSDL(); + + virtual bool init(); + virtual void pause(); + virtual void reset(); + virtual void resume(); + virtual void write(u16 * finalWave, int length); + virtual bool setThrottle(unsigned short throttle); + +//private: + RingBuffer _rbuf; + + SDL_cond * _cond; + SDL_mutex * _mutex; + + bool _initialized; + + // Defines what delay in seconds we keep in the sound buffer + static const float _delay; + + static void soundCallback(void *data, u8 *stream, int length); + virtual void read(u16 * stream, int length); +}; + +#endif // __VBA_SOUND_SDL_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/TestEmu.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/TestEmu.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,501 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include +#include +#include +#include + +#include "AutoBuild.h" + +#include "SDL.h" +#include "debugger.h" +#include "common/System.h" +#include "common/unzip.h" +#include "common/Util.h" +#include "gba/GBA.h" +#include "gba/GBAGlobals.h" +#include "gba/GBASound.h" +#include "gb/GB.h" +#include "gb/gbGlobals.h" + +#ifndef WIN32 +# include +# define GETCWD getcwd +#else // WIN32 +# include +# define GETCWD _getcwd +#endif // WIN32 + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif +extern bool8 soundEcho; +extern bool8 soundLowPass; +extern bool8 soundReverse; + +extern void remoteInit(); +extern void remoteCleanUp(); +extern void remoteStubMain(); +extern void remoteStubSignal(int,int); +extern void remoteOutput(char *, u32); +extern void remoteSetProtocol(int); +extern void remoteSetPort(int); +extern void debuggerOutput(char *, u32); + +struct EmulatedSystem emulator; + +int systemRedShift = 0; +int systemBlueShift = 16; +int systemGreenShift = 8; +int systemColorDepth = 32; +int systemDebug = 0; +int systemVerbose = 0; +int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + +int sensorX = 2047; +int sensorY = 2047; +bool sensorOn = false; + +int cartridgeType = 3; +int captureFormat = 0; + +int emulating = 0; +int RGB_LOW_BITS_MASK=0x821; +int systemFrameSkip = 0; +u32 systemColorMap32[0x10000]; +u16 systemColorMap16[0x10000]; +u16 systemGbPalette[24]; +char filename[2048]; +char biosFileName[2048]; +char captureDir[2048]; +char saveDir[2048]; +char batteryDir[2048]; + +int throttle = 0; + +bool paused = false; +bool debugger = true; +bool debuggerStub = false; +bool systemSoundOn = false; +bool removeIntros = false; +int sdlFlashSize = 0; +int sdlAutoIPS = 1; +int sdlRtcEnable = 0; +int sdlAgbPrint = 0; + +int sdlDefaultJoypad = 0; + +u16 motion[4] = { + SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2 +}; + +extern void debuggerSignal(int,int); + +void (*dbgMain)() = debuggerMain; +void (*dbgSignal)(int,int) = debuggerSignal; +void (*dbgOutput)(char *, u32) = debuggerOutput; + +char *sdlGetFilename(char *name) +{ + static char filebuffer[2048]; + + int len = strlen(name); + + char *p = name + len - 1; + + while(true) { + if(*p == '/' || + *p == '\\') { + p++; + break; + } + len--; + p--; + if(len == 0) + break; + } + + if(len == 0) + strcpy(filebuffer, name); + else + strcpy(filebuffer, p); + return filebuffer; +} + +void usage(char *cmd) +{ + printf("%s file-name\n",cmd); +} + +int main(int argc, char **argv) +{ + fprintf(stderr,"VisualBoyAdvance-Test version %s\n", VERSION); + + captureDir[0] = 0; + saveDir[0] = 0; + batteryDir[0] = 0; + + char buffer[1024]; + + systemFrameSkip = frameSkip = 2; + gbBorderOn = 0; + + parseDebug = true; + + if(!debuggerStub) { + if(argc <= 1) { + systemMessage(0,"Missing image name"); + usage(argv[0]); + exit(-1); + } + } + + for(int i = 0; i < 24;) { + systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10); + systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + systemGbPalette[i++] = 0; + } + + if(argc == 2) { + char *szFile = argv[optind]; + bool failed = false; + if(utilIsZipFile(szFile)) { + unzFile unz = unzOpen(szFile); + + if(unz == NULL) { + systemMessage(0, "Cannot open file %s", szFile); + exit(-1); + } + int r = unzGoToFirstFile(unz); + + if(r != UNZ_OK) { + unzClose(unz); + systemMessage(0, "Bad ZIP file %s", szFile); + exit(-1); + } + + bool found = false; + + unz_file_info info; + + while(true) { + r = unzGetCurrentFileInfo(unz, + &info, + buffer, + sizeof(buffer), + NULL, + 0, + NULL, + 0); + + if(r != UNZ_OK) { + unzClose(unz); + systemMessage(0,"Bad ZIP file %s", szFile); + exit(-1); + } + + if(utilIsGBImage(buffer)) { + found = true; + cartridgeType = 1; + break; + } + if(utilIsGBAImage(buffer)) { + found = true; + cartridgeType = 0; + break; + } + + r = unzGoToNextFile(unz); + + if(r != UNZ_OK) + break; + } + + if(!found) { + unzClose(unz); + systemMessage(0, "No image found on ZIP file %s", szFile); + exit(-1); + } + + unzClose(unz); + } + + if(utilIsGBImage(szFile) || cartridgeType == 1) { + failed = !gbLoadRom(szFile); + cartridgeType = 1; + emulator = GBSystem; + } else if(utilIsGBAImage(szFile) || cartridgeType == 0) { + failed = !CPULoadRom(szFile); + cartridgeType = 0; + emulator = GBASystem; + + //CPUInit(biosFileName, useBios); + CPUInit(); + CPUReset(); + } else { + systemMessage(0, "Unknown file type %s", szFile); + exit(-1); + } + + if(failed) { + systemMessage(0, "Failed to load file %s", szFile); + exit(-1); + } + strcpy(filename, szFile); + char *p = strrchr(filename, '.'); + + if(p) + *p = 0; + } else { + cartridgeType = 0; + strcpy(filename, "gnu_stub"); + rom = (u8 *)malloc(0x2000000); + workRAM = (u8 *)calloc(1, 0x40000); + bios = (u8 *)calloc(1,0x4000); + internalRAM = (u8 *)calloc(1,0x8000); + paletteRAM = (u8 *)calloc(1,0x400); + vram = (u8 *)calloc(1, 0x20000); + oam = (u8 *)calloc(1, 0x400); + pix = (u8 *)calloc(1, 4 * 240 * 160); + ioMem = (u8 *)calloc(1, 0x400); + + emulator = GBASystem; + + //CPUInit(biosFileName, useBios); + CPUInit(); + CPUReset(); + } + + if(debuggerStub) + remoteInit(); + + if(cartridgeType == 0) { + } else if (cartridgeType == 1) { + if(gbBorderOn) { + gbBorderLineSkip = 256; + gbBorderColumnSkip = 48; + gbBorderRowSkip = 40; + } else { + gbBorderLineSkip = 160; + gbBorderColumnSkip = 0; + gbBorderRowSkip = 0; + } + } else { + } + + for(int i = 0; i < 0x10000; i++) { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + + emulating = 1; + soundInit(); + + while(emulating) { + if(!paused) { + if(debugger && emulator.emuHasDebugger) + dbgMain(); + else + emulator.emuMain(emulator.emuCount); + } + } + emulating = 0; + fprintf(stderr,"Shutting down\n"); + remoteCleanUp(); + soundShutdown(); + + if(gbRom != NULL || rom != NULL) { + emulator.emuCleanUp(); + } + + return 0; +} + +void systemMessage(int num, const char *msg, ...) +{ + char buffer[2048]; + va_list valist; + + va_start(valist, msg); + vsprintf(buffer, msg, valist); + + fprintf(stderr, "%s\n", buffer); + va_end(valist); +} + +void systemDrawScreen() +{ +} + +bool systemReadJoypads() +{ + return true; +} + +u32 systemReadJoypad(int,bool) +{ + return 0; +} + +void systemShowSpeed(int speed) +{ +} + +void system10Frames(int rate) +{ +} + +void systemFrame(int) +{ +} + +void systemSetTitle(const char *title) +{ +} + +int systemScreenCapture(int a) +{ + char buffer[2048]; + + if(captureFormat) { + if(captureDir[0]) + sprintf(buffer, "%s/%s%02d.bmp", captureDir, sdlGetFilename(filename), a); + else + sprintf(buffer, "%s%02d.bmp", filename, a); + + emulator.emuWriteBMP(buffer); + } else { + if(captureDir[0]) + sprintf(buffer, "%s/%s%02d.png", captureDir, sdlGetFilename(filename), a); + else + sprintf(buffer, "%s%02d.png", filename, a); + emulator.emuWritePNG(buffer); + } + + systemScreenMessage("Screen capture"); +} + +u32 systemReadJoypadExtended() +{ + return 0; +} + +void systemWriteDataToSoundBuffer() +{ +} + +bool systemSoundInit() +{ + return true; +} + +void systemSoundShutdown() +{ +} + +void systemSoundPause() +{ +} + +void systemSoundResume() +{ +} + +void systemSoundReset() +{ +} + +static int ticks = 0; + +u32 systemGetClock() +{ + return ticks++; +} + +void systemUpdateMotionSensor() +{ +} + +int systemGetSensorX() +{ + return 0; +} + +int systemGetSensorY() +{ + return 0; +} + +void systemGbPrint(u8 *data,int pages,int feed,int palette, int contrast) +{ +} + +void systemScreenMessage(const char *msg, int slot, int duration, const char *colorList) +{ +} + +bool systemCanChangeSoundQuality() +{ + return false; +} + +bool systemPauseOnFrame() +{ + return false; +} + +void systemGbBorderOn() +{ +} + +bool sdlCheckJoyKey(int) +{ + return true; +} + +u16 checksumBIOS() +{ + bool hasBIOS = false; + u8 * tempBIOS; + if(useBios) + { + tempBIOS = (u8 *)malloc(0x4000); + int size = 0x4000; + if(utilLoad(biosFileName, + utilIsGBABios, + tempBIOS, + size)) { + if(size == 0x4000) + hasBIOS = true; + } + } + + u16 biosCheck = 0; + if(hasBIOS) { + for(int i = 0; i < 0x4000; i += 4) + biosCheck += *((u32 *)&tempBIOS[i]); + free(tempBIOS); + } + + return biosCheck; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/Types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/Types.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,33 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 2008 VBA-M development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef __VBA_TYPES_H__ +#define __VBA_TYPES_H__ + +#include + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +#endif // __VBA_TYPES_H__ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/debugger.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/debugger.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1460 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +extern "C" { +#include +#include +} // FIXME: should use c++ headers instead + +#include + +#include "Port.h" +#include "gba/GBA.h" +#include "gba/GBAGlobals.h" +#include "gba/GBACheats.h" +#include "gba/armdis.h" +#include "gba/elf.h" +#include "common/System.h" +#include "exprNode.h" + +extern bool debugger; +extern int emulating; + +extern struct EmulatedSystem theEmulator; + +#define debuggerReadMemory(addr) \ + READ32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define debuggerReadHalfWord(addr) \ + READ16LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +#define debuggerReadByte(addr) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] + +#define debuggerWriteMemory(addr, value) \ + WRITE32LE(&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask], value) + +#define debuggerWriteHalfWord(addr, value) \ + WRITE16LE(&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask], value) + +#define debuggerWriteByte(addr, value) \ + map[(addr)>>24].address[(addr) & map[(addr)>>24].mask] = (value) + +struct breakpointInfo { + u32 address; + u32 value; + int size; +}; + +struct DebuggerCommand { + const char *name; + void (*function)(int,char **); + const char *help; + const char *syntax; +}; + +void debuggerContinueAfterBreakpoint(); + +void debuggerHelp(int,char **); +void debuggerNext(int,char **); +void debuggerContinue(int, char **); +void debuggerRegisters(int, char **); +void debuggerBreak(int, char **); +void debuggerBreakDelete(int, char **); +void debuggerBreakList(int, char **); +void debuggerBreakArm(int, char **); +void debuggerBreakWriteClear(int, char **); +void debuggerBreakThumb(int, char **); +void debuggerBreakWrite(int, char **); +void debuggerDebug(int, char **); +void debuggerDisassemble(int, char **); +void debuggerDisassembleArm(int, char **); +void debuggerDisassembleThumb(int, char **); +void debuggerEditByte(int, char **); +void debuggerEditHalfWord(int, char **); +void debuggerEdit(int, char **); +void debuggerIo(int, char **); +void debuggerLocals(int, char **); +void debuggerMemoryByte(int, char **); +void debuggerMemoryHalfWord(int, char **); +void debuggerMemory(int, char **); +void debuggerPrint(int, char **); +void debuggerQuit(int, char **); +void debuggerSetRadix(int, char **); +void debuggerSymbols(int, char **); +void debuggerVerbose(int, char **); +void debuggerWhere(int, char **); + +DebuggerCommand debuggerCommands[] = { + { "?", debuggerHelp, "Shows this help information. Type ? for command help", "[]" }, + { "ba", debuggerBreakArm, "Adds an ARM breakpoint", "

" }, + { "bd", debuggerBreakDelete,"Deletes a breakpoint", "" }, + { "bl", debuggerBreakList, "Lists breakpoints" }, + { "bpw", debuggerBreakWrite, "Break on write", "
" }, + { "bpwc", debuggerBreakWriteClear, "Clear break on write", NULL }, + { "break", debuggerBreak, "Adds a breakpoint on the given function", "||" }, + { "bt", debuggerBreakThumb, "Adds a THUMB breakpoint", "
" }, + { "c", debuggerContinue, "Continues execution" , NULL }, + { "d", debuggerDisassemble, "Disassembles instructions", "[
[]]" }, + { "da", debuggerDisassembleArm, "Disassembles ARM instructions", "[
[]]" }, + { "dt", debuggerDisassembleThumb, "Disassembles THUMB instructions", "[
[]]" }, + { "eb", debuggerEditByte, "Modify memory location (byte)", "
" }, + { "eh", debuggerEditHalfWord,"Modify memory location (half-word)","
" }, + { "ew", debuggerEdit, "Modify memory location (word)", "
for command help", "[]" }, + { "io", debuggerIo, "Show I/O registers status", "[video|video2|dma|timer|misc]" }, + { "locals", debuggerLocals, "Shows local variables", NULL }, + { "mb", debuggerMemoryByte, "Shows memory contents (bytes)", "
" }, + { "mh", debuggerMemoryHalfWord, "Shows memory contents (half-words)", "
"}, + { "mw", debuggerMemory, "Shows memory contents (words)", "
" }, + { "n", debuggerNext, "Executes the next instruction", "[]" }, + { "print", debuggerPrint, "Print the value of a expression (if known)", "[/x|/o|/d] " }, + { "q", debuggerQuit, "Quits the emulator", NULL }, + { "r", debuggerRegisters, "Shows ARM registers", NULL }, + { "radix", debuggerSetRadix, "Sets the print radix", "" }, + { "symbols", debuggerSymbols, "List symbols", "[]" }, +#ifndef FINAL_VERSION + { "trace", debuggerDebug, "Sets the trace level", "" }, +#endif +#ifdef DEV_VERSION + { "verbose", debuggerVerbose, "Change verbose setting", "" }, +#endif + { "where", debuggerWhere, "Shows call chain", NULL }, + { NULL, NULL, NULL, NULL} // end marker +}; + +breakpointInfo debuggerBreakpointList[100]; + +int debuggerNumOfBreakpoints = 0; +bool debuggerAtBreakpoint = false; +int debuggerBreakpointNumber = 0; +int debuggerRadix = 0; + +void debuggerApplyBreakpoint(u32 address, int num, int size) +{ + if(size) + debuggerWriteMemory(address, (u32)(0xe1200070 | + (num & 0xf) | + ((num<<4)&0xf0))); + else + debuggerWriteHalfWord(address, + (u16)(0xbe00 | num)); +} + +void debuggerDisableBreakpoints() +{ + for(int i = 0; i < debuggerNumOfBreakpoints; i++) { + if(debuggerBreakpointList[i].size) + debuggerWriteMemory(debuggerBreakpointList[i].address, + debuggerBreakpointList[i].value); + else + debuggerWriteHalfWord(debuggerBreakpointList[i].address, + debuggerBreakpointList[i].value); + } +} + +void debuggerEnableBreakpoints(bool skipPC) +{ + for(int i = 0; i < debuggerNumOfBreakpoints; i++) { + if(debuggerBreakpointList[i].address == armNextPC && skipPC) + continue; + + debuggerApplyBreakpoint(debuggerBreakpointList[i].address, + i, + debuggerBreakpointList[i].size); + } +} + +void debuggerUsage(const char *cmd) +{ + for(int i = 0; ; i++) { + if(debuggerCommands[i].name) { + if(!strcmp(debuggerCommands[i].name, cmd)) { + printf("%s %s\t%s\n", + debuggerCommands[i].name, + debuggerCommands[i].syntax ? debuggerCommands[i].syntax : "", + debuggerCommands[i].help); + break; + } + } else { + printf("Unrecognized command '%s'.", cmd); + break; + } + } +} + +void debuggerPrintBaseType(Type *t, u32 value, u32 location, + LocationType type, + int bitSize, int bitOffset) +{ + if(bitSize) { + if(bitOffset) + value >>= ((t->size*8)-bitOffset-bitSize); + value &= (1 << bitSize)-1; + } else { + if(t->size == 2) + value &= 0xFFFF; + else if(t->size == 1) + value &= 0xFF; + } + + if(t->size == 8) { + u64 value = 0; + if(type == LOCATION_memory) { + value = debuggerReadMemory(location) | + ((u64)debuggerReadMemory(location+4)<<32); + } else if(type == LOCATION_register) { + value = reg[location].I | ((u64)reg[location+1].I << 32); + } + switch(t->encoding) { + case DW_ATE_signed: + switch(debuggerRadix) { + case 0: + printf("%lld", value); + break; + case 1: + printf("0x%llx", value); + break; + case 2: + printf("0%llo", value); + break; + } + break; + case DW_ATE_unsigned: + switch(debuggerRadix) { + case 0: + printf("%llu", value); + break; + case 1: + printf("0x%llx", value); + break; + case 2: + printf("0%llo", value); + break; + } + break; + default: + printf("Unknowing 64-bit encoding\n"); + } + return; + } + + switch(t->encoding) { + case DW_ATE_boolean: + if(value) + printf("true"); + else + printf("false"); + break; + case DW_ATE_signed: + switch(debuggerRadix) { + case 0: + printf("%d", value); + break; + case 1: + printf("0x%x", value); + break; + case 2: + printf("0%o", value); + break; + } + break; + case DW_ATE_unsigned: + case DW_ATE_unsigned_char: + switch(debuggerRadix) { + case 0: + printf("%u", value); + break; + case 1: + printf("0x%x", value); + break; + case 2: + printf("0%o", value); + break; + } + break; + default: + printf("UNKNOWN BASE %d %08x", t->encoding, value); + } +} + +char *debuggerPrintType(Type *t) +{ + char buffer[1024]; + static char buffer2[1024]; + + if(t->type == TYPE_pointer) { + if(t->pointer) + strcpy(buffer, debuggerPrintType(t->pointer)); + else + strcpy(buffer, "void"); + sprintf(buffer2, "%s *", buffer); + return buffer2; + } else if(t->type == TYPE_reference) { + strcpy(buffer, debuggerPrintType(t->pointer)); + sprintf(buffer2, "%s &", buffer); + return buffer2; + } + return t->name; +} + +void debuggerPrintValueInternal(Function *, Type *, ELFBlock *, int, int, u32); +void debuggerPrintValueInternal(Function *f, Type *t, + int bitSize, int bitOffset, + u32 objLocation, LocationType type); + +u32 debuggerGetValue(u32 location, LocationType type) +{ + switch(type) { + case LOCATION_memory: + return debuggerReadMemory(location); + case LOCATION_register: + return reg[location].I; + case LOCATION_value: + return location; + } + return 0; +} + +void debuggerPrintPointer(Type *t, u32 value) +{ + printf("(%s)0x%08x", debuggerPrintType(t), value); +} + +void debuggerPrintReference(Type *t, u32 value) +{ + printf("(%s)0x%08x", debuggerPrintType(t), value); +} + +void debuggerPrintFunction(Type *t, u32 value) +{ + printf("(%s)0x%08x", debuggerPrintType(t), value); +} + +void debuggerPrintArray(Type *t, u32 value) +{ + // todo + printf("(%s[])0x%08x", debuggerPrintType(t->array->type), value); +} + +void debuggerPrintMember(Function *f, + Member *m, + u32 objLocation, + u32 location) +{ + int bitSize = m->bitSize; + if(bitSize) { + u32 value = 0; + int off = m->bitOffset; + int size = m->byteSize; + u32 v = 0; + if(size == 1) + v = debuggerReadByte(location); + else if(size == 2) + v = debuggerReadHalfWord(location); + else if(size == 4) + v = debuggerReadMemory(location); + + while(bitSize) { + int top = size*8 - off; + int bot = top - bitSize; + top--; + if(bot >= 0) { + value = (v >> (size*8 - bitSize - off)) & ((1 << bitSize)-1); + bitSize = 0; + } else { + value |= (v & ((1 << top)-1)) << (bitSize - top); + bitSize -= (top+1); + location -= size; + off = 0; + if(size == 1) + v = debuggerReadByte(location); + else if(size == 2) + v = debuggerReadHalfWord(location); + else + v = debuggerReadMemory(location); + } + } + debuggerPrintBaseType(m->type, value, location, LOCATION_memory, + bitSize, 0); + } else { + debuggerPrintValueInternal(f, m->type, m->location, m->bitSize, + m->bitOffset, objLocation); + } +} + +void debuggerPrintStructure(Function *f, Type *t, u32 objLocation) +{ + printf("{"); + int count = t->structure->memberCount; + int i = 0; + while(i < count) { + Member *m = &t->structure->members[i]; + printf("%s=", m->name); + LocationType type; + u32 location = elfDecodeLocation(f, m->location, &type, objLocation); + debuggerPrintMember(f, m, objLocation, location); + i++; + if(i < count) + printf(","); + } + printf("}"); +} + +void debuggerPrintUnion(Function *f, Type *t, u32 objLocation) +{ + // todo + printf("{"); + int count = t->structure->memberCount; + int i = 0; + while(i < count) { + Member *m = &t->structure->members[i]; + printf("%s=", m->name); + debuggerPrintMember(f, m, objLocation, 0); + i++; + if(i < count) + printf(","); + } + printf("}"); +} + +void debuggerPrintEnum(Type *t, u32 value) +{ + int i; + for(i = 0; i < t->enumeration->count; i++) { + EnumMember *m = (EnumMember *)&t->enumeration->members[i]; + if(value == m->value) { + puts(m->name); + return; + } + } + printf("(UNKNOWN VALUE) %d", value); +} + +void debuggerPrintValueInternal(Function *f, Type *t, + int bitSize, int bitOffset, + u32 objLocation, LocationType type) +{ + u32 value = debuggerGetValue(objLocation, type); + if(!t) { + printf("void"); + return; + } + switch(t->type) { + case TYPE_base: + debuggerPrintBaseType(t, value, objLocation, type, bitSize, bitOffset); + break; + case TYPE_pointer: + debuggerPrintPointer(t, value); + break; + case TYPE_reference: + debuggerPrintReference(t, value); + break; + case TYPE_function: + debuggerPrintFunction(t, value); + break; + case TYPE_array: + debuggerPrintArray(t, objLocation); + break; + case TYPE_struct: + debuggerPrintStructure(f, t, objLocation); + break; + case TYPE_union: + debuggerPrintUnion(f, t, objLocation); + break; + case TYPE_enum: + debuggerPrintEnum(t, value); + break; + default: + printf("%08x", value); + break; + } +} + +void debuggerPrintValueInternal(Function *f, Type *t, ELFBlock *loc, + int bitSize, int bitOffset, u32 objLocation) +{ + LocationType type; + u32 location; + if(loc) { + if(objLocation) + location = elfDecodeLocation(f, loc, &type, objLocation); + else + location = elfDecodeLocation(f, loc,&type); + } else { + location = objLocation; + type = LOCATION_memory; + } + + debuggerPrintValueInternal(f, t, bitSize, bitOffset, location, type); +} + +void debuggerPrintValue(Function *f, Object *o) +{ + debuggerPrintValueInternal(f, o->type, o->location, 0, 0, 0); + + printf("\n"); +} + +void debuggerSymbols(int argc, char **argv) +{ + int i = 0; + u32 value; + u32 size; + int type; + bool match = false; + int matchSize = 0; + char *matchStr = NULL; + + if(argc == 2) { + match = true; + matchSize = strlen(argv[1]); + matchStr = argv[1]; + } + printf("Symbol Value Size Type \n"); + printf("-------------------- ------- -------- -------\n"); + char *s = NULL; + while((s = elfGetSymbol(i, &value, &size, &type))) { + if(*s) { + if(match) { + if(strncmp(s, matchStr, matchSize) != 0) { + i++; + continue; + } + } + const char *ts = "?"; + switch(type) { + case 2: + ts = "ARM"; + break; + case 0x0d: + ts = "THUMB"; + break; + case 1: + ts = "DATA"; + break; + } + printf("%-20s %08x %08x %-7s\n", + s, value, size, ts); + } + i++; + } +} + +void debuggerSetRadix(int argc, char **argv) +{ + if(argc != 2) + debuggerUsage(argv[0]); + else { + int r = atoi(argv[1]); + + bool error = false; + switch(r) { + case 10: + debuggerRadix = 0; + break; + case 8: + debuggerRadix = 2; + break; + case 16: + debuggerRadix = 1; + break; + default: + error = true; + printf("Unknown radix %d. Valid values are 8, 10 and 16.\n", r); + break; + } + if(!error) + printf("Radix set to %d\n", r); + } +} + +void debuggerPrint(int argc, char **argv) +{ + if(argc != 2 && argc != 3) { + debuggerUsage(argv[0]); + } else { + u32 pc = armNextPC; + Function *f = NULL; + CompileUnit *u = NULL; + + elfGetCurrentFunction(pc, + &f, &u); + + int oldRadix = debuggerRadix; + if(argc == 3) { + if(argv[1][0] == '/') { + if(argv[1][1] == 'x') + debuggerRadix = 1; + else if(argv[1][1] == 'o') + debuggerRadix = 2; + else if(argv[1][1] == 'd') + debuggerRadix = 0; + else { + printf("Unknown format %c\n", argv[1][1]); + return; + } + } else { + printf("Unknown option %s\n", argv[1]); + return; + } + } + + char *s = argc == 2 ? argv[1] : argv[2]; + + extern char *exprString; + extern int exprCol; + extern int yyparse(); + exprString = s; + exprCol = 0; + if(!yyparse()) { + extern Node *result; + if(result->resolve(result, f, u)) { + if(result->member) + debuggerPrintMember(f, + result->member, + result->objLocation, + result->location); + else + debuggerPrintValueInternal(f, result->type, 0, 0, + result->location, + result->locType); + printf("\n"); + } else { + printf("Error resolving expression\n"); + } + } else { + printf("Error parsing expression:\n"); + printf("%s\n", s); + exprCol--; + for(int i = 0; i < exprCol; i++) + printf(" "); + printf("^\n"); + } + extern void exprCleanBuffer(); + exprCleanBuffer(); + exprNodeCleanUp(); + debuggerRadix = oldRadix; + } +} + +void debuggerHelp(int n, char **args) +{ + if(n == 2) { + debuggerUsage(args[1]); + } else { + for(int i = 0; ; i++) { + if(debuggerCommands[i].name) { + printf("%s\t%s\n", debuggerCommands[i].name, debuggerCommands[i].help); + } else + break; + } + } +} + +void debuggerDebug(int n, char **args) +{ + if(n == 2) { + int v = 0; + sscanf(args[1], "%d", &v); + systemDebug = v; + printf("Debug level set to %d\n", systemDebug); + } else + debuggerUsage("trace"); +} + +void debuggerVerbose(int n, char **args) +{ + if(n == 2) { + int v = 0; + sscanf(args[1], "%d", &v); + systemVerbose = v; + printf("Verbose level set to %d\n", systemVerbose); + } else + debuggerUsage("verbose"); +} + +void debuggerWhere(int n, char **args) +{ + void elfPrintCallChain(u32); + elfPrintCallChain(armNextPC); +} + +void debuggerLocals(int n, char **args) +{ + Function *f = NULL; + CompileUnit *u = NULL; + u32 pc = armNextPC; + if(elfGetCurrentFunction(pc, + &f, &u)) { + Object *o = f->parameters; + while(o) { + printf("%s=", o->name); + debuggerPrintValue(f, o); + o = o->next; + } + + o = f->variables; + while(o) { + bool visible = o->startScope ? pc>=o->startScope : true; + if(visible) + visible = o->endScope ? pc < o->endScope : true; + if(visible) { + printf("%s=", o->name); + debuggerPrintValue(f, o); + } + o = o->next; + } + } else { + printf("No information for current address\n"); + } +} + +void debuggerNext(int n, char **args) +{ + int count = 1; + if(n == 2) { + sscanf(args[1], "%d", &count); + } + for(int i = 0; i < count; i++) { + if(debuggerAtBreakpoint) { + debuggerContinueAfterBreakpoint(); + debuggerEnableBreakpoints(false); + } else + theEmulator.emuMain(1); + } + debuggerDisableBreakpoints(); + Function *f = NULL; + CompileUnit *u = NULL; + u32 a = armNextPC; + if(elfGetCurrentFunction(a, &f, &u)) { + char *file; + int line = elfFindLine(u, f, a, &file); + + printf("File %s, function %s, line %d\n", file, f->name, + line); + } + debuggerRegisters(0, NULL); +} + +void debuggerContinue(int n, char **args) +{ + if(debuggerAtBreakpoint) + debuggerContinueAfterBreakpoint(); + debuggerEnableBreakpoints(false); + debugger = false; +} + +void debuggerSignal(int sig,int number) +{ + switch(sig) { + case 4: + { + printf("Illegal instruction at %08x\n", armNextPC); + debugger = true; + } + break; + case 5: + { + printf("Breakpoint %d reached\n", number); + debugger = true; + debuggerAtBreakpoint = true; + debuggerBreakpointNumber = number; + debuggerDisableBreakpoints(); + + Function *f = NULL; + CompileUnit *u = NULL; + + if(elfGetCurrentFunction(armNextPC, &f, &u)) { + char *file; + int line = elfFindLine(u,f,armNextPC,&file); + printf("File %s, function %s, line %d\n", file, f->name, + line); + } + } + break; + default: + printf("Unknown signal %d\n", sig); + break; + } +} + +void debuggerBreakList(int, char **) +{ + printf("Num Address Type Symbol\n"); + printf("--- -------- ----- ------\n"); + for(int i = 0; i < debuggerNumOfBreakpoints; i++) { + printf("%3d %08x %s %s\n",i, debuggerBreakpointList[i].address, + debuggerBreakpointList[i].size ? "ARM" : "THUMB", + elfGetAddressSymbol(debuggerBreakpointList[i].address)); + } +} + +void debuggerBreakDelete(int n, char **args) +{ + if(n == 2) { + int n = 0; + sscanf(args[1], "%d", &n); + printf("Deleting breakpoint %d (%d)\n", n, debuggerNumOfBreakpoints); + if(n >= 0 && n < debuggerNumOfBreakpoints) { + n++; + if(n < debuggerNumOfBreakpoints) { + for(int i = n; i < debuggerNumOfBreakpoints; i++) { + debuggerBreakpointList[i-1].address = + debuggerBreakpointList[i].address; + debuggerBreakpointList[i-1].value = + debuggerBreakpointList[i].value; + debuggerBreakpointList[i-1].size = + debuggerBreakpointList[i].size; + } + } + debuggerNumOfBreakpoints--; + } + } else + debuggerUsage("bd"); +} + +void debuggerBreak(int n, char **args) +{ + if(n == 2) { + u32 address = 0; + u32 value = 0; + int type = 0; + char *s = args[1]; + char c = *s; + if(strchr(s, ':')) { + char *name = s; + char *l = strchr(s, ':'); + *l++ = 0; + int line = atoi(l); + + u32 addr; + Function *f; + CompileUnit *u; + + if(elfFindLineInModule(&addr, name, line)) { + if(elfGetCurrentFunction(addr, &f, &u)) { + u32 addr2; + if(elfGetSymbolAddress(f->name, &addr2, &value, &type)) { + address = addr; + } else { + printf("Unable to get function symbol data\n"); + return; + } + } else { + printf("Unable to find function for address\n"); + return; + } + } else { + printf("Unable to find module or line\n"); + return; + } + } else if(c >= '0' && c <= '9') { + int line = atoi(s); + Function *f; + CompileUnit *u; + u32 addr; + + if(elfGetCurrentFunction(armNextPC, &f, &u)) { + if(elfFindLineInUnit(&addr, u, line)) { + if(elfGetCurrentFunction(addr, &f, &u)) { + u32 addr2; + if(elfGetSymbolAddress(f->name, &addr2, &value, &type)) { + address = addr; + } else { + printf("Unable to get function symbol data\n"); + return; + } + } else { + printf("Unable to find function for address\n"); + return; + } + } else { + printf("Unable to find line\n"); + return; + } + } else { + printf("Cannot find current function\n"); + return; + } + } else { + if(!elfGetSymbolAddress(s, &address, &value, &type)) { + printf("Function %s not found\n", args[1]); + return; + } + } + if(type == 0x02 || type == 0x0d) { + int i = debuggerNumOfBreakpoints; + int size = 0; + if(type == 2) + size = 1; + debuggerBreakpointList[i].address = address; + debuggerBreakpointList[i].value = type == 0x02 ? + debuggerReadMemory(address) : debuggerReadHalfWord(address); + debuggerBreakpointList[i].size = size; + // debuggerApplyBreakpoint(address, i, size); + debuggerNumOfBreakpoints++; + if(size) + printf("Added ARM breakpoint at %08x\n", address); + else + printf("Added THUMB breakpoint at %08x\n", address); + } else { + printf("%s is not a function symbol\n", args[1]); + } + } else + debuggerUsage("break"); +} + +void debuggerBreakThumb(int n, char **args) +{ + if(n == 2) { + u32 address = 0; + sscanf(args[1],"%x", &address); + int i = debuggerNumOfBreakpoints; + debuggerBreakpointList[i].address = address; + debuggerBreakpointList[i].value = debuggerReadHalfWord(address); + debuggerBreakpointList[i].size = 0; + // debuggerApplyBreakpoint(address, i, 0); + debuggerNumOfBreakpoints++; + printf("Added THUMB breakpoint at %08x\n", address); + } else + debuggerUsage("bt"); +} + +void debuggerBreakArm(int n, char **args) +{ + if(n == 2) { + u32 address = 0; + sscanf(args[1],"%x", &address); + int i = debuggerNumOfBreakpoints; + debuggerBreakpointList[i].address = address; + debuggerBreakpointList[i].value = debuggerReadMemory(address); + debuggerBreakpointList[i].size = 1; + // debuggerApplyBreakpoint(address, i, 1); + debuggerNumOfBreakpoints++; + printf("Added ARM breakpoint at %08x\n", address); + } else + debuggerUsage("ba"); +} + +void debuggerBreakOnWrite(u32 *mem, u32 oldvalue, u32 value, int size) +{ + u32 address = 0; + if(mem >= (u32*)&workRAM[0] && mem <= (u32*)&workRAM[0x3ffff]) + address = 0x2000000 + ((u64)mem - (u64)&workRAM[0]); + else + address = 0x3000000 + ((u64)mem - (u64)&internalRAM[0]); + + if(size == 2) + printf("Breakpoint (on write) address %08x old:%08x new:%08x\n", + address, oldvalue, value); + else if(size == 1) + printf("Breakpoint (on write) address %08x old:%04x new:%04x\n", + address, (u16)oldvalue,(u16)value); + else + printf("Breakpoint (on write) address %08x old:%02x new:%02x\n", + address, (u8)oldvalue, (u8)value); + debugger = true; +} + +void debuggerBreakWriteClear(int n, char **args) +{ + memset(freezeWorkRAM, false, 0x40000); + memset(freezeInternalRAM, false, 0x8000); + printf("Cleared all break on write\n"); +} + +void debuggerBreakWrite(int n, char **args) +{ + if(n == 3) { + if(cheatsNumber != 0) { + printf("Cheats are enabled. Cannot continue.\n"); + return; + } + u32 address = 0; + sscanf(args[1], "%x", &address); + int n = 0; + sscanf(args[2], "%d", &n); + + if(address < 0x2000000 || address > 0x3007fff) { + printf("Invalid address: %08x\n", address); + return; + } + + if(address > 0x203ffff && address < 0x3000000) { + printf("Invalid address: %08x\n", address); + return; + } + + u32 final = address + n; + + if(address < 0x2040000 && final > 0x2040000) { + printf("Invalid byte count: %d\n", n); + return; + } else if(address < 0x3008000 && final > 0x3008000) { + printf("Invalid byte count: %d\n", n); + return; + } + printf("Added break on write at %08x for %d bytes\n", address, n); + for(int i = 0; i < n; i++) { + if((address >> 24) == 2) + freezeWorkRAM[address & 0x3ffff] = true; + else + freezeInternalRAM[address & 0x7fff] = true; + address++; + } + } else + debuggerUsage("bpw"); +} + +void debuggerDisassembleArm(int n, char **args) +{ + char buffer[80]; + u32 pc = reg[15].I; + pc -= 4; + int count = 20; + if(n >= 2) { + sscanf(args[1], "%x", &pc); + } + if(pc & 3) { + printf("Misaligned address %08x\n", pc); + pc &= 0xfffffffc; + } + if(n >= 3) { + sscanf(args[2], "%d", &count); + } + int i = 0; + int len = 0; + char format[30]; + for(i = 0; i < count; i++) { + int l = strlen(elfGetAddressSymbol(pc+4*i)); + if(l > len) + len = l; + } + sprintf(format, "%%08x %%-%ds %%s\n", len); + for(i = 0; i < count; i++) { + u32 addr = pc; + pc += disArm(pc, buffer, 2); + printf(format, addr, elfGetAddressSymbol(addr), buffer); + } +} + +void debuggerDisassembleThumb(int n, char **args) +{ + char buffer[80]; + u32 pc = reg[15].I; + pc -= 2; + int count = 20; + if(n >= 2) { + sscanf(args[1], "%x", &pc); + } + if(pc & 1) { + printf("Misaligned address %08x\n", pc); + pc &= 0xfffffffe; + } + if(n >= 3) { + sscanf(args[2], "%d", &count); + } + + int i = 0; + int len = 0; + char format[30]; + for(i = 0; i < count; i++) { + int l = strlen(elfGetAddressSymbol(pc+2*i)); + if(l > len) + len = l; + } + sprintf(format, "%%08x %%-%ds %%s\n", len); + + for(i = 0; i < count; i++) { + u32 addr = pc; + pc += disThumb(pc, buffer, 2); + printf(format, addr, elfGetAddressSymbol(addr), buffer); + } +} + +void debuggerDisassemble(int n, char **args) +{ + if(armState) + debuggerDisassembleArm(n, args); + else + debuggerDisassembleThumb(n, args); +} + +void debuggerContinueAfterBreakpoint() +{ + printf("Continuing after breakpoint\n"); + debuggerEnableBreakpoints(true); + theEmulator.emuMain(1); + debuggerAtBreakpoint = false; +} + +void debuggerRegisters(int, char **) +{ + char *command[3]; + char buffer[10]; + + printf("R00=%08x R04=%08x R08=%08x R12=%08x\n", + reg[0].I, reg[4].I, reg[8].I, reg[12].I); + printf("R01=%08x R05=%08x R09=%08x R13=%08x\n", + reg[1].I, reg[5].I, reg[9].I, reg[13].I); + printf("R02=%08x R06=%08x R10=%08x R14=%08x\n", + reg[2].I, reg[6].I, reg[10].I, reg[14].I); + printf("R03=%08x R07=%08x R11=%08x R15=%08x\n", + reg[3].I, reg[7].I, reg[11].I, reg[15].I); + printf("CPSR=%08x (%c%c%c%c%c%c%c Mode: %02x)\n", + reg[16].I, + (N_FLAG ? 'N' : '.'), + (Z_FLAG ? 'Z' : '.'), + (C_FLAG ? 'C' : '.'), + (V_FLAG ? 'V' : '.'), + (armIrqEnable ? '.' : 'I'), + ((!(reg[16].I & 0x40)) ? '.' : 'F'), + (armState ? '.' : 'T'), + armMode); + sprintf(buffer,"%08x", armState ? reg[15].I - 4 : reg[15].I - 2); + command[0]=const_cast("m"); + command[1]=buffer; + command[2]=const_cast("1"); + debuggerDisassemble(3, command); +} + +void debuggerIoVideo() +{ + printf("DISPCNT = %04x\n", DISPCNT); + printf("DISPSTAT = %04x\n", DISPSTAT); + printf("VCOUNT = %04x\n", VCOUNT); + printf("BG0CNT = %04x\n", BG0CNT); + printf("BG1CNT = %04x\n", BG1CNT); + printf("BG2CNT = %04x\n", BG2CNT); + printf("BG3CNT = %04x\n", BG3CNT); + printf("WIN0H = %04x\n", WIN0H); + printf("WIN0V = %04x\n", WIN0V); + printf("WIN1H = %04x\n", WIN1H); + printf("WIN1V = %04x\n", WIN1V); + printf("WININ = %04x\n", WININ); + printf("WINOUT = %04x\n", WINOUT); + printf("MOSAIC = %04x\n", MOSAIC); + printf("BLDMOD = %04x\n", BLDMOD); + printf("COLEV = %04x\n", COLEV); + printf("COLY = %04x\n", COLY); +} + +void debuggerIoVideo2() +{ + printf("BG0HOFS = %04x\n", BG0HOFS); + printf("BG0VOFS = %04x\n", BG0VOFS); + printf("BG1HOFS = %04x\n", BG1HOFS); + printf("BG1VOFS = %04x\n", BG1VOFS); + printf("BG2HOFS = %04x\n", BG2HOFS); + printf("BG2VOFS = %04x\n", BG2VOFS); + printf("BG3HOFS = %04x\n", BG3HOFS); + printf("BG3VOFS = %04x\n", BG3VOFS); + printf("BG2PA = %04x\n", BG2PA); + printf("BG2PB = %04x\n", BG2PB); + printf("BG2PC = %04x\n", BG2PC); + printf("BG2PD = %04x\n", BG2PD); + printf("BG2X = %08x\n", (BG2X_H<<16)|BG2X_L); + printf("BG2Y = %08x\n", (BG2Y_H<<16)|BG2Y_L); + printf("BG3PA = %04x\n", BG3PA); + printf("BG3PB = %04x\n", BG3PB); + printf("BG3PC = %04x\n", BG3PC); + printf("BG3PD = %04x\n", BG3PD); + printf("BG3X = %08x\n", (BG3X_H<<16)|BG3X_L); + printf("BG3Y = %08x\n", (BG3Y_H<<16)|BG3Y_L); +} + +void debuggerIoDMA() +{ + printf("DM0SAD = %08x\n", (DM0SAD_H<<16)|DM0SAD_L); + printf("DM0DAD = %08x\n", (DM0DAD_H<<16)|DM0DAD_L); + printf("DM0CNT = %08x\n", (DM0CNT_H<<16)|DM0CNT_L); + printf("DM1SAD = %08x\n", (DM1SAD_H<<16)|DM1SAD_L); + printf("DM1DAD = %08x\n", (DM1DAD_H<<16)|DM1DAD_L); + printf("DM1CNT = %08x\n", (DM1CNT_H<<16)|DM1CNT_L); + printf("DM2SAD = %08x\n", (DM2SAD_H<<16)|DM2SAD_L); + printf("DM2DAD = %08x\n", (DM2DAD_H<<16)|DM2DAD_L); + printf("DM2CNT = %08x\n", (DM2CNT_H<<16)|DM2CNT_L); + printf("DM3SAD = %08x\n", (DM3SAD_H<<16)|DM3SAD_L); + printf("DM3DAD = %08x\n", (DM3DAD_H<<16)|DM3DAD_L); + printf("DM3CNT = %08x\n", (DM3CNT_H<<16)|DM3CNT_L); +} + +void debuggerIoTimer() +{ + printf("TM0D = %04x\n", TM0D); + printf("TM0CNT = %04x\n", TM0CNT); + printf("TM1D = %04x\n", TM1D); + printf("TM1CNT = %04x\n", TM1CNT); + printf("TM2D = %04x\n", TM2D); + printf("TM2CNT = %04x\n", TM2CNT); + printf("TM3D = %04x\n", TM3D); + printf("TM3CNT = %04x\n", TM3CNT); +} + +void debuggerIoMisc() +{ + printf("P1 = %04x\n", P1); + printf("IE = %04x\n", IE); + printf("IF = %04x\n", IF); + printf("IME = %04x\n", IME); +} + +void debuggerIo(int n, char **args) +{ + if(n == 1) { + debuggerIoVideo(); + return; + } + if(!strcmp(args[1], "video")) + debuggerIoVideo(); + else if(!strcmp(args[1], "video2")) + debuggerIoVideo2(); + else if(!strcmp(args[1], "dma")) + debuggerIoDMA(); + else if(!strcmp(args[1], "timer")) + debuggerIoTimer(); + else if(!strcmp(args[1], "misc")) + debuggerIoMisc(); + else printf("Unrecognized option %s\n", args[1]); +} + +void debuggerEditByte(int n, char **args) +{ + if(n == 3) { + u32 address; + u32 byte; + sscanf(args[1], "%x", &address); + sscanf(args[2], "%x", &byte); + debuggerWriteByte(address, (u8)byte); + } else + debuggerUsage("eb"); +} + +void debuggerEditHalfWord(int n, char **args) +{ + if(n == 3) { + u32 address; + u32 byte; + sscanf(args[1], "%x", &address); + if(address & 1) { + printf("Error: address must be half-word aligned\n"); + return; + } + sscanf(args[2], "%x", &byte); + debuggerWriteHalfWord(address, (u16)byte); + } else + debuggerUsage("eh"); +} + +void debuggerEdit(int n, char **args) +{ + if(n == 3) { + u32 address; + u32 byte; + sscanf(args[1], "%x", &address); + if(address & 3) { + printf("Error: address must be word aligned\n"); + return; + } + sscanf(args[2], "%x", &byte); + debuggerWriteMemory(address, (u32)byte); + } else + debuggerUsage("ew"); +} + + +#define ASCII(c) (c) < 32 ? '.' : (c) > 127 ? '.' : (c) + +void debuggerMemoryByte(int n, char **args) +{ + if(n == 2) { + u32 addr = 0; + sscanf(args[1], "%x", &addr); + for(int ii = 0; ii < 16; ii++) { + int a = debuggerReadByte(addr); + int b = debuggerReadByte(addr+1); + int c = debuggerReadByte(addr+2); + int d = debuggerReadByte(addr+3); + int e = debuggerReadByte(addr+4); + int f = debuggerReadByte(addr+5); + int g = debuggerReadByte(addr+6); + int h = debuggerReadByte(addr+7); + int i = debuggerReadByte(addr+8); + int j = debuggerReadByte(addr+9); + int k = debuggerReadByte(addr+10); + int l = debuggerReadByte(addr+11); + int m = debuggerReadByte(addr+12); + int n = debuggerReadByte(addr+13); + int o = debuggerReadByte(addr+14); + int p = debuggerReadByte(addr+15); + + printf("%08x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", + addr,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p, + ASCII(a),ASCII(b),ASCII(c),ASCII(d), + ASCII(e),ASCII(f),ASCII(g),ASCII(h), + ASCII(i),ASCII(j),ASCII(k),ASCII(l), + ASCII(m),ASCII(n),ASCII(o),ASCII(p)); + addr += 16; + } + } else + debuggerUsage("mb"); +} + +void debuggerMemoryHalfWord(int n, char **args) +{ + if(n == 2) { + u32 addr = 0; + sscanf(args[1], "%x", &addr); + addr = addr & 0xfffffffe; + for(int ii = 0; ii < 16; ii++) { + int a = debuggerReadByte(addr); + int b = debuggerReadByte(addr+1); + int c = debuggerReadByte(addr+2); + int d = debuggerReadByte(addr+3); + int e = debuggerReadByte(addr+4); + int f = debuggerReadByte(addr+5); + int g = debuggerReadByte(addr+6); + int h = debuggerReadByte(addr+7); + int i = debuggerReadByte(addr+8); + int j = debuggerReadByte(addr+9); + int k = debuggerReadByte(addr+10); + int l = debuggerReadByte(addr+11); + int m = debuggerReadByte(addr+12); + int n = debuggerReadByte(addr+13); + int o = debuggerReadByte(addr+14); + int p = debuggerReadByte(addr+15); + + printf("%08x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", + addr,b,a,d,c,f,e,h,g,j,i,l,k,n,m,p,o, + ASCII(a),ASCII(b),ASCII(c),ASCII(d), + ASCII(e),ASCII(f),ASCII(g),ASCII(h), + ASCII(i),ASCII(j),ASCII(k),ASCII(l), + ASCII(m),ASCII(n),ASCII(o),ASCII(p)); + addr += 16; + } + } else + debuggerUsage("mh"); +} + +void debuggerMemory(int n, char **args) +{ + if(n == 2) { + u32 addr = 0; + sscanf(args[1], "%x", &addr); + addr = addr & 0xfffffffc; + for(int ii = 0; ii < 16; ii++) { + int a = debuggerReadByte(addr); + int b = debuggerReadByte(addr+1); + int c = debuggerReadByte(addr+2); + int d = debuggerReadByte(addr+3); + + int e = debuggerReadByte(addr+4); + int f = debuggerReadByte(addr+5); + int g = debuggerReadByte(addr+6); + int h = debuggerReadByte(addr+7); + + int i = debuggerReadByte(addr+8); + int j = debuggerReadByte(addr+9); + int k = debuggerReadByte(addr+10); + int l = debuggerReadByte(addr+11); + + int m = debuggerReadByte(addr+12); + int n = debuggerReadByte(addr+13); + int o = debuggerReadByte(addr+14); + int p = debuggerReadByte(addr+15); + + printf("%08x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", + addr,d,c,b,a,h,g,f,e,l,k,j,i,p,o,n,m, + ASCII(a),ASCII(b),ASCII(c),ASCII(d), + ASCII(e),ASCII(f),ASCII(g),ASCII(h), + ASCII(i),ASCII(j),ASCII(k),ASCII(l), + ASCII(m),ASCII(n),ASCII(o),ASCII(p)); + addr += 16; + } + } else + debuggerUsage("mw"); +} + +void debuggerQuit(int, char **) +{ + char buffer[10]; + printf("Are you sure you want to quit (y/n)? "); + fgets(buffer, 1024, stdin); + + if(buffer[0] == 'y' || buffer[0] == 'Y') { + debugger = false; + emulating = false; + } +} + +void debuggerOutput(char *s, u32 addr) +{ + if(s) + puts(s); + else { + char c; + + c = debuggerReadByte(addr); + addr++; + while(c) { + putchar(c); + c = debuggerReadByte(addr); + addr++; + } + } +} + +void debuggerMain() +{ + char buffer[1024]; + char *commands[10]; + int commandCount = 0; + + if(theEmulator.emuUpdateCPSR) + theEmulator.emuUpdateCPSR(); + debuggerRegisters(0, NULL); + + while(debugger) { + systemSoundPause(); + printf("debugger> "); + commandCount = 0; + char *s = fgets(buffer, 1024, stdin); + + commands[0] = strtok(s, " \t\n"); + if(commands[0] == NULL) + continue; + commandCount++; + while((s = strtok(NULL, " \t\n"))) { + commands[commandCount++] = s; + if(commandCount == 10) + break; + } + + for(int j = 0; ; j++) { + if(debuggerCommands[j].name == NULL) { + printf("Unrecognized command %s. Type h for help.\n", commands[0]); + break; + } + if(!strcmp(commands[0], debuggerCommands[j].name)) { + debuggerCommands[j].function(commandCount, commands); + break; + } + } + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/debugger.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/debugger.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +extern void debuggerMain(); diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/expr-lex.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/expr-lex.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1590 @@ +#line 2 "expr-lex.cpp" +/* A lexical scanner generated by flex */ + +/* Scanner skeleton version: + * $Header: /cvsroot/vba/VisualBoyAdvance/src/expr-lex.cpp,v 1.2 2003/06/06 14:17:21 forgotten Exp $ + */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 5 + +#include +#ifdef __GNUC__ +#include +#endif + + +/* cfront 1.2 defines "c_plusplus" instead of "__cplusplus" */ +#ifdef c_plusplus +#ifndef __cplusplus +#define __cplusplus +#endif +#endif + + +#ifdef __cplusplus + +#include + +/* Use prototypes in function declarations. */ +#define YY_USE_PROTOS + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +#if __STDC__ + +#define YY_USE_PROTOS +#define YY_USE_CONST + +#endif /* __STDC__ */ +#endif /* ! __cplusplus */ + +#ifdef __TURBOC__ + #pragma warn -rch + #pragma warn -use +#include +#include +#define YY_USE_CONST +#define YY_USE_PROTOS +#endif + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + + +#ifdef YY_USE_PROTOS +#define YY_PROTO(proto) proto +#else +#define YY_PROTO(proto) () +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN yy_start = 1 + 2 * + +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START ((yy_start - 1) / 2) +#define YYSTATE YY_START + +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) + +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart( yyin ) + +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#define YY_BUF_SIZE 16384 + +typedef struct yy_buffer_state *YY_BUFFER_STATE; + +extern int yyleng; +extern FILE *yyin, *yyout; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + +/* The funky do-while in the following #define is used to turn the definition + * int a single C statement (which needs a semi-colon terminator). This + * avoids problems with code like: + * + * if ( condition_holds ) + * yyless( 5 ); + * else + * do_something_else(); + * + * Prior to using the do-while the compiler would get upset at the + * "else" because it interpreted the "if" statement as being all + * done when it reached the ';' after the yyless() call. + */ + +/* Return all but the first 'n' matched characters back to the input stream. */ + +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + *yy_cp = yy_hold_char; \ + YY_RESTORE_YY_MORE_OFFSET \ + yy_c_buf_p = yy_cp = yy_bp + n - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } \ + while ( 0 ) + +#define unput(c) yyunput( c, yytext_ptr ) + +/* The following is because we cannot portably get our hands on size_t + * (without autoconf's help, which isn't available because we want + * flex-generated scanners to compile on their own). + */ +typedef unsigned int yy_size_t; + + +struct yy_buffer_state + { + FILE *yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + }; + +static YY_BUFFER_STATE yy_current_buffer = 0; + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + */ +#define YY_CURRENT_BUFFER yy_current_buffer + + +/* yy_hold_char holds the character lost when yytext is formed. */ +static char yy_hold_char; + +static int yy_n_chars; /* number of characters read into yy_ch_buf */ + + +int yyleng; + +/* Points to current character in buffer. */ +static char *yy_c_buf_p = (char *) 0; +static int yy_init = 1; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow yywrap()'s to do buffer switches + * instead of setting up a fresh yyin. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void yyrestart YY_PROTO(( FILE *input_file )); + +void yy_switch_to_buffer YY_PROTO(( YY_BUFFER_STATE new_buffer )); +void yy_load_buffer_state YY_PROTO(( void )); +YY_BUFFER_STATE yy_create_buffer YY_PROTO(( FILE *file, int size )); +void yy_delete_buffer YY_PROTO(( YY_BUFFER_STATE b )); +void yy_init_buffer YY_PROTO(( YY_BUFFER_STATE b, FILE *file )); +void yy_flush_buffer YY_PROTO(( YY_BUFFER_STATE b )); +#define YY_FLUSH_BUFFER yy_flush_buffer( yy_current_buffer ) + +YY_BUFFER_STATE yy_scan_buffer YY_PROTO(( char *base, yy_size_t size )); +YY_BUFFER_STATE yy_scan_string YY_PROTO(( yyconst char *yy_str )); +YY_BUFFER_STATE yy_scan_bytes YY_PROTO(( yyconst char *bytes, int len )); + +static void *yy_flex_alloc YY_PROTO(( yy_size_t )); +static void *yy_flex_realloc YY_PROTO(( void *, yy_size_t )); +static void yy_flex_free YY_PROTO(( void * )); + +#define yy_new_buffer yy_create_buffer + +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! yy_current_buffer ) \ + yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); \ + yy_current_buffer->yy_is_interactive = is_interactive; \ + } + +#define yy_set_bol(at_bol) \ + { \ + if ( ! yy_current_buffer ) \ + yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); \ + yy_current_buffer->yy_at_bol = at_bol; \ + } + +#define YY_AT_BOL() (yy_current_buffer->yy_at_bol) + + +#define yywrap() 1 +#define YY_SKIP_YYWRAP +typedef unsigned char YY_CHAR; +FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0; +typedef int yy_state_type; +extern char *yytext; +#define yytext_ptr yytext + +static yy_state_type yy_get_previous_state YY_PROTO(( void )); +static yy_state_type yy_try_NUL_trans YY_PROTO(( yy_state_type current_state )); +static int yy_get_next_buffer YY_PROTO(( void )); +static void yy_fatal_error YY_PROTO(( yyconst char msg[] )); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + yytext_ptr = yy_bp; \ + yyleng = (int) (yy_cp - yy_bp); \ + yy_hold_char = *yy_cp; \ + *yy_cp = '\0'; \ + yy_c_buf_p = yy_cp; + +#define YY_NUM_RULES 10 +#define YY_END_OF_BUFFER 11 +static yyconst short int yy_accept[24] = + { 0, + 0, 0, 11, 9, 8, 8, 6, 7, 9, 4, + 3, 2, 2, 8, 5, 3, 2, 2, 2, 2, + 2, 1, 0 + } ; + +static yyconst int yy_ec[256] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 1, 1, 1, 1, 1, 4, 1, 1, + 1, 5, 1, 1, 6, 7, 1, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, + 1, 9, 1, 1, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 1, 1, 1, 1, 10, 1, 10, 10, 10, 10, + + 11, 12, 10, 10, 13, 10, 10, 10, 10, 10, + 14, 10, 10, 10, 15, 10, 10, 10, 10, 10, + 10, 16, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + } ; + +static yyconst int yy_meta[17] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, + 2, 2, 2, 2, 2, 2 + } ; + +static yyconst short int yy_base[25] = + { 0, + 0, 0, 32, 33, 15, 17, 33, 33, 22, 33, + 22, 0, 16, 19, 33, 20, 0, 11, 15, 11, + 12, 0, 33, 21 + } ; + +static yyconst short int yy_def[25] = + { 0, + 23, 1, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 24, 24, 23, 23, 23, 24, 24, 24, 24, + 24, 24, 0, 23 + } ; + +static yyconst short int yy_nxt[50] = + { 0, + 4, 5, 6, 7, 8, 9, 10, 11, 4, 12, + 12, 12, 12, 12, 13, 12, 14, 14, 14, 14, + 14, 14, 17, 22, 21, 20, 19, 16, 18, 16, + 15, 23, 3, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23 + } ; + +static yyconst short int yy_chk[50] = + { 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 5, 5, 6, 6, + 14, 14, 24, 21, 20, 19, 18, 16, 13, 11, + 9, 3, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23 + } ; + +static yy_state_type yy_last_accepting_state; +static char *yy_last_accepting_cpos; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char *yytext; +#line 1 "expr.l" +#define INITIAL 0 +#line 2 "expr.l" +#include "expr.cpp.h" + +#ifndef __GNUC__ +#include +#define isatty _isatty +#endif + +char *exprString; +int exprCol; + +#define YY_INPUT(buf,result,max_size) \ + { \ + int c = *exprString++; \ + exprCol++;\ + result = (c == 0) ? YY_NULL : (buf[0] = c, 1); \ + } +#define YY_MAIN 0 +#line 400 "expr-lex.cpp" + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +#ifdef __cplusplus +extern "C" int yywrap YY_PROTO(( void )); +#else +extern int yywrap YY_PROTO(( void )); +#endif +#endif + +#ifndef YY_NO_UNPUT +static void yyunput YY_PROTO(( int c, char *buf_ptr )); +#endif + +#ifndef yytext_ptr +static void yy_flex_strncpy YY_PROTO(( char *, yyconst char *, int )); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen YY_PROTO(( yyconst char * )); +#endif + +#ifndef YY_NO_INPUT +#ifdef __cplusplus +static int yyinput YY_PROTO(( void )); +#else +static int input YY_PROTO(( void )); +#endif +#endif + +#if YY_STACK_USED +static int yy_start_stack_ptr = 0; +static int yy_start_stack_depth = 0; +static int *yy_start_stack = 0; +#ifndef YY_NO_PUSH_STATE +static void yy_push_state YY_PROTO(( int new_state )); +#endif +#ifndef YY_NO_POP_STATE +static void yy_pop_state YY_PROTO(( void )); +#endif +#ifndef YY_NO_TOP_STATE +static int yy_top_state YY_PROTO(( void )); +#endif + +#else +#define YY_NO_PUSH_STATE 1 +#define YY_NO_POP_STATE 1 +#define YY_NO_TOP_STATE 1 +#endif + +#ifdef YY_MALLOC_DECL +YY_MALLOC_DECL +#else +#if __STDC__ +#ifndef __cplusplus +#include +#endif +#else +/* Just try to get by without declaring the routines. This will fail + * miserably on non-ANSI systems for which sizeof(size_t) != sizeof(int) + * or sizeof(void*) != sizeof(int). + */ +#endif +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#define YY_READ_BUF_SIZE 8192 +#endif + +/* Copy whatever the last rule matched to the standard output. */ + +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +#define ECHO (void) fwrite( yytext, yyleng, 1, yyout ) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ + if ( yy_current_buffer->yy_is_interactive ) \ + { \ + int c = '*', n; \ + for ( n = 0; n < max_size && \ + (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ + buf[n] = (char) c; \ + if ( c == '\n' ) \ + buf[n++] = (char) c; \ + if ( c == EOF && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); \ + result = n; \ + } \ + else if ( ((result = fread( buf, 1, max_size, yyin )) == 0) \ + && ferror( yyin ) ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) +#endif + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL int yylex YY_PROTO(( void )) +#endif + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK break; +#endif + +#define YY_RULE_SETUP \ + YY_USER_ACTION + +YY_DECL + { + register yy_state_type yy_current_state; + register char *yy_cp = NULL, *yy_bp = NULL; + register int yy_act; + +#line 31 "expr.l" + + +#line 554 "expr-lex.cpp" + + if ( yy_init ) + { + yy_init = 0; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! yy_start ) + yy_start = 1; /* first start state */ + + if ( ! yyin ) + yyin = stdin; + + if ( ! yyout ) + yyout = stdout; + + if ( ! yy_current_buffer ) + yy_current_buffer = + yy_create_buffer( yyin, YY_BUF_SIZE ); + + yy_load_buffer_state(); + } + + while ( 1 ) /* loops until end-of-file is reached */ + { + yy_cp = yy_c_buf_p; + + /* Support of yytext. */ + *yy_cp = yy_hold_char; + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = yy_start; +yy_match: + do + { + register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; + if ( yy_accept[yy_current_state] ) + { + yy_last_accepting_state = yy_current_state; + yy_last_accepting_cpos = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 24 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + ++yy_cp; + } + while ( yy_base[yy_current_state] != 33 ); + +yy_find_action: + yy_act = yy_accept[yy_current_state]; + if ( yy_act == 0 ) + { /* have to back up */ + yy_cp = yy_last_accepting_cpos; + yy_current_state = yy_last_accepting_state; + yy_act = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + + +do_action: /* This label is used only to access EOF actions. */ + + + switch ( yy_act ) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = yy_hold_char; + yy_cp = yy_last_accepting_cpos; + yy_current_state = yy_last_accepting_state; + goto yy_find_action; + +case 1: +YY_RULE_SETUP +#line 33 "expr.l" +{ + return TOKEN_SIZEOF; +} + YY_BREAK +case 2: +YY_RULE_SETUP +#line 37 "expr.l" +{ + return TOKEN_IDENTIFIER; +} + YY_BREAK +case 3: +YY_RULE_SETUP +#line 41 "expr.l" +{ + return TOKEN_NUMBER; +} + YY_BREAK +case 4: +YY_RULE_SETUP +#line 45 "expr.l" +{ + return TOKEN_DOT; +} + YY_BREAK +case 5: +YY_RULE_SETUP +#line 49 "expr.l" +{ + return TOKEN_ARROW; +} + YY_BREAK +case 6: +YY_RULE_SETUP +#line 53 "expr.l" +{ + return TOKEN_ADDR; +} + YY_BREAK +case 7: +YY_RULE_SETUP +#line 57 "expr.l" +{ + return TOKEN_STAR; +} + YY_BREAK +case 8: +YY_RULE_SETUP +#line 61 "expr.l" + + YY_BREAK +case 9: +YY_RULE_SETUP +#line 63 "expr.l" +return *yytext; + YY_BREAK +case 10: +YY_RULE_SETUP +#line 65 "expr.l" +ECHO; + YY_BREAK +#line 701 "expr-lex.cpp" +case YY_STATE_EOF(INITIAL): + yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - yytext_ptr) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = yy_hold_char; + YY_RESTORE_YY_MORE_OFFSET + + if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between yy_current_buffer and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + yy_n_chars = yy_current_buffer->yy_n_chars; + yy_current_buffer->yy_input_file = yyin; + yy_current_buffer->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( yy_c_buf_p <= &yy_current_buffer->yy_ch_buf[yy_n_chars] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + yy_c_buf_p = yytext_ptr + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state(); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = yytext_ptr + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++yy_c_buf_p; + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = yy_c_buf_p; + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer() ) + { + case EOB_ACT_END_OF_FILE: + { + yy_did_buffer_switch_on_eof = 0; + + if ( yywrap() ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + yy_c_buf_p = yytext_ptr + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! yy_did_buffer_switch_on_eof ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + yy_c_buf_p = + yytext_ptr + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state(); + + yy_cp = yy_c_buf_p; + yy_bp = yytext_ptr + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + yy_c_buf_p = + &yy_current_buffer->yy_ch_buf[yy_n_chars]; + + yy_current_state = yy_get_previous_state(); + + yy_cp = yy_c_buf_p; + yy_bp = yytext_ptr + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ + } /* end of yylex */ + + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ + +static int yy_get_next_buffer() + { + register char *dest = yy_current_buffer->yy_ch_buf; + register char *source = yytext_ptr; + register int number_to_move, i; + int ret_val; + + if ( yy_c_buf_p > &yy_current_buffer->yy_ch_buf[yy_n_chars + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( yy_current_buffer->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( yy_c_buf_p - yytext_ptr - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (int) (yy_c_buf_p - yytext_ptr) - 1; + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + yy_current_buffer->yy_n_chars = yy_n_chars = 0; + + else + { + int num_to_read = + yy_current_buffer->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ +#ifdef YY_USES_REJECT + YY_FATAL_ERROR( +"input buffer overflow, can't enlarge buffer because scanner uses REJECT" ); +#else + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = yy_current_buffer; + + int yy_c_buf_p_offset = + (int) (yy_c_buf_p - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + int new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + yy_flex_realloc( (void *) b->yy_ch_buf, + b->yy_buf_size + 2 ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = 0; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + yy_c_buf_p = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = yy_current_buffer->yy_buf_size - + number_to_move - 1; +#endif + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&yy_current_buffer->yy_ch_buf[number_to_move]), + yy_n_chars, num_to_read ); + + yy_current_buffer->yy_n_chars = yy_n_chars; + } + + if ( yy_n_chars == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart( yyin ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + yy_current_buffer->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + yy_n_chars += number_to_move; + yy_current_buffer->yy_ch_buf[yy_n_chars] = YY_END_OF_BUFFER_CHAR; + yy_current_buffer->yy_ch_buf[yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR; + + yytext_ptr = &yy_current_buffer->yy_ch_buf[0]; + + return ret_val; + } + + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + +static yy_state_type yy_get_previous_state() + { + register yy_state_type yy_current_state; + register char *yy_cp; + + yy_current_state = yy_start; + + for ( yy_cp = yytext_ptr + YY_MORE_ADJ; yy_cp < yy_c_buf_p; ++yy_cp ) + { + register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + if ( yy_accept[yy_current_state] ) + { + yy_last_accepting_state = yy_current_state; + yy_last_accepting_cpos = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 24 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + } + + return yy_current_state; + } + + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + +#ifdef YY_USE_PROTOS +static yy_state_type yy_try_NUL_trans( yy_state_type yy_current_state ) +#else +static yy_state_type yy_try_NUL_trans( yy_current_state ) +yy_state_type yy_current_state; +#endif + { + register int yy_is_jam; + register char *yy_cp = yy_c_buf_p; + + register YY_CHAR yy_c = 1; + if ( yy_accept[yy_current_state] ) + { + yy_last_accepting_state = yy_current_state; + yy_last_accepting_cpos = yy_cp; + } + while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) + { + yy_current_state = (int) yy_def[yy_current_state]; + if ( yy_current_state >= 24 ) + yy_c = yy_meta[(unsigned int) yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; + yy_is_jam = (yy_current_state == 23); + + return yy_is_jam ? 0 : yy_current_state; + } + + +#ifndef YY_NO_UNPUT +#ifdef YY_USE_PROTOS +static void yyunput( int c, register char *yy_bp ) +#else +static void yyunput( c, yy_bp ) +int c; +register char *yy_bp; +#endif + { + register char *yy_cp = yy_c_buf_p; + + /* undo effects of setting up yytext */ + *yy_cp = yy_hold_char; + + if ( yy_cp < yy_current_buffer->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + register int number_to_move = yy_n_chars + 2; + register char *dest = &yy_current_buffer->yy_ch_buf[ + yy_current_buffer->yy_buf_size + 2]; + register char *source = + &yy_current_buffer->yy_ch_buf[number_to_move]; + + while ( source > yy_current_buffer->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + yy_current_buffer->yy_n_chars = + yy_n_chars = yy_current_buffer->yy_buf_size; + + if ( yy_cp < yy_current_buffer->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + + yytext_ptr = yy_bp; + yy_hold_char = *yy_cp; + yy_c_buf_p = yy_cp; + } +#endif /* ifndef YY_NO_UNPUT */ + + +#ifdef __cplusplus +static int yyinput() +#else +static int input() +#endif + { + int c; + + *yy_c_buf_p = yy_hold_char; + + if ( *yy_c_buf_p == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( yy_c_buf_p < &yy_current_buffer->yy_ch_buf[yy_n_chars] ) + /* This was really a NUL. */ + *yy_c_buf_p = '\0'; + + else + { /* need more input */ + int offset = yy_c_buf_p - yytext_ptr; + ++yy_c_buf_p; + + switch ( yy_get_next_buffer() ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart( yyin ); + + /* fall through */ + + case EOB_ACT_END_OF_FILE: + { + if ( yywrap() ) + return EOF; + + if ( ! yy_did_buffer_switch_on_eof ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + yy_c_buf_p = yytext_ptr + offset; + break; + } + } + } + + c = *(unsigned char *) yy_c_buf_p; /* cast for 8-bit char's */ + *yy_c_buf_p = '\0'; /* preserve yytext */ + yy_hold_char = *++yy_c_buf_p; + + + return c; + } + + +#ifdef YY_USE_PROTOS +void yyrestart( FILE *input_file ) +#else +void yyrestart( input_file ) +FILE *input_file; +#endif + { + if ( ! yy_current_buffer ) + yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); + + yy_init_buffer( yy_current_buffer, input_file ); + yy_load_buffer_state(); + } + + +#ifdef YY_USE_PROTOS +void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer ) +#else +void yy_switch_to_buffer( new_buffer ) +YY_BUFFER_STATE new_buffer; +#endif + { + if ( yy_current_buffer == new_buffer ) + return; + + if ( yy_current_buffer ) + { + /* Flush out information for old buffer. */ + *yy_c_buf_p = yy_hold_char; + yy_current_buffer->yy_buf_pos = yy_c_buf_p; + yy_current_buffer->yy_n_chars = yy_n_chars; + } + + yy_current_buffer = new_buffer; + yy_load_buffer_state(); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + yy_did_buffer_switch_on_eof = 1; + } + + +#ifdef YY_USE_PROTOS +void yy_load_buffer_state( void ) +#else +void yy_load_buffer_state() +#endif + { + yy_n_chars = yy_current_buffer->yy_n_chars; + yytext_ptr = yy_c_buf_p = yy_current_buffer->yy_buf_pos; + yyin = yy_current_buffer->yy_input_file; + yy_hold_char = *yy_c_buf_p; + } + + +#ifdef YY_USE_PROTOS +YY_BUFFER_STATE yy_create_buffer( FILE *file, int size ) +#else +YY_BUFFER_STATE yy_create_buffer( file, size ) +FILE *file; +int size; +#endif + { + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_buf_size = size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) yy_flex_alloc( b->yy_buf_size + 2 ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_is_our_buffer = 1; + + yy_init_buffer( b, file ); + + return b; + } + + +#ifdef YY_USE_PROTOS +void yy_delete_buffer( YY_BUFFER_STATE b ) +#else +void yy_delete_buffer( b ) +YY_BUFFER_STATE b; +#endif + { + if ( ! b ) + return; + + if ( b == yy_current_buffer ) + yy_current_buffer = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + yy_flex_free( (void *) b->yy_ch_buf ); + + yy_flex_free( (void *) b ); + } + + + +#ifdef YY_USE_PROTOS +void yy_init_buffer( YY_BUFFER_STATE b, FILE *file ) +#else +void yy_init_buffer( b, file ) +YY_BUFFER_STATE b; +FILE *file; +#endif + + + { + yy_flush_buffer( b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + +#if YY_ALWAYS_INTERACTIVE + b->yy_is_interactive = 1; +#else +#if YY_NEVER_INTERACTIVE + b->yy_is_interactive = 0; +#else + b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; +#endif +#endif + } + + +#ifdef YY_USE_PROTOS +void yy_flush_buffer( YY_BUFFER_STATE b ) +#else +void yy_flush_buffer( b ) +YY_BUFFER_STATE b; +#endif + + { + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == yy_current_buffer ) + yy_load_buffer_state(); + } + + +#ifndef YY_NO_SCAN_BUFFER +#ifdef YY_USE_PROTOS +YY_BUFFER_STATE yy_scan_buffer( char *base, yy_size_t size ) +#else +YY_BUFFER_STATE yy_scan_buffer( base, size ) +char *base; +yy_size_t size; +#endif + { + YY_BUFFER_STATE b; + + if ( size < 2 || + base[size-2] != YY_END_OF_BUFFER_CHAR || + base[size-1] != YY_END_OF_BUFFER_CHAR ) + /* They forgot to leave room for the EOB's. */ + return 0; + + b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); + + b->yy_buf_size = size - 2; /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = 0; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + yy_switch_to_buffer( b ); + + return b; + } +#endif + + +#ifndef YY_NO_SCAN_STRING +#ifdef YY_USE_PROTOS +YY_BUFFER_STATE yy_scan_string( yyconst char *yy_str ) +#else +YY_BUFFER_STATE yy_scan_string( yy_str ) +yyconst char *yy_str; +#endif + { + int len; + for ( len = 0; yy_str[len]; ++len ) + ; + + return yy_scan_bytes( yy_str, len ); + } +#endif + + +#ifndef YY_NO_SCAN_BYTES +#ifdef YY_USE_PROTOS +YY_BUFFER_STATE yy_scan_bytes( yyconst char *bytes, int len ) +#else +YY_BUFFER_STATE yy_scan_bytes( bytes, len ) +yyconst char *bytes; +int len; +#endif + { + YY_BUFFER_STATE b; + char *buf; + yy_size_t n; + int i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = len + 2; + buf = (char *) yy_flex_alloc( n ); + if ( ! buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); + + for ( i = 0; i < len; ++i ) + buf[i] = bytes[i]; + + buf[len] = buf[len+1] = YY_END_OF_BUFFER_CHAR; + + b = yy_scan_buffer( buf, n ); + if ( ! b ) + YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; + } +#endif + + +#ifndef YY_NO_PUSH_STATE +#ifdef YY_USE_PROTOS +static void yy_push_state( int new_state ) +#else +static void yy_push_state( new_state ) +int new_state; +#endif + { + if ( yy_start_stack_ptr >= yy_start_stack_depth ) + { + yy_size_t new_size; + + yy_start_stack_depth += YY_START_STACK_INCR; + new_size = yy_start_stack_depth * sizeof( int ); + + if ( ! yy_start_stack ) + yy_start_stack = (int *) yy_flex_alloc( new_size ); + + else + yy_start_stack = (int *) yy_flex_realloc( + (void *) yy_start_stack, new_size ); + + if ( ! yy_start_stack ) + YY_FATAL_ERROR( + "out of memory expanding start-condition stack" ); + } + + yy_start_stack[yy_start_stack_ptr++] = YY_START; + + BEGIN(new_state); + } +#endif + + +#ifndef YY_NO_POP_STATE +static void yy_pop_state() + { + if ( --yy_start_stack_ptr < 0 ) + YY_FATAL_ERROR( "start-condition stack underflow" ); + + BEGIN(yy_start_stack[yy_start_stack_ptr]); + } +#endif + + +#ifndef YY_NO_TOP_STATE +static int yy_top_state() + { + return yy_start_stack[yy_start_stack_ptr - 1]; + } +#endif + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +#ifdef YY_USE_PROTOS +static void yy_fatal_error( yyconst char msg[] ) +#else +static void yy_fatal_error( msg ) +char msg[]; +#endif + { + (void) fprintf( stderr, "%s\n", msg ); + exit( YY_EXIT_FAILURE ); + } + + + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + yytext[yyleng] = yy_hold_char; \ + yy_c_buf_p = yytext + n; \ + yy_hold_char = *yy_c_buf_p; \ + *yy_c_buf_p = '\0'; \ + yyleng = n; \ + } \ + while ( 0 ) + + +/* Internal utility routines. */ + +#ifndef yytext_ptr +#ifdef YY_USE_PROTOS +static void yy_flex_strncpy( char *s1, yyconst char *s2, int n ) +#else +static void yy_flex_strncpy( s1, s2, n ) +char *s1; +yyconst char *s2; +int n; +#endif + { + register int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; + } +#endif + +#ifdef YY_NEED_STRLEN +#ifdef YY_USE_PROTOS +static int yy_flex_strlen( yyconst char *s ) +#else +static int yy_flex_strlen( s ) +yyconst char *s; +#endif + { + register int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; + } +#endif + + +#ifdef YY_USE_PROTOS +static void *yy_flex_alloc( yy_size_t size ) +#else +static void *yy_flex_alloc( size ) +yy_size_t size; +#endif + { + return (void *) malloc( size ); + } + +#ifdef YY_USE_PROTOS +static void *yy_flex_realloc( void *ptr, yy_size_t size ) +#else +static void *yy_flex_realloc( ptr, size ) +void *ptr; +yy_size_t size; +#endif + { + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return (void *) realloc( (char *) ptr, size ); + } + +#ifdef YY_USE_PROTOS +static void yy_flex_free( void *ptr ) +#else +static void yy_flex_free( ptr ) +void *ptr; +#endif + { + free( ptr ); + } + +#if YY_MAIN +int main() + { + yylex(); + return 0; + } +#endif +#line 65 "expr.l" + + +void exprCleanBuffer() +{ + yy_delete_buffer(yy_current_buffer); + yy_init = 1; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/expr.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/expr.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,998 @@ + +/* A Bison parser, made from expr.y + by GNU Bison version 1.28 */ + +#define YYBISON 1 /* Identify Bison output. */ + +#define TOKEN_IDENTIFIER 257 +#define TOKEN_DOT 258 +#define TOKEN_STAR 259 +#define TOKEN_ARROW 260 +#define TOKEN_ADDR 261 +#define TOKEN_SIZEOF 262 +#define TOKEN_NUMBER 263 + +#line 1 "expr.y" + +namespace std { +#include +#include +#include +#include +} + +using namespace std; + +#include "../common/System.h" +#include "../gba/elf.h" +#include "exprNode.h" + +extern int yyerror(char *); +extern int yylex(); +extern char *yytext; + + +//#define YYERROR_VERBOSE 1 +//#define YYDEBUG 1 + + Node *result = NULL; +#ifndef YYSTYPE +#define YYSTYPE int +#endif +#include + +#ifndef __cplusplus +#ifndef __STDC__ +#define const +#endif +#endif + + + +#define YYFINAL 26 +#define YYFLAG -32768 +#define YYNTBASE 14 + +#define YYTRANSLATE(x) ((unsigned)(x) <= 263 ? yytranslate[x] : 19) + +static const char yytranslate[] = { 0, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 11, + 12, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 10, 2, 13, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 1, 3, 4, 5, 6, + 7, 8, 9 +}; + +#if YYDEBUG != 0 +static const short yyprhs[] = { 0, + 0, 2, 4, 8, 12, 16, 21, 23, 26, 29, + 34, 36 +}; + +static const short yyrhs[] = { 15, + 0, 16, 0, 11, 15, 12, 0, 15, 4, 18, + 0, 15, 6, 18, 0, 15, 10, 17, 13, 0, + 18, 0, 5, 15, 0, 7, 15, 0, 8, 11, + 15, 12, 0, 9, 0, 3, 0 +}; + +#endif + +#if YYDEBUG != 0 +static const short yyrline[] = { 0, + 32, 35, 36, 37, 38, 39, 42, 43, 44, 45, + 49, 53 +}; +#endif + + +#if YYDEBUG != 0 || defined (YYERROR_VERBOSE) + +static const char * const yytname[] = { "$","error","$undefined.","TOKEN_IDENTIFIER", +"TOKEN_DOT","TOKEN_STAR","TOKEN_ARROW","TOKEN_ADDR","TOKEN_SIZEOF","TOKEN_NUMBER", +"'['","'('","')'","']'","final","expression","simple_expression","number","ident", NULL +}; +#endif + +static const short yyr1[] = { 0, + 14, 15, 15, 15, 15, 15, 16, 16, 16, 16, + 17, 18 +}; + +static const short yyr2[] = { 0, + 1, 1, 3, 3, 3, 4, 1, 2, 2, 4, + 1, 1 +}; + +static const short yydefact[] = { 0, + 12, 0, 0, 0, 0, 1, 2, 7, 8, 9, + 0, 0, 0, 0, 0, 0, 3, 4, 5, 11, + 0, 10, 6, 0, 0, 0 +}; + +static const short yydefgoto[] = { 24, + 6, 7, 21, 8 +}; + +static const short yypact[] = { -1, +-32768, -1, -1, -6, -1, 17,-32768,-32768, 17, 17, + -1, 7, 5, 5, 13, 8,-32768,-32768,-32768,-32768, + 11,-32768,-32768, 25, 26,-32768 +}; + +static const short yypgoto[] = {-32768, + -2,-32768,-32768, 2 +}; + + +#define YYLAST 27 + + +static const short yytable[] = { 9, + 10, 1, 12, 2, 11, 3, 4, 1, 16, 5, + 13, 13, 14, 14, 18, 19, 15, 15, 17, 22, + 13, 20, 14, 23, 25, 26, 15 +}; + +static const short yycheck[] = { 2, + 3, 3, 5, 5, 11, 7, 8, 3, 11, 11, + 4, 4, 6, 6, 13, 14, 10, 10, 12, 12, + 4, 9, 6, 13, 0, 0, 10 +}; +/* -*-C-*- Note some compilers choke on comments on `#line' lines. */ +#line 3 "/usr/lib/bison.simple" +/* This file comes from bison-1.28. */ + +/* Skeleton output parser for bison, + Copyright (C) 1984, 1989, 1990 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* As a special exception, when this file is copied by Bison into a + Bison output file, you may use that output file without restriction. + This special exception was added by the Free Software Foundation + in version 1.24 of Bison. */ + +/* This is the parser code that is written into each bison parser + when the %semantic_parser declaration is not specified in the grammar. + It was written by Richard Stallman by simplifying the hairy parser + used when %semantic_parser is specified. */ + +#ifndef YYSTACK_USE_ALLOCA +#ifdef alloca +#define YYSTACK_USE_ALLOCA +#else /* alloca not defined */ +#ifdef __GNUC__ +#define YYSTACK_USE_ALLOCA +#define alloca __builtin_alloca +#else /* not GNU C. */ +#if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi) || (defined (__sun) && defined (__i386)) +#define YYSTACK_USE_ALLOCA +#include +#else /* not sparc */ +/* We think this test detects Watcom and Microsoft C. */ +/* This used to test MSDOS, but that is a bad idea + since that symbol is in the user namespace. */ +#if (defined (_MSDOS) || defined (_MSDOS_)) && !defined (__TURBOC__) +#if 0 /* No need for malloc.h, which pollutes the namespace; + instead, just don't use alloca. */ +#include +#endif +#else /* not MSDOS, or __TURBOC__ */ +#if defined(_AIX) +/* I don't know what this was needed for, but it pollutes the namespace. + So I turned it off. rms, 2 May 1997. */ +/* #include */ + #pragma alloca +#define YYSTACK_USE_ALLOCA +#else /* not MSDOS, or __TURBOC__, or _AIX */ +#if 0 +#ifdef __hpux /* haible@ilog.fr says this works for HPUX 9.05 and up, + and on HPUX 10. Eventually we can turn this on. */ +#define YYSTACK_USE_ALLOCA +#define alloca __builtin_alloca +#endif /* __hpux */ +#endif +#endif /* not _AIX */ +#endif /* not MSDOS, or __TURBOC__ */ +#endif /* not sparc */ +#endif /* not GNU C */ +#endif /* alloca not defined */ +#endif /* YYSTACK_USE_ALLOCA not defined */ + +#ifdef YYSTACK_USE_ALLOCA +#define YYSTACK_ALLOC alloca +#else +#define YYSTACK_ALLOC malloc +#endif + +/* Note: there must be only one dollar sign in this file. + It is replaced by the list of actions, each action + as one case of the switch. */ + +#define yyerrok (yyerrstatus = 0) +#define yyclearin (yychar = YYEMPTY) +#define YYEMPTY -2 +#define YYEOF 0 +#define YYACCEPT goto yyacceptlab +#define YYABORT goto yyabortlab +#define YYERROR goto yyerrlab1 +/* Like YYERROR except do call yyerror. + This remains here temporarily to ease the + transition to the new meaning of YYERROR, for GCC. + Once GCC version 2 has supplanted version 1, this can go. */ +#define YYFAIL goto yyerrlab +#define YYRECOVERING() (!!yyerrstatus) +#define YYBACKUP(token, value) \ +do \ + if (yychar == YYEMPTY && yylen == 1) \ + { yychar = (token), yylval = (value); \ + yychar1 = YYTRANSLATE (yychar); \ + YYPOPSTACK; \ + goto yybackup; \ + } \ + else \ + { yyerror ("syntax error: cannot back up"); YYERROR; } \ +while (0) + +#define YYTERROR 1 +#define YYERRCODE 256 + +#ifndef YYPURE +#define YYLEX yylex() +#endif + +#ifdef YYPURE +#ifdef YYLSP_NEEDED +#ifdef YYLEX_PARAM +#define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM) +#else +#define YYLEX yylex(&yylval, &yylloc) +#endif +#else /* not YYLSP_NEEDED */ +#ifdef YYLEX_PARAM +#define YYLEX yylex(&yylval, YYLEX_PARAM) +#else +#define YYLEX yylex(&yylval) +#endif +#endif /* not YYLSP_NEEDED */ +#endif + +/* If nonreentrant, generate the variables here */ + +#ifndef YYPURE + +int yychar; /* the lookahead symbol */ +YYSTYPE yylval; /* the semantic value of the */ + /* lookahead symbol */ + +#ifdef YYLSP_NEEDED +YYLTYPE yylloc; /* location data for the lookahead */ + /* symbol */ +#endif + +int yynerrs; /* number of parse errors so far */ +#endif /* not YYPURE */ + +#if YYDEBUG != 0 +int yydebug; /* nonzero means print parse trace */ +/* Since this is uninitialized, it does not stop multiple parsers + from coexisting. */ +#endif + +/* YYINITDEPTH indicates the initial size of the parser's stacks */ + +#ifndef YYINITDEPTH +#define YYINITDEPTH 200 +#endif + +/* YYMAXDEPTH is the maximum size the stacks can grow to + (effective only if the built-in stack extension method is used). */ + +#if YYMAXDEPTH == 0 +#undef YYMAXDEPTH +#endif + +#ifndef YYMAXDEPTH +#define YYMAXDEPTH 10000 +#endif + +/* Define __yy_memcpy. Note that the size argument + should be passed with type unsigned int, because that is what the non-GCC + definitions require. With GCC, __builtin_memcpy takes an arg + of type size_t, but it can handle unsigned int. */ + +#if __GNUC__ > 1 /* GNU C and GNU C++ define this. */ +#define __yy_memcpy(TO,FROM,COUNT) __builtin_memcpy(TO,FROM,COUNT) +#else /* not GNU C or C++ */ +#ifndef __cplusplus + +/* This is the most reliable way to avoid incompatibilities + in available built-in functions on various systems. */ +static void +__yy_memcpy (to, from, count) + char *to; + char *from; + unsigned int count; +{ + register char *f = from; + register char *t = to; + register int i = count; + + while (i-- > 0) + *t++ = *f++; +} + +#else /* __cplusplus */ + +/* This is the most reliable way to avoid incompatibilities + in available built-in functions on various systems. */ +static void +__yy_memcpy (char *to, char *from, unsigned int count) +{ + register char *t = to; + register char *f = from; + register int i = count; + + while (i-- > 0) + *t++ = *f++; +} + +#endif +#endif + +#line 217 "/usr/lib/bison.simple" + +/* The user can define YYPARSE_PARAM as the name of an argument to be passed + into yyparse. The argument should have type void *. + It should actually point to an object. + Grammar actions can access the variable by casting it + to the proper pointer type. */ + +#ifdef YYPARSE_PARAM +#ifdef __cplusplus +#define YYPARSE_PARAM_ARG void *YYPARSE_PARAM +#define YYPARSE_PARAM_DECL +#else /* not __cplusplus */ +#define YYPARSE_PARAM_ARG YYPARSE_PARAM +#define YYPARSE_PARAM_DECL void *YYPARSE_PARAM; +#endif /* not __cplusplus */ +#else /* not YYPARSE_PARAM */ +#define YYPARSE_PARAM_ARG +#define YYPARSE_PARAM_DECL +#endif /* not YYPARSE_PARAM */ + +/* Prevent warning if -Wstrict-prototypes. */ +#ifdef __GNUC__ +#ifdef YYPARSE_PARAM +int yyparse (void *); +#else +int yyparse (void); +#endif +#endif + +int +yyparse(YYPARSE_PARAM_ARG) + YYPARSE_PARAM_DECL +{ + register int yystate; + register int yyn; + register short *yyssp; + register YYSTYPE *yyvsp; + int yyerrstatus; /* number of tokens to shift before error messages enabled */ + int yychar1 = 0; /* lookahead token as an internal (translated) token number */ + + short yyssa[YYINITDEPTH]; /* the state stack */ + YYSTYPE yyvsa[YYINITDEPTH]; /* the semantic value stack */ + + short *yyss = yyssa; /* refer to the stacks thru separate pointers */ + YYSTYPE *yyvs = yyvsa; /* to allow yyoverflow to reallocate them elsewhere */ + +#ifdef YYLSP_NEEDED + YYLTYPE yylsa[YYINITDEPTH]; /* the location stack */ + YYLTYPE *yyls = yylsa; + YYLTYPE *yylsp; + +#define YYPOPSTACK (yyvsp--, yyssp--, yylsp--) +#else +#define YYPOPSTACK (yyvsp--, yyssp--) +#endif + + int yystacksize = YYINITDEPTH; + int yyfree_stacks = 0; + +#ifdef YYPURE + int yychar; + YYSTYPE yylval; + int yynerrs; +#ifdef YYLSP_NEEDED + YYLTYPE yylloc; +#endif +#endif + + YYSTYPE yyval; /* the variable used to return */ + /* semantic values from the action */ + /* routines */ + + int yylen; + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Starting parse\n"); +#endif + + yystate = 0; + yyerrstatus = 0; + yynerrs = 0; + yychar = YYEMPTY; /* Cause a token to be read. */ + + /* Initialize stack pointers. + Waste one element of value and location stack + so that they stay on the same level as the state stack. + The wasted elements are never initialized. */ + + yyssp = yyss - 1; + yyvsp = yyvs; +#ifdef YYLSP_NEEDED + yylsp = yyls; +#endif + +/* Push a new state, which is found in yystate . */ +/* In all cases, when you get here, the value and location stacks + have just been pushed. so pushing a state here evens the stacks. */ +yynewstate: + + *++yyssp = yystate; + + if (yyssp >= yyss + yystacksize - 1) + { + /* Give user a chance to reallocate the stack */ + /* Use copies of these so that the &'s don't force the real ones into memory. */ + YYSTYPE *yyvs1 = yyvs; + short *yyss1 = yyss; +#ifdef YYLSP_NEEDED + YYLTYPE *yyls1 = yyls; +#endif + + /* Get the current used size of the three stacks, in elements. */ + int size = yyssp - yyss + 1; + +#ifdef yyoverflow + /* Each stack pointer address is followed by the size of + the data in use in that stack, in bytes. */ +#ifdef YYLSP_NEEDED + /* This used to be a conditional around just the two extra args, + but that might be undefined if yyoverflow is a macro. */ + yyoverflow("parser stack overflow", + &yyss1, size * sizeof (*yyssp), + &yyvs1, size * sizeof (*yyvsp), + &yyls1, size * sizeof (*yylsp), + &yystacksize); +#else + yyoverflow("parser stack overflow", + &yyss1, size * sizeof (*yyssp), + &yyvs1, size * sizeof (*yyvsp), + &yystacksize); +#endif + + yyss = yyss1; yyvs = yyvs1; +#ifdef YYLSP_NEEDED + yyls = yyls1; +#endif +#else /* no yyoverflow */ + /* Extend the stack our own way. */ + if (yystacksize >= YYMAXDEPTH) + { + yyerror("parser stack overflow"); + if (yyfree_stacks) + { + free (yyss); + free (yyvs); +#ifdef YYLSP_NEEDED + free (yyls); +#endif + } + return 2; + } + yystacksize *= 2; + if (yystacksize > YYMAXDEPTH) + yystacksize = YYMAXDEPTH; +#ifndef YYSTACK_USE_ALLOCA + yyfree_stacks = 1; +#endif + yyss = (short *) YYSTACK_ALLOC (yystacksize * sizeof (*yyssp)); + __yy_memcpy ((char *)yyss, (char *)yyss1, + size * (unsigned int) sizeof (*yyssp)); + yyvs = (YYSTYPE *) YYSTACK_ALLOC (yystacksize * sizeof (*yyvsp)); + __yy_memcpy ((char *)yyvs, (char *)yyvs1, + size * (unsigned int) sizeof (*yyvsp)); +#ifdef YYLSP_NEEDED + yyls = (YYLTYPE *) YYSTACK_ALLOC (yystacksize * sizeof (*yylsp)); + __yy_memcpy ((char *)yyls, (char *)yyls1, + size * (unsigned int) sizeof (*yylsp)); +#endif +#endif /* no yyoverflow */ + + yyssp = yyss + size - 1; + yyvsp = yyvs + size - 1; +#ifdef YYLSP_NEEDED + yylsp = yyls + size - 1; +#endif + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Stack size increased to %d\n", yystacksize); +#endif + + if (yyssp >= yyss + yystacksize - 1) + YYABORT; + } + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Entering state %d\n", yystate); +#endif + + goto yybackup; + yybackup: + +/* Do appropriate processing given the current state. */ +/* Read a lookahead token if we need one and don't already have one. */ +/* yyresume: */ + + /* First try to decide what to do without reference to lookahead token. */ + + yyn = yypact[yystate]; + if (yyn == YYFLAG) + goto yydefault; + + /* Not known => get a lookahead token if don't already have one. */ + + /* yychar is either YYEMPTY or YYEOF + or a valid token in external form. */ + + if (yychar == YYEMPTY) + { +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Reading a token: "); +#endif + yychar = YYLEX; + } + + /* Convert token to internal form (in yychar1) for indexing tables with */ + + if (yychar <= 0) /* This means end of input. */ + { + yychar1 = 0; + yychar = YYEOF; /* Don't call YYLEX any more */ + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Now at end of input.\n"); +#endif + } + else + { + yychar1 = YYTRANSLATE(yychar); + +#if YYDEBUG != 0 + if (yydebug) + { + fprintf (stderr, "Next token is %d (%s", yychar, yytname[yychar1]); + /* Give the individual parser a way to print the precise meaning + of a token, for further debugging info. */ +#ifdef YYPRINT + YYPRINT (stderr, yychar, yylval); +#endif + fprintf (stderr, ")\n"); + } +#endif + } + + yyn += yychar1; + if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1) + goto yydefault; + + yyn = yytable[yyn]; + + /* yyn is what to do for this token type in this state. + Negative => reduce, -yyn is rule number. + Positive => shift, yyn is new state. + New state is final state => don't bother to shift, + just return success. + 0, or most negative number => error. */ + + if (yyn < 0) + { + if (yyn == YYFLAG) + goto yyerrlab; + yyn = -yyn; + goto yyreduce; + } + else if (yyn == 0) + goto yyerrlab; + + if (yyn == YYFINAL) + YYACCEPT; + + /* Shift the lookahead token. */ + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]); +#endif + + /* Discard the token being shifted unless it is eof. */ + if (yychar != YYEOF) + yychar = YYEMPTY; + + *++yyvsp = yylval; +#ifdef YYLSP_NEEDED + *++yylsp = yylloc; +#endif + + /* count tokens shifted since error; after three, turn off error status. */ + if (yyerrstatus) yyerrstatus--; + + yystate = yyn; + goto yynewstate; + +/* Do the default action for the current state. */ +yydefault: + + yyn = yydefact[yystate]; + if (yyn == 0) + goto yyerrlab; + +/* Do a reduction. yyn is the number of a rule to reduce with. */ +yyreduce: + yylen = yyr2[yyn]; + if (yylen > 0) + yyval = yyvsp[1-yylen]; /* implement default value of the action */ + +#if YYDEBUG != 0 + if (yydebug) + { + int i; + + fprintf (stderr, "Reducing via rule %d (line %d), ", + yyn, yyrline[yyn]); + + /* Print the symbols being reduced, and their result. */ + for (i = yyprhs[yyn]; yyrhs[i] > 0; i++) + fprintf (stderr, "%s ", yytname[yyrhs[i]]); + fprintf (stderr, " -> %s\n", yytname[yyr1[yyn]]); + } +#endif + + + switch (yyn) { + +case 1: +#line 32 "expr.y" +{ result = yyvsp[0]; ; + break;} +case 2: +#line 36 "expr.y" +{ yyval = yyvsp[0]; ; + break;} +case 3: +#line 37 "expr.y" +{ yyval = yyvsp[-1]; ; + break;} +case 4: +#line 38 "expr.y" +{ yyval = exprNodeDot(yyvsp[-2], yyvsp[0]); ; + break;} +case 5: +#line 39 "expr.y" +{ yyval = exprNodeArrow(yyvsp[-2], yyvsp[0]); ; + break;} +case 6: +#line 40 "expr.y" +{ yyval = exprNodeArray(yyvsp[-3], yyvsp[-1]); ; + break;} +case 7: +#line 43 "expr.y" +{ yyval = yyvsp[0]; ; + break;} +case 8: +#line 44 "expr.y" +{ yyval = exprNodeStar(yyvsp[0]); ; + break;} +case 9: +#line 45 "expr.y" +{ yyval = exprNodeAddr(yyvsp[0]); ; + break;} +case 10: +#line 46 "expr.y" +{ yyval = exprNodeSizeof(yyvsp[-1]); ; + break;} +case 11: +#line 50 "expr.y" +{ yyval = exprNodeNumber(); ; + break;} +case 12: +#line 54 "expr.y" +{yyval = exprNodeIdentifier(); ; + break;} +} + /* the action file gets copied in in place of this dollarsign */ +#line 543 "/usr/lib/bison.simple" + + yyvsp -= yylen; + yyssp -= yylen; +#ifdef YYLSP_NEEDED + yylsp -= yylen; +#endif + +#if YYDEBUG != 0 + if (yydebug) + { + short *ssp1 = yyss - 1; + fprintf (stderr, "state stack now"); + while (ssp1 != yyssp) + fprintf (stderr, " %d", *++ssp1); + fprintf (stderr, "\n"); + } +#endif + + *++yyvsp = yyval; + +#ifdef YYLSP_NEEDED + yylsp++; + if (yylen == 0) + { + yylsp->first_line = yylloc.first_line; + yylsp->first_column = yylloc.first_column; + yylsp->last_line = (yylsp-1)->last_line; + yylsp->last_column = (yylsp-1)->last_column; + yylsp->text = 0; + } + else + { + yylsp->last_line = (yylsp+yylen-1)->last_line; + yylsp->last_column = (yylsp+yylen-1)->last_column; + } +#endif + + /* Now "shift" the result of the reduction. + Determine what state that goes to, + based on the state we popped back to + and the rule number reduced by. */ + + yyn = yyr1[yyn]; + + yystate = yypgoto[yyn - YYNTBASE] + *yyssp; + if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp) + yystate = yytable[yystate]; + else + yystate = yydefgoto[yyn - YYNTBASE]; + + goto yynewstate; + +yyerrlab: /* here on detecting error */ + + if (! yyerrstatus) + /* If not already recovering from an error, report this error. */ + { + ++yynerrs; + +#ifdef YYERROR_VERBOSE + yyn = yypact[yystate]; + + if (yyn > YYFLAG && yyn < YYLAST) + { + int size = 0; + char *msg; + int x, count; + + count = 0; + /* Start X at -yyn if nec to avoid negative indexes in yycheck. */ + for (x = (yyn < 0 ? -yyn : 0); + x < (sizeof(yytname) / sizeof(char *)); x++) + if (yycheck[x + yyn] == x) + size += strlen(yytname[x]) + 15, count++; + msg = (char *) malloc(size + 15); + if (msg != 0) + { + strcpy(msg, "parse error"); + + if (count < 5) + { + count = 0; + for (x = (yyn < 0 ? -yyn : 0); + x < (sizeof(yytname) / sizeof(char *)); x++) + if (yycheck[x + yyn] == x) + { + strcat(msg, count == 0 ? ", expecting `" : " or `"); + strcat(msg, yytname[x]); + strcat(msg, "'"); + count++; + } + } + yyerror(msg); + free(msg); + } + else + yyerror ("parse error; also virtual memory exceeded"); + } + else +#endif /* YYERROR_VERBOSE */ + yyerror("parse error"); + } + + goto yyerrlab1; +yyerrlab1: /* here on error raised explicitly by an action */ + + if (yyerrstatus == 3) + { + /* if just tried and failed to reuse lookahead token after an error, discard it. */ + + /* return failure if at end of input */ + if (yychar == YYEOF) + YYABORT; + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]); +#endif + + yychar = YYEMPTY; + } + + /* Else will try to reuse lookahead token + after shifting the error token. */ + + yyerrstatus = 3; /* Each real token shifted decrements this */ + + goto yyerrhandle; + +yyerrdefault: /* current state does not do anything special for the error token. */ + +#if 0 + /* This is wrong; only states that explicitly want error tokens + should shift them. */ + yyn = yydefact[yystate]; /* If its default is to accept any token, ok. Otherwise pop it.*/ + if (yyn) goto yydefault; +#endif + +yyerrpop: /* pop the current state because it cannot handle the error token */ + + if (yyssp == yyss) YYABORT; + yyvsp--; + yystate = *--yyssp; +#ifdef YYLSP_NEEDED + yylsp--; +#endif + +#if YYDEBUG != 0 + if (yydebug) + { + short *ssp1 = yyss - 1; + fprintf (stderr, "Error: state stack now"); + while (ssp1 != yyssp) + fprintf (stderr, " %d", *++ssp1); + fprintf (stderr, "\n"); + } +#endif + +yyerrhandle: + + yyn = yypact[yystate]; + if (yyn == YYFLAG) + goto yyerrdefault; + + yyn += YYTERROR; + if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR) + goto yyerrdefault; + + yyn = yytable[yyn]; + if (yyn < 0) + { + if (yyn == YYFLAG) + goto yyerrpop; + yyn = -yyn; + goto yyreduce; + } + else if (yyn == 0) + goto yyerrpop; + + if (yyn == YYFINAL) + YYACCEPT; + +#if YYDEBUG != 0 + if (yydebug) + fprintf(stderr, "Shifting error token, "); +#endif + + *++yyvsp = yylval; +#ifdef YYLSP_NEEDED + *++yylsp = yylloc; +#endif + + yystate = yyn; + goto yynewstate; + + yyacceptlab: + /* YYACCEPT comes here. */ + if (yyfree_stacks) + { + free (yyss); + free (yyvs); +#ifdef YYLSP_NEEDED + free (yyls); +#endif + } + return 0; + + yyabortlab: + /* YYABORT comes here. */ + if (yyfree_stacks) + { + free (yyss); + free (yyvs); +#ifdef YYLSP_NEEDED + free (yyls); +#endif + } + return 1; +} +#line 57 "expr.y" + + +int yyerror(char *s) +{ + return 0; +} + +#ifndef SDL +extern FILE *yyin; +int main(int argc, char **argv) +{ + // yydebug = 1; + ++argv, --argc; + if(argc > 0) + yyin = fopen(argv[0], "r"); + else + yyin = stdin; + if(!yyparse()) + result->print(); +} +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/expr.cpp.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/expr.cpp.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,13 @@ +#ifndef YYSTYPE +#define YYSTYPE int +#endif +#define TOKEN_IDENTIFIER 257 +#define TOKEN_DOT 258 +#define TOKEN_STAR 259 +#define TOKEN_ARROW 260 +#define TOKEN_ADDR 261 +#define TOKEN_SIZEOF 262 +#define TOKEN_NUMBER 263 + + +extern YYSTYPE yylval; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/expr.l --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/expr.l Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,71 @@ +%{ +#include "expr.cpp.h" + +#ifndef __GNUC__ +#include +#define isatty _isatty +#endif + +char *exprString; +int exprCol; + +#define YY_INPUT(buf,result,max_size) \ + { \ + int c = *exprString++; \ + exprCol++;\ + result = (c == 0) ? YY_NULL : (buf[0] = c, 1); \ + } +%} + +%option nomain +%option noyywrap + +SIZEOF "sizeof" +ID [a-zA-Z_][a-zA-Z0-9_]* +NUM [0-9]+ +DOT "." +ARROW "->" +STAR "*" +ADDR "&" + +%% + +{SIZEOF} { + return TOKEN_SIZEOF; +} + +{ID} { + return TOKEN_IDENTIFIER; +} + +{NUM} { + return TOKEN_NUMBER; +} + +{DOT} { + return TOKEN_DOT; +} + +{ARROW} { + return TOKEN_ARROW; +} + +{ADDR} { + return TOKEN_ADDR; +} + +{STAR} { + return TOKEN_STAR; +} + +[ \t\n]+ + +. return *yytext; + +%% + +void exprCleanBuffer() +{ + yy_delete_buffer(yy_current_buffer); + yy_init = 1; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/expr.y --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/expr.y Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,77 @@ +%{ +namespace std { +#include +#include +#include +#include +} + +using namespace std; + +#include "System.h" +#include "elf.h" +#include "exprNode.h" + +extern int yyerror(char *); +extern int yylex(); +extern char *yytext; + + +//#define YYERROR_VERBOSE 1 +//#define YYDEBUG 1 + + Node *result = NULL; +%} + +%token TOKEN_IDENTIFIER TOKEN_DOT TOKEN_STAR TOKEN_ARROW TOKEN_ADDR +%token TOKEN_SIZEOF TOKEN_NUMBER +%left TOKEN_DOT TOKEN_ARROW '[' +%expect 6 +%% + +final: expression { result = $1; } +; + +expression: + simple_expression { $$ = $1; } | + '(' expression ')' { $$ = $2; } | + expression TOKEN_DOT ident { $$ = exprNodeDot($1, $3); } | + expression TOKEN_ARROW ident { $$ = exprNodeArrow($1, $3); } | + expression '[' number ']' { $$ = exprNodeArray($1, $3); } +; +simple_expression: + ident { $$ = $1; } | + TOKEN_STAR expression { $$ = exprNodeStar($2); } | + TOKEN_ADDR expression { $$ = exprNodeAddr($2); } | + TOKEN_SIZEOF '(' expression ')' { $$ = exprNodeSizeof($3); } +; + +number: + TOKEN_NUMBER { $$ = exprNodeNumber(); } +; + +ident: + TOKEN_IDENTIFIER {$$ = exprNodeIdentifier(); } +; + +%% + +int yyerror(char *s) +{ + return 0; +} + +#ifndef SDL +extern FILE *yyin; +int main(int argc, char **argv) +{ + // yydebug = 1; + ++argv, --argc; + if(argc > 0) + yyin = fopen(argv[0], "r"); + else + yyin = stdin; + if(!yyparse()) + result->print(); +} +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/exprNode.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/exprNode.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,411 @@ +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include +#include +#include + +#include "Port.h" +#include "gba/GBAGlobals.h" +#include "gba/elf.h" +#include "exprNode.h" + +extern char *yytext; + +#define debuggerReadMemory(addr) \ + READ32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) + +void *exprNodeCleanUpList[100]; +int exprNodeCleanUpCount = 0; +Type exprNodeType = { 0, TYPE_base, "int", DW_ATE_signed, 4, 0, {0}, 0 }; + +void exprNodeClean(void *m) +{ + exprNodeCleanUpList[exprNodeCleanUpCount++] = m; +} + +void exprNodeCleanUp() +{ + for(int i = 0; i < exprNodeCleanUpCount; i++) { + free(exprNodeCleanUpList[i]); + } + exprNodeCleanUpCount = 0; +} + +Node *exprNodeIdentifier() +{ + Node *n = (Node *)calloc(1, sizeof(Node)); + n->name = strdup(yytext); + + exprNodeClean(n->name); + exprNodeClean(n); + + n->print = exprNodeIdentifierPrint; + n->resolve = exprNodeIdentifierResolve; + return n; +} + +bool exprNodeIdentifierResolve(Node *n, Function *f, CompileUnit *u) +{ + Object *o; + if(elfGetObject(n->name, f, u, &o)) { + n->type = o->type; + n->location = elfDecodeLocation(f, o->location, &n->locType); + return true; + } else { + printf("Object %s not found\n", n->name); + } + return false; +} + +void exprNodeIdentifierPrint(Node *n) +{ + printf("%s", n->name); +} + +Node *exprNodeNumber() +{ + Node *n = (Node *)calloc(1, sizeof(Node)); + + exprNodeClean(n); + n->location = atoi(yytext); + n->type = &exprNodeType; + n->locType = LOCATION_value; + n->print = exprNodeNumberPrint; + n->resolve = exprNodeNumberResolve; + return n; +} + +bool exprNodeNumberResolve(Node *n, Function *f, CompileUnit *u) +{ + return true; +} + +void exprNodeNumberPrint(Node *n) +{ + printf("%d", n->location); +} + +Node *exprNodeStar(Node *exp) +{ + Node *n = (Node *)calloc(1, sizeof(Node)); + exprNodeClean(n); + + n->expression = exp; + + n->print = exprNodeStarPrint; + n->resolve = exprNodeStarResolve; + return n; +} + +bool exprNodeStarResolve(Node *n, Function *f, CompileUnit *u) +{ + if(n->expression->resolve(n->expression, f, u)) { + if(n->expression->type->type == TYPE_pointer) { + n->location = n->expression->location; + if(n->expression->locType == LOCATION_memory) { + n->location = debuggerReadMemory(n->location); + } else if(n->expression->locType == LOCATION_register) { + n->location = reg[n->expression->location].I; + } else { + n->location = n->expression->location; + } + n->type = n->expression->type->pointer; + n->locType = LOCATION_memory; + return true; + } else { + printf("Object is not of pointer type\n"); + } + } + return false; +} + +void exprNodeStarPrint(Node *n) +{ + printf("*"); + n->expression->print(n->expression); +} + +Node *exprNodeDot(Node *exp, Node *ident) +{ + Node *n = (Node *)calloc(1, sizeof(Node)); + exprNodeClean(n); + + n->expression = exp; + n->name = ident->name; + + n->print = exprNodeDotPrint; + n->resolve = exprNodeDotResolve; + return n; +} + +bool exprNodeDotResolve(Node *n, Function *f, CompileUnit *u) +{ + if(n->expression->resolve(n->expression, f, u)) { + TypeEnum tt = n->expression->type->type; + + if(tt == TYPE_struct || + tt == TYPE_union) { + u32 loc = n->expression->location; + Type *t = n->expression->type; + int count = t->structure->memberCount; + int i = 0; + while(i < count) { + Member *m = &t->structure->members[i]; + if(strcmp(m->name, n->name) == 0) { + // found member + n->type = m->type; + if(tt == TYPE_struct) { + n->location = elfDecodeLocation(f, m->location, &n->locType, + loc); + n->objLocation = loc; + } else { + n->location = loc; + n->locType = n->expression->locType; + n->objLocation = loc; + } + n->member = m; + return true; + } + i++; + } + printf("Member %s not found\n", n->name); + } else { + printf("Object is not of structure type\n"); + } + } + return false; +} + +void exprNodeDotPrint(Node *n) +{ + n->expression->print(n->expression); + printf(".%s", n->name); +} + +Node *exprNodeArrow(Node *exp, Node *ident) +{ + Node *n = (Node *)calloc(1, sizeof(Node)); + exprNodeClean(n); + + n->expression = exp; + n->name = ident->name; + + n->print = exprNodeArrowPrint; + n->resolve = exprNodeArrowResolve; + return n; +} + +bool exprNodeArrowResolve(Node *n, Function *f, CompileUnit *u) +{ + if(n->expression->resolve(n->expression, f, u)) { + TypeEnum tt = n->expression->type->type; + if(tt != TYPE_pointer) { + printf("Object not of pointer type\n"); + return false; + } + tt = n->expression->type->pointer->type; + + if(tt == TYPE_struct || + tt == TYPE_union) { + u32 loc = debuggerReadMemory(n->expression->location); + Type *t = n->expression->type->pointer; + int count = t->structure->memberCount; + int i = 0; + while(i < count) { + Member *m = &t->structure->members[i]; + if(strcmp(m->name, n->name) == 0) { + // found member + n->type = m->type; + if(tt == TYPE_struct) { + n->location = elfDecodeLocation(f, m->location, &n->locType, + loc); + n->objLocation = loc; + } else { + n->location = loc; + n->objLocation = loc; + } + n->locType = LOCATION_memory; + n->member = m; + return true; + } + i++; + } + printf("Member %s not found\n", n->name); + } else { + printf("Object is not of structure type\n"); + } + } + return false; +} + +void exprNodeArrowPrint(Node *n) +{ + n->expression->print(n->expression); + printf("->%s", n->name); +} + +Node *exprNodeAddr(Node *exp) +{ + Node *n = (Node *)calloc(1, sizeof(Node)); + exprNodeClean(n); + + n->expression = exp; + + n->print = exprNodeAddrPrint; + n->resolve = exprNodeAddrResolve; + return n; +} + +bool exprNodeAddrResolve(Node *n, Function *f, CompileUnit *u) +{ + if(n->expression->resolve(n->expression, f, u)) { + if(n->expression->locType == LOCATION_memory) { + n->location = n->expression->location; + n->locType = LOCATION_value; + n->type = &exprNodeType; + } else if(n->expression->locType == LOCATION_register) { + printf("Value is in register %d\n", n->expression->location); + } else { + printf("Direct value is %d\n", n->location); + } + return true; + } + return false; +} + +void exprNodeAddrPrint(Node *n) +{ + printf("*"); + n->expression->print(n->expression); +} + +Node *exprNodeSizeof(Node *exp) +{ + Node *n = (Node *)calloc(1, sizeof(Node)); + exprNodeClean(n); + + n->expression = exp; + + n->print = exprNodeSizeofPrint; + n->resolve = exprNodeSizeofResolve; + return n; +} + +bool exprNodeSizeofResolve(Node *n, Function *f, CompileUnit *u) +{ + if(n->expression->resolve(n->expression, f, u)) { + n->location = n->expression->type->size; + n->locType = LOCATION_value; + n->type = &exprNodeType; + return true; + } + return false; +} + +void exprNodeSizeofPrint(Node *n) +{ + printf("sizeof("); + n->expression->print(n->expression); + printf(")"); +} + +Node *exprNodeArray(Node *exp, Node *number) +{ + Node *n = (Node *)calloc(1, sizeof(Node)); + exprNodeClean(n); + + n->expression = exp; + n->value = number->location; + + n->print = exprNodeArrayPrint; + n->resolve = exprNodeArrayResolve; + return n; +} + +int exprNodeGetSize(Array *a, int index) +{ + index++; + if(index == a->maxBounds) { + return a->type->size; + } else { + int size = a->bounds[a->maxBounds-1] * a->type->size; + + for(int i = index; i < a->maxBounds-1; i++) { + size *= a->bounds[i]; + } + return size; + } +} + +bool exprNodeArrayResolve(Node *n, Function *f, CompileUnit *u) +{ + if(n->expression->resolve(n->expression, f, u)) { + TypeEnum tt = n->expression->type->type; + if(tt != TYPE_array && + tt != TYPE_pointer) { + printf("Object not of array or pointer type\n"); + return false; + } + + if(tt == TYPE_array) { + Array *a = n->expression->type->array; + + u32 loc = n->expression->location; + Type *t = a->type; + if(a->maxBounds > 1) { + int index = n->expression->index; + + if(index == a->maxBounds) { + printf("Too many indices for array\n"); + return false; + } + + if((index+1) < a->maxBounds) { + n->type = n->expression->type; + n->index = index+1; + n->locType = LOCATION_memory; + n->location = n->expression->location + + n->value * exprNodeGetSize(a, index); + return true; + } + } + n->type = t; + n->location = loc + n->value * t->size; + n->locType = LOCATION_memory; + } else { + Type *t = n->expression->type->pointer; + u32 loc = n->expression->location; + if(n->expression->locType == LOCATION_register) + loc = reg[loc].I; + else + loc = debuggerReadMemory(loc); + n->type = t; + n->location = loc + n->value * t->size; + n->locType = LOCATION_memory; + } + return true; + } + return false; +} + +void exprNodeArrayPrint(Node *n) +{ + n->expression->print(n->expression); + printf("[%d]", n->value); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/exprNode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/exprNode.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,68 @@ +// -*- C++ -*- +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. +// Copyright (C) 1999-2003 Forgotten +// Copyright (C) 2004 Forgotten and the VBA development team + +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2, or(at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +struct Node { + Type *type; + u32 location; + u32 objLocation; + LocationType locType; + int value; + int index; + char *name; + Node *expression; + Member *member; + void (*print)(Node *); + bool (*resolve)(Node *, Function *f, CompileUnit *u); +}; + +extern void exprNodeCleanUp(); + +extern Node *exprNodeIdentifier(); +extern void exprNodeIdentifierPrint(Node *); +extern bool exprNodeIdentifierResolve(Node *, Function *, CompileUnit *); + +extern Node *exprNodeNumber(); +extern void exprNodeNumberPrint(Node *); +extern bool exprNodeNumberResolve(Node *, Function *, CompileUnit *); + +extern Node *exprNodeStar(Node *); +extern void exprNodeStarPrint(Node *); +extern bool exprNodeStarResolve(Node *, Function *, CompileUnit *); + +extern Node *exprNodeDot(Node *, Node *); +extern void exprNodeDotPrint(Node *); +extern bool exprNodeDotResolve(Node *, Function *, CompileUnit *); + +extern Node *exprNodeArrow(Node *, Node *); +extern void exprNodeArrowPrint(Node *); +extern bool exprNodeArrowResolve(Node *, Function *, CompileUnit *); + +extern Node *exprNodeAddr(Node *); +extern void exprNodeAddrPrint(Node *); +extern bool exprNodeAddrResolve(Node *, Function *, CompileUnit *); + +extern Node *exprNodeSizeof(Node *); +extern void exprNodeSizeofPrint(Node *); +extern bool exprNodeSizeofResolve(Node *, Function *, CompileUnit *); + +extern Node *exprNodeArray(Node *, Node *); +extern void exprNodeArrayPrint(Node *); +extern bool exprNodeArrayResolve(Node *, Function *, CompileUnit *); + +#define YYSTYPE struct Node * diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/getopt.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/getopt.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1060 @@ +/* Getopt for GNU. + NOTE: getopt is now part of the C library, so if you don't know what + "Keep this file name-space clean" means, talk to drepper@gnu.org + before changing it! + + Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98 + Free Software Foundation, Inc. + + NOTE: This source is derived from an old version taken from the GNU C + Library (glibc). + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +/* This tells Alpha OSF/1 not to define a getopt prototype in . + Ditto for AIX 3.2 and . */ +#ifndef _NO_PROTO +# define _NO_PROTO +#endif + +#ifdef HAVE_CONFIG_H +# include +#endif + +#if !defined __STDC__ || !__STDC__ +/* This is a separate conditional since some stdc systems + reject `defined (const)'. */ +# ifndef const +# define const +# endif +#endif + +#include + +/* Comment out all this code if we are using the GNU C Library, and are not + actually compiling the library itself. This code is part of the GNU C + Library, but also included in many other GNU distributions. Compiling + and linking in this code is a waste when using the GNU C library + (especially if it is a shared library). Rather than having every GNU + program understand `configure --with-gnu-libc' and omit the object files, + it is simpler to just do this in the source for each such file. */ + +#define GETOPT_INTERFACE_VERSION 2 +#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 +# include +# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION +# define ELIDE_CODE +# endif +#endif + +#ifndef ELIDE_CODE + + +/* This needs to come after some library #include + to get __GNU_LIBRARY__ defined. */ +#ifdef __GNU_LIBRARY__ +/* Don't include stdlib.h for non-GNU C libraries because some of them + contain conflicting prototypes for getopt. */ +# include +# include +#endif /* GNU C library. */ + +#ifdef VMS +# include +# if HAVE_STRING_H - 0 +# include +# endif +#endif + +#ifndef _ +/* This is for other GNU distributions with internationalized messages. + When compiling libc, the _ macro is predefined. */ +# ifdef HAVE_LIBINTL_H +# include +# define _(msgid) gettext (msgid) +# else +# define _(msgid) (msgid) +# endif +#endif + +#ifdef _MSC_VER +#include +#endif + +/* This version of `getopt' appears to the caller like standard Unix `getopt' + but it behaves differently for the user, since it allows the user + to intersperse the options with the other arguments. + + As `getopt' works, it permutes the elements of ARGV so that, + when it is done, all the options precede everything else. Thus + all application programs are extended to handle flexible argument order. + + Setting the environment variable POSIXLY_CORRECT disables permutation. + Then the behavior is completely standard. + + GNU application programs can use a third alternative mode in which + they can distinguish the relative order of options and other arguments. */ + +#include "getopt.h" + +/* For communication from `getopt' to the caller. + When `getopt' finds an option that takes an argument, + the argument value is returned here. + Also, when `ordering' is RETURN_IN_ORDER, + each non-option ARGV-element is returned here. */ + +char *optarg = NULL; + +/* Index in ARGV of the next element to be scanned. + This is used for communication to and from the caller + and for communication between successive calls to `getopt'. + + On entry to `getopt', zero means this is the first call; initialize. + + When `getopt' returns -1, this is the index of the first of the + non-option elements that the caller should itself scan. + + Otherwise, `optind' communicates from one call to the next + how much of ARGV has been scanned so far. */ + +/* 1003.2 says this must be 1 before any call. */ +int optind = 1; + +/* Formerly, initialization of getopt depended on optind==0, which + causes problems with re-calling getopt as programs generally don't + know that. */ + +int __getopt_initialized = 0; + +/* The next char to be scanned in the option-element + in which the last option character we returned was found. + This allows us to pick up the scan where we left off. + + If this is zero, or a null string, it means resume the scan + by advancing to the next ARGV-element. */ + +static char *nextchar; + +/* Callers store zero here to inhibit the error message + for unrecognized options. */ + +int opterr = 1; + +/* Set to an option character which was unrecognized. + This must be initialized on some systems to avoid linking in the + system's own getopt implementation. */ + +int optopt = '?'; + +/* Describe how to deal with options that follow non-option ARGV-elements. + + If the caller did not specify anything, + the default is REQUIRE_ORDER if the environment variable + POSIXLY_CORRECT is defined, PERMUTE otherwise. + + REQUIRE_ORDER means don't recognize them as options; + stop option processing when the first non-option is seen. + This is what Unix does. + This mode of operation is selected by either setting the environment + variable POSIXLY_CORRECT, or using `+' as the first character + of the list of option characters. + + PERMUTE is the default. We permute the contents of ARGV as we scan, + so that eventually all the non-options are at the end. This allows options + to be given in any order, even with programs that were not written to + expect this. + + RETURN_IN_ORDER is an option available to programs that were written + to expect options and other ARGV-elements in any order and that care about + the ordering of the two. We describe each non-option ARGV-element + as if it were the argument of an option with character code 1. + Using `-' as the first character of the list of option characters + selects this mode of operation. + + The special argument `--' forces an end of option-scanning regardless + of the value of `ordering'. In the case of RETURN_IN_ORDER, only + `--' can cause `getopt' to return -1 with `optind' != ARGC. */ + +static enum +{ + REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER +} ordering; + +/* Value of POSIXLY_CORRECT environment variable. */ +static char *posixly_correct; + +#ifdef __GNU_LIBRARY__ +/* We want to avoid inclusion of string.h with non-GNU libraries + because there are many ways it can cause trouble. + On some systems, it contains special magic macros that don't work + in GCC. */ +# include +# define my_index strchr +#else + +# if HAVE_STRING_H +# include +# else +# if HAVE_STRINGS_H +# include +# endif +# endif + +/* Avoid depending on library functions or files + whose names are inconsistent. */ + +#ifndef getenv +extern char *getenv (); +#endif + +static char * +my_index (str, chr) + const char *str; + int chr; +{ + while (*str) + { + if (*str == chr) + return (char *) str; + str++; + } + return 0; +} + +/* If using GCC, we can safely declare strlen this way. + If not using GCC, it is ok not to declare it. */ +#ifdef __GNUC__ +/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. + That was relevant to code that was here before. */ +# if (!defined __STDC__ || !__STDC__) && !defined strlen +/* gcc with -traditional declares the built-in strlen to return int, + and has done so at least since version 2.4.5. -- rms. */ +extern int strlen (const char *); +# endif /* not __STDC__ */ +#endif /* __GNUC__ */ + +#endif /* not __GNU_LIBRARY__ */ + +/* Handle permutation of arguments. */ + +/* Describe the part of ARGV that contains non-options that have + been skipped. `first_nonopt' is the index in ARGV of the first of them; + `last_nonopt' is the index after the last of them. */ + +static int first_nonopt; +static int last_nonopt; + +#ifdef _LIBC +/* Bash 2.0 gives us an environment variable containing flags + indicating ARGV elements that should not be considered arguments. */ + +/* Defined in getopt_init.c */ +extern char *__getopt_nonoption_flags; + +static int nonoption_flags_max_len; +static int nonoption_flags_len; + +static int original_argc; +static char *const *original_argv; + +/* Make sure the environment variable bash 2.0 puts in the environment + is valid for the getopt call we must make sure that the ARGV passed + to getopt is that one passed to the process. */ +static void +__attribute__ ((unused)) +store_args_and_env (int argc, char *const *argv) +{ + /* XXX This is no good solution. We should rather copy the args so + that we can compare them later. But we must not use malloc(3). */ + original_argc = argc; + original_argv = argv; +} +# ifdef text_set_element +text_set_element (__libc_subinit, store_args_and_env); +# endif /* text_set_element */ + +# define SWAP_FLAGS(ch1, ch2) \ + if (nonoption_flags_len > 0) \ + { \ + char __tmp = __getopt_nonoption_flags[ch1]; \ + __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ + __getopt_nonoption_flags[ch2] = __tmp; \ + } +#else /* !_LIBC */ +# define SWAP_FLAGS(ch1, ch2) +#endif /* _LIBC */ + +/* Exchange two adjacent subsequences of ARGV. + One subsequence is elements [first_nonopt,last_nonopt) + which contains all the non-options that have been skipped so far. + The other is elements [last_nonopt,optind), which contains all + the options processed since those non-options were skipped. + + `first_nonopt' and `last_nonopt' are relocated so that they describe + the new indices of the non-options in ARGV after they are moved. */ + +#if defined __STDC__ && __STDC__ +static void exchange (char **); +#endif + +static void +exchange (argv) + char **argv; +{ + int bottom = first_nonopt; + int middle = last_nonopt; + int top = optind; + char *tem; + + /* Exchange the shorter segment with the far end of the longer segment. + That puts the shorter segment into the right place. + It leaves the longer segment in the right place overall, + but it consists of two parts that need to be swapped next. */ + +#ifdef _LIBC + /* First make sure the handling of the `__getopt_nonoption_flags' + string can work normally. Our top argument must be in the range + of the string. */ + if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) + { + /* We must extend the array. The user plays games with us and + presents new arguments. */ + char *new_str = malloc (top + 1); + if (new_str == NULL) + nonoption_flags_len = nonoption_flags_max_len = 0; + else + { + memset (__mempcpy (new_str, __getopt_nonoption_flags, + nonoption_flags_max_len), + '\0', top + 1 - nonoption_flags_max_len); + nonoption_flags_max_len = top + 1; + __getopt_nonoption_flags = new_str; + } + } +#endif + + while (top > middle && middle > bottom) + { + if (top - middle > middle - bottom) + { + /* Bottom segment is the short one. */ + int len = middle - bottom; + register int i; + + /* Swap it with the top part of the top segment. */ + for (i = 0; i < len; i++) + { + tem = argv[bottom + i]; + argv[bottom + i] = argv[top - (middle - bottom) + i]; + argv[top - (middle - bottom) + i] = tem; + SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); + } + /* Exclude the moved bottom segment from further swapping. */ + top -= len; + } + else + { + /* Top segment is the short one. */ + int len = top - middle; + register int i; + + /* Swap it with the bottom part of the bottom segment. */ + for (i = 0; i < len; i++) + { + tem = argv[bottom + i]; + argv[bottom + i] = argv[middle + i]; + argv[middle + i] = tem; + SWAP_FLAGS (bottom + i, middle + i); + } + /* Exclude the moved top segment from further swapping. */ + bottom += len; + } + } + + /* Update records for the slots the non-options now occupy. */ + + first_nonopt += (optind - last_nonopt); + last_nonopt = optind; +} + +/* Initialize the internal data when the first call is made. */ + +#if defined __STDC__ && __STDC__ +static const char *_getopt_initialize (int, char *const *, const char *); +#endif +static const char * +_getopt_initialize (argc, argv, optstring) + int argc; + char *const *argv; + const char *optstring; +{ + /* Start processing options with ARGV-element 1 (since ARGV-element 0 + is the program name); the sequence of previously skipped + non-option ARGV-elements is empty. */ + + first_nonopt = last_nonopt = optind; + + nextchar = NULL; + + posixly_correct = getenv ("POSIXLY_CORRECT"); + + /* Determine how to handle the ordering of options and nonoptions. */ + + if (optstring[0] == '-') + { + ordering = RETURN_IN_ORDER; + ++optstring; + } + else if (optstring[0] == '+') + { + ordering = REQUIRE_ORDER; + ++optstring; + } + else if (posixly_correct != NULL) + ordering = REQUIRE_ORDER; + else + ordering = PERMUTE; + +#ifdef _LIBC + if (posixly_correct == NULL + && argc == original_argc && argv == original_argv) + { + if (nonoption_flags_max_len == 0) + { + if (__getopt_nonoption_flags == NULL + || __getopt_nonoption_flags[0] == '\0') + nonoption_flags_max_len = -1; + else + { + const char *orig_str = __getopt_nonoption_flags; + int len = nonoption_flags_max_len = strlen (orig_str); + if (nonoption_flags_max_len < argc) + nonoption_flags_max_len = argc; + __getopt_nonoption_flags = + (char *) malloc (nonoption_flags_max_len); + if (__getopt_nonoption_flags == NULL) + nonoption_flags_max_len = -1; + else + memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), + '\0', nonoption_flags_max_len - len); + } + } + nonoption_flags_len = nonoption_flags_max_len; + } + else + nonoption_flags_len = 0; +#endif + + return optstring; +} + +/* Scan elements of ARGV (whose length is ARGC) for option characters + given in OPTSTRING. + + If an element of ARGV starts with '-', and is not exactly "-" or "--", + then it is an option element. The characters of this element + (aside from the initial '-') are option characters. If `getopt' + is called repeatedly, it returns successively each of the option characters + from each of the option elements. + + If `getopt' finds another option character, it returns that character, + updating `optind' and `nextchar' so that the next call to `getopt' can + resume the scan with the following option character or ARGV-element. + + If there are no more option characters, `getopt' returns -1. + Then `optind' is the index in ARGV of the first ARGV-element + that is not an option. (The ARGV-elements have been permuted + so that those that are not options now come last.) + + OPTSTRING is a string containing the legitimate option characters. + If an option character is seen that is not listed in OPTSTRING, + return '?' after printing an error message. If you set `opterr' to + zero, the error message is suppressed but we still return '?'. + + If a char in OPTSTRING is followed by a colon, that means it wants an arg, + so the following text in the same ARGV-element, or the text of the following + ARGV-element, is returned in `optarg'. Two colons mean an option that + wants an optional arg; if there is text in the current ARGV-element, + it is returned in `optarg', otherwise `optarg' is set to zero. + + If OPTSTRING starts with `-' or `+', it requests different methods of + handling the non-option ARGV-elements. + See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. + + Long-named options begin with `--' instead of `-'. + Their names may be abbreviated as long as the abbreviation is unique + or is an exact match for some defined option. If they have an + argument, it follows the option name in the same ARGV-element, separated + from the option name by a `=', or else the in next ARGV-element. + When `getopt' finds a long-named option, it returns 0 if that option's + `flag' field is nonzero, the value of the option's `val' field + if the `flag' field is zero. + + The elements of ARGV aren't really const, because we permute them. + But we pretend they're const in the prototype to be compatible + with other systems. + + LONGOPTS is a vector of `struct option' terminated by an + element containing a name which is zero. + + LONGIND returns the index in LONGOPT of the long-named option found. + It is only valid when a long-named option has been found by the most + recent call. + + If LONG_ONLY is nonzero, '-' as well as '--' can introduce + long-named options. */ + +int +_getopt_internal (argc, argv, optstring, longopts, longind, long_only) + int argc; + char *const *argv; + const char *optstring; + const struct option *longopts; + int *longind; + int long_only; +{ + optarg = NULL; + + if (optind == 0 || !__getopt_initialized) + { + if (optind == 0) + optind = 1; /* Don't scan ARGV[0], the program name. */ + optstring = _getopt_initialize (argc, argv, optstring); + __getopt_initialized = 1; + } + + /* Test whether ARGV[optind] points to a non-option argument. + Either it does not have option syntax, or there is an environment flag + from the shell indicating it is not an option. The later information + is only used when the used in the GNU libc. */ +#ifdef _LIBC +# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ + || (optind < nonoption_flags_len \ + && __getopt_nonoption_flags[optind] == '1')) +#else +# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') +#endif + + if (nextchar == NULL || *nextchar == '\0') + { + /* Advance to the next ARGV-element. */ + + /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been + moved back by the user (who may also have changed the arguments). */ + if (last_nonopt > optind) + last_nonopt = optind; + if (first_nonopt > optind) + first_nonopt = optind; + + if (ordering == PERMUTE) + { + /* If we have just processed some options following some non-options, + exchange them so that the options come first. */ + + if (first_nonopt != last_nonopt && last_nonopt != optind) + exchange ((char **) argv); + else if (last_nonopt != optind) + first_nonopt = optind; + + /* Skip any additional non-options + and extend the range of non-options previously skipped. */ + + while (optind < argc && NONOPTION_P) + optind++; + last_nonopt = optind; + } + + /* The special ARGV-element `--' means premature end of options. + Skip it like a null option, + then exchange with previous non-options as if it were an option, + then skip everything else like a non-option. */ + + if (optind != argc && !strcmp (argv[optind], "--")) + { + optind++; + + if (first_nonopt != last_nonopt && last_nonopt != optind) + exchange ((char **) argv); + else if (first_nonopt == last_nonopt) + first_nonopt = optind; + last_nonopt = argc; + + optind = argc; + } + + /* If we have done all the ARGV-elements, stop the scan + and back over any non-options that we skipped and permuted. */ + + if (optind == argc) + { + /* Set the next-arg-index to point at the non-options + that we previously skipped, so the caller will digest them. */ + if (first_nonopt != last_nonopt) + optind = first_nonopt; + return -1; + } + + /* If we have come to a non-option and did not permute it, + either stop the scan or describe it to the caller and pass it by. */ + + if (NONOPTION_P) + { + if (ordering == REQUIRE_ORDER) + return -1; + optarg = argv[optind++]; + return 1; + } + + /* We have found another option-ARGV-element. + Skip the initial punctuation. */ + + nextchar = (argv[optind] + 1 + + (longopts != NULL && argv[optind][1] == '-')); + } + + /* Decode the current option-ARGV-element. */ + + /* Check whether the ARGV-element is a long option. + + If long_only and the ARGV-element has the form "-f", where f is + a valid short option, don't consider it an abbreviated form of + a long option that starts with f. Otherwise there would be no + way to give the -f short option. + + On the other hand, if there's a long option "fubar" and + the ARGV-element is "-fu", do consider that an abbreviation of + the long option, just like "--fu", and not "-f" with arg "u". + + This distinction seems to be the most useful approach. */ + + if (longopts != NULL + && (argv[optind][1] == '-' + || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) + { + char *nameend; + const struct option *p; + const struct option *pfound = NULL; + int exact = 0; + int ambig = 0; + int indfound = -1; + int option_index; + + for (nameend = nextchar; *nameend && *nameend != '='; nameend++) + /* Do nothing. */ ; + + /* Test all long options for either exact match + or abbreviated matches. */ + for (p = longopts, option_index = 0; p->name; p++, option_index++) + if (!strncmp (p->name, nextchar, nameend - nextchar)) + { + if ((unsigned int) (nameend - nextchar) + == (unsigned int) strlen (p->name)) + { + /* Exact match found. */ + pfound = p; + indfound = option_index; + exact = 1; + break; + } + else if (pfound == NULL) + { + /* First nonexact match found. */ + pfound = p; + indfound = option_index; + } + else + /* Second or later nonexact match found. */ + ambig = 1; + } + + if (ambig && !exact) + { + if (opterr) + fprintf (stderr, _("%s: option `%s' is ambiguous\n"), + argv[0], argv[optind]); + nextchar += strlen (nextchar); + optind++; + optopt = 0; + return '?'; + } + + if (pfound != NULL) + { + option_index = indfound; + optind++; + if (*nameend) + { + /* Don't test has_arg with >, because some C compilers don't + allow it to be used on enums. */ + if (pfound->has_arg) + optarg = nameend + 1; + else + { + if (opterr) + { + if (argv[optind - 1][1] == '-') + /* --option */ + fprintf (stderr, + _("%s: option `--%s' doesn't allow an argument\n"), + argv[0], pfound->name); + else + /* +option or -option */ + fprintf (stderr, + _("%s: option `%c%s' doesn't allow an argument\n"), + argv[0], argv[optind - 1][0], pfound->name); + + nextchar += strlen (nextchar); + + optopt = pfound->val; + return '?'; + } + } + } + else if (pfound->has_arg == 1) + { + if (optind < argc) + optarg = argv[optind++]; + else + { + if (opterr) + fprintf (stderr, + _("%s: option `%s' requires an argument\n"), + argv[0], argv[optind - 1]); + nextchar += strlen (nextchar); + optopt = pfound->val; + return optstring[0] == ':' ? ':' : '?'; + } + } + nextchar += strlen (nextchar); + if (longind != NULL) + *longind = option_index; + if (pfound->flag) + { + *(pfound->flag) = pfound->val; + return 0; + } + return pfound->val; + } + + /* Can't find it as a long option. If this is not getopt_long_only, + or the option starts with '--' or is not a valid short + option, then it's an error. + Otherwise interpret it as a short option. */ + if (!long_only || argv[optind][1] == '-' + || my_index (optstring, *nextchar) == NULL) + { + if (opterr) + { + if (argv[optind][1] == '-') + /* --option */ + fprintf (stderr, _("%s: unrecognized option `--%s'\n"), + argv[0], nextchar); + else + /* +option or -option */ + fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), + argv[0], argv[optind][0], nextchar); + } + nextchar = (char *) ""; + optind++; + optopt = 0; + return '?'; + } + } + + /* Look at and handle the next short option-character. */ + + { + char c = *nextchar++; + char *temp = my_index (optstring, c); + + /* Increment `optind' when we start to process its last character. */ + if (*nextchar == '\0') + ++optind; + + if (temp == NULL || c == ':') + { + if (opterr) + { + if (posixly_correct) + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, _("%s: illegal option -- %c\n"), + argv[0], c); + else + fprintf (stderr, _("%s: invalid option -- %c\n"), + argv[0], c); + } + optopt = c; + return '?'; + } + /* Convenience. Treat POSIX -W foo same as long option --foo */ + if (temp[0] == 'W' && temp[1] == ';') + { + char *nameend; + const struct option *p; + const struct option *pfound = NULL; + int exact = 0; + int ambig = 0; + int indfound = 0; + int option_index; + + /* This is an option that requires an argument. */ + if (*nextchar != '\0') + { + optarg = nextchar; + /* If we end this ARGV-element by taking the rest as an arg, + we must advance to the next element now. */ + optind++; + } + else if (optind == argc) + { + if (opterr) + { + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, _("%s: option requires an argument -- %c\n"), + argv[0], c); + } + optopt = c; + if (optstring[0] == ':') + c = ':'; + else + c = '?'; + return c; + } + else + /* We already incremented `optind' once; + increment it again when taking next ARGV-elt as argument. */ + optarg = argv[optind++]; + + /* optarg is now the argument, see if it's in the + table of longopts. */ + + for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) + /* Do nothing. */ ; + + /* Test all long options for either exact match + or abbreviated matches. */ + for (p = longopts, option_index = 0; p->name; p++, option_index++) + if (!strncmp (p->name, nextchar, nameend - nextchar)) + { + if ((unsigned int) (nameend - nextchar) == strlen (p->name)) + { + /* Exact match found. */ + pfound = p; + indfound = option_index; + exact = 1; + break; + } + else if (pfound == NULL) + { + /* First nonexact match found. */ + pfound = p; + indfound = option_index; + } + else + /* Second or later nonexact match found. */ + ambig = 1; + } + if (ambig && !exact) + { + if (opterr) + fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), + argv[0], argv[optind]); + nextchar += strlen (nextchar); + optind++; + return '?'; + } + if (pfound != NULL) + { + option_index = indfound; + if (*nameend) + { + /* Don't test has_arg with >, because some C compilers don't + allow it to be used on enums. */ + if (pfound->has_arg) + optarg = nameend + 1; + else + { + if (opterr) + fprintf (stderr, _("\ +%s: option `-W %s' doesn't allow an argument\n"), + argv[0], pfound->name); + + nextchar += strlen (nextchar); + return '?'; + } + } + else if (pfound->has_arg == 1) + { + if (optind < argc) + optarg = argv[optind++]; + else + { + if (opterr) + fprintf (stderr, + _("%s: option `%s' requires an argument\n"), + argv[0], argv[optind - 1]); + nextchar += strlen (nextchar); + return optstring[0] == ':' ? ':' : '?'; + } + } + nextchar += strlen (nextchar); + if (longind != NULL) + *longind = option_index; + if (pfound->flag) + { + *(pfound->flag) = pfound->val; + return 0; + } + return pfound->val; + } + nextchar = NULL; + return 'W'; /* Let the application handle it. */ + } + if (temp[1] == ':') + { + if (temp[2] == ':') + { + /* This is an option that accepts an argument optionally. */ + if (*nextchar != '\0') + { + optarg = nextchar; + optind++; + } + else + optarg = NULL; + nextchar = NULL; + } + else + { + /* This is an option that requires an argument. */ + if (*nextchar != '\0') + { + optarg = nextchar; + /* If we end this ARGV-element by taking the rest as an arg, + we must advance to the next element now. */ + optind++; + } + else if (optind == argc) + { + if (opterr) + { + /* 1003.2 specifies the format of this message. */ + fprintf (stderr, + _("%s: option requires an argument -- %c\n"), + argv[0], c); + } + optopt = c; + if (optstring[0] == ':') + c = ':'; + else + c = '?'; + } + else + /* We already incremented `optind' once; + increment it again when taking next ARGV-elt as argument. */ + optarg = argv[optind++]; + nextchar = NULL; + } + } + return c; + } +} + +int +getopt (argc, argv, optstring) + int argc; + char *const *argv; + const char *optstring; +{ + return _getopt_internal (argc, argv, optstring, + (const struct option *) 0, + (int *) 0, + 0); +} + +#endif /* Not ELIDE_CODE. */ + +#ifdef TEST + +/* Compile with -DTEST to make an executable for use in testing + the above definition of `getopt'. */ + +int +main (argc, argv) + int argc; + char **argv; +{ + int c; + int digit_optind = 0; + + while (1) + { + int this_option_optind = optind ? optind : 1; + + c = getopt (argc, argv, "abc:d:0123456789"); + if (c == -1) + break; + + switch (c) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (digit_optind != 0 && digit_optind != this_option_optind) + printf ("digits occur in two different argv-elements.\n"); + digit_optind = this_option_optind; + printf ("option %c\n", c); + break; + + case 'a': + printf ("option a\n"); + break; + + case 'b': + printf ("option b\n"); + break; + + case 'c': + printf ("option c with value `%s'\n", optarg); + break; + + case '?': + break; + + default: + printf ("?? getopt returned character code 0%o ??\n", c); + } + } + + if (optind < argc) + { + printf ("non-option ARGV-elements: "); + while (optind < argc) + printf ("%s ", argv[optind++]); + printf ("\n"); + } + + exit (0); +} + +#endif /* TEST */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/getopt.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/getopt.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,141 @@ +/* Declarations for getopt. + Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 2000 + Free Software Foundation, Inc. + + NOTE: The canonical source of this file is maintained with the GNU C Library. + Bugs can be reported to bug-glibc@gnu.org. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +#ifndef _GETOPT_H +#define _GETOPT_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +/* For communication from `getopt' to the caller. + When `getopt' finds an option that takes an argument, + the argument value is returned here. + Also, when `ordering' is RETURN_IN_ORDER, + each non-option ARGV-element is returned here. */ + +extern char *optarg; + +/* Index in ARGV of the next element to be scanned. + This is used for communication to and from the caller + and for communication between successive calls to `getopt'. + + On entry to `getopt', zero means this is the first call; initialize. + + When `getopt' returns -1, this is the index of the first of the + non-option elements that the caller should itself scan. + + Otherwise, `optind' communicates from one call to the next + how much of ARGV has been scanned so far. */ + +extern int optind; + +/* Callers store zero here to inhibit the error message `getopt' prints + for unrecognized options. */ + +extern int opterr; + +/* Set to an option character which was unrecognized. */ + +extern int optopt; + +/* Describe the long-named options requested by the application. + The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector + of `struct option' terminated by an element containing a name which is + zero. + + The field `has_arg' is: + no_argument (or 0) if the option does not take an argument, + required_argument (or 1) if the option requires an argument, + optional_argument (or 2) if the option takes an optional argument. + + If the field `flag' is not NULL, it points to a variable that is set + to the value given in the field `val' when the option is found, but + left unchanged if the option is not found. + + To have a long-named option do something other than set an `int' to + a compiled-in constant, such as set a value from `optarg', set the + option's `flag' field to zero and its `val' field to a nonzero + value (the equivalent single-letter option character, if there is + one). For long options that have a zero `flag' field, `getopt' + returns the contents of the `val' field. */ + +struct option +{ +#if defined (__STDC__) && __STDC__ + const char *name; +#else + char *name; +#endif + /* has_arg can't be an enum because some compilers complain about + type mismatches in all the code that assumes it is an int. */ + int has_arg; + int *flag; + int val; +}; + +/* Names for the values of the `has_arg' field of `struct option'. */ + +#define no_argument 0 +#define required_argument 1 +#define optional_argument 2 + +#if defined (__STDC__) && __STDC__ +/* HAVE_DECL_* is a three-state macro: undefined, 0 or 1. If it is + undefined, we haven't run the autoconf check so provide the + declaration without arguments. If it is 0, we checked and failed + to find the declaration so provide a fully prototyped one. If it + is 1, we found it so don't provide any declaration at all. */ +#if defined (__GNU_LIBRARY__) || (defined (HAVE_DECL_GETOPT) && !HAVE_DECL_GETOPT) +/* Many other libraries have conflicting prototypes for getopt, with + differences in the consts, in stdlib.h. To avoid compilation + errors, only prototype getopt for the GNU C library. */ +extern int getopt (int argc, char *const *argv, const char *shortopts); +#else /* not __GNU_LIBRARY__ */ +# if !defined (HAVE_DECL_GETOPT) +extern int getopt (); +# endif +#endif /* __GNU_LIBRARY__ */ +extern int getopt_long (int argc, char *const *argv, const char *shortopts, + const struct option *longopts, int *longind); +extern int getopt_long_only (int argc, char *const *argv, + const char *shortopts, + const struct option *longopts, int *longind); + +/* Internal only. Users should not call this directly. */ +extern int _getopt_internal (int argc, char *const *argv, + const char *shortopts, + const struct option *longopts, int *longind, + int long_only); +#else /* not __STDC__ */ +extern int getopt (); +extern int getopt_long (); +extern int getopt_long_only (); + +extern int _getopt_internal (); +#endif /* __STDC__ */ + +#ifdef __cplusplus +} +#endif + +#endif /* getopt.h */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/sdl/getopt1.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/getopt1.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,190 @@ +/* getopt_long and getopt_long_only entry points for GNU getopt. + Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 + Free Software Foundation, Inc. + + NOTE: This source is derived from an old version taken from the GNU C + Library (glibc). + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "getopt.h" + +#if !defined __STDC__ || !__STDC__ +/* This is a separate conditional since some stdc systems + reject `defined (const)'. */ +#ifndef const +#define const +#endif +#endif + +#include + +/* Comment out all this code if we are using the GNU C Library, and are not + actually compiling the library itself. This code is part of the GNU C + Library, but also included in many other GNU distributions. Compiling + and linking in this code is a waste when using the GNU C library + (especially if it is a shared library). Rather than having every GNU + program understand `configure --with-gnu-libc' and omit the object files, + it is simpler to just do this in the source for each such file. */ + +#define GETOPT_INTERFACE_VERSION 2 +#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 +#include +#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION +#define ELIDE_CODE +#endif +#endif + +#ifndef ELIDE_CODE + + +/* This needs to come after some library #include + to get __GNU_LIBRARY__ defined. */ +#ifdef __GNU_LIBRARY__ +#include +#endif + +#ifndef NULL +#define NULL 0 +#endif + +int +getopt_long (argc, argv, options, long_options, opt_index) + int argc; + char *const *argv; + const char *options; + const struct option *long_options; + int *opt_index; +{ + return _getopt_internal (argc, argv, options, long_options, opt_index, 0); +} + +/* Like getopt_long, but '-' as well as '--' can indicate a long option. + If an option that starts with '-' (not '--') doesn't match a long option, + but does match a short option, it is parsed as a short option + instead. */ + +int +getopt_long_only (argc, argv, options, long_options, opt_index) + int argc; + char *const *argv; + const char *options; + const struct option *long_options; + int *opt_index; +{ + return _getopt_internal (argc, argv, options, long_options, opt_index, 1); +} + + +#endif /* Not ELIDE_CODE. */ + +#ifdef TEST + +#include + +int +main (argc, argv) + int argc; + char **argv; +{ + int c; + int digit_optind = 0; + + while (1) + { + int this_option_optind = optind ? optind : 1; + int option_index = 0; + static struct option long_options[] = + { + {"add", 1, 0, 0}, + {"append", 0, 0, 0}, + {"delete", 1, 0, 0}, + {"verbose", 0, 0, 0}, + {"create", 0, 0, 0}, + {"file", 1, 0, 0}, + {0, 0, 0, 0} + }; + + c = getopt_long (argc, argv, "abc:d:0123456789", + long_options, &option_index); + if (c == -1) + break; + + switch (c) + { + case 0: + printf ("option %s", long_options[option_index].name); + if (optarg) + printf (" with arg %s", optarg); + printf ("\n"); + break; + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (digit_optind != 0 && digit_optind != this_option_optind) + printf ("digits occur in two different argv-elements.\n"); + digit_optind = this_option_optind; + printf ("option %c\n", c); + break; + + case 'a': + printf ("option a\n"); + break; + + case 'b': + printf ("option b\n"); + break; + + case 'c': + printf ("option c with value `%s'\n", optarg); + break; + + case 'd': + printf ("option d with value `%s'\n", optarg); + break; + + case '?': + break; + + default: + printf ("?? getopt returned character code 0%o ??\n", c); + } + } + + if (optind < argc) + { + printf ("non-option ARGV-elements: "); + while (optind < argc) + printf ("%s ", argv[optind++]); + printf ("\n"); + } + + exit (0); +} + +#endif /* TEST */ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/version.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/version.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,66 @@ +#ifndef VBA_VERSION_H +#define VBA_VERSION_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#define STRINGIZE_VALUE(X) _Py_STRINGIZE2(X) +#define _Py_STRINGIZE(X) _Py_STRINGIZE1((X)) +#define _Py_STRINGIZE1(X) _Py_STRINGIZE2 ## X +#define _Py_STRINGIZE2(X) #X +//re: http://72.14.203.104/search?q=cache:HG-okth5NGkJ:mail.python.org/pipermail/python-checkins/2002-November/030704.html+_msc_ver+compiler+version+string&hl=en&gl=us&ct=clnk&cd=5 + +#if defined(_MSC_VER) +# define VBA_COMPILER "" +# define VBA_COMPILER_DETAIL " msvc " _Py_STRINGIZE(_MSC_VER) +#else +// TODO: make for others compilers +# define VBA_COMPILER "" +# define VBA_COMPILER_DETAIL "" +#endif + +#define VBA_NAME "VBA-RR" +#define VBA_RR_MAJOR_VERSION_NO 23 +#define VBA_RR_MINOR_VERSION_NO 5 + +#if !defined(_DEBUG) && (defined(WIN32) || defined(RC_INVOKED)) +# include "../win32/userconfig/svnrev.h" +#endif +# +#ifndef SVN_REV +# define SVN_REV 0 +#endif + +#ifndef SVN_REV_STR +# if SVN_REV > 0 +# define SVN_REV_STR STRINGIZE_VALUE(SVN_REV) +# else +# define define SVN_REV_STR "" +# endif +#endif + +#ifdef _DEBUG +# define VBA_SUBVERSION_STRING " DEBUG" +# define VBA_BUILDTYPE_STRING "Debug" +#elif defined(PUBLIC_RELEASE) +# define VBA_SUBVERSION_STRING "" +# define VBA_BUILDTYPE_STRING "Release" +#else // interim +# define VBA_SUBVERSION_STRING " svn" SVN_REV_STR +# define VBA_BUILDTYPE_STRING "Interim" +#endif + +#define VBA_FEATURE_STRING "" + +#if VBA_RR_MINOR_VERSION_NO > 0 +# define VBA_RR_VERSION_NO VBA_RR_MAJOR_VERSION_NO ## . ## VBA_RR_MINOR_VERSION_NO +#else +# define VBA_RR_VERSION_NO VBA_RR_MAJOR_VERSION_NO +#endif + +#define VBA_VERSION_STRING "v" STRINGIZE_VALUE(VBA_RR_VERSION_NO) VBA_SUBVERSION_STRING VBA_FEATURE_STRING VBA_COMPILER +#define VBA_NAME_AND_VERSION VBA_NAME " " VBA_VERSION_STRING +#define VBA_RR_SITE "http://code.google.com/p/vba-rerecording/" + +#endif // !VBA_VERSION_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/.cvsignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/.cvsignore Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1 @@ +vba.aps diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/7zip_2005.vcproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/7zip_2005.vcproj Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,4058 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/7zip_2008.vcproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/7zip_2008.vcproj Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,5347 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/7zip_2010.vcxproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/7zip_2010.vcxproj Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1193 @@ + + + + + Debug + Win32 + + + Release FastBuild + Win32 + + + Release + Win32 + + + + wlib7zip + {5646C572-A578-49F8-9DA9-3E00A8CBFE3F} + gens + + + + StaticLibrary + false + false + MultiByte + + + StaticLibrary + false + MultiByte + + + StaticLibrary + false + true + MultiByte + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.21006.1 + $(SolutionDir).VS2010\$(Configuration)\$(Platform)\$(ProjectName)\ + $(SolutionDir).VS2010\$(Configuration)\$(Platform)\$(ProjectName)\ + $(SolutionDir).VS2010\$(Configuration)\$(Platform)\$(ProjectName)\ + $(SolutionDir).VS2010\$(Configuration)\$(Platform)\$(ProjectName)\ + $(SolutionDir).VS2010\$(Configuration)\$(Platform)\$(ProjectName)\ + $(SolutionDir).VS2010\$(Configuration)\$(Platform)\$(ProjectName)\ + AllRules.ruleset + + + AllRules.ruleset + + + AllRules.ruleset + + + + + + NDEBUG;%(PreprocessorDefinitions) + true + true + Win32 + .\Release/gens.tlb + + + + + Full + AnySuitable + false + Speed + true + true + true + CPP;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;EXTRACT_ONLY;_NO_CRYPTO;%(PreprocessorDefinitions) + true + Sync + MultiThreaded + Use + $(IntDir)7z.pch + $(IntDir) + $(IntDir) + $(IntDir)vc80.pdb + Level3 + true + ProgramDatabase + true + false + NotSet + + + NDEBUG;%(PreprocessorDefinitions) + + + /LTCG %(AdditionalOptions) + + + + + + + + + + + + + NDEBUG;%(PreprocessorDefinitions) + true + true + Win32 + .\Release/gens.tlb + + + + + Disabled + Default + false + CPP;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;EXTRACT_ONLY;_NO_CRYPTO;%(PreprocessorDefinitions) + false + Sync + EnableFastChecks + true + MultiThreadedDebug + false + Use + $(IntDir)7z.pch + $(IntDir) + $(IntDir) + $(IntDir)vc80.pdb + Level3 + true + ProgramDatabase + true + + + NDEBUG;%(PreprocessorDefinitions) + + + + + NDEBUG;%(PreprocessorDefinitions) + true + true + Win32 + .\Release/gens.tlb + + + + + Full + AnySuitable + true + Speed + true + true + false + CPP;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;EXTRACT_ONLY;_NO_CRYPTO;%(PreprocessorDefinitions) + true + Sync + MultiThreaded + Use + $(IntDir)7z.pch + $(IntDir) + $(IntDir) + $(IntDir)vc80.pdb + Level3 + true + ProgramDatabase + true + + + NDEBUG;%(PreprocessorDefinitions) + + + /LTCG %(AdditionalOptions) + + + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + true + Use + true + Use + true + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Create + Create + Create + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + Use + Use + Use + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/7zip_2010.vcxproj.filters --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/7zip_2010.vcxproj.filters Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1101 @@ + + + + + {640011be-3f4f-4873-8d35-29603260353a} + + + {ca6f9c4b-9f5f-48ad-a31c-af98fcd62e40} + + + {92298691-8079-41ab-97b4-ea8c5d57705d} + + + {07c1f99d-328c-4292-bcae-756aa615b6b8} + + + {d4404f87-37f6-47c0-84ad-28f886e050f1} + + + {2da83f5f-49e0-4bf4-94df-5f71548d513e} + + + {c294dbd6-e157-4b12-a25f-f86c6f33c0ac} + + + {cf17b1b3-505a-4072-82c3-a558076da4c9} + + + {2c848328-adab-4c70-8850-0cd99749a22a} + + + {681491fd-a85a-4c9d-979c-16310a4bc7d2} + + + {f3894462-e945-41f6-8e24-095eb40a5366} + + + {67371b0c-95dc-4dc8-8783-b64dff05d198} + + + {5ea4ccf3-187c-4ab5-a59c-a7ad7eec0faa} + + + {f50e973f-7173-4cde-a755-fb5e8d91ab0e} + + + {6af22ddd-1f4e-4843-a216-ec5e10c4fa28} + + + {79334f59-b7f7-4c4f-86af-408256cd4abe} + + + {489a1d17-d337-4891-9071-61795accd837} + + + {97f1da29-65d5-42e9-9d87-6e25185e01c6} + + + {6ae17cf5-91d4-437c-84ea-e70b79c5f915} + + + {17dd5db8-6972-4bde-a450-a16aded15a07} + + + {3a7d5a57-f03c-4adb-8101-4dd3c84d9514} + + + {7164d745-e58e-44a4-bfe9-037f44bee447} + + + {33eeb3f2-448c-4542-b150-472136c39410} + + + {6f4b2871-2e91-4697-a7f5-6596fae9ac6e} + + + {c1dbfe74-7113-46fd-80cf-bb76de09bf98} + + + + + Archive\7z + + + Archive\7z + + + Archive\7z + + + Archive\7z + + + Archive\7z + + + Archive\7z + + + Archive\7z + + + Archive\7z + + + Archive\7z + + + Register + + + Archive\7z + + + Register + + + Register + + + Archive\bz2 + + + Archive\Archive common + + + Archive\Archive common + + + Archive\Archive common + + + Archive\Archive common + + + Archive\Archive common + + + Archive\Archive common + + + Archive\Archive common + + + Archive\Archive common + + + Archive\Archive common + + + Register + + + Archive\GZip + + + Archive\GZip + + + Archive\GZip + + + Register + + + Archive\Lzh + + + Archive\Lzh + + + Archive\Lzh + + + Archive\Lzh + + + Register + + + Register + + + Archive\Lzma Ar + + + Archive\Lzma Ar + + + Archive\Lzma Ar + + + Archive\Rar + + + Archive\Rar + + + Archive\Rar + + + Archive\Rar + + + Register + + + Archive\Rar + + + Archive\Split + + + Register + + + Archive\Tar + + + Archive\Tar + + + Archive\Tar + + + Register + + + Archive\Zip + + + Archive\Zip + + + Archive\Zip + + + Archive\Zip + + + Register + + + Register + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + 7zip + + + Compress\7z Compress + + + Register + + + Compress\7z Compress + + + Register + + + Compress\Bit Coder + + + Compress\7z Compress + + + Compress\7z Compress + + + Register + + + Compress\7z Compress + + + Register + + + Compress\BZip2 Compress + + + Compress\BZip2 Compress + + + Register + + + Register + + + Compress\7z Compress + + + Register + + + Register + + + Compress\Zip Compress + + + Register + + + Register + + + Compress\Zip Compress + + + Compress\Zip Compress + + + Compress + + + Compress\7z Compress + + + Register + + + Compress + + + Compress\PPMD + + + Register + + + Compress\Rar Compress + + + Compress\Rar Compress + + + Compress\Rar Compress + + + Compress\Rar Compress + + + Register + + + Compress\Zip Compress + + + Compress\Zip Compress + + + Crypto + + + Register + + + Crypto + + + Crypto + + + Crypto + + + Crypto + + + Crypto + + + Crypto + + + Crypto + + + Crypto + + + Crypto + + + Crypto + + + Common + + + Common + + + Common + + + Common + + + Common + + + Common + + + Common + + + Common + + + Common + + + Common + + + Common + + + Windows + + + Windows + + + Windows + + + Windows + + + Windows + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + C + + + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + Headers + + + \ No newline at end of file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/7zCrc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/7zCrc.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,35 @@ +/* 7zCrc.c -- CRC32 calculation +2008-08-05 +Igor Pavlov +Public domain */ + +#include "7zCrc.h" + +#define kCrcPoly 0xEDB88320 +UInt32 g_CrcTable[256]; + +void MY_FAST_CALL CrcGenerateTable(void) +{ + UInt32 i; + for (i = 0; i < 256; i++) + { + UInt32 r = i; + int j; + for (j = 0; j < 8; j++) + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1)); + g_CrcTable[i] = r; + } +} + +UInt32 MY_FAST_CALL CrcUpdate(UInt32 v, const void *data, size_t size) +{ + const Byte *p = (const Byte *)data; + for (; size > 0 ; size--, p++) + v = CRC_UPDATE_BYTE(v, *p); + return v; +} + +UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size) +{ + return CrcUpdate(CRC_INIT_VAL, data, size) ^ 0xFFFFFFFF; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/7zCrc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/7zCrc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +/* 7zCrc.h -- CRC32 calculation +2008-03-13 +Igor Pavlov +Public domain */ + +#ifndef __7Z_CRC_H +#define __7Z_CRC_H + +#include + +#include "Types.h" + +extern UInt32 g_CrcTable[]; + +void MY_FAST_CALL CrcGenerateTable(void); + +#define CRC_INIT_VAL 0xFFFFFFFF +#define CRC_GET_DIGEST(crc) ((crc) ^ 0xFFFFFFFF) +#define CRC_UPDATE_BYTE(crc, b) (g_CrcTable[((crc) ^ (b)) & 0xFF] ^ ((crc) >> 8)) + +UInt32 MY_FAST_CALL CrcUpdate(UInt32 crc, const void *data, size_t size); +UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Aes.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Aes.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,262 @@ +/* Aes.c -- AES encryption / decryption +2008-08-05 +Igor Pavlov +Public domain */ + +#include "Aes.h" +#include "CpuArch.h" + +static UInt32 T[256 * 4]; +static Byte Sbox[256] = { + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}; + +static UInt32 D[256 * 4]; +static Byte InvS[256]; + +static Byte Rcon[11] = { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; + +#define xtime(x) ((((x) << 1) ^ (((x) & 0x80) != 0 ? 0x1B : 0)) & 0xFF) + +#define Ui32(a0, a1, a2, a3) ((UInt32)(a0) | ((UInt32)(a1) << 8) | ((UInt32)(a2) << 16) | ((UInt32)(a3) << 24)) + +#define gb0(x) ( (x) & 0xFF) +#define gb1(x) (((x) >> ( 8)) & 0xFF) +#define gb2(x) (((x) >> (16)) & 0xFF) +#define gb3(x) (((x) >> (24)) & 0xFF) + +void AesGenTables(void) +{ + unsigned i; + for (i = 0; i < 256; i++) + InvS[Sbox[i]] = (Byte)i; + for (i = 0; i < 256; i++) + { + { + UInt32 a1 = Sbox[i]; + UInt32 a2 = xtime(a1); + UInt32 a3 = xtime(a1) ^ a1; + T[ i] = Ui32(a2, a1, a1, a3); + T[0x100 + i] = Ui32(a3, a2, a1, a1); + T[0x200 + i] = Ui32(a1, a3, a2, a1); + T[0x300 + i] = Ui32(a1, a1, a3, a2); + } + { + UInt32 a1 = InvS[i]; + UInt32 a2 = xtime(a1); + UInt32 a4 = xtime(a2); + UInt32 a8 = xtime(a4); + UInt32 a9 = a8 ^ a1; + UInt32 aB = a8 ^ a2 ^ a1; + UInt32 aD = a8 ^ a4 ^ a1; + UInt32 aE = a8 ^ a4 ^ a2; + D[ i] = Ui32(aE, a9, aD, aB); + D[0x100 + i] = Ui32(aB, aE, a9, aD); + D[0x200 + i] = Ui32(aD, aB, aE, a9); + D[0x300 + i] = Ui32(a9, aD, aB, aE); + } + } +} + +#define HT(i, x, s) (T + (x << 8))[gb ## x(s[(i + x) & 3])] +#define HT4(m, i, s, p) m[i] = \ + HT(i, 0, s) ^ \ + HT(i, 1, s) ^ \ + HT(i, 2, s) ^ \ + HT(i, 3, s) ^ w[p + i] +/* such order (2031) in HT16 is for VC6/K8 speed optimization) */ +#define HT16(m, s, p) \ + HT4(m, 2, s, p); \ + HT4(m, 0, s, p); \ + HT4(m, 3, s, p); \ + HT4(m, 1, s, p); \ + +#define FT(i, x) Sbox[gb ## x(m[(i + x) & 3])] +#define FT4(i) dest[i] = Ui32(FT(i, 0), FT(i, 1), FT(i, 2), FT(i, 3)) ^ w[i]; + +#define HD(i, x, s) (D + (x << 8))[gb ## x(s[(i - x) & 3])] +#define HD4(m, i, s, p) m[i] = \ + HD(i, 0, s) ^ \ + HD(i, 1, s) ^ \ + HD(i, 2, s) ^ \ + HD(i, 3, s) ^ w[p + i]; +/* such order (0231) in HD16 is for VC6/K8 speed optimization) */ +#define HD16(m, s, p) \ + HD4(m, 0, s, p); \ + HD4(m, 2, s, p); \ + HD4(m, 3, s, p); \ + HD4(m, 1, s, p); \ + +#define FD(i, x) InvS[gb ## x(m[(i - x) & 3])] +#define FD4(i) dest[i] = Ui32(FD(i, 0), FD(i, 1), FD(i, 2), FD(i, 3)) ^ w[i]; + +void Aes_SetKeyEncode(CAes *p, const Byte *key, unsigned keySize) +{ + unsigned i, wSize; + UInt32 *w; + keySize /= 4; + p->numRounds2 = keySize / 2 + 3; + + wSize = (p->numRounds2 * 2 + 1) * 4; + w = p->rkey; + + for (i = 0; i < keySize; i++, key += 4) + w[i] = Ui32(key[0], key[1], key[2], key[3]); + + for (; i < wSize; i++) + { + UInt32 t = w[i - 1]; + unsigned rem = i % keySize; + if (rem == 0) + t = Ui32(Sbox[gb1(t)] ^ Rcon[i / keySize], Sbox[gb2(t)], Sbox[gb3(t)], Sbox[gb0(t)]); + else if (keySize > 6 && rem == 4) + t = Ui32(Sbox[gb0(t)], Sbox[gb1(t)], Sbox[gb2(t)], Sbox[gb3(t)]); + w[i] = w[i - keySize] ^ t; + } +} + +void Aes_SetKeyDecode(CAes *p, const Byte *key, unsigned keySize) +{ + unsigned i, num; + UInt32 *w; + Aes_SetKeyEncode(p, key, keySize); + num = p->numRounds2 * 8 - 4; + w = p->rkey + 4; + for (i = 0; i < num; i++) + { + UInt32 r = w[i]; + w[i] = + D[ Sbox[gb0(r)]] ^ + D[0x100 + Sbox[gb1(r)]] ^ + D[0x200 + Sbox[gb2(r)]] ^ + D[0x300 + Sbox[gb3(r)]]; + } +} + +static void AesEncode32(UInt32 *dest, const UInt32 *src, const UInt32 *w, unsigned numRounds2) +{ + UInt32 s[4]; + UInt32 m[4]; + s[0] = src[0] ^ w[0]; + s[1] = src[1] ^ w[1]; + s[2] = src[2] ^ w[2]; + s[3] = src[3] ^ w[3]; + w += 4; + for (;;) + { + HT16(m, s, 0); + if (--numRounds2 == 0) + break; + HT16(s, m, 4); + w += 8; + } + w += 4; + FT4(0); FT4(1); FT4(2); FT4(3); +} + +static void AesDecode32(UInt32 *dest, const UInt32 *src, const UInt32 *w, unsigned numRounds2) +{ + UInt32 s[4]; + UInt32 m[4]; + w += numRounds2 * 8; + s[0] = src[0] ^ w[0]; + s[1] = src[1] ^ w[1]; + s[2] = src[2] ^ w[2]; + s[3] = src[3] ^ w[3]; + for (;;) + { + w -= 8; + HD16(m, s, 4); + if (--numRounds2 == 0) + break; + HD16(s, m, 0); + } + FD4(0); FD4(1); FD4(2); FD4(3); +} + +void Aes_Encode32(const CAes *p, UInt32 *dest, const UInt32 *src) +{ + AesEncode32(dest, src, p->rkey, p->numRounds2); +} + +void Aes_Decode32(const CAes *p, UInt32 *dest, const UInt32 *src) +{ + AesDecode32(dest, src, p->rkey, p->numRounds2); +} + +void AesCbc_Init(CAesCbc *p, const Byte *iv) +{ + unsigned i; + for (i = 0; i < 4; i++) + p->prev[i] = GetUi32(iv + i * 4); +} + +SizeT AesCbc_Encode(CAesCbc *p, Byte *data, SizeT size) +{ + SizeT i; + if (size == 0) + return 0; + if (size < AES_BLOCK_SIZE) + return AES_BLOCK_SIZE; + size -= AES_BLOCK_SIZE; + for (i = 0; i <= size; i += AES_BLOCK_SIZE, data += AES_BLOCK_SIZE) + { + p->prev[0] ^= GetUi32(data); + p->prev[1] ^= GetUi32(data + 4); + p->prev[2] ^= GetUi32(data + 8); + p->prev[3] ^= GetUi32(data + 12); + + AesEncode32(p->prev, p->prev, p->aes.rkey, p->aes.numRounds2); + + SetUi32(data, p->prev[0]); + SetUi32(data + 4, p->prev[1]); + SetUi32(data + 8, p->prev[2]); + SetUi32(data + 12, p->prev[3]); + } + return i; +} + +SizeT AesCbc_Decode(CAesCbc *p, Byte *data, SizeT size) +{ + SizeT i; + UInt32 in[4], out[4]; + if (size == 0) + return 0; + if (size < AES_BLOCK_SIZE) + return AES_BLOCK_SIZE; + size -= AES_BLOCK_SIZE; + for (i = 0; i <= size; i += AES_BLOCK_SIZE, data += AES_BLOCK_SIZE) + { + in[0] = GetUi32(data); + in[1] = GetUi32(data + 4); + in[2] = GetUi32(data + 8); + in[3] = GetUi32(data + 12); + + AesDecode32(out, in, p->aes.rkey, p->aes.numRounds2); + + SetUi32(data, p->prev[0] ^ out[0]); + SetUi32(data + 4, p->prev[1] ^ out[1]); + SetUi32(data + 8, p->prev[2] ^ out[2]); + SetUi32(data + 12, p->prev[3] ^ out[3]); + + p->prev[0] = in[0]; + p->prev[1] = in[1]; + p->prev[2] = in[2]; + p->prev[3] = in[3]; + } + return i; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Aes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Aes.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,48 @@ +/* Aes.h -- AES encryption / decryption +2008-08-05 +Igor Pavlov +Public domain */ + +#ifndef __AES_H +#define __AES_H + +#include "Types.h" + +#define AES_BLOCK_SIZE 16 + +typedef struct +{ + unsigned numRounds2; /* = numRounds / 2 */ + UInt32 rkey[(14 + 1) * 4]; +} CAes; + +/* Call AesGenTables one time before other AES functions */ +void AesGenTables(void); + +/* keySize = 16 or 24 or 32 (bytes) */ +void Aes_SetKeyEncode(CAes *p, const Byte *key, unsigned keySize); +void Aes_SetKeyDecode(CAes *p, const Byte *key, unsigned keySize); + +/* Aes_Encode32 and Aes_Decode32 functions work with little-endian words. + src and dest are pointers to 4 UInt32 words. + arc and dest can point to same block */ +void Aes_Encode32(const CAes *p, UInt32 *dest, const UInt32 *src); +void Aes_Decode32(const CAes *p, UInt32 *dest, const UInt32 *src); + +typedef struct +{ + UInt32 prev[4]; + CAes aes; +} CAesCbc; + +void AesCbc_Init(CAesCbc *p, const Byte *iv); /* iv size is AES_BLOCK_SIZE */ + +/* AesCbc_Encode and AesCbc_Decode: + if (res <= size): Filter have converted res bytes + if (res > size): Filter have not converted anything. And it needs at + least res = AES_BLOCK_SIZE bytes to convert one block */ + +SizeT AesCbc_Encode(CAesCbc *p, Byte *data, SizeT size); +SizeT AesCbc_Decode(CAesCbc *p, Byte *data, SizeT size); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Alloc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Alloc.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,127 @@ +/* Alloc.c -- Memory allocation functions +2008-09-24 +Igor Pavlov +Public domain */ + +#ifdef _WIN32 +#include +#endif +#include + +#include "Alloc.h" + +/* #define _SZ_ALLOC_DEBUG */ + +/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ +#ifdef _SZ_ALLOC_DEBUG +#include +int g_allocCount = 0; +int g_allocCountMid = 0; +int g_allocCountBig = 0; +#endif + +void *MyAlloc(size_t size) +{ + if (size == 0) + return 0; + #ifdef _SZ_ALLOC_DEBUG + { + void *p = malloc(size); + fprintf(stderr, "\nAlloc %10d bytes, count = %10d, addr = %8X", size, g_allocCount++, (unsigned)p); + return p; + } + #else + return malloc(size); + #endif +} + +void MyFree(void *address) +{ + #ifdef _SZ_ALLOC_DEBUG + if (address != 0) + fprintf(stderr, "\nFree; count = %10d, addr = %8X", --g_allocCount, (unsigned)address); + #endif + free(address); +} + +#ifdef _WIN32 + +void *MidAlloc(size_t size) +{ + if (size == 0) + return 0; + #ifdef _SZ_ALLOC_DEBUG + fprintf(stderr, "\nAlloc_Mid %10d bytes; count = %10d", size, g_allocCountMid++); + #endif + return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); +} + +void MidFree(void *address) +{ + #ifdef _SZ_ALLOC_DEBUG + if (address != 0) + fprintf(stderr, "\nFree_Mid; count = %10d", --g_allocCountMid); + #endif + if (address == 0) + return; + VirtualFree(address, 0, MEM_RELEASE); +} + +#ifndef MEM_LARGE_PAGES +#undef _7ZIP_LARGE_PAGES +#endif + +#ifdef _7ZIP_LARGE_PAGES +SIZE_T g_LargePageSize = 0; +typedef SIZE_T (WINAPI *GetLargePageMinimumP)(); +#endif + +void SetLargePageSize() +{ + #ifdef _7ZIP_LARGE_PAGES + SIZE_T size = 0; + GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) + GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum"); + if (largePageMinimum == 0) + return; + size = largePageMinimum(); + if (size == 0 || (size & (size - 1)) != 0) + return; + g_LargePageSize = size; + #endif +} + + +void *BigAlloc(size_t size) +{ + if (size == 0) + return 0; + #ifdef _SZ_ALLOC_DEBUG + fprintf(stderr, "\nAlloc_Big %10d bytes; count = %10d", size, g_allocCountBig++); + #endif + + #ifdef _7ZIP_LARGE_PAGES + if (g_LargePageSize != 0 && g_LargePageSize <= (1 << 30) && size >= (1 << 18)) + { + void *res = VirtualAlloc(0, (size + g_LargePageSize - 1) & (~(g_LargePageSize - 1)), + MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); + if (res != 0) + return res; + } + #endif + return VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); +} + +void BigFree(void *address) +{ + #ifdef _SZ_ALLOC_DEBUG + if (address != 0) + fprintf(stderr, "\nFree_Big; count = %10d", --g_allocCountBig); + #endif + + if (address == 0) + return; + VirtualFree(address, 0, MEM_RELEASE); +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Alloc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Alloc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,32 @@ +/* Alloc.h -- Memory allocation functions +2008-03-13 +Igor Pavlov +Public domain */ + +#ifndef __COMMON_ALLOC_H +#define __COMMON_ALLOC_H + +#include + +void *MyAlloc(size_t size); +void MyFree(void *address); + +#ifdef _WIN32 + +void SetLargePageSize(); + +void *MidAlloc(size_t size); +void MidFree(void *address); +void *BigAlloc(size_t size); +void BigFree(void *address); + +#else + +#define MidAlloc(size) MyAlloc(size) +#define MidFree(address) MyFree(address) +#define BigAlloc(size) MyAlloc(size) +#define BigFree(address) MyFree(address) + +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Bra.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Bra.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,133 @@ +/* Bra.c -- Converters for RISC code +2008-10-04 : Igor Pavlov : Public domain */ + +#include "Bra.h" + +SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) +{ + SizeT i; + if (size < 4) + return 0; + size -= 4; + ip += 8; + for (i = 0; i <= size; i += 4) + { + if (data[i + 3] == 0xEB) + { + UInt32 dest; + UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]); + src <<= 2; + if (encoding) + dest = ip + (UInt32)i + src; + else + dest = src - (ip + (UInt32)i); + dest >>= 2; + data[i + 2] = (Byte)(dest >> 16); + data[i + 1] = (Byte)(dest >> 8); + data[i + 0] = (Byte)dest; + } + } + return i; +} + +SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) +{ + SizeT i; + if (size < 4) + return 0; + size -= 4; + ip += 4; + for (i = 0; i <= size; i += 2) + { + if ((data[i + 1] & 0xF8) == 0xF0 && + (data[i + 3] & 0xF8) == 0xF8) + { + UInt32 dest; + UInt32 src = + (((UInt32)data[i + 1] & 0x7) << 19) | + ((UInt32)data[i + 0] << 11) | + (((UInt32)data[i + 3] & 0x7) << 8) | + (data[i + 2]); + + src <<= 1; + if (encoding) + dest = ip + (UInt32)i + src; + else + dest = src - (ip + (UInt32)i); + dest >>= 1; + + data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7)); + data[i + 0] = (Byte)(dest >> 11); + data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7)); + data[i + 2] = (Byte)dest; + i += 2; + } + } + return i; +} + +SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) +{ + SizeT i; + if (size < 4) + return 0; + size -= 4; + for (i = 0; i <= size; i += 4) + { + if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1) + { + UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) | + ((UInt32)data[i + 1] << 16) | + ((UInt32)data[i + 2] << 8) | + ((UInt32)data[i + 3] & (~3)); + + UInt32 dest; + if (encoding) + dest = ip + (UInt32)i + src; + else + dest = src - (ip + (UInt32)i); + data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3)); + data[i + 1] = (Byte)(dest >> 16); + data[i + 2] = (Byte)(dest >> 8); + data[i + 3] &= 0x3; + data[i + 3] |= dest; + } + } + return i; +} + +SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) +{ + UInt32 i; + if (size < 4) + return 0; + size -= 4; + for (i = 0; i <= size; i += 4) + { + if (data[i] == 0x40 && (data[i + 1] & 0xC0) == 0x00 || + data[i] == 0x7F && (data[i + 1] & 0xC0) == 0xC0) + { + UInt32 src = + ((UInt32)data[i + 0] << 24) | + ((UInt32)data[i + 1] << 16) | + ((UInt32)data[i + 2] << 8) | + ((UInt32)data[i + 3]); + UInt32 dest; + + src <<= 2; + if (encoding) + dest = ip + i + src; + else + dest = src - (ip + i); + dest >>= 2; + + dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000; + + data[i + 0] = (Byte)(dest >> 24); + data[i + 1] = (Byte)(dest >> 16); + data[i + 2] = (Byte)(dest >> 8); + data[i + 3] = (Byte)dest; + } + } + return i; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Bra.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Bra.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,60 @@ +/* Bra.h -- Branch converters for executables +2008-10-04 : Igor Pavlov : Public domain */ + +#ifndef __BRA_H +#define __BRA_H + +#include "Types.h" + +/* +These functions convert relative addresses to absolute addresses +in CALL instructions to increase the compression ratio. + + In: + data - data buffer + size - size of data + ip - current virtual Instruction Pinter (IP) value + state - state variable for x86 converter + encoding - 0 (for decoding), 1 (for encoding) + + Out: + state - state variable for x86 converter + + Returns: + The number of processed bytes. If you call these functions with multiple calls, + you must start next call with first byte after block of processed bytes. + + Type Endian Alignment LookAhead + + x86 little 1 4 + ARMT little 2 2 + ARM little 4 0 + PPC big 4 0 + SPARC big 4 0 + IA64 little 16 0 + + size must be >= Alignment + LookAhead, if it's not last block. + If (size < Alignment + LookAhead), converter returns 0. + + Example: + + UInt32 ip = 0; + for () + { + ; size must be >= Alignment + LookAhead, if it's not last block + SizeT processed = Convert(data, size, ip, 1); + data += processed; + size -= processed; + ip += processed; + } +*/ + +#define x86_Convert_Init(state) { state = 0; } +SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding); +SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); +SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); +SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); +SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); +SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Bra86.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Bra86.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,85 @@ +/* Bra86.c -- Converter for x86 code (BCJ) +2008-10-04 : Igor Pavlov : Public domain */ + +#include "Bra.h" + +#define Test86MSByte(b) ((b) == 0 || (b) == 0xFF) + +const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0}; +const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3}; + +SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding) +{ + SizeT bufferPos = 0, prevPosT; + UInt32 prevMask = *state & 0x7; + if (size < 5) + return 0; + ip += 5; + prevPosT = (SizeT)0 - 1; + + for (;;) + { + Byte *p = data + bufferPos; + Byte *limit = data + size - 4; + for (; p < limit; p++) + if ((*p & 0xFE) == 0xE8) + break; + bufferPos = (SizeT)(p - data); + if (p >= limit) + break; + prevPosT = bufferPos - prevPosT; + if (prevPosT > 3) + prevMask = 0; + else + { + prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7; + if (prevMask != 0) + { + Byte b = p[4 - kMaskToBitNumber[prevMask]]; + if (!kMaskToAllowedStatus[prevMask] || Test86MSByte(b)) + { + prevPosT = bufferPos; + prevMask = ((prevMask << 1) & 0x7) | 1; + bufferPos++; + continue; + } + } + } + prevPosT = bufferPos; + + if (Test86MSByte(p[4])) + { + UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]); + UInt32 dest; + for (;;) + { + Byte b; + int index; + if (encoding) + dest = (ip + (UInt32)bufferPos) + src; + else + dest = src - (ip + (UInt32)bufferPos); + if (prevMask == 0) + break; + index = kMaskToBitNumber[prevMask] * 8; + b = (Byte)((dest >> (24 - index)) & 0xFF); + if (!Test86MSByte(b)) + break; + src = dest ^ ((1 << (32 - index)) - 1); + } + p[4] = (Byte)((~(((dest >> 24) & 1) - 1)) & 0xFF); + p[3] = (Byte)((dest >> 16) & 0xFF); + p[2] = (Byte)((dest >> 8) & 0xFF); + p[1] = (Byte)(dest & 0xFF); + bufferPos += 5; + } + else + { + prevMask = ((prevMask << 1) & 0x7) | 1; + bufferPos++; + } + } + prevPosT = bufferPos - prevPosT; + *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7)); + return bufferPos; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/BraIA64.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/BraIA64.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,67 @@ +/* BraIA64.c -- Converter for IA-64 code +2008-10-04 : Igor Pavlov : Public domain */ + +#include "Bra.h" + +static const Byte kBranchTable[32] = +{ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 4, 4, 6, 6, 0, 0, 7, 7, + 4, 4, 0, 0, 4, 4, 0, 0 +}; + +SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding) +{ + SizeT i; + if (size < 16) + return 0; + size -= 16; + for (i = 0; i <= size; i += 16) + { + UInt32 instrTemplate = data[i] & 0x1F; + UInt32 mask = kBranchTable[instrTemplate]; + UInt32 bitPos = 5; + int slot; + for (slot = 0; slot < 3; slot++, bitPos += 41) + { + UInt32 bytePos, bitRes; + UInt64 instruction, instNorm; + int j; + if (((mask >> slot) & 1) == 0) + continue; + bytePos = (bitPos >> 3); + bitRes = bitPos & 0x7; + instruction = 0; + for (j = 0; j < 6; j++) + instruction += (UInt64)data[i + j + bytePos] << (8 * j); + + instNorm = instruction >> bitRes; + if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0) + { + UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF); + UInt32 dest; + src |= ((UInt32)(instNorm >> 36) & 1) << 20; + + src <<= 4; + + if (encoding) + dest = ip + (UInt32)i + src; + else + dest = src - (ip + (UInt32)i); + + dest >>= 4; + + instNorm &= ~((UInt64)(0x8FFFFF) << 13); + instNorm |= ((UInt64)(dest & 0xFFFFF) << 13); + instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20)); + + instruction &= (1 << bitRes) - 1; + instruction |= (instNorm << bitRes); + for (j = 0; j < 6; j++) + data[i + j + bytePos] = (Byte)(instruction >> (8 * j)); + } + } + } + return i; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/BwtSort.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/BwtSort.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,516 @@ +/* BwtSort.c -- BWT block sorting +2008-08-17 +Igor Pavlov +Public domain */ + +#include "BwtSort.h" +#include "Sort.h" + +/* #define BLOCK_SORT_USE_HEAP_SORT */ + +#define NO_INLINE MY_FAST_CALL + +/* Don't change it !!! */ +#define kNumHashBytes 2 +#define kNumHashValues (1 << (kNumHashBytes * 8)) + +/* kNumRefBitsMax must be < (kNumHashBytes * 8) = 16 */ +#define kNumRefBitsMax 12 + +#define BS_TEMP_SIZE kNumHashValues + +#ifdef BLOCK_SORT_EXTERNAL_FLAGS + +/* 32 Flags in UInt32 word */ +#define kNumFlagsBits 5 +#define kNumFlagsInWord (1 << kNumFlagsBits) +#define kFlagsMask (kNumFlagsInWord - 1) +#define kAllFlags 0xFFFFFFFF + +#else + +#define kNumBitsMax 20 +#define kIndexMask ((1 << kNumBitsMax) - 1) +#define kNumExtraBits (32 - kNumBitsMax) +#define kNumExtra0Bits (kNumExtraBits - 2) +#define kNumExtra0Mask ((1 << kNumExtra0Bits) - 1) + +#define SetFinishedGroupSize(p, size) \ + { *(p) |= ((((size) - 1) & kNumExtra0Mask) << kNumBitsMax); \ + if ((size) > (1 << kNumExtra0Bits)) { \ + *(p) |= 0x40000000; *((p) + 1) |= ((((size) - 1)>> kNumExtra0Bits) << kNumBitsMax); } } \ + +static void SetGroupSize(UInt32 *p, UInt32 size) +{ + if (--size == 0) + return; + *p |= 0x80000000 | ((size & kNumExtra0Mask) << kNumBitsMax); + if (size >= (1 << kNumExtra0Bits)) + { + *p |= 0x40000000; + p[1] |= ((size >> kNumExtra0Bits) << kNumBitsMax); + } +} + +#endif + +/* +SortGroup - is recursive Range-Sort function with HeapSort optimization for small blocks + "range" is not real range. It's only for optimization. +returns: 1 - if there are groups, 0 - no more groups +*/ + +UInt32 NO_INLINE SortGroup(UInt32 BlockSize, UInt32 NumSortedBytes, UInt32 groupOffset, UInt32 groupSize, int NumRefBits, UInt32 *Indices + #ifndef BLOCK_SORT_USE_HEAP_SORT + , UInt32 left, UInt32 range + #endif + ) +{ + UInt32 *ind2 = Indices + groupOffset; + UInt32 *Groups; + if (groupSize <= 1) + { + /* + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + SetFinishedGroupSize(ind2, 1); + #endif + */ + return 0; + } + Groups = Indices + BlockSize + BS_TEMP_SIZE; + if (groupSize <= ((UInt32)1 << NumRefBits) + #ifndef BLOCK_SORT_USE_HEAP_SORT + && groupSize <= range + #endif + ) + { + UInt32 *temp = Indices + BlockSize; + UInt32 j; + UInt32 mask, thereAreGroups, group, cg; + { + UInt32 gPrev; + UInt32 gRes = 0; + { + UInt32 sp = ind2[0] + NumSortedBytes; + if (sp >= BlockSize) sp -= BlockSize; + gPrev = Groups[sp]; + temp[0] = (gPrev << NumRefBits); + } + + for (j = 1; j < groupSize; j++) + { + UInt32 sp = ind2[j] + NumSortedBytes; + UInt32 g; + if (sp >= BlockSize) sp -= BlockSize; + g = Groups[sp]; + temp[j] = (g << NumRefBits) | j; + gRes |= (gPrev ^ g); + } + if (gRes == 0) + { + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + SetGroupSize(ind2, groupSize); + #endif + return 1; + } + } + + HeapSort(temp, groupSize); + mask = ((1 << NumRefBits) - 1); + thereAreGroups = 0; + + group = groupOffset; + cg = (temp[0] >> NumRefBits); + temp[0] = ind2[temp[0] & mask]; + + { + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + UInt32 *Flags = Groups + BlockSize; + #else + UInt32 prevGroupStart = 0; + #endif + + for (j = 1; j < groupSize; j++) + { + UInt32 val = temp[j]; + UInt32 cgCur = (val >> NumRefBits); + + if (cgCur != cg) + { + cg = cgCur; + group = groupOffset + j; + + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + { + UInt32 t = group - 1; + Flags[t >> kNumFlagsBits] &= ~(1 << (t & kFlagsMask)); + } + #else + SetGroupSize(temp + prevGroupStart, j - prevGroupStart); + prevGroupStart = j; + #endif + } + else + thereAreGroups = 1; + { + UInt32 ind = ind2[val & mask]; + temp[j] = ind; + Groups[ind] = group; + } + } + + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + SetGroupSize(temp + prevGroupStart, j - prevGroupStart); + #endif + } + + for (j = 0; j < groupSize; j++) + ind2[j] = temp[j]; + return thereAreGroups; + } + + /* Check that all strings are in one group (cannot sort) */ + { + UInt32 group, j; + UInt32 sp = ind2[0] + NumSortedBytes; if (sp >= BlockSize) sp -= BlockSize; + group = Groups[sp]; + for (j = 1; j < groupSize; j++) + { + sp = ind2[j] + NumSortedBytes; if (sp >= BlockSize) sp -= BlockSize; + if (Groups[sp] != group) + break; + } + if (j == groupSize) + { + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + SetGroupSize(ind2, groupSize); + #endif + return 1; + } + } + + #ifndef BLOCK_SORT_USE_HEAP_SORT + { + /* ---------- Range Sort ---------- */ + UInt32 i; + UInt32 mid; + for (;;) + { + UInt32 j; + if (range <= 1) + { + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + SetGroupSize(ind2, groupSize); + #endif + return 1; + } + mid = left + ((range + 1) >> 1); + j = groupSize; + i = 0; + do + { + UInt32 sp = ind2[i] + NumSortedBytes; if (sp >= BlockSize) sp -= BlockSize; + if (Groups[sp] >= mid) + { + for (j--; j > i; j--) + { + sp = ind2[j] + NumSortedBytes; if (sp >= BlockSize) sp -= BlockSize; + if (Groups[sp] < mid) + { + UInt32 temp = ind2[i]; ind2[i] = ind2[j]; ind2[j] = temp; + break; + } + } + if (i >= j) + break; + } + } + while (++i < j); + if (i == 0) + { + range = range - (mid - left); + left = mid; + } + else if (i == groupSize) + range = (mid - left); + else + break; + } + + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + { + UInt32 t = (groupOffset + i - 1); + UInt32 *Flags = Groups + BlockSize; + Flags[t >> kNumFlagsBits] &= ~(1 << (t & kFlagsMask)); + } + #endif + + { + UInt32 j; + for (j = i; j < groupSize; j++) + Groups[ind2[j]] = groupOffset + i; + } + + { + UInt32 res = SortGroup(BlockSize, NumSortedBytes, groupOffset, i, NumRefBits, Indices, left, mid - left); + return res | SortGroup(BlockSize, NumSortedBytes, groupOffset + i, groupSize - i, NumRefBits, Indices, mid, range - (mid - left)); + } + + } + + #else + + /* ---------- Heap Sort ---------- */ + + { + UInt32 j; + for (j = 0; j < groupSize; j++) + { + UInt32 sp = ind2[j] + NumSortedBytes; if (sp >= BlockSize) sp -= BlockSize; + ind2[j] = sp; + } + + HeapSortRef(ind2, Groups, groupSize); + + /* Write Flags */ + { + UInt32 sp = ind2[0]; + UInt32 group = Groups[sp]; + + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + UInt32 *Flags = Groups + BlockSize; + #else + UInt32 prevGroupStart = 0; + #endif + + for (j = 1; j < groupSize; j++) + { + sp = ind2[j]; + if (Groups[sp] != group) + { + group = Groups[sp]; + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + { + UInt32 t = groupOffset + j - 1; + Flags[t >> kNumFlagsBits] &= ~(1 << (t & kFlagsMask)); + } + #else + SetGroupSize(ind2 + prevGroupStart, j - prevGroupStart); + prevGroupStart = j; + #endif + } + } + + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + SetGroupSize(ind2 + prevGroupStart, j - prevGroupStart); + #endif + } + { + /* Write new Groups values and Check that there are groups */ + UInt32 thereAreGroups = 0; + for (j = 0; j < groupSize; j++) + { + UInt32 group = groupOffset + j; + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + UInt32 subGroupSize = ((ind2[j] & ~0xC0000000) >> kNumBitsMax); + if ((ind2[j] & 0x40000000) != 0) + subGroupSize += ((ind2[j + 1] >> kNumBitsMax) << kNumExtra0Bits); + subGroupSize++; + for (;;) + { + UInt32 original = ind2[j]; + UInt32 sp = original & kIndexMask; + if (sp < NumSortedBytes) sp += BlockSize; sp -= NumSortedBytes; + ind2[j] = sp | (original & ~kIndexMask); + Groups[sp] = group; + if (--subGroupSize == 0) + break; + j++; + thereAreGroups = 1; + } + #else + UInt32 *Flags = Groups + BlockSize; + for (;;) + { + UInt32 sp = ind2[j]; if (sp < NumSortedBytes) sp += BlockSize; sp -= NumSortedBytes; + ind2[j] = sp; + Groups[sp] = group; + if ((Flags[(groupOffset + j) >> kNumFlagsBits] & (1 << ((groupOffset + j) & kFlagsMask))) == 0) + break; + j++; + thereAreGroups = 1; + } + #endif + } + return thereAreGroups; + } + } + #endif +} + +/* conditions: blockSize > 0 */ +UInt32 BlockSort(UInt32 *Indices, const Byte *data, UInt32 blockSize) +{ + UInt32 *counters = Indices + blockSize; + UInt32 i; + UInt32 *Groups; + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + UInt32 *Flags; + #endif + + /* Radix-Sort for 2 bytes */ + for (i = 0; i < kNumHashValues; i++) + counters[i] = 0; + for (i = 0; i < blockSize - 1; i++) + counters[((UInt32)data[i] << 8) | data[i + 1]]++; + counters[((UInt32)data[i] << 8) | data[0]]++; + + Groups = counters + BS_TEMP_SIZE; + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + Flags = Groups + blockSize; + { + UInt32 numWords = (blockSize + kFlagsMask) >> kNumFlagsBits; + for (i = 0; i < numWords; i++) + Flags[i] = kAllFlags; + } + #endif + + { + UInt32 sum = 0; + for (i = 0; i < kNumHashValues; i++) + { + UInt32 groupSize = counters[i]; + if (groupSize > 0) + { + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + UInt32 t = sum + groupSize - 1; + Flags[t >> kNumFlagsBits] &= ~(1 << (t & kFlagsMask)); + #endif + sum += groupSize; + } + counters[i] = sum - groupSize; + } + + for (i = 0; i < blockSize - 1; i++) + Groups[i] = counters[((UInt32)data[i] << 8) | data[i + 1]]; + Groups[i] = counters[((UInt32)data[i] << 8) | data[0]]; + + for (i = 0; i < blockSize - 1; i++) + Indices[counters[((UInt32)data[i] << 8) | data[i + 1]]++] = i; + Indices[counters[((UInt32)data[i] << 8) | data[0]]++] = i; + + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + { + UInt32 prev = 0; + for (i = 0; i < kNumHashValues; i++) + { + UInt32 prevGroupSize = counters[i] - prev; + if (prevGroupSize == 0) + continue; + SetGroupSize(Indices + prev, prevGroupSize); + prev = counters[i]; + } + } + #endif + } + + { + int NumRefBits; + UInt32 NumSortedBytes; + for (NumRefBits = 0; ((blockSize - 1) >> NumRefBits) != 0; NumRefBits++); + NumRefBits = 32 - NumRefBits; + if (NumRefBits > kNumRefBitsMax) + NumRefBits = kNumRefBitsMax; + + for (NumSortedBytes = kNumHashBytes; ; NumSortedBytes <<= 1) + { + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + UInt32 finishedGroupSize = 0; + #endif + UInt32 newLimit = 0; + for (i = 0; i < blockSize;) + { + UInt32 groupSize; + #ifdef BLOCK_SORT_EXTERNAL_FLAGS + + if ((Flags[i >> kNumFlagsBits] & (1 << (i & kFlagsMask))) == 0) + { + i++; + continue; + } + for (groupSize = 1; + (Flags[(i + groupSize) >> kNumFlagsBits] & (1 << ((i + groupSize) & kFlagsMask))) != 0; + groupSize++); + + groupSize++; + + #else + + groupSize = ((Indices[i] & ~0xC0000000) >> kNumBitsMax); + { + Bool finishedGroup = ((Indices[i] & 0x80000000) == 0); + if ((Indices[i] & 0x40000000) != 0) + { + groupSize += ((Indices[i + 1] >> kNumBitsMax) << kNumExtra0Bits); + Indices[i + 1] &= kIndexMask; + } + Indices[i] &= kIndexMask; + groupSize++; + if (finishedGroup || groupSize == 1) + { + Indices[i - finishedGroupSize] &= kIndexMask; + if (finishedGroupSize > 1) + Indices[i - finishedGroupSize + 1] &= kIndexMask; + { + UInt32 newGroupSize = groupSize + finishedGroupSize; + SetFinishedGroupSize(Indices + i - finishedGroupSize, newGroupSize); + finishedGroupSize = newGroupSize; + } + i += groupSize; + continue; + } + finishedGroupSize = 0; + } + + #endif + + if (NumSortedBytes >= blockSize) + { + UInt32 j; + for (j = 0; j < groupSize; j++) + { + UInt32 t = (i + j); + /* Flags[t >> kNumFlagsBits] &= ~(1 << (t & kFlagsMask)); */ + Groups[Indices[t]] = t; + } + } + else + if (SortGroup(blockSize, NumSortedBytes, i, groupSize, NumRefBits, Indices + #ifndef BLOCK_SORT_USE_HEAP_SORT + , 0, blockSize + #endif + ) != 0) + newLimit = i + groupSize; + i += groupSize; + } + if (newLimit == 0) + break; + } + } + #ifndef BLOCK_SORT_EXTERNAL_FLAGS + for (i = 0; i < blockSize;) + { + UInt32 groupSize = ((Indices[i] & ~0xC0000000) >> kNumBitsMax); + if ((Indices[i] & 0x40000000) != 0) + { + groupSize += ((Indices[i + 1] >> kNumBitsMax) << kNumExtra0Bits); + Indices[i + 1] &= kIndexMask; + } + Indices[i] &= kIndexMask; + groupSize++; + i += groupSize; + } + #endif + return Groups[0]; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/BwtSort.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/BwtSort.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +/* BwtSort.h -- BWT block sorting +2008-03-26 +Igor Pavlov +Public domain */ + +#ifndef __BWTSORT_H +#define __BWTSORT_H + +#include "Types.h" + +/* use BLOCK_SORT_EXTERNAL_FLAGS if blockSize can be > 1M */ +/* #define BLOCK_SORT_EXTERNAL_FLAGS */ + +#ifdef BLOCK_SORT_EXTERNAL_FLAGS +#define BLOCK_SORT_EXTERNAL_SIZE(blockSize) ((((blockSize) + 31) >> 5)) +#else +#define BLOCK_SORT_EXTERNAL_SIZE(blockSize) 0 +#endif + +#define BLOCK_SORT_BUF_SIZE(blockSize) ((blockSize) * 2 + BLOCK_SORT_EXTERNAL_SIZE(blockSize) + (1 << 16)) + +UInt32 BlockSort(UInt32 *indices, const Byte *data, UInt32 blockSize); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/CpuArch.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/CpuArch.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,69 @@ +/* CpuArch.h +2008-08-05 +Igor Pavlov +Public domain */ + +#ifndef __CPUARCH_H +#define __CPUARCH_H + +/* +LITTLE_ENDIAN_UNALIGN means: + 1) CPU is LITTLE_ENDIAN + 2) it's allowed to make unaligned memory accesses +if LITTLE_ENDIAN_UNALIGN is not defined, it means that we don't know +about these properties of platform. +*/ + +#if defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(__x86_64__) +#define LITTLE_ENDIAN_UNALIGN +#endif + +#ifdef LITTLE_ENDIAN_UNALIGN + +#define GetUi16(p) (*(const UInt16 *)(p)) +#define GetUi32(p) (*(const UInt32 *)(p)) +#define GetUi64(p) (*(const UInt64 *)(p)) +#define SetUi32(p, d) *(UInt32 *)(p) = (d); + +#else + +#define GetUi16(p) (((const Byte *)(p))[0] | ((UInt16)((const Byte *)(p))[1] << 8)) + +#define GetUi32(p) ( \ + ((const Byte *)(p))[0] | \ + ((UInt32)((const Byte *)(p))[1] << 8) | \ + ((UInt32)((const Byte *)(p))[2] << 16) | \ + ((UInt32)((const Byte *)(p))[3] << 24)) + +#define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32)) + +#define SetUi32(p, d) { UInt32 _x_ = (d); \ + ((Byte *)(p))[0] = (Byte)_x_; \ + ((Byte *)(p))[1] = (Byte)(_x_ >> 8); \ + ((Byte *)(p))[2] = (Byte)(_x_ >> 16); \ + ((Byte *)(p))[3] = (Byte)(_x_ >> 24); } + +#endif + +#if defined(LITTLE_ENDIAN_UNALIGN) && defined(_WIN64) && (_MSC_VER >= 1300) + +#pragma intrinsic(_byteswap_ulong) +#pragma intrinsic(_byteswap_uint64) +#define GetBe32(p) _byteswap_ulong(*(const UInt32 *)(const Byte *)(p)) +#define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const Byte *)(p)) + +#else + +#define GetBe32(p) ( \ + ((UInt32)((const Byte *)(p))[0] << 24) | \ + ((UInt32)((const Byte *)(p))[1] << 16) | \ + ((UInt32)((const Byte *)(p))[2] << 8) | \ + ((const Byte *)(p))[3] ) + +#define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4)) + +#endif + +#define GetBe16(p) (((UInt16)((const Byte *)(p))[0] << 8) | ((const Byte *)(p))[1]) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/HuffEnc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/HuffEnc.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,148 @@ +/* HuffEnc.c -- functions for Huffman encoding +2008-08-05 +Igor Pavlov +Public domain */ + +#include "HuffEnc.h" +#include "Sort.h" + +#define kMaxLen 16 +#define NUM_BITS 10 +#define MASK ((1 << NUM_BITS) - 1) + +#define NUM_COUNTERS 64 + +#define HUFFMAN_SPEED_OPT + +void Huffman_Generate(const UInt32 *freqs, UInt32 *p, Byte *lens, UInt32 numSymbols, UInt32 maxLen) +{ + UInt32 num = 0; + /* if (maxLen > 10) maxLen = 10; */ + { + UInt32 i; + + #ifdef HUFFMAN_SPEED_OPT + + UInt32 counters[NUM_COUNTERS]; + for (i = 0; i < NUM_COUNTERS; i++) + counters[i] = 0; + for (i = 0; i < numSymbols; i++) + { + UInt32 freq = freqs[i]; + counters[(freq < NUM_COUNTERS - 1) ? freq : NUM_COUNTERS - 1]++; + } + + for (i = 1; i < NUM_COUNTERS; i++) + { + UInt32 temp = counters[i]; + counters[i] = num; + num += temp; + } + + for (i = 0; i < numSymbols; i++) + { + UInt32 freq = freqs[i]; + if (freq == 0) + lens[i] = 0; + else + p[counters[((freq < NUM_COUNTERS - 1) ? freq : NUM_COUNTERS - 1)]++] = i | (freq << NUM_BITS); + } + counters[0] = 0; + HeapSort(p + counters[NUM_COUNTERS - 2], counters[NUM_COUNTERS - 1] - counters[NUM_COUNTERS - 2]); + + #else + + for (i = 0; i < numSymbols; i++) + { + UInt32 freq = freqs[i]; + if (freq == 0) + lens[i] = 0; + else + p[num++] = i | (freq << NUM_BITS); + } + HeapSort(p, num); + + #endif + } + + if (num < 2) + { + int minCode = 0; + int maxCode = 1; + if (num == 1) + { + maxCode = (int)(p[0] & MASK); + if (maxCode == 0) + maxCode++; + } + p[minCode] = 0; + p[maxCode] = 1; + lens[minCode] = lens[maxCode] = 1; + return; + } + + { + UInt32 b, e, i; + + i = b = e = 0; + do + { + UInt32 n, m, freq; + n = (i != num && (b == e || (p[i] >> NUM_BITS) <= (p[b] >> NUM_BITS))) ? i++ : b++; + freq = (p[n] & ~MASK); + p[n] = (p[n] & MASK) | (e << NUM_BITS); + m = (i != num && (b == e || (p[i] >> NUM_BITS) <= (p[b] >> NUM_BITS))) ? i++ : b++; + freq += (p[m] & ~MASK); + p[m] = (p[m] & MASK) | (e << NUM_BITS); + p[e] = (p[e] & MASK) | freq; + e++; + } + while (num - e > 1); + + { + UInt32 lenCounters[kMaxLen + 1]; + for (i = 0; i <= kMaxLen; i++) + lenCounters[i] = 0; + + p[--e] &= MASK; + lenCounters[1] = 2; + while (e > 0) + { + UInt32 len = (p[p[--e] >> NUM_BITS] >> NUM_BITS) + 1; + p[e] = (p[e] & MASK) | (len << NUM_BITS); + if (len >= maxLen) + for (len = maxLen - 1; lenCounters[len] == 0; len--); + lenCounters[len]--; + lenCounters[len + 1] += 2; + } + + { + UInt32 len; + i = 0; + for (len = maxLen; len != 0; len--) + { + UInt32 num; + for (num = lenCounters[len]; num != 0; num--) + lens[p[i++] & MASK] = (Byte)len; + } + } + + { + UInt32 nextCodes[kMaxLen + 1]; + { + UInt32 code = 0; + UInt32 len; + for (len = 1; len <= kMaxLen; len++) + nextCodes[len] = code = (code + lenCounters[len - 1]) << 1; + } + /* if (code + lenCounters[kMaxLen] - 1 != (1 << kMaxLen) - 1) throw 1; */ + + { + UInt32 i; + for (i = 0; i < numSymbols; i++) + p[i] = nextCodes[lens[i]]++; + } + } + } + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/HuffEnc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/HuffEnc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,21 @@ +/* HuffEnc.h -- functions for Huffman encoding +2008-03-26 +Igor Pavlov +Public domain */ + +#ifndef __HUFFENC_H +#define __HUFFENC_H + +#include "Types.h" + +/* +Conditions: + num <= 1024 = 2 ^ NUM_BITS + Sum(freqs) < 4M = 2 ^ (32 - NUM_BITS) + maxLen <= 16 = kMaxLen + Num_Items(p) >= HUFFMAN_TEMP_SIZE(num) +*/ + +void Huffman_Generate(const UInt32 *freqs, UInt32 *p, Byte *lens, UInt32 num, UInt32 maxLen); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzFind.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzFind.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,751 @@ +/* LzFind.c -- Match finder for LZ algorithms +2008-10-04 : Igor Pavlov : Public domain */ + +#include + +#include "LzFind.h" +#include "LzHash.h" + +#define kEmptyHashValue 0 +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF) +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */ +#define kNormalizeMask (~(kNormalizeStepMin - 1)) +#define kMaxHistorySize ((UInt32)3 << 30) + +#define kStartMaxLen 3 + +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc) +{ + if (!p->directInput) + { + alloc->Free(alloc, p->bufferBase); + p->bufferBase = 0; + } +} + +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */ + +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc) +{ + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv; + if (p->directInput) + { + p->blockSize = blockSize; + return 1; + } + if (p->bufferBase == 0 || p->blockSize != blockSize) + { + LzInWindow_Free(p, alloc); + p->blockSize = blockSize; + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize); + } + return (p->bufferBase != 0); +} + +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; } +Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; } + +UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; } + +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue) +{ + p->posLimit -= subValue; + p->pos -= subValue; + p->streamPos -= subValue; +} + +static void MatchFinder_ReadBlock(CMatchFinder *p) +{ + if (p->streamEndWasReached || p->result != SZ_OK) + return; + for (;;) + { + Byte *dest = p->buffer + (p->streamPos - p->pos); + size_t size = (p->bufferBase + p->blockSize - dest); + if (size == 0) + return; + p->result = p->stream->Read(p->stream, dest, &size); + if (p->result != SZ_OK) + return; + if (size == 0) + { + p->streamEndWasReached = 1; + return; + } + p->streamPos += (UInt32)size; + if (p->streamPos - p->pos > p->keepSizeAfter) + return; + } +} + +void MatchFinder_MoveBlock(CMatchFinder *p) +{ + memmove(p->bufferBase, + p->buffer - p->keepSizeBefore, + (size_t)(p->streamPos - p->pos + p->keepSizeBefore)); + p->buffer = p->bufferBase + p->keepSizeBefore; +} + +int MatchFinder_NeedMove(CMatchFinder *p) +{ + /* if (p->streamEndWasReached) return 0; */ + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter); +} + +void MatchFinder_ReadIfRequired(CMatchFinder *p) +{ + if (p->streamEndWasReached) + return; + if (p->keepSizeAfter >= p->streamPos - p->pos) + MatchFinder_ReadBlock(p); +} + +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p) +{ + if (MatchFinder_NeedMove(p)) + MatchFinder_MoveBlock(p); + MatchFinder_ReadBlock(p); +} + +static void MatchFinder_SetDefaultSettings(CMatchFinder *p) +{ + p->cutValue = 32; + p->btMode = 1; + p->numHashBytes = 4; + /* p->skipModeBits = 0; */ + p->directInput = 0; + p->bigHash = 0; +} + +#define kCrcPoly 0xEDB88320 + +void MatchFinder_Construct(CMatchFinder *p) +{ + UInt32 i; + p->bufferBase = 0; + p->directInput = 0; + p->hash = 0; + MatchFinder_SetDefaultSettings(p); + + for (i = 0; i < 256; i++) + { + UInt32 r = i; + int j; + for (j = 0; j < 8; j++) + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1)); + p->crc[i] = r; + } +} + +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc) +{ + alloc->Free(alloc, p->hash); + p->hash = 0; +} + +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc) +{ + MatchFinder_FreeThisClassMemory(p, alloc); + LzInWindow_Free(p, alloc); +} + +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc) +{ + size_t sizeInBytes = (size_t)num * sizeof(CLzRef); + if (sizeInBytes / sizeof(CLzRef) != num) + return 0; + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes); +} + +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, + ISzAlloc *alloc) +{ + UInt32 sizeReserv; + if (historySize > kMaxHistorySize) + { + MatchFinder_Free(p, alloc); + return 0; + } + sizeReserv = historySize >> 1; + if (historySize > ((UInt32)2 << 30)) + sizeReserv = historySize >> 2; + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19); + + p->keepSizeBefore = historySize + keepAddBufferBefore + 1; + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter; + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */ + if (LzInWindow_Create(p, sizeReserv, alloc)) + { + UInt32 newCyclicBufferSize = (historySize /* >> p->skipModeBits */) + 1; + UInt32 hs; + p->matchMaxLen = matchMaxLen; + { + p->fixedHashSize = 0; + if (p->numHashBytes == 2) + hs = (1 << 16) - 1; + else + { + hs = historySize - 1; + hs |= (hs >> 1); + hs |= (hs >> 2); + hs |= (hs >> 4); + hs |= (hs >> 8); + hs >>= 1; + /* hs >>= p->skipModeBits; */ + hs |= 0xFFFF; /* don't change it! It's required for Deflate */ + if (hs > (1 << 24)) + { + if (p->numHashBytes == 3) + hs = (1 << 24) - 1; + else + hs >>= 1; + } + } + p->hashMask = hs; + hs++; + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size; + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size; + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size; + hs += p->fixedHashSize; + } + + { + UInt32 prevSize = p->hashSizeSum + p->numSons; + UInt32 newSize; + p->historySize = historySize; + p->hashSizeSum = hs; + p->cyclicBufferSize = newCyclicBufferSize; + p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize); + newSize = p->hashSizeSum + p->numSons; + if (p->hash != 0 && prevSize == newSize) + return 1; + MatchFinder_FreeThisClassMemory(p, alloc); + p->hash = AllocRefs(newSize, alloc); + if (p->hash != 0) + { + p->son = p->hash + p->hashSizeSum; + return 1; + } + } + } + MatchFinder_Free(p, alloc); + return 0; +} + +static void MatchFinder_SetLimits(CMatchFinder *p) +{ + UInt32 limit = kMaxValForNormalize - p->pos; + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos; + if (limit2 < limit) + limit = limit2; + limit2 = p->streamPos - p->pos; + if (limit2 <= p->keepSizeAfter) + { + if (limit2 > 0) + limit2 = 1; + } + else + limit2 -= p->keepSizeAfter; + if (limit2 < limit) + limit = limit2; + { + UInt32 lenLimit = p->streamPos - p->pos; + if (lenLimit > p->matchMaxLen) + lenLimit = p->matchMaxLen; + p->lenLimit = lenLimit; + } + p->posLimit = p->pos + limit; +} + +void MatchFinder_Init(CMatchFinder *p) +{ + UInt32 i; + for (i = 0; i < p->hashSizeSum; i++) + p->hash[i] = kEmptyHashValue; + p->cyclicBufferPos = 0; + p->buffer = p->bufferBase; + p->pos = p->streamPos = p->cyclicBufferSize; + p->result = SZ_OK; + p->streamEndWasReached = 0; + MatchFinder_ReadBlock(p); + MatchFinder_SetLimits(p); +} + +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p) +{ + return (p->pos - p->historySize - 1) & kNormalizeMask; +} + +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems) +{ + UInt32 i; + for (i = 0; i < numItems; i++) + { + UInt32 value = items[i]; + if (value <= subValue) + value = kEmptyHashValue; + else + value -= subValue; + items[i] = value; + } +} + +static void MatchFinder_Normalize(CMatchFinder *p) +{ + UInt32 subValue = MatchFinder_GetSubValue(p); + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons); + MatchFinder_ReduceOffsets(p, subValue); +} + +static void MatchFinder_CheckLimits(CMatchFinder *p) +{ + if (p->pos == kMaxValForNormalize) + MatchFinder_Normalize(p); + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos) + MatchFinder_CheckAndMoveAndRead(p); + if (p->cyclicBufferPos == p->cyclicBufferSize) + p->cyclicBufferPos = 0; + MatchFinder_SetLimits(p); +} + +static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue, + UInt32 *distances, UInt32 maxLen) +{ + son[_cyclicBufferPos] = curMatch; + for (;;) + { + UInt32 delta = pos - curMatch; + if (cutValue-- == 0 || delta >= _cyclicBufferSize) + return distances; + { + const Byte *pb = cur - delta; + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)]; + if (pb[maxLen] == cur[maxLen] && *pb == *cur) + { + UInt32 len = 0; + while (++len != lenLimit) + if (pb[len] != cur[len]) + break; + if (maxLen < len) + { + *distances++ = maxLen = len; + *distances++ = delta - 1; + if (len == lenLimit) + return distances; + } + } + } + } +} + +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue, + UInt32 *distances, UInt32 maxLen) +{ + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1; + CLzRef *ptr1 = son + (_cyclicBufferPos << 1); + UInt32 len0 = 0, len1 = 0; + for (;;) + { + UInt32 delta = pos - curMatch; + if (cutValue-- == 0 || delta >= _cyclicBufferSize) + { + *ptr0 = *ptr1 = kEmptyHashValue; + return distances; + } + { + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1); + const Byte *pb = cur - delta; + UInt32 len = (len0 < len1 ? len0 : len1); + if (pb[len] == cur[len]) + { + if (++len != lenLimit && pb[len] == cur[len]) + while (++len != lenLimit) + if (pb[len] != cur[len]) + break; + if (maxLen < len) + { + *distances++ = maxLen = len; + *distances++ = delta - 1; + if (len == lenLimit) + { + *ptr1 = pair[0]; + *ptr0 = pair[1]; + return distances; + } + } + } + if (pb[len] < cur[len]) + { + *ptr1 = curMatch; + ptr1 = pair + 1; + curMatch = *ptr1; + len1 = len; + } + else + { + *ptr0 = curMatch; + ptr0 = pair; + curMatch = *ptr0; + len0 = len; + } + } + } +} + +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue) +{ + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1; + CLzRef *ptr1 = son + (_cyclicBufferPos << 1); + UInt32 len0 = 0, len1 = 0; + for (;;) + { + UInt32 delta = pos - curMatch; + if (cutValue-- == 0 || delta >= _cyclicBufferSize) + { + *ptr0 = *ptr1 = kEmptyHashValue; + return; + } + { + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1); + const Byte *pb = cur - delta; + UInt32 len = (len0 < len1 ? len0 : len1); + if (pb[len] == cur[len]) + { + while (++len != lenLimit) + if (pb[len] != cur[len]) + break; + { + if (len == lenLimit) + { + *ptr1 = pair[0]; + *ptr0 = pair[1]; + return; + } + } + } + if (pb[len] < cur[len]) + { + *ptr1 = curMatch; + ptr1 = pair + 1; + curMatch = *ptr1; + len1 = len; + } + else + { + *ptr0 = curMatch; + ptr0 = pair; + curMatch = *ptr0; + len0 = len; + } + } + } +} + +#define MOVE_POS \ + ++p->cyclicBufferPos; \ + p->buffer++; \ + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p); + +#define MOVE_POS_RET MOVE_POS return offset; + +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; } + +#define GET_MATCHES_HEADER2(minLen, ret_op) \ + UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \ + lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \ + cur = p->buffer; + +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0) +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue) + +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue + +#define GET_MATCHES_FOOTER(offset, maxLen) \ + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \ + distances + offset, maxLen) - distances); MOVE_POS_RET; + +#define SKIP_FOOTER \ + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS; + +static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) +{ + UInt32 offset; + GET_MATCHES_HEADER(2) + HASH2_CALC; + curMatch = p->hash[hashValue]; + p->hash[hashValue] = p->pos; + offset = 0; + GET_MATCHES_FOOTER(offset, 1) +} + +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) +{ + UInt32 offset; + GET_MATCHES_HEADER(3) + HASH_ZIP_CALC; + curMatch = p->hash[hashValue]; + p->hash[hashValue] = p->pos; + offset = 0; + GET_MATCHES_FOOTER(offset, 2) +} + +static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) +{ + UInt32 hash2Value, delta2, maxLen, offset; + GET_MATCHES_HEADER(3) + + HASH3_CALC; + + delta2 = p->pos - p->hash[hash2Value]; + curMatch = p->hash[kFix3HashSize + hashValue]; + + p->hash[hash2Value] = + p->hash[kFix3HashSize + hashValue] = p->pos; + + + maxLen = 2; + offset = 0; + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur) + { + for (; maxLen != lenLimit; maxLen++) + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen]) + break; + distances[0] = maxLen; + distances[1] = delta2 - 1; + offset = 2; + if (maxLen == lenLimit) + { + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); + MOVE_POS_RET; + } + } + GET_MATCHES_FOOTER(offset, maxLen) +} + +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) +{ + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset; + GET_MATCHES_HEADER(4) + + HASH4_CALC; + + delta2 = p->pos - p->hash[ hash2Value]; + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value]; + curMatch = p->hash[kFix4HashSize + hashValue]; + + p->hash[ hash2Value] = + p->hash[kFix3HashSize + hash3Value] = + p->hash[kFix4HashSize + hashValue] = p->pos; + + maxLen = 1; + offset = 0; + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur) + { + distances[0] = maxLen = 2; + distances[1] = delta2 - 1; + offset = 2; + } + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur) + { + maxLen = 3; + distances[offset + 1] = delta3 - 1; + offset += 2; + delta2 = delta3; + } + if (offset != 0) + { + for (; maxLen != lenLimit; maxLen++) + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen]) + break; + distances[offset - 2] = maxLen; + if (maxLen == lenLimit) + { + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); + MOVE_POS_RET; + } + } + if (maxLen < 3) + maxLen = 3; + GET_MATCHES_FOOTER(offset, maxLen) +} + +static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) +{ + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset; + GET_MATCHES_HEADER(4) + + HASH4_CALC; + + delta2 = p->pos - p->hash[ hash2Value]; + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value]; + curMatch = p->hash[kFix4HashSize + hashValue]; + + p->hash[ hash2Value] = + p->hash[kFix3HashSize + hash3Value] = + p->hash[kFix4HashSize + hashValue] = p->pos; + + maxLen = 1; + offset = 0; + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur) + { + distances[0] = maxLen = 2; + distances[1] = delta2 - 1; + offset = 2; + } + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur) + { + maxLen = 3; + distances[offset + 1] = delta3 - 1; + offset += 2; + delta2 = delta3; + } + if (offset != 0) + { + for (; maxLen != lenLimit; maxLen++) + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen]) + break; + distances[offset - 2] = maxLen; + if (maxLen == lenLimit) + { + p->son[p->cyclicBufferPos] = curMatch; + MOVE_POS_RET; + } + } + if (maxLen < 3) + maxLen = 3; + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p), + distances + offset, maxLen) - (distances)); + MOVE_POS_RET +} + +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) +{ + UInt32 offset; + GET_MATCHES_HEADER(3) + HASH_ZIP_CALC; + curMatch = p->hash[hashValue]; + p->hash[hashValue] = p->pos; + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p), + distances, 2) - (distances)); + MOVE_POS_RET +} + +static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num) +{ + do + { + SKIP_HEADER(2) + HASH2_CALC; + curMatch = p->hash[hashValue]; + p->hash[hashValue] = p->pos; + SKIP_FOOTER + } + while (--num != 0); +} + +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num) +{ + do + { + SKIP_HEADER(3) + HASH_ZIP_CALC; + curMatch = p->hash[hashValue]; + p->hash[hashValue] = p->pos; + SKIP_FOOTER + } + while (--num != 0); +} + +static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num) +{ + do + { + UInt32 hash2Value; + SKIP_HEADER(3) + HASH3_CALC; + curMatch = p->hash[kFix3HashSize + hashValue]; + p->hash[hash2Value] = + p->hash[kFix3HashSize + hashValue] = p->pos; + SKIP_FOOTER + } + while (--num != 0); +} + +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num) +{ + do + { + UInt32 hash2Value, hash3Value; + SKIP_HEADER(4) + HASH4_CALC; + curMatch = p->hash[kFix4HashSize + hashValue]; + p->hash[ hash2Value] = + p->hash[kFix3HashSize + hash3Value] = p->pos; + p->hash[kFix4HashSize + hashValue] = p->pos; + SKIP_FOOTER + } + while (--num != 0); +} + +static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num) +{ + do + { + UInt32 hash2Value, hash3Value; + SKIP_HEADER(4) + HASH4_CALC; + curMatch = p->hash[kFix4HashSize + hashValue]; + p->hash[ hash2Value] = + p->hash[kFix3HashSize + hash3Value] = + p->hash[kFix4HashSize + hashValue] = p->pos; + p->son[p->cyclicBufferPos] = curMatch; + MOVE_POS + } + while (--num != 0); +} + +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num) +{ + do + { + SKIP_HEADER(3) + HASH_ZIP_CALC; + curMatch = p->hash[hashValue]; + p->hash[hashValue] = p->pos; + p->son[p->cyclicBufferPos] = curMatch; + MOVE_POS + } + while (--num != 0); +} + +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable) +{ + vTable->Init = (Mf_Init_Func)MatchFinder_Init; + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte; + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes; + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos; + if (!p->btMode) + { + vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip; + } + else if (p->numHashBytes == 2) + { + vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip; + } + else if (p->numHashBytes == 3) + { + vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip; + } + else + { + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip; + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzFind.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzFind.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,107 @@ +/* LzFind.h -- Match finder for LZ algorithms +2008-10-04 : Igor Pavlov : Public domain */ + +#ifndef __LZFIND_H +#define __LZFIND_H + +#include "Types.h" + +typedef UInt32 CLzRef; + +typedef struct _CMatchFinder +{ + Byte *buffer; + UInt32 pos; + UInt32 posLimit; + UInt32 streamPos; + UInt32 lenLimit; + + UInt32 cyclicBufferPos; + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */ + + UInt32 matchMaxLen; + CLzRef *hash; + CLzRef *son; + UInt32 hashMask; + UInt32 cutValue; + + Byte *bufferBase; + ISeqInStream *stream; + int streamEndWasReached; + + UInt32 blockSize; + UInt32 keepSizeBefore; + UInt32 keepSizeAfter; + + UInt32 numHashBytes; + int directInput; + int btMode; + /* int skipModeBits; */ + int bigHash; + UInt32 historySize; + UInt32 fixedHashSize; + UInt32 hashSizeSum; + UInt32 numSons; + SRes result; + UInt32 crc[256]; +} CMatchFinder; + +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer) +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)]) + +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos) + +int MatchFinder_NeedMove(CMatchFinder *p); +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p); +void MatchFinder_MoveBlock(CMatchFinder *p); +void MatchFinder_ReadIfRequired(CMatchFinder *p); + +void MatchFinder_Construct(CMatchFinder *p); + +/* Conditions: + historySize <= 3 GB + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB +*/ +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize, + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, + ISzAlloc *alloc); +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc); +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems); +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue); + +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue, + UInt32 *distances, UInt32 maxLen); + +/* +Conditions: + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func. + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function +*/ + +typedef void (*Mf_Init_Func)(void *object); +typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index); +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object); +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object); +typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances); +typedef void (*Mf_Skip_Func)(void *object, UInt32); + +typedef struct _IMatchFinder +{ + Mf_Init_Func Init; + Mf_GetIndexByte_Func GetIndexByte; + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes; + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos; + Mf_GetMatches_Func GetMatches; + Mf_Skip_Func Skip; +} IMatchFinder; + +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable); + +void MatchFinder_Init(CMatchFinder *p); +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances); +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzFindMt.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzFindMt.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,793 @@ +/* LzFindMt.c -- multithreaded Match finder for LZ algorithms +2008-10-04 : Igor Pavlov : Public domain */ + +#include "LzHash.h" + +#include "LzFindMt.h" + +void MtSync_Construct(CMtSync *p) +{ + p->wasCreated = False; + p->csWasInitialized = False; + p->csWasEntered = False; + Thread_Construct(&p->thread); + Event_Construct(&p->canStart); + Event_Construct(&p->wasStarted); + Event_Construct(&p->wasStopped); + Semaphore_Construct(&p->freeSemaphore); + Semaphore_Construct(&p->filledSemaphore); +} + +void MtSync_GetNextBlock(CMtSync *p) +{ + if (p->needStart) + { + p->numProcessedBlocks = 1; + p->needStart = False; + p->stopWriting = False; + p->exit = False; + Event_Reset(&p->wasStarted); + Event_Reset(&p->wasStopped); + + Event_Set(&p->canStart); + Event_Wait(&p->wasStarted); + } + else + { + CriticalSection_Leave(&p->cs); + p->csWasEntered = False; + p->numProcessedBlocks++; + Semaphore_Release1(&p->freeSemaphore); + } + Semaphore_Wait(&p->filledSemaphore); + CriticalSection_Enter(&p->cs); + p->csWasEntered = True; +} + +/* MtSync_StopWriting must be called if Writing was started */ + +void MtSync_StopWriting(CMtSync *p) +{ + UInt32 myNumBlocks = p->numProcessedBlocks; + if (!Thread_WasCreated(&p->thread) || p->needStart) + return; + p->stopWriting = True; + if (p->csWasEntered) + { + CriticalSection_Leave(&p->cs); + p->csWasEntered = False; + } + Semaphore_Release1(&p->freeSemaphore); + + Event_Wait(&p->wasStopped); + + while (myNumBlocks++ != p->numProcessedBlocks) + { + Semaphore_Wait(&p->filledSemaphore); + Semaphore_Release1(&p->freeSemaphore); + } + p->needStart = True; +} + +void MtSync_Destruct(CMtSync *p) +{ + if (Thread_WasCreated(&p->thread)) + { + MtSync_StopWriting(p); + p->exit = True; + if (p->needStart) + Event_Set(&p->canStart); + Thread_Wait(&p->thread); + Thread_Close(&p->thread); + } + if (p->csWasInitialized) + { + CriticalSection_Delete(&p->cs); + p->csWasInitialized = False; + } + + Event_Close(&p->canStart); + Event_Close(&p->wasStarted); + Event_Close(&p->wasStopped); + Semaphore_Close(&p->freeSemaphore); + Semaphore_Close(&p->filledSemaphore); + + p->wasCreated = False; +} + +#define RINOK_THREAD(x) { if ((x) != 0) return SZ_ERROR_THREAD; } + +static SRes MtSync_Create2(CMtSync *p, unsigned (MY_STD_CALL *startAddress)(void *), void *obj, UInt32 numBlocks) +{ + if (p->wasCreated) + return SZ_OK; + + RINOK_THREAD(CriticalSection_Init(&p->cs)); + p->csWasInitialized = True; + + RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->canStart)); + RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->wasStarted)); + RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->wasStopped)); + + RINOK_THREAD(Semaphore_Create(&p->freeSemaphore, numBlocks, numBlocks)); + RINOK_THREAD(Semaphore_Create(&p->filledSemaphore, 0, numBlocks)); + + p->needStart = True; + + RINOK_THREAD(Thread_Create(&p->thread, startAddress, obj)); + p->wasCreated = True; + return SZ_OK; +} + +static SRes MtSync_Create(CMtSync *p, unsigned (MY_STD_CALL *startAddress)(void *), void *obj, UInt32 numBlocks) +{ + SRes res = MtSync_Create2(p, startAddress, obj, numBlocks); + if (res != SZ_OK) + MtSync_Destruct(p); + return res; +} + +void MtSync_Init(CMtSync *p) { p->needStart = True; } + +#define kMtMaxValForNormalize 0xFFFFFFFF + +#define DEF_GetHeads2(name, v, action) \ +static void GetHeads ## name(const Byte *p, UInt32 pos, \ +UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc) \ +{ action; for (; numHeads != 0; numHeads--) { \ +const UInt32 value = (v); p++; *heads++ = pos - hash[value]; hash[value] = pos++; } } + +#define DEF_GetHeads(name, v) DEF_GetHeads2(name, v, ;) + +DEF_GetHeads2(2, (p[0] | ((UInt32)p[1] << 8)), hashMask = hashMask; crc = crc; ) +DEF_GetHeads(3, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8)) & hashMask) +DEF_GetHeads(4, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ (crc[p[3]] << 5)) & hashMask) +DEF_GetHeads(4b, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ ((UInt32)p[3] << 16)) & hashMask) +DEF_GetHeads(5, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ (crc[p[3]] << 5) ^ (crc[p[4]] << 3)) & hashMask) + +void HashThreadFunc(CMatchFinderMt *mt) +{ + CMtSync *p = &mt->hashSync; + for (;;) + { + UInt32 numProcessedBlocks = 0; + Event_Wait(&p->canStart); + Event_Set(&p->wasStarted); + for (;;) + { + if (p->exit) + return; + if (p->stopWriting) + { + p->numProcessedBlocks = numProcessedBlocks; + Event_Set(&p->wasStopped); + break; + } + + { + CMatchFinder *mf = mt->MatchFinder; + if (MatchFinder_NeedMove(mf)) + { + CriticalSection_Enter(&mt->btSync.cs); + CriticalSection_Enter(&mt->hashSync.cs); + { + const Byte *beforePtr = MatchFinder_GetPointerToCurrentPos(mf); + const Byte *afterPtr; + MatchFinder_MoveBlock(mf); + afterPtr = MatchFinder_GetPointerToCurrentPos(mf); + mt->pointerToCurPos -= beforePtr - afterPtr; + mt->buffer -= beforePtr - afterPtr; + } + CriticalSection_Leave(&mt->btSync.cs); + CriticalSection_Leave(&mt->hashSync.cs); + continue; + } + + Semaphore_Wait(&p->freeSemaphore); + + MatchFinder_ReadIfRequired(mf); + if (mf->pos > (kMtMaxValForNormalize - kMtHashBlockSize)) + { + UInt32 subValue = (mf->pos - mf->historySize - 1); + MatchFinder_ReduceOffsets(mf, subValue); + MatchFinder_Normalize3(subValue, mf->hash + mf->fixedHashSize, mf->hashMask + 1); + } + { + UInt32 *heads = mt->hashBuf + ((numProcessedBlocks++) & kMtHashNumBlocksMask) * kMtHashBlockSize; + UInt32 num = mf->streamPos - mf->pos; + heads[0] = 2; + heads[1] = num; + if (num >= mf->numHashBytes) + { + num = num - mf->numHashBytes + 1; + if (num > kMtHashBlockSize - 2) + num = kMtHashBlockSize - 2; + mt->GetHeadsFunc(mf->buffer, mf->pos, mf->hash + mf->fixedHashSize, mf->hashMask, heads + 2, num, mf->crc); + heads[0] += num; + } + mf->pos += num; + mf->buffer += num; + } + } + + Semaphore_Release1(&p->filledSemaphore); + } + } +} + +void MatchFinderMt_GetNextBlock_Hash(CMatchFinderMt *p) +{ + MtSync_GetNextBlock(&p->hashSync); + p->hashBufPosLimit = p->hashBufPos = ((p->hashSync.numProcessedBlocks - 1) & kMtHashNumBlocksMask) * kMtHashBlockSize; + p->hashBufPosLimit += p->hashBuf[p->hashBufPos++]; + p->hashNumAvail = p->hashBuf[p->hashBufPos++]; +} + +#define kEmptyHashValue 0 + +/* #define MFMT_GM_INLINE */ + +#ifdef MFMT_GM_INLINE + +#define NO_INLINE MY_FAST_CALL + +Int32 NO_INLINE GetMatchesSpecN(UInt32 lenLimit, UInt32 pos, const Byte *cur, CLzRef *son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue, + UInt32 *_distances, UInt32 _maxLen, const UInt32 *hash, Int32 limit, UInt32 size, UInt32 *posRes) +{ + do + { + UInt32 *distances = _distances + 1; + UInt32 curMatch = pos - *hash++; + + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1; + CLzRef *ptr1 = son + (_cyclicBufferPos << 1); + UInt32 len0 = 0, len1 = 0; + UInt32 cutValue = _cutValue; + UInt32 maxLen = _maxLen; + for (;;) + { + UInt32 delta = pos - curMatch; + if (cutValue-- == 0 || delta >= _cyclicBufferSize) + { + *ptr0 = *ptr1 = kEmptyHashValue; + break; + } + { + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1); + const Byte *pb = cur - delta; + UInt32 len = (len0 < len1 ? len0 : len1); + if (pb[len] == cur[len]) + { + if (++len != lenLimit && pb[len] == cur[len]) + while (++len != lenLimit) + if (pb[len] != cur[len]) + break; + if (maxLen < len) + { + *distances++ = maxLen = len; + *distances++ = delta - 1; + if (len == lenLimit) + { + *ptr1 = pair[0]; + *ptr0 = pair[1]; + break; + } + } + } + if (pb[len] < cur[len]) + { + *ptr1 = curMatch; + ptr1 = pair + 1; + curMatch = *ptr1; + len1 = len; + } + else + { + *ptr0 = curMatch; + ptr0 = pair; + curMatch = *ptr0; + len0 = len; + } + } + } + pos++; + _cyclicBufferPos++; + cur++; + { + UInt32 num = (UInt32)(distances - _distances); + *_distances = num - 1; + _distances += num; + limit -= num; + } + } + while (limit > 0 && --size != 0); + *posRes = pos; + return limit; +} + +#endif + +void BtGetMatches(CMatchFinderMt *p, UInt32 *distances) +{ + UInt32 numProcessed = 0; + UInt32 curPos = 2; + UInt32 limit = kMtBtBlockSize - (p->matchMaxLen * 2); + distances[1] = p->hashNumAvail; + while (curPos < limit) + { + if (p->hashBufPos == p->hashBufPosLimit) + { + MatchFinderMt_GetNextBlock_Hash(p); + distances[1] = numProcessed + p->hashNumAvail; + if (p->hashNumAvail >= p->numHashBytes) + continue; + for (; p->hashNumAvail != 0; p->hashNumAvail--) + distances[curPos++] = 0; + break; + } + { + UInt32 size = p->hashBufPosLimit - p->hashBufPos; + UInt32 lenLimit = p->matchMaxLen; + UInt32 pos = p->pos; + UInt32 cyclicBufferPos = p->cyclicBufferPos; + if (lenLimit >= p->hashNumAvail) + lenLimit = p->hashNumAvail; + { + UInt32 size2 = p->hashNumAvail - lenLimit + 1; + if (size2 < size) + size = size2; + size2 = p->cyclicBufferSize - cyclicBufferPos; + if (size2 < size) + size = size2; + } + #ifndef MFMT_GM_INLINE + while (curPos < limit && size-- != 0) + { + UInt32 *startDistances = distances + curPos; + UInt32 num = (UInt32)(GetMatchesSpec1(lenLimit, pos - p->hashBuf[p->hashBufPos++], + pos, p->buffer, p->son, cyclicBufferPos, p->cyclicBufferSize, p->cutValue, + startDistances + 1, p->numHashBytes - 1) - startDistances); + *startDistances = num - 1; + curPos += num; + cyclicBufferPos++; + pos++; + p->buffer++; + } + #else + { + UInt32 posRes; + curPos = limit - GetMatchesSpecN(lenLimit, pos, p->buffer, p->son, cyclicBufferPos, p->cyclicBufferSize, p->cutValue, + distances + curPos, p->numHashBytes - 1, p->hashBuf + p->hashBufPos, (Int32)(limit - curPos) , size, &posRes); + p->hashBufPos += posRes - pos; + cyclicBufferPos += posRes - pos; + p->buffer += posRes - pos; + pos = posRes; + } + #endif + + numProcessed += pos - p->pos; + p->hashNumAvail -= pos - p->pos; + p->pos = pos; + if (cyclicBufferPos == p->cyclicBufferSize) + cyclicBufferPos = 0; + p->cyclicBufferPos = cyclicBufferPos; + } + } + distances[0] = curPos; +} + +void BtFillBlock(CMatchFinderMt *p, UInt32 globalBlockIndex) +{ + CMtSync *sync = &p->hashSync; + if (!sync->needStart) + { + CriticalSection_Enter(&sync->cs); + sync->csWasEntered = True; + } + + BtGetMatches(p, p->btBuf + (globalBlockIndex & kMtBtNumBlocksMask) * kMtBtBlockSize); + + if (p->pos > kMtMaxValForNormalize - kMtBtBlockSize) + { + UInt32 subValue = p->pos - p->cyclicBufferSize; + MatchFinder_Normalize3(subValue, p->son, p->cyclicBufferSize * 2); + p->pos -= subValue; + } + + if (!sync->needStart) + { + CriticalSection_Leave(&sync->cs); + sync->csWasEntered = False; + } +} + +void BtThreadFunc(CMatchFinderMt *mt) +{ + CMtSync *p = &mt->btSync; + for (;;) + { + UInt32 blockIndex = 0; + Event_Wait(&p->canStart); + Event_Set(&p->wasStarted); + for (;;) + { + if (p->exit) + return; + if (p->stopWriting) + { + p->numProcessedBlocks = blockIndex; + MtSync_StopWriting(&mt->hashSync); + Event_Set(&p->wasStopped); + break; + } + Semaphore_Wait(&p->freeSemaphore); + BtFillBlock(mt, blockIndex++); + Semaphore_Release1(&p->filledSemaphore); + } + } +} + +void MatchFinderMt_Construct(CMatchFinderMt *p) +{ + p->hashBuf = 0; + MtSync_Construct(&p->hashSync); + MtSync_Construct(&p->btSync); +} + +void MatchFinderMt_FreeMem(CMatchFinderMt *p, ISzAlloc *alloc) +{ + alloc->Free(alloc, p->hashBuf); + p->hashBuf = 0; +} + +void MatchFinderMt_Destruct(CMatchFinderMt *p, ISzAlloc *alloc) +{ + MtSync_Destruct(&p->hashSync); + MtSync_Destruct(&p->btSync); + MatchFinderMt_FreeMem(p, alloc); +} + +#define kHashBufferSize (kMtHashBlockSize * kMtHashNumBlocks) +#define kBtBufferSize (kMtBtBlockSize * kMtBtNumBlocks) + +static unsigned MY_STD_CALL HashThreadFunc2(void *p) { HashThreadFunc((CMatchFinderMt *)p); return 0; } +static unsigned MY_STD_CALL BtThreadFunc2(void *p) +{ + Byte allocaDummy[0x180]; + int i = 0; + for (i = 0; i < 16; i++) + allocaDummy[i] = (Byte)i; + BtThreadFunc((CMatchFinderMt *)p); + return 0; +} + +SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddBufferBefore, + UInt32 matchMaxLen, UInt32 keepAddBufferAfter, ISzAlloc *alloc) +{ + CMatchFinder *mf = p->MatchFinder; + p->historySize = historySize; + if (kMtBtBlockSize <= matchMaxLen * 4) + return SZ_ERROR_PARAM; + if (p->hashBuf == 0) + { + p->hashBuf = (UInt32 *)alloc->Alloc(alloc, (kHashBufferSize + kBtBufferSize) * sizeof(UInt32)); + if (p->hashBuf == 0) + return SZ_ERROR_MEM; + p->btBuf = p->hashBuf + kHashBufferSize; + } + keepAddBufferBefore += (kHashBufferSize + kBtBufferSize); + keepAddBufferAfter += kMtHashBlockSize; + if (!MatchFinder_Create(mf, historySize, keepAddBufferBefore, matchMaxLen, keepAddBufferAfter, alloc)) + return SZ_ERROR_MEM; + + RINOK(MtSync_Create(&p->hashSync, HashThreadFunc2, p, kMtHashNumBlocks)); + RINOK(MtSync_Create(&p->btSync, BtThreadFunc2, p, kMtBtNumBlocks)); + return SZ_OK; +} + +/* Call it after ReleaseStream / SetStream */ +void MatchFinderMt_Init(CMatchFinderMt *p) +{ + CMatchFinder *mf = p->MatchFinder; + p->btBufPos = p->btBufPosLimit = 0; + p->hashBufPos = p->hashBufPosLimit = 0; + MatchFinder_Init(mf); + p->pointerToCurPos = MatchFinder_GetPointerToCurrentPos(mf); + p->btNumAvailBytes = 0; + p->lzPos = p->historySize + 1; + + p->hash = mf->hash; + p->fixedHashSize = mf->fixedHashSize; + p->crc = mf->crc; + + p->son = mf->son; + p->matchMaxLen = mf->matchMaxLen; + p->numHashBytes = mf->numHashBytes; + p->pos = mf->pos; + p->buffer = mf->buffer; + p->cyclicBufferPos = mf->cyclicBufferPos; + p->cyclicBufferSize = mf->cyclicBufferSize; + p->cutValue = mf->cutValue; +} + +/* ReleaseStream is required to finish multithreading */ +void MatchFinderMt_ReleaseStream(CMatchFinderMt *p) +{ + MtSync_StopWriting(&p->btSync); + /* p->MatchFinder->ReleaseStream(); */ +} + +void MatchFinderMt_Normalize(CMatchFinderMt *p) +{ + MatchFinder_Normalize3(p->lzPos - p->historySize - 1, p->hash, p->fixedHashSize); + p->lzPos = p->historySize + 1; +} + +void MatchFinderMt_GetNextBlock_Bt(CMatchFinderMt *p) +{ + UInt32 blockIndex; + MtSync_GetNextBlock(&p->btSync); + blockIndex = ((p->btSync.numProcessedBlocks - 1) & kMtBtNumBlocksMask); + p->btBufPosLimit = p->btBufPos = blockIndex * kMtBtBlockSize; + p->btBufPosLimit += p->btBuf[p->btBufPos++]; + p->btNumAvailBytes = p->btBuf[p->btBufPos++]; + if (p->lzPos >= kMtMaxValForNormalize - kMtBtBlockSize) + MatchFinderMt_Normalize(p); +} + +const Byte * MatchFinderMt_GetPointerToCurrentPos(CMatchFinderMt *p) +{ + return p->pointerToCurPos; +} + +#define GET_NEXT_BLOCK_IF_REQUIRED if (p->btBufPos == p->btBufPosLimit) MatchFinderMt_GetNextBlock_Bt(p); + +UInt32 MatchFinderMt_GetNumAvailableBytes(CMatchFinderMt *p) +{ + GET_NEXT_BLOCK_IF_REQUIRED; + return p->btNumAvailBytes; +} + +Byte MatchFinderMt_GetIndexByte(CMatchFinderMt *p, Int32 index) +{ + return p->pointerToCurPos[index]; +} + +UInt32 * MixMatches2(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances) +{ + UInt32 hash2Value, curMatch2; + UInt32 *hash = p->hash; + const Byte *cur = p->pointerToCurPos; + UInt32 lzPos = p->lzPos; + MT_HASH2_CALC + + curMatch2 = hash[hash2Value]; + hash[hash2Value] = lzPos; + + if (curMatch2 >= matchMinPos) + if (cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0]) + { + *distances++ = 2; + *distances++ = lzPos - curMatch2 - 1; + } + return distances; +} + +UInt32 * MixMatches3(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances) +{ + UInt32 hash2Value, hash3Value, curMatch2, curMatch3; + UInt32 *hash = p->hash; + const Byte *cur = p->pointerToCurPos; + UInt32 lzPos = p->lzPos; + MT_HASH3_CALC + + curMatch2 = hash[ hash2Value]; + curMatch3 = hash[kFix3HashSize + hash3Value]; + + hash[ hash2Value] = + hash[kFix3HashSize + hash3Value] = + lzPos; + + if (curMatch2 >= matchMinPos && cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0]) + { + distances[1] = lzPos - curMatch2 - 1; + if (cur[(ptrdiff_t)curMatch2 - lzPos + 2] == cur[2]) + { + distances[0] = 3; + return distances + 2; + } + distances[0] = 2; + distances += 2; + } + if (curMatch3 >= matchMinPos && cur[(ptrdiff_t)curMatch3 - lzPos] == cur[0]) + { + *distances++ = 3; + *distances++ = lzPos - curMatch3 - 1; + } + return distances; +} + +/* +UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances) +{ + UInt32 hash2Value, hash3Value, hash4Value, curMatch2, curMatch3, curMatch4; + UInt32 *hash = p->hash; + const Byte *cur = p->pointerToCurPos; + UInt32 lzPos = p->lzPos; + MT_HASH4_CALC + + curMatch2 = hash[ hash2Value]; + curMatch3 = hash[kFix3HashSize + hash3Value]; + curMatch4 = hash[kFix4HashSize + hash4Value]; + + hash[ hash2Value] = + hash[kFix3HashSize + hash3Value] = + hash[kFix4HashSize + hash4Value] = + lzPos; + + if (curMatch2 >= matchMinPos && cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0]) + { + distances[1] = lzPos - curMatch2 - 1; + if (cur[(ptrdiff_t)curMatch2 - lzPos + 2] == cur[2]) + { + distances[0] = (cur[(ptrdiff_t)curMatch2 - lzPos + 3] == cur[3]) ? 4 : 3; + return distances + 2; + } + distances[0] = 2; + distances += 2; + } + if (curMatch3 >= matchMinPos && cur[(ptrdiff_t)curMatch3 - lzPos] == cur[0]) + { + distances[1] = lzPos - curMatch3 - 1; + if (cur[(ptrdiff_t)curMatch3 - lzPos + 3] == cur[3]) + { + distances[0] = 4; + return distances + 2; + } + distances[0] = 3; + distances += 2; + } + + if (curMatch4 >= matchMinPos) + if ( + cur[(ptrdiff_t)curMatch4 - lzPos] == cur[0] && + cur[(ptrdiff_t)curMatch4 - lzPos + 3] == cur[3] + ) + { + *distances++ = 4; + *distances++ = lzPos - curMatch4 - 1; + } + return distances; +} +*/ + +#define INCREASE_LZ_POS p->lzPos++; p->pointerToCurPos++; + +UInt32 MatchFinderMt2_GetMatches(CMatchFinderMt *p, UInt32 *distances) +{ + const UInt32 *btBuf = p->btBuf + p->btBufPos; + UInt32 len = *btBuf++; + p->btBufPos += 1 + len; + p->btNumAvailBytes--; + { + UInt32 i; + for (i = 0; i < len; i += 2) + { + *distances++ = *btBuf++; + *distances++ = *btBuf++; + } + } + INCREASE_LZ_POS + return len; +} + +UInt32 MatchFinderMt_GetMatches(CMatchFinderMt *p, UInt32 *distances) +{ + const UInt32 *btBuf = p->btBuf + p->btBufPos; + UInt32 len = *btBuf++; + p->btBufPos += 1 + len; + + if (len == 0) + { + if (p->btNumAvailBytes-- >= 4) + len = (UInt32)(p->MixMatchesFunc(p, p->lzPos - p->historySize, distances) - (distances)); + } + else + { + /* Condition: there are matches in btBuf with length < p->numHashBytes */ + UInt32 *distances2; + p->btNumAvailBytes--; + distances2 = p->MixMatchesFunc(p, p->lzPos - btBuf[1], distances); + do + { + *distances2++ = *btBuf++; + *distances2++ = *btBuf++; + } + while ((len -= 2) != 0); + len = (UInt32)(distances2 - (distances)); + } + INCREASE_LZ_POS + return len; +} + +#define SKIP_HEADER2 do { GET_NEXT_BLOCK_IF_REQUIRED +#define SKIP_HEADER(n) SKIP_HEADER2 if (p->btNumAvailBytes-- >= (n)) { const Byte *cur = p->pointerToCurPos; UInt32 *hash = p->hash; +#define SKIP_FOOTER } INCREASE_LZ_POS p->btBufPos += p->btBuf[p->btBufPos] + 1; } while (--num != 0); + +void MatchFinderMt0_Skip(CMatchFinderMt *p, UInt32 num) +{ + SKIP_HEADER2 { p->btNumAvailBytes--; + SKIP_FOOTER +} + +void MatchFinderMt2_Skip(CMatchFinderMt *p, UInt32 num) +{ + SKIP_HEADER(2) + UInt32 hash2Value; + MT_HASH2_CALC + hash[hash2Value] = p->lzPos; + SKIP_FOOTER +} + +void MatchFinderMt3_Skip(CMatchFinderMt *p, UInt32 num) +{ + SKIP_HEADER(3) + UInt32 hash2Value, hash3Value; + MT_HASH3_CALC + hash[kFix3HashSize + hash3Value] = + hash[ hash2Value] = + p->lzPos; + SKIP_FOOTER +} + +/* +void MatchFinderMt4_Skip(CMatchFinderMt *p, UInt32 num) +{ + SKIP_HEADER(4) + UInt32 hash2Value, hash3Value, hash4Value; + MT_HASH4_CALC + hash[kFix4HashSize + hash4Value] = + hash[kFix3HashSize + hash3Value] = + hash[ hash2Value] = + p->lzPos; + SKIP_FOOTER +} +*/ + +void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder *vTable) +{ + vTable->Init = (Mf_Init_Func)MatchFinderMt_Init; + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinderMt_GetIndexByte; + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinderMt_GetNumAvailableBytes; + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinderMt_GetPointerToCurrentPos; + vTable->GetMatches = (Mf_GetMatches_Func)MatchFinderMt_GetMatches; + switch(p->MatchFinder->numHashBytes) + { + case 2: + p->GetHeadsFunc = GetHeads2; + p->MixMatchesFunc = (Mf_Mix_Matches)0; + vTable->Skip = (Mf_Skip_Func)MatchFinderMt0_Skip; + vTable->GetMatches = (Mf_GetMatches_Func)MatchFinderMt2_GetMatches; + break; + case 3: + p->GetHeadsFunc = GetHeads3; + p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches2; + vTable->Skip = (Mf_Skip_Func)MatchFinderMt2_Skip; + break; + default: + /* case 4: */ + p->GetHeadsFunc = p->MatchFinder->bigHash ? GetHeads4b : GetHeads4; + /* p->GetHeadsFunc = GetHeads4; */ + p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches3; + vTable->Skip = (Mf_Skip_Func)MatchFinderMt3_Skip; + break; + /* + default: + p->GetHeadsFunc = GetHeads5; + p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches4; + vTable->Skip = (Mf_Skip_Func)MatchFinderMt4_Skip; + break; + */ + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzFindMt.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzFindMt.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,97 @@ +/* LzFindMt.h -- multithreaded Match finder for LZ algorithms +2008-10-04 : Igor Pavlov : Public domain */ + +#ifndef __LZFINDMT_H +#define __LZFINDMT_H + +#include "Threads.h" +#include "LzFind.h" + +#define kMtHashBlockSize (1 << 13) +#define kMtHashNumBlocks (1 << 3) +#define kMtHashNumBlocksMask (kMtHashNumBlocks - 1) + +#define kMtBtBlockSize (1 << 14) +#define kMtBtNumBlocks (1 << 6) +#define kMtBtNumBlocksMask (kMtBtNumBlocks - 1) + +typedef struct _CMtSync +{ + Bool wasCreated; + Bool needStart; + Bool exit; + Bool stopWriting; + + CThread thread; + CAutoResetEvent canStart; + CAutoResetEvent wasStarted; + CAutoResetEvent wasStopped; + CSemaphore freeSemaphore; + CSemaphore filledSemaphore; + Bool csWasInitialized; + Bool csWasEntered; + CCriticalSection cs; + UInt32 numProcessedBlocks; +} CMtSync; + +typedef UInt32 * (*Mf_Mix_Matches)(void *p, UInt32 matchMinPos, UInt32 *distances); + +/* kMtCacheLineDummy must be >= size_of_CPU_cache_line */ +#define kMtCacheLineDummy 128 + +typedef void (*Mf_GetHeads)(const Byte *buffer, UInt32 pos, + UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc); + +typedef struct _CMatchFinderMt +{ + /* LZ */ + const Byte *pointerToCurPos; + UInt32 *btBuf; + UInt32 btBufPos; + UInt32 btBufPosLimit; + UInt32 lzPos; + UInt32 btNumAvailBytes; + + UInt32 *hash; + UInt32 fixedHashSize; + UInt32 historySize; + const UInt32 *crc; + + Mf_Mix_Matches MixMatchesFunc; + + /* LZ + BT */ + CMtSync btSync; + Byte btDummy[kMtCacheLineDummy]; + + /* BT */ + UInt32 *hashBuf; + UInt32 hashBufPos; + UInt32 hashBufPosLimit; + UInt32 hashNumAvail; + + CLzRef *son; + UInt32 matchMaxLen; + UInt32 numHashBytes; + UInt32 pos; + Byte *buffer; + UInt32 cyclicBufferPos; + UInt32 cyclicBufferSize; /* it must be historySize + 1 */ + UInt32 cutValue; + + /* BT + Hash */ + CMtSync hashSync; + /* Byte hashDummy[kMtCacheLineDummy]; */ + + /* Hash */ + Mf_GetHeads GetHeadsFunc; + CMatchFinder *MatchFinder; +} CMatchFinderMt; + +void MatchFinderMt_Construct(CMatchFinderMt *p); +void MatchFinderMt_Destruct(CMatchFinderMt *p, ISzAlloc *alloc); +SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddBufferBefore, + UInt32 matchMaxLen, UInt32 keepAddBufferAfter, ISzAlloc *alloc); +void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder *vTable); +void MatchFinderMt_ReleaseStream(CMatchFinderMt *p); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzHash.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzHash.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,54 @@ +/* LzHash.h -- HASH functions for LZ algorithms +2008-10-04 : Igor Pavlov : Public domain */ + +#ifndef __LZHASH_H +#define __LZHASH_H + +#define kHash2Size (1 << 10) +#define kHash3Size (1 << 16) +#define kHash4Size (1 << 20) + +#define kFix3HashSize (kHash2Size) +#define kFix4HashSize (kHash2Size + kHash3Size) +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size) + +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8); + +#define HASH3_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + hash2Value = temp & (kHash2Size - 1); \ + hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; } + +#define HASH4_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + hash2Value = temp & (kHash2Size - 1); \ + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ + hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; } + +#define HASH5_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + hash2Value = temp & (kHash2Size - 1); \ + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \ + hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \ + hash4Value &= (kHash4Size - 1); } + +/* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */ +#define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF; + + +#define MT_HASH2_CALC \ + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1); + +#define MT_HASH3_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + hash2Value = temp & (kHash2Size - 1); \ + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); } + +#define MT_HASH4_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + hash2Value = temp & (kHash2Size - 1); \ + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \ + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); } + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzmaDec.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzmaDec.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1007 @@ +/* LzmaDec.c -- LZMA Decoder +2008-11-06 : Igor Pavlov : Public domain */ + +#include "LzmaDec.h" + +#include + +#define kNumTopBits 24 +#define kTopValue ((UInt32)1 << kNumTopBits) + +#define kNumBitModelTotalBits 11 +#define kBitModelTotal (1 << kNumBitModelTotalBits) +#define kNumMoveBits 5 + +#define RC_INIT_SIZE 5 + +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); } + +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound) +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \ + { UPDATE_0(p); i = (i + i); A0; } else \ + { UPDATE_1(p); i = (i + i) + 1; A1; } +#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;) + +#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); } +#define TREE_DECODE(probs, limit, i) \ + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; } + +/* #define _LZMA_SIZE_OPT */ + +#ifdef _LZMA_SIZE_OPT +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i) +#else +#define TREE_6_DECODE(probs, i) \ + { i = 1; \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + i -= 0x40; } +#endif + +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); } + +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound) +#define UPDATE_0_CHECK range = bound; +#define UPDATE_1_CHECK range -= bound; code -= bound; +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \ + { UPDATE_0_CHECK; i = (i + i); A0; } else \ + { UPDATE_1_CHECK; i = (i + i) + 1; A1; } +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;) +#define TREE_DECODE_CHECK(probs, limit, i) \ + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; } + + +#define kNumPosBitsMax 4 +#define kNumPosStatesMax (1 << kNumPosBitsMax) + +#define kLenNumLowBits 3 +#define kLenNumLowSymbols (1 << kLenNumLowBits) +#define kLenNumMidBits 3 +#define kLenNumMidSymbols (1 << kLenNumMidBits) +#define kLenNumHighBits 8 +#define kLenNumHighSymbols (1 << kLenNumHighBits) + +#define LenChoice 0 +#define LenChoice2 (LenChoice + 1) +#define LenLow (LenChoice2 + 1) +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits)) +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits)) +#define kNumLenProbs (LenHigh + kLenNumHighSymbols) + + +#define kNumStates 12 +#define kNumLitStates 7 + +#define kStartPosModelIndex 4 +#define kEndPosModelIndex 14 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1)) + +#define kNumPosSlotBits 6 +#define kNumLenToPosStates 4 + +#define kNumAlignBits 4 +#define kAlignTableSize (1 << kNumAlignBits) + +#define kMatchMinLen 2 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols) + +#define IsMatch 0 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax)) +#define IsRepG0 (IsRep + kNumStates) +#define IsRepG1 (IsRepG0 + kNumStates) +#define IsRepG2 (IsRepG1 + kNumStates) +#define IsRep0Long (IsRepG2 + kNumStates) +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax)) +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits)) +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex) +#define LenCoder (Align + kAlignTableSize) +#define RepLenCoder (LenCoder + kNumLenProbs) +#define Literal (RepLenCoder + kNumLenProbs) + +#define LZMA_BASE_SIZE 1846 +#define LZMA_LIT_SIZE 768 + +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp))) + +#if Literal != LZMA_BASE_SIZE +StopCompilingDueBUG +#endif + +static const Byte kLiteralNextStates[kNumStates * 2] = +{ + 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5, + 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10 +}; + +#define LZMA_DIC_MIN (1 << 12) + +/* First LZMA-symbol is always decoded. +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization +Out: + Result: + SZ_OK - OK + SZ_ERROR_DATA - Error + p->remainLen: + < kMatchSpecLenStart : normal remain + = kMatchSpecLenStart : finished + = kMatchSpecLenStart + 1 : Flush marker + = kMatchSpecLenStart + 2 : State Init Marker +*/ + +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit) +{ + CLzmaProb *probs = p->probs; + + unsigned state = p->state; + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3]; + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1; + unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1; + unsigned lc = p->prop.lc; + + Byte *dic = p->dic; + SizeT dicBufSize = p->dicBufSize; + SizeT dicPos = p->dicPos; + + UInt32 processedPos = p->processedPos; + UInt32 checkDicSize = p->checkDicSize; + unsigned len = 0; + + const Byte *buf = p->buf; + UInt32 range = p->range; + UInt32 code = p->code; + + do + { + CLzmaProb *prob; + UInt32 bound; + unsigned ttt; + unsigned posState = processedPos & pbMask; + + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState; + IF_BIT_0(prob) + { + unsigned symbol; + UPDATE_0(prob); + prob = probs + Literal; + if (checkDicSize != 0 || processedPos != 0) + prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) + + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc)))); + + if (state < kNumLitStates) + { + symbol = 1; + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100); + } + else + { + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)]; + unsigned offs = 0x100; + symbol = 1; + do + { + unsigned bit; + CLzmaProb *probLit; + matchByte <<= 1; + bit = (matchByte & offs); + probLit = prob + offs + bit + symbol; + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit) + } + while (symbol < 0x100); + } + dic[dicPos++] = (Byte)(symbol & 0xFF); + processedPos++; + + state = kLiteralNextStates[state]; + /* if (state < 4) state = 0; else if (state < 10) state -= 3; else state -= 6; */ + continue; + } + else + { + UPDATE_1(prob); + prob = probs + IsRep + state; + IF_BIT_0(prob) + { + UPDATE_0(prob); + state += kNumStates; + prob = probs + LenCoder; + } + else + { + UPDATE_1(prob); + if (checkDicSize == 0 && processedPos == 0) + return SZ_ERROR_DATA; + prob = probs + IsRepG0 + state; + IF_BIT_0(prob) + { + UPDATE_0(prob); + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState; + IF_BIT_0(prob) + { + UPDATE_0(prob); + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)]; + dicPos++; + processedPos++; + state = state < kNumLitStates ? 9 : 11; + continue; + } + UPDATE_1(prob); + } + else + { + UInt32 distance; + UPDATE_1(prob); + prob = probs + IsRepG1 + state; + IF_BIT_0(prob) + { + UPDATE_0(prob); + distance = rep1; + } + else + { + UPDATE_1(prob); + prob = probs + IsRepG2 + state; + IF_BIT_0(prob) + { + UPDATE_0(prob); + distance = rep2; + } + else + { + UPDATE_1(prob); + distance = rep3; + rep3 = rep2; + } + rep2 = rep1; + } + rep1 = rep0; + rep0 = distance; + } + state = state < kNumLitStates ? 8 : 11; + prob = probs + RepLenCoder; + } + { + unsigned limit, offset; + CLzmaProb *probLen = prob + LenChoice; + IF_BIT_0(probLen) + { + UPDATE_0(probLen); + probLen = prob + LenLow + (posState << kLenNumLowBits); + offset = 0; + limit = (1 << kLenNumLowBits); + } + else + { + UPDATE_1(probLen); + probLen = prob + LenChoice2; + IF_BIT_0(probLen) + { + UPDATE_0(probLen); + probLen = prob + LenMid + (posState << kLenNumMidBits); + offset = kLenNumLowSymbols; + limit = (1 << kLenNumMidBits); + } + else + { + UPDATE_1(probLen); + probLen = prob + LenHigh; + offset = kLenNumLowSymbols + kLenNumMidSymbols; + limit = (1 << kLenNumHighBits); + } + } + TREE_DECODE(probLen, limit, len); + len += offset; + } + + if (state >= kNumStates) + { + UInt32 distance; + prob = probs + PosSlot + + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits); + TREE_6_DECODE(prob, distance); + if (distance >= kStartPosModelIndex) + { + unsigned posSlot = (unsigned)distance; + int numDirectBits = (int)(((distance >> 1) - 1)); + distance = (2 | (distance & 1)); + if (posSlot < kEndPosModelIndex) + { + distance <<= numDirectBits; + prob = probs + SpecPos + distance - posSlot - 1; + { + UInt32 mask = 1; + unsigned i = 1; + do + { + GET_BIT2(prob + i, i, ; , distance |= mask); + mask <<= 1; + } + while (--numDirectBits != 0); + } + } + else + { + numDirectBits -= kNumAlignBits; + do + { + NORMALIZE + range >>= 1; + + { + UInt32 t; + code -= range; + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */ + distance = (distance << 1) + (t + 1); + code += range & t; + } + /* + distance <<= 1; + if (code >= range) + { + code -= range; + distance |= 1; + } + */ + } + while (--numDirectBits != 0); + prob = probs + Align; + distance <<= kNumAlignBits; + { + unsigned i = 1; + GET_BIT2(prob + i, i, ; , distance |= 1); + GET_BIT2(prob + i, i, ; , distance |= 2); + GET_BIT2(prob + i, i, ; , distance |= 4); + GET_BIT2(prob + i, i, ; , distance |= 8); + } + if (distance == (UInt32)0xFFFFFFFF) + { + len += kMatchSpecLenStart; + state -= kNumStates; + break; + } + } + } + rep3 = rep2; + rep2 = rep1; + rep1 = rep0; + rep0 = distance + 1; + if (checkDicSize == 0) + { + if (distance >= processedPos) + return SZ_ERROR_DATA; + } + else if (distance >= checkDicSize) + return SZ_ERROR_DATA; + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3; + /* state = kLiteralNextStates[state]; */ + } + + len += kMatchMinLen; + + if (limit == dicPos) + return SZ_ERROR_DATA; + { + SizeT rem = limit - dicPos; + unsigned curLen = ((rem < len) ? (unsigned)rem : len); + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0); + + processedPos += curLen; + + len -= curLen; + if (pos + curLen <= dicBufSize) + { + Byte *dest = dic + dicPos; + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos; + const Byte *lim = dest + curLen; + dicPos += curLen; + do + *(dest) = (Byte)*(dest + src); + while (++dest != lim); + } + else + { + do + { + dic[dicPos++] = dic[pos]; + if (++pos == dicBufSize) + pos = 0; + } + while (--curLen != 0); + } + } + } + } + while (dicPos < limit && buf < bufLimit); + NORMALIZE; + p->buf = buf; + p->range = range; + p->code = code; + p->remainLen = len; + p->dicPos = dicPos; + p->processedPos = processedPos; + p->reps[0] = rep0; + p->reps[1] = rep1; + p->reps[2] = rep2; + p->reps[3] = rep3; + p->state = state; + + return SZ_OK; +} + +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit) +{ + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart) + { + Byte *dic = p->dic; + SizeT dicPos = p->dicPos; + SizeT dicBufSize = p->dicBufSize; + unsigned len = p->remainLen; + UInt32 rep0 = p->reps[0]; + if (limit - dicPos < len) + len = (unsigned)(limit - dicPos); + + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len) + p->checkDicSize = p->prop.dicSize; + + p->processedPos += len; + p->remainLen -= len; + while (len-- != 0) + { + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)]; + dicPos++; + } + p->dicPos = dicPos; + } +} + +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit) +{ + do + { + SizeT limit2 = limit; + if (p->checkDicSize == 0) + { + UInt32 rem = p->prop.dicSize - p->processedPos; + if (limit - p->dicPos > rem) + limit2 = p->dicPos + rem; + } + RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit)); + if (p->processedPos >= p->prop.dicSize) + p->checkDicSize = p->prop.dicSize; + LzmaDec_WriteRem(p, limit); + } + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart); + + if (p->remainLen > kMatchSpecLenStart) + { + p->remainLen = kMatchSpecLenStart; + } + return 0; +} + +typedef enum +{ + DUMMY_ERROR, /* unexpected end of input stream */ + DUMMY_LIT, + DUMMY_MATCH, + DUMMY_REP +} ELzmaDummy; + +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize) +{ + UInt32 range = p->range; + UInt32 code = p->code; + const Byte *bufLimit = buf + inSize; + CLzmaProb *probs = p->probs; + unsigned state = p->state; + ELzmaDummy res; + + { + CLzmaProb *prob; + UInt32 bound; + unsigned ttt; + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1); + + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK + + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */ + + prob = probs + Literal; + if (p->checkDicSize != 0 || p->processedPos != 0) + prob += (LZMA_LIT_SIZE * + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) + + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc)))); + + if (state < kNumLitStates) + { + unsigned symbol = 1; + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100); + } + else + { + unsigned matchByte = p->dic[p->dicPos - p->reps[0] + + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)]; + unsigned offs = 0x100; + unsigned symbol = 1; + do + { + unsigned bit; + CLzmaProb *probLit; + matchByte <<= 1; + bit = (matchByte & offs); + probLit = prob + offs + bit + symbol; + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit) + } + while (symbol < 0x100); + } + res = DUMMY_LIT; + } + else + { + unsigned len; + UPDATE_1_CHECK; + + prob = probs + IsRep + state; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + state = 0; + prob = probs + LenCoder; + res = DUMMY_MATCH; + } + else + { + UPDATE_1_CHECK; + res = DUMMY_REP; + prob = probs + IsRepG0 + state; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + NORMALIZE_CHECK; + return DUMMY_REP; + } + else + { + UPDATE_1_CHECK; + } + } + else + { + UPDATE_1_CHECK; + prob = probs + IsRepG1 + state; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + } + else + { + UPDATE_1_CHECK; + prob = probs + IsRepG2 + state; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + } + else + { + UPDATE_1_CHECK; + } + } + } + state = kNumStates; + prob = probs + RepLenCoder; + } + { + unsigned limit, offset; + CLzmaProb *probLen = prob + LenChoice; + IF_BIT_0_CHECK(probLen) + { + UPDATE_0_CHECK; + probLen = prob + LenLow + (posState << kLenNumLowBits); + offset = 0; + limit = 1 << kLenNumLowBits; + } + else + { + UPDATE_1_CHECK; + probLen = prob + LenChoice2; + IF_BIT_0_CHECK(probLen) + { + UPDATE_0_CHECK; + probLen = prob + LenMid + (posState << kLenNumMidBits); + offset = kLenNumLowSymbols; + limit = 1 << kLenNumMidBits; + } + else + { + UPDATE_1_CHECK; + probLen = prob + LenHigh; + offset = kLenNumLowSymbols + kLenNumMidSymbols; + limit = 1 << kLenNumHighBits; + } + } + TREE_DECODE_CHECK(probLen, limit, len); + len += offset; + } + + if (state < 4) + { + unsigned posSlot; + prob = probs + PosSlot + + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << + kNumPosSlotBits); + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot); + if (posSlot >= kStartPosModelIndex) + { + int numDirectBits = ((posSlot >> 1) - 1); + + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */ + + if (posSlot < kEndPosModelIndex) + { + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1; + } + else + { + numDirectBits -= kNumAlignBits; + do + { + NORMALIZE_CHECK + range >>= 1; + code -= range & (((code - range) >> 31) - 1); + /* if (code >= range) code -= range; */ + } + while (--numDirectBits != 0); + prob = probs + Align; + numDirectBits = kNumAlignBits; + } + { + unsigned i = 1; + do + { + GET_BIT_CHECK(prob + i, i); + } + while (--numDirectBits != 0); + } + } + } + } + } + NORMALIZE_CHECK; + return res; +} + + +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data) +{ + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]); + p->range = 0xFFFFFFFF; + p->needFlush = 0; +} + +void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState) +{ + p->needFlush = 1; + p->remainLen = 0; + p->tempBufSize = 0; + + if (initDic) + { + p->processedPos = 0; + p->checkDicSize = 0; + p->needInitState = 1; + } + if (initState) + p->needInitState = 1; +} + +void LzmaDec_Init(CLzmaDec *p) +{ + p->dicPos = 0; + LzmaDec_InitDicAndState(p, True, True); +} + +static void LzmaDec_InitStateReal(CLzmaDec *p) +{ + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp)); + UInt32 i; + CLzmaProb *probs = p->probs; + for (i = 0; i < numProbs; i++) + probs[i] = kBitModelTotal >> 1; + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1; + p->state = 0; + p->needInitState = 0; +} + +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen, + ELzmaFinishMode finishMode, ELzmaStatus *status) +{ + SizeT inSize = *srcLen; + (*srcLen) = 0; + LzmaDec_WriteRem(p, dicLimit); + + *status = LZMA_STATUS_NOT_SPECIFIED; + + while (p->remainLen != kMatchSpecLenStart) + { + int checkEndMarkNow; + + if (p->needFlush != 0) + { + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--) + p->tempBuf[p->tempBufSize++] = *src++; + if (p->tempBufSize < RC_INIT_SIZE) + { + *status = LZMA_STATUS_NEEDS_MORE_INPUT; + return SZ_OK; + } + if (p->tempBuf[0] != 0) + return SZ_ERROR_DATA; + + LzmaDec_InitRc(p, p->tempBuf); + p->tempBufSize = 0; + } + + checkEndMarkNow = 0; + if (p->dicPos >= dicLimit) + { + if (p->remainLen == 0 && p->code == 0) + { + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK; + return SZ_OK; + } + if (finishMode == LZMA_FINISH_ANY) + { + *status = LZMA_STATUS_NOT_FINISHED; + return SZ_OK; + } + if (p->remainLen != 0) + { + *status = LZMA_STATUS_NOT_FINISHED; + return SZ_ERROR_DATA; + } + checkEndMarkNow = 1; + } + + if (p->needInitState) + LzmaDec_InitStateReal(p); + + if (p->tempBufSize == 0) + { + SizeT processed; + const Byte *bufLimit; + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) + { + int dummyRes = LzmaDec_TryDummy(p, src, inSize); + if (dummyRes == DUMMY_ERROR) + { + memcpy(p->tempBuf, src, inSize); + p->tempBufSize = (unsigned)inSize; + (*srcLen) += inSize; + *status = LZMA_STATUS_NEEDS_MORE_INPUT; + return SZ_OK; + } + if (checkEndMarkNow && dummyRes != DUMMY_MATCH) + { + *status = LZMA_STATUS_NOT_FINISHED; + return SZ_ERROR_DATA; + } + bufLimit = src; + } + else + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX; + p->buf = src; + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0) + return SZ_ERROR_DATA; + processed = (SizeT)(p->buf - src); + (*srcLen) += processed; + src += processed; + inSize -= processed; + } + else + { + unsigned rem = p->tempBufSize, lookAhead = 0; + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize) + p->tempBuf[rem++] = src[lookAhead++]; + p->tempBufSize = rem; + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) + { + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem); + if (dummyRes == DUMMY_ERROR) + { + (*srcLen) += lookAhead; + *status = LZMA_STATUS_NEEDS_MORE_INPUT; + return SZ_OK; + } + if (checkEndMarkNow && dummyRes != DUMMY_MATCH) + { + *status = LZMA_STATUS_NOT_FINISHED; + return SZ_ERROR_DATA; + } + } + p->buf = p->tempBuf; + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0) + return SZ_ERROR_DATA; + lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf)); + (*srcLen) += lookAhead; + src += lookAhead; + inSize -= lookAhead; + p->tempBufSize = 0; + } + } + if (p->code == 0) + *status = LZMA_STATUS_FINISHED_WITH_MARK; + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA; +} + +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status) +{ + SizeT outSize = *destLen; + SizeT inSize = *srcLen; + *srcLen = *destLen = 0; + for (;;) + { + SizeT inSizeCur = inSize, outSizeCur, dicPos; + ELzmaFinishMode curFinishMode; + SRes res; + if (p->dicPos == p->dicBufSize) + p->dicPos = 0; + dicPos = p->dicPos; + if (outSize > p->dicBufSize - dicPos) + { + outSizeCur = p->dicBufSize; + curFinishMode = LZMA_FINISH_ANY; + } + else + { + outSizeCur = dicPos + outSize; + curFinishMode = finishMode; + } + + res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status); + src += inSizeCur; + inSize -= inSizeCur; + *srcLen += inSizeCur; + outSizeCur = p->dicPos - dicPos; + memcpy(dest, p->dic + dicPos, outSizeCur); + dest += outSizeCur; + outSize -= outSizeCur; + *destLen += outSizeCur; + if (res != 0) + return res; + if (outSizeCur == 0 || outSize == 0) + return SZ_OK; + } +} + +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc) +{ + alloc->Free(alloc, p->probs); + p->probs = 0; +} + +static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc) +{ + alloc->Free(alloc, p->dic); + p->dic = 0; +} + +void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc) +{ + LzmaDec_FreeProbs(p, alloc); + LzmaDec_FreeDict(p, alloc); +} + +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size) +{ + UInt32 dicSize; + Byte d; + + if (size < LZMA_PROPS_SIZE) + return SZ_ERROR_UNSUPPORTED; + else + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24); + + if (dicSize < LZMA_DIC_MIN) + dicSize = LZMA_DIC_MIN; + p->dicSize = dicSize; + + d = data[0]; + if (d >= (9 * 5 * 5)) + return SZ_ERROR_UNSUPPORTED; + + p->lc = d % 9; + d /= 9; + p->pb = d / 5; + p->lp = d % 5; + + return SZ_OK; +} + +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc) +{ + UInt32 numProbs = LzmaProps_GetNumProbs(propNew); + if (p->probs == 0 || numProbs != p->numProbs) + { + LzmaDec_FreeProbs(p, alloc); + p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb)); + p->numProbs = numProbs; + if (p->probs == 0) + return SZ_ERROR_MEM; + } + return SZ_OK; +} + +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc) +{ + CLzmaProps propNew; + RINOK(LzmaProps_Decode(&propNew, props, propsSize)); + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); + p->prop = propNew; + return SZ_OK; +} + +SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc) +{ + CLzmaProps propNew; + SizeT dicBufSize; + RINOK(LzmaProps_Decode(&propNew, props, propsSize)); + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); + dicBufSize = propNew.dicSize; + if (p->dic == 0 || dicBufSize != p->dicBufSize) + { + LzmaDec_FreeDict(p, alloc); + p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize); + if (p->dic == 0) + { + LzmaDec_FreeProbs(p, alloc); + return SZ_ERROR_MEM; + } + } + p->dicBufSize = dicBufSize; + p->prop = propNew; + return SZ_OK; +} + +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, + ELzmaStatus *status, ISzAlloc *alloc) +{ + CLzmaDec p; + SRes res; + SizeT inSize = *srcLen; + SizeT outSize = *destLen; + *srcLen = *destLen = 0; + if (inSize < RC_INIT_SIZE) + return SZ_ERROR_INPUT_EOF; + + LzmaDec_Construct(&p); + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc); + if (res != 0) + return res; + p.dic = dest; + p.dicBufSize = outSize; + + LzmaDec_Init(&p); + + *srcLen = inSize; + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status); + + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT) + res = SZ_ERROR_INPUT_EOF; + + (*destLen) = p.dicPos; + LzmaDec_FreeProbs(&p, alloc); + return res; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzmaDec.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzmaDec.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,223 @@ +/* LzmaDec.h -- LZMA Decoder +2008-10-04 : Igor Pavlov : Public domain */ + +#ifndef __LZMADEC_H +#define __LZMADEC_H + +#include "Types.h" + +/* #define _LZMA_PROB32 */ +/* _LZMA_PROB32 can increase the speed on some CPUs, + but memory usage for CLzmaDec::probs will be doubled in that case */ + +#ifdef _LZMA_PROB32 +#define CLzmaProb UInt32 +#else +#define CLzmaProb UInt16 +#endif + + +/* ---------- LZMA Properties ---------- */ + +#define LZMA_PROPS_SIZE 5 + +typedef struct _CLzmaProps +{ + unsigned lc, lp, pb; + UInt32 dicSize; +} CLzmaProps; + +/* LzmaProps_Decode - decodes properties +Returns: + SZ_OK + SZ_ERROR_UNSUPPORTED - Unsupported properties +*/ + +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size); + + +/* ---------- LZMA Decoder state ---------- */ + +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case. + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */ + +#define LZMA_REQUIRED_INPUT_MAX 20 + +typedef struct +{ + CLzmaProps prop; + CLzmaProb *probs; + Byte *dic; + const Byte *buf; + UInt32 range, code; + SizeT dicPos; + SizeT dicBufSize; + UInt32 processedPos; + UInt32 checkDicSize; + unsigned state; + UInt32 reps[4]; + unsigned remainLen; + int needFlush; + int needInitState; + UInt32 numProbs; + unsigned tempBufSize; + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; +} CLzmaDec; + +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; } + +void LzmaDec_Init(CLzmaDec *p); + +/* There are two types of LZMA streams: + 0) Stream with end mark. That end mark adds about 6 bytes to compressed size. + 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */ + +typedef enum +{ + LZMA_FINISH_ANY, /* finish at any point */ + LZMA_FINISH_END /* block must be finished at the end */ +} ELzmaFinishMode; + +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! + + You must use LZMA_FINISH_END, when you know that current output buffer + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY. + + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK, + and output value of destLen will be less than output buffer size limit. + You can check status result also. + + You can use multiple checks to test data integrity after full decompression: + 1) Check Result and "status" variable. + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize. + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize. + You must use correct finish mode in that case. */ + +typedef enum +{ + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */ + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */ + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */ + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */ + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */ +} ELzmaStatus; + +/* ELzmaStatus is used only as output value for function call */ + + +/* ---------- Interfaces ---------- */ + +/* There are 3 levels of interfaces: + 1) Dictionary Interface + 2) Buffer Interface + 3) One Call Interface + You can select any of these interfaces, but don't mix functions from different + groups for same object. */ + + +/* There are two variants to allocate state for Dictionary Interface: + 1) LzmaDec_Allocate / LzmaDec_Free + 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs + You can use variant 2, if you set dictionary buffer manually. + For Buffer Interface you must always use variant 1. + +LzmaDec_Allocate* can return: + SZ_OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_UNSUPPORTED - Unsupported properties +*/ + +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc); +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc); + +SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc); +void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc); + +/* ---------- Dictionary Interface ---------- */ + +/* You can use it, if you want to eliminate the overhead for data copying from + dictionary to some other external buffer. + You must work with CLzmaDec variables directly in this interface. + + STEPS: + LzmaDec_Constr() + LzmaDec_Allocate() + for (each new stream) + { + LzmaDec_Init() + while (it needs more decompression) + { + LzmaDec_DecodeToDic() + use data from CLzmaDec::dic and update CLzmaDec::dicPos + } + } + LzmaDec_Free() +*/ + +/* LzmaDec_DecodeToDic + + The decoding to internal dictionary buffer (CLzmaDec::dic). + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!! + +finishMode: + It has meaning only if the decoding reaches output limit (dicLimit). + LZMA_FINISH_ANY - Decode just dicLimit bytes. + LZMA_FINISH_END - Stream must be finished after dicLimit. + +Returns: + SZ_OK + status: + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED + LZMA_STATUS_NEEDS_MORE_INPUT + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK + SZ_ERROR_DATA - Data error +*/ + +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); + + +/* ---------- Buffer Interface ---------- */ + +/* It's zlib-like interface. + See LzmaDec_DecodeToDic description for information about STEPS and return results, + but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need + to work with CLzmaDec variables manually. + +finishMode: + It has meaning only if the decoding reaches output limit (*destLen). + LZMA_FINISH_ANY - Decode just destLen bytes. + LZMA_FINISH_END - Stream must be finished after (*destLen). +*/ + +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); + + +/* ---------- One Call Interface ---------- */ + +/* LzmaDecode + +finishMode: + It has meaning only if the decoding reaches output limit (*destLen). + LZMA_FINISH_ANY - Decode just destLen bytes. + LZMA_FINISH_END - Stream must be finished after (*destLen). + +Returns: + SZ_OK + status: + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK + SZ_ERROR_DATA - Data error + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_UNSUPPORTED - Unsupported properties + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). +*/ + +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, + ELzmaStatus *status, ISzAlloc *alloc); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzmaEnc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzmaEnc.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,2275 @@ +/* LzmaEnc.c -- LZMA Encoder +2008-10-04 : Igor Pavlov : Public domain */ + +#include + +/* #define SHOW_STAT */ +/* #define SHOW_STAT2 */ + +#if defined(SHOW_STAT) || defined(SHOW_STAT2) +#include +#endif + +#include "LzmaEnc.h" + +#include "LzFind.h" +#ifdef COMPRESS_MF_MT +#include "LzFindMt.h" +#endif + +#ifdef SHOW_STAT +static int ttt = 0; +#endif + +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1) + +#define kBlockSize (9 << 10) +#define kUnpackBlockSize (1 << 18) +#define kMatchArraySize (1 << 21) +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX) + +#define kNumMaxDirectBits (31) + +#define kNumTopBits 24 +#define kTopValue ((UInt32)1 << kNumTopBits) + +#define kNumBitModelTotalBits 11 +#define kBitModelTotal (1 << kNumBitModelTotalBits) +#define kNumMoveBits 5 +#define kProbInitValue (kBitModelTotal >> 1) + +#define kNumMoveReducingBits 4 +#define kNumBitPriceShiftBits 4 +#define kBitPrice (1 << kNumBitPriceShiftBits) + +void LzmaEncProps_Init(CLzmaEncProps *p) +{ + p->level = 5; + p->dictSize = p->mc = 0; + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1; + p->writeEndMark = 0; +} + +void LzmaEncProps_Normalize(CLzmaEncProps *p) +{ + int level = p->level; + if (level < 0) level = 5; + p->level = level; + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26))); + if (p->lc < 0) p->lc = 3; + if (p->lp < 0) p->lp = 0; + if (p->pb < 0) p->pb = 2; + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1); + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64); + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1); + if (p->numHashBytes < 0) p->numHashBytes = 4; + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1); + if (p->numThreads < 0) p->numThreads = ((p->btMode && p->algo) ? 2 : 1); +} + +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2) +{ + CLzmaEncProps props = *props2; + LzmaEncProps_Normalize(&props); + return props.dictSize; +} + +/* #define LZMA_LOG_BSR */ +/* Define it for Intel's CPU */ + + +#ifdef LZMA_LOG_BSR + +#define kDicLogSizeMaxCompress 30 + +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); } + +UInt32 GetPosSlot1(UInt32 pos) +{ + UInt32 res; + BSR2_RET(pos, res); + return res; +} +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); } + +#else + +#define kNumLogBits (9 + (int)sizeof(size_t) / 2) +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7) + +void LzmaEnc_FastPosInit(Byte *g_FastPos) +{ + int c = 2, slotFast; + g_FastPos[0] = 0; + g_FastPos[1] = 1; + + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++) + { + UInt32 k = (1 << ((slotFast >> 1) - 1)); + UInt32 j; + for (j = 0; j < k; j++, c++) + g_FastPos[c] = (Byte)slotFast; + } +} + +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \ + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \ + res = p->g_FastPos[pos >> i] + (i * 2); } +/* +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \ + p->g_FastPos[pos >> 6] + 12 : \ + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; } +*/ + +#define GetPosSlot1(pos) p->g_FastPos[pos] +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); } + +#endif + + +#define LZMA_NUM_REPS 4 + +typedef unsigned CState; + +typedef struct _COptimal +{ + UInt32 price; + + CState state; + int prev1IsChar; + int prev2; + + UInt32 posPrev2; + UInt32 backPrev2; + + UInt32 posPrev; + UInt32 backPrev; + UInt32 backs[LZMA_NUM_REPS]; +} COptimal; + +#define kNumOpts (1 << 12) + +#define kNumLenToPosStates 4 +#define kNumPosSlotBits 6 +#define kDicLogSizeMin 0 +#define kDicLogSizeMax 32 +#define kDistTableSizeMax (kDicLogSizeMax * 2) + + +#define kNumAlignBits 4 +#define kAlignTableSize (1 << kNumAlignBits) +#define kAlignMask (kAlignTableSize - 1) + +#define kStartPosModelIndex 4 +#define kEndPosModelIndex 14 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex) + +#define kNumFullDistances (1 << (kEndPosModelIndex / 2)) + +#ifdef _LZMA_PROB32 +#define CLzmaProb UInt32 +#else +#define CLzmaProb UInt16 +#endif + +#define LZMA_PB_MAX 4 +#define LZMA_LC_MAX 8 +#define LZMA_LP_MAX 4 + +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX) + + +#define kLenNumLowBits 3 +#define kLenNumLowSymbols (1 << kLenNumLowBits) +#define kLenNumMidBits 3 +#define kLenNumMidSymbols (1 << kLenNumMidBits) +#define kLenNumHighBits 8 +#define kLenNumHighSymbols (1 << kLenNumHighBits) + +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols) + +#define LZMA_MATCH_LEN_MIN 2 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1) + +#define kNumStates 12 + +typedef struct +{ + CLzmaProb choice; + CLzmaProb choice2; + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits]; + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits]; + CLzmaProb high[kLenNumHighSymbols]; +} CLenEnc; + +typedef struct +{ + CLenEnc p; + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal]; + UInt32 tableSize; + UInt32 counters[LZMA_NUM_PB_STATES_MAX]; +} CLenPriceEnc; + +typedef struct _CRangeEnc +{ + UInt32 range; + Byte cache; + UInt64 low; + UInt64 cacheSize; + Byte *buf; + Byte *bufLim; + Byte *bufBase; + ISeqOutStream *outStream; + UInt64 processed; + SRes res; +} CRangeEnc; + +typedef struct _CSeqInStreamBuf +{ + ISeqInStream funcTable; + const Byte *data; + SizeT rem; +} CSeqInStreamBuf; + +static SRes MyRead(void *pp, void *data, size_t *size) +{ + size_t curSize = *size; + CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp; + if (p->rem < curSize) + curSize = p->rem; + memcpy(data, p->data, curSize); + p->rem -= curSize; + p->data += curSize; + *size = curSize; + return SZ_OK; +} + +typedef struct +{ + CLzmaProb *litProbs; + + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; + CLzmaProb isRep[kNumStates]; + CLzmaProb isRepG0[kNumStates]; + CLzmaProb isRepG1[kNumStates]; + CLzmaProb isRepG2[kNumStates]; + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; + + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; + CLzmaProb posAlignEncoder[1 << kNumAlignBits]; + + CLenPriceEnc lenEnc; + CLenPriceEnc repLenEnc; + + UInt32 reps[LZMA_NUM_REPS]; + UInt32 state; +} CSaveState; + +typedef struct _CLzmaEnc +{ + IMatchFinder matchFinder; + void *matchFinderObj; + + #ifdef COMPRESS_MF_MT + Bool mtMode; + CMatchFinderMt matchFinderMt; + #endif + + CMatchFinder matchFinderBase; + + #ifdef COMPRESS_MF_MT + Byte pad[128]; + #endif + + UInt32 optimumEndIndex; + UInt32 optimumCurrentIndex; + + UInt32 longestMatchLength; + UInt32 numPairs; + UInt32 numAvail; + COptimal opt[kNumOpts]; + + #ifndef LZMA_LOG_BSR + Byte g_FastPos[1 << kNumLogBits]; + #endif + + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; + UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1]; + UInt32 numFastBytes; + UInt32 additionalOffset; + UInt32 reps[LZMA_NUM_REPS]; + UInt32 state; + + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax]; + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances]; + UInt32 alignPrices[kAlignTableSize]; + UInt32 alignPriceCount; + + UInt32 distTableSize; + + unsigned lc, lp, pb; + unsigned lpMask, pbMask; + + CLzmaProb *litProbs; + + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; + CLzmaProb isRep[kNumStates]; + CLzmaProb isRepG0[kNumStates]; + CLzmaProb isRepG1[kNumStates]; + CLzmaProb isRepG2[kNumStates]; + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; + + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; + CLzmaProb posAlignEncoder[1 << kNumAlignBits]; + + CLenPriceEnc lenEnc; + CLenPriceEnc repLenEnc; + + unsigned lclp; + + Bool fastMode; + + CRangeEnc rc; + + Bool writeEndMark; + UInt64 nowPos64; + UInt32 matchPriceCount; + Bool finished; + Bool multiThread; + + SRes result; + UInt32 dictSize; + UInt32 matchFinderCycles; + + ISeqInStream *inStream; + CSeqInStreamBuf seqBufInStream; + + CSaveState saveState; +} CLzmaEnc; + +void LzmaEnc_SaveState(CLzmaEncHandle pp) +{ + CLzmaEnc *p = (CLzmaEnc *)pp; + CSaveState *dest = &p->saveState; + int i; + dest->lenEnc = p->lenEnc; + dest->repLenEnc = p->repLenEnc; + dest->state = p->state; + + for (i = 0; i < kNumStates; i++) + { + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); + } + for (i = 0; i < kNumLenToPosStates; i++) + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i])); + memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); + memcpy(dest->reps, p->reps, sizeof(p->reps)); + memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb)); +} + +void LzmaEnc_RestoreState(CLzmaEncHandle pp) +{ + CLzmaEnc *dest = (CLzmaEnc *)pp; + const CSaveState *p = &dest->saveState; + int i; + dest->lenEnc = p->lenEnc; + dest->repLenEnc = p->repLenEnc; + dest->state = p->state; + + for (i = 0; i < kNumStates; i++) + { + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); + } + for (i = 0; i < kNumLenToPosStates; i++) + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i])); + memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); + memcpy(dest->reps, p->reps, sizeof(p->reps)); + memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb)); +} + +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2) +{ + CLzmaEnc *p = (CLzmaEnc *)pp; + CLzmaEncProps props = *props2; + LzmaEncProps_Normalize(&props); + + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX || + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30)) + return SZ_ERROR_PARAM; + p->dictSize = props.dictSize; + p->matchFinderCycles = props.mc; + { + unsigned fb = props.fb; + if (fb < 5) + fb = 5; + if (fb > LZMA_MATCH_LEN_MAX) + fb = LZMA_MATCH_LEN_MAX; + p->numFastBytes = fb; + } + p->lc = props.lc; + p->lp = props.lp; + p->pb = props.pb; + p->fastMode = (props.algo == 0); + p->matchFinderBase.btMode = props.btMode; + { + UInt32 numHashBytes = 4; + if (props.btMode) + { + if (props.numHashBytes < 2) + numHashBytes = 2; + else if (props.numHashBytes < 4) + numHashBytes = props.numHashBytes; + } + p->matchFinderBase.numHashBytes = numHashBytes; + } + + p->matchFinderBase.cutValue = props.mc; + + p->writeEndMark = props.writeEndMark; + + #ifdef COMPRESS_MF_MT + /* + if (newMultiThread != _multiThread) + { + ReleaseMatchFinder(); + _multiThread = newMultiThread; + } + */ + p->multiThread = (props.numThreads > 1); + #endif + + return SZ_OK; +} + +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5}; +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10}; +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11}; +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11}; + +#define IsCharState(s) ((s) < 7) + +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1) + +#define kInfinityPrice (1 << 30) + +static void RangeEnc_Construct(CRangeEnc *p) +{ + p->outStream = 0; + p->bufBase = 0; +} + +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize) + +#define RC_BUF_SIZE (1 << 16) +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc) +{ + if (p->bufBase == 0) + { + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE); + if (p->bufBase == 0) + return 0; + p->bufLim = p->bufBase + RC_BUF_SIZE; + } + return 1; +} + +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc) +{ + alloc->Free(alloc, p->bufBase); + p->bufBase = 0; +} + +static void RangeEnc_Init(CRangeEnc *p) +{ + /* Stream.Init(); */ + p->low = 0; + p->range = 0xFFFFFFFF; + p->cacheSize = 1; + p->cache = 0; + + p->buf = p->bufBase; + + p->processed = 0; + p->res = SZ_OK; +} + +static void RangeEnc_FlushStream(CRangeEnc *p) +{ + size_t num; + if (p->res != SZ_OK) + return; + num = p->buf - p->bufBase; + if (num != p->outStream->Write(p->outStream, p->bufBase, num)) + p->res = SZ_ERROR_WRITE; + p->processed += num; + p->buf = p->bufBase; +} + +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p) +{ + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0) + { + Byte temp = p->cache; + do + { + Byte *buf = p->buf; + *buf++ = (Byte)(temp + (Byte)(p->low >> 32)); + p->buf = buf; + if (buf == p->bufLim) + RangeEnc_FlushStream(p); + temp = 0xFF; + } + while (--p->cacheSize != 0); + p->cache = (Byte)((UInt32)p->low >> 24); + } + p->cacheSize++; + p->low = (UInt32)p->low << 8; +} + +static void RangeEnc_FlushData(CRangeEnc *p) +{ + int i; + for (i = 0; i < 5; i++) + RangeEnc_ShiftLow(p); +} + +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits) +{ + do + { + p->range >>= 1; + p->low += p->range & (0 - ((value >> --numBits) & 1)); + if (p->range < kTopValue) + { + p->range <<= 8; + RangeEnc_ShiftLow(p); + } + } + while (numBits != 0); +} + +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol) +{ + UInt32 ttt = *prob; + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt; + if (symbol == 0) + { + p->range = newBound; + ttt += (kBitModelTotal - ttt) >> kNumMoveBits; + } + else + { + p->low += newBound; + p->range -= newBound; + ttt -= ttt >> kNumMoveBits; + } + *prob = (CLzmaProb)ttt; + if (p->range < kTopValue) + { + p->range <<= 8; + RangeEnc_ShiftLow(p); + } +} + +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol) +{ + symbol |= 0x100; + do + { + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1); + symbol <<= 1; + } + while (symbol < 0x10000); +} + +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte) +{ + UInt32 offs = 0x100; + symbol |= 0x100; + do + { + matchByte <<= 1; + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1); + symbol <<= 1; + offs &= ~(matchByte ^ symbol); + } + while (symbol < 0x10000); +} + +void LzmaEnc_InitPriceTables(UInt32 *ProbPrices) +{ + UInt32 i; + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits)) + { + const int kCyclesBits = kNumBitPriceShiftBits; + UInt32 w = i; + UInt32 bitCount = 0; + int j; + for (j = 0; j < kCyclesBits; j++) + { + w = w * w; + bitCount <<= 1; + while (w >= ((UInt32)1 << 16)) + { + w >>= 1; + bitCount++; + } + } + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount); + } +} + + +#define GET_PRICE(prob, symbol) \ + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; + +#define GET_PRICEa(prob, symbol) \ + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; + +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits] +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] + +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits] +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] + +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices) +{ + UInt32 price = 0; + symbol |= 0x100; + do + { + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1); + symbol <<= 1; + } + while (symbol < 0x10000); + return price; +} + +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices) +{ + UInt32 price = 0; + UInt32 offs = 0x100; + symbol |= 0x100; + do + { + matchByte <<= 1; + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1); + symbol <<= 1; + offs &= ~(matchByte ^ symbol); + } + while (symbol < 0x10000); + return price; +} + + +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol) +{ + UInt32 m = 1; + int i; + for (i = numBitLevels; i != 0;) + { + UInt32 bit; + i--; + bit = (symbol >> i) & 1; + RangeEnc_EncodeBit(rc, probs + m, bit); + m = (m << 1) | bit; + } +} + +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol) +{ + UInt32 m = 1; + int i; + for (i = 0; i < numBitLevels; i++) + { + UInt32 bit = symbol & 1; + RangeEnc_EncodeBit(rc, probs + m, bit); + m = (m << 1) | bit; + symbol >>= 1; + } +} + +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices) +{ + UInt32 price = 0; + symbol |= (1 << numBitLevels); + while (symbol != 1) + { + price += GET_PRICEa(probs[symbol >> 1], symbol & 1); + symbol >>= 1; + } + return price; +} + +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices) +{ + UInt32 price = 0; + UInt32 m = 1; + int i; + for (i = numBitLevels; i != 0; i--) + { + UInt32 bit = symbol & 1; + symbol >>= 1; + price += GET_PRICEa(probs[m], bit); + m = (m << 1) | bit; + } + return price; +} + + +static void LenEnc_Init(CLenEnc *p) +{ + unsigned i; + p->choice = p->choice2 = kProbInitValue; + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++) + p->low[i] = kProbInitValue; + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++) + p->mid[i] = kProbInitValue; + for (i = 0; i < kLenNumHighSymbols; i++) + p->high[i] = kProbInitValue; +} + +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState) +{ + if (symbol < kLenNumLowSymbols) + { + RangeEnc_EncodeBit(rc, &p->choice, 0); + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol); + } + else + { + RangeEnc_EncodeBit(rc, &p->choice, 1); + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols) + { + RangeEnc_EncodeBit(rc, &p->choice2, 0); + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols); + } + else + { + RangeEnc_EncodeBit(rc, &p->choice2, 1); + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols); + } + } +} + +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices) +{ + UInt32 a0 = GET_PRICE_0a(p->choice); + UInt32 a1 = GET_PRICE_1a(p->choice); + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2); + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2); + UInt32 i = 0; + for (i = 0; i < kLenNumLowSymbols; i++) + { + if (i >= numSymbols) + return; + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices); + } + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++) + { + if (i >= numSymbols) + return; + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices); + } + for (; i < numSymbols; i++) + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices); +} + +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices) +{ + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices); + p->counters[posState] = p->tableSize; +} + +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices) +{ + UInt32 posState; + for (posState = 0; posState < numPosStates; posState++) + LenPriceEnc_UpdateTable(p, posState, ProbPrices); +} + +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices) +{ + LenEnc_Encode(&p->p, rc, symbol, posState); + if (updatePrice) + if (--p->counters[posState] == 0) + LenPriceEnc_UpdateTable(p, posState, ProbPrices); +} + + + + +static void MovePos(CLzmaEnc *p, UInt32 num) +{ + #ifdef SHOW_STAT + ttt += num; + printf("\n MovePos %d", num); + #endif + if (num != 0) + { + p->additionalOffset += num; + p->matchFinder.Skip(p->matchFinderObj, num); + } +} + +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes) +{ + UInt32 lenRes = 0, numPairs; + p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); + numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches); + #ifdef SHOW_STAT + printf("\n i = %d numPairs = %d ", ttt, numPairs / 2); + ttt++; + { + UInt32 i; + for (i = 0; i < numPairs; i += 2) + printf("%2d %6d | ", p->matches[i], p->matches[i + 1]); + } + #endif + if (numPairs > 0) + { + lenRes = p->matches[numPairs - 2]; + if (lenRes == p->numFastBytes) + { + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + UInt32 distance = p->matches[numPairs - 1] + 1; + UInt32 numAvail = p->numAvail; + if (numAvail > LZMA_MATCH_LEN_MAX) + numAvail = LZMA_MATCH_LEN_MAX; + { + const Byte *pby2 = pby - distance; + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++); + } + } + } + p->additionalOffset++; + *numDistancePairsRes = numPairs; + return lenRes; +} + + +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False; +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False; +#define IsShortRep(p) ((p)->backPrev == 0) + +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState) +{ + return + GET_PRICE_0(p->isRepG0[state]) + + GET_PRICE_0(p->isRep0Long[state][posState]); +} + +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState) +{ + UInt32 price; + if (repIndex == 0) + { + price = GET_PRICE_0(p->isRepG0[state]); + price += GET_PRICE_1(p->isRep0Long[state][posState]); + } + else + { + price = GET_PRICE_1(p->isRepG0[state]); + if (repIndex == 1) + price += GET_PRICE_0(p->isRepG1[state]); + else + { + price += GET_PRICE_1(p->isRepG1[state]); + price += GET_PRICE(p->isRepG2[state], repIndex - 2); + } + } + return price; +} + +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState) +{ + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] + + GetPureRepPrice(p, repIndex, state, posState); +} + +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur) +{ + UInt32 posMem = p->opt[cur].posPrev; + UInt32 backMem = p->opt[cur].backPrev; + p->optimumEndIndex = cur; + do + { + if (p->opt[cur].prev1IsChar) + { + MakeAsChar(&p->opt[posMem]) + p->opt[posMem].posPrev = posMem - 1; + if (p->opt[cur].prev2) + { + p->opt[posMem - 1].prev1IsChar = False; + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2; + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2; + } + } + { + UInt32 posPrev = posMem; + UInt32 backCur = backMem; + + backMem = p->opt[posPrev].backPrev; + posMem = p->opt[posPrev].posPrev; + + p->opt[posPrev].backPrev = backCur; + p->opt[posPrev].posPrev = cur; + cur = posPrev; + } + } + while (cur != 0); + *backRes = p->opt[0].backPrev; + p->optimumCurrentIndex = p->opt[0].posPrev; + return p->optimumCurrentIndex; +} + +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300) + +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes) +{ + UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur; + UInt32 matchPrice, repMatchPrice, normalMatchPrice; + UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS]; + UInt32 *matches; + const Byte *data; + Byte curByte, matchByte; + if (p->optimumEndIndex != p->optimumCurrentIndex) + { + const COptimal *opt = &p->opt[p->optimumCurrentIndex]; + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex; + *backRes = opt->backPrev; + p->optimumCurrentIndex = opt->posPrev; + return lenRes; + } + p->optimumCurrentIndex = p->optimumEndIndex = 0; + + if (p->additionalOffset == 0) + mainLen = ReadMatchDistances(p, &numPairs); + else + { + mainLen = p->longestMatchLength; + numPairs = p->numPairs; + } + + numAvail = p->numAvail; + if (numAvail < 2) + { + *backRes = (UInt32)(-1); + return 1; + } + if (numAvail > LZMA_MATCH_LEN_MAX) + numAvail = LZMA_MATCH_LEN_MAX; + + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + repMaxIndex = 0; + for (i = 0; i < LZMA_NUM_REPS; i++) + { + UInt32 lenTest; + const Byte *data2; + reps[i] = p->reps[i]; + data2 = data - (reps[i] + 1); + if (data[0] != data2[0] || data[1] != data2[1]) + { + repLens[i] = 0; + continue; + } + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++); + repLens[i] = lenTest; + if (lenTest > repLens[repMaxIndex]) + repMaxIndex = i; + } + if (repLens[repMaxIndex] >= p->numFastBytes) + { + UInt32 lenRes; + *backRes = repMaxIndex; + lenRes = repLens[repMaxIndex]; + MovePos(p, lenRes - 1); + return lenRes; + } + + matches = p->matches; + if (mainLen >= p->numFastBytes) + { + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS; + MovePos(p, mainLen - 1); + return mainLen; + } + curByte = *data; + matchByte = *(data - (reps[0] + 1)); + + if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2) + { + *backRes = (UInt32)-1; + return 1; + } + + p->opt[0].state = (CState)p->state; + + posState = (position & p->pbMask); + + { + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1)); + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) + + (!IsCharState(p->state) ? + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) : + LitEnc_GetPrice(probs, curByte, p->ProbPrices)); + } + + MakeAsChar(&p->opt[1]); + + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]); + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]); + + if (matchByte == curByte) + { + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState); + if (shortRepPrice < p->opt[1].price) + { + p->opt[1].price = shortRepPrice; + MakeAsShortRep(&p->opt[1]); + } + } + lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]); + + if (lenEnd < 2) + { + *backRes = p->opt[1].backPrev; + return 1; + } + + p->opt[1].posPrev = 0; + for (i = 0; i < LZMA_NUM_REPS; i++) + p->opt[0].backs[i] = reps[i]; + + len = lenEnd; + do + p->opt[len--].price = kInfinityPrice; + while (len >= 2); + + for (i = 0; i < LZMA_NUM_REPS; i++) + { + UInt32 repLen = repLens[i]; + UInt32 price; + if (repLen < 2) + continue; + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState); + do + { + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2]; + COptimal *opt = &p->opt[repLen]; + if (curAndLenPrice < opt->price) + { + opt->price = curAndLenPrice; + opt->posPrev = 0; + opt->backPrev = i; + opt->prev1IsChar = False; + } + } + while (--repLen >= 2); + } + + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]); + + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2); + if (len <= mainLen) + { + UInt32 offs = 0; + while (len > matches[offs]) + offs += 2; + for (; ; len++) + { + COptimal *opt; + UInt32 distance = matches[offs + 1]; + + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN]; + UInt32 lenToPosState = GetLenToPosState(len); + if (distance < kNumFullDistances) + curAndLenPrice += p->distancesPrices[lenToPosState][distance]; + else + { + UInt32 slot; + GetPosSlot2(distance, slot); + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot]; + } + opt = &p->opt[len]; + if (curAndLenPrice < opt->price) + { + opt->price = curAndLenPrice; + opt->posPrev = 0; + opt->backPrev = distance + LZMA_NUM_REPS; + opt->prev1IsChar = False; + } + if (len == matches[offs]) + { + offs += 2; + if (offs == numPairs) + break; + } + } + } + + cur = 0; + + #ifdef SHOW_STAT2 + if (position >= 0) + { + unsigned i; + printf("\n pos = %4X", position); + for (i = cur; i <= lenEnd; i++) + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price); + } + #endif + + for (;;) + { + UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen; + UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice; + Bool nextIsChar; + Byte curByte, matchByte; + const Byte *data; + COptimal *curOpt; + COptimal *nextOpt; + + cur++; + if (cur == lenEnd) + return Backward(p, backRes, cur); + + newLen = ReadMatchDistances(p, &numPairs); + if (newLen >= p->numFastBytes) + { + p->numPairs = numPairs; + p->longestMatchLength = newLen; + return Backward(p, backRes, cur); + } + position++; + curOpt = &p->opt[cur]; + posPrev = curOpt->posPrev; + if (curOpt->prev1IsChar) + { + posPrev--; + if (curOpt->prev2) + { + state = p->opt[curOpt->posPrev2].state; + if (curOpt->backPrev2 < LZMA_NUM_REPS) + state = kRepNextStates[state]; + else + state = kMatchNextStates[state]; + } + else + state = p->opt[posPrev].state; + state = kLiteralNextStates[state]; + } + else + state = p->opt[posPrev].state; + if (posPrev == cur - 1) + { + if (IsShortRep(curOpt)) + state = kShortRepNextStates[state]; + else + state = kLiteralNextStates[state]; + } + else + { + UInt32 pos; + const COptimal *prevOpt; + if (curOpt->prev1IsChar && curOpt->prev2) + { + posPrev = curOpt->posPrev2; + pos = curOpt->backPrev2; + state = kRepNextStates[state]; + } + else + { + pos = curOpt->backPrev; + if (pos < LZMA_NUM_REPS) + state = kRepNextStates[state]; + else + state = kMatchNextStates[state]; + } + prevOpt = &p->opt[posPrev]; + if (pos < LZMA_NUM_REPS) + { + UInt32 i; + reps[0] = prevOpt->backs[pos]; + for (i = 1; i <= pos; i++) + reps[i] = prevOpt->backs[i - 1]; + for (; i < LZMA_NUM_REPS; i++) + reps[i] = prevOpt->backs[i]; + } + else + { + UInt32 i; + reps[0] = (pos - LZMA_NUM_REPS); + for (i = 1; i < LZMA_NUM_REPS; i++) + reps[i] = prevOpt->backs[i - 1]; + } + } + curOpt->state = (CState)state; + + curOpt->backs[0] = reps[0]; + curOpt->backs[1] = reps[1]; + curOpt->backs[2] = reps[2]; + curOpt->backs[3] = reps[3]; + + curPrice = curOpt->price; + nextIsChar = False; + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + curByte = *data; + matchByte = *(data - (reps[0] + 1)); + + posState = (position & p->pbMask); + + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]); + { + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1)); + curAnd1Price += + (!IsCharState(state) ? + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) : + LitEnc_GetPrice(probs, curByte, p->ProbPrices)); + } + + nextOpt = &p->opt[cur + 1]; + + if (curAnd1Price < nextOpt->price) + { + nextOpt->price = curAnd1Price; + nextOpt->posPrev = cur; + MakeAsChar(nextOpt); + nextIsChar = True; + } + + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]); + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]); + + if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0)) + { + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState); + if (shortRepPrice <= nextOpt->price) + { + nextOpt->price = shortRepPrice; + nextOpt->posPrev = cur; + MakeAsShortRep(nextOpt); + nextIsChar = True; + } + } + numAvailFull = p->numAvail; + { + UInt32 temp = kNumOpts - 1 - cur; + if (temp < numAvailFull) + numAvailFull = temp; + } + + if (numAvailFull < 2) + continue; + numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes); + + if (!nextIsChar && matchByte != curByte) /* speed optimization */ + { + /* try Literal + rep0 */ + UInt32 temp; + UInt32 lenTest2; + const Byte *data2 = data - (reps[0] + 1); + UInt32 limit = p->numFastBytes + 1; + if (limit > numAvailFull) + limit = numAvailFull; + + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++); + lenTest2 = temp - 1; + if (lenTest2 >= 2) + { + UInt32 state2 = kLiteralNextStates[state]; + UInt32 posStateNext = (position + 1) & p->pbMask; + UInt32 nextRepMatchPrice = curAnd1Price + + GET_PRICE_1(p->isMatch[state2][posStateNext]) + + GET_PRICE_1(p->isRep[state2]); + /* for (; lenTest2 >= 2; lenTest2--) */ + { + UInt32 curAndLenPrice; + COptimal *opt; + UInt32 offset = cur + 1 + lenTest2; + while (lenEnd < offset) + p->opt[++lenEnd].price = kInfinityPrice; + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); + opt = &p->opt[offset]; + if (curAndLenPrice < opt->price) + { + opt->price = curAndLenPrice; + opt->posPrev = cur + 1; + opt->backPrev = 0; + opt->prev1IsChar = True; + opt->prev2 = False; + } + } + } + } + + startLen = 2; /* speed optimization */ + { + UInt32 repIndex; + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++) + { + UInt32 lenTest; + UInt32 lenTestTemp; + UInt32 price; + const Byte *data2 = data - (reps[repIndex] + 1); + if (data[0] != data2[0] || data[1] != data2[1]) + continue; + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++); + while (lenEnd < cur + lenTest) + p->opt[++lenEnd].price = kInfinityPrice; + lenTestTemp = lenTest; + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState); + do + { + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2]; + COptimal *opt = &p->opt[cur + lenTest]; + if (curAndLenPrice < opt->price) + { + opt->price = curAndLenPrice; + opt->posPrev = cur; + opt->backPrev = repIndex; + opt->prev1IsChar = False; + } + } + while (--lenTest >= 2); + lenTest = lenTestTemp; + + if (repIndex == 0) + startLen = lenTest + 1; + + /* if (_maxMode) */ + { + UInt32 lenTest2 = lenTest + 1; + UInt32 limit = lenTest2 + p->numFastBytes; + UInt32 nextRepMatchPrice; + if (limit > numAvailFull) + limit = numAvailFull; + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++); + lenTest2 -= lenTest + 1; + if (lenTest2 >= 2) + { + UInt32 state2 = kRepNextStates[state]; + UInt32 posStateNext = (position + lenTest) & p->pbMask; + UInt32 curAndLenCharPrice = + price + p->repLenEnc.prices[posState][lenTest - 2] + + GET_PRICE_0(p->isMatch[state2][posStateNext]) + + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]), + data[lenTest], data2[lenTest], p->ProbPrices); + state2 = kLiteralNextStates[state2]; + posStateNext = (position + lenTest + 1) & p->pbMask; + nextRepMatchPrice = curAndLenCharPrice + + GET_PRICE_1(p->isMatch[state2][posStateNext]) + + GET_PRICE_1(p->isRep[state2]); + + /* for (; lenTest2 >= 2; lenTest2--) */ + { + UInt32 curAndLenPrice; + COptimal *opt; + UInt32 offset = cur + lenTest + 1 + lenTest2; + while (lenEnd < offset) + p->opt[++lenEnd].price = kInfinityPrice; + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); + opt = &p->opt[offset]; + if (curAndLenPrice < opt->price) + { + opt->price = curAndLenPrice; + opt->posPrev = cur + lenTest + 1; + opt->backPrev = 0; + opt->prev1IsChar = True; + opt->prev2 = True; + opt->posPrev2 = cur; + opt->backPrev2 = repIndex; + } + } + } + } + } + } + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */ + if (newLen > numAvail) + { + newLen = numAvail; + for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2); + matches[numPairs] = newLen; + numPairs += 2; + } + if (newLen >= startLen) + { + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]); + UInt32 offs, curBack, posSlot; + UInt32 lenTest; + while (lenEnd < cur + newLen) + p->opt[++lenEnd].price = kInfinityPrice; + + offs = 0; + while (startLen > matches[offs]) + offs += 2; + curBack = matches[offs + 1]; + GetPosSlot2(curBack, posSlot); + for (lenTest = /*2*/ startLen; ; lenTest++) + { + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN]; + UInt32 lenToPosState = GetLenToPosState(lenTest); + COptimal *opt; + if (curBack < kNumFullDistances) + curAndLenPrice += p->distancesPrices[lenToPosState][curBack]; + else + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask]; + + opt = &p->opt[cur + lenTest]; + if (curAndLenPrice < opt->price) + { + opt->price = curAndLenPrice; + opt->posPrev = cur; + opt->backPrev = curBack + LZMA_NUM_REPS; + opt->prev1IsChar = False; + } + + if (/*_maxMode && */lenTest == matches[offs]) + { + /* Try Match + Literal + Rep0 */ + const Byte *data2 = data - (curBack + 1); + UInt32 lenTest2 = lenTest + 1; + UInt32 limit = lenTest2 + p->numFastBytes; + UInt32 nextRepMatchPrice; + if (limit > numAvailFull) + limit = numAvailFull; + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++); + lenTest2 -= lenTest + 1; + if (lenTest2 >= 2) + { + UInt32 state2 = kMatchNextStates[state]; + UInt32 posStateNext = (position + lenTest) & p->pbMask; + UInt32 curAndLenCharPrice = curAndLenPrice + + GET_PRICE_0(p->isMatch[state2][posStateNext]) + + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]), + data[lenTest], data2[lenTest], p->ProbPrices); + state2 = kLiteralNextStates[state2]; + posStateNext = (posStateNext + 1) & p->pbMask; + nextRepMatchPrice = curAndLenCharPrice + + GET_PRICE_1(p->isMatch[state2][posStateNext]) + + GET_PRICE_1(p->isRep[state2]); + + /* for (; lenTest2 >= 2; lenTest2--) */ + { + UInt32 offset = cur + lenTest + 1 + lenTest2; + UInt32 curAndLenPrice; + COptimal *opt; + while (lenEnd < offset) + p->opt[++lenEnd].price = kInfinityPrice; + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); + opt = &p->opt[offset]; + if (curAndLenPrice < opt->price) + { + opt->price = curAndLenPrice; + opt->posPrev = cur + lenTest + 1; + opt->backPrev = 0; + opt->prev1IsChar = True; + opt->prev2 = True; + opt->posPrev2 = cur; + opt->backPrev2 = curBack + LZMA_NUM_REPS; + } + } + } + offs += 2; + if (offs == numPairs) + break; + curBack = matches[offs + 1]; + if (curBack >= kNumFullDistances) + GetPosSlot2(curBack, posSlot); + } + } + } + } +} + +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist)) + +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes) +{ + UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i; + const Byte *data; + const UInt32 *matches; + + if (p->additionalOffset == 0) + mainLen = ReadMatchDistances(p, &numPairs); + else + { + mainLen = p->longestMatchLength; + numPairs = p->numPairs; + } + + numAvail = p->numAvail; + *backRes = (UInt32)-1; + if (numAvail < 2) + return 1; + if (numAvail > LZMA_MATCH_LEN_MAX) + numAvail = LZMA_MATCH_LEN_MAX; + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + + repLen = repIndex = 0; + for (i = 0; i < LZMA_NUM_REPS; i++) + { + UInt32 len; + const Byte *data2 = data - (p->reps[i] + 1); + if (data[0] != data2[0] || data[1] != data2[1]) + continue; + for (len = 2; len < numAvail && data[len] == data2[len]; len++); + if (len >= p->numFastBytes) + { + *backRes = i; + MovePos(p, len - 1); + return len; + } + if (len > repLen) + { + repIndex = i; + repLen = len; + } + } + + matches = p->matches; + if (mainLen >= p->numFastBytes) + { + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS; + MovePos(p, mainLen - 1); + return mainLen; + } + + mainDist = 0; /* for GCC */ + if (mainLen >= 2) + { + mainDist = matches[numPairs - 1]; + while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1) + { + if (!ChangePair(matches[numPairs - 3], mainDist)) + break; + numPairs -= 2; + mainLen = matches[numPairs - 2]; + mainDist = matches[numPairs - 1]; + } + if (mainLen == 2 && mainDist >= 0x80) + mainLen = 1; + } + + if (repLen >= 2 && ( + (repLen + 1 >= mainLen) || + (repLen + 2 >= mainLen && mainDist >= (1 << 9)) || + (repLen + 3 >= mainLen && mainDist >= (1 << 15)))) + { + *backRes = repIndex; + MovePos(p, repLen - 1); + return repLen; + } + + if (mainLen < 2 || numAvail <= 2) + return 1; + + p->longestMatchLength = ReadMatchDistances(p, &p->numPairs); + if (p->longestMatchLength >= 2) + { + UInt32 newDistance = matches[p->numPairs - 1]; + if ((p->longestMatchLength >= mainLen && newDistance < mainDist) || + (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) || + (p->longestMatchLength > mainLen + 1) || + (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist))) + return 1; + } + + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + for (i = 0; i < LZMA_NUM_REPS; i++) + { + UInt32 len, limit; + const Byte *data2 = data - (p->reps[i] + 1); + if (data[0] != data2[0] || data[1] != data2[1]) + continue; + limit = mainLen - 1; + for (len = 2; len < limit && data[len] == data2[len]; len++); + if (len >= limit) + return 1; + } + *backRes = mainDist + LZMA_NUM_REPS; + MovePos(p, mainLen - 2); + return mainLen; +} + +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState) +{ + UInt32 len; + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1); + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0); + p->state = kMatchNextStates[p->state]; + len = LZMA_MATCH_LEN_MIN; + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1); + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits); + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask); +} + +static SRes CheckErrors(CLzmaEnc *p) +{ + if (p->result != SZ_OK) + return p->result; + if (p->rc.res != SZ_OK) + p->result = SZ_ERROR_WRITE; + if (p->matchFinderBase.result != SZ_OK) + p->result = SZ_ERROR_READ; + if (p->result != SZ_OK) + p->finished = True; + return p->result; +} + +static SRes Flush(CLzmaEnc *p, UInt32 nowPos) +{ + /* ReleaseMFStream(); */ + p->finished = True; + if (p->writeEndMark) + WriteEndMarker(p, nowPos & p->pbMask); + RangeEnc_FlushData(&p->rc); + RangeEnc_FlushStream(&p->rc); + return CheckErrors(p); +} + +static void FillAlignPrices(CLzmaEnc *p) +{ + UInt32 i; + for (i = 0; i < kAlignTableSize; i++) + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices); + p->alignPriceCount = 0; +} + +static void FillDistancesPrices(CLzmaEnc *p) +{ + UInt32 tempPrices[kNumFullDistances]; + UInt32 i, lenToPosState; + for (i = kStartPosModelIndex; i < kNumFullDistances; i++) + { + UInt32 posSlot = GetPosSlot1(i); + UInt32 footerBits = ((posSlot >> 1) - 1); + UInt32 base = ((2 | (posSlot & 1)) << footerBits); + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices); + } + + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++) + { + UInt32 posSlot; + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState]; + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState]; + for (posSlot = 0; posSlot < p->distTableSize; posSlot++) + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices); + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++) + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits); + + { + UInt32 *distancesPrices = p->distancesPrices[lenToPosState]; + UInt32 i; + for (i = 0; i < kStartPosModelIndex; i++) + distancesPrices[i] = posSlotPrices[i]; + for (; i < kNumFullDistances; i++) + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i]; + } + } + p->matchPriceCount = 0; +} + +void LzmaEnc_Construct(CLzmaEnc *p) +{ + RangeEnc_Construct(&p->rc); + MatchFinder_Construct(&p->matchFinderBase); + #ifdef COMPRESS_MF_MT + MatchFinderMt_Construct(&p->matchFinderMt); + p->matchFinderMt.MatchFinder = &p->matchFinderBase; + #endif + + { + CLzmaEncProps props; + LzmaEncProps_Init(&props); + LzmaEnc_SetProps(p, &props); + } + + #ifndef LZMA_LOG_BSR + LzmaEnc_FastPosInit(p->g_FastPos); + #endif + + LzmaEnc_InitPriceTables(p->ProbPrices); + p->litProbs = 0; + p->saveState.litProbs = 0; +} + +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc) +{ + void *p; + p = alloc->Alloc(alloc, sizeof(CLzmaEnc)); + if (p != 0) + LzmaEnc_Construct((CLzmaEnc *)p); + return p; +} + +void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc) +{ + alloc->Free(alloc, p->litProbs); + alloc->Free(alloc, p->saveState.litProbs); + p->litProbs = 0; + p->saveState.litProbs = 0; +} + +void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig) +{ + #ifdef COMPRESS_MF_MT + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig); + #endif + MatchFinder_Free(&p->matchFinderBase, allocBig); + LzmaEnc_FreeLits(p, alloc); + RangeEnc_Free(&p->rc, alloc); +} + +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig) +{ + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig); + alloc->Free(alloc, p); +} + +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize) +{ + UInt32 nowPos32, startPos32; + if (p->inStream != 0) + { + p->matchFinderBase.stream = p->inStream; + p->matchFinder.Init(p->matchFinderObj); + p->inStream = 0; + } + + if (p->finished) + return p->result; + RINOK(CheckErrors(p)); + + nowPos32 = (UInt32)p->nowPos64; + startPos32 = nowPos32; + + if (p->nowPos64 == 0) + { + UInt32 numPairs; + Byte curByte; + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) + return Flush(p, nowPos32); + ReadMatchDistances(p, &numPairs); + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0); + p->state = kLiteralNextStates[p->state]; + curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset); + LitEnc_Encode(&p->rc, p->litProbs, curByte); + p->additionalOffset--; + nowPos32++; + } + + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0) + for (;;) + { + UInt32 pos, len, posState; + + if (p->fastMode) + len = GetOptimumFast(p, &pos); + else + len = GetOptimum(p, nowPos32, &pos); + + #ifdef SHOW_STAT2 + printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos); + #endif + + posState = nowPos32 & p->pbMask; + if (len == 1 && pos == (UInt32)-1) + { + Byte curByte; + CLzmaProb *probs; + const Byte *data; + + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0); + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; + curByte = *data; + probs = LIT_PROBS(nowPos32, *(data - 1)); + if (IsCharState(p->state)) + LitEnc_Encode(&p->rc, probs, curByte); + else + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1)); + p->state = kLiteralNextStates[p->state]; + } + else + { + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1); + if (pos < LZMA_NUM_REPS) + { + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1); + if (pos == 0) + { + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0); + RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1)); + } + else + { + UInt32 distance = p->reps[pos]; + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1); + if (pos == 1) + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0); + else + { + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1); + RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2); + if (pos == 3) + p->reps[3] = p->reps[2]; + p->reps[2] = p->reps[1]; + } + p->reps[1] = p->reps[0]; + p->reps[0] = distance; + } + if (len == 1) + p->state = kShortRepNextStates[p->state]; + else + { + LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); + p->state = kRepNextStates[p->state]; + } + } + else + { + UInt32 posSlot; + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0); + p->state = kMatchNextStates[p->state]; + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); + pos -= LZMA_NUM_REPS; + GetPosSlot(pos, posSlot); + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot); + + if (posSlot >= kStartPosModelIndex) + { + UInt32 footerBits = ((posSlot >> 1) - 1); + UInt32 base = ((2 | (posSlot & 1)) << footerBits); + UInt32 posReduced = pos - base; + + if (posSlot < kEndPosModelIndex) + RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced); + else + { + RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits); + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask); + p->alignPriceCount++; + } + } + p->reps[3] = p->reps[2]; + p->reps[2] = p->reps[1]; + p->reps[1] = p->reps[0]; + p->reps[0] = pos; + p->matchPriceCount++; + } + } + p->additionalOffset -= len; + nowPos32 += len; + if (p->additionalOffset == 0) + { + UInt32 processed; + if (!p->fastMode) + { + if (p->matchPriceCount >= (1 << 7)) + FillDistancesPrices(p); + if (p->alignPriceCount >= kAlignTableSize) + FillAlignPrices(p); + } + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) + break; + processed = nowPos32 - startPos32; + if (useLimits) + { + if (processed + kNumOpts + 300 >= maxUnpackSize || + RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize) + break; + } + else if (processed >= (1 << 15)) + { + p->nowPos64 += nowPos32 - startPos32; + return CheckErrors(p); + } + } + } + p->nowPos64 += nowPos32 - startPos32; + return Flush(p, nowPos32); +} + +#define kBigHashDicLimit ((UInt32)1 << 24) + +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) +{ + UInt32 beforeSize = kNumOpts; + Bool btMode; + if (!RangeEnc_Alloc(&p->rc, alloc)) + return SZ_ERROR_MEM; + btMode = (p->matchFinderBase.btMode != 0); + #ifdef COMPRESS_MF_MT + p->mtMode = (p->multiThread && !p->fastMode && btMode); + #endif + + { + unsigned lclp = p->lc + p->lp; + if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp) + { + LzmaEnc_FreeLits(p, alloc); + p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb)); + p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb)); + if (p->litProbs == 0 || p->saveState.litProbs == 0) + { + LzmaEnc_FreeLits(p, alloc); + return SZ_ERROR_MEM; + } + p->lclp = lclp; + } + } + + p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit); + + if (beforeSize + p->dictSize < keepWindowSize) + beforeSize = keepWindowSize - p->dictSize; + + #ifdef COMPRESS_MF_MT + if (p->mtMode) + { + RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig)); + p->matchFinderObj = &p->matchFinderMt; + MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder); + } + else + #endif + { + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig)) + return SZ_ERROR_MEM; + p->matchFinderObj = &p->matchFinderBase; + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder); + } + return SZ_OK; +} + +void LzmaEnc_Init(CLzmaEnc *p) +{ + UInt32 i; + p->state = 0; + for (i = 0 ; i < LZMA_NUM_REPS; i++) + p->reps[i] = 0; + + RangeEnc_Init(&p->rc); + + + for (i = 0; i < kNumStates; i++) + { + UInt32 j; + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++) + { + p->isMatch[i][j] = kProbInitValue; + p->isRep0Long[i][j] = kProbInitValue; + } + p->isRep[i] = kProbInitValue; + p->isRepG0[i] = kProbInitValue; + p->isRepG1[i] = kProbInitValue; + p->isRepG2[i] = kProbInitValue; + } + + { + UInt32 num = 0x300 << (p->lp + p->lc); + for (i = 0; i < num; i++) + p->litProbs[i] = kProbInitValue; + } + + { + for (i = 0; i < kNumLenToPosStates; i++) + { + CLzmaProb *probs = p->posSlotEncoder[i]; + UInt32 j; + for (j = 0; j < (1 << kNumPosSlotBits); j++) + probs[j] = kProbInitValue; + } + } + { + for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++) + p->posEncoders[i] = kProbInitValue; + } + + LenEnc_Init(&p->lenEnc.p); + LenEnc_Init(&p->repLenEnc.p); + + for (i = 0; i < (1 << kNumAlignBits); i++) + p->posAlignEncoder[i] = kProbInitValue; + + p->optimumEndIndex = 0; + p->optimumCurrentIndex = 0; + p->additionalOffset = 0; + + p->pbMask = (1 << p->pb) - 1; + p->lpMask = (1 << p->lp) - 1; +} + +void LzmaEnc_InitPrices(CLzmaEnc *p) +{ + if (!p->fastMode) + { + FillDistancesPrices(p); + FillAlignPrices(p); + } + + p->lenEnc.tableSize = + p->repLenEnc.tableSize = + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN; + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices); + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices); +} + +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) +{ + UInt32 i; + for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++) + if (p->dictSize <= ((UInt32)1 << i)) + break; + p->distTableSize = i * 2; + + p->finished = False; + p->result = SZ_OK; + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig)); + LzmaEnc_Init(p); + LzmaEnc_InitPrices(p); + p->nowPos64 = 0; + return SZ_OK; +} + +static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqInStream *inStream, ISeqOutStream *outStream, + ISzAlloc *alloc, ISzAlloc *allocBig) +{ + CLzmaEnc *p = (CLzmaEnc *)pp; + p->inStream = inStream; + p->rc.outStream = outStream; + return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig); +} + +SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, + ISeqInStream *inStream, UInt32 keepWindowSize, + ISzAlloc *alloc, ISzAlloc *allocBig) +{ + CLzmaEnc *p = (CLzmaEnc *)pp; + p->inStream = inStream; + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); +} + +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen) +{ + p->seqBufInStream.funcTable.Read = MyRead; + p->seqBufInStream.data = src; + p->seqBufInStream.rem = srcLen; +} + +SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen, + UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) +{ + CLzmaEnc *p = (CLzmaEnc *)pp; + LzmaEnc_SetInputBuf(p, src, srcLen); + p->inStream = &p->seqBufInStream.funcTable; + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); +} + +void LzmaEnc_Finish(CLzmaEncHandle pp) +{ + #ifdef COMPRESS_MF_MT + CLzmaEnc *p = (CLzmaEnc *)pp; + if (p->mtMode) + MatchFinderMt_ReleaseStream(&p->matchFinderMt); + #else + pp = pp; + #endif +} + +typedef struct _CSeqOutStreamBuf +{ + ISeqOutStream funcTable; + Byte *data; + SizeT rem; + Bool overflow; +} CSeqOutStreamBuf; + +static size_t MyWrite(void *pp, const void *data, size_t size) +{ + CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp; + if (p->rem < size) + { + size = p->rem; + p->overflow = True; + } + memcpy(p->data, data, size); + p->rem -= size; + p->data += size; + return size; +} + + +UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp) +{ + const CLzmaEnc *p = (CLzmaEnc *)pp; + return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); +} + +const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp) +{ + const CLzmaEnc *p = (CLzmaEnc *)pp; + return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; +} + +SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit, + Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize) +{ + CLzmaEnc *p = (CLzmaEnc *)pp; + UInt64 nowPos64; + SRes res; + CSeqOutStreamBuf outStream; + + outStream.funcTable.Write = MyWrite; + outStream.data = dest; + outStream.rem = *destLen; + outStream.overflow = False; + + p->writeEndMark = False; + p->finished = False; + p->result = SZ_OK; + + if (reInit) + LzmaEnc_Init(p); + LzmaEnc_InitPrices(p); + nowPos64 = p->nowPos64; + RangeEnc_Init(&p->rc); + p->rc.outStream = &outStream.funcTable; + + res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize); + + *unpackSize = (UInt32)(p->nowPos64 - nowPos64); + *destLen -= outStream.rem; + if (outStream.overflow) + return SZ_ERROR_OUTPUT_EOF; + + return res; +} + +SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress, + ISzAlloc *alloc, ISzAlloc *allocBig) +{ + CLzmaEnc *p = (CLzmaEnc *)pp; + SRes res = SZ_OK; + + #ifdef COMPRESS_MF_MT + Byte allocaDummy[0x300]; + int i = 0; + for (i = 0; i < 16; i++) + allocaDummy[i] = (Byte)i; + #endif + + RINOK(LzmaEnc_Prepare(pp, inStream, outStream, alloc, allocBig)); + + for (;;) + { + res = LzmaEnc_CodeOneBlock(p, False, 0, 0); + if (res != SZ_OK || p->finished != 0) + break; + if (progress != 0) + { + res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc)); + if (res != SZ_OK) + { + res = SZ_ERROR_PROGRESS; + break; + } + } + } + LzmaEnc_Finish(pp); + return res; +} + +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size) +{ + CLzmaEnc *p = (CLzmaEnc *)pp; + int i; + UInt32 dictSize = p->dictSize; + if (*size < LZMA_PROPS_SIZE) + return SZ_ERROR_PARAM; + *size = LZMA_PROPS_SIZE; + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc); + + for (i = 11; i <= 30; i++) + { + if (dictSize <= ((UInt32)2 << i)) + { + dictSize = (2 << i); + break; + } + if (dictSize <= ((UInt32)3 << i)) + { + dictSize = (3 << i); + break; + } + } + + for (i = 0; i < 4; i++) + props[1 + i] = (Byte)(dictSize >> (8 * i)); + return SZ_OK; +} + +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) +{ + SRes res; + CLzmaEnc *p = (CLzmaEnc *)pp; + + CSeqOutStreamBuf outStream; + + LzmaEnc_SetInputBuf(p, src, srcLen); + + outStream.funcTable.Write = MyWrite; + outStream.data = dest; + outStream.rem = *destLen; + outStream.overflow = False; + + p->writeEndMark = writeEndMark; + res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable, + progress, alloc, allocBig); + + *destLen -= outStream.rem; + if (outStream.overflow) + return SZ_ERROR_OUTPUT_EOF; + return res; +} + +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) +{ + CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc); + SRes res; + if (p == 0) + return SZ_ERROR_MEM; + + res = LzmaEnc_SetProps(p, props); + if (res == SZ_OK) + { + res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize); + if (res == SZ_OK) + res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen, + writeEndMark, progress, alloc, allocBig); + } + + LzmaEnc_Destroy(p, alloc, allocBig); + return res; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/LzmaEnc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/LzmaEnc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,72 @@ +/* LzmaEnc.h -- LZMA Encoder +2008-10-04 : Igor Pavlov : Public domain */ + +#ifndef __LZMAENC_H +#define __LZMAENC_H + +#include "Types.h" + +#define LZMA_PROPS_SIZE 5 + +typedef struct _CLzmaEncProps +{ + int level; /* 0 <= level <= 9 */ + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version + (1 << 12) <= dictSize <= (1 << 30) for 64-bit version + default = (1 << 24) */ + int lc; /* 0 <= lc <= 8, default = 3 */ + int lp; /* 0 <= lp <= 4, default = 0 */ + int pb; /* 0 <= pb <= 4, default = 2 */ + int algo; /* 0 - fast, 1 - normal, default = 1 */ + int fb; /* 5 <= fb <= 273, default = 32 */ + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */ + int numHashBytes; /* 2, 3 or 4, default = 4 */ + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */ + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */ + int numThreads; /* 1 or 2, default = 2 */ +} CLzmaEncProps; + +void LzmaEncProps_Init(CLzmaEncProps *p); +void LzmaEncProps_Normalize(CLzmaEncProps *p); +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2); + + +/* ---------- CLzmaEncHandle Interface ---------- */ + +/* LzmaEnc_* functions can return the following exit codes: +Returns: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater in props + SZ_ERROR_WRITE - Write callback error. + SZ_ERROR_PROGRESS - some break from progress callback + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) +*/ + +typedef void * CLzmaEncHandle; + +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc); +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig); +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props); +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size); +SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream, + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); + +/* ---------- One Call Interface ---------- */ + +/* LzmaEncode +Return code: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater + SZ_ERROR_OUTPUT_EOF - output buffer overflow + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) +*/ + +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/RotateDefs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/RotateDefs.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,22 @@ +/* RotateDefs.h -- Rotate functions +2008-08-05 +Igor Pavlov +Public domain */ + +#ifndef __ROTATEDEFS_H +#define __ROTATEDEFS_H + +#ifdef _MSC_VER + +#include +#define rotlFixed(x, n) _rotl((x), (n)) +#define rotrFixed(x, n) _rotr((x), (n)) + +#else + +#define rotlFixed(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +#define rotrFixed(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) + +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Sha256.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Sha256.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,204 @@ +/* Crypto/Sha256.c -- SHA-256 Hash function +2008-11-06 : Igor Pavlov : Public domain +This code is based on public domain code from Wei Dai's Crypto++ library. */ + +#include "Sha256.h" +#include "RotateDefs.h" + +/* define it for speed optimization */ +/* #define _SHA256_UNROLL */ +/* #define _SHA256_UNROLL2 */ + +void Sha256_Init(CSha256 *p) +{ + p->state[0] = 0x6a09e667; + p->state[1] = 0xbb67ae85; + p->state[2] = 0x3c6ef372; + p->state[3] = 0xa54ff53a; + p->state[4] = 0x510e527f; + p->state[5] = 0x9b05688c; + p->state[6] = 0x1f83d9ab; + p->state[7] = 0x5be0cd19; + p->count = 0; +} + +#define S0(x) (rotrFixed(x, 2) ^ rotrFixed(x,13) ^ rotrFixed(x, 22)) +#define S1(x) (rotrFixed(x, 6) ^ rotrFixed(x,11) ^ rotrFixed(x, 25)) +#define s0(x) (rotrFixed(x, 7) ^ rotrFixed(x,18) ^ (x >> 3)) +#define s1(x) (rotrFixed(x,17) ^ rotrFixed(x,19) ^ (x >> 10)) + +#define blk0(i) (W[i] = data[i]) +#define blk2(i) (W[i&15] += s1(W[(i-2)&15]) + W[(i-7)&15] + s0(W[(i-15)&15])) + +#define Ch(x,y,z) (z^(x&(y^z))) +#define Maj(x,y,z) ((x&y)|(z&(x|y))) + +#define a(i) T[(0-(i))&7] +#define b(i) T[(1-(i))&7] +#define c(i) T[(2-(i))&7] +#define d(i) T[(3-(i))&7] +#define e(i) T[(4-(i))&7] +#define f(i) T[(5-(i))&7] +#define g(i) T[(6-(i))&7] +#define h(i) T[(7-(i))&7] + + +#ifdef _SHA256_UNROLL2 + +#define R(a,b,c,d,e,f,g,h, i) h += S1(e) + Ch(e,f,g) + K[i+j] + (j?blk2(i):blk0(i));\ + d += h; h += S0(a) + Maj(a, b, c) + +#define RX_8(i) \ + R(a,b,c,d,e,f,g,h, i); \ + R(h,a,b,c,d,e,f,g, i+1); \ + R(g,h,a,b,c,d,e,f, i+2); \ + R(f,g,h,a,b,c,d,e, i+3); \ + R(e,f,g,h,a,b,c,d, i+4); \ + R(d,e,f,g,h,a,b,c, i+5); \ + R(c,d,e,f,g,h,a,b, i+6); \ + R(b,c,d,e,f,g,h,a, i+7) + +#else + +#define R(i) h(i) += S1(e(i)) + Ch(e(i),f(i),g(i)) + K[i+j] + (j?blk2(i):blk0(i));\ + d(i) += h(i); h(i) += S0(a(i)) + Maj(a(i), b(i), c(i)) + +#ifdef _SHA256_UNROLL + +#define RX_8(i) R(i+0); R(i+1); R(i+2); R(i+3); R(i+4); R(i+5); R(i+6); R(i+7); + +#endif + +#endif + +const UInt32 K[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; + +static void Sha256_Transform(UInt32 *state, const UInt32 *data) +{ + UInt32 W[16]; + unsigned j; + #ifdef _SHA256_UNROLL2 + UInt32 a,b,c,d,e,f,g,h; + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + f = state[5]; + g = state[6]; + h = state[7]; + #else + UInt32 T[8]; + for (j = 0; j < 8; j++) + T[j] = state[j]; + #endif + + for (j = 0; j < 64; j += 16) + { + #if defined(_SHA256_UNROLL) || defined(_SHA256_UNROLL2) + RX_8(0); RX_8(8); + #else + unsigned i; + for (i = 0; i < 16; i++) { R(i); } + #endif + } + + #ifdef _SHA256_UNROLL2 + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + state[5] += f; + state[6] += g; + state[7] += h; + #else + for (j = 0; j < 8; j++) + state[j] += T[j]; + #endif + + /* Wipe variables */ + /* memset(W, 0, sizeof(W)); */ + /* memset(T, 0, sizeof(T)); */ +} + +#undef S0 +#undef S1 +#undef s0 +#undef s1 + +static void Sha256_WriteByteBlock(CSha256 *p) +{ + UInt32 data32[16]; + unsigned i; + for (i = 0; i < 16; i++) + data32[i] = + ((UInt32)(p->buffer[i * 4 ]) << 24) + + ((UInt32)(p->buffer[i * 4 + 1]) << 16) + + ((UInt32)(p->buffer[i * 4 + 2]) << 8) + + ((UInt32)(p->buffer[i * 4 + 3])); + Sha256_Transform(p->state, data32); +} + +void Sha256_Update(CSha256 *p, const Byte *data, size_t size) +{ + UInt32 curBufferPos = (UInt32)p->count & 0x3F; + while (size > 0) + { + p->buffer[curBufferPos++] = *data++; + p->count++; + size--; + if (curBufferPos == 64) + { + curBufferPos = 0; + Sha256_WriteByteBlock(p); + } + } +} + +void Sha256_Final(CSha256 *p, Byte *digest) +{ + UInt64 lenInBits = (p->count << 3); + UInt32 curBufferPos = (UInt32)p->count & 0x3F; + unsigned i; + p->buffer[curBufferPos++] = 0x80; + while (curBufferPos != (64 - 8)) + { + curBufferPos &= 0x3F; + if (curBufferPos == 0) + Sha256_WriteByteBlock(p); + p->buffer[curBufferPos++] = 0; + } + for (i = 0; i < 8; i++) + { + p->buffer[curBufferPos++] = (Byte)(lenInBits >> 56); + lenInBits <<= 8; + } + Sha256_WriteByteBlock(p); + + for (i = 0; i < 8; i++) + { + *digest++ = (Byte)((p->state[i] >> 24) & 0xFF); + *digest++ = (Byte)((p->state[i] >> 16) & 0xFF); + *digest++ = (Byte)((p->state[i] >> 8) & 0xFF); + *digest++ = (Byte)((p->state[i]) & 0xFF); + } + Sha256_Init(p); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Sha256.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Sha256.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,22 @@ +/* Crypto/Sha256.h -- SHA-256 Hash function +2008-10-04 : Igor Pavlov : Public domain */ + +#ifndef __CRYPTO_SHA256_H +#define __CRYPTO_SHA256_H + +#include "Types.h" + +#define SHA256_DIGEST_SIZE 32 + +typedef struct +{ + UInt32 state[8]; + UInt64 count; + Byte buffer[64]; +} CSha256; + +void Sha256_Init(CSha256 *p); +void Sha256_Update(CSha256 *p, const Byte *data, size_t size); +void Sha256_Final(CSha256 *p, Byte *digest); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Sort.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Sort.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,95 @@ +/* Sort.c -- Sort functions +2008-08-17 +Igor Pavlov +Public domain */ + +#include "Sort.h" + +#define HeapSortDown(p, k, size, temp) \ + { for (;;) { \ + UInt32 s = (k << 1); \ + if (s > size) break; \ + if (s < size && p[s + 1] > p[s]) s++; \ + if (temp >= p[s]) break; \ + p[k] = p[s]; k = s; \ + } p[k] = temp; } + +void HeapSort(UInt32 *p, UInt32 size) +{ + if (size <= 1) + return; + p--; + { + UInt32 i = size / 2; + do + { + UInt32 temp = p[i]; + UInt32 k = i; + HeapSortDown(p, k, size, temp) + } + while (--i != 0); + } + /* + do + { + UInt32 k = 1; + UInt32 temp = p[size]; + p[size--] = p[1]; + HeapSortDown(p, k, size, temp) + } + while (size > 1); + */ + while (size > 3) + { + UInt32 temp = p[size]; + UInt32 k = (p[3] > p[2]) ? 3 : 2; + p[size--] = p[1]; + p[1] = p[k]; + HeapSortDown(p, k, size, temp) + } + { + UInt32 temp = p[size]; + p[size] = p[1]; + if (size > 2 && p[2] < temp) + { + p[1] = p[2]; + p[2] = temp; + } + else + p[1] = temp; + } +} + +/* +#define HeapSortRefDown(p, vals, n, size, temp) \ + { UInt32 k = n; UInt32 val = vals[temp]; for (;;) { \ + UInt32 s = (k << 1); \ + if (s > size) break; \ + if (s < size && vals[p[s + 1]] > vals[p[s]]) s++; \ + if (val >= vals[p[s]]) break; \ + p[k] = p[s]; k = s; \ + } p[k] = temp; } + +void HeapSortRef(UInt32 *p, UInt32 *vals, UInt32 size) +{ + if (size <= 1) + return; + p--; + { + UInt32 i = size / 2; + do + { + UInt32 temp = p[i]; + HeapSortRefDown(p, vals, i, size, temp); + } + while (--i != 0); + } + do + { + UInt32 temp = p[size]; + p[size--] = p[1]; + HeapSortRefDown(p, vals, 1, size, temp); + } + while (size > 1); +} +*/ \ No newline at end of file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Sort.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Sort.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +/* Sort.h -- Sort functions +2008-03-19 +Igor Pavlov +Public domain */ + +#ifndef __7Z_SORT_H +#define __7Z_SORT_H + +#include "Types.h" + +void HeapSort(UInt32 *p, UInt32 size); +/* void HeapSortRef(UInt32 *p, UInt32 *vals, UInt32 size); */ + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Threads.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Threads.c Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,109 @@ +/* Threads.c -- multithreading library +2008-08-05 +Igor Pavlov +Public domain */ + +#include "Threads.h" +#include + +static WRes GetError() +{ + DWORD res = GetLastError(); + return (res) ? (WRes)(res) : 1; +} + +WRes HandleToWRes(HANDLE h) { return (h != 0) ? 0 : GetError(); } +WRes BOOLToWRes(BOOL v) { return v ? 0 : GetError(); } + +static WRes MyCloseHandle(HANDLE *h) +{ + if (*h != NULL) + if (!CloseHandle(*h)) + return GetError(); + *h = NULL; + return 0; +} + +WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), LPVOID parameter) +{ + unsigned threadId; /* Windows Me/98/95: threadId parameter may not be NULL in _beginthreadex/CreateThread functions */ + thread->handle = + /* CreateThread(0, 0, startAddress, parameter, 0, &threadId); */ + (HANDLE)_beginthreadex(NULL, 0, startAddress, parameter, 0, &threadId); + /* maybe we must use errno here, but probably GetLastError() is also OK. */ + return HandleToWRes(thread->handle); +} + +WRes WaitObject(HANDLE h) +{ + return (WRes)WaitForSingleObject(h, INFINITE); +} + +WRes Thread_Wait(CThread *thread) +{ + if (thread->handle == NULL) + return 1; + return WaitObject(thread->handle); +} + +WRes Thread_Close(CThread *thread) +{ + return MyCloseHandle(&thread->handle); +} + +WRes Event_Create(CEvent *p, BOOL manualReset, int initialSignaled) +{ + p->handle = CreateEvent(NULL, manualReset, (initialSignaled ? TRUE : FALSE), NULL); + return HandleToWRes(p->handle); +} + +WRes ManualResetEvent_Create(CManualResetEvent *p, int initialSignaled) + { return Event_Create(p, TRUE, initialSignaled); } +WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p) + { return ManualResetEvent_Create(p, 0); } + +WRes AutoResetEvent_Create(CAutoResetEvent *p, int initialSignaled) + { return Event_Create(p, FALSE, initialSignaled); } +WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p) + { return AutoResetEvent_Create(p, 0); } + +WRes Event_Set(CEvent *p) { return BOOLToWRes(SetEvent(p->handle)); } +WRes Event_Reset(CEvent *p) { return BOOLToWRes(ResetEvent(p->handle)); } +WRes Event_Wait(CEvent *p) { return WaitObject(p->handle); } +WRes Event_Close(CEvent *p) { return MyCloseHandle(&p->handle); } + + +WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount) +{ + p->handle = CreateSemaphore(NULL, (LONG)initiallyCount, (LONG)maxCount, NULL); + return HandleToWRes(p->handle); +} + +WRes Semaphore_Release(CSemaphore *p, LONG releaseCount, LONG *previousCount) +{ + return BOOLToWRes(ReleaseSemaphore(p->handle, releaseCount, previousCount)); +} +WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount) +{ + return Semaphore_Release(p, (LONG)releaseCount, NULL); +} +WRes Semaphore_Release1(CSemaphore *p) +{ + return Semaphore_ReleaseN(p, 1); +} + +WRes Semaphore_Wait(CSemaphore *p) { return WaitObject(p->handle); } +WRes Semaphore_Close(CSemaphore *p) { return MyCloseHandle(&p->handle); } + +WRes CriticalSection_Init(CCriticalSection *p) +{ + /* InitializeCriticalSection can raise only STATUS_NO_MEMORY exception */ + __try + { + InitializeCriticalSection(p); + /* InitializeCriticalSectionAndSpinCount(p, 0); */ + } + __except (EXCEPTION_EXECUTE_HANDLER) { return 1; } + return 0; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Threads.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Threads.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,68 @@ +/* Threads.h -- multithreading library +2008-11-22 : Igor Pavlov : Public domain */ + +#ifndef __7Z_THRESDS_H +#define __7Z_THRESDS_H + +#include "Types.h" + +typedef struct _CThread +{ + HANDLE handle; +} CThread; + +#define Thread_Construct(thread) (thread)->handle = NULL +#define Thread_WasCreated(thread) ((thread)->handle != NULL) + +typedef unsigned THREAD_FUNC_RET_TYPE; +#define THREAD_FUNC_CALL_TYPE MY_STD_CALL +#define THREAD_FUNC_DECL THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE + +WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), LPVOID parameter); +WRes Thread_Wait(CThread *thread); +WRes Thread_Close(CThread *thread); + +typedef struct _CEvent +{ + HANDLE handle; +} CEvent; + +typedef CEvent CAutoResetEvent; +typedef CEvent CManualResetEvent; + +#define Event_Construct(event) (event)->handle = NULL +#define Event_IsCreated(event) ((event)->handle != NULL) + +WRes ManualResetEvent_Create(CManualResetEvent *event, int initialSignaled); +WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *event); +WRes AutoResetEvent_Create(CAutoResetEvent *event, int initialSignaled); +WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *event); +WRes Event_Set(CEvent *event); +WRes Event_Reset(CEvent *event); +WRes Event_Wait(CEvent *event); +WRes Event_Close(CEvent *event); + + +typedef struct _CSemaphore +{ + HANDLE handle; +} CSemaphore; + +#define Semaphore_Construct(p) (p)->handle = NULL + +WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount); +WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num); +WRes Semaphore_Release1(CSemaphore *p); +WRes Semaphore_Wait(CSemaphore *p); +WRes Semaphore_Close(CSemaphore *p); + + +typedef CRITICAL_SECTION CCriticalSection; + +WRes CriticalSection_Init(CCriticalSection *p); +#define CriticalSection_Delete(p) DeleteCriticalSection(p) +#define CriticalSection_Enter(p) EnterCriticalSection(p) +#define CriticalSection_Leave(p) LeaveCriticalSection(p) + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/C/Types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/C/Types.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,208 @@ +/* Types.h -- Basic types +2008-11-23 : Igor Pavlov : Public domain */ + +#ifndef __7Z_TYPES_H +#define __7Z_TYPES_H + +#include + +#ifdef _WIN32 +#include +#endif + +#define SZ_OK 0 + +#define SZ_ERROR_DATA 1 +#define SZ_ERROR_MEM 2 +#define SZ_ERROR_CRC 3 +#define SZ_ERROR_UNSUPPORTED 4 +#define SZ_ERROR_PARAM 5 +#define SZ_ERROR_INPUT_EOF 6 +#define SZ_ERROR_OUTPUT_EOF 7 +#define SZ_ERROR_READ 8 +#define SZ_ERROR_WRITE 9 +#define SZ_ERROR_PROGRESS 10 +#define SZ_ERROR_FAIL 11 +#define SZ_ERROR_THREAD 12 + +#define SZ_ERROR_ARCHIVE 16 +#define SZ_ERROR_NO_ARCHIVE 17 + +typedef int SRes; + +#ifdef _WIN32 +typedef DWORD WRes; +#else +typedef int WRes; +#endif + +#ifndef RINOK +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; } +#endif + +typedef unsigned char Byte; +typedef short Int16; +typedef unsigned short UInt16; + +#ifdef _LZMA_UINT32_IS_ULONG +typedef long Int32; +typedef unsigned long UInt32; +#else +typedef int Int32; +typedef unsigned int UInt32; +#endif + +#ifdef _SZ_NO_INT_64 + +/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers. + NOTES: Some code will work incorrectly in that case! */ + +typedef long Int64; +typedef unsigned long UInt64; + +#else + +#if defined(_MSC_VER) || defined(__BORLANDC__) +typedef __int64 Int64; +typedef unsigned __int64 UInt64; +#else +typedef long long int Int64; +typedef unsigned long long int UInt64; +#endif + +#endif + +#ifdef _LZMA_NO_SYSTEM_SIZE_T +typedef UInt32 SizeT; +#else +typedef size_t SizeT; +#endif + +typedef int Bool; +#define True 1 +#define False 0 + + +#ifdef _MSC_VER + +#if _MSC_VER >= 1300 +#define MY_NO_INLINE __declspec(noinline) +#else +#define MY_NO_INLINE +#endif + +#define MY_CDECL __cdecl +#define MY_STD_CALL __stdcall +#define MY_FAST_CALL MY_NO_INLINE __fastcall + +#else + +#define MY_CDECL +#define MY_STD_CALL +#define MY_FAST_CALL + +#endif + + +/* The following interfaces use first parameter as pointer to structure */ + +typedef struct +{ + SRes (*Read)(void *p, void *buf, size_t *size); + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. + (output(*size) < input(*size)) is allowed */ +} ISeqInStream; + +/* it can return SZ_ERROR_INPUT_EOF */ +SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size); +SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType); +SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf); + +typedef struct +{ + size_t (*Write)(void *p, const void *buf, size_t size); + /* Returns: result - the number of actually written bytes. + (result < size) means error */ +} ISeqOutStream; + +typedef enum +{ + SZ_SEEK_SET = 0, + SZ_SEEK_CUR = 1, + SZ_SEEK_END = 2 +} ESzSeek; + +typedef struct +{ + SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */ + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin); +} ISeekInStream; + +typedef struct +{ + SRes (*Look)(void *p, void **buf, size_t *size); + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. + (output(*size) > input(*size)) is not allowed + (output(*size) < input(*size)) is allowed */ + SRes (*Skip)(void *p, size_t offset); + /* offset must be <= output(*size) of Look */ + + SRes (*Read)(void *p, void *buf, size_t *size); + /* reads directly (without buffer). It's same as ISeqInStream::Read */ + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin); +} ILookInStream; + +SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size); +SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset); + +/* reads via ILookInStream::Read */ +SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType); +SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size); + +#define LookToRead_BUF_SIZE (1 << 14) + +typedef struct +{ + ILookInStream s; + ISeekInStream *realStream; + size_t pos; + size_t size; + Byte buf[LookToRead_BUF_SIZE]; +} CLookToRead; + +void LookToRead_CreateVTable(CLookToRead *p, int lookahead); +void LookToRead_Init(CLookToRead *p); + +typedef struct +{ + ISeqInStream s; + ILookInStream *realStream; +} CSecToLook; + +void SecToLook_CreateVTable(CSecToLook *p); + +typedef struct +{ + ISeqInStream s; + ILookInStream *realStream; +} CSecToRead; + +void SecToRead_CreateVTable(CSecToRead *p); + +typedef struct +{ + SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize); + /* Returns: result. (result != SZ_OK) means break. + Value (UInt64)(Int64)-1 for size means unknown value. */ +} ICompressProgress; + +typedef struct +{ + void *(*Alloc)(void *p, size_t size); + void (*Free)(void *p, void *address); /* address can be 0 */ +} ISzAlloc; + +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size) +#define IAlloc_Free(p, a) (p)->Free((p), a) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zCompressionMode.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zCompressionMode.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,3 @@ +// CompressionMethod.cpp + +#include "StdAfx.h" diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zCompressionMode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zCompressionMode.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,50 @@ +// 7zCompressionMode.h + +#ifndef __7Z_COMPRESSION_MODE_H +#define __7Z_COMPRESSION_MODE_H + +#include "../../../Common/MyString.h" + +#include "../../../Windows/PropVariant.h" + +#include "../../Common/MethodProps.h" + +namespace NArchive { +namespace N7z { + +struct CMethodFull: public CMethod +{ + UInt32 NumInStreams; + UInt32 NumOutStreams; + bool IsSimpleCoder() const { return (NumInStreams == 1) && (NumOutStreams == 1); } +}; + +struct CBind +{ + UInt32 InCoder; + UInt32 InStream; + UInt32 OutCoder; + UInt32 OutStream; +}; + +struct CCompressionMethodMode +{ + CObjectVector Methods; + CRecordVector Binds; + #ifdef COMPRESS_MT + UInt32 NumThreads; + #endif + bool PasswordIsDefined; + UString Password; + + bool IsEmpty() const { return (Methods.IsEmpty() && !PasswordIsDefined); } + CCompressionMethodMode(): PasswordIsDefined(false) + #ifdef COMPRESS_MT + , NumThreads(1) + #endif + {} +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zDecode.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zDecode.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,332 @@ +// 7zDecode.cpp + +#include "StdAfx.h" + +#include "../../Common/LimitedStreams.h" +#include "../../Common/LockedStream.h" +#include "../../Common/ProgressUtils.h" +#include "../../Common/StreamObjects.h" + +#include "7zDecode.h" + +namespace NArchive { +namespace N7z { + +static void ConvertFolderItemInfoToBindInfo(const CFolder &folder, + CBindInfoEx &bindInfo) +{ + bindInfo.Clear(); + int i; + for (i = 0; i < folder.BindPairs.Size(); i++) + { + NCoderMixer::CBindPair bindPair; + bindPair.InIndex = (UInt32)folder.BindPairs[i].InIndex; + bindPair.OutIndex = (UInt32)folder.BindPairs[i].OutIndex; + bindInfo.BindPairs.Add(bindPair); + } + UInt32 outStreamIndex = 0; + for (i = 0; i < folder.Coders.Size(); i++) + { + NCoderMixer::CCoderStreamsInfo coderStreamsInfo; + const CCoderInfo &coderInfo = folder.Coders[i]; + coderStreamsInfo.NumInStreams = (UInt32)coderInfo.NumInStreams; + coderStreamsInfo.NumOutStreams = (UInt32)coderInfo.NumOutStreams; + bindInfo.Coders.Add(coderStreamsInfo); + bindInfo.CoderMethodIDs.Add(coderInfo.MethodID); + for (UInt32 j = 0; j < coderStreamsInfo.NumOutStreams; j++, outStreamIndex++) + if (folder.FindBindPairForOutStream(outStreamIndex) < 0) + bindInfo.OutStreams.Add(outStreamIndex); + } + for (i = 0; i < folder.PackStreams.Size(); i++) + bindInfo.InStreams.Add((UInt32)folder.PackStreams[i]); +} + +static bool AreCodersEqual(const NCoderMixer::CCoderStreamsInfo &a1, + const NCoderMixer::CCoderStreamsInfo &a2) +{ + return (a1.NumInStreams == a2.NumInStreams) && + (a1.NumOutStreams == a2.NumOutStreams); +} + +static bool AreBindPairsEqual(const NCoderMixer::CBindPair &a1, const NCoderMixer::CBindPair &a2) +{ + return (a1.InIndex == a2.InIndex) && + (a1.OutIndex == a2.OutIndex); +} + +static bool AreBindInfoExEqual(const CBindInfoEx &a1, const CBindInfoEx &a2) +{ + if (a1.Coders.Size() != a2.Coders.Size()) + return false; + int i; + for (i = 0; i < a1.Coders.Size(); i++) + if (!AreCodersEqual(a1.Coders[i], a2.Coders[i])) + return false; + if (a1.BindPairs.Size() != a2.BindPairs.Size()) + return false; + for (i = 0; i < a1.BindPairs.Size(); i++) + if (!AreBindPairsEqual(a1.BindPairs[i], a2.BindPairs[i])) + return false; + for (i = 0; i < a1.CoderMethodIDs.Size(); i++) + if (a1.CoderMethodIDs[i] != a2.CoderMethodIDs[i]) + return false; + if (a1.InStreams.Size() != a2.InStreams.Size()) + return false; + if (a1.OutStreams.Size() != a2.OutStreams.Size()) + return false; + return true; +} + +CDecoder::CDecoder(bool multiThread) +{ + #ifndef _ST_MODE + multiThread = true; + #endif + _multiThread = multiThread; + _bindInfoExPrevIsDefined = false; +} + +HRESULT CDecoder::Decode( + DECL_EXTERNAL_CODECS_LOC_VARS + IInStream *inStream, + UInt64 startPos, + const UInt64 *packSizes, + const CFolder &folderInfo, + ISequentialOutStream *outStream, + ICompressProgressInfo *compressProgress + #ifndef _NO_CRYPTO + , ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + #ifdef COMPRESS_MT + , bool mtMode, UInt32 numThreads + #endif + ) +{ + if (!folderInfo.CheckStructure()) + return E_NOTIMPL; + #ifndef _NO_CRYPTO + passwordIsDefined = false; + #endif + CObjectVector< CMyComPtr > inStreams; + + CLockedInStream lockedInStream; + lockedInStream.Init(inStream); + + for (int j = 0; j < folderInfo.PackStreams.Size(); j++) + { + CLockedSequentialInStreamImp *lockedStreamImpSpec = new + CLockedSequentialInStreamImp; + CMyComPtr lockedStreamImp = lockedStreamImpSpec; + lockedStreamImpSpec->Init(&lockedInStream, startPos); + startPos += packSizes[j]; + + CLimitedSequentialInStream *streamSpec = new + CLimitedSequentialInStream; + CMyComPtr inStream = streamSpec; + streamSpec->SetStream(lockedStreamImp); + streamSpec->Init(packSizes[j]); + inStreams.Add(inStream); + } + + int numCoders = folderInfo.Coders.Size(); + + CBindInfoEx bindInfo; + ConvertFolderItemInfoToBindInfo(folderInfo, bindInfo); + bool createNewCoders; + if (!_bindInfoExPrevIsDefined) + createNewCoders = true; + else + createNewCoders = !AreBindInfoExEqual(bindInfo, _bindInfoExPrev); + if (createNewCoders) + { + int i; + _decoders.Clear(); + // _decoders2.Clear(); + + _mixerCoder.Release(); + + if (_multiThread) + { + _mixerCoderMTSpec = new NCoderMixer::CCoderMixer2MT; + _mixerCoder = _mixerCoderMTSpec; + _mixerCoderCommon = _mixerCoderMTSpec; + } + else + { + #ifdef _ST_MODE + _mixerCoderSTSpec = new NCoderMixer::CCoderMixer2ST; + _mixerCoder = _mixerCoderSTSpec; + _mixerCoderCommon = _mixerCoderSTSpec; + #endif + } + RINOK(_mixerCoderCommon->SetBindInfo(bindInfo)); + + for (i = 0; i < numCoders; i++) + { + const CCoderInfo &coderInfo = folderInfo.Coders[i]; + + + CMyComPtr decoder; + CMyComPtr decoder2; + RINOK(CreateCoder( + EXTERNAL_CODECS_LOC_VARS + coderInfo.MethodID, decoder, decoder2, false)); + CMyComPtr decoderUnknown; + if (coderInfo.IsSimpleCoder()) + { + if (decoder == 0) + return E_NOTIMPL; + + decoderUnknown = (IUnknown *)decoder; + + if (_multiThread) + _mixerCoderMTSpec->AddCoder(decoder); + #ifdef _ST_MODE + else + _mixerCoderSTSpec->AddCoder(decoder, false); + #endif + } + else + { + if (decoder2 == 0) + return E_NOTIMPL; + decoderUnknown = (IUnknown *)decoder2; + if (_multiThread) + _mixerCoderMTSpec->AddCoder2(decoder2); + #ifdef _ST_MODE + else + _mixerCoderSTSpec->AddCoder2(decoder2, false); + #endif + } + _decoders.Add(decoderUnknown); + #ifdef EXTERNAL_CODECS + CMyComPtr setCompressCodecsInfo; + decoderUnknown.QueryInterface(IID_ISetCompressCodecsInfo, (void **)&setCompressCodecsInfo); + if (setCompressCodecsInfo) + { + RINOK(setCompressCodecsInfo->SetCompressCodecsInfo(codecsInfo)); + } + #endif + } + _bindInfoExPrev = bindInfo; + _bindInfoExPrevIsDefined = true; + } + int i; + _mixerCoderCommon->ReInit(); + + UInt32 packStreamIndex = 0, unpackStreamIndex = 0; + UInt32 coderIndex = 0; + // UInt32 coder2Index = 0; + + for (i = 0; i < numCoders; i++) + { + const CCoderInfo &coderInfo = folderInfo.Coders[i]; + CMyComPtr &decoder = _decoders[coderIndex]; + + { + CMyComPtr setDecoderProperties; + decoder.QueryInterface(IID_ICompressSetDecoderProperties2, &setDecoderProperties); + if (setDecoderProperties) + { + const CByteBuffer &props = coderInfo.Props; + size_t size = props.GetCapacity(); + if (size > 0xFFFFFFFF) + return E_NOTIMPL; + if (size > 0) + { + RINOK(setDecoderProperties->SetDecoderProperties2((const Byte *)props, (UInt32)size)); + } + } + } + + #ifdef COMPRESS_MT + if (mtMode) + { + CMyComPtr setCoderMt; + decoder.QueryInterface(IID_ICompressSetCoderMt, &setCoderMt); + if (setCoderMt) + { + RINOK(setCoderMt->SetNumberOfThreads(numThreads)); + } + } + #endif + + #ifndef _NO_CRYPTO + { + CMyComPtr cryptoSetPassword; + decoder.QueryInterface(IID_ICryptoSetPassword, &cryptoSetPassword); + if (cryptoSetPassword) + { + if (getTextPassword == 0) + return E_FAIL; + CMyComBSTR passwordBSTR; + RINOK(getTextPassword->CryptoGetTextPassword(&passwordBSTR)); + CByteBuffer buffer; + passwordIsDefined = true; + const UString password(passwordBSTR); + const UInt32 sizeInBytes = password.Length() * 2; + buffer.SetCapacity(sizeInBytes); + for (int i = 0; i < password.Length(); i++) + { + wchar_t c = password[i]; + ((Byte *)buffer)[i * 2] = (Byte)c; + ((Byte *)buffer)[i * 2 + 1] = (Byte)(c >> 8); + } + RINOK(cryptoSetPassword->CryptoSetPassword((const Byte *)buffer, sizeInBytes)); + } + } + #endif + + coderIndex++; + + UInt32 numInStreams = (UInt32)coderInfo.NumInStreams; + UInt32 numOutStreams = (UInt32)coderInfo.NumOutStreams; + CRecordVector packSizesPointers; + CRecordVector unpackSizesPointers; + packSizesPointers.Reserve(numInStreams); + unpackSizesPointers.Reserve(numOutStreams); + UInt32 j; + for (j = 0; j < numOutStreams; j++, unpackStreamIndex++) + unpackSizesPointers.Add(&folderInfo.UnpackSizes[unpackStreamIndex]); + + for (j = 0; j < numInStreams; j++, packStreamIndex++) + { + int bindPairIndex = folderInfo.FindBindPairForInStream(packStreamIndex); + if (bindPairIndex >= 0) + packSizesPointers.Add( + &folderInfo.UnpackSizes[(UInt32)folderInfo.BindPairs[bindPairIndex].OutIndex]); + else + { + int index = folderInfo.FindPackStreamArrayIndex(packStreamIndex); + if (index < 0) + return E_FAIL; + packSizesPointers.Add(&packSizes[index]); + } + } + + _mixerCoderCommon->SetCoderInfo(i, + &packSizesPointers.Front(), + &unpackSizesPointers.Front()); + } + UInt32 mainCoder, temp; + bindInfo.FindOutStream(bindInfo.OutStreams[0], mainCoder, temp); + + if (_multiThread) + _mixerCoderMTSpec->SetProgressCoderIndex(mainCoder); + /* + else + _mixerCoderSTSpec->SetProgressCoderIndex(mainCoder);; + */ + + if (numCoders == 0) + return 0; + CRecordVector inStreamPointers; + inStreamPointers.Reserve(inStreams.Size()); + for (i = 0; i < inStreams.Size(); i++) + inStreamPointers.Add(inStreams[i]); + ISequentialOutStream *outStreamPointer = outStream; + return _mixerCoder->Code(&inStreamPointers.Front(), NULL, + inStreams.Size(), &outStreamPointer, NULL, 1, compressProgress); +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zDecode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zDecode.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,68 @@ +// 7zDecode.h + +#ifndef __7Z_DECODE_H +#define __7Z_DECODE_H + +#include "../../IStream.h" +#include "../../IPassword.h" + +#include "../Common/CoderMixer2.h" +#include "../Common/CoderMixer2MT.h" +#ifdef _ST_MODE +#include "../Common/CoderMixer2ST.h" +#endif + +#include "../../Common/CreateCoder.h" + +#include "7zItem.h" + +namespace NArchive { +namespace N7z { + +struct CBindInfoEx: public NCoderMixer::CBindInfo +{ + CRecordVector CoderMethodIDs; + void Clear() + { + CBindInfo::Clear(); + CoderMethodIDs.Clear(); + } +}; + +class CDecoder +{ + bool _bindInfoExPrevIsDefined; + CBindInfoEx _bindInfoExPrev; + + bool _multiThread; + #ifdef _ST_MODE + NCoderMixer::CCoderMixer2ST *_mixerCoderSTSpec; + #endif + NCoderMixer::CCoderMixer2MT *_mixerCoderMTSpec; + NCoderMixer::CCoderMixer2 *_mixerCoderCommon; + + CMyComPtr _mixerCoder; + CObjectVector > _decoders; + // CObjectVector > _decoders2; +public: + CDecoder(bool multiThread); + HRESULT Decode( + DECL_EXTERNAL_CODECS_LOC_VARS + IInStream *inStream, + UInt64 startPos, + const UInt64 *packSizes, + const CFolder &folder, + ISequentialOutStream *outStream, + ICompressProgressInfo *compressProgress + #ifndef _NO_CRYPTO + , ICryptoGetTextPassword *getTextPasswordSpec, bool &passwordIsDefined + #endif + #ifdef COMPRESS_MT + , bool mtMode, UInt32 numThreads + #endif + ); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zExtract.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zExtract.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,273 @@ +// 7zExtract.cpp + +#include "StdAfx.h" + +#include "7zHandler.h" +#include "7zFolderOutStream.h" +#include "7zDecode.h" +// #include "7z1Decode.h" + +#include "../../../Common/ComTry.h" +#include "../../Common/StreamObjects.h" +#include "../../Common/ProgressUtils.h" +#include "../../Common/LimitedStreams.h" + +namespace NArchive { +namespace N7z { + +struct CExtractFolderInfo +{ + #ifdef _7Z_VOL + int VolumeIndex; + #endif + CNum FileIndex; + CNum FolderIndex; + CBoolVector ExtractStatuses; + UInt64 UnpackSize; + CExtractFolderInfo( + #ifdef _7Z_VOL + int volumeIndex, + #endif + CNum fileIndex, CNum folderIndex): + #ifdef _7Z_VOL + VolumeIndex(volumeIndex), + #endif + FileIndex(fileIndex), + FolderIndex(folderIndex), + UnpackSize(0) + { + if (fileIndex != kNumNoIndex) + { + ExtractStatuses.Reserve(1); + ExtractStatuses.Add(true); + } + }; +}; + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 testModeSpec, IArchiveExtractCallback *extractCallbackSpec) +{ + COM_TRY_BEGIN + bool testMode = (testModeSpec != 0); + CMyComPtr extractCallback = extractCallbackSpec; + UInt64 importantTotalUnpacked = 0; + + bool allFilesMode = (numItems == UInt32(-1)); + if (allFilesMode) + numItems = + #ifdef _7Z_VOL + _refs.Size(); + #else + _db.Files.Size(); + #endif + + if(numItems == 0) + return S_OK; + + /* + if(_volumes.Size() != 1) + return E_FAIL; + const CVolume &volume = _volumes.Front(); + const CArchiveDatabaseEx &_db = volume.Database; + IInStream *_inStream = volume.Stream; + */ + + CObjectVector extractFolderInfoVector; + for(UInt32 ii = 0; ii < numItems; ii++) + { + // UInt32 fileIndex = allFilesMode ? indexIndex : indices[indexIndex]; + UInt32 ref2Index = allFilesMode ? ii : indices[ii]; + // const CRef2 &ref2 = _refs[ref2Index]; + + // for(UInt32 ri = 0; ri < ref2.Refs.Size(); ri++) + { + #ifdef _7Z_VOL + // const CRef &ref = ref2.Refs[ri]; + const CRef &ref = _refs[ref2Index]; + + int volumeIndex = ref.VolumeIndex; + const CVolume &volume = _volumes[volumeIndex]; + const CArchiveDatabaseEx &db = volume.Database; + UInt32 fileIndex = ref.ItemIndex; + #else + const CArchiveDatabaseEx &db = _db; + UInt32 fileIndex = ref2Index; + #endif + + CNum folderIndex = db.FileIndexToFolderIndexMap[fileIndex]; + if (folderIndex == kNumNoIndex) + { + extractFolderInfoVector.Add(CExtractFolderInfo( + #ifdef _7Z_VOL + volumeIndex, + #endif + fileIndex, kNumNoIndex)); + continue; + } + if (extractFolderInfoVector.IsEmpty() || + folderIndex != extractFolderInfoVector.Back().FolderIndex + #ifdef _7Z_VOL + || volumeIndex != extractFolderInfoVector.Back().VolumeIndex + #endif + ) + { + extractFolderInfoVector.Add(CExtractFolderInfo( + #ifdef _7Z_VOL + volumeIndex, + #endif + kNumNoIndex, folderIndex)); + const CFolder &folderInfo = db.Folders[folderIndex]; + UInt64 unpackSize = folderInfo.GetUnpackSize(); + importantTotalUnpacked += unpackSize; + extractFolderInfoVector.Back().UnpackSize = unpackSize; + } + + CExtractFolderInfo &efi = extractFolderInfoVector.Back(); + + // const CFolderInfo &folderInfo = m_dam_Folders[folderIndex]; + CNum startIndex = db.FolderStartFileIndex[folderIndex]; + for (CNum index = efi.ExtractStatuses.Size(); + index <= fileIndex - startIndex; index++) + { + // UInt64 unpackSize = _db.Files[startIndex + index].UnpackSize; + // Count partial_folder_size + // efi.UnpackSize += unpackSize; + // importantTotalUnpacked += unpackSize; + efi.ExtractStatuses.Add(index == fileIndex - startIndex); + } + } + } + + extractCallback->SetTotal(importantTotalUnpacked); + + CDecoder decoder( + #ifdef _ST_MODE + false + #else + true + #endif + ); + // CDecoder1 decoder; + + UInt64 currentTotalPacked = 0; + UInt64 currentTotalUnpacked = 0; + UInt64 totalFolderUnpacked; + UInt64 totalFolderPacked; + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, false); + + for(int i = 0; i < extractFolderInfoVector.Size(); i++, + currentTotalUnpacked += totalFolderUnpacked, + currentTotalPacked += totalFolderPacked) + { + lps->OutSize = currentTotalUnpacked; + lps->InSize = currentTotalPacked; + RINOK(lps->SetCur()); + + const CExtractFolderInfo &efi = extractFolderInfoVector[i]; + totalFolderUnpacked = efi.UnpackSize; + + totalFolderPacked = 0; + + CFolderOutStream *folderOutStream = new CFolderOutStream; + CMyComPtr outStream(folderOutStream); + + #ifdef _7Z_VOL + const CVolume &volume = _volumes[efi.VolumeIndex]; + const CArchiveDatabaseEx &db = volume.Database; + #else + const CArchiveDatabaseEx &db = _db; + #endif + + CNum startIndex; + if (efi.FileIndex != kNumNoIndex) + startIndex = efi.FileIndex; + else + startIndex = db.FolderStartFileIndex[efi.FolderIndex]; + + + HRESULT result = folderOutStream->Init(&db, + #ifdef _7Z_VOL + volume.StartRef2Index, + #else + 0, + #endif + startIndex, + &efi.ExtractStatuses, extractCallback, testMode, _crcSize != 0); + + RINOK(result); + + if (efi.FileIndex != kNumNoIndex) + continue; + + CNum folderIndex = efi.FolderIndex; + const CFolder &folderInfo = db.Folders[folderIndex]; + + totalFolderPacked = _db.GetFolderFullPackSize(folderIndex); + + CNum packStreamIndex = db.FolderStartPackStreamIndex[folderIndex]; + UInt64 folderStartPackPos = db.GetFolderStreamPos(folderIndex, 0); + + #ifndef _NO_CRYPTO + CMyComPtr getTextPassword; + if (extractCallback) + extractCallback.QueryInterface(IID_ICryptoGetTextPassword, &getTextPassword); + #endif + + try + { + #ifndef _NO_CRYPTO + bool passwordIsDefined; + #endif + + HRESULT result = decoder.Decode( + EXTERNAL_CODECS_VARS + #ifdef _7Z_VOL + volume.Stream, + #else + _inStream, + #endif + folderStartPackPos, + &db.PackSizes[packStreamIndex], + folderInfo, + outStream, + progress + #ifndef _NO_CRYPTO + , getTextPassword, passwordIsDefined + #endif + #ifdef COMPRESS_MT + , true, _numThreads + #endif + ); + + if (result == S_FALSE) + { + RINOK(folderOutStream->FlushCorrupted(NArchive::NExtract::NOperationResult::kDataError)); + continue; + } + if (result == E_NOTIMPL) + { + RINOK(folderOutStream->FlushCorrupted(NArchive::NExtract::NOperationResult::kUnSupportedMethod)); + continue; + } + if (result != S_OK) + return result; + if (folderOutStream->WasWritingFinished() != S_OK) + { + RINOK(folderOutStream->FlushCorrupted(NArchive::NExtract::NOperationResult::kDataError)); + continue; + } + } + catch(...) + { + RINOK(folderOutStream->FlushCorrupted(NArchive::NExtract::NOperationResult::kDataError)); + continue; + } + } + return S_OK; + COM_TRY_END +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderInStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderInStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,130 @@ +// 7zFolderInStream.cpp + +#include "StdAfx.h" + +#include "7zFolderInStream.h" + +namespace NArchive { +namespace N7z { + +CFolderInStream::CFolderInStream() +{ + _inStreamWithHashSpec = new CSequentialInStreamWithCRC; + _inStreamWithHash = _inStreamWithHashSpec; +} + +void CFolderInStream::Init(IArchiveUpdateCallback *updateCallback, + const UInt32 *fileIndices, UInt32 numFiles) +{ + _updateCallback = updateCallback; + _numFiles = numFiles; + _fileIndex = 0; + _fileIndices = fileIndices; + Processed.Clear(); + CRCs.Clear(); + Sizes.Clear(); + _fileIsOpen = false; + _currentSizeIsDefined = false; +} + +HRESULT CFolderInStream::OpenStream() +{ + _filePos = 0; + while (_fileIndex < _numFiles) + { + _currentSizeIsDefined = false; + CMyComPtr stream; + HRESULT result = _updateCallback->GetStream(_fileIndices[_fileIndex], &stream); + if (result != S_OK && result != S_FALSE) + return result; + _fileIndex++; + _inStreamWithHashSpec->SetStream(stream); + _inStreamWithHashSpec->Init(); + if (!stream) + { + RINOK(_updateCallback->SetOperationResult(NArchive::NUpdate::NOperationResult::kOK)); + Sizes.Add(0); + Processed.Add(result == S_OK); + AddDigest(); + continue; + } + CMyComPtr streamGetSize; + if (stream.QueryInterface(IID_IStreamGetSize, &streamGetSize) == S_OK) + { + if(streamGetSize) + { + _currentSizeIsDefined = true; + RINOK(streamGetSize->GetSize(&_currentSize)); + } + } + + _fileIsOpen = true; + return S_OK; + } + return S_OK; +} + +void CFolderInStream::AddDigest() +{ + CRCs.Add(_inStreamWithHashSpec->GetCRC()); +} + +HRESULT CFolderInStream::CloseStream() +{ + RINOK(_updateCallback->SetOperationResult(NArchive::NUpdate::NOperationResult::kOK)); + _inStreamWithHashSpec->ReleaseStream(); + _fileIsOpen = false; + Processed.Add(true); + Sizes.Add(_filePos); + AddDigest(); + return S_OK; +} + +STDMETHODIMP CFolderInStream::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize = 0; + while ((_fileIndex < _numFiles || _fileIsOpen) && size > 0) + { + if (_fileIsOpen) + { + UInt32 localProcessedSize; + RINOK(_inStreamWithHash->Read( + ((Byte *)data) + realProcessedSize, size, &localProcessedSize)); + if (localProcessedSize == 0) + { + RINOK(CloseStream()); + continue; + } + realProcessedSize += localProcessedSize; + _filePos += localProcessedSize; + size -= localProcessedSize; + break; + } + else + { + RINOK(OpenStream()); + } + } + if (processedSize != 0) + *processedSize = realProcessedSize; + return S_OK; +} + +STDMETHODIMP CFolderInStream::GetSubStreamSize(UInt64 subStream, UInt64 *value) +{ + *value = 0; + int subStreamIndex = (int)subStream; + if (subStreamIndex < 0 || subStream > Sizes.Size()) + return E_FAIL; + if (subStreamIndex < Sizes.Size()) + { + *value= Sizes[subStreamIndex]; + return S_OK; + } + if (!_currentSizeIsDefined) + return S_FALSE; + *value = _currentSize; + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderInStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderInStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,66 @@ +// 7z/FolderInStream.h + +#ifndef __7Z_FOLDERINSTREAM_H +#define __7Z_FOLDERINSTREAM_H + +#include "7zItem.h" +#include "7zHeader.h" + +#include "../IArchive.h" +#include "../Common/InStreamWithCRC.h" +#include "../../IStream.h" +#include "../../ICoder.h" + +namespace NArchive { +namespace N7z { + +class CFolderInStream: + public ISequentialInStream, + public ICompressGetSubStreamSize, + public CMyUnknownImp +{ +public: + + MY_UNKNOWN_IMP1(ICompressGetSubStreamSize) + + CFolderInStream(); + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + + STDMETHOD(GetSubStreamSize)(UInt64 subStream, UInt64 *value); +private: + CSequentialInStreamWithCRC *_inStreamWithHashSpec; + CMyComPtr _inStreamWithHash; + CMyComPtr _updateCallback; + + bool _currentSizeIsDefined; + UInt64 _currentSize; + + bool _fileIsOpen; + UInt64 _filePos; + + const UInt32 *_fileIndices; + UInt32 _numFiles; + UInt32 _fileIndex; + + HRESULT OpenStream(); + HRESULT CloseStream(); + void AddDigest(); +public: + void Init(IArchiveUpdateCallback *updateCallback, + const UInt32 *fileIndices, UInt32 numFiles); + CRecordVector Processed; + CRecordVector CRCs; + CRecordVector Sizes; + UInt64 GetFullSize() const + { + UInt64 size = 0; + for (int i = 0; i < Sizes.Size(); i++) + size += Sizes[i]; + return size; + } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderOutStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderOutStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,164 @@ +// 7zFolderOutStream.cpp + +#include "StdAfx.h" + +#include "7zFolderOutStream.h" + +namespace NArchive { +namespace N7z { + +CFolderOutStream::CFolderOutStream() +{ + _outStreamWithHashSpec = new COutStreamWithCRC; + _outStreamWithHash = _outStreamWithHashSpec; +} + +HRESULT CFolderOutStream::Init( + const CArchiveDatabaseEx *archiveDatabase, + UInt32 ref2Offset, + UInt32 startIndex, + const CBoolVector *extractStatuses, + IArchiveExtractCallback *extractCallback, + bool testMode, + bool checkCrc) +{ + _archiveDatabase = archiveDatabase; + _ref2Offset = ref2Offset; + _startIndex = startIndex; + + _extractStatuses = extractStatuses; + _extractCallback = extractCallback; + _testMode = testMode; + + _checkCrc = checkCrc; + + _currentIndex = 0; + _fileIsOpen = false; + return WriteEmptyFiles(); +} + +HRESULT CFolderOutStream::OpenFile() +{ + Int32 askMode; + if((*_extractStatuses)[_currentIndex]) + askMode = _testMode ? + NArchive::NExtract::NAskMode::kTest : + NArchive::NExtract::NAskMode::kExtract; + else + askMode = NArchive::NExtract::NAskMode::kSkip; + CMyComPtr realOutStream; + + UInt32 index = _startIndex + _currentIndex; + RINOK(_extractCallback->GetStream(_ref2Offset + index, &realOutStream, askMode)); + + _outStreamWithHashSpec->SetStream(realOutStream); + _outStreamWithHashSpec->Init(_checkCrc); + if (askMode == NArchive::NExtract::NAskMode::kExtract && + (!realOutStream)) + { + const CFileItem &fi = _archiveDatabase->Files[index]; + if (!_archiveDatabase->IsItemAnti(index) && !fi.IsDir) + askMode = NArchive::NExtract::NAskMode::kSkip; + } + return _extractCallback->PrepareOperation(askMode); +} + +HRESULT CFolderOutStream::WriteEmptyFiles() +{ + for(;_currentIndex < _extractStatuses->Size(); _currentIndex++) + { + UInt32 index = _startIndex + _currentIndex; + const CFileItem &fi = _archiveDatabase->Files[index]; + if (!_archiveDatabase->IsItemAnti(index) && !fi.IsDir && fi.Size != 0) + return S_OK; + RINOK(OpenFile()); + RINOK(_extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK)); + _outStreamWithHashSpec->ReleaseStream(); + } + return S_OK; +} + +STDMETHODIMP CFolderOutStream::Write(const void *data, + UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize = 0; + while(_currentIndex < _extractStatuses->Size()) + { + if (_fileIsOpen) + { + UInt32 index = _startIndex + _currentIndex; + const CFileItem &fi = _archiveDatabase->Files[index]; + UInt64 fileSize = fi.Size; + + UInt32 numBytesToWrite = (UInt32)MyMin(fileSize - _filePos, + UInt64(size - realProcessedSize)); + + UInt32 processedSizeLocal; + RINOK(_outStreamWithHash->Write((const Byte *)data + realProcessedSize, + numBytesToWrite, &processedSizeLocal)); + + _filePos += processedSizeLocal; + realProcessedSize += processedSizeLocal; + if (_filePos == fileSize) + { + bool digestsAreEqual; + if (fi.CrcDefined && _checkCrc) + digestsAreEqual = fi.Crc == _outStreamWithHashSpec->GetCRC(); + else + digestsAreEqual = true; + + RINOK(_extractCallback->SetOperationResult( + digestsAreEqual ? + NArchive::NExtract::NOperationResult::kOK : + NArchive::NExtract::NOperationResult::kCRCError)); + _outStreamWithHashSpec->ReleaseStream(); + _fileIsOpen = false; + _currentIndex++; + } + if (realProcessedSize == size) + { + if (processedSize != NULL) + *processedSize = realProcessedSize; + return WriteEmptyFiles(); + } + } + else + { + RINOK(OpenFile()); + _fileIsOpen = true; + _filePos = 0; + } + } + if (processedSize != NULL) + *processedSize = size; + return S_OK; +} + +HRESULT CFolderOutStream::FlushCorrupted(Int32 resultEOperationResult) +{ + while(_currentIndex < _extractStatuses->Size()) + { + if (_fileIsOpen) + { + RINOK(_extractCallback->SetOperationResult(resultEOperationResult)); + _outStreamWithHashSpec->ReleaseStream(); + _fileIsOpen = false; + _currentIndex++; + } + else + { + RINOK(OpenFile()); + _fileIsOpen = true; + } + } + return S_OK; +} + +HRESULT CFolderOutStream::WasWritingFinished() +{ + if (_currentIndex == _extractStatuses->Size()) + return S_OK; + return E_FAIL; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderOutStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderOutStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,60 @@ +// 7zFolderOutStream.h + +#ifndef __7Z_FOLDEROUTSTREAM_H +#define __7Z_FOLDEROUTSTREAM_H + +#include "7zIn.h" + +#include "../../IStream.h" +#include "../IArchive.h" +#include "../Common/OutStreamWithCRC.h" + +namespace NArchive { +namespace N7z { + +class CFolderOutStream: + public ISequentialOutStream, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP + + CFolderOutStream(); + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); +private: + + COutStreamWithCRC *_outStreamWithHashSpec; + CMyComPtr _outStreamWithHash; + const CArchiveDatabaseEx *_archiveDatabase; + const CBoolVector *_extractStatuses; + UInt32 _startIndex; + UInt32 _ref2Offset; + int _currentIndex; + // UInt64 _currentDataPos; + CMyComPtr _extractCallback; + bool _testMode; + + bool _fileIsOpen; + + bool _checkCrc; + UInt64 _filePos; + + HRESULT OpenFile(); + HRESULT WriteEmptyFiles(); +public: + HRESULT Init( + const CArchiveDatabaseEx *archiveDatabase, + UInt32 ref2Offset, + UInt32 startIndex, + const CBoolVector *extractStatuses, + IArchiveExtractCallback *extractCallback, + bool testMode, + bool checkCrc); + HRESULT FlushCorrupted(Int32 resultEOperationResult); + HRESULT WasWritingFinished(); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,503 @@ +// 7zHandler.cpp + +#include "StdAfx.h" + +extern "C" +{ + #include "../../../../C/CpuArch.h" +} + +#include "../../../Common/ComTry.h" +#include "../../../Common/IntToString.h" + +#ifdef COMPRESS_MT +#include "../../../Windows/System.h" +#endif + +#include "../Common/ItemNameUtils.h" + +#include "7zHandler.h" +#include "7zProperties.h" + +#ifdef __7Z_SET_PROPERTIES +#ifdef EXTRACT_ONLY +#include "../Common/ParseProperties.h" +#endif +#endif + +using namespace NWindows; + +extern UString ConvertMethodIdToString(UInt64 id); + +namespace NArchive { +namespace N7z { + +CHandler::CHandler() +{ + _crcSize = 4; + + #ifndef _NO_CRYPTO + _passwordIsDefined = false; + #endif + + #ifdef EXTRACT_ONLY + #ifdef COMPRESS_MT + _numThreads = NSystem::GetNumberOfProcessors(); + #endif + #else + Init(); + #endif +} + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = _db.Files.Size(); + return S_OK; +} + +#ifdef _SFX + +IMP_IInArchive_ArcProps_NO + +STDMETHODIMP CHandler::GetNumberOfProperties(UInt32 * /* numProperties */) +{ + return E_NOTIMPL; +} + +STDMETHODIMP CHandler::GetPropertyInfo(UInt32 /* index */, + BSTR * /* name */, PROPID * /* propID */, VARTYPE * /* varType */) +{ + return E_NOTIMPL; +} + + +#else + +STATPROPSTG kArcProps[] = +{ + { NULL, kpidMethod, VT_BSTR}, + { NULL, kpidSolid, VT_BOOL}, + { NULL, kpidNumBlocks, VT_UI4}, + { NULL, kpidPhySize, VT_UI8}, + { NULL, kpidHeadersSize, VT_UI8}, + { NULL, kpidOffset, VT_UI8} +}; + +STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value) +{ + COM_TRY_BEGIN + NCOM::CPropVariant prop; + switch(propID) + { + case kpidMethod: + { + UString resString; + CRecordVector ids; + int i; + for (i = 0; i < _db.Folders.Size(); i++) + { + const CFolder &f = _db.Folders[i]; + for (int j = f.Coders.Size() - 1; j >= 0; j--) + ids.AddToUniqueSorted(f.Coders[j].MethodID); + } + + for (i = 0; i < ids.Size(); i++) + { + UInt64 id = ids[i]; + UString methodName; + /* bool methodIsKnown = */ FindMethod(EXTERNAL_CODECS_VARS id, methodName); + if (methodName.IsEmpty()) + methodName = ConvertMethodIdToString(id); + if (!resString.IsEmpty()) + resString += L' '; + resString += methodName; + } + prop = resString; + break; + } + case kpidSolid: prop = _db.IsSolid(); break; + case kpidNumBlocks: prop = (UInt32)_db.Folders.Size(); break; + case kpidHeadersSize: prop = _db.HeadersSize; break; + case kpidPhySize: prop = _db.PhySize; break; + case kpidOffset: if (_db.ArchiveInfo.StartPosition != 0) prop = _db.ArchiveInfo.StartPosition; break; + } + prop.Detach(value); + return S_OK; + COM_TRY_END +} + +IMP_IInArchive_ArcProps + +#endif + +static void SetPropFromUInt64Def(CUInt64DefVector &v, int index, NCOM::CPropVariant &prop) +{ + UInt64 value; + if (v.GetItem(index, value)) + { + FILETIME ft; + ft.dwLowDateTime = (DWORD)value; + ft.dwHighDateTime = (DWORD)(value >> 32); + prop = ft; + } +} + +#ifndef _SFX + +static UString ConvertUInt32ToString(UInt32 value) +{ + wchar_t buffer[32]; + ConvertUInt64ToString(value, buffer); + return buffer; +} + +static UString GetStringForSizeValue(UInt32 value) +{ + for (int i = 31; i >= 0; i--) + if ((UInt32(1) << i) == value) + return ConvertUInt32ToString(i); + UString result; + if (value % (1 << 20) == 0) + { + result += ConvertUInt32ToString(value >> 20); + result += L"m"; + } + else if (value % (1 << 10) == 0) + { + result += ConvertUInt32ToString(value >> 10); + result += L"k"; + } + else + { + result += ConvertUInt32ToString(value); + result += L"b"; + } + return result; +} + +static const UInt64 k_Copy = 0x0; +static const UInt64 k_LZMA = 0x030101; +static const UInt64 k_PPMD = 0x030401; + +static wchar_t GetHex(Byte value) +{ + return (wchar_t)((value < 10) ? (L'0' + value) : (L'A' + (value - 10))); +} +static inline UString GetHex2(Byte value) +{ + UString result; + result += GetHex((Byte)(value >> 4)); + result += GetHex((Byte)(value & 0xF)); + return result; +} + +#endif + +static const UInt64 k_AES = 0x06F10701; + +bool CHandler::IsEncrypted(UInt32 index2) const +{ + CNum folderIndex = _db.FileIndexToFolderIndexMap[index2]; + if (folderIndex != kNumNoIndex) + { + const CFolder &folderInfo = _db.Folders[folderIndex]; + for (int i = folderInfo.Coders.Size() - 1; i >= 0; i--) + if (folderInfo.Coders[i].MethodID == k_AES) + return true; + } + return false; +} + +STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value) +{ + COM_TRY_BEGIN + NCOM::CPropVariant prop; + + /* + const CRef2 &ref2 = _refs[index]; + if (ref2.Refs.IsEmpty()) + return E_FAIL; + const CRef &ref = ref2.Refs.Front(); + */ + + const CFileItem &item = _db.Files[index]; + UInt32 index2 = index; + + switch(propID) + { + case kpidPath: + if (!item.Name.IsEmpty()) + prop = NItemName::GetOSName(item.Name); + break; + case kpidIsDir: prop = item.IsDir; break; + case kpidSize: + { + prop = item.Size; + // prop = ref2.Size; + break; + } + case kpidPackSize: + { + // prop = ref2.PackSize; + { + CNum folderIndex = _db.FileIndexToFolderIndexMap[index2]; + if (folderIndex != kNumNoIndex) + { + if (_db.FolderStartFileIndex[folderIndex] == (CNum)index2) + prop = _db.GetFolderFullPackSize(folderIndex); + /* + else + prop = (UInt64)0; + */ + } + else + prop = (UInt64)0; + } + break; + } + case kpidPosition: { UInt64 v; if (_db.StartPos.GetItem(index2, v)) prop = v; break; } + case kpidCTime: SetPropFromUInt64Def(_db.CTime, index2, prop); break; + case kpidATime: SetPropFromUInt64Def(_db.ATime, index2, prop); break; + case kpidMTime: SetPropFromUInt64Def(_db.MTime, index2, prop); break; + case kpidAttrib: if (item.AttribDefined) prop = item.Attrib; break; + case kpidCRC: if (item.CrcDefined) prop = item.Crc; break; + case kpidEncrypted: prop = IsEncrypted(index2); break; + case kpidIsAnti: prop = _db.IsItemAnti(index2); break; + #ifndef _SFX + case kpidMethod: + { + CNum folderIndex = _db.FileIndexToFolderIndexMap[index2]; + if (folderIndex != kNumNoIndex) + { + const CFolder &folderInfo = _db.Folders[folderIndex]; + UString methodsString; + for (int i = folderInfo.Coders.Size() - 1; i >= 0; i--) + { + const CCoderInfo &coderInfo = folderInfo.Coders[i]; + if (!methodsString.IsEmpty()) + methodsString += L' '; + + { + UString methodName; + bool methodIsKnown = FindMethod( + EXTERNAL_CODECS_VARS + coderInfo.MethodID, methodName); + + if (methodIsKnown) + { + methodsString += methodName; + if (coderInfo.MethodID == k_LZMA) + { + if (coderInfo.Props.GetCapacity() >= 5) + { + methodsString += L":"; + UInt32 dicSize = GetUi32((const Byte *)coderInfo.Props + 1); + methodsString += GetStringForSizeValue(dicSize); + } + } + else if (coderInfo.MethodID == k_PPMD) + { + if (coderInfo.Props.GetCapacity() >= 5) + { + Byte order = *(const Byte *)coderInfo.Props; + methodsString += L":o"; + methodsString += ConvertUInt32ToString(order); + methodsString += L":mem"; + UInt32 dicSize = GetUi32((const Byte *)coderInfo.Props + 1); + methodsString += GetStringForSizeValue(dicSize); + } + } + else if (coderInfo.MethodID == k_AES) + { + if (coderInfo.Props.GetCapacity() >= 1) + { + methodsString += L":"; + const Byte *data = (const Byte *)coderInfo.Props; + Byte firstByte = *data++; + UInt32 numCyclesPower = firstByte & 0x3F; + methodsString += ConvertUInt32ToString(numCyclesPower); + /* + if ((firstByte & 0xC0) != 0) + { + methodsString += L":"; + return S_OK; + UInt32 saltSize = (firstByte >> 7) & 1; + UInt32 ivSize = (firstByte >> 6) & 1; + if (coderInfo.Props.GetCapacity() >= 2) + { + Byte secondByte = *data++; + saltSize += (secondByte >> 4); + ivSize += (secondByte & 0x0F); + } + } + */ + } + } + else + { + if (coderInfo.Props.GetCapacity() > 0) + { + methodsString += L":["; + for (size_t bi = 0; bi < coderInfo.Props.GetCapacity(); bi++) + { + if (bi > 5 && bi + 1 < coderInfo.Props.GetCapacity()) + { + methodsString += L".."; + break; + } + else + methodsString += GetHex2(coderInfo.Props[bi]); + } + methodsString += L"]"; + } + } + } + else + { + methodsString += ConvertMethodIdToString(coderInfo.MethodID); + } + } + } + prop = methodsString; + } + } + break; + case kpidBlock: + { + CNum folderIndex = _db.FileIndexToFolderIndexMap[index2]; + if (folderIndex != kNumNoIndex) + prop = (UInt32)folderIndex; + } + break; + case kpidPackedSize0: + case kpidPackedSize1: + case kpidPackedSize2: + case kpidPackedSize3: + case kpidPackedSize4: + { + CNum folderIndex = _db.FileIndexToFolderIndexMap[index2]; + if (folderIndex != kNumNoIndex) + { + const CFolder &folderInfo = _db.Folders[folderIndex]; + if (_db.FolderStartFileIndex[folderIndex] == (CNum)index2 && + folderInfo.PackStreams.Size() > (int)(propID - kpidPackedSize0)) + { + prop = _db.GetFolderPackStreamSize(folderIndex, propID - kpidPackedSize0); + } + else + prop = (UInt64)0; + } + else + prop = (UInt64)0; + } + break; + #endif + } + prop.Detach(value); + return S_OK; + COM_TRY_END +} + +STDMETHODIMP CHandler::Open(IInStream *stream, + const UInt64 *maxCheckStartPosition, + IArchiveOpenCallback *openArchiveCallback) +{ + COM_TRY_BEGIN + Close(); + #ifndef _SFX + _fileInfoPopIDs.Clear(); + #endif + try + { + CMyComPtr openArchiveCallbackTemp = openArchiveCallback; + + #ifndef _NO_CRYPTO + CMyComPtr getTextPassword; + if (openArchiveCallback) + { + openArchiveCallbackTemp.QueryInterface( + IID_ICryptoGetTextPassword, &getTextPassword); + } + #endif + CInArchive archive; + RINOK(archive.Open(stream, maxCheckStartPosition)); + #ifndef _NO_CRYPTO + _passwordIsDefined = false; + UString password; + #endif + HRESULT result = archive.ReadDatabase( + EXTERNAL_CODECS_VARS + _db + #ifndef _NO_CRYPTO + , getTextPassword, _passwordIsDefined + #endif + ); + RINOK(result); + _db.Fill(); + _inStream = stream; + } + catch(...) + { + Close(); + return S_FALSE; + } + // _inStream = stream; + #ifndef _SFX + FillPopIDs(); + #endif + return S_OK; + COM_TRY_END +} + +STDMETHODIMP CHandler::Close() +{ + COM_TRY_BEGIN + _inStream.Release(); + _db.Clear(); + return S_OK; + COM_TRY_END +} + +#ifdef __7Z_SET_PROPERTIES +#ifdef EXTRACT_ONLY + +STDMETHODIMP CHandler::SetProperties(const wchar_t **names, const PROPVARIANT *values, Int32 numProperties) +{ + COM_TRY_BEGIN + #ifdef COMPRESS_MT + const UInt32 numProcessors = NSystem::GetNumberOfProcessors(); + _numThreads = numProcessors; + #endif + + for (int i = 0; i < numProperties; i++) + { + UString name = names[i]; + name.MakeUpper(); + if (name.IsEmpty()) + return E_INVALIDARG; + const PROPVARIANT &value = values[i]; + UInt32 number; + int index = ParseStringToUInt32(name, number); + if (index == 0) + { + if(name.Left(2).CompareNoCase(L"MT") == 0) + { + #ifdef COMPRESS_MT + RINOK(ParseMtProp(name.Mid(2), value, numProcessors, _numThreads)); + #endif + continue; + } + else + return E_INVALIDARG; + } + } + return S_OK; + COM_TRY_END +} + +#endif +#endif + +IMPL_ISetCompressCodecsInfo + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,121 @@ +// 7z/Handler.h + +#ifndef __7Z_HANDLER_H +#define __7Z_HANDLER_H + +#include "../../ICoder.h" +#include "../IArchive.h" +#include "7zIn.h" + +#include "7zCompressionMode.h" + +#include "../../Common/CreateCoder.h" + +#ifndef EXTRACT_ONLY +#include "../Common/HandlerOut.h" +#endif + +namespace NArchive { +namespace N7z { + +#ifndef __7Z_SET_PROPERTIES + +#ifdef EXTRACT_ONLY +#ifdef COMPRESS_MT +#define __7Z_SET_PROPERTIES +#endif +#else +#define __7Z_SET_PROPERTIES +#endif + +#endif + + +class CHandler: + #ifndef EXTRACT_ONLY + public NArchive::COutHandler, + #endif + public IInArchive, + #ifdef __7Z_SET_PROPERTIES + public ISetProperties, + #endif + #ifndef EXTRACT_ONLY + public IOutArchive, + #endif + PUBLIC_ISetCompressCodecsInfo + public CMyUnknownImp +{ +public: + MY_QUERYINTERFACE_BEGIN2(IInArchive) + #ifdef __7Z_SET_PROPERTIES + MY_QUERYINTERFACE_ENTRY(ISetProperties) + #endif + #ifndef EXTRACT_ONLY + MY_QUERYINTERFACE_ENTRY(IOutArchive) + #endif + QUERY_ENTRY_ISetCompressCodecsInfo + MY_QUERYINTERFACE_END + MY_ADDREF_RELEASE + + INTERFACE_IInArchive(;) + + #ifdef __7Z_SET_PROPERTIES + STDMETHOD(SetProperties)(const wchar_t **names, const PROPVARIANT *values, Int32 numProperties); + #endif + + #ifndef EXTRACT_ONLY + INTERFACE_IOutArchive(;) + #endif + + DECL_ISetCompressCodecsInfo + + CHandler(); + +private: + CMyComPtr _inStream; + NArchive::N7z::CArchiveDatabaseEx _db; + #ifndef _NO_CRYPTO + bool _passwordIsDefined; + #endif + + #ifdef EXTRACT_ONLY + + #ifdef COMPRESS_MT + UInt32 _numThreads; + #endif + + UInt32 _crcSize; + + #else + + CRecordVector _binds; + + HRESULT SetPassword(CCompressionMethodMode &methodMode, IArchiveUpdateCallback *updateCallback); + + HRESULT SetCompressionMethod(CCompressionMethodMode &method, + CObjectVector &methodsInfo + #ifdef COMPRESS_MT + , UInt32 numThreads + #endif + ); + + HRESULT SetCompressionMethod( + CCompressionMethodMode &method, + CCompressionMethodMode &headerMethod); + + #endif + + bool IsEncrypted(UInt32 index2) const; + #ifndef _SFX + + CRecordVector _fileInfoPopIDs; + void FillPopIDs(); + + #endif + + DECL_EXTERNAL_CODECS_VARS +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zHeader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zHeader.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,27 @@ +// 7z/Header.cpp + +#include "StdAfx.h" +#include "7zHeader.h" + +namespace NArchive { +namespace N7z { + +Byte kSignature[kSignatureSize] = {'7' + 1, 'z', 0xBC, 0xAF, 0x27, 0x1C}; +#ifdef _7Z_VOL +Byte kFinishSignature[kSignatureSize] = {'7' + 1, 'z', 0xBC, 0xAF, 0x27, 0x1C + 1}; +#endif + +class SignatureInitializer +{ +public: + SignatureInitializer() + { + kSignature[0]--; + #ifdef _7Z_VOL + kFinishSignature[0]--; + #endif + }; +} g_SignatureInitializer; + +}} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zHeader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zHeader.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,97 @@ +// 7z/7zHeader.h + +#ifndef __7Z_HEADER_H +#define __7Z_HEADER_H + +#include "../../../Common/Types.h" + +namespace NArchive { +namespace N7z { + +const int kSignatureSize = 6; +extern Byte kSignature[kSignatureSize]; + +// #define _7Z_VOL +// 7z-MultiVolume is not finished yet. +// It can work already, but I still do not like some +// things of that new multivolume format. +// So please keep it commented. + +#ifdef _7Z_VOL +extern Byte kFinishSignature[kSignatureSize]; +#endif + +struct CArchiveVersion +{ + Byte Major; + Byte Minor; +}; + +const Byte kMajorVersion = 0; + +struct CStartHeader +{ + UInt64 NextHeaderOffset; + UInt64 NextHeaderSize; + UInt32 NextHeaderCRC; +}; + +const UInt32 kStartHeaderSize = 20; + +#ifdef _7Z_VOL +struct CFinishHeader: public CStartHeader +{ + UInt64 ArchiveStartOffset; // data offset from end if that struct + UInt64 AdditionalStartBlockSize; // start signature & start header size +}; + +const UInt32 kFinishHeaderSize = kStartHeaderSize + 16; +#endif + +namespace NID +{ + enum EEnum + { + kEnd, + + kHeader, + + kArchiveProperties, + + kAdditionalStreamsInfo, + kMainStreamsInfo, + kFilesInfo, + + kPackInfo, + kUnpackInfo, + kSubStreamsInfo, + + kSize, + kCRC, + + kFolder, + + kCodersUnpackSize, + kNumUnpackStream, + + kEmptyStream, + kEmptyFile, + kAnti, + + kName, + kCTime, + kATime, + kMTime, + kWinAttributes, + kComment, + + kEncodedHeader, + + kStartPos, + kDummy + }; +} + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zIn.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1260 @@ +// 7zIn.cpp + +#include "StdAfx.h" + +extern "C" +{ + #include "../../../../C/7zCrc.h" + #include "../../../../C/CpuArch.h" +} + +#include "../../Common/StreamObjects.h" +#include "../../Common/StreamUtils.h" + +#include "7zDecode.h" +#include "7zIn.h" + +#define Get16(p) GetUi16(p) +#define Get32(p) GetUi32(p) +#define Get64(p) GetUi64(p) + +// define FORMAT_7Z_RECOVERY if you want to recover multivolume archives with empty StartHeader +#ifndef _SFX +#define FORMAT_7Z_RECOVERY +#endif + +namespace NArchive { +namespace N7z { + +static void BoolVector_Fill_False(CBoolVector &v, int size) +{ + v.Clear(); + v.Reserve(size); + for (int i = 0; i < size; i++) + v.Add(false); +} + +static bool BoolVector_GetAndSet(CBoolVector &v, UInt32 index) +{ + if (index >= (UInt32)v.Size()) + return true; + bool res = v[index]; + v[index] = true; + return res; +} + +bool CFolder::CheckStructure() const +{ + const int kNumCodersMax = sizeof(UInt32) * 8; // don't change it + const int kMaskSize = sizeof(UInt32) * 8; // it must be >= kNumCodersMax + const int kNumBindsMax = 32; + + if (Coders.Size() > kNumCodersMax || BindPairs.Size() > kNumBindsMax) + return false; + + { + CBoolVector v; + BoolVector_Fill_False(v, BindPairs.Size() + PackStreams.Size()); + + int i; + for (i = 0; i < BindPairs.Size(); i++) + if (BoolVector_GetAndSet(v, BindPairs[i].InIndex)) + return false; + for (i = 0; i < PackStreams.Size(); i++) + if (BoolVector_GetAndSet(v, PackStreams[i])) + return false; + + BoolVector_Fill_False(v, UnpackSizes.Size()); + for (i = 0; i < BindPairs.Size(); i++) + if (BoolVector_GetAndSet(v, BindPairs[i].OutIndex)) + return false; + } + + UInt32 mask[kMaskSize]; + int i; + for (i = 0; i < kMaskSize; i++) + mask[i] = 0; + + { + CIntVector inStreamToCoder, outStreamToCoder; + for (i = 0; i < Coders.Size(); i++) + { + CNum j; + const CCoderInfo &coder = Coders[i]; + for (j = 0; j < coder.NumInStreams; j++) + inStreamToCoder.Add(i); + for (j = 0; j < coder.NumOutStreams; j++) + outStreamToCoder.Add(i); + } + + for (i = 0; i < BindPairs.Size(); i++) + { + const CBindPair &bp = BindPairs[i]; + mask[inStreamToCoder[bp.InIndex]] |= (1 << outStreamToCoder[bp.OutIndex]); + } + } + + for (i = 0; i < kMaskSize; i++) + for (int j = 0; j < kMaskSize; j++) + if (((1 << j) & mask[i]) != 0) + mask[i] |= mask[j]; + + for (i = 0; i < kMaskSize; i++) + if (((1 << i) & mask[i]) != 0) + return false; + + return true; +} + +class CInArchiveException {}; + +static void ThrowException() { throw CInArchiveException(); } +static inline void ThrowEndOfData() { ThrowException(); } +static inline void ThrowUnsupported() { ThrowException(); } +static inline void ThrowIncorrect() { ThrowException(); } +static inline void ThrowUnsupportedVersion() { ThrowException(); } + +/* +class CInArchiveException +{ +public: + enum CCauseType + { + kUnsupportedVersion = 0, + kUnsupported, + kIncorrect, + kEndOfData, + } Cause; + CInArchiveException(CCauseType cause): Cause(cause) {}; +}; + +static void ThrowException(CInArchiveException::CCauseType c) { throw CInArchiveException(c); } +static void ThrowEndOfData() { ThrowException(CInArchiveException::kEndOfData); } +static void ThrowUnsupported() { ThrowException(CInArchiveException::kUnsupported); } +static void ThrowIncorrect() { ThrowException(CInArchiveException::kIncorrect); } +static void ThrowUnsupportedVersion() { ThrowException(CInArchiveException::kUnsupportedVersion); } +*/ + +class CStreamSwitch +{ + CInArchive *_archive; + bool _needRemove; +public: + CStreamSwitch(): _needRemove(false) {} + ~CStreamSwitch() { Remove(); } + void Remove(); + void Set(CInArchive *archive, const Byte *data, size_t size); + void Set(CInArchive *archive, const CByteBuffer &byteBuffer); + void Set(CInArchive *archive, const CObjectVector *dataVector); +}; + +void CStreamSwitch::Remove() +{ + if (_needRemove) + { + _archive->DeleteByteStream(); + _needRemove = false; + } +} + +void CStreamSwitch::Set(CInArchive *archive, const Byte *data, size_t size) +{ + Remove(); + _archive = archive; + _archive->AddByteStream(data, size); + _needRemove = true; +} + +void CStreamSwitch::Set(CInArchive *archive, const CByteBuffer &byteBuffer) +{ + Set(archive, byteBuffer, byteBuffer.GetCapacity()); +} + +void CStreamSwitch::Set(CInArchive *archive, const CObjectVector *dataVector) +{ + Remove(); + Byte external = archive->ReadByte(); + if (external != 0) + { + int dataIndex = (int)archive->ReadNum(); + if (dataIndex < 0 || dataIndex >= dataVector->Size()) + ThrowIncorrect(); + Set(archive, (*dataVector)[dataIndex]); + } +} + +Byte CInByte2::ReadByte() +{ + if (_pos >= _size) + ThrowEndOfData(); + return _buffer[_pos++]; +} + +void CInByte2::ReadBytes(Byte *data, size_t size) +{ + if (size > _size - _pos) + ThrowEndOfData(); + for (size_t i = 0; i < size; i++) + data[i] = _buffer[_pos++]; +} + +void CInByte2::SkeepData(UInt64 size) +{ + if (size > _size - _pos) + ThrowEndOfData(); + _pos += (size_t)size; +} + +void CInByte2::SkeepData() +{ + SkeepData(ReadNumber()); +} + +UInt64 CInByte2::ReadNumber() +{ + if (_pos >= _size) + ThrowEndOfData(); + Byte firstByte = _buffer[_pos++]; + Byte mask = 0x80; + UInt64 value = 0; + for (int i = 0; i < 8; i++) + { + if ((firstByte & mask) == 0) + { + UInt64 highPart = firstByte & (mask - 1); + value += (highPart << (i * 8)); + return value; + } + if (_pos >= _size) + ThrowEndOfData(); + value |= ((UInt64)_buffer[_pos++] << (8 * i)); + mask >>= 1; + } + return value; +} + +CNum CInByte2::ReadNum() +{ + UInt64 value = ReadNumber(); + if (value > kNumMax) + ThrowUnsupported(); + return (CNum)value; +} + +UInt32 CInByte2::ReadUInt32() +{ + if (_pos + 4 > _size) + ThrowEndOfData(); + UInt32 res = Get32(_buffer + _pos); + _pos += 4; + return res; +} + +UInt64 CInByte2::ReadUInt64() +{ + if (_pos + 8 > _size) + ThrowEndOfData(); + UInt64 res = Get64(_buffer + _pos); + _pos += 8; + return res; +} + +void CInByte2::ReadString(UString &s) +{ + const Byte *buf = _buffer + _pos; + size_t rem = (_size - _pos) / 2 * 2; + { + size_t i; + for (i = 0; i < rem; i += 2) + if (buf[i] == 0 && buf[i + 1] == 0) + break; + if (i == rem) + ThrowEndOfData(); + rem = i; + } + int len = (int)(rem / 2); + if (len < 0 || (size_t)len * 2 != rem) + ThrowUnsupported(); + wchar_t *p = s.GetBuffer(len); + int i; + for (i = 0; i < len; i++, buf += 2) + p[i] = (wchar_t)Get16(buf); + s.ReleaseBuffer(len); + _pos += rem + 2; +} + +static inline bool TestSignatureCandidate(const Byte *p) +{ + for (int i = 0; i < kSignatureSize; i++) + if (p[i] != kSignature[i]) + return false; + return (p[0x1A] == 0 && p[0x1B] == 0); +} + +HRESULT CInArchive::FindAndReadSignature(IInStream *stream, const UInt64 *searchHeaderSizeLimit) +{ + RINOK(ReadStream_FALSE(stream, _header, kHeaderSize)); + + if (TestSignatureCandidate(_header)) + return S_OK; + + CByteBuffer byteBuffer; + const UInt32 kBufferSize = (1 << 16); + byteBuffer.SetCapacity(kBufferSize); + Byte *buffer = byteBuffer; + UInt32 numPrevBytes = kHeaderSize - 1; + memcpy(buffer, _header + 1, numPrevBytes); + UInt64 curTestPos = _arhiveBeginStreamPosition + 1; + for (;;) + { + if (searchHeaderSizeLimit != NULL) + if (curTestPos - _arhiveBeginStreamPosition > *searchHeaderSizeLimit) + break; + do + { + UInt32 numReadBytes = kBufferSize - numPrevBytes; + UInt32 processedSize; + RINOK(stream->Read(buffer + numPrevBytes, numReadBytes, &processedSize)); + numPrevBytes += processedSize; + if (processedSize == 0) + return S_FALSE; + } + while (numPrevBytes < kHeaderSize); + UInt32 numTests = numPrevBytes - kHeaderSize + 1; + for (UInt32 pos = 0; pos < numTests; pos++) + { + for (; buffer[pos] != '7' && pos < numTests; pos++); + if (pos == numTests) + break; + if (TestSignatureCandidate(buffer + pos)) + { + memcpy(_header, buffer + pos, kHeaderSize); + curTestPos += pos; + _arhiveBeginStreamPosition = curTestPos; + return stream->Seek(curTestPos + kHeaderSize, STREAM_SEEK_SET, NULL); + } + } + curTestPos += numTests; + numPrevBytes -= numTests; + memmove(buffer, buffer + numTests, numPrevBytes); + } + return S_FALSE; +} + +// S_FALSE means that file is not archive +HRESULT CInArchive::Open(IInStream *stream, const UInt64 *searchHeaderSizeLimit) +{ + HeadersSize = 0; + Close(); + RINOK(stream->Seek(0, STREAM_SEEK_CUR, &_arhiveBeginStreamPosition)) + RINOK(FindAndReadSignature(stream, searchHeaderSizeLimit)); + _stream = stream; + return S_OK; +} + +void CInArchive::Close() +{ + _stream.Release(); +} + +void CInArchive::ReadArchiveProperties(CInArchiveInfo & /* archiveInfo */) +{ + for (;;) + { + if (ReadID() == NID::kEnd) + break; + SkeepData(); + } +} + +void CInArchive::GetNextFolderItem(CFolder &folder) +{ + CNum numCoders = ReadNum(); + + folder.Coders.Clear(); + folder.Coders.Reserve((int)numCoders); + CNum numInStreams = 0; + CNum numOutStreams = 0; + CNum i; + for (i = 0; i < numCoders; i++) + { + folder.Coders.Add(CCoderInfo()); + CCoderInfo &coder = folder.Coders.Back(); + + { + Byte mainByte = ReadByte(); + int idSize = (mainByte & 0xF); + Byte longID[15]; + ReadBytes(longID, idSize); + if (idSize > 8) + ThrowUnsupported(); + UInt64 id = 0; + for (int j = 0; j < idSize; j++) + id |= (UInt64)longID[idSize - 1 - j] << (8 * j); + coder.MethodID = id; + + if ((mainByte & 0x10) != 0) + { + coder.NumInStreams = ReadNum(); + coder.NumOutStreams = ReadNum(); + } + else + { + coder.NumInStreams = 1; + coder.NumOutStreams = 1; + } + if ((mainByte & 0x20) != 0) + { + CNum propsSize = ReadNum(); + coder.Props.SetCapacity((size_t)propsSize); + ReadBytes((Byte *)coder.Props, (size_t)propsSize); + } + if ((mainByte & 0x80) != 0) + ThrowUnsupported(); + } + numInStreams += coder.NumInStreams; + numOutStreams += coder.NumOutStreams; + } + + CNum numBindPairs = numOutStreams - 1; + folder.BindPairs.Clear(); + folder.BindPairs.Reserve(numBindPairs); + for (i = 0; i < numBindPairs; i++) + { + CBindPair bp; + bp.InIndex = ReadNum(); + bp.OutIndex = ReadNum(); + folder.BindPairs.Add(bp); + } + + if (numInStreams < numBindPairs) + ThrowUnsupported(); + CNum numPackStreams = numInStreams - numBindPairs; + folder.PackStreams.Reserve(numPackStreams); + if (numPackStreams == 1) + { + for (i = 0; i < numInStreams; i++) + if (folder.FindBindPairForInStream(i) < 0) + { + folder.PackStreams.Add(i); + break; + } + if (folder.PackStreams.Size() != 1) + ThrowUnsupported(); + } + else + for (i = 0; i < numPackStreams; i++) + folder.PackStreams.Add(ReadNum()); +} + +void CInArchive::WaitAttribute(UInt64 attribute) +{ + for (;;) + { + UInt64 type = ReadID(); + if (type == attribute) + return; + if (type == NID::kEnd) + ThrowIncorrect(); + SkeepData(); + } +} + +void CInArchive::ReadHashDigests(int numItems, + CBoolVector &digestsDefined, + CRecordVector &digests) +{ + ReadBoolVector2(numItems, digestsDefined); + digests.Clear(); + digests.Reserve(numItems); + for (int i = 0; i < numItems; i++) + { + UInt32 crc = 0; + if (digestsDefined[i]) + crc = ReadUInt32(); + digests.Add(crc); + } +} + +void CInArchive::ReadPackInfo( + UInt64 &dataOffset, + CRecordVector &packSizes, + CBoolVector &packCRCsDefined, + CRecordVector &packCRCs) +{ + dataOffset = ReadNumber(); + CNum numPackStreams = ReadNum(); + + WaitAttribute(NID::kSize); + packSizes.Clear(); + packSizes.Reserve(numPackStreams); + for (CNum i = 0; i < numPackStreams; i++) + packSizes.Add(ReadNumber()); + + UInt64 type; + for (;;) + { + type = ReadID(); + if (type == NID::kEnd) + break; + if (type == NID::kCRC) + { + ReadHashDigests(numPackStreams, packCRCsDefined, packCRCs); + continue; + } + SkeepData(); + } + if (packCRCsDefined.IsEmpty()) + { + BoolVector_Fill_False(packCRCsDefined, numPackStreams); + packCRCs.Reserve(numPackStreams); + packCRCs.Clear(); + for (CNum i = 0; i < numPackStreams; i++) + packCRCs.Add(0); + } +} + +void CInArchive::ReadUnpackInfo( + const CObjectVector *dataVector, + CObjectVector &folders) +{ + WaitAttribute(NID::kFolder); + CNum numFolders = ReadNum(); + + { + CStreamSwitch streamSwitch; + streamSwitch.Set(this, dataVector); + folders.Clear(); + folders.Reserve(numFolders); + for (CNum i = 0; i < numFolders; i++) + { + folders.Add(CFolder()); + GetNextFolderItem(folders.Back()); + } + } + + WaitAttribute(NID::kCodersUnpackSize); + + CNum i; + for (i = 0; i < numFolders; i++) + { + CFolder &folder = folders[i]; + CNum numOutStreams = folder.GetNumOutStreams(); + folder.UnpackSizes.Reserve(numOutStreams); + for (CNum j = 0; j < numOutStreams; j++) + folder.UnpackSizes.Add(ReadNumber()); + } + + for (;;) + { + UInt64 type = ReadID(); + if (type == NID::kEnd) + return; + if (type == NID::kCRC) + { + CBoolVector crcsDefined; + CRecordVector crcs; + ReadHashDigests(numFolders, crcsDefined, crcs); + for (i = 0; i < numFolders; i++) + { + CFolder &folder = folders[i]; + folder.UnpackCRCDefined = crcsDefined[i]; + folder.UnpackCRC = crcs[i]; + } + continue; + } + SkeepData(); + } +} + +void CInArchive::ReadSubStreamsInfo( + const CObjectVector &folders, + CRecordVector &numUnpackStreamsInFolders, + CRecordVector &unpackSizes, + CBoolVector &digestsDefined, + CRecordVector &digests) +{ + numUnpackStreamsInFolders.Clear(); + numUnpackStreamsInFolders.Reserve(folders.Size()); + UInt64 type; + for (;;) + { + type = ReadID(); + if (type == NID::kNumUnpackStream) + { + for (int i = 0; i < folders.Size(); i++) + numUnpackStreamsInFolders.Add(ReadNum()); + continue; + } + if (type == NID::kCRC || type == NID::kSize) + break; + if (type == NID::kEnd) + break; + SkeepData(); + } + + if (numUnpackStreamsInFolders.IsEmpty()) + for (int i = 0; i < folders.Size(); i++) + numUnpackStreamsInFolders.Add(1); + + int i; + for (i = 0; i < numUnpackStreamsInFolders.Size(); i++) + { + // v3.13 incorrectly worked with empty folders + // v4.07: we check that folder is empty + CNum numSubstreams = numUnpackStreamsInFolders[i]; + if (numSubstreams == 0) + continue; + UInt64 sum = 0; + for (CNum j = 1; j < numSubstreams; j++) + if (type == NID::kSize) + { + UInt64 size = ReadNumber(); + unpackSizes.Add(size); + sum += size; + } + unpackSizes.Add(folders[i].GetUnpackSize() - sum); + } + if (type == NID::kSize) + type = ReadID(); + + int numDigests = 0; + int numDigestsTotal = 0; + for (i = 0; i < folders.Size(); i++) + { + CNum numSubstreams = numUnpackStreamsInFolders[i]; + if (numSubstreams != 1 || !folders[i].UnpackCRCDefined) + numDigests += numSubstreams; + numDigestsTotal += numSubstreams; + } + + for (;;) + { + if (type == NID::kCRC) + { + CBoolVector digestsDefined2; + CRecordVector digests2; + ReadHashDigests(numDigests, digestsDefined2, digests2); + int digestIndex = 0; + for (i = 0; i < folders.Size(); i++) + { + CNum numSubstreams = numUnpackStreamsInFolders[i]; + const CFolder &folder = folders[i]; + if (numSubstreams == 1 && folder.UnpackCRCDefined) + { + digestsDefined.Add(true); + digests.Add(folder.UnpackCRC); + } + else + for (CNum j = 0; j < numSubstreams; j++, digestIndex++) + { + digestsDefined.Add(digestsDefined2[digestIndex]); + digests.Add(digests2[digestIndex]); + } + } + } + else if (type == NID::kEnd) + { + if (digestsDefined.IsEmpty()) + { + BoolVector_Fill_False(digestsDefined, numDigestsTotal); + digests.Clear(); + for (int i = 0; i < numDigestsTotal; i++) + digests.Add(0); + } + return; + } + else + SkeepData(); + type = ReadID(); + } +} + +void CInArchive::ReadStreamsInfo( + const CObjectVector *dataVector, + UInt64 &dataOffset, + CRecordVector &packSizes, + CBoolVector &packCRCsDefined, + CRecordVector &packCRCs, + CObjectVector &folders, + CRecordVector &numUnpackStreamsInFolders, + CRecordVector &unpackSizes, + CBoolVector &digestsDefined, + CRecordVector &digests) +{ + for (;;) + { + UInt64 type = ReadID(); + if (type > ((UInt32)1 << 30)) + ThrowIncorrect(); + switch((UInt32)type) + { + case NID::kEnd: + return; + case NID::kPackInfo: + { + ReadPackInfo(dataOffset, packSizes, packCRCsDefined, packCRCs); + break; + } + case NID::kUnpackInfo: + { + ReadUnpackInfo(dataVector, folders); + break; + } + case NID::kSubStreamsInfo: + { + ReadSubStreamsInfo(folders, numUnpackStreamsInFolders, + unpackSizes, digestsDefined, digests); + break; + } + default: + ThrowIncorrect(); + } + } +} + +void CInArchive::ReadBoolVector(int numItems, CBoolVector &v) +{ + v.Clear(); + v.Reserve(numItems); + Byte b = 0; + Byte mask = 0; + for (int i = 0; i < numItems; i++) + { + if (mask == 0) + { + b = ReadByte(); + mask = 0x80; + } + v.Add((b & mask) != 0); + mask >>= 1; + } +} + +void CInArchive::ReadBoolVector2(int numItems, CBoolVector &v) +{ + Byte allAreDefined = ReadByte(); + if (allAreDefined == 0) + { + ReadBoolVector(numItems, v); + return; + } + v.Clear(); + v.Reserve(numItems); + for (int i = 0; i < numItems; i++) + v.Add(true); +} + +void CInArchive::ReadUInt64DefVector(const CObjectVector &dataVector, + CUInt64DefVector &v, int numFiles) +{ + ReadBoolVector2(numFiles, v.Defined); + + CStreamSwitch streamSwitch; + streamSwitch.Set(this, &dataVector); + v.Values.Reserve(numFiles); + + for (int i = 0; i < numFiles; i++) + { + UInt64 t = 0; + if (v.Defined[i]) + t = ReadUInt64(); + v.Values.Add(t); + } +} + +HRESULT CInArchive::ReadAndDecodePackedStreams( + DECL_EXTERNAL_CODECS_LOC_VARS + UInt64 baseOffset, + UInt64 &dataOffset, CObjectVector &dataVector + #ifndef _NO_CRYPTO + , ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + ) +{ + CRecordVector packSizes; + CBoolVector packCRCsDefined; + CRecordVector packCRCs; + CObjectVector folders; + + CRecordVector numUnpackStreamsInFolders; + CRecordVector unpackSizes; + CBoolVector digestsDefined; + CRecordVector digests; + + ReadStreamsInfo(NULL, + dataOffset, + packSizes, + packCRCsDefined, + packCRCs, + folders, + numUnpackStreamsInFolders, + unpackSizes, + digestsDefined, + digests); + + // db.ArchiveInfo.DataStartPosition2 += db.ArchiveInfo.StartPositionAfterHeader; + + CNum packIndex = 0; + CDecoder decoder( + #ifdef _ST_MODE + false + #else + true + #endif + ); + UInt64 dataStartPos = baseOffset + dataOffset; + for (int i = 0; i < folders.Size(); i++) + { + const CFolder &folder = folders[i]; + dataVector.Add(CByteBuffer()); + CByteBuffer &data = dataVector.Back(); + UInt64 unpackSize64 = folder.GetUnpackSize(); + size_t unpackSize = (size_t)unpackSize64; + if (unpackSize != unpackSize64) + ThrowUnsupported(); + data.SetCapacity(unpackSize); + + CSequentialOutStreamImp2 *outStreamSpec = new CSequentialOutStreamImp2; + CMyComPtr outStream = outStreamSpec; + outStreamSpec->Init(data, unpackSize); + + HRESULT result = decoder.Decode( + EXTERNAL_CODECS_LOC_VARS + _stream, dataStartPos, + &packSizes[packIndex], folder, outStream, NULL + #ifndef _NO_CRYPTO + , getTextPassword, passwordIsDefined + #endif + #ifdef COMPRESS_MT + , false, 1 + #endif + ); + RINOK(result); + + if (folder.UnpackCRCDefined) + if (CrcCalc(data, unpackSize) != folder.UnpackCRC) + ThrowIncorrect(); + for (int j = 0; j < folder.PackStreams.Size(); j++) + { + UInt64 packSize = packSizes[packIndex++]; + dataStartPos += packSize; + HeadersSize += packSize; + } + } + return S_OK; +} + +HRESULT CInArchive::ReadHeader( + DECL_EXTERNAL_CODECS_LOC_VARS + CArchiveDatabaseEx &db + #ifndef _NO_CRYPTO + , ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + ) +{ + UInt64 type = ReadID(); + + if (type == NID::kArchiveProperties) + { + ReadArchiveProperties(db.ArchiveInfo); + type = ReadID(); + } + + CObjectVector dataVector; + + if (type == NID::kAdditionalStreamsInfo) + { + HRESULT result = ReadAndDecodePackedStreams( + EXTERNAL_CODECS_LOC_VARS + db.ArchiveInfo.StartPositionAfterHeader, + db.ArchiveInfo.DataStartPosition2, + dataVector + #ifndef _NO_CRYPTO + , getTextPassword, passwordIsDefined + #endif + ); + RINOK(result); + db.ArchiveInfo.DataStartPosition2 += db.ArchiveInfo.StartPositionAfterHeader; + type = ReadID(); + } + + CRecordVector unpackSizes; + CBoolVector digestsDefined; + CRecordVector digests; + + if (type == NID::kMainStreamsInfo) + { + ReadStreamsInfo(&dataVector, + db.ArchiveInfo.DataStartPosition, + db.PackSizes, + db.PackCRCsDefined, + db.PackCRCs, + db.Folders, + db.NumUnpackStreamsVector, + unpackSizes, + digestsDefined, + digests); + db.ArchiveInfo.DataStartPosition += db.ArchiveInfo.StartPositionAfterHeader; + type = ReadID(); + } + else + { + for (int i = 0; i < db.Folders.Size(); i++) + { + db.NumUnpackStreamsVector.Add(1); + CFolder &folder = db.Folders[i]; + unpackSizes.Add(folder.GetUnpackSize()); + digestsDefined.Add(folder.UnpackCRCDefined); + digests.Add(folder.UnpackCRC); + } + } + + db.Files.Clear(); + + if (type == NID::kEnd) + return S_OK; + if (type != NID::kFilesInfo) + ThrowIncorrect(); + + CNum numFiles = ReadNum(); + db.Files.Reserve(numFiles); + CNum i; + for (i = 0; i < numFiles; i++) + db.Files.Add(CFileItem()); + + db.ArchiveInfo.FileInfoPopIDs.Add(NID::kSize); + if (!db.PackSizes.IsEmpty()) + db.ArchiveInfo.FileInfoPopIDs.Add(NID::kPackInfo); + if (numFiles > 0 && !digests.IsEmpty()) + db.ArchiveInfo.FileInfoPopIDs.Add(NID::kCRC); + + CBoolVector emptyStreamVector; + BoolVector_Fill_False(emptyStreamVector, (int)numFiles); + CBoolVector emptyFileVector; + CBoolVector antiFileVector; + CNum numEmptyStreams = 0; + + for (;;) + { + UInt64 type = ReadID(); + if (type == NID::kEnd) + break; + UInt64 size = ReadNumber(); + size_t ppp = _inByteBack->_pos; + bool addPropIdToList = true; + bool isKnownType = true; + if (type > ((UInt32)1 << 30)) + isKnownType = false; + else switch((UInt32)type) + { + case NID::kName: + { + CStreamSwitch streamSwitch; + streamSwitch.Set(this, &dataVector); + for (int i = 0; i < db.Files.Size(); i++) + _inByteBack->ReadString(db.Files[i].Name); + break; + } + case NID::kWinAttributes: + { + CBoolVector boolVector; + ReadBoolVector2(db.Files.Size(), boolVector); + CStreamSwitch streamSwitch; + streamSwitch.Set(this, &dataVector); + for (i = 0; i < numFiles; i++) + { + CFileItem &file = db.Files[i]; + file.AttribDefined = boolVector[i]; + if (file.AttribDefined) + file.Attrib = ReadUInt32(); + } + break; + } + case NID::kEmptyStream: + { + ReadBoolVector(numFiles, emptyStreamVector); + for (i = 0; i < (CNum)emptyStreamVector.Size(); i++) + if (emptyStreamVector[i]) + numEmptyStreams++; + + BoolVector_Fill_False(emptyFileVector, numEmptyStreams); + BoolVector_Fill_False(antiFileVector, numEmptyStreams); + + break; + } + case NID::kEmptyFile: ReadBoolVector(numEmptyStreams, emptyFileVector); break; + case NID::kAnti: ReadBoolVector(numEmptyStreams, antiFileVector); break; + case NID::kStartPos: ReadUInt64DefVector(dataVector, db.StartPos, (int)numFiles); break; + case NID::kCTime: ReadUInt64DefVector(dataVector, db.CTime, (int)numFiles); break; + case NID::kATime: ReadUInt64DefVector(dataVector, db.ATime, (int)numFiles); break; + case NID::kMTime: ReadUInt64DefVector(dataVector, db.MTime, (int)numFiles); break; + case NID::kDummy: + { + for (UInt64 j = 0; j < size; j++) + if (ReadByte() != 0) + ThrowIncorrect(); + addPropIdToList = false; + break; + } + default: + addPropIdToList = isKnownType = false; + } + if (isKnownType) + { + if(addPropIdToList) + db.ArchiveInfo.FileInfoPopIDs.Add(type); + } + else + SkeepData(size); + bool checkRecordsSize = (db.ArchiveInfo.Version.Major > 0 || + db.ArchiveInfo.Version.Minor > 2); + if (checkRecordsSize && _inByteBack->_pos - ppp != size) + ThrowIncorrect(); + } + + CNum emptyFileIndex = 0; + CNum sizeIndex = 0; + + CNum numAntiItems = 0; + for (i = 0; i < numEmptyStreams; i++) + if (antiFileVector[i]) + numAntiItems++; + + for (i = 0; i < numFiles; i++) + { + CFileItem &file = db.Files[i]; + bool isAnti; + file.HasStream = !emptyStreamVector[i]; + if (file.HasStream) + { + file.IsDir = false; + isAnti = false; + file.Size = unpackSizes[sizeIndex]; + file.Crc = digests[sizeIndex]; + file.CrcDefined = digestsDefined[sizeIndex]; + sizeIndex++; + } + else + { + file.IsDir = !emptyFileVector[emptyFileIndex]; + isAnti = antiFileVector[emptyFileIndex]; + emptyFileIndex++; + file.Size = 0; + file.CrcDefined = false; + } + if (numAntiItems != 0) + db.IsAnti.Add(isAnti); + } + return S_OK; +} + + +void CArchiveDatabaseEx::FillFolderStartPackStream() +{ + FolderStartPackStreamIndex.Clear(); + FolderStartPackStreamIndex.Reserve(Folders.Size()); + CNum startPos = 0; + for (int i = 0; i < Folders.Size(); i++) + { + FolderStartPackStreamIndex.Add(startPos); + startPos += (CNum)Folders[i].PackStreams.Size(); + } +} + +void CArchiveDatabaseEx::FillStartPos() +{ + PackStreamStartPositions.Clear(); + PackStreamStartPositions.Reserve(PackSizes.Size()); + UInt64 startPos = 0; + for (int i = 0; i < PackSizes.Size(); i++) + { + PackStreamStartPositions.Add(startPos); + startPos += PackSizes[i]; + } +} + +void CArchiveDatabaseEx::FillFolderStartFileIndex() +{ + FolderStartFileIndex.Clear(); + FolderStartFileIndex.Reserve(Folders.Size()); + FileIndexToFolderIndexMap.Clear(); + FileIndexToFolderIndexMap.Reserve(Files.Size()); + + int folderIndex = 0; + CNum indexInFolder = 0; + for (int i = 0; i < Files.Size(); i++) + { + const CFileItem &file = Files[i]; + bool emptyStream = !file.HasStream; + if (emptyStream && indexInFolder == 0) + { + FileIndexToFolderIndexMap.Add(kNumNoIndex); + continue; + } + if (indexInFolder == 0) + { + // v3.13 incorrectly worked with empty folders + // v4.07: Loop for skipping empty folders + for (;;) + { + if (folderIndex >= Folders.Size()) + ThrowIncorrect(); + FolderStartFileIndex.Add(i); // check it + if (NumUnpackStreamsVector[folderIndex] != 0) + break; + folderIndex++; + } + } + FileIndexToFolderIndexMap.Add(folderIndex); + if (emptyStream) + continue; + indexInFolder++; + if (indexInFolder >= NumUnpackStreamsVector[folderIndex]) + { + folderIndex++; + indexInFolder = 0; + } + } +} + +HRESULT CInArchive::ReadDatabase2( + DECL_EXTERNAL_CODECS_LOC_VARS + CArchiveDatabaseEx &db + #ifndef _NO_CRYPTO + , ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + ) +{ + db.Clear(); + db.ArchiveInfo.StartPosition = _arhiveBeginStreamPosition; + + db.ArchiveInfo.Version.Major = _header[6]; + db.ArchiveInfo.Version.Minor = _header[7]; + + if (db.ArchiveInfo.Version.Major != kMajorVersion) + ThrowUnsupportedVersion(); + + UInt32 crcFromArchive = Get32(_header + 8); + UInt64 nextHeaderOffset = Get64(_header + 0xC); + UInt64 nextHeaderSize = Get64(_header + 0x14); + UInt32 nextHeaderCRC = Get32(_header + 0x1C); + UInt32 crc = CrcCalc(_header + 0xC, 20); + + #ifdef FORMAT_7Z_RECOVERY + if (crcFromArchive == 0 && nextHeaderOffset == 0 && nextHeaderSize == 0 && nextHeaderCRC == 0) + { + UInt64 cur, cur2; + RINOK(_stream->Seek(0, STREAM_SEEK_CUR, &cur)); + const int kCheckSize = 500; + Byte buf[kCheckSize]; + RINOK(_stream->Seek(0, STREAM_SEEK_END, &cur2)); + int checkSize = kCheckSize; + if (cur2 - cur < kCheckSize) + checkSize = (int)(cur2 - cur); + RINOK(_stream->Seek(-checkSize, STREAM_SEEK_END, &cur2)); + + RINOK(ReadStream_FALSE(_stream, buf, (size_t)checkSize)); + + int i; + for (i = (int)checkSize - 2; i >= 0; i--) + if (buf[i] == 0x17 && buf[i + 1] == 0x6 || buf[i] == 0x01 && buf[i + 1] == 0x04) + break; + if (i < 0) + return S_FALSE; + nextHeaderSize = checkSize - i; + nextHeaderOffset = cur2 - cur + i; + nextHeaderCRC = CrcCalc(buf + i, (size_t)nextHeaderSize); + RINOK(_stream->Seek(cur, STREAM_SEEK_SET, NULL)); + } + #endif + + #ifdef FORMAT_7Z_RECOVERY + crcFromArchive = crc; + #endif + + db.ArchiveInfo.StartPositionAfterHeader = _arhiveBeginStreamPosition + kHeaderSize; + + if (crc != crcFromArchive) + ThrowIncorrect(); + + if (nextHeaderSize == 0) + return S_OK; + + if (nextHeaderSize > (UInt64)0xFFFFFFFF) + return S_FALSE; + + RINOK(_stream->Seek(nextHeaderOffset, STREAM_SEEK_CUR, NULL)); + + CByteBuffer buffer2; + buffer2.SetCapacity((size_t)nextHeaderSize); + + RINOK(ReadStream_FALSE(_stream, buffer2, (size_t)nextHeaderSize)); + HeadersSize += kHeaderSize + nextHeaderSize; + db.PhySize = kHeaderSize + nextHeaderOffset + nextHeaderSize; + + if (CrcCalc(buffer2, (UInt32)nextHeaderSize) != nextHeaderCRC) + ThrowIncorrect(); + + CStreamSwitch streamSwitch; + streamSwitch.Set(this, buffer2); + + CObjectVector dataVector; + + UInt64 type = ReadID(); + if (type != NID::kHeader) + { + if (type != NID::kEncodedHeader) + ThrowIncorrect(); + HRESULT result = ReadAndDecodePackedStreams( + EXTERNAL_CODECS_LOC_VARS + db.ArchiveInfo.StartPositionAfterHeader, + db.ArchiveInfo.DataStartPosition2, + dataVector + #ifndef _NO_CRYPTO + , getTextPassword, passwordIsDefined + #endif + ); + RINOK(result); + if (dataVector.Size() == 0) + return S_OK; + if (dataVector.Size() > 1) + ThrowIncorrect(); + streamSwitch.Remove(); + streamSwitch.Set(this, dataVector.Front()); + if (ReadID() != NID::kHeader) + ThrowIncorrect(); + } + + db.HeadersSize = HeadersSize; + + return ReadHeader( + EXTERNAL_CODECS_LOC_VARS + db + #ifndef _NO_CRYPTO + , getTextPassword, passwordIsDefined + #endif + ); +} + +HRESULT CInArchive::ReadDatabase( + DECL_EXTERNAL_CODECS_LOC_VARS + CArchiveDatabaseEx &db + #ifndef _NO_CRYPTO + , ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + ) +{ + try + { + return ReadDatabase2( + EXTERNAL_CODECS_LOC_VARS db + #ifndef _NO_CRYPTO + , getTextPassword, passwordIsDefined + #endif + ); + } + catch(CInArchiveException &) { return S_FALSE; } +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zIn.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,245 @@ +// 7zIn.h + +#ifndef __7Z_IN_H +#define __7Z_IN_H + +#include "../../../Common/MyCom.h" + +#include "../../IPassword.h" +#include "../../IStream.h" + +#include "../../Common/CreateCoder.h" +#include "../../Common/InBuffer.h" + +#include "7zItem.h" + +namespace NArchive { +namespace N7z { + +struct CInArchiveInfo +{ + CArchiveVersion Version; + UInt64 StartPosition; + UInt64 StartPositionAfterHeader; + UInt64 DataStartPosition; + UInt64 DataStartPosition2; + CRecordVector FileInfoPopIDs; + void Clear() + { + FileInfoPopIDs.Clear(); + } +}; + +struct CArchiveDatabaseEx: public CArchiveDatabase +{ + CInArchiveInfo ArchiveInfo; + CRecordVector PackStreamStartPositions; + CRecordVector FolderStartPackStreamIndex; + CRecordVector FolderStartFileIndex; + CRecordVector FileIndexToFolderIndexMap; + + UInt64 HeadersSize; + UInt64 PhySize; + + void Clear() + { + CArchiveDatabase::Clear(); + ArchiveInfo.Clear(); + PackStreamStartPositions.Clear(); + FolderStartPackStreamIndex.Clear(); + FolderStartFileIndex.Clear(); + FileIndexToFolderIndexMap.Clear(); + + HeadersSize = 0; + PhySize = 0; + } + + void FillFolderStartPackStream(); + void FillStartPos(); + void FillFolderStartFileIndex(); + + void Fill() + { + FillFolderStartPackStream(); + FillStartPos(); + FillFolderStartFileIndex(); + } + + UInt64 GetFolderStreamPos(int folderIndex, int indexInFolder) const + { + return ArchiveInfo.DataStartPosition + + PackStreamStartPositions[FolderStartPackStreamIndex[folderIndex] + indexInFolder]; + } + + UInt64 GetFolderFullPackSize(int folderIndex) const + { + CNum packStreamIndex = FolderStartPackStreamIndex[folderIndex]; + const CFolder &folder = Folders[folderIndex]; + UInt64 size = 0; + for (int i = 0; i < folder.PackStreams.Size(); i++) + size += PackSizes[packStreamIndex + i]; + return size; + } + + UInt64 GetFolderPackStreamSize(int folderIndex, int streamIndex) const + { + return PackSizes[FolderStartPackStreamIndex[folderIndex] + streamIndex]; + } + + UInt64 GetFilePackSize(CNum fileIndex) const + { + CNum folderIndex = FileIndexToFolderIndexMap[fileIndex]; + if (folderIndex != kNumNoIndex) + if (FolderStartFileIndex[folderIndex] == fileIndex) + return GetFolderFullPackSize(folderIndex); + return 0; + } +}; + +class CInByte2 +{ + const Byte *_buffer; + size_t _size; +public: + size_t _pos; + void Init(const Byte *buffer, size_t size) + { + _buffer = buffer; + _size = size; + _pos = 0; + } + Byte ReadByte(); + void ReadBytes(Byte *data, size_t size); + void SkeepData(UInt64 size); + void SkeepData(); + UInt64 ReadNumber(); + CNum ReadNum(); + UInt32 ReadUInt32(); + UInt64 ReadUInt64(); + void ReadString(UString &s); +}; + +class CStreamSwitch; + +const UInt32 kHeaderSize = 32; + +class CInArchive +{ + friend class CStreamSwitch; + + CMyComPtr _stream; + + CObjectVector _inByteVector; + CInByte2 *_inByteBack; + + UInt64 _arhiveBeginStreamPosition; + + Byte _header[kHeaderSize]; + + UInt64 HeadersSize; + + void AddByteStream(const Byte *buffer, size_t size) + { + _inByteVector.Add(CInByte2()); + _inByteBack = &_inByteVector.Back(); + _inByteBack->Init(buffer, size); + } + + void DeleteByteStream() + { + _inByteVector.DeleteBack(); + if (!_inByteVector.IsEmpty()) + _inByteBack = &_inByteVector.Back(); + } + +private: + HRESULT FindAndReadSignature(IInStream *stream, const UInt64 *searchHeaderSizeLimit); + + void ReadBytes(Byte *data, size_t size) { _inByteBack->ReadBytes(data, size); } + Byte ReadByte() { return _inByteBack->ReadByte(); } + UInt64 ReadNumber() { return _inByteBack->ReadNumber(); } + CNum ReadNum() { return _inByteBack->ReadNum(); } + UInt64 ReadID() { return _inByteBack->ReadNumber(); } + UInt32 ReadUInt32() { return _inByteBack->ReadUInt32(); } + UInt64 ReadUInt64() { return _inByteBack->ReadUInt64(); } + void SkeepData(UInt64 size) { _inByteBack->SkeepData(size); } + void SkeepData() { _inByteBack->SkeepData(); } + void WaitAttribute(UInt64 attribute); + + void ReadArchiveProperties(CInArchiveInfo &archiveInfo); + void GetNextFolderItem(CFolder &itemInfo); + void ReadHashDigests(int numItems, + CBoolVector &digestsDefined, CRecordVector &digests); + + void ReadPackInfo( + UInt64 &dataOffset, + CRecordVector &packSizes, + CBoolVector &packCRCsDefined, + CRecordVector &packCRCs); + + void ReadUnpackInfo( + const CObjectVector *dataVector, + CObjectVector &folders); + + void ReadSubStreamsInfo( + const CObjectVector &folders, + CRecordVector &numUnpackStreamsInFolders, + CRecordVector &unpackSizes, + CBoolVector &digestsDefined, + CRecordVector &digests); + + void ReadStreamsInfo( + const CObjectVector *dataVector, + UInt64 &dataOffset, + CRecordVector &packSizes, + CBoolVector &packCRCsDefined, + CRecordVector &packCRCs, + CObjectVector &folders, + CRecordVector &numUnpackStreamsInFolders, + CRecordVector &unpackSizes, + CBoolVector &digestsDefined, + CRecordVector &digests); + + + void ReadBoolVector(int numItems, CBoolVector &v); + void ReadBoolVector2(int numItems, CBoolVector &v); + void ReadUInt64DefVector(const CObjectVector &dataVector, + CUInt64DefVector &v, int numFiles); + HRESULT ReadAndDecodePackedStreams( + DECL_EXTERNAL_CODECS_LOC_VARS + UInt64 baseOffset, UInt64 &dataOffset, + CObjectVector &dataVector + #ifndef _NO_CRYPTO + , ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + ); + HRESULT ReadHeader( + DECL_EXTERNAL_CODECS_LOC_VARS + CArchiveDatabaseEx &db + #ifndef _NO_CRYPTO + ,ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + ); + HRESULT ReadDatabase2( + DECL_EXTERNAL_CODECS_LOC_VARS + CArchiveDatabaseEx &db + #ifndef _NO_CRYPTO + ,ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + ); +public: + HRESULT Open(IInStream *stream, const UInt64 *searchHeaderSizeLimit); // S_FALSE means is not archive + void Close(); + + HRESULT ReadDatabase( + DECL_EXTERNAL_CODECS_LOC_VARS + CArchiveDatabaseEx &db + #ifndef _NO_CRYPTO + ,ICryptoGetTextPassword *getTextPassword, bool &passwordIsDefined + #endif + ); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zItem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zItem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,258 @@ +// 7zItem.h + +#ifndef __7Z_ITEM_H +#define __7Z_ITEM_H + +#include "../../../Common/Buffer.h" +#include "../../../Common/MyString.h" + +#include "../../Common/MethodId.h" + +#include "7zHeader.h" + +namespace NArchive { +namespace N7z { + +typedef UInt32 CNum; +const CNum kNumMax = 0x7FFFFFFF; +const CNum kNumNoIndex = 0xFFFFFFFF; + +struct CCoderInfo +{ + CMethodId MethodID; + CByteBuffer Props; + CNum NumInStreams; + CNum NumOutStreams; + bool IsSimpleCoder() const { return (NumInStreams == 1) && (NumOutStreams == 1); } +}; + +struct CBindPair +{ + CNum InIndex; + CNum OutIndex; +}; + +struct CFolder +{ + CObjectVector Coders; + CRecordVector BindPairs; + CRecordVector PackStreams; + CRecordVector UnpackSizes; + UInt32 UnpackCRC; + bool UnpackCRCDefined; + + CFolder(): UnpackCRCDefined(false) {} + + UInt64 GetUnpackSize() const // test it + { + if (UnpackSizes.IsEmpty()) + return 0; + for (int i = UnpackSizes.Size() - 1; i >= 0; i--) + if (FindBindPairForOutStream(i) < 0) + return UnpackSizes[i]; + throw 1; + } + + CNum GetNumOutStreams() const + { + CNum result = 0; + for (int i = 0; i < Coders.Size(); i++) + result += Coders[i].NumOutStreams; + return result; + } + + int FindBindPairForInStream(CNum inStreamIndex) const + { + for(int i = 0; i < BindPairs.Size(); i++) + if (BindPairs[i].InIndex == inStreamIndex) + return i; + return -1; + } + int FindBindPairForOutStream(CNum outStreamIndex) const + { + for(int i = 0; i < BindPairs.Size(); i++) + if (BindPairs[i].OutIndex == outStreamIndex) + return i; + return -1; + } + int FindPackStreamArrayIndex(CNum inStreamIndex) const + { + for(int i = 0; i < PackStreams.Size(); i++) + if (PackStreams[i] == inStreamIndex) + return i; + return -1; + } + + bool CheckStructure() const; +}; + +struct CUInt64DefVector +{ + CRecordVector Values; + CRecordVector Defined; + + void Clear() + { + Values.Clear(); + Defined.Clear(); + } + + void ReserveDown() + { + Values.ReserveDown(); + Values.ReserveDown(); + } + + bool GetItem(int index, UInt64 &value) const + { + if (index < Defined.Size() && Defined[index]) + { + value = Values[index]; + return true; + } + value = 0; + return false; + } + + void SetItem(int index, bool defined, UInt64 value) + { + while (index >= Defined.Size()) + Defined.Add(false); + Defined[index] = defined; + if (!defined) + return; + while (index >= Values.Size()) + Values.Add(0); + Values[index] = value; + } + + bool CheckSize(int size) const { return Defined.Size() == size || Defined.Size() == 0; } +}; + +struct CFileItem +{ + UInt64 Size; + UInt32 Attrib; + UInt32 Crc; + UString Name; + + bool HasStream; // Test it !!! it means that there is + // stream in some folder. It can be empty stream + bool IsDir; + bool CrcDefined; + bool AttribDefined; + + CFileItem(): + HasStream(true), + IsDir(false), + CrcDefined(false), + AttribDefined(false) + {} + void SetAttrib(UInt32 attrib) + { + AttribDefined = true; + Attrib = attrib; + } +}; + +struct CFileItem2 +{ + UInt64 CTime; + UInt64 ATime; + UInt64 MTime; + UInt64 StartPos; + bool CTimeDefined; + bool ATimeDefined; + bool MTimeDefined; + bool StartPosDefined; + bool IsAnti; +}; + +struct CArchiveDatabase +{ + CRecordVector PackSizes; + CRecordVector PackCRCsDefined; + CRecordVector PackCRCs; + CObjectVector Folders; + CRecordVector NumUnpackStreamsVector; + CObjectVector Files; + + CUInt64DefVector CTime; + CUInt64DefVector ATime; + CUInt64DefVector MTime; + CUInt64DefVector StartPos; + CRecordVector IsAnti; + + void Clear() + { + PackSizes.Clear(); + PackCRCsDefined.Clear(); + PackCRCs.Clear(); + Folders.Clear(); + NumUnpackStreamsVector.Clear(); + Files.Clear(); + CTime.Clear(); + ATime.Clear(); + MTime.Clear(); + StartPos.Clear(); + IsAnti.Clear(); + } + + void ReserveDown() + { + PackSizes.ReserveDown(); + PackCRCsDefined.ReserveDown(); + PackCRCs.ReserveDown(); + Folders.ReserveDown(); + NumUnpackStreamsVector.ReserveDown(); + Files.ReserveDown(); + CTime.ReserveDown(); + ATime.ReserveDown(); + MTime.ReserveDown(); + StartPos.ReserveDown(); + IsAnti.ReserveDown(); + } + + bool IsEmpty() const + { + return (PackSizes.IsEmpty() && + PackCRCsDefined.IsEmpty() && + PackCRCs.IsEmpty() && + Folders.IsEmpty() && + NumUnpackStreamsVector.IsEmpty() && + Files.IsEmpty()); + } + + bool CheckNumFiles() const + { + int size = Files.Size(); + return ( + CTime.CheckSize(size) && + ATime.CheckSize(size) && + MTime.CheckSize(size) && + StartPos.CheckSize(size) && + (size == IsAnti.Size() || IsAnti.Size() == 0)); + } + + bool IsSolid() const + { + for (int i = 0; i < NumUnpackStreamsVector.Size(); i++) + if (NumUnpackStreamsVector[i] > 1) + return true; + return false; + } + bool IsItemAnti(int index) const { return (index < IsAnti.Size() && IsAnti[index]); } + void SetItemAnti(int index, bool isAnti) + { + while (index >= IsAnti.Size()) + IsAnti.Add(false); + IsAnti[index] = isAnti; + } + + void GetFile(int index, CFileItem &file, CFileItem2 &file2) const; + void AddFile(const CFileItem &file, const CFileItem2 &file2); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zProperties.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zProperties.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,163 @@ +// 7zProperties.cpp + +#include "StdAfx.h" + +#include "7zProperties.h" +#include "7zHeader.h" +#include "7zHandler.h" + +// #define _MULTI_PACK + +namespace NArchive { +namespace N7z { + +struct CPropMap +{ + UInt64 FilePropID; + STATPROPSTG StatPROPSTG; +}; + +CPropMap kPropMap[] = +{ + { NID::kName, NULL, kpidPath, VT_BSTR}, + { NID::kSize, NULL, kpidSize, VT_UI8}, + { NID::kPackInfo, NULL, kpidPackSize, VT_UI8}, + + #ifdef _MULTI_PACK + { 100, L"Pack0", kpidPackedSize0, VT_UI8}, + { 101, L"Pack1", kpidPackedSize1, VT_UI8}, + { 102, L"Pack2", kpidPackedSize2, VT_UI8}, + { 103, L"Pack3", kpidPackedSize3, VT_UI8}, + { 104, L"Pack4", kpidPackedSize4, VT_UI8}, + #endif + + { NID::kCTime, NULL, kpidCTime, VT_FILETIME}, + { NID::kMTime, NULL, kpidMTime, VT_FILETIME}, + { NID::kATime, NULL, kpidATime, VT_FILETIME}, + { NID::kWinAttributes, NULL, kpidAttrib, VT_UI4}, + { NID::kStartPos, NULL, kpidPosition, VT_UI4}, + + { NID::kCRC, NULL, kpidCRC, VT_UI4}, + + { NID::kAnti, NULL, kpidIsAnti, VT_BOOL}, + + #ifndef _SFX + { 97, NULL, kpidEncrypted, VT_BOOL}, + { 98, NULL, kpidMethod, VT_BSTR}, + { 99, NULL, kpidBlock, VT_UI4} + #endif +}; + +static const int kPropMapSize = sizeof(kPropMap) / sizeof(kPropMap[0]); + +static int FindPropInMap(UInt64 filePropID) +{ + for (int i = 0; i < kPropMapSize; i++) + if (kPropMap[i].FilePropID == filePropID) + return i; + return -1; +} + +static void CopyOneItem(CRecordVector &src, + CRecordVector &dest, UInt32 item) +{ + for (int i = 0; i < src.Size(); i++) + if (src[i] == item) + { + dest.Add(item); + src.Delete(i); + return; + } +} + +static void RemoveOneItem(CRecordVector &src, UInt32 item) +{ + for (int i = 0; i < src.Size(); i++) + if (src[i] == item) + { + src.Delete(i); + return; + } +} + +static void InsertToHead(CRecordVector &dest, UInt32 item) +{ + for (int i = 0; i < dest.Size(); i++) + if (dest[i] == item) + { + dest.Delete(i); + break; + } + dest.Insert(0, item); +} + +void CHandler::FillPopIDs() +{ + _fileInfoPopIDs.Clear(); + + #ifdef _7Z_VOL + if(_volumes.Size() < 1) + return; + const CVolume &volume = _volumes.Front(); + const CArchiveDatabaseEx &_db = volume.Database; + #endif + + CRecordVector fileInfoPopIDs = _db.ArchiveInfo.FileInfoPopIDs; + + RemoveOneItem(fileInfoPopIDs, NID::kEmptyStream); + RemoveOneItem(fileInfoPopIDs, NID::kEmptyFile); + + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kName); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kAnti); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kSize); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kPackInfo); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kCTime); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kMTime); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kATime); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kWinAttributes); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kCRC); + CopyOneItem(fileInfoPopIDs, _fileInfoPopIDs, NID::kComment); + _fileInfoPopIDs += fileInfoPopIDs; + + #ifndef _SFX + _fileInfoPopIDs.Add(97); + _fileInfoPopIDs.Add(98); + _fileInfoPopIDs.Add(99); + #endif + #ifdef _MULTI_PACK + _fileInfoPopIDs.Add(100); + _fileInfoPopIDs.Add(101); + _fileInfoPopIDs.Add(102); + _fileInfoPopIDs.Add(103); + _fileInfoPopIDs.Add(104); + #endif + + #ifndef _SFX + InsertToHead(_fileInfoPopIDs, NID::kMTime); + InsertToHead(_fileInfoPopIDs, NID::kPackInfo); + InsertToHead(_fileInfoPopIDs, NID::kSize); + InsertToHead(_fileInfoPopIDs, NID::kName); + #endif +} + +STDMETHODIMP CHandler::GetNumberOfProperties(UInt32 *numProperties) +{ + *numProperties = _fileInfoPopIDs.Size(); + return S_OK; +} + +STDMETHODIMP CHandler::GetPropertyInfo(UInt32 index, BSTR *name, PROPID *propID, VARTYPE *varType) +{ + if ((int)index >= _fileInfoPopIDs.Size()) + return E_INVALIDARG; + int indexInMap = FindPropInMap(_fileInfoPopIDs[index]); + if (indexInMap == -1) + return E_INVALIDARG; + const STATPROPSTG &srcItem = kPropMap[indexInMap].StatPROPSTG; + *propID = srcItem.propid; + *varType = srcItem.vt; + *name = 0; + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zProperties.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zProperties.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,22 @@ +// 7zProperties.h + +#ifndef __7Z_PROPERTIES_H +#define __7Z_PROPERTIES_H + +#include "../../PropID.h" + +namespace NArchive { +namespace N7z { + +enum +{ + kpidPackedSize0 = kpidUserDefined, + kpidPackedSize1, + kpidPackedSize2, + kpidPackedSize3, + kpidPackedSize4 +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// 7zRegister.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "7zHandler.h" +static IInArchive *CreateArc() { return new NArchive::N7z::CHandler; } +#ifndef EXTRACT_ONLY +static IOutArchive *CreateArcOut() { return new NArchive::N7z::CHandler; } +#else +#define CreateArcOut 0 +#endif + +static CArcInfo g_ArcInfo = + { L"7z", L"7z", 0, 7, {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}, 6, false, CreateArc, CreateArcOut }; + +REGISTER_ARC(7z) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zSpecStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zSpecStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +// 7zSpecStream.cpp + +#include "StdAfx.h" + +#include "7zSpecStream.h" + +STDMETHODIMP CSequentialInStreamSizeCount2::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize; + HRESULT result = _stream->Read(data, size, &realProcessedSize); + _size += realProcessedSize; + if (processedSize != 0) + *processedSize = realProcessedSize; + return result; +} + +STDMETHODIMP CSequentialInStreamSizeCount2::GetSubStreamSize( + UInt64 subStream, UInt64 *value) +{ + if (_getSubStreamSize == NULL) + return E_NOTIMPL; + return _getSubStreamSize->GetSubStreamSize(subStream, value); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/7zSpecStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zSpecStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,35 @@ +// 7zSpecStream.h + +#ifndef __7Z_SPEC_STREAM_H +#define __7Z_SPEC_STREAM_H + +#include "../../IStream.h" +#include "../../ICoder.h" +#include "../../../Common/MyCom.h" + +class CSequentialInStreamSizeCount2: + public ISequentialInStream, + public ICompressGetSubStreamSize, + public CMyUnknownImp +{ + CMyComPtr _stream; + CMyComPtr _getSubStreamSize; + UInt64 _size; +public: + void Init(ISequentialInStream *stream) + { + _stream = stream; + _getSubStreamSize = 0; + _stream.QueryInterface(IID_ICompressGetSubStreamSize, &_getSubStreamSize); + _size = 0; + } + UInt64 GetSize() const { return _size; } + + MY_UNKNOWN_IMP1(ICompressGetSubStreamSize) + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + + STDMETHOD(GetSubStreamSize)(UInt64 subStream, UInt64 *value); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/7z/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,9 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" +#include "../../../Common/NewHandler.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/ArchiveExports.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/ArchiveExports.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,130 @@ +// ArchiveExports.cpp + +#include "StdAfx.h" + +#include "../../Common/ComTry.h" +#include "../../Common/Types.h" +#include "../../Windows/PropVariant.h" +#include "../Common/RegisterArc.h" + +#include "IArchive.h" +#include "../ICoder.h" +#include "../IPassword.h" + +static const unsigned int kNumArcsMax = 32; +static unsigned int g_NumArcs = 0; +static const CArcInfo *g_Arcs[kNumArcsMax]; +void RegisterArc(const CArcInfo *arcInfo) +{ + if (g_NumArcs < kNumArcsMax) + g_Arcs[g_NumArcs++] = arcInfo; +} + +DEFINE_GUID(CLSID_CArchiveHandler, +0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00); + +#define CLS_ARC_ID_ITEM(cls) ((cls).Data4[5]) + +static inline HRESULT SetPropString(const char *s, unsigned int size, PROPVARIANT *value) +{ + if ((value->bstrVal = ::SysAllocStringByteLen(s, size)) != 0) + value->vt = VT_BSTR; + return S_OK; +} + +static inline HRESULT SetPropGUID(const GUID &guid, PROPVARIANT *value) +{ + return SetPropString((const char *)&guid, sizeof(GUID), value); +} + +int FindFormatCalssId(const GUID *clsID) +{ + GUID cls = *clsID; + CLS_ARC_ID_ITEM(cls) = 0; + if (cls != CLSID_CArchiveHandler) + return -1; + Byte id = CLS_ARC_ID_ITEM(*clsID); + for (unsigned i = 0; i < g_NumArcs; i++) + if (g_Arcs[i]->ClassId == id) + return (int)i; + return -1; +} + +STDAPI CreateArchiver(const GUID *clsid, const GUID *iid, void **outObject) +{ + COM_TRY_BEGIN + { + int needIn = (*iid == IID_IInArchive); + int needOut = (*iid == IID_IOutArchive); + if (!needIn && !needOut) + return E_NOINTERFACE; + int formatIndex = FindFormatCalssId(clsid); + if (formatIndex < 0) + return CLASS_E_CLASSNOTAVAILABLE; + + const CArcInfo &arc = *g_Arcs[formatIndex]; + if (needIn) + { + *outObject = arc.CreateInArchive(); + ((IInArchive *)*outObject)->AddRef(); + } + else + { + if (!arc.CreateOutArchive) + return CLASS_E_CLASSNOTAVAILABLE; + *outObject = arc.CreateOutArchive(); + ((IOutArchive *)*outObject)->AddRef(); + } + } + COM_TRY_END + return S_OK; +} + +STDAPI GetHandlerProperty2(UInt32 formatIndex, PROPID propID, PROPVARIANT *value) +{ + if (formatIndex >= g_NumArcs) + return E_INVALIDARG; + const CArcInfo &arc = *g_Arcs[formatIndex]; + NWindows::NCOM::CPropVariant prop; + switch(propID) + { + case NArchive::kName: + prop = arc.Name; + break; + case NArchive::kClassID: + { + GUID clsId = CLSID_CArchiveHandler; + CLS_ARC_ID_ITEM(clsId) = arc.ClassId; + return SetPropGUID(clsId, value); + } + case NArchive::kExtension: + if (arc.Ext != 0) + prop = arc.Ext; + break; + case NArchive::kAddExtension: + if (arc.AddExt != 0) + prop = arc.AddExt; + break; + case NArchive::kUpdate: + prop = (bool)(arc.CreateOutArchive != 0); + break; + case NArchive::kKeepName: + prop = arc.KeepName; + break; + case NArchive::kStartSignature: + return SetPropString((const char *)arc.Signature, arc.SignatureSize, value); + } + prop.Detach(value); + return S_OK; +} + +STDAPI GetHandlerProperty(PROPID propID, PROPVARIANT *value) +{ + return GetHandlerProperty2(0, propID, value); +} + +STDAPI GetNumberOfFormats(UINT32 *numFormats) +{ + *numFormats = g_NumArcs; + return S_OK; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/BZip2/BZip2Handler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/BZip2/BZip2Handler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,216 @@ +// BZip2Handler.cpp + +#include "StdAfx.h" + +#include "Common/ComTry.h" + +#include "Windows/PropVariant.h" + +#include "../../Common/CreateCoder.h" +#include "../../Common/ProgressUtils.h" +#include "../../Common/StreamUtils.h" + +#include "../Common/DummyOutStream.h" + +#include "BZip2Handler.h" + +using namespace NWindows; + +namespace NArchive { +namespace NBZip2 { + +static const CMethodId kMethodId_BZip2 = 0x040202; + +STATPROPSTG kProps[] = +{ + { NULL, kpidPackSize, VT_UI8} +}; + +IMP_IInArchive_Props +IMP_IInArchive_ArcProps_NO + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = 1; + return S_OK; +} + +STDMETHODIMP CHandler::GetProperty(UInt32 /* index */, PROPID propID, PROPVARIANT *value) +{ + NWindows::NCOM::CPropVariant prop; + switch(propID) + { + case kpidPackSize: prop = _item.PackSize; break; + } + prop.Detach(value); + return S_OK; +} + +STDMETHODIMP CHandler::Open(IInStream *stream, + const UInt64 * /* maxCheckStartPosition */, + IArchiveOpenCallback * /* openArchiveCallback */) +{ + COM_TRY_BEGIN + try + { + RINOK(stream->Seek(0, STREAM_SEEK_CUR, &_streamStartPosition)); + const int kSignatureSize = 3; + Byte buffer[kSignatureSize]; + RINOK(ReadStream_FALSE(stream, buffer, kSignatureSize)); + if (buffer[0] != 'B' || buffer[1] != 'Z' || buffer[2] != 'h') + return S_FALSE; + + UInt64 endPosition; + RINOK(stream->Seek(0, STREAM_SEEK_END, &endPosition)); + _item.PackSize = endPosition - _streamStartPosition; + + _stream = stream; + } + catch(...) + { + return S_FALSE; + } + return S_OK; + COM_TRY_END +} + +STDMETHODIMP CHandler::Close() +{ + _stream.Release(); + return S_OK; +} + + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 testModeSpec, IArchiveExtractCallback *extractCallback) +{ + COM_TRY_BEGIN + bool allFilesMode = (numItems == UInt32(-1)); + if (!allFilesMode) + { + if (numItems == 0) + return S_OK; + if (numItems != 1) + return E_INVALIDARG; + if (indices[0] != 0) + return E_INVALIDARG; + } + + bool testMode = (testModeSpec != 0); + + extractCallback->SetTotal(_item.PackSize); + + UInt64 currentTotalPacked = 0; + + RINOK(extractCallback->SetCompleted(¤tTotalPacked)); + + CMyComPtr realOutStream; + Int32 askMode; + askMode = testMode ? NExtract::NAskMode::kTest : + NExtract::NAskMode::kExtract; + + RINOK(extractCallback->GetStream(0, &realOutStream, askMode)); + + if(!testMode && !realOutStream) + return S_OK; + + + extractCallback->PrepareOperation(askMode); + + CMyComPtr decoder; + HRESULT loadResult = CreateCoder( + EXTERNAL_CODECS_VARS + kMethodId_BZip2, decoder, false); + if (loadResult != S_OK || !decoder) + { + RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kUnSupportedMethod)); + return S_OK; + } + + #ifdef COMPRESS_MT + { + CMyComPtr setCoderMt; + decoder.QueryInterface(IID_ICompressSetCoderMt, &setCoderMt); + if (setCoderMt) + { + RINOK(setCoderMt->SetNumberOfThreads(_numThreads)); + } + } + #endif + + CDummyOutStream *outStreamSpec = new CDummyOutStream; + CMyComPtr outStream(outStreamSpec); + outStreamSpec->SetStream(realOutStream); + outStreamSpec->Init(); + + realOutStream.Release(); + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, true); + + RINOK(_stream->Seek(_streamStartPosition, STREAM_SEEK_SET, NULL)); + + HRESULT result = S_OK; + + bool firstItem = true; + for (;;) + { + lps->InSize = currentTotalPacked; + lps->OutSize = outStreamSpec->GetSize(); + + RINOK(lps->SetCur()); + + const int kSignatureSize = 3; + Byte buffer[kSignatureSize]; + size_t processedSize = kSignatureSize; + RINOK(ReadStream(_stream, buffer, &processedSize)); + if (processedSize != kSignatureSize) + { + if (firstItem) + return E_FAIL; + break; + } + if (buffer[0] != 'B' || buffer[1] != 'Z' || buffer[2] != 'h') + { + if (firstItem) + return E_FAIL; + break; + } + firstItem = false; + + UInt64 dataStartPos; + RINOK(_stream->Seek((UInt64)(Int64)(-3), STREAM_SEEK_CUR, &dataStartPos)); + + result = decoder->Code(_stream, outStream, NULL, NULL, progress); + + if (result != S_OK) + break; + + CMyComPtr getInStreamProcessedSize; + decoder.QueryInterface(IID_ICompressGetInStreamProcessedSize, &getInStreamProcessedSize); + if (!getInStreamProcessedSize) + break; + UInt64 packSize; + RINOK(getInStreamProcessedSize->GetInStreamProcessedSize(&packSize)); + UInt64 pos; + RINOK(_stream->Seek(dataStartPos + packSize, STREAM_SEEK_SET, &pos)); + currentTotalPacked = pos - _streamStartPosition; + } + outStream.Release(); + + Int32 retResult; + if (result == S_OK) + retResult = NExtract::NOperationResult::kOK; + else if (result == S_FALSE) + retResult = NExtract::NOperationResult::kDataError; + else + return result; + return extractCallback->SetOperationResult(retResult); + + COM_TRY_END +} + +IMPL_ISetCompressCodecsInfo + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/BZip2/BZip2Handler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/BZip2/BZip2Handler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,70 @@ +// BZip2/Handler.h + +#ifndef __BZIP2_HANDLER_H +#define __BZIP2_HANDLER_H + +#include "Common/MyCom.h" +#include "../IArchive.h" +#include "../../Common/CreateCoder.h" +#include "BZip2Item.h" + +#ifdef COMPRESS_MT +#include "../../../Windows/System.h" +#endif + +namespace NArchive { +namespace NBZip2 { + +class CHandler: + public IInArchive, + public IOutArchive, + public ISetProperties, + PUBLIC_ISetCompressCodecsInfo + public CMyUnknownImp +{ + CMyComPtr _stream; + NArchive::NBZip2::CItem _item; + UInt64 _streamStartPosition; + + UInt32 _level; + UInt32 _dicSize; + UInt32 _numPasses; + #ifdef COMPRESS_MT + UInt32 _numThreads; + #endif + + DECL_EXTERNAL_CODECS_VARS + + void InitMethodProperties() + { + _level = 5; + _dicSize = + _numPasses = 0xFFFFFFFF; + #ifdef COMPRESS_MT + _numThreads = NWindows::NSystem::GetNumberOfProcessors();; + #endif + } + +public: + MY_QUERYINTERFACE_BEGIN2(IInArchive) + MY_QUERYINTERFACE_ENTRY(IOutArchive) + MY_QUERYINTERFACE_ENTRY(ISetProperties) + QUERY_ENTRY_ISetCompressCodecsInfo + MY_QUERYINTERFACE_END + MY_ADDREF_RELEASE + + INTERFACE_IInArchive(;) +#ifndef EXTRACT_ONLY + INTERFACE_IOutArchive(;) + + STDMETHOD(SetProperties)(const wchar_t **names, const PROPVARIANT *values, Int32 numProperties); +#endif + + DECL_ISetCompressCodecsInfo + + CHandler() { InitMethodProperties(); } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/BZip2/BZip2Item.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/BZip2/BZip2Item.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// Archive/BZip2Item.h + +#ifndef __ARCHIVE_BZIP2_ITEM_H +#define __ARCHIVE_BZIP2_ITEM_H + +namespace NArchive { +namespace NBZip2 { + +struct CItem +{ + UInt64 PackSize; + UInt64 UnPackSize; +}; + +}} + +#endif + + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/BZip2/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/BZip2/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/BZip2/bz2Register.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/BZip2/bz2Register.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// BZip2Register.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "BZip2Handler.h" +static IInArchive *CreateArc() { return new NArchive::NBZip2::CHandler; } +#ifndef EXTRACT_ONLY +static IOutArchive *CreateArcOut() { return new NArchive::NBZip2::CHandler; } +#else +#define CreateArcOut 0 +#endif + +static CArcInfo g_ArcInfo = + { L"BZip2", L"bz2 bzip2 tbz2 tbz", L"* * .tar .tar", 2, { 'B', 'Z', 'h' }, 3, true, CreateArc, CreateArcOut }; + +REGISTER_ARC(BZip2) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/CoderMixer2.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/CoderMixer2.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,121 @@ +// CoderMixer2.cpp + +#include "StdAfx.h" + +#include "CoderMixer2.h" + +namespace NCoderMixer { + +CBindReverseConverter::CBindReverseConverter(const CBindInfo &srcBindInfo): + _srcBindInfo(srcBindInfo) +{ + srcBindInfo.GetNumStreams(NumSrcInStreams, _numSrcOutStreams); + + UInt32 j; + for (j = 0; j < NumSrcInStreams; j++) + { + _srcInToDestOutMap.Add(0); + DestOutToSrcInMap.Add(0); + } + for (j = 0; j < _numSrcOutStreams; j++) + { + _srcOutToDestInMap.Add(0); + _destInToSrcOutMap.Add(0); + } + + UInt32 destInOffset = 0; + UInt32 destOutOffset = 0; + UInt32 srcInOffset = NumSrcInStreams; + UInt32 srcOutOffset = _numSrcOutStreams; + + for (int i = srcBindInfo.Coders.Size() - 1; i >= 0; i--) + { + const CCoderStreamsInfo &srcCoderInfo = srcBindInfo.Coders[i]; + + srcInOffset -= srcCoderInfo.NumInStreams; + srcOutOffset -= srcCoderInfo.NumOutStreams; + + UInt32 j; + for (j = 0; j < srcCoderInfo.NumInStreams; j++, destOutOffset++) + { + UInt32 index = srcInOffset + j; + _srcInToDestOutMap[index] = destOutOffset; + DestOutToSrcInMap[destOutOffset] = index; + } + for (j = 0; j < srcCoderInfo.NumOutStreams; j++, destInOffset++) + { + UInt32 index = srcOutOffset + j; + _srcOutToDestInMap[index] = destInOffset; + _destInToSrcOutMap[destInOffset] = index; + } + } +} + +void CBindReverseConverter::CreateReverseBindInfo(CBindInfo &destBindInfo) +{ + destBindInfo.Coders.Clear(); + destBindInfo.BindPairs.Clear(); + destBindInfo.InStreams.Clear(); + destBindInfo.OutStreams.Clear(); + + int i; + for (i = _srcBindInfo.Coders.Size() - 1; i >= 0; i--) + { + const CCoderStreamsInfo &srcCoderInfo = _srcBindInfo.Coders[i]; + CCoderStreamsInfo destCoderInfo; + destCoderInfo.NumInStreams = srcCoderInfo.NumOutStreams; + destCoderInfo.NumOutStreams = srcCoderInfo.NumInStreams; + destBindInfo.Coders.Add(destCoderInfo); + } + for (i = _srcBindInfo.BindPairs.Size() - 1; i >= 0; i--) + { + const CBindPair &srcBindPair = _srcBindInfo.BindPairs[i]; + CBindPair destBindPair; + destBindPair.InIndex = _srcOutToDestInMap[srcBindPair.OutIndex]; + destBindPair.OutIndex = _srcInToDestOutMap[srcBindPair.InIndex]; + destBindInfo.BindPairs.Add(destBindPair); + } + for (i = 0; i < _srcBindInfo.InStreams.Size(); i++) + destBindInfo.OutStreams.Add(_srcInToDestOutMap[_srcBindInfo.InStreams[i]]); + for (i = 0; i < _srcBindInfo.OutStreams.Size(); i++) + destBindInfo.InStreams.Add(_srcOutToDestInMap[_srcBindInfo.OutStreams[i]]); +} + +CCoderInfo2::CCoderInfo2(UInt32 numInStreams, UInt32 numOutStreams): + NumInStreams(numInStreams), + NumOutStreams(numOutStreams) +{ + InSizes.Reserve(NumInStreams); + InSizePointers.Reserve(NumInStreams); + OutSizePointers.Reserve(NumOutStreams); + OutSizePointers.Reserve(NumOutStreams); +} + +static void SetSizes(const UInt64 **srcSizes, CRecordVector &sizes, + CRecordVector &sizePointers, UInt32 numItems) +{ + sizes.Clear(); + sizePointers.Clear(); + for(UInt32 i = 0; i < numItems; i++) + { + if (srcSizes == 0 || srcSizes[i] == NULL) + { + sizes.Add(0); + sizePointers.Add(NULL); + } + else + { + sizes.Add(*srcSizes[i]); + sizePointers.Add(&sizes.Back()); + } + } +} + +void CCoderInfo2::SetCoderInfo(const UInt64 **inSizes, + const UInt64 **outSizes) +{ + SetSizes(inSizes, InSizes, InSizePointers, NumInStreams); + SetSizes(outSizes, OutSizes, OutSizePointers, NumOutStreams); +} + +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/CoderMixer2.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/CoderMixer2.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,174 @@ +// CoderMixer2.h + +#ifndef __CODER_MIXER2_H +#define __CODER_MIXER2_H + +#include "../../../Common/MyVector.h" +#include "../../../Common/Types.h" +#include "../../../Common/MyCom.h" +#include "../../ICoder.h" + +namespace NCoderMixer { + +struct CBindPair +{ + UInt32 InIndex; + UInt32 OutIndex; +}; + +struct CCoderStreamsInfo +{ + UInt32 NumInStreams; + UInt32 NumOutStreams; +}; + +struct CBindInfo +{ + CRecordVector Coders; + CRecordVector BindPairs; + CRecordVector InStreams; + CRecordVector OutStreams; + + void Clear() + { + Coders.Clear(); + BindPairs.Clear(); + InStreams.Clear(); + OutStreams.Clear(); + } + + /* + UInt32 GetCoderStartOutStream(UInt32 coderIndex) const + { + UInt32 numOutStreams = 0; + for (UInt32 i = 0; i < coderIndex; i++) + numOutStreams += Coders[i].NumOutStreams; + return numOutStreams; + } + */ + + + void GetNumStreams(UInt32 &numInStreams, UInt32 &numOutStreams) const + { + numInStreams = 0; + numOutStreams = 0; + for (int i = 0; i < Coders.Size(); i++) + { + const CCoderStreamsInfo &coderStreamsInfo = Coders[i]; + numInStreams += coderStreamsInfo.NumInStreams; + numOutStreams += coderStreamsInfo.NumOutStreams; + } + } + + int FindBinderForInStream(UInt32 inStream) const + { + for (int i = 0; i < BindPairs.Size(); i++) + if (BindPairs[i].InIndex == inStream) + return i; + return -1; + } + int FindBinderForOutStream(UInt32 outStream) const + { + for (int i = 0; i < BindPairs.Size(); i++) + if (BindPairs[i].OutIndex == outStream) + return i; + return -1; + } + + UInt32 GetCoderInStreamIndex(UInt32 coderIndex) const + { + UInt32 streamIndex = 0; + for (UInt32 i = 0; i < coderIndex; i++) + streamIndex += Coders[i].NumInStreams; + return streamIndex; + } + + UInt32 GetCoderOutStreamIndex(UInt32 coderIndex) const + { + UInt32 streamIndex = 0; + for (UInt32 i = 0; i < coderIndex; i++) + streamIndex += Coders[i].NumOutStreams; + return streamIndex; + } + + + void FindInStream(UInt32 streamIndex, UInt32 &coderIndex, + UInt32 &coderStreamIndex) const + { + for (coderIndex = 0; coderIndex < (UInt32)Coders.Size(); coderIndex++) + { + UInt32 curSize = Coders[coderIndex].NumInStreams; + if (streamIndex < curSize) + { + coderStreamIndex = streamIndex; + return; + } + streamIndex -= curSize; + } + throw 1; + } + void FindOutStream(UInt32 streamIndex, UInt32 &coderIndex, + UInt32 &coderStreamIndex) const + { + for (coderIndex = 0; coderIndex < (UInt32)Coders.Size(); coderIndex++) + { + UInt32 curSize = Coders[coderIndex].NumOutStreams; + if (streamIndex < curSize) + { + coderStreamIndex = streamIndex; + return; + } + streamIndex -= curSize; + } + throw 1; + } +}; + +class CBindReverseConverter +{ + UInt32 _numSrcOutStreams; + NCoderMixer::CBindInfo _srcBindInfo; + CRecordVector _srcInToDestOutMap; + CRecordVector _srcOutToDestInMap; + CRecordVector _destInToSrcOutMap; +public: + UInt32 NumSrcInStreams; + CRecordVector DestOutToSrcInMap; + + CBindReverseConverter(const NCoderMixer::CBindInfo &srcBindInfo); + void CreateReverseBindInfo(NCoderMixer::CBindInfo &destBindInfo); +}; + +struct CCoderInfo2 +{ + CMyComPtr Coder; + CMyComPtr Coder2; + UInt32 NumInStreams; + UInt32 NumOutStreams; + + CRecordVector InSizes; + CRecordVector OutSizes; + CRecordVector InSizePointers; + CRecordVector OutSizePointers; + + CCoderInfo2(UInt32 numInStreams, UInt32 numOutStreams); + void SetCoderInfo(const UInt64 **inSizes, const UInt64 **outSizes); + + HRESULT QueryInterface(REFGUID iid, void** pp) const + { + IUnknown *p = Coder ? (IUnknown *)Coder : (IUnknown *)Coder2; + return p->QueryInterface(iid, pp); + } +}; + +class CCoderMixer2 +{ +public: + virtual HRESULT SetBindInfo(const CBindInfo &bindInfo) = 0; + virtual void ReInit() = 0; + virtual void SetCoderInfo(UInt32 coderIndex, const UInt64 **inSizes, const UInt64 **outSizes) = 0; +}; + +} +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/CoderMixer2MT.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/CoderMixer2MT.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,230 @@ +// CoderMixer2MT.cpp + +#include "StdAfx.h" + +#include "CoderMixer2MT.h" + +namespace NCoderMixer { + +CCoder2::CCoder2(UInt32 numInStreams, UInt32 numOutStreams): + CCoderInfo2(numInStreams, numOutStreams) +{ + InStreams.Reserve(NumInStreams); + InStreamPointers.Reserve(NumInStreams); + OutStreams.Reserve(NumOutStreams); + OutStreamPointers.Reserve(NumOutStreams); +} + +void CCoder2::Execute() { Code(NULL); } + +void CCoder2::Code(ICompressProgressInfo *progress) +{ + InStreamPointers.Clear(); + OutStreamPointers.Clear(); + UInt32 i; + for (i = 0; i < NumInStreams; i++) + { + if (InSizePointers[i] != NULL) + InSizePointers[i] = &InSizes[i]; + InStreamPointers.Add((ISequentialInStream *)InStreams[i]); + } + for (i = 0; i < NumOutStreams; i++) + { + if (OutSizePointers[i] != NULL) + OutSizePointers[i] = &OutSizes[i]; + OutStreamPointers.Add((ISequentialOutStream *)OutStreams[i]); + } + if (Coder) + Result = Coder->Code(InStreamPointers[0], OutStreamPointers[0], + InSizePointers[0], OutSizePointers[0], progress); + else + Result = Coder2->Code(&InStreamPointers.Front(), &InSizePointers.Front(), NumInStreams, + &OutStreamPointers.Front(), &OutSizePointers.Front(), NumOutStreams, progress); + { + int i; + for (i = 0; i < InStreams.Size(); i++) + InStreams[i].Release(); + for (i = 0; i < OutStreams.Size(); i++) + OutStreams[i].Release(); + } +} + +static void SetSizes(const UInt64 **srcSizes, CRecordVector &sizes, + CRecordVector &sizePointers, UInt32 numItems) +{ + sizes.Clear(); + sizePointers.Clear(); + for(UInt32 i = 0; i < numItems; i++) + { + if (srcSizes == 0 || srcSizes[i] == NULL) + { + sizes.Add(0); + sizePointers.Add(NULL); + } + else + { + sizes.Add(*srcSizes[i]); + sizePointers.Add(&sizes.Back()); + } + } +} + + +void CCoder2::SetCoderInfo(const UInt64 **inSizes, const UInt64 **outSizes) +{ + SetSizes(inSizes, InSizes, InSizePointers, NumInStreams); + SetSizes(outSizes, OutSizes, OutSizePointers, NumOutStreams); +} + +////////////////////////////////////// +// CCoderMixer2MT + +HRESULT CCoderMixer2MT::SetBindInfo(const CBindInfo &bindInfo) +{ + _bindInfo = bindInfo; + _streamBinders.Clear(); + for(int i = 0; i < _bindInfo.BindPairs.Size(); i++) + { + _streamBinders.Add(CStreamBinder()); + RINOK(_streamBinders.Back().CreateEvents()); + } + return S_OK; +} + +void CCoderMixer2MT::AddCoderCommon() +{ + const CCoderStreamsInfo &c = _bindInfo.Coders[_coders.Size()]; + CCoder2 threadCoderInfo(c.NumInStreams, c.NumOutStreams); + _coders.Add(threadCoderInfo); +} + +void CCoderMixer2MT::AddCoder(ICompressCoder *coder) +{ + AddCoderCommon(); + _coders.Back().Coder = coder; +} + +void CCoderMixer2MT::AddCoder2(ICompressCoder2 *coder) +{ + AddCoderCommon(); + _coders.Back().Coder2 = coder; +} + + +void CCoderMixer2MT::ReInit() +{ + for(int i = 0; i < _streamBinders.Size(); i++) + _streamBinders[i].ReInit(); +} + + +HRESULT CCoderMixer2MT::Init(ISequentialInStream **inStreams, ISequentialOutStream **outStreams) +{ + /* + if (_coders.Size() != _bindInfo.Coders.Size()) + throw 0; + */ + int i; + for(i = 0; i < _coders.Size(); i++) + { + CCoder2 &coderInfo = _coders[i]; + const CCoderStreamsInfo &coderStreamsInfo = _bindInfo.Coders[i]; + coderInfo.InStreams.Clear(); + UInt32 j; + for(j = 0; j < coderStreamsInfo.NumInStreams; j++) + coderInfo.InStreams.Add(NULL); + coderInfo.OutStreams.Clear(); + for(j = 0; j < coderStreamsInfo.NumOutStreams; j++) + coderInfo.OutStreams.Add(NULL); + } + + for(i = 0; i < _bindInfo.BindPairs.Size(); i++) + { + const CBindPair &bindPair = _bindInfo.BindPairs[i]; + UInt32 inCoderIndex, inCoderStreamIndex; + UInt32 outCoderIndex, outCoderStreamIndex; + _bindInfo.FindInStream(bindPair.InIndex, inCoderIndex, inCoderStreamIndex); + _bindInfo.FindOutStream(bindPair.OutIndex, outCoderIndex, outCoderStreamIndex); + + _streamBinders[i].CreateStreams( + &_coders[inCoderIndex].InStreams[inCoderStreamIndex], + &_coders[outCoderIndex].OutStreams[outCoderStreamIndex]); + } + + for(i = 0; i < _bindInfo.InStreams.Size(); i++) + { + UInt32 inCoderIndex, inCoderStreamIndex; + _bindInfo.FindInStream(_bindInfo.InStreams[i], inCoderIndex, inCoderStreamIndex); + _coders[inCoderIndex].InStreams[inCoderStreamIndex] = inStreams[i]; + } + + for(i = 0; i < _bindInfo.OutStreams.Size(); i++) + { + UInt32 outCoderIndex, outCoderStreamIndex; + _bindInfo.FindOutStream(_bindInfo.OutStreams[i], outCoderIndex, outCoderStreamIndex); + _coders[outCoderIndex].OutStreams[outCoderStreamIndex] = outStreams[i]; + } + return S_OK; +} + +HRESULT CCoderMixer2MT::ReturnIfError(HRESULT code) +{ + for (int i = 0; i < _coders.Size(); i++) + if (_coders[i].Result == code) + return code; + return S_OK; +} + +STDMETHODIMP CCoderMixer2MT::Code(ISequentialInStream **inStreams, + const UInt64 ** /* inSizes */, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 ** /* outSizes */, + UInt32 numOutStreams, + ICompressProgressInfo *progress) +{ + if (numInStreams != (UInt32)_bindInfo.InStreams.Size() || + numOutStreams != (UInt32)_bindInfo.OutStreams.Size()) + return E_INVALIDARG; + + Init(inStreams, outStreams); + + int i; + for (i = 0; i < _coders.Size(); i++) + if (i != _progressCoderIndex) + { + RINOK(_coders[i].Create()); + } + + for (i = 0; i < _coders.Size(); i++) + if (i != _progressCoderIndex) + _coders[i].Start(); + + _coders[_progressCoderIndex].Code(progress); + + for (i = 0; i < _coders.Size(); i++) + if (i != _progressCoderIndex) + _coders[i].WaitFinish(); + + RINOK(ReturnIfError(E_ABORT)); + RINOK(ReturnIfError(E_OUTOFMEMORY)); + + for (i = 0; i < _coders.Size(); i++) + { + HRESULT result = _coders[i].Result; + if (result != S_OK && result != E_FAIL && result != S_FALSE) + return result; + } + + RINOK(ReturnIfError(S_FALSE)); + + for (i = 0; i < _coders.Size(); i++) + { + HRESULT result = _coders[i].Result; + if (result != S_OK) + return result; + } + return S_OK; +} + +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/CoderMixer2MT.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/CoderMixer2MT.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,80 @@ +// CoderMixer2MT.h + +#ifndef __CODER_MIXER2_MT_H +#define __CODER_MIXER2_MT_H + +#include "CoderMixer2.h" +#include "../../../Common/MyCom.h" +#include "../../Common/StreamBinder.h" +#include "../../Common/VirtThread.h" + +namespace NCoderMixer { + +struct CCoder2: public CCoderInfo2, public CVirtThread +{ + HRESULT Result; + CObjectVector< CMyComPtr > InStreams; + CObjectVector< CMyComPtr > OutStreams; + CRecordVector InStreamPointers; + CRecordVector OutStreamPointers; + + CCoder2(UInt32 numInStreams, UInt32 numOutStreams); + void SetCoderInfo(const UInt64 **inSizes, const UInt64 **outSizes); + virtual void Execute(); + void Code(ICompressProgressInfo *progress); +}; + + +/* + SetBindInfo() + for each coder + AddCoder[2]() + SetProgressIndex(UInt32 coderIndex); + + for each file + { + ReInit() + for each coder + SetCoderInfo + Code + } +*/ + +class CCoderMixer2MT: + public ICompressCoder2, + public CCoderMixer2, + public CMyUnknownImp +{ + CBindInfo _bindInfo; + CObjectVector _streamBinders; + int _progressCoderIndex; + + void AddCoderCommon(); + HRESULT Init(ISequentialInStream **inStreams, ISequentialOutStream **outStreams); + HRESULT ReturnIfError(HRESULT code); +public: + CObjectVector _coders; + MY_UNKNOWN_IMP + + STDMETHOD(Code)(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 **outSizes, + UInt32 numOutStreams, + ICompressProgressInfo *progress); + + HRESULT SetBindInfo(const CBindInfo &bindInfo); + void AddCoder(ICompressCoder *coder); + void AddCoder2(ICompressCoder2 *coder); + void SetProgressCoderIndex(int coderIndex) { _progressCoderIndex = coderIndex; } + + void ReInit(); + void SetCoderInfo(UInt32 coderIndex, const UInt64 **inSizes, const UInt64 **outSizes) + { _coders[coderIndex].SetCoderInfo(inSizes, outSizes); } + UInt64 GetWriteProcessedSize(UInt32 binderIndex) const + { return _streamBinders[binderIndex].ProcessedSize; } +}; + +} +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/DummyOutStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/DummyOutStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,22 @@ +// DummyOutStream.cpp + +#include "StdAfx.h" + +#include "DummyOutStream.h" + +STDMETHODIMP CDummyOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize; + HRESULT result; + if(!_stream) + { + realProcessedSize = size; + result = S_OK; + } + else + result = _stream->Write(data, size, &realProcessedSize); + _size += realProcessedSize; + if(processedSize != NULL) + *processedSize = realProcessedSize; + return result; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/DummyOutStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/DummyOutStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +// DummyOutStream.h + +#ifndef __DUMMYOUTSTREAM_H +#define __DUMMYOUTSTREAM_H + +#include "../../IStream.h" +#include "Common/MyCom.h" + +class CDummyOutStream: + public ISequentialOutStream, + public CMyUnknownImp +{ + CMyComPtr _stream; + UInt64 _size; +public: + void SetStream(ISequentialOutStream *outStream) { _stream = outStream; } + void ReleaseStream() { _stream.Release(); } + void Init() { _size = 0; } + MY_UNKNOWN_IMP + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + UInt64 GetSize() const { return _size; } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/FindSignature.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/FindSignature.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,62 @@ +// FindSignature.cpp + +#include "StdAfx.h" + +#include "Common/Buffer.h" + +#include "FindSignature.h" + +#include "../../Common/StreamUtils.h" + +HRESULT FindSignatureInStream(ISequentialInStream *stream, + const Byte *signature, unsigned signatureSize, + const UInt64 *limit, UInt64 &resPos) +{ + resPos = 0; + CByteBuffer byteBuffer2; + byteBuffer2.SetCapacity(signatureSize); + RINOK(ReadStream_FALSE(stream, byteBuffer2, signatureSize)); + + if (memcmp(byteBuffer2, signature, signatureSize) == 0) + return S_OK; + + const UInt32 kBufferSize = (1 << 16); + CByteBuffer byteBuffer; + byteBuffer.SetCapacity(kBufferSize); + Byte *buffer = byteBuffer; + UInt32 numPrevBytes = signatureSize - 1; + memcpy(buffer, (const Byte *)byteBuffer2 + 1, numPrevBytes); + resPos = 1; + for (;;) + { + if (limit != NULL) + if (resPos > *limit) + return S_FALSE; + do + { + UInt32 numReadBytes = kBufferSize - numPrevBytes; + UInt32 processedSize; + RINOK(stream->Read(buffer + numPrevBytes, numReadBytes, &processedSize)); + numPrevBytes += processedSize; + if (processedSize == 0) + return S_FALSE; + } + while (numPrevBytes < signatureSize); + UInt32 numTests = numPrevBytes - signatureSize + 1; + for (UInt32 pos = 0; pos < numTests; pos++) + { + Byte b = signature[0]; + for (; buffer[pos] != b && pos < numTests; pos++); + if (pos == numTests) + break; + if (memcmp(buffer + pos, signature, signatureSize) == 0) + { + resPos += pos; + return S_OK; + } + } + resPos += numTests; + numPrevBytes -= numTests; + memmove(buffer, buffer + numTests, numPrevBytes); + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/FindSignature.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/FindSignature.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,12 @@ +// FindSignature.h + +#ifndef __FINDSIGNATURE_H +#define __FINDSIGNATURE_H + +#include "../../IStream.h" + +HRESULT FindSignatureInStream(ISequentialInStream *stream, + const Byte *signature, unsigned signatureSize, + const UInt64 *limit, UInt64 &resPos); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/InStreamWithCRC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/InStreamWithCRC.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,40 @@ +// InStreamWithCRC.cpp + +#include "StdAfx.h" + +#include "InStreamWithCRC.h" + +STDMETHODIMP CSequentialInStreamWithCRC::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize; + HRESULT result = _stream->Read(data, size, &realProcessedSize); + _size += realProcessedSize; + if (size > 0 && realProcessedSize == 0) + _wasFinished = true; + _crc = CrcUpdate(_crc, data, realProcessedSize); + if(processedSize != NULL) + *processedSize = realProcessedSize; + return result; +} + +STDMETHODIMP CInStreamWithCRC::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize; + HRESULT result = _stream->Read(data, size, &realProcessedSize); + if (size > 0 && realProcessedSize == 0) + _wasFinished = true; + _size += realProcessedSize; + _crc = CrcUpdate(_crc, data, realProcessedSize); + if(processedSize != NULL) + *processedSize = realProcessedSize; + return result; +} + +STDMETHODIMP CInStreamWithCRC::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) +{ + if (seekOrigin != STREAM_SEEK_SET || offset != 0) + return E_FAIL; + _size = 0; + _crc = CRC_INIT_VAL; + return _stream->Seek(offset, seekOrigin, newPosition); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/InStreamWithCRC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/InStreamWithCRC.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,69 @@ +// InStreamWithCRC.h + +#ifndef __INSTREAMWITHCRC_H +#define __INSTREAMWITHCRC_H + +#include "../../../Common/MyCom.h" +#include "../../IStream.h" + +extern "C" +{ +#include "../../../../C/7zCrc.h" +} + +class CSequentialInStreamWithCRC: + public ISequentialInStream, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); +private: + CMyComPtr _stream; + UInt64 _size; + UInt32 _crc; + bool _wasFinished; +public: + void SetStream(ISequentialInStream *stream) { _stream = stream; } + void Init() + { + _size = 0; + _wasFinished = false; + _crc = CRC_INIT_VAL; + } + void ReleaseStream() { _stream.Release(); } + UInt32 GetCRC() const { return CRC_GET_DIGEST(_crc); } + UInt64 GetSize() const { return _size; } + bool WasFinished() const { return _wasFinished; } +}; + +class CInStreamWithCRC: + public IInStream, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP1(IInStream) + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition); +private: + CMyComPtr _stream; + UInt64 _size; + UInt32 _crc; + bool _wasFinished; +public: + void SetStream(IInStream *stream) { _stream = stream; } + void Init() + { + _size = 0; + _wasFinished = false; + _crc = CRC_INIT_VAL; + } + void ReleaseStream() { _stream.Release(); } + UInt32 GetCRC() const { return CRC_GET_DIGEST(_crc); } + UInt64 GetSize() const { return _size; } + bool WasFinished() const { return _wasFinished; } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/ItemNameUtils.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/ItemNameUtils.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,59 @@ +// Archive/Common/ItemNameUtils.cpp + +#include "StdAfx.h" + +#include "ItemNameUtils.h" + +namespace NArchive { +namespace NItemName { + +static const wchar_t kOSDirDelimiter = WCHAR_PATH_SEPARATOR; +static const wchar_t kDirDelimiter = L'/'; + +UString MakeLegalName(const UString &name) +{ + UString zipName = name; + zipName.Replace(kOSDirDelimiter, kDirDelimiter); + return zipName; +} + +UString GetOSName(const UString &name) +{ + UString newName = name; + newName.Replace(kDirDelimiter, kOSDirDelimiter); + return newName; +} + +UString GetOSName2(const UString &name) +{ + if (name.IsEmpty()) + return UString(); + UString newName = GetOSName(name); + if (newName[newName.Length() - 1] == kOSDirDelimiter) + newName.Delete(newName.Length() - 1); + return newName; +} + +bool HasTailSlash(const AString &name, UINT codePage) +{ + if (name.IsEmpty()) + return false; + LPCSTR prev = + #ifdef _WIN32 + CharPrevExA((WORD)codePage, name, &name[name.Length()], 0); + #else + (LPCSTR)(name) + (name.Length() - 1); + #endif + return (*prev == '/'); +} + +#ifndef _WIN32 +UString WinNameToOSName(const UString &name) +{ + UString newName = name; + newName.Replace(L'\\', kOSDirDelimiter); + return newName; +} +#endif + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/ItemNameUtils.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/ItemNameUtils.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +// Archive/Common/ItemNameUtils.h + +#ifndef __ARCHIVE_ITEMNAMEUTILS_H +#define __ARCHIVE_ITEMNAMEUTILS_H + +#include "../../../Common/MyString.h" + +namespace NArchive { +namespace NItemName { + + UString MakeLegalName(const UString &name); + UString GetOSName(const UString &name); + UString GetOSName2(const UString &name); + bool HasTailSlash(const AString &name, UINT codePage); + + #ifdef _WIN32 + inline UString WinNameToOSName(const UString &name) { return name; } + #else + UString WinNameToOSName(const UString &name); + #endif + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/MultiStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/MultiStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,201 @@ +// MultiStream.cpp + +#include "StdAfx.h" + +#include "MultiStream.h" + +STDMETHODIMP CMultiStream::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + if(processedSize != NULL) + *processedSize = 0; + while(_streamIndex < Streams.Size() && size > 0) + { + CSubStreamInfo &s = Streams[_streamIndex]; + if (_pos == s.Size) + { + _streamIndex++; + _pos = 0; + continue; + } + RINOK(s.Stream->Seek(s.Pos + _pos, STREAM_SEEK_SET, 0)); + UInt32 sizeToRead = UInt32(MyMin((UInt64)size, s.Size - _pos)); + UInt32 realProcessed; + HRESULT result = s.Stream->Read(data, sizeToRead, &realProcessed); + data = (void *)((Byte *)data + realProcessed); + size -= realProcessed; + if(processedSize != NULL) + *processedSize += realProcessed; + _pos += realProcessed; + _seekPos += realProcessed; + RINOK(result); + break; + } + return S_OK; +} + +STDMETHODIMP CMultiStream::Seek(Int64 offset, UInt32 seekOrigin, + UInt64 *newPosition) +{ + UInt64 newPos; + switch(seekOrigin) + { + case STREAM_SEEK_SET: + newPos = offset; + break; + case STREAM_SEEK_CUR: + newPos = _seekPos + offset; + break; + case STREAM_SEEK_END: + newPos = _totalLength + offset; + break; + default: + return STG_E_INVALIDFUNCTION; + } + _seekPos = 0; + for (_streamIndex = 0; _streamIndex < Streams.Size(); _streamIndex++) + { + UInt64 size = Streams[_streamIndex].Size; + if (newPos < _seekPos + size) + { + _pos = newPos - _seekPos; + _seekPos += _pos; + if (newPosition != 0) + *newPosition = newPos; + return S_OK; + } + _seekPos += size; + } + if (newPos == _seekPos) + { + if (newPosition != 0) + *newPosition = newPos; + return S_OK; + } + return E_FAIL; +} + + +/* +class COutVolumeStream: + public ISequentialOutStream, + public CMyUnknownImp +{ + int _volIndex; + UInt64 _volSize; + UInt64 _curPos; + CMyComPtr _volumeStream; + COutArchive _archive; + CCRC _crc; + +public: + MY_UNKNOWN_IMP + + CFileItem _file; + CUpdateOptions _options; + CMyComPtr VolumeCallback; + void Init(IArchiveUpdateCallback2 *volumeCallback, + const UString &name) + { + _file.Name = name; + _file.IsStartPosDefined = true; + _file.StartPos = 0; + + VolumeCallback = volumeCallback; + _volIndex = 0; + _volSize = 0; + } + + HRESULT Flush(); + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); +}; + +HRESULT COutVolumeStream::Flush() +{ + if (_volumeStream) + { + _file.UnPackSize = _curPos; + _file.FileCRC = _crc.GetDigest(); + RINOK(WriteVolumeHeader(_archive, _file, _options)); + _archive.Close(); + _volumeStream.Release(); + _file.StartPos += _file.UnPackSize; + } + return S_OK; +} +*/ + +/* +STDMETHODIMP COutMultiStream::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + if(processedSize != NULL) + *processedSize = 0; + while(size > 0) + { + if (_streamIndex >= Streams.Size()) + { + CSubStreamInfo subStream; + RINOK(VolumeCallback->GetVolumeSize(Streams.Size(), &subStream.Size)); + RINOK(VolumeCallback->GetVolumeStream(Streams.Size(), &subStream.Stream)); + subStream.Pos = 0; + Streams.Add(subStream); + continue; + } + CSubStreamInfo &subStream = Streams[_streamIndex]; + if (_offsetPos >= subStream.Size) + { + _offsetPos -= subStream.Size; + _streamIndex++; + continue; + } + if (_offsetPos != subStream.Pos) + { + CMyComPtr outStream; + RINOK(subStream.Stream.QueryInterface(IID_IOutStream, &outStream)); + RINOK(outStream->Seek(_offsetPos, STREAM_SEEK_SET, NULL)); + subStream.Pos = _offsetPos; + } + + UInt32 curSize = (UInt32)MyMin((UInt64)size, subStream.Size - subStream.Pos); + UInt32 realProcessed; + RINOK(subStream.Stream->Write(data, curSize, &realProcessed)); + data = (void *)((Byte *)data + realProcessed); + size -= realProcessed; + subStream.Pos += realProcessed; + _offsetPos += realProcessed; + _absPos += realProcessed; + if (_absPos > _length) + _length = _absPos; + if(processedSize != NULL) + *processedSize += realProcessed; + if (subStream.Pos == subStream.Size) + { + _streamIndex++; + _offsetPos = 0; + } + if (realProcessed != curSize && realProcessed == 0) + return E_FAIL; + } + return S_OK; +} + +STDMETHODIMP COutMultiStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) +{ + if(seekOrigin >= 3) + return STG_E_INVALIDFUNCTION; + switch(seekOrigin) + { + case STREAM_SEEK_SET: + _absPos = offset; + break; + case STREAM_SEEK_CUR: + _absPos += offset; + break; + case STREAM_SEEK_END: + _absPos = _length + offset; + break; + } + _offsetPos = _absPos; + _streamIndex = 0; + return S_OK; +} +*/ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/MultiStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/MultiStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,76 @@ +// MultiStream.h + +#ifndef __MULTISTREAM_H +#define __MULTISTREAM_H + +#include "../../../Common/MyCom.h" +#include "../../../Common/MyVector.h" +#include "../../Archive/IArchive.h" + +class CMultiStream: + public IInStream, + public CMyUnknownImp +{ + int _streamIndex; + UInt64 _pos; + UInt64 _seekPos; + UInt64 _totalLength; +public: + struct CSubStreamInfo + { + CMyComPtr Stream; + UInt64 Pos; + UInt64 Size; + }; + CObjectVector Streams; + void Init() + { + _streamIndex = 0; + _pos = 0; + _seekPos = 0; + _totalLength = 0; + for (int i = 0; i < Streams.Size(); i++) + _totalLength += Streams[i].Size; + } + + MY_UNKNOWN_IMP1(IInStream) + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition); +}; + +/* +class COutMultiStream: + public IOutStream, + public CMyUnknownImp +{ + int _streamIndex; // required stream + UInt64 _offsetPos; // offset from start of _streamIndex index + UInt64 _absPos; + UInt64 _length; + + struct CSubStreamInfo + { + CMyComPtr Stream; + UInt64 Size; + UInt64 Pos; + }; + CObjectVector Streams; +public: + CMyComPtr VolumeCallback; + void Init() + { + _streamIndex = 0; + _offsetPos = 0; + _absPos = 0; + _length = 0; + } + + MY_UNKNOWN_IMP1(IOutStream) + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition); +}; +*/ + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/OutStreamWithCRC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/OutStreamWithCRC.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +// OutStreamWithCRC.cpp + +#include "StdAfx.h" + +#include "OutStreamWithCRC.h" + +STDMETHODIMP COutStreamWithCRC::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize; + HRESULT result; + if(!_stream) + { + realProcessedSize = size; + result = S_OK; + } + else + result = _stream->Write(data, size, &realProcessedSize); + if (_calculate) + _crc = CrcUpdate(_crc, data, realProcessedSize); + _size += realProcessedSize; + if(processedSize != NULL) + *processedSize = realProcessedSize; + return result; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/OutStreamWithCRC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/OutStreamWithCRC.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,38 @@ +// OutStreamWithCRC.h + +#ifndef __OUT_STREAM_WITH_CRC_H +#define __OUT_STREAM_WITH_CRC_H + +#include "../../../Common/MyCom.h" +#include "../../IStream.h" + +extern "C" +{ +#include "../../../../C/7zCrc.h" +} + +class COutStreamWithCRC: + public ISequentialOutStream, + public CMyUnknownImp +{ + CMyComPtr _stream; + UInt64 _size; + UInt32 _crc; + bool _calculate; +public: + MY_UNKNOWN_IMP + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + void SetStream(ISequentialOutStream *stream) { _stream = stream; } + void ReleaseStream() { _stream.Release(); } + void Init(bool calculate = true) + { + _size = 0; + _calculate = calculate; + _crc = CRC_INIT_VAL; + } + void InitCRC() { _crc = CRC_INIT_VAL; } + UInt64 GetSize() const { return _size; } + UInt32 GetCRC() const { return CRC_GET_DIGEST(_crc); } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/ParseProperties.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/ParseProperties.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,177 @@ +// ParseProperties.cpp + +#include "StdAfx.h" + +#include "ParseProperties.h" + +#include "Common/StringToInt.h" +#include "Common/MyCom.h" + +HRESULT ParsePropValue(const UString &name, const PROPVARIANT &prop, UInt32 &resValue) +{ + if (prop.vt == VT_UI4) + { + if (!name.IsEmpty()) + return E_INVALIDARG; + resValue = prop.ulVal; + } + else if (prop.vt == VT_EMPTY) + { + if(!name.IsEmpty()) + { + const wchar_t *start = name; + const wchar_t *end; + UInt64 v = ConvertStringToUInt64(start, &end); + if (end - start != name.Length()) + return E_INVALIDARG; + resValue = (UInt32)v; + } + } + else + return E_INVALIDARG; + return S_OK; +} + +static const int kLogarithmicSizeLimit = 32; +static const wchar_t kByteSymbol = L'B'; +static const wchar_t kKiloByteSymbol = L'K'; +static const wchar_t kMegaByteSymbol = L'M'; + +HRESULT ParsePropDictionaryValue(const UString &srcStringSpec, UInt32 &dicSize) +{ + UString srcString = srcStringSpec; + srcString.MakeUpper(); + + const wchar_t *start = srcString; + const wchar_t *end; + UInt64 number = ConvertStringToUInt64(start, &end); + int numDigits = (int)(end - start); + if (numDigits == 0 || srcString.Length() > numDigits + 1) + return E_INVALIDARG; + if (srcString.Length() == numDigits) + { + if (number >= kLogarithmicSizeLimit) + return E_INVALIDARG; + dicSize = (UInt32)1 << (int)number; + return S_OK; + } + switch (srcString[numDigits]) + { + case kByteSymbol: + if (number >= ((UInt64)1 << kLogarithmicSizeLimit)) + return E_INVALIDARG; + dicSize = (UInt32)number; + break; + case kKiloByteSymbol: + if (number >= ((UInt64)1 << (kLogarithmicSizeLimit - 10))) + return E_INVALIDARG; + dicSize = (UInt32)(number << 10); + break; + case kMegaByteSymbol: + if (number >= ((UInt64)1 << (kLogarithmicSizeLimit - 20))) + return E_INVALIDARG; + dicSize = (UInt32)(number << 20); + break; + default: + return E_INVALIDARG; + } + return S_OK; +} + +HRESULT ParsePropDictionaryValue(const UString &name, const PROPVARIANT &prop, UInt32 &resValue) +{ + if (name.IsEmpty()) + { + if (prop.vt == VT_UI4) + { + UInt32 logDicSize = prop.ulVal; + if (logDicSize >= 32) + return E_INVALIDARG; + resValue = (UInt32)1 << logDicSize; + return S_OK; + } + if (prop.vt == VT_BSTR) + return ParsePropDictionaryValue(prop.bstrVal, resValue); + return E_INVALIDARG; + } + return ParsePropDictionaryValue(name, resValue); +} + +bool StringToBool(const UString &s, bool &res) +{ + if (s.IsEmpty() || s.CompareNoCase(L"ON") == 0 || s.Compare(L"+") == 0) + { + res = true; + return true; + } + if (s.CompareNoCase(L"OFF") == 0 || s.Compare(L"-") == 0) + { + res = false; + return true; + } + return false; +} + +HRESULT SetBoolProperty(bool &dest, const PROPVARIANT &value) +{ + switch(value.vt) + { + case VT_EMPTY: + dest = true; + return S_OK; + case VT_BOOL: + dest = (value.boolVal != VARIANT_FALSE); + return S_OK; + /* + case VT_UI4: + dest = (value.ulVal != 0); + break; + */ + case VT_BSTR: + return StringToBool(value.bstrVal, dest) ? S_OK : E_INVALIDARG; + } + return E_INVALIDARG; +} + +int ParseStringToUInt32(const UString &srcString, UInt32 &number) +{ + const wchar_t *start = srcString; + const wchar_t *end; + UInt64 number64 = ConvertStringToUInt64(start, &end); + if (number64 > 0xFFFFFFFF) + { + number = 0; + return 0; + } + number = (UInt32)number64; + return (int)(end - start); +} + +HRESULT ParseMtProp(const UString &name, const PROPVARIANT &prop, UInt32 defaultNumThreads, UInt32 &numThreads) +{ + if (name.IsEmpty()) + { + switch(prop.vt) + { + case VT_UI4: + numThreads = prop.ulVal; + break; + default: + { + bool val; + RINOK(SetBoolProperty(val, prop)); + numThreads = (val ? defaultNumThreads : 1); + break; + } + } + } + else + { + UInt32 number; + int index = ParseStringToUInt32(name, number); + if (index != name.Length()) + return E_INVALIDARG; + numThreads = number; + } + return S_OK; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/ParseProperties.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/ParseProperties.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// ParseProperties.h + +#ifndef __PARSEPROPERTIES_H +#define __PARSEPROPERTIES_H + +#include "Common/MyString.h" +#include "Common/Types.h" + +HRESULT ParsePropValue(const UString &name, const PROPVARIANT &prop, UInt32 &resValue); +HRESULT ParsePropDictionaryValue(const UString &srcStringSpec, UInt32 &dicSize); +HRESULT ParsePropDictionaryValue(const UString &name, const PROPVARIANT &prop, UInt32 &resValue); + +bool StringToBool(const UString &s, bool &res); +HRESULT SetBoolProperty(bool &dest, const PROPVARIANT &value); +int ParseStringToUInt32(const UString &srcString, UInt32 &number); +HRESULT ParseMtProp(const UString &name, const PROPVARIANT &prop, UInt32 defaultNumThreads, UInt32 &numThreads); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Common/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Common/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,9 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" +#include "../../../Common/NewHandler.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/DllExports2.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/DllExports2.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,82 @@ +// DLLExports.cpp + +#include "StdAfx.h" + +#include "../../Common/MyInitGuid.h" +#include "../../Common/ComTry.h" +#include "../../Common/Types.h" +#include "../../Windows/PropVariant.h" +#if defined(_WIN32) && defined(_7ZIP_LARGE_PAGES) +extern "C" +{ +#include "../../../C/Alloc.h" +} +#endif + +#include "IArchive.h" +#include "../ICoder.h" +#include "../IPassword.h" + +HINSTANCE g_hInstance; +#ifndef _UNICODE +#ifdef _WIN32 +bool g_IsNT = false; +static bool IsItWindowsNT() +{ + OSVERSIONINFO versionInfo; + versionInfo.dwOSVersionInfoSize = sizeof(versionInfo); + if (!::GetVersionEx(&versionInfo)) + return false; + return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT); +} +#endif +#endif + +extern "C" +BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/) +{ + if (dwReason == DLL_PROCESS_ATTACH) + { + g_hInstance = hInstance; + #ifndef _UNICODE + #ifdef _WIN32 + g_IsNT = IsItWindowsNT(); + #endif + #endif + } + return TRUE; +} + +DEFINE_GUID(CLSID_CArchiveHandler, +0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00); + +static const UInt16 kDecodeId = 0x2790; + +DEFINE_GUID(CLSID_CCodec, +0x23170F69, 0x40C1, kDecodeId, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); + +STDAPI CreateCoder(const GUID *clsid, const GUID *iid, void **outObject); +STDAPI CreateArchiver(const GUID *classID, const GUID *iid, void **outObject); + +STDAPI CreateObject(const GUID *clsid, const GUID *iid, void **outObject) +{ + // COM_TRY_BEGIN + *outObject = 0; + if (*iid == IID_ICompressCoder || *iid == IID_ICompressCoder2 || *iid == IID_ICompressFilter) + { + return CreateCoder(clsid, iid, outObject); + } + else + { + return CreateArchiver(clsid, iid, outObject); + } + // COM_TRY_END +} + +STDAPI SetLargePageMode() +{ + #if defined(_WIN32) && defined(_7ZIP_LARGE_PAGES) + SetLargePageSize(); + #endif + return S_OK; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,284 @@ +// GZipHandler.cpp + +#include "StdAfx.h" + +#include "GZipHandler.h" + +#include "Common/Defs.h" +#include "Common/StringConvert.h" +#include "Common/ComTry.h" +#include "Windows/PropVariant.h" +#include "Windows/Time.h" + +#include "../../ICoder.h" +#include "../../Common/ProgressUtils.h" +#include "../../Common/CreateCoder.h" +#include "../Common/OutStreamWithCRC.h" + +using namespace NWindows; + +namespace NArchive { +namespace NGZip { + +static const CMethodId kMethodId_Deflate = 0x040108; + +const wchar_t *kHostOS[] = +{ + L"FAT", + L"AMIGA", + L"VMS", + L"Unix", + L"VM_CMS", + L"Atari", // what if it's a minix filesystem? [cjh] + L"HPFS", // filesystem used by OS/2 (and NT 3.x) + L"Mac", + L"Z_System", + L"CPM", + L"TOPS20", // pkzip 2.50 NTFS + L"NTFS", // filesystem used by Windows NT + L"QDOS ", // SMS/QDOS + L"Acorn", // Archimedes Acorn RISC OS + L"VFAT", // filesystem used by Windows 95, NT + L"MVS", + L"BeOS", // hybrid POSIX/database filesystem + // BeBOX or PowerMac + L"Tandem", + L"THEOS" +}; + +static const int kNumHostOSes = sizeof(kHostOS) / sizeof(kHostOS[0]); + +static const wchar_t *kUnknownOS = L"Unknown"; + +/* +enum // PropID +{ + kpidExtraIsPresent = kpidUserDefined, + kpidExtraFlags, + kpidIsText +}; +*/ + +STATPROPSTG kProps[] = +{ + { NULL, kpidPath, VT_BSTR}, + { NULL, kpidSize, VT_UI8}, + { NULL, kpidPackSize, VT_UI8}, + { NULL, kpidMTime, VT_FILETIME}, + // { NULL, kpidMethod, VT_UI1}, + { NULL, kpidHostOS, VT_BSTR}, + { NULL, kpidCRC, VT_UI4} + // { L"Extra", kpidExtraIsPresent, VT_BOOL} + // { L"Extra flags", kpidExtraFlags, VT_UI1}, + // { L"Is Text", kpidIsText, VT_BOOL}, +}; + +IMP_IInArchive_Props +IMP_IInArchive_ArcProps_NO + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = 1; + return S_OK; +} + +STDMETHODIMP CHandler::GetProperty(UInt32 /* index */, PROPID propID, PROPVARIANT *value) +{ + COM_TRY_BEGIN + NWindows::NCOM::CPropVariant prop; + switch(propID) + { + case kpidPath: + if (m_Item.NameIsPresent()) + prop = MultiByteToUnicodeString(m_Item.Name, CP_ACP); + break; + case kpidMTime: + { + FILETIME utcTime; + if (m_Item.Time != 0) + { + NTime::UnixTimeToFileTime((UInt32)m_Item.Time, utcTime); + prop = utcTime; + } + else + { + // utcTime.dwLowDateTime = utcTime.dwHighDateTime = 0; + // prop = utcTime; + } + break; + } + case kpidSize: prop = UInt64(m_Item.UnPackSize32); break; + case kpidPackSize: prop = m_PackSize; break; + case kpidCommented: prop = m_Item.CommentIsPresent(); break; + case kpidHostOS: + prop = (m_Item.HostOS < kNumHostOSes) ? + kHostOS[m_Item.HostOS] : kUnknownOS; + break; + case kpidMethod: prop = m_Item.CompressionMethod; break; + case kpidCRC: prop = m_Item.FileCRC; break; + /* + case kpidExtraFlags: prop = m_Item.ExtraFlags; break; + case kpidIsText: prop = m_Item.IsText(); break; + case kpidExtraIsPresent: prop = m_Item.ExtraFieldIsPresent(); break; + */ + } + prop.Detach(value); + return S_OK; + COM_TRY_END +} + +STDMETHODIMP CHandler::Open(IInStream *inStream, + const UInt64 * /* maxCheckStartPosition */, + IArchiveOpenCallback * /* openArchiveCallback */) +{ + COM_TRY_BEGIN + try + { + CInArchive archive; + RINOK(inStream->Seek(0, STREAM_SEEK_CUR, &m_StreamStartPosition)); + RINOK(archive.ReadHeader(inStream, m_Item)); + m_DataOffset = archive.GetOffset(); + UInt64 newPosition; + RINOK(inStream->Seek(-8, STREAM_SEEK_END, &newPosition)); + m_PackSize = newPosition - (m_StreamStartPosition + m_DataOffset); + if (archive.ReadPostHeader(inStream, m_Item) != S_OK) + return S_FALSE; + m_Stream = inStream; + } + catch(...) + { + return S_FALSE; + } + return S_OK; + COM_TRY_END +} + +STDMETHODIMP CHandler::Close() +{ + m_Stream.Release(); + return S_OK; +} + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 _aTestMode, IArchiveExtractCallback *extractCallback) +{ + COM_TRY_BEGIN + bool allFilesMode = (numItems == UInt32(-1)); + if (!allFilesMode) + { + if (numItems == 0) + return S_OK; + if (numItems != 1) + return E_INVALIDARG; + if (indices[0] != 0) + return E_INVALIDARG; + } + + bool testMode = (_aTestMode != 0); + + extractCallback->SetTotal(m_PackSize); + + UInt64 currentTotalPacked = 0; + + RINOK(extractCallback->SetCompleted(¤tTotalPacked)); + CMyComPtr realOutStream; + Int32 askMode; + askMode = testMode ? NArchive::NExtract::NAskMode::kTest : + NArchive::NExtract::NAskMode::kExtract; + + RINOK(extractCallback->GetStream(0, &realOutStream, askMode)); + + if(!testMode && !realOutStream) + return S_OK; + + extractCallback->PrepareOperation(askMode); + + COutStreamWithCRC *outStreamSpec = new COutStreamWithCRC; + CMyComPtr outStream(outStreamSpec); + outStreamSpec->SetStream(realOutStream); + outStreamSpec->Init(); + realOutStream.Release(); + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, true); + + CMyComPtr deflateDecoder; + bool firstItem = true; + RINOK(m_Stream->Seek(m_StreamStartPosition, STREAM_SEEK_SET, NULL)); + Int32 opRes; + for (;;) + { + lps->InSize = currentTotalPacked; + lps->OutSize = outStreamSpec->GetSize(); + + CInArchive archive; + CItem item; + HRESULT result = archive.ReadHeader(m_Stream, item); + if (result != S_OK) + { + if (firstItem) + return E_FAIL; + opRes = NArchive::NExtract::NOperationResult::kOK; + break; + } + firstItem = false; + + UInt64 dataStartPos; + RINOK(m_Stream->Seek(0, STREAM_SEEK_CUR, &dataStartPos)); + + outStreamSpec->InitCRC(); + + if (item.CompressionMethod != NFileHeader::NCompressionMethod::kDeflate) + { + opRes = NArchive::NExtract::NOperationResult::kUnSupportedMethod; + break; + } + + if (!deflateDecoder) + { + RINOK(CreateCoder( + EXTERNAL_CODECS_VARS + kMethodId_Deflate, deflateDecoder, false)); + if (!deflateDecoder) + { + opRes = NArchive::NExtract::NOperationResult::kUnSupportedMethod; + break; + } + } + result = deflateDecoder->Code(m_Stream, outStream, NULL, NULL, progress); + if (result != S_OK) + { + if (result != S_FALSE) + return result; + opRes = NArchive::NExtract::NOperationResult::kDataError; + break; + } + + CMyComPtr getInStreamProcessedSize; + RINOK(deflateDecoder.QueryInterface(IID_ICompressGetInStreamProcessedSize, + &getInStreamProcessedSize)); + UInt64 packSize; + RINOK(getInStreamProcessedSize->GetInStreamProcessedSize(&packSize)); + UInt64 pos; + RINOK(m_Stream->Seek(dataStartPos + packSize, STREAM_SEEK_SET, &pos)); + + currentTotalPacked = pos - m_StreamStartPosition; + + CItem postItem; + if (archive.ReadPostHeader(m_Stream, postItem) != S_OK) + return E_FAIL; + if((outStreamSpec->GetCRC() != postItem.FileCRC)) + { + opRes = NArchive::NExtract::NOperationResult::kCRCError; + break; + } + } + outStream.Release(); + return extractCallback->SetOperationResult(opRes); + COM_TRY_END +} + +IMPL_ISetCompressCodecsInfo + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,65 @@ +// GZip/Handler.h + +#ifndef __GZIP_HANDLER_H +#define __GZIP_HANDLER_H + +#include "Common/MyCom.h" + +#include "../IArchive.h" + +#include "../../Common/CreateCoder.h" + +#include "GZipIn.h" +#include "GZipUpdate.h" + +namespace NArchive { +namespace NGZip { + +class CHandler: + public IInArchive, + public IOutArchive, + public ISetProperties, + PUBLIC_ISetCompressCodecsInfo + public CMyUnknownImp +{ +public: + MY_QUERYINTERFACE_BEGIN2(IInArchive) + MY_QUERYINTERFACE_ENTRY(IOutArchive) + MY_QUERYINTERFACE_ENTRY(ISetProperties) + QUERY_ENTRY_ISetCompressCodecsInfo + MY_QUERYINTERFACE_END + MY_ADDREF_RELEASE + + INTERFACE_IInArchive(;) +#ifndef EXTRACT_ONLY + INTERFACE_IOutArchive(;) + + STDMETHOD(SetProperties)(const wchar_t **names, const PROPVARIANT *values, Int32 numProperties); +#endif + + DECL_ISetCompressCodecsInfo + + CHandler() { InitMethodProperties(); } + +private: + NArchive::NGZip::CItem m_Item; + UInt64 m_StreamStartPosition; + UInt64 m_DataOffset; + UInt64 m_PackSize; + CMyComPtr m_Stream; + CCompressionMethodMode m_Method; + UInt32 m_Level; + + DECL_EXTERNAL_CODECS_VARS + + void InitMethodProperties() + { + m_Method.NumMatchFinderCyclesDefined = false; + m_Level = m_Method.NumPasses = m_Method.NumFastBytes = + m_Method.NumMatchFinderCycles = m_Method.Algo = 0xFFFFFFFF; + } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipHeader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipHeader.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// Archive/GZip/Header.h + +#include "StdAfx.h" + +#include "GZipHeader.h" + +namespace NArchive { +namespace NGZip { + +extern UInt16 kSignature = 0x8B1F + 1; + +class CMarkersInitializer +{ +public: + CMarkersInitializer() + { kSignature--; } +} g_MarkerInitializer; + +}} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipHeader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipHeader.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,85 @@ +// Archive/GZip/Header.h + +#ifndef __ARCHIVE_GZIP_HEADER_H +#define __ARCHIVE_GZIP_HEADER_H + +#include "Common/Types.h" + +namespace NArchive { +namespace NGZip { + +extern UInt16 kSignature; +static const UInt32 kSignatureSize = 2; + +namespace NFileHeader +{ + /* + struct CBlock + { + UInt16 Id; + Byte CompressionMethod; + Byte Flags; + UInt32 Time; + Byte ExtraFlags; + Byte HostOS; + }; + */ + + namespace NFlags + { + const int kDataIsText = 1 << 0; + const int kHeaderCRCIsPresent = 1 << 1; + const int kExtraIsPresent = 1 << 2; + const int kNameIsPresent = 1 << 3; + const int kComentIsPresent = 1 << 4; + } + + namespace NExtraFlags + { + enum EEnum + { + kMaximum = 2, + kFastest = 4 + }; + } + + namespace NCompressionMethod + { + const Byte kDeflate = 8; + } + + namespace NHostOS + { + enum EEnum + { + kFAT = 0, // filesystem used by MS-DOS, OS/2, Win32 + // pkzip 2.50 (FAT / VFAT / FAT32 file systems) + kAMIGA = 1, + kVMS = 2, // VAX/VMS + kUnix = 3, + kVM_CMS = 4, + kAtari = 5, // what if it's a minix filesystem? [cjh] + kHPFS = 6, // filesystem used by OS/2 (and NT 3.x) + kMac = 7, + kZ_System = 8, + kCPM = 9, + kTOPS20 = 10, // pkzip 2.50 NTFS + kNTFS = 11, // filesystem used by Windows NT + kQDOS = 12, // SMS/QDOS + kAcorn = 13, // Archimedes Acorn RISC OS + kVFAT = 14, // filesystem used by Windows 95, NT + kMVS = 15, + kBeOS = 16, // hybrid POSIX/database filesystem + // BeBOX or PowerMac + kTandem = 17, + kTHEOS = 18, + + kUnknown = 255 + }; + const int kNumHostSystems = 19; + } +} + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipIn.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,119 @@ +// Archive/GZipIn.cpp + +#include "StdAfx.h" + +#include "GZipIn.h" + +#include "Common/Defs.h" +#include "Common/MyCom.h" +#include "Windows/Defs.h" + +#include "../../Common/StreamUtils.h" + +extern "C" +{ + #include "../../../../C/7zCrc.h" +} + +namespace NArchive { +namespace NGZip { + +HRESULT CInArchive::ReadBytes(ISequentialInStream *inStream, void *data, UInt32 size) +{ + RINOK(ReadStream_FALSE(inStream, data, size)); + m_Position += size; + return S_OK; +} + +HRESULT CInArchive::ReadByte(ISequentialInStream *inStream, Byte &value, UInt32 &crc) +{ + RINOK(ReadBytes(inStream, &value, 1)); + crc = CRC_UPDATE_BYTE(crc, value); + return S_OK; +} + +HRESULT CInArchive::ReadUInt16(ISequentialInStream *inStream, UInt16 &value, UInt32 &crc) +{ + value = 0; + for (int i = 0; i < 2; i++) + { + Byte b; + RINOK(ReadByte(inStream, b, crc)); + value |= (UInt16(b) << (8 * i)); + } + return S_OK; +} + +HRESULT CInArchive::ReadUInt32(ISequentialInStream *inStream, UInt32 &value, UInt32 &crc) +{ + value = 0; + for (int i = 0; i < 4; i++) + { + Byte b; + RINOK(ReadByte(inStream, b, crc)); + value |= (UInt32(b) << (8 * i)); + } + return S_OK; +} + +HRESULT CInArchive::ReadZeroTerminatedString(ISequentialInStream *inStream, AString &resString, UInt32 &crc) +{ + resString.Empty(); + for (;;) + { + Byte c; + RINOK(ReadByte(inStream, c, crc)); + if (c == 0) + return S_OK; + resString += char(c); + } +} + +HRESULT CInArchive::ReadHeader(ISequentialInStream *inStream, CItem &item) +{ + item.Clear(); + m_Position = 0; + + UInt16 signature; + UInt32 crc = CRC_INIT_VAL;; + RINOK(ReadUInt16(inStream, signature, crc)); + if (signature != kSignature) + return S_FALSE; + + RINOK(ReadByte(inStream, item.CompressionMethod, crc)); + RINOK(ReadByte(inStream, item.Flags, crc)); + RINOK(ReadUInt32(inStream, item.Time, crc)); + RINOK(ReadByte(inStream, item.ExtraFlags, crc)); + RINOK(ReadByte(inStream, item.HostOS, crc)); + + if (item.ExtraFieldIsPresent()) + { + UInt16 extraSize; + RINOK(ReadUInt16(inStream, extraSize, crc)); + item.Extra.SetCapacity(extraSize); + RINOK(ReadBytes(inStream, item.Extra, extraSize)); + crc = CrcUpdate(crc, item.Extra, extraSize); + } + if (item.NameIsPresent()) + RINOK(ReadZeroTerminatedString(inStream, item.Name, crc)); + if (item.CommentIsPresent()) + RINOK(ReadZeroTerminatedString(inStream, item.Comment, crc)); + if (item.HeaderCRCIsPresent()) + { + UInt16 headerCRC; + UInt32 dummy = 0; + RINOK(ReadUInt16(inStream, headerCRC, dummy)); + if ((UInt16)CRC_GET_DIGEST(crc) != headerCRC) + return S_FALSE; + } + return S_OK; +} + +HRESULT CInArchive::ReadPostHeader(ISequentialInStream *inStream, CItem &item) +{ + UInt32 dummy = 0; + RINOK(ReadUInt32(inStream, item.FileCRC, dummy)); + return ReadUInt32(inStream, item.UnPackSize32, dummy); +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipIn.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +// Archive/GZipIn.h + +#ifndef __ARCHIVE_GZIP_IN_H +#define __ARCHIVE_GZIP_IN_H + +#include "GZipHeader.h" +#include "GZipItem.h" +#include "../../IStream.h" + +namespace NArchive { +namespace NGZip { + +class CInArchive +{ + UInt64 m_Position; + + HRESULT ReadBytes(ISequentialInStream *inStream, void *data, UInt32 size); + HRESULT ReadZeroTerminatedString(ISequentialInStream *inStream, AString &resString, UInt32 &crc); + HRESULT ReadByte(ISequentialInStream *inStream, Byte &value, UInt32 &crc); + HRESULT ReadUInt16(ISequentialInStream *inStream, UInt16 &value, UInt32 &crc); + HRESULT ReadUInt32(ISequentialInStream *inStream, UInt32 &value, UInt32 &crc); +public: + HRESULT ReadHeader(ISequentialInStream *inStream, CItem &item); + HRESULT ReadPostHeader(ISequentialInStream *inStream, CItem &item); + UInt64 GetOffset() const { return m_Position; } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipItem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipItem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,59 @@ +// Archive/GZipItem.h + +#ifndef __ARCHIVE_GZIP_ITEM_H +#define __ARCHIVE_GZIP_ITEM_H + +#include "Common/Types.h" +#include "Common/MyString.h" +#include "Common/Buffer.h" + +namespace NArchive { +namespace NGZip { + +class CItem +{ +private: + bool TestFlag(Byte flag) const { return ((Flags & flag) != 0); } +public: + Byte CompressionMethod; + Byte Flags; + UInt32 Time; + Byte ExtraFlags; + Byte HostOS; + UInt32 FileCRC; + UInt32 UnPackSize32; + + AString Name; + AString Comment; + CByteBuffer Extra; + + bool IsText() const + { return TestFlag(NFileHeader::NFlags::kDataIsText); } + bool HeaderCRCIsPresent() const + { return TestFlag(NFileHeader::NFlags::kHeaderCRCIsPresent); } + bool ExtraFieldIsPresent() const + { return TestFlag(NFileHeader::NFlags::kExtraIsPresent); } + bool NameIsPresent() const + { return TestFlag(NFileHeader::NFlags::kNameIsPresent); } + bool CommentIsPresent() const + { return TestFlag(NFileHeader::NFlags::kComentIsPresent); } + + void SetNameIsPresentFlag(bool nameIsPresent) + { + if (nameIsPresent) + Flags |= NFileHeader::NFlags::kNameIsPresent; + else + Flags &= (~NFileHeader::NFlags::kNameIsPresent); + } + + void Clear() + { + Name.Empty(); + Comment.Empty();; + Extra.SetCapacity(0); + } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// GZipRegister.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "GZipHandler.h" +static IInArchive *CreateArc() { return new NArchive::NGZip::CHandler; } +#ifndef EXTRACT_ONLY +static IOutArchive *CreateArcOut() { return new NArchive::NGZip::CHandler; } +#else +#define CreateArcOut 0 +#endif + +static CArcInfo g_ArcInfo = + { L"GZip", L"gz gzip tgz tpz", L"* * .tar .tar", 0xEF, { 0x1F, 0x8B, 8 }, 3, true, CreateArc, CreateArcOut }; + +REGISTER_ARC(GZip) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipUpdate.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipUpdate.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,39 @@ +// GZip/Update.h + +#ifndef __GZIP_UPDATE_H +#define __GZIP_UPDATE_H + +#include "../IArchive.h" + +#include "../../Common/CreateCoder.h" + +#ifndef EXTRACT_ONLY +#include "GZipOut.h" +#endif +#include "GZipItem.h" + +namespace NArchive { +namespace NGZip { + +struct CCompressionMethodMode +{ + UInt32 NumPasses; + UInt32 NumFastBytes; + UInt32 Algo; + bool NumMatchFinderCyclesDefined; + UInt32 NumMatchFinderCycles; +}; + +HRESULT UpdateArchive( + DECL_EXTERNAL_CODECS_LOC_VARS + IInStream *inStream, + UInt64 unpackSize, + ISequentialOutStream *outStream, + const CItem &newItem, + const CCompressionMethodMode &compressionMethod, + int indexInClient, + IArchiveUpdateCallback *updateCallback); + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/GZip/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/GZip/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/IArchive.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/IArchive.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,237 @@ +// IArchive.h + +#ifndef __IARCHIVE_H +#define __IARCHIVE_H + +#include "../IStream.h" +#include "../IProgress.h" +#include "../PropID.h" + +#define ARCHIVE_INTERFACE_SUB(i, base, x) DECL_INTERFACE_SUB(i, base, 6, x) +#define ARCHIVE_INTERFACE(i, x) ARCHIVE_INTERFACE_SUB(i, IUnknown, x) + +namespace NFileTimeType +{ + enum EEnum + { + kWindows, + kUnix, + kDOS + }; +} + +namespace NArchive +{ + enum + { + kName = 0, + kClassID, + kExtension, + kAddExtension, + kUpdate, + kKeepName, + kStartSignature, + kFinishSignature, + kAssociate + }; + + namespace NExtract + { + namespace NAskMode + { + enum + { + kExtract = 0, + kTest, + kSkip + }; + } + namespace NOperationResult + { + enum + { + kOK = 0, + kUnSupportedMethod, + kDataError, + kCRCError + }; + } + } + namespace NUpdate + { + namespace NOperationResult + { + enum + { + kOK = 0, + kError + }; + } + } +} + +#define INTERFACE_IArchiveOpenCallback(x) \ + STDMETHOD(SetTotal)(const UInt64 *files, const UInt64 *bytes) x; \ + STDMETHOD(SetCompleted)(const UInt64 *files, const UInt64 *bytes) x; \ + +ARCHIVE_INTERFACE(IArchiveOpenCallback, 0x10) +{ + INTERFACE_IArchiveOpenCallback(PURE); +}; + + +#define INTERFACE_IArchiveExtractCallback(x) \ + INTERFACE_IProgress(x) \ + /* GetStream OUT: S_OK - OK, S_FALSE - skeep this file */ \ + STDMETHOD(GetStream)(UInt32 index, ISequentialOutStream **outStream, Int32 askExtractMode) x; \ + STDMETHOD(PrepareOperation)(Int32 askExtractMode) x; \ + STDMETHOD(SetOperationResult)(Int32 resultEOperationResult) x; \ + +ARCHIVE_INTERFACE_SUB(IArchiveExtractCallback, IProgress, 0x20) +{ + INTERFACE_IArchiveExtractCallback(PURE) +}; + + +#define INTERFACE_IArchiveOpenVolumeCallback(x) \ + STDMETHOD(GetProperty)(PROPID propID, PROPVARIANT *value) x; \ + STDMETHOD(GetStream)(const wchar_t *name, IInStream **inStream) x; \ + +ARCHIVE_INTERFACE(IArchiveOpenVolumeCallback, 0x30) +{ + INTERFACE_IArchiveOpenVolumeCallback(PURE); +}; + + +ARCHIVE_INTERFACE(IInArchiveGetStream, 0x40) +{ + STDMETHOD(GetStream)(UInt32 index, ISequentialInStream **stream) PURE; +}; + + +ARCHIVE_INTERFACE(IArchiveOpenSetSubArchiveName, 0x50) +{ + STDMETHOD(SetSubArchiveName)(const wchar_t *name) PURE; +}; + + +/* +IInArchive::Extract: + indices must be sorted + numItems = 0xFFFFFFFF means "all files" + testMode != 0 means "test files without writing to outStream" +*/ + +#define INTERFACE_IInArchive(x) \ + STDMETHOD(Open)(IInStream *stream, const UInt64 *maxCheckStartPosition, IArchiveOpenCallback *openArchiveCallback) x; \ + STDMETHOD(Close)() x; \ + STDMETHOD(GetNumberOfItems)(UInt32 *numItems) x; \ + STDMETHOD(GetProperty)(UInt32 index, PROPID propID, PROPVARIANT *value) x; \ + STDMETHOD(Extract)(const UInt32* indices, UInt32 numItems, Int32 testMode, IArchiveExtractCallback *extractCallback) x; \ + STDMETHOD(GetArchiveProperty)(PROPID propID, PROPVARIANT *value) x; \ + STDMETHOD(GetNumberOfProperties)(UInt32 *numProperties) x; \ + STDMETHOD(GetPropertyInfo)(UInt32 index, BSTR *name, PROPID *propID, VARTYPE *varType) x; \ + STDMETHOD(GetNumberOfArchiveProperties)(UInt32 *numProperties) x; \ + STDMETHOD(GetArchivePropertyInfo)(UInt32 index, BSTR *name, PROPID *propID, VARTYPE *varType) x; + +ARCHIVE_INTERFACE(IInArchive, 0x60) +{ + INTERFACE_IInArchive(PURE) + virtual ~IInArchive() {} +}; + + +#define INTERFACE_IArchiveUpdateCallback(x) \ + INTERFACE_IProgress(x); \ + STDMETHOD(GetUpdateItemInfo)(UInt32 index, \ + Int32 *newData, /*1 - new data, 0 - old data */ \ + Int32 *newProperties, /* 1 - new properties, 0 - old properties */ \ + UInt32 *indexInArchive /* -1 if there is no in archive, or if doesn't matter */ \ + ) x; \ + STDMETHOD(GetProperty)(UInt32 index, PROPID propID, PROPVARIANT *value) x; \ + STDMETHOD(GetStream)(UInt32 index, ISequentialInStream **inStream) x; \ + STDMETHOD(SetOperationResult)(Int32 operationResult) x; \ + +ARCHIVE_INTERFACE_SUB(IArchiveUpdateCallback, IProgress, 0x80) +{ + INTERFACE_IArchiveUpdateCallback(PURE); +}; + +#define INTERFACE_IArchiveUpdateCallback2(x) \ + INTERFACE_IArchiveUpdateCallback(x) \ + STDMETHOD(GetVolumeSize)(UInt32 index, UInt64 *size) x; \ + STDMETHOD(GetVolumeStream)(UInt32 index, ISequentialOutStream **volumeStream) x; \ + +ARCHIVE_INTERFACE_SUB(IArchiveUpdateCallback2, IArchiveUpdateCallback, 0x82) +{ + INTERFACE_IArchiveUpdateCallback2(PURE); +}; + + +#ifndef EXTRACT_ONLY +#define INTERFACE_IOutArchive(x) \ + STDMETHOD(UpdateItems)(ISequentialOutStream *outStream, UInt32 numItems, IArchiveUpdateCallback *updateCallback) x; \ + STDMETHOD(GetFileTimeType)(UInt32 *type) x; +#else +#define INTERFACE_IOutArchive(x) +#endif + +ARCHIVE_INTERFACE(IOutArchive, 0xA0) +{ +#ifndef EXTRACT_ONLY + INTERFACE_IOutArchive(PURE) +#endif +}; + + +ARCHIVE_INTERFACE(ISetProperties, 0x03) +{ +#ifndef EXTRACT_ONLY + STDMETHOD(SetProperties)(const wchar_t **names, const PROPVARIANT *values, Int32 numProperties) PURE; +#endif +}; + + +#define IMP_IInArchive_GetProp(k) \ + (UInt32 index, BSTR *name, PROPID *propID, VARTYPE *varType) \ + { if(index >= sizeof(k) / sizeof(k[0])) return E_INVALIDARG; \ + const STATPROPSTG &srcItem = k[index]; \ + *propID = srcItem.propid; *varType = srcItem.vt; *name = 0; return S_OK; } \ + +#define IMP_IInArchive_GetProp_WITH_NAME(k) \ + (UInt32 index, BSTR *name, PROPID *propID, VARTYPE *varType) \ + { if(index >= sizeof(k) / sizeof(k[0])) return E_INVALIDARG; \ + const STATPROPSTG &srcItem = k[index]; \ + *propID = srcItem.propid; *varType = srcItem.vt; \ + if (srcItem.lpwstrName == 0) *name = 0; else *name = ::SysAllocString(srcItem.lpwstrName); return S_OK; } \ + +#define IMP_IInArchive_Props \ + STDMETHODIMP CHandler::GetNumberOfProperties(UInt32 *numProperties) \ + { *numProperties = sizeof(kProps) / sizeof(kProps[0]); return S_OK; } \ + STDMETHODIMP CHandler::GetPropertyInfo IMP_IInArchive_GetProp(kProps) + +#define IMP_IInArchive_Props_WITH_NAME \ + STDMETHODIMP CHandler::GetNumberOfProperties(UInt32 *numProperties) \ + { *numProperties = sizeof(kProps) / sizeof(kProps[0]); return S_OK; } \ + STDMETHODIMP CHandler::GetPropertyInfo IMP_IInArchive_GetProp_WITH_NAME(kProps) + + +#define IMP_IInArchive_ArcProps \ + STDMETHODIMP CHandler::GetNumberOfArchiveProperties(UInt32 *numProperties) \ + { *numProperties = sizeof(kArcProps) / sizeof(kArcProps[0]); return S_OK; } \ + STDMETHODIMP CHandler::GetArchivePropertyInfo IMP_IInArchive_GetProp(kArcProps) + +#define IMP_IInArchive_ArcProps_WITH_NAME \ + STDMETHODIMP CHandler::GetNumberOfArchiveProperties(UInt32 *numProperties) \ + { *numProperties = sizeof(kArcProps) / sizeof(kArcProps[0]); return S_OK; } \ + STDMETHODIMP CHandler::GetArchivePropertyInfo IMP_IInArchive_GetProp_WITH_NAME(kArcProps) + +#define IMP_IInArchive_ArcProps_NO \ + STDMETHODIMP CHandler::GetNumberOfArchiveProperties(UInt32 *numProperties) \ + { *numProperties = 0; return S_OK; } \ + STDMETHODIMP CHandler::GetArchivePropertyInfo(UInt32, BSTR *, PROPID *, VARTYPE *) \ + { return E_NOTIMPL; } \ + STDMETHODIMP CHandler::GetArchiveProperty(PROPID, PROPVARIANT *value) \ + { value->vt = VT_EMPTY; return S_OK; } + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhCRC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhCRC.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,43 @@ +// LzhCRC.cpp + +#include "StdAfx.h" + +#include "LzhCRC.h" + +namespace NArchive { +namespace NLzh { + +static const UInt16 kCRCPoly = 0xA001; + +UInt16 CCRC::Table[256]; + +void CCRC::InitTable() +{ + for (UInt32 i = 0; i < 256; i++) + { + UInt32 r = i; + for (int j = 0; j < 8; j++) + if (r & 1) + r = (r >> 1) ^ kCRCPoly; + else + r >>= 1; + CCRC::Table[i] = (UInt16)r; + } +} + +class CCRCTableInit +{ +public: + CCRCTableInit() { CCRC::InitTable(); } +} g_CRCTableInit; + +void CCRC::Update(const void *data, size_t size) +{ + UInt16 v = _value; + const Byte *p = (const Byte *)data; + for (; size > 0; size--, p++) + v = (UInt16)(Table[((Byte)(v)) ^ *p] ^ (v >> 8)); + _value = v; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhCRC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhCRC.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,27 @@ +// LzhCRC.h + +#ifndef __LZH_CRC_H +#define __LZH_CRC_H + +#include +#include "Common/Types.h" + +namespace NArchive { +namespace NLzh { + +class CCRC +{ + UInt16 _value; +public: + static UInt16 Table[256]; + static void InitTable(); + + CCRC(): _value(0){}; + void Init() { _value = 0; } + void Update(const void *data, size_t size); + UInt16 GetDigest() const { return _value; } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,387 @@ +// LzhHandler.cpp + +#include "StdAfx.h" + +#include "Common/ComTry.h" +#include "Common/Defs.h" +#include "Common/StringConvert.h" + +#include "Windows/PropVariant.h" +#include "Windows/Time.h" + +#include "LzhHandler.h" +#include "LzhOutStreamWithCRC.h" + +#include "../../ICoder.h" + +#include "../../Common/LimitedStreams.h" +#include "../../Common/ProgressUtils.h" + +#include "../../Compress/CopyCoder.h" +#include "../../Compress/LzhDecoder.h" + +#include "../Common/ItemNameUtils.h" + +using namespace NWindows; +using namespace NTime; + +namespace NArchive { +namespace NLzh{ + +struct COsPair +{ + Byte Id; + const wchar_t *Name; +}; + +COsPair g_OsPairs[] = +{ + { 'M', L"MS-DOS" }, + { '2', L"OS/2" }, + { '9', L"OS9" }, + { 'K', L"OS/68K" }, + { '3', L"OS/386" }, + { 'H', L"HUMAN" }, + { 'U', L"UNIX" }, + { 'C', L"CP/M" }, + { 'F', L"FLEX" }, + { 'm', L"Mac" }, + { 'R', L"Runser" }, + { 'T', L"TownsOS" }, + { 'X', L"XOSK" }, + { 'w', L"Windows95" }, + { 'W', L"WindowsNT" }, + { 0, L"MS-DOS" }, + { 'J', L"Java VM" } +}; + +const wchar_t *kUnknownOS = L"Unknown"; + +const int kNumHostOSes = sizeof(g_OsPairs) / sizeof(g_OsPairs[0]); + +static const wchar_t *GetOS(Byte osId) +{ + for (int i = 0; i < kNumHostOSes; i++) + if (g_OsPairs[i].Id == osId) + return g_OsPairs[i].Name; + return kUnknownOS; +}; + +STATPROPSTG kProps[] = +{ + { NULL, kpidPath, VT_BSTR}, + { NULL, kpidIsDir, VT_BOOL}, + { NULL, kpidSize, VT_UI8}, + { NULL, kpidPackSize, VT_UI8}, + { NULL, kpidMTime, VT_FILETIME}, + { NULL, kpidAttrib, VT_UI4}, + + // { NULL, kpidCommented, VT_BOOL}, + + { NULL, kpidCRC, VT_UI4}, + + { NULL, kpidMethod, VT_UI1}, + { NULL, kpidHostOS, VT_BSTR} + +}; + +IMP_IInArchive_Props +IMP_IInArchive_ArcProps_NO + +CHandler::CHandler() {} + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = _items.Size(); + return S_OK; +} + +STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value) +{ + COM_TRY_BEGIN + NWindows::NCOM::CPropVariant prop; + const CItemEx &item = _items[index]; + switch(propID) + { + case kpidPath: + { + UString s = NItemName::WinNameToOSName(MultiByteToUnicodeString(item.GetName(), CP_OEMCP)); + if (!s.IsEmpty()) + { + if (s[s.Length() - 1] == WCHAR_PATH_SEPARATOR) + s.Delete(s.Length() - 1); + prop = s; + } + break; + } + case kpidIsDir: prop = item.IsDir(); break; + case kpidSize: prop = item.Size; break; + case kpidPackSize: prop = item.PackSize; break; + case kpidCRC: prop = (UInt32)item.CRC; break; + case kpidHostOS: prop = GetOS(item.OsId); break; + case kpidMTime: + { + FILETIME utcFileTime; + UInt32 unixTime; + if (item.GetUnixTime(unixTime)) + NTime::UnixTimeToFileTime(unixTime, utcFileTime); + else + { + FILETIME localFileTime; + if (DosTimeToFileTime(item.ModifiedTime, localFileTime)) + { + if (!LocalFileTimeToFileTime(&localFileTime, &utcFileTime)) + utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0; + } + else + utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0; + } + prop = utcFileTime; + break; + } + /* + case kpidAttrib: prop = (UInt32)item.Attributes; break; + case kpidCommented: prop = item.IsCommented(); break; + */ + case kpidMethod: + { + wchar_t method2[kMethodIdSize + 1]; + method2[kMethodIdSize] = 0; + for (int i = 0; i < kMethodIdSize; i++) + method2[i] = item.Method[i]; + prop = method2; + break; + } + } + prop.Detach(value); + return S_OK; + COM_TRY_END +} + +/* +class CProgressImp: public CProgressVirt +{ +public: + CMyComPtr Callback; + STDMETHOD(SetCompleted)(const UInt64 *numFiles); +}; + +STDMETHODIMP CProgressImp::SetCompleted(const UInt64 *numFiles) +{ + if (Callback) + return Callback->SetCompleted(numFiles, NULL); + return S_OK; +} +*/ + +STDMETHODIMP CHandler::Open(IInStream *stream, + const UInt64 * /* maxCheckStartPosition */, IArchiveOpenCallback *callback) +{ + COM_TRY_BEGIN + try + { + _items.Clear(); + CInArchive archive; + + UInt64 endPos = 0; + bool needSetTotal = true; + + if (callback != NULL) + { + RINOK(stream->Seek(0, STREAM_SEEK_END, &endPos)); + RINOK(stream->Seek(0, STREAM_SEEK_SET, NULL)); + } + + RINOK(archive.Open(stream)); + for (;;) + { + CItemEx item; + bool filled; + HRESULT result = archive.GetNextItem(filled, item); + if (result == S_FALSE) + return S_FALSE; + if (result != S_OK) + return S_FALSE; + if (!filled) + break; + _items.Add(item); + archive.Skeep(item.PackSize); + if (callback != NULL) + { + if (needSetTotal) + { + RINOK(callback->SetTotal(NULL, &endPos)); + needSetTotal = false; + } + if (_items.Size() % 100 == 0) + { + UInt64 numFiles = _items.Size(); + UInt64 numBytes = item.DataPosition; + RINOK(callback->SetCompleted(&numFiles, &numBytes)); + } + } + } + if (_items.IsEmpty()) + return S_FALSE; + + _stream = stream; + } + catch(...) + { + return S_FALSE; + } + COM_TRY_END + return S_OK; +} + +STDMETHODIMP CHandler::Close() +{ + _items.Clear(); + _stream.Release(); + return S_OK; +} + + + +////////////////////////////////////// +// CHandler::DecompressItems + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 testModeSpec, IArchiveExtractCallback *extractCallback) +{ + COM_TRY_BEGIN + bool testMode = (testModeSpec != 0); + UInt64 totalUnPacked = 0, totalPacked = 0; + bool allFilesMode = (numItems == UInt32(-1)); + if (allFilesMode) + numItems = _items.Size(); + if(numItems == 0) + return S_OK; + UInt32 i; + for(i = 0; i < numItems; i++) + { + const CItemEx &item = _items[allFilesMode ? i : indices[i]]; + totalUnPacked += item.Size; + totalPacked += item.PackSize; + } + extractCallback->SetTotal(totalUnPacked); + + UInt64 currentTotalUnPacked = 0, currentTotalPacked = 0; + UInt64 currentItemUnPacked, currentItemPacked; + + NCompress::NLzh::NDecoder::CCoder *lzhDecoderSpec = 0; + CMyComPtr lzhDecoder; + CMyComPtr lzh1Decoder; + CMyComPtr arj2Decoder; + + NCompress::CCopyCoder *copyCoderSpec = new NCompress::CCopyCoder(); + CMyComPtr copyCoder = copyCoderSpec; + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, false); + + CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream; + CMyComPtr inStream(streamSpec); + streamSpec->SetStream(_stream); + + for(i = 0; i < numItems; i++, currentTotalUnPacked += currentItemUnPacked, + currentTotalPacked += currentItemPacked) + { + currentItemUnPacked = 0; + currentItemPacked = 0; + + lps->InSize = currentTotalPacked; + lps->OutSize = currentTotalUnPacked; + RINOK(lps->SetCur()); + + CMyComPtr realOutStream; + Int32 askMode; + askMode = testMode ? NExtract::NAskMode::kTest : + NExtract::NAskMode::kExtract; + Int32 index = allFilesMode ? i : indices[i]; + const CItemEx &item = _items[index]; + RINOK(extractCallback->GetStream(index, &realOutStream, askMode)); + + if (item.IsDir()) + { + // if (!testMode) + { + RINOK(extractCallback->PrepareOperation(askMode)); + RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kOK)); + } + continue; + } + + if (!testMode && (!realOutStream)) + continue; + + RINOK(extractCallback->PrepareOperation(askMode)); + currentItemUnPacked = item.Size; + currentItemPacked = item.PackSize; + + { + COutStreamWithCRC *outStreamSpec = new COutStreamWithCRC; + CMyComPtr outStream(outStreamSpec); + outStreamSpec->Init(realOutStream); + realOutStream.Release(); + + UInt64 pos; + _stream->Seek(item.DataPosition, STREAM_SEEK_SET, &pos); + + streamSpec->Init(item.PackSize); + + HRESULT result = S_OK; + Int32 opRes = NExtract::NOperationResult::kOK; + + if (item.IsCopyMethod()) + { + result = copyCoder->Code(inStream, outStream, NULL, NULL, progress); + if (result == S_OK && copyCoderSpec->TotalSize != item.PackSize) + result = S_FALSE; + } + else if (item.IsLh4GroupMethod()) + { + if (!lzhDecoder) + { + lzhDecoderSpec = new NCompress::NLzh::NDecoder::CCoder; + lzhDecoder = lzhDecoderSpec; + } + lzhDecoderSpec->SetDictionary(item.GetNumDictBits()); + result = lzhDecoder->Code(inStream, outStream, NULL, ¤tItemUnPacked, progress); + } + /* + else if (item.IsLh1GroupMethod()) + { + if (!lzh1Decoder) + { + lzh1DecoderSpec = new NCompress::NLzh1::NDecoder::CCoder; + lzh1Decoder = lzh1DecoderSpec; + } + lzh1DecoderSpec->SetDictionary(item.GetNumDictBits()); + result = lzh1Decoder->Code(inStream, outStream, NULL, ¤tItemUnPacked, progress); + } + */ + else + opRes = NExtract::NOperationResult::kUnSupportedMethod; + + if (opRes == NExtract::NOperationResult::kOK) + { + if (result == S_FALSE) + opRes = NExtract::NOperationResult::kDataError; + else + { + RINOK(result); + if (outStreamSpec->GetCRC() != item.CRC) + opRes = NExtract::NOperationResult::kCRCError; + } + } + outStream.Release(); + RINOK(extractCallback->SetOperationResult(opRes)); + } + } + return S_OK; + COM_TRY_END +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +// LzhHandler.h + +#ifndef __LZH_HANDLER_H +#define __LZH_HANDLER_H + +#include "Common/MyCom.h" +#include "../IArchive.h" +#include "LzhIn.h" + +namespace NArchive { +namespace NLzh { + +class CHandler: + public IInArchive, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP1(IInArchive) + + INTERFACE_IInArchive(;) + + CHandler(); +private: + CObjectVector _items; + CMyComPtr _stream; +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhHeader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhHeader.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,19 @@ +// Archive/Lzh/Header.h + +#ifndef __ARCHIVE_LZH_HEADER_H +#define __ARCHIVE_LZH_HEADER_H + +#include "Common/Types.h" + +namespace NArchive { +namespace NLzh { + +const int kMethodIdSize = 5; + +const Byte kExtIdFileName = 0x01; +const Byte kExtIdDirName = 0x02; +const Byte kExtIdUnixTime = 0x54; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhIn.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,172 @@ +// Archive/LzhIn.cpp + +#include "StdAfx.h" + +#include "Common/StringConvert.h" +#include "Common/Buffer.h" + +#include "../../Common/StreamUtils.h" + +#include "LzhIn.h" + +namespace NArchive { +namespace NLzh { + +HRESULT CInArchive::ReadBytes(void *data, UInt32 size, UInt32 &processedSize) +{ + size_t realProcessedSize = size; + RINOK(ReadStream(m_Stream, data, &realProcessedSize)); + processedSize = (UInt32)realProcessedSize; + m_Position += processedSize; + return S_OK; +} + +HRESULT CInArchive::CheckReadBytes(void *data, UInt32 size) +{ + UInt32 processedSize; + RINOK(ReadBytes(data, size, processedSize)); + return (processedSize == size) ? S_OK: S_FALSE; +} + +HRESULT CInArchive::Open(IInStream *inStream) +{ + RINOK(inStream->Seek(0, STREAM_SEEK_CUR, &m_Position)); + m_Stream = inStream; + return S_OK; +} + +static const Byte *ReadUInt32(const Byte *p, UInt32 &v) +{ + v = 0; + for (int i = 0; i < 4; i++) + v |= ((UInt32)(*p++) << (i * 8)); + return p; +} + +static const Byte *ReadUInt16(const Byte *p, UInt16 &v) +{ + v = 0; + for (int i = 0; i < 2; i++) + v |= ((UInt16)(*p++) << (i * 8)); + return p; +} + +static const Byte *ReadString(const Byte *p, size_t size, AString &s) +{ + s.Empty(); + for (size_t i = 0; i < size; i++) + { + char c = p[i]; + if (c == 0) + break; + s += c; + } + return p + size; +} + +static Byte CalcSum(const Byte *data, size_t size) +{ + Byte sum = 0; + for (size_t i = 0; i < size; i++) + sum = (Byte)(sum + data[i]); + return sum; +} + +HRESULT CInArchive::GetNextItem(bool &filled, CItemEx &item) +{ + filled = false; + + UInt32 processedSize; + Byte startHeader[2]; + RINOK(ReadBytes(startHeader, 2, processedSize)) + if (processedSize == 0) + return S_OK; + if (processedSize == 1) + return (startHeader[0] == 0) ? S_OK: S_FALSE; + if (startHeader[0] == 0 && startHeader[1] == 0) + return S_OK; + + Byte header[256]; + const UInt32 kBasicPartSize = 22; + RINOK(ReadBytes(header, kBasicPartSize, processedSize)); + if (processedSize != kBasicPartSize) + return (startHeader[0] == 0) ? S_OK: S_FALSE; + + const Byte *p = header; + memmove(item.Method, p, kMethodIdSize); + if (!item.IsValidMethod()) + return S_OK; + p += kMethodIdSize; + p = ReadUInt32(p, item.PackSize); + p = ReadUInt32(p, item.Size); + p = ReadUInt32(p, item.ModifiedTime); + item.Attributes = *p++; + item.Level = *p++; + if (item.Level > 2) + return S_FALSE; + UInt32 headerSize; + if (item.Level < 2) + { + headerSize = startHeader[0]; + if (headerSize < kBasicPartSize) + return S_FALSE; + UInt32 remain = headerSize - kBasicPartSize; + RINOK(CheckReadBytes(header + kBasicPartSize, remain)); + if (startHeader[1] != CalcSum(header, headerSize)) + return S_FALSE; + size_t nameLength = *p++; + if ((p - header) + nameLength + 2 > headerSize) + return S_FALSE; + p = ReadString(p, nameLength, item.Name); + } + else + headerSize = startHeader[0] | ((UInt32)startHeader[1] << 8); + p = ReadUInt16(p, item.CRC); + if (item.Level != 0) + { + if (item.Level == 2) + { + RINOK(CheckReadBytes(header + kBasicPartSize, 2)); + } + if ((size_t)(p - header) + 3 > headerSize) + return S_FALSE; + item.OsId = *p++; + UInt16 nextSize; + p = ReadUInt16(p, nextSize); + while (nextSize != 0) + { + if (nextSize < 3) + return S_FALSE; + if (item.Level == 1) + { + if (item.PackSize < nextSize) + return S_FALSE; + item.PackSize -= nextSize; + } + CExtension ext; + RINOK(CheckReadBytes(&ext.Type, 1)) + nextSize -= 3; + ext.Data.SetCapacity(nextSize); + RINOK(CheckReadBytes((Byte *)ext.Data, nextSize)) + item.Extensions.Add(ext); + Byte hdr2[2]; + RINOK(CheckReadBytes(hdr2, 2)); + ReadUInt16(hdr2, nextSize); + } + } + item.DataPosition = m_Position; + filled = true; + return S_OK; +} + +HRESULT CInArchive::Skeep(UInt64 numBytes) +{ + UInt64 newPostion; + RINOK(m_Stream->Seek(numBytes, STREAM_SEEK_CUR, &newPostion)); + m_Position += numBytes; + if (m_Position != newPostion) + return E_FAIL; + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhIn.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,29 @@ +// Archive/LzhIn.h + +#ifndef __ARCHIVE_LZHIN_H +#define __ARCHIVE_LZHIN_H + +#include "Common/MyCom.h" +#include "../../IStream.h" + +#include "LzhItem.h" + +namespace NArchive { +namespace NLzh { + +class CInArchive +{ + CMyComPtr m_Stream; + UInt64 m_Position; + + HRESULT ReadBytes(void *data, UInt32 size, UInt32 &processedSize); + HRESULT CheckReadBytes(void *data, UInt32 size); +public: + HRESULT Open(IInStream *inStream); + HRESULT GetNextItem(bool &filled, CItemEx &itemInfo); + HRESULT Skeep(UInt64 numBytes); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhItem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhItem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,172 @@ +// Archive/LzhItem.h + +#ifndef __ARCHIVE_LZH_ITEM_H +#define __ARCHIVE_LZH_ITEM_H + +#include "Common/Types.h" +#include "Common/MyString.h" +#include "Common/Buffer.h" +#include "LzhHeader.h" + +namespace NArchive { +namespace NLzh { + +struct CExtension +{ + Byte Type; + CByteBuffer Data; + AString GetString() const + { + AString s; + for (size_t i = 0; i < Data.GetCapacity(); i++) + { + char c = (char)Data[i]; + if (c == 0) + break; + s += c; + } + return s; + } +}; + +struct CItem +{ +public: + AString Name; + Byte Method[kMethodIdSize]; + UInt32 PackSize; + UInt32 Size; + UInt32 ModifiedTime; + Byte Attributes; + Byte Level; + UInt16 CRC; + Byte OsId; + CObjectVector Extensions; + + bool IsValidMethod() const { return (Method[0] == '-' && Method[1] == 'l' && Method[4] == '-'); } + bool IsLhMethod() const {return (IsValidMethod() && Method[2] == 'h'); } + bool IsDir() const {return (IsLhMethod() && Method[3] == 'd'); } + + bool IsCopyMethod() const + { + return (IsLhMethod() && Method[3] == '0') || + (IsValidMethod() && Method[2] == 'z' && Method[3] == '4'); + } + + bool IsLh1GroupMethod() const + { + if (!IsLhMethod()) + return false; + switch(Method[3]) + { + case '1': + return true; + } + return false; + } + + bool IsLh4GroupMethod() const + { + if (!IsLhMethod()) + return false; + switch(Method[3]) + { + case '4': + case '5': + case '6': + case '7': + return true; + } + return false; + } + + int GetNumDictBits() const + { + if (!IsLhMethod()) + return 0; + switch(Method[3]) + { + case '1': + return 12; + case '2': + return 13; + case '3': + return 13; + case '4': + return 12; + case '5': + return 13; + case '6': + return 15; + case '7': + return 16; + } + return 0; + } + + int FindExt(Byte type) const + { + for (int i = 0; i < Extensions.Size(); i++) + if (Extensions[i].Type == type) + return i; + return -1; + } + bool GetUnixTime(UInt32 &value) const + { + int index = FindExt(kExtIdUnixTime); + if (index < 0) + { + if (Level == 2) + { + value = ModifiedTime; + return true; + } + return false; + } + const Byte *data = (const Byte *)(Extensions[index].Data); + value = data[0] | + ((UInt32)data[1] << 8) | + ((UInt32)data[2] << 16) | + ((UInt32)data[3] << 24); + return true; + } + + AString GetDirName() const + { + int index = FindExt(kExtIdDirName); + if (index < 0) + return AString(); + return Extensions[index].GetString(); + } + + AString GetFileName() const + { + int index = FindExt(kExtIdFileName); + if (index < 0) + return Name; + return Extensions[index].GetString(); + } + + AString GetName() const + { + AString dirName = GetDirName(); + dirName.Replace((char)(unsigned char)0xFF, '\\'); + if (!dirName.IsEmpty()) + { + char c = dirName[dirName.Length() - 1]; + if (c != '\\') + dirName += '\\'; + } + return dirName + GetFileName(); + } +}; + +class CItemEx: public CItem +{ +public: + UInt64 DataPosition; +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhOutStreamWithCRC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhOutStreamWithCRC.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,27 @@ +// LzhOutStreamWithCRC.cpp + +#include "StdAfx.h" + +#include "LzhOutStreamWithCRC.h" + +namespace NArchive { +namespace NLzh { + +STDMETHODIMP COutStreamWithCRC::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize; + HRESULT result; + if(!_stream) + { + realProcessedSize = size; + result = S_OK; + } + else + result = _stream->Write(data, size, &realProcessedSize); + _crc.Update(data, realProcessedSize); + if(processedSize != NULL) + *processedSize = realProcessedSize; + return result; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhOutStreamWithCRC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhOutStreamWithCRC.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,38 @@ +// LzhOutStreamWithCRC.h + +#ifndef __LZHOUTSTREAMWITHCRC_H +#define __LZHOUTSTREAMWITHCRC_H + +#include "LzhCRC.h" +#include "../../../Common/MyCom.h" +#include "../../IStream.h" + +namespace NArchive { +namespace NLzh { + +class COutStreamWithCRC: + public ISequentialOutStream, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); +private: + CCRC _crc; + CMyComPtr _stream; +public: + void Init(ISequentialOutStream *stream) + { + _stream = stream; + _crc.Init(); + } + void ReleaseStream() { _stream.Release(); } + UInt32 GetCRC() const { return _crc.GetDigest(); } + void InitCRC() { _crc.Init(); } + +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/LzhRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,13 @@ +// LzhRegister.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "LzhHandler.h" +static IInArchive *CreateArc() { return new NArchive::NLzh::CHandler; } + +static CArcInfo g_ArcInfo = + { L"Lzh", L"lzh lha", 0, 6, { '-', 'l' }, 2, false, CreateArc, 0 }; + +REGISTER_ARC(Lzh) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzh/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzh/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaArcRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaArcRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +// LzmaArcRegister.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "LzmaHandler.h" + +static IInArchive *CreateArc() { return new NArchive::NLzma::CHandler; } + +static CArcInfo g_ArcInfo = + { L"Lzma", L"lzma lzma86", 0, 0xA, {0 }, 0, true, CreateArc, NULL }; + +REGISTER_ARC(Lzma) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaFiltersDecode.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaFiltersDecode.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,86 @@ +// LzmaFiltersDecode.cpp + +#include "StdAfx.h" + +#include "LzmaFiltersDecode.h" + +namespace NArchive { +namespace NLzma { + +static const UInt64 k_LZMA = 0x030101; +static const UInt64 k_BCJ = 0x03030103; + +HRESULT CDecoder::Code( + DECL_EXTERNAL_CODECS_LOC_VARS + const CHeader &block, + ISequentialInStream *inStream, ISequentialOutStream *outStream, + UInt64 *inProcessedSize, ICompressProgressInfo *progress) +{ + *inProcessedSize = (UInt64)(Int64)-1; + + if (block.FilterMethod > 1) + return E_NOTIMPL; + + if (!_lzmaDecoder) + { + RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS k_LZMA, _lzmaDecoder, false)); + if (_lzmaDecoder == 0) + return E_NOTIMPL; + } + + { + CMyComPtr setDecoderProperties; + _lzmaDecoder.QueryInterface(IID_ICompressSetDecoderProperties2, &setDecoderProperties); + if (!setDecoderProperties) + return E_NOTIMPL; + RINOK(setDecoderProperties->SetDecoderProperties2(block.LzmaProps, 5)); + } + + bool filteredMode = (block.FilterMethod == 1); + + CMyComPtr setOutStream; + + if (filteredMode) + { + if (!_bcjStream) + { + CMyComPtr coder; + RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS k_BCJ, coder, false)); + if (!coder) + return E_NOTIMPL; + coder.QueryInterface(IID_ISequentialOutStream, &_bcjStream); + if (!_bcjStream) + return E_NOTIMPL; + } + + _bcjStream.QueryInterface(IID_ICompressSetOutStream, &setOutStream); + if (!setOutStream) + return E_NOTIMPL; + RINOK(setOutStream->SetOutStream(outStream)); + outStream = _bcjStream; + } + + const UInt64 *unpackSize = block.HasUnpackSize() ? &block.UnpackSize : NULL; + RINOK(_lzmaDecoder->Code(inStream, outStream, NULL, unpackSize, progress)); + + if (filteredMode) + { + CMyComPtr flush; + _bcjStream.QueryInterface(IID_IOutStreamFlush, &flush); + if (flush) + { + RINOK(flush->Flush()); + } + RINOK(setOutStream->ReleaseOutStream()); + } + + CMyComPtr getInStreamProcessedSize; + _lzmaDecoder.QueryInterface(IID_ICompressGetInStreamProcessedSize, &getInStreamProcessedSize); + if (getInStreamProcessedSize) + { + RINOK(getInStreamProcessedSize->GetInStreamProcessedSize(inProcessedSize)); + } + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaFiltersDecode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaFiltersDecode.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,26 @@ +// LzmaFiltersDecode.h + +#ifndef __LZMA_FILTERS_DECODE_H +#define __LZMA_FILTERS_DECODE_H + +#include "../../Common/CreateCoder.h" + +#include "LzmaItem.h" + +namespace NArchive { +namespace NLzma { + +class CDecoder +{ + CMyComPtr _lzmaDecoder; + CMyComPtr _bcjStream; +public: + HRESULT Code(DECL_EXTERNAL_CODECS_LOC_VARS + const CHeader &block, + ISequentialInStream *inStream, ISequentialOutStream *outStream, + UInt64 *inProcessedSize, ICompressProgressInfo *progress); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,243 @@ +// LzmaHandler.cpp + +#include "StdAfx.h" + +#include "LzmaHandler.h" + +#include "Common/Defs.h" +#include "Common/StringConvert.h" +#include "Common/ComTry.h" +#include "Common/IntToString.h" + +#include "Windows/PropVariant.h" + +#include "../../Common/ProgressUtils.h" +#include "../../Common/StreamUtils.h" +#include "../Common/DummyOutStream.h" + +#include "LzmaFiltersDecode.h" + +namespace NArchive { +namespace NLzma { + +STATPROPSTG kProps[] = +{ + { NULL, kpidSize, VT_UI8}, + { NULL, kpidPackSize, VT_UI8}, + { NULL, kpidMethod, VT_UI1} +}; + +IMP_IInArchive_Props +IMP_IInArchive_ArcProps_NO + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = 1; + return S_OK; +} + +static void ConvertUInt32ToString(UInt32 value, wchar_t *s) +{ + ConvertUInt64ToString(value, s + MyStringLen(s)); +} + +static void DictSizeToString(UInt32 value, wchar_t *s) +{ + for (int i = 0; i <= 31; i++) + if ((UInt32(1) << i) == value) + { + ConvertUInt32ToString(i, s); + return; + } + wchar_t c = L'b'; + if ((value & ((1 << 20) - 1)) == 0) + { + value >>= 20; + c = L'm'; + } + else if ((value & ((1 << 10) - 1)) == 0) + { + value >>= 10; + c = L'k'; + } + ConvertUInt32ToString(value, s); + int p = MyStringLen(s); + s[p++] = c; + s[p++] = L'\0'; +} + +static void MyStrCat(wchar_t *d, const wchar_t *s) +{ + MyStringCopy(d + MyStringLen(d), s); +} + +STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value) +{ + if (index != 0) + return E_INVALIDARG; + NWindows::NCOM::CPropVariant propVariant; + switch(propID) + { + case kpidSize: + if (m_StreamInfo.HasUnpackSize()) + propVariant = (UInt64)m_StreamInfo.UnpackSize; + break; + case kpidPackSize: + propVariant = (UInt64)m_PackSize; + break; + case kpidMethod: + { + wchar_t s[64]; + s[0] = '\0'; + if (m_StreamInfo.IsThereFilter) + { + const wchar_t *f; + if (m_StreamInfo.FilterMethod == 0) + f = L"Copy"; + else if (m_StreamInfo.FilterMethod == 1) + f = L"BCJ"; + else + f = L"Unknown"; + MyStrCat(s, f); + MyStrCat(s, L" "); + } + MyStrCat(s, L"LZMA:"); + DictSizeToString(m_StreamInfo.GetDicSize(), s); + propVariant = s; + break; + } + } + propVariant.Detach(value); + return S_OK; +} + +STDMETHODIMP CHandler::Open(IInStream *inStream, + const UInt64 * /* maxCheckStartPosition */, + IArchiveOpenCallback * /* openArchiveCallback */) +{ + { + RINOK(inStream->Seek(0, STREAM_SEEK_CUR, &m_StreamStartPosition)); + + HRESULT res = ReadStreamHeader(inStream, m_StreamInfo); + if (res != S_OK) + return S_FALSE; + + Byte b; + RINOK(ReadStream_FALSE(inStream, &b, 1)); + if (b != 0) + return S_FALSE; + + UInt64 endPos; + RINOK(inStream->Seek(0, STREAM_SEEK_END, &endPos)); + m_PackSize = endPos - m_StreamStartPosition - m_StreamInfo.GetHeaderSize(); + + m_Stream = inStream; + } + return S_OK; +} + +STDMETHODIMP CHandler::Close() +{ + m_Stream.Release(); + return S_OK; +} + + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 _aTestMode, IArchiveExtractCallback *extractCallback) +{ + COM_TRY_BEGIN + bool allFilesMode = (numItems == UInt32(-1)); + if (!allFilesMode) + { + if (numItems == 0) + return S_OK; + if (numItems != 1) + return E_INVALIDARG; + if (indices[0] != 0) + return E_INVALIDARG; + } + + bool testMode = (_aTestMode != 0); + + RINOK(extractCallback->SetTotal(m_PackSize)); + + UInt64 currentTotalPacked = 0; + + CDummyOutStream *outStreamSpec = new CDummyOutStream; + CMyComPtr outStream(outStreamSpec); + + { + CMyComPtr realOutStream; + Int32 askMode = testMode ? + NArchive::NExtract::NAskMode::kTest : + NArchive::NExtract::NAskMode::kExtract; + + RINOK(extractCallback->GetStream(0, &realOutStream, askMode)); + + outStreamSpec->SetStream(realOutStream); + outStreamSpec->Init(); + if(!testMode && !realOutStream) + return S_OK; + extractCallback->PrepareOperation(askMode); + } + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, true); + + CDecoder decoder; + RINOK(m_Stream->Seek(m_StreamStartPosition, STREAM_SEEK_SET, NULL)); + UInt64 streamPos = m_StreamStartPosition; + Int32 opRes = NArchive::NExtract::NOperationResult::kOK; + bool firstItem = true; + for (;;) + { + CHeader st; + HRESULT result = ReadStreamHeader(m_Stream, st); + if (result != S_OK) + { + if (firstItem) + return E_FAIL; + break; + } + firstItem = false; + + lps->OutSize = outStreamSpec->GetSize(); + lps->InSize = currentTotalPacked; + RINOK(lps->SetCur()); + + streamPos += st.GetHeaderSize(); + UInt64 packProcessed; + + { + result = decoder.Code( + EXTERNAL_CODECS_VARS + st, m_Stream, outStream, &packProcessed, progress); + if (result == E_NOTIMPL) + { + opRes = NArchive::NExtract::NOperationResult::kUnSupportedMethod; + break; + } + if (result == S_FALSE) + { + opRes = NArchive::NExtract::NOperationResult::kDataError; + break; + } + RINOK(result); + } + + if (packProcessed == (UInt64)(Int64)-1) + break; + RINOK(m_Stream->Seek(streamPos + packProcessed, STREAM_SEEK_SET, NULL)); + currentTotalPacked += packProcessed; + streamPos += packProcessed; + } + outStream.Release(); + return extractCallback->SetOperationResult(opRes); + COM_TRY_END +} + +IMPL_ISetCompressCodecsInfo + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,69 @@ +// Lzma/Handler.h + +#ifndef __GZIP_HANDLER_H +#define __GZIP_HANDLER_H + +#include "Common/MyCom.h" + +#include "../IArchive.h" +#include "../../Common/CreateCoder.h" + +#include "LzmaIn.h" + +namespace NArchive { +namespace NLzma { + +// const UInt64 k_LZMA = 0x030101; + +class CHandler: + public IInArchive, + PUBLIC_ISetCompressCodecsInfo + public CMyUnknownImp +{ +public: + MY_QUERYINTERFACE_BEGIN + MY_QUERYINTERFACE_ENTRY(IInArchive) + QUERY_ENTRY_ISetCompressCodecsInfo + MY_QUERYINTERFACE_END + MY_ADDREF_RELEASE + + STDMETHOD(Open)(IInStream *inStream, + const UInt64 *maxCheckStartPosition, + IArchiveOpenCallback *openArchiveCallback); + STDMETHOD(Close)(); + + STDMETHOD(GetNumberOfItems)(UInt32 *numItems); + STDMETHOD(GetProperty)(UInt32 index, PROPID propID, PROPVARIANT *value); + STDMETHOD(Extract)(const UInt32* indices, UInt32 numItems, + Int32 testMode, IArchiveExtractCallback *extractCallback); + + STDMETHOD(GetArchiveProperty)(PROPID propID, PROPVARIANT *value); + + STDMETHOD(GetNumberOfProperties)(UInt32 *numProperties); + STDMETHOD(GetPropertyInfo)(UInt32 index, + BSTR *name, PROPID *propID, VARTYPE *varType); + + STDMETHOD(GetNumberOfArchiveProperties)(UInt32 *numProperties); + STDMETHOD(GetArchivePropertyInfo)(UInt32 index, + BSTR *name, PROPID *propID, VARTYPE *varType); + + UString GetMethodString(); +public: + CHandler() { } + +private: + CHeader m_StreamInfo; + UInt64 m_StreamStartPosition; + UInt64 m_PackSize; + + CMyComPtr m_Stream; + + DECL_EXTERNAL_CODECS_VARS + + DECL_ISetCompressCodecsInfo + +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaIn.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,56 @@ +// Archive/LzmaIn.cpp + +#include "StdAfx.h" + +#include "LzmaIn.h" + +#include "../../Common/StreamUtils.h" + +namespace NArchive { +namespace NLzma { + +static bool CheckDictSize(const Byte *p) +{ + UInt32 dicSize = GetUi32(p); + int i; + for (i = 1; i <= 30; i++) + if (dicSize == ((UInt32)2 << i) || dicSize == ((UInt32)3 << i)) + return true; + return false; +} + +HRESULT ReadStreamHeader(ISequentialInStream *inStream, CHeader &block) +{ + Byte sig[5 + 9]; + RINOK(ReadStream_FALSE(inStream, sig, 5 + 8)); + + const Byte kMaxProp0Val = 5 * 5 * 9 - 1; + if (sig[0] > kMaxProp0Val) + return S_FALSE; + + for (int i = 0; i < 5; i++) + block.LzmaProps[i] = sig[i]; + + block.IsThereFilter = false; + block.FilterMethod = 0; + + if (!CheckDictSize(sig + 1)) + { + if (sig[0] > 1 || sig[1] > kMaxProp0Val) + return S_FALSE; + block.IsThereFilter = true; + block.FilterMethod = sig[0]; + for (int i = 0; i < 5; i++) + block.LzmaProps[i] = sig[i + 1]; + if (!CheckDictSize(block.LzmaProps + 1)) + return S_FALSE; + RINOK(ReadStream_FALSE(inStream, sig + 5 + 8, 1)); + } + UInt32 unpOffset = 5 + (block.IsThereFilter ? 1 : 0); + block.UnpackSize = GetUi64(sig + unpOffset); + if (block.HasUnpackSize() && block.UnpackSize >= ((UInt64)1 << 56)) + return S_FALSE; + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaIn.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,16 @@ +// Archive/LzmaIn.h + +#ifndef __ARCHIVE_LZMA_IN_H +#define __ARCHIVE_LZMA_IN_H + +#include "LzmaItem.h" +#include "../../IStream.h" + +namespace NArchive { +namespace NLzma { + +HRESULT ReadStreamHeader(ISequentialInStream *inStream, CHeader &st); + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaItem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/LzmaItem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,27 @@ +// Archive/LzmaItem.h + +#ifndef __ARCHIVE_LZMA_ITEM_H +#define __ARCHIVE_LZMA_ITEM_H + +#include "Common/Types.h" + +#include "../../../../C/CpuArch.h" + +namespace NArchive { +namespace NLzma { + +struct CHeader +{ + UInt64 UnpackSize; + bool IsThereFilter; + Byte FilterMethod; + Byte LzmaProps[5]; + + UInt32 GetDicSize() const { return GetUi32(LzmaProps + 1); } + bool HasUnpackSize() const { return (UnpackSize != (UInt64)(Int64)-1); } + unsigned GetHeaderSize() const { return 5 + 8 + (IsThereFilter ? 1 : 0); } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Lzma/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Lzma/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,835 @@ +// RarHandler.cpp + +#include "StdAfx.h" + +#include "Common/ComTry.h" +#include "Common/IntToString.h" +#include "Common/StringConvert.h" + +#include "Windows/PropVariant.h" +#include "Windows/Time.h" + +#include "../../IPassword.h" + +#include "../../Common/CreateCoder.h" +#include "../../Common/FilterCoder.h" +#include "../../Common/MethodId.h" +#include "../../Common/ProgressUtils.h" + +#include "../../Compress/CopyCoder.h" + +#include "../../Crypto/Rar20Crypto.h" +#include "../../Crypto/RarAes.h" + +#include "../Common/ItemNameUtils.h" +#include "../Common/OutStreamWithCRC.h" + +#include "RarHandler.h" + +using namespace NWindows; +using namespace NTime; + +namespace NArchive { +namespace NRar { + +static const wchar_t *kHostOS[] = +{ + L"MS DOS", + L"OS/2", + L"Win32", + L"Unix", + L"Mac OS", + L"BeOS" +}; + +static const int kNumHostOSes = sizeof(kHostOS) / sizeof(kHostOS[0]); + +static const wchar_t *kUnknownOS = L"Unknown"; + +STATPROPSTG kProps[] = +{ + { NULL, kpidPath, VT_BSTR}, + { NULL, kpidIsDir, VT_BOOL}, + { NULL, kpidSize, VT_UI8}, + { NULL, kpidPackSize, VT_UI8}, + { NULL, kpidMTime, VT_FILETIME}, + { NULL, kpidCTime, VT_FILETIME}, + { NULL, kpidATime, VT_FILETIME}, + { NULL, kpidAttrib, VT_UI4}, + + { NULL, kpidEncrypted, VT_BOOL}, + { NULL, kpidSolid, VT_BOOL}, + { NULL, kpidCommented, VT_BOOL}, + { NULL, kpidSplitBefore, VT_BOOL}, + { NULL, kpidSplitAfter, VT_BOOL}, + { NULL, kpidCRC, VT_UI4}, + { NULL, kpidHostOS, VT_BSTR}, + { NULL, kpidMethod, VT_BSTR}, + { NULL, kpidUnpackVer, VT_UI1} +}; + +STATPROPSTG kArcProps[] = +{ + { NULL, kpidSolid, VT_BOOL}, + { NULL, kpidNumBlocks, VT_UI4}, + // { NULL, kpidEncrypted, VT_BOOL}, + { NULL, kpidIsVolume, VT_BOOL}, + { NULL, kpidNumVolumes, VT_UI4}, + { NULL, kpidPhySize, VT_UI8} + // { NULL, kpidCommented, VT_BOOL} +}; + +IMP_IInArchive_Props +IMP_IInArchive_ArcProps + +UInt64 CHandler::GetPackSize(int refIndex) const +{ + const CRefItem &refItem = _refItems[refIndex]; + UInt64 totalPackSize = 0; + for (int i = 0; i < refItem.NumItems; i++) + totalPackSize += _items[refItem.ItemIndex + i].PackSize; + return totalPackSize; +} + +STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value) +{ + // COM_TRY_BEGIN + NWindows::NCOM::CPropVariant prop; + switch(propID) + { + case kpidSolid: prop = _archiveInfo.IsSolid(); break; + // case kpidEncrypted: prop = _archiveInfo.IsEncrypted(); break; // it's for encrypted names. + case kpidIsVolume: prop = _archiveInfo.IsVolume(); break; + case kpidNumVolumes: prop = (UInt32)_archives.Size(); break; + case kpidOffset: if (_archiveInfo.StartPosition != 0) prop = _archiveInfo.StartPosition; break; + // case kpidCommented: prop = _archiveInfo.IsCommented(); break; + case kpidNumBlocks: + { + UInt32 numBlocks = 0; + for (int i = 0; i < _refItems.Size(); i++) + if (!IsSolid(i)) + numBlocks++; + prop = (UInt32)numBlocks; + break; + } + } + prop.Detach(value); + return S_OK; + // COM_TRY_END +} + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = _refItems.Size(); + return S_OK; +} + +static bool RarTimeToFileTime(const CRarTime &rarTime, FILETIME &result) +{ + if (!DosTimeToFileTime(rarTime.DosTime, result)) + return false; + UInt64 value = (((UInt64)result.dwHighDateTime) << 32) + result.dwLowDateTime; + value += (UInt64)rarTime.LowSecond * 10000000; + value += ((UInt64)rarTime.SubTime[2] << 16) + + ((UInt64)rarTime.SubTime[1] << 8) + + ((UInt64)rarTime.SubTime[0]); + result.dwLowDateTime = (DWORD)value; + result.dwHighDateTime = DWORD(value >> 32); + return true; +} + +static void RarTimeToProp(const CRarTime &rarTime, NWindows::NCOM::CPropVariant &prop) +{ + FILETIME localFileTime, utcFileTime; + if (RarTimeToFileTime(rarTime, localFileTime)) + { + if (!LocalFileTimeToFileTime(&localFileTime, &utcFileTime)) + utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0; + } + else + utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0; + prop = utcFileTime; +} + +STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value) +{ + COM_TRY_BEGIN + NWindows::NCOM::CPropVariant prop; + const CRefItem &refItem = _refItems[index]; + const CItemEx &item = _items[refItem.ItemIndex]; + switch(propID) + { + case kpidPath: + { + UString u; + if (item.HasUnicodeName() && !item.UnicodeName.IsEmpty()) + u = item.UnicodeName; + else + u = MultiByteToUnicodeString(item.Name, CP_OEMCP); + prop = (const wchar_t *)NItemName::WinNameToOSName(u); + break; + } + case kpidIsDir: prop = item.IsDir(); break; + case kpidSize: prop = item.Size; break; + case kpidPackSize: prop = GetPackSize(index); break; + case kpidMTime: RarTimeToProp(item.MTime, prop); break; + case kpidCTime: if (item.CTimeDefined) RarTimeToProp(item.CTime, prop); break; + case kpidATime: if (item.ATimeDefined) RarTimeToProp(item.ATime, prop); break; + case kpidAttrib: prop = item.GetWinAttributes(); break; + case kpidEncrypted: prop = item.IsEncrypted(); break; + case kpidSolid: prop = IsSolid(index); break; + case kpidCommented: prop = item.IsCommented(); break; + case kpidSplitBefore: prop = item.IsSplitBefore(); break; + case kpidSplitAfter: prop = _items[refItem.ItemIndex + refItem.NumItems - 1].IsSplitAfter(); break; + case kpidCRC: + { + const CItemEx &lastItem = _items[refItem.ItemIndex + refItem.NumItems - 1]; + prop = ((lastItem.IsSplitAfter()) ? item.FileCRC : lastItem.FileCRC); + break; + } + case kpidUnpackVer: prop = item.UnPackVersion; break; + case kpidMethod: + { + UString method; + if (item.Method >= Byte('0') && item.Method <= Byte('5')) + { + method = L"m"; + wchar_t temp[32]; + ConvertUInt64ToString(item.Method - Byte('0'), temp); + method += temp; + if (!item.IsDir()) + { + method += L":"; + ConvertUInt64ToString(16 + item.GetDictSize(), temp); + method += temp; + } + } + else + { + wchar_t temp[32]; + ConvertUInt64ToString(item.Method, temp); + method += temp; + } + prop = method; + break; + } + case kpidHostOS: prop = (item.HostOS < kNumHostOSes) ? (kHostOS[item.HostOS]) : kUnknownOS; break; + } + prop.Detach(value); + return S_OK; + COM_TRY_END +} + +class CVolumeName +{ + bool _first; + bool _newStyle; + UString _unchangedPart; + UString _changedPart; + UString _afterPart; +public: + CVolumeName(): _newStyle(true) {}; + + bool InitName(const UString &name, bool newStyle) + { + _first = true; + _newStyle = newStyle; + int dotPos = name.ReverseFind('.'); + UString basePart = name; + if (dotPos >= 0) + { + UString ext = name.Mid(dotPos + 1); + if (ext.CompareNoCase(L"rar") == 0) + { + _afterPart = name.Mid(dotPos); + basePart = name.Left(dotPos); + } + else if (ext.CompareNoCase(L"exe") == 0) + { + _afterPart = L".rar"; + basePart = name.Left(dotPos); + } + else if (!_newStyle) + { + if (ext.CompareNoCase(L"000") == 0 || ext.CompareNoCase(L"001") == 0) + { + _afterPart.Empty(); + _first = false; + _changedPart = ext; + _unchangedPart = name.Left(dotPos + 1); + return true; + } + } + } + + if (!_newStyle) + { + _afterPart.Empty(); + _unchangedPart = basePart + UString(L"."); + _changedPart = L"r00"; + return true; + } + + int numLetters = 1; + if (basePart.Right(numLetters) == L"1" || basePart.Right(numLetters) == L"0") + { + while (numLetters < basePart.Length()) + { + if (basePart[basePart.Length() - numLetters - 1] != '0') + break; + numLetters++; + } + } + else + return false; + _unchangedPart = basePart.Left(basePart.Length() - numLetters); + _changedPart = basePart.Right(numLetters); + return true; + } + + UString GetNextName() + { + UString newName; + if (_newStyle || !_first) + { + int i; + int numLetters = _changedPart.Length(); + for (i = numLetters - 1; i >= 0; i--) + { + wchar_t c = _changedPart[i]; + if (c == L'9') + { + c = L'0'; + newName = c + newName; + if (i == 0) + newName = UString(L'1') + newName; + continue; + } + c++; + newName = UString(c) + newName; + i--; + for (; i >= 0; i--) + newName = _changedPart[i] + newName; + break; + } + _changedPart = newName; + } + _first = false; + return _unchangedPart + _changedPart + _afterPart; + } +}; + +HRESULT CHandler::Open2(IInStream *stream, + const UInt64 *maxCheckStartPosition, + IArchiveOpenCallback *openArchiveCallback) +{ + { + CMyComPtr openVolumeCallback; + CMyComPtr getTextPassword; + CMyComPtr openArchiveCallbackWrap = openArchiveCallback; + + CVolumeName seqName; + + UInt64 totalBytes = 0; + UInt64 curBytes = 0; + + if (openArchiveCallback != NULL) + { + openArchiveCallbackWrap.QueryInterface(IID_IArchiveOpenVolumeCallback, &openVolumeCallback); + openArchiveCallbackWrap.QueryInterface(IID_ICryptoGetTextPassword, &getTextPassword); + } + + for (;;) + { + CMyComPtr inStream; + if (!_archives.IsEmpty()) + { + if (!openVolumeCallback) + break; + + if(_archives.Size() == 1) + { + if (!_archiveInfo.IsVolume()) + break; + UString baseName; + { + NCOM::CPropVariant prop; + RINOK(openVolumeCallback->GetProperty(kpidName, &prop)); + if (prop.vt != VT_BSTR) + break; + baseName = prop.bstrVal; + } + seqName.InitName(baseName, _archiveInfo.HaveNewVolumeName()); + } + + UString fullName = seqName.GetNextName(); + HRESULT result = openVolumeCallback->GetStream(fullName, &inStream); + if (result == S_FALSE) + break; + if (result != S_OK) + return result; + if (!stream) + break; + } + else + inStream = stream; + + UInt64 endPos = 0; + if (openArchiveCallback != NULL) + { + RINOK(stream->Seek(0, STREAM_SEEK_END, &endPos)); + RINOK(stream->Seek(0, STREAM_SEEK_SET, NULL)); + totalBytes += endPos; + RINOK(openArchiveCallback->SetTotal(NULL, &totalBytes)); + } + + NArchive::NRar::CInArchive archive; + RINOK(archive.Open(inStream, maxCheckStartPosition)); + + if (_archives.IsEmpty()) + archive.GetArchiveInfo(_archiveInfo); + + CItemEx item; + for (;;) + { + HRESULT result = archive.GetNextItem(item, getTextPassword); + if (result == S_FALSE) + break; + RINOK(result); + if (item.IgnoreItem()) + continue; + + bool needAdd = true; + if (item.IsSplitBefore()) + { + if (!_refItems.IsEmpty()) + { + CRefItem &refItem = _refItems.Back(); + refItem.NumItems++; + needAdd = false; + } + } + if (needAdd) + { + CRefItem refItem; + refItem.ItemIndex = _items.Size(); + refItem.NumItems = 1; + refItem.VolumeIndex = _archives.Size(); + _refItems.Add(refItem); + } + _items.Add(item); + if (openArchiveCallback != NULL && _items.Size() % 100 == 0) + { + UInt64 numFiles = _items.Size(); + UInt64 numBytes = curBytes + item.Position; + RINOK(openArchiveCallback->SetCompleted(&numFiles, &numBytes)); + } + } + curBytes += endPos; + _archives.Add(archive); + } + } + return S_OK; +} + +STDMETHODIMP CHandler::Open(IInStream *stream, + const UInt64 *maxCheckStartPosition, + IArchiveOpenCallback *openArchiveCallback) +{ + COM_TRY_BEGIN + Close(); + try + { + HRESULT res = Open2(stream, maxCheckStartPosition, openArchiveCallback); + if (res != S_OK) + Close(); + return res; + } + catch(const CInArchiveException &) { Close(); return S_FALSE; } + catch(...) { Close(); throw; } + COM_TRY_END +} + +STDMETHODIMP CHandler::Close() +{ + COM_TRY_BEGIN + _refItems.Clear(); + _items.Clear(); + _archives.Clear(); + return S_OK; + COM_TRY_END +} + +struct CMethodItem +{ + Byte RarUnPackVersion; + CMyComPtr Coder; +}; + + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 _aTestMode, IArchiveExtractCallback *_anExtractCallback) +{ + COM_TRY_BEGIN + CMyComPtr getTextPassword; + bool testMode = (_aTestMode != 0); + CMyComPtr extractCallback = _anExtractCallback; + UInt64 censoredTotalUnPacked = 0, + // censoredTotalPacked = 0, + importantTotalUnPacked = 0; + // importantTotalPacked = 0; + bool allFilesMode = (numItems == UInt32(-1)); + if (allFilesMode) + numItems = _refItems.Size(); + if(numItems == 0) + return S_OK; + int lastIndex = 0; + CRecordVector importantIndexes; + CRecordVector extractStatuses; + + for(UInt32 t = 0; t < numItems; t++) + { + int index = allFilesMode ? t : indices[t]; + const CRefItem &refItem = _refItems[index]; + const CItemEx &item = _items[refItem.ItemIndex]; + censoredTotalUnPacked += item.Size; + // censoredTotalPacked += item.PackSize; + int j; + for(j = lastIndex; j <= index; j++) + // if(!_items[_refItems[j].ItemIndex].IsSolid()) + if(!IsSolid(j)) + lastIndex = j; + for(j = lastIndex; j <= index; j++) + { + const CRefItem &refItem = _refItems[j]; + const CItemEx &item = _items[refItem.ItemIndex]; + + // const CItemEx &item = _items[j]; + + importantTotalUnPacked += item.Size; + // importantTotalPacked += item.PackSize; + importantIndexes.Add(j); + extractStatuses.Add(j == index); + } + lastIndex = index + 1; + } + + extractCallback->SetTotal(importantTotalUnPacked); + UInt64 currentImportantTotalUnPacked = 0; + UInt64 currentImportantTotalPacked = 0; + UInt64 currentUnPackSize, currentPackSize; + + CObjectVector methodItems; + + NCompress::CCopyCoder *copyCoderSpec = new NCompress::CCopyCoder; + CMyComPtr copyCoder = copyCoderSpec; + + CFilterCoder *filterStreamSpec = new CFilterCoder; + CMyComPtr filterStream = filterStreamSpec; + + NCrypto::NRar20::CDecoder *rar20CryptoDecoderSpec = NULL; + CMyComPtr rar20CryptoDecoder; + NCrypto::NRar29::CDecoder *rar29CryptoDecoderSpec = NULL; + CMyComPtr rar29CryptoDecoder; + + CFolderInStream *folderInStreamSpec = NULL; + CMyComPtr folderInStream; + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, false); + + bool solidStart = true; + for(int i = 0; i < importantIndexes.Size(); i++, + currentImportantTotalUnPacked += currentUnPackSize, + currentImportantTotalPacked += currentPackSize) + { + lps->InSize = currentImportantTotalPacked; + lps->OutSize = currentImportantTotalUnPacked; + RINOK(lps->SetCur()); + CMyComPtr realOutStream; + + Int32 askMode; + if(extractStatuses[i]) + askMode = testMode ? + NArchive::NExtract::NAskMode::kTest : + NArchive::NExtract::NAskMode::kExtract; + else + askMode = NArchive::NExtract::NAskMode::kSkip; + + UInt32 index = importantIndexes[i]; + + const CRefItem &refItem = _refItems[index]; + const CItemEx &item = _items[refItem.ItemIndex]; + + currentUnPackSize = item.Size; + + currentPackSize = GetPackSize(index); + + if(item.IgnoreItem()) + continue; + + RINOK(extractCallback->GetStream(index, &realOutStream, askMode)); + + if (!IsSolid(index)) + solidStart = true; + if(item.IsDir()) + { + RINOK(extractCallback->PrepareOperation(askMode)); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK)); + continue; + } + + bool mustBeProcessedAnywhere = false; + if(i < importantIndexes.Size() - 1) + { + // const CRefItem &nextRefItem = _refItems[importantIndexes[i + 1]]; + // const CItemEx &nextItemInfo = _items[nextRefItem.ItemIndex]; + // mustBeProcessedAnywhere = nextItemInfo.IsSolid(); + mustBeProcessedAnywhere = IsSolid(importantIndexes[i + 1]); + } + + if (!mustBeProcessedAnywhere && !testMode && !realOutStream) + continue; + + if (!realOutStream && !testMode) + askMode = NArchive::NExtract::NAskMode::kSkip; + + RINOK(extractCallback->PrepareOperation(askMode)); + + COutStreamWithCRC *outStreamSpec = new COutStreamWithCRC; + CMyComPtr outStream(outStreamSpec); + outStreamSpec->SetStream(realOutStream); + outStreamSpec->Init(); + realOutStream.Release(); + + /* + for (int partIndex = 0; partIndex < 1; partIndex++) + { + CMyComPtr inStream; + + // item redefinition + const CItemEx &item = _items[refItem.ItemIndex + partIndex]; + + NArchive::NRar::CInArchive &archive = _archives[refItem.VolumeIndex + partIndex]; + + inStream.Attach(archive.CreateLimitedStream(item.GetDataPosition(), + item.PackSize)); + */ + if (!folderInStream) + { + folderInStreamSpec = new CFolderInStream; + folderInStream = folderInStreamSpec; + } + + folderInStreamSpec->Init(&_archives, &_items, refItem); + + UInt64 packSize = currentPackSize; + + // packedPos += item.PackSize; + // unpackedPos += 0; + + CMyComPtr inStream; + if (item.IsEncrypted()) + { + CMyComPtr cryptoSetPassword; + if (item.UnPackVersion >= 29) + { + if (!rar29CryptoDecoder) + { + rar29CryptoDecoderSpec = new NCrypto::NRar29::CDecoder; + rar29CryptoDecoder = rar29CryptoDecoderSpec; + // RINOK(rar29CryptoDecoder.CoCreateInstance(CLSID_CCryptoRar29Decoder)); + } + rar29CryptoDecoderSpec->SetRar350Mode(item.UnPackVersion < 36); + CMyComPtr cryptoProperties; + RINOK(rar29CryptoDecoder.QueryInterface(IID_ICompressSetDecoderProperties2, + &cryptoProperties)); + RINOK(cryptoProperties->SetDecoderProperties2(item.Salt, item.HasSalt() ? sizeof(item.Salt) : 0)); + filterStreamSpec->Filter = rar29CryptoDecoder; + } + else if (item.UnPackVersion >= 20) + { + if (!rar20CryptoDecoder) + { + rar20CryptoDecoderSpec = new NCrypto::NRar20::CDecoder; + rar20CryptoDecoder = rar20CryptoDecoderSpec; + // RINOK(rar20CryptoDecoder.CoCreateInstance(CLSID_CCryptoRar20Decoder)); + } + filterStreamSpec->Filter = rar20CryptoDecoder; + } + else + { + outStream.Release(); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod)); + continue; + } + RINOK(filterStreamSpec->Filter.QueryInterface(IID_ICryptoSetPassword, + &cryptoSetPassword)); + + if (!getTextPassword) + extractCallback.QueryInterface(IID_ICryptoGetTextPassword, + &getTextPassword); + if (getTextPassword) + { + CMyComBSTR password; + RINOK(getTextPassword->CryptoGetTextPassword(&password)); + if (item.UnPackVersion >= 29) + { + CByteBuffer buffer; + UString unicodePassword(password); + const UInt32 sizeInBytes = unicodePassword.Length() * 2; + buffer.SetCapacity(sizeInBytes); + for (int i = 0; i < unicodePassword.Length(); i++) + { + wchar_t c = unicodePassword[i]; + ((Byte *)buffer)[i * 2] = (Byte)c; + ((Byte *)buffer)[i * 2 + 1] = (Byte)(c >> 8); + } + RINOK(cryptoSetPassword->CryptoSetPassword( + (const Byte *)buffer, sizeInBytes)); + } + else + { + AString oemPassword = UnicodeStringToMultiByte( + (const wchar_t *)password, CP_OEMCP); + RINOK(cryptoSetPassword->CryptoSetPassword( + (const Byte *)(const char *)oemPassword, oemPassword.Length())); + } + } + else + { + RINOK(cryptoSetPassword->CryptoSetPassword(0, 0)); + } + filterStreamSpec->SetInStream(folderInStream); + inStream = filterStream; + } + else + { + inStream = folderInStream; + } + CMyComPtr commonCoder; + switch(item.Method) + { + case '0': + { + commonCoder = copyCoder; + break; + } + case '1': + case '2': + case '3': + case '4': + case '5': + { + /* + if (item.UnPackVersion >= 29) + { + outStream.Release(); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod)); + continue; + } + */ + int m; + for (m = 0; m < methodItems.Size(); m++) + if (methodItems[m].RarUnPackVersion == item.UnPackVersion) + break; + if (m == methodItems.Size()) + { + CMethodItem mi; + mi.RarUnPackVersion = item.UnPackVersion; + + mi.Coder.Release(); + if (item.UnPackVersion <= 30) + { + UInt32 methodID = 0x040300; + if (item.UnPackVersion < 20) + methodID += 1; + else if (item.UnPackVersion < 29) + methodID += 2; + else + methodID += 3; + RINOK(CreateCoder(EXTERNAL_CODECS_VARS methodID, mi.Coder, false)); + } + + if (mi.Coder == 0) + { + outStream.Release(); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod)); + continue; + } + + m = methodItems.Add(mi); + } + CMyComPtr decoder = methodItems[m].Coder; + + CMyComPtr compressSetDecoderProperties; + RINOK(decoder.QueryInterface(IID_ICompressSetDecoderProperties2, + &compressSetDecoderProperties)); + + Byte isSolid = (Byte)((IsSolid(index) || item.IsSplitBefore()) ? 1: 0); + if (solidStart) + { + isSolid = false; + solidStart = false; + } + + + RINOK(compressSetDecoderProperties->SetDecoderProperties2(&isSolid, 1)); + + commonCoder = decoder; + break; + } + default: + outStream.Release(); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod)); + continue; + } + HRESULT result = commonCoder->Code(inStream, outStream, &packSize, &item.Size, progress); + if (item.IsEncrypted()) + filterStreamSpec->ReleaseInStream(); + if (result == S_FALSE) + { + outStream.Release(); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kDataError)); + continue; + } + if (result != S_OK) + return result; + + /* + if (refItem.NumItems == 1 && + !item.IsSplitBefore() && !item.IsSplitAfter()) + */ + { + const CItemEx &lastItem = _items[refItem.ItemIndex + refItem.NumItems - 1]; + bool crcOK = outStreamSpec->GetCRC() == lastItem.FileCRC; + outStream.Release(); + RINOK(extractCallback->SetOperationResult(crcOK ? NArchive::NExtract::NOperationResult::kOK: + NArchive::NExtract::NOperationResult::kCRCError)); + } + /* + else + { + bool crcOK = true; + for (int partIndex = 0; partIndex < refItem.NumItems; partIndex++) + { + const CItemEx &item = _items[refItem.ItemIndex + partIndex]; + if (item.FileCRC != folderInStreamSpec->CRCs[partIndex]) + { + crcOK = false; + break; + } + } + RINOK(extractCallback->SetOperationResult(crcOK ? NArchive::NExtract::NOperationResult::kOK: + NArchive::NExtract::NOperationResult::kCRCError)); + } + */ + } + return S_OK; + COM_TRY_END +} + +IMPL_ISetCompressCodecsInfo + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,60 @@ +// Rar/Handler.h + +#ifndef __RAR_HANDLER_H +#define __RAR_HANDLER_H + +#include "../IArchive.h" +#include "RarIn.h" +#include "RarVolumeInStream.h" + +#include "../../Common/CreateCoder.h" + +namespace NArchive { +namespace NRar { + +class CHandler: + public IInArchive, + PUBLIC_ISetCompressCodecsInfo + public CMyUnknownImp +{ +public: + MY_QUERYINTERFACE_BEGIN2(IInArchive) + QUERY_ENTRY_ISetCompressCodecsInfo + MY_QUERYINTERFACE_END + MY_ADDREF_RELEASE + + INTERFACE_IInArchive(;) + + DECL_ISetCompressCodecsInfo + +private: + CRecordVector _refItems; + CObjectVector _items; + CObjectVector _archives; + NArchive::NRar::CInArchiveInfo _archiveInfo; + + DECL_EXTERNAL_CODECS_VARS + + UInt64 GetPackSize(int refIndex) const; + // NArchive::NRar::CInArchive _archive; + + bool IsSolid(int refIndex) + { + const CItemEx &item = _items[_refItems[refIndex].ItemIndex]; + if (item.UnPackVersion < 20) + { + if (_archiveInfo.IsSolid()) + return (refIndex > 0); + return false; + } + return item.IsSolid(); + } + + HRESULT Open2(IInStream *stream, + const UInt64 *maxCheckStartPosition, + IArchiveOpenCallback *openArchiveCallback); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarHeader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarHeader.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,19 @@ +// Archive/Rar/Headers.cpp + +#include "StdAfx.h" + +#include "RarHeader.h" + +namespace NArchive{ +namespace NRar{ +namespace NHeader{ + +Byte kMarker[kMarkerSize] = {0x52 + 1, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00}; + +class CMarkerInitializer +{ +public: + CMarkerInitializer() { kMarker[0]--; }; +} g_MarkerInitializer; + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarHeader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarHeader.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,224 @@ +// Archive/RarHeader.h + +#ifndef __ARCHIVE_RAR_HEADER_H +#define __ARCHIVE_RAR_HEADER_H + +#include "Common/Types.h" + +namespace NArchive{ +namespace NRar{ +namespace NHeader{ + +const int kMarkerSize = 7; +extern Byte kMarker[kMarkerSize]; + +const int kArchiveSolid = 0x1; + +namespace NBlockType +{ + enum EBlockType + { + kMarker = 0x72, + kArchiveHeader = 0x73, + kFileHeader = 0x74, + kCommentHeader = 0x75, + kOldAuthenticity = 0x76, + kSubBlock = 0x77, + kRecoveryRecord = 0x78, + kAuthenticity = 0x79, + + kEndOfArchive = 0x7B // Is not safe + }; +} + +namespace NArchive +{ + const UInt16 kVolume = 1; + const UInt16 kComment = 2; + const UInt16 kLock = 4; + const UInt16 kSolid = 8; + const UInt16 kNewVolName = 0x10; // ('volname.partN.rar') + const UInt16 kAuthenticity = 0x20; + const UInt16 kRecovery = 0x40; + const UInt16 kBlockEncryption = 0x80; + const UInt16 kFirstVolume = 0x100; // (set only by RAR 3.0 and later) + const UInt16 kEncryptVer = 0x200; // RAR 3.6 there is EncryptVer Byte in End of MainHeader + + const int kHeaderSizeMin = 7; + + struct CBlock + { + UInt16 CRC; + Byte Type; + UInt16 Flags; + UInt16 Size; + UInt16 Reserved1; + UInt32 Reserved2; + // UInt16 GetRealCRC() const; + }; + + const int kArchiveHeaderSize = 13; + + const int kBlockHeadersAreEncrypted = 0x80; + + struct CHeader360: public CBlock + { + Byte EncryptVersion; + bool IsEncrypted() const { return (Flags & NHeader::NArchive::kBlockEncryption) != 0; } + bool IsThereEncryptVer() const { return (Flags & NHeader::NArchive::kEncryptVer) != 0; } + bool IsEncryptOld() const { return (!IsThereEncryptVer() || EncryptVersion < 36); } + UInt32 GetBaseSize() const { return kArchiveHeaderSize + (IsEncryptOld() ? 0 : 1); } + }; +} + +namespace NFile +{ + const int kSplitBefore = 1 << 0; + const int kSplitAfter = 1 << 1; + const int kEncrypted = 1 << 2; + const int kComment = 1 << 3; + const int kSolid = 1 << 4; + + const int kDictBitStart = 5; + const int kNumDictBits = 3; + const int kDictMask = (1 << kNumDictBits) - 1; + const int kDictDirectoryValue = 0x7; + + const int kSize64Bits = 1 << 8; + const int kUnicodeName = 1 << 9; + const int kSalt = 1 << 10; + const int kOldVersion = 1 << 11; + const int kExtTime = 1 << 12; + // const int kExtFlags = 1 << 13; + // const int kSkipIfUnknown = 1 << 14; + + const int kLongBlock = 1 << 15; + + /* + struct CBlock + { + // UInt16 HeadCRC; + // Byte Type; + // UInt16 Flags; + // UInt16 HeadSize; + UInt32 PackSize; + UInt32 UnPackSize; + Byte HostOS; + UInt32 FileCRC; + UInt32 Time; + Byte UnPackVersion; + Byte Method; + UInt16 NameSize; + UInt32 Attributes; + }; + */ + + /* + struct CBlock32 + { + UInt16 HeadCRC; + Byte Type; + UInt16 Flags; + UInt16 HeadSize; + UInt32 PackSize; + UInt32 UnPackSize; + Byte HostOS; + UInt32 FileCRC; + UInt32 Time; + Byte UnPackVersion; + Byte Method; + UInt16 NameSize; + UInt32 Attributes; + UInt16 GetRealCRC(const void *aName, UInt32 aNameSize, + bool anExtraDataDefined = false, Byte *anExtraData = 0) const; + }; + struct CBlock64 + { + UInt16 HeadCRC; + Byte Type; + UInt16 Flags; + UInt16 HeadSize; + UInt32 PackSizeLow; + UInt32 UnPackSizeLow; + Byte HostOS; + UInt32 FileCRC; + UInt32 Time; + Byte UnPackVersion; + Byte Method; + UInt16 NameSize; + UInt32 Attributes; + UInt32 PackSizeHigh; + UInt32 UnPackSizeHigh; + UInt16 GetRealCRC(const void *aName, UInt32 aNameSize) const; + }; + */ + + const int kLabelFileAttribute = 0x08; + const int kWinFileDirectoryAttributeMask = 0x10; + + enum CHostOS + { + kHostMSDOS = 0, + kHostOS2 = 1, + kHostWin32 = 2, + kHostUnix = 3, + kHostMacOS = 4, + kHostBeOS = 5 + }; +} + +namespace NBlock +{ + const UInt16 kLongBlock = 1 << 15; + struct CBlock + { + UInt16 CRC; + Byte Type; + UInt16 Flags; + UInt16 HeadSize; + // UInt32 DataSize; + }; +} + +/* +struct CSubBlock +{ + UInt16 HeadCRC; + Byte HeadType; + UInt16 Flags; + UInt16 HeadSize; + UInt32 DataSize; + UInt16 SubType; + Byte Level; // Reserved : Must be 0 +}; + +struct CCommentBlock +{ + UInt16 HeadCRC; + Byte HeadType; + UInt16 Flags; + UInt16 HeadSize; + UInt16 UnpSize; + Byte UnpVer; + Byte Method; + UInt16 CommCRC; +}; + + +struct CProtectHeader +{ + UInt16 HeadCRC; + Byte HeadType; + UInt16 Flags; + UInt16 HeadSize; + UInt32 DataSize; + Byte Version; + UInt16 RecSectors; + UInt32 TotalBlocks; + Byte Mark[8]; +}; +*/ + +}}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarIn.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,513 @@ +// Archive/RarIn.cpp + +#include "StdAfx.h" + +#include "Common/StringConvert.h" +#include "Common/UTFConvert.h" + +#include "RarIn.h" +#include "../../Common/LimitedStreams.h" +#include "../../Common/StreamUtils.h" + +#include "../Common/FindSignature.h" + +extern "C" +{ + #include "../../../../C/7zCrc.h" +} + +namespace NArchive { +namespace NRar { + +void CInArchive::ThrowExceptionWithCode( + CInArchiveException::CCauseType cause) +{ + throw CInArchiveException(cause); +} + +HRESULT CInArchive::Open(IInStream *inStream, const UInt64 *searchHeaderSizeLimit) +{ + try + { + Close(); + HRESULT res = Open2(inStream, searchHeaderSizeLimit); + if (res == S_OK) + return res; + Close(); + return res; + } + catch(...) { Close(); throw; } +} + +void CInArchive::Close() +{ + m_Stream.Release(); +} + + +static inline bool TestMarkerCandidate(const void *aTestBytes) +{ + for (UInt32 i = 0; i < NHeader::kMarkerSize; i++) + if (((const Byte *)aTestBytes)[i] != NHeader::kMarker[i]) + return false; + return true; +} + +HRESULT CInArchive::FindAndReadMarker(IInStream *stream, const UInt64 *searchHeaderSizeLimit) +{ + RINOK(FindSignatureInStream(stream, + NHeader::kMarker, NHeader::kMarkerSize, + searchHeaderSizeLimit, m_ArchiveStartPosition)); + m_Stream = stream; + m_Position = m_ArchiveStartPosition + NHeader::kMarkerSize; + return m_Stream->Seek(m_Position, STREAM_SEEK_SET, NULL); +} + +void CInArchive::ThrowUnexpectedEndOfArchiveException() +{ + ThrowExceptionWithCode(CInArchiveException::kUnexpectedEndOfArchive); +} + +bool CInArchive::ReadBytesAndTestSize(void *data, UInt32 size) +{ + if (m_CryptoMode) + { + const Byte *bufData = (const Byte *)m_DecryptedData; + UInt32 bufSize = m_DecryptedDataSize; + UInt32 i; + for (i = 0; i < size && m_CryptoPos < bufSize; i++) + ((Byte *)data)[i] = bufData[m_CryptoPos++]; + return (i == size); + } + return (ReadStream_FALSE(m_Stream, data, size) == S_OK); +} + +void CInArchive::ReadBytesAndTestResult(void *data, UInt32 size) +{ + if(!ReadBytesAndTestSize(data,size)) + ThrowUnexpectedEndOfArchiveException(); +} + +HRESULT CInArchive::ReadBytes(void *data, UInt32 size, UInt32 *processedSize) +{ + size_t realProcessedSize = size; + HRESULT result = ReadStream(m_Stream, data, &realProcessedSize); + if (processedSize != NULL) + *processedSize = (UInt32)realProcessedSize; + AddToSeekValue(realProcessedSize); + return result; +} + +static UInt32 CrcUpdateUInt16(UInt32 crc, UInt16 v) +{ + crc = CRC_UPDATE_BYTE(crc, (Byte)(v & 0xFF)); + crc = CRC_UPDATE_BYTE(crc, (Byte)((v >> 8) & 0xFF)); + return crc; +} + +static UInt32 CrcUpdateUInt32(UInt32 crc, UInt32 v) +{ + crc = CRC_UPDATE_BYTE(crc, (Byte)(v & 0xFF)); + crc = CRC_UPDATE_BYTE(crc, (Byte)((v >> 8) & 0xFF)); + crc = CRC_UPDATE_BYTE(crc, (Byte)((v >> 16) & 0xFF)); + crc = CRC_UPDATE_BYTE(crc, (Byte)((v >> 24) & 0xFF)); + return crc; +} + + +HRESULT CInArchive::Open2(IInStream *stream, const UInt64 *searchHeaderSizeLimit) +{ + m_CryptoMode = false; + RINOK(stream->Seek(0, STREAM_SEEK_SET, &m_StreamStartPosition)); + m_Position = m_StreamStartPosition; + + RINOK(FindAndReadMarker(stream, searchHeaderSizeLimit)); + + Byte buf[NHeader::NArchive::kArchiveHeaderSize]; + UInt32 processedSize; + ReadBytes(buf, sizeof(buf), &processedSize); + if (processedSize != sizeof(buf)) + return S_FALSE; + m_CurData = buf; + m_CurPos = 0; + m_PosLimit = sizeof(buf); + + m_ArchiveHeader.CRC = ReadUInt16(); + m_ArchiveHeader.Type = ReadByte(); + m_ArchiveHeader.Flags = ReadUInt16(); + m_ArchiveHeader.Size = ReadUInt16(); + m_ArchiveHeader.Reserved1 = ReadUInt16(); + m_ArchiveHeader.Reserved2 = ReadUInt32(); + m_ArchiveHeader.EncryptVersion = 0; + + UInt32 crc = CRC_INIT_VAL; + crc = CRC_UPDATE_BYTE(crc, m_ArchiveHeader.Type); + crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Flags); + crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Size); + crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Reserved1); + crc = CrcUpdateUInt32(crc, m_ArchiveHeader.Reserved2); + + if (m_ArchiveHeader.IsThereEncryptVer() && m_ArchiveHeader.Size > NHeader::NArchive::kArchiveHeaderSize) + { + ReadBytes(&m_ArchiveHeader.EncryptVersion, 1, &processedSize); + if (processedSize != 1) + return S_FALSE; + crc = CRC_UPDATE_BYTE(crc, m_ArchiveHeader.EncryptVersion); + } + + if(m_ArchiveHeader.CRC != (CRC_GET_DIGEST(crc) & 0xFFFF)) + ThrowExceptionWithCode(CInArchiveException::kArchiveHeaderCRCError); + if (m_ArchiveHeader.Type != NHeader::NBlockType::kArchiveHeader) + return S_FALSE; + m_ArchiveCommentPosition = m_Position; + m_SeekOnArchiveComment = true; + return S_OK; +} + +void CInArchive::SkipArchiveComment() +{ + if (!m_SeekOnArchiveComment) + return; + AddToSeekValue(m_ArchiveHeader.Size - m_ArchiveHeader.GetBaseSize()); + m_SeekOnArchiveComment = false; +} + +void CInArchive::GetArchiveInfo(CInArchiveInfo &archiveInfo) const +{ + archiveInfo.StartPosition = m_ArchiveStartPosition; + archiveInfo.Flags = m_ArchiveHeader.Flags; + archiveInfo.CommentPosition = m_ArchiveCommentPosition; + archiveInfo.CommentSize = (UInt16)(m_ArchiveHeader.Size - NHeader::NArchive::kArchiveHeaderSize); +} + +static void DecodeUnicodeFileName(const char *name, const Byte *encName, + int encSize, wchar_t *unicodeName, int maxDecSize) +{ + int encPos = 0; + int decPos = 0; + int flagBits = 0; + Byte flags = 0; + Byte highByte = encName[encPos++]; + while (encPos < encSize && decPos < maxDecSize) + { + if (flagBits == 0) + { + flags = encName[encPos++]; + flagBits = 8; + } + switch(flags >> 6) + { + case 0: + unicodeName[decPos++] = encName[encPos++]; + break; + case 1: + unicodeName[decPos++] = (wchar_t)(encName[encPos++] + (highByte << 8)); + break; + case 2: + unicodeName[decPos++] = (wchar_t)(encName[encPos] + (encName[encPos + 1] << 8)); + encPos += 2; + break; + case 3: + { + int length = encName[encPos++]; + if (length & 0x80) + { + Byte correction = encName[encPos++]; + for (length = (length & 0x7f) + 2; + length > 0 && decPos < maxDecSize; length--, decPos++) + unicodeName[decPos] = (wchar_t)(((name[decPos] + correction) & 0xff) + (highByte << 8)); + } + else + for (length += 2; length > 0 && decPos < maxDecSize; length--, decPos++) + unicodeName[decPos] = name[decPos]; + } + break; + } + flags <<= 2; + flagBits -= 2; + } + unicodeName[decPos < maxDecSize ? decPos : maxDecSize - 1] = 0; +} + +void CInArchive::ReadName(CItemEx &item, int nameSize) +{ + item.UnicodeName.Empty(); + if (nameSize > 0) + { + m_NameBuffer.EnsureCapacity(nameSize + 1); + char *buffer = (char *)m_NameBuffer; + + for (int i = 0; i < nameSize; i++) + buffer[i] = ReadByte(); + + int mainLen; + for (mainLen = 0; mainLen < nameSize; mainLen++) + if (buffer[mainLen] == '\0') + break; + buffer[mainLen] = '\0'; + item.Name = buffer; + + if(item.HasUnicodeName()) + { + if(mainLen < nameSize) + { + int unicodeNameSizeMax = MyMin(nameSize, (0x400)); + _unicodeNameBuffer.EnsureCapacity(unicodeNameSizeMax + 1); + DecodeUnicodeFileName(buffer, (const Byte *)buffer + mainLen + 1, + nameSize - (mainLen + 1), _unicodeNameBuffer, unicodeNameSizeMax); + item.UnicodeName = _unicodeNameBuffer; + } + else if (!ConvertUTF8ToUnicode(item.Name, item.UnicodeName)) + item.UnicodeName.Empty(); + } + } + else + item.Name.Empty(); +} + +Byte CInArchive::ReadByte() +{ + if (m_CurPos >= m_PosLimit) + throw CInArchiveException(CInArchiveException::kIncorrectArchive); + return m_CurData[m_CurPos++]; +} + +UInt16 CInArchive::ReadUInt16() +{ + UInt16 value = 0; + for (int i = 0; i < 2; i++) + { + Byte b = ReadByte(); + value |= (UInt16(b) << (8 * i)); + } + return value; +} + +UInt32 CInArchive::ReadUInt32() +{ + UInt32 value = 0; + for (int i = 0; i < 4; i++) + { + Byte b = ReadByte(); + value |= (UInt32(b) << (8 * i)); + } + return value; +} + +void CInArchive::ReadTime(Byte mask, CRarTime &rarTime) +{ + rarTime.LowSecond = (Byte)(((mask & 4) != 0) ? 1 : 0); + int numDigits = (mask & 3); + rarTime.SubTime[0] = rarTime.SubTime[1] = rarTime.SubTime[2] = 0; + for (int i = 0; i < numDigits; i++) + rarTime.SubTime[3 - numDigits + i] = ReadByte(); +} + +void CInArchive::ReadHeaderReal(CItemEx &item) +{ + item.Flags = m_BlockHeader.Flags; + item.PackSize = ReadUInt32(); + item.Size = ReadUInt32(); + item.HostOS = ReadByte(); + item.FileCRC = ReadUInt32(); + item.MTime.DosTime = ReadUInt32(); + item.UnPackVersion = ReadByte(); + item.Method = ReadByte(); + int nameSize = ReadUInt16(); + item.Attrib = ReadUInt32(); + + item.MTime.LowSecond = 0; + item.MTime.SubTime[0] = + item.MTime.SubTime[1] = + item.MTime.SubTime[2] = 0; + + if((item.Flags & NHeader::NFile::kSize64Bits) != 0) + { + item.PackSize |= ((UInt64)ReadUInt32() << 32); + item.Size |= ((UInt64)ReadUInt32() << 32); + } + + ReadName(item, nameSize); + + if (item.HasSalt()) + for (int i = 0; i < sizeof(item.Salt); i++) + item.Salt[i] = ReadByte(); + + // some rar archives have HasExtTime flag without field. + if (m_CurPos < m_PosLimit && item.HasExtTime()) + { + Byte accessMask = (Byte)(ReadByte() >> 4); + Byte b = ReadByte(); + Byte modifMask = (Byte)(b >> 4); + Byte createMask = (Byte)(b & 0xF); + if ((modifMask & 8) != 0) + ReadTime(modifMask, item.MTime); + item.CTimeDefined = ((createMask & 8) != 0); + if (item.CTimeDefined) + { + item.CTime.DosTime = ReadUInt32(); + ReadTime(createMask, item.CTime); + } + item.ATimeDefined = ((accessMask & 8) != 0); + if (item.ATimeDefined) + { + item.ATime.DosTime = ReadUInt32(); + ReadTime(accessMask, item.ATime); + } + } + + UInt16 fileHeaderWithNameSize = (UInt16)m_CurPos; + + item.Position = m_Position; + item.MainPartSize = fileHeaderWithNameSize; + item.CommentSize = (UInt16)(m_BlockHeader.HeadSize - fileHeaderWithNameSize); + + if (m_CryptoMode) + item.AlignSize = (UInt16)((16 - ((m_BlockHeader.HeadSize) & 0xF)) & 0xF); + else + item.AlignSize = 0; + AddToSeekValue(m_BlockHeader.HeadSize); +} + +void CInArchive::AddToSeekValue(UInt64 addValue) +{ + m_Position += addValue; +} + +HRESULT CInArchive::GetNextItem(CItemEx &item, ICryptoGetTextPassword *getTextPassword) +{ + if (m_SeekOnArchiveComment) + SkipArchiveComment(); + for (;;) + { + if(!SeekInArchive(m_Position)) + return S_FALSE; + if (!m_CryptoMode && (m_ArchiveHeader.Flags & + NHeader::NArchive::kBlockHeadersAreEncrypted) != 0) + { + m_CryptoMode = false; + if (getTextPassword == 0) + return S_FALSE; + if(!SeekInArchive(m_Position)) + return S_FALSE; + if (!m_RarAES) + { + m_RarAESSpec = new NCrypto::NRar29::CDecoder; + m_RarAES = m_RarAESSpec; + } + m_RarAESSpec->SetRar350Mode(m_ArchiveHeader.IsEncryptOld()); + + // Salt + const UInt32 kSaltSize = 8; + Byte salt[kSaltSize]; + if(!ReadBytesAndTestSize(salt, kSaltSize)) + return S_FALSE; + m_Position += kSaltSize; + RINOK(m_RarAESSpec->SetDecoderProperties2(salt, kSaltSize)) + // Password + CMyComBSTR password; + RINOK(getTextPassword->CryptoGetTextPassword(&password)) + UString unicodePassword(password); + + CByteBuffer buffer; + const UInt32 sizeInBytes = unicodePassword.Length() * 2; + buffer.SetCapacity(sizeInBytes); + for (int i = 0; i < unicodePassword.Length(); i++) + { + wchar_t c = unicodePassword[i]; + ((Byte *)buffer)[i * 2] = (Byte)c; + ((Byte *)buffer)[i * 2 + 1] = (Byte)(c >> 8); + } + + RINOK(m_RarAESSpec->CryptoSetPassword((const Byte *)buffer, sizeInBytes)); + + const UInt32 kDecryptedBufferSize = (1 << 12); + if (m_DecryptedData.GetCapacity() == 0) + { + m_DecryptedData.SetCapacity(kDecryptedBufferSize); + } + RINOK(m_RarAES->Init()); + size_t decryptedDataSizeT = kDecryptedBufferSize; + RINOK(ReadStream(m_Stream, (Byte *)m_DecryptedData, &decryptedDataSizeT)); + m_DecryptedDataSize = (UInt32)decryptedDataSizeT; + m_DecryptedDataSize = m_RarAES->Filter((Byte *)m_DecryptedData, m_DecryptedDataSize); + + m_CryptoMode = true; + m_CryptoPos = 0; + } + + m_FileHeaderData.EnsureCapacity(7); + if(!ReadBytesAndTestSize((Byte *)m_FileHeaderData, 7)) + return S_FALSE; + + m_CurData = (Byte *)m_FileHeaderData; + m_CurPos = 0; + m_PosLimit = 7; + m_BlockHeader.CRC = ReadUInt16(); + m_BlockHeader.Type = ReadByte(); + m_BlockHeader.Flags = ReadUInt16(); + m_BlockHeader.HeadSize = ReadUInt16(); + + if (m_BlockHeader.HeadSize < 7) + ThrowExceptionWithCode(CInArchiveException::kIncorrectArchive); + + if (m_BlockHeader.Type == NHeader::NBlockType::kEndOfArchive) + return S_FALSE; + + if (m_BlockHeader.Type == NHeader::NBlockType::kFileHeader) + { + m_FileHeaderData.EnsureCapacity(m_BlockHeader.HeadSize); + m_CurData = (Byte *)m_FileHeaderData; + m_PosLimit = m_BlockHeader.HeadSize; + ReadBytesAndTestResult(m_CurData + m_CurPos, m_BlockHeader.HeadSize - 7); + ReadHeaderReal(item); + if ((CrcCalc(m_CurData + 2, + m_BlockHeader.HeadSize - item.CommentSize - 2) & 0xFFFF) != m_BlockHeader.CRC) + ThrowExceptionWithCode(CInArchiveException::kFileHeaderCRCError); + + FinishCryptoBlock(); + m_CryptoMode = false; + SeekInArchive(m_Position); // Move Position to compressed Data; + AddToSeekValue(item.PackSize); // m_Position points to next header; + return S_OK; + } + if (m_CryptoMode && m_BlockHeader.HeadSize > (1 << 12)) + return E_FAIL; // it's for bad passwords + if ((m_BlockHeader.Flags & NHeader::NBlock::kLongBlock) != 0) + { + m_FileHeaderData.EnsureCapacity(7 + 4); + m_CurData = (Byte *)m_FileHeaderData; + ReadBytesAndTestResult(m_CurData + m_CurPos, 4); // test it + m_PosLimit = 7 + 4; + UInt32 dataSize = ReadUInt32(); + AddToSeekValue(dataSize); + if (m_CryptoMode && dataSize > (1 << 27)) + return E_FAIL; // it's for bad passwords + m_CryptoPos = m_BlockHeader.HeadSize; + } + else + m_CryptoPos = 0; + AddToSeekValue(m_BlockHeader.HeadSize); + FinishCryptoBlock(); + m_CryptoMode = false; + } +} + +bool CInArchive::SeekInArchive(UInt64 position) +{ + UInt64 newPosition; + m_Stream->Seek(position, STREAM_SEEK_SET, &newPosition); + return newPosition == position; +} + +ISequentialInStream* CInArchive::CreateLimitedStream(UInt64 position, UInt64 size) +{ + CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream; + CMyComPtr inStream(streamSpec); + SeekInArchive(position); + streamSpec->SetStream(m_Stream); + streamSpec->Init(size); + return inStream.Detach(); +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarIn.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,126 @@ +// RarIn.h + +#ifndef __ARCHIVE_RAR_IN_H +#define __ARCHIVE_RAR_IN_H + +#include "Common/DynamicBuffer.h" +#include "Common/MyCom.h" + +#include "../../ICoder.h" +#include "../../IStream.h" + +#include "../../Common/StreamObjects.h" + +#include "../../Crypto/RarAes.h" + +#include "RarHeader.h" +#include "RarItem.h" + +namespace NArchive { +namespace NRar { + +class CInArchiveException +{ +public: + enum CCauseType + { + kUnexpectedEndOfArchive = 0, + kArchiveHeaderCRCError, + kFileHeaderCRCError, + kIncorrectArchive + } + Cause; + CInArchiveException(CCauseType cause) : Cause(cause) {} +}; + +class CInArchiveInfo +{ +public: + UInt64 StartPosition; + UInt16 Flags; + UInt64 CommentPosition; + UInt16 CommentSize; + bool IsSolid() const { return (Flags & NHeader::NArchive::kSolid) != 0; } + bool IsCommented() const { return (Flags & NHeader::NArchive::kComment) != 0; } + bool IsVolume() const { return (Flags & NHeader::NArchive::kVolume) != 0; } + bool HaveNewVolumeName() const { return (Flags & NHeader::NArchive::kNewVolName) != 0; } + bool IsEncrypted() const { return (Flags & NHeader::NArchive::kBlockEncryption) != 0; } +}; + +class CInArchive +{ + CMyComPtr m_Stream; + + UInt64 m_StreamStartPosition; + UInt64 m_Position; + UInt64 m_ArchiveStartPosition; + + NHeader::NArchive::CHeader360 m_ArchiveHeader; + CDynamicBuffer m_NameBuffer; + CDynamicBuffer _unicodeNameBuffer; + bool m_SeekOnArchiveComment; + UInt64 m_ArchiveCommentPosition; + + void ReadName(CItemEx &item, int nameSize); + void ReadHeaderReal(CItemEx &item); + + HRESULT ReadBytes(void *data, UInt32 size, UInt32 *aProcessedSize); + bool ReadBytesAndTestSize(void *data, UInt32 size); + void ReadBytesAndTestResult(void *data, UInt32 size); + + HRESULT FindAndReadMarker(IInStream *stream, const UInt64 *searchHeaderSizeLimit); + HRESULT Open2(IInStream *stream, const UInt64 *searchHeaderSizeLimit); + + void ThrowExceptionWithCode(CInArchiveException::CCauseType cause); + void ThrowUnexpectedEndOfArchiveException(); + + void AddToSeekValue(UInt64 addValue); + +protected: + + CDynamicBuffer m_FileHeaderData; + + NHeader::NBlock::CBlock m_BlockHeader; + + NCrypto::NRar29::CDecoder *m_RarAESSpec; + CMyComPtr m_RarAES; + + Byte *m_CurData; // it must point to start of Rar::Block + UInt32 m_CurPos; + UInt32 m_PosLimit; + Byte ReadByte(); + UInt16 ReadUInt16(); + UInt32 ReadUInt32(); + void ReadTime(Byte mask, CRarTime &rarTime); + + CBuffer m_DecryptedData; + UInt32 m_DecryptedDataSize; + + bool m_CryptoMode; + UInt32 m_CryptoPos; + void FinishCryptoBlock() + { + if (m_CryptoMode) + while ((m_CryptoPos & 0xF) != 0) + { + m_CryptoPos++; + m_Position++; + } + } + +public: + HRESULT Open(IInStream *inStream, const UInt64 *searchHeaderSizeLimit); + void Close(); + HRESULT GetNextItem(CItemEx &item, ICryptoGetTextPassword *getTextPassword); + + void SkipArchiveComment(); + + void GetArchiveInfo(CInArchiveInfo &archiveInfo) const; + + bool SeekInArchive(UInt64 position); + ISequentialInStream *CreateLimitedStream(UInt64 position, UInt64 size); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarItem.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarItem.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,55 @@ +// RarItem.cpp + +#include "StdAfx.h" + +#include "RarItem.h" + +namespace NArchive{ +namespace NRar{ + +bool CItem::IgnoreItem() const +{ + switch(HostOS) + { + case NHeader::NFile::kHostMSDOS: + case NHeader::NFile::kHostOS2: + case NHeader::NFile::kHostWin32: + return ((Attrib & NHeader::NFile::kLabelFileAttribute) != 0); + } + return false; +} + +bool CItem::IsDir() const +{ + if (GetDictSize() == NHeader::NFile::kDictDirectoryValue) + return true; + switch(HostOS) + { + case NHeader::NFile::kHostMSDOS: + case NHeader::NFile::kHostOS2: + case NHeader::NFile::kHostWin32: + if ((Attrib & FILE_ATTRIBUTE_DIRECTORY) != 0) + return true; + } + return false; +} + +UInt32 CItem::GetWinAttributes() const +{ + UInt32 winAttributes; + switch(HostOS) + { + case NHeader::NFile::kHostMSDOS: + case NHeader::NFile::kHostOS2: + case NHeader::NFile::kHostWin32: + winAttributes = Attrib; + break; + default: + winAttributes = 0; // must be converted from unix value; + } + if (IsDir()) + winAttributes |= NHeader::NFile::kWinFileDirectoryAttributeMask; + return winAttributes; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarItem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarItem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,79 @@ +// RarItem.h + +#ifndef __ARCHIVE_RAR_ITEM_H +#define __ARCHIVE_RAR_ITEM_H + +#include "Common/Types.h" +#include "Common/MyString.h" + +#include "RarHeader.h" + +namespace NArchive{ +namespace NRar{ + +struct CRarTime +{ + UInt32 DosTime; + Byte LowSecond; + Byte SubTime[3]; +}; + +struct CItem +{ + UInt64 Size; + UInt64 PackSize; + + CRarTime CTime; + CRarTime ATime; + CRarTime MTime; + + UInt32 FileCRC; + UInt32 Attrib; + + UInt16 Flags; + Byte HostOS; + Byte UnPackVersion; + Byte Method; + + bool CTimeDefined; + bool ATimeDefined; + + AString Name; + UString UnicodeName; + + Byte Salt[8]; + + bool IsEncrypted() const { return (Flags & NHeader::NFile::kEncrypted) != 0; } + bool IsSolid() const { return (Flags & NHeader::NFile::kSolid) != 0; } + bool IsCommented() const { return (Flags & NHeader::NFile::kComment) != 0; } + bool IsSplitBefore() const { return (Flags & NHeader::NFile::kSplitBefore) != 0; } + bool IsSplitAfter() const { return (Flags & NHeader::NFile::kSplitAfter) != 0; } + bool HasSalt() const { return (Flags & NHeader::NFile::kSalt) != 0; } + bool HasExtTime() const { return (Flags & NHeader::NFile::kExtTime) != 0; } + bool HasUnicodeName()const { return (Flags & NHeader::NFile::kUnicodeName) != 0; } + bool IsOldVersion() const { return (Flags & NHeader::NFile::kOldVersion) != 0; } + + UInt32 GetDictSize() const { return (Flags >> NHeader::NFile::kDictBitStart) & NHeader::NFile::kDictMask; } + bool IsDir() const; + bool IgnoreItem() const; + UInt32 GetWinAttributes() const; + + CItem(): CTimeDefined(false), ATimeDefined(false) {} +}; + +class CItemEx: public CItem +{ +public: + UInt64 Position; + UInt16 MainPartSize; + UInt16 CommentSize; + UInt16 AlignSize; + UInt64 GetFullSize() const { return MainPartSize + CommentSize + AlignSize + PackSize; }; + // DWORD GetHeaderWithCommentSize() const { return MainPartSize + CommentSize; }; + UInt64 GetCommentPosition() const { return Position + MainPartSize; }; + UInt64 GetDataPosition() const { return GetCommentPosition() + CommentSize + AlignSize; }; +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,13 @@ +// RarRegister.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "RarHandler.h" +static IInArchive *CreateArc() { return new NArchive::NRar::CHandler; } + +static CArcInfo g_ArcInfo = + { L"Rar", L"rar r00", 0, 3, {0x52 , 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00}, 7, false, CreateArc, 0, }; + +REGISTER_ARC(Rar) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarVolumeInStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarVolumeInStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,84 @@ +// RarVolumeInStream.cpp + +#include "StdAfx.h" + +#include "RarVolumeInStream.h" + +#include "Windows/Defs.h" +#include "Common/Defs.h" + +extern "C" +{ + #include "../../../../C/7zCrc.h" +} + +namespace NArchive { +namespace NRar { + +void CFolderInStream::Init( + CObjectVector *archives, + const CObjectVector *items, + const CRefItem &refItem) +{ + _archives = archives; + _items = items; + _refItem = refItem; + _curIndex = 0; + CRCs.Clear(); + _fileIsOpen = false; +} + +HRESULT CFolderInStream::OpenStream() +{ + while (_curIndex < _refItem.NumItems) + { + const CItemEx &item = (*_items)[_refItem.ItemIndex + _curIndex]; + _stream.Attach((*_archives)[_refItem.VolumeIndex + _curIndex]. + CreateLimitedStream(item.GetDataPosition(), item.PackSize)); + _curIndex++; + _fileIsOpen = true; + _crc = CRC_INIT_VAL; + return S_OK; + } + return S_OK; +} + +HRESULT CFolderInStream::CloseStream() +{ + CRCs.Add(CRC_GET_DIGEST(_crc)); + _stream.Release(); + _fileIsOpen = false; + return S_OK; +} + +STDMETHODIMP CFolderInStream::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize = 0; + while ((_curIndex < _refItem.NumItems || _fileIsOpen) && size > 0) + { + if (_fileIsOpen) + { + UInt32 localProcessedSize; + RINOK(_stream->Read( + ((Byte *)data) + realProcessedSize, size, &localProcessedSize)); + _crc = CrcUpdate(_crc, ((Byte *)data) + realProcessedSize, localProcessedSize); + if (localProcessedSize == 0) + { + RINOK(CloseStream()); + continue; + } + realProcessedSize += localProcessedSize; + size -= localProcessedSize; + break; + } + else + { + RINOK(OpenStream()); + } + } + if (processedSize != 0) + *processedSize = realProcessedSize; + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarVolumeInStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/RarVolumeInStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,49 @@ +// RarVolumeInStream.h + +#ifndef __RAR_VOLUME_IN_STREAM_H +#define __RAR_VOLUME_IN_STREAM_H + +#include "../../IStream.h" +#include "RarIn.h" + +namespace NArchive { +namespace NRar { + +struct CRefItem +{ + int VolumeIndex; + int ItemIndex; + int NumItems; +}; + +class CFolderInStream: + public ISequentialInStream, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + +private: + CObjectVector *_archives; + const CObjectVector *_items; + CRefItem _refItem; + int _curIndex; + UInt32 _crc; + bool _fileIsOpen; + CMyComPtr _stream; + + HRESULT OpenStream(); + HRESULT CloseStream(); +public: + void Init(CObjectVector *archives, + const CObjectVector *items, + const CRefItem &refItem); + + CRecordVector CRCs; +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Rar/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Rar/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Split/SplitHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Split/SplitHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,357 @@ +// SplitHandler.cpp + +#include "StdAfx.h" + +#include "Common/ComTry.h" +#include "Common/Defs.h" +#include "Common/NewHandler.h" +#include "Common/StringConvert.h" + +#include "Windows/PropVariant.h" +#include "Windows/Time.h" + +#include "../../Common/ProgressUtils.h" + +#include "../../Compress/CopyCoder.h" + +#include "../Common/ItemNameUtils.h" +#include "../Common/MultiStream.h" + +#include "SplitHandler.h" + +using namespace NWindows; +using namespace NTime; + +namespace NArchive { +namespace NSplit { + +STATPROPSTG kProps[] = +{ + { NULL, kpidPath, VT_BSTR}, + { NULL, kpidSize, VT_UI8}, + { NULL, kpidPackSize, VT_UI8}, +}; + +IMP_IInArchive_Props +IMP_IInArchive_ArcProps_NO + +class CSeqName +{ +public: + UString _unchangedPart; + UString _changedPart; + bool _splitStyle; + UString GetNextName() + { + UString newName; + if (_splitStyle) + { + int i; + int numLetters = _changedPart.Length(); + for (i = numLetters - 1; i >= 0; i--) + { + wchar_t c = _changedPart[i]; + if (c == 'z') + { + c = 'a'; + newName = c + newName; + continue; + } + else if (c == 'Z') + { + c = 'A'; + newName = c + newName; + continue; + } + c++; + if ((c == 'z' || c == 'Z') && i == 0) + { + _unchangedPart += c; + wchar_t newChar = (c == 'z') ? L'a' : L'A'; + newName.Empty(); + numLetters++; + for (int k = 0; k < numLetters; k++) + newName += newChar; + break; + } + newName = c + newName; + i--; + for (; i >= 0; i--) + newName = _changedPart[i] + newName; + break; + } + } + else + { + int i; + int numLetters = _changedPart.Length(); + for (i = numLetters - 1; i >= 0; i--) + { + wchar_t c = _changedPart[i]; + if (c == L'9') + { + c = L'0'; + newName = c + newName; + if (i == 0) + newName = UString(L'1') + newName; + continue; + } + c++; + newName = c + newName; + i--; + for (; i >= 0; i--) + newName = _changedPart[i] + newName; + break; + } + } + _changedPart = newName; + return _unchangedPart + _changedPart; + } +}; + +STDMETHODIMP CHandler::Open(IInStream *stream, + const UInt64 * /* maxCheckStartPosition */, + IArchiveOpenCallback *openArchiveCallback) +{ + COM_TRY_BEGIN + Close(); + if (openArchiveCallback == 0) + return S_FALSE; + // try + { + CMyComPtr openVolumeCallback; + CMyComPtr openArchiveCallbackWrap = openArchiveCallback; + if (openArchiveCallbackWrap.QueryInterface(IID_IArchiveOpenVolumeCallback, + &openVolumeCallback) != S_OK) + return S_FALSE; + + { + NCOM::CPropVariant prop; + RINOK(openVolumeCallback->GetProperty(kpidName, &prop)); + if (prop.vt != VT_BSTR) + return S_FALSE; + _name = prop.bstrVal; + } + + int dotPos = _name.ReverseFind('.'); + UString prefix, ext; + if (dotPos >= 0) + { + prefix = _name.Left(dotPos + 1); + ext = _name.Mid(dotPos + 1); + } + else + ext = _name; + UString extBig = ext; + extBig.MakeUpper(); + + CSeqName seqName; + + int numLetters = 2; + bool splitStyle = false; + if (extBig.Right(2) == L"AA") + { + splitStyle = true; + while (numLetters < extBig.Length()) + { + if (extBig[extBig.Length() - numLetters - 1] != 'A') + break; + numLetters++; + } + } + else if (ext.Right(2) == L"01") + { + while (numLetters < extBig.Length()) + { + if (extBig[extBig.Length() - numLetters - 1] != '0') + break; + numLetters++; + } + if (numLetters != ext.Length()) + return S_FALSE; + } + else + return S_FALSE; + + _streams.Add(stream); + + seqName._unchangedPart = prefix + ext.Left(extBig.Length() - numLetters); + seqName._changedPart = ext.Right(numLetters); + seqName._splitStyle = splitStyle; + + if (prefix.Length() < 1) + _subName = L"file"; + else + _subName = prefix.Left(prefix.Length() - 1); + + _totalSize = 0; + UInt64 size; + { + NCOM::CPropVariant prop; + RINOK(openVolumeCallback->GetProperty(kpidSize, &prop)); + if (prop.vt != VT_UI8) + return E_INVALIDARG; + size = prop.uhVal.QuadPart; + } + _totalSize += size; + _sizes.Add(size); + + if (openArchiveCallback != NULL) + { + UInt64 numFiles = _streams.Size(); + RINOK(openArchiveCallback->SetCompleted(&numFiles, NULL)); + } + + for (;;) + { + UString fullName = seqName.GetNextName(); + CMyComPtr nextStream; + HRESULT result = openVolumeCallback->GetStream(fullName, &nextStream); + if (result == S_FALSE) + break; + if (result != S_OK) + return result; + if (!stream) + break; + { + NCOM::CPropVariant prop; + RINOK(openVolumeCallback->GetProperty(kpidSize, &prop)); + if (prop.vt != VT_UI8) + return E_INVALIDARG; + size = prop.uhVal.QuadPart; + } + _totalSize += size; + _sizes.Add(size); + _streams.Add(nextStream); + if (openArchiveCallback != NULL) + { + UInt64 numFiles = _streams.Size(); + RINOK(openArchiveCallback->SetCompleted(&numFiles, NULL)); + } + } + } + /* + catch(...) + { + return S_FALSE; + } + */ + return S_OK; + COM_TRY_END +} + +STDMETHODIMP CHandler::Close() +{ + _sizes.Clear(); + _streams.Clear(); + return S_OK; +} + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = _streams.IsEmpty() ? 0 : 1; + return S_OK; +} + +STDMETHODIMP CHandler::GetProperty(UInt32 /* index */, PROPID propID, PROPVARIANT *value) +{ + NWindows::NCOM::CPropVariant prop; + switch(propID) + { + case kpidPath: + prop = _subName; + break; + case kpidSize: + case kpidPackSize: + prop = _totalSize; + break; + } + prop.Detach(value); + return S_OK; +} + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 _aTestMode, IArchiveExtractCallback *_anExtractCallback) +{ + COM_TRY_BEGIN + + if (numItems != UInt32(-1)) + { + if (numItems != 1) + return E_INVALIDARG; + if (indices[0] != 0) + return E_INVALIDARG; + } + bool testMode = (_aTestMode != 0); + CMyComPtr extractCallback = _anExtractCallback; + extractCallback->SetTotal(_totalSize); + + /* + CMyComPtr volumeExtractCallback; + if (extractCallback.QueryInterface(&volumeExtractCallback) != S_OK) + return E_FAIL; + */ + + UInt64 currentTotalSize = 0; + UInt64 currentItemSize; + + RINOK(extractCallback->SetCompleted(¤tTotalSize)); + CMyComPtr realOutStream; + Int32 askMode; + askMode = testMode ? NArchive::NExtract::NAskMode::kTest : + NArchive::NExtract::NAskMode::kExtract; + Int32 index = 0; + RINOK(extractCallback->GetStream(index, &realOutStream, askMode)); + + RINOK(extractCallback->PrepareOperation(askMode)); + if (testMode) + { + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK)); + return S_OK; + } + + if (!testMode && (!realOutStream)) + return S_OK; + + NCompress::CCopyCoder *copyCoderSpec = new NCompress::CCopyCoder; + CMyComPtr copyCoder = copyCoderSpec; + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, false); + + for (int i = 0; i < _streams.Size(); i++, currentTotalSize += currentItemSize) + { + lps->InSize = lps->OutSize = currentTotalSize; + RINOK(lps->SetCur()); + IInStream *inStream = _streams[i]; + RINOK(inStream->Seek(0, STREAM_SEEK_SET, NULL)); + RINOK(copyCoder->Code(inStream, realOutStream, NULL, NULL, progress)); + currentItemSize = copyCoderSpec->TotalSize; + } + realOutStream.Release(); + return extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK); + COM_TRY_END +} + +STDMETHODIMP CHandler::GetStream(UInt32 index, ISequentialInStream **stream) +{ + if (index != 0) + return E_INVALIDARG; + *stream = 0; + CMultiStream *streamSpec = new CMultiStream; + CMyComPtr streamTemp = streamSpec; + for (int i = 0; i < _streams.Size(); i++) + { + CMultiStream::CSubStreamInfo subStreamInfo; + subStreamInfo.Stream = _streams[i]; + subStreamInfo.Pos = 0; + subStreamInfo.Size = _sizes[i]; + streamSpec->Streams.Add(subStreamInfo); + } + streamSpec->Init(); + *stream = streamTemp.Detach(); + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Split/SplitHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Split/SplitHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,37 @@ +// Split/Handler.h + +#ifndef __SPLIT_HANDLER_H +#define __SPLIT_HANDLER_H + +#include "Common/MyCom.h" +#include "Common/MyString.h" +#include "../IArchive.h" + +namespace NArchive { +namespace NSplit { + +class CHandler: + public IInArchive, + public IInArchiveGetStream, + // public IOutArchive, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP2(IInArchive, IInArchiveGetStream) + + INTERFACE_IInArchive(;) + + STDMETHOD(GetStream)(UInt32 index, ISequentialInStream **stream); + +private: + UString _subName; + UString _name; + CObjectVector > _streams; + CRecordVector _sizes; + + UInt64 _totalSize; +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Split/SplitRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Split/SplitRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// SplitRegister.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "SplitHandler.h" +static IInArchive *CreateArc() { return new NArchive::NSplit::CHandler; } +/* +#ifndef EXTRACT_ONLY +static IOutArchive *CreateArcOut() { return new NArchive::NSplit::CHandler; } +#else +#define CreateArcOut 0 +#endif +*/ + +static CArcInfo g_ArcInfo = +{ L"Split", L"001", 0, 0xEA, { 0 }, 0, false, CreateArc, 0 }; + +REGISTER_ARC(Split) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Split/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Split/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,9 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../Common/MyWindows.h" +#include "../../Common/NewHandler.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,9 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" +#include "../../../Common/NewHandler.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,229 @@ +// TarHandler.cpp + +#include "StdAfx.h" + +#include "Common/ComTry.h" +#include "Common/Defs.h" +#include "Common/NewHandler.h" +#include "Common/StringConvert.h" + +#include "Windows/PropVariant.h" +#include "Windows/Time.h" + +#include "../../Common/LimitedStreams.h" +#include "../../Common/ProgressUtils.h" + +#include "../../Compress/CopyCoder.h" + +#include "../Common/DummyOutStream.h" +#include "../Common/ItemNameUtils.h" + +#include "TarHandler.h" +#include "TarIn.h" + +using namespace NWindows; + +namespace NArchive { +namespace NTar { + +STATPROPSTG kProps[] = +{ + { NULL, kpidPath, VT_BSTR}, + { NULL, kpidIsDir, VT_BOOL}, + { NULL, kpidSize, VT_UI8}, + { NULL, kpidPackSize, VT_UI8}, + { NULL, kpidMTime, VT_FILETIME}, + { NULL, kpidUser, VT_BSTR}, + { NULL, kpidGroup, VT_BSTR} +}; + +IMP_IInArchive_Props +IMP_IInArchive_ArcProps_NO + +HRESULT CHandler::Open2(IInStream *stream, IArchiveOpenCallback *callback) +{ + UInt64 endPos = 0; + if (callback != NULL) + { + RINOK(stream->Seek(0, STREAM_SEEK_END, &endPos)); + RINOK(stream->Seek(0, STREAM_SEEK_SET, NULL)); + } + + UInt64 pos = 0; + for (;;) + { + CItemEx item; + bool filled; + item.HeaderPosition = pos; + RINOK(ReadItem(stream, filled, item)); + if (!filled) + break; + _items.Add(item); + + RINOK(stream->Seek(item.GetPackSize(), STREAM_SEEK_CUR, &pos)); + if (pos >= endPos) + return S_FALSE; + if (callback != NULL) + { + if (_items.Size() == 1) + { + RINOK(callback->SetTotal(NULL, &endPos)); + } + if (_items.Size() % 100 == 0) + { + UInt64 numFiles = _items.Size(); + RINOK(callback->SetCompleted(&numFiles, &pos)); + } + } + } + + if (_items.Size() == 0) + { + CMyComPtr openVolumeCallback; + if (!callback) + return S_FALSE; + callback->QueryInterface(IID_IArchiveOpenVolumeCallback, (void **)&openVolumeCallback); + if (!openVolumeCallback) + return S_FALSE; + NCOM::CPropVariant prop; + if (openVolumeCallback->GetProperty(kpidName, &prop) != S_OK) + return S_FALSE; + if (prop.vt != VT_BSTR) + return S_FALSE; + UString baseName = prop.bstrVal; + baseName = baseName.Right(4); + if (baseName.CompareNoCase(L".tar") != 0) + return S_FALSE; + } + return S_OK; +} + +STDMETHODIMP CHandler::Open(IInStream *stream, + const UInt64 * /* maxCheckStartPosition */, + IArchiveOpenCallback *openArchiveCallback) +{ + COM_TRY_BEGIN + { + Close(); + RINOK(Open2(stream, openArchiveCallback)); + _inStream = stream; + } + return S_OK; + COM_TRY_END +} + +STDMETHODIMP CHandler::Close() +{ + _items.Clear(); + _inStream.Release(); + return S_OK; +} + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = _items.Size(); + return S_OK; +} + +STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value) +{ + COM_TRY_BEGIN + NWindows::NCOM::CPropVariant prop; + const CItemEx &item = _items[index]; + + switch(propID) + { + case kpidPath: prop = NItemName::GetOSName2(MultiByteToUnicodeString(item.Name, CP_OEMCP)); break; + case kpidIsDir: prop = item.IsDir(); break; + case kpidSize: prop = item.Size; break; + case kpidPackSize: prop = item.GetPackSize(); break; + case kpidMTime: + if (item.MTime != 0) + { + FILETIME ft; + NTime::UnixTimeToFileTime(item.MTime, ft); + prop = ft; + } + break; + case kpidUser: prop = MultiByteToUnicodeString(item.UserName, CP_OEMCP); break; + case kpidGroup: prop = MultiByteToUnicodeString(item.GroupName, CP_OEMCP); break; + } + prop.Detach(value); + return S_OK; + COM_TRY_END +} + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 _aTestMode, IArchiveExtractCallback *extractCallback) +{ + COM_TRY_BEGIN + bool testMode = (_aTestMode != 0); + bool allFilesMode = (numItems == UInt32(-1)); + if (allFilesMode) + numItems = _items.Size(); + if (numItems == 0) + return S_OK; + UInt64 totalSize = 0; + UInt32 i; + for (i = 0; i < numItems; i++) + totalSize += _items[allFilesMode ? i : indices[i]].Size; + extractCallback->SetTotal(totalSize); + + UInt64 totalPackSize, curPackSize, curSize; + totalSize = totalPackSize = 0; + + NCompress::CCopyCoder *copyCoderSpec = new NCompress::CCopyCoder(); + CMyComPtr copyCoder = copyCoderSpec; + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, false); + + CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream; + CMyComPtr inStream(streamSpec); + streamSpec->SetStream(_inStream); + + CDummyOutStream *outStreamSpec = new CDummyOutStream; + CMyComPtr outStream(outStreamSpec); + + for (i = 0; i < numItems; i++, totalSize += curSize, totalPackSize += curPackSize) + { + lps->InSize = totalPackSize; + lps->OutSize = totalSize; + RINOK(lps->SetCur()); + CMyComPtr realOutStream; + Int32 askMode = testMode ? + NArchive::NExtract::NAskMode::kTest : + NArchive::NExtract::NAskMode::kExtract; + Int32 index = allFilesMode ? i : indices[i]; + const CItemEx &item = _items[index]; + RINOK(extractCallback->GetStream(index, &realOutStream, askMode)); + curSize = item.Size; + curPackSize = item.GetPackSize(); + if (item.IsDir()) + { + RINOK(extractCallback->PrepareOperation(askMode)); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK)); + continue; + } + if (!testMode && (!realOutStream)) + continue; + RINOK(extractCallback->PrepareOperation(askMode)); + + outStreamSpec->SetStream(realOutStream); + realOutStream.Release(); + outStreamSpec->Init(); + + RINOK(_inStream->Seek(item.GetDataPosition(), STREAM_SEEK_SET, NULL)); + streamSpec->Init(item.Size); + RINOK(copyCoder->Code(inStream, outStream, NULL, NULL, progress)); + outStreamSpec->ReleaseStream(); + RINOK(extractCallback->SetOperationResult(copyCoderSpec->TotalSize == item.Size ? + NArchive::NExtract::NOperationResult::kOK: + NArchive::NExtract::NOperationResult::kDataError)); + } + return S_OK; + COM_TRY_END +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,37 @@ +// Tar/Handler.h + +#ifndef __TAR_HANDLER_H +#define __TAR_HANDLER_H + +#include "Common/MyCom.h" +#include "../IArchive.h" + +#include "TarItem.h" + +namespace NArchive { +namespace NTar { + +class CHandler: + public IInArchive, + public IOutArchive, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP2( + IInArchive, + IOutArchive + ) + + INTERFACE_IInArchive(;) + INTERFACE_IOutArchive(;) + + HRESULT Open2(IInStream *stream, IArchiveOpenCallback *callback); + +private: + CObjectVector _items; + CMyComPtr _inStream; +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarHeader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarHeader.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,25 @@ +// Archive/Tar/Header.h + +#include "StdAfx.h" + +#include "TarHeader.h" + +namespace NArchive { +namespace NTar { +namespace NFileHeader { + + // The checksum field is filled with this while the checksum is computed. + const char *kCheckSumBlanks = " "; // 8 blanks, no null + + const char *kLongLink = "././@LongLink"; + const char *kLongLink2 = "@LongLink"; + + // The magic field is filled with this if uname and gname are valid. + namespace NMagic + { + const char *kUsTar = "ustar"; // 5 chars + const char *kGNUTar = "GNUtar "; // 7 chars and a null + const char *kEmpty = "\0\0\0\0\0\0\0\0"; // 7 chars and a null + } + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarHeader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarHeader.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,108 @@ +// Archive/Tar/Header.h + +#ifndef __ARCHIVE_TAR_HEADER_H +#define __ARCHIVE_TAR_HEADER_H + +#include "Common/Types.h" + +namespace NArchive { +namespace NTar { + +namespace NFileHeader +{ + const int kRecordSize = 512; + const int kNameSize = 100; + const int kUserNameSize = 32; + const int kGroupNameSize = 32; + const int kPrefixSize = 155; + + /* + struct CHeader + { + char Name[kNameSize]; + char Mode[8]; + char UID[8]; + char GID[8]; + char Size[12]; + char ModificationTime[12]; + char CheckSum[8]; + char LinkFlag; + char LinkName[kNameSize]; + char Magic[8]; + char UserName[kUserNameSize]; + char GroupName[kGroupNameSize]; + char DeviceMajor[8]; + char DeviceMinor[8]; + char Prefix[155]; + }; + union CRecord + { + CHeader Header; + Byte Padding[kRecordSize]; + }; + */ + + namespace NMode + { + const int kSetUID = 04000; // Set UID on execution + const int kSetGID = 02000; // Set GID on execution + const int kSaveText = 01000; // Save text (sticky bit) + } + + namespace NFilePermissions + { + const int kUserRead = 00400; // read by owner + const int kUserWrite = 00200; // write by owner + const int kUserExecute = 00100; // execute/search by owner + const int kGroupRead = 00040; // read by group + const int kGroupWrite = 00020; // write by group + const int kGroupExecute = 00010; // execute/search by group + const int kOtherRead = 00004; // read by other + const int kOtherWrite = 00002; // write by other + const int kOtherExecute = 00001; // execute/search by other + } + + + // The linkflag defines the type of file + namespace NLinkFlag + { + const char kOldNormal = '\0'; // Normal disk file, Unix compatible + const char kNormal = '0'; // Normal disk file + const char kLink = '1'; // Link to previously dumped file + const char kSymbolicLink = '2'; // Symbolic link + const char kCharacter = '3'; // Character special file + const char kBlock = '4'; // Block special file + const char kDirectory = '5'; // Directory + const char kFIFO = '6'; // FIFO special file + const char kContiguous = '7'; // Contiguous file + + const char kDumpDir = 'D'; /* GNUTYPE_DUMPDIR. + data: list of files created by the --incremental (-G) option + Each file name is preceded by either + - 'Y' (file should be in this archive) + - 'N' (file is a directory, or is not stored in the archive.) + Each file name is terminated by a null + an additional null after + the last file name. */ + + } + // Further link types may be defined later. + + // The checksum field is filled with this while the checksum is computed. + extern const char *kCheckSumBlanks;// = " "; // 8 blanks, no null + + extern const char *kLongLink; // = "././@LongLink"; + extern const char *kLongLink2; // = "@LongLink"; + + // The magic field is filled with this if uname and gname are valid. + namespace NMagic + { + extern const char *kUsTar; // = "ustar"; // 5 chars + extern const char *kGNUTar; // = "GNUtar "; // 7 chars and a null + extern const char *kEmpty; // = "GNUtar "; // 7 chars and a null + } + +} + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarIn.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,176 @@ +// Archive/TarIn.cpp + +#include "StdAfx.h" + +#include "TarIn.h" +#include "TarHeader.h" + +#include "Common/StringToInt.h" +#include "Windows/Defs.h" + +#include "../../Common/StreamUtils.h" + +namespace NArchive { +namespace NTar { + +static void MyStrNCpy(char *dest, const char *src, int size) +{ + for (int i = 0; i < size; i++) + { + char c = src[i]; + dest[i] = c; + if (c == 0) + break; + } +} + +static bool OctalToNumber(const char *srcString, int size, UInt64 &res) +{ + char sz[32]; + MyStrNCpy(sz, srcString, size); + sz[size] = 0; + const char *end; + int i; + for (i = 0; sz[i] == ' '; i++); + res = ConvertOctStringToUInt64(sz + i, &end); + return (*end == ' ' || *end == 0); +} + +static bool OctalToNumber32(const char *srcString, int size, UInt32 &res) +{ + UInt64 res64; + if (!OctalToNumber(srcString, size, res64)) + return false; + res = (UInt32)res64; + return (res64 <= 0xFFFFFFFF); +} + +#define RIF(x) { if (!(x)) return S_FALSE; } + +static bool IsRecordLast(const char *buf) +{ + for (int i = 0; i < NFileHeader::kRecordSize; i++) + if (buf[i] != 0) + return false; + return true; +} + +static void ReadString(const char *s, int size, AString &result) +{ + char temp[NFileHeader::kRecordSize + 1]; + MyStrNCpy(temp, s, size); + temp[size] = '\0'; + result = temp; +} + +static HRESULT GetNextItemReal(ISequentialInStream *stream, bool &filled, CItemEx &item, size_t &processedSize) +{ + item.LongLinkSize = 0; + char buf[NFileHeader::kRecordSize]; + char *p = buf; + + filled = false; + + processedSize = NFileHeader::kRecordSize; + RINOK(ReadStream(stream, buf, &processedSize)); + if (processedSize == 0 || (processedSize == NFileHeader::kRecordSize && IsRecordLast(buf))) + return S_OK; + if (processedSize < NFileHeader::kRecordSize) + return S_FALSE; + + ReadString(p, NFileHeader::kNameSize, item.Name); p += NFileHeader::kNameSize; + + RIF(OctalToNumber32(p, 8, item.Mode)); p += 8; + + if (!OctalToNumber32(p, 8, item.UID)) item.UID = 0; p += 8; + if (!OctalToNumber32(p, 8, item.GID)) item.GID = 0; p += 8; + + RIF(OctalToNumber(p, 12, item.Size)); p += 12; + RIF(OctalToNumber32(p, 12, item.MTime)); p += 12; + + UInt32 checkSum; + RIF(OctalToNumber32(p, 8, checkSum)); + memcpy(p, NFileHeader::kCheckSumBlanks, 8); p += 8; + + item.LinkFlag = *p++; + + ReadString(p, NFileHeader::kNameSize, item.LinkName); p += NFileHeader::kNameSize; + + memcpy(item.Magic, p, 8); p += 8; + + ReadString(p, NFileHeader::kUserNameSize, item.UserName); p += NFileHeader::kUserNameSize; + ReadString(p, NFileHeader::kUserNameSize, item.GroupName); p += NFileHeader::kUserNameSize; + + item.DeviceMajorDefined = (p[0] != 0); RIF(OctalToNumber32(p, 8, item.DeviceMajor)); p += 8; + item.DeviceMinorDefined = (p[0] != 0); RIF(OctalToNumber32(p, 8, item.DeviceMinor)); p += 8; + + AString prefix; + ReadString(p, NFileHeader::kPrefixSize, prefix); + p += NFileHeader::kPrefixSize; + if (!prefix.IsEmpty() && item.IsMagic() && + (item.LinkFlag != 'L' /* || prefix != "00000000000" */ )) + item.Name = prefix + AString('/') + item.Name; + + if (item.LinkFlag == NFileHeader::NLinkFlag::kLink) + item.Size = 0; + + UInt32 checkSumReal = 0; + for (int i = 0; i < NFileHeader::kRecordSize; i++) + checkSumReal += (Byte)buf[i]; + + if (checkSumReal != checkSum) + return S_FALSE; + + filled = true; + return S_OK; +} + +HRESULT ReadItem(ISequentialInStream *stream, bool &filled, CItemEx &item) +{ + size_t processedSize; + RINOK(GetNextItemReal(stream, filled, item, processedSize)); + if (!filled) + return S_OK; + // GNUtar extension + if (item.LinkFlag == 'L') + { + if (item.Name.Compare(NFileHeader::kLongLink) != 0) + if (item.Name.Compare(NFileHeader::kLongLink2) != 0) + return S_FALSE; + + AString fullName; + if (item.Size > (1 << 15)) + return S_FALSE; + int packSize = (int)item.GetPackSize(); + char *buffer = fullName.GetBuffer(packSize + 1); + + RINOK(ReadStream_FALSE(stream, buffer, packSize)); + processedSize += packSize; + buffer[item.Size] = '\0'; + fullName.ReleaseBuffer(); + + UInt64 headerPosition = item.HeaderPosition; + { + size_t processedSize2; + RINOK(GetNextItemReal(stream, filled, item, processedSize2)); + } + item.LongLinkSize = (unsigned)processedSize; + item.Name = fullName; + item.HeaderPosition = headerPosition; + } + else if (item.LinkFlag == 'g' || item.LinkFlag == 'x' || item.LinkFlag == 'X') + { + // pax Extended Header + return S_OK; + } + else if (item.LinkFlag == NFileHeader::NLinkFlag::kDumpDir) + { + // GNU Extensions to the Archive Format + return S_OK; + } + else if (item.LinkFlag > '7' || (item.LinkFlag < '0' && item.LinkFlag != 0)) + return S_FALSE; + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarIn.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// Archive/TarIn.h + +#ifndef __ARCHIVE_TAR_IN_H +#define __ARCHIVE_TAR_IN_H + +#include "Common/MyCom.h" +#include "../../IStream.h" + +#include "TarItem.h" + +namespace NArchive { +namespace NTar { + +HRESULT ReadItem(ISequentialInStream *stream, bool &filled, CItemEx &itemInfo); + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarItem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarItem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,71 @@ +// Archive/Tar/Item.h + +#ifndef __ARCHIVE_TAR_ITEM_H +#define __ARCHIVE_TAR_ITEM_H + +#include "Common/Types.h" +#include "Common/MyString.h" + +#include "../Common/ItemNameUtils.h" +#include "TarHeader.h" + +namespace NArchive { +namespace NTar { + +struct CItem +{ + AString Name; + UInt64 Size; + + UInt32 Mode; + UInt32 UID; + UInt32 GID; + UInt32 MTime; + UInt32 DeviceMajor; + UInt32 DeviceMinor; + + AString LinkName; + AString UserName; + AString GroupName; + + char Magic[8]; + char LinkFlag; + bool DeviceMajorDefined; + bool DeviceMinorDefined; + + bool IsDir() const + { + switch(LinkFlag) + { + case NFileHeader::NLinkFlag::kDirectory: + case NFileHeader::NLinkFlag::kDumpDir: + return true; + case NFileHeader::NLinkFlag::kOldNormal: + case NFileHeader::NLinkFlag::kNormal: + return NItemName::HasTailSlash(Name, CP_OEMCP); + } + return false; + } + + bool IsMagic() const + { + for (int i = 0; i < 5; i++) + if (Magic[i] != NFileHeader::NMagic::kUsTar[i]) + return false; + return true; + } + + UInt64 GetPackSize() const { return (Size + 0x1FF) & (~((UInt64)0x1FF)); } +}; + +struct CItemEx: public CItem +{ + UInt64 HeaderPosition; + unsigned LongLinkSize; + UInt64 GetDataPosition() const { return HeaderPosition + LongLinkSize + NFileHeader::kRecordSize; } + UInt64 GetFullSize() const { return LongLinkSize + NFileHeader::kRecordSize + Size; } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Tar/TarRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// TarRegister.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "TarHandler.h" +static IInArchive *CreateArc() { return new NArchive::NTar::CHandler; } +#ifndef EXTRACT_ONLY +static IOutArchive *CreateArcOut() { return new NArchive::NTar::CHandler; } +#else +#define CreateArcOut 0 +#endif + +static CArcInfo g_ArcInfo = +{ L"Tar", L"tar", 0, 0xEE, { 'u', 's', 't', 'a', 'r' }, 5, false, CreateArc, CreateArcOut }; + +REGISTER_ARC(Tar) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipCompressionMode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipCompressionMode.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,39 @@ +// CompressionMode.h + +#ifndef __ZIP_COMPRESSIONMETHOD_H +#define __ZIP_COMPRESSIONMETHOD_H + +#include "../../../Common/MyString.h" + +namespace NArchive { +namespace NZip { + +struct CCompressionMethodMode +{ + CRecordVector MethodSequence; + UString MatchFinder; + UInt32 Algo; + UInt32 NumPasses; + UInt32 NumFastBytes; + bool NumMatchFinderCyclesDefined; + UInt32 NumMatchFinderCycles; + UInt32 DicSize; + #ifdef COMPRESS_MT + UInt32 NumThreads; + #endif + bool PasswordIsDefined; + AString Password; + bool IsAesMode; + Byte AesKeyMode; + + CCompressionMethodMode(): + NumMatchFinderCyclesDefined(false), + PasswordIsDefined(false), + IsAesMode(false), + AesKeyMode(3) + {} +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,831 @@ +// ZipHandler.cpp + +#include "StdAfx.h" + +#include "Common/ComTry.h" +#include "Common/Defs.h" +#include "Common/IntToString.h" +#include "Common/StringConvert.h" + +#include "Windows/PropVariant.h" +#include "Windows/Time.h" + +#include "../../IPassword.h" + +#include "../../Common/CreateCoder.h" +#include "../../Common/FilterCoder.h" +#include "../../Common/ProgressUtils.h" +#include "../../Common/StreamObjects.h" +#include "../../Common/StreamUtils.h" + +#include "../../Compress/CopyCoder.h" +#include "../../Compress/LzmaDecoder.h" +#include "../../Compress/ImplodeDecoder.h" +#include "../../Compress/ShrinkDecoder.h" + +#include "../../Crypto/WzAes.h" +#include "../../Crypto/ZipCrypto.h" +#include "../../Crypto/ZipStrong.h" + +#include "../Common/ItemNameUtils.h" +#include "../Common/OutStreamWithCRC.h" + +#include "ZipHandler.h" + +using namespace NWindows; + +namespace NArchive { +namespace NZip { + +// static const CMethodId kMethodId_Store = 0; +static const CMethodId kMethodId_ZipBase = 0x040100; +static const CMethodId kMethodId_BZip2 = 0x040202; + +const wchar_t *kHostOS[] = +{ + L"FAT", + L"AMIGA", + L"VMS", + L"Unix", + L"VM/CMS", + L"Atari", + L"HPFS", + L"Macintosh", + L"Z-System", + L"CP/M", + L"TOPS-20", + L"NTFS", + L"SMS/QDOS", + L"Acorn", + L"VFAT", + L"MVS", + L"BeOS", + L"Tandem", + L"OS/400", + L"OS/X" +}; + + +static const int kNumHostOSes = sizeof(kHostOS) / sizeof(kHostOS[0]); + +static const wchar_t *kUnknownOS = L"Unknown"; + +STATPROPSTG kProps[] = +{ + { NULL, kpidPath, VT_BSTR}, + { NULL, kpidIsDir, VT_BOOL}, + { NULL, kpidSize, VT_UI8}, + { NULL, kpidPackSize, VT_UI8}, + { NULL, kpidMTime, VT_FILETIME}, + { NULL, kpidCTime, VT_FILETIME}, + { NULL, kpidATime, VT_FILETIME}, + + { NULL, kpidAttrib, VT_UI4}, + + { NULL, kpidEncrypted, VT_BOOL}, + { NULL, kpidComment, VT_BSTR}, + + { NULL, kpidCRC, VT_UI4}, + + { NULL, kpidMethod, VT_BSTR}, + { NULL, kpidHostOS, VT_BSTR} + + // { NULL, kpidUnpackVer, VT_UI1}, +}; + +const wchar_t *kMethods[] = +{ + L"Store", + L"Shrink", + L"Reduced1", + L"Reduced2", + L"Reduced2", + L"Reduced3", + L"Implode", + L"Tokenizing", + L"Deflate", + L"Deflate64", + L"PKImploding" +}; + +const int kNumMethods = sizeof(kMethods) / sizeof(kMethods[0]); +const wchar_t *kBZip2Method = L"BZip2"; +const wchar_t *kLZMAMethod = L"LZMA"; +const wchar_t *kJpegMethod = L"Jpeg"; +const wchar_t *kWavPackMethod = L"WavPack"; +const wchar_t *kPPMdMethod = L"PPMd"; +const wchar_t *kAESMethod = L"AES"; +const wchar_t *kZipCryptoMethod = L"ZipCrypto"; +const wchar_t *kStrongCryptoMethod = L"StrongCrypto"; + +struct CStrongCryptoPair +{ + UInt16 Id; + const wchar_t *Name; +}; + +CStrongCryptoPair g_StrongCryptoPairs[] = +{ + { NStrongCryptoFlags::kDES, L"DES" }, + { NStrongCryptoFlags::kRC2old, L"RC2a" }, + { NStrongCryptoFlags::k3DES168, L"3DES-168" }, + { NStrongCryptoFlags::k3DES112, L"3DES-112" }, + { NStrongCryptoFlags::kAES128, L"pkAES-128" }, + { NStrongCryptoFlags::kAES192, L"pkAES-192" }, + { NStrongCryptoFlags::kAES256, L"pkAES-256" }, + { NStrongCryptoFlags::kRC2, L"RC2" }, + { NStrongCryptoFlags::kBlowfish, L"Blowfish" }, + { NStrongCryptoFlags::kTwofish, L"Twofish" }, + { NStrongCryptoFlags::kRC4, L"RC4" } +}; + +STATPROPSTG kArcProps[] = +{ + { NULL, kpidBit64, VT_BOOL}, + { NULL, kpidComment, VT_BSTR} +}; + +CHandler::CHandler() +{ + InitMethodProperties(); +} + +static AString BytesToString(const CByteBuffer &data) +{ + AString s; + int size = (int)data.GetCapacity(); + if (size > 0) + { + char *p = s.GetBuffer(size + 1); + memcpy(p, (const Byte *)data, size); + p[size] = '\0'; + s.ReleaseBuffer(); + } + return s; +} + +IMP_IInArchive_Props +IMP_IInArchive_ArcProps + +STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value) +{ + COM_TRY_BEGIN + NWindows::NCOM::CPropVariant prop; + switch(propID) + { + case kpidBit64: if (m_Archive.IsZip64) prop = m_Archive.IsZip64; break; + case kpidComment: + prop = MultiByteToUnicodeString(BytesToString(m_Archive.m_ArchiveInfo.Comment), CP_ACP); + break; + } + prop.Detach(value); + COM_TRY_END + return S_OK; +} + +STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems) +{ + *numItems = m_Items.Size(); + return S_OK; +} + +STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value) +{ + COM_TRY_BEGIN + NWindows::NCOM::CPropVariant prop; + const CItemEx &item = m_Items[index]; + switch(propID) + { + case kpidPath: prop = NItemName::GetOSName2(item.GetUnicodeString(item.Name)); break; + case kpidIsDir: prop = item.IsDir(); break; + case kpidSize: prop = item.UnPackSize; break; + case kpidPackSize: prop = item.PackSize; break; + case kpidTimeType: + { + FILETIME utcFileTime; + if (item.CentralExtra.GetNtfsTime(NFileHeader::NNtfsExtra::kTagTime, utcFileTime)) + prop = (UInt32)NFileTimeType::kWindows; + break; + } + case kpidCTime: + { + FILETIME ft; + if (item.CentralExtra.GetNtfsTime(NFileHeader::NNtfsExtra::kCTime, ft)) + prop = ft; + break; + } + case kpidATime: + { + FILETIME ft; + if (item.CentralExtra.GetNtfsTime(NFileHeader::NNtfsExtra::kATime, ft)) + prop = ft; + break; + } + case kpidMTime: + { + FILETIME utcFileTime; + if (!item.CentralExtra.GetNtfsTime(NFileHeader::NNtfsExtra::kMTime, utcFileTime)) + { + FILETIME localFileTime; + if (NTime::DosTimeToFileTime(item.Time, localFileTime)) + { + if (!LocalFileTimeToFileTime(&localFileTime, &utcFileTime)) + utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0; + } + else + utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0; + } + prop = utcFileTime; + break; + } + case kpidAttrib: prop = item.GetWinAttributes(); break; + case kpidEncrypted: prop = item.IsEncrypted(); break; + case kpidComment: prop = item.GetUnicodeString(BytesToString(item.Comment)); break; + case kpidCRC: if (item.IsThereCrc()) prop = item.FileCRC; break; + case kpidMethod: + { + UInt16 methodId = item.CompressionMethod; + UString method; + if (item.IsEncrypted()) + { + if (methodId == NFileHeader::NCompressionMethod::kWzAES) + { + method = kAESMethod; + CWzAesExtraField aesField; + if (item.CentralExtra.GetWzAesField(aesField)) + { + method += L"-"; + wchar_t s[32]; + ConvertUInt64ToString((aesField.Strength + 1) * 64 , s); + method += s; + method += L" "; + methodId = aesField.Method; + } + } + else + { + if (item.IsStrongEncrypted()) + { + CStrongCryptoField f; + bool finded = false; + if (item.CentralExtra.GetStrongCryptoField(f)) + { + for (int i = 0; i < sizeof(g_StrongCryptoPairs) / sizeof(g_StrongCryptoPairs[0]); i++) + { + const CStrongCryptoPair &pair = g_StrongCryptoPairs[i]; + if (f.AlgId == pair.Id) + { + method += pair.Name; + finded = true; + break; + } + } + } + if (!finded) + method += kStrongCryptoMethod; + } + else + method += kZipCryptoMethod; + method += L" "; + } + } + if (methodId < kNumMethods) + method += kMethods[methodId]; + else switch (methodId) + { + case NFileHeader::NCompressionMethod::kLZMA: + method += kLZMAMethod; + if (item.IsLzmaEOS()) + method += L":EOS"; + break; + case NFileHeader::NCompressionMethod::kBZip2: method += kBZip2Method; break; + case NFileHeader::NCompressionMethod::kJpeg: method += kJpegMethod; break; + case NFileHeader::NCompressionMethod::kWavPack: method += kWavPackMethod; break; + case NFileHeader::NCompressionMethod::kPPMd: method += kPPMdMethod; break; + default: + { + wchar_t s[32]; + ConvertUInt64ToString(methodId, s); + method += s; + } + } + prop = method; + break; + } + case kpidHostOS: + prop = (item.MadeByVersion.HostOS < kNumHostOSes) ? + (kHostOS[item.MadeByVersion.HostOS]) : kUnknownOS; + break; + } + prop.Detach(value); + return S_OK; + COM_TRY_END +} + +class CProgressImp: public CProgressVirt +{ + CMyComPtr _callback; +public: + STDMETHOD(SetTotal)(UInt64 numFiles); + STDMETHOD(SetCompleted)(UInt64 numFiles); + CProgressImp(IArchiveOpenCallback *callback): _callback(callback) {} +}; + +STDMETHODIMP CProgressImp::SetTotal(UInt64 numFiles) +{ + if (_callback) + return _callback->SetTotal(&numFiles, NULL); + return S_OK; +} + +STDMETHODIMP CProgressImp::SetCompleted(UInt64 numFiles) +{ + if (_callback) + return _callback->SetCompleted(&numFiles, NULL); + return S_OK; +} + +STDMETHODIMP CHandler::Open(IInStream *inStream, + const UInt64 *maxCheckStartPosition, IArchiveOpenCallback *callback) +{ + COM_TRY_BEGIN + try + { + Close(); + RINOK(inStream->Seek(0, STREAM_SEEK_SET, NULL)); + RINOK(m_Archive.Open(inStream, maxCheckStartPosition)); + CProgressImp progressImp(callback); + return m_Archive.ReadHeaders(m_Items, &progressImp); + } + catch(const CInArchiveException &) { Close(); return S_FALSE; } + catch(...) { Close(); throw; } + COM_TRY_END +} + +STDMETHODIMP CHandler::Close() +{ + m_Items.Clear(); + m_Archive.Close(); + return S_OK; +} + +////////////////////////////////////// +// CHandler::DecompressItems + +class CLzmaDecoder: + public ICompressCoder, + public CMyUnknownImp +{ + NCompress::NLzma::CDecoder *DecoderSpec; + CMyComPtr Decoder; +public: + CLzmaDecoder(); + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + MY_UNKNOWN_IMP +}; + +CLzmaDecoder::CLzmaDecoder() +{ + DecoderSpec = new NCompress::NLzma::CDecoder; + Decoder = DecoderSpec; +} + +HRESULT CLzmaDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + Byte buf[9]; + RINOK(ReadStream_FALSE(inStream, buf, 9)); + if (buf[2] != 5 || buf[3] != 0) + return E_NOTIMPL; + RINOK(DecoderSpec->SetDecoderProperties2(buf + 4, 5)); + return Decoder->Code(inStream, outStream, NULL, outSize, progress); +} + +struct CMethodItem +{ + UInt16 ZipMethod; + CMyComPtr Coder; +}; + +class CZipDecoder +{ + NCrypto::NZip::CDecoder *_zipCryptoDecoderSpec; + NCrypto::NZipStrong::CDecoder *_pkAesDecoderSpec; + NCrypto::NWzAes::CDecoder *_wzAesDecoderSpec; + + CMyComPtr _zipCryptoDecoder; + CMyComPtr _pkAesDecoder; + CMyComPtr _wzAesDecoder; + + CFilterCoder *filterStreamSpec; + CMyComPtr filterStream; + CMyComPtr getTextPassword; + CObjectVector methodItems; + +public: + CZipDecoder(): + _zipCryptoDecoderSpec(0), + _pkAesDecoderSpec(0), + _wzAesDecoderSpec(0), + filterStreamSpec(0) {} + + HRESULT Decode( + DECL_EXTERNAL_CODECS_LOC_VARS + CInArchive &archive, const CItemEx &item, + ISequentialOutStream *realOutStream, + IArchiveExtractCallback *extractCallback, + ICompressProgressInfo *compressProgress, + UInt32 numThreads, Int32 &res); +}; + +HRESULT CZipDecoder::Decode( + DECL_EXTERNAL_CODECS_LOC_VARS + CInArchive &archive, const CItemEx &item, + ISequentialOutStream *realOutStream, + IArchiveExtractCallback *extractCallback, + ICompressProgressInfo *compressProgress, + UInt32 numThreads, Int32 &res) +{ + res = NArchive::NExtract::NOperationResult::kDataError; + CInStreamReleaser inStreamReleaser; + + bool needCRC = true; + bool wzAesMode = false; + bool pkAesMode = false; + UInt16 methodId = item.CompressionMethod; + if (item.IsEncrypted()) + { + if (item.IsStrongEncrypted()) + { + CStrongCryptoField f; + if (item.CentralExtra.GetStrongCryptoField(f)) + { + pkAesMode = true; + } + if (!pkAesMode) + { + res = NArchive::NExtract::NOperationResult::kUnSupportedMethod; + return S_OK; + } + } + if (methodId == NFileHeader::NCompressionMethod::kWzAES) + { + CWzAesExtraField aesField; + if (item.CentralExtra.GetWzAesField(aesField)) + { + wzAesMode = true; + needCRC = aesField.NeedCrc(); + } + } + } + + COutStreamWithCRC *outStreamSpec = new COutStreamWithCRC; + CMyComPtr outStream = outStreamSpec; + outStreamSpec->SetStream(realOutStream); + outStreamSpec->Init(needCRC); + + UInt64 authenticationPos; + + CMyComPtr inStream; + { + UInt64 packSize = item.PackSize; + if (wzAesMode) + { + if (packSize < NCrypto::NWzAes::kMacSize) + return S_OK; + packSize -= NCrypto::NWzAes::kMacSize; + } + UInt64 dataPos = item.GetDataPosition(); + inStream.Attach(archive.CreateLimitedStream(dataPos, packSize)); + authenticationPos = dataPos + packSize; + } + + CMyComPtr cryptoFilter; + if (item.IsEncrypted()) + { + if (wzAesMode) + { + CWzAesExtraField aesField; + if (!item.CentralExtra.GetWzAesField(aesField)) + return S_OK; + methodId = aesField.Method; + if (!_wzAesDecoder) + { + _wzAesDecoderSpec = new NCrypto::NWzAes::CDecoder; + _wzAesDecoder = _wzAesDecoderSpec; + } + cryptoFilter = _wzAesDecoder; + Byte properties = aesField.Strength; + RINOK(_wzAesDecoderSpec->SetDecoderProperties2(&properties, 1)); + } + else if (pkAesMode) + { + if (!_pkAesDecoder) + { + _pkAesDecoderSpec = new NCrypto::NZipStrong::CDecoder; + _pkAesDecoder = _pkAesDecoderSpec; + } + cryptoFilter = _pkAesDecoder; + } + else + { + if (!_zipCryptoDecoder) + { + _zipCryptoDecoderSpec = new NCrypto::NZip::CDecoder; + _zipCryptoDecoder = _zipCryptoDecoderSpec; + } + cryptoFilter = _zipCryptoDecoder; + } + CMyComPtr cryptoSetPassword; + RINOK(cryptoFilter.QueryInterface(IID_ICryptoSetPassword, &cryptoSetPassword)); + + if (!getTextPassword) + extractCallback->QueryInterface(IID_ICryptoGetTextPassword, (void **)&getTextPassword); + + if (getTextPassword) + { + CMyComBSTR password; + RINOK(getTextPassword->CryptoGetTextPassword(&password)); + AString charPassword; + if (wzAesMode || pkAesMode) + { + charPassword = UnicodeStringToMultiByte((const wchar_t *)password, CP_ACP); + /* + for (int i = 0;; i++) + { + wchar_t c = password[i]; + if (c == 0) + break; + if (c >= 0x80) + { + res = NArchive::NExtract::NOperationResult::kDataError; + return S_OK; + } + charPassword += (char)c; + } + */ + } + else + { + // we use OEM. WinZip/Windows probably use ANSI for some files + charPassword = UnicodeStringToMultiByte((const wchar_t *)password, CP_OEMCP); + } + HRESULT result = cryptoSetPassword->CryptoSetPassword( + (const Byte *)(const char *)charPassword, charPassword.Length()); + if (result != S_OK) + return S_OK; + } + else + { + RINOK(cryptoSetPassword->CryptoSetPassword(0, 0)); + } + } + + int m; + for (m = 0; m < methodItems.Size(); m++) + if (methodItems[m].ZipMethod == methodId) + break; + + if (m == methodItems.Size()) + { + CMethodItem mi; + mi.ZipMethod = methodId; + if (methodId == NFileHeader::NCompressionMethod::kStored) + mi.Coder = new NCompress::CCopyCoder; + else if (methodId == NFileHeader::NCompressionMethod::kShrunk) + mi.Coder = new NCompress::NShrink::CDecoder; + else if (methodId == NFileHeader::NCompressionMethod::kImploded) + mi.Coder = new NCompress::NImplode::NDecoder::CCoder; + else if (methodId == NFileHeader::NCompressionMethod::kLZMA) + mi.Coder = new CLzmaDecoder; + else + { + CMethodId szMethodID; + if (methodId == NFileHeader::NCompressionMethod::kBZip2) + szMethodID = kMethodId_BZip2; + else + { + if (methodId > 0xFF) + { + res = NArchive::NExtract::NOperationResult::kUnSupportedMethod; + return S_OK; + } + szMethodID = kMethodId_ZipBase + (Byte)methodId; + } + + RINOK(CreateCoder(EXTERNAL_CODECS_LOC_VARS szMethodID, mi.Coder, false)); + + if (mi.Coder == 0) + { + res = NArchive::NExtract::NOperationResult::kUnSupportedMethod; + return S_OK; + } + } + m = methodItems.Add(mi); + } + ICompressCoder *coder = methodItems[m].Coder; + + { + CMyComPtr setDecoderProperties; + coder->QueryInterface(IID_ICompressSetDecoderProperties2, (void **)&setDecoderProperties); + if (setDecoderProperties) + { + Byte properties = (Byte)item.Flags; + RINOK(setDecoderProperties->SetDecoderProperties2(&properties, 1)); + } + } + + #ifdef COMPRESS_MT + { + CMyComPtr setCoderMt; + coder->QueryInterface(IID_ICompressSetCoderMt, (void **)&setCoderMt); + if (setCoderMt) + { + RINOK(setCoderMt->SetNumberOfThreads(numThreads)); + } + } + #endif + + { + HRESULT result = S_OK; + CMyComPtr inStreamNew; + if (item.IsEncrypted()) + { + if (!filterStream) + { + filterStreamSpec = new CFilterCoder; + filterStream = filterStreamSpec; + } + filterStreamSpec->Filter = cryptoFilter; + if (wzAesMode) + { + result = _wzAesDecoderSpec->ReadHeader(inStream); + } + else if (pkAesMode) + { + result =_pkAesDecoderSpec->ReadHeader(inStream, item.FileCRC, item.UnPackSize); + if (result == S_OK) + { + bool passwOK; + result = _pkAesDecoderSpec->CheckPassword(passwOK); + if (result == S_OK && !passwOK) + result = S_FALSE; + } + } + else + { + result = _zipCryptoDecoderSpec->ReadHeader(inStream); + } + + if (result == S_OK) + { + RINOK(filterStreamSpec->SetInStream(inStream)); + inStreamReleaser.FilterCoder = filterStreamSpec; + inStreamNew = filterStream; + if (wzAesMode) + { + if (!_wzAesDecoderSpec->CheckPasswordVerifyCode()) + result = S_FALSE; + } + } + } + else + inStreamNew = inStream; + if (result == S_OK) + result = coder->Code(inStreamNew, outStream, NULL, &item.UnPackSize, compressProgress); + if (result == S_FALSE) + return S_OK; + if (result == E_NOTIMPL) + { + res = NArchive::NExtract::NOperationResult::kUnSupportedMethod; + return S_OK; + } + + RINOK(result); + } + bool crcOK = true; + bool authOk = true; + if (needCRC) + crcOK = (outStreamSpec->GetCRC() == item.FileCRC); + if (wzAesMode) + { + inStream.Attach(archive.CreateLimitedStream(authenticationPos, NCrypto::NWzAes::kMacSize)); + if (_wzAesDecoderSpec->CheckMac(inStream, authOk) != S_OK) + authOk = false; + } + + res = ((crcOK && authOk) ? + NArchive::NExtract::NOperationResult::kOK : + NArchive::NExtract::NOperationResult::kCRCError); + return S_OK; +} + + +STDMETHODIMP CHandler::Extract(const UInt32* indices, UInt32 numItems, + Int32 _aTestMode, IArchiveExtractCallback *extractCallback) +{ + COM_TRY_BEGIN + CZipDecoder myDecoder; + bool testMode = (_aTestMode != 0); + UInt64 totalUnPacked = 0, totalPacked = 0; + bool allFilesMode = (numItems == UInt32(-1)); + if (allFilesMode) + numItems = m_Items.Size(); + if(numItems == 0) + return S_OK; + UInt32 i; + for(i = 0; i < numItems; i++) + { + const CItemEx &item = m_Items[allFilesMode ? i : indices[i]]; + totalUnPacked += item.UnPackSize; + totalPacked += item.PackSize; + } + RINOK(extractCallback->SetTotal(totalUnPacked)); + + UInt64 currentTotalUnPacked = 0, currentTotalPacked = 0; + UInt64 currentItemUnPacked, currentItemPacked; + + CLocalProgress *lps = new CLocalProgress; + CMyComPtr progress = lps; + lps->Init(extractCallback, false); + + for (i = 0; i < numItems; i++, currentTotalUnPacked += currentItemUnPacked, + currentTotalPacked += currentItemPacked) + { + currentItemUnPacked = 0; + currentItemPacked = 0; + + lps->InSize = currentTotalPacked; + lps->OutSize = currentTotalUnPacked; + RINOK(lps->SetCur()); + + CMyComPtr realOutStream; + Int32 askMode = testMode ? + NArchive::NExtract::NAskMode::kTest : + NArchive::NExtract::NAskMode::kExtract; + Int32 index = allFilesMode ? i : indices[i]; + + RINOK(extractCallback->GetStream(index, &realOutStream, askMode)); + + CItemEx item = m_Items[index]; + if (!item.FromLocal) + { + HRESULT res = m_Archive.ReadLocalItemAfterCdItem(item); + if (res == S_FALSE) + { + if (item.IsDir() || realOutStream || testMode) + { + RINOK(extractCallback->PrepareOperation(askMode)); + realOutStream.Release(); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kUnSupportedMethod)); + } + continue; + } + RINOK(res); + } + + if (item.IsDir() || item.IgnoreItem()) + { + // if (!testMode) + { + RINOK(extractCallback->PrepareOperation(askMode)); + realOutStream.Release(); + RINOK(extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK)); + } + continue; + } + + currentItemUnPacked = item.UnPackSize; + currentItemPacked = item.PackSize; + + if (!testMode && (!realOutStream)) + continue; + + RINOK(extractCallback->PrepareOperation(askMode)); + +#ifndef COMPRESS_MT +#define _numThreads 1 +#endif + + Int32 res; + RINOK(myDecoder.Decode( + EXTERNAL_CODECS_VARS + m_Archive, item, realOutStream, extractCallback, + progress, + _numThreads, + res)); + realOutStream.Release(); + + RINOK(extractCallback->SetOperationResult(res)) + } + return S_OK; + COM_TRY_END +} + +#ifndef EXTRACT_ONLY +IMPL_ISetCompressCodecsInfo +#endif + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,100 @@ +// Zip/Handler.h + +#ifndef __ZIP_HANDLER_H +#define __ZIP_HANDLER_H + +#include "../../../Common/DynamicBuffer.h" +#include "../../ICoder.h" +#include "../IArchive.h" + +#include "../../Common/CreateCoder.h" + +#include "ZipIn.h" +#include "ZipCompressionMode.h" + +#ifdef COMPRESS_MT +#include "../../../Windows/System.h" +#endif + +namespace NArchive { +namespace NZip { + +class CHandler: + public IInArchive, +#ifndef EXTRACT_ONLY + public IOutArchive, + public ISetProperties, + PUBLIC_ISetCompressCodecsInfo +#endif + public CMyUnknownImp +{ +public: + MY_QUERYINTERFACE_BEGIN2(IInArchive) +#ifndef EXTRACT_ONLY + MY_QUERYINTERFACE_ENTRY(IOutArchive) + MY_QUERYINTERFACE_ENTRY(ISetProperties) + QUERY_ENTRY_ISetCompressCodecsInfo +#endif + MY_QUERYINTERFACE_END + MY_ADDREF_RELEASE + + INTERFACE_IInArchive(;) +#ifndef EXTRACT_ONLY + INTERFACE_IOutArchive(;) + + STDMETHOD(SetProperties)(const wchar_t **names, const PROPVARIANT *values, Int32 numProperties); + + DECL_ISetCompressCodecsInfo +#endif + + CHandler(); +private: + CObjectVector m_Items; + CInArchive m_Archive; + + int m_Level; + int m_MainMethod; + UInt32 m_DicSize; + UInt32 m_Algo; + UInt32 m_NumPasses; + UInt32 m_NumFastBytes; + UInt32 m_NumMatchFinderCycles; + bool m_NumMatchFinderCyclesDefined; + + bool m_IsAesMode; + Byte m_AesKeyMode; + + bool m_WriteNtfsTimeExtra; + bool m_ForseLocal; + bool m_ForseUtf8; + + #ifdef COMPRESS_MT + UInt32 _numThreads; + #endif + + DECL_EXTERNAL_CODECS_VARS + + void InitMethodProperties() + { + m_Level = -1; + m_MainMethod = -1; + m_Algo = + m_DicSize = + m_NumPasses = + m_NumFastBytes = + m_NumMatchFinderCycles = 0xFFFFFFFF; + m_NumMatchFinderCyclesDefined = false; + m_IsAesMode = false; + m_AesKeyMode = 3; // aes-256 + m_WriteNtfsTimeExtra = false; + m_ForseLocal = false; + m_ForseUtf8 = false; + #ifdef COMPRESS_MT + _numThreads = NWindows::NSystem::GetNumberOfProcessors();; + #endif + } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipHeader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipHeader.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,35 @@ +// Archive/Zip/Header.h + +#include "StdAfx.h" + +#include "ZipHeader.h" + +namespace NArchive { +namespace NZip { + +namespace NSignature +{ + UInt32 kLocalFileHeader = 0x04034B50 + 1; + UInt32 kDataDescriptor = 0x08074B50 + 1; + UInt32 kCentralFileHeader = 0x02014B50 + 1; + UInt32 kEndOfCentralDir = 0x06054B50 + 1; + UInt32 kZip64EndOfCentralDir = 0x06064B50 + 1; + UInt32 kZip64EndOfCentralDirLocator = 0x07064B50 + 1; + + class CMarkersInitializer + { + public: + CMarkersInitializer() + { + kLocalFileHeader--; + kDataDescriptor--; + kCentralFileHeader--; + kEndOfCentralDir--; + kZip64EndOfCentralDir--; + kZip64EndOfCentralDirLocator--; + } + } g_MarkerInitializer; +} + +}} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipHeader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipHeader.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,265 @@ +// Archive/Zip/Header.h + +#ifndef __ARCHIVE_ZIP_HEADER_H +#define __ARCHIVE_ZIP_HEADER_H + +#include "../../../Common/Types.h" + +namespace NArchive { +namespace NZip { + +namespace NSignature +{ + extern UInt32 kLocalFileHeader; + extern UInt32 kDataDescriptor; + extern UInt32 kCentralFileHeader; + extern UInt32 kEndOfCentralDir; + extern UInt32 kZip64EndOfCentralDir; + extern UInt32 kZip64EndOfCentralDirLocator; + + static const UInt32 kMarkerSize = 4; +} + +const UInt32 kEcdSize = 22; +const UInt32 kZip64EcdSize = 44; +const UInt32 kZip64EcdLocatorSize = 20; +/* +struct CEndOfCentralDirectoryRecord +{ + UInt16 ThisDiskNumber; + UInt16 StartCentralDirectoryDiskNumber; + UInt16 NumEntriesInCentaralDirectoryOnThisDisk; + UInt16 NumEntriesInCentaralDirectory; + UInt32 CentralDirectorySize; + UInt32 CentralDirectoryStartOffset; + UInt16 CommentSize; +}; + +struct CEndOfCentralDirectoryRecordFull +{ + UInt32 Signature; + CEndOfCentralDirectoryRecord Header; +}; +*/ + +namespace NFileHeader +{ + /* + struct CVersion + { + Byte Version; + Byte HostOS; + }; + */ + + namespace NCompressionMethod + { + enum EType + { + kStored = 0, + kShrunk = 1, + kReduced1 = 2, + kReduced2 = 3, + kReduced3 = 4, + kReduced4 = 5, + kImploded = 6, + kReservedTokenizing = 7, // reserved for tokenizing + kDeflated = 8, + kDeflated64 = 9, + kPKImploding = 10, + + kBZip2 = 12, + kLZMA = 14, + kTerse = 18, + kLz77 = 19, + kJpeg = 0x60, + kWavPack = 0x61, + kPPMd = 0x62, + kWzAES = 0x63 + }; + const int kNumCompressionMethods = 11; + const Byte kMadeByProgramVersion = 20; + + const Byte kDeflateExtractVersion = 20; + const Byte kStoreExtractVersion = 10; + + const Byte kSupportedVersion = 20; + } + + namespace NExtraID + { + enum + { + kZip64 = 0x01, + kNTFS = 0x0A, + kStrongEncrypt = 0x17, + kWzAES = 0x9901 + }; + } + + namespace NNtfsExtra + { + const UInt16 kTagTime = 1; + enum + { + kMTime = 0, + kATime = 1, + kCTime = 2 + }; + } + + const UInt32 kLocalBlockSize = 26; + /* + struct CLocalBlock + { + CVersion ExtractVersion; + + UInt16 Flags; + UInt16 CompressionMethod; + UInt32 Time; + UInt32 FileCRC; + UInt32 PackSize; + UInt32 UnPackSize; + UInt16 NameSize; + UInt16 ExtraSize; + }; + */ + + const UInt32 kDataDescriptorSize = 16; + // const UInt32 kDataDescriptor64Size = 16 + 8; + /* + struct CDataDescriptor + { + UInt32 Signature; + UInt32 FileCRC; + UInt32 PackSize; + UInt32 UnPackSize; + }; + + struct CLocalBlockFull + { + UInt32 Signature; + CLocalBlock Header; + }; + */ + + const UInt32 kCentralBlockSize = 42; + /* + struct CBlock + { + CVersion MadeByVersion; + CVersion ExtractVersion; + UInt16 Flags; + UInt16 CompressionMethod; + UInt32 Time; + UInt32 FileCRC; + UInt32 PackSize; + UInt32 UnPackSize; + UInt16 NameSize; + UInt16 ExtraSize; + UInt16 CommentSize; + UInt16 DiskNumberStart; + UInt16 InternalAttributes; + UInt32 ExternalAttributes; + UInt32 LocalHeaderOffset; + }; + + struct CBlockFull + { + UInt32 Signature; + CBlock Header; + }; + */ + + namespace NFlags + { + const int kEncrypted = 1 << 0; + const int kLzmaEOS = 1 << 1; + const int kDescriptorUsedMask = 1 << 3; + const int kStrongEncrypted = 1 << 6; + const int kUtf8 = 1 << 11; + + const int kImplodeDictionarySizeMask = 1 << 1; + const int kImplodeLiteralsOnMask = 1 << 2; + + const int kDeflateTypeBitStart = 1; + const int kNumDeflateTypeBits = 2; + const int kNumDeflateTypes = (1 << kNumDeflateTypeBits); + const int kDeflateTypeMask = (1 << kNumDeflateTypeBits) - 1; + } + + namespace NHostOS + { + enum EEnum + { + kFAT = 0, + kAMIGA = 1, + kVMS = 2, // VAX/VMS + kUnix = 3, + kVM_CMS = 4, + kAtari = 5, // what if it's a minix filesystem? [cjh] + kHPFS = 6, // filesystem used by OS/2 (and NT 3.x) + kMac = 7, + kZ_System = 8, + kCPM = 9, + kTOPS20 = 10, // pkzip 2.50 NTFS + kNTFS = 11, // filesystem used by Windows NT + kQDOS = 12, // SMS/QDOS + kAcorn = 13, // Archimedes Acorn RISC OS + kVFAT = 14, // filesystem used by Windows 95, NT + kMVS = 15, + kBeOS = 16, // hybrid POSIX/database filesystem + kTandem = 17, + kOS400 = 18, + kOSX = 19 + }; + } + namespace NUnixAttribute + { + const UInt32 kIFMT = 0170000; /* Unix file type mask */ + + const UInt32 kIFDIR = 0040000; /* Unix directory */ + const UInt32 kIFREG = 0100000; /* Unix regular file */ + const UInt32 kIFSOCK = 0140000; /* Unix socket (BSD, not SysV or Amiga) */ + const UInt32 kIFLNK = 0120000; /* Unix symbolic link (not SysV, Amiga) */ + const UInt32 kIFBLK = 0060000; /* Unix block special (not Amiga) */ + const UInt32 kIFCHR = 0020000; /* Unix character special (not Amiga) */ + const UInt32 kIFIFO = 0010000; /* Unix fifo (BCC, not MSC or Amiga) */ + + const UInt32 kISUID = 04000; /* Unix set user id on execution */ + const UInt32 kISGID = 02000; /* Unix set group id on execution */ + const UInt32 kISVTX = 01000; /* Unix directory permissions control */ + const UInt32 kENFMT = kISGID; /* Unix record locking enforcement flag */ + const UInt32 kIRWXU = 00700; /* Unix read, write, execute: owner */ + const UInt32 kIRUSR = 00400; /* Unix read permission: owner */ + const UInt32 kIWUSR = 00200; /* Unix write permission: owner */ + const UInt32 kIXUSR = 00100; /* Unix execute permission: owner */ + const UInt32 kIRWXG = 00070; /* Unix read, write, execute: group */ + const UInt32 kIRGRP = 00040; /* Unix read permission: group */ + const UInt32 kIWGRP = 00020; /* Unix write permission: group */ + const UInt32 kIXGRP = 00010; /* Unix execute permission: group */ + const UInt32 kIRWXO = 00007; /* Unix read, write, execute: other */ + const UInt32 kIROTH = 00004; /* Unix read permission: other */ + const UInt32 kIWOTH = 00002; /* Unix write permission: other */ + const UInt32 kIXOTH = 00001; /* Unix execute permission: other */ + } + + namespace NAmigaAttribute + { + const UInt32 kIFMT = 06000; /* Amiga file type mask */ + const UInt32 kIFDIR = 04000; /* Amiga directory */ + const UInt32 kIFREG = 02000; /* Amiga regular file */ + const UInt32 kIHIDDEN = 00200; /* to be supported in AmigaDOS 3.x */ + const UInt32 kISCRIPT = 00100; /* executable script (text command file) */ + const UInt32 kIPURE = 00040; /* allow loading into resident memory */ + const UInt32 kIARCHIVE = 00020; /* not modified since bit was last set */ + const UInt32 kIREAD = 00010; /* can be opened for reading */ + const UInt32 kIWRITE = 00004; /* can be opened for writing */ + const UInt32 kIEXECUTE = 00002; /* executable image, a loadable runfile */ + const UInt32 kIDELETE = 00001; /* can be deleted */ + } +} + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipIn.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,868 @@ +// Archive/ZipIn.cpp + +#include "StdAfx.h" + +#include "ZipIn.h" +#include "Windows/Defs.h" +#include "Common/StringConvert.h" +#include "Common/DynamicBuffer.h" +#include "../../Common/LimitedStreams.h" +#include "../../Common/StreamUtils.h" + +extern "C" +{ + #include "../../../../C/CpuArch.h" +} + +#define Get16(p) GetUi16(p) +#define Get32(p) GetUi32(p) +#define Get64(p) GetUi64(p) + +namespace NArchive { +namespace NZip { + +// static const char kEndOfString = '\0'; + +HRESULT CInArchive::Open(IInStream *stream, const UInt64 *searchHeaderSizeLimit) +{ + Close(); + RINOK(stream->Seek(0, STREAM_SEEK_CUR, &m_StreamStartPosition)); + m_Position = m_StreamStartPosition; + RINOK(FindAndReadMarker(stream, searchHeaderSizeLimit)); + RINOK(stream->Seek(m_Position, STREAM_SEEK_SET, NULL)); + m_Stream = stream; + return S_OK; +} + +void CInArchive::Close() +{ + m_Stream.Release(); +} + +HRESULT CInArchive::Seek(UInt64 offset) +{ + return m_Stream->Seek(offset, STREAM_SEEK_SET, NULL); +} + +////////////////////////////////////// +// Markers + +static inline bool TestMarkerCandidate(const Byte *p, UInt32 &value) +{ + value = Get32(p); + return + (value == NSignature::kLocalFileHeader) || + (value == NSignature::kEndOfCentralDir); +} + +static const UInt32 kNumMarkerAddtionalBytes = 2; +static inline bool TestMarkerCandidate2(const Byte *p, UInt32 &value) +{ + value = Get32(p); + if (value == NSignature::kEndOfCentralDir) + return (Get16(p + 4) == 0); + return (value == NSignature::kLocalFileHeader && p[4] < 128); +} + +HRESULT CInArchive::FindAndReadMarker(IInStream *stream, const UInt64 *searchHeaderSizeLimit) +{ + m_ArchiveInfo.Clear(); + m_Position = m_StreamStartPosition; + + Byte marker[NSignature::kMarkerSize]; + RINOK(ReadStream_FALSE(stream, marker, NSignature::kMarkerSize)); + m_Position += NSignature::kMarkerSize; + if (TestMarkerCandidate(marker, m_Signature)) + return S_OK; + + CByteDynamicBuffer dynamicBuffer; + const UInt32 kSearchMarkerBufferSize = 0x10000; + dynamicBuffer.EnsureCapacity(kSearchMarkerBufferSize); + Byte *buffer = dynamicBuffer; + UInt32 numBytesPrev = NSignature::kMarkerSize - 1; + memcpy(buffer, marker + 1, numBytesPrev); + UInt64 curTestPos = m_StreamStartPosition + 1; + for (;;) + { + if (searchHeaderSizeLimit != NULL) + if (curTestPos - m_StreamStartPosition > *searchHeaderSizeLimit) + break; + size_t numReadBytes = kSearchMarkerBufferSize - numBytesPrev; + RINOK(ReadStream(stream, buffer + numBytesPrev, &numReadBytes)); + m_Position += numReadBytes; + UInt32 numBytesInBuffer = numBytesPrev + (UInt32)numReadBytes; + const UInt32 kMarker2Size = NSignature::kMarkerSize + kNumMarkerAddtionalBytes; + if (numBytesInBuffer < kMarker2Size) + break; + UInt32 numTests = numBytesInBuffer - kMarker2Size + 1; + for (UInt32 pos = 0; pos < numTests; pos++) + { + if (buffer[pos] != 0x50) + continue; + if (TestMarkerCandidate2(buffer + pos, m_Signature)) + { + curTestPos += pos; + m_ArchiveInfo.StartPosition = curTestPos; + m_Position = curTestPos + NSignature::kMarkerSize; + return S_OK; + } + } + curTestPos += numTests; + numBytesPrev = numBytesInBuffer - numTests; + memmove(buffer, buffer + numTests, numBytesPrev); + } + return S_FALSE; +} + +HRESULT CInArchive::ReadBytes(void *data, UInt32 size, UInt32 *processedSize) +{ + size_t realProcessedSize = size; + HRESULT result = ReadStream(m_Stream, data, &realProcessedSize); + if (processedSize != NULL) + *processedSize = (UInt32)realProcessedSize; + m_Position += realProcessedSize; + return result; +} + +void CInArchive::IncreaseRealPosition(UInt64 addValue) +{ + if (m_Stream->Seek(addValue, STREAM_SEEK_CUR, &m_Position) != S_OK) + throw CInArchiveException(CInArchiveException::kSeekStreamError); +} + +bool CInArchive::ReadBytesAndTestSize(void *data, UInt32 size) +{ + UInt32 realProcessedSize; + if (ReadBytes(data, size, &realProcessedSize) != S_OK) + throw CInArchiveException(CInArchiveException::kReadStreamError); + return (realProcessedSize == size); +} + +void CInArchive::SafeReadBytes(void *data, UInt32 size) +{ + if (!ReadBytesAndTestSize(data, size)) + throw CInArchiveException(CInArchiveException::kUnexpectedEndOfArchive); +} + +void CInArchive::ReadBuffer(CByteBuffer &buffer, UInt32 size) +{ + buffer.SetCapacity(size); + if (size > 0) + SafeReadBytes(buffer, size); +} + +Byte CInArchive::ReadByte() +{ + Byte b; + SafeReadBytes(&b, 1); + return b; +} + +UInt16 CInArchive::ReadUInt16() +{ + UInt16 value = 0; + for (int i = 0; i < 2; i++) + value |= (((UInt16)ReadByte()) << (8 * i)); + return value; +} + +UInt32 CInArchive::ReadUInt32() +{ + UInt32 value = 0; + for (int i = 0; i < 4; i++) + value |= (((UInt32)ReadByte()) << (8 * i)); + return value; +} + +UInt64 CInArchive::ReadUInt64() +{ + UInt64 value = 0; + for (int i = 0; i < 8; i++) + value |= (((UInt64)ReadByte()) << (8 * i)); + return value; +} + +bool CInArchive::ReadUInt32(UInt32 &value) +{ + value = 0; + for (int i = 0; i < 4; i++) + { + Byte b; + if (!ReadBytesAndTestSize(&b, 1)) + return false; + value |= (UInt32(b) << (8 * i)); + } + return true; +} + + +AString CInArchive::ReadFileName(UInt32 nameSize) +{ + if (nameSize == 0) + return AString(); + char *p = m_NameBuffer.GetBuffer(nameSize); + SafeReadBytes(p, nameSize); + p[nameSize] = 0; + m_NameBuffer.ReleaseBuffer(); + return m_NameBuffer; +} + +void CInArchive::GetArchiveInfo(CInArchiveInfo &archiveInfo) const +{ + archiveInfo = m_ArchiveInfo; +} + +/* +void CInArchive::ThrowIncorrectArchiveException() +{ + throw CInArchiveException(CInArchiveException::kIncorrectArchive); +} +*/ + +static UInt32 GetUInt32(const Byte *data) +{ + return + ((UInt32)(Byte)data[0]) | + (((UInt32)(Byte)data[1]) << 8) | + (((UInt32)(Byte)data[2]) << 16) | + (((UInt32)(Byte)data[3]) << 24); +} + +/* +static UInt16 GetUInt16(const Byte *data) +{ + return + ((UInt16)(Byte)data[0]) | + (((UInt16)(Byte)data[1]) << 8); +} +*/ + +static UInt64 GetUInt64(const Byte *data) +{ + return GetUInt32(data) | ((UInt64)GetUInt32(data + 4) << 32); +} + + + +void CInArchive::ReadExtra(UInt32 extraSize, CExtraBlock &extraBlock, + UInt64 &unpackSize, UInt64 &packSize, UInt64 &localHeaderOffset, UInt32 &diskStartNumber) +{ + extraBlock.Clear(); + UInt32 remain = extraSize; + while(remain >= 4) + { + CExtraSubBlock subBlock; + subBlock.ID = ReadUInt16(); + UInt32 dataSize = ReadUInt16(); + remain -= 4; + if (dataSize > remain) // it's bug + dataSize = remain; + if (subBlock.ID == NFileHeader::NExtraID::kZip64) + { + if (unpackSize == 0xFFFFFFFF) + { + if (dataSize < 8) + break; + unpackSize = ReadUInt64(); + remain -= 8; + dataSize -= 8; + } + if (packSize == 0xFFFFFFFF) + { + if (dataSize < 8) + break; + packSize = ReadUInt64(); + remain -= 8; + dataSize -= 8; + } + if (localHeaderOffset == 0xFFFFFFFF) + { + if (dataSize < 8) + break; + localHeaderOffset = ReadUInt64(); + remain -= 8; + dataSize -= 8; + } + if (diskStartNumber == 0xFFFF) + { + if (dataSize < 4) + break; + diskStartNumber = ReadUInt32(); + remain -= 4; + dataSize -= 4; + } + for (UInt32 i = 0; i < dataSize; i++) + ReadByte(); + } + else + { + ReadBuffer(subBlock.Data, dataSize); + extraBlock.SubBlocks.Add(subBlock); + } + remain -= dataSize; + } + IncreaseRealPosition(remain); +} + +HRESULT CInArchive::ReadLocalItem(CItemEx &item) +{ + item.ExtractVersion.Version = ReadByte(); + item.ExtractVersion.HostOS = ReadByte(); + item.Flags = ReadUInt16(); + item.CompressionMethod = ReadUInt16(); + item.Time = ReadUInt32(); + item.FileCRC = ReadUInt32(); + item.PackSize = ReadUInt32(); + item.UnPackSize = ReadUInt32(); + UInt32 fileNameSize = ReadUInt16(); + item.LocalExtraSize = ReadUInt16(); + item.Name = ReadFileName(fileNameSize); + item.FileHeaderWithNameSize = 4 + NFileHeader::kLocalBlockSize + fileNameSize; + if (item.LocalExtraSize > 0) + { + UInt64 localHeaderOffset = 0; + UInt32 diskStartNumber = 0; + ReadExtra(item.LocalExtraSize, item.LocalExtra, item.UnPackSize, item.PackSize, + localHeaderOffset, diskStartNumber); + } + /* + if (item.IsDir()) + item.UnPackSize = 0; // check It + */ + return S_OK; +} + +HRESULT CInArchive::ReadLocalItemAfterCdItem(CItemEx &item) +{ + if (item.FromLocal) + return S_OK; + try + { + RINOK(Seek(m_ArchiveInfo.Base + item.LocalHeaderPosition)); + CItemEx localItem; + if (ReadUInt32() != NSignature::kLocalFileHeader) + return S_FALSE; + RINOK(ReadLocalItem(localItem)); + if (item.Flags != localItem.Flags) + { + if ( + (item.CompressionMethod != NFileHeader::NCompressionMethod::kDeflated || + (item.Flags & 0x7FF9) != (localItem.Flags & 0x7FF9)) && + (item.CompressionMethod != NFileHeader::NCompressionMethod::kStored || + (item.Flags & 0x7FFF) != (localItem.Flags & 0x7FFF)) && + (item.CompressionMethod != NFileHeader::NCompressionMethod::kImploded || + (item.Flags & 0x7FFF) != (localItem.Flags & 0x7FFF)) + ) + return S_FALSE; + } + + if (item.CompressionMethod != localItem.CompressionMethod || + // item.Time != localItem.Time || + (!localItem.HasDescriptor() && + ( + item.FileCRC != localItem.FileCRC || + item.PackSize != localItem.PackSize || + item.UnPackSize != localItem.UnPackSize + ) + ) || + item.Name.Length() != localItem.Name.Length() + ) + return S_FALSE; + item.FileHeaderWithNameSize = localItem.FileHeaderWithNameSize; + item.LocalExtraSize = localItem.LocalExtraSize; + item.LocalExtra = localItem.LocalExtra; + item.FromLocal = true; + } + catch(...) { return S_FALSE; } + return S_OK; +} + +HRESULT CInArchive::ReadLocalItemDescriptor(CItemEx &item) +{ + if (item.HasDescriptor()) + { + const int kBufferSize = (1 << 12); + Byte buffer[kBufferSize]; + + UInt32 numBytesInBuffer = 0; + UInt32 packedSize = 0; + + bool descriptorWasFound = false; + for (;;) + { + UInt32 processedSize; + RINOK(ReadBytes(buffer + numBytesInBuffer, kBufferSize - numBytesInBuffer, &processedSize)); + numBytesInBuffer += processedSize; + if (numBytesInBuffer < NFileHeader::kDataDescriptorSize) + return S_FALSE; + UInt32 i; + for (i = 0; i <= numBytesInBuffer - NFileHeader::kDataDescriptorSize; i++) + { + // descriptorSignature field is Info-ZIP's extension + // to Zip specification. + UInt32 descriptorSignature = GetUInt32(buffer + i); + + // !!!! It must be fixed for Zip64 archives + UInt32 descriptorPackSize = GetUInt32(buffer + i + 8); + if (descriptorSignature== NSignature::kDataDescriptor && descriptorPackSize == packedSize + i) + { + descriptorWasFound = true; + item.FileCRC = GetUInt32(buffer + i + 4); + item.PackSize = descriptorPackSize; + item.UnPackSize = GetUInt32(buffer + i + 12); + IncreaseRealPosition(Int64(Int32(0 - (numBytesInBuffer - i - NFileHeader::kDataDescriptorSize)))); + break; + } + } + if (descriptorWasFound) + break; + packedSize += i; + int j; + for (j = 0; i < numBytesInBuffer; i++, j++) + buffer[j] = buffer[i]; + numBytesInBuffer = j; + } + } + else + IncreaseRealPosition(item.PackSize); + return S_OK; +} + +HRESULT CInArchive::ReadLocalItemAfterCdItemFull(CItemEx &item) +{ + if (item.FromLocal) + return S_OK; + try + { + RINOK(ReadLocalItemAfterCdItem(item)); + if (item.HasDescriptor()) + { + RINOK(Seek(m_ArchiveInfo.Base + item.GetDataPosition() + item.PackSize)); + if (ReadUInt32() != NSignature::kDataDescriptor) + return S_FALSE; + UInt32 crc = ReadUInt32(); + UInt64 packSize, unpackSize; + + /* + if (IsZip64) + { + packSize = ReadUInt64(); + unpackSize = ReadUInt64(); + } + else + */ + { + packSize = ReadUInt32(); + unpackSize = ReadUInt32(); + } + + if (crc != item.FileCRC || item.PackSize != packSize || item.UnPackSize != unpackSize) + return S_FALSE; + } + } + catch(...) { return S_FALSE; } + return S_OK; +} + +HRESULT CInArchive::ReadCdItem(CItemEx &item) +{ + item.FromCentral = true; + const int kBufSize = 42; + Byte p[kBufSize]; + SafeReadBytes(p, kBufSize); + item.MadeByVersion.Version = p[0]; + item.MadeByVersion.HostOS = p[1]; + item.ExtractVersion.Version = p[2]; + item.ExtractVersion.HostOS = p[3]; + item.Flags = Get16(p + 4); + item.CompressionMethod = Get16(p + 6); + item.Time = Get32(p + 8); + item.FileCRC = Get32(p + 12); + item.PackSize = Get32(p + 16); + item.UnPackSize = Get32(p + 20); + UInt16 headerNameSize = Get16(p + 24); + UInt16 headerExtraSize = Get16(p + 26); + UInt16 headerCommentSize = Get16(p + 28); + UInt32 headerDiskNumberStart = Get16(p + 30); + item.InternalAttributes = Get16(p + 32); + item.ExternalAttributes = Get32(p + 34); + item.LocalHeaderPosition = Get32(p + 38); + item.Name = ReadFileName(headerNameSize); + + if (headerExtraSize > 0) + { + ReadExtra(headerExtraSize, item.CentralExtra, item.UnPackSize, item.PackSize, + item.LocalHeaderPosition, headerDiskNumberStart); + } + + if (headerDiskNumberStart != 0) + throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported); + + // May be these strings must be deleted + /* + if (item.IsDir()) + item.UnPackSize = 0; + */ + + ReadBuffer(item.Comment, headerCommentSize); + return S_OK; +} + +HRESULT CInArchive::TryEcd64(UInt64 offset, CCdInfo &cdInfo) +{ + RINOK(Seek(offset)); + const UInt32 kEcd64Size = 56; + Byte buf[kEcd64Size]; + if (!ReadBytesAndTestSize(buf, kEcd64Size)) + return S_FALSE; + if (GetUInt32(buf) != NSignature::kZip64EndOfCentralDir) + return S_FALSE; + // cdInfo.NumEntries = GetUInt64(buf + 24); + cdInfo.Size = GetUInt64(buf + 40); + cdInfo.Offset = GetUInt64(buf + 48); + return S_OK; +} + +HRESULT CInArchive::FindCd(CCdInfo &cdInfo) +{ + UInt64 endPosition; + RINOK(m_Stream->Seek(0, STREAM_SEEK_END, &endPosition)); + const UInt32 kBufSizeMax = (1 << 16) + kEcdSize + kZip64EcdLocatorSize; + Byte buf[kBufSizeMax]; + UInt32 bufSize = (endPosition < kBufSizeMax) ? (UInt32)endPosition : kBufSizeMax; + if (bufSize < kEcdSize) + return S_FALSE; + UInt64 startPosition = endPosition - bufSize; + RINOK(m_Stream->Seek(startPosition, STREAM_SEEK_SET, &m_Position)); + if (m_Position != startPosition) + return S_FALSE; + if (!ReadBytesAndTestSize(buf, bufSize)) + return S_FALSE; + for (int i = (int)(bufSize - kEcdSize); i >= 0; i--) + { + if (GetUInt32(buf + i) == NSignature::kEndOfCentralDir) + { + if (i >= kZip64EcdLocatorSize) + { + const Byte *locator = buf + i - kZip64EcdLocatorSize; + if (GetUInt32(locator) == NSignature::kZip64EndOfCentralDirLocator) + { + UInt64 ecd64Offset = GetUInt64(locator + 8); + if (TryEcd64(ecd64Offset, cdInfo) == S_OK) + return S_OK; + if (TryEcd64(m_ArchiveInfo.StartPosition + ecd64Offset, cdInfo) == S_OK) + { + m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition; + return S_OK; + } + } + } + if (GetUInt32(buf + i + 4) == 0) + { + // cdInfo.NumEntries = GetUInt16(buf + i + 10); + cdInfo.Size = GetUInt32(buf + i + 12); + cdInfo.Offset = GetUInt32(buf + i + 16); + UInt64 curPos = endPosition - bufSize + i; + UInt64 cdEnd = cdInfo.Size + cdInfo.Offset; + if (curPos > cdEnd) + m_ArchiveInfo.Base = curPos - cdEnd; + return S_OK; + } + } + } + return S_FALSE; +} + +HRESULT CInArchive::TryReadCd(CObjectVector &items, UInt64 cdOffset, UInt64 cdSize, CProgressVirt *progress) +{ + items.Clear(); + RINOK(m_Stream->Seek(cdOffset, STREAM_SEEK_SET, &m_Position)); + if (m_Position != cdOffset) + return S_FALSE; + while(m_Position - cdOffset < cdSize) + { + if (ReadUInt32() != NSignature::kCentralFileHeader) + return S_FALSE; + CItemEx cdItem; + RINOK(ReadCdItem(cdItem)); + items.Add(cdItem); + if (progress && items.Size() % 1000 == 0) + RINOK(progress->SetCompleted(items.Size())); + } + return (m_Position - cdOffset == cdSize) ? S_OK : S_FALSE; +} + +HRESULT CInArchive::ReadCd(CObjectVector &items, UInt64 &cdOffset, UInt64 &cdSize, CProgressVirt *progress) +{ + m_ArchiveInfo.Base = 0; + CCdInfo cdInfo; + RINOK(FindCd(cdInfo)); + HRESULT res = S_FALSE; + cdSize = cdInfo.Size; + cdOffset = cdInfo.Offset; + res = TryReadCd(items, m_ArchiveInfo.Base + cdOffset, cdSize, progress); + if (res == S_FALSE && m_ArchiveInfo.Base == 0) + { + res = TryReadCd(items, cdInfo.Offset + m_ArchiveInfo.StartPosition, cdSize, progress); + if (res == S_OK) + m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition; + } + if (!ReadUInt32(m_Signature)) + return S_FALSE; + return res; +} + +HRESULT CInArchive::ReadLocalsAndCd(CObjectVector &items, CProgressVirt *progress, UInt64 &cdOffset) +{ + items.Clear(); + while (m_Signature == NSignature::kLocalFileHeader) + { + // FSeek points to next byte after signature + // NFileHeader::CLocalBlock localHeader; + CItemEx item; + item.LocalHeaderPosition = m_Position - m_StreamStartPosition - 4; // points to signature; + RINOK(ReadLocalItem(item)); + item.FromLocal = true; + ReadLocalItemDescriptor(item); + items.Add(item); + if (progress && items.Size() % 100 == 0) + RINOK(progress->SetCompleted(items.Size())); + if (!ReadUInt32(m_Signature)) + break; + } + cdOffset = m_Position - 4; + for (int i = 0; i < items.Size(); i++) + { + if (progress && i % 1000 == 0) + RINOK(progress->SetCompleted(items.Size())); + if (m_Signature != NSignature::kCentralFileHeader) + return S_FALSE; + + CItemEx cdItem; + RINOK(ReadCdItem(cdItem)); + + if (i == 0) + { + if (cdItem.LocalHeaderPosition == 0) + m_ArchiveInfo.Base = m_ArchiveInfo.StartPosition; + } + + int index; + int left = 0, right = items.Size(); + for (;;) + { + if (left >= right) + return S_FALSE; + index = (left + right) / 2; + UInt64 position = items[index].LocalHeaderPosition - m_ArchiveInfo.Base; + if (cdItem.LocalHeaderPosition == position) + break; + if (cdItem.LocalHeaderPosition < position) + right = index; + else + left = index + 1; + } + CItemEx &item = items[index]; + item.LocalHeaderPosition = cdItem.LocalHeaderPosition; + item.MadeByVersion = cdItem.MadeByVersion; + item.CentralExtra = cdItem.CentralExtra; + + if ( + // item.ExtractVersion != cdItem.ExtractVersion || + item.Flags != cdItem.Flags || + item.CompressionMethod != cdItem.CompressionMethod || + // item.Time != cdItem.Time || + item.FileCRC != cdItem.FileCRC) + return S_FALSE; + + if (item.Name.Length() != cdItem.Name.Length() || + item.PackSize != cdItem.PackSize || + item.UnPackSize != cdItem.UnPackSize + ) + return S_FALSE; + item.Name = cdItem.Name; + item.InternalAttributes = cdItem.InternalAttributes; + item.ExternalAttributes = cdItem.ExternalAttributes; + item.Comment = cdItem.Comment; + item.FromCentral = cdItem.FromCentral; + if (!ReadUInt32(m_Signature)) + return S_FALSE; + } + return S_OK; +} + +struct CEcd +{ + UInt16 thisDiskNumber; + UInt16 startCDDiskNumber; + UInt16 numEntriesInCDOnThisDisk; + UInt16 numEntriesInCD; + UInt32 cdSize; + UInt32 cdStartOffset; + UInt16 commentSize; + void Parse(const Byte *p); +}; + +void CEcd::Parse(const Byte *p) +{ + thisDiskNumber = Get16(p); + startCDDiskNumber = Get16(p + 2); + numEntriesInCDOnThisDisk = Get16(p + 4); + numEntriesInCD = Get16(p + 6); + cdSize = Get32(p + 8); + cdStartOffset = Get32(p + 12); + commentSize = Get16(p + 16); +} + +struct CEcd64 +{ + UInt16 versionMade; + UInt16 versionNeedExtract; + UInt32 thisDiskNumber; + UInt32 startCDDiskNumber; + UInt64 numEntriesInCDOnThisDisk; + UInt64 numEntriesInCD; + UInt64 cdSize; + UInt64 cdStartOffset; + void Parse(const Byte *p); + CEcd64() { memset(this, 0, sizeof(*this)); } +}; + +void CEcd64::Parse(const Byte *p) +{ + versionMade = Get16(p); + versionNeedExtract = Get16(p + 2); + thisDiskNumber = Get32(p + 4); + startCDDiskNumber = Get32(p + 8); + numEntriesInCDOnThisDisk = Get64(p + 12); + numEntriesInCD = Get64(p + 20); + cdSize = Get64(p + 28); + cdStartOffset = Get64(p + 36); +} + +#define COPY_ECD_ITEM_16(n) if (!isZip64 || ecd. n != 0xFFFF) ecd64. n = ecd. n; +#define COPY_ECD_ITEM_32(n) if (!isZip64 || ecd. n != 0xFFFFFFFF) ecd64. n = ecd. n; + +HRESULT CInArchive::ReadHeaders(CObjectVector &items, CProgressVirt *progress) +{ + // m_Signature must be kLocalFileHeaderSignature or + // kEndOfCentralDirSignature + // m_Position points to next byte after signature + + IsZip64 = false; + items.Clear(); + + UInt64 cdSize, cdStartOffset; + HRESULT res = ReadCd(items, cdStartOffset, cdSize, progress); + if (res != S_FALSE && res != S_OK) + return res; + + /* + if (res != S_OK) + return res; + res = S_FALSE; + */ + + if (res == S_FALSE) + { + m_ArchiveInfo.Base = 0; + RINOK(m_Stream->Seek(m_ArchiveInfo.StartPosition, STREAM_SEEK_SET, &m_Position)); + if (m_Position != m_ArchiveInfo.StartPosition) + return S_FALSE; + if (!ReadUInt32(m_Signature)) + return S_FALSE; + RINOK(ReadLocalsAndCd(items, progress, cdStartOffset)); + cdSize = (m_Position - 4) - cdStartOffset; + cdStartOffset -= m_ArchiveInfo.Base; + } + + CEcd64 ecd64; + bool isZip64 = false; + UInt64 zip64EcdStartOffset = m_Position - 4 - m_ArchiveInfo.Base; + if (m_Signature == NSignature::kZip64EndOfCentralDir) + { + IsZip64 = isZip64 = true; + UInt64 recordSize = ReadUInt64(); + + const int kBufSize = kZip64EcdSize; + Byte buf[kBufSize]; + SafeReadBytes(buf, kBufSize); + ecd64.Parse(buf); + + IncreaseRealPosition(recordSize - kZip64EcdSize); + if (!ReadUInt32(m_Signature)) + return S_FALSE; + if (ecd64.thisDiskNumber != 0 || ecd64.startCDDiskNumber != 0) + throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported); + if (ecd64.numEntriesInCDOnThisDisk != items.Size() || + ecd64.numEntriesInCD != items.Size() || + ecd64.cdSize != cdSize || + (ecd64.cdStartOffset != cdStartOffset && + (!items.IsEmpty()))) + return S_FALSE; + } + if (m_Signature == NSignature::kZip64EndOfCentralDirLocator) + { + /* UInt32 startEndCDDiskNumber = */ ReadUInt32(); + UInt64 endCDStartOffset = ReadUInt64(); + /* UInt32 numberOfDisks = */ ReadUInt32(); + if (zip64EcdStartOffset != endCDStartOffset) + return S_FALSE; + if (!ReadUInt32(m_Signature)) + return S_FALSE; + } + if (m_Signature != NSignature::kEndOfCentralDir) + return S_FALSE; + + const int kBufSize = kEcdSize - 4; + Byte buf[kBufSize]; + SafeReadBytes(buf, kBufSize); + CEcd ecd; + ecd.Parse(buf); + + COPY_ECD_ITEM_16(thisDiskNumber); + COPY_ECD_ITEM_16(startCDDiskNumber); + COPY_ECD_ITEM_16(numEntriesInCDOnThisDisk); + COPY_ECD_ITEM_16(numEntriesInCD); + COPY_ECD_ITEM_32(cdSize); + COPY_ECD_ITEM_32(cdStartOffset); + + ReadBuffer(m_ArchiveInfo.Comment, ecd.commentSize); + + if (ecd64.thisDiskNumber != 0 || ecd64.startCDDiskNumber != 0) + throw CInArchiveException(CInArchiveException::kMultiVolumeArchiveAreNotSupported); + if ((UInt16)ecd64.numEntriesInCDOnThisDisk != ((UInt16)items.Size()) || + (UInt16)ecd64.numEntriesInCD != ((UInt16)items.Size()) || + (UInt32)ecd64.cdSize != (UInt32)cdSize || + ((UInt32)(ecd64.cdStartOffset) != (UInt32)cdStartOffset && + (!items.IsEmpty()))) + return S_FALSE; + + return S_OK; +} + +ISequentialInStream* CInArchive::CreateLimitedStream(UInt64 position, UInt64 size) +{ + CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream; + CMyComPtr stream(streamSpec); + SeekInArchive(m_ArchiveInfo.Base + position); + streamSpec->SetStream(m_Stream); + streamSpec->Init(size); + return stream.Detach(); +} + +IInStream* CInArchive::CreateStream() +{ + CMyComPtr stream = m_Stream; + return stream.Detach(); +} + +bool CInArchive::SeekInArchive(UInt64 position) +{ + UInt64 newPosition; + if (m_Stream->Seek(position, STREAM_SEEK_SET, &newPosition) != S_OK) + return false; + return (newPosition == position); +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipIn.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,116 @@ +// Archive/ZipIn.h + +#ifndef __ZIP_IN_H +#define __ZIP_IN_H + +#include "../../../Common/MyCom.h" +#include "../../IStream.h" + +#include "ZipHeader.h" +#include "ZipItemEx.h" + +namespace NArchive { +namespace NZip { + +class CInArchiveException +{ +public: + enum ECauseType + { + kUnexpectedEndOfArchive = 0, + kArchiceHeaderCRCError, + kFileHeaderCRCError, + kIncorrectArchive, + kDataDescroptorsAreNotSupported, + kMultiVolumeArchiveAreNotSupported, + kReadStreamError, + kSeekStreamError + } + Cause; + CInArchiveException(ECauseType cause): Cause(cause) {} +}; + +class CInArchiveInfo +{ +public: + UInt64 Base; + UInt64 StartPosition; + CByteBuffer Comment; + CInArchiveInfo(): Base(0), StartPosition(0) {} + void Clear() + { + Base = 0; + StartPosition = 0; + Comment.SetCapacity(0); + } +}; + +class CProgressVirt +{ +public: + STDMETHOD(SetTotal)(UInt64 numFiles) PURE; + STDMETHOD(SetCompleted)(UInt64 numFiles) PURE; +}; + +struct CCdInfo +{ + // UInt64 NumEntries; + UInt64 Size; + UInt64 Offset; +}; + +class CInArchive +{ + CMyComPtr m_Stream; + UInt32 m_Signature; + UInt64 m_StreamStartPosition; + UInt64 m_Position; + AString m_NameBuffer; + + HRESULT Seek(UInt64 offset); + + HRESULT FindAndReadMarker(IInStream *stream, const UInt64 *searchHeaderSizeLimit); + bool ReadUInt32(UInt32 &signature); + AString ReadFileName(UInt32 nameSize); + + HRESULT ReadBytes(void *data, UInt32 size, UInt32 *processedSize); + bool ReadBytesAndTestSize(void *data, UInt32 size); + void SafeReadBytes(void *data, UInt32 size); + void ReadBuffer(CByteBuffer &buffer, UInt32 size); + Byte ReadByte(); + UInt16 ReadUInt16(); + UInt32 ReadUInt32(); + UInt64 ReadUInt64(); + + void IncreaseRealPosition(UInt64 addValue); + + void ReadExtra(UInt32 extraSize, CExtraBlock &extraBlock, + UInt64 &unpackSize, UInt64 &packSize, UInt64 &localHeaderOffset, UInt32 &diskStartNumber); + HRESULT ReadLocalItem(CItemEx &item); + HRESULT ReadLocalItemDescriptor(CItemEx &item); + HRESULT ReadCdItem(CItemEx &item); + HRESULT TryEcd64(UInt64 offset, CCdInfo &cdInfo); + HRESULT FindCd(CCdInfo &cdInfo); + HRESULT TryReadCd(CObjectVector &items, UInt64 cdOffset, UInt64 cdSize, CProgressVirt *progress); + HRESULT ReadCd(CObjectVector &items, UInt64 &cdOffset, UInt64 &cdSize, CProgressVirt *progress); + HRESULT ReadLocalsAndCd(CObjectVector &items, CProgressVirt *progress, UInt64 &cdOffset); +public: + CInArchiveInfo m_ArchiveInfo; + bool IsZip64; + + HRESULT ReadHeaders(CObjectVector &items, CProgressVirt *progress); + HRESULT ReadLocalItemAfterCdItem(CItemEx &item); + HRESULT ReadLocalItemAfterCdItemFull(CItemEx &item); + HRESULT Open(IInStream *stream, const UInt64 *searchHeaderSizeLimit); + void Close(); + void GetArchiveInfo(CInArchiveInfo &archiveInfo) const; + bool SeekInArchive(UInt64 position); + ISequentialInStream *CreateLimitedStream(UInt64 position, UInt64 size); + IInStream* CreateStream(); + + bool IsOpen() const { return m_Stream != NULL; } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipItem.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipItem.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,147 @@ +// Archive/ZipItem.cpp + +#include "StdAfx.h" + +#include "ZipHeader.h" +#include "ZipItem.h" +#include "../Common/ItemNameUtils.h" +#include "../../../../C/CpuArch.h" + +namespace NArchive { +namespace NZip { + +bool operator==(const CVersion &v1, const CVersion &v2) +{ + return (v1.Version == v2.Version) && (v1.HostOS == v2.HostOS); +} + +bool operator!=(const CVersion &v1, const CVersion &v2) +{ + return !(v1 == v2); +} + +bool CExtraSubBlock::ExtractNtfsTime(int index, FILETIME &ft) const +{ + ft.dwHighDateTime = ft.dwLowDateTime = 0; + UInt32 size = (UInt32)Data.GetCapacity(); + if (ID != NFileHeader::NExtraID::kNTFS || size < 32) + return false; + const Byte *p = (const Byte *)Data; + p += 4; // for reserved + size -= 4; + while (size > 4) + { + UInt16 tag = GetUi16(p); + UInt32 attrSize = GetUi16(p + 2); + p += 4; + size -= 4; + if (attrSize > size) + attrSize = size; + + if (tag == NFileHeader::NNtfsExtra::kTagTime && attrSize >= 24) + { + p += 8 * index; + ft.dwLowDateTime = GetUi32(p); + ft.dwHighDateTime = GetUi32(p + 4); + return true; + } + p += attrSize; + size -= attrSize; + } + return false; +} + +bool CLocalItem::IsDir() const +{ + return NItemName::HasTailSlash(Name, GetCodePage()); +} + +bool CItem::IsDir() const +{ + if (NItemName::HasTailSlash(Name, GetCodePage())) + return true; + if (!FromCentral) + return false; + WORD highAttributes = WORD((ExternalAttributes >> 16 ) & 0xFFFF); + switch(MadeByVersion.HostOS) + { + case NFileHeader::NHostOS::kAMIGA: + switch (highAttributes & NFileHeader::NAmigaAttribute::kIFMT) + { + case NFileHeader::NAmigaAttribute::kIFDIR: return true; + case NFileHeader::NAmigaAttribute::kIFREG: return false; + default: return false; // change it throw kUnknownAttributes; + } + case NFileHeader::NHostOS::kFAT: + case NFileHeader::NHostOS::kNTFS: + case NFileHeader::NHostOS::kHPFS: + case NFileHeader::NHostOS::kVFAT: + return ((ExternalAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0); + case NFileHeader::NHostOS::kAtari: + case NFileHeader::NHostOS::kMac: + case NFileHeader::NHostOS::kVMS: + case NFileHeader::NHostOS::kVM_CMS: + case NFileHeader::NHostOS::kAcorn: + case NFileHeader::NHostOS::kMVS: + return false; // change it throw kUnknownAttributes; + default: + /* + switch (highAttributes & NFileHeader::NUnixAttribute::kIFMT) + { + case NFileHeader::NUnixAttribute::kIFDIR: + return true; + default: + return false; + } + */ + return false; + } +} + +UInt32 CLocalItem::GetWinAttributes() const +{ + DWORD winAttributes = 0; + if (IsDir()) + winAttributes |= FILE_ATTRIBUTE_DIRECTORY; + return winAttributes; +} + +UInt32 CItem::GetWinAttributes() const +{ + DWORD winAttributes = 0; + switch(MadeByVersion.HostOS) + { + case NFileHeader::NHostOS::kFAT: + case NFileHeader::NHostOS::kNTFS: + if (FromCentral) + winAttributes = ExternalAttributes; + break; + default: + winAttributes = 0; // must be converted from unix value; + } + if (IsDir()) // test it; + winAttributes |= FILE_ATTRIBUTE_DIRECTORY; + return winAttributes; +} + +void CLocalItem::SetFlagBits(int startBitNumber, int numBits, int value) +{ + UInt16 mask = (UInt16)(((1 << numBits) - 1) << startBitNumber); + Flags &= ~mask; + Flags |= value << startBitNumber; +} + +void CLocalItem::SetBitMask(int bitMask, bool enable) +{ + if(enable) + Flags |= bitMask; + else + Flags &= ~bitMask; +} + +void CLocalItem::SetEncrypted(bool encrypted) + { SetBitMask(NFileHeader::NFlags::kEncrypted, encrypted); } +void CLocalItem::SetUtf8(bool isUtf8) + { SetBitMask(NFileHeader::NFlags::kUtf8, isUtf8); } + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipItem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipItem.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,268 @@ +// Archive/ZipItem.h + +#ifndef __ARCHIVE_ZIP_ITEM_H +#define __ARCHIVE_ZIP_ITEM_H + +#include "../../../Common/Types.h" +#include "../../../Common/MyString.h" +#include "../../../Common/Buffer.h" +#include "../../../Common/UTFConvert.h" +#include "../../../Common/StringConvert.h" + +#include "ZipHeader.h" + +namespace NArchive { +namespace NZip { + +struct CVersion +{ + Byte Version; + Byte HostOS; +}; + +bool operator==(const CVersion &v1, const CVersion &v2); +bool operator!=(const CVersion &v1, const CVersion &v2); + +struct CExtraSubBlock +{ + UInt16 ID; + CByteBuffer Data; + bool ExtractNtfsTime(int index, FILETIME &ft) const; +}; + +struct CWzAesExtraField +{ + UInt16 VendorVersion; // 0x0001 - AE-1, 0x0002 - AE-2, + // UInt16 VendorId; // "AE" + Byte Strength; // 1 - 128-bit , 2 - 192-bit , 3 - 256-bit + UInt16 Method; + + CWzAesExtraField(): VendorVersion(2), Strength(3), Method(0) {} + + bool NeedCrc() const { return (VendorVersion == 1); } + + bool ParseFromSubBlock(const CExtraSubBlock &sb) + { + if (sb.ID != NFileHeader::NExtraID::kWzAES) + return false; + if (sb.Data.GetCapacity() < 7) + return false; + const Byte *p = (const Byte *)sb.Data; + VendorVersion = (((UInt16)p[1]) << 8) | p[0]; + if (p[2] != 'A' || p[3] != 'E') + return false; + Strength = p[4]; + Method = (((UInt16)p[6]) << 16) | p[5]; + return true; + } + void SetSubBlock(CExtraSubBlock &sb) const + { + sb.Data.SetCapacity(7); + sb.ID = NFileHeader::NExtraID::kWzAES; + Byte *p = (Byte *)sb.Data; + p[0] = (Byte)VendorVersion; + p[1] = (Byte)(VendorVersion >> 8); + p[2] = 'A'; + p[3] = 'E'; + p[4] = Strength; + p[5] = (Byte)Method; + p[6] = (Byte)(Method >> 8); + } +}; + +namespace NStrongCryptoFlags +{ + const UInt16 kDES = 0x6601; + const UInt16 kRC2old = 0x6602; + const UInt16 k3DES168 = 0x6603; + const UInt16 k3DES112 = 0x6609; + const UInt16 kAES128 = 0x660E; + const UInt16 kAES192 = 0x660F; + const UInt16 kAES256 = 0x6610; + const UInt16 kRC2 = 0x6702; + const UInt16 kBlowfish = 0x6720; + const UInt16 kTwofish = 0x6721; + const UInt16 kRC4 = 0x6801; +} + +struct CStrongCryptoField +{ + UInt16 Format; + UInt16 AlgId; + UInt16 BitLen; + UInt16 Flags; + + bool ParseFromSubBlock(const CExtraSubBlock &sb) + { + if (sb.ID != NFileHeader::NExtraID::kStrongEncrypt) + return false; + const Byte *p = (const Byte *)sb.Data; + if (sb.Data.GetCapacity() < 8) + return false; + Format = (((UInt16)p[1]) << 8) | p[0]; + AlgId = (((UInt16)p[3]) << 8) | p[2]; + BitLen = (((UInt16)p[5]) << 8) | p[4]; + Flags = (((UInt16)p[7]) << 8) | p[6]; + return (Format == 2); + } +}; + +struct CExtraBlock +{ + CObjectVector SubBlocks; + void Clear() { SubBlocks.Clear(); } + size_t GetSize() const + { + size_t res = 0; + for (int i = 0; i < SubBlocks.Size(); i++) + res += SubBlocks[i].Data.GetCapacity() + 2 + 2; + return res; + } + bool GetWzAesField(CWzAesExtraField &aesField) const + { + for (int i = 0; i < SubBlocks.Size(); i++) + if (aesField.ParseFromSubBlock(SubBlocks[i])) + return true; + return false; + } + + bool GetStrongCryptoField(CStrongCryptoField &f) const + { + for (int i = 0; i < SubBlocks.Size(); i++) + if (f.ParseFromSubBlock(SubBlocks[i])) + return true; + return false; + } + + bool HasWzAesField() const + { + CWzAesExtraField aesField; + return GetWzAesField(aesField); + } + + bool GetNtfsTime(int index, FILETIME &ft) const + { + for (int i = 0; i < SubBlocks.Size(); i++) + { + const CExtraSubBlock &sb = SubBlocks[i]; + if (sb.ID == NFileHeader::NExtraID::kNTFS) + return sb.ExtractNtfsTime(index, ft); + } + return false; + } + + /* + bool HasStrongCryptoField() const + { + CStrongCryptoField f; + return GetStrongCryptoField(f); + } + */ + + void RemoveUnknownSubBlocks() + { + for (int i = SubBlocks.Size() - 1; i >= 0; i--) + if (SubBlocks[i].ID != NFileHeader::NExtraID::kWzAES) + SubBlocks.Delete(i); + } +}; + + +class CLocalItem +{ +public: + CVersion ExtractVersion; + UInt16 Flags; + UInt16 CompressionMethod; + UInt32 Time; + UInt32 FileCRC; + UInt64 PackSize; + UInt64 UnPackSize; + + AString Name; + + CExtraBlock LocalExtra; + + bool IsUtf8() const { return (Flags & NFileHeader::NFlags::kUtf8) != 0; } + + bool IsEncrypted() const { return (Flags & NFileHeader::NFlags::kEncrypted) != 0; } + bool IsStrongEncrypted() const { return IsEncrypted() && (Flags & NFileHeader::NFlags::kStrongEncrypted) != 0; }; + + bool IsLzmaEOS() const { return (Flags & NFileHeader::NFlags::kLzmaEOS) != 0; } + + bool IsDir() const; + bool IgnoreItem() const { return false; } + UInt32 GetWinAttributes() const; + + bool HasDescriptor() const { return (Flags & NFileHeader::NFlags::kDescriptorUsedMask) != 0; } + + UString GetUnicodeString(const AString &s) const + { + UString res; + if (IsUtf8()) + if (!ConvertUTF8ToUnicode(s, res)) + res.Empty(); + if (res.IsEmpty()) + res = MultiByteToUnicodeString(s, GetCodePage()); + return res; + } + +private: + void SetFlagBits(int startBitNumber, int numBits, int value); + void SetBitMask(int bitMask, bool enable); +public: + void ClearFlags() { Flags = 0; } + void SetEncrypted(bool encrypted); + void SetUtf8(bool isUtf8); + + WORD GetCodePage() const { return CP_OEMCP; } +}; + +class CItem: public CLocalItem +{ +public: + CVersion MadeByVersion; + UInt16 InternalAttributes; + UInt32 ExternalAttributes; + + UInt64 LocalHeaderPosition; + + FILETIME NtfsMTime; + FILETIME NtfsATime; + FILETIME NtfsCTime; + + CExtraBlock CentralExtra; + CByteBuffer Comment; + + bool FromLocal; + bool FromCentral; + bool NtfsTimeIsDefined; + + bool IsDir() const; + UInt32 GetWinAttributes() const; + + bool IsThereCrc() const + { + if (CompressionMethod == NFileHeader::NCompressionMethod::kWzAES) + { + CWzAesExtraField aesField; + if (CentralExtra.GetWzAesField(aesField)) + return aesField.NeedCrc(); + } + return (FileCRC != 0 || !IsDir()); + } + + WORD GetCodePage() const + { + return (WORD)((MadeByVersion.HostOS == NFileHeader::NHostOS::kFAT + || MadeByVersion.HostOS == NFileHeader::NHostOS::kNTFS + ) ? CP_OEMCP : CP_ACP); + } + CItem() : FromLocal(false), FromCentral(false), NtfsTimeIsDefined(false) {} +}; + +}} + +#endif + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipItemEx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipItemEx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,34 @@ +// Archive/ZipItemEx.h + +#ifndef __ARCHIVE_ZIP_ITEMEX_H +#define __ARCHIVE_ZIP_ITEMEX_H + +#include "ZipHeader.h" +#include "ZipItem.h" + +namespace NArchive { +namespace NZip { + +class CItemEx: public CItem +{ +public: + UInt32 FileHeaderWithNameSize; + UInt16 LocalExtraSize; + + UInt64 GetLocalFullSize() const + { return FileHeaderWithNameSize + LocalExtraSize + PackSize + + (HasDescriptor() ? NFileHeader::kDataDescriptorSize : 0); }; + /* + UInt64 GetLocalFullSize(bool isZip64) const + { return FileHeaderWithNameSize + LocalExtraSize + PackSize + + (HasDescriptor() ? (isZip64 ? NFileHeader::kDataDescriptor64Size : NFileHeader::kDataDescriptorSize) : 0); }; + */ + UInt64 GetLocalExtraPosition() const + { return LocalHeaderPosition + FileHeaderWithNameSize; }; + UInt64 GetDataPosition() const + { return GetLocalExtraPosition() + LocalExtraSize; }; +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/Zip/ZipRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,21 @@ +// ZipRegister.cpp + +#include "StdAfx.h" + +#include "../../Common/RegisterArc.h" + +#include "ZipHandler.h" +static IInArchive *CreateArc() { return new NArchive::NZip::CHandler; } +#ifndef EXTRACT_ONLY +static IOutArchive *CreateArcOut() { return new NArchive::NZip::CHandler; } +#else +#define CreateArcOut 0 +#endif + +static CArcInfo g_ArcInfo = { L"Zip", L"zip jar xpi zsg", 0, 1, { 0x50, 0x4B, 0x03, 0x04 }, 4, false, CreateArc, CreateArcOut }; +static CArcInfo g_ArcInfo2 = { L"Zip", L"zip jar xpi zsg", 0, 1, { 0x50, 0x4B, 0x01, 0x02 }, 4, false, CreateArc, CreateArcOut }; +static CArcInfo g_ArcInfo3 = { L"Zip", L"zip jar xpi zsg", 0, 1, { 0x50, 0x4B, 0x05, 0x06 }, 4, false, CreateArc, CreateArcOut }; + +REGISTER_ARC(Zip) +REGISTER_ARCN(Zip,2) +REGISTER_ARCN(Zip,3) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/CreateCoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/CreateCoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,292 @@ +// CreateCoder.cpp + +#include "StdAfx.h" + +#include "CreateCoder.h" + +#include "../../Windows/PropVariant.h" +#include "../../Windows/Defs.h" +#include "FilterCoder.h" +#include "RegisterCodec.h" + +static const unsigned int kNumCodecsMax = 64; +unsigned int g_NumCodecs = 0; +const CCodecInfo *g_Codecs[kNumCodecsMax]; +void RegisterCodec(const CCodecInfo *codecInfo) +{ + if (g_NumCodecs < kNumCodecsMax) + g_Codecs[g_NumCodecs++] = codecInfo; +} + +#ifdef EXTERNAL_CODECS +static HRESULT ReadNumberOfStreams(ICompressCodecsInfo *codecsInfo, UInt32 index, PROPID propID, UInt32 &res) +{ + NWindows::NCOM::CPropVariant prop; + RINOK(codecsInfo->GetProperty(index, propID, &prop)); + if (prop.vt == VT_EMPTY) + res = 1; + else if (prop.vt == VT_UI4) + res = prop.ulVal; + else + return E_INVALIDARG; + return S_OK; +} + +static HRESULT ReadIsAssignedProp(ICompressCodecsInfo *codecsInfo, UInt32 index, PROPID propID, bool &res) +{ + NWindows::NCOM::CPropVariant prop; + RINOK(codecsInfo->GetProperty(index, propID, &prop)); + if (prop.vt == VT_EMPTY) + res = true; + else if (prop.vt == VT_BOOL) + res = VARIANT_BOOLToBool(prop.boolVal); + else + return E_INVALIDARG; + return S_OK; +} + +HRESULT LoadExternalCodecs(ICompressCodecsInfo *codecsInfo, CObjectVector &externalCodecs) +{ + UInt32 num; + RINOK(codecsInfo->GetNumberOfMethods(&num)); + for (UInt32 i = 0; i < num; i++) + { + CCodecInfoEx info; + NWindows::NCOM::CPropVariant prop; + RINOK(codecsInfo->GetProperty(i, NMethodPropID::kID, &prop)); + // if (prop.vt != VT_BSTR) + // info.Id.IDSize = (Byte)SysStringByteLen(prop.bstrVal); + // memmove(info.Id.ID, prop.bstrVal, info.Id.IDSize); + if (prop.vt != VT_UI8) + { + continue; // old Interface + // return E_INVALIDARG; + } + info.Id = prop.uhVal.QuadPart; + prop.Clear(); + + RINOK(codecsInfo->GetProperty(i, NMethodPropID::kName, &prop)); + if (prop.vt == VT_BSTR) + info.Name = prop.bstrVal; + else if (prop.vt != VT_EMPTY) + return E_INVALIDARG;; + + RINOK(ReadNumberOfStreams(codecsInfo, i, NMethodPropID::kInStreams, info.NumInStreams)); + RINOK(ReadNumberOfStreams(codecsInfo, i, NMethodPropID::kOutStreams, info.NumOutStreams)); + RINOK(ReadIsAssignedProp(codecsInfo, i, NMethodPropID::kEncoderIsAssigned, info.EncoderIsAssigned)); + RINOK(ReadIsAssignedProp(codecsInfo, i, NMethodPropID::kDecoderIsAssigned, info.DecoderIsAssigned)); + + externalCodecs.Add(info); + } + return S_OK; +} + +#endif + +bool FindMethod( + #ifdef EXTERNAL_CODECS + ICompressCodecsInfo * /* codecsInfo */, const CObjectVector *externalCodecs, + #endif + const UString &name, + CMethodId &methodId, UInt32 &numInStreams, UInt32 &numOutStreams) +{ + UInt32 i; + for (i = 0; i < g_NumCodecs; i++) + { + const CCodecInfo &codec = *g_Codecs[i]; + if (name.CompareNoCase(codec.Name) == 0) + { + methodId = codec.Id; + numInStreams = codec.NumInStreams; + numOutStreams = 1; + return true; + } + } + #ifdef EXTERNAL_CODECS + if (externalCodecs) + for (i = 0; i < (UInt32)externalCodecs->Size(); i++) + { + const CCodecInfoEx &codec = (*externalCodecs)[i]; + if (codec.Name.CompareNoCase(name) == 0) + { + methodId = codec.Id; + numInStreams = codec.NumInStreams; + numOutStreams = codec.NumOutStreams; + return true; + } + } + #endif + return false; +} + +bool FindMethod( + #ifdef EXTERNAL_CODECS + ICompressCodecsInfo * /* codecsInfo */, const CObjectVector *externalCodecs, + #endif + CMethodId methodId, UString &name) +{ + UInt32 i; + for (i = 0; i < g_NumCodecs; i++) + { + const CCodecInfo &codec = *g_Codecs[i]; + if (methodId == codec.Id) + { + name = codec.Name; + return true; + } + } + #ifdef EXTERNAL_CODECS + if (externalCodecs) + for (i = 0; i < (UInt32)externalCodecs->Size(); i++) + { + const CCodecInfoEx &codec = (*externalCodecs)[i]; + if (methodId == codec.Id) + { + name = codec.Name; + return true; + } + } + #endif + return false; +} + +HRESULT CreateCoder( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, + CMyComPtr &filter, + CMyComPtr &coder, + CMyComPtr &coder2, + bool encode, bool onlyCoder) +{ + bool created = false; + UInt32 i; + for (i = 0; i < g_NumCodecs; i++) + { + const CCodecInfo &codec = *g_Codecs[i]; + if (codec.Id == methodId) + { + if (encode) + { + if (codec.CreateEncoder) + { + void *p = codec.CreateEncoder(); + if (codec.IsFilter) filter = (ICompressFilter *)p; + else if (codec.NumInStreams == 1) coder = (ICompressCoder *)p; + else coder2 = (ICompressCoder2 *)p; + created = (p != 0); + break; + } + } + else + if (codec.CreateDecoder) + { + void *p = codec.CreateDecoder(); + if (codec.IsFilter) filter = (ICompressFilter *)p; + else if (codec.NumInStreams == 1) coder = (ICompressCoder *)p; + else coder2 = (ICompressCoder2 *)p; + created = (p != 0); + break; + } + } + } + + #ifdef EXTERNAL_CODECS + if (!created && externalCodecs) + for (i = 0; i < (UInt32)externalCodecs->Size(); i++) + { + const CCodecInfoEx &codec = (*externalCodecs)[i]; + if (codec.Id == methodId) + { + if (encode) + { + if (codec.EncoderIsAssigned) + { + if (codec.IsSimpleCodec()) + { + HRESULT result = codecsInfo->CreateEncoder(i, &IID_ICompressCoder, (void **)&coder); + if (result != S_OK && result != E_NOINTERFACE && result != CLASS_E_CLASSNOTAVAILABLE) + return result; + if (!coder) + { + RINOK(codecsInfo->CreateEncoder(i, &IID_ICompressFilter, (void **)&filter)); + } + } + else + { + RINOK(codecsInfo->CreateEncoder(i, &IID_ICompressCoder2, (void **)&coder2)); + } + break; + } + } + else + if (codec.DecoderIsAssigned) + { + if (codec.IsSimpleCodec()) + { + HRESULT result = codecsInfo->CreateDecoder(i, &IID_ICompressCoder, (void **)&coder); + if (result != S_OK && result != E_NOINTERFACE && result != CLASS_E_CLASSNOTAVAILABLE) + return result; + if (!coder) + { + RINOK(codecsInfo->CreateDecoder(i, &IID_ICompressFilter, (void **)&filter)); + } + } + else + { + RINOK(codecsInfo->CreateDecoder(i, &IID_ICompressCoder2, (void **)&coder2)); + } + break; + } + } + } + #endif + + if (onlyCoder && filter) + { + CFilterCoder *coderSpec = new CFilterCoder; + coder = coderSpec; + coderSpec->Filter = filter; + } + return S_OK; +} + +HRESULT CreateCoder( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, + CMyComPtr &coder, + CMyComPtr &coder2, + bool encode) +{ + CMyComPtr filter; + return CreateCoder( + EXTERNAL_CODECS_LOC_VARS + methodId, + filter, coder, coder2, encode, true); +} + +HRESULT CreateCoder( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, + CMyComPtr &coder, bool encode) +{ + CMyComPtr filter; + CMyComPtr coder2; + return CreateCoder( + EXTERNAL_CODECS_LOC_VARS + methodId, + coder, coder2, encode); +} + +HRESULT CreateFilter( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, + CMyComPtr &filter, + bool encode) +{ + CMyComPtr coder; + CMyComPtr coder2; + return CreateCoder( + EXTERNAL_CODECS_LOC_VARS + methodId, + filter, coder, coder2, encode, false); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/CreateCoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/CreateCoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,98 @@ +// CreateCoder.h + +#ifndef __CREATECODER_H +#define __CREATECODER_H + +#include "../../Common/MyCom.h" +#include "../../Common/MyString.h" +#include "../ICoder.h" + +#include "MethodId.h" + +#ifdef EXTERNAL_CODECS + +struct CCodecInfoEx +{ + UString Name; + CMethodId Id; + UInt32 NumInStreams; + UInt32 NumOutStreams; + bool EncoderIsAssigned; + bool DecoderIsAssigned; + bool IsSimpleCodec() const { return NumOutStreams == 1 && NumInStreams == 1; } + CCodecInfoEx(): EncoderIsAssigned(false), DecoderIsAssigned(false) {} +}; + +HRESULT LoadExternalCodecs(ICompressCodecsInfo *codecsInfo, CObjectVector &externalCodecs); + +#define PUBLIC_ISetCompressCodecsInfo public ISetCompressCodecsInfo, +#define QUERY_ENTRY_ISetCompressCodecsInfo MY_QUERYINTERFACE_ENTRY(ISetCompressCodecsInfo) +#define DECL_ISetCompressCodecsInfo STDMETHOD(SetCompressCodecsInfo)(ICompressCodecsInfo *compressCodecsInfo); +#define IMPL_ISetCompressCodecsInfo2(x) \ +STDMETHODIMP x::SetCompressCodecsInfo(ICompressCodecsInfo *compressCodecsInfo) { \ + COM_TRY_BEGIN _codecsInfo = compressCodecsInfo; return LoadExternalCodecs(_codecsInfo, _externalCodecs); COM_TRY_END } +#define IMPL_ISetCompressCodecsInfo IMPL_ISetCompressCodecsInfo2(CHandler) + +#define EXTERNAL_CODECS_VARS2 _codecsInfo, &_externalCodecs + +#define DECL_EXTERNAL_CODECS_VARS CMyComPtr _codecsInfo; CObjectVector _externalCodecs; +#define EXTERNAL_CODECS_VARS EXTERNAL_CODECS_VARS2, + +#define DECL_EXTERNAL_CODECS_LOC_VARS2 ICompressCodecsInfo *codecsInfo, const CObjectVector *externalCodecs +#define EXTERNAL_CODECS_LOC_VARS2 codecsInfo, externalCodecs + +#define DECL_EXTERNAL_CODECS_LOC_VARS DECL_EXTERNAL_CODECS_LOC_VARS2, +#define EXTERNAL_CODECS_LOC_VARS EXTERNAL_CODECS_LOC_VARS2, + +#else + +#define PUBLIC_ISetCompressCodecsInfo +#define QUERY_ENTRY_ISetCompressCodecsInfo +#define DECL_ISetCompressCodecsInfo +#define IMPL_ISetCompressCodecsInfo +#define EXTERNAL_CODECS_VARS2 +#define DECL_EXTERNAL_CODECS_VARS +#define EXTERNAL_CODECS_VARS EXTERNAL_CODECS_VARS2 +#define DECL_EXTERNAL_CODECS_LOC_VARS2 +#define EXTERNAL_CODECS_LOC_VARS2 +#define DECL_EXTERNAL_CODECS_LOC_VARS +#define EXTERNAL_CODECS_LOC_VARS + +#endif + +bool FindMethod( + DECL_EXTERNAL_CODECS_LOC_VARS + const UString &name, CMethodId &methodId, UInt32 &numInStreams, UInt32 &numOutStreams); + +bool FindMethod( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, UString &name); + + +HRESULT CreateCoder( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, + CMyComPtr &filter, + CMyComPtr &coder, + CMyComPtr &coder2, + bool encode, bool onlyCoder); + +HRESULT CreateCoder( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, + CMyComPtr &coder, + CMyComPtr &coder2, + bool encode); + +HRESULT CreateCoder( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, + CMyComPtr &coder, bool encode); + +HRESULT CreateFilter( + DECL_EXTERNAL_CODECS_LOC_VARS + CMethodId methodId, + CMyComPtr &filter, + bool encode); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/DeclareArcs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/DeclareArcs.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,28 @@ +// DeclareArcs.h + +#ifndef __DECLAREARCS_H +#define __DECLAREARCS_H + +#define DECLARE_ARC(x) struct CRegister##x { CRegister##x(); }; \ + FORCE_REF(CRegister##x, g_RegisterArc##x) + +#define DECLARE_ARCN(x,n) struct CRegister##x##n { CRegister##x##n(); }; \ + FORCE_REF(CRegister##x##n, g_RegisterArc##n##x) + +#ifndef FORCE_REF + #define FORCE_REF(x,y) +#endif + +DECLARE_ARC(7z) +DECLARE_ARC(BZip2) +DECLARE_ARC(GZip) +DECLARE_ARC(Lzh) +DECLARE_ARC(Lzma) +DECLARE_ARC(Rar) +DECLARE_ARC(Split) +DECLARE_ARC(Tar) +DECLARE_ARC(Zip) +DECLARE_ARCN(Zip,2) +DECLARE_ARCN(Zip,3) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/DeclareCodecs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/DeclareCodecs.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,31 @@ +// DeclareCodecs.h + +#ifndef __DECLARECODECS_H +#define __DECLARECODECS_H + +#define DECLARE_CODEC(x) struct CRegisterCodec##x { CRegisterCodec##x(); }; \ + FORCE_REF(CRegisterCodec##x, g_RegisterCodec##x) + +#define DECLARE_CODECS(x) struct CRegisterCodecs##x { CRegisterCodecs##x(); }; \ + FORCE_REF(CRegisterCodecs##x, g_RegisterCodecs##x) + +#ifndef FORCE_REF + #define FORCE_REF(x,y) +#endif + +DECLARE_CODEC(7zAES) +DECLARE_CODEC(BCJ2) +DECLARE_CODEC(BCJ) +DECLARE_CODEC(BZip2) +DECLARE_CODEC(Copy) +DECLARE_CODEC(Deflate64) +DECLARE_CODEC(DeflateNsis) +DECLARE_CODEC(Deflate) +DECLARE_CODEC(LZMA) +DECLARE_CODEC(PPMD) + +DECLARE_CODECS(Branch) +DECLARE_CODECS(ByteSwap) +DECLARE_CODECS(Rar) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/FilterCoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/FilterCoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,262 @@ +// FilterCoder.cpp + +#include "StdAfx.h" + +#include "FilterCoder.h" +extern "C" +{ +#include "../../../C/Alloc.h" +} +#include "../../Common/Defs.h" +#include "StreamUtils.h" + +static const UInt32 kBufferSize = 1 << 17; + +CFilterCoder::CFilterCoder() +{ + _buffer = (Byte *)::MidAlloc(kBufferSize); +} + +CFilterCoder::~CFilterCoder() +{ + ::MidFree(_buffer); +} + +HRESULT CFilterCoder::WriteWithLimit(ISequentialOutStream *outStream, UInt32 size) +{ + if (_outSizeIsDefined) + { + UInt64 remSize = _outSize - _nowPos64; + if (size > remSize) + size = (UInt32)remSize; + } + RINOK(WriteStream(outStream, _buffer, size)); + _nowPos64 += size; + return S_OK; +} + + +STDMETHODIMP CFilterCoder::Code(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 * /* inSize */, const UInt64 *outSize, + ICompressProgressInfo *progress) +{ + RINOK(Init()); + UInt32 bufferPos = 0; + _outSizeIsDefined = (outSize != 0); + if (_outSizeIsDefined) + _outSize = *outSize; + + while(NeedMore()) + { + size_t processedSize = kBufferSize - bufferPos; + + // Change it: It can be optimized using ReadPart + RINOK(ReadStream(inStream, _buffer + bufferPos, &processedSize)); + + UInt32 endPos = bufferPos + (UInt32)processedSize; + + bufferPos = Filter->Filter(_buffer, endPos); + if (bufferPos > endPos) + { + for (; endPos< bufferPos; endPos++) + _buffer[endPos] = 0; + bufferPos = Filter->Filter(_buffer, endPos); + } + + if (bufferPos == 0) + { + if (endPos > 0) + return WriteWithLimit(outStream, endPos); + return S_OK; + } + RINOK(WriteWithLimit(outStream, bufferPos)); + if (progress != NULL) + { + RINOK(progress->SetRatioInfo(&_nowPos64, &_nowPos64)); + } + UInt32 i = 0; + while(bufferPos < endPos) + _buffer[i++] = _buffer[bufferPos++]; + bufferPos = i; + } + return S_OK; +} + +// #ifdef _ST_MODE +STDMETHODIMP CFilterCoder::SetOutStream(ISequentialOutStream *outStream) +{ + _bufferPos = 0; + _outStream = outStream; + return Init(); +} + +STDMETHODIMP CFilterCoder::ReleaseOutStream() +{ + _outStream.Release(); + return S_OK; +}; + + +STDMETHODIMP CFilterCoder::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 processedSizeTotal = 0; + while(size > 0) + { + UInt32 sizeMax = kBufferSize - _bufferPos; + UInt32 sizeTemp = size; + if (sizeTemp > sizeMax) + sizeTemp = sizeMax; + memmove(_buffer + _bufferPos, data, sizeTemp); + size -= sizeTemp; + processedSizeTotal += sizeTemp; + data = (const Byte *)data + sizeTemp; + UInt32 endPos = _bufferPos + sizeTemp; + _bufferPos = Filter->Filter(_buffer, endPos); + if (_bufferPos == 0) + { + _bufferPos = endPos; + break; + } + if (_bufferPos > endPos) + { + if (size != 0) + return E_FAIL; + break; + } + RINOK(WriteWithLimit(_outStream, _bufferPos)); + UInt32 i = 0; + while(_bufferPos < endPos) + _buffer[i++] = _buffer[_bufferPos++]; + _bufferPos = i; + } + if (processedSize != NULL) + *processedSize = processedSizeTotal; + return S_OK; +} + +STDMETHODIMP CFilterCoder::Flush() +{ + if (_bufferPos != 0) + { + UInt32 endPos = Filter->Filter(_buffer, _bufferPos); + if (endPos > _bufferPos) + { + for (; _bufferPos < endPos; _bufferPos++) + _buffer[_bufferPos] = 0; + if (Filter->Filter(_buffer, endPos) != endPos) + return E_FAIL; + } + RINOK(WriteStream(_outStream, _buffer, _bufferPos)); + _bufferPos = 0; + } + CMyComPtr flush; + _outStream.QueryInterface(IID_IOutStreamFlush, &flush); + if (flush) + return flush->Flush(); + return S_OK; +} + + +STDMETHODIMP CFilterCoder::SetInStream(ISequentialInStream *inStream) +{ + _convertedPosBegin = _convertedPosEnd = _bufferPos = 0; + _inStream = inStream; + return Init(); +} + +STDMETHODIMP CFilterCoder::ReleaseInStream() +{ + _inStream.Release(); + return S_OK; +}; + +STDMETHODIMP CFilterCoder::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 processedSizeTotal = 0; + while(size > 0) + { + if (_convertedPosBegin != _convertedPosEnd) + { + UInt32 sizeTemp = MyMin(size, _convertedPosEnd - _convertedPosBegin); + memmove(data, _buffer + _convertedPosBegin, sizeTemp); + _convertedPosBegin += sizeTemp; + data = (void *)((Byte *)data + sizeTemp); + size -= sizeTemp; + processedSizeTotal += sizeTemp; + break; + } + int i; + for (i = 0; _convertedPosEnd + i < _bufferPos; i++) + _buffer[i] = _buffer[i + _convertedPosEnd]; + _bufferPos = i; + _convertedPosBegin = _convertedPosEnd = 0; + size_t processedSizeTemp = kBufferSize - _bufferPos; + RINOK(ReadStream(_inStream, _buffer + _bufferPos, &processedSizeTemp)); + _bufferPos = _bufferPos + (UInt32)processedSizeTemp; + _convertedPosEnd = Filter->Filter(_buffer, _bufferPos); + if (_convertedPosEnd == 0) + { + if (_bufferPos == 0) + break; + else + { + _convertedPosEnd = _bufferPos; // check it + continue; + } + } + if (_convertedPosEnd > _bufferPos) + { + for (; _bufferPos < _convertedPosEnd; _bufferPos++) + _buffer[_bufferPos] = 0; + _convertedPosEnd = Filter->Filter(_buffer, _bufferPos); + } + } + if (processedSize != NULL) + *processedSize = processedSizeTotal; + return S_OK; +} + +// #endif // _ST_MODE + +#ifndef _NO_CRYPTO +STDMETHODIMP CFilterCoder::CryptoSetPassword(const Byte *data, UInt32 size) +{ + return _setPassword->CryptoSetPassword(data, size); +} +#endif + +#ifndef EXTRACT_ONLY +STDMETHODIMP CFilterCoder::SetCoderProperties(const PROPID *propIDs, + const PROPVARIANT *properties, UInt32 numProperties) +{ + return _SetCoderProperties->SetCoderProperties(propIDs, properties, numProperties); +} + +STDMETHODIMP CFilterCoder::WriteCoderProperties(ISequentialOutStream *outStream) +{ + return _writeCoderProperties->WriteCoderProperties(outStream); +} + +/* +STDMETHODIMP CFilterCoder::ResetSalt() +{ + return _CryptoResetSalt->ResetSalt(); +} +*/ + +STDMETHODIMP CFilterCoder::ResetInitVector() +{ + return _CryptoResetInitVector->ResetInitVector(); +} +#endif + +STDMETHODIMP CFilterCoder::SetDecoderProperties2(const Byte *data, UInt32 size) +{ + return _setDecoderProperties->SetDecoderProperties2(data, size); +} + + + +void foo() +{ +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/FilterCoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/FilterCoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,143 @@ +// FilterCoder.h + +#ifndef __FILTERCODER_H +#define __FILTERCODER_H + +#include "../../Common/MyCom.h" +#include "../ICoder.h" +#include "../IPassword.h" + +#define MY_QUERYINTERFACE_ENTRY_AG(i, sub0, sub) if (iid == IID_ ## i) \ +{ if (!sub) RINOK(sub0->QueryInterface(IID_ ## i, (void **)&sub)) \ +*outObject = (void *)(i *)this; AddRef(); return S_OK; } + +class CFilterCoder: + public ICompressCoder, + // #ifdef _ST_MODE + public ICompressSetInStream, + public ISequentialInStream, + public ICompressSetOutStream, + public ISequentialOutStream, + public IOutStreamFlush, + // #endif + + #ifndef _NO_CRYPTO + public ICryptoSetPassword, + #endif + #ifndef EXTRACT_ONLY + public ICompressSetCoderProperties, + public ICompressWriteCoderProperties, + // public ICryptoResetSalt, + public ICryptoResetInitVector, + #endif + public ICompressSetDecoderProperties2, + public CMyUnknownImp +{ +protected: + Byte *_buffer; + // #ifdef _ST_MODE + CMyComPtr _inStream; + CMyComPtr _outStream; + UInt32 _bufferPos; + UInt32 _convertedPosBegin; + UInt32 _convertedPosEnd; + // #endif + bool _outSizeIsDefined; + UInt64 _outSize; + UInt64 _nowPos64; + + HRESULT Init() + { + _nowPos64 = 0; + _outSizeIsDefined = false; + return Filter->Init(); + } + + CMyComPtr _setPassword; + #ifndef EXTRACT_ONLY + CMyComPtr _SetCoderProperties; + CMyComPtr _writeCoderProperties; + // CMyComPtr _CryptoResetSalt; + CMyComPtr _CryptoResetInitVector; + #endif + CMyComPtr _setDecoderProperties; +public: + CMyComPtr Filter; + + CFilterCoder(); + ~CFilterCoder(); + HRESULT WriteWithLimit(ISequentialOutStream *outStream, UInt32 size); + bool NeedMore() const + { return (!_outSizeIsDefined || (_nowPos64 < _outSize)); } + +public: + MY_QUERYINTERFACE_BEGIN + MY_QUERYINTERFACE_ENTRY(ICompressCoder) + // #ifdef _ST_MODE + MY_QUERYINTERFACE_ENTRY(ICompressSetInStream) + MY_QUERYINTERFACE_ENTRY(ISequentialInStream) + + MY_QUERYINTERFACE_ENTRY(ICompressSetOutStream) + MY_QUERYINTERFACE_ENTRY(ISequentialOutStream) + MY_QUERYINTERFACE_ENTRY(IOutStreamFlush) + // #endif + + #ifndef _NO_CRYPTO + MY_QUERYINTERFACE_ENTRY_AG(ICryptoSetPassword, Filter, _setPassword) + #endif + + #ifndef EXTRACT_ONLY + MY_QUERYINTERFACE_ENTRY_AG(ICompressSetCoderProperties, Filter, _SetCoderProperties) + MY_QUERYINTERFACE_ENTRY_AG(ICompressWriteCoderProperties, Filter, _writeCoderProperties) + // MY_QUERYINTERFACE_ENTRY_AG(ICryptoResetSalt, Filter, _CryptoResetSalt) + MY_QUERYINTERFACE_ENTRY_AG(ICryptoResetInitVector, Filter, _CryptoResetInitVector) + #endif + + MY_QUERYINTERFACE_ENTRY_AG(ICompressSetDecoderProperties2, Filter, _setDecoderProperties) + MY_QUERYINTERFACE_END + MY_ADDREF_RELEASE + STDMETHOD(Code)(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress); + // #ifdef _ST_MODE + STDMETHOD(ReleaseInStream)(); + STDMETHOD(SetInStream)(ISequentialInStream *inStream); + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); \ + STDMETHOD(SetOutStream)(ISequentialOutStream *outStream); + STDMETHOD(ReleaseOutStream)(); + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + STDMETHOD(Flush)(); + // #endif + + #ifndef _NO_CRYPTO + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); + #endif + #ifndef EXTRACT_ONLY + STDMETHOD(SetCoderProperties)(const PROPID *propIDs, + const PROPVARIANT *properties, UInt32 numProperties); + STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream); + // STDMETHOD(ResetSalt)(); + STDMETHOD(ResetInitVector)(); + #endif + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); +}; + +// #ifdef _ST_MODE +class CInStreamReleaser +{ +public: + CFilterCoder *FilterCoder; + CInStreamReleaser(): FilterCoder(0) {} + ~CInStreamReleaser() { if (FilterCoder) FilterCoder->ReleaseInStream(); } +}; + +class COutStreamReleaser +{ +public: + CFilterCoder *FilterCoder; + COutStreamReleaser(): FilterCoder(0) {} + ~COutStreamReleaser() { if (FilterCoder) FilterCoder->ReleaseOutStream(); } +}; +// #endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/InBuffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/InBuffer.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,83 @@ +// InBuffer.cpp + +#include "StdAfx.h" + +#include "InBuffer.h" + +extern "C" +{ + #include "../../../C/Alloc.h" +} + +CInBuffer::CInBuffer(): + _buffer(0), + _bufferLimit(0), + _bufferBase(0), + _stream(0), + _bufferSize(0) +{} + +bool CInBuffer::Create(UInt32 bufferSize) +{ + const UInt32 kMinBlockSize = 1; + if (bufferSize < kMinBlockSize) + bufferSize = kMinBlockSize; + if (_bufferBase != 0 && _bufferSize == bufferSize) + return true; + Free(); + _bufferSize = bufferSize; + _bufferBase = (Byte *)::MidAlloc(bufferSize); + return (_bufferBase != 0); +} + +void CInBuffer::Free() +{ + ::MidFree(_bufferBase); + _bufferBase = 0; +} + +void CInBuffer::SetStream(ISequentialInStream *stream) +{ + _stream = stream; +} + +void CInBuffer::Init() +{ + _processedSize = 0; + _buffer = _bufferBase; + _bufferLimit = _buffer; + _wasFinished = false; + #ifdef _NO_EXCEPTIONS + ErrorCode = S_OK; + #endif +} + +bool CInBuffer::ReadBlock() +{ + #ifdef _NO_EXCEPTIONS + if (ErrorCode != S_OK) + return false; + #endif + if (_wasFinished) + return false; + _processedSize += (_buffer - _bufferBase); + UInt32 numProcessedBytes; + HRESULT result = _stream->Read(_bufferBase, _bufferSize, &numProcessedBytes); + #ifdef _NO_EXCEPTIONS + ErrorCode = result; + #else + if (result != S_OK) + throw CInBufferException(result); + #endif + _buffer = _bufferBase; + _bufferLimit = _buffer + numProcessedBytes; + _wasFinished = (numProcessedBytes == 0); + return (!_wasFinished); +} + +Byte CInBuffer::ReadBlock2() +{ + if(!ReadBlock()) + return 0xFF; + return *_buffer++; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/InBuffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/InBuffer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,81 @@ +// InBuffer.h + +#ifndef __INBUFFER_H +#define __INBUFFER_H + +#include "../IStream.h" +#include "../../Common/MyCom.h" +#include "../../Common/MyException.h" + +#ifndef _NO_EXCEPTIONS +struct CInBufferException: public CSystemException +{ + CInBufferException(HRESULT errorCode): CSystemException(errorCode) {} +}; +#endif + +class CInBuffer +{ + Byte *_buffer; + Byte *_bufferLimit; + Byte *_bufferBase; + CMyComPtr _stream; + UInt64 _processedSize; + UInt32 _bufferSize; + bool _wasFinished; + + bool ReadBlock(); + Byte ReadBlock2(); + +public: + #ifdef _NO_EXCEPTIONS + HRESULT ErrorCode; + #endif + + CInBuffer(); + ~CInBuffer() { Free(); } + + bool Create(UInt32 bufferSize); + void Free(); + + void SetStream(ISequentialInStream *stream); + void Init(); + void ReleaseStream() { _stream.Release(); } + + bool ReadByte(Byte &b) + { + if (_buffer >= _bufferLimit) + if (!ReadBlock()) + return false; + b = *_buffer++; + return true; + } + Byte ReadByte() + { + if (_buffer >= _bufferLimit) + return ReadBlock2(); + return *_buffer++; + } + UInt32 ReadBytes(Byte *buf, UInt32 size) + { + if ((UInt32)(_bufferLimit - _buffer) >= size) + { + for (UInt32 i = 0; i < size; i++) + buf[i] = _buffer[i]; + _buffer += size; + return size; + } + for (UInt32 i = 0; i < size; i++) + { + if (_buffer >= _bufferLimit) + if (!ReadBlock()) + return i; + buf[i] = *_buffer++; + } + return size; + } + UInt64 GetProcessedSize() const { return _processedSize + (_buffer - _bufferBase); } + bool WasFinished() const { return _wasFinished; } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/InOutTempBuffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/InOutTempBuffer.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,122 @@ +// InOutTempBuffer.cpp + +#include "StdAfx.h" + +#include "InOutTempBuffer.h" +#include "../../Common/Defs.h" +// #include "Windows/Defs.h" + +#include "StreamUtils.h" + +using namespace NWindows; +using namespace NFile; +using namespace NDirectory; + +static UInt32 kTmpBufferMemorySize = (1 << 20); + +static LPCTSTR kTempFilePrefixString = TEXT("iot"); + +CInOutTempBuffer::CInOutTempBuffer(): + _buffer(NULL) +{ +} + +void CInOutTempBuffer::Create() +{ + _buffer = new Byte[kTmpBufferMemorySize]; +} + +CInOutTempBuffer::~CInOutTempBuffer() +{ + delete []_buffer; +} +void CInOutTempBuffer::InitWriting() +{ + _bufferPosition = 0; + _tmpFileCreated = false; + _fileSize = 0; +} + +bool CInOutTempBuffer::WriteToFile(const void *data, UInt32 size) +{ + if (size == 0) + return true; + if(!_tmpFileCreated) + { + CSysString tempDirPath; + if(!MyGetTempPath(tempDirPath)) + return false; + if (_tempFile.Create(tempDirPath, kTempFilePrefixString, _tmpFileName) == 0) + return false; + // _outFile.SetOpenCreationDispositionCreateAlways(); + if(!_outFile.Create(_tmpFileName, true)) + return false; + _tmpFileCreated = true; + } + UInt32 processedSize; + if(!_outFile.Write(data, size, processedSize)) + return false; + _fileSize += processedSize; + return (processedSize == size); +} + +bool CInOutTempBuffer::FlushWrite() +{ + return _outFile.Close(); +} + +bool CInOutTempBuffer::Write(const void *data, UInt32 size) +{ + if(_bufferPosition < kTmpBufferMemorySize) + { + UInt32 curSize = MyMin(kTmpBufferMemorySize - _bufferPosition, size); + memmove(_buffer + _bufferPosition, (const Byte *)data, curSize); + _bufferPosition += curSize; + size -= curSize; + data = ((const Byte *)data) + curSize; + _fileSize += curSize; + } + return WriteToFile(data, size); +} + +bool CInOutTempBuffer::InitReading() +{ + _currentPositionInBuffer = 0; + if(_tmpFileCreated) + return _inFile.Open(_tmpFileName); + return true; +} + +HRESULT CInOutTempBuffer::WriteToStream(ISequentialOutStream *stream) +{ + if (_currentPositionInBuffer < _bufferPosition) + { + UInt32 sizeToWrite = _bufferPosition - _currentPositionInBuffer; + RINOK(WriteStream(stream, _buffer + _currentPositionInBuffer, sizeToWrite)); + _currentPositionInBuffer += sizeToWrite; + } + if (!_tmpFileCreated) + return true; + for (;;) + { + UInt32 localProcessedSize; + if (!_inFile.ReadPart(_buffer, kTmpBufferMemorySize, localProcessedSize)) + return E_FAIL; + if (localProcessedSize == 0) + return S_OK; + RINOK(WriteStream(stream, _buffer, localProcessedSize)); + } +} + +STDMETHODIMP CSequentialOutTempBufferImp::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + if (!_buffer->Write(data, size)) + { + if (processedSize != NULL) + *processedSize = 0; + return E_FAIL; + } + if (processedSize != NULL) + *processedSize = size; + return S_OK; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/InOutTempBuffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/InOutTempBuffer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,55 @@ +// Util/InOutTempBuffer.h + +#ifndef __IN_OUT_TEMP_BUFFER_H +#define __IN_OUT_TEMP_BUFFER_H + +#include "../../Windows/FileIO.h" +#include "../../Windows/FileDir.h" +#include "../../Common/MyCom.h" + +#include "../IStream.h" + +class CInOutTempBuffer +{ + NWindows::NFile::NDirectory::CTempFile _tempFile; + NWindows::NFile::NIO::COutFile _outFile; + NWindows::NFile::NIO::CInFile _inFile; + Byte *_buffer; + UInt32 _bufferPosition; + UInt32 _currentPositionInBuffer; + CSysString _tmpFileName; + bool _tmpFileCreated; + + UInt64 _fileSize; + + bool WriteToFile(const void *data, UInt32 size); +public: + CInOutTempBuffer(); + ~CInOutTempBuffer(); + void Create(); + + void InitWriting(); + bool Write(const void *data, UInt32 size); + UInt64 GetDataSize() const { return _fileSize; } + bool FlushWrite(); + bool InitReading(); + HRESULT WriteToStream(ISequentialOutStream *stream); +}; + +class CSequentialOutTempBufferImp: + public ISequentialOutStream, + public CMyUnknownImp +{ + CInOutTempBuffer *_buffer; +public: + // CSequentialOutStreamImp(): _size(0) {} + // UInt32 _size; + void Init(CInOutTempBuffer *buffer) { _buffer = buffer; } + // UInt32 GetSize() const { return _size; } + + MY_UNKNOWN_IMP + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/LimitedStreams.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/LimitedStreams.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,45 @@ +// LimitedStreams.cpp + +#include "StdAfx.h" + +#include "LimitedStreams.h" +#include "../../Common/Defs.h" + +STDMETHODIMP CLimitedSequentialInStream::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize = 0; + UInt32 sizeToRead = (UInt32)MyMin((_size - _pos), (UInt64)size); + HRESULT result = S_OK; + if (sizeToRead > 0) + { + result = _stream->Read(data, sizeToRead, &realProcessedSize); + _pos += realProcessedSize; + if (realProcessedSize == 0) + _wasFinished = true; + } + if(processedSize != NULL) + *processedSize = realProcessedSize; + return result; +} + +STDMETHODIMP CLimitedSequentialOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + HRESULT result = S_OK; + if (processedSize != NULL) + *processedSize = 0; + if (size > _size) + { + size = (UInt32)_size; + if (size == 0) + { + _overflow = true; + return E_FAIL; + } + } + if (_stream) + result = _stream->Write(data, size, &size); + _size -= size; + if (processedSize != NULL) + *processedSize = size; + return result; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/LimitedStreams.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/LimitedStreams.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,54 @@ +// LimitedStreams.h + +#ifndef __LIMITEDSTREAMS_H +#define __LIMITEDSTREAMS_H + +#include "../../Common/MyCom.h" +#include "../IStream.h" + +class CLimitedSequentialInStream: + public ISequentialInStream, + public CMyUnknownImp +{ + CMyComPtr _stream; + UInt64 _size; + UInt64 _pos; + bool _wasFinished; +public: + void SetStream(ISequentialInStream *stream) { _stream = stream; } + void Init(UInt64 streamSize) + { + _size = streamSize; + _pos = 0; + _wasFinished = false; + } + + MY_UNKNOWN_IMP + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + UInt64 GetSize() const { return _pos; } + bool WasFinished() const { return _wasFinished; } +}; + +class CLimitedSequentialOutStream: + public ISequentialOutStream, + public CMyUnknownImp +{ + CMyComPtr _stream; + UInt64 _size; + bool _overflow; +public: + MY_UNKNOWN_IMP + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + void SetStream(ISequentialOutStream *stream) { _stream = stream; } + void ReleaseStream() { _stream.Release(); } + void Init(UInt64 size) + { + _size = size; + _overflow = false; + } + bool IsFinishedOK() const { return (_size == 0 && !_overflow); } + UInt64 GetRem() const { return _size; } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/LockedStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/LockedStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,23 @@ +// LockedStream.cpp + +#include "StdAfx.h" + +#include "LockedStream.h" + +HRESULT CLockedInStream::Read(UInt64 startPos, void *data, UInt32 size, + UInt32 *processedSize) +{ + NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection); + RINOK(_stream->Seek(startPos, STREAM_SEEK_SET, NULL)); + return _stream->Read(data, size, processedSize); +} + +STDMETHODIMP CLockedSequentialInStreamImp::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize = 0; + HRESULT result = _lockedInStream->Read(_pos, data, size, &realProcessedSize); + _pos += realProcessedSize; + if (processedSize != NULL) + *processedSize = realProcessedSize; + return result; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/LockedStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/LockedStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,38 @@ +// LockedStream.h + +#ifndef __LOCKEDSTREAM_H +#define __LOCKEDSTREAM_H + +#include "../../Windows/Synchronization.h" +#include "../../Common/MyCom.h" +#include "../IStream.h" + +class CLockedInStream +{ + CMyComPtr _stream; + NWindows::NSynchronization::CCriticalSection _criticalSection; +public: + void Init(IInStream *stream) + { _stream = stream; } + HRESULT Read(UInt64 startPos, void *data, UInt32 size, UInt32 *processedSize); +}; + +class CLockedSequentialInStreamImp: + public ISequentialInStream, + public CMyUnknownImp +{ + CLockedInStream *_lockedInStream; + UInt64 _pos; +public: + void Init(CLockedInStream *lockedInStream, UInt64 startPos) + { + _lockedInStream = lockedInStream; + _pos = startPos; + } + + MY_UNKNOWN_IMP + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/MemBlocks.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/MemBlocks.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,183 @@ +// MemBlocks.cpp + +#include "StdAfx.h" + +#include "Common/MyCom.h" + +#include "StreamUtils.h" +#include "MemBlocks.h" + +bool CMemBlockManager::AllocateSpace(size_t numBlocks) +{ + FreeSpace(); + if (_blockSize < sizeof(void *) || numBlocks < 1) + return false; + size_t totalSize = numBlocks * _blockSize; + if (totalSize / _blockSize != numBlocks) + return false; + _data = ::MidAlloc(totalSize); + if (_data == 0) + return false; + Byte *p = (Byte *)_data; + for (size_t i = 0; i + 1 < numBlocks; i++, p += _blockSize) + *(Byte **)p = (p + _blockSize); + *(Byte **)p = 0; + _headFree = _data; + return true; +} + +void CMemBlockManager::FreeSpace() +{ + ::MidFree(_data); + _data = 0; + _headFree= 0; +} + +void *CMemBlockManager::AllocateBlock() +{ + if (_headFree == 0) + return 0; + void *p = _headFree; + _headFree = *(void **)_headFree; + return p; +} + +void CMemBlockManager::FreeBlock(void *p) +{ + if (p == 0) + return; + *(void **)p = _headFree; + _headFree = p; +} + + +HRes CMemBlockManagerMt::AllocateSpace(size_t numBlocks, size_t numNoLockBlocks) +{ + if (numNoLockBlocks > numBlocks) + return E_INVALIDARG; + if (!CMemBlockManager::AllocateSpace(numBlocks)) + return E_OUTOFMEMORY; + size_t numLockBlocks = numBlocks - numNoLockBlocks; + Semaphore.Close(); + return Semaphore.Create((LONG)numLockBlocks, (LONG)numLockBlocks); +} + +HRes CMemBlockManagerMt::AllocateSpaceAlways(size_t desiredNumberOfBlocks, size_t numNoLockBlocks) +{ + if (numNoLockBlocks > desiredNumberOfBlocks) + return E_INVALIDARG; + for (;;) + { + if (AllocateSpace(desiredNumberOfBlocks, numNoLockBlocks) == 0) + return 0; + if (desiredNumberOfBlocks == numNoLockBlocks) + return E_OUTOFMEMORY; + desiredNumberOfBlocks = numNoLockBlocks + ((desiredNumberOfBlocks - numNoLockBlocks) >> 1); + } +} + +void CMemBlockManagerMt::FreeSpace() +{ + Semaphore.Close(); + CMemBlockManager::FreeSpace(); +} + +void *CMemBlockManagerMt::AllocateBlock() +{ + // Semaphore.Lock(); + NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection); + return CMemBlockManager::AllocateBlock(); +} + +void CMemBlockManagerMt::FreeBlock(void *p, bool lockMode) +{ + if (p == 0) + return; + { + NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection); + CMemBlockManager::FreeBlock(p); + } + if (lockMode) + Semaphore.Release(); +} + +void CMemBlocks::Free(CMemBlockManagerMt *manager) +{ + while(Blocks.Size() > 0) + { + manager->FreeBlock(Blocks.Back()); + Blocks.DeleteBack(); + } + TotalSize = 0; +} + +void CMemBlocks::FreeOpt(CMemBlockManagerMt *manager) +{ + Free(manager); + Blocks.ClearAndFree(); +} + +HRESULT CMemBlocks::WriteToStream(size_t blockSize, ISequentialOutStream *outStream) const +{ + UInt64 totalSize = TotalSize; + for (int blockIndex = 0; totalSize > 0; blockIndex++) + { + UInt32 curSize = (UInt32)blockSize; + if (totalSize < curSize) + curSize = (UInt32)totalSize; + if (blockIndex >= Blocks.Size()) + return E_FAIL; + RINOK(WriteStream(outStream, Blocks[blockIndex], curSize)); + totalSize -= curSize; + } + return S_OK; +} + + +void CMemLockBlocks::FreeBlock(int index, CMemBlockManagerMt *memManager) +{ + memManager->FreeBlock(Blocks[index], LockMode); + Blocks[index] = 0; +} + +void CMemLockBlocks::Free(CMemBlockManagerMt *memManager) +{ + while (Blocks.Size() > 0) + { + FreeBlock(Blocks.Size() - 1, memManager); + Blocks.DeleteBack(); + } + TotalSize = 0; +} + +HRes CMemLockBlocks::SwitchToNoLockMode(CMemBlockManagerMt *memManager) +{ + if (LockMode) + { + if (Blocks.Size() > 0) + { + RINOK(memManager->ReleaseLockedBlocks(Blocks.Size())); + } + LockMode = false; + } + return 0; +} + +void CMemLockBlocks::Detach(CMemLockBlocks &blocks, CMemBlockManagerMt *memManager) +{ + blocks.Free(memManager); + blocks.LockMode = LockMode; + UInt64 totalSize = 0; + size_t blockSize = memManager->GetBlockSize(); + for (int i = 0; i < Blocks.Size(); i++) + { + if (totalSize < TotalSize) + blocks.Blocks.Add(Blocks[i]); + else + FreeBlock(i, memManager); + Blocks[i] = 0; + totalSize += blockSize; + } + blocks.TotalSize = TotalSize; + Free(memManager); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/MemBlocks.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/MemBlocks.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,77 @@ +// MemBlocks.h + +#ifndef __MEMBLOCKS_H +#define __MEMBLOCKS_H + +extern "C" +{ +#include "../../../C/Alloc.h" +} + +#include "Common/Types.h" +#include "Common/MyVector.h" + +#include "Windows/Synchronization.h" + +#include "../IStream.h" + +class CMemBlockManager +{ + void *_data; + size_t _blockSize; + void *_headFree; +public: + CMemBlockManager(size_t blockSize = (1 << 20)): _data(0), _blockSize(blockSize), _headFree(0) {} + ~CMemBlockManager() { FreeSpace(); } + + bool AllocateSpace(size_t numBlocks); + void FreeSpace(); + size_t GetBlockSize() const { return _blockSize; } + void *AllocateBlock(); + void FreeBlock(void *p); +}; + + +class CMemBlockManagerMt: public CMemBlockManager +{ + NWindows::NSynchronization::CCriticalSection _criticalSection; +public: + NWindows::NSynchronization::CSemaphore Semaphore; + + CMemBlockManagerMt(size_t blockSize = (1 << 20)): CMemBlockManager(blockSize) {} + ~CMemBlockManagerMt() { FreeSpace(); } + + HRes AllocateSpace(size_t numBlocks, size_t numNoLockBlocks = 0); + HRes AllocateSpaceAlways(size_t desiredNumberOfBlocks, size_t numNoLockBlocks = 0); + void FreeSpace(); + void *AllocateBlock(); + void FreeBlock(void *p, bool lockMode = true); + HRes ReleaseLockedBlocks(int number) { return Semaphore.Release(number); } +}; + + +class CMemBlocks +{ + void Free(CMemBlockManagerMt *manager); +public: + CRecordVector Blocks; + UInt64 TotalSize; + + CMemBlocks(): TotalSize(0) {} + + void FreeOpt(CMemBlockManagerMt *manager); + HRESULT WriteToStream(size_t blockSize, ISequentialOutStream *outStream) const; +}; + +struct CMemLockBlocks: public CMemBlocks +{ + bool LockMode; + + CMemLockBlocks(): LockMode(true) {}; + void Free(CMemBlockManagerMt *memManager); + void FreeBlock(int index, CMemBlockManagerMt *memManager); + HRes SwitchToNoLockMode(CMemBlockManagerMt *memManager); + void Detach(CMemLockBlocks &blocks, CMemBlockManagerMt *memManager); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/MethodId.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/MethodId.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,27 @@ +// MethodId.cpp + +#include "StdAfx.h" + +#include "MethodId.h" +#include "../../Common/MyString.h" + +static inline wchar_t GetHex(Byte value) +{ + return (wchar_t)((value < 10) ? ('0' + value) : ('A' + (value - 10))); +} + +UString ConvertMethodIdToString(UInt64 id) +{ + wchar_t s[32]; + int len = 32; + s[--len] = 0; + do + { + s[--len] = GetHex((Byte)id & 0xF); + id >>= 4; + s[--len] = GetHex((Byte)id & 0xF); + id >>= 4; + } + while (id != 0); + return s + len; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/MethodId.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/MethodId.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,10 @@ +// MethodId.h + +#ifndef __7Z_METHOD_ID_H +#define __7Z_METHOD_ID_H + +#include "../../Common/Types.h" + +typedef UInt64 CMethodId; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/MethodProps.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/MethodProps.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,99 @@ +// MethodProps.cpp + +#include "StdAfx.h" + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "MethodProps.h" + +static UInt64 k_LZMA = 0x030101; +// static UInt64 k_LZMA2 = 0x030102; + +HRESULT SetMethodProperties(const CMethod &method, const UInt64 *inSizeForReduce, IUnknown *coder) +{ + bool tryReduce = false; + UInt32 reducedDictionarySize = 1 << 10; + if (inSizeForReduce != 0 && (method.Id == k_LZMA /* || methodFull.MethodID == k_LZMA2 */)) + { + for (;;) + { + const UInt32 step = (reducedDictionarySize >> 1); + if (reducedDictionarySize >= *inSizeForReduce) + { + tryReduce = true; + break; + } + reducedDictionarySize += step; + if (reducedDictionarySize >= *inSizeForReduce) + { + tryReduce = true; + break; + } + if (reducedDictionarySize >= ((UInt32)3 << 30)) + break; + reducedDictionarySize += step; + } + } + + { + int numProps = method.Props.Size(); + CMyComPtr setCoderProperties; + coder->QueryInterface(IID_ICompressSetCoderProperties, (void **)&setCoderProperties); + if (setCoderProperties == NULL) + { + if (numProps != 0) + return E_INVALIDARG; + } + else + { + CRecordVector propIDs; + NWindows::NCOM::CPropVariant *values = new NWindows::NCOM::CPropVariant[numProps]; + HRESULT res = S_OK; + try + { + for (int i = 0; i < numProps; i++) + { + const CProp &prop = method.Props[i]; + propIDs.Add(prop.Id); + NWindows::NCOM::CPropVariant &value = values[i]; + value = prop.Value; + // if (tryReduce && prop.Id == NCoderPropID::kDictionarySize && value.vt == VT_UI4 && reducedDictionarySize < value.ulVal) + if (tryReduce) + if (prop.Id == NCoderPropID::kDictionarySize) + if (value.vt == VT_UI4) + if (reducedDictionarySize < value.ulVal) + value.ulVal = reducedDictionarySize; + } + CMyComPtr setCoderProperties; + coder->QueryInterface(IID_ICompressSetCoderProperties, (void **)&setCoderProperties); + res = setCoderProperties->SetCoderProperties(&propIDs.Front(), values, numProps); + } + catch(...) + { + delete []values; + throw; + } + delete []values; + RINOK(res); + } + } + + /* + CMyComPtr writeCoderProperties; + coder->QueryInterface(IID_ICompressWriteCoderProperties, (void **)&writeCoderProperties); + if (writeCoderProperties != NULL) + { + CSequentialOutStreamImp *outStreamSpec = new CSequentialOutStreamImp; + CMyComPtr outStream(outStreamSpec); + outStreamSpec->Init(); + RINOK(writeCoderProperties->WriteCoderProperties(outStream)); + size_t size = outStreamSpec->GetSize(); + filterProps.SetCapacity(size); + memmove(filterProps, outStreamSpec->GetBuffer(), size); + } + */ + return S_OK; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/MethodProps.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/MethodProps.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,41 @@ +// MethodProps.h + +#ifndef __7Z_METHOD_PROPS_H +#define __7Z_METHOD_PROPS_H + +#include "../../Common/MyVector.h" + +#include "../../Windows/PropVariant.h" + +#include "MethodId.h" + +struct CProp +{ + PROPID Id; + NWindows::NCOM::CPropVariant Value; +}; + +struct CMethod +{ + CMethodId Id; + CObjectVector Props; +}; + +struct CMethodsMode +{ + CObjectVector Methods; + #ifdef COMPRESS_MT + UInt32 NumThreads; + #endif + + CMethodsMode() + #ifdef COMPRESS_MT + : NumThreads(1) + #endif + {} + bool IsEmpty() const { return Methods.IsEmpty() ; } +}; + +HRESULT SetMethodProperties(const CMethod &method, const UInt64 *inSizeForReduce, IUnknown *coder); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/OffsetStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/OffsetStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,35 @@ +// OffsetStream.cpp + +#include "StdAfx.h" + +#include "Common/Defs.h" +#include "OffsetStream.h" + +HRESULT COffsetOutStream::Init(IOutStream *stream, UInt64 offset) +{ + _offset = offset; + _stream = stream; + return _stream->Seek(offset, STREAM_SEEK_SET, NULL); +} + +STDMETHODIMP COffsetOutStream::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + return _stream->Write(data, size, processedSize); +} + +STDMETHODIMP COffsetOutStream::Seek(Int64 offset, UInt32 seekOrigin, + UInt64 *newPosition) +{ + UInt64 absoluteNewPosition; + if (seekOrigin == STREAM_SEEK_SET) + offset += _offset; + HRESULT result = _stream->Seek(offset, seekOrigin, &absoluteNewPosition); + if (newPosition != NULL) + *newPosition = absoluteNewPosition - _offset; + return result; +} + +STDMETHODIMP COffsetOutStream::SetSize(Int64 newSize) +{ + return _stream->SetSize(_offset + newSize); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/OffsetStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/OffsetStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,25 @@ +// OffsetStream.h + +#ifndef __OFFSETSTREAM_H +#define __OFFSETSTREAM_H + +#include "Common/MyCom.h" +#include "../IStream.h" + +class COffsetOutStream: + public IOutStream, + public CMyUnknownImp +{ + UInt64 _offset; + CMyComPtr _stream; +public: + HRESULT Init(IOutStream *stream, UInt64 offset); + + MY_UNKNOWN_IMP + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition); + STDMETHOD(SetSize)(Int64 newSize); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/OutBuffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/OutBuffer.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,119 @@ +// OutByte.cpp + +#include "StdAfx.h" + +#include "OutBuffer.h" + +extern "C" +{ + #include "../../../C/Alloc.h" +} + +bool COutBuffer::Create(UInt32 bufferSize) +{ + const UInt32 kMinBlockSize = 1; + if (bufferSize < kMinBlockSize) + bufferSize = kMinBlockSize; + if (_buffer != 0 && _bufferSize == bufferSize) + return true; + Free(); + _bufferSize = bufferSize; + _buffer = (Byte *)::MidAlloc(bufferSize); + return (_buffer != 0); +} + +void COutBuffer::Free() +{ + ::MidFree(_buffer); + _buffer = 0; +} + +void COutBuffer::SetStream(ISequentialOutStream *stream) +{ + _stream = stream; +} + +void COutBuffer::Init() +{ + _streamPos = 0; + _limitPos = _bufferSize; + _pos = 0; + _processedSize = 0; + _overDict = false; + #ifdef _NO_EXCEPTIONS + ErrorCode = S_OK; + #endif +} + +UInt64 COutBuffer::GetProcessedSize() const +{ + UInt64 res = _processedSize + _pos - _streamPos; + if (_streamPos > _pos) + res += _bufferSize; + return res; +} + + +HRESULT COutBuffer::FlushPart() +{ + // _streamPos < _bufferSize + UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos); + HRESULT result = S_OK; + #ifdef _NO_EXCEPTIONS + result = ErrorCode; + #endif + if (_buffer2 != 0) + { + memmove(_buffer2, _buffer + _streamPos, size); + _buffer2 += size; + } + + if (_stream != 0 + #ifdef _NO_EXCEPTIONS + && (ErrorCode == S_OK) + #endif + ) + { + UInt32 processedSize = 0; + result = _stream->Write(_buffer + _streamPos, size, &processedSize); + size = processedSize; + } + _streamPos += size; + if (_streamPos == _bufferSize) + _streamPos = 0; + if (_pos == _bufferSize) + { + _overDict = true; + _pos = 0; + } + _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize; + _processedSize += size; + return result; +} + +HRESULT COutBuffer::Flush() +{ + #ifdef _NO_EXCEPTIONS + if (ErrorCode != S_OK) + return ErrorCode; + #endif + + while(_streamPos != _pos) + { + HRESULT result = FlushPart(); + if (result != S_OK) + return result; + } + return S_OK; +} + +void COutBuffer::FlushWithCheck() +{ + HRESULT result = Flush(); + #ifdef _NO_EXCEPTIONS + ErrorCode = result; + #else + if (result != S_OK) + throw COutBufferException(result); + #endif +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/OutBuffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/OutBuffer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,64 @@ +// OutBuffer.h + +#ifndef __OUTBUFFER_H +#define __OUTBUFFER_H + +#include "../IStream.h" +#include "../../Common/MyCom.h" +#include "../../Common/MyException.h" + +#ifndef _NO_EXCEPTIONS +struct COutBufferException: public CSystemException +{ + COutBufferException(HRESULT errorCode): CSystemException(errorCode) {} +}; +#endif + +class COutBuffer +{ +protected: + Byte *_buffer; + UInt32 _pos; + UInt32 _limitPos; + UInt32 _streamPos; + UInt32 _bufferSize; + CMyComPtr _stream; + UInt64 _processedSize; + Byte *_buffer2; + bool _overDict; + + HRESULT FlushPart(); +public: + #ifdef _NO_EXCEPTIONS + HRESULT ErrorCode; + #endif + + COutBuffer(): _buffer(0), _pos(0), _stream(0), _buffer2(0) {} + ~COutBuffer() { Free(); } + + bool Create(UInt32 bufferSize); + void Free(); + + void SetMemStream(Byte *buffer) { _buffer2 = buffer; } + void SetStream(ISequentialOutStream *stream); + void Init(); + HRESULT Flush(); + void FlushWithCheck(); + void ReleaseStream() { _stream.Release(); } + + void WriteByte(Byte b) + { + _buffer[_pos++] = b; + if(_pos == _limitPos) + FlushWithCheck(); + } + void WriteBytes(const void *data, size_t size) + { + for (size_t i = 0; i < size; i++) + WriteByte(((const Byte *)data)[i]); + } + + UInt64 GetProcessedSize() const; +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/OutMemStream.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/OutMemStream.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,142 @@ +// OutMemStream.cpp + +#include "StdAfx.h" + +#include "OutMemStream.h" + +void COutMemStream::Free() +{ + Blocks.Free(_memManager); + Blocks.LockMode = true; +} + +void COutMemStream::Init() +{ + WriteToRealStreamEvent.Reset(); + _unlockEventWasSent = false; + _realStreamMode = false; + Free(); + _curBlockPos = 0; + _curBlockIndex = 0; +} + +void COutMemStream::DetachData(CMemLockBlocks &blocks) +{ + Blocks.Detach(blocks, _memManager); + Free(); +} + + +HRESULT COutMemStream::WriteToRealStream() +{ + RINOK(Blocks.WriteToStream(_memManager->GetBlockSize(), OutSeqStream)); + Blocks.Free(_memManager); + return S_OK; +} + +STDMETHODIMP COutMemStream::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + if (_realStreamMode) + return OutSeqStream->Write(data, size, processedSize); + if (processedSize != 0) + *processedSize = 0; + while(size != 0) + { + if ((int)_curBlockIndex < Blocks.Blocks.Size()) + { + Byte *p = (Byte *)Blocks.Blocks[(int)_curBlockIndex] + _curBlockPos; + size_t curSize = _memManager->GetBlockSize() - _curBlockPos; + if (size < curSize) + curSize = size; + memmove(p, data, curSize); + if (processedSize != 0) + *processedSize += (UInt32)curSize; + data = (const void *)((const Byte *)data + curSize); + size -= (UInt32)curSize; + _curBlockPos += curSize; + + UInt64 pos64 = GetPos(); + if (pos64 > Blocks.TotalSize) + Blocks.TotalSize = pos64; + if (_curBlockPos == _memManager->GetBlockSize()) + { + _curBlockIndex++; + _curBlockPos = 0; + } + continue; + } + HANDLE events[3] = { StopWritingEvent, WriteToRealStreamEvent, /* NoLockEvent, */ _memManager->Semaphore }; + DWORD waitResult = ::WaitForMultipleObjects((Blocks.LockMode ? 3 : 2), events, FALSE, INFINITE); + switch (waitResult) + { + case (WAIT_OBJECT_0 + 0): + return StopWriteResult; + case (WAIT_OBJECT_0 + 1): + { + _realStreamMode = true; + RINOK(WriteToRealStream()); + UInt32 processedSize2; + HRESULT res = OutSeqStream->Write(data, size, &processedSize2); + if (processedSize != 0) + *processedSize += processedSize2; + return res; + } + /* + case (WAIT_OBJECT_0 + 2): + { + // it has bug: no write. + if (!Blocks.SwitchToNoLockMode(_memManager)) + return E_FAIL; + break; + } + */ + case (WAIT_OBJECT_0 + 2): + break; + default: + return E_FAIL; + } + Blocks.Blocks.Add(_memManager->AllocateBlock()); + if (Blocks.Blocks.Back() == 0) + return E_FAIL; + } + return S_OK; +} + +STDMETHODIMP COutMemStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) +{ + if (_realStreamMode) + { + if (!OutStream) + return E_FAIL; + return OutStream->Seek(offset, seekOrigin, newPosition); + } + if (seekOrigin == STREAM_SEEK_CUR) + { + if (offset != 0) + return E_NOTIMPL; + } + else if (seekOrigin == STREAM_SEEK_SET) + { + if (offset != 0) + return E_NOTIMPL; + _curBlockIndex = 0; + _curBlockPos = 0; + } + else + return E_NOTIMPL; + if (newPosition != 0) + *newPosition = GetPos(); + return S_OK; +} + +STDMETHODIMP COutMemStream::SetSize(Int64 newSize) +{ + if (_realStreamMode) + { + if (!OutStream) + return E_FAIL; + return OutStream->SetSize(newSize); + } + Blocks.TotalSize = newSize; + return S_OK; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/OutMemStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/OutMemStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,96 @@ +// OutMemStream.h + +#ifndef __OUTMEMSTREAM_H +#define __OUTMEMSTREAM_H + +#include "Common/MyCom.h" +#include "MemBlocks.h" + +class COutMemStream: + public IOutStream, + public CMyUnknownImp +{ + CMemBlockManagerMt *_memManager; + size_t _curBlockIndex; + size_t _curBlockPos; + bool _realStreamMode; + + bool _unlockEventWasSent; + NWindows::NSynchronization::CAutoResetEvent StopWritingEvent; + NWindows::NSynchronization::CAutoResetEvent WriteToRealStreamEvent; + // NWindows::NSynchronization::CAutoResetEvent NoLockEvent; + + HRESULT StopWriteResult; + CMemLockBlocks Blocks; + + UInt64 GetPos() const { return (UInt64)_curBlockIndex * _memManager->GetBlockSize() + _curBlockPos; } + + CMyComPtr OutSeqStream; + CMyComPtr OutStream; + +public: + + HRes CreateEvents() + { + RINOK(StopWritingEvent.CreateIfNotCreated()); + return WriteToRealStreamEvent.CreateIfNotCreated(); + } + + void SetOutStream(IOutStream *outStream) + { + OutStream = outStream; + OutSeqStream = outStream; + } + + void SetSeqOutStream(ISequentialOutStream *outStream) + { + OutStream = NULL; + OutSeqStream = outStream; + } + + void ReleaseOutStream() + { + OutStream.Release(); + OutSeqStream.Release(); + } + + COutMemStream(CMemBlockManagerMt *memManager): _memManager(memManager) { } + + ~COutMemStream() { Free(); } + void Free(); + + void Init(); + HRESULT WriteToRealStream(); + + void DetachData(CMemLockBlocks &blocks); + + bool WasUnlockEventSent() const { return _unlockEventWasSent; } + + void SetRealStreamMode() + { + _unlockEventWasSent = true; + WriteToRealStreamEvent.Set(); + } + + /* + void SetNoLockMode() + { + _unlockEventWasSent = true; + NoLockEvent.Set(); + } + */ + + void StopWriting(HRESULT res) + { + StopWriteResult = res; + StopWritingEvent.Set(); + } + + MY_UNKNOWN_IMP + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition); + STDMETHOD(SetSize)(Int64 newSize); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/ProgressMt.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/ProgressMt.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,53 @@ +// ProgressMt.h + +#include "StdAfx.h" + +#include "ProgressMt.h" + +void CMtCompressProgressMixer::Init(int numItems, ICompressProgressInfo *progress) +{ + NWindows::NSynchronization::CCriticalSectionLock lock(CriticalSection); + InSizes.Clear(); + OutSizes.Clear(); + for (int i = 0; i < numItems; i++) + { + InSizes.Add(0); + OutSizes.Add(0); + } + TotalInSize = 0; + TotalOutSize = 0; + _progress = progress; +} + +void CMtCompressProgressMixer::Reinit(int index) +{ + NWindows::NSynchronization::CCriticalSectionLock lock(CriticalSection); + InSizes[index] = 0; + OutSizes[index] = 0; +} + +HRESULT CMtCompressProgressMixer::SetRatioInfo(int index, const UInt64 *inSize, const UInt64 *outSize) +{ + NWindows::NSynchronization::CCriticalSectionLock lock(CriticalSection); + if (inSize != 0) + { + UInt64 diff = *inSize - InSizes[index]; + InSizes[index] = *inSize; + TotalInSize += diff; + } + if (outSize != 0) + { + UInt64 diff = *outSize - OutSizes[index]; + OutSizes[index] = *outSize; + TotalOutSize += diff; + } + if (_progress) + return _progress->SetRatioInfo(&TotalInSize, &TotalOutSize); + return S_OK; +} + + +STDMETHODIMP CMtCompressProgress::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize) +{ + return _progress->SetRatioInfo(_index, inSize, outSize); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/ProgressMt.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/ProgressMt.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +// ProgressMt.h + +#ifndef __PROGRESSMT_H +#define __PROGRESSMT_H + +#include "../../Common/MyCom.h" +#include "../../Common/MyVector.h" +#include "../../Windows/Synchronization.h" + +#include "../ICoder.h" +#include "../IProgress.h" + +class CMtCompressProgressMixer +{ + CMyComPtr _progress; + CRecordVector InSizes; + CRecordVector OutSizes; + UInt64 TotalInSize; + UInt64 TotalOutSize; +public: + NWindows::NSynchronization::CCriticalSection CriticalSection; + void Init(int numItems, ICompressProgressInfo *progress); + void Reinit(int index); + HRESULT SetRatioInfo(int index, const UInt64 *inSize, const UInt64 *outSize); +}; + +class CMtCompressProgress: + public ICompressProgressInfo, + public CMyUnknownImp +{ + CMtCompressProgressMixer *_progress; + int _index; +public: + void Init(CMtCompressProgressMixer *progress, int index) + { + _progress = progress; + _index = index; + } + void Reinit() { _progress->Reinit(_index); } + + MY_UNKNOWN_IMP + + STDMETHOD(SetRatioInfo)(const UInt64 *inSize, const UInt64 *outSize); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/ProgressUtils.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/ProgressUtils.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,42 @@ +// ProgressUtils.h + +#include "StdAfx.h" + +#include "ProgressUtils.h" + +CLocalProgress::CLocalProgress() +{ + ProgressOffset = InSize = OutSize = 0; + SendRatio = SendProgress = true; +} + +void CLocalProgress::Init(IProgress *progress, bool inSizeIsMain) +{ + _ratioProgress.Release(); + _progress = progress; + _progress.QueryInterface(IID_ICompressProgressInfo, &_ratioProgress); + _inSizeIsMain = inSizeIsMain; +} + +STDMETHODIMP CLocalProgress::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize) +{ + UInt64 inSizeNew = InSize, outSizeNew = OutSize; + if (inSize) + inSizeNew += (*inSize); + if (outSize) + outSizeNew += (*outSize); + if (SendRatio && _ratioProgress) + { + RINOK(_ratioProgress->SetRatioInfo(&inSizeNew, &outSizeNew)); + } + inSizeNew += ProgressOffset; + outSizeNew += ProgressOffset; + if (SendProgress) + return _progress->SetCompleted(_inSizeIsMain ? &inSizeNew : &outSizeNew); + return S_OK; +} + +HRESULT CLocalProgress::SetCur() +{ + return SetRatioInfo(NULL, NULL); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/ProgressUtils.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/ProgressUtils.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,34 @@ +// ProgressUtils.h + +#ifndef __PROGRESSUTILS_H +#define __PROGRESSUTILS_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" +#include "../IProgress.h" + +class CLocalProgress: + public ICompressProgressInfo, + public CMyUnknownImp +{ + CMyComPtr _progress; + CMyComPtr _ratioProgress; + bool _inSizeIsMain; +public: + UInt64 ProgressOffset; + UInt64 InSize; + UInt64 OutSize; + bool SendRatio; + bool SendProgress; + + CLocalProgress(); + void Init(IProgress *progress, bool inSizeIsMain); + HRESULT SetCur(); + + MY_UNKNOWN_IMP + + STDMETHOD(SetRatioInfo)(const UInt64 *inSize, const UInt64 *outSize); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/RegisterArc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/RegisterArc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,33 @@ +// RegisterArc.h + +#ifndef __REGISTERARC_H +#define __REGISTERARC_H + +#include "../Archive/IArchive.h" +#include "DeclareArcs.h" + +typedef IInArchive * (*CreateInArchiveP)(); +typedef IOutArchive * (*CreateOutArchiveP)(); + +struct CArcInfo +{ + const wchar_t *Name; + const wchar_t *Ext; + const wchar_t *AddExt; + Byte ClassId; + Byte Signature[16]; + int SignatureSize; + bool KeepName; + CreateInArchiveP CreateInArchive; + CreateOutArchiveP CreateOutArchive; +}; + +void RegisterArc(const CArcInfo *arcInfo); + +#define REGISTER_ARC(x) CRegister##x::CRegister##x() { RegisterArc(&g_ArcInfo); } \ + CRegister##x g_RegisterArc##x; + +#define REGISTER_ARCN(x,n) CRegister##x##n::CRegister##x##n() { RegisterArc(&g_ArcInfo##n); } \ + CRegister##x##n g_RegisterArc##n##x; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/RegisterCodec.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/RegisterCodec.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +// RegisterCodec.h + +#ifndef __REGISTERCODEC_H +#define __REGISTERCODEC_H + +#include "../Common/MethodId.h" +#include "DeclareCodecs.h" + +typedef void * (*CreateCodecP)(); +struct CCodecInfo +{ + CreateCodecP CreateDecoder; + CreateCodecP CreateEncoder; + CMethodId Id; + const wchar_t *Name; + UInt32 NumInStreams; + bool IsFilter; +}; + +void RegisterCodec(const CCodecInfo *codecInfo); + +#define REGISTER_CODEC(x) CRegisterCodec##x::CRegisterCodec##x() { RegisterCodec(&g_CodecInfo); } \ + CRegisterCodec##x g_RegisterCodec##x; + +#define REGISTER_CODECS(x) CRegisterCodecs##x::CRegisterCodecs##x() { \ + for(int i=0;iCloseRead(); } + void SetBinder(CStreamBinder *streamBinder) { m_StreamBinder = streamBinder; } +}; + +STDMETHODIMP CSequentialInStreamForBinder::Read(void *data, UInt32 size, UInt32 *processedSize) + { return m_StreamBinder->Read(data, size, processedSize); } + +class CSequentialOutStreamForBinder: + public ISequentialOutStream, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + +private: + CStreamBinder *m_StreamBinder; +public: + ~CSequentialOutStreamForBinder() { m_StreamBinder->CloseWrite(); } + void SetBinder(CStreamBinder *streamBinder) { m_StreamBinder = streamBinder; } +}; + +STDMETHODIMP CSequentialOutStreamForBinder::Write(const void *data, UInt32 size, UInt32 *processedSize) + { return m_StreamBinder->Write(data, size, processedSize); } + + +////////////////////////// +// CStreamBinder +// (_thereAreBytesToReadEvent && _bufferSize == 0) means that stream is finished. + +HRes CStreamBinder::CreateEvents() +{ + RINOK(_allBytesAreWritenEvent.Create(true)); + RINOK(_thereAreBytesToReadEvent.Create()); + return _readStreamIsClosedEvent.Create(); +} + +void CStreamBinder::ReInit() +{ + _thereAreBytesToReadEvent.Reset(); + _readStreamIsClosedEvent.Reset(); + ProcessedSize = 0; +} + + + +void CStreamBinder::CreateStreams(ISequentialInStream **inStream, + ISequentialOutStream **outStream) +{ + CSequentialInStreamForBinder *inStreamSpec = new + CSequentialInStreamForBinder; + CMyComPtr inStreamLoc(inStreamSpec); + inStreamSpec->SetBinder(this); + *inStream = inStreamLoc.Detach(); + + CSequentialOutStreamForBinder *outStreamSpec = new + CSequentialOutStreamForBinder; + CMyComPtr outStreamLoc(outStreamSpec); + outStreamSpec->SetBinder(this); + *outStream = outStreamLoc.Detach(); + + _buffer = NULL; + _bufferSize= 0; + ProcessedSize = 0; +} + +HRESULT CStreamBinder::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 sizeToRead = size; + if (size > 0) + { + RINOK(_thereAreBytesToReadEvent.Lock()); + sizeToRead = MyMin(_bufferSize, size); + if (_bufferSize > 0) + { + memcpy(data, _buffer, sizeToRead); + _buffer = ((const Byte *)_buffer) + sizeToRead; + _bufferSize -= sizeToRead; + if (_bufferSize == 0) + { + _thereAreBytesToReadEvent.Reset(); + _allBytesAreWritenEvent.Set(); + } + } + } + if (processedSize != NULL) + *processedSize = sizeToRead; + ProcessedSize += sizeToRead; + return S_OK; +} + +void CStreamBinder::CloseRead() +{ + _readStreamIsClosedEvent.Set(); +} + +HRESULT CStreamBinder::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + if (size > 0) + { + _buffer = data; + _bufferSize = size; + _allBytesAreWritenEvent.Reset(); + _thereAreBytesToReadEvent.Set(); + + HANDLE events[2]; + events[0] = _allBytesAreWritenEvent; + events[1] = _readStreamIsClosedEvent; + DWORD waitResult = ::WaitForMultipleObjects(2, events, FALSE, INFINITE); + if (waitResult != WAIT_OBJECT_0 + 0) + { + // ReadingWasClosed = true; + return S_FALSE; + } + // if(!_allBytesAreWritenEvent.Lock()) + // return E_FAIL; + } + if (processedSize != NULL) + *processedSize = size; + return S_OK; +} + +void CStreamBinder::CloseWrite() +{ + // _bufferSize must be = 0 + _thereAreBytesToReadEvent.Set(); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/StreamBinder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/StreamBinder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,32 @@ +// StreamBinder.h + +#ifndef __STREAMBINDER_H +#define __STREAMBINDER_H + +#include "../IStream.h" +#include "../../Windows/Synchronization.h" + +class CStreamBinder +{ + NWindows::NSynchronization::CManualResetEvent _allBytesAreWritenEvent; + NWindows::NSynchronization::CManualResetEvent _thereAreBytesToReadEvent; + NWindows::NSynchronization::CManualResetEvent _readStreamIsClosedEvent; + UInt32 _bufferSize; + const void *_buffer; +public: + // bool ReadingWasClosed; + UInt64 ProcessedSize; + CStreamBinder() {} + HRes CreateEvents(); + + void CreateStreams(ISequentialInStream **inStream, + ISequentialOutStream **outStream); + HRESULT Read(void *data, UInt32 size, UInt32 *processedSize); + void CloseRead(); + + HRESULT Write(const void *data, UInt32 size, UInt32 *processedSize); + void CloseWrite(); + void ReInit(); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/StreamObjects.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/StreamObjects.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,68 @@ +// StreamObjects.cpp + +#include "StdAfx.h" + +#include "StreamObjects.h" +#include "../../Common/Defs.h" + + +STDMETHODIMP CSequentialInStreamImp::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + size_t rem = _size - _pos; + if (size < rem) + rem = (size_t)size; + memcpy(data, _dataPointer + _pos, rem); + _pos += rem; + if (processedSize != NULL) + *processedSize = (UInt32)rem; + return S_OK; +} + + +void CWriteBuffer::Write(const void *data, size_t size) +{ + size_t newCapacity = _size + size; + _buffer.EnsureCapacity(newCapacity); + memcpy(_buffer + _size, data, size); + _size += size; +} + +STDMETHODIMP CSequentialOutStreamImp::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + _writeBuffer.Write(data, (size_t)size); + if(processedSize != NULL) + *processedSize = size; + return S_OK; +} + +STDMETHODIMP CSequentialOutStreamImp2::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + size_t rem = _size - _pos; + if (size < rem) + rem = (size_t)size; + memcpy(_buffer + _pos, data, rem); + _pos += rem; + if (processedSize != NULL) + *processedSize = (UInt32)rem; + return (rem == size ? S_OK : E_FAIL); +} + +STDMETHODIMP CSequentialInStreamSizeCount::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize; + HRESULT result = _stream->Read(data, size, &realProcessedSize); + _size += realProcessedSize; + if (processedSize != 0) + *processedSize = realProcessedSize; + return result; +} + +STDMETHODIMP CSequentialOutStreamSizeCount::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + UInt32 realProcessedSize; + HRESULT result = _stream->Write(data, size, &realProcessedSize); + _size += realProcessedSize; + if (processedSize != 0) + *processedSize = realProcessedSize; + return result; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/StreamObjects.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/StreamObjects.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,117 @@ +// StreamObjects.h + +#ifndef __STREAMOBJECTS_H +#define __STREAMOBJECTS_H + +#include "../../Common/DynamicBuffer.h" +#include "../../Common/MyCom.h" +#include "../IStream.h" + +class CSequentialInStreamImp: + public ISequentialInStream, + public CMyUnknownImp +{ + const Byte *_dataPointer; + size_t _size; + size_t _pos; + +public: + void Init(const Byte *dataPointer, size_t size) + { + _dataPointer = dataPointer; + _size = size; + _pos = 0; + } + + MY_UNKNOWN_IMP + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); +}; + + +class CWriteBuffer +{ + CByteDynamicBuffer _buffer; + size_t _size; +public: + CWriteBuffer(): _size(0) {} + void Init() { _size = 0; } + void Write(const void *data, size_t size); + size_t GetSize() const { return _size; } + const CByteDynamicBuffer& GetBuffer() const { return _buffer; } +}; + +class CSequentialOutStreamImp: + public ISequentialOutStream, + public CMyUnknownImp +{ + CWriteBuffer _writeBuffer; +public: + void Init() { _writeBuffer.Init(); } + size_t GetSize() const { return _writeBuffer.GetSize(); } + const CByteDynamicBuffer& GetBuffer() const { return _writeBuffer.GetBuffer(); } + + MY_UNKNOWN_IMP + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); +}; + +class CSequentialOutStreamImp2: + public ISequentialOutStream, + public CMyUnknownImp +{ + Byte *_buffer; + size_t _size; + size_t _pos; +public: + + void Init(Byte *buffer, size_t size) + { + _buffer = buffer; + _pos = 0; + _size = size; + } + + size_t GetPos() const { return _pos; } + + MY_UNKNOWN_IMP + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); +}; + +class CSequentialInStreamSizeCount: + public ISequentialInStream, + public CMyUnknownImp +{ + CMyComPtr _stream; + UInt64 _size; +public: + void Init(ISequentialInStream *stream) + { + _stream = stream; + _size = 0; + } + UInt64 GetSize() const { return _size; } + + MY_UNKNOWN_IMP + + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); +}; + +class CSequentialOutStreamSizeCount: + public ISequentialOutStream, + public CMyUnknownImp +{ + CMyComPtr _stream; + UInt64 _size; +public: + void SetStream(ISequentialOutStream *stream) { _stream = stream; } + void Init() { _size = 0; } + UInt64 GetSize() const { return _size; } + + MY_UNKNOWN_IMP + + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/StreamUtils.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/StreamUtils.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,56 @@ +// StreamUtils.cpp + +#include "StdAfx.h" + +#include "StreamUtils.h" + +static const UInt32 kBlockSize = ((UInt32)1 << 31); + +HRESULT ReadStream(ISequentialInStream *stream, void *data, size_t *processedSize) +{ + size_t size = *processedSize; + *processedSize = 0; + while (size != 0) + { + UInt32 curSize = (size < kBlockSize) ? (UInt32)size : kBlockSize; + UInt32 processedSizeLoc; + HRESULT res = stream->Read(data, curSize, &processedSizeLoc); + *processedSize += processedSizeLoc; + data = (void *)((Byte *)data + processedSizeLoc); + size -= processedSizeLoc; + RINOK(res); + if (processedSizeLoc == 0) + return S_OK; + } + return S_OK; +} + +HRESULT ReadStream_FALSE(ISequentialInStream *stream, void *data, size_t size) +{ + size_t processedSize = size; + RINOK(ReadStream(stream, data, &processedSize)); + return (size == processedSize) ? S_OK : S_FALSE; +} + +HRESULT ReadStream_FAIL(ISequentialInStream *stream, void *data, size_t size) +{ + size_t processedSize = size; + RINOK(ReadStream(stream, data, &processedSize)); + return (size == processedSize) ? S_OK : E_FAIL; +} + +HRESULT WriteStream(ISequentialOutStream *stream, const void *data, size_t size) +{ + while (size != 0) + { + UInt32 curSize = (size < kBlockSize) ? (UInt32)size : kBlockSize; + UInt32 processedSizeLoc; + HRESULT res = stream->Write(data, curSize, &processedSizeLoc); + data = (const void *)((const Byte *)data + processedSizeLoc); + size -= processedSizeLoc; + RINOK(res); + if (processedSizeLoc == 0) + return E_FAIL; + } + return S_OK; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/StreamUtils.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/StreamUtils.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,13 @@ +// StreamUtils.h + +#ifndef __STREAMUTILS_H +#define __STREAMUTILS_H + +#include "../IStream.h" + +HRESULT ReadStream(ISequentialInStream *stream, void *data, size_t *size); +HRESULT ReadStream_FALSE(ISequentialInStream *stream, void *data, size_t size); +HRESULT ReadStream_FAIL(ISequentialInStream *stream, void *data, size_t size); +HRESULT WriteStream(ISequentialOutStream *stream, const void *data, size_t size); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/VirtThread.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/VirtThread.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,45 @@ +// VirtThread.cpp + +#include "StdAfx.h" + +#include "VirtThread.h" + +static THREAD_FUNC_DECL CoderThread(void *p) +{ + for (;;) + { + CVirtThread *t = (CVirtThread *)p; + t->StartEvent.Lock(); + if (t->ExitEvent) + return 0; + t->Execute(); + t->FinishedEvent.Set(); + } +} + +WRes CVirtThread::Create() +{ + RINOK(StartEvent.CreateIfNotCreated()); + RINOK(FinishedEvent.CreateIfNotCreated()); + StartEvent.Reset(); + FinishedEvent.Reset(); + ExitEvent = false; + if (Thread.IsCreated()) + return S_OK; + return Thread.Create(CoderThread, this); +} + +void CVirtThread::Start() +{ + ExitEvent = false; + StartEvent.Set(); +} + +CVirtThread::~CVirtThread() +{ + ExitEvent = true; + if (StartEvent.IsCreated()) + StartEvent.Set(); + Thread.Wait(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Common/VirtThread.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Common/VirtThread.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,23 @@ +// VirtThread.h + +#ifndef __VIRTTHREAD_H +#define __VIRTTHREAD_H + +#include "../../Windows/Synchronization.h" +#include "../../Windows/Thread.h" + +struct CVirtThread +{ + NWindows::NSynchronization::CAutoResetEvent StartEvent; + NWindows::NSynchronization::CAutoResetEvent FinishedEvent; + NWindows::CThread Thread; + bool ExitEvent; + + ~CVirtThread(); + WRes Create(); + void Start(); + void WaitFinish() { FinishedEvent.Lock(); } + virtual void Execute() = 0; +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BZip2Const.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BZip2Const.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,54 @@ +// Compress/BZip2Const.h + +#ifndef __COMPRESS_BZIP2_CONST_H +#define __COMPRESS_BZIP2_CONST_H + +namespace NCompress { +namespace NBZip2 { + +const Byte kArSig0 = 'B'; +const Byte kArSig1 = 'Z'; +const Byte kArSig2 = 'h'; +const Byte kArSig3 = '0'; + +const Byte kFinSig0 = 0x17; +const Byte kFinSig1 = 0x72; +const Byte kFinSig2 = 0x45; +const Byte kFinSig3 = 0x38; +const Byte kFinSig4 = 0x50; +const Byte kFinSig5 = 0x90; + +const Byte kBlockSig0 = 0x31; +const Byte kBlockSig1 = 0x41; +const Byte kBlockSig2 = 0x59; +const Byte kBlockSig3 = 0x26; +const Byte kBlockSig4 = 0x53; +const Byte kBlockSig5 = 0x59; + +const int kNumOrigBits = 24; + +const int kNumTablesBits = 3; +const int kNumTablesMin = 2; +const int kNumTablesMax = 6; + +const int kNumLevelsBits = 5; + +const int kMaxHuffmanLen = 20; // Check it + +const int kMaxAlphaSize = 258; + +const int kGroupSize = 50; + +const int kBlockSizeMultMin = 1; +const int kBlockSizeMultMax = 9; +const UInt32 kBlockSizeStep = 100000; +const UInt32 kBlockSizeMax = kBlockSizeMultMax * kBlockSizeStep; + +const int kNumSelectorsBits = 15; +const UInt32 kNumSelectorsMax = (2 + (kBlockSizeMax / kGroupSize)); + +const int kRleModeRepSize = 4; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BZip2Crc.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BZip2Crc.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,26 @@ +// BZip2Crc.cpp + +#include "StdAfx.h" + +#include "BZip2Crc.h" + +UInt32 CBZip2Crc::Table[256]; + +static const UInt32 kBZip2CrcPoly = 0x04c11db7; /* AUTODIN II, Ethernet, & FDDI */ + +void CBZip2Crc::InitTable() +{ + for (UInt32 i = 0; i < 256; i++) + { + UInt32 r = (i << 24); + for (int j = 8; j > 0; j--) + r = (r & 0x80000000) ? ((r << 1) ^ kBZip2CrcPoly) : (r << 1); + Table[i] = r; + } +} + +class CBZip2CrcTableInit +{ +public: + CBZip2CrcTableInit() { CBZip2Crc::InitTable(); } +} g_BZip2CrcTableInit; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BZip2Crc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BZip2Crc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,31 @@ +// BZip2Crc.h + +#ifndef __BZIP2_CRC_H +#define __BZIP2_CRC_H + +#include "Common/Types.h" + +class CBZip2Crc +{ + UInt32 _value; + static UInt32 Table[256]; +public: + static void InitTable(); + CBZip2Crc(): _value(0xFFFFFFFF) {}; + void Init() { _value = 0xFFFFFFFF; } + void UpdateByte(Byte b) { _value = Table[(_value >> 24) ^ b] ^ (_value << 8); } + void UpdateByte(unsigned int b) { _value = Table[(_value >> 24) ^ b] ^ (_value << 8); } + UInt32 GetDigest() const { return _value ^ 0xFFFFFFFF; } +}; + +class CBZip2CombinedCrc +{ + UInt32 _value; +public: + CBZip2CombinedCrc(): _value(0){}; + void Init() { _value = 0; } + void Update(UInt32 v) { _value = ((_value << 1) | (_value >> 31)) ^ v; } + UInt32 GetDigest() const { return _value ; } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BZip2Decoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BZip2Decoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,791 @@ +// BZip2Decoder.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/Alloc.h" +} + +#include "../../Common/Defs.h" + +#include "BZip2Crc.h" +#include "BZip2Decoder.h" +#include "Mtf8.h" + +namespace NCompress { +namespace NBZip2 { + +#define NO_INLINE MY_FAST_CALL + +const UInt32 kNumThreadsMax = 4; + +static const UInt32 kBufferSize = (1 << 17); + +static Int16 kRandNums[512] = { + 619, 720, 127, 481, 931, 816, 813, 233, 566, 247, + 985, 724, 205, 454, 863, 491, 741, 242, 949, 214, + 733, 859, 335, 708, 621, 574, 73, 654, 730, 472, + 419, 436, 278, 496, 867, 210, 399, 680, 480, 51, + 878, 465, 811, 169, 869, 675, 611, 697, 867, 561, + 862, 687, 507, 283, 482, 129, 807, 591, 733, 623, + 150, 238, 59, 379, 684, 877, 625, 169, 643, 105, + 170, 607, 520, 932, 727, 476, 693, 425, 174, 647, + 73, 122, 335, 530, 442, 853, 695, 249, 445, 515, + 909, 545, 703, 919, 874, 474, 882, 500, 594, 612, + 641, 801, 220, 162, 819, 984, 589, 513, 495, 799, + 161, 604, 958, 533, 221, 400, 386, 867, 600, 782, + 382, 596, 414, 171, 516, 375, 682, 485, 911, 276, + 98, 553, 163, 354, 666, 933, 424, 341, 533, 870, + 227, 730, 475, 186, 263, 647, 537, 686, 600, 224, + 469, 68, 770, 919, 190, 373, 294, 822, 808, 206, + 184, 943, 795, 384, 383, 461, 404, 758, 839, 887, + 715, 67, 618, 276, 204, 918, 873, 777, 604, 560, + 951, 160, 578, 722, 79, 804, 96, 409, 713, 940, + 652, 934, 970, 447, 318, 353, 859, 672, 112, 785, + 645, 863, 803, 350, 139, 93, 354, 99, 820, 908, + 609, 772, 154, 274, 580, 184, 79, 626, 630, 742, + 653, 282, 762, 623, 680, 81, 927, 626, 789, 125, + 411, 521, 938, 300, 821, 78, 343, 175, 128, 250, + 170, 774, 972, 275, 999, 639, 495, 78, 352, 126, + 857, 956, 358, 619, 580, 124, 737, 594, 701, 612, + 669, 112, 134, 694, 363, 992, 809, 743, 168, 974, + 944, 375, 748, 52, 600, 747, 642, 182, 862, 81, + 344, 805, 988, 739, 511, 655, 814, 334, 249, 515, + 897, 955, 664, 981, 649, 113, 974, 459, 893, 228, + 433, 837, 553, 268, 926, 240, 102, 654, 459, 51, + 686, 754, 806, 760, 493, 403, 415, 394, 687, 700, + 946, 670, 656, 610, 738, 392, 760, 799, 887, 653, + 978, 321, 576, 617, 626, 502, 894, 679, 243, 440, + 680, 879, 194, 572, 640, 724, 926, 56, 204, 700, + 707, 151, 457, 449, 797, 195, 791, 558, 945, 679, + 297, 59, 87, 824, 713, 663, 412, 693, 342, 606, + 134, 108, 571, 364, 631, 212, 174, 643, 304, 329, + 343, 97, 430, 751, 497, 314, 983, 374, 822, 928, + 140, 206, 73, 263, 980, 736, 876, 478, 430, 305, + 170, 514, 364, 692, 829, 82, 855, 953, 676, 246, + 369, 970, 294, 750, 807, 827, 150, 790, 288, 923, + 804, 378, 215, 828, 592, 281, 565, 555, 710, 82, + 896, 831, 547, 261, 524, 462, 293, 465, 502, 56, + 661, 821, 976, 991, 658, 869, 905, 758, 745, 193, + 768, 550, 608, 933, 378, 286, 215, 979, 792, 961, + 61, 688, 793, 644, 986, 403, 106, 366, 905, 644, + 372, 567, 466, 434, 645, 210, 389, 550, 919, 135, + 780, 773, 635, 389, 707, 100, 626, 958, 165, 504, + 920, 176, 193, 713, 857, 265, 203, 50, 668, 108, + 645, 990, 626, 197, 510, 357, 358, 850, 858, 364, + 936, 638 +}; + +bool CState::Alloc() +{ + if (Counters == 0) + Counters = (UInt32 *)BigAlloc((256 + kBlockSizeMax) * sizeof(UInt32)); + return (Counters != 0); +} + +void CState::Free() +{ + ::BigFree(Counters); + Counters = 0; +} + +UInt32 CDecoder::ReadBits(int numBits) { return m_InStream.ReadBits(numBits); } +Byte CDecoder::ReadByte() {return (Byte)ReadBits(8); } +bool CDecoder::ReadBit() { return ReadBits(1) != 0; } + +UInt32 CDecoder::ReadCrc() +{ + UInt32 crc = 0; + for (int i = 0; i < 4; i++) + { + crc <<= 8; + crc |= ReadByte(); + } + return crc; +} + +UInt32 NO_INLINE ReadBits(NBitm::CDecoder *m_InStream, int num) +{ + return m_InStream->ReadBits(num); +} + +UInt32 NO_INLINE ReadBit(NBitm::CDecoder *m_InStream) +{ + return m_InStream->ReadBits(1); +} + +static HRESULT NO_INLINE ReadBlock(NBitm::CDecoder *m_InStream, + UInt32 *CharCounters, UInt32 blockSizeMax, Byte *m_Selectors, CHuffmanDecoder *m_HuffmanDecoders, + UInt32 *blockSizeRes, UInt32 *origPtrRes, bool *randRes) +{ + *randRes = ReadBit(m_InStream) ? true : false; + *origPtrRes = ReadBits(m_InStream, kNumOrigBits); + + // in original code it compares OrigPtr to (UInt32)(10 + blockSizeMax)) : why ? + if (*origPtrRes >= blockSizeMax) + return S_FALSE; + + CMtf8Decoder mtf; + mtf.StartInit(); + + int numInUse = 0; + { + Byte inUse16[16]; + int i; + for (i = 0; i < 16; i++) + inUse16[i] = (Byte)ReadBit(m_InStream); + for (i = 0; i < 256; i++) + if (inUse16[i >> 4]) + { + if (ReadBit(m_InStream)) + mtf.Add(numInUse++, (Byte)i); + } + if (numInUse == 0) + return S_FALSE; + // mtf.Init(numInUse); + } + int alphaSize = numInUse + 2; + + int numTables = ReadBits(m_InStream, kNumTablesBits); + if (numTables < kNumTablesMin || numTables > kNumTablesMax) + return S_FALSE; + + UInt32 numSelectors = ReadBits(m_InStream, kNumSelectorsBits); + if (numSelectors < 1 || numSelectors > kNumSelectorsMax) + return S_FALSE; + + { + Byte mtfPos[kNumTablesMax]; + int t = 0; + do + mtfPos[t] = (Byte)t; + while(++t < numTables); + UInt32 i = 0; + do + { + int j = 0; + while (ReadBit(m_InStream)) + if (++j >= numTables) + return S_FALSE; + Byte tmp = mtfPos[j]; + for (;j > 0; j--) + mtfPos[j] = mtfPos[j - 1]; + m_Selectors[i] = mtfPos[0] = tmp; + } + while(++i < numSelectors); + } + + int t = 0; + do + { + Byte lens[kMaxAlphaSize]; + int len = (int)ReadBits(m_InStream, kNumLevelsBits); + int i; + for (i = 0; i < alphaSize; i++) + { + for (;;) + { + if (len < 1 || len > kMaxHuffmanLen) + return S_FALSE; + if (!ReadBit(m_InStream)) + break; + len += 1 - (int)(ReadBit(m_InStream) << 1); + } + lens[i] = (Byte)len; + } + for (; i < kMaxAlphaSize; i++) + lens[i] = 0; + if(!m_HuffmanDecoders[t].SetCodeLengths(lens)) + return S_FALSE; + } + while(++t < numTables); + + { + for (int i = 0; i < 256; i++) + CharCounters[i] = 0; + } + + UInt32 blockSize = 0; + { + UInt32 groupIndex = 0; + UInt32 groupSize = 0; + CHuffmanDecoder *huffmanDecoder = 0; + int runPower = 0; + UInt32 runCounter = 0; + + for (;;) + { + if (groupSize == 0) + { + if (groupIndex >= numSelectors) + return S_FALSE; + groupSize = kGroupSize; + huffmanDecoder = &m_HuffmanDecoders[m_Selectors[groupIndex++]]; + } + groupSize--; + + UInt32 nextSym = huffmanDecoder->DecodeSymbol(m_InStream); + + if (nextSym < 2) + { + runCounter += ((UInt32)(nextSym + 1) << runPower++); + if (blockSizeMax - blockSize < runCounter) + return S_FALSE; + continue; + } + if (runCounter != 0) + { + UInt32 b = (UInt32)mtf.GetHead(); + CharCounters[b] += runCounter; + do + CharCounters[256 + blockSize++] = b; + while(--runCounter != 0); + runPower = 0; + } + if (nextSym <= (UInt32)numInUse) + { + UInt32 b = (UInt32)mtf.GetAndMove((int)nextSym - 1); + if (blockSize >= blockSizeMax) + return S_FALSE; + CharCounters[b]++; + CharCounters[256 + blockSize++] = b; + } + else if (nextSym == (UInt32)numInUse + 1) + break; + else + return S_FALSE; + } + } + *blockSizeRes = blockSize; + return (*origPtrRes < blockSize) ? S_OK : S_FALSE; +} + +void NO_INLINE DecodeBlock1(UInt32 *charCounters, UInt32 blockSize) +{ + { + UInt32 sum = 0; + for (UInt32 i = 0; i < 256; i++) + { + sum += charCounters[i]; + charCounters[i] = sum - charCounters[i]; + } + } + + UInt32 *tt = charCounters + 256; + // Compute the T^(-1) vector + UInt32 i = 0; + do + tt[charCounters[tt[i] & 0xFF]++] |= (i << 8); + while(++i < blockSize); +} + +static UInt32 NO_INLINE DecodeBlock2(const UInt32 *tt, UInt32 blockSize, UInt32 OrigPtr, COutBuffer &m_OutStream) +{ + CBZip2Crc crc; + + // it's for speed optimization: prefetch & prevByte_init; + UInt32 tPos = tt[tt[OrigPtr] >> 8]; + unsigned int prevByte = (unsigned int)(tPos & 0xFF); + + int numReps = 0; + + do + { + unsigned int b = (unsigned int)(tPos & 0xFF); + tPos = tt[tPos >> 8]; + + if (numReps == kRleModeRepSize) + { + for (; b > 0; b--) + { + crc.UpdateByte(prevByte); + m_OutStream.WriteByte((Byte)prevByte); + } + numReps = 0; + continue; + } + if (b != prevByte) + numReps = 0; + numReps++; + prevByte = b; + crc.UpdateByte(b); + m_OutStream.WriteByte((Byte)b); + + /* + prevByte = b; + crc.UpdateByte(b); + m_OutStream.WriteByte((Byte)b); + for (; --blockSize != 0;) + { + b = (unsigned int)(tPos & 0xFF); + tPos = tt[tPos >> 8]; + crc.UpdateByte(b); + m_OutStream.WriteByte((Byte)b); + if (b != prevByte) + { + prevByte = b; + continue; + } + if (--blockSize == 0) + break; + + b = (unsigned int)(tPos & 0xFF); + tPos = tt[tPos >> 8]; + crc.UpdateByte(b); + m_OutStream.WriteByte((Byte)b); + if (b != prevByte) + { + prevByte = b; + continue; + } + if (--blockSize == 0) + break; + + b = (unsigned int)(tPos & 0xFF); + tPos = tt[tPos >> 8]; + crc.UpdateByte(b); + m_OutStream.WriteByte((Byte)b); + if (b != prevByte) + { + prevByte = b; + continue; + } + --blockSize; + break; + } + if (blockSize == 0) + break; + + b = (unsigned int)(tPos & 0xFF); + tPos = tt[tPos >> 8]; + + for (; b > 0; b--) + { + crc.UpdateByte(prevByte); + m_OutStream.WriteByte((Byte)prevByte); + } + */ + } + while(--blockSize != 0); + return crc.GetDigest(); +} + +static UInt32 NO_INLINE DecodeBlock2Rand(const UInt32 *tt, UInt32 blockSize, UInt32 OrigPtr, COutBuffer &m_OutStream) +{ + CBZip2Crc crc; + + UInt32 randIndex = 1; + UInt32 randToGo = kRandNums[0] - 2; + + int numReps = 0; + + // it's for speed optimization: prefetch & prevByte_init; + UInt32 tPos = tt[tt[OrigPtr] >> 8]; + unsigned int prevByte = (unsigned int)(tPos & 0xFF); + + do + { + unsigned int b = (unsigned int)(tPos & 0xFF); + tPos = tt[tPos >> 8]; + + { + if (randToGo == 0) + { + b ^= 1; + randToGo = kRandNums[randIndex++]; + randIndex &= 0x1FF; + } + randToGo--; + } + + if (numReps == kRleModeRepSize) + { + for (; b > 0; b--) + { + crc.UpdateByte(prevByte); + m_OutStream.WriteByte((Byte)prevByte); + } + numReps = 0; + continue; + } + if (b != prevByte) + numReps = 0; + numReps++; + prevByte = b; + crc.UpdateByte(b); + m_OutStream.WriteByte((Byte)b); + } + while(--blockSize != 0); + return crc.GetDigest(); +} + +#ifdef COMPRESS_BZIP2_MT + +CDecoder::CDecoder(): + m_States(0) +{ + m_NumThreadsPrev = 0; + NumThreads = 1; +} + +CDecoder::~CDecoder() +{ + Free(); +} + +#define RINOK_THREAD(x) { WRes __result_ = (x); if(__result_ != 0) return __result_; } + +HRESULT CDecoder::Create() +{ + RINOK_THREAD(CanProcessEvent.CreateIfNotCreated()); + RINOK_THREAD(CanStartWaitingEvent.CreateIfNotCreated()); + if (m_States != 0 && m_NumThreadsPrev == NumThreads) + return S_OK; + Free(); + MtMode = (NumThreads > 1); + m_NumThreadsPrev = NumThreads; + try + { + m_States = new CState[NumThreads]; + if (m_States == 0) + return E_OUTOFMEMORY; + } + catch(...) { return E_OUTOFMEMORY; } + for (UInt32 t = 0; t < NumThreads; t++) + { + CState &ti = m_States[t]; + ti.Decoder = this; + if (MtMode) + { + HRESULT res = ti.Create(); + if (res != S_OK) + { + NumThreads = t; + Free(); + return res; + } + } + } + return S_OK; +} + +void CDecoder::Free() +{ + if (!m_States) + return; + CloseThreads = true; + CanProcessEvent.Set(); + for (UInt32 t = 0; t < NumThreads; t++) + { + CState &s = m_States[t]; + if (MtMode) + s.Thread.Wait(); + s.Free(); + } + delete []m_States; + m_States = 0; +} +#endif + +HRESULT CDecoder::ReadSignatures(bool &wasFinished, UInt32 &crc) +{ + wasFinished = false; + Byte s[6]; + for (int i = 0; i < 6; i++) + s[i] = ReadByte(); + crc = ReadCrc(); + if (s[0] == kFinSig0) + { + if (s[1] != kFinSig1 || + s[2] != kFinSig2 || + s[3] != kFinSig3 || + s[4] != kFinSig4 || + s[5] != kFinSig5) + return S_FALSE; + + wasFinished = true; + return (crc == CombinedCrc.GetDigest()) ? S_OK : S_FALSE; + } + if (s[0] != kBlockSig0 || + s[1] != kBlockSig1 || + s[2] != kBlockSig2 || + s[3] != kBlockSig3 || + s[4] != kBlockSig4 || + s[5] != kBlockSig5) + return S_FALSE; + CombinedCrc.Update(crc); + return S_OK; +} + +HRESULT CDecoder::DecodeFile(bool &isBZ, ICompressProgressInfo *progress) +{ + #ifdef COMPRESS_BZIP2_MT + Progress = progress; + RINOK(Create()); + for (UInt32 t = 0; t < NumThreads; t++) + { + CState &s = m_States[t]; + if (!s.Alloc()) + return E_OUTOFMEMORY; + if (MtMode) + { + RINOK(s.StreamWasFinishedEvent.Reset()); + RINOK(s.WaitingWasStartedEvent.Reset()); + RINOK(s.CanWriteEvent.Reset()); + } + } + #else + if (!m_States[0].Alloc()) + return E_OUTOFMEMORY; + #endif + + isBZ = false; + Byte s[6]; + int i; + for (i = 0; i < 4; i++) + s[i] = ReadByte(); + if (s[0] != kArSig0 || + s[1] != kArSig1 || + s[2] != kArSig2 || + s[3] <= kArSig3 || + s[3] > kArSig3 + kBlockSizeMultMax) + return S_OK; + isBZ = true; + UInt32 dicSize = (UInt32)(s[3] - kArSig3) * kBlockSizeStep; + + CombinedCrc.Init(); + #ifdef COMPRESS_BZIP2_MT + if (MtMode) + { + NextBlockIndex = 0; + StreamWasFinished1 = StreamWasFinished2 = false; + CloseThreads = false; + CanStartWaitingEvent.Reset(); + m_States[0].CanWriteEvent.Set(); + BlockSizeMax = dicSize; + Result1 = Result2 = S_OK; + CanProcessEvent.Set(); + UInt32 t; + for (t = 0; t < NumThreads; t++) + m_States[t].StreamWasFinishedEvent.Lock(); + CanProcessEvent.Reset(); + CanStartWaitingEvent.Set(); + for (t = 0; t < NumThreads; t++) + m_States[t].WaitingWasStartedEvent.Lock(); + CanStartWaitingEvent.Reset(); + RINOK(Result2); + RINOK(Result1); + } + else + #endif + { + CState &state = m_States[0]; + for (;;) + { + if (progress) + { + UInt64 packSize = m_InStream.GetProcessedSize(); + UInt64 unpackSize = m_OutStream.GetProcessedSize(); + RINOK(progress->SetRatioInfo(&packSize, &unpackSize)); + } + bool wasFinished; + UInt32 crc; + RINOK(ReadSignatures(wasFinished, crc)); + if (wasFinished) + return S_OK; + + UInt32 blockSize, origPtr; + bool randMode; + RINOK(ReadBlock(&m_InStream, state.Counters, dicSize, + m_Selectors, m_HuffmanDecoders, + &blockSize, &origPtr, &randMode)); + DecodeBlock1(state.Counters, blockSize); + if ((randMode ? + DecodeBlock2Rand(state.Counters + 256, blockSize, origPtr, m_OutStream) : + DecodeBlock2(state.Counters + 256, blockSize, origPtr, m_OutStream)) != crc) + return S_FALSE; + } + } + return S_OK; +} + +HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress) +{ + if (!m_InStream.Create(kBufferSize)) + return E_OUTOFMEMORY; + if (!m_OutStream.Create(kBufferSize)) + return E_OUTOFMEMORY; + + m_InStream.SetStream(inStream); + m_InStream.Init(); + + m_OutStream.SetStream(outStream); + m_OutStream.Init(); + + CDecoderFlusher flusher(this); + + bool isBZ; + RINOK(DecodeFile(isBZ, progress)); + return isBZ ? S_OK: S_FALSE; +} + +STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + try { return CodeReal(inStream, outStream, inSize, outSize, progress); } + catch(const CInBufferException &e) { return e.ErrorCode; } + catch(const COutBufferException &e) { return e.ErrorCode; } + catch(...) { return E_FAIL; } +} + +STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value) +{ + if (value == NULL) + return E_INVALIDARG; + *value = m_InStream.GetProcessedSize(); + return S_OK; +} + +#ifdef COMPRESS_BZIP2_MT + +static THREAD_FUNC_DECL MFThread(void *p) { ((CState *)p)->ThreadFunc(); return 0; } + +HRESULT CState::Create() +{ + RINOK_THREAD(StreamWasFinishedEvent.CreateIfNotCreated()); + RINOK_THREAD(WaitingWasStartedEvent.CreateIfNotCreated()); + RINOK_THREAD(CanWriteEvent.CreateIfNotCreated()); + RINOK_THREAD(Thread.Create(MFThread, this)); + return S_OK; +} + +void CState::FinishStream() +{ + Decoder->StreamWasFinished1 = true; + StreamWasFinishedEvent.Set(); + Decoder->CS.Leave(); + Decoder->CanStartWaitingEvent.Lock(); + WaitingWasStartedEvent.Set(); +} + +void CState::ThreadFunc() +{ + for (;;) + { + Decoder->CanProcessEvent.Lock(); + Decoder->CS.Enter(); + if (Decoder->CloseThreads) + { + Decoder->CS.Leave(); + return; + } + if (Decoder->StreamWasFinished1) + { + FinishStream(); + continue; + } + HRESULT res = S_OK; + + UInt32 blockIndex = Decoder->NextBlockIndex; + UInt32 nextBlockIndex = blockIndex + 1; + if (nextBlockIndex == Decoder->NumThreads) + nextBlockIndex = 0; + Decoder->NextBlockIndex = nextBlockIndex; + UInt32 crc; + UInt64 packSize; + UInt32 blockSize = 0, origPtr = 0; + bool randMode = false; + + try + { + bool wasFinished; + res = Decoder->ReadSignatures(wasFinished, crc); + if (res != S_OK) + { + Decoder->Result1 = res; + FinishStream(); + continue; + } + if (wasFinished) + { + Decoder->Result1 = res; + FinishStream(); + continue; + } + + res = ReadBlock(&Decoder->m_InStream, Counters, Decoder->BlockSizeMax, + Decoder->m_Selectors, Decoder->m_HuffmanDecoders, + &blockSize, &origPtr, &randMode); + if (res != S_OK) + { + Decoder->Result1 = res; + FinishStream(); + continue; + } + packSize = Decoder->m_InStream.GetProcessedSize(); + } + catch(const CInBufferException &e) { res = e.ErrorCode; if (res != S_OK) res = E_FAIL; } + catch(...) { res = E_FAIL; } + if (res != S_OK) + { + Decoder->Result1 = res; + FinishStream(); + continue; + } + + Decoder->CS.Leave(); + + DecodeBlock1(Counters, blockSize); + + bool needFinish = true; + try + { + Decoder->m_States[blockIndex].CanWriteEvent.Lock(); + needFinish = Decoder->StreamWasFinished2; + if (!needFinish) + { + if ((randMode ? + DecodeBlock2Rand(Counters + 256, blockSize, origPtr, Decoder->m_OutStream) : + DecodeBlock2(Counters + 256, blockSize, origPtr, Decoder->m_OutStream)) == crc) + { + if (Decoder->Progress) + { + UInt64 unpackSize = Decoder->m_OutStream.GetProcessedSize(); + res = Decoder->Progress->SetRatioInfo(&packSize, &unpackSize); + } + } + else + res = S_FALSE; + } + } + catch(const COutBufferException &e) { res = e.ErrorCode; if (res != S_OK) res = E_FAIL; } + catch(...) { res = E_FAIL; } + if (res != S_OK) + { + Decoder->Result2 = res; + Decoder->StreamWasFinished2 = true; + } + Decoder->m_States[nextBlockIndex].CanWriteEvent.Set(); + if (res != S_OK || needFinish) + { + StreamWasFinishedEvent.Set(); + Decoder->CanStartWaitingEvent.Lock(); + WaitingWasStartedEvent.Set(); + } + } +} + +STDMETHODIMP CDecoder::SetNumberOfThreads(UInt32 numThreads) +{ + NumThreads = numThreads; + if (NumThreads < 1) + NumThreads = 1; + if (NumThreads > kNumThreadsMax) + NumThreads = kNumThreadsMax; + return S_OK; +} +#endif + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BZip2Decoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BZip2Decoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,159 @@ +// Compress/BZip2Decoder.h + +#ifndef __COMPRESS_BZIP2_DECODER_H +#define __COMPRESS_BZIP2_DECODER_H + +#include "../../Common/MyCom.h" + +#ifdef COMPRESS_BZIP2_MT +#include "../../Windows/Synchronization.h" +#include "../../Windows/Thread.h" +#endif + +#include "../ICoder.h" + +#include "../Common/InBuffer.h" +#include "../Common/OutBuffer.h" + +#include "BitmDecoder.h" +#include "BZip2Const.h" +#include "BZip2Crc.h" +#include "HuffmanDecoder.h" + +namespace NCompress { +namespace NBZip2 { + +typedef NCompress::NHuffman::CDecoder CHuffmanDecoder; + +class CDecoder; + +struct CState +{ + UInt32 *Counters; + + #ifdef COMPRESS_BZIP2_MT + + CDecoder *Decoder; + NWindows::CThread Thread; + bool m_OptimizeNumTables; + + NWindows::NSynchronization::CAutoResetEvent StreamWasFinishedEvent; + NWindows::NSynchronization::CAutoResetEvent WaitingWasStartedEvent; + + // it's not member of this thread. We just need one event per thread + NWindows::NSynchronization::CAutoResetEvent CanWriteEvent; + + Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size. + + HRESULT Create(); + void FinishStream(); + void ThreadFunc(); + + #endif + + CState(): Counters(0) {} + ~CState() { Free(); } + bool Alloc(); + void Free(); +}; + +class CDecoder : + public ICompressCoder, + #ifdef COMPRESS_BZIP2_MT + public ICompressSetCoderMt, + #endif + public ICompressGetInStreamProcessedSize, + public CMyUnknownImp +{ +public: + COutBuffer m_OutStream; + Byte MtPad[1 << 8]; // It's pad for Multi-Threading. Must be >= Cache_Line_Size. + NBitm::CDecoder m_InStream; + Byte m_Selectors[kNumSelectorsMax]; + CHuffmanDecoder m_HuffmanDecoders[kNumTablesMax]; +private: + + UInt32 m_NumThreadsPrev; + + UInt32 ReadBits(int numBits); + Byte ReadByte(); + bool ReadBit(); + UInt32 ReadCrc(); + HRESULT PrepareBlock(CState &state); + HRESULT DecodeFile(bool &isBZ, ICompressProgressInfo *progress); + HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + class CDecoderFlusher + { + CDecoder *_decoder; + public: + bool NeedFlush; + CDecoderFlusher(CDecoder *decoder): _decoder(decoder), NeedFlush(true) {} + ~CDecoderFlusher() + { + if (NeedFlush) + _decoder->Flush(); + _decoder->ReleaseStreams(); + } + }; + +public: + CBZip2CombinedCrc CombinedCrc; + + #ifdef COMPRESS_BZIP2_MT + ICompressProgressInfo *Progress; + CState *m_States; + + NWindows::NSynchronization::CManualResetEvent CanProcessEvent; + NWindows::NSynchronization::CCriticalSection CS; + UInt32 NumThreads; + bool MtMode; + UInt32 NextBlockIndex; + bool CloseThreads; + bool StreamWasFinished1; + bool StreamWasFinished2; + NWindows::NSynchronization::CManualResetEvent CanStartWaitingEvent; + + HRESULT Result1; + HRESULT Result2; + + UInt32 BlockSizeMax; + CDecoder(); + ~CDecoder(); + HRESULT Create(); + void Free(); + + #else + CState m_States[1]; + #endif + + HRESULT ReadSignatures(bool &wasFinished, UInt32 &crc); + + + HRESULT Flush() { return m_OutStream.Flush(); } + void ReleaseStreams() + { + m_InStream.ReleaseStream(); + m_OutStream.ReleaseStream(); + } + + #ifdef COMPRESS_BZIP2_MT + MY_UNKNOWN_IMP2(ICompressSetCoderMt, ICompressGetInStreamProcessedSize) + #else + MY_UNKNOWN_IMP1(ICompressGetInStreamProcessedSize) + #endif + + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(GetInStreamProcessedSize)(UInt64 *value); + + #ifdef COMPRESS_BZIP2_MT + STDMETHOD(SetNumberOfThreads)(UInt32 numThreads); + #endif +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BZip2Register.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BZip2Register.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// BZip2Register.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "BZip2Decoder.h" + +static void *CreateCodec() { return (void *)(ICompressCoder *)(new NCompress::NBZip2::CDecoder); } +#if !defined(EXTRACT_ONLY) && !defined(BZIP2_EXTRACT_ONLY) +#include "BZip2Encoder.h" +static void *CreateCodecOut() { return (void *)(ICompressCoder *)(new NCompress::NBZip2::CEncoder); } +#else +#define CreateCodecOut 0 +#endif + +static CCodecInfo g_CodecInfo = + { CreateCodec, CreateCodecOut, 0x040202, L"BZip2", 1, false }; + +REGISTER_CODEC(BZip2) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Bcj2Coder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Bcj2Coder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,393 @@ +// Bcj2Coder.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/Alloc.h" +} + +#include "Bcj2Coder.h" + +namespace NCompress { +namespace NBcj2 { + +inline bool IsJcc(Byte b0, Byte b1) { return (b0 == 0x0F && (b1 & 0xF0) == 0x80); } +inline bool IsJ(Byte b0, Byte b1) { return ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1)); } +inline unsigned GetIndex(Byte b0, Byte b1) { return ((b1 == 0xE8) ? b0 : ((b1 == 0xE9) ? 256 : 257)); } + +#ifndef EXTRACT_ONLY + +static const int kBufferSize = 1 << 17; + +static bool inline Test86MSByte(Byte b) +{ + return (b == 0 || b == 0xFF); +} + +bool CEncoder::Create() +{ + if (!_mainStream.Create(1 << 16)) + return false; + if (!_callStream.Create(1 << 20)) + return false; + if (!_jumpStream.Create(1 << 20)) + return false; + if (!_rangeEncoder.Create(1 << 20)) + return false; + if (_buffer == 0) + { + _buffer = (Byte *)MidAlloc(kBufferSize); + if (_buffer == 0) + return false; + } + return true; +} + +CEncoder::~CEncoder() +{ + ::MidFree(_buffer); +} + +HRESULT CEncoder::Flush() +{ + RINOK(_mainStream.Flush()); + RINOK(_callStream.Flush()); + RINOK(_jumpStream.Flush()); + _rangeEncoder.FlushData(); + return _rangeEncoder.FlushStream(); +} + +const UInt32 kDefaultLimit = (1 << 24); + +HRESULT CEncoder::CodeReal(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 ** /* outSizes */, + UInt32 numOutStreams, + ICompressProgressInfo *progress) +{ + if (numInStreams != 1 || numOutStreams != 4) + return E_INVALIDARG; + + if (!Create()) + return E_OUTOFMEMORY; + + bool sizeIsDefined = false; + UInt64 inSize = 0; + if (inSizes != NULL) + if (inSizes[0] != NULL) + { + inSize = *inSizes[0]; + if (inSize <= kDefaultLimit) + sizeIsDefined = true; + } + + ISequentialInStream *inStream = inStreams[0]; + + _mainStream.SetStream(outStreams[0]); + _mainStream.Init(); + _callStream.SetStream(outStreams[1]); + _callStream.Init(); + _jumpStream.SetStream(outStreams[2]); + _jumpStream.Init(); + _rangeEncoder.SetStream(outStreams[3]); + _rangeEncoder.Init(); + for (int i = 0; i < 256 + 2; i++) + _statusEncoder[i].Init(); + CCoderReleaser releaser(this); + + CMyComPtr getSubStreamSize; + { + inStream->QueryInterface(IID_ICompressGetSubStreamSize, (void **)&getSubStreamSize); + } + + UInt32 nowPos = 0; + UInt64 nowPos64 = 0; + UInt32 bufferPos = 0; + + Byte prevByte = 0; + + UInt64 subStreamIndex = 0; + UInt64 subStreamStartPos = 0; + UInt64 subStreamEndPos = 0; + + for (;;) + { + UInt32 processedSize = 0; + for (;;) + { + UInt32 size = kBufferSize - (bufferPos + processedSize); + UInt32 processedSizeLoc; + if (size == 0) + break; + RINOK(inStream->Read(_buffer + bufferPos + processedSize, size, &processedSizeLoc)); + if (processedSizeLoc == 0) + break; + processedSize += processedSizeLoc; + } + UInt32 endPos = bufferPos + processedSize; + + if (endPos < 5) + { + // change it + for (bufferPos = 0; bufferPos < endPos; bufferPos++) + { + Byte b = _buffer[bufferPos]; + _mainStream.WriteByte(b); + UInt32 index; + if (b == 0xE8) + index = prevByte; + else if (b == 0xE9) + index = 256; + else if (IsJcc(prevByte, b)) + index = 257; + else + { + prevByte = b; + continue; + } + _statusEncoder[index].Encode(&_rangeEncoder, 0); + prevByte = b; + } + return Flush(); + } + + bufferPos = 0; + + UInt32 limit = endPos - 5; + while(bufferPos <= limit) + { + Byte b = _buffer[bufferPos]; + _mainStream.WriteByte(b); + if (!IsJ(prevByte, b)) + { + bufferPos++; + prevByte = b; + continue; + } + Byte nextByte = _buffer[bufferPos + 4]; + UInt32 src = + (UInt32(nextByte) << 24) | + (UInt32(_buffer[bufferPos + 3]) << 16) | + (UInt32(_buffer[bufferPos + 2]) << 8) | + (_buffer[bufferPos + 1]); + UInt32 dest = (nowPos + bufferPos + 5) + src; + // if (Test86MSByte(nextByte)) + bool convert; + if (getSubStreamSize != NULL) + { + UInt64 currentPos = (nowPos64 + bufferPos); + while (subStreamEndPos < currentPos) + { + UInt64 subStreamSize; + HRESULT result = getSubStreamSize->GetSubStreamSize(subStreamIndex, &subStreamSize); + if (result == S_OK) + { + subStreamStartPos = subStreamEndPos; + subStreamEndPos += subStreamSize; + subStreamIndex++; + } + else if (result == S_FALSE || result == E_NOTIMPL) + { + getSubStreamSize.Release(); + subStreamStartPos = 0; + subStreamEndPos = subStreamStartPos - 1; + } + else + return result; + } + if (getSubStreamSize == NULL) + { + if (sizeIsDefined) + convert = (dest < inSize); + else + convert = Test86MSByte(nextByte); + } + else if (subStreamEndPos - subStreamStartPos > kDefaultLimit) + convert = Test86MSByte(nextByte); + else + { + UInt64 dest64 = (currentPos + 5) + Int64(Int32(src)); + convert = (dest64 >= subStreamStartPos && dest64 < subStreamEndPos); + } + } + else if (sizeIsDefined) + convert = (dest < inSize); + else + convert = Test86MSByte(nextByte); + unsigned index = GetIndex(prevByte, b); + if (convert) + { + _statusEncoder[index].Encode(&_rangeEncoder, 1); + bufferPos += 5; + COutBuffer &s = (b == 0xE8) ? _callStream : _jumpStream; + for (int i = 24; i >= 0; i -= 8) + s.WriteByte((Byte)(dest >> i)); + prevByte = nextByte; + } + else + { + _statusEncoder[index].Encode(&_rangeEncoder, 0); + bufferPos++; + prevByte = b; + } + } + nowPos += bufferPos; + nowPos64 += bufferPos; + + if (progress != NULL) + { + /* + const UInt64 compressedSize = + _mainStream.GetProcessedSize() + + _callStream.GetProcessedSize() + + _jumpStream.GetProcessedSize() + + _rangeEncoder.GetProcessedSize(); + */ + RINOK(progress->SetRatioInfo(&nowPos64, NULL)); + } + + UInt32 i = 0; + while(bufferPos < endPos) + _buffer[i++] = _buffer[bufferPos++]; + bufferPos = i; + } +} + +STDMETHODIMP CEncoder::Code(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 **outSizes, + UInt32 numOutStreams, + ICompressProgressInfo *progress) +{ + try + { + return CodeReal(inStreams, inSizes, numInStreams, + outStreams, outSizes,numOutStreams, progress); + } + catch(const COutBufferException &e) { return e.ErrorCode; } + catch(...) { return S_FALSE; } +} + +#endif + +HRESULT CDecoder::CodeReal(ISequentialInStream **inStreams, + const UInt64 ** /* inSizes */, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 ** /* outSizes */, + UInt32 numOutStreams, + ICompressProgressInfo *progress) +{ + if (numInStreams != 4 || numOutStreams != 1) + return E_INVALIDARG; + + if (!_mainInStream.Create(1 << 16)) + return E_OUTOFMEMORY; + if (!_callStream.Create(1 << 20)) + return E_OUTOFMEMORY; + if (!_jumpStream.Create(1 << 16)) + return E_OUTOFMEMORY; + if (!_rangeDecoder.Create(1 << 20)) + return E_OUTOFMEMORY; + if (!_outStream.Create(1 << 16)) + return E_OUTOFMEMORY; + + _mainInStream.SetStream(inStreams[0]); + _callStream.SetStream(inStreams[1]); + _jumpStream.SetStream(inStreams[2]); + _rangeDecoder.SetStream(inStreams[3]); + _outStream.SetStream(outStreams[0]); + + _mainInStream.Init(); + _callStream.Init(); + _jumpStream.Init(); + _rangeDecoder.Init(); + _outStream.Init(); + + for (int i = 0; i < 256 + 2; i++) + _statusDecoder[i].Init(); + + CCoderReleaser releaser(this); + + Byte prevByte = 0; + UInt32 processedBytes = 0; + for (;;) + { + if (processedBytes >= (1 << 20) && progress != NULL) + { + /* + const UInt64 compressedSize = + _mainInStream.GetProcessedSize() + + _callStream.GetProcessedSize() + + _jumpStream.GetProcessedSize() + + _rangeDecoder.GetProcessedSize(); + */ + const UInt64 nowPos64 = _outStream.GetProcessedSize(); + RINOK(progress->SetRatioInfo(NULL, &nowPos64)); + processedBytes = 0; + } + UInt32 i; + Byte b = 0; + const UInt32 kBurstSize = (1 << 18); + for (i = 0; i < kBurstSize; i++) + { + if (!_mainInStream.ReadByte(b)) + return Flush(); + _outStream.WriteByte(b); + if (IsJ(prevByte, b)) + break; + prevByte = b; + } + processedBytes += i; + if (i == kBurstSize) + continue; + unsigned index = GetIndex(prevByte, b); + if (_statusDecoder[index].Decode(&_rangeDecoder) == 1) + { + UInt32 src = 0; + CInBuffer &s = (b == 0xE8) ? _callStream : _jumpStream; + for (int i = 0; i < 4; i++) + { + Byte b0; + if(!s.ReadByte(b0)) + return S_FALSE; + src <<= 8; + src |= ((UInt32)b0); + } + UInt32 dest = src - (UInt32(_outStream.GetProcessedSize()) + 4) ; + _outStream.WriteByte((Byte)(dest)); + _outStream.WriteByte((Byte)(dest >> 8)); + _outStream.WriteByte((Byte)(dest >> 16)); + _outStream.WriteByte((Byte)(dest >> 24)); + prevByte = (Byte)(dest >> 24); + processedBytes += 4; + } + else + prevByte = b; + } +} + +STDMETHODIMP CDecoder::Code(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 **outSizes, + UInt32 numOutStreams, + ICompressProgressInfo *progress) +{ + try + { + return CodeReal(inStreams, inSizes, numInStreams, + outStreams, outSizes,numOutStreams, progress); + } + catch(const CInBufferException &e) { return e.ErrorCode; } + catch(const COutBufferException &e) { return e.ErrorCode; } + catch(...) { return S_FALSE; } +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Bcj2Coder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Bcj2Coder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,125 @@ +// Bcj2Coder.h + +#ifndef __COMPRESS_BCJ2_CODER_H +#define __COMPRESS_BCJ2_CODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "RangeCoderBit.h" + +namespace NCompress { +namespace NBcj2 { + +const int kNumMoveBits = 5; + +#ifndef EXTRACT_ONLY + +class CEncoder: + public ICompressCoder2, + public CMyUnknownImp +{ + Byte *_buffer; +public: + CEncoder(): _buffer(0) {}; + ~CEncoder(); + bool Create(); + + COutBuffer _mainStream; + COutBuffer _callStream; + COutBuffer _jumpStream; + NCompress::NRangeCoder::CEncoder _rangeEncoder; + NCompress::NRangeCoder::CBitEncoder _statusEncoder[256 + 2]; + + HRESULT Flush(); + void ReleaseStreams() + { + _mainStream.ReleaseStream(); + _callStream.ReleaseStream(); + _jumpStream.ReleaseStream(); + _rangeEncoder.ReleaseStream(); + } + + class CCoderReleaser + { + CEncoder *_coder; + public: + CCoderReleaser(CEncoder *coder): _coder(coder) {} + ~CCoderReleaser() { _coder->ReleaseStreams(); } + }; + +public: + + MY_UNKNOWN_IMP + + HRESULT CodeReal(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 **outSizes, + UInt32 numOutStreams, + ICompressProgressInfo *progress); + STDMETHOD(Code)(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 **outSizes, + UInt32 numOutStreams, + ICompressProgressInfo *progress); +}; + +#endif + +class CDecoder: + public ICompressCoder2, + public CMyUnknownImp +{ +public: + CInBuffer _mainInStream; + CInBuffer _callStream; + CInBuffer _jumpStream; + NCompress::NRangeCoder::CDecoder _rangeDecoder; + NCompress::NRangeCoder::CBitDecoder _statusDecoder[256 + 2]; + + COutBuffer _outStream; + + void ReleaseStreams() + { + _mainInStream.ReleaseStream(); + _callStream.ReleaseStream(); + _jumpStream.ReleaseStream(); + _rangeDecoder.ReleaseStream(); + _outStream.ReleaseStream(); + } + + HRESULT Flush() { return _outStream.Flush(); } + class CCoderReleaser + { + CDecoder *_coder; + public: + CCoderReleaser(CDecoder *coder): _coder(coder) {} + ~CCoderReleaser() { _coder->ReleaseStreams(); } + }; + +public: + MY_UNKNOWN_IMP + HRESULT CodeReal(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 **outSizes, + UInt32 numOutStreams, + ICompressProgressInfo *progress); + STDMETHOD(Code)(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 **outSizes, + UInt32 numOutStreams, + ICompressProgressInfo *progress); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Bcj2Register.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Bcj2Register.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,19 @@ +// Bcj2Register.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "Bcj2Coder.h" + +static void *CreateCodec() { return (void *)(ICompressCoder2 *)(new NCompress::NBcj2::CDecoder()); } +#ifndef EXTRACT_ONLY +static void *CreateCodecOut() { return (void *)(ICompressCoder2 *)(new NCompress::NBcj2::CEncoder()); } +#else +#define CreateCodecOut 0 +#endif + +static CCodecInfo g_CodecInfo = + { CreateCodec, CreateCodecOut, 0x0303011B, L"BCJ2", 4, false }; + +REGISTER_CODEC(BCJ2) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BcjCoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BcjCoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,15 @@ +// BcjCoder.cpp + +#include "StdAfx.h" + +#include "BcjCoder.h" + +UInt32 CBCJ_x86_Encoder::SubFilter(Byte *data, UInt32 size) +{ + return (UInt32)::x86_Convert(data, size, _bufferPos, &_prevMask, 1); +} + +UInt32 CBCJ_x86_Decoder::SubFilter(Byte *data, UInt32 size) +{ + return (UInt32)::x86_Convert(data, size, _bufferPos, &_prevMask, 0); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BcjCoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BcjCoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,22 @@ +// BcjCoder.h + +#ifndef __COMPRESS_BCJ_CODER_H +#define __COMPRESS_BCJ_CODER_H + +extern "C" +{ +#include "../../../C/Bra.h" +} + +#include "BranchCoder.h" + +struct CBranch86 +{ + UInt32 _prevMask; + void x86Init() { x86_Convert_Init(_prevMask); } +}; + +MyClassB(BCJ_x86, 0x01, 3, CBranch86 , + virtual void SubInit() { x86Init(); }) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BcjRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BcjRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,19 @@ +// BcjRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "BcjCoder.h" + +static void *CreateCodec() { return (void *)(ICompressFilter *)(new CBCJ_x86_Decoder()); } +#ifndef EXTRACT_ONLY +static void *CreateCodecOut() { return (void *)(ICompressFilter *)(new CBCJ_x86_Encoder()); } +#else +#define CreateCodecOut 0 +#endif + +static CCodecInfo g_CodecInfo = + { CreateCodec, CreateCodecOut, 0x03030103, L"BCJ", 1, true }; + +REGISTER_CODEC(BCJ) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BitlDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BitlDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +// BitlDecoder.cpp + +#include "StdAfx.h" + +#include "BitlDecoder.h" + +namespace NBitl { + +Byte kInvertTable[256]; + +struct CInverterTableInitializer +{ + CInverterTableInitializer() + { + for (int i = 0; i < 256; i++) + { + int x = ((i & 0x55) << 1) | ((i & 0xAA) >> 1); + x = ((x & 0x33) << 2) | ((x & 0xCC) >> 2); + kInvertTable[i] = (Byte)(((x & 0x0F) << 4) | ((x & 0xF0) >> 4)); + } + } +} g_InverterTableInitializer; + +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BitlDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BitlDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,125 @@ +// BitlDecoder.h -- the Least Significant Bit of byte is First + +#ifndef __BITL_DECODER_H +#define __BITL_DECODER_H + +#include "../IStream.h" + +namespace NBitl { + +const int kNumBigValueBits = 8 * 4; + +const int kNumValueBytes = 3; +const int kNumValueBits = 8 * kNumValueBytes; + +const UInt32 kMask = (1 << kNumValueBits) - 1; + +extern Byte kInvertTable[256]; + +template +class CBaseDecoder +{ +protected: + int m_BitPos; + UInt32 m_Value; + TInByte m_Stream; +public: + UInt32 NumExtraBytes; + bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } + void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream); } + void ReleaseStream() { m_Stream.ReleaseStream(); } + void Init() + { + m_Stream.Init(); + m_BitPos = kNumBigValueBits; + m_Value = 0; + NumExtraBytes = 0; + } + UInt64 GetProcessedSize() const + { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; } + UInt64 GetProcessedBitsSize() const + { return (m_Stream.GetProcessedSize() << 3) - (kNumBigValueBits - m_BitPos); } + int GetBitPosition() const { return (m_BitPos & 7); } + + void Normalize() + { + for (;m_BitPos >= 8; m_BitPos -= 8) + { + Byte b = 0; + if (!m_Stream.ReadByte(b)) + { + b = 0xFF; // check it + NumExtraBytes++; + } + m_Value = (b << (kNumBigValueBits - m_BitPos)) | m_Value; + } + } + + UInt32 ReadBits(int numBits) + { + Normalize(); + UInt32 res = m_Value & ((1 << numBits) - 1); + m_BitPos += numBits; + m_Value >>= numBits; + return res; + } + + bool ExtraBitsWereRead() const + { + if (NumExtraBytes == 0) + return false; + return ((UInt32)(kNumBigValueBits - m_BitPos) < (NumExtraBytes << 3)); + } +}; + +template +class CDecoder: public CBaseDecoder +{ + UInt32 m_NormalValue; + +public: + void Init() + { + CBaseDecoder::Init(); + m_NormalValue = 0; + } + + void Normalize() + { + for (; this->m_BitPos >= 8; this->m_BitPos -= 8) + { + Byte b = 0; + if (!this->m_Stream.ReadByte(b)) + { + b = 0xFF; // check it + this->NumExtraBytes++; + } + m_NormalValue = (b << (kNumBigValueBits - this->m_BitPos)) | m_NormalValue; + this->m_Value = (this->m_Value << 8) | kInvertTable[b]; + } + } + + UInt32 GetValue(int numBits) + { + Normalize(); + return ((this->m_Value >> (8 - this->m_BitPos)) & kMask) >> (kNumValueBits - numBits); + } + + void MovePos(int numBits) + { + this->m_BitPos += numBits; + m_NormalValue >>= numBits; + } + + UInt32 ReadBits(int numBits) + { + Normalize(); + UInt32 res = m_NormalValue & ( (1 << numBits) - 1); + MovePos(numBits); + return res; + } +}; + +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BitmDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BitmDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,66 @@ +// BitmDecoder.h -- the Most Significant Bit of byte is First + +#ifndef __BITM_DECODER_H +#define __BITM_DECODER_H + +#include "../IStream.h" + +namespace NBitm { + +const int kNumBigValueBits = 8 * 4; +const int kNumValueBytes = 3; +const int kNumValueBits = 8 * kNumValueBytes; + +const UInt32 kMask = (1 << kNumValueBits) - 1; + +template +class CDecoder +{ + UInt32 m_BitPos; + UInt32 m_Value; +public: + TInByte m_Stream; + bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } + void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);} + void ReleaseStream() { m_Stream.ReleaseStream();} + + void Init() + { + m_Stream.Init(); + m_BitPos = kNumBigValueBits; + Normalize(); + } + + UInt64 GetProcessedSize() const + { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; } + UInt32 GetBitPosition() const { return (m_BitPos & 7); } + + void Normalize() + { + for (;m_BitPos >= 8; m_BitPos -= 8) + m_Value = (m_Value << 8) | m_Stream.ReadByte(); + } + + UInt32 GetValue(UInt32 numBits) const + { + // return (m_Value << m_BitPos) >> (kNumBigValueBits - numBits); + return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits); + } + + void MovePos(UInt32 numBits) + { + m_BitPos += numBits; + Normalize(); + } + + UInt32 ReadBits(UInt32 numBits) + { + UInt32 res = GetValue(numBits); + MovePos(numBits); + return res; + } +}; + +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BranchCoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BranchCoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,19 @@ +// BranchCoder.cpp + +#include "StdAfx.h" + +#include "BranchCoder.h" + +STDMETHODIMP CBranchConverter::Init() +{ + _bufferPos = 0; + SubInit(); + return S_OK; +} + +STDMETHODIMP_(UInt32) CBranchConverter::Filter(Byte *data, UInt32 size) +{ + UInt32 processedSize = SubFilter(data, size); + _bufferPos += processedSize; + return processedSize; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BranchCoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BranchCoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,44 @@ +// BranchCoder.h + +#ifndef __COMPRESS_BRANCH_CODER_H +#define __COMPRESS_BRANCH_CODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +class CBranchConverter: + public ICompressFilter, + public CMyUnknownImp +{ +protected: + UInt32 _bufferPos; + virtual void SubInit() {} + virtual UInt32 SubFilter(Byte *data, UInt32 size) = 0; +public: + MY_UNKNOWN_IMP; + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); +}; + +#define MyClassEncoderA(Name) class C ## Name: public CBranchConverter \ + { public: UInt32 SubFilter(Byte *data, UInt32 size); }; + +#define MyClassDecoderA(Name) class C ## Name: public CBranchConverter \ + { public: UInt32 SubFilter(Byte *data, UInt32 size); }; + +#define MyClassEncoderB(Name, ADD_ITEMS, ADD_INIT) class C ## Name: public CBranchConverter, public ADD_ITEMS \ + { public: UInt32 SubFilter(Byte *data, UInt32 size); ADD_INIT}; + +#define MyClassDecoderB(Name, ADD_ITEMS, ADD_INIT) class C ## Name: public CBranchConverter, public ADD_ITEMS \ + { public: UInt32 SubFilter(Byte *data, UInt32 size); ADD_INIT}; + +#define MyClassA(Name, id, subId) \ +MyClassEncoderA(Name ## _Encoder) \ +MyClassDecoderA(Name ## _Decoder) + +#define MyClassB(Name, id, subId, ADD_ITEMS, ADD_INIT) \ +MyClassEncoderB(Name ## _Encoder, ADD_ITEMS, ADD_INIT) \ +MyClassDecoderB(Name ## _Decoder, ADD_ITEMS, ADD_INIT) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BranchMisc.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BranchMisc.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,40 @@ +// BranchMisc.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/Bra.h" +} + +#include "BranchMisc.h" + +UInt32 CBC_ARM_Encoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::ARM_Convert(data, size, _bufferPos, 1); } + +UInt32 CBC_ARM_Decoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::ARM_Convert(data, size, _bufferPos, 0); } + +UInt32 CBC_ARMT_Encoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::ARMT_Convert(data, size, _bufferPos, 1); } + +UInt32 CBC_ARMT_Decoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::ARMT_Convert(data, size, _bufferPos, 0); } + +UInt32 CBC_PPC_Encoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::PPC_Convert(data, size, _bufferPos, 1); } + +UInt32 CBC_PPC_Decoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::PPC_Convert(data, size, _bufferPos, 0); } + +UInt32 CBC_SPARC_Encoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::SPARC_Convert(data, size, _bufferPos, 1); } + +UInt32 CBC_SPARC_Decoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::SPARC_Convert(data, size, _bufferPos, 0); } + +UInt32 CBC_IA64_Encoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::IA64_Convert(data, size, _bufferPos, 1); } + +UInt32 CBC_IA64_Decoder::SubFilter(Byte *data, UInt32 size) + { return (UInt32)::IA64_Convert(data, size, _bufferPos, 0); } diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BranchMisc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BranchMisc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +// BranchMisc.h + +#ifndef __COMPRESS_BRANCH_MISC_H +#define __COMPRESS_BRANCH_MISC_H + +#include "BranchCoder.h" + +MyClassA(BC_ARM, 0x05, 1) +MyClassA(BC_ARMT, 0x07, 1) +MyClassA(BC_PPC, 0x02, 5) +MyClassA(BC_SPARC, 0x08, 5) +MyClassA(BC_IA64, 0x04, 1) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/BranchRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/BranchRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +// BranchRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "BranchMisc.h" + +#define CREATE_CODEC(x) \ + static void *CreateCodec ## x() { return (void *)(ICompressFilter *)(new C ## x ## _Decoder); } \ + static void *CreateCodec ## x ## Out() { return (void *)(ICompressFilter *)(new C ## x ## _Encoder); } + +CREATE_CODEC(BC_PPC) +CREATE_CODEC(BC_IA64) +CREATE_CODEC(BC_ARM) +CREATE_CODEC(BC_ARMT) +CREATE_CODEC(BC_SPARC) + +#define METHOD_ITEM(x, id1, id2, name) { CreateCodec ## x, CreateCodec ## x ## Out, 0x03030000 + (id1 * 256) + id2, name, 1, true } + +static CCodecInfo g_CodecsInfo[] = +{ + METHOD_ITEM(BC_PPC, 0x02, 0x05, L"PPC"), + METHOD_ITEM(BC_IA64, 0x04, 1, L"IA64"), + METHOD_ITEM(BC_ARM, 0x05, 1, L"ARM"), + METHOD_ITEM(BC_ARMT, 0x07, 1, L"ARMT"), + METHOD_ITEM(BC_SPARC, 0x08, 0x05, L"SPARC") +}; + +REGISTER_CODECS(Branch) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ByteSwap.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ByteSwap.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,38 @@ +// ByteSwap.cpp + +#include "StdAfx.h" + +#include "ByteSwap.h" + +STDMETHODIMP CByteSwap2::Init() { return S_OK; } + +STDMETHODIMP_(UInt32) CByteSwap2::Filter(Byte *data, UInt32 size) +{ + const UInt32 kStep = 2; + UInt32 i; + for (i = 0; i + kStep <= size; i += kStep) + { + Byte b = data[i]; + data[i] = data[i + 1]; + data[i + 1] = b; + } + return i; +} + +STDMETHODIMP CByteSwap4::Init() { return S_OK; } + +STDMETHODIMP_(UInt32) CByteSwap4::Filter(Byte *data, UInt32 size) +{ + const UInt32 kStep = 4; + UInt32 i; + for (i = 0; i + kStep <= size; i += kStep) + { + Byte b0 = data[i]; + Byte b1 = data[i + 1]; + data[i] = data[i + 3]; + data[i + 1] = data[i + 2]; + data[i + 2] = b1; + data[i + 3] = b0; + } + return i; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ByteSwap.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ByteSwap.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +// ByteSwap.h + +#ifndef __COMPRESS_BYTE_SWAP_H +#define __COMPRESS_BYTE_SWAP_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +class CByteSwap2: + public ICompressFilter, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); +}; + +class CByteSwap4: + public ICompressFilter, + public CMyUnknownImp +{ +public: + MY_UNKNOWN_IMP + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ByteSwapRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ByteSwapRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// ByteSwapRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "ByteSwap.h" + +static void *CreateCodec2() { return (void *)(ICompressFilter *)(new CByteSwap2); } +static void *CreateCodec4() { return (void *)(ICompressFilter *)(new CByteSwap4); } + +static CCodecInfo g_CodecsInfo[] = +{ + { CreateCodec2, CreateCodec4, 0x020302, L"Swap2", 1, true }, + { CreateCodec4, CreateCodec4, 0x020304, L"Swap4", 1, true } +}; + +REGISTER_CODECS(ByteSwap) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Codec.def --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Codec.def Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,4 @@ +EXPORTS + CreateObject PRIVATE + GetNumberOfMethods PRIVATE + GetMethodProperty PRIVATE diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/CodecExports.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/CodecExports.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,157 @@ +// CodecExports.cpp + +#include "StdAfx.h" + +#include "../../Common/ComTry.h" +#include "../../Windows/PropVariant.h" +#include "../Common/RegisterCodec.h" +#include "../ICoder.h" + +extern unsigned int g_NumCodecs; +extern const CCodecInfo *g_Codecs[]; + +static const UInt16 kDecodeId = 0x2790; + +DEFINE_GUID(CLSID_CCodec, +0x23170F69, 0x40C1, kDecodeId, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); + +static inline HRESULT SetPropString(const char *s, unsigned int size, PROPVARIANT *value) +{ + if ((value->bstrVal = ::SysAllocStringByteLen(s, size)) != 0) + value->vt = VT_BSTR; + return S_OK; +} + +static inline HRESULT SetPropGUID(const GUID &guid, PROPVARIANT *value) +{ + return SetPropString((const char *)&guid, sizeof(GUID), value); +} + +static HRESULT SetClassID(CMethodId id, bool encode, PROPVARIANT *value) +{ + GUID clsId = CLSID_CCodec; + for (int i = 0; i < sizeof(id); i++, id >>= 8) + clsId.Data4[i] = (Byte)(id & 0xFF); + if (encode) + clsId.Data3++; + return SetPropGUID(clsId, value); +} + +static HRESULT FindCodecClassId(const GUID *clsID, UInt32 isCoder2, bool isFilter, bool &encode, int &index) +{ + index = -1; + if (clsID->Data1 != CLSID_CCodec.Data1 || + clsID->Data2 != CLSID_CCodec.Data2 || + (clsID->Data3 & ~1) != kDecodeId) + return S_OK; + encode = (clsID->Data3 != kDecodeId); + UInt64 id = 0; + for (int j = 0; j < 8; j++) + id |= ((UInt64)clsID->Data4[j]) << (8 * j); + for (unsigned i = 0; i < g_NumCodecs; i++) + { + const CCodecInfo &codec = *g_Codecs[i]; + if (id != codec.Id || encode && !codec.CreateEncoder || !encode && !codec.CreateDecoder) + continue; + if (!isFilter && codec.IsFilter || isFilter && !codec.IsFilter || + codec.NumInStreams != 1 && !isCoder2 || codec.NumInStreams == 1 && isCoder2) + return E_NOINTERFACE; + index = i; + return S_OK; + } + return S_OK; +} + +STDAPI CreateCoder2(bool encode, UInt32 index, const GUID *iid, void **outObject) +{ + COM_TRY_BEGIN + *outObject = 0; + bool isCoder = (*iid == IID_ICompressCoder) != 0; + bool isCoder2 = (*iid == IID_ICompressCoder2) != 0; + bool isFilter = (*iid == IID_ICompressFilter) != 0; + const CCodecInfo &codec = *g_Codecs[index]; + if (!isFilter && codec.IsFilter || isFilter && !codec.IsFilter || + codec.NumInStreams != 1 && !isCoder2 || codec.NumInStreams == 1 && isCoder2) + return E_NOINTERFACE; + if (encode) + { + if (!codec.CreateEncoder) + return CLASS_E_CLASSNOTAVAILABLE; + *outObject = codec.CreateEncoder(); + } + else + { + if (!codec.CreateDecoder) + return CLASS_E_CLASSNOTAVAILABLE; + *outObject = codec.CreateDecoder(); + } + if (isCoder) + ((ICompressCoder *)*outObject)->AddRef(); + else if (isCoder2) + ((ICompressCoder2 *)*outObject)->AddRef(); + else + ((ICompressFilter *)*outObject)->AddRef(); + return S_OK; + COM_TRY_END +} + +STDAPI CreateCoder(const GUID *clsid, const GUID *iid, void **outObject) +{ + *outObject = 0; + bool isCoder = (*iid == IID_ICompressCoder) != 0; + bool isCoder2 = (*iid == IID_ICompressCoder2) != 0; + bool isFilter = (*iid == IID_ICompressFilter) != 0; + if (!isCoder && !isCoder2 && !isFilter) + return E_NOINTERFACE; + bool encode; + int codecIndex; + HRESULT res = FindCodecClassId(clsid, isCoder2, isFilter, encode, codecIndex); + if (res != S_OK) + return res; + if (codecIndex < 0) + return CLASS_E_CLASSNOTAVAILABLE; + return CreateCoder2(encode, codecIndex, iid, outObject); +} + +STDAPI GetMethodProperty(UInt32 codecIndex, PROPID propID, PROPVARIANT *value) +{ + ::VariantClear((VARIANTARG *)value); + const CCodecInfo &codec = *g_Codecs[codecIndex]; + switch(propID) + { + case NMethodPropID::kID: + { + value->uhVal.QuadPart = (UInt64)codec.Id; + value->vt = VT_UI8; + break; + } + case NMethodPropID::kName: + if ((value->bstrVal = ::SysAllocString(codec.Name)) != 0) + value->vt = VT_BSTR; + break; + case NMethodPropID::kDecoder: + if (codec.CreateDecoder) + return SetClassID(codec.Id, false, value); + break; + case NMethodPropID::kEncoder: + if (codec.CreateEncoder) + return SetClassID(codec.Id, true, value); + break; + case NMethodPropID::kInStreams: + { + if (codec.NumInStreams != 1) + { + value->vt = VT_UI4; + value->ulVal = (ULONG)codec.NumInStreams; + } + break; + } + } + return S_OK; +} + +STDAPI GetNumberOfMethods(UINT32 *numCodecs) +{ + *numCodecs = g_NumCodecs; + return S_OK; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/CopyCoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/CopyCoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,62 @@ +// Compress/CopyCoder.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/Alloc.h" +} + +#include "../Common/StreamUtils.h" + +#include "CopyCoder.h" + +namespace NCompress { + +static const UInt32 kBufferSize = 1 << 17; + +CCopyCoder::~CCopyCoder() +{ + ::MidFree(_buffer); +} + +STDMETHODIMP CCopyCoder::Code(ISequentialInStream *inStream, + ISequentialOutStream *outStream, + const UInt64 * /* inSize */, const UInt64 *outSize, + ICompressProgressInfo *progress) +{ + if (_buffer == 0) + { + _buffer = (Byte *)::MidAlloc(kBufferSize); + if (_buffer == 0) + return E_OUTOFMEMORY; + } + + TotalSize = 0; + for (;;) + { + UInt32 realProcessedSize; + UInt32 size = kBufferSize; + if (outSize != 0) + if (size > *outSize - TotalSize) + size = (UInt32)(*outSize - TotalSize); + RINOK(inStream->Read(_buffer, size, &realProcessedSize)); + if (realProcessedSize == 0) + break; + RINOK(WriteStream(outStream, _buffer, realProcessedSize)); + TotalSize += realProcessedSize; + if (progress != NULL) + { + RINOK(progress->SetRatioInfo(&TotalSize, &TotalSize)); + } + } + return S_OK; +} + +STDMETHODIMP CCopyCoder::GetInStreamProcessedSize(UInt64 *value) +{ + *value = TotalSize; + return S_OK; +} + +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/CopyCoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/CopyCoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,32 @@ +// Compress/CopyCoder.h + +#ifndef __COMPRESS_COPY_CODER_H +#define __COMPRESS_COPY_CODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +namespace NCompress { + +class CCopyCoder: + public ICompressCoder, + public ICompressGetInStreamProcessedSize, + public CMyUnknownImp +{ + Byte *_buffer; +public: + UInt64 TotalSize; + CCopyCoder(): TotalSize(0) , _buffer(0) {}; + ~CCopyCoder(); + + MY_UNKNOWN_IMP1(ICompressGetInStreamProcessedSize) + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + STDMETHOD(GetInStreamProcessedSize)(UInt64 *value); +}; + +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/CopyRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/CopyRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +// CopyRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "CopyCoder.h" + +static void *CreateCodec() { return (void *)(ICompressCoder *)(new NCompress::CCopyCoder); } + +static CCodecInfo g_CodecInfo = +{ CreateCodec, CreateCodec, 0x00, L"Copy", 1, false }; + +REGISTER_CODEC(Copy) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Deflate64Register.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Deflate64Register.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// Deflate64Register.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "DeflateDecoder.h" + +static void *CreateCodecDeflate64() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NDecoder::CCOMCoder64); } +#if !defined(EXTRACT_ONLY) && !defined(DEFLATE_EXTRACT_ONLY) +#include "DeflateEncoder.h" +static void *CreateCodecOutDeflate64() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NEncoder::CCOMCoder64); } +#else +#define CreateCodecOutDeflate64 0 +#endif + +static CCodecInfo g_CodecInfo = + { CreateCodecDeflate64, CreateCodecOutDeflate64, 0x040109, L"Deflate64", 1, false }; + +REGISTER_CODEC(Deflate64) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/DeflateConst.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/DeflateConst.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,134 @@ +// DeflateConst.h + +#ifndef __DEFLATE_CONST_H +#define __DEFLATE_CONST_H + +namespace NCompress { +namespace NDeflate { + +const int kNumHuffmanBits = 15; + +const UInt32 kHistorySize32 = (1 << 15); +const UInt32 kHistorySize64 = (1 << 16); + +const UInt32 kDistTableSize32 = 30; +const UInt32 kDistTableSize64 = 32; + +const UInt32 kNumLenSymbols32 = 256; +const UInt32 kNumLenSymbols64 = 255; // don't change it. It must be <= 255. +const UInt32 kNumLenSymbolsMax = kNumLenSymbols32; + +const UInt32 kNumLenSlots = 29; + +const UInt32 kFixedDistTableSize = 32; +const UInt32 kFixedLenTableSize = 31; + +const UInt32 kSymbolEndOfBlock = 0x100; +const UInt32 kSymbolMatch = kSymbolEndOfBlock + 1; + +const UInt32 kMainTableSize = kSymbolMatch + kNumLenSlots; +const UInt32 kFixedMainTableSize = kSymbolMatch + kFixedLenTableSize; + +const UInt32 kLevelTableSize = 19; + +const UInt32 kTableDirectLevels = 16; +const UInt32 kTableLevelRepNumber = kTableDirectLevels; +const UInt32 kTableLevel0Number = kTableLevelRepNumber + 1; +const UInt32 kTableLevel0Number2 = kTableLevel0Number + 1; + +const UInt32 kLevelMask = 0xF; + +const Byte kLenStart32[kFixedLenTableSize] = + {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224, 255, 0, 0}; +const Byte kLenStart64[kFixedLenTableSize] = + {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224, 0, 0, 0}; + +const Byte kLenDirectBits32[kFixedLenTableSize] = + {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0}; +const Byte kLenDirectBits64[kFixedLenTableSize] = + {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 16, 0, 0}; + +const UInt32 kDistStart[kDistTableSize64] = + {0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768, + 1024,1536,2048,3072,4096,6144,8192,12288,16384,24576,32768,49152}; +const Byte kDistDirectBits[kDistTableSize64] = + {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14}; + +const Byte kLevelDirectBits[3] = {2, 3, 7}; + +const Byte kCodeLengthAlphabetOrder[kLevelTableSize] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + +const UInt32 kMatchMinLen = 3; +const UInt32 kMatchMaxLen32 = kNumLenSymbols32 + kMatchMinLen - 1; //256 + 2 +const UInt32 kMatchMaxLen64 = kNumLenSymbols64 + kMatchMinLen - 1; //255 + 2 +const UInt32 kMatchMaxLen = kMatchMaxLen32; + +const int kFinalBlockFieldSize = 1; + +namespace NFinalBlockField +{ + enum + { + kNotFinalBlock = 0, + kFinalBlock = 1 + }; +} + +const int kBlockTypeFieldSize = 2; + +namespace NBlockType +{ + enum + { + kStored = 0, + kFixedHuffman = 1, + kDynamicHuffman = 2 + }; +} + +const int kNumLenCodesFieldSize = 5; +const int kNumDistCodesFieldSize = 5; +const int kNumLevelCodesFieldSize = 4; + +const UInt32 kNumLitLenCodesMin = 257; +const UInt32 kNumDistCodesMin = 1; +const UInt32 kNumLevelCodesMin = 4; + +const int kLevelFieldSize = 3; + +const int kStoredBlockLengthFieldSize = 16; + +struct CLevels +{ + Byte litLenLevels[kFixedMainTableSize]; + Byte distLevels[kFixedDistTableSize]; + + void SubClear() + { + UInt32 i; + for(i = kNumLitLenCodesMin; i < kFixedMainTableSize; i++) + litLenLevels[i] = 0; + for(i = 0; i < kFixedDistTableSize; i++) + distLevels[i] = 0; + } + + void SetFixedLevels() + { + int i; + + for (i = 0; i < 144; i++) + litLenLevels[i] = 8; + for (; i < 256; i++) + litLenLevels[i] = 9; + for (; i < 280; i++) + litLenLevels[i] = 7; + for (; i < 288; i++) + litLenLevels[i] = 8; + for (i = 0; i < kFixedDistTableSize; i++) // test it: InfoZip only uses kDistTableSize + distLevels[i] = 5; + } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/DeflateDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/DeflateDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,345 @@ +// DeflateDecoder.cpp + +#include "StdAfx.h" + +#include "DeflateDecoder.h" + +namespace NCompress { +namespace NDeflate { +namespace NDecoder { + +static const int kLenIdFinished = -1; +static const int kLenIdNeedInit = -2; + +CCoder::CCoder(bool deflate64Mode, bool deflateNSIS): + _deflate64Mode(deflate64Mode), + _deflateNSIS(deflateNSIS), + _keepHistory(false), + ZlibMode(false) {} + +UInt32 CCoder::ReadBits(int numBits) +{ + return m_InBitStream.ReadBits(numBits); +} + +bool CCoder::DeCodeLevelTable(Byte *values, int numSymbols) +{ + int i = 0; + do + { + UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream); + if (number < kTableDirectLevels) + values[i++] = (Byte)number; + else if (number < kLevelTableSize) + { + if (number == kTableLevelRepNumber) + { + if (i == 0) + return false; + int num = ReadBits(2) + 3; + for (; num > 0 && i < numSymbols; num--, i++) + values[i] = values[i - 1]; + } + else + { + int num; + if (number == kTableLevel0Number) + num = ReadBits(3) + 3; + else + num = ReadBits(7) + 11; + for (;num > 0 && i < numSymbols; num--) + values[i++] = 0; + } + } + else + return false; + } + while(i < numSymbols); + return true; +} + +#define RIF(x) { if (!(x)) return false; } + +bool CCoder::ReadTables(void) +{ + m_FinalBlock = (ReadBits(kFinalBlockFieldSize) == NFinalBlockField::kFinalBlock); + UInt32 blockType = ReadBits(kBlockTypeFieldSize); + if (blockType > NBlockType::kDynamicHuffman) + return false; + + if (blockType == NBlockType::kStored) + { + m_StoredMode = true; + UInt32 currentBitPosition = m_InBitStream.GetBitPosition(); + int numBitsForAlign = (int)(currentBitPosition > 0 ? (8 - currentBitPosition): 0); + ReadBits(numBitsForAlign); + m_StoredBlockSize = ReadBits(kStoredBlockLengthFieldSize); + if (_deflateNSIS) + return true; + return (m_StoredBlockSize == (UInt16)~ReadBits(kStoredBlockLengthFieldSize)); + } + + m_StoredMode = false; + + CLevels levels; + if (blockType == NBlockType::kFixedHuffman) + { + levels.SetFixedLevels(); + _numDistLevels = _deflate64Mode ? kDistTableSize64 : kDistTableSize32; + } + else + { + int numLitLenLevels = ReadBits(kNumLenCodesFieldSize) + kNumLitLenCodesMin; + _numDistLevels = ReadBits(kNumDistCodesFieldSize) + kNumDistCodesMin; + int numLevelCodes = ReadBits(kNumLevelCodesFieldSize) + kNumLevelCodesMin; + + if (!_deflate64Mode) + if (_numDistLevels > kDistTableSize32) + return false; + + Byte levelLevels[kLevelTableSize]; + for (int i = 0; i < kLevelTableSize; i++) + { + int position = kCodeLengthAlphabetOrder[i]; + if(i < numLevelCodes) + levelLevels[position] = (Byte)ReadBits(kLevelFieldSize); + else + levelLevels[position] = 0; + } + + RIF(m_LevelDecoder.SetCodeLengths(levelLevels)); + + Byte tmpLevels[kFixedMainTableSize + kFixedDistTableSize]; + if (!DeCodeLevelTable(tmpLevels, numLitLenLevels + _numDistLevels)) + return false; + + levels.SubClear(); + memcpy(levels.litLenLevels, tmpLevels, numLitLenLevels); + memcpy(levels.distLevels, tmpLevels + numLitLenLevels, _numDistLevels); + } + RIF(m_MainDecoder.SetCodeLengths(levels.litLenLevels)); + return m_DistDecoder.SetCodeLengths(levels.distLevels); +} + +HRESULT CCoder::CodeSpec(UInt32 curSize) +{ + if (_remainLen == kLenIdFinished) + return S_OK; + if (_remainLen == kLenIdNeedInit) + { + if (!_keepHistory) + if (!m_OutWindowStream.Create(_deflate64Mode ? kHistorySize64: kHistorySize32)) + return E_OUTOFMEMORY; + if (!m_InBitStream.Create(1 << 17)) + return E_OUTOFMEMORY; + m_OutWindowStream.Init(_keepHistory); + m_InBitStream.Init(); + m_FinalBlock = false; + _remainLen = 0; + _needReadTable = true; + } + + if (curSize == 0) + return S_OK; + + while(_remainLen > 0 && curSize > 0) + { + _remainLen--; + Byte b = m_OutWindowStream.GetByte(_rep0); + m_OutWindowStream.PutByte(b); + curSize--; + } + + while(curSize > 0) + { + if (_needReadTable) + { + if (m_FinalBlock) + { + _remainLen = kLenIdFinished; + break; + } + if (!ReadTables()) + return S_FALSE; + _needReadTable = false; + } + + if(m_StoredMode) + { + for (; m_StoredBlockSize > 0 && curSize > 0; m_StoredBlockSize--, curSize--) + m_OutWindowStream.PutByte((Byte)m_InBitStream.ReadBits(8)); + _needReadTable = (m_StoredBlockSize == 0); + continue; + } + while(curSize > 0) + { + if (m_InBitStream.NumExtraBytes > 4) + return S_FALSE; + + UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream); + if (number < 0x100) + { + m_OutWindowStream.PutByte((Byte)number); + curSize--; + continue; + } + else if (number == kSymbolEndOfBlock) + { + _needReadTable = true; + break; + } + else if (number < kMainTableSize) + { + number -= kSymbolMatch; + UInt32 len; + { + int numBits; + if (_deflate64Mode) + { + len = kLenStart64[number]; + numBits = kLenDirectBits64[number]; + } + else + { + len = kLenStart32[number]; + numBits = kLenDirectBits32[number]; + } + len += kMatchMinLen + m_InBitStream.ReadBits(numBits); + } + UInt32 locLen = len; + if (locLen > curSize) + locLen = (UInt32)curSize; + number = m_DistDecoder.DecodeSymbol(&m_InBitStream); + if (number >= _numDistLevels) + return S_FALSE; + UInt32 distance = kDistStart[number] + m_InBitStream.ReadBits(kDistDirectBits[number]); + if (!m_OutWindowStream.CopyBlock(distance, locLen)) + return S_FALSE; + curSize -= locLen; + len -= locLen; + if (len != 0) + { + _remainLen = (Int32)len; + _rep0 = distance; + break; + } + } + else + return S_FALSE; + } + } + return S_OK; +} + +HRESULT CCoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + SetInStream(inStream); + m_OutWindowStream.SetStream(outStream); + SetOutStreamSize(outSize); + CCoderReleaser flusher(this); + + const UInt64 start = m_OutWindowStream.GetProcessedSize(); + for (;;) + { + UInt32 curSize = 1 << 18; + if (outSize != 0) + { + const UInt64 rem = *outSize - (m_OutWindowStream.GetProcessedSize() - start); + if (curSize > rem) + curSize = (UInt32)rem; + } + if (curSize == 0) + break; + RINOK(CodeSpec(curSize)); + if (_remainLen == kLenIdFinished) + break; + if (progress != NULL) + { + const UInt64 inSize = m_InBitStream.GetProcessedSize(); + const UInt64 nowPos64 = m_OutWindowStream.GetProcessedSize() - start; + RINOK(progress->SetRatioInfo(&inSize, &nowPos64)); + } + } + if (_remainLen == kLenIdFinished && ZlibMode) + { + UInt32 currentBitPosition = m_InBitStream.GetBitPosition(); + int numBitsForAlign = (int)(currentBitPosition > 0 ? (8 - currentBitPosition): 0); + ReadBits(numBitsForAlign); + for (int i = 0; i < 4; i++) + ZlibFooter[i] = (Byte)m_InBitStream.ReadBits(8); + } + flusher.NeedFlush = false; + return Flush(); +} + + +#ifdef _NO_EXCEPTIONS + +#define DEFLATE_TRY_BEGIN +#define DEFLATE_TRY_END + +#else + +#define DEFLATE_TRY_BEGIN try { +#define DEFLATE_TRY_END } \ + catch(const CInBufferException &e) { return e.ErrorCode; } \ + catch(const CLzOutWindowException &e) { return e.ErrorCode; } \ + catch(...) { return S_FALSE; } + +#endif + +HRESULT CCoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + DEFLATE_TRY_BEGIN + return CodeReal(inStream, outStream, inSize, outSize, progress); + DEFLATE_TRY_END +} + +STDMETHODIMP CCoder::GetInStreamProcessedSize(UInt64 *value) +{ + if (value == NULL) + return E_INVALIDARG; + *value = m_InBitStream.GetProcessedSize(); + return S_OK; +} + +STDMETHODIMP CCoder::SetInStream(ISequentialInStream *inStream) +{ + m_InBitStream.SetStream(inStream); + return S_OK; +} + +STDMETHODIMP CCoder::ReleaseInStream() +{ + m_InBitStream.ReleaseStream(); + return S_OK; +} + +STDMETHODIMP CCoder::SetOutStreamSize(const UInt64 * /* outSize */) +{ + _remainLen = kLenIdNeedInit; + m_OutWindowStream.Init(_keepHistory); + return S_OK; +} + +#ifndef NO_READ_FROM_CODER + +STDMETHODIMP CCoder::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + DEFLATE_TRY_BEGIN + if (processedSize) + *processedSize = 0; + const UInt64 startPos = m_OutWindowStream.GetProcessedSize(); + m_OutWindowStream.SetMemStream((Byte *)data); + RINOK(CodeSpec(size)); + if (processedSize) + *processedSize = (UInt32)(m_OutWindowStream.GetProcessedSize() - startPos); + return Flush(); + DEFLATE_TRY_END +} + +#endif + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/DeflateDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/DeflateDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,136 @@ +// DeflateDecoder.h + +#ifndef __DEFLATE_DECODER_H +#define __DEFLATE_DECODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "../Common/InBuffer.h" + +#include "BitlDecoder.h" +#include "DeflateConst.h" +#include "HuffmanDecoder.h" +#include "LzOutWindow.h" + +namespace NCompress { +namespace NDeflate { +namespace NDecoder { + +class CCoder: + public ICompressCoder, + public ICompressGetInStreamProcessedSize, + #ifndef NO_READ_FROM_CODER + public ICompressSetInStream, + public ICompressSetOutStreamSize, + public ISequentialInStream, + #endif + public CMyUnknownImp +{ + CLzOutWindow m_OutWindowStream; + NBitl::CDecoder m_InBitStream; + NCompress::NHuffman::CDecoder m_MainDecoder; + NCompress::NHuffman::CDecoder m_DistDecoder; + NCompress::NHuffman::CDecoder m_LevelDecoder; + + UInt32 m_StoredBlockSize; + + bool m_FinalBlock; + bool m_StoredMode; + UInt32 _numDistLevels; + + + bool _deflateNSIS; + bool _deflate64Mode; + bool _keepHistory; + Int32 _remainLen; + UInt32 _rep0; + bool _needReadTable; + + UInt32 ReadBits(int numBits); + + bool DeCodeLevelTable(Byte *values, int numSymbols); + bool ReadTables(); + + void ReleaseStreams() + { + m_OutWindowStream.ReleaseStream(); + ReleaseInStream(); + } + + HRESULT Flush() { return m_OutWindowStream.Flush(); } + class CCoderReleaser + { + CCoder *m_Coder; + public: + bool NeedFlush; + CCoderReleaser(CCoder *coder): m_Coder(coder), NeedFlush(true) {} + ~CCoderReleaser() + { + if (NeedFlush) + m_Coder->Flush(); + m_Coder->ReleaseStreams(); + } + }; + friend class CCoderReleaser; + + HRESULT CodeSpec(UInt32 curSize); +public: + bool ZlibMode; + Byte ZlibFooter[4]; + + CCoder(bool deflate64Mode, bool deflateNSIS = false); + void SetKeepHistory(bool keepHistory) { _keepHistory = keepHistory; } + + HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + #ifndef NO_READ_FROM_CODER + MY_UNKNOWN_IMP4( + ICompressGetInStreamProcessedSize, + ICompressSetInStream, + ICompressSetOutStreamSize, + ISequentialInStream + ) + #else + MY_UNKNOWN_IMP1( + ICompressGetInStreamProcessedSize) + #endif + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(SetInStream)(ISequentialInStream *inStream); + STDMETHOD(ReleaseInStream)(); + STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); + + #ifndef NO_READ_FROM_CODER + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + #endif + + // IGetInStreamProcessedSize + STDMETHOD(GetInStreamProcessedSize)(UInt64 *value); +}; + +class CCOMCoder : public CCoder +{ +public: + CCOMCoder(): CCoder(false) {} +}; + +class CNsisCOMCoder : public CCoder +{ +public: + CNsisCOMCoder(): CCoder(false, true) {} +}; + +class CCOMCoder64 : public CCoder +{ +public: + CCOMCoder64(): CCoder(true) {} +}; + +}}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/DeflateNsisRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/DeflateNsisRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +// DeflateNsisRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "DeflateDecoder.h" + +static void *CreateCodecDeflateNsis() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NDecoder::CNsisCOMCoder); } + +static CCodecInfo g_CodecInfo = + { CreateCodecDeflateNsis, 0, 0x040901, L"DeflateNSIS", 1, false }; + +REGISTER_CODEC(DeflateNsis) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/DeflateRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/DeflateRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,21 @@ +// DeflateRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "DeflateDecoder.h" + +static void *CreateCodecDeflate() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NDecoder::CCOMCoder); } + +#if !defined(EXTRACT_ONLY) && !defined(DEFLATE_EXTRACT_ONLY) +#include "DeflateEncoder.h" +static void *CreateCodecOutDeflate() { return (void *)(ICompressCoder *)(new NCompress::NDeflate::NEncoder::CCOMCoder); } +#else +#define CreateCodecOutDeflate 0 +#endif + +static CCodecInfo g_CodecInfo = + { CreateCodecDeflate, CreateCodecOutDeflate, 0x040108, L"Deflate", 1, false }; + +REGISTER_CODEC(Deflate) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/HuffmanDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/HuffmanDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,89 @@ +// Compress/HuffmanDecoder.h + +#ifndef __COMPRESS_HUFFMAN_DECODER_H +#define __COMPRESS_HUFFMAN_DECODER_H + +#include "../../Common/Types.h" + +namespace NCompress { +namespace NHuffman { + +const int kNumTableBits = 9; + +template +class CDecoder +{ + UInt32 m_Limits[kNumBitsMax + 1]; // m_Limits[i] = value limit for symbols with length = i + UInt32 m_Positions[kNumBitsMax + 1]; // m_Positions[i] = index in m_Symbols[] of first symbol with length = i + UInt32 m_Symbols[m_NumSymbols]; + Byte m_Lengths[1 << kNumTableBits]; // Table oh length for short codes. + +public: + + bool SetCodeLengths(const Byte *codeLengths) + { + int lenCounts[kNumBitsMax + 1]; + UInt32 tmpPositions[kNumBitsMax + 1]; + int i; + for(i = 1; i <= kNumBitsMax; i++) + lenCounts[i] = 0; + UInt32 symbol; + for (symbol = 0; symbol < m_NumSymbols; symbol++) + { + int len = codeLengths[symbol]; + if (len > kNumBitsMax) + return false; + lenCounts[len]++; + m_Symbols[symbol] = 0xFFFFFFFF; + } + lenCounts[0] = 0; + m_Positions[0] = m_Limits[0] = 0; + UInt32 startPos = 0; + UInt32 index = 0; + const UInt32 kMaxValue = (1 << kNumBitsMax); + for (i = 1; i <= kNumBitsMax; i++) + { + startPos += lenCounts[i] << (kNumBitsMax - i); + if (startPos > kMaxValue) + return false; + m_Limits[i] = (i == kNumBitsMax) ? kMaxValue : startPos; + m_Positions[i] = m_Positions[i - 1] + lenCounts[i - 1]; + tmpPositions[i] = m_Positions[i]; + if(i <= kNumTableBits) + { + UInt32 limit = (m_Limits[i] >> (kNumBitsMax - kNumTableBits)); + for (; index < limit; index++) + m_Lengths[index] = (Byte)i; + } + } + for (symbol = 0; symbol < m_NumSymbols; symbol++) + { + int len = codeLengths[symbol]; + if (len != 0) + m_Symbols[tmpPositions[len]++] = symbol; + } + return true; + } + + template + UInt32 DecodeSymbol(TBitDecoder *bitStream) + { + int numBits; + UInt32 value = bitStream->GetValue(kNumBitsMax); + if (value < m_Limits[kNumTableBits]) + numBits = m_Lengths[value >> (kNumBitsMax - kNumTableBits)]; + else + for (numBits = kNumTableBits + 1; value >= m_Limits[numBits]; numBits++); + bitStream->MovePos(numBits); + UInt32 index = m_Positions[numBits] + + ((value - m_Limits[numBits - 1]) >> (kNumBitsMax - numBits)); + if (index >= m_NumSymbols) + // throw CDecoderException(); // test it + return 0xFFFFFFFF; + return m_Symbols[index]; + } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ImplodeDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ImplodeDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,219 @@ +// Implode/Decoder.cpp + +#include "StdAfx.h" + +#include "ImplodeDecoder.h" +#include "Common/Defs.h" + +namespace NCompress { +namespace NImplode { +namespace NDecoder { + +class CException +{ +public: + enum ECauseType + { + kData + } m_Cause; + CException(ECauseType cause): m_Cause(cause) {} +}; + +static const int kNumDistanceLowDirectBitsForBigDict = 7; +static const int kNumDistanceLowDirectBitsForSmallDict = 6; + +static const int kNumBitsInByte = 8; + +// static const int kLevelStructuresNumberFieldSize = kNumBitsInByte; +static const int kLevelStructuresNumberAdditionalValue = 1; + +static const int kNumLevelStructureLevelBits = 4; +static const int kLevelStructureLevelAdditionalValue = 1; + +static const int kNumLevelStructureRepNumberBits = 4; +static const int kLevelStructureRepNumberAdditionalValue = 1; + + +static const int kLiteralTableSize = (1 << kNumBitsInByte); +static const int kDistanceTableSize = 64; +static const int kLengthTableSize = 64; + +static const UInt32 kHistorySize = + (1 << MyMax(kNumDistanceLowDirectBitsForBigDict, + kNumDistanceLowDirectBitsForSmallDict)) * + kDistanceTableSize; // = 8 KB; + +static const int kNumAdditionalLengthBits = 8; + +static const UInt32 kMatchMinLenWhenLiteralsOn = 3; +static const UInt32 kMatchMinLenWhenLiteralsOff = 2; + +static const UInt32 kMatchMinLenMax = MyMax(kMatchMinLenWhenLiteralsOn, + kMatchMinLenWhenLiteralsOff); // 3 + +// static const UInt32 kMatchMaxLenMax = kMatchMinLenMax + (kLengthTableSize - 1) + (1 << kNumAdditionalLengthBits) - 1; // or 2 + +enum +{ + kMatchId = 0, + kLiteralId = 1 +}; + + +CCoder::CCoder(): + m_LiteralDecoder(kLiteralTableSize), + m_LengthDecoder(kLengthTableSize), + m_DistanceDecoder(kDistanceTableSize) +{ +} + +void CCoder::ReleaseStreams() +{ + m_OutWindowStream.ReleaseStream(); + m_InBitStream.ReleaseStream(); +} + +bool CCoder::ReadLevelItems(NImplode::NHuffman::CDecoder &decoder, + Byte *levels, int numLevelItems) +{ + int numCodedStructures = m_InBitStream.ReadBits(kNumBitsInByte) + + kLevelStructuresNumberAdditionalValue; + int currentIndex = 0; + for(int i = 0; i < numCodedStructures; i++) + { + int level = m_InBitStream.ReadBits(kNumLevelStructureLevelBits) + + kLevelStructureLevelAdditionalValue; + int rep = m_InBitStream.ReadBits(kNumLevelStructureRepNumberBits) + + kLevelStructureRepNumberAdditionalValue; + if (currentIndex + rep > numLevelItems) + throw CException(CException::kData); + for(int j = 0; j < rep; j++) + levels[currentIndex++] = (Byte)level; + } + if (currentIndex != numLevelItems) + return false; + return decoder.SetCodeLengths(levels); +} + + +bool CCoder::ReadTables(void) +{ + if (m_LiteralsOn) + { + Byte literalLevels[kLiteralTableSize]; + if (!ReadLevelItems(m_LiteralDecoder, literalLevels, kLiteralTableSize)) + return false; + } + + Byte lengthLevels[kLengthTableSize]; + if (!ReadLevelItems(m_LengthDecoder, lengthLevels, kLengthTableSize)) + return false; + + Byte distanceLevels[kDistanceTableSize]; + return ReadLevelItems(m_DistanceDecoder, distanceLevels, kDistanceTableSize); +} + +class CCoderReleaser +{ + CCoder *m_Coder; +public: + CCoderReleaser(CCoder *coder): m_Coder(coder) {} + ~CCoderReleaser() { m_Coder->ReleaseStreams(); } +}; + +HRESULT CCoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + if (!m_InBitStream.Create(1 << 20)) + return E_OUTOFMEMORY; + if (!m_OutWindowStream.Create(kHistorySize)) + return E_OUTOFMEMORY; + if (outSize == NULL) + return E_INVALIDARG; + UInt64 pos = 0, unPackSize = *outSize; + + m_OutWindowStream.SetStream(outStream); + m_OutWindowStream.Init(false); + m_InBitStream.SetStream(inStream); + m_InBitStream.Init(); + CCoderReleaser coderReleaser(this); + + if (!ReadTables()) + return S_FALSE; + + while(pos < unPackSize) + { + if (progress != NULL && pos % (1 << 16) == 0) + { + UInt64 packSize = m_InBitStream.GetProcessedSize(); + RINOK(progress->SetRatioInfo(&packSize, &pos)); + } + if(m_InBitStream.ReadBits(1) == kMatchId) // match + { + UInt32 lowDistBits = m_InBitStream.ReadBits(m_NumDistanceLowDirectBits); + UInt32 distance = m_DistanceDecoder.DecodeSymbol(&m_InBitStream); + if (distance >= kDistanceTableSize) + return S_FALSE; + distance = (distance << m_NumDistanceLowDirectBits) + lowDistBits; + UInt32 lengthSymbol = m_LengthDecoder.DecodeSymbol(&m_InBitStream); + if (lengthSymbol >= kLengthTableSize) + return S_FALSE; + UInt32 length = lengthSymbol + m_MinMatchLength; + if (lengthSymbol == kLengthTableSize - 1) // special symbol = 63 + length += m_InBitStream.ReadBits(kNumAdditionalLengthBits); + while(distance >= pos && length > 0) + { + m_OutWindowStream.PutByte(0); + pos++; + length--; + } + if (length > 0) + m_OutWindowStream.CopyBlock(distance, length); + pos += length; + } + else + { + Byte b; + if (m_LiteralsOn) + { + UInt32 temp = m_LiteralDecoder.DecodeSymbol(&m_InBitStream); + if (temp >= kLiteralTableSize) + return S_FALSE; + b = (Byte)temp; + } + else + b = (Byte)m_InBitStream.ReadBits(kNumBitsInByte); + m_OutWindowStream.PutByte(b); + pos++; + } + } + if (pos > unPackSize) + return S_FALSE; + return m_OutWindowStream.Flush(); +} + +STDMETHODIMP CCoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + try { return CodeReal(inStream, outStream, inSize, outSize, progress); } + catch(const CLzOutWindowException &e) { return e.ErrorCode; } + catch(...) { return S_FALSE; } +} + +STDMETHODIMP CCoder::SetDecoderProperties2(const Byte *data, UInt32 size) +{ + if (size < 1) + return E_INVALIDARG; + Byte flag = data[0]; + m_BigDictionaryOn = ((flag & 2) != 0); + m_NumDistanceLowDirectBits = m_BigDictionaryOn ? + kNumDistanceLowDirectBitsForBigDict: + kNumDistanceLowDirectBitsForSmallDict; + m_LiteralsOn = ((flag & 4) != 0); + m_MinMatchLength = m_LiteralsOn ? + kMatchMinLenWhenLiteralsOn : + kMatchMinLenWhenLiteralsOff; + return S_OK; +} + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ImplodeDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ImplodeDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,57 @@ +// ImplodeDecoder.h + +#ifndef __COMPRESS_IMPLODE_DECODER_H +#define __COMPRESS_IMPLODE_DECODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "ImplodeHuffmanDecoder.h" +#include "LzOutWindow.h" + +namespace NCompress { +namespace NImplode { +namespace NDecoder { + +class CCoder: + public ICompressCoder, + public ICompressSetDecoderProperties2, + public CMyUnknownImp +{ + CLzOutWindow m_OutWindowStream; + NBitl::CDecoder m_InBitStream; + + NImplode::NHuffman::CDecoder m_LiteralDecoder; + NImplode::NHuffman::CDecoder m_LengthDecoder; + NImplode::NHuffman::CDecoder m_DistanceDecoder; + + bool m_BigDictionaryOn; + bool m_LiteralsOn; + + int m_NumDistanceLowDirectBits; + UInt32 m_MinMatchLength; + + bool ReadLevelItems(NImplode::NHuffman::CDecoder &table, Byte *levels, int numLevelItems); + bool ReadTables(); + void DeCodeLevelTable(Byte *newLevels, int numLevels); +public: + CCoder(); + + MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) + + void ReleaseStreams(); + HRESULT Flush() { return m_OutWindowStream.Flush(); } + + HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); +}; + +}}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ImplodeHuffmanDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ImplodeHuffmanDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,89 @@ +// ImplodeHuffmanDecoder.cpp + +#include "StdAfx.h" + +#include "ImplodeHuffmanDecoder.h" + +namespace NCompress { +namespace NImplode { +namespace NHuffman { + +CDecoder::CDecoder(UInt32 numSymbols): + m_NumSymbols(numSymbols) +{ + m_Symbols = new UInt32[m_NumSymbols]; +} + +CDecoder::~CDecoder() +{ + delete []m_Symbols; +} + +bool CDecoder::SetCodeLengths(const Byte *codeLengths) +{ + // int lenCounts[kNumBitsInLongestCode + 1], tmpPositions[kNumBitsInLongestCode + 1]; + int lenCounts[kNumBitsInLongestCode + 2], tmpPositions[kNumBitsInLongestCode + 1]; + int i; + for(i = 0; i <= kNumBitsInLongestCode; i++) + lenCounts[i] = 0; + UInt32 symbolIndex; + for (symbolIndex = 0; symbolIndex < m_NumSymbols; symbolIndex++) + lenCounts[codeLengths[symbolIndex]]++; + // lenCounts[0] = 0; + + // tmpPositions[0] = m_Positions[0] = m_Limitits[0] = 0; + m_Limitits[kNumBitsInLongestCode + 1] = 0; + m_Positions[kNumBitsInLongestCode + 1] = 0; + lenCounts[kNumBitsInLongestCode + 1] = 0; + + + UInt32 startPos = 0; + static const UInt32 kMaxValue = (1 << kNumBitsInLongestCode); + + for (i = kNumBitsInLongestCode; i > 0; i--) + { + startPos += lenCounts[i] << (kNumBitsInLongestCode - i); + if (startPos > kMaxValue) + return false; + m_Limitits[i] = startPos; + m_Positions[i] = m_Positions[i + 1] + lenCounts[i + 1]; + tmpPositions[i] = m_Positions[i] + lenCounts[i]; + + } + + // if _ZIP_MODE do not throw exception for trees containing only one node + // #ifndef _ZIP_MODE + if (startPos != kMaxValue) + return false; + // #endif + + for (symbolIndex = 0; symbolIndex < m_NumSymbols; symbolIndex++) + if (codeLengths[symbolIndex] != 0) + m_Symbols[--tmpPositions[codeLengths[symbolIndex]]] = symbolIndex; + return true; +} + +UInt32 CDecoder::DecodeSymbol(CInBit *inStream) +{ + UInt32 numBits = 0; + UInt32 value = inStream->GetValue(kNumBitsInLongestCode); + int i; + for(i = kNumBitsInLongestCode; i > 0; i--) + { + if (value < m_Limitits[i]) + { + numBits = i; + break; + } + } + if (i == 0) + return 0xFFFFFFFF; + inStream->MovePos(numBits); + UInt32 index = m_Positions[numBits] + + ((value - m_Limitits[numBits + 1]) >> (kNumBitsInLongestCode - numBits)); + if (index >= m_NumSymbols) + return 0xFFFFFFFF; + return m_Symbols[index]; +} + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ImplodeHuffmanDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ImplodeHuffmanDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,34 @@ +// ImplodeHuffmanDecoder.h + +#ifndef __IMPLODE_HUFFMAN_DECODER_H +#define __IMPLODE_HUFFMAN_DECODER_H + +#include "../Common/InBuffer.h" + +#include "BitlDecoder.h" + +namespace NCompress { +namespace NImplode { +namespace NHuffman { + +const int kNumBitsInLongestCode = 16; + +typedef NBitl::CDecoder CInBit; + +class CDecoder +{ + UInt32 m_Limitits[kNumBitsInLongestCode + 2]; // m_Limitits[i] = value limit for symbols with length = i + UInt32 m_Positions[kNumBitsInLongestCode + 2]; // m_Positions[i] = index in m_Symbols[] of first symbol with length = i + UInt32 m_NumSymbols; // number of symbols in m_Symbols + UInt32 *m_Symbols; // symbols: at first with len=1 then 2, ... 15. +public: + CDecoder(UInt32 numSymbols); + ~CDecoder(); + + bool SetCodeLengths(const Byte *codeLengths); + UInt32 DecodeSymbol(CInBit *inStream); +}; + +}}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/LzOutWindow.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/LzOutWindow.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +// LzOutWindow.cpp + +#include "StdAfx.h" + +#include "LzOutWindow.h" + +void CLzOutWindow::Init(bool solid) +{ + if (!solid) + COutBuffer::Init(); + #ifdef _NO_EXCEPTIONS + ErrorCode = S_OK; + #endif +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/LzOutWindow.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/LzOutWindow.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,66 @@ +// LzOutWindow.h + +#ifndef __LZ_OUT_WINDOW_H +#define __LZ_OUT_WINDOW_H + +#include "../IStream.h" + +#include "../Common/OutBuffer.h" + +#ifndef _NO_EXCEPTIONS +typedef COutBufferException CLzOutWindowException; +#endif + +class CLzOutWindow: public COutBuffer +{ +public: + void Init(bool solid = false); + + // distance >= 0, len > 0, + bool CopyBlock(UInt32 distance, UInt32 len) + { + UInt32 pos = _pos - distance - 1; + if (distance >= _pos) + { + if (!_overDict || distance >= _bufferSize) + return false; + pos += _bufferSize; + } + if (_limitPos - _pos > len && _bufferSize - pos > len) + { + const Byte *src = _buffer + pos; + Byte *dest = _buffer + _pos; + _pos += len; + do + *dest++ = *src++; + while(--len != 0); + } + else do + { + if (pos == _bufferSize) + pos = 0; + _buffer[_pos++] = _buffer[pos++]; + if (_pos == _limitPos) + FlushWithCheck(); + } + while(--len != 0); + return true; + } + + void PutByte(Byte b) + { + _buffer[_pos++] = b; + if (_pos == _limitPos) + FlushWithCheck(); + } + + Byte GetByte(UInt32 distance) const + { + UInt32 pos = _pos - distance - 1; + if (pos >= _bufferSize) + pos += _bufferSize; + return _buffer[pos]; + } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/LzhDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/LzhDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,220 @@ +// LzhDecoder.cpp + +#include "StdAfx.h" + +#include "LzhDecoder.h" + +#include "Windows/Defs.h" + +namespace NCompress{ +namespace NLzh { +namespace NDecoder { + +static const UInt32 kHistorySize = (1 << 16); + +static const int kBlockSizeBits = 16; +static const int kNumCBits = 9; +static const int kNumLevelBits = 5; // smallest integer such that (1 << kNumLevelBits) > kNumLevelSymbols/ + +UInt32 CCoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); } + +HRESULT CCoder::ReadLevelTable() +{ + int n = ReadBits(kNumLevelBits); + if (n == 0) + { + m_LevelHuffman.Symbol = ReadBits(kNumLevelBits); + if (m_LevelHuffman.Symbol >= kNumLevelSymbols) + return S_FALSE; + } + else + { + if (n > kNumLevelSymbols) + return S_FALSE; + m_LevelHuffman.Symbol = -1; + Byte lens[kNumLevelSymbols]; + int i = 0; + while (i < n) + { + int c = m_InBitStream.ReadBits(3); + if (c == 7) + while (ReadBits(1)) + if (c++ > kMaxHuffmanLen) + return S_FALSE; + lens[i++] = (Byte)c; + if (i == kNumSpecLevelSymbols) + { + c = ReadBits(2); + while (--c >= 0) + lens[i++] = 0; + } + } + while (i < kNumLevelSymbols) + lens[i++] = 0; + m_LevelHuffman.SetCodeLengths(lens); + } + return S_OK; +} + +HRESULT CCoder::ReadPTable(int numBits) +{ + int n = ReadBits(numBits); + if (n == 0) + { + m_PHuffmanDecoder.Symbol = ReadBits(numBits); + if (m_PHuffmanDecoder.Symbol >= kNumDistanceSymbols) + return S_FALSE; + } + else + { + if (n > kNumDistanceSymbols) + return S_FALSE; + m_PHuffmanDecoder.Symbol = -1; + Byte lens[kNumDistanceSymbols]; + int i = 0; + while (i < n) + { + int c = m_InBitStream.ReadBits(3); + if (c == 7) + while (ReadBits(1)) + { + if (c > kMaxHuffmanLen) + return S_FALSE; + c++; + } + lens[i++] = (Byte)c; + } + while (i < kNumDistanceSymbols) + lens[i++] = 0; + m_PHuffmanDecoder.SetCodeLengths(lens); + } + return S_OK; +} + +HRESULT CCoder::ReadCTable() +{ + int n = ReadBits(kNumCBits); + if (n == 0) + { + m_CHuffmanDecoder.Symbol = ReadBits(kNumCBits); + if (m_CHuffmanDecoder.Symbol >= kNumCSymbols) + return S_FALSE; + } + else + { + if (n > kNumCSymbols) + return S_FALSE; + m_CHuffmanDecoder.Symbol = -1; + Byte lens[kNumCSymbols]; + int i = 0; + while (i < n) + { + int c = m_LevelHuffman.Decode(&m_InBitStream); + if (c < kNumSpecLevelSymbols) + { + if (c == 0) + c = 1; + else if (c == 1) + c = ReadBits(4) + 3; + else + c = ReadBits(kNumCBits) + 20; + while (--c >= 0) + { + if (i > kNumCSymbols) + return S_FALSE; + lens[i++] = 0; + } + } + else + lens[i++] = (Byte)(c - 2); + } + while (i < kNumCSymbols) + lens[i++] = 0; + m_CHuffmanDecoder.SetCodeLengths(lens); + } + return S_OK; +} + +STDMETHODIMP CCoder::CodeReal(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 * /* inSize */, const UInt64 *outSize, + ICompressProgressInfo *progress) +{ + if (outSize == NULL) + return E_INVALIDARG; + + if (!m_OutWindowStream.Create(kHistorySize)) + return E_OUTOFMEMORY; + if (!m_InBitStream.Create(1 << 20)) + return E_OUTOFMEMORY; + + UInt64 pos = 0; + m_OutWindowStream.SetStream(outStream); + m_OutWindowStream.Init(false); + m_InBitStream.SetStream(inStream); + m_InBitStream.Init(); + + CCoderReleaser coderReleaser(this); + + int pbit; + if (m_NumDictBits <= 13) + pbit = 4; + else + pbit = 5; + + UInt32 blockSize = 0; + + while(pos < *outSize) + { + // for (i = 0; i < dictSize; i++) dtext[i] = 0x20; + + if (blockSize == 0) + { + if (progress != NULL) + { + UInt64 packSize = m_InBitStream.GetProcessedSize(); + RINOK(progress->SetRatioInfo(&packSize, &pos)); + } + blockSize = ReadBits(kBlockSizeBits); + ReadLevelTable(); + ReadCTable(); + RINOK(ReadPTable(pbit)); + } + blockSize--; + UInt32 c = m_CHuffmanDecoder.Decode(&m_InBitStream); + if (c < 256) + { + m_OutWindowStream.PutByte((Byte)c); + pos++; + } + else if (c >= kNumCSymbols) + return S_FALSE; + else + { + // offset = (interface->method == LARC_METHOD_NUM) ? 0x100 - 2 : 0x100 - 3; + UInt32 len = c - 256 + kMinMatch; + UInt32 distance = m_PHuffmanDecoder.Decode(&m_InBitStream); + if (distance != 0) + distance = (1 << (distance - 1)) + ReadBits(distance - 1); + if (distance >= pos) + return S_FALSE; + if (pos + len > *outSize) + len = (UInt32)(*outSize - pos); + pos += len; + m_OutWindowStream.CopyBlock(distance, len); + } + } + coderReleaser.NeedFlush = false; + return m_OutWindowStream.Flush(); +} + +STDMETHODIMP CCoder::Code(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress) +{ + try { return CodeReal(inStream, outStream, inSize, outSize, progress);} + catch(const CInBufferException &e) { return e.ErrorCode; } + catch(const CLzOutWindowException &e) { return e.ErrorCode; } + catch(...) { return S_FALSE; } +} + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/LzhDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/LzhDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,106 @@ +// LzhDecoder.h + +#ifndef __COMPRESS_LZH_DECODER_H +#define __COMPRESS_LZH_DECODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "../Common/InBuffer.h" + +#include "BitmDecoder.h" +#include "HuffmanDecoder.h" +#include "LzOutWindow.h" + +namespace NCompress { +namespace NLzh { +namespace NDecoder { + +const int kMaxHuffmanLen = 16; // Check it + +const int kNumSpecLevelSymbols = 3; +const int kNumLevelSymbols = kNumSpecLevelSymbols + kMaxHuffmanLen; + +const int kDictBitsMax = 16; +const int kNumDistanceSymbols = kDictBitsMax + 1; + +const int kMaxMatch = 256; +const int kMinMatch = 3; +const int kNumCSymbols = 256 + kMaxMatch + 2 - kMinMatch; + +template +class CHuffmanDecoder:public NCompress::NHuffman::CDecoder +{ +public: + int Symbol; + template + UInt32 Decode(TBitDecoder *bitStream) + { + if (Symbol >= 0) + return (UInt32)Symbol; + return DecodeSymbol(bitStream); + } +}; + +class CCoder : + public ICompressCoder, + public CMyUnknownImp +{ + CLzOutWindow m_OutWindowStream; + NBitm::CDecoder m_InBitStream; + + int m_NumDictBits; + + CHuffmanDecoder m_LevelHuffman; + CHuffmanDecoder m_PHuffmanDecoder; + CHuffmanDecoder m_CHuffmanDecoder; + + void ReleaseStreams() + { + m_OutWindowStream.ReleaseStream(); + m_InBitStream.ReleaseStream(); + } + + class CCoderReleaser + { + CCoder *m_Coder; + public: + bool NeedFlush; + CCoderReleaser(CCoder *coder): m_Coder(coder), NeedFlush(true) {} + ~CCoderReleaser() + { + if (NeedFlush) + m_Coder->m_OutWindowStream.Flush(); + m_Coder->ReleaseStreams(); + } + }; + friend class CCoderReleaser; + + void MakeTable(int nchar, Byte *bitlen, int tablebits, + UInt32 *table, int tablesize); + + UInt32 ReadBits(int numBits); + HRESULT ReadLevelTable(); + HRESULT ReadPTable(int numBits); + HRESULT ReadCTable(); + +public: + + MY_UNKNOWN_IMP + + STDMETHOD(CodeReal)(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress); + + STDMETHOD(Code)(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress); + + void SetDictionary(int numDictBits) { m_NumDictBits = numDictBits; } + CCoder(): m_NumDictBits(0) {} +}; + +}}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/LzmaDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/LzmaDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,190 @@ +// LzmaDecoder.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/Alloc.h" +} + +#include "../Common/StreamUtils.h" + +#include "LzmaDecoder.h" + +static HRESULT SResToHRESULT(SRes res) +{ + switch(res) + { + case SZ_OK: return S_OK; + case SZ_ERROR_MEM: return E_OUTOFMEMORY; + case SZ_ERROR_PARAM: return E_INVALIDARG; + case SZ_ERROR_UNSUPPORTED: return E_NOTIMPL; + // case SZ_ERROR_PROGRESS: return E_ABORT; + case SZ_ERROR_DATA: return S_FALSE; + } + return E_FAIL; +} + +namespace NCompress { +namespace NLzma { + +static const UInt32 kInBufSize = 1 << 20; + +CDecoder::CDecoder(): _inBuf(0), _outSizeDefined(false), FinishStream(false) +{ + LzmaDec_Construct(&_state); +} + +static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); } +static void SzFree(void *p, void *address) { p = p; MyFree(address); } +static ISzAlloc g_Alloc = { SzAlloc, SzFree }; + +CDecoder::~CDecoder() +{ + LzmaDec_Free(&_state, &g_Alloc); + MyFree(_inBuf); +} + +STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *prop, UInt32 size) +{ + RINOK(SResToHRESULT(LzmaDec_Allocate(&_state, prop, size, &g_Alloc))); + + if (_inBuf == 0) + { + _inBuf = (Byte *)MyAlloc(kInBufSize); + if (_inBuf == 0) + return E_OUTOFMEMORY; + } + + return S_OK; +} + +STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value) { *value = _inSizeProcessed; return S_OK; } +STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) { _inStream = inStream; return S_OK; } +STDMETHODIMP CDecoder::ReleaseInStream() { _inStream.Release(); return S_OK; } + +STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize) +{ + _outSizeDefined = (outSize != NULL); + if (_outSizeDefined) + _outSize = *outSize; + + LzmaDec_Init(&_state); + + _inPos = _inSize = 0; + _inSizeProcessed = _outSizeProcessed = 0; + return S_OK; +} + +STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + if (_inBuf == 0) + return S_FALSE; + SetOutStreamSize(outSize); + + for (;;) + { + if (_inPos == _inSize) + { + _inPos = _inSize = 0; + RINOK(inStream->Read(_inBuf, kInBufSize, &_inSize)); + } + + SizeT dicPos = _state.dicPos; + SizeT curSize = _state.dicBufSize - dicPos; + const UInt32 kStepSize = ((UInt32)1 << 22); + if (curSize > kStepSize) + curSize = (SizeT)kStepSize; + + ELzmaFinishMode finishMode = LZMA_FINISH_ANY; + if (_outSizeDefined) + { + const UInt64 rem = _outSize - _outSizeProcessed; + if (rem < curSize) + { + curSize = (SizeT)rem; + if (FinishStream) + finishMode = LZMA_FINISH_END; + } + } + + SizeT inSizeProcessed = _inSize - _inPos; + ELzmaStatus status; + SRes res = LzmaDec_DecodeToDic(&_state, dicPos + curSize, _inBuf + _inPos, &inSizeProcessed, finishMode, &status); + + _inPos += (UInt32)inSizeProcessed; + _inSizeProcessed += inSizeProcessed; + SizeT outSizeProcessed = _state.dicPos - dicPos; + _outSizeProcessed += outSizeProcessed; + + bool finished = (inSizeProcessed == 0 && outSizeProcessed == 0); + bool stopDecoding = (_outSizeDefined && _outSizeProcessed >= _outSize); + + if (res != 0 || _state.dicPos == _state.dicBufSize || finished || stopDecoding) + { + HRESULT res2 = WriteStream(outStream, _state.dic, _state.dicPos); + if (res != 0) + return S_FALSE; + RINOK(res2); + if (stopDecoding) + return S_OK; + if (finished) + return (status == LZMA_STATUS_FINISHED_WITH_MARK ? S_OK : S_FALSE); + } + if (_state.dicPos == _state.dicBufSize) + _state.dicPos = 0; + + if (progress != NULL) + { + RINOK(progress->SetRatioInfo(&_inSizeProcessed, &_outSizeProcessed)); + } + } +} + +#ifndef NO_READ_FROM_CODER + +STDMETHODIMP CDecoder::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + if (processedSize) + *processedSize = 0; + do + { + if (_inPos == _inSize) + { + _inPos = _inSize = 0; + RINOK(_inStream->Read(_inBuf, kInBufSize, &_inSize)); + } + { + SizeT inProcessed = _inSize - _inPos; + + if (_outSizeDefined) + { + const UInt64 rem = _outSize - _outSizeProcessed; + if (rem < size) + size = (UInt32)rem; + } + + SizeT outProcessed = size; + ELzmaStatus status; + SRes res = LzmaDec_DecodeToBuf(&_state, (Byte *)data, &outProcessed, + _inBuf + _inPos, &inProcessed, LZMA_FINISH_ANY, &status); + _inPos += (UInt32)inProcessed; + _inSizeProcessed += inProcessed; + _outSizeProcessed += outProcessed; + size -= (UInt32)outProcessed; + data = (Byte *)data + outProcessed; + if (processedSize) + *processedSize += (UInt32)outProcessed; + RINOK(SResToHRESULT(res)); + if (inProcessed == 0 && outProcessed == 0) + return S_OK; + } + } + while (size != 0); + return S_OK; +} + +#endif + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/LzmaDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/LzmaDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,73 @@ +// LzmaDecoder.h + +#ifndef __LZMA_DECODER_H +#define __LZMA_DECODER_H + +extern "C" +{ +#include "../../../C/LzmaDec.h" +} + +#include "../../Common/MyCom.h" +#include "../ICoder.h" + +namespace NCompress { +namespace NLzma { + +class CDecoder: + public ICompressCoder, + public ICompressSetDecoderProperties2, + public ICompressGetInStreamProcessedSize, + #ifndef NO_READ_FROM_CODER + public ICompressSetInStream, + public ICompressSetOutStreamSize, + public ISequentialInStream, + #endif + public CMyUnknownImp +{ + CMyComPtr _inStream; + Byte *_inBuf; + UInt32 _inPos; + UInt32 _inSize; + CLzmaDec _state; + bool _outSizeDefined; + UInt64 _outSize; + UInt64 _inSizeProcessed; + UInt64 _outSizeProcessed; +public: + + #ifndef NO_READ_FROM_CODER + MY_UNKNOWN_IMP5( + ICompressSetDecoderProperties2, + ICompressGetInStreamProcessedSize, + ICompressSetInStream, + ICompressSetOutStreamSize, + ISequentialInStream) + #else + MY_UNKNOWN_IMP2( + ICompressSetDecoderProperties2, + ICompressGetInStreamProcessedSize) + #endif + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); + STDMETHOD(GetInStreamProcessedSize)(UInt64 *value); + STDMETHOD(SetInStream)(ISequentialInStream *inStream); + STDMETHOD(ReleaseInStream)(); + STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); + + #ifndef NO_READ_FROM_CODER + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + #endif + + bool FinishStream; + + CDecoder(); + virtual ~CDecoder(); + +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/LzmaRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/LzmaRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// LzmaRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "LzmaDecoder.h" + +static void *CreateCodec() { return (void *)(ICompressCoder *)(new NCompress::NLzma::CDecoder); } +#ifndef EXTRACT_ONLY +#include "LzmaEncoder.h" +static void *CreateCodecOut() { return (void *)(ICompressCoder *)(new NCompress::NLzma::CEncoder); } +#else +#define CreateCodecOut 0 +#endif + +static CCodecInfo g_CodecInfo = + { CreateCodec, CreateCodecOut, 0x030101, L"LZMA", 1, false }; + +REGISTER_CODEC(LZMA) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Mtf8.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Mtf8.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,196 @@ +// Mtf8.h + +#ifndef __COMPRESS_MTF8_H +#define __COMPRESS_MTF8_H + +#include "../../Common/Types.h" + +namespace NCompress { + +struct CMtf8Encoder +{ + Byte Buf[256]; + + int FindAndMove(Byte v) + { + int pos; + for (pos = 0; Buf[pos] != v; pos++); + int resPos = pos; + for (; pos >= 8; pos -= 8) + { + Buf[pos] = Buf[pos - 1]; + Buf[pos - 1] = Buf[pos - 2]; + Buf[pos - 2] = Buf[pos - 3]; + Buf[pos - 3] = Buf[pos - 4]; + Buf[pos - 4] = Buf[pos - 5]; + Buf[pos - 5] = Buf[pos - 6]; + Buf[pos - 6] = Buf[pos - 7]; + Buf[pos - 7] = Buf[pos - 8]; + } + for (; pos > 0; pos--) + Buf[pos] = Buf[pos - 1]; + Buf[0] = v; + return resPos; + } +}; + +/* +struct CMtf8Decoder +{ + Byte Buf[256]; + + void Init(int) {}; + Byte GetHead() const { return Buf[0]; } + Byte GetAndMove(int pos) + { + Byte res = Buf[pos]; + for (; pos >= 8; pos -= 8) + { + Buf[pos] = Buf[pos - 1]; + Buf[pos - 1] = Buf[pos - 2]; + Buf[pos - 2] = Buf[pos - 3]; + Buf[pos - 3] = Buf[pos - 4]; + Buf[pos - 4] = Buf[pos - 5]; + Buf[pos - 5] = Buf[pos - 6]; + Buf[pos - 6] = Buf[pos - 7]; + Buf[pos - 7] = Buf[pos - 8]; + } + for (; pos > 0; pos--) + Buf[pos] = Buf[pos - 1]; + Buf[0] = res; + return res; + } +}; +*/ + +#ifdef _WIN64 +#define MODE_64BIT +#endif + +#ifdef MODE_64BIT +typedef UInt64 CMtfVar; +#define MTF_MOVS 3 +#else +typedef UInt32 CMtfVar; +#define MTF_MOVS 2 +#endif + +#define MTF_MASK ((1 << MTF_MOVS) - 1) + + +struct CMtf8Decoder +{ + CMtfVar Buf[256 >> MTF_MOVS]; + + void StartInit() { memset(Buf, 0, sizeof(Buf)); } + void Add(unsigned int pos, Byte val) { Buf[pos >> MTF_MOVS] |= ((CMtfVar)val << ((pos & MTF_MASK) << 3)); } + Byte GetHead() const { return (Byte)Buf[0]; } + Byte GetAndMove(unsigned int pos) + { + UInt32 lim = ((UInt32)pos >> MTF_MOVS); + pos = (pos & MTF_MASK) << 3; + CMtfVar prev = (Buf[lim] >> pos) & 0xFF; + + UInt32 i = 0; + if ((lim & 1) != 0) + { + CMtfVar next = Buf[0]; + Buf[0] = (next << 8) | prev; + prev = (next >> (MTF_MASK << 3)); + i = 1; + lim -= 1; + } + for (; i < lim; i += 2) + { + CMtfVar next = Buf[i]; + Buf[i] = (next << 8) | prev; + prev = (next >> (MTF_MASK << 3)); + next = Buf[i + 1]; + Buf[i + 1] = (next << 8) | prev; + prev = (next >> (MTF_MASK << 3)); + } + CMtfVar next = Buf[i]; + CMtfVar mask = (((CMtfVar)0x100 << pos) - 1); + Buf[i] = (next & ~mask) | (((next << 8) | prev) & mask); + return (Byte)Buf[0]; + } +}; + +/* +const int kSmallSize = 64; +class CMtf8Decoder +{ + Byte SmallBuffer[kSmallSize]; + int SmallSize; + Byte Counts[16]; + int Size; +public: + Byte Buf[256]; + + Byte GetHead() const + { + if (SmallSize > 0) + return SmallBuffer[kSmallSize - SmallSize]; + return Buf[0]; + } + + void Init(int size) + { + Size = size; + SmallSize = 0; + for (int i = 0; i < 16; i++) + { + Counts[i] = ((size >= 16) ? 16 : size); + size -= Counts[i]; + } + } + + Byte GetAndMove(int pos) + { + if (pos < SmallSize) + { + Byte *p = SmallBuffer + kSmallSize - SmallSize; + Byte res = p[pos]; + for (; pos > 0; pos--) + p[pos] = p[pos - 1]; + SmallBuffer[kSmallSize - SmallSize] = res; + return res; + } + if (SmallSize == kSmallSize) + { + int i = Size - 1; + int g = 16; + do + { + g--; + int offset = (g << 4); + for (int t = Counts[g] - 1; t >= 0; t--, i--) + Buf[i] = Buf[offset + t]; + } + while(g != 0); + + for (i = kSmallSize - 1; i >= 0; i--) + Buf[i] = SmallBuffer[i]; + Init(Size); + } + pos -= SmallSize; + int g; + for (g = 0; pos >= Counts[g]; g++) + pos -= Counts[g]; + int offset = (g << 4); + Byte res = Buf[offset + pos]; + for (pos; pos < 16 - 1; pos++) + Buf[offset + pos] = Buf[offset + pos + 1]; + + SmallSize++; + SmallBuffer[kSmallSize - SmallSize] = res; + + Counts[g]--; + return res; + } +}; +*/ + +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/PpmdContext.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/PpmdContext.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,489 @@ +// PpmdContext.h +// This code is based on Dmitry Shkarin's PPMdH code + +#ifndef __COMPRESS_PPMD_CONTEXT_H +#define __COMPRESS_PPMD_CONTEXT_H + +#include "../../Common/Types.h" + +#include "PpmdSubAlloc.h" +#include "RangeCoder.h" + +namespace NCompress { +namespace NPpmd { + +const int INT_BITS=7, PERIOD_BITS=7, TOT_BITS=INT_BITS+PERIOD_BITS, + INTERVAL=1 << INT_BITS, BIN_SCALE=1 << TOT_BITS, MAX_FREQ=124; + +struct SEE2_CONTEXT +{ + // SEE-contexts for PPM-contexts with masked symbols + UInt16 Summ; + Byte Shift, Count; + void init(int InitVal) { Summ = (UInt16)(InitVal << (Shift=PERIOD_BITS-4)); Count=4; } + unsigned int getMean() + { + unsigned int RetVal=(Summ >> Shift); + Summ = (UInt16)(Summ - RetVal); + return RetVal+(RetVal == 0); + } + void update() + { + if (Shift < PERIOD_BITS && --Count == 0) + { + Summ <<= 1; + Count = (Byte)(3 << Shift++); + } + } +}; + +struct PPM_CONTEXT +{ + UInt16 NumStats; // sizeof(UInt16) > sizeof(Byte) + UInt16 SummFreq; + + struct STATE + { + Byte Symbol, Freq; + UInt16 SuccessorLow; + UInt16 SuccessorHigh; + + UInt32 GetSuccessor() const { return SuccessorLow | ((UInt32)SuccessorHigh << 16); } + void SetSuccessor(UInt32 v) + { + SuccessorLow = (UInt16)(v & 0xFFFF); + SuccessorHigh = (UInt16)((v >> 16) & 0xFFFF); + } + }; + + UInt32 Stats; + UInt32 Suffix; + + PPM_CONTEXT* createChild(CSubAllocator &subAllocator, STATE* pStats, STATE& FirstState) + { + PPM_CONTEXT* pc = (PPM_CONTEXT*) subAllocator.AllocContext(); + if (pc) + { + pc->NumStats = 1; + pc->oneState() = FirstState; + pc->Suffix = subAllocator.GetOffset(this); + pStats->SetSuccessor(subAllocator.GetOffsetNoCheck(pc)); + } + return pc; + } + + STATE& oneState() const { return (STATE&) SummFreq; } +}; + +///////////////////////////////// + +const UInt16 InitBinEsc[] = + {0x3CDD, 0x1F3F, 0x59BF, 0x48F3, 0x64A1, 0x5ABC, 0x6632, 0x6051}; + +struct CInfo +{ + CSubAllocator SubAllocator; + SEE2_CONTEXT SEE2Cont[25][16], DummySEE2Cont; + PPM_CONTEXT * MinContext, * MaxContext; + + PPM_CONTEXT::STATE* FoundState; // found next state transition + int NumMasked, InitEsc, OrderFall, RunLength, InitRL, MaxOrder; + Byte CharMask[256], NS2Indx[256], NS2BSIndx[256], HB2Flag[256]; + Byte EscCount, PrintCount, PrevSuccess, HiBitsFlag; + UInt16 BinSumm[128][64]; // binary SEE-contexts + + UInt16 &GetBinSumm(const PPM_CONTEXT::STATE &rs, int numStates) + { + HiBitsFlag = HB2Flag[FoundState->Symbol]; + return BinSumm[rs.Freq - 1][ + PrevSuccess + NS2BSIndx[numStates - 1] + + HiBitsFlag + 2 * HB2Flag[rs.Symbol] + + ((RunLength >> 26) & 0x20)]; + } + + PPM_CONTEXT *GetContext(UInt32 offset) const { return (PPM_CONTEXT *)SubAllocator.GetPtr(offset); } + PPM_CONTEXT *GetContextNoCheck(UInt32 offset) const { return (PPM_CONTEXT *)SubAllocator.GetPtrNoCheck(offset); } + PPM_CONTEXT::STATE *GetState(UInt32 offset) const { return (PPM_CONTEXT::STATE *)SubAllocator.GetPtr(offset); } + PPM_CONTEXT::STATE *GetStateNoCheck(UInt32 offset) const { return (PPM_CONTEXT::STATE *)SubAllocator.GetPtr(offset); } + + void RestartModelRare() + { + int i, k, m; + memset(CharMask,0,sizeof(CharMask)); + SubAllocator.InitSubAllocator(); + InitRL = -((MaxOrder < 12) ? MaxOrder : 12) - 1; + MinContext = MaxContext = (PPM_CONTEXT*) SubAllocator.AllocContext(); + MinContext->Suffix = 0; + OrderFall = MaxOrder; + MinContext->SummFreq = (UInt16)((MinContext->NumStats = 256) + 1); + FoundState = (PPM_CONTEXT::STATE*)SubAllocator.AllocUnits(256 / 2); + MinContext->Stats = SubAllocator.GetOffsetNoCheck(FoundState); + PrevSuccess = 0; + for (RunLength = InitRL, i = 0; i < 256; i++) + { + PPM_CONTEXT::STATE &state = FoundState[i]; + state.Symbol = (Byte)i; + state.Freq = 1; + state.SetSuccessor(0); + } + for (i = 0; i < 128; i++) + for (k = 0; k < 8; k++) + for ( m=0; m < 64; m += 8) + BinSumm[i][k + m] = (UInt16)(BIN_SCALE - InitBinEsc[k] / (i + 2)); + for (i = 0; i < 25; i++) + for (k = 0; k < 16; k++) + SEE2Cont[i][k].init(5*i+10); + } + + void StartModelRare(int maxOrder) + { + int i, k, m ,Step; + EscCount=PrintCount=1; + if (maxOrder < 2) + { + memset(CharMask,0,sizeof(CharMask)); + OrderFall = MaxOrder; + MinContext = MaxContext; + while (MinContext->Suffix != 0) + { + MinContext = GetContextNoCheck(MinContext->Suffix); + OrderFall--; + } + FoundState = GetState(MinContext->Stats); + MinContext = MaxContext; + } + else + { + MaxOrder = maxOrder; + RestartModelRare(); + NS2BSIndx[0] = 2 * 0; + NS2BSIndx[1] = 2 * 1; + memset(NS2BSIndx + 2, 2 * 2, 9); + memset(NS2BSIndx + 11, 2 * 3, 256 - 11); + for (i = 0; i < 3; i++) + NS2Indx[i] = (Byte)i; + for (m = i, k = Step = 1; i < 256; i++) + { + NS2Indx[i] = (Byte)m; + if ( !--k ) + { + k = ++Step; + m++; + } + } + memset(HB2Flag, 0, 0x40); + memset(HB2Flag + 0x40, 0x08, 0x100 - 0x40); + DummySEE2Cont.Shift = PERIOD_BITS; + } + } + + PPM_CONTEXT* CreateSuccessors(bool skip, PPM_CONTEXT::STATE* p1) + { + // static UpState declaration bypasses IntelC bug + // static PPM_CONTEXT::STATE UpState; + PPM_CONTEXT::STATE UpState; + + PPM_CONTEXT *pc = MinContext; + PPM_CONTEXT *UpBranch = GetContext(FoundState->GetSuccessor()); + PPM_CONTEXT::STATE * p, * ps[MAX_O], ** pps = ps; + if ( !skip ) + { + *pps++ = FoundState; + if ( !pc->Suffix ) + goto NO_LOOP; + } + if ( p1 ) + { + p = p1; + pc = GetContext(pc->Suffix); + goto LOOP_ENTRY; + } + do + { + pc = GetContext(pc->Suffix); + if (pc->NumStats != 1) + { + if ((p = GetStateNoCheck(pc->Stats))->Symbol != FoundState->Symbol) + do { p++; } while (p->Symbol != FoundState->Symbol); + } + else + p = &(pc->oneState()); +LOOP_ENTRY: + if (GetContext(p->GetSuccessor()) != UpBranch) + { + pc = GetContext(p->GetSuccessor()); + break; + } + *pps++ = p; + } + while ( pc->Suffix ); +NO_LOOP: + if (pps == ps) + return pc; + UpState.Symbol = *(Byte*) UpBranch; + UpState.SetSuccessor(SubAllocator.GetOffset(UpBranch) + 1); + if (pc->NumStats != 1) + { + if ((p = GetStateNoCheck(pc->Stats))->Symbol != UpState.Symbol) + do { p++; } while (p->Symbol != UpState.Symbol); + unsigned int cf = p->Freq-1; + unsigned int s0 = pc->SummFreq - pc->NumStats - cf; + UpState.Freq = (Byte)(1 + ((2 * cf <= s0) ? (5 * cf > s0) : + ((2 * cf + 3 * s0 - 1) / (2 * s0)))); + } + else + UpState.Freq = pc->oneState().Freq; + do + { + pc = pc->createChild(SubAllocator, *--pps, UpState); + if ( !pc ) + return NULL; + } + while (pps != ps); + return pc; + } + + void UpdateModel() + { + PPM_CONTEXT::STATE fs = *FoundState, * p = NULL; + PPM_CONTEXT* pc, * Successor; + unsigned int ns1, ns, cf, sf, s0; + if (fs.Freq < MAX_FREQ / 4 && MinContext->Suffix != 0) + { + pc = GetContextNoCheck(MinContext->Suffix); + + if (pc->NumStats != 1) + { + if ((p = GetStateNoCheck(pc->Stats))->Symbol != fs.Symbol) + { + do { p++; } while (p->Symbol != fs.Symbol); + if (p[0].Freq >= p[-1].Freq) + { + _PPMD_SWAP(p[0],p[-1]); + p--; + } + } + if (p->Freq < MAX_FREQ-9) + { + p->Freq += 2; + pc->SummFreq += 2; + } + } + else + { + p = &(pc->oneState()); + p->Freq = (Byte)(p->Freq + ((p->Freq < 32) ? 1 : 0)); + } + } + if ( !OrderFall ) + { + MinContext = MaxContext = CreateSuccessors(true, p); + FoundState->SetSuccessor(SubAllocator.GetOffset(MinContext)); + if (MinContext == 0) + goto RESTART_MODEL; + return; + } + *SubAllocator.pText++ = fs.Symbol; + Successor = (PPM_CONTEXT*) SubAllocator.pText; + if (SubAllocator.pText >= SubAllocator.UnitsStart) + goto RESTART_MODEL; + if (fs.GetSuccessor() != 0) + { + if ((Byte *)GetContext(fs.GetSuccessor()) <= SubAllocator.pText) + { + PPM_CONTEXT* cs = CreateSuccessors(false, p); + fs.SetSuccessor(SubAllocator.GetOffset(cs)); + if (cs == NULL) + goto RESTART_MODEL; + } + if ( !--OrderFall ) + { + Successor = GetContext(fs.GetSuccessor()); + SubAllocator.pText -= (MaxContext != MinContext); + } + } + else + { + FoundState->SetSuccessor(SubAllocator.GetOffsetNoCheck(Successor)); + fs.SetSuccessor(SubAllocator.GetOffsetNoCheck(MinContext)); + } + s0 = MinContext->SummFreq - (ns = MinContext->NumStats) - (fs.Freq - 1); + for (pc = MaxContext; pc != MinContext; pc = GetContext(pc->Suffix)) + { + if ((ns1 = pc->NumStats) != 1) + { + if ((ns1 & 1) == 0) + { + void *ppp = SubAllocator.ExpandUnits(GetState(pc->Stats), ns1 >> 1); + pc->Stats = SubAllocator.GetOffset(ppp); + if (!ppp) + goto RESTART_MODEL; + } + pc->SummFreq = (UInt16)(pc->SummFreq + (2 * ns1 < ns) + 2 * ((4 * ns1 <= ns) & + (pc->SummFreq <= 8 * ns1))); + } + else + { + p = (PPM_CONTEXT::STATE*) SubAllocator.AllocUnits(1); + if ( !p ) + goto RESTART_MODEL; + *p = pc->oneState(); + pc->Stats = SubAllocator.GetOffsetNoCheck(p); + if (p->Freq < MAX_FREQ / 4 - 1) + p->Freq <<= 1; + else + p->Freq = MAX_FREQ - 4; + pc->SummFreq = (UInt16)(p->Freq + InitEsc + (ns > 3)); + } + cf = 2 * fs.Freq * (pc->SummFreq+6); + sf = s0 + pc->SummFreq; + if (cf < 6 * sf) + { + cf = 1 + (cf > sf)+(cf >= 4 * sf); + pc->SummFreq += 3; + } + else + { + cf = 4 + (cf >= 9 * sf) + (cf >= 12 * sf) + (cf >= 15 * sf); + pc->SummFreq = (UInt16)(pc->SummFreq + cf); + } + p = GetState(pc->Stats) + ns1; + p->SetSuccessor(SubAllocator.GetOffset(Successor)); + p->Symbol = fs.Symbol; + p->Freq = (Byte)cf; + pc->NumStats = (UInt16)++ns1; + } + MaxContext = MinContext = GetContext(fs.GetSuccessor()); + return; +RESTART_MODEL: + RestartModelRare(); + EscCount = 0; + PrintCount = 0xFF; + } + + void ClearMask() + { + EscCount = 1; + memset(CharMask, 0, sizeof(CharMask)); + // if (++PrintCount == 0) + // PrintInfo(DecodedFile,EncodedFile); + } + + void update1(PPM_CONTEXT::STATE* p) + { + (FoundState = p)->Freq += 4; + MinContext->SummFreq += 4; + if (p[0].Freq > p[-1].Freq) + { + _PPMD_SWAP(p[0],p[-1]); + FoundState = --p; + if (p->Freq > MAX_FREQ) + rescale(); + } + } + + + void update2(PPM_CONTEXT::STATE* p) + { + (FoundState = p)->Freq += 4; + MinContext->SummFreq += 4; + if (p->Freq > MAX_FREQ) + rescale(); + EscCount++; + RunLength = InitRL; + } + + SEE2_CONTEXT* makeEscFreq2(int Diff, UInt32 &scale) + { + SEE2_CONTEXT* psee2c; + if (MinContext->NumStats != 256) + { + psee2c = SEE2Cont[NS2Indx[Diff-1]] + + (Diff < (GetContext(MinContext->Suffix))->NumStats - MinContext->NumStats) + + 2 * (MinContext->SummFreq < 11 * MinContext->NumStats) + + 4 * (NumMasked > Diff) + + HiBitsFlag; + scale = psee2c->getMean(); + } + else + { + psee2c = &DummySEE2Cont; + scale = 1; + } + return psee2c; + } + + + + void rescale() + { + int OldNS = MinContext->NumStats, i = MinContext->NumStats - 1, Adder, EscFreq; + PPM_CONTEXT::STATE* p1, * p; + PPM_CONTEXT::STATE *stats = GetStateNoCheck(MinContext->Stats); + for (p = FoundState; p != stats; p--) + _PPMD_SWAP(p[0], p[-1]); + stats->Freq += 4; + MinContext->SummFreq += 4; + EscFreq = MinContext->SummFreq - p->Freq; + Adder = (OrderFall != 0); + p->Freq = (Byte)((p->Freq + Adder) >> 1); + MinContext->SummFreq = p->Freq; + do + { + EscFreq -= (++p)->Freq; + p->Freq = (Byte)((p->Freq + Adder) >> 1); + MinContext->SummFreq = (UInt16)(MinContext->SummFreq + p->Freq); + if (p[0].Freq > p[-1].Freq) + { + PPM_CONTEXT::STATE tmp = *(p1 = p); + do + { + p1[0] = p1[-1]; + } + while (--p1 != stats && tmp.Freq > p1[-1].Freq); + *p1 = tmp; + } + } + while ( --i ); + if (p->Freq == 0) + { + do { i++; } while ((--p)->Freq == 0); + EscFreq += i; + MinContext->NumStats = (UInt16)(MinContext->NumStats - i); + if (MinContext->NumStats == 1) + { + PPM_CONTEXT::STATE tmp = *stats; + do { tmp.Freq = (Byte)(tmp.Freq - (tmp.Freq >> 1)); EscFreq >>= 1; } while (EscFreq > 1); + SubAllocator.FreeUnits(stats, (OldNS+1) >> 1); + *(FoundState = &MinContext->oneState()) = tmp; return; + } + } + EscFreq -= (EscFreq >> 1); + MinContext->SummFreq = (UInt16)(MinContext->SummFreq + EscFreq); + int n0 = (OldNS+1) >> 1, n1 = (MinContext->NumStats + 1) >> 1; + if (n0 != n1) + MinContext->Stats = SubAllocator.GetOffset(SubAllocator.ShrinkUnits(stats, n0, n1)); + FoundState = GetState(MinContext->Stats); + } + + void NextContext() + { + PPM_CONTEXT *c = GetContext(FoundState->GetSuccessor()); + if (!OrderFall && (Byte *)c > SubAllocator.pText) + MinContext = MaxContext = c; + else + { + UpdateModel(); + if (EscCount == 0) + ClearMask(); + } + } +}; + +// Tabulated escapes for exponential symbol distribution +const Byte ExpEscape[16]={ 25,14, 9, 7, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2 }; +#define GET_MEAN(SUMM,SHIFT,ROUND) ((SUMM+(1 << (SHIFT-ROUND))) >> (SHIFT)) + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/PpmdDecode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/PpmdDecode.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,154 @@ +// PpmdDecode.h +// This code is based on Dmitry Shkarin's PPMdH code + +#ifndef __COMPRESS_PPMD_DECODE_H +#define __COMPRESS_PPMD_DECODE_H + +#include "PpmdContext.h" + +namespace NCompress { +namespace NPpmd { + +class CRangeDecoderVirt +{ +public: + virtual UInt32 GetThreshold(UInt32 total) = 0; + virtual void Decode(UInt32 start, UInt32 size) = 0; + virtual UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits) = 0; +}; + +typedef NRangeCoder::CDecoder CRangeDecoderMy; + +class CRangeDecoder:public CRangeDecoderVirt, public CRangeDecoderMy +{ + UInt32 GetThreshold(UInt32 total) { return CRangeDecoderMy::GetThreshold(total); } + void Decode(UInt32 start, UInt32 size) { CRangeDecoderMy::Decode(start, size); } + UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits) { return CRangeDecoderMy::DecodeBit(size0, numTotalBits); } +}; + +struct CDecodeInfo: public CInfo +{ + void DecodeBinSymbol(CRangeDecoderVirt *rangeDecoder) + { + PPM_CONTEXT::STATE& rs = MinContext->oneState(); + UInt16& bs = GetBinSumm(rs, GetContextNoCheck(MinContext->Suffix)->NumStats); + if (rangeDecoder->DecodeBit(bs, TOT_BITS) == 0) + { + FoundState = &rs; + rs.Freq = (Byte)(rs.Freq + (rs.Freq < 128 ? 1: 0)); + bs = (UInt16)(bs + INTERVAL - GET_MEAN(bs, PERIOD_BITS, 2)); + PrevSuccess = 1; + RunLength++; + } + else + { + bs = (UInt16)(bs - GET_MEAN(bs, PERIOD_BITS, 2)); + InitEsc = ExpEscape[bs >> 10]; + NumMasked = 1; + CharMask[rs.Symbol] = EscCount; + PrevSuccess = 0; + FoundState = NULL; + } + } + + void DecodeSymbol1(CRangeDecoderVirt *rangeDecoder) + { + PPM_CONTEXT::STATE* p = GetStateNoCheck(MinContext->Stats); + int i, count, hiCnt; + if ((count = rangeDecoder->GetThreshold(MinContext->SummFreq)) < (hiCnt = p->Freq)) + { + PrevSuccess = (2 * hiCnt > MinContext->SummFreq); + RunLength += PrevSuccess; + rangeDecoder->Decode(0, p->Freq); // MinContext->SummFreq); + (FoundState = p)->Freq = (Byte)(hiCnt += 4); + MinContext->SummFreq += 4; + if (hiCnt > MAX_FREQ) + rescale(); + return; + } + PrevSuccess = 0; + i = MinContext->NumStats - 1; + while ((hiCnt += (++p)->Freq) <= count) + if (--i == 0) + { + HiBitsFlag = HB2Flag[FoundState->Symbol]; + rangeDecoder->Decode(hiCnt, MinContext->SummFreq - hiCnt); // , MinContext->SummFreq); + CharMask[p->Symbol] = EscCount; + i = (NumMasked = MinContext->NumStats)-1; + FoundState = NULL; + do { CharMask[(--p)->Symbol] = EscCount; } while ( --i ); + return; + } + rangeDecoder->Decode(hiCnt - p->Freq, p->Freq); // , MinContext->SummFreq); + update1(p); + } + + + void DecodeSymbol2(CRangeDecoderVirt *rangeDecoder) + { + int count, hiCnt, i = MinContext->NumStats - NumMasked; + UInt32 freqSum; + SEE2_CONTEXT* psee2c = makeEscFreq2(i, freqSum); + PPM_CONTEXT::STATE* ps[256], ** pps = ps, * p = GetStateNoCheck(MinContext->Stats)-1; + hiCnt = 0; + do + { + do { p++; } while (CharMask[p->Symbol] == EscCount); + hiCnt += p->Freq; + *pps++ = p; + } + while ( --i ); + + freqSum += hiCnt; + count = rangeDecoder->GetThreshold(freqSum); + + p = *(pps = ps); + if (count < hiCnt) + { + hiCnt = 0; + while ((hiCnt += p->Freq) <= count) + p=*++pps; + rangeDecoder->Decode(hiCnt - p->Freq, p->Freq); // , freqSum); + + psee2c->update(); + update2(p); + } + else + { + rangeDecoder->Decode(hiCnt, freqSum - hiCnt); // , freqSum); + + i = MinContext->NumStats - NumMasked; + pps--; + do { CharMask[(*++pps)->Symbol] = EscCount; } while ( --i ); + psee2c->Summ = (UInt16)(psee2c->Summ + freqSum); + NumMasked = MinContext->NumStats; + } + } + + int DecodeSymbol(CRangeDecoderVirt *rangeDecoder) + { + if (MinContext->NumStats != 1) + DecodeSymbol1(rangeDecoder); + else + DecodeBinSymbol(rangeDecoder); + while ( !FoundState ) + { + do + { + OrderFall++; + MinContext = GetContext(MinContext->Suffix); + if (MinContext == 0) + return -1; + } + while (MinContext->NumStats == NumMasked); + DecodeSymbol2(rangeDecoder); + } + Byte symbol = FoundState->Symbol; + NextContext(); + return symbol; + } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/PpmdDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/PpmdDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,182 @@ +// PpmdDecoder.cpp + +#include "StdAfx.h" + +#include "Common/Defs.h" +#include "Windows/Defs.h" + +#include "PpmdDecoder.h" + +namespace NCompress { +namespace NPpmd { + +const int kLenIdFinished = -1; +const int kLenIdNeedInit = -2; + +STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *properties, UInt32 size) +{ + if (size < 5) + return E_INVALIDARG; + _order = properties[0]; + _usedMemorySize = 0; + for (int i = 0; i < 4; i++) + _usedMemorySize += ((UInt32)(properties[1 + i])) << (i * 8); + + if (_usedMemorySize > kMaxMemBlockSize) + return E_NOTIMPL; + + if (!_rangeDecoder.Create(1 << 20)) + return E_OUTOFMEMORY; + if (!_info.SubAllocator.StartSubAllocator(_usedMemorySize)) + return E_OUTOFMEMORY; + + return S_OK; +} + +class CDecoderFlusher +{ + CDecoder *_coder; +public: + bool NeedFlush; + CDecoderFlusher(CDecoder *coder): _coder(coder), NeedFlush(true) {} + ~CDecoderFlusher() + { + if (NeedFlush) + _coder->Flush(); + _coder->ReleaseStreams(); + } +}; + +HRESULT CDecoder::CodeSpec(UInt32 size, Byte *memStream) +{ + if (_outSizeDefined) + { + const UInt64 rem = _outSize - _processedSize; + if (size > rem) + size = (UInt32)rem; + } + const UInt32 startSize = size; + + if (_remainLen == kLenIdFinished) + return S_OK; + if (_remainLen == kLenIdNeedInit) + { + _rangeDecoder.Init(); + _remainLen = 0; + _info.MaxOrder = 0; + _info.StartModelRare(_order); + } + while (size != 0) + { + int symbol = _info.DecodeSymbol(&_rangeDecoder); + if (symbol < 0) + { + _remainLen = kLenIdFinished; + break; + } + if (memStream != 0) + *memStream++ = (Byte)symbol; + else + _outStream.WriteByte((Byte)symbol); + size--; + } + _processedSize += startSize - size; + return S_OK; +} + +STDMETHODIMP CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + if (!_outStream.Create(1 << 20)) + return E_OUTOFMEMORY; + + SetInStream(inStream); + _outStream.SetStream(outStream); + SetOutStreamSize(outSize); + CDecoderFlusher flusher(this); + + for (;;) + { + _processedSize = _outStream.GetProcessedSize(); + UInt32 curSize = (1 << 18); + RINOK(CodeSpec(curSize, NULL)); + if (_remainLen == kLenIdFinished) + break; + if (progress != NULL) + { + UInt64 inSize = _rangeDecoder.GetProcessedSize(); + RINOK(progress->SetRatioInfo(&inSize, &_processedSize)); + } + if (_outSizeDefined) + if (_outStream.GetProcessedSize() >= _outSize) + break; + } + flusher.NeedFlush = false; + return Flush(); +} + +#ifdef _NO_EXCEPTIONS + +#define PPMD_TRY_BEGIN +#define PPMD_TRY_END + +#else + +#define PPMD_TRY_BEGIN try { +#define PPMD_TRY_END } \ + catch(const CInBufferException &e) { return e.ErrorCode; } \ + catch(const COutBufferException &e) { return e.ErrorCode; } \ + catch(...) { return S_FALSE; } + +#endif + + +STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + PPMD_TRY_BEGIN + return CodeReal(inStream, outStream, inSize, outSize, progress); + PPMD_TRY_END +} + +STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) +{ + _rangeDecoder.SetStream(inStream); + return S_OK; +} + +STDMETHODIMP CDecoder::ReleaseInStream() +{ + _rangeDecoder.ReleaseStream(); + return S_OK; +} + +STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize) +{ + _outSizeDefined = (outSize != NULL); + if (_outSizeDefined) + _outSize = *outSize; + _processedSize = 0; + _remainLen = kLenIdNeedInit; + _outStream.Init(); + return S_OK; +} + +#ifndef NO_READ_FROM_CODER + +STDMETHODIMP CDecoder::Read(void *data, UInt32 size, UInt32 *processedSize) +{ + PPMD_TRY_BEGIN + if (processedSize) + *processedSize = 0; + const UInt64 startPos = _processedSize; + RINOK(CodeSpec(size, (Byte *)data)); + if (processedSize) + *processedSize = (UInt32)(_processedSize - startPos); + return Flush(); + PPMD_TRY_END +} + +#endif + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/PpmdDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/PpmdDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,86 @@ +// PpmdDecoder.h + +#ifndef __COMPRESS_PPMD_DECODER_H +#define __COMPRESS_PPMD_DECODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "../Common/OutBuffer.h" + +#include "PpmdDecode.h" +#include "RangeCoder.h" + +namespace NCompress { +namespace NPpmd { + +class CDecoder : + public ICompressCoder, + public ICompressSetDecoderProperties2, + #ifndef NO_READ_FROM_CODER + public ICompressSetInStream, + public ICompressSetOutStreamSize, + public ISequentialInStream, + #endif + public CMyUnknownImp +{ + CRangeDecoder _rangeDecoder; + + COutBuffer _outStream; + + CDecodeInfo _info; + + Byte _order; + UInt32 _usedMemorySize; + + int _remainLen; + UInt64 _outSize; + bool _outSizeDefined; + UInt64 _processedSize; + + HRESULT CodeSpec(UInt32 num, Byte *memStream); + +public: + + #ifndef NO_READ_FROM_CODER + MY_UNKNOWN_IMP4( + ICompressSetDecoderProperties2, + ICompressSetInStream, + ICompressSetOutStreamSize, + ISequentialInStream) + #else + MY_UNKNOWN_IMP1( + ICompressSetDecoderProperties2) + #endif + + void ReleaseStreams() + { + ReleaseInStream(); + _outStream.ReleaseStream(); + } + + HRESULT Flush() { return _outStream.Flush(); } + + STDMETHOD(CodeReal)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); + + STDMETHOD(SetInStream)(ISequentialInStream *inStream); + STDMETHOD(ReleaseInStream)(); + STDMETHOD(SetOutStreamSize)(const UInt64 *outSize); + + #ifndef NO_READ_FROM_CODER + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize); + #endif + + CDecoder(): _outSizeDefined(false) {} +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/PpmdRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/PpmdRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// PpmdRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "PpmdDecoder.h" + +static void *CreateCodec() { return (void *)(ICompressCoder *)(new NCompress::NPpmd::CDecoder); } +#ifndef EXTRACT_ONLY +#include "PpmdEncoder.h" +static void *CreateCodecOut() { return (void *)(ICompressCoder *)(new NCompress::NPpmd::CEncoder); } +#else +#define CreateCodecOut 0 +#endif + +static CCodecInfo g_CodecInfo = + { CreateCodec, CreateCodecOut, 0x030401, L"PPMD", 1, false }; + +REGISTER_CODEC(PPMD) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/PpmdSubAlloc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/PpmdSubAlloc.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,295 @@ +// PpmdSubAlloc.h +// This code is based on Dmitry Shkarin's PPMdH code + +#ifndef __COMPRESS_PPMD_SUB_ALLOC_H +#define __COMPRESS_PPMD_SUB_ALLOC_H + +extern "C" +{ +#include "../../../C/Alloc.h" +} + +#include "PpmdType.h" + +const UINT N1=4, N2=4, N3=4, N4=(128+3-1*N1-2*N2-3*N3)/4; +const UINT UNIT_SIZE=12, N_INDEXES=N1+N2+N3+N4; + +// Extra 1 * UNIT_SIZE for NULL support +// Extra 2 * UNIT_SIZE for s0 in GlueFreeBlocks() +const UInt32 kExtraSize = (UNIT_SIZE * 3); +const UInt32 kMaxMemBlockSize = 0xFFFFFFFF - kExtraSize; + +struct MEM_BLK +{ + UInt16 Stamp, NU; + UInt32 Next, Prev; + void InsertAt(Byte *Base, UInt32 p) + { + Prev = p; + MEM_BLK *pp = (MEM_BLK *)(Base + p); + Next = pp->Next; + pp->Next = ((MEM_BLK *)(Base + Next))->Prev = (UInt32)((Byte *)this - Base); + } + void Remove(Byte *Base) + { + ((MEM_BLK *)(Base + Prev))->Next = Next; + ((MEM_BLK *)(Base + Next))->Prev = Prev; + } +}; + + +class CSubAllocator +{ + UInt32 SubAllocatorSize; + Byte Indx2Units[N_INDEXES], Units2Indx[128], GlueCount; + UInt32 FreeList[N_INDEXES]; + + Byte *Base; + Byte *HeapStart, *LoUnit, *HiUnit; +public: + Byte *pText, *UnitsStart; + CSubAllocator(): + SubAllocatorSize(0), + GlueCount(0), + LoUnit(0), + HiUnit(0), + pText(0), + UnitsStart(0) + { + memset(Indx2Units, 0, sizeof(Indx2Units)); + memset(FreeList, 0, sizeof(FreeList)); + } + ~CSubAllocator() + { + StopSubAllocator(); + }; + + void *GetPtr(UInt32 offset) const { return (offset == 0) ? 0 : (void *)(Base + offset); } + void *GetPtrNoCheck(UInt32 offset) const { return (void *)(Base + offset); } + UInt32 GetOffset(void *ptr) const { return (ptr == 0) ? 0 : (UInt32)((Byte *)ptr - Base); } + UInt32 GetOffsetNoCheck(void *ptr) const { return (UInt32)((Byte *)ptr - Base); } + MEM_BLK *GetBlk(UInt32 offset) const { return (MEM_BLK *)(Base + offset); } + UInt32 *GetNode(UInt32 offset) const { return (UInt32 *)(Base + offset); } + + void InsertNode(void* p, int indx) + { + *(UInt32 *)p = FreeList[indx]; + FreeList[indx] = GetOffsetNoCheck(p); + } + + void* RemoveNode(int indx) + { + UInt32 offset = FreeList[indx]; + UInt32 *p = GetNode(offset); + FreeList[indx] = *p; + return (void *)p; + } + + UINT U2B(int NU) const { return (UINT)(NU) * UNIT_SIZE; } + + void SplitBlock(void* pv, int oldIndx, int newIndx) + { + int i, UDiff = Indx2Units[oldIndx] - Indx2Units[newIndx]; + Byte* p = ((Byte*)pv) + U2B(Indx2Units[newIndx]); + if (Indx2Units[i = Units2Indx[UDiff-1]] != UDiff) + { + InsertNode(p, --i); + p += U2B(i = Indx2Units[i]); + UDiff -= i; + } + InsertNode(p, Units2Indx[UDiff - 1]); + } + + UInt32 GetUsedMemory() const + { + UInt32 RetVal = SubAllocatorSize - (UInt32)(HiUnit - LoUnit) - (UInt32)(UnitsStart - pText); + for (UInt32 i = 0; i < N_INDEXES; i++) + for (UInt32 pn = FreeList[i]; pn != 0; RetVal -= (UInt32)Indx2Units[i] * UNIT_SIZE) + pn = *GetNode(pn); + return (RetVal >> 2); + } + + UInt32 GetSubAllocatorSize() const { return SubAllocatorSize; } + + void StopSubAllocator() + { + if (SubAllocatorSize != 0) + { + BigFree(Base); + SubAllocatorSize = 0; + Base = 0; + } + } + + bool StartSubAllocator(UInt32 size) + { + if (SubAllocatorSize == size) + return true; + StopSubAllocator(); + if (size == 0) + Base = 0; + else + { + if ((Base = (Byte *)::BigAlloc(size + kExtraSize)) == 0) + return false; + HeapStart = Base + UNIT_SIZE; // we need such code to support NULL; + } + SubAllocatorSize = size; + return true; + } + + void InitSubAllocator() + { + int i, k; + memset(FreeList, 0, sizeof(FreeList)); + HiUnit = (pText = HeapStart) + SubAllocatorSize; + UINT Diff = UNIT_SIZE * (SubAllocatorSize / 8 / UNIT_SIZE * 7); + LoUnit = UnitsStart = HiUnit - Diff; + for (i = 0, k=1; i < N1 ; i++, k += 1) Indx2Units[i] = (Byte)k; + for (k++; i < N1 + N2 ;i++, k += 2) Indx2Units[i] = (Byte)k; + for (k++; i < N1 + N2 + N3 ;i++,k += 3) Indx2Units[i] = (Byte)k; + for (k++; i < N1 + N2 + N3 + N4; i++, k += 4) Indx2Units[i] = (Byte)k; + GlueCount = 0; + for (k = i = 0; k < 128; k++) + { + i += (Indx2Units[i] < k+1); + Units2Indx[k] = (Byte)i; + } + } + + void GlueFreeBlocks() + { + UInt32 s0 = (UInt32)(HeapStart + SubAllocatorSize - Base); + + // We need add exta MEM_BLK with Stamp=0 + GetBlk(s0)->Stamp = 0; + s0 += UNIT_SIZE; + MEM_BLK *ps0 = GetBlk(s0); + + UInt32 p; + int i; + if (LoUnit != HiUnit) + *LoUnit=0; + ps0->Next = ps0->Prev = s0; + + for (i = 0; i < N_INDEXES; i++) + while (FreeList[i] != 0) + { + MEM_BLK *pp = (MEM_BLK *)RemoveNode(i); + pp->InsertAt(Base, s0); + pp->Stamp = 0xFFFF; + pp->NU = Indx2Units[i]; + } + for (p = ps0->Next; p != s0; p = GetBlk(p)->Next) + { + for (;;) + { + MEM_BLK *pp = GetBlk(p); + MEM_BLK *pp1 = GetBlk(p + pp->NU * UNIT_SIZE); + if (pp1->Stamp != 0xFFFF || int(pp->NU) + pp1->NU >= 0x10000) + break; + pp1->Remove(Base); + pp->NU = (UInt16)(pp->NU + pp1->NU); + } + } + while ((p = ps0->Next) != s0) + { + MEM_BLK *pp = GetBlk(p); + pp->Remove(Base); + int sz; + for (sz = pp->NU; sz > 128; sz -= 128, p += 128 * UNIT_SIZE) + InsertNode(Base + p, N_INDEXES - 1); + if (Indx2Units[i = Units2Indx[sz-1]] != sz) + { + int k = sz - Indx2Units[--i]; + InsertNode(Base + p + (sz - k) * UNIT_SIZE, k - 1); + } + InsertNode(Base + p, i); + } + } + void* AllocUnitsRare(int indx) + { + if ( !GlueCount ) + { + GlueCount = 255; + GlueFreeBlocks(); + if (FreeList[indx] != 0) + return RemoveNode(indx); + } + int i = indx; + do + { + if (++i == N_INDEXES) + { + GlueCount--; + i = U2B(Indx2Units[indx]); + return (UnitsStart - pText > i) ? (UnitsStart -= i) : (NULL); + } + } while (FreeList[i] == 0); + void* RetVal = RemoveNode(i); + SplitBlock(RetVal, i, indx); + return RetVal; + } + + void* AllocUnits(int NU) + { + int indx = Units2Indx[NU - 1]; + if (FreeList[indx] != 0) + return RemoveNode(indx); + void* RetVal = LoUnit; + LoUnit += U2B(Indx2Units[indx]); + if (LoUnit <= HiUnit) + return RetVal; + LoUnit -= U2B(Indx2Units[indx]); + return AllocUnitsRare(indx); + } + + void* AllocContext() + { + if (HiUnit != LoUnit) + return (HiUnit -= UNIT_SIZE); + if (FreeList[0] != 0) + return RemoveNode(0); + return AllocUnitsRare(0); + } + + void* ExpandUnits(void* oldPtr, int oldNU) + { + int i0=Units2Indx[oldNU - 1], i1=Units2Indx[oldNU - 1 + 1]; + if (i0 == i1) + return oldPtr; + void* ptr = AllocUnits(oldNU + 1); + if (ptr) + { + memcpy(ptr, oldPtr, U2B(oldNU)); + InsertNode(oldPtr, i0); + } + return ptr; + } + + void* ShrinkUnits(void* oldPtr, int oldNU, int newNU) + { + int i0 = Units2Indx[oldNU - 1], i1 = Units2Indx[newNU - 1]; + if (i0 == i1) + return oldPtr; + if (FreeList[i1] != 0) + { + void* ptr = RemoveNode(i1); + memcpy(ptr, oldPtr, U2B(newNU)); + InsertNode(oldPtr,i0); + return ptr; + } + else + { + SplitBlock(oldPtr, i0, i1); + return oldPtr; + } + } + + void FreeUnits(void* ptr, int oldNU) + { + InsertNode(ptr, Units2Indx[oldNU - 1]); + } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/PpmdType.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/PpmdType.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +/**************************************************************************** + * This file is part of PPMd project * + * Written and distributed to public domain by Dmitry Shkarin 1997, * + * 1999-2001 * + * Contents: compilation parameters and miscelaneous definitions * + * Comments: system & compiler dependent file + + * modified by Igor Pavlov (2004-08-29). + ****************************************************************************/ + +#ifndef __COMPRESS_PPMD_TYPE_H +#define __COMPRESS_PPMD_TYPE_H + +const int kMaxOrderCompress = 32; +const int MAX_O = 255; /* maximum allowed model order */ + +template +inline void _PPMD_SWAP(T& t1,T& t2) { T tmp = t1; t1 = t2; t2 = tmp; } + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/RangeCoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/RangeCoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,204 @@ +// Compress/RangeCoder.h + +#ifndef __COMPRESS_RANGE_CODER_H +#define __COMPRESS_RANGE_CODER_H + +#include "../Common/InBuffer.h" +#include "../Common/OutBuffer.h" + +namespace NCompress { +namespace NRangeCoder { + +const int kNumTopBits = 24; +const UInt32 kTopValue = (1 << kNumTopBits); + +class CEncoder +{ + UInt32 _cacheSize; + Byte _cache; +public: + UInt64 Low; + UInt32 Range; + COutBuffer Stream; + bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); } + + void SetStream(ISequentialOutStream *stream) { Stream.SetStream(stream); } + void Init() + { + Stream.Init(); + Low = 0; + Range = 0xFFFFFFFF; + _cacheSize = 1; + _cache = 0; + } + + void FlushData() + { + // Low += 1; + for(int i = 0; i < 5; i++) + ShiftLow(); + } + + HRESULT FlushStream() { return Stream.Flush(); } + + void ReleaseStream() { Stream.ReleaseStream(); } + + void Encode(UInt32 start, UInt32 size, UInt32 total) + { + Low += start * (Range /= total); + Range *= size; + while (Range < kTopValue) + { + Range <<= 8; + ShiftLow(); + } + } + + void ShiftLow() + { + if ((UInt32)Low < (UInt32)0xFF000000 || (int)(Low >> 32) != 0) + { + Byte temp = _cache; + do + { + Stream.WriteByte((Byte)(temp + (Byte)(Low >> 32))); + temp = 0xFF; + } + while(--_cacheSize != 0); + _cache = (Byte)((UInt32)Low >> 24); + } + _cacheSize++; + Low = (UInt32)Low << 8; + } + + void EncodeDirectBits(UInt32 value, int numBits) + { + for (numBits--; numBits >= 0; numBits--) + { + Range >>= 1; + Low += Range & (0 - ((value >> numBits) & 1)); + if (Range < kTopValue) + { + Range <<= 8; + ShiftLow(); + } + } + } + + void EncodeBit(UInt32 size0, UInt32 numTotalBits, UInt32 symbol) + { + UInt32 newBound = (Range >> numTotalBits) * size0; + if (symbol == 0) + Range = newBound; + else + { + Low += newBound; + Range -= newBound; + } + while (Range < kTopValue) + { + Range <<= 8; + ShiftLow(); + } + } + + UInt64 GetProcessedSize() { return Stream.GetProcessedSize() + _cacheSize + 4; } +}; + +class CDecoder +{ +public: + CInBuffer Stream; + UInt32 Range; + UInt32 Code; + bool Create(UInt32 bufferSize) { return Stream.Create(bufferSize); } + + void Normalize() + { + while (Range < kTopValue) + { + Code = (Code << 8) | Stream.ReadByte(); + Range <<= 8; + } + } + + void SetStream(ISequentialInStream *stream) { Stream.SetStream(stream); } + void Init() + { + Stream.Init(); + Code = 0; + Range = 0xFFFFFFFF; + for(int i = 0; i < 5; i++) + Code = (Code << 8) | Stream.ReadByte(); + } + + void ReleaseStream() { Stream.ReleaseStream(); } + + UInt32 GetThreshold(UInt32 total) + { + return (Code) / ( Range /= total); + } + + void Decode(UInt32 start, UInt32 size) + { + Code -= start * Range; + Range *= size; + Normalize(); + } + + UInt32 DecodeDirectBits(int numTotalBits) + { + UInt32 range = Range; + UInt32 code = Code; + UInt32 result = 0; + for (int i = numTotalBits; i != 0; i--) + { + range >>= 1; + /* + result <<= 1; + if (code >= range) + { + code -= range; + result |= 1; + } + */ + UInt32 t = (code - range) >> 31; + code -= range & (t - 1); + result = (result << 1) | (1 - t); + + if (range < kTopValue) + { + code = (code << 8) | Stream.ReadByte(); + range <<= 8; + } + } + Range = range; + Code = code; + return result; + } + + UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits) + { + UInt32 newBound = (Range >> numTotalBits) * size0; + UInt32 symbol; + if (Code < newBound) + { + symbol = 0; + Range = newBound; + } + else + { + symbol = 1; + Code -= newBound; + Range -= newBound; + } + Normalize(); + return symbol; + } + + UInt64 GetProcessedSize() {return Stream.GetProcessedSize(); } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/RangeCoderBit.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/RangeCoderBit.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,113 @@ +// Compress/RangeCoderBit.h + +#ifndef __COMPRESS_RANGE_CODER_BIT_H +#define __COMPRESS_RANGE_CODER_BIT_H + +#include "RangeCoder.h" + +namespace NCompress { +namespace NRangeCoder { + +const int kNumBitModelTotalBits = 11; +const UInt32 kBitModelTotal = (1 << kNumBitModelTotalBits); + +const int kNumMoveReducingBits = 4; + +const int kNumBitPriceShiftBits = 4; +const UInt32 kBitPrice = 1 << kNumBitPriceShiftBits; + +extern UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; + +template +class CBitModel +{ +public: + UInt32 Prob; + void UpdateModel(UInt32 symbol) + { + /* + Prob -= (Prob + ((symbol - 1) & ((1 << numMoveBits) - 1))) >> numMoveBits; + Prob += (1 - symbol) << (kNumBitModelTotalBits - numMoveBits); + */ + if (symbol == 0) + Prob += (kBitModelTotal - Prob) >> numMoveBits; + else + Prob -= (Prob) >> numMoveBits; + } +public: + void Init() { Prob = kBitModelTotal / 2; } +}; + +template +class CBitEncoder: public CBitModel +{ +public: + void Encode(CEncoder *encoder, UInt32 symbol) + { + /* + encoder->EncodeBit(this->Prob, kNumBitModelTotalBits, symbol); + this->UpdateModel(symbol); + */ + UInt32 newBound = (encoder->Range >> kNumBitModelTotalBits) * this->Prob; + if (symbol == 0) + { + encoder->Range = newBound; + this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits; + } + else + { + encoder->Low += newBound; + encoder->Range -= newBound; + this->Prob -= (this->Prob) >> numMoveBits; + } + if (encoder->Range < kTopValue) + { + encoder->Range <<= 8; + encoder->ShiftLow(); + } + } + UInt32 GetPrice(UInt32 symbol) const + { + return ProbPrices[(this->Prob ^ ((-(int)symbol)) & (kBitModelTotal - 1)) >> kNumMoveReducingBits]; + } + UInt32 GetPrice0() const { return ProbPrices[this->Prob >> kNumMoveReducingBits]; } + UInt32 GetPrice1() const { return ProbPrices[(this->Prob ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]; } +}; + + +template +class CBitDecoder: public CBitModel +{ +public: + UInt32 Decode(CDecoder *decoder) + { + UInt32 newBound = (decoder->Range >> kNumBitModelTotalBits) * this->Prob; + if (decoder->Code < newBound) + { + decoder->Range = newBound; + this->Prob += (kBitModelTotal - this->Prob) >> numMoveBits; + if (decoder->Range < kTopValue) + { + decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte(); + decoder->Range <<= 8; + } + return 0; + } + else + { + decoder->Range -= newBound; + decoder->Code -= newBound; + this->Prob -= (this->Prob) >> numMoveBits; + if (decoder->Range < kTopValue) + { + decoder->Code = (decoder->Code << 8) | decoder->Stream.ReadByte(); + decoder->Range <<= 8; + } + return 1; + } + } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Rar1Decoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar1Decoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,478 @@ +// Rar1Decoder.cpp +// According to unRAR license, this code may not be used to develop +// a program that creates RAR archives + +#include "StdAfx.h" + +#include "Rar1Decoder.h" + +namespace NCompress { +namespace NRar1 { + +static UInt32 PosL1[]={0,0,0,2,3,5,7,11,16,20,24,32,32, 256}; +static UInt32 PosL2[]={0,0,0,0,5,7,9,13,18,22,26,34,36, 256}; +static UInt32 PosHf0[]={0,0,0,0,0,8,16,24,33,33,33,33,33, 257}; +static UInt32 PosHf1[]={0,0,0,0,0,0,4,44,60,76,80,80,127, 257}; +static UInt32 PosHf2[]={0,0,0,0,0,0,2,7,53,117,233, 257,0}; +static UInt32 PosHf3[]={0,0,0,0,0,0,0,2,16,218,251, 257,0}; +static UInt32 PosHf4[]={0,0,0,0,0,0,0,0,0,255, 257,0,0}; + +static const UInt32 kHistorySize = (1 << 16); + +class CCoderReleaser +{ + CDecoder *m_Coder; +public: + CCoderReleaser(CDecoder *coder): m_Coder(coder) {} + ~CCoderReleaser() { m_Coder->ReleaseStreams(); } +}; + +CDecoder::CDecoder(): m_IsSolid(false) { } + +void CDecoder::InitStructures() +{ + for(int i = 0; i < kNumRepDists; i++) + m_RepDists[i] = 0; + m_RepDistPtr = 0; + LastLength = 0; + LastDist = 0; +} + +UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); } + +HRESULT CDecoder::CopyBlock(UInt32 distance, UInt32 len) +{ + m_UnpackSize -= len; + return m_OutWindowStream.CopyBlock(distance, len) ? S_OK : S_FALSE; +} + + +UInt32 CDecoder::DecodeNum(const UInt32 *posTab) +{ + UInt32 startPos = 2; + UInt32 num = m_InBitStream.GetValue(12); + for (;;) + { + UInt32 cur = (posTab[startPos + 1] - posTab[startPos]) << (12 - startPos); + if (num < cur) + break; + startPos++; + num -= cur; + } + m_InBitStream.MovePos(startPos); + return((num >> (12 - startPos)) + posTab[startPos]); +} + +static Byte kShortLen1[] = {1,3,4,4,5,6,7,8,8,4,4,5,6,6 }; +static Byte kShortLen1a[] = {1,4,4,4,5,6,7,8,8,4,4,5,6,6,4 }; +static Byte kShortLen2[] = {2,3,3,3,4,4,5,6,6,4,4,5,6,6 }; +static Byte kShortLen2a[] = {2,3,3,4,4,4,5,6,6,4,4,5,6,6,4 }; +static UInt32 kShortXor1[] = {0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xc0,0x80,0x90,0x98,0x9c,0xb0}; +static UInt32 kShortXor2[] = {0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xc0,0x80,0x90,0x98,0x9c,0xb0}; + +HRESULT CDecoder::ShortLZ() +{ + UInt32 len, saveLen, dist; + int distancePlace; + Byte *kShortLen; + const UInt32 *kShortXor; + NumHuf = 0; + + if (LCount == 2) + { + if (ReadBits(1)) + return CopyBlock(LastDist, LastLength); + LCount = 0; + } + + UInt32 bitField = m_InBitStream.GetValue(8); + + if (AvrLn1 < 37) + { + kShortLen = Buf60 ? kShortLen1a : kShortLen1; + kShortXor = kShortXor1; + } + else + { + kShortLen = Buf60 ? kShortLen2a : kShortLen2; + kShortXor = kShortXor2; + } + + for (len = 0; ((bitField ^ kShortXor[len]) & (~(0xff >> kShortLen[len]))) != 0; len++); + m_InBitStream.MovePos(kShortLen[len]); + + if (len >= 9) + { + if (len == 9) + { + LCount++; + return CopyBlock(LastDist, LastLength); + } + if (len == 14) + { + LCount = 0; + len = DecodeNum(PosL2) + 5; + dist = 0x8000 + ReadBits(15) - 1; + LastLength = len; + LastDist = dist; + return CopyBlock(dist, len); + } + + LCount = 0; + saveLen = len; + dist = m_RepDists[(m_RepDistPtr - (len - 9)) & 3]; + len = DecodeNum(PosL1) + 2; + if (len == 0x101 && saveLen == 10) + { + Buf60 ^= 1; + return S_OK; + } + if (dist >= 256) + len++; + if (dist >= MaxDist3 - 1) + len++; + } + else + { + LCount = 0; + AvrLn1 += len; + AvrLn1 -= AvrLn1 >> 4; + + distancePlace = DecodeNum(PosHf2) & 0xff; + dist = ChSetA[distancePlace]; + if (--distancePlace != -1) + { + PlaceA[dist]--; + UInt32 lastDistance = ChSetA[distancePlace]; + PlaceA[lastDistance]++; + ChSetA[distancePlace + 1] = lastDistance; + ChSetA[distancePlace] = dist; + } + len += 2; + } + m_RepDists[m_RepDistPtr++] = dist; + m_RepDistPtr &= 3; + LastLength = len; + LastDist = dist; + return CopyBlock(dist, len); +} + + +HRESULT CDecoder::LongLZ() +{ + UInt32 len; + UInt32 dist; + UInt32 distancePlace, newDistancePlace; + UInt32 oldAvr2, oldAvr3; + + NumHuf = 0; + Nlzb += 16; + if (Nlzb > 0xff) + { + Nlzb = 0x90; + Nhfb >>= 1; + } + oldAvr2=AvrLn2; + + if (AvrLn2 >= 122) + len = DecodeNum(PosL2); + else if (AvrLn2 >= 64) + len = DecodeNum(PosL1); + else + { + UInt32 bitField = m_InBitStream.GetValue(16); + if (bitField < 0x100) + { + len = bitField; + m_InBitStream.MovePos(16); + } + else + { + for (len = 0; ((bitField << len) & 0x8000) == 0; len++) + ; + m_InBitStream.MovePos(len + 1); + } + } + + AvrLn2 += len; + AvrLn2 -= AvrLn2 >> 5; + + if (AvrPlcB > 0x28ff) + distancePlace = DecodeNum(PosHf2); + else if (AvrPlcB > 0x6ff) + distancePlace = DecodeNum(PosHf1); + else + distancePlace = DecodeNum(PosHf0); + + AvrPlcB += distancePlace; + AvrPlcB -= AvrPlcB >> 8; + for (;;) + { + dist = ChSetB[distancePlace & 0xff]; + newDistancePlace = NToPlB[dist++ & 0xff]++; + if (!(dist & 0xff)) + CorrHuff(ChSetB,NToPlB); + else + break; + } + + ChSetB[distancePlace] = ChSetB[newDistancePlace]; + ChSetB[newDistancePlace] = dist; + + dist = ((dist & 0xff00) >> 1) | ReadBits(7); + + oldAvr3 = AvrLn3; + if (len != 1 && len != 4) + if (len == 0 && dist <= MaxDist3) + { + AvrLn3++; + AvrLn3 -= AvrLn3 >> 8; + } + else + if (AvrLn3 > 0) + AvrLn3--; + len += 3; + if (dist >= MaxDist3) + len++; + if (dist <= 256) + len += 8; + if (oldAvr3 > 0xb0 || AvrPlc >= 0x2a00 && oldAvr2 < 0x40) + MaxDist3 = 0x7f00; + else + MaxDist3 = 0x2001; + m_RepDists[m_RepDistPtr++] = --dist; + m_RepDistPtr &= 3; + LastLength = len; + LastDist = dist; + return CopyBlock(dist, len); +} + + +HRESULT CDecoder::HuffDecode() +{ + UInt32 curByte, newBytePlace; + UInt32 len; + UInt32 dist; + int bytePlace; + + if (AvrPlc > 0x75ff) bytePlace = DecodeNum(PosHf4); + else if (AvrPlc > 0x5dff) bytePlace = DecodeNum(PosHf3); + else if (AvrPlc > 0x35ff) bytePlace = DecodeNum(PosHf2); + else if (AvrPlc > 0x0dff) bytePlace = DecodeNum(PosHf1); + else bytePlace = DecodeNum(PosHf0); + if (StMode) + { + if (--bytePlace == -1) + { + if (ReadBits(1)) + { + NumHuf = StMode = 0; + return S_OK; + } + else + { + len = (ReadBits(1)) ? 4 : 3; + dist = DecodeNum(PosHf2); + dist = (dist << 5) | ReadBits(5); + return CopyBlock(dist - 1, len); + } + } + } + else if (NumHuf++ >= 16 && FlagsCnt == 0) + StMode = 1; + bytePlace &= 0xff; + AvrPlc += bytePlace; + AvrPlc -= AvrPlc >> 8; + Nhfb+=16; + if (Nhfb > 0xff) + { + Nhfb=0x90; + Nlzb >>= 1; + } + + m_UnpackSize --; + m_OutWindowStream.PutByte((Byte)(ChSet[bytePlace] >> 8)); + + for (;;) + { + curByte = ChSet[bytePlace]; + newBytePlace = NToPl[curByte++ & 0xff]++; + if ((curByte & 0xff) > 0xa1) + CorrHuff(ChSet, NToPl); + else + break; + } + + ChSet[bytePlace] = ChSet[newBytePlace]; + ChSet[newBytePlace] = curByte; + return S_OK; +} + + +void CDecoder::GetFlagsBuf() +{ + UInt32 flags, newFlagsPlace; + UInt32 flagsPlace = DecodeNum(PosHf2); + + for (;;) + { + flags = ChSetC[flagsPlace]; + FlagBuf = flags >> 8; + newFlagsPlace = NToPlC[flags++ & 0xff]++; + if ((flags & 0xff) != 0) + break; + CorrHuff(ChSetC, NToPlC); + } + + ChSetC[flagsPlace] = ChSetC[newFlagsPlace]; + ChSetC[newFlagsPlace] = flags; +} + +void CDecoder::InitData() +{ + if (!m_IsSolid) + { + AvrPlcB = AvrLn1 = AvrLn2 = AvrLn3 = NumHuf = Buf60 = 0; + AvrPlc = 0x3500; + MaxDist3 = 0x2001; + Nhfb = Nlzb = 0x80; + } + FlagsCnt = 0; + FlagBuf = 0; + StMode = 0; + LCount = 0; +} + +void CDecoder::CorrHuff(UInt32 *CharSet,UInt32 *NumToPlace) +{ + int i; + for (i = 7; i >= 0; i--) + for (int j = 0; j < 32; j++, CharSet++) + *CharSet = (*CharSet & ~0xff) | i; + memset(NumToPlace, 0, sizeof(NToPl)); + for (i = 6; i >= 0; i--) + NumToPlace[i] = (7 - i) * 32; +} + +void CDecoder::InitHuff() +{ + for (UInt32 i = 0; i < 256; i++) + { + Place[i] = PlaceA[i] = PlaceB[i] = i; + PlaceC[i] = (~i + 1) & 0xff; + ChSet[i] = ChSetB[i] = i << 8; + ChSetA[i] = i; + ChSetC[i] = ((~i + 1) & 0xff) << 8; + } + memset(NToPl, 0, sizeof(NToPl)); + memset(NToPlB, 0, sizeof(NToPlB)); + memset(NToPlC, 0, sizeof(NToPlC)); + CorrHuff(ChSetB, NToPlB); +} + +HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo * /* progress */) +{ + if (inSize == NULL || outSize == NULL) + return E_INVALIDARG; + + if (!m_OutWindowStream.Create(kHistorySize)) + return E_OUTOFMEMORY; + if (!m_InBitStream.Create(1 << 20)) + return E_OUTOFMEMORY; + + m_UnpackSize = (Int64)*outSize; + m_OutWindowStream.SetStream(outStream); + m_OutWindowStream.Init(m_IsSolid); + m_InBitStream.SetStream(inStream); + m_InBitStream.Init(); + + CCoderReleaser coderReleaser(this); + InitData(); + if (!m_IsSolid) + { + InitStructures(); + InitHuff(); + } + if (m_UnpackSize > 0) + { + GetFlagsBuf(); + FlagsCnt = 8; + } + + while (m_UnpackSize > 0) + { + if (StMode) + { + RINOK(HuffDecode()); + continue; + } + + if (--FlagsCnt < 0) + { + GetFlagsBuf(); + FlagsCnt=7; + } + + if (FlagBuf & 0x80) + { + FlagBuf <<= 1; + if (Nlzb > Nhfb) + { + RINOK(LongLZ()); + } + else + { + RINOK(HuffDecode()); + } + } + else + { + FlagBuf <<= 1; + if (--FlagsCnt < 0) + { + GetFlagsBuf(); + FlagsCnt = 7; + } + if (FlagBuf & 0x80) + { + FlagBuf <<= 1; + if (Nlzb > Nhfb) + { + RINOK(HuffDecode()); + } + else + { + RINOK(LongLZ()); + } + } + else + { + FlagBuf <<= 1; + RINOK(ShortLZ()); + } + } + } + if (m_UnpackSize < 0) + return S_FALSE; + return m_OutWindowStream.Flush(); +} + +STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + try { return CodeReal(inStream, outStream, inSize, outSize, progress); } + catch(const CInBufferException &e) { return e.ErrorCode; } + catch(const CLzOutWindowException &e) { return e.ErrorCode; } + catch(...) { return S_FALSE; } +} + +STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) +{ + if (size < 1) + return E_INVALIDARG; + m_IsSolid = (data[0] != 0); + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Rar1Decoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar1Decoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,88 @@ +// Rar1Decoder.h +// According to unRAR license, this code may not be used to develop +// a program that creates RAR archives + +#ifndef __COMPRESS_RAR1_DECODER_H +#define __COMPRESS_RAR1_DECODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "../Common/InBuffer.h" + +#include "BitmDecoder.h" +#include "HuffmanDecoder.h" +#include "LzOutWindow.h" + +namespace NCompress { +namespace NRar1 { + +const UInt32 kNumRepDists = 4; + +typedef NBitm::CDecoder CBitDecoder; + +class CDecoder : + public ICompressCoder, + public ICompressSetDecoderProperties2, + public CMyUnknownImp +{ +public: + CLzOutWindow m_OutWindowStream; + CBitDecoder m_InBitStream; + + UInt32 m_RepDists[kNumRepDists]; + UInt32 m_RepDistPtr; + + UInt32 LastDist; + UInt32 LastLength; + + Int64 m_UnpackSize; + bool m_IsSolid; + + UInt32 ReadBits(int numBits); + HRESULT CopyBlock(UInt32 distance, UInt32 len); + + UInt32 DecodeNum(const UInt32 *posTab); + HRESULT ShortLZ(); + HRESULT LongLZ(); + HRESULT HuffDecode(); + void GetFlagsBuf(); + void InitData(); + void InitHuff(); + void CorrHuff(UInt32 *CharSet, UInt32 *NumToPlace); + void OldUnpWriteBuf(); + + UInt32 ChSet[256],ChSetA[256],ChSetB[256],ChSetC[256]; + UInt32 Place[256],PlaceA[256],PlaceB[256],PlaceC[256]; + UInt32 NToPl[256],NToPlB[256],NToPlC[256]; + UInt32 FlagBuf,AvrPlc,AvrPlcB,AvrLn1,AvrLn2,AvrLn3; + int Buf60,NumHuf,StMode,LCount,FlagsCnt; + UInt32 Nhfb,Nlzb,MaxDist3; + + void InitStructures(); + + HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + +public: + CDecoder(); + + MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) + + void ReleaseStreams() + { + m_OutWindowStream.ReleaseStream(); + m_InBitStream.ReleaseStream(); + } + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); + +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Rar2Decoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar2Decoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,389 @@ +// Rar2Decoder.cpp +// According to unRAR license, this code may not be used to develop +// a program that creates RAR archives + +#include "StdAfx.h" + +#include "Rar2Decoder.h" + +namespace NCompress { +namespace NRar2 { + +namespace NMultimedia { + +Byte CFilter::Decode(int &channelDelta, Byte deltaByte) +{ + D4 = D3; + D3 = D2; + D2 = LastDelta - D1; + D1 = LastDelta; + int predictedValue = ((8 * LastChar + K1 * D1 + K2 * D2 + K3 * D3 + K4 * D4 + K5 * channelDelta) >> 3); + + Byte realValue = (Byte)(predictedValue - deltaByte); + int i = ((int)(signed char)deltaByte) << 3; + + Dif[0] += abs(i); + Dif[1] += abs(i - D1); + Dif[2] += abs(i + D1); + Dif[3] += abs(i - D2); + Dif[4] += abs(i + D2); + Dif[5] += abs(i - D3); + Dif[6] += abs(i + D3); + Dif[7] += abs(i - D4); + Dif[8] += abs(i + D4); + Dif[9] += abs(i - channelDelta); + Dif[10] += abs(i + channelDelta); + + channelDelta = LastDelta = (signed char)(realValue - LastChar); + LastChar = realValue; + + if (((++ByteCount) & 0x1F) == 0) + { + UInt32 minDif = Dif[0]; + UInt32 numMinDif = 0; + Dif[0] = 0; + for (i = 1; i < sizeof(Dif) / sizeof(Dif[0]); i++) + { + if (Dif[i] < minDif) + { + minDif = Dif[i]; + numMinDif = i; + } + Dif[i] = 0; + } + switch(numMinDif) + { + case 1: if (K1 >= -16) K1--; break; + case 2: if (K1 < 16) K1++; break; + case 3: if (K2 >= -16) K2--; break; + case 4: if (K2 < 16) K2++; break; + case 5: if (K3 >= -16) K3--; break; + case 6: if (K3 < 16) K3++; break; + case 7: if (K4 >= -16) K4--; break; + case 8: if (K4 < 16) K4++; break; + case 9: if (K5 >= -16) K5--; break; + case 10:if (K5 < 16) K5++; break; + } + } + return realValue; +} +} + +static const char *kNumberErrorMessage = "Number error"; + +static const UInt32 kHistorySize = 1 << 20; + +static const int kNumStats = 11; + +static const UInt32 kWindowReservSize = (1 << 22) + 256; + +CDecoder::CDecoder(): + m_IsSolid(false) +{ +} + +void CDecoder::InitStructures() +{ + m_MmFilter.Init(); + for(int i = 0; i < kNumRepDists; i++) + m_RepDists[i] = 0; + m_RepDistPtr = 0; + m_LastLength = 0; + memset(m_LastLevels, 0, kMaxTableSize); +} + +UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); } + +#define RIF(x) { if (!(x)) return false; } + +bool CDecoder::ReadTables(void) +{ + Byte levelLevels[kLevelTableSize]; + Byte newLevels[kMaxTableSize]; + m_AudioMode = (ReadBits(1) == 1); + + if (ReadBits(1) == 0) + memset(m_LastLevels, 0, kMaxTableSize); + int numLevels; + if (m_AudioMode) + { + m_NumChannels = ReadBits(2) + 1; + if (m_MmFilter.CurrentChannel >= m_NumChannels) + m_MmFilter.CurrentChannel = 0; + numLevels = m_NumChannels * kMMTableSize; + } + else + numLevels = kHeapTablesSizesSum; + + int i; + for (i = 0; i < kLevelTableSize; i++) + levelLevels[i] = (Byte)ReadBits(4); + RIF(m_LevelDecoder.SetCodeLengths(levelLevels)); + i = 0; + while (i < numLevels) + { + UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream); + if (number < kTableDirectLevels) + { + newLevels[i] = (Byte)((number + m_LastLevels[i]) & kLevelMask); + i++; + } + else + { + if (number == kTableLevelRepNumber) + { + int t = ReadBits(2) + 3; + for (int reps = t; reps > 0 && i < numLevels ; reps--, i++) + newLevels[i] = newLevels[i - 1]; + } + else + { + int num; + if (number == kTableLevel0Number) + num = ReadBits(3) + 3; + else if (number == kTableLevel0Number2) + num = ReadBits(7) + 11; + else + return false; + for (;num > 0 && i < numLevels; num--) + newLevels[i++] = 0; + } + } + } + if (m_AudioMode) + for (i = 0; i < m_NumChannels; i++) + { + RIF(m_MMDecoders[i].SetCodeLengths(&newLevels[i * kMMTableSize])); + } + else + { + RIF(m_MainDecoder.SetCodeLengths(&newLevels[0])); + RIF(m_DistDecoder.SetCodeLengths(&newLevels[kMainTableSize])); + RIF(m_LenDecoder.SetCodeLengths(&newLevels[kMainTableSize + kDistTableSize])); + } + memcpy(m_LastLevels, newLevels, kMaxTableSize); + return true; +} + +bool CDecoder::ReadLastTables() +{ + // it differs a little from pure RAR sources; + // UInt64 ttt = m_InBitStream.GetProcessedSize() + 2; + // + 2 works for: return 0xFF; in CInBuffer::ReadByte. + if (m_InBitStream.GetProcessedSize() + 7 <= m_PackSize) // test it: probably incorrect; + // if (m_InBitStream.GetProcessedSize() + 2 <= m_PackSize) // test it: probably incorrect; + if (m_AudioMode) + { + UInt32 symbol = m_MMDecoders[m_MmFilter.CurrentChannel].DecodeSymbol(&m_InBitStream); + if (symbol == 256) + return ReadTables(); + if (symbol >= kMMTableSize) + return false; + } + else + { + UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream); + if (number == kReadTableNumber) + return ReadTables(); + if (number >= kMainTableSize) + return false; + } + return true; +} + +class CCoderReleaser +{ + CDecoder *m_Coder; +public: + CCoderReleaser(CDecoder *coder): m_Coder(coder) {} + ~CCoderReleaser() + { + m_Coder->ReleaseStreams(); + } +}; + +bool CDecoder::DecodeMm(UInt32 pos) +{ + while (pos-- > 0) + { + UInt32 symbol = m_MMDecoders[m_MmFilter.CurrentChannel].DecodeSymbol(&m_InBitStream); + if (symbol == 256) + return true; + if (symbol >= kMMTableSize) + return false; + /* + Byte byPredict = m_Predictor.Predict(); + Byte byReal = (Byte)(byPredict - (Byte)symbol); + m_Predictor.Update(byReal, byPredict); + */ + Byte byReal = m_MmFilter.Decode((Byte)symbol); + m_OutWindowStream.PutByte(byReal); + if (++m_MmFilter.CurrentChannel == m_NumChannels) + m_MmFilter.CurrentChannel = 0; + } + return true; +} + +bool CDecoder::DecodeLz(Int32 pos) +{ + while (pos > 0) + { + UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream); + UInt32 length, distance; + if (number < 256) + { + m_OutWindowStream.PutByte(Byte(number)); + pos--; + continue; + } + else if (number >= kMatchNumber) + { + number -= kMatchNumber; + length = kNormalMatchMinLen + UInt32(kLenStart[number]) + + m_InBitStream.ReadBits(kLenDirectBits[number]); + number = m_DistDecoder.DecodeSymbol(&m_InBitStream); + if (number >= kDistTableSize) + return false; + distance = kDistStart[number] + m_InBitStream.ReadBits(kDistDirectBits[number]); + if (distance >= kDistLimit3) + { + length += 2 - ((distance - kDistLimit4) >> 31); + // length++; + // if (distance >= kDistLimit4) + // length++; + } + } + else if (number == kRepBothNumber) + { + length = m_LastLength; + distance = m_RepDists[(m_RepDistPtr + 4 - 1) & 3]; + } + else if (number < kLen2Number) + { + distance = m_RepDists[(m_RepDistPtr - (number - kRepNumber + 1)) & 3]; + number = m_LenDecoder.DecodeSymbol(&m_InBitStream); + if (number >= kLenTableSize) + return false; + length = 2 + kLenStart[number] + m_InBitStream.ReadBits(kLenDirectBits[number]); + if (distance >= kDistLimit2) + { + length++; + if (distance >= kDistLimit3) + { + length += 2 - ((distance - kDistLimit4) >> 31); + // length++; + // if (distance >= kDistLimit4) + // length++; + } + } + } + else if (number < kReadTableNumber) + { + number -= kLen2Number; + distance = kLen2DistStarts[number] + + m_InBitStream.ReadBits(kLen2DistDirectBits[number]); + length = 2; + } + else if (number == kReadTableNumber) + return true; + else + return false; + m_RepDists[m_RepDistPtr++ & 3] = distance; + m_LastLength = length; + if (!m_OutWindowStream.CopyBlock(distance, length)) + return false; + pos -= length; + } + return true; +} + +HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + if (inSize == NULL || outSize == NULL) + return E_INVALIDARG; + + if (!m_OutWindowStream.Create(kHistorySize)) + return E_OUTOFMEMORY; + if (!m_InBitStream.Create(1 << 20)) + return E_OUTOFMEMORY; + + m_PackSize = *inSize; + + UInt64 pos = 0, unPackSize = *outSize; + + m_OutWindowStream.SetStream(outStream); + m_OutWindowStream.Init(m_IsSolid); + m_InBitStream.SetStream(inStream); + m_InBitStream.Init(); + + CCoderReleaser coderReleaser(this); + if (!m_IsSolid) + { + InitStructures(); + if (unPackSize == 0) + { + if (m_InBitStream.GetProcessedSize() + 2 <= m_PackSize) // test it: probably incorrect; + if (!ReadTables()) + return S_FALSE; + return S_OK; + } + if (!ReadTables()) + return S_FALSE; + } + + UInt64 startPos = m_OutWindowStream.GetProcessedSize(); + while(pos < unPackSize) + { + UInt32 blockSize = 1 << 20; + if (blockSize > unPackSize - pos) + blockSize = (UInt32)(unPackSize - pos); + UInt64 blockStartPos = m_OutWindowStream.GetProcessedSize(); + if (m_AudioMode) + { + if (!DecodeMm(blockSize)) + return S_FALSE; + } + else + { + if (!DecodeLz((Int32)blockSize)) + return S_FALSE; + } + UInt64 globalPos = m_OutWindowStream.GetProcessedSize(); + pos = globalPos - blockStartPos; + if (pos < blockSize) + if (!ReadTables()) + return S_FALSE; + pos = globalPos - startPos; + if (progress != 0) + { + UInt64 packSize = m_InBitStream.GetProcessedSize(); + RINOK(progress->SetRatioInfo(&packSize, &pos)); + } + } + if (pos > unPackSize) + return S_FALSE; + + if (!ReadLastTables()) + return S_FALSE; + return m_OutWindowStream.Flush(); +} + +STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + try { return CodeReal(inStream, outStream, inSize, outSize, progress); } + catch(const CInBufferException &e) { return e.ErrorCode; } + catch(const CLzOutWindowException &e) { return e.ErrorCode; } + catch(...) { return S_FALSE; } +} + +STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) +{ + if (size < 1) + return E_INVALIDARG; + m_IsSolid = (data[0] != 0); + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Rar2Decoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar2Decoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,174 @@ +// Rar2Decoder.h +// According to unRAR license, this code may not be used to develop +// a program that creates RAR archives + +#ifndef __COMPRESS_RAR2_DECODER_H +#define __COMPRESS_RAR2_DECODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "../Common/InBuffer.h" + +#include "BitmDecoder.h" +#include "HuffmanDecoder.h" +#include "LzOutWindow.h" + +namespace NCompress { +namespace NRar2 { + +const UInt32 kNumRepDists = 4; +const UInt32 kDistTableSize = 48; + +const int kMMTableSize = 256 + 1; + +const UInt32 kMainTableSize = 298; +const UInt32 kLenTableSize = 28; + +const UInt32 kDistTableStart = kMainTableSize; +const UInt32 kLenTableStart = kDistTableStart + kDistTableSize; + +const UInt32 kHeapTablesSizesSum = kMainTableSize + kDistTableSize + kLenTableSize; + +const UInt32 kLevelTableSize = 19; + +const UInt32 kMMTablesSizesSum = kMMTableSize * 4; + +const UInt32 kMaxTableSize = kMMTablesSizesSum; + +const UInt32 kTableDirectLevels = 16; +const UInt32 kTableLevelRepNumber = kTableDirectLevels; +const UInt32 kTableLevel0Number = kTableLevelRepNumber + 1; +const UInt32 kTableLevel0Number2 = kTableLevel0Number + 1; + +const UInt32 kLevelMask = 0xF; + + +const UInt32 kRepBothNumber = 256; +const UInt32 kRepNumber = kRepBothNumber + 1; +const UInt32 kLen2Number = kRepNumber + 4; + +const UInt32 kLen2NumNumbers = 8; +const UInt32 kReadTableNumber = kLen2Number + kLen2NumNumbers; +const UInt32 kMatchNumber = kReadTableNumber + 1; + +const Byte kLenStart[kLenTableSize] = {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224}; +const Byte kLenDirectBits[kLenTableSize] = {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5}; + +const UInt32 kDistStart[kDistTableSize] = {0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048,3072,4096,6144,8192,12288,16384,24576,32768U,49152U,65536,98304,131072,196608,262144,327680,393216,458752,524288,589824,655360,720896,786432,851968,917504,983040}; +const Byte kDistDirectBits[kDistTableSize] = {0,0,0,0,1,1,2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}; + +const Byte kLevelDirectBits[kLevelTableSize] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7}; + +const Byte kLen2DistStarts[kLen2NumNumbers]={0,4,8,16,32,64,128,192}; +const Byte kLen2DistDirectBits[kLen2NumNumbers]={2,2,3, 4, 5, 6, 6, 6}; + +const UInt32 kDistLimit2 = 0x101 - 1; +const UInt32 kDistLimit3 = 0x2000 - 1; +const UInt32 kDistLimit4 = 0x40000 - 1; + +const UInt32 kMatchMaxLen = 255 + 2; +const UInt32 kMatchMaxLenMax = 255 + 5; +const UInt32 kNormalMatchMinLen = 3; + +namespace NMultimedia { + +struct CFilter +{ + int K1,K2,K3,K4,K5; + int D1,D2,D3,D4; + int LastDelta; + UInt32 Dif[11]; + UInt32 ByteCount; + int LastChar; + + Byte Decode(int &channelDelta, Byte delta); + + void Init() { memset(this, 0, sizeof(*this)); } + +}; + +const int kNumChanelsMax = 4; + +class CFilter2 +{ +public: + CFilter m_Filters[kNumChanelsMax]; + int m_ChannelDelta; + int CurrentChannel; + + void Init() { memset(this, 0, sizeof(*this)); } + Byte Decode(Byte delta) + { + return m_Filters[CurrentChannel].Decode(m_ChannelDelta, delta); + } + +}; + +} + +typedef NBitm::CDecoder CBitDecoder; + +const int kNumHuffmanBits = 15; + +class CDecoder : + public ICompressCoder, + public ICompressSetDecoderProperties2, + public CMyUnknownImp +{ + CLzOutWindow m_OutWindowStream; + CBitDecoder m_InBitStream; + NHuffman::CDecoder m_MainDecoder; + NHuffman::CDecoder m_DistDecoder; + NHuffman::CDecoder m_LenDecoder; + NHuffman::CDecoder m_MMDecoders[NMultimedia::kNumChanelsMax]; + NHuffman::CDecoder m_LevelDecoder; + + bool m_AudioMode; + + NMultimedia::CFilter2 m_MmFilter; + int m_NumChannels; + + UInt32 m_RepDists[kNumRepDists]; + UInt32 m_RepDistPtr; + + UInt32 m_LastLength; + + Byte m_LastLevels[kMaxTableSize]; + + UInt64 m_PackSize; + bool m_IsSolid; + + void InitStructures(); + UInt32 ReadBits(int numBits); + bool ReadTables(); + bool ReadLastTables(); + + bool DecodeMm(UInt32 pos); + bool DecodeLz(Int32 pos); + + HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + +public: + CDecoder(); + + MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) + + void ReleaseStreams() + { + m_OutWindowStream.ReleaseStream(); + m_InBitStream.ReleaseStream(); + } + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); + +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Rar3Decoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar3Decoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,834 @@ +// Rar3Decoder.cpp +// According to unRAR license, this code may not be used to develop +// a program that creates RAR archives + +#include "StdAfx.h" + +#include "../Common/StreamUtils.h" + +#include "Rar3Decoder.h" + +namespace NCompress { +namespace NRar3 { + +static const UInt32 kNumAlignReps = 15; + +static const UInt32 kSymbolReadTable = 256; +static const UInt32 kSymbolRep = 259; +static const UInt32 kSymbolLen2 = kSymbolRep + kNumReps; + +static const Byte kLenStart[kLenTableSize] = {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224}; +static const Byte kLenDirectBits[kLenTableSize] = {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5}; + +static const Byte kDistDirectBits[kDistTableSize] = + {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15, + 16,16,16,16,16,16,16,16,16,16,16,16,16,16, + 18,18,18,18,18,18,18,18,18,18,18,18}; + +static const Byte kLevelDirectBits[kLevelTableSize] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; + +static const Byte kLen2DistStarts[kNumLen2Symbols]={0,4,8,16,32,64,128,192}; +static const Byte kLen2DistDirectBits[kNumLen2Symbols]={2,2,3, 4, 5, 6, 6, 6}; + +static const UInt32 kDistLimit3 = 0x2000 - 2; +static const UInt32 kDistLimit4 = 0x40000 - 2; + +static const UInt32 kNormalMatchMinLen = 3; + +static const UInt32 kVmDataSizeMax = 1 << 16; +static const UInt32 kVmCodeSizeMax = 1 << 16; + +CDecoder::CDecoder(): + _window(0), + _winPos(0), + _wrPtr(0), + _lzSize(0), + _writtenFileSize(0), + _vmData(0), + _vmCode(0), + m_IsSolid(false) +{ +} + +CDecoder::~CDecoder() +{ + InitFilters(); + ::MidFree(_vmData); + ::MidFree(_window); +} + +HRESULT CDecoder::WriteDataToStream(const Byte *data, UInt32 size) +{ + return WriteStream(_outStream, data, size); +} + +HRESULT CDecoder::WriteData(const Byte *data, UInt32 size) +{ + HRESULT res = S_OK; + if (_writtenFileSize < _unpackSize) + { + UInt32 curSize = size; + UInt64 remain = _unpackSize - _writtenFileSize; + if (remain < curSize) + curSize = (UInt32)remain; + res = WriteDataToStream(data, curSize); + } + _writtenFileSize += size; + return res; +} + +HRESULT CDecoder::WriteArea(UInt32 startPtr, UInt32 endPtr) +{ + if (startPtr <= endPtr) + return WriteData(_window + startPtr, endPtr - startPtr); + RINOK(WriteData(_window + startPtr, kWindowSize - startPtr)); + return WriteData(_window, endPtr); +} + +void CDecoder::ExecuteFilter(int tempFilterIndex, NVm::CBlockRef &outBlockRef) +{ + CTempFilter *tempFilter = _tempFilters[tempFilterIndex]; + tempFilter->InitR[6] = (UInt32)_writtenFileSize; + NVm::SetValue32(&tempFilter->GlobalData[0x24], (UInt32)_writtenFileSize); + NVm::SetValue32(&tempFilter->GlobalData[0x28], (UInt32)(_writtenFileSize >> 32)); + CFilter *filter = _filters[tempFilter->FilterIndex]; + _vm.Execute(filter, tempFilter, outBlockRef, filter->GlobalData); + delete tempFilter; + _tempFilters[tempFilterIndex] = 0; +} + +HRESULT CDecoder::WriteBuf() +{ + UInt32 writtenBorder = _wrPtr; + UInt32 writeSize = (_winPos - writtenBorder) & kWindowMask; + for (int i = 0; i < _tempFilters.Size(); i++) + { + CTempFilter *filter = _tempFilters[i]; + if (filter == NULL) + continue; + if (filter->NextWindow) + { + filter->NextWindow = false; + continue; + } + UInt32 blockStart = filter->BlockStart; + UInt32 blockSize = filter->BlockSize; + if (((blockStart - writtenBorder) & kWindowMask) < writeSize) + { + if (writtenBorder != blockStart) + { + RINOK(WriteArea(writtenBorder, blockStart)); + writtenBorder = blockStart; + writeSize = (_winPos - writtenBorder) & kWindowMask; + } + if (blockSize <= writeSize) + { + UInt32 blockEnd = (blockStart + blockSize) & kWindowMask; + if (blockStart < blockEnd || blockEnd == 0) + _vm.SetMemory(0, _window + blockStart, blockSize); + else + { + UInt32 tailSize = kWindowSize - blockStart; + _vm.SetMemory(0, _window + blockStart, tailSize); + _vm.SetMemory(tailSize, _window, blockEnd); + } + NVm::CBlockRef outBlockRef; + ExecuteFilter(i, outBlockRef); + while (i + 1 < _tempFilters.Size()) + { + CTempFilter *nextFilter = _tempFilters[i + 1]; + if (nextFilter == NULL || nextFilter->BlockStart != blockStart || + nextFilter->BlockSize != outBlockRef.Size || nextFilter->NextWindow) + break; + _vm.SetMemory(0, _vm.GetDataPointer(outBlockRef.Offset), outBlockRef.Size); + ExecuteFilter(++i, outBlockRef); + } + WriteDataToStream(_vm.GetDataPointer(outBlockRef.Offset), outBlockRef.Size); + _writtenFileSize += outBlockRef.Size; + writtenBorder = blockEnd; + writeSize = (_winPos - writtenBorder) & kWindowMask; + } + else + { + for (int j = i; j < _tempFilters.Size(); j++) + { + CTempFilter *filter = _tempFilters[j]; + if (filter != NULL && filter->NextWindow) + filter->NextWindow = false; + } + _wrPtr = writtenBorder; + return S_OK; // check it + } + } + } + + _wrPtr = _winPos; + return WriteArea(writtenBorder, _winPos); +} + +void CDecoder::InitFilters() +{ + _lastFilter = 0; + int i; + for (i = 0; i < _tempFilters.Size(); i++) + delete _tempFilters[i]; + _tempFilters.Clear(); + for (i = 0; i < _filters.Size(); i++) + delete _filters[i]; + _filters.Clear(); +} + +bool CDecoder::AddVmCode(UInt32 firstByte, UInt32 codeSize) +{ + CMemBitDecoder inp; + inp.Init(_vmData, codeSize); + + UInt32 filterIndex; + if (firstByte & 0x80) + { + filterIndex = NVm::ReadEncodedUInt32(inp); + if (filterIndex == 0) + InitFilters(); + else + filterIndex--; + } + else + filterIndex = _lastFilter; + if (filterIndex > (UInt32)_filters.Size()) + return false; + _lastFilter = filterIndex; + bool newFilter = (filterIndex == (UInt32)_filters.Size()); + + CFilter *filter; + if (newFilter) + { + // check if too many filters + if (filterIndex > 1024) + return false; + filter = new CFilter; + _filters.Add(filter); + } + else + { + filter = _filters[filterIndex]; + filter->ExecCount++; + } + + int numEmptyItems = 0; + int i; + for (i = 0; i < _tempFilters.Size(); i++) + { + _tempFilters[i - numEmptyItems] = _tempFilters[i]; + if (_tempFilters[i] == NULL) + numEmptyItems++; + if (numEmptyItems > 0) + _tempFilters[i] = NULL; + } + if (numEmptyItems == 0) + { + _tempFilters.Add(NULL); + numEmptyItems = 1; + } + CTempFilter *tempFilter = new CTempFilter; + _tempFilters[_tempFilters.Size() - numEmptyItems] = tempFilter; + tempFilter->FilterIndex = filterIndex; + tempFilter->ExecCount = filter->ExecCount; + + UInt32 blockStart = NVm::ReadEncodedUInt32(inp); + if (firstByte & 0x40) + blockStart += 258; + tempFilter->BlockStart = (blockStart + _winPos) & kWindowMask; + if (firstByte & 0x20) + filter->BlockSize = NVm::ReadEncodedUInt32(inp); + tempFilter->BlockSize = filter->BlockSize; + tempFilter->NextWindow = _wrPtr != _winPos && ((_wrPtr - _winPos) & kWindowMask) <= blockStart; + + memset(tempFilter->InitR, 0, sizeof(tempFilter->InitR)); + tempFilter->InitR[3] = NVm::kGlobalOffset; + tempFilter->InitR[4] = tempFilter->BlockSize; + tempFilter->InitR[5] = tempFilter->ExecCount; + if (firstByte & 0x10) + { + UInt32 initMask = inp.ReadBits(NVm::kNumGpRegs); + for (int i = 0; i < NVm::kNumGpRegs; i++) + if (initMask & (1 << i)) + tempFilter->InitR[i] = NVm::ReadEncodedUInt32(inp); + } + if (newFilter) + { + UInt32 vmCodeSize = NVm::ReadEncodedUInt32(inp); + if (vmCodeSize >= kVmCodeSizeMax || vmCodeSize == 0) + return false; + for (UInt32 i = 0; i < vmCodeSize; i++) + _vmCode[i] = (Byte)inp.ReadBits(8); + _vm.PrepareProgram(_vmCode, vmCodeSize, filter); + } + + tempFilter->AllocateEmptyFixedGlobal(); + + Byte *globalData = &tempFilter->GlobalData[0]; + for (i = 0; i < NVm::kNumGpRegs; i++) + NVm::SetValue32(&globalData[i * 4], tempFilter->InitR[i]); + NVm::SetValue32(&globalData[NVm::NGlobalOffset::kBlockSize], tempFilter->BlockSize); + NVm::SetValue32(&globalData[NVm::NGlobalOffset::kBlockPos], 0); // It was commented. why? + NVm::SetValue32(&globalData[NVm::NGlobalOffset::kExecCount], tempFilter->ExecCount); + + if (firstByte & 8) + { + UInt32 dataSize = NVm::ReadEncodedUInt32(inp); + if (dataSize > NVm::kGlobalSize - NVm::kFixedGlobalSize) + return false; + CRecordVector &globalData = tempFilter->GlobalData; + int requredSize = (int)(dataSize + NVm::kFixedGlobalSize); + if (globalData.Size() < requredSize) + { + globalData.Reserve(requredSize); + for (; globalData.Size() < requredSize; i++) + globalData.Add(0); + } + for (UInt32 i = 0; i < dataSize; i++) + globalData[NVm::kFixedGlobalSize + i] = (Byte)inp.ReadBits(8); + } + return true; +} + +bool CDecoder::ReadVmCodeLZ() +{ + UInt32 firstByte = m_InBitStream.ReadBits(8); + UInt32 length = (firstByte & 7) + 1; + if (length == 7) + length = m_InBitStream.ReadBits(8) + 7; + else if (length == 8) + length = m_InBitStream.ReadBits(16); + if (length > kVmDataSizeMax) + return false; + for (UInt32 i = 0; i < length; i++) + _vmData[i] = (Byte)m_InBitStream.ReadBits(8); + return AddVmCode(firstByte, length); +} + +bool CDecoder::ReadVmCodePPM() +{ + int firstByte = DecodePpmSymbol(); + if (firstByte == -1) + return false; + UInt32 length = (firstByte & 7) + 1; + if (length == 7) + { + int b1 = DecodePpmSymbol(); + if (b1 == -1) + return false; + length = b1 + 7; + } + else if (length == 8) + { + int b1 = DecodePpmSymbol(); + if (b1 == -1) + return false; + int b2 = DecodePpmSymbol(); + if (b2 == -1) + return false; + length = b1 * 256 + b2; + } + if (length > kVmDataSizeMax) + return false; + for (UInt32 i = 0; i < length; i++) + { + int b = DecodePpmSymbol(); + if (b == -1) + return false; + _vmData[i] = (Byte)b; + } + return AddVmCode(firstByte, length); +} + +#define RIF(x) { if (!(x)) return S_FALSE; } + +UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); } + +///////////////////////////////////////////////// +// PPM + +HRESULT CDecoder::InitPPM() +{ + Byte maxOrder = (Byte)ReadBits(7); + + bool reset = ((maxOrder & 0x20) != 0); + int maxMB = 0; + if (reset) + maxMB = (Byte)ReadBits(8); + else + { + if (_ppm.SubAllocator.GetSubAllocatorSize()== 0) + return S_FALSE; + } + if (maxOrder & 0x40) + PpmEscChar = (Byte)ReadBits(8); + m_InBitStream.InitRangeCoder(); + /* + if (m_InBitStream.m_BitPos != 0) + return S_FALSE; + */ + if (reset) + { + maxOrder = (maxOrder & 0x1F) + 1; + if (maxOrder > 16) + maxOrder = 16 + (maxOrder - 16) * 3; + if (maxOrder == 1) + { + // SubAlloc.StopSubAllocator(); + _ppm.SubAllocator.StopSubAllocator(); + return S_FALSE; + } + // SubAlloc.StartSubAllocator(MaxMB+1); + // StartModelRare(maxOrder); + + if (!_ppm.SubAllocator.StartSubAllocator((maxMB + 1) << 20)) + return E_OUTOFMEMORY; + _ppm.MaxOrder = 0; + _ppm.StartModelRare(maxOrder); + + } + // return (minContext != NULL); + + return S_OK; +} + +int CDecoder::DecodePpmSymbol() { return _ppm.DecodeSymbol(&m_InBitStream); } + +HRESULT CDecoder::DecodePPM(Int32 num, bool &keepDecompressing) +{ + keepDecompressing = false; + do + { + if (((_wrPtr - _winPos) & kWindowMask) < 260 && _wrPtr != _winPos) + { + RINOK(WriteBuf()); + if (_writtenFileSize > _unpackSize) + { + keepDecompressing = false; + return S_OK; + } + } + int c = DecodePpmSymbol(); + if (c == -1) + { + // Original code sets PPMError=true here and then it returns S_OK. Why ??? + // return S_OK; + return S_FALSE; + } + if (c == PpmEscChar) + { + int nextCh = DecodePpmSymbol(); + if (nextCh == 0) + return ReadTables(keepDecompressing); + if (nextCh == 2 || nextCh == -1) + return S_OK; + if (nextCh == 3) + { + if (!ReadVmCodePPM()) + return S_FALSE; + continue; + } + if (nextCh == 4 || nextCh == 5) + { + UInt32 distance = 0; + UInt32 length = 4; + if (nextCh == 4) + { + for (int i = 0; i < 3; i++) + { + int c = DecodePpmSymbol(); + if (c == -1) + return S_OK; + distance = (distance << 8) + (Byte)c; + } + distance++; + length += 28; + } + int c = DecodePpmSymbol(); + if (c == -1) + return S_OK; + length += c; + if (distance >= _lzSize) + return S_FALSE; + CopyBlock(distance, length); + num -= (Int32)length; + continue; + } + } + PutByte((Byte)c); + num--; + } + while (num >= 0); + keepDecompressing = true; + return S_OK; +} + +///////////////////////////////////////////////// +// LZ + +HRESULT CDecoder::ReadTables(bool &keepDecompressing) +{ + keepDecompressing = true; + ReadBits((8 - m_InBitStream.GetBitPosition()) & 7); + if (ReadBits(1) != 0) + { + _lzMode = false; + return InitPPM(); + } + + _lzMode = true; + PrevAlignBits = 0; + PrevAlignCount = 0; + + Byte levelLevels[kLevelTableSize]; + Byte newLevels[kTablesSizesSum]; + + if (ReadBits(1) == 0) + memset(m_LastLevels, 0, kTablesSizesSum); + + int i; + for (i = 0; i < kLevelTableSize; i++) + { + UInt32 length = ReadBits(4); + if (length == 15) + { + UInt32 zeroCount = ReadBits(4); + if (zeroCount != 0) + { + zeroCount += 2; + while (zeroCount-- > 0 && i < kLevelTableSize) + levelLevels[i++]=0; + i--; + continue; + } + } + levelLevels[i] = (Byte)length; + } + RIF(m_LevelDecoder.SetCodeLengths(levelLevels)); + i = 0; + while (i < kTablesSizesSum) + { + UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream); + if (number < 16) + { + newLevels[i] = Byte((number + m_LastLevels[i]) & 15); + i++; + } + else if (number > kLevelTableSize) + return S_FALSE; + else + { + int num; + if (((number - 16) & 1) == 0) + num = ReadBits(3) + 3; + else + num = ReadBits(7) + 11; + if (number < 18) + { + if (i == 0) + return S_FALSE; + for (; num > 0 && i < kTablesSizesSum; num--, i++) + newLevels[i] = newLevels[i - 1]; + } + else + { + for (; num > 0 && i < kTablesSizesSum; num--) + newLevels[i++] = 0; + } + } + } + TablesRead = true; + + // original code has check here: + /* + if (InAddr > ReadTop) + { + keepDecompressing = false; + return true; + } + */ + + RIF(m_MainDecoder.SetCodeLengths(&newLevels[0])); + RIF(m_DistDecoder.SetCodeLengths(&newLevels[kMainTableSize])); + RIF(m_AlignDecoder.SetCodeLengths(&newLevels[kMainTableSize + kDistTableSize])); + RIF(m_LenDecoder.SetCodeLengths(&newLevels[kMainTableSize + kDistTableSize + kAlignTableSize])); + + memcpy(m_LastLevels, newLevels, kTablesSizesSum); + return S_OK; +} + +class CCoderReleaser +{ + CDecoder *m_Coder; +public: + CCoderReleaser(CDecoder *coder): m_Coder(coder) {} + ~CCoderReleaser() + { + // m_Coder->m_OutWindowStream.Flush(); + m_Coder->ReleaseStreams(); + } +}; + +HRESULT CDecoder::ReadEndOfBlock(bool &keepDecompressing) +{ + if (ReadBits(1) != 0) + { + // old file + TablesRead = false; + return ReadTables(keepDecompressing); + } + // new file + keepDecompressing = false; + TablesRead = (ReadBits(1) == 0); + return S_OK; +} + +UInt32 kDistStart[kDistTableSize]; + +class CDistInit +{ +public: + CDistInit() { Init(); } + void Init() + { + UInt32 start = 0; + for (UInt32 i = 0; i < kDistTableSize; i++) + { + kDistStart[i] = start; + start += (1 << kDistDirectBits[i]); + } + } +} g_DistInit; + +HRESULT CDecoder::DecodeLZ(bool &keepDecompressing) +{ + UInt32 rep0 = _reps[0]; + UInt32 rep1 = _reps[1]; + UInt32 rep2 = _reps[2]; + UInt32 rep3 = _reps[3]; + UInt32 length = _lastLength; + for (;;) + { + if (((_wrPtr - _winPos) & kWindowMask) < 260 && _wrPtr != _winPos) + { + RINOK(WriteBuf()); + if (_writtenFileSize > _unpackSize) + { + keepDecompressing = false; + return S_OK; + } + } + UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream); + if (number < 256) + { + PutByte(Byte(number)); + + continue; + } + else if (number == kSymbolReadTable) + { + RINOK(ReadEndOfBlock(keepDecompressing)); + break; + } + else if (number == 257) + { + if (!ReadVmCodeLZ()) + return S_FALSE; + continue; + } + else if (number == 258) + { + } + else if (number < kSymbolRep + 4) + { + if (number != kSymbolRep) + { + UInt32 distance; + if (number == kSymbolRep + 1) + distance = rep1; + else + { + if (number == kSymbolRep + 2) + distance = rep2; + else + { + distance = rep3; + rep3 = rep2; + } + rep2 = rep1; + } + rep1 = rep0; + rep0 = distance; + } + + UInt32 number = m_LenDecoder.DecodeSymbol(&m_InBitStream); + if (number >= kLenTableSize) + return S_FALSE; + length = 2 + kLenStart[number] + m_InBitStream.ReadBits(kLenDirectBits[number]); + } + else + { + rep3 = rep2; + rep2 = rep1; + rep1 = rep0; + if (number < 271) + { + number -= 263; + rep0 = kLen2DistStarts[number] + m_InBitStream.ReadBits(kLen2DistDirectBits[number]); + length = 2; + } + else if (number < 299) + { + number -= 271; + length = kNormalMatchMinLen + (UInt32)kLenStart[number] + m_InBitStream.ReadBits(kLenDirectBits[number]); + UInt32 number = m_DistDecoder.DecodeSymbol(&m_InBitStream); + if (number >= kDistTableSize) + return S_FALSE; + rep0 = kDistStart[number]; + int numBits = kDistDirectBits[number]; + if (number >= (kNumAlignBits * 2) + 2) + { + if (numBits > kNumAlignBits) + rep0 += (m_InBitStream.ReadBits(numBits - kNumAlignBits) << kNumAlignBits); + if (PrevAlignCount > 0) + { + PrevAlignCount--; + rep0 += PrevAlignBits; + } + else + { + UInt32 number = m_AlignDecoder.DecodeSymbol(&m_InBitStream); + if (number < (1 << kNumAlignBits)) + { + rep0 += number; + PrevAlignBits = number; + } + else if (number == (1 << kNumAlignBits)) + { + PrevAlignCount = kNumAlignReps; + rep0 += PrevAlignBits; + } + else + return S_FALSE; + } + } + else + rep0 += m_InBitStream.ReadBits(numBits); + length += ((kDistLimit4 - rep0) >> 31) + ((kDistLimit3 - rep0) >> 31); + } + else + return S_FALSE; + } + if (rep0 >= _lzSize) + return S_FALSE; + CopyBlock(rep0, length); + } + _reps[0] = rep0; + _reps[1] = rep1; + _reps[2] = rep2; + _reps[3] = rep3; + _lastLength = length; + + return S_OK; +} + +HRESULT CDecoder::CodeReal(ICompressProgressInfo *progress) +{ + _writtenFileSize = 0; + if (!m_IsSolid) + { + _lzSize = 0; + _winPos = 0; + _wrPtr = 0; + for (int i = 0; i < kNumReps; i++) + _reps[i] = 0; + _lastLength = 0; + memset(m_LastLevels, 0, kTablesSizesSum); + TablesRead = false; + PpmEscChar = 2; + InitFilters(); + } + if (!m_IsSolid || !TablesRead) + { + bool keepDecompressing; + RINOK(ReadTables(keepDecompressing)); + if (!keepDecompressing) + return S_OK; + } + + for(;;) + { + bool keepDecompressing; + if (_lzMode) + { + RINOK(DecodeLZ(keepDecompressing)) + } + else + { + RINOK(DecodePPM(1 << 18, keepDecompressing)) + } + UInt64 packSize = m_InBitStream.GetProcessedSize(); + RINOK(progress->SetRatioInfo(&packSize, &_writtenFileSize)); + if (!keepDecompressing) + break; + } + RINOK(WriteBuf()); + if (_writtenFileSize < _unpackSize) + return S_FALSE; + // return m_OutWindowStream.Flush(); + return S_OK; +} + +STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + try + { + if (inSize == NULL || outSize == NULL) + return E_INVALIDARG; + + if (_vmData == 0) + { + _vmData = (Byte *)::MidAlloc(kVmDataSizeMax + kVmCodeSizeMax); + if (_vmData == 0) + return E_OUTOFMEMORY; + _vmCode = _vmData + kVmDataSizeMax; + } + + if (_window == 0) + { + _window = (Byte *)::MidAlloc(kWindowSize); + if (_window == 0) + return E_OUTOFMEMORY; + } + if (!m_InBitStream.Create(1 << 20)) + return E_OUTOFMEMORY; + if (!_vm.Create()) + return E_OUTOFMEMORY; + + + m_InBitStream.SetStream(inStream); + m_InBitStream.Init(); + _outStream = outStream; + + CCoderReleaser coderReleaser(this); + _unpackSize = *outSize; + return CodeReal(progress); + } + catch(const CInBufferException &e) { return e.ErrorCode; } + catch(...) { return S_FALSE; } + // CNewException is possible here. But probably CNewException is caused + // by error in data stream. +} + +STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) +{ + if (size < 1) + return E_INVALIDARG; + m_IsSolid = (data[0] != 0); + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Rar3Decoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar3Decoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,301 @@ +// Rar3Decoder.h +// According to unRAR license, this code may not be used to develop +// a program that creates RAR archives + +#ifndef __COMPRESS_RAR3_DECODER_H +#define __COMPRESS_RAR3_DECODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +#include "../Common/InBuffer.h" + +#include "BitmDecoder.h" +#include "HuffmanDecoder.h" +#include "PpmdDecode.h" +#include "Rar3Vm.h" + +namespace NCompress { +namespace NRar3 { + +const UInt32 kWindowSize = 1 << 22; +const UInt32 kWindowMask = (kWindowSize - 1); + +const UInt32 kNumReps = 4; +const UInt32 kNumLen2Symbols = 8; +const UInt32 kLenTableSize = 28; +const UInt32 kMainTableSize = 256 + 1 + 1 + 1 + kNumReps + kNumLen2Symbols + kLenTableSize; +const UInt32 kDistTableSize = 60; + +const int kNumAlignBits = 4; +const UInt32 kAlignTableSize = (1 << kNumAlignBits) + 1; + +const UInt32 kLevelTableSize = 20; + +const UInt32 kTablesSizesSum = kMainTableSize + kDistTableSize + kAlignTableSize + kLenTableSize; + +class CBitDecoder +{ + UInt32 m_Value; +public: + UInt32 m_BitPos; + CInBuffer m_Stream; + bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } + void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);} + void ReleaseStream() { m_Stream.ReleaseStream();} + + void Init() + { + m_Stream.Init(); + m_BitPos = 0; + m_Value = 0; + // m_BitPos = kNumBigValueBits; + // Normalize(); + } + + UInt64 GetProcessedSize() const + { return m_Stream.GetProcessedSize() - (m_BitPos) / 8; } + UInt32 GetBitPosition() const { return ((8 - m_BitPos) & 7); } + + /* + void Normalize() + { + for (;m_BitPos >= 8; m_BitPos -= 8) + m_Value = (m_Value << 8) | m_Stream.ReadByte(); + } + */ + + UInt32 GetValue(UInt32 numBits) + { + // return (m_Value << m_BitPos) >> (kNumBigValueBits - numBits); + // return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits); + if (m_BitPos < numBits) + { + m_BitPos += 8; + m_Value = (m_Value << 8) | m_Stream.ReadByte(); + if (m_BitPos < numBits) + { + m_BitPos += 8; + m_Value = (m_Value << 8) | m_Stream.ReadByte(); + } + } + return m_Value >> (m_BitPos - numBits); + } + + void MovePos(UInt32 numBits) + { + m_BitPos -= numBits; + m_Value = m_Value & ((1 << m_BitPos) - 1); + } + + UInt32 ReadBits(UInt32 numBits) + { + UInt32 res = GetValue(numBits); + MovePos(numBits); + return res; + } +}; + +const int kNumTopBits = 24; +const UInt32 kTopValue = (1 << kNumTopBits); +const UInt32 kBot = (1 << 15); + +class CRangeDecoder:public NPpmd::CRangeDecoderVirt, public CBitDecoder +{ +public: + UInt32 Range; + UInt32 Low; + UInt32 Code; + + void Normalize() + { + while ((Low ^ (Low + Range)) < kTopValue || + Range < kBot && ((Range = (0 - Low) & (kBot - 1)), 1)) + { + Code = (Code << 8) | m_Stream.ReadByte(); + Range <<= 8; + Low <<= 8; + } + } + + void InitRangeCoder() + { + Code = 0; + Low = 0; + Range = 0xFFFFFFFF; + for(int i = 0; i < 4; i++) + Code = (Code << 8) | ReadBits(8); + } + + virtual UInt32 GetThreshold(UInt32 total) + { + return (Code - Low) / ( Range /= total); + } + + virtual void Decode(UInt32 start, UInt32 size) + { + Low += start * Range; + Range *= size; + Normalize(); + } + + virtual UInt32 DecodeBit(UInt32 size0, UInt32 numTotalBits) + { + if (((Code - Low) / (Range >>= numTotalBits)) < size0) + { + Decode(0, size0); + return 0; + } + else + { + Decode(size0, (1 << numTotalBits) - size0); + return 1; + } + } + + // UInt64 GetProcessedSizeRangeCoder() {return Stream.GetProcessedSize(); } +}; + + +struct CFilter: public NVm::CProgram +{ + CRecordVector GlobalData; + UInt32 BlockStart; + UInt32 BlockSize; + UInt32 ExecCount; + CFilter(): BlockStart(0), BlockSize(0), ExecCount(0) {} +}; + +struct CTempFilter: public NVm::CProgramInitState +{ + UInt32 BlockStart; + UInt32 BlockSize; + UInt32 ExecCount; + bool NextWindow; + + UInt32 FilterIndex; +}; + +const int kNumHuffmanBits = 15; + +class CDecoder: + public ICompressCoder, + public ICompressSetDecoderProperties2, + public CMyUnknownImp +{ + CRangeDecoder m_InBitStream; + Byte *_window; + UInt32 _winPos; + UInt32 _wrPtr; + UInt64 _lzSize; + UInt64 _unpackSize; + UInt64 _writtenFileSize; // if it's > _unpackSize, then _unpackSize only written + CMyComPtr _outStream; + NHuffman::CDecoder m_MainDecoder; + NHuffman::CDecoder m_DistDecoder; + NHuffman::CDecoder m_AlignDecoder; + NHuffman::CDecoder m_LenDecoder; + NHuffman::CDecoder m_LevelDecoder; + + UInt32 _reps[kNumReps]; + UInt32 _lastLength; + + Byte m_LastLevels[kTablesSizesSum]; + + Byte *_vmData; + Byte *_vmCode; + NVm::CVm _vm; + CRecordVector _filters; + CRecordVector _tempFilters; + UInt32 _lastFilter; + + bool m_IsSolid; + + bool _lzMode; + + UInt32 PrevAlignBits; + UInt32 PrevAlignCount; + + bool TablesRead; + + NPpmd::CDecodeInfo _ppm; + int PpmEscChar; + + HRESULT WriteDataToStream(const Byte *data, UInt32 size); + HRESULT WriteData(const Byte *data, UInt32 size); + HRESULT WriteArea(UInt32 startPtr, UInt32 endPtr); + void ExecuteFilter(int tempFilterIndex, NVm::CBlockRef &outBlockRef); + HRESULT WriteBuf(); + + void InitFilters(); + bool AddVmCode(UInt32 firstByte, UInt32 codeSize); + bool ReadVmCodeLZ(); + bool ReadVmCodePPM(); + + UInt32 ReadBits(int numBits); + + HRESULT InitPPM(); + int DecodePpmSymbol(); + HRESULT DecodePPM(Int32 num, bool &keepDecompressing); + + HRESULT ReadTables(bool &keepDecompressing); + HRESULT ReadEndOfBlock(bool &keepDecompressing); + HRESULT DecodeLZ(bool &keepDecompressing); + HRESULT CodeReal(ICompressProgressInfo *progress); +public: + CDecoder(); + ~CDecoder(); + + MY_UNKNOWN_IMP1(ICompressSetDecoderProperties2) + + void ReleaseStreams() + { + _outStream.Release(); + m_InBitStream.ReleaseStream(); + } + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); + + void CopyBlock(UInt32 distance, UInt32 len) + { + _lzSize += len; + UInt32 pos = (_winPos - distance - 1) & kWindowMask; + Byte *window = _window; + UInt32 winPos = _winPos; + if (kWindowSize - winPos > len && kWindowSize - pos > len) + { + const Byte *src = window + pos; + Byte *dest = window + winPos; + _winPos += len; + do + *dest++ = *src++; + while(--len != 0); + return; + } + do + { + window[winPos] = window[pos]; + winPos = (winPos + 1) & kWindowMask; + pos = (pos + 1) & kWindowMask; + } + while(--len != 0); + _winPos = winPos; + } + + void PutByte(Byte b) + { + _window[_winPos] = b; + _winPos = (_winPos + 1) & kWindowMask; + _lzSize++; + } + + +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Rar3Vm.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar3Vm.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1094 @@ +// Rar3Vm.cpp +// According to unRAR license, this code may not be used to develop +// a program that creates RAR archives + +/* +Note: + Due to performance considerations Rar VM may set Flags C incorrectly + for some operands (SHL x, 0, ... ). + Check implementation of concrete VM command + to see if it sets flags right. +*/ + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/7zCrc.h" +#include "../../../C/Alloc.h" +} + +#include "Rar3Vm.h" + +namespace NCompress { +namespace NRar3 { + +UInt32 CMemBitDecoder::ReadBits(int numBits) +{ + UInt32 res = 0; + for (;;) + { + Byte b = _bitPos < _bitSize ? _data[_bitPos >> 3] : 0; + int avail = (int)(8 - (_bitPos & 7)); + if (numBits <= avail) + { + _bitPos += numBits; + return res | (b >> (avail - numBits)) & ((1 << numBits) - 1); + } + numBits -= avail; + res |= (UInt32)(b & ((1 << avail) - 1)) << numBits; + _bitPos += avail; + } +} + +UInt32 CMemBitDecoder::ReadBit() { return ReadBits(1); } + +namespace NVm { + +static const UInt32 kStackRegIndex = kNumRegs - 1; + +static const UInt32 FLAG_C = 1; +static const UInt32 FLAG_Z = 2; +static const UInt32 FLAG_S = 0x80000000; + +static const Byte CF_OP0 = 0; +static const Byte CF_OP1 = 1; +static const Byte CF_OP2 = 2; +static const Byte CF_OPMASK = 3; +static const Byte CF_BYTEMODE = 4; +static const Byte CF_JUMP = 8; +static const Byte CF_PROC = 16; +static const Byte CF_USEFLAGS = 32; +static const Byte CF_CHFLAGS = 64; + +static Byte kCmdFlags[]= +{ + /* CMD_MOV */ CF_OP2 | CF_BYTEMODE, + /* CMD_CMP */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_ADD */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_SUB */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_JZ */ CF_OP1 | CF_JUMP | CF_USEFLAGS, + /* CMD_JNZ */ CF_OP1 | CF_JUMP | CF_USEFLAGS, + /* CMD_INC */ CF_OP1 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_DEC */ CF_OP1 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_JMP */ CF_OP1 | CF_JUMP, + /* CMD_XOR */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_AND */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_OR */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_TEST */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_JS */ CF_OP1 | CF_JUMP | CF_USEFLAGS, + /* CMD_JNS */ CF_OP1 | CF_JUMP | CF_USEFLAGS, + /* CMD_JB */ CF_OP1 | CF_JUMP | CF_USEFLAGS, + /* CMD_JBE */ CF_OP1 | CF_JUMP | CF_USEFLAGS, + /* CMD_JA */ CF_OP1 | CF_JUMP | CF_USEFLAGS, + /* CMD_JAE */ CF_OP1 | CF_JUMP | CF_USEFLAGS, + /* CMD_PUSH */ CF_OP1, + /* CMD_POP */ CF_OP1, + /* CMD_CALL */ CF_OP1 | CF_PROC, + /* CMD_RET */ CF_OP0 | CF_PROC, + /* CMD_NOT */ CF_OP1 | CF_BYTEMODE, + /* CMD_SHL */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_SHR */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_SAR */ CF_OP2 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_NEG */ CF_OP1 | CF_BYTEMODE | CF_CHFLAGS, + /* CMD_PUSHA */ CF_OP0, + /* CMD_POPA */ CF_OP0, + /* CMD_PUSHF */ CF_OP0 | CF_USEFLAGS, + /* CMD_POPF */ CF_OP0 | CF_CHFLAGS, + /* CMD_MOVZX */ CF_OP2, + /* CMD_MOVSX */ CF_OP2, + /* CMD_XCHG */ CF_OP2 | CF_BYTEMODE, + /* CMD_MUL */ CF_OP2 | CF_BYTEMODE, + /* CMD_DIV */ CF_OP2 | CF_BYTEMODE, + /* CMD_ADC */ CF_OP2 | CF_BYTEMODE | CF_USEFLAGS | CF_CHFLAGS , + /* CMD_SBB */ CF_OP2 | CF_BYTEMODE | CF_USEFLAGS | CF_CHFLAGS , + /* CMD_PRINT */ CF_OP0 +}; + +CVm::CVm(): Mem(NULL) {} + +bool CVm::Create() +{ + if (Mem == NULL) + Mem = (Byte *)::MyAlloc(kSpaceSize + 4); + return (Mem != NULL); +} + +CVm::~CVm() +{ + ::MyFree(Mem); +} + +// CVm::Execute can change CProgram object: it clears progarm if VM returns error. + +bool CVm::Execute(CProgram *prg, const CProgramInitState *initState, + CBlockRef &outBlockRef, CRecordVector &outGlobalData) +{ + memcpy(R, initState->InitR, sizeof(initState->InitR)); + R[kStackRegIndex] = kSpaceSize; + R[kNumRegs] = 0; + Flags = 0; + + UInt32 globalSize = MyMin((UInt32)initState->GlobalData.Size(), kGlobalSize); + if (globalSize != 0) + memcpy(Mem + kGlobalOffset, &initState->GlobalData[0], globalSize); + UInt32 staticSize = MyMin((UInt32)prg->StaticData.Size(), kGlobalSize - globalSize); + if (staticSize != 0) + memcpy(Mem + kGlobalOffset + globalSize, &prg->StaticData[0], staticSize); + + bool res = true; + #ifdef RARVM_STANDARD_FILTERS + if (prg->StandardFilterIndex >= 0) + ExecuteStandardFilter(prg->StandardFilterIndex); + else + #endif + { + res = ExecuteCode(prg); + if (!res) + prg->Commands[0].OpCode = CMD_RET; + } + UInt32 newBlockPos = GetFixedGlobalValue32(NGlobalOffset::kBlockPos) & kSpaceMask; + UInt32 newBlockSize = GetFixedGlobalValue32(NGlobalOffset::kBlockSize) & kSpaceMask; + if (newBlockPos + newBlockSize >= kSpaceSize) + newBlockPos = newBlockSize = 0; + outBlockRef.Offset = newBlockPos; + outBlockRef.Size = newBlockSize; + + outGlobalData.Clear(); + UInt32 dataSize = GetFixedGlobalValue32(NGlobalOffset::kGlobalMemOutSize); + dataSize = MyMin(dataSize, kGlobalSize - kFixedGlobalSize); + if (dataSize != 0) + { + dataSize += kFixedGlobalSize; + outGlobalData.Reserve(dataSize); + for (UInt32 i = 0; i < dataSize; i++) + outGlobalData.Add(Mem[kGlobalOffset + i]); + } + return res; +} + + +#define SET_IP(IP) \ + if ((IP) >= numCommands) return true; \ + if (--maxOpCount <= 0) return false; \ + cmd = commands + (IP); + +#define GET_FLAG_S_B(res) (((res) & 0x80) ? FLAG_S : 0) +#define SET_IP_OP1 { UInt32 val = GetOperand32(&cmd->Op1); SET_IP(val); } +#define FLAGS_UPDATE_SZ Flags = res == 0 ? FLAG_Z : res & FLAG_S +#define FLAGS_UPDATE_SZ_B Flags = (res & 0xFF) == 0 ? FLAG_Z : GET_FLAG_S_B(res) + +UInt32 CVm::GetOperand32(const COperand *op) const +{ + switch(op->Type) + { + case OP_TYPE_REG: return R[op->Data]; + case OP_TYPE_REGMEM: return GetValue32(&Mem[(op->Base + R[op->Data]) & kSpaceMask]); + default: return op->Data; + } +} + +void CVm::SetOperand32(const COperand *op, UInt32 val) +{ + switch(op->Type) + { + case OP_TYPE_REG: R[op->Data] = val; return; + case OP_TYPE_REGMEM: SetValue32(&Mem[(op->Base + R[op->Data]) & kSpaceMask], val); return; + } +} + +Byte CVm::GetOperand8(const COperand *op) const +{ + switch(op->Type) + { + case OP_TYPE_REG: return (Byte)R[op->Data]; + case OP_TYPE_REGMEM: return Mem[(op->Base + R[op->Data]) & kSpaceMask];; + default: return (Byte)op->Data; + } +} + +void CVm::SetOperand8(const COperand *op, Byte val) +{ + switch(op->Type) + { + case OP_TYPE_REG: R[op->Data] = (R[op->Data] & 0xFFFFFF00) | val; return; + case OP_TYPE_REGMEM: Mem[(op->Base + R[op->Data]) & kSpaceMask] = val; return; + } +} + +UInt32 CVm::GetOperand(bool byteMode, const COperand *op) const +{ + if (byteMode) + return GetOperand8(op); + return GetOperand32(op); +} + +void CVm::SetOperand(bool byteMode, const COperand *op, UInt32 val) +{ + if (byteMode) + SetOperand8(op, (Byte)(val & 0xFF)); + else + SetOperand32(op, val); +} + +bool CVm::ExecuteCode(const CProgram *prg) +{ + Int32 maxOpCount = 25000000; + const CCommand *commands = &prg->Commands[0]; + const CCommand *cmd = commands; + UInt32 numCommands = prg->Commands.Size(); + for (;;) + { + switch(cmd->OpCode) + { + #ifndef RARVM_NO_VM + + case CMD_MOV: + SetOperand32(&cmd->Op1, GetOperand32(&cmd->Op2)); + break; + case CMD_MOVB: + SetOperand8(&cmd->Op1, GetOperand8(&cmd->Op2)); + break; + case CMD_CMP: + { + UInt32 v1 = GetOperand32(&cmd->Op1); + UInt32 res = v1 - GetOperand32(&cmd->Op2); + Flags = res == 0 ? FLAG_Z : (res > v1) | (res & FLAG_S); + } + break; + case CMD_CMPB: + { + Byte v1 = GetOperand8(&cmd->Op1); + Byte res = v1 - GetOperand8(&cmd->Op2); + res &= 0xFF; + Flags = res == 0 ? FLAG_Z : (res > v1) | GET_FLAG_S_B(res); + } + break; + case CMD_ADD: + { + UInt32 v1 = GetOperand32(&cmd->Op1); + UInt32 res = v1 + GetOperand32(&cmd->Op2); + SetOperand32(&cmd->Op1, res); + Flags = (res < v1) | (res == 0 ? FLAG_Z : (res & FLAG_S)); + } + break; + case CMD_ADDB: + { + Byte v1 = GetOperand8(&cmd->Op1); + Byte res = v1 + GetOperand8(&cmd->Op2); + res &= 0xFF; + SetOperand8(&cmd->Op1, (Byte)res); + Flags = (res < v1) | (res == 0 ? FLAG_Z : GET_FLAG_S_B(res)); + } + break; + case CMD_ADC: + { + UInt32 v1 = GetOperand(cmd->ByteMode, &cmd->Op1); + UInt32 FC = (Flags & FLAG_C); + UInt32 res = v1 + GetOperand(cmd->ByteMode, &cmd->Op2) + FC; + if (cmd->ByteMode) + res &= 0xFF; + SetOperand(cmd->ByteMode, &cmd->Op1, res); + Flags = (res < v1 || res == v1 && FC) | (res == 0 ? FLAG_Z : (res & FLAG_S)); + } + break; + case CMD_SUB: + { + UInt32 v1 = GetOperand32(&cmd->Op1); + UInt32 res = v1 - GetOperand32(&cmd->Op2); + SetOperand32(&cmd->Op1, res); + Flags = res == 0 ? FLAG_Z : (res > v1) | (res & FLAG_S); + } + break; + case CMD_SUBB: + { + UInt32 v1 = GetOperand8(&cmd->Op1); + UInt32 res = v1 - GetOperand8(&cmd->Op2); + SetOperand8(&cmd->Op1, (Byte)res); + Flags = res == 0 ? FLAG_Z : (res > v1) | (res & FLAG_S); + } + break; + case CMD_SBB: + { + UInt32 v1 = GetOperand(cmd->ByteMode, &cmd->Op1); + UInt32 FC = (Flags & FLAG_C); + UInt32 res = v1 - GetOperand(cmd->ByteMode, &cmd->Op2) - FC; + // Flags = res == 0 ? FLAG_Z : (res > v1 || res == v1 && FC) | (res & FLAG_S); + if (cmd->ByteMode) + res &= 0xFF; + SetOperand(cmd->ByteMode, &cmd->Op1, res); + Flags = (res > v1 || res == v1 && FC) | (res == 0 ? FLAG_Z : (res & FLAG_S)); + } + break; + case CMD_INC: + { + UInt32 res = GetOperand32(&cmd->Op1) + 1; + SetOperand32(&cmd->Op1, res); + FLAGS_UPDATE_SZ; + } + break; + case CMD_INCB: + { + Byte res = GetOperand8(&cmd->Op1) + 1; + SetOperand8(&cmd->Op1, res);; + FLAGS_UPDATE_SZ_B; + } + break; + case CMD_DEC: + { + UInt32 res = GetOperand32(&cmd->Op1) - 1; + SetOperand32(&cmd->Op1, res); + FLAGS_UPDATE_SZ; + } + break; + case CMD_DECB: + { + Byte res = GetOperand8(&cmd->Op1) - 1; + SetOperand8(&cmd->Op1, res);; + FLAGS_UPDATE_SZ_B; + } + break; + case CMD_XOR: + { + UInt32 res = GetOperand32(&cmd->Op1) ^ GetOperand32(&cmd->Op2); + SetOperand32(&cmd->Op1, res); + FLAGS_UPDATE_SZ; + } + break; + case CMD_XORB: + { + Byte res = GetOperand8(&cmd->Op1) ^ GetOperand8(&cmd->Op2); + SetOperand8(&cmd->Op1, res); + FLAGS_UPDATE_SZ_B; + } + break; + case CMD_AND: + { + UInt32 res = GetOperand32(&cmd->Op1) & GetOperand32(&cmd->Op2); + SetOperand32(&cmd->Op1, res); + FLAGS_UPDATE_SZ; + } + break; + case CMD_ANDB: + { + Byte res = GetOperand8(&cmd->Op1) & GetOperand8(&cmd->Op2); + SetOperand8(&cmd->Op1, res); + FLAGS_UPDATE_SZ_B; + } + break; + case CMD_OR: + { + UInt32 res = GetOperand32(&cmd->Op1) | GetOperand32(&cmd->Op2); + SetOperand32(&cmd->Op1, res); + FLAGS_UPDATE_SZ; + } + break; + case CMD_ORB: + { + Byte res = GetOperand8(&cmd->Op1) | GetOperand8(&cmd->Op2); + SetOperand8(&cmd->Op1, res); + FLAGS_UPDATE_SZ_B; + } + break; + case CMD_TEST: + { + UInt32 res = GetOperand32(&cmd->Op1) & GetOperand32(&cmd->Op2); + FLAGS_UPDATE_SZ; + } + break; + case CMD_TESTB: + { + Byte res = GetOperand8(&cmd->Op1) & GetOperand8(&cmd->Op2); + FLAGS_UPDATE_SZ_B; + } + break; + case CMD_NOT: + SetOperand(cmd->ByteMode, &cmd->Op1, ~GetOperand(cmd->ByteMode, &cmd->Op1)); + break; + case CMD_NEG: + { + UInt32 res = 0 - GetOperand32(&cmd->Op1); + SetOperand32(&cmd->Op1, res); + Flags = res == 0 ? FLAG_Z : FLAG_C | (res & FLAG_S); + } + break; + case CMD_NEGB: + { + Byte res = (Byte)(0 - GetOperand8(&cmd->Op1)); + SetOperand8(&cmd->Op1, res); + Flags = res == 0 ? FLAG_Z : FLAG_C | GET_FLAG_S_B(res); + } + break; + + case CMD_SHL: + { + UInt32 v1 = GetOperand32(&cmd->Op1); + int v2 = (int)GetOperand32(&cmd->Op2); + UInt32 res = v1 << v2; + SetOperand32(&cmd->Op1, res); + Flags = (res == 0 ? FLAG_Z : (res & FLAG_S)) | ((v1 << (v2 - 1)) & 0x80000000 ? FLAG_C : 0); + } + break; + case CMD_SHLB: + { + Byte v1 = GetOperand8(&cmd->Op1); + int v2 = (int)GetOperand8(&cmd->Op2); + Byte res = (Byte)(v1 << v2); + SetOperand8(&cmd->Op1, res); + Flags = (res == 0 ? FLAG_Z : GET_FLAG_S_B(res)) | ((v1 << (v2 - 1)) & 0x80 ? FLAG_C : 0); + } + break; + case CMD_SHR: + { + UInt32 v1 = GetOperand32(&cmd->Op1); + int v2 = (int)GetOperand32(&cmd->Op2); + UInt32 res = v1 >> v2; + SetOperand32(&cmd->Op1, res); + Flags = (res == 0 ? FLAG_Z : (res & FLAG_S)) | ((v1 >> (v2 - 1)) & FLAG_C); + } + break; + case CMD_SHRB: + { + Byte v1 = GetOperand8(&cmd->Op1); + int v2 = (int)GetOperand8(&cmd->Op2); + Byte res = (Byte)(v1 >> v2); + SetOperand8(&cmd->Op1, res); + Flags = (res == 0 ? FLAG_Z : GET_FLAG_S_B(res)) | ((v1 >> (v2 - 1)) & FLAG_C); + } + break; + case CMD_SAR: + { + UInt32 v1 = GetOperand32(&cmd->Op1); + int v2 = (int)GetOperand32(&cmd->Op2); + UInt32 res = UInt32(((Int32)v1) >> v2); + SetOperand32(&cmd->Op1, res); + Flags= (res == 0 ? FLAG_Z : (res & FLAG_S)) | ((v1 >> (v2 - 1)) & FLAG_C); + } + break; + case CMD_SARB: + { + Byte v1 = GetOperand8(&cmd->Op1); + int v2 = (int)GetOperand8(&cmd->Op2); + Byte res = (Byte)(((signed char)v1) >> v2); + SetOperand8(&cmd->Op1, res); + Flags= (res == 0 ? FLAG_Z : GET_FLAG_S_B(res)) | ((v1 >> (v2 - 1)) & FLAG_C); + } + break; + + case CMD_JMP: + SET_IP_OP1; + continue; + case CMD_JZ: + if ((Flags & FLAG_Z) != 0) + { + SET_IP_OP1; + continue; + } + break; + case CMD_JNZ: + if ((Flags & FLAG_Z) == 0) + { + SET_IP_OP1; + continue; + } + break; + case CMD_JS: + if ((Flags & FLAG_S) != 0) + { + SET_IP_OP1; + continue; + } + break; + case CMD_JNS: + if ((Flags & FLAG_S) == 0) + { + SET_IP_OP1; + continue; + } + break; + case CMD_JB: + if ((Flags & FLAG_C) != 0) + { + SET_IP_OP1; + continue; + } + break; + case CMD_JBE: + if ((Flags & (FLAG_C | FLAG_Z)) != 0) + { + SET_IP_OP1; + continue; + } + break; + case CMD_JA: + if ((Flags & (FLAG_C | FLAG_Z)) == 0) + { + SET_IP_OP1; + continue; + } + break; + case CMD_JAE: + if ((Flags & FLAG_C) == 0) + { + SET_IP_OP1; + continue; + } + break; + + case CMD_PUSH: + R[kStackRegIndex] -= 4; + SetValue32(&Mem[R[kStackRegIndex] & kSpaceMask], GetOperand32(&cmd->Op1)); + break; + case CMD_POP: + SetOperand32(&cmd->Op1, GetValue32(&Mem[R[kStackRegIndex] & kSpaceMask])); + R[kStackRegIndex] += 4; + break; + case CMD_CALL: + R[kStackRegIndex] -= 4; + SetValue32(&Mem[R[kStackRegIndex] & kSpaceMask], (UInt32)(cmd - commands + 1)); + SET_IP_OP1; + continue; + + case CMD_PUSHA: + { + for (UInt32 i = 0, SP = R[kStackRegIndex] - 4; i < kNumRegs; i++, SP -= 4) + SetValue32(&Mem[SP & kSpaceMask], R[i]); + R[kStackRegIndex] -= kNumRegs * 4; + } + break; + case CMD_POPA: + { + for (UInt32 i = 0, SP = R[kStackRegIndex]; i < kNumRegs; i++, SP += 4) + R[kStackRegIndex - i] = GetValue32(&Mem[SP & kSpaceMask]); + } + break; + case CMD_PUSHF: + R[kStackRegIndex] -= 4; + SetValue32(&Mem[R[kStackRegIndex]&kSpaceMask], Flags); + break; + case CMD_POPF: + Flags = GetValue32(&Mem[R[kStackRegIndex] & kSpaceMask]); + R[kStackRegIndex] += 4; + break; + + case CMD_MOVZX: + SetOperand32(&cmd->Op1, GetOperand8(&cmd->Op2)); + break; + case CMD_MOVSX: + SetOperand32(&cmd->Op1, (UInt32)(Int32)(signed char)GetOperand8(&cmd->Op2)); + break; + case CMD_XCHG: + { + UInt32 v1 = GetOperand(cmd->ByteMode, &cmd->Op1); + SetOperand(cmd->ByteMode, &cmd->Op1, GetOperand(cmd->ByteMode, &cmd->Op2)); + SetOperand(cmd->ByteMode, &cmd->Op2, v1); + } + break; + case CMD_MUL: + { + UInt32 res = GetOperand32(&cmd->Op1) * GetOperand32(&cmd->Op2); + SetOperand32(&cmd->Op1, res); + } + break; + case CMD_MULB: + { + Byte res = GetOperand8(&cmd->Op1) * GetOperand8(&cmd->Op2); + SetOperand8(&cmd->Op1, res); + } + break; + case CMD_DIV: + { + UInt32 divider = GetOperand(cmd->ByteMode, &cmd->Op2); + if (divider != 0) + { + UInt32 res = GetOperand(cmd->ByteMode, &cmd->Op1) / divider; + SetOperand(cmd->ByteMode, &cmd->Op1, res); + } + } + break; + + #endif + + case CMD_RET: + { + if (R[kStackRegIndex] >= kSpaceSize) + return true; + UInt32 ip = GetValue32(&Mem[R[kStackRegIndex] & kSpaceMask]); + SET_IP(ip); + R[kStackRegIndex] += 4; + continue; + } + case CMD_PRINT: + break; + } + cmd++; + --maxOpCount; + } +} + + +////////////////////////////////////////////////////// +// Read program + +UInt32 ReadEncodedUInt32(CMemBitDecoder &inp) +{ + switch(inp.ReadBits(2)) + { + case 0: + return inp.ReadBits(4); + case 1: + { + UInt32 v = inp.ReadBits(4); + if (v == 0) + return 0xFFFFFF00 | inp.ReadBits(8); + else + return (v << 4) | inp.ReadBits(4); + } + case 2: + return inp.ReadBits(16); + default: + return inp.ReadBits(32); + } +} + +void CVm::DecodeArg(CMemBitDecoder &inp, COperand &op, bool byteMode) +{ + if (inp.ReadBit()) + { + op.Type = OP_TYPE_REG; + op.Data = inp.ReadBits(kNumRegBits); + } + else if (inp.ReadBit() == 0) + { + op.Type = OP_TYPE_INT; + if (byteMode) + op.Data = inp.ReadBits(8); + else + op.Data = ReadEncodedUInt32(inp); + } + else + { + op.Type = OP_TYPE_REGMEM; + if (inp.ReadBit() == 0) + { + op.Data = inp.ReadBits(kNumRegBits); + op.Base = 0; + } + else + { + if (inp.ReadBit() == 0) + op.Data = inp.ReadBits(kNumRegBits); + else + op.Data = kNumRegs; + op.Base = ReadEncodedUInt32(inp); + } + } +} + +void CVm::ReadVmProgram(const Byte *code, UInt32 codeSize, CProgram *prg) +{ + CMemBitDecoder inp; + inp.Init(code, codeSize); + + prg->StaticData.Clear(); + if (inp.ReadBit()) + { + UInt32 dataSize = ReadEncodedUInt32(inp) + 1; + for (UInt32 i = 0; inp.Avail() && i < dataSize; i++) + prg->StaticData.Add((Byte)inp.ReadBits(8)); + } + while (inp.Avail()) + { + prg->Commands.Add(CCommand()); + CCommand *cmd = &prg->Commands.Back(); + if (inp.ReadBit() == 0) + cmd->OpCode = (ECommand)inp.ReadBits(3); + else + cmd->OpCode = (ECommand)(8 + inp.ReadBits(5)); + if (kCmdFlags[cmd->OpCode] & CF_BYTEMODE) + cmd->ByteMode = (inp.ReadBit()) ? true : false; + else + cmd->ByteMode = 0; + int opNum = (kCmdFlags[cmd->OpCode] & CF_OPMASK); + if (opNum > 0) + { + DecodeArg(inp, cmd->Op1, cmd->ByteMode); + if (opNum == 2) + DecodeArg(inp, cmd->Op2, cmd->ByteMode); + else + { + if (cmd->Op1.Type == OP_TYPE_INT && (kCmdFlags[cmd->OpCode] & (CF_JUMP | CF_PROC))) + { + int Distance = cmd->Op1.Data; + if (Distance >= 256) + Distance -= 256; + else + { + if (Distance >= 136) + Distance -= 264; + else if (Distance >= 16) + Distance -= 8; + else if (Distance >= 8) + Distance -= 16; + Distance += prg->Commands.Size() - 1; + } + cmd->Op1.Data = Distance; + } + } + } + if (cmd->ByteMode) + { + switch (cmd->OpCode) + { + case CMD_MOV: cmd->OpCode = CMD_MOVB; break; + case CMD_CMP: cmd->OpCode = CMD_CMPB; break; + case CMD_ADD: cmd->OpCode = CMD_ADDB; break; + case CMD_SUB: cmd->OpCode = CMD_SUBB; break; + case CMD_INC: cmd->OpCode = CMD_INCB; break; + case CMD_DEC: cmd->OpCode = CMD_DECB; break; + case CMD_XOR: cmd->OpCode = CMD_XORB; break; + case CMD_AND: cmd->OpCode = CMD_ANDB; break; + case CMD_OR: cmd->OpCode = CMD_ORB; break; + case CMD_TEST: cmd->OpCode = CMD_TESTB; break; + case CMD_NEG: cmd->OpCode = CMD_NEGB; break; + case CMD_SHL: cmd->OpCode = CMD_SHLB; break; + case CMD_SHR: cmd->OpCode = CMD_SHRB; break; + case CMD_SAR: cmd->OpCode = CMD_SARB; break; + case CMD_MUL: cmd->OpCode = CMD_MULB; break; + } + } + } +} + +#ifdef RARVM_STANDARD_FILTERS + +enum EStandardFilter +{ + SF_E8, + SF_E8E9, + SF_ITANIUM, + SF_RGB, + SF_AUDIO, + SF_DELTA, + SF_UPCASE +}; + +struct StandardFilterSignature +{ + UInt32 Length; + UInt32 CRC; + EStandardFilter Type; +} +kStdFilters[]= +{ + 53, 0xad576887, SF_E8, + 57, 0x3cd7e57e, SF_E8E9, + 120, 0x3769893f, SF_ITANIUM, + 29, 0x0e06077d, SF_DELTA, + 149, 0x1c2c5dc8, SF_RGB, + 216, 0xbc85e701, SF_AUDIO, + 40, 0x46b9c560, SF_UPCASE +}; + +static int FindStandardFilter(const Byte *code, UInt32 codeSize) +{ + UInt32 crc = CrcCalc(code, codeSize); + for (int i = 0; i < sizeof(kStdFilters) / sizeof(kStdFilters[0]); i++) + { + StandardFilterSignature &sfs = kStdFilters[i]; + if (sfs.CRC == crc && sfs.Length == codeSize) + return i; + } + return -1; +} + +#endif + +void CVm::PrepareProgram(const Byte *code, UInt32 codeSize, CProgram *prg) +{ + Byte xorSum = 0; + for (UInt32 i = 1; i < codeSize; i++) + xorSum ^= code[i]; + + prg->Commands.Clear(); + #ifdef RARVM_STANDARD_FILTERS + prg->StandardFilterIndex = -1; + #endif + + if (xorSum == code[0] && codeSize > 0) + { + #ifdef RARVM_STANDARD_FILTERS + prg->StandardFilterIndex = FindStandardFilter(code, codeSize); + if (prg->StandardFilterIndex >= 0) + return; + #endif + // 1 byte for checksum + ReadVmProgram(code + 1, codeSize - 1, prg); + } + prg->Commands.Add(CCommand()); + CCommand *cmd = &prg->Commands.Back(); + cmd->OpCode = CMD_RET; +} + +void CVm::SetMemory(UInt32 pos, const Byte *data, UInt32 dataSize) +{ + if (pos < kSpaceSize && data != Mem + pos) + memmove(Mem + pos, data, MyMin(dataSize, kSpaceSize - pos)); +} + +#ifdef RARVM_STANDARD_FILTERS + +static void E8E9Decode(Byte *data, UInt32 dataSize, UInt32 fileOffset, bool e9) +{ + if (dataSize <= 4) + return; + dataSize -= 4; + const UInt32 kFileSize = 0x1000000; + Byte cmpByte2 = (e9 ? 0xE9 : 0xE8); + for (UInt32 curPos = 0; curPos < dataSize;) + { + Byte curByte = *(data++); + curPos++; + if (curByte == 0xE8 || curByte == cmpByte2) + { + UInt32 offset = curPos + fileOffset; + UInt32 addr = (Int32)GetValue32(data); + if (addr < kFileSize) + SetValue32(data, addr - offset); + else if ((Int32)addr < 0 && (Int32)(addr + offset) >= 0) + SetValue32(data, addr + kFileSize); + data += 4; + curPos += 4; + } + } +} + +static inline UInt32 ItaniumGetOpType(const Byte *data, int bitPos) +{ + return (data[(unsigned int)bitPos >> 3] >> (bitPos & 7)) & 0xF; +} + + +static void ItaniumDecode(Byte *data, UInt32 dataSize, UInt32 fileOffset) +{ + UInt32 curPos = 0; + fileOffset >>= 4; + while (curPos < dataSize - 21) + { + int b = (data[0] & 0x1F) - 0x10; + if (b >= 0) + { + static Byte kCmdMasks[16] = {4,4,6,6,0,0,7,7,4,4,0,0,4,4,0,0}; + Byte cmdMask = kCmdMasks[b]; + if (cmdMask != 0) + for (int i = 0; i < 3; i++) + if (cmdMask & (1 << i)) + { + int startPos = i * 41 + 18; + if (ItaniumGetOpType(data, startPos + 24) == 5) + { + const UInt32 kMask = 0xFFFFF; + Byte *p = data + ((unsigned int)startPos >> 3); + UInt32 bitField = ((UInt32)p[0]) | ((UInt32)p[1] << 8) | ((UInt32)p[2] << 16); + int inBit = (startPos & 7); + UInt32 offset = (bitField >> inBit) & kMask; + UInt32 andMask = ~(kMask << inBit); + bitField = ((offset - fileOffset) & kMask) << inBit; + for (int j = 0; j < 3; j++) + { + p[j] &= andMask; + p[j] |= bitField; + andMask >>= 8; + bitField >>= 8; + } + } + } + } + data += 16; + curPos += 16; + fileOffset++; + } +} + +static void DeltaDecode(Byte *data, UInt32 dataSize, UInt32 numChannels) +{ + UInt32 srcPos = 0; + UInt32 border = dataSize * 2; + for (UInt32 curChannel = 0; curChannel < numChannels; curChannel++) + { + Byte prevByte = 0; + for (UInt32 destPos = dataSize + curChannel; destPos < border; destPos += numChannels) + data[destPos] = (prevByte = prevByte - data[srcPos++]); + } +} + +static void RgbDecode(Byte *srcData, UInt32 dataSize, UInt32 width, UInt32 posR) +{ + Byte *destData = srcData + dataSize; + const UInt32 numChannels = 3; + for (UInt32 curChannel = 0; curChannel < numChannels; curChannel++) + { + Byte prevByte = 0; + + for (UInt32 i = curChannel; i < dataSize; i+= numChannels) + { + unsigned int predicted; + if (i < width) + predicted = prevByte; + else + { + unsigned int upperLeftByte = destData[i - width]; + unsigned int upperByte = destData[i - width + 3]; + predicted = prevByte + upperByte - upperLeftByte; + int pa = abs((int)(predicted - prevByte)); + int pb = abs((int)(predicted - upperByte)); + int pc = abs((int)(predicted - upperLeftByte)); + if (pa <= pb && pa <= pc) + predicted = prevByte; + else + if (pb <= pc) + predicted = upperByte; + else + predicted = upperLeftByte; + } + destData[i] = prevByte = (Byte)(predicted - *(srcData++)); + } + } + if (dataSize < 3) + return; + for (UInt32 i = posR, border = dataSize - 2; i < border; i += 3) + { + Byte g = destData[i + 1]; + destData[i] = destData[i] + g; + destData[i + 2] = destData[i + 2] + g; + } +} + +static void AudioDecode(Byte *srcData, UInt32 dataSize, UInt32 numChannels) +{ + Byte *destData = srcData + dataSize; + for (UInt32 curChannel = 0; curChannel < numChannels; curChannel++) + { + UInt32 prevByte = 0, prevDelta = 0, dif[7]; + Int32 D1 = 0, D2 = 0, D3; + Int32 K1 = 0, K2 = 0, K3 = 0; + memset(dif, 0, sizeof(dif)); + + for (UInt32 i = curChannel, byteCount = 0; i < dataSize; i += numChannels, byteCount++) + { + D3 = D2; + D2 = prevDelta - D1; + D1 = prevDelta; + + UInt32 predicted = 8 * prevByte + K1 * D1 + K2 * D2 + K3 * D3; + predicted = (predicted >> 3) & 0xFF; + + UInt32 curByte = *(srcData++); + + predicted -= curByte; + destData[i] = (Byte)predicted; + prevDelta = (UInt32)(Int32)(signed char)(predicted - prevByte); + prevByte = predicted; + + Int32 D = ((Int32)(signed char)curByte) << 3; + + dif[0] += abs(D); + dif[1] += abs(D - D1); + dif[2] += abs(D + D1); + dif[3] += abs(D - D2); + dif[4] += abs(D + D2); + dif[5] += abs(D - D3); + dif[6] += abs(D + D3); + + if ((byteCount & 0x1F) == 0) + { + UInt32 minDif = dif[0], numMinDif = 0; + dif[0] = 0; + for (int j = 1; j < sizeof(dif) / sizeof(dif[0]); j++) + { + if (dif[j] < minDif) + { + minDif = dif[j]; + numMinDif = j; + } + dif[j] = 0; + } + switch (numMinDif) + { + case 1: if (K1 >= -16) K1--; break; + case 2: if (K1 < 16) K1++; break; + case 3: if (K2 >= -16) K2--; break; + case 4: if (K2 < 16) K2++; break; + case 5: if (K3 >= -16) K3--; break; + case 6: if (K3 < 16) K3++; break; + } + } + } + } +} + +static UInt32 UpCaseDecode(Byte *data, UInt32 dataSize) +{ + UInt32 srcPos = 0, destPos = dataSize; + while (srcPos < dataSize) + { + Byte curByte = data[srcPos++]; + if (curByte == 2 && (curByte = data[srcPos++]) != 2) + curByte -= 32; + data[destPos++] = curByte; + } + return destPos - dataSize; +} + +void CVm::ExecuteStandardFilter(int filterIndex) +{ + UInt32 dataSize = R[4]; + if (dataSize >= kGlobalOffset) + return; + EStandardFilter filterType = kStdFilters[filterIndex].Type; + + switch (filterType) + { + case SF_E8: + case SF_E8E9: + E8E9Decode(Mem, dataSize, R[6], (filterType == SF_E8E9)); + break; + case SF_ITANIUM: + ItaniumDecode(Mem, dataSize, R[6]); + break; + case SF_DELTA: + if (dataSize >= kGlobalOffset / 2) + break; + SetBlockPos(dataSize); + DeltaDecode(Mem, dataSize, R[0]); + break; + case SF_RGB: + if (dataSize >= kGlobalOffset / 2) + break; + { + UInt32 width = R[0]; + if (width <= 3) + break; + SetBlockPos(dataSize); + RgbDecode(Mem, dataSize, width, R[1]); + } + break; + case SF_AUDIO: + if (dataSize >= kGlobalOffset / 2) + break; + SetBlockPos(dataSize); + AudioDecode(Mem, dataSize, R[0]); + break; + case SF_UPCASE: + if (dataSize >= kGlobalOffset / 2) + break; + UInt32 destSize = UpCaseDecode(Mem, dataSize); + SetBlockSize(destSize); + SetBlockPos(dataSize); + break; + } +} + +#endif + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/Rar3Vm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar3Vm.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,179 @@ +// Rar3Vm.h +// According to unRAR license, this code may not be used to develop +// a program that creates RAR archives + +#ifndef __COMPRESS_RAR3_VM_H +#define __COMPRESS_RAR3_VM_H + +#include "../../../C/CpuArch.h" + +#include "Common/MyVector.h" +#include "Common/Types.h" + +#define RARVM_STANDARD_FILTERS + +namespace NCompress { +namespace NRar3 { + +class CMemBitDecoder +{ + const Byte *_data; + UInt32 _bitSize; + UInt32 _bitPos; +public: + void Init(const Byte *data, UInt32 byteSize) + { + _data = data; + _bitSize = (byteSize << 3); + _bitPos = 0; + } + UInt32 ReadBits(int numBits); + UInt32 ReadBit(); + bool Avail() const { return (_bitPos < _bitSize); } +}; + +namespace NVm { + +inline UInt32 GetValue32(const void *addr) { return GetUi32(addr); } +inline void SetValue32(void *addr, UInt32 value) { SetUi32(addr, value); } + +UInt32 ReadEncodedUInt32(CMemBitDecoder &inp); + +const int kNumRegBits = 3; +const UInt32 kNumRegs = 1 << kNumRegBits; +const UInt32 kNumGpRegs = kNumRegs - 1; + +const UInt32 kSpaceSize = 0x40000; +const UInt32 kSpaceMask = kSpaceSize -1; +const UInt32 kGlobalOffset = 0x3C000; +const UInt32 kGlobalSize = 0x2000; +const UInt32 kFixedGlobalSize = 64; + +namespace NGlobalOffset +{ + const UInt32 kBlockSize = 0x1C; + const UInt32 kBlockPos = 0x20; + const UInt32 kExecCount = 0x2C; + const UInt32 kGlobalMemOutSize = 0x30; +} + +enum ECommand +{ + CMD_MOV, CMD_CMP, CMD_ADD, CMD_SUB, CMD_JZ, CMD_JNZ, CMD_INC, CMD_DEC, + CMD_JMP, CMD_XOR, CMD_AND, CMD_OR, CMD_TEST, CMD_JS, CMD_JNS, CMD_JB, + CMD_JBE, CMD_JA, CMD_JAE, CMD_PUSH, CMD_POP, CMD_CALL, CMD_RET, CMD_NOT, + CMD_SHL, CMD_SHR, CMD_SAR, CMD_NEG, CMD_PUSHA,CMD_POPA, CMD_PUSHF,CMD_POPF, + CMD_MOVZX,CMD_MOVSX,CMD_XCHG, CMD_MUL, CMD_DIV, CMD_ADC, CMD_SBB, CMD_PRINT, + + CMD_MOVB, CMD_CMPB, CMD_ADDB, CMD_SUBB, CMD_INCB, CMD_DECB, + CMD_XORB, CMD_ANDB, CMD_ORB, CMD_TESTB,CMD_NEGB, + CMD_SHLB, CMD_SHRB, CMD_SARB, CMD_MULB +}; + +enum EOpType {OP_TYPE_REG, OP_TYPE_INT, OP_TYPE_REGMEM, OP_TYPE_NONE}; + +// Addr in COperand object can link (point) to CVm object!!! + +struct COperand +{ + EOpType Type; + UInt32 Data; + UInt32 Base; + COperand(): Type(OP_TYPE_NONE), Data(0), Base(0) {} +}; + +struct CCommand +{ + ECommand OpCode; + bool ByteMode; + COperand Op1, Op2; +}; + +struct CBlockRef +{ + UInt32 Offset; + UInt32 Size; +}; + +struct CProgram +{ + CRecordVector Commands; + #ifdef RARVM_STANDARD_FILTERS + int StandardFilterIndex; + #endif + CRecordVector StaticData; +}; + +struct CProgramInitState +{ + UInt32 InitR[kNumGpRegs]; + CRecordVector GlobalData; + + void AllocateEmptyFixedGlobal() + { + GlobalData.Clear(); + GlobalData.Reserve(NVm::kFixedGlobalSize); + for (UInt32 i = 0; i < NVm::kFixedGlobalSize; i++) + GlobalData.Add(0); + } +}; + +class CVm +{ + static UInt32 GetValue(bool byteMode, const void *addr) + { + if (byteMode) + return(*(const Byte *)addr); + else + return GetUi32(addr); + } + + static void SetValue(bool byteMode, void *addr, UInt32 value) + { + if (byteMode) + *(Byte *)addr = (Byte)value; + else + SetUi32(addr, value); + } + + UInt32 GetFixedGlobalValue32(UInt32 globalOffset) { return GetValue(false, &Mem[kGlobalOffset + globalOffset]); } + + void SetBlockSize(UInt32 v) { SetValue(&Mem[kGlobalOffset + NGlobalOffset::kBlockSize], v); } + void SetBlockPos(UInt32 v) { SetValue(&Mem[kGlobalOffset + NGlobalOffset::kBlockPos], v); } +public: + static void SetValue(void *addr, UInt32 value) { SetValue(false, addr, value); } +private: + UInt32 GetOperand32(const COperand *op) const; + void SetOperand32(const COperand *op, UInt32 val); + Byte GetOperand8(const COperand *op) const; + void SetOperand8(const COperand *op, Byte val); + UInt32 GetOperand(bool byteMode, const COperand *op) const; + void SetOperand(bool byteMode, const COperand *op, UInt32 val); + + void DecodeArg(CMemBitDecoder &inp, COperand &op, bool byteMode); + + bool ExecuteCode(const CProgram *prg); + + #ifdef RARVM_STANDARD_FILTERS + void ExecuteStandardFilter(int filterIndex); + #endif + + Byte *Mem; + UInt32 R[kNumRegs + 1]; // R[kNumRegs] = 0 always (speed optimization) + UInt32 Flags; + void ReadVmProgram(const Byte *code, UInt32 codeSize, CProgram *prg); +public: + CVm(); + ~CVm(); + bool Create(); + void PrepareProgram(const Byte *code, UInt32 codeSize, CProgram *prg); + void SetMemory(UInt32 pos, const Byte *data, UInt32 dataSize); + bool Execute(CProgram *prg, const CProgramInitState *initState, + CBlockRef &outBlockRef, CRecordVector &outGlobalData); + const Byte *GetDataPointer(UInt32 offset) const { return Mem + offset; } + +}; + +#endif + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/RarCodecsRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/RarCodecsRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,26 @@ +// RarCodecsRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" + +#include "Rar1Decoder.h" +#include "Rar2Decoder.h" +#include "Rar3Decoder.h" + +#define CREATE_CODEC(x) static void *CreateCodec ## x() { return (void *)(ICompressCoder *)(new NCompress::NRar ## x::CDecoder); } + +CREATE_CODEC(1) +CREATE_CODEC(2) +CREATE_CODEC(3) + +#define RAR_CODEC(x, name) { CreateCodec ## x, 0, 0x040300 + x, L"Rar" name, 1, false } + +static CCodecInfo g_CodecsInfo[] = +{ + RAR_CODEC(1, L"1"), + RAR_CODEC(2, L"2"), + RAR_CODEC(3, L"3"), +}; + +REGISTER_CODECS(Rar) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ShrinkDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ShrinkDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,148 @@ +// ShrinkDecoder.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/Alloc.h" +} + +#include "../Common/InBuffer.h" +#include "../Common/OutBuffer.h" + +#include "BitlDecoder.h" +#include "ShrinkDecoder.h" + +namespace NCompress { +namespace NShrink { + +static const UInt32 kBufferSize = (1 << 20); +static const int kNumMinBits = 9; + +HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress) +{ + NBitl::CBaseDecoder inBuffer; + COutBuffer outBuffer; + + if (!inBuffer.Create(kBufferSize)) + return E_OUTOFMEMORY; + inBuffer.SetStream(inStream); + inBuffer.Init(); + + if (!outBuffer.Create(kBufferSize)) + return E_OUTOFMEMORY; + outBuffer.SetStream(outStream); + outBuffer.Init(); + + UInt64 prevPos = 0; + int numBits = kNumMinBits; + UInt32 head = 257; + bool needPrev = false; + UInt32 lastSymbol = 0; + + int i; + for (i = 0; i < kNumItems; i++) + _parents[i] = 0; + for (i = 0; i < kNumItems; i++) + _suffixes[i] = 0; + for (i = 0; i < 257; i++) + _isFree[i] = false; + for (; i < kNumItems; i++) + _isFree[i] = true; + + for (;;) + { + UInt32 symbol = inBuffer.ReadBits(numBits); + if (inBuffer.ExtraBitsWereRead()) + break; + if (_isFree[symbol]) + return S_FALSE; + if (symbol == 256) + { + UInt32 symbol = inBuffer.ReadBits(numBits); + if (symbol == 1) + { + if (numBits < kNumMaxBits) + numBits++; + } + else if (symbol == 2) + { + if (needPrev) + _isFree[head - 1] = true; + for (i = 257; i < kNumItems; i++) + _isParent[i] = false; + for (i = 257; i < kNumItems; i++) + if (!_isFree[i]) + _isParent[_parents[i]] = true; + for (i = 257; i < kNumItems; i++) + if (!_isParent[i]) + _isFree[i] = true; + head = 257; + while (head < kNumItems && !_isFree[head]) + head++; + if (head < kNumItems) + { + needPrev = true; + _isFree[head] = false; + _parents[head] = (UInt16)lastSymbol; + head++; + } + } + else + return S_FALSE; + continue; + } + UInt32 cur = symbol; + i = 0; + int corectionIndex = -1; + while (cur >= 256) + { + if (cur == head - 1) + corectionIndex = i; + _stack[i++] = _suffixes[cur]; + cur = _parents[cur]; + } + _stack[i++] = (Byte)cur; + if (needPrev) + { + _suffixes[head - 1] = (Byte)cur; + if (corectionIndex >= 0) + _stack[corectionIndex] = (Byte)cur; + } + while (i > 0) + outBuffer.WriteByte((_stack[--i])); + while (head < kNumItems && !_isFree[head]) + head++; + if (head < kNumItems) + { + needPrev = true; + _isFree[head] = false; + _parents[head] = (UInt16)symbol; + head++; + } + else + needPrev = false; + lastSymbol = symbol; + + UInt64 nowPos = outBuffer.GetProcessedSize(); + if (progress != NULL && nowPos - prevPos > (1 << 18)) + { + prevPos = nowPos; + UInt64 packSize = inBuffer.GetProcessedSize(); + RINOK(progress->SetRatioInfo(&packSize, &nowPos)); + } + } + return outBuffer.Flush(); +} + +STDMETHODIMP CDecoder ::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + try { return CodeReal(inStream, outStream, inSize, outSize, progress); } + catch(const CInBufferException &e) { return e.ErrorCode; } + catch(const COutBufferException &e) { return e.ErrorCode; } + catch(...) { return S_FALSE; } +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ShrinkDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ShrinkDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,38 @@ +// ShrinkDecoder.h + +#ifndef __COMPRESS_SHRINK_DECODER_H +#define __COMPRESS_SHRINK_DECODER_H + +#include "../../Common/MyCom.h" + +#include "../ICoder.h" + +namespace NCompress { +namespace NShrink { + +const int kNumMaxBits = 13; +const UInt32 kNumItems = 1 << kNumMaxBits; + +class CDecoder : + public ICompressCoder, + public CMyUnknownImp +{ + UInt16 _parents[kNumItems]; + Byte _suffixes[kNumItems]; + Byte _stack[kNumItems]; + bool _isFree[kNumItems]; + bool _isParent[kNumItems]; + +public: + MY_UNKNOWN_IMP + + HRESULT CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ZlibDecoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ZlibDecoder.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,94 @@ +// ZlibDecoder.cpp + +#include "StdAfx.h" + +#include "../Common/StreamUtils.h" + +#include "DeflateDecoder.h" +#include "ZlibDecoder.h" + +namespace NCompress { +namespace NZlib { + +#define DEFLATE_TRY_BEGIN try { +#define DEFLATE_TRY_END } \ + catch(...) { return S_FALSE; } + +#define ADLER_MOD 65521 +#define ADLER_LOOP_MAX 5550 + +static UInt32 Adler32_Update(UInt32 adler, const Byte *buf, size_t size) +{ + UInt32 a = adler & 0xFFFF; + UInt32 b = (adler >> 16) & 0xFFFF; + while (size > 0) + { + unsigned curSize = (size > ADLER_LOOP_MAX) ? ADLER_LOOP_MAX : (unsigned )size; + unsigned i; + for (i = 0; i < curSize; i++) + { + a += buf[i]; + b += a; + } + buf += curSize; + size -= curSize; + a %= ADLER_MOD; + b %= ADLER_MOD; + } + return (b << 16) + a; +} + +STDMETHODIMP COutStreamWithAdler::Write(const void *data, UInt32 size, UInt32 *processedSize) +{ + HRESULT result = _stream->Write(data, size, &size); + _adler = Adler32_Update(_adler, (const Byte *)data, size); + if (processedSize != NULL) + *processedSize = size; + return result; +} + +STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress) +{ + DEFLATE_TRY_BEGIN + if (!AdlerStream) + { + AdlerSpec = new COutStreamWithAdler; + AdlerStream = AdlerSpec; + } + if (!DeflateDecoder) + { + DeflateDecoderSpec = new NCompress::NDeflate::NDecoder::CCOMCoder; + DeflateDecoderSpec->ZlibMode = true; + DeflateDecoder = DeflateDecoderSpec; + } + + Byte buf[2]; + RINOK(ReadStream_FALSE(inStream, buf, 2)); + int method = buf[0] & 0xF; + if (method != 8) + return S_FALSE; + // int dicSize = buf[0] >> 4; + if ((((UInt32)buf[0] << 8) + buf[1]) % 31 != 0) + return S_FALSE; + if ((buf[1] & 0x20) != 0) // dictPresent + return S_FALSE; + // int level = (buf[1] >> 6); + + AdlerSpec->SetStream(outStream); + AdlerSpec->Init(); + HRESULT res = DeflateDecoder->Code(inStream, AdlerStream, inSize, outSize, progress); + AdlerSpec->ReleaseStream(); + + if (res == S_OK) + { + const Byte *p = DeflateDecoderSpec->ZlibFooter; + UInt32 adler = ((UInt32)p[0] << 24) | ((UInt32)p[1] << 16) | ((UInt32)p[2] << 8) | p[3]; + if (adler != AdlerSpec->GetAdler()) + return S_FALSE; + } + return res; + DEFLATE_TRY_END +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/ZlibDecoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/ZlibDecoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +// ZlibDecoder.h + +#ifndef __ZLIB_DECODER_H +#define __ZLIB_DECODER_H + +#include "DeflateDecoder.h" + +namespace NCompress { +namespace NZlib { + +const UInt32 ADLER_INIT_VAL = 1; + +class COutStreamWithAdler: + public ISequentialOutStream, + public CMyUnknownImp +{ + CMyComPtr _stream; + UInt32 _adler; +public: + MY_UNKNOWN_IMP + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize); + void SetStream(ISequentialOutStream *stream) { _stream = stream; } + void ReleaseStream() { _stream.Release(); } + void Init() { _adler = ADLER_INIT_VAL; } + UInt32 GetAdler() const { return _adler; } +}; + +class CDecoder: + public ICompressCoder, + public CMyUnknownImp +{ + COutStreamWithAdler *AdlerSpec; + CMyComPtr AdlerStream; + + NCompress::NDeflate::NDecoder::CCOMCoder *DeflateDecoderSpec; + CMyComPtr DeflateDecoder; +public: + STDMETHOD(Code)(ISequentialInStream *inStream, ISequentialOutStream *outStream, + const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress); + + MY_UNKNOWN_IMP +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Compress/makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/makefile Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +DIRS = \ + Branch\~ \ + ByteSwap\~ \ + BZip2\~ \ + Copy\~ \ + Deflate\~ \ + LZMA\~ \ + PPMD\~ \ + Rar\~ \ + +all: $(DIRS) + +$(DIRS): +!include "../SubBuild.mak" diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/7zAes.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/7zAes.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,245 @@ +// 7zAes.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/Sha256.h" +} + +#include "Windows/Synchronization.h" +#include "../Common/StreamObjects.h" +#include "../Common/StreamUtils.h" +#include "7zAes.h" +#include "MyAes.h" + +#ifndef EXTRACT_ONLY +#include "RandGen.h" +#endif + +using namespace NWindows; + +namespace NCrypto { +namespace NSevenZ { + +bool CKeyInfo::IsEqualTo(const CKeyInfo &a) const +{ + if (SaltSize != a.SaltSize || NumCyclesPower != a.NumCyclesPower) + return false; + for (UInt32 i = 0; i < SaltSize; i++) + if (Salt[i] != a.Salt[i]) + return false; + return (Password == a.Password); +} + +void CKeyInfo::CalculateDigest() +{ + if (NumCyclesPower == 0x3F) + { + UInt32 pos; + for (pos = 0; pos < SaltSize; pos++) + Key[pos] = Salt[pos]; + for (UInt32 i = 0; i < Password.GetCapacity() && pos < kKeySize; i++) + Key[pos++] = Password[i]; + for (; pos < kKeySize; pos++) + Key[pos] = 0; + } + else + { + CSha256 sha; + Sha256_Init(&sha); + const UInt64 numRounds = UInt64(1) << (NumCyclesPower); + Byte temp[8] = { 0,0,0,0,0,0,0,0 }; + for (UInt64 round = 0; round < numRounds; round++) + { + Sha256_Update(&sha, Salt, (size_t)SaltSize); + Sha256_Update(&sha, Password, Password.GetCapacity()); + Sha256_Update(&sha, temp, 8); + for (int i = 0; i < 8; i++) + if (++(temp[i]) != 0) + break; + } + Sha256_Final(&sha, Key); + } +} + +bool CKeyInfoCache::Find(CKeyInfo &key) +{ + for (int i = 0; i < Keys.Size(); i++) + { + const CKeyInfo &cached = Keys[i]; + if (key.IsEqualTo(cached)) + { + for (int j = 0; j < kKeySize; j++) + key.Key[j] = cached.Key[j]; + if (i != 0) + { + Keys.Insert(0, cached); + Keys.Delete(i+1); + } + return true; + } + } + return false; +} + +void CKeyInfoCache::Add(CKeyInfo &key) +{ + if (Find(key)) + return; + if (Keys.Size() >= Size) + Keys.DeleteBack(); + Keys.Insert(0, key); +} + +static CKeyInfoCache g_GlobalKeyCache(32); +static NSynchronization::CCriticalSection g_GlobalKeyCacheCriticalSection; + +CBase::CBase(): + _cachedKeys(16), + _ivSize(0) +{ + for (int i = 0; i < sizeof(_iv); i++) + _iv[i] = 0; +} + +void CBase::CalculateDigest() +{ + NSynchronization::CCriticalSectionLock lock(g_GlobalKeyCacheCriticalSection); + if (_cachedKeys.Find(_key)) + g_GlobalKeyCache.Add(_key); + else + { + if (!g_GlobalKeyCache.Find(_key)) + { + _key.CalculateDigest(); + g_GlobalKeyCache.Add(_key); + } + _cachedKeys.Add(_key); + } +} + +#ifndef EXTRACT_ONLY + +/* +STDMETHODIMP CEncoder::ResetSalt() +{ + _key.SaltSize = 4; + g_RandomGenerator.Generate(_key.Salt, _key.SaltSize); + return S_OK; +} +*/ + +STDMETHODIMP CEncoder::ResetInitVector() +{ + _ivSize = 8; + g_RandomGenerator.Generate(_iv, (unsigned)_ivSize); + return S_OK; +} + +STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream) +{ + // _key.Init(); + for (UInt32 i = _ivSize; i < sizeof(_iv); i++) + _iv[i] = 0; + + UInt32 ivSize = _ivSize; + + // _key.NumCyclesPower = 0x3F; + _key.NumCyclesPower = 19; + + Byte firstByte = (Byte)(_key.NumCyclesPower | + (((_key.SaltSize == 0) ? 0 : 1) << 7) | + (((ivSize == 0) ? 0 : 1) << 6)); + RINOK(outStream->Write(&firstByte, 1, NULL)); + if (_key.SaltSize == 0 && ivSize == 0) + return S_OK; + Byte saltSizeSpec = (Byte)((_key.SaltSize == 0) ? 0 : (_key.SaltSize - 1)); + Byte ivSizeSpec = (Byte)((ivSize == 0) ? 0 : (ivSize - 1)); + Byte secondByte = (Byte)(((saltSizeSpec) << 4) | ivSizeSpec); + RINOK(outStream->Write(&secondByte, 1, NULL)); + if (_key.SaltSize > 0) + { + RINOK(WriteStream(outStream, _key.Salt, _key.SaltSize)); + } + if (ivSize > 0) + { + RINOK(WriteStream(outStream, _iv, ivSize)); + } + return S_OK; +} + +HRESULT CEncoder::CreateFilter() +{ + _aesFilter = new CAesCbcEncoder; + return S_OK; +} + +#endif + +STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) +{ + _key.Init(); + UInt32 i; + for (i = 0; i < sizeof(_iv); i++) + _iv[i] = 0; + if (size == 0) + return S_OK; + UInt32 pos = 0; + Byte firstByte = data[pos++]; + + _key.NumCyclesPower = firstByte & 0x3F; + if ((firstByte & 0xC0) == 0) + return S_OK; + _key.SaltSize = (firstByte >> 7) & 1; + UInt32 ivSize = (firstByte >> 6) & 1; + + if (pos >= size) + return E_INVALIDARG; + Byte secondByte = data[pos++]; + + _key.SaltSize += (secondByte >> 4); + ivSize += (secondByte & 0x0F); + + if (pos + _key.SaltSize + ivSize > size) + return E_INVALIDARG; + for (i = 0; i < _key.SaltSize; i++) + _key.Salt[i] = data[pos++]; + for (i = 0; i < ivSize; i++) + _iv[i] = data[pos++]; + return S_OK; +} + +STDMETHODIMP CBaseCoder::CryptoSetPassword(const Byte *data, UInt32 size) +{ + _key.Password.SetCapacity((size_t)size); + memcpy(_key.Password, data, (size_t)size); + return S_OK; +} + +STDMETHODIMP CBaseCoder::Init() +{ + CalculateDigest(); + if (_aesFilter == 0) + { + RINOK(CreateFilter()); + } + CMyComPtr cp; + RINOK(_aesFilter.QueryInterface(IID_ICryptoProperties, &cp)); + RINOK(cp->SetKey(_key.Key, sizeof(_key.Key))); + RINOK(cp->SetInitVector(_iv, sizeof(_iv))); + return S_OK; +} + +STDMETHODIMP_(UInt32) CBaseCoder::Filter(Byte *data, UInt32 size) +{ + return _aesFilter->Filter(data, size); +} + +HRESULT CDecoder::CreateFilter() +{ + _aesFilter = new CAesCbcDecoder; + return S_OK; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/7zAes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/7zAes.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,117 @@ +// 7zAes.h + +#ifndef __CRYPTO_7Z_AES_H +#define __CRYPTO_7Z_AES_H + +#include "Common/Buffer.h" +#include "Common/MyCom.h" +#include "Common/MyVector.h" + +#include "../ICoder.h" +#include "../IPassword.h" + +namespace NCrypto { +namespace NSevenZ { + +const int kKeySize = 32; + +class CKeyInfo +{ +public: + int NumCyclesPower; + UInt32 SaltSize; + Byte Salt[16]; + CByteBuffer Password; + Byte Key[kKeySize]; + + bool IsEqualTo(const CKeyInfo &a) const; + void CalculateDigest(); + + CKeyInfo() { Init(); } + void Init() + { + NumCyclesPower = 0; + SaltSize = 0; + for (int i = 0; i < sizeof(Salt); i++) + Salt[i] = 0; + } +}; + +class CKeyInfoCache +{ + int Size; + CObjectVector Keys; +public: + CKeyInfoCache(int size): Size(size) {} + bool Find(CKeyInfo &key); + // HRESULT Calculate(CKeyInfo &key); + void Add(CKeyInfo &key); +}; + +class CBase +{ + CKeyInfoCache _cachedKeys; +protected: + CKeyInfo _key; + Byte _iv[16]; + UInt32 _ivSize; + void CalculateDigest(); + CBase(); +}; + +class CBaseCoder: + public ICompressFilter, + public ICryptoSetPassword, + public CMyUnknownImp, + public CBase +{ +protected: + CMyComPtr _aesFilter; + + virtual HRESULT CreateFilter() = 0; + #ifndef CRYPTO_AES + HRESULT CreateFilterFromDLL(REFCLSID clsID); + #endif +public: + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); +}; + +#ifndef EXTRACT_ONLY + +class CEncoder: + public CBaseCoder, + public ICompressWriteCoderProperties, + // public ICryptoResetSalt, + public ICryptoResetInitVector +{ + virtual HRESULT CreateFilter(); +public: + MY_UNKNOWN_IMP3( + ICryptoSetPassword, + ICompressWriteCoderProperties, + // ICryptoResetSalt, + ICryptoResetInitVector) + STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream); + // STDMETHOD(ResetSalt)(); + STDMETHOD(ResetInitVector)(); +}; +#endif + +class CDecoder: + public CBaseCoder, + public ICompressSetDecoderProperties2 +{ + virtual HRESULT CreateFilter(); +public: + MY_UNKNOWN_IMP2( + ICryptoSetPassword, + ICompressSetDecoderProperties2) + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/7zAesRegister.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/7zAesRegister.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// 7zAesRegister.cpp + +#include "StdAfx.h" + +#include "../Common/RegisterCodec.h" +#include "7zAes.h" + +static void *CreateCodec() { return (void *)(ICompressFilter *)(new NCrypto::NSevenZ::CDecoder()); } +#ifndef EXTRACT_ONLY +static void *CreateCodecOut() { return (void *)(ICompressFilter *)(new NCrypto::NSevenZ::CEncoder()); } +#else +#define CreateCodecOut 0 +#endif + +static CCodecInfo g_CodecInfo = + { CreateCodec, CreateCodecOut, 0x06F10701, L"7zAES", 1, true }; + +REGISTER_CODEC(7zAES) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/HmacSha1.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/HmacSha1.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,109 @@ +// HmacSha1.cpp + +#include "StdAfx.h" + +#include "HmacSha1.h" + +namespace NCrypto { +namespace NSha1 { + +void CHmac::SetKey(const Byte *key, size_t keySize) +{ + Byte keyTemp[kBlockSize]; + size_t i; + for (i = 0; i < kBlockSize; i++) + keyTemp[i] = 0; + if(keySize > kBlockSize) + { + _sha.Init(); + _sha.Update(key, keySize); + _sha.Final(keyTemp); + keySize = kDigestSize; + } + else + for (i = 0; i < keySize; i++) + keyTemp[i] = key[i]; + for (i = 0; i < kBlockSize; i++) + keyTemp[i] ^= 0x36; + _sha.Init(); + _sha.Update(keyTemp, kBlockSize); + for (i = 0; i < kBlockSize; i++) + keyTemp[i] ^= 0x36 ^ 0x5C; + _sha2.Init(); + _sha2.Update(keyTemp, kBlockSize); +} + +void CHmac::Final(Byte *mac, size_t macSize) +{ + Byte digest[kDigestSize]; + _sha.Final(digest); + _sha2.Update(digest, kDigestSize); + _sha2.Final(digest); + for(size_t i = 0; i < macSize; i++) + mac[i] = digest[i]; +} + + +void CHmac32::SetKey(const Byte *key, size_t keySize) +{ + UInt32 keyTemp[kBlockSizeInWords]; + size_t i; + for (i = 0; i < kBlockSizeInWords; i++) + keyTemp[i] = 0; + if(keySize > kBlockSize) + { + CContext sha; + sha.Init(); + sha.Update(key, keySize); + Byte digest[kDigestSize]; + sha.Final(digest); + + for (int i = 0 ; i < kDigestSizeInWords; i++) + keyTemp[i] = + ((UInt32)(digest[i * 4 + 0]) << 24) | + ((UInt32)(digest[i * 4 + 1]) << 16) | + ((UInt32)(digest[i * 4 + 2]) << 8) | + ((UInt32)(digest[i * 4 + 3])); + keySize = kDigestSizeInWords; + } + else + for (size_t i = 0; i < keySize; i++) + keyTemp[i / 4] |= (key[i] << (24 - 8 * (i & 3))); + for (i = 0; i < kBlockSizeInWords; i++) + keyTemp[i] ^= 0x36363636; + _sha.Init(); + _sha.Update(keyTemp, kBlockSizeInWords); + for (i = 0; i < kBlockSizeInWords; i++) + keyTemp[i] ^= 0x36363636 ^ 0x5C5C5C5C; + _sha2.Init(); + _sha2.Update(keyTemp, kBlockSizeInWords); +} + +void CHmac32::Final(UInt32 *mac, size_t macSize) +{ + UInt32 digest[kDigestSizeInWords]; + _sha.Final(digest); + _sha2.Update(digest, kDigestSizeInWords); + _sha2.Final(digest); + for(size_t i = 0; i < macSize; i++) + mac[i] = digest[i]; +} + +void CHmac32::GetLoopXorDigest(UInt32 *mac, UInt32 numIteration) +{ + UInt32 block[kBlockSizeInWords]; + UInt32 block2[kBlockSizeInWords]; + _sha.PrepareBlock(block, kDigestSizeInWords); + _sha2.PrepareBlock(block2, kDigestSizeInWords); + for(unsigned int s = 0; s < kDigestSizeInWords; s++) + block[s] = mac[s]; + for(UInt32 i = 0; i < numIteration; i++) + { + _sha.GetBlockDigest(block, block2); + _sha2.GetBlockDigest(block2, block); + for (unsigned int s = 0; s < kDigestSizeInWords; s++) + mac[s] ^= block[s]; + } +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/HmacSha1.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/HmacSha1.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,39 @@ +// HmacSha1.h +// Implements HMAC-SHA-1 (RFC2104, FIPS-198) + +#ifndef __CRYPTO_HMAC_SHA1_H +#define __CRYPTO_HMAC_SHA1_H + +#include "Sha1.h" + +namespace NCrypto { +namespace NSha1 { + +// Use: SetKey(key, keySize); for () Update(data, size); Final(mac, macSize); + +class CHmac +{ + CContext _sha; + CContext _sha2; +public: + void SetKey(const Byte *key, size_t keySize); + void Update(const Byte *data, size_t dataSize) { _sha.Update(data, dataSize); } + void Final(Byte *mac, size_t macSize = kDigestSize); +}; + +class CHmac32 +{ + CContext32 _sha; + CContext32 _sha2; +public: + void SetKey(const Byte *key, size_t keySize); + void Update(const UInt32 *data, size_t dataSize) { _sha.Update(data, dataSize); } + void Final(UInt32 *mac, size_t macSize = kDigestSizeInWords); + + // It'sa for hmac function. in,out: mac[kDigestSizeInWords]. + void GetLoopXorDigest(UInt32 *mac, UInt32 numIteration); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/MyAes.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/MyAes.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,57 @@ +// Crypto/MyAes.cpp + +#include "StdAfx.h" + +#include "MyAes.h" + +namespace NCrypto { + +struct CAesTabInit { CAesTabInit() { AesGenTables();} } g_AesTabInit; + +STDMETHODIMP CAesCbcEncoder::Init() { return S_OK; } + +STDMETHODIMP_(UInt32) CAesCbcEncoder::Filter(Byte *data, UInt32 size) +{ + return (UInt32)AesCbc_Encode(&Aes, data, size); +} + +STDMETHODIMP CAesCbcEncoder::SetKey(const Byte *data, UInt32 size) +{ + if ((size & 0x7) != 0 || size < 16 || size > 32) + return E_INVALIDARG; + Aes_SetKeyEncode(&Aes.aes, data, size); + return S_OK; +} + +STDMETHODIMP CAesCbcEncoder::SetInitVector(const Byte *data, UInt32 size) +{ + if (size != AES_BLOCK_SIZE) + return E_INVALIDARG; + AesCbc_Init(&Aes, data); + return S_OK; +} + +STDMETHODIMP CAesCbcDecoder::Init() { return S_OK; } + +STDMETHODIMP_(UInt32) CAesCbcDecoder::Filter(Byte *data, UInt32 size) +{ + return (UInt32)AesCbc_Decode(&Aes, data, size); +} + +STDMETHODIMP CAesCbcDecoder::SetKey(const Byte *data, UInt32 size) +{ + if ((size & 0x7) != 0 || size < 16 || size > 32) + return E_INVALIDARG; + Aes_SetKeyDecode(&Aes.aes, data, size); + return S_OK; +} + +STDMETHODIMP CAesCbcDecoder::SetInitVector(const Byte *data, UInt32 size) +{ + if (size != AES_BLOCK_SIZE) + return E_INVALIDARG; + AesCbc_Init(&Aes, data); + return S_OK; +} + +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/MyAes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/MyAes.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,48 @@ +// Crypto/MyAes.h + +#ifndef __CRYPTO_MY_AES_H +#define __CRYPTO_MY_AES_H + +extern "C" +{ +#include "../../../C/Aes.h" +} + +#include "../../Common/MyCom.h" +#include "../../Common/Types.h" + +#include "../ICoder.h" + +namespace NCrypto { + +class CAesCbcEncoder: + public ICompressFilter, + public ICryptoProperties, + public CMyUnknownImp +{ + CAesCbc Aes; +public: + MY_UNKNOWN_IMP1(ICryptoProperties) + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + STDMETHOD(SetKey)(const Byte *data, UInt32 size); + STDMETHOD(SetInitVector)(const Byte *data, UInt32 size); +}; + +class CAesCbcDecoder: + public ICompressFilter, + public ICryptoProperties, + public CMyUnknownImp +{ + CAesCbc Aes; +public: + MY_UNKNOWN_IMP1(ICryptoProperties) + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + STDMETHOD(SetKey)(const Byte *data, UInt32 size); + STDMETHOD(SetInitVector)(const Byte *data, UInt32 size); +}; + +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/Pbkdf2HmacSha1.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/Pbkdf2HmacSha1.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,83 @@ +// Pbkdf2HmacSha1.cpp + +#include "StdAfx.h" + +#include "HmacSha1.h" + +namespace NCrypto { +namespace NSha1 { + +void Pbkdf2Hmac(const Byte *pwd, size_t pwdSize, const Byte *salt, size_t saltSize, + UInt32 numIterations, Byte *key, size_t keySize) +{ + CHmac baseCtx; + baseCtx.SetKey(pwd, pwdSize); + for (UInt32 i = 1; keySize > 0; i++) + { + CHmac ctx = baseCtx; + ctx.Update(salt, saltSize); + Byte u[kDigestSize] = { (Byte)(i >> 24), (Byte)(i >> 16), (Byte)(i >> 8), (Byte)(i) }; + const unsigned int curSize = (keySize < kDigestSize) ? (unsigned int)keySize : kDigestSize; + ctx.Update(u, 4); + ctx.Final(u, kDigestSize); + + unsigned int s; + for (s = 0; s < curSize; s++) + key[s] = u[s]; + + for (UInt32 j = numIterations; j > 1; j--) + { + ctx = baseCtx; + ctx.Update(u, kDigestSize); + ctx.Final(u, kDigestSize); + for (s = 0; s < curSize; s++) + key[s] ^= u[s]; + } + + key += curSize; + keySize -= curSize; + } +} + +void Pbkdf2Hmac32(const Byte *pwd, size_t pwdSize, const UInt32 *salt, size_t saltSize, + UInt32 numIterations, UInt32 *key, size_t keySize) +{ + CHmac32 baseCtx; + baseCtx.SetKey(pwd, pwdSize); + for (UInt32 i = 1; keySize > 0; i++) + { + CHmac32 ctx = baseCtx; + ctx.Update(salt, saltSize); + UInt32 u[kDigestSizeInWords] = { i }; + const unsigned int curSize = (keySize < kDigestSizeInWords) ? (unsigned int)keySize : kDigestSizeInWords; + ctx.Update(u, 1); + ctx.Final(u, kDigestSizeInWords); + + // Speed-optimized code start + ctx = baseCtx; + ctx.GetLoopXorDigest(u, numIterations - 1); + // Speed-optimized code end + + unsigned int s; + for (s = 0; s < curSize; s++) + key[s] = u[s]; + + /* + // Default code start + for (UInt32 j = numIterations; j > 1; j--) + { + ctx = baseCtx; + ctx.Update(u, kDigestSizeInWords); + ctx.Final(u, kDigestSizeInWords); + for (s = 0; s < curSize; s++) + key[s] ^= u[s]; + } + // Default code end + */ + + key += curSize; + keySize -= curSize; + } +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/Pbkdf2HmacSha1.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/Pbkdf2HmacSha1.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,21 @@ +// Pbkdf2HmacSha1.h +// Password-Based Key Derivation Function (RFC 2898, PKCS #5) based on HMAC-SHA-1 + +#ifndef __CRYPTO_PBKDF2_HMAC_SHA1_H +#define __CRYPTO_PBKDF2_HMAC_SHA1_H + +#include +#include "../../Common/Types.h" + +namespace NCrypto { +namespace NSha1 { + +void Pbkdf2Hmac(const Byte *pwd, size_t pwdSize, const Byte *salt, size_t saltSize, + UInt32 numIterations, Byte *key, size_t keySize); + +void Pbkdf2Hmac32(const Byte *pwd, size_t pwdSize, const UInt32 *salt, size_t saltSize, + UInt32 numIterations, UInt32 *key, size_t keySize); + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/RandGen.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/RandGen.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,107 @@ +// RandGen.cpp + +#include "StdAfx.h" + +#include +#include "Windows/Synchronization.h" +#include "RandGen.h" + +#ifndef _WIN32 +#include +#define USE_POSIX_TIME +#define USE_POSIX_TIME2 +#endif + +#ifdef USE_POSIX_TIME +#include +#ifdef USE_POSIX_TIME2 +#include +#endif +#endif + +// This is not very good random number generator. +// Please use it only for salt. +// First generated data block depends from timer and processID. +// Other generated data blocks depend from previous state +// Maybe it's possible to restore original timer value from generated value. + +void CRandomGenerator::Init() +{ + NCrypto::NSha1::CContext hash; + hash.Init(); + + #ifdef _WIN32 + DWORD w = ::GetCurrentProcessId(); + hash.Update((const Byte *)&w, sizeof(w)); + w = ::GetCurrentThreadId(); + hash.Update((const Byte *)&w, sizeof(w)); + #else + pid_t pid = getpid(); + hash.Update((const Byte *)&pid, sizeof(pid)); + pid = getppid(); + hash.Update((const Byte *)&pid, sizeof(pid)); + #endif + + for (int i = 0; i < 1000; i++) + { + #ifdef _WIN32 + LARGE_INTEGER v; + if (::QueryPerformanceCounter(&v)) + hash.Update((const Byte *)&v.QuadPart, sizeof(v.QuadPart)); + #endif + + #ifdef USE_POSIX_TIME + #ifdef USE_POSIX_TIME2 + timeval v; + if (gettimeofday(&v, 0) == 0) + { + hash.Update((const Byte *)&v.tv_sec, sizeof(v.tv_sec)); + hash.Update((const Byte *)&v.tv_usec, sizeof(v.tv_usec)); + } + #endif + time_t v2 = time(NULL); + hash.Update((const Byte *)&v2, sizeof(v2)); + #endif + + DWORD tickCount = ::GetTickCount(); + hash.Update((const Byte *)&tickCount, sizeof(tickCount)); + + for (int j = 0; j < 100; j++) + { + hash.Final(_buff); + hash.Init(); + hash.Update(_buff, NCrypto::NSha1::kDigestSize); + } + } + hash.Final(_buff); + _needInit = false; +} + +static NWindows::NSynchronization::CCriticalSection g_CriticalSection; + +void CRandomGenerator::Generate(Byte *data, unsigned int size) +{ + g_CriticalSection.Enter(); + if (_needInit) + Init(); + while (size > 0) + { + NCrypto::NSha1::CContext hash; + + hash.Init(); + hash.Update(_buff, NCrypto::NSha1::kDigestSize); + hash.Final(_buff); + + hash.Init(); + UInt32 salt = 0xF672ABD1; + hash.Update((const Byte *)&salt, sizeof(salt)); + hash.Update(_buff, NCrypto::NSha1::kDigestSize); + Byte buff[NCrypto::NSha1::kDigestSize]; + hash.Final(buff); + for (unsigned int i = 0; i < NCrypto::NSha1::kDigestSize && size > 0; i++, size--) + *data++ = buff[i]; + } + g_CriticalSection.Leave(); +} + +CRandomGenerator g_RandomGenerator; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/RandGen.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/RandGen.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,21 @@ +// RandGen.h + +#ifndef __CRYPTO_RAND_GEN_H +#define __CRYPTO_RAND_GEN_H + +#include "Sha1.h" + +class CRandomGenerator +{ + Byte _buff[NCrypto::NSha1::kDigestSize]; + bool _needInit; + + void Init(); +public: + CRandomGenerator(): _needInit(true) {}; + void Generate(Byte *data, unsigned size); +}; + +extern CRandomGenerator g_RandomGenerator; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/Rar20Crypto.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/Rar20Crypto.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,136 @@ +// Crypto/Rar20Crypto.cpp + +#include "StdAfx.h" + +extern "C" +{ + #include "../../../C/7zCrc.h" + #include "../../../C/CpuArch.h" + #include "../../../C/RotateDefs.h" +} + +#include "Rar20Crypto.h" + +namespace NCrypto { +namespace NRar20 { + +static const int kNumRounds = 32; + +static const Byte InitSubstTable[256] = { + 215, 19,149, 35, 73,197,192,205,249, 28, 16,119, 48,221, 2, 42, + 232, 1,177,233, 14, 88,219, 25,223,195,244, 90, 87,239,153,137, + 255,199,147, 70, 92, 66,246, 13,216, 40, 62, 29,217,230, 86, 6, + 71, 24,171,196,101,113,218,123, 93, 91,163,178,202, 67, 44,235, + 107,250, 75,234, 49,167,125,211, 83,114,157,144, 32,193,143, 36, + 158,124,247,187, 89,214,141, 47,121,228, 61,130,213,194,174,251, + 97,110, 54,229,115, 57,152, 94,105,243,212, 55,209,245, 63, 11, + 164,200, 31,156, 81,176,227, 21, 76, 99,139,188,127, 17,248, 51, + 207,120,189,210, 8,226, 41, 72,183,203,135,165,166, 60, 98, 7, + 122, 38,155,170, 69,172,252,238, 39,134, 59,128,236, 27,240, 80, + 131, 3, 85,206,145, 79,154,142,159,220,201,133, 74, 64, 20,129, + 224,185,138,103,173,182, 43, 34,254, 82,198,151,231,180, 58, 10, + 118, 26,102, 12, 50,132, 22,191,136,111,162,179, 45, 4,148,108, + 161, 56, 78,126,242,222, 15,175,146, 23, 33,241,181,190, 77,225, + 0, 46,169,186, 68, 95,237, 65, 53,208,253,168, 9, 18,100, 52, + 116,184,160, 96,109, 37, 30,106,140,104,150, 5,204,117,112, 84 +}; + +void CData::UpdateKeys(const Byte *data) +{ + for (int i = 0; i < 16; i += 4) + for (int j = 0; j < 4; j++) + Keys[j] ^= g_CrcTable[data[i + j]]; +} + +static void Swap(Byte *b1, Byte *b2) +{ + Byte b = *b1; + *b1 = *b2; + *b2 = b; +} + +void CData::SetPassword(const Byte *password, UInt32 passwordLen) +{ + Keys[0] = 0xD3A3B879L; + Keys[1] = 0x3F6D12F7L; + Keys[2] = 0x7515A235L; + Keys[3] = 0xA4E7F123L; + + Byte psw[256]; + memset(psw, 0, sizeof(psw)); + memcpy(psw, password, passwordLen); + memcpy(SubstTable, InitSubstTable, sizeof(SubstTable)); + + for (UInt32 j = 0; j < 256; j++) + for (UInt32 i = 0; i < passwordLen; i += 2) + { + UInt32 n2 = (Byte)g_CrcTable[(psw[i + 1] + j) & 0xFF]; + UInt32 n1 = (Byte)g_CrcTable[(psw[i] - j) & 0xFF]; + for (UInt32 k = 1; (n1 & 0xFF) != n2; n1++, k++) + Swap(&SubstTable[n1 & 0xFF], &SubstTable[(n1 + i + k) & 0xFF]); + } + for (UInt32 i = 0; i < passwordLen; i+= 16) + EncryptBlock(&psw[i]); +} + +void CData::CryptBlock(Byte *buf, bool encrypt) +{ + Byte inBuf[16]; + UInt32 A, B, C, D, T, TA, TB; + + A = GetUi32(buf + 0) ^ Keys[0]; + B = GetUi32(buf + 4) ^ Keys[1]; + C = GetUi32(buf + 8) ^ Keys[2]; + D = GetUi32(buf + 12) ^ Keys[3]; + + if (!encrypt) + memcpy(inBuf, buf, sizeof(inBuf)); + + for (int i = 0; i < kNumRounds; i++) + { + UInt32 key = Keys[(encrypt ? i : (kNumRounds - 1 - i)) & 3]; + T = ((C + rotlFixed(D, 11)) ^ key); + TA = A ^ SubstLong(T); + T = ((D ^ rotlFixed(C, 17)) + key); + TB = B ^ SubstLong(T); + A = C; + B = D; + C = TA; + D = TB; + } + + SetUi32(buf + 0, C ^ Keys[0]); + SetUi32(buf + 4, D ^ Keys[1]); + SetUi32(buf + 8, A ^ Keys[2]); + SetUi32(buf + 12, B ^ Keys[3]); + + UpdateKeys(encrypt ? buf : inBuf); +} + +STDMETHODIMP CDecoder::CryptoSetPassword(const Byte *data, UInt32 size) +{ + _cipher.SetPassword(data, size); + return S_OK; +} + +STDMETHODIMP CDecoder::Init() +{ + return S_OK; +} + +static const UInt32 kBlockSize = 16; + +STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size) +{ + if (size == 0) + return 0; + if (size < kBlockSize) + return kBlockSize; + UInt32 i; + size -= kBlockSize; + for (i = 0; i <= size; i += kBlockSize) + _cipher.DecryptBlock(data + i); + return i; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/Rar20Crypto.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/Rar20Crypto.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,50 @@ +// Crypto/Rar20Crypto.h + +#ifndef __CRYPTO_RAR20_CRYPTO_H +#define __CRYPTO_RAR20_CRYPTO_H + +#include "Common/MyCom.h" + +#include "../ICoder.h" +#include "../IPassword.h" + +namespace NCrypto { +namespace NRar20 { + +class CData +{ + Byte SubstTable[256]; + UInt32 Keys[4]; + + UInt32 SubstLong(UInt32 t) + { + return (UInt32)SubstTable[(int)t & 255] | + ((UInt32)SubstTable[(int)(t >> 8) & 255] << 8) | + ((UInt32)SubstTable[(int)(t >> 16) & 255] << 16) | + ((UInt32)SubstTable[(int)(t >> 24) & 255] << 24); + } + void UpdateKeys(const Byte *data); + void CryptBlock(Byte *buf, bool encrypt); +public: + void EncryptBlock(Byte *buf) { CryptBlock(buf, true); } + void DecryptBlock(Byte *buf) { CryptBlock(buf, false); } + void SetPassword(const Byte *password, UInt32 passwordLen); +}; + +class CDecoder: + public ICompressFilter, + public ICryptoSetPassword, + public CMyUnknownImp +{ + CData _cipher; +public: + MY_UNKNOWN_IMP1(ICryptoSetPassword) + + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/RarAes.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/RarAes.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,139 @@ +// Crypto/RarAes.cpp +// Note: you must include MyAes.cpp to project to initialize AES tables + +#include "StdAfx.h" + +#include "RarAes.h" +#include "Sha1.h" + +namespace NCrypto { +namespace NRar29 { + +CDecoder::CDecoder(): + _thereIsSalt(false), + _needCalculate(true), + _rar350Mode(false) +{ + for (int i = 0; i < sizeof(_salt); i++) + _salt[i] = 0; +} + +STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) +{ + bool thereIsSaltPrev = _thereIsSalt; + _thereIsSalt = false; + if (size == 0) + return S_OK; + if (size < 8) + return E_INVALIDARG; + _thereIsSalt = true; + bool same = false; + if (_thereIsSalt == thereIsSaltPrev) + { + same = true; + if (_thereIsSalt) + { + for (int i = 0; i < sizeof(_salt); i++) + if (_salt[i] != data[i]) + { + same = false; + break; + } + } + } + for (int i = 0; i < sizeof(_salt); i++) + _salt[i] = data[i]; + if (!_needCalculate && !same) + _needCalculate = true; + return S_OK; +} + +static const int kMaxPasswordLength = 127 * 2; + +STDMETHODIMP CDecoder::CryptoSetPassword(const Byte *data, UInt32 size) +{ + if (size > kMaxPasswordLength) + size = kMaxPasswordLength; + bool same = false; + if (size == buffer.GetCapacity()) + { + same = true; + for (UInt32 i = 0; i < size; i++) + if (data[i] != buffer[i]) + { + same = false; + break; + } + } + if (!_needCalculate && !same) + _needCalculate = true; + buffer.SetCapacity(size); + memcpy(buffer, data, size); + return S_OK; +} + +STDMETHODIMP CDecoder::Init() +{ + Calculate(); + Aes_SetKeyDecode(&Aes.aes, aesKey, kRarAesKeySize); + AesCbc_Init(&Aes, aesInit); + return S_OK; +} + +STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size) +{ + return (UInt32)AesCbc_Decode(&Aes, data, size); +} + +void CDecoder::Calculate() +{ + if (_needCalculate) + { + const int kSaltSize = 8; + + Byte rawPassword[kMaxPasswordLength + kSaltSize]; + + memcpy(rawPassword, buffer, buffer.GetCapacity()); + + size_t rawLength = buffer.GetCapacity(); + + if (_thereIsSalt) + { + memcpy(rawPassword + rawLength, _salt, kSaltSize); + rawLength += kSaltSize; + } + + NSha1::CContext sha; + sha.Init(); + + // seems rar reverts hash for sha. + const int hashRounds = 0x40000; + int i; + for (i = 0; i < hashRounds; i++) + { + sha.Update(rawPassword, rawLength, _rar350Mode); + Byte pswNum[3] = { (Byte)i, (Byte)(i >> 8), (Byte)(i >> 16) }; + sha.Update(pswNum, 3, _rar350Mode); + if (i % (hashRounds / 16) == 0) + { + NSha1::CContext shaTemp = sha; + Byte digest[NSha1::kDigestSize]; + shaTemp.Final(digest); + aesInit[i / (hashRounds / 16)] = (Byte)digest[4 * 4 + 3]; + } + } + /* + // it's test message for sha + const char *message = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; + sha.Update((const Byte *)message, strlen(message)); + */ + Byte digest[20]; + sha.Final(digest); + for (i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + aesKey[i * 4 + j] = (digest[i * 4 + 3 - j]); + } + _needCalculate = false; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/RarAes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/RarAes.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,62 @@ +// Crypto/RarAes.h + +#ifndef __CRYPTO_RAR_AES_H +#define __CRYPTO_RAR_AES_H + +extern "C" +{ +#include "../../../C/Aes.h" +} + +#include "Common/Buffer.h" +#include "Common/MyCom.h" + +#include "../ICoder.h" +#include "../IPassword.h" + +namespace NCrypto { +namespace NRar29 { + +const UInt32 kRarAesKeySize = 16; + +class CDecoder: + public ICompressFilter, + public ICompressSetDecoderProperties2, + public ICryptoSetPassword, + public CMyUnknownImp +{ + Byte _salt[8]; + bool _thereIsSalt; + CByteBuffer buffer; + Byte aesKey[kRarAesKeySize]; + Byte aesInit[AES_BLOCK_SIZE]; + bool _needCalculate; + + CAesCbc Aes; + + bool _rar350Mode; + + void Calculate(); + +public: + + MY_UNKNOWN_IMP2( + ICryptoSetPassword, + ICompressSetDecoderProperties2) + + STDMETHOD(Init)(); + + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + + STDMETHOD(CryptoSetPassword)(const Byte *aData, UInt32 aSize); + + // ICompressSetDecoderProperties + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); + + CDecoder(); + void SetRar350Mode(bool rar350Mode) { _rar350Mode = rar350Mode; } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/Sha1.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/Sha1.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,213 @@ +// Crypto/Sha1.cpp +// This file is based on public domain +// Steve Reid and Wei Dai's code from Crypto++ + +#include "StdAfx.h" + +#include "Sha1.h" +extern "C" +{ +#include "../../../C/RotateDefs.h" +} + +namespace NCrypto { +namespace NSha1 { + +// define it for speed optimization +// #define _SHA1_UNROLL + +static const unsigned kNumW = + #ifdef _SHA1_UNROLL + 16; + #else + 80; + #endif + + +#define w0(i) (W[(i)] = data[(i)]) + +#ifdef _SHA1_UNROLL +#define w1(i) (W[(i)&15] = rotlFixed(W[((i)-3)&15] ^ W[((i)-8)&15] ^ W[((i)-14)&15] ^ W[((i)-16)&15], 1)) +#else +#define w1(i) (W[(i)] = rotlFixed(W[(i)-3] ^ W[(i)-8] ^ W[(i)-14] ^ W[(i)-16], 1)) +#endif + +#define f1(x,y,z) (z^(x&(y^z))) +#define f2(x,y,z) (x^y^z) +#define f3(x,y,z) ((x&y)|(z&(x|y))) +#define f4(x,y,z) (x^y^z) + +#define RK1(a,b,c,d,e,i, f, w, k) e += f(b,c,d) + w(i) + k + rotlFixed(a,5); b = rotlFixed(b,30); + +#define R0(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f1, w0, 0x5A827999) +#define R1(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f1, w1, 0x5A827999) +#define R2(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f2, w1, 0x6ED9EBA1) +#define R3(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f3, w1, 0x8F1BBCDC) +#define R4(a,b,c,d,e,i) RK1(a,b,c,d,e,i, f4, w1, 0xCA62C1D6) + +#define RX_1_4(rx1, rx4, i) rx1(a,b,c,d,e,i); rx4(e,a,b,c,d,i+1); rx4(d,e,a,b,c,i+2); rx4(c,d,e,a,b,i+3); rx4(b,c,d,e,a,i+4); +#define RX_5(rx, i) RX_1_4(rx, rx, i); + +void CContextBase::Init() +{ + _state[0] = 0x67452301; + _state[1] = 0xEFCDAB89; + _state[2] = 0x98BADCFE; + _state[3] = 0x10325476; + _state[4] = 0xC3D2E1F0; + _count = 0; +} + +void CContextBase::GetBlockDigest(UInt32 *data, UInt32 *destDigest, bool returnRes) +{ + UInt32 a, b, c, d, e; + UInt32 W[kNumW]; + + a = _state[0]; + b = _state[1]; + c = _state[2]; + d = _state[3]; + e = _state[4]; + #ifdef _SHA1_UNROLL + RX_5(R0, 0); RX_5(R0, 5); RX_5(R0, 10); + #else + int i; + for (i = 0; i < 15; i += 5) { RX_5(R0, i); } + #endif + + RX_1_4(R0, R1, 15); + + + #ifdef _SHA1_UNROLL + RX_5(R2, 20); RX_5(R2, 25); RX_5(R2, 30); RX_5(R2, 35); + RX_5(R3, 40); RX_5(R3, 45); RX_5(R3, 50); RX_5(R3, 55); + RX_5(R4, 60); RX_5(R4, 65); RX_5(R4, 70); RX_5(R4, 75); + #else + i = 20; + for (; i < 40; i += 5) { RX_5(R2, i); } + for (; i < 60; i += 5) { RX_5(R3, i); } + for (; i < 80; i += 5) { RX_5(R4, i); } + #endif + + destDigest[0] = _state[0] + a; + destDigest[1] = _state[1] + b; + destDigest[2] = _state[2] + c; + destDigest[3] = _state[3] + d; + destDigest[4] = _state[4] + e; + + if (returnRes) + for (int i = 0 ; i < 16; i++) + data[i] = W[kNumW - 16 + i]; + + // Wipe variables + // a = b = c = d = e = 0; +} + +void CContextBase::PrepareBlock(UInt32 *block, unsigned size) const +{ + unsigned curBufferPos = size & 0xF; + block[curBufferPos++] = 0x80000000; + while (curBufferPos != (16 - 2)) + block[curBufferPos++] = 0; + const UInt64 lenInBits = (_count << 9) + ((UInt64)size << 5); + block[curBufferPos++] = (UInt32)(lenInBits >> 32); + block[curBufferPos++] = (UInt32)(lenInBits); +} + +void CContext::Update(Byte *data, size_t size, bool rar350Mode) +{ + bool returnRes = false; + unsigned curBufferPos = _count2; + while (size-- > 0) + { + int pos = (int)(curBufferPos & 3); + if (pos == 0) + _buffer[curBufferPos >> 2] = 0; + _buffer[curBufferPos >> 2] |= ((UInt32)*data++) << (8 * (3 - pos)); + if (++curBufferPos == kBlockSize) + { + curBufferPos = 0; + CContextBase::UpdateBlock(_buffer, returnRes); + if (returnRes) + for (int i = 0; i < kBlockSizeInWords; i++) + { + UInt32 d = _buffer[i]; + data[i * 4 + 0 - kBlockSize] = (Byte)(d); + data[i * 4 + 1 - kBlockSize] = (Byte)(d >> 8); + data[i * 4 + 2 - kBlockSize] = (Byte)(d >> 16); + data[i * 4 + 3 - kBlockSize] = (Byte)(d >> 24); + } + returnRes = rar350Mode; + } + } + _count2 = curBufferPos; +} + +void CContext::Final(Byte *digest) +{ + const UInt64 lenInBits = (_count << 9) + ((UInt64)_count2 << 3); + unsigned curBufferPos = _count2; + int pos = (int)(curBufferPos & 3); + curBufferPos >>= 2; + if (pos == 0) + _buffer[curBufferPos] = 0; + _buffer[curBufferPos++] |= ((UInt32)0x80) << (8 * (3 - pos)); + + while (curBufferPos != (16 - 2)) + { + curBufferPos &= 0xF; + if (curBufferPos == 0) + UpdateBlock(); + _buffer[curBufferPos++] = 0; + } + _buffer[curBufferPos++] = (UInt32)(lenInBits >> 32); + _buffer[curBufferPos++] = (UInt32)(lenInBits); + UpdateBlock(); + + int i; + for (i = 0; i < kDigestSizeInWords; i++) + { + UInt32 state = _state[i] & 0xFFFFFFFF; + *digest++ = (Byte)(state >> 24); + *digest++ = (Byte)(state >> 16); + *digest++ = (Byte)(state >> 8); + *digest++ = (Byte)(state); + } + Init(); +} + +/////////////////////////// +// Words version + +void CContext32::Update(const UInt32 *data, size_t size) +{ + while (size-- > 0) + { + _buffer[_count2++] = *data++; + if (_count2 == kBlockSizeInWords) + { + _count2 = 0; + UpdateBlock(); + } + } +} + +void CContext32::Final(UInt32 *digest) +{ + const UInt64 lenInBits = (_count << 9) + ((UInt64)_count2 << 5); + unsigned curBufferPos = _count2; + _buffer[curBufferPos++] = 0x80000000; + while (curBufferPos != (16 - 2)) + { + curBufferPos &= 0xF; + if (curBufferPos == 0) + UpdateBlock(); + _buffer[curBufferPos++] = 0; + } + _buffer[curBufferPos++] = (UInt32)(lenInBits >> 32); + _buffer[curBufferPos++] = (UInt32)(lenInBits); + GetBlockDigest(_buffer, digest); + Init(); +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/Sha1.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/Sha1.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,68 @@ +// Crypto/Sha1.h +// This file is based on public domain +// Steve Reid and Wei Dai's code from Crypto++ + +#ifndef __CRYPTO_SHA1_H +#define __CRYPTO_SHA1_H + +#include +#include "../../Common/Types.h" + +// Sha1 implementation in RAR before version 3.60 has bug: +// it changes data bytes in some cases. +// So this class supports both versions: normal_SHA and rar3Mode + +namespace NCrypto { +namespace NSha1 { + +const unsigned kBlockSize = 64; +const unsigned kDigestSize = 20; + +const unsigned kBlockSizeInWords = (kBlockSize >> 2); +const unsigned kDigestSizeInWords = (kDigestSize >> 2); + +class CContextBase +{ +protected: + UInt32 _state[5]; + UInt64 _count; + void UpdateBlock(UInt32 *data, bool returnRes = false) + { + GetBlockDigest(data, _state, returnRes); + _count++; + } +public: + void Init(); + void GetBlockDigest(UInt32 *blockData, UInt32 *destDigest, bool returnRes = false); + // PrepareBlock can be used only when size <= 13. size in Words + void PrepareBlock(UInt32 *block, unsigned int size) const; +}; + +class CContextBase2: public CContextBase +{ +protected: + unsigned _count2; + UInt32 _buffer[kBlockSizeInWords]; + void UpdateBlock() { CContextBase::UpdateBlock(_buffer); } +public: + void Init() { CContextBase::Init(); _count2 = 0; } +}; + +class CContext: public CContextBase2 +{ +public: + void Update(Byte *data, size_t size, bool rar350Mode = false); + void Update(const Byte *data, size_t size) { Update((Byte *)data, size, false); } + void Final(Byte *digest); +}; + +class CContext32: public CContextBase2 +{ +public: + void Update(const UInt32 *data, size_t size); + void Final(UInt32 *digest); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../Common/MyWindows.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/WzAes.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/WzAes.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,209 @@ +// Crypto/WzAes.cpp +/* +This code implements Brian Gladman's scheme +specified in password Based File Encryption Utility. + +Note: you must include MyAes.cpp to project to initialize AES tables +*/ + +#include "StdAfx.h" + +#include "../Common/StreamObjects.h" +#include "../Common/StreamUtils.h" + +#include "Pbkdf2HmacSha1.h" +#include "RandGen.h" +#include "WzAes.h" + +// define it if you don't want to use speed-optimized version of Pbkdf2HmacSha1 +// #define _NO_WZAES_OPTIMIZATIONS + +namespace NCrypto { +namespace NWzAes { + +const unsigned int kAesKeySizeMax = 32; + +static const UInt32 kNumKeyGenIterations = 1000; + +STDMETHODIMP CBaseCoder::CryptoSetPassword(const Byte *data, UInt32 size) +{ + if(size > kPasswordSizeMax) + return E_INVALIDARG; + _key.Password.SetCapacity(size); + memcpy(_key.Password, data, size); + return S_OK; +} + +#define SetUi32(p, d) { UInt32 x = (d); (p)[0] = (Byte)x; (p)[1] = (Byte)(x >> 8); \ + (p)[2] = (Byte)(x >> 16); (p)[3] = (Byte)(x >> 24); } + +void CBaseCoder::EncryptData(Byte *data, UInt32 size) +{ + unsigned int pos = _blockPos; + for (; size > 0; size--) + { + if (pos == AES_BLOCK_SIZE) + { + if (++_counter[0] == 0) + _counter[1]++; + UInt32 temp[4]; + Aes_Encode32(&Aes, temp, _counter); + SetUi32(_buffer, temp[0]); + SetUi32(_buffer + 4, temp[1]); + SetUi32(_buffer + 8, temp[2]); + SetUi32(_buffer + 12, temp[3]); + pos = 0; + } + *data++ ^= _buffer[pos++]; + } + _blockPos = pos; +} + +#ifndef _NO_WZAES_OPTIMIZATIONS + +static void BytesToBeUInt32s(const Byte *src, UInt32 *dest, int destSize) +{ + for (int i = 0 ; i < destSize; i++) + dest[i] = + ((UInt32)(src[i * 4 + 0]) << 24) | + ((UInt32)(src[i * 4 + 1]) << 16) | + ((UInt32)(src[i * 4 + 2]) << 8) | + ((UInt32)(src[i * 4 + 3])); +} + +#endif + +STDMETHODIMP CBaseCoder::Init() +{ + UInt32 keySize = _key.GetKeySize(); + UInt32 keysTotalSize = 2 * keySize + kPwdVerifCodeSize; + Byte buf[2 * kAesKeySizeMax + kPwdVerifCodeSize]; + + // for (int ii = 0; ii < 1000; ii++) + { + #ifdef _NO_WZAES_OPTIMIZATIONS + + NSha1::Pbkdf2Hmac( + _key.Password, _key.Password.GetCapacity(), + _key.Salt, _key.GetSaltSize(), + kNumKeyGenIterations, + buf, keysTotalSize); + + #else + + UInt32 buf32[(2 * kAesKeySizeMax + kPwdVerifCodeSize + 3) / 4]; + UInt32 key32SizeTotal = (keysTotalSize + 3) / 4; + UInt32 salt[kSaltSizeMax * 4]; + UInt32 saltSizeInWords = _key.GetSaltSize() / 4; + BytesToBeUInt32s(_key.Salt, salt, saltSizeInWords); + NSha1::Pbkdf2Hmac32( + _key.Password, _key.Password.GetCapacity(), + salt, saltSizeInWords, + kNumKeyGenIterations, + buf32, key32SizeTotal); + for (UInt32 j = 0; j < keysTotalSize; j++) + buf[j] = (Byte)(buf32[j / 4] >> (24 - 8 * (j & 3))); + + #endif + } + + _hmac.SetKey(buf + keySize, keySize); + memcpy(_key.PwdVerifComputed, buf + 2 * keySize, kPwdVerifCodeSize); + + _blockPos = AES_BLOCK_SIZE; + for (int i = 0; i < 4; i++) + _counter[i] = 0; + + Aes_SetKeyEncode(&Aes, buf, keySize); + return S_OK; +} + +/* +STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream) +{ + Byte keySizeMode = 3; + return outStream->Write(&keySizeMode, 1, NULL); +} +*/ + +HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream) +{ + UInt32 saltSize = _key.GetSaltSize(); + g_RandomGenerator.Generate(_key.Salt, saltSize); + Init(); + RINOK(WriteStream(outStream, _key.Salt, saltSize)); + return WriteStream(outStream, _key.PwdVerifComputed, kPwdVerifCodeSize); +} + +HRESULT CEncoder::WriteFooter(ISequentialOutStream *outStream) +{ + Byte mac[kMacSize]; + _hmac.Final(mac, kMacSize); + return WriteStream(outStream, mac, kMacSize); +} + +STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size) +{ + if (size != 1) + return E_INVALIDARG; + _key.Init(); + Byte keySizeMode = data[0]; + if (keySizeMode < 1 || keySizeMode > 3) + return E_INVALIDARG; + _key.KeySizeMode = keySizeMode; + return S_OK; +} + +HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream) +{ + UInt32 saltSize = _key.GetSaltSize(); + UInt32 extraSize = saltSize + kPwdVerifCodeSize; + Byte temp[kSaltSizeMax + kPwdVerifCodeSize]; + RINOK(ReadStream_FAIL(inStream, temp, extraSize)); + UInt32 i; + for (i = 0; i < saltSize; i++) + _key.Salt[i] = temp[i]; + for (i = 0; i < kPwdVerifCodeSize; i++) + _pwdVerifFromArchive[i] = temp[saltSize + i]; + return S_OK; +} + +static bool CompareArrays(const Byte *p1, const Byte *p2, UInt32 size) +{ + for (UInt32 i = 0; i < size; i++) + if (p1[i] != p2[i]) + return false; + return true; +} + +bool CDecoder::CheckPasswordVerifyCode() +{ + return CompareArrays(_key.PwdVerifComputed, _pwdVerifFromArchive, kPwdVerifCodeSize); +} + +HRESULT CDecoder::CheckMac(ISequentialInStream *inStream, bool &isOK) +{ + isOK = false; + Byte mac1[kMacSize]; + RINOK(ReadStream_FAIL(inStream, mac1, kMacSize)); + Byte mac2[kMacSize]; + _hmac.Final(mac2, kMacSize); + isOK = CompareArrays(mac1, mac2, kMacSize); + return S_OK; +} + +STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size) +{ + EncryptData(data, size); + _hmac.Update(data, size); + return size; +} + +STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size) +{ + _hmac.Update(data, size); + EncryptData(data, size); + return size; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/WzAes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/WzAes.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,119 @@ +// Crypto/WzAes.h +/* +This code implements Brian Gladman's scheme +specified in password Based File Encryption Utility: + - AES encryption (128,192,256-bit) in Counter (CTR) mode. + - HMAC-SHA1 authentication for encrypted data (10 bytes) + - Keys are derived by PPKDF2(RFC2898)-HMAC-SHA1 from ASCII password and + Salt (saltSize = aesKeySize / 2). + - 2 bytes contain Password Verifier's Code +*/ + +#ifndef __CRYPTO_WZ_AES_H +#define __CRYPTO_WZ_AES_H + +extern "C" +{ +#include "../../../C/Aes.h" +} + +#include "Common/Buffer.h" +#include "Common/MyCom.h" +#include "Common/MyVector.h" + +#include "../ICoder.h" +#include "../IPassword.h" + +#include "HmacSha1.h" + +namespace NCrypto { +namespace NWzAes { + +const unsigned int kSaltSizeMax = 16; +const unsigned int kMacSize = 10; + +const UInt32 kPasswordSizeMax = 99; // 128; + +// Password Verification Code Size +const unsigned int kPwdVerifCodeSize = 2; + +class CKeyInfo +{ +public: + Byte KeySizeMode; // 1 - 128-bit , 2 - 192-bit , 3 - 256-bit + Byte Salt[kSaltSizeMax]; + Byte PwdVerifComputed[kPwdVerifCodeSize]; + + CByteBuffer Password; + + UInt32 GetKeySize() const { return (8 * (KeySizeMode & 3) + 8); } + UInt32 GetSaltSize() const { return (4 * (KeySizeMode & 3) + 4); } + + CKeyInfo() { Init(); } + void Init() { KeySizeMode = 3; } +}; + +class CBaseCoder: + public ICompressFilter, + public ICryptoSetPassword, + public CMyUnknownImp +{ +protected: + CKeyInfo _key; + UInt32 _counter[AES_BLOCK_SIZE / 4]; + Byte _buffer[AES_BLOCK_SIZE]; + NSha1::CHmac _hmac; + unsigned int _blockPos; + Byte _pwdVerifFromArchive[kPwdVerifCodeSize]; + + void EncryptData(Byte *data, UInt32 size); + + CAes Aes; + +public: + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size) = 0; + + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); + + UInt32 GetHeaderSize() const { return _key.GetSaltSize() + kPwdVerifCodeSize; } +}; + +class CEncoder: + public CBaseCoder + // public ICompressWriteCoderProperties +{ +public: + MY_UNKNOWN_IMP1(ICryptoSetPassword) + // ICompressWriteCoderProperties + // STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + HRESULT WriteHeader(ISequentialOutStream *outStream); + HRESULT WriteFooter(ISequentialOutStream *outStream); + bool SetKeyMode(Byte mode) + { + if (mode < 1 || mode > 3) + return false; + _key.KeySizeMode = mode; + return true; + } +}; + +class CDecoder: + public CBaseCoder, + public ICompressSetDecoderProperties2 +{ +public: + MY_UNKNOWN_IMP2( + ICryptoSetPassword, + ICompressSetDecoderProperties2) + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size); + HRESULT ReadHeader(ISequentialInStream *inStream); + bool CheckPasswordVerifyCode(); + HRESULT CheckMac(ISequentialInStream *inStream, bool &isOK); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/ZipCrypto.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/ZipCrypto.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,131 @@ +// Crypto/ZipCrypto.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/7zCrc.h" +} + +#include "../Common/StreamUtils.h" + +#include "RandGen.h" +#include "ZipCrypto.h" + +namespace NCrypto { +namespace NZip { + +void CCipher::UpdateKeys(Byte b) +{ + Keys[0] = CRC_UPDATE_BYTE(Keys[0], b); + Keys[1] += Keys[0] & 0xff; + Keys[1] = Keys[1] * 134775813L + 1; + Keys[2] = CRC_UPDATE_BYTE(Keys[2], (Byte)(Keys[1] >> 24)); +} + +void CCipher::SetPassword(const Byte *password, UInt32 passwordLen) +{ + Keys[0] = 305419896L; + Keys[1] = 591751049L; + Keys[2] = 878082192L; + for (UInt32 i = 0; i < passwordLen; i++) + UpdateKeys(password[i]); +} + +Byte CCipher::DecryptByteSpec() +{ + UInt32 temp = Keys[2] | 2; + return (Byte)((temp * (temp ^ 1)) >> 8); +} + +Byte CCipher::DecryptByte(Byte b) +{ + Byte c = (Byte)(b ^ DecryptByteSpec()); + UpdateKeys(c); + return c; +} + +Byte CCipher::EncryptByte(Byte b) +{ + Byte c = (Byte)(b ^ DecryptByteSpec()); + UpdateKeys(b); + return c; +} + +void CCipher::DecryptHeader(Byte *buf) +{ + for (unsigned i = 0; i < kHeaderSize; i++) + buf[i] = DecryptByte(buf[i]); +} + +void CCipher::EncryptHeader(Byte *buf) +{ + for (unsigned i = 0; i < kHeaderSize; i++) + buf[i] = EncryptByte(buf[i]); +} + +STDMETHODIMP CEncoder::CryptoSetPassword(const Byte *data, UInt32 size) +{ + _cipher.SetPassword(data, size); + return S_OK; +} + +STDMETHODIMP CEncoder::CryptoSetCRC(UInt32 crc) +{ + _crc = crc; + return S_OK; +} + +STDMETHODIMP CEncoder::Init() +{ + return S_OK; +} + +HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream) +{ + Byte header[kHeaderSize]; + g_RandomGenerator.Generate(header, kHeaderSize - 2); + + header[kHeaderSize - 1] = Byte(_crc >> 24); + header[kHeaderSize - 2] = Byte(_crc >> 16); + + _cipher.EncryptHeader(header); + return WriteStream(outStream, header, kHeaderSize); +} + +STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size) +{ + UInt32 i; + for (i = 0; i < size; i++) + data[i] = _cipher.EncryptByte(data[i]); + return i; +} + +STDMETHODIMP CDecoder::CryptoSetPassword(const Byte *data, UInt32 size) +{ + _cipher.SetPassword(data, size); + return S_OK; +} + +HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream) +{ + Byte header[kHeaderSize]; + RINOK(ReadStream_FAIL(inStream, header, kHeaderSize)); + _cipher.DecryptHeader(header); + return S_OK; +} + +STDMETHODIMP CDecoder::Init() +{ + return S_OK; +} + +STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size) +{ + UInt32 i; + for (i = 0; i < size; i++) + data[i] = _cipher.DecryptByte(data[i]); + return i; +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/ZipCrypto.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/ZipCrypto.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,71 @@ +// Crypto/ZipCrypto.h + +#ifndef __CRYPTO_ZIP_CRYPTO_H +#define __CRYPTO_ZIP_CRYPTO_H + +#include "Common/MyCom.h" + +#include "../ICoder.h" +#include "../IPassword.h" + +namespace NCrypto { +namespace NZip { + +const unsigned kHeaderSize = 12; + +class CCipher +{ + UInt32 Keys[3]; + + void UpdateKeys(Byte b); + Byte DecryptByteSpec(); +public: + void SetPassword(const Byte *password, UInt32 passwordLen); + Byte DecryptByte(Byte b); + Byte EncryptByte(Byte b); + void DecryptHeader(Byte *buf); + void EncryptHeader(Byte *buf); +}; + +class CEncoder : + public ICompressFilter, + public ICryptoSetPassword, + public ICryptoSetCRC, + public CMyUnknownImp +{ + CCipher _cipher; + UInt32 _crc; +public: + MY_UNKNOWN_IMP2( + ICryptoSetPassword, + ICryptoSetCRC + ) + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); + STDMETHOD(CryptoSetCRC)(UInt32 crc); + HRESULT WriteHeader(ISequentialOutStream *outStream); +}; + + +class CDecoder: + public ICompressFilter, + public ICryptoSetPassword, + public CMyUnknownImp +{ + CCipher _cipher; +public: + MY_UNKNOWN_IMP1(ICryptoSetPassword) + + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); + + HRESULT ReadHeader(ISequentialInStream *inStream); +}; + + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/ZipStrong.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/ZipStrong.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,180 @@ +// Crypto/ZipStrong.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../../C/7zCrc.h" +#include "../../../C/CpuArch.h" +} + +#include "../Common/StreamUtils.h" + +#include "MyAES.h" +#include "Sha1.h" +#include "ZipStrong.h" + +namespace NCrypto { +namespace NZipStrong { + +static const UInt16 kAES128 = 0x660E; + +// DeriveKey* function is similar to CryptDeriveKey() from Windows. +// But MSDN tells that we need such scheme only if +// "the required key length is longer than the hash value" +// but ZipStrong uses it always. + +static void DeriveKey2(const Byte *digest, Byte c, Byte *dest) +{ + Byte buf[64]; + memset(buf, c, 64); + for (unsigned i = 0; i < NSha1::kDigestSize; i++) + buf[i] ^= digest[i]; + NSha1::CContext sha; + sha.Init(); + sha.Update(buf, 64); + sha.Final(dest); +} + +static void DeriveKey(NSha1::CContext &sha, Byte *key) +{ + Byte digest[NSha1::kDigestSize]; + sha.Final(digest); + Byte temp[NSha1::kDigestSize * 2]; + DeriveKey2(digest, 0x36, temp); + DeriveKey2(digest, 0x5C, temp + NSha1::kDigestSize); + memcpy(key, temp, 32); +} + +void CKeyInfo::SetPassword(const Byte *data, UInt32 size) +{ + NSha1::CContext sha; + sha.Init(); + sha.Update(data, size); + DeriveKey(sha, MasterKey); +} + +STDMETHODIMP CBaseCoder::CryptoSetPassword(const Byte *data, UInt32 size) +{ + _key.SetPassword(data, size); + return S_OK; +} + +STDMETHODIMP CBaseCoder::Init() +{ + return S_OK; +} + +HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream, UInt32 /* crc */, UInt64 /* unpackSize */) +{ + Byte temp[4]; + RINOK(ReadStream_FALSE(inStream, temp, 2)); + _ivSize = GetUi16(temp); + if (_ivSize == 0) + { + return E_NOTIMPL; + /* + SetUi32(_iv, crc); + for (int i = 0; i < 8; i++) + _iv[4 + i] = (Byte)(unpackSize >> (8 * i)); + SetUi32(_iv + 12, 0); + */ + } + else if (_ivSize == 16) + { + RINOK(ReadStream_FALSE(inStream, _iv, _ivSize)); + } + else + return E_NOTIMPL; + RINOK(ReadStream_FALSE(inStream, temp, 4)); + _remSize = GetUi32(temp); + if (_remSize > _buf.GetCapacity()) + { + _buf.Free(); + _buf.SetCapacity(_remSize); + } + return ReadStream_FALSE(inStream, _buf, _remSize); +} + +HRESULT CDecoder::CheckPassword(bool &passwOK) +{ + passwOK = false; + if (_remSize < 10) + return E_NOTIMPL; + Byte *p = _buf; + UInt16 format = GetUi16(p); + if (format != 3) + return E_NOTIMPL; + UInt16 algId = GetUi16(p + 2); + if (algId < kAES128) + return E_NOTIMPL; + algId -= kAES128; + if (algId > 2) + return E_NOTIMPL; + UInt16 bitLen = GetUi16(p + 4); + UInt16 flags = GetUi16(p + 6); + if (algId * 64 + 128 != bitLen) + return E_NOTIMPL; + _key.KeySize = 16 + algId * 8; + if ((flags & 1) == 0) + return E_NOTIMPL; + UInt32 rdSize = GetUi16(p + 8); + UInt32 pos = 10; + Byte *rd = p + pos; + pos += rdSize; + if (pos + 4 > _remSize) + return E_NOTIMPL; + UInt32 reserved = GetUi32(p + pos); + pos += 4; + if (reserved != 0) + return E_NOTIMPL; + if (pos + 2 > _remSize) + return E_NOTIMPL; + UInt32 validSize = GetUi16(p + pos); + pos += 2; + Byte *validData = p + pos; + if (pos + validSize != _remSize) + return E_NOTIMPL; + + if (!_aesFilter) + _aesFilter = new CAesCbcDecoder; + + CMyComPtr cp; + RINOK(_aesFilter.QueryInterface(IID_ICryptoProperties, &cp)); + { + RINOK(cp->SetKey(_key.MasterKey, _key.KeySize)); + RINOK(cp->SetInitVector(_iv, 16)); + _aesFilter->Init(); + if (_aesFilter->Filter(rd, rdSize) != rdSize) + return E_NOTIMPL; + } + + Byte fileKey[32]; + NSha1::CContext sha; + sha.Init(); + sha.Update(_iv, 16); + sha.Update(rd, rdSize - 16); // we don't use last 16 bytes (PAD bytes) + DeriveKey(sha, fileKey); + + RINOK(cp->SetKey(fileKey, _key.KeySize)); + RINOK(cp->SetInitVector(_iv, 16)); + _aesFilter->Init(); + if (_aesFilter->Filter(validData, validSize) != validSize) + return E_NOTIMPL; + + if (validSize < 4) + return E_NOTIMPL; + validSize -= 4; + if (GetUi32(validData + validSize) != CrcCalc(validData, validSize)) + return S_OK; + passwOK = true; + _aesFilter->Init(); + return S_OK; +} + +STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size) +{ + return _aesFilter->Filter(data, size); +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/Crypto/ZipStrong.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/ZipStrong.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,53 @@ +// Crypto/ZipStrong.h + +#ifndef __CRYPTO_ZIP_STRONG_H +#define __CRYPTO_ZIP_STRONG_H + +#include "Common/MyCom.h" +#include "Common/Buffer.h" + +#include "../ICoder.h" +#include "../IPassword.h" + +namespace NCrypto { +namespace NZipStrong { + +struct CKeyInfo +{ + Byte MasterKey[32]; + UInt32 KeySize; + void SetPassword(const Byte *data, UInt32 size); +}; + +class CBaseCoder: + public ICompressFilter, + public ICryptoSetPassword, + public CMyUnknownImp +{ +protected: + CKeyInfo _key; + CMyComPtr _aesFilter; + CByteBuffer _buf; +public: + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size) = 0; + + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); +}; + +class CDecoder: + public CBaseCoder +{ + UInt32 _ivSize; + Byte _iv[16]; + UInt32 _remSize; +public: + MY_UNKNOWN_IMP1(ICryptoSetPassword) + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + HRESULT ReadHeader(ISequentialInStream *inStream, UInt32 crc, UInt64 unpackSize); + HRESULT CheckPassword(bool &passwOK); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/ICoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/ICoder.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,186 @@ +// ICoder.h + +#ifndef __ICODER_H +#define __ICODER_H + +#include "IStream.h" + +#define CODER_INTERFACE(i, x) DECL_INTERFACE(i, 4, x) + +CODER_INTERFACE(ICompressProgressInfo, 0x04) +{ + STDMETHOD(SetRatioInfo)(const UInt64 *inSize, const UInt64 *outSize) PURE; +}; + +CODER_INTERFACE(ICompressCoder, 0x05) +{ + STDMETHOD(Code)(ISequentialInStream *inStream, + ISequentialOutStream *outStream, + const UInt64 *inSize, + const UInt64 *outSize, + ICompressProgressInfo *progress) PURE; +}; + +CODER_INTERFACE(ICompressCoder2, 0x18) +{ + STDMETHOD(Code)(ISequentialInStream **inStreams, + const UInt64 **inSizes, + UInt32 numInStreams, + ISequentialOutStream **outStreams, + const UInt64 **outSizes, + UInt32 numOutStreams, + ICompressProgressInfo *progress) PURE; +}; + +namespace NCoderPropID +{ + enum EEnum + { + kDictionarySize = 0x400, + kUsedMemorySize, + kOrder, + kBlockSize, + kPosStateBits = 0x440, + kLitContextBits, + kLitPosBits, + kNumFastBytes = 0x450, + kMatchFinder, + kMatchFinderCycles, + kNumPasses = 0x460, + kAlgorithm = 0x470, + kMultiThread = 0x480, + kNumThreads, + kEndMarker = 0x490 + }; +} + +CODER_INTERFACE(ICompressSetCoderProperties, 0x20) +{ + STDMETHOD(SetCoderProperties)(const PROPID *propIDs, + const PROPVARIANT *properties, UInt32 numProperties) PURE; +}; + +/* +CODER_INTERFACE(ICompressSetCoderProperties, 0x21) +{ + STDMETHOD(SetDecoderProperties)(ISequentialInStream *inStream) PURE; +}; +*/ + +CODER_INTERFACE(ICompressSetDecoderProperties2, 0x22) +{ + STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size) PURE; +}; + +CODER_INTERFACE(ICompressWriteCoderProperties, 0x23) +{ + STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStreams) PURE; +}; + +CODER_INTERFACE(ICompressGetInStreamProcessedSize, 0x24) +{ + STDMETHOD(GetInStreamProcessedSize)(UInt64 *value) PURE; +}; + +CODER_INTERFACE(ICompressSetCoderMt, 0x25) +{ + STDMETHOD(SetNumberOfThreads)(UInt32 numThreads) PURE; +}; + +CODER_INTERFACE(ICompressGetSubStreamSize, 0x30) +{ + STDMETHOD(GetSubStreamSize)(UInt64 subStream, UInt64 *value) PURE; +}; + +CODER_INTERFACE(ICompressSetInStream, 0x31) +{ + STDMETHOD(SetInStream)(ISequentialInStream *inStream) PURE; + STDMETHOD(ReleaseInStream)() PURE; +}; + +CODER_INTERFACE(ICompressSetOutStream, 0x32) +{ + STDMETHOD(SetOutStream)(ISequentialOutStream *outStream) PURE; + STDMETHOD(ReleaseOutStream)() PURE; +}; + +CODER_INTERFACE(ICompressSetInStreamSize, 0x33) +{ + STDMETHOD(SetInStreamSize)(const UInt64 *inSize) PURE; +}; + +CODER_INTERFACE(ICompressSetOutStreamSize, 0x34) +{ + STDMETHOD(SetOutStreamSize)(const UInt64 *outSize) PURE; +}; + +CODER_INTERFACE(ICompressFilter, 0x40) +{ + STDMETHOD(Init)() PURE; + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size) PURE; + // Filter return outSize (UInt32) + // if (outSize <= size): Filter have converted outSize bytes + // if (outSize > size): Filter have not converted anything. + // and it needs at least outSize bytes to convert one block + // (it's for crypto block algorithms). +}; + +CODER_INTERFACE(ICompressCodecsInfo, 0x60) +{ + STDMETHOD(GetNumberOfMethods)(UInt32 *numMethods) PURE; + STDMETHOD(GetProperty)(UInt32 index, PROPID propID, PROPVARIANT *value) PURE; + STDMETHOD(CreateDecoder)(UInt32 index, const GUID *iid, void **coder) PURE; + STDMETHOD(CreateEncoder)(UInt32 index, const GUID *iid, void **coder) PURE; +}; +CODER_INTERFACE(ISetCompressCodecsInfo, 0x61) +{ + STDMETHOD(SetCompressCodecsInfo)(ICompressCodecsInfo *compressCodecsInfo) PURE; +}; + +CODER_INTERFACE(ICryptoProperties, 0x80) +{ + STDMETHOD(SetKey)(const Byte *data, UInt32 size) PURE; + STDMETHOD(SetInitVector)(const Byte *data, UInt32 size) PURE; +}; + +/* +CODER_INTERFACE(ICryptoResetSalt, 0x88) +{ + STDMETHOD(ResetSalt)() PURE; +}; +*/ + +CODER_INTERFACE(ICryptoResetInitVector, 0x8C) +{ + STDMETHOD(ResetInitVector)() PURE; +}; + +CODER_INTERFACE(ICryptoSetPassword, 0x90) +{ + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size) PURE; +}; + +CODER_INTERFACE(ICryptoSetCRC, 0xA0) +{ + STDMETHOD(CryptoSetCRC)(UInt32 crc) PURE; +}; + +////////////////////// +// It's for DLL file +namespace NMethodPropID +{ + enum EEnum + { + kID, + kName, + kDecoder, + kEncoder, + kInStreams, + kOutStreams, + kDescription, + kDecoderIsAssigned, + kEncoderIsAssigned + }; +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/IDecl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/IDecl.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,15 @@ +// IDecl.h + +#ifndef __IDECL_H +#define __IDECL_H + +#include "../Common/MyUnknown.h" + +#define DECL_INTERFACE_SUB(i, base, groupId, subId) \ +DEFINE_GUID(IID_ ## i, \ +0x23170F69, 0x40C1, 0x278A, 0, 0, 0, (groupId), 0, (subId), 0, 0); \ +struct i: public base + +#define DECL_INTERFACE(i, groupId, subId) DECL_INTERFACE_SUB(i, IUnknown, groupId, subId) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/IPassword.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/IPassword.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +// IPassword.h + +#ifndef __IPASSWORD_H +#define __IPASSWORD_H + +#include "../Common/MyUnknown.h" +#include "../Common/Types.h" + +#include "IDecl.h" + +#define PASSWORD_INTERFACE(i, x) DECL_INTERFACE(i, 5, x) + +PASSWORD_INTERFACE(ICryptoGetTextPassword, 0x10) +{ + STDMETHOD(CryptoGetTextPassword)(BSTR *password) PURE; +}; + +PASSWORD_INTERFACE(ICryptoGetTextPassword2, 0x11) +{ + STDMETHOD(CryptoGetTextPassword2)(Int32 *passwordIsDefined, BSTR *password) PURE; +}; + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/IProgress.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/IProgress.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,33 @@ +// Interface/IProgress.h + +#ifndef __IPROGRESS_H +#define __IPROGRESS_H + +#include "../Common/MyUnknown.h" +#include "../Common/Types.h" + +#include "IDecl.h" + +#define INTERFACE_IProgress(x) \ + STDMETHOD(SetTotal)(UInt64 total) x; \ + STDMETHOD(SetCompleted)(const UInt64 *completeValue) x; \ + +DECL_INTERFACE(IProgress, 0, 5) +{ + INTERFACE_IProgress(PURE) +}; + +/* +// {23170F69-40C1-278A-0000-000000050002} +DEFINE_GUID(IID_IProgress2, +0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x02); +MIDL_INTERFACE("23170F69-40C1-278A-0000-000000050002") +IProgress2: public IUnknown +{ +public: + STDMETHOD(SetTotal)(const UInt64 *total) PURE; + STDMETHOD(SetCompleted)(const UInt64 *completeValue) PURE; +}; +*/ + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/IStream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/IStream.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,58 @@ +// IStream.h + +#ifndef __ISTREAM_H +#define __ISTREAM_H + +#include "../Common/MyUnknown.h" +#include "../Common/Types.h" + +#include "IDecl.h" + +#define STREAM_INTERFACE_SUB(i, base, x) DECL_INTERFACE_SUB(i, base, 3, x) +#define STREAM_INTERFACE(i, x) STREAM_INTERFACE_SUB(i, IUnknown, x) + +STREAM_INTERFACE(ISequentialInStream, 0x01) +{ + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize) PURE; + /* + Out: if size != 0, return_value = S_OK and (*processedSize == 0), + then there are no more bytes in stream. + if (size > 0) && there are bytes in stream, + this function must read at least 1 byte. + This function is allowed to read less than number of remaining bytes in stream. + You must call Read function in loop, if you need exact amount of data + */ +}; + +STREAM_INTERFACE(ISequentialOutStream, 0x02) +{ + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize) PURE; + /* + if (size > 0) this function must write at least 1 byte. + This function is allowed to write less than "size". + You must call Write function in loop, if you need to write exact amount of data + */ +}; + +STREAM_INTERFACE_SUB(IInStream, ISequentialInStream, 0x03) +{ + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) PURE; +}; + +STREAM_INTERFACE_SUB(IOutStream, ISequentialOutStream, 0x04) +{ + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) PURE; + STDMETHOD(SetSize)(Int64 newSize) PURE; +}; + +STREAM_INTERFACE(IStreamGetSize, 0x06) +{ + STDMETHOD(GetSize)(UInt64 *size) PURE; +}; + +STREAM_INTERFACE(IOutStreamFlush, 0x07) +{ + STDMETHOD(Flush)() PURE; +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/7zip/PropID.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/7zip/PropID.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,69 @@ +// PropID.h + +#ifndef __7ZIP_PROPID_H +#define __7ZIP_PROPID_H + +enum +{ + kpidNoProperty = 0, + + kpidHandlerItemIndex = 2, + kpidPath, + kpidName, + kpidExtension, + kpidIsDir, + kpidSize, + kpidPackSize, + kpidAttrib, + kpidCTime, + kpidATime, + kpidMTime, + kpidSolid, + kpidCommented, + kpidEncrypted, + kpidSplitBefore, + kpidSplitAfter, + kpidDictionarySize, + kpidCRC, + kpidType, + kpidIsAnti, + kpidMethod, + kpidHostOS, + kpidFileSystem, + kpidUser, + kpidGroup, + kpidBlock, + kpidComment, + kpidPosition, + kpidPrefix, + kpidNumSubDirs, + kpidNumSubFiles, + kpidUnpackVer, + kpidVolume, + kpidIsVolume, + kpidOffset, + kpidLinks, + kpidNumBlocks, + kpidNumVolumes, + kpidTimeType, + kpidBit64, + kpidBigEndian, + kpidCpu, + kpidPhySize, + kpidHeadersSize, + kpidChecksum, + kpidCharacts, + kpidVa, + + kpidTotalSize = 0x1100, + kpidFreeSpace, + kpidClusterSize, + kpidVolumeName, + + kpidLocalName = 0x1200, + kpidProvider, + + kpidUserDefined = 0x10000 +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/Buffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/Buffer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,77 @@ +// Common/Buffer.h + +#ifndef __COMMON_BUFFER_H +#define __COMMON_BUFFER_H + +#include "Defs.h" + +template class CBuffer +{ +protected: + size_t _capacity; + T *_items; +public: + void Free() + { + delete []_items; + _items = 0; + _capacity = 0; + } + CBuffer(): _capacity(0), _items(0) {}; + CBuffer(const CBuffer &buffer): _capacity(0), _items(0) { *this = buffer; } + CBuffer(size_t size): _items(0), _capacity(0) { SetCapacity(size); } + virtual ~CBuffer() { delete []_items; } + operator T *() { return _items; }; + operator const T *() const { return _items; }; + size_t GetCapacity() const { return _capacity; } + void SetCapacity(size_t newCapacity) + { + if (newCapacity == _capacity) + return; + T *newBuffer; + if (newCapacity > 0) + { + newBuffer = new T[newCapacity]; + if (_capacity > 0) + memmove(newBuffer, _items, MyMin(_capacity, newCapacity) * sizeof(T)); + } + else + newBuffer = 0; + delete []_items; + _items = newBuffer; + _capacity = newCapacity; + } + CBuffer& operator=(const CBuffer &buffer) + { + Free(); + if (buffer._capacity > 0) + { + SetCapacity(buffer._capacity); + memmove(_items, buffer._items, buffer._capacity * sizeof(T)); + } + return *this; + } +}; + +template +bool operator==(const CBuffer& b1, const CBuffer& b2) +{ + if (b1.GetCapacity() != b2.GetCapacity()) + return false; + for (size_t i = 0; i < b1.GetCapacity(); i++) + if (b1[i] != b2[i]) + return false; + return true; +} + +template +bool operator!=(const CBuffer& b1, const CBuffer& b2) +{ + return !(b1 == b2); +} + +typedef CBuffer CCharBuffer; +typedef CBuffer CWCharBuffer; +typedef CBuffer CByteBuffer; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/CRC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/CRC.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,10 @@ +// Common/CRC.cpp + +#include "StdAfx.h" + +extern "C" +{ +#include "../../C/7zCrc.h" +} + +struct CCRCTableInit { CCRCTableInit() { CrcGenerateTable(); } } g_CRCTableInit; diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/ComTry.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/ComTry.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,17 @@ +// ComTry.h + +#ifndef __COM_TRY_H +#define __COM_TRY_H + +#include "MyWindows.h" +// #include "Exception.h" +// #include "NewHandler.h" + +#define COM_TRY_BEGIN try { +#define COM_TRY_END } catch(...) { return E_OUTOFMEMORY; } + + // catch(const CNewException &) { return E_OUTOFMEMORY; }\ + // catch(const CSystemException &e) { return e.ErrorCode; }\ + // catch(...) { return E_FAIL; } + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/Defs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/Defs.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,20 @@ +// Common/Defs.h + +#ifndef __COMMON_DEFS_H +#define __COMMON_DEFS_H + +template inline T MyMin(T a, T b) + { return a < b ? a : b; } +template inline T MyMax(T a, T b) + { return a > b ? a : b; } + +template inline int MyCompare(T a, T b) + { return a < b ? -1 : (a == b ? 0 : 1); } + +inline int BoolToInt(bool value) + { return (value ? 1: 0); } + +inline bool IntToBool(int value) + { return (value != 0); } + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/DynamicBuffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/DynamicBuffer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,47 @@ +// Common/DynamicBuffer.h + +#ifndef __COMMON_DYNAMICBUFFER_H +#define __COMMON_DYNAMICBUFFER_H + +#include "Buffer.h" + +template class CDynamicBuffer: public CBuffer +{ + void GrowLength(size_t size) + { + size_t delta; + if (this->_capacity > 64) + delta = this->_capacity / 4; + else if (this->_capacity > 8) + delta = 16; + else + delta = 4; + delta = MyMax(delta, size); + SetCapacity(this->_capacity + delta); + } +public: + CDynamicBuffer(): CBuffer() {}; + CDynamicBuffer(const CDynamicBuffer &buffer): CBuffer(buffer) {}; + CDynamicBuffer(size_t size): CBuffer(size) {}; + CDynamicBuffer& operator=(const CDynamicBuffer &buffer) + { + this->Free(); + if (buffer._capacity > 0) + { + SetCapacity(buffer._capacity); + memmove(this->_items, buffer._items, buffer._capacity * sizeof(T)); + } + return *this; + } + void EnsureCapacity(size_t capacity) + { + if (this->_capacity < capacity) + GrowLength(capacity - this->_capacity); + } +}; + +typedef CDynamicBuffer CCharDynamicBuffer; +typedef CDynamicBuffer CWCharDynamicBuffer; +typedef CDynamicBuffer CByteDynamicBuffer; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/InitializeStaticLib.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/InitializeStaticLib.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,29 @@ +// Common/InitializeStaticLib.h +// +// it's necessary to include this from one cpp file in order to use 7zip as a static library, +// otherwise the linker will optimize away some important internals of 7zip. + +#ifndef __COMMON_INITIALIZESTATICLIB_H +#define __COMMON_INITIALIZESTATICLIB_H + +#define FORCE_REF(dec, var) extern dec var; void* var##ref = (void*)&var; + +#include "../7zip/Common/DeclareCodecs.h" +#include "../7zip/Common/DeclareArcs.h" + +FORCE_REF(struct CCRCTableInit, g_CRCTableInit) + + +// these don't seem to be necessary with my compiler, +// but they're here in case a different compiler more aggressively strips out unreferenced code +FORCE_REF(class CBZip2CrcTableInit, g_BZip2CrcTableInit) +namespace NCrypto { struct CAesTabInit; FORCE_REF(CAesTabInit, g_AesTabInit) } +namespace NBitl { struct CInverterTableInitializer; FORCE_REF(CInverterTableInitializer, g_InverterTableInitializer) } +namespace NCompress { namespace NRar3 { class CDistInit; FORCE_REF(CDistInit, g_DistInit) }} +namespace NArchive { namespace NLzh { class CCRCTableInit; FORCE_REF(CCRCTableInit, g_CRCTableInit) }} +namespace NArchive { namespace N7z { class SignatureInitializer; FORCE_REF(SignatureInitializer, g_SignatureInitializer) }} +namespace NArchive{ namespace NRar{ namespace NHeader{ class CMarkerInitializer; FORCE_REF(CMarkerInitializer, g_MarkerInitializer) }}} +namespace NArchive { namespace NZip { namespace NSignature{ class CMarkersInitializer; FORCE_REF(CMarkersInitializer, g_MarkerInitializer) }}} + + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/IntToString.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/IntToString.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,63 @@ +// Common/IntToString.cpp + +#include "StdAfx.h" + +#include "IntToString.h" + +void ConvertUInt64ToString(UInt64 value, char *s, UInt32 base) +{ + if (base < 2 || base > 36) + { + *s = '\0'; + return; + } + char temp[72]; + int pos = 0; + do + { + int delta = (int)(value % base); + temp[pos++] = (char)((delta < 10) ? ('0' + delta) : ('a' + (delta - 10))); + value /= base; + } + while (value != 0); + do + *s++ = temp[--pos]; + while (pos > 0); + *s = '\0'; +} + +void ConvertUInt64ToString(UInt64 value, wchar_t *s) +{ + wchar_t temp[32]; + int pos = 0; + do + { + temp[pos++] = (wchar_t)(L'0' + (int)(value % 10)); + value /= 10; + } + while (value != 0); + do + *s++ = temp[--pos]; + while (pos > 0); + *s = L'\0'; +} + +void ConvertInt64ToString(Int64 value, char *s) +{ + if (value < 0) + { + *s++ = '-'; + value = -value; + } + ConvertUInt64ToString(value, s); +} + +void ConvertInt64ToString(Int64 value, wchar_t *s) +{ + if (value < 0) + { + *s++ = L'-'; + value = -value; + } + ConvertUInt64ToString(value, s); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/IntToString.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/IntToString.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,15 @@ +// Common/IntToString.h + +#ifndef __COMMON_INTTOSTRING_H +#define __COMMON_INTTOSTRING_H + +#include +#include "Types.h" + +void ConvertUInt64ToString(UInt64 value, char *s, UInt32 base = 10); +void ConvertUInt64ToString(UInt64 value, wchar_t *s); + +void ConvertInt64ToString(Int64 value, char *s); +void ConvertInt64ToString(Int64 value, wchar_t *s); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyCom.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyCom.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,225 @@ +// MyCom.h + +#ifndef __MYCOM_H +#define __MYCOM_H + +#include "MyWindows.h" + +#ifndef RINOK +#define RINOK(x) { HRESULT __result_ = (x); if (__result_ != S_OK) return __result_; } +#endif + +template +class CMyComPtr +{ + T* _p; +public: + // typedef T _PtrClass; + CMyComPtr() { _p = NULL;} + CMyComPtr(T* p) {if ((_p = p) != NULL) p->AddRef(); } + CMyComPtr(const CMyComPtr& lp) + { + if ((_p = lp._p) != NULL) + _p->AddRef(); + } + ~CMyComPtr() { if (_p) _p->Release(); } + void Release() { if (_p) { _p->Release(); _p = NULL; } } + operator T*() const { return (T*)_p; } + // T& operator*() const { return *_p; } + T** operator&() { return &_p; } + T* operator->() const { return _p; } + T* operator=(T* p) + { + if (p != 0) + p->AddRef(); + if (_p) + _p->Release(); + _p = p; + return p; + } + T* operator=(const CMyComPtr& lp) { return (*this = lp._p); } + bool operator!() const { return (_p == NULL); } + // bool operator==(T* pT) const { return _p == pT; } + // Compare two objects for equivalence + void Attach(T* p2) + { + Release(); + _p = p2; + } + T* Detach() + { + T* pt = _p; + _p = NULL; + return pt; + } + #ifdef _WIN32 + HRESULT CoCreateInstance(REFCLSID rclsid, REFIID iid, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) + { + return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, (void**)&_p); + } + #endif + /* + HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) + { + CLSID clsid; + HRESULT hr = CLSIDFromProgID(szProgID, &clsid); + ATLASSERT(_p == NULL); + if (SUCCEEDED(hr)) + hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T), (void**)&_p); + return hr; + } + */ + template + HRESULT QueryInterface(REFGUID iid, Q** pp) const + { + return _p->QueryInterface(iid, (void**)pp); + } +}; + +////////////////////////////////////////////////////////// + +inline HRESULT StringToBstr(LPCOLESTR src, BSTR *bstr) +{ + *bstr = ::SysAllocString(src); + return (*bstr != 0) ? S_OK : E_OUTOFMEMORY; +} + +class CMyComBSTR +{ +public: + BSTR m_str; + CMyComBSTR(): m_str(NULL) {} + CMyComBSTR(LPCOLESTR src) { m_str = ::SysAllocString(src); } + // CMyComBSTR(int nSize) { m_str = ::SysAllocStringLen(NULL, nSize); } + // CMyComBSTR(int nSize, LPCOLESTR sz) { m_str = ::SysAllocStringLen(sz, nSize); } + CMyComBSTR(const CMyComBSTR& src) { m_str = src.MyCopy(); } + /* + CMyComBSTR(REFGUID src) + { + LPOLESTR szGuid; + StringFromCLSID(src, &szGuid); + m_str = ::SysAllocString(szGuid); + CoTaskMemFree(szGuid); + } + */ + ~CMyComBSTR() { ::SysFreeString(m_str); } + CMyComBSTR& operator=(const CMyComBSTR& src) + { + if (m_str != src.m_str) + { + if (m_str) + ::SysFreeString(m_str); + m_str = src.MyCopy(); + } + return *this; + } + CMyComBSTR& operator=(LPCOLESTR src) + { + ::SysFreeString(m_str); + m_str = ::SysAllocString(src); + return *this; + } + unsigned int Length() const { return ::SysStringLen(m_str); } + operator BSTR() const { return m_str; } + BSTR* operator&() { return &m_str; } + BSTR MyCopy() const + { + int byteLen = ::SysStringByteLen(m_str); + BSTR res = ::SysAllocStringByteLen(NULL, byteLen); + memcpy(res, m_str, byteLen); + return res; + } + /* + void Attach(BSTR src) { m_str = src; } + BSTR Detach() + { + BSTR s = m_str; + m_str = NULL; + return s; + } + */ + void Empty() + { + ::SysFreeString(m_str); + m_str = NULL; + } + bool operator!() const { return (m_str == NULL); } +}; + +////////////////////////////////////////////////////////// + +class CMyUnknownImp +{ +public: + ULONG __m_RefCount; + CMyUnknownImp(): __m_RefCount(0) {} +}; + +#define MY_QUERYINTERFACE_BEGIN STDMETHOD(QueryInterface) \ + (REFGUID iid, void **outObject) { + +#define MY_QUERYINTERFACE_ENTRY(i) if (iid == IID_ ## i) \ + { *outObject = (void *)(i *)this; AddRef(); return S_OK; } + +#define MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) if (iid == IID_IUnknown) \ + { *outObject = (void *)(IUnknown *)(i *)this; AddRef(); return S_OK; } + +#define MY_QUERYINTERFACE_BEGIN2(i) MY_QUERYINTERFACE_BEGIN \ + MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \ + MY_QUERYINTERFACE_ENTRY(i) + +#define MY_QUERYINTERFACE_END return E_NOINTERFACE; } + +#define MY_ADDREF_RELEASE \ +STDMETHOD_(ULONG, AddRef)() { return ++__m_RefCount; } \ +STDMETHOD_(ULONG, Release)() { if (--__m_RefCount != 0) \ + return __m_RefCount; delete this; return 0; } + +#define MY_UNKNOWN_IMP_SPEC(i) \ + MY_QUERYINTERFACE_BEGIN \ + i \ + MY_QUERYINTERFACE_END \ + MY_ADDREF_RELEASE + + +#define MY_UNKNOWN_IMP MY_QUERYINTERFACE_BEGIN \ + MY_QUERYINTERFACE_ENTRY_UNKNOWN(IUnknown) \ + MY_QUERYINTERFACE_END \ + MY_ADDREF_RELEASE + +#define MY_UNKNOWN_IMP1(i) MY_UNKNOWN_IMP_SPEC( \ + MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \ + MY_QUERYINTERFACE_ENTRY(i) \ + ) + +#define MY_UNKNOWN_IMP2(i1, i2) MY_UNKNOWN_IMP_SPEC( \ + MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ + MY_QUERYINTERFACE_ENTRY(i1) \ + MY_QUERYINTERFACE_ENTRY(i2) \ + ) + +#define MY_UNKNOWN_IMP3(i1, i2, i3) MY_UNKNOWN_IMP_SPEC( \ + MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ + MY_QUERYINTERFACE_ENTRY(i1) \ + MY_QUERYINTERFACE_ENTRY(i2) \ + MY_QUERYINTERFACE_ENTRY(i3) \ + ) + +#define MY_UNKNOWN_IMP4(i1, i2, i3, i4) MY_UNKNOWN_IMP_SPEC( \ + MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ + MY_QUERYINTERFACE_ENTRY(i1) \ + MY_QUERYINTERFACE_ENTRY(i2) \ + MY_QUERYINTERFACE_ENTRY(i3) \ + MY_QUERYINTERFACE_ENTRY(i4) \ + ) + +#define MY_UNKNOWN_IMP5(i1, i2, i3, i4, i5) MY_UNKNOWN_IMP_SPEC( \ + MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ + MY_QUERYINTERFACE_ENTRY(i1) \ + MY_QUERYINTERFACE_ENTRY(i2) \ + MY_QUERYINTERFACE_ENTRY(i3) \ + MY_QUERYINTERFACE_ENTRY(i4) \ + MY_QUERYINTERFACE_ENTRY(i5) \ + ) + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyException.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyException.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +// Common/Exception.h + +#ifndef __COMMON_EXCEPTION_H +#define __COMMON_EXCEPTION_H + +#include "MyWindows.h" + +struct CSystemException +{ + HRESULT ErrorCode; + CSystemException(HRESULT errorCode): ErrorCode(errorCode) {} +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyInitGuid.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyInitGuid.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,15 @@ +// Common/MyInitGuid.h + +#ifndef __COMMON_MYINITGUID_H +#define __COMMON_MYINITGUID_H + +#ifdef _WIN32 +#include +#else +#define INITGUID +#include "MyGuidDef.h" +DEFINE_GUID(IID_IUnknown, +0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyMap.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyMap.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,140 @@ +// MyMap.cpp + +#include "StdAfx.h" + +#include "MyMap.h" + +static const unsigned kNumBitsMax = sizeof(UInt32) * 8; + +static UInt32 GetSubBits(UInt32 value, unsigned startPos, unsigned numBits) +{ + if (startPos == sizeof(value) * 8) + return 0; + value >>= startPos; + if (numBits == sizeof(value) * 8) + return value; + return value & (((UInt32)1 << numBits) - 1); +} + +static inline unsigned GetSubBit(UInt32 v, unsigned n) { return (unsigned)(v >> n) & 1; } + +bool CMap32::Find(UInt32 key, UInt32 &valueRes) const +{ + valueRes = (UInt32)(Int32)-1; + if (Nodes.Size() == 0) + return false; + if (Nodes.Size() == 1) + { + const CNode &n = Nodes[0]; + if (n.Len == kNumBitsMax) + { + valueRes = n.Values[0]; + return (key == n.Key); + } + } + + int cur = 0; + unsigned bitPos = kNumBitsMax; + for (;;) + { + const CNode &n = Nodes[cur]; + bitPos -= n.Len; + if (GetSubBits(key, bitPos, n.Len) != GetSubBits(n.Key, bitPos, n.Len)) + return false; + unsigned bit = GetSubBit(key, --bitPos); + if (n.IsLeaf[bit]) + { + valueRes = n.Values[bit]; + return (key == n.Keys[bit]); + } + cur = (int)n.Keys[bit]; + } +} + +bool CMap32::Set(UInt32 key, UInt32 value) +{ + if (Nodes.Size() == 0) + { + CNode n; + n.Key = n.Keys[0] = n.Keys[1] = key; + n.Values[0] = n.Values[1] = value; + n.IsLeaf[0] = n.IsLeaf[1] = 1; + n.Len = kNumBitsMax; + Nodes.Add(n); + return false; + } + if (Nodes.Size() == 1) + { + CNode &n = Nodes[0]; + if (n.Len == kNumBitsMax) + { + if (key == n.Key) + { + n.Values[0] = n.Values[1] = value; + return true; + } + unsigned i = kNumBitsMax - 1; + for (;GetSubBit(key, i) == GetSubBit(n.Key, i); i--); + n.Len = (UInt16)(kNumBitsMax - (1 + i)); + unsigned newBit = GetSubBit(key, i); + n.Values[newBit] = value; + n.Keys[newBit] = key; + return false; + } + } + + int cur = 0; + unsigned bitPos = kNumBitsMax; + for (;;) + { + CNode &n = Nodes[cur]; + bitPos -= n.Len; + if (GetSubBits(key, bitPos, n.Len) != GetSubBits(n.Key, bitPos, n.Len)) + { + unsigned i = n.Len - 1; + for (; GetSubBit(key, bitPos + i) == GetSubBit(n.Key, bitPos + i); i--); + + CNode e2(n); + e2.Len = (UInt16)i; + + n.Len = (UInt16)(n.Len - (1 + i)); + unsigned newBit = GetSubBit(key, bitPos + i); + n.Values[newBit] = value; + n.IsLeaf[newBit] = 1; + n.IsLeaf[1 - newBit] = 0; + n.Keys[newBit] = key; + n.Keys[1 - newBit] = Nodes.Size(); + Nodes.Add(e2); + return false; + } + unsigned bit = GetSubBit(key, --bitPos); + + if (n.IsLeaf[bit]) + { + if (key == n.Keys[bit]) + { + n.Values[bit] = value; + return true; + } + unsigned i = bitPos - 1; + for (;GetSubBit(key, i) == GetSubBit(n.Keys[bit], i); i--); + + CNode e2; + + unsigned newBit = GetSubBit(key, i); + e2.Values[newBit] = value; + e2.Values[1 - newBit] = n.Values[bit]; + e2.IsLeaf[newBit] = e2.IsLeaf[1 - newBit] = 1; + e2.Keys[newBit] = key; + e2.Keys[1 - newBit] = e2.Key = n.Keys[bit]; + e2.Len = (UInt16)(bitPos - (1 + i)); + + n.IsLeaf[bit] = 0; + n.Keys[bit] = Nodes.Size(); + + Nodes.Add(e2); + return false; + } + cur = (int)n.Keys[bit]; + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyMap.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyMap.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,28 @@ +// MyMap.h + +#ifndef __COMMON_MYMAP_H +#define __COMMON_MYMAP_H + +#include "MyVector.h" +#include "Types.h" + +class CMap32 +{ + struct CNode + { + UInt32 Key; + UInt32 Keys[2]; + UInt32 Values[2]; + UInt16 Len; + Byte IsLeaf[2]; + }; + CRecordVector Nodes; + +public: + + void Clear() { Nodes.Clear(); } + bool Find(UInt32 key, UInt32 &valueRes) const; + bool Set(UInt32 key, UInt32 value); // returns true, if there is such key already +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyString.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyString.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,198 @@ +// Common/MyString.cpp + +#include "StdAfx.h" + +#ifdef _WIN32 +#include "StringConvert.h" +#else +#include +#endif + +#include "MyString.h" + + +#ifdef _WIN32 + +#ifndef _UNICODE + +wchar_t MyCharUpper(wchar_t c) +{ + if (c == 0) + return 0; + wchar_t *res = CharUpperW((LPWSTR)(UINT_PTR)(unsigned int)c); + if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) + return (wchar_t)(unsigned int)(UINT_PTR)res; + const int kBufferSize = 4; + char s[kBufferSize + 1]; + int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0); + if (numChars == 0 || numChars > kBufferSize) + return c; + s[numChars] = 0; + ::CharUpperA(s); + ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1); + return c; +} + +wchar_t MyCharLower(wchar_t c) +{ + if (c == 0) + return 0; + wchar_t *res = CharLowerW((LPWSTR)(UINT_PTR)(unsigned int)c); + if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) + return (wchar_t)(unsigned int)(UINT_PTR)res; + const int kBufferSize = 4; + char s[kBufferSize + 1]; + int numChars = ::WideCharToMultiByte(CP_ACP, 0, &c, 1, s, kBufferSize, 0, 0); + if (numChars == 0 || numChars > kBufferSize) + return c; + s[numChars] = 0; + ::CharLowerA(s); + ::MultiByteToWideChar(CP_ACP, 0, s, numChars, &c, 1); + return c; +} + +wchar_t * MyStringUpper(wchar_t *s) +{ + if (s == 0) + return 0; + wchar_t *res = CharUpperW(s); + if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) + return res; + AString a = UnicodeStringToMultiByte(s); + a.MakeUpper(); + return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a)); +} + +wchar_t * MyStringLower(wchar_t *s) +{ + if (s == 0) + return 0; + wchar_t *res = CharLowerW(s); + if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) + return res; + AString a = UnicodeStringToMultiByte(s); + a.MakeLower(); + return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a)); +} + +#endif + +/* +inline int ConvertCompareResult(int r) { return r - 2; } + +int MyStringCollate(const wchar_t *s1, const wchar_t *s2) +{ + int res = CompareStringW( + LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1); + #ifdef _UNICODE + return ConvertCompareResult(res); + #else + if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) + return ConvertCompareResult(res); + return MyStringCollate(UnicodeStringToMultiByte(s1), + UnicodeStringToMultiByte(s2)); + #endif +} + +#ifndef _WIN32_WCE +int MyStringCollate(const char *s1, const char *s2) +{ + return ConvertCompareResult(CompareStringA( + LOCALE_USER_DEFAULT, SORT_STRINGSORT, s1, -1, s2, -1)); +} + +int MyStringCollateNoCase(const char *s1, const char *s2) +{ + return ConvertCompareResult(CompareStringA( + LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1)); +} +#endif + +int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2) +{ + int res = CompareStringW( + LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, s1, -1, s2, -1); + #ifdef _UNICODE + return ConvertCompareResult(res); + #else + if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) + return ConvertCompareResult(res); + return MyStringCollateNoCase(UnicodeStringToMultiByte(s1), + UnicodeStringToMultiByte(s2)); + #endif +} +*/ + +#else + +wchar_t MyCharUpper(wchar_t c) +{ + return toupper(c); +} + +/* +int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2) +{ + for (;;) + { + wchar_t c1 = *s1++; + wchar_t c2 = *s2++; + wchar_t u1 = MyCharUpper(c1); + wchar_t u2 = MyCharUpper(c2); + + if (u1 < u2) return -1; + if (u1 > u2) return 1; + if (u1 == 0) return 0; + } +} +*/ + +#endif + +int MyStringCompare(const char *s1, const char *s2) +{ + for (;;) + { + unsigned char c1 = (unsigned char)*s1++; + unsigned char c2 = (unsigned char)*s2++; + if (c1 < c2) return -1; + if (c1 > c2) return 1; + if (c1 == 0) return 0; + } +} + +int MyStringCompare(const wchar_t *s1, const wchar_t *s2) +{ + for (;;) + { + wchar_t c1 = *s1++; + wchar_t c2 = *s2++; + if (c1 < c2) return -1; + if (c1 > c2) return 1; + if (c1 == 0) return 0; + } +} + +int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2) +{ + for (;;) + { + wchar_t c1 = *s1++; + wchar_t c2 = *s2++; + if (c1 != c2) + { + wchar_t u1 = MyCharUpper(c1); + wchar_t u2 = MyCharUpper(c2); + if (u1 < u2) return -1; + if (u1 > u2) return 1; + } + if (c1 == 0) return 0; + } +} + +#ifdef _WIN32 +int MyStringCompareNoCase(const char *s1, const char *s2) +{ + return MyStringCompareNoCase(MultiByteToUnicodeString(s1), MultiByteToUnicodeString(s2)); +} +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyString.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyString.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,631 @@ +// Common/String.h + +#ifndef __COMMON_STRING_H +#define __COMMON_STRING_H + +#include +// #include + +#include "MyVector.h" + +#ifdef _WIN32 +#include "MyWindows.h" +#endif + +template +inline int MyStringLen(const T *s) +{ + int i; + for (i = 0; s[i] != '\0'; i++); + return i; +} + +template +inline T * MyStringCopy(T *dest, const T *src) +{ + T *destStart = dest; + while ((*dest++ = *src++) != 0); + return destStart; +} + +inline wchar_t* MyStringGetNextCharPointer(wchar_t *p) + { return (p + 1); } +inline const wchar_t* MyStringGetNextCharPointer(const wchar_t *p) + { return (p + 1); } +inline wchar_t* MyStringGetPrevCharPointer(const wchar_t *, wchar_t *p) + { return (p - 1); } +inline const wchar_t* MyStringGetPrevCharPointer(const wchar_t *, const wchar_t *p) + { return (p - 1); } + +#ifdef _WIN32 + +inline char* MyStringGetNextCharPointer(char *p) + { return CharNextA(p); } +inline const char* MyStringGetNextCharPointer(const char *p) + { return CharNextA(p); } + +inline char* MyStringGetPrevCharPointer(char *base, char *p) + { return CharPrevA(base, p); } +inline const char* MyStringGetPrevCharPointer(const char *base, const char *p) + { return CharPrevA(base, p); } + +inline char MyCharUpper(char c) + { return (char)(unsigned int)(UINT_PTR)CharUpperA((LPSTR)(UINT_PTR)(unsigned int)(unsigned char)c); } +#ifdef _UNICODE +inline wchar_t MyCharUpper(wchar_t c) + { return (wchar_t)(unsigned int)(UINT_PTR)CharUpperW((LPWSTR)(UINT_PTR)(unsigned int)c); } +#else +wchar_t MyCharUpper(wchar_t c); +#endif + +inline char MyCharLower(char c) + { return (char)(unsigned int)(UINT_PTR)CharLowerA((LPSTR)(UINT_PTR)(unsigned int)(unsigned char)c); } +#ifdef _UNICODE +inline wchar_t MyCharLower(wchar_t c) + { return (wchar_t)(unsigned int)(UINT_PTR)CharLowerW((LPWSTR)(UINT_PTR)(unsigned int)c); } +#else +wchar_t MyCharLower(wchar_t c); +#endif + +inline char * MyStringUpper(char *s) { return CharUpperA(s); } +#ifdef _UNICODE +inline wchar_t * MyStringUpper(wchar_t *s) { return CharUpperW(s); } +#else +wchar_t * MyStringUpper(wchar_t *s); +#endif + +inline char * MyStringLower(char *s) { return CharLowerA(s); } +#ifdef _UNICODE +inline wchar_t * MyStringLower(wchar_t *s) { return CharLowerW(s); } +#else +wchar_t * MyStringLower(wchar_t *s); +#endif + +#else // Standard-C +wchar_t MyCharUpper(wchar_t c); +#endif + +////////////////////////////////////// +// Compare + +/* +#ifndef _WIN32_WCE +int MyStringCollate(const char *s1, const char *s2); +int MyStringCollateNoCase(const char *s1, const char *s2); +#endif +int MyStringCollate(const wchar_t *s1, const wchar_t *s2); +int MyStringCollateNoCase(const wchar_t *s1, const wchar_t *s2); +*/ + +int MyStringCompare(const char *s1, const char *s2); +int MyStringCompare(const wchar_t *s1, const wchar_t *s2); + +#ifdef _WIN32 +int MyStringCompareNoCase(const char *s1, const char *s2); +#endif + +int MyStringCompareNoCase(const wchar_t *s1, const wchar_t *s2); + +template +class CStringBase +{ + void TrimLeftWithCharSet(const CStringBase &charSet) + { + const T *p = _chars; + while (charSet.Find(*p) >= 0 && (*p != 0)) + p = GetNextCharPointer(p); + Delete(0, (int)(p - _chars)); + } + void TrimRightWithCharSet(const CStringBase &charSet) + { + const T *p = _chars; + const T *pLast = NULL; + while (*p != 0) + { + if (charSet.Find(*p) >= 0) + { + if (pLast == NULL) + pLast = p; + } + else + pLast = NULL; + p = GetNextCharPointer(p); + } + if (pLast != NULL) + { + int i = (int)(pLast - _chars); + Delete(i, _length - i); + } + + } + void MoveItems(int destIndex, int srcIndex) + { + memmove(_chars + destIndex, _chars + srcIndex, + sizeof(T) * (_length - srcIndex + 1)); + } + + void InsertSpace(int &index, int size) + { + CorrectIndex(index); + GrowLength(size); + MoveItems(index + size, index); + } + + static T *GetNextCharPointer(T *p) + { return MyStringGetNextCharPointer(p); } + static const T *GetNextCharPointer(const T *p) + { return MyStringGetNextCharPointer(p); } + static T *GetPrevCharPointer(T *base, T *p) + { return MyStringGetPrevCharPointer(base, p); } + static const T *GetPrevCharPointer(const T *base, const T *p) + { return MyStringGetPrevCharPointer(base, p); } +protected: + T *_chars; + int _length; + int _capacity; + + void SetCapacity(int newCapacity) + { + int realCapacity = newCapacity + 1; + if (realCapacity == _capacity) + return; + /* + const int kMaxStringSize = 0x20000000; + #ifndef _WIN32_WCE + if (newCapacity > kMaxStringSize || newCapacity < _length) + throw 1052337; + #endif + */ + T *newBuffer = new T[realCapacity]; + if (_capacity > 0) + { + for (int i = 0; i < _length; i++) + newBuffer[i] = _chars[i]; + delete []_chars; + } + _chars = newBuffer; + _chars[_length] = 0; + _capacity = realCapacity; + } + + void GrowLength(int n) + { + int freeSize = _capacity - _length - 1; + if (n <= freeSize) + return; + int delta; + if (_capacity > 64) + delta = _capacity / 2; + else if (_capacity > 8) + delta = 16; + else + delta = 4; + if (freeSize + delta < n) + delta = n - freeSize; + SetCapacity(_capacity + delta); + } + + void CorrectIndex(int &index) const + { + if (index > _length) + index = _length; + } + +public: + CStringBase(): _chars(0), _length(0), _capacity(0) { SetCapacity(3); } + CStringBase(T c): _chars(0), _length(0), _capacity(0) + { + SetCapacity(1); + _chars[0] = c; + _chars[1] = 0; + _length = 1; + } + CStringBase(const T *chars): _chars(0), _length(0), _capacity(0) + { + int length = MyStringLen(chars); + SetCapacity(length); + MyStringCopy(_chars, chars); // can be optimized by memove() + _length = length; + } + CStringBase(const CStringBase &s): _chars(0), _length(0), _capacity(0) + { + SetCapacity(s._length); + MyStringCopy(_chars, s._chars); + _length = s._length; + } + ~CStringBase() { delete []_chars; } + + operator const T*() const { return _chars;} + + // The minimum size of the character buffer in characters. + // This value does not include space for a null terminator. + T* GetBuffer(int minBufLength) + { + if (minBufLength >= _capacity) + SetCapacity(minBufLength); + return _chars; + } + void ReleaseBuffer() { ReleaseBuffer(MyStringLen(_chars)); } + void ReleaseBuffer(int newLength) + { + /* + #ifndef _WIN32_WCE + if (newLength >= _capacity) + throw 282217; + #endif + */ + _chars[newLength] = 0; + _length = newLength; + } + + CStringBase& operator=(T c) + { + Empty(); + SetCapacity(1); + _chars[0] = c; + _chars[1] = 0; + _length = 1; + return *this; + } + CStringBase& operator=(const T *chars) + { + Empty(); + int length = MyStringLen(chars); + SetCapacity(length); + MyStringCopy(_chars, chars); + _length = length; + return *this; + } + CStringBase& operator=(const CStringBase& s) + { + if (&s == this) + return *this; + Empty(); + SetCapacity(s._length); + MyStringCopy(_chars, s._chars); + _length = s._length; + return *this; + } + + CStringBase& operator+=(T c) + { + GrowLength(1); + _chars[_length] = c; + _chars[++_length] = 0; + return *this; + } + CStringBase& operator+=(const T *s) + { + int len = MyStringLen(s); + GrowLength(len); + MyStringCopy(_chars + _length, s); + _length += len; + return *this; + } + CStringBase& operator+=(const CStringBase &s) + { + GrowLength(s._length); + MyStringCopy(_chars + _length, s._chars); + _length += s._length; + return *this; + } + void Empty() + { + _length = 0; + _chars[0] = 0; + } + int Length() const { return _length; } + bool IsEmpty() const { return (_length == 0); } + + CStringBase Mid(int startIndex) const + { return Mid(startIndex, _length - startIndex); } + CStringBase Mid(int startIndex, int count ) const + { + if (startIndex + count > _length) + count = _length - startIndex; + + if (startIndex == 0 && startIndex + count == _length) + return *this; + + CStringBase result; + result.SetCapacity(count); + // MyStringNCopy(result._chars, _chars + startIndex, count); + for (int i = 0; i < count; i++) + result._chars[i] = _chars[startIndex + i]; + result._chars[count] = 0; + result._length = count; + return result; + } + CStringBase Left(int count) const + { return Mid(0, count); } + CStringBase Right(int count) const + { + if (count > _length) + count = _length; + return Mid(_length - count, count); + } + + void MakeUpper() + { MyStringUpper(_chars); } + void MakeLower() + { MyStringLower(_chars); } + + int Compare(const CStringBase& s) const + { return MyStringCompare(_chars, s._chars); } + + int Compare(const T *s) const + { return MyStringCompare(_chars, s); } + + int CompareNoCase(const CStringBase& s) const + { return MyStringCompareNoCase(_chars, s._chars); } + + int CompareNoCase(const T *s) const + { return MyStringCompareNoCase(_chars, s); } + + /* + int Collate(const CStringBase& s) const + { return MyStringCollate(_chars, s._chars); } + int CollateNoCase(const CStringBase& s) const + { return MyStringCollateNoCase(_chars, s._chars); } + */ + + int Find(T c) const { return Find(c, 0); } + int Find(T c, int startIndex) const + { + T *p = _chars + startIndex; + for (;;) + { + if (*p == c) + return (int)(p - _chars); + if (*p == 0) + return -1; + p = GetNextCharPointer(p); + } + } + int Find(const CStringBase &s) const { return Find(s, 0); } + int Find(const CStringBase &s, int startIndex) const + { + if (s.IsEmpty()) + return startIndex; + for (; startIndex < _length; startIndex++) + { + int j; + for (j = 0; j < s._length && startIndex + j < _length; j++) + if (_chars[startIndex+j] != s._chars[j]) + break; + if (j == s._length) + return startIndex; + } + return -1; + } + int ReverseFind(T c) const + { + if (_length == 0) + return -1; + T *p = _chars + _length - 1; + for (;;) + { + if (*p == c) + return (int)(p - _chars); + if (p == _chars) + return -1; + p = GetPrevCharPointer(_chars, p); + } + } + int FindOneOf(const CStringBase &s) const + { + for (int i = 0; i < _length; i++) + if (s.Find(_chars[i]) >= 0) + return i; + return -1; + } + + void TrimLeft(T c) + { + const T *p = _chars; + while (c == *p) + p = GetNextCharPointer(p); + Delete(0, p - _chars); + } + private: + CStringBase GetTrimDefaultCharSet() + { + CStringBase charSet; + charSet += (T)' '; + charSet += (T)'\n'; + charSet += (T)'\t'; + return charSet; + } + public: + + void TrimLeft() + { + TrimLeftWithCharSet(GetTrimDefaultCharSet()); + } + void TrimRight() + { + TrimRightWithCharSet(GetTrimDefaultCharSet()); + } + void TrimRight(T c) + { + const T *p = _chars; + const T *pLast = NULL; + while (*p != 0) + { + if (*p == c) + { + if (pLast == NULL) + pLast = p; + } + else + pLast = NULL; + p = GetNextCharPointer(p); + } + if (pLast != NULL) + { + int i = pLast - _chars; + Delete(i, _length - i); + } + } + void Trim() + { + TrimRight(); + TrimLeft(); + } + + int Insert(int index, T c) + { + InsertSpace(index, 1); + _chars[index] = c; + _length++; + return _length; + } + int Insert(int index, const CStringBase &s) + { + CorrectIndex(index); + if (s.IsEmpty()) + return _length; + int numInsertChars = s.Length(); + InsertSpace(index, numInsertChars); + for (int i = 0; i < numInsertChars; i++) + _chars[index + i] = s[i]; + _length += numInsertChars; + return _length; + } + + // !!!!!!!!!!!!!!! test it if newChar = '\0' + int Replace(T oldChar, T newChar) + { + if (oldChar == newChar) + return 0; + int number = 0; + int pos = 0; + while (pos < Length()) + { + pos = Find(oldChar, pos); + if (pos < 0) + break; + _chars[pos] = newChar; + pos++; + number++; + } + return number; + } + int Replace(const CStringBase &oldString, const CStringBase &newString) + { + if (oldString.IsEmpty()) + return 0; + if (oldString == newString) + return 0; + int oldStringLength = oldString.Length(); + int newStringLength = newString.Length(); + int number = 0; + int pos = 0; + while (pos < _length) + { + pos = Find(oldString, pos); + if (pos < 0) + break; + Delete(pos, oldStringLength); + Insert(pos, newString); + pos += newStringLength; + number++; + } + return number; + } + int Delete(int index, int count = 1 ) + { + if (index + count > _length) + count = _length - index; + if (count > 0) + { + MoveItems(index, index + count); + _length -= count; + } + return _length; + } +}; + +template +CStringBase operator+(const CStringBase& s1, const CStringBase& s2) +{ + CStringBase result(s1); + result += s2; + return result; +} + +template +CStringBase operator+(const CStringBase& s, T c) +{ + CStringBase result(s); + result += c; + return result; +} + +template +CStringBase operator+(T c, const CStringBase& s) +{ + CStringBase result(c); + result += s; + return result; +} + +template +CStringBase operator+(const CStringBase& s, const T * chars) +{ + CStringBase result(s); + result += chars; + return result; +} + +template +CStringBase operator+(const T * chars, const CStringBase& s) +{ + CStringBase result(chars); + result += s; + return result; +} + +template +bool operator==(const CStringBase& s1, const CStringBase& s2) + { return (s1.Compare(s2) == 0); } + +template +bool operator<(const CStringBase& s1, const CStringBase& s2) + { return (s1.Compare(s2) < 0); } + +template +bool operator==(const T *s1, const CStringBase& s2) + { return (s2.Compare(s1) == 0); } + +template +bool operator==(const CStringBase& s1, const T *s2) + { return (s1.Compare(s2) == 0); } + +template +bool operator!=(const CStringBase& s1, const CStringBase& s2) + { return (s1.Compare(s2) != 0); } + +template +bool operator!=(const T *s1, const CStringBase& s2) + { return (s2.Compare(s1) != 0); } + +template +bool operator!=(const CStringBase& s1, const T *s2) + { return (s1.Compare(s2) != 0); } + +typedef CStringBase AString; +typedef CStringBase UString; + +typedef CObjectVector AStringVector; +typedef CObjectVector UStringVector; + +#ifdef _UNICODE + typedef UString CSysString; +#else + typedef AString CSysString; +#endif + +typedef CObjectVector CSysStringVector; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyUnknown.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyUnknown.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +// MyUnknown.h + +#ifndef __MYUNKNOWN_H +#define __MYUNKNOWN_H + +#ifdef _WIN32 + +#ifdef _WIN32_WCE +#if (_WIN32_WCE > 300) +#include +#else +#define MIDL_INTERFACE(x) struct +#endif +#else +#include +#endif + +#include + +#else +#include "MyWindows.h" +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyVector.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyVector.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,90 @@ +// Common/MyVector.cpp + +#include "StdAfx.h" + +#include + +#include "MyVector.h" + +CBaseRecordVector::~CBaseRecordVector() { ClearAndFree(); } + +void CBaseRecordVector::ClearAndFree() +{ + Clear(); + delete []((unsigned char *)_items); + _capacity = 0; + _size = 0; + _items = 0; +} + +void CBaseRecordVector::Clear() { DeleteFrom(0); } +void CBaseRecordVector::DeleteBack() { Delete(_size - 1); } +void CBaseRecordVector::DeleteFrom(int index) +{ + Delete(index, _size - index); +} + +void CBaseRecordVector::ReserveOnePosition() +{ + if (_size != _capacity) + return; + int delta = 1; + if (_capacity >= 64) + delta = _capacity / 4; + else if (_capacity >= 8) + delta = 8; + Reserve(_capacity + delta); +} + +void CBaseRecordVector::Reserve(int newCapacity) +{ + // if (newCapacity <= _capacity) + if (newCapacity == _capacity) + return; + if ((unsigned)newCapacity >= ((unsigned)1 << (sizeof(unsigned) * 8 - 1))) + throw 1052353; + size_t newSize = (size_t)(unsigned)newCapacity * _itemSize; + if (newSize / _itemSize != (size_t)(unsigned)newCapacity) + throw 1052354; + unsigned char *p = NULL; + if (newSize > 0) + { + p = new unsigned char[newSize]; + if (p == 0) + throw 1052355; + int numRecordsToMove = (_size < newCapacity ? _size : newCapacity); + memcpy(p, _items, _itemSize * numRecordsToMove); + } + delete [](unsigned char *)_items; + _items = p; + _capacity = newCapacity; +} + +void CBaseRecordVector::ReserveDown() +{ + Reserve(_size); +} + +void CBaseRecordVector::MoveItems(int destIndex, int srcIndex) +{ + memmove(((unsigned char *)_items) + destIndex * _itemSize, + ((unsigned char *)_items) + srcIndex * _itemSize, + _itemSize * (_size - srcIndex)); +} + +void CBaseRecordVector::InsertOneItem(int index) +{ + ReserveOnePosition(); + MoveItems(index + 1, index); + _size++; +} + +void CBaseRecordVector::Delete(int index, int num) +{ + TestIndexAndCorrectNum(index, num); + if (num > 0) + { + MoveItems(index, index + num); + _size -= num; + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyVector.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyVector.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,256 @@ +// Common/Vector.h + +#ifndef __COMMON_VECTOR_H +#define __COMMON_VECTOR_H + +#include "Defs.h" + +class CBaseRecordVector +{ + void MoveItems(int destIndex, int srcIndex); +protected: + int _capacity; + int _size; + void *_items; + size_t _itemSize; + + void ReserveOnePosition(); + void InsertOneItem(int index); + void TestIndexAndCorrectNum(int index, int &num) const + { if (index + num > _size) num = _size - index; } +public: + CBaseRecordVector(size_t itemSize): _capacity(0), _size(0), _items(0), _itemSize(itemSize) {} + virtual ~CBaseRecordVector(); + void ClearAndFree(); + int Size() const { return _size; } + bool IsEmpty() const { return (_size == 0); } + void Reserve(int newCapacity); + void ReserveDown(); + virtual void Delete(int index, int num = 1); + void Clear(); + void DeleteFrom(int index); + void DeleteBack(); +}; + +template +class CRecordVector: public CBaseRecordVector +{ +public: + CRecordVector(): CBaseRecordVector(sizeof(T)){}; + CRecordVector(const CRecordVector &v): CBaseRecordVector(sizeof(T)) { *this = v; } + CRecordVector& operator=(const CRecordVector &v) + { + Clear(); + return (*this += v); + } + CRecordVector& operator+=(const CRecordVector &v) + { + int size = v.Size(); + Reserve(Size() + size); + for (int i = 0; i < size; i++) + Add(v[i]); + return *this; + } + int Add(T item) + { + ReserveOnePosition(); + ((T *)_items)[_size] = item; + return _size++; + } + void Insert(int index, T item) + { + InsertOneItem(index); + ((T *)_items)[index] = item; + } + // T* GetPointer() const { return (T*)_items; } + // operator const T *() const { return _items; }; + const T& operator[](int index) const { return ((T *)_items)[index]; } + T& operator[](int index) { return ((T *)_items)[index]; } + const T& Front() const { return operator[](0); } + T& Front() { return operator[](0); } + const T& Back() const { return operator[](_size - 1); } + T& Back() { return operator[](_size - 1); } + + void Swap(int i, int j) + { + T temp = operator[](i); + operator[](i) = operator[](j); + operator[](j) = temp; + } + + int FindInSorted(const T& item) const + { + int left = 0, right = Size(); + while (left != right) + { + int mid = (left + right) / 2; + const T& midValue = (*this)[mid]; + if (item == midValue) + return mid; + if (item < midValue) + right = mid; + else + left = mid + 1; + } + return -1; + } + + int AddToUniqueSorted(const T& item) + { + int left = 0, right = Size(); + while (left != right) + { + int mid = (left + right) / 2; + const T& midValue = (*this)[mid]; + if (item == midValue) + return mid; + if (item < midValue) + right = mid; + else + left = mid + 1; + } + Insert(right, item); + return right; + } + + static void SortRefDown(T* p, int k, int size, int (*compare)(const T*, const T*, void *), void *param) + { + T temp = p[k]; + for (;;) + { + int s = (k << 1); + if (s > size) + break; + if (s < size && compare(p + s + 1, p + s, param) > 0) + s++; + if (compare(&temp, p + s, param) >= 0) + break; + p[k] = p[s]; + k = s; + } + p[k] = temp; + } + + void Sort(int (*compare)(const T*, const T*, void *), void *param) + { + int size = _size; + if (size <= 1) + return; + T* p = (&Front()) - 1; + { + int i = size / 2; + do + SortRefDown(p, i, size, compare, param); + while (--i != 0); + } + do + { + T temp = p[size]; + p[size--] = p[1]; + p[1] = temp; + SortRefDown(p, 1, size, compare, param); + } + while (size > 1); + } +}; + +typedef CRecordVector CIntVector; +typedef CRecordVector CUIntVector; +typedef CRecordVector CBoolVector; +typedef CRecordVector CByteVector; +typedef CRecordVector CPointerVector; + +template +class CObjectVector: public CPointerVector +{ +public: + CObjectVector() {}; + ~CObjectVector() { Clear(); }; + CObjectVector(const CObjectVector &v) { *this = v; } + CObjectVector& operator=(const CObjectVector &v) + { + Clear(); + return (*this += v); + } + CObjectVector& operator+=(const CObjectVector &v) + { + int size = v.Size(); + Reserve(Size() + size); + for (int i = 0; i < size; i++) + Add(v[i]); + return *this; + } + const T& operator[](int index) const + { + return *((T *)CPointerVector::operator[](index)); + } + T& operator[](int index) + { + return *((T *)CPointerVector::operator[](index)); + } + T& Front() { return operator[](0); } + const T& Front() const { return operator[](0); } + T& Back() { return operator[](_size - 1); } + const T& Back() const { return operator[](_size - 1); } + int Add(const T& item) { return CPointerVector::Add(new T(item)); } + void Insert(int index, const T& item) { CPointerVector::Insert(index, new T(item)); } + virtual void Delete(int index, int num = 1) + { + TestIndexAndCorrectNum(index, num); + for (int i = 0; i < num; i++) + delete (T *)(((void **)_items)[index + i]); + CPointerVector::Delete(index, num); + } + int Find(const T& item) const + { + for (int i = 0; i < Size(); i++) + if (item == (*this)[i]) + return i; + return -1; + } + int FindInSorted(const T& item) const + { + int left = 0, right = Size(); + while (left != right) + { + int mid = (left + right) / 2; + const T& midValue = (*this)[mid]; + if (item == midValue) + return mid; + if (item < midValue) + right = mid; + else + left = mid + 1; + } + return -1; + } + int AddToSorted(const T& item) + { + int left = 0, right = Size(); + while (left != right) + { + int mid = (left + right) / 2; + const T& midValue = (*this)[mid]; + if (item == midValue) + { + right = mid + 1; + break; + } + if (item < midValue) + right = mid; + else + left = mid + 1; + } + Insert(right, item); + return right; + } + + void Sort(int (*compare)(void *const *, void *const *, void *), void *param) + { CPointerVector::Sort(compare, param); } + + static int CompareObjectItems(void *const *a1, void *const *a2, void * /* param */) + { return MyCompare(*(*((const T **)a1)), *(*((const T **)a2))); } + void Sort() { CPointerVector::Sort(CompareObjectItems, 0); } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyWindows.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyWindows.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,214 @@ +// MyWindows.h + +#ifndef __MYWINDOWS_H +#define __MYWINDOWS_H + +#ifdef _WIN32 + +#include + +#define CHAR_PATH_SEPARATOR '\\' +#define WCHAR_PATH_SEPARATOR L'\\' +#define STRING_PATH_SEPARATOR "\\" +#define WSTRING_PATH_SEPARATOR L"\\" + +#else + +#define CHAR_PATH_SEPARATOR '/' +#define WCHAR_PATH_SEPARATOR L'/' +#define STRING_PATH_SEPARATOR "/" +#define WSTRING_PATH_SEPARATOR L"/" + +#include // for wchar_t +#include + +#include "MyGuidDef.h" + +typedef char CHAR; +typedef unsigned char UCHAR; + +#undef BYTE +typedef unsigned char BYTE; + +typedef short SHORT; +typedef unsigned short USHORT; + +#undef WORD +typedef unsigned short WORD; +typedef short VARIANT_BOOL; + +typedef int INT; +typedef Int32 INT32; +typedef unsigned int UINT; +typedef UInt32 UINT32; +typedef INT32 LONG; // LONG, ULONG and DWORD must be 32-bit +typedef UINT32 ULONG; + +#undef DWORD +typedef UINT32 DWORD; + +typedef Int64 LONGLONG; +typedef UInt64 ULONGLONG; + +typedef struct LARGE_INTEGER { LONGLONG QuadPart; }LARGE_INTEGER; +typedef struct _ULARGE_INTEGER { ULONGLONG QuadPart;} ULARGE_INTEGER; + +typedef const CHAR *LPCSTR; +typedef CHAR TCHAR; +typedef const TCHAR *LPCTSTR; +typedef wchar_t WCHAR; +typedef WCHAR OLECHAR; +typedef const WCHAR *LPCWSTR; +typedef OLECHAR *BSTR; +typedef const OLECHAR *LPCOLESTR; +typedef OLECHAR *LPOLESTR; + +typedef struct _FILETIME +{ + DWORD dwLowDateTime; + DWORD dwHighDateTime; +}FILETIME; + +#define HRESULT LONG +#define FAILED(Status) ((HRESULT)(Status)<0) +typedef ULONG PROPID; +typedef LONG SCODE; + +#define S_OK ((HRESULT)0x00000000L) +#define S_FALSE ((HRESULT)0x00000001L) +#define E_NOTIMPL ((HRESULT)0x80004001L) +#define E_NOINTERFACE ((HRESULT)0x80004002L) +#define E_ABORT ((HRESULT)0x80004004L) +#define E_FAIL ((HRESULT)0x80004005L) +#define STG_E_INVALIDFUNCTION ((HRESULT)0x80030001L) +#define E_OUTOFMEMORY ((HRESULT)0x8007000EL) +#define E_INVALIDARG ((HRESULT)0x80070057L) + +#ifdef _MSC_VER +#define STDMETHODCALLTYPE __stdcall +#else +#define STDMETHODCALLTYPE +#endif + +#define STDMETHOD_(t, f) virtual t STDMETHODCALLTYPE f +#define STDMETHOD(f) STDMETHOD_(HRESULT, f) +#define STDMETHODIMP_(type) type STDMETHODCALLTYPE +#define STDMETHODIMP STDMETHODIMP_(HRESULT) + +#define PURE = 0 + +#define MIDL_INTERFACE(x) struct + +#ifdef __cplusplus + +DEFINE_GUID(IID_IUnknown, +0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); +struct IUnknown +{ + STDMETHOD(QueryInterface) (REFIID iid, void **outObject) PURE; + STDMETHOD_(ULONG, AddRef)() PURE; + STDMETHOD_(ULONG, Release)() PURE; + #ifndef _WIN32 + virtual ~IUnknown() {} + #endif +}; + +typedef IUnknown *LPUNKNOWN; + +#endif + +#define VARIANT_TRUE ((VARIANT_BOOL)-1) +#define VARIANT_FALSE ((VARIANT_BOOL)0) + +enum VARENUM +{ + VT_EMPTY = 0, + VT_NULL = 1, + VT_I2 = 2, + VT_I4 = 3, + VT_R4 = 4, + VT_R8 = 5, + VT_CY = 6, + VT_DATE = 7, + VT_BSTR = 8, + VT_DISPATCH = 9, + VT_ERROR = 10, + VT_BOOL = 11, + VT_VARIANT = 12, + VT_UNKNOWN = 13, + VT_DECIMAL = 14, + VT_I1 = 16, + VT_UI1 = 17, + VT_UI2 = 18, + VT_UI4 = 19, + VT_I8 = 20, + VT_UI8 = 21, + VT_INT = 22, + VT_UINT = 23, + VT_VOID = 24, + VT_HRESULT = 25, + VT_FILETIME = 64 +}; + +typedef unsigned short VARTYPE; +typedef WORD PROPVAR_PAD1; +typedef WORD PROPVAR_PAD2; +typedef WORD PROPVAR_PAD3; + +#ifdef __cplusplus + +typedef struct tagPROPVARIANT +{ + VARTYPE vt; + PROPVAR_PAD1 wReserved1; + PROPVAR_PAD2 wReserved2; + PROPVAR_PAD3 wReserved3; + union + { + CHAR cVal; + UCHAR bVal; + SHORT iVal; + USHORT uiVal; + LONG lVal; + ULONG ulVal; + INT intVal; + UINT uintVal; + LARGE_INTEGER hVal; + ULARGE_INTEGER uhVal; + VARIANT_BOOL boolVal; + SCODE scode; + FILETIME filetime; + BSTR bstrVal; + }; +} PROPVARIANT; + +typedef PROPVARIANT tagVARIANT; +typedef tagVARIANT VARIANT; +typedef VARIANT VARIANTARG; + +MY_EXTERN_C HRESULT VariantClear(VARIANTARG *prop); +MY_EXTERN_C HRESULT VariantCopy(VARIANTARG *dest, VARIANTARG *src); + +#endif + +MY_EXTERN_C BSTR SysAllocStringByteLen(LPCSTR psz, UINT len); +MY_EXTERN_C BSTR SysAllocString(const OLECHAR *sz); +MY_EXTERN_C void SysFreeString(BSTR bstr); +MY_EXTERN_C UINT SysStringByteLen(BSTR bstr); +MY_EXTERN_C UINT SysStringLen(BSTR bstr); + +MY_EXTERN_C DWORD GetLastError(); +MY_EXTERN_C LONG CompareFileTime(const FILETIME* ft1, const FILETIME* ft2); + +#define CP_ACP 0 +#define CP_OEMCP 1 + +typedef enum tagSTREAM_SEEK +{ + STREAM_SEEK_SET = 0, + STREAM_SEEK_CUR = 1, + STREAM_SEEK_END = 2 +} STREAM_SEEK; + +#endif +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyXml.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyXml.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,209 @@ +// MyXml.cpp + +#include "StdAfx.h" + +#include "MyXml.h" + +static bool IsValidChar(char c) +{ + return + c >= 'a' && c <= 'z' || + c >= 'A' && c <= 'Z' || + c >= '0' && c <= '9' || + c == '-'; +} + +static bool IsSpaceChar(char c) +{ + return (c == ' ' || c == '\t' || c == 0x0D || c == 0x0A); +} + +#define SKEEP_SPACES(s, pos) while (IsSpaceChar(s[pos])) pos++; + +static bool ReadProperty(const AString &s, int &pos, CXmlProp &prop) +{ + prop.Name.Empty(); + prop.Value.Empty(); + for (; pos < s.Length(); pos++) + { + char c = s[pos]; + if (!IsValidChar(c)) + break; + prop.Name += c; + } + + if (prop.Name.IsEmpty()) + return false; + + SKEEP_SPACES(s, pos); + if (s[pos++] != '=') + return false; + + SKEEP_SPACES(s, pos); + if (s[pos++] != '\"') + return false; + + while (pos < s.Length()) + { + char c = s[pos++]; + if (c == '\"') + return true; + prop.Value += c; + } + return false; +} + +int CXmlItem::FindProperty(const AString &propName) const +{ + for (int i = 0; i < Props.Size(); i++) + if (Props[i].Name == propName) + return i; + return -1; +} + +AString CXmlItem::GetPropertyValue(const AString &propName) const +{ + int index = FindProperty(propName); + if (index >= 0) + return Props[index].Value; + return AString(); +} + +bool CXmlItem::IsTagged(const AString &tag) const +{ + return (IsTag && Name == tag); +} + +int CXmlItem::FindSubTag(const AString &tag) const +{ + for (int i = 0; i < SubItems.Size(); i++) + if (SubItems[i].IsTagged(tag)) + return i; + return -1; +} + +AString CXmlItem::GetSubString() const +{ + if (SubItems.Size() == 1) + { + const CXmlItem &item = SubItems[0]; + if (!item.IsTag) + return item.Name; + } + return AString(); +} + +AString CXmlItem::GetSubStringForTag(const AString &tag) const +{ + int index = FindSubTag(tag); + if (index >= 0) + return SubItems[index].GetSubString(); + return AString(); +} + +bool CXmlItem::ParseItems(const AString &s, int &pos, int numAllowedLevels) +{ + if (numAllowedLevels == 0) + return false; + SubItems.Clear(); + AString finishString = "'); + } + if (s[pos] == '>') + { + if (!ParseItems(s, ++pos, numAllowedLevels)) + return false; + AString finishString = AString(""); + if (s.Mid(pos, finishString.Length()) != finishString) + return false; + pos += finishString.Length(); + return true; + } + if (posTemp == pos) + return false; + + CXmlProp prop; + if (!ReadProperty(s, pos, prop)) + return false; + Props.Add(prop); + posTemp = pos; + } +} + +bool SkeepHeader(const AString &s, int &pos, const AString &startString, const AString &endString) +{ + SKEEP_SPACES(s, pos); + if (s.Mid(pos, startString.Length()) == startString) + { + pos = s.Find(endString, pos); + if (pos < 0) + return false; + pos += endString.Length(); + SKEEP_SPACES(s, pos); + } + return true; +} + +bool CXml::Parse(const AString &s) +{ + int pos = 0; + if (!SkeepHeader(s, pos, "")) + return false; + if (!SkeepHeader(s, pos, "")) + return false; + if (!Root.ParseItem(s, pos, 1000)) + return false; + SKEEP_SPACES(s, pos); + return (pos == s.Length() && Root.IsTag); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/MyXml.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/MyXml.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,40 @@ +// MyXml.h + +#ifndef __MYXML_H +#define __MYXML_H + +#include "MyString.h" + +struct CXmlProp +{ + AString Name; + AString Value; +}; + +class CXmlItem +{ + bool ParseItems(const AString &s, int &pos, int numAllowedLevels); + +public: + AString Name; + bool IsTag; + CObjectVector Props; + CObjectVector SubItems; + + bool ParseItem(const AString &s, int &pos, int numAllowedLevels); + + bool IsTagged(const AString &tag) const; + int FindProperty(const AString &propName) const; + AString GetPropertyValue(const AString &propName) const; + AString GetSubString() const; + int FindSubTag(const AString &tag) const; + AString GetSubStringForTag(const AString &tag) const; +}; + +struct CXml +{ + CXmlItem Root; + bool Parse(const AString &s); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/NewHandler.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/NewHandler.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,116 @@ +// NewHandler.cpp + +#include "StdAfx.h" + +#include + +#include "NewHandler.h" + +// #define DEBUG_MEMORY_LEAK + +#ifndef DEBUG_MEMORY_LEAK + +#ifdef _WIN32 +void * +#ifdef _MSC_VER +__cdecl +#endif +operator new(size_t size) +{ + // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size); + void *p = ::malloc(size); + if (p == 0) + throw CNewException(); + return p; +} + +void +#ifdef _MSC_VER +__cdecl +#endif +operator delete(void *p) throw() +{ + /* + if (p == 0) + return; + ::HeapFree(::GetProcessHeap(), 0, p); + */ + ::free(p); +} +#endif + +#else + +#pragma init_seg(lib) +const int kDebugSize = 1000000; +static void *a[kDebugSize]; +static int index = 0; + +static int numAllocs = 0; +void * __cdecl operator new(size_t size) +{ + numAllocs++; + void *p = HeapAlloc(GetProcessHeap(), 0, size); + if (index == 40) + { + int t = 1; + } + if (index < kDebugSize) + { + a[index] = p; + index++; + } + if (p == 0) + throw CNewException(); + printf("Alloc %6d, size = %8d\n", numAllocs, size); + return p; +} + +class CC +{ +public: + CC() + { + for (int i = 0; i < kDebugSize; i++) + a[i] = 0; + } + ~CC() + { + for (int i = 0; i < kDebugSize; i++) + if (a[i] != 0) + return; + } +} g_CC; + + +void __cdecl operator delete(void *p) +{ + if (p == 0) + return; + /* + for (int i = 0; i < index; i++) + if (a[i] == p) + a[i] = 0; + */ + HeapFree(GetProcessHeap(), 0, p); + numAllocs--; + printf("Free %d\n", numAllocs); +} + +#endif + +/* +int MemErrorVC(size_t) +{ + throw CNewException(); + // return 1; +} +CNewHandlerSetter::CNewHandlerSetter() +{ + // MemErrorOldVCFunction = _set_new_handler(MemErrorVC); +} +CNewHandlerSetter::~CNewHandlerSetter() +{ + // _set_new_handler(MemErrorOldVCFunction); +} +*/ diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/NewHandler.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/NewHandler.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,16 @@ +// Common/NewHandler.h + +#ifndef __COMMON_NEWHANDLER_H +#define __COMMON_NEWHANDLER_H + +class CNewException {}; + +#ifdef _WIN32 +void +#ifdef _MSC_VER +__cdecl +#endif +operator delete(void *p) throw(); +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,9 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "MyWindows.h" +#include "NewHandler.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/StringConvert.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/StringConvert.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,102 @@ +// Common/StringConvert.cpp + +#include "StdAfx.h" + +#include "StringConvert.h" + +#ifndef _WIN32 +#include +#endif + +#ifdef _WIN32 +UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) +{ + UString resultString; + if (!srcString.IsEmpty()) + { + int numChars = MultiByteToWideChar(codePage, 0, srcString, + srcString.Length(), resultString.GetBuffer(srcString.Length()), + srcString.Length() + 1); + #ifndef _WIN32_WCE + if (numChars == 0) + throw 282228; + #endif + resultString.ReleaseBuffer(numChars); + } + return resultString; +} + +AString UnicodeStringToMultiByte(const UString &s, UINT codePage, char defaultChar, bool &defaultCharWasUsed) +{ + AString dest; + defaultCharWasUsed = false; + if (!s.IsEmpty()) + { + int numRequiredBytes = s.Length() * 2; + BOOL defUsed; + int numChars = WideCharToMultiByte(codePage, 0, s, s.Length(), + dest.GetBuffer(numRequiredBytes), numRequiredBytes + 1, + &defaultChar, &defUsed); + defaultCharWasUsed = (defUsed != FALSE); + #ifndef _WIN32_WCE + if (numChars == 0) + throw 282229; + #endif + dest.ReleaseBuffer(numChars); + } + return dest; +} + +AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage) +{ + bool defaultCharWasUsed; + return UnicodeStringToMultiByte(srcString, codePage, '_', defaultCharWasUsed); +} + +#ifndef _WIN32_WCE +AString SystemStringToOemString(const CSysString &srcString) +{ + AString result; + CharToOem(srcString, result.GetBuffer(srcString.Length() * 2)); + result.ReleaseBuffer(); + return result; +} +#endif + +#else + +UString MultiByteToUnicodeString(const AString &srcString, UINT codePage) +{ + UString resultString; + for (int i = 0; i < srcString.Length(); i++) + resultString += wchar_t(srcString[i]); + /* + if (!srcString.IsEmpty()) + { + int numChars = mbstowcs(resultString.GetBuffer(srcString.Length()), srcString, srcString.Length() + 1); + if (numChars < 0) throw "Your environment does not support UNICODE"; + resultString.ReleaseBuffer(numChars); + } + */ + return resultString; +} + +AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage) +{ + AString resultString; + for (int i = 0; i < srcString.Length(); i++) + resultString += char(srcString[i]); + /* + if (!srcString.IsEmpty()) + { + int numRequiredBytes = srcString.Length() * 6 + 1; + int numChars = wcstombs(resultString.GetBuffer(numRequiredBytes), srcString, numRequiredBytes); + if (numChars < 0) throw "Your environment does not support UNICODE"; + resultString.ReleaseBuffer(numChars); + } + */ + return resultString; +} + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/StringConvert.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/StringConvert.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,73 @@ +// Common/StringConvert.h + +#ifndef __COMMON_STRINGCONVERT_H +#define __COMMON_STRINGCONVERT_H + +#include "MyWindows.h" +#include "MyString.h" +#include "Types.h" + +UString MultiByteToUnicodeString(const AString &srcString, UINT codePage = CP_ACP); +AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage, char defaultChar, bool &defaultCharWasUsed); +AString UnicodeStringToMultiByte(const UString &srcString, UINT codePage = CP_ACP); + + +inline const wchar_t* GetUnicodeString(const wchar_t* unicodeString) + { return unicodeString; } +inline const UString& GetUnicodeString(const UString &unicodeString) + { return unicodeString; } +inline UString GetUnicodeString(const AString &ansiString) + { return MultiByteToUnicodeString(ansiString); } +inline UString GetUnicodeString(const AString &multiByteString, UINT codePage) + { return MultiByteToUnicodeString(multiByteString, codePage); } +inline const wchar_t* GetUnicodeString(const wchar_t* unicodeString, UINT) + { return unicodeString; } +inline const UString& GetUnicodeString(const UString &unicodeString, UINT) + { return unicodeString; } + +inline const char* GetAnsiString(const char* ansiString) + { return ansiString; } +inline const AString& GetAnsiString(const AString &ansiString) + { return ansiString; } +inline AString GetAnsiString(const UString &unicodeString) + { return UnicodeStringToMultiByte(unicodeString); } + +inline const char* GetOemString(const char* oemString) + { return oemString; } +inline const AString& GetOemString(const AString &oemString) + { return oemString; } +inline AString GetOemString(const UString &unicodeString) + { return UnicodeStringToMultiByte(unicodeString, CP_OEMCP); } + + +#ifdef _UNICODE + inline const wchar_t* GetSystemString(const wchar_t* unicodeString) + { return unicodeString;} + inline const UString& GetSystemString(const UString &unicodeString) + { return unicodeString;} + inline const wchar_t* GetSystemString(const wchar_t* unicodeString, UINT /* codePage */) + { return unicodeString;} + inline const UString& GetSystemString(const UString &unicodeString, UINT /* codePage */) + { return unicodeString;} + inline UString GetSystemString(const AString &multiByteString, UINT codePage) + { return MultiByteToUnicodeString(multiByteString, codePage);} + inline UString GetSystemString(const AString &multiByteString) + { return MultiByteToUnicodeString(multiByteString);} +#else + inline const char* GetSystemString(const char *ansiString) + { return ansiString; } + inline const AString& GetSystemString(const AString &multiByteString, UINT) + { return multiByteString; } + inline const char * GetSystemString(const char *multiByteString, UINT) + { return multiByteString; } + inline AString GetSystemString(const UString &unicodeString) + { return UnicodeStringToMultiByte(unicodeString); } + inline AString GetSystemString(const UString &unicodeString, UINT codePage) + { return UnicodeStringToMultiByte(unicodeString, codePage); } +#endif + +#ifndef _WIN32_WCE +AString SystemStringToOemString(const CSysString &srcString); +#endif + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/StringToInt.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/StringToInt.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,90 @@ +// Common/StringToInt.cpp + +#include "StdAfx.h" + +#include "StringToInt.h" + +UInt64 ConvertStringToUInt64(const char *s, const char **end) +{ + UInt64 result = 0; + for (;;) + { + char c = *s; + if (c < '0' || c > '9') + { + if (end != NULL) + *end = s; + return result; + } + result *= 10; + result += (c - '0'); + s++; + } +} + +UInt64 ConvertOctStringToUInt64(const char *s, const char **end) +{ + UInt64 result = 0; + for (;;) + { + char c = *s; + if (c < '0' || c > '7') + { + if (end != NULL) + *end = s; + return result; + } + result <<= 3; + result += (c - '0'); + s++; + } +} + +UInt64 ConvertHexStringToUInt64(const char *s, const char **end) +{ + UInt64 result = 0; + for (;;) + { + char c = *s; + UInt32 v; + if (c >= '0' && c <= '9') v = (c - '0'); + else if (c >= 'A' && c <= 'F') v = 10 + (c - 'A'); + else if (c >= 'a' && c <= 'f') v = 10 + (c - 'a'); + else + { + if (end != NULL) + *end = s; + return result; + } + result <<= 4; + result |= v; + s++; + } +} + + +UInt64 ConvertStringToUInt64(const wchar_t *s, const wchar_t **end) +{ + UInt64 result = 0; + for (;;) + { + wchar_t c = *s; + if (c < '0' || c > '9') + { + if (end != NULL) + *end = s; + return result; + } + result *= 10; + result += (c - '0'); + s++; + } +} + + +Int64 ConvertStringToInt64(const char *s, const char **end) +{ + if (*s == '-') + return -(Int64)ConvertStringToUInt64(s + 1, end); + return ConvertStringToUInt64(s, end); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/StringToInt.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/StringToInt.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +// Common/StringToInt.h + +#ifndef __COMMON_STRINGTOINT_H +#define __COMMON_STRINGTOINT_H + +#include +#include "Types.h" + +UInt64 ConvertStringToUInt64(const char *s, const char **end); +UInt64 ConvertOctStringToUInt64(const char *s, const char **end); +UInt64 ConvertHexStringToUInt64(const char *s, const char **end); +UInt64 ConvertStringToUInt64(const wchar_t *s, const wchar_t **end); + +Int64 ConvertStringToInt64(const char *s, const char **end); + +#endif + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/Types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/Types.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +// Common/Types.h + +#ifndef __COMMON_TYPES_H +#define __COMMON_TYPES_H + +extern "C" +{ +#include "../../C/Types.h" +} + +typedef int HRes; + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/UTFConvert.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/UTFConvert.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,145 @@ +// UTFConvert.cpp + +#include "StdAfx.h" + +#include "UTFConvert.h" +#include "Types.h" + +static const Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; + +static Bool Utf8_To_Utf16(wchar_t *dest, size_t *destLen, const char *src, size_t srcLen) +{ + size_t destPos = 0, srcPos = 0; + for (;;) + { + Byte c; + int numAdds; + if (srcPos == srcLen) + { + *destLen = destPos; + return True; + } + c = (Byte)src[srcPos++]; + + if (c < 0x80) + { + if (dest) + dest[destPos] = (wchar_t)c; + destPos++; + continue; + } + if (c < 0xC0) + break; + for (numAdds = 1; numAdds < 5; numAdds++) + if (c < kUtf8Limits[numAdds]) + break; + UInt32 value = (c - kUtf8Limits[numAdds - 1]); + + do + { + Byte c2; + if (srcPos == srcLen) + break; + c2 = (Byte)src[srcPos++]; + if (c2 < 0x80 || c2 >= 0xC0) + break; + value <<= 6; + value |= (c2 - 0x80); + } + while (--numAdds != 0); + + if (value < 0x10000) + { + if (dest) + dest[destPos] = (wchar_t)value; + destPos++; + } + else + { + value -= 0x10000; + if (value >= 0x100000) + break; + if (dest) + { + dest[destPos + 0] = (wchar_t)(0xD800 + (value >> 10)); + dest[destPos + 1] = (wchar_t)(0xDC00 + (value & 0x3FF)); + } + destPos += 2; + } + } + *destLen = destPos; + return False; +} + +static Bool Utf16_To_Utf8(char *dest, size_t *destLen, const wchar_t *src, size_t srcLen) +{ + size_t destPos = 0, srcPos = 0; + for (;;) + { + unsigned numAdds; + UInt32 value; + if (srcPos == srcLen) + { + *destLen = destPos; + return True; + } + value = src[srcPos++]; + if (value < 0x80) + { + if (dest) + dest[destPos] = (char)value; + destPos++; + continue; + } + if (value >= 0xD800 && value < 0xE000) + { + UInt32 c2; + if (value >= 0xDC00 || srcPos == srcLen) + break; + c2 = src[srcPos++]; + if (c2 < 0xDC00 || c2 >= 0xE000) + break; + value = ((value - 0xD800) << 10) | (c2 - 0xDC00); + } + for (numAdds = 1; numAdds < 5; numAdds++) + if (value < (((UInt32)1) << (numAdds * 5 + 6))) + break; + if (dest) + dest[destPos] = (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds))); + destPos++; + do + { + numAdds--; + if (dest) + dest[destPos] = (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F)); + destPos++; + } + while (numAdds != 0); + } + *destLen = destPos; + return False; +} + +bool ConvertUTF8ToUnicode(const AString &src, UString &dest) +{ + dest.Empty(); + size_t destLen = 0; + Utf8_To_Utf16(NULL, &destLen, src, src.Length()); + wchar_t *p = dest.GetBuffer((int)destLen); + Bool res = Utf8_To_Utf16(p, &destLen, src, src.Length()); + p[destLen] = 0; + dest.ReleaseBuffer(); + return res ? true : false; +} + +bool ConvertUnicodeToUTF8(const UString &src, AString &dest) +{ + dest.Empty(); + size_t destLen = 0; + Utf16_To_Utf8(NULL, &destLen, src, src.Length()); + char *p = dest.GetBuffer((int)destLen); + Bool res = Utf16_To_Utf8(p, &destLen, src, src.Length()); + p[destLen] = 0; + dest.ReleaseBuffer(); + return res ? true : false; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/UTFConvert.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/UTFConvert.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,11 @@ +// Common/UTFConvert.h + +#ifndef __COMMON_UTFCONVERT_H +#define __COMMON_UTFCONVERT_H + +#include "MyString.h" + +bool ConvertUTF8ToUnicode(const AString &utfString, UString &resultString); +bool ConvertUnicodeToUTF8(const UString &unicodeString, AString &resultString); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/Wildcard.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/Wildcard.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,458 @@ +// Common/Wildcard.cpp + +#include "StdAfx.h" + +#include "Wildcard.h" + +bool g_CaseSensitive = + #ifdef _WIN32 + false; + #else + true; + #endif + +static const wchar_t kAnyCharsChar = L'*'; +static const wchar_t kAnyCharChar = L'?'; + +#ifdef _WIN32 +static const wchar_t kDirDelimiter1 = L'\\'; +#endif +static const wchar_t kDirDelimiter2 = L'/'; + +static const UString kWildCardCharSet = L"?*"; + +static const UString kIllegalWildCardFileNameChars= + L"\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA\xB\xC\xD\xE\xF" + L"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F" + L"\"/:<>\\|"; + + +static inline bool IsCharDirLimiter(wchar_t c) +{ + return ( + #ifdef _WIN32 + c == kDirDelimiter1 || + #endif + c == kDirDelimiter2); +} + +int CompareFileNames(const UString &s1, const UString &s2) +{ + if (g_CaseSensitive) + return s1.Compare(s2); + return s1.CompareNoCase(s2); +} + +// ----------------------------------------- +// this function compares name with mask +// ? - any char +// * - any char or empty + +static bool EnhancedMaskTest(const wchar_t *mask, const wchar_t *name) +{ + for (;;) + { + wchar_t m = *mask; + wchar_t c = *name; + if (m == 0) + return (c == 0); + if (m == kAnyCharsChar) + { + if (EnhancedMaskTest(mask + 1, name)) + return true; + if (c == 0) + return false; + } + else + { + if (m == kAnyCharChar) + { + if (c == 0) + return false; + } + else if (m != c) + if (g_CaseSensitive || MyCharUpper(m) != MyCharUpper(c)) + return false; + mask++; + } + name++; + } +} + +// -------------------------------------------------- +// Splits path to strings + +void SplitPathToParts(const UString &path, UStringVector &pathParts) +{ + pathParts.Clear(); + UString name; + int len = path.Length(); + if (len == 0) + return; + for (int i = 0; i < len; i++) + { + wchar_t c = path[i]; + if (IsCharDirLimiter(c)) + { + pathParts.Add(name); + name.Empty(); + } + else + name += c; + } + pathParts.Add(name); +} + +void SplitPathToParts(const UString &path, UString &dirPrefix, UString &name) +{ + int i; + for (i = path.Length() - 1; i >= 0; i--) + if (IsCharDirLimiter(path[i])) + break; + dirPrefix = path.Left(i + 1); + name = path.Mid(i + 1); +} + +UString ExtractDirPrefixFromPath(const UString &path) +{ + int i; + for (i = path.Length() - 1; i >= 0; i--) + if (IsCharDirLimiter(path[i])) + break; + return path.Left(i + 1); +} + +UString ExtractFileNameFromPath(const UString &path) +{ + int i; + for (i = path.Length() - 1; i >= 0; i--) + if (IsCharDirLimiter(path[i])) + break; + return path.Mid(i + 1); +} + + +bool CompareWildCardWithName(const UString &mask, const UString &name) +{ + return EnhancedMaskTest(mask, name); +} + +bool DoesNameContainWildCard(const UString &path) +{ + return (path.FindOneOf(kWildCardCharSet) >= 0); +} + + +// ----------------------------------------------------------' +// NWildcard + +namespace NWildcard { + + +/* +M = MaskParts.Size(); +N = TestNameParts.Size(); + + File Dir +ForFile req M<=N [N-M, N) - + nonreq M=N [0, M) - + +ForDir req M 1) + return true; + } + return false; +} + +bool CCensorNode::AreThereIncludeItems() const +{ + if (IncludeItems.Size() > 0) + return true; + for (int i = 0; i < SubNodes.Size(); i++) + if (SubNodes[i].AreThereIncludeItems()) + return true; + return false; +} + +bool CCensorNode::CheckPathCurrent(bool include, const UStringVector &pathParts, bool isFile) const +{ + const CObjectVector &items = include ? IncludeItems : ExcludeItems; + for (int i = 0; i < items.Size(); i++) + if (items[i].CheckPath(pathParts, isFile)) + return true; + return false; +} + +bool CCensorNode::CheckPath(UStringVector &pathParts, bool isFile, bool &include) const +{ + if (CheckPathCurrent(false, pathParts, isFile)) + { + include = false; + return true; + } + include = true; + bool finded = CheckPathCurrent(true, pathParts, isFile); + if (pathParts.Size() == 1) + return finded; + int index = FindSubNode(pathParts.Front()); + if (index >= 0) + { + UStringVector pathParts2 = pathParts; + pathParts2.Delete(0); + if (SubNodes[index].CheckPath(pathParts2, isFile, include)) + return true; + } + return finded; +} + +bool CCensorNode::CheckPath(const UString &path, bool isFile, bool &include) const +{ + UStringVector pathParts; + SplitPathToParts(path, pathParts); + return CheckPath(pathParts, isFile, include); +} + +bool CCensorNode::CheckPath(const UString &path, bool isFile) const +{ + bool include; + if (CheckPath(path, isFile, include)) + return include; + return false; +} + +bool CCensorNode::CheckPathToRoot(bool include, UStringVector &pathParts, bool isFile) const +{ + if (CheckPathCurrent(include, pathParts, isFile)) + return true; + if (Parent == 0) + return false; + pathParts.Insert(0, Name); + return Parent->CheckPathToRoot(include, pathParts, isFile); +} + +/* +bool CCensorNode::CheckPathToRoot(bool include, const UString &path, bool isFile) const +{ + UStringVector pathParts; + SplitPathToParts(path, pathParts); + return CheckPathToRoot(include, pathParts, isFile); +} +*/ + +void CCensorNode::AddItem2(bool include, const UString &path, bool recursive) +{ + if (path.IsEmpty()) + return; + bool forFile = true; + bool forFolder = true; + UString path2 = path; + if (IsCharDirLimiter(path[path.Length() - 1])) + { + path2.Delete(path.Length() - 1); + forFile = false; + } + AddItem(include, path2, recursive, forFile, forFolder); +} + +void CCensorNode::ExtendExclude(const CCensorNode &fromNodes) +{ + ExcludeItems += fromNodes.ExcludeItems; + for (int i = 0; i < fromNodes.SubNodes.Size(); i++) + { + const CCensorNode &node = fromNodes.SubNodes[i]; + int subNodeIndex = FindSubNode(node.Name); + if (subNodeIndex < 0) + subNodeIndex = SubNodes.Add(CCensorNode(node.Name, this)); + SubNodes[subNodeIndex].ExtendExclude(node); + } +} + +int CCensor::FindPrefix(const UString &prefix) const +{ + for (int i = 0; i < Pairs.Size(); i++) + if (CompareFileNames(Pairs[i].Prefix, prefix) == 0) + return i; + return -1; +} + +void CCensor::AddItem(bool include, const UString &path, bool recursive) +{ + UStringVector pathParts; + SplitPathToParts(path, pathParts); + bool forFile = true; + if (pathParts.Back().IsEmpty()) + { + forFile = false; + pathParts.DeleteBack(); + } + const UString &front = pathParts.Front(); + bool isAbs = false; + if (front.IsEmpty()) + isAbs = true; + else if (front.Length() == 2 && front[1] == L':') + isAbs = true; + else + { + for (int i = 0; i < pathParts.Size(); i++) + { + const UString &part = pathParts[i]; + if (part == L".." || part == L".") + { + isAbs = true; + break; + } + } + } + int numAbsParts = 0; + if (isAbs) + if (pathParts.Size() > 1) + numAbsParts = pathParts.Size() - 1; + else + numAbsParts = 1; + UString prefix; + for (int i = 0; i < numAbsParts; i++) + { + const UString &front = pathParts.Front(); + if (DoesNameContainWildCard(front)) + break; + prefix += front; + prefix += WCHAR_PATH_SEPARATOR; + pathParts.Delete(0); + } + int index = FindPrefix(prefix); + if (index < 0) + index = Pairs.Add(CPair(prefix)); + + CItem item; + item.PathParts = pathParts; + item.ForDir = true; + item.ForFile = forFile; + item.Recursive = recursive; + Pairs[index].Head.AddItem(include, item); +} + +bool CCensor::CheckPath(const UString &path, bool isFile) const +{ + bool finded = false; + for (int i = 0; i < Pairs.Size(); i++) + { + bool include; + if (Pairs[i].Head.CheckPath(path, isFile, include)) + { + if (!include) + return false; + finded = true; + } + } + return finded; +} + +void CCensor::ExtendExclude() +{ + int i; + for (i = 0; i < Pairs.Size(); i++) + if (Pairs[i].Prefix.IsEmpty()) + break; + if (i == Pairs.Size()) + return; + int index = i; + for (i = 0; i < Pairs.Size(); i++) + if (index != i) + Pairs[i].Head.ExtendExclude(Pairs[index].Head); +} + +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Common/Wildcard.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Common/Wildcard.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,80 @@ +// Common/Wildcard.h + +#ifndef __COMMON_WILDCARD_H +#define __COMMON_WILDCARD_H + +#include "MyString.h" + +int CompareFileNames(const UString &s1, const UString &s2); + +void SplitPathToParts(const UString &path, UStringVector &pathParts); +void SplitPathToParts(const UString &path, UString &dirPrefix, UString &name); +UString ExtractDirPrefixFromPath(const UString &path); +UString ExtractFileNameFromPath(const UString &path); +bool DoesNameContainWildCard(const UString &path); +bool CompareWildCardWithName(const UString &mask, const UString &name); + +namespace NWildcard { + +struct CItem +{ + UStringVector PathParts; + bool Recursive; + bool ForFile; + bool ForDir; + bool CheckPath(const UStringVector &pathParts, bool isFile) const; +}; + +class CCensorNode +{ + CCensorNode *Parent; + bool CheckPathCurrent(bool include, const UStringVector &pathParts, bool isFile) const; + void AddItemSimple(bool include, CItem &item); + bool CheckPath(UStringVector &pathParts, bool isFile, bool &include) const; +public: + CCensorNode(): Parent(0) { }; + CCensorNode(const UString &name, CCensorNode *parent): Name(name), Parent(parent) { }; + UString Name; + CObjectVector SubNodes; + CObjectVector IncludeItems; + CObjectVector ExcludeItems; + + int FindSubNode(const UString &path) const; + + void AddItem(bool include, CItem &item); + void AddItem(bool include, const UString &path, bool recursive, bool forFile, bool forDir); + void AddItem2(bool include, const UString &path, bool recursive); + + bool NeedCheckSubDirs() const; + bool AreThereIncludeItems() const; + + bool CheckPath(const UString &path, bool isFile, bool &include) const; + bool CheckPath(const UString &path, bool isFile) const; + + bool CheckPathToRoot(bool include, UStringVector &pathParts, bool isFile) const; + // bool CheckPathToRoot(const UString &path, bool isFile, bool include) const; + void ExtendExclude(const CCensorNode &fromNodes); +}; + +struct CPair +{ + UString Prefix; + CCensorNode Head; + CPair(const UString &prefix): Prefix(prefix) { }; +}; + +class CCensor +{ + int FindPrefix(const UString &prefix) const; +public: + CObjectVector Pairs; + bool AllAreRelative() const + { return (Pairs.Size() == 1 && Pairs.Front().Prefix.IsEmpty()); } + void AddItem(bool include, const UString &path, bool recursive); + bool CheckPath(const UString &path, bool isFile) const; + void ExtendExclude(); +}; + +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/Defs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/Defs.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,23 @@ +// Windows/Defs.h + +#ifndef __WINDOWS_DEFS_H +#define __WINDOWS_DEFS_H + +inline bool BOOLToBool(BOOL value) + { return (value != FALSE); } + +#ifdef _WIN32 +inline bool LRESULTToBool(LRESULT value) + { return (value != FALSE); } +#endif + +inline BOOL BoolToBOOL(bool value) + { return (value ? TRUE: FALSE); } + +inline VARIANT_BOOL BoolToVARIANT_BOOL(bool value) + { return (value ? VARIANT_TRUE: VARIANT_FALSE); } + +inline bool VARIANT_BOOLToBool(VARIANT_BOOL value) + { return (value != VARIANT_FALSE); } + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/FileDir.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/FileDir.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,841 @@ +// Windows/FileDir.cpp + +#include "StdAfx.h" + +#include "FileDir.h" +#include "FileName.h" +#include "FileFind.h" +#include "Defs.h" +#ifndef _UNICODE +#include "../Common/StringConvert.h" +#endif + +#ifndef _UNICODE +extern bool g_IsNT; +#endif + +namespace NWindows { +namespace NFile { + +#if defined(WIN_LONG_PATH) && defined(_UNICODE) +#define WIN_LONG_PATH2 +#endif + +// SetCurrentDirectory doesn't support \\?\ prefix + +#ifdef WIN_LONG_PATH +bool GetLongPathBase(LPCWSTR fileName, UString &res); +bool GetLongPath(LPCWSTR fileName, UString &res); +#endif + +namespace NDirectory { + +#ifndef _UNICODE +static inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; } +static UString GetUnicodePath(const CSysString &sysPath) + { return MultiByteToUnicodeString(sysPath, GetCurrentCodePage()); } +static CSysString GetSysPath(LPCWSTR sysPath) + { return UnicodeStringToMultiByte(sysPath, GetCurrentCodePage()); } +#endif + +bool MyGetWindowsDirectory(CSysString &path) +{ + UINT needLength = ::GetWindowsDirectory(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); + path.ReleaseBuffer(); + return (needLength > 0 && needLength <= MAX_PATH); +} + +bool MyGetSystemDirectory(CSysString &path) +{ + UINT needLength = ::GetSystemDirectory(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); + path.ReleaseBuffer(); + return (needLength > 0 && needLength <= MAX_PATH); +} + +#ifndef _UNICODE +bool MyGetWindowsDirectory(UString &path) +{ + if (g_IsNT) + { + UINT needLength = ::GetWindowsDirectoryW(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); + path.ReleaseBuffer(); + return (needLength > 0 && needLength <= MAX_PATH); + } + CSysString sysPath; + if (!MyGetWindowsDirectory(sysPath)) + return false; + path = GetUnicodePath(sysPath); + return true; +} + +bool MyGetSystemDirectory(UString &path) +{ + if (g_IsNT) + { + UINT needLength = ::GetSystemDirectoryW(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); + path.ReleaseBuffer(); + return (needLength > 0 && needLength <= MAX_PATH); + } + CSysString sysPath; + if (!MyGetSystemDirectory(sysPath)) + return false; + path = GetUnicodePath(sysPath); + return true; +} +#endif + +bool SetDirTime(LPCWSTR fileName, const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime) +{ + #ifndef _UNICODE + if (!g_IsNT) + { + ::SetLastError(ERROR_CALL_NOT_IMPLEMENTED); + return false; + } + #endif + HANDLE hDir = ::CreateFileW(fileName, GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + #ifdef WIN_LONG_PATH + if (hDir == INVALID_HANDLE_VALUE) + { + UString longPath; + if (GetLongPath(fileName, longPath)) + hDir = ::CreateFileW(longPath, GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + } + #endif + + bool res = false; + if (hDir != INVALID_HANDLE_VALUE) + { + res = BOOLToBool(::SetFileTime(hDir, cTime, aTime, mTime)); + ::CloseHandle(hDir); + } + return res; +} + +bool MySetFileAttributes(LPCTSTR fileName, DWORD fileAttributes) +{ + if (::SetFileAttributes(fileName, fileAttributes)) + return true; + #ifdef WIN_LONG_PATH2 + UString longPath; + if (GetLongPath(fileName, longPath)) + return BOOLToBool(::SetFileAttributesW(longPath, fileAttributes)); + #endif + return false; +} + +bool MyRemoveDirectory(LPCTSTR pathName) +{ + if (::RemoveDirectory(pathName)) + return true; + #ifdef WIN_LONG_PATH2 + UString longPath; + if (GetLongPath(pathName, longPath)) + return BOOLToBool(::RemoveDirectoryW(longPath)); + #endif + return false; +} + +#ifdef WIN_LONG_PATH +bool GetLongPaths(LPCWSTR s1, LPCWSTR s2, UString &d1, UString &d2) +{ + if (!GetLongPathBase(s1, d1) || !GetLongPathBase(s2, d2)) + return false; + if (d1.IsEmpty() && d2.IsEmpty()) return false; + if (d1.IsEmpty()) d1 = s1; + if (d2.IsEmpty()) d2 = s2; + return true; +} +#endif + +bool MyMoveFile(LPCTSTR existFileName, LPCTSTR newFileName) +{ + if (::MoveFile(existFileName, newFileName)) + return true; + #ifdef WIN_LONG_PATH2 + UString d1, d2; + if (GetLongPaths(existFileName, newFileName, d1, d2)) + return BOOLToBool(::MoveFileW(d1, d2)); + #endif + return false; +} + +#ifndef _UNICODE +bool MySetFileAttributes(LPCWSTR fileName, DWORD fileAttributes) +{ + if (!g_IsNT) + return MySetFileAttributes(GetSysPath(fileName), fileAttributes); + if (::SetFileAttributesW(fileName, fileAttributes)) + return true; + #ifdef WIN_LONG_PATH + UString longPath; + if (GetLongPath(fileName, longPath)) + return BOOLToBool(::SetFileAttributesW(longPath, fileAttributes)); + #endif + return false; +} + + +bool MyRemoveDirectory(LPCWSTR pathName) +{ + if (!g_IsNT) + return MyRemoveDirectory(GetSysPath(pathName)); + if (::RemoveDirectoryW(pathName)) + return true; + #ifdef WIN_LONG_PATH + UString longPath; + if (GetLongPath(pathName, longPath)) + return BOOLToBool(::RemoveDirectoryW(longPath)); + #endif + return false; +} + +bool MyMoveFile(LPCWSTR existFileName, LPCWSTR newFileName) +{ + if (!g_IsNT) + return MyMoveFile(GetSysPath(existFileName), GetSysPath(newFileName)); + if (::MoveFileW(existFileName, newFileName)) + return true; + #ifdef WIN_LONG_PATH + UString d1, d2; + if (GetLongPaths(existFileName, newFileName, d1, d2)) + return BOOLToBool(::MoveFileW(d1, d2)); + #endif + return false; +} +#endif + +bool MyCreateDirectory(LPCTSTR pathName) +{ + if (::CreateDirectory(pathName, NULL)) + return true; + #ifdef WIN_LONG_PATH2 + if (::GetLastError() != ERROR_ALREADY_EXISTS) + { + UString longPath; + if (GetLongPath(pathName, longPath)) + return BOOLToBool(::CreateDirectoryW(longPath, NULL)); + } + #endif + return false; +} + +#ifndef _UNICODE +bool MyCreateDirectory(LPCWSTR pathName) +{ + if (!g_IsNT) + return MyCreateDirectory(GetSysPath(pathName)); + if (::CreateDirectoryW(pathName, NULL)) + return true; + #ifdef WIN_LONG_PATH + if (::GetLastError() != ERROR_ALREADY_EXISTS) + { + UString longPath; + if (GetLongPath(pathName, longPath)) + return BOOLToBool(::CreateDirectoryW(longPath, NULL)); + } + #endif + return false; +} +#endif + +/* +bool CreateComplexDirectory(LPCTSTR pathName) +{ + NName::CParsedPath path; + path.ParsePath(pathName); + CSysString fullPath = path.Prefix; + DWORD errorCode = ERROR_SUCCESS; + for (int i = 0; i < path.PathParts.Size(); i++) + { + const CSysString &string = path.PathParts[i]; + if (string.IsEmpty()) + { + if (i != path.PathParts.Size() - 1) + return false; + return true; + } + fullPath += path.PathParts[i]; + if (!MyCreateDirectory(fullPath)) + { + DWORD errorCode = GetLastError(); + if (errorCode != ERROR_ALREADY_EXISTS) + return false; + } + fullPath += NName::kDirDelimiter; + } + return true; +} +*/ + +bool CreateComplexDirectory(LPCTSTR _aPathName) +{ + CSysString pathName = _aPathName; + int pos = pathName.ReverseFind(TEXT(CHAR_PATH_SEPARATOR)); + if (pos > 0 && pos == pathName.Length() - 1) + { + if (pathName.Length() == 3 && pathName[1] == ':') + return true; // Disk folder; + pathName.Delete(pos); + } + CSysString pathName2 = pathName; + pos = pathName.Length(); + for (;;) + { + if (MyCreateDirectory(pathName)) + break; + if (::GetLastError() == ERROR_ALREADY_EXISTS) + { + NFind::CFileInfo fileInfo; + if (!NFind::FindFile(pathName, fileInfo)) // For network folders + return true; + if (!fileInfo.IsDir()) + return false; + break; + } + pos = pathName.ReverseFind(TEXT(CHAR_PATH_SEPARATOR)); + if (pos < 0 || pos == 0) + return false; + if (pathName[pos - 1] == ':') + return false; + pathName = pathName.Left(pos); + } + pathName = pathName2; + while (pos < pathName.Length()) + { + pos = pathName.Find(TEXT(CHAR_PATH_SEPARATOR), pos + 1); + if (pos < 0) + pos = pathName.Length(); + if (!MyCreateDirectory(pathName.Left(pos))) + return false; + } + return true; +} + +#ifndef _UNICODE + +bool CreateComplexDirectory(LPCWSTR _aPathName) +{ + UString pathName = _aPathName; + int pos = pathName.ReverseFind(WCHAR_PATH_SEPARATOR); + if (pos > 0 && pos == pathName.Length() - 1) + { + if (pathName.Length() == 3 && pathName[1] == L':') + return true; // Disk folder; + pathName.Delete(pos); + } + UString pathName2 = pathName; + pos = pathName.Length(); + for (;;) + { + if (MyCreateDirectory(pathName)) + break; + if (::GetLastError() == ERROR_ALREADY_EXISTS) + { + NFind::CFileInfoW fileInfo; + if (!NFind::FindFile(pathName, fileInfo)) // For network folders + return true; + if (!fileInfo.IsDir()) + return false; + break; + } + pos = pathName.ReverseFind(WCHAR_PATH_SEPARATOR); + if (pos < 0 || pos == 0) + return false; + if (pathName[pos - 1] == L':') + return false; + pathName = pathName.Left(pos); + } + pathName = pathName2; + while (pos < pathName.Length()) + { + pos = pathName.Find(WCHAR_PATH_SEPARATOR, pos + 1); + if (pos < 0) + pos = pathName.Length(); + if (!MyCreateDirectory(pathName.Left(pos))) + return false; + } + return true; +} + +#endif + +bool DeleteFileAlways(LPCTSTR name) +{ + if (!MySetFileAttributes(name, 0)) + return false; + if (::DeleteFile(name)) + return true; + #ifdef WIN_LONG_PATH2 + UString longPath; + if (GetLongPath(name, longPath)) + return BOOLToBool(::DeleteFileW(longPath)); + #endif + return false; +} + +#ifndef _UNICODE +bool DeleteFileAlways(LPCWSTR name) +{ + if (!g_IsNT) + return DeleteFileAlways(GetSysPath(name)); + if (!MySetFileAttributes(name, 0)) + return false; + if (::DeleteFileW(name)) + return true; + #ifdef WIN_LONG_PATH + UString longPath; + if (GetLongPath(name, longPath)) + return BOOLToBool(::DeleteFileW(longPath)); + #endif + return false; +} +#endif + +static bool RemoveDirectorySubItems2(const CSysString pathPrefix, const NFind::CFileInfo &fileInfo) +{ + if (fileInfo.IsDir()) + return RemoveDirectoryWithSubItems(pathPrefix + fileInfo.Name); + return DeleteFileAlways(pathPrefix + fileInfo.Name); +} + +bool RemoveDirectoryWithSubItems(const CSysString &path) +{ + NFind::CFileInfo fileInfo; + CSysString pathPrefix = path + NName::kDirDelimiter; + { + NFind::CEnumerator enumerator(pathPrefix + TCHAR(NName::kAnyStringWildcard)); + while (enumerator.Next(fileInfo)) + if (!RemoveDirectorySubItems2(pathPrefix, fileInfo)) + return false; + } + if (!MySetFileAttributes(path, 0)) + return false; + return MyRemoveDirectory(path); +} + +#ifndef _UNICODE +static bool RemoveDirectorySubItems2(const UString pathPrefix, const NFind::CFileInfoW &fileInfo) +{ + if (fileInfo.IsDir()) + return RemoveDirectoryWithSubItems(pathPrefix + fileInfo.Name); + return DeleteFileAlways(pathPrefix + fileInfo.Name); +} +bool RemoveDirectoryWithSubItems(const UString &path) +{ + NFind::CFileInfoW fileInfo; + UString pathPrefix = path + UString(NName::kDirDelimiter); + { + NFind::CEnumeratorW enumerator(pathPrefix + UString(NName::kAnyStringWildcard)); + while (enumerator.Next(fileInfo)) + if (!RemoveDirectorySubItems2(pathPrefix, fileInfo)) + return false; + } + if (!MySetFileAttributes(path, 0)) + return false; + return MyRemoveDirectory(path); +} +#endif + +#ifndef _WIN32_WCE + +bool MyGetShortPathName(LPCTSTR longPath, CSysString &shortPath) +{ + DWORD needLength = ::GetShortPathName(longPath, shortPath.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); + shortPath.ReleaseBuffer(); + return (needLength > 0 && needLength < MAX_PATH); +} + +bool MyGetFullPathName(LPCTSTR fileName, CSysString &resultPath, int &fileNamePartStartIndex) +{ + resultPath.Empty(); + LPTSTR fileNamePointer = 0; + LPTSTR buffer = resultPath.GetBuffer(MAX_PATH); + DWORD needLength = ::GetFullPathName(fileName, MAX_PATH + 1, buffer, &fileNamePointer); + resultPath.ReleaseBuffer(); + if (needLength == 0) + return false; + if (needLength >= MAX_PATH) + { + #ifdef WIN_LONG_PATH2 + needLength++; + buffer = resultPath.GetBuffer(needLength + 1); + DWORD needLength2 = ::GetFullPathNameW(fileName, needLength, buffer, &fileNamePointer); + resultPath.ReleaseBuffer(); + if (needLength2 == 0 || needLength2 > needLength) + #endif + return false; + } + if (fileNamePointer == 0) + fileNamePartStartIndex = lstrlen(fileName); + else + fileNamePartStartIndex = (int)(fileNamePointer - buffer); + return true; +} + +#ifndef _UNICODE +bool MyGetFullPathName(LPCWSTR fileName, UString &resultPath, int &fileNamePartStartIndex) +{ + resultPath.Empty(); + if (g_IsNT) + { + LPWSTR fileNamePointer = 0; + LPWSTR buffer = resultPath.GetBuffer(MAX_PATH); + DWORD needLength = ::GetFullPathNameW(fileName, MAX_PATH + 1, buffer, &fileNamePointer); + resultPath.ReleaseBuffer(); + if (needLength == 0) + return false; + if (needLength >= MAX_PATH) + { + #ifdef WIN_LONG_PATH + needLength++; + buffer = resultPath.GetBuffer(needLength + 1); + DWORD needLength2 = ::GetFullPathNameW(fileName, needLength, buffer, &fileNamePointer); + resultPath.ReleaseBuffer(); + if (needLength2 == 0 || needLength2 > needLength) + #endif + return false; + } + if (fileNamePointer == 0) + fileNamePartStartIndex = MyStringLen(fileName); + else + fileNamePartStartIndex = (int)(fileNamePointer - buffer); + } + else + { + CSysString sysPath; + if (!MyGetFullPathName(GetSysPath(fileName), sysPath, fileNamePartStartIndex)) + return false; + UString resultPath1 = GetUnicodePath(sysPath.Left(fileNamePartStartIndex)); + UString resultPath2 = GetUnicodePath(sysPath.Mid(fileNamePartStartIndex)); + fileNamePartStartIndex = resultPath1.Length(); + resultPath = resultPath1 + resultPath2; + } + return true; +} +#endif + + +bool MyGetFullPathName(LPCTSTR fileName, CSysString &path) +{ + int index; + return MyGetFullPathName(fileName, path, index); +} + +#ifndef _UNICODE +bool MyGetFullPathName(LPCWSTR fileName, UString &path) +{ + int index; + return MyGetFullPathName(fileName, path, index); +} +#endif + +bool GetOnlyName(LPCTSTR fileName, CSysString &resultName) +{ + int index; + if (!MyGetFullPathName(fileName, resultName, index)) + return false; + resultName = resultName.Mid(index); + return true; +} + +#ifndef _UNICODE +bool GetOnlyName(LPCWSTR fileName, UString &resultName) +{ + int index; + if (!MyGetFullPathName(fileName, resultName, index)) + return false; + resultName = resultName.Mid(index); + return true; +} +#endif + +bool GetOnlyDirPrefix(LPCTSTR fileName, CSysString &resultName) +{ + int index; + if (!MyGetFullPathName(fileName, resultName, index)) + return false; + resultName = resultName.Left(index); + return true; +} + +#ifndef _UNICODE +bool GetOnlyDirPrefix(LPCWSTR fileName, UString &resultName) +{ + int index; + if (!MyGetFullPathName(fileName, resultName, index)) + return false; + resultName = resultName.Left(index); + return true; +} +#endif + +bool MyGetCurrentDirectory(CSysString &path) +{ + DWORD needLength = ::GetCurrentDirectory(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); + path.ReleaseBuffer(); + return (needLength > 0 && needLength <= MAX_PATH); +} + +#ifndef _UNICODE +bool MySetCurrentDirectory(LPCWSTR path) +{ + if (g_IsNT) + return BOOLToBool(::SetCurrentDirectoryW(path)); + return MySetCurrentDirectory(GetSysPath(path)); +} +bool MyGetCurrentDirectory(UString &path) +{ + if (g_IsNT) + { + DWORD needLength = ::GetCurrentDirectoryW(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); + path.ReleaseBuffer(); + return (needLength > 0 && needLength <= MAX_PATH); + } + CSysString sysPath; + if (!MyGetCurrentDirectory(sysPath)) + return false; + path = GetUnicodePath(sysPath); + return true; +} +#endif +#endif + +bool MySearchPath(LPCTSTR path, LPCTSTR fileName, LPCTSTR extension, + CSysString &resultPath, UINT32 &filePart) +{ + LPTSTR filePartPointer; + DWORD value = ::SearchPath(path, fileName, extension, + MAX_PATH, resultPath.GetBuffer(MAX_PATH + 1), &filePartPointer); + filePart = (UINT32)(filePartPointer - (LPCTSTR)resultPath); + resultPath.ReleaseBuffer(); + return (value > 0 && value <= MAX_PATH); +} + +#ifndef _UNICODE +bool MySearchPath(LPCWSTR path, LPCWSTR fileName, LPCWSTR extension, + UString &resultPath, UINT32 &filePart) +{ + if (g_IsNT) + { + LPWSTR filePartPointer = 0; + DWORD value = ::SearchPathW(path, fileName, extension, + MAX_PATH, resultPath.GetBuffer(MAX_PATH + 1), &filePartPointer); + filePart = (UINT32)(filePartPointer - (LPCWSTR)resultPath); + resultPath.ReleaseBuffer(); + return (value > 0 && value <= MAX_PATH); + } + + CSysString sysPath; + if (!MySearchPath( + path != 0 ? (LPCTSTR)GetSysPath(path): 0, + fileName != 0 ? (LPCTSTR)GetSysPath(fileName): 0, + extension != 0 ? (LPCTSTR)GetSysPath(extension): 0, + sysPath, filePart)) + return false; + UString resultPath1 = GetUnicodePath(sysPath.Left(filePart)); + UString resultPath2 = GetUnicodePath(sysPath.Mid(filePart)); + filePart = resultPath1.Length(); + resultPath = resultPath1 + resultPath2; + return true; +} +#endif + +bool MyGetTempPath(CSysString &path) +{ + DWORD needLength = ::GetTempPath(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); + path.ReleaseBuffer(); + return (needLength > 0 && needLength <= MAX_PATH); +} + +#ifndef _UNICODE +bool MyGetTempPath(UString &path) +{ + path.Empty(); + if (g_IsNT) + { + DWORD needLength = ::GetTempPathW(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); + path.ReleaseBuffer(); + return (needLength > 0 && needLength <= MAX_PATH); + } + CSysString sysPath; + if (!MyGetTempPath(sysPath)) + return false; + path = GetUnicodePath(sysPath); + return true; +} +#endif + +UINT MyGetTempFileName(LPCTSTR dirPath, LPCTSTR prefix, CSysString &path) +{ + UINT number = ::GetTempFileName(dirPath, prefix, 0, path.GetBuffer(MAX_PATH + 1)); + path.ReleaseBuffer(); + return number; +} + +#ifndef _UNICODE +UINT MyGetTempFileName(LPCWSTR dirPath, LPCWSTR prefix, UString &path) +{ + if (g_IsNT) + { + UINT number = ::GetTempFileNameW(dirPath, prefix, 0, path.GetBuffer(MAX_PATH)); + path.ReleaseBuffer(); + return number; + } + CSysString sysPath; + UINT number = MyGetTempFileName( + dirPath ? (LPCTSTR)GetSysPath(dirPath): 0, + prefix ? (LPCTSTR)GetSysPath(prefix): 0, + sysPath); + path = GetUnicodePath(sysPath); + return number; +} +#endif + +UINT CTempFile::Create(LPCTSTR dirPath, LPCTSTR prefix, CSysString &resultPath) +{ + Remove(); + UINT number = MyGetTempFileName(dirPath, prefix, resultPath); + if (number != 0) + { + _fileName = resultPath; + _mustBeDeleted = true; + } + return number; +} + +bool CTempFile::Create(LPCTSTR prefix, CSysString &resultPath) +{ + CSysString tempPath; + if (!MyGetTempPath(tempPath)) + return false; + if (Create(tempPath, prefix, resultPath) != 0) + return true; + if (!MyGetWindowsDirectory(tempPath)) + return false; + return (Create(tempPath, prefix, resultPath) != 0); +} + +bool CTempFile::Remove() +{ + if (!_mustBeDeleted) + return true; + _mustBeDeleted = !DeleteFileAlways(_fileName); + return !_mustBeDeleted; +} + +#ifndef _UNICODE + +UINT CTempFileW::Create(LPCWSTR dirPath, LPCWSTR prefix, UString &resultPath) +{ + Remove(); + UINT number = MyGetTempFileName(dirPath, prefix, resultPath); + if (number != 0) + { + _fileName = resultPath; + _mustBeDeleted = true; + } + return number; +} + +bool CTempFileW::Create(LPCWSTR prefix, UString &resultPath) +{ + UString tempPath; + if (!MyGetTempPath(tempPath)) + return false; + if (Create(tempPath, prefix, resultPath) != 0) + return true; + if (!MyGetWindowsDirectory(tempPath)) + return false; + return (Create(tempPath, prefix, resultPath) != 0); +} + +bool CTempFileW::Remove() +{ + if (!_mustBeDeleted) + return true; + _mustBeDeleted = !DeleteFileAlways(_fileName); + return !_mustBeDeleted; +} + +#endif + +bool CreateTempDirectory(LPCTSTR prefix, CSysString &dirName) +{ + /* + CSysString prefix = tempPath + prefixChars; + CRandom random; + random.Init(); + */ + for (;;) + { + CTempFile tempFile; + if (!tempFile.Create(prefix, dirName)) + return false; + if (!::DeleteFile(dirName)) + return false; + /* + UINT32 randomNumber = random.Generate(); + TCHAR randomNumberString[32]; + _stprintf(randomNumberString, _T("%04X"), randomNumber); + dirName = prefix + randomNumberString; + */ + if (NFind::DoesFileExist(dirName)) + continue; + if (MyCreateDirectory(dirName)) + return true; + if (::GetLastError() != ERROR_ALREADY_EXISTS) + return false; + } +} + +bool CTempDirectory::Create(LPCTSTR prefix) +{ + Remove(); + return (_mustBeDeleted = CreateTempDirectory(prefix, _tempDir)); +} + +#ifndef _UNICODE + +bool CreateTempDirectory(LPCWSTR prefix, UString &dirName) +{ + /* + CSysString prefix = tempPath + prefixChars; + CRandom random; + random.Init(); + */ + for (;;) + { + CTempFileW tempFile; + if (!tempFile.Create(prefix, dirName)) + return false; + if (!DeleteFileAlways(dirName)) + return false; + /* + UINT32 randomNumber = random.Generate(); + TCHAR randomNumberString[32]; + _stprintf(randomNumberString, _T("%04X"), randomNumber); + dirName = prefix + randomNumberString; + */ + if (NFind::DoesFileExist(dirName)) + continue; + if (MyCreateDirectory(dirName)) + return true; + if (::GetLastError() != ERROR_ALREADY_EXISTS) + return false; + } +} + +bool CTempDirectoryW::Create(LPCWSTR prefix) +{ + Remove(); + return (_mustBeDeleted = CreateTempDirectory(prefix, _tempDir)); +} + +#endif + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/FileDir.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/FileDir.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,178 @@ +// Windows/FileDir.h + +#ifndef __WINDOWS_FILEDIR_H +#define __WINDOWS_FILEDIR_H + +#include "../Common/MyString.h" +#include "Defs.h" + +namespace NWindows { +namespace NFile { +namespace NDirectory { + +#ifdef WIN_LONG_PATH +bool GetLongPaths(LPCWSTR s1, LPCWSTR s2, UString &d1, UString &d2); +#endif + +bool MyGetWindowsDirectory(CSysString &path); +bool MyGetSystemDirectory(CSysString &path); +#ifndef _UNICODE +bool MyGetWindowsDirectory(UString &path); +bool MyGetSystemDirectory(UString &path); +#endif + +bool SetDirTime(LPCWSTR fileName, const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime); + +bool MySetFileAttributes(LPCTSTR fileName, DWORD fileAttributes); +bool MyMoveFile(LPCTSTR existFileName, LPCTSTR newFileName); +bool MyRemoveDirectory(LPCTSTR pathName); +bool MyCreateDirectory(LPCTSTR pathName); +bool CreateComplexDirectory(LPCTSTR pathName); +bool DeleteFileAlways(LPCTSTR name); +bool RemoveDirectoryWithSubItems(const CSysString &path); + +#ifndef _UNICODE +bool MySetFileAttributes(LPCWSTR fileName, DWORD fileAttributes); +bool MyMoveFile(LPCWSTR existFileName, LPCWSTR newFileName); +bool MyRemoveDirectory(LPCWSTR pathName); +bool MyCreateDirectory(LPCWSTR pathName); +bool CreateComplexDirectory(LPCWSTR pathName); +bool DeleteFileAlways(LPCWSTR name); +bool RemoveDirectoryWithSubItems(const UString &path); +#endif + +#ifndef _WIN32_WCE +bool MyGetShortPathName(LPCTSTR longPath, CSysString &shortPath); + +bool MyGetFullPathName(LPCTSTR fileName, CSysString &resultPath, + int &fileNamePartStartIndex); +bool MyGetFullPathName(LPCTSTR fileName, CSysString &resultPath); +bool GetOnlyName(LPCTSTR fileName, CSysString &resultName); +bool GetOnlyDirPrefix(LPCTSTR fileName, CSysString &resultName); +#ifndef _UNICODE +bool MyGetFullPathName(LPCWSTR fileName, UString &resultPath, + int &fileNamePartStartIndex); +bool MyGetFullPathName(LPCWSTR fileName, UString &resultPath); +bool GetOnlyName(LPCWSTR fileName, UString &resultName); +bool GetOnlyDirPrefix(LPCWSTR fileName, UString &resultName); +#endif + +inline bool MySetCurrentDirectory(LPCTSTR path) + { return BOOLToBool(::SetCurrentDirectory(path)); } +bool MyGetCurrentDirectory(CSysString &resultPath); +#ifndef _UNICODE +bool MySetCurrentDirectory(LPCWSTR path); +bool MyGetCurrentDirectory(UString &resultPath); +#endif +#endif + +bool MySearchPath(LPCTSTR path, LPCTSTR fileName, LPCTSTR extension, + CSysString &resultPath, UINT32 &filePart); +#ifndef _UNICODE +bool MySearchPath(LPCWSTR path, LPCWSTR fileName, LPCWSTR extension, + UString &resultPath, UINT32 &filePart); +#endif + +inline bool MySearchPath(LPCTSTR path, LPCTSTR fileName, LPCTSTR extension, + CSysString &resultPath) +{ + UINT32 value; + return MySearchPath(path, fileName, extension, resultPath, value); +} + +#ifndef _UNICODE +inline bool MySearchPath(LPCWSTR path, LPCWSTR fileName, LPCWSTR extension, + UString &resultPath) +{ + UINT32 value; + return MySearchPath(path, fileName, extension, resultPath, value); +} +#endif + +bool MyGetTempPath(CSysString &resultPath); +#ifndef _UNICODE +bool MyGetTempPath(UString &resultPath); +#endif + +UINT MyGetTempFileName(LPCTSTR dirPath, LPCTSTR prefix, CSysString &resultPath); +#ifndef _UNICODE +UINT MyGetTempFileName(LPCWSTR dirPath, LPCWSTR prefix, UString &resultPath); +#endif + +class CTempFile +{ + bool _mustBeDeleted; + CSysString _fileName; +public: + CTempFile(): _mustBeDeleted(false) {} + ~CTempFile() { Remove(); } + void DisableDeleting() { _mustBeDeleted = false; } + UINT Create(LPCTSTR dirPath, LPCTSTR prefix, CSysString &resultPath); + bool Create(LPCTSTR prefix, CSysString &resultPath); + bool Remove(); +}; + +#ifdef _UNICODE +typedef CTempFile CTempFileW; +#else +class CTempFileW +{ + bool _mustBeDeleted; + UString _fileName; +public: + CTempFileW(): _mustBeDeleted(false) {} + ~CTempFileW() { Remove(); } + void DisableDeleting() { _mustBeDeleted = false; } + UINT Create(LPCWSTR dirPath, LPCWSTR prefix, UString &resultPath); + bool Create(LPCWSTR prefix, UString &resultPath); + bool Remove(); +}; +#endif + +bool CreateTempDirectory(LPCTSTR prefixChars, CSysString &dirName); + +class CTempDirectory +{ + bool _mustBeDeleted; + CSysString _tempDir; +public: + const CSysString &GetPath() const { return _tempDir; } + CTempDirectory(): _mustBeDeleted(false) {} + ~CTempDirectory() { Remove(); } + bool Create(LPCTSTR prefix) ; + bool Remove() + { + if (!_mustBeDeleted) + return true; + _mustBeDeleted = !RemoveDirectoryWithSubItems(_tempDir); + return (!_mustBeDeleted); + } + void DisableDeleting() { _mustBeDeleted = false; } +}; + +#ifdef _UNICODE +typedef CTempDirectory CTempDirectoryW; +#else +class CTempDirectoryW +{ + bool _mustBeDeleted; + UString _tempDir; +public: + const UString &GetPath() const { return _tempDir; } + CTempDirectoryW(): _mustBeDeleted(false) {} + ~CTempDirectoryW() { Remove(); } + bool Create(LPCWSTR prefix) ; + bool Remove() + { + if (!_mustBeDeleted) + return true; + _mustBeDeleted = !RemoveDirectoryWithSubItems(_tempDir); + return (!_mustBeDeleted); + } + void DisableDeleting() { _mustBeDeleted = false; } +}; +#endif + +}}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/FileFind.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/FileFind.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,402 @@ +// Windows/FileFind.cpp + +#include "StdAfx.h" + +#include "FileFind.h" +#ifndef _UNICODE +#include "../Common/StringConvert.h" +#endif + +#ifndef _UNICODE +extern bool g_IsNT; +#endif + +namespace NWindows { +namespace NFile { + +#if defined(WIN_LONG_PATH) && defined(_UNICODE) +#define WIN_LONG_PATH2 +#endif + +bool GetLongPath(LPCWSTR fileName, UString &res); + +namespace NFind { + +static const TCHAR kDot = TEXT('.'); + +bool CFileInfo::IsDots() const +{ + if (!IsDir() || Name.IsEmpty()) + return false; + if (Name[0] != kDot) + return false; + return Name.Length() == 1 || (Name[1] == kDot && Name.Length() == 2); +} + +#ifndef _UNICODE +bool CFileInfoW::IsDots() const +{ + if (!IsDir() || Name.IsEmpty()) + return false; + if (Name[0] != kDot) + return false; + return Name.Length() == 1 || (Name[1] == kDot && Name.Length() == 2); +} +#endif + +static void ConvertWIN32_FIND_DATA_To_FileInfo(const WIN32_FIND_DATA &fd, CFileInfo &fi) +{ + fi.Attrib = fd.dwFileAttributes; + fi.CTime = fd.ftCreationTime; + fi.ATime = fd.ftLastAccessTime; + fi.MTime = fd.ftLastWriteTime; + fi.Size = (((UInt64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; + fi.Name = fd.cFileName; + #ifndef _WIN32_WCE + fi.ReparseTag = fd.dwReserved0; + #else + fi.ObjectID = fd.dwOID; + #endif +} + +#ifndef _UNICODE + +static inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; } + +static void ConvertWIN32_FIND_DATA_To_FileInfo(const WIN32_FIND_DATAW &fd, CFileInfoW &fi) +{ + fi.Attrib = fd.dwFileAttributes; + fi.CTime = fd.ftCreationTime; + fi.ATime = fd.ftLastAccessTime; + fi.MTime = fd.ftLastWriteTime; + fi.Size = (((UInt64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; + fi.Name = fd.cFileName; + #ifndef _WIN32_WCE + fi.ReparseTag = fd.dwReserved0; + #else + fi.ObjectID = fd.dwOID; + #endif +} + +static void ConvertWIN32_FIND_DATA_To_FileInfo(const WIN32_FIND_DATA &fd, CFileInfoW &fi) +{ + fi.Attrib = fd.dwFileAttributes; + fi.CTime = fd.ftCreationTime; + fi.ATime = fd.ftLastAccessTime; + fi.MTime = fd.ftLastWriteTime; + fi.Size = (((UInt64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; + fi.Name = GetUnicodeString(fd.cFileName, GetCurrentCodePage()); + #ifndef _WIN32_WCE + fi.ReparseTag = fd.dwReserved0; + #else + fi.ObjectID = fd.dwOID; + #endif +} +#endif + +//////////////////////////////// +// CFindFile + +bool CFindFile::Close() +{ + if (_handle == INVALID_HANDLE_VALUE) + return true; + if (!::FindClose(_handle)) + return false; + _handle = INVALID_HANDLE_VALUE; + return true; +} + + +bool CFindFile::FindFirst(LPCTSTR wildcard, CFileInfo &fileInfo) +{ + if (!Close()) + return false; + WIN32_FIND_DATA fd; + _handle = ::FindFirstFile(wildcard, &fd); + #ifdef WIN_LONG_PATH2 + if (_handle == INVALID_HANDLE_VALUE) + { + UString longPath; + if (GetLongPath(wildcard, longPath)) + _handle = ::FindFirstFileW(longPath, &fd); + } + #endif + if (_handle == INVALID_HANDLE_VALUE) + return false; + ConvertWIN32_FIND_DATA_To_FileInfo(fd, fileInfo); + return true; +} + +#ifndef _UNICODE +bool CFindFile::FindFirst(LPCWSTR wildcard, CFileInfoW &fileInfo) +{ + if (!Close()) + return false; + if (g_IsNT) + { + WIN32_FIND_DATAW fd; + _handle = ::FindFirstFileW(wildcard, &fd); + #ifdef WIN_LONG_PATH + if (_handle == INVALID_HANDLE_VALUE) + { + UString longPath; + if (GetLongPath(wildcard, longPath)) + _handle = ::FindFirstFileW(longPath, &fd); + } + #endif + if (_handle != INVALID_HANDLE_VALUE) + ConvertWIN32_FIND_DATA_To_FileInfo(fd, fileInfo); + } + else + { + WIN32_FIND_DATAA fd; + _handle = ::FindFirstFileA(UnicodeStringToMultiByte(wildcard, + GetCurrentCodePage()), &fd); + if (_handle != INVALID_HANDLE_VALUE) + ConvertWIN32_FIND_DATA_To_FileInfo(fd, fileInfo); + } + return (_handle != INVALID_HANDLE_VALUE); +} +#endif + +bool CFindFile::FindNext(CFileInfo &fileInfo) +{ + WIN32_FIND_DATA fd; + bool result = BOOLToBool(::FindNextFile(_handle, &fd)); + if (result) + ConvertWIN32_FIND_DATA_To_FileInfo(fd, fileInfo); + return result; +} + +#ifndef _UNICODE +bool CFindFile::FindNext(CFileInfoW &fileInfo) +{ + if (g_IsNT) + { + WIN32_FIND_DATAW fd; + if (!::FindNextFileW(_handle, &fd)) + return false; + ConvertWIN32_FIND_DATA_To_FileInfo(fd, fileInfo); + } + else + { + WIN32_FIND_DATAA fd; + if (!::FindNextFileA(_handle, &fd)) + return false; + ConvertWIN32_FIND_DATA_To_FileInfo(fd, fileInfo); + } + return true; +} +#endif + +bool FindFile(LPCTSTR wildcard, CFileInfo &fileInfo) +{ + CFindFile finder; + return finder.FindFirst(wildcard, fileInfo); +} + +#ifndef _UNICODE +bool FindFile(LPCWSTR wildcard, CFileInfoW &fileInfo) +{ + CFindFile finder; + return finder.FindFirst(wildcard, fileInfo); +} +#endif + +bool DoesFileExist(LPCTSTR name) +{ + CFileInfo fileInfo; + return FindFile(name, fileInfo); +} + +#ifndef _UNICODE +bool DoesFileExist(LPCWSTR name) +{ + CFileInfoW fileInfo; + return FindFile(name, fileInfo); +} +#endif + +///////////////////////////////////// +// CEnumerator + +bool CEnumerator::NextAny(CFileInfo &fileInfo) +{ + if (_findFile.IsHandleAllocated()) + return _findFile.FindNext(fileInfo); + else + return _findFile.FindFirst(_wildcard, fileInfo); +} + +bool CEnumerator::Next(CFileInfo &fileInfo) +{ + for (;;) + { + if (!NextAny(fileInfo)) + return false; + if (!fileInfo.IsDots()) + return true; + } +} + +bool CEnumerator::Next(CFileInfo &fileInfo, bool &found) +{ + if (Next(fileInfo)) + { + found = true; + return true; + } + found = false; + return (::GetLastError() == ERROR_NO_MORE_FILES); +} + +#ifndef _UNICODE +bool CEnumeratorW::NextAny(CFileInfoW &fileInfo) +{ + if (_findFile.IsHandleAllocated()) + return _findFile.FindNext(fileInfo); + else + return _findFile.FindFirst(_wildcard, fileInfo); +} + +bool CEnumeratorW::Next(CFileInfoW &fileInfo) +{ + for (;;) + { + if (!NextAny(fileInfo)) + return false; + if (!fileInfo.IsDots()) + return true; + } +} + +bool CEnumeratorW::Next(CFileInfoW &fileInfo, bool &found) +{ + if (Next(fileInfo)) + { + found = true; + return true; + } + found = false; + return (::GetLastError() == ERROR_NO_MORE_FILES); +} + +#endif + +//////////////////////////////// +// CFindChangeNotification +// FindFirstChangeNotification can return 0. MSDN doesn't tell about it. + +bool CFindChangeNotification::Close() +{ + if (!IsHandleAllocated()) + return true; + if (!::FindCloseChangeNotification(_handle)) + return false; + _handle = INVALID_HANDLE_VALUE; + return true; +} + +HANDLE CFindChangeNotification::FindFirst(LPCTSTR pathName, bool watchSubtree, DWORD notifyFilter) +{ + _handle = ::FindFirstChangeNotification(pathName, BoolToBOOL(watchSubtree), notifyFilter); + #ifdef WIN_LONG_PATH2 + if (!IsHandleAllocated()) + { + UString longPath; + if (GetLongPath(pathName, longPath)) + _handle = ::FindFirstChangeNotificationW(longPath, BoolToBOOL(watchSubtree), notifyFilter); + } + #endif + return _handle; +} + +#ifndef _UNICODE +HANDLE CFindChangeNotification::FindFirst(LPCWSTR pathName, bool watchSubtree, DWORD notifyFilter) +{ + if (!g_IsNT) + return FindFirst(UnicodeStringToMultiByte(pathName, GetCurrentCodePage()), watchSubtree, notifyFilter); + _handle = ::FindFirstChangeNotificationW(pathName, BoolToBOOL(watchSubtree), notifyFilter); + #ifdef WIN_LONG_PATH + if (!IsHandleAllocated()) + { + UString longPath; + if (GetLongPath(pathName, longPath)) + _handle = ::FindFirstChangeNotificationW(longPath, BoolToBOOL(watchSubtree), notifyFilter); + } + #endif + return _handle; +} +#endif + +#ifndef _WIN32_WCE +bool MyGetLogicalDriveStrings(CSysStringVector &driveStrings) +{ + driveStrings.Clear(); + UINT32 size = GetLogicalDriveStrings(0, NULL); + if (size == 0) + return false; + CSysString buffer; + UINT32 newSize = GetLogicalDriveStrings(size, buffer.GetBuffer(size)); + if (newSize == 0) + return false; + if (newSize > size) + return false; + CSysString string; + for (UINT32 i = 0; i < newSize; i++) + { + TCHAR c = buffer[i]; + if (c == TEXT('\0')) + { + driveStrings.Add(string); + string.Empty(); + } + else + string += c; + } + if (!string.IsEmpty()) + return false; + return true; +} + +#ifndef _UNICODE +bool MyGetLogicalDriveStrings(UStringVector &driveStrings) +{ + driveStrings.Clear(); + if (g_IsNT) + { + UINT32 size = GetLogicalDriveStringsW(0, NULL); + if (size == 0) + return false; + UString buffer; + UINT32 newSize = GetLogicalDriveStringsW(size, buffer.GetBuffer(size)); + if (newSize == 0) + return false; + if (newSize > size) + return false; + UString string; + for (UINT32 i = 0; i < newSize; i++) + { + WCHAR c = buffer[i]; + if (c == L'\0') + { + driveStrings.Add(string); + string.Empty(); + } + else + string += c; + } + return string.IsEmpty(); + } + CSysStringVector driveStringsA; + bool res = MyGetLogicalDriveStrings(driveStringsA); + for (int i = 0; i < driveStringsA.Size(); i++) + driveStrings.Add(GetUnicodeString(driveStringsA[i])); + return res; +} +#endif + +#endif + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/FileFind.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/FileFind.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,153 @@ +// Windows/FileFind.h + +#ifndef __WINDOWS_FILEFIND_H +#define __WINDOWS_FILEFIND_H + +#include "../Common/MyString.h" +#include "../Common/Types.h" +#include "FileName.h" +#include "Defs.h" + +namespace NWindows { +namespace NFile { +namespace NFind { + +namespace NAttributes +{ + inline bool IsReadOnly(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_READONLY) != 0; } + inline bool IsHidden(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_HIDDEN) != 0; } + inline bool IsSystem(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_SYSTEM) != 0; } + inline bool IsDir(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; } + inline bool IsArchived(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ARCHIVE) != 0; } + inline bool IsCompressed(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_COMPRESSED) != 0; } + inline bool IsEncrypted(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ENCRYPTED) != 0; } +} + +class CFileInfoBase +{ + bool MatchesMask(UINT32 mask) const { return ((Attrib & mask) != 0); } +public: + UInt64 Size; + FILETIME CTime; + FILETIME ATime; + FILETIME MTime; + DWORD Attrib; + + #ifndef _WIN32_WCE + UINT32 ReparseTag; + #else + DWORD ObjectID; + #endif + + bool IsArchived() const { return MatchesMask(FILE_ATTRIBUTE_ARCHIVE); } + bool IsCompressed() const { return MatchesMask(FILE_ATTRIBUTE_COMPRESSED); } + bool IsDir() const { return MatchesMask(FILE_ATTRIBUTE_DIRECTORY); } + bool IsEncrypted() const { return MatchesMask(FILE_ATTRIBUTE_ENCRYPTED); } + bool IsHidden() const { return MatchesMask(FILE_ATTRIBUTE_HIDDEN); } + bool IsNormal() const { return MatchesMask(FILE_ATTRIBUTE_NORMAL); } + bool IsOffline() const { return MatchesMask(FILE_ATTRIBUTE_OFFLINE); } + bool IsReadOnly() const { return MatchesMask(FILE_ATTRIBUTE_READONLY); } + bool HasReparsePoint() const { return MatchesMask(FILE_ATTRIBUTE_REPARSE_POINT); } + bool IsSparse() const { return MatchesMask(FILE_ATTRIBUTE_SPARSE_FILE); } + bool IsSystem() const { return MatchesMask(FILE_ATTRIBUTE_SYSTEM); } + bool IsTemporary() const { return MatchesMask(FILE_ATTRIBUTE_TEMPORARY); } +}; + +class CFileInfo: public CFileInfoBase +{ +public: + CSysString Name; + bool IsDots() const; +}; + +#ifdef _UNICODE +typedef CFileInfo CFileInfoW; +#else +class CFileInfoW: public CFileInfoBase +{ +public: + UString Name; + bool IsDots() const; +}; +#endif + +class CFindFile +{ + friend class CEnumerator; + HANDLE _handle; +public: + bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE; } + CFindFile(): _handle(INVALID_HANDLE_VALUE) {} + ~CFindFile() { Close(); } + bool FindFirst(LPCTSTR wildcard, CFileInfo &fileInfo); + bool FindNext(CFileInfo &fileInfo); + #ifndef _UNICODE + bool FindFirst(LPCWSTR wildcard, CFileInfoW &fileInfo); + bool FindNext(CFileInfoW &fileInfo); + #endif + bool Close(); +}; + +bool FindFile(LPCTSTR wildcard, CFileInfo &fileInfo); + +bool DoesFileExist(LPCTSTR name); +#ifndef _UNICODE +bool FindFile(LPCWSTR wildcard, CFileInfoW &fileInfo); +bool DoesFileExist(LPCWSTR name); +#endif + +class CEnumerator +{ + CFindFile _findFile; + CSysString _wildcard; + bool NextAny(CFileInfo &fileInfo); +public: + CEnumerator(): _wildcard(NName::kAnyStringWildcard) {} + CEnumerator(const CSysString &wildcard): _wildcard(wildcard) {} + bool Next(CFileInfo &fileInfo); + bool Next(CFileInfo &fileInfo, bool &found); +}; + +#ifdef _UNICODE +typedef CEnumerator CEnumeratorW; +#else +class CEnumeratorW +{ + CFindFile _findFile; + UString _wildcard; + bool NextAny(CFileInfoW &fileInfo); +public: + CEnumeratorW(): _wildcard(NName::kAnyStringWildcard) {} + CEnumeratorW(const UString &wildcard): _wildcard(wildcard) {} + bool Next(CFileInfoW &fileInfo); + bool Next(CFileInfoW &fileInfo, bool &found); +}; +#endif + +class CFindChangeNotification +{ + HANDLE _handle; +public: + operator HANDLE () { return _handle; } + bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE && _handle != 0; } + CFindChangeNotification(): _handle(INVALID_HANDLE_VALUE) {} + ~CFindChangeNotification() { Close(); } + bool Close(); + HANDLE FindFirst(LPCTSTR pathName, bool watchSubtree, DWORD notifyFilter); + #ifndef _UNICODE + HANDLE FindFirst(LPCWSTR pathName, bool watchSubtree, DWORD notifyFilter); + #endif + bool FindNext() { return BOOLToBool(::FindNextChangeNotification(_handle)); } +}; + +#ifndef _WIN32_WCE +bool MyGetLogicalDriveStrings(CSysStringVector &driveStrings); +#ifndef _UNICODE +bool MyGetLogicalDriveStrings(UStringVector &driveStrings); +#endif +#endif + +}}} + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/FileIO.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/FileIO.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,317 @@ +// Windows/FileIO.cpp + +#include "StdAfx.h" + +#include "FileIO.h" +#include "Defs.h" +#ifdef WIN_LONG_PATH +#include "../Common/MyString.h" +#endif +#ifndef _UNICODE +#include "../Common/StringConvert.h" +#endif + +#ifndef _UNICODE +extern bool g_IsNT; +#endif + +namespace NWindows { +namespace NFile { + +#if defined(WIN_LONG_PATH) && defined(_UNICODE) +#define WIN_LONG_PATH2 +#endif + +#ifdef WIN_LONG_PATH +bool GetLongPathBase(LPCWSTR s, UString &res) +{ + res.Empty(); + int len = MyStringLen(s); + wchar_t c = s[0]; + if (len < 1 || c == L'\\' || c == L'.' && (len == 1 || len == 2 && s[1] == L'.')) + return true; + UString curDir; + bool isAbs = false; + if (len > 3) + isAbs = (s[1] == L':' && s[2] == L'\\' && (c >= L'a' && c <= L'z' || c >= L'A' && c <= L'Z')); + + if (!isAbs) + { + DWORD needLength = ::GetCurrentDirectoryW(MAX_PATH + 1, curDir.GetBuffer(MAX_PATH + 1)); + curDir.ReleaseBuffer(); + if (needLength == 0 || needLength > MAX_PATH) + return false; + if (curDir[curDir.Length() - 1] != L'\\') + curDir += L'\\'; + } + res = UString(L"\\\\?\\") + curDir + s; + return true; +} + +bool GetLongPath(LPCWSTR path, UString &longPath) +{ + if (GetLongPathBase(path, longPath)) + return !longPath.IsEmpty(); + return false; +} +#endif + +namespace NIO { + +CFileBase::~CFileBase() { Close(); } + +bool CFileBase::Create(LPCTSTR fileName, DWORD desiredAccess, + DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) +{ + if (!Close()) + return false; + _handle = ::CreateFile(fileName, desiredAccess, shareMode, + (LPSECURITY_ATTRIBUTES)NULL, creationDisposition, + flagsAndAttributes, (HANDLE)NULL); + #ifdef WIN_LONG_PATH2 + if (_handle == INVALID_HANDLE_VALUE) + { + UString longPath; + if (GetLongPath(fileName, longPath)) + _handle = ::CreateFileW(longPath, desiredAccess, shareMode, + (LPSECURITY_ATTRIBUTES)NULL, creationDisposition, + flagsAndAttributes, (HANDLE)NULL); + } + #endif + return (_handle != INVALID_HANDLE_VALUE); +} + +#ifndef _UNICODE +bool CFileBase::Create(LPCWSTR fileName, DWORD desiredAccess, + DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) +{ + if (!g_IsNT) + return Create(UnicodeStringToMultiByte(fileName, ::AreFileApisANSI() ? CP_ACP : CP_OEMCP), + desiredAccess, shareMode, creationDisposition, flagsAndAttributes); + if (!Close()) + return false; + _handle = ::CreateFileW(fileName, desiredAccess, shareMode, + (LPSECURITY_ATTRIBUTES)NULL, creationDisposition, + flagsAndAttributes, (HANDLE)NULL); + #ifdef WIN_LONG_PATH + if (_handle == INVALID_HANDLE_VALUE) + { + UString longPath; + if (GetLongPath(fileName, longPath)) + _handle = ::CreateFileW(longPath, desiredAccess, shareMode, + (LPSECURITY_ATTRIBUTES)NULL, creationDisposition, + flagsAndAttributes, (HANDLE)NULL); + } + #endif + return (_handle != INVALID_HANDLE_VALUE); +} +#endif + +bool CFileBase::Close() +{ + if (_handle == INVALID_HANDLE_VALUE) + return true; + if (!::CloseHandle(_handle)) + return false; + _handle = INVALID_HANDLE_VALUE; + return true; +} + +bool CFileBase::GetPosition(UInt64 &position) const +{ + return Seek(0, FILE_CURRENT, position); +} + +bool CFileBase::GetLength(UInt64 &length) const +{ + DWORD sizeHigh; + DWORD sizeLow = ::GetFileSize(_handle, &sizeHigh); + if (sizeLow == 0xFFFFFFFF) + if (::GetLastError() != NO_ERROR) + return false; + length = (((UInt64)sizeHigh) << 32) + sizeLow; + return true; +} + +bool CFileBase::Seek(Int64 distanceToMove, DWORD moveMethod, UInt64 &newPosition) const +{ + LARGE_INTEGER value; + value.QuadPart = distanceToMove; + value.LowPart = ::SetFilePointer(_handle, value.LowPart, &value.HighPart, moveMethod); + if (value.LowPart == 0xFFFFFFFF) + if (::GetLastError() != NO_ERROR) + return false; + newPosition = value.QuadPart; + return true; +} + +bool CFileBase::Seek(UInt64 position, UInt64 &newPosition) +{ + return Seek(position, FILE_BEGIN, newPosition); +} + +bool CFileBase::SeekToBegin() +{ + UInt64 newPosition; + return Seek(0, newPosition); +} + +bool CFileBase::SeekToEnd(UInt64 &newPosition) +{ + return Seek(0, FILE_END, newPosition); +} + +bool CFileBase::GetFileInformation(CByHandleFileInfo &fileInfo) const +{ + BY_HANDLE_FILE_INFORMATION winFileInfo; + if (!::GetFileInformationByHandle(_handle, &winFileInfo)) + return false; + fileInfo.Attributes = winFileInfo.dwFileAttributes; + fileInfo.CTime = winFileInfo.ftCreationTime; + fileInfo.ATime = winFileInfo.ftLastAccessTime; + fileInfo.MTime = winFileInfo.ftLastWriteTime; + fileInfo.VolumeSerialNumber = winFileInfo.dwFileAttributes; + fileInfo.Size = (((UInt64)winFileInfo.nFileSizeHigh) << 32) + winFileInfo.nFileSizeLow; + fileInfo.NumberOfLinks = winFileInfo.nNumberOfLinks; + fileInfo.FileIndex = (((UInt64)winFileInfo.nFileIndexHigh) << 32) + winFileInfo.nFileIndexLow; + return true; +} + +///////////////////////// +// CInFile + +bool CInFile::Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) + { return Create(fileName, GENERIC_READ, shareMode, creationDisposition, flagsAndAttributes); } + +bool CInFile::OpenShared(LPCTSTR fileName, bool shareForWrite) +{ return Open(fileName, FILE_SHARE_READ | (shareForWrite ? FILE_SHARE_WRITE : 0), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL); } + +bool CInFile::Open(LPCTSTR fileName) + { return OpenShared(fileName, false); } + +#ifndef _UNICODE +bool CInFile::Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) + { return Create(fileName, GENERIC_READ, shareMode, creationDisposition, flagsAndAttributes); } + +bool CInFile::OpenShared(LPCWSTR fileName, bool shareForWrite) +{ return Open(fileName, FILE_SHARE_READ | (shareForWrite ? FILE_SHARE_WRITE : 0), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL); } + +bool CInFile::Open(LPCWSTR fileName) + { return OpenShared(fileName, false); } +#endif + +// ReadFile and WriteFile functions in Windows have BUG: +// If you Read or Write 64MB or more (probably min_failure_size = 64MB - 32KB + 1) +// from/to Network file, it returns ERROR_NO_SYSTEM_RESOURCES +// (Insufficient system resources exist to complete the requested service). + +// Probably in some version of Windows there are problems with other sizes: +// for 32 MB (maybe also for 16 MB). +// And message can be "Network connection was lost" + +static UInt32 kChunkSizeMax = (1 << 22); + +bool CInFile::ReadPart(void *data, UInt32 size, UInt32 &processedSize) +{ + if (size > kChunkSizeMax) + size = kChunkSizeMax; + DWORD processedLoc = 0; + bool res = BOOLToBool(::ReadFile(_handle, data, size, &processedLoc, NULL)); + processedSize = (UInt32)processedLoc; + return res; +} + +bool CInFile::Read(void *data, UInt32 size, UInt32 &processedSize) +{ + processedSize = 0; + do + { + UInt32 processedLoc = 0; + bool res = ReadPart(data, size, processedLoc); + processedSize += processedLoc; + if (!res) + return false; + if (processedLoc == 0) + return true; + data = (void *)((unsigned char *)data + processedLoc); + size -= processedLoc; + } + while (size > 0); + return true; +} + +///////////////////////// +// COutFile + +bool COutFile::Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) + { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); } + +static inline DWORD GetCreationDisposition(bool createAlways) + { return createAlways? CREATE_ALWAYS: CREATE_NEW; } + +bool COutFile::Open(LPCTSTR fileName, DWORD creationDisposition) + { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); } + +bool COutFile::Create(LPCTSTR fileName, bool createAlways) + { return Open(fileName, GetCreationDisposition(createAlways)); } + +#ifndef _UNICODE + +bool COutFile::Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) + { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); } + +bool COutFile::Open(LPCWSTR fileName, DWORD creationDisposition) + { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); } + +bool COutFile::Create(LPCWSTR fileName, bool createAlways) + { return Open(fileName, GetCreationDisposition(createAlways)); } + +#endif + +bool COutFile::SetTime(const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime) + { return BOOLToBool(::SetFileTime(_handle, cTime, aTime, mTime)); } + +bool COutFile::SetMTime(const FILETIME *mTime) { return SetTime(NULL, NULL, mTime); } + +bool COutFile::WritePart(const void *data, UInt32 size, UInt32 &processedSize) +{ + if (size > kChunkSizeMax) + size = kChunkSizeMax; + DWORD processedLoc = 0; + bool res = BOOLToBool(::WriteFile(_handle, data, size, &processedLoc, NULL)); + processedSize = (UInt32)processedLoc; + return res; +} + +bool COutFile::Write(const void *data, UInt32 size, UInt32 &processedSize) +{ + processedSize = 0; + do + { + UInt32 processedLoc = 0; + bool res = WritePart(data, size, processedLoc); + processedSize += processedLoc; + if (!res) + return false; + if (processedLoc == 0) + return true; + data = (const void *)((const unsigned char *)data + processedLoc); + size -= processedLoc; + } + while (size > 0); + return true; +} + +bool COutFile::SetEndOfFile() { return BOOLToBool(::SetEndOfFile(_handle)); } + +bool COutFile::SetLength(UInt64 length) +{ + UInt64 newPosition; + if (!Seek(length, newPosition)) + return false; + if (newPosition != length) + return false; + return SetEndOfFile(); +} + +}}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/FileIO.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/FileIO.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,99 @@ +// Windows/FileIO.h + +#ifndef __WINDOWS_FILEIO_H +#define __WINDOWS_FILEIO_H + +#include "../Common/Types.h" + +namespace NWindows { +namespace NFile { +namespace NIO { + +struct CByHandleFileInfo +{ + DWORD Attributes; + FILETIME CTime; + FILETIME ATime; + FILETIME MTime; + DWORD VolumeSerialNumber; + UInt64 Size; + DWORD NumberOfLinks; + UInt64 FileIndex; +}; + +class CFileBase +{ +protected: + HANDLE _handle; + bool Create(LPCTSTR fileName, DWORD desiredAccess, + DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); + #ifndef _UNICODE + bool Create(LPCWSTR fileName, DWORD desiredAccess, + DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); + #endif + +public: + CFileBase(): _handle(INVALID_HANDLE_VALUE){}; + ~CFileBase(); + + bool Close(); + + bool GetPosition(UInt64 &position) const; + bool GetLength(UInt64 &length) const; + + bool Seek(Int64 distanceToMove, DWORD moveMethod, UInt64 &newPosition) const; + bool Seek(UInt64 position, UInt64 &newPosition); + bool SeekToBegin(); + bool SeekToEnd(UInt64 &newPosition); + + bool GetFileInformation(CByHandleFileInfo &fileInfo) const; +}; + +class CInFile: public CFileBase +{ +public: + bool Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); + bool OpenShared(LPCTSTR fileName, bool shareForWrite); + bool Open(LPCTSTR fileName); + #ifndef _UNICODE + bool Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); + bool OpenShared(LPCWSTR fileName, bool shareForWrite); + bool Open(LPCWSTR fileName); + #endif + bool ReadPart(void *data, UInt32 size, UInt32 &processedSize); + bool Read(void *data, UInt32 size, UInt32 &processedSize); +}; + +class COutFile: public CFileBase +{ + // DWORD m_CreationDisposition; +public: + // COutFile(): m_CreationDisposition(CREATE_NEW){}; + bool Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); + bool Open(LPCTSTR fileName, DWORD creationDisposition); + bool Create(LPCTSTR fileName, bool createAlways); + + #ifndef _UNICODE + bool Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes); + bool Open(LPCWSTR fileName, DWORD creationDisposition); + bool Create(LPCWSTR fileName, bool createAlways); + #endif + + /* + void SetOpenCreationDisposition(DWORD creationDisposition) + { m_CreationDisposition = creationDisposition; } + void SetOpenCreationDispositionCreateAlways() + { m_CreationDisposition = CREATE_ALWAYS; } + */ + + bool SetTime(const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime); + bool SetMTime(const FILETIME *mTime); + bool WritePart(const void *data, UInt32 size, UInt32 &processedSize); + bool Write(const void *data, UInt32 size, UInt32 &processedSize); + bool SetEndOfFile(); + bool SetLength(UInt64 length); +}; + +}}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/FileName.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/FileName.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,25 @@ +// Windows/FileName.h + +#ifndef __WINDOWS_FILENAME_H +#define __WINDOWS_FILENAME_H + +#include "../Common/MyString.h" + +namespace NWindows { +namespace NFile { +namespace NName { + +const TCHAR kDirDelimiter = CHAR_PATH_SEPARATOR; +const TCHAR kAnyStringWildcard = '*'; + +void NormalizeDirPathPrefix(CSysString &dirPath); // ensures that it ended with '\\' +#ifndef _UNICODE +void NormalizeDirPathPrefix(UString &dirPath); // ensures that it ended with '\\' +#endif + +void SplitNameToPureNameAndExtension(const UString &fullName, + UString &pureName, UString &extensionDelimiter, UString &extension); + +}}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/Handle.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/Handle.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,37 @@ +// Windows/Handle.h + +#ifndef __WINDOWS_HANDLE_H +#define __WINDOWS_HANDLE_H + +namespace NWindows { + +class CHandle +{ +protected: + HANDLE _handle; +public: + operator HANDLE() { return _handle; } + CHandle(): _handle(NULL) {} + ~CHandle() { Close(); } + bool Close() + { + if (_handle == NULL) + return true; + if (!::CloseHandle(_handle)) + return false; + _handle = NULL; + return true; + } + void Attach(HANDLE handle) + { _handle = handle; } + HANDLE Detach() + { + HANDLE handle = _handle; + _handle = NULL; + return handle; + } +}; + +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/PropVariant.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/PropVariant.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,312 @@ +// Windows/PropVariant.cpp + +#include "StdAfx.h" + +#include "PropVariant.h" + +#include "../Common/Defs.h" + +namespace NWindows { +namespace NCOM { + +CPropVariant::CPropVariant(const PROPVARIANT& varSrc) +{ + vt = VT_EMPTY; + InternalCopy(&varSrc); +} + +CPropVariant::CPropVariant(const CPropVariant& varSrc) +{ + vt = VT_EMPTY; + InternalCopy(&varSrc); +} + +CPropVariant::CPropVariant(BSTR bstrSrc) +{ + vt = VT_EMPTY; + *this = bstrSrc; +} + +CPropVariant::CPropVariant(LPCOLESTR lpszSrc) +{ + vt = VT_EMPTY; + *this = lpszSrc; +} + +CPropVariant& CPropVariant::operator=(const CPropVariant& varSrc) +{ + InternalCopy(&varSrc); + return *this; +} +CPropVariant& CPropVariant::operator=(const PROPVARIANT& varSrc) +{ + InternalCopy(&varSrc); + return *this; +} + +CPropVariant& CPropVariant::operator=(BSTR bstrSrc) +{ + *this = (LPCOLESTR)bstrSrc; + return *this; +} + +CPropVariant& CPropVariant::operator=(LPCOLESTR lpszSrc) +{ + InternalClear(); + vt = VT_BSTR; + wReserved1 = 0; + bstrVal = ::SysAllocString(lpszSrc); + if (bstrVal == NULL && lpszSrc != NULL) + { + vt = VT_ERROR; + scode = E_OUTOFMEMORY; + } + return *this; +} + + +CPropVariant& CPropVariant::operator=(bool bSrc) +{ + if (vt != VT_BOOL) + { + InternalClear(); + vt = VT_BOOL; + } + boolVal = bSrc ? VARIANT_TRUE : VARIANT_FALSE; + return *this; +} + +CPropVariant& CPropVariant::operator=(UInt32 value) +{ + if (vt != VT_UI4) + { + InternalClear(); + vt = VT_UI4; + } + ulVal = value; + return *this; +} + +CPropVariant& CPropVariant::operator=(UInt64 value) +{ + if (vt != VT_UI8) + { + InternalClear(); + vt = VT_UI8; + } + uhVal.QuadPart = value; + return *this; +} + +CPropVariant& CPropVariant::operator=(const FILETIME &value) +{ + if (vt != VT_FILETIME) + { + InternalClear(); + vt = VT_FILETIME; + } + filetime = value; + return *this; +} + +CPropVariant& CPropVariant::operator=(Int32 value) +{ + if (vt != VT_I4) + { + InternalClear(); + vt = VT_I4; + } + lVal = value; + + return *this; +} + +CPropVariant& CPropVariant::operator=(Byte value) +{ + if (vt != VT_UI1) + { + InternalClear(); + vt = VT_UI1; + } + bVal = value; + return *this; +} + +CPropVariant& CPropVariant::operator=(Int16 value) +{ + if (vt != VT_I2) + { + InternalClear(); + vt = VT_I2; + } + iVal = value; + return *this; +} + +/* +CPropVariant& CPropVariant::operator=(LONG value) +{ + if (vt != VT_I4) + { + InternalClear(); + vt = VT_I4; + } + lVal = value; + return *this; +} +*/ + +static HRESULT MyPropVariantClear(PROPVARIANT *propVariant) +{ + switch(propVariant->vt) + { + case VT_UI1: + case VT_I1: + case VT_I2: + case VT_UI2: + case VT_BOOL: + case VT_I4: + case VT_UI4: + case VT_R4: + case VT_INT: + case VT_UINT: + case VT_ERROR: + case VT_FILETIME: + case VT_UI8: + case VT_R8: + case VT_CY: + case VT_DATE: + propVariant->vt = VT_EMPTY; + propVariant->wReserved1 = 0; + return S_OK; + } + return ::VariantClear((VARIANTARG *)propVariant); +} + +HRESULT CPropVariant::Clear() +{ + return MyPropVariantClear(this); +} + +HRESULT CPropVariant::Copy(const PROPVARIANT* pSrc) +{ + ::VariantClear((tagVARIANT *)this); + switch(pSrc->vt) + { + case VT_UI1: + case VT_I1: + case VT_I2: + case VT_UI2: + case VT_BOOL: + case VT_I4: + case VT_UI4: + case VT_R4: + case VT_INT: + case VT_UINT: + case VT_ERROR: + case VT_FILETIME: + case VT_UI8: + case VT_R8: + case VT_CY: + case VT_DATE: + memmove((PROPVARIANT*)this, pSrc, sizeof(PROPVARIANT)); + return S_OK; + } + return ::VariantCopy((tagVARIANT *)this, (tagVARIANT *)(pSrc)); +} + + +HRESULT CPropVariant::Attach(PROPVARIANT* pSrc) +{ + HRESULT hr = Clear(); + if (FAILED(hr)) + return hr; + memcpy(this, pSrc, sizeof(PROPVARIANT)); + pSrc->vt = VT_EMPTY; + return S_OK; +} + +HRESULT CPropVariant::Detach(PROPVARIANT* pDest) +{ + HRESULT hr = MyPropVariantClear(pDest); + if (FAILED(hr)) + return hr; + memcpy(pDest, this, sizeof(PROPVARIANT)); + vt = VT_EMPTY; + return S_OK; +} + +HRESULT CPropVariant::InternalClear() +{ + HRESULT hr = Clear(); + if (FAILED(hr)) + { + vt = VT_ERROR; + scode = hr; + } + return hr; +} + +void CPropVariant::InternalCopy(const PROPVARIANT* pSrc) +{ + HRESULT hr = Copy(pSrc); + if (FAILED(hr)) + { + vt = VT_ERROR; + scode = hr; + } +} + +int CPropVariant::Compare(const CPropVariant &a) +{ + if (vt != a.vt) + return 0; // it's mean some bug + switch (vt) + { + case VT_EMPTY: + return 0; + + /* + case VT_I1: + return MyCompare(cVal, a.cVal); + */ + case VT_UI1: + return MyCompare(bVal, a.bVal); + + case VT_I2: + return MyCompare(iVal, a.iVal); + case VT_UI2: + return MyCompare(uiVal, a.uiVal); + + case VT_I4: + return MyCompare(lVal, a.lVal); + /* + case VT_INT: + return MyCompare(intVal, a.intVal); + */ + case VT_UI4: + return MyCompare(ulVal, a.ulVal); + /* + case VT_UINT: + return MyCompare(uintVal, a.uintVal); + */ + case VT_I8: + return MyCompare(hVal.QuadPart, a.hVal.QuadPart); + case VT_UI8: + return MyCompare(uhVal.QuadPart, a.uhVal.QuadPart); + + case VT_BOOL: + return -MyCompare(boolVal, a.boolVal); + + case VT_FILETIME: + return ::CompareFileTime(&filetime, &a.filetime); + case VT_BSTR: + return 0; // Not implemented + // return MyCompare(aPropVarint.cVal); + + default: + return 0; + } +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/PropVariant.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/PropVariant.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,57 @@ +// Windows/PropVariant.h + +#ifndef __WINDOWS_PROPVARIANT_H +#define __WINDOWS_PROPVARIANT_H + +#include "../Common/MyWindows.h" +#include "../Common/Types.h" + +namespace NWindows { +namespace NCOM { + +class CPropVariant : public tagPROPVARIANT +{ +public: + CPropVariant() { vt = VT_EMPTY; wReserved1 = 0; } + ~CPropVariant() { Clear(); } + CPropVariant(const PROPVARIANT& varSrc); + CPropVariant(const CPropVariant& varSrc); + CPropVariant(BSTR bstrSrc); + CPropVariant(LPCOLESTR lpszSrc); + CPropVariant(bool bSrc) { vt = VT_BOOL; wReserved1 = 0; boolVal = (bSrc ? VARIANT_TRUE : VARIANT_FALSE); }; + CPropVariant(UInt32 value) { vt = VT_UI4; wReserved1 = 0; ulVal = value; } + CPropVariant(UInt64 value) { vt = VT_UI8; wReserved1 = 0; uhVal = *(ULARGE_INTEGER*)&value; } + CPropVariant(const FILETIME &value) { vt = VT_FILETIME; wReserved1 = 0; filetime = value; } + CPropVariant(Int32 value) { vt = VT_I4; wReserved1 = 0; lVal = value; } + CPropVariant(Byte value) { vt = VT_UI1; wReserved1 = 0; bVal = value; } + CPropVariant(Int16 value) { vt = VT_I2; wReserved1 = 0; iVal = value; } + // CPropVariant(LONG value, VARTYPE vtSrc = VT_I4) { vt = vtSrc; lVal = value; } + + CPropVariant& operator=(const CPropVariant& varSrc); + CPropVariant& operator=(const PROPVARIANT& varSrc); + CPropVariant& operator=(BSTR bstrSrc); + CPropVariant& operator=(LPCOLESTR lpszSrc); + CPropVariant& operator=(bool bSrc); + CPropVariant& operator=(UInt32 value); + CPropVariant& operator=(UInt64 value); + CPropVariant& operator=(const FILETIME &value); + + CPropVariant& operator=(Int32 value); + CPropVariant& operator=(Byte value); + CPropVariant& operator=(Int16 value); + // CPropVariant& operator=(LONG value); + + HRESULT Clear(); + HRESULT Copy(const PROPVARIANT* pSrc); + HRESULT Attach(PROPVARIANT* pSrc); + HRESULT Detach(PROPVARIANT* pDest); + + HRESULT InternalClear(); + void InternalCopy(const PROPVARIANT* pSrc); + + int Compare(const CPropVariant &a1); +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/StdAfx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/StdAfx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,9 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../Common/MyWindows.h" +#include "../Common/NewHandler.h" + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/Synchronization.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/Synchronization.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,168 @@ +// Windows/Synchronization.h + +#ifndef __WINDOWS_SYNCHRONIZATION_H +#define __WINDOWS_SYNCHRONIZATION_H + +#include "Defs.h" + +extern "C" +{ +#include "../../C/Threads.h" +} + +#ifdef _WIN32 +#include "Handle.h" +#endif + +namespace NWindows { +namespace NSynchronization { + +class CBaseEvent +{ +protected: + ::CEvent _object; +public: + bool IsCreated() { return Event_IsCreated(&_object) != 0; } + operator HANDLE() { return _object.handle; } + CBaseEvent() { Event_Construct(&_object); } + ~CBaseEvent() { Close(); } + WRes Close() { return Event_Close(&_object); } + #ifdef _WIN32 + WRes Create(bool manualReset, bool initiallyOwn, LPCTSTR name = NULL, + LPSECURITY_ATTRIBUTES securityAttributes = NULL) + { + _object.handle = ::CreateEvent(securityAttributes, BoolToBOOL(manualReset), + BoolToBOOL(initiallyOwn), name); + if (_object.handle != 0) + return 0; + return ::GetLastError(); + } + WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) + { + _object.handle = ::OpenEvent(desiredAccess, BoolToBOOL(inheritHandle), name); + if (_object.handle != 0) + return 0; + return ::GetLastError(); + } + #endif + + WRes Set() { return Event_Set(&_object); } + // bool Pulse() { return BOOLToBool(::PulseEvent(_handle)); } + WRes Reset() { return Event_Reset(&_object); } + WRes Lock() { return Event_Wait(&_object); } +}; + +class CManualResetEvent: public CBaseEvent +{ +public: + WRes Create(bool initiallyOwn = false) + { + return ManualResetEvent_Create(&_object, initiallyOwn ? 1: 0); + } + WRes CreateIfNotCreated() + { + if (IsCreated()) + return 0; + return ManualResetEvent_CreateNotSignaled(&_object); + } + #ifdef _WIN32 + WRes CreateWithName(bool initiallyOwn, LPCTSTR name) + { + return CBaseEvent::Create(true, initiallyOwn, name); + } + #endif +}; + +class CAutoResetEvent: public CBaseEvent +{ +public: + WRes Create() + { + return AutoResetEvent_CreateNotSignaled(&_object); + } + WRes CreateIfNotCreated() + { + if (IsCreated()) + return 0; + return AutoResetEvent_CreateNotSignaled(&_object); + } +}; + +#ifdef _WIN32 +class CObject: public CHandle +{ +public: + WRes Lock(DWORD timeoutInterval = INFINITE) + { return (::WaitForSingleObject(_handle, timeoutInterval) == WAIT_OBJECT_0 ? 0 : ::GetLastError()); } +}; +class CMutex: public CObject +{ +public: + WRes Create(bool initiallyOwn, LPCTSTR name = NULL, + LPSECURITY_ATTRIBUTES securityAttributes = NULL) + { + _handle = ::CreateMutex(securityAttributes, BoolToBOOL(initiallyOwn), name); + if (_handle != 0) + return 0; + return ::GetLastError(); + } + WRes Open(DWORD desiredAccess, bool inheritHandle, LPCTSTR name) + { + _handle = ::OpenMutex(desiredAccess, BoolToBOOL(inheritHandle), name); + if (_handle != 0) + return 0; + return ::GetLastError(); + } + WRes Release() + { + return ::ReleaseMutex(_handle) ? 0 : ::GetLastError(); + } +}; +class CMutexLock +{ + CMutex *_object; +public: + CMutexLock(CMutex &object): _object(&object) { _object->Lock(); } + ~CMutexLock() { _object->Release(); } +}; +#endif + +class CSemaphore +{ + ::CSemaphore _object; +public: + CSemaphore() { Semaphore_Construct(&_object); } + ~CSemaphore() { Close(); } + WRes Close() { return Semaphore_Close(&_object); } + operator HANDLE() { return _object.handle; } + WRes Create(UInt32 initiallyCount, UInt32 maxCount) + { + return Semaphore_Create(&_object, initiallyCount, maxCount); + } + WRes Release() { return Semaphore_Release1(&_object); } + WRes Release(UInt32 releaseCount) { return Semaphore_ReleaseN(&_object, releaseCount); } + WRes Lock() { return Semaphore_Wait(&_object); } +}; + +class CCriticalSection +{ + ::CCriticalSection _object; +public: + CCriticalSection() { CriticalSection_Init(&_object); } + ~CCriticalSection() { CriticalSection_Delete(&_object); } + void Enter() { CriticalSection_Enter(&_object); } + void Leave() { CriticalSection_Leave(&_object); } +}; + +class CCriticalSectionLock +{ + CCriticalSection *_object; + void Unlock() { _object->Leave(); } +public: + CCriticalSectionLock(CCriticalSection &object): _object(&object) {_object->Enter(); } + ~CCriticalSectionLock() { Unlock(); } +}; + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/Thread.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/Thread.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,38 @@ +// Windows/Thread.h + +#ifndef __WINDOWS_THREAD_H +#define __WINDOWS_THREAD_H + +#include "Defs.h" + +extern "C" +{ +#include "../../C/Threads.h" +} + +namespace NWindows { + +class CThread +{ + ::CThread thread; +public: + CThread() { Thread_Construct(&thread); } + ~CThread() { Close(); } + bool IsCreated() { return Thread_WasCreated(&thread) != 0; } + WRes Close() { return Thread_Close(&thread); } + WRes Create(THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), LPVOID parameter) + { return Thread_Create(&thread, startAddress, parameter); } + WRes Wait() { return Thread_Wait(&thread); } + + #ifdef _WIN32 + DWORD Resume() { return ::ResumeThread(thread.handle); } + DWORD Suspend() { return ::SuspendThread(thread.handle); } + bool Terminate(DWORD exitCode) { return BOOLToBool(::TerminateThread(thread.handle, exitCode)); } + int GetPriority() { return ::GetThreadPriority(thread.handle); } + bool SetPriority(int priority) { return BOOLToBool(::SetThreadPriority(thread.handle, priority)); } + #endif +}; + +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/Time.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/Time.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,86 @@ +// Windows/Time.cpp + +#include "StdAfx.h" + +#include "Time.h" +#include "Windows/Defs.h" + +namespace NWindows { +namespace NTime { + +bool DosTimeToFileTime(UInt32 dosTime, FILETIME &fileTime) +{ + return BOOLToBool(::DosDateTimeToFileTime((UInt16)(dosTime >> 16), (UInt16)(dosTime & 0xFFFF), &fileTime)); +} + +static const UInt32 kHighDosTime = 0xFF9FBF7D; +static const UInt32 kLowDosTime = 0x210000; + +bool FileTimeToDosTime(const FILETIME &fileTime, UInt32 &dosTime) +{ + WORD datePart, timePart; + if (!::FileTimeToDosDateTime(&fileTime, &datePart, &timePart)) + { + dosTime = (fileTime.dwHighDateTime >= 0x01C00000) ? kHighDosTime : kLowDosTime; + return false; + } + dosTime = (((UInt32)datePart) << 16) + timePart; + return true; +} + +static const UInt32 kNumTimeQuantumsInSecond = 10000000; +static const UInt64 kUnixTimeStartValue = ((UInt64)kNumTimeQuantumsInSecond) * 60 * 60 * 24 * 134774; + +void UnixTimeToFileTime(UInt32 unixTime, FILETIME &fileTime) +{ + UInt64 v = kUnixTimeStartValue + ((UInt64)unixTime) * kNumTimeQuantumsInSecond; + fileTime.dwLowDateTime = (DWORD)v; + fileTime.dwHighDateTime = (DWORD)(v >> 32); +} + +bool FileTimeToUnixTime(const FILETIME &fileTime, UInt32 &unixTime) +{ + UInt64 winTime = (((UInt64)fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime; + if (winTime < kUnixTimeStartValue) + { + unixTime = 0; + return false; + } + winTime = (winTime - kUnixTimeStartValue) / kNumTimeQuantumsInSecond; + if (winTime > 0xFFFFFFFF) + { + unixTime = 0xFFFFFFFF; + return false; + } + unixTime = (UInt32)winTime; + return true; +} + +bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day, + unsigned hour, unsigned min, unsigned sec, UInt64 &resSeconds) +{ + resSeconds = 0; + if (year < 1601 || year >= 10000 || month < 1 || month > 12 || + day < 1 || day > 31 || hour > 23 || min > 59 || sec > 59) + return false; + UInt32 numYears = year - 1601; + UInt32 numDays = numYears * 365 + numYears / 4 - numYears / 100 + numYears / 400; + Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) + ms[1] = 29; + month--; + for (unsigned i = 0; i < month; i++) + numDays += ms[i]; + numDays += day - 1; + resSeconds = ((UInt64)(numDays * 24 + hour) * 60 + min) * 60 + sec; + return true; +} + +void GetCurUtcFileTime(FILETIME &ft) +{ + SYSTEMTIME st; + GetSystemTime(&st); + SystemTimeToFileTime(&st, &ft); +} + +}} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/CPP/Windows/Time.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/CPP/Windows/Time.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,21 @@ +// Windows/Time.h + +#ifndef __WINDOWS_TIME_H +#define __WINDOWS_TIME_H + +#include "Common/Types.h" + +namespace NWindows { +namespace NTime { + +bool DosTimeToFileTime(UInt32 dosTime, FILETIME &fileTime); +bool FileTimeToDosTime(const FILETIME &fileTime, UInt32 &dosTime); +void UnixTimeToFileTime(UInt32 unixTime, FILETIME &fileTime); +bool FileTimeToUnixTime(const FILETIME &fileTime, UInt32 &unixTime); +bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day, + unsigned hour, unsigned min, unsigned sec, UInt64 &resSeconds); +void GetCurUtcFileTime(FILETIME &ft); + +}} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/DOC/License.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/DOC/License.txt Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,57 @@ +NOTE: The 7-zip library source is available under the LGPL, +however, this copy of it is distributed under the GPL 2.0. +This license has been modified accordingly. + + + 7-Zip source code + ~~~~~~~~~~~~~~~~~ + License for use and distribution + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + 7-Zip Copyright (C) 1999-2009 Igor Pavlov. + + Licenses for files are: + + 1) The 8 files in CPP/7zip/Compress/Rar*: GNU GPL 2.0 + unRAR restriction + 2) All other files: GNU GPL 2.0 + + The GNU GPL + unRAR restriction means that you must follow both + GNU GPL rules and unRAR restriction rules. + + + GNU GPL information + -------------------- + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + + unRAR restriction + ----------------- + + The decompression engine for RAR archives was developed using source + code of unRAR program. + All copyrights to original unRAR code are owned by Alexander Roshal. + + The license for original unRAR code has the following restriction: + + The unRAR sources cannot be used to re-create the RAR compression algorithm, + which is proprietary. Distribution of modified unRAR sources in separate form + or as a part of other software is permitted, provided that it is clearly + stated in the documentation and source comments that the code may + not be used to develop a RAR (WinRAR) compatible archiver. + + + -- + Igor Pavlov diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/DOC/gpl.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/DOC/gpl.txt Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/DOC/readme.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/DOC/readme.txt Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,49 @@ +This copy of the 7-zip source code has been modified as follows: +- Encoding support has been disabled, so only archive extraction is supported. +- Some archive formats have been removed, so only 7z, bzip2, gzip, lzh, lzma, rar, split, tar, and zip are supported. +- Support for using it as a static library has been added. (specifically, a file called InitializeStaticLib.h) +- Occasional minor fixes/customization not really worth describing in detail. +- The LGPL part of the 7-zip library source license has been upgraded to GPL 2.0 (as per condition 3 of the LGPL 2.1) + +You can find most of the files in this library in the "7-Zip Source code" download at http://www.7-zip.org/ distributed under the LGPL 2.1. +A small subset of them are in the LZMA SDK which is public domain and can be found at http://www.7-zip.org/sdk.html + + + +7-Zip 4.64 Sources +------------------ + +7-Zip is a file archiver for Windows 95/98/ME/NT/2000/2003/XP/Vista. + +7-Zip Copyright (C) 1999-2009 Igor Pavlov. + + +License Info +------------ + +This copy of 7-Zip is free software distributed under the GNU GPL 2.0 +(with an additional restriction that has always applied to the unRar code). +read License.txt for more infomation about license. + +Notes about unRAR license: + +Please check main restriction from unRar license: + + 2. The unRAR sources may be used in any software to handle RAR + archives without limitations free of charge, but cannot be used + to re-create the RAR compression algorithm, which is proprietary. + Distribution of modified unRAR sources in separate form or as a + part of other software is permitted, provided that it is clearly + stated in the documentation and source comments that the code may + not be used to develop a RAR (WinRAR) compatible archiver. + +In brief it means: +1) You can compile and use compiled files under GNU GPL rules, since + unRAR license almost has no restrictions for compiled files. + You can link these compiled files to GPL programs. +2) You can fix bugs in source code and use compiled fixed version. +3) You can not use unRAR sources to re-create the RAR compression algorithm. + +--- +Igor Pavlov +http://www.7-zip.org diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7z/DOC/unRarLicense.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7z/DOC/unRarLicense.txt Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,41 @@ + ****** ***** ****** unRAR - free utility for RAR archives + ** ** ** ** ** ** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ****** ******* ****** License for use and distribution of + ** ** ** ** ** ** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ** ** ** ** ** ** FREE portable version + ~~~~~~~~~~~~~~~~~~~~~ + + The source code of unRAR utility is freeware. This means: + + 1. All copyrights to RAR and the utility unRAR are exclusively + owned by the author - Alexander Roshal. + + 2. The unRAR sources may be used in any software to handle RAR + archives without limitations free of charge, but cannot be used + to re-create the RAR compression algorithm, which is proprietary. + Distribution of modified unRAR sources in separate form or as a + part of other software is permitted, provided that it is clearly + stated in the documentation and source comments that the code may + not be used to develop a RAR (WinRAR) compatible archiver. + + 3. The unRAR utility may be freely distributed. No person or company + may charge a fee for the distribution of unRAR without written + permission from the copyright holder. + + 4. THE RAR ARCHIVER AND THE UNRAR UTILITY ARE DISTRIBUTED "AS IS". + NO WARRANTY OF ANY KIND IS EXPRESSED OR IMPLIED. YOU USE AT + YOUR OWN RISK. THE AUTHOR WILL NOT BE LIABLE FOR DATA LOSS, + DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING + OR MISUSING THIS SOFTWARE. + + 5. Installing and using the unRAR utility signifies acceptance of + these terms and conditions of the license. + + 6. If you don't agree with terms of the license you must remove + unRAR files from your storage devices and cease to use the + utility. + + Thank you for your interest in RAR and unRAR. + + + Alexander L. Roshal \ No newline at end of file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7zip.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7zip.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,392 @@ +#include "7zip.h" + +#include "7z/C/Types.h" +#include "7z/CPP/7zip/Archive/IArchive.h" +#include "7z/CPP/Common/InitializeStaticLib.h" // important! (if using a static lib) + +#include +#include +#include +#include + +#include "7zipstreams.h" // defines OutStream and InFileStream + +STDAPI GetNumberOfFormats(UINT32 *numFormats); +STDAPI GetHandlerProperty2(UInt32 formatIndex, PROPID propID, PROPVARIANT *value); +STDAPI CreateObject(const GUID *clsid, const GUID *iid, void **outObject); + +struct ArchiveFormatInfo +{ + std::string name; + std::vector extensions; + std::string signature; + GUID guid; +}; + +static std::vector s_formatInfos; + +static std::string wstrToStr(const wchar_t* wstr) +{ +/* + // This thing wouldn't work + char* str = (char*)_alloca((wcslen(wstr)+1)); + sprintf(str, "%S", wstr); + return std::string(str); +*/ +// setlocale(LC_CTYPE, ".ACP"); + size_t n = wcstombs(NULL, wstr, 0); + if (n == (size_t)-1) // failed + { +// setlocale(LC_CTYPE, "C"); + return std::string(); + } + char* str = (char*)_alloca(n + 1); + wcstombs(str, wstr, n); + str[n] = '\0'; +// setlocale(LC_CTYPE, "C"); + return std::string(str); +} + +static std::vector tokenize(const std::string & str, const std::string & delim) +{ + std::vector tokens; + size_t p0 = 0, p1 = std::string::npos; + while(p0 != std::string::npos) + { + p1 = str.find_first_of(delim, p0); + if(p1 != p0) + { + std::string token = str.substr(p0, p1 - p0); + tokens.push_back(token); + } + p0 = str.find_first_not_of(delim, p1); + } + return tokens; +} + +static std::string s_supportedFormatsFilter; +const char* GetSupportedFormatsFilter() +{ + assert(!s_formatInfos.empty()); + if(s_supportedFormatsFilter.empty()) + { + s_supportedFormatsFilter = ""; + for(size_t i = 0; i < s_formatInfos.size(); i++) + { + for(size_t j = 0; j < s_formatInfos[i].extensions.size(); j++) + { + s_supportedFormatsFilter += ";*."; + s_supportedFormatsFilter += s_formatInfos[i].extensions[j]; + } + } + } + return s_supportedFormatsFilter.c_str(); +} + +void InitDecoder() +{ + CleanupDecoder(); + + UINT32 numFormats = 0; + GetNumberOfFormats(&numFormats); + + for(unsigned int i = 0; i < numFormats; i++) + { + PROPVARIANT var = {VT_EMPTY}; + ArchiveFormatInfo info; + + GetHandlerProperty2(i, NArchive::kName, &var); + if(var.vt == VT_BSTR) + info.name = wstrToStr(var.bstrVal); + + GetHandlerProperty2(i, NArchive::kExtension, &var); + if(var.vt == VT_BSTR) + info.extensions = tokenize(wstrToStr(var.bstrVal), " "); + + GetHandlerProperty2(i, NArchive::kStartSignature, &var); + if(var.vt == VT_BSTR) + info.signature = (const char*)var.bstrVal; // note: there's no 100% correct way of doing this with the existing 7-zip interface, but this is much better than using SysStringLen() in any way (it would return 1 for the signature "BZh", for example) + + GetHandlerProperty2(i,NArchive::kClassID,&var); + if(var.vt == VT_BSTR) + memcpy(&info.guid, var.bstrVal, 16); + else + memset(&info.guid, 0, 16); + + s_formatInfos.push_back(info); + + VariantClear((VARIANTARG*)&var); + } +} + +void CleanupDecoder() +{ + s_formatInfos.clear(); + s_supportedFormatsFilter.clear(); +} + +#include "7z/CPP/7zip/Archive/Zip/ZipHandler.h" + + +ArchiveFile::ArchiveFile(const char* filename, const char* displayFilename) +{ + assert(!s_formatInfos.empty()); + + m_typeIndex = -1; + m_numItems = 0; + m_items = NULL; + m_filename = NULL; + m_displayFilename = NULL; + m_userMadeSelection = false; + + FILE* file = fopen(filename, "rb"); + if(!file) + return; + + m_filename = new char[strlen(filename)+1]; + strcpy(m_filename, filename); + + if(displayFilename) + { + m_displayFilename = new char[strlen(displayFilename)+1]; + strcpy(m_displayFilename, displayFilename); + } + + // detect archive type using format signature in file + for(size_t i = 0; i < s_formatInfos.size() && m_typeIndex < 0; i++) + { + fseek(file, 0, SEEK_SET); + + std::string& formatSig = s_formatInfos[i].signature; + int len = formatSig.size(); + + if(len == 0) + continue; // because some formats have no signature + + char* fileSig = (char*)_alloca(len); + fread(fileSig, 1, len, file); + + if(!memcmp(formatSig.c_str(), fileSig, len)) + m_typeIndex = i; + } + + // if no signature match has been found, detect archive type using filename. + // this is only for signature-less formats + const char* fileExt = strrchr(filename, '.'); + if(fileExt++) + { + for(size_t i = 0; i < s_formatInfos.size() && m_typeIndex < 0; i++) + { + if(s_formatInfos[i].signature.empty()) + { + std::vector& formatExts = s_formatInfos[i].extensions; + for(size_t j = 0; j < formatExts.size(); j++) + { + if(!_stricmp(formatExts[j].c_str(), fileExt)) + { + m_typeIndex = i; + break; + } + } + } + } + } + + if(m_typeIndex < 0) + { + // uncompressed + + m_numItems = 1; + m_items = new ArchiveItem[m_numItems]; + + fseek(file, 0, SEEK_END); + m_items[0].size = ftell(file); + + m_items[0].name = new char[strlen(filename)+1]; + strcpy(m_items[0].name, filename); + } + else + { + IInArchive* object = NULL; + if(SUCCEEDED(CreateObject(&s_formatInfos[m_typeIndex].guid, &IID_IInArchive, (void**)&object))) + { + InFileStream* ifs = new InFileStream(filename); + if(SUCCEEDED(object->Open(ifs,0,0))) + { + UInt32 numItems = 0; + object->GetNumberOfItems(&numItems); + m_numItems = numItems; + m_items = new ArchiveItem[m_numItems]; + + for(int i = 0; i < m_numItems; i++) + { + PROPVARIANT var = {VT_EMPTY}; + ArchiveItem& item = m_items[i]; + + object->GetProperty(i, kpidSize, &var); + item.size = var.uhVal.LowPart; + + object->GetProperty(i, kpidPath, &var); + std::string& path = wstrToStr(var.bstrVal); + item.name = new char[path.size()+1]; + strcpy(item.name, path.c_str()); + + //object->GetProperty(i, kpidMethod, &var); + //std::string& method = wstrToStr(var.bstrVal); + //item.method = new char[method.size()+1]; + //strcpy(item.method, method.c_str()); + + object->GetProperty(i, kpidEncrypted, &var); +#ifdef _NO_CRYPTO + if(var.boolVal) + item.size = 0; // don't support decompressing it, pretend size zero +#else + #error password support NYI... see client7z.cpp + item.encrypted = !!var.boolVal; +#endif + + VariantClear((VARIANTARG*)&var); + } + + object->Close(); + } + + object->Release(); + } + } + + fclose(file); +} + +ArchiveFile::~ArchiveFile() +{ + for(int i = 0; i < m_numItems; i++) + { + delete[] m_items[i].name; + } + delete[] m_items; + delete[] m_filename; + delete[] m_displayFilename; +} + +const char* ArchiveFile::GetArchiveTypeName() +{ + assert(!s_formatInfos.empty()); + + if((size_t)m_typeIndex >= s_formatInfos.size()) + return ""; + + return s_formatInfos[m_typeIndex].name.c_str(); +} + +int ArchiveFile::GetNumItems() +{ + return m_numItems; +} + +int ArchiveFile::GetItemSize(int item) +{ + assert(item >= 0 && item < m_numItems); + if(!(item >= 0 && item < m_numItems)) return 0; + return m_items[item].size; +} + +const char* ArchiveFile::GetItemName(int item) +{ + //assert(item >= 0 && item < m_numItems); + if(!(item >= 0 && item < m_numItems)) return ""; + return m_items[item].name; +} + +bool ArchiveFile::IsCompressed() +{ + return (m_typeIndex >= 0); +} + +int ArchiveFile::ExtractItem(int index, unsigned char* outBuffer, int bufSize) const +{ + assert(!s_formatInfos.empty()); + //assert(index >= 0 && index < m_numItems); + if(!(index >= 0 && index < m_numItems)) return 0; + + ArchiveItem& item = m_items[index]; + + if(bufSize < item.size) + return 0; + + if(m_typeIndex < 0) + { + // uncompressed + FILE* file = fopen(m_filename, "rb"); + fread(outBuffer, 1, item.size, file); + fclose(file); + } + else + { + IInArchive* object = NULL; + HRESULT hr = E_FAIL; + if(SUCCEEDED(CreateObject(&s_formatInfos[m_typeIndex].guid, &IID_IInArchive, (void**)&object))) + { + InFileStream* ifs = new InFileStream(m_filename); + if(SUCCEEDED(object->Open(ifs,0,0))) + { + OutStream* os = new OutStream(index, outBuffer, item.size); + const UInt32 indices [1] = {index}; + hr = object->Extract(indices, 1, 0, os); + object->Close(); + } + object->Release(); + } + if(FAILED(hr)) + return 0; + } + + return item.size; +} + + + +int ArchiveFile::ExtractItem(int index, const char* outFilename) const +{ + assert(!s_formatInfos.empty()); + //assert(index >= 0 && index < m_numItems); + if(!(index >= 0 && index < m_numItems)) return 0; + + ArchiveItem& item = m_items[index]; + int rv = item.size; + + DWORD outAttributes = GetFileAttributes(outFilename); + if(outAttributes & FILE_ATTRIBUTE_READONLY) + SetFileAttributes(outFilename, outAttributes & ~FILE_ATTRIBUTE_READONLY); // temporarily remove read-only attribute so we can decompress to there + + if(m_typeIndex < 0) + { + // uncompressed + if(!CopyFile(m_filename, outFilename, false)) + rv = 0; + } + else + { + IInArchive* object = NULL; + HRESULT hr = E_FAIL; + if(SUCCEEDED(CreateObject(&s_formatInfos[m_typeIndex].guid, &IID_IInArchive, (void**)&object))) + { + InFileStream* ifs = new InFileStream(m_filename); + if(SUCCEEDED(object->Open(ifs,0,0))) + { + OutStream* os = new OutStream(index, outFilename); + const UInt32 indices [1] = {index}; + hr = object->Extract(indices, 1, 0, os); + object->Close(); + } + object->Release(); + } + if(FAILED(hr)) + rv = 0; + } + + if(outAttributes & FILE_ATTRIBUTE_READONLY) + SetFileAttributes(outFilename, outAttributes); // restore read-only attribute + + return rv; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7zip.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7zip.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,45 @@ +#ifndef _7ZIP_DEC_HEADER +#define _7ZIP_DEC_HEADER + +// 7zip file extraction +// NOTE: if you want to add support for opening files within archives to something, +// consider using the higher-level interface provided by OpenArchive.h instead + +void InitDecoder(); +void CleanupDecoder(); +const char* GetSupportedFormatsFilter(); + +// simplest way of extracting a file after calling InitDecoder(): +// int size = ArchiveFile(filename).ExtractItem(0, buf, sizeof(buf)); + +struct ArchiveFile +{ + ArchiveFile(const char* filename, const char* displayFilename=0); + virtual ~ArchiveFile(); + + int GetNumItems(); + int GetItemSize(int item); + const char* GetItemName(int item); + int ExtractItem(int item, unsigned char* outBuffer, int bufSize) const; // returns size, or 0 if failed + int ExtractItem(int item, const char* outFilename) const; + + bool IsCompressed(); + const char* GetArchiveTypeName(); + const char* GetArchiveFileName() { return m_displayFilename ? m_displayFilename : m_filename; } + + bool m_userMadeSelection; + +protected: + struct ArchiveItem + { + int size; + char* name; + }; + ArchiveItem* m_items; + int m_numItems; + int m_typeIndex; + char* m_filename; + char* m_displayFilename; +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7zip.rc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7zip.rc Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,97 @@ +// Microsoft Visual C++ generated resource script. +// +#include "../resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_ARCHIVEFILECHOOSER DIALOGEX 0, 0, 182, 113 +STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_THICKFRAME +CAPTION "Choose File" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + DEFPUSHBUTTON "&OK",IDOK,128,94,50,16 + PUSHBUTTON "&Cancel",ID_CANCEL,73,94,50,16 + LISTBOX IDC_LIST1,4,4,173,87,LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP +END + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO +BEGIN + IDD_ARCHIVEFILECHOOSER, DIALOG + BEGIN + RIGHTMARGIN, 181 + END +END +#endif // APSTUDIO_INVOKED + + + + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""afxres.h""\r\0" +END + +#endif // APSTUDIO_INVOKED + + +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/7zipstreams.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/7zipstreams.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,315 @@ +// This file is (modified) from +// FCEUX (2009) +// FCE Ultra - NES/Famicom Emulator +// Copyright (C) 2003 Xodnizel +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +#ifndef _7ZIPSTREAMS_HEADER +#define _7ZIPSTREAMS_HEADER + +#include "7z/CPP/Common/MyCom.h" + +class ICountedSequentialOutStream : public ISequentialOutStream +{ +public: + virtual UINT32 Size() const = 0; +}; + +class SeqMemoryOutStream : public ICountedSequentialOutStream, private CMyUnknownImp +{ + UINT8* const output; + UINT32 pos; + const UINT32 size; + ULONG refCount; + + HRESULT STDMETHODCALLTYPE QueryInterface(REFGUID, void**) + { + return E_NOINTERFACE; + } + + HRESULT STDMETHODCALLTYPE Write(const void* data, UInt32 length, UInt32* bytesWritten) + { + if (data != NULL || size == 0) + { + //assert(length <= size - pos); + + if (length > size - pos) + length = size - pos; + + if(data) + memcpy(output + pos, data, length); + pos += length; + + if (bytesWritten) + *bytesWritten = length; + + return S_OK; + } + else + { + return E_INVALIDARG; + } + } + + MY_ADDREF_RELEASE + +public: + + SeqMemoryOutStream(void* d, UINT32 s) : output((UINT8*)d), pos(0), size(s), refCount(0) {} + + virtual ~SeqMemoryOutStream() + { + int a = 0; + } + + UINT32 Size() const + { + return pos; + } +}; + +class SeqFileOutStream : public ICountedSequentialOutStream, private CMyUnknownImp +{ + FILE* file; + UINT32 pos; + ULONG refCount; + + HRESULT STDMETHODCALLTYPE QueryInterface(REFGUID, void**) + { + return E_NOINTERFACE; + } + + HRESULT STDMETHODCALLTYPE Write(const void* data, UInt32 length, UInt32* bytesWritten) + { + if(!file) + return E_FAIL; + + if (data != NULL) + { + int written = 0; + if(data) + written = fwrite(data, 1, length, file); + + pos += written; + if (bytesWritten) + *bytesWritten = written; + + return S_OK; + } + else + { + return E_INVALIDARG; + } + } + + MY_ADDREF_RELEASE + +public: + + SeqFileOutStream(const char* outFilename) : pos(0), refCount(0) + { + file = fopen(outFilename, "wb"); + } + virtual ~SeqFileOutStream() + { + if(file) + fclose(file); + } + + UINT32 Size() const + { + return pos; + } +}; + + +class OutStream : public IArchiveExtractCallback, private CMyUnknownImp +{ + ICountedSequentialOutStream* seqStream; + const UINT32 index; + ULONG refCount; + + HRESULT STDMETHODCALLTYPE QueryInterface(REFGUID, void**) + { + return E_NOINTERFACE; + } + + HRESULT STDMETHODCALLTYPE PrepareOperation(Int32) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE SetTotal(UInt64) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE SetCompleted(const UInt64*) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE SetOperationResult(Int32) + { + return S_OK; + } + + HRESULT STDMETHODCALLTYPE GetStream(UInt32 id, ISequentialOutStream** ptr, Int32 mode) + { + switch (mode) + { + case NArchive::NExtract::NAskMode::kExtract: + case NArchive::NExtract::NAskMode::kTest: + + if (id != index || ptr == NULL) + return S_FALSE; + else + *ptr = seqStream; + // fall through + case NArchive::NExtract::NAskMode::kSkip: + return S_OK; + + default: + return E_INVALIDARG; + } + } + + MY_ADDREF_RELEASE + +public: + + OutStream(UINT32 index, void* data, UINT32 size) : index(index), refCount(0) + { + seqStream = new SeqMemoryOutStream(data, size); + seqStream->AddRef(); + } + OutStream(UINT32 index, const char* outFilename) : index(index), refCount(0) + { + seqStream = new SeqFileOutStream(outFilename); + seqStream->AddRef(); + } + virtual ~OutStream() + { + //seqStream->Release(); // commented out because apparently IInArchive::Extract() calls Release one more time than it calls AddRef + } + UINT32 Size() const + { + return seqStream->Size(); + } +}; + +class InStream : public IInStream, private IStreamGetSize, private CMyUnknownImp +{ + ULONG refCount; + + HRESULT STDMETHODCALLTYPE QueryInterface(REFGUID, void**) + { + return E_NOINTERFACE; + } + + HRESULT STDMETHODCALLTYPE GetSize(UInt64* outSize) + { + if (outSize) + { + *outSize = size; + return S_OK; + } + else + { + return E_INVALIDARG; + } + } + + MY_ADDREF_RELEASE + +protected: + + UINT32 size; + +public: + + explicit InStream() : refCount(0) {} + virtual ~InStream() {} +}; + + +class InFileStream : public InStream +{ +public: + + virtual ~InFileStream() + { + if(file) + fclose(file); + } + + FILE* file; + + InFileStream(const char* fname) : file(NULL) + { + file = fopen(fname, "rb"); + if(file) + { + fseek(file, 0, SEEK_END); + size = ftell(file); + fseek(file, 0, SEEK_SET); + } + } + + HRESULT STDMETHODCALLTYPE Read(void* data, UInt32 length, UInt32* bytesRead) + { + if(!file) + return E_FAIL; + + if (data != NULL || length == 0) + { + int read = fread(data, 1, length, file); + + if (bytesRead) + *bytesRead = read; + + return S_OK; + } + else + { + return E_INVALIDARG; + } + } + + HRESULT STDMETHODCALLTYPE Seek(Int64 offset, UInt32 origin, UInt64* pos) + { + if(!file) + return E_FAIL; + + if (origin < 3) + { + fseek(file, (long)offset, origin); + origin = ftell(file); + + if (pos) + *pos = origin; + + return S_OK; + } + else + { + return E_INVALIDARG; + } + + } +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/OpenArchive.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/OpenArchive.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,713 @@ +#include "../stdafx.h" +#include "../resource.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "7zip.h" +//#include "G_main.h" +//#include "G_dsound.h" +#include "OpenArchive.h" + +LRESULT CALLBACK ArchiveFileChooser(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); +static int s_archiveFileChooserResult = -1; + +static HWND s_parentHWND = NULL; +void SetArchiveParentHWND(void* hwnd) { s_parentHWND = (HWND)hwnd; } +static HWND GetArchiveParentHWND() { return s_parentHWND ? s_parentHWND : AfxGetApp()->m_pMainWnd->GetSafeHwnd(); } + +static char Str_Tmp [2048]; + +struct ArchiveFileChooserInfo +{ + ArchiveFileChooserInfo(ArchiveFile& theArchive, const char** ignoreExtensions, int& numIgnoreExtensions) : archive(theArchive) + { +tryagain: + int numItems = archive.GetNumItems(); + for(int i = 0; i < numItems; i++) + { + if(archive.GetItemSize(i)) + { + const char* name = archive.GetItemName(i); + const char* ext = strrchr(name, '.'); + bool ok = true; + if(ext++) + { + for(int j = 0; j < numIgnoreExtensions; j++) + { + const char* ext2 = ignoreExtensions[j]; + const char* wild = strchr(ext2, '*'); + if(!wild) + { + if(!_stricmp(ext, ext2)) + { + ok = false; + break; + } + } + else // very limited (end only) wildcard support + { + if(!_strnicmp(ext, ext2, wild - ext2)) + { + ok = false; + break; + } + } + } + } + if(ok) + { + ArchiveFileChooserInfo::FileInfo fi = { name, i }; + files.push_back(fi); + } + } + } + + if(files.empty() && numIgnoreExtensions) + { + // try again without any exclusions if we excluded everything in the archive + numIgnoreExtensions = 0; + goto tryagain; + } + + // strip away prefix paths that are common to all the files + bool stripping = !files.empty(); + while(stripping) + { + const char* firstName = files[0].name.c_str(); + const char* slash = strchr(firstName, '\\'); + const char* slash2 = strchr(firstName, '/'); + slash = max(slash, slash2); + if(!slash++) + break; + for(size_t i = 1; i < files.size(); i++) + if(strncmp(firstName, files[i].name.c_str(), slash - firstName)) + stripping = false; + if(stripping) + for(size_t i = 0; i < files.size(); i++) + files[i].name = files[i].name.substr(slash - firstName, files[i].name.length() - (slash - firstName)); + } + + // sort by filename + std::sort(files.begin(), files.end(), FileInfo::Sort); + } + +//protected: + + struct FileInfo + { + std::string name; + int itemIndex; + + static bool Sort(const FileInfo& elem1, const FileInfo& elem2) + { + int comp = elem1.name.compare(elem2.name); + return comp ? (comp < 0) : (elem1.itemIndex < elem2.itemIndex); + } + }; + + ArchiveFile& archive; + std::vector files; +}; + +static void ClearLayoutStates(); + +int ChooseItemFromArchive(ArchiveFile& archive, bool autoChooseIfOnly1, const char** ignoreExtensions, int numIgnoreExtensions) +{ + int prevNumIgnoreExtensions = numIgnoreExtensions; + archive.m_userMadeSelection = false; + + // prepare a list of files to choose from the archive + ArchiveFileChooserInfo info (archive, ignoreExtensions, numIgnoreExtensions); + + // based on our list, decide which item in the archive to choose + + // check if there's nothing + if(info.files.size() < 1) + { + MessageBox(GetArchiveParentHWND(), "The archive is either empty or encrypted.", "Nothing to load!", MB_OK | MB_ICONWARNING); + return -1; + } + + // warn if all the files in the archive have extensions we should ignore + if(numIgnoreExtensions != prevNumIgnoreExtensions) + { + CString msg; + msg.Format("The archive appears to only contain the wrong type of files.\n\n(in \"%s\")", archive.GetArchiveFileName()); + int answer = MessageBox(GetArchiveParentHWND(), msg, "Warning", MB_OKCANCEL | MB_ICONWARNING | MB_DEFBUTTON2); + if(answer == IDCANCEL) + return -1; + } + + // if there's only 1 item, choose it + if(info.files.size() == 1 && autoChooseIfOnly1 && numIgnoreExtensions == prevNumIgnoreExtensions) + return info.files[0].itemIndex; + + // bring up a dialog to choose the index if there's more than 1 + DialogBoxParam(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD_ARCHIVEFILECHOOSER), GetArchiveParentHWND(), (DLGPROC) ArchiveFileChooser,(LPARAM) &info); + archive.m_userMadeSelection = (s_archiveFileChooserResult != -1); + return s_archiveFileChooserResult; +} + + + + +#define DEFAULT_EXTENSION ".tmp" +#define DEFAULT_CATEGORY "vba" + +static struct TempFiles +{ + struct TemporaryFile + { + TemporaryFile(const char* cat, const char* ext) + { + if(!ext || !*ext) ext = DEFAULT_EXTENSION; + if(!cat || !*cat) cat = DEFAULT_CATEGORY; + category = cat; + + char tempPath [2048]; + GetTempPath(2048, tempPath); + //GetTempFileName(tempPath, cat, 0, filename, ext); // alas + + char*const fname = tempPath + strlen(tempPath); + unsigned short start = (unsigned short)(timeGetTime() & 0xFFFF); + unsigned short n = start + 1; + while(n != start) + { + _snprintf(fname, 2048 - (fname - tempPath), "%s%04X%s", cat, n, ext); + FILE* file = fopen(tempPath, "wb"); + if(file) + { + // mark the temporary file as read-only and (whatever this does) temporary + DWORD attributes = GetFileAttributes(tempPath); + attributes |= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_TEMPORARY; + SetFileAttributes(tempPath, attributes); + + fclose(file); + + // add it to our registry of files that need to be deleted, in case we fail to terminate properly + TempFiles::AddEntryToGarbageRegistry(tempPath); + + break; + } + n++; + } + strcpy(filename, tempPath); + } + TemporaryFile(const TemporaryFile& copy) + { + strcpy(filename, copy.filename); + category = copy.category; + } + TemporaryFile() + { + filename[0] = 0; + // category[0] = 0; // error + } + bool Delete(bool returnFalseOnRegistryRemovalFailure=false) + { + if(!*filename) + return true; // guess it already didn't exist + + // remove read-only attribute so Windows will let us delete it + // (our temporary files are read-only to discourage other apps from tampering) + DWORD attributes = GetFileAttributes(filename); + if(attributes & FILE_ATTRIBUTE_READONLY) + SetFileAttributes(filename, attributes & ~FILE_ATTRIBUTE_READONLY); + + if(_unlink(filename) == 0 || errno != EACCES) + { + // remove it from our registry of files that need to be deleted, to reduce accumulation + bool removed = TempFiles::RemoveEntryFromGarbageRegistry(filename); + + *filename = '\0'; + return removed || !returnFalseOnRegistryRemovalFailure; // successfully deleted or already didn't exist, return true unless registry removal failure notification was requested and that failed + } + + // restore read-only if we couldn't delete it (not sure if this ever succeeds or matters though) + if(attributes & FILE_ATTRIBUTE_READONLY) + SetFileAttributes(filename, attributes); + + return false; // failed to delete read-only or in-use file + } + char filename [MAX_PATH]; + std::string category; + }; + + std::vector tempFiles; + + const char* GetFile(const char* category, const char* extension) + { + tempFiles.push_back(TemporaryFile(category, extension)); + return tempFiles.back().filename; + } + + void ReleaseFile(const char* filename) + { + for(int i = (int)tempFiles.size()-1; i >= 0; i--) + { + if(!strcmp(filename, tempFiles[i].filename)) + { + if(tempFiles[i].Delete()) + tempFiles.erase(tempFiles.begin() + i); + } + } + } + + void ReleaseCategory(const char* cat, const char* exceptionFilename) + { + for(int i = (int)tempFiles.size()-1; i >= 0; i--) + { + if(!strcmp(cat, tempFiles[i].category.c_str()) && + (!exceptionFilename || + strcmp(exceptionFilename, tempFiles[i].filename))) + { + if(tempFiles[i].Delete()) + tempFiles.erase(tempFiles.begin() + i); + } + } + } + + // delete all temporary files on shutdown + ~TempFiles() + { + for(size_t i = 0; i < tempFiles.size(); i++) + { + tempFiles[i].Delete(); + } + + TempFiles::CleanOutGarbageRegistry(); + } + + // run this on startup to delete any files that we failed to delete last time + // in case we crashed or were forcefully terminated + TempFiles() + { + TempFiles::CleanOutGarbageRegistry(); + } + + static void AddEntryToGarbageRegistry(const char* filename) + { + char gbgFile[2048]; + GetTempPath(2048, gbgFile); + strcat(gbgFile, "VBATempFileRecords"); + char key[64]; + int i = 0; + while(true) + { + sprintf(key, "File%d", i); + GetPrivateProfileString("Files", key, "", Str_Tmp, 2048, gbgFile); + if(!*Str_Tmp) + break; + i++; + } + WritePrivateProfileString("Files", key, filename, gbgFile); + } + static bool RemoveEntryFromGarbageRegistry(const char* filename) + { + char gbgFile[2048]; + GetTempPath(2048, gbgFile); + strcat(gbgFile, "VBATempFileRecords"); + char key[64]; + int i = 0; + int deleteSlot = -1; + while(true) + { + sprintf(key, "File%d", i); + GetPrivateProfileString("Files", key, "", Str_Tmp, 2048, gbgFile); + if(!*Str_Tmp) + break; + if(!strcmp(Str_Tmp, filename)) + deleteSlot = i; + i++; + } + --i; + if(i >= 0 && deleteSlot >= 0) + { + if(i != deleteSlot) + { + sprintf(key, "File%d", i); + GetPrivateProfileString("Files", key, "", Str_Tmp, 2048, gbgFile); + sprintf(key, "File%d", deleteSlot); + WritePrivateProfileString("Files", key, Str_Tmp, gbgFile); + } + sprintf(key, "File%d", i); + if(0 == WritePrivateProfileString("Files", key, NULL, gbgFile)) + return false; + } + if(i <= 0 && deleteSlot == 0) + _unlink(gbgFile); + return true; + } + +private: + static void CleanOutGarbageRegistry() + { + char gbgFile[2048]; + GetTempPath(2048, gbgFile); + strcat(gbgFile, "VBATempFileRecords"); + + char key[64]; + int i = 0; + while(true) + { + sprintf(key, "File%d", i); + GetPrivateProfileString("Files", key, "", Str_Tmp, 2048, gbgFile); + if(!*Str_Tmp) + break; + TemporaryFile temp; + strcpy(temp.filename, Str_Tmp); + if(!temp.Delete(true)) + i++; + } + } + +} s_tempFiles; + + +const char* GetTempFile(const char* category, const char* extension) +{ + return s_tempFiles.GetFile(category, extension); +} +void ReleaseTempFile(const char* filename) +{ + s_tempFiles.ReleaseFile(filename); +} +void ReleaseTempFileCategory(const char* cat, const char* exceptionFilename) +{ + if(!cat || !*cat) cat = DEFAULT_CATEGORY; + s_tempFiles.ReleaseCategory(cat, exceptionFilename); +} + + + +// example input Name: "C:\games.zip" +// example output LogicalName: "C:\games.zip|Metroid.gba" +// example output PhysicalName: "C:\Documents and Settings\User\Local Settings\Temp\VBA\rom7A37.gba" +// assumes arguments are character buffers with 2048 bytes each +bool ObtainFile(const char* Name, char *const & LogicalName, char *const & PhysicalName, const char* category, const char** ignoreExtensions, int numIgnoreExtensions) +{ +restart: + char ArchivePaths [2048]; + strcpy(LogicalName, Name); + strcpy(PhysicalName, Name); + strcpy(ArchivePaths, Name); + char* bar = strchr(ArchivePaths, '|'); + if(bar) + { + PhysicalName[bar - ArchivePaths] = 0; // doesn't belong in the physical name + LogicalName[bar - ArchivePaths] = 0; // we'll reconstruct the logical name as we go + *bar++ = 0; // bar becomes the next logical archive path component + } + + bool userSelected = false; + + while(true) + { + ArchiveFile archive (PhysicalName, LogicalName); + if(!archive.IsCompressed()) + { + if(archive.GetNumItems() > 0) + return true; + else + { + // failed or cancelled... backtrack to outermost archive if not already there + char* div = NULL; + if(LogicalName[strlen(LogicalName)-1] == '|') + { + LogicalName[strlen(LogicalName)-1] = '\0'; + div = strrchr(LogicalName, '|'); + } + if(div && userSelected) + goto restart; + else + return false; + } + } + else + { + int item = -1; + bool forceManual = false; + if(bar && *bar) // try following the in-archive part of the logical path + { + char* bar2 = strchr(bar, '|'); + if(bar2) *bar2++ = 0; + int numItems = archive.GetNumItems(); + for(int i = 0; i < numItems; i++) + { + if(archive.GetItemSize(i)) + { + const char* itemName = archive.GetItemName(i); + if(!_stricmp(itemName, bar)) + { + item = i; // match found, now we'll auto-follow the path + break; + } + } + } + if(item < 0) + { + forceManual = true; // we don't want it choosing something else without user permission + bar = NULL; // remaining archive path is invalid + } + else + bar = bar2; // advance to next archive path part + } + if(item < 0) + item = ChooseItemFromArchive(archive, !forceManual, ignoreExtensions, numIgnoreExtensions); + + userSelected |= archive.m_userMadeSelection; + + const char* TempFileName = s_tempFiles.GetFile(category, strrchr(archive.GetItemName(item), '.')); + if(!archive.ExtractItem(item, TempFileName)) + s_tempFiles.ReleaseFile(TempFileName); + s_tempFiles.ReleaseFile(PhysicalName); + strcpy(PhysicalName, TempFileName); + _snprintf(LogicalName + strlen(LogicalName), 2048 - (strlen(LogicalName)+1), "|%s", archive.GetItemName(item)); + } + } +} + + + +struct ControlLayoutInfo +{ + int controlID; + + enum LayoutType // what to do when the containing window resizes + { + NONE, // leave the control where it was + RESIZE_END, // resize the control + MOVE_START, // move the control + }; + LayoutType horizontalLayout; + LayoutType verticalLayout; +}; +struct ControlLayoutState +{ + int x,y,width,height; + bool valid; + ControlLayoutState() : valid(false) {} +}; + +static ControlLayoutInfo controlLayoutInfos [] = { + {IDC_LIST1, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::RESIZE_END}, + {IDOK, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::MOVE_START}, + {ID_CANCEL, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::MOVE_START}, +}; +static const int numControlLayoutInfos = sizeof(controlLayoutInfos)/sizeof(*controlLayoutInfos); + +static ControlLayoutState s_layoutState [numControlLayoutInfos]; +static int s_windowWidth = 182, s_windowHeight = 113; + + +LRESULT CALLBACK ArchiveFileChooser(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + RECT r, r2; + int dx1, dy1, dx2, dy2; + static std::map s_listToItemsMap; + + switch(uMsg) + { + case WM_INITDIALOG: + { + //Clear_Sound_Buffer(); + + //if(Full_Screen) + //{ + // while (ShowCursor(false) >= 0); + // while (ShowCursor(true) < 0); + //} + + for(int i = 0; i < numControlLayoutInfos; i++) + s_layoutState[i].valid = false; + + GetWindowRect(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), &r); + dx1 = (r.right - r.left) / 2; + dy1 = (r.bottom - r.top) / 2; + + GetWindowRect(hDlg, &r2); + dx2 = (r2.right - r2.left) / 2; + dy2 = (r2.bottom - r2.top) / 2; + + //SetWindowPos(hDlg, NULL, max(0, r.left + (dx1 - dx2)), max(0, r.top + (dy1 - dy2)), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + + ArchiveFileChooserInfo& info = *(ArchiveFileChooserInfo*)lParam; + std::vector& files = info.files; + ArchiveFile& archive = info.archive; + + std::string title = "Choose File in "; + title += archive.GetArchiveTypeName(); + title += " Archive"; + SetWindowText(hDlg, title.c_str()); + + // populate list + for(size_t i = 0; i < files.size(); i++) + { + int listIndex = SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, (WPARAM) 0, (LONG) (LPTSTR) files[i].name.c_str()); + s_listToItemsMap[listIndex] = files[i].itemIndex; + } + + SendDlgItemMessage(hDlg, IDC_LIST1, LB_SETCURSEL, (WPARAM) 0, (LPARAM) 0); + + { + RECT r3; + GetClientRect(hDlg, &r3); + s_windowWidth = r3.right - r3.left; + s_windowHeight = r3.bottom - r3.top; + } + + return true; + } break; + + case WM_SIZING: + { + // enforce a minimum window size + + LPRECT r = (LPRECT) lParam; + int minimumWidth = 281; + int minimumHeight = 117; + if(r->right - r->left < minimumWidth) + if(wParam == WMSZ_LEFT || wParam == WMSZ_TOPLEFT || wParam == WMSZ_BOTTOMLEFT) + r->left = r->right - minimumWidth; + else + r->right = r->left + minimumWidth; + if(r->bottom - r->top < minimumHeight) + if(wParam == WMSZ_TOP || wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOPRIGHT) + r->top = r->bottom - minimumHeight; + else + r->bottom = r->top + minimumHeight; + return TRUE; + } + + case WM_SIZE: + { + // resize or move controls in the window as necessary when the window is resized + + int prevDlgWidth = s_windowWidth; + int prevDlgHeight = s_windowHeight; + + int dlgWidth = LOWORD(lParam); + int dlgHeight = HIWORD(lParam); + + int deltaWidth = dlgWidth - prevDlgWidth; + int deltaHeight = dlgHeight - prevDlgHeight; + + for(int i = 0; i < numControlLayoutInfos; i++) + { + ControlLayoutInfo layoutInfo = controlLayoutInfos[i]; + ControlLayoutState& layoutState = s_layoutState[i]; + + HWND hCtrl = GetDlgItem(hDlg,layoutInfo.controlID); + + int x,y,width,height; + if(layoutState.valid) + { + x = layoutState.x; + y = layoutState.y; + width = layoutState.width; + height = layoutState.height; + } + else + { + RECT r; + GetWindowRect(hCtrl, &r); + POINT p = {r.left, r.top}; + ScreenToClient(hDlg, &p); + x = p.x; + y = p.y; + width = r.right - r.left; + height = r.bottom - r.top; + } + + switch(layoutInfo.horizontalLayout) + { + case ControlLayoutInfo::RESIZE_END: width += deltaWidth; break; + case ControlLayoutInfo::MOVE_START: x += deltaWidth; break; + default: break; + } + switch(layoutInfo.verticalLayout) + { + case ControlLayoutInfo::RESIZE_END: height += deltaHeight; break; + case ControlLayoutInfo::MOVE_START: y += deltaHeight; break; + default: break; + } + + SetWindowPos(hCtrl, 0, x,y, width,height, 0); + + layoutState.x = x; + layoutState.y = y; + layoutState.width = width; + layoutState.height = height; + layoutState.valid = true; + } + + s_windowWidth = dlgWidth; + s_windowHeight = dlgHeight; + + RedrawWindow(hDlg, NULL, NULL, RDW_INVALIDATE); + } + break; + + case WM_COMMAND: + switch(LOWORD(wParam)) + { + case IDC_LIST1: + if(HIWORD(wParam) == LBN_DBLCLK) + { + POINT pos; + GetCursorPos(&pos); + int clickedItem = LBItemFromPt(GetDlgItem(hDlg, IDC_LIST1), pos, FALSE); + if(clickedItem != -1) + { + SendMessage(hDlg, WM_COMMAND, IDOK, 0); + } + } + return TRUE; + + case IDOK: + { + int listIndex = SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0); + s_archiveFileChooserResult = s_listToItemsMap[listIndex]; + s_listToItemsMap.clear(); + //if(Full_Screen) + //{ + // while (ShowCursor(true) < 0); + // while (ShowCursor(false) >= 0); + //} + EndDialog(hDlg, false); + } return TRUE; + + case ID_CANCEL: + case IDCANCEL: + s_archiveFileChooserResult = -1; + s_listToItemsMap.clear(); + //if(Full_Screen) + //{ + // while (ShowCursor(true) < 0); + // while (ShowCursor(false) >= 0); + //} + EndDialog(hDlg, false); + return TRUE; + } + + case WM_CLOSE: + s_archiveFileChooserResult = -1; + s_listToItemsMap.clear(); + //if(Full_Screen) + //{ + // while (ShowCursor(true) < 0); + // while (ShowCursor(false) >= 0); + //} + EndDialog(hDlg, false); + return TRUE; + } + + return false; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/7zip/OpenArchive.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/7zip/OpenArchive.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,43 @@ +// for retrieving files from archives and/or managing temporary files + +#ifndef OPENARCHIVE_HEADER +#define OPENARCHIVE_HEADER + +#include "7zip.h" + +// ObtainFile() +// this is the main, high-level function for opening possibly-compressed files. +// you don't need to check whether the file is compressed beforehand, +// this function will figure that out and work correctly either way. +// it also does the work of bringing up a within-archive file selector dialog if necessary, +// which even allows navigating to a file within an archive that's within the archive. +// the output PhysicalName is the filename of an uncompressed file +// for you to load with fopen or whatever, +// unless the function fails (or is cancelled) in which case it will return false. +// example input Name: "C:\games.zip" +// example output LogicalName: "C:\games.zip|Metroid.gba" +// example output PhysicalName: "C:\Documents and Settings\User\Local Settings\Temp\VBA\rom7A37.gba" +// assumes the three name arguments are distinct character buffers with exactly 2048 bytes each +bool ObtainFile(const char* Name, char *const & LogicalName, char *const & PhysicalName, const char* category=NULL, const char** ignoreExtensions=NULL, int numIgnoreExtensions=0); + +// ReleaseTempFileCategory() +// this is for deleting the temporary files that ObtainFile() can create. +// using it is optional because they will auto-delete on proper shutdown of the program, +// but it's nice to be able to clean up the files as early as possible. +// pass in the same "category" string you passed into ObtainFile(), +// and this will delete all files of that category. +// you can optionally specify one filename to not delete even if its category matches. +// note that any still-open files cannot be deleted yet and will be skipped. +void ReleaseTempFileCategory(const char* category, const char* exceptionFilename=NULL); + +// sets the parent window of subsequent archive selector dialogs +// NULL resets this to the default (main VBA emulator window) +void SetArchiveParentHWND(void* hwnd=NULL); + +// the rest of these are more internal utility functions, +// but they could be generally useful outside of that +const char* GetTempFile(const char* category=NULL, const char* extension=NULL); // creates a temp file and returns a path to it. extension if any should include the '.' +void ReleaseTempFile(const char* filename); // deletes a particular temporary file, by filename +int ChooseItemFromArchive(ArchiveFile& archive, bool autoChooseIfOnly1=true, const char** ignoreExtensions=0, int numIgnoreExtensions=0); // gets an index to a file within an already-open archive, using the file chooser if there's more than one choice + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/AVIWrite.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/AVIWrite.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,281 @@ +#include "stdafx.h" +#include +#include + +#include "AVIWrite.h" +#include "../common/System.h" + +AVIWrite::AVIWrite() +{ + m_failed = false; + m_file = NULL; + m_stream = NULL; + m_streamCompressed = NULL; + m_streamSound = NULL; + m_videoFrames = 0; + m_samplesSound = 0; + m_videoFramesTotal = 0; + m_samplesSoundTotal= 0; + m_totalBytes = 0; + m_segmentNumber = 0; + m_usePrevOptions = false; + m_pauseRecording = false; + + AVIFileInit(); +} + +void AVIWrite::CleanUp() +{ + if (m_streamSound) + { + AVIStreamClose(m_streamSound); + m_streamSound = NULL; + } + if (m_streamCompressed) + { + AVIStreamClose(m_streamCompressed); + m_streamCompressed = NULL; + } + if (m_stream) + { + AVIStreamClose(m_stream); + m_stream = NULL; + } + if (m_file) + { + AVIFileClose(m_file); + m_file = NULL; + } +} + +AVIWrite::~AVIWrite() +{ + CleanUp(); + AVIFileExit(); +} + +void AVIWrite::SetVideoFormat(BITMAPINFOHEADER *bh) +{ + // force size to 0x28 to avoid extra fields + memcpy(&m_bitmap, bh, 0x28); +} + +void AVIWrite::SetSoundFormat(WAVEFORMATEX *format) +{ + memcpy(&m_soundFormat, format, sizeof(WAVEFORMATEX)); + ZeroMemory(&m_soundHeader, sizeof(AVISTREAMINFO)); + // setup the sound stream header + m_soundHeader.fccType = streamtypeAUDIO; + m_soundHeader.dwQuality = (DWORD)-1; + m_soundHeader.dwScale = format->nBlockAlign; + m_soundHeader.dwInitialFrames = 1; + m_soundHeader.dwRate = format->nAvgBytesPerSec; + m_soundHeader.dwSampleSize = format->nBlockAlign; + + // create the sound stream + if (FAILED(AVIFileCreateStream(m_file, &m_streamSound, &m_soundHeader))) + { + m_failed = true; + return; + } + + // setup the sound stream format + if (FAILED(AVIStreamSetFormat(m_streamSound, 0, (void *)&m_soundFormat, + sizeof(WAVEFORMATEX)))) + { + m_failed = true; + return; + } +} + +bool AVIWrite::Open(const char *filename) +{ + // this is only here because AVIFileOpen doesn't seem to do it for us + FILE*fd = fopen(filename, "wb"); + if (!fd) + { + systemMessage(0, "AVI recording failed: file is read-only or already in use."); + m_failed = true; + return false; + } + fclose(fd); + + // create the AVI file + if (FAILED(AVIFileOpen(&m_file, + filename, + OF_WRITE | OF_CREATE, + NULL))) + { + m_failed = true; + return false; + } + // setup the video stream information + ZeroMemory(&m_header, sizeof(AVISTREAMINFO)); + m_header.fccType = streamtypeVIDEO; + m_header.dwScale = 1; + m_header.dwRate = m_fps; + m_header.dwSuggestedBufferSize = m_bitmap.biSizeImage; + + // create the video stream + if (FAILED(AVIFileCreateStream(m_file, + &m_stream, + &m_header))) + { + m_failed = true; + return false; + } + + if (!m_usePrevOptions) + { + ZeroMemory(&m_options, sizeof(AVICOMPRESSOPTIONS)); + m_arrayOptions[0] = &m_options; + + // call the dialog to setup the compress options to be used + if (!AVISaveOptions(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), 0, 1, &m_stream, m_arrayOptions)) + { + m_failed = true; + return false; + } + } + + // create the compressed stream + if (FAILED(AVIMakeCompressedStream(&m_streamCompressed, m_stream, &m_options, NULL))) + { + m_failed = true; + return false; + } + + // setup the video stream format + if (FAILED(AVIStreamSetFormat(m_streamCompressed, 0, + &m_bitmap, + m_bitmap.biSize + + m_bitmap.biClrUsed * sizeof(RGBQUAD)))) + { + m_failed = true; + return false; + } + + m_videoFrames = 0; + m_samplesSound = 0; + m_totalBytes = 0; + if (!m_usePrevOptions) { + m_videoFramesTotal = 0; + m_samplesSoundTotal = 0; + } + + strncpy(m_aviFileName, filename, MAX_PATH); + strncpy(m_aviBaseName, filename, MAX_PATH); + m_aviFileName[MAX_PATH - 1] = '\0'; + m_aviBaseName[MAX_PATH - 1] = '\0'; + char *dot = strrchr(m_aviBaseName, '.'); + if (dot && dot > strrchr(m_aviBaseName, '/') && dot > strrchr(m_aviBaseName, '\\')) + { + strcpy(m_aviExtension, dot); + dot[0] = '\0'; + } + + return true; +} + +bool AVIWrite::AddSound(const u8 *sound, int len) +{ + LONG byteBuffer; + + // return if we failed somewhere already + if (m_failed) + return false; + + assert(len % m_soundFormat.nBlockAlign == 0); + int samples = len / m_soundFormat.nBlockAlign; + + if (FAILED(AVIStreamWrite(m_streamSound, + m_samplesSound, + samples, + (LPVOID)sound, + len, + 0, + NULL, + &byteBuffer))) + { + m_failed = true; + return false; + } + m_samplesSound += samples; + m_samplesSoundTotal += samples; + m_totalBytes += byteBuffer; + return true; +} + +bool AVIWrite::NextSegment() +{ + char avi_fname[MAX_PATH]; + strcpy(avi_fname, m_aviBaseName); + char avi_fname_temp[MAX_PATH]; + sprintf(avi_fname_temp, "%s_part%d%s", avi_fname, m_segmentNumber+2, m_aviExtension); + m_segmentNumber++; + + CleanUp(); + + m_usePrevOptions = true; + bool ret = Open(avi_fname_temp); + m_usePrevOptions = false; + strcpy(m_aviBaseName, avi_fname); + + return ret; +} + +bool AVIWrite::AddFrame(const u8 *bmp) +{ + LONG byteBuffer; + + if (m_failed) + return false; + + // write the frame to the video stream + if (FAILED(AVIStreamWrite(m_streamCompressed, + m_videoFrames, + 1, + (LPVOID)bmp, + m_bitmap.biSizeImage, + AVIIF_KEYFRAME, + NULL, + &byteBuffer))) + { + m_failed = true; + return false; + } + m_videoFrames++; + m_videoFramesTotal++; + m_totalBytes += byteBuffer; + + // segment / split AVI when it's almost 2 GB (2000MB, to be precise) + if (!(m_videoFrames % 60) && m_totalBytes > 2097152000) + return NextSegment(); + else + return true; +} + +bool AVIWrite::IsSoundAdded() +{ + return m_streamSound != NULL; +} + +void AVIWrite::SetFPS(int f) +{ + m_fps = f; +} + +int AVIWrite::videoFrames() +{ + return m_videoFramesTotal; +} + +void AVIWrite::Pause(bool pause) +{ + m_pauseRecording = pause; +} + +bool AVIWrite::IsPaused() +{ + return m_pauseRecording; +} \ No newline at end of file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/AVIWrite.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/AVIWrite.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,54 @@ +#ifndef VBA_WIN32_AVIWRITE_H +#define VBA_WIN32_AVIWRITE_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include + +class AVIWrite { + public: + AVIWrite(); + virtual ~AVIWrite(); + + bool Open(const char *filename); + virtual bool AddFrame(const u8 *bmp); + void SetFPS(int fps); + void SetVideoFormat(BITMAPINFOHEADER *); + bool IsSoundAdded(); + void SetSoundFormat(WAVEFORMATEX *); + bool AddSound(const u8 *sound, int len); + int videoFrames(); + void Pause(bool pause); + bool IsPaused(); + + private: + int m_fps; + WAVEFORMATEX m_soundFormat; + BITMAPINFOHEADER m_bitmap; + AVISTREAMINFO m_header; + AVISTREAMINFO m_soundHeader; + PAVIFILE m_file; + PAVISTREAM m_stream; + PAVISTREAM m_streamCompressed; + PAVISTREAM m_streamSound; + AVICOMPRESSOPTIONS m_options; + AVICOMPRESSOPTIONS *m_arrayOptions[1]; + int m_videoFrames; + int m_samplesSound; + int m_videoFramesTotal; + int m_samplesSoundTotal; + LONG m_totalBytes; + bool m_failed; + int m_segmentNumber; + bool m_usePrevOptions; + bool m_pauseRecording; + char m_aviFileName[MAX_PATH]; + char m_aviBaseName[MAX_PATH]; + char m_aviExtension[MAX_PATH]; + void CleanUp(); + bool NextSegment(); +}; + +#endif // VBA_WIN32_AVIWRITE_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/AboutDialog.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/AboutDialog.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,60 @@ +// AboutDialog.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "AboutDialog.h" + +#include "../version.h" + +///////////////////////////////////////////////////////////////////////////// +// AboutDialog dialog + +AboutDialog::AboutDialog(CWnd*pParent /*=NULL*/) + : CDialog(AboutDialog::IDD, pParent) +{ + //{{AFX_DATA_INIT(AboutDialog) + m_version = _T(VBA_VERSION_STRING); + //}}AFX_DATA_INIT +} + +void AboutDialog::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AboutDialog) + DDX_Text(pDX, IDC_VERSION, m_version); + DDX_Control(pDX, IDC_URL, m_link); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(AboutDialog, CDialog) +//{{AFX_MSG_MAP(AboutDialog) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// AboutDialog message handlers + +BOOL AboutDialog::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CWnd *p = GetDlgItem(IDC_TRANSLATOR_URL); + if (p) + { + m_translator.SubclassDlgItem(IDC_TRANSLATOR_URL, this); + } + + m_link.SetWindowText(VBA_RR_SITE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void AboutDialog::OnOK() +{ + // TODO: Add extra validation here + + CDialog::OnOK(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/AboutDialog.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/AboutDialog.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,52 @@ +#if !defined(AFX_ABOUTDIALOG_H__48D787B2_0699_4F03_827D_404EC70DDDB2__INCLUDED_) +#define AFX_ABOUTDIALOG_H__48D787B2_0699_4F03_827D_404EC70DDDB2__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +// -*- C++ -*- +// AboutDialog.h : header file +// + +#include "Hyperlink.h" + +///////////////////////////////////////////////////////////////////////////// +// AboutDialog dialog + +class AboutDialog : public CDialog +{ + Hyperlink m_link; + Hyperlink m_translator; + // Construction +public: + AboutDialog(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AboutDialog) + enum { IDD = IDD_ABOUT }; + CString m_version; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AboutDialog) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(AboutDialog) + virtual BOOL OnInitDialog(); + virtual void OnOK(); + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ABOUTDIALOG_H__48D787B2_0699_4F03_827D_404EC70DDDB2__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/AccelEditor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/AccelEditor.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,659 @@ +// AccelEditor.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "AccelEditor.h" +#include "CmdAccelOb.h" +#include "VBA.h" + +///////////////////////////////////////////////////////////////////////////// +// AccelEditor dialog + +AccelEditor::AccelEditor(CWnd *pParent, CMenu *pMenu, CAcceleratorManager *pExtMgr) + : ResizeDlg(AccelEditor::IDD, pParent), m_pMenuSrc(pMenu), m_pExtMgr(pExtMgr) +{ + //{{AFX_DATA_INIT(AccelEditor) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +BOOL AccelEditor::IsModified() const +{ + return m_modified; +} + +const CAcceleratorManager &AccelEditor::GetResultMangager() const +{ + return m_result; +} + +void AccelEditor::DoDataExchange(CDataExchange *pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AccelEditor) + DDX_Control(pDX, IDC_CURRENTS, m_currents); + DDX_Control(pDX, IDC_ALREADY_AFFECTED, m_alreadyAffected); + DDX_Control(pDX, IDC_COMMANDS, m_commands); + DDX_Control(pDX, IDC_EDIT_KEY, m_key); + DDX_Control(pDX, IDC_ACCELEDIT_AUTOTIMEOUT, m_timeout); + DDX_Control(pDX, IDC_ACCELEDIT_PROGRESSBAR, m_progress); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(AccelEditor, CDialog) +//{{AFX_MSG_MAP(AccelEditor) +ON_BN_CLICKED(ID_OK, &AccelEditor::OnOk) +ON_BN_CLICKED(ID_CANCEL, &AccelEditor::OnCancel) +ON_BN_CLICKED(IDC_ACCELEDIT_APPLY, &AccelEditor::OnApply) +ON_BN_CLICKED(IDC_RESET, &AccelEditor::OnReset) +ON_BN_CLICKED(IDC_ASSIGN, &AccelEditor::OnAssign) +ON_BN_CLICKED(IDC_REMOVE, &AccelEditor::OnRemove) +ON_BN_CLICKED(IDC_ACCELEDIT_REPLACE, &AccelEditor::OnReplace) +ON_CONTROL(EN_CHANGE, IDC_EDIT_KEY, &AccelEditor::OnKeyboardEditChange) +ON_CONTROL(EN_KILLFOCUS, IDC_EDIT_KEY, &AccelEditor::OnKeyboardEditKillfocus) +ON_CONTROL(EN_SETFOCUS, IDC_ACCELEDIT_AUTOTIMEOUT, &AccelEditor::OnTimeoutEditSetfocus) +ON_CONTROL(EN_KILLFOCUS, IDC_ACCELEDIT_AUTOTIMEOUT, &AccelEditor::OnTimeoutEditKillfocus) +ON_NOTIFY(TVN_SELCHANGED, IDC_COMMANDS, &AccelEditor::OnTvnSelchangedCommands) +//ON_NOTIFY(LVN_ITEMCHANGED, IDC_CURRENTS, &AccelEditor::OnListItemChanged) +ON_NOTIFY(NM_DBLCLK, IDC_CURRENTS, &AccelEditor::OnListDblClick) +ON_NOTIFY(NM_CLICK, IDC_CURRENTS, &AccelEditor::OnListClick) +ON_WM_TIMER() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// AccelEditor message handlers + +BOOL AccelEditor::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(ID_OK, DS_MoveX) + DIALOG_SIZER_ENTRY(ID_CANCEL, DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_ACCELEDIT_APPLY, DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_ASSIGN, DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_REMOVE, DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_ACCELEDIT_REPLACE, DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_COMMANDS, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_CURRENTS, DS_MoveX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_EDIT_KEY, DS_MoveX | DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_EDIT_KEY, DS_MoveX | DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_STATIC2, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_STATIC3, DS_MoveX | DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_STATIC4, DS_MoveX | DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_STATIC5, DS_MoveX | DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_ACCELEDIT_AUTOTIMEOUT, DS_SizeX | DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\AccelEditor", + NULL); + + if (m_pExtMgr) + m_result = m_mgr = *m_pExtMgr; + + m_currents.SetExtendedStyle(LVS_EX_FULLROWSELECT); + m_currents.InsertColumn(0, "Keys"); + m_currents.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER); + InitCommands(); + m_autoMode = AUTO_REPLACE; + m_modified = FALSE; + m_timeoutValue = 1000; + CString timeoutStr; + timeoutStr.Format("%d", m_timeoutValue); + m_timeout.SetWindowText(timeoutStr); + m_progress.SetPos(0); + + GetDlgItem(IDC_ACCELEDIT_APPLY)->EnableWindow(FALSE); + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void AccelEditor::AddCommandsFromTable() +{ + POSITION pos = m_mgr.m_mapAccelString.GetStartPosition(); + while (pos != NULL) + { + CString command; + WORD wID; + m_mgr.m_mapAccelString.GetNextAssoc(pos, command, wID); + int nPos = command.Find('\\'); + + if (nPos == 0) // skip menu commands + { + continue; + } + + HTREEITEM newItem = TVI_ROOT; +#if 0 +/* + while (nPos != -1) + { + newItem = m_commands.InsertItem(command.Left(nPos), newItem); + command.Delete(0, nPos + 1); + nPos = command.Find('\\'); + } + */ +#endif + newItem = m_commands.InsertItem(command, newItem); + m_commands.SetItemData(newItem, wID); + m_hItems.AddTail(newItem); + } +} + +// recursive calls +void AccelEditor::AddCommandsFromMenu(CMenu *pMenu, HTREEITEM hParent) +{ + UINT nIndexMax = pMenu->GetMenuItemCount(); + for (UINT nIndex = 0; nIndex < nIndexMax; ++nIndex) + { + UINT nID = pMenu->GetMenuItemID(nIndex); + if (nID == 0) + continue; // menu separator or invalid cmd - ignore it + + if (nID == (UINT)-1) + { + // possibly a submenu + CMenu *pSubMenu = pMenu->GetSubMenu(nIndex); + if (pSubMenu != NULL) + { + CString tempStr; + pMenu->GetMenuString(nIndex, tempStr, MF_BYPOSITION); + tempStr.Remove('&'); + HTREEITEM newItem = m_commands.InsertItem(tempStr, hParent); + AddCommandsFromMenu(pSubMenu, newItem); + } + } + else + { + // normal menu item + // generate the strings + CString command; + pMenu->GetMenuString(nIndex, command, MF_BYPOSITION); + int nPos = command.ReverseFind('\t'); + if (nPos != -1) + { + command.Delete(nPos, command.GetLength() - nPos); + } + command.Remove('&'); + HTREEITEM newItem = m_commands.InsertItem(command, hParent); + m_commands.SetItemData(newItem, nID); + m_hItems.AddTail(newItem); + } + } +} + +void AccelEditor::InitCommands() +{ + m_commands.DeleteAllItems(); + m_hItems.RemoveAll(); + m_alreadyAffected.SetWindowText(""); + + AddCommandsFromMenu(m_pMenuSrc, TVI_ROOT); + AddCommandsFromTable(); +} + +BOOL AccelEditor::PreTranslateMessage(MSG *pMsg) +{ + CWnd *pFocus = GetFocus(); + if (pFocus == &m_currents) + { + if (pMsg->message == WM_KEYDOWN) + { + switch (pMsg->wParam) + { + case VK_ESCAPE: + m_currents.SetItemState(-1, 0, LVIS_SELECTED); + CheckListSelections(); + break; + case VK_RETURN: + case VK_INSERT: + // kludge to workaround CKeyboardEdit::PreTranslateMessage() + break; + case VK_DELETE: + case VK_BACK: + OnRemove(); + break; + case VK_F6: + case VK_LEFT: + m_commands.SetFocus(); + break; + case VK_RIGHT: + GetDlgItem(ID_OK)->SetFocus(); + default: + return ResizeDlg::PreTranslateMessage(pMsg); + } + return TRUE; + } + else if (pMsg->message == WM_KEYUP) // kludge to workaround CKeyboardEdit::PreTranslateMessage() + { + switch (pMsg->wParam) + { + case VK_RETURN: + OnEdit(); + break; + case VK_INSERT: + OnNew(); + break; + default: + return ResizeDlg::PreTranslateMessage(pMsg); + } + return TRUE; + } + } + else if (pFocus == &m_commands) + { + if (pMsg->message == WM_KEYDOWN) + { + switch (pMsg->wParam) + { + case VK_F6: + m_currents.SetFocus(); + break; + case VK_RIGHT: + if (!m_commands.ItemHasChildren(m_commands.GetSelectedItem())) + { + m_currents.SetFocus(); + break; + } + // fall through + default: + return ResizeDlg::PreTranslateMessage(pMsg); + } + return TRUE; + } + } + + return ResizeDlg::PreTranslateMessage(pMsg); +} + +void AccelEditor::OnOk() +{ + OnApply(); +// OnTimeoutEditKillfocus(); + EndDialog(TRUE); +} + +void AccelEditor::OnCancel() +{ +// OnTimeoutEditKillfocus(); +// EndDialog(m_modified); + EndDialog(FALSE); // this allows the caller to cancel even if the user has Apply'ed +} + +void AccelEditor::OnApply() +{ + m_result = m_mgr; + GetDlgItem(IDC_ACCELEDIT_APPLY)->EnableWindow(FALSE); +} + +void AccelEditor::OnReset() +{ + m_mgr.Default(); /// FIXME accelerator reset NYI + systemMessage( + 0, + "The \"Reset All Accelerators\" feature is currently unimplemented.\nYou can achieve the same result by closing VBA, opening up your \"vba.ini\" file, deleting the line that starts with \"keyboard\", then reopening VBA."); + InitCommands(); // update the listboxes. +} + +void AccelEditor::OnAssign() +{ + if (CheckAffected()) + return; + + // get the currently selected group + HTREEITEM hItem = m_commands.GetSelectedItem(); + if (hItem == NULL) + return; // abort + + // Get the object who manage the accels list, associated to the command. + WORD wIDCommand = LOWORD(m_commands.GetItemData(hItem)); + + CCmdAccelOb *pCmdAccel; + if (m_mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) != TRUE) + return; + + WORD wKey; + bool bCtrl, bAlt, bShift; + if (!m_key.GetAccelKey(wKey, bCtrl, bAlt, bShift)) + return; // no valid key, abort + + BYTE cVirt = 0; + if (bCtrl) + cVirt |= FCONTROL; + if (bAlt) + cVirt |= FALT; + if (bShift) + cVirt |= FSHIFT; + + cVirt |= FVIRTKEY; + + // Create the new key... + CAccelsOb *pAccel = new CAccelsOb(cVirt, wKey, false); + ASSERT(pAccel != NULL); + // ...and add in the list. + pCmdAccel->m_Accels.AddTail(pAccel); + + // Update the listbox. + CString szBuffer; + pAccel->GetString(szBuffer); + + int index = m_currents.GetNextItem(-1, LVNI_SELECTED); + if (index < 0) + index = 0; + m_currents.InsertItem(index, szBuffer); + m_currents.SetItemData(index, reinterpret_cast(pAccel)); + m_currents.SetItemState(-1, 0, LVIS_SELECTED); // deselect other items first + m_currents.SetItemState(index, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED); + GetDlgItem(IDC_REMOVE)->EnableWindow(TRUE); + GetDlgItem(IDC_ACCELEDIT_REPLACE)->EnableWindow(TRUE); + + // Reset the key editor. +// m_key.ResetKey(); + + m_modified = TRUE; + GetDlgItem(IDC_ACCELEDIT_APPLY)->EnableWindow(TRUE); +} + +void AccelEditor::OnRemove() +{ + // Some controls + POSITION selected = m_currents.GetFirstSelectedItemPosition(); + if (selected == NULL) + return; + + HTREEITEM hItem = m_commands.GetSelectedItem(); + if (hItem == NULL) + return; + + // Ref to the ID command + WORD wIDCommand = LOWORD(m_commands.GetItemData(hItem)); + + // Run through the accels, and control if it can be deleted. + CCmdAccelOb *pCmdAccel; + if (m_mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE) + { + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + POSITION PrevPos; + while (pos != NULL) + { + PrevPos = pos; + CAccelsOb *pAccel = pCmdAccel->m_Accels.GetNext(pos); + do + { + int indexCurrent = m_currents.GetNextSelectedItem(selected); + CAccelsOb *pAccelCurrent = reinterpret_cast(m_currents.GetItemData(indexCurrent)); + if (pAccel == pAccelCurrent) + { + if (!pAccel->m_bLocked) + { + // not locked, so we delete the key + pCmdAccel->m_Accels.RemoveAt(PrevPos); + delete pAccel; + // and update the listboxes/key editor/static text + m_currents.DeleteItem(indexCurrent); + m_modified = TRUE; + break; + } + else + { + systemMessage(0, "Unable to remove this locked accelerator: ", m_currents.GetItemText(indexCurrent, KEY_COLUMN)); + m_currents.SetItemState(indexCurrent, 0, LVIS_SELECTED); // deselect it + break; + } + } + } + while (selected != NULL); + + selected = m_currents.GetFirstSelectedItemPosition(); + if (selected == NULL) // the normal exit of this function + { + m_currents.SetItemState(m_currents.GetNextItem(-1, LVIS_FOCUSED), LVIS_SELECTED, LVIS_SELECTED); + if (m_currents.GetSelectedCount() == 0) + { + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ACCELEDIT_REPLACE)->EnableWindow(FALSE); + } + GetDlgItem(IDC_ACCELEDIT_APPLY)->EnableWindow(m_modified); + return; + } + } + systemMessage(0, "internal error (AccelEditor::Remove : pAccel unavailable)"); + return; + } + systemMessage(0, "internal error (AccelEditor::Remove : Lookup failed)"); +} + +void AccelEditor::OnReplace() +{ + if (CheckAffected()) + return; + OnRemove(); + OnAssign(); +} + +void AccelEditor::OnNew() +{ + m_autoMode = AUTO_NEW; + m_key.SetFocus(); +} + +void AccelEditor::OnEdit() +{ + m_autoMode = AUTO_REPLACE; + m_key.SetFocus(); +} + +BOOL AccelEditor::CheckAffected() +{ + m_alreadyAffected.SetWindowText(""); + + WORD wKey; + bool bCtrl, bAlt, bShift; + if (!m_key.GetAccelKey(wKey, bCtrl, bAlt, bShift)) + return TRUE; // no valid key, abort + + POSITION posItem = m_hItems.GetHeadPosition(); + while (posItem != NULL) + { + HTREEITEM hItem = m_hItems.GetNext(posItem); + WORD wIDCommand2 = LOWORD(m_commands.GetItemData(hItem)); + + CCmdAccelOb *pCmdAccel; + m_mgr.m_mapAccelTable.Lookup(wIDCommand2, pCmdAccel); + + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) + { + CAccelsOb *pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel->IsEqual(wKey, bCtrl, bAlt, bShift)) + { + // the key is already affected (in the same or other command) + // (the parts that were commented out allow for a one-to-many mapping, + // which is only disabled because the MFC stuff that automagically activates the commands + // doesn't seem to be capable of activating more than one command per accelerator...) + m_alreadyAffected.SetWindowText(pCmdAccel->m_szCommand); + return TRUE; + } + } + } + + return FALSE; +} + +BOOL AccelEditor::CheckJammed() +{ + WORD jam; + if (m_key.GetJamKey(jam)) + { + // these go first, or the timer would be set again + m_key.ResetKey(); + m_key.SetWindowText("Interrupted"); + if (m_currents.IsWindowEnabled()) + m_currents.SetFocus(); + else + m_commands.SetFocus(); + return TRUE; + } + return FALSE; +} + +BOOL AccelEditor::CheckListSelections() +{ + BOOL result = m_currents.GetFirstSelectedItemPosition() ? TRUE : FALSE; + + GetDlgItem(IDC_REMOVE)->EnableWindow(result); + GetDlgItem(IDC_ACCELEDIT_REPLACE)->EnableWindow(result); + + return result; +} + +void AccelEditor::OnTvnSelchangedCommands(NMHDR *pNMHDR, LRESULT *pResult) +{ +// LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); + + // TODO: Add your control notification handler code here + // Check if some commands exist. + HTREEITEM hItem = m_commands.GetSelectedItem(); + if (hItem == NULL) + return; + + m_currents.DeleteAllItems(); + + WORD wIDCommand = LOWORD(m_commands.GetItemData(hItem)); + CCmdAccelOb *pCmdAccel; + if (m_mgr.m_mapAccelTable.Lookup(wIDCommand, pCmdAccel)) + { + CAccelsOb *pAccel; + CString szBuffer; + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + + // Add the keys to the 'currents keys' listbox. + while (pos != NULL) + { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + pAccel->GetString(szBuffer); + int index = m_currents.InsertItem(m_currents.GetItemCount(), szBuffer); + // and a pointer to the accel object. + m_currents.SetItemData(index, (DWORD)pAccel); + } + + m_currents.SetItemState(0, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED); + GetDlgItem(IDC_ASSIGN)->EnableWindow(TRUE); + m_currents.EnableWindow(TRUE); + } + else + { + GetDlgItem(IDC_ASSIGN)->EnableWindow(FALSE); + m_currents.EnableWindow(FALSE); + } + + // Init the key editor +// m_pKey->ResetKey(); +// m_alreadyAffected.SetWindowText(""); + + CheckListSelections(); + + *pResult = 0; +} + +/* +void AccelEditor::OnListItemChanged(NMHDR *pNMHDR, LRESULT *pResult) +{ + NMLISTVIEW *pNMListView = reinterpret_cast(pNMHDR); + if (pNMListView->uChanged == LVIF_STATE) + { + if ((pNMListView->uOldState & LVIS_SELECTED) && !(pNMListView->uNewState & LVIS_SELECTED)) + { + } + } + + *pResult = 0; +} +*/ + +void AccelEditor::OnListClick(NMHDR *pNMHDR, LRESULT *pResult) +{ + CheckListSelections(); + *pResult = 0; +} + +void AccelEditor::OnListDblClick(NMHDR *pNMHDR, LRESULT *pResult) +{ + if (m_currents.GetFirstSelectedItemPosition()) + OnEdit(); + else + OnNew(); + *pResult = 0; +} + +void AccelEditor::OnKeyboardEditChange() +{ + if (!m_key.IsDefined()) + return; + +// if (CheckJammed()) +// return; + + OnKeyboardEditKillfocus(); + CheckAffected(); + if (m_timeoutValue == 0) + return; + + m_progress.SetRange32(0, m_timeoutValue); + SetTimer(1, 50, NULL); +} + +void AccelEditor::OnKeyboardEditKillfocus() +{ + KillTimer(1); + m_timer = 0; + m_progress.SetPos(0); + m_progress.SetBarColor(RGB(128, 0, 255)); +} + +void AccelEditor::OnTimeoutEditSetfocus() +{ + m_timeout.PostMessage(EM_SETSEL, 0, -1); +} + +void AccelEditor::OnTimeoutEditKillfocus() +{ + CString str; + m_timeout.GetWindowText(str); + m_timeoutValue = atoi(str); + m_autoMode = AUTO_REPLACE; +} + +void AccelEditor::OnTimer(UINT_PTR nIDEvent) +{ + if (nIDEvent == 1) + { + m_timer += 50; + if (m_timer >= m_timeoutValue) + { + m_progress.SetPos(m_timeoutValue); + m_progress.SetBarColor(RGB(255, 255, 0)); + if (m_autoMode == AUTO_NEW) + { + OnAssign(); + } + else + { + OnReplace(); + } + if (m_currents.IsWindowEnabled()) + m_currents.SetFocus(); + else + m_commands.SetFocus(); + return; + } + UINT green = (m_timer * 255 / m_timeoutValue) ; + m_progress.SetBarColor(RGB(128 + green / 2, green, 255 - green)); + m_progress.SetPos(m_timer); + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/AccelEditor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/AccelEditor.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,93 @@ +#if !defined(AFX_ACCELEDITOR_H__66F5C854_E28E_40D1_B763_1850374B46A2__INCLUDED_) +#define AFX_ACCELEDITOR_H__66F5C854_E28E_40D1_B763_1850374B46A2__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// AccelEditor.h : header file +// +#include "AcceleratorManager.h" +#include "KeyboardEdit.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// AccelEditor dialog + +class AccelEditor : public ResizeDlg +{ + // Construction +public: + void InitCommands(); + void AddCommandsFromTable(); + void AddCommandsFromMenu(CMenu *pMenu, HTREEITEM hParent); + BOOL IsModified() const; + const CAcceleratorManager &GetResultMangager() const; + AccelEditor(CWnd *pParent, CMenu *pMenu, CAcceleratorManager *pExtMgr = NULL); // non-standard constructor + virtual BOOL PreTranslateMessage(MSG *pMsg); + + // Dialog Data + //{{AFX_DATA(AccelEditor) +protected: + enum { IDD = IDD_ACCEL_EDITOR }; + enum { KEY_COLUMN = 0 }; + enum { AUTO_REPLACE = 0, AUTO_NEW }; + CListCtrl m_currents; + CStatic m_alreadyAffected; + CTreeCtrl m_commands; + CKeyboardEdit m_key; + CEdit m_timeout; + CProgressCtrl m_progress; + CList m_hItems; + + int m_timeoutValue; + int m_timer; + int m_autoMode; + BOOL m_modified; + CAcceleratorManager m_mgr, m_result; + CAcceleratorManager *m_pExtMgr; + CMenu *m_pMenuSrc; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AccelEditor) +protected: + virtual void DoDataExchange(CDataExchange *pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + BOOL CheckAffected(); + BOOL CheckJammed(); + BOOL CheckListSelections(); + + // Generated message map functions + //{{AFX_MSG(AccelEditor) + virtual BOOL OnInitDialog(); + afx_msg void OnOk(); + afx_msg void OnCancel(); + afx_msg void OnApply(); + afx_msg void OnReset(); + afx_msg void OnAssign(); + afx_msg void OnRemove(); + afx_msg void OnReplace(); + afx_msg void OnNew(); + afx_msg void OnEdit(); + + afx_msg void OnTvnSelchangedCommands(NMHDR *pNMHDR, LRESULT *pResult); +// afx_msg void OnListItemChanged(NMHDR *pNMHDR, LRESULT *pResult); + afx_msg void OnListClick(NMHDR *pNMHDR, LRESULT *pResult); + afx_msg void OnListDblClick(NMHDR *pNMHDR, LRESULT *pResult); + afx_msg void OnKeyboardEditChange(); + afx_msg void OnKeyboardEditKillfocus(); + afx_msg void OnTimeoutEditSetfocus(); + afx_msg void OnTimeoutEditKillfocus(); + afx_msg void OnTimer(UINT_PTR nIDEvent); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ACCELEDITOR_H__66F5C854_E28E_40D1_B763_1850374B46A2__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/AcceleratorManager.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/AcceleratorManager.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,729 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : AcceleratorManager.cpp +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Author : T.Maurel +// Date : 17.08.98 +// +// Remarks : implementation of the CAcceleratorManager class. +// +//////////////////////////////////////////////////////////////////////////////// +// Modified by aquanull: +// All win32 registry stuff removed + +#include "stdafx.h" +#include // MIIM_STRING +#include "resource.h" +#include "AcceleratorManager.h" +#include "CmdAccelOb.h" +#include "Reg.h" +#include "VBA.h" +//#include "../common/System.h" + +CAcceleratorManager::CAcceleratorManager() +{ + m_bAutoSave = FALSE; + m_pWndConnected = NULL; + + m_bDefaultTable = false; +} + +CAcceleratorManager::~CAcceleratorManager() +{ +#if 0 + if (m_bAutoSave) + { + bool bRet = Write(); + if (!bRet) + systemMessage(0, "CAcceleratorManager::~CAcceleratorManager\nError in CAcceleratorManager::Write..."); + } +#endif + Reset(); +} + +CAcceleratorManager & CAcceleratorManager::operator=(const CAcceleratorManager& accelmgr) +{ + Reset(); + + CCmdAccelOb*pCmdAccel; + CCmdAccelOb*pNewCmdAccel; + WORD wKey; + // Copy the 2 tables : normal accel table... + POSITION pos = accelmgr.m_mapAccelTable.GetStartPosition(); + while (pos != NULL) + { + accelmgr.m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + pNewCmdAccel = new CCmdAccelOb; + ASSERT(pNewCmdAccel != NULL); + *pNewCmdAccel = *pCmdAccel; + m_mapAccelTable.SetAt(wKey, pNewCmdAccel); + } + // ... and saved accel table. + pos = accelmgr.m_mapAccelTableSaved.GetStartPosition(); + while (pos != NULL) + { + accelmgr.m_mapAccelTableSaved.GetNextAssoc(pos, wKey, pCmdAccel); + pNewCmdAccel = new CCmdAccelOb; + ASSERT(pNewCmdAccel != NULL); + *pNewCmdAccel = *pCmdAccel; + m_mapAccelTableSaved.SetAt(wKey, pNewCmdAccel); + } + + // The Strings-ID table + CString szKey; + pos = accelmgr.m_mapAccelString.GetStartPosition(); + while (pos != NULL) + { + accelmgr.m_mapAccelString.GetNextAssoc(pos, szKey, wKey); + m_mapAccelString.SetAt(szKey, wKey); + } + m_bDefaultTable = accelmgr.m_bDefaultTable; + + return *this; +} + +////////////////////////////////////////////////////////////////////// +// Internal fcts +// +void CAcceleratorManager::Reset() +{ + CCmdAccelOb*pCmdAccel; + WORD wKey; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) + { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + delete pCmdAccel; + } + m_mapAccelTable.RemoveAll(); + m_mapAccelString.RemoveAll(); + + pos = m_mapAccelTableSaved.GetStartPosition(); + while (pos != NULL) + { + m_mapAccelTableSaved.GetNextAssoc(pos, wKey, pCmdAccel); + delete pCmdAccel; + } + m_mapAccelTableSaved.RemoveAll(); +} + +bool CAcceleratorManager::AddAccel(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked) +{ + ASSERT(szCommand != NULL); + + WORD wIDCmd; + if (m_mapAccelString.Lookup(szCommand, wIDCmd) == TRUE) + { + if (wIDCmd != wIDCommand) + return false; + } + + CCmdAccelOb*pCmdAccel = NULL; + if (m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE) + { + if (pCmdAccel->m_szCommand != szCommand) + { + return false; + } + CAccelsOb*pAccel; + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel->m_cVirt == cVirt && + pAccel->m_wKey == wKey) + return FALSE; + } + // Adding the accelerator + pCmdAccel->Add(cVirt, wKey, bLocked); + } + else + { + pCmdAccel = new CCmdAccelOb(cVirt, wIDCommand, wKey, szCommand, bLocked); + ASSERT(pCmdAccel != NULL); + m_mapAccelTable.SetAt(wIDCommand, pCmdAccel); + } + // 2nd table + m_mapAccelString.SetAt(szCommand, wIDCommand); + return true; +} + +////////////////////////////////////////////////////////////////////// +// Debug fcts +// +#ifdef _DEBUG +void CAcceleratorManager::AssertValid() const +{} + +void CAcceleratorManager::Dump(CDumpContext& dc) const +{ + CCmdAccelOb*pCmdAccel; + WORD wKey; + dc << "CAcceleratorManager::Dump :\n"; + dc << "m_mapAccelTable :\n"; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) + { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + dc << "a CCmdAccelOb at 0x" << (void *)pCmdAccel << " = {\n"; + dc << pCmdAccel; + dc << "}\n"; + } + dc << "\nm_mapAccelTableSaved\n"; + pos = m_mapAccelTableSaved.GetStartPosition(); + while (pos != NULL) + { + m_mapAccelTableSaved.GetNextAssoc(pos, wKey, pCmdAccel); + dc << "a CCmdAccelOb at 0x" << (void *)pCmdAccel << " = {\n"; + dc << pCmdAccel; + dc << "}\n"; + } +} + +#endif + +void CAcceleratorManager::Connect(CWnd*pWnd, bool bAutoSave) +{ + ASSERT(m_pWndConnected == NULL); + m_pWndConnected = pWnd; + m_bAutoSave = bAutoSave; +} + +////////////////////////////////////////////////////////////////////// +// Update the application's ACCELs table +// +bool CAcceleratorManager::UpdateWndTable() +{ + int iLoop = 0; + CTypedPtrArray arrayACCEL; + + CCmdAccelOb*pCmdAccel; + WORD wKey; + LPACCEL pACCEL; + CAccelsOb* pAccelOb; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) + { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccelOb = pCmdAccel->m_Accels.GetNext(pos); + + pACCEL = new ACCEL; + ASSERT(pACCEL != NULL); + pACCEL->fVirt = pAccelOb->m_cVirt; + pACCEL->key = pAccelOb->m_wKey; + pACCEL->cmd = pCmdAccel->m_wIDCommand; + arrayACCEL.Add(pACCEL); + } + } + + int nAccel = arrayACCEL.GetSize(); + LPACCEL lpAccel = (LPACCEL)LocalAlloc(LPTR, nAccel * sizeof(ACCEL)); + if (!lpAccel) + { + for (iLoop = 0; iLoop < nAccel; iLoop++) + delete arrayACCEL.GetAt(iLoop); + arrayACCEL.RemoveAll(); + + return false; + } + + for (iLoop = 0; iLoop < nAccel; iLoop++) + { + pACCEL = arrayACCEL.GetAt(iLoop); + lpAccel[iLoop].fVirt = pACCEL->fVirt; + lpAccel[iLoop].key = pACCEL->key; + lpAccel[iLoop].cmd = pACCEL->cmd; + + delete pACCEL; + } + arrayACCEL.RemoveAll(); + + HACCEL hNewTable = CreateAcceleratorTable(lpAccel, nAccel); + if (!hNewTable) + { + ::LocalFree(lpAccel); + return false; + } + HACCEL hOldTable = theApp.hAccel; + if (!::DestroyAcceleratorTable(hOldTable)) + { + ::LocalFree(lpAccel); + return false; + } + theApp.hAccel = hNewTable; + ::LocalFree(lpAccel); + + UpdateMenu(GetMenu(*AfxGetApp()->m_pMainWnd)); + + return true; +} + +////////////////////////////////////////////////////////////////////// +// Create/Destroy accelerators +// +bool CAcceleratorManager::DeleteAccel(BYTE cVirt, WORD wIDCommand, WORD wKey) +{ + CCmdAccelOb*pCmdAccel = NULL; + if (m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE) + { + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + POSITION PrevPos; + CAccelsOb*pAccel = NULL; + while (pos != NULL) + { + PrevPos = pos; + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel->m_bLocked == true) + return false; + + if (pAccel->m_cVirt == cVirt && pAccel->m_wKey == wKey) + { + pCmdAccel->m_Accels.RemoveAt(PrevPos); + delete pAccel; + return true; + } + } + } + return false; +} + +bool CAcceleratorManager::DeleteEntry(WORD wIDCommand) +{ + CCmdAccelOb*pCmdAccel = NULL; + VERIFY(m_mapAccelTable.Lookup(wIDCommand, pCmdAccel) == TRUE); + + CAccelsOb*pAccel; + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (pAccel->m_bLocked == true) + return false; + } + m_mapAccelString.RemoveKey(pCmdAccel->m_szCommand); + m_mapAccelTable.RemoveKey(wIDCommand); + delete pCmdAccel; + + return true; +} + +bool CAcceleratorManager::DeleteEntry(LPCTSTR szCommand) +{ + ASSERT(szCommand != NULL); + + WORD wIDCommand; + if (m_mapAccelString.Lookup(szCommand, wIDCommand) == TRUE) + { + return DeleteEntry(wIDCommand); + } + return true; +} + +bool CAcceleratorManager::SetAccel(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked) +{ + ASSERT(szCommand != NULL); + + return AddAccel(cVirt, wIDCommand, wKey, szCommand, bLocked); +} + +bool CAcceleratorManager::AddCommandAccel(WORD wIDCommand, LPCTSTR szCommand, bool bLocked) +{ + ASSERT(szCommand != NULL); + + ASSERT(m_pWndConnected != NULL); + HACCEL hOriginalTable = theApp.hAccel; + + int nAccel = ::CopyAcceleratorTable(hOriginalTable, NULL, 0); + LPACCEL lpAccel = (LPACCEL)LocalAlloc(LPTR, (nAccel) * sizeof(ACCEL)); + if (!lpAccel) + return false; + ::CopyAcceleratorTable(hOriginalTable, lpAccel, nAccel); + + bool bRet = false; + for (int i = 0; i < nAccel; i++) + { + if (lpAccel[i].cmd == wIDCommand) + bRet = AddAccel(lpAccel[i].fVirt, wIDCommand, lpAccel[i].key, szCommand, bLocked); + } + ::LocalFree(lpAccel); + return bRet; +} + +bool CAcceleratorManager::CreateEntry(WORD wIDCommand, LPCTSTR szCommand) +{ + ASSERT(szCommand != NULL); + + WORD wIDDummy; + if (m_mapAccelString.Lookup(szCommand, wIDDummy) == TRUE) + return false; + + CCmdAccelOb*pCmdAccel = new CCmdAccelOb(wIDCommand, szCommand); + ASSERT(pCmdAccel != NULL); + m_mapAccelTable.SetAt(wIDCommand, pCmdAccel); + m_mapAccelString.SetAt(szCommand, wIDCommand); + + return false; +} + +////////////////////////////////////////////////////////////////////// +// Get a string from the ACCEL definition +// +bool CAcceleratorManager::GetStringFromACCEL(ACCEL*pACCEL, CString& szAccel) +{ + ASSERT(pACCEL != NULL); + + CAccelsOb accel(pACCEL); + accel.GetString(szAccel); + + if (szAccel.IsEmpty()) + return false; + else + return true; +} + +bool CAcceleratorManager::GetStringFromACCEL(BYTE cVirt, WORD nCode, CString& szAccel) +{ + CAccelsOb accel(cVirt, nCode); + accel.GetString(szAccel); + + if (szAccel.IsEmpty()) + return false; + else + return true; +} + +void CAcceleratorManager::UpdateMenu(HMENU menu) +{ + int count = GetMenuItemCount(menu); + + OSVERSIONINFO info = {0}; + info.dwOSVersionInfoSize = sizeof(info); + GetVersionEx(&info); + + if (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) + { + MENUITEMINFO info = {0}; + info.cbSize = sizeof(info) - sizeof(HBITMAP); + info.fMask = MIIM_ID | MIIM_SUBMENU; + for (int i = 0; i < count; i++) + { + GetMenuItemInfo(menu, i, TRUE, &info); + + if (info.hSubMenu != NULL) + { + UpdateMenu(info.hSubMenu); + } + else + { + if (info.wID != (UINT)-1) + { + char ss[128]; + MENUITEMINFO info2 = {0}; + info2.cbSize = sizeof(info2) - sizeof(HBITMAP); // FIXME: why? + info2.fMask = MIIM_STRING; + info2.dwTypeData = ss; + info2.cch = 128; + GetMenuItemInfo(menu, i, MF_BYPOSITION, &info2); + + CString str(ss); + int index = str.Find('\t'); + if (index != -1) + str = str.Left(index); + + WORD command = info.wID; + + CCmdAccelOb *o; + if (m_mapAccelTable.Lookup(command, o)) + { + if (o->m_Accels.GetCount()) + { + POSITION pos = o->m_Accels.GetHeadPosition(); + CAccelsOb *accel = o->m_Accels.GetNext(pos); + + CString s; + accel->GetString(s); + str += "\t"; + str += s; + } + } + if (str != ss) + ModifyMenu(menu, i, MF_BYPOSITION | MF_STRING, info.wID, str); + } + } + } + } + else + { + MENUITEMINFO info = {0}; + info.cbSize = sizeof(info); + info.fMask = MIIM_ID | MIIM_SUBMENU; + for (int i = 0; i < count; i++) + { + GetMenuItemInfo(menu, i, TRUE, &info); + + if (info.hSubMenu != NULL) + { + UpdateMenu(info.hSubMenu); + } + else + { + if (info.wID != (WORD)-1) + { + wchar_t ss[128]; + wchar_t str[512]; + MENUITEMINFOW info2 = {0}; + info2.cbSize = sizeof(info2); + info2.fMask = MIIM_STRING; + info2.dwTypeData = ss; + info2.cch = 128; + GetMenuItemInfoW(menu, i, MF_BYPOSITION, &info2); + + wcscpy(str, ss); + + wchar_t *p = wcschr(str, '\t'); + if (p) + *p = 0; + + CCmdAccelOb *o; + WORD command = info.wID; + if (m_mapAccelTable.Lookup(command, o)) + { + if (o->m_Accels.GetCount()) + { + POSITION pos = o->m_Accels.GetHeadPosition(); + + CAccelsOb *accel = o->m_Accels.GetNext(pos); + + CString s; + accel->GetString(s); + + wchar_t temp[128]; + temp[0] = '\t'; + temp[1] = 0; + wcscat(str, temp); + p = temp; + for (const char *sp = s; *sp; sp++) + *p++ = *sp; + *p = 0; + wcscat(str, temp); + } + } + if (wcscmp(str, ss)) + ModifyMenuW(menu, i, MF_BYPOSITION | MF_STRING, info.wID, str); + } + } + } + } +} + +////////////////////////////////////////////////////////////////////// +// In/Out to the registry +// +bool CAcceleratorManager::Load() +{ +// ASSERT(szRegKey != NULL); + +// m_hRegKey = hRegKey; +// m_szRegKey = szRegKey; + + DWORD data[2048/sizeof(DWORD)]; + + DWORD len = sizeof(data); + if (regQueryBinaryValue("keyboard", (char *)data, len)) + { + int count = len/sizeof(DWORD); + + CCmdAccelOb*pCmdAccel; + CAccelsOb* pAccel; + DWORD dwIDAccelData, dwAccelData; + BOOL bExistID; + int iIndex = 0; + if (count) + { + WORD wKey; + POSITION pos = m_mapAccelTable.GetStartPosition(); + + while (pos != NULL) + { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + pCmdAccel->DeleteUserAccels(); + } + + while (iIndex < count) + { + dwIDAccelData = data[iIndex++]; + + WORD wIDCommand = LOWORD(dwIDAccelData); + bExistID = m_mapAccelTable.Lookup(wIDCommand, pCmdAccel); + + if (bExistID) + { + pCmdAccel->DeleteUserAccels(); + } + for (int j = 0; j < HIWORD(dwIDAccelData) && iIndex < count; j++) + { + dwAccelData = data[iIndex++]; + if (bExistID) + { + pAccel = new CAccelsOb; + ASSERT(pAccel != NULL); + pAccel->SetData(dwAccelData); + pCmdAccel->Add(pAccel); + } + } + } + } + UpdateWndTable(); + return true; + } + return false; +} + +bool CAcceleratorManager::Write() +{ + CDWordArray AccelsDatasArray; + CDWordArray CmdDatasArray; + + int iCount = 0; + CCmdAccelOb*pCmdAccel; + CAccelsOb* pAccel; + DWORD dwAccelData; + + WORD wKey; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) + { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + CmdDatasArray.RemoveAll(); + + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + // if (!pAccel->m_bLocked) { + dwAccelData = pAccel->GetData(); + CmdDatasArray.Add(dwAccelData); + // } + } + + if (CmdDatasArray.GetSize() > 0) + { + CmdDatasArray.InsertAt(0, MAKELONG(pCmdAccel->m_wIDCommand, CmdDatasArray.GetSize())); + + AccelsDatasArray.Append(CmdDatasArray); + iCount++; + } + } + // AccelsDatasArray.InsertAt(0, MAKELONG(65535, iCount)); + + int count = AccelsDatasArray.GetSize(); + DWORD *data = (DWORD *)malloc(count * sizeof(DWORD)); + ASSERT(data != NULL); + + for (int index = 0; index < count; index++) + data[index] = AccelsDatasArray[index]; + + regSetBinaryValue("keyboard", (char *)data, count*sizeof(DWORD)); + + AccelsDatasArray.RemoveAll(); + CmdDatasArray.RemoveAll(); + + free(data); + + return true; +} + +////////////////////////////////////////////////////////////////////// +// Defaults values management. +// +bool CAcceleratorManager::CreateDefaultTable() +{ + if (m_bDefaultTable) + return false; + + CCmdAccelOb*pCmdAccel; + CCmdAccelOb*pNewCmdAccel; + + CAccelsOb*pAccel; + CAccelsOb*pNewAccel; + + WORD wKey; + POSITION pos = m_mapAccelTable.GetStartPosition(); + while (pos != NULL) + { + m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + pNewCmdAccel = new CCmdAccelOb; + ASSERT(pNewCmdAccel != NULL); + + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccel = pCmdAccel->m_Accels.GetNext(pos); + if (!pAccel->m_bLocked) + { + pNewAccel = new CAccelsOb; + ASSERT(pNewAccel != NULL); + + *pNewAccel = *pAccel; + pNewCmdAccel->m_Accels.AddTail(pNewAccel); + } + } + if (pNewCmdAccel->m_Accels.GetCount() != 0) + { + pNewCmdAccel->m_wIDCommand = pCmdAccel->m_wIDCommand; + pNewCmdAccel->m_szCommand = pCmdAccel->m_szCommand; + + m_mapAccelTableSaved.SetAt(wKey, pNewCmdAccel); + } + else + delete pNewCmdAccel; + } + + m_bDefaultTable = true; + return true; +} + +#include "mainwnd.h" +bool CAcceleratorManager::Default() +{ + /// this is NYI for some reason, so the "Reset All" button doesn't work + +#if 0 + // still doesn't work: + Reset(); + regDeleteValue("keyboard"); + regDeleteValue("keyboardCount"); + Connect((MainWnd *)theApp.m_pMainWnd); + extern void winAccelAddCommands(CAcceleratorManager& mgr); + Load(); + CreateDefaultTable(); + winAccelAddCommands(*this); + UpdateWndTable(); + Write(); + UpdateMenu(theApp.menu); + m_pWndConnected = NULL; +#endif + + return true; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/AcceleratorManager.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/AcceleratorManager.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,135 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : AcceleratorManager.h +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Author : T.Maurel +// Date : 17.08.98 +// +// Remarks : interface for the CAcceleratorManager class. +// +//////////////////////////////////////////////////////////////////////////////// +#if !defined(AFX_ACCELERATORMANAGER_H__A6D76F4B_26C6_11D2_BE72_006097AC8D00__INCLUDED_) +#define AFX_ACCELERATORMANAGER_H__A6D76F4B_26C6_11D2_BE72_006097AC8D00__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 + +#include "afxtempl.h" + +class CCmdAccelOb; + +// Helper map +#ifndef CMapStringToWord +typedef CMap< CString, LPCSTR, WORD, WORD & > CMapStringToWord; +#endif + +#ifndef CMapWordToCCmdAccelOb +typedef CMap< WORD, WORD &, CCmdAccelOb *, CCmdAccelOb * & > CMapWordToCCmdAccelOb; +#endif + +//////////////////////////////////////////////////////////////////////////////// +// All Registry stuff removed + +class CAcceleratorManager : public CObject +{ + friend class AccelEditor; +public: + CAcceleratorManager(); + virtual ~CAcceleratorManager(); + + // Operations +public: + void UpdateMenu(HMENU menu); + // Connection to the main application wnd + void Connect(CWnd *pWnd, bool bAutoSave = true); + // In/Out + bool Load(); + bool Write(); + // Get the initials accels, not the user's + bool Default(); + // Save a copy in the 2 maps called xxxSaved, which are used in case + // of Default(), to reload the defaults accels. + bool CreateDefaultTable(); + bool IsDefaultTableAvailable() {return m_bDefaultTable;} + bool IsMapStringCommandsEmpty() + { + if (m_mapAccelString.IsEmpty()) + return true; + else + return false; + } + + bool IsAutoSave() {return m_bAutoSave;} + void SetAutoSave(bool bAutoSave) {m_bAutoSave = bAutoSave;} + + // Helper fct, used for new menus strings + bool GetStringFromACCEL(ACCEL*pACCEL, CString& szAccel); + bool GetStringFromACCEL(BYTE cVirt, WORD nCode, CString& szAccel); + + // Update the ACCELS table in the application, from the specified + // datas in the manager. + bool UpdateWndTable(); + + // Modification helper fcts + bool SetAccel(BYTE cVirt, WORD wIDCommand, WORD wNewCaract, + LPCTSTR szCommand, bool bLocked = false); + bool AddCommandAccel(WORD wIDCommand, LPCTSTR szCommand, bool bLocked = true); + bool CreateEntry(WORD wIDCommand, LPCTSTR szCommand); + + bool DeleteEntry(LPCTSTR szCommand); + bool DeleteEntry(WORD wIDCommand); + bool DeleteAccel(BYTE cVirt, WORD wIDCommand, WORD wNewCaract); + + // Affectation operator + CAcceleratorManager & operator=(const CAcceleratorManager& accelmgr); +public: +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif +protected: + // Erase all the datas + void Reset(); + // Internal affect fct. + bool AddAccel(BYTE cVirt, WORD wIDCommand, WORD wKey, + LPCTSTR szCommand, bool bLocked); + + // Attributes +protected: + CWnd *m_pWndConnected; + + // User datas +/*TEMP HACK*/ +public: + CMapStringToWord m_mapAccelString; +/*TEMP HACK*/ +public: + CMapWordToCCmdAccelOb m_mapAccelTable; +protected: + // Default datas + CMapWordToCCmdAccelOb m_mapAccelTableSaved; + bool m_bDefaultTable; + + // if true, there is an auto-save, when the destructor is called + bool m_bAutoSave; +}; + +#endif // !defined(AFX_ACCELERATORMANAGER_H__A6D76F4B_26C6_11D2_BE72_006097AC8D00__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Associate.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Associate.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,105 @@ +// Associate.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "Associate.h" +#include "Reg.h" + +///////////////////////////////////////////////////////////////////////////// +// Associate dialog + +Associate::Associate(CWnd*pParent /*=NULL*/) + : CDialog(Associate::IDD, pParent) +{ + //{{AFX_DATA_INIT(Associate) + m_agb = FALSE; + m_bin = FALSE; + m_cgb = FALSE; + m_gb = FALSE; + m_gba = FALSE; + m_gbc = FALSE; + m_sgb = FALSE; + //}}AFX_DATA_INIT +} + +void Associate::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Associate) + DDX_Check(pDX, IDC_AGB, m_agb); + DDX_Check(pDX, IDC_BIN, m_bin); + DDX_Check(pDX, IDC_CGB, m_cgb); + DDX_Check(pDX, IDC_GB, m_gb); + DDX_Check(pDX, IDC_GBA, m_gba); + DDX_Check(pDX, IDC_GBC, m_gbc); + DDX_Check(pDX, IDC_SGB, m_sgb); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(Associate, CDialog) +//{{AFX_MSG_MAP(Associate) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// Associate message handlers + +BOOL Associate::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Associate::OnCancel() +{ + EndDialog(FALSE); +} + +void Associate::OnOk() +{ + UpdateData(); + + int mask = 0; + if (m_gb) + mask |= 1; + if (m_sgb) + mask |= 2; + if (m_cgb) + mask |= 4; + if (m_gbc) + mask |= 8; + if (m_gba) + mask |= 16; + if (m_agb) + mask |= 32; + if (m_bin) + mask |= 64; + if (mask) + { + char applicationPath[2048]; + CString commandPath; + LPCTSTR types[] = { ".gb", ".sgb", ".cgb", ".gbc", ".gba", ".agb", ".bin" }; + GetModuleFileName(NULL, applicationPath, 2048); + commandPath.Format("\"%s\" \"%%1\"", applicationPath); + regAssociateType("VisualBoyAdvance.Binary", + "Binary", + commandPath); + + for (int i = 0; i < 7; i++) + { + if (mask & (1< 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Associate.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// Associate dialog + +class Associate : public CDialog +{ + // Construction +public: + Associate(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Associate) + enum { IDD = IDD_ASSOCIATIONS }; + BOOL m_agb; + BOOL m_bin; + BOOL m_cgb; + BOOL m_gb; + BOOL m_gba; + BOOL m_gbc; + BOOL m_sgb; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Associate) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(Associate) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ASSOCIATE_H__3326525B_B405_40A7_82C4_B2594669A930__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/BitmapControl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/BitmapControl.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,262 @@ +// BitmapControl.cpp : implementation file +// + +#include "stdafx.h" +#include "BitmapControl.h" + +bool BitmapControl::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// BitmapControl + +IMPLEMENT_DYNCREATE(BitmapControl, CScrollView) + + BitmapControl::BitmapControl() +{ + w = 0; + h = 0; + data = NULL; + bmpInfo = NULL; + stretch = false; + registerClass(); + CSize sizeTotal; + sizeTotal.cx = sizeTotal.cy = 0; + SetScrollSizes(MM_TEXT, sizeTotal); +} + +BitmapControl::~BitmapControl() +{ +} + + +BEGIN_MESSAGE_MAP(BitmapControl, CScrollView) + //{{AFX_MSG_MAP(BitmapControl) + ON_WM_ERASEBKGND() + ON_WM_SIZE() + ON_WM_LBUTTONDOWN() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// BitmapControl drawing + +void BitmapControl::OnInitialUpdate() +{ + CScrollView::OnInitialUpdate(); + + CSize sizeTotal; + // TODO: calculate the total size of this view + sizeTotal.cx = sizeTotal.cy = 100; + SetScrollSizes(MM_TEXT, sizeTotal); +} + +void BitmapControl::OnDraw(CDC* dc) +{ + RECT r; + GetClientRect(&r); + int w1 = r.right - r.left; + int h1 = r.bottom - r.top; + CDC memDC; + memDC.CreateCompatibleDC(dc); + if(!stretch) { + if(w > w1) + w1 = w; + if(h > h1) + h1 = h; + } + CBitmap bitmap, *pOldBitmap; + bitmap.CreateCompatibleBitmap(dc, w1, h1); + pOldBitmap = memDC.SelectObject(&bitmap); + if(stretch) { + bmpInfo->bmiHeader.biWidth = w; + bmpInfo->bmiHeader.biHeight = -h; + + StretchDIBits(memDC.GetSafeHdc(), + 0, + 0, + w1, + h1, + 0, + 0, + w, + h, + data, + bmpInfo, + DIB_RGB_COLORS, + SRCCOPY); + } else { + FillOutsideRect(&memDC, CBrush::FromHandle(GetSysColorBrush(COLOR_BTNFACE))); + + bmpInfo->bmiHeader.biWidth = w; + bmpInfo->bmiHeader.biHeight = -h; + SetDIBitsToDevice(memDC.GetSafeHdc(), + 0, + 0, + w, + h, + 0, + 0, + 0, + h, + data, + bmpInfo, + DIB_RGB_COLORS); + } + + dc->BitBlt(0,0,w1,h1, + &memDC,0,0,SRCCOPY); + memDC.SelectObject(pOldBitmap); + + bitmap.DeleteObject(); + memDC.DeleteDC(); +} + +///////////////////////////////////////////////////////////////////////////// +// BitmapControl diagnostics + +#ifdef _DEBUG +void BitmapControl::AssertValid() const +{ + CScrollView::AssertValid(); +} + +void BitmapControl::Dump(CDumpContext& dc) const +{ + CScrollView::Dump(dc); +} +#endif //_DEBUG + +///////////////////////////////////////////////////////////////////////////// +// BitmapControl message handlers + +BOOL BitmapControl::OnEraseBkgnd(CDC* pDC) +{ + return TRUE; +} + +void BitmapControl::OnSize(UINT nType, int cx, int cy) +{ + if(!stretch) + CScrollView::OnSize(nType, cx, cy); +} + +void BitmapControl::OnLButtonDown(UINT nFlags, CPoint pt) +{ + if(!data) + return; + int x = pt.x; + int y = pt.y; + + WPARAM point; + + if(stretch) { + RECT rect; + GetClientRect(&rect); + + int height = rect.bottom - rect.top; + int width = rect.right - rect.left; + + int xx = (x * w) / width; + int yy = (y * h) / height; + + point = xx | (yy<<16); + + int xxx = xx / 8; + int yyy = yy / 8; + + for(int i = 0; i < 8; i++) { + memcpy(&colors[i*3*8], &data[xxx * 8 * 3 + + w * yyy * 8 * 3 + + i * w * 3], 8 * 3); + } + } else { + POINT p; + p.x = GetScrollPos(SB_HORZ); + p.y = GetScrollPos(SB_VERT); + + p.x += x; + p.y += y; + + if(p.x >= w || + p.y >= h) + return; + + point = p.x | (p.y<<16); + + int xxx = p.x / 8; + int yyy = p.y / 8; + + for(int i = 0; i < 8; i++) { + memcpy(&colors[i*3*8], &data[xxx * 8 * 3 + + w * yyy * 8 * 3 + + i * w * 3], 8 * 3); + } + } + + GetParent()->SendMessage(WM_MAPINFO, + point, + (LPARAM)colors); +} + +void BitmapControl::setBmpInfo(BITMAPINFO *info) +{ + bmpInfo = info; +} + +void BitmapControl::setData(u8 *d) +{ + data = d; +} + +void BitmapControl::setSize(int w1, int h1) +{ + if(w != w1 || h != h1) { + w = w1; + h = h1; + SIZE s; + s.cx = w; + s.cy = h; + SetScrollSizes(MM_TEXT, s); + } +} + +void BitmapControl::refresh() +{ + Invalidate(); +} + + +void BitmapControl::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hIcon = NULL; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaBitmapControl"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} + +void BitmapControl::setStretch(bool b) +{ + if(b != stretch) { + stretch = b; + Invalidate(); + } +} + +bool BitmapControl::getStretch() +{ + return stretch; +} + +void BitmapControl::PostNcDestroy() +{ +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/BitmapControl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/BitmapControl.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,75 @@ +#if !defined(AFX_BITMAPCONTROL_H__2434AADB_B6A5_4E43_AA16_7B65B6F7FA26__INCLUDED_) +#define AFX_BITMAPCONTROL_H__2434AADB_B6A5_4E43_AA16_7B65B6F7FA26__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// BitmapControl.h : header file +// +#ifndef WM_MAPINFO +#define WM_MAPINFO WM_APP+101 +#endif + +///////////////////////////////////////////////////////////////////////////// +// BitmapControl view + +class BitmapControl : public CScrollView +{ +public: + BitmapControl(); // protected constructor used by dynamic creation +protected: + DECLARE_DYNCREATE(BitmapControl) + + // Attributes +public: + // Operations +public: + void setStretch(bool b); + void refresh(); + void setSize(int w1, int h1); + void setData(u8 *d); + void setBmpInfo(BITMAPINFO *info); + static bool isRegistered; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(BitmapControl) +protected: + virtual void OnDraw(CDC*pDC); // overridden to draw this view + virtual void OnInitialUpdate(); // first time after construct + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +public: + bool getStretch(); + virtual ~BitmapControl(); +protected: +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif + + // Generated message map functions + //{{AFX_MSG(BitmapControl) + afx_msg BOOL OnEraseBkgnd(CDC*pDC); + afx_msg void OnSize(UINT nType, int cx, int cy); + afx_msg void OnLButtonDown(UINT nFlags, CPoint point); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +private: + void registerClass(); + bool stretch; + u8 colors[3*64]; + BITMAPINFO *bmpInfo; + u8 * data; + int h; + int w; +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_BITMAPCONTROL_H__2434AADB_B6A5_4E43_AA16_7B65B6F7FA26__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/BugReport.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/BugReport.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,235 @@ +// BugReport.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "BugReport.h" +#include "VBA.h" + +#include "../version.h" +#include "../gba/agbprint.h" +#include "../gba/Flash.h" +#include "../gba/GBACheats.h" +#include "../gba/GBAGlobals.h" +#include "../gb/gbCheats.h" +#include "../gb/gbGlobals.h" +#include "../gba/RTC.h" +#include "../gba/GBASound.h" +#include "../common/vbalua.h" + +///////////////////////////////////////////////////////////////////////////// +// BugReport dialog + +BugReport::BugReport(CWnd*pParent /*=NULL*/) + : CDialog(BugReport::IDD, pParent) +{ + //{{AFX_DATA_INIT(BugReport) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void BugReport::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(BugReport) + DDX_Control(pDX, IDC_BUG_REPORT, m_report); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(BugReport, CDialog) +//{{AFX_MSG_MAP(BugReport) +ON_BN_CLICKED(IDC_COPY, OnCopy) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// BugReport message handlers + +void BugReport::OnCopy() +{ + OpenClipboard(); + + EmptyClipboard(); + CString report; + m_report.GetWindowText(report); + + HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, + (report.GetLength() + 1) * sizeof(CHAR)); + if (hglbCopy == NULL) + { + CloseClipboard(); + return; + } + + // Lock the handle and copy the text to the buffer. + + LPSTR lptstrCopy = (LPSTR)GlobalLock(hglbCopy); + memcpy(lptstrCopy, (const char *)report, + report.GetLength() * sizeof(CHAR)); + lptstrCopy[report.GetLength()] = (TCHAR) 0; // null character + GlobalUnlock(hglbCopy); + + // Place the handle on the clipboard. + + SetClipboardData(CF_TEXT, hglbCopy); + CloseClipboard(); + + systemMessage(IDS_BUG_REPORT, "Bug report has been copied to the Clipboard"); +} + +void BugReport::OnOk() +{ + EndDialog(TRUE); +} + +BOOL BugReport::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CenterWindow(); + + CString report = createReport(); + + m_report.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT))); + + m_report.SetWindowText(report); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +static void AppendFormat(CString& report, const char *format, ...) +{ + CString buffer; + va_list valist; + + va_start(valist, format); + buffer.FormatV(format, valist); + va_end(valist); + report += buffer; +} + +CString BugReport::createReport() +{ + theApp.winCheckFullscreen(); + + CString report = ""; + AppendFormat(report, "Emu version : %s\r\n", VBA_VERSION_STRING); +AppendFormat(report, "Emu type : %s\r\n", VBA_BUILDTYPE_STRING); + + if (systemIsEmulating()) + { + AppendFormat(report, "Game : %s\r\n", theApp.gameFilename); + + char buffer[20]; + if (systemCartridgeType == 0) + { + u32 check = 0; + for (int i = 0; i < 0x4000; i += 4) + { + check += *((u32 *)&bios[i]); + } + AppendFormat(report, "BIOS checksum: %08X\r\n", check); + + strncpy(buffer, (const char *)&rom[0xa0], 12); + buffer[12] = 0; + AppendFormat(report, "Internal name: %s\r\n", buffer); + + strncpy(buffer, (const char *)&rom[0xac], 4); + buffer[4] = 0; + AppendFormat(report, "Game code : %s\r\n", buffer); + + CString res = ""; + u32 * p = (u32 *)rom; + u32 * end = (u32 *)((char *)rom+theApp.romSize); + while (p < end) + { + u32 d = READ32LE(p); + + if (d == 0x52504545) + { + if (memcmp(p, "EEPROM_", 7) == 0) + { + res += (const char *)p; + res += ' '; + } + } + else if (d == 0x4D415253) + { + if (memcmp(p, "SRAM_", 5) == 0) + { + res += (const char *)p; + res += ' '; + } + } + else if (d == 0x53414C46) + { + if (memcmp(p, "FLASH1M_", 8) == 0) + { + res += (const char *)p; + res += ' '; + } + } + else if (memcmp(p, "FLASH", 5) == 0) + { + res += (const char *)p; + res += ' '; + } + else if (d == 0x52494953) + { + if (memcmp(p, "SIIRTC_V", 8) == 0) + { + res += (const char *)p; + res += ' '; + } + } + p++; + } + if (res.GetLength() > 0) + AppendFormat(report, "Cart Save : %s\r\n", res); + } + else if (systemCartridgeType == 1) + { + strncpy(buffer, (const char *)&gbRom[0x134], 15); + buffer[15] = 0; + AppendFormat(report, "Game title : %s\r\n", buffer); + } + } + + AppendFormat(report, "Using BIOS : %d\r\n", useBios); + AppendFormat(report, "Skip BIOS : %d\r\n", theApp.skipBiosFile); + AppendFormat(report, "Disable SFX : %d\r\n", cpuDisableSfx); +/// AppendFormat(report, "Skip intro : %d\r\n", theApp.removeIntros); + AppendFormat(report, "Throttle : %d\r\n", theApp.throttle); + AppendFormat(report, "Rewind : %d\r\n", theApp.rewindTimer); + AppendFormat(report, "Lua : %d\r\n", VBALuaRunning()); +/// AppendFormat(report, "Auto frame : %d\r\n", theApp.autoFrameSkip); + AppendFormat(report, "Video option : %d\r\n", theApp.videoOption); + AppendFormat(report, "Render type : %d\r\n", theApp.renderMethod); + AppendFormat(report, "Color depth : %d\r\n", systemColorDepth); + AppendFormat(report, "Red shift : %08x\r\n", systemRedShift); + AppendFormat(report, "Green shift : %08x\r\n", systemGreenShift); + AppendFormat(report, "Blue shift : %08x\r\n", systemBlueShift); + AppendFormat(report, "Layer setting: %04X\r\n", layerSettings); + AppendFormat(report, "Save type : %d (%d)\r\n", + theApp.winSaveType, cpuSaveType); + AppendFormat(report, "Flash size : %08X (%08x)\r\n", + theApp.winFlashSize, flashSize); + AppendFormat(report, "RTC : %d (%d)\r\n", theApp.winRtcEnable, + rtcIsEnabled()); + AppendFormat(report, "AGBPrint : %d\r\n", agbPrintIsEnabled()); + AppendFormat(report, "Turbo Mode : %d\r\n", theApp.speedupToggle); + AppendFormat(report, "Synchronize : %d\r\n", synchronize); + AppendFormat(report, "Sound OFF : %d\r\n", soundOffFlag); + AppendFormat(report, "Channels : %04x\r\n", soundGetEnabledChannels() & 0x30f); + AppendFormat(report, "Old Sync : %d\r\n", theApp.useOldSync); + AppendFormat(report, "Priority : %d\r\n", theApp.threadPriority); + AppendFormat(report, "Filters : %d (%d)\r\n", theApp.filterType, theApp.ifbType); + AppendFormat(report, "Cheats : %d\r\n", cheatsNumber); + AppendFormat(report, "GB Cheats : %d\r\n", gbCheatNumber); + AppendFormat(report, "GB Emu Type : %d\r\n", gbEmulatorType); + + return report; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/BugReport.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/BugReport.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,48 @@ +#if !defined(AFX_BUGREPORT_H__DE7BC381_E45D_4200_910C_E5378E6364C9__INCLUDED_) +#define AFX_BUGREPORT_H__DE7BC381_E45D_4200_910C_E5378E6364C9__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// BugReport.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// BugReport dialog + +class BugReport : public CDialog +{ + // Construction +public: + BugReport(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(BugReport) + enum { IDD = IDD_BUG_REPORT }; + CEdit m_report; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(BugReport) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + CString createReport(); + + // Generated message map functions + //{{AFX_MSG(BugReport) + afx_msg void OnCopy(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_BUGREPORT_H__DE7BC381_E45D_4200_910C_E5378E6364C9__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/CmdAccelOb.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/CmdAccelOb.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,539 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : CmdAccelOb.cpp +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Author : T.Maurel +// Date : 17.08.98 +// +// Remarks : +// +//////////////////////////////////////////////////////////////////////////////// +// modified by the VBA-rr team + +#include "stdafx.h" +#include "CmdAccelOb.h" + +//////////////////////////////////////////////////////////////////////// +// +// +MAPVIRTKEYS mapVirtKeys[] = { + { VK_LBUTTON, "LBUTTON" }, + { VK_RBUTTON, "RBUTTON" }, + { VK_CANCEL, "Cancel" }, + { VK_MBUTTON, "MBUTTON" }, + { VK_BACK, "Backspace" }, + { VK_TAB, "Tab" }, + { VK_CLEAR, "Clear" }, + { VK_RETURN, "Enter" }, + { VK_SHIFT, "Shift" }, + { VK_LSHIFT, "LShift" }, + { VK_RSHIFT, "RShift" }, + { VK_CONTROL, "Control" }, + { VK_LCONTROL, "LControl" }, + { VK_RCONTROL, "RControl" }, + { VK_MENU, "Alt" }, + { VK_LMENU, "LAlt" }, + { VK_RMENU, "RAlt" }, + { VK_PAUSE, "Pause" }, + { VK_CAPITAL, "Caps Lock" }, + { VK_ESCAPE, "Escape" }, + { VK_SPACE, "Space" }, + { VK_PRIOR, "Prior" }, + { VK_NEXT, "Next" }, + { VK_END, "End" }, + { VK_HOME, "Home" }, + { VK_LEFT, "Left" }, + { VK_UP, "Up" }, + { VK_RIGHT, "Right" }, + { VK_DOWN, "Down" }, + { VK_SELECT, "Select" }, + { VK_PRINT, "Print" }, + { VK_EXECUTE, "Execute" }, + { VK_SNAPSHOT, "Snapshot" }, + { VK_INSERT, "Insert" }, + { VK_DELETE, "Delete" }, + { VK_HELP, "Help" }, + { WORD('0'), "0" }, + { WORD('1'), "1" }, + { WORD('2'), "2" }, + { WORD('3'), "3" }, + { WORD('4'), "4" }, + { WORD('5'), "5" }, + { WORD('6'), "6" }, + { WORD('7'), "7" }, + { WORD('8'), "8" }, + { WORD('9'), "9" }, + { WORD('A'), "A" }, + { WORD('B'), "B" }, + { WORD('C'), "C" }, + { WORD('D'), "D" }, + { WORD('E'), "E" }, + { WORD('F'), "F" }, + { WORD('G'), "G" }, + { WORD('H'), "H" }, + { WORD('I'), "I" }, + { WORD('J'), "J" }, + { WORD('K'), "K" }, + { WORD('L'), "L" }, + { WORD('M'), "M" }, + { WORD('N'), "N" }, + { WORD('O'), "O" }, + { WORD('P'), "P" }, + { WORD('Q'), "Q" }, + { WORD('R'), "R" }, + { WORD('S'), "S" }, + { WORD('T'), "T" }, + { WORD('U'), "U" }, + { WORD('V'), "V" }, + { WORD('W'), "W" }, + { WORD('X'), "X" }, + { WORD('Y'), "Y" }, + { WORD('Z'), "Z" }, + { VK_LWIN, "LWIN" }, + { VK_RWIN, "RWIN" }, + { VK_APPS, "APPS" }, + { VK_NUMPAD0, "NumPad0" }, + { VK_NUMPAD1, "NumPad1" }, + { VK_NUMPAD2, "NumPad2" }, + { VK_NUMPAD3, "NumPad3" }, + { VK_NUMPAD4, "NumPad4" }, + { VK_NUMPAD5, "NumPad5" }, + { VK_NUMPAD6, "NumPad6" }, + { VK_NUMPAD7, "NumPad7" }, + { VK_NUMPAD8, "NumPad8" }, + { VK_NUMPAD9, "NumPad9" }, + { VK_MULTIPLY, "NumpadMultiply" }, + { VK_ADD, "NumpadAdd" }, + { VK_SEPARATOR, "Separator" }, + { VK_SUBTRACT, "NumpadSubtract" }, + { VK_DECIMAL, "NumpadDecimal" }, + { VK_DIVIDE, "NumpadDivide" }, + { VK_F1, "F1" }, + { VK_F2, "F2" }, + { VK_F3, "F3" }, + { VK_F4, "F4" }, + { VK_F5, "F5" }, + { VK_F6, "F6" }, + { VK_F7, "F7" }, + { VK_F8, "F8" }, + { VK_F9, "F9" }, + { VK_F10, "F10" }, + { VK_F11, "F11" }, + { VK_F12, "F12" }, + { VK_F13, "F13" }, + { VK_F14, "F14" }, + { VK_F15, "F15" }, + { VK_F16, "F16" }, + { VK_F17, "F17" }, + { VK_F18, "F18" }, + { VK_F19, "F19" }, + { VK_F20, "F20" }, + { VK_F21, "F21" }, + { VK_F22, "F22" }, + { VK_F23, "F23" }, + { VK_F24, "F24" }, + { VK_NUMLOCK, "Num Lock" }, + { VK_SCROLL, "Scroll Lock" }, + { VK_ATTN, "Attention" }, + { VK_CRSEL, "CRSEL" }, + { VK_EXSEL, "EXSEL" }, + { VK_EREOF, "EREOF" }, + { VK_PLAY, "Play" }, + { VK_ZOOM, "Zoom" }, + { VK_NONAME, "No Name" }, + { VK_PA1, "PA1" }, + { VK_OEM_CLEAR, "Clear2" }, + { VK_OEM_1, "Semicolon;" }, + { VK_OEM_2, "Slash/" }, + { VK_OEM_3, "Tilde~" }, + { VK_OEM_4, "LBracket[" }, + { VK_OEM_5, "Backslash\\" }, + { VK_OEM_6, "RBracket]" }, + { VK_OEM_7, "Apostrophe'" }, + { VK_OEM_8, "OEM8" }, + { VK_OEM_PLUS, "Plus+" }, + { VK_OEM_MINUS, "Minus-" }, + { VK_OEM_COMMA, "Comma," }, + { VK_OEM_PERIOD, "Period." }, + { VK_OEM_AX, "Apostrophe`" }, + { VK_OEM_102, "<> or \\|" }, + { VK_ICO_HELP, "ICO Help" }, + { VK_ICO_00, "ICO 00" }, + { VK_OEM_FJ_JISHO, "JISHO" }, + { VK_OEM_FJ_MASSHOU, "MASSHOU" }, + { VK_OEM_FJ_TOUROKU, "TOUROKU" }, + { VK_OEM_FJ_LOYA, "LOYA" }, + { VK_OEM_FJ_ROYA, "ROYA" }, + { VK_OEM_NEC_EQUAL, "Numpad Equals" }, +}; + +//////////////////////////////////////////////////////////////////////// +// +// +MAPVIRTKEYS mapVirtSysKeys[] = { + { FCONTROL, "Ctrl" }, + { FALT, "Alt" }, + { FSHIFT, "Shift" }, +}; + +//////////////////////////////////////////////////////////////////////// +// helper fct for external access +//////////////////////////////////////////////////////////////////////// +// +// +TCHAR *mapVirtKeysStringFromWORD(WORD wKey) +{ + for (int index = 0; index < sizeof(mapVirtKeys) / sizeof(mapVirtKeys[0]); index++) + { + if (mapVirtKeys[index].wKey == wKey) + return mapVirtKeys[index].szKey; + } + return NULL; +} + +//////////////////////////////////////////////////////////////////////// +// +#define DEFAULT_ACCEL 0x01 +#define USER_ACCEL 0x02 + +//////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb::CAccelsOb() +{ + m_cVirt = 0; + m_wKey = 0; + m_bLocked = false; +} + +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb::CAccelsOb(CAccelsOb *pFrom) +{ + ASSERT(pFrom != NULL); + + m_cVirt = pFrom->m_cVirt; + m_wKey = pFrom->m_wKey; + m_bLocked = pFrom->m_bLocked; +} + +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb::CAccelsOb(BYTE cVirt, WORD wKey, bool bLocked) +{ + m_cVirt = cVirt; + m_wKey = wKey; + m_bLocked = bLocked; +} + +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb::CAccelsOb(LPACCEL pACCEL) +{ + ASSERT(pACCEL != NULL); + + m_cVirt = pACCEL->fVirt; + m_wKey = pACCEL->key; + m_bLocked = false; +} + +//////////////////////////////////////////////////////////////////////// +// +// +CAccelsOb & CAccelsOb::operator=(const CAccelsOb &from) +{ + m_cVirt = from.m_cVirt; + m_wKey = from.m_wKey; + m_bLocked = from.m_bLocked; + + return *this; +} + +//////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////// +// +// +void CAccelsOb::GetString(CString &szBuffer) +{ + szBuffer = ""; + + // modifiers part + for (int i = 0; i < sizetable(mapVirtSysKeys); i++) + { + if (m_cVirt & mapVirtSysKeys[i].wKey) + { + szBuffer += mapVirtSysKeys[i].szKey; + if (m_wKey) + szBuffer += "+"; + } + } + + // in case of the object is not assigned, we avoid error messages + if (m_wKey == 0) + return; + + // and virtual key part + for (int i = 0; i < sizetable(mapVirtKeys); i++) + { + if (m_wKey == mapVirtKeys[i].wKey) + { + szBuffer += mapVirtKeys[i].szKey; + return; + } + } + + AfxMessageBox("Internal error : (CAccelsOb::GetString) m_wKey invalid"); +} + +//////////////////////////////////////////////////////////////////////// +// +// +bool CAccelsOb::IsEqual(WORD wKey, bool bCtrl, bool bAlt, bool bShift) +{ + bool m_bCtrl = (m_cVirt & FCONTROL) ? true : false; + bool m_bAlt = (m_cVirt & FALT) ? true : false; + bool m_bShift = (m_cVirt & FSHIFT) ? true : false; + + bool bRet = (bCtrl == m_bCtrl) && (bAlt == m_bAlt) && (bShift == m_bShift) && (m_wKey == wKey); + + return bRet; +} + +//////////////////////////////////////////////////////////////////////// +// +// +DWORD CAccelsOb::GetData() +{ + BYTE cLocalCodes = 0; + if (m_bLocked) + cLocalCodes = DEFAULT_ACCEL; + else + cLocalCodes = USER_ACCEL; + + WORD bCodes = MAKEWORD(m_cVirt, cLocalCodes); + return MAKELONG(m_wKey, bCodes); +} + +//////////////////////////////////////////////////////////////////////// +// +// +bool CAccelsOb::SetData(DWORD dwDatas) +{ + m_wKey = LOWORD(dwDatas); + + WORD bCodes = HIWORD(dwDatas); + m_cVirt = LOBYTE(bCodes); + + BYTE cLocalCodes = HIBYTE(bCodes); + m_bLocked = (cLocalCodes == DEFAULT_ACCEL); + + return true; +} + +//////////////////////////////////////////////////////////////////////// +// +#ifdef _DEBUG +//////////////////////////////////////////////////////////////////////// +// +// +void CAccelsOb::AssertValid() const +{ + CObject::AssertValid(); +} + +//////////////////////////////////////////////////////////////////////// +// +// +void CAccelsOb::Dump(CDumpContext &dc) const +{ + dc << "\t\t"; + CObject::Dump(dc); + dc << "\t\tlocked=" << m_bLocked << ", cVirt=" << m_cVirt << ", wKey=" << m_wKey << "\n\n"; +} + +#endif + +//////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb::CCmdAccelOb() +{} + +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb::CCmdAccelOb(WORD wIDCommand, LPCTSTR szCommand) +{ + ASSERT(szCommand != NULL); + + m_wIDCommand = wIDCommand; + m_szCommand = szCommand; +} + +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb::CCmdAccelOb(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked) +{ + ASSERT(szCommand != NULL); + + m_wIDCommand = wIDCommand; + m_szCommand = szCommand; + + CAccelsOb *pAccel = new CAccelsOb(cVirt, wKey, bLocked); + ASSERT(pAccel != NULL); + m_Accels.AddTail(pAccel); +} + +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb::~CCmdAccelOb() +{ + POSITION pos = m_Accels.GetHeadPosition(); + while (pos != NULL) + delete m_Accels.GetNext(pos); + m_Accels.RemoveAll(); +} + +//////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::Add(BYTE cVirt, WORD wKey, bool bLocked) +{ + CAccelsOb *pAccel = new CAccelsOb(cVirt, wKey, bLocked); + ASSERT(pAccel != NULL); + m_Accels.AddTail(pAccel); +} + +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::Add(CAccelsOb *pAccel) +{ + ASSERT(pAccel != NULL); + m_Accels.AddTail(pAccel); +} + +//////////////////////////////////////////////////////////////////////// +// +// +CCmdAccelOb & CCmdAccelOb::operator=(const CCmdAccelOb &from) +{ + Reset(); + + m_wIDCommand = from.m_wIDCommand; + m_szCommand = from.m_szCommand; + + CAccelsOb *pAccel; + POSITION pos = from.m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccel = new CAccelsOb(from.m_Accels.GetNext(pos)); + ASSERT(pAccel != NULL); + m_Accels.AddTail(pAccel); + } + return *this; +} + +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::DeleteUserAccels() +{ + CAccelsOb *pAccel; + POSITION prevPos; + POSITION pos = m_Accels.GetHeadPosition(); + while (pos != NULL) + { + prevPos = pos; + pAccel = m_Accels.GetNext(pos); + if (!pAccel->m_bLocked) + { + delete pAccel; + m_Accels.RemoveAt(prevPos); + } + } +} + +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::Reset() +{ + m_wIDCommand = 0; + m_szCommand = "Empty command"; + + CAccelsOb *pAccel; + POSITION pos = m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccel = m_Accels.GetNext(pos); + delete pAccel; + } +} + +//////////////////////////////////////////////////////////////////////// +// +#ifdef _DEBUG +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::AssertValid() const +{ + // call base class function first + CObject::AssertValid(); +} + +//////////////////////////////////////////////////////////////////////// +// +// +void CCmdAccelOb::Dump(CDumpContext &dc) const +{ + // call base class function first + dc << "\t"; + CObject::Dump(dc); + + // now do the stuff for our specific class + dc << "\tIDCommand = " << m_wIDCommand; + dc << "\n\tszCommand = " << m_szCommand; + dc << "\n\tAccelerators = {\n"; + + CAccelsOb *pAccel; + POSITION pos = m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccel = m_Accels.GetNext(pos); + dc << pAccel; + } + dc << "\t}\n"; +} + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/CmdAccelOb.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/CmdAccelOb.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,110 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : CmdAccelOb.h +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Author : T.Maurel +// Date : 17.08.98 +// +// Remarks : +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef __CMDACCEL_OB_INCLUDE +#define __CMDACCEL_OB_INCLUDE + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include // MFC Templates extension + +//////////////////////////////////////////////////////////////////////// +// +// +typedef struct tagMAPVIRTKEYS +{ + WORD wKey; + TCHAR szKey[15]; +} MAPVIRTKEYS, *PMAPVIRTKEYS; + +//////////////////////////////////////////////////////////////////////// +// +// +#define sizetable(table) (sizeof(table)/sizeof(table[0])) + +//////////////////////////////////////////////////////////////////////// +// +// +class CAccelsOb : public CObject +{ +public: + CAccelsOb(); + CAccelsOb(CAccelsOb*pFrom); + CAccelsOb(BYTE cVirt, WORD wKey, bool bLocked = false); + CAccelsOb(LPACCEL pACCEL); +public: + CAccelsOb & operator=(const CAccelsOb& from); + + void GetString(CString& szBuffer); + bool IsEqual(WORD wKey, bool bCtrl, bool bAlt, bool bShift); + DWORD GetData(); + bool SetData(DWORD dwDatas); +public: +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif +public: + BYTE m_cVirt; + WORD m_wKey; + bool m_bLocked; +}; + +////////////////////////////////////////////////////////////////////// +// +// +class CCmdAccelOb : public CObject +{ +public: + CCmdAccelOb(); + CCmdAccelOb(WORD wIDCommand, LPCTSTR szCommand); + CCmdAccelOb(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked = false); + ~CCmdAccelOb(); +public: + void Add(CAccelsOb*pAccel); + void Add(BYTE cVirt, WORD wKey, bool bLocked = false); + void Reset(); + void DeleteUserAccels(); + + CCmdAccelOb & operator=(const CCmdAccelOb& from); +public: +#ifdef _DEBUG + virtual void AssertValid() const; + virtual void Dump(CDumpContext& dc) const; +#endif +public: + WORD m_wIDCommand; + CString m_szCommand; + + CList< CAccelsOb *, CAccelsOb * & > m_Accels; +}; + +//////////////////////////////////////////////////////////////////////// +#endif // __CMDACCEL_OB_INCLUDE + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ColorButton.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ColorButton.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,95 @@ +// ColorButton.cpp : implementation file +// + +#include "stdafx.h" +#include "ColorButton.h" + +bool ColorButton::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// ColorButton + +ColorButton::ColorButton() +{ + color = 0; + registerClass(); +} + +ColorButton::~ColorButton() +{ +} + + +BEGIN_MESSAGE_MAP(ColorButton, CButton) + //{{AFX_MSG_MAP(ColorButton) + // NOTE - the ClassWizard will add and remove mapping macros here. + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// ColorButton message handlers + +void ColorButton::PreSubclassWindow() +{ + SetWindowLong(m_hWnd, GWL_STYLE, GetStyle() | BS_OWNERDRAW); + CWnd::PreSubclassWindow(); +} + +void ColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) +{ + ASSERT(lpDrawItemStruct); + + int r = (color & 0x1f) << 3; + int g = (color & 0x3e0) >> 2; + int b = (color & 0x7c00) >> 7; + + HDC dc = lpDrawItemStruct->hDC; + UINT state = lpDrawItemStruct->itemState; + RECT rect = lpDrawItemStruct->rcItem; + + SIZE margins; + margins.cx = ::GetSystemMetrics(SM_CXEDGE); + margins.cy = ::GetSystemMetrics(SM_CYEDGE); + + if(GetState() & BST_PUSHED) + DrawEdge(dc, &rect, EDGE_SUNKEN, BF_RECT); + else + DrawEdge(dc, &rect, EDGE_RAISED, BF_RECT); + + InflateRect(&rect, -margins.cx, -margins.cy); + + HBRUSH br = CreateSolidBrush((state & ODS_DISABLED) ? + ::GetSysColor(COLOR_3DFACE) : RGB(r,g,b)); + + FillRect(dc, &rect, br); + + if(state & ODS_FOCUS) { + InflateRect(&rect, -1, -1); + DrawFocusRect(dc, &rect); + } + + DeleteObject(br); +} + +void ColorButton::setColor(u16 c) +{ + color = c; + Invalidate(); +} + +void ColorButton::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hIcon = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaColorButton"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ColorButton.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ColorButton.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,54 @@ +#if !defined(AFX_COLORBUTTON_H__DF02109B_B91C_49FD_954F_74A48B83C314__INCLUDED_) +#define AFX_COLORBUTTON_H__DF02109B_B91C_49FD_954F_74A48B83C314__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ColorButton.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// ColorButton window + +class ColorButton : public CButton +{ + // Construction +public: + ColorButton(); + + // Attributes +public: + // Operations + static bool isRegistered; +public: + void PreSubclassWindow(); + void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ColorButton) + //}}AFX_VIRTUAL + + // Implementation +public: + void setColor(u16 c); + u16 color; + virtual ~ColorButton(); + + void registerClass(); + + // Generated message map functions +protected: + //{{AFX_MSG(ColorButton) + // NOTE - the ClassWizard will add and remove member functions here. + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_COLORBUTTON_H__DF02109B_B91C_49FD_954F_74A48B83C314__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ColorControl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ColorControl.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,78 @@ +// ColorControl.cpp : implementation file +// + +#include "stdafx.h" +#include "ColorControl.h" + +bool ColorControl::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// ColorControl + +ColorControl::ColorControl() +{ + color = 0; + registerClass(); +} + +ColorControl::~ColorControl() +{ +} + + +BEGIN_MESSAGE_MAP(ColorControl, CWnd) + //{{AFX_MSG_MAP(ColorControl) + ON_WM_PAINT() + ON_WM_ERASEBKGND() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + + ///////////////////////////////////////////////////////////////////////////// +// ColorControl message handlers + +void ColorControl::OnPaint() +{ + CPaintDC dc(this); // device context for painting +} + +BOOL ColorControl::OnEraseBkgnd(CDC* pDC) +{ + int r = (color & 0x1f) << 3; + int g = (color & 0x3e0) >> 2; + int b = (color & 0x7c00) >> 7; + + CBrush br; + br.CreateSolidBrush(RGB(r,g,b)); + + RECT rect; + GetClientRect(&rect); + pDC->FillRect(&rect,&br); + pDC->DrawEdge(&rect, EDGE_SUNKEN, BF_RECT); + br.DeleteObject(); + return TRUE; +} + +void ColorControl::setColor(u16 c) +{ + color = c; + Invalidate(); +} + +void ColorControl::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hIcon = NULL; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaColorControl"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ColorControl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ColorControl.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,53 @@ +#if !defined(AFX_COLORCONTROL_H__747E1E47_DDFA_4D67_B337_A473F2BACB86__INCLUDED_) +#define AFX_COLORCONTROL_H__747E1E47_DDFA_4D67_B337_A473F2BACB86__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ColorControl.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// ColorControl window + +class ColorControl : public CWnd +{ + // Construction +public: + ColorControl(); + + // Attributes +public: + // Operations + static bool isRegistered; + + // Operations +public: + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ColorControl) + //}}AFX_VIRTUAL + + // Implementation +public: + void setColor(u16 c); + u16 color; + virtual ~ColorControl(); + + // Generated message map functions +protected: + //{{AFX_MSG(ColorControl) + afx_msg void OnPaint(); + afx_msg BOOL OnEraseBkgnd(CDC*pDC); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +private: + void registerClass(); +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_COLORCONTROL_H__747E1E47_DDFA_4D67_B337_A473F2BACB86__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Commands.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Commands.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,75 @@ +#include "stdafx.h" +#include +#include "resource.h" +#include "AcceleratorManager.h" + +#ifndef CMapStringToWord +typedef CMap< CString, LPCSTR, WORD, WORD & > CMapStringToWord; +#endif + +static CMapStringToWord winAccelStrings; +static bool initialized = false; + +struct +{ + const char *command; + WORD id; +} winAccelCommands[] = { + { "Minimize", ID_SYSTEM_MINIMIZE }, + { "Maximize", ID_SYSTEM_MAXIMIZE }, +}; + +void winAccelAddCommandsFromTable(CAcceleratorManager &mgr) +{ + int count = sizeof(winAccelCommands)/sizeof(winAccelCommands[0]); + + for (int i = 0; i < count; i++) + { + if (!mgr.AddCommandAccel(winAccelCommands[i].id, winAccelCommands[i].command, false)) + mgr.CreateEntry(winAccelCommands[i].id, winAccelCommands[i].command); + } +} + +// recursive calls +void winAccelAddCommandsFromMenu(CAcceleratorManager &mgr, CMenu *pMenu, const CString &parentStr) +{ + UINT nIndexMax = pMenu->GetMenuItemCount(); + for (UINT nIndex = 0; nIndex < nIndexMax; ++nIndex) + { + UINT nID = pMenu->GetMenuItemID(nIndex); + if (nID == 0) + continue; // menu separator or invalid cmd - ignore it + + if (nID == (UINT)-1) + { + // possibly a submenu + CMenu *pSubMenu = pMenu->GetSubMenu(nIndex); + if (pSubMenu != NULL) + { + CString tempStr; + pMenu->GetMenuString(nIndex, tempStr, MF_BYPOSITION); + tempStr.Remove('&'); + winAccelAddCommandsFromMenu(mgr, pSubMenu, parentStr + '\\' + tempStr); + } + } + else + { + // normal menu item + // generate the strings + CString command; + pMenu->GetMenuString(nIndex, command, MF_BYPOSITION); + int nPos = command.ReverseFind('\t'); + if (nPos != -1) + { + command.Delete(nPos, command.GetLength() - nPos); + } + command.Remove('&'); + command = parentStr + '\\' + command; + if (!mgr.AddCommandAccel(nID, command, false)) + { + mgr.CreateEntry(nID, command); + } + } + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Direct3D.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Direct3D.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,752 @@ +#include "stdafx.h" + +#define DIRECT3D_VERSION 0x0800 +#include "d3d8.h" +#include "d3dx8.h" + +#include "resource.h" +#include "MainWnd.h" +#include "Reg.h" +#include "VBA.h" + +//#include "../common/System.h" +#include "../gba/GBAGlobals.h" +#include "../gb/gbGlobals.h" +#include "../common/Text.h" +#include "../version.h" + +#ifdef MMX +extern "C" bool cpu_mmx; + +extern bool detectMMX(); +#endif + +extern int Init_2xSaI(u32); +extern void directXMessage(const char *); +extern void winlog(const char *, ...); + +typedef struct _D3DTLVERTEX +{ + float sx; /* Screen coordinates */ + float sy; + float sz; + float rhw; /* Reciprocal of homogeneous w */ + D3DCOLOR color; /* Vertex color */ + float tu; /* Texture coordinates */ + float tv; + _D3DTLVERTEX() { } + _D3DTLVERTEX(const D3DVECTOR& v, float _rhw, + D3DCOLOR _color, + float _tu, float _tv) + { + sx = v.x; sy = v.y; sz = v.z; rhw = _rhw; + color = _color; + tu = _tu; tv = _tv; + } +} D3DTLVERTEX, *LPD3DTLVERTEX; + +#define D3DFVF_TLVERTEX D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1 + +class Direct3DDisplay : public IDisplay +{ +public: + Direct3DDisplay(); + virtual ~Direct3DDisplay(); + + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual bool changeRenderSize(int w, int h); + virtual void resize(int w, int h); + virtual DISPLAY_TYPE getType() { return DIRECT_3D; }; + virtual void setOption(const char *, int); + virtual int selectFullScreenMode(GUID * *); + +private: + HINSTANCE d3dDLL; + LPDIRECT3D8 pD3D; + LPDIRECT3DDEVICE8 pDevice; + LPDIRECT3DTEXTURE8 pTexture; + D3DSURFACE_DESC dsdBackBuffer; + D3DPRESENT_PARAMETERS dpp; + D3DFORMAT screenFormat; + int width; + int height; + bool filterDisabled; + ID3DXFont *pFont; + bool failed; + unsigned int textureSize; + D3DTLVERTEX verts[4]; + + + void restoreDeviceObjects(); + void invalidateDeviceObjects(); + bool initializeOffscreen(int w, int h); + void updateFiltering(int); + void calculateVertices(); +}; + +Direct3DDisplay::Direct3DDisplay() +{ + d3dDLL = NULL; + pD3D = NULL; + pDevice = NULL; + pTexture = NULL; + pFont = NULL; + screenFormat = D3DFMT_R5G6B5; + width = 0; + height = 0; + filterDisabled = false; + failed = false; + textureSize = 0; +} + +Direct3DDisplay::~Direct3DDisplay() +{ + cleanup(); +} + +void Direct3DDisplay::cleanup() +{ + if (pD3D != NULL) + { + if (pFont) + { + pFont->Release(); + pFont = NULL; + } + + if (pTexture) + { + pTexture->Release(); + pTexture = NULL; + } + + if (pDevice) + { + pDevice->Release(); + pDevice = NULL; + } + + pD3D->Release(); + pD3D = NULL; + + if (d3dDLL != NULL) + { + FreeLibrary(d3dDLL); + d3dDLL = NULL; + } + } +} + +bool Direct3DDisplay::initialize() +{ + CWnd *pWnd = theApp.m_pMainWnd; + + d3dDLL = LoadLibrary("D3D8.DLL"); + LPDIRECT3D8 (WINAPI *D3DCreate)(UINT); + if (d3dDLL != NULL) + { + D3DCreate = (LPDIRECT3D8 (WINAPI *)(UINT)) + GetProcAddress(d3dDLL, "Direct3DCreate8"); + + if (D3DCreate == NULL) + { + directXMessage("Direct3DCreate8"); + return FALSE; + } + } + else + { + directXMessage("D3D8.DLL"); + return FALSE; + } + + pD3D = D3DCreate(120); + + if (pD3D == NULL) + { + winlog("Error creating Direct3D object\n"); + return FALSE; + } + + theApp.mode320Available = false; + theApp.mode640Available = false; + theApp.mode800Available = false; + + D3DDISPLAYMODE mode; + pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode); + + switch (mode.Format) + { + case D3DFMT_R8G8B8: + systemColorDepth = 24; + systemRedShift = 19; + systemGreenShift = 11; + systemBlueShift = 3; + break; + case D3DFMT_X8R8G8B8: + systemColorDepth = 32; + systemRedShift = 19; + systemGreenShift = 11; + systemBlueShift = 3; + Init_2xSaI(32); + break; + case D3DFMT_R5G6B5: + systemColorDepth = 16; + systemRedShift = 11; + systemGreenShift = 6; + systemBlueShift = 0; + Init_2xSaI(565); + break; + case D3DFMT_X1R5G5B5: + systemColorDepth = 16; + systemRedShift = 10; + systemGreenShift = 5; + systemBlueShift = 0; + Init_2xSaI(555); + break; + default: + systemMessage(0, "Unsupport D3D format %d", mode.Format); + return false; + } + theApp.fsColorDepth = systemColorDepth; + +#ifdef MMX + if (!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + screenFormat = mode.Format; + + // check for available fullscreen modes + ZeroMemory(&dpp, sizeof(dpp)); + dpp.Windowed = TRUE; + dpp.BackBufferFormat = mode.Format; + dpp.BackBufferCount = 1; + dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; + dpp.BackBufferWidth = theApp.surfaceSizeX; + dpp.BackBufferHeight = theApp.surfaceSizeY; + + HRESULT hret = pD3D->CreateDevice(D3DADAPTER_DEFAULT, + D3DDEVTYPE_HAL, + pWnd->GetSafeHwnd(), + D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, + &dpp, + &pDevice); + if (!SUCCEEDED(hret)) + { + winlog("Error creating Direct3DDevice %08x\n", hret); + return false; + } + + restoreDeviceObjects(); + + switch (systemColorDepth) + { + case 16: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + case 24: + case 32: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + } + + theApp.updateFilter(); + theApp.updateIFB(); + + if (failed) + return false; + + pWnd->DragAcceptFiles(TRUE); + + return TRUE; +} + +bool Direct3DDisplay::initializeOffscreen(int w, int h) +{ + int size = 256; + if (w > 512 || h > 512) + size = 1024; + else if (w > 256 || h > 256) + size = 512; + textureSize = size; + + UINT ww = size; + UINT hh = size; + D3DFORMAT format = screenFormat; + + if (SUCCEEDED(D3DXCheckTextureRequirements(pDevice, + &ww, + &hh, + NULL, + 0, + &format, + D3DPOOL_MANAGED))) + { + if ((int)ww < w || (int)hh < h) + { + if (theApp.filterFunction) + { + filterDisabled = true; + theApp.filterFunction = NULL; + systemMessage(0, "3D card cannot support needed texture size for filter function. Disabling it"); + } + } + else + filterDisabled = false; + if (SUCCEEDED(D3DXCreateTexture(pDevice, + ww, + hh, + 0, + 0, + format, + D3DPOOL_MANAGED, + &pTexture))) + { + width = w; + height = h; + return true; + } + } + return false; +} + +void Direct3DDisplay::updateFiltering(int filter) +{ + switch (filter) + { + default: + case 0: + // point filtering + pDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_POINT); + pDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT); + pDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_POINT); + break; + case 1: + // bilinear + pDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); + pDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); + pDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_POINT); + break; + } +} + +void Direct3DDisplay::restoreDeviceObjects() +{ + // Store render target surface desc + LPDIRECT3DSURFACE8 pBackBuffer; + HRESULT hr; + if (SUCCEEDED(hr = pDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer))) + { + pBackBuffer->GetDesc(&dsdBackBuffer); + pBackBuffer->Release(); + } + else + systemMessage(0, "Failed GetBackBuffer %08x", hr); + + // Set up the texture + pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); + pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); + pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); + + int filter = regQueryDwordValue("d3dFilter", 0); + if (filter < 0 || filter > 3) + filter = 0; + updateFiltering(filter); + + // Set miscellaneous render states + pDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); + pDevice->SetRenderState(D3DRS_ZENABLE, FALSE); + + // Set the projection matrix + D3DXMATRIX matProj; + FLOAT fAspect = ((FLOAT)dsdBackBuffer.Width) / dsdBackBuffer.Height; + D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f); + pDevice->SetTransform(D3DTS_PROJECTION, &matProj); + + // turn off lighting + pDevice->SetRenderState(D3DRS_LIGHTING, FALSE); + + if (pFont) + { + pFont->Release(); + pFont = NULL; + } + // Create a D3D font using D3DX + HFONT hFont = CreateFont(14, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, + ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, + ANTIALIASED_QUALITY, FF_DONTCARE, "Arial"); + D3DXCreateFont(pDevice, hFont, &pFont); +} + +void Direct3DDisplay::clear() +{} + +void Direct3DDisplay::renderMenu() +{ + checkFullScreen(); + if (theApp.m_pMainWnd) + theApp.m_pMainWnd->DrawMenuBar(); +} + +void Direct3DDisplay::checkFullScreen() +{ + // if(tripleBuffering) + // pDirect3D->FlipToGDISurface(); +} + +void Direct3DDisplay::calculateVertices() +{ + // color + D3DCOLOR col = 0xFFFFFFFF; + + // calculate rhw + FLOAT z = 1.0f; + FLOAT rhw = 1.0f / (z * 990.0f + 10.0f); + + // -0.5f is necessary in order to match texture alignment to display pixels + FLOAT left = -0.5f; + FLOAT right = dpp.BackBufferWidth - 0.5f; + FLOAT top = -0.5f; + FLOAT bottom = dpp.BackBufferHeight - 0.5f; + + FLOAT textureX = (FLOAT)theApp.rect.right / (FLOAT)textureSize; + FLOAT textureY = (FLOAT)theApp.rect.bottom / (FLOAT)textureSize; + +//#define D3D_DRAW_SINGLE_RECTANGLE +#ifdef D3D_DRAW_SINGLE_RECTANGLE + // set up a rectangle + verts[0] = D3DTLVERTEX(D3DXVECTOR3(left, top, z), rhw, col, 0.0f, 0.0f); + verts[1] = D3DTLVERTEX(D3DXVECTOR3(right, top, z), rhw, col, textureX, 0.0f); + verts[2] = D3DTLVERTEX(D3DXVECTOR3(right, bottom, z), rhw, col, textureX, textureY); + verts[3] = D3DTLVERTEX(D3DXVECTOR3(left, bottom, z), rhw, col, 0.0f, textureY); +#else + // set up triangles + verts[0] = D3DTLVERTEX(D3DXVECTOR3(left, bottom, z), rhw, col, 0.0f, textureY); + verts[1] = D3DTLVERTEX(D3DXVECTOR3(left, top, z), rhw, col, 0.0f, 0.0f); + verts[2] = D3DTLVERTEX(D3DXVECTOR3(right, bottom, z), rhw, col, textureX, textureY); + verts[3] = D3DTLVERTEX(D3DXVECTOR3(right, top, z), rhw, col, textureX, 0.0f); +#endif +} + +void Direct3DDisplay::render() +{ + if (!pDevice) + return; + + // Test the cooperative level to see if it's okay to render + if (FAILED(pDevice->TestCooperativeLevel())) + { + return; + } + pDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0x000000ff, 1.0f, 0L); + + if (SUCCEEDED(pDevice->BeginScene())) + { + D3DLOCKED_RECT locked; + if (pTexture && SUCCEEDED(pTexture->LockRect(0, &locked, NULL, 0))) + { + if (theApp.filterFunction) + { + if (systemColorDepth == 16) + theApp.filterFunction(pix + theApp.filterWidth * 2 + 4, + theApp.filterWidth*2 + 4, + (u8 *)theApp.delta, + (u8 *)locked.pBits, + locked.Pitch, + theApp.filterWidth, + theApp.filterHeight); + else + theApp.filterFunction(pix + theApp.filterWidth * 4 + 4, + theApp.filterWidth * 4 + 4, + (u8 *)theApp.delta, + (u8 *)locked.pBits, + locked.Pitch, + theApp.filterWidth, + theApp.filterHeight); + } + else + { + int copyX = 240; + int copyY = 160; + + if (systemCartridgeType == 1) + { + if (gbBorderOn) + { + copyX = 256; + copyY = 224; + } + else + { + copyX = 160; + copyY = 144; + } + } + // MMX doesn't seem to be faster to copy the data + __asm { + mov eax, copyX; + mov ebx, copyY; + + mov esi, pix; + mov edi, locked.pBits; + mov edx, locked.Pitch; + cmp systemColorDepth, 16; + jnz gbaOtherColor; + sub edx, eax; + sub edx, eax; + lea esi, [esi+2*eax+4]; + shr eax, 1; +gbaLoop16bit: + mov ecx, eax; + repz movsd; + inc esi; + inc esi; + inc esi; + inc esi; + add edi, edx; + dec ebx; + jnz gbaLoop16bit; + jmp gbaLoopEnd; +gbaOtherColor: + cmp systemColorDepth, 32; + jnz gbaOtherColor2; + + sub edx, eax; + sub edx, eax; + sub edx, eax; + sub edx, eax; + lea esi, [esi+4*eax+4]; +gbaLoop32bit: + mov ecx, eax; + repz movsd; + add esi, 4; + add edi, edx; + dec ebx; + jnz gbaLoop32bit; + jmp gbaLoopEnd; +gbaOtherColor2: + lea eax, [eax+2*eax]; + sub edx, eax; +gbaLoop24bit: + mov ecx, eax; + shr ecx, 2; + repz movsd; + add edi, edx; + dec ebx; + jnz gbaLoop24bit; +gbaLoopEnd: + } + } + + if (theApp.videoOption > VIDEO_4X && theApp.showSpeed) + { + char buffer[30]; + if (theApp.showSpeed == 1) + sprintf(buffer, "%3d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + if (theApp.showSpeedTransparent) + drawTextTransp((u8 *)locked.pBits, + locked.Pitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8 *)locked.pBits, + locked.Pitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + } + + if (textMethod == 1) + { + DrawTextMessages((u8 *)locked.pBits, locked.Pitch, theApp.rect.left, theApp.rect.bottom); + } + + pTexture->UnlockRect(0); + + // set the texture + pDevice->SetTexture(0, pTexture); + + // configure shader for vertex type + pDevice->SetVertexShader(D3DFVF_TLVERTEX); + +#ifdef D3D_DRAW_SINGLE_RECTANGLE + // draw the rectangle + pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, verts, sizeof(D3DTLVERTEX)); + //#undef D3D_DRAW_RECT +#else + // draw the triangles + pDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, verts, sizeof(D3DTLVERTEX)); +#endif + } + + if (textMethod == 2) + { + for (int slot = 0; slot < SCREEN_MESSAGE_SLOTS; slot++) + { + if (theApp.screenMessage[slot]) + { + if ((theApp.screenMessageDuration[slot] < 0 || + (int)(GetTickCount() - theApp.screenMessageTime[slot]) < theApp.screenMessageDuration[slot]) && + (!theApp.disableStatusMessage || slot == 1 || slot == 2) && pFont) + { + pFont->Begin(); + RECT r; + D3DCOLOR color; + + r.left = 10; + r.top = dpp.BackBufferHeight - 20*(slot+1); + r.right = dpp.BackBufferWidth - 10; + r.bottom = r.top + 20; + + if (outlinedText) + { + color = textColor != 7 ? D3DCOLOR_ARGB(255, 0, 0, 0) : D3DCOLOR_ARGB(255, 255, 255, 255); + + // draw outline + const static int xd [8] = {-1, 0, 1, 1, 1, 0, -1, -1}; + const static int yd [8] = {-1, -1, -1, 0, 1, 1, 1, 0}; + for (int i = 0; i < 8; i++) + { + RECT r2 = r; + r2.left += xd[i]; r2.right += xd[i]; + r2.top += yd[i]; r2.bottom += yd[i]; + + pFont->DrawText(theApp.screenMessageBuffer[slot], -1, &r2, 0, color); + } + } + + // draw center text + switch (textColor) + { + case 0: + color = D3DCOLOR_ARGB(255, 255, 255, 255); break; + case 1: + color = D3DCOLOR_ARGB(255, 255, 0, 0); break; + case 2: + color = D3DCOLOR_ARGB(255, 255, 255, 0); break; + case 3: + color = D3DCOLOR_ARGB(255, 0, 255, 0); break; + case 4: + color = D3DCOLOR_ARGB(255, 0, 255, 255); break; + case 5: + color = D3DCOLOR_ARGB(255, 0, 0, 255); break; + case 6: + color = D3DCOLOR_ARGB(255, 255, 0, 255); break; + case 7: + color = D3DCOLOR_ARGB(255, 0, 0, 0); break; + } + pFont->DrawText(theApp.screenMessageBuffer[slot], -1, &r, 0, color); + + pFont->End(); + } + else + { + theApp.screenMessage[slot] = false; + } + } + } + } + + pDevice->EndScene(); + + pDevice->Present(NULL, NULL, NULL, NULL); + } +} + +void Direct3DDisplay::invalidateDeviceObjects() +{ + if (pFont) + pFont->Release(); + pFont = NULL; +} + +void Direct3DDisplay::resize(int w, int h) +{ + if (pDevice && w > 0 && h > 0) + { + dpp.BackBufferWidth = w; + dpp.BackBufferHeight = h; + HRESULT hr; + invalidateDeviceObjects(); + if (SUCCEEDED(hr = pDevice->Reset(&dpp))) + { + restoreDeviceObjects(); + } + else + systemMessage(0, "Failed device reset %08x", hr); + } + calculateVertices(); +} + +bool Direct3DDisplay::changeRenderSize(int w, int h) +{ + if (w != width || h != height) + { + if (pTexture) + { + pTexture->Release(); + pTexture = NULL; + } + if (!initializeOffscreen(w, h)) + { + failed = true; + return false; + } + } + calculateVertices(); + + return true; +} + +void Direct3DDisplay::setOption(const char *option, int value) +{ + if (!strcmp(option, "d3dFilter")) + updateFiltering(value); +} + +int Direct3DDisplay::selectFullScreenMode(GUID * *) +{ + HWND wnd = GetDesktopWindow(); + RECT r; + GetWindowRect(wnd, &r); + int w = (r.right - r.left) & 4095; + int h = (r.bottom - r.top) & 4095; + HDC dc = GetDC(wnd); + int c = GetDeviceCaps(dc, BITSPIXEL); + ReleaseDC(wnd, dc); + + return (c << 24) | (w << 12) | h; +} + +IDisplay *newDirect3DDisplay() +{ + return new Direct3DDisplay(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/DirectDraw.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/DirectDraw.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,921 @@ +#include "stdafx.h" + +#define DIRECTDRAW_VERSION 0x0700 +#include "ddraw.h" + +#include "resource.h" +#include "MainWnd.h" +#include "Reg.h" +#include "VBA.h" + +#include "../common/System.h" +#include "../gba/GBAGlobals.h" +#include "../gb/gbGlobals.h" +#include "../common/Text.h" +#include "../version.h" + +extern u32 RGB_LOW_BITS_MASK; +extern int systemSpeed; +extern int Init_2xSaI(u32); +extern void directXMessage(const char *); +extern void winlog(const char *, ...); +extern int winVideoModeSelect(CWnd *, GUID * *); + +class DirectDrawDisplay : public IDisplay +{ +private: + HINSTANCE ddrawDLL; + LPDIRECTDRAW7 pDirectDraw; + LPDIRECTDRAWSURFACE7 ddsPrimary; + LPDIRECTDRAWSURFACE7 ddsOffscreen; + LPDIRECTDRAWSURFACE7 ddsFlip; + LPDIRECTDRAWCLIPPER ddsClipper; + int width; + int height; + bool failed; + + bool initializeOffscreen(int w, int h); +public: + DirectDrawDisplay(); + virtual ~DirectDrawDisplay(); + + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual bool changeRenderSize(int w, int h); + virtual DISPLAY_TYPE getType() { return DIRECT_DRAW; }; + virtual void setOption(const char *, int) {} + virtual int selectFullScreenMode(GUID * *); +}; + +static HRESULT WINAPI checkModesAvailable(LPDDSURFACEDESC2 surf, LPVOID lpContext) +{ + if (surf->dwWidth == 320 && + surf->dwHeight == 240 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) + { + theApp.mode320Available = TRUE; + } + if (surf->dwWidth == 640 && + surf->dwHeight == 480 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) + { + theApp.mode640Available = TRUE; + } + if (surf->dwWidth == 800 && + surf->dwHeight == 600 && + surf->ddpfPixelFormat.dwRGBBitCount == 16) + { + theApp.mode800Available = TRUE; + } + return DDENUMRET_OK; +} + +static int ffs(UINT mask) +{ + int m = 0; + if (mask) + { + while (!(mask & (1 << m))) + m++; + + return (m); + } + + return (0); +} + +DirectDrawDisplay::DirectDrawDisplay() +{ + pDirectDraw = NULL; + ddsPrimary = NULL; + ddsOffscreen = NULL; + ddsFlip = NULL; + ddsClipper = NULL; + ddrawDLL = NULL; + width = 0; + height = 0; + failed = false; +} + +DirectDrawDisplay::~DirectDrawDisplay() +{ + cleanup(); +} + +void DirectDrawDisplay::cleanup() +{ + if (pDirectDraw != NULL) + { + if (ddsClipper != NULL) + { + ddsClipper->Release(); + ddsClipper = NULL; + } + + if (ddsFlip != NULL) + { + ddsFlip->Release(); + ddsFlip = NULL; + } + + if (ddsOffscreen != NULL) + { + ddsOffscreen->Release(); + ddsOffscreen = NULL; + } + + if (ddsPrimary != NULL) + { + ddsPrimary->Release(); + ddsPrimary = NULL; + } + + pDirectDraw->Release(); + pDirectDraw = NULL; + } + + if (ddrawDLL != NULL) + { + /**/ ::FreeLibrary(ddrawDLL); + ddrawDLL = NULL; + } + width = 0; + height = 0; +} + +bool DirectDrawDisplay::initialize() +{ + CWnd *pWnd = theApp.m_pMainWnd; + + GUID *guid = NULL; + if (theApp.ddrawEmulationOnly) + guid = (GUID *)DDCREATE_EMULATIONONLY; + + if (theApp.pVideoDriverGUID) + guid = theApp.pVideoDriverGUID; + + ddrawDLL = /**/ ::LoadLibrary("DDRAW.DLL"); + HRESULT (WINAPI *DDrawCreateEx)(GUID *, LPVOID *, REFIID, IUnknown *); + if (ddrawDLL != NULL) + { + DDrawCreateEx = (HRESULT (WINAPI *)(GUID *, LPVOID *, REFIID, IUnknown *)) + GetProcAddress(ddrawDLL, "DirectDrawCreateEx"); + + if (DDrawCreateEx == NULL) + { + directXMessage("DirectDrawCreateEx"); + return FALSE; + } + } + else + { + directXMessage("DDRAW.DLL"); + return FALSE; + } + + theApp.ddrawUsingEmulationOnly = theApp.ddrawEmulationOnly; + + HRESULT hret = DDrawCreateEx(guid, + (void * *)&pDirectDraw, + IID_IDirectDraw7, + NULL); + + if (hret != DD_OK) + { + winlog("Error creating DirectDraw object %08x\n", hret); + if (theApp.ddrawEmulationOnly) + { + // disable emulation only setting in case of failure + regSetDwordValue("ddrawEmulationOnly", 0); + } + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWCREATE), hret); + return FALSE; + } + + if (theApp.ddrawDebug) + { + DDCAPS driver; + DDCAPS hel; + ZeroMemory(&driver, sizeof(driver)); + ZeroMemory(&hel, sizeof(hel)); + driver.dwSize = sizeof(driver); + hel.dwSize = sizeof(hel); + pDirectDraw->GetCaps(&driver, &hel); + int i; + DWORD *p = (DWORD *)&driver; + for (i = 0; i < (int)driver.dwSize; i += 4) + winlog("Driver CAPS %2d: %08x\n", i>>2, *p++); + p = (DWORD *)&hel; + for (i = 0; i < (int)hel.dwSize; i += 4) + winlog("HEL CAPS %2d: %08x\n", i>>2, *p++); + } + + theApp.mode320Available = false; + theApp.mode640Available = false; + theApp.mode800Available = false; + + // check for available fullscreen modes + pDirectDraw->EnumDisplayModes(DDEDM_STANDARDVGAMODES, NULL, NULL, checkModesAvailable); + + DWORD flags = DDSCL_NORMAL; + + if (theApp.videoOption >= VIDEO_320x240) + flags = DDSCL_ALLOWMODEX | + DDSCL_ALLOWREBOOT | + DDSCL_EXCLUSIVE | + DDSCL_FULLSCREEN; + + hret = pDirectDraw->SetCooperativeLevel(pWnd->m_hWnd, flags); + + if (hret != DD_OK) + { + winlog("Error SetCooperativeLevel %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWLEVEL), hret); + return FALSE; + } + + if (theApp.videoOption > VIDEO_4X) + { + hret = pDirectDraw->SetDisplayMode(theApp.fsWidth, + theApp.fsHeight, + theApp.fsColorDepth, + 0, + 0); + if (hret != DD_OK) + { + winlog("Error SetDisplayMode %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSET), hret); + return FALSE; + } + } + + DDSURFACEDESC2 ddsd; + ZeroMemory(&ddsd, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_CAPS; + ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; + + if (theApp.videoOption > VIDEO_4X && theApp.tripleBuffering) + { + // setup triple buffering + ddsd.dwFlags |= DDSD_BACKBUFFERCOUNT; + ddsd.ddsCaps.dwCaps |= DDSCAPS_COMPLEX | DDSCAPS_FLIP; + ddsd.dwBackBufferCount = 2; + } + + hret = pDirectDraw->CreateSurface(&ddsd, &ddsPrimary, NULL); + if (hret != DD_OK) + { + winlog("Error primary CreateSurface %08x\n", hret); + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSURFACE), hret); + return FALSE; + } + + if (theApp.ddrawDebug) + { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + ddsPrimary->GetCaps(&caps); + + winlog("Primary CAPS 1: %08x\n", caps.dwCaps); + winlog("Primary CAPS 2: %08x\n", caps.dwCaps2); + winlog("Primary CAPS 3: %08x\n", caps.dwCaps3); + winlog("Primary CAPS 4: %08x\n", caps.dwCaps4); + } + + if (theApp.videoOption > VIDEO_4X && theApp.tripleBuffering) + { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + // this gets the third surface. The front one is the primary, + // the second is the backbuffer and the third is the flip + // surface + caps.dwCaps = DDSCAPS_BACKBUFFER; + + hret = ddsPrimary->GetAttachedSurface(&caps, &ddsFlip); + if (hret != DD_OK) + { + winlog("Failed to get attached surface %08x", hret); + return FALSE; + } + ddsFlip->AddRef(); + clear(); + } + + // create clipper in all modes to avoid paint problems + // if(videoOption <= VIDEO_4X) { + hret = pDirectDraw->CreateClipper(0, &ddsClipper, NULL); + if (hret == DD_OK) + { + ddsClipper->SetHWnd(0, pWnd->m_hWnd); + if (theApp.videoOption > VIDEO_4X) + { + if (theApp.tripleBuffering) + ddsFlip->SetClipper(ddsClipper); + else + ddsPrimary->SetClipper(ddsClipper); + } + else + ddsPrimary->SetClipper(ddsClipper); + } + // } + + DDPIXELFORMAT px; + + px.dwSize = sizeof(px); + + hret = ddsPrimary->GetPixelFormat(&px); + + switch (px.dwRGBBitCount) + { + case 15: + case 16: + systemColorDepth = 16; + break; + case 24: + systemColorDepth = 24; + theApp.filterFunction = NULL; + break; + case 32: + systemColorDepth = 32; + break; + default: + systemMessage( + IDS_ERROR_DISP_COLOR, + "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode.", + px.dwRGBBitCount); + return FALSE; + } + theApp.updateFilter(); + theApp.updateIFB(); + + if (failed) + return false; + + pWnd->DragAcceptFiles(TRUE); + + return true; +} + +bool DirectDrawDisplay::changeRenderSize(int w, int h) +{ + if (w != width || h != height) + { + if (ddsOffscreen) + { + ddsOffscreen->Release(); + ddsOffscreen = NULL; + } + if (!initializeOffscreen(w, h)) + { + failed = true; + return false; + } + } + return true; +} + +bool DirectDrawDisplay::initializeOffscreen(int w, int h) +{ + DDSURFACEDESC2 ddsd; + + ZeroMemory(&ddsd, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; + ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; + if (theApp.ddrawUseVideoMemory) + ddsd.ddsCaps.dwCaps |= (DDSCAPS_LOCALVIDMEM|DDSCAPS_VIDEOMEMORY); + ddsd.dwWidth = w; + ddsd.dwHeight = h; + + HRESULT hret = pDirectDraw->CreateSurface(&ddsd, &ddsOffscreen, NULL); + + if (hret != DD_OK) + { + winlog("Error offscreen CreateSurface %08x\n", hret); + if (theApp.ddrawUseVideoMemory) + { + regSetDwordValue("ddrawUseVideoMemory", 0); + } + // errorMessage(myLoadString(IDS_ERROR_DISP_DRAWSURFACE2), hret); + return false; + } + + if (theApp.ddrawDebug) + { + DDSCAPS2 caps; + ZeroMemory(&caps, sizeof(caps)); + ddsOffscreen->GetCaps(&caps); + + winlog("Offscreen CAPS 1: %08x\n", caps.dwCaps); + winlog("Offscreen CAPS 2: %08x\n", caps.dwCaps2); + winlog("Offscreen CAPS 3: %08x\n", caps.dwCaps3); + winlog("Offscreen CAPS 4: %08x\n", caps.dwCaps4); + } + + DDPIXELFORMAT px; + + px.dwSize = sizeof(px); + + hret = ddsOffscreen->GetPixelFormat(&px); + + if (theApp.ddrawDebug) + { + DWORD *pdword = (DWORD *)&px; + for (int ii = 0; ii < 8; ii++) + { + winlog("Pixel format %d %08x\n", ii, pdword[ii]); + } + } + + switch (px.dwRGBBitCount) + { + case 15: + case 16: + systemColorDepth = 16; + break; + case 24: + systemColorDepth = 24; + theApp.filterFunction = NULL; + break; + case 32: + systemColorDepth = 32; + break; + default: + systemMessage( + IDS_ERROR_DISP_COLOR, + "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode.", + px.dwRGBBitCount); + return FALSE; + } + if (theApp.ddrawDebug) + { + winlog("R Mask: %08x\n", px.dwRBitMask); + winlog("G Mask: %08x\n", px.dwGBitMask); + winlog("B Mask: %08x\n", px.dwBBitMask); + } + + systemRedShift = ffs(px.dwRBitMask); + systemGreenShift = ffs(px.dwGBitMask); + systemBlueShift = ffs(px.dwBBitMask); + +#ifdef MMX + if (!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + if ((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0xF800 && + px.dwGBitMask == 0x07E0 && + px.dwBBitMask == 0x001F) + { + systemGreenShift++; + Init_2xSaI(565); + RGB_LOW_BITS_MASK = 0x821; + } + else if ((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x7C00 && + px.dwGBitMask == 0x03E0 && + px.dwBBitMask == 0x001F) + { + Init_2xSaI(555); + RGB_LOW_BITS_MASK = 0x421; + } + else if ((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x001F && + px.dwGBitMask == 0x07E0 && + px.dwBBitMask == 0xF800) + { + systemGreenShift++; + Init_2xSaI(565); + RGB_LOW_BITS_MASK = 0x821; + } + else if ((px.dwFlags&DDPF_RGB) != 0 && + px.dwRBitMask == 0x001F && + px.dwGBitMask == 0x03E0 && + px.dwBBitMask == 0x7C00) + { + Init_2xSaI(555); + RGB_LOW_BITS_MASK = 0x421; + } + else + { + // 32-bit or 24-bit + if (systemColorDepth == 32 || systemColorDepth == 24) + { + systemRedShift += 3; + systemGreenShift += 3; + systemBlueShift += 3; + if (systemColorDepth == 32) + Init_2xSaI(32); + } + } + + if (theApp.ddrawDebug) + { + winlog("R shift: %d\n", systemRedShift); + winlog("G shift: %d\n", systemGreenShift); + winlog("B shift: %d\n", systemBlueShift); + } + + switch (systemColorDepth) + { + case 16: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + case 24: + case 32: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + } + width = w; + height = h; + return true; +} + +void DirectDrawDisplay::clear() +{ + if (theApp.videoOption <= VIDEO_4X || !theApp.tripleBuffering || ddsFlip == NULL) + return; + + DDBLTFX fx; + ZeroMemory(&fx, sizeof(fx)); + fx.dwSize = sizeof(fx); + fx.dwFillColor = 0; + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); + ddsFlip->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &fx); + ddsPrimary->Flip(NULL, 0); +} + +void DirectDrawDisplay::renderMenu() +{ + checkFullScreen(); + theApp.m_pMainWnd->DrawMenuBar(); +} + +void DirectDrawDisplay::checkFullScreen() +{ + if (theApp.tripleBuffering) + pDirectDraw->FlipToGDISurface(); +} + +void DirectDrawDisplay::render() +{ + HRESULT hret; + + if (pDirectDraw == NULL || + ddsOffscreen == NULL || + ddsPrimary == NULL) + return; + + bool fastForward = speedup; +#if (defined(WIN32) && !defined(SDL)) + if (theApp.frameSearchSkipping) + { + if (theApp.frameSearchFirstStep) + fastForward = true; + else + return; // don't render skipped frame search frames + } +#endif + + DDSURFACEDESC2 ddsDesc; + + ZeroMemory(&ddsDesc, sizeof(ddsDesc)); + + ddsDesc.dwSize = sizeof(ddsDesc); + + hret = ddsOffscreen->Lock(NULL, + &ddsDesc, +#ifndef FINAL_VERSION + DDLOCK_NOSYSLOCK | +#endif + DDLOCK_WRITEONLY | DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, + NULL); + + if (hret == DDERR_SURFACELOST) + { + hret = ddsPrimary->Restore(); + if (hret == DD_OK) + { + hret = ddsOffscreen->Restore(); + + if (hret == DD_OK) + { + hret = ddsOffscreen->Lock(NULL, + &ddsDesc, +#ifndef FINAL_VERSION + DDLOCK_NOSYSLOCK | +#endif + DDLOCK_WRITEONLY | DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, + NULL); + } + } + } + + if (hret == DD_OK) + { + if (theApp.filterFunction) + { + if (systemColorDepth == 16) + (*theApp.filterFunction)(pix + theApp.filterWidth * 2 + 4, + theApp.filterWidth * 2 + 4, + (u8 *)theApp.delta, + (u8 *)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.filterWidth, + theApp.filterHeight); + else + (*theApp.filterFunction)(pix + theApp.filterWidth * 4 + 4, + theApp.filterWidth * 4 + 4, + (u8 *)theApp.delta, + (u8 *)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.filterWidth, + theApp.filterHeight); + } + else + { + int copyX = 240; + int copyY = 160; + + if (systemCartridgeType == 1) + { + if (gbBorderOn) + { + copyX = 256; + copyY = 224; + } + else + { + copyX = 160; + copyY = 144; + } + } + // MMX doesn't seem to be faster to copy the data + __asm { + mov eax, copyX; + mov ebx, copyY; + + mov esi, pix; + mov edi, ddsDesc.lpSurface; + mov edx, ddsDesc.lPitch; + cmp systemColorDepth, 16; + jnz gbaOtherColor; + sub edx, eax; + sub edx, eax; + lea esi, [esi+2*eax+4]; + shr eax, 1; +gbaLoop16bit: + mov ecx, eax; + repz movsd; + inc esi; + inc esi; + inc esi; + inc esi; + add edi, edx; + dec ebx; + jnz gbaLoop16bit; + jmp gbaLoopEnd; +gbaOtherColor: + cmp systemColorDepth, 32; + jnz gbaOtherColor2; + + sub edx, eax; + sub edx, eax; + sub edx, eax; + sub edx, eax; + lea esi, [esi+4*eax+4]; +gbaLoop32bit: + mov ecx, eax; + repz movsd; + add esi, 4; + add edi, edx; + dec ebx; + jnz gbaLoop32bit; + jmp gbaLoopEnd; +gbaOtherColor2: + lea eax, [eax+2*eax]; + sub edx, eax; +gbaLoop24bit: + mov ecx, eax; + shr ecx, 2; + repz movsd; + add edi, edx; + dec ebx; + jnz gbaLoop24bit; +gbaLoopEnd: + } + } + if (theApp.showSpeed && theApp.videoOption > VIDEO_4X) + { + char buffer[30]; + if (theApp.showSpeed == 1) + sprintf(buffer, "%3d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + if (theApp.showSpeedTransparent) + drawTextTransp((u8 *)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8 *)ddsDesc.lpSurface, + ddsDesc.lPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + } + + if (textMethod == 1) + { + DrawTextMessages((u8 *)ddsDesc.lpSurface, ddsDesc.lPitch, theApp.rect.left, theApp.rect.bottom); + } + } + else if (theApp.ddrawDebug) + winlog("Error during lock: %08x\n", hret); + + hret = ddsOffscreen->Unlock(NULL); + + if (hret == DD_OK) + { + // the correct point where to wait + if (theApp.vsync && !fastForward) + { + hret = pDirectDraw->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, 0); + } + + ddsOffscreen->PageLock(0); + if (theApp.tripleBuffering && theApp.videoOption > VIDEO_4X) + { + hret = ddsFlip->Blt(&theApp.dest, ddsOffscreen, NULL, DDBLT_WAIT, NULL); + if (hret == DD_OK) + { + if (theApp.menuToggle || !theApp.active) + { + pDirectDraw->FlipToGDISurface(); + ddsPrimary->SetClipper(ddsClipper); + hret = ddsPrimary->Blt(&theApp.dest, ddsFlip, NULL, DDBLT_ASYNC, NULL); + // if using emulation only, then we have to redraw the menu + // everytime. It seems like a bug in DirectDraw to me as we not + // overwritting the menu area at all. + if (theApp.ddrawUsingEmulationOnly) + theApp.m_pMainWnd->DrawMenuBar(); + } + else + hret = ddsPrimary->Flip(NULL, 0); + } + } + else + { + hret = ddsPrimary->Blt(&theApp.dest, ddsOffscreen, NULL, DDBLT_ASYNC, NULL); + + if (hret == DDERR_SURFACELOST) + { + hret = ddsPrimary->Restore(); + + if (hret == DD_OK) + { + hret = ddsPrimary->Blt(&theApp.dest, ddsOffscreen, NULL, DDBLT_ASYNC, NULL); + } + } + } + ddsOffscreen->PageUnlock(0); + } + else if (theApp.ddrawDebug) + winlog("Error during unlock: %08x\n", hret); + + bool textMessageStarted = false; + + if (textMethod == 2) + { + HDC hdc; + + for (int slot = 0; slot < SCREEN_MESSAGE_SLOTS; slot++) + { + if (theApp.screenMessage[slot]) + { + if ((theApp.screenMessageDuration[slot] < 0 || + (int)(GetTickCount() - theApp.screenMessageTime[slot]) < theApp.screenMessageDuration[slot]) && + (!theApp.disableStatusMessage || slot == 1 || slot == 2)) + { + if (!textMessageStarted) + { + textMessageStarted = true; + ddsPrimary->SetClipper(ddsClipper); + ddsPrimary->GetDC(&hdc); + SetBkMode(hdc, TRANSPARENT); + SetTextColor(hdc, textColor != 7 ? RGB(0, 0, 0) : RGB(255, 255, 255)); + } + + if (outlinedText) + { + // draw black outline + const static int xd [8] = {-1, 0, 1, 1, 1, 0, -1, -1}; + const static int yd [8] = {-1, -1, -1, 0, 1, 1, 1, 0}; + for (int i = 0; i < 8; i++) + { + TextOut(hdc, + theApp.dest.left+10+xd[i], + theApp.dest.bottom - 20*(slot+1)+yd[i], + theApp.screenMessageBuffer[slot], + strlen(theApp.screenMessageBuffer[slot])); + } + } + } + else + { + theApp.screenMessage[slot] = false; + } + } + } + + if (textMessageStarted) + { + COLORREF color; + switch (textColor) + { + case 0: + color = RGB(255, 255, 255); break; + case 1: + color = RGB(255, 0, 0); break; + case 2: + color = RGB(255, 255, 0); break; + case 3: + color = RGB(0, 255, 0); break; + case 4: + color = RGB(0, 255, 255); break; + case 5: + color = RGB(0, 0, 255); break; + case 6: + color = RGB(255, 0, 255); break; + case 7: + color = RGB(0, 0, 0); break; + } + SetTextColor(hdc, color); + + // draw center text + for (int slot = 0; slot < SCREEN_MESSAGE_SLOTS; slot++) + { + if (theApp.screenMessage[slot]) + { + if ((theApp.screenMessageDuration[slot] < 0 || + (int)(GetTickCount() - theApp.screenMessageTime[slot]) < theApp.screenMessageDuration[slot]) && + (!theApp.disableStatusMessage || slot == 1 || slot == 2)) + { + TextOut(hdc, theApp.dest.left+10, theApp.dest.bottom - 20*(slot+1), theApp.screenMessageBuffer[slot], + strlen(theApp.screenMessageBuffer[slot])); + } + } + } + } + + if (textMessageStarted) + { + ddsPrimary->ReleaseDC(hdc); + } + } + + if (hret != DD_OK) + { + if (theApp.ddrawDebug) + winlog("Error on update screen: %08x\n", hret); + } +} + +int DirectDrawDisplay::selectFullScreenMode(GUID **pGUID) +{ + return winVideoModeSelect(theApp.m_pMainWnd, pGUID); +} + +IDisplay *newDirectDrawDisplay() +{ + return new DirectDrawDisplay(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/DirectInput.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/DirectInput.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1242 @@ +//#define USE_GETASYNCKEYSTATE_FOR_KEYBOARD + +#include "stdafx.h" + +#define DIRECTINPUT_VERSION 0x0500 +#include "dinput.h" + +#include "resource.h" +#include "Input.h" +#include "Reg.h" +#include "WinResUtil.h" + +// master keyboard translation table +static const struct { + int dik; + int vk; + int ascii; +} win_key_trans_table[] = { + // dinput key virtual key ascii + { DIK_ESCAPE, VK_ESCAPE, 27 }, + { DIK_1, '1', '1' }, + { DIK_2, '2', '2' }, + { DIK_3, '3', '3' }, + { DIK_4, '4', '4' }, + { DIK_5, '5', '5' }, + { DIK_6, '6', '6' }, + { DIK_7, '7', '7' }, + { DIK_8, '8', '8' }, + { DIK_9, '9', '9' }, + { DIK_0, '0', '0' }, + { DIK_MINUS, VK_OEM_MINUS, '-' }, + { DIK_EQUALS, VK_OEM_PLUS, '=' }, + { DIK_BACK, VK_BACK, 8 }, + { DIK_TAB, VK_TAB, 9 }, + { DIK_Q, 'Q', 'Q' }, + { DIK_W, 'W', 'W' }, + { DIK_E, 'E', 'E' }, + { DIK_R, 'R', 'R' }, + { DIK_T, 'T', 'T' }, + { DIK_Y, 'Y', 'Y' }, + { DIK_U, 'U', 'U' }, + { DIK_I, 'I', 'I' }, + { DIK_O, 'O', 'O' }, + { DIK_P, 'P', 'P' }, + { DIK_LBRACKET, VK_OEM_4, '[' }, + { DIK_RBRACKET, VK_OEM_6, ']' }, + { DIK_RETURN, VK_RETURN, 13 }, + { DIK_LCONTROL, VK_LCONTROL, 0 }, + { DIK_A, 'A', 'A' }, + { DIK_S, 'S', 'S' }, + { DIK_D, 'D', 'D' }, + { DIK_F, 'F', 'F' }, + { DIK_G, 'G', 'G' }, + { DIK_H, 'H', 'H' }, + { DIK_J, 'J', 'J' }, + { DIK_K, 'K', 'K' }, + { DIK_L, 'L', 'L' }, + { DIK_SEMICOLON, VK_OEM_1, ';' }, + { DIK_APOSTROPHE, VK_OEM_7, '\'' }, + { DIK_GRAVE, VK_OEM_3, '`' }, + { DIK_LSHIFT, VK_LSHIFT, 0 }, + { DIK_BACKSLASH, VK_OEM_5, '\\' }, + { DIK_Z, 'Z', 'Z' }, + { DIK_X, 'X', 'X' }, + { DIK_C, 'C', 'C' }, + { DIK_V, 'V', 'V' }, + { DIK_B, 'B', 'B' }, + { DIK_N, 'N', 'N' }, + { DIK_M, 'M', 'M' }, + { DIK_COMMA, VK_OEM_COMMA, ',' }, + { DIK_PERIOD, VK_OEM_PERIOD, '.' }, + { DIK_SLASH, VK_OEM_2, '/' }, + { DIK_RSHIFT, VK_RSHIFT, 0 }, + { DIK_MULTIPLY, VK_MULTIPLY, '*' }, + { DIK_LMENU, VK_LMENU, 0 }, + { DIK_SPACE, VK_SPACE, ' ' }, + { DIK_CAPITAL, VK_CAPITAL, 0 }, + { DIK_F1, VK_F1, 0 }, + { DIK_F2, VK_F2, 0 }, + { DIK_F3, VK_F3, 0 }, + { DIK_F4, VK_F4, 0 }, + { DIK_F5, VK_F5, 0 }, + { DIK_F6, VK_F6, 0 }, + { DIK_F7, VK_F7, 0 }, + { DIK_F8, VK_F8, 0 }, + { DIK_F9, VK_F9, 0 }, + { DIK_F10, VK_F10, 0 }, + { DIK_NUMLOCK, VK_NUMLOCK, 0 }, + { DIK_SCROLL, VK_SCROLL, 0 }, + { DIK_NUMPAD7, VK_NUMPAD7, 0 }, + { DIK_NUMPAD8, VK_NUMPAD8, 0 }, + { DIK_NUMPAD9, VK_NUMPAD9, 0 }, + { DIK_SUBTRACT, VK_SUBTRACT, 0 }, + { DIK_NUMPAD4, VK_NUMPAD4, 0 }, + { DIK_NUMPAD5, VK_NUMPAD5, 0 }, + { DIK_NUMPAD6, VK_NUMPAD6, 0 }, + { DIK_ADD, VK_ADD, 0 }, + { DIK_NUMPAD1, VK_NUMPAD1, 0 }, + { DIK_NUMPAD2, VK_NUMPAD2, 0 }, + { DIK_NUMPAD3, VK_NUMPAD3, 0 }, + { DIK_NUMPAD0, VK_NUMPAD0, 0 }, + { DIK_DECIMAL, VK_DECIMAL, 0 }, + { DIK_F11, VK_F11, 0 }, + { DIK_F12, VK_F12, 0 }, + { DIK_F13, VK_F13, 0 }, + { DIK_F14, VK_F14, 0 }, + { DIK_F15, VK_F15, 0 }, + { DIK_NUMPADENTER, VK_RETURN, 0 }, + { DIK_RCONTROL, VK_RCONTROL, 0 }, + { DIK_DIVIDE, VK_DIVIDE, 0 }, + { DIK_SYSRQ, 0, 0 }, + { DIK_RMENU, VK_RMENU, 0 }, + { DIK_HOME, VK_HOME, 0 }, + { DIK_UP, VK_UP, 0 }, + { DIK_PRIOR, VK_PRIOR, 0 }, + { DIK_LEFT, VK_LEFT, 0 }, + { DIK_RIGHT, VK_RIGHT, 0 }, + { DIK_END, VK_END, 0 }, + { DIK_DOWN, VK_DOWN, 0 }, + { DIK_NEXT, VK_NEXT, 0 }, + { DIK_INSERT, VK_INSERT, 0 }, + { DIK_DELETE, VK_DELETE, 0 }, + { DIK_LWIN, VK_LWIN, 0 }, + { DIK_RWIN, VK_RWIN, 0 }, + { DIK_APPS, VK_APPS, 0 }, + { DIK_PAUSE, VK_PAUSE, 0 }, + { 0, VK_CANCEL, 0 }, + + // New keys introduced in Windows 2000. These have no MAME codes to + // preserve compatibility with old config files that may refer to them + // as e.g. FORWARD instead of e.g. KEYCODE_WEBFORWARD. They need table + // entries anyway because otherwise they aren't recognized when + // GetAsyncKeyState polling is used (as happens currently when MAME is + // paused). Some codes are missing because the mapping to vkey codes + // isn't clear, and MapVirtualKey is no help. + + { DIK_MUTE, VK_VOLUME_MUTE, 0 }, + { DIK_VOLUMEDOWN, VK_VOLUME_DOWN, 0 }, + { DIK_VOLUMEUP, VK_VOLUME_UP, 0 }, + { DIK_WEBHOME, VK_BROWSER_HOME, 0 }, + { DIK_WEBSEARCH, VK_BROWSER_SEARCH, 0 }, + { DIK_WEBFAVORITES, VK_BROWSER_FAVORITES, 0 }, + { DIK_WEBREFRESH, VK_BROWSER_REFRESH, 0 }, + { DIK_WEBSTOP, VK_BROWSER_STOP, 0 }, + { DIK_WEBFORWARD, VK_BROWSER_FORWARD, 0 }, + { DIK_WEBBACK, VK_BROWSER_BACK, 0 }, + { DIK_MAIL, VK_LAUNCH_MAIL, 0 }, + { DIK_MEDIASELECT, VK_LAUNCH_MEDIA_SELECT, 0 }, +}; + +extern void directXMessage(const char *); +extern void winlog(const char *msg, ...); + +#define POV_UP 1 +#define POV_DOWN 2 +#define POV_RIGHT 4 +#define POV_LEFT 8 + +class DirectInput : public Input +{ +private: + HINSTANCE dinputDLL; +public: + virtual void checkDevices(); + DirectInput(); + virtual ~DirectInput(); + + virtual bool initialize(); + virtual bool readDevices(); + virtual u32 readDevice(int which, bool sensor); + virtual CString getKeyName(LONG_PTR key); + virtual void checkKeys(); + virtual void activate(); + virtual void loadSettings(); + virtual void saveSettings(); +}; + +struct deviceInfo +{ + LPDIRECTINPUTDEVICE device; + BOOL isPolled; + int nButtons; + int nAxes; + int nPovs; + BOOL first; + struct + { + DWORD offset; + LONG center; + LONG negative; + LONG positive; + } axis[8]; + int needed; + union + { + UCHAR data[256]; + DIJOYSTATE state; + }; +}; + +static deviceInfo * currentDevice = NULL; +static int numDevices = 1; +static deviceInfo * pDevices = NULL; +static LPDIRECTINPUT pDirectInput = NULL; +static int joyDebug = 0; +static int axisNumber = 0; + +USHORT joypad[4][13] = { + { + DIK_LEFT, DIK_RIGHT, + DIK_UP, DIK_DOWN, + DIK_Z, DIK_X, + DIK_RETURN, DIK_BACK, + DIK_A, DIK_S, + DIK_SPACE, DIK_F12, + DIK_C + }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } +}; + +USHORT motion[4] = { + DIK_NUMPAD4, DIK_NUMPAD6, DIK_NUMPAD8, DIK_NUMPAD2 +}; + +static int winReadKey(char *name, int num) +{ + char buffer[80]; + + sprintf(buffer, "Joy%d_%s", num, name); + + return regQueryDwordValue(buffer, (DWORD)-1); +} + +void winReadKeys() +{ + int key = -1; + + for (int i = 0; i < 4; i++) + { + key = winReadKey("Left", i); + if (key != -1) + joypad[i][KEY_LEFT] = key; + key = winReadKey("Right", i); + if (key != -1) + joypad[i][KEY_RIGHT] = key; + key = winReadKey("Up", i); + if (key != -1) + joypad[i][KEY_UP] = key; + key = winReadKey("Down", i); + if (key != -1) + joypad[i][KEY_DOWN] = key; + key = winReadKey("A", i); + if (key != -1) + joypad[i][KEY_BUTTON_A] = key; + key = winReadKey("B", i); + if (key != -1) + joypad[i][KEY_BUTTON_B] = key; + key = winReadKey("L", i); + if (key != -1) + joypad[i][KEY_BUTTON_L] = key; + key = winReadKey("R", i); + if (key != -1) + joypad[i][KEY_BUTTON_R] = key; + key = winReadKey("Start", i); + if (key != -1) + joypad[i][KEY_BUTTON_START] = key; + key = winReadKey("Select", i); + if (key != -1) + joypad[i][KEY_BUTTON_SELECT] = key; + key = winReadKey("Speed", i); + if (key != -1) + joypad[i][KEY_BUTTON_SPEED] = key; + key = winReadKey("Capture", i); + if (key != -1) + joypad[i][KEY_BUTTON_CAPTURE] = key; + key = winReadKey("GS", i); + if (key != -1) + joypad[i][KEY_BUTTON_GS] = key; + } + key = regQueryDwordValue("Motion_Left", (DWORD)-1); + if (key != -1) + motion[KEY_LEFT] = key; + key = regQueryDwordValue("Motion_Right", (DWORD)-1); + if (key != -1) + motion[KEY_RIGHT] = key; + key = regQueryDwordValue("Motion_Up", (DWORD)-1); + if (key != -1) + motion[KEY_UP] = key; + key = regQueryDwordValue("Motion_Down", (DWORD)-1); + if (key != -1) + motion[KEY_DOWN] = key; +} + +static void winSaveKey(char *name, int num, USHORT value) +{ + char buffer[80]; + + sprintf(buffer, "Joy%d_%s", num, name); + + regSetDwordValue(buffer, value); +} + +void winSaveKeys() +{ + for (int i = 0; i < 4; i++) + { + winSaveKey("Left", i, joypad[i][KEY_LEFT]); + winSaveKey("Right", i, joypad[i][KEY_RIGHT]); + winSaveKey("Up", i, joypad[i][KEY_UP]); + winSaveKey("Speed", i, joypad[i][KEY_BUTTON_SPEED]); + winSaveKey("Capture", i, joypad[i][KEY_BUTTON_CAPTURE]); + winSaveKey("GS", i, joypad[i][KEY_BUTTON_GS]); + winSaveKey("Down", i, joypad[i][KEY_DOWN]); + winSaveKey("A", i, joypad[i][KEY_BUTTON_A]); + winSaveKey("B", i, joypad[i][KEY_BUTTON_B]); + winSaveKey("L", i, joypad[i][KEY_BUTTON_L]); + winSaveKey("R", i, joypad[i][KEY_BUTTON_R]); + winSaveKey("Start", i, joypad[i][KEY_BUTTON_START]); + winSaveKey("Select", i, joypad[i][KEY_BUTTON_SELECT]); + } + regSetDwordValue("joyVersion", 1); + + regSetDwordValue("Motion_Left", + motion[KEY_LEFT]); + regSetDwordValue("Motion_Right", + motion[KEY_RIGHT]); + regSetDwordValue("Motion_Up", + motion[KEY_UP]); + regSetDwordValue("Motion_Down", + motion[KEY_DOWN]); +} + +static BOOL CALLBACK EnumAxesCallback(const DIDEVICEOBJECTINSTANCE*pdidoi, + VOID*pContext) +{ + DIPROPRANGE diprg; + diprg.diph.dwSize = sizeof(DIPROPRANGE); + diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); + diprg.diph.dwHow = DIPH_BYOFFSET; + diprg.diph.dwObj = pdidoi->dwOfs; // Specify the enumerated axis + + diprg.lMin = -32768; + diprg.lMax = 32767; + // try to set the range + if (FAILED(currentDevice->device->SetProperty(DIPROP_RANGE, &diprg.diph))) + { + // Get the range for the axis + if (FAILED(currentDevice->device-> + GetProperty(DIPROP_RANGE, &diprg.diph))) + { + return DIENUM_STOP; + } + } + + DIPROPDWORD didz; + + didz.diph.dwSize = sizeof(didz); + didz.diph.dwHeaderSize = sizeof(DIPROPHEADER); + didz.diph.dwHow = DIPH_BYOFFSET; + didz.diph.dwObj = pdidoi->dwOfs; + + didz.dwData = 5000; + + currentDevice->device->SetProperty(DIPROP_DEADZONE, &didz.diph); + + LONG center = (diprg.lMin + diprg.lMax)/2; + LONG threshold = (diprg.lMax - center)/2; + + // only 8 axis supported + if (axisNumber < 8) + { + currentDevice->axis[axisNumber].center = center; + currentDevice->axis[axisNumber].negative = center - threshold; + currentDevice->axis[axisNumber].positive = center + threshold; + currentDevice->axis[axisNumber].offset = pdidoi->dwOfs; + } + axisNumber++; + return DIENUM_CONTINUE; +} + +static BOOL CALLBACK EnumPovsCallback(const DIDEVICEOBJECTINSTANCE*pdidoi, + VOID*pContext) +{ + return DIENUM_CONTINUE; +} + +static BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE pInst, + LPVOID lpvContext) +{ + ZeroMemory(&pDevices[numDevices], sizeof(deviceInfo)); + + HRESULT hRet = pDirectInput->CreateDevice(pInst->guidInstance, + &pDevices[numDevices].device, + NULL); + + if (hRet != DI_OK) + return DIENUM_STOP; + + DIDEVCAPS caps; + caps.dwSize = sizeof(DIDEVCAPS); + + hRet = pDevices[numDevices].device->GetCapabilities(&caps); + + if (hRet == DI_OK) + { + if (caps.dwFlags & DIDC_POLLEDDATAFORMAT || + caps.dwFlags & DIDC_POLLEDDEVICE) + pDevices[numDevices].isPolled = TRUE; + + pDevices[numDevices].nButtons = caps.dwButtons; + pDevices[numDevices].nAxes = caps.dwAxes; + pDevices[numDevices].nPovs = caps.dwPOVs; + + for (int i = 0; i < 6; i++) + { + pDevices[numDevices].axis[i].center = 0x8000; + pDevices[numDevices].axis[i].negative = 0x4000; + pDevices[numDevices].axis[i].positive = 0xc000; + } + } + else if (joyDebug) + winlog("Failed to get device capabilities %08x\n", hRet); + + if (joyDebug) + { + // don't translate. debug only + winlog("******************************\n"); + winlog("Joystick %2d name : %s\n", numDevices, pInst->tszProductName); + } + + numDevices++; + + return DIENUM_CONTINUE; +} + +BOOL CALLBACK DIEnumDevicesCallback2(LPCDIDEVICEINSTANCE pInst, + LPVOID lpvContext) +{ + numDevices++; + + return DIENUM_CONTINUE; +} + +static int getPovState(DWORD value) +{ + int state = 0; + if (LOWORD(value) != 0xFFFF) + { + if (value < 9000 || value > 27000) + state |= POV_UP; + if (value > 0 && value < 18000) + state |= POV_RIGHT; + if (value > 9000 && value < 27000) + state |= POV_DOWN; + if (value > 18000) + state |= POV_LEFT; + } + return state; +} + +static void checkKeys() +{ + LONG_PTR dev = 0; + int i; + + for (i = 0; i < numDevices; i++) + pDevices[i].needed = 0; + + for (i = 0; i < 4; i++) + { + dev = joypad[i][KEY_LEFT] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_LEFT] = DIK_LEFT; + + dev = joypad[i][KEY_RIGHT] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_RIGHT] = DIK_RIGHT; + + dev = joypad[i][KEY_UP] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_UP] = DIK_UP; + + dev = joypad[i][KEY_DOWN] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_DOWN] = DIK_DOWN; + + dev = joypad[i][KEY_BUTTON_A] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_A] = DIK_Z; + + dev = joypad[i][KEY_BUTTON_B] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_B] = DIK_X; + + dev = joypad[i][KEY_BUTTON_L] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_L] = DIK_A; + + dev = joypad[i][KEY_BUTTON_R] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_R] = DIK_S; + + dev = joypad[i][KEY_BUTTON_START] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_START] = DIK_RETURN; + + dev = joypad[i][KEY_BUTTON_SELECT] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_SELECT] = DIK_BACK; + + dev = joypad[i][KEY_BUTTON_SPEED] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_SPEED] = DIK_SPACE; + + dev = joypad[i][KEY_BUTTON_CAPTURE] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_CAPTURE] = DIK_F12; + + dev = joypad[i][KEY_BUTTON_GS] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + joypad[i][KEY_BUTTON_GS] = DIK_C; + } + + dev = motion[KEY_UP] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_UP] = DIK_NUMPAD8; + + dev = motion[KEY_DOWN] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_DOWN] = DIK_NUMPAD2; + + dev = motion[KEY_LEFT] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_LEFT] = DIK_NUMPAD4; + + dev = motion[KEY_RIGHT] >> 8; + if (dev < numDevices && dev >= 0) + pDevices[dev].needed = 1; + else + motion[KEY_RIGHT] = DIK_NUMPAD6; +} + +#define KEYDOWN(buffer, key) (buffer[key] & 0x80) + +static bool IsKeyDownAsync (WORD KeyIdent) +{ + //if (KeyIdent == 0 || KeyIdent == VK_ESCAPE) // if it's the 'disabled' key, it's never pressed + // return false; + + //if (!GUI.BackgroundInput && GUI.hWnd != GetForegroundWindow()) + // return false; + + // the pause key is special, need this to catch all presses of it + // Both GetKeyState and GetAsyncKeyState cannot catch it anyway, + // so this should be handled in WM_KEYDOWN message. + if (KeyIdent == VK_PAUSE) + { + return false; +// if(GetAsyncKeyState(VK_PAUSE)) // not &'ing this with 0x8000 is intentional and necessary +// return true; + } + + if (KeyIdent == VK_CAPITAL || KeyIdent == VK_NUMLOCK || KeyIdent == VK_SCROLL) + return ((GetKeyState(KeyIdent) & 0x01) != 0); + else + return ((GetAsyncKeyState(KeyIdent) & 0x8000) != 0); + //return ((GetKeyState (KeyIdent) & 0x80) != 0); +} + +static bool readKeyboard() +{ +#ifndef USE_GETASYNCKEYSTATE_FOR_KEYBOARD + if (pDevices[0].needed) + { + HRESULT hret = pDevices[0].device-> + GetDeviceState(256, + (LPVOID)pDevices[0].data); + + if (hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) + { + hret = pDevices[0].device->Acquire(); + if (hret != DI_OK) + return false; + hret = pDevices[0].device->GetDeviceState(256, (LPVOID)pDevices[0].data); + } + + return hret == DI_OK; + } +#else + for (int i = 0; i < sizeof(win_key_trans_table)/sizeof(win_key_trans_table[0]); i++) { + pDevices[0].data[win_key_trans_table[i].dik] = IsKeyDownAsync(win_key_trans_table[i].vk) ? 0x80 : 0; + } +#endif + return true; +} + +static bool readJoystick(int joy) +{ + if (pDevices[joy].needed) + { + if (pDevices[joy].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[joy].device)->Poll(); + + HRESULT hret = pDevices[joy].device-> + GetDeviceState(sizeof(DIJOYSTATE), + (LPVOID)&pDevices[joy].state); + + if (hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) + { + hret = pDevices[joy].device->Acquire(); + + if (hret == DI_OK) + { + if (pDevices[joy].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[joy].device)->Poll(); + + hret = pDevices[joy].device-> + GetDeviceState(sizeof(DIJOYSTATE), + (LPVOID)&pDevices[joy].state); + } + } + + return hret == DI_OK; + } + + return true; +} + +static void checkKeyboard() +{ + // mham fix. Patch #1378104 + UCHAR keystate[256]; + HRESULT hret = pDevices[0].device->Acquire(); + + if (pDevices[0].first) + { + pDevices[0].device->GetDeviceState(256, (LPVOID)pDevices[0].data); + pDevices[0].first = FALSE; + return; + } + + hret = pDevices[0].device-> + GetDeviceState(256, (LPVOID)keystate); + + if (hret == DIERR_INPUTLOST || hret == DIERR_NOTACQUIRED) + { + return; + } + + if (hret == DI_OK) + { + for (int i = 0; i < 256; i++) + { + if (keystate[i] == pDevices[0].data[i]) + continue; + if (KEYDOWN(keystate, i)) + { + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, 0, i); + break; + } + } + } + memcpy(pDevices[0].data, keystate, sizeof(UCHAR) * 256); +} + +static void checkJoypads() +{ + DIDEVICEOBJECTINSTANCE di; + + ZeroMemory(&di, sizeof(DIDEVICEOBJECTINSTANCE)); + + di.dwSize = sizeof(DIDEVICEOBJECTINSTANCE); + + int i = 0; + + DIJOYSTATE joystick; + + for (i = 1; i < numDevices; i++) + { + HRESULT hret = pDevices[i].device->Acquire(); + + if (pDevices[i].isPolled) + ((LPDIRECTINPUTDEVICE2)pDevices[i].device)->Poll(); + + hret = pDevices[i].device->GetDeviceState(sizeof(joystick), &joystick); + + int j; + + if (pDevices[i].first) + { + memcpy(&pDevices[i].state, &joystick, sizeof(joystick)); + pDevices[i].first = FALSE; + continue; + } + + for (j = 0; j < pDevices[i].nButtons; j++) + { + if (((pDevices[i].state.rgbButtons[j] ^ joystick.rgbButtons[j]) + & joystick.rgbButtons[j]) & 0x80) + { + HWND focus = GetFocus(); + + SendMessage(focus, JOYCONFIG_MESSAGE, i, j+128); + } + } + + for (j = 0; j < pDevices[i].nAxes && j < 8; j++) + { + LONG value = pDevices[i].axis[j].center; + LONG old = 0; + switch (pDevices[i].axis[j].offset) + { + case DIJOFS_X: + value = joystick.lX; + old = pDevices[i].state.lX; + break; + case DIJOFS_Y: + value = joystick.lY; + old = pDevices[i].state.lY; + break; + case DIJOFS_Z: + value = joystick.lZ; + old = pDevices[i].state.lZ; + break; + case DIJOFS_RX: + value = joystick.lRx; + old = pDevices[i].state.lRx; + break; + case DIJOFS_RY: + value = joystick.lRy; + old = pDevices[i].state.lRy; + break; + case DIJOFS_RZ: + value = joystick.lRz; + old = pDevices[i].state.lRz; + break; + case DIJOFS_SLIDER(0): + value = joystick.rglSlider[0]; + old = pDevices[i].state.rglSlider[0]; + break; + case DIJOFS_SLIDER(1): + value = joystick.rglSlider[1]; + old = pDevices[i].state.rglSlider[1]; + break; + } + if (value != old) + { + if (value < pDevices[i].axis[j].negative) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<1)); + else if (value > pDevices[i].axis[j].positive) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<1)+1); + } + } + + for (j = 0; j < 4 && j < pDevices[i].nPovs; j++) + { + if (LOWORD(pDevices[i].state.rgdwPOV[j]) != LOWORD(joystick.rgdwPOV[j])) + { + int state = getPovState(joystick.rgdwPOV[j]); + + if (state & POV_UP) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x20); + else if (state & POV_DOWN) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x21); + else if (state & POV_RIGHT) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x22); + else if (state & POV_LEFT) + SendMessage(GetFocus(), JOYCONFIG_MESSAGE, i, (j<<2)+0x23); + } + } + + memcpy(&pDevices[i].state, &joystick, sizeof(joystick)); + } +} + +BOOL checkKey(LONG_PTR key) +{ + LONG_PTR dev = (key >> 8); + + LONG_PTR k = (key & 255); + + if (dev == 0) + { + return KEYDOWN(pDevices[0].data, k); + } + else if (dev >= numDevices) + { + return FALSE; + } + else + { + if (k < 16) + { + LONG_PTR axis = k >> 1; + LONG value = pDevices[dev].axis[axis].center; + switch (pDevices[dev].axis[axis].offset) + { + case DIJOFS_X: + value = pDevices[dev].state.lX; + break; + case DIJOFS_Y: + value = pDevices[dev].state.lY; + break; + case DIJOFS_Z: + value = pDevices[dev].state.lZ; + break; + case DIJOFS_RX: + value = pDevices[dev].state.lRx; + break; + case DIJOFS_RY: + value = pDevices[dev].state.lRy; + break; + case DIJOFS_RZ: + value = pDevices[dev].state.lRz; + break; + case DIJOFS_SLIDER(0): + value = pDevices[dev].state.rglSlider[0]; + break; + case DIJOFS_SLIDER(1): + value = pDevices[dev].state.rglSlider[1]; + break; + } + + if (k & 1) + return value > pDevices[dev].axis[axis].positive; + return value < pDevices[dev].axis[axis].negative; + } + else if (k < 48) + { + LONG_PTR hat = (k >> 2) & 3; + int state = getPovState(pDevices[dev].state.rgdwPOV[hat]); + BOOL res = FALSE; + switch (k & 3) + { + case 0: + res = state & POV_UP; + break; + case 1: + res = state & POV_DOWN; + break; + case 2: + res = state & POV_RIGHT; + break; + case 3: + res = state & POV_LEFT; + break; + } + return res; + } + else if (k >= 128) + { + return pDevices[dev].state.rgbButtons[k-128] & 0x80; + } + } + + return FALSE; +} + +DirectInput::DirectInput() +{ + dinputDLL = NULL; +} + +DirectInput::~DirectInput() +{ + saveSettings(); + if (pDirectInput != NULL) + { + if (pDevices) + { + for (int i = 0; i < numDevices; i++) + { + if (pDevices[i].device) + { + pDevices[i].device->Unacquire(); + pDevices[i].device->Release(); + pDevices[i].device = NULL; + } + } + free(pDevices); + pDevices = NULL; + } + + pDirectInput->Release(); + pDirectInput = NULL; + } + + if (dinputDLL) + { + /**/ ::FreeLibrary(dinputDLL); + dinputDLL = NULL; + } +} + +bool DirectInput::initialize() +{ + joyDebug = GetPrivateProfileInt("config", + "joyDebug", + 0, + "VBA.ini"); + dinputDLL = /**/ ::LoadLibrary("DINPUT.DLL"); + HRESULT (WINAPI *DInputCreate)(HINSTANCE, DWORD, LPDIRECTINPUT *, IUnknown *); + if (dinputDLL != NULL) + { + DInputCreate = (HRESULT (WINAPI *)(HINSTANCE, DWORD, LPDIRECTINPUT *, IUnknown *)) + GetProcAddress(dinputDLL, "DirectInputCreateA"); + + if (DInputCreate == NULL) + { + directXMessage("DirectInputCreateA"); + return false; + } + } + else + { + directXMessage("DINPUT.DLL"); + return false; + } + + HRESULT hret = DInputCreate(AfxGetInstanceHandle(), + DIRECTINPUT_VERSION, + &pDirectInput, + NULL); + if (hret != DI_OK) + { + // errorMessage(myLoadString(IDS_ERROR_DISP_CREATE), hret); + return false; + } + + hret = pDirectInput->EnumDevices(DIDEVTYPE_JOYSTICK, + DIEnumDevicesCallback2, + NULL, + DIEDFL_ATTACHEDONLY); + + pDevices = (deviceInfo *)calloc(numDevices, sizeof(deviceInfo)); + + hret = pDirectInput->CreateDevice(GUID_SysKeyboard, &pDevices[0].device, NULL); + pDevices[0].isPolled = false; + pDevices[0].needed = true; + pDevices[0].first = true; + + if (hret != DI_OK) + { + // errorMessage(myLoadString(IDS_ERROR_DISP_CREATEDEVICE), hret); + return false; + } + + numDevices = 1; + + hret = pDirectInput->EnumDevices(DIDEVTYPE_JOYSTICK, + DIEnumDevicesCallback, + NULL, + DIEDFL_ATTACHEDONLY); + + // hret = pDevices[0].device->SetCooperativeLevel(hWindow, + // DISCL_FOREGROUND| + // DISCL_NONEXCLUSIVE); + + if (hret != DI_OK) + { + // errorMessage(myLoadString(IDS_ERROR_DISP_LEVEL), hret); + return false; + } + + hret = pDevices[0].device->SetDataFormat(&c_dfDIKeyboard); + + if (hret != DI_OK) + { + // errorMessage(myLoadString(IDS_ERROR_DISP_DATAFORMAT), hret); + return false; + } + + for (int i = 1; i < numDevices; i++) + { + pDevices[i].device->SetDataFormat(&c_dfDIJoystick); + pDevices[i].needed = false; + pDevices[i].first = true; + currentDevice = &pDevices[i]; + axisNumber = 0; + currentDevice->device->EnumObjects(EnumAxesCallback, NULL, DIDFT_AXIS); + currentDevice->device->EnumObjects(EnumPovsCallback, NULL, DIDFT_POV); + if (joyDebug) + { + // don't translate. debug only + winlog("Joystick %2d polled : %d\n", i, currentDevice->isPolled); + winlog("Joystick %2d buttons : %d\n", i, currentDevice->nButtons); + winlog("Joystick %2d povs : %d\n", i, currentDevice->nPovs); + winlog("Joystick %2d axes : %d\n", i, currentDevice->nAxes); + for (int j = 0; j < currentDevice->nAxes; j++) + { + winlog("Axis %2d offset : %08lx\n", j, currentDevice->axis[j]. + offset); + winlog("Axis %2d center : %08lx\n", j, currentDevice->axis[j]. + center); + winlog("Axis %2d negative : %08lx\n", j, currentDevice->axis[j]. + negative); + winlog("Axis %2d positive : %08lx\n", j, currentDevice->axis[j]. + positive); + } + } + + currentDevice = NULL; + } + + for (int i = 0; i < numDevices; i++) + pDevices[i].device->Acquire(); + + return true; +} + +bool DirectInput::readDevices() +{ + bool ok = true; + for (int i = 0; i < numDevices; i++) + { + if (pDevices[i].needed) + { + ok = (i > 0 ? readJoystick(i) : readKeyboard()) || ok; + } + } + return ok; +} + +bool inputActive = true; // used to disable all input when the window is inactive + +u32 DirectInput::readDevice(int i, bool sensor) +{ + // this old hack is evil + extern int systemGetDefaultJoypad(); + extern int32 gbSgbMode, gbSgbMultiplayer; + if (!(gbSgbMode && gbSgbMultiplayer)) + i = systemGetDefaultJoypad(); + + u32 res = 0; + + // manual input + if (inputActive) + { + if (checkKey(joypad[i][KEY_BUTTON_A])) + res |= BUTTON_MASK_A; + if (checkKey(joypad[i][KEY_BUTTON_B])) + res |= BUTTON_MASK_B; + if (checkKey(joypad[i][KEY_BUTTON_SELECT])) + res |= BUTTON_MASK_SELECT; + if (checkKey(joypad[i][KEY_BUTTON_START])) + res |= BUTTON_MASK_START; + if (checkKey(joypad[i][KEY_RIGHT])) + res |= BUTTON_MASK_RIGHT; + if (checkKey(joypad[i][KEY_LEFT])) + res |= BUTTON_MASK_LEFT; + if (checkKey(joypad[i][KEY_UP])) + res |= BUTTON_MASK_UP; + if (checkKey(joypad[i][KEY_DOWN])) + res |= BUTTON_MASK_DOWN; + if (checkKey(joypad[i][KEY_BUTTON_R])) + res |= BUTTON_MASK_R; + if (checkKey(joypad[i][KEY_BUTTON_L])) + res |= BUTTON_MASK_L; + + // unused + if (checkKey(motion[KEY_LEFT])) + res |= BUTTON_MASK_LEFT_MOTION; + else if (checkKey(motion[KEY_RIGHT])) + res |= BUTTON_MASK_RIGHT_MOTION; + if (checkKey(motion[KEY_UP])) + res |= BUTTON_MASK_UP_MOTION; + else if (checkKey(motion[KEY_DOWN])) + res |= BUTTON_MASK_DOWN_MOTION; + } + + u32 hackedButtons = 0; + if (inputActive) + { + // the "non-button" buttons (what a hack!) + if (checkKey(joypad[i][KEY_BUTTON_SPEED])) + hackedButtons |= BUTTON_MASK_SPEED; + if (checkKey(joypad[i][KEY_BUTTON_CAPTURE])) + hackedButtons |= BUTTON_MASK_CAPTURE; + if (checkKey(joypad[i][KEY_BUTTON_GS])) + hackedButtons |= BUTTON_MASK_GAMESHARK; + } + + extern bool systemIsSpedUp(); + if (systemIsSpedUp()) + hackedButtons |= BUTTON_MASK_SPEED; + + return res | hackedButtons; +} + +CString DirectInput::getKeyName(LONG_PTR key) +{ + LONG_PTR d = (key >> 8); + LONG_PTR k = key & 255; + + DIDEVICEOBJECTINSTANCE di; + + ZeroMemory(&di, sizeof(DIDEVICEOBJECTINSTANCE)); + + di.dwSize = sizeof(DIDEVICEOBJECTINSTANCE); + + CString winBuffer = winResLoadString(IDS_ERROR); + + if (d == 0) + { + pDevices[0].device->GetObjectInfo(&di, (DWORD)key, DIPH_BYOFFSET); + winBuffer = di.tszName; + } + else if (d < numDevices) + { + if (k < 16) + { + if (k < 4) + { + switch (k) + { + case 0: + winBuffer.Format(winResLoadString(IDS_JOY_LEFT), d); + break; + case 1: + winBuffer.Format(winResLoadString(IDS_JOY_RIGHT), d); + break; + case 2: + winBuffer.Format(winResLoadString(IDS_JOY_UP), d); + break; + case 3: + winBuffer.Format(winResLoadString(IDS_JOY_DOWN), d); + break; + } + } + else + { + pDevices[d].device->GetObjectInfo(&di, + pDevices[d].axis[k>>1].offset, + DIPH_BYOFFSET); + if (k & 1) + winBuffer.Format("Joy %d %s +", d, di.tszName); + else + winBuffer.Format("Joy %d %s -", d, di.tszName); + } + } + else if (k < 48) + { + LONG_PTR hat = (k >> 2) & 3; + pDevices[d].device->GetObjectInfo(&di, + (DWORD)DIJOFS_POV(hat), + DIPH_BYOFFSET); + char * dir = "up"; + LONG_PTR dd = k & 3; + if (dd == 1) + dir = "down"; + else if (dd == 2) + dir = "right"; + else if (dd == 3) + dir = "left"; + winBuffer.Format("Joy %d %s %s", d, di.tszName, dir); + } + else + { + pDevices[d].device->GetObjectInfo(&di, + (DWORD)DIJOFS_BUTTON(k-128), + DIPH_BYOFFSET); + winBuffer.Format(winResLoadString(IDS_JOY_BUTTON), d, di.tszName); + } + } + else + { + // Joystick isn't plugged in. We can't decipher k, so just show its value. + winBuffer.Format("Joy %d (%d)", d, k); + } + + return winBuffer; +} + +void DirectInput::checkKeys() +{ + ::checkKeys(); +} + +Input *newDirectInput() +{ + return new DirectInput; +} + +void DirectInput::checkDevices() +{ + checkJoypads(); + checkKeyboard(); +} + +void DirectInput::activate() +{ + for (int i = 0; i < numDevices; i++) + { + if (pDevices != NULL && pDevices[i].device != NULL) + pDevices[i].device->Acquire(); + } +} + +void DirectInput::loadSettings() +{ + winReadKeys(); +} + +void DirectInput::saveSettings() +{ + winSaveKeys(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/DirectSound.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/DirectSound.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,534 @@ +#include "stdafx.h" +#include +#include + +#include "resource.h" +#include "AVIWrite.h" +#include "Sound.h" +#include "WavWriter.h" +#include "VBA.h" + +#include "../gba/GBAGlobals.h" +#include "../gba/GBASound.h" +#include "../common/nesvideos-piece.h" + +extern void directXMessage(const char *); + +class DirectSound : public ISound +{ +private: + HINSTANCE dsoundDLL; + LPDIRECTSOUND pDirectSound; + LPDIRECTSOUNDBUFFER dsbPrimary; + LPDIRECTSOUNDBUFFER dsbSecondary; + LPDIRECTSOUNDNOTIFY dsbNotify; + HANDLE dsbEvent; + WAVEFORMATEX wfx; + float curRate; +public: + DirectSound(); + virtual ~DirectSound(); + + bool init(); + void pause(); + void reset(); + void resume(); + void write(); + void setSpeed(float rate); + bool isPlaying(); + void clearAudioBuffer(); +}; + +DirectSound::DirectSound() +{ + dsoundDLL = NULL; + pDirectSound = NULL; + dsbPrimary = NULL; + dsbSecondary = NULL; + dsbNotify = NULL; + dsbEvent = NULL; +} + +DirectSound::~DirectSound() +{ + if (theApp.aviRecorder != NULL) + { + delete theApp.aviRecorder; + theApp.aviRecorder = NULL; + theApp.aviRecording = false; + } + + if (theApp.soundRecording) + { + if (theApp.soundRecorder != NULL) + { + delete theApp.soundRecorder; + theApp.soundRecorder = NULL; + } + theApp.soundRecording = false; + } + + if (dsbNotify != NULL) + { + dsbNotify->Release(); + dsbNotify = NULL; + } + + if (dsbEvent != NULL) + { + CloseHandle(dsbEvent); + dsbEvent = NULL; + } + + if (pDirectSound != NULL) + { + if (dsbPrimary != NULL) + { + dsbPrimary->Release(); + dsbPrimary = NULL; + } + + if (dsbSecondary != NULL) + { + dsbSecondary->Release(); + dsbSecondary = NULL; + } + + pDirectSound->Release(); + pDirectSound = NULL; + } + + if (dsoundDLL != NULL) + { + FreeLibrary(dsoundDLL); + dsoundDLL = NULL; + } +} + +bool DirectSound::init() +{ + HRESULT hr; + + dsoundDLL = LoadLibrary("DSOUND.DLL"); + HRESULT (WINAPI *DSoundCreate)(LPCGUID, LPDIRECTSOUND *, IUnknown *); + if (dsoundDLL != NULL) + { + DSoundCreate = (HRESULT (WINAPI *)(LPCGUID, LPDIRECTSOUND *, IUnknown *)) + GetProcAddress(dsoundDLL, "DirectSoundCreate"); + + if (DSoundCreate == NULL) + { + directXMessage("DirectSoundCreate"); + return false; + } + } + else + { + directXMessage("DSOUND.DLL"); + return false; + } + + if (FAILED(hr = DSoundCreate(NULL, &pDirectSound, NULL))) + { + // errorMessage(myLoadString(IDS_ERROR_SOUND_CREATE), hr); + systemMessage(IDS_CANNOT_CREATE_DIRECTSOUND, + "Cannot create DirectSound %08x", hr); + pDirectSound = NULL; + dsbSecondary = NULL; + return false; + } + + if (FAILED(hr = pDirectSound->SetCooperativeLevel((HWND)*theApp.m_pMainWnd, DSSCL_EXCLUSIVE))) + { + // errorMessage(myLoadString(IDS_ERROR_SOUND_LEVEL), hr); + systemMessage(IDS_CANNOT_SETCOOPERATIVELEVEL, + "Cannot SetCooperativeLevel %08x", hr); + return false; + } + + DSBUFFERDESC dsbdesc; + ZeroMemory(&dsbdesc, sizeof(DSBUFFERDESC)); + dsbdesc.dwSize = sizeof(DSBUFFERDESC); + dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER; + + if (FAILED(hr = pDirectSound->CreateSoundBuffer(&dsbdesc, &dsbPrimary, NULL))) + { + // errorMessage(myLoadString(IDS_ERROR_SOUND_BUFFER),hr); + systemMessage(IDS_CANNOT_CREATESOUNDBUFFER, + "Cannot CreateSoundBuffer %08x", hr); + return false; + } + + // Set primary buffer format + + memset(&wfx, 0, sizeof(WAVEFORMATEX)); + wfx.wFormatTag = WAVE_FORMAT_PCM; + wfx.nChannels = 2; + switch (soundQuality) + { + case 2: + wfx.nSamplesPerSec = 22050; + soundBufferLen = 736 * 2; + soundBufferTotalLen = 7360 * 2; + break; + case 4: + wfx.nSamplesPerSec = 11025; + soundBufferLen = 368 * 2; + soundBufferTotalLen = 3680 * 2; + break; + default: + soundQuality = 1; + wfx.nSamplesPerSec = 44100; + soundBufferLen = 1470 * 2; + soundBufferTotalLen = 14700 * 2; + } + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = (wfx.wBitsPerSample / 8) * wfx.nChannels; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + + if (FAILED(hr = dsbPrimary->SetFormat(&wfx))) + { + // errorMessage(myLoadString(IDS_ERROR_SOUND_PRIMARY),hr); + systemMessage(IDS_CANNOT_SETFORMAT_PRIMARY, + "Cannot SetFormat for primary %08x", hr); + return false; + } + + ZeroMemory(&dsbdesc, sizeof(DSBUFFERDESC)); + dsbdesc.dwSize = sizeof(DSBUFFERDESC); + dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_CTRLFREQUENCY | DSBCAPS_GLOBALFOCUS; + dsbdesc.dwBufferBytes = soundBufferTotalLen; + dsbdesc.lpwfxFormat = &wfx; + + if (FAILED(hr = pDirectSound->CreateSoundBuffer(&dsbdesc, &dsbSecondary, NULL))) + { + bool ok = false; + while (dsbdesc.dwFlags != DSBCAPS_GETCURRENTPOSITION2) + { + if (dsbdesc.dwFlags & DSBCAPS_CTRLFREQUENCY) + dsbdesc.dwFlags ^= DSBCAPS_CTRLFREQUENCY; + else if (dsbdesc.dwFlags & DSBCAPS_GLOBALFOCUS) + dsbdesc.dwFlags ^= DSBCAPS_GLOBALFOCUS; + else if (dsbdesc.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY) + dsbdesc.dwFlags ^= DSBCAPS_CTRLPOSITIONNOTIFY; + if (SUCCEEDED(hr = pDirectSound->CreateSoundBuffer(&dsbdesc, &dsbSecondary, NULL))) + { + ok = true; + break; + } + } + if (!ok) + { + systemMessage(IDS_CANNOT_CREATESOUNDBUFFER_SEC, "Cannot CreateSoundBuffer secondary %08x", hr); + return false; + } + + dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2; + } + + dsbSecondary->SetCurrentPosition(0); + + if (!theApp.useOldSync) + { + hr = dsbSecondary->QueryInterface(IID_IDirectSoundNotify, + (void * *)&dsbNotify); + if (!FAILED(hr)) + { + dsbEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + + DSBPOSITIONNOTIFY notify[10]; + + for (int i = 0; i < 10; i++) + { + notify[i].dwOffset = i * soundBufferLen; + notify[i].hEventNotify = dsbEvent; + } + if (FAILED(dsbNotify->SetNotificationPositions(10, notify))) + { + dsbNotify->Release(); + dsbNotify = NULL; + CloseHandle(dsbEvent); + dsbEvent = NULL; + } + } + } + + hr = dsbPrimary->Play(0, 0, DSBPLAY_LOOPING); + + if (FAILED(hr)) + { + // errorMessage(myLoadString(IDS_ERROR_SOUND_PLAYPRIM), hr); + systemMessage(IDS_CANNOT_PLAY_PRIMARY, "Cannot Play primary %08x", hr); + return false; + } + + systemSoundOn = true; + + return true; +} + +void DirectSound::setSpeed(float rate) +{ + if (dsbSecondary == NULL || wfx.nSamplesPerSec <= 0) + return; + + if (rate != curRate) + { + curRate = rate; + + if (rate > 4.0f) + rate = 4.0f; + if (rate < 0.06f) + rate = 0.06f; + + dsbSecondary->SetFrequency((DWORD)((float)wfx.nSamplesPerSec * rate)); + } +} + +void DirectSound::pause() +{ + if (dsbSecondary != NULL) + { + DWORD status = 0; + dsbSecondary->GetStatus(&status); + + if (status & DSBSTATUS_PLAYING) + { + //systemScreenMessage("sound stopped (pause)!", 3); + dsbSecondary->Stop(); + } + } +} + +bool DirectSound::isPlaying() +{ + if (dsbSecondary != NULL) + { + DWORD status = 0; + dsbSecondary->GetStatus(&status); + + if (status & DSBSTATUS_PLAYING) + { + return true; + } + } + return false; +} + +void DirectSound::reset() +{ + if (dsbSecondary) + { + //systemScreenMessage("sound stopped (reset)!", 3); + dsbSecondary->Stop(); + dsbSecondary->SetCurrentPosition(0); + } +} + +void DirectSound::resume() +{ + if (dsbSecondary != NULL) + { + dsbSecondary->Play(0, 0, DSBPLAY_LOOPING); + } +} + +long linearFrameCount = 0; +long linearSoundByteCount = 0; +long linearSoundFrameCount = 0; + +void DirectSound::write() +{ + int len = soundBufferLen; + LPVOID lpvPtr1; + DWORD dwBytes1; + LPVOID lpvPtr2; + DWORD dwBytes2; + + do + { + linearSoundByteCount += len; + if (wfx.nAvgBytesPerSec) + linearSoundFrameCount = 60 * linearSoundByteCount / wfx.nAvgBytesPerSec; + + if (pDirectSound != NULL) + { + if (theApp.soundRecording) + { + if (dsbSecondary) + { + if (theApp.soundRecorder == NULL) + { + theApp.soundRecorder = new WavWriter; + WAVEFORMATEX format; + dsbSecondary->GetFormat(&format, sizeof(format), NULL); + if (theApp.soundRecorder->Open(theApp.soundRecordName)) + theApp.soundRecorder->SetFormat(&format); + } + } + + if (theApp.soundRecorder) + { + theApp.soundRecorder->AddSound((u8 *)soundFinalWave, len); + } + } + + if (theApp.nvAudioLog) + { + NESVideoLoggingAudio((u8 *)soundFinalWave, wfx.nSamplesPerSec, wfx.wBitsPerSample, wfx.nChannels, len / + (wfx.nChannels * (wfx.wBitsPerSample / 8))); + } + + // alternate avi record routine has been added in VBA.cpp + if (!theApp.altAviRecordMethod && theApp.aviRecording) + { + if (theApp.aviRecorder && !theApp.aviRecorder->IsPaused()) + { + if (dsbSecondary) + { + if (!theApp.aviRecorder->IsSoundAdded()) + { + WAVEFORMATEX format; + dsbSecondary->GetFormat(&format, sizeof(format), NULL); + theApp.aviRecorder->SetSoundFormat(&format); + } + } + + theApp.aviRecorder->AddSound((u8 *)soundFinalWave, len); + } + } + } + } + while (linearSoundFrameCount <= linearFrameCount); + + // arbitrarily wrap counters at 10000 frames to avoid mismatching wrap-around freeze + if (linearSoundFrameCount > 10000 && linearFrameCount > 10000) + { + linearFrameCount -= 10000; + linearSoundByteCount -= wfx.nAvgBytesPerSec * 10000 / 60; + linearSoundFrameCount = 60 * linearSoundByteCount / wfx.nAvgBytesPerSec; + } + + if (!pDirectSound) + return; + + HRESULT hr; + + bool fastForward = speedup; +#if (defined(WIN32) && !defined(SDL)) + fastForward |= theApp.frameSearchSkipping; +#endif + + // slows down emulator to match up with the sound speed + if (!fastForward && synchronize && !(theApp.throttle > 100 && theApp.accuratePitchThrottle) + && theApp.throttle >= 6 && theApp.throttle <= 400) + { + DWORD status = 0; + hr = dsbSecondary->GetStatus(&status); + if (status & DSBSTATUS_PLAYING) + { + if (!soundPaused) + { + DWORD play; + while (true) + { + dsbSecondary->GetCurrentPosition(&play, NULL); + + if (soundNextPosition + soundBufferLen < soundBufferTotalLen) + { + if (play < soundNextPosition + || play > soundNextPosition + soundBufferLen) + break; + } + else + { + if (play < soundNextPosition + && play > (soundNextPosition + soundBufferLen) % soundBufferTotalLen) + break; + } + + if (dsbEvent) + { + WaitForSingleObject(dsbEvent, 50); + } + } + } + } + else + { + soundPaused = 1; + } + } + + // Obtain memory address of write block. This will be in two parts + // if the block wraps around. + hr = dsbSecondary->Lock(soundNextPosition, soundBufferLen, + &lpvPtr1, &dwBytes1, &lpvPtr2, &dwBytes2, + 0); + + if (FAILED(hr)) + { + char str [256]; + sprintf(str, "Locking secondary failed with %d", hr); + systemScreenMessage(str); + } + + // If DSERR_BUFFERLOST is returned, restore and retry lock. + if (DSERR_BUFFERLOST == hr) + { + dsbSecondary->Restore(); + hr = dsbSecondary->Lock(soundNextPosition, soundBufferLen, + &lpvPtr1, &dwBytes1, &lpvPtr2, &dwBytes2, + 0); + } + + if (SUCCEEDED(hr)) + { + if (theApp.muteFrameAdvance && theApp.winPauseNextFrame || theApp.winMuteForNow) + { + // Write 0 to pointers. + if (NULL != lpvPtr1) + ZeroMemory(lpvPtr1, dwBytes1); + if (NULL != lpvPtr2) + ZeroMemory(lpvPtr2, dwBytes2); + } + else + { + // Write to pointers. + if (NULL != lpvPtr1) + CopyMemory(lpvPtr1, soundFinalWave, dwBytes1); + if (NULL != lpvPtr2) + CopyMemory(lpvPtr2, soundFinalWave + dwBytes1, dwBytes2); + } + + // Release the data back to DirectSound. + hr = dsbSecondary->Unlock(lpvPtr1, dwBytes1, lpvPtr2, + dwBytes2); + } + + soundNextPosition += soundBufferLen; + soundNextPosition %= soundBufferTotalLen; +} + +void DirectSound::clearAudioBuffer() +{ + LPVOID lpvPtr1; + DWORD dwBytes1; + LPVOID lpvPtr2; + DWORD dwBytes2; + HRESULT hr = dsbSecondary->Lock(0, soundBufferTotalLen, &lpvPtr1, &dwBytes1, &lpvPtr2, &dwBytes2, 0); + if (!FAILED(hr)) + { + if (lpvPtr1) + memset(lpvPtr1, 0, dwBytes1); + if (lpvPtr2) + memset(lpvPtr2, 0, dwBytes2); + hr = dsbSecondary->Unlock(lpvPtr1, dwBytes1, lpvPtr2, dwBytes2); + } +} + +ISound *newDirectSound() +{ + return new DirectSound(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Directories.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Directories.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,420 @@ +// Directories.cpp : implementation file +// + +#include "stdafx.h" +#include +#include "resource.h" +#include "Directories.h" +#include "Reg.h" +#include "WinMiscUtil.h" +#include "WinResUtil.h" + +///////////////////////////////////////////////////////////////////////////// +// Directories dialog + +static int CALLBACK browseCallbackProc(HWND hWnd, UINT msg, + LPARAM l, LPARAM data) +{ + char *buffer = (char *)data; + switch (msg) + { + case BFFM_INITIALIZED: + if (buffer[0]) + SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)buffer); + break; + default: + break; + } + return 0; +} + +Directories::Directories(CWnd*pParent /*=NULL*/) + : CDialog(Directories::IDD, pParent) +{ + //{{AFX_DATA_INIT(Directories) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void Directories::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Directories) + DDX_Control(pDX, IDC_ROM_PATH, m_romPath); + DDX_Control(pDX, IDC_GBXROM_PATH, m_gbxromPath); + DDX_Control(pDX, IDC_BATTERY_PATH, m_batteryPath); + DDX_Control(pDX, IDC_SAVE_PATH, m_savePath); + DDX_Control(pDX, IDC_MOVIE_PATH, m_moviePath); + DDX_Control(pDX, IDC_CHEAT_PATH, m_cheatPath); + DDX_Control(pDX, IDC_IPS_PATH, m_ipsPath); + DDX_Control(pDX, IDC_LUA_PATH, m_luaPath); + DDX_Control(pDX, IDC_AVI_PATH, m_aviPath); + DDX_Control(pDX, IDC_WAV_PATH, m_wavPath); + DDX_Control(pDX, IDC_CAPTURE_PATH, m_capturePath); + DDX_Control(pDX, IDC_WATCH_PATH, m_watchPath); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(Directories, CDialog) +//{{AFX_MSG_MAP(Directories) +ON_BN_CLICKED(IDC_ROM_DIR, OnRomDir) +ON_BN_CLICKED(IDC_ROM_DIR_RESET, OnRomDirReset) +ON_BN_CLICKED(IDC_GBXROM_DIR, OnGBxRomDir) +ON_BN_CLICKED(IDC_GBXROM_DIR_RESET, OnGBxRomDirReset) +ON_BN_CLICKED(IDC_BATTERY_DIR, OnBatteryDir) +ON_BN_CLICKED(IDC_BATTERY_DIR_RESET, OnBatteryDirReset) +ON_BN_CLICKED(IDC_SAVE_DIR, OnSaveDir) +ON_BN_CLICKED(IDC_SAVE_DIR_RESET, OnSaveDirReset) +ON_BN_CLICKED(IDC_MOVIE_DIR, OnMovieDir) +ON_BN_CLICKED(IDC_MOVIE_DIR_RESET, OnMovieDirReset) +ON_BN_CLICKED(IDC_CHEAT_DIR, OnCheatDir) +ON_BN_CLICKED(IDC_CHEAT_DIR_RESET, OnCheatDirReset) +ON_BN_CLICKED(IDC_IPS_DIR, OnIpsDir) +ON_BN_CLICKED(IDC_IPS_DIR_RESET, OnIpsDirReset) +ON_BN_CLICKED(IDC_LUA_DIR, OnLuaDir) +ON_BN_CLICKED(IDC_LUA_DIR_RESET, OnLuaDirReset) +ON_BN_CLICKED(IDC_AVI_DIR, OnAviDir) +ON_BN_CLICKED(IDC_AVI_DIR_RESET, OnAviDirReset) +ON_BN_CLICKED(IDC_WAV_DIR, OnWavDir) +ON_BN_CLICKED(IDC_WAV_DIR_RESET, OnWavDirReset) +ON_BN_CLICKED(IDC_CAPTURE_DIR, OnCaptureDir) +ON_BN_CLICKED(IDC_CAPTURE_DIR_RESET, OnCaptureDirReset) +ON_BN_CLICKED(IDC_WATCH_DIR, OnWatchDir) +ON_BN_CLICKED(IDC_WATCH_DIR_RESET, OnWatchDirReset) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// Directories message handlers + +BOOL Directories::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString p = regQueryStringValue(IDS_ROM_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_ROM_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_GBXROM_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_GBXROM_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_BATTERY_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_BATTERY_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_SAVE_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_SAVE_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_MOVIE_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_MOVIE_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_CHEAT_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_CHEAT_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_IPS_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_IPS_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_LUA_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_LUA_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_AVI_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_AVI_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_WAV_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_WAV_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_CAPTURE_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_CAPTURE_PATH)->SetWindowText(p); + + p = regQueryStringValue(IDS_WATCH_DIR, NULL); + if (!p.IsEmpty()) + GetDlgItem(IDC_WATCH_PATH)->SetWindowText(p); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Directories::OnRomDir() +{ + m_romPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_ROM_DIR)); + if (!p.IsEmpty()) + m_romPath.SetWindowText(p); +} + +void Directories::OnRomDirReset() +{ + m_romPath.SetWindowText(""); +} + +void Directories::OnGBxRomDir() +{ + m_gbxromPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_GBXROM_DIR)); + if (!p.IsEmpty()) + m_gbxromPath.SetWindowText(p); +} + +void Directories::OnGBxRomDirReset() +{ + m_gbxromPath.SetWindowText(""); +} + +void Directories::OnBatteryDir() +{ + m_batteryPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_BATTERY_DIR)); + if (!p.IsEmpty()) + m_batteryPath.SetWindowText(p); +} + +void Directories::OnBatteryDirReset() +{ + m_batteryPath.SetWindowText(""); +} + +void Directories::OnSaveDir() +{ + m_savePath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_SAVE_DIR)); + if (!p.IsEmpty()) + m_savePath.SetWindowText(p); +} + +void Directories::OnSaveDirReset() +{ + m_savePath.SetWindowText(""); +} + +void Directories::OnMovieDir() +{ + m_moviePath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_MOVIE_DIR)); + if (!p.IsEmpty()) + m_moviePath.SetWindowText(p); +} + +void Directories::OnMovieDirReset() +{ + m_moviePath.SetWindowText(""); +} + +void Directories::OnCheatDir() +{ + m_cheatPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_CHEAT_DIR)); + if (!p.IsEmpty()) + m_cheatPath.SetWindowText(p); +} + +void Directories::OnCheatDirReset() +{ + m_cheatPath.SetWindowText(""); +} + +void Directories::OnLuaDir() +{ + m_luaPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_LUA_DIR)); + if (!p.IsEmpty()) + m_luaPath.SetWindowText(p); +} + +void Directories::OnLuaDirReset() +{ + m_luaPath.SetWindowText(""); +} + +void Directories::OnAviDir() +{ + m_aviPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_AVI_DIR)); + if (!p.IsEmpty()) + m_aviPath.SetWindowText(p); +} + +void Directories::OnAviDirReset() +{ + m_aviPath.SetWindowText(""); +} + +void Directories::OnWavDir() +{ + m_wavPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_WAV_DIR)); + if (!p.IsEmpty()) + m_wavPath.SetWindowText(p); +} + +void Directories::OnWavDirReset() +{ + m_wavPath.SetWindowText(""); +} + +void Directories::OnCaptureDir() +{ + m_capturePath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_CAPTURE_DIR)); + if (!p.IsEmpty()) + m_capturePath.SetWindowText(p); +} + +void Directories::OnCaptureDirReset() +{ + m_capturePath.SetWindowText(""); +} + +void Directories::OnIpsDir() +{ + m_ipsPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_IPS_DIR)); + if (!p.IsEmpty()) + m_ipsPath.SetWindowText(p); +} + +void Directories::OnIpsDirReset() +{ + m_ipsPath.SetWindowText(""); +} + +void Directories::OnWatchDir() +{ + m_watchPath.GetWindowText(initialFolderDir); + CString p = browseForDir(winResLoadString(IDS_SELECT_WATCH_DIR)); + if(!p.IsEmpty()) + m_watchPath.SetWindowText(p); +} + +void Directories::OnWatchDirReset() +{ + m_watchPath.SetWindowText(""); +} + +void Directories::OnCancel() +{ + EndDialog(FALSE); +} + +void Directories::OnOK() +{ + CString buffer; + + m_romPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_ROM_DIR, buffer); + else + regDeleteValue(IDS_ROM_DIR); + + m_gbxromPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_GBXROM_DIR, buffer); + else + regDeleteValue(IDS_GBXROM_DIR); + + m_batteryPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_BATTERY_DIR, buffer); + else + regDeleteValue(IDS_BATTERY_DIR); + + m_savePath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_SAVE_DIR, buffer); + else + regDeleteValue(IDS_SAVE_DIR); + + m_moviePath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_MOVIE_DIR, buffer); + else + regDeleteValue(IDS_MOVIE_DIR); + + m_cheatPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_CHEAT_DIR, buffer); + else + regDeleteValue(IDS_CHEAT_DIR); + + m_ipsPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_IPS_DIR, buffer); + else + regDeleteValue(IDS_IPS_DIR); + + m_luaPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_LUA_DIR, buffer); + else + regDeleteValue(IDS_LUA_DIR); + + m_aviPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_AVI_DIR, buffer); + else + regDeleteValue(IDS_AVI_DIR); + + m_wavPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_WAV_DIR, buffer); + else + regDeleteValue(IDS_WAV_DIR); + + m_capturePath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_CAPTURE_DIR, buffer); + else + regDeleteValue(IDS_CAPTURE_DIR); + + m_watchPath.GetWindowText(buffer); + if (!buffer.IsEmpty()) + regSetStringValue(IDS_WATCH_DIR, buffer); + else + regDeleteValue(IDS_WATCH_DIR); + + EndDialog(TRUE); +} + +CString Directories::browseForDir(CString title) +{ + static char buffer[1024]; + LPMALLOC pMalloc; + LPITEMIDLIST pidl; + + CString res; + + if (SUCCEEDED(SHGetMalloc(&pMalloc))) + { + BROWSEINFO bi; + ZeroMemory(&bi, sizeof(bi)); + bi.hwndOwner = m_hWnd; + bi.lpszTitle = title; + bi.pidlRoot = 0; + bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI; // will fail if COINIT_MULTITHREADED + bi.lpfn = browseCallbackProc; + bi.lParam = (LPARAM)(LPCTSTR)initialFolderDir; + + pidl = SHBrowseForFolder(&bi); + + if (pidl) + { + if (SHGetPathFromIDList(pidl, buffer)) + { + res = buffer; + } + pMalloc->Free(pidl); + pMalloc->Release(); + } + } + return res; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Directories.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Directories.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,84 @@ +#if !defined(AFX_DIRECTORIES_H__7ADB14C1_3C1B_4294_8D66_A4E87D6FC731__INCLUDED_) +#define AFX_DIRECTORIES_H__7ADB14C1_3C1B_4294_8D66_A4E87D6FC731__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Directories.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// Directories dialog + +class Directories : public CDialog +{ + // Construction +public: + CString initialFolderDir; + CString browseForDir(CString title); + Directories(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Directories) + enum { IDD = IDD_DIRECTORIES }; + CEdit m_romPath; + CEdit m_gbxromPath; + CEdit m_batteryPath; + CEdit m_savePath; + CEdit m_moviePath; + CEdit m_cheatPath; + CEdit m_ipsPath; + CEdit m_luaPath; + CEdit m_aviPath; + CEdit m_wavPath; + CEdit m_capturePath; + CEdit m_watchPath; +// CEdit m_pluginPath; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Directories) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(Directories) + virtual BOOL OnInitDialog(); + afx_msg void OnRomDir(); + afx_msg void OnRomDirReset(); + afx_msg void OnGBxRomDir(); + afx_msg void OnGBxRomDirReset(); + afx_msg void OnBatteryDir(); + afx_msg void OnBatteryDirReset(); + afx_msg void OnSaveDir(); + afx_msg void OnSaveDirReset(); + afx_msg void OnMovieDir(); + afx_msg void OnMovieDirReset(); + afx_msg void OnCheatDir(); + afx_msg void OnCheatDirReset(); + afx_msg void OnIpsDir(); + afx_msg void OnIpsDirReset(); + afx_msg void OnLuaDir(); + afx_msg void OnLuaDirReset(); + afx_msg void OnAviDir(); + afx_msg void OnAviDirReset(); + afx_msg void OnWavDir(); + afx_msg void OnWavDirReset(); + afx_msg void OnCaptureDir(); + afx_msg void OnCaptureDirReset(); + afx_msg void OnWatchDir(); + afx_msg void OnWatchDirReset(); + virtual void OnCancel(); + virtual void OnOK(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_DIRECTORIES_H__7ADB14C1_3C1B_4294_8D66_A4E87D6FC731__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Disassemble.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Disassemble.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,342 @@ +// Disassemble.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "Disassemble.h" +#include "VBA.h" + +#include "../gba/armdis.h" +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" + +extern void CPUUpdateCPSR(); + +///////////////////////////////////////////////////////////////////////////// +// Disassemble dialog + +Disassemble::Disassemble(CWnd*pParent /*=NULL*/) + : ResizeDlg(Disassemble::IDD, pParent) +{ + //{{AFX_DATA_INIT(Disassemble) + m_c = FALSE; + m_f = FALSE; + m_i = FALSE; + m_n = FALSE; + m_t = FALSE; + m_v = FALSE; + m_z = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + address = 0; + count = 1; + mode = 0; +} + +void Disassemble::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Disassemble) + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_DISASSEMBLE, m_list); + DDX_Check(pDX, IDC_C, m_c); + DDX_Check(pDX, IDC_F, m_f); + DDX_Check(pDX, IDC_I, m_i); + DDX_Check(pDX, IDC_N, m_n); + DDX_Check(pDX, IDC_T, m_t); + DDX_Check(pDX, IDC_V, m_v); + DDX_Check(pDX, IDC_Z, m_z); + DDX_Radio(pDX, IDC_AUTOMATIC, mode); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(Disassemble, CDialog) +//{{AFX_MSG_MAP(Disassemble) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_AUTOMATIC, OnAutomatic) +ON_BN_CLICKED(IDC_ARM, OnArm) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_BN_CLICKED(IDC_GO, OnGo) +ON_BN_CLICKED(IDC_GOPC, OnGopc) +ON_BN_CLICKED(IDC_NEXT, OnNext) +ON_BN_CLICKED(IDC_REFRESH, OnRefresh) +ON_BN_CLICKED(IDC_THUMB, OnThumb) +ON_WM_VSCROLL() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// Disassemble message handlers + +void Disassemble::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void Disassemble::OnAutomatic() +{ + mode = 0; + refresh(); +} + +void Disassemble::OnArm() +{ + mode = 1; + refresh(); +} + +void Disassemble::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void Disassemble::OnGo() +{ + CString buffer; + m_address.GetWindowText(buffer); + sscanf(buffer, "%x", &address); + refresh(); +} + +void Disassemble::OnGopc() +{ + if (armState) + address = armNextPC - 16; + else + address = armNextPC - 8; + + refresh(); +} + +void Disassemble::OnNext() +{ + CPULoop(1); + if (armState) + { + u32 total = address+count*4; + if (armNextPC >= address && armNextPC < total) + {} + else + { + OnGopc(); + } + } + else + { + u32 total = address+count*2; + if (armNextPC >= address && armNextPC < total) + {} + else + { + OnGopc(); + } + } + refresh(); +} + +void Disassemble::OnRefresh() +{ + refresh(); +} + +void Disassemble::OnThumb() +{ + mode = 2; + refresh(); +} + +BOOL Disassemble::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_DISASSEMBLE, DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_NEXT, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_AUTO_UPDATE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_GOPC, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_VSCROLL, DS_SizeY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\DisassembleView", + NULL); + + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS; + si.nMin = 0; + si.nMax = 100; + si.nPos = 50; + si.nPage = 0; + GetDlgItem(IDC_VSCROLL)->SetScrollInfo(SB_CTL, &si, TRUE); + + CFont *font = CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)); + m_list.SetFont(font, FALSE); + for (int i = 0; i < 17; i++) + GetDlgItem(IDC_R0+i)->SetFont(font, FALSE); + + GetDlgItem(IDC_MODE)->SetFont(font, FALSE); + + m_address.LimitText(8); + refresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Disassemble::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar) +{ + switch (nSBCode) + { + case SB_LINEDOWN: + if (mode == 0) + { + if (armState) + address += 4; + else + address += 2; + } + else if (mode == 1) + address += 4; + else + address += 2; + break; + case SB_LINEUP: + if (mode == 0) + { + if (armState) + address -= 4; + else + address -= 2; + } + else if (mode == 1) + address -= 4; + else + address -= 2; + break; + case SB_PAGEDOWN: + if (mode == 0) + { + if (armState) + address += count*4; + else + address += count*2; + } + else if (mode == 1) + address += count*4; + else + address += count*2; + break; + case SB_PAGEUP: + if (mode == 0) + { + if (armState) + address -= count*4; + else + address -= count*2; + } + else if (mode == 1) + address -= count*4; + else + address -= count*2; + break; + } + refresh(); + + CDialog::OnVScroll(nSBCode, nPos, pScrollBar); +} + +void Disassemble::refresh() +{ + if (rom == NULL) + return; + + bool8 arm = armState; + + if (mode != 0) + { + if (mode == 1) + arm = true; + else + arm = false; + } + + int h = m_list.GetItemHeight(0); + RECT r; + m_list.GetClientRect(&r); + count = (r.bottom - r.top+1)/h; + + m_list.ResetContent(); + if (!systemIsEmulating() && systemCartridgeType == 0) + return; + + char buffer[80]; + u32 addr = address; + int i; + int sel = -1; + for (i = 0; i < count; i++) + { + if (addr == armNextPC) + sel = i; + if (arm) + { + addr += disArm(addr, buffer, 3); + } + else + { + addr += disThumb(addr, buffer, 3); + } + m_list.InsertString(-1, buffer); + } + + if (sel != -1) + m_list.SetCurSel(sel); + + CPUUpdateCPSR(); + + for (i = 0; i < 17; i++) + { + sprintf(buffer, "%08x", reg[i].I); + GetDlgItem(IDC_R0+i)->SetWindowText(buffer); + } + + m_n = (reg[16].I & 0x80000000) != 0; + m_z = (reg[16].I & 0x40000000) != 0; + m_c = (reg[16].I & 0x20000000) != 0; + m_v = (reg[16].I & 0x10000000) != 0; + m_i = (reg[16].I & 0x80) != 0; + m_f = (reg[16].I & 0x40) != 0; + m_t = (reg[16].I & 0x20) != 0; + + UpdateData(FALSE); + + int v = reg[16].I & 0x1f; + sprintf(buffer, "%02x", v); + GetDlgItem(IDC_MODE)->SetWindowText(buffer); +} + +void Disassemble::update() +{ + OnGopc(); + refresh(); +} + +void Disassemble::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Disassemble.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Disassemble.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,72 @@ +#if !defined(AFX_DISASSEMBLE_H__CA10E857_7D76_4B19_A62B_D0677040FD0F__INCLUDED_) +#define AFX_DISASSEMBLE_H__CA10E857_7D76_4B19_A62B_D0677040FD0F__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Disassemble.h : header file +// + +#include "IUpdate.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// Disassemble dialog + +class Disassemble : public ResizeDlg, IUpdateListener +{ + // Construction +public: + virtual void update(); + void refresh(); + Disassemble(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Disassemble) + enum { IDD = IDD_DISASSEMBLE }; + CEdit m_address; + CListBox m_list; + BOOL m_c; + BOOL m_f; + BOOL m_i; + BOOL m_n; + BOOL m_t; + BOOL m_v; + BOOL m_z; + //}}AFX_DATA + bool autoUpdate; + int count; + u32 address; + int mode; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Disassemble) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(Disassemble) + afx_msg void OnAutoUpdate(); + afx_msg void OnAutomatic(); + afx_msg void OnArm(); + afx_msg void OnClose(); + afx_msg void OnGo(); + afx_msg void OnGopc(); + afx_msg void OnNext(); + afx_msg void OnRefresh(); + afx_msg void OnThumb(); + virtual BOOL OnInitDialog(); + afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_DISASSEMBLE_H__CA10E857_7D76_4B19_A62B_D0677040FD0F__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Display.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Display.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,35 @@ +#ifndef VBA_WIN32_DISASSEMBLE_H +#define VBA_WIN32_DISASSEMBLE_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +enum DISPLAY_TYPE +{ + GDI = 0, + DIRECT_DRAW = 1, + DIRECT_3D = 2, + OPENGL = 3 +}; + +class IDisplay +{ +public: + IDisplay() {}; + virtual ~IDisplay() {}; + + virtual bool initialize() = 0; + virtual void cleanup() = 0; + virtual void render() = 0; + virtual void checkFullScreen() { }; + virtual void renderMenu() { }; + virtual void clear() = 0; + virtual bool changeRenderSize(int w, int h) { return true; }; + virtual void resize(int w, int h) {}; + virtual void setOption(const char *option, int value) = 0; + virtual DISPLAY_TYPE getType() = 0; + virtual int selectFullScreenMode(GUID * *) = 0; +}; + +#endif // VBA_WIN32_DISASSEMBLE_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ExportGSASnapshot.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ExportGSASnapshot.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,92 @@ +// ExportGSASnapshot.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "ExportGSASnapshot.h" + +#include "../common/System.h" +#include "../gba/GBA.h" +#include "../NLS.h" + +///////////////////////////////////////////////////////////////////////////// +// ExportGSASnapshot dialog + +ExportGSASnapshot::ExportGSASnapshot(CString filename, CString title, CWnd*pParent /*=NULL*/) + : CDialog(ExportGSASnapshot::IDD, pParent) +{ + //{{AFX_DATA_INIT(ExportGSASnapshot) + m_desc = _T(""); + m_notes = _T(""); + m_title = _T(""); + //}}AFX_DATA_INIT + m_title = title; + m_filename = filename; + char date[100]; + char time[100]; + + GetDateFormat(LOCALE_USER_DEFAULT, + DATE_SHORTDATE, + NULL, + NULL, + date, + 100); + GetTimeFormat(LOCALE_USER_DEFAULT, + 0, + NULL, + NULL, + time, + 100); + m_desc.Format("%s %s", date, time); +} + +void ExportGSASnapshot::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(ExportGSASnapshot) + DDX_Text(pDX, IDC_DESC, m_desc); + DDV_MaxChars(pDX, m_desc, 100); + DDX_Text(pDX, IDC_NOTES, m_notes); + DDV_MaxChars(pDX, m_notes, 512); + DDX_Text(pDX, IDC_TITLE, m_title); + DDV_MaxChars(pDX, m_title, 100); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(ExportGSASnapshot, CDialog) +//{{AFX_MSG_MAP(ExportGSASnapshot) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// ExportGSASnapshot message handlers + +BOOL ExportGSASnapshot::OnInitDialog() +{ + CDialog::OnInitDialog(); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void ExportGSASnapshot::OnCancel() +{ + EndDialog(FALSE); +} + +void ExportGSASnapshot::OnOk() +{ + UpdateData(TRUE); + + bool result = CPUWriteGSASnapshot(m_filename, m_title, m_desc, m_notes); + + if (!result) + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", + m_filename); + + EndDialog(TRUE); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ExportGSASnapshot.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ExportGSASnapshot.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,49 @@ +#if !defined(AFX_EXPORTGSASNAPSHOT_H__ADF8566A_C64D_43CF_9CD2_A290370BA4F1__INCLUDED_) +#define AFX_EXPORTGSASNAPSHOT_H__ADF8566A_C64D_43CF_9CD2_A290370BA4F1__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ExportGSASnapshot.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// ExportGSASnapshot dialog + +class ExportGSASnapshot : public CDialog +{ + // Construction +public: + ExportGSASnapshot(CString filename, CString title, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(ExportGSASnapshot) + enum { IDD = IDD_EXPORT_SPS }; + CString m_desc; + CString m_notes; + CString m_title; + //}}AFX_DATA + CString m_filename; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ExportGSASnapshot) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(ExportGSASnapshot) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_EXPORTGSASNAPSHOT_H__ADF8566A_C64D_43CF_9CD2_A290370BA4F1__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/FileDlg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/FileDlg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,199 @@ +// FileDlg.cpp: implementation of the FileDlg class. +// +////////////////////////////////////////////////////////////////////// +//#define WINVER 0x0410 // windows 98 - just for this 1 file - just in case +#include "stdafx.h" +#include +#include +#include "resource.h" +#include "FileDlg.h" +#include "Sound.h" +#include "VBA.h" + +static FileDlg *instance = NULL; + +static UINT_PTR CALLBACK HookFunc(HWND hwnd, + UINT msg, + WPARAM wParam, + LPARAM lParam) +{ + if (instance) + { + if (msg == WM_NOTIFY) + { + OFNOTIFY *notify = (OFNOTIFY *)lParam; + if (notify) + { + if (notify->hdr.code == CDN_TYPECHANGE) + { + instance->OnTypeChange(hwnd); + return 1; + } + } + } + } + return 0; +} + +static UINT_PTR CALLBACK HookFuncOldStyle(HWND hwnd, + UINT msg, + WPARAM wParam, + LPARAM lParam) +{ + if (instance) + { + if (msg == WM_COMMAND) + { + if (HIWORD(wParam) == CBN_SELCHANGE) + { + if (LOWORD(wParam) == cmb1) + { + // call method with combobox handle to keep + // behaviour there + instance->OnTypeChange((HWND)lParam); + return 1; + } + } + } + } + return 0; +} + +///////////////////////////////////////////////////////////////////////////// +// FileDlg + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +FileDlg::FileDlg(CWnd *parent, LPCTSTR file, LPCTSTR filter, + int filterIndex, LPCTSTR ext, LPCTSTR *exts, LPCTSTR initialDir, + LPCTSTR title, bool save, bool noReadOnly) +{ + OSVERSIONINFO info; + info.dwOSVersionInfoSize = sizeof(info); + GetVersionEx(&info); + m_file = file; + int size = sizeof(OPENFILENAME); + + // avoid problems if OPENFILENAME is already defined with the extended fields + // needed for the enhanced open/save dialog +#if _WIN32_WINNT < 0x0500 + if (info.dwPlatformId == VER_PLATFORM_WIN32_NT) + { + if (info.dwMajorVersion >= 5) + size = sizeof(OPENFILENAMEEX); + } +#endif + + ZeroMemory(&m_ofn, sizeof(m_ofn)); + m_ofn.lpstrFile = m_file.GetBuffer(MAX_PATH); + m_ofn.nMaxFile = MAX_PATH; + m_ofn.lStructSize = size; + m_ofn.hwndOwner = parent ? parent->GetSafeHwnd() : NULL; + m_ofn.nFilterIndex = filterIndex; + m_ofn.lpstrInitialDir = initialDir; + m_ofn.lpstrTitle = title; + m_ofn.lpstrDefExt = ext; + m_ofn.lpfnHook = HookFunc; + m_ofn.Flags = OFN_PATHMUSTEXIST | OFN_ENABLESIZING | OFN_ENABLEHOOK; + m_ofn.Flags |= OFN_EXPLORER; + if (noReadOnly) + m_ofn.Flags |= OFN_HIDEREADONLY; + m_filter = filter; + + char *p = m_filter.GetBuffer(0); + + while ((p = strchr(p, '|')) != NULL) + *p++ = 0; + m_ofn.lpstrFilter = m_filter; + + if (theApp.videoOption == VIDEO_320x240) + { + m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_OPENDLG); + m_ofn.lpfnHook = HookFuncOldStyle; + m_ofn.Flags |= OFN_ENABLETEMPLATE; + m_ofn.Flags &= ~OFN_EXPLORER; + } + + isSave = save; + extensions = exts; + + instance = this; +} + +FileDlg::~FileDlg() +{ + instance = NULL; +} + +void FileDlg::OnTypeChange(HWND hwnd) +{ + HWND parent = GetParent(hwnd); + + HWND fileNameControl = ::GetDlgItem(parent, cmb13); + + if (fileNameControl == NULL) + fileNameControl = ::GetDlgItem(parent, edt1); + + if (fileNameControl == NULL) + return; + + CString filename; + GetWindowText(fileNameControl, filename.GetBuffer(MAX_PATH), MAX_PATH); + filename.ReleaseBuffer(); + + HWND typeControl = ::GetDlgItem(parent, cmb1); + + ASSERT(typeControl != NULL); + + int sel = ::SendMessage(typeControl, CB_GETCURSEL, 0, 0); + + ASSERT(sel != -1); + + LPCTSTR typeName = extensions[sel]; + + // sel could easily be an invalid index of extensions, so check for null guard + for(int i = 0; i <= sel; i++) + if(extensions[i] == NULL) + typeName = ""; + + if (filename.GetLength() == 0) + { + if(*typeName) + filename.Format("*%s", typeName); + } + else + { + int index = filename.Find('.'); + if (index == -1) + { + filename = filename + typeName; + } + else + { + filename = filename.Left(index) + typeName; + } + } + SetWindowText(fileNameControl, filename); +} + +int FileDlg::getFilterIndex() +{ + return m_ofn.nFilterIndex; +} + +int FileDlg::DoModal() +{ + systemSoundClearBuffer(); + BOOL res = isSave ? GetSaveFileName(&m_ofn) : + GetOpenFileName(&m_ofn); + + return res ? IDOK : IDCANCEL; +} + +LPCTSTR FileDlg::GetPathName() +{ + return (LPCTSTR)m_file; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/FileDlg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/FileDlg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,43 @@ +#if !defined(AFX_FILEDLG_H__7E4F8B92_1B63_4126_8261_D9334C645940__INCLUDED_) +#define AFX_FILEDLG_H__7E4F8B92_1B63_4126_8261_D9334C645940__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// FileDlg.h : header file +// + +struct OPENFILENAMEEX : public OPENFILENAME +{ + void *pvReserved; + DWORD dwReserved; + DWORD FlagsEx; +}; + +///////////////////////////////////////////////////////////////////////////// +// FileDlg dialog + +class FileDlg +{ +private: + CString m_file; + CString m_filter; +public: + OPENFILENAMEEX m_ofn; + int DoModal(); + LPCTSTR GetPathName(); + virtual int getFilterIndex(); + virtual void OnTypeChange(HWND hwnd); + FileDlg(CWnd *parent, LPCTSTR file, LPCTSTR filter, + int filterIndex, LPCTSTR ext, LPCTSTR *exts, LPCTSTR initialDir, + LPCTSTR title, bool save, bool noReadOnly = false); + virtual ~FileDlg(); +protected: + bool isSave; + LPCTSTR *extensions; +protected: + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. +}; + +#endif // !defined(AFX_FILEDLG_H__7E4F8B92_1B63_4126_8261_D9334C645940__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBACheatsDlg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBACheatsDlg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1279 @@ +// GBACheats.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBACheatsDlg.h" +#include "Reg.h" +#include "StringTokenizer.h" +#include "WinResUtil.h" +#include "Sound.h" +#include "VBA.h" + +#include "../gba/GBACheats.h" +#include "../gba/GBAGlobals.h" +#include "../common/CheatSearch.h" + +//////////////////////////////// + +bool winGbaCheatReaddress() +{ + if (cheatSearchData.count != 2) + return false; + + CheatSearchBlock *block = &cheatSearchData.blocks[0]; + block->data = workRAM; + + block = &cheatSearchData.blocks[1]; + block->data = internalRAM; + + cheatSearchData.count = 2; + return true; +} + +///////////////////////////////////////////////////////////////////////////// +// GBACheatSearch dialog + +GBACheatSearch::GBACheatSearch(CWnd*pParent /*=NULL*/) + : CDialog(GBACheatSearch::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBACheatSearch) + valueType = -1; + sizeType = -1; + searchType = -1; + numberType = -1; + updateValues = FALSE; + //}}AFX_DATA_INIT + data = NULL; +} + +GBACheatSearch::~GBACheatSearch() +{ + if (data) + free(data); +} + +void GBACheatSearch::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBACheatSearch) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_CHEAT_LIST, m_list); + DDX_Radio(pDX, IDC_OLD_VALUE, valueType); + DDX_Radio(pDX, IDC_SIZE_8, sizeType); + DDX_Radio(pDX, IDC_EQ, searchType); + DDX_Radio(pDX, IDC_SIGNED, numberType); + DDX_Check(pDX, IDC_UPDATE, updateValues); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GBACheatSearch, CDialog) +//{{AFX_MSG_MAP(GBACheatSearch) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(IDC_START, OnStart) +ON_BN_CLICKED(IDC_SEARCH, OnSearch) +ON_BN_CLICKED(IDC_ADD_CHEAT, OnAddCheat) +ON_BN_CLICKED(IDC_UPDATE, OnUpdate) +ON_NOTIFY(LVN_GETDISPINFO, IDC_CHEAT_LIST, OnGetdispinfoCheatList) +ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHEAT_LIST, OnItemchangedCheatList) +ON_CONTROL_RANGE(BN_CLICKED, IDC_OLD_VALUE, IDC_SPECIFIC_VALUE, OnValueType) +ON_CONTROL_RANGE(BN_CLICKED, IDC_EQ, IDC_GE, OnSearchType) +ON_CONTROL_RANGE(BN_CLICKED, IDC_SIGNED, IDC_HEXADECIMAL, OnNumberType) +ON_CONTROL_RANGE(BN_CLICKED, IDC_SIZE_8, IDC_SIZE_32, OnSizeType) +//}}AFX_MSG_MAP +ON_WM_CLOSE() +ON_BN_CLICKED(IDC_CHEATREFRESHBUTTON, OnBnClickedCheatrefreshbutton) +//ON_WM_DESTROY() +//ON_WM_NCDESTROY() +//ON_WM_SYSCOMMAND() +//ON_WM_ACTIVATE() +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBACheatSearch message handlers + +void GBACheatSearch::OnOk() +{ + if (theApp.modelessCheatDialogIsOpen) + { + theApp.modelessCheatDialogIsOpen = false; + DestroyWindow(); + } + else + { + EndDialog(TRUE); + } +} + +void GBACheatSearch::OnClose() +{ + CDialog::OnClose(); + if (theApp.modelessCheatDialogIsOpen) + { + theApp.modelessCheatDialogIsOpen = false; + DestroyWindow(); + } + else + { + EndDialog(FALSE); + } +} + +void GBACheatSearch::OnStart() +{ + if (cheatSearchData.count == 0) + { + CheatSearchBlock *block = &cheatSearchData.blocks[0]; + block->data = workRAM; + block->offset = 0x2000000; + block->size = 0x40000; + block->saved = (u8 *)malloc(0x40000); + block->bits = (u8 *)malloc(0x40000>>3); + + block = &cheatSearchData.blocks[1]; + block->data = internalRAM; + block->offset = 0x3000000; + block->size = 0x8000; + block->saved = (u8 *)malloc(0x8000); + block->bits = (u8 *)malloc(0x8000>>3); + + cheatSearchData.count = 2; + } + + cheatSearchStart(&cheatSearchData); + GetDlgItem(IDC_SEARCH)->EnableWindow(TRUE); + + if (theApp.modelessCheatDialogIsOpen) + { + GetDlgItem(IDC_CHEATREFRESHBUTTON)->ShowWindow(TRUE); + GetDlgItem(IDC_CHEATREFRESHBUTTON)->EnableWindow(FALSE); + } +} + +void GBACheatSearch::OnSearch() +{ + CString buffer; + + if (valueType == 0) + cheatSearch(&cheatSearchData, + searchType, + sizeType, + numberType == 0); + else + { + m_value.GetWindowText(buffer); + if (buffer.IsEmpty()) + { + systemMessage(IDS_NUMBER_CANNOT_BE_EMPTY, "Number cannot be empty"); + return; + } + int value = 0; + switch (numberType) + { + case 0: + sscanf(buffer, "%d", &value); + break; + case 1: + sscanf(buffer, "%u", &value); + break; + default: + sscanf(buffer, "%x", &value); + } + cheatSearchValue(&cheatSearchData, + searchType, + sizeType, + numberType == 0, + value); + } + + addChanges(true); + + if (updateValues) + cheatSearchUpdateValues(&cheatSearchData); + + if (theApp.modelessCheatDialogIsOpen) + GetDlgItem(IDC_CHEATREFRESHBUTTON)->EnableWindow(cheatSearchGetCount(&cheatSearchData, sizeType) == 0 ? FALSE : TRUE); + + if (0 == cheatSearchGetCount(&cheatSearchData, sizeType)) + OnStart(); +} + +void GBACheatSearch::OnAddCheat() +{ + int mark = m_list.GetSelectionMark(); + + if (mark != -1) + { + LVITEM item; + memset(&item, 0, sizeof(item)); + item.mask = LVIF_PARAM; + item.iItem = mark; + if (m_list.GetItem(&item)) + { + AddCheat dlg((u32)item.lParam); + dlg.DoModal(); + } + } +} + +void GBACheatSearch::OnUpdate() +{ + if (GetDlgItem(IDC_UPDATE)->SendMessage(BM_GETCHECK, + 0, + 0) & BST_CHECKED) + updateValues = true; + else + updateValues = false; + regSetDwordValue("cheatsUpdate", updateValues); +} + +void GBACheatSearch::OnGetdispinfoCheatList(NMHDR*pNMHDR, LRESULT*pResult) +{ + LV_DISPINFO*info = (LV_DISPINFO *)pNMHDR; + if (info->item.mask & LVIF_TEXT) + { + int index = info->item.iItem; + int col = info->item.iSubItem; + + switch (col) + { + case 0: + strcpy(info->item.pszText, data[index].address); + break; + case 1: + strcpy(info->item.pszText, data[index].oldValue); + break; + case 2: + strcpy(info->item.pszText, data[index].newValue); + break; + } + } + *pResult = TRUE; +} + +void GBACheatSearch::OnItemchangedCheatList(NMHDR*pNMHDR, LRESULT*pResult) +{ + GetDlgItem(IDC_ADD_CHEAT)->EnableWindow(m_list.GetSelectionMark() != -1); + *pResult = TRUE; +} + +BOOL GBACheatSearch::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp = winResLoadString(IDS_ADDRESS); + + m_list.InsertColumn(0, temp, LVCFMT_CENTER, 125, 0); + + temp = winResLoadString(IDS_OLD_VALUE); + m_list.InsertColumn(1, temp, LVCFMT_CENTER, 125, 1); + + temp = winResLoadString(IDS_NEW_VALUE); + m_list.InsertColumn(2, temp, LVCFMT_CENTER, 125, 2); + + m_list.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)), + TRUE); + + m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT); + + if (!cheatSearchData.count) + { + GetDlgItem(IDC_SEARCH)->EnableWindow(FALSE); + GetDlgItem(IDC_ADD_CHEAT)->EnableWindow(FALSE); + } + + valueType = regQueryDwordValue("cheatsValueType", 0); + if (valueType < 0 || valueType > 1) + valueType = 0; + + searchType = regQueryDwordValue("cheatsSearchType", SEARCH_EQ); + if (searchType > 5 || searchType < 0) + searchType = 0; + + numberType = regQueryDwordValue("cheatsNumberType", 2); + if (numberType < 0 || numberType > 2) + numberType = 2; + + sizeType = regQueryDwordValue("cheatsSizeType", 0); + if (sizeType < 0 || sizeType > 2) + sizeType = 0; + + updateValues = regQueryDwordValue("cheatsUpdate", 0) ? + true : false; + + UpdateData(FALSE); + + if (valueType == 0) + m_value.EnableWindow(FALSE); + CenterWindow(); + + if (theApp.modelessCheatDialogIsOpen) + GetDlgItem(IDC_CHEATREFRESHBUTTON)->ShowWindow(TRUE); + + if (cheatSearchData.count) + { + addChanges(false); + if (theApp.modelessCheatDialogIsOpen) + GetDlgItem(IDC_CHEATREFRESHBUTTON)->EnableWindow(cheatSearchGetCount(&cheatSearchData, + sizeType) == 0 ? FALSE : TRUE); + } + else + { + if (theApp.modelessCheatDialogIsOpen) + GetDlgItem(IDC_CHEATREFRESHBUTTON)->EnableWindow(FALSE); + } + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBACheatSearch::addChanges(bool showMsgs) +{ + int count = cheatSearchGetCount(&cheatSearchData, sizeType); + + m_list.DeleteAllItems(); + + if (count > 4000) + { + if (showMsgs) + systemMessage( + IDS_SEARCH_PRODUCED_TOO_MANY, + "Search produced %d results.\nThey have been remembered, but are too many to display.\nPlease refine it better by performing additional searches.", + count); + return; + } + + if (count == 0) + { + if (showMsgs) + systemMessage(IDS_SEARCH_PRODUCED_NO_RESULTS, "Search produced no results."); + return; + } + + m_list.SetItemCount(count); + if (data) + free(data); + + data = (WinCheatsData *)calloc(count, sizeof(WinCheatsData)); + + int inc = 1; + switch (sizeType) + { + case 1: + inc = 2; + break; + case 2: + inc = 4; + break; + } + + int index = 0; + if (numberType == 0) + { + for (int i = 0; i < cheatSearchData.count; i++) + { + CheatSearchBlock *block = &cheatSearchData.blocks[i]; + + for (int j = 0; j < block->size; j += inc) + { + if (IS_BIT_SET(block->bits, j)) + { + addChange(index++, + block->offset | j, + cheatSearchSignedRead(block->saved, + j, + sizeType), + cheatSearchSignedRead(block->data, + j, + sizeType)); + } + } + } + } + else + { + for (int i = 0; i < cheatSearchData.count; i++) + { + CheatSearchBlock *block = &cheatSearchData.blocks[i]; + + for (int j = 0; j < block->size; j += inc) + { + if (IS_BIT_SET(block->bits, j)) + { + addChange(index++, + block->offset | j, + cheatSearchRead(block->saved, + j, + sizeType), + cheatSearchRead(block->data, + j, + sizeType)); + } + } + } + } + + for (int i = 0; i < count; i++) + { + LVITEM item; + + item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; + item.iItem = i; + item.iSubItem = 0; + item.lParam = data[i].addr; + item.state = 0; + item.stateMask = 0; + item.pszText = LPSTR_TEXTCALLBACK; + m_list.InsertItem(&item); + + m_list.SetItemText(i, 1, LPSTR_TEXTCALLBACK); + m_list.SetItemText(i, 2, LPSTR_TEXTCALLBACK); + } +} + +void GBACheatSearch::addChange(int index, u32 address, u32 oldValue, u32 newValue) +{ + data[index].addr = address; + sprintf(data[index].address, "%08x", address); + switch (numberType) + { + case 0: + sprintf(data[index].oldValue, "%d", oldValue); + sprintf(data[index].newValue, "%d", newValue); + break; + case 1: + sprintf(data[index].oldValue, "%u", oldValue); + sprintf(data[index].newValue, "%u", newValue); + break; + case 2: + switch (sizeType) + { + case 0: + sprintf(data[index].oldValue, "%02x", oldValue); + sprintf(data[index].newValue, "%02x", newValue); + break; + case 1: + sprintf(data[index].oldValue, "%04x", oldValue); + sprintf(data[index].newValue, "%04x", newValue); + break; + case 2: + sprintf(data[index].oldValue, "%08x", oldValue); + sprintf(data[index].newValue, "%08x", newValue); + break; + } + } +} + +void GBACheatSearch::OnValueType(UINT id) +{ + switch (id) + { + case IDC_OLD_VALUE: + valueType = 0; + m_value.EnableWindow(FALSE); + regSetDwordValue("cheatsValueType", 0); + break; + case IDC_SPECIFIC_VALUE: + valueType = 1; + m_value.EnableWindow(TRUE); + regSetDwordValue("cheatsValueType", 1); + break; + } +} + +void GBACheatSearch::OnSearchType(UINT id) +{ + switch (id) + { + case IDC_EQ: + searchType = SEARCH_EQ; + regSetDwordValue("cheatsSearchType", 0); + break; + case IDC_NE: + searchType = SEARCH_NE; + regSetDwordValue("cheatsSearchType", 1); + break; + case IDC_LT: + searchType = SEARCH_LT; + regSetDwordValue("cheatsSearchType", 2); + break; + case IDC_LE: + searchType = SEARCH_LE; + regSetDwordValue("cheatsSearchType", 3); + break; + case IDC_GT: + searchType = SEARCH_GT; + regSetDwordValue("cheatsSearchType", 4); + break; + case IDC_GE: + searchType = SEARCH_GE; + regSetDwordValue("cheatsSearchType", 5); + break; + } +} + +void GBACheatSearch::OnNumberType(UINT id) +{ + switch (id) + { + case IDC_SIGNED: + numberType = 0; + regSetDwordValue("cheatsNumberType", 0); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + case IDC_UNSIGNED: + numberType = 1; + regSetDwordValue("cheatsNumberType", 1); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + case IDC_HEXADECIMAL: + numberType = 2; + regSetDwordValue("cheatsNumberType", 2); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + } +} + +void GBACheatSearch::OnSizeType(UINT id) +{ + switch (id) + { + case IDC_SIZE_8: + sizeType = BITS_8; + regSetDwordValue("cheatsSizeType", 0); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + case IDC_SIZE_16: + sizeType = BITS_16; + regSetDwordValue("cheatsSizeType", 1); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + case IDC_SIZE_32: + sizeType = BITS_32; + regSetDwordValue("cheatsSizeType", 2); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + } +} + +///////////////////////////////////////////////////////////////////////////// +// AddCheat dialog + +AddCheat::AddCheat(u32 address, CWnd*pParent /*=NULL*/) + : CDialog(AddCheat::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddCheat) + numberType = regQueryDwordValue("cheatsNumberType", 2); + if (numberType < 0 || numberType > 2) + numberType = 2; + sizeType = regQueryDwordValue("cheatsSizeType", 0); + if (sizeType < 0 || sizeType > 2) + sizeType = 0; + //}}AFX_DATA_INIT + this->address = address; +} + +void AddCheat::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddCheat) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Radio(pDX, IDC_SIZE_8, sizeType); + DDX_Radio(pDX, IDC_SIGNED, numberType); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(AddCheat, CDialog) +//{{AFX_MSG_MAP(AddCheat) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_CONTROL_RANGE(BN_CLICKED, IDC_SIGNED, IDC_HEXADECIMAL, OnNumberType) +ON_CONTROL_RANGE(BN_CLICKED, IDC_SIZE_8, IDC_SIZE_32, OnSizeType) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// AddCheat message handlers + +void AddCheat::OnOk() +{ + // add cheat + if (addCheat()) + { + EndDialog(TRUE); + } +} + +void AddCheat::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddCheat::OnInitDialog() +{ + CDialog::OnInitDialog(); + + if (address != 0) + { + CString buffer; + buffer.Format("%08x", address); + m_address.SetWindowText(buffer); + m_address.EnableWindow(FALSE); + } + + UpdateData(FALSE); + + GetDlgItem(IDC_DESC)->SendMessage(EM_LIMITTEXT, + 32, + 0); + if (address != 0) + { + GetDlgItem(IDC_SIZE_8)->EnableWindow(FALSE); + GetDlgItem(IDC_SIZE_16)->EnableWindow(FALSE); + GetDlgItem(IDC_SIZE_32)->EnableWindow(FALSE); + GetDlgItem(IDC_HEXADECIMAL)->EnableWindow(FALSE); + GetDlgItem(IDC_UNSIGNED)->EnableWindow(FALSE); + GetDlgItem(IDC_SIGNED)->EnableWindow(FALSE); + } + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void AddCheat::OnNumberType(UINT id) +{ + switch (id) + { + case IDC_SIGNED: + numberType = 0; + regSetDwordValue("cheatsNumberType", 0); + break; + case IDC_UNSIGNED: + numberType = 1; + regSetDwordValue("cheatsNumberType", 1); + break; + case IDC_HEXADECIMAL: + numberType = 2; + regSetDwordValue("cheatsNumberType", 2); + break; + } +} + +void AddCheat::OnSizeType(UINT id) +{ + switch (id) + { + case IDC_SIZE_8: + sizeType = BITS_8; + regSetDwordValue("cheatsSizeType", 0); + break; + case IDC_SIZE_16: + sizeType = BITS_16; + regSetDwordValue("cheatsSizeType", 1); + break; + case IDC_SIZE_32: + sizeType = BITS_32; + regSetDwordValue("cheatsSizeType", 2); + break; + } +} + +bool AddCheat::addCheat() +{ + CString buffer; + CString code; + + m_address.GetWindowText(buffer); + u32 address = 0; + sscanf(buffer, "%x", &address); + if ((address >= 0x02000000 && address < 0x02040000) || + (address >= 0x03000000 && address < 0x03008000)) + {} + else + { + systemMessage(IDS_INVALID_ADDRESS, "Invalid address: %08x", address); + return false; + } + if (sizeType != 0) + { + if (sizeType == 1 && address & 1) + { + systemMessage(IDS_MISALIGNED_HALFWORD, "Misaligned half-word address: %08x", address); + return false; + } + if (sizeType == 2 && address & 3) + { + systemMessage(IDS_MISALIGNED_WORD, "Misaligned word address: %08x", address); + return false; + } + } + u32 value; + m_value.GetWindowText(buffer); + + if (buffer.IsEmpty()) + { + systemMessage(IDS_VALUE_CANNOT_BE_EMPTY, "Value cannot be empty"); + return false; + } + + switch (numberType) + { + case 0: + sscanf(buffer, "%d", &value); + break; + case 1: + sscanf(buffer, "%u", &value); + break; + default: + sscanf(buffer, "%x", &value); + } + + m_desc.GetWindowText(buffer); + + switch (sizeType) + { + case 0: + code.Format("%08x:%02x", address, value); + break; + case 1: + code.Format("%08x:%04x", address, value); + break; + case 2: + code.Format("%08x:%08x", address, value); + break; + } + + cheatsAdd(code, buffer, address, value, -1, sizeType); + return true; +} + +///////////////////////////////////////////////////////////////////////////// +// GBACheatList dialog + +GBACheatList::GBACheatList(CWnd*pParent /*=NULL*/) + : CDialog(GBACheatList::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBACheatList) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void GBACheatList::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBACheatList) + DDX_Control(pDX, IDC_RESTORE, m_restore); + DDX_Control(pDX, IDC_CHEAT_LIST, m_list); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GBACheatList, CDialog) +//{{AFX_MSG_MAP(GBACheatList) +ON_BN_CLICKED(IDC_ADD_CHEAT, OnAddCheat) +ON_BN_CLICKED(IDC_ADD_CODE, OnAddCode) +ON_BN_CLICKED(IDC_ADD_CODEBREAKER, OnAddCodebreaker) +ON_BN_CLICKED(IDC_ADD_GAMESHARK, OnAddGameshark) +ON_BN_CLICKED(IDC_ENABLE, OnEnable) +ON_BN_CLICKED(IDC_REMOVE, OnRemove) +ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll) +ON_BN_CLICKED(IDC_RESTORE, OnRestore) +ON_BN_CLICKED(ID_OK, OnOk) +ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHEAT_LIST, OnItemchangedCheatList) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBACheatList message handlers + +void GBACheatList::OnAddCheat() +{ + AddCheat dlg(0); + dlg.DoModal(); + refresh(); +} + +void GBACheatList::OnAddCode() +{ + AddCheatCode dlg; + dlg.DoModal(); + refresh(); +} + +void GBACheatList::OnAddCodebreaker() +{ + AddCBACode dlg; + dlg.DoModal(); + refresh(); +} + +void GBACheatList::OnAddGameshark() +{ + AddGSACode dlg; + dlg.DoModal(); + refresh(); +} + +void GBACheatList::OnEnable() +{ + int mark = m_list.GetSelectionMark(); + int count = m_list.GetItemCount(); + + if (mark != -1) + { + LVITEM item; + for (int i = 0; i < count; i++) + { + memset(&item, 0, sizeof(item)); + item.mask = LVIF_PARAM|LVIF_STATE; + item.stateMask = LVIS_SELECTED; + item.iItem = i; + if (m_list.GetItem(&item)) + { + if (item.state & LVIS_SELECTED) + { + if (cheatsList[item.lParam].enabled) + cheatsDisable(item.lParam); + else + cheatsEnable(item.lParam); + } + } + } + refresh(); + } +} + +void GBACheatList::OnRemove() +{ + int mark = m_list.GetSelectionMark(); + int count = m_list.GetItemCount(); + + if (mark != -1) + { + for (int i = count - 1; i >= 0; i--) + { + LVITEM item; + memset(&item, 0, sizeof(item)); + item.mask = LVIF_PARAM|LVIF_STATE; + item.iItem = i; + item.stateMask = LVIS_SELECTED; + if (m_list.GetItem(&item)) + { + if (item.state & LVIS_SELECTED) + { + cheatsDelete(item.lParam, restoreValues); + } + } + } + refresh(); + } +} + +void GBACheatList::OnRemoveAll() +{ + cheatsDeleteAll(restoreValues); + refresh(); +} + +void GBACheatList::OnRestore() +{ + restoreValues = !restoreValues; + regSetDwordValue("cheatsRestore", restoreValues); +} + +void GBACheatList::OnOk() +{ + EndDialog(TRUE); +} + +void GBACheatList::OnItemchangedCheatList(NMHDR*pNMHDR, LRESULT*pResult) +{ + if (m_list.GetSelectionMark() != -1) + { + GetDlgItem(IDC_REMOVE)->EnableWindow(TRUE); + GetDlgItem(IDC_ENABLE)->EnableWindow(TRUE); + } + else + { + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ENABLE)->EnableWindow(FALSE); + } + + if (!duringRefresh) + { + LPNMLISTVIEW l = (LPNMLISTVIEW)pNMHDR; + if (l->uChanged & LVIF_STATE) + { + if (((l->uOldState & LVIS_STATEIMAGEMASK)>>12) != + (((l->uNewState & LVIS_STATEIMAGEMASK)>>12))) + { + if (m_list.GetCheck(l->iItem)) + cheatsEnable(l->lParam); + else + cheatsDisable(l->lParam); + refresh(); + } + } + } + + *pResult = 0; +} + +BOOL GBACheatList::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp = winResLoadString(IDS_CODE); + m_list.InsertColumn(0, temp, LVCFMT_LEFT, 170, 0); + temp = winResLoadString(IDS_DESCRIPTION); + m_list.InsertColumn(1, temp, LVCFMT_LEFT, 150, 1); + temp = winResLoadString(IDS_STATUS); + m_list.InsertColumn(2, temp, LVCFMT_LEFT, 80, 1); + + m_list.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)), + TRUE); + + m_list.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT); + + duringRefresh = false; + + restoreValues = regQueryDwordValue("cheatsRestore", 0) ? + true : false; + + m_restore.SetCheck(restoreValues); + + refresh(); + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ENABLE)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBACheatList::refresh() +{ + duringRefresh = true; + m_list.DeleteAllItems(); + + CString buffer; + + for (int i = 0; i < cheatsNumber; i++) + { + LVITEM item; + + item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; + item.iItem = i; + item.iSubItem = 0; + item.lParam = i; + item.state = 0; + item.stateMask = 0; + item.pszText = cheatsList[i].codestring; + m_list.InsertItem(&item); + + m_list.SetCheck(i, (cheatsList[i].enabled) ? TRUE : FALSE); + + m_list.SetItemText(i, 1, cheatsList[i].desc); + + buffer = (cheatsList[i].enabled) ? 'E' : 'D'; + m_list.SetItemText(i, 2, buffer); + } + duringRefresh = false; +} + +///////////////////////////////////////////////////////////////////////////// +// AddGSACode dialog + +AddGSACode::AddGSACode(CWnd*pParent /*=NULL*/) + : CDialog(AddGSACode::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddGSACode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void AddGSACode::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddGSACode) + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_CODE, m_code); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(AddGSACode, CDialog) +//{{AFX_MSG_MAP(AddGSACode) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// AddGSACode message handlers + +void AddGSACode::OnOk() +{ + CString desc; + CString buffer; + CString part1; + CString code; + CString token; + + m_code.GetWindowText(buffer); + m_desc.GetWindowText(desc); + + StringTokenizer st(buffer, " \t\n\r"); + part1.Empty(); + const char *t = st.next(); + while (t) + { + token = t; + token.MakeUpper(); + if (token.GetLength() == 16) + cheatsAddGSACode(token, desc, false); + else if (token.GetLength() == 12) + { + code = token.Left(8); + code += " "; + code += token.Right(4); + cheatsAddCBACode(code, desc); + } + else if (part1.IsEmpty()) + part1 = token; + else + { + if (token.GetLength() == 4) + { + code = part1; + code += " "; + code += token; + cheatsAddCBACode(code, desc); + } + else + { + code = part1 + token; + cheatsAddGSACode(code, desc, true); + } + part1.Empty(); + } + + t = st.next(); + } + EndDialog(TRUE); +} + +void AddGSACode::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddGSACode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_code.LimitText(1024); + m_desc.LimitText(32); + CString title = winResLoadString(IDS_ADD_GSA_CODE); + SetWindowText(title); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +///////////////////////////////////////////////////////////////////////////// +// AddCBACode dialog + +AddCBACode::AddCBACode(CWnd*pParent /*=NULL*/) + : CDialog(AddCBACode::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddCBACode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void AddCBACode::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddCBACode) + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_CODE, m_code); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(AddCBACode, CDialog) +//{{AFX_MSG_MAP(AddCBACode) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// AddCBACode message handlers + +void AddCBACode::OnOk() +{ + CString desc; + CString buffer; + CString part1; + CString code; + CString token; + + m_code.GetWindowText(buffer); + m_desc.GetWindowText(desc); + + StringTokenizer st(buffer, " \t\n\r"); + part1.Empty(); + const char *t = st.next(); + while (t) + { + token = t; + token.MakeUpper(); + if (token.GetLength() == 16) + cheatsAddGSACode(token, desc, false); + else if (token.GetLength() == 12) + { + code = token.Left(8); + code += " "; + code += token.Right(4); + cheatsAddCBACode(code, desc); + } + else if (part1.IsEmpty()) + part1 = token; + else + { + if (token.GetLength() == 4) + { + code = part1; + code += " "; + code += token; + cheatsAddCBACode(code, desc); + } + else + { + code = part1 + token; + cheatsAddGSACode(code, desc, true); + } + part1.Empty(); + } + + t = st.next(); + } + EndDialog(TRUE); +} + +void AddCBACode::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddCBACode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_code.LimitText(1024); + m_desc.LimitText(32); + CString title = winResLoadString(IDS_ADD_CBA_CODE); + SetWindowText(title); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +///////////////////////////////////////////////////////////////////////////// +// AddCheatCode dialog + +AddCheatCode::AddCheatCode(CWnd*pParent /*=NULL*/) + : CDialog(AddCheatCode::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddCheatCode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void AddCheatCode::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddCheatCode) + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_CODE, m_code); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(AddCheatCode, CDialog) +//{{AFX_MSG_MAP(AddCheatCode) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// AddCheatCode message handlers + +void AddCheatCode::OnOk() +{ + CString desc; + CString buffer; + CString token; + + m_code.GetWindowText(buffer); + m_desc.GetWindowText(desc); + + StringTokenizer st(buffer, " \t\n\r"); + const char * t = st.next(); + while (t) + { + token = t; + token.MakeUpper(); + cheatsAddCheatCode(token, desc); + t = st.next(); + } + EndDialog(TRUE); +} + +void AddCheatCode::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddCheatCode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_code.LimitText(1024); + m_desc.LimitText(32); + CString title = winResLoadString(IDS_ADD_CHEAT_CODE); + SetWindowText(title); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBACheatSearch::OnBnClickedCheatrefreshbutton() +{ + addChanges(false); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBACheatsDlg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBACheatsDlg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,264 @@ +#if !defined(AFX_GBACHEATS_H__FC31D47D_52C8_42B2_95C7_7C3FD09316A4__INCLUDED_) +#define AFX_GBACHEATS_H__FC31D47D_52C8_42B2_95C7_7C3FD09316A4__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBACheatsDlg.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GBACheatSearch dialog + +struct WinCheatsData +{ + u32 addr; + char address[9]; + char oldValue[12]; + char newValue[12]; +}; + +class GBACheatSearch : public CDialog +{ + // Construction +public: + afx_msg void OnSizeType(UINT id); + afx_msg void OnNumberType(UINT id); + afx_msg void OnSearchType(UINT id); + afx_msg void OnValueType(UINT id); + void addChange(int index, u32 address, u32 oldValue, u32 newValue); + GBACheatSearch(CWnd*pParent = NULL); // standard constructor + ~GBACheatSearch(); + + // Dialog Data + //{{AFX_DATA(GBACheatSearch) + enum { IDD = IDD_CHEATS }; + CEdit m_value; + CListCtrl m_list; + int valueType; + int sizeType; + int searchType; + int numberType; + BOOL updateValues; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBACheatSearch) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GBACheatSearch) + afx_msg void OnOk(); + afx_msg void OnStart(); + afx_msg void OnSearch(); + afx_msg void OnAddCheat(); + afx_msg void OnUpdate(); + afx_msg void OnGetdispinfoCheatList(NMHDR*pNMHDR, LRESULT*pResult); + afx_msg void OnItemchangedCheatList(NMHDR*pNMHDR, LRESULT*pResult); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +private: + void addChanges(bool showMsgs); + WinCheatsData *data; +public: + afx_msg void OnClose(); + afx_msg void OnBnClickedCheatrefreshbutton(); +}; + +///////////////////////////////////////////////////////////////////////////// +// AddCheat dialog + +class AddCheat : public CDialog +{ + // Construction +public: + bool addCheat(); + afx_msg void OnSizeType(UINT id); + afx_msg void OnNumberType(UINT id); + AddCheat(u32 address, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddCheat) + enum { IDD = IDD_ADD_CHEAT }; + CEdit m_value; + CEdit m_desc; + CEdit m_address; + int sizeType; + int numberType; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddCheat) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + u32 address; + + // Generated message map functions + //{{AFX_MSG(AddCheat) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +///////////////////////////////////////////////////////////////////////////// +// GBACheatList dialog + +class GBACheatList : public CDialog +{ + // Construction +public: + void refresh(); + GBACheatList(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBACheatList) + enum { IDD = IDD_CHEAT_LIST }; + CButton m_restore; + CListCtrl m_list; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBACheatList) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + bool duringRefresh; + bool restoreValues; + + // Generated message map functions + //{{AFX_MSG(GBACheatList) + afx_msg void OnAddCheat(); + afx_msg void OnAddCode(); + afx_msg void OnAddCodebreaker(); + afx_msg void OnAddGameshark(); + afx_msg void OnEnable(); + afx_msg void OnRemove(); + afx_msg void OnRemoveAll(); + afx_msg void OnRestore(); + afx_msg void OnOk(); + afx_msg void OnItemchangedCheatList(NMHDR*pNMHDR, LRESULT*pResult); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +///////////////////////////////////////////////////////////////////////////// +// AddGSACode dialog + +class AddGSACode : public CDialog +{ + // Construction +public: + AddGSACode(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddGSACode) + enum { IDD = IDD_ADD_CHEAT_DLG }; + CEdit m_desc; + CEdit m_code; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddGSACode) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(AddGSACode) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +// AddCBACode dialog + +class AddCBACode : public CDialog +{ + // Construction +public: + AddCBACode(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddCBACode) + enum { IDD = IDD_ADD_CHEAT_DLG }; + CEdit m_desc; + CEdit m_code; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddCBACode) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(AddCBACode) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +// AddCheatCode dialog + +class AddCheatCode : public CDialog +{ + // Construction +public: + AddCheatCode(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddCheatCode) + enum { IDD = IDD_ADD_CHEAT_DLG }; + CEdit m_desc; + CEdit m_code; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddCheatCode) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(AddCheatCode) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBACHEATS_H__FC31D47D_52C8_42B2_95C7_7C3FD09316A4__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBCheatsDlg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBCheatsDlg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1140 @@ +// GBCheats.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBCheatsDlg.h" +#include "Reg.h" +#include "StringTokenizer.h" +#include "WinResUtil.h" +#include "Sound.h" +#include "VBA.h" + +#include "../common/CheatSearch.h" +#include "../gb/gbCheats.h" +#include "../gb/gbGlobals.h" + +static inline bool winGbCheatAddVerifyGs(const char *code, const char *desc) +{ + gbAddGsCheat(code, desc); + return true; +} + +static inline bool winGbCheatAddVerifyGg(const char *code, const char *desc) +{ + gbAddGgCheat(code, desc); + return true; +} + +//////////////////////////////// + +bool winGbCheatReaddress() +{ + if (cheatSearchData.count != 3) + return false; + + CheatSearchBlock *block = &cheatSearchData.blocks[0]; + if (gbRamSize > 0) + { + if (gbRam) + block->data = gbRam; + else + block->data = &gbMemory[0xa000]; + block->offset = 0xa000; + block->size = gbRamSize; + cheatSearchSetSavedAndBits(block); + } + else + { + cheatSearchZeroBlock(&cheatSearchData.blocks[0]); + } + + block = &cheatSearchData.blocks[1]; + if (gbCgbMode) + { + block->data = &gbMemory[0xc000]; + block->offset = 0xc000; + block->size = 0x1000; + cheatSearchSetSavedAndBits(block); + + block = &cheatSearchData.blocks[2]; + block->data = gbWram; + block->offset = 0xd000; + block->size = 0x8000; + cheatSearchSetSavedAndBits(block); + } + else + { + block->data = &gbMemory[0xc000]; + block->offset = 0xc000; + block->size = 0x2000; + cheatSearchSetSavedAndBits(block); + + cheatSearchZeroBlock(&cheatSearchData.blocks[2]); + } + + cheatSearchData.count = 3; + return true; +} + +///////////////////////////////////////////////////////////////////////////// +// GBCheatSearch dialog + +GBCheatSearch::GBCheatSearch(CWnd*pParent /*=NULL*/) + : CDialog(GBCheatSearch::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBCheatSearch) + searchType = -1; + numberType = -1; + sizeType = -1; + updateValues = FALSE; + valueType = -1; + //}}AFX_DATA_INIT + data = NULL; +} + +GBCheatSearch::~GBCheatSearch() +{ + if (data) + free(data); +} + +void GBCheatSearch::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBCheatSearch) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_CHEAT_LIST, m_list); + DDX_Radio(pDX, IDC_EQ, searchType); + DDX_Radio(pDX, IDC_SIGNED, numberType); + DDX_Radio(pDX, IDC_SIZE_8, sizeType); + DDX_Check(pDX, IDC_UPDATE, updateValues); + DDX_Radio(pDX, IDC_OLD_VALUE, valueType); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GBCheatSearch, CDialog) +//{{AFX_MSG_MAP(GBCheatSearch) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(IDC_ADD_CHEAT, OnAddCheat) +ON_BN_CLICKED(IDC_SEARCH, OnSearch) +ON_BN_CLICKED(IDC_START, OnStart) +ON_BN_CLICKED(IDC_UPDATE, OnUpdate) +ON_NOTIFY(LVN_GETDISPINFO, IDC_CHEAT_LIST, OnGetdispinfoCheatList) +ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHEAT_LIST, OnItemchangedCheatList) +ON_CONTROL_RANGE(BN_CLICKED, IDC_OLD_VALUE, IDC_SPECIFIC_VALUE, OnValueType) +ON_CONTROL_RANGE(BN_CLICKED, IDC_EQ, IDC_GE, OnSearchType) +ON_CONTROL_RANGE(BN_CLICKED, IDC_SIGNED, IDC_HEXADECIMAL, OnNumberType) +ON_CONTROL_RANGE(BN_CLICKED, IDC_SIZE_8, IDC_SIZE_32, OnSizeType) +//}}AFX_MSG_MAP +ON_WM_CLOSE() +ON_BN_CLICKED(IDC_CHEATREFRESHBUTTON, OnBnClickedCheatrefreshbutton) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBCheatSearch message handlers + +void GBCheatSearch::OnOk() +{ + if (data) + free(data); + data = NULL; + + if (theApp.modelessCheatDialogIsOpen) + { + theApp.modelessCheatDialogIsOpen = false; + DestroyWindow(); + } + else + { + EndDialog(TRUE); + } +} + +void GBCheatSearch::OnClose() +{ + CDialog::OnClose(); + if (theApp.modelessCheatDialogIsOpen) + { + theApp.modelessCheatDialogIsOpen = false; + DestroyWindow(); + } + else + { + EndDialog(FALSE); + } +} + +void GBCheatSearch::OnBnClickedCheatrefreshbutton() +{ + addChanges(false); +} + +void GBCheatSearch::OnAddCheat() +{ + int mark = m_list.GetSelectionMark(); + + if (mark != -1) + { + LVITEM item; + memset(&item, 0, sizeof(item)); + item.mask = LVIF_PARAM; + item.iItem = mark; + if (m_list.GetItem(&item)) + { + AddGBCheat dlg((u32)item.lParam); + dlg.DoModal(); + } + } +} + +void GBCheatSearch::OnSearch() +{ + CString buffer; + if (valueType == 0) + cheatSearch(&cheatSearchData, + searchType, + sizeType, + numberType == 0); + else + { + m_value.GetWindowText(buffer); + if (buffer.IsEmpty()) + { + systemMessage(IDS_NUMBER_CANNOT_BE_EMPTY, "Number cannot be empty"); + return; + } + int value = 0; + switch (numberType) + { + case 0: + sscanf(buffer, "%d", &value); + break; + case 1: + sscanf(buffer, "%u", &value); + break; + default: + sscanf(buffer, "%x", &value); + } + cheatSearchValue(&cheatSearchData, + searchType, + sizeType, + numberType == 0, + value); + } + + addChanges(true); + + if (updateValues) + cheatSearchUpdateValues(&cheatSearchData); + + if (theApp.modelessCheatDialogIsOpen) + GetDlgItem(IDC_CHEATREFRESHBUTTON)->EnableWindow(cheatSearchGetCount(&cheatSearchData, sizeType) == 0 ? FALSE : TRUE); + + if (0 == cheatSearchGetCount(&cheatSearchData, sizeType)) + OnStart(); +} + +void GBCheatSearch::OnStart() +{ + if (cheatSearchData.count == 0) + { + CheatSearchBlock *block = &cheatSearchData.blocks[0]; + if (gbRamSize > 0) + { + if (gbRam) + block->data = gbRam; + else + block->data = &gbMemory[0xa000]; + block->size = gbRamSize; + block->offset = 0xa000; + block->saved = (u8 *)malloc(gbRamSize); + block->bits = (u8 *)malloc(gbRamSize >> 3); + } + else + { + cheatSearchZeroBlock(&cheatSearchData.blocks[0]); + } + + block = &cheatSearchData.blocks[1]; + if (gbCgbMode) + { + block->data = &gbMemory[0xc000]; + block->size = 0x1000; + block->offset = 0xc000; + block->saved = (u8 *)malloc(0x1000); + block->bits = (u8 *)malloc(0x1000 >> 3); + + block = &cheatSearchData.blocks[2]; + block->data = gbWram; + block->size = 0x8000; + block->offset = 0xd000; + block->saved = (u8 *)malloc(0x8000); + block->bits = (u8 *)malloc(0x8000 >> 3); + } + else + { + block->data = &gbMemory[0xc000]; + block->size = 0x2000; + block->offset = 0xc000; + block->saved = (u8 *)malloc(0x2000); + block->bits = (u8 *)malloc(0x2000 >> 3); + + cheatSearchZeroBlock(&cheatSearchData.blocks[2]); + } + + cheatSearchData.count = 3; + } + + cheatSearchStart(&cheatSearchData); + GetDlgItem(IDC_SEARCH)->EnableWindow(TRUE); + + if (theApp.modelessCheatDialogIsOpen) + { + GetDlgItem(IDC_CHEATREFRESHBUTTON)->ShowWindow(TRUE); + GetDlgItem(IDC_CHEATREFRESHBUTTON)->EnableWindow(FALSE); + } +} + +void GBCheatSearch::OnUpdate() +{ + if (GetDlgItem(IDC_UPDATE)->SendMessage(BM_GETCHECK, + 0, + 0) & BST_CHECKED) + updateValues = true; + else + updateValues = false; + regSetDwordValue("gbCheatsUpdate", updateValues); +} + +BOOL GBCheatSearch::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp = winResLoadString(IDS_ADDRESS); + + m_list.InsertColumn(0, temp, LVCFMT_CENTER, 125, 0); + + temp = winResLoadString(IDS_OLD_VALUE); + m_list.InsertColumn(1, temp, LVCFMT_CENTER, 125, 1); + + temp = winResLoadString(IDS_NEW_VALUE); + m_list.InsertColumn(2, temp, LVCFMT_CENTER, 125, 2); + + m_list.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)), + TRUE); + + m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT); + + if (!cheatSearchData.count) + { + GetDlgItem(IDC_SEARCH)->EnableWindow(FALSE); + GetDlgItem(IDC_ADD_CHEAT)->EnableWindow(FALSE); + } + + valueType = regQueryDwordValue("gbCheatsValueType", 0); + if (valueType < 0 || valueType > 1) + valueType = 2; + + searchType = regQueryDwordValue("gbCheatsSearchType", + SEARCH_EQ); + if (searchType < 0 || searchType > 5) + searchType = 0; + + numberType = regQueryDwordValue("gbCheatsNumberType", 2); + if (numberType < 0 || numberType > 2) + numberType = 2; + + sizeType = regQueryDwordValue("gbCheatsSizeType", 0); + if (sizeType < 0 || sizeType > 2) + sizeType = 0; + + updateValues = regQueryDwordValue("gbCheatsUpdate", 0) ? + true : false; + + UpdateData(FALSE); + + if (valueType == 0) + m_value.EnableWindow(FALSE); + + CenterWindow(); + + if (theApp.modelessCheatDialogIsOpen) + GetDlgItem(IDC_CHEATREFRESHBUTTON)->ShowWindow(TRUE); + + if (cheatSearchData.count) + { + addChanges(false); + if (theApp.modelessCheatDialogIsOpen) + GetDlgItem(IDC_CHEATREFRESHBUTTON)->EnableWindow(cheatSearchGetCount(&cheatSearchData, + sizeType) == 0 ? FALSE : TRUE); + } + else + { + if (theApp.modelessCheatDialogIsOpen) + GetDlgItem(IDC_CHEATREFRESHBUTTON)->EnableWindow(FALSE); + } + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBCheatSearch::OnGetdispinfoCheatList(NMHDR*pNMHDR, LRESULT*pResult) +{ + LV_DISPINFO*info = (LV_DISPINFO *)pNMHDR; + if (info->item.mask & LVIF_TEXT) + { + int index = info->item.iItem; + int col = info->item.iSubItem; + + switch (col) + { + case 0: + strcpy(info->item.pszText, data[index].address); + break; + case 1: + strcpy(info->item.pszText, data[index].oldValue); + break; + case 2: + strcpy(info->item.pszText, data[index].newValue); + break; + } + } + *pResult = TRUE; +} + +void GBCheatSearch::OnItemchangedCheatList(NMHDR*pNMHDR, LRESULT*pResult) +{ + GetDlgItem(IDC_ADD_CHEAT)->EnableWindow(m_list.GetSelectionMark() != -1); +} + +int GBCheatSearch::getBank(u16 addr, int j) +{ + switch (addr >> 12) + { + case 0x0a: + return j / 0x2000; + case 0x0d: + return j / 0x1000; + } + return 0; +} + +void GBCheatSearch::addChange(int index, int bank, u16 address, int offset, u32 oldValue, u32 newValue) +{ + data[index].bank = bank; + if (bank) + { + if (address == 0xa000) + address |= offset & 0x1fff; + else + address |= offset & 0xfff; + } + else + address |= offset; + data[index].addr = address; + sprintf(data[index].address, "%02x:%04x", bank, address); + switch (numberType) + { + case 0: + sprintf(data[index].oldValue, "%d", oldValue); + sprintf(data[index].newValue, "%d", newValue); + break; + case 1: + sprintf(data[index].oldValue, "%u", oldValue); + sprintf(data[index].newValue, "%u", newValue); + break; + case 2: + switch (sizeType) + { + case 0: + sprintf(data[index].oldValue, "%02x", oldValue); + sprintf(data[index].newValue, "%02x", newValue); + break; + case 1: + sprintf(data[index].oldValue, "%04x", oldValue); + sprintf(data[index].newValue, "%04x", newValue); + break; + case 2: + sprintf(data[index].oldValue, "%08x", oldValue); + sprintf(data[index].newValue, "%08x", newValue); + break; + } + } +} + +void GBCheatSearch::addChanges(bool showMsg) +{ + int count = cheatSearchGetCount(&cheatSearchData, sizeType); + + m_list.DeleteAllItems(); + + if (count > 4000) + { + if (showMsg) + systemMessage( + IDS_SEARCH_PRODUCED_TOO_MANY, + "Search produced %d results.\nThey have been remembered, but are too many to display.\nPlease refine it better by performing additional searches.", + count); + return; + } + + if (count == 0) + { + if (showMsg) + systemMessage(IDS_SEARCH_PRODUCED_NO_RESULTS, "Search produced no results"); + return; + } + + m_list.SetItemCount(count); + if (data) + free(data); + + data = (WinGbCheatsData *)calloc(count, sizeof(WinGbCheatsData)); + + int inc = 1; + switch (sizeType) + { + case 1: + inc = 2; + break; + case 2: + inc = 4; + break; + } + + int index = 0; + if (numberType == 0) + { + for (int i = 0; i < cheatSearchData.count; i++) + { + CheatSearchBlock *block = &cheatSearchData.blocks[i]; + + for (int j = 0; j < block->size; j += inc) + { + if (IS_BIT_SET(block->bits, j)) + { + addChange(index++, + getBank(block->offset|j, j), + block->offset, + j, + cheatSearchSignedRead(block->saved, + j, + sizeType), + cheatSearchSignedRead(block->data, + j, + sizeType)); + } + } + } + } + else + { + for (int i = 0; i < cheatSearchData.count; i++) + { + CheatSearchBlock *block = &cheatSearchData.blocks[i]; + + for (int j = 0; j < block->size; j += inc) + { + if (IS_BIT_SET(block->bits, j)) + { + addChange(index++, + getBank(block->offset|j, j), + block->offset, + j, + cheatSearchRead(block->saved, + j, + sizeType), + cheatSearchRead(block->data, + j, + sizeType)); + } + } + } + } + + for (int i = 0; i < count; i++) + { + LVITEM item; + + item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; + item.iItem = i; + item.iSubItem = 0; + item.lParam = data[i].addr| + (data[i].bank << 16); + item.state = 0; + item.stateMask = 0; + item.pszText = LPSTR_TEXTCALLBACK; + m_list.InsertItem(&item); + + m_list.SetItemText(i, 1, LPSTR_TEXTCALLBACK); + m_list.SetItemText(i, 2, LPSTR_TEXTCALLBACK); + } +} + +void GBCheatSearch::OnValueType(UINT id) +{ + switch (id) + { + case IDC_OLD_VALUE: + valueType = 0; + m_value.EnableWindow(FALSE); + regSetDwordValue("gbCheatsValueType", 0); + break; + case IDC_SPECIFIC_VALUE: + valueType = 1; + m_value.EnableWindow(TRUE); + regSetDwordValue("gbCheatsValueType", 1); + break; + } +} + +void GBCheatSearch::OnSearchType(UINT id) +{ + switch (id) + { + case IDC_EQ: + searchType = SEARCH_EQ; + regSetDwordValue("gbCheatsSearchType", 0); + break; + case IDC_NE: + searchType = SEARCH_NE; + regSetDwordValue("gbCheatsSearchType", 1); + break; + case IDC_LT: + searchType = SEARCH_LT; + regSetDwordValue("gbCheatsSearchType", 2); + break; + case IDC_LE: + searchType = SEARCH_LE; + regSetDwordValue("gbCheatsSearchType", 3); + break; + case IDC_GT: + searchType = SEARCH_GT; + regSetDwordValue("gbCheatsSearchType", 4); + break; + case IDC_GE: + searchType = SEARCH_GE; + regSetDwordValue("gbCheatsSearchType", 5); + break; + } +} + +void GBCheatSearch::OnNumberType(UINT id) +{ + switch (id) + { + case IDC_SIGNED: + numberType = 0; + regSetDwordValue("gbCheatsNumberType", 0); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + case IDC_UNSIGNED: + numberType = 1; + regSetDwordValue("gbCheatsNumberType", 1); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + case IDC_HEXADECIMAL: + numberType = 2; + regSetDwordValue("gbCheatsNumberType", 2); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + } +} + +void GBCheatSearch::OnSizeType(UINT id) +{ + switch (id) + { + case IDC_SIZE_8: + sizeType = BITS_8; + regSetDwordValue("gbCheatsSizeType", 0); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + case IDC_SIZE_16: + sizeType = BITS_16; + regSetDwordValue("gbCheatsSizeType", 1); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + case IDC_SIZE_32: + sizeType = BITS_32; + regSetDwordValue("gbCheatsSizeType", 2); + if (m_list.GetItemCount()) + { + addChanges(false); + } + break; + } +} + +///////////////////////////////////////////////////////////////////////////// +// AddGBCheat dialog + +AddGBCheat::AddGBCheat(u32 addr, CWnd*pParent /*=NULL*/) + : CDialog(AddGBCheat::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddGBCheat) + numberType = regQueryDwordValue("gbCheatsNumberType", 2); + if (numberType < 0 || numberType > 2) + numberType = 2; + sizeType = regQueryDwordValue("gbCheatsSizeType", 0); + if (sizeType < 0 || sizeType > 2) + sizeType = 0; + //}}AFX_DATA_INIT + address = addr; +} + +void AddGBCheat::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddGBCheat) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Radio(pDX, IDC_SIZE_8, sizeType); + DDX_Radio(pDX, IDC_SIGNED, numberType); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(AddGBCheat, CDialog) +//{{AFX_MSG_MAP(AddGBCheat) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_CONTROL_RANGE(BN_CLICKED, IDC_SIGNED, IDC_HEXADECIMAL, OnNumberType) +ON_CONTROL_RANGE(BN_CLICKED, IDC_SIZE_8, IDC_SIZE_32, OnSizeType) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// AddGBCheat message handlers + +void AddGBCheat::OnCancel() +{ + EndDialog(FALSE); +} + +void AddGBCheat::OnOk() +{ + // add cheat + if (addCheat()) + { + EndDialog(TRUE); + } +} + +bool AddGBCheat::addCheat() +{ + CString buffer; + CString code; + + u32 value; + m_value.GetWindowText(buffer); + + if (buffer.IsEmpty()) + { + systemMessage(IDS_VALUE_CANNOT_BE_EMPTY, "Value cannot be empty"); + return false; + } + + switch (numberType) + { + case 0: + sscanf(buffer, "%d", &value); + break; + case 1: + sscanf(buffer, "%u", &value); + break; + default: + sscanf(buffer, "%x", &value); + } + + m_desc.GetWindowText(buffer); + + int bank = (address >> 16); + address &= 0xFFFF; + + if (address >= 0xd000) + bank += 0x90; + else + bank = 0x01; + + switch (sizeType) + { + case 0: + code.Format("%02X%02X%02X%02X", bank, value, address&0xFF, address>>8); + gbAddGsCheat(code, buffer); + break; + case 1: + code.Format("%02X%02X%02X%02X", bank, value&0xFF, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + address++; + code.Format("%02X%02X%02X%02X", bank, value>>8, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + break; + case 2: + code.Format("%02X%02X%02X%02X", bank, value&0xFF, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + address++; + code.Format("%02X%02X%02X%02X", bank, (value>>8) & 0xFF, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + address++; + code.Format("%02X%02X%02X%02X", bank, (value>>16)&0xFF, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + address++; + code.Format("%02X%02X%02X%02X", bank, value>>24, address&0xFF, + address>>8); + gbAddGsCheat(code, buffer); + break; + } + + return true; +} + +BOOL AddGBCheat::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString buffer; + buffer.Format("%02x:%08x", (address>>16), address&0xFFFF); + m_address.SetWindowText(buffer); + m_address.EnableWindow(FALSE); + ::SetWindowLong(m_address, + GWL_USERDATA, + address); + + UpdateData(FALSE); + + m_desc.LimitText(32); + + if (address != 0) + { + GetDlgItem(IDC_SIZE_8)->EnableWindow(FALSE); + GetDlgItem(IDC_SIZE_16)->EnableWindow(FALSE); + GetDlgItem(IDC_SIZE_32)->EnableWindow(FALSE); + GetDlgItem(IDC_HEXADECIMAL)->EnableWindow(FALSE); + GetDlgItem(IDC_UNSIGNED)->EnableWindow(FALSE); + GetDlgItem(IDC_SIGNED)->EnableWindow(FALSE); + } + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void AddGBCheat::OnNumberType(UINT id) +{ + switch (id) + { + case IDC_SIGNED: + numberType = 0; + regSetDwordValue("gbCheatsNumberType", 0); + break; + case IDC_UNSIGNED: + numberType = 1; + regSetDwordValue("gbCheatsNumberType", 1); + break; + case IDC_HEXADECIMAL: + numberType = 2; + regSetDwordValue("gbCheatsNumberType", 2); + break; + } +} + +void AddGBCheat::OnSizeType(UINT id) +{ + switch (id) + { + case IDC_SIZE_8: + sizeType = BITS_8; + regSetDwordValue("gbCheatsSizeType", 0); + break; + case IDC_SIZE_16: + sizeType = BITS_16; + regSetDwordValue("gbCheatsSizeType", 1); + break; + case IDC_SIZE_32: + sizeType = BITS_32; + regSetDwordValue("gbCheatsSizeType", 2); + break; + } +} + +///////////////////////////////////////////////////////////////////////////// +// GBCheatList dialog + +GBCheatList::GBCheatList(CWnd*pParent /*=NULL*/) + : CDialog(GBCheatList::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBCheatList) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + duringRefresh = false; +} + +void GBCheatList::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBCheatList) + DDX_Control(pDX, IDC_CHEAT_LIST, m_list); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GBCheatList, CDialog) +//{{AFX_MSG_MAP(GBCheatList) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(IDC_ADD_GG_CHEAT, OnAddGgCheat) +ON_BN_CLICKED(IDC_ADD_GS_CHEAT, OnAddGsCheat) +ON_BN_CLICKED(IDC_ENABLE, OnEnable) +ON_BN_CLICKED(IDC_REMOVE, OnRemove) +ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll) +ON_NOTIFY(LVN_ITEMCHANGED, IDC_CHEAT_LIST, OnItemchangedCheatList) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBCheatList message handlers + +void GBCheatList::OnOk() +{ + EndDialog(TRUE); +} + +void GBCheatList::OnAddGgCheat() +{ + CString temp = winResLoadString(IDS_ADD_GG_CODE); + AddGBCode dlg(winGbCheatAddVerifyGg, 11, temp); + dlg.DoModal(); + refresh(); +} + +void GBCheatList::OnAddGsCheat() +{ + CString temp = winResLoadString(IDS_ADD_GS_CODE); + + AddGBCode dlg(winGbCheatAddVerifyGs, 8, temp); + dlg.DoModal(); + refresh(); +} + +void GBCheatList::OnEnable() +{ + int mark = m_list.GetSelectionMark(); + + if (mark != -1) + { + LVITEM item; + memset(&item, 0, sizeof(item)); + item.mask = LVIF_PARAM; + item.iItem = mark; + if (m_list.GetItem(&item)) + { + if (gbCheatList[item.lParam].enabled) + gbCheatDisable(item.lParam); + else + gbCheatEnable(item.lParam); + refresh(); + } + } +} + +void GBCheatList::OnRemove() +{ + int mark = m_list.GetSelectionMark(); + + if (mark != -1) + { + LVITEM item; + memset(&item, 0, sizeof(item)); + item.mask = LVIF_PARAM; + item.iItem = mark; + if (m_list.GetItem(&item)) + { + gbCheatRemove(item.lParam); + refresh(); + } + } +} + +void GBCheatList::OnRemoveAll() +{ + gbCheatRemoveAll(); + refresh(); +} + +void GBCheatList::OnItemchangedCheatList(NMHDR*pNMHDR, LRESULT*pResult) +{ + if (m_list.GetSelectionMark() != -1) + { + GetDlgItem(IDC_REMOVE)->EnableWindow(TRUE); + GetDlgItem(IDC_ENABLE)->EnableWindow(TRUE); + } + else + { + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ENABLE)->EnableWindow(FALSE); + } + + if (!duringRefresh) + { + LPNMLISTVIEW l = (LPNMLISTVIEW)pNMHDR; + if (l->uChanged & LVIF_STATE) + { + if (((l->uOldState & LVIS_STATEIMAGEMASK)>>12) != + (((l->uNewState & LVIS_STATEIMAGEMASK)>>12))) + { + if (m_list.GetCheck(l->iItem)) + gbCheatEnable(l->lParam); + else + gbCheatDisable(l->lParam); + refresh(); + } + } + } +} + +BOOL GBCheatList::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp = winResLoadString(IDS_CODE); + m_list.InsertColumn(0, temp, LVCFMT_LEFT, 120, 0); + temp = winResLoadString(IDS_DESCRIPTION); + m_list.InsertColumn(1, temp, LVCFMT_LEFT, 200, 1); + temp = winResLoadString(IDS_STATUS); + m_list.InsertColumn(2, temp, LVCFMT_LEFT, 80, 2); + + m_list.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)), + TRUE); + + m_list.SetExtendedStyle(LVS_EX_CHECKBOXES | + LVS_EX_FULLROWSELECT); + + refresh(); + GetDlgItem(IDC_REMOVE)->EnableWindow(FALSE); + GetDlgItem(IDC_ENABLE)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBCheatList::refresh() +{ + duringRefresh = true; + + m_list.DeleteAllItems(); + + char buffer[2]; + + for (int i = 0; i < gbCheatNumber; i++) + { + LVITEM item; + + item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE; + item.iItem = i; + item.iSubItem = 0; + item.lParam = i; + item.state = 0; + item.stateMask = 0; + item.pszText = gbCheatList[i].cheatCode; + m_list.InsertItem(&item); + + m_list.SetCheck(i, (gbCheatList[i].enabled ? TRUE : FALSE)); + + m_list.SetItemText(i, 1, gbCheatList[i].cheatDesc); + + buffer[0] = (gbCheatList[i].enabled) ? 'E' : 'D'; + buffer[1] = 0; + m_list.SetItemText(i, 2, buffer); + } + duringRefresh = false; +} + +///////////////////////////////////////////////////////////////////////////// +// AddGBCode dialog + +AddGBCode::AddGBCode(bool(*verify)(const char *, const char *), int len, const char *title, CWnd*pParent /*=NULL*/) + : CDialog(AddGBCode::IDD, pParent) +{ + //{{AFX_DATA_INIT(AddGBCode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + addVerify = verify; + addLength = len; + addTitle = title; +} + +void AddGBCode::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(AddGBCode) + DDX_Control(pDX, IDC_DESC, m_desc); + DDX_Control(pDX, IDC_CODE, m_code); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(AddGBCode, CDialog) +//{{AFX_MSG_MAP(AddGBCode) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// AddGBCode message handlers + +void AddGBCode::OnOk() +{ + CString desc; + CString buffer; + m_code.GetWindowText(buffer); + m_desc.GetWindowText(desc); + + StringTokenizer st(buffer, " \t\n\r"); + const char * t = st.next(); + while (t) + { + addVerify(t, desc); + t = st.next(); + } + EndDialog(TRUE); +} + +void AddGBCode::OnCancel() +{ + EndDialog(FALSE); +} + +BOOL AddGBCode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_code.LimitText(1024); + m_desc.LimitText(32); + SetWindowText(addTitle); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBCheatsDlg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBCheatsDlg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,198 @@ +#if !defined(AFX_GBCHEATSDLG_H__8ECCB04A_AB75_4552_8625_C6FBF30A95D9__INCLUDED_) +#define AFX_GBCHEATSDLG_H__8ECCB04A_AB75_4552_8625_C6FBF30A95D9__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBCheats.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GBCheatSearch dialog + +struct WinGbCheatsData +{ + int bank; + u16 addr; + char address[9]; + char oldValue[12]; + char newValue[12]; +}; + +class GBCheatSearch : public CDialog +{ + // Construction +public: + afx_msg void OnSizeType(UINT id); + afx_msg void OnNumberType(UINT id); + afx_msg void OnSearchType(UINT id); + afx_msg void OnValueType(UINT id); + void addChanges(bool showMsg); + void addChange(int index, int bank, u16 address, int offset, u32 oldValue, u32 newValue); + int getBank(u16 addr, int j); + GBCheatSearch(CWnd*pParent = NULL); // standard constructor + ~GBCheatSearch(); + + // Dialog Data + //{{AFX_DATA(GBCheatSearch) + enum { IDD = IDD_CHEATS }; + CEdit m_value; + CListCtrl m_list; + int searchType; + int numberType; + int sizeType; + BOOL updateValues; + int valueType; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBCheatSearch) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + WinGbCheatsData *data; + + // Generated message map functions + //{{AFX_MSG(GBCheatSearch) + afx_msg void OnOk(); + afx_msg void OnAddCheat(); + afx_msg void OnSearch(); + afx_msg void OnStart(); + afx_msg void OnUpdate(); + virtual BOOL OnInitDialog(); + afx_msg void OnGetdispinfoCheatList(NMHDR*pNMHDR, LRESULT*pResult); + afx_msg void OnItemchangedCheatList(NMHDR*pNMHDR, LRESULT*pResult); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +public: + afx_msg void OnClose(); + afx_msg void OnBnClickedCheatrefreshbutton(); +}; + +///////////////////////////////////////////////////////////////////////////// +// AddGBCheat dialog + +class AddGBCheat : public CDialog +{ + // Construction +public: + afx_msg void OnSizeType(UINT id); + afx_msg void OnNumberType(UINT id); + bool addCheat(); + AddGBCheat(u32 addr, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddGBCheat) + enum { IDD = IDD_ADD_CHEAT }; + CEdit m_value; + CEdit m_address; + CEdit m_desc; + int sizeType; + int numberType; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddGBCheat) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + u32 address; + + // Generated message map functions + //{{AFX_MSG(AddGBCheat) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +///////////////////////////////////////////////////////////////////////////// +// GBCheatList dialog + +class GBCheatList : public CDialog +{ + // Construction +public: + void refresh(); + GBCheatList(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBCheatList) + enum { IDD = IDD_GB_CHEAT_LIST }; + CListCtrl m_list; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBCheatList) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + bool duringRefresh; + + // Generated message map functions + //{{AFX_MSG(GBCheatList) + afx_msg void OnOk(); + afx_msg void OnAddGgCheat(); + afx_msg void OnAddGsCheat(); + afx_msg void OnEnable(); + afx_msg void OnRemove(); + afx_msg void OnRemoveAll(); + afx_msg void OnItemchangedCheatList(NMHDR*pNMHDR, LRESULT*pResult); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +// AddGBCode dialog + +class AddGBCode : public CDialog +{ + // Construction +public: + AddGBCode(bool(*verify)(const char *, const char *), int, const char *, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(AddGBCode) + enum { IDD = IDD_ADD_CHEAT_DLG }; + CEdit m_desc; + CEdit m_code; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(AddGBCode) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + int addLength; + CString addTitle; + bool (*addVerify)(const char *, const char *); + + // Generated message map functions + //{{AFX_MSG(AddGBCode) + afx_msg void OnOk(); + afx_msg void OnCancel(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBCHEATSDLG_H__8ECCB04A_AB75_4552_8625_C6FBF30A95D9__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBColorDlg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBColorDlg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,238 @@ +// GBColorDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBColorDlg.h" +#include "Reg.h" + +extern int32 gbPaletteOption; +extern int emulating; +extern int systemCartridgeType; +extern u16 gbPalette[128]; +extern u16 systemGbPalette[24]; + +static u16 defaultPalettes[][24] = { + { + 0x7FFF, 0x56B5, 0x318C, 0x0000, 0x7FFF, 0x56B5, 0x318C, 0x0000, + }, + { + 0x6200, 0x7E10, 0x7C10, 0x5000, 0x6200, 0x7E10, 0x7C10, 0x5000, + }, + { + 0x4008, 0x4000, 0x2000, 0x2008, 0x4008, 0x4000, 0x2000, 0x2008, + }, + { + 0x43F0, 0x03E0, 0x4200, 0x2200, 0x43F0, 0x03E0, 0x4200, 0x2200, + }, + { + 0x43FF, 0x03FF, 0x221F, 0x021F, 0x43FF, 0x03FF, 0x221F, 0x021F, + }, + { + 0x621F, 0x7E1F, 0x7C1F, 0x2010, 0x621F, 0x7E1F, 0x7C1F, 0x2010, + }, + { + 0x621F, 0x401F, 0x001F, 0x2010, 0x621F, 0x401F, 0x001F, 0x2010, + }, + { + 0x1314, 0x0E91, 0x0E0D, 0x2108, 0x1314, 0x0E91, 0x0E0D, 0x2108, + } +}; + +///////////////////////////////////////////////////////////////////////////// +// GBColorDlg dialog + +GBColorDlg::GBColorDlg(CWnd*pParent /*=NULL*/) + : CDialog(GBColorDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBColorDlg) + which = -1; + //}}AFX_DATA_INIT + which = gbPaletteOption; +} + +void GBColorDlg::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBColorDlg) + DDX_Control(pDX, IDC_PREDEFINED, m_predefined); + DDX_Radio(pDX, IDC_DEFAULT, which); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GBColorDlg, CDialog) +//{{AFX_MSG_MAP(GBColorDlg) +ON_BN_CLICKED(IDC_DEFAULT, OnDefault) +ON_BN_CLICKED(IDC_RESET, OnReset) +ON_BN_CLICKED(IDC_USER1, OnUser1) +ON_BN_CLICKED(IDC_USER2, OnUser2) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_CBN_SELCHANGE(IDC_PREDEFINED, OnSelchangePredefined) +//}}AFX_MSG_MAP +ON_CONTROL_RANGE(BN_CLICKED, IDC_COLOR_BG0, IDC_COLOR_OB3, OnColorClicked) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBColorDlg message handlers + +void GBColorDlg::OnDefault() +{ + setWhich(0); +} + +void GBColorDlg::OnReset() +{ + int s = which * 8; + colors[s++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + colors[s++] = (0x15) | (0x15 << 5) | (0x15 << 10); + colors[s++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + colors[s++] = 0; + + colors[s++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + colors[s++] = (0x15) | (0x15 << 5) | (0x15 << 10); + colors[s++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + colors[s] = 0; + setWhich(which); +} + +void GBColorDlg::OnUser1() +{ + setWhich(1); +} + +void GBColorDlg::OnUser2() +{ + setWhich(2); +} + +void GBColorDlg::OnCancel() +{ + EndDialog(FALSE); +} + +void GBColorDlg::OnOk() +{ + EndDialog(TRUE); +} + +BOOL GBColorDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + colorControls[0].SubclassDlgItem(IDC_COLOR_BG0, this); + colorControls[1].SubclassDlgItem(IDC_COLOR_BG1, this); + colorControls[2].SubclassDlgItem(IDC_COLOR_BG2, this); + colorControls[3].SubclassDlgItem(IDC_COLOR_BG3, this); + colorControls[4].SubclassDlgItem(IDC_COLOR_OB0, this); + colorControls[5].SubclassDlgItem(IDC_COLOR_OB1, this); + colorControls[6].SubclassDlgItem(IDC_COLOR_OB2, this); + colorControls[7].SubclassDlgItem(IDC_COLOR_OB3, this); + + for (int i = 0; i < 24; i++) + { + colors[i] = systemGbPalette[i]; + } + + const char *names[] = { + "Standard", + "Blue Sea", + "Dark Night", + "Green Forest", + "Hot Desert", + "Pink Dreams", + "Weird Colors", + "Real Colors" + }; + + for (int j = 0; j < 8; j++) + { + int index = m_predefined.AddString(names[j]); + m_predefined.SetItemData(index, j); + } + + RECT cbSize; + int Height; + + m_predefined.GetClientRect(&cbSize); + Height = m_predefined.GetItemHeight(0); + Height += m_predefined.GetItemHeight(0) * (10); + + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 + // Now add on the height of the border of the edit box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // The height of the border of the drop-down box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // now set the size of the window + m_predefined.SetWindowPos(NULL, + 0, 0, + cbSize.right, Height, + SWP_NOMOVE | SWP_NOZORDER); + + setWhich(which); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBColorDlg::setWhich(int w) +{ + which = w; + + for (int i = 0; i < 8; i++) + { + colorControls[i].setColor(colors[which*8+i]); + } +} + +u16 *GBColorDlg::getColors() +{ + return colors; +} + +void GBColorDlg::OnColorClicked(UINT id) +{ + id -= IDC_COLOR_BG0; + + u16 color = colors[which*8+id]; + + COLORREF colorInit = + RGB((color & 0x1f) << 3, ((color >> 5) & 0x1f) << 3, ((color >> 10) & 0x1f) << 3); + + CColorDialog dlg(colorInit, + CC_FULLOPEN | CC_ANYCOLOR, this); + + if (IDOK == dlg.DoModal()) + { + COLORREF c = dlg.GetColor(); + + colors[which*8+id] = (u16)((c >> 3) & 0x1f | ((c >> 11) & 0x1f) << 5 | + ((c >> 19) & 0x1f) << 10); + colorControls[id].setColor(colors[which*8+id]); + } +} + +int GBColorDlg::getWhich() +{ + return which; +} + +void GBColorDlg::OnSelchangePredefined() +{ + int sel = m_predefined.GetCurSel(); + + if (sel != -1) + { + int data = m_predefined.GetItemData(sel); + for (int i = 0; i < 8; i++) + { + colorControls[i].setColor(defaultPalettes[data][i]); + colors[which*8+i] = defaultPalettes[data][i]; + } + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBColorDlg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBColorDlg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,60 @@ +#if !defined(AFX_GBCOLORDLG_H__8D6126EF_06BB_48CF_ABB3_2CC4B1B60358__INCLUDED_) +#define AFX_GBCOLORDLG_H__8D6126EF_06BB_48CF_ABB3_2CC4B1B60358__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// GBColorDlg.h : header file +// + +#include "ColorButton.h" // Added by ClassView + +///////////////////////////////////////////////////////////////////////////// +// GBColorDlg dialog + +class GBColorDlg : public CDialog +{ + // Construction +public: + int getWhich(); + afx_msg void OnColorClicked(UINT id); + u16 *getColors(); + void setWhich(int w); + u16 colors[24]; + ColorButton colorControls[8]; + GBColorDlg(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBColorDlg) + enum { IDD = IDD_GB_COLORS }; + CComboBox m_predefined; + int which; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBColorDlg) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GBColorDlg) + afx_msg void OnDefault(); + afx_msg void OnReset(); + afx_msg void OnUser1(); + afx_msg void OnUser2(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + afx_msg void OnSelchangePredefined(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBCOLORDLG_H__8D6126EF_06BB_48CF_ABB3_2CC4B1B60358__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBDisassemble.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBDisassemble.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,241 @@ +// GBDisassemble.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBDisassemble.h" +#include "VBA.h" + +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" + +extern gbRegister AF; +extern gbRegister BC; +extern gbRegister DE; +extern gbRegister HL; +extern gbRegister SP; +extern gbRegister PC; +extern u16 IFF; +extern int gbDis(char *, u16); + +///////////////////////////////////////////////////////////////////////////// +// GBDisassemble dialog + +GBDisassemble::GBDisassemble(CWnd*pParent /*=NULL*/) + : ResizeDlg(GBDisassemble::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBDisassemble) + m_c = FALSE; + m_h = FALSE; + m_n = FALSE; + m_z = FALSE; + //}}AFX_DATA_INIT + address = 0; + autoUpdate = false; + count = 1; + lastAddress = 0; +} + +void GBDisassemble::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBDisassemble) + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_DISASSEMBLE, m_list); + DDX_Check(pDX, IDC_C, m_c); + DDX_Check(pDX, IDC_H, m_h); + DDX_Check(pDX, IDC_N, m_n); + DDX_Check(pDX, IDC_Z, m_z); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GBDisassemble, CDialog) +//{{AFX_MSG_MAP(GBDisassemble) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_BN_CLICKED(IDC_REFRESH, OnRefresh) +ON_BN_CLICKED(IDC_NEXT, OnNext) +ON_BN_CLICKED(IDC_GO, OnGo) +ON_BN_CLICKED(IDC_GOPC, OnGopc) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_WM_VSCROLL() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBDisassemble message handlers + +void GBDisassemble::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void GBDisassemble::OnRefresh() +{ + refresh(); +} + +void GBDisassemble::OnNext() +{ + gbEmulate(1); + if (PC.W < address || PC.W >= lastAddress) + OnGopc(); + refresh(); +} + +void GBDisassemble::OnGo() +{ + CString buffer; + m_address.GetWindowText(buffer); + sscanf(buffer, "%x", &address); + refresh(); +} + +void GBDisassemble::OnGopc() +{ + address = PC.W; + + refresh(); +} + +void GBDisassemble::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +BOOL GBDisassemble::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_DISASSEMBLE, DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_NEXT, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_AUTO_UPDATE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_GOPC, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_VSCROLL, DS_SizeY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBDisassembleView", + NULL); + + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS; + si.nMin = 0; + si.nMax = 100; + si.nPos = 50; + si.nPage = 0; + GetDlgItem(IDC_VSCROLL)->SetScrollInfo(SB_CTL, &si, TRUE); + CFont *font = CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)); + m_list.SetFont(font); + + for (int i = 0; i < 7; i++) + GetDlgItem(IDC_R0+i)->SetFont(font); + + m_address.LimitText(4); + refresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBDisassemble::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar) +{ + char buffer[80]; + + switch (nSBCode) + { + case SB_LINEDOWN: + address += gbDis(buffer, address); + break; + case SB_LINEUP: + address--; + break; + case SB_PAGEDOWN: + address = lastAddress; + break; + case SB_PAGEUP: + address -= count; + break; + } + refresh(); + + CDialog::OnVScroll(nSBCode, nPos, pScrollBar); +} + +void GBDisassemble::refresh() +{ + if (gbRom == NULL) + return; + + int h = m_list.GetItemHeight(0); + RECT r; + m_list.GetClientRect(&r); + count = (r.bottom - r.top+1)/h; + + m_list.ResetContent(); + if (!systemIsEmulating() || systemCartridgeType != 1) + return; + + char buffer[80]; + u16 addr = address; + int i; + int sel = -1; + for (i = 0; i < count; i++) + { + if (addr == PC.W) + sel = i; + addr += gbDis(buffer, addr); + m_list.InsertString(-1, buffer); + } + lastAddress = addr-1; + if (sel != -1) + m_list.SetCurSel(sel); + + sprintf(buffer, "%04x", AF.W); + GetDlgItem(IDC_R0)->SetWindowText(buffer); + sprintf(buffer, "%04x", BC.W); + GetDlgItem(IDC_R1)->SetWindowText(buffer); + sprintf(buffer, "%04x", DE.W); + GetDlgItem(IDC_R2)->SetWindowText(buffer); + sprintf(buffer, "%04x", HL.W); + GetDlgItem(IDC_R3)->SetWindowText(buffer); + sprintf(buffer, "%04x", SP.W); + GetDlgItem(IDC_R4)->SetWindowText(buffer); + sprintf(buffer, "%04x", PC.W); + GetDlgItem(IDC_R5)->SetWindowText(buffer); + sprintf(buffer, "%04x", IFF); + GetDlgItem(IDC_R6)->SetWindowText(buffer); + + m_z = (AF.B.B0 & 0x80) != 0; + m_n = (AF.B.B0 & 0x40) != 0; + m_h = (AF.B.B0 & 0x20) != 0; + m_c = (AF.B.B0 & 0x10) != 0; + UpdateData(FALSE); +} + +void GBDisassemble::update() +{ + OnGopc(); + refresh(); +} + +void GBDisassemble::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBDisassemble.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBDisassemble.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,69 @@ +#if !defined(AFX_GBDISASSEMBLE_H__3EFD5B47_6DBF_4F63_8F91_A9511EC590EB__INCLUDED_) +#define AFX_GBDISASSEMBLE_H__3EFD5B47_6DBF_4F63_8F91_A9511EC590EB__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GBDisassemble.h : header file +// + +#include "IUpdate.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// GBDisassemble dialog + +class GBDisassemble : public ResizeDlg, IUpdateListener +{ + // Construction +public: + //bool autoUpdate; // If these two variables are defined here, the dialog will crash! + //u16 address; // There might be a bug within MFC. + virtual void update(); + void refresh(); + GBDisassemble(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBDisassemble) + enum { IDD = IDD_GB_DISASSEMBLE }; + CEdit m_address; + CListBox m_list; + BOOL m_c; + BOOL m_h; + BOOL m_n; + BOOL m_z; + //}}AFX_DATA + bool autoUpdate; + int count; + u16 address; + u16 lastAddress; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBDisassemble) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GBDisassemble) + afx_msg void OnClose(); + afx_msg void OnRefresh(); + afx_msg void OnNext(); + afx_msg void OnGo(); + afx_msg void OnGopc(); + afx_msg void OnAutoUpdate(); + virtual BOOL OnInitDialog(); + afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBDISASSEMBLE_H__3EFD5B47_6DBF_4F63_8F91_A9511EC590EB__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBMapView.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBMapView.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,589 @@ +// GBMapView.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBMapView.h" +#include "FileDlg.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "VBA.h" + +//#include "../common/System.h" +#include "../gb/gbGlobals.h" +#include "../NLS.h" +#include "../common/Util.h" + +extern "C" { +#include +} + +extern u8 gbInvertTab[256]; + +///////////////////////////////////////////////////////////////////////////// +// GBMapView dialog + +GBMapView::GBMapView(CWnd*pParent /*=NULL*/) + : ResizeDlg(GBMapView::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBMapView) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 1024; + bmpInfo.bmiHeader.biHeight = -1024; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 1024 * 1024); + + mapView.setData(data); + mapView.setBmpInfo(&bmpInfo); + + bg = 0; + bank = 0; +} + +void GBMapView::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBMapView) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_MAP_VIEW, mapView); + DDX_Control(pDX, IDC_MAP_VIEW_ZOOM, mapViewZoom); + DDX_Control(pDX, IDC_COLOR, color); +} + +BEGIN_MESSAGE_MAP(GBMapView, CDialog) +//{{AFX_MSG_MAP(GBMapView) +ON_BN_CLICKED(IDC_SAVE, OnSave) +ON_BN_CLICKED(IDC_REFRESH, OnRefresh) +ON_BN_CLICKED(IDC_BG0, OnBg0) +ON_BN_CLICKED(IDC_BG1, OnBg1) +ON_BN_CLICKED(IDC_BANK_0, OnBank0) +ON_BN_CLICKED(IDC_BANK_1, OnBank1) +ON_BN_CLICKED(IDC_STRETCH, OnStretch) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +//}}AFX_MSG_MAP +ON_MESSAGE(WM_MAPINFO, OnMapInfo) +ON_MESSAGE(WM_COLINFO, OnColInfo) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBMapView message handlers + +GBMapView::~GBMapView() +{ + free(data); + data = NULL; +} + +void GBMapView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct + { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + +void GBMapView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if (!png_ptr) + { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + if (setjmp(png_ptr->jmpbuf)) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr, fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void GBMapView::OnSave() +{ + CString filename; + + if (theApp.captureFormat == 0) + filename = "map.png"; + else + filename = "map.bmp"; + + LPCTSTR exts[] = {".png", ".bmp", NULL }; + CString filter = winResLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + filename, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + if (dlg.getFilterIndex() == 2) + saveBMP(dlg.GetPathName()); + else + savePNG(dlg.GetPathName()); +} + +void GBMapView::render() +{ + u8 *bank0; + u8 *bank1; + if (gbCgbMode) + { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } + else + { + bank0 = &gbMemory[0x8000]; + bank1 = NULL; + } + + int tile_map_address = 0x1800; + if (bg == 1) + tile_map_address = 0x1c00; + + int tile_pattern = 0x0000; + if (bank == 1) + tile_pattern = 0x0800; + + w = 256; + h = 256; + + int tile = 0; + for (int y = 0; y < 32; y++) + { + for (int x = 0; x < 32; x++) + { + u8 *bmp = &data[y * 8 * 32 * 24 + x*24]; + u8 attrs = 0; + if (bank1 != NULL) + attrs = bank1[tile_map_address]; + u8 tile = bank0[tile_map_address]; + tile_map_address++; + + if (bank == 1) + { + if (tile < 128) + tile += 128; + else + tile -= 128; + } + for (int j = 0; j < 8; j++) + { + int tile_pattern_address = attrs & 0x40 ? + tile_pattern + tile*16 + (7-j)*2 : + tile_pattern + tile*16+j*2; + + u8 tile_a = 0; + u8 tile_b = 0; + + if (attrs & 0x08) + { + tile_a = bank1[tile_pattern_address++]; + tile_b = bank1[tile_pattern_address]; + } + else + { + tile_a = bank0[tile_pattern_address++]; + tile_b = bank0[tile_pattern_address]; + } + + if (attrs & 0x20) + { + tile_a = gbInvertTab[tile_a]; + tile_b = gbInvertTab[tile_b]; + } + + u8 mask = 0x80; + + while (mask > 0) + { + u8 c = (tile_a & mask) ? 1 : 0; + c += (tile_b & mask) ? 2 : 0; + + if (gbCgbMode) + c = c + (attrs & 7)*4; + + u16 color = gbPalette[c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + + mask >>= 1; + } + bmp += 31*24; + } + } + } +} + +void GBMapView::paint() +{ + if (gbRom == NULL) + return; + render(); + + SIZE s; + if (mapView.getStretch()) + { + mapView.setSize(w, h); + s.cx = s.cy = 1; + mapView.SetScrollSizes(MM_TEXT, s); + } + else + { + mapView.setSize(w, h); + s.cx = w; + s.cy = h; + mapView.SetScrollSizes(MM_TEXT, s); + } + + mapView.refresh(); +} + +void GBMapView::OnRefresh() +{ + paint(); +} + +void GBMapView::update() +{ + paint(); +} + +BOOL GBMapView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_MAP_VIEW, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_B, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBMapView", + NULL); + + int s = regQueryDwordValue("mapViewStretch", 0); + if (s) + mapView.setStretch(true); + ((CButton *)GetDlgItem(IDC_STRETCH))->SetCheck(s); + + UINT id = IDC_BANK_0; + if (bank == 1) + id = IDC_BANK_1; + CheckRadioButton(IDC_BANK_0, IDC_BANK_1, id); + id = IDC_BG0; + if (bg == 1) + id = IDC_BG1; + CheckRadioButton(IDC_BG0, IDC_BG1, id); + paint(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBMapView::OnBg0() +{ + bg = 0; + paint(); +} + +void GBMapView::OnBg1() +{ + bg = 1; + paint(); +} + +void GBMapView::OnBank0() +{ + bank = 0; + paint(); +} + +void GBMapView::OnBank1() +{ + bank = 1; + paint(); +} + +void GBMapView::OnStretch() +{ + mapView.setStretch(!mapView.getStretch()); + paint(); + regSetDwordValue("mapViewStretch", mapView.getStretch()); +} + +void GBMapView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void GBMapView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +u32 GBMapView::GetClickAddress(int x, int y) +{ + u32 base = 0x9800; + if (bg == 1) + base = 0x9c00; + + return base + (y >> 3)*32 + (x >> 3); +} + +LRESULT GBMapView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + mapViewZoom.setColors(colors); + + int x = wParam & 0xffff; + int y = (wParam >> 16); + + CString buffer; + buffer.Format("(%d,%d)", x, y); + GetDlgItem(IDC_XY)->SetWindowText(buffer); + + u32 address = GetClickAddress(x, y); + buffer.Format("0x%08X", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + u8 attrs = 0; + + u8 tile = gbReadMemoryQuick(0x9000 | (address & 0xfff)); + if (gbCgbMode) + { + attrs = gbVram[0x2000 + address - 0x8000]; + tile = gbVram[address & 0x1fff]; + } + + if (bank == 1) + { + if (tile > 128) + tile -= 128; + else + tile += 128; + } + + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE_NUM)->SetWindowText(buffer); + + buffer.Empty(); + buffer += attrs & 0x20 ? 'H' : '-'; + buffer += attrs & 0x40 ? 'V' : '-'; + GetDlgItem(IDC_FLIP)->SetWindowText(buffer); + + if (gbCgbMode) + { + buffer.Format("%d", (attrs & 7)); + } + else + buffer = "---"; + GetDlgItem(IDC_PALETTE_NUM)->SetWindowText(buffer); + + buffer.Empty(); + if (gbCgbMode) + buffer += attrs & 0x80 ? 'P' : '-'; + else + buffer += '-'; + GetDlgItem(IDC_PRIORITY)->SetWindowText(buffer); + + return TRUE; +} + +LRESULT GBMapView::OnColInfo(WPARAM wParam, LPARAM) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void GBMapView::PostNcDestroy() +{ + delete this; + CDialog::PostNcDestroy(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBMapView.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBMapView.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,81 @@ +#if !defined(AFX_GBMAPVIEW_H__4CD23D38_F2CD_4B95_AE76_2781591DD077__INCLUDED_) +#define AFX_GBMAPVIEW_H__4CD23D38_F2CD_4B95_AE76_2781591DD077__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GBMapView.h : header file +// + +#include "BitmapControl.h" +#include "ColorControl.h" +#include "ZoomControl.h" +#include "ResizeDlg.h" +#include "IUpdate.h" + +///////////////////////////////////////////////////////////////////////////// +// GBMapView dialog + +class GBMapView : public ResizeDlg, IUpdateListener +{ +private: + BITMAPINFO bmpInfo; + u8 * data; + int bank; + int bg; + int w; + int h; + BitmapControl mapView; + ZoomControl mapViewZoom; + ColorControl color; + bool autoUpdate; + // Construction +public: + afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + u32 GetClickAddress(int x, int y); + void update(); + void paint(); + void render(); + void savePNG(const char *name); + void saveBMP(const char *name); + ~GBMapView(); + GBMapView(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBMapView) + enum { IDD = IDD_GB_MAP_VIEW }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBMapView) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GBMapView) + afx_msg void OnSave(); + afx_msg void OnRefresh(); + virtual BOOL OnInitDialog(); + afx_msg void OnBg0(); + afx_msg void OnBg1(); + afx_msg void OnBank0(); + afx_msg void OnBank1(); + afx_msg void OnStretch(); + afx_msg void OnAutoUpdate(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBMAPVIEW_H__4CD23D38_F2CD_4B95_AE76_2781591DD077__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBMemoryViewerDlg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBMemoryViewerDlg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,448 @@ +// GBMemoryViewerDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBMemoryViewerDlg.h" +#include "FileDlg.h" +#include "MemoryViewerAddressSize.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "VBA.h" + +//#include "../common/System.h" +#include "../gb/gbGlobals.h" + +GBMemoryViewer::GBMemoryViewer() + : MemoryViewer() +{ + setAddressSize(1); +} + +void GBMemoryViewer::readData(u32 address, int len, u8 *data) +{ + u16 addr = address & 0xffff; + if (emulating && gbRom != NULL) + { + for (int i = 0; i < len; i++) + { + *data++ = gbReadMemoryQuick(addr); + addr++; + } + } + else + { + for (int i = 0; i < len; i++) + { + *data++ = 0; + addr++; + } + } +} + +void GBMemoryViewer::editData(u32 address, int size, int mask, u32 value) +{ + u32 oldValue; + u16 addr = (u16)address & 0xffff; + switch (size) + { + case 8: + oldValue = gbReadMemoryQuick(addr); + oldValue &= mask; + oldValue |= (u8)value; + gbWriteMemoryQuick(addr, oldValue); + break; + case 16: + oldValue = gbReadMemoryQuick(addr) | + (gbReadMemoryQuick(addr + 1) << 8); + oldValue &= mask; + oldValue |= (u16)value; + gbWriteMemoryQuick(addr, (oldValue & 255)); + gbWriteMemoryQuick(addr+1, (oldValue >> 8)); + break; + case 32: + oldValue = gbReadMemoryQuick(addr) | + (gbReadMemoryQuick(addr + 1) << 8) | + (gbReadMemoryQuick(addr + 2) << 16) | + (gbReadMemoryQuick(addr + 3) << 24); + oldValue &= mask; + oldValue |= (u32)value; + gbWriteMemoryQuick(addr, (oldValue & 255)); + gbWriteMemoryQuick(addr+1, (oldValue >> 8)); + gbWriteMemoryQuick(addr+2, (oldValue >> 16)); + gbWriteMemoryQuick(addr+3, (oldValue >> 24)); + break; + } +} + +///////////////////////////////////////////////////////////////////////////// +// GBMemoryViewerDlg dialog + +GBMemoryViewerDlg::GBMemoryViewerDlg(CWnd*pParent /*=NULL*/) + : ResizeDlg(GBMemoryViewerDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBMemoryViewerDlg) + m_size = -1; + //}}AFX_DATA_INIT + autoUpdate = false; +} + +void GBMemoryViewerDlg::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBMemoryViewerDlg) + DDX_Control(pDX, IDC_CURRENT_ADDRESS, m_current); + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_ADDRESSES, m_addresses); + DDX_Radio(pDX, IDC_8_BIT, m_size); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_VIEWER, m_viewer); +} + +BEGIN_MESSAGE_MAP(GBMemoryViewerDlg, CDialog) +//{{AFX_MSG_MAP(GBMemoryViewerDlg) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_BN_CLICKED(IDC_REFRESH, OnRefresh) +ON_BN_CLICKED(IDC_8_BIT, On8Bit) +ON_BN_CLICKED(IDC_16_BIT, On16Bit) +ON_BN_CLICKED(IDC_32_BIT, On32Bit) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_DECIMAL_DISPLAY, OnDecimalDisplay) +ON_BN_CLICKED(IDC_ALIGN, OnAlign) +ON_BN_CLICKED(IDC_GO, OnGo) +ON_CBN_SELCHANGE(IDC_ADDRESSES, OnSelchangeAddresses) +ON_BN_CLICKED(IDC_SAVE, OnSave) +ON_BN_CLICKED(IDC_LOAD, OnLoad) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBMemoryViewerDlg message handlers + +BOOL GBMemoryViewerDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + autoUpdate = !regQueryDwordValue("memViewerAutoUpdate", 1); + OnAutoUpdate(); // inverts and update dialog + + decimalDisplay = !regQueryDwordValue("memViewerDecimalDisplay", 0); + OnDecimalDisplay(); + + align = !regQueryDwordValue("memViewerAlign", 0); + OnAlign(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_VIEWER, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_LOAD, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_AUTO_UPDATE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_DECIMAL_DISPLAY, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_ALIGN, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CURRENT_ADDRESS_LABEL, DS_MoveY | DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_CURRENT_ADDRESS, DS_MoveY | DS_MoveX) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBMemoryView", + NULL); + + m_viewer.setDialog(this); + m_viewer.ShowScrollBar(SB_VERT, TRUE); + m_viewer.EnableScrollBar(SB_VERT, ESB_ENABLE_BOTH); + + LPCTSTR s[] = { + "0x0000 - ROM", + "0x4000 - ROM", + "0x8000 - VRAM", + "0xA000 - SRAM", + "0xC000 - RAM", + "0xD000 - WRAM", + "0xFF00 - I/O", + "0xFF80 - RAM" + }; + + for (int i = 0; i < 8; i++) + m_addresses.AddString(s[i]); + + m_addresses.SetCurSel(0); + + RECT cbSize; + int Height; + + m_addresses.GetClientRect(&cbSize); + Height = m_addresses.GetItemHeight(-1); + Height += m_addresses.GetItemHeight(0) * (9); + + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 + // Now add on the height of the border of the edit box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // The height of the border of the drop-down box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // now set the size of the window + m_addresses.SetWindowPos(NULL, + 0, 0, + cbSize.right, Height, + SWP_NOMOVE | SWP_NOZORDER); + + m_address.LimitText(8); + + m_size = regQueryDwordValue("memViewerDataSize", 0); + if (m_size < 0 || m_size > 2) + m_size = 0; + m_viewer.setSize(m_size); + UpdateData(FALSE); + + m_current.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT))); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBMemoryViewerDlg::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void GBMemoryViewerDlg::OnRefresh() +{ + m_viewer.Invalidate(); +} + +void GBMemoryViewerDlg::update() +{ + OnRefresh(); +} + +void GBMemoryViewerDlg::On8Bit() +{ + m_viewer.setSize(0); + regSetDwordValue("memViewerDataSize", 0); +} + +void GBMemoryViewerDlg::On16Bit() +{ + m_viewer.setSize(1); + regSetDwordValue("memViewerDataSize", 1); +} + +void GBMemoryViewerDlg::On32Bit() +{ + m_viewer.setSize(2); + regSetDwordValue("memViewerDataSize", 2); +} + +void GBMemoryViewerDlg::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } + if (GetDlgItem(IDC_AUTO_UPDATE)) + ((CButton *)GetDlgItem(IDC_AUTO_UPDATE))->SetCheck(autoUpdate ? TRUE : FALSE); + regSetDwordValue("memViewerAutoUpdate", autoUpdate); +} + +void GBMemoryViewerDlg::OnDecimalDisplay() +{ + decimalDisplay = !decimalDisplay; + m_viewer.setDecimal(decimalDisplay ? true : false); + if (GetDlgItem(IDC_DECIMAL_DISPLAY)) + ((CButton *)GetDlgItem(IDC_DECIMAL_DISPLAY))->SetCheck(decimalDisplay ? TRUE : FALSE); + + regSetDwordValue("memViewerDecimalDisplay", decimalDisplay); +} + +void GBMemoryViewerDlg::OnAlign() +{ + align = !align; + if (GetDlgItem(IDC_ALIGN)) + ((CButton *)GetDlgItem(IDC_ALIGN))->SetCheck(align ? TRUE : FALSE); + + regSetDwordValue("memViewerAlign", align); +} + +void GBMemoryViewerDlg::OnGo() +{ + CString buffer; + + m_address.GetWindowText(buffer); + + u32 address; + sscanf(buffer, "%x", &address); + if (align) + address &= ~0xF; + else + { + if (m_viewer.getSize() == 1) + address &= ~1; + else if (m_viewer.getSize() == 2) + address &= ~3; + } + m_viewer.setAddress(address); +} + +void GBMemoryViewerDlg::OnSelchangeAddresses() +{ + int cur = m_addresses.GetCurSel(); + + switch (cur) + { + case 0: + m_viewer.setAddress(0x0000); + break; + case 1: + m_viewer.setAddress(0x4000); + break; + case 2: + m_viewer.setAddress(0x8000); + break; + case 3: + m_viewer.setAddress(0xa000); + break; + case 4: + m_viewer.setAddress(0xc000); + break; + case 5: + m_viewer.setAddress(0xd000); + break; + case 6: + m_viewer.setAddress(0xff00); + break; + case 7: + m_viewer.setAddress(0xff80); + break; + } +} + +void GBMemoryViewerDlg::setCurrentAddress(u32 address) +{ + CString buffer; + + buffer.Format("0x%08X", address); + m_current.SetWindowText(buffer); +} + +void GBMemoryViewerDlg::OnSave() +{ + MemoryViewerAddressSize dlg; + + dlg.setAddress(m_viewer.getCurrentAddress()); + + LPCTSTR exts[] = { ".dmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_DUMP); + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); + + if (dlg.DoModal() == IDOK) + { + CString buffer; + FileDlg file(this, + buffer, + filter, + 0, + "DMP", + exts, + "", + title, + true); + if (file.DoModal() == IDOK) + { + buffer = file.GetPathName(); + FILE *f = fopen(buffer, "wb"); + + if (f == NULL) + { + systemMessage(IDS_ERROR_CREATING_FILE, buffer); + return; + } + + int size = dlg.getSize(); + u16 addr = dlg.getAddress() & 0xffff; + + for (int i = 0; i < size; i++) + { + fputc(gbReadMemoryQuick(addr), f); + addr++; + } + + fclose(f); + } + } +} + +void GBMemoryViewerDlg::OnLoad() +{ + CString buffer; + LPCTSTR exts[] = { ".dmp", NULL }; + CString filter = winResLoadFilter(IDS_FILTER_DUMP); + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); + + FileDlg file(this, + buffer, + filter, + 0, + "DMP", + exts, + "", + title, + false); + + if (file.DoModal() == IDOK) + { + buffer = file.GetPathName(); + FILE *f = fopen(buffer, "rb"); + if (f == NULL) + { + systemMessage(IDS_CANNOT_OPEN_FILE, + "Cannot open file %s", + buffer); + return; + } + + MemoryViewerAddressSize dlg; + + fseek(f, 0, SEEK_END); + int size = ftell(f); + + fseek(f, 0, SEEK_SET); + + dlg.setAddress(m_viewer.getCurrentAddress()); + dlg.setSize(size); + + if (dlg.DoModal() == IDOK) + { + int size = dlg.getSize(); + u16 addr = dlg.getAddress() & 0xffff; + + for (int i = 0; i < size; i++) + { + int c = fgetc(f); + if (c == -1) + break; + gbWriteMemoryQuick(addr, c); + addr++; + } + OnRefresh(); + } + fclose(f); + } +} + +void GBMemoryViewerDlg::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBMemoryViewerDlg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBMemoryViewerDlg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,79 @@ +#if !defined(AFX_GBMEMORYVIEWERDLG_H__23AD2804_EFA5_4900_AEC5_47196A41C50D__INCLUDED_) +#define AFX_GBMEMORYVIEWERDLG_H__23AD2804_EFA5_4900_AEC5_47196A41C50D__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GBMemoryViewerDlg.h : header file +// +#include "MemoryViewer.h" +#include "ResizeDlg.h" +#include "IUpdate.h" + +class GBMemoryViewer : public MemoryViewer +{ +public: + GBMemoryViewer(); + virtual void readData(u32, int, u8 *); + virtual void editData(u32, int, int, u32); +}; + +///////////////////////////////////////////////////////////////////////////// +// GBMemoryViewerDlg dialog + +class GBMemoryViewerDlg : public ResizeDlg, IUpdateListener, IMemoryViewerDlg +{ + GBMemoryViewer m_viewer; + bool autoUpdate; + int decimalDisplay; + int align; + // Construction +public: + void setCurrentAddress(u32 address); + GBMemoryViewerDlg(CWnd*pParent = NULL); // standard constructor + + virtual void update(); + + // Dialog Data + //{{AFX_DATA(GBMemoryViewerDlg) + enum { IDD = IDD_MEM_VIEWER }; + CEdit m_current; + CEdit m_address; + CComboBox m_addresses; + int m_size; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBMemoryViewerDlg) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GBMemoryViewerDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + afx_msg void OnRefresh(); + afx_msg void On8Bit(); + afx_msg void On16Bit(); + afx_msg void On32Bit(); + afx_msg void OnAutoUpdate(); + afx_msg void OnDecimalDisplay(); + afx_msg void OnAlign(); + afx_msg void OnGo(); + afx_msg void OnSelchangeAddresses(); + afx_msg void OnSave(); + afx_msg void OnLoad(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBMEMORYVIEWERDLG_H__23AD2804_EFA5_4900_AEC5_47196A41C50D__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBOamView.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBOamView.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,598 @@ +// GBOamView.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBOamView.h" +#include "FileDlg.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "VBA.h" + +//#include "../common/System.h" +#include "../gb/gbGlobals.h" +#include "../NLS.h" +#include "../common/Util.h" + +extern "C" { +#include +} + +///////////////////////////////////////////////////////////////////////////// +// GBOamView dialog + +GBOamView::GBOamView(CWnd*pParent /*=NULL*/) + : ResizeDlg(GBOamView::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBOamView) + m_stretch = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 8; + bmpInfo.bmiHeader.biHeight = 16; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 8 * 16); + + oamView.setData(data); + oamView.setBmpInfo(&bmpInfo); + + number = 0; +} + +void GBOamView::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBOamView) + DDX_Control(pDX, IDC_SPRITE, m_sprite); + DDX_Check(pDX, IDC_STRETCH, m_stretch); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_COLOR, color); + DDX_Control(pDX, IDC_OAM_VIEW, oamView); + DDX_Control(pDX, IDC_OAM_VIEW_ZOOM, oamZoom); +} + +BEGIN_MESSAGE_MAP(GBOamView, CDialog) +//{{AFX_MSG_MAP(GBOamView) +ON_BN_CLICKED(IDC_STRETCH, OnStretch) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_EN_CHANGE(IDC_SPRITE, OnChangeSprite) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_WM_HSCROLL() +//}}AFX_MSG_MAP +ON_MESSAGE(WM_MAPINFO, OnMapInfo) +ON_MESSAGE(WM_COLINFO, OnColInfo) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBOamView message handlers + +GBOamView::~GBOamView() +{ + free(data); + data = NULL; +} + +void GBOamView::paint() +{ + if (gbRom == NULL) + return; + + render(); + oamView.setSize(w, h); + oamView.refresh(); +} + +void GBOamView::update() +{ + paint(); +} + +void GBOamView::setAttributes(int y, int x, int tile, int flags) +{ + CString buffer; + + int flipH = flags & 0x20; + int flipV = flags & 0x40; + int prio = (flags & 0x80) >> 7; + int pal = flags & 0x7; + int oap = (flags & 0x08) >> 3; + int bank = (flags & 0x10) >> 4; + + buffer.Format("%d,%d", x, y); + GetDlgItem(IDC_POS)->SetWindowText(buffer); + + buffer.Format("%d", pal); + GetDlgItem(IDC_PALETTE)->SetWindowText(buffer); + + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE)->SetWindowText(buffer); + + buffer.Format("%d", prio); + GetDlgItem(IDC_PRIO)->SetWindowText(buffer); + + buffer.Format("%d", bank); + GetDlgItem(IDC_BANK)->SetWindowText(buffer); + + buffer.Empty(); + if (flipH) + buffer += 'H'; + else + buffer += ' '; + if (flipV) + buffer += 'V'; + else + buffer += ' '; + GetDlgItem(IDC_FLAGS)->SetWindowText(buffer); + + buffer.Format("%d", oap); + GetDlgItem(IDC_OAP)->SetWindowText(buffer); +} + +void GBOamView::render() +{ + int m = 0; + if (gbRom == NULL) + return; + + u16 addr = number * 4 + 0xfe00; + + int size = register_LCDC & 4; + + u8 y = gbReadMemoryQuick(addr++); + u8 x = gbReadMemoryQuick(addr++); + u8 tile = gbReadMemoryQuick(addr++); + if (size) + tile &= 254; + u8 flags = gbReadMemoryQuick(addr++); + + u8 *bmp = data; + + w = 8; + h = size ? 16 : 8; + + setAttributes(y, x, tile, flags); + + u8 *bank0; + u8 *bank1; + if (gbCgbMode) + { + if (register_VBK & 1) + { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } + else + { + bank0 = &gbVram[0x0000]; + bank1 = &gbVram[0x2000]; + } + } + else + { + bank0 = &gbMemory[0x8000]; + bank1 = NULL; + } + + int init = 0x0000; + + u8 *pal = gbObp0; + + if ((flags & 0x10)) + pal = gbObp1; + + for (int yy = 0; yy < h; yy++) + { + int address = init + tile * 16 + 2*yy; + int a = 0; + int b = 0; + + if (gbCgbMode && flags & 0x08) + { + a = bank1[address++]; + b = bank1[address++]; + } + else + { + a = bank0[address++]; + b = bank0[address++]; + } + + for (int xx = 0; xx < 8; xx++) + { + u8 mask = 1 << (7-xx); + u8 c = 0; + if ((a & mask)) + c++; + if ((b & mask)) + c += 2; + + // make sure that sprites will work even in CGB mode + if (gbCgbMode) + { + c = c + (flags & 0x07)*4 + 32; + } + else + { + c = pal[c]; + } + + u16 color = gbPalette[c]; + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + } +} + +void GBOamView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct + { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + +void GBOamView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if (!png_ptr) + { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + if (setjmp(png_ptr->jmpbuf)) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr, fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void GBOamView::save() +{ + CString captureBuffer; + + if (theApp.captureFormat == 0) + captureBuffer = "oam.png"; + else + captureBuffer = "oam.bmp"; + + LPCTSTR exts[] = {".png", ".bmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + captureBuffer = dlg.GetPathName(); + + if (dlg.getFilterIndex() == 2) + saveBMP(captureBuffer); + else + savePNG(captureBuffer); +} + +BOOL GBOamView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_OAM_VIEW, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_OAM_VIEW_ZOOM, DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_B, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBOamView", + NULL); + + m_sprite.SetWindowText("0"); + + updateScrollInfo(); + + m_stretch = regQueryDwordValue("GBOamViewStretch", 0); + if (m_stretch) + oamView.setStretch(true); + UpdateData(FALSE); + + paint(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBOamView::OnStretch() +{ + oamView.setStretch(!oamView.getStretch()); + paint(); + regSetDwordValue("GBOamViewStretch", oamView.getStretch()); +} + +void GBOamView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void GBOamView::OnChangeSprite() +{ + CString buffer; + m_sprite.GetWindowText(buffer); + int n = atoi(buffer); + if (n < 0 || n > 39) + { + buffer.Format("%d", number); + m_sprite.SetWindowText(buffer); + return; + } + number = n; + paint(); + updateScrollInfo(); +} + +void GBOamView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +LRESULT GBOamView::OnMapInfo(WPARAM, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + oamZoom.setColors(colors); + + return TRUE; +} + +LRESULT GBOamView::OnColInfo(WPARAM wParam, LPARAM lParam) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void GBOamView::updateScrollInfo() +{ + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL | SIF_POS; + si.nMin = 0; + si.nMax = 39; + si.nPage = 1; + si.nPos = number; + GetDlgItem(IDC_SCROLLBAR)->SetScrollInfo(SB_CTL, + &si, + TRUE); +} + +void GBOamView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar) +{ + switch (nSBCode) + { + case SB_BOTTOM: + number = 39; + break; + case SB_LINEDOWN: + number++; + if (number > 39) + number = 39; + break; + case SB_LINEUP: + number--; + if (number < 0) + number = 0; + break; + case SB_PAGEDOWN: + number += 16; + if (number > 39) + number = 39; + break; + case SB_PAGEUP: + number -= 16; + if (number < 0) + number = 0; + break; + case SB_TOP: + number = 0; + break; + case SB_THUMBTRACK: + number = nPos; + if (number < 0) + number = 0; + if (number > 39) + number = 39; + break; + } + + updateScrollInfo(); + + CString buffer; + buffer.Format("%d", number); + m_sprite.SetWindowText(buffer); + paint(); +} + +void GBOamView::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBOamView.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBOamView.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,82 @@ +#if !defined(AFX_GBOAMVIEW_H__FE8105E6_9693_479A_8C57_DEEA1B2EA3D6__INCLUDED_) +#define AFX_GBOAMVIEW_H__FE8105E6_9693_479A_8C57_DEEA1B2EA3D6__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GBOamView.h : header file +// + +#include "BitmapControl.h" +#include "ZoomControl.h" +#include "ColorControl.h" + +#include "IUpdate.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// GBOamView dialog + +class GBOamView : public ResizeDlg, IUpdateListener +{ +private: + BITMAPINFO bmpInfo; + u8 * data; + int w; + int h; + int number; + bool autoUpdate; + BitmapControl oamView; + ZoomControl oamZoom; + ColorControl color; + + // Construction +public: + void updateScrollInfo(); + void save(); + void savePNG(const char *name); + void saveBMP(const char *name); + void render(); + void setAttributes(int y, int x, int tile, int flags); + void paint(); + ~GBOamView(); + GBOamView(CWnd*pParent = NULL); // standard constructor + + afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + virtual void update(); + + // Dialog Data + //{{AFX_DATA(GBOamView) + enum { IDD = IDD_GB_OAM_VIEW }; + CEdit m_sprite; + BOOL m_stretch; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBOamView) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GBOamView) + virtual BOOL OnInitDialog(); + afx_msg void OnStretch(); + afx_msg void OnAutoUpdate(); + afx_msg void OnChangeSprite(); + afx_msg void OnClose(); + afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBOAMVIEW_H__FE8105E6_9693_479A_8C57_DEEA1B2EA3D6__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBPaletteView.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBPaletteView.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,219 @@ +// GBPaletteView.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBPaletteView.h" +#include "FileDlg.h" +#include "WinResUtil.h" +#include "VBA.h" + +#include "../gb/gbGlobals.h" + +void GBPaletteViewControl::updatePalette() +{ + if (gbRom) + { + memcpy(palette, &gbPalette[paletteAddress], 64); + } +} + +///////////////////////////////////////////////////////////////////////////// +// GBPaletteView dialog + +GBPaletteView::GBPaletteView(CWnd*pParent /*=NULL*/) + : ResizeDlg(GBPaletteView::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBPaletteView) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + autoUpdate = false; +} + +GBPaletteView::~GBPaletteView() +{} + +void GBPaletteView::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBPaletteView) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_PALETTE_VIEW, paletteView); + DDX_Control(pDX, IDC_PALETTE_VIEW_OBJ, paletteViewOBJ); + DDX_Control(pDX, IDC_COLOR, colorControl); +} + +BEGIN_MESSAGE_MAP(GBPaletteView, CDialog) +//{{AFX_MSG_MAP(GBPaletteView) +ON_BN_CLICKED(IDC_SAVE_BG, OnSaveBg) +ON_BN_CLICKED(IDC_SAVE_OBJ, OnSaveObj) +ON_BN_CLICKED(IDC_REFRESH2, OnRefresh2) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +//}}AFX_MSG_MAP +ON_MESSAGE(WM_PALINFO, OnPalInfo) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBPaletteView message handlers + +BOOL GBPaletteView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_END() + SetData(sz, + FALSE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBPaletteView", + NULL); + + paletteView.init(32, 64, 128); + paletteViewOBJ.init(32, 64, 128); + + paletteView.setPaletteAddress(0); + paletteView.refresh(); + + paletteViewOBJ.setPaletteAddress(32); + paletteViewOBJ.refresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBPaletteView::save(int which) +{ + CString captureBuffer; + + if (which == 0) + captureBuffer = "bg.pal"; + else + captureBuffer = "obj.pal"; + + LPCTSTR exts[] = {".pal", ".pal", ".act", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PAL); + CString title = winResLoadString(IDS_SELECT_PALETTE_NAME); + FileDlg dlg(this, + captureBuffer, + filter, + 1, + "PAL", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + PaletteViewControl *p = NULL; + + if (which == 0) + p = &paletteView; + else + p = &paletteViewOBJ; + + switch (dlg.getFilterIndex()) + { + case 0: + case 1: + p->saveMSPAL(captureBuffer); + break; + case 2: + p->saveJASCPAL(captureBuffer); + break; + case 3: + p->saveAdobe(captureBuffer); + break; + } +} + +void GBPaletteView::OnSaveBg() +{ + save(0); +} + +void GBPaletteView::OnSaveObj() +{ + save(1); +} + +void GBPaletteView::OnRefresh2() +{ + paletteView.refresh(); + paletteViewOBJ.refresh(); +} + +void GBPaletteView::update() +{ + OnRefresh2(); +} + +void GBPaletteView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void GBPaletteView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +LRESULT GBPaletteView::OnPalInfo(WPARAM wParam, LPARAM lParam) +{ + u16 color = (u16)wParam; + u32 address = (u32)lParam; + CString buffer; + + bool isOBJ = address >= 32; + address &= 31; + + buffer.Format("%d", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + int r = (color & 0x1f); + int g = (color & 0x3e0) >> 5; + int b = (color & 0x7c00) >> 10; + + buffer.Format("%d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("%d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("%d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + buffer.Format("0x%04X", color); + GetDlgItem(IDC_VALUE)->SetWindowText(buffer); + + colorControl.setColor(color); + + if (isOBJ) + paletteView.setSelected(-1); + else + paletteViewOBJ.setSelected(-1); + + return TRUE; +} + +void GBPaletteView::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBPaletteView.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBPaletteView.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,72 @@ +#if !defined(AFX_GBPALETTEVIEW_H__F909FF55_3021_4301_B017_0C2C9D8D8C08__INCLUDED_) +#define AFX_GBPALETTEVIEW_H__F909FF55_3021_4301_B017_0C2C9D8D8C08__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GBPaletteView.h : header file +// + +#include "ColorControl.h" +#include "IUpdate.h" +#include "PaletteViewControl.h" +#include "ResizeDlg.h" + +class GBPaletteViewControl : public PaletteViewControl +{ +public: + virtual void updatePalette(); +}; + +///////////////////////////////////////////////////////////////////////////// +// GBPaletteView dialog + +class GBPaletteView : public ResizeDlg, IUpdateListener +{ +private: + GBPaletteViewControl paletteView; + GBPaletteViewControl paletteViewOBJ; + ColorControl colorControl; + bool autoUpdate; + // Construction +public: + void save(int which); + GBPaletteView(CWnd*pParent = NULL); // standard constructor + virtual ~GBPaletteView(); + + // Dialog Data + //{{AFX_DATA(GBPaletteView) + enum { IDD = IDD_GB_PALETTE_VIEW }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBPaletteView) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + virtual void update(); + + // Implementation +protected: + afx_msg virtual LRESULT OnPalInfo(WPARAM wParam, LPARAM lParam); + // Generated message map functions + //{{AFX_MSG(GBPaletteView) + virtual BOOL OnInitDialog(); + afx_msg void OnSaveBg(); + afx_msg void OnSaveObj(); + afx_msg void OnRefresh2(); + afx_msg void OnAutoUpdate(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBPALETTEVIEW_H__F909FF55_3021_4301_B017_0C2C9D8D8C08__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBPrinterDlg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBPrinterDlg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,485 @@ +// GBPrinter.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBPrinterDlg.h" +#include "FileDlg.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "VBA.h" + +//#include "../common/System.h" +#include "../NLS.h" +#include "../common/Util.h" + +extern "C" { +#include +} + +///////////////////////////////////////////////////////////////////////////// +// GBPrinter dialog + +GBPrinterDlg::GBPrinterDlg(CWnd*pParent /*=NULL*/) + : CDialog(GBPrinterDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBPrinterDlg) + m_scale = -1; + //}}AFX_DATA_INIT + bitmap = (BITMAPINFO *)bitmapHeader; + + bitmap->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bitmap->bmiHeader.biWidth = 160; + bitmap->bmiHeader.biHeight = -144; + bitmap->bmiHeader.biPlanes = 1; + bitmap->bmiHeader.biBitCount = 8; + bitmap->bmiHeader.biCompression = BI_RGB; + bitmap->bmiHeader.biSizeImage = 160*144; + bitmap->bmiHeader.biXPelsPerMeter = 0; + bitmap->bmiHeader.biYPelsPerMeter = 0; + bitmap->bmiHeader.biClrUsed = 4; + bitmap->bmiHeader.biClrImportant = 4; + bitmap->bmiColors[0].rgbBlue = + bitmap->bmiColors[0].rgbGreen = + bitmap->bmiColors[0].rgbRed = + 255; + bitmap->bmiColors[0].rgbReserved = 0; + bitmap->bmiColors[1].rgbBlue = + bitmap->bmiColors[1].rgbGreen = + bitmap->bmiColors[1].rgbRed = + 168; + bitmap->bmiColors[1].rgbReserved = 0; + bitmap->bmiColors[2].rgbBlue = + bitmap->bmiColors[2].rgbGreen = + bitmap->bmiColors[2].rgbRed = + 96; + bitmap->bmiColors[2].rgbReserved = 0; + bitmap->bmiColors[3].rgbBlue = + bitmap->bmiColors[3].rgbGreen = + bitmap->bmiColors[3].rgbRed = + 0; + bitmap->bmiColors[3].rgbReserved = 0; +} + +void GBPrinterDlg::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBPrinterDlg) + DDX_Radio(pDX, IDC_1X, m_scale); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GBPrinterDlg, CDialog) +//{{AFX_MSG_MAP(GBPrinterDlg) +ON_BN_CLICKED(ID_SAVE, OnSave) +ON_BN_CLICKED(ID_PRINT, OnPrint) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(IDC_1X, On1x) +ON_BN_CLICKED(IDC_2X, On2x) +ON_BN_CLICKED(IDC_3X, On3x) +ON_BN_CLICKED(IDC_4X, On4x) +ON_WM_PAINT() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBPrinter message handlers + +void GBPrinterDlg::saveAsBMP(const char *name) +{ + u8 writeBuffer[512 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct + { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + 160*144*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, 160); + utilPutDword(bmpheader.height, 144); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 160*144); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + u8 *data = (u8 *)bitmapData; + u8 *p = data + (160*143); + for (int y = 0; y < 144; y++) + { + for (int x = 0; x < 160; x++) + { + u8 c = *p++; + + *b++ = bitmap->bmiColors[c].rgbBlue; + *b++ = bitmap->bmiColors[c].rgbGreen; + *b++ = bitmap->bmiColors[c].rgbRed; + } + p -= 2*(160); + fwrite(writeBuffer, 1, 3*160, fp); + + b = writeBuffer; + } + + fclose(fp); +} + +void GBPrinterDlg::saveAsPNG(const char *name) +{ + u8 writeBuffer[160 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", + name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if (!png_ptr) + { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + if (setjmp(png_ptr->jmpbuf)) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr, fp); + + png_set_IHDR(png_ptr, + info_ptr, + 160, + 144, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + u8 *b = writeBuffer; + + int sizeX = 160; + int sizeY = 144; + + u8 *pixU8 = (u8 *)bitmapData; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u8 c = *pixU8++; + *b++ = bitmap->bmiColors[c].rgbRed; + *b++ = bitmap->bmiColors[c].rgbGreen; + *b++ = bitmap->bmiColors[c].rgbBlue; + } + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void GBPrinterDlg::OnSave() +{ + CString captureBuffer; + + if (theApp.captureFormat == 0) + captureBuffer = "printer.png"; + else + captureBuffer = "printer.bmp"; + + LPCTSTR exts[] = {".png", ".bmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + captureBuffer = dlg.GetPathName(); + + if (dlg.getFilterIndex() == 2) + saveAsBMP(captureBuffer); + else + saveAsPNG(captureBuffer); +} + +void GBPrinterDlg::OnPrint() +{ + CPrintDialog dlg(FALSE); + + dlg.m_pd.nFromPage = 1; + dlg.m_pd.nToPage = 1; + dlg.m_pd.nMinPage = 1; + dlg.m_pd.nMaxPage = 1; + dlg.m_pd.nCopies = 1; + + if (dlg.DoModal() == IDOK) + { + DOCINFO di; + float fLogPelsX1 = 0; + float fLogPelsX2 = 0; + float fLogPelsY1 = 0; + float fLogPelsY2 = 0; + float fScaleX = 0, fScaleY = 0; + CDC * winDC = NULL; + memset(&di, 0, sizeof(di)); + di.cbSize = sizeof(DOCINFO); + CString docName = winResLoadString(IDS_POCKET_PRINTER); + di.lpszDocName = docName; + CDC dc; + dc.Attach(dlg.GetPrinterDC()); + int nError = dc.StartDoc(&di); + + if (nError == SP_ERROR) + { + systemMessage(IDS_ERROR_ON_STARTDOC, "Error on StartDoc"); + goto error; + } + nError = dc.StartPage(); + if (nError <= 0) + { + systemMessage(IDS_ERROR_ON_STARTPAGE, "Error on StartPage"); + goto error; + } + + winDC = GetDC(); + fLogPelsX1 = (float)winDC->GetDeviceCaps(LOGPIXELSX); + fLogPelsY1 = (float)winDC->GetDeviceCaps(LOGPIXELSY); + ReleaseDC(winDC); + + fLogPelsX2 = (float)dc.GetDeviceCaps(LOGPIXELSX); + fLogPelsY2 = (float)dc.GetDeviceCaps(LOGPIXELSY); + + if (fLogPelsX1 > fLogPelsX2) + fScaleX = fLogPelsX1 / fLogPelsX2; + else + fScaleX = fLogPelsX2 / fLogPelsX1; + + if (fLogPelsY1 > fLogPelsY2) + fScaleY = fLogPelsY1 / fLogPelsY2; + else + fScaleY = fLogPelsY2 / fLogPelsY1; + + fScaleX *= (scale+1); + fScaleY *= (scale+1); + + if (StretchDIBits(dc, + 0, + 0, + (int)((float)160*fScaleX), + (int)((float)144*fScaleY), + 0, + 0, + 160, + 144, + bitmapData, + bitmap, + DIB_RGB_COLORS, + SRCCOPY) == GDI_ERROR) + { + systemMessage(IDS_ERROR_PRINTING_ON_STRETCH, + "Error printing on StretchDIBits"); + } + + nError = dc.EndPage(); + + if (nError <= 0) + { + systemMessage(IDS_ERROR_ON_ENDPAGE, "Error on EndPage"); + goto error; + } + + nError = dc.EndDoc(); + + if (nError <= 0) + systemMessage(IDS_ERROR_ON_ENDDOC, "Error on EndDoc"); +error: + dc.DeleteDC(); + } +} + +void GBPrinterDlg::processData(u8 *data) +{ + for (int y = 0; y < 0x12; y++) + { + for (int x = 0; x < 0x14; x++) + { + for (int k = 0; k < 8; k++) + { + int a = *data++; + int b = *data++; + for (int j = 0; j < 8; j++) + { + int mask = 1 << (7-j); + int c = 0; + if (a & mask) + c++; + if (b & mask) + c += 2; + bitmapData[x*8+j + 160*(y*8+k)] = c; + } + } + } + } +} + +BOOL GBPrinterDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + scale = regQueryDwordValue("printerScale", 0); + if (scale < 0 || scale > 3) + scale = 0; + m_scale = scale; + UpdateData(FALSE); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBPrinterDlg::OnOk() +{ + EndDialog(TRUE); +} + +void GBPrinterDlg::On1x() +{ + regSetDwordValue("printerScale", 0); + scale = 0; +} + +void GBPrinterDlg::On2x() +{ + regSetDwordValue("printerScale", 1); + scale = 1; +} + +void GBPrinterDlg::On3x() +{ + regSetDwordValue("printerScale", 2); + scale = 2; +} + +void GBPrinterDlg::On4x() +{ + regSetDwordValue("printerScale", 3); + scale = 3; +} + +void GBPrinterDlg::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + RECT rect; + CWnd *h = GetDlgItem(IDC_GB_PRINTER); + h->GetWindowRect(&rect); + POINT p; + p.x = rect.left; + p.y = rect.top; + ScreenToClient((POINT *)&p); + rect.left = p.x+1; + rect.top = p.y+1; + p.x = rect.right; + p.y = rect.bottom; + ScreenToClient((POINT *)&p); + rect.right = p.x-1; + rect.bottom = p.y-1; + + StretchDIBits(dc, + rect.left, + rect.top, + rect.right - rect.left, + rect.bottom - rect.top, + 0, + 0, + 160, + 144, + bitmapData, + bitmap, + DIB_RGB_COLORS, + SRCCOPY); +} + +void systemGbPrint(u8 *data, + int pages, + int feed, + int palette, + int contrast) +{ + theApp.winCheckFullscreen(); + GBPrinterDlg printer; + printer.processData(data); + printer.DoModal(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBPrinterDlg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBPrinterDlg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,61 @@ +#if !defined(AFX_GBPRINTER_H__3180CC5A_1F9D_47E5_B044_407442CB40A4__INCLUDED_) +#define AFX_GBPRINTER_H__3180CC5A_1F9D_47E5_B044_407442CB40A4__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GBPrinter.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GBPrinter dialog + +class GBPrinterDlg : public CDialog +{ +private: + u8 bitmapHeader[sizeof(BITMAPINFO)+4*sizeof(RGBQUAD)]; + BITMAPINFO *bitmap; + u8 bitmapData[160*144]; + int scale; + // Construction +public: + void processData(u8 *data); + void saveAsPNG(const char *name); + void saveAsBMP(const char *name); + GBPrinterDlg(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GBPrinterDlg) + enum { IDD = IDD_GB_PRINTER }; + int m_scale; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBPrinterDlg) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GBPrinterDlg) + afx_msg void OnSave(); + afx_msg void OnPrint(); + virtual BOOL OnInitDialog(); + afx_msg void OnOk(); + afx_msg void On1x(); + afx_msg void On2x(); + afx_msg void On3x(); + afx_msg void On4x(); + afx_msg void OnPaint(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBPRINTER_H__3180CC5A_1F9D_47E5_B044_407442CB40A4__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBTileView.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBTileView.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,517 @@ +// GBTileView.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GBTileView.h" +#include "FileDlg.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "VBA.h" + +//#include "../common/System.h" +#include "../gb/gbGlobals.h" +#include "../NLS.h" +#include "../common/Util.h" + +extern "C" { +#include +} + +///////////////////////////////////////////////////////////////////////////// +// GBTileView dialog + +GBTileView::GBTileView(CWnd*pParent /*=NULL*/) + : ResizeDlg(GBTileView::IDD, pParent) +{ + //{{AFX_DATA_INIT(GBTileView) + m_charBase = -1; + m_bank = -1; + m_stretch = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo, 0, sizeof(bmpInfo)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 32*8; + bmpInfo.bmiHeader.biHeight = 32*8; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 32*32 * 64); + + tileView.setData(data); + tileView.setBmpInfo(&bmpInfo); + + charBase = 0; + palette = 0; + bank = 0; + w = h = 0; +} + +GBTileView::~GBTileView() +{ + free(data); + data = NULL; +} + +void GBTileView::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GBTileView) + DDX_Control(pDX, IDC_PALETTE_SLIDER, m_slider); + DDX_Radio(pDX, IDC_CHARBASE_0, m_charBase); + DDX_Radio(pDX, IDC_BANK_0, m_bank); + DDX_Check(pDX, IDC_STRETCH, m_stretch); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_TILE_VIEW, tileView); + DDX_Control(pDX, IDC_MAP_VIEW_ZOOM, zoom); + DDX_Control(pDX, IDC_COLOR, color); +} + +BEGIN_MESSAGE_MAP(GBTileView, CDialog) +//{{AFX_MSG_MAP(GBTileView) +ON_BN_CLICKED(IDC_SAVE, OnSave) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_CHARBASE_0, OnCharbase0) +ON_BN_CLICKED(IDC_CHARBASE_1, OnCharbase1) +ON_BN_CLICKED(IDC_BANK_0, OnBank0) +ON_BN_CLICKED(IDC_BANK_1, OnBank1) +ON_BN_CLICKED(IDC_STRETCH, OnStretch) +ON_WM_HSCROLL() +//}}AFX_MSG_MAP +ON_MESSAGE(WM_MAPINFO, OnMapInfo) +ON_MESSAGE(WM_COLINFO, OnColInfo) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GBTileView message handlers + +void GBTileView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct + { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + +void GBTileView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if (!png_ptr) + { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + if (setjmp(png_ptr->jmpbuf)) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr, fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void GBTileView::OnSave() +{ + CString captureBuffer; + + if (theApp.captureFormat == 0) + captureBuffer = "tiles.png"; + else + captureBuffer = "tiles.bmp"; + + LPCTSTR exts[] = {".png", ".bmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + captureBuffer = dlg.GetPathName(); + + if (theApp.captureFormat) + saveBMP(captureBuffer); + else + savePNG(captureBuffer); +} + +void GBTileView::renderTile(int tile, int x, int y, u8 *charBase) +{ + u8 *bmp = &data[24*x + 8*16*24*y]; + + for (int j = 0; j < 8; j++) + { + u8 mask = 0x80; + u8 tile_a = charBase[tile*16+j*2]; + u8 tile_b = charBase[tile*16+j*2+1]; + + for (int i = 0; i < 8; i++) + { + u8 c = (tile_a & mask) ? 1 : 0; + c += ((tile_b & mask) ? 2 : 0); + + if (gbCgbMode) + { + c = c + palette*4; + } + else + { + c = gbBgp[c]; + } + + u16 color = gbPalette[c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + + mask >>= 1; + } + bmp += 15*24; // advance line + } +} + +void GBTileView::render() +{ + int tiles = 0x0000; + if (charBase) + tiles = 0x0800; + u8 *charBase = (gbVram != NULL) ? + (bank ? &gbVram[0x2000+tiles] : &gbVram[tiles]) : + &gbMemory[0x8000+tiles]; + + int tile = 0; + for (int y = 0; y < 16; y++) + { + for (int x = 0; x < 16; x++) + { + renderTile(tile, x, y, charBase); + tile++; + } + } + tileView.setSize(16*8, 16*8); + w = 16*8; + h = 16*8; + SIZE s; + s.cx = s.cy = 16*8; + if (tileView.getStretch()) + { + s.cx = s.cy = 1; + } + tileView.SetScrollSizes(MM_TEXT, s); +} + +void GBTileView::update() +{ + paint(); +} + +BOOL GBTileView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_TILE_VIEW, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_B, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBTileView", + NULL); + + m_charBase = charBase; + m_bank = bank; + + m_slider.SetRange(0, 7); + m_slider.SetPageSize(2); + m_slider.SetTicFreq(1); + paint(); + + m_stretch = regQueryDwordValue("tileViewStretch", 0); + if (m_stretch) + tileView.setStretch(true); + UpdateData(FALSE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GBTileView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void GBTileView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void GBTileView::paint() +{ + if (gbRom != NULL) + { + render(); + tileView.refresh(); + } +} + +void GBTileView::OnCharbase0() +{ + charBase = 0; + paint(); +} + +void GBTileView::OnCharbase1() +{ + charBase = 1; + paint(); +} + +void GBTileView::OnBank0() +{ + bank = 0; + paint(); +} + +void GBTileView::OnBank1() +{ + bank = 1; + paint(); +} + +void GBTileView::OnStretch() +{ + tileView.setStretch(!tileView.getStretch()); + paint(); + regSetDwordValue("tileViewStretch", tileView.getStretch()); +} + +LRESULT GBTileView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + zoom.setColors(colors); + + int x = (wParam & 0xFFFF)/8; + int y = ((wParam >> 16) & 0xFFFF)/8; + + int tiles = 0x0000; + if (charBase) + tiles = 0x0800; + u32 address = 0x8000 + tiles; + int tile = 16 * y + x; + + address += 16 * tile; + + CString buffer; + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE_NUMBER)->SetWindowText(buffer); + + buffer.Format("%04x", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + return TRUE; +} + +LRESULT GBTileView::OnColInfo(WPARAM wParam, LPARAM) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void GBTileView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar) +{ + switch (nSBCode) + { + case TB_THUMBPOSITION: + palette = nPos; + break; + default: + palette = m_slider.GetPos(); + break; + } + paint(); +} + +void GBTileView::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GBTileView.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GBTileView.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,85 @@ +#if !defined(AFX_GBTILEVIEW_H__C8C8DEBB_17ED_4C5C_9DBE_D730A49B312C__INCLUDED_) +#define AFX_GBTILEVIEW_H__C8C8DEBB_17ED_4C5C_9DBE_D730A49B312C__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GBTileView.h : header file +// +#include "BitmapControl.h" +#include "ColorControl.h" +#include "IUpdate.h" +#include "ResizeDlg.h" +#include "ZoomControl.h" + +///////////////////////////////////////////////////////////////////////////// +// GBTileView dialog + +class GBTileView : public ResizeDlg, IUpdateListener +{ + int charBase; + int palette; + int bank; + BitmapControl tileView; + BITMAPINFO bmpInfo; + u8 * data; + ZoomControl zoom; + ColorControl color; + int w; + int h; + bool autoUpdate; + // Construction +public: + void paint(); + void render(); + void renderTile(int tile, int x, int y, u8 *charBase); + void savePNG(const char *name); + void saveBMP(const char *name); + GBTileView(CWnd*pParent = NULL); // standard constructor + virtual ~GBTileView(); + + virtual void update(); + + // Dialog Data + //{{AFX_DATA(GBTileView) + enum { IDD = IDD_GB_TILE_VIEWER }; + CSliderCtrl m_slider; + int m_charBase; + int m_bank; + BOOL m_stretch; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBTileView) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + virtual afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + virtual afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + + // Generated message map functions + //{{AFX_MSG(GBTileView) + afx_msg void OnSave(); + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + afx_msg void OnAutoUpdate(); + afx_msg void OnCharbase0(); + afx_msg void OnCharbase1(); + afx_msg void OnBank0(); + afx_msg void OnBank1(); + afx_msg void OnStretch(); + afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GBTILEVIEW_H__C8C8DEBB_17ED_4C5C_9DBE_D730A49B312C__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GDBConnection.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GDBConnection.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,248 @@ +// GDBConnection.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GDBConnection.h" + +#include "../common/System.h" + +#define SOCKET_MESSAGE WM_APP+1 + +static bool initialized = false; + +///////////////////////////////////////////////////////////////////////////// +// GDBPortDlg dialog + +GDBPortDlg::GDBPortDlg(CWnd*pParent /*=NULL*/) + : CDialog(GDBPortDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GDBPortDlg) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + port = 55555; + sock = INVALID_SOCKET; + + if (!initialized) + { + WSADATA wsaData; + + int error = WSAStartup(MAKEWORD(1, 1), &wsaData); + + initialized = true; + } +} + +void GDBPortDlg::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GDBPortDlg) + DDX_Control(pDX, IDC_PORT, m_port); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GDBPortDlg, CDialog) +//{{AFX_MSG_MAP(GDBPortDlg) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_WM_CLOSE() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GDBPortDlg message handlers + +int GDBPortDlg::getPort() +{ + return port; +} + +SOCKET GDBPortDlg::getSocket() +{ + return sock; +} + +BOOL GDBPortDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString buffer; + + buffer.Format("%d", port); + + m_port.SetWindowText(buffer); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void GDBPortDlg::OnOk() +{ + CString buffer; + + m_port.GetWindowText(buffer); + + sockaddr_in address; + address.sin_family = AF_INET; + address.sin_addr.s_addr = inet_addr("0.0.0.0"); + address.sin_port = htons(atoi(buffer)); + port = ntohs(address.sin_port); + + SOCKET s = socket(AF_INET, SOCK_STREAM, 0); + + if (s != INVALID_SOCKET) + { + int error = bind(s, (sockaddr *)&address, sizeof(address)); + + if (error) + { + systemMessage(IDS_ERROR_BINDING, "Error binding socket. Port probably in use."); + error = closesocket(s); + EndDialog(FALSE); + } + else + { + error = listen(s, 1); + if (!error) + { + sock = s; + EndDialog(TRUE); + } + else + { + systemMessage(IDS_ERROR_LISTENING, "Error listening on socket."); + closesocket(s); + EndDialog(FALSE); + } + } + } + else + { + systemMessage(IDS_ERROR_CREATING_SOCKET, "Error creating socket."); + EndDialog(FALSE); + } +} + +void GDBPortDlg::OnCancel() +{ + OnClose(); +} + +void GDBPortDlg::OnClose() +{ + EndDialog(FALSE); +} + +///////////////////////////////////////////////////////////////////////////// +// GDBWaitingDlg dialog + +GDBWaitingDlg::GDBWaitingDlg(SOCKET s, int p, CWnd*pParent /*=NULL*/) + : CDialog(GDBWaitingDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(GDBWaitingDlg) + //}}AFX_DATA_INIT + port = p & 65535; + listenSocket = s; +} + +void GDBWaitingDlg::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GDBWaitingDlg) + DDX_Control(pDX, IDC_PORT, m_port); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GDBWaitingDlg, CDialog) +//{{AFX_MSG_MAP(GDBWaitingDlg) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_WM_CLOSE() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GDBWaitingDlg message handlers + +BOOL GDBWaitingDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString buffer; + + buffer.Format("%d", port); + + m_port.SetWindowText(buffer); + + CenterWindow(); + + int error = WSAAsyncSelect(listenSocket, + (HWND)*this, + SOCKET_MESSAGE, + FD_ACCEPT); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +LRESULT GDBWaitingDlg::OnSocketAccept(WPARAM wParam, LPARAM lParam) +{ + if (LOWORD(lParam) == FD_ACCEPT) + { + WSAAsyncSelect(listenSocket, (HWND)*this, 0, 0); + + int flag = 0; + ioctlsocket(listenSocket, FIONBIO, (unsigned long *)&flag); + + SOCKET s = accept(listenSocket, NULL, NULL); + if (s != INVALID_SOCKET) + { + char dummy; + recv(s, &dummy, 1, 0); + if (dummy != '+') + { + systemMessage(IDS_ACK_NOT_RECEIVED, "ACK not received from GDB."); + OnClose(); // calls EndDialog + } + else + { + sock = s; + EndDialog(TRUE); + } + } + } + + return TRUE; +} + +void GDBWaitingDlg::OnCancel() +{ + OnClose(); +} + +void GDBWaitingDlg::OnClose() +{ + if (sock != INVALID_SOCKET) + { + closesocket(sock); + sock = INVALID_SOCKET; + } + if (listenSocket != INVALID_SOCKET) + { + closesocket(listenSocket); + listenSocket = INVALID_SOCKET; + } + EndDialog(FALSE); +} + +SOCKET GDBWaitingDlg::getListenSocket() +{ + return listenSocket; +} + +SOCKET GDBWaitingDlg::getSocket() +{ + return sock; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GDBConnection.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GDBConnection.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,92 @@ +#if !defined(AFX_GDBCONNECTION_H__DD73B298_E1A7_4A46_B282_E7A2B37FC9D9__INCLUDED_) +#define AFX_GDBCONNECTION_H__DD73B298_E1A7_4A46_B282_E7A2B37FC9D9__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GDBConnection.h : header file +// + +#include + +///////////////////////////////////////////////////////////////////////////// +// GDBPortDlg dialog + +class GDBPortDlg : public CDialog +{ + int port; + SOCKET sock; + // Construction +public: + SOCKET getSocket(); + int getPort(); + GDBPortDlg(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GDBPortDlg) + enum { IDD = IDD_GDB_PORT }; + CEdit m_port; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GDBPortDlg) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GDBPortDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnOk(); + afx_msg void OnCancel(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +// GDBWaitingDlg dialog + +class GDBWaitingDlg : public CDialog +{ + int port; + SOCKET listenSocket; + SOCKET sock; + // Construction +public: + SOCKET getSocket(); + SOCKET getListenSocket(); + afx_msg LRESULT OnSocketAccept(WPARAM wParam, LPARAM lParam); + GDBWaitingDlg(SOCKET s, int p, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GDBWaitingDlg) + enum { IDD = IDD_GDB_WAITING }; + CStatic m_port; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GDBWaitingDlg) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GDBWaitingDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GDBCONNECTION_H__DD73B298_E1A7_4A46_B282_E7A2B37FC9D9__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GDIDisplay.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GDIDisplay.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,387 @@ +#include "stdafx.h" +#include + +#include "resource.h" +#include "MainWnd.h" +#include "Reg.h" +#include "VBA.h" + +#include "../gba/GBAGlobals.h" +#include "../gb/gbGlobals.h" +#include "../common/Text.h" +#include "../version.h" + +extern u32 RGB_LOW_BITS_MASK; +extern int systemSpeed; +extern void winlog(const char *, ...); +extern int Init_2xSaI(u32); + +class GDIDisplay : public IDisplay +{ +private: + u8 *filterData; + u8 info[sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)]; +public: + GDIDisplay(); + virtual ~GDIDisplay(); + + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual DISPLAY_TYPE getType() { return GDI; }; + virtual void setOption(const char *, int) {} + virtual int selectFullScreenMode(GUID * *); +}; + +static int calculateShift(u32 mask) +{ + int m = 0; + + while (mask) + { + m++; + mask >>= 1; + } + + return m-5; +} + +GDIDisplay::GDIDisplay() +{ + filterData = (u8 *)malloc(4*16*256*256); // sufficient for 4x filters @ 32bit color depth +} + +GDIDisplay::~GDIDisplay() +{ + cleanup(); +} + +void GDIDisplay::cleanup() +{ + if (filterData) + { + free(filterData); + filterData = NULL; + } +} + +bool GDIDisplay::initialize() +{ + CWnd *pWnd = theApp.m_pMainWnd; + + theApp.mode320Available = false; + theApp.mode640Available = false; + theApp.mode800Available = false; + + HDC dc = GetDC(NULL); + HBITMAP hbm = CreateCompatibleBitmap(dc, 1, 1); + BITMAPINFO *bi = (BITMAPINFO *)info; + ZeroMemory(bi, sizeof(info)); + bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + GetDIBits(dc, hbm, 0, 1, NULL, (LPBITMAPINFO)info, DIB_RGB_COLORS); + GetDIBits(dc, hbm, 0, 1, NULL, (LPBITMAPINFO)info, DIB_RGB_COLORS); + DeleteObject(hbm); + ReleaseDC(NULL, dc); + + if (bi->bmiHeader.biCompression == BI_BITFIELDS) + { + systemColorDepth = bi->bmiHeader.biBitCount; + if (systemColorDepth == 15) + systemColorDepth = 16; + systemRedShift = calculateShift(*((DWORD *)&bi->bmiColors[0])); + systemGreenShift = calculateShift(*((DWORD *)&bi->bmiColors[1])); + systemBlueShift = calculateShift(*((DWORD *)&bi->bmiColors[2])); + if (systemColorDepth == 16) + { + if (systemGreenShift == 6) + { + Init_2xSaI(565); + RGB_LOW_BITS_MASK = 0x821; + } + else + { + Init_2xSaI(555); + RGB_LOW_BITS_MASK = 0x421; + } + } + else if (systemColorDepth == 32) + Init_2xSaI(32); + } + else + { + systemColorDepth = 32; + systemRedShift = 19; + systemGreenShift = 11; + systemBlueShift = 3; + + Init_2xSaI(32); + } + theApp.fsColorDepth = systemColorDepth; + if (systemColorDepth == 24) + theApp.filterFunction = NULL; +#ifdef MMX + if (!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + switch (systemColorDepth) + { + case 16: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + case 24: + case 32: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + } + theApp.updateFilter(); + theApp.updateIFB(); + + pWnd->DragAcceptFiles(TRUE); + + return TRUE; +} + +void GDIDisplay::clear() +{} + +void GDIDisplay::renderMenu() +{ + checkFullScreen(); + theApp.m_pMainWnd->DrawMenuBar(); +} + +void GDIDisplay::checkFullScreen() +{} + +void GDIDisplay::render() +{ + void (*filterFunction)(u8 *, u32, u8 *, u8 *, u32, int, int) = theApp.filterFunction; + int filterWidth = theApp.filterWidth, filterHeight = theApp.filterHeight; +/* + if(textMethod == 1) + { + int copyX = 240, copyY = 160; + if(systemCartridgeType == 1) + if(gbBorderOn) copyX = 256, copyY = 224; + else copyX = 160, copyY = 144; + + extern void Simple1x(u8*,u32,u8*,u8*,u32,int,int); + filterFunction = Simple1x; + filterWidth = copyX*2; + filterHeight = copyY*2; + } + */ + BITMAPINFO *bi = (BITMAPINFO *)info; + bi->bmiHeader.biWidth = filterWidth+1; + bi->bmiHeader.biHeight = -filterHeight; + + int pitch = filterWidth * 2 + 4; + if (systemColorDepth == 32) + pitch = filterWidth * 4 + 4; + // FIXME: is the 24bit color depth still being used nowadays? + else if (systemColorDepth == 24) + pitch = filterWidth * 3; + + // FIXME: is this working if systemColorDepth == 24? + int filterPitch = theApp.rect.right*2; + if (systemColorDepth == 32) + filterPitch = theApp.rect.right*4; + else if (systemColorDepth == 24) + filterPitch = theApp.rect.right*3; + +/* + // HACK: see below + if (textMethod == 1 && !filterFunction) + { + textMethod = 0; // must not be after systemMessage! + systemMessage( + 0, + "The \"On Game\" text display mode does not work with this combination of renderers and filters.\nThe display mode is automatically being changed to \"In Game\" instead,\nbut this may cause message text to go into AVI recordings and screenshots.\nThis can be reconfigured by choosing \"Options->Video->Text Display Options...\""); + } +*/ + + if (filterFunction) + { + bi->bmiHeader.biWidth = theApp.rect.right; + bi->bmiHeader.biHeight = -theApp.rect.bottom; + + (*filterFunction)(pix + pitch, + pitch, + (u8 *)theApp.delta, + (u8 *)filterData, + filterPitch, + filterWidth, + filterHeight); + } + + if (theApp.showSpeed && theApp.videoOption > VIDEO_4X) + { + char buffer[30]; + if (theApp.showSpeed == 1) + sprintf(buffer, "%3d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + + if (filterFunction) + { + if (theApp.showSpeedTransparent) + drawTextTransp((u8 *)filterData, + filterPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8 *)filterData, + filterPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + } + else + { + if (theApp.showSpeedTransparent) + drawTextTransp((u8 *)pix, + pitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8 *)pix, + pitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + } + } + + if (textMethod == 1 && filterFunction) + { + DrawTextMessages((u8 *)filterData, filterPitch, theApp.rect.left, theApp.rect.bottom); + } + + POINT p; + p.x = theApp.dest.left; + p.y = theApp.dest.top; + CWnd *pWnd = theApp.m_pMainWnd; + pWnd->ScreenToClient(&p); + POINT p2; + p2.x = theApp.dest.right; + p2.y = theApp.dest.bottom; + pWnd->ScreenToClient(&p2); + + CDC *dc = pWnd->GetDC(); + + StretchDIBits((HDC)*dc, + p.x, + p.y, + p2.x - p.x, + p2.y - p.y, + 0, + 0, + theApp.rect.right, + theApp.rect.bottom, + filterFunction ? filterData : pix+pitch, + bi, + DIB_RGB_COLORS, + SRCCOPY); + + if (textMethod == 2 || (textMethod == 1 && !filterFunction)) // HACK: so that textMethod isn't changed + for (int slot = 0; slot < SCREEN_MESSAGE_SLOTS; slot++) + { + if (theApp.screenMessage[slot]) + { + if ((theApp.screenMessageDuration[slot] < 0 || + (int)(GetTickCount() - theApp.screenMessageTime[slot]) < theApp.screenMessageDuration[slot]) && + (!theApp.disableStatusMessage || slot == 1 || slot == 2)) + { + dc->SetBkMode(TRANSPARENT); + + if (outlinedText) + { + dc->SetTextColor(textColor != 7 ? RGB(0, 0, 0) : RGB(255, 255, 255)); + + // draw black outline + const static int xd [8] = {-1, 0, 1, 1, 1, 0, -1, -1}; + const static int yd [8] = {-1, -1, -1, 0, 1, 1, 1, 0}; + for (int i = 0; i < 8; i++) + { + dc->TextOut(p.x+10+xd[i], p2.y - 20*(slot+1)+yd[i], theApp.screenMessageBuffer[slot]); + } + } + + COLORREF color; + switch (textColor) + { + case 0: + color = RGB(255, 255, 255); break; + case 1: + color = RGB(255, 0, 0); break; + case 2: + color = RGB(255, 255, 0); break; + case 3: + color = RGB(0, 255, 0); break; + case 4: + color = RGB(0, 255, 255); break; + case 5: + color = RGB(0, 0, 255); break; + case 6: + color = RGB(255, 0, 255); break; + case 7: + color = RGB(0, 0, 0); break; + } + dc->SetTextColor(color); + + // draw center text + dc->TextOut(p.x+10, p2.y - 20*(slot+1), theApp.screenMessageBuffer[slot]); + } + else + { + theApp.screenMessage[slot] = false; + } + } + } + + pWnd->ReleaseDC(dc); +} + +int GDIDisplay::selectFullScreenMode(GUID * *) +{ + HWND wnd = GetDesktopWindow(); + RECT r; + GetWindowRect(wnd, &r); + int w = (r.right - r.left) & 4095; + int h = (r.bottom - r.top) & 4095; + HDC dc = GetDC(wnd); + int c = GetDeviceCaps(dc, BITSPIXEL); + ReleaseDC(wnd, dc); + + return (c << 24) | (w << 12) | h; +} + +IDisplay *newGDIDisplay() +{ + return new GDIDisplay(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GSACodeSelect.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GSACodeSelect.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,96 @@ +// GSACodeSelect.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "GSACodeSelect.h" + +///////////////////////////////////////////////////////////////////////////// +// GSACodeSelect dialog + +GSACodeSelect::GSACodeSelect(FILE *file, CWnd*pParent /*=NULL*/) + : CDialog(GSACodeSelect::IDD, pParent) +{ + //{{AFX_DATA_INIT(GSACodeSelect) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + m_file = file; +} + +void GSACodeSelect::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(GSACodeSelect) + DDX_Control(pDX, IDC_GAME_LIST, m_games); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(GSACodeSelect, CDialog) +//{{AFX_MSG_MAP(GSACodeSelect) +ON_BN_CLICKED(ID_OK, OnOk) +ON_LBN_SELCHANGE(IDC_GAME_LIST, OnSelchangeGameList) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// GSACodeSelect message handlers + +void GSACodeSelect::OnCancel() +{ + EndDialog(-1); +} + +void GSACodeSelect::OnOk() +{ + EndDialog(m_games.GetCurSel()); +} + +void GSACodeSelect::OnSelchangeGameList() +{ + int item = m_games.GetCurSel(); + CWnd *ok = GetDlgItem(ID_OK); + + ok->EnableWindow(item != -1); +} + +BOOL GSACodeSelect::OnInitDialog() +{ + CDialog::OnInitDialog(); + + char buffer[1024]; + + FILE *f = m_file; + int games = 0; + int len = 0; + fseek(f, -4, SEEK_CUR); + fread(&games, 1, 4, f); + while (games > 0) + { + fread(&len, 1, 4, f); + fread(buffer, 1, len, f); + buffer[len] = 0; + m_games.AddString(buffer); + int codes = 0; + fread(&codes, 1, 4, f); + + while (codes > 0) + { + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + fread(&len, 1, 4, f); + fseek(f, len, SEEK_CUR); + fseek(f, 4, SEEK_CUR); + fread(&len, 1, 4, f); + fseek(f, len*12, SEEK_CUR); + codes--; + } + games--; + } + GetDlgItem(ID_OK)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/GSACodeSelect.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/GSACodeSelect.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,49 @@ +#if !defined(AFX_GSACODESELECT_H__189BD94D_288F_4E2A_9395_EAB16F104D87__INCLUDED_) +#define AFX_GSACODESELECT_H__189BD94D_288F_4E2A_9395_EAB16F104D87__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// GSACodeSelect.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// GSACodeSelect dialog + +class GSACodeSelect : public CDialog +{ + // Construction +public: + GSACodeSelect(FILE *file, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(GSACodeSelect) + enum { IDD = IDD_CODE_SELECT }; + CListBox m_games; + //}}AFX_DATA + FILE *m_file; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GSACodeSelect) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(GSACodeSelect) + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnSelchangeGameList(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_GSACODESELECT_H__189BD94D_288F_4E2A_9395_EAB16F104D87__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Hyperlink.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Hyperlink.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,93 @@ +// Hyperlink.cpp : implementation file +// + +#include "stdafx.h" +#include "Hyperlink.h" + +///////////////////////////////////////////////////////////////////////////// +// Hyperlink + +Hyperlink::Hyperlink() +{ + m_over = false; +} + +Hyperlink::~Hyperlink() +{ + m_underlineFont.DeleteObject(); +} + + +BEGIN_MESSAGE_MAP(Hyperlink, CStatic) + //{{AFX_MSG_MAP(Hyperlink) + ON_WM_CTLCOLOR_REFLECT() + ON_WM_ERASEBKGND() + ON_WM_MOUSEMOVE() + //}}AFX_MSG_MAP + ON_CONTROL_REFLECT(STN_CLICKED, OnClicked) +END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// Hyperlink message handlers + +void Hyperlink::PreSubclassWindow() +{ + DWORD dwStyle = GetStyle(); + ::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY); + + // 32649 is the hand cursor + m_cursor = LoadCursor(NULL, MAKEINTRESOURCE(32649)); + + CFont *font = GetFont(); + + LOGFONT lg; + font->GetLogFont(&lg); + + lg.lfUnderline = TRUE; + + m_underlineFont.CreateFontIndirect(&lg); + SetFont(&m_underlineFont); + + CStatic::PreSubclassWindow(); +} + +void Hyperlink::OnClicked() +{ + CString url; + GetWindowText(url); + ::ShellExecute(0, _T("open"), url, + 0, 0, SW_SHOWNORMAL); +} + +HBRUSH Hyperlink::CtlColor(CDC* pDC, UINT nCtlColor) +{ + pDC->SetTextColor(RGB(0,0,240)); + + return (HBRUSH)GetStockObject(NULL_BRUSH); +} + +BOOL Hyperlink::OnEraseBkgnd(CDC* pDC) +{ + CRect rect; + GetClientRect(rect); + pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE)); + + return TRUE; +} + +void Hyperlink::OnMouseMove(UINT nFlags, CPoint point) +{ + if(!m_over) { + m_over = true; + SetCapture(); + ::SetCursor(m_cursor); + } else { + CRect r; + GetClientRect(&r); + + if(!r.PtInRect(point)) { + m_over = false; + ReleaseCapture(); + } + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Hyperlink.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Hyperlink.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,54 @@ +#if !defined(AFX_HYPERLINK_H__BECEAB7D_31FB_4727_A42B_8732162EEBCC__INCLUDED_) +#define AFX_HYPERLINK_H__BECEAB7D_31FB_4727_A42B_8732162EEBCC__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Hyperlink.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// Hyperlink window + +class Hyperlink : public CStatic +{ +// Construction +public: + Hyperlink(); + +// Attributes +public: +// Operations +public: +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Hyperlink) +protected: + virtual void PreSubclassWindow(); + //}}AFX_VIRTUAL + +// Implementation +public: + bool m_over; + HCURSOR m_cursor; + afx_msg void OnClicked(); + CFont m_underlineFont; + virtual ~Hyperlink(); + + // Generated message map functions +protected: + //{{AFX_MSG(Hyperlink) + afx_msg HBRUSH CtlColor(CDC*pDC, UINT nCtlColor); + afx_msg BOOL OnEraseBkgnd(CDC*pDC); + afx_msg void OnMouseMove(UINT nFlags, CPoint point); + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_HYPERLINK_H__BECEAB7D_31FB_4727_A42B_8732162EEBCC__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/IOViewer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/IOViewer.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,183 @@ +// IOViewer.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "IOViewer.h" +#include "VBA.h" + +#include "../gba/GBA.h" // CPUWriteHalfWord +#include "../gba/GBAGlobals.h" + +#include "IOViewerRegs.h" + +///////////////////////////////////////////////////////////////////////////// +// IOViewer dialog + +IOViewer::IOViewer(CWnd*pParent /*=NULL*/) + : ResizeDlg(IOViewer::IDD, pParent) +{ + //{{AFX_DATA_INIT(IOViewer) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + selected = 0; + autoUpdate = false; +} + +void IOViewer::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(IOViewer) + DDX_Control(pDX, IDC_VALUE, m_value); + DDX_Control(pDX, IDC_ADDRESSES, m_address); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(IOViewer, CDialog) +//{{AFX_MSG_MAP(IOViewer) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_BN_CLICKED(IDC_REFRESH, OnRefresh) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_CBN_SELCHANGE(IDC_ADDRESSES, OnSelchangeAddresses) +ON_BN_CLICKED(IDC_APPLY, OnApply) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// IOViewer message handlers + +void IOViewer::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void IOViewer::OnRefresh() +{ + // TODO: Add your control notification handler code here +} + +void IOViewer::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void IOViewer::OnSelchangeAddresses() +{ + selected = m_address.GetCurSel(); + + update(); +} + +void IOViewer::PostNcDestroy() +{ + delete this; +} + +BOOL IOViewer::OnInitDialog() +{ + CDialog::OnInitDialog(); + + // winCenterWindow(getHandle()); + DIALOG_SIZER_START(sz) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\IOView", + NULL); + + CFont *font = CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT)); + int i; + for (i = 0; i < sizeof(ioViewRegisters)/sizeof(IOData); i++) + { + m_address.AddString(ioViewRegisters[i].name); + } + m_address.SetFont(font); + for (i = 0; i < 16; i++) + { + GetDlgItem(IDC_BIT_0+i)->SetFont(font); + } + + RECT cbSize; + int Height; + + m_address.GetClientRect(&cbSize); + Height = m_address.GetItemHeight(0); + Height += m_address.GetItemHeight(0) * (10); + + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 + // Now add on the height of the border of the edit box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // The height of the border of the drop-down box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // now set the size of the window + m_address.SetWindowPos(NULL, + 0, 0, + cbSize.right, Height, + SWP_NOMOVE | SWP_NOZORDER); + + m_address.SetCurSel(0); + update(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void IOViewer::update() +{ + CString buffer; + + const IOData *sel = &ioViewRegisters[selected]; + u16 data = sel->address ? *sel->address : + (ioMem ? *((u16 *)&ioMem[sel->offset]) : 0); + + for (int i = 0; i < 16; i++) + { + CButton *pWnd = (CButton *)GetDlgItem(IDC_BIT_0 + i); + + if (pWnd) + { + if (!(sel->write & (1 << i))) + pWnd->EnableWindow(FALSE); + else + pWnd->EnableWindow(TRUE); + pWnd->SetCheck(((data & (1 << i)) >> i)); + buffer.Format("%2d %s", i, sel->bits[i]); + pWnd->SetWindowText(buffer); + } + } + + buffer.Format("%04X", data); + m_value.SetWindowText(buffer); +} + +void IOViewer::OnApply() +{ + const IOData *sel = &ioViewRegisters[selected]; + u16 res = 0; + for (int i = 0; i < 16; i++) + { + CButton *pWnd = (CButton *)GetDlgItem(IDC_BIT_0 + i); + + if (pWnd) + { + if (pWnd->GetCheck()) + res |= (1 << i); + } + } + CPUWriteHalfWord(0x4000000+sel->offset, res); + update(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/IOViewer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/IOViewer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,58 @@ +#if !defined(AFX_IOVIEWER_H__9C266B78_FC02_4572_9062_0241802B0E76__INCLUDED_) +#define AFX_IOVIEWER_H__9C266B78_FC02_4572_9062_0241802B0E76__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// IOViewer.h : header file +// + +#include "ResizeDlg.h" +#include "IUpdate.h" + +///////////////////////////////////////////////////////////////////////////// +// IOViewer dialog + +class IOViewer : public ResizeDlg, IUpdateListener +{ + // Construction +public: + void update(); + bool autoUpdate; + int selected; + IOViewer(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(IOViewer) + enum { IDD = IDD_IO_VIEWER }; + CStatic m_value; + CComboBox m_address; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(IOViewer) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(IOViewer) + afx_msg void OnClose(); + afx_msg void OnRefresh(); + afx_msg void OnAutoUpdate(); + afx_msg void OnSelchangeAddresses(); + virtual BOOL OnInitDialog(); + afx_msg void OnApply(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_IOVIEWER_H__9C266B78_FC02_4572_9062_0241802B0E76__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/IOViewerRegs.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/IOViewerRegs.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,2078 @@ +#ifndef VBA_IOVIEWERREGS_H +#define VBA_IOVIEWERREGS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +struct IOData +{ + u16 * address; + u16 offset; + char *name; + u16 write; + char *bits[16]; +}; + +const IOData ioViewRegisters[] = { + { + &DISPCNT, 0, "0x4000000-DISPCNT", 0xFFF7, + { + "", + "", + "BG Mode (3 bits)", + "CGB Mode", + "Display Frame", + "H-Blank Interval OBJ processing", + "OBJ Character mapping", + "Forced blank", + "BG0", + "BG1", + "BG2", + "BG3", + "OBJ", + "WIN0", + "WIN1", + "OBJWIN" + } + }, + { + &DISPSTAT, 4, "0x4000004-DISPSTAT", 0xFF38, + { + "V-Blank Status", + "H-Blank Status", + "VCOUNT Evaluation", + "V-Blank Interrupt Enable", + "H-Blank Interrupt Enable", + "VCOUNT Match Interrupt Enable", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "VCOUNT setting (8 bits)" + } + }, + { + &VCOUNT, 6, "0x4000006-VCOUNT", 0x0000, + { + "", + "", + "", + "", + "", + "", + "", + "VCOUNT (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG0CNT, 8, "0x4000008-BG0CNT", 0xDFCF, + { + "", + "Priority (2 bits)", + "", + "Char base (2 bits)", + "", + "", + "Mosaic", + "16/256 colors", + "", + "", + "", + "", + "Screen Base Block (5 bits)", + "", + "", + "Size (2 bits)" + } + }, + { + &BG1CNT, 0xA, "0x400000A-BG1CNT", 0xDFCF, + { + "", + "Priority (2 bits)", + "", + "Char base (2 bits)", + "", + "", + "Mosaic", + "16/256 colors", + "", + "", + "", + "", + "Screen Base Block (5 bits)", + "", + "", + "Size (2 bits)" + } + }, + { + &BG2CNT, 0xC, "0x400000C-BG2CNT", 0xFFCF, + { + "", + "Priority (2 bits)", + "", + "Char base (2 bits)", + "", + "", + "Mosaic", + "16/256 colors", + "", + "", + "", + "", + "Screen Base Block (5 bits)", + "Area Overflow", + "", + "Size (2 bits)" + } + }, + { + &BG3CNT, 0xE, "0x400000E-BG3CNT", 0xFFCF, + { + "", + "Priority (2 bits)", + "", + "Char base (2 bits)", + "", + "", + "Mosaic", + "16/256 colors", + "", + "", + "", + "", + "Screen Base Block (5 bits)", + "Area Overflow", + "", + "Size (2 bits)" + } + }, + { + &BG0HOFS, 0x10, "0x4000010-BG0HOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Horizontal Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG0VOFS, 0x12, "0x4000012-BG0VOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Vertical Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG1HOFS, 0x14, "0x4000014-BG1HOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Horizontal Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG1VOFS, 0x16, "0x4000016-BG1VOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Vertical Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG2HOFS, 0x18, "0x4000018-BG8HOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Horizontal Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG2VOFS, 0x1A, "0x400001A-BG2VOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Vertical Offset (9 bits, W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG3HOFS, 0x1C, "0x400001C-BG3HOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Horizontal Offset (9 bits,W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG3VOFS, 0x1E, "0x400001E-BG3VOFS", 0x01FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "Vertical Offset (9 bits,W)", + "", + "", + "", + "", + "", + "", + "" + } + }, + { + &BG2PA, 0x20, "0x4000020-BG2PA", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dx (16 bits,W)" + } + }, + { + &BG2PB, 0x22, "0x4000022-BG2PB", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dmx (16 bits,W)" + } + }, + { + &BG2PC, 0x24, "0x4000024-BG2PC", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dy (16 bits,W)" + } + }, + { + &BG2PD, 0x26, "0x4000026-BG2PD", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dmy (16 bits,W)" + } + }, + { + &BG2X_L, 0x28, "0x4000028-BG2X_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "X low bits (16 bits,W)" + } + }, + { + &BG2X_H, 0x2A, "0x400002A-BG2X_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "X high bits (12 bits,W)", + "", + "", + "", + "", + } + }, + { + &BG2Y_L, 0x2C, "0x400002C-BG2Y_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Y low bits (16 bits,W)" + } + }, + { + &BG2Y_H, 0x2E, "0x400002E-BG2Y_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Y hight bits (12 bits,W)", + "", + "", + "", + "", + } + }, + { + &BG3PA, 0x30, "0x4000030-BG3PA", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dx (16 bits,W)" + } + }, + { + &BG3PB, 0x32, "0x4000032-BG3PB", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dmx (16 bits,W)" + } + }, + { + &BG3PC, 0x34, "0x4000034-BG3PC", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dy (16 bits,W)" + } + }, + { + &BG3PD, 0x36, "0x4000036-BG3PD", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "dmy (16 bits,W)" + } + }, + { + &BG3X_L, 0x38, "0x4000038-BG3X_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "X low bits (16 bits,W)" + } + }, + { + &BG3X_H, 0x3A, "0x400003A-BG3X_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "X hight bits (12 bits,W)", + "", + "", + "", + "", + } + }, + { + &BG3Y_L, 0x3C, "0x400003C-BG3Y_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Y low bits (16 bits,W)" + } + }, + { + &BG3Y_H, 0x3E, "0x400003E-BG3Y_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Y hight bits (12 bits,W)", + "", + "", + "", + "", + } + }, + { + &WIN0H, 0x40, "0x4000040-WIN0H", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Win 0 lower-right X (8 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Win 0 upper-left X (8 bits,W)", + } + }, + { + &WIN1H, 0x42, "0x4000042-WIN1H", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Win 1 lower-right X (8 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Win 1 upper-left (8 bits,W)", + } + }, + { + &WIN0V, 0x44, "0x4000044-WIN0V", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Win 0 lower-right Y (8 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Win 0 upper-left Y (8 bits,W)", + } + }, + { + &WIN1V, 0x46, "0x4000046-WIN1V", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Win 1 lower-right Y (8 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Win 1 upper-left Y (8 bits,W)", + } + }, + { + &WININ, 0x48, "0x4000048-WININ", 0x3F3F, + { + "WIN0 BG0", + "WIN0 BG1", + "WIN0 BG2", + "WIN0 BG3", + "WIN0 OBJ", + "WIN0 Special FX", + "", + "", + "WIN1 BG0", + "WIN1 BG1", + "WIN1 BG2", + "WIN1 BG3", + "WIN1 OBJ", + "WIN1 Special FX", + "", + "", + } + }, + { + &WINOUT, 0x4A, "0x400004A-WINOUT", 0x3F3F, + { + "WIN0/1 BG0", + "WIN0/1 BG1", + "WIN0/1 BG2", + "WIN0/1 BG3", + "WIN0/1 OBJ", + "WIN0/1 Special FX", + "", + "", + "OBJWIN BG0", + "OBJWIN BG1", + "OBJWIN BG2", + "OBJWIN BG3", + "OBJWIN OBJ", + "OBJWIN Special FX", + "", + "", + } + }, + { + &MOSAIC, 0x4C, "0x400004C-MOSAIC", 0xFFFF, + { + "", + "", + "", + "BG H Size (4 bits,W)", + "", + "", + "", + "BG V Size (4 bits,W)", + "", + "", + "", + "OBJ H Size (4 bits,W)", + "", + "", + "", + "OBJ V Size (4 bits,W)", + } + }, + { + &BLDMOD, 0x50, "0x4000050-BLDMOD", 0x3FFF, + { + "1st BG0", + "1st BG1", + "1st BG2", + "1st BG3", + "1st OBJ", + "1st BD", + "", + "FX Type (2 bits)", + "2nd BG0", + "2nd BG1", + "2nd BG2", + "2nd BG3", + "2nd OBJ", + "2nd BD", + "", + "", + } + }, + { + &COLEV, 0x52, "0x4000052-COLEV", 0x1F1F, + { + "", + "", + "", + "", + "Coefficient EVA (5 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "Coefficient EVB (5 bits,W)", + "", + "", + "", + } + }, + { + &COLY, 0x54, "0x4000054-COLEY", 0x001F, + { + "", + "", + "", + "", + "Coefficient EVY (5 bits,W)", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x60, "0x4000060-SG10_L", 0x007F, + { + "", + "", + "Sweep Shifts (3 bits)", + "Sweep addition/decrease", + "", + "", + "Sweep Time (3 bits)", + "", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x62, "0x4000062-SG10_H", 0xFFFF, + { + "", + "", + "", + "", + "", + "Sound Length (6 bits,W)", + "", + "Waveform Type (2 bits)", + "", + "", + "Envelope Steps (3 bits)", + "Envelope Attenuate/Amplify", + "", + "", + "", + "Envelope Initial Value", + } + }, + { + NULL, 0x64, "0x4000064-SG11", 0xC7FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Frequency (11 bits,W)", + "", + "", + "", + "Sound Continuous/Counter", + "Initialization (W)", + } + }, + { + NULL, 0x68, "0x4000068-SG20", 0xFFFF, + { + "", + "", + "", + "", + "", + "Sound Length (6 bits,W)", + "", + "Waveform Type (2 bits)", + "", + "", + "Envelope Steps (3 bits)", + "Envelope Attenuate/Amplify", + "", + "", + "", + "Envelope Initial Value", + } + }, + { + NULL, 0x6C, "0x400006C-SG21", 0xC7FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Frequency (11 bits,W)", + "", + "", + "", + "Sound Continuous/Counter", + "Initialization (W)", + } + }, + { + NULL, 0x70, "0x4000070-SG30_L", 0x00E0, + { + "", + "", + "", + "", + "", + "Waveform 32/64 Steps", + "Waveform Bank 0/1", + "Sound Output", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x72, "0x4000072-SG30_H", 0xE0FF, + { + "", + "", + "", + "", + "", + "", + "", + "Sound Length (8 bits,W)", + "", + "", + "", + "", + "", + "", + "Output Level (2 bits)", + "Forced 3/4 Output Level", + } + }, + { + NULL, 0x74, "0x4000074-SG31", 0xC7FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Frequency (11 bits,W)", + "", + "", + "", + "Sound Continuous/Counter", + "Initialization (W)", + } + }, + { + NULL, 0x78, "0x4000078-SG40", 0xFF3F, + { + "", + "", + "", + "", + "", + "Sound Length (6 bits,W)", + "", + "", + "", + "", + "Envelope Steps (3 bits)", + "Envelope Attenuate/Amplify", + "", + "", + "", + "Envelope Initial Value", + } + }, + { + NULL, 0x7C, "0x400007C-SG41", 0xC0FF, + { + "", + "", + "Dividing Ratio Freq. (3 bits)", + "Counter 15/7 Steps", + "", + "", + "", + "Counter Shift Clock (4 bits)", + "", + "", + "", + "", + "", + "", + "Sound Continuous/Counter", + "Initialization (W)", + } + }, + { + NULL, 0x80, "0x4000080-SGCNT0_L", 0xFF77, + { + "", + "", + "Right Volume (3 bits)", + "", + "", + "", + "Left Volume (3 bits)", + "", + "Channel 1->Right", + "Channel 2->Right", + "Channel 3->Right", + "Channel 4->Right", + "Channel 1->Left", + "Channel 2->Left", + "Channel 3->Left", + "Channel 4->Left", + } + }, + { + NULL, 0x82, "0x4000082-SGCNT0_H", 0xFF1F, + { + "", + "Sound 1-4 Volume (2 bits)", + "DMA Sound A Volume", + "DMA Sound B Volume", + "", + "", + "", + "", + "DMA Sound A->Right", + "DMA Sound A->Left", + "DMA Sound A Timer", + "DMA Sound A Reset FIFO", + "DMA Sound B->Right", + "DMA Sound B->Left", + "DMA Sound B Timer", + "DMA Sound B Reset FIFO", + } + }, + { + NULL, 0x84, "0x4000084-SGCNT1", 0x0080, + { + "Sound 1 On", + "Sound 2 On", + "Sound 3 On", + "Sound 4 On", + "", + "", + "", + "Master Sound Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x88, "0x4000088-SGBIAS", 0xC3FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Bias Level (10 bits)", + "", + "", + "", + "", + "", + "Sampling Rate (2 bits)", + } + }, + { + NULL, 0xA0, "0x40000A0-SIGFIFOA_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Data 0 (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "Data 1 (8 bits)", + } + }, + { + NULL, 0xA2, "0x40000A2-SIGFIFOA_H", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Data 2 (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "Data 3 (8 bits)", + } + }, + { + NULL, 0xA4, "0x40000A4-SIGFIFOB_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Data 0 (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "Data 1 (8 bits)", + } + }, + { + NULL, 0xA6, "0x40000A6-SIGFIFOB_H", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "Data 2 (8 bits)", + "", + "", + "", + "", + "", + "", + "", + "Data 3 (8 bits)", + } + }, + { + &DM0SAD_L, 0xB0, "0x40000B0-DM0SAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (lower 16 bits)", + } + }, + { + &DM0SAD_H, 0xB2, "0x40000B2-DM0SAD_H", 0x07FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (upper 11 bits)", + "", + "", + "", + "", + "", + } + }, + { + &DM0DAD_L, 0xB4, "0x40000B4-DM0DAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (lower 16 bits)", + } + }, + { + &DM0DAD_H, 0xB6, "0x40000B6-DM0DAD_H", 0x07FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (upper 11 bits)", + "", + "", + "", + "", + "", + } + }, + { + &DM0CNT_L, 0xB8, "0x40000B8-DM0CNT_L", 0x3FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Count (14 bits)", + "", + "", + } + }, + { + &DM0CNT_H, 0xBA, "0x40000BA-DM0CNT_H", 0xF7E0, + { + "", + "", + "", + "", + "", + "", + "Destination Address Control (2 bits)", + "", + "Source Address Control (2 bits)", + "Repeat", + "Transfer Type", + "", + "", + "Start Timing (2 bits)", + "Interrupt Request", + "Enable", + } + }, + { + &DM1SAD_L, 0xBC, "0x40000BC-DM1SAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (lower 16 bits)", + } + }, + { + &DM1SAD_H, 0xBE, "0x40000BE-DM1SAD_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (upper 12 bits)", + "", + "", + "", + "", + } + }, + { + &DM1DAD_L, 0xC0, "0x40000C0-DM1DAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (lower 16 bits)", + } + }, + { + &DM1DAD_H, 0xC2, "0x40000C2-DM1DAD_H", 0x07FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (upper 11 bits)", + "", + "", + "", + "", + "", + } + }, + { + &DM1CNT_L, 0xC4, "0x40000C4-DM1CNT_L", 0x3FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Count (14 bits)", + "", + "", + } + }, + { + &DM1CNT_H, 0xC6, "0x40000C6-DM1CNT_H", 0xF7E0, + { + "", + "", + "", + "", + "", + "", + "Destination Address Control (2 bits)", + "", + "Source Address Control (2 bits)", + "Repeat", + "Transfer Type", + "", + "", + "Start Timing (2 bits)", + "Interrupt Request", + "Enable", + } + }, + { + &DM2SAD_L, 0xC8, "0x40000C8-DM2SAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (lower 16 bits)", + } + }, + { + &DM2SAD_H, 0xCA, "0x40000CA-DM2SAD_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (upper 12 bits)", + "", + "", + "", + "", + } + }, + { + &DM2DAD_L, 0xCC, "0x40000CC-DM2DAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (lower 16 bits)", + } + }, + { + &DM2DAD_H, 0xCE, "0x40000CE-DM2DAD_H", 0x07FF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (upper 11 bits)", + "", + "", + "", + "", + "", + } + }, + { + &DM2CNT_L, 0xD0, "0x40000D0-DM2CNT_L", 0x3FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Count (14 bits)", + "", + "", + } + }, + { + &DM2CNT_H, 0xD2, "0x40000D2-DM2CNT_H", 0xF7E0, + { + "", + "", + "", + "", + "", + "", + "Destination Address Control (2 bits)", + "", + "Source Address Control (2 bits)", + "Repeat", + "Transfer Type", + "", + "", + "Start Timing (2 bits)", + "Interrupt Request", + "Enable", + } + }, + { + &DM3SAD_L, 0xD4, "0x40000D4-DM3SAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (lower 16 bits)", + } + }, + { + &DM3SAD_H, 0xD6, "0x40000D6-DM3SAD_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Source Address (upper 12 bits)", + "", + "", + "", + "", + } + }, + { + &DM3DAD_L, 0xD8, "0x40000D8-DM3DAD_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (lower 16 bits)", + } + }, + { + &DM3DAD_H, 0xDA, "0x40000DA-DM3DAD_H", 0x0FFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Destination Address (upper 12 bits)", + "", + "", + "", + "", + } + }, + { + &DM3CNT_L, 0xDC, "0x40000DC-DM3CNT_L", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Count (16 bits)", + } + }, + { + &DM3CNT_H, 0xDE, "0x40000DE-DM3CNT_H", 0xFFE0, + { + "", + "", + "", + "", + "", + "", + "Destination Address Control (2 bits)", + "", + "Source Address Control (2 bits)", + "Repeat", + "Transfer Type", + "Game Pak Data Request", + "", + "Start Timing (2 bits)", + "Interrupt Request", + "Enable", + } + }, + { + &TM0D, 0x100, "0x4000100-TM0D", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Timer Counter (16 bits)", + } + }, + { + &TM0CNT, 0x102, "0x4000102-TM0CNT", 0x00C7, + { + "", + "Scalar Selection (2 bits)", + "Count Up", + "", + "", + "", + "Interrupt Request", + "Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + &TM1D, 0x104, "0x4000104-TM1D", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Timer Counter (16 bits)", + } + }, + { + &TM1CNT, 0x106, "0x4000106-TM1CNT", 0x00C7, + { + "", + "Scalar Selection (2 bits)", + "Count Up", + "", + "", + "", + "Interrupt Request", + "Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + &TM2D, 0x108, "0x4000108-TM2D", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Timer Counter (16 bits)", + } + }, + { + &TM2CNT, 0x10A, "0x400010A-TM2CNT", 0x00C7, + { + "", + "Scalar Selection (2 bits)", + "Count Up", + "", + "", + "", + "Interrupt Request", + "Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + &TM3D, 0x10C, "0x400010C-TM3D", 0xFFFF, + { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Timer Counter (16 bits)", + } + }, + { + &TM3CNT, 0x10E, "0x400010E-TM3CNT", 0x00C7, + { + "", + "Scalar Selection (2 bits)", + "Count Up", + "", + "", + "", + "Interrupt Request", + "Enable", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + &P1, 0x130, "0x4000130-P1", 0x03FF, + { + "A", + "B", + "Select", + "Start", + "Right", + "Left", + "Up", + "Down", + "Shoulder Right", + "Shoulder Left", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x132, "0x4000132-P1CNT", 0xC3FF, + { + "A", + "B", + "Select", + "Start", + "Right", + "Left", + "Up", + "Down", + "Shoulder Right", + "Shoulder Left", + "", + "", + "", + "", + "Interrupt Request", + "Interrupt Condition", + } + }, + { + &IE, 0x200, "0x4000200-IE", 0x3FFF, + { + "VBlank", + "HBlank", + "VCount", + "Timer 0", + "Timer 1", + "Timer 2", + "Timer 3", + "Serial", + "DMA 0", + "DMA 1", + "DMA 2", + "DMA 3", + "Keypad", + "Game Pak", + "", + "", + } + }, + { + &IF, 0x202, "0x4000202-IF", 0x0000, + { + "VBlank", + "HBlank", + "VCount", + "Timer 0", + "Timer 1", + "Timer 2", + "Timer 3", + "Serial", + "DMA 0", + "DMA 1", + "DMA 2", + "DMA 3", + "Keypad", + "Game Pak", + "", + "", + } + }, + { + NULL, 0x204, "0x4000204-WAITCNT", 0x5FFF, + { + "", + "SRAM Wait Control (2 bits)", + "", + "Wait State 0 First Access (2 bits)", + "Wait State 0 Second Access", + "", + "Wait State 1 First Access (2 bits)", + "Wait State 1 Second Access", + "", + "Wait State 2 First Access (2 bits)", + "Wait State 2 Second Access", + "", + "PHI Terminal Output (2 bits)", + "", + "Game Pak Prefetch Buffer", + "Game Pak Type Flag", + } + }, + { + &IME, 0x208, "0x4000208-IME", 0x0001, + { + "Master Interrupt Enable", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + } + }, + { + NULL, 0x300, "0x4000300-HALTCNT", 0x8001, + { + "First Boot", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "Power Down", + } + }, +}; + +#endif // VBA_IOVIEWERREGS_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/IUpdate.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/IUpdate.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +#ifndef VBA_WIN32_IUPDATE_H +#define VBA_WIN32_IUPDATE_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +class IUpdateListener +{ +public: + virtual void update() = 0; +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Input.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Input.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +#ifndef VBA_WIN32_INPUT_H +#define VBA_WIN32_INPUT_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../common/inputGlobal.h" + +#define JOYCONFIG_MESSAGE (WM_USER + 1000) + +class Input +{ +public: + Input() {}; + virtual ~Input() {}; + + virtual bool initialize() = 0; + + virtual bool readDevices() = 0; + virtual u32 readDevice(int which, bool sensor) = 0; + virtual CString getKeyName(LONG_PTR key) = 0; + virtual void checkKeys() = 0; + virtual void checkDevices() = 0; + virtual void activate() = 0; + virtual void loadSettings() = 0; + virtual void saveSettings() = 0; +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Joypad.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Joypad.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,402 @@ +// Joypad.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "Joypad.h" +#include "Input.h" +#include "VBA.h" + +extern USHORT joypad[4][13]; +extern USHORT motion[4]; + +///////////////////////////////////////////////////////////////////////////// +// JoypadEditControl + +JoypadEditControl::JoypadEditControl() +{} + +JoypadEditControl::~JoypadEditControl() +{} + +BEGIN_MESSAGE_MAP(JoypadEditControl, CEdit) +//{{AFX_MSG_MAP(JoypadEditControl) +ON_WM_CHAR() +//}}AFX_MSG_MAP +ON_MESSAGE(JOYCONFIG_MESSAGE, OnJoyConfig) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// JoypadEditControl message handlers + +void JoypadEditControl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) +{} + +LRESULT JoypadEditControl::OnJoyConfig(WPARAM wParam, LPARAM lParam) +{ + SetWindowLong(GetSafeHwnd(), GWL_USERDATA, ((wParam<<8)|lParam)); + SetWindowText(theApp.input->getKeyName((wParam<<8)|lParam)); + GetParent()->GetNextDlgTabItem(this, FALSE)->SetFocus(); + return TRUE; +} + +BOOL JoypadEditControl::PreTranslateMessage(MSG *pMsg) +{ + if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_RETURN)) + return TRUE; + + return CEdit::PreTranslateMessage(pMsg); +} + +///////////////////////////////////////////////////////////////////////////// +// JoypadConfig dialog + +JoypadConfig::JoypadConfig(int w, CWnd*pParent /*=NULL*/) + : CDialog(JoypadConfig::IDD, pParent) +{ + //{{AFX_DATA_INIT(JoypadConfig) + //}}AFX_DATA_INIT + timerId = 0; + which = w; + if (which < 0 || which > 3) + which = 0; +} + +void JoypadConfig::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(JoypadConfig) + DDX_Control(pDX, IDC_EDIT_UP, up); + DDX_Control(pDX, IDC_EDIT_SPEED, speed); + DDX_Control(pDX, IDC_EDIT_RIGHT, right); + DDX_Control(pDX, IDC_EDIT_LEFT, left); + DDX_Control(pDX, IDC_EDIT_DOWN, down); + DDX_Control(pDX, IDC_EDIT_CAPTURE, capture); + DDX_Control(pDX, IDC_EDIT_BUTTON_START, buttonStart); + DDX_Control(pDX, IDC_EDIT_BUTTON_SELECT, buttonSelect); + DDX_Control(pDX, IDC_EDIT_BUTTON_R, buttonR); + DDX_Control(pDX, IDC_EDIT_BUTTON_L, buttonL); + DDX_Control(pDX, IDC_EDIT_BUTTON_GS, buttonGS); + DDX_Control(pDX, IDC_EDIT_BUTTON_B, buttonB); + DDX_Control(pDX, IDC_EDIT_BUTTON_A, buttonA); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(JoypadConfig, CDialog) +//{{AFX_MSG_MAP(JoypadConfig) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_BN_CLICKED(ID_OK, OnOk) +ON_WM_CHAR() +ON_WM_DESTROY() +ON_WM_TIMER() +ON_WM_KEYDOWN() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// JoypadConfig message handlers + +void JoypadConfig::OnCancel() +{ + EndDialog(FALSE); +} + +void JoypadConfig::OnOk() +{ + assignKeys(); + theApp.input->checkKeys(); + EndDialog(TRUE); +} + +void JoypadConfig::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) +{} + +void JoypadConfig::OnDestroy() +{ + CDialog::OnDestroy(); + + KillTimer(timerId); +} + +void JoypadConfig::OnTimer(UINT nIDEvent) +{ + theApp.input->checkDevices(); + + CDialog::OnTimer(nIDEvent); +} + +void JoypadConfig::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) +{} + +BOOL JoypadConfig::OnInitDialog() +{ + CDialog::OnInitDialog(); + + timerId = SetTimer(0, 200, NULL); + + SetWindowLong(up, GWL_USERDATA, joypad[which][KEY_UP]); + up.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_UP])); + + SetWindowLong(down, GWL_USERDATA, joypad[which][KEY_DOWN]); + down.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_DOWN])); + + SetWindowLong(left, GWL_USERDATA, joypad[which][KEY_LEFT]); + left.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_LEFT])); + + SetWindowLong(right, GWL_USERDATA, joypad[which][KEY_RIGHT]); + right.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_RIGHT])); + + SetWindowLong(buttonA, GWL_USERDATA, joypad[which][KEY_BUTTON_A]); + buttonA.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_A])); + + SetWindowLong(buttonB, GWL_USERDATA, joypad[which][KEY_BUTTON_B]); + buttonB.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_B])); + + SetWindowLong(buttonL, GWL_USERDATA, joypad[which][KEY_BUTTON_L]); + buttonL.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_L])); + + SetWindowLong(buttonR, GWL_USERDATA, joypad[which][KEY_BUTTON_R]); + buttonR.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_R])); + + SetWindowLong(buttonSelect, GWL_USERDATA, joypad[which][KEY_BUTTON_SELECT]); + buttonSelect.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_SELECT])); + + SetWindowLong(buttonStart, GWL_USERDATA, joypad[which][KEY_BUTTON_START]); + buttonStart.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_START])); + + SetWindowLong(speed, GWL_USERDATA, joypad[which][KEY_BUTTON_SPEED]); + speed.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_SPEED])); + + SetWindowLong(capture, GWL_USERDATA, joypad[which][KEY_BUTTON_CAPTURE]); + capture.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_CAPTURE])); + + SetWindowLong(buttonGS, GWL_USERDATA, joypad[which][KEY_BUTTON_GS]); + buttonGS.SetWindowText(theApp.input->getKeyName(joypad[which][KEY_BUTTON_GS])); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void JoypadConfig::assignKey(int id, int key) +{ + switch (id) + { + case IDC_EDIT_LEFT: + joypad[which][KEY_LEFT] = key; + break; + case IDC_EDIT_RIGHT: + joypad[which][KEY_RIGHT] = key; + break; + case IDC_EDIT_UP: + joypad[which][KEY_UP] = key; + break; + case IDC_EDIT_SPEED: + joypad[which][KEY_BUTTON_SPEED] = key; + break; + case IDC_EDIT_CAPTURE: + joypad[which][KEY_BUTTON_CAPTURE] = key; + break; + case IDC_EDIT_DOWN: + joypad[which][KEY_DOWN] = key; + break; + case IDC_EDIT_BUTTON_A: + joypad[which][KEY_BUTTON_A] = key; + break; + case IDC_EDIT_BUTTON_B: + joypad[which][KEY_BUTTON_B] = key; + break; + case IDC_EDIT_BUTTON_L: + joypad[which][KEY_BUTTON_L] = key; + break; + case IDC_EDIT_BUTTON_R: + joypad[which][KEY_BUTTON_R] = key; + break; + case IDC_EDIT_BUTTON_START: + joypad[which][KEY_BUTTON_START] = key; + break; + case IDC_EDIT_BUTTON_SELECT: + joypad[which][KEY_BUTTON_SELECT] = key; + break; + case IDC_EDIT_BUTTON_GS: + joypad[which][KEY_BUTTON_GS] = key; + break; + } +} + +void JoypadConfig::assignKeys() +{ + int id; + + id = IDC_EDIT_UP; + assignKey(id, GetWindowLong(up, GWL_USERDATA)); + + id = IDC_EDIT_DOWN; + assignKey(id, GetWindowLong(down, GWL_USERDATA)); + + id = IDC_EDIT_LEFT; + assignKey(id, GetWindowLong(left, GWL_USERDATA)); + + id = IDC_EDIT_RIGHT; + assignKey(id, GetWindowLong(right, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_A; + assignKey(id, GetWindowLong(buttonA, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_B; + assignKey(id, GetWindowLong(buttonB, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_L; + assignKey(id, GetWindowLong(buttonL, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_R; + assignKey(id, GetWindowLong(buttonR, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_SELECT; + assignKey(id, GetWindowLong(buttonSelect, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_START; + assignKey(id, GetWindowLong(buttonStart, GWL_USERDATA)); + + id = IDC_EDIT_SPEED; + assignKey(id, GetWindowLong(speed, GWL_USERDATA)); + + id = IDC_EDIT_CAPTURE; + assignKey(id, GetWindowLong(capture, GWL_USERDATA)); + + id = IDC_EDIT_BUTTON_GS; + assignKey(id, GetWindowLong(buttonGS, GWL_USERDATA)); + + // winSaveKeys(); +} + +///////////////////////////////////////////////////////////////////////////// +// MotionConfig dialog + +MotionConfig::MotionConfig(CWnd*pParent /*=NULL*/) + : CDialog(MotionConfig::IDD, pParent) +{ + //{{AFX_DATA_INIT(MotionConfig) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + timerId = 0; +} + +void MotionConfig::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MotionConfig) + DDX_Control(pDX, IDC_EDIT_UP, up); + DDX_Control(pDX, IDC_EDIT_RIGHT, right); + DDX_Control(pDX, IDC_EDIT_LEFT, left); + DDX_Control(pDX, IDC_EDIT_DOWN, down); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(MotionConfig, CDialog) +//{{AFX_MSG_MAP(MotionConfig) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_BN_CLICKED(ID_OK, OnOk) +ON_WM_CHAR() +ON_WM_DESTROY() +ON_WM_KEYDOWN() +ON_WM_TIMER() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// MotionConfig message handlers + +void MotionConfig::OnCancel() +{ + EndDialog(FALSE); +} + +void MotionConfig::OnOk() +{ + assignKeys(); + theApp.input->checkKeys(); + EndDialog(TRUE); +} + +void MotionConfig::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) +{} + +void MotionConfig::OnDestroy() +{ + CDialog::OnDestroy(); + + KillTimer(timerId); +} + +BOOL MotionConfig::OnInitDialog() +{ + CDialog::OnInitDialog(); + + timerId = SetTimer(0, 200, NULL); + + SetWindowLong(up, GWL_USERDATA, motion[KEY_UP]); + up.SetWindowText(theApp.input->getKeyName(motion[KEY_UP])); + + SetWindowLong(down, GWL_USERDATA, motion[KEY_DOWN]); + down.SetWindowText(theApp.input->getKeyName(motion[KEY_DOWN])); + + SetWindowLong(left, GWL_USERDATA, motion[KEY_LEFT]); + left.SetWindowText(theApp.input->getKeyName(motion[KEY_LEFT])); + + SetWindowLong(right, GWL_USERDATA, motion[KEY_RIGHT]); + right.SetWindowText(theApp.input->getKeyName(motion[KEY_RIGHT])); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MotionConfig::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) +{} + +void MotionConfig::OnTimer(UINT nIDEvent) +{ + theApp.input->checkDevices(); + + CDialog::OnTimer(nIDEvent); +} + +void MotionConfig::assignKey(int id, int key) +{ + switch (id) + { + case IDC_EDIT_LEFT: + motion[KEY_LEFT] = key; + break; + case IDC_EDIT_RIGHT: + motion[KEY_RIGHT] = key; + break; + case IDC_EDIT_UP: + motion[KEY_UP] = key; + break; + case IDC_EDIT_DOWN: + motion[KEY_DOWN] = key; + break; + } +} + +void MotionConfig::assignKeys() +{ + int id; + + id = IDC_EDIT_UP; + assignKey(id, GetWindowLong(up, GWL_USERDATA)); + + id = IDC_EDIT_DOWN; + assignKey(id, GetWindowLong(down, GWL_USERDATA)); + + id = IDC_EDIT_LEFT; + assignKey(id, GetWindowLong(left, GWL_USERDATA)); + + id = IDC_EDIT_RIGHT; + assignKey(id, GetWindowLong(right, GWL_USERDATA)); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Joypad.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Joypad.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,145 @@ +#if !defined(AFX_JOYPAD_H__FFFB2470_9EEC_4D2D_A5F0_3BF31579999A__INCLUDED_) +#define AFX_JOYPAD_H__FFFB2470_9EEC_4D2D_A5F0_3BF31579999A__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// Joypad.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// JoypadEditControl window + +class JoypadEditControl : public CEdit +{ + // Construction +public: + JoypadEditControl(); + + // Attributes +public: + // Operations +public: + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(JoypadEditControl) + //}}AFX_VIRTUAL + + // Implementation +public: + virtual BOOL PreTranslateMessage(MSG *pMsg); + afx_msg LRESULT OnJoyConfig(WPARAM wParam, LPARAM lParam); + virtual ~JoypadEditControl(); + + // Generated message map functions +protected: + //{{AFX_MSG(JoypadEditControl) + afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +///////////////////////////////////////////////////////////////////////////// +// JoypadConfig dialog + +class JoypadConfig : public CDialog +{ + // Construction +public: + void assignKeys(); + void assignKey(int id, int key); + JoypadConfig(int w, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(JoypadConfig) + enum { IDD = IDD_CONFIG }; + JoypadEditControl up; + JoypadEditControl speed; + JoypadEditControl right; + JoypadEditControl left; + JoypadEditControl down; + JoypadEditControl capture; + JoypadEditControl buttonStart; + JoypadEditControl buttonSelect; + JoypadEditControl buttonR; + JoypadEditControl buttonL; + JoypadEditControl buttonGS; + JoypadEditControl buttonB; + JoypadEditControl buttonA; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(JoypadConfig) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + UINT timerId; + int which; + + // Generated message map functions + //{{AFX_MSG(JoypadConfig) + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); + afx_msg void OnDestroy(); + afx_msg void OnTimer(UINT nIDEvent); + afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +///////////////////////////////////////////////////////////////////////////// +// MotionConfig dialog + +class MotionConfig : public CDialog +{ + // Construction +public: + void assignKeys(); + void assignKey(int id, int key); + MotionConfig(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(MotionConfig) + enum { IDD = IDD_MOTION_CONFIG }; + JoypadEditControl up; + JoypadEditControl right; + JoypadEditControl left; + JoypadEditControl down; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MotionConfig) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(MotionConfig) + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); + afx_msg void OnDestroy(); + virtual BOOL OnInitDialog(); + afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); + afx_msg void OnTimer(UINT nIDEvent); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +private: + UINT timerId; +}; +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_JOYPAD_H__FFFB2470_9EEC_4D2D_A5F0_3BF31579999A__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/KeyboardEdit.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/KeyboardEdit.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,271 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : KeyboardEdit.cpp +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Authors : A.Lebatard + T.Maurel +// Date : 17.08.98 +// +// Remarks : implementation file +// + +//////////////////////////////////////////////////////////////////////////////// +// modified by aquanull + +#include "stdafx.h" +#include "KeyboardEdit.h" + +extern TCHAR *mapVirtKeysStringFromWORD(WORD wKey); + +IMPLEMENT_DYNAMIC(CKeyboardEdit, CEdit) + +///////////////////////////////////////////////////////////////////////////// +// CKeyboardEdit + +CKeyboardEdit::CKeyboardEdit() +{ + ResetKey(); +} + +CKeyboardEdit::~CKeyboardEdit() +{} + +BEGIN_MESSAGE_MAP(CKeyboardEdit, CEdit) +//{{AFX_MSG_MAP(CKeyboardEdit) +ON_CONTROL_REFLECT_EX(EN_CHANGE, &CKeyboardEdit::OnEnChange) +ON_CONTROL_REFLECT_EX(EN_SETFOCUS, &CKeyboardEdit::OnEnSetfocus) +ON_CONTROL_REFLECT_EX(EN_KILLFOCUS, &CKeyboardEdit::OnEnKillfocus) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// CKeyboardEdit message handlers +BOOL CKeyboardEdit::PreTranslateMessage(MSG *pMsg) +{ + if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN) + { + int key = pMsg->wParam; + m_keys[key] = true; + switch (key) + { + case VK_CONTROL: + case VK_MENU: + case VK_SHIFT: + m_bCtrlPressed = m_keys[VK_CONTROL]; + m_bAltPressed = m_keys[VK_MENU];; + m_bShiftPressed = m_keys[VK_SHIFT]; + if (!m_keys[m_wVirtKey]) + { + if (m_keys[m_wJamKey]) + { + m_wVirtKey = m_wJamKey; + m_wJamKey = 0; + } + else + m_wVirtKey = 0; + } + if (!m_keys[m_wJamKey]) + m_wJamKey = 0; + break; + default: + m_bCtrlPressed = m_keys[VK_CONTROL]; + m_bAltPressed = m_keys[VK_MENU]; + m_bShiftPressed = m_keys[VK_SHIFT]; + if (m_wVirtKey != key) + { + if (m_keys[m_wVirtKey]) + m_wJamKey = m_wVirtKey; + else + m_wJamKey = 0; + m_wVirtKey = key; + } + else if (!m_keys[m_wJamKey]) + m_wJamKey = 0; + break; + } + DisplayKeyboardString(); + return TRUE; + } + else if (pMsg->message == WM_KEYUP || pMsg->message == WM_SYSKEYUP) + { + int key = pMsg->wParam; + m_keys[key] = false; + switch (key) + { + case VK_CONTROL: + case VK_MENU: + case VK_SHIFT: + break; + default: + m_bCtrlPressed = m_keys[VK_CONTROL]; + m_bAltPressed = m_keys[VK_MENU]; + m_bShiftPressed = m_keys[VK_SHIFT]; + if (m_wJamKey) + { + if (!m_keys[m_wVirtKey]) + { + m_wVirtKey = m_wJamKey; + m_wJamKey = 0; + } + if (!m_keys[m_wJamKey]) + m_wJamKey = 0; + } + break; + } + DisplayKeyboardString(); + return TRUE; + } + + return CEdit::PreTranslateMessage(pMsg); +} + +BOOL CKeyboardEdit::OnEnChange() +{ + return FALSE; +} + +BOOL CKeyboardEdit::OnEnSetfocus() +{ + //SetSel(0, -1, TRUE); // mouse click makes this in vain, so we use the method below instead + PostMessage(EM_SETSEL, 0, -1); + m_bForceUpdate = true; + return FALSE; +} + +BOOL CKeyboardEdit::OnEnKillfocus() +{ + AllKeyUp(); + return FALSE; +} + +//////////////////////////////////////////////////////////////////////// +// +void CKeyboardEdit::DisplayKeyboardString() +{ + CString strKbd; + + // modifiers + if (m_bCtrlPressed) + strKbd = "Ctrl"; + + if (m_bAltPressed) + { + if (strKbd.GetLength() > 0) + strKbd += '+'; + strKbd += "Alt"; + } + if (m_bShiftPressed) + { + if (strKbd.GetLength() > 0) + strKbd += '+'; + strKbd += "Shift"; + } + // virtual key + LPCTSTR szVirtKey = mapVirtKeysStringFromWORD(m_wVirtKey); + if (szVirtKey != NULL) + { + if (strKbd.GetLength() > 0) + strKbd += '+'; + strKbd += szVirtKey; + } + // jammed key + LPCTSTR szJamKey = mapVirtKeysStringFromWORD(m_wJamKey); + if (szJamKey != NULL) + { + strKbd += '('; + strKbd += szJamKey; + strKbd += ')'; + } + + if (m_bForceUpdate) + { + m_bForceUpdate = false; + SetWindowText(strKbd); + } + else + { + CString oldString; + GetWindowText(oldString); + if (oldString.Compare(strKbd)) + SetWindowText(strKbd); + } +} + +//////////////////////////////////////////////////////////////////////// +// +void CKeyboardEdit::ResetKey() +{ + AllKeyUp(); + + m_bForceUpdate = true; + m_bCtrlPressed = false; + m_bAltPressed = false; + m_bShiftPressed = false; + m_wVirtKey = 0; + m_wJamKey = 0; + + if (m_hWnd != NULL) + { + CString oldString; + GetWindowText(oldString); + if (!oldString.IsEmpty()) + SetWindowText(_T("")); + } +} + +void CKeyboardEdit::AllKeyUp() +{ + for (int i = 0; i < 256; ++i) + m_keys[i] = 0; +} + +//////////////////////////////////////////////////////////////////////// +// +bool CKeyboardEdit::GetAccelKey(WORD &wVirtKey, bool &bCtrl, bool &bAlt, bool &bShift) const +{ + if (!IsDefined()) + return false; + + wVirtKey = m_wVirtKey; + bAlt = m_bAltPressed; + bCtrl = m_bCtrlPressed; + bShift = m_bShiftPressed; + return true; +} + +bool CKeyboardEdit::GetJamKey(WORD &wJamKey) const +{ + if (m_wJamKey != 0) + wJamKey = m_wJamKey; + return m_wJamKey != 0; +} + +bool CKeyboardEdit::IsDefined() const +{ + return bool(m_wVirtKey || m_bAltPressed || m_bCtrlPressed || m_bShiftPressed); +} + +bool CKeyboardEdit::IsFinished() const +{ + bool finished = true; + for (int i = 0; i < 256; ++i) + if (m_keys[i]) + finished = false; + return finished; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/KeyboardEdit.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/KeyboardEdit.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,96 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 1998 by Thierry Maurel +// All rights reserved +// +// Distribute freely, except: don't remove my name from the source or +// documentation (don't take credit for my work), mark your changes (don't +// get me blamed for your possible bugs), don't alter or remove this +// notice. +// No warrantee of any kind, express or implied, is included with this +// software; use at your own risk, responsibility for damages (if any) to +// anyone resulting from the use of this software rests entirely with the +// user. +// +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and +// I'll try to keep a version up to date. I can be reached as follows: +// tmaurel@caramail.com (or tmaurel@hol.fr) +// +//////////////////////////////////////////////////////////////////////////////// +// File : KeyboardEdit.h +// Project : AccelsEditor +//////////////////////////////////////////////////////////////////////////////// +// Version : 1.0 * Authors : A.Lebatard + T.Maurel +// Date : 17.08.98 +// +// Remarks : +// +//////////////////////////////////////////////////////////////////////////////// +// Modified by the VBA-rr Team + +#if !defined(AFX_KEYBOARDEDIT_H__88E35AB0_2E23_11D2_BA24_0060B0B5E151__INCLUDED_) +#define AFX_KEYBOARDEDIT_H__88E35AB0_2E23_11D2_BA24_0060B0B5E151__INCLUDED_ + +#if _MSC_VER >= 1000 +#pragma once +#endif // _MSC_VER >= 1000 +// KeyboardEdit.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// CKeyboardEdit window + +class CKeyboardEdit : public CEdit +{ + DECLARE_DYNAMIC(CKeyboardEdit) // what will this do? + + // Construction +public: + CKeyboardEdit(); + + // Operations +public: + void ResetKey(); + void AllKeyUp(); + bool GetAccelKey(WORD &wVirtKey, bool &bCtrl, bool &bAlt, bool &bShift) const; + bool GetJamKey(WORD &wJamKey) const; + bool IsDefined() const; + bool IsFinished() const; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(CKeyboardEdit) +public: + virtual BOOL PreTranslateMessage(MSG *pMsg); + virtual ~CKeyboardEdit(); + //}}AFX_VIRTUAL + +protected: + void DisplayKeyboardString(); + + // Attributes +protected: + BYTE m_keys[256]; + bool m_bForceUpdate; + bool m_bCtrlPressed; + bool m_bAltPressed; + bool m_bShiftPressed; + WORD m_wVirtKey; + WORD m_wJamKey; + + // Generated message map functions +public: + //{{AFX_MSG(CKeyboardEdit) + afx_msg BOOL OnEnChange(); + afx_msg BOOL OnEnSetfocus(); + afx_msg BOOL OnEnKillfocus(); + //}}AFX_MSG + + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Developer Studio will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_KEYBOARDEDIT_H__88E35AB0_2E23_11D2_BA24_0060B0B5E151__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/LangSelect.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/LangSelect.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,75 @@ +// LangSelect.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "LangSelect.h" +#include "VBA.h" + +///////////////////////////////////////////////////////////////////////////// +// LangSelect dialog + +LangSelect::LangSelect(CWnd*pParent /*=NULL*/) + : CDialog(LangSelect::IDD, pParent) +{ + //{{AFX_DATA_INIT(LangSelect) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void LangSelect::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(LangSelect) + DDX_Control(pDX, IDC_LANG_STRING, m_langString); + DDX_Control(pDX, IDC_LANG_NAME, m_langName); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(LangSelect, CDialog) +//{{AFX_MSG_MAP(LangSelect) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// LangSelect message handlers + +void LangSelect::OnCancel() +{ + EndDialog(FALSE); +} + +void LangSelect::OnOk() +{ + m_langString.GetWindowText(theApp.languageName); + EndDialog(TRUE); +} + +BOOL LangSelect::OnInitDialog() +{ + CDialog::OnInitDialog(); + + char lbuffer[10]; + if (GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, + lbuffer, 10)) + { + m_langName.SetWindowText(lbuffer); + } + else + { + m_langName.SetWindowText("???"); + } + + if (!theApp.languageName.IsEmpty()) + m_langString.SetWindowText(theApp.languageName); + + m_langString.LimitText(3); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/LangSelect.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/LangSelect.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,48 @@ +#if !defined(AFX_LANGSELECT_H__63619E13_A375_4ED4_952F_DCF8998C2914__INCLUDED_) +#define AFX_LANGSELECT_H__63619E13_A375_4ED4_952F_DCF8998C2914__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// LangSelect.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// LangSelect dialog + +class LangSelect : public CDialog +{ + // Construction +public: + LangSelect(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(LangSelect) + enum { IDD = IDD_LANG_SELECT }; + CEdit m_langString; + CStatic m_langName; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(LangSelect) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(LangSelect) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_LANGSELECT_H__63619E13_A375_4ED4_952F_DCF8998C2914__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Logging.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Logging.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,273 @@ +// Logging.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "Logging.h" +#include "FileDlg.h" + +#include "../common/System.h" + +///////////////////////////////////////////////////////////////////////////// +// Logging dialog + +Logging *Logging::instance = NULL; +CString Logging:: text; + +Logging::Logging(CWnd*pParent /*=NULL*/) + : ResizeDlg(Logging::IDD, pParent) +{ + //{{AFX_DATA_INIT(Logging) + m_swi = FALSE; + m_unaligned_access = FALSE; + m_illegal_write = FALSE; + m_illegal_read = FALSE; + m_dma0 = FALSE; + m_dma1 = FALSE; + m_dma2 = FALSE; + m_dma3 = FALSE; + m_agbprint = FALSE; + m_undefined = FALSE; + //}}AFX_DATA_INIT +} + +void Logging::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Logging) + DDX_Control(pDX, IDC_LOG, m_log); + DDX_Check(pDX, IDC_VERBOSE_SWI, m_swi); + DDX_Check(pDX, IDC_VERBOSE_UNALIGNED_ACCESS, m_unaligned_access); + DDX_Check(pDX, IDC_VERBOSE_ILLEGAL_WRITE, m_illegal_write); + DDX_Check(pDX, IDC_VERBOSE_ILLEGAL_READ, m_illegal_read); + DDX_Check(pDX, IDC_VERBOSE_DMA0, m_dma0); + DDX_Check(pDX, IDC_VERBOSE_DMA1, m_dma1); + DDX_Check(pDX, IDC_VERBOSE_DMA2, m_dma2); + DDX_Check(pDX, IDC_VERBOSE_DMA3, m_dma3); + DDX_Check(pDX, IDC_VERBOSE_AGBPRINT, m_agbprint); + DDX_Check(pDX, IDC_VERBOSE_UNDEFINED, m_undefined); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(Logging, CDialog) +//{{AFX_MSG_MAP(Logging) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(IDC_CLEAR, OnClear) +ON_BN_CLICKED(IDC_VERBOSE_AGBPRINT, OnVerboseAgbprint) +ON_BN_CLICKED(IDC_VERBOSE_DMA0, OnVerboseDma0) +ON_BN_CLICKED(IDC_VERBOSE_DMA1, OnVerboseDma1) +ON_BN_CLICKED(IDC_VERBOSE_DMA2, OnVerboseDma2) +ON_BN_CLICKED(IDC_VERBOSE_DMA3, OnVerboseDma3) +ON_BN_CLICKED(IDC_VERBOSE_ILLEGAL_READ, OnVerboseIllegalRead) +ON_BN_CLICKED(IDC_VERBOSE_ILLEGAL_WRITE, OnVerboseIllegalWrite) +ON_BN_CLICKED(IDC_VERBOSE_SWI, OnVerboseSwi) +ON_BN_CLICKED(IDC_VERBOSE_UNALIGNED_ACCESS, OnVerboseUnalignedAccess) +ON_BN_CLICKED(IDC_VERBOSE_UNDEFINED, OnVerboseUndefined) +ON_BN_CLICKED(IDC_SAVE, OnSave) +ON_EN_ERRSPACE(IDC_LOG, OnErrspaceLog) +ON_EN_MAXTEXT(IDC_LOG, OnMaxtextLog) +ON_WM_CLOSE() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// Logging message handlers + +void Logging::OnOk() +{ + EndDialog(TRUE); + + instance = NULL; +} + +void Logging::OnClear() +{ + text = ""; + m_log.SetWindowText(""); +} + +void Logging::OnVerboseAgbprint() +{ + systemVerbose ^= 512; +} + +void Logging::OnVerboseDma0() +{ + systemVerbose ^= 16; +} + +void Logging::OnVerboseDma1() +{ + systemVerbose ^= 32; +} + +void Logging::OnVerboseDma2() +{ + systemVerbose ^= 64; +} + +void Logging::OnVerboseDma3() +{ + systemVerbose ^= 128; +} + +void Logging::OnVerboseIllegalRead() +{ + systemVerbose ^= 8; +} + +void Logging::OnVerboseIllegalWrite() +{ + systemVerbose ^= 4; +} + +void Logging::OnVerboseSwi() +{ + systemVerbose ^= 1; +} + +void Logging::OnVerboseUnalignedAccess() +{ + systemVerbose ^= 2; +} + +void Logging::OnVerboseUndefined() +{ + systemVerbose ^= 256; +} + +void Logging::OnSave() +{ + int len = m_log.GetWindowTextLength(); + + char *mem = (char *)malloc(len); + + if (mem) + { + LPCTSTR exts[] = { ".txt", NULL }; + m_log.GetWindowText(mem, len); + CString filter = "All Files|*.*||"; + FileDlg dlg(this, "", filter, 0, + NULL, exts, NULL, "Save output", true); + + if (dlg.DoModal() == IDOK) + { + FILE *f = fopen(dlg.GetPathName(), "w"); + if (f) + { + fwrite(mem, 1, len, f); + fclose(f); + } + } + } + + free(mem); +} + +void Logging::OnErrspaceLog() +{ + systemMessage(0, "Error allocating space"); +} + +void Logging::OnMaxtextLog() +{ + systemMessage(0, "Max text length reached %d", m_log.GetLimitText()); +} + +void Logging::PostNcDestroy() +{ + delete this; +} + +BOOL Logging::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_LOG, DS_SizeY|DS_SizeX) + DIALOG_SIZER_ENTRY(ID_OK, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLEAR, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\LogView", + NULL); + + m_swi = (systemVerbose & 1) != 0; + m_unaligned_access = (systemVerbose & 2) != 0; + m_illegal_write = (systemVerbose & 4) != 0; + m_illegal_read = (systemVerbose & 8) != 0; + m_dma0 = (systemVerbose & 16) != 0; + m_dma1 = (systemVerbose & 32) != 0; + m_dma2 = (systemVerbose & 64) != 0; + m_dma3 = (systemVerbose & 128) != 0; + m_undefined = (systemVerbose & 256) != 0; + m_agbprint = (systemVerbose & 256) != 0; + UpdateData(FALSE); + + m_log.LimitText(-1); + m_log.SetWindowText(text); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Logging::log(const char *s) +{ + int size = ::SendMessage(m_log, WM_GETTEXTLENGTH, 0, 0); + m_log.SetSel(size, size); + m_log.ReplaceSel(s); +} + +void Logging::OnClose() +{ + EndDialog(FALSE); + + instance = NULL; + + CDialog::OnClose(); +} + +void toolsLogging() +{ + if (Logging::instance == NULL) + { + Logging::instance = new Logging(); + Logging::instance->Create(IDD_LOGGING, AfxGetApp()->m_pMainWnd); + Logging::instance->ShowWindow(SW_SHOW); + } + else + { + Logging::instance->SetForegroundWindow(); + } +} + +void toolsLog(const char *s) +{ + CString str; + int state = 0; + if (s) + { + char c = *s++; + while (c) + { + if (c == '\n' && state == 0) + str += '\r'; + else if (c == '\r') + state = 1; + else + state = 0; + str += c; + c = *s++; + } + } + + Logging::text += str; + if (Logging::instance != NULL) + { + Logging::instance->log(str); + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Logging.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Logging.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,78 @@ +#if !defined(AFX_LOGGING_H__222FC21A_D40D_450D_8A1C_D33305E47B85__INCLUDED_) +#define AFX_LOGGING_H__222FC21A_D40D_450D_8A1C_D33305E47B85__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// Logging.h : header file +// + +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// Logging dialog + +class Logging : public ResizeDlg +{ + // Construction +public: + void log(const char *); + Logging(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Logging) + enum { IDD = IDD_LOGGING }; + CEdit m_log; + BOOL m_swi; + BOOL m_unaligned_access; + BOOL m_illegal_write; + BOOL m_illegal_read; + BOOL m_dma0; + BOOL m_dma1; + BOOL m_dma2; + BOOL m_dma3; + BOOL m_agbprint; + BOOL m_undefined; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Logging) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(Logging) + afx_msg void OnOk(); + afx_msg void OnClear(); + afx_msg void OnVerboseAgbprint(); + afx_msg void OnVerboseDma0(); + afx_msg void OnVerboseDma1(); + afx_msg void OnVerboseDma2(); + afx_msg void OnVerboseDma3(); + afx_msg void OnVerboseIllegalRead(); + afx_msg void OnVerboseIllegalWrite(); + afx_msg void OnVerboseSwi(); + afx_msg void OnVerboseUnalignedAccess(); + afx_msg void OnVerboseUndefined(); + afx_msg void OnSave(); + afx_msg void OnErrspaceLog(); + afx_msg void OnMaxtextLog(); + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +public: + static Logging *instance; + static CString text; +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_LOGGING_H__222FC21A_D40D_450D_8A1C_D33305E47B85__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/LuaOpenDialog.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/LuaOpenDialog.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,369 @@ +// LuaOpenDialog.cpp : implementation file +// + +//#include "stdafx.h" +//#include "resource.h" +#include "LuaOpenDialog.h" +#include "MainWnd.h" +#include "WinResUtil.h" +#include "WinMiscUtil.h" +#include "VBA.h" +#include "Sound.h" + +#include "../common/vbalua.h" + +HWND LuaConsoleHWnd = NULL; +HFONT hFont = NULL; +LOGFONT LuaConsoleLogFont; + +struct ControlLayoutInfo +{ + int controlID; + + enum LayoutType // what to do when the containing window resizes + { + NONE, // leave the control where it was + RESIZE_END, // resize the control + MOVE_START, // move the control + }; + LayoutType horizontalLayout; + LayoutType verticalLayout; +}; +struct ControlLayoutState +{ + int x,y,width,height; + bool valid; + ControlLayoutState() : valid(false) {} +}; + +static ControlLayoutInfo controlLayoutInfos [] = { + {IDC_LUACONSOLE, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::RESIZE_END}, + {IDC_EDIT_LUAPATH, ControlLayoutInfo::RESIZE_END, ControlLayoutInfo::NONE}, + {IDC_BUTTON_LUARUN, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE}, + {IDC_BUTTON_LUASTOP, ControlLayoutInfo::MOVE_START, ControlLayoutInfo::NONE}, +}; +static const int numControlLayoutInfos = sizeof(controlLayoutInfos)/sizeof(*controlLayoutInfos); + +struct { + int width; int height; + ControlLayoutState layoutState [numControlLayoutInfos]; +} windowInfo; + +void PrintToWindowConsole(int hDlgAsInt, const char* str) +{ + HWND hDlg = (HWND)hDlgAsInt; + HWND hConsole = GetDlgItem(hDlg, IDC_LUACONSOLE); + + int length = GetWindowTextLength(hConsole); + if(length >= 250000) + { + // discard first half of text if it's getting too long + SendMessage(hConsole, EM_SETSEL, 0, length/2); + SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)""); + length = GetWindowTextLength(hConsole); + } + SendMessage(hConsole, EM_SETSEL, length, length); + + //LuaPerWindowInfo& info = LuaWindowInfo[hDlg]; + + { + SendMessage(hConsole, EM_REPLACESEL, false, (LPARAM)str); + } +} + +void WinLuaOnStart(int hDlgAsInt) +{ + HWND hDlg = (HWND)hDlgAsInt; + //LuaPerWindowInfo& info = LuaWindowInfo[hDlg]; + //info.started = true; + EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), false); // disable browse while running because it misbehaves if clicked in a frameadvance loop + EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), true); + SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Restart"); + SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), ""); // clear the console +// Show_Genesis_Screen(HWnd); // otherwise we might never show the first thing the script draws +} + +void WinLuaOnStop(int hDlgAsInt) +{ + HWND hDlg = (HWND)hDlgAsInt; + //LuaPerWindowInfo& info = LuaWindowInfo[hDlg]; + + HWND prevWindow = GetActiveWindow(); + SetActiveWindow(hDlg); // bring to front among other script/secondary windows, since a stopped script will have some message for the user that would be easier to miss otherwise + if(prevWindow == AfxGetMainWnd()->GetSafeHwnd()) SetActiveWindow(prevWindow); + + //info.started = false; + EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUABROWSE), true); + EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUASTOP), false); + SetWindowText(GetDlgItem(hDlg, IDC_BUTTON_LUARUN), "Run"); +// if(statusOK) +// Show_Genesis_Screen(MainWindow->getHWnd()); // otherwise we might never show the last thing the script draws + //if(info.closeOnStop) + // PostMessage(hDlg, WM_CLOSE, 0, 0); +} + +INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) +{ + RECT r; + RECT r2; + int dx1, dy1, dx2, dy2; + + switch (msg) { + + case WM_INITDIALOG: + { + // remove the 30000 character limit from the console control + SendMessage(GetDlgItem(hDlg, IDC_LUACONSOLE),EM_LIMITTEXT,0,0); + + GetWindowRect(AfxGetMainWnd()->GetSafeHwnd(), &r); + dx1 = (r.right - r.left) / 2; + dy1 = (r.bottom - r.top) / 2; + + GetWindowRect(hDlg, &r2); + dx2 = (r2.right - r2.left) / 2; + dy2 = (r2.bottom - r2.top) / 2; + + int windowIndex = 0;//std::find(LuaScriptHWnds.begin(), LuaScriptHWnds.end(), hDlg) - LuaScriptHWnds.begin(); + int staggerOffset = windowIndex * 24; + r.left += staggerOffset; + r.right += staggerOffset; + r.top += staggerOffset; + r.bottom += staggerOffset; + + // push it away from the main window if we can + const int width = (r.right-r.left); + const int width2 = (r2.right-r2.left); + if(r.left+width2 + width < GetSystemMetrics(SM_CXSCREEN)) + { + r.right += width; + r.left += width; + } + else if((int)r.left - (int)width2 > 0) + { + r.right -= width2; + r.left -= width2; + } + + SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + + RECT r3; + GetClientRect(hDlg, &r3); + windowInfo.width = r3.right - r3.left; + windowInfo.height = r3.bottom - r3.top; + for(int i = 0; i < numControlLayoutInfos; i++) { + ControlLayoutState& layoutState = windowInfo.layoutState[i]; + layoutState.valid = false; + } + + DragAcceptFiles(hDlg, true); + SetDlgItemText(hDlg, IDC_EDIT_LUAPATH, VBAGetLuaScriptName()); + + SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &LuaConsoleLogFont, 0); // reset with an acceptable font + return true; + } break; + + case WM_SIZE: + { + // resize or move controls in the window as necessary when the window is resized + + //LuaPerWindowInfo& windowInfo = LuaWindowInfo[hDlg]; + int prevDlgWidth = windowInfo.width; + int prevDlgHeight = windowInfo.height; + + int dlgWidth = LOWORD(lParam); + int dlgHeight = HIWORD(lParam); + + int deltaWidth = dlgWidth - prevDlgWidth; + int deltaHeight = dlgHeight - prevDlgHeight; + + for(int i = 0; i < numControlLayoutInfos; i++) + { + ControlLayoutInfo layoutInfo = controlLayoutInfos[i]; + ControlLayoutState& layoutState = windowInfo.layoutState[i]; + + HWND hCtrl = GetDlgItem(hDlg,layoutInfo.controlID); + + int x,y,width,height; + if(layoutState.valid) + { + x = layoutState.x; + y = layoutState.y; + width = layoutState.width; + height = layoutState.height; + } + else + { + RECT r; + GetWindowRect(hCtrl, &r); + POINT p = {r.left, r.top}; + ScreenToClient(hDlg, &p); + x = p.x; + y = p.y; + width = r.right - r.left; + height = r.bottom - r.top; + } + + switch(layoutInfo.horizontalLayout) + { + case ControlLayoutInfo::RESIZE_END: width += deltaWidth; break; + case ControlLayoutInfo::MOVE_START: x += deltaWidth; break; + default: break; + } + switch(layoutInfo.verticalLayout) + { + case ControlLayoutInfo::RESIZE_END: height += deltaHeight; break; + case ControlLayoutInfo::MOVE_START: y += deltaHeight; break; + default: break; + } + + SetWindowPos(hCtrl, 0, x,y, width,height, 0); + + layoutState.x = x; + layoutState.y = y; + layoutState.width = width; + layoutState.height = height; + layoutState.valid = true; + } + + windowInfo.width = dlgWidth; + windowInfo.height = dlgHeight; + + RedrawWindow(hDlg, NULL, NULL, RDW_INVALIDATE); + } break; + + case WM_COMMAND: + switch (LOWORD(wParam)) { + case IDOK: + case IDCANCEL: { + EndDialog(hDlg, true); // goto case WM_CLOSE; + } break; + + case IDC_BUTTON_LUARUN: + { + char filename[MAX_PATH]; + GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH); + VBALoadLuaCode(filename); + } break; + + case IDC_BUTTON_LUASTOP: + { + VBALuaStop(); + } break; + + case IDC_BUTTON_LUAEDIT: + { + char Str_Tmp [1024]; + SendDlgItemMessage(hDlg,IDC_EDIT_LUAPATH,WM_GETTEXT,(WPARAM)512,(LPARAM)Str_Tmp); + // tell the OS to open the file with its associated editor, + // without blocking on it or leaving a command window open. + if((int)ShellExecute(NULL, "edit", Str_Tmp, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC) + if((int)ShellExecute(NULL, "open", Str_Tmp, NULL, NULL, SW_SHOWNORMAL) == SE_ERR_NOASSOC) + ShellExecute(NULL, NULL, "notepad", Str_Tmp, NULL, SW_SHOWNORMAL); + } break; + + case IDC_BUTTON_LUABROWSE: + { + systemSoundClearBuffer(); + + CString filter = winResLoadFilter(IDS_FILTER_LUA); + CString title = winResLoadString(IDS_SELECT_LUA_NAME); + + CString luaName = winGetDestFilename(theApp.gameFilename, IDS_LUA_DIR, ".lua"); + CString luaDir = winGetDestDir(IDS_LUA_DIR); + + filter.Replace('|', '\000'); +// char *p = filter.GetBuffer(0); +// while ((p = strchr(p, '|')) != NULL) +// *p++ = 0; + + OPENFILENAME ofn; + ZeroMemory( (LPVOID)&ofn, sizeof(OPENFILENAME) ); + ofn.lpstrFile = luaName.GetBuffer(MAX_PATH); + ofn.nMaxFile = MAX_PATH; + ofn.lStructSize = sizeof(OPENFILENAME); + ofn.hwndOwner = hDlg; + ofn.lpstrFilter = filter; + ofn.nFilterIndex = 0; + ofn.lpstrInitialDir = luaDir; + ofn.lpstrTitle = title; + ofn.lpstrDefExt = "lua"; + ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_ENABLESIZING | OFN_EXPLORER; // hide previously-ignored read-only checkbox (the real read-only box is in the open-movie dialog itself) + if(GetOpenFileName( &ofn )) + { + SetWindowText(GetDlgItem(hDlg, IDC_EDIT_LUAPATH), luaName); + } + return true; + } break; + + case IDC_EDIT_LUAPATH: + { + char filename[MAX_PATH]; + GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH); + FILE* file = fopen(filename, "rb"); + EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_LUAEDIT), file != NULL); + if(file) + fclose(file); + } break; + + case IDC_LUACONSOLE_CHOOSEFONT: + { + CHOOSEFONT cf; + + ZeroMemory(&cf, sizeof(cf)); + cf.lStructSize = sizeof(CHOOSEFONT); + cf.hwndOwner = hDlg; + cf.lpLogFont = &LuaConsoleLogFont; + cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT; + if (ChooseFont(&cf)) { + if (hFont) { + DeleteObject(hFont); + hFont = NULL; + } + hFont = CreateFontIndirect(&LuaConsoleLogFont); + if (hFont) + SendDlgItemMessage(hDlg, IDC_LUACONSOLE, WM_SETFONT, (WPARAM)hFont, 0); + } + } break; + + case IDC_LUACONSOLE_CLEAR: + { + SetWindowText(GetDlgItem(hDlg, IDC_LUACONSOLE), ""); + } break; + } + break; + + case WM_CLOSE: { + SendMessage(hDlg, WM_DESTROY, 0, 0); + } break; + + case WM_DESTROY: { + //VBALuaStop(); + DragAcceptFiles(hDlg, FALSE); + if (hFont) { + DeleteObject(hFont); + hFont = NULL; + } + LuaConsoleHWnd = NULL; + } break; + + case WM_DROPFILES: { + HDROP hDrop; + //UINT fileNo; + UINT fileCount; + char filename[_MAX_PATH]; + + hDrop = (HDROP)wParam; + fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); + if (fileCount > 0) { + DragQueryFile(hDrop, 0, filename, sizeof(filename)); + SetWindowText(GetDlgItem(hDlg, IDC_EDIT_LUAPATH), filename); + } + DragFinish(hDrop); + return true; + } break; + + } + + return false; + +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/LuaOpenDialog.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/LuaOpenDialog.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,21 @@ +#if !defined(VBA_WIN32_LUAOPENDIALOG_H_INCLUDED) +#define VBA_WIN32_LUAOPENDIALOG_H_INCLUDED + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// LuaOpenDialog.h : header file +// + +#include "stdafx.h" +#include "resource.h" + +extern HWND LuaConsoleHWnd; + +void PrintToWindowConsole(int hDlgAsInt, const char* str); +void WinLuaOnStart(int hDlgAsInt); +void WinLuaOnStop(int hDlgAsInt); +INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam); + +#endif // !defined(VBA_WIN32_LUAOPENDIALOG_H_INCLUDED) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MainWnd.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MainWnd.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1511 @@ +// MainWnd.cpp : implementation file +// + +#include "stdafx.h" +#include + +#include "resource.h" +#include "MainWnd.h" + +#include "CmdAccelOb.h" +#include "FileDlg.h" +#include "ModeConfirm.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "WinMiscUtil.h" +#include "Input.h" +#include "7zip/7zip.h" +#include "7zip/OpenArchive.h" +#include "LuaOpenDialog.h" +#include "ram_search.h" +#include "ramwatch.h" +#include "Sound.h" +#include "VBA.h" + +#include "../version.h" +#include "../common/Util.h" +#include "../common/movie.h" +#include "../common/vbalua.h" +#include "../gba/GBASound.h" +#include "../gba/GBAGlobals.h" +#include "../gb/gbGlobals.h" + +#define VBA_CONFIRM_MODE WM_APP + 100 + +///////////////////////////////////////////////////////////////////////////// +// MainWnd + +MainWnd::MainWnd() +{ + m_hAccelTable = NULL; + arrow = LoadCursor(NULL, IDC_ARROW); + + InitDecoder(); +} + +MainWnd::~MainWnd() +{ + CleanupDecoder(); +} + +BEGIN_MESSAGE_MAP(MainWnd, CWnd) +//{{AFX_MSG_MAP(MainWnd) +ON_WM_MOVE() +ON_WM_SIZE() +ON_WM_CLOSE() +ON_WM_INITMENUPOPUP() +ON_WM_INITMENU() +ON_WM_CONTEXTMENU() +ON_WM_MOUSEMOVE() +ON_WM_ACTIVATE() +ON_WM_ACTIVATEAPP() +ON_WM_DROPFILES() +ON_WM_PAINT() + +ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout) +ON_COMMAND(ID_HELP_FAQ, OnHelpFaq) +ON_COMMAND(ID_FILE_OPEN, OnFileOpen) +ON_COMMAND(ID_FILE_OPENGAMEBOY, OnFileOpenGBx) +ON_COMMAND(ID_FILE_PAUSE, OnFilePause) +ON_UPDATE_COMMAND_UI(ID_FILE_PAUSE, OnUpdateFilePause) +ON_COMMAND(ID_FILE_RESET, OnFileReset) +ON_UPDATE_COMMAND_UI(ID_FILE_RESET, OnUpdateFileReset) +ON_UPDATE_COMMAND_UI(ID_FILE_RECENT_FREEZE, OnUpdateFileRecentFreeze) +ON_COMMAND(ID_FILE_RECENT_RESET, OnFileRecentReset) +ON_COMMAND(ID_FILE_RECENT_FREEZE, OnFileRecentFreeze) +ON_COMMAND(ID_FILE_EXIT, OnFileExit) +ON_COMMAND(ID_FILE_CLOSE, OnFileClose) +ON_UPDATE_COMMAND_UI(ID_FILE_CLOSE, OnUpdateFileClose) +ON_COMMAND(ID_FILE_LOAD, OnFileLoad) +ON_UPDATE_COMMAND_UI(ID_FILE_LOAD, OnUpdateFileLoad) +ON_COMMAND(ID_FILE_SAVE, OnFileSave) +ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave) +ON_COMMAND(ID_FILE_IMPORT_BATTERYFILE, OnFileImportBatteryfile) +ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_BATTERYFILE, OnUpdateFileImportBatteryfile) +ON_COMMAND(ID_FILE_IMPORT_GAMESHARKCODEFILE, OnFileImportGamesharkcodefile) +ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_GAMESHARKCODEFILE, OnUpdateFileImportGamesharkcodefile) +ON_COMMAND(ID_FILE_IMPORT_GAMESHARKSNAPSHOT, OnFileImportGamesharksnapshot) +ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_GAMESHARKSNAPSHOT, OnUpdateFileImportGamesharksnapshot) +ON_COMMAND(ID_FILE_EXPORT_BATTERYFILE, OnFileExportBatteryfile) +ON_UPDATE_COMMAND_UI(ID_FILE_EXPORT_BATTERYFILE, OnUpdateFileExportBatteryfile) +ON_COMMAND(ID_FILE_EXPORT_GAMESHARKSNAPSHOT, OnFileExportGamesharksnapshot) +ON_UPDATE_COMMAND_UI(ID_FILE_EXPORT_GAMESHARKSNAPSHOT, OnUpdateFileExportGamesharksnapshot) +ON_COMMAND(ID_FILE_QUICKSCREENCAPTURE, OnFileQuickScreencapture) +ON_COMMAND(ID_FILE_SCREENCAPTURE, OnFileScreencapture) +ON_UPDATE_COMMAND_UI(ID_FILE_SCREENCAPTURE, OnUpdateFileScreencapture) +ON_COMMAND(ID_FILE_ROMINFORMATION, OnFileRominformation) +ON_UPDATE_COMMAND_UI(ID_FILE_ROMINFORMATION, OnUpdateFileRominformation) +ON_COMMAND(ID_FILE_TOGGLEMENU, OnFileTogglemenu) +ON_UPDATE_COMMAND_UI(ID_FILE_TOGGLEMENU, OnUpdateFileTogglemenu) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_6, OnUpdateOptionsFrameskipThrottle6) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_15, OnUpdateOptionsFrameskipThrottle15) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_25, OnUpdateOptionsFrameskipThrottle25) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_50, OnUpdateOptionsFrameskipThrottle50) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_75, OnUpdateOptionsFrameskipThrottle75) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_100, OnUpdateOptionsFrameskipThrottle100) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_125, OnUpdateOptionsFrameskipThrottle125) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_150, OnUpdateOptionsFrameskipThrottle150) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_200, OnUpdateOptionsFrameskipThrottle200) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_300, OnUpdateOptionsFrameskipThrottle300) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_400, OnUpdateOptionsFrameskipThrottle400) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_600, OnUpdateOptionsFrameskipThrottle600) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_1000, OnUpdateOptionsFrameskipThrottle1000) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER, OnUpdateOptionsFrameskipThrottleOther) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_INCREASE, OnUpdateOptionsFrameskipThrottleIncrease) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_THROTTLE_DECREASE, OnUpdateOptionsFrameskipThrottleDecrease) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_6, OnOptionsFrameskipThrottle6) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_15, OnOptionsFrameskipThrottle15) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_25, OnOptionsFrameskipThrottle25) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_50, OnOptionsFrameskipThrottle50) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_75, OnOptionsFrameskipThrottle75) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_100, OnOptionsFrameskipThrottle100) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_125, OnOptionsFrameskipThrottle125) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_150, OnOptionsFrameskipThrottle150) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_200, OnOptionsFrameskipThrottle200) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_300, OnOptionsFrameskipThrottle300) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_400, OnOptionsFrameskipThrottle400) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_600, OnOptionsFrameskipThrottle600) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_1000, OnOptionsFrameskipThrottle1000) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER, OnOptionsFrameskipThrottleOther) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_INCREASE, OnOptionsFrameskipThrottleIncrease) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_THROTTLE_DECREASE, OnOptionsFrameskipThrottleDecrease) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_ACCURATEPITCH, OnOptionsFrameskipAccuratePitch) +ON_COMMAND(ID_OPTIONS_FRAMESKIP_ACCURATESPEED, OnOptionsFrameskipAccurateSpeed) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_ACCURATEPITCH, OnUpdateOptionsFrameskipAccuratePitch) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FRAMESKIP_ACCURATESPEED, OnUpdateOptionsFrameskipAccurateSpeed) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_0, OnUpdateOptionsVideoFrameskip0) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_1, OnUpdateOptionsVideoFrameskip1) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_2, OnUpdateOptionsVideoFrameskip2) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_3, OnUpdateOptionsVideoFrameskip3) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_4, OnUpdateOptionsVideoFrameskip4) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_5, OnUpdateOptionsVideoFrameskip5) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_6, OnUpdateOptionsVideoFrameskip6) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_7, OnUpdateOptionsVideoFrameskip7) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_8, OnUpdateOptionsVideoFrameskip8) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FRAMESKIP_9, OnUpdateOptionsVideoFrameskip9) +ON_COMMAND(ID_OPTIONS_VIDEO_VSYNC, OnOptionsVideoVsync) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_VSYNC, OnUpdateOptionsVideoVsync) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X1, OnUpdateOptionsVideoX1) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X2, OnUpdateOptionsVideoX2) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X3, OnUpdateOptionsVideoX3) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_X4, OnUpdateOptionsVideoX4) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN320X240, OnUpdateOptionsVideoFullscreen320x240) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN640X480, OnUpdateOptionsVideoFullscreen640x480) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN800X600, OnUpdateOptionsVideoFullscreen800x600) +ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN320X240, OnOptionsVideoFullscreen320x240) +ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN640X480, OnOptionsVideoFullscreen640x480) +ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN800X600, OnOptionsVideoFullscreen800x600) +ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREEN, OnOptionsVideoFullscreen) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREEN, OnUpdateOptionsVideoFullscreen) +ON_COMMAND(ID_OPTIONS_VIDEO_DISABLESFX, OnOptionsVideoDisablesfx) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DISABLESFX, OnUpdateOptionsVideoDisablesfx) +ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT, OnOptionsVideoFullscreenstretchtofit) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT, OnUpdateOptionsVideoFullscreenstretchtofit) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_GDI, OnOptionsVideoRendermethodGdi) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_GDI, OnUpdateOptionsVideoRendermethodGdi) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW, OnOptionsVideoRendermethodDirectdraw) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW, OnUpdateOptionsVideoRendermethodDirectdraw) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D, OnOptionsVideoRendermethodDirect3d) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D, OnUpdateOptionsVideoRendermethodDirect3d) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL, OnOptionsVideoRendermethodOpengl) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL, OnUpdateOptionsVideoRendermethodOpengl) +ON_COMMAND(ID_OPTIONS_VIDEO_TRIPLEBUFFERING, OnOptionsVideoTriplebuffering) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_TRIPLEBUFFERING, OnUpdateOptionsVideoTriplebuffering) +ON_COMMAND(ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY, OnOptionsVideoDdrawemulationonly) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY, OnUpdateOptionsVideoDdrawemulationonly) +ON_COMMAND(ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY, OnOptionsVideoDdrawusevideomemory) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY, OnUpdateOptionsVideoDdrawusevideomemory) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER, OnOptionsVideoRenderoptionsD3dnofilter) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER, OnUpdateOptionsVideoRenderoptionsD3dnofilter) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR, OnOptionsVideoRenderoptionsD3dbilinear) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR, OnUpdateOptionsVideoRenderoptionsD3dbilinear) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST, OnOptionsVideoRenderoptionsGlnearest) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST, OnUpdateOptionsVideoRenderoptionsGlnearest) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR, OnOptionsVideoRenderoptionsGlbilinear) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR, OnUpdateOptionsVideoRenderoptionsGlbilinear) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE, OnOptionsVideoRenderoptionsGltriangle) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE, OnUpdateOptionsVideoRenderoptionsGltriangle) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS, OnOptionsVideoRenderoptionsGlquads) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS, OnUpdateOptionsVideoRenderoptionsGlquads) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_SELECTSKIN, OnOptionsVideoRenderoptionsSelectskin) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_SELECTSKIN, OnUpdateOptionsVideoRenderoptionsSelectskin) +ON_COMMAND(ID_OPTIONS_VIDEO_RENDEROPTIONS_SKIN, OnOptionsVideoRenderoptionsSkin) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_RENDEROPTIONS_SKIN, OnUpdateOptionsVideoRenderoptionsSkin) +ON_COMMAND(ID_OPTIONS_EMULATOR_ASSOCIATE, OnOptionsEmulatorAssociate) +ON_COMMAND(ID_OPTIONS_EMULATOR_DIRECTORIES, OnOptionsEmulatorDirectories) +ON_COMMAND_RANGE(ID_OPTIONS_PREFER_ARCHIVE_NAME, ID_OPTIONS_PREFER_ROM_NAME, OnOptionsEmulatorFilenamePreference) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_PREFER_ARCHIVE_NAME, ID_OPTIONS_PREFER_ROM_NAME, OnUpdateOptionsEmulatorFilenamePreference) +ON_COMMAND(ID_OPTIONS_VIDEO_DISABLESTATUSMESSAGES, OnOptionsVideoDisablestatusmessages) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_DISABLESTATUSMESSAGES, OnUpdateOptionsVideoDisablestatusmessages) +ON_COMMAND(ID_OPTIONS_EMULATOR_SYNCHRONIZE, OnOptionsEmulatorSynchronize) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SYNCHRONIZE, OnUpdateOptionsEmulatorSynchronize) + +ON_COMMAND(ID_OPTIONS_EMULATOR_ALWAYSONTOP, OnOptionsEmulatorAlwaysOnTop) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_ALWAYSONTOP, OnUpdateOptionsEmulatorAlwaysOnTop) +ON_COMMAND(ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE, OnOptionsEmulatorPausewheninactive) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE, OnUpdateOptionsEmulatorPausewheninactive) +ON_COMMAND(ID_OPTIONS_EMULATOR_BACKGROUNDINPUT, OnOptionsEmulatorEnableBackgroundInput) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_BACKGROUNDINPUT, OnUpdateOptionsEmulatorEnableBackgroundInput) +ON_COMMAND(ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE, OnOptionsEmulatorSpeeduptoggle) + +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE, OnUpdateOptionsEmulatorSpeeduptoggle) +ON_COMMAND(ID_OPTIONS_EMULATOR_REMOVEINTROSGBA, OnOptionsEmulatorRemoveintrosgba) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_REMOVEINTROSGBA, OnUpdateOptionsEmulatorRemoveintrosgba) +ON_COMMAND(ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH, OnOptionsEmulatorAutomaticallyipspatch) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH, OnUpdateOptionsEmulatorAutomaticallyipspatch) +ON_COMMAND(ID_OPTIONS_EMULATOR_AGBPRINT, OnOptionsEmulatorAgbprint) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AGBPRINT, OnUpdateOptionsEmulatorAgbprint) +ON_COMMAND(ID_OPTIONS_EMULATOR_REALTIMECLOCK, OnOptionsEmulatorRealtimeclock) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_REALTIMECLOCK, OnUpdateOptionsEmulatorRealtimeclock) +ON_COMMAND(ID_OPTIONS_EMULATOR_AUTOHIDEMENU, OnOptionsEmulatorAutohidemenu) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_AUTOHIDEMENU, OnUpdateOptionsEmulatorAutohidemenu) +ON_COMMAND(ID_OPTIONS_EMULATOR_REWINDINTERVAL, OnOptionsEmulatorRewindinterval) +ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC, OnOptionsEmulatorSavetypeAutomatic) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC, OnUpdateOptionsEmulatorSavetypeAutomatic) +ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM, OnOptionsEmulatorSavetypeEeprom) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM, OnUpdateOptionsEmulatorSavetypeEeprom) +ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_SRAM, OnOptionsEmulatorSavetypeSram) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_SRAM, OnUpdateOptionsEmulatorSavetypeSram) +ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH, OnOptionsEmulatorSavetypeFlash) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH, OnUpdateOptionsEmulatorSavetypeFlash) +ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR, OnOptionsEmulatorSavetypeEepromsensor) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR, OnUpdateOptionsEmulatorSavetypeEepromsensor) +ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_NONE, OnOptionsEmulatorSavetypeNone) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_NONE, OnUpdateOptionsEmulatorSavetypeNone) +ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K, OnOptionsEmulatorSavetypeFlash512k) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K, OnUpdateOptionsEmulatorSavetypeFlash512k) +ON_COMMAND(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M, OnOptionsEmulatorSavetypeFlash1m) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M, OnUpdateOptionsEmulatorSavetypeFlash1m) +ON_COMMAND(ID_OPTIONS_EMULATOR_USEBIOSFILE, OnOptionsEmulatorUsebiosfile) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_USEBIOSFILE, OnUpdateOptionsEmulatorUsebiosfile) +ON_COMMAND(ID_OPTIONS_EMULATOR_SKIPBIOS, OnOptionsEmulatorSkipbios) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_SKIPBIOS, OnUpdateOptionsEmulatorSkipbios) +ON_COMMAND(ID_OPTIONS_EMULATOR_SELECTBIOSFILE, OnOptionsEmulatorSelectbiosfile) + +ON_COMMAND(ID_EMULATOR_USE_OLD_FRAME_TIMING, OnOptionsEmulatorUseOldGBTiming) +ON_UPDATE_COMMAND_UI(ID_EMULATOR_USE_OLD_FRAME_TIMING, OnUpdateOptionsEmulatorUseOldGBTiming) +ON_COMMAND(ID_EMULATOR_USE_GB_INPUT_KLUDGE, OnOptionsEmulatorUseGBNullInputKludge) +ON_UPDATE_COMMAND_UI(ID_EMULATOR_USE_GB_INPUT_KLUDGE, OnUpdateOptionsEmulatorUseGBNullInputKludge) +ON_COMMAND(ID_EMULATOR_GBALAG, OnOptionsEmulatorGBALag) +ON_UPDATE_COMMAND_UI(ID_EMULATOR_GBALAG, OnUpdateOptionsEmulatorGBALag) + +ON_COMMAND(ID_OPTIONS_EMULATOR_PNGFORMAT, OnOptionsEmulatorPngformat) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_PNGFORMAT, OnUpdateOptionsEmulatorPngformat) +ON_COMMAND(ID_OPTIONS_EMULATOR_BMPFORMAT, OnOptionsEmulatorBmpformat) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_EMULATOR_BMPFORMAT, OnUpdateOptionsEmulatorBmpformat) +ON_COMMAND(ID_OPTIONS_SOUND_DISABLE, OnOptionsSoundDisable) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_DISABLE, OnUpdateOptionsSoundDisable) +ON_COMMAND(ID_OPTIONS_SOUND_MUTE, OnOptionsSoundMute) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_MUTE, OnUpdateOptionsSoundMute) +ON_COMMAND(ID_OPTIONS_SOUND_OFF, OnOptionsSoundOff) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_OFF, OnUpdateOptionsSoundOff) +ON_COMMAND(ID_OPTIONS_SOUND_ON, OnOptionsSoundOn) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_ON, OnUpdateOptionsSoundOn) +ON_COMMAND(ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION, OnOptionsSoundUseoldsynchronization) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION, OnUpdateOptionsSoundUseoldsynchronization) +ON_COMMAND(ID_OPTIONS_SOUND_ECHO, OnOptionsSoundEcho) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_ECHO, OnUpdateOptionsSoundEcho) +ON_COMMAND(ID_OPTIONS_SOUND_LOWPASSFILTER, OnOptionsSoundLowpassfilter) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_LOWPASSFILTER, OnUpdateOptionsSoundLowpassfilter) +ON_COMMAND(ID_OPTIONS_SOUND_REVERSESTEREO, OnOptionsSoundReversestereo) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_REVERSESTEREO, OnUpdateOptionsSoundReversestereo) +ON_COMMAND(ID_OPTIONS_SOUND_MUTEFRAMEADVANCE, OnOptionsSoundMuteFrameAdvance) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_MUTEFRAMEADVANCE, OnUpdateOptionsSoundMuteFrameAdvance) +ON_COMMAND(ID_OPTIONS_SOUND_MUTEWHENINACTIVE, OnOptionsSoundMuteWhenInactive) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_MUTEWHENINACTIVE, OnUpdateOptionsSoundMuteWhenInactive) +ON_COMMAND(ID_OPTIONS_SOUND_11KHZ, OnOptionsSound11khz) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_11KHZ, OnUpdateOptionsSound11khz) +ON_COMMAND(ID_OPTIONS_SOUND_22KHZ, OnOptionsSound22khz) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_22KHZ, OnUpdateOptionsSound22khz) +ON_COMMAND(ID_OPTIONS_SOUND_44KHZ, OnOptionsSound44khz) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_44KHZ, OnUpdateOptionsSound44khz) +ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL1, OnOptionsSoundChannel1) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL1, OnUpdateOptionsSoundChannel1) +ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL2, OnOptionsSoundChannel2) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL2, OnUpdateOptionsSoundChannel2) +ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL3, OnOptionsSoundChannel3) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL3, OnUpdateOptionsSoundChannel3) +ON_COMMAND(ID_OPTIONS_SOUND_CHANNEL4, OnOptionsSoundChannel4) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_CHANNEL4, OnUpdateOptionsSoundChannel4) +ON_COMMAND(ID_OPTIONS_SOUND_DIRECTSOUNDA, OnOptionsSoundDirectsounda) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_DIRECTSOUNDA, OnUpdateOptionsSoundDirectsounda) +ON_COMMAND(ID_OPTIONS_SOUND_DIRECTSOUNDB, OnOptionsSoundDirectsoundb) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_DIRECTSOUNDB, OnUpdateOptionsSoundDirectsoundb) +ON_COMMAND(ID_OPTIONS_GAMEBOY_BORDER, OnOptionsGameboyBorder) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_BORDER, OnUpdateOptionsGameboyBorder) +ON_COMMAND(ID_OPTIONS_GAMEBOY_PRINTER, OnOptionsGameboyPrinter) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_PRINTER, OnUpdateOptionsGameboyPrinter) +ON_COMMAND(ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC, OnOptionsGameboyBorderAutomatic) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC, OnUpdateOptionsGameboyBorderAutomatic) +ON_COMMAND(ID_OPTIONS_GAMEBOY_AUTOMATIC, OnOptionsGameboyAutomatic) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_AUTOMATIC, OnUpdateOptionsGameboyAutomatic) +ON_COMMAND(ID_OPTIONS_GAMEBOY_GBA, OnOptionsGameboyGba) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GBA, OnUpdateOptionsGameboyGba) +ON_COMMAND(ID_OPTIONS_GAMEBOY_CGB, OnOptionsGameboyCgb) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_CGB, OnUpdateOptionsGameboyCgb) +ON_COMMAND(ID_OPTIONS_GAMEBOY_SGB, OnOptionsGameboySgb) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_SGB, OnUpdateOptionsGameboySgb) +ON_COMMAND(ID_OPTIONS_GAMEBOY_SGB2, OnOptionsGameboySgb2) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_SGB2, OnUpdateOptionsGameboySgb2) +ON_COMMAND(ID_OPTIONS_GAMEBOY_GB, OnOptionsGameboyGb) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GB, OnUpdateOptionsGameboyGb) +ON_COMMAND(ID_OPTIONS_GAMEBOY_REALCOLORS, OnOptionsGameboyRealcolors) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_REALCOLORS, OnUpdateOptionsGameboyRealcolors) +ON_COMMAND(ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS, OnOptionsGameboyGameboycolors) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS, OnUpdateOptionsGameboyGameboycolors) +ON_COMMAND(ID_OPTIONS_GAMEBOY_COLORS, OnOptionsGameboyColors) +ON_COMMAND(ID_OPTIONS_FILTER_DISABLEMMX, OnOptionsFilterDisablemmx) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_FILTER_DISABLEMMX, OnUpdateOptionsFilterDisablemmx) +ON_COMMAND(ID_OPTIONS_LANGUAGE_SYSTEM, OnOptionsLanguageSystem) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_SYSTEM, OnUpdateOptionsLanguageSystem) +ON_COMMAND(ID_OPTIONS_LANGUAGE_ENGLISH, OnOptionsLanguageEnglish) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_ENGLISH, OnUpdateOptionsLanguageEnglish) +ON_COMMAND(ID_OPTIONS_LANGUAGE_OTHER, OnOptionsLanguageOther) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_LANGUAGE_OTHER, OnUpdateOptionsLanguageOther) +ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_1, OnOptionsJoypadConfigure1) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_1, OnUpdateOptionsJoypadConfigure1) +ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_2, OnOptionsJoypadConfigure2) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_2, OnUpdateOptionsJoypadConfigure2) +ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_3, OnOptionsJoypadConfigure3) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_3, OnUpdateOptionsJoypadConfigure3) +ON_COMMAND(ID_OPTIONS_JOYPAD_CONFIGURE_4, OnOptionsJoypadConfigure4) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_CONFIGURE_4, OnUpdateOptionsJoypadConfigure4) +ON_COMMAND(ID_OPTIONS_JOYPAD_MOTIONCONFIGURE, OnOptionsJoypadMotionconfigure) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_MOTIONCONFIGURE, OnUpdateOptionsJoypadMotionconfigure) +ON_COMMAND(ID_OPTIONS_JOYPAD_ALLOWLEFTRIGHT, OnOptionsJoypadAllowLeftRight) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_ALLOWLEFTRIGHT, OnUpdateOptionsJoypadAllowLeftRight) +ON_COMMAND(ID_OPTIONS_JOYPAD_AUTOFIRE_ACCOUNTFORLAG, OnOptionsJoypadAutofireAccountForLag) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_JOYPAD_AUTOFIRE_ACCOUNTFORLAG, OnUpdateOptionsJoypadAutofireAccountForLag) +ON_COMMAND(ID_CHEATS_SEARCHFORCHEATS, OnCheatsSearchforcheats) +ON_UPDATE_COMMAND_UI(ID_CHEATS_SEARCHFORCHEATS, OnUpdateCheatsSearchforcheats) +ON_COMMAND(ID_CHEATS_CHEATLIST, OnCheatsCheatlist) +ON_UPDATE_COMMAND_UI(ID_CHEATS_CHEATLIST, OnUpdateCheatsCheatlist) +ON_COMMAND(ID_CHEATS_AUTOMATICSAVELOADCHEATS, OnCheatsAutomaticsaveloadcheats) +ON_COMMAND(ID_CHEATS_PAUSEDURINGCHEATSEARCH, OnCheatsPauseDuringCheatSearch) +ON_COMMAND(ID_CHEATS_LOADCHEATLIST, OnCheatsLoadcheatlist) +ON_UPDATE_COMMAND_UI(ID_CHEATS_LOADCHEATLIST, OnUpdateCheatsLoadcheatlist) +ON_COMMAND(ID_CHEATS_SAVECHEATLIST, OnCheatsSavecheatlist) +ON_UPDATE_COMMAND_UI(ID_CHEATS_SAVECHEATLIST, OnUpdateCheatsSavecheatlist) +ON_COMMAND(ID_TOOLS_DISASSEMBLE, OnToolsDisassemble) +ON_UPDATE_COMMAND_UI(ID_TOOLS_DISASSEMBLE, OnUpdateToolsDisassemble) +ON_COMMAND(ID_TOOLS_LOGGING, OnToolsLogging) +ON_UPDATE_COMMAND_UI(ID_TOOLS_LOGGING, OnUpdateToolsLogging) +ON_COMMAND(ID_TOOLS_IOVIEWER, OnToolsIoviewer) +ON_UPDATE_COMMAND_UI(ID_TOOLS_IOVIEWER, OnUpdateToolsIoviewer) +ON_COMMAND(ID_TOOLS_MAPVIEW, OnToolsMapview) +ON_UPDATE_COMMAND_UI(ID_TOOLS_MAPVIEW, OnUpdateToolsMapview) +ON_COMMAND(ID_TOOLS_MEMORYVIEWER, OnToolsMemoryviewer) +ON_UPDATE_COMMAND_UI(ID_TOOLS_MEMORYVIEWER, OnUpdateToolsMemoryviewer) +ON_COMMAND(ID_TOOLS_OAMVIEWER, OnToolsOamviewer) +ON_UPDATE_COMMAND_UI(ID_TOOLS_OAMVIEWER, OnUpdateToolsOamviewer) +ON_COMMAND(ID_TOOLS_PALETTEVIEW, OnToolsPaletteview) +ON_UPDATE_COMMAND_UI(ID_TOOLS_PALETTEVIEW, OnUpdateToolsPaletteview) +ON_COMMAND(ID_TOOLS_TILEVIEWER, OnToolsTileviewer) +ON_UPDATE_COMMAND_UI(ID_TOOLS_TILEVIEWER, OnUpdateToolsTileviewer) + +ON_COMMAND(ID_DEBUG_NEXTFRAME, OnDebugNextframe) +ON_UPDATE_COMMAND_UI(ID_DEBUG_NEXTFRAME, OnUpdateDebugNextframe) +ON_COMMAND(ID_DEBUG_NEXTFRAME_ACCOUNTFORLAG, OnDebugNextframeAccountForLag) +ON_UPDATE_COMMAND_UI(ID_DEBUG_NEXTFRAME_ACCOUNTFORLAG, OnUpdateDebugNextframeAccountForLag) +ON_COMMAND(ID_DEBUG_FRAMESEARCH, OnDebugFramesearch) +ON_UPDATE_COMMAND_UI(ID_DEBUG_FRAMESEARCH, OnUpdateDebugFramesearch) +ON_COMMAND(ID_DEBUG_FRAMESEARCHPREV, OnDebugFramesearchPrev) +ON_UPDATE_COMMAND_UI(ID_DEBUG_FRAMESEARCHPREV, OnUpdateDebugFramesearchPrev) +ON_COMMAND(ID_DEBUG_FRAMESEARCHLOAD, OnDebugFramesearchLoad) +ON_UPDATE_COMMAND_UI(ID_DEBUG_FRAMESEARCHLOAD, OnUpdateDebugFramesearchLoad) +ON_UPDATE_COMMAND_UI(ID_CHEATS_AUTOMATICSAVELOADCHEATS, OnUpdateCheatsAutomaticsaveloadcheats) +ON_UPDATE_COMMAND_UI(ID_CHEATS_PAUSEDURINGCHEATSEARCH, OnUpdateCheatsPauseDuringCheatSearch) +ON_COMMAND(ID_TOOLS_FRAMECOUNTER, OnToolsFrameCounter) +ON_UPDATE_COMMAND_UI(ID_TOOLS_FRAMECOUNTER, OnUpdateToolsFrameCounter) +ON_COMMAND(ID_TOOLS_LAGCOUNTER, OnToolsLagCounter) +ON_UPDATE_COMMAND_UI(ID_TOOLS_LAGCOUNTER, OnUpdateToolsLagCounter) +ON_COMMAND(ID_TOOLS_EXTRACOUNTER, OnToolsExtraCounter) +ON_UPDATE_COMMAND_UI(ID_TOOLS_EXTRACOUNTER, OnUpdateToolsExtraCounter) +ON_COMMAND(ID_TOOLS_EXTRACOUNTERRESET, OnToolsExtraCounterReset) +ON_COMMAND(ID_TOOLS_INPUTDISPLAY, OnToolsInputDisplay) +ON_UPDATE_COMMAND_UI(ID_TOOLS_INPUTDISPLAY, OnUpdateToolsInputDisplay) +ON_COMMAND(ID_TOOLS_DEBUG_GDB, OnToolsDebugGdb) +ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_GDB, OnUpdateToolsDebugGdb) +ON_COMMAND(ID_TOOLS_DEBUG_LOADANDWAIT, OnToolsDebugLoadandwait) +ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_LOADANDWAIT, OnUpdateToolsDebugLoadandwait) +ON_COMMAND(ID_TOOLS_DEBUG_BREAK, OnToolsDebugBreak) +ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_BREAK, OnUpdateToolsDebugBreak) +ON_COMMAND(ID_TOOLS_DEBUG_DISCONNECT, OnToolsDebugDisconnect) +ON_UPDATE_COMMAND_UI(ID_TOOLS_DEBUG_DISCONNECT, OnUpdateToolsDebugDisconnect) + +ON_COMMAND(ID_TOOLS_SOUNDRECORDING, OnToolsSoundRecording) +ON_UPDATE_COMMAND_UI(ID_TOOLS_SOUNDRECORDING, OnUpdateToolsSoundRecording) +ON_COMMAND(ID_TOOLS_AVIRECORDING, OnToolsAVIRecording) +ON_UPDATE_COMMAND_UI(ID_TOOLS_AVIRECORDING, OnUpdateToolsAVIRecording) +ON_COMMAND(ID_TOOLS_PAUSEAVIRECORDING, OnToolsPauseAVIRecording) +ON_UPDATE_COMMAND_UI(ID_TOOLS_PAUSEAVIRECORDING, OnUpdateToolsPauseAVIRecording) + +ON_COMMAND(ID_MOVIE_RECORD, OnToolsRecordMovie) +ON_UPDATE_COMMAND_UI(ID_MOVIE_RECORD, OnUpdateToolsRecordMovie) +ON_COMMAND(ID_MOVIE_STOP, OnToolsStopMovie) +ON_UPDATE_COMMAND_UI(ID_MOVIE_STOP, OnUpdateToolsStopMovie) +ON_COMMAND(ID_MOVIE_PLAY, OnToolsPlayMovie) +ON_UPDATE_COMMAND_UI(ID_MOVIE_PLAY, OnUpdateToolsPlayMovie) +ON_COMMAND(ID_MOVIE_READONLY, OnToolsPlayReadOnly) +ON_UPDATE_COMMAND_UI(ID_MOVIE_READONLY, OnUpdateToolsPlayReadOnly) +ON_COMMAND(ID_MOVIE_ASSC_WITH_SAVESTATE, OnAsscWithSaveState) +ON_UPDATE_COMMAND_UI(ID_MOVIE_ASSC_WITH_SAVESTATE, OnUpdateAsscWithSaveState) +ON_COMMAND(ID_MOVIE_RESUME_RECORD, OnToolsResumeRecord) +ON_UPDATE_COMMAND_UI(ID_MOVIE_RESUME_RECORD, OnUpdateToolsResumeRecord) +ON_COMMAND(ID_MOVIE_RESTART_PLAY, OnToolsPlayRestart) +ON_UPDATE_COMMAND_UI(ID_MOVIE_RESTART_PLAY, OnUpdateToolsPlayRestart) + +ON_COMMAND(ID_MOVIE_END_PAUSE, OnToolsOnMovieEndPause) +ON_UPDATE_COMMAND_UI(ID_MOVIE_END_PAUSE, OnUpdateToolsOnMovieEndPause) +ON_COMMAND(ID_MOVIE_END_STOP, OnToolsOnMovieEndStop) +ON_UPDATE_COMMAND_UI(ID_MOVIE_END_STOP, OnUpdateToolsOnMovieEndStop) +ON_COMMAND(ID_MOVIE_END_RESTART, OnToolsOnMovieEndRestart) +ON_UPDATE_COMMAND_UI(ID_MOVIE_END_RESTART, OnUpdateToolsOnMovieEndRestart) +ON_COMMAND(ID_MOVIE_END_APPEND, OnToolsOnMovieEndAppend) +ON_UPDATE_COMMAND_UI(ID_MOVIE_END_APPEND, OnUpdateToolsOnMovieEndAppend) +ON_COMMAND(ID_MOVIE_END_KEEP, OnToolsOnMovieEndKeep) +ON_UPDATE_COMMAND_UI(ID_MOVIE_END_KEEP, OnUpdateToolsOnMovieEndKeep) + +ON_COMMAND(ID_MOVIE_TOOL_SET_PAUSE_AT, OnToolsMovieSetPauseAt) +ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_SET_PAUSE_AT, OnUpdateToolsSetMoviePauseAt) +ON_COMMAND(ID_MOVIE_TOOL_CONVERT, OnToolsMovieConvertCurrent) +ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_CONVERT, OnUpdateToolsMovieConvertCurrent) +ON_COMMAND(ID_MOVIE_TOOL_AUTO_CONVERT, OnToolsMovieAutoConvert) +ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_AUTO_CONVERT, OnUpdateToolsMovieAutoConvert) +ON_COMMAND(ID_MOVIE_TOOL_FIX_HEADER, OnToolsMovieFixHeader) +ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_FIX_HEADER, OnUpdateToolsMovieFixHeader) +ON_COMMAND(ID_MOVIE_TOOL_TRUNCATE, OnToolsMovieTruncateAtCurrent) +ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_TRUNCATE, OnUpdateToolsMovieTruncateAtCurrent) +ON_COMMAND(ID_MOVIE_TOOL_EXTRACT_FROM_SAVEGAME, OnToolsMovieExtractFromSavegame) +ON_UPDATE_COMMAND_UI(ID_MOVIE_TOOL_EXTRACT_FROM_SAVEGAME, OnUpdateToolsMovieExtractFromSavegame) + +ON_COMMAND(ID_TOOLS_REWIND, OnToolsRewind) +ON_UPDATE_COMMAND_UI(ID_TOOLS_REWIND, OnUpdateToolsRewind) +ON_COMMAND(ID_TOOLS_CUSTOMIZE, OnToolsCustomize) +ON_UPDATE_COMMAND_UI(ID_TOOLS_CUSTOMIZE, OnUpdateToolsCustomize) +// ON_COMMAND(ID_TOOLS_CUSTOMIZE_COMMON, OnToolsCustomizeCommon) +// ON_UPDATE_COMMAND_UI(ID_TOOLS_CUSTOMIZE_COMMON, OnUpdateToolsCustomizeCommon) +ON_COMMAND(ID_HELP_BUGREPORT, OnHelpBugreport) + +ON_COMMAND_EX_RANGE(ID_FILE_MRU_FILE1, ID_FILE_MRU_FILE10, OnFileRecentFile) +ON_COMMAND_EX_RANGE(ID_FILE_LOADGAME_SLOT1, ID_FILE_LOADGAME_SLOT10, OnFileLoadSlot) +ON_COMMAND_EX_RANGE(ID_FILE_SAVEGAME_SLOT1, ID_FILE_SAVEGAME_SLOT10, OnFileSaveSlot) +ON_COMMAND_EX_RANGE(ID_SELECT_SLOT1, ID_SELECT_SLOT10, OnSelectSlot) +ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_MRU_FILE1, ID_FILE_MRU_FILE10, OnUpdateFileRecentFile) +ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_LOADGAME_SLOT1, ID_FILE_LOADGAME_SLOT10, OnUpdateFileLoadSlot) +ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_SAVEGAME_SLOT1, ID_FILE_SAVEGAME_SLOT10, OnUpdateFileSaveSlot) +ON_UPDATE_COMMAND_UI_RANGE(ID_SELECT_SLOT1, ID_SELECT_SLOT10, OnUpdateSelectSlot) + +ON_COMMAND(ID_FILE_SAVEGAME_OLDESTSLOT, OnFileSavegameOldestslot) +ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_OLDESTSLOT, OnUpdateFileSavegameOldestslot) +ON_COMMAND(ID_FILE_LOADGAME_MOSTRECENT, OnFileLoadgameMostrecent) +ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_MOSTRECENT, OnUpdateFileLoadgameMostrecent) +ON_COMMAND(ID_FILE_SAVEGAME_CURRENT, OnFileSavegameCurrent) +ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_CURRENT, OnUpdateFileSavegameCurrent) +ON_COMMAND(ID_FILE_LOADGAME_CURRENT, OnFileLoadgameCurrent) +ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_CURRENT, OnUpdateFileLoadgameCurrent) +ON_COMMAND(ID_FILE_LOADGAME_MAKECURRENT, OnFileLoadgameMakeCurrent) +ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_MAKECURRENT, OnUpdateFileLoadgameMakeCurrent) +ON_COMMAND(ID_FILE_SAVEGAME_MAKECURRENT, OnFileSavegameMakeCurrent) +ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_MAKECURRENT, OnUpdateFileSavegameMakeCurrent) + +ON_COMMAND(ID_FILE_SAVEGAME_INCREMENTSLOT, OnFileSavegameIncrementSlot) +ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_INCREMENTSLOT, OnUpdateFileSavegameIncrementSlot) +ON_COMMAND(ID_FILE_SAVEGAME_DECREMENTSLOT, OnFileSavegameDecrementSlot) +ON_UPDATE_COMMAND_UI(ID_FILE_SAVEGAME_DECREMENTSLOT, OnUpdateFileSavegameDecrementSlot) +ON_COMMAND(ID_FILE_SLOT_DISPLAYMODIFICATIONTIME, OnFileSlotDisplayModificationTime) +ON_UPDATE_COMMAND_UI(ID_FILE_SLOT_DISPLAYMODIFICATIONTIME, OnUpdateFileSlotDisplayModificationTime) + +ON_COMMAND(ID_FILE_LOADGAME_AUTOLOADMOSTRECENT, OnFileLoadgameAutoloadmostrecent) +ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_AUTOLOADMOSTRECENT, OnUpdateFileLoadgameAutoloadmostrecent) +ON_COMMAND(ID_FILE_LOADGAME_MAKERECENT, OnFileLoadgameMakeRecent) +ON_UPDATE_COMMAND_UI(ID_FILE_LOADGAME_MAKERECENT, OnUpdateFileLoadgameMakeRecent) + +ON_COMMAND(ID_OPTIONS_SOUND_VOLUME_25X, OnOptionsSoundVolume25x) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_VOLUME_25X, OnUpdateOptionsSoundVolume25x) +ON_COMMAND(ID_OPTIONS_SOUND_VOLUME_5X, OnOptionsSoundVolume5x) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_SOUND_VOLUME_5X, OnUpdateOptionsSoundVolume5x) +ON_COMMAND(ID_CHEATS_DISABLECHEATS, OnCheatsDisablecheats) +ON_UPDATE_COMMAND_UI(ID_CHEATS_DISABLECHEATS, OnUpdateCheatsDisablecheats) +ON_COMMAND(ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE, OnOptionsVideoFullscreenmaxscale) + +ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_FRAMESKIP_0, ID_OPTIONS_VIDEO_FRAMESKIP_9, OnOptionsFrameskip) +ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_X1, ID_OPTIONS_VIDEO_X4, OnOptionVideoSize) +ON_COMMAND_EX_RANGE(ID_OPTIONS_VIDEO_LAYERS_BG0, ID_OPTIONS_VIDEO_LAYERS_OBJWIN, OnVideoLayer) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_VIDEO_LAYERS_BG0, ID_OPTIONS_VIDEO_LAYERS_OBJWIN, OnUpdateVideoLayer) +ON_COMMAND(ID_SYSTEM_MINIMIZE, OnSystemMinimize) +ON_COMMAND(ID_SYSTEM_MAXIMIZE, OnSystemMaximize) +ON_COMMAND_EX_RANGE(ID_OPTIONS_EMULATOR_SHOWSPEED_NONE, ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT, OnOptionsEmulatorShowSpeed) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_EMULATOR_SHOWSPEED_NONE, + ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT, + OnUpdateOptionsEmulatorShowSpeed) +ON_COMMAND_EX_RANGE(ID_OPTIONS_SOUND_VOLUME_1X, ID_OPTIONS_SOUND_VOLUME_4X, OnOptionsSoundVolume) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_SOUND_VOLUME_1X, ID_OPTIONS_SOUND_VOLUME_4X, OnUpdateOptionsSoundVolume) +ON_COMMAND_EX_RANGE(ID_OPTIONS_PRIORITY_HIGHEST, ID_OPTIONS_PRIORITY_BELOWNORMAL, OnOptionsPriority) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_PRIORITY_HIGHEST, ID_OPTIONS_PRIORITY_BELOWNORMAL, OnUpdateOptionsPriority) + +ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_NORMAL, ID_OPTIONS_FILTER_TVMODE, OnOptionsFilter) +ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL, ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL, OnOptionsFilter) +ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X, ID_OPTIONS_FILTER16BIT_SIMPLE2X, OnOptionsFilter) +ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_BILINEAR, ID_OPTIONS_FILTER_BILINEARPLUS, OnOptionsFilter) +ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_SCANLINES, ID_OPTIONS_FILTER_SCANLINES, OnOptionsFilter) +ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_LQ2X, ID_OPTIONS_FILTER_HQ3X2, OnOptionsFilter) +ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER16BIT_SIMPLE3X, ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL4X, OnOptionsFilter) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_NORMAL, ID_OPTIONS_FILTER_TVMODE, OnUpdateOptionsFilter) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL, ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL, OnUpdateOptionsFilter) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X, ID_OPTIONS_FILTER16BIT_SIMPLE2X, OnUpdateOptionsFilter) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_BILINEAR, ID_OPTIONS_FILTER_BILINEARPLUS, OnUpdateOptionsFilter) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_SCANLINES, ID_OPTIONS_FILTER_SCANLINES, OnUpdateOptionsFilter) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_LQ2X, ID_OPTIONS_FILTER_HQ3X2, OnUpdateOptionsFilter) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER16BIT_SIMPLE3X, ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL4X, OnUpdateOptionsFilter) +ON_COMMAND_EX_RANGE(ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE, ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART, OnOptionsFilterIFB) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE, + ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART, + OnUpdateOptionsFilterIFB) + +ON_COMMAND_EX_RANGE(ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1, ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4, OnOptionsJoypadDefault) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1, ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4, OnUpdateOptionsJoypadDefault) +ON_COMMAND_EX_RANGE(ID_OPTIONS_JOYPAD_AUTOFIRE_A, ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR, OnOptionsJoypadAutofire) +ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_JOYPAD_AUTOFIRE_A, ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR, OnUpdateOptionsJoypadAutofire) +ON_COMMAND_EX_RANGE(ID_STICKY_A, ID_STICKY_CLEAR, OnOptionsJoypadSticky) +ON_UPDATE_COMMAND_UI_RANGE(ID_STICKY_A, ID_STICKY_CLEAR, OnUpdateOptionsJoypadSticky) +ON_MESSAGE(VBA_CONFIRM_MODE, OnConfirmMode) +ON_MESSAGE(WM_SYSCOMMAND, OnMySysCommand) +ON_COMMAND(ID_OPTIONS_VIDEO_TEXTDISPLAYOPTIONS, OnOptionsVideoTextdisplayoptions) +ON_UPDATE_COMMAND_UI(ID_OPTIONS_VIDEO_TEXTDISPLAYOPTIONS, OnUpdateOptionsVideoTextdisplayoptions) + +ON_COMMAND(ID_FILE_LUA_OPEN, OnFileLuaOpen) +ON_UPDATE_COMMAND_UI(ID_FILE_LUA_OPEN, OnUpdateFileLuaOpen) +ON_COMMAND(ID_FILE_LUA_CLOSE_ALL, OnFileLuaCloseAll) +ON_UPDATE_COMMAND_UI(ID_FILE_LUA_CLOSE_ALL, OnUpdateFileLuaCloseAll) +ON_COMMAND(ID_FILE_LUA_RELOAD, OnFileLuaReload) +ON_COMMAND(ID_FILE_LUA_STOP, OnFileLuaStop) +ON_COMMAND(ID_RAM_SEARCH, OnFileRamSearch) +ON_UPDATE_COMMAND_UI(ID_RAM_SEARCH, OnUpdateFileRamSearch) +ON_COMMAND(ID_RAM_WATCH, OnFileRamWatch) +ON_UPDATE_COMMAND_UI(ID_RAM_WATCH, OnUpdateFileRamWatch) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// MainWnd message handlers + +bool vbaShuttingDown = false; + +void MainWnd::OnClose() +{ + vbaShuttingDown = true; // HACK to fix crash on exit while memory viewer is open + + CWnd::OnClose(); + + delete this; +} + +void MainWnd::OnMove(int x, int y) +{ + CWnd::OnMove(x, y); + + if (!theApp.changingVideoSize) + { + if (this) + { + if (!IsIconic() && !IsZoomed()) + { + RECT r; + + GetWindowRect(&r); + theApp.windowPositionX = r.left; + theApp.windowPositionY = r.top; + theApp.adjustDestRect(); + regSetDwordValue("windowX", theApp.windowPositionX); + regSetDwordValue("windowY", theApp.windowPositionY); + } + } + } +} + +void MainWnd::OnSize(UINT nType, int cx, int cy) +{ + CWnd::OnSize(nType, cx, cy); + + static int lastType = -1; + + // FIXME: hack to re-maximize window after it auto-unmaximizes while loading a ROM + if (nType == SIZE_MAXIMIZED && lastType == SIZE_MAXIMIZED) + { + lastType = -1; + ShowWindow(SW_SHOWMAXIMIZED); + MoveWindow(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); + return; + } + + lastType = nType; + + if (IsIconic()) + { + theApp.iconic = true; + return; + } + else if (theApp.iconic) + { + theApp.iconic = false; + } + + if (!theApp.changingVideoSize) + { + if (this) + { + if (theApp.videoOption <= VIDEO_4X) + { + theApp.surfaceSizeX = cx; + theApp.surfaceSizeY = cy; + theApp.adjustDestRect(); + if (theApp.display) + theApp.display->resize(theApp.dest.right - theApp.dest.left, theApp.dest.bottom - theApp.dest.top); + systemRefreshScreen(); // useful when shrinking + } + } + } +} + +void MainWnd::OnContextMenu(CWnd *pWnd, CPoint point) +{ + winMouseOn(); +} + +void MainWnd::OnSystemMinimize() +{ + ShowWindow(SW_SHOWMINIMIZED); +} + +void MainWnd::OnSystemMaximize() +{ + ShowWindow(SW_SHOWMAXIMIZED); +} + +void MainWnd::OnPaint() +{ + CPaintDC dc(this); // device context for painting, calling BeginPaint/EndPaint internally + if (emulating && (!theApp.active || theApp.paused)) + { + systemRedrawScreen(); + } +} + +static bool translatingAccelerator = false; + +// FIXME: this fix for accel keys is ugly +// using too many static variables for a single accel key kludge +static bool recursiveCall = true; +static bool fullUpdated = false; +static bool lastKeyModifier = false; // maybe better check current key press status instead +static WPARAM lastKey = 0; + +BOOL MainWnd::PreTranslateMessage(MSG *pMsg) +{ + if (RamSearchHWnd && ::IsDialogMessage(RamSearchHWnd, pMsg)) + { + return TRUE; + } + else if (RamWatchHWnd && ::IsDialogMessage(RamWatchHWnd, pMsg)) + { + if (RamWatchAccels) + TranslateAccelerator(RamWatchHWnd, RamWatchAccels, pMsg); + return TRUE; + } + else if (LuaConsoleHWnd && ::IsDialogMessage(LuaConsoleHWnd, pMsg)) + { + return TRUE; + } + else if (CWnd::PreTranslateMessage(pMsg)) + { + return TRUE; + } + else if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN) + { + translatingAccelerator = true; + + bool bHit = theApp.hAccel != NULL && ::TranslateAccelerator(m_hWnd, theApp.hAccel, pMsg); + bool isModifier = pMsg->wParam == VK_SHIFT || pMsg->wParam == VK_CONTROL || pMsg->wParam == VK_MENU; + + // HACK to get around the fact that TranslateAccelerator can't handle modifier-only accelerators + // (it would be better to fix TranslateAccelerator, but its code is in a Microsoft library...) + if (!bHit) + { + if (isModifier) + { + // do a linear loop through all accelerators to find modifier-only ones... + CCmdAccelOb *pCmdAccel; + WORD wKey; + CAccelsOb * pAccelOb; + POSITION pos = theApp.winAccelMgr.m_mapAccelTable.GetStartPosition(); + const int modifiers = ((pMsg->wParam == VK_SHIFT) ? FSHIFT : ((pMsg->wParam == VK_CONTROL) ? FCONTROL : FALT)); + while (pos != NULL) + { + theApp.winAccelMgr.m_mapAccelTable.GetNextAssoc(pos, wKey, pCmdAccel); + POSITION pos = pCmdAccel->m_Accels.GetHeadPosition(); + while (pos != NULL) + { + pAccelOb = pCmdAccel->m_Accels.GetNext(pos); + + if (pAccelOb->m_wKey == 0) // if accelerator-only + { + if ((pAccelOb->m_cVirt & modifiers) == modifiers) // if modifier matches + { + bHit = true; + SendMessage(WM_COMMAND, pCmdAccel->m_wIDCommand, 0); // tell Windows to call the right function + } + } + } + } + } + + if (!bHit) + { + lastKeyModifier = true; + } + } + + if (bHit) + { + if (lastKeyModifier && !isModifier) + { + fullUpdated = false; + lastKeyModifier = false; + } + + if (lastKey != pMsg->wParam) + { + fullUpdated = false; + lastKey = pMsg->wParam; + } + } + + translatingAccelerator = false; + return bHit ? TRUE : FALSE; + } + + return FALSE; +} + +void MainWnd::OnMouseMove(UINT nFlags, CPoint point) +{ + winMouseOn(); + + CWnd::OnMouseMove(nFlags, point); +} + +// recursive kludge +static void InitMenuKludge(CMenu *pParentMenu, CMenu *pMenu, CCmdTarget *pWnd) +{ + ASSERT(pMenu != NULL); + + CCmdUI state; + state.m_pParentMenu = pParentMenu; + state.m_pMenu = pMenu; + ASSERT(state.m_pOther == NULL); + + state.m_nIndexMax = pMenu->GetMenuItemCount(); + for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; + state.m_nIndex++) + { + state.m_nID = pMenu->GetMenuItemID(state.m_nIndex); + if (state.m_nID == 0) + continue; // menu separator or invalid cmd - ignore it + + ASSERT(state.m_pOther == NULL); + ASSERT(state.m_pMenu != NULL); + if (state.m_nID == (UINT)-1) + { + // possibly a popup menu, route to first item of that popup + state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex); + if (state.m_pSubMenu == NULL) + { + continue; // first item of popup can't be routed to + } + + state.DoUpdate(pWnd, false); + if (recursiveCall) + { + // FIXME: slow recursive calls to fix enabling/disabling of accel keys + InitMenuKludge(state.m_pMenu, state.m_pSubMenu, pWnd); + } + } + else + { + // normal menu item + // Auto enable/disable if frame window has 'm_bAutoMenuEnable' + // set and command is _not_ a system command. + state.m_pSubMenu = NULL; + state.DoUpdate(pWnd, state.m_nID < 0xF000); + } + + // adjust for menu deletions and additions + UINT nCount = pMenu->GetMenuItemCount(); + if (nCount < state.m_nIndexMax) + { + state.m_nIndex -= (state.m_nIndexMax - nCount); + while (state.m_nIndex < nCount && + pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID) + { + state.m_nIndex++; + } + } + state.m_nIndexMax = nCount; + } +} + +void MainWnd::OnInitMenuPopup(CMenu *pMenu, UINT nIndex, BOOL bSysMenu) +{ + ASSERT(pMenu != NULL); + + CCmdUI state; + state.m_pMenu = pMenu; + ASSERT(state.m_pOther == NULL); + ASSERT(state.m_pParentMenu == NULL); + + // determine if menu is popup in top-level menu and set m_pOther to + // it if so (m_pParentMenu == NULL indicates that it is secondary popup) + HMENU hParentMenu; + if (AfxGetThreadState()->m_hTrackingMenu == pMenu->m_hMenu) + state.m_pParentMenu = pMenu; // parent == child for tracking popup + else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL) + { + CWnd *pParent = GetTopLevelParent(); + // children windows don't have menus -- need to go to the top! + if (pParent != NULL && + (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL) + { + int nIndexMax = ::GetMenuItemCount(hParentMenu); + for (int nIndex = 0; nIndex < nIndexMax; nIndex++) + { + if (::GetSubMenu(hParentMenu, nIndex) == pMenu->m_hMenu) + { + // when popup is found, m_pParentMenu is containing menu + state.m_pParentMenu = CMenu::FromHandle(hParentMenu); + break; + } + } + } + } + + // FIXME: magic to workaround the accel key bug without slowing down too much + if (translatingAccelerator && !fullUpdated && state.m_pParentMenu == &theApp.m_menu) + { + state.m_pMenu = state.m_pParentMenu; + recursiveCall = true; + fullUpdated = true; + } + else if (!translatingAccelerator && fullUpdated) + { + fullUpdated = false; + } + + InitMenuKludge(state.m_pParentMenu, state.m_pMenu, this); + + recursiveCall = false; +} + +void MainWnd::OnInitMenu(CMenu *pMenu) +{ +// CWnd::OnInitMenu(pMenu); + + if (translatingAccelerator) + {} + else + { + // HACK: we only want to call this if the user is pulling down the menu, + // but TranslateAccelerator also causes OnInitMenu to be called, so ignore that + + systemSoundPause(); + } +} + +void MainWnd::OnActivate(UINT nState, CWnd *pWndOther, BOOL bMinimized) +{ + CWnd::OnActivate(nState, pWndOther, bMinimized); + + bool activated = (nState == WA_ACTIVE || nState == WA_CLICKACTIVE) && !bMinimized; + + theApp.active = activated || !theApp.pauseWhenInactive; + + extern bool inputActive; + inputActive = activated || (!theApp.pauseWhenInactive && theApp.enableBackgroundInput); + + if (theApp.active) + { + if (theApp.input) + { + theApp.input->activate(); + } + + if (!theApp.paused && emulating) + { + systemSoundResume(); + } + } + else + { + theApp.wasPaused = true; + + if (!theApp.paused && emulating) + { + systemSoundPause(); + } + + memset(theApp.delta, 255, sizeof(theApp.delta)); + } + + if (theApp.muteWhenInactive) + { + theApp.winMuteForNow = !activated; + } + + if (theApp.paused && emulating) + { + systemRefreshScreen(); + } +} + +#if _MSC_VER <= 1200 +void MainWnd::OnActivateApp(BOOL bActive, HTASK hTask) +#else +void MainWnd::OnActivateApp(BOOL bActive, DWORD hTask) +#endif +{ + CWnd::OnActivateApp(bActive, hTask); + + if (theApp.tripleBuffering && theApp.videoOption > VIDEO_4X) + { + if (bActive) + { + if (theApp.display) + theApp.display->clear(); + } + } +} + +LRESULT MainWnd::OnMySysCommand(WPARAM wParam, LPARAM lParam) +{ + if (emulating && !theApp.paused) + { + if ((wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER) + return 0; + } + return Default(); +} + +void MainWnd::OnDropFiles(HDROP hDropInfo) +{ + // FIXME: required for the accel key fix + fullUpdated = false; + + systemSoundClearBuffer(); + + char szFile[1024]; + char ext[1024]; + + if (DragQueryFile(hDropInfo, 0, szFile, 1024)) + { + DragFinish(hDropInfo); + + _splitpath(szFile, NULL, NULL, NULL, ext); + if (strcasecmp(ext, ".lua") == 0) + { + if (VBALoadLuaCode(szFile)) + { + // success, there is nothing to do + } + else + { + // Errors are displayed by the Lua code. + } + } + else if (strcasecmp(ext, ".vbm") == 0) + { + SMovie movieInfo; + char * movieName = szFile; + char romTitle [12]; + uint32 romGameCode; + uint16 checksum; + uint8 crc; + + if (VBAMovieGetInfo(movieName, &movieInfo) != MOVIE_SUCCESS) + { + return; + } + + int cartType = movieInfo.header.typeFlags & 1 ? 0 : 1; + + if (!emulating) + { + theApp.winCheckFullscreen(); + if (winFileOpenSelect(cartType)) + { + if (VBAMovieActive()) + VBAMovieStop(false); // will only get here on user selecting to play a ROM, canceling movie + if (!winFileRun()) + return; + } + else + return; + } + VBAMovieGetRomInfo(movieInfo, romTitle, romGameCode, checksum, crc); + + while (movieInfo.header.romCRC != crc + || strncmp(movieInfo.header.romTitle, romTitle, 12) != 0 + || movieInfo.header.romOrBiosChecksum != checksum + && !((movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) == 0 && checksum == 0)) + { + char msg[1024], warning1[1024], warning2[1024], buffer[1024]; + + strcpy(warning1, ""); + strcpy(warning2, ""); + { + char str [13]; + strncpy(str, movieInfo.header.romTitle, 12); + str[12] = '\0'; + sprintf(buffer, "title=%s ", str); + strcat(warning1, buffer); + + strncpy(str, romTitle, 12); + str[12] = '\0'; + sprintf(buffer, "title=%s ", str); + strcat(warning2, buffer); + } + { + sprintf(buffer, "type=%s ", + (movieInfo.header.typeFlags & MOVIE_TYPE_GBA) ? + "GBA" : (movieInfo.header.typeFlags & MOVIE_TYPE_GBC) ? + "GBC" : (movieInfo.header.typeFlags & MOVIE_TYPE_SGB) ? "SGB" : "GB"); + strcat(warning1, buffer); + + sprintf(buffer, "type=%s ", systemCartridgeType == + 0 ? "GBA" : (gbRom[0x143] & 0x80 ? "GBC" : (gbRom[0x146] == 0x03 ? "SGB" : "GB"))); + strcat(warning2, buffer); + } + { + sprintf(buffer, "crc=%02x ", movieInfo.header.romCRC); + strcat(warning1, buffer); + + sprintf(buffer, "crc=%02x ", crc); + strcat(warning2, buffer); + } + { + char code [5]; + if (movieInfo.header.typeFlags & MOVIE_TYPE_GBA) + { + memcpy(code, &movieInfo.header.romGameCode, 4); + code[4] = '\0'; + sprintf(buffer, "code=%s ", code); + strcat(warning1, buffer); + } + + if (systemCartridgeType == 0) + { + memcpy(code, &romGameCode, 4); + code[4] = '\0'; + sprintf(buffer, "code=%s ", code); + strcat(warning2, buffer); + } + } + { + sprintf(buffer, + movieInfo.header.typeFlags & + MOVIE_TYPE_GBA ? ((movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) == + 0 ? "(bios=none) " : "(bios=%04x) ") : "check=%04x ", + movieInfo.header.romOrBiosChecksum); + strcat(warning1, buffer); + + sprintf(buffer, + checksum == 0 ? "(bios=none) " : systemCartridgeType == 0 ? "(bios=%04x) " : "check=%04x ", + checksum); + strcat(warning2, buffer); + } + + strcpy(msg, ""); + sprintf(buffer, "Movie ROM: %s\n", warning1); + strcat(msg, buffer); + sprintf(buffer, "Your ROM: %s\n", warning2); + strcat(msg, buffer); + strcat(msg, "still want to play the movie?"); + + int sel = MessageBox(msg, TEXT("ROM Mismatch"), MB_ABORTRETRYIGNORE | MB_ICONQUESTION); + switch (sel) + { + case IDABORT: + return; + case IDRETRY: + theApp.winCheckFullscreen(); + if (winFileOpenSelect(cartType)) + { + if (VBAMovieActive()) + VBAMovieStop(false); // will only get here on user selecting to play a ROM, canceling movie + if (!winFileRun()) + return; + VBAMovieGetRomInfo(movieInfo, romTitle, romGameCode, checksum, crc); + } + else + return; + break; + default: + goto romcheck_exit; + } + } +romcheck_exit: + + bool useBIOSFile = (movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0; + if (useBIOSFile) + { + extern bool systemLoadBIOS(const char *biosFileName, bool useBiosFile); + if (!systemLoadBIOS(theApp.biosFileName, useBIOSFile)) + { + systemMessage(0, "This movie requires a valid GBA BIOS file to play.\nPlease locate a BIOS file."); + ((MainWnd *)theApp.m_pMainWnd)->OnOptionsEmulatorSelectbiosfile(); + if (!systemLoadBIOS(theApp.biosFileName, useBIOSFile)) + { + systemMessage(0, "\"%s\" is not a valid BIOS file; cannot play movie without one.", theApp.biosFileName); + return; + } + } + } + + int code = VBAMovieOpen(movieName, TRUE); + + if (code != MOVIE_SUCCESS) + { + if (code == MOVIE_FILE_NOT_FOUND) + systemMessage(0, "Could not find movie file \"%s\".", (const char *)movieName); + else if (code == MOVIE_WRONG_FORMAT) + systemMessage(0, "Movie file \"%s\" is not in proper VBM format.", (const char *)movieName); + else if (code == MOVIE_WRONG_VERSION) + systemMessage(0, "Movie file \"%s\" is not a supported version.", (const char *)movieName); + else + systemMessage(0, "Failed to open movie \"%s\".", (const char *)movieName); + return; + } + } + else if (strcasecmp(ext, ".wch") == 0) + { + if (emulating) + { + MainWnd::OnFileRamWatch(); + Load_Watches(true, szFile); + } + } + else + { + theApp.romFilename = szFile; + if (winFileRun()) + { + SetForegroundWindow(); + } + } + } + else + DragFinish(hDropInfo); +} + +///////////////////// + +void MainWnd::winMouseOn() +{ + SetCursor(arrow); + if (theApp.videoOption > VIDEO_4X) + { + theApp.mouseCounter = 120; + } + else + theApp.mouseCounter = 0; +} + +void MainWnd::winConfirmMode() +{ + if (theApp.renderMethod == DIRECT_DRAW && theApp.videoOption > VIDEO_4X) + { + theApp.winCheckFullscreen(); + ModeConfirm dlg(this); + + if (!dlg.DoModal()) + { + theApp.updateVideoSize(ID_OPTIONS_VIDEO_X2); + } + } +} + +bool MainWnd::winFileOpenSelect(int cartridgeType) +{ + int selectedFilter = regQueryDwordValue("selectedFilter", 0); + if (selectedFilter < 0 || selectedFilter > 2) + selectedFilter = 0; + + LPCTSTR exts[] = { NULL }; + CString filter = winResLoadFilter(IDS_FILTER_ROM); + CString title = winResLoadString(IDS_SELECT_ROM); + + bool isOverrideEmpty = false; + CString initialDir = regQueryStringValue(cartridgeType == 0 ? IDS_ROM_DIR : IDS_GBXROM_DIR, NULL); + if (initialDir.IsEmpty()) + { + isOverrideEmpty = true; + CString altDir = regQueryStringValue(cartridgeType != 0 ? IDS_ROM_DIR : IDS_GBXROM_DIR, NULL); + initialDir = altDir.IsEmpty() ? theApp.exeDir : altDir; + } + + FileDlg dlg(this, "", filter, selectedFilter, "ROM", exts, initialDir, title, false, true); + + if (dlg.DoModal() == IDOK) + { + regSetDwordValue("selectedFilter", dlg.m_ofn.nFilterIndex); + theApp.romFilename = dlg.GetPathName(); + initialDir = winGetDirFromFilename(theApp.romFilename); + + // we have directory override for that purpose + // but this can be...desirable + if (isOverrideEmpty) + regSetStringValue(cartridgeType == 0 ? IDS_ROM_DIR : IDS_GBXROM_DIR, initialDir); + return true; + } + return false; +} + +// some extensions that might commonly be near emulation-related files that we almost certainly can't open, or at least not +// directly. +// also includes definitely non-ROM extensions we know about, since we only use this variable in a ROM opening function. +// we do this by exclusion instead of inclusion because we don't want to exclude extensions used for any archive files, even +// extensionless or unusually-named archives. +static const char *s_romIgnoreExtensions[] = { + "vbm", "sgm", "clt", "dat", "gbs", "gcf", "spc", "xpc", "pal", "act", "dmp", "avi", "ini", "txt", "nfo", + "htm", "html", "jpg", "jpeg", "png", "bmp", "gif", "mp3", "wav", "lnk", "exe", "bat", "sav", "luasav" +}; + +#include "GBACheatsDlg.h" +#include "GBCheatsDlg.h" + +#include "../common/CheatSearch.h" +#include "../gba/GBA.h" +#include "../gb/GB.h" +#include "../gba/Flash.h" +#include "../gba/RTC.h" + +void MainWnd::winFileClose(bool reopening) +{ + if (rom != NULL || gbRom != NULL) + { + if (theApp.autoSaveLoadCheatList) + winSaveCheatListDefault(); + + if (!reopening) + { + // save battery file before we change the filename... + winWriteBatteryFile(); + cheatSearchCleanup(&cheatSearchData); + capturePrevious = 0; + captureNumber = 0; + } + + theApp.emulator.emuCleanUp(); + + extern void remoteCleanUp(); + remoteCleanUp(); + } + + if (VBAMovieActive()) + VBAMovieStop(false); // will only get here on user selecting to open a ROM, canceling movie + + theApp.frameSearching = false; + theApp.frameSearchSkipping = false; + emulating = 0; + + if (this) + RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN); + systemSetTitle(VBA_NAME_AND_VERSION); +} + +bool MainWnd::winFileRun(bool reopening) +{ + int prevCartridgeType = systemCartridgeType; + + //bool requiresInitRAMSearch = (rom == NULL && gbRom == NULL) || !reopening; + winFileClose(reopening); + + // use ObtainFile to support opening files within archives (.7z, .rar, .zip, .zip.rar.7z, etc.) + if (theApp.romFilename.GetLength() > 2048) theApp.romFilename.Truncate(2048); + + char logicalName[2048], physicalName[2048]; + + // FIXME: assertion failure in fopen.c if canceled + if (ObtainFile(theApp.romFilename, logicalName, physicalName, "rom", s_romIgnoreExtensions, + sizeof(s_romIgnoreExtensions) / sizeof(*s_romIgnoreExtensions))) + { + // theApp.romFilename is exactly the filename used for opening, while theApp.gameFilename is always the logical name + theApp.romFilename = theApp.gameFilename = logicalName; + ReleaseTempFileCategory("rom", physicalName); + } + else + { + return false; + } + + IMAGE_TYPE type = utilFindType(physicalName); + + if (type == IMAGE_UNKNOWN) + { + systemMessage(IDS_UNSUPPORTED_FILE_TYPE, + "The file \"%s\" is an unsupported type.", logicalName); + return false; + } + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + systemCartridgeType = (int)type; + if (type == IMAGE_GB) + { + if (!gbLoadRom(physicalName)) + return false; + + gbBorderOn = theApp.winGbBorderOn; + theApp.emulator = GBSystem; + theApp.romSize = gbRomSize; + if (theApp.autoIPS) + { + CString ipsname = winGetDestFilename(logicalName, IDS_IPS_DIR, ".ips"); + int size = gbRomSize; + utilApplyIPS(ipsname, &gbRom, &size); + if (size != gbRomSize) + { + extern bool gbUpdateSizes(); + gbUpdateSizes(); + theApp.romSize = size; + } + } + + useBios = false; // FIXME + + if (reopening) + { + bool winGbCheatReaddress(); + winGbCheatReaddress(); + } + } + else + { + int size = CPULoadRom(physicalName); + if (!size) + return false; + + flashSetSize(theApp.winFlashSize); + rtcEnable(theApp.winRtcEnable); + cpuSaveType = theApp.winSaveType; + + // if(cpuEnhancedDetection && winSaveType == 0) { + // utilGBAFindSave(rom, size); + // } + + char buffer[5]; + strncpy(buffer, (const char *)&rom[0xac], 4); + buffer[4] = 0; + + // vba-over.ini + CString vbaOverINI = theApp.exeDir; + vbaOverINI += "\\vba-over.ini"; + + UINT i = GetPrivateProfileInt(buffer, "rtcEnabled", -1, vbaOverINI); + if (i != (UINT)-1) + rtcEnable(i == 0 ? false : true); + + i = GetPrivateProfileInt(buffer, "flashSize", -1, vbaOverINI); + if (i != (UINT)-1 && (i == 0x10000 || i == 0x20000)) + flashSetSize((int)i); + + i = GetPrivateProfileInt(buffer, "saveType", -1, vbaOverINI); + if (i != (UINT)-1 && (i <= 5)) + cpuSaveType = (int)i; + + /* disabled due to problems + if(theApp.removeIntros && rom != NULL) { + *((u32 *)rom)= 0xea00002e; + } + */ + theApp.emulator = GBASystem; + theApp.romSize = size; + if (theApp.autoIPS) + { + CString ipsname = winGetDestFilename(logicalName, IDS_IPS_DIR, ".ips"); + int size = 0x2000000; + utilApplyIPS(ipsname, &rom, &size); + } + + skipBios = theApp.skipBiosFile ? true : false; + CPUInit(); + CPULoadBios(theApp.biosFileName, theApp.useBiosFile ? true : false); + CPUReset(); + + if (reopening) + { + bool winGbaCheatReaddress(); + winGbaCheatReaddress(); + } + } + + if (theApp.soundInitialized) + { + if (systemCartridgeType == 1) + gbSoundReset(); + else + soundReset(); + } + else + { + if (!soundOffFlag) + soundInit(); + theApp.soundInitialized = true; + } + + winReadBatteryFile(); + + emulating = true; + + if (theApp.autoSaveLoadCheatList) + winLoadCheatListDefault(); + + if (theApp.filenamePreference == 0) + theApp.addRecentFile(winGetOriginalFilename(logicalName)); + else + theApp.addRecentFile(logicalName); + + theApp.updateWindowSize(theApp.videoOption); + + theApp.updateFrameSkip(); + + if (theApp.autoHideMenu && theApp.videoOption > VIDEO_4X && theApp.menuToggle) + OnFileTogglemenu(); + + if (theApp.autoLoadMostRecent && !VBAMovieActive() && !VBAMovieLoading()) // would cause desync in movies... + OnFileLoadgameMostrecent(); + + theApp.renderedFrames = 0; + + theApp.rewindCount = 0; + theApp.rewindCounter = 0; + theApp.rewindSaveNeeded = false; + + { + extern bool playMovieFile, playMovieFileReadOnly, outputWavFile, outputAVIFile, flagHideMenu; // from VBA.cpp + extern char movieFileToPlay[1024], wavFileToOutput[1024]; // from VBA.cpp + extern int pauseAfterTime; // from VBA.cpp + if (playMovieFile) + { + playMovieFile = false; + VBAMovieOpen(movieFileToPlay, playMovieFileReadOnly); + } + if (outputWavFile) + { + outputWavFile = false; + theApp.soundRecordName = wavFileToOutput; + theApp.soundRecording = true; + } + if (outputAVIFile) + { + outputAVIFile = false; + OnToolsStartAVIRecording(); + } + if (pauseAfterTime >= 0) + { + VBAMovieSetPauseAt(pauseAfterTime); + } + if (flagHideMenu) + { + OnFileTogglemenu(); + theApp.updateWindowSize(theApp.videoOption); + } + } + + if (systemCartridgeType != prevCartridgeType) + { + extern GBACheatSearch gbaDlg; + extern GBCheatSearch gbDlg; + if (!theApp.pauseDuringCheatSearch && theApp.modelessCheatDialogIsOpen) + { + gbaDlg.DestroyWindow(); + gbDlg.DestroyWindow(); + theApp.modelessCheatDialogIsOpen = false; + } + } + + theApp.winCheckFullscreen(); + ReopenRamWindows(); + + // FIXME + reset_address_info(); + //if (requiresInitRAMSearch) + { + // extern void soft_reset_address_info(); + // soft_reset_address_info(); + } + + VBAUpdateButtonPressDisplay(); + VBAUpdateFrameCountDisplay(); + systemRefreshScreen(); + + return true; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MainWnd.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MainWnd.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,541 @@ +#if !defined(AFX_MAINWND_H__E8AD28B9_C9FB_4EC2_A2DC_DD1BBA55A275__INCLUDED_) +#define AFX_MAINWND_H__E8AD28B9_C9FB_4EC2_A2DC_DD1BBA55A275__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// MainWnd.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// MainWnd window + +class MainWnd : public CWnd +{ + // Constructor/Destructor +public: + MainWnd(); + virtual ~MainWnd(); + + // Attributes +private: + HCURSOR arrow; + HACCEL m_hAccelTable; + + // Implementation +private: + + // Operations +public: + afx_msg void OnOptionsEmulatorSelectbiosfile(); + void winFileClose(bool reopening = false); + bool winFileRun(bool reopening = false); + bool winFileOpenSelect(int cartridgeType); + void winMouseOn(); + void winConfirmMode(); + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MainWnd) +public: + virtual BOOL PreTranslateMessage(MSG*pMsg); + //}}AFX_VIRTUAL + + // Generated message map functions +protected: + //{{AFX_MSG(MainWnd) + afx_msg LRESULT OnConfirmMode(WPARAM, LPARAM); + afx_msg LRESULT OnMySysCommand(WPARAM, LPARAM); + + afx_msg void OnMove(int x, int y); + afx_msg void OnContextMenu(CWnd*pWnd, CPoint point); + afx_msg void OnSize(UINT nType, int cx, int cy); + afx_msg void OnClose(); + afx_msg void OnInitMenuPopup(CMenu*pPopupMenu, UINT nIndex, BOOL bSysMenu); + afx_msg void OnMouseMove(UINT nFlags, CPoint point); + afx_msg void OnInitMenu(CMenu*pMenu); + afx_msg void OnPaint(); + afx_msg void OnDropFiles(HDROP hDropInfo); + afx_msg void OnActivate(UINT nState, CWnd*pWndOther, BOOL bMinimized); +#if _MSC_VER <= 1200 + afx_msg void OnActivateApp(BOOL bActive, HTASK hTask); +#else + afx_msg void OnActivateApp(BOOL bActive, DWORD hTask); +#endif + + afx_msg void OnUpdateOptionsJoypadAutofire(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsJoypadAutofire(UINT nID); + afx_msg void OnUpdateOptionsJoypadSticky(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsJoypadSticky(UINT nID); + afx_msg void OnUpdateOptionsJoypadDefault(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsJoypadDefault(UINT nID); + afx_msg void OnUpdateOptionsFilterIFB(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsFilterIFB(UINT nID); + afx_msg void OnUpdateOptionsFilter(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsFilter(UINT nID); + afx_msg void OnUpdateOptionsPriority(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsPriority(UINT nID); + afx_msg void OnUpdateOptionsSoundVolume(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsSoundVolume(UINT nID); + afx_msg void OnUpdateOptionsEmulatorShowSpeed(CCmdUI *pCmdUI); + afx_msg BOOL OnOptionsEmulatorShowSpeed(UINT nID); + afx_msg void OnUpdateVideoLayer(CCmdUI*pCmdUI); + afx_msg BOOL OnVideoLayer(UINT nID); + afx_msg BOOL OnOptionVideoSize(UINT nID); + afx_msg BOOL OnOptionsFrameskip(UINT nID); + afx_msg void OnFileTogglemenu(); + + afx_msg void OnSystemMinimize(); + afx_msg void OnSystemMaximize(); + + afx_msg void OnHelpAbout(); + afx_msg void OnHelpFaq(); + afx_msg void OnFileOpen(); + afx_msg void OnFileOpenGBx(); + afx_msg void OnFilePause(); + afx_msg void OnUpdateFilePause(CCmdUI*pCmdUI); + afx_msg void OnFileReset(); + afx_msg void OnUpdateFileReset(CCmdUI*pCmdUI); + afx_msg void OnUpdateFileRecentFreeze(CCmdUI*pCmdUI); + afx_msg void OnFileRecentReset(); + afx_msg void OnFileRecentFreeze(); + afx_msg void OnFileExit(); + afx_msg void OnFileClose(); + afx_msg void OnUpdateFileClose(CCmdUI*pCmdUI); + afx_msg void OnFileLoad(); + afx_msg void OnUpdateFileLoad(CCmdUI*pCmdUI); + afx_msg void OnFileSave(); + afx_msg void OnUpdateFileSave(CCmdUI*pCmdUI); + afx_msg void OnFileImportBatteryfile(); + afx_msg void OnUpdateFileImportBatteryfile(CCmdUI*pCmdUI); + afx_msg void OnFileImportGamesharkcodefile(); + afx_msg void OnUpdateFileImportGamesharkcodefile(CCmdUI*pCmdUI); + afx_msg void OnFileImportGamesharksnapshot(); + afx_msg void OnUpdateFileImportGamesharksnapshot(CCmdUI*pCmdUI); + afx_msg void OnFileExportBatteryfile(); + afx_msg void OnUpdateFileExportBatteryfile(CCmdUI*pCmdUI); + afx_msg void OnFileExportGamesharksnapshot(); + afx_msg void OnUpdateFileExportGamesharksnapshot(CCmdUI*pCmdUI); + afx_msg void OnFileQuickScreencapture(); + afx_msg void OnFileScreencapture(); + afx_msg void OnUpdateFileScreencapture(CCmdUI*pCmdUI); + afx_msg void OnFileRominformation(); + afx_msg void OnUpdateFileRominformation(CCmdUI*pCmdUI); + afx_msg void OnUpdateFileTogglemenu(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottleNothrottle(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle6(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle15(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle25(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle50(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle75(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle100(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle125(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle150(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle200(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle300(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle400(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle600(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottle1000(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottleOther(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottleIncrease(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipThrottleDecrease(CCmdUI*pCmdUI); + afx_msg void OnOptionsFrameskipThrottleNothrottle(); + afx_msg void OnOptionsFrameskipThrottle6(); + afx_msg void OnOptionsFrameskipThrottle15(); + afx_msg void OnOptionsFrameskipThrottle25(); + afx_msg void OnOptionsFrameskipThrottle50(); + afx_msg void OnOptionsFrameskipThrottle75(); + afx_msg void OnOptionsFrameskipThrottle100(); + afx_msg void OnOptionsFrameskipThrottle125(); + afx_msg void OnOptionsFrameskipThrottle150(); + afx_msg void OnOptionsFrameskipThrottle200(); + afx_msg void OnOptionsFrameskipThrottle300(); + afx_msg void OnOptionsFrameskipThrottle400(); + afx_msg void OnOptionsFrameskipThrottle600(); + afx_msg void OnOptionsFrameskipThrottle1000(); + afx_msg void OnOptionsFrameskipThrottleOther(); + afx_msg void OnOptionsFrameskipThrottleIncrease(); + afx_msg void OnOptionsFrameskipThrottleDecrease(); + afx_msg void OnOptionsFrameskipAccuratePitch(); + afx_msg void OnOptionsFrameskipAccurateSpeed(); + afx_msg void OnUpdateOptionsFrameskipAccuratePitch(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsFrameskipAccurateSpeed(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip0(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip1(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip2(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip3(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip4(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip5(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip6(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip7(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip8(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFrameskip9(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoVsync(); + afx_msg void OnUpdateOptionsVideoVsync(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoX1(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoX2(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoX3(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoX4(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFullscreen320x240(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFullscreen640x480(CCmdUI*pCmdUI); + afx_msg void OnUpdateOptionsVideoFullscreen800x600(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoFullscreen320x240(); + afx_msg void OnOptionsVideoFullscreen640x480(); + afx_msg void OnOptionsVideoFullscreen800x600(); + afx_msg void OnOptionsVideoFullscreen(); + afx_msg void OnUpdateOptionsVideoFullscreen(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoDisablesfx(); + afx_msg void OnUpdateOptionsVideoDisablesfx(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoFullscreenstretchtofit(); + afx_msg void OnUpdateOptionsVideoFullscreenstretchtofit(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRendermethodGdi(); + afx_msg void OnUpdateOptionsVideoRendermethodGdi(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRendermethodDirectdraw(); + afx_msg void OnUpdateOptionsVideoRendermethodDirectdraw(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRendermethodDirect3d(); + afx_msg void OnUpdateOptionsVideoRendermethodDirect3d(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRendermethodOpengl(); + afx_msg void OnUpdateOptionsVideoRendermethodOpengl(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoTriplebuffering(); + afx_msg void OnUpdateOptionsVideoTriplebuffering(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoDdrawemulationonly(); + afx_msg void OnUpdateOptionsVideoDdrawemulationonly(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoDdrawusevideomemory(); + afx_msg void OnUpdateOptionsVideoDdrawusevideomemory(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsD3dnofilter(); + afx_msg void OnUpdateOptionsVideoRenderoptionsD3dnofilter(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsD3dbilinear(); + afx_msg void OnUpdateOptionsVideoRenderoptionsD3dbilinear(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsGlnearest(); + afx_msg void OnUpdateOptionsVideoRenderoptionsGlnearest(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsGlbilinear(); + afx_msg void OnUpdateOptionsVideoRenderoptionsGlbilinear(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsGltriangle(); + afx_msg void OnUpdateOptionsVideoRenderoptionsGltriangle(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsGlquads(); + afx_msg void OnUpdateOptionsVideoRenderoptionsGlquads(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsSelectskin(); + afx_msg void OnUpdateOptionsVideoRenderoptionsSelectskin(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoRenderoptionsSkin(); + afx_msg void OnUpdateOptionsVideoRenderoptionsSkin(CCmdUI*pCmdUI); + + afx_msg void OnOptionsEmulatorAssociate(); + afx_msg void OnOptionsEmulatorDirectories(); + afx_msg void OnOptionsEmulatorFilenamePreference(UINT nID); + afx_msg void OnUpdateOptionsEmulatorFilenamePreference(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoDisablestatusmessages(); + afx_msg void OnUpdateOptionsVideoDisablestatusmessages(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSynchronize(); + afx_msg void OnUpdateOptionsEmulatorSynchronize(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorAlwaysOnTop(); + afx_msg void OnUpdateOptionsEmulatorAlwaysOnTop(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorPausewheninactive(); + afx_msg void OnUpdateOptionsEmulatorPausewheninactive(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorEnableBackgroundInput(); + afx_msg void OnUpdateOptionsEmulatorEnableBackgroundInput(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSpeeduptoggle(); + afx_msg void OnUpdateOptionsEmulatorSpeeduptoggle(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorRemoveintrosgba(); + afx_msg void OnUpdateOptionsEmulatorRemoveintrosgba(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorAutomaticallyipspatch(); + afx_msg void OnUpdateOptionsEmulatorAutomaticallyipspatch(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorAgbprint(); + afx_msg void OnUpdateOptionsEmulatorAgbprint(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorRealtimeclock(); + afx_msg void OnUpdateOptionsEmulatorRealtimeclock(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorAutohidemenu(); + afx_msg void OnUpdateOptionsEmulatorAutohidemenu(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorRewindinterval(); + afx_msg void OnOptionsEmulatorSavetypeAutomatic(); + afx_msg void OnUpdateOptionsEmulatorSavetypeAutomatic(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeEeprom(); + afx_msg void OnUpdateOptionsEmulatorSavetypeEeprom(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeSram(); + afx_msg void OnUpdateOptionsEmulatorSavetypeSram(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeFlash(); + afx_msg void OnUpdateOptionsEmulatorSavetypeFlash(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeEepromsensor(); + afx_msg void OnUpdateOptionsEmulatorSavetypeEepromsensor(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeNone(); + afx_msg void OnUpdateOptionsEmulatorSavetypeNone(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeFlash512k(); + afx_msg void OnUpdateOptionsEmulatorSavetypeFlash512k(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorSavetypeFlash1m(); + afx_msg void OnUpdateOptionsEmulatorSavetypeFlash1m(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorUseOldGBTiming(); + afx_msg void OnUpdateOptionsEmulatorUseOldGBTiming(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorUseGBNullInputKludge(); + afx_msg void OnUpdateOptionsEmulatorUseGBNullInputKludge(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorGBALag(); + afx_msg void OnUpdateOptionsEmulatorGBALag(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorUsebiosfile(); + afx_msg void OnUpdateOptionsEmulatorUsebiosfile(CCmdUI*pCmdUI); + + afx_msg void OnOptionsEmulatorSkipbios(); + afx_msg void OnUpdateOptionsEmulatorSkipbios(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorPngformat(); + afx_msg void OnUpdateOptionsEmulatorPngformat(CCmdUI*pCmdUI); + afx_msg void OnOptionsEmulatorBmpformat(); + afx_msg void OnUpdateOptionsEmulatorBmpformat(CCmdUI*pCmdUI); + + afx_msg void OnOptionsSoundDisable(); + afx_msg void OnUpdateOptionsSoundDisable(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundMute(); + afx_msg void OnUpdateOptionsSoundMute(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundOff(); + afx_msg void OnUpdateOptionsSoundOff(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundOn(); + afx_msg void OnUpdateOptionsSoundOn(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundUseoldsynchronization(); + afx_msg void OnUpdateOptionsSoundUseoldsynchronization(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundEcho(); + afx_msg void OnUpdateOptionsSoundEcho(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundLowpassfilter(); + afx_msg void OnUpdateOptionsSoundLowpassfilter(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundReversestereo(); + afx_msg void OnUpdateOptionsSoundReversestereo(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundMuteFrameAdvance(); + afx_msg void OnUpdateOptionsSoundMuteFrameAdvance(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundMuteWhenInactive(); + afx_msg void OnUpdateOptionsSoundMuteWhenInactive(CCmdUI*pCmdUI); + afx_msg void OnOptionsSound11khz(); + afx_msg void OnUpdateOptionsSound11khz(CCmdUI*pCmdUI); + afx_msg void OnOptionsSound22khz(); + afx_msg void OnUpdateOptionsSound22khz(CCmdUI*pCmdUI); + afx_msg void OnOptionsSound44khz(); + afx_msg void OnUpdateOptionsSound44khz(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundChannel1(); + afx_msg void OnUpdateOptionsSoundChannel1(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundChannel2(); + afx_msg void OnUpdateOptionsSoundChannel2(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundChannel3(); + afx_msg void OnUpdateOptionsSoundChannel3(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundChannel4(); + afx_msg void OnUpdateOptionsSoundChannel4(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundDirectsounda(); + afx_msg void OnUpdateOptionsSoundDirectsounda(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundDirectsoundb(); + afx_msg void OnUpdateOptionsSoundDirectsoundb(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyBorder(); + afx_msg void OnUpdateOptionsGameboyBorder(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyPrinter(); + afx_msg void OnUpdateOptionsGameboyPrinter(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyBorderAutomatic(); + afx_msg void OnUpdateOptionsGameboyBorderAutomatic(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyAutomatic(); + afx_msg void OnUpdateOptionsGameboyAutomatic(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyGba(); + afx_msg void OnUpdateOptionsGameboyGba(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyCgb(); + afx_msg void OnUpdateOptionsGameboyCgb(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboySgb(); + afx_msg void OnUpdateOptionsGameboySgb(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboySgb2(); + afx_msg void OnUpdateOptionsGameboySgb2(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyGb(); + afx_msg void OnUpdateOptionsGameboyGb(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyRealcolors(); + afx_msg void OnUpdateOptionsGameboyRealcolors(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyGameboycolors(); + afx_msg void OnUpdateOptionsGameboyGameboycolors(CCmdUI*pCmdUI); + afx_msg void OnOptionsGameboyColors(); + afx_msg void OnOptionsFilterDisablemmx(); + afx_msg void OnUpdateOptionsFilterDisablemmx(CCmdUI*pCmdUI); + afx_msg void OnOptionsLanguageSystem(); + afx_msg void OnUpdateOptionsLanguageSystem(CCmdUI*pCmdUI); + afx_msg void OnOptionsLanguageEnglish(); + afx_msg void OnUpdateOptionsLanguageEnglish(CCmdUI*pCmdUI); + afx_msg void OnOptionsLanguageOther(); + afx_msg void OnUpdateOptionsLanguageOther(CCmdUI*pCmdUI); + afx_msg void OnOptionsJoypadConfigure1(); + afx_msg void OnUpdateOptionsJoypadConfigure1(CCmdUI*pCmdUI); + afx_msg void OnOptionsJoypadConfigure2(); + afx_msg void OnUpdateOptionsJoypadConfigure2(CCmdUI*pCmdUI); + afx_msg void OnOptionsJoypadConfigure3(); + afx_msg void OnUpdateOptionsJoypadConfigure3(CCmdUI*pCmdUI); + afx_msg void OnOptionsJoypadConfigure4(); + afx_msg void OnUpdateOptionsJoypadConfigure4(CCmdUI*pCmdUI); + afx_msg void OnOptionsJoypadMotionconfigure(); + afx_msg void OnUpdateOptionsJoypadMotionconfigure(CCmdUI*pCmdUI); + afx_msg void OnOptionsJoypadAllowLeftRight(); + afx_msg void OnUpdateOptionsJoypadAllowLeftRight(CCmdUI*pCmdUI); + afx_msg void OnOptionsJoypadAutofireAccountForLag(); + afx_msg void OnUpdateOptionsJoypadAutofireAccountForLag(CCmdUI*pCmdUI); + afx_msg void OnCheatsSearchforcheats(); + afx_msg void OnUpdateCheatsSearchforcheats(CCmdUI*pCmdUI); + afx_msg void OnCheatsCheatlist(); + afx_msg void OnUpdateCheatsCheatlist(CCmdUI*pCmdUI); + afx_msg void OnCheatsAutomaticsaveloadcheats(); + afx_msg void OnCheatsPauseDuringCheatSearch(); + afx_msg void OnCheatsLoadcheatlist(); + afx_msg void OnUpdateCheatsLoadcheatlist(CCmdUI*pCmdUI); + afx_msg void OnCheatsSavecheatlist(); + afx_msg void OnUpdateCheatsSavecheatlist(CCmdUI*pCmdUI); + afx_msg void OnToolsDisassemble(); + afx_msg void OnUpdateToolsDisassemble(CCmdUI*pCmdUI); + afx_msg void OnToolsLogging(); + afx_msg void OnUpdateToolsLogging(CCmdUI*pCmdUI); + afx_msg void OnToolsIoviewer(); + afx_msg void OnUpdateToolsIoviewer(CCmdUI*pCmdUI); + afx_msg void OnToolsMapview(); + afx_msg void OnUpdateToolsMapview(CCmdUI*pCmdUI); + afx_msg void OnToolsMemoryviewer(); + afx_msg void OnUpdateToolsMemoryviewer(CCmdUI*pCmdUI); + afx_msg void OnToolsOamviewer(); + afx_msg void OnUpdateToolsOamviewer(CCmdUI*pCmdUI); + afx_msg void OnToolsPaletteview(); + afx_msg void OnUpdateToolsPaletteview(CCmdUI*pCmdUI); + afx_msg void OnToolsTileviewer(); + afx_msg void OnUpdateToolsTileviewer(CCmdUI*pCmdUI); + afx_msg void OnDebugNextframe(); + afx_msg void OnUpdateDebugNextframe(CCmdUI*pCmdUI); + afx_msg void OnDebugNextframeAccountForLag(); + afx_msg void OnUpdateDebugNextframeAccountForLag(CCmdUI*pCmdUI); + afx_msg void OnDebugFramesearch(); + afx_msg void OnUpdateDebugFramesearch(CCmdUI*pCmdUI); + afx_msg void OnDebugFramesearchPrev(); + afx_msg void OnUpdateDebugFramesearchPrev(CCmdUI*pCmdUI); + afx_msg void OnDebugFramesearchLoad(); + afx_msg void OnUpdateDebugFramesearchLoad(CCmdUI*pCmdUI); + afx_msg void OnUpdateCheatsAutomaticsaveloadcheats(CCmdUI*pCmdUI); + afx_msg void OnUpdateCheatsPauseDuringCheatSearch(CCmdUI*pCmdUI); + afx_msg void OnToolsFrameCounter(); + afx_msg void OnUpdateToolsFrameCounter(CCmdUI*pCmdUI); + afx_msg void OnToolsLagCounter(); + afx_msg void OnUpdateToolsLagCounter(CCmdUI*pCmdUI); + afx_msg void OnToolsExtraCounter(); + afx_msg void OnUpdateToolsExtraCounter(CCmdUI*pCmdUI); + afx_msg void OnToolsExtraCounterReset(); + afx_msg void OnToolsInputDisplay(); + afx_msg void OnUpdateToolsInputDisplay(CCmdUI*pCmdUI); + afx_msg void OnToolsDebugGdb(); + afx_msg void OnUpdateToolsDebugGdb(CCmdUI*pCmdUI); + afx_msg void OnToolsDebugLoadandwait(); + afx_msg void OnUpdateToolsDebugLoadandwait(CCmdUI*pCmdUI); + afx_msg void OnToolsDebugBreak(); + afx_msg void OnUpdateToolsDebugBreak(CCmdUI*pCmdUI); + afx_msg void OnToolsDebugDisconnect(); + afx_msg void OnUpdateToolsDebugDisconnect(CCmdUI*pCmdUI); + afx_msg void OnToolsSoundStartrecording(); + afx_msg void OnToolsSoundStoprecording(); + afx_msg void OnToolsSoundRecording(); + afx_msg void OnUpdateToolsSoundRecording(CCmdUI*pCmdUI); + afx_msg void OnToolsStartAVIRecording(); + afx_msg void OnToolsPauseAVIRecording(); + afx_msg void OnToolsStopAVIRecording(); + afx_msg void OnToolsAVIRecording(); + afx_msg void OnUpdateToolsAVIRecording(CCmdUI*pCmdUI); + afx_msg void OnUpdateToolsPauseAVIRecording(CCmdUI*pCmdUI); + + afx_msg void OnToolsRecordMovie(); + afx_msg void OnUpdateToolsRecordMovie(CCmdUI*pCmdUI); + afx_msg void OnToolsStopMovie(); + afx_msg void OnUpdateToolsStopMovie(CCmdUI*pCmdUI); + afx_msg void OnToolsPlayMovie(); + afx_msg void OnUpdateToolsPlayMovie(CCmdUI*pCmdUI); + afx_msg void OnToolsPlayReadOnly(); + afx_msg void OnUpdateToolsPlayReadOnly(CCmdUI*pCmdUI); + afx_msg void OnAsscWithSaveState(); + afx_msg void OnUpdateAsscWithSaveState(CCmdUI*pCmdUI); + afx_msg void OnToolsResumeRecord(); + afx_msg void OnUpdateToolsResumeRecord(CCmdUI*pCmdUI); + afx_msg void OnToolsPlayRestart(); + afx_msg void OnUpdateToolsPlayRestart(CCmdUI*pCmdUI); + + afx_msg void OnToolsOnMovieEndPause(); + afx_msg void OnUpdateToolsOnMovieEndPause(CCmdUI*pCmdUI); + afx_msg void OnToolsOnMovieEndStop(); + afx_msg void OnUpdateToolsOnMovieEndStop(CCmdUI*pCmdUI); + afx_msg void OnToolsOnMovieEndRestart(); + afx_msg void OnUpdateToolsOnMovieEndRestart(CCmdUI*pCmdUI); + afx_msg void OnToolsOnMovieEndAppend(); + afx_msg void OnUpdateToolsOnMovieEndAppend(CCmdUI*pCmdUI); + afx_msg void OnToolsOnMovieEndKeep(); + afx_msg void OnUpdateToolsOnMovieEndKeep(CCmdUI*pCmdUI); + + afx_msg void OnToolsMovieSetPauseAt(); + afx_msg void OnUpdateToolsSetMoviePauseAt(CCmdUI*pCmdUI); + afx_msg void OnToolsMovieConvertCurrent(); + afx_msg void OnUpdateToolsMovieConvertCurrent(CCmdUI*pCmdUI); + afx_msg void OnToolsMovieAutoConvert(); + afx_msg void OnUpdateToolsMovieAutoConvert(CCmdUI*pCmdUI); + afx_msg void OnToolsMovieFixHeader(); + afx_msg void OnUpdateToolsMovieFixHeader(CCmdUI*pCmdUI); + afx_msg void OnToolsMovieTruncateAtCurrent(); + afx_msg void OnUpdateToolsMovieTruncateAtCurrent(CCmdUI*pCmdUI); + afx_msg void OnToolsMovieExtractFromSavegame(); + afx_msg void OnUpdateToolsMovieExtractFromSavegame(CCmdUI*pCmdUI); + + afx_msg void OnToolsRewind(); + afx_msg void OnUpdateToolsRewind(CCmdUI*pCmdUI); + afx_msg void OnToolsCustomize(); + afx_msg void OnUpdateToolsCustomize(CCmdUI*pCmdUI); +// afx_msg void OnToolsCustomizeCommon(); +// afx_msg void OnUpdateToolsCustomizeCommon(CCmdUI*pCmdUI); + afx_msg void OnToolsCopyVBAWatchSetting(); + afx_msg void OnToolsCopyVBxWatchSetting(); + afx_msg void OnHelpBugreport(); + + afx_msg void OnFileSavegameOldestslot(); + afx_msg void OnUpdateFileSavegameOldestslot(CCmdUI*pCmdUI); + afx_msg void OnFileLoadgameMostrecent(); + afx_msg void OnUpdateFileLoadgameMostrecent(CCmdUI*pCmdUI); + afx_msg void OnFileLoadgameAutoloadmostrecent(); + afx_msg void OnUpdateFileLoadgameAutoloadmostrecent(CCmdUI*pCmdUI); + afx_msg void OnFileLoadgameMakeRecent(); + afx_msg void OnUpdateFileLoadgameMakeRecent(CCmdUI*pCmdUI); + + afx_msg void OnFileSavegameCurrent(); + afx_msg void OnUpdateFileSavegameCurrent(CCmdUI*pCmdUI); + afx_msg void OnFileLoadgameCurrent(); + afx_msg void OnUpdateFileLoadgameCurrent(CCmdUI*pCmdUI); + afx_msg void OnFileLoadgameMakeCurrent(); + afx_msg void OnUpdateFileLoadgameMakeCurrent(CCmdUI*pCmdUI); + afx_msg void OnFileSavegameMakeCurrent(); + afx_msg void OnUpdateFileSavegameMakeCurrent(CCmdUI*pCmdUI); + afx_msg void OnFileSavegameIncrementSlot(); + afx_msg void OnUpdateFileSavegameIncrementSlot(CCmdUI*pCmdUI); + afx_msg void OnFileSavegameDecrementSlot(); + afx_msg void OnUpdateFileSavegameDecrementSlot(CCmdUI*pCmdUI); + afx_msg void OnFileSlotDisplayModificationTime(); + afx_msg void OnUpdateFileSlotDisplayModificationTime(CCmdUI*pCmdUI); + + afx_msg void OnOptionsSoundVolume25x(); + afx_msg void OnUpdateOptionsSoundVolume25x(CCmdUI*pCmdUI); + afx_msg void OnOptionsSoundVolume5x(); + afx_msg void OnUpdateOptionsSoundVolume5x(CCmdUI*pCmdUI); + afx_msg void OnCheatsDisablecheats(); + afx_msg void OnUpdateCheatsDisablecheats(CCmdUI*pCmdUI); + afx_msg void OnOptionsVideoFullscreenmaxscale(); + afx_msg void OnOptionsVideoTextdisplayoptions(); + afx_msg void OnUpdateOptionsVideoTextdisplayoptions(CCmdUI *pCmdUI); + + afx_msg void OnUpdateFileRecentFile(CCmdUI *pCmdUI); + afx_msg void OnUpdateFileLoadSlot(CCmdUI *pCmdUI); + afx_msg void OnUpdateFileSaveSlot(CCmdUI *pCmdUI); + afx_msg void OnUpdateSelectSlot(CCmdUI *pCmdUI); + afx_msg BOOL OnFileRecentFile(UINT nID); + afx_msg BOOL OnFileLoadSlot(UINT nID); + afx_msg BOOL OnFileSaveSlot(UINT nID); + afx_msg BOOL OnSelectSlot(UINT nID); + + afx_msg void OnFileLuaOpen(); + afx_msg void OnUpdateFileLuaOpen(CCmdUI *pCmdUI); + afx_msg void OnFileLuaCloseAll(); + afx_msg void OnUpdateFileLuaCloseAll(CCmdUI *pCmdUI); + afx_msg void OnFileLuaReload(); + afx_msg void OnFileLuaStop(); + afx_msg void OnFileRamSearch(); + afx_msg void OnUpdateFileRamSearch(CCmdUI *pCmdUI); + afx_msg void OnFileRamWatch(); + afx_msg void OnUpdateFileRamWatch(CCmdUI *pCmdUI); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAINWND_H__E8AD28B9_C9FB_4EC2_A2DC_DD1BBA55A275__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MainWndCheats.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MainWndCheats.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,164 @@ +#include "stdafx.h" +#include "resource.h" +#include "MainWnd.h" +#include "FileDlg.h" +#include "GBACheatsDlg.h" +#include "GBCheatsDlg.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "WinMiscUtil.h" +#include "VBA.h" + +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gb/gbCheats.h" + +GBACheatSearch gbaDlg; +GBCheatSearch gbDlg; + +void MainWnd::OnCheatsSearchforcheats() +{ + theApp.winCheckFullscreen(); + + if (theApp.modelessCheatDialogIsOpen) + { + gbaDlg.DestroyWindow(); + gbDlg.DestroyWindow(); + theApp.modelessCheatDialogIsOpen = false; + } + + if (systemCartridgeType == 0) + { + if (theApp.pauseDuringCheatSearch) + { + gbaDlg.DoModal(); + } + else + { + if (!theApp.modelessCheatDialogIsOpen) + { + theApp.modelessCheatDialogIsOpen = true; + gbaDlg.Create(GBACheatSearch::IDD, theApp.m_pMainWnd); + } + } + } + else + { + if (theApp.pauseDuringCheatSearch) + { + gbDlg.DoModal(); + } + else + { + if (!theApp.modelessCheatDialogIsOpen) + { + theApp.modelessCheatDialogIsOpen = true; + gbDlg.Create(GBCheatSearch::IDD, theApp.m_pMainWnd); + } + } + } +} + +void MainWnd::OnUpdateCheatsSearchforcheats(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnCheatsCheatlist() +{ + theApp.winCheckFullscreen(); + if (systemCartridgeType == 0) + { + GBACheatList dlg; + dlg.DoModal(); + } + else + { + GBCheatList dlg; + dlg.DoModal(); + } +} + +void MainWnd::OnUpdateCheatsCheatlist(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnCheatsAutomaticsaveloadcheats() +{ + theApp.autoSaveLoadCheatList = !theApp.autoSaveLoadCheatList; +} + +void MainWnd::OnUpdateCheatsAutomaticsaveloadcheats(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoSaveLoadCheatList); +} + +void MainWnd::OnCheatsPauseDuringCheatSearch() +{ + theApp.pauseDuringCheatSearch = !theApp.pauseDuringCheatSearch; +} + +void MainWnd::OnUpdateCheatsPauseDuringCheatSearch(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.pauseDuringCheatSearch); +} + +void MainWnd::OnCheatsLoadcheatlist() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { ".clt", NULL }; + CString filter = winResLoadFilter(IDS_FILTER_CHEAT_LIST); + CString title = winResLoadString(IDS_SELECT_CHEAT_LIST_NAME); + + CString cheatName = winGetDestFilename(theApp.gameFilename, IDS_CHEAT_DIR, exts[0]); + CString cheatDir = winGetDestDir(IDS_CHEAT_DIR); + + FileDlg dlg(this, cheatName, filter, 0, "CLT", exts, cheatDir, title, false); + + if (dlg.DoModal() == IDOK) + { + winLoadCheatList(dlg.GetPathName()); + } +} + +void MainWnd::OnUpdateCheatsLoadcheatlist(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnCheatsSavecheatlist() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { ".clt", NULL }; + CString filter = winResLoadFilter(IDS_FILTER_CHEAT_LIST); + CString title = winResLoadString(IDS_SELECT_CHEAT_LIST_NAME); + + CString cheatName = winGetDestFilename(theApp.gameFilename, IDS_CHEAT_DIR, exts[0]); + CString cheatDir = winGetDestDir(IDS_CHEAT_DIR); + + FileDlg dlg(this, cheatName, filter, 0, "CLT", exts, cheatDir, title, true); + + if (dlg.DoModal() == IDOK) + { + winSaveCheatList(dlg.GetPathName()); + } +} + +void MainWnd::OnUpdateCheatsSavecheatlist(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnCheatsDisablecheats() +{ + cheatsEnabled = !cheatsEnabled; +} + +void MainWnd::OnUpdateCheatsDisablecheats(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(!cheatsEnabled); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MainWndFile.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MainWndFile.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1004 @@ +#include "stdafx.h" +#include "resource.h" +#include "MainWnd.h" +#include "ExportGSASnapshot.h" +#include "FileDlg.h" +#include "GSACodeSelect.h" +#include "RomInfo.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "WinMiscUtil.h" +#include "LuaOpenDialog.h" +#include "ram_search.h" +#include "ramwatch.h" +#include "Sound.h" +#include "VBA.h" + +#include "../NLS.h" +#include "../version.h" +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gba/EEprom.h" +#include "../gba/GBASound.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +#include "../common/movie.h" +#include "../common/vbalua.h" + +void MainWnd::OnFileOpen() +{ + theApp.winCheckFullscreen(); + if (winFileOpenSelect(0)) + { + winFileRun(); + } +} + +void MainWnd::OnFileOpenGBx() +{ + theApp.winCheckFullscreen(); + if (winFileOpenSelect(1)) + { + winFileRun(); + } +} + +void MainWnd::OnFilePause() +{ + systemSetPause(!theApp.paused); +} + +void MainWnd::OnUpdateFilePause(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.paused); +} + +void MainWnd::OnFileReset() +{ + if (emulating) + { + if (VBAMovieGetState() == MOVIE_STATE_PLAY) + { + OnToolsPlayRestart(); // HACK: shortcut + } + else + { + theApp.emulator.emuReset(true); + systemScreenMessage(winResLoadString(IDS_RESET)); + } + } +} + +void MainWnd::OnUpdateFileReset(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileRecentFreeze() +{ + theApp.recentFreeze = !theApp.recentFreeze; +} + +void MainWnd::OnUpdateFileRecentFreeze(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.recentFreeze); +} + +void MainWnd::OnFileRecentReset() +{ + theApp.winCheckFullscreen(); + systemSoundClearBuffer(); + if (MessageBox("Really clear your recent ROMs list?", //winResLoadString(IDS_REALLY_CLEAR), + winResLoadString(IDS_CONFIRM_ACTION), + MB_YESNO | MB_DEFBUTTON2) == IDNO) + return; + + for (int i = 0; i < 10; ++i) + theApp.recentFiles[i] = ""; +} + +BOOL MainWnd::OnFileRecentFile(UINT nID) +{ + if (theApp.recentFiles[(nID & 0xFFFF) - ID_FILE_MRU_FILE1].GetLength()) + { + theApp.romFilename = theApp.recentFiles[(nID & 0xFFFF) - ID_FILE_MRU_FILE1]; + winFileRun(); + } + return TRUE; +} + +void MainWnd::OnUpdateFileRecentFile(CCmdUI *pCmdUI) +{ + int fileID = pCmdUI->m_nID - ID_FILE_MRU_FILE1; + + bool bExist = !theApp.recentFiles[fileID].IsEmpty(); + + if (pCmdUI->m_pMenu != NULL) + { + CString p = theApp.recentFiles[fileID]; + + int index = max(p.ReverseFind('/'), max(p.ReverseFind('\\'), p.ReverseFind('|'))); + + if (index != -1) + { + p.Delete(0, index + 1); + } + + p.Replace("&", "&&"); + + CString number("1&0 - "); + if (fileID < 9) + number.Format("&%d - ", fileID + 1); + + if (p.IsEmpty()) + { + p = "No Recent ROM"; + bExist = false; + } + + pCmdUI->SetText(number + p); + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + pCmdUI->Enable(bExist); +} + +void MainWnd::OnFileExit() +{ + if (AskSave()) + SendMessage(WM_CLOSE); +} + +void MainWnd::OnFileClose() +{ + // save battery file before we change the filename... + CloseRamWindows(); + winFileClose(); +} + +void MainWnd::OnUpdateFileClose(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileLoad() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { ".sgm", NULL }; + CString filter = winResLoadFilter(IDS_FILTER_SGM); + CString title = winResLoadString(IDS_SELECT_SAVE_GAME_NAME); + + CString saveName = winGetDestFilename(theApp.gameFilename, IDS_SAVE_DIR, exts[0]); + CString saveDir = winGetDestDir(IDS_SAVE_DIR); + + FileDlg dlg(this, saveName, filter, 0, "SGM", exts, saveDir, title, false, true); + + if (dlg.DoModal() == IDOK) + { + bool res = winReadSaveGame(dlg.GetPathName()); + + theApp.rewindCount = 0; + theApp.rewindCounter = 0; + theApp.rewindSaveNeeded = false; + + if (res) + { + if (VBAMovieActive() && !VBAMovieReadOnly()) + { + VBAMovieSwitchToRecording(); + } + systemScreenMessage(winResLoadString(IDS_LOADED_STATE)); + } + } +} + +void MainWnd::OnUpdateFileLoad(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +BOOL MainWnd::OnFileLoadSlot(UINT nID) +{ + nID = nID + 1 - ID_FILE_LOADGAME_SLOT1; + + CString filename = winGetSavestateFilename(theApp.gameFilename, nID); + + bool res = winReadSaveGame(filename); + + // deleting rewinds because you loaded a save state is stupid +/// theApp.rewindCount = 0; +/// theApp.rewindCounter = 0; +/// theApp.rewindSaveNeeded = false; + + if (res) + { + CString format; + if (VBAMovieActive()) + { + if (VBAMovieReadOnly()) + { + format = winResLoadString(IDS_REPLAYED_STATE_N); + } + else + { + VBAMovieSwitchToRecording(); + format = winResLoadString(IDS_RERECORDED_STATE_N); + } + } + else + { + format = winResLoadString(IDS_LOADED_STATE_N); + } + + CString buffer; + buffer.Format(format, nID); + systemScreenMessage(buffer); + + int lastSlot = theApp.currentSlot; + + if (theApp.loadMakesRecent) + { + // to update the file's modification date + SYSTEMTIME st; + FILETIME ft; + GetSystemTime(&st); + SystemTimeToFileTime(&st, &ft); + HANDLE fh = CreateFile(filename, + GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + OPEN_EXISTING, + 0, + NULL); + if (fh != INVALID_HANDLE_VALUE) + SetFileTime(fh, NULL, NULL, &ft); + CloseHandle(fh); + } + + if (theApp.loadMakesCurrent) + theApp.currentSlot = nID - 1; + else + theApp.currentSlot = lastSlot; // restore value in case the call to OnFileSaveSlot changed it + + theApp.frameSearching = false; + theApp.frameSearchSkipping = false; + } + + return res; +} + +void MainWnd::OnFileSave() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { ".sgm", NULL }; + CString filter = winResLoadFilter(IDS_FILTER_SGM); + CString title = winResLoadString(IDS_SELECT_SAVE_GAME_NAME); + + CString saveName = winGetDestFilename(theApp.gameFilename, IDS_SAVE_DIR, exts[0]); + CString saveDir = winGetDestDir(IDS_SAVE_DIR); + + FileDlg dlg(this, saveName, filter, 0, "SGM", exts, saveDir, title, true); + + if (dlg.DoModal() == IDOK) + { + bool res = winWriteSaveGame(dlg.GetPathName()); + if (res) + systemScreenMessage(winResLoadString(IDS_WROTE_STATE)); + } +} + +void MainWnd::OnUpdateFileSave(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +BOOL MainWnd::OnFileSaveSlot(UINT nID) +{ + nID = nID + 1 - ID_FILE_SAVEGAME_SLOT1; + + if (theApp.saveMakesCurrent) + theApp.currentSlot = nID - 1; + + CString filename = winGetSavestateFilename(theApp.gameFilename, nID); + + bool res = winWriteSaveGame(filename); + + CString format = winResLoadString(IDS_WROTE_STATE_N); + CString buffer; + buffer.Format(format, nID); + + systemScreenMessage(buffer); + + return res; +} + +BOOL MainWnd::OnSelectSlot(UINT nID) +{ + nID -= ID_SELECT_SLOT1; + theApp.currentSlot = nID; + + CString buffer; + buffer.Format("Slot %d selected", nID + 1); + systemScreenMessage(buffer, 0); + + return true; +} + +void MainWnd::OnFileImportBatteryfile() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { ".sav", NULL }; + CString filter = winResLoadFilter(IDS_FILTER_SAV); + CString title = winResLoadString(IDS_SELECT_BATTERY_FILE); + + CString batteryName = winGetDestFilename(theApp.gameFilename, IDS_BATTERY_DIR, exts[0]); + CString batteryDir = winGetDestDir(IDS_BATTERY_DIR); + + FileDlg dlg(this, batteryName, filter, 0, "SAV", exts, batteryDir, title, false, true); + + if (dlg.DoModal() == IDCANCEL) + return; + + CString str1 = winResLoadString(IDS_SAVE_WILL_BE_LOST); + CString str2 = winResLoadString(IDS_CONFIRM_ACTION); + + systemSoundClearBuffer(); + if (MessageBox(str1, + str2, + MB_OKCANCEL) == IDCANCEL) + return; + + bool res = false; + + res = theApp.emulator.emuReadBattery(dlg.GetPathName()); + + if (!res) + systemMessage(MSG_CANNOT_OPEN_FILE, "Cannot open file %s", dlg.GetPathName()); + else + { + theApp.emulator.emuReset(true); + } +} + +void MainWnd::OnUpdateFileImportBatteryfile(CCmdUI *pCmdUI) +{ + // we allow this as we allow using cheats during recording + pCmdUI->Enable(emulating /*&& !VBAMovieActive()*/); +} + +void MainWnd::OnFileImportGamesharkcodefile() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { NULL }; + CString filter = systemCartridgeType == 0 ? winResLoadFilter(IDS_FILTER_SPC) : winResLoadFilter(IDS_FILTER_GCF); + CString title = winResLoadString(IDS_SELECT_CODE_FILE); + + FileDlg dlg(this, "", filter, 0, "", exts, "", title, false, true); + + if (dlg.DoModal() == IDCANCEL) + return; + + CString str1 = winResLoadString(IDS_CODES_WILL_BE_LOST); + CString str2 = winResLoadString(IDS_CONFIRM_ACTION); + + systemSoundClearBuffer(); + if (MessageBox(str1, + str2, + MB_OKCANCEL) == IDCANCEL) + return; + + CString file = dlg.GetPathName(); + bool res = winImportGSACodeFile(file); +} + +void MainWnd::OnUpdateFileImportGamesharkcodefile(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating /*&& !VBAMovieActive()*/); +} + +void MainWnd::OnFileImportGamesharksnapshot() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { NULL }; + CString filter = systemCartridgeType == 1 ? winResLoadFilter(IDS_FILTER_GBS) : winResLoadFilter(IDS_FILTER_SPS); + CString title = winResLoadString(IDS_SELECT_SNAPSHOT_FILE); + + FileDlg dlg(this, "", filter, 0, "", exts, "", title, false, true); + + if (dlg.DoModal() == IDCANCEL) + return; + + CString str1 = winResLoadString(IDS_SAVE_WILL_BE_LOST); + CString str2 = winResLoadString(IDS_CONFIRM_ACTION); + + systemSoundClearBuffer(); + if (MessageBox(str1, + str2, + MB_OKCANCEL) == IDCANCEL) + return; + + if (systemCartridgeType == 1) + gbReadGSASnapshot(dlg.GetPathName()); + else + CPUReadGSASnapshot(dlg.GetPathName()); +} + +void MainWnd::OnUpdateFileImportGamesharksnapshot(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating /*&& !VBAMovieActive()*/); +} + +void MainWnd::OnFileExportBatteryfile() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { ".sav", ".dat", NULL }; + CString filter = winResLoadFilter(IDS_FILTER_SAV); + CString title = winResLoadString(IDS_SELECT_BATTERY_FILE); + + CString batteryName = winGetDestFilename(theApp.gameFilename, IDS_BATTERY_DIR, exts[0]); + CString batteryDir = winGetDestDir(IDS_BATTERY_DIR); + + FileDlg dlg(this, batteryName, filter, 1, "SAV", exts, batteryDir, title, true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + bool result = false; + + if (systemCartridgeType == 1) + { + result = gbWriteBatteryFile(dlg.GetPathName(), false); + } + else + result = theApp.emulator.emuWriteBattery(dlg.GetPathName()); + + if (!result) + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", + dlg.GetPathName()); +} + +void MainWnd::OnUpdateFileExportBatteryfile(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileExportGamesharksnapshot() +{ + theApp.winCheckFullscreen(); + if (eepromInUse) + { + systemMessage(IDS_EEPROM_NOT_SUPPORTED, "EEPROM saves cannot be exported"); + return; + } + + LPCTSTR exts[] = { ".sps", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_SPS); + CString title = winResLoadString(IDS_SELECT_SNAPSHOT_FILE); + + CString name = winGetDestFilename(theApp.gameFilename, CString(), exts[0]); + + FileDlg dlg(this, name, filter, 1, "SPS", exts, "", title, true); + + if (dlg.DoModal() == IDCANCEL) + return; + + char buffer[16]; + strncpy(buffer, (const char *)&rom[0xa0], 12); + buffer[12] = 0; + + ExportGSASnapshot dlg2(dlg.GetPathName(), buffer, this); + dlg2.DoModal(); +} + +void MainWnd::OnUpdateFileExportGamesharksnapshot(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating && systemCartridgeType == 0); +} + +void MainWnd::OnFileQuickScreencapture() +{ + extern int32 captureNumber; // GBAGlobals.cpp + captureNumber = winScreenCapture(captureNumber); +} + +void MainWnd::OnFileScreencapture() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { ".png", ".bmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + CString ext; + + if (theApp.captureFormat != 0) + ext.Format(".bmp"); + else + ext.Format(".png"); + + CString captureName = winGetDestFilename(theApp.gameFilename, IDS_CAPTURE_DIR, ext); + CString captureDir = winGetDestDir(IDS_CAPTURE_DIR); + + FileDlg dlg(this, + captureName, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + captureDir, + title, + true); + + if (dlg.DoModal() == IDCANCEL) + return; + + if (dlg.getFilterIndex() == 2) + theApp.emulator.emuWriteBMP(dlg.GetPathName()); + else + theApp.emulator.emuWritePNG(dlg.GetPathName()); + + systemScreenMessage(winResLoadString(IDS_SCREEN_CAPTURE)); +} + +void MainWnd::OnUpdateFileScreencapture(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileRominformation() +{ + theApp.winCheckFullscreen(); + if (systemCartridgeType == 0) + { + RomInfoGBA dlg(rom); + dlg.DoModal(); + } + else + { + RomInfoGB dlg(gbRom); + dlg.DoModal(); + } +} + +void MainWnd::OnUpdateFileRominformation(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileTogglemenu() +{ + theApp.menuToggle = !theApp.menuToggle; + + if (theApp.menuToggle) + { + SetMenu(&theApp.m_menu); + if (theApp.tripleBuffering) + { + if (theApp.display) + theApp.display->renderMenu(); + } + } + else + { + SetMenu(NULL); + } + + theApp.adjustDestRect(); +} + +void MainWnd::OnUpdateFileTogglemenu(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption > VIDEO_4X); +} + +void MainWnd::OnFileSavegameOldestslot() +{ + if (!emulating) + return; + + CFileStatus status; + CString str; + time_t time = -1; + int found = -1; + + for (int i = 0; i < 10; i++) + { + if (CFile::GetStatus(winGetSavestateFilename(theApp.gameFilename, i + 1), status)) + { + if (time - status.m_mtime.GetTime() > 0 || time == -1) + { + time = (time_t)status.m_mtime.GetTime(); + found = i; + } + } + else + { + found = i; + break; + } + } + + OnFileSaveSlot(ID_FILE_SAVEGAME_SLOT1 + found); +} + +void MainWnd::OnUpdateFileSavegameOldestslot(CCmdUI *pCmdUI) +{ + bool enabled = emulating; + if (pCmdUI->m_pMenu != NULL) + { + CFileStatus status; + time_t time = -1; + int found = -1; + + if (emulating) + { + for (int i = 0; i < 10; i++) + { + if (CFile::GetStatus(winGetSavestateFilename(theApp.gameFilename, i + 1), status)) + { + if (time - status.m_mtime.GetTime() > 0 || time == -1) + { + time = (time_t)status.m_mtime.GetTime(); + found = i; + } + } + else + { + found = i; + break; + } + } + } + + CString str; + enabled = (found != -1); + if (enabled) + str.Format("&Oldest Slot (#%d)", found + 1); + else + str.Format("&Oldest Slot", found + 1); + + pCmdUI->SetText(str); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + pCmdUI->Enable(enabled); +} + +void MainWnd::OnFileLoadgameMostrecent() +{ + if (!emulating) + return; + + CFileStatus status; + CString str; + time_t time = 0; + int found = -1; + + for (int i = 0; i < 10; i++) + { + if (CFile::GetStatus(winGetSavestateFilename(theApp.gameFilename, i + 1), status)) + { + if (status.m_mtime.GetTime() > time) + { + time = (time_t)status.m_mtime.GetTime(); + found = i; + } + } + } + + if (found != -1) + { + OnFileLoadSlot(ID_FILE_LOADGAME_SLOT1 + found); + } +} + +void MainWnd::OnUpdateFileLoadgameMostrecent(CCmdUI *pCmdUI) +{ + bool enabled = emulating; + if (pCmdUI->m_pMenu != NULL) + { + CFileStatus status; + int found = -1; + + time_t time = 0; + if (emulating) + { + for (int i = 0; i < 10; i++) + { + if (CFile::GetStatus(winGetSavestateFilename(theApp.gameFilename, i + 1), status)) + { + if (status.m_mtime.GetTime() > time) + { + time = (time_t)status.m_mtime.GetTime(); + found = i; + } + } + } + } + + CString str; + enabled = (found != -1); + if (enabled) + str.Format("Most &Recent Slot (#%d)", found + 1); + else + str.Format("Most &Recent Slot", found + 1); + + pCmdUI->SetText(str); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + pCmdUI->Enable(enabled); +} + +void MainWnd::OnUpdateFileLoadSlot(CCmdUI *pCmdUI) +{ + int slotID = pCmdUI->m_nID - ID_FILE_LOADGAME_SLOT1 + 1; + + if (pCmdUI->m_pMenu != NULL) + { + pCmdUI->SetText(winGetSavestateMenuString(theApp.gameFilename, slotID)); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + pCmdUI->Enable(emulating && winFileExists(winGetSavestateFilename(theApp.gameFilename, slotID))); +} + +void MainWnd::OnUpdateFileSaveSlot(CCmdUI *pCmdUI) +{ + if (pCmdUI->m_pMenu != NULL) + { + int slotID = pCmdUI->m_nID - ID_FILE_SAVEGAME_SLOT1 + 1; + + pCmdUI->SetText(winGetSavestateMenuString(theApp.gameFilename, slotID)); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + pCmdUI->Enable(emulating); +} + +void MainWnd::OnUpdateSelectSlot(CCmdUI *pCmdUI) +{ + if (pCmdUI->m_pMenu != NULL) + { + int slot = pCmdUI->m_nID - ID_SELECT_SLOT1; + + pCmdUI->SetText(winGetSavestateMenuString(theApp.gameFilename, slot + 1)); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + + pCmdUI->SetCheck(slot == theApp.currentSlot); + } +} + +void MainWnd::OnFileLoadgameAutoloadmostrecent() +{ + theApp.autoLoadMostRecent = !theApp.autoLoadMostRecent; +} + +void MainWnd::OnUpdateFileLoadgameAutoloadmostrecent(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoLoadMostRecent); +} + +void MainWnd::OnFileLoadgameMakeRecent() +{ + theApp.loadMakesRecent = !theApp.loadMakesRecent; +} + +void MainWnd::OnUpdateFileLoadgameMakeRecent(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.loadMakesRecent); +} + +void MainWnd::OnFileSavegameCurrent() +{ + OnFileSaveSlot(ID_FILE_SAVEGAME_SLOT1 + theApp.currentSlot); +} + +void MainWnd::OnUpdateFileSavegameCurrent(CCmdUI *pCmdUI) +{ + if (pCmdUI->m_pMenu != NULL) + { + int slotID = theApp.currentSlot + 1; + + CString str; + str.Format("&Current Slot (#%d)", slotID); + + pCmdUI->SetText(str); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + pCmdUI->Enable(emulating); +} + +void MainWnd::OnFileLoadgameCurrent() +{ + OnFileLoadSlot(ID_FILE_LOADGAME_SLOT1 + theApp.currentSlot); +} + +void MainWnd::OnUpdateFileLoadgameCurrent(CCmdUI *pCmdUI) +{ + int slotID = theApp.currentSlot + 1; + + if (pCmdUI->m_pMenu != NULL) + { + CString str; + str.Format("&Current Slot (#%d)", slotID); + + pCmdUI->SetText(str); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + CFileStatus status; + pCmdUI->Enable(emulating && CFile::GetStatus(winGetSavestateFilename(theApp.gameFilename, slotID), status)); +} + +void MainWnd::OnFileLoadgameMakeCurrent() +{ + theApp.loadMakesCurrent = !theApp.loadMakesCurrent; +} + +void MainWnd::OnUpdateFileLoadgameMakeCurrent(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.loadMakesCurrent); +} + +void MainWnd::OnFileSavegameMakeCurrent() +{ + theApp.saveMakesCurrent = !theApp.saveMakesCurrent; +} + +void MainWnd::OnUpdateFileSavegameMakeCurrent(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.saveMakesCurrent); +} + +void MainWnd::OnFileSavegameIncrementSlot() +{ + theApp.currentSlot = (theApp.currentSlot + 1) % 10; + + char str [32]; + sprintf(str, "Current Slot: %d", theApp.currentSlot + 1); + systemScreenMessage(str, 0); +} + +void MainWnd::OnUpdateFileSavegameIncrementSlot(CCmdUI *pCmdUI) +{ + if (pCmdUI->m_pMenu != NULL) + { + int slotID = theApp.currentSlot + 1; + + CString str; + str.Format("&Increase Current Slot (#%d -> #%d)", slotID, slotID % 10 + 1); + + pCmdUI->SetText(str); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } +} + +void MainWnd::OnFileSavegameDecrementSlot() +{ + theApp.currentSlot = (theApp.currentSlot + 9) % 10; + + char str [32]; + sprintf(str, "Current Slot: %d", theApp.currentSlot + 1); + systemScreenMessage(str, 0); +} + +void MainWnd::OnUpdateFileSavegameDecrementSlot(CCmdUI *pCmdUI) +{ + if (pCmdUI->m_pMenu != NULL) + { + int slotID = theApp.currentSlot + 1; + + CString str; + str.Format("&Decrease Current Slot (#%d -> #%d)", slotID, (slotID + 8) % 10 + 1); + + pCmdUI->SetText(str); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } +} + +void MainWnd::OnFileSlotDisplayModificationTime() +{ + theApp.showSlotTime = !theApp.showSlotTime; +} + +void MainWnd::OnUpdateFileSlotDisplayModificationTime(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.showSlotTime); +} + +void MainWnd::OnFileLuaOpen() +{ + theApp.winCheckFullscreen(); + + if (!LuaConsoleHWnd) + { + LuaConsoleHWnd = ::CreateDialog(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD_LUA), AfxGetMainWnd()->GetSafeHwnd(), (DLGPROC) DlgLuaScriptDialog); + } + else + ::SetForegroundWindow(LuaConsoleHWnd); +} + +void MainWnd::OnUpdateFileLuaOpen(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(LuaConsoleHWnd != NULL); + pCmdUI->Enable(true); +} + +void MainWnd::OnFileLuaCloseAll() +{ + if (LuaConsoleHWnd) + ::PostMessage(LuaConsoleHWnd, WM_CLOSE, 0, 0); +} + +void MainWnd::OnFileLuaReload() +{ + VBAReloadLuaCode(); +} + +void MainWnd::OnFileLuaStop() +{ + VBALuaStop(); +} + +void MainWnd::OnFileRamSearch() +{ + theApp.winCheckFullscreen(); + + if (!RamSearchHWnd) + { + reset_address_info(); + LRESULT CALLBACK RamSearchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); + ::CreateDialog(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD_RAMSEARCH), AfxGetMainWnd()->GetSafeHwnd(), (DLGPROC) RamSearchProc); + } + else + ::SetForegroundWindow(RamSearchHWnd); +} + +void MainWnd::OnUpdateFileRamSearch(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(TRUE); +} + +void MainWnd::OnFileRamWatch() +{ + theApp.winCheckFullscreen(); + + if (!RamWatchHWnd) + { + LRESULT CALLBACK RamWatchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); + RamWatchHWnd = ::CreateDialog(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDD_RAMWATCH), AfxGetMainWnd()->GetSafeHwnd(), (DLGPROC) RamWatchProc); + } + else + ::SetForegroundWindow(RamWatchHWnd); +} + +void MainWnd::OnUpdateFileRamWatch(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(TRUE); +} + +void MainWnd::OnUpdateFileLuaCloseAll(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(LuaConsoleHWnd != NULL); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MainWndHelp.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MainWndHelp.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,30 @@ +#include "stdafx.h" +#include "resource.h" +#include "MainWnd.h" +#include "AboutDialog.h" +#include "BugReport.h" +#include "VBA.h" + +extern int emulating; + +void MainWnd::OnHelpAbout() +{ + theApp.winCheckFullscreen(); + AboutDialog dlg; + + dlg.DoModal(); +} + +void MainWnd::OnHelpFaq() +{ + ::ShellExecute(0, _T("open"), "http://vba.ngemu.com/faq.shtml", + 0, 0, SW_SHOWNORMAL); +} + +void MainWnd::OnHelpBugreport() +{ + BugReport dlg(theApp.m_pMainWnd); + + dlg.DoModal(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MainWndOptions.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MainWndOptions.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,2309 @@ +#include "stdafx.h" +#include "resource.h" +#include "MainWnd.h" +#include "Associate.h" +#include "Directories.h" +#include "FileDlg.h" +#include "GBColorDlg.h" +#include "Joypad.h" +#include "MaxScale.h" +#include "ModeConfirm.h" +#include "Reg.h" +#include "RewindInterval.h" +#include "Throttle.h" +#include "TextOptions.h" +#include "WinResUtil.h" +#include "VBA.h" + +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gba/Flash.h" +#include "../gba/GBASound.h" +#include "../gba/agbprint.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +#include "../gb/gbPrinter.h" +#include "../common/inputGlobal.h" +#include "../common/movie.h" +#include "../version.h" + +extern int emulating; + +#define VBA_CONFIRM_MODE WM_APP + 100 + +void MainWnd::OnOptionsFrameskipThrottleNothrottle() +{ + systemSetThrottle(0); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottleNothrottle(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 0); +} + +void MainWnd::OnOptionsFrameskipThrottle6() +{ + systemSetThrottle(6); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle6(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 6); +} + +void MainWnd::OnOptionsFrameskipThrottle15() +{ + systemSetThrottle(15); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle15(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 15); +} + +void MainWnd::OnOptionsFrameskipThrottle25() +{ + systemSetThrottle(25); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle25(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 25); +} + +void MainWnd::OnOptionsFrameskipThrottle50() +{ + systemSetThrottle(50); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle50(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 50); +} + +void MainWnd::OnOptionsFrameskipThrottle75() +{ + systemSetThrottle(75); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle75(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 75); +} + +void MainWnd::OnOptionsFrameskipThrottle100() +{ + systemSetThrottle(100); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle100(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 100); +} + +void MainWnd::OnOptionsFrameskipThrottle125() +{ + systemSetThrottle(125); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle125(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 125); +} + +void MainWnd::OnOptionsFrameskipThrottle150() +{ + systemSetThrottle(150); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle150(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 150); +} + +void MainWnd::OnOptionsFrameskipThrottle200() +{ + systemSetThrottle(200); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle200(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 200); +} + +void MainWnd::OnOptionsFrameskipThrottle300() +{ + systemSetThrottle(300); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle300(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 300); +} + +void MainWnd::OnOptionsFrameskipThrottle400() +{ + systemSetThrottle(400); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle400(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 400); +} + +void MainWnd::OnOptionsFrameskipThrottle600() +{ + systemSetThrottle(600); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle600(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 600); +} + +void MainWnd::OnOptionsFrameskipThrottle1000() +{ + systemSetThrottle(1000); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottle1000(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.throttle == 1000); +} + +void MainWnd::OnOptionsFrameskipThrottleOther() +{ + Throttle dlg; + int v = dlg.DoModal(); + if (v) + systemSetThrottle(v); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottleOther(CCmdUI*pCmdUI) +{ +} + +void MainWnd::OnOptionsFrameskipThrottleIncrease() +{ + systemIncreaseThrottle(); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottleIncrease(CCmdUI*pCmdUI) +{ +} + +void MainWnd::OnOptionsFrameskipThrottleDecrease() +{ + systemDecreaseThrottle(); +} + +void MainWnd::OnUpdateOptionsFrameskipThrottleDecrease(CCmdUI*pCmdUI) +{ +} + +/* + void MainWnd::OnOptionsFrameskipAutomatic() + { + theApp.autoFrameSkip = !theApp.autoFrameSkip; + if(!theApp.autoFrameSkip && emulating) + theApp.updateFrameSkip(); + } + + void MainWnd::OnUpdateOptionsFrameskipAutomatic(CCmdUI* pCmdUI) + { + pCmdUI->SetCheck(theApp.autoFrameSkip); + } + */ + +void MainWnd::OnOptionsFrameskipAccuratePitch() +{ + theApp.accuratePitchThrottle = true; +} + +void MainWnd::OnUpdateOptionsFrameskipAccuratePitch(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.accuratePitchThrottle); + pCmdUI->Enable(!soundOffFlag && synchronize); +} + +void MainWnd::OnOptionsFrameskipAccurateSpeed() +{ + theApp.accuratePitchThrottle = false; +} + +void MainWnd::OnUpdateOptionsFrameskipAccurateSpeed(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(!theApp.accuratePitchThrottle); + pCmdUI->Enable(!soundOffFlag && synchronize); +} + +BOOL MainWnd::OnOptionsFrameskip(UINT nID) +{ + switch (nID) + { + case ID_OPTIONS_VIDEO_FRAMESKIP_0: + case ID_OPTIONS_VIDEO_FRAMESKIP_1: + case ID_OPTIONS_VIDEO_FRAMESKIP_2: + case ID_OPTIONS_VIDEO_FRAMESKIP_3: + case ID_OPTIONS_VIDEO_FRAMESKIP_4: + case ID_OPTIONS_VIDEO_FRAMESKIP_5: + case ID_OPTIONS_VIDEO_FRAMESKIP_6: + case ID_OPTIONS_VIDEO_FRAMESKIP_7: + case ID_OPTIONS_VIDEO_FRAMESKIP_8: + case ID_OPTIONS_VIDEO_FRAMESKIP_9: + if (systemCartridgeType == 0) + { + frameSkip = nID - ID_OPTIONS_VIDEO_FRAMESKIP_0; + } + else + { + gbFrameSkip = nID - ID_OPTIONS_VIDEO_FRAMESKIP_0; + } + if (emulating) + theApp.updateFrameSkip(); + return TRUE; + break; + } + return FALSE; +} + +void MainWnd::OnUpdateOptionsVideoFrameskip0(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 0 : gbFrameSkip == 0); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip1(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 1 : gbFrameSkip == 1); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip2(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 2 : gbFrameSkip == 2); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip3(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 3 : gbFrameSkip == 3); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip4(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 4 : gbFrameSkip == 4); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip5(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 5 : gbFrameSkip == 5); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip6(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 6 : gbFrameSkip == 6); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip7(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 7 : gbFrameSkip == 7); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip8(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 8 : gbFrameSkip == 8); +} + +void MainWnd::OnUpdateOptionsVideoFrameskip9(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(systemCartridgeType == 0 ? frameSkip == 9 : gbFrameSkip == 9); +} + +void MainWnd::OnOptionsVideoVsync() +{ + theApp.vsync = !theApp.vsync; +} + +void MainWnd::OnUpdateOptionsVideoVsync(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.vsync); +} + +void MainWnd::OnUpdateOptionsVideoX1(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_1X); +} + +void MainWnd::OnUpdateOptionsVideoX2(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_2X); +} + +void MainWnd::OnUpdateOptionsVideoX3(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_3X); +} + +void MainWnd::OnUpdateOptionsVideoX4(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_4X); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen320x240(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(theApp.mode320Available); + pCmdUI->SetCheck(theApp.videoOption == VIDEO_320x240); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen640x480(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(theApp.mode640Available); + pCmdUI->SetCheck(theApp.videoOption == VIDEO_640x480); +} + +void MainWnd::OnUpdateOptionsVideoFullscreen800x600(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(theApp.mode800Available); + pCmdUI->SetCheck(theApp.videoOption == VIDEO_800x600); +} + +BOOL MainWnd::OnOptionVideoSize(UINT nID) +{ + theApp.updateVideoSize(nID); + theApp.m_pMainWnd->PostMessage(VBA_CONFIRM_MODE); + return TRUE; +} + +void MainWnd::OnOptionsVideoFullscreen320x240() +{ + OnOptionVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN320X240); +} + +void MainWnd::OnOptionsVideoFullscreen640x480() +{ + OnOptionVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN640X480); +} + +void MainWnd::OnOptionsVideoFullscreen800x600() +{ + OnOptionVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN800X600); +} + +void MainWnd::OnOptionsVideoFullscreen() +{ + theApp.winCheckFullscreen(); + GUID *pGUID = NULL; + int size = theApp.display->selectFullScreenMode(&pGUID); + if (size != -1) + { + int width = (size >> 12) & 4095; + int height = (size & 4095); + int colorDepth = (size >> 24); + if (width != theApp.fsWidth || + height != theApp.fsHeight || + colorDepth != theApp.fsColorDepth || + pGUID != theApp.pVideoDriverGUID || + theApp.videoOption != VIDEO_OTHER) + { + theApp.fsForceChange = true; + theApp.fsWidth = width; + theApp.fsHeight = height; + theApp.fsColorDepth = colorDepth; + theApp.pVideoDriverGUID = pGUID; + if (pGUID) + { + theApp.videoDriverGUID = *pGUID; + regSetDwordValue("defaultVideoDriver", FALSE); + regSetBinaryValue("videoDriverGUID", + (char *)pGUID, sizeof(GUID)); + } + else + { + regSetDwordValue("defaultVideoDriver", TRUE); + } + theApp.updateVideoSize(ID_OPTIONS_VIDEO_FULLSCREEN); + theApp.m_pMainWnd->PostMessage(VBA_CONFIRM_MODE); + } + } +} + +void MainWnd::OnUpdateOptionsVideoFullscreen(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.videoOption == VIDEO_OTHER); +} + +void MainWnd::OnOptionsVideoDisablesfx() +{ + cpuDisableSfx = !cpuDisableSfx; + if (emulating && systemCartridgeType == 0) + CPUUpdateRender(); +} + +void MainWnd::OnUpdateOptionsVideoDisablesfx(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(cpuDisableSfx); +} + +void MainWnd::OnOptionsVideoFullscreenstretchtofit() +{ + theApp.fullScreenStretch = !theApp.fullScreenStretch; + theApp.updateWindowSize(theApp.videoOption); + if (theApp.display) + theApp.display->clear(); +} + +void MainWnd::OnUpdateOptionsVideoFullscreenstretchtofit(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.fullScreenStretch); +} + +BOOL MainWnd::OnVideoLayer(UINT nID) +{ + layerSettings ^= 0x0100 << ((nID & 0xFFFF) - ID_OPTIONS_VIDEO_LAYERS_BG0); + layerEnable = DISPCNT & layerSettings; + CPUUpdateRenderBuffers(false); + return TRUE; +} + +void MainWnd::OnUpdateVideoLayer(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck((layerSettings >> (8 + pCmdUI->m_nID - ID_OPTIONS_VIDEO_LAYERS_BG0)) & 1); + switch (pCmdUI->m_nID) + { + case ID_OPTIONS_VIDEO_LAYERS_BG1: + case ID_OPTIONS_VIDEO_LAYERS_BG2: + case ID_OPTIONS_VIDEO_LAYERS_BG3: + case ID_OPTIONS_VIDEO_LAYERS_WIN1: + case ID_OPTIONS_VIDEO_LAYERS_OBJWIN: + pCmdUI->Enable(systemCartridgeType == 0); + break; + } +} + +void MainWnd::OnOptionsVideoRendermethodGdi() +{ + theApp.renderMethod = GDI; + theApp.updateRenderMethod(false); +} + +void MainWnd::OnUpdateOptionsVideoRendermethodGdi(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.renderMethod == GDI); +} + +void MainWnd::OnOptionsVideoRendermethodDirectdraw() +{ + theApp.renderMethod = DIRECT_DRAW; + theApp.updateRenderMethod(false); +} + +void MainWnd::OnUpdateOptionsVideoRendermethodDirectdraw(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.renderMethod == DIRECT_DRAW); +} + +void MainWnd::OnOptionsVideoRendermethodDirect3d() +{ + theApp.renderMethod = DIRECT_3D; + theApp.updateRenderMethod(false); +} + +void MainWnd::OnUpdateOptionsVideoRendermethodDirect3d(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.renderMethod == DIRECT_3D); +} + +void MainWnd::OnOptionsVideoRendermethodOpengl() +{ + theApp.renderMethod = OPENGL; + theApp.updateRenderMethod(false); +} + +void MainWnd::OnUpdateOptionsVideoRendermethodOpengl(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.renderMethod == OPENGL); +} + +void MainWnd::OnOptionsVideoTriplebuffering() +{ + theApp.tripleBuffering = !theApp.tripleBuffering; +} + +void MainWnd::OnUpdateOptionsVideoTriplebuffering(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.tripleBuffering); +} + +void MainWnd::OnOptionsVideoDdrawemulationonly() +{ + theApp.ddrawEmulationOnly = !theApp.ddrawEmulationOnly; +} + +void MainWnd::OnUpdateOptionsVideoDdrawemulationonly(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.ddrawEmulationOnly); +} + +void MainWnd::OnOptionsVideoDdrawusevideomemory() +{ + theApp.ddrawUseVideoMemory = !theApp.ddrawUseVideoMemory; +} + +void MainWnd::OnUpdateOptionsVideoDdrawusevideomemory(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.ddrawUseVideoMemory); +} + +void MainWnd::OnOptionsVideoRenderoptionsD3dnofilter() +{ + theApp.d3dFilter = 0; + if (theApp.display) + theApp.display->setOption("d3dFilter", 0); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsD3dnofilter(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.d3dFilter == 0); +} + +void MainWnd::OnOptionsVideoRenderoptionsD3dbilinear() +{ + theApp.d3dFilter = 1; + if (theApp.display) + theApp.display->setOption("d3dFilter", 1); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsD3dbilinear(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.d3dFilter == 1); +} + +void MainWnd::OnOptionsVideoRenderoptionsGlnearest() +{ + theApp.glFilter = 0; + if (theApp.display) + theApp.display->setOption("glFilter", 0); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsGlnearest(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.glFilter == 0); +} + +void MainWnd::OnOptionsVideoRenderoptionsGlbilinear() +{ + theApp.glFilter = 1; + if (theApp.display) + theApp.display->setOption("glFilter", 1); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsGlbilinear(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.glFilter == 1); +} + +void MainWnd::OnOptionsVideoRenderoptionsGltriangle() +{ + theApp.glType = 0; + if (theApp.display) + theApp.display->setOption("glType", 0); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsGltriangle(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.glType == 0); +} + +void MainWnd::OnOptionsVideoRenderoptionsGlquads() +{ + theApp.glType = 1; + if (theApp.display) + theApp.display->setOption("glType", 1); +} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsGlquads(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.glType == 1); +} + +void MainWnd::OnOptionsVideoRenderoptionsSelectskin() +{} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsSelectskin(CCmdUI*pCmdUI) +{} + +void MainWnd::OnOptionsVideoRenderoptionsSkin() +{} + +void MainWnd::OnUpdateOptionsVideoRenderoptionsSkin(CCmdUI*pCmdUI) +{} + +void MainWnd::OnOptionsEmulatorAssociate() +{ + theApp.winCheckFullscreen(); + Associate dlg; + dlg.DoModal(); +} + +void MainWnd::OnOptionsEmulatorDirectories() +{ + theApp.winCheckFullscreen(); + Directories dlg; + dlg.DoModal(); +} + +void MainWnd::OnOptionsEmulatorFilenamePreference(UINT nID) +{ + theApp.filenamePreference = nID - ID_OPTIONS_PREFER_ARCHIVE_NAME; +} + +void MainWnd::OnUpdateOptionsEmulatorFilenamePreference(CCmdUI *pCmdUI) +{ + pCmdUI->SetRadio(pCmdUI->m_nID == theApp.filenamePreference + ID_OPTIONS_PREFER_ARCHIVE_NAME); +} + +void MainWnd::OnOptionsVideoDisablestatusmessages() +{ + theApp.disableStatusMessage = !theApp.disableStatusMessage; +} + +void MainWnd::OnUpdateOptionsVideoDisablestatusmessages(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.disableStatusMessage); +} + +void MainWnd::OnOptionsEmulatorSynchronize() +{ + synchronize = !synchronize; +} + +void MainWnd::OnUpdateOptionsEmulatorSynchronize(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(synchronize); +} + +void MainWnd::OnOptionsEmulatorAlwaysOnTop() +{ + theApp.alwaysOnTop = !theApp.alwaysOnTop; + SetWindowPos((theApp.alwaysOnTop ? &wndTopMost : &wndNoTopMost), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); +} + +void MainWnd::OnUpdateOptionsEmulatorAlwaysOnTop(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.alwaysOnTop); +} + +void MainWnd::OnOptionsEmulatorPausewheninactive() +{ + theApp.pauseWhenInactive = !theApp.pauseWhenInactive; +} + +void MainWnd::OnUpdateOptionsEmulatorPausewheninactive(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.pauseWhenInactive); +} + +void MainWnd::OnOptionsEmulatorEnableBackgroundInput() +{ + theApp.enableBackgroundInput = !theApp.enableBackgroundInput; +} + +void MainWnd::OnUpdateOptionsEmulatorEnableBackgroundInput(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.enableBackgroundInput); +} + +BOOL MainWnd::OnOptionsPriority(UINT nID) +{ + switch (nID) + { + case ID_OPTIONS_PRIORITY_HIGHEST: + theApp.threadPriority = 0; + break; + case ID_OPTIONS_PRIORITY_ABOVENORMAL: + theApp.threadPriority = 1; + break; + case ID_OPTIONS_PRIORITY_NORMAL: + theApp.threadPriority = 2; + break; + case ID_OPTIONS_PRIORITY_BELOWNORMAL: + theApp.threadPriority = 3; + break; + default: + return FALSE; + } + theApp.updatePriority(); + + return TRUE; +} + +void MainWnd::OnUpdateOptionsPriority(CCmdUI *pCmdUI) +{ + switch (pCmdUI->m_nID) + { + case ID_OPTIONS_PRIORITY_HIGHEST: + pCmdUI->SetCheck(theApp.threadPriority == 0); + break; + case ID_OPTIONS_PRIORITY_ABOVENORMAL: + pCmdUI->SetCheck(theApp.threadPriority == 1); + break; + case ID_OPTIONS_PRIORITY_NORMAL: + pCmdUI->SetCheck(theApp.threadPriority == 2); + break; + case ID_OPTIONS_PRIORITY_BELOWNORMAL: + pCmdUI->SetCheck(theApp.threadPriority == 3); + break; + } +} + +void MainWnd::OnOptionsEmulatorSpeeduptoggle() +{ + theApp.speedupToggle = !theApp.speedupToggle; +} + +void MainWnd::OnUpdateOptionsEmulatorSpeeduptoggle(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.speedupToggle); +} + +void MainWnd::OnOptionsEmulatorRemoveintrosgba() +{ + // theApp.removeIntros = !theApp.removeIntros; +} + +void MainWnd::OnUpdateOptionsEmulatorRemoveintrosgba(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(false); + // pCmdUI->SetCheck(theApp.removeIntros); +} + +void MainWnd::OnOptionsEmulatorAutomaticallyipspatch() +{ + theApp.autoIPS = !theApp.autoIPS; +} + +void MainWnd::OnUpdateOptionsEmulatorAutomaticallyipspatch(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoIPS); +} + +void MainWnd::OnOptionsEmulatorAgbprint() +{ + agbPrintEnable(!agbPrintIsEnabled()); +} + +void MainWnd::OnUpdateOptionsEmulatorAgbprint(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(agbPrintIsEnabled()); +} + +void MainWnd::OnOptionsEmulatorRealtimeclock() +{ + theApp.winRtcEnable = !theApp.winRtcEnable; +} + +void MainWnd::OnUpdateOptionsEmulatorRealtimeclock(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winRtcEnable); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorAutohidemenu() +{ + theApp.autoHideMenu = !theApp.autoHideMenu; +} + +void MainWnd::OnUpdateOptionsEmulatorAutohidemenu(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.autoHideMenu); +} + +void MainWnd::OnOptionsEmulatorRewindinterval() +{ + RewindInterval dlg((float)theApp.rewindTimer/6.0f, theApp.rewindSlots); + int v = dlg.DoModal(); + + if (v >= 0) + { + int interval = v & 0x0000ffff; + int slots = (v & 0xffff0000) >> 16; + + int prevSlots = theApp.rewindSlots; + + theApp.rewindTimer = interval; // already converted to a multiple of 10 frames + theApp.rewindSlots = slots; + if (interval == 0 || slots == 0) + { + theApp.rewindTimer = theApp.rewindSlots = 0; + regSetDwordValue("rewindTimer", interval); + regSetDwordValue("rewindSlots", slots); + if (theApp.rewindMemory) + free(theApp.rewindMemory); + theApp.rewindMemory = NULL; + theApp.rewindCount = 0; + theApp.rewindCounter = 0; + theApp.rewindSaveNeeded = false; + } + else + { + regSetDwordValue("rewindTimer", interval); + regSetDwordValue("rewindSlots", slots); + if (slots != prevSlots) + { + if (theApp.rewindMemory) + free(theApp.rewindMemory); + theApp.rewindMemory = NULL; + theApp.rewindPos = 0; + } + if (theApp.rewindMemory == NULL) + theApp.rewindMemory = (char *)malloc(theApp.rewindSlots*REWIND_SIZE); + theApp.rewindCount = 0; + theApp.rewindSaveNeeded = true; + } + } +} + +BOOL MainWnd::OnOptionsEmulatorShowSpeed(UINT nID) +{ + switch (nID) + { + case ID_OPTIONS_EMULATOR_SHOWSPEED_NONE: + theApp.showSpeed = 0; + systemSetTitle(VBA_NAME_AND_VERSION); + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE: + theApp.showSpeed = 1; + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED: + theApp.showSpeed = 2; + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT: + theApp.showSpeedTransparent = !theApp.showSpeedTransparent; + break; + default: + return FALSE; + } + return TRUE; +} + +void MainWnd::OnUpdateOptionsEmulatorShowSpeed(CCmdUI *pCmdUI) +{ + switch (pCmdUI->m_nID) + { + case ID_OPTIONS_EMULATOR_SHOWSPEED_NONE: + pCmdUI->SetCheck(theApp.showSpeed == 0); + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE: + pCmdUI->SetCheck(theApp.showSpeed == 1); + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED: + pCmdUI->SetCheck(theApp.showSpeed == 2); + break; + case ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT: + pCmdUI->SetCheck(theApp.showSpeedTransparent); + break; + } +} + +void MainWnd::OnOptionsEmulatorSavetypeAutomatic() +{ + theApp.winSaveType = 0; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeAutomatic(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 0); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorSavetypeEeprom() +{ + theApp.winSaveType = 1; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeEeprom(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 1); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorSavetypeSram() +{ + theApp.winSaveType = 2; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeSram(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 2); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorSavetypeFlash() +{ + theApp.winSaveType = 3; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeFlash(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 3); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorSavetypeEepromsensor() +{ + theApp.winSaveType = 4; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeEepromsensor(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 4); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorSavetypeNone() +{ + theApp.winSaveType = 5; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeNone(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winSaveType == 5); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorSavetypeFlash512k() +{ + flashSetSize(0x10000); + theApp.winFlashSize = 0x10000; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeFlash512k(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winFlashSize == 0x10000); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorSavetypeFlash1m() +{ + flashSetSize(0x20000); + theApp.winFlashSize = 0x20000; +} + +void MainWnd::OnUpdateOptionsEmulatorSavetypeFlash1m(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winFlashSize == 0x20000); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorUsebiosfile() +{ + if (!theApp.biosFileName.IsEmpty()) + theApp.useBiosFile = !theApp.useBiosFile; +} + +void MainWnd::OnUpdateOptionsEmulatorUsebiosfile(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.useBiosFile); + pCmdUI->Enable(!theApp.biosFileName.IsEmpty() && (!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL))); +} + +void MainWnd::OnOptionsEmulatorSkipbios() +{ + theApp.skipBiosFile = !theApp.skipBiosFile; +} + +void MainWnd::OnUpdateOptionsEmulatorSkipbios(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.skipBiosFile); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorUseOldGBTiming() +{ + useOldFrameTiming = !useOldFrameTiming; +} + +void MainWnd::OnUpdateOptionsEmulatorUseOldGBTiming(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(useOldFrameTiming); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorUseGBNullInputKludge() +{ + if (VBAMovieActive()) + gbNullInputHackTempEnabled = !gbNullInputHackTempEnabled; + else + gbNullInputHackTempEnabled = gbNullInputHackEnabled = !gbNullInputHackEnabled; +} + +void MainWnd::OnUpdateOptionsEmulatorUseGBNullInputKludge(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(VBAMovieActive() || GetAsyncKeyState(VK_CONTROL) ? gbNullInputHackTempEnabled : gbNullInputHackEnabled); + pCmdUI->Enable((!VBAMovieActive() && !useOldFrameTiming) || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorGBALag() +{ + extern void TogglePrefetchHack(); + TogglePrefetchHack(); + memLagEnabled = memLagTempEnabled; // memLagEnabled is only to hold the last value that the user chose, so temporary changes + // don't get into the registry +} + +void MainWnd::OnUpdateOptionsEmulatorGBALag(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(!memLagTempEnabled); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsEmulatorSelectbiosfile() +{ + theApp.winCheckFullscreen(); + LPCTSTR exts[] = { NULL }; + CString filter = winResLoadFilter(IDS_FILTER_BIOS); + CString title = winResLoadString(IDS_SELECT_BIOS_FILE); + + FileDlg dlg(this, + theApp.biosFileName, + filter, + 0, + "BIOS", + exts, + "", + title, + false); + + if (dlg.DoModal() == IDOK) + { + theApp.biosFileName = dlg.GetPathName(); + } +} + +void MainWnd::OnOptionsEmulatorPngformat() +{ + theApp.captureFormat = 0; +} + +void MainWnd::OnUpdateOptionsEmulatorPngformat(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.captureFormat == 0); +} + +void MainWnd::OnOptionsEmulatorBmpformat() +{ + theApp.captureFormat = 1; +} + +void MainWnd::OnUpdateOptionsEmulatorBmpformat(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.captureFormat == 1); +} + +void MainWnd::OnOptionsSoundDisable() +{ + if (soundOffFlag) + { + soundOffFlag = false; + soundInit(); + } + else + { + soundOffFlag = true; + soundShutdown(); + } +} + +void MainWnd::OnUpdateOptionsSoundDisable(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundOffFlag); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +static void OnSoundToggleEnabled(int c) +{ + if (soundGetEnabledChannels() & c) + { + soundDisableChannels(c); + } + else + { + soundEnableChannels(c); + } +} + +void MainWnd::OnOptionsSoundMute() +{ + if ((soundGetEnabledChannels() & 0x030f) == 0) + soundEnableChannels(0x030f); + else + soundDisableChannels(0x030f); +} + +void MainWnd::OnUpdateOptionsSoundMute(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck((soundGetEnabledChannels() & 0x030f) == 0); +} + +void MainWnd::OnOptionsSoundOff() +{ + soundDisableChannels(0x030f); +} + +void MainWnd::OnUpdateOptionsSoundOff(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck((soundGetEnabledChannels() & 0x030f) == 0); +} + +void MainWnd::OnOptionsSoundOn() +{ + soundEnableChannels(0x030f); +} + +void MainWnd::OnUpdateOptionsSoundOn(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnabledChannels() == 0x030f); +} + +void MainWnd::OnOptionsSoundUseoldsynchronization() +{ + theApp.useOldSync = !theApp.useOldSync; + systemMessage(IDS_SETTING_WILL_BE_EFFECTIVE, + "Setting will be effective the next time you start the emulator"); +} + +void MainWnd::OnUpdateOptionsSoundUseoldsynchronization(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.useOldSync); +} + +void MainWnd::OnOptionsSoundEcho() +{ + soundEcho = !soundEcho; +} + +void MainWnd::OnUpdateOptionsSoundEcho(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundEcho); +} + +void MainWnd::OnOptionsSoundLowpassfilter() +{ + soundLowPass = !soundLowPass; +} + +void MainWnd::OnUpdateOptionsSoundLowpassfilter(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundLowPass); +} + +void MainWnd::OnOptionsSoundReversestereo() +{ + soundReverse = !soundReverse; +} + +void MainWnd::OnUpdateOptionsSoundReversestereo(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundReverse); +} + +void MainWnd::OnOptionsSoundMuteFrameAdvance() +{ + theApp.muteFrameAdvance = !theApp.muteFrameAdvance; +} + +void MainWnd::OnUpdateOptionsSoundMuteFrameAdvance(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.muteFrameAdvance); +} + +void MainWnd::OnOptionsSoundMuteWhenInactive() +{ + theApp.muteWhenInactive = !theApp.muteWhenInactive; +} + +void MainWnd::OnUpdateOptionsSoundMuteWhenInactive(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.muteWhenInactive); +} + +void MainWnd::OnOptionsSound11khz() +{ + if (systemCartridgeType == 0) + soundSetQuality(4); + else + gbSoundSetQuality(4); +} + +void MainWnd::OnUpdateOptionsSound11khz(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundQuality == 4); + pCmdUI->Enable((!VBAMovieActive() || + GetAsyncKeyState(VK_CONTROL)) && !(theApp.soundRecording || theApp.aviRecording || theApp.nvAudioLog)); +} + +void MainWnd::OnOptionsSound22khz() +{ + if (systemCartridgeType == 0) + soundSetQuality(2); + else + gbSoundSetQuality(2); +} + +void MainWnd::OnUpdateOptionsSound22khz(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundQuality == 2); + pCmdUI->Enable((!VBAMovieActive() || + GetAsyncKeyState(VK_CONTROL)) && !(theApp.soundRecording || theApp.aviRecording || theApp.nvAudioLog)); +} + +void MainWnd::OnOptionsSound44khz() +{ + systemSoundSetQuality(1); +} + +void MainWnd::OnUpdateOptionsSound44khz(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundQuality == 1); + pCmdUI->Enable(!(theApp.soundRecording || theApp.aviRecording || theApp.nvAudioLog)); +} + +BOOL MainWnd::OnOptionsSoundVolume(UINT nID) +{ + soundVolume = nID - ID_OPTIONS_SOUND_VOLUME_1X; + return TRUE; +} + +void MainWnd::OnUpdateOptionsSoundVolume(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(soundVolume == (int)(pCmdUI->m_nID - ID_OPTIONS_SOUND_VOLUME_1X)); +} + +void MainWnd::OnOptionsSoundVolume25x() +{ + soundVolume = 4; +} + +void MainWnd::OnUpdateOptionsSoundVolume25x(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundVolume == 4); +} + +void MainWnd::OnOptionsSoundVolume5x() +{ + soundVolume = 5; +} + +void MainWnd::OnUpdateOptionsSoundVolume5x(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundVolume == 5); +} + +void MainWnd::OnOptionsSoundChannel1() +{ + OnSoundToggleEnabled(0x01); +} + +void MainWnd::OnUpdateOptionsSoundChannel1(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnabledChannels() & 0x01); +} + +void MainWnd::OnOptionsSoundChannel2() +{ + OnSoundToggleEnabled(0x02); +} + +void MainWnd::OnUpdateOptionsSoundChannel2(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnabledChannels() & 0x02); +} + +void MainWnd::OnOptionsSoundChannel3() +{ + OnSoundToggleEnabled(0x04); +} + +void MainWnd::OnUpdateOptionsSoundChannel3(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnabledChannels() & 0x04); +} + +void MainWnd::OnOptionsSoundChannel4() +{ + OnSoundToggleEnabled(0x08); +} + +void MainWnd::OnUpdateOptionsSoundChannel4(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnabledChannels() & 0x08); +} + +void MainWnd::OnOptionsSoundDirectsounda() +{ + OnSoundToggleEnabled(0x0100); +} + +void MainWnd::OnUpdateOptionsSoundDirectsounda(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnabledChannels() & 0x0100); + //pCmdUI->Enable(systemCartridgeType == 0); +} + +void MainWnd::OnOptionsSoundDirectsoundb() +{ + OnSoundToggleEnabled(0x0200); +} + +void MainWnd::OnUpdateOptionsSoundDirectsoundb(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(soundGetEnabledChannels() & 0x0200); + //pCmdUI->Enable(systemCartridgeType == 0); +} + +void MainWnd::OnOptionsGameboyBorder() +{ + theApp.winGbBorderOn = !theApp.winGbBorderOn; + gbBorderOn = theApp.winGbBorderOn; + if (emulating && systemCartridgeType == 1 && gbBorderOn) + { + gbSgbRenderBorder(); + } + theApp.updateWindowSize(theApp.videoOption); +} + +void MainWnd::OnUpdateOptionsGameboyBorder(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.winGbBorderOn); +} + +void MainWnd::OnOptionsGameboyPrinter() +{ + theApp.winGbPrinterEnabled = !theApp.winGbPrinterEnabled; + if (theApp.winGbPrinterEnabled) + gbSerialFunction = gbPrinterSend; + else + gbSerialFunction = NULL; +} + +void MainWnd::OnUpdateOptionsGameboyPrinter(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbSerialFunction == gbPrinterSend); +} + +void MainWnd::OnOptionsGameboyBorderAutomatic() +{ + gbBorderAutomatic = !gbBorderAutomatic; + if (emulating && systemCartridgeType == 1 && gbBorderOn) + { + gbSgbRenderBorder(); + theApp.updateWindowSize(theApp.videoOption); + } +} + +void MainWnd::OnUpdateOptionsGameboyBorderAutomatic(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbBorderAutomatic); +} + +void MainWnd::OnOptionsGameboyAutomatic() +{ + gbEmulatorType = 0; +} + +void MainWnd::OnUpdateOptionsGameboyAutomatic(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 0); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsGameboyGba() +{ + gbEmulatorType = 4; +} + +void MainWnd::OnUpdateOptionsGameboyGba(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 4); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsGameboyCgb() +{ + gbEmulatorType = 1; +} + +void MainWnd::OnUpdateOptionsGameboyCgb(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 1); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsGameboySgb() +{ + gbEmulatorType = 2; +} + +void MainWnd::OnUpdateOptionsGameboySgb(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 2); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsGameboySgb2() +{ + gbEmulatorType = 5; +} + +void MainWnd::OnUpdateOptionsGameboySgb2(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 5); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsGameboyGb() +{ + gbEmulatorType = 3; +} + +void MainWnd::OnUpdateOptionsGameboyGb(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbEmulatorType == 3); + pCmdUI->Enable(!VBAMovieActive() || GetAsyncKeyState(VK_CONTROL)); +} + +void MainWnd::OnOptionsGameboyRealcolors() +{ + gbColorOption = 0; +} + +void MainWnd::OnUpdateOptionsGameboyRealcolors(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbColorOption == 0); +} + +void MainWnd::OnOptionsGameboyGameboycolors() +{ + gbColorOption = 1; +} + +void MainWnd::OnUpdateOptionsGameboyGameboycolors(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(gbColorOption == 1); +} + +void MainWnd::OnOptionsGameboyColors() +{ + theApp.winCheckFullscreen(); + GBColorDlg dlg; + if (dlg.DoModal()) + { + gbPaletteOption = dlg.getWhich(); + memcpy(systemGbPalette, dlg.getColors(), 24*sizeof(u16)); + if (emulating && systemCartridgeType == 1) + { + memcpy(gbPalette, &systemGbPalette[dlg.getWhich()*8], 8*sizeof(u16)); + } + } +} + +BOOL MainWnd::OnOptionsFilter(UINT nID) +{ + switch (nID) + { + case ID_OPTIONS_FILTER_NORMAL: + theApp.filterType = 0; + break; + case ID_OPTIONS_FILTER_TVMODE: + theApp.filterType = 1; + break; + case ID_OPTIONS_FILTER_2XSAI: + theApp.filterType = 2; + break; + case ID_OPTIONS_FILTER_SUPER2XSAI: + theApp.filterType = 3; + break; + case ID_OPTIONS_FILTER_SUPEREAGLE: + theApp.filterType = 4; + break; + case ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL: + theApp.filterType = 5; + break; + case ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL: + theApp.filterType = 6; + break; + case ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X: + theApp.filterType = 7; + break; + case ID_OPTIONS_FILTER16BIT_SIMPLE2X: + theApp.filterType = 8; + break; + case ID_OPTIONS_FILTER_BILINEAR: + theApp.filterType = 9; + break; + case ID_OPTIONS_FILTER_BILINEARPLUS: + theApp.filterType = 10; + break; + case ID_OPTIONS_FILTER_SCANLINES: + theApp.filterType = 11; + break; + case ID_OPTIONS_FILTER_HQ2X2: + theApp.filterType = 12; + break; + case ID_OPTIONS_FILTER_HQ2X: + theApp.filterType = 13; + break; + case ID_OPTIONS_FILTER_LQ2X: + theApp.filterType = 14; + break; + case ID_OPTIONS_FILTER_HQ3X2: + theApp.filterType = 15; + break; + case ID_OPTIONS_FILTER_HQ3X: + theApp.filterType = 16; + break; + case ID_OPTIONS_FILTER16BIT_SIMPLE3X: + theApp.filterType = 17; + break; + case ID_OPTIONS_FILTER16BIT_SIMPLE4X: + theApp.filterType = 18; + break; + case ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL3X: + theApp.filterType = 19; + break; + case ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL4X: + theApp.filterType = 20; + break; + default: + return FALSE; + } + theApp.updateFilter(); + return TRUE; +} + +void MainWnd::OnUpdateOptionsFilter(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(systemColorDepth == 16 || systemColorDepth == 32); + switch (pCmdUI->m_nID) + { + case ID_OPTIONS_FILTER_NORMAL: + pCmdUI->SetCheck(theApp.filterType == 0); + break; + case ID_OPTIONS_FILTER_TVMODE: + pCmdUI->SetCheck(theApp.filterType == 1); + break; + case ID_OPTIONS_FILTER_2XSAI: + pCmdUI->SetCheck(theApp.filterType == 2); + break; + case ID_OPTIONS_FILTER_SUPER2XSAI: + pCmdUI->SetCheck(theApp.filterType == 3); + break; + case ID_OPTIONS_FILTER_SUPEREAGLE: + pCmdUI->SetCheck(theApp.filterType == 4); + break; + case ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL: + pCmdUI->SetCheck(theApp.filterType == 5); + break; + case ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL: + pCmdUI->SetCheck(theApp.filterType == 6); + break; + case ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X: + pCmdUI->SetCheck(theApp.filterType == 7); + break; + case ID_OPTIONS_FILTER16BIT_SIMPLE2X: + pCmdUI->SetCheck(theApp.filterType == 8); + break; + case ID_OPTIONS_FILTER_BILINEAR: + pCmdUI->SetCheck(theApp.filterType == 9); + break; + case ID_OPTIONS_FILTER_BILINEARPLUS: + pCmdUI->SetCheck(theApp.filterType == 10); + break; + case ID_OPTIONS_FILTER_SCANLINES: + pCmdUI->SetCheck(theApp.filterType == 11); + break; + case ID_OPTIONS_FILTER_HQ2X2: + pCmdUI->SetCheck(theApp.filterType == 12); + break; + case ID_OPTIONS_FILTER_HQ2X: + pCmdUI->SetCheck(theApp.filterType == 13); + break; + case ID_OPTIONS_FILTER_LQ2X: + pCmdUI->SetCheck(theApp.filterType == 14); + break; + case ID_OPTIONS_FILTER_HQ3X2: + pCmdUI->SetCheck(theApp.filterType == 15); + break; + case ID_OPTIONS_FILTER_HQ3X: + pCmdUI->SetCheck(theApp.filterType == 16); + break; + case ID_OPTIONS_FILTER16BIT_SIMPLE3X: + pCmdUI->SetCheck(theApp.filterType == 17); + break; + case ID_OPTIONS_FILTER16BIT_SIMPLE4X: + pCmdUI->SetCheck(theApp.filterType == 18); + break; + case ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL3X: + pCmdUI->SetCheck(theApp.filterType == 19); + break; + case ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL4X: + pCmdUI->SetCheck(theApp.filterType == 20); + break; + } +} + +BOOL MainWnd::OnOptionsFilterIFB(UINT nID) +{ + switch (nID) + { + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE: + theApp.ifbType = 0; + break; + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR: + theApp.ifbType = 1; + break; + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART: + theApp.ifbType = 2; + break; + default: + return FALSE; + } + theApp.updateIFB(); + return TRUE; +} + +void MainWnd::OnUpdateOptionsFilterIFB(CCmdUI *pCmdUI) +{ + switch (pCmdUI->m_nID) + { + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE: + pCmdUI->SetCheck(theApp.ifbType == 0); + break; + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR: + pCmdUI->SetCheck(theApp.ifbType == 1); + break; + case ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART: + pCmdUI->SetCheck(theApp.ifbType == 2); + break; + } +} + +void MainWnd::OnOptionsFilterDisablemmx() +{ + theApp.disableMMX = !theApp.disableMMX; + if (!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +} + +void MainWnd::OnUpdateOptionsFilterDisablemmx(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.disableMMX); +} + +void MainWnd::OnOptionsLanguageSystem() +{ + theApp.winSetLanguageOption(0, false); +} + +void MainWnd::OnUpdateOptionsLanguageSystem(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.languageOption == 0); +} + +void MainWnd::OnOptionsLanguageEnglish() +{ + theApp.winSetLanguageOption(1, false); +} + +void MainWnd::OnUpdateOptionsLanguageEnglish(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.languageOption == 1); +} + +void MainWnd::OnOptionsLanguageOther() +{ + theApp.winCheckFullscreen(); + theApp.winSetLanguageOption(2, false); +} + +void MainWnd::OnUpdateOptionsLanguageOther(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.languageOption == 2); +} + +void MainWnd::OnOptionsJoypadConfigure1() +{ + theApp.winCheckFullscreen(); + JoypadConfig dlg(0); + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadConfigure1(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +void MainWnd::OnOptionsJoypadConfigure2() +{ + theApp.winCheckFullscreen(); + JoypadConfig dlg(1); + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadConfigure2(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +void MainWnd::OnOptionsJoypadConfigure3() +{ + theApp.winCheckFullscreen(); + JoypadConfig dlg(2); + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadConfigure3(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +void MainWnd::OnOptionsJoypadConfigure4() +{ + theApp.winCheckFullscreen(); + JoypadConfig dlg(3); + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadConfigure4(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +BOOL MainWnd::OnOptionsJoypadDefault(UINT nID) +{ + theApp.joypadDefault = nID - ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1; + return TRUE; +} + +void MainWnd::OnUpdateOptionsJoypadDefault(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.joypadDefault == (int)(pCmdUI->m_nID - ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1)); +} + +void MainWnd::OnOptionsJoypadMotionconfigure() +{ + theApp.winCheckFullscreen(); + MotionConfig dlg; + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsJoypadMotionconfigure(CCmdUI*pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + +void MainWnd::OnOptionsJoypadAllowLeftRight() +{ + theApp.allowLeftRight = !theApp.allowLeftRight; +} + +void MainWnd::OnUpdateOptionsJoypadAllowLeftRight(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.allowLeftRight); +} + +void MainWnd::OnOptionsJoypadAutofireAccountForLag() +{ + theApp.autofireAccountForLag = !theApp.autofireAccountForLag; +} + +void MainWnd::OnUpdateOptionsJoypadAutofireAccountForLag(CCmdUI*pCmdUI) +{ + pCmdUI->SetCheck(theApp.autofireAccountForLag); +} + +BOOL MainWnd::OnOptionsJoypadAutofire(UINT nID) +{ + int & autoFire = (theApp.autoFireToggle ? theApp.autoFire : theApp.autoFire2); + int & autoFire2 = (theApp.autoFireToggle ? theApp.autoFire2 : theApp.autoFire); + int autoFires = (theApp.autoFire | theApp.autoFire2); + + switch (nID) + { + case ID_OPTIONS_JOYPAD_AUTOFIRE_A: + if (autoFires & BUTTON_MASK_A) + { + autoFire &= ~BUTTON_MASK_A; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_A_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_A; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_A)); + } + autoFire2 &= ~BUTTON_MASK_A; + theApp.autoHold &= ~BUTTON_MASK_A; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_B: + if (autoFires & BUTTON_MASK_B) + { + autoFire &= ~BUTTON_MASK_B; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_B_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_B; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_B)); + } + autoFire2 &= ~BUTTON_MASK_B; + theApp.autoHold &= ~BUTTON_MASK_B; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_L: + if (autoFires & BUTTON_MASK_L) + { + autoFire &= ~BUTTON_MASK_L; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_L_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_L; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_L)); + } + autoFire2 &= ~BUTTON_MASK_L; + theApp.autoHold &= ~BUTTON_MASK_L; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_R: + if (autoFires & BUTTON_MASK_R) + { + autoFire &= ~BUTTON_MASK_R; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_R_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_R; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_R)); + } + autoFire2 &= ~BUTTON_MASK_R; + theApp.autoHold &= ~BUTTON_MASK_R; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_START: + if (autoFires & BUTTON_MASK_START) + { + autoFire &= ~BUTTON_MASK_START; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_START_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_START; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_START)); + } + autoFire2 &= ~BUTTON_MASK_START; + theApp.autoHold &= ~BUTTON_MASK_START; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_SELECT: + if (autoFires & BUTTON_MASK_SELECT) + { + autoFire &= ~BUTTON_MASK_SELECT; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_SELECT_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_SELECT; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_SELECT)); + } + autoFire2 &= ~BUTTON_MASK_SELECT; + theApp.autoHold &= ~BUTTON_MASK_SELECT; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_UP: + if (autoFires & BUTTON_MASK_UP) + { + autoFire &= ~BUTTON_MASK_UP; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_UP_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_UP; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_UP)); + } + autoFire2 &= ~BUTTON_MASK_UP; + theApp.autoHold &= ~BUTTON_MASK_UP; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_DOWN: + if (autoFires & BUTTON_MASK_DOWN) + { + autoFire &= ~BUTTON_MASK_DOWN; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_DOWN_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_DOWN; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_DOWN)); + } + autoFire2 &= ~BUTTON_MASK_DOWN; + theApp.autoHold &= ~BUTTON_MASK_DOWN; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_LEFT: + if (autoFires & BUTTON_MASK_LEFT) + { + autoFire &= ~BUTTON_MASK_LEFT; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_LEFT_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_LEFT; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_LEFT)); + } + autoFire2 &= ~BUTTON_MASK_LEFT; + theApp.autoHold &= ~BUTTON_MASK_LEFT; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_RIGHT: + if (autoFires & BUTTON_MASK_RIGHT) + { + autoFire &= ~BUTTON_MASK_RIGHT; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_RIGHT_DISABLED)); + } + else + { + autoFire |= BUTTON_MASK_RIGHT; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_RIGHT)); + } + autoFire2 &= ~BUTTON_MASK_RIGHT; + theApp.autoHold &= ~BUTTON_MASK_RIGHT; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR: + if (autoFires != 0) + { + theApp.autoFire = theApp.autoFire2 = 0; +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_DISABLED)); + } + else + { +/// systemScreenMessage(winResLoadString(IDS_AUTOFIRE_ALREADY_DISABLED)); + systemScreenMessage("already cleared"); + } + break; + default: + return FALSE; + } + + extern void VBAUpdateButtonPressDisplay(); VBAUpdateButtonPressDisplay(); + + return TRUE; +} + +void MainWnd::OnUpdateOptionsJoypadAutofire(CCmdUI *pCmdUI) +{ +/// pCmdUI->Enable(emulating); // FIXME: this is right, but disabling menu items screws up accelerators until you view the +// menu! + pCmdUI->Enable(TRUE); // TEMP + + int autoFires = (theApp.autoFire | theApp.autoFire2); + + bool check = true; + switch (pCmdUI->m_nID) + { + case ID_OPTIONS_JOYPAD_AUTOFIRE_A: + check = (autoFires & BUTTON_MASK_A) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_B: + check = (autoFires & BUTTON_MASK_B) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_L: + check = (autoFires & BUTTON_MASK_L) != 0; +/// extern int gbSgbMode; // from gbSGB.cpp +/// if(emulating && systemCartridgeType != 0 && !gbSgbMode) // regular GB has no L button +/// pCmdUI->Enable(false); // FIXME: this is right, but disabling menu items screws up accelerators until you view the +// menu! + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_R: + check = (autoFires & BUTTON_MASK_R) != 0; +/// extern int gbSgbMode; // from gbSGB.cpp +/// if(emulating && systemCartridgeType != 0 && !gbSgbMode) // regular GB has no R button +/// pCmdUI->Enable(false); // FIXME: this is right, but disabling menu items screws up accelerators until you view the +// menu! + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_START: + check = (autoFires & BUTTON_MASK_START) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_SELECT: + check = (autoFires & BUTTON_MASK_SELECT) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_UP: + check = (autoFires & BUTTON_MASK_UP) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_DOWN: + check = (autoFires & BUTTON_MASK_DOWN) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_LEFT: + check = (autoFires & BUTTON_MASK_LEFT) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_RIGHT: + check = (autoFires & BUTTON_MASK_RIGHT) != 0; + break; + case ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR: + check = (autoFires == 0); +/// pCmdUI->Enable(!check); // FIXME: this is right, but disabling menu items screws up accelerators until you view the menu! + break; + } + pCmdUI->SetCheck(check); +} + +BOOL MainWnd::OnOptionsJoypadSticky(UINT nID) +{ + switch (nID) + { + case ID_STICKY_A: + if (theApp.autoHold & BUTTON_MASK_A) + { + theApp.autoHold &= ~BUTTON_MASK_A; +/// systemScreenMessage(winResLoadString(IDS_STICKY_A_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_A; +/// systemScreenMessage(winResLoadString(IDS_STICKY_A)); + } + theApp.autoFire &= ~BUTTON_MASK_A; + theApp.autoFire2 &= ~BUTTON_MASK_A; + break; + case ID_STICKY_B: + if (theApp.autoHold & BUTTON_MASK_B) + { + theApp.autoHold &= ~BUTTON_MASK_B; +/// systemScreenMessage(winResLoadString(IDS_STICKY_B_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_B; +/// systemScreenMessage(winResLoadString(IDS_STICKY_B)); + } + theApp.autoFire &= ~BUTTON_MASK_B; + theApp.autoFire2 &= ~BUTTON_MASK_B; + break; + case ID_STICKY_L: + if (theApp.autoHold & BUTTON_MASK_L) + { + theApp.autoHold &= ~BUTTON_MASK_L; +/// systemScreenMessage(winResLoadString(IDS_STICKY_L_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_L; +/// systemScreenMessage(winResLoadString(IDS_STICKY_L)); + } + theApp.autoFire &= ~BUTTON_MASK_L; + theApp.autoFire2 &= ~BUTTON_MASK_L; + break; + case ID_STICKY_R: + if (theApp.autoHold & BUTTON_MASK_R) + { + theApp.autoHold &= ~BUTTON_MASK_R; +/// systemScreenMessage(winResLoadString(IDS_STICKY_R_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_R; +/// systemScreenMessage(winResLoadString(IDS_STICKY_R)); + } + theApp.autoFire &= ~BUTTON_MASK_R; + theApp.autoFire2 &= ~BUTTON_MASK_R; + break; + case ID_STICKY_START: + if (theApp.autoHold & BUTTON_MASK_START) + { + theApp.autoHold &= ~BUTTON_MASK_START; +/// systemScreenMessage(winResLoadString(IDS_STICKY_START_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_START; +/// systemScreenMessage(winResLoadString(IDS_STICKY_START)); + } + theApp.autoFire &= ~BUTTON_MASK_START; + theApp.autoFire2 &= ~BUTTON_MASK_START; + break; + case ID_STICKY_SELECT: + if (theApp.autoHold & BUTTON_MASK_SELECT) + { + theApp.autoHold &= ~BUTTON_MASK_SELECT; +/// systemScreenMessage(winResLoadString(IDS_STICKY_SELECT_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_SELECT; +/// systemScreenMessage(winResLoadString(IDS_STICKY_SELECT)); + } + theApp.autoFire &= ~BUTTON_MASK_SELECT; + theApp.autoFire2 &= ~BUTTON_MASK_SELECT; + break; + case ID_STICKY_UP: + if (theApp.autoHold & BUTTON_MASK_UP) + { + theApp.autoHold &= ~BUTTON_MASK_UP; +/// systemScreenMessage(winResLoadString(IDS_STICKY_UP_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_UP; +/// systemScreenMessage(winResLoadString(IDS_STICKY_UP)); + } + theApp.autoFire &= ~BUTTON_MASK_UP; + theApp.autoFire2 &= ~BUTTON_MASK_UP; + if (!theApp.allowLeftRight) + theApp.autoHold &= ~BUTTON_MASK_DOWN; + break; + case ID_STICKY_DOWN: + if (theApp.autoHold & BUTTON_MASK_DOWN) + { + theApp.autoHold &= ~BUTTON_MASK_DOWN; +/// systemScreenMessage(winResLoadString(IDS_STICKY_DOWN_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_DOWN; +/// systemScreenMessage(winResLoadString(IDS_STICKY_DOWN)); + } + theApp.autoFire &= ~BUTTON_MASK_DOWN; + theApp.autoFire2 &= ~BUTTON_MASK_DOWN; + if (!theApp.allowLeftRight) + theApp.autoHold &= ~BUTTON_MASK_UP; + break; + case ID_STICKY_LEFT: + if (theApp.autoHold & BUTTON_MASK_LEFT) + { + theApp.autoHold &= ~BUTTON_MASK_LEFT; +/// systemScreenMessage(winResLoadString(IDS_STICKY_LEFT_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_LEFT; +/// systemScreenMessage(winResLoadString(IDS_STICKY_LEFT)); + } + theApp.autoFire &= ~BUTTON_MASK_LEFT; + theApp.autoFire2 &= ~BUTTON_MASK_LEFT; + if (!theApp.allowLeftRight) + theApp.autoHold &= ~BUTTON_MASK_RIGHT; + break; + case ID_STICKY_RIGHT: + if (theApp.autoHold & BUTTON_MASK_RIGHT) + { + theApp.autoHold &= ~BUTTON_MASK_RIGHT; +/// systemScreenMessage(winResLoadString(IDS_STICKY_RIGHT_DISABLED)); + } + else + { + theApp.autoHold |= BUTTON_MASK_RIGHT; +/// systemScreenMessage(winResLoadString(IDS_STICKY_RIGHT)); + } + theApp.autoFire &= ~BUTTON_MASK_RIGHT; + theApp.autoFire2 &= ~BUTTON_MASK_RIGHT; + if (!theApp.allowLeftRight) + theApp.autoHold &= ~BUTTON_MASK_LEFT; + break; + case ID_STICKY_CLEAR: + if (theApp.autoHold != 0) + { + theApp.autoHold = 0; +/// systemScreenMessage(winResLoadString(IDS_STICKY_DISABLED)); + } + else + { +/// systemScreenMessage(winResLoadString(IDS_STICKY_ALREADY_DISABLED)); + systemScreenMessage("already cleared"); + } + break; + default: + return FALSE; + } + + extern void VBAUpdateButtonPressDisplay(); VBAUpdateButtonPressDisplay(); + + return TRUE; +} + +void MainWnd::OnUpdateOptionsJoypadSticky(CCmdUI *pCmdUI) +{ +/// pCmdUI->Enable(emulating); // FIXME: this is right, but disabling menu items screws up accelerators until you view the +// menu! + pCmdUI->Enable(TRUE); // TEMP + + bool check = true; + switch (pCmdUI->m_nID) + { + case ID_STICKY_A: + check = (theApp.autoHold & BUTTON_MASK_A) != 0; + break; + case ID_STICKY_B: + check = (theApp.autoHold & BUTTON_MASK_B) != 0; + break; + case ID_STICKY_L: + check = (theApp.autoHold & BUTTON_MASK_L) != 0; +/// extern int gbSgbMode; // from gbSGB.cpp +/// if(emulating && systemCartridgeType != 0 && !gbSgbMode) // regular GB has no L button +/// pCmdUI->Enable(false); // FIXME: this is right, but disabling menu items screws up accelerators until you view the +// menu! + break; + case ID_STICKY_R: + check = (theApp.autoHold & BUTTON_MASK_R) != 0; +/// extern int gbSgbMode; // from gbSGB.cpp +/// if(emulating && systemCartridgeType != 0 && !gbSgbMode) // regular GB has no R button +/// pCmdUI->Enable(false); // FIXME: this is right, but disabling menu items screws up accelerators until you view the +// menu! + break; + case ID_STICKY_START: + check = (theApp.autoHold & BUTTON_MASK_START) != 0; + break; + case ID_STICKY_SELECT: + check = (theApp.autoHold & BUTTON_MASK_SELECT) != 0; + break; + case ID_STICKY_UP: + check = (theApp.autoHold & BUTTON_MASK_UP) != 0; + break; + case ID_STICKY_DOWN: + check = (theApp.autoHold & BUTTON_MASK_DOWN) != 0; + break; + case ID_STICKY_LEFT: + check = (theApp.autoHold & BUTTON_MASK_LEFT) != 0; + break; + case ID_STICKY_RIGHT: + check = (theApp.autoHold & BUTTON_MASK_RIGHT) != 0; + break; + case ID_STICKY_CLEAR: + check = (theApp.autoHold == 0); +/// pCmdUI->Enable(!check); // FIXME: this is right, but disabling menu items screws up accelerators until you view the menu! + break; + } + pCmdUI->SetCheck(check); +} + +LRESULT MainWnd::OnConfirmMode(WPARAM, LPARAM) +{ + // we need to do this separately or the window will not have the right + // parent. must be related to the way MFC does modal dialogs + winConfirmMode(); + return 0; +} + +void MainWnd::OnOptionsVideoFullscreenmaxscale() +{ + MaxScale dlg; + + theApp.winCheckFullscreen(); + + dlg.DoModal(); +} + +void MainWnd::OnOptionsVideoTextdisplayoptions() +{ + TextOptions dlg; + + theApp.winCheckFullscreen(); + + dlg.DoModal(); +} + +void MainWnd::OnUpdateOptionsVideoTextdisplayoptions(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(TRUE); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MainWndTools.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MainWndTools.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1000 @@ +#include "stdafx.h" +#include +#include "resource.h" +#include "MainWnd.h" +#include "AccelEditor.h" +#include "AVIWrite.h" +#include "Disassemble.h" +#include "FileDlg.h" +#include "GBDisassemble.h" +#include "GBMapView.h" +#include "GBMemoryViewerDlg.h" +#include "GBOamView.h" +#include "GBPaletteView.h" +#include "GBTileView.h" +#include "GDBConnection.h" +#include "IOViewer.h" +#include "MapView.h" +#include "MemoryViewerDlg.h" +#include "MovieOpen.h" +#include "MovieCreate.h" +#include "OamView.h" +#include "PaletteView.h" +#include "Reg.h" +#include "TileView.h" +#include "WavWriter.h" +#include "WinResUtil.h" +#include "WinMiscUtil.h" +#include "VBA.h" + +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gb/GB.h" + +extern int32 gbBorderOn; +extern int32 soundQuality; + +extern bool debugger; +extern int emulating; +extern int remoteSocket; + +extern void remoteCleanUp(); +extern void remoteSetSockets(SOCKET, SOCKET); +extern void toolsLogging(); + +void MainWnd::OnToolsDisassemble() +{ + if (systemCartridgeType == 0) + { + Disassemble *dlg = new Disassemble(); + dlg->Create(IDD_DISASSEMBLE, this); + dlg->ShowWindow(SW_SHOW); + } + else + { + GBDisassemble *dlg = new GBDisassemble(); + dlg->Create(IDD_GB_DISASSEMBLE, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsDisassemble(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsLogging() +{ + toolsLogging(); +} + +void MainWnd::OnUpdateToolsLogging(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsIoviewer() +{ + IOViewer *dlg = new IOViewer; + dlg->Create(IDD_IO_VIEWER, this); + dlg->ShowWindow(SW_SHOW); +} + +void MainWnd::OnUpdateToolsIoviewer(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && systemCartridgeType == 0); +} + +void MainWnd::OnToolsMapview() +{ + if (systemCartridgeType == 0) + { + MapView *dlg = new MapView; + dlg->Create(IDD_MAP_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } + else + { + GBMapView *dlg = new GBMapView; + dlg->Create(IDD_GB_MAP_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsMapview(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsMemoryviewer() +{ + if (systemCartridgeType == 0) + { + MemoryViewerDlg *dlg = new MemoryViewerDlg; + dlg->Create(IDD_MEM_VIEWER, this); + dlg->ShowWindow(SW_SHOW); + } + else + { + GBMemoryViewerDlg *dlg = new GBMemoryViewerDlg; + dlg->Create(IDD_MEM_VIEWER, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsMemoryviewer(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsOamviewer() +{ + if (systemCartridgeType == 0) + { + OamView *dlg = new OamView; + dlg->Create(IDD_OAM_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } + else + { + GBOamView *dlg = new GBOamView; + dlg->Create(IDD_GB_OAM_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsOamviewer(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsPaletteview() +{ + if (systemCartridgeType == 0) + { + PaletteView *dlg = new PaletteView; + dlg->Create(IDD_PALETTE_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } + else + { + GBPaletteView *dlg = new GBPaletteView; + dlg->Create(IDD_GB_PALETTE_VIEW, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsPaletteview(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnToolsTileviewer() +{ + if (systemCartridgeType == 0) + { + TileView *dlg = new TileView; + dlg->Create(IDD_TILE_VIEWER, this); + dlg->ShowWindow(SW_SHOW); + } + else + { + GBTileView *dlg = new GBTileView; + dlg->Create(IDD_GB_TILE_VIEWER, this); + dlg->ShowWindow(SW_SHOW); + } +} + +void MainWnd::OnUpdateToolsTileviewer(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X); +} + +void MainWnd::OnDebugNextframe() +{ + systemSetPause(false); + theApp.winPauseNextFrame = true; +} + +void MainWnd::OnUpdateDebugNextframe(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnDebugNextframeAccountForLag() +{ + theApp.nextframeAccountForLag = !theApp.nextframeAccountForLag; +} + +void MainWnd::OnUpdateDebugNextframeAccountForLag(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.nextframeAccountForLag); +} + +void MainWnd::OnDebugFramesearch() +{ + extern u16 currentButtons [4]; // from System.cpp + extern SMovie Movie; + if (!theApp.frameSearching) + { + // starting a new search + theApp.frameSearching = true; + theApp.frameSearchStart = (Movie.state == MOVIE_STATE_NONE) ? systemCounters.frameCount : Movie.currentFrame; + theApp.frameSearchLength = 0; + theApp.frameSearchLoadValid = false; + theApp.emulator.emuWriteMemState(&theApp.frameSearchMemory[REWIND_SIZE * 0], REWIND_SIZE); // 0 is start state, 1 is + // intermediate state (for + // speedup when going + // forward), + // 2 is end state + theApp.emulator.emuWriteMemState(&theApp.frameSearchMemory[REWIND_SIZE * 1], REWIND_SIZE); + + // store old buttons from frame before the search + for (int i = 0; i < 4; i++) + theApp.frameSearchOldInput[i] = currentButtons[i]; + } + else + { + // advance forward 1 step in the search + theApp.frameSearchLength++; + + // try it + theApp.emulator.emuReadMemState(&theApp.frameSearchMemory[REWIND_SIZE * 1], REWIND_SIZE); + } + + char str [32]; + sprintf(str, "%d frame search", theApp.frameSearchLength); + systemScreenMessage(str, 0); + + theApp.frameSearchSkipping = true; + + // make sure the display updates at least 1 frame to show the new message + theApp.frameSearchFirstStep = true; + + if (theApp.paused) + theApp.paused = false; +} + +void MainWnd::OnUpdateDebugFramesearch(CCmdUI *pCmdUI) +{ + extern SMovie Movie; + pCmdUI->Enable(emulating && Movie.state != MOVIE_STATE_PLAY); + pCmdUI->SetCheck(theApp.frameSearching); +} + +void MainWnd::OnDebugFramesearchPrev() +{ + if (theApp.frameSearching) + { + if (theApp.frameSearchLength > 0) + { + // rewind 1 step in the search + theApp.frameSearchLength--; + } + + // try it + theApp.emulator.emuReadMemState(&theApp.frameSearchMemory[REWIND_SIZE * 0], REWIND_SIZE); + + char str[32]; + sprintf(str, "%d frame search", theApp.frameSearchLength); + systemScreenMessage(str, 0); + + theApp.frameSearchSkipping = true; + + // make sure the display updates at least 1 frame to show the new message + theApp.frameSearchFirstStep = true; + + if (theApp.paused) + theApp.paused = false; + } +} + +void MainWnd::OnUpdateDebugFramesearchPrev(CCmdUI *pCmdUI) +{ + extern SMovie Movie; + pCmdUI->Enable(emulating && theApp.frameSearching && Movie.state != MOVIE_STATE_PLAY); +} + +void MainWnd::OnDebugFramesearchLoad() +{ + if (theApp.frameSearchLoadValid) + { + theApp.emulator.emuReadMemState(&theApp.frameSearchMemory[REWIND_SIZE * 2], REWIND_SIZE); + theApp.paused = true; + + if (theApp.frameSearching) + systemScreenMessage("end frame search", 0); + else + systemScreenMessage("restore search end", 0); + } + theApp.frameSearching = false; + theApp.frameSearchSkipping = false; +} + +void MainWnd::OnUpdateDebugFramesearchLoad(CCmdUI *pCmdUI) +{ + extern SMovie Movie; + pCmdUI->Enable(emulating && Movie.state != MOVIE_STATE_PLAY); +} + +void MainWnd::OnToolsFrameCounter() +{ + theApp.frameCounter = !theApp.frameCounter; + extern void VBAUpdateFrameCountDisplay(); VBAUpdateFrameCountDisplay(); +} + +void MainWnd::OnUpdateToolsFrameCounter(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.frameCounter); +} + +void MainWnd::OnToolsLagCounter() +{ + theApp.lagCounter = !theApp.lagCounter; + extern void VBAUpdateFrameCountDisplay(); VBAUpdateFrameCountDisplay(); +} + +void MainWnd::OnUpdateToolsLagCounter(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.lagCounter); +} + +void MainWnd::OnToolsExtraCounter() +{ + theApp.extraCounter = !theApp.extraCounter; + extern void VBAUpdateFrameCountDisplay(); VBAUpdateFrameCountDisplay(); +} + +void MainWnd::OnUpdateToolsExtraCounter(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.extraCounter); +} + +void MainWnd::OnToolsExtraCounterReset() +{ + systemCounters.extraCount = systemCounters.frameCount; +} + +void MainWnd::OnToolsInputDisplay() +{ + theApp.inputDisplay = !theApp.inputDisplay; + systemScreenMessage(theApp.inputDisplay ? "Input Display On" : "Input Display Off"); + extern void VBAUpdateButtonPressDisplay(); VBAUpdateButtonPressDisplay(); +} + +void MainWnd::OnUpdateToolsInputDisplay(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.inputDisplay); +} + +void MainWnd::OnToolsDebugGdb() +{ + theApp.winCheckFullscreen(); + GDBPortDlg dlg; + + if (dlg.DoModal()) + { + GDBWaitingDlg wait(dlg.getSocket(), dlg.getPort()); + if (wait.DoModal()) + { + remoteSetSockets(wait.getListenSocket(), wait.getSocket()); + debugger = true; + emulating = 1; + systemCartridgeType = 0; + theApp.gameFilename = "\\gnu_stub"; + rom = (u8 *)malloc(0x2000000 + 4); + workRAM = (u8 *)calloc(1, 0x40000 + 4); + bios = (u8 *)calloc(1, 0x4000 + 4); + internalRAM = (u8 *)calloc(1, 0x8000 + 4); + paletteRAM = (u8 *)calloc(1, 0x400 + 4); + vram = (u8 *)calloc(1, 0x20000 + 4); + oam = (u8 *)calloc(1, 0x400 + 4); + pix = (u8 *)calloc(1, 4 * 240 * 160); + ioMem = (u8 *)calloc(1, 0x400 + 4); + + theApp.emulator = GBASystem; + + CPUInit(); + CPULoadBios(theApp.biosFileName, theApp.useBiosFile ? true : false); + CPUReset(); + } + } +} + +void MainWnd::OnUpdateToolsDebugGdb(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && remoteSocket == -1); +} + +void MainWnd::OnToolsDebugLoadandwait() +{ + theApp.winCheckFullscreen(); + if (winFileOpenSelect(0)) + { + if (winFileRun()) + { + if (systemCartridgeType != 0) + { + systemMessage(IDS_ERROR_NOT_GBA_IMAGE, "Error: not a GBA image"); + OnFileClose(); + return; + } + GDBPortDlg dlg; + + if (dlg.DoModal()) + { + GDBWaitingDlg wait(dlg.getSocket(), dlg.getPort()); + if (wait.DoModal()) + { + remoteSetSockets(wait.getListenSocket(), wait.getSocket()); + debugger = true; + emulating = 1; + } + } + } + } +} + +void MainWnd::OnUpdateToolsDebugLoadandwait(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && remoteSocket == -1); +} + +void MainWnd::OnToolsDebugBreak() +{ + if (armState) + { + armNextPC -= 4; + reg[15].I -= 4; + } + else + { + armNextPC -= 2; + reg[15].I -= 2; + } + debugger = true; +} + +void MainWnd::OnUpdateToolsDebugBreak(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && remoteSocket != -1); +} + +void MainWnd::OnToolsDebugDisconnect() +{ + remoteCleanUp(); + debugger = false; +} + +void MainWnd::OnUpdateToolsDebugDisconnect(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption <= VIDEO_4X && remoteSocket != -1); +} + +void MainWnd::OnToolsSoundRecording() +{ + if (!theApp.soundRecording) + OnToolsSoundStartrecording(); + else + OnToolsSoundStoprecording(); +} + +void MainWnd::OnToolsSoundStartrecording() +{ + theApp.winCheckFullscreen(); + + CString wavName = theApp.gameFilename; + + if (VBAMovieActive()) + { + extern SMovie Movie; + wavName = Movie.filename; + int index = wavName.ReverseFind('.'); + if (index != -1) + wavName = wavName.Left(index); + } + + LPCTSTR exts[] = { ".wav", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_WAV); + CString title = winResLoadString(IDS_SELECT_WAV_NAME); + + wavName = winGetDestFilename(wavName, IDS_WAV_DIR, exts[0]); + CString wavDir = winGetDestDir(IDS_WAV_DIR); + + FileDlg dlg(this, wavName, filter, 1, "WAV", exts, wavDir, title, true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + theApp.soundRecordName = dlg.GetPathName(); + theApp.soundRecording = true; +} + +void MainWnd::OnToolsSoundStoprecording() +{ + if (theApp.soundRecorder) + { + delete theApp.soundRecorder; + theApp.soundRecorder = NULL; + } + theApp.soundRecording = false; +} + +void MainWnd::OnUpdateToolsSoundRecording(CCmdUI *pCmdUI) +{ + if (pCmdUI->m_pMenu != NULL) + { + if (!theApp.soundRecording) + pCmdUI->SetText(winResLoadString(IDS_STARTSOUNDRECORDING)); + else + pCmdUI->SetText(winResLoadString(IDS_STOPSOUNDRECORDING)); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + pCmdUI->Enable(emulating); +} + +void MainWnd::OnToolsAVIRecording() +{ + if (!theApp.aviRecording) + OnToolsStartAVIRecording(); + else + OnToolsStopAVIRecording(); +} + +void MainWnd::OnToolsStartAVIRecording() +{ + theApp.winCheckFullscreen(); + + CString aviName = theApp.gameFilename; + + if (VBAMovieActive()) + { + extern SMovie Movie; + aviName = Movie.filename; + int index = aviName.ReverseFind('.'); + if (index != -1) + aviName = aviName.Left(index); + } + + LPCTSTR exts[] = { ".avi", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_AVI); + CString title = winResLoadString(IDS_SELECT_AVI_NAME); + + aviName = winGetDestFilename(aviName, IDS_AVI_DIR, exts[0]); + CString aviDir = winGetDestDir(IDS_AVI_DIR); + + FileDlg dlg(this, aviName, filter, 1, "AVI", exts, aviDir, title, true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + theApp.aviRecordName = theApp.soundRecordName = dlg.GetPathName(); + theApp.aviRecording = true; + +/// extern long linearFrameCount; linearFrameCount = 0; +/// extern long linearSoundByteCount; linearSoundByteCount = 0; + + if (theApp.aviRecorder == NULL) + { + int width = 240; + int height = 160; + switch (systemCartridgeType) + { + case 0: + width = 240; + height = 160; + break; + case 1: + if (gbBorderOn) + { + width = 256; + height = 224; + } + else + { + width = 160; + height = 144; + } + break; + } + + theApp.aviRecorder = new AVIWrite(); + + theApp.aviRecorder->SetFPS(60); + + BITMAPINFOHEADER bi; + memset(&bi, 0, sizeof(bi)); + bi.biSize = 0x28; + bi.biPlanes = 1; + bi.biBitCount = 24; + bi.biWidth = width; + bi.biHeight = height; + bi.biSizeImage = 3 * width * height; + theApp.aviRecorder->SetVideoFormat(&bi); + if (!theApp.aviRecorder->Open(theApp.aviRecordName)) + { + delete theApp.aviRecorder; + theApp.aviRecorder = NULL; + theApp.aviRecording = false; + } + + if (theApp.aviRecorder) + { + WAVEFORMATEX wfx; + memset(&wfx, 0, sizeof(wfx)); + wfx.wFormatTag = WAVE_FORMAT_PCM; + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100 / soundQuality; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = (wfx.wBitsPerSample / 8) * wfx.nChannels; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + wfx.cbSize = 0; + theApp.aviRecorder->SetSoundFormat(&wfx); + } + } +} + +void MainWnd::OnToolsPauseAVIRecording() +{ + theApp.aviRecorder->Pause(!theApp.aviRecorder->IsPaused()); +} + +void MainWnd::OnToolsStopAVIRecording() +{ + if (theApp.aviRecorder != NULL) + { + delete theApp.aviRecorder; + theApp.aviRecorder = NULL; + } + theApp.aviRecording = false; +} + +void MainWnd::OnUpdateToolsAVIRecording(CCmdUI *pCmdUI) +{ + if (pCmdUI->m_pMenu != NULL) + { + if (!theApp.aviRecording) + pCmdUI->SetText(winResLoadString(IDS_STARTAVIRECORDING)); + else + pCmdUI->SetText(winResLoadString(IDS_STOPAVIRECORDING)); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + } + + pCmdUI->Enable(emulating); +} + +void MainWnd::OnUpdateToolsPauseAVIRecording(CCmdUI *pCmdUI) +{ + if (pCmdUI->m_pMenu != NULL) + { + if (!theApp.aviRecording) + { + pCmdUI->SetText(winResLoadString(IDS_PAUSEAVIRECORDING)); + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + pCmdUI->Enable(false); + } + else + { + if (!theApp.aviRecorder->IsPaused()) + pCmdUI->SetText(winResLoadString(IDS_PAUSEAVIRECORDING)); + else + pCmdUI->SetText(winResLoadString(IDS_RESUMEAVIRECORDING)); + + theApp.winAccelMgr.UpdateMenu(pCmdUI->m_pMenu->GetSafeHmenu()); + pCmdUI->Enable(emulating); + } + } +} + +void MainWnd::OnToolsRecordMovie() +{ + MovieCreate dlg; + dlg.DoModal(); +} + +void MainWnd::OnUpdateToolsRecordMovie(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnToolsStopMovie() +{ + VBAMovieStop(false); +} + +void MainWnd::OnUpdateToolsStopMovie(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating && VBAMovieActive()); +} + +void MainWnd::OnToolsPlayMovie() +{ + MovieOpen dlg; + dlg.DoModal(); +} + +void MainWnd::OnUpdateToolsPlayMovie(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(emulating); +} + +void MainWnd::OnToolsPlayReadOnly() +{ + if (!VBAMovieActive()) + { + theApp.movieReadOnly = !theApp.movieReadOnly; + systemScreenMessage(theApp.movieReadOnly ? "Movie now read-only" : "Movie now editable"); + } + else + VBAMovieToggleReadOnly(); +} + +void MainWnd::OnUpdateToolsPlayReadOnly(CCmdUI *pCmdUI) +{ +/// pCmdUI->Enable(VBAMovieActive()); // FIXME: this is right, but disabling menu items screws up accelerators until you view +// the menu! +/// pCmdUI->SetCheck(VBAMovieReadOnly()); + pCmdUI->Enable(TRUE); // TEMP + pCmdUI->SetCheck(VBAMovieActive() ? VBAMovieReadOnly() : theApp.movieReadOnly); +} + +void MainWnd::OnAsscWithSaveState() +{ + theApp.AsscWithSaveState = !theApp.AsscWithSaveState; +} + +void MainWnd::OnUpdateAsscWithSaveState(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(TRUE); // TEMP + pCmdUI->SetCheck(theApp.AsscWithSaveState); +} + +void MainWnd::OnToolsResumeRecord() +{ + // toggle playing/recording + if (VBAMovieRecording()) + { + if (!VBAMovieSwitchToPlaying()) + systemScreenMessage("Cannot continue playing"); + } + else + { + if (!VBAMovieSwitchToRecording()) + systemScreenMessage("Cannot resume recording now"); + } +} + +void MainWnd::OnUpdateToolsResumeRecord(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(VBAMovieActive()); +} + +void MainWnd::OnToolsPlayRestart() +{ + VBAMovieRestart(); +} + +void MainWnd::OnUpdateToolsPlayRestart(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(VBAMovieActive()); +} + +void MainWnd::OnToolsOnMovieEndPause() +{ + theApp.movieOnEndPause = !theApp.movieOnEndPause; +} + +void MainWnd::OnUpdateToolsOnMovieEndPause(CCmdUI *pCmdUI) +{ + pCmdUI->SetCheck(theApp.movieOnEndPause); +} + +void MainWnd::OnToolsOnMovieEndStop() +{ + theApp.movieOnEndBehavior = 0; +} + +void MainWnd::OnUpdateToolsOnMovieEndStop(CCmdUI *pCmdUI) +{ + pCmdUI->SetRadio(theApp.movieOnEndBehavior == 0); +} + +void MainWnd::OnToolsOnMovieEndRestart() +{ + theApp.movieOnEndBehavior = 1; +} + +void MainWnd::OnUpdateToolsOnMovieEndRestart(CCmdUI *pCmdUI) +{ + pCmdUI->SetRadio(theApp.movieOnEndBehavior == 1); +} + +void MainWnd::OnToolsOnMovieEndAppend() +{ + theApp.movieOnEndBehavior = 2; +} + +void MainWnd::OnUpdateToolsOnMovieEndAppend(CCmdUI *pCmdUI) +{ + pCmdUI->SetRadio(theApp.movieOnEndBehavior == 2); +} + +void MainWnd::OnToolsOnMovieEndKeep() +{ + theApp.movieOnEndBehavior = 3; +} + +void MainWnd::OnUpdateToolsOnMovieEndKeep(CCmdUI *pCmdUI) +{ + pCmdUI->SetRadio(theApp.movieOnEndBehavior == 3); +} + +///////////////////////////////// + +void MainWnd::OnToolsMovieSetPauseAt() +{ + // TODO + VBAMovieSetPauseAt(-1); +} + +void MainWnd::OnUpdateToolsSetMoviePauseAt(CCmdUI *pCmdUI) +{ + // TODO + pCmdUI->SetCheck(VBAMovieGetPauseAt() >= 0); + pCmdUI->Enable(FALSE && VBAMovieActive()); +} + +void MainWnd::OnToolsMovieConvertCurrent() +{ + // temporary + int result = VBAMovieConvertCurrent(); + switch (result) + { + case MOVIE_SUCCESS: + systemScreenMessage("Movie converted"); + break; + case MOVIE_WRONG_VERSION: + systemMessage(0, "Cannot convert from VBM revision %u", VBAMovieGetMinorVersion()); + break; + default: + systemScreenMessage("Nothing to convert"); + break; + } +} + +void MainWnd::OnUpdateToolsMovieConvertCurrent(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(VBAMovieActive()); +} + +void MainWnd::OnToolsMovieAutoConvert() +{ + extern bool autoConvertMovieWhenPlaying; // from movie.cpp + autoConvertMovieWhenPlaying = !autoConvertMovieWhenPlaying; + if (autoConvertMovieWhenPlaying) + { + int result = VBAMovieConvertCurrent(); + switch (result) + { + case MOVIE_SUCCESS: + systemScreenMessage("Movie converted"); + break; + case MOVIE_WRONG_VERSION: + systemMessage(0, "Cannot convert from VBM revision %u", VBAMovieGetMinorVersion()); + break; + default: + systemScreenMessage("Auto movie conversion enabled"); + break; + } + } +} + +void MainWnd::OnUpdateToolsMovieAutoConvert(CCmdUI *pCmdUI) +{ + extern bool autoConvertMovieWhenPlaying; // from movie.cpp + pCmdUI->SetCheck(autoConvertMovieWhenPlaying); +} + +void MainWnd::OnToolsMovieTruncateAtCurrent() +{ + if (VBAMovieReadOnly()) + systemScreenMessage("Cannot truncate movie in this mode"); + else + VBAMovieTuncateAtCurrentFrame(); +} + +void MainWnd::OnUpdateToolsMovieTruncateAtCurrent(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(VBAMovieActive()); +} + +void MainWnd::OnToolsMovieFixHeader() +{ + VBAMovieFixHeader(); +} + +void MainWnd::OnUpdateToolsMovieFixHeader(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(VBAMovieActive()); +} + +// TODO +void MainWnd::OnToolsMovieExtractFromSavegame() +{ + // Currently, snapshots taken from a movie don't contain the initial SRAM or savestate of the movie, + // even if the movie was recorded from either of them. If a snapshot was taken at the first frame + // i.e. Frame 0, it can be safely assumed that the snapshot reflects the initial state of such a movie. + // However, if it was taken after the first frame, the SRAM contained might either be still the same + // as the original (usually true if no write operations on the SRAM occured) or have been modified, + // while the exact original state could hardly, if not impossibly, be safely worked out. + + // TODO +} + +void MainWnd::OnUpdateToolsMovieExtractFromSavegame(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(FALSE); +} + +/////////////////////////////////////////////////////////// + +void MainWnd::OnToolsRewind() +{ + assert(theApp.rewindTimer > 0 && theApp.rewindSlots > 0); + if (emulating && theApp.emulator.emuReadMemState && theApp.rewindMemory && theApp.rewindCount) + { + assert(theApp.rewindPos >= 0 && theApp.rewindPos < theApp.rewindSlots); + theApp.rewindPos = (--theApp.rewindPos + theApp.rewindSlots) % theApp.rewindSlots; + assert(theApp.rewindPos >= 0 && theApp.rewindPos < theApp.rewindSlots); + theApp.emulator.emuReadMemState(&theApp.rewindMemory[REWIND_SIZE * theApp.rewindPos], REWIND_SIZE); + theApp.rewindCount--; + if (theApp.rewindCount > 0) + theApp.rewindCounter = 0; + else + { + theApp.rewindCounter = theApp.rewindTimer; + theApp.rewindSaveNeeded = true; + + // immediately save state to avoid eroding away the earliest you can rewind to + theApp.saveRewindStateIfNecessary(); + + theApp.rewindSaveNeeded = false; + } + } +} + +void MainWnd::OnUpdateToolsRewind(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.rewindMemory != NULL && emulating && theApp.rewindCount); +} + +void MainWnd::OnToolsCustomize() +{ + theApp.recreateMenuBar(); + + AccelEditor dlg(this, &theApp.m_menu, &theApp.winAccelMgr); + dlg.DoModal(); + if (dlg.IsModified()) + { + theApp.winAccelMgr = dlg.GetResultMangager(); + theApp.winAccelMgr.UpdateWndTable(); + theApp.winAccelMgr.Write(); + } + + theApp.winAccelMgr.UpdateMenu(theApp.menu); // we should always do this since the menu has been reloaded +} + +void MainWnd::OnUpdateToolsCustomize(CCmdUI *pCmdUI) +{ + pCmdUI->Enable(theApp.videoOption != VIDEO_320x240); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MapView.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MapView.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1066 @@ +// MapView.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "MapView.h" +#include "FileDlg.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "VBA.h" + +#include "../gba/GBAGlobals.h" +#include "../NLS.h" +#include "../common/Util.h" + +extern "C" { +#include +} + +///////////////////////////////////////////////////////////////////////////// +// MapView dialog + +MapView::MapView(CWnd*pParent /*=NULL*/) + : ResizeDlg(MapView::IDD, pParent) +{ + //{{AFX_DATA_INIT(MapView) + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 1024; + bmpInfo.bmiHeader.biHeight = -1024; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 1024 * 1024); + + mapView.setData(data); + mapView.setBmpInfo(&bmpInfo); + + control = BG0CNT; + + bg = 0; + frame = 0; +} + +MapView::~MapView() +{ + free(data); + data = NULL; +} + +void MapView::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MapView) + DDX_Control(pDX, IDC_NUMCOLORS, m_numcolors); + DDX_Control(pDX, IDC_MODE, m_mode); + DDX_Control(pDX, IDC_OVERFLOW, m_overflow); + DDX_Control(pDX, IDC_MOSAIC, m_mosaic); + DDX_Control(pDX, IDC_PRIORITY, m_priority); + DDX_Control(pDX, IDC_DIM, m_dim); + DDX_Control(pDX, IDC_CHARBASE, m_charbase); + DDX_Control(pDX, IDC_MAPBASE, m_mapbase); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_MAP_VIEW, mapView); + DDX_Control(pDX, IDC_MAP_VIEW_ZOOM, mapViewZoom); + DDX_Control(pDX, IDC_COLOR, color); +} + +BEGIN_MESSAGE_MAP(MapView, CDialog) +//{{AFX_MSG_MAP(MapView) +ON_BN_CLICKED(IDC_REFRESH, OnRefresh) +ON_BN_CLICKED(IDC_FRAME_0, OnFrame0) +ON_BN_CLICKED(IDC_FRAME_1, OnFrame1) +ON_BN_CLICKED(IDC_BG0, OnBg0) +ON_BN_CLICKED(IDC_BG1, OnBg1) +ON_BN_CLICKED(IDC_BG2, OnBg2) +ON_BN_CLICKED(IDC_BG3, OnBg3) +ON_BN_CLICKED(IDC_STRETCH, OnStretch) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_BN_CLICKED(IDC_SAVE, OnSave) +//}}AFX_MSG_MAP +ON_MESSAGE(WM_MAPINFO, OnMapInfo) +ON_MESSAGE(WM_COLINFO, OnColInfo) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// MapView message handlers + +void MapView::renderTextScreen(u16 control) +{ + u16 *palette = (u16 *)paletteRAM; + u8 * charBase = &vram[((control >> 2) & 0x03) * 0x4000]; + u16 *screenBase = (u16 *)&vram[((control >> 8) & 0x1f) * 0x800]; + u8 * bmp = data; + + int sizeX = 256; + int sizeY = 256; + switch ((control >> 14) & 3) + { + case 0: + break; + case 1: + sizeX = 512; + break; + case 2: + sizeY = 512; + break; + case 3: + sizeX = 512; + sizeY = 512; + break; + } + + w = sizeX; + h = sizeY; + + if (control & 0x80) + { + for (int y = 0; y < sizeY; y++) + { + int yy = y & 255; + + if (y == 256 && sizeY > 256) + { + screenBase += 0x400; + if (sizeX > 256) + screenBase += 0x400; + } + u16 *screenSource = screenBase + ((yy>>3)*32); + + for (int x = 0; x < sizeX; x++) + { + u16 data = *screenSource; + + int tile = data & 0x3FF; + int tileX = (x & 7); + int tileY = y & 7; + + if (data & 0x0400) + tileX = 7 - tileX; + if (data & 0x0800) + tileY = 7 - tileY; + + u8 c = charBase[tile * 64 + tileY * 8 + tileX]; + + u16 color = palette[c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + + if (data & 0x0400) + { + if (tileX == 0) + screenSource++; + } + else if (tileX == 7) + screenSource++; + if (x == 255 && sizeX > 256) + { + screenSource = screenBase + 0x400 + ((yy>>3)*32); + } + } + } + } + else + { + for (int y = 0; y < sizeY; y++) + { + int yy = y & 255; + + if (y == 256 && sizeY > 256) + { + screenBase += 0x400; + if (sizeX > 256) + screenBase += 0x400; + } + u16 *screenSource = screenBase + ((yy>>3)*32); + + for (int x = 0; x < sizeX; x++) + { + u16 data = *screenSource; + + int tile = data & 0x3FF; + int tileX = (x & 7); + int tileY = y & 7; + + if (data & 0x0400) + tileX = 7 - tileX; + if (data & 0x0800) + tileY = 7 - tileY; + + u8 color = charBase[tile * 32 + tileY * 4 + (tileX>>1)]; + + if (tileX & 1) + { + color = (color >> 4); + } + else + { + color &= 0x0F; + } + + int pal = (*screenSource>>8) & 0xF0; + u16 color2 = palette[pal + color]; + + *bmp++ = ((color2 >> 10) & 0x1f) << 3; + *bmp++ = ((color2 >> 5) & 0x1f) << 3; + *bmp++ = (color2 & 0x1f) << 3; + + if (data & 0x0400) + { + if (tileX == 0) + screenSource++; + } + else if (tileX == 7) + screenSource++; + + if (x == 255 && sizeX > 256) + { + screenSource = screenBase + 0x400 + ((yy>>3)*32); + } + } + } + } + /* + switch(bg) { + case 0: + renderView(BG0HOFS<<8, BG0VOFS<<8, + 0x100, 0x000, + 0x000, 0x100, + (sizeX -1) <<8, + (sizeY -1) << 8, + true); + break; + case 1: + renderView(BG1HOFS<<8, BG1VOFS<<8, + 0x100, 0x000, + 0x000, 0x100, + (sizeX -1) <<8, + (sizeY -1) << 8, + true); + break; + case 2: + renderView(BG2HOFS<<8, BG2VOFS<<8, + 0x100, 0x000, + 0x000, 0x100, + (sizeX -1) <<8, + (sizeY -1) << 8, + true); + break; + case 3: + renderView(BG3HOFS<<8, BG3VOFS<<8, + 0x100, 0x000, + 0x000, 0x100, + (sizeX -1) <<8, + (sizeY -1) << 8, + true); + break; + } + */ +} + +void MapView::renderRotScreen(u16 control) +{ + u16 *palette = (u16 *)paletteRAM; + u8 * charBase = &vram[((control >> 2) & 0x03) * 0x4000]; + u8 * screenBase = (u8 *)&vram[((control >> 8) & 0x1f) * 0x800]; + u8 * bmp = data; + + int sizeX = 128; + int sizeY = 128; + switch ((control >> 14) & 3) + { + case 0: + break; + case 1: + sizeX = sizeY = 256; + break; + case 2: + sizeX = sizeY = 512; + break; + case 3: + sizeX = sizeY = 1024; + break; + } + + w = sizeX; + h = sizeY; + + if (control & 0x80) + { + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + int tile = screenBase[(x>>3) + (y>>3)*(w>>3)]; + + int tileX = (x & 7); + int tileY = y & 7; + + u8 color = charBase[tile * 64 + tileY * 8 + tileX]; + u16 color2 = palette[color]; + + *bmp++ = ((color2 >> 10) & 0x1f) << 3; + *bmp++ = ((color2 >> 5) & 0x1f) << 3; + *bmp++ = (color2 & 0x1f) << 3; + } + } + } + else + { + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + int tile = screenBase[(x>>3) + (y>>3)*(w>>3)]; + + int tileX = (x & 7); + int tileY = y & 7; + + u8 color = charBase[tile * 64 + tileY * 8 + tileX]; + u16 color2 = palette[color]; + + *bmp++ = ((color2 >> 10) & 0x1f) << 3; + *bmp++ = ((color2 >> 5) & 0x1f) << 3; + *bmp++ = (color2 & 0x1f) << 3; + } + } + } + + u32 xx; + u32 yy; + + switch (bg) + { + case 2: + xx = BG2X_L | BG2X_H << 16; + yy = BG2Y_L | BG2Y_H << 16; + + /* + renderView(xx, yy, + BG2PA, BG2PC, + BG2PB, BG2PD, + (sizeX -1) <<8, + (sizeY -1) << 8, + (control & 0x2000) != 0); + */ + break; + case 3: + xx = BG3X_L | BG3X_H << 16; + yy = BG3Y_L | BG3Y_H << 16; + /* + renderView(xx, yy, + BG3PA, BG3PC, + BG3PB, BG3PD, + (sizeX -1) <<8, + (sizeY -1) << 8, + (control & 0x2000) != 0); + */ + break; + } +} + +void MapView::renderMode0() +{ + renderTextScreen(control); +} + +void MapView::renderMode1() +{ + switch (bg) + { + case 0: + case 1: + renderTextScreen(control); + break; + case 2: + renderRotScreen(control); + break; + default: + bg = 0; + control = BG0CNT; + renderTextScreen(control); + break; + } +} + +void MapView::renderMode2() +{ + switch (bg) + { + case 2: + case 3: + renderRotScreen(control); + break; + default: + bg = 2; + control = BG2CNT; + renderRotScreen(control); + break; + } +} + +void MapView::renderMode3() +{ + u8 * bmp = data; + u16 *src = (u16 *)&vram[0]; + + w = 240; + h = 160; + + for (int y = 0; y < 160; y++) + { + for (int x = 0; x < 240; x++) + { + u16 data = *src++; + *bmp++ = ((data >> 10) & 0x1f) << 3; + *bmp++ = ((data >> 5) & 0x1f) << 3; + *bmp++ = (data & 0x1f) << 3; + } + } + bg = 2; +} + +void MapView::renderMode4() +{ + u8 * bmp = data; + u8 * src = frame ? &vram[0xa000] : &vram[0]; + u16 *pal = (u16 *)&paletteRAM[0]; + + w = 240; + h = 160; + + for (int y = 0; y < 160; y++) + { + for (int x = 0; x < 240; x++) + { + u8 c = *src++; + u16 data = pal[c]; + *bmp++ = ((data >> 10) & 0x1f) << 3; + *bmp++ = ((data >> 5) & 0x1f) << 3; + *bmp++ = (data & 0x1f) << 3; + } + } + bg = 2; +} + +void MapView::renderMode5() +{ + u8 * bmp = data; + u16 *src = (u16 *)(frame ? &vram[0xa000] : &vram[0]); + + w = 160; + h = 128; + + for (int y = 0; y < 128; y++) + { + for (int x = 0; x < 160; x++) + { + u16 data = *src++; + *bmp++ = ((data >> 10) & 0x1f) << 3; + *bmp++ = ((data >> 5) & 0x1f) << 3; + *bmp++ = (data & 0x1f) << 3; + } + } + bg = 2; +} + +void MapView::OnRefresh() +{ + paint(); +} + +void MapView::paint() +{ + if (vram == NULL) + return; + int mode = DISPCNT & 7; + + switch (bg) + { + default: + case 0: + control = BG0CNT; + break; + case 1: + control = BG1CNT; + break; + case 2: + control = BG2CNT; + break; + case 3: + control = BG3CNT; + break; + } + + switch (mode) + { + case 0: + renderMode0(); + break; + case 1: + renderMode1(); + break; + case 2: + renderMode2(); + break; + case 3: + renderMode3(); + break; + case 4: + renderMode4(); + break; + case 5: + renderMode5(); + break; + } + enableButtons(mode); + SIZE s; + + if (mapView.getStretch()) + { + mapView.setSize(w, h); + s.cx = s.cy = 1; + mapView.SetScrollSizes(MM_TEXT, s); + } + else + { + mapView.setSize(w, h); + s.cx = w; + s.cy = h; + mapView.SetScrollSizes(MM_TEXT, s); + } + + mapView.refresh(); + + CString buffer; + + u32 charBase = ((control >> 2) & 0x03) * 0x4000 + 0x6000000; + u32 screenBase = ((control >> 8) & 0x1f) * 0x800 + 0x6000000; + + buffer.Format("%d", mode); + m_mode.SetWindowText(buffer); + + if (mode >= 3) + { + m_mapbase.SetWindowText(""); + m_charbase.SetWindowText(""); + } + else + { + buffer.Format("0x%08X", screenBase); + m_mapbase.SetWindowText(buffer); + + buffer.Format("0x%08X", charBase); + m_charbase.SetWindowText(buffer); + } + + buffer.Format("%dx%d", w, h); + m_dim.SetWindowText(buffer); + + m_numcolors.SetWindowText(control & 0x80 ? "256" : "16"); + + buffer.Format("%d", control & 3); + m_priority.SetWindowText(buffer); + + m_mosaic.SetWindowText(control & 0x40 ? "1" : "0"); + + m_overflow.SetWindowText(bg <= 1 ? "" : + control & 0x2000 ? "1" : "0"); +} + +BOOL MapView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_MAP_VIEW, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_B, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\MapView", + NULL); + + SIZE size; + size.cx = 1; + size.cy = 1; + mapView.SetScrollSizes(MM_TEXT, size); + int s = regQueryDwordValue("mapViewStretch", 0); + if (s) + mapView.setStretch(true); + ((CButton *)GetDlgItem(IDC_STRETCH))->SetCheck(s); + paint(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MapView::PostNcDestroy() +{ + delete this; +} + +void MapView::enableButtons(int mode) +{ + bool enable[6] = { true, true, true, true, true, true }; + + switch (mode) + { + case 0: + enable[4] = false; + enable[5] = false; + break; + case 1: + enable[3] = false; + enable[4] = false; + enable[5] = false; + break; + case 2: + enable[0] = false; + enable[1] = false; + enable[4] = false; + enable[5] = false; + break; + case 3: + enable[0] = false; + enable[1] = false; + enable[2] = false; + enable[3] = false; + enable[4] = false; + enable[5] = false; + break; + case 4: + enable[0] = false; + enable[1] = false; + enable[2] = false; + enable[3] = false; + break; + case 5: + enable[0] = false; + enable[1] = false; + enable[2] = false; + enable[3] = false; + break; + } + GetDlgItem(IDC_BG0)->EnableWindow(enable[0]); + GetDlgItem(IDC_BG1)->EnableWindow(enable[1]); + GetDlgItem(IDC_BG2)->EnableWindow(enable[2]); + GetDlgItem(IDC_BG3)->EnableWindow(enable[3]); + GetDlgItem(IDC_FRAME_0)->EnableWindow(enable[4]); + GetDlgItem(IDC_FRAME_1)->EnableWindow(enable[5]); + int id = IDC_BG0; + switch (bg) + { + case 1: + id = IDC_BG1; + break; + case 2: + id = IDC_BG2; + break; + case 3: + id = IDC_BG3; + break; + } + CheckRadioButton(IDC_BG0, IDC_BG3, id); + id = IDC_FRAME_0; + if (frame != 0) + id = IDC_FRAME_1; + CheckRadioButton(IDC_FRAME_0, IDC_FRAME_1, id); +} + +void MapView::OnFrame0() +{ + frame = 0; + paint(); +} + +void MapView::OnFrame1() +{ + frame = 1; + paint(); +} + +void MapView::OnBg0() +{ + bg = 0; + control = BG0CNT; + paint(); +} + +void MapView::OnBg1() +{ + bg = 1; + control = BG1CNT; + paint(); +} + +void MapView::OnBg2() +{ + bg = 2; + control = BG2CNT; + paint(); +} + +void MapView::OnBg3() +{ + bg = 3; + control = BG3CNT; + paint(); +} + +void MapView::OnStretch() +{ + mapView.setStretch(!mapView.getStretch()); + paint(); + regSetDwordValue("mapViewStretch", mapView.getStretch()); +} + +void MapView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void MapView::update() +{ + paint(); +} + +void MapView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +u32 MapView::GetTextClickAddress(u32 base, int x, int y) +{ + if (y > 255 && h > 256) + { + base += 0x800; + if (w > 256) + base += 0x800; + } + if (x >= 256) + base += 0x800; + x &= 255; + y &= 255; + base += (x>>3)*2 + 64*(y>>3); + + return base; +} + +u32 MapView::GetClickAddress(int x, int y) +{ + int mode = DISPCNT & 7; + + u32 base = ((control >> 8) & 0x1f) * 0x800 + 0x6000000; + + // all text bgs (16 bits) + if (mode == 0 || (mode < 3 && bg < 2)) + { + return GetTextClickAddress(base, x, y); + } + // rot bgs (8 bits) + if (mode < 3) + { + return base + (x>>3) + (w>>3)*(y>>3); + } + // mode 3/5 (16 bits) + if (mode != 4) + { + return 0x6000000 + 0xa000*frame + 2*x + w*y*2; + } + // mode 4 (8 bits) + return 0x6000000 + 0xa000*frame + x + w*y; +} + +LRESULT MapView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + mapViewZoom.setColors(colors); + + int x = wParam & 0xffff; + int y = (wParam >> 16); + + CString buffer; + buffer.Format("(%d,%d)", x, y); + GetDlgItem(IDC_XY)->SetWindowText(buffer); + + u32 address = GetClickAddress(x, y); + buffer.Format("0x%08X", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + int mode = DISPCNT & 7; + if (mode >= 3) + { + // bitmap modes + GetDlgItem(IDC_TILE_NUM)->SetWindowText("---"); + GetDlgItem(IDC_FLIP)->SetWindowText("--"); + GetDlgItem(IDC_PALETTE_NUM)->SetWindowText("---"); + } + else if (mode == 0 || bg < 2) + { + // text bgs + u16 value = *((u16 *)&vram[address - 0x6000000]); + + int tile = value & 1023; + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE_NUM)->SetWindowText(buffer); + buffer.Empty(); + buffer += value & 1024 ? 'H' : '-'; + buffer += value & 2048 ? 'V' : '-'; + GetDlgItem(IDC_FLIP)->SetWindowText(buffer); + + if (!(control & 0x80)) + { + buffer.Format("%d", (value >> 12) & 15); + } + else + buffer = "---"; + GetDlgItem(IDC_PALETTE_NUM)->SetWindowText(buffer); + } + else + { + // rot bgs + GetDlgItem(IDC_TILE_NUM)->SetWindowText("---"); + GetDlgItem(IDC_FLIP)->SetWindowText("--"); + GetDlgItem(IDC_PALETTE_NUM)->SetWindowText("---"); + } + + return TRUE; +} + +LRESULT MapView::OnColInfo(WPARAM wParam, LPARAM lParam) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void MapView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct + { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + +void MapView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if (!png_ptr) + { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + if (setjmp(png_ptr->jmpbuf)) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr, fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void MapView::OnSave() +{ + CString filename; + + if (theApp.captureFormat == 0) + filename = "map.png"; + else + filename = "map.bmp"; + + LPCTSTR exts[] = {".png", ".bmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + filename, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + if (dlg.getFilterIndex() == 2) + saveBMP(dlg.GetPathName()); + else + savePNG(dlg.GetPathName()); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MapView.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MapView.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,101 @@ +#if !defined(AFX_MAPVIEW_H__20F40C77_8E10_44B7_BB49_7865F73C3E75__INCLUDED_) +#define AFX_MAPVIEW_H__20F40C77_8E10_44B7_BB49_7865F73C3E75__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// MapView.h : header file +// + +#include "BitmapControl.h" +#include "ColorControl.h" +#include "ZoomControl.h" +#include "ResizeDlg.h" +#include "IUpdate.h" + +///////////////////////////////////////////////////////////////////////////// +// MapView dialog + +class MapView : public ResizeDlg, IUpdateListener +{ +private: + BITMAPINFO bmpInfo; + u8 * data; + int frame; + u16 control; + int bg; + int w; + int h; + BitmapControl mapView; + ZoomControl mapViewZoom; + ColorControl color; + bool autoUpdate; + + // Construction +public: + void savePNG(const char *name); + void saveBMP(const char *name); + afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + u32 GetClickAddress(int x, int y); + u32 GetTextClickAddress(u32 base, int x, int y); + void update(); + void enableButtons(int mode); + void paint(); + void renderMode5(); + void renderMode4(); + void renderMode3(); + void renderMode2(); + void renderMode1(); + void renderMode0(); + void renderRotScreen(u16 control); + void renderTextScreen(u16 control); + MapView(CWnd*pParent = NULL); // standard constructor + ~MapView(); + + // Dialog Data + //{{AFX_DATA(MapView) + enum { IDD = IDD_MAP_VIEW }; + CStatic m_numcolors; + CStatic m_mode; + CStatic m_overflow; + CStatic m_mosaic; + CStatic m_priority; + CStatic m_dim; + CStatic m_charbase; + CStatic m_mapbase; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MapView) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(MapView) + afx_msg void OnRefresh(); + virtual BOOL OnInitDialog(); + afx_msg void OnFrame0(); + afx_msg void OnFrame1(); + afx_msg void OnBg0(); + afx_msg void OnBg1(); + afx_msg void OnBg2(); + afx_msg void OnBg3(); + afx_msg void OnStretch(); + afx_msg void OnAutoUpdate(); + afx_msg void OnClose(); + afx_msg void OnSave(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAPVIEW_H__20F40C77_8E10_44B7_BB49_7865F73C3E75__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MaxScale.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MaxScale.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,64 @@ +// MaxScale.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "MaxScale.h" +#include "VBA.h" + +///////////////////////////////////////////////////////////////////////////// +// MaxScale dialog + +MaxScale::MaxScale(CWnd*pParent /*=NULL*/) + : CDialog(MaxScale::IDD, pParent) +{ + //{{AFX_DATA_INIT(MaxScale) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void MaxScale::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MaxScale) + DDX_Control(pDX, IDC_VALUE, m_value); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(MaxScale, CDialog) +//{{AFX_MSG_MAP(MaxScale) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// MaxScale message handlers + +void MaxScale::OnCancel() +{ + EndDialog(FALSE); +} + +void MaxScale::OnOk() +{ + CString tmp; + m_value.GetWindowText(tmp); + theApp.fsMaxScale = atoi(tmp); + EndDialog(TRUE); +} + +BOOL MaxScale::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString temp; + + temp.Format("%d", theApp.fsMaxScale); + + m_value.SetWindowText(temp); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MaxScale.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MaxScale.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,47 @@ +#if !defined(AFX_MAXSCALE_H__3F42C0CC_DD5E_4A96_A60D_33AB7CBDE406__INCLUDED_) +#define AFX_MAXSCALE_H__3F42C0CC_DD5E_4A96_A60D_33AB7CBDE406__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// MaxScale.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// MaxScale dialog + +class MaxScale : public CDialog +{ +// Construction +public: + MaxScale(CWnd*pParent = NULL); // standard constructor + +// Dialog Data + //{{AFX_DATA(MaxScale) + enum { IDD = IDD_MAX_SCALE }; + CEdit m_value; + //}}AFX_DATA + +// Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MaxScale) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +// Implementation +protected: + // Generated message map functions + //{{AFX_MSG(MaxScale) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MAXSCALE_H__3F42C0CC_DD5E_4A96_A60D_33AB7CBDE406__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MemoryViewer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MemoryViewer.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,688 @@ +// MemoryViewer.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "MemoryViewer.h" + +extern int emulating; + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewer + +bool MemoryViewer::isRegistered = false; + +MemoryViewer::MemoryViewer() +{ + address = 0; + addressSize = 0; + dataSize = 0; + editAddress = 0; + editNibble = 0; + displayedLines = 0; + hasCaret = false; + maxNibble = 0; + font = (HFONT)GetStockObject(SYSTEM_FIXED_FONT); + fontSize.cx = fontSize.cy = 0; + beginAscii = 0; + beginHex = 0; + dlg = NULL; + registerClass(); +} + +MemoryViewer::~MemoryViewer() +{} + +BEGIN_MESSAGE_MAP(MemoryViewer, CWnd) +//{{AFX_MSG_MAP(MemoryViewer) +ON_WM_ERASEBKGND() +ON_WM_PAINT() +ON_WM_VSCROLL() +ON_WM_GETDLGCODE() +ON_WM_LBUTTONDOWN() +ON_WM_SETFOCUS() +ON_WM_KILLFOCUS() +ON_WM_KEYDOWN() +//}}AFX_MSG_MAP +ON_MESSAGE(WM_CHAR, OnWMChar) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewer message handlers + +void MemoryViewer::setDialog(IMemoryViewerDlg *d) +{ + dlg = d; +} + +void MemoryViewer::setAddress(u32 a) +{ + address = a; + if (displayedLines) + { + if (addressSize) + { + u16 addr = address; + if ((u16)((addr+(displayedLines<<4)) & 0xFFFF) < addr) + { + address = 0xffff - (displayedLines<<4) + 1; + } + } + else + { + if ((address+(displayedLines<<4)) < address) + { + address = 0xffffffff - (displayedLines<<4) + 1; + } + } + } + if (addressSize) + address &= 0xffff; + setCaretPos(); + InvalidateRect(NULL, TRUE); +} + +void MemoryViewer::setSize(int s) +{ + dataSize = s; + if (s == 0) + maxNibble = 1; + else if (s == 1) + maxNibble = 3; + else + maxNibble = 7; + + InvalidateRect(NULL, TRUE); +} + +void MemoryViewer::setDecimal(bool decimalDisplayMode) +{ + decimalDisplay = decimalDisplayMode; + InvalidateRect(NULL, TRUE); +} + +BOOL MemoryViewer::OnEraseBkgnd(CDC*pDC) +{ + return TRUE; +} + +void MemoryViewer::updateScrollInfo(int lines) +{ + int page = lines * 16; + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL | SIF_POS; + si.nMin = 0; + if (addressSize) + { + si.nMax = 0x10000/page; + si.nPage = 1; + } + else + { + si.nMax = 0xa000000 / page; + si.nPage = page; + } + + si.nPos = address / page; + SetScrollInfo(SB_VERT, + &si, + TRUE); +} + +void MemoryViewer::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + RECT rect; + GetClientRect(&rect); + int w = rect.right - rect.left; + int h = rect.bottom - rect.top - 6; + + CDC memDC; + memDC.CreateCompatibleDC(&dc); + CBitmap bitmap, *pOldBitmap; + bitmap.CreateCompatibleBitmap(&dc, w, rect.bottom - rect.top); + pOldBitmap = memDC.SelectObject(&bitmap); + + memDC.FillRect(&rect, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH))); + memDC.DrawEdge(&rect, EDGE_ETCHED, BF_RECT); + + CFont *oldFont = memDC.SelectObject(CFont::FromHandle(font)); + + fontSize = memDC.GetTextExtent("0", 1); + + int lines = h / fontSize.cy; + + displayedLines = lines; + + updateScrollInfo(lines); + + u32 addr = address; + + memDC.SetTextColor(RGB(0, 0, 0)); + + u8 data[32]; + + RECT r; + r.top = 3; + r.left = 3; + r.bottom = r.top+fontSize.cy; + r.right = rect.right-3; + + int line = 0; + + for (int i = 0; i < lines; i++) + { + CString buffer; + if (addressSize) + buffer.Format("%04X", addr); + else + buffer.Format("%08X", addr); + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + r.left += 10*fontSize.cx; + beginHex = r.left; + readData(addr, 16, data); + + int j; + + if (dataSize == 0) + { + for (j = 0; j < 16; j++) + { + const int nextRLeft = r.left + 3*fontSize.cx; + if (!decimalDisplay) + buffer.Format("%02X", data[j]); + else + { + const signed char num = data[j]; + if (num < -9 || num > 99) + r.left -= fontSize.cx; + if (num >= -99 && num <= 99) + buffer.Format("%2d", num); + else + buffer.Format(""); + } + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + r.left = nextRLeft; + } + } + if (dataSize == 1) + { + for (j = 0; j < 16; j += 2) + { + const int nextRLeft = r.left + 5*fontSize.cx; + if (!decimalDisplay) + buffer.Format("%04X", data[j] | data[j+1]<<8); + else + { + const signed short num = data[j] | data[j+1]<<8; + if (num < -999 || num > 9999) + r.left -= fontSize.cx; + if (num >= -9999 && num <= 9999) + buffer.Format("%4d", num); + else + buffer.Format(""); + } + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + r.left = nextRLeft; + } + } + if (dataSize == 2) + { + for (j = 0; j < 16; j += 4) + { + if (!decimalDisplay) + buffer.Format("%08X", data[j] | data[j+1]<<8 | data[j+2] << 16 | data[j+3] << 24); + else + { + const signed long num = data[j] | data[j+1]<<8 | data[j+2] << 16 | data[j+3] << 24; + if (num >= -9999999 && num <= 99999999) + buffer.Format("%8d", num); + else + buffer.Format(""); + } + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + r.left += 9*fontSize.cx; + } + } + + line = r.left; + + r.left += fontSize.cx; + beginAscii = r.left; + buffer.Empty(); + for (j = 0; j < 16; j++) + { + char c = data[j]; + if (c >= 32 && c <= 127) + { + buffer += c; + } + else + buffer += '.'; + } + + memDC.DrawText(buffer, &r, DT_TOP | DT_LEFT | DT_NOPREFIX); + addr += 16; + if (addressSize) + addr &= 0xffff; + r.top += fontSize.cy; + r.bottom += fontSize.cy; + r.left = 3; + } + CPen pen; + pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); + CPen *old = memDC.SelectObject(&pen); + + memDC.MoveTo(3+fontSize.cx*9, 3); + memDC.LineTo(3+fontSize.cx*9, 3+displayedLines*fontSize.cy); + + memDC.MoveTo(line, 3); + memDC.LineTo(line, 3+displayedLines*fontSize.cy); + + memDC.SelectObject(old); + pen.DeleteObject(); + + memDC.SelectObject(oldFont); + + dc.BitBlt(0, 0, w, rect.bottom - rect.top, &memDC, 0, 0, SRCCOPY); + + memDC.SelectObject(pOldBitmap); + memDC.DeleteDC(); + bitmap.DeleteObject(); +} + +void MemoryViewer::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar) +{ + int address = this->address; + switch (nSBCode) + { + case SB_BOTTOM: + address = 0xffffff00; + break; + case SB_LINEDOWN: + address += 0x10; + break; + case SB_LINEUP: + address -= 0x10; + break; + case SB_PAGEDOWN: + address += (displayedLines<<4); + break; + case SB_PAGEUP: + address -= (displayedLines<<4); + break; + case SB_TOP: + address = 0; + break; + case SB_THUMBTRACK: + { + int page = displayedLines * 16; + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_TRACKPOS; + GetScrollInfo(SB_VERT, &si); + address = page * si.nTrackPos; + break; + } + } + setAddress(address); +} + +UINT MemoryViewer::OnGetDlgCode() +{ + return DLGC_WANTALLKEYS; +} + +void MemoryViewer::createEditCaret(int w, int h) +{ + if (!hasCaret || caretWidth != w || caretHeight != h) + { + hasCaret = true; + caretWidth = w; + caretHeight = h; + ::CreateCaret(m_hWnd, (HBITMAP)0, w, h); + } +} + +void MemoryViewer::destroyEditCaret() +{ + hasCaret = false; + DestroyCaret(); +} + +void MemoryViewer::setCaretPos() +{ + if (GetFocus() != this) + { + destroyEditCaret(); + return; + } + + if (dlg) + dlg->setCurrentAddress(editAddress); + + if (editAddress < address || editAddress > (address -1 + (displayedLines<<4))) + { + destroyEditCaret(); + return; + } + + int subAddress = (editAddress - address); + + int x = 3+10*fontSize.cx+editNibble*fontSize.cx; + int y = 3+fontSize.cy*((editAddress-address)>>4); + + if (editAscii) + { + x = beginAscii + fontSize.cx*(subAddress&15); + } + else + { + switch (dataSize) + { + case 0: + x += 3*fontSize.cx*(subAddress & 15); + break; + case 1: + x += 5*fontSize.cx*((subAddress>>1) & 7); + break; + case 2: + x += 9*fontSize.cx*((subAddress>>2) & 3); + break; + } + } + + RECT r; + GetClientRect(&r); + r.right -= 3; + if (x >= r.right) + { + destroyEditCaret(); + return; + } + int w = fontSize.cx; + if ((x+fontSize.cx) >= r.right) + w = r.right - x; + createEditCaret(w, fontSize.cy); + ::SetCaretPos(x, y); + ShowCaret(); +} + +void MemoryViewer::OnLButtonDown(UINT nFlags, CPoint point) +{ + int x = point.x; + int y = point.y; + int line = (y-3)/fontSize.cy; + int beforeAscii = beginHex; + int inc = 1; + int sub = 3*fontSize.cx; + switch (dataSize) + { + case 0: + beforeAscii += 47*fontSize.cx; + break; + case 1: + beforeAscii += 39*fontSize.cx; + inc = 2; + sub = 5*fontSize.cx; + break; + case 2: + beforeAscii += 35*fontSize.cx; + inc = 4; + sub = 9*fontSize.cx; + break; + } + + editAddress = address + (line<<4); + if (x >= beginHex && x < beforeAscii) + { + x -= beginHex; + editNibble = 0; + while (x > 0) + { + x -= sub; + if (x >= 0) + editAddress += inc; + else + { + editNibble = (x + sub)/fontSize.cx; + } + } + editAscii = false; + } + else if (x >= beginAscii) + { + int afterAscii = beginAscii+16*fontSize.cx; + if (x >= afterAscii) + x = afterAscii-1; + editAddress += (x-beginAscii)/fontSize.cx; + editNibble = 0; + editAscii = true; + } + else + { + return; + } + + if (editNibble > maxNibble) + editNibble = maxNibble; + SetFocus(); + setCaretPos(); +} + +void MemoryViewer::OnSetFocus(CWnd*pOldWnd) +{ + setCaretPos(); + InvalidateRect(NULL, TRUE); +} + +void MemoryViewer::OnKillFocus(CWnd*pNewWnd) +{ + destroyEditCaret(); + InvalidateRect(NULL, TRUE); +} + +void MemoryViewer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) +{ + bool isShift = (GetKeyState(VK_SHIFT) & 0x80000000) == 0x80000000; + + switch (nChar) + { + case VK_RIGHT: + if (editAscii) + moveAddress(1, 0); + else if (isShift) + moveAddress((maxNibble+1)>>1, 0); + else + moveAddress(0, 1); + break; + case VK_LEFT: + if (editAscii) + moveAddress(-1, 0); + else if (isShift) + moveAddress(-((maxNibble+1)>>1), 0); + else + moveAddress(0, -1); + break; + case VK_DOWN: + moveAddress(16, 0); + break; + case VK_UP: + moveAddress(-16, 0); + break; + case VK_TAB: + GetNextDlgTabItem(GetParent(), isShift)->SetFocus(); + break; + } +} + +void MemoryViewer::moveAddress(s32 offset, int nibbleOff) +{ + if (offset == 0) + { + if (nibbleOff == -1) + { + editNibble--; + if (editNibble == -1) + { + editAddress -= (maxNibble + 1) >> 1; + editNibble = maxNibble; + } + if (address == 0 && (editAddress >= (u32)(displayedLines<<4))) + { + editAddress = 0; + editNibble = 0; + beep(); + } + if (editAddress < address) + setAddress(address - 16); + } + else + { + editNibble++; + if (editNibble > maxNibble) + { + editNibble = 0; + editAddress += (maxNibble + 1) >> 1; + } + if (editAddress < address) + { + editAddress -= (maxNibble + 1) >> 1; + editNibble = maxNibble; + beep(); + } + if (editAddress >= (address+(displayedLines<<4))) + setAddress(address+16); + } + } + else + { + editAddress += offset; + if (offset < 0 && editAddress > (address-1+(displayedLines<<4))) + { + editAddress -= offset; + beep(); + return; + } + if (offset > 0 && (editAddress < address)) + { + editAddress -= offset; + beep(); + return; + } + if (editAddress < address) + { + if (offset & 15) + setAddress((address+offset-16) & ~15); + else + setAddress(address+offset); + } + else if (editAddress > (address - 1 + (displayedLines<<4))) + { + if (offset & 15) + setAddress((address+offset+16) & ~15); + else + setAddress(address+offset); + } + } + + setCaretPos(); +} + +LRESULT MemoryViewer::OnWMChar(WPARAM wParam, LPARAM LPARAM) +{ + if (OnEditInput(wParam)) + return 0; + return 1; +} + +bool MemoryViewer::OnEditInput(UINT c) +{ + if (c > 255 || !emulating) + { + beep(); + return false; + } + + if (!editAscii) + c = tolower(c); + + u32 value = 256; + + if (c >= 'a' && c <= 'f') + value = 10 + (c - 'a'); + else if (c >= '0' && c <= '9') + value = (c - '0'); + if (editAscii) + { + editData(editAddress, 8, 0, c); + moveAddress(1, 0); + InvalidateRect(NULL, TRUE); + } + else + { + if (value != 256) + { + value <<= 4*(maxNibble-editNibble); + u32 mask = ~(15 << 4*(maxNibble - editNibble)); + switch (dataSize) + { + case 0: + editData(editAddress, 8, mask, value); + break; + case 1: + editData(editAddress, 16, mask, value); + break; + case 2: + editData(editAddress, 32, mask, value); + break; + } + moveAddress(0, 1); + InvalidateRect(NULL, TRUE); + } + } + return true; +} + +void MemoryViewer::beep() +{ + MessageBeep((UINT)-1); +} + +void MemoryViewer::registerClass() +{ + if (!isRegistered) + { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_PARENTDC | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC) ::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaMemoryViewer"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} + +void MemoryViewer::setAddressSize(int s) +{ + addressSize = s; +} + +u32 MemoryViewer::getCurrentAddress() +{ + return editAddress; +} + +int MemoryViewer::getSize() +{ + return dataSize; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MemoryViewer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MemoryViewer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,97 @@ +#if !defined(AFX_MEMORYVIEWER_H__52C50474_5399_4D0B_A3E4_4C52C4E0EAA0__INCLUDED_) +#define AFX_MEMORYVIEWER_H__52C50474_5399_4D0B_A3E4_4C52C4E0EAA0__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// MemoryViewer.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewer window + +class IMemoryViewerDlg +{ +public: + virtual void setCurrentAddress(u32 address) = 0; +}; + +class MemoryViewer : public CWnd +{ + u32 address; + int addressSize; + int dataSize; + bool hasCaret; + int caretWidth; + int caretHeight; + HFONT font; + CSize fontSize; + u32 editAddress; + int editNibble; + int maxNibble; + int displayedLines; + int beginAscii; + int beginHex; + bool editAscii; + bool decimalDisplay; + IMemoryViewerDlg *dlg; + + static bool isRegistered; + // Construction +public: + MemoryViewer(); + + // Attributes +public: + // Operations +public: + virtual void readData(u32, int, u8 *) = 0; + virtual void editData(u32, int, int, u32) = 0; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MemoryViewer) + //}}AFX_VIRTUAL + + // Implementation +public: + int getSize(); + u32 getCurrentAddress(); + void setAddressSize(int s); + void registerClass(); + void beep(); + bool OnEditInput(UINT c); + void moveAddress(s32 offset, int nibbleOff); + void setCaretPos(); + void destroyEditCaret(); + void createEditCaret(int w, int h); + void updateScrollInfo(int lines); + void setSize(int s); + void setDecimal(bool decimalDisplayMode); + void setAddress(u32 a); + void setDialog(IMemoryViewerDlg *d); + virtual ~MemoryViewer(); + + // Generated message map functions +protected: + //{{AFX_MSG(MemoryViewer) + afx_msg BOOL OnEraseBkgnd(CDC*pDC); + afx_msg void OnPaint(); + afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar); + afx_msg UINT OnGetDlgCode(); + afx_msg void OnLButtonDown(UINT nFlags, CPoint point); + afx_msg void OnSetFocus(CWnd*pOldWnd); + afx_msg void OnKillFocus(CWnd*pNewWnd); + afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + afx_msg LRESULT OnWMChar(WPARAM wParam, LPARAM lParam); +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MEMORYVIEWER_H__52C50474_5399_4D0B_A3E4_4C52C4E0EAA0__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MemoryViewerAddressSize.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MemoryViewerAddressSize.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,115 @@ +// MemoryViewerAddressSize.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "MemoryViewerAddressSize.h" + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerAddressSize dialog + +MemoryViewerAddressSize::MemoryViewerAddressSize(u32 a, int s, CWnd*pParent /*=NULL*/) + : CDialog(MemoryViewerAddressSize::IDD, pParent) +{ + //{{AFX_DATA_INIT(MemoryViewerAddressSize) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + address = a; + size = s; +} + +void MemoryViewerAddressSize::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MemoryViewerAddressSize) + DDX_Control(pDX, IDC_SIZE_CONTROL, m_size); + DDX_Control(pDX, IDC_ADDRESS, m_address); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(MemoryViewerAddressSize, CDialog) +//{{AFX_MSG_MAP(MemoryViewerAddressSize) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerAddressSize message handlers + +BOOL MemoryViewerAddressSize::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CString buffer; + if (address != 0xFFFFFFFF) + { + buffer.Format("%08X", address); + m_address.SetWindowText(buffer); + } + if (size != -1) + { + buffer.Format("%08X", size); + m_size.SetWindowText(buffer); + m_size.EnableWindow(FALSE); + } + + if (size == -1 && address != 0xFFFFFFFF) + m_size.SetFocus(); + + m_address.LimitText(9); + m_size.LimitText(9); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MemoryViewerAddressSize::OnOk() +{ + CString buffer; + + m_address.GetWindowText(buffer); + if (buffer.IsEmpty()) + { + m_address.SetFocus(); + return; + } + sscanf(buffer, "%x", &address); + + m_size.GetWindowText(buffer); + if (buffer.IsEmpty()) + { + m_size.SetFocus(); + return; + } + sscanf(buffer, "%x", &size); + EndDialog(TRUE); +} + +void MemoryViewerAddressSize::OnCancel() +{ + EndDialog(FALSE); +} + +void MemoryViewerAddressSize::setAddress(u32 a) +{ + address = a; +} + +void MemoryViewerAddressSize::setSize(int s) +{ + size = s; +} + +u32 MemoryViewerAddressSize::getAddress() +{ + return address; +} + +\ + +int MemoryViewerAddressSize::getSize() +{ + return size; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MemoryViewerAddressSize.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MemoryViewerAddressSize.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,54 @@ +#if !defined(AFX_MEMORYVIEWERADDRESSSIZE_H__04605262_2B1D_4EED_A467_B6C56AC2CACD__INCLUDED_) +#define AFX_MEMORYVIEWERADDRESSSIZE_H__04605262_2B1D_4EED_A467_B6C56AC2CACD__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// MemoryViewerAddressSize.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerAddressSize dialog + +class MemoryViewerAddressSize : public CDialog +{ + u32 address; + int size; + // Construction +public: + int getSize(); + u32 getAddress(); + void setSize(int s); + void setAddress(u32 a); + MemoryViewerAddressSize(u32 a = 0xffffff, int s = -1, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(MemoryViewerAddressSize) + enum { IDD = IDD_ADDR_SIZE }; + CEdit m_size; + CEdit m_address; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MemoryViewerAddressSize) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(MemoryViewerAddressSize) + virtual BOOL OnInitDialog(); + afx_msg void OnOk(); + afx_msg void OnCancel(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MEMORYVIEWERADDRESSSIZE_H__04605262_2B1D_4EED_A467_B6C56AC2CACD__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MemoryViewerDlg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MemoryViewerDlg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,449 @@ +// MemoryViewerDlg.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "MemoryViewerDlg.h" +#include "FileDlg.h" +#include "MemoryViewerAddressSize.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "VBA.h" + +#include "../gba/GBAGlobals.h" + +#define CPUReadByteQuick(addr) \ + ::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask] +#define CPUWriteByteQuick(addr, b) \ + ::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask] = (b) +#define CPUReadHalfWordQuick(addr) \ + *((u16 *)&::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask]) +#define CPUWriteHalfWordQuick(addr, b) \ + *((u16 *)&::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask]) = (b) +#define CPUReadMemoryQuick(addr) \ + *((u32 *)&::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask]) +#define CPUWriteMemoryQuick(addr, b) \ + *((u32 *)&::map[(addr)>>24].address[(addr) & ::map[(addr)>>24].mask]) = (b) + +///////////////////////////////////////////////////////////////////////////// +// GBAMemoryViewer control + +GBAMemoryViewer::GBAMemoryViewer() + : MemoryViewer() +{ + setAddressSize(0); +} + +void GBAMemoryViewer::readData(u32 address, int len, u8 *data) +{ + if (emulating && rom != NULL) + { + for (int i = 0; i < len; i++) + { + *data++ = CPUReadByteQuick(address); + address++; + } + } + else + { + for (int i = 0; i < len; i++) + { + *data++ = 0; + address++; + } + } +} + +void GBAMemoryViewer::editData(u32 address, int size, int mask, u32 value) +{ + u32 oldValue; + + switch (size) + { + case 8: + oldValue = (CPUReadByteQuick(address) & mask) | value; + CPUWriteByteQuick(address, oldValue); + break; + case 16: + oldValue = (CPUReadHalfWordQuick(address) & mask) | value; + CPUWriteHalfWordQuick(address, oldValue); + break; + case 32: + oldValue = (CPUReadMemoryQuick(address) & mask) | value; + CPUWriteMemoryQuick(address, oldValue); + break; + } +} + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerDlg dialog + +MemoryViewerDlg::MemoryViewerDlg(CWnd*pParent /*=NULL*/) + : ResizeDlg(MemoryViewerDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(MemoryViewerDlg) + m_size = -1; + //}}AFX_DATA_INIT +} + +void MemoryViewerDlg::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MemoryViewerDlg) + DDX_Control(pDX, IDC_CURRENT_ADDRESS, m_current); + DDX_Control(pDX, IDC_ADDRESS, m_address); + DDX_Control(pDX, IDC_ADDRESSES, m_addresses); + DDX_Check(pDX, IDC_AUTO_UPDATE, autoUpdate); + DDX_Check(pDX, IDC_DECIMAL_DISPLAY, decimalDisplay); + DDX_Check(pDX, IDC_ALIGN, align); + DDX_Radio(pDX, IDC_8_BIT, m_size); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_VIEWER, m_viewer); +} + +BEGIN_MESSAGE_MAP(MemoryViewerDlg, CDialog) +//{{AFX_MSG_MAP(MemoryViewerDlg) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_BN_CLICKED(IDC_REFRESH, OnRefresh) +ON_BN_CLICKED(IDC_8_BIT, On8Bit) +ON_BN_CLICKED(IDC_16_BIT, On16Bit) +ON_BN_CLICKED(IDC_32_BIT, On32Bit) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_DECIMAL_DISPLAY, OnDecimalDisplay) +ON_BN_CLICKED(IDC_ALIGN, OnAlign) +ON_BN_CLICKED(IDC_GO, OnGo) +ON_CBN_SELCHANGE(IDC_ADDRESSES, OnSelchangeAddresses) +ON_BN_CLICKED(IDC_SAVE, OnSave) +ON_BN_CLICKED(IDC_LOAD, OnLoad) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerDlg message handlers + +BOOL MemoryViewerDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_VIEWER, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_LOAD, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_AUTO_UPDATE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_DECIMAL_DISPLAY, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_ALIGN, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CURRENT_ADDRESS_LABEL, DS_MoveY | DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_CURRENT_ADDRESS, DS_MoveY | DS_MoveX) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\GBDisassembleView", + NULL); + + autoUpdate = regQueryDwordValue("memViewerAutoUpdate", 1); + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } + + decimalDisplay = regQueryDwordValue("memViewerDecimalDisplay", 0); + m_viewer.setDecimal(decimalDisplay ? true : false); + align = regQueryDwordValue("memViewerAlign", 0); + + m_viewer.setDialog(this); + m_viewer.ShowScrollBar(SB_VERT, TRUE); + m_viewer.EnableScrollBar(SB_VERT, ESB_ENABLE_BOTH); + + LPCTSTR s[] = { + "0x00000000 - BIOS", + "0x02000000 - WRAM", + "0x03000000 - IRAM", + "0x04000000 - I/O", + "0x05000000 - PALETTE", + "0x06000000 - VRAM", + "0x07000000 - OAM", + "0x08000000 - ROM" + }; + + for (int i = 0; i < 8; i++) + m_addresses.AddString(s[i]); + + m_addresses.SetCurSel(0); + + RECT cbSize; + int Height; + + m_addresses.GetClientRect(&cbSize); + Height = m_addresses.GetItemHeight(-1); + Height += m_addresses.GetItemHeight(0) * (9); + + // Note: The use of SM_CYEDGE assumes that we're using Windows '95 + // Now add on the height of the border of the edit box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // The height of the border of the drop-down box + Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges + + // now set the size of the window + m_addresses.SetWindowPos(NULL, + 0, 0, + cbSize.right, Height, + SWP_NOMOVE | SWP_NOZORDER); + + m_address.LimitText(8); + + m_size = regQueryDwordValue("memViewerDataSize", 1); + if (m_size < 0 || m_size > 2) + m_size = 0; + m_viewer.setSize(m_size); + UpdateData(FALSE); + + m_current.SetFont(CFont::FromHandle((HFONT)GetStockObject(SYSTEM_FIXED_FONT))); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MemoryViewerDlg::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void MemoryViewerDlg::OnRefresh() +{ + m_viewer.Invalidate(); +} + +void MemoryViewerDlg::update() +{ + OnRefresh(); +} + +void MemoryViewerDlg::On8Bit() +{ + m_viewer.setSize(0); + regSetDwordValue("memViewerDataSize", 0); +} + +void MemoryViewerDlg::On16Bit() +{ + m_viewer.setSize(1); + regSetDwordValue("memViewerDataSize", 1); +} + +void MemoryViewerDlg::On32Bit() +{ + m_viewer.setSize(2); + regSetDwordValue("memViewerDataSize", 2); +} + +void MemoryViewerDlg::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } + regSetDwordValue("memViewerAutoUpdate", autoUpdate); +} + +void MemoryViewerDlg::OnDecimalDisplay() +{ + decimalDisplay = !decimalDisplay; + m_viewer.setDecimal(decimalDisplay ? true : false); + regSetDwordValue("memViewerDecimalDisplay", decimalDisplay); +} + +void MemoryViewerDlg::OnAlign() +{ + align = !align; + regSetDwordValue("memViewerAlign", align); +} + +void MemoryViewerDlg::OnGo() +{ + CString buffer; + + m_address.GetWindowText(buffer); + + u32 address; + sscanf(buffer, "%x", &address); + if (align) + address &= ~0xF; + else + { + if (m_viewer.getSize() == 1) + address &= ~1; + else if (m_viewer.getSize() == 2) + address &= ~3; + } + m_viewer.setAddress(address); +} + +void MemoryViewerDlg::OnSelchangeAddresses() +{ + int cur = m_addresses.GetCurSel(); + + switch (cur) + { + case 0: + m_viewer.setAddress(0); + break; + case 1: + m_viewer.setAddress(0x2000000); + break; + case 2: + m_viewer.setAddress(0x3000000); + break; + case 3: + m_viewer.setAddress(0x4000000); + break; + case 4: + m_viewer.setAddress(0x5000000); + break; + case 5: + m_viewer.setAddress(0x6000000); + break; + case 6: + m_viewer.setAddress(0x7000000); + break; + case 7: + m_viewer.setAddress(0x8000000); + break; + } +} + +void MemoryViewerDlg::setCurrentAddress(u32 address) +{ + CString buffer; + + buffer.Format("0x%08X", address); + m_current.SetWindowText(buffer); +} + +void MemoryViewerDlg::OnSave() +{ + MemoryViewerAddressSize dlg; + CString buffer; + + dlg.setAddress(m_viewer.getCurrentAddress()); + + LPCTSTR exts[] = { ".dmp", NULL }; + + if (dlg.DoModal() == IDOK) + { + CString filter = winResLoadFilter(IDS_FILTER_DUMP); + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); + + FileDlg file(this, + buffer, + filter, + 0, + "DMP", + exts, + "", + title, + true); + if (file.DoModal() == IDOK) + { + buffer = file.GetPathName(); + + FILE *f = fopen(buffer, "wb"); + + if (f == NULL) + { + systemMessage(IDS_ERROR_CREATING_FILE, buffer); + return; + } + + int size = dlg.getSize(); + u32 addr = dlg.getAddress(); + + for (int i = 0; i < size; i++) + { + fputc(CPUReadByteQuick(addr), f); + addr++; + } + + fclose(f); + } + } +} + +void MemoryViewerDlg::OnLoad() +{ + CString buffer; + LPCTSTR exts[] = { ".dmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_DUMP); + CString title = winResLoadString(IDS_SELECT_DUMP_FILE); + + FileDlg file(this, + buffer, + filter, + 0, + "DMP", + exts, + "", + title, + false); + + if (file.DoModal() == IDOK) + { + buffer = file.GetPathName(); + FILE *f = fopen(buffer, "rb"); + if (f == NULL) + { + systemMessage(IDS_CANNOT_OPEN_FILE, + "Cannot open file %s", + buffer); + return; + } + + MemoryViewerAddressSize dlg; + + fseek(f, 0, SEEK_END); + int size = ftell(f); + + fseek(f, 0, SEEK_SET); + + dlg.setAddress(m_viewer.getCurrentAddress()); + dlg.setSize(size); + + if (dlg.DoModal() == IDOK) + { + int size = dlg.getSize(); + u32 addr = dlg.getAddress(); + + for (int i = 0; i < size; i++) + { + int c = fgetc(f); + if (c == -1) + break; + CPUWriteByteQuick(addr, c); + addr++; + } + OnRefresh(); + } + fclose(f); + } +} + +void MemoryViewerDlg::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MemoryViewerDlg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MemoryViewerDlg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,79 @@ +#if !defined(AFX_MEMORYVIEWERDLG_H__15046D5B_D5A2_4C49_A969_2A77F803F2F1__INCLUDED_) +#define AFX_MEMORYVIEWERDLG_H__15046D5B_D5A2_4C49_A969_2A77F803F2F1__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// MemoryViewerDlg.h : header file +// + +#include "MemoryViewer.h" +#include "ResizeDlg.h" +#include "IUpdate.h" + +class GBAMemoryViewer : public MemoryViewer +{ +public: + GBAMemoryViewer(); + virtual void readData(u32, int, u8 *); + virtual void editData(u32, int, int, u32); +}; + +///////////////////////////////////////////////////////////////////////////// +// MemoryViewerDlg dialog + +class MemoryViewerDlg : public ResizeDlg, IUpdateListener, IMemoryViewerDlg +{ + GBAMemoryViewer m_viewer; + // Construction +public: + void setCurrentAddress(u32 address); + int autoUpdate; + int decimalDisplay; + int align; + void update(); + MemoryViewerDlg(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(MemoryViewerDlg) + enum { IDD = IDD_MEM_VIEWER }; + CEdit m_current; + CEdit m_address; + CComboBox m_addresses; + int m_size; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(MemoryViewerDlg) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(MemoryViewerDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + afx_msg void OnRefresh(); + afx_msg void On8Bit(); + afx_msg void On16Bit(); + afx_msg void On32Bit(); + afx_msg void OnAutoUpdate(); + afx_msg void OnDecimalDisplay(); + afx_msg void OnAlign(); + afx_msg void OnGo(); + afx_msg void OnSelchangeAddresses(); + afx_msg void OnSave(); + afx_msg void OnLoad(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MEMORYVIEWERDLG_H__15046D5B_D5A2_4C49_A969_2A77F803F2F1__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ModeConfirm.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ModeConfirm.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,90 @@ +// ModeConfirm.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "ModeConfirm.h" +#include "VBA.h" + +///////////////////////////////////////////////////////////////////////////// +// ModeConfirm dialog + + +ModeConfirm::ModeConfirm(CWnd* pParent /*=NULL*/) + : CDialog(ModeConfirm::IDD, pParent) +{ + //{{AFX_DATA_INIT(ModeConfirm) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + + +void ModeConfirm::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(ModeConfirm) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(ModeConfirm, CDialog) + //{{AFX_MSG_MAP(ModeConfirm) + ON_BN_CLICKED(ID_CANCEL, OnCancel) + ON_BN_CLICKED(ID_OK, OnOk) + ON_WM_DESTROY() + ON_WM_TIMER() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + ///////////////////////////////////////////////////////////////////////////// +// ModeConfirm message handlers + +void ModeConfirm::OnCancel() +{ + EndDialog(FALSE); +} + +void ModeConfirm::OnOk() +{ + EndDialog(TRUE); +} + +void ModeConfirm::OnDestroy() +{ + CDialog::OnDestroy(); + + KillTimer(timer); + timer = 0; +} + +BOOL ModeConfirm::OnInitDialog() +{ + CDialog::OnInitDialog(); + + timer = SetTimer(0, 1000, NULL); + + count = 10; + + CString buffer; + buffer.Format("%d", count); + + GetDlgItem(IDC_TIMER)->SetWindowText(buffer); + + CenterWindow(theApp.m_pMainWnd); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void ModeConfirm::OnTimer(UINT nIDEvent) +{ + CString buffer; + count--; + if(count == 0) + EndDialog(FALSE); + buffer.Format("%d", count); + GetDlgItem(IDC_TIMER)->SetWindowText(buffer); + + CDialog::OnTimer(nIDEvent); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ModeConfirm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ModeConfirm.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,51 @@ +#if !defined(AFX_MODECONFIRM_H__AF9F877E_6EDF_4523_95C9_1C745ABBA796__INCLUDED_) +#define AFX_MODECONFIRM_H__AF9F877E_6EDF_4523_95C9_1C745ABBA796__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// ModeConfirm.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// ModeConfirm dialog + +class ModeConfirm : public CDialog +{ + // Construction +public: + int count; + UINT timer; + ModeConfirm(CWnd*pParent); // standard constructor + + // Dialog Data + //{{AFX_DATA(ModeConfirm) + enum { IDD = IDD_MODE_CONFIRM }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ModeConfirm) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(ModeConfirm) + afx_msg void OnCancel(); + afx_msg void OnOk(); + afx_msg void OnDestroy(); + virtual BOOL OnInitDialog(); + afx_msg void OnTimer(UINT nIDEvent); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_MODECONFIRM_H__AF9F877E_6EDF_4523_95C9_1C745ABBA796__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MovieCreate.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MovieCreate.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,389 @@ +// MovieCreate.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "MovieCreate.h" +#include "MainWnd.h" +#include "FileDlg.h" +#include "WinResUtil.h" +#include "WinMiscUtil.h" +#include "VBA.h" + +#include "../NLS.h" +#include "../common/Util.h" +#include "../common/movie.h" +#include "../gba/GBAGlobals.h" +#include "../gb/gbGlobals.h" + +extern u32 myROM[]; + +#define GBC_CAPABLE ((gbRom[0x143] & 0x80) != 0) +#define SGB_CAPABLE (gbRom[0x146] == 0x03) + +// MovieCreate dialog + +IMPLEMENT_DYNAMIC(MovieCreate, CDialog) +MovieCreate::MovieCreate(CWnd *pParent /*=NULL*/) + : CDialog(MovieCreate::IDD, pParent) +{ + //{{AFX_DATA_INIT(MovieCreate) + m_startOption = 2; // "from start" as default + m_systemOption = systemCartridgeType == 0 ? 0 : (GBC_CAPABLE ? 1 : (SGB_CAPABLE ? 2 : 3)); // GBA, GBC, SGB, or GB + m_biosOption = systemCartridgeType == 0 ? (useBios ? 2 : 1) : 0; // none for non-GBA, or introless and based on settings + //}}AFX_DATA_INIT +} + +MovieCreate::~MovieCreate() +{} + +BOOL MovieCreate::OnInitDialog() +{ + CDialog::OnInitDialog(); + + GetDlgItem(IDC_REC_GBA)->EnableWindow(systemCartridgeType == 0); + GetDlgItem(IDC_REC_GBC)->EnableWindow(systemCartridgeType != 0 && GBC_CAPABLE); + GetDlgItem(IDC_REC_SGB)->EnableWindow(systemCartridgeType != 0 && SGB_CAPABLE); + GetDlgItem(IDC_REC_GB)->EnableWindow(systemCartridgeType != 0); + + GetDlgItem(IDC_REC_NOBIOS)->EnableWindow(systemCartridgeType != 0); + GetDlgItem(IDC_REC_EMUBIOS)->EnableWindow(systemCartridgeType == 0); + GetDlgItem(IDC_REC_GBABIOS)->EnableWindow(systemCartridgeType == 0); + GetDlgItem(IDC_REC_GBABIOSINTRO)->EnableWindow(systemCartridgeType == 0); + + CheckRadioButton(IDC_REC_NOBIOS, IDC_REC_GBABIOSINTRO, IDC_REC_NOBIOS + m_biosOption); + + m_editFilename.LimitText(_MAX_PATH); + m_editAuthor.LimitText(MOVIE_METADATA_AUTHOR_SIZE); + m_editDescription.LimitText(MOVIE_METADATA_SIZE - MOVIE_METADATA_AUTHOR_SIZE); + + // convert the ROM filename into a default movie name + CString movieName = winGetDestFilename(theApp.gameFilename, IDS_MOVIE_DIR, ".vbm"); + + GetDlgItem(IDC_MOVIE_FILENAME)->SetWindowText(movieName); + + // scroll to show the rightmost side of the movie filename + ((CEdit *)GetDlgItem(IDC_MOVIE_FILENAME))->SetSel((DWORD)(movieName.GetLength() - 1), FALSE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MovieCreate::DoDataExchange(CDataExchange *pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MovieCreate) + DDX_Radio(pDX, IDC_RECNOW, m_startOption); + DDX_Radio(pDX, IDC_REC_GBA, m_systemOption); +/// done manually DDX_Radio(pDX, IDC_REC_NOBIOS, m_biosOption); + DDX_Control(pDX, IDC_EDIT_AUTHOR, m_editAuthor); + DDX_Control(pDX, IDC_EDIT_DESCRIPTION, m_editDescription); + DDX_Control(pDX, IDC_MOVIE_FILENAME, m_editFilename); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(MovieCreate, CDialog) +ON_BN_CLICKED(IDOK, OnBnClickedOk) +ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel) +ON_BN_CLICKED(IDC_BROWSE, OnBnClickedBrowse) +ON_BN_CLICKED(IDC_RECSTART, OnBnClickedRecstart) +ON_BN_CLICKED(IDC_RECRESET, OnBnClickedRecreset) +ON_BN_CLICKED(IDC_RECNOW, OnBnClickedRecnow) +ON_BN_CLICKED(IDC_REC_GBA, OnBnClickedRecGba) +ON_BN_CLICKED(IDC_REC_GBC, OnBnClickedRecGbc) +ON_BN_CLICKED(IDC_REC_SGB, OnBnClickedRecSgb) +ON_BN_CLICKED(IDC_REC_GB, OnBnClickedRecGb) +ON_BN_CLICKED(IDC_REC_NOBIOS, OnBnClickedRecNobios) +ON_BN_CLICKED(IDC_REC_EMUBIOS, OnBnClickedRecEmubios) +ON_BN_CLICKED(IDC_REC_GBABIOS, OnBnClickedRecGbabios) +ON_BN_CLICKED(IDC_REC_GBABIOSINTRO, OnBnClickedRecGbabiosintro) +END_MESSAGE_MAP() + +// MovieCreate message handlers + +void MovieCreate::OnBnClickedBrowse() +{ + theApp.winCheckFullscreen(); + + LPCTSTR exts[] = { ".vbm", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_MOVIE); + CString title = winResLoadString(IDS_SELECT_MOVIE_NAME); + + CString movieName = winGetDestFilename(theApp.gameFilename, IDS_MOVIE_DIR, exts[0]); + CString movieDir = winGetDestDir(IDS_MOVIE_DIR); + + FileDlg dlg(this, movieName, filter, 1, "VBM", exts, movieDir, title, true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + movieName = dlg.GetPathName(); + + GetDlgItem(IDC_MOVIE_FILENAME)->SetWindowText(movieName); + + // scroll to show the rightmost side of the movie filename + ((CEdit *)GetDlgItem(IDC_MOVIE_FILENAME))->SetSel((DWORD)(movieName.GetLength() - 1), FALSE); +} + +void MovieCreate::OnBnClickedOk() +{ + // has to be done before creating the movie + bool useBiosFile = false; + bool skipBiosFile = false; + + if (m_biosOption == 1) + { + useBiosFile = false; + } + else if (m_biosOption == 2) + { + useBiosFile = true; + skipBiosFile = true; + } + else if (m_biosOption == 3) + { + useBiosFile = true; + skipBiosFile = false; + } + + extern bool systemLoadBIOS(const char *biosFileName, bool useBiosFile); + if (!systemLoadBIOS(theApp.biosFileName, useBiosFile)) + { + if (m_biosOption > 1) + { + systemMessage(0, "Invalid GBA BIOS file!"); + return; + } + } + + theApp.useBiosFile = useBiosFile; + if (useBiosFile) + theApp.skipBiosFile = skipBiosFile; + + int startFlags = 0, controllerFlags = 0, typeFlags = 0; + + if (m_startOption == 0) + startFlags |= MOVIE_START_FROM_SNAPSHOT; + else if (m_startOption == 1) + startFlags |= MOVIE_START_FROM_SRAM; + //else + // startFlags = 0; // no SRAM or snapshot + + if (m_systemOption == 0) + { + typeFlags |= MOVIE_TYPE_GBA; + gbEmulatorType = 4; + } + else if (m_systemOption == 1) + { + typeFlags |= MOVIE_TYPE_GBC; + gbEmulatorType = 1; + } + else if (m_systemOption == 2) + { + typeFlags |= MOVIE_TYPE_SGB; + gbEmulatorType = 2; + } + else + { + gbEmulatorType = 3; // plain old GB + } + + controllerFlags |= MOVIE_CONTROLLER(0); + if (typeFlags & MOVIE_TYPE_SGB) + { + // XXX FIXME - the code for multiple controllers must be broken somehow + // (it crashes strangely during FreezeToStream in SGB games) + + // SGB games are free to request controllers while running, so we have to assume it needs all 4 +/// controllerFlags |= MOVIE_CONTROLLER(1) | MOVIE_CONTROLLER(2) | MOVIE_CONTROLLER(3); + } + + // get author and movie info from the edit fields: + char info [MOVIE_METADATA_SIZE], buffer [MOVIE_METADATA_SIZE]; + + GetDlgItem(IDC_EDIT_AUTHOR)->GetWindowText(buffer, MOVIE_METADATA_AUTHOR_SIZE); + strncpy(info, buffer, MOVIE_METADATA_AUTHOR_SIZE); + info[MOVIE_METADATA_AUTHOR_SIZE - 1] = '\0'; + + GetDlgItem(IDC_EDIT_DESCRIPTION)->GetWindowText(buffer, MOVIE_METADATA_SIZE - MOVIE_METADATA_AUTHOR_SIZE); + strncpy(info + MOVIE_METADATA_AUTHOR_SIZE, buffer, MOVIE_METADATA_SIZE - MOVIE_METADATA_AUTHOR_SIZE); + info[MOVIE_METADATA_SIZE - 1] = '\0'; + + if (memLagTempEnabled && systemCartridgeType == 0) + { + // lag reduction is off -- if the user didn't turn it off, silently turn it back on, otherwise ask + int ans = !memLagEnabled ? IDYES : AfxGetApp()->m_pMainWnd->MessageBox( + "Lag reduction is currently OFF.\nIt is recommended that you turn this ON for recording.\nWould you like to turn it on now?", + "Warning", + MB_YESNOCANCEL | MB_ICONWARNING); + switch (ans) + { + case IDYES: + extern void SetPrefetchHack(bool); + SetPrefetchHack(true); + break; + case IDNO: + break; + case IDCANCEL: + return; + } + } + + CString movieName; + GetDlgItem(IDC_MOVIE_FILENAME)->GetWindowText(movieName); + + // actually make the movie file: + int code = VBAMovieCreate(movieName, info, startFlags, controllerFlags, typeFlags); + + if (code != MOVIE_SUCCESS) + { + systemMessage(0, "Failed to create movie %s", (const char *)movieName); + return; + } + + OnOK(); +} + +void MovieCreate::OnBnClickedCancel() +{ + OnCancel(); +} + +void MovieCreate::OnBnClickedRecstart() +{ + m_startOption = 2; + if (systemCartridgeType == 0) + { + GetDlgItem(IDC_REC_EMUBIOS)->EnableWindow(TRUE); + GetDlgItem(IDC_REC_GBABIOSINTRO)->EnableWindow(TRUE); + } + else + { + GetDlgItem(IDC_REC_GBC)->EnableWindow(GBC_CAPABLE); + GetDlgItem(IDC_REC_SGB)->EnableWindow(SGB_CAPABLE); + GetDlgItem(IDC_REC_GB)->EnableWindow(TRUE); + } +} + +void MovieCreate::OnBnClickedRecreset() +{ + m_startOption = 1; + if (systemCartridgeType == 0) + { + GetDlgItem(IDC_REC_EMUBIOS)->EnableWindow(TRUE); + GetDlgItem(IDC_REC_GBABIOSINTRO)->EnableWindow(TRUE); + } + else + { + GetDlgItem(IDC_REC_GBC)->EnableWindow(GBC_CAPABLE); + GetDlgItem(IDC_REC_SGB)->EnableWindow(SGB_CAPABLE); + GetDlgItem(IDC_REC_GB)->EnableWindow(TRUE); + } +} + +void MovieCreate::OnBnClickedRecnow() +{ + m_startOption = 0; + + // starting from emulator bios file from a snapshot made while playing with GBA bios file won't work + if (systemCartridgeType == 0 && useBios) + { + if (m_biosOption == 1) + { + OnBnClickedRecGbabios(); + CheckRadioButton(IDC_REC_NOBIOS, IDC_REC_GBABIOSINTRO, IDC_REC_NOBIOS + m_biosOption); + } + GetDlgItem(IDC_REC_EMUBIOS)->EnableWindow(FALSE); + } + + // "with intro" distinction makes no sense when continuing from snapshot + if (systemCartridgeType == 0) + { + if (m_biosOption == 3) + { + OnBnClickedRecGbabios(); + CheckRadioButton(IDC_REC_NOBIOS, IDC_REC_GBABIOSINTRO, IDC_REC_NOBIOS + m_biosOption); + } + GetDlgItem(IDC_REC_GBABIOSINTRO)->EnableWindow(FALSE); + } + + // can't switch systems while recording from snapshot! + if (systemCartridgeType != 0) + { + int curSystemOption = (gbCgbMode == 1 ? 1 : (gbSgbMode == 1 ? 2 : 3)); // GBC, SGB, or GB + GetDlgItem(IDC_REC_GBC)->EnableWindow(curSystemOption == 1); + GetDlgItem(IDC_REC_SGB)->EnableWindow(curSystemOption == 2); + GetDlgItem(IDC_REC_GB)->EnableWindow(curSystemOption == 3); + m_systemOption = curSystemOption; + CheckRadioButton(IDC_REC_GBA, IDC_REC_GB, IDC_REC_GBA + m_systemOption); + } +} + +void MovieCreate::OnBnClickedRecGba() +{ + m_systemOption = 0; +} + +void MovieCreate::OnBnClickedRecGbc() +{ + m_systemOption = 1; +} + +void MovieCreate::OnBnClickedRecSgb() +{ + m_systemOption = 2; +} + +void MovieCreate::OnBnClickedRecGb() +{ + m_systemOption = 3; +} + +void MovieCreate::OnBnClickedRecNobios() +{ + m_biosOption = 0; +} + +void MovieCreate::OnBnClickedRecEmubios() +{ + m_biosOption = 1; +} + +void MovieCreate::OnBnClickedRecGbabios() +{ + if (utilCheckBIOS(theApp.biosFileName, 4)) + m_biosOption = 2; + else + { + ((MainWnd *)theApp.m_pMainWnd)->OnOptionsEmulatorSelectbiosfile(); + if (utilCheckBIOS(theApp.biosFileName, 4)) + m_biosOption = 2; + else + { + systemMessage(0, "This option requires a valid GBA BIOS file."); + CheckRadioButton(IDC_REC_NOBIOS, IDC_REC_GBABIOSINTRO, IDC_REC_EMUBIOS); + } + } +} + +void MovieCreate::OnBnClickedRecGbabiosintro() +{ + if (utilCheckBIOS(theApp.biosFileName, 4)) + m_biosOption = 3; + else + { + ((MainWnd *)theApp.m_pMainWnd)->OnOptionsEmulatorSelectbiosfile(); + if (utilCheckBIOS(theApp.biosFileName, 4)) + m_biosOption = 3; + else + { + systemMessage(0, "This option requires a valid GBA BIOS file."); + CheckRadioButton(IDC_REC_NOBIOS, IDC_REC_GBABIOSINTRO, IDC_REC_EMUBIOS); + } + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MovieCreate.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MovieCreate.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,50 @@ +#ifndef VBA_MOVIECREATE_H +#define VBA_MOVIECREATE_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +// MovieCreate dialog +class MovieCreate : public CDialog +{ + DECLARE_DYNAMIC(MovieCreate) + +public: + MovieCreate(CWnd* pParent = NULL); // standard constructor + virtual ~MovieCreate(); + virtual BOOL OnInitDialog() ; + +// Dialog Data + //{{AFX_DATA(MovieCreate) + enum { IDD = IDD_MOVIECREATE }; + int m_startOption; + int m_systemOption; + int m_biosOption; + CEdit m_editAuthor; + CEdit m_editDescription; + CEdit m_editFilename; + //}}AFX_DATA + +protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + + DECLARE_MESSAGE_MAP() +public: + afx_msg void OnBnClickedOk(); + afx_msg void OnBnClickedCancel(); + afx_msg void OnBnClickedBrowse(); + afx_msg void OnBnClickedRecstart(); + afx_msg void OnBnClickedRecreset(); + afx_msg void OnBnClickedRecnow(); + afx_msg void OnBnClickedRecGba(); + afx_msg void OnBnClickedRecGbc(); + afx_msg void OnBnClickedRecSgb(); + afx_msg void OnBnClickedRecGb(); + afx_msg void OnBnClickedRecNobios(); + afx_msg void OnBnClickedRecEmubios(); + afx_msg void OnBnClickedRecGbabios(); + afx_msg void OnBnClickedRecGbabiosintro(); +}; + +#endif // VBA_MOVIECREATE_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MovieOpen.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MovieOpen.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,532 @@ +// MovieOpen.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "MovieOpen.h" +#include "MainWnd.h" +#include "FileDlg.h" +#include "WinResUtil.h" +#include "WinMiscUtil.h" +#include "VBA.h" + +#include "../gba/GBA.h" +#include "../gb/gbGlobals.h" +#include "../common/Util.h" +#include "7zip/OpenArchive.h" + +// MovieOpen dialog + +IMPLEMENT_DYNAMIC(MovieOpen, CDialog) +MovieOpen::MovieOpen(CWnd *pParent /*=NULL*/) + : CDialog(MovieOpen::IDD, pParent) +{} + +MovieOpen::~MovieOpen() +{ + SetArchiveParentHWND(NULL); +} + +BOOL MovieOpen::OnInitDialog() +{ + CDialog::OnInitDialog(); + + pauseFrame = 0; + ZeroMemory(&movieInfo, sizeof SMovie); + + SetArchiveParentHWND(GetSafeHwnd()); + + GetDlgItem(IDC_CHECK_HIDEBORDER)->ShowWindow(FALSE); + GetDlgItem(IDC_LABEL_WARNING1)->SetWindowText(""); + GetDlgItem(IDC_LABEL_WARNING2)->SetWindowText(""); + GetDlgItem(IDC_EDIT_PAUSEFRAME)->SetWindowText(""); + GetDlgItem(IDC_EDIT_PAUSEFRAME)->EnableWindow(FALSE); + GetDlgItem(IDC_CHECK_PAUSEFRAME)->EnableWindow(FALSE); + + CheckDlgButton(IDC_READONLY, TRUE); + m_editDescription.SetReadOnly(theApp.movieReadOnly); + + m_editFilename.LimitText(_MAX_PATH); + m_editAuthor.LimitText(MOVIE_METADATA_AUTHOR_SIZE); + m_editDescription.LimitText(MOVIE_METADATA_SIZE - MOVIE_METADATA_AUTHOR_SIZE); + m_editPauseFrame.LimitText(8); + + // convert the ROM filename into a default movie name + CString movieName = winGetDestFilename(theApp.gameFilename, IDS_MOVIE_DIR, ".vbm"); + + GetDlgItem(IDC_MOVIE_FILENAME)->SetWindowText(movieName); + + // scroll to show the rightmost side of the movie filename + ((CEdit *)GetDlgItem(IDC_MOVIE_FILENAME))->SetSel((DWORD)(movieName.GetLength() - 1), FALSE); + + OnBnClickedMovieRefresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void MovieOpen::DoDataExchange(CDataExchange *pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(MovieCreate) + DDX_Control(pDX, IDC_EDIT_AUTHOR, m_editAuthor); + DDX_Control(pDX, IDC_EDIT_DESCRIPTION, m_editDescription); + DDX_Control(pDX, IDC_MOVIE_FILENAME, m_editFilename); + DDX_Control(pDX, IDC_EDIT_PAUSEFRAME, m_editPauseFrame); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(MovieOpen, CDialog) +ON_BN_CLICKED(IDC_BROWSE, OnBnClickedBrowse) +ON_BN_CLICKED(IDOK, OnBnClickedOk) +ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel) +ON_BN_CLICKED(IDC_MOVIE_REFRESH, OnBnClickedMovieRefresh) +ON_BN_CLICKED(IDC_READONLY, OnBnClickedReadonly) +ON_BN_CLICKED(IDC_CHECK_PAUSEFRAME, OnBnClickedCheckPauseframe) +ON_BN_CLICKED(IDC_CHECK_HIDEBORDER, OnBnClickedHideborder) +ON_EN_CHANGE(IDC_MOVIE_FILENAME, OnEnChangeMovieFilename) +END_MESSAGE_MAP() + +// MovieOpen message handlers + +// FIXME: file-scope-global +static bool shouldReopenBrowse = false; + +void MovieOpen::OnBnClickedBrowse() +{ + theApp.winCheckFullscreen(); // FIXME: necessary or not? + + LPCTSTR exts[] = { ".vbm", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_MOVIE); + CString title = winResLoadString(IDS_SELECT_MOVIE_NAME); + + CString movieName = winGetDestFilename(theApp.gameFilename, IDS_MOVIE_DIR, exts[0]); + CString movieDir = winGetDestDir(IDS_MOVIE_DIR); + + FileDlg dlg(this, movieName, filter, 1, "VBM", exts, movieDir, title, false, true); + + do + { + shouldReopenBrowse = false; + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + movieName = dlg.GetPathName(); + + GetDlgItem(IDC_MOVIE_FILENAME)->SetWindowText(movieName); + + // SetWindowText calls OnEnChangeMovieFilename which calls OnBnClickedMovieRefresh + // so this extra call to OnBnClickedMovieRefresh is bad + //OnBnClickedMovieRefresh(); + } + while (shouldReopenBrowse); + + // scroll to show the rightmost side of the movie filename + ((CEdit *)GetDlgItem(IDC_MOVIE_FILENAME))->SetSel((DWORD)(movieName.GetLength() - 1), FALSE); +} + +// some extensions that might commonly be near emulation-related files that we almost certainly can't open, or at least not +// directly. +// also includes definitely non-movie extensions we know about, since we only use this variable in a movie opening function. +// we do this by exclusion instead of inclusion because we don't want to exclude extensions used for any archive files, even +// extensionless or unusually-named archives. +static const char *s_movieIgnoreExtensions [] = { + "gba", "gbc", "gb", "sgb", "cgb", "bin", "agb", "bios", "mb", "elf", "sgm", "clt", "dat", "gbs", "gcf", "spc", + "xpc", "pal", "act", "dmp", "avi", "ini", "txt", "nfo", "htm", "html", "jpg", "jpeg", "png", "bmp", "gif", "mp3", + "wav", "lnk", "exe", "bat", "sav", "luasav" +}; + +void MovieOpen::OnBnClickedMovieRefresh() +{ + static int recursionDepth = 0; + if (recursionDepth > 0) + return; + struct Scope {Scope(){ ++ recursionDepth; } ~Scope(){ --recursionDepth; }} scope; + + CString tempName; + GetDlgItem(IDC_MOVIE_FILENAME)->GetWindowText(tempName); + +#if 1 + // use ObtainFile to support opening files within archives (.7z, .rar, .zip, .zip.rar.7z, etc.) + + if (movieLogicalName.GetLength() > 2048) movieLogicalName.Truncate(2048); + + char LogicalName[2048], PhysicalName[2048]; + if (ObtainFile(tempName, LogicalName, PhysicalName, "mov", s_movieIgnoreExtensions, + sizeof(s_movieIgnoreExtensions) / sizeof(*s_movieIgnoreExtensions))) + { + if (tempName != LogicalName) + { + int selStart = 0, selEnd = 0; + ((CEdit *)GetDlgItem(IDC_MOVIE_FILENAME))->GetSel(selStart, selEnd); + + GetDlgItem(IDC_MOVIE_FILENAME)->SetWindowText(LogicalName); + + ((CEdit *)GetDlgItem(IDC_MOVIE_FILENAME))->SetSel(selStart, selEnd, FALSE); + } + moviePhysicalName = PhysicalName; + movieLogicalName = LogicalName; + ReleaseTempFileCategory("mov", PhysicalName); + } + else + { + shouldReopenBrowse = true; + return; + } +#else + // old version that only supports uncompressed movies + moviePhysicalName = tempName; + movieLogicalName = tempName; +#endif + + if (VBAMovieGetInfo(moviePhysicalName, &movieInfo) == MOVIE_SUCCESS) + { + if (movieInfo.readOnly) + { + CheckDlgButton(IDC_READONLY, TRUE); + m_editDescription.SetReadOnly(TRUE); + } + + char buffer[MOVIE_METADATA_SIZE]; + + strncpy(buffer, movieInfo.authorInfo, MOVIE_METADATA_AUTHOR_SIZE); + buffer[MOVIE_METADATA_AUTHOR_SIZE - 1] = '\0'; + GetDlgItem(IDC_EDIT_AUTHOR)->SetWindowText(buffer); + + strncpy(buffer, movieInfo.authorInfo + MOVIE_METADATA_AUTHOR_SIZE, MOVIE_METADATA_SIZE - MOVIE_METADATA_AUTHOR_SIZE); + buffer[MOVIE_METADATA_SIZE - MOVIE_METADATA_AUTHOR_SIZE - 1] = '\0'; + GetDlgItem(IDC_EDIT_DESCRIPTION)->SetWindowText(buffer); + + int option = 2; + if (movieInfo.header.startFlags & MOVIE_START_FROM_SRAM) + option = 1; + if (movieInfo.header.startFlags & MOVIE_START_FROM_SNAPSHOT) + option = 0; + CheckRadioButton(IDC_RECNOW, IDC_RECSTART, IDC_RECNOW + option); + + option = 3; + if (movieInfo.header.typeFlags & MOVIE_TYPE_SGB) + option = 2; + if (movieInfo.header.typeFlags & MOVIE_TYPE_GBC) + option = 1; + if (movieInfo.header.typeFlags & MOVIE_TYPE_GBA) + option = 0; + CheckRadioButton(IDC_REC_GBA, IDC_REC_GB, IDC_REC_GBA + option); + + GetDlgItem(IDC_CHECK_HIDEBORDER)->ShowWindow(option == 2 ? TRUE : FALSE); + + if (movieInfo.header.typeFlags & MOVIE_TYPE_GBA) + { + if (movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) + { + if (movieInfo.header.optionFlags & MOVIE_SETTING_SKIPBIOSFILE) + option = 2; + else + option = 3; + } + else + option = 1; + } + else + option = 0; + + CheckRadioButton(IDC_REC_NOBIOS, IDC_REC_GBABIOSINTRO, IDC_REC_NOBIOS + option); + + { + char * p; + time_t ttime = (time_t)movieInfo.header.uid; + strncpy(buffer, ctime(&ttime), 127); + buffer[127] = '\0'; + if ((p = strrchr(buffer, '\n'))) + *p = '\0'; + GetDlgItem(IDC_LABEL_DATE)->SetWindowText(buffer); + + uint32 div = 60; + uint32 l = (movieInfo.header.length_frames + (div >> 1)) / div; + uint32 seconds = l % 60; + l /= 60; + uint32 minutes = l % 60; + l /= 60; + uint32 hours = l % 60; + sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds); + GetDlgItem(IDC_LABEL_LENGTH)->SetWindowText(buffer); + sprintf(buffer, "%ld", movieInfo.header.length_frames); + GetDlgItem(IDC_LABEL_FRAMES)->SetWindowText(buffer); + sprintf(buffer, "%ld", movieInfo.header.rerecord_count); + GetDlgItem(IDC_LABEL_RERECORD)->SetWindowText(buffer); + } + + { + char warning1 [1024], warning2 [1024], buffer [1024]; + + strcpy(warning1, ""); + strcpy(warning2, ""); + + char romTitle [12]; + uint32 romGameCode; + uint16 checksum; + uint8 crc; + + VBAMovieGetRomInfo(movieInfo, romTitle, romGameCode, checksum, crc); + + // rather than treat these as warnings, might as well always show the info in the dialog (it's probably more + // informative and reassuring) +/// if (strncmp(movieInfo.header.romTitle,romTitle,12) != 0) + { + char str [13]; + strncpy(str, movieInfo.header.romTitle, 12); + str[12] = '\0'; + sprintf(buffer, "title=%s ", str); + strcat(warning1, buffer); + + strncpy(str, romTitle, 12); + str[12] = '\0'; + sprintf(buffer, "title=%s ", str); + strcat(warning2, buffer); + } +/// if (((movieInfo.header.typeFlags & MOVIE_TYPE_GBA)!=0) != (systemCartridgeType == 0)) + { + sprintf(buffer, "type=%s ", + (movieInfo.header.typeFlags & MOVIE_TYPE_GBA) ? "GBA" : (movieInfo.header.typeFlags & + MOVIE_TYPE_GBC) ? "GBC" : (movieInfo.header. + typeFlags & + MOVIE_TYPE_SGB) ? "SGB" : "GB"); + strcat(warning1, buffer); + + sprintf(buffer, "type=%s ", systemCartridgeType == + 0 ? "GBA" : (gbRom[0x143] & 0x80 ? "GBC" : (gbRom[0x146] == 0x03 ? "SGB" : "GB"))); + strcat(warning2, buffer); + } +/// if (movieInfo.header.romCRC != crc) + { + sprintf(buffer, "crc=%02x ", movieInfo.header.romCRC); + strcat(warning1, buffer); + + sprintf(buffer, "crc=%02x ", crc); + strcat(warning2, buffer); + } +/// if (movieInfo.header.romGameCode != romGameCode) + { + char code [5]; + if (movieInfo.header.typeFlags & MOVIE_TYPE_GBA) + { + memcpy(code, &movieInfo.header.romGameCode, 4); + code[4] = '\0'; + sprintf(buffer, "code=%s ", code); + strcat(warning1, buffer); + } + + if (systemCartridgeType == 0) + { + memcpy(code, &romGameCode, 4); + code[4] = '\0'; + sprintf(buffer, "code=%s ", code); + strcat(warning2, buffer); + } + } +/// if (movieInfo.header.romOrBiosChecksum != checksum && !((movieInfo.header.optionFlags & +// MOVIE_SETTING_USEBIOSFILE)==0 +// && checksum==0)) + { + sprintf(buffer, + movieInfo.header.typeFlags & + MOVIE_TYPE_GBA ? ((movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) == + 0 ? "(bios=none) " : "(bios=%04x) ") : "check=%04x ", + movieInfo.header.romOrBiosChecksum); + strcat(warning1, buffer); + + sprintf(buffer, + checksum == 0 ? "(bios=none) " : systemCartridgeType == 0 ? "(bios=%04x) " : "check=%04x ", + checksum); + strcat(warning2, buffer); + } + + if (strlen(warning1) > 0) + { + sprintf(buffer, "Movie ROM: %s", warning1); + GetDlgItem(IDC_LABEL_WARNING1)->SetWindowText(buffer); + + sprintf(buffer, "Your ROM: %s", warning2); + + //if(movieInfo.header.romCRC != crc + //|| strncmp(movieInfo.header.romTitle,romTitle,12) != 0 + //|| movieInfo.header.romOrBiosChecksum != checksum && !((movieInfo.header.optionFlags & + // MOVIE_SETTING_USEBIOSFILE)==0 && checksum==0)) + // strcat(buffer, "<-- MISMATCH"); + + GetDlgItem(IDC_LABEL_WARNING2)->SetWindowText(buffer); + } + else + { + GetDlgItem(IDC_LABEL_WARNING1)->SetWindowText("(No problems detected)"); + GetDlgItem(IDC_LABEL_WARNING2)->SetWindowText(" "); + } + } + GetDlgItem(IDC_CHECK_PAUSEFRAME)->EnableWindow(TRUE); + } + else + { + GetDlgItem(IDC_LABEL_DATE)->SetWindowText(" "); + GetDlgItem(IDC_LABEL_LENGTH)->SetWindowText(" "); + GetDlgItem(IDC_LABEL_FRAMES)->SetWindowText(" "); + GetDlgItem(IDC_LABEL_RERECORD)->SetWindowText(" "); + GetDlgItem(IDC_EDIT_AUTHOR)->SetWindowText(" "); + GetDlgItem(IDC_EDIT_DESCRIPTION)->SetWindowText(" "); + GetDlgItem(IDC_LABEL_WARNING1)->SetWindowText(" "); + GetDlgItem(IDC_LABEL_WARNING2)->SetWindowText(" "); + + GetDlgItem(IDC_EDIT_PAUSEFRAME)->SetWindowText(""); + GetDlgItem(IDC_EDIT_PAUSEFRAME)->EnableWindow(FALSE); + GetDlgItem(IDC_CHECK_PAUSEFRAME)->EnableWindow(FALSE); + CheckDlgButton(IDC_CHECK_PAUSEFRAME, FALSE); +/* + /// FIXME: how to un-check all the radio buttons? + CheckRadioButton(IDC_RECNOW, IDC_RECSTART, -1); + CheckRadioButton(IDC_REC_GBA, IDC_REC_GB, -1); + CheckRadioButton(IDC_REC_NOBIOS, IDC_REC_GBABIOSINTRO, -1);*/ + } +} + +BOOL MovieOpen::OnWndMsg(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT *res) +{ + switch (msg) + { + case WM_CTLCOLORSTATIC: + { + //HWND hwndDlg = GetSafeHwnd(); + HWND warnDlg = NULL; + GetDlgItem(IDC_LABEL_WARNING2, &warnDlg); + + if ((HWND)lParam == warnDlg) + { + char romTitle [12]; + uint32 romGameCode; + uint16 checksum; + uint8 crc; + + VBAMovieGetRomInfo(movieInfo, romTitle, romGameCode, checksum, crc); + + if (movieInfo.header.romCRC != crc + || strncmp(movieInfo.header.romTitle, romTitle, 12) != 0 + || movieInfo.header.romOrBiosChecksum != checksum + && !((movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) == 0 && checksum == 0)) + { + // draw the md5 sum in red if it's different from the md5 of the rom used in the replay + HDC hdcStatic = (HDC)wParam; + SetTextColor(hdcStatic, RGB(255, 0, 0)); // use red for a mismatch + + // I'm not sure why this doesn't work to make the background transparent... it turns white anyway + SetBkMode(hdcStatic, TRANSPARENT); + return (LONG)GetStockObject(NULL_BRUSH); + } + } + return FALSE; + } + } + return CDialog::OnWndMsg(msg, wParam, lParam, res); +} + +void MovieOpen::OnBnClickedOk() +{ + bool useBIOSFile = (movieInfo.header.optionFlags & MOVIE_SETTING_USEBIOSFILE) != 0; + if (useBIOSFile) + { + extern bool systemLoadBIOS(const char *biosFileName, bool useBiosFile); + if (!systemLoadBIOS(theApp.biosFileName, useBIOSFile)) + { + systemMessage(0, "This movie requires a valid GBA BIOS file to play.\nPlease locate a BIOS file."); + ((MainWnd *)theApp.m_pMainWnd)->OnOptionsEmulatorSelectbiosfile(); + if (!systemLoadBIOS(theApp.biosFileName, useBIOSFile)) + { + systemMessage(0, "\"%s\" is not a valid BIOS file; cannot play movie without one.", theApp.biosFileName); + return; + } + } + } + + int code = VBAMovieOpen(moviePhysicalName, IsDlgButtonChecked(IDC_READONLY) != FALSE); + + if (code != MOVIE_SUCCESS) + { + if (code == MOVIE_FILE_NOT_FOUND) + systemMessage(0, "Could not find movie file \"%s\".", (const char *)movieLogicalName); + else if (code == MOVIE_WRONG_FORMAT) + systemMessage(0, "Movie file \"%s\" is not in proper VBM format.", (const char *)movieLogicalName); + else if (code == MOVIE_WRONG_VERSION) + systemMessage(0, "Movie file \"%s\" is not a supported version.", (const char *)movieLogicalName); + else + systemMessage(0, "Failed to open movie \"%s\".", (const char *)movieLogicalName); + return; + } + else + { + // get author and movie info from the edit fields (the description might change): + char info[MOVIE_METADATA_SIZE], buffer[MOVIE_METADATA_SIZE]; + + GetDlgItem(IDC_EDIT_AUTHOR)->GetWindowText(buffer, MOVIE_METADATA_AUTHOR_SIZE); + strncpy(info, buffer, MOVIE_METADATA_AUTHOR_SIZE); + info[MOVIE_METADATA_AUTHOR_SIZE - 1] = '\0'; + + GetDlgItem(IDC_EDIT_DESCRIPTION)->GetWindowText(buffer, MOVIE_METADATA_SIZE - MOVIE_METADATA_AUTHOR_SIZE); + strncpy(info + MOVIE_METADATA_AUTHOR_SIZE, buffer, MOVIE_METADATA_SIZE - MOVIE_METADATA_AUTHOR_SIZE); + info[MOVIE_METADATA_SIZE - 1] = '\0'; + + VBAMovieSetMetadata(info); + } + + if (IsDlgButtonChecked(IDC_CHECK_PAUSEFRAME)) + { + char buffer [9]; + GetDlgItem(IDC_EDIT_PAUSEFRAME)->GetWindowText(buffer, 8); + buffer[8] = '\0'; + + int pauseFrame = atoi(buffer); + if (pauseFrame >= 0) + VBAMovieSetPauseAt(pauseFrame); + } + + OnOK(); +} + +void MovieOpen::OnBnClickedCancel() +{ + OnCancel(); +} + +void MovieOpen::OnBnClickedReadonly() +{ + theApp.movieReadOnly = IsDlgButtonChecked(IDC_READONLY) != 0; + m_editDescription.SetReadOnly(theApp.movieReadOnly); + OnBnClickedMovieRefresh(); +} + +void MovieOpen::OnBnClickedCheckPauseframe() +{ + if (IsDlgButtonChecked(IDC_CHECK_PAUSEFRAME)) + { + char buffer [8]; + _itoa(movieInfo.header.length_frames, buffer, 10); + GetDlgItem(IDC_EDIT_PAUSEFRAME)->SetWindowText(buffer); + GetDlgItem(IDC_EDIT_PAUSEFRAME)->EnableWindow(TRUE); + } + else + { + GetDlgItem(IDC_EDIT_PAUSEFRAME)->SetWindowText(""); + GetDlgItem(IDC_EDIT_PAUSEFRAME)->EnableWindow(FALSE); + } +} + +void MovieOpen::OnBnClickedHideborder() +{ + theApp.hideMovieBorder = IsDlgButtonChecked(IDC_CHECK_HIDEBORDER) != 0; +} + +void MovieOpen::OnEnChangeMovieFilename() +{ + OnBnClickedMovieRefresh(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/MovieOpen.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/MovieOpen.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,58 @@ +#ifndef VBA_MOVIE_OPEN_H +#define VBA_MOVIE_OPEN_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../common/movie.h" + +// MovieOpen dialog + +class MovieOpen : public CDialog +{ + DECLARE_DYNAMIC(MovieOpen) +public: + MovieOpen(CWnd*pParent = NULL); // standard constructor + virtual ~MovieOpen(); + +// Dialog Data + //{{AFX_DATA(MovieOpen) + enum { IDD = IDD_MOVIEOPEN }; + CEdit m_editAuthor; + CEdit m_editDescription; + CEdit m_editFilename; + CEdit m_editPauseFrame; + CString moviePhysicalName; + CString movieLogicalName; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(GBACheatList) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + SMovie movieInfo; + int pauseFrame; + + // Generated message map functions + //{{AFX_MSG(GBACheatList) + afx_msg void OnBnClickedBrowse(); + afx_msg void OnBnClickedOk(); + afx_msg void OnBnClickedCancel(); + afx_msg void OnBnClickedMovieRefresh(); + afx_msg void OnBnClickedReadonly(); + afx_msg void OnBnClickedCheckPauseframe(); + afx_msg void OnBnClickedHideborder(); + afx_msg void OnEnChangeMovieFilename(); + virtual BOOL OnInitDialog() ; + virtual BOOL OnWndMsg(UINT, WPARAM, LPARAM, LRESULT *); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +#endif // VBA_MOVIE_OPEN_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/OamView.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/OamView.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,668 @@ +// OamView.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "OamView.h" +#include "FileDlg.h" +#include "Reg.h" +#include "WinResUtil.h" +#include "VBA.h" + +#include "../gba/GBAGlobals.h" +#include "../NLS.h" +#include "../common/Util.h" + +extern "C" { +#include +} + +///////////////////////////////////////////////////////////////////////////// +// OamView dialog + +OamView::OamView(CWnd*pParent /*=NULL*/) + : ResizeDlg(OamView::IDD, pParent) +{ + //{{AFX_DATA_INIT(OamView) + m_stretch = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 32; + bmpInfo.bmiHeader.biHeight = 32; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 64 * 64); + + oamView.setData(data); + oamView.setBmpInfo(&bmpInfo); + + number = 0; +} + +void OamView::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(OamView) + DDX_Control(pDX, IDC_SPRITE, m_sprite); + DDX_Check(pDX, IDC_STRETCH, m_stretch); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_COLOR, color); + DDX_Control(pDX, IDC_OAM_VIEW, oamView); + DDX_Control(pDX, IDC_OAM_VIEW_ZOOM, oamZoom); +} + +BEGIN_MESSAGE_MAP(OamView, CDialog) +//{{AFX_MSG_MAP(OamView) +ON_BN_CLICKED(IDC_SAVE, OnSave) +ON_BN_CLICKED(IDC_STRETCH, OnStretch) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_EN_CHANGE(IDC_SPRITE, OnChangeSprite) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_WM_HSCROLL() +//}}AFX_MSG_MAP +ON_MESSAGE(WM_MAPINFO, OnMapInfo) +ON_MESSAGE(WM_COLINFO, OnColInfo) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// OamView message handlers + +OamView::~OamView() +{ + free(data); + data = NULL; +} + +void OamView::paint() +{ + if (oam == NULL || paletteRAM == NULL || vram == NULL) + return; + + render(); + oamView.setSize(w, h); + oamView.refresh(); +} + +void OamView::update() +{ + paint(); +} + +void OamView::setAttributes(u16 a0, u16 a1, u16 a2) +{ + CString buffer; + + int y = a0 & 255; + int rot = a0 & 512; + int mode = (a0 >> 10) & 3; + int mosaic = a0 & 4096; + int color = a0 & 8192; + int duple = a0 & 1024; + int shape = (a0 >> 14) & 3; + int x = a1 & 511; + int rotParam = (a1 >> 9) & 31; + int flipH = a1 & 4096; + int flipV = a1 & 8192; + int size = (a1 >> 14) & 3; + int tile = a2 & 1023; + int prio = (a2 >> 10) & 3; + int pal = (a2 >> 12) & 15; + + buffer.Format("%d,%d", x, y); + GetDlgItem(IDC_POS)->SetWindowText(buffer); + + buffer.Format("%d", mode); + GetDlgItem(IDC_MODE)->SetWindowText(buffer); + + GetDlgItem(IDC_COLORS)->SetWindowText(color ? "256" : "16"); + + buffer.Format("%d", pal); + GetDlgItem(IDC_PALETTE)->SetWindowText(buffer); + + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE)->SetWindowText(buffer); + + buffer.Format("%d", prio); + GetDlgItem(IDC_PRIO)->SetWindowText(buffer); + + buffer.Format("%d,%d", w, h); + GetDlgItem(IDC_SIZE2)->SetWindowText(buffer); + + if (rot) + { + buffer.Format("%d", rotParam); + } + else + buffer.Empty(); + GetDlgItem(IDC_ROT)->SetWindowText(buffer); + + buffer.Empty(); + + if (rot) + buffer += 'R'; + else + buffer += ' '; + if (!rot) + { + if (flipH) + buffer += 'H'; + else + buffer += ' '; + if (flipV) + buffer += 'V'; + else + buffer += ' '; + } + else + { + buffer += ' '; + buffer += ' '; + } + if (mosaic) + buffer += 'M'; + else + buffer += ' '; + if (duple) + buffer += 'D'; + else + buffer += ' '; + + GetDlgItem(IDC_FLAGS)->SetWindowText(buffer); +} + +void OamView::render() +{ + int m = 0; + if (oam == NULL || paletteRAM == NULL || vram == NULL) + return; + + u16 *sprites = &((u16 *)oam)[4*number]; + u16 *spritePalette = &((u16 *)paletteRAM)[0x100]; + u8 * bmp = data; + + u16 a0 = *sprites++; + u16 a1 = *sprites++; + u16 a2 = *sprites++; + + int sizeY = 8; + int sizeX = 8; + + switch (((a0 >>12) & 0x0c)|(a1>>14)) + { + case 0: + break; + case 1: + sizeX = sizeY = 16; + break; + case 2: + sizeX = sizeY = 32; + break; + case 3: + sizeX = sizeY = 64; + break; + case 4: + sizeX = 16; + break; + case 5: + sizeX = 32; + break; + case 6: + sizeX = 32; + sizeY = 16; + break; + case 7: + sizeX = 64; + sizeY = 32; + break; + case 8: + sizeY = 16; + break; + case 9: + sizeY = 32; + break; + case 10: + sizeX = 16; + sizeY = 32; + break; + case 11: + sizeX = 32; + sizeY = 64; + break; + default: + return; + } + + w = sizeX; + h = sizeY; + + setAttributes(a0, a1, a2); + + int sy = (a0 & 255); + + if (a0 & 0x2000) + { + int c = (a2 & 0x3FF); + // if((DISPCNT & 7) > 2 && (c < 512)) + // return; + int inc = 32; + if (DISPCNT & 0x40) + inc = sizeX >> 2; + else + c &= 0x3FE; + + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u32 color = vram[0x10000 + (((c + (y>>3) * inc)* + 32 + (y & 7) * 8 + (x >> 3) * 64 + + (x & 7))&0x7FFF)]; + color = spritePalette[color]; + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + } + } + else + { + int c = (a2 & 0x3FF); + // if((DISPCNT & 7) > 2 && (c < 512)) + // continue; + + int inc = 32; + if (DISPCNT & 0x40) + inc = sizeX >> 3; + int palette = (a2 >> 8) & 0xF0; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + u32 color = vram[0x10000 + (((c + (y>>3) * inc)* + 32 + (y & 7) * 4 + (x >> 3) * 32 + + ((x & 7)>>1))&0x7FFF)]; + if (x & 1) + color >>= 4; + else + color &= 0x0F; + + color = spritePalette[palette+color]; + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + } + } +} + +void OamView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct + { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + +void OamView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if (!png_ptr) + { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + if (setjmp(png_ptr->jmpbuf)) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr, fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void OamView::OnSave() +{ + CString captureBuffer; + + if (theApp.captureFormat == 0) + captureBuffer = "oam.png"; + else + captureBuffer = "oam.bmp"; + + LPCTSTR exts[] = {".png", ".bmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + captureBuffer = dlg.GetPathName(); + + if (dlg.getFilterIndex() == 2) + saveBMP(captureBuffer); + else + savePNG(captureBuffer); +} + +BOOL OamView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_OAM_VIEW, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_OAM_VIEW_ZOOM, DS_MoveX) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_B, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\OamView", + NULL); + + m_sprite.SetWindowText("0"); + + updateScrollInfo(); + + m_stretch = regQueryDwordValue("oamViewStretch", 0); + if (m_stretch) + oamView.setStretch(true); + UpdateData(FALSE); + + paint(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void OamView::OnStretch() +{ + oamView.setStretch(!oamView.getStretch()); + paint(); + regSetDwordValue("oamViewStretch", oamView.getStretch()); +} + +void OamView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void OamView::OnChangeSprite() +{ + CString buffer; + m_sprite.GetWindowText(buffer); + int n = atoi(buffer); + if (n < 0 || n > 127) + { + buffer.Format("%d", number); + m_sprite.SetWindowText(buffer); + return; + } + number = n; + paint(); + updateScrollInfo(); +} + +void OamView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +LRESULT OamView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + oamZoom.setColors(colors); + + return TRUE; +} + +LRESULT OamView::OnColInfo(WPARAM wParam, LPARAM lParam) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void OamView::updateScrollInfo() +{ + SCROLLINFO si; + ZeroMemory(&si, sizeof(si)); + si.cbSize = sizeof(si); + si.fMask = SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL | SIF_POS; + si.nMin = 0; + si.nMax = 127; + si.nPage = 1; + si.nPos = number; + GetDlgItem(IDC_SCROLLBAR)->SetScrollInfo(SB_CTL, + &si, + TRUE); +} + +void OamView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar) +{ + switch (nSBCode) + { + case SB_BOTTOM: + number = 127; + break; + case SB_LINEDOWN: + number++; + if (number > 127) + number = 127; + break; + case SB_LINEUP: + number--; + if (number < 0) + number = 0; + break; + case SB_PAGEDOWN: + number += 16; + if (number > 127) + number = 127; + break; + case SB_PAGEUP: + number -= 16; + if (number < 0) + number = 0; + break; + case SB_TOP: + number = 0; + break; + case SB_THUMBTRACK: + number = nPos; + if (number < 0) + number = 0; + if (number > 127) + number = 127; + break; + } + + updateScrollInfo(); + + CString buffer; + buffer.Format("%d", number); + m_sprite.SetWindowText(buffer); + paint(); +} + +void OamView::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/OamView.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/OamView.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,78 @@ +#if !defined(AFX_OAMVIEW_H__E5369352_80F8_49C4_9F23_05EB6FC1345B__INCLUDED_) +#define AFX_OAMVIEW_H__E5369352_80F8_49C4_9F23_05EB6FC1345B__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// OamView.h : header file +// +#include "BitmapControl.h" +#include "ZoomControl.h" +#include "ColorControl.h" +#include "IUpdate.h" +#include "ResizeDlg.h" + +///////////////////////////////////////////////////////////////////////////// +// OamView dialog + +class OamView : public ResizeDlg, IUpdateListener +{ +private: + BITMAPINFO bmpInfo; + u8 * data; + int w; + int h; + int number; + bool autoUpdate; + BitmapControl oamView; + ZoomControl oamZoom; + ColorControl color; + + // Construction +public: + void updateScrollInfo(); + afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + void savePNG(const char *name); + void saveBMP(const char *name); + void render(); + void setAttributes(u16 a0, u16 a1, u16 a2); + void paint(); + ~OamView(); + OamView(CWnd*pParent = NULL); // standard constructor + + virtual void update(); + // Dialog Data + //{{AFX_DATA(OamView) + enum { IDD = IDD_OAM_VIEW }; + CEdit m_sprite; + BOOL m_stretch; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(OamView) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(OamView) + afx_msg void OnSave(); + virtual BOOL OnInitDialog(); + afx_msg void OnStretch(); + afx_msg void OnAutoUpdate(); + afx_msg void OnChangeSprite(); + afx_msg void OnClose(); + afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_OAMVIEW_H__E5369352_80F8_49C4_9F23_05EB6FC1345B__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/OpenGL.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/OpenGL.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,544 @@ +#include "stdafx.h" +#include + +#include "resource.h" +#include "MainWnd.h" +#include "Reg.h" +#include "VBA.h" + +#include "../gba/GBAGlobals.h" +#include "../gb/gbGlobals.h" +#include "../common/Text.h" +#include "../version.h" + +#ifdef MMX +extern "C" bool cpu_mmx; + +extern bool detectMMX(); +#endif + +extern int systemSpeed; +extern int Init_2xSaI(u32); +extern void winlog(const char *, ...); + +class OpenGLDisplay : public IDisplay +{ +private: + HDC hDC; + HGLRC hglrc; + GLuint texture; + int width; + int height; + float size; + u8 * filterData; + bool failed; + + bool initializeTexture(int w, int h); + void updateFiltering(int); +public: + OpenGLDisplay(); + virtual ~OpenGLDisplay(); + + virtual bool initialize(); + virtual void cleanup(); + virtual void render(); + virtual void checkFullScreen(); + virtual void renderMenu(); + virtual void clear(); + virtual bool changeRenderSize(int w, int h); + virtual void resize(int w, int h); + virtual DISPLAY_TYPE getType() { return OPENGL; }; + virtual void setOption(const char *, int); + virtual int selectFullScreenMode(GUID * *); +}; + +OpenGLDisplay::OpenGLDisplay() +{ + hDC = NULL; + hglrc = NULL; + texture = 0; + width = 0; + height = 0; + size = 0.0f; + filterData = (u8 *)malloc(4*16*256*256); // sufficient for 4x filters @ 32bit color depth + failed = false; +} + +OpenGLDisplay::~OpenGLDisplay() +{ + cleanup(); +} + +void OpenGLDisplay::cleanup() +{ + if (texture != 0) + { + glDeleteTextures(1, &texture); + texture = 0; + } + if (hglrc != NULL) + { + wglDeleteContext(hglrc); + wglMakeCurrent(NULL, NULL); + hglrc = NULL; + } + if (hDC != NULL) + { + ReleaseDC(*theApp.m_pMainWnd, hDC); + hDC = NULL; + } + if (filterData) + { + free(filterData); + filterData = NULL; + } + width = 0; + height = 0; + size = 0.0f; +} + +bool OpenGLDisplay::initialize() +{ + CWnd *pWnd = theApp.m_pMainWnd; + + theApp.mode320Available = false; + theApp.mode640Available = false; + theApp.mode800Available = false; + + CDC *dc = pWnd->GetDC(); + HDC hDC = dc->GetSafeHdc(); + + PIXELFORMATDESCRIPTOR pfd = { + sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd + 1, // version number + PFD_DRAW_TO_WINDOW | // support window + PFD_SUPPORT_OPENGL | // support OpenGL + PFD_DOUBLEBUFFER, // double buffered + PFD_TYPE_RGBA, // RGBA type + 16, // 16-bit color depth + 0, 0, 0, 0, 0, 0, // color bits ignored + 0, // no alpha buffer + 0, // shift bit ignored + 0, // no accumulation buffer + 0, 0, 0, 0, // accum bits ignored + 32, // 32-bit z-buffer + 0, // no stencil buffer + 0, // no auxiliary buffer + PFD_MAIN_PLANE, // main layer + 0, // reserved + 0, 0, 0 // layer masks ignored + }; + int iPixelFormat; + + if (!(iPixelFormat = ChoosePixelFormat(hDC, &pfd))) + { + winlog("Failed ChoosePixelFormat\n"); + return false; + } + + // obtain detailed information about + // the device context's first pixel format + if (!(DescribePixelFormat(hDC, iPixelFormat, + sizeof(PIXELFORMATDESCRIPTOR), &pfd))) + { + winlog("Failed DescribePixelFormat\n"); + return false; + } + + if (!SetPixelFormat(hDC, iPixelFormat, &pfd)) + { + winlog("Failed SetPixelFormat\n"); + return false; + } + + if (!(hglrc = wglCreateContext(hDC))) + { + winlog("Failed wglCreateContext\n"); + return false; + } + + if (!wglMakeCurrent(hDC, hglrc)) + { + winlog("Failed wglMakeCurrent\n"); + return false; + } + pWnd->ReleaseDC(dc); + + // setup 2D gl environment + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_TEXTURE_2D); + + glViewport(0, 0, theApp.surfaceSizeX, theApp.surfaceSizeY); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho(0.0, (GLdouble)(theApp.surfaceSizeX), (GLdouble)(theApp.surfaceSizeY), + 0.0, 0.0, 1.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + systemRedShift = 3; + systemGreenShift = 11; + systemBlueShift = 19; + systemColorDepth = 32; + theApp.fsColorDepth = 32; + + Init_2xSaI(32); +#ifdef MMX + if (!theApp.disableMMX) + cpu_mmx = theApp.detectMMX(); + else + cpu_mmx = 0; +#endif + + if (theApp.ddrawDebug) + { + winlog("R shift: %d\n", systemRedShift); + winlog("G shift: %d\n", systemGreenShift); + winlog("B shift: %d\n", systemBlueShift); + } + + switch (systemColorDepth) + { + case 16: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + case 24: + case 32: + { + for (int i = 0; i < 0x10000; i++) + { + systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | + (((i & 0x3e0) >> 5) << systemGreenShift) | + (((i & 0x7c00) >> 10) << systemBlueShift); + } + break; + } + } + theApp.updateFilter(); + theApp.updateIFB(); + + if (failed) + return false; + + pWnd->DragAcceptFiles(TRUE); + + return TRUE; +} + +void OpenGLDisplay::clear() +{} + +void OpenGLDisplay::renderMenu() +{ + checkFullScreen(); + if (theApp.m_pMainWnd) + theApp.m_pMainWnd->DrawMenuBar(); +} + +void OpenGLDisplay::checkFullScreen() +{ + // if(tripleBuffering) + // pOpenGL->FlipToGDISurface(); +} + +void OpenGLDisplay::render() +{ + void (*filterFunction)(u8 *, u32, u8 *, u8 *, u32, int, int) = theApp.filterFunction; + int filterWidth = theApp.filterWidth, filterHeight = theApp.filterHeight; +/* + if(textMethod == 1) + { + int copyX = 240, copyY = 160; + if(systemCartridgeType == 1) + if(gbBorderOn) copyX = 256, copyY = 224; + else copyX = 160, copyY = 144; + + extern void Simple1x(u8*,u32,u8*,u8*,u32,int,int); + filterFunction = Simple1x; + filterWidth = copyX; + filterHeight = copyY; + } + */ + int pitch = filterWidth * 4 + 4; + u8 *data = pix + (theApp.sizeX+1)*4; + + int filterPitch = theApp.rect.right*4; + +/* + // HACK: see below + if (textMethod == 1 && !filterFunction) + { + textMethod = 0; // must not be after systemMessage! + systemMessage( + 0, + "The \"On Game\" text display mode does not work with this combination of renderers and filters.\nThe display mode is automatically being changed to \"In Game\" instead,\nbut this may cause message text to go into AVI recordings and screenshots.\nThis can be reconfigured by choosing \"Options->Video->Text Display Options...\""); + } +*/ + + if (filterFunction) + { + data = filterData; + filterFunction(pix + pitch, + pitch, + (u8 *)theApp.delta, + (u8 *)filterData, + filterPitch, + filterWidth, + filterHeight); + } + + if (theApp.showSpeed && theApp.videoOption > VIDEO_4X) + { + char buffer[30]; + if (theApp.showSpeed == 1) + sprintf(buffer, "%3d%%", systemSpeed); + else + sprintf(buffer, "%3d%%(%d, %d fps)", systemSpeed, + systemFrameSkip, + theApp.showRenderedFrames); + + if (filterFunction) + { + if (theApp.showSpeedTransparent) + drawTextTransp((u8 *)filterData, + filterPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8 *)filterData, + filterPitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + } + else + { + if (theApp.showSpeedTransparent) + drawTextTransp((u8 *)pix, + pitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + else + drawText((u8 *)pix, + pitch, + theApp.rect.left+10, + theApp.rect.bottom-10, + buffer); + } + } + + if (textMethod == 1 && filterFunction) + { + DrawTextMessages((u8 *)filterData, filterPitch, theApp.rect.left, theApp.rect.bottom); + } + + // Texturemap complete texture to surface so we have free scaling + // and antialiasing + if (filterFunction) + { + glPixelStorei(GL_UNPACK_ROW_LENGTH, theApp.rect.right); + } + else + { + glPixelStorei(GL_UNPACK_ROW_LENGTH, theApp.sizeX+1); + } + + glTexSubImage2D(GL_TEXTURE_2D, 0, + 0, 0, theApp.rect.right, theApp.rect.bottom, + GL_RGBA, GL_UNSIGNED_BYTE, data); + + if (theApp.glType == 0) + { + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.0, 0.0); glVertex3i(0, 0, 0); + glTexCoord2f(theApp.rect.right/size, 0.0); glVertex3i(theApp.surfaceSizeX, 0, 0); + glTexCoord2f(0.0, theApp.rect.bottom/size); glVertex3i(0, theApp.surfaceSizeY, 0); + glTexCoord2f(theApp.rect.right/size, theApp.rect.bottom/size); glVertex3i(theApp.surfaceSizeX, theApp.surfaceSizeY, 0); + glEnd(); + } + else + { + glBegin(GL_QUADS); + glTexCoord2f(0.0, 0.0); glVertex3i(0, 0, 0); + glTexCoord2f(theApp.rect.right/size, 0.0); glVertex3i(theApp.surfaceSizeX, 0, 0); + glTexCoord2f(theApp.rect.right/size, theApp.rect.bottom/size); glVertex3i(theApp.surfaceSizeX, theApp.surfaceSizeY, 0); + glTexCoord2f(0.0, theApp.rect.bottom/size); glVertex3i(0, theApp.surfaceSizeY, 0); + glEnd(); + } + + CDC *dc = theApp.m_pMainWnd->GetDC(); + + if (textMethod == 2 || (textMethod == 1 && !filterFunction)) // HACK: so that textMethod isn't changed + { + for (int slot = 0; slot < SCREEN_MESSAGE_SLOTS; slot++) + { + if (theApp.screenMessage[slot]) + { + if ((theApp.screenMessageDuration[slot] < 0 || + (int)(GetTickCount() - theApp.screenMessageTime[slot]) < theApp.screenMessageDuration[slot]) && + (!theApp.disableStatusMessage || slot == 1 || slot == 2)) + { + dc->SetBkMode(TRANSPARENT); + + if (outlinedText) + { + dc->SetTextColor(textColor != 7 ? RGB(0, 0, 0) : RGB(255, 255, 255)); + // draw black outline + const static int xd [8] = {-1, 0, 1, 1, 1, 0, -1, -1}; + const static int yd [8] = {-1, -1, -1, 0, 1, 1, 1, 0}; + for (int i = 0; i < 8; i++) + { + dc->TextOut(10+xd[i], theApp.surfaceSizeY - 20*(slot+1)+yd[i], theApp.screenMessageBuffer[slot]); + } + } + + COLORREF color; + switch (textColor) + { + case 0: + color = RGB(255, 255, 255); break; + case 1: + color = RGB(255, 0, 0); break; + case 2: + color = RGB(255, 255, 0); break; + case 3: + color = RGB(0, 255, 0); break; + case 4: + color = RGB(0, 255, 255); break; + case 5: + color = RGB(0, 0, 255); break; + case 6: + color = RGB(255, 0, 255); break; + case 7: + color = RGB(0, 0, 0); break; + } + dc->SetTextColor(color); + + // draw center text + dc->TextOut(10, theApp.surfaceSizeY - 20*(slot+1), theApp.screenMessageBuffer[slot]); + } + else + { + theApp.screenMessage[slot] = false; + } + } + } + } + + SwapBuffers(dc->GetSafeHdc()); + + theApp.m_pMainWnd->ReleaseDC(dc); +} + +void OpenGLDisplay::resize(int w, int h) +{ + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho(0.0, (GLdouble)(w), (GLdouble)(h), 0.0, 0.0, 1.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +void OpenGLDisplay::updateFiltering(int value) +{ + switch (value) + { + case 0: + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + break; + case 1: + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + break; + } +} + +bool OpenGLDisplay::initializeTexture(int w, int h) +{ + int mySize = 256; + size = 256.0f; + if (w > 511 || h > 511) + { + size = 1024.0f; + mySize = 1024; + } + else if (w > 255 || h > 255) + { + size = 512.0f; + mySize = 512; + } + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + + int filter = regQueryDwordValue("glFilter", 0); + if (filter < 0 || filter > 1) + filter = 0; + updateFiltering(filter); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mySize, mySize, 0, GL_RGBA, + GL_UNSIGNED_BYTE, NULL); + width = w; + height = h; + + return true; +} + +bool OpenGLDisplay::changeRenderSize(int w, int h) +{ + if (width != w || height != h) + { + if (texture != 0) + { + glDeleteTextures(1, &texture); + texture = 0; + } + if (!initializeTexture(w, h)) + { + failed = true; + return false; + } + } + return true; +} + +void OpenGLDisplay::setOption(const char *option, int value) +{ + if (!strcmp(option, "glFilter")) + updateFiltering(value); +} + +int OpenGLDisplay::selectFullScreenMode(GUID * *) +{ + HWND wnd = GetDesktopWindow(); + RECT r; + GetWindowRect(wnd, &r); + int w = (r.right - r.left) & 4095; + int h = (r.bottom - r.top) & 4095; + HDC dc = GetDC(wnd); + int c = GetDeviceCaps(dc, BITSPIXEL); + ReleaseDC(wnd, dc); + + return (c << 24) | (w << 12) | h; +} + +IDisplay *newOpenGLDisplay() +{ + return new OpenGLDisplay(); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/PaletteView.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/PaletteView.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,217 @@ +// PaletteView.cpp : implementation file +// + +#include "stdafx.h" +#include "FileDlg.h" +#include "PaletteView.h" +#include "WinResUtil.h" +#include "VBA.h" // for theApp + +#include "../gba/GBAGlobals.h" + +void GBAPaletteViewControl::updatePalette() +{ + if (paletteRAM != NULL) + memcpy(palette, &paletteRAM[paletteAddress], 512); +} + +///////////////////////////////////////////////////////////////////////////// +// PaletteView dialog + +PaletteView::PaletteView(CWnd*pParent /*=NULL*/) + : ResizeDlg(PaletteView::IDD, pParent) +{ + //{{AFX_DATA_INIT(PaletteView) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + autoUpdate = false; +} + +PaletteView::~PaletteView() +{} + +void PaletteView::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(PaletteView) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_PALETTE_VIEW, paletteView); + DDX_Control(pDX, IDC_PALETTE_VIEW_OBJ, paletteViewOBJ); + DDX_Control(pDX, IDC_COLOR, colorControl); +} + +BEGIN_MESSAGE_MAP(PaletteView, CDialog) +//{{AFX_MSG_MAP(PaletteView) +ON_BN_CLICKED(IDC_SAVE_BG, OnSaveBg) +ON_BN_CLICKED(IDC_SAVE_OBJ, OnSaveObj) +ON_BN_CLICKED(IDC_REFRESH2, OnRefresh2) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +//}}AFX_MSG_MAP +ON_MESSAGE(WM_PALINFO, OnPalInfo) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// PaletteView message handlers + +BOOL PaletteView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_END() + SetData(sz, + FALSE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\PaletteView", + NULL); + + paletteView.setPaletteAddress(0); + paletteView.refresh(); + + paletteViewOBJ.setPaletteAddress(0x200); + paletteViewOBJ.refresh(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void PaletteView::save(int which) +{ + CString captureBuffer; + + if (which == 0) + captureBuffer = "bg.pal"; + else + captureBuffer = "obj.pal"; + + LPCTSTR exts[] = {".pal", ".pal", ".act", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PAL); + CString title = winResLoadString(IDS_SELECT_PALETTE_NAME); + FileDlg dlg(this, + captureBuffer, + filter, + 1, + "PAL", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + PaletteViewControl *p = NULL; + + if (which == 0) + p = &paletteView; + else + p = &paletteViewOBJ; + + switch (dlg.getFilterIndex()) + { + case 0: + case 1: + p->saveMSPAL(captureBuffer); + break; + case 2: + p->saveJASCPAL(captureBuffer); + break; + case 3: + p->saveAdobe(captureBuffer); + break; + } +} + +void PaletteView::OnSaveBg() +{ + save(0); +} + +void PaletteView::OnSaveObj() +{ + save(1); +} + +void PaletteView::OnRefresh2() +{ + paletteView.refresh(); + paletteViewOBJ.refresh(); +} + +void PaletteView::update() +{ + OnRefresh2(); +} + +void PaletteView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void PaletteView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +LRESULT PaletteView::OnPalInfo(WPARAM wParam, LPARAM lParam) +{ + u16 color = (u16)wParam; + u32 address = (u32)lParam; + CString buffer; + + if (address >= 0x200) + address = 0x5000200 + 2*(address & 255); + else + address = 0x5000000 + 2*(address & 255); + + buffer.Format("0x%08X", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + int r = (color & 0x1f); + int g = (color & 0x3e0) >> 5; + int b = (color & 0x7c00) >> 10; + + buffer.Format("%d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("%d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("%d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + buffer.Format("0x%04X", color); + GetDlgItem(IDC_VALUE)->SetWindowText(buffer); + + colorControl.setColor(color); + + if (address >= 0x5000200) + { + paletteView.setSelected(-1); + } + else + paletteViewOBJ.setSelected(-1); + + return TRUE; +} + +void PaletteView::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/PaletteView.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/PaletteView.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,73 @@ +#if !defined(AFX_PALETTEVIEW1_H__0873E3FF_9486_4B2C_8EF0_59C3B4F47162__INCLUDED_) +#define AFX_PALETTEVIEW1_H__0873E3FF_9486_4B2C_8EF0_59C3B4F47162__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// PaletteView.h : header file +// + +#include "IUpdate.h" +#include "resource.h" +#include "ResizeDlg.h" +#include "ColorControl.h" +#include "PaletteViewControl.h" + +class GBAPaletteViewControl : public PaletteViewControl +{ +public: + virtual void updatePalette(); +}; + +///////////////////////////////////////////////////////////////////////////// +// PaletteView dialog + +class PaletteView : public ResizeDlg, IUpdateListener +{ +private: + GBAPaletteViewControl paletteView; + GBAPaletteViewControl paletteViewOBJ; + ColorControl colorControl; + bool autoUpdate; + // Construction +public: + void save(int which); + PaletteView(CWnd*pParent = NULL); // standard constructor + ~PaletteView(); + afx_msg LRESULT OnPalInfo(WPARAM wParam, LPARAM lParam); + + // Dialog Data + //{{AFX_DATA(PaletteView) + enum { IDD = IDD_PALETTE_VIEW }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(PaletteView) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + virtual void update(); + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(PaletteView) + virtual BOOL OnInitDialog(); + afx_msg void OnSaveBg(); + afx_msg void OnSaveObj(); + afx_msg void OnRefresh2(); + afx_msg void OnAutoUpdate(); + afx_msg void OnClose(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_PALETTEVIEW1_H__0873E3FF_9486_4B2C_8EF0_59C3B4F47162__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/PaletteViewControl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/PaletteViewControl.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,384 @@ +// PaletteViewControl.cpp : implementation file +// + +#include "stdafx.h" +#include "PaletteViewControl.h" + +#include "../common/Util.h" + +bool PaletteViewControl::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// PaletteViewControl + +PaletteViewControl::PaletteViewControl() +{ + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 256; + bmpInfo.bmiHeader.biHeight = -256; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)malloc(3 * 256 * 256); + + w = 256; + h = 256; + + colors = 256; + + paletteAddress = 0; + + ZeroMemory(palette, 512); + + selected = -1; + registerClass(); +} + +PaletteViewControl::~PaletteViewControl() +{ + if(data) + free(data); +} + + +BEGIN_MESSAGE_MAP(PaletteViewControl, CWnd) + //{{AFX_MSG_MAP(PaletteViewControl) + ON_WM_LBUTTONDOWN() + ON_WM_ERASEBKGND() + ON_WM_PAINT() + //}}AFX_MSG_MAP + END_MESSAGE_MAP() + + + ///////////////////////////////////////////////////////////////////////////// +// PaletteViewControl message handlers + +void PaletteViewControl::init(int c, int w, int h) +{ + this->w = w; + this->h = h; + this->colors = c; + + bmpInfo.bmiHeader.biWidth = w; + bmpInfo.bmiHeader.biHeight = -h; +} + + +bool PaletteViewControl::saveAdobe(const char *name) +{ + FILE *f = fopen(name, "wb"); + + if(!f) + return false; + + for(int i = 0; i < colors; i++) { + u16 c = palette[i]; + int r = (c & 0x1f) << 3; + int g = (c & 0x3e0) >> 2; + int b = (c & 0x7c00) >> 7; + + u8 data[3] = { r, g, b }; + fwrite(data, 1, 3, f); + } + if(colors < 256) { + for(int i = colors; i < 256; i++) { + u8 data[3] = { 0, 0, 0 }; + fwrite(data, 1, 3, f); + } + } + fclose(f); + + return true; +} + + +bool PaletteViewControl::saveMSPAL(const char *name) +{ + FILE *f = fopen(name, "wb"); + + if(!f) + return false; + + u8 data[4] = { 'R', 'I', 'F', 'F' }; + + fwrite(data, 1, 4, f); + utilPutDword(data, 256 * 4 + 16); + fwrite(data, 1, 4, f); + u8 data3[4] = { 'P', 'A', 'L', ' ' }; + fwrite(data3, 1, 4, f); + u8 data4[4] = { 'd', 'a', 't', 'a' }; + fwrite(data4, 1, 4, f); + utilPutDword(data, 256*4+4); + fwrite(data, 1, 4, f); + utilPutWord(&data[0], 0x0300); + utilPutWord(&data[2], 256); // causes problems if not 16 or 256 + fwrite(data, 1, 4, f); + + for(int i = 0; i < colors; i++) { + u16 c = palette[i]; + int r = (c & 0x1f) << 3; + int g = (c & 0x3e0) >> 2; + int b = (c & 0x7c00) >> 7; + + u8 data7[4] = { r, g, b, 0 }; + fwrite(data7, 1, 4, f); + } + if(colors < 256) { + for(int i = colors; i < 256; i++) { + u8 data7[4] = { 0, 0, 0, 0 }; + fwrite(data7, 1, 4, f); + } + } + fclose(f); + + return true; +} + + +bool PaletteViewControl::saveJASCPAL(const char *name) +{ + FILE *f = fopen(name, "wb"); + + if(!f) + return false; + + fprintf(f, "JASC-PAL\r\n0100\r\n256\r\n"); + + for(int i = 0; i < colors; i++) { + u16 c = palette[i]; + int r = (c & 0x1f) << 3; + int g = (c & 0x3e0) >> 2; + int b = (c & 0x7c00) >> 7; + + fprintf(f, "%d %d %d\r\n", r, g, b); + } + if(colors < 256) { + for(int i = colors; i < 256; i++) + fprintf(f, "0 0 0\r\n"); + } + fclose(f); + + return true; +} + +void PaletteViewControl::setPaletteAddress(int address) +{ + paletteAddress = address; +} + + +void PaletteViewControl::setSelected(int s) +{ + selected = s; + InvalidateRect(NULL, FALSE); +} + + +void PaletteViewControl::render(u16 color, int x, int y) +{ + u8 *start = data + y*16*w*3 + x*16*3; + int skip = w*3-16*3; + + int r = (color & 0x1f) << 3; + int g = (color & 0x3e0) >> 2; + int b = (color & 0x7c00) >> 7; + + for(int i = 0; i < 16; i++) { + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + *start++ = b; + *start++ = g; + *start++ = r; + + start += skip; + } +} + +void PaletteViewControl::refresh() +{ + updatePalette(); + int sw = w/16; + int sh = h/16; + for(int i = 0; i < colors; i++) { + render(palette[i], i & (sw-1), i/sw); + } + InvalidateRect(NULL, FALSE); +} + +void PaletteViewControl::OnLButtonDown(UINT nFlags, CPoint point) +{ + int x = point.x; + int y = point.y; + RECT rect; + GetClientRect(&rect); + int h = rect.bottom - rect.top; + int w = rect.right - rect.left; + int sw = (this->w/16); + int sh = (this->h/16); + int mult = w / sw; + int multY = h / sh; + + setSelected(x/mult + (y/multY)*sw); + + GetParent()->SendMessage(WM_PALINFO, + palette[x/mult+(y/multY)*sw], + paletteAddress+(x/mult+(y/multY)*sw)); +} + +BOOL PaletteViewControl::OnEraseBkgnd(CDC* pDC) +{ + return TRUE; +} + + +void PaletteViewControl::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + RECT rect; + GetClientRect(&rect); + int w = rect.right - rect.left; + int h = rect.bottom - rect.top; + + CDC memDC; + memDC.CreateCompatibleDC(&dc); + CBitmap bitmap, *pOldBitmap; + bitmap.CreateCompatibleBitmap(&dc, w, h); + pOldBitmap = memDC.SelectObject(&bitmap); + + StretchDIBits(memDC.GetSafeHdc(), + 0, + 0, + w, + h, + 0, + 0, + this->w, + this->h, + data, + &bmpInfo, + DIB_RGB_COLORS, + SRCCOPY); + int sw = this->w / 16; + int sh = this->h / 16; + int mult = w / sw; + int multY = h / sh; + CPen pen; + pen.CreatePen(PS_SOLID, 1, RGB(192,192,192)); + CPen *old = memDC.SelectObject(&pen); + int i; + for(i = 1; i < sh; i++) { + memDC.MoveTo(0, i * multY); + memDC.LineTo(w, i * multY); + } + for(i = 1; i < sw; i++) { + memDC.MoveTo(i * mult, 0); + memDC.LineTo(i * mult, h); + } + memDC.DrawEdge(&rect, EDGE_SUNKEN, BF_RECT); + memDC.SelectObject(old); + pen.DeleteObject(); + + if(selected != -1) { + pen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); + old = memDC.SelectObject(&pen); + + int startX = (selected & (sw-1))*mult+1; + int startY = (selected / sw)*multY+1; + int endX = startX + mult-2; + int endY = startY + multY-2; + + memDC.MoveTo(startX, startY); + memDC.LineTo(endX, startY); + memDC.LineTo(endX, endY); + memDC.LineTo(startX, endY); + memDC.LineTo(startX, startY-1); + + memDC.SelectObject(old); + pen.DeleteObject(); + } + + dc.BitBlt(0,0,w,h, + &memDC,0,0,SRCCOPY); + + memDC.SelectObject(pOldBitmap); + bitmap.DeleteObject(); + memDC.DeleteDC(); +} + +void PaletteViewControl::registerClass() +{ + if(!isRegistered) { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaPaletteViewControl"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/PaletteViewControl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/PaletteViewControl.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,71 @@ +#if !defined(AFX_PALETTEVIEWCONTROL_H__31F600AE_B7E5_4F6C_80B6_55E4B61FBD57__INCLUDED_) +#define AFX_PALETTEVIEWCONTROL_H__31F600AE_B7E5_4F6C_80B6_55E4B61FBD57__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// PaletteViewControl.h : header file +// +#define WM_PALINFO WM_APP+1 + +///////////////////////////////////////////////////////////////////////////// +// PaletteViewControl window + +class PaletteViewControl : public CWnd +{ + int w; + int h; + int colors; + u8 *data; + BITMAPINFO bmpInfo; + static bool isRegistered; + int selected; + protected: + u16 palette[256]; + int paletteAddress; + // Construction + public: + PaletteViewControl(); + + virtual void updatePalette()=0; + + // Attributes + public: + + // Operations + public: + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(PaletteViewControl) + //}}AFX_VIRTUAL + + // Implementation + public: + void registerClass(); + void refresh(); + void render(u16 color, int x, int y); + void setSelected(int s); + void setPaletteAddress(int address); + bool saveJASCPAL(const char *name); + bool saveMSPAL(const char *name); + bool saveAdobe(const char *name); + void init(int c, int w, int h); + virtual ~PaletteViewControl(); + + // Generated message map functions + protected: + //{{AFX_MSG(PaletteViewControl) + afx_msg void OnLButtonDown(UINT nFlags, CPoint point); + afx_msg BOOL OnEraseBkgnd(CDC* pDC); + afx_msg void OnPaint(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + ///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_PALETTEVIEWCONTROL_H__31F600AE_B7E5_4F6C_80B6_55E4B61FBD57__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/PerfTimer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/PerfTimer.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,35 @@ +// CPerfTimer - a simple Win32 performance counter wrapper +// by Dean Wyant dwyant@mindspring.com + +#include "stdafx.h" +#include "PerfTimer.h" + +// Declare and initialize static member vars that get set only once and never change +__int64 CPerfTimer::m_Freq = 0; +__int64 CPerfTimer::m_Adjust = 0; + +// All functions defined inline for speed. After all, the performance counter is +// supposed to be able to time very short events fairly accurately. + + + +BOOL CPerfTimer::IsSupported() +{ // Returns FALSE if performance counter not supported. + // Call after constructing at least one CPerfTimer + return (m_Freq > 1); +} + +const double CPerfTimer::Resolution() +{ // Returns timer resolution in seconds + return 1.0/(double)m_Freq; +} + +const double CPerfTimer::Resolutionms() +{ // Returns timer resolution in milliseconds + return 1000.0/(double)m_Freq; +} + +const double CPerfTimer::Resolutionus() +{ // Returns timer resolution in microseconds + return 1000000.0/(double)m_Freq; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/PerfTimer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/PerfTimer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,403 @@ +// CPerfTimer - a simple Win32 performance counter wrapper +// by Dean Wyant dwyant@mindspring.com + +/* + +This class is simple to use. Just declare a variable(s) as type CPerfTimer, +call Start() to start timimg and call Stop() to stop timimg. You can pause a +timer by calling Stop() and then you can call Start() to resume. Retrieve the +elapsed time by calling an Elapsed..() function. Assignment, addition, +subtraction and comparison are supported. There are a few information calls +available also. All calls except Start and Stop can be performed on a timer +without stopping it. + +*/ + +#ifndef __PERFTIMER_H__ +#define __PERFTIMER_H__ + +class CPerfTimer +{ +public: + CPerfTimer(BOOL bStart = FALSE) {Init(bStart);} + + CPerfTimer(const CPerfTimer& Src); + + virtual ~CPerfTimer() {;} + + void Start(BOOL bReset = FALSE); // Start from current value or optionally from 0 + void Stop(); // Stop timing. Use Start afterwards to continue. + + BOOL IsRunning(); // Returns FALSE if stopped. + + BOOL IsSupported(); // Returns FALSE if performance counter not supported. + // Call after constructing at least one CPerfTimer + + const double Resolution(); // Returns timer resolution in seconds + const double Resolutionms(); // Returns timer resolution in milliseconds + const double Resolutionus(); // Returns timer resolution in microseconds + + const double Elapsed(); // Returns elapsed time in seconds + const double Elapsedms(); // Returns elapsed time in milliseconds + const double Elapsedus(); // Returns elapsed time in microseconds + + const CPerfTimer& operator=(const CPerfTimer& Src); // Assignment operator + + // Math operators + CPerfTimer operator+(const CPerfTimer& Src) const; + CPerfTimer operator-(const CPerfTimer& Src) const; + const CPerfTimer& operator+=(const CPerfTimer& Src); + const CPerfTimer& operator-=(const CPerfTimer& Src); + // For time in seconds + CPerfTimer operator+(const double Secs) const; + CPerfTimer operator-(const double Secs) const; + const CPerfTimer& operator+=(const double Secs); + const CPerfTimer& operator-=(const double Secs); + + // Boolean comparison operators + BOOL operator<(const CPerfTimer& Src); + BOOL operator>(const CPerfTimer& Src); + BOOL operator<=(const CPerfTimer& Src); + BOOL operator>=(const CPerfTimer& Src); + // For time in seconds + BOOL operator<(const double Secs); + BOOL operator>(const double Secs); + BOOL operator<=(const double Secs); + BOOL operator>=(const double Secs); + + virtual void Lock() const {;} // Override for thread safe operation + virtual void Unlock() const {;} // Override for thread safe operation +protected: + void Init(BOOL bStart); + void Copy(const CPerfTimer& Src); + +private: + __int64 m_Start; + static __int64 m_Freq; // does not change while system is running + static __int64 m_Adjust; // Adjustment time it takes to Start and Stop +}; + +class CPerfTimerT : public CPerfTimer +{ // You only need to use types of this class if a timer is going to be shared between threads +public: + CPerfTimerT(BOOL bStart = FALSE) + { + m_hMutex = CreateMutex(NULL,FALSE,""); + Init(bStart); + } + + CPerfTimerT(const CPerfTimerT& Src) + { + m_hMutex = CreateMutex(NULL,FALSE,""); + Copy(Src); + } + + CPerfTimerT(const CPerfTimer& Src) + { + m_hMutex = CreateMutex(NULL,FALSE,""); + Copy(Src); + } + + virtual ~CPerfTimerT() + { CloseHandle(m_hMutex); } + + const CPerfTimerT& operator=(const CPerfTimerT& Src) // Assignment operator + { + Copy(Src); + return *this; + } + + virtual void Lock() const { WaitForSingleObject(m_hMutex,10000); } + virtual void Unlock() const { ReleaseMutex(m_hMutex); } +private: + HANDLE m_hMutex; +}; + +inline void CPerfTimer::Init(BOOL bStart) +{ + if (!m_Freq) + { // Initialization should only run once + QueryPerformanceFrequency((LARGE_INTEGER *)&m_Freq); + if (!m_Freq) + m_Freq = 1; // Timer will be useless but will not cause divide by zero + m_Start = 0; + m_Adjust = 0; + Start(); // Time a Stop + Stop(); + m_Adjust = m_Start; + } + // This is the only part that normally runs + m_Start = 0; + if (bStart) + Start(); +} + +inline CPerfTimer::CPerfTimer(const CPerfTimer& Src) +{ + Copy(Src); +} + +inline void CPerfTimer::Copy(const CPerfTimer& Src) +{ + if (&Src == this) + return; // avoid deadlock if someone tries to copy it to itself + Src.Lock(); + Lock(); + m_Start = Src.m_Start; + Unlock(); + Src.Unlock(); +} + +inline void CPerfTimer::Start(BOOL bReset) +{ // Start from current value or optionally from 0 + __int64 i; + QueryPerformanceCounter((LARGE_INTEGER *)&i); + Lock(); + if ((!bReset) && (m_Start < 0)) + m_Start += i; // We are starting with an accumulated time + else + m_Start = i; // Starting from 0 + Unlock(); +} + +inline void CPerfTimer::Stop() +{ // Stop timing. Use Start afterwards to continue + Lock(); + if (m_Start <= 0) + { + Unlock(); + return; // Was not running + } + __int64 i; + QueryPerformanceCounter((LARGE_INTEGER *)&i); + m_Start += -i; // Stopped timer keeps elapsed timer ticks as a negative + if (m_Start < m_Adjust) // Do not overflow + m_Start -= m_Adjust; // Adjust for time timer code takes to run + else + m_Start = 0; // Stop must have been called directly after Start + Unlock(); +} + +inline BOOL CPerfTimer::IsRunning() +{ // Returns FALSE if stopped. + Lock(); + BOOL bRet = (m_Start > 0); // When < 0, holds elpased clicks + Unlock(); + return bRet; +} +inline const double CPerfTimer::Elapsed() +{ // Returns elapsed time in seconds + CPerfTimer Result(*this); + Result.Stop(); + return (double)(-Result.m_Start)/(double)m_Freq; +} + +inline const double CPerfTimer::Elapsedms() +{ // Returns elapsed time in milliseconds + CPerfTimer Result(*this); + Result.Stop(); + return (-Result.m_Start*1000.0)/(double)m_Freq; +} + +inline const double CPerfTimer::Elapsedus() +{ // Returns elapsed time in microseconds + CPerfTimer Result(*this); + + return (-Result.m_Start * 1000000.0)/(double)m_Freq; +} + + +// Assignment operator +inline const CPerfTimer& CPerfTimer::operator=(const CPerfTimer& Src) +{ + Copy(Src); + return *this; +} + + +// Math operators +inline CPerfTimer CPerfTimer::operator+(const CPerfTimer& Src) const +{ + CPerfTimer Result(*this); + Result += Src; + return Result; +} + +inline CPerfTimer CPerfTimer::operator-(const CPerfTimer& Src) const +{ + CPerfTimer Result(*this); + Result -= Src; + return Result; +} + +inline const CPerfTimer& CPerfTimer::operator+=(const CPerfTimer& Src) +{ + CPerfTimer SrcStop(Src); // Temp is necessary in case Src is not stopped + SrcStop.Stop(); + Lock(); + m_Start += SrcStop.m_Start; + Unlock(); + return *this; +} + +inline const CPerfTimer& CPerfTimer::operator-=(const CPerfTimer& Src) +{ + CPerfTimer SrcStop(Src); // Temp is necessary in case Src is not stopped + SrcStop.Stop(); + Lock(); + m_Start -= SrcStop.m_Start; + Unlock(); + return *this; +} + +// For time in seconds +inline CPerfTimer CPerfTimer::operator+(const double Secs) const +{ + CPerfTimer Result(*this); + Result += Secs; + return Result; +} + +inline CPerfTimer CPerfTimer::operator-(const double Secs) const +{ + CPerfTimer Result(*this); + Result += Secs; + return Result; +} + +inline const CPerfTimer& CPerfTimer::operator+=(const double Secs) +{ + Lock(); + m_Start -= (__int64)(Secs*(double)m_Freq); + Unlock(); + return *this; +} + +inline const CPerfTimer& CPerfTimer::operator-=(const double Secs) +{ + Lock(); + m_Start += (__int64)(Secs*(double)m_Freq); + Unlock(); + return *this; +} + + + +// Boolean comparison operators +inline BOOL CPerfTimer::operator<(const CPerfTimer& Src) +{ + BOOL bRet; + CPerfTimer Temp(Src); + Lock(); + if (m_Start <= 0) + { + Temp.Stop(); + bRet = (m_Start > Temp.m_Start); + Unlock(); + return bRet; + } + else + if (Temp.m_Start > 0) + { + bRet = (m_Start < Temp.m_Start); + Unlock(); + return bRet; + } + else + { + Unlock(); + CPerfTimer ThisStop(*this); + ThisStop.Stop(); + return (ThisStop.m_Start > Temp.m_Start); + } +} + +inline BOOL CPerfTimer::operator>(const CPerfTimer& Src) +{ + BOOL bRet; + CPerfTimer Temp(Src); + Lock(); + if (m_Start <= 0) + { + Temp.Stop(); + bRet = (m_Start < Temp.m_Start); + Unlock(); + return bRet; + } + else + if (Temp.m_Start > 0) + { + bRet = (m_Start > Temp.m_Start); + Unlock(); + return bRet; + } + else + { + Unlock(); + CPerfTimer ThisStop(*this); + ThisStop.Stop(); + return (ThisStop.m_Start < Temp.m_Start); + } +} + +inline BOOL CPerfTimer::operator<=(const CPerfTimer& Src) +{ + return !(*this > Src); +} + +inline BOOL CPerfTimer::operator>=(const CPerfTimer& Src) +{ + return !(*this < Src); +} + +// For time in seconds +inline BOOL CPerfTimer::operator<(const double Secs) +{ + BOOL bRet; + Lock(); + if (m_Start <= 0) + { + bRet = (m_Start > (__int64)(-Secs*(double)m_Freq)); + Unlock(); + return bRet; + } + else + { + Unlock(); + CPerfTimer ThisStop(*this); + ThisStop.Stop(); + return (ThisStop.m_Start > (__int64)(-Secs*(double)m_Freq)); + } +} + +inline BOOL CPerfTimer::operator>(const double Secs) +{ + BOOL bRet; + Lock(); + if (m_Start <= 0) + { + bRet = (m_Start < (__int64)(-Secs*(double)m_Freq)); + Unlock(); + return bRet; + } + else + { + Unlock(); + CPerfTimer ThisStop(*this); + ThisStop.Stop(); + return (ThisStop.m_Start < (__int64)(-Secs*(double)m_Freq)); + } +} + +inline BOOL CPerfTimer::operator<=(const double Secs) +{ + return !(*this > Secs); +} + +inline BOOL CPerfTimer::operator>=(const double Secs) +{ + return !(*this < Secs); +} + + +#endif //__PERFTIMER_H__ \ No newline at end of file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Reg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Reg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,379 @@ +#include "stdafx.h" +#include "Reg.h" + +static char buffer[2048]; +static HKEY vbKey = NULL; +static CString regVbaPath; + +#define VBA_PREF "preferences" + +bool regEnabled = true; + +void regInit(const char *path) +{ + if (regEnabled) + { + DWORD disp = 0; + LONG res = RegCreateKeyEx(HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance", + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &vbKey, + &disp); + } + regVbaPath.Format("%s\\vba.ini", path); +} + +void regShutdown() +{ + LONG res = RegCloseKey(vbKey); +} + +const char *regGetINIPath() +{ + return regVbaPath; +} + +const char *regQueryStringValue(const char *key, const char *def) +{ + if (regEnabled) + { + DWORD type = 0; + DWORD size = 2048; + + LONG res = RegQueryValueEx(vbKey, + key, + NULL, + &type, + (UCHAR *)buffer, + &size); + + if (res == ERROR_SUCCESS && type == REG_SZ) + return buffer; + + return def; + } + + DWORD res = GetPrivateProfileString(VBA_PREF, + key, + def, + (LPTSTR)buffer, + 2048, + regVbaPath); + + if (res) + return buffer; + + return def; +} + +DWORD regQueryDwordValue(const char *key, DWORD def, bool force) +{ + if (regEnabled || force) + { + DWORD type = 0; + DWORD size = sizeof(DWORD); + DWORD result = 0; + + LONG res = RegQueryValueEx(vbKey, + key, + NULL, + &type, + (UCHAR *)&result, + &size); + + if (res == ERROR_SUCCESS && type == REG_DWORD) + return result; + + return def; + } + + return GetPrivateProfileInt(VBA_PREF, + key, + def, + regVbaPath); +} + +BOOL regQueryBinaryValue(const char *key, char *value, int count) +{ + if (regEnabled) + { + DWORD type = 0; + DWORD size = count; + DWORD result = 0; + + LONG res = RegQueryValueEx(vbKey, + key, + NULL, + &type, + (UCHAR *)value, + &size); + + if (res == ERROR_SUCCESS && type == REG_BINARY) + return TRUE; + + return FALSE; + } + CString k = key; + k += "Count"; + int size = GetPrivateProfileInt(VBA_PREF, + k, + -1, + regVbaPath); + if (size >= 0 && size < count) + count = size; + return GetPrivateProfileStruct(VBA_PREF, + key, + value, + count, + regVbaPath); +} + +void regSetStringValue(const char *key, const char *value) +{ + if (regEnabled) + { + LONG res = RegSetValueEx(vbKey, + key, + NULL, + REG_SZ, + (const UCHAR *)value, + strlen(value)+1); + } + else + { + WritePrivateProfileString(VBA_PREF, + key, + value, + regVbaPath); + } +} + +void regSetDwordValue(const char *key, DWORD value, bool force) +{ + if (regEnabled || force) + { + LONG res = RegSetValueEx(vbKey, + key, + NULL, + REG_DWORD, + (const UCHAR *)&value, + sizeof(DWORD)); + } + else + { + wsprintf(buffer, "%u", value); + WritePrivateProfileString(VBA_PREF, + key, + buffer, + regVbaPath); + } +} + +void regSetBinaryValue(const char *key, char *value, int count) +{ + if (regEnabled) + { + LONG res = RegSetValueEx(vbKey, + key, + NULL, + REG_BINARY, + (const UCHAR *)value, + count); + } + else + { + CString k = key; + k += "Count"; + wsprintf(buffer, "%u", count); + + WritePrivateProfileString(VBA_PREF, + k, + buffer, + regVbaPath); + + WritePrivateProfileStruct(VBA_PREF, + key, + value, + count, + regVbaPath); + } +} + +void regDeleteValue(const char *key) +{ + if (regEnabled) + { + LONG res = RegDeleteValue(vbKey, + key); + } + else + { + WritePrivateProfileString(VBA_PREF, + key, + NULL, + regVbaPath); + } +} + +bool regCreateFileType(const char *ext, const char *type) +{ + DWORD disp = 0; + HKEY key; + LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT, + ext, + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &key, + &disp); + if (res == ERROR_SUCCESS) + { + res = RegSetValueEx(key, + "", + 0, + REG_SZ, + (const UCHAR *)type, + strlen(type)+1); + RegCloseKey(key); + return true; + } + return false; +} + +bool regAssociateType(const char *type, const char *desc, const char *application) +{ + DWORD disp = 0; + HKEY key; + LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT, + type, + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &key, + &disp); + if (res == ERROR_SUCCESS) + { + res = RegSetValueEx(key, + "", + 0, + REG_SZ, + (const UCHAR *)desc, + strlen(desc)+1); + HKEY key2; + res = RegCreateKeyEx(key, + "Shell\\Open\\Command", + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_ALL_ACCESS, + NULL, + &key2, + &disp); + if (res == ERROR_SUCCESS) + { + res = RegSetValueEx(key2, + "", + 0, + REG_SZ, + (const UCHAR *)application, + strlen(application)+1); + RegCloseKey(key2); + RegCloseKey(key); + return true; + } + + RegCloseKey(key); + } + return false; +} + +static void regExportSettingsToINI(HKEY key, const char *section) +{ + char valueName[256]; + int index = 0; + while (1) + { + DWORD nameSize = 256; + DWORD size = 2048; + DWORD type; + LONG res = RegEnumValue(key, + index, + valueName, + &nameSize, + NULL, + &type, + (LPBYTE)buffer, + &size); + + if (res == ERROR_SUCCESS) + { + switch (type) + { + case REG_DWORD: + { + char temp[256]; + wsprintf(temp, "%u", *((DWORD *)buffer)); + WritePrivateProfileString(section, + valueName, + temp, + regVbaPath); + break; + } + case REG_SZ: + WritePrivateProfileString(section, + valueName, + buffer, + regVbaPath); + break; + case REG_BINARY: + { + char temp[256]; + + wsprintf(temp, "%u", size); + CString k = valueName; + k += "Count"; + WritePrivateProfileString(section, + k, + temp, + regVbaPath); + WritePrivateProfileStruct(section, + valueName, + buffer, + size, + regVbaPath); + break; + } + } + index++; + } + else + break; + } +} + +void regExportSettingsToINI() +{ + if (vbKey != NULL) + { + regExportSettingsToINI(vbKey, VBA_PREF); + } + + HKEY key; + + if (RegOpenKey(HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer", &key) == + ERROR_SUCCESS) + { + regExportSettingsToINI(key, "Viewer"); + RegCloseKey(key); + } +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Reg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Reg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,18 @@ +#ifndef VBA_REG_H +#define VBA_REG_H + +extern bool regEnabled; + +const char *regQueryStringValue(const char *key, const char *def); +DWORD regQueryDwordValue(const char *key, DWORD def, bool force = false); +BOOL regQueryBinaryValue(const char *key, char *value, int count); +void regSetStringValue(const char *key, const char *value); +void regSetDwordValue(const char *key, DWORD value, bool force = false); +void regSetBinaryValue(const char *key, char *value, int count); +void regDeleteValue(const char *key); +void regInit(const char *); +void regShutdown(); +bool regCreateFileType(const char *ext, const char *type); +bool regAssociateType(const char *type, const char *desc, const char *application); +void regExportSettingsToINI(); +#endif // VBA_REG_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ResizeDlg.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ResizeDlg.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,609 @@ +/*---------------------------------------------------------------------- + Copyright (c) Gipsysoft. All Rights Reserved. + File: DialogSizer_Set.cpp + Web site: http://gipsysoft.com + + This software is provided 'as-is', without any express or implied warranty. + + In no event will the author be held liable for any damages arising from the + use of this software. + + Permission is granted to anyone to use this software for any purpose, including + commercial applications, and to alter it and redistribute it freely, subject + to the following restrictions: + + 1) The origin of this software must not be misrepresented; you must not claim + that you wrote the original software. If you use this software in a product, + an acknowledgment in the product documentation is requested but not required. + 2) Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. Altered source is encouraged + to be submitted back to the original author so it can be shared with the + community. Please share your changes. + 3) This notice may not be removed or altered from any source distribution. + + Owner: russf@gipsysoft.com + Purpose: Main functionality for sizeable dialogs + + Store a local copy of the user settings + Subclass the window + Respond to various messages withinn the subclassed window. + + ----------------------------------------------------------------------*/ +// modified by the VBA-rr Team + +#include "stdafx.h" +#include "ResizeDlg.h" +#include "VBA.h" +#include "Sound.h" +#include "WinHelper.h" + +IMPLEMENT_DYNAMIC(ResizeDlg, CDialog) + +// moved functions to this file to reduce number of files + +struct RegistryData +{ + WINDOWPLACEMENT m_wpl; +}; + +struct DialogData // dd +{ + HKEY hkRootSave; + LPCTSTR pcszName; + + // + // The number of items contained in the psd member. + // Used in the DeferWindowPos structure and in allocating memory + int nItemCount; + DialogSizerSizingItem *psd; + + // + // We need the smallest to respond to the WM_GETMINMAXINFO message + POINT m_ptSmallest; + + // + // We don't strictly speaking need to say how big the biggest can be but + POINT m_ptLargest; + bool m_bLargestSet; + + // + // we need this to decide how much the window has changed size when we get a WM_SIZE message + SIZE m_sizeClient; + + // + // Draw the sizing grip...or not + bool m_bMaximised; + BOOL m_bShowSizingGrip; + + WinHelper::CRect m_rcGrip; +}; + +extern bool regEnabled; +extern const char *regGetINIPath(); + +void AssertFailed(char *file, int line, char *exp) +{ + char buffer[1024]; + + sprintf(buffer, "File %s\nLine %d\nExpression %s\nPress Retry to debug", + file, line, exp); + systemSoundClearBuffer(); + int res = MessageBox(*theApp.m_pMainWnd, buffer, "Assertion failed!", + MB_ICONHAND | MB_SETFOREGROUND | MB_TASKMODAL | + MB_ABORTRETRYIGNORE); + + if (res == IDRETRY) + { + __asm int 3; + } + else if (res == IDABORT) + SendMessage(*theApp.m_pMainWnd, WM_QUIT, 0, 0); +} + +void ApiFailure(char *pcszFilename, int nLine, char *pcszExpression) +{ + const DWORD dwLastError = ::GetLastError(); + LPCTSTR lpMsgBuf; + (void)::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, dwLastError, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lpMsgBuf, 0, NULL); + + char szExeName[ MAX_PATH ]; + + if (!GetModuleFileName(NULL, szExeName, countof(szExeName))) + strcpy(szExeName, ""); + + char szMessage[ 1024 ]; + _snprintf(szMessage, countof(szMessage), + "API VERIFY Failure!" + "\nProgram: %s" + "\n" + "\nFile %s" + "\nLine %d" + "\n" + "\nExpression %s" + "\n" + "\nLast Error %d" + "\n %s" + "\n\nPress Retry to debug the application", + szExeName, + pcszFilename, + nLine, + pcszExpression, + dwLastError, + lpMsgBuf + ); + + (void)LocalFree((LPVOID)lpMsgBuf); + HWND hwndParent = ::GetActiveWindow(); + hwndParent = ::GetLastActivePopup(hwndParent); + systemSoundClearBuffer(); + int nCode = ::MessageBoxA(hwndParent, + szMessage, + "Debug Helper", + MB_TASKMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE | + MB_SETFOREGROUND); + if (nCode == IDABORT) + { + ::SendMessage(*theApp.m_pMainWnd, WM_QUIT, 0, 0); + } + else if (nCode == IDRETRY) + __asm int 3; +} + +long FASTCALL RegQueryValueExRecursive(HKEY hKey, + LPCTSTR lpValueName, + LPDWORD lpReserved, + LPDWORD lpType, + LPBYTE lpData, + LPDWORD lpcbData) +{ + TCHAR szBuffer[ 256 ]; + R_ASSERT(lstrlen(lpValueName) < countof(szBuffer)); + (void)lstrcpy(szBuffer, lpValueName); + + LPTSTR pszBuffer = szBuffer; + LPTSTR pszLast = szBuffer; + while (*pszBuffer) + { + if (*pszBuffer == _T('\\') || *pszBuffer == _T('/')) + { + pszLast = pszBuffer; + lpValueName = pszLast + 1; + } + pszBuffer++; + } + + if (!regEnabled) + { + if (GetPrivateProfileStruct("Viewer", + lpValueName, + lpData, + *lpcbData, + regGetINIPath())) + { + *lpType = REG_BINARY; + return ERROR_SUCCESS; + } + return -1; + } + + bool m_bNeedToCloseKey = false; + if (pszLast != szBuffer) + { + *pszLast = _T('\000'); + HKEY hkeyTemp; + long lRet = RegOpenKey(hKey, szBuffer, &hkeyTemp); + if (lRet != ERROR_SUCCESS) + { + return lRet; + } + hKey = hkeyTemp; + m_bNeedToCloseKey = true; + } + + long lRet = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); + if (m_bNeedToCloseKey) + { + R_VERIFY(RegCloseKey(hKey) == ERROR_SUCCESS); + } + return lRet; +} + +long FASTCALL RegSetValueExRecursive(HKEY hKey, + LPCTSTR lpValueName, + DWORD Reserved, + DWORD dwType, + CONST BYTE*lpData, + DWORD cbData) +{ + TCHAR szBuffer[ 256 ]; + R_ASSERT(lstrlen(lpValueName) < countof(szBuffer)); + (void)lstrcpy(szBuffer, lpValueName); + + LPTSTR pszBuffer = szBuffer; + LPTSTR pszLast = szBuffer; + while (*pszBuffer) + { + if (*pszBuffer == _T('\\') || *pszBuffer == _T('/')) + { + pszLast = pszBuffer; + lpValueName = pszLast + 1; + } + pszBuffer++; + } + + if (!regEnabled) + { + if (WritePrivateProfileStruct("Viewer", + lpValueName, + (LPVOID)lpData, + cbData, + regGetINIPath())) + { + return ERROR_SUCCESS; + } + return -1; + } + + bool m_bNeedToCloseKey = false; + if (pszLast != szBuffer) + { + *pszLast = _T('\000'); + HKEY hkeyTemp; + long lRet = RegOpenKey(hKey, szBuffer, &hkeyTemp); + if (lRet != ERROR_SUCCESS) + { + lRet = RegCreateKey(hKey, szBuffer, &hkeyTemp); + if (lRet != ERROR_SUCCESS) + return lRet; + } + hKey = hkeyTemp; + m_bNeedToCloseKey = true; + } + + long lRet = RegSetValueEx(hKey, lpValueName, Reserved, dwType, lpData, cbData); + if (m_bNeedToCloseKey) + { + R_VERIFY(RegCloseKey(hKey) == ERROR_SUCCESS); + } + return lRet; +} + +int ResizeDlgGetItemCount(const DialogSizerSizingItem *psd) +{ + R_ASSERT(psd); + int nCount = 0; + while (psd->uSizeInfo != 0xFFFFFFFF) + { + nCount++; + psd++; + } + return nCount; +} + +void ResizeDlgUpdateGripperRect(const int cx, const int cy, WinHelper::CRect &rcGrip) +{ + const int nGripWidth = GetSystemMetrics(SM_CYVSCROLL); + const int nGripHeight = GetSystemMetrics(SM_CXVSCROLL); + rcGrip.left = cx - nGripWidth; + rcGrip.top = cy - nGripHeight; + rcGrip.right = cx; + rcGrip.bottom = cy; +} + +void ResizeDlgUpdateGripper(HWND hwnd, DialogData *pdd) +{ + if (pdd->m_bShowSizingGrip) + { + WinHelper::CRect rcOld(pdd->m_rcGrip); + + ResizeDlgUpdateGripperRect(pdd->m_sizeClient.cx, pdd->m_sizeClient.cy, pdd->m_rcGrip); + + // + // We also need to invalidate the combined area of the old and new rectangles + // otherwise we would have trail of grippers when we sized the dialog larger + // in any axis + (void)UnionRect(&rcOld, &rcOld, &pdd->m_rcGrip); + (void)InvalidateRect(hwnd, &rcOld, TRUE); + } +} + +void ResizeDlgCopyItems(DialogSizerSizingItem *psdDest, const DialogSizerSizingItem *psdSource) +// +// Will copy all of the items in psdSource into psdDest. +{ + // + // Loop til we reach the end + while (psdSource->uSizeInfo != 0xFFFFFFFF) + { + *psdDest = *psdSource; + psdDest++; + psdSource++; + } + // And when we do copy the last item + *psdDest = *psdSource; +} + +ResizeDlg::ResizeDlg(UINT id, CWnd *parent) + : CDialog(id, parent) +{ + dd = NULL; +} + +void *ResizeDlg::AddDialogData() +// +// Firstly determine if the data already exists, if it does then return that, if not then we will +// create and initialise a brand new structure. +{ + DialogData *pdd = (DialogData *)dd; + if (!pdd) + { + pdd = (DialogData *)calloc(1, sizeof(DialogData)); + } + + if (pdd) + { + // + // Store some sizes etc. for later. + CRect rc; + GetWindowRect(rc); + pdd->m_ptSmallest.x = rc.Width(); + pdd->m_ptSmallest.y = rc.Height(); + + GetClientRect(rc); + pdd->m_sizeClient = rc.Size(); + dd = pdd; + ResizeDlgUpdateGripperRect(pdd->m_sizeClient.cx, pdd->m_sizeClient.cy, pdd->m_rcGrip); + } + return pdd; +} + +BOOL ResizeDlg::SetData(const DialogSizerSizingItem *psd, + BOOL bShowSizingGrip, + HKEY hkRootSave, + LPCTSTR pcszName, + SIZE *psizeMax) +// +// Setting a dialog sizeable involves subclassing the window and handling it's +// WM_SIZE messages, if we have a hkRootSave and pcszName then we will also be loading/saving +// the size and position of the window from the registry. We load from the registry when we +// subclass the window and we save to the registry when we get a WM_DESTROY. +// +// It will return non-zero for success and zero if it fails +{ + R_ASSERT(psd); + R_ASSERT((hkRootSave != NULL && pcszName != NULL) + || (hkRootSave == NULL && pcszName == NULL)); + // + // Make sure all of the parameters are valid. + if (::IsWindow(*this) + && psd + && ((hkRootSave != NULL && pcszName != NULL && + !IsBadStringPtr(pcszName, 0xFFFF)) || + (hkRootSave == NULL && pcszName == NULL)) + && (psizeMax == NULL || !IsBadReadPtr(psizeMax, sizeof(SIZE))) + ) + { + DialogData *pdd = (DialogData *)AddDialogData(); + if (pdd) + { + pdd->hkRootSave = hkRootSave; + pdd->pcszName = pcszName; + pdd->m_bShowSizingGrip = bShowSizingGrip; + pdd->nItemCount = ResizeDlgGetItemCount(psd) + 1; + pdd->psd = (DialogSizerSizingItem *) + calloc(pdd->nItemCount, + sizeof(DialogSizerSizingItem)); + if (pdd->psd) + { + // + // Copy all of the user controls etc. for later, this way the user can quite happily + // let the structure go out of scope. + ResizeDlgCopyItems(pdd->psd, psd); + if (psizeMax) + { + pdd->m_ptLargest.x = psizeMax->cx; + pdd->m_ptLargest.y = psizeMax->cy; + pdd->m_bLargestSet = true; + } + + // + // If the there was save info passed in then we need to make damn good use of it + // by attempting to load the RegistryData structure + if (hkRootSave && pcszName) + { + RegistryData rd; + DWORD dwSize = sizeof(RegistryData); + DWORD dwType = REG_BINARY; + if (RegQueryValueExRecursive(hkRootSave, pcszName, NULL, &dwType, reinterpret_cast(&rd), + &dwSize) == ERROR_SUCCESS && dwSize == sizeof(rd)) + { + if (!(GetWindowLong(*this, GWL_STYLE) & WS_VISIBLE)) + rd.m_wpl.showCmd = SW_HIDE; + + VAPI(SetWindowPlacement(&rd.m_wpl)); + } + } + return TRUE; + } + else + { + free(pdd); + } + } + } + return FALSE; +} + +void ResizeDlg::UpdateWindowSize(const int cx, const int cy, HWND hwnd) +{ + DialogData *pdd = (DialogData *)dd; + if (pdd) + { + const int nDeltaX = cx - pdd->m_sizeClient.cx; + const int nDeltaY = cy - pdd->m_sizeClient.cy; + WinHelper::CDeferWindowPos def(pdd->nItemCount); + WinHelper::CRect rc; + const DialogSizerSizingItem *psd = pdd->psd; + while (psd->uSizeInfo != 0xFFFFFFFF) + { + HWND hwndChild = ::GetDlgItem(*this, psd->uControlID); + if (::IsWindow(hwndChild)) + { + VAPI(::GetWindowRect(hwndChild, rc)); + (void)::MapWindowPoints(::GetDesktopWindow(), hwnd, + (LPPOINT)&rc, 2); + + // + // Adjust the window horizontally + if (psd->uSizeInfo & DS_MoveX) + { + rc.left += nDeltaX; + rc.right += nDeltaX; + } + + // + // Adjust the window vertically + if (psd->uSizeInfo & DS_MoveY) + { + rc.top += nDeltaY; + rc.bottom += nDeltaY; + } + + // + // Size the window horizontally + if (psd->uSizeInfo & DS_SizeX) + { + rc.right += nDeltaX; + } + + // + // Size the window vertically + if (psd->uSizeInfo & DS_SizeY) + { + rc.bottom += nDeltaY; + } + + (void)def.DeferWindowPos(hwndChild, NULL, rc, + SWP_NOACTIVATE | SWP_NOZORDER); + } + psd++; + } + + pdd->m_sizeClient.cx = cx; + pdd->m_sizeClient.cy = cy; + + // + // If we have a sizing grip enabled then adjust it's position + ResizeDlgUpdateGripper(hwnd, pdd); + } +} + +BOOL ResizeDlg::OnWndMsg(UINT msg, WPARAM wParam, LPARAM lParam, LRESULT *res) +// Actual window procedure that will handle saving window size/position and moving +// the controls whilst the window sizes. +{ + if (dd == NULL) + { + return CDialog::OnWndMsg(msg, wParam, lParam, res); + } + switch (msg) + { + case WM_ERASEBKGND: + { + BOOL r = CDialog::OnWndMsg(msg, wParam, lParam, res); + DialogData *pdd = (DialogData *)dd; + if (pdd && pdd->m_bShowSizingGrip && !pdd->m_bMaximised) + { + VAPI(::DrawFrameControl(reinterpret_cast(wParam), + pdd->m_rcGrip, + DFC_SCROLL, DFCS_SCROLLSIZEGRIP)); + } + return r; + } + case WM_SIZE: + { + DialogData *pdd = (DialogData *)dd; + if (pdd && wParam != SIZE_MINIMIZED) + { + pdd->m_bMaximised = (wParam == SIZE_MAXIMIZED ? true : false); + UpdateWindowSize(LOWORD(lParam), HIWORD(lParam), *this); + } + break; + } + case WM_NCHITTEST: + { + // + // If the gripper is enabled then perform a simple hit test on our gripper area. + DialogData *pdd = (DialogData *)dd; + if (pdd && pdd->m_bShowSizingGrip) + { + POINT pt = { LOWORD(lParam), HIWORD(lParam) }; + (void)ScreenToClient(&pt); + if (PtInRect(pdd->m_rcGrip, pt)) + return (BOOL)HTBOTTOMRIGHT; + } + break; + } + case WM_GETMINMAXINFO: + { + // + // Our opportunity to say that we do not want the dialog to grow or shrink any more. + DialogData * pdd = (DialogData *)dd; + LPMINMAXINFO lpmmi = reinterpret_cast(lParam); + lpmmi->ptMinTrackSize = pdd->m_ptSmallest; + if (pdd->m_bLargestSet) + { + lpmmi->ptMaxTrackSize = pdd->m_ptLargest; + } + } + return (BOOL)0; + case WM_NOTIFY: + { + if (reinterpret_cast(lParam)->code == PSN_SETACTIVE) + { + CRect rc; + VAPI(::GetClientRect(*GetParent(), &rc)); + UpdateWindowSize(rc.Width(), rc.Height(), *GetParent()); + } + break; + } + case WM_DESTROY: + { + // + // Our opportunty for cleanup. + // Simply acquire all of our objects, free the appropriate memory and remove the + // properties from the window. If we do not remove the properties then they will constitute + // a resource leak. + DialogData *pdd = (DialogData *)dd; + if (pdd) + { + RegistryData rd; + rd.m_wpl.length = sizeof(rd.m_wpl); + VAPI(GetWindowPlacement(&rd.m_wpl)); + + if (pdd->hkRootSave && pdd->pcszName) + { + (void)RegSetValueExRecursive(pdd->hkRootSave, pdd->pcszName, + NULL, REG_BINARY, + reinterpret_cast(&rd), + sizeof(rd)); + } + + if (pdd->psd) + { + free(pdd->psd); + } + free(pdd); + } + + break; + } + } + return CDialog::OnWndMsg(msg, wParam, lParam, res); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ResizeDlg.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ResizeDlg.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,44 @@ +#ifndef VBA_WIN32_RESIZEDLG_H +#define VBA_WIN32_RESIZEDLG_H + +#ifndef _INC_TCHAR +#include +#endif // _INC_TCHAR + +// +// Predefined sizing information +#define DS_MoveX 1 +#define DS_MoveY 2 +#define DS_SizeX 4 +#define DS_SizeY 8 + +typedef struct DialogSizerSizingItem // sdi +{ + UINT uControlID; + UINT uSizeInfo; +} DialogSizerSizingItem; + +#define DIALOG_SIZER_START(name) DialogSizerSizingItem name[] = { +#define DIALOG_SIZER_ENTRY(controlID, flags) { controlID, flags }, +#define DIALOG_SIZER_END() { 0xFFFFFFFF, 0xFFFFFFFF } }; + +class ResizeDlg : public CDialog +{ + DECLARE_DYNAMIC(ResizeDlg) // what will this do? +public: + ResizeDlg(UINT id, CWnd *parent = NULL); + + void *AddDialogData(); + BOOL SetData(const DialogSizerSizingItem *psd, + BOOL bShowSizingGrip, + HKEY hkRootSave, + LPCTSTR pcszName, + SIZE *psizeMax); + void UpdateWindowSize(const int cx, const int cy, HWND); + + virtual BOOL OnWndMsg(UINT, WPARAM, LPARAM, LRESULT *); +private: + void *dd; +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/RewindInterval.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/RewindInterval.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,96 @@ +// RewindInterval.cpp : implementation file +// + +#include "stdafx.h" +#include +#include "resource.h" +#include "RewindInterval.h" +#include "VBA.h" + +///////////////////////////////////////////////////////////////////////////// +// RewindInterval dialog + +RewindInterval::RewindInterval(float interval, int slots, CWnd*pParent /*=NULL*/) + : CDialog(RewindInterval::IDD, pParent) +{ + //{{AFX_DATA_INIT(RewindInterval) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + this->interval = interval; + this->slots = slots; +} + +void RewindInterval::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(RewindInterval) + DDX_Control(pDX, IDC_INTERVAL, m_interval); + DDX_Control(pDX, IDC_REWINDSLOTS, m_slots); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(RewindInterval, CDialog) +//{{AFX_MSG_MAP(RewindInterval) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// RewindInterval message handlers + +void RewindInterval::OnCancel() +{ + EndDialog(-1); +} + +void RewindInterval::OnOk() +{ + CString buffer, buffer2; + + m_interval.GetWindowText(buffer); + m_slots.GetWindowText(buffer2); + + float interval = (float)atof(buffer); + int slots = atoi(buffer2); + + if (interval >= 0 && (int)interval <= 600) + { + if (slots >= 0 && slots <= MAX_REWIND_SLOTS) + { + int iInterval = (int)(interval*6.0f + 0.5f); + if (interval > 0 && iInterval == 0) + iInterval = 1; + EndDialog(iInterval | (slots << 16)); + theApp.winAccelMgr.UpdateMenu(theApp.menu); + } + else + systemMessage(IDS_INVALID_INTERVAL_VALUE, + "Invalid rewind slot amount. Please enter a number " + "between 0 and 128 slots"); + } + else + systemMessage(IDS_INVALID_INTERVAL_VALUE, + "Invalid rewind interval value. Please enter a number " + "between 0 and 600 seconds"); +} + +BOOL RewindInterval::OnInitDialog() +{ + CDialog::OnInitDialog(); + + m_interval.LimitText(5); + m_slots.LimitText(3); + + CString buffer, buffer2; + buffer.Format("%.1f", interval); + m_interval.SetWindowText(buffer); + buffer2.Format("%d", slots); + m_slots.SetWindowText(buffer2); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/RewindInterval.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/RewindInterval.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,49 @@ +#if !defined(AFX_REWINDINTERVAL_H__C95AFF44_1F64_44C8_BAAB_A54B982D28EA__INCLUDED_) +#define AFX_REWINDINTERVAL_H__C95AFF44_1F64_44C8_BAAB_A54B982D28EA__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// RewindInterval.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// RewindInterval dialog + +class RewindInterval : public CDialog +{ + // Construction +public: + float interval; + int slots; + RewindInterval(float interval, int slots, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(RewindInterval) + enum { IDD = IDD_REWIND_INTERVAL }; + CEdit m_interval; + CEdit m_slots; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(RewindInterval) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(RewindInterval) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_REWINDINTERVAL_H__C95AFF44_1F64_44C8_BAAB_A54B982D28EA__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/RomInfo.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/RomInfo.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,555 @@ +// RomInfo.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "RomInfo.h" +#include "WinResUtil.h" + +extern int32 gbRomSize; + +struct WinGBACompanyName +{ + LPCTSTR code; + LPCTSTR name; +}; + +static WinGBACompanyName winGBARomInfoCompanies[] = { + { "01", "Nintendo" }, + { "02", "Rocket Games" }, + { "08", "Capcom" }, + { "09", "Hot B Co." }, + { "0A", "Jaleco" }, + { "0B", "Coconuts Japan" }, + { "0C", "Coconuts Japan/G.X.Media" }, + { "0H", "Starfish" }, + { "0L", "Warashi Inc." }, + { "0N", "Nowpro" }, + { "0P", "Game Village" }, + { "13", "Electronic Arts Japan" }, + { "18", "Hudson Soft Japan" }, + { "19", "S.C.P." }, + { "1A", "Yonoman" }, + { "1G", "SMDE" }, + { "1P", "Creatures Inc." }, + { "1Q", "TDK Deep Impresion" }, + { "20", "Destination Software" }, + { "22", "VR 1 Japan" }, + { "25", "San-X" }, + { "28", "Kemco Japan" }, + { "29", "Seta" }, + { "2H", "Ubisoft Japan" }, + { "2K", "NEC InterChannel" }, + { "2L", "Tam" }, + { "2M", "Jordan" }, + { "2N", "Smilesoft" }, + { "2Q", "Mediakite" }, + { "36", "Codemasters" }, + { "37", "GAGA Communications" }, + { "38", "Laguna" }, + { "39", "Telstar Fun and Games" }, + { "41", "Ubi Soft Entertainment" }, + { "42", "Sunsoft" }, + { "47", "Spectrum Holobyte" }, + { "49", "IREM" }, + { "4D", "Malibu Games" }, + { "4F", "Eidos/U.S. Gold" }, + { "4J", "Fox Interactive" }, + { "4K", "Time Warner Interactive" }, + { "4Q", "Disney" }, + { "4S", "Black Pearl" }, + { "4X", "GT Interactive" }, + { "4Y", "RARE" }, + { "4Z", "Crave Entertainment" }, + { "50", "Absolute Entertainment" }, + { "51", "Acclaim" }, + { "52", "Activision" }, + { "53", "American Sammy Corp." }, + { "54", "Take 2 Interactive" }, + { "55", "Hi Tech" }, + { "56", "LJN LTD." }, + { "58", "Mattel" }, + { "5A", "Mindscape/Red Orb Ent." }, + { "5C", "Taxan" }, + { "5D", "Midway" }, + { "5F", "American Softworks" }, + { "5G", "Majesco Sales Inc" }, + { "5H", "3DO" }, + { "5K", "Hasbro" }, + { "5L", "NewKidCo" }, + { "5M", "Telegames" }, + { "5N", "Metro3D" }, + { "5P", "Vatical Entertainment" }, + { "5Q", "LEGO Media" }, + { "5S", "Xicat Interactive" }, + { "5T", "Cryo Interactive" }, + { "5W", "Red Storm Ent./BKN Ent." }, + { "5X", "Microids" }, + { "5Z", "Conspiracy Entertainment Corp." }, + { "60", "Titus Interactive Studios" }, + { "61", "Virgin Interactive" }, + { "62", "Maxis" }, + { "64", "LucasArts Entertainment" }, + { "67", "Ocean" }, + { "69", "Electronic Arts" }, + { "6E", "Elite Systems Ltd." }, + { "6F", "Electro Brain" }, + { "6G", "The Learning Company" }, + { "6H", "BBC" }, + { "6J", "Software 2000" }, + { "6L", "BAM! Entertainment" }, + { "6M", "Studio 3" }, + { "6Q", "Classified Games" }, + { "6S", "TDK Mediactive" }, + { "6U", "DreamCatcher" }, + { "6V", "JoWood Productions" }, + { "6W", "SEGA" }, + { "6X", "Wannado Edition" }, + { "6Y", "LSP" }, + { "6Z", "ITE Media" }, + { "70", "Infogrames" }, + { "71", "Interplay" }, + { "72", "JVC Musical Industries Inc" }, + { "73", "Parker Brothers" }, + { "75", "SCI" }, + { "78", "THQ" }, + { "79", "Accolade" }, + { "7A", "Triffix Ent. Inc." }, + { "7C", "Microprose Software" }, + { "7D", "Universal Interactive Studios" }, + { "7F", "Kemco" }, + { "7G", "Rage Software" }, + { "7H", "Encore" }, + { "7J", "Zoo" }, + { "7K", "BVM" }, + { "7L", "Simon & Schuster Interactive" }, + { "7M", "Asmik Ace Entertainment Inc./AIA" }, + { "7N", "Empire Interactive" }, + { "7Q", "Jester Interactive" }, + { "7T", "Scholastic" }, + { "7U", "Ignition Entertainment" }, + { "7W", "Stadlbauer" }, + { "80", "Misawa" }, + { "83", "LOZC" }, + { "8B", "Bulletproof Software" }, + { "8C", "Vic Tokai Inc." }, + { "8J", "General Entertainment" }, + { "8N", "Success" }, + { "8P", "SEGA Japan" }, + { "91", "Chun Soft" }, + { "92", "Video System" }, + { "93", "BEC" }, + { "96", "Yonezawa/S'pal" }, + { "97", "Kaneko" }, + { "99", "Victor Interactive Software" }, + { "9A", "Nichibutsu/Nihon Bussan" }, + { "9B", "Tecmo" }, + { "9C", "Imagineer" }, + { "9F", "Nova" }, + { "9H", "Bottom Up" }, + { "9L", "Hasbro Japan" }, + { "9N", "Marvelous Entertainment" }, + { "9P", "Keynet Inc." }, + { "9Q", "Hands-On Entertainment" }, + { "A0", "Telenet" }, + { "A1", "Hori" }, + { "A4", "Konami" }, + { "A6", "Kawada" }, + { "A7", "Takara" }, + { "A9", "Technos Japan Corp." }, + { "AA", "JVC" }, + { "AC", "Toei Animation" }, + { "AD", "Toho" }, + { "AF", "Namco" }, + { "AG", "Media Rings Corporation" }, + { "AH", "J-Wing" }, + { "AK", "KID" }, + { "AL", "MediaFactory" }, + { "AP", "Infogrames Hudson" }, + { "AQ", "Kiratto. Ludic Inc" }, + { "B0", "Acclaim Japan" }, + { "B1", "ASCII" }, + { "B2", "Bandai" }, + { "B4", "Enix" }, + { "B6", "HAL Laboratory" }, + { "B7", "SNK" }, + { "B9", "Pony Canyon Hanbai" }, + { "BA", "Culture Brain" }, + { "BB", "Sunsoft" }, + { "BD", "Sony Imagesoft" }, + { "BF", "Sammy" }, + { "BG", "Magical" }, + { "BJ", "Compile" }, + { "BL", "MTO Inc." }, + { "BN", "Sunrise Interactive" }, + { "BP", "Global A Entertainment" }, + { "BQ", "Fuuki" }, + { "C0", "Taito" }, + { "C2", "Kemco" }, + { "C3", "Square Soft" }, + { "C5", "Data East" }, + { "C6", "Tonkin House" }, + { "C8", "Koei" }, + { "CA", "Konami/Palcom/Ultra" }, + { "CB", "Vapinc/NTVIC" }, + { "CC", "Use Co.,Ltd." }, + { "CD", "Meldac" }, + { "CE", "FCI/Pony Canyon" }, + { "CF", "Angel" }, + { "CM", "Konami Computer Entertainment Osaka" }, + { "CP", "Enterbrain" }, + { "D1", "Sofel" }, + { "D2", "Quest" }, + { "D3", "Sigma Enterprises" }, + { "D4", "Ask Kodansa" }, + { "D6", "Naxat" }, + { "D7", "Copya System" }, + { "D9", "Banpresto" }, + { "DA", "TOMY" }, + { "DB", "LJN Japan" }, + { "DD", "NCS" }, + { "DF", "Altron Corporation" }, + { "DH", "Gaps Inc." }, + { "DN", "ELF" }, + { "E2", "Yutaka" }, + { "E3", "Varie" }, + { "E5", "Epoch" }, + { "E7", "Athena" }, + { "E8", "Asmik Ace Entertainment Inc." }, + { "E9", "Natsume" }, + { "EA", "King Records" }, + { "EB", "Atlus" }, + { "EC", "Epic/Sony Records" }, + { "EE", "IGS" }, + { "EL", "Spike" }, + { "EM", "Konami Computer Entertainment Tokyo" }, + { "EN", "Alphadream Corporation" }, + { "F0", "A Wave" }, + { "G1", "PCCW" }, + { "G4", "KiKi Co Ltd" }, + { "G5", "Open Sesame Inc." }, + { "G6", "Sims" }, + { "G7", "Broccoli" }, + { "G8", "Avex" }, + { "G9", "D3 Publisher" }, + { "GB", "Konami Computer Entertainment Japan" }, + { "GD", "Square-Enix" }, + { "HY", "Sachen" }, + { NULL, NULL } +}; + +static LPCTSTR winGBARomInfoFindMakerCode(LPCTSTR code) +{ + int i = 0; + while (winGBARomInfoCompanies[i].code) + { + if (!strcmp(winGBARomInfoCompanies[i].code, code)) + return winGBARomInfoCompanies[i].name; + i++; + } + return (LPCTSTR)winResLoadString(IDS_UNKNOWN); +} + +///////////////////////////////////////////////////////////////////////////// +// RomInfoGB dialog + +RomInfoGB::RomInfoGB(u8 *rom, CWnd *pParent /*=NULL*/) + : CDialog(RomInfoGB::IDD, pParent) +{ + //{{AFX_DATA_INIT(RomInfoGB) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + this->rom = rom; +} + +void RomInfoGB::DoDataExchange(CDataExchange *pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(RomInfoGB) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(RomInfoGB, CDialog) +//{{AFX_MSG_MAP(RomInfoGB) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// RomInfoGB message handlers + +void RomInfoGB::OnOk() +{ + EndDialog(TRUE); +} + +BOOL RomInfoGB::OnInitDialog() +{ + CDialog::OnInitDialog(); + + char buffer[128]; + + strncpy(buffer, (const char *)&rom[0x134], 15); + buffer[15] = 0; + GetDlgItem(IDC_ROM_TITLE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0x143]); + GetDlgItem(IDC_ROM_COLOR)->SetWindowText(buffer); + + strncpy(buffer, (const char *)&rom[0x144], 2); + buffer[2] = 0; + GetDlgItem(IDC_ROM_MAKER_CODE)->SetWindowText(buffer); + + if (rom[0x14b] != 0x33) + { + sprintf(buffer, "%02X", rom[0x14b]); + GetDlgItem(IDC_ROM_MAKER_CODE)->SetWindowText(buffer); + } + GetDlgItem(IDC_ROM_MAKER_NAME2)->SetWindowText(winGBARomInfoFindMakerCode(buffer)); + + sprintf(buffer, "%02x", rom[0x146]); + GetDlgItem(IDC_ROM_UNIT_CODE)->SetWindowText(buffer); + + CString type = winResLoadString(IDS_UNKNOWN); + switch (rom[0x147]) + { + case 0x00: + type = "ROM"; + break; + case 0x01: + type = "ROM+MBC1"; + break; + case 0x02: + type = "ROM+MBC1+RAM"; + break; + case 0x03: + type = "ROM+MBC1+RAM+BATT"; + break; + case 0x05: + type = "ROM+MBC2"; + break; + case 0x06: + type = "ROM+MBC2+BATT"; + break; + case 0x0f: + type = "ROM+MBC3+TIMER+BATT"; + break; + case 0x10: + type = "ROM+MBC3+TIMER+RAM+BATT"; + break; + case 0x11: + type = "ROM+MBC3"; + break; + case 0x12: + type = "ROM+MBC3+RAM"; + break; + case 0x13: + type = "ROM+MBC3+RAM+BATT"; + break; + case 0x19: + type = "ROM+MBC5"; + break; + case 0x1a: + type = "ROM+MBC5+RAM"; + break; + case 0x1b: + type = "ROM+MBC5+RAM+BATT"; + break; + case 0x1c: + type = "ROM+MBC5+RUMBLE"; + break; + case 0x1d: + type = "ROM+MBC5+RUMBLE+RAM"; + break; + case 0x1e: + type = "ROM+MBC5+RUMBLE+RAM+BATT"; + break; + case 0x22: + type = "ROM+MBC7+BATT"; + break; + case 0xfe: + type = "ROM+HuC-3"; + break; + case 0xff: + type = "ROM+HuC-1"; + break; + } + sprintf(buffer, "%02x (%s)", rom[0x147], (const char *)type); + GetDlgItem(IDC_ROM_DEVICE_TYPE)->SetWindowText(buffer); + + type = winResLoadString(IDS_UNKNOWN); + switch (rom[0x148]) + { + case 0: + type = "32K"; + break; + case 1: + type = "64K"; + break; + case 2: + type = "128K"; + break; + case 3: + type = "256K"; + break; + case 4: + type = "512K"; + break; + case 5: + type = "1M"; + break; + case 6: + type = "2M"; + break; + case 7: + type = "4M"; + break; + } + + sprintf(buffer, "%02x (%s)", rom[0x148], (const char *)type); + GetDlgItem(IDC_ROM_SIZE)->SetWindowText(buffer); + + type = winResLoadString(IDS_UNKNOWN); + switch (rom[0x149]) + { + case 0: + type = winResLoadString(IDS_NONE); + break; + case 1: + type = "2K"; + break; + case 2: + type = "8K"; + break; + case 3: + type = "32K"; + break; + case 4: + type = "128K"; + break; + case 5: + type = "64K"; + break; + } + + sprintf(buffer, "%02x (%s)", rom[0x149], (const char *)type); + GetDlgItem(IDC_ROM_RAM_SIZE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0x14a]); + GetDlgItem(IDC_ROM_DEST_CODE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0x14b]); + GetDlgItem(IDC_ROM_LIC_CODE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0x14c]); + GetDlgItem(IDC_ROM_VERSION)->SetWindowText(buffer); + + u8 crc8 = 25; + int i; + for (i = 0x134; i < 0x14d; i++) + { + crc8 = (crc8 + rom[i]) & 0xFF; + } + + crc8 = 256 - crc8; + + sprintf(buffer, "%02x (%02x)", crc8, rom[0x14d]); + GetDlgItem(IDC_ROM_CRC)->SetWindowText(buffer); + + u16 crc16 = 0; + for (i = 0; i < gbRomSize; i++) + { + crc16 = (crc16 + rom[i]) & 0xFFFF; + } + + crc16 -= rom[0x14e]; + crc16 -= rom[0x14f]; + sprintf(buffer, "%04x (%04x)", crc16, (rom[0x14e] << 8) | rom[0x14f]); + GetDlgItem(IDC_ROM_CHECKSUM)->SetWindowText(buffer); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +///////////////////////////////////////////////////////////////////////////// +// RomInfoGBA dialog + +RomInfoGBA::RomInfoGBA(u8 *rom, CWnd *pParent /*=NULL*/) + : CDialog(RomInfoGBA::IDD, pParent) +{ + //{{AFX_DATA_INIT(RomInfoGBA) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + this->rom = rom; +} + +void RomInfoGBA::DoDataExchange(CDataExchange *pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(RomInfoGBA) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(RomInfoGBA, CDialog) +//{{AFX_MSG_MAP(RomInfoGBA) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// RomInfoGBA message handlers + +void RomInfoGBA::OnOk() +{ + EndDialog(TRUE); +} + +BOOL RomInfoGBA::OnInitDialog() +{ + CDialog::OnInitDialog(); + + char buffer[13]; + + strncpy(buffer, (const char *)&rom[0xa0], 12); + buffer[12] = 0; + GetDlgItem(IDC_ROM_TITLE)->SetWindowText(buffer); + + strncpy(buffer, (const char *)&rom[0xac], 4); + buffer[4] = 0; + GetDlgItem(IDC_ROM_GAME_CODE)->SetWindowText(buffer); + + strncpy(buffer, (const char *)&rom[0xb0], 2); + buffer[2] = 0; + GetDlgItem(IDC_ROM_MAKER_CODE)->SetWindowText(buffer); + + GetDlgItem(IDC_ROM_MAKER_NAME)->SetWindowText(winGBARomInfoFindMakerCode(buffer)); + + sprintf(buffer, "%02x", rom[0xb3]); + GetDlgItem(IDC_ROM_UNIT_CODE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0xb4]); + GetDlgItem(IDC_ROM_DEVICE_TYPE)->SetWindowText(buffer); + + sprintf(buffer, "%02x", rom[0xbc]); + GetDlgItem(IDC_ROM_VERSION)->SetWindowText(buffer); + + u8 crc = 0x19; + for (int i = 0xa0; i < 0xbd; i++) + { + crc = (crc + rom[i]) & 0xff; + } + + crc = (-crc) & 0xff; + + sprintf(buffer, "%02x (%02x)", crc, rom[0xbd]); + GetDlgItem(IDC_ROM_CRC)->SetWindowText(buffer); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/RomInfo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/RomInfo.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,79 @@ +#if !defined(AFX_ROMINFO_H__9888A45C_3E71_4C0F_B119_EFC74DFF8CD3__INCLUDED_) +#define AFX_ROMINFO_H__9888A45C_3E71_4C0F_B119_EFC74DFF8CD3__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// -*- C++ -*- +// RomInfo.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// RomInfoGB dialog + +class RomInfoGB : public CDialog +{ + // Construction +public: + RomInfoGB(u8 *rom, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(RomInfoGB) + enum { IDD = IDD_GB_ROM_INFO }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(RomInfoGB) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + u8 *rom; + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(RomInfoGB) + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +///////////////////////////////////////////////////////////////////////////// +// RomInfoGBA dialog + +class RomInfoGBA : public CDialog +{ + // Construction +public: + RomInfoGBA(u8 *rom, CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(RomInfoGBA) + enum { IDD = IDD_GBA_ROM_INFO }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + u8 *rom; + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(RomInfoGBA) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(RomInfoGBA) + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ROMINFO_H__9888A45C_3E71_4C0F_B119_EFC74DFF8CD3__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Sound.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Sound.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,19 @@ +#ifndef VBA_WIN32_SOUND_H +#define VBA_WIN32_SOUND_H + +class ISound +{ +public: + virtual ~ISound() {}; + + virtual bool init() = 0; + virtual void pause() = 0; + virtual void reset() = 0; + virtual void resume() = 0; + virtual void write() = 0; + virtual void setSpeed(float rate) = 0; + virtual bool isPlaying() = 0; + virtual void clearAudioBuffer() {} +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/StringTokenizer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/StringTokenizer.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,44 @@ +// StringTokenizer.cpp: implementation of the StringTokenizer class. +// +////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +#include "StringTokenizer.h" + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +StringTokenizer::StringTokenizer(CString str, CString del) +{ + m_right = str; + m_delim = del; +} + +StringTokenizer::~StringTokenizer() +{} + +const char *StringTokenizer::next() +{ + int index = m_right.FindOneOf(m_delim); + + while (index == 0) + { + m_right = m_right.Right(m_right.GetLength()-1); + index = m_right.FindOneOf(m_delim); + } + if (index == -1) + { + if (m_right.IsEmpty()) + return NULL; + m_token = m_right; + m_right.Empty(); + return m_token; + } + + m_token = m_right.Left(index); + m_right = m_right.Right(m_right.GetLength()-(1+index)); + + return m_token; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/StringTokenizer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/StringTokenizer.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,24 @@ +// StringTokenizer.h: interface for the StringTokenizer class. +// +////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_STRINGTOKENIZER_H__1AB4CD12_6B7A_49E4_A87F_75D3DC3FF20F__INCLUDED_) +#define AFX_STRINGTOKENIZER_H__1AB4CD12_6B7A_49E4_A87F_75D3DC3FF20F__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +class StringTokenizer +{ +public: + const char *next(); + StringTokenizer(CString str, CString token); + virtual ~StringTokenizer(); +private: + CString m_token; + CString m_delim; + CString m_right; +}; + +#endif // !defined(AFX_STRINGTOKENIZER_H__1AB4CD12_6B7A_49E4_A87F_75D3DC3FF20F__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/System.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/System.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,936 @@ +// System.cpp : Defines the system behaviors for the emulator. +// +#include "stdafx.h" +#include "Sound.h" +#include "Input.h" +#include "IUpdate.h" +#include "ram_search.h" +#include "WinMiscUtil.h" +#include "WinResUtil.h" +#include "resource.h" +#include "VBA.h" +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gba/GBASound.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +//#include "../common/System.h" +#include "../common/movie.h" +#include "../common/vbalua.h" +#include "../common/Text.h" +#include "../common/Util.h" +#include "../common/nesvideos-piece.h" +#include "../version.h" +#include + +struct EmulatedSystem theEmulator; + +u32 RGB_LOW_BITS_MASK = 0; +int emulating = 0; +int systemCartridgeType = 0; +int systemSpeed = 0; +bool systemSoundOn = false; +u32 systemColorMap32[0x10000]; +u16 systemColorMap16[0x10000]; +u16 systemGbPalette[24]; +int systemRedShift = 0; +int systemBlueShift = 0; +int systemGreenShift = 0; +int systemColorDepth = 16; +int systemDebug = 0; +int systemVerbose = 0; +int systemFrameSkip = 0; +int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + +const int32 INITIAL_SENSOR_VALUE = 2047; + +int32 sensorX = INITIAL_SENSOR_VALUE; +int32 sensorY = INITIAL_SENSOR_VALUE; +u16 currentButtons [4] = { 0, 0, 0, 0 }; // constrain: never contains hacked buttons, only the lower 16 bits of each are used +u16 lastKeys = 0; + +// static_assertion that BUTTON_REGULAR_RECORDING_MASK should be an u16 constant +namespace { const void * const s_STATIC_ASSERTION_(static_cast(BUTTON_REGULAR_RECORDING_MASK & 0xFFFF0000)); } + +#define BMP_BUFFER_MAX_WIDTH (256) +#define BMP_BUFFER_MAX_HEIGHT (224) +#define BMP_BUFFER_MAX_DEPTH (4) +static u8 bmpBuffer[BMP_BUFFER_MAX_WIDTH * BMP_BUFFER_MAX_HEIGHT * BMP_BUFFER_MAX_DEPTH]; + +static int s_stockThrottleValues[] = { + 6, 15, 25, 25, 37, 50, 75, 87, 100, 112, 125, 150, 200, 300, 400, 600, 800, 1000 +}; + +// systemXYZ: Win32 stuff + +// input + +void systemSetSensorX(int32 x) +{ + sensorX = x; +} + +void systemSetSensorY(int32 y) +{ + sensorY = y; +} + +void systemResetSensor() +{ + sensorX = sensorY = INITIAL_SENSOR_VALUE; +} + +int32 systemGetSensorX() +{ + return sensorX; +} + +int32 systemGetSensorY() +{ + return sensorY; +} + +// handles motion sensor input +void systemUpdateMotionSensor(int i) +{ + if (i < 0 || i > 3) + i = 0; + + if (currentButtons[i] & BUTTON_MASK_LEFT_MOTION) + { + sensorX += 3; + if (sensorX > 2197) + sensorX = 2197; + if (sensorX < 2047) + sensorX = 2057; + } + else if (currentButtons[i] & BUTTON_MASK_RIGHT_MOTION) + { + sensorX -= 3; + if (sensorX < 1897) + sensorX = 1897; + if (sensorX > 2047) + sensorX = 2037; + } + else if (sensorX > 2047) + { + sensorX -= 2; + if (sensorX < 2047) + sensorX = 2047; + } + else + { + sensorX += 2; + if (sensorX > 2047) + sensorX = 2047; + } + + if (currentButtons[i] & BUTTON_MASK_UP_MOTION) + { + sensorY += 3; + if (sensorY > 2197) + sensorY = 2197; + if (sensorY < 2047) + sensorY = 2057; + } + else if (currentButtons[i] & BUTTON_MASK_DOWN_MOTION) + { + sensorY -= 3; + if (sensorY < 1897) + sensorY = 1897; + if (sensorY > 2047) + sensorY = 2037; + } + else if (sensorY > 2047) + { + sensorY -= 2; + if (sensorY < 2047) + sensorY = 2047; + } + else + { + sensorY += 2; + if (sensorY > 2047) + sensorY = 2047; + } +} + +int systemGetDefaultJoypad() +{ + return theApp.joypadDefault; +} + +void systemSetDefaultJoypad(int which) +{ + theApp.joypadDefault = which; +} + +bool systemReadJoypads() +{ + // this function is called at every frame, even if vba is fast-forwarded. + // so we try to limit the input frequency here just in case. + static u32 lastTime = systemGetClock(); + if ((u32)(systemGetClock() - lastTime) < 10) + return false; + lastTime = systemGetClock(); + + if (theApp.input) + return theApp.input->readDevices(); + return false; +} + +u32 systemGetOriginalJoypad(int i, bool sensor) +{ + if (i < 0 || i > 3) + i = 0; + + u32 res = 0; + if (theApp.input) + res = theApp.input->readDevice(i, sensor); + + // +auto input, XOR + // maybe these should be moved into DirectInput.cpp + if (theApp.autoFire || theApp.autoFire2) + { + res ^= (theApp.autoFireToggle ? theApp.autoFire : theApp.autoFire2); + if (!theApp.autofireAccountForLag || !systemCounters.laggedLast) + { + theApp.autoFireToggle = !theApp.autoFireToggle; + } + } + if (theApp.autoHold) + { + res ^= theApp.autoHold; + } + + // filter buttons + // maybe better elsewhere? + if (!theApp.allowLeftRight) + { + // disallow L+R or U+D to being pressed at the same time + if ((res & (BUTTON_MASK_RIGHT | BUTTON_MASK_LEFT)) == (BUTTON_MASK_RIGHT | BUTTON_MASK_LEFT)) + res &= ~BUTTON_MASK_RIGHT; // leave only LEFT on + if ((res & (BUTTON_MASK_DOWN | BUTTON_MASK_UP)) == (BUTTON_MASK_DOWN | BUTTON_MASK_UP)) + res &= ~BUTTON_MASK_DOWN; // leave only UP on + } + + if (!sensor) + { + if (res & BUTTON_MOTION_MASK) + res &= ~BUTTON_MOTION_MASK; + } + + if (systemCartridgeType != 0 && !gbSgbMode) // regular GB has no L/R buttons + { + if (res & (BUTTON_GBA_ONLY)) + res &= ~BUTTON_GBA_ONLY; + } + + currentButtons[i] = res & BUTTON_REGULAR_RECORDING_MASK; + + return res; +} + +u32 systemGetJoypad(int i, bool sensor) +{ + if (i < 0 || i > 3) + i = 0; + + // input priority: original+auto < Lua < frame search < movie, correct this if wrong + + // get original+auto input + u32 hackedButtons = systemGetOriginalJoypad(i, sensor) & BUTTON_NONRECORDINGONLY_MASK; + u32 res = currentButtons[i]; + + // since movie input has the highest priority, there's no point to read from other input + if (VBAMoviePlaying()) + { + // VBAMovieRead() overwrites currentButtons[i] + VBAMovieRead(i, sensor); + res = currentButtons[i]; + } + else + { + // Lua input, shouldn't have any side effect within them + if (VBALuaUsingJoypad(i)) + res = VBALuaReadJoypad(i); + + // override input above + if (theApp.frameSearchSkipping) + res = theApp.frameSearchOldInput[i]; + + // flush non-hack buttons into the "current buttons" input buffer, which will be read by the movie routine + currentButtons[i] = res & BUTTON_REGULAR_RECORDING_MASK; + VBAMovieWrite(i, sensor); + } + + return res | hackedButtons; +} + +void systemSetJoypad(int which, u32 buttons) +{ + if (which < 0 || which > 3) + which = 0; + + currentButtons[which] = buttons; + + lastKeys = 0; +} + +void systemClearJoypads() +{ + for (int i = 0; i < 3; ++i) + currentButtons[i] = 0; + + lastKeys = 0; +} + +// screen + +// delayed repaint +void systemRefreshScreen() +{ + if (theApp.m_pMainWnd) + { + theApp.m_pMainWnd->PostMessage(WM_PAINT, NULL, NULL); + } +} + +extern bool vbaShuttingDown; + +void systemRenderFrame() +{ + extern long linearSoundFrameCount; + extern long linearFrameCount; + + if (vbaShuttingDown) + return; + + ++theApp.renderedFrames; + + VBAUpdateFrameCountDisplay(); + VBAUpdateButtonPressDisplay(); + + // "in-game" text rendering + if (textMethod == 0) // transparent text can only be painted once, so timed messages will not be updated + { + extern void DrawLuaGui(); + DrawLuaGui(); + + int copyX = 240, copyY = 160; + if (systemCartridgeType == 1) + if (gbBorderOn) + copyX = 256, copyY = 224; + else + copyX = 160, copyY = 144; + int pitch = copyX * (systemColorDepth / 8) + (systemColorDepth == 24 ? 0 : 4); // FIXME: sure? + + DrawTextMessages((u8 *)pix, pitch, 0, copyY); + } + + ++linearFrameCount; + if (!theApp.sound) + { + if (linearFrameCount > 10000) + linearFrameCount -= 10000; + linearSoundFrameCount = linearFrameCount; + } + + // record avi + int width = 240; + int height = 160; + switch (systemCartridgeType) + { + case 0: + width = 240; + height = 160; + break; + case 1: + if (gbBorderOn) + { + width = 256; + height = 224; + } + else + { + width = 160; + height = 144; + } + break; + } + + bool firstFrameLogged = false; + --linearFrameCount; + do + { + ++linearFrameCount; + + if (theApp.aviRecording && (!theApp.altAviRecordMethod || (theApp.altAviRecordMethod && !firstFrameLogged))) + { + // usually aviRecorder is created when vba starts avi recording, though + if (theApp.aviRecorder == NULL) + { + theApp.aviRecorder = new AVIWrite(); + + theApp.aviRecorder->SetFPS(60); + + BITMAPINFOHEADER bi; + memset(&bi, 0, sizeof(bi)); + bi.biSize = 0x28; + bi.biPlanes = 1; + bi.biBitCount = 24; + bi.biWidth = width; + bi.biHeight = height; + bi.biSizeImage = 3 * width * height; + theApp.aviRecorder->SetVideoFormat(&bi); + if (!theApp.aviRecorder->Open(theApp.aviRecordName)) + { + delete theApp.aviRecorder; + theApp.aviRecorder = NULL; + theApp.aviRecording = false; + } + } + + if (theApp.aviRecorder != NULL && !theApp.aviRecorder->IsPaused()) + { + assert( + width <= BMP_BUFFER_MAX_WIDTH && height <= BMP_BUFFER_MAX_HEIGHT && systemColorDepth <= + BMP_BUFFER_MAX_DEPTH * 8); + utilWriteBMP(bmpBuffer, width, height, systemColorDepth, pix); + theApp.aviRecorder->AddFrame(bmpBuffer); + } + } + + if (theApp.nvVideoLog) + { + // convert from whatever bit depth to 16-bit, while stripping away extra pixels + assert(width <= BMP_BUFFER_MAX_WIDTH && height <= BMP_BUFFER_MAX_HEIGHT && 16 <= BMP_BUFFER_MAX_DEPTH * 8); + utilWriteBMP(bmpBuffer, width, -height, 16, pix); + NESVideoLoggingVideo((u8 *)bmpBuffer, width, height, 0x1000000 * 60); + } + + firstFrameLogged = true; + } + while (linearFrameCount < linearSoundFrameCount); // compensate for frames lost due to frame skip being nonzero, etc. + + if (textMethod != 0) // do not draw Lua HUD to a video dump + { + extern void DrawLuaGui(); + DrawLuaGui(); + } + + // interframe blending + if (theApp.ifbFunction) + { + if (systemColorDepth == 16) + theApp.ifbFunction(pix + theApp.filterWidth * 2 + 4, theApp.filterWidth * 2 + 4, + theApp.filterWidth, theApp.filterHeight); + else + theApp.ifbFunction(pix + theApp.filterWidth * 4 + 4, theApp.filterWidth * 4 + 4, + theApp.filterWidth, theApp.filterHeight); + } + + systemRedrawScreen(); +} + +void systemRedrawScreen() +{ + if (vbaShuttingDown) + return; + + if (theApp.display) + theApp.display->render(); + + systemUpdateListeners(); +} + +void systemUpdateListeners() +{ + if (vbaShuttingDown) + return; + + Update_RAM_Search(); // updates RAM search and RAM watch + + // update viewers etc. + if (theApp.updateCount) + { + POSITION pos = theApp.updateList.GetHeadPosition(); + while (pos) + { + IUpdateListener *up = theApp.updateList.GetNext(pos); + if (up) + up->update(); + } + } +} + +int systemScreenCapture(int captureNumber) +{ + return winScreenCapture(captureNumber); +} + +void systemMessage(int number, const char *defaultMsg, ...) +{ + CString buffer; + va_list valist; + CString msg = defaultMsg; + if (number) + msg = winResLoadString(number); + + va_start(valist, defaultMsg); + buffer.FormatV(msg, valist); + + theApp.winCheckFullscreen(); + systemSoundClearBuffer(); + AfxGetApp()->m_pMainWnd->MessageBox(buffer, winResLoadString(IDS_ERROR), MB_OK | MB_ICONERROR); + + va_end(valist); +} + +void systemScreenMessage(const char *msg, int slot, int duration, const char *colorList) +{ + if (slot < 0 || slot > SCREEN_MESSAGE_SLOTS) + return; + + theApp.screenMessage[slot] = true; + theApp.screenMessageTime[slot] = GetTickCount(); + theApp.screenMessageDuration[slot] = duration; + theApp.screenMessageBuffer[slot] = msg; + theApp.screenMessageColorBuffer[slot] = colorList ? colorList : ""; + + if (theApp.screenMessageBuffer[slot].GetLength() > 40) + theApp.screenMessageBuffer[slot] = theApp.screenMessageBuffer[slot].Left(40); + + // update the display when a main slot message appears while the game is paused + if (slot == 0 && (theApp.paused || (theApp.frameSearching))) + systemRefreshScreen(); +} + +void systemShowSpeed(int speed) +{ + systemSpeed = speed; + theApp.showRenderedFrames = theApp.renderedFrames; + theApp.renderedFrames = 0; + if (theApp.videoOption <= VIDEO_4X && theApp.showSpeed) + { + CString buffer; + if (theApp.showSpeed == 1) + buffer.Format(VBA_NAME_AND_VERSION " %3d%%", systemSpeed); + else + buffer.Format(VBA_NAME_AND_VERSION " %3d%% (%d fps | %d skipped)", + systemSpeed, + theApp.showRenderedFrames, + systemFrameSkip); + + systemSetTitle(buffer); + } +} + +void systemSetTitle(const char *title) +{ + if (theApp.m_pMainWnd != NULL) + { + AfxGetApp()->m_pMainWnd->SetWindowText(title); + } +} + +// timing/speed + +u32 systemGetClock() +{ + return timeGetTime(); +} + +void systemIncreaseThrottle() +{ + int throttle = theApp.throttle; + + if (throttle < 6) + ++throttle; + else if (throttle < s_stockThrottleValues[_countof(s_stockThrottleValues) - 1]) + { + int i = 0; + while (throttle >= s_stockThrottleValues[i]) + { + ++i; + } + throttle = s_stockThrottleValues[i]; + } + + systemSetThrottle(throttle); +} + +void systemDecreaseThrottle() +{ + int throttle = theApp.throttle; + + if (throttle > 6) + { + int i = _countof(s_stockThrottleValues) - 1; + while (throttle <= s_stockThrottleValues[i]) + { + --i; + } + throttle = s_stockThrottleValues[i]; + } + else if (throttle > 1) + --throttle; + + systemSetThrottle(throttle); +} + +void systemSetThrottle(int throttle) +{ + theApp.throttle = throttle; + char str[256]; + sprintf(str, "%d%% throttle speed", theApp.throttle); + systemScreenMessage(str); +} + +int systemGetThrottle() +{ + return theApp.throttle; +} + +void systemFrame() +{ + if (theApp.altAviRecordMethod && theApp.aviRecording) + { + if (theApp.aviRecorder) + { + if (!theApp.aviRecorder->IsSoundAdded()) + { + WAVEFORMATEX wfx; + memset(&wfx, 0, sizeof(wfx)); + wfx.wFormatTag = WAVE_FORMAT_PCM; + wfx.nChannels = 2; + wfx.nSamplesPerSec = 44100 / soundQuality; + wfx.wBitsPerSample = 16; + wfx.nBlockAlign = (wfx.wBitsPerSample / 8) * wfx.nChannels; + wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; + wfx.cbSize = 0; + theApp.aviRecorder->SetSoundFormat(&wfx); + } + theApp.aviRecorder->AddSound((u8 *)soundFrameSound, soundFrameSoundWritten * 2); + } + } + + soundFrameSoundWritten = 0; + + // no more stupid updates :) + + extern int quitAfterTime; // from VBA.cpp + void VBAMovieStop(bool8 suppress_message); // from ../movie.cpp + if (quitAfterTime >= 0 && systemCounters.frameCount == quitAfterTime) + { + VBAMovieStop(true); + AfxPostQuitMessage(0); + } + + // change the sound speed, or set it to normal - must always do this or it won't get reset after a change, but that's OK + // because it's inexpensive + if (theApp.sound) + { + theApp.sound->setSpeed( + speedup || theApp.winPauseNextFrame || !synchronize || theApp.accuratePitchThrottle || theApp.useOldSync + ? 1.0f : (float)theApp.throttle / 100.0f); + } + + // if a throttle speed is set and we're not allowed to change the sound frequency to achieve it, + // sleep for a certain amount each time we get here to approximate the necessary slowdown + if (synchronize && (theApp.accuratePitchThrottle || !theApp.sound || theApp.throttle < 6) /*&& !theApp.winPauseNextFrame*/) + { + /// FIXME: this is still a horrible way of achieving a certain frame time + /// (look at what Snes9x does - it's complicated but much much better) + + static float sleepAmt = 0.0f; // variable to smooth out the sleeping amount so it doesn't oscillate so fast +// if(!theApp.wasPaused) { + if (!speedup) + { + u32 time = systemGetClock(); + u32 diff = time - theApp.throttleLastTime; + if (theApp.wasPaused) + diff = 0; + + int target = (100000 / (60 * theApp.throttle)); + int d = (target - diff); + + if (d > 1000) // added to avoid 500-day waits for vba to start emulating. + d = 1000; // I suspect most users aren't that patient, and would find 1 second to be a more reasonable delay. + + sleepAmt = 0.8f * sleepAmt + 0.2f * (float)d; + if (d - sleepAmt <= 1.5f && d - sleepAmt >= -1.5f) + d = (int)(sleepAmt); + + if (d > 0) + { + Sleep(d); + } + } + theApp.throttleLastTime = systemGetClock(); + //} + //else + //{ + // Sleep(100); + //} + } + + if (systemCounters.frameCount % 10 == 0) + { + if (theApp.rewindMemory) + { + if (++theApp.rewindCounter >= (theApp.rewindTimer)) + { + theApp.rewindSaveNeeded = true; + theApp.rewindCounter = 0; + } + } + if (systemSaveUpdateCounter) + { + if (--systemSaveUpdateCounter <= SYSTEM_SAVE_NOT_UPDATED) + { + winWriteBatteryFile(); + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + } + } + } + + theApp.wasPaused = false; +/// theApp.autoFrameSkipLastTime = time; +} + +int systemFramesToSkip() +{ + int framesToSkip = systemFrameSkip; + + bool fastForward = speedup; + +#if (defined(WIN32) && !defined(SDL)) + fastForward = (fastForward || theApp.frameSearchSkipping); + int throttle = theApp.throttle; + if (theApp.frameSearching && throttle < 100) + throttle = 100; +#else + extern int throttle; +#endif + +#if (defined(WIN32) && !defined(SDL)) + if (theApp.aviRecording || theApp.nvVideoLog) + { + framesToSkip = 0; // render all frames + } + else + { + if (fastForward) + framesToSkip = 9; // try 6 FPS during speedup + else if (throttle != 100) + framesToSkip = (framesToSkip * throttle) / 100; + } +#endif + + return framesToSkip; +} + +// sound + +bool systemSoundInit() +{ + if (theApp.sound) + delete theApp.sound; + + extern ISound *newDirectSound(); + theApp.sound = newDirectSound(); + return theApp.sound->init(); +} + +void systemSoundShutdown() +{ + if (theApp.sound) + delete theApp.sound; + theApp.sound = NULL; +} + +void systemSoundPause() +{ + if (theApp.sound) + theApp.sound->pause(); + soundPaused = 1; +} + +void systemSoundResume() +{ + if (theApp.sound) + theApp.sound->resume(); + soundPaused = 0; +} + +bool systemSoundIsPaused() +{ +// return soundPaused; + return !(theApp.sound && theApp.sound->isPlaying()); +} + +void systemSoundClearBuffer() +{ + if (theApp.sound) + theApp.sound->clearAudioBuffer(); +} + +void systemSoundReset() +{ + if (theApp.sound) + theApp.sound->reset(); +} + +void systemSoundWriteToBuffer() +{ + if (theApp.sound) + theApp.sound->write(); +} + +bool systemSoundCanChangeQuality() +{ + return true; +} + +bool systemSoundSetQuality(int quality) +{ + if (systemCartridgeType == 0) + soundSetQuality(quality); + else + gbSoundSetQuality(quality); + + return true; +} + +// emulation + +bool systemIsEmulating() +{ + return emulating != 0; +} + +void systemGbBorderOn() +{ + if (vbaShuttingDown) + return; + + if (emulating && systemCartridgeType == 1) + { + theApp.updateWindowSize(theApp.videoOption); + } +} + +bool systemIsRunningGBA() +{ + return (systemCartridgeType == 0); +} + +bool systemIsSpedUp() +{ + return theApp.speedupToggle; +} + +bool systemIsPaused() +{ + return theApp.paused; +} + +void systemSetPause(bool pause) +{ + if (pause) + { + capturePrevious = false; + theApp.wasPaused = true; + theApp.paused = true; + theApp.speedupToggle = false; + theApp.winPauseNextFrame = false; + systemSoundPause(); + systemRefreshScreen();; + } + else + { + theApp.paused = false; + systemSoundResume(); + } +} + +// aka. frame advance +bool systemPauseOnFrame() +{ + if (theApp.winPauseNextFrame) + { + if (!theApp.nextframeAccountForLag || !systemCounters.laggedLast) + { + theApp.winPauseNextFrame = false; + return true; + } + } + + return false; +} + +bool systemLoadBIOS(const char *biosFileName, bool useBiosFile) +{ + bool use = false; + if (systemCartridgeType == 0) + use = CPULoadBios(biosFileName, useBiosFile); + else + use = false; + return use; +} + +// FIXME: now platform-independant stuff +// it should be admitted that the naming schema/code organization is a whole mess +// these things should be moved somewhere else + +EmulatedSystemCounters systemCounters = +{ + // frameCount + 0, + // lagCount + 0, + // extraCount + 0, + // lagged + true, + // laggedLast + true, +}; + +// VBAxyz stuff are not part of the core. + +void VBAOnEnteringFrameBoundary() +{ + CallRegisteredLuaFunctions(LUACALL_AFTEREMULATION); + + if (VBALuaRunning()) + { + VBALuaFrameBoundary(); + } + + VBAMovieUpdateState(); +} + +void VBAOnExitingFrameBoundary() +{ + ; +} + +////////////////////////////////////////////// +// ultility + +extern void toolsLog(const char *); + +void log(const char *msg, ...) +{ + CString buffer; + va_list valist; + + va_start(valist, msg); + buffer.FormatV(msg, valist); + + toolsLog(buffer); + + va_end(valist); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/TextOptions.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/TextOptions.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,81 @@ +// TextOptions.cpp : implementation file +// + +#include "stdafx.h" +#include "../common/Text.h" +#include "resource.h" +#include "TextOptions.h" + +// TextOptions dialog + +IMPLEMENT_DYNAMIC(TextOptions, CDialog) +TextOptions::TextOptions(CWnd*pParent /*=NULL*/) + : CDialog(TextOptions::IDD, pParent) +{} + +TextOptions::~TextOptions() +{} + +BOOL TextOptions::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CheckRadioButton(IDC_RADIO_WHITE, IDC_RADIO_BLACK, IDC_RADIO_WHITE + textColor); + CheckRadioButton(IDC_RADIO_PREFILTER, IDC_RADIO_POSTRENDER, IDC_RADIO_PREFILTER + textMethod); + CheckDlgButton(IDC_CHECK_OUTLINED, outlinedText); + CheckDlgButton(IDC_CHECK_TRANSPARENT, transparentText); + GetDlgItem(IDC_CHECK_TRANSPARENT)->EnableWindow(GetCheckedRadioButton(IDC_RADIO_PREFILTER, + IDC_RADIO_POSTRENDER) != IDC_RADIO_POSTRENDER); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void TextOptions::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); +} + +BEGIN_MESSAGE_MAP(TextOptions, CDialog) +ON_BN_CLICKED(IDOK, OnBnClickedOk) +ON_BN_CLICKED(IDC_RADIO_PREFILTER, OnBnClickedRadioPrefilter) +ON_BN_CLICKED(IDC_RADIO_POSTFILTER, OnBnClickedRadioPostfilter) +ON_BN_CLICKED(IDC_RADIO_POSTRENDER, OnBnClickedRadioPostrender) +END_MESSAGE_MAP() + +// TextOptions message handlers + +void TextOptions::OnBnClickedOk() +{ + transparentText = IsDlgButtonChecked(IDC_CHECK_TRANSPARENT) != 0; + outlinedText = IsDlgButtonChecked(IDC_CHECK_OUTLINED) != 0; + textMethod = GetCheckedRadioButton(IDC_RADIO_PREFILTER, IDC_RADIO_POSTRENDER) - IDC_RADIO_PREFILTER; + textColor = GetCheckedRadioButton(IDC_RADIO_WHITE, IDC_RADIO_BLACK) - IDC_RADIO_WHITE; + if (textMethod < 0) + textMethod = 0; + if (textMethod > 2) + textMethod = 2; + if (textColor < 0) + textColor = 0; + if (textColor > 7) + textColor = 7; + + OnOK(); +} + +void TextOptions::OnBnClickedRadioPrefilter() +{ + GetDlgItem(IDC_CHECK_TRANSPARENT)->EnableWindow(TRUE); +} + +void TextOptions::OnBnClickedRadioPostfilter() +{ + GetDlgItem(IDC_CHECK_TRANSPARENT)->EnableWindow(TRUE); +} + +void TextOptions::OnBnClickedRadioPostrender() +{ + GetDlgItem(IDC_CHECK_TRANSPARENT)->EnableWindow(FALSE); + CheckDlgButton(IDC_CHECK_TRANSPARENT, FALSE); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/TextOptions.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/TextOptions.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,34 @@ +#ifndef VBA_WIN32_TEXT_OPTIONS_H +#define VBA_WIN32_TEXT_OPTIONS_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +// TextOptions dialog + +class TextOptions : public CDialog +{ + DECLARE_DYNAMIC(TextOptions) + +public: + TextOptions(CWnd* pParent = NULL); // standard constructor + virtual ~TextOptions(); + + virtual BOOL OnInitDialog() ; + +// Dialog Data + enum { IDD = IDD_TEXTCONFIG }; + +protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + + DECLARE_MESSAGE_MAP() +public: + afx_msg void OnBnClickedOk(); + afx_msg void OnBnClickedRadioPrefilter(); + afx_msg void OnBnClickedRadioPostfilter(); + afx_msg void OnBnClickedRadioPostrender(); +}; + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Throttle.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Throttle.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,62 @@ +// Throttle.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "Throttle.h" +#include "../common/System.h" // for system messages + +///////////////////////////////////////////////////////////////////////////// +// Throttle dialog + +Throttle::Throttle(CWnd*pParent /*=NULL*/) + : CDialog(Throttle::IDD, pParent) +{ + //{{AFX_DATA_INIT(Throttle) + m_throttle = 100; + //}}AFX_DATA_INIT +} + +void Throttle::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(Throttle) + DDX_Text(pDX, IDC_THROTTLE, m_throttle); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(Throttle, CDialog) +//{{AFX_MSG_MAP(Throttle) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// Throttle message handlers + +BOOL Throttle::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void Throttle::OnCancel() +{ + EndDialog(false); +} + +void Throttle::OnOk() +{ + UpdateData(); + + if (m_throttle < 1 || m_throttle > 1000) + systemMessage(IDS_INVALID_THROTTLE_VALUE, "Invalid throttle value. Please enter a number between 1 and 1000"); + else + EndDialog(m_throttle); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/Throttle.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/Throttle.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#if !defined(AFX_THROTTLE_H__5F03B6E9_0C43_4933_A7BC_1618428C2B7F__INCLUDED_) +#define AFX_THROTTLE_H__5F03B6E9_0C43_4933_A7BC_1618428C2B7F__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// Throttle.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// Throttle dialog + +class Throttle : public CDialog +{ + // Construction +public: + Throttle(CWnd*pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(Throttle) + enum { IDD = IDD_THROTTLE }; + int m_throttle; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(Throttle) +protected: + virtual void DoDataExchange(CDataExchange*pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(Throttle) + virtual BOOL OnInitDialog(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_THROTTLE_H__5F03B6E9_0C43_4933_A7BC_1618428C2B7F__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/TileView.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/TileView.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,582 @@ +// TileView.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" +#include "FileDlg.h" +#include "Reg.h" +#include "TileView.h" +#include "WinResUtil.h" +#include "VBA.h" // for theApp + +#include "../gba/GBAGlobals.h" +#include "../NLS.h" +#include "../common/Util.h" + +extern "C" { +#include +} + +///////////////////////////////////////////////////////////////////////////// +// TileView dialog + +TileView::TileView(CWnd*pParent /*=NULL*/) + : ResizeDlg(TileView::IDD, pParent) +{ + //{{AFX_DATA_INIT(TileView) + m_colors = -1; + m_charBase = -1; + m_stretch = FALSE; + //}}AFX_DATA_INIT + autoUpdate = false; + + memset(&bmpInfo, 0, sizeof(bmpInfo)); + + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); + bmpInfo.bmiHeader.biWidth = 32*8; + bmpInfo.bmiHeader.biHeight = 32*8; + bmpInfo.bmiHeader.biPlanes = 1; + bmpInfo.bmiHeader.biBitCount = 24; + bmpInfo.bmiHeader.biCompression = BI_RGB; + data = (u8 *)calloc(1, 3 * 32*32 * 64); + + tileView.setData(data); + tileView.setBmpInfo(&bmpInfo); + + charBase = 0; + is256Colors = 0; + palette = 0; + w = h = 0; +} + +TileView::~TileView() +{ + free(data); + data = NULL; +} + +void TileView::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(TileView) + DDX_Control(pDX, IDC_PALETTE_SLIDER, m_slider); + DDX_Radio(pDX, IDC_16_COLORS, m_colors); + DDX_Radio(pDX, IDC_CHARBASE_0, m_charBase); + DDX_Check(pDX, IDC_STRETCH, m_stretch); + //}}AFX_DATA_MAP + DDX_Control(pDX, IDC_TILE_VIEW, tileView); + DDX_Control(pDX, IDC_MAP_VIEW_ZOOM, zoom); + DDX_Control(pDX, IDC_COLOR, color); +} + +BEGIN_MESSAGE_MAP(TileView, CDialog) +//{{AFX_MSG_MAP(TileView) +ON_BN_CLICKED(IDC_SAVE, OnSave) +ON_BN_CLICKED(IDC_CLOSE, OnClose) +ON_BN_CLICKED(IDC_AUTO_UPDATE, OnAutoUpdate) +ON_BN_CLICKED(IDC_16_COLORS, On16Colors) +ON_BN_CLICKED(IDC_256_COLORS, On256Colors) +ON_BN_CLICKED(IDC_CHARBASE_0, OnCharbase0) +ON_BN_CLICKED(IDC_CHARBASE_1, OnCharbase1) +ON_BN_CLICKED(IDC_CHARBASE_2, OnCharbase2) +ON_BN_CLICKED(IDC_CHARBASE_3, OnCharbase3) +ON_BN_CLICKED(IDC_CHARBASE_4, OnCharbase4) +ON_BN_CLICKED(IDC_STRETCH, OnStretch) +ON_WM_HSCROLL() +//}}AFX_MSG_MAP +ON_MESSAGE(WM_MAPINFO, OnMapInfo) +ON_MESSAGE(WM_COLINFO, OnColInfo) +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// TileView message handlers + +void TileView::saveBMP(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + struct + { + u8 ident[2]; + u8 filesize[4]; + u8 reserved[4]; + u8 dataoffset[4]; + u8 headersize[4]; + u8 width[4]; + u8 height[4]; + u8 planes[2]; + u8 bitsperpixel[2]; + u8 compression[4]; + u8 datasize[4]; + u8 hres[4]; + u8 vres[4]; + u8 colors[4]; + u8 importantcolors[4]; + u8 pad[2]; + } bmpheader; + memset(&bmpheader, 0, sizeof(bmpheader)); + + bmpheader.ident[0] = 'B'; + bmpheader.ident[1] = 'M'; + + u32 fsz = sizeof(bmpheader) + w*h*3; + utilPutDword(bmpheader.filesize, fsz); + utilPutDword(bmpheader.dataoffset, 0x38); + utilPutDword(bmpheader.headersize, 0x28); + utilPutDword(bmpheader.width, w); + utilPutDword(bmpheader.height, h); + utilPutDword(bmpheader.planes, 1); + utilPutDword(bmpheader.bitsperpixel, 24); + utilPutDword(bmpheader.datasize, 3*w*h); + + fwrite(&bmpheader, 1, sizeof(bmpheader), fp); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data+3*w*(h-1); + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + *b++ = *pixU8++; // B + *b++ = *pixU8++; // G + *b++ = *pixU8++; // R + } + pixU8 -= 2*3*w; + fwrite(writeBuffer, 1, 3*w, fp); + + b = writeBuffer; + } + + fclose(fp); +} + +void TileView::savePNG(const char *name) +{ + u8 writeBuffer[1024 * 3]; + + FILE *fp = fopen(name, "wb"); + + if (!fp) + { + systemMessage(MSG_ERROR_CREATING_FILE, "Error creating file %s", name); + return; + } + + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + NULL, + NULL, + NULL); + if (!png_ptr) + { + fclose(fp); + return; + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + if (setjmp(png_ptr->jmpbuf)) + { + png_destroy_write_struct(&png_ptr, NULL); + fclose(fp); + return; + } + + png_init_io(png_ptr, fp); + + png_set_IHDR(png_ptr, + info_ptr, + w, + h, + 8, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, + PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + u8 *b = writeBuffer; + + int sizeX = w; + int sizeY = h; + + u8 *pixU8 = (u8 *)data; + for (int y = 0; y < sizeY; y++) + { + for (int x = 0; x < sizeX; x++) + { + int blue = *pixU8++; + int green = *pixU8++; + int red = *pixU8++; + + *b++ = red; + *b++ = green; + *b++ = blue; + } + png_write_row(png_ptr, writeBuffer); + + b = writeBuffer; + } + + png_write_end(png_ptr, info_ptr); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} + +void TileView::OnSave() +{ + CString captureBuffer; + + if (theApp.captureFormat == 0) + captureBuffer = "tiles.png"; + else + captureBuffer = "tiles.bmp"; + + LPCTSTR exts[] = {".png", ".bmp", NULL }; + + CString filter = winResLoadFilter(IDS_FILTER_PNG); + CString title = winResLoadString(IDS_SELECT_CAPTURE_NAME); + + FileDlg dlg(this, + captureBuffer, + filter, + theApp.captureFormat ? 2 : 1, + theApp.captureFormat ? "BMP" : "PNG", + exts, + "", + title, + true); + + if (dlg.DoModal() == IDCANCEL) + { + return; + } + + captureBuffer = dlg.GetPathName(); + + if (dlg.getFilterIndex() == 2) + saveBMP(captureBuffer); + else + savePNG(captureBuffer); +} + +void TileView::renderTile256(int tile, int x, int y, u8 *charBase, u16 *palette) +{ + u8 *bmp = &data[24*x + 8*32*24*y]; + + for (int j = 0; j < 8; j++) + { + for (int i = 0; i < 8; i++) + { + u8 c = charBase[tile*64 + j * 8 + i]; + + u16 color = palette[c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + bmp += 31*24; // advance line + } +} + +void TileView::renderTile16(int tile, int x, int y, u8 *charBase, u16 *palette) +{ + u8 *bmp = &data[24*x + 8*32*24*y]; + + int pal = this->palette; + + if (this->charBase == 4) + pal += 16; + + for (int j = 0; j < 8; j++) + { + for (int i = 0; i < 8; i++) + { + u8 c = charBase[tile*32 + j * 4 + (i>>1)]; + + if (i & 1) + c = c>>4; + else + c = c & 15; + + u16 color = palette[pal*16+c]; + + *bmp++ = ((color >> 10) & 0x1f) << 3; + *bmp++ = ((color >> 5) & 0x1f) << 3; + *bmp++ = (color & 0x1f) << 3; + } + bmp += 31*24; // advance line + } +} + +void TileView::render() +{ + u16 *palette = (u16 *)paletteRAM; + u8 * charBase = &vram[this->charBase * 0x4000]; + + int maxY; + + if (is256Colors) + { + int tile = 0; + maxY = 16; + for (int y = 0; y < maxY; y++) + { + for (int x = 0; x < 32; x++) + { + if (this->charBase == 4) + renderTile256(tile, x, y, charBase, &palette[256]); + else + renderTile256(tile, x, y, charBase, palette); + tile++; + } + } + tileView.setSize(32*8, maxY*8); + w = 32*8; + h = maxY*8; + SIZE s; + s.cx = 32*8; + s.cy = maxY*8; + if (tileView.getStretch()) + { + s.cx = s.cy = 1; + } + tileView.SetScrollSizes(MM_TEXT, s); + } + else + { + int tile = 0; + maxY = 32; + if (this->charBase == 3) + maxY = 16; + for (int y = 0; y < maxY; y++) + { + for (int x = 0; x < 32; x++) + { + renderTile16(tile, x, y, charBase, palette); + tile++; + } + } + tileView.setSize(32*8, maxY*8); + w = 32*8; + h = maxY*8; + SIZE s; + s.cx = 32*8; + s.cy = maxY*8; + if (tileView.getStretch()) + { + s.cx = s.cy = 1; + } + tileView.SetScrollSizes(MM_TEXT, s); + } +} + +void TileView::update() +{ + paint(); +} + +BOOL TileView::OnInitDialog() +{ + CDialog::OnInitDialog(); + + DIALOG_SIZER_START(sz) + DIALOG_SIZER_ENTRY(IDC_TILE_VIEW, DS_SizeX | DS_SizeY) + DIALOG_SIZER_ENTRY(IDC_COLOR, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_R, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_G, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_B, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_REFRESH, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_CLOSE, DS_MoveY) + DIALOG_SIZER_ENTRY(IDC_SAVE, DS_MoveY) + DIALOG_SIZER_END() + SetData(sz, + TRUE, + HKEY_CURRENT_USER, + "Software\\Emulators\\VisualBoyAdvance\\Viewer\\TileView", + NULL); + + m_colors = is256Colors; + m_charBase = charBase; + + m_slider.SetRange(0, 15); + m_slider.SetPageSize(4); + m_slider.SetTicFreq(1); + + paint(); + + m_stretch = regQueryDwordValue("tileViewStretch", 0); + if (m_stretch) + tileView.setStretch(true); + UpdateData(FALSE); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void TileView::OnClose() +{ + theApp.winRemoveUpdateListener(this); + + DestroyWindow(); +} + +void TileView::OnAutoUpdate() +{ + autoUpdate = !autoUpdate; + if (autoUpdate) + { + theApp.winAddUpdateListener(this); + } + else + { + theApp.winRemoveUpdateListener(this); + } +} + +void TileView::paint() +{ + if (vram != NULL && paletteRAM != NULL) + { + render(); + tileView.refresh(); + } +} + +void TileView::On16Colors() +{ + is256Colors = 0; + paint(); +} + +void TileView::On256Colors() +{ + is256Colors = 1; + paint(); +} + +void TileView::OnCharbase0() +{ + charBase = 0; + paint(); +} + +void TileView::OnCharbase1() +{ + charBase = 1; + paint(); +} + +void TileView::OnCharbase2() +{ + charBase = 2; + paint(); +} + +void TileView::OnCharbase3() +{ + charBase = 3; + paint(); +} + +void TileView::OnCharbase4() +{ + charBase = 4; + paint(); +} + +void TileView::OnStretch() +{ + tileView.setStretch(!tileView.getStretch()); + paint(); + regSetDwordValue("tileViewStretch", tileView.getStretch()); +} + +LRESULT TileView::OnMapInfo(WPARAM wParam, LPARAM lParam) +{ + u8 *colors = (u8 *)lParam; + zoom.setColors(colors); + + int x = (wParam & 0xFFFF)/8; + int y = ((wParam >> 16) & 0xFFFF)/8; + + u32 address = 0x6000000 + 0x4000 * charBase; + int tile = 32 * y + x; + if (is256Colors) + tile *= 2; + address += 32 * tile; + + CString buffer; + buffer.Format("%d", tile); + GetDlgItem(IDC_TILE_NUMBER)->SetWindowText(buffer); + + buffer.Format("%08x", address); + GetDlgItem(IDC_ADDRESS)->SetWindowText(buffer); + + return TRUE; +} + +LRESULT TileView::OnColInfo(WPARAM wParam, LPARAM) +{ + u16 c = (u16)wParam; + + color.setColor(c); + + int r = (c & 0x1f); + int g = (c & 0x3e0) >> 5; + int b = (c & 0x7c00) >> 10; + + CString buffer; + buffer.Format("R: %d", r); + GetDlgItem(IDC_R)->SetWindowText(buffer); + + buffer.Format("G: %d", g); + GetDlgItem(IDC_G)->SetWindowText(buffer); + + buffer.Format("B: %d", b); + GetDlgItem(IDC_B)->SetWindowText(buffer); + + return TRUE; +} + +void TileView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar*pScrollBar) +{ + switch (nSBCode) + { + case TB_THUMBPOSITION: + palette = nPos; + break; + default: + palette = m_slider.GetPos(); + break; + } + paint(); +} + +void TileView::PostNcDestroy() +{ + delete this; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/TileView.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/TileView.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,90 @@ +#if !defined(AFX_TILEVIEW_H__055751EC_2DF3_495B_B643_29025465CD2E__INCLUDED_) +#define AFX_TILEVIEW_H__055751EC_2DF3_495B_B643_29025465CD2E__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// TileView.h : header file +// + +#include "BitmapControl.h" +#include "ColorControl.h" +#include "IUpdate.h" +#include "ResizeDlg.h" +#include "ZoomControl.h" + +///////////////////////////////////////////////////////////////////////////// +// TileView dialog + +class TileView : public ResizeDlg, IUpdateListener +{ + int charBase; + int is256Colors; + int palette; + BitmapControl tileView; + BITMAPINFO bmpInfo; + u8 *data; + ZoomControl zoom; + ColorControl color; + int w; + int h; + bool autoUpdate; + // Construction + public: + void paint(); + void render(); + void renderTile16(int tile, int x, int y, u8 *charBase, u16 *palette); + void renderTile256(int tile, int x, int y, u8 *charBase, u16 *palette); + void savePNG(const char *name); + void saveBMP(const char *name); + TileView(CWnd* pParent = NULL); // standard constructor + virtual ~TileView(); + + virtual void update(); + + // Dialog Data + //{{AFX_DATA(TileView) + enum { IDD = IDD_TILE_VIEWER }; + CSliderCtrl m_slider; + int m_colors; + int m_charBase; + BOOL m_stretch; + //}}AFX_DATA + + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(TileView) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + virtual void PostNcDestroy(); + //}}AFX_VIRTUAL + + // Implementation + protected: + virtual afx_msg LRESULT OnMapInfo(WPARAM wParam, LPARAM lParam); + virtual afx_msg LRESULT OnColInfo(WPARAM wParam, LPARAM lParam); + + // Generated message map functions + //{{AFX_MSG(TileView) + afx_msg void OnSave(); + virtual BOOL OnInitDialog(); + afx_msg void OnClose(); + afx_msg void OnAutoUpdate(); + afx_msg void On16Colors(); + afx_msg void On256Colors(); + afx_msg void OnCharbase0(); + afx_msg void OnCharbase1(); + afx_msg void OnCharbase2(); + afx_msg void OnCharbase3(); + afx_msg void OnCharbase4(); + afx_msg void OnStretch(); + afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + }; + + //{{AFX_INSERT_LOCATION}} + // Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_TILEVIEW_H__055751EC_2DF3_495B_B643_29025465CD2E__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/VBA.clw --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/VBA.clw Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1715 @@ +; CLW file contains information for the MFC ClassWizard + +[General Info] +Version=1 +LastClass=Hyperlink +LastTemplate=CDialog +NewFileInclude1=#include "stdafx.h" +NewFileInclude2=#include "vba.h" +LastPage=0 + +ClassCount=57 +Class1=AboutDialog +Class2=Associate +Class3=ColorButton +Class4=ColorControl +Class5=Directories +Class6=ExportGSASnapshot +Class7=FileDlg +Class8=GBACheatSearch +Class9=AddCheat +Class10=GBACheatList +Class11=GBCheatSearch +Class12=AddGBCheat +Class13=GBColorDlg +Class14=GSACodeSelect +Class15=JoypadEditControl +Class16=JoypadConfig +Class17=MotionConfig +Class18=Hyperlink +Class19=LangSelect +Class20=MainWnd +Class21=ModeConfirm +Class22=RewindInterval +Class23=RomInfoGB +Class24=RomInfoGBA +Class25=SkinButton +Class26=Throttle +Class27=VBA +Class28=VideoMode +Class29=VideoDriverSelect + +ResourceCount=44 +Resource1=IDD_MAX_SCALE +Resource2=IDD_GDB_WAITING +Resource3=IDD_GB_CHEAT_LIST +Resource4=IDD_CONFIG +Resource5=IDD_ADD_CHEAT_DLG +Resource6=IDD_GB_OAM_VIEW +Resource7=IDD_GB_ROM_INFO +Resource8=IDD_OPENDLG +Resource9=IDD_MAP_VIEW +Resource10=IDD_ABOUT +Resource11=IDD_CHEAT_LIST +Resource12=IDD_OAM_VIEW +Resource13=IDD_CODE_SELECT +Resource14=IDD_CHEATS +Resource15=IDD_GBA_ROM_INFO +Resource16=IDD_IO_VIEWER +Resource17=IDD_MEM_VIEWER +Resource18=IDD_ADDR_SIZE +Resource19=IDR_MENU +Resource20=IDD_PALETTE_VIEW +Resource21=IDD_ASSOCIATIONS +Resource22=IDD_GB_COLORS +Resource23=IDD_ADD_CHEAT +Resource24=IDD_DISASSEMBLE +Resource25=IDD_MODES +Resource26=IDD_GDB_PORT +Resource27=IDD_LANG_SELECT +Resource28=IDD_LOGGING +Resource29=IDD_DRIVERS +Resource30=IDD_EXPORT_SPS +Resource31=IDD_THROTTLE +Resource32=IDD_MODE_CONFIRM +Resource33=IDD_GB_DISASSEMBLE +Resource34=IDD_GB_PALETTE_VIEW +Resource35=IDD_REWIND_INTERVAL +Resource36=IDD_GB_TILE_VIEWER +Resource37=IDD_ACCEL_EDITOR +Resource38=IDD_GB_MAP_VIEW +Resource39=IDD_BUG_REPORT +Resource40=IDD_MOTION_CONFIG +Resource41=IDD_TILE_VIEWER +Class30=AddGSACode +Class31=GBCheatList +Class32=Disassemble +Class33=GBDisassemble +Class34=Logging +Class35=IOViewer +Class36=ZoomControl +Class37=BitmapControl +Class38=MapView +Class39=GBMapView +Class40=MemoryViewer +Class41=MemoryViewerDlg +Class42=MemoryViewerAddressSize +Class43=GBMemoryViewerDlg +Class44=OamView +Class45=GBOamView +Class46=PaletteViewControl +Class47=PaletteView +Class48=GBPaletteView +Class49=TileView +Class50=GBTileView +Class51=GBPrinterDlg +Class52=GDBPortDlg +Class53=GDBWaitingDlg +Class54=AccelEditor +Class55=CKeyboardEdit +Resource42=IDD_DIRECTORIES +Class56=MaxScale +Resource43=IDD_GB_PRINTER +Class57=BugReport +Resource44=IDR_ACCELERATOR + +[CLS:AboutDialog] +Type=0 +BaseClass=CDialog +HeaderFile=AboutDialog.h +ImplementationFile=AboutDialog.cpp +LastObject=AboutDialog + +[CLS:Associate] +Type=0 +BaseClass=CDialog +HeaderFile=Associate.h +ImplementationFile=Associate.cpp +LastObject=ID_OK + +[CLS:ColorButton] +Type=0 +BaseClass=CButton +HeaderFile=ColorButton.h +ImplementationFile=ColorButton.cpp + +[CLS:ColorControl] +Type=0 +BaseClass=CWnd +HeaderFile=ColorControl.h +ImplementationFile=ColorControl.cpp + +[CLS:Directories] +Type=0 +BaseClass=CDialog +HeaderFile=Directories.h +ImplementationFile=Directories.cpp + +[CLS:ExportGSASnapshot] +Type=0 +BaseClass=CDialog +HeaderFile=ExportGSASnapshot.h +ImplementationFile=ExportGSASnapshot.cpp + +[CLS:FileDlg] +Type=0 +BaseClass=CFileDialog +HeaderFile=FileDlg.h +ImplementationFile=FileDlg.cpp + +[CLS:GBACheatSearch] +Type=0 +BaseClass=CDialog +HeaderFile=GBACheats.h +ImplementationFile=GBACheats.cpp + +[CLS:AddCheat] +Type=0 +BaseClass=CDialog +HeaderFile=GBACheats.h +ImplementationFile=GBACheats.cpp + +[CLS:GBACheatList] +Type=0 +BaseClass=CDialog +HeaderFile=GBACheats.h +ImplementationFile=GBACheats.cpp +LastObject=GBACheatList + +[CLS:GBCheatSearch] +Type=0 +BaseClass=CDialog +HeaderFile=GBCheatsDlg.h +ImplementationFile=GBCheatsDlg.cpp + +[CLS:AddGBCheat] +Type=0 +BaseClass=CDialog +HeaderFile=GBCheatsDlg.h +ImplementationFile=GBCheatsDlg.cpp +LastObject=AddGBCheat + +[CLS:GBColorDlg] +Type=0 +BaseClass=CDialog +HeaderFile=GBColorDlg.h +ImplementationFile=GBColorDlg.cpp +LastObject=IDC_PREDEFINED +Filter=D +VirtualFilter=dWC + +[CLS:GSACodeSelect] +Type=0 +BaseClass=CDialog +HeaderFile=GSACodeSelect.h +ImplementationFile=GSACodeSelect.cpp + +[CLS:JoypadEditControl] +Type=0 +BaseClass=CEdit +HeaderFile=Joypad.h +ImplementationFile=Joypad.cpp + +[CLS:JoypadConfig] +Type=0 +BaseClass=CDialog +HeaderFile=Joypad.h +ImplementationFile=Joypad.cpp + +[CLS:CKeyboardEdit] +Type=0 +BaseClass=CEdit +HeaderFile=KeyboardEdit.h +ImplementationFile=KeyboardEdit.cpp + +[CLS:MotionConfig] +Type=0 +BaseClass=CDialog +HeaderFile=Joypad.h +ImplementationFile=Joypad.cpp + +[CLS:LangSelect] +Type=0 +BaseClass=CDialog +HeaderFile=LangSelect.h +ImplementationFile=LangSelect.cpp + +[CLS:MainWnd] +Type=0 +BaseClass=CWnd +HeaderFile=MainWnd.h +ImplementationFile=MainWnd.cpp +LastObject=ID_CHEATS_AUTOMATICSAVELOADCHEATS +Filter=W +VirtualFilter=WC + +[CLS:ModeConfirm] +Type=0 +BaseClass=CDialog +HeaderFile=ModeConfirm.h +ImplementationFile=ModeConfirm.cpp + +[CLS:RewindInterval] +Type=0 +BaseClass=CDialog +HeaderFile=RewindInterval.h +ImplementationFile=RewindInterval.cpp + +[CLS:RomInfoGB] +Type=0 +BaseClass=CDialog +HeaderFile=RomInfo.h +ImplementationFile=RomInfo.cpp + +[CLS:RomInfoGBA] +Type=0 +BaseClass=CDialog +HeaderFile=RomInfo.h +ImplementationFile=RomInfo.cpp + +[CLS:SkinButton] +Type=0 +BaseClass=CWnd +HeaderFile=skinButton.h +ImplementationFile=skinButton.cpp + +[CLS:Throttle] +Type=0 +BaseClass=CDialog +HeaderFile=Throttle.h +ImplementationFile=Throttle.cpp + +[CLS:VBA] +Type=0 +BaseClass=CWinApp +HeaderFile=VBA.h +ImplementationFile=VBA.cpp + +[CLS:VideoMode] +Type=0 +BaseClass=CDialog +HeaderFile=VideoMode.h +ImplementationFile=VideoMode.cpp + +[CLS:VideoDriverSelect] +Type=0 +BaseClass=CDialog +HeaderFile=VideoMode.h +ImplementationFile=VideoMode.cpp +Filter=D +VirtualFilter=dWC +LastObject=ID_CANCEL + +[DLG:IDD_ABOUT] +Type=1 +Class=AboutDialog +ControlCount=8 +Control1=IDOK,button,1342242817 +Control2=IDC_STATIC,static,1342177283 +Control3=IDC_STATIC,static,1342308353 +Control4=IDC_STATIC,static,1342308353 +Control5=IDC_URL,static,1342308353 +Control6=IDC_STATIC,static,1342308353 +Control7=IDC_STATIC,static,1342308353 +Control8=IDC_VERSION,static,1342308353 + +[DLG:IDD_ASSOCIATIONS] +Type=1 +Class=Associate +ControlCount=11 +Control1=IDC_GB,button,1342242819 +Control2=IDC_SGB,button,1342242819 +Control3=IDC_CGB,button,1342242819 +Control4=IDC_GBC,button,1342242819 +Control5=IDC_GBA,button,1342242819 +Control6=IDC_AGB,button,1342242819 +Control7=IDC_BIN,button,1342242819 +Control8=ID_OK,button,1342242817 +Control9=ID_CANCEL,button,1342242816 +Control10=IDC_STATIC,button,1342177287 +Control11=IDC_STATIC,button,1342177287 + +[DLG:IDD_DIRECTORIES] +Type=1 +Class=Directories +ControlCount=22 +Control1=IDC_ROM_DIR,button,1342242816 +Control2=IDC_GBROM_DIR,button,1342242816 +Control3=IDC_BATTERY_DIR,button,1342242816 +Control4=IDC_SAVE_DIR,button,1342242816 +Control5=IDC_CAPTURE_DIR,button,1342242816 +Control6=IDOK,button,1342242817 +Control7=IDCANCEL,button,1342242816 +Control8=IDC_STATIC,static,1342308352 +Control9=IDC_STATIC,static,1342308352 +Control10=IDC_STATIC,static,1342308352 +Control11=IDC_STATIC,static,1342308352 +Control12=IDC_ROM_PATH,edit,1484849280 +Control13=IDC_BATTERY_PATH,edit,1484849280 +Control14=IDC_SAVE_PATH,edit,1484849280 +Control15=IDC_CAPTURE_PATH,edit,1484849280 +Control16=IDC_STATIC,static,1342308352 +Control17=IDC_GBROM_PATH,edit,1484849280 +Control18=IDC_ROM_DIR_RESET,button,1342242816 +Control19=IDC_GBROM_DIR_RESET,button,1342242816 +Control20=IDC_BATTERY_DIR_RESET,button,1342242816 +Control21=IDC_SAVE_DIR_RESET,button,1342242816 +Control22=IDC_CAPTURE_DIR_RESET,button,1342242816 + +[DLG:IDD_EXPORT_SPS] +Type=1 +Class=ExportGSASnapshot +ControlCount=8 +Control1=IDC_TITLE,edit,1350631552 +Control2=IDC_DESC,edit,1350631552 +Control3=IDC_NOTES,edit,1350635652 +Control4=ID_OK,button,1342242817 +Control5=ID_CANCEL,button,1342242816 +Control6=IDC_STATIC,static,1342308352 +Control7=IDC_STATIC,static,1342308352 +Control8=IDC_STATIC,static,1342308352 + +[DLG:IDD_CHEATS] +Type=1 +Class=GBACheatSearch +ControlCount=26 +Control1=IDC_CHEAT_LIST,SysListView32,1350631429 +Control2=IDC_OLD_VALUE,button,1342308361 +Control3=IDC_SPECIFIC_VALUE,button,1342177289 +Control4=IDC_SIZE_8,button,1342308361 +Control5=IDC_SIZE_16,button,1342177289 +Control6=IDC_SIZE_32,button,1342177289 +Control7=IDC_EQ,button,1342308361 +Control8=IDC_NE,button,1342177289 +Control9=IDC_LT,button,1342177289 +Control10=IDC_LE,button,1342177289 +Control11=IDC_GT,button,1342177289 +Control12=IDC_GE,button,1342177289 +Control13=IDC_SIGNED,button,1342308361 +Control14=IDC_UNSIGNED,button,1342177289 +Control15=IDC_HEXADECIMAL,button,1342177289 +Control16=IDC_UPDATE,button,1342373891 +Control17=IDC_VALUE,edit,1350631552 +Control18=IDC_START,button,1342373888 +Control19=IDC_SEARCH,button,1342242816 +Control20=IDC_ADD_CHEAT,button,1342242816 +Control21=ID_OK,button,1342242817 +Control22=IDC_STATIC,button,1342177287 +Control23=IDC_STATIC,button,1342177287 +Control24=IDC_STATIC,button,1342177287 +Control25=IDC_STATIC,button,1342177287 +Control26=IDC_STATIC,static,1342308352 + +[DLG:IDD_ADD_CHEAT] +Type=1 +Class=AddCheat +ControlCount=16 +Control1=IDC_ADDRESS,edit,1350631552 +Control2=IDC_VALUE,edit,1350631552 +Control3=IDC_DESC,edit,1350631552 +Control4=IDC_SIZE_8,button,1342373897 +Control5=IDC_SIZE_16,button,1342177289 +Control6=IDC_SIZE_32,button,1342177289 +Control7=IDC_SIGNED,button,1342373897 +Control8=IDC_UNSIGNED,button,1342177289 +Control9=IDC_HEXADECIMAL,button,1342177289 +Control10=ID_OK,button,1342373889 +Control11=ID_CANCEL,button,1342242816 +Control12=IDC_STATIC,static,1342308352 +Control13=IDC_STATIC,button,1342177287 +Control14=IDC_STATIC,static,1342308352 +Control15=IDC_STATIC,button,1342177287 +Control16=IDC_STATIC,static,1342308352 + +[DLG:IDD_CHEAT_LIST] +Type=1 +Class=GBACheatList +ControlCount=14 +Control1=IDC_RESTORE,button,1342373891 +Control2=IDC_ADD_CODE,button,1342373888 +Control3=IDC_ADD_CHEAT,button,1342242816 +Control4=IDC_ADD_GAMESHARK,button,1342242816 +Control5=IDC_ADD_CODEBREAKER,button,1342242816 +Control6=IDC_REMOVE,button,1342242816 +Control7=IDC_REMOVE_ALL,button,1342242816 +Control8=IDC_ENABLE,button,1342242816 +Control9=ID_OK,button,1342373889 +Control10=IDC_CHEAT_LIST,SysListView32,1350762497 +Control11=IDC_STATIC,static,1342308352 +Control12=IDC_STATIC,static,1342308352 +Control13=IDC_STATIC,static,1342308352 +Control14=IDC_STATIC,button,1342177287 + +[DLG:IDD_GB_COLORS] +Type=1 +Class=GBColorDlg +ControlCount=17 +Control1=IDC_DEFAULT,button,1342308361 +Control2=IDC_USER1,button,1342177289 +Control3=IDC_USER2,button,1342177289 +Control4=IDC_PREDEFINED,combobox,1344340227 +Control5=IDC_COLOR_BG0,button,1342373888 +Control6=IDC_COLOR_BG1,button,1342242816 +Control7=IDC_COLOR_BG2,button,1342242816 +Control8=IDC_COLOR_BG3,button,1342242816 +Control9=IDC_COLOR_OB0,button,1342242816 +Control10=IDC_COLOR_OB1,button,1342242816 +Control11=IDC_COLOR_OB2,button,1342242816 +Control12=IDC_COLOR_OB3,button,1342242816 +Control13=IDC_RESET,button,1342242816 +Control14=ID_OK,button,1342242817 +Control15=ID_CANCEL,button,1342242816 +Control16=IDC_STATIC,button,1342177287 +Control17=IDC_STATIC,button,1342177287 + +[DLG:IDD_CODE_SELECT] +Type=1 +Class=GSACodeSelect +ControlCount=3 +Control1=ID_OK,button,1342242817 +Control2=ID_CANCEL,button,1342242816 +Control3=IDC_GAME_LIST,listbox,1353777411 + +[DLG:IDD_CONFIG] +Type=1 +Class=JoypadConfig +ControlCount=28 +Control1=IDC_EDIT_UP,edit,1350631552 +Control2=IDC_EDIT_DOWN,edit,1350631552 +Control3=IDC_EDIT_LEFT,edit,1350631552 +Control4=IDC_EDIT_RIGHT,edit,1350631552 +Control5=IDC_EDIT_BUTTON_A,edit,1350631552 +Control6=IDC_EDIT_BUTTON_B,edit,1350631552 +Control7=IDC_EDIT_BUTTON_L,edit,1350631552 +Control8=IDC_EDIT_BUTTON_R,edit,1350631552 +Control9=IDC_EDIT_BUTTON_SELECT,edit,1350631552 +Control10=IDC_EDIT_BUTTON_START,edit,1350631552 +Control11=IDC_EDIT_SPEED,edit,1350631552 +Control12=IDC_EDIT_CAPTURE,edit,1350631552 +Control13=IDC_EDIT_BUTTON_GS,edit,1350631552 +Control14=ID_OK,button,1342242816 +Control15=ID_CANCEL,button,1342242816 +Control16=IDC_STATIC,static,1342308352 +Control17=IDC_STATIC,static,1342308352 +Control18=IDC_STATIC,static,1342308352 +Control19=IDC_STATIC,static,1342308352 +Control20=IDC_STATIC,static,1342308352 +Control21=IDC_STATIC,static,1342308352 +Control22=IDC_STATIC,static,1342308352 +Control23=IDC_STATIC,static,1342308352 +Control24=IDC_STATIC,static,1342308352 +Control25=IDC_STATIC,static,1342308352 +Control26=IDC_STATIC,static,1342308352 +Control27=IDC_STATIC,static,1342308352 +Control28=IDC_STATIC,static,1342308352 + +[DLG:IDD_MOTION_CONFIG] +Type=1 +Class=MotionConfig +ControlCount=10 +Control1=IDC_EDIT_UP,edit,1350631552 +Control2=IDC_EDIT_DOWN,edit,1350631552 +Control3=IDC_EDIT_LEFT,edit,1350631552 +Control4=IDC_EDIT_RIGHT,edit,1350631552 +Control5=ID_OK,button,1342242816 +Control6=ID_CANCEL,button,1342242816 +Control7=IDC_STATIC,static,1342308352 +Control8=IDC_STATIC,static,1342308352 +Control9=IDC_STATIC,static,1342308352 +Control10=IDC_STATIC,static,1342308352 + +[DLG:IDD_LANG_SELECT] +Type=1 +Class=LangSelect +ControlCount=6 +Control1=IDC_LANG_STRING,edit,1350631552 +Control2=ID_OK,button,1342242817 +Control3=ID_CANCEL,button,1342242816 +Control4=IDC_STATIC,static,1342308352 +Control5=IDC_STATIC,static,1342308352 +Control6=IDC_LANG_NAME,static,1342308352 + +[DLG:IDD_MODE_CONFIRM] +Type=1 +Class=ModeConfirm +ControlCount=4 +Control1=ID_OK,button,1342242817 +Control2=ID_CANCEL,button,1342242816 +Control3=IDC_STATIC,static,1342308353 +Control4=IDC_TIMER,static,1342308353 + +[DLG:IDD_REWIND_INTERVAL] +Type=1 +Class=RewindInterval +ControlCount=5 +Control1=IDC_INTERVAL,edit,1350631552 +Control2=ID_OK,button,1342242817 +Control3=ID_CANCEL,button,1342242816 +Control4=IDC_STATIC,static,1342308352 +Control5=IDC_STATIC,static,1342308352 + +[DLG:IDD_GB_ROM_INFO] +Type=1 +Class=RomInfoGB +ControlCount=27 +Control1=ID_OK,button,1342242817 +Control2=IDC_STATIC,static,1342308352 +Control3=IDC_STATIC,static,1342308352 +Control4=IDC_STATIC,static,1342308352 +Control5=IDC_STATIC,static,1342308352 +Control6=IDC_STATIC,static,1342308352 +Control7=IDC_STATIC,static,1342308352 +Control8=IDC_ROM_TITLE,static,1342308352 +Control9=IDC_ROM_MAKER_CODE,static,1342308352 +Control10=IDC_ROM_UNIT_CODE,static,1342308352 +Control11=IDC_ROM_DEVICE_TYPE,static,1342308352 +Control12=IDC_ROM_VERSION,static,1342308352 +Control13=IDC_ROM_CRC,static,1342308352 +Control14=IDC_STATIC,static,1342308352 +Control15=IDC_ROM_COLOR,static,1342308352 +Control16=IDC_STATIC,static,1342308352 +Control17=IDC_ROM_SIZE,static,1342308352 +Control18=IDC_STATIC,static,1342308352 +Control19=IDC_ROM_RAM_SIZE,static,1342308352 +Control20=IDC_STATIC,static,1342308352 +Control21=IDC_ROM_DEST_CODE,static,1342308352 +Control22=IDC_STATIC,static,1342308352 +Control23=IDC_ROM_LIC_CODE,static,1342308352 +Control24=IDC_STATIC,static,1342308352 +Control25=IDC_ROM_CHECKSUM,static,1342308352 +Control26=IDC_ROM_MAKER_NAME2,static,1342308352 +Control27=IDC_STATIC,static,1342308352 + +[DLG:IDD_GBA_ROM_INFO] +Type=1 +Class=RomInfoGBA +ControlCount=17 +Control1=ID_OK,button,1342242817 +Control2=IDC_STATIC,static,1342308352 +Control3=IDC_STATIC,static,1342308352 +Control4=IDC_STATIC,static,1342308352 +Control5=IDC_STATIC,static,1342308352 +Control6=IDC_STATIC,static,1342308352 +Control7=IDC_STATIC,static,1342308352 +Control8=IDC_STATIC,static,1342308352 +Control9=IDC_ROM_TITLE,static,1342308352 +Control10=IDC_ROM_GAME_CODE,static,1342308352 +Control11=IDC_ROM_MAKER_CODE,static,1342308352 +Control12=IDC_ROM_UNIT_CODE,static,1342308352 +Control13=IDC_ROM_DEVICE_TYPE,static,1342308352 +Control14=IDC_ROM_VERSION,static,1342308352 +Control15=IDC_ROM_CRC,static,1342308352 +Control16=IDC_ROM_MAKER_NAME,static,1342308352 +Control17=IDC_STATIC,static,1342308352 + +[DLG:IDD_THROTTLE] +Type=1 +Class=Throttle +ControlCount=4 +Control1=IDC_THROTTLE,edit,1350631552 +Control2=ID_OK,button,1342242817 +Control3=ID_CANCEL,button,1342242816 +Control4=IDC_STATIC,static,1342308352 + +[DLG:IDD_MODES] +Type=1 +Class=VideoMode +ControlCount=4 +Control1=IDC_MODES,listbox,1352728835 +Control2=ID_OK,button,1342242817 +Control3=ID_CANCEL,button,1342242816 +Control4=IDC_STATIC,static,1342308352 + +[DLG:IDD_DRIVERS] +Type=1 +Class=VideoDriverSelect +ControlCount=4 +Control1=IDC_DRIVERS,listbox,1352728833 +Control2=ID_OK,button,1342242817 +Control3=ID_CANCEL,button,1342242816 +Control4=IDC_STATIC,static,1342308352 + +[DLG:IDD_OPENDLG] +Type=1 +Class=GBMemoryViewerDlg +ControlCount=12 +Control1=1090,static,1342308352 +Control2=1152,edit,1350632576 +Control3=1120,listbox,1352732755 +Control4=65535,static,1342308352 +Control5=1088,static,1342308480 +Control6=1121,listbox,1352732755 +Control7=1089,static,1342308352 +Control8=1136,combobox,1344340035 +Control9=1091,static,1342308352 +Control10=1137,combobox,1344340819 +Control11=IDOK,button,1342373889 +Control12=IDCANCEL,button,1342373888 + +[DLG:IDD_GB_CHEAT_LIST] +Type=1 +Class=GBCheatList +ControlCount=10 +Control1=IDC_CHEAT_LIST,SysListView32,1350762497 +Control2=IDC_ADD_GG_CHEAT,button,1342373888 +Control3=IDC_ADD_GS_CHEAT,button,1342373888 +Control4=IDC_REMOVE,button,1342242816 +Control5=IDC_REMOVE_ALL,button,1342242816 +Control6=IDC_ENABLE,button,1342242816 +Control7=ID_OK,button,1342242817 +Control8=IDC_STATIC,static,1342308352 +Control9=IDC_STATIC,static,1342308352 +Control10=IDC_STATIC,static,1342308352 + +[DLG:IDD_ADD_CHEAT_DLG] +Type=1 +Class=AddGSACode +ControlCount=6 +Control1=IDC_DESC,edit,1350631552 +Control2=IDC_CODE,edit,1350635724 +Control3=ID_OK,button,1342242817 +Control4=ID_CANCEL,button,1342242816 +Control5=IDC_STATIC,static,1342308352 +Control6=IDC_STATIC,static,1342308352 + +[DLG:IDD_GB_PRINTER] +Type=1 +Class=GBPrinterDlg +ControlCount=9 +Control1=IDC_1X,button,1342308361 +Control2=IDC_2X,button,1342177289 +Control3=IDC_3X,button,1342177289 +Control4=IDC_4X,button,1342177289 +Control5=ID_PRINT,button,1342373889 +Control6=ID_SAVE,button,1342242816 +Control7=ID_OK,button,1342242816 +Control8=IDC_GB_PRINTER,static,1342308359 +Control9=IDC_STATIC,button,1342177287 + +[DLG:IDD_MAP_VIEW] +Type=1 +Class=MapView +ControlCount=44 +Control1=IDC_FRAME_0,button,1342308361 +Control2=IDC_FRAME_1,button,1342177289 +Control3=IDC_BG0,button,1342308361 +Control4=IDC_BG1,button,1342177289 +Control5=IDC_BG2,button,1342177289 +Control6=IDC_BG3,button,1342177289 +Control7=IDC_STRETCH,button,1342242819 +Control8=IDC_REFRESH,button,1342373888 +Control9=IDC_SAVE,button,1342373888 +Control10=IDC_CLOSE,button,1342242816 +Control11=IDC_MAP_VIEW,VbaBitmapControl,1342373888 +Control12=IDC_MAP_VIEW_ZOOM,VbaZoomControl,1342373888 +Control13=IDC_COLOR,VbaColorControl,1342242816 +Control14=IDC_R,static,1342308352 +Control15=IDC_G,static,1342308352 +Control16=IDC_B,static,1342308352 +Control17=IDC_STATIC,button,1342177287 +Control18=IDC_STATIC,button,1342177287 +Control19=IDC_AUTO_UPDATE,button,1342242819 +Control20=IDC_XY,static,1342308352 +Control21=IDC_STATIC,static,1342308352 +Control22=IDC_MODE,static,1342308352 +Control23=IDC_STATIC,static,1342308352 +Control24=IDC_MAPBASE,static,1342308352 +Control25=IDC_STATIC,static,1342308352 +Control26=IDC_CHARBASE,static,1342308352 +Control27=IDC_STATIC,static,1342308352 +Control28=IDC_DIM,static,1342308352 +Control29=IDC_STATIC,static,1342308352 +Control30=IDC_NUMCOLORS,static,1342308352 +Control31=IDC_STATIC,static,1342308352 +Control32=IDC_PRIORITY,static,1342308352 +Control33=IDC_STATIC,static,1342308352 +Control34=IDC_MOSAIC,static,1342308352 +Control35=IDC_STATIC,static,1342308352 +Control36=IDC_OVERFLOW,static,1342308352 +Control37=IDC_STATIC,static,1342308352 +Control38=IDC_ADDRESS,static,1342308352 +Control39=IDC_STATIC,static,1342308352 +Control40=IDC_TILE_NUM,static,1342308352 +Control41=IDC_STATIC,static,1342308352 +Control42=IDC_FLIP,static,1342308352 +Control43=IDC_STATIC,static,1342308352 +Control44=IDC_PALETTE_NUM,static,1342308352 + +[DLG:IDD_PALETTE_VIEW] +Type=1 +Class=PaletteView +ControlCount=21 +Control1=IDC_SAVE_BG,button,1342242816 +Control2=IDC_SAVE_OBJ,button,1342242816 +Control3=IDC_REFRESH2,button,1342242816 +Control4=IDC_CLOSE,button,1342242816 +Control5=IDC_ADDRESS,static,1342308352 +Control6=IDC_R,static,1342308352 +Control7=IDC_G,static,1342308352 +Control8=IDC_B,static,1342308352 +Control9=IDC_VALUE,static,1342308352 +Control10=IDC_COLOR,VbaColorControl,1342242816 +Control11=IDC_PALETTE_VIEW,VbaPaletteViewControl,1342242816 +Control12=IDC_PALETTE_VIEW_OBJ,VbaPaletteViewControl,1342242816 +Control13=IDC_STATIC,button,1342177287 +Control14=IDC_STATIC,button,1342177287 +Control15=IDC_STATIC,static,1342308352 +Control16=IDC_STATIC,static,1342308352 +Control17=IDC_STATIC,static,1342308352 +Control18=IDC_STATIC,static,1342308352 +Control19=IDC_STATIC,static,1342308352 +Control20=IDC_STATIC,static,1342308352 +Control21=IDC_AUTO_UPDATE,button,1342242819 + +[DLG:IDD_MEM_VIEWER] +Type=1 +Class=MemoryViewerDlg +ControlCount=14 +Control1=IDC_ADDRESSES,combobox,1344339971 +Control2=IDC_8_BIT,button,1342308361 +Control3=IDC_16_BIT,button,1342177289 +Control4=IDC_32_BIT,button,1342177289 +Control5=IDC_ADDRESS,edit,1350766728 +Control6=IDC_GO,button,1342373889 +Control7=IDC_VIEWER,VbaMemoryViewer,1342242816 +Control8=IDC_AUTO_UPDATE,button,1342242819 +Control9=IDC_REFRESH,button,1342242816 +Control10=IDC_LOAD,button,1342242816 +Control11=IDC_SAVE,button,1342242816 +Control12=IDC_CLOSE,button,1342242816 +Control13=IDC_CURRENT_ADDRESS_LABEL,static,1342308352 +Control14=IDC_CURRENT_ADDRESS,edit,1484849282 + +[DLG:IDD_OAM_VIEW] +Type=1 +Class=OamView +ControlCount=32 +Control1=IDC_SPRITE,edit,1350639746 +Control2=IDC_SCROLLBAR,scrollbar,1342177280 +Control3=IDC_STRETCH,button,1342242819 +Control4=IDC_REFRESH,button,1342373888 +Control5=IDC_SAVE,button,1342373888 +Control6=IDC_CLOSE,button,1342242816 +Control7=IDC_OAM_VIEW,VbaBitmapControl,1342373888 +Control8=IDC_OAM_VIEW_ZOOM,VbaZoomControl,1342373888 +Control9=IDC_COLOR,VbaColorControl,1342242816 +Control10=IDC_POS,static,1342308352 +Control11=IDC_MODE,static,1342308352 +Control12=IDC_COLORS,static,1342308352 +Control13=IDC_PALETTE,static,1342308352 +Control14=IDC_TILE,static,1342308352 +Control15=IDC_PRIO,static,1342308352 +Control16=IDC_SIZE2,static,1342308352 +Control17=IDC_ROT,static,1342308352 +Control18=IDC_FLAGS,static,1342308352 +Control19=IDC_R,static,1342308352 +Control20=IDC_G,static,1342308352 +Control21=IDC_B,static,1342308352 +Control22=IDC_STATIC,static,1342308352 +Control23=IDC_STATIC,static,1342308352 +Control24=IDC_STATIC,static,1342308352 +Control25=IDC_STATIC,static,1342308352 +Control26=IDC_STATIC,static,1342308352 +Control27=IDC_STATIC,static,1342308352 +Control28=IDC_STATIC,static,1342308352 +Control29=IDC_STATIC,static,1342308352 +Control30=IDC_STATIC,static,1342308352 +Control31=IDC_STATIC,static,1342308352 +Control32=IDC_AUTO_UPDATE,button,1342242819 + +[DLG:IDD_ACCEL_EDITOR] +Type=1 +Class=AccelEditor +ControlCount=13 +Control1=IDC_STATIC,static,1342308352 +Control2=IDC_COMMANDS,listbox,1352732931 +Control3=IDC_STATIC1,static,1342308352 +Control4=IDC_CURRENTS,listbox,1352728835 +Control5=ID_OK,button,1342242817 +Control6=ID_CANCEL,button,1342242816 +Control7=IDC_STATIC3,static,1342308352 +Control8=IDC_EDIT_KEY,edit,1350631552 +Control9=IDC_ASSIGN,button,1342242816 +Control10=IDC_REMOVE,button,1342242816 +Control11=IDC_RESET,button,1342242816 +Control12=IDC_ALREADY_AFFECTED,static,1342308864 +Control13=IDC_STATIC2,static,1342308352 + +[DLG:IDD_TILE_VIEWER] +Type=1 +Class=TileView +ControlCount=26 +Control1=IDC_16_COLORS,button,1342308361 +Control2=IDC_256_COLORS,button,1342177289 +Control3=IDC_CHARBASE_0,button,1342308361 +Control4=IDC_CHARBASE_1,button,1342177289 +Control5=IDC_CHARBASE_2,button,1342177289 +Control6=IDC_CHARBASE_3,button,1342177289 +Control7=IDC_CHARBASE_4,button,1342177289 +Control8=IDC_PALETTE_SLIDER,msctls_trackbar32,1342373889 +Control9=IDC_STRETCH,button,1342242819 +Control10=IDC_REFRESH,button,1342373888 +Control11=IDC_SAVE,button,1342242816 +Control12=IDC_CLOSE,button,1342242816 +Control13=IDC_TILE_VIEW,VbaBitmapControl,1342373888 +Control14=IDC_STATIC,button,1342177287 +Control15=IDC_STATIC,button,1342177287 +Control16=IDC_MAP_VIEW_ZOOM,VbaZoomControl,1342373888 +Control17=IDC_COLOR,VbaColorControl,1342242816 +Control18=IDC_R,static,1342308352 +Control19=IDC_G,static,1342308352 +Control20=IDC_B,static,1342308352 +Control21=IDC_STATIC,static,1342308352 +Control22=IDC_AUTO_UPDATE,button,1342242819 +Control23=IDC_STATIC,static,1342308352 +Control24=IDC_STATIC,static,1342308352 +Control25=IDC_TILE_NUMBER,static,1342308352 +Control26=IDC_ADDRESS,static,1342308352 + +[DLG:IDD_DISASSEMBLE] +Type=1 +Class=Disassemble +ControlCount=55 +Control1=IDC_AUTOMATIC,button,1342308361 +Control2=IDC_ARM,button,1342177289 +Control3=IDC_THUMB,button,1342177289 +Control4=IDC_ADDRESS,edit,1350762632 +Control5=IDC_GO,button,1342242816 +Control6=IDC_DISASSEMBLE,listbox,1350631683 +Control7=IDC_AUTO_UPDATE,button,1342242819 +Control8=IDC_REFRESH,button,1342242816 +Control9=IDC_NEXT,button,1342242816 +Control10=IDC_CLOSE,button,1342242817 +Control11=IDC_STATIC,static,1342308352 +Control12=IDC_STATIC,static,1342308352 +Control13=IDC_STATIC,static,1342308352 +Control14=IDC_STATIC,static,1342308352 +Control15=IDC_STATIC,static,1342308352 +Control16=IDC_STATIC,static,1342308352 +Control17=IDC_STATIC,static,1342308352 +Control18=IDC_STATIC,static,1342308352 +Control19=IDC_R0,static,1342308352 +Control20=IDC_R1,static,1342308352 +Control21=IDC_R2,static,1342308352 +Control22=IDC_R3,static,1342308352 +Control23=IDC_R4,static,1342308352 +Control24=IDC_R5,static,1342308352 +Control25=IDC_R6,static,1342308352 +Control26=IDC_R7,static,1342308352 +Control27=IDC_R8,static,1342308352 +Control28=IDC_R9,static,1342308352 +Control29=IDC_R10,static,1342308352 +Control30=IDC_R11,static,1342308352 +Control31=IDC_R12,static,1342308352 +Control32=IDC_R13,static,1342308352 +Control33=IDC_R14,static,1342308352 +Control34=IDC_R15,static,1342308352 +Control35=IDC_STATIC,static,1342308352 +Control36=IDC_STATIC,static,1342308352 +Control37=IDC_STATIC,static,1342308352 +Control38=IDC_STATIC,static,1342308352 +Control39=IDC_STATIC,static,1342308352 +Control40=IDC_STATIC,static,1342308352 +Control41=IDC_STATIC,static,1342308352 +Control42=IDC_STATIC,static,1342308352 +Control43=IDC_R16,static,1342308352 +Control44=IDC_STATIC,static,1342308352 +Control45=IDC_N,button,1476460547 +Control46=IDC_Z,button,1476460547 +Control47=IDC_C,button,1476460547 +Control48=IDC_V,button,1476460547 +Control49=IDC_F,button,1476460547 +Control50=IDC_I,button,1476460547 +Control51=IDC_T,button,1476460547 +Control52=IDC_STATIC,static,1342308352 +Control53=IDC_MODE,static,1342308352 +Control54=IDC_VSCROLL,scrollbar,1342177281 +Control55=IDC_GOPC,button,1342242816 + +[DLG:IDD_GDB_PORT] +Type=1 +Class=GDBPortDlg +ControlCount=4 +Control1=ID_OK,button,1342242817 +Control2=ID_CANCEL,button,1342242816 +Control3=IDC_STATIC,static,1342308352 +Control4=IDC_PORT,edit,1350631554 + +[DLG:IDD_GDB_WAITING] +Type=1 +Class=GDBWaitingDlg +ControlCount=3 +Control1=ID_CANCEL,button,1342242816 +Control2=IDC_STATIC,static,1342308352 +Control3=IDC_PORT,static,1342308352 + +[DLG:IDD_LOGGING] +Type=1 +Class=Logging +ControlCount=15 +Control1=IDC_VERBOSE_SWI,button,1342242819 +Control2=IDC_VERBOSE_UNALIGNED_ACCESS,button,1342242819 +Control3=IDC_VERBOSE_ILLEGAL_WRITE,button,1342242819 +Control4=IDC_VERBOSE_ILLEGAL_READ,button,1342242819 +Control5=IDC_VERBOSE_DMA0,button,1342242819 +Control6=IDC_VERBOSE_DMA1,button,1342242819 +Control7=IDC_VERBOSE_DMA2,button,1342242819 +Control8=IDC_VERBOSE_DMA3,button,1342242819 +Control9=IDC_VERBOSE_UNDEFINED,button,1342242819 +Control10=IDC_VERBOSE_AGBPRINT,button,1342242819 +Control11=IDC_LOG,edit,1353779396 +Control12=IDC_SAVE,button,1342242816 +Control13=IDC_CLEAR,button,1342242816 +Control14=ID_OK,button,1342242817 +Control15=IDC_STATIC,button,1342177287 + +[DLG:IDD_ADDR_SIZE] +Type=1 +Class=MemoryViewerAddressSize +ControlCount=6 +Control1=IDC_ADDRESS,edit,1350631552 +Control2=IDC_SIZE_CONTROL,edit,1350631552 +Control3=ID_OK,button,1342242817 +Control4=ID_CANCEL,button,1342242816 +Control5=IDC_STATIC,static,1342308352 +Control6=IDC_STATIC,static,1342308352 + +[DLG:IDD_GB_DISASSEMBLE] +Type=1 +Class=GBDisassemble +ControlCount=27 +Control1=IDC_ADDRESS,edit,1350762632 +Control2=IDC_GO,button,1342242816 +Control3=IDC_DISASSEMBLE,listbox,1350631683 +Control4=IDC_AUTO_UPDATE,button,1342242819 +Control5=IDC_REFRESH,button,1342242816 +Control6=IDC_NEXT,button,1342242816 +Control7=IDC_CLOSE,button,1342242817 +Control8=IDC_STATIC,static,1342308352 +Control9=IDC_STATIC,static,1342308352 +Control10=IDC_STATIC,static,1342308352 +Control11=IDC_STATIC,static,1342308352 +Control12=IDC_STATIC,static,1342308352 +Control13=IDC_R0,static,1342308352 +Control14=IDC_R1,static,1342308352 +Control15=IDC_R2,static,1342308352 +Control16=IDC_R3,static,1342308352 +Control17=IDC_R6,static,1342308352 +Control18=IDC_N,button,1476460547 +Control19=IDC_Z,button,1476460547 +Control20=IDC_C,button,1476460547 +Control21=IDC_H,button,1476460547 +Control22=IDC_VSCROLL,scrollbar,1342177281 +Control23=IDC_GOPC,button,1342242816 +Control24=IDC_STATIC,static,1342308352 +Control25=IDC_R4,static,1342308352 +Control26=IDC_STATIC,static,1342308352 +Control27=IDC_R5,static,1342308352 + +[DLG:IDD_GB_OAM_VIEW] +Type=1 +Class=GBOamView +ControlCount=28 +Control1=IDC_SPRITE,edit,1350639746 +Control2=IDC_SCROLLBAR,scrollbar,1342177280 +Control3=IDC_STRETCH,button,1342242819 +Control4=IDC_REFRESH,button,1342373888 +Control5=IDC_SAVE,button,1342373888 +Control6=IDC_CLOSE,button,1342242816 +Control7=IDC_OAM_VIEW,VbaBitmapControl,1342373888 +Control8=IDC_OAM_VIEW_ZOOM,VbaZoomControl,1342373888 +Control9=IDC_COLOR,VbaColorControl,1342242816 +Control10=IDC_POS,static,1342308352 +Control11=IDC_PALETTE,static,1342308352 +Control12=IDC_TILE,static,1342308352 +Control13=IDC_PRIO,static,1342308352 +Control14=IDC_OAP,static,1342308352 +Control15=IDC_FLAGS,static,1342308352 +Control16=IDC_R,static,1342308352 +Control17=IDC_G,static,1342308352 +Control18=IDC_B,static,1342308352 +Control19=IDC_STATIC,static,1342308352 +Control20=IDC_STATIC,static,1342308352 +Control21=IDC_STATIC,static,1342308352 +Control22=IDC_STATIC,static,1342308352 +Control23=IDC_STATIC,static,1342308352 +Control24=IDC_STATIC,static,1342308352 +Control25=IDC_STATIC,static,1342308352 +Control26=IDC_AUTO_UPDATE,button,1342242819 +Control27=IDC_BANK,static,1342308352 +Control28=IDC_STATIC,static,1342308352 + +[DLG:IDD_GB_TILE_VIEWER] +Type=1 +Class=GBTileView +ControlCount=23 +Control1=IDC_BANK_0,button,1342308361 +Control2=IDC_BANK_1,button,1342177289 +Control3=IDC_CHARBASE_0,button,1342308361 +Control4=IDC_CHARBASE_1,button,1342177289 +Control5=IDC_STRETCH,button,1342242819 +Control6=IDC_REFRESH,button,1342373888 +Control7=IDC_SAVE,button,1342242816 +Control8=IDC_CLOSE,button,1342242816 +Control9=IDC_TILE_VIEW,VbaBitmapControl,1342373888 +Control10=IDC_STATIC,button,1342177287 +Control11=IDC_STATIC,button,1342177287 +Control12=IDC_MAP_VIEW_ZOOM,VbaZoomControl,1342373888 +Control13=IDC_COLOR,VbaColorControl,1342242816 +Control14=IDC_R,static,1342308352 +Control15=IDC_G,static,1342308352 +Control16=IDC_B,static,1342308352 +Control17=IDC_STATIC,static,1342308352 +Control18=IDC_AUTO_UPDATE,button,1342242819 +Control19=IDC_STATIC,static,1342308352 +Control20=IDC_STATIC,static,1342308352 +Control21=IDC_TILE_NUMBER,static,1342308352 +Control22=IDC_ADDRESS,static,1342308352 +Control23=IDC_PALETTE_SLIDER,msctls_trackbar32,1342373889 + +[DLG:IDD_GB_MAP_VIEW] +Type=1 +Class=GBMapView +ControlCount=28 +Control1=IDC_BANK_0,button,1342308361 +Control2=IDC_BANK_1,button,1342177289 +Control3=IDC_BG0,button,1342308361 +Control4=IDC_BG1,button,1342177289 +Control5=IDC_STRETCH,button,1342242819 +Control6=IDC_REFRESH,button,1342373888 +Control7=IDC_SAVE,button,1342373888 +Control8=IDC_CLOSE,button,1342242816 +Control9=IDC_MAP_VIEW,VbaBitmapControl,1342373888 +Control10=IDC_MAP_VIEW_ZOOM,VbaZoomControl,1342373888 +Control11=IDC_COLOR,VbaColorControl,1342242816 +Control12=IDC_R,static,1342308352 +Control13=IDC_G,static,1342308352 +Control14=IDC_B,static,1342308352 +Control15=IDC_STATIC,button,1342177287 +Control16=IDC_STATIC,button,1342177287 +Control17=IDC_AUTO_UPDATE,button,1342242819 +Control18=IDC_XY,static,1342308352 +Control19=IDC_STATIC,static,1342308352 +Control20=IDC_PRIORITY,static,1342308352 +Control21=IDC_STATIC,static,1342308352 +Control22=IDC_ADDRESS,static,1342308352 +Control23=IDC_STATIC,static,1342308352 +Control24=IDC_TILE_NUM,static,1342308352 +Control25=IDC_STATIC,static,1342308352 +Control26=IDC_FLIP,static,1342308352 +Control27=IDC_STATIC,static,1342308352 +Control28=IDC_PALETTE_NUM,static,1342308352 + +[DLG:IDD_GB_PALETTE_VIEW] +Type=1 +Class=GBPaletteView +ControlCount=21 +Control1=IDC_SAVE_BG,button,1342242816 +Control2=IDC_SAVE_OBJ,button,1342242816 +Control3=IDC_REFRESH2,button,1342242816 +Control4=IDC_CLOSE,button,1342242816 +Control5=IDC_ADDRESS,static,1342308352 +Control6=IDC_R,static,1342308352 +Control7=IDC_G,static,1342308352 +Control8=IDC_B,static,1342308352 +Control9=IDC_VALUE,static,1342308352 +Control10=IDC_COLOR,VbaColorControl,1342242816 +Control11=IDC_PALETTE_VIEW,VbaPaletteViewControl,1342242816 +Control12=IDC_PALETTE_VIEW_OBJ,VbaPaletteViewControl,1342242816 +Control13=IDC_STATIC,button,1342177287 +Control14=IDC_STATIC,button,1342177287 +Control15=IDC_STATIC,static,1342308352 +Control16=IDC_STATIC,static,1342308352 +Control17=IDC_STATIC,static,1342308352 +Control18=IDC_STATIC,static,1342308352 +Control19=IDC_STATIC,static,1342308352 +Control20=IDC_STATIC,static,1342308352 +Control21=IDC_AUTO_UPDATE,button,1342242819 + +[DLG:IDD_IO_VIEWER] +Type=1 +Class=IOViewer +ControlCount=23 +Control1=IDC_ADDRESSES,combobox,1344339971 +Control2=IDC_VALUE,static,1342308354 +Control3=IDC_BIT_15,button,1342242819 +Control4=IDC_BIT_14,button,1342242819 +Control5=IDC_BIT_13,button,1342242819 +Control6=IDC_BIT_12,button,1342242819 +Control7=IDC_BIT_11,button,1342242819 +Control8=IDC_BIT_10,button,1342242819 +Control9=IDC_BIT_9,button,1342242819 +Control10=IDC_BIT_8,button,1342242819 +Control11=IDC_BIT_7,button,1342242819 +Control12=IDC_BIT_6,button,1342242819 +Control13=IDC_BIT_5,button,1342242819 +Control14=IDC_BIT_4,button,1342242819 +Control15=IDC_BIT_3,button,1342242819 +Control16=IDC_BIT_2,button,1342242819 +Control17=IDC_BIT_1,button,1342242819 +Control18=IDC_BIT_0,button,1342242819 +Control19=IDC_AUTO_UPDATE,button,1342242819 +Control20=IDC_REFRESH,button,1342242817 +Control21=IDC_APPLY,button,1342242817 +Control22=IDC_CLOSE,button,1342242816 +Control23=IDC_STATIC,static,1342308352 + +[MNU:IDR_MENU] +Type=1 +Class=MainWnd +Command1=ID_FILE_OPEN +Command2=ID_FILE_OPENGAMEBOY +Command3=ID_FILE_LOAD +Command4=ID_FILE_SAVE +Command5=ID_FILE_LOADGAME_MOSTRECENT +Command6=ID_FILE_LOADGAME_AUTOLOADMOSTRECENT +Command7=ID_FILE_LOADGAME_SLOT1 +Command8=ID_FILE_LOADGAME_SLOT2 +Command9=ID_FILE_LOADGAME_SLOT3 +Command10=ID_FILE_LOADGAME_SLOT4 +Command11=ID_FILE_LOADGAME_SLOT5 +Command12=ID_FILE_LOADGAME_SLOT6 +Command13=ID_FILE_LOADGAME_SLOT7 +Command14=ID_FILE_LOADGAME_SLOT8 +Command15=ID_FILE_LOADGAME_SLOT9 +Command16=ID_FILE_LOADGAME_SLOT10 +Command17=ID_FILE_SAVEGAME_OLDESTSLOT +Command18=ID_FILE_SAVEGAME_SLOT1 +Command19=ID_FILE_SAVEGAME_SLOT2 +Command20=ID_FILE_SAVEGAME_SLOT3 +Command21=ID_FILE_SAVEGAME_SLOT4 +Command22=ID_FILE_SAVEGAME_SLOT5 +Command23=ID_FILE_SAVEGAME_SLOT6 +Command24=ID_FILE_SAVEGAME_SLOT7 +Command25=ID_FILE_SAVEGAME_SLOT8 +Command26=ID_FILE_SAVEGAME_SLOT9 +Command27=ID_FILE_SAVEGAME_SLOT10 +Command28=ID_FILE_PAUSE +Command29=ID_FILE_RESET +Command30=ID_FILE_RECENT_RESET +Command31=ID_FILE_RECENT_FREEZE +Command32=ID_FILE_IMPORT_BATTERYFILE +Command33=ID_FILE_IMPORT_GAMESHARKCODEFILE +Command34=ID_FILE_IMPORT_GAMESHARKSNAPSHOT +Command35=ID_FILE_EXPORT_BATTERYFILE +Command36=ID_FILE_EXPORT_GAMESHARKSNAPSHOT +Command37=ID_FILE_SCREENCAPTURE +Command38=ID_FILE_ROMINFORMATION +Command39=ID_FILE_TOGGLEMENU +Command40=ID_FILE_CLOSE +Command41=ID_FILE_EXIT +Command42=ID_OPTIONS_FRAMESKIP_THROTTLE_NOTHROTTLE +Command43=ID_OPTIONS_FRAMESKIP_THROTTLE_25 +Command44=ID_OPTIONS_FRAMESKIP_THROTTLE_50 +Command45=ID_OPTIONS_FRAMESKIP_THROTTLE_100 +Command46=ID_OPTIONS_FRAMESKIP_THROTTLE_150 +Command47=ID_OPTIONS_FRAMESKIP_THROTTLE_200 +Command48=ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER +Command49=ID_OPTIONS_FRAMESKIP_AUTOMATIC +Command50=ID_OPTIONS_VIDEO_FRAMESKIP_0 +Command51=ID_OPTIONS_VIDEO_FRAMESKIP_1 +Command52=ID_OPTIONS_VIDEO_FRAMESKIP_2 +Command53=ID_OPTIONS_VIDEO_FRAMESKIP_3 +Command54=ID_OPTIONS_VIDEO_FRAMESKIP_4 +Command55=ID_OPTIONS_VIDEO_FRAMESKIP_5 +Command56=ID_OPTIONS_VIDEO_FRAMESKIP_6 +Command57=ID_OPTIONS_VIDEO_FRAMESKIP_7 +Command58=ID_OPTIONS_VIDEO_FRAMESKIP_8 +Command59=ID_OPTIONS_VIDEO_FRAMESKIP_9 +Command60=ID_OPTIONS_VIDEO_VSYNC +Command61=ID_OPTIONS_VIDEO_RENDERMETHOD_GDI +Command62=ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW +Command63=ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D +Command64=ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL +Command65=ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY +Command66=ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY +Command67=ID_OPTIONS_VIDEO_TRIPLEBUFFERING +Command68=ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER +Command69=ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR +Command70=ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST +Command71=ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR +Command72=ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE +Command73=ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS +Command74=ID_OPTIONS_VIDEO_RENDEROPTIONS_SELECTSKIN +Command75=ID_OPTIONS_VIDEO_RENDEROPTIONS_SKIN +Command76=ID_OPTIONS_VIDEO_X1 +Command77=ID_OPTIONS_VIDEO_X2 +Command78=ID_OPTIONS_VIDEO_X3 +Command79=ID_OPTIONS_VIDEO_X4 +Command80=ID_OPTIONS_VIDEO_FULLSCREEN320X240 +Command81=ID_OPTIONS_VIDEO_FULLSCREEN640X480 +Command82=ID_OPTIONS_VIDEO_FULLSCREEN800X600 +Command83=ID_OPTIONS_VIDEO_FULLSCREEN +Command84=ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE +Command85=ID_OPTIONS_VIDEO_DISABLESFX +Command86=ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT +Command87=ID_OPTIONS_VIDEO_LAYERS_BG0 +Command88=ID_OPTIONS_VIDEO_LAYERS_BG1 +Command89=ID_OPTIONS_VIDEO_LAYERS_BG2 +Command90=ID_OPTIONS_VIDEO_LAYERS_BG3 +Command91=ID_OPTIONS_VIDEO_LAYERS_OBJ +Command92=ID_OPTIONS_VIDEO_LAYERS_WIN0 +Command93=ID_OPTIONS_VIDEO_LAYERS_WIN1 +Command94=ID_OPTIONS_VIDEO_LAYERS_OBJWIN +Command95=ID_OPTIONS_EMULATOR_ASSOCIATE +Command96=ID_OPTIONS_EMULATOR_DIRECTORIES +Command97=ID_OPTIONS_EMULATOR_DISABLESTATUSMESSAGES +Command98=ID_OPTIONS_EMULATOR_SYNCHRONIZE +Command99=ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE +Command100=ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE +Command101=ID_OPTIONS_EMULATOR_REMOVEINTROSGBA +Command102=ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH +Command103=ID_OPTIONS_EMULATOR_AGBPRINT +Command104=ID_OPTIONS_EMULATOR_REALTIMECLOCK +Command105=ID_OPTIONS_EMULATOR_AUTOHIDEMENU +Command106=ID_OPTIONS_EMULATOR_REWINDINTERVAL +Command107=ID_OPTIONS_EMULATOR_SHOWSPEED_NONE +Command108=ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE +Command109=ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED +Command110=ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT +Command111=ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC +Command112=ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM +Command113=ID_OPTIONS_EMULATOR_SAVETYPE_SRAM +Command114=ID_OPTIONS_EMULATOR_SAVETYPE_FLASH +Command115=ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR +Command116=ID_OPTIONS_EMULATOR_SAVETYPE_NONE +Command117=ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K +Command118=ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M +Command119=ID_OPTIONS_EMULATOR_SAVETYPE_ENHANCEDDETECTION +Command120=ID_OPTIONS_EMULATOR_USEBIOSFILE +Command121=ID_OPTIONS_EMULATOR_SKIPBIOS +Command122=ID_OPTIONS_EMULATOR_SELECTBIOSFILE +Command123=ID_OPTIONS_EMULATOR_PNGFORMAT +Command124=ID_OPTIONS_EMULATOR_BMPFORMAT +Command125=ID_OPTIONS_SOUND_OFF +Command126=ID_OPTIONS_SOUND_MUTE +Command127=ID_OPTIONS_SOUND_ON +Command128=ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION +Command129=ID_OPTIONS_SOUND_ECHO +Command130=ID_OPTIONS_SOUND_LOWPASSFILTER +Command131=ID_OPTIONS_SOUND_REVERSESTEREO +Command132=ID_OPTIONS_SOUND_CHANNEL1 +Command133=ID_OPTIONS_SOUND_CHANNEL2 +Command134=ID_OPTIONS_SOUND_CHANNEL3 +Command135=ID_OPTIONS_SOUND_CHANNEL4 +Command136=ID_OPTIONS_SOUND_DIRECTSOUNDA +Command137=ID_OPTIONS_SOUND_DIRECTSOUNDB +Command138=ID_OPTIONS_SOUND_11KHZ +Command139=ID_OPTIONS_SOUND_22KHZ +Command140=ID_OPTIONS_SOUND_44KHZ +Command141=ID_OPTIONS_SOUND_VOLUME_25X +Command142=ID_OPTIONS_SOUND_VOLUME_5X +Command143=ID_OPTIONS_SOUND_VOLUME_1X +Command144=ID_OPTIONS_SOUND_VOLUME_2X +Command145=ID_OPTIONS_SOUND_VOLUME_3X +Command146=ID_OPTIONS_SOUND_VOLUME_4X +Command147=ID_OPTIONS_GAMEBOY_BORDER +Command148=ID_OPTIONS_GAMEBOY_PRINTER +Command149=ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC +Command150=ID_OPTIONS_GAMEBOY_AUTOMATIC +Command151=ID_OPTIONS_GAMEBOY_GBA +Command152=ID_OPTIONS_GAMEBOY_CGB +Command153=ID_OPTIONS_GAMEBOY_SGB +Command154=ID_OPTIONS_GAMEBOY_SGB2 +Command155=ID_OPTIONS_GAMEBOY_GB +Command156=ID_OPTIONS_GAMEBOY_REALCOLORS +Command157=ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS +Command158=ID_OPTIONS_GAMEBOY_COLORS +Command159=ID_OPTIONS_PRIORITY_HIGHEST +Command160=ID_OPTIONS_PRIORITY_ABOVENORMAL +Command161=ID_OPTIONS_PRIORITY_NORMAL +Command162=ID_OPTIONS_PRIORITY_BELOWNORMAL +Command163=ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE +Command164=ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR +Command165=ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART +Command166=ID_OPTIONS_FILTER_NORMAL +Command167=ID_OPTIONS_FILTER_TVMODE +Command168=ID_OPTIONS_FILTER_2XSAI +Command169=ID_OPTIONS_FILTER_SUPER2XSAI +Command170=ID_OPTIONS_FILTER_SUPEREAGLE +Command171=ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL +Command172=ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL +Command173=ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X +Command174=ID_OPTIONS_FILTER16BIT_SIMPLE2X +Command175=ID_OPTIONS_FILTER_BILINEAR +Command176=ID_OPTIONS_FILTER_BILINEARPLUS +Command177=ID_OPTIONS_FILTER_SCANLINES +Command178=ID_OPTIONS_FILTER_HQ2X +Command179=ID_OPTIONS_FILTER_LQ2X +Command180=ID_OPTIONS_FILTER_DISABLEMMX +Command181=ID_OPTIONS_JOYPAD_CONFIGURE_1 +Command182=ID_OPTIONS_JOYPAD_CONFIGURE_2 +Command183=ID_OPTIONS_JOYPAD_CONFIGURE_3 +Command184=ID_OPTIONS_JOYPAD_CONFIGURE_4 +Command185=ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1 +Command186=ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_2 +Command187=ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_3 +Command188=ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4 +Command189=ID_OPTIONS_JOYPAD_MOTIONCONFIGURE +Command190=ID_OPTIONS_JOYPAD_AUTOFIRE_A +Command191=ID_OPTIONS_JOYPAD_AUTOFIRE_B +Command192=ID_OPTIONS_JOYPAD_AUTOFIRE_L +Command193=ID_OPTIONS_JOYPAD_AUTOFIRE_R +Command194=ID_OPTIONS_LANGUAGE_SYSTEM +Command195=ID_OPTIONS_LANGUAGE_ENGLISH +Command196=ID_OPTIONS_LANGUAGE_OTHER +Command197=ID_CHEATS_SEARCHFORCHEATS +Command198=ID_CHEATS_CHEATLIST +Command199=ID_CHEATS_AUTOMATICSAVELOADCHEATS +Command200=ID_CHEATS_DISABLECHEATS +Command201=ID_CHEATS_LOADCHEATLIST +Command202=ID_CHEATS_SAVECHEATLIST +Command203=ID_TOOLS_DISASSEMBLE +Command204=ID_TOOLS_LOGGING +Command205=ID_TOOLS_IOVIEWER +Command206=ID_TOOLS_MAPVIEW +Command207=ID_TOOLS_MEMORYVIEWER +Command208=ID_TOOLS_OAMVIEWER +Command209=ID_TOOLS_PALETTEVIEW +Command210=ID_TOOLS_TILEVIEWER +Command211=ID_DEBUG_NEXTFRAME +Command212=ID_TOOLS_DEBUG_GDB +Command213=ID_TOOLS_DEBUG_LOADANDWAIT +Command214=ID_TOOLS_DEBUG_BREAK +Command215=ID_TOOLS_DEBUG_DISCONNECT +Command216=ID_OPTIONS_SOUND_STARTRECORDING +Command217=ID_OPTIONS_SOUND_STOPRECORDING +Command218=ID_TOOLS_RECORD_STARTAVIRECORDING +Command219=ID_TOOLS_RECORD_STOPAVIRECORDING +Command220=ID_TOOLS_RECORD_STARTMOVIERECORDING +Command221=ID_TOOLS_RECORD_STOPMOVIERECORDING +Command222=ID_TOOLS_PLAY_STARTMOVIEPLAYING +Command223=ID_TOOLS_PLAY_STOPMOVIEPLAYING +Command224=ID_TOOLS_REWIND +Command225=ID_TOOLS_CUSTOMIZE +Command226=ID_HELP_BUGREPORT +Command227=ID_HELP_FAQ +Command228=ID_HELP_ABOUT +CommandCount=228 + +[ACL:IDR_ACCELERATOR] +Type=1 +Class=? +Command1=ID_OPTIONS_VIDEO_LAYERS_BG0 +Command2=ID_OPTIONS_JOYPAD_AUTOFIRE_A +Command3=ID_OPTIONS_VIDEO_LAYERS_BG1 +Command4=ID_OPTIONS_JOYPAD_AUTOFIRE_B +Command5=ID_OPTIONS_VIDEO_LAYERS_BG2 +Command6=ID_OPTIONS_JOYPAD_AUTOFIRE_L +Command7=ID_OPTIONS_VIDEO_LAYERS_BG3 +Command8=ID_OPTIONS_JOYPAD_AUTOFIRE_R +Command9=ID_OPTIONS_VIDEO_LAYERS_OBJ +Command10=ID_OPTIONS_VIDEO_LAYERS_WIN0 +Command11=ID_OPTIONS_VIDEO_LAYERS_WIN1 +Command12=ID_OPTIONS_VIDEO_LAYERS_OBJWIN +Command13=ID_TOOLS_REWIND +Command14=ID_CHEATS_SEARCHFORCHEATS +Command15=ID_FILE_LOAD +Command16=ID_DEBUG_NEXTFRAME +Command17=ID_FILE_OPEN +Command18=ID_FILE_PAUSE +Command19=ID_FILE_RESET +Command20=ID_FILE_SAVE +Command21=ID_FILE_TOGGLEMENU +Command22=ID_FILE_LOADGAME_SLOT1 +Command23=ID_FILE_MRU_FILE1 +Command24=ID_FILE_SAVEGAME_SLOT1 +Command25=ID_FILE_LOADGAME_SLOT10 +Command26=ID_FILE_MRU_FILE10 +Command27=ID_FILE_SAVEGAME_SLOT10 +Command28=ID_FILE_LOADGAME_SLOT2 +Command29=ID_FILE_MRU_FILE2 +Command30=ID_FILE_SAVEGAME_SLOT2 +Command31=ID_FILE_LOADGAME_SLOT3 +Command32=ID_FILE_MRU_FILE3 +Command33=ID_FILE_SAVEGAME_SLOT3 +Command34=ID_FILE_LOADGAME_SLOT4 +Command35=ID_FILE_MRU_FILE4 +Command36=ID_FILE_SAVEGAME_SLOT4 +Command37=ID_FILE_LOADGAME_SLOT5 +Command38=ID_FILE_MRU_FILE5 +Command39=ID_FILE_SAVEGAME_SLOT5 +Command40=ID_FILE_LOADGAME_SLOT6 +Command41=ID_FILE_MRU_FILE6 +Command42=ID_FILE_SAVEGAME_SLOT6 +Command43=ID_FILE_LOADGAME_SLOT7 +Command44=ID_FILE_MRU_FILE7 +Command45=ID_FILE_SAVEGAME_SLOT7 +Command46=ID_FILE_LOADGAME_SLOT8 +Command47=ID_FILE_MRU_FILE8 +Command48=ID_FILE_SAVEGAME_SLOT8 +Command49=ID_FILE_LOADGAME_SLOT9 +Command50=ID_FILE_MRU_FILE9 +Command51=ID_FILE_SAVEGAME_SLOT9 +Command52=ID_FILE_EXIT +CommandCount=52 + +[CLS:AddGSACode] +Type=0 +HeaderFile=GBACheats.h +ImplementationFile=GBACheats.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=AddGSACode + +[CLS:GBCheatList] +Type=0 +HeaderFile=GBCheatsDlg.h +ImplementationFile=GBCheatsDlg.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=GBCheatList + +[CLS:Disassemble] +Type=0 +HeaderFile=Disassemble.h +ImplementationFile=Disassemble.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=Disassemble + +[CLS:GBDisassemble] +Type=0 +HeaderFile=GBDisassemble.h +ImplementationFile=GBDisassemble.cpp +BaseClass=CDialog +Filter=D +LastObject=IDC_GO +VirtualFilter=dWC + +[CLS:Logging] +Type=0 +HeaderFile=Logging.h +ImplementationFile=Logging.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=IDC_LOG + +[CLS:IOViewer] +Type=0 +HeaderFile=IOViewer.h +ImplementationFile=IOViewer.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=ID_FILE_LOADGAME_SLOT1 + +[CLS:ZoomControl] +Type=0 +HeaderFile=ZoomControl.h +ImplementationFile=ZoomControl.cpp +BaseClass=CWnd +Filter=W +VirtualFilter=WC +LastObject=ZoomControl + +[CLS:BitmapControl] +Type=0 +HeaderFile=BitmapControl.h +ImplementationFile=BitmapControl.cpp +BaseClass=CScrollView +Filter=C +VirtualFilter=VWC +LastObject=BitmapControl + +[CLS:MapView] +Type=0 +HeaderFile=MapView.h +ImplementationFile=MapView.cpp +BaseClass=CDialog +Filter=D +LastObject=ID_CHEATS_AUTOMATICSAVELOADCHEATS +VirtualFilter=dWC + +[CLS:GBMapView] +Type=0 +HeaderFile=GBMapView.h +ImplementationFile=GBMapView.cpp +BaseClass=CDialog +Filter=D +LastObject=GBMapView +VirtualFilter=dWC + +[CLS:MemoryViewer] +Type=0 +HeaderFile=MemoryViewer.h +ImplementationFile=MemoryViewer.cpp +BaseClass=CWnd +Filter=W +VirtualFilter=WC +LastObject=ID_CHEATS_AUTOMATICSAVELOADCHEATS + +[CLS:MemoryViewerDlg] +Type=0 +HeaderFile=MemoryViewerDlg.h +ImplementationFile=MemoryViewerDlg.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=MemoryViewerDlg + +[CLS:MemoryViewerAddressSize] +Type=0 +HeaderFile=MemoryViewerAddressSize.h +ImplementationFile=MemoryViewerAddressSize.cpp +BaseClass=CDialog +Filter=D +LastObject=ID_CANCEL +VirtualFilter=dWC + +[CLS:GBMemoryViewerDlg] +Type=0 +HeaderFile=GBMemoryViewerDlg.h +ImplementationFile=GBMemoryViewerDlg.cpp +BaseClass=CDialog +Filter=D +LastObject=GBMemoryViewerDlg +VirtualFilter=dWC + +[CLS:OamView] +Type=0 +HeaderFile=OamView.h +ImplementationFile=OamView.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=ID_CHEATS_AUTOMATICSAVELOADCHEATS + +[CLS:GBOamView] +Type=0 +HeaderFile=GBOamView.h +ImplementationFile=GBOamView.cpp +BaseClass=CDialog +Filter=D +LastObject=GBOamView +VirtualFilter=dWC + +[CLS:PaletteViewControl] +Type=0 +HeaderFile=PaletteViewControl.h +ImplementationFile=PaletteViewControl.cpp +BaseClass=CWnd +Filter=W +VirtualFilter=WC +LastObject=ID_CHEATS_AUTOMATICSAVELOADCHEATS + +[CLS:PaletteView] +Type=0 +HeaderFile=paletteview.h +ImplementationFile=paletteview.cpp +BaseClass=CDialog +LastObject=ID_CHEATS_AUTOMATICSAVELOADCHEATS + +[CLS:GBPaletteView] +Type=0 +HeaderFile=GBPaletteView.h +ImplementationFile=GBPaletteView.cpp +BaseClass=CDialog +Filter=D +LastObject=GBPaletteView +VirtualFilter=dWC + +[CLS:TileView] +Type=0 +HeaderFile=TileView.h +ImplementationFile=TileView.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=ID_CHEATS_AUTOMATICSAVELOADCHEATS + +[CLS:GBTileView] +Type=0 +HeaderFile=GBTileView.h +ImplementationFile=GBTileView.cpp +BaseClass=CDialog +Filter=D +LastObject=GBTileView +VirtualFilter=dWC + +[CLS:GBPrinterDlg] +Type=0 +HeaderFile=gbprinterdlg.h +ImplementationFile=gbprinterdlg.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=GBPrinterDlg + +[CLS:GDBPortDlg] +Type=0 +HeaderFile=GDBConnection.h +ImplementationFile=GDBConnection.cpp +BaseClass=CDialog +Filter=D +LastObject=GDBPortDlg +VirtualFilter=dWC + +[CLS:GDBWaitingDlg] +Type=0 +HeaderFile=GDBConnection.h +ImplementationFile=GDBConnection.cpp +BaseClass=CDialog +Filter=D +LastObject=GDBWaitingDlg +VirtualFilter=dWC + +[CLS:AccelEditor] +Type=0 +HeaderFile=AccelEditor.h +ImplementationFile=AccelEditor.cpp +BaseClass=CDialog +Filter=D +LastObject=IDC_REMOVE +VirtualFilter=dWC + +[CLS:Hyperlink] +Type=0 +HeaderFile=Hyperlink.h +ImplementationFile=Hyperlink.cpp +BaseClass=CStatic +Filter=W +VirtualFilter=WC +LastObject=Hyperlink + +[DLG:IDD_MAX_SCALE] +Type=1 +Class=MaxScale +ControlCount=5 +Control1=IDC_VALUE,edit,1350631552 +Control2=ID_OK,button,1342242817 +Control3=ID_CANCEL,button,1342242816 +Control4=IDC_STATIC,static,1342308352 +Control5=IDC_STATIC,static,1342308352 + +[CLS:MaxScale] +Type=0 +HeaderFile=MaxScale.h +ImplementationFile=MaxScale.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=MaxScale + +[DLG:IDD_BUG_REPORT] +Type=1 +Class=BugReport +ControlCount=4 +Control1=ID_OK,button,1342242816 +Control2=IDC_BUG_REPORT,edit,1352730820 +Control3=IDC_STATIC,static,1342308352 +Control4=IDC_COPY,button,1342242817 + +[CLS:BugReport] +Type=0 +HeaderFile=BugReport.h +ImplementationFile=BugReport.cpp +BaseClass=CDialog +Filter=D +VirtualFilter=dWC +LastObject=BugReport + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/VBA.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/VBA.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,2272 @@ +// VBA.cpp : Defines the class behaviors for the application. +// +#include "stdafx.h" +#include +#include + +#include "resource.h" +#include "VBA.h" +#include "AVIWrite.h" +#include "Input.h" +#include "IUpdate.h" +#include "LangSelect.h" +#include "MainWnd.h" +#include "Reg.h" +#include "WavWriter.h" +#include "WinResUtil.h" +#include "WinMiscUtil.h" +#include "ramwatch.h" + +#include "../gba/GBA.h" +#include "../gba/GBAGlobals.h" +#include "../gba/agbprint.h" +#include "../gb/GB.h" +#include "../gb/gbGlobals.h" +#include "../gb/gbPrinter.h" +#include "../common/CheatSearch.h" +#include "../gba/RTC.h" +#include "../gba/GBASound.h" +#include "../common/Util.h" +#include "../common/Text.h" +#include "../common/movie.h" +#include "../common/nesvideos-piece.h" +#include "../common/vbalua.h" +#include "../filters/filters.h" +#include "../version.h" + +extern IDisplay *newGDIDisplay(); +extern IDisplay *newDirectDrawDisplay(); +extern IDisplay *newDirect3DDisplay(); +extern IDisplay *newOpenGLDisplay(); + +extern Input *newDirectInput(); + +extern void remoteStubSignal(int, int); +extern void remoteOutput(char *, u32); +extern void remoteStubMain(); +extern void remoteSetProtocol(int); +extern void remoteCleanUp(); +extern int remoteSocket; + +void winlog(const char *msg, ...); + +bool debugger = false; + +char movieFileToPlay[1024]; +bool playMovieFile = false; +bool playMovieFileReadOnly = false; +char wavFileToOutput [1024]; +bool outputWavFile = false; +bool outputAVIFile = false; +bool flagHideMenu = false; +int quitAfterTime = -1; +int pauseAfterTime = -1; + +void winSignal(int, int); +void winOutput(char *, u32); + +void (*dbgSignal)(int, int) = winSignal; +void (*dbgOutput)(char *, u32) = winOutput; + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif + +// nowhere good to put them to + +void DrawTextMessages(u8 *dest, int pitch, int left, int bottom) +{ + for (int slot = 0; slot < SCREEN_MESSAGE_SLOTS; slot++) + { + if (theApp.screenMessage[slot]) + { + if ((theApp.screenMessageDuration[slot] < 0 || + (int)(GetTickCount() - theApp.screenMessageTime[slot]) < theApp.screenMessageDuration[slot]) && + (!theApp.disableStatusMessage || slot == 1 || slot == 2)) + { + drawText(dest, + pitch, + left, + bottom - 10 * (slot + 1), + theApp.screenMessageBuffer[slot], + theApp.screenMessageColorBuffer[slot]); + } + else + { + theApp.screenMessage[slot] = false; + } + } + } +} + +// draw Lua graphics in game screen +void DrawLuaGui() +{ + int copyX = 240, copyY = 160; + int screenX = 240, screenY = 160; + int copyOffsetX = 0, copyOffsetY = 0; + if (systemCartridgeType == 1) + { + if (gbBorderOn) + { + copyX = 256, copyY = 224; + screenX = 256, screenY = 224; + } + else + { + copyX = 160, copyY = 144; + screenX = 160, screenY = 144; + } + } + int pitch = copyX * (systemColorDepth / 8) + (systemColorDepth == 24 ? 0 : 4); + + ++copyOffsetY; // don't know why it's needed + + VBALuaGui(&pix[copyOffsetY * pitch + copyOffsetX * (systemColorDepth / 8)], copyX, screenX, screenY); + VBALuaClearGui(); +} + +void directXMessage(const char *msg) +{ + systemMessage( + IDS_DIRECTX_7_REQUIRED, + "DirectX 7.0 or greater is required to run.\nDownload at http://www.microsoft.com/directx.\n\nError found at: %s", + msg); +} + +void winlog(const char *msg, ...) +{ + CString buffer; + va_list valist; + + va_start(valist, msg); + buffer.FormatV(msg, valist); + + FILE *winout = fopen("vba-trace.log", "w"); + + fputs(buffer, winout); + + fclose(winout); + + va_end(valist); +} + +// code from SDL_main.c for Windows +/* Parse a command line buffer into arguments */ + +static int parseCommandLine(char *cmdline, char * *argv) +{ + char *bufp; + int argc; + + argc = 0; + for (bufp = cmdline; *bufp; ) + { + /* Skip leading whitespace */ + while (isspace(*bufp)) + { + ++bufp; + } + /* Skip over argument */ + if (*bufp == '"') + { + ++bufp; + if (*bufp) + { + if (argv) + { + argv[argc] = bufp; + } + ++argc; + } + /* Skip over word */ + while (*bufp && (*bufp != '"')) + { + ++bufp; + } + } + else + { + if (*bufp) + { + if (argv) + { + argv[argc] = bufp; + } + ++argc; + } + /* Skip over word */ + while (*bufp && !isspace(*bufp)) + { + ++bufp; + } + } + if (*bufp) + { + if (argv) + { + *bufp = '\0'; + } + ++bufp; + } + } + if (argv) + { + argv[argc] = NULL; + } + return(argc); +} + +static void debugSystemScreenMessage1(const char *msg) +{ + systemScreenMessage(msg, 3); +} + +static void debugSystemScreenMessage2(const char *msg) +{ + systemScreenMessage(msg, 4); +} + +static void winSignal(int, int) +{} + +#define CPUReadByteQuick(addr) \ + map[(addr) >> 24].address[(addr) & map[(addr) >> 24].mask] + +static void winOutput(char *s, u32 addr) +{ + if (s) + { + log(s); + } + else + { + CString str; + char c; + + c = CPUReadByteQuick(addr); + addr++; + while (c) + { + str += c; + c = CPUReadByteQuick(addr); + addr++; + } + log(str); + } +} + +typedef BOOL (WINAPI * GETMENUBARINFO)(HWND, LONG, LONG, PMENUBARINFO); + +static int winGetMenuBarHeight() +{ + HINSTANCE hinstDll = /**/ ::LoadLibrary("USER32.DLL"); + + if (hinstDll) + { + GETMENUBARINFO func = (GETMENUBARINFO)GetProcAddress(hinstDll, "GetMenuBarInfo"); + + if (func) + { + MENUBARINFO info; + info.cbSize = sizeof(info); + + func(AfxGetMainWnd()->GetSafeHwnd(), OBJID_MENU, 0, &info); + + /**/ ::FreeLibrary(hinstDll); + + return info.rcBar.bottom - info.rcBar.top + 1; + } + } + + return GetSystemMetrics(SM_CYMENU); +} + +///////////////////////////////////////////////////////////////////////////// +// VBA + +BEGIN_MESSAGE_MAP(VBA, CWinApp) +//{{AFX_MSG_MAP(VBA) +// NOTE - the ClassWizard will add and remove mapping macros here. +// DO NOT EDIT what you see in these blocks of generated code! +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// The one and only VBA object + +VBA theApp; + +///////////////////////////////////////////////////////////////////////////// +// VBA construction + +VBA::VBA() : emulator(::theEmulator) +{ + // important + { +#ifdef MULTITHREAD_STDLOCALE_WORKAROUND + // Note: there's a known threading bug regarding std::locale with MSVC according to + // http://connect.microsoft.com/VisualStudio/feedback/details/492128/std-locale-constructor-modifies-global-locale-via-setlocale + int iPreviousFlag = ::_configthreadlocale(_ENABLE_PER_THREAD_LOCALE); +#endif + using std::locale; + locale::global(locale(locale::classic(), "", locale::collate | locale::ctype)); + +#ifdef MULTITHREAD_STDLOCALE_WORKAROUND + if (iPreviousFlag > 0 ) + ::_configthreadlocale(iPreviousFlag); +#endif + } + + mode320Available = false; + mode640Available = false; + mode800Available = false; + windowPositionX = 0; + windowPositionY = 0; + filterFunction = NULL; + ifbFunction = NULL; + ifbType = 0; + filterType = 0; + filterWidth = 0; + filterHeight = 0; + fsWidth = 0; + fsHeight = 0; + fsColorDepth = 0; + fsForceChange = false; + surfaceSizeX = 0; + surfaceSizeY = 0; + sizeX = 0; + sizeY = 0; + videoOption = 0; + fullScreenStretch = false; + disableStatusMessage = false; + showSpeed = 1; + showSpeedTransparent = true; + showRenderedFrames = 0; + for (int j = 0; j < SCREEN_MESSAGE_SLOTS; j++) + { + screenMessage[j] = false; + screenMessageTime[j] = 0; + screenMessageDuration[j] = 0; + } + menuToggle = true; + display = NULL; + menu = NULL; + popup = NULL; + soundInitialized = false; + useBiosFile = false; + skipBiosFile = false; + active = true; + paused = false; + recentFreeze = false; + autoSaveLoadCheatList = false; + pauseDuringCheatSearch = false; + modelessCheatDialogIsOpen = false; +// winout = NULL; +// removeIntros = false; + autoIPS = true; + winGbBorderOn = 0; + hideMovieBorder = false; + winFlashSize = 0x10000; + winRtcEnable = false; + winSaveType = 0; + rewindMemory = NULL; + frameSearchMemory = NULL; + rewindPos = 0; + rewindTopPos = 0; + rewindCounter = 0; + rewindCount = 0; + rewindSaveNeeded = false; + rewindTimer = 0; + captureFormat = 0; + tripleBuffering = true; + autoHideMenu = false; + throttle = 100; + throttleLastTime = 0; +/// autoFrameSkipLastTime = 0; +/// autoFrameSkip = false; + vsync = false; + changingVideoSize = false; + pVideoDriverGUID = NULL; + renderMethod = DIRECT_DRAW; + iconic = false; + ddrawEmulationOnly = false; + ddrawUsingEmulationOnly = false; + ddrawDebug = false; + ddrawUseVideoMemory = false; + d3dFilter = 0; + glFilter = 0; + glType = 0; + regEnabled = false; + pauseWhenInactive = true; + muteWhenInactive = true; + enableBackgroundInput = false; + alwaysOnTop = false; + filenamePreference = true; + frameCounter = false; + lagCounter = false; + extraCounter = false; + inputDisplay = false; + speedupToggle = false; + useOldSync = false; + allowLeftRight = false; + autofireAccountForLag = false; + nextframeAccountForLag = false; + muteFrameAdvance = false; + muteWhenInactive = false; + winMuteForNow = false; + winGbPrinterEnabled = false; + threadPriority = 2; + disableMMX = false; + languageOption = 0; + languageModule = NULL; + languageName = ""; + renderedFrames = 0; + input = NULL; + joypadDefault = 0; + autoFire = 0; + autoFire2 = 0; + autoHold = 0; + autoFireToggle = false; + winPauseNextFrame = false; + soundRecording = false; + soundRecorder = NULL; + sound = NULL; + aviRecording = false; + aviRecorder = NULL; + painting = false; + mouseCounter = 0; + movieReadOnly = true; + movieOnEndPause = false; + movieOnEndBehavior = 0; + wasPaused = false; + fsMaxScale = 0; + romSize = 0; + autoLoadMostRecent = false; + loadMakesRecent = false; + loadMakesCurrent = false; + saveMakesCurrent = false; + currentSlot = 0; + showSlotTime = false; + frameSearchLoadValid = false; + frameSearching = false; + frameSearchSkipping = false; + nvVideoLog = false; + nvAudioLog = false; + LoggingEnabled = 0; +/// FPS = 60; + + updateCount = 0; + + systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; + + ZeroMemory(&emulator, sizeof(emulator)); + + hAccel = NULL; + + for (int i = 0; i < 24; ) + { + systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10); + systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10); + systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10); + systemGbPalette[i++] = 0; + } + + VBAMovieInit(); + + TIMECAPS tc; + if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) == TIMERR_NOERROR) + { + wmTimerRes = min(max(tc.wPeriodMin, 1), tc.wPeriodMax); + timeBeginPeriod(wmTimerRes); + } + else + { + wmTimerRes = 5; + timeBeginPeriod(wmTimerRes); + } +} + +VBA::~VBA() +{ + if (VBAMovieActive()) + VBAMovieStop(true); + + saveSettings(); + + InterframeCleanup(); + + if (aviRecorder) + { + delete aviRecorder; + aviRecorder = NULL; + aviRecording = false; + } + + if (soundRecorder) + { + delete soundRecorder; + soundRecorder = NULL; + } + soundRecording = false; + soundPause(); + soundShutdown(); + + ((MainWnd *)(m_pMainWnd))->winFileClose(); + + if (input) + delete input; + + shutdownDisplay(); + + if (rewindMemory) + free(rewindMemory); + + if (frameSearchMemory) + free(frameSearchMemory); + + timeEndPeriod(wmTimerRes); +} + +///////////////////////////////////////////////////////////////////////////// +// VBA initialization + +#include + +BOOL VBA::InitInstance() +{ + AfxEnableControlContainer(); + // Standard initialization + // If you are not using these features and wish to reduce the size + // of your final executable, you should remove from the following + // the specific initialization routines you do not need. + +//#ifdef _AFXDLL +// Enable3dControls(); // Call this when using MFC in a shared DLL +//#else +// Enable3dControlsStatic(); // Call this when linking to MFC statically +//#endif + + SetRegistryKey(_T("VBA")); + + remoteSetProtocol(0); + + systemVerbose = GetPrivateProfileInt("config", "verbose", 0, "VBA.ini"); + systemDebug = GetPrivateProfileInt("config", "debug", 0, "VBA.ini"); + ddrawDebug = GetPrivateProfileInt("config", "ddrawDebug", 0, "VBA.ini") ? true : false; + + wndClass = AfxRegisterWndClass(0, LoadCursor(IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH), LoadIcon(IDI_ICON)); + + char winBuffer[2048]; + GetModuleFileName(NULL, winBuffer, 2048); + char *p = strrchr(winBuffer, '\\'); + if (p) + *p = 0; + exeDir = winBuffer; + + regInit(winBuffer); + + loadSettings(); + theApp.LuaFastForward = -1; + if (!initInput()) + return FALSE; + + if (!initDisplay()) + { + if (videoOption >= VIDEO_320x240) + { + regSetDwordValue("video", VIDEO_1X); + if (pVideoDriverGUID) + regSetDwordValue("defaultVideoDriver", TRUE); + } + return FALSE; + } + + hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR)); + + winAccelMgr.Connect((MainWnd *)m_pMainWnd); + + extern void winAccelAddCommandsFromMenu(CAcceleratorManager & mgr, CMenu * pMenu, const CString &parentStr); + extern void winAccelAddCommandsFromTable(CAcceleratorManager & mgr); + + winAccelAddCommandsFromMenu(winAccelMgr, &m_menu, CString()); + winAccelAddCommandsFromTable(winAccelMgr); + + winAccelMgr.CreateDefaultTable(); + winAccelMgr.Load(); + winAccelMgr.UpdateWndTable(); + winAccelMgr.UpdateMenu(menu); + + if (m_lpCmdLine[0]) + { + int argc = parseCommandLine(m_lpCmdLine, NULL); + char * *argv = (char * *)malloc((argc + 1) * sizeof(char *)); + parseCommandLine(m_lpCmdLine, argv); + + bool gotFlag = false, enoughArgs = false; + for (int i = 0; i < argc; i++) + { + if (argv[i][0] == '-' || gotFlag) + { + if (!gotFlag) + loadSettings(); + gotFlag = true; + if (_stricmp(argv[i], "-rom") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + romFilename = argv[++i]; + winCorrectPath(romFilename); + gameFilename = romFilename; + } + else if (_stricmp(argv[i], "-bios") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + biosFileName = argv[++i]; + winCorrectPath(biosFileName); + + //systemLoadBIOS(); + } + else if (_stricmp(argv[i], "-frameskip") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + frameSkip = atoi(argv[++i]); + if (frameSkip < 0) + frameSkip = 0; + if (frameSkip > 9) + frameSkip = 9; + gbFrameSkip = frameSkip; + } + else if (_stricmp(argv[i], "-throttle") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + throttle = atoi(argv[++i]); + if (throttle < 5) + throttle = 5; + if (throttle > 1000) + throttle = 1000; + } + else if (_stricmp(argv[i], "-throttleKeepPitch") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + accuratePitchThrottle = atoi(argv[++i]) != 0; + } + else if (_stricmp(argv[i], "-synchronize") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + synchronize = atoi(argv[++i]) != 0; + } + else if (_stricmp(argv[i], "-hideborder") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + hideMovieBorder = atoi(argv[++i]) != 0; + } + else if (_stricmp(argv[i], "-play") == 0) + { + playMovieFile = true; + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + strcpy(movieFileToPlay, argv[++i]); + winCorrectPath(movieFileToPlay); + if (i + 1 >= argc || argv[i + 1][0] == '-') { --i; goto invalidArgument; } + playMovieFileReadOnly = atoi(argv[++i]) != 0; + } + else if (_stricmp(argv[i], "-videoLog") == 0) + { + nvVideoLog = true; + nvAudioLog = true; + LoggingEnabled = 2; + if (i + 1 >= argc || argv[i + 1][0] == '-') {} + else + NESVideoSetVideoCmd(argv[++i]); + } + else if (_stricmp(argv[i], "-logDebug") == 0) + { + NESVideoEnableDebugging(debugSystemScreenMessage1, debugSystemScreenMessage2); + } + else if (_stricmp(argv[i], "-logToFile") == 0) + { + NESVideoSetFileFuncs(fopen, fclose); + } + else if (_stricmp(argv[i], "-outputWAV") == 0) + { + outputWavFile = true; + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + strcpy(wavFileToOutput, argv[++i]); + } + else if (_stricmp(argv[i], "-outputAVI") == 0) + { + outputAVIFile = true; + } + else if (_stricmp(argv[i], "-quitAfter") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + quitAfterTime = atoi(argv[++i]); + } + else if (_stricmp(argv[i], "-pauseAt") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + pauseAfterTime = atoi(argv[++i]); + } + else if (_stricmp(argv[i], "-videoScale") == 0) + { + if (i + 1 >= argc || argv[i + 1][0] == '-') + goto invalidArgument; + int size = atoi(argv[++i]); + if (size < 1) + size = 1; + if (size > 4) + size = 4; + switch (size) + { + case 1: + videoOption = VIDEO_1X; break; + case 2: + videoOption = VIDEO_2X; break; + case 3: + videoOption = VIDEO_3X; break; + case 4: + videoOption = VIDEO_4X; break; + } + } + else if (_stricmp(argv[i], "-hideMenu") == 0) + { + flagHideMenu = true; + } + else + { + enoughArgs = true; +invalidArgument: + char str [2048]; // the string is larger than 1024 bytes + strcpy(str, ""); + if (_stricmp(argv[i], "-h") != 0) + if (enoughArgs) + sprintf(str, "Invalid commandline argument %d: %s\n", i, argv[i]); + else + sprintf(str, "Not enough arguments for arg %d: %s\n", i, argv[i]); + strcat(str, "Valid commands:\n" + "-h \t\t\t displays this help\n" + "-rom filename \t\t opens the given ROM\n" + "-bios filename \t\t use the given GBA BIOS\n" + "-play filename val \t\t plays the given VBM movie (val: 1 = read-only, 0 = editable)\n" + "-outputWAV filename \t outputs WAV audio to the given file\n" + "-outputAVI \t\t outputs an AVI (you are prompted for location and codec)\n" + "-frameskip val \t\t sets the frameskip amount to the given value\n" + "-synchronize val \t\t limits running speed to sound playing speed, (0 = off, 1 = on)\n" + "-throttle val \t\t sets the throttle speed to the given percentage\n" + "-hideborder val \t\t hides SGB border, if any (0 = show, 1 = hide)\n" + "-throttleKeepPitch val \t if throttle and synch, don't change sound freq (0 = off, 1 = on)\n" + "-quitAfter val \t\t close program when frame counter == val\n" + "-pauseAt val \t\t pause (movie) once when frame counter == val\n" + "-videoScale val \t\t sets the video size (val = 1 for 1X, 2 for 2X, 3 for 3X, or 4 for 4X)\n" + "-hideMenu \t\t hides the menu until program exit\n" + "\n" + "-videoLog args \t does (nesvideos) video+audio logging with the given arguments\n" + "-logToFile \t tells logging to use fopen/fclose of args, if logging is enabled\n" + "-logDebug \t tells logging to output debug info to screen, if logging is enabled\n" + ); + theApp.winCheckFullscreen(); + AfxGetApp()->m_pMainWnd->MessageBox(str, "Commandline Help", MB_OK | MB_ICONINFORMATION); + exit(0); + } + } + else + { + // assume anything else is a ROM, for backward compatibility + romFilename = argv[i++]; + gameFilename = romFilename; + loadSettings(); + } + } + +/* + int index = filename.ReverseFind('.'); + + if (index != -1) + filename = filename.Left(index); + */ + if (romFilename.GetLength() > 0) + { + ((MainWnd *)theApp.m_pMainWnd)->winFileRun(); + } + free(argv); + } + + return TRUE; +} + +void VBA::adjustDestRect() +{ + POINT point; + + point.x = 0; + point.y = 0; + + m_pMainWnd->ClientToScreen(&point); + dest.top = point.y; + dest.left = point.x; + + point.x = surfaceSizeX; + point.y = surfaceSizeY; + + m_pMainWnd->ClientToScreen(&point); + dest.bottom = point.y; + dest.right = point.x; + + if (videoOption > VIDEO_4X) + { + int menuSkip = 0; + if (menuToggle) + { + menuSkip = winGetMenuBarHeight(); + } + + if (fullScreenStretch) + { + dest.top = menuSkip; + dest.left = 0; + dest.right = fsWidth; + dest.bottom = fsHeight; + } + else + { + int top = (fsHeight - surfaceSizeY) / 2; + int left = (fsWidth - surfaceSizeX) / 2; + dest.top += top - menuSkip * 2; + dest.bottom += top; + dest.left += left; + dest.right += left; + } + } +} + +void VBA::updateIFB() +{ + if (systemColorDepth == 16) + { + switch (ifbType) + { + case 0: + default: + ifbFunction = NULL; + break; + case 1: + ifbFunction = MotionBlurIB; + break; + case 2: + ifbFunction = SmartIB; + break; + } + } + else if (systemColorDepth == 32) + { + switch (ifbType) + { + case 0: + default: + ifbFunction = NULL; + break; + case 1: + ifbFunction = MotionBlurIB32; + break; + case 2: + ifbFunction = SmartIB32; + break; + } + } + else + ifbFunction = NULL; +} + +void VBA::updateFilter() +{ + filterWidth = sizeX; + filterHeight = sizeY; + + if (systemColorDepth == 16 && (videoOption > VIDEO_1X && + videoOption != VIDEO_320x240)) + { + switch (filterType) + { + default: + case 0: + filterFunction = NULL; + break; + case 1: + filterFunction = ScanlinesTV; + break; + case 2: + filterFunction = _2xSaI; + break; + case 3: + filterFunction = Super2xSaI; + break; + case 4: + filterFunction = SuperEagle; + break; + case 5: + filterFunction = Pixelate2x16; + break; + case 6: + filterFunction = MotionBlur; + break; + case 7: + filterFunction = AdMame2x; + break; + case 8: + filterFunction = Simple2x16; + break; + case 9: + filterFunction = Bilinear; + break; + case 10: + filterFunction = BilinearPlus; + break; + case 11: + filterFunction = Scanlines; + break; + case 12: + filterFunction = hq2xS; + break; + case 13: + filterFunction = hq2x; + break; + case 14: + filterFunction = lq2x; + break; + case 15: + filterFunction = hq3xS; + break; + case 16: + filterFunction = hq3x; + break; + case 17: + filterFunction = Simple3x16; + break; + case 18: + filterFunction = Simple4x16; + break; + case 19: + filterFunction = Pixelate3x16; + break; + case 20: + filterFunction = Pixelate4x16; + break; + } + switch (filterType) + { + case 0: // normal -> 1x texture + rect.right = sizeX; + rect.bottom = sizeY; + break; + default: // other -> 2x texture + rect.right = sizeX * 2; + rect.bottom = sizeY * 2; + memset(delta, 255, sizeof(delta)); + break; + case 15: // hq3x -> 3x texture + case 16: + case 17: + case 19: + rect.right = sizeX * 3; + rect.bottom = sizeY * 3; + memset(delta, 255, sizeof(delta)); + break; + case 18: // Simple4x -> 4x texture + case 20: + rect.right = sizeX * 4; + rect.bottom = sizeY * 4; + memset(delta, 255, sizeof(delta)); + break; + } + } + else + { + if (systemColorDepth == 32 && videoOption > VIDEO_1X && + videoOption != VIDEO_320x240) + { + switch (filterType) + { + default: + case 0: + filterFunction = NULL; + break; + case 1: + filterFunction = ScanlinesTV32; + break; + case 2: + filterFunction = _2xSaI32; + break; + case 3: + filterFunction = Super2xSaI32; + break; + case 4: + filterFunction = SuperEagle32; + break; + case 5: + filterFunction = Pixelate2x32; + break; + case 6: + filterFunction = MotionBlur32; + break; + case 7: + filterFunction = AdMame2x32; + break; + case 8: + filterFunction = Simple2x32; + break; + case 9: + filterFunction = Bilinear32; + break; + case 10: + filterFunction = BilinearPlus32; + break; + case 11: + filterFunction = Scanlines32; + break; + case 12: + filterFunction = hq2xS32; + break; + case 13: + filterFunction = hq2x32; + break; + case 14: + filterFunction = lq2x32; + break; + case 15: + filterFunction = hq3xS32; + break; + case 16: + filterFunction = hq3x32; + break; + case 17: + filterFunction = Simple3x32; + break; + case 18: + filterFunction = Simple4x32; + break; + case 19: + filterFunction = Pixelate3x32; + break; + case 20: + filterFunction = Pixelate4x32; + break; + } + switch (filterType) + { + case 0: // normal -> 1x texture + rect.right = sizeX; + rect.bottom = sizeY; + break; + default: // other -> 2x texture + rect.right = sizeX * 2; + rect.bottom = sizeY * 2; + memset(delta, 255, sizeof(delta)); + break; + case 15: // hq3x -> 3x texture + case 16: + case 17: + case 19: + rect.right = sizeX * 3; + rect.bottom = sizeY * 3; + memset(delta, 255, sizeof(delta)); + break; + case 18: // Simple4x -> 4x texture + case 20: + rect.right = sizeX * 4; + rect.bottom = sizeY * 4; + memset(delta, 255, sizeof(delta)); + break; + } + } + else + filterFunction = NULL; + } + + if (display) + display->changeRenderSize(rect.right, rect.bottom); +} + +void VBA::recreateMenuBar() +{ + m_menu.Detach(); + m_menu.Attach(winResLoadMenu(MAKEINTRESOURCE(IDR_MENU))); + + if (m_pMainWnd && menuToggle) // assuming that whether the menu has been set is always kept tracked + { + m_pMainWnd->SetMenu(&m_menu); + } + + if (menu != NULL) + { + DestroyMenu(menu); + } + + menu = m_menu.GetSafeHmenu(); +} + +void VBA::updateMenuBar() +{ + if (flagHideMenu) + return; + + recreateMenuBar(); + + if (popup != NULL) + { + // force popup recreation if language changed + DestroyMenu(popup); + popup = NULL; + } +} + +void VBA::saveRewindStateIfNecessary() +{ + if (rewindSaveNeeded && rewindMemory && emulator.emuWriteMemState) + { + rewindCount++; + if (rewindCount > rewindSlots) + rewindCount = rewindSlots; + assert(rewindPos >= 0 && rewindPos < rewindSlots); + if (emulator.emuWriteMemState(&rewindMemory[rewindPos * REWIND_SIZE], REWIND_SIZE)) + { + rewindPos = ++rewindPos % rewindSlots; + assert(rewindPos >= 0 && rewindPos < rewindSlots); + if (rewindCount == rewindSlots) + rewindTopPos = ++rewindTopPos % rewindSlots; + } + } + + // also update/cache some frame search stuff + if (frameSearching) + { + extern SMovie Movie; + int curFrame = (Movie.state == MOVIE_STATE_NONE) ? systemCounters.frameCount : Movie.currentFrame; + int endFrame = theApp.frameSearchStart + theApp.frameSearchLength; + frameSearchSkipping = (curFrame < endFrame); + frameSearchFirstStep = false; + + if (curFrame == endFrame) + { + // cache intermediate state to speed up searching forward + emulator.emuWriteMemState(&frameSearchMemory[REWIND_SIZE * 1], REWIND_SIZE); + } + + if (curFrame == endFrame + 1) + { + emulator.emuWriteMemState(&frameSearchMemory[REWIND_SIZE * 2], REWIND_SIZE); + frameSearchLoadValid = true; + } + } + else + { + frameSearchFirstStep = false; + + assert(!frameSearchSkipping); + // just in case + frameSearchSkipping = false; + } +} + +BOOL VBA::OnIdle(LONG lCount) +{ + if (emulating && debugger) + { + MSG msg; + remoteStubMain(); + if (debugger) + return TRUE; // continue loop + return !::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE); + } + else if (emulating && active && !paused) + { +/// for(int i = 0; i < 2; i++) + { + emulator.emuMain(emulator.emuCount); + + // save the state for rewinding, if necessary + saveRewindStateIfNecessary(); + + rewindSaveNeeded = false; + } + + if (mouseCounter) + { + if (--mouseCounter == 0) + { + SetCursor(NULL); + } + } + return TRUE; + } + else if (emulating) // this fixes display if resetting while paused + { +// VBAUpdateButtonPressDisplay(); + VBAUpdateFrameCountDisplay(); + systemRefreshScreen(); + } + + return FALSE; + + // return CWinApp::OnIdle(lCount); +} + +void VBA::addRecentFile(const CString &file) +{ + // Do not change recent list if frozen + if (recentFreeze) + return; + int i = 0; + for (i = 0; i < 10; ++i) + { + if (recentFiles[i].GetLength() == 0) + break; + + if (recentFiles[i].Compare(file) == 0) + { + if (i == 0) + return; + CString p = recentFiles[i]; + for (int j = i; j > 0; --j) + { + recentFiles[j] = recentFiles[j - 1]; + } + recentFiles[0] = p; + return; + } + } + int num = 0; + for (i = 0; i < 10; ++i) + { + if (recentFiles[i].GetLength() != 0) + ++num; + } + if (num == 10) + { + --num; + } + + for (i = num; i >= 1; --i) + { + recentFiles[i] = recentFiles[i - 1]; + } + recentFiles[0] = file; +} + +void VBA::updateFrameSkip() +{ + switch (systemCartridgeType) + { + case 0: + systemFrameSkip = frameSkip; + break; + case 1: + systemFrameSkip = gbFrameSkip; + break; + } +} + +void VBA::updateVideoSize(UINT id) +{ + int value = 0; + bool forceUpdate = false; + + switch (id) + { + case ID_OPTIONS_VIDEO_X1: + value = VIDEO_1X; + forceUpdate = true; + break; + case ID_OPTIONS_VIDEO_X2: + value = VIDEO_2X; + forceUpdate = true; + break; + case ID_OPTIONS_VIDEO_X3: + value = VIDEO_3X; + forceUpdate = true; + break; + case ID_OPTIONS_VIDEO_X4: + value = VIDEO_4X; + forceUpdate = true; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN320X240: + value = VIDEO_320x240; + fsWidth = 320; + fsHeight = 240; + fsColorDepth = 16; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN640X480: + value = VIDEO_640x480; + fsWidth = 640; + fsHeight = 480; + fsColorDepth = 16; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN800X600: + value = VIDEO_800x600; + fsWidth = 800; + fsHeight = 600; + fsColorDepth = 16; + break; + case ID_OPTIONS_VIDEO_FULLSCREEN: + value = VIDEO_OTHER; + forceUpdate = true; + break; + } + + if (videoOption != value || forceUpdate) + updateWindowSize(value); +} + +void VBA::updateWindowSize(int value) +{ + regSetDwordValue("video", value); + + if (value == VIDEO_OTHER) + { + regSetDwordValue("fsWidth", fsWidth); + regSetDwordValue("fsHeight", fsHeight); + regSetDwordValue("fsColorDepth", fsColorDepth); + } + + if (display && + (((value >= VIDEO_320x240 || videoOption >= VIDEO_320x240) && videoOption != value) || + fsForceChange)) + { + fsForceChange = false; + videoOption = value; + initDisplay(); + } + + videoOption = value; + + if (systemCartridgeType == 1) + { + if (gbBorderOn) + { + sizeX = 256; + sizeY = 224; + gbBorderLineSkip = 256; + gbBorderColumnSkip = 48; + gbBorderRowSkip = 40; + } + else + { + sizeX = 160; + sizeY = 144; + gbBorderLineSkip = 160; + gbBorderColumnSkip = 0; + gbBorderRowSkip = 0; + } + } + else + { + sizeX = 240; + sizeY = 160; + } + + switch (videoOption) + { + case VIDEO_1X: + surfaceSizeX = sizeX; + surfaceSizeY = sizeY; + break; + case VIDEO_2X: + surfaceSizeX = sizeX * 2; + surfaceSizeY = sizeY * 2; + break; + case VIDEO_3X: + surfaceSizeX = sizeX * 3; + surfaceSizeY = sizeY * 3; + break; + case VIDEO_4X: + surfaceSizeX = sizeX * 4; + surfaceSizeY = sizeY * 4; + break; + case VIDEO_320x240: + case VIDEO_640x480: + case VIDEO_800x600: + case VIDEO_OTHER: + // Need to fix this code later. For now, Fullscreen takes the whole screen. + if (fullScreenStretch) + { + surfaceSizeX = fsWidth; + surfaceSizeY = fsHeight; + } + else + { + double scaleX = (double)fsWidth / (double)sizeX; + double scaleY = (double)fsHeight / (double)sizeY; + double scaleMin = scaleX < scaleY ? scaleX : scaleY; + if (fsMaxScale) + scaleMin = scaleMin > fsMaxScale ? fsMaxScale : scaleMin; + surfaceSizeX = (int)(scaleMin * sizeX); + surfaceSizeY = (int)(scaleMin * sizeY); + } + break; + } + + rect.left = 0; + rect.top = 0; + rect.right = sizeX; + rect.bottom = sizeY; + + int winSizeX = 0; + int winSizeY = 0; + int x = 0; + int y = 0; + + DWORD style = WS_POPUP | WS_VISIBLE; + DWORD styleEx = alwaysOnTop ? WS_EX_TOPMOST : 0; + + if (videoOption <= VIDEO_4X) + { + style |= WS_OVERLAPPEDWINDOW; + + dest.left = 0; + dest.top = 0; + dest.right = surfaceSizeX; + dest.bottom = surfaceSizeY; + + x = windowPositionX; + y = windowPositionY; + } + else + { + dest.left = 0; + dest.top = 0; + dest.right = fsWidth; + dest.bottom = fsHeight; + } + + AdjustWindowRectEx(&dest, style, flagHideMenu ? FALSE : TRUE, styleEx); + winSizeX = dest.right - dest.left; + winSizeY = dest.bottom - dest.top; + + if (m_pMainWnd == NULL) + { + // Create a new window + m_pMainWnd = new MainWnd; + m_pMainWnd->CreateEx(styleEx, + theApp.wndClass, + VBA_NAME_AND_VERSION, + style, + x, y, winSizeX, winSizeY, + NULL, + 0); + + if (!(HWND)*m_pMainWnd) + { + winlog("Error creating Window %08x\n", GetLastError()); + AfxPostQuitMessage(0); + return; + } + } + else + { + m_pMainWnd->SetWindowPos(0, //HWND_TOPMOST, + x, + y, + winSizeX, + winSizeY, + SWP_NOMOVE | SWP_SHOWWINDOW); + } + + updateMenuBar(); // add menubar first of all, or winGetMenuBarHeight() will get random height. + winAccelMgr.UpdateMenu(menu); + adjustDestRect(); + + updateIFB(); + updateFilter(); + + m_pMainWnd->RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN); +} + +bool VBA::initDisplay() +{ + if (display) + { + changingVideoSize = true; + shutdownDisplay(); + if (input) + { + delete input; + input = NULL; + } + CWnd *pWnd = m_pMainWnd; + + m_pMainWnd = NULL; + pWnd->DragAcceptFiles(FALSE); + pWnd->DestroyWindow(); + delete pWnd; + + display = NULL; + } + + if (display == NULL) + { + updateWindowSize(videoOption); + + switch (renderMethod) + { + case GDI: + display = newGDIDisplay(); + break; + case DIRECT_DRAW: + display = newDirectDrawDisplay(); + break; + case DIRECT_3D: + display = newDirect3DDisplay(); + break; + case OPENGL: + display = newOpenGLDisplay(); + break; + } + + if (display->initialize()) + { + if (input == NULL) + { + if (!initInput()) + { + changingVideoSize = false; + AfxPostQuitMessage(0); + return false; + } + } + + input->checkKeys(); + + changingVideoSize = false; + } + else + { + if (videoOption == VIDEO_320x240 || + videoOption == VIDEO_640x480 || + videoOption == VIDEO_800x600 || + videoOption == VIDEO_OTHER) + { + regSetDwordValue("video", VIDEO_1X); + if (pVideoDriverGUID) + regSetDwordValue("defaultVideoDriver", TRUE); + } + changingVideoSize = false; + return false; + } + } + changingVideoSize = false; + return true; +} + +bool VBA::updateRenderMethod(bool force) +{ + bool res = true; + if (force || (display && display->getType() != renderMethod)) + { + res = initDisplay(); + + while (!res && renderMethod > 0) + { + if (renderMethod == OPENGL) + renderMethod = DIRECT_3D; + else if (renderMethod == DIRECT_3D) + renderMethod = DIRECT_DRAW; + else if (renderMethod == DIRECT_DRAW) + renderMethod = GDI; + + res = initDisplay(); + } + } + + updateIFB(); + updateFilter(); + + m_pMainWnd->RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN); + + regSetDwordValue("renderMethod", renderMethod); + + return res; +} + +void VBA::winCheckFullscreen() +{ + if (videoOption > VIDEO_4X && tripleBuffering) + { + if (display) + display->checkFullScreen(); + } +} + +void VBA::shutdownDisplay() +{ + if (display != NULL) + { + display->cleanup(); + delete display; + display = NULL; + } +} + +void VBA::updatePriority() +{ + switch (threadPriority) + { + case 0: + SetThreadPriority(THREAD_PRIORITY_HIGHEST); + break; + case 1: + SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL); + break; + case 3: + SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL); + break; + default: + SetThreadPriority(THREAD_PRIORITY_NORMAL); + } +} + +#ifdef MMX +bool VBA::detectMMX() +{ + bool support = false; + char brand[13]; + + // check for Intel chip + __try { + __asm { + mov eax, 0; + cpuid; + mov [dword ptr brand + 0], ebx; + mov [dword ptr brand + 4], edx; + mov [dword ptr brand + 8], ecx; + } + } + __except(EXCEPTION_EXECUTE_HANDLER) { + if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION) + { + return false; + } + return false; + } + // Check for Intel or AMD CPUs + if (strncmp(brand, "GenuineIntel", 12)) + { + if (strncmp(brand, "AuthenticAMD", 12)) + { + return false; + } + } + + __asm { + mov eax, 1; + cpuid; + test edx, 00800000h; + jz NotFound; + mov [support], 1; +NotFound: + } + return support; +} + +#endif + +void VBA::winSetLanguageOption(int option, bool force) +{ + if (((option == languageOption) && option != 2) && !force) + return; + switch (option) + { + case 0: + { + char lbuffer[10]; + + if (GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, + lbuffer, 10)) + { + HINSTANCE l = winLoadLanguage(lbuffer); + if (l == NULL) + { + LCID locIdBase = MAKELCID(MAKELANGID(PRIMARYLANGID(GetSystemDefaultLangID()), SUBLANG_NEUTRAL), SORT_DEFAULT); + if (GetLocaleInfo(locIdBase, LOCALE_SABBREVLANGNAME, + lbuffer, 10)) + { + l = winLoadLanguage(lbuffer); + if (l == NULL) + { + systemMessage(IDS_FAILED_TO_LOAD_LIBRARY, + "Failed to load library %s", + lbuffer); + return; + } + } + } + AfxSetResourceHandle(l); + if (languageModule != NULL) + /**/ ::FreeLibrary(languageModule); + languageModule = l; + } + else + { + systemMessage(IDS_FAILED_TO_GET_LOCINFO, + "Failed to get locale information"); + return; + } + break; + } + case 1: + if (languageModule != NULL) + /**/ ::FreeLibrary(languageModule); + languageModule = NULL; + AfxSetResourceHandle(AfxGetInstanceHandle()); + break; + case 2: + { + if (!force) + { + LangSelect dlg; + if (dlg.DoModal()) + { + HINSTANCE l = winLoadLanguage(languageName); + if (l == NULL) + { + systemMessage(IDS_FAILED_TO_LOAD_LIBRARY, + "Failed to load library %s", + languageName); + return; + } + AfxSetResourceHandle(l); + if (languageModule != NULL) + /**/ ::FreeLibrary(languageModule); + languageModule = l; + } + } + else + { + if (languageName.IsEmpty()) + return; + HINSTANCE l = winLoadLanguage(languageName); + if (l == NULL) + { + systemMessage(IDS_FAILED_TO_LOAD_LIBRARY, + "Failed to load library %s", + languageName); + return; + } + AfxSetResourceHandle(l); + if (languageModule != NULL) + FreeLibrary(languageModule); + languageModule = l; + } + break; + } + } + languageOption = option; + updateMenuBar(); + theApp.winAccelMgr.UpdateMenu(theApp.menu); +} + +HINSTANCE VBA::winLoadLanguage(const char *name) +{ + CString buffer; + + buffer.Format("vba_%s.dll", name); + + HINSTANCE l = /**/ ::LoadLibrary(buffer); + + if (l == NULL) + { + if (strlen(name) == 3) + { + char buffer2[3]; + buffer2[0] = name[0]; + buffer2[1] = name[1]; + buffer2[2] = 0; + buffer.Format("vba_%s.dll", buffer2); + + return /**/ ::LoadLibrary(buffer); + } + } + return l; +} + +bool VBA::initInput() +{ + if (input) + delete input; + input = newDirectInput(); + if (input->initialize()) + { + input->loadSettings(); + input->checkKeys(); + return true; + } + delete input; + return false; +} + +void VBA::winAddUpdateListener(IUpdateListener *l) +{ + updateList.AddTail(l); + updateCount++; +} + +void VBA::winRemoveUpdateListener(IUpdateListener *l) +{ + POSITION pos = updateList.Find(l); + if (pos) + { + updateList.RemoveAt(pos); + updateCount--; + if (updateCount < 0) + updateCount = 0; + } +} + +void VBA::loadSettings() +{ + CString buffer; + // video + bool defaultVideoDriver = regQueryDwordValue("defaultVideoDriver", true) ? true : false; + if (!regQueryBinaryValue("videoDriverGUID", (char *)&videoDriverGUID, sizeof(GUID))) + { + defaultVideoDriver = TRUE; + } + if (defaultVideoDriver) + pVideoDriverGUID = NULL; + else + pVideoDriverGUID = &videoDriverGUID; + + videoOption = regQueryDwordValue("video", 0); + if (videoOption < 0 || videoOption > VIDEO_OTHER) + videoOption = 0; + switch (videoOption) + { + case VIDEO_320x240: + fsWidth = 320; + fsHeight = 240; + fsColorDepth = 16; + break; + case VIDEO_640x480: + fsWidth = 640; + fsHeight = 480; + fsColorDepth = 16; + break; + case VIDEO_800x600: + fsWidth = 800; + fsHeight = 600; + fsColorDepth = 16; + break; + } + if (videoOption == VIDEO_OTHER) + { + if (fsWidth < 0 || fsWidth > 4095 || fsHeight < 0 || fsHeight > 4095) + videoOption = 0; + if (fsColorDepth != 16 && fsColorDepth != 24 && fsColorDepth != 32) + videoOption = 0; + } + + fsWidth = regQueryDwordValue("fsWidth", 0); + fsHeight = regQueryDwordValue("fsHeight", 0); + fsColorDepth = regQueryDwordValue("fsColorDepth", 0); + fsMaxScale = regQueryDwordValue("fsMaxScale", 0); + fullScreenStretch = regQueryDwordValue("stretch", 0) ? true : false; + + renderMethod = (DISPLAY_TYPE)regQueryDwordValue("renderMethod", DIRECT_DRAW); + if (renderMethod < GDI || renderMethod > OPENGL) + renderMethod = DIRECT_DRAW; + + ddrawEmulationOnly = regQueryDwordValue("ddrawEmulationOnly", false) ? true : false; + ddrawUseVideoMemory = regQueryDwordValue("ddrawUseVideoMemory", false) ? true : false; + tripleBuffering = regQueryDwordValue("tripleBuffering", true) ? true : false; + vsync = regQueryDwordValue("vsync", false) ? true : false; + + d3dFilter = regQueryDwordValue("d3dFilter", 0); + if (d3dFilter < 0 || d3dFilter > 1) + d3dFilter = 0; + glFilter = regQueryDwordValue("glFilter", 0); + if (glFilter < 0 || glFilter > 1) + glFilter = 0; + glType = regQueryDwordValue("glType", 0); + if (glType < 0 || glType > 1) + glType = 0; + + // pixel filter & ifb + filterType = regQueryDwordValue("filter", 0); + if (filterType < 0 || filterType > 20) + filterType = 0; + disableMMX = regQueryDwordValue("disableMMX", 0) ? true : false; + ifbType = regQueryDwordValue("ifbType", 0); + if (ifbType < 0 || ifbType > 2) + ifbType = 0; + + // frame skipping + frameSkip = regQueryDwordValue("frameSkip", /*2*/ 0); + if (frameSkip < 0 || frameSkip > 9) + frameSkip = 1; + gbFrameSkip = regQueryDwordValue("gbFrameSkip", 0); + if (gbFrameSkip < 0 || gbFrameSkip > 9) + gbFrameSkip = 0; +/// autoFrameSkip = regQueryDwordValue("autoFrameSkip", FALSE) ? TRUE : FALSE; + + // input + joypadDefault = regQueryDwordValue("joypadDefault", 0); + if (joypadDefault < 0 || joypadDefault > 3) + joypadDefault = 0; + allowLeftRight = regQueryDwordValue("allowLeftRight", false) ? true : false; + autofireAccountForLag = regQueryDwordValue("autofireAccountForLag", false) ? true : false; + nextframeAccountForLag = regQueryDwordValue("nextframeAccountForLag", false) ? true : false; + theApp.AsscWithSaveState = regQueryDwordValue("AsscWithSaveState", false) ? true : false; + + // speed + throttle = regQueryDwordValue("throttle", 0); + if (throttle < 5 || throttle > 1000) + throttle = 100; + + synchronize = regQueryDwordValue("synchronize", 1) ? true : false; + accuratePitchThrottle = regQueryDwordValue("accuratePitchThrottle", FALSE) ? TRUE : FALSE; + + // sound + int resChannels = regQueryDwordValue("soundEnable", 0x30f); + soundEnableChannels(resChannels); + soundDisableChannels(~resChannels); + soundOffFlag = (regQueryDwordValue("soundOff", 0)) ? true : false; + soundQuality = regQueryDwordValue("soundQuality", 2); + soundEcho = regQueryDwordValue("soundEcho", 0) ? true : false; + soundLowPass = regQueryDwordValue("soundLowPass", 0) ? true : false; + soundReverse = regQueryDwordValue("soundReverse", 0) ? true : false; + soundVolume = regQueryDwordValue("soundVolume", 0); + if (soundVolume < 0 || soundVolume > 5) + soundVolume = 0; + muteFrameAdvance = regQueryDwordValue("muteFrameAdvance", 0) ? TRUE : FALSE; + muteWhenInactive = regQueryDwordValue("muteWhenInactive", 0) ? TRUE : FALSE; + + // emulation + memLagEnabled = regQueryDwordValue("memLagEnabled", false) ? true : false; + memLagTempEnabled = memLagEnabled; + gbNullInputHackEnabled = regQueryDwordValue("gbNullInputHackEnabled", false) ? true : false; + gbNullInputHackTempEnabled = gbNullInputHackEnabled; + useOldSync = regQueryDwordValue("useOldSync", 0) ? TRUE : FALSE; + useOldFrameTiming = regQueryDwordValue("useOldGBTiming", false) ? true : false; + + useBiosFile = regQueryDwordValue("useBios", 0) ? true : false; + skipBiosFile = regQueryDwordValue("skipBios", 0) ? true : false; + buffer = regQueryStringValue("biosFile", ""); + if (!buffer.IsEmpty()) + { + biosFileName = buffer; + } +// removeIntros = regQueryDwordValue("removeIntros", false) ? true : false; + + autoIPS = regQueryDwordValue("autoIPS", true) ? true : false; + + agbPrintEnable(regQueryDwordValue("agbPrint", 0) ? true : false); + winRtcEnable = regQueryDwordValue("rtcEnabled", 0) ? true : false; + rtcEnable(winRtcEnable); + + winSaveType = regQueryDwordValue("saveType", 0); + if (winSaveType < 0 || winSaveType > 5) + winSaveType = 0; + cpuEnhancedDetection = regQueryDwordValue("enhancedDetection", 1) ? true : false; + winFlashSize = regQueryDwordValue("flashSize", 0x10000); + if (winFlashSize != 0x10000 && winFlashSize != 0x20000) + winFlashSize = 0x10000; + + cpuDisableSfx = regQueryDwordValue("disableSfx", 0) ? true : false; + + // GBx + winGbPrinterEnabled = regQueryDwordValue("gbPrinter", false) ? true : false; + if (winGbPrinterEnabled) + gbSerialFunction = gbPrinterSend; + else + gbSerialFunction = NULL; + gbEmulatorType = regQueryDwordValue("emulatorType", 0); + if (gbEmulatorType < 0 || gbEmulatorType > 5) + gbEmulatorType = 1; + winGbBorderOn = regQueryDwordValue("borderOn", 0); + gbBorderAutomatic = regQueryDwordValue("borderAutomatic", 0); + + gbColorOption = regQueryDwordValue("colorOption", 0); + gbPaletteOption = regQueryDwordValue("gbPaletteOption", 0); + if (gbPaletteOption < 0) + gbPaletteOption = 0; + if (gbPaletteOption > 2) + gbPaletteOption = 2; + regQueryBinaryValue("gbPalette", (char *)systemGbPalette, 24 * sizeof(u16)); + + // head-up display + showSpeed = regQueryDwordValue("showSpeed", 1); + if (showSpeed < 0 || showSpeed > 2) + showSpeed = 1; + showSpeedTransparent = regQueryDwordValue("showSpeedTransparent", TRUE) ? TRUE : FALSE; + outlinedText = regQueryDwordValue("outlinedText", TRUE) != 0; + transparentText = regQueryDwordValue("transparentText", FALSE) != 0; + textColor = regQueryDwordValue("textColor", 0); + textMethod = regQueryDwordValue("textMethod", 1); + frameCounter = regQueryDwordValue("frameCounter", false) ? true : false; + lagCounter = regQueryDwordValue("lagCounter", false) ? true : false; + extraCounter = regQueryDwordValue("extraCounter", false) ? true : false; + inputDisplay = regQueryDwordValue("inputDisplay", false) ? true : false; + disableStatusMessage = regQueryDwordValue("disableStatus", 0) ? true : false; + + // UI + windowPositionX = regQueryDwordValue("windowX", 0); + if (windowPositionX < 0) + windowPositionX = 0; + windowPositionY = regQueryDwordValue("windowY", 0); + if (windowPositionY < 0) + windowPositionY = 0; + + autoHideMenu = regQueryDwordValue("autoHideMenu", 0) ? true : false; + + languageOption = regQueryDwordValue("language", 1); + if (languageOption < 0 || languageOption > 2) + languageOption = 1; + buffer = regQueryStringValue("languageName", ""); + if (!buffer.IsEmpty()) + { + languageName = buffer.Left(3); + } + else + languageName = ""; + winSetLanguageOption(languageOption, true); + + // preferences + alwaysOnTop = regQueryDwordValue("alwaysOnTop", false) ? true : false; + pauseWhenInactive = regQueryDwordValue("pauseWhenInactive", 1) ? true : false; + enableBackgroundInput = regQueryDwordValue("enableBackgroundInput", 0) ? true : false; + threadPriority = regQueryDwordValue("priority", 2); + if (threadPriority < 0 || threadPriority > 3) + threadPriority = 2; + updatePriority(); + + filenamePreference = regQueryDwordValue("filenamePreference", 0); + altAviRecordMethod = regQueryDwordValue("altAviRecordMethod", false) ? true : false; + captureFormat = regQueryDwordValue("captureFormat", 0); + + rewindTimer = regQueryDwordValue("rewindTimer", 0); + rewindSlots = regQueryDwordValue("rewindSlots", 64); + if (rewindTimer < 0 || rewindTimer > 600) + rewindTimer = 0; + if (rewindSlots <= 0) + rewindTimer = rewindSlots = 0; + if (rewindSlots > MAX_REWIND_SLOTS) + rewindSlots = MAX_REWIND_SLOTS; + if (rewindTimer != 0) + { + if (rewindMemory == NULL) + rewindMemory = (char *)malloc(rewindSlots * REWIND_SIZE); + } + + if (frameSearchMemory == NULL) + frameSearchMemory = (char *)malloc(3 * REWIND_SIZE); + + recentFreeze = regQueryDwordValue("recentFreeze", false) ? true : false; + for (int i = 0, j = 0; i < 10; ++i) + { + buffer.Format("recent%d", i); + const char *s = regQueryStringValue(buffer, NULL); + if (s == NULL) + continue; + recentFiles[j] = s; + ++j; + } + + autoLoadMostRecent = regQueryDwordValue("autoLoadMostRecent", false) ? true : false; + loadMakesRecent = regQueryDwordValue("loadMakesRecent", false) ? true : false; + loadMakesCurrent = regQueryDwordValue("loadMakesCurrent", false) ? true : false; + saveMakesCurrent = regQueryDwordValue("saveMakesCurrent", false) ? true : false; + currentSlot = regQueryDwordValue("currentSlot", 0); + showSlotTime = regQueryDwordValue("showSlotTime", 0) ? true : false; + + cheatsEnabled = regQueryDwordValue("cheatsEnabled", true) ? true : false; + autoSaveLoadCheatList = regQueryDwordValue("autoSaveCheatList", 0) ? true : false; + pauseDuringCheatSearch = regQueryDwordValue("pauseDuringCheatSearch2", 0) ? true : false; + + movieOnEndBehavior = regQueryDwordValue("movieOnEndBehavior", 0); + movieOnEndPause = regQueryDwordValue("movieOnEndPause", 0) ? true : false; + + extern bool autoConvertMovieWhenPlaying; // from movie.cpp + autoConvertMovieWhenPlaying = regQueryDwordValue("autoConvertMovieWhenPlaying", 0) ? true : false; + + // RamWatch Settings + AutoRWLoad = regQueryDwordValue(AUTORWLOAD, false); + RWSaveWindowPos = regQueryDwordValue(RWSAVEPOS, false); + ramw_x = regQueryDwordValue(RAMWX, 0); + ramw_y = regQueryDwordValue(RAMWY, 0); + + // this is FILO + for (int i = MAX_RECENT_WATCHES; i > 0; --i) + { + buffer.Format("recentWatch%d", i); + const char *s = regQueryStringValue(buffer, NULL); + if (s == NULL) + continue; + RWAddRecentFile(s); + } +} + +void VBA::saveSettings() +{ + regSetDwordValue("defaultVideoDriver", pVideoDriverGUID == NULL); + if (pVideoDriverGUID) + { + regSetBinaryValue("videoDriverGUID", (char *)&videoDriverGUID, + sizeof(GUID)); + } + regSetDwordValue("video", videoOption); + + regSetDwordValue("fsWidth", fsWidth); + regSetDwordValue("fsHeight", fsHeight); + regSetDwordValue("fsColorDepth", fsColorDepth); + regSetDwordValue("fsMaxScale", fsMaxScale); + + regSetDwordValue("stretch", fullScreenStretch); + + regSetDwordValue("renderMethod", renderMethod); + + regSetDwordValue("ddrawEmulationOnly", ddrawEmulationOnly); + regSetDwordValue("ddrawUseVideoMemory", ddrawUseVideoMemory); + regSetDwordValue("tripleBuffering", tripleBuffering); + regSetDwordValue("vsync", vsync); + + regSetDwordValue("d3dFilter", d3dFilter); + regSetDwordValue("glFilter", glFilter); + regSetDwordValue("glType", glType); + + // pixel filter & ifb + regSetDwordValue("filter", filterType); + regSetDwordValue("ifbType", ifbType); + regSetDwordValue("disableMMX", disableMMX); + + // frame skipping + regSetDwordValue("frameSkip", frameSkip); + regSetDwordValue("gbFrameSkip", gbFrameSkip); +/// regSetDwordValue("autoFrameSkip", autoFrameSkip); + + // input + regSetDwordValue("joypadDefault", joypadDefault); + regSetDwordValue("allowLeftRight", allowLeftRight); + regSetDwordValue("autofireAccountforLag", autofireAccountForLag); + regSetDwordValue("nextframeAccountforLag", nextframeAccountForLag); + regSetDwordValue("AsscWithSaveState", theApp.AsscWithSaveState); + + + // speed + regSetDwordValue("throttle", throttle); + regSetDwordValue("synchronize", synchronize); + regSetDwordValue("accuratePitchThrottle", accuratePitchThrottle); + + // sound + regSetDwordValue("soundEnable", soundGetEnabledChannels() & 0x030f); + regSetDwordValue("soundOff", soundOffFlag); + regSetDwordValue("soundQuality", soundQuality); + regSetDwordValue("soundEcho", soundEcho); + regSetDwordValue("soundLowPass", soundLowPass); + regSetDwordValue("soundReverse", soundReverse); + regSetDwordValue("soundVolume", soundVolume); + regSetDwordValue("muteFrameAdvance", muteFrameAdvance); + regSetDwordValue("muteWhenInactive", muteWhenInactive); + + // emulation + regSetDwordValue("useBios", useBiosFile); + regSetDwordValue("skipBios", skipBiosFile); + if (!biosFileName.IsEmpty()) + regSetStringValue("biosFile", biosFileName); +// regSetDwordValue("removeIntros", removeIntros); + + regSetDwordValue("autoIPS", autoIPS); + + regSetDwordValue("memLagEnabled", memLagEnabled); + regSetDwordValue("gbNullInputHackEnabled", gbNullInputHackEnabled); + regSetDwordValue("useOldGBTiming", useOldFrameTiming); + regSetDwordValue("useOldSync", useOldSync); + + regSetDwordValue("agbPrint", agbPrintIsEnabled()); + regSetDwordValue("rtcEnabled", winRtcEnable); + + regSetDwordValue("saveType", winSaveType); + regSetDwordValue("enhancedDetection", cpuEnhancedDetection); + regSetDwordValue("flashSize", winFlashSize); + + regSetDwordValue("disableSfx", cpuDisableSfx); + + // GBx + regSetDwordValue("emulatorType", gbEmulatorType); + regSetDwordValue("gbPrinter", winGbPrinterEnabled); + regSetDwordValue("borderOn", winGbBorderOn); + regSetDwordValue("borderAutomatic", gbBorderAutomatic); + + regSetDwordValue("colorOption", gbColorOption); + regSetDwordValue("gbPaletteOption", gbPaletteOption); + regSetBinaryValue("gbPalette", (char *)systemGbPalette, 24 * sizeof(u16)); + + // head-up display + regSetDwordValue("showSpeed", showSpeed); + regSetDwordValue("showSpeedTransparent", showSpeedTransparent); + + regSetDwordValue("outlinedText", outlinedText); + regSetDwordValue("transparentText", transparentText); + regSetDwordValue("textColor", textColor); + regSetDwordValue("textMethod", textMethod); + regSetDwordValue("frameCounter", frameCounter); + regSetDwordValue("lagCounter", lagCounter); + regSetDwordValue("extraCounter", extraCounter); + regSetDwordValue("inputDisplay", inputDisplay); + regSetDwordValue("disableStatus", disableStatusMessage); + + // UI + regSetDwordValue("windowX", windowPositionX); + regSetDwordValue("windowY", windowPositionY); + + regSetDwordValue("autoHideMenu", autoHideMenu); + + regSetDwordValue("language", languageOption); + regSetStringValue("languageName", languageName); + + // preferences + regSetDwordValue("alwaysOnTop", alwaysOnTop); + regSetDwordValue("pauseWhenInactive", pauseWhenInactive); + regSetDwordValue("enableBackgroundInput", enableBackgroundInput); + regSetDwordValue("priority", threadPriority); + + regSetDwordValue("filenamePreference", filenamePreference); + regSetDwordValue("altAviRecordMethod", altAviRecordMethod); + regSetDwordValue("captureFormat", captureFormat); + + regSetDwordValue("cheatsEnabled", cheatsEnabled); + regSetDwordValue("autoSaveCheatList", autoSaveLoadCheatList); + regSetDwordValue("pauseDuringCheatSearch2", pauseDuringCheatSearch); + + regSetDwordValue("rewindTimer", rewindTimer); + regSetDwordValue("rewindSlots", rewindSlots); + + regSetDwordValue("recentFreeze", recentFreeze); + CString buffer; + for (int i = 0; i < 10; i++) + { + buffer.Format("recent%d", i); + regSetStringValue(buffer, recentFiles[i]); + } + + regSetDwordValue("autoLoadMostRecent", autoLoadMostRecent); + regSetDwordValue("loadMakesRecent", loadMakesRecent); + regSetDwordValue("loadMakesCurrent", loadMakesCurrent); + regSetDwordValue("saveMakesCurrent", saveMakesCurrent); + regSetDwordValue("currentSlot", currentSlot); + regSetDwordValue("showSlotTime", showSlotTime); + + regSetDwordValue("movieOnEndBehavior", movieOnEndBehavior); + regSetDwordValue("movieOnEndPause", movieOnEndPause); + + extern bool autoConvertMovieWhenPlaying; // from movie.cpp + regSetDwordValue("autoConvertMovieWhenPlaying", autoConvertMovieWhenPlaying); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/VBA.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/VBA.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,274 @@ +#if !defined(AFX_VBA_H__57514A10_49F9_4B83_A928_0D8A4A7306A3__INCLUDED_) +#define AFX_VBA_H__57514A10_49F9_4B83_A928_0D8A4A7306A3__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +/* + #ifndef __AFXWIN_H__ + #error include 'stdafx.h' before including this file for PCH + #endif + */ + +#include + +#include "AcceleratorManager.h" +#include "AVIWrite.h" +#include "Display.h" +#include "../common/System.h" + +///////////////////////////////////////////////////////////////////////////// +// VBA: +// See VBA.cpp for the implementation of this class +// + +enum +{ + VIDEO_1X, VIDEO_2X, VIDEO_3X, VIDEO_4X, + VIDEO_320x240, VIDEO_640x480, VIDEO_800x600, VIDEO_OTHER +}; + +#define REWIND_SIZE 400000 +#define MAX_REWIND_SLOTS 256 +#define SCREEN_MESSAGE_SLOTS 8 + +///////////////////////////////////////////////////////////////////////////// +// forward decl +class IUpdateListener; +class Input; +class ISound; +class AVIWrite; +class WavWriter; + +class VBA : public CWinApp +{ +public: + CMenu m_menu; + HMENU menu; + HMENU popup; + bool mode320Available; + bool mode640Available; + bool mode800Available; + int windowPositionX; + int windowPositionY; + void (*filterFunction)(u8 *, u32, u8 *, u8 *, u32, int, int); + void (*ifbFunction)(u8 *, u32, int, int); + int ifbType; + int filterType; + int filterWidth; + int filterHeight; + int fsWidth; + int fsHeight; + int fsColorDepth; + bool fsForceChange; + bool AsscWithSaveState; + int sizeX; + int sizeY; + int surfaceSizeX; + int surfaceSizeY; + int videoOption; + bool fullScreenStretch; + bool disableStatusMessage; + int showSpeed; + BOOL showSpeedTransparent; + int showRenderedFrames; + bool screenMessage [SCREEN_MESSAGE_SLOTS]; + CString screenMessageBuffer [SCREEN_MESSAGE_SLOTS]; + DWORD screenMessageTime [SCREEN_MESSAGE_SLOTS]; + int screenMessageDuration [SCREEN_MESSAGE_SLOTS]; + CString screenMessageColorBuffer [SCREEN_MESSAGE_SLOTS]; + u8 * delta[257 * 244 * 4]; + bool menuToggle; + IDisplay *display; + bool soundInitialized; + bool useBiosFile; + bool skipBiosFile; + CString biosFileName; + bool allowLeftRight; + bool autofireAccountForLag; + bool nextframeAccountForLag; + bool active; + bool iconic; + bool paused; + CString recentFiles[10]; + bool recentFreeze; + bool autoSaveLoadCheatList; + bool pauseDuringCheatSearch; + bool modelessCheatDialogIsOpen; +// FILE * winout; +// bool removeIntros; + bool autoIPS; + int winGbBorderOn; + bool hideMovieBorder; + int winFlashSize; + bool winRtcEnable; + int winSaveType; + char *rewindMemory; + int rewindPos; + int rewindTopPos; + int rewindCounter; + int rewindCount; + bool rewindSaveNeeded; + int rewindTimer; + int rewindSlots; + int captureFormat; + bool tripleBuffering; + bool autoHideMenu; + bool speedupToggle; + int throttle; + u32 throttleLastTime; +/// u32 autoFrameSkipLastTime; +/// bool autoFrameSkip; + bool accuratePitchThrottle; + bool vsync; + bool changingVideoSize; + GUID videoDriverGUID; + GUID * pVideoDriverGUID; + DISPLAY_TYPE renderMethod; + bool ddrawEmulationOnly; + bool ddrawUsingEmulationOnly; + bool ddrawDebug; + bool ddrawUseVideoMemory; + int d3dFilter; + int glFilter; + int glType; + bool muteWhenInactive; + bool muteFrameAdvance; + bool pauseWhenInactive; + bool enableBackgroundInput; + bool alwaysOnTop; + bool useOldSync; + bool winGbPrinterEnabled; + int threadPriority; + bool disableMMX; + int languageOption; + CString languageName; + HINSTANCE languageModule; + int renderedFrames; + Input * input; + int joypadDefault; + int autoFire, autoFire2; + int autoHold; + bool autoFireToggle; + bool frameCounter; + bool lagCounter; + bool extraCounter; + bool inputDisplay; + bool movieReadOnly; + bool movieOnEndPause; + int movieOnEndBehavior; + bool soundRecording; + WavWriter * soundRecorder; + CString soundRecordName; + ISound * sound; + bool aviRecording; + AVIWrite * aviRecorder; + CString aviRecordName; + bool altAviRecordMethod; + bool nvVideoLog; + bool nvAudioLog; + bool painting; // for systemDrawScreen() + int mouseCounter; + bool winMuteForNow; + bool winPauseNextFrame; + bool wasPaused; + int fsMaxScale; + int romSize; + bool autoLoadMostRecent; + bool loadMakesRecent; + bool loadMakesCurrent; + bool saveMakesCurrent; + int currentSlot; + bool showSlotTime; + int filenamePreference; + int LuaFastForward; + bool frameSearching; + bool frameSearchSkipping; + bool frameSearchFirstStep; + bool frameSearchLoadValid; + int frameSearchLength; + int frameSearchStart; + u32 frameSearchOldInput[4]; + char * frameSearchMemory; + DWORD wmTimerRes; + + CList updateList; + int updateCount; + + CAcceleratorManager winAccelMgr; + HACCEL hAccel; + + RECT rect; + RECT dest; + + struct EmulatedSystem &emulator; + + CString romFilename; + CString gameFilename; + CString exeName; + CString exeDir; + CString wndClass; + +public: + VBA(); + ~VBA(); + + void adjustDestRect(); + void recreateMenuBar(); + void updateIFB(); + void updateFilter(); + void updateMenuBar(); + void winAddUpdateListener(IUpdateListener *l); + void winRemoveUpdateListener(IUpdateListener *l); + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(VBA) +public: + virtual BOOL InitInstance(); + virtual BOOL OnIdle(LONG lCount); + //}}AFX_VIRTUAL + + // Implementation +public: + HINSTANCE winLoadLanguage(const char *name); + void winSetLanguageOption(int option, bool force); +#ifdef MMX + bool detectMMX(); +#endif + void updatePriority(); + void shutdownDisplay(); + void winCheckFullscreen(); + bool updateRenderMethod(bool force); + bool initDisplay(); + void updateWindowSize(int value); + void updateVideoSize(UINT id); + void updateFrameSkip(); + void loadSettings(); + void saveSettings(); + bool initInput(); + void addRecentFile(const CString &file); + void saveRewindStateIfNecessary(); + //{{AFX_MSG(VBA) + afx_msg void OnAppAbout(); + // NOTE - the ClassWizard will add and remove member functions here. + // DO NOT EDIT what you see in these blocks of generated code ! + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; + +extern VBA theApp; + +#ifdef MMX +extern "C" bool cpu_mmx; +#endif + +extern void DrawTextMessages(u8 *dest, int pitch, int left, int bottom); + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_VBA_H__57514A10_49F9_4B83_A928_0D8A4A7306A3__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/VersionInfo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/VersionInfo.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,33 @@ +#ifndef VBA_VERSIONINFO_H +#define VBA_VERSIONINFO_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include "../version.h" + +#if SVN_REV > 65535 +# define VBA_RR_BUILD_NO 0 +#else +# define VBA_RR_BUILD_NO SVN_REV +#endif + +// diaplayed in the file attribute dialog +#define VBA_VERSIONINFO_FILEVER 7, VBA_RR_MAJOR_VERSION_NO, VBA_RR_MINOR_VERSION_NO, VBA_RR_BUILD_NO +#define VBA_VERSIONINFO_STRFILEVER STRINGIZE_VALUE(VBA_VERSIONINFO_FILEVER) "\0" + +#define VBA_VERSIONINFO_PRODUCTVER VBA_VERSIONINFO_FILEVER +#define VBA_VERSIONINFO_STRPRODUCTVER VBA_VERSIONINFO_STRFILEVER +#define VBA_VERSIONINFO_STRCOPYRIGHT "Copyright (C) 2005-2011 VBA-RR Development Team" +#define VBA_VERSIONINFO_ORGANIZATION VBA_RR_SITE + +#ifdef _DEBUG +# define VBA_VERSIONINFO_FILEFLAGS VS_FF_DEBUG +#elif defined(PUBLIC_RELEASE) +# define VBA_VERSIONINFO_FILEFLAGS 0 +#else +# define VBA_VERSIONINFO_FILEFLAGS VS_FF_PRERELEASE +#endif + +#endif // VBA_VERSIONINFO_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/VideoMode.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/VideoMode.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,347 @@ +// VideoMode.cpp : implementation file +// + +#include "stdafx.h" +#include "resource.h" + +/// #define _AFXDLL /// EVIL +/// #include "afxwin.h" /// EVIL +/// #include "afxdll_.h" /// EVIL + +#define DIRECTDRAW_VERSION 0x0700 +#include "ddraw.h" + +#include "VideoMode.h" + +#include "../common/System.h" // for system messages + +#define MAX_DRIVERS 32 // 32 drivers maximum + +//----------------------------------------------------------------------------- +// Local structures +//----------------------------------------------------------------------------- +// Keeps data on the available DDraw drivers +struct +{ + char szDescription[128]; + char szName[128]; + GUID * pGUID; + GUID GUIDcopy; + HMONITOR hm; +} Drivers[MAX_DRIVERS]; + +//----------------------------------------------------------------------------- +// Local data +//----------------------------------------------------------------------------- +static int gDriverCnt = 0; // Total number of drivers +static GUID *gpSelectedDriverGUID; + +//----------------------------------------------------------------------------- +// Name: DDEnumCallbackEx() +// Desc: This call back is used to determine the existing available DDraw +// devices, so the user can pick which one to run on. +//----------------------------------------------------------------------------- +BOOL WINAPI +DDEnumCallbackEx(GUID *pGUID, LPSTR pDescription, LPSTR pName, LPVOID pContext, HMONITOR hm) +{ + if (pGUID) + { + Drivers[gDriverCnt].GUIDcopy = *pGUID; + Drivers[gDriverCnt].pGUID = &Drivers[gDriverCnt].GUIDcopy; + } + else + Drivers[gDriverCnt].pGUID = NULL; + Drivers[gDriverCnt].szDescription[127] = '\0'; + Drivers[gDriverCnt].szName[127] = '\0'; + strncpy(Drivers[gDriverCnt].szDescription, pDescription, 127); + strncpy(Drivers[gDriverCnt].szName, pName, 127); + Drivers[gDriverCnt].hm = hm; + if (gDriverCnt < MAX_DRIVERS) + gDriverCnt++; + else + return DDENUMRET_CANCEL; + return DDENUMRET_OK; +} + +//----------------------------------------------------------------------------- +// Name: DDEnumCallback() +// Desc: This callback is used only with old versions of DDraw. +//----------------------------------------------------------------------------- +BOOL WINAPI +DDEnumCallback(GUID *pGUID, LPSTR pDescription, LPSTR pName, LPVOID context) +{ + return (DDEnumCallbackEx(pGUID, pDescription, pName, context, NULL)); +} + +static HRESULT WINAPI addVideoMode(LPDDSURFACEDESC2 surf, LPVOID lpContext) +{ + HWND h = (HWND)lpContext; + char buffer[50]; + + switch (surf->ddpfPixelFormat.dwRGBBitCount) + { + case 16: + case 24: + case 32: + if (surf->dwWidth >= 640 && surf->dwHeight >= 480) + { + sprintf(buffer, "%4dx%4dx%2d", surf->dwWidth, surf->dwHeight, + surf->ddpfPixelFormat.dwRGBBitCount); + int pos = ::SendMessage(h, LB_ADDSTRING, 0, (LPARAM)buffer); + ::SendMessage(h, LB_SETITEMDATA, pos, + (surf->ddpfPixelFormat.dwRGBBitCount << 24) | + ((surf->dwWidth & 4095) << 12) | + (surf->dwHeight & 4095)); + } + } + + return DDENUMRET_OK; +} + +int winVideoModeSelect(CWnd *pWnd, GUID **guid) +{ + HINSTANCE h = /**/ ::LoadLibrary("ddraw.dll"); + + // If ddraw.dll doesn't exist in the search path, + // then DirectX probably isn't installed, so fail. + if (!h) + return -1; + + gDriverCnt = 0; + + // Note that you must know which version of the + // function to retrieve (see the following text). + // For this example, we use the ANSI version. + LPDIRECTDRAWENUMERATEEX lpDDEnumEx; + lpDDEnumEx = (LPDIRECTDRAWENUMERATEEX) + GetProcAddress(h, "DirectDrawEnumerateExA"); + + // If the function is there, call it to enumerate all display + // devices attached to the desktop, and any non-display DirectDraw + // devices. + if (lpDDEnumEx) + lpDDEnumEx(DDEnumCallbackEx, NULL, + DDENUM_ATTACHEDSECONDARYDEVICES | + DDENUM_NONDISPLAYDEVICES + ); + else + { + /* + * We must be running on an old version of DirectDraw. + * Therefore MultiMon isn't supported. Fall back on + * DirectDrawEnumerate to enumerate standard devices on a + * single-monitor system. + */ + BOOL (WINAPI *lpDDEnum)(LPDDENUMCALLBACK, LPVOID); + + lpDDEnum = (BOOL (WINAPI *)(LPDDENUMCALLBACK, LPVOID)) + GetProcAddress(h, "DirectDrawEnumerateA"); + if (lpDDEnum) + lpDDEnum(DDEnumCallback, NULL); + + /* Note that it could be handy to let the OldCallback function + * be a wrapper for a DDEnumCallbackEx. + * + * Such a function would look like: + * BOOL FAR PASCAL OldCallback(GUID FAR *lpGUID, + * LPSTR pDesc, + * LPSTR pName, + * LPVOID pContext) + * { + * return Callback(lpGUID,pDesc,pName,pContext,NULL); + * } + */ + } + + int selected = 0; + + if (gDriverCnt > 1) + { + VideoDriverSelect d(pWnd); + + selected = d.DoModal(); + + if (selected == -1) + { + // If the library was loaded by calling LoadLibrary(), + // then you must use FreeLibrary() to let go of it. + /**/ ::FreeLibrary(h); + + return -1; + } + } + + HRESULT (WINAPI *DDrawCreateEx)(GUID *, LPVOID *, REFIID, IUnknown *); + DDrawCreateEx = (HRESULT (WINAPI *)(GUID *, LPVOID *, REFIID, IUnknown *)) + GetProcAddress(h, "DirectDrawCreateEx"); + + LPDIRECTDRAW7 ddraw = NULL; + if (DDrawCreateEx) + { + HRESULT hret = DDrawCreateEx(Drivers[selected].pGUID, + (void * *)&ddraw, + IID_IDirectDraw7, + NULL); + if (hret != DD_OK) + { + systemMessage(0, "Error during DirectDrawCreateEx: %08x", hret); + /**/ ::FreeLibrary(h); + return -1; + } + } + else + { + // should not happen.... + systemMessage(0, "Error getting DirectDrawCreateEx"); + /**/ ::FreeLibrary(h); + return -1; + } + + VideoMode dlg(ddraw, pWnd); + + int res = dlg.DoModal(); + + if (res != -1) + { + *guid = Drivers[selected].pGUID; + } + ddraw->Release(); + ddraw = NULL; + + // If the library was loaded by calling LoadLibrary(), + // then you must use FreeLibrary() to let go of it. + /**/ ::FreeLibrary(h); + + return res; +} + +///////////////////////////////////////////////////////////////////////////// +// VideoMode dialog + +VideoMode::VideoMode(LPDIRECTDRAW7 pDraw, CWnd*pParent /*=NULL*/) + : CDialog(VideoMode::IDD, pParent) +{ + //{{AFX_DATA_INIT(VideoMode) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + pDirectDraw = pDraw; +} + +void VideoMode::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(VideoMode) + DDX_Control(pDX, IDC_MODES, m_modes); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(VideoMode, CDialog) +//{{AFX_MSG_MAP(VideoMode) +ON_LBN_SELCHANGE(IDC_MODES, OnSelchangeModes) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_BN_CLICKED(ID_OK, OnOk) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// VideoMode message handlers + +void VideoMode::OnSelchangeModes() +{ + int item = m_modes.GetCurSel(); + + GetDlgItem(ID_OK)->EnableWindow(item != -1); +} + +void VideoMode::OnCancel() +{ + EndDialog(-1); +} + +void VideoMode::OnOk() +{ + int cur = m_modes.GetCurSel(); + + if (cur != -1) + { + cur = m_modes.GetItemData(cur); + } + EndDialog(cur); +} + +BOOL VideoMode::OnInitDialog() +{ + CDialog::OnInitDialog(); + + // check for available fullscreen modes + pDirectDraw->EnumDisplayModes(DDEDM_STANDARDVGAMODES, NULL, m_modes.m_hWnd, + addVideoMode); + + GetDlgItem(ID_OK)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +///////////////////////////////////////////////////////////////////////////// +// VideoDriverSelect dialog + +VideoDriverSelect::VideoDriverSelect(CWnd*pParent /*=NULL*/) + : CDialog(VideoDriverSelect::IDD, pParent) +{ + //{{AFX_DATA_INIT(VideoDriverSelect) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT +} + +void VideoDriverSelect::DoDataExchange(CDataExchange*pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(VideoDriverSelect) + DDX_Control(pDX, IDC_DRIVERS, m_drivers); + //}}AFX_DATA_MAP +} + +BEGIN_MESSAGE_MAP(VideoDriverSelect, CDialog) +//{{AFX_MSG_MAP(VideoDriverSelect) +ON_BN_CLICKED(ID_OK, OnOk) +ON_BN_CLICKED(ID_CANCEL, OnCancel) +ON_LBN_SELCHANGE(IDC_DRIVERS, OnSelchangeDrivers) +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// VideoDriverSelect message handlers + +void VideoDriverSelect::OnCancel() +{ + EndDialog(-1); +} + +void VideoDriverSelect::OnOk() +{ + EndDialog(m_drivers.GetCurSel()); +} + +BOOL VideoDriverSelect::OnInitDialog() +{ + CDialog::OnInitDialog(); + + for (int i = 0; i < gDriverCnt; i++) + { + m_drivers.AddString(Drivers[i].szDescription); + } + + GetDlgItem(ID_OK)->EnableWindow(FALSE); + CenterWindow(); + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + +void VideoDriverSelect::OnSelchangeDrivers() +{ + GetDlgItem(ID_OK)->EnableWindow(m_drivers.GetCurSel() != -1); +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/VideoMode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/VideoMode.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,79 @@ +#if !defined(AFX_VIDEOMODE_H__074B2426_32EA_4D69_9215_AB5E90F885D0__INCLUDED_) +#define AFX_VIDEOMODE_H__074B2426_32EA_4D69_9215_AB5E90F885D0__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// VideoMode.h : header file +// + +///////////////////////////////////////////////////////////////////////////// +// VideoMode dialog + +class VideoMode : public CDialog +{ + // Construction +public: + VideoMode(LPDIRECTDRAW7 pDraw, CWnd *pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(VideoMode) + enum { IDD = IDD_MODES }; + CListBox m_modes; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(VideoMode) +protected: + virtual void DoDataExchange(CDataExchange *pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(VideoMode) + afx_msg void OnSelchangeModes(); + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +private: + LPDIRECTDRAW7 pDirectDraw; +}; + +///////////////////////////////////////////////////////////////////////////// +// VideoDriverSelect dialog + +class VideoDriverSelect : public CDialog +{ + // Construction +public: + VideoDriverSelect(CWnd *pParent = NULL); // standard constructor + + // Dialog Data + //{{AFX_DATA(VideoDriverSelect) + enum { IDD = IDD_DRIVERS }; + CListBox m_drivers; + //}}AFX_DATA + + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(VideoDriverSelect) +protected: + virtual void DoDataExchange(CDataExchange *pDX); // DDX/DDV support + //}}AFX_VIRTUAL + + // Implementation +protected: + // Generated message map functions + //{{AFX_MSG(VideoDriverSelect) + afx_msg void OnCancel(); + afx_msg void OnOk(); + virtual BOOL OnInitDialog(); + afx_msg void OnSelchangeDrivers(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +}; +#endif // !defined(AFX_VIDEOMODE_H__074B2426_32EA_4D69_9215_AB5E90F885D0__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/VisualBoyAdvance.exe.manifest --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/VisualBoyAdvance.exe.manifest Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,23 @@ + + + +VisualBoyAdvance Emulator. + + + + + + + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/WavWriter.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/WavWriter.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,92 @@ +// WavWriter.cpp: implementation of the WavWriter class. +// +////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +#include "WavWriter.h" + +#include "../common/Util.h" + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +WavWriter::WavWriter() +{ + m_file = NULL; + m_len = 0; + m_posSize = 0; +} + +WavWriter::~WavWriter() +{ + if (m_file) + Close(); +} + +void WavWriter::Close() +{ + // calculate the total file length + u32 len = ftell(m_file)-8; + fseek(m_file, 4, SEEK_SET); + u8 data[4]; + utilPutDword(data, len); + fwrite(data, 1, 4, m_file); + // write out the size of the data section + fseek(m_file, m_posSize, SEEK_SET); + utilPutDword(data, m_len); + fwrite(data, 1, 4, m_file); + fclose(m_file); + m_file = NULL; +} + +bool WavWriter::Open(const char *name) +{ + if (m_file) + Close(); + m_file = fopen(name, "wb"); + + if (!m_file) + return false; + // RIFF header + u8 data[4] = { 'R', 'I', 'F', 'F' }; + fwrite(data, 1, 4, m_file); + utilPutDword(data, 0); + // write 0 for now. Will get filled during close + fwrite(data, 1, 4, m_file); + // write WAVE header + u8 data2[4] = { 'W', 'A', 'V', 'E' }; + fwrite(data2, 1, 4, m_file); + return true; +} + +void WavWriter::SetFormat(const WAVEFORMATEX *format) +{ + if (m_file == NULL) + return; + // write fmt header + u8 data[4] = { 'f', 'm', 't', ' ' }; + fwrite(data, 1, 4, m_file); + u32 value = sizeof(WAVEFORMATEX); + utilPutDword(data, value); + fwrite(data, 1, 4, m_file); + fwrite(format, 1, sizeof(WAVEFORMATEX), m_file); + // start data header + u8 data2[4] = { 'd', 'a', 't', 'a' }; + fwrite(data2, 1, 4, m_file); + + m_posSize = ftell(m_file); + // write 0 for data chunk size. Filled out during Close() + utilPutDword(data, 0); + fwrite(data, 1, 4, m_file); +} + +void WavWriter::AddSound(const u8 *data, int len) +{ + if (m_file == NULL) + return; + // write a block of sound data + fwrite(data, 1, len, m_file); + m_len += len; +} + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/WavWriter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/WavWriter.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,33 @@ +#if !defined(AFX_WAVWRITER_H__BE6C9DE9_60E7_4192_9797_8C7F55B3CE46__INCLUDED_) +#define AFX_WAVWRITER_H__BE6C9DE9_60E7_4192_9797_8C7F55B3CE46__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +// -*- C++ -*- +// WavWriter.h: interface for the WavWriter class. +// + +#include + +class WavWriter +{ + private: + FILE *m_file; + int m_len; + long m_posSize; + + public: + WavWriter(); + ~WavWriter(); + + bool Open(const char *name); + void SetFormat(const WAVEFORMATEX *format); + void AddSound(const u8 *data, int len); + + private: + void Close(); +}; + +#endif // !defined(AFX_WAVWRITER_H__BE6C9DE9_60E7_4192_9797_8C7F55B3CE46__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/WinHelper.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/WinHelper.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,229 @@ +/*---------------------------------------------------------------------- + Copyright (c) 1998 Gipsysoft. All Rights Reserved. + Please see the file "licence.txt" for licencing details. + File: WinHelper.h + Owner: russf@gipsysoft.com + Purpose: Windows helper functions, classes, structures and macros + that make life a little easier + These should all be zero impact classes etc. that is they + should *not* have a cpp file associated with them. + ----------------------------------------------------------------------*/ +#ifndef WINHELPER_H +#define WINHELPER_H + +//#ifndef DEBUGHLP_H +// #include +//#endif // DEBUGHLP_H + +#ifndef FASTCALL +#define FASTCALL +#endif // FASTCALL + +extern void AssertFailed(char *, int, char *); +extern void ApiFailure(char *, int, char *); + +#define R_VERIFY(a) R_ASSERT(a) +#define R_ASSERT(a) \ + do { \ + if (!(a)) { \ + AssertFailed(__FILE__, __LINE__, # a); \ + } \ + } while (0); + +#define VAPI(a) \ + do { \ + if (!(a)) { \ + ApiFailure(__FILE__, __LINE__, # a); \ + } \ + } while (0); + +#define ASSERT_VALID_HWND(a) ASSERT(::IsWindow(a)) + +namespace WinHelper +{ + class CSize : public tagSIZE + // + // Wrapper for the SIZE structure + { +public: + inline CSize() {}; + inline explicit CSize(const SIZE &size) { cx = size.cx; cy = size.cy; } + inline explicit CSize(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; } + inline void Set(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; } + inline operator LPSIZE() { return this; }; + + inline bool operator !=(const SIZE &size) const { return cx != size.cx || cy != size.cy;} + inline CSize & operator =(const SIZE &size) { cx = size.cx; cy = size.cy; return *this; } + inline void Empty() { cx = cy = 0; } + }; + + class CRect : public tagRECT + // + // Wrapper for a RECT structure + { +public: + inline CRect() {} + // Initialisation constructor + inline explicit CRect(const RECT& rhs) { Set(rhs.left, rhs.top, rhs.right, rhs.bottom);} + inline CRect(int xLeft, int yTop, int xRight, int yBottom) { Set(xLeft, yTop, xRight, yBottom); } + // Get the width of the rectangle + inline int Width() const { return right - left; } + // Get the height of the rectangle + inline int Height() const { return bottom - top; } + // overloaded operator so you don't have to do &rc anymore + inline operator LPCRECT() const { return this; }; + inline operator LPRECT() { return this; }; + // Return the SIZE of the rectangle; + inline CSize Size() const { CSize s(Width(), Height()); return s; } + // Return the top left of the rectangle + inline POINT TopLeft() const { POINT pt = { left, top }; return pt; } + // Return the bottom right of the rectangle + inline POINT BottomRight() const { POINT pt = { right, bottom }; return pt; } + // Set the rectangles left, top, right and bottom + inline void Set(int xLeft, int yTop, int xRight, int yBottom) { top = yTop; bottom = yBottom; right = xRight; left = + xLeft; } + // Return true if the rectangle contains all zeros + inline bool IsEmpty() const { return left == 0 && right == 0 && top == 0 && bottom == 0 ? true : false; } + // Zero out our rectangle + inline void Empty() { left = right = top = bottom = 0; } + // Set the size of the rect but leave the top left position untouched. + inline void SetSize(const CSize &size) { bottom = top + size.cy; right = left + size.cx; } + inline void SetSize(const SIZE &size) { bottom = top + size.cy; right = left + size.cx; } + inline void SetSize(int cx, int cy) { bottom = top + cy; right = left + cx; } + // Move the rectangle by an offset + inline void Offset(int cx, int cy) + { + top += cy; + bottom += cy; + right += cx; + left += cx; + } + + // Inflate the rectangle by the cx and cy, use negative to shrink the rectangle + inline void Inflate(int cx, int cy) + { + top -= cy; + bottom += cy; + right += cx; + left -= cx; + } + + // Assignment from a RECT + inline CRect &operator =(const RECT&rhs) + { + left = rhs.left; top = rhs.top; + right = rhs.right; bottom = rhs.bottom; + return *this; + } + + // Return true if the point passed is within the rectangle + inline bool PtInRect(const POINT &pt) const { return (pt.x >= left && pt.x < right && pt.y >= top && pt.y < + bottom); } + // Return true if the rectangle passed overlaps this rectangle + inline bool Intersect(const RECT &rc) const { return (rc.left < right && + rc.right > left && rc.top < bottom && rc.bottom > top); } + }; + + class CPoint : public tagPOINT + // + // Wrapper for the POINT structure + { +public: + inline CPoint() {}; + inline CPoint(LPARAM lParam) { x = LOWORD(lParam); y = HIWORD(lParam); } + inline CPoint(int nX, int nY) { x = nX; y = nY; } + inline CPoint(const POINT &pt) { x = pt.x; y = pt.y; } + inline bool operator ==(const CPoint &rhs) const { return x == rhs.x && y == rhs.y; } + inline bool operator !=(const CPoint &rhs) const { return x != rhs.x || y != rhs.y; } + inline operator LPPOINT() { return this; } + }; + + class CScrollInfo : public tagSCROLLINFO + { +public: + CScrollInfo(UINT fPassedMask) { cbSize = sizeof(tagSCROLLINFO); fMask = fPassedMask; } + }; + + class CCriticalSection + // + // Simple crtical section handler/wrapper + { +public: + inline CCriticalSection() { ::InitializeCriticalSection(&m_sect); } + inline ~CCriticalSection() { ::DeleteCriticalSection(&m_sect); } + + // Blocking lock. + inline void Lock() { ::EnterCriticalSection(&m_sect); } + // Unlock + inline void Unlock() { ::LeaveCriticalSection(&m_sect); } + + class CLock + // + // Simple lock class for the critcal section + { +public: + inline CLock(CCriticalSection §) : m_sect(sect) { m_sect.Lock(); } + inline ~CLock() { m_sect.Unlock(); } +private: + CCriticalSection &m_sect; + + CLock(); + CLock(const CLock &); + CLock & operator =(const CLock &); + }; +private: + CRITICAL_SECTION m_sect; + + CCriticalSection(const CCriticalSection &); + CCriticalSection & operator =(const CCriticalSection &); + }; + +#define ZeroStructure(t) ZeroMemory(&t, sizeof(t)) +#define countof(t) (sizeof((t)) / sizeof((t)[0])) +#define UNREF(P) UNREFERENCED_PARAMETER(P) + + inline bool IsShiftPressed() + { + return GetKeyState(VK_SHIFT) & 0x8000 ? true : false; + } + + inline bool IsAltPressed() + { + return GetKeyState(VK_MENU) & 0x8000 ? true : false; + } + + inline bool IsControlPressed() + { + return GetKeyState(VK_CONTROL) & 0x8000 ? true : false; + } + + inline HICON LoadIcon16x16(HINSTANCE hInst, UINT uID) + // + // Load a 16x16 icon from the same resource as the other size icons. + { + return reinterpret_cast(::LoadImage(hInst, MAKEINTRESOURCE(uID), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR)); + } + + class CDeferWindowPos + // + // Wrapper for the Begin, Defer and End WindowPos functions. Nothing glamorous. + { +public: + inline CDeferWindowPos(const int nWindows = 1) : m_hdlDef(::BeginDeferWindowPos(nWindows)) {} + inline ~CDeferWindowPos() { R_VERIFY(::EndDeferWindowPos(m_hdlDef)); } + inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags) + { + return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, x, y, cx, cy, uFlags); + } + + inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, const CRect &rc, UINT uFlags) + { + return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, rc.left, rc.top, rc.Width(), rc.Height(), uFlags); + } + +private: + HDWP m_hdlDef; + }; +} // WinHelper + +#endif //WINHELPER_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/WinMiscUtil.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/WinMiscUtil.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,497 @@ +#include "stdafx.h" +#include "WinMiscUtil.h" +#include "WinResUtil.h" +#include "resource.h" +#include "../NLS.h" +#include "VBA.h" +#include "Reg.h" +#include "../common/movie.h" +#include + +#include "GSACodeSelect.h" +#include "../gba/GBACheats.h" +#include "../gb/gbCheats.h" + +// #undef WinDef macro garbage +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +using std::max; +using std::min; + +extern int emulating; + +extern const char IDS_ROM_DIR[] = "romDir"; +extern const char IDS_GBXROM_DIR[] = "gbromDir"; +extern const char IDS_BATTERY_DIR[] = "batteryDir"; +extern const char IDS_SAVE_DIR[] = "saveDir"; +extern const char IDS_MOVIE_DIR[] = "moviesDir"; +extern const char IDS_CHEAT_DIR[] = "cheatsDir"; +extern const char IDS_LUA_DIR[] = "luaDir"; +extern const char IDS_IPS_DIR[] = "ipsDir"; +extern const char IDS_AVI_DIR[] = "aviRecordDir"; +extern const char IDS_WAV_DIR[] = "soundRecordDir"; +extern const char IDS_CAPTURE_DIR[] = "captureDir"; +extern const char IDS_WATCH_DIR[] = "watchDir"; + +extern const char IDS_ROM_DEFAULT_DIR[] = "\\roms"; +extern const char IDS_GBXROM_DEFAULT_DIR[] = "\\gbroms"; +extern const char IDS_BATTERY_DEFAULT_DIR[] = "\\battery"; +extern const char IDS_SAVE_DEFAULT_DIR[] = "\\save"; +extern const char IDS_MOVIE_DEFAULT_DIR[] = "\\movies"; +extern const char IDS_CHEAT_DEFAULT_DIR[] = "\\cheats"; +extern const char IDS_LUA_DEFAULT_DIR[] = "\\lua"; +extern const char IDS_IPS_DEFAULT_DIR[] = "\\ips"; +extern const char IDS_AVI_DEFAULT_DIR[] = "\\avi"; +extern const char IDS_WAV_DEFAULT_DIR[] = "\\wav"; +extern const char IDS_CAPTURE_DEFAULT_DIR[] = "\\screen"; +extern const char IDS_WATCH_DEFAULT_DIR[] = "\\watches"; + +extern const char *IDS_tbl[] = { + IDS_ROM_DIR, IDS_GBXROM_DIR, IDS_BATTERY_DIR, IDS_SAVE_DIR, + IDS_MOVIE_DIR, IDS_CHEAT_DIR, IDS_LUA_DIR, IDS_IPS_DIR, + IDS_AVI_DIR, IDS_WAV_DIR, IDS_CAPTURE_DIR, IDS_WATCH_DIR +}; + +extern const char *IDS_def_tbl[] = { + IDS_ROM_DEFAULT_DIR, IDS_GBXROM_DEFAULT_DIR, IDS_BATTERY_DEFAULT_DIR, IDS_SAVE_DEFAULT_DIR, + IDS_MOVIE_DEFAULT_DIR, IDS_CHEAT_DEFAULT_DIR, IDS_LUA_DEFAULT_DIR, IDS_IPS_DEFAULT_DIR, + IDS_AVI_DEFAULT_DIR, IDS_WAV_DEFAULT_DIR, IDS_CAPTURE_DEFAULT_DIR, IDS_WATCH_DEFAULT_DIR +}; + +// these could be made VBA members, but the VBA class is already oversized too much +// + +bool winFileExists(const CString &filename) +{ + FILE *f = fopen(filename, "rb"); + if (f) + { + fclose(f); + return true; + } + return false; +} + +bool winIsDriveRoot(const CString &file) +{ + if (file.GetLength() == 3) + { + if (file[1] == ':' && file[2] == '\\') + return true; + } + return false; +} + +CString winGetOriginalFilename(const CString &file) +{ + int index = file.Find('|'); + + if (index != -1) + return file.Left(index); + else + return file; +} + +CString winGetDirFromFilename(const CString &file) +{ + CString temp = winGetOriginalFilename(file); + int index = max(temp.ReverseFind('/'), temp.ReverseFind('\\')); + if (index != -1) + { + temp = temp.Left(index); + if (temp.GetLength() == 2 && temp[1] == ':') + temp += "\\"; + } + + return temp; +} + +CString winGetDestDir(const CString &TargetDirReg) +{ + CString targetDir = regQueryStringValue(TargetDirReg, NULL); + int pos = targetDir.ReverseFind('\\'); + if (pos > 0 && pos == targetDir.GetLength() - 1) + targetDir.Delete(pos); + + // it makes no sense to create rom directories + // see MainWnd::winFileOpenSelect for more info + if (!TargetDirReg.Compare(IDS_ROM_DIR) || !TargetDirReg.Compare(IDS_GBXROM_DIR)) + return targetDir; + + if (targetDir.IsEmpty()) + { + targetDir = theApp.exeDir; // reset the targetDir to the application's path + if (!TargetDirReg.Compare(IDS_BATTERY_DIR)) + { + targetDir += IDS_BATTERY_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_SAVE_DIR)) + { + targetDir += IDS_SAVE_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_MOVIE_DIR)) + { + targetDir += IDS_MOVIE_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_CHEAT_DIR)) + { + targetDir += IDS_CHEAT_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_LUA_DIR)) + { + targetDir += IDS_LUA_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_IPS_DIR)) + { + targetDir += IDS_IPS_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_AVI_DIR)) + { + targetDir += IDS_AVI_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_WAV_DIR)) + { + targetDir += IDS_WAV_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_CAPTURE_DIR)) + { + targetDir += IDS_CAPTURE_DEFAULT_DIR; + } + else if (!TargetDirReg.Compare(IDS_WATCH_DIR)) + { + targetDir += IDS_WATCH_DEFAULT_DIR; + } + regSetStringValue(TargetDirReg, targetDir); // Add the directory to the INI file + } + + _mkdir(targetDir); // make the directory + + return targetDir; +} + +CString winGetDestFilename(const CString &LogicalRomName, const CString &TargetDirReg, const CString &ext) +{ + if (LogicalRomName.GetLength() == 0) + return CString(); + + CString targetDir = winGetDestDir(TargetDirReg); + targetDir += '\\'; + + CString buffer = LogicalRomName; + + int index = max(buffer.ReverseFind('/'), max(buffer.ReverseFind('\\'), buffer.ReverseFind('|'))); + if (index != -1) + buffer = buffer.Right(buffer.GetLength() - index - 1); + + index = buffer.ReverseFind('.'); + if (index != -1) + buffer = buffer.Left(index); + + CString filename; + filename.Format("%s%s%s", targetDir, buffer, ext); + bool fileExists = winFileExists(filename); + + // check for old style of naming, for better backward compatibility + if (!fileExists || theApp.filenamePreference == 0) + { + index = LogicalRomName.Find('|'); + if (index != -1) + { + buffer = LogicalRomName.Left(index); + index = max(buffer.ReverseFind('/'), buffer.ReverseFind('\\')); + + int dotIndex = buffer.ReverseFind('.'); + if (dotIndex > index) + buffer = buffer.Left(dotIndex); + + if (index != -1) + buffer = buffer.Right(buffer.GetLength() - index - 1); + + CString filename2; + filename2.Format("%s%s%s", targetDir, buffer, ext); + bool file2Exists = winFileExists(filename2); + + if ((file2Exists && !fileExists) || (theApp.filenamePreference == 0 && (file2Exists || !fileExists))) + return filename2; + } + } + + return filename; +} + +CString winGetSavestateFilename(const CString &LogicalRomName, int nID) +{ + CString ext; +// size_t startindex; // forget about C89/ANSI-C +// size_t endindex; + if (VBAMovieActive() && theApp.AsscWithSaveState) + { + std::string fs(VBAMovieGetFilename()); // RVO tip + size_t startindex = fs.find_last_of("/\\") ; + if (startindex < fs.length()) + ++startindex; // luckily the found character can't be at the end of fs + else + startindex = 0; + size_t endindex = fs.find_last_of("."); + if (endindex < fs.length() && endindex > startindex) + endindex; //?? + else + endindex = fs.length(); + fs = fs.substr(startindex, endindex - startindex); + ext.Format("-%s-%d.sgm", fs.c_str(), nID); + } + else + { + ext.Format("%d.sgm", nID); + } + return winGetDestFilename(LogicalRomName, IDS_SAVE_DIR, ext); +} + +CString winGetSavestateMenuString(const CString &LogicalRomName, int nID) +{ + CString str; + if (theApp.showSlotTime) + { + CFileStatus status; + if (emulating && CFile::GetStatus(winGetSavestateFilename(LogicalRomName, nID), status)) + { + str.Format("#&%d %s", nID, status.m_mtime.Format("%Y/%m/%d %H:%M:%S")); + } + else + { + str.Format("#&%d ----/--/-- --:--:--", nID); + } + } + else + { + str.Format("Slot #&%d", nID); + } + + return str; +} + +void winCorrectPath(CString &path) +{ + if (winFileExists(path)) + { + return; + } + + CString tempStr = theApp.exeDir; + tempStr += "\\"; + tempStr += path; + + if (winFileExists(tempStr)) + { + path = tempStr; + return; + } + + for (int i = 0; i < _countof(IDS_tbl); ++i) + { + tempStr = winGetDestDir(IDS_tbl[i]); + tempStr += "\\"; + tempStr += path; + + if (winFileExists(tempStr)) + { + path = tempStr; + return; + } + } +} + +void winCorrectPath(char *path) +{ + CString pathCStr(path); + winCorrectPath(pathCStr); + strcpy(path, pathCStr); +} + +// some file I/O + +int winScreenCapture(int captureNumber) +{ + CString ext; + CString captureName; + + do + { + if (theApp.captureFormat == 0) + ext.Format("_%03d.png", captureNumber); + else + ext.Format("_%03d.bmp", captureNumber); + + captureName = winGetDestFilename(theApp.gameFilename, IDS_CAPTURE_DIR, ext); + ++captureNumber; + } while (winFileExists(captureName) && captureNumber > 0); + + if (captureNumber < 0) + { + systemMessage(0, "Too many existing files (not less than %d)! Screen capture failed!", captureNumber - 1); + return 0; + } + + if (theApp.captureFormat == 0) + theApp.emulator.emuWritePNG(captureName); + else + theApp.emulator.emuWriteBMP(captureName); + + systemScreenMessage(winResLoadString(IDS_SCREEN_CAPTURE)); + + return captureNumber; +} + +bool winImportGSACodeFile(CString &fileName) +{ + FILE *f = fopen(fileName, "rb"); + + if (f == NULL) + { + systemMessage(MSG_CANNOT_OPEN_FILE, "Cannot open file %s", fileName); + return false; + } + + if (systemCartridgeType == 1) + { + fclose(f); + return gbCheatReadGSCodeFile(fileName); + } + + u32 len; + fread(&len, 1, 4, f); + if (len != 14) + { + fclose(f); + systemMessage(MSG_UNSUPPORTED_CODE_FILE, "Unsupported code file %s", + fileName); + return false; + } + char buffer[16]; + fread(buffer, 1, 14, f); + buffer[14] = 0; + if (memcmp(buffer, "SharkPortCODES", 14)) + { + fclose(f); + systemMessage(MSG_UNSUPPORTED_CODE_FILE, "Unsupported code file %s", + fileName); + return false; + } + fseek(f, 0x1e, SEEK_SET); + fread(&len, 1, 4, f); + int game = 0; + if (len > 1) + { + GSACodeSelect dlg(f); + game = dlg.DoModal(); + } + fclose(f); + + bool v3 = false; + + int index = fileName.ReverseFind('.'); + + if (index != -1) + { + if (fileName.Right(3).CompareNoCase("XPC") == 0) + v3 = true; + } + + if (game != -1) + { + return cheatsImportGSACodeFile(fileName, game, v3); + } + + return true; +} + +void winLoadCheatList(const char *name) +{ + bool res = false; + + if (systemCartridgeType == 0) + res = cheatsLoadCheatList(name); + else + res = gbCheatsLoadCheatList(name); + + if (res) + systemScreenMessage(winResLoadString(IDS_LOADED_CHEATS)); +} + +void winSaveCheatList(const char *name) +{ + if (systemCartridgeType == 0) + cheatsSaveCheatList(name); + else + gbCheatsSaveCheatList(name); +} + +void winLoadCheatListDefault() +{ + CString cheatName = winGetDestFilename(theApp.gameFilename, IDS_CHEAT_DIR, ".clt"); + + winLoadCheatList(cheatName); +} + +void winSaveCheatListDefault() +{ + CString cheatName = winGetDestFilename(theApp.gameFilename, IDS_CHEAT_DIR, ".clt"); + + winSaveCheatList(cheatName); +} + +bool winReadBatteryFile() +{ + CString batteryName = winGetDestFilename(theApp.gameFilename, IDS_BATTERY_DIR, ".sav"); + + bool res = false; + + if (theApp.emulator.emuReadBattery) + res = theApp.emulator.emuReadBattery(batteryName); + + if (res) + systemScreenMessage(winResLoadString(IDS_LOADED_BATTERY)); + + return res; +} + +bool winWriteBatteryFile() +{ + CString batteryName = winGetDestFilename(theApp.gameFilename, IDS_BATTERY_DIR, ".sav"); + + if (theApp.emulator.emuWriteBattery) + return theApp.emulator.emuWriteBattery(batteryName); + + return false; +} + +bool winEraseBatteryFile() +{ + CString batteryName = winGetDestFilename(theApp.gameFilename, IDS_BATTERY_DIR, ".sav"); + return !remove(batteryName); +} + +bool winReadSaveGame(const char *name) +{ + if (theApp.emulator.emuReadState) + return theApp.emulator.emuReadState(name); + return false; +} + +bool winWriteSaveGame(const char *name) +{ + if (theApp.emulator.emuWriteState) + return theApp.emulator.emuWriteState(name); + return false; +} + +bool winEraseSaveGame(const char *name) +{ + return !remove(name); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/WinMiscUtil.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/WinMiscUtil.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,45 @@ +#ifndef VBA_WIN32_WINMISCUTIL_H +#define VBA_WIN32_WINMISCUTIL_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +extern const char IDS_ROM_DIR[]; +extern const char IDS_GBXROM_DIR[]; +extern const char IDS_BATTERY_DIR[]; +extern const char IDS_SAVE_DIR[]; +extern const char IDS_MOVIE_DIR[]; +extern const char IDS_CHEAT_DIR[]; +extern const char IDS_LUA_DIR[]; +extern const char IDS_IPS_DIR[]; +extern const char IDS_AVI_DIR[]; +extern const char IDS_WAV_DIR[]; +extern const char IDS_CAPTURE_DIR[]; +extern const char IDS_WATCH_DIR[]; + +extern bool winFileExists(const CString &filename); +extern bool winIsDriveRoot(const CString &file); +extern CString winGetOriginalFilename(const CString &file); +extern CString winGetDirFromFilename(const CString &file); +extern CString winGetSavestateFilename(const CString &LogicalRomName, int nID); +extern CString winGetSavestateMenuString(const CString &LogicalRomName, int nID); +extern CString winGetDestDir(const CString &TargetDirReg); +extern CString winGetDestFilename(const CString &LogicalRomName, const CString &TargetDirReg, const CString &ext); +extern void winCorrectPath(CString &path); +extern void winCorrectPath(char *path); + +int winScreenCapture(int captureNumber = 0); +bool winImportGSACodeFile(CString& fileName); +void winLoadCheatList(const char *name); +void winSaveCheatList(const char *name); +void winLoadCheatListDefault(); +void winSaveCheatListDefault(); +bool winReadBatteryFile(); +bool winWriteBatteryFile(); +bool winEraseBatteryFile(); +bool winReadSaveGame(const char *name); +bool winWriteSaveGame(const char *name); +bool winEraseSaveGame(const char *name); + +#endif // VBA_WIN32_WINMISCUTIL_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/WinResUtil.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/WinResUtil.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,99 @@ +#include "stdafx.h" +#include "WinResUtil.h" + +static HINSTANCE winResGetInstance(LPCTSTR resType, LPCTSTR resName) +{ + // TODO: make language DLL first + return AfxFindResourceHandle(resName, resType); +} + +UCHAR *winResGetResource(LPCTSTR resType, LPCTSTR resName) +{ + HINSTANCE winResInstance = winResGetInstance(resType, resName); + + HRSRC hRsrc = FindResourceEx(winResInstance, resType, resName, 0); + + if (hRsrc != NULL) + { + HGLOBAL hGlobal = LoadResource(winResInstance, hRsrc); + + if (hGlobal != NULL) + { + UCHAR *b = (UCHAR *)LockResource(hGlobal); + + return b; + } + } + return NULL; +} + +HMENU winResLoadMenu(LPCTSTR menuName) +{ + UCHAR *b = winResGetResource(RT_MENU, menuName); + + if (b != NULL) + { + HMENU menu = LoadMenuIndirect((CONST MENUTEMPLATE *)b); + + if (menu != NULL) + return menu; + } + + return LoadMenu(NULL, menuName); +} + +int winResDialogBox(LPCTSTR boxName, HWND parent, DLGPROC dlgProc, LPARAM lParam) +{ + /* + UCHAR * b = winResGetResource(RT_DIALOG, boxName); + + if(b != NULL) { + + return DialogBoxIndirectParam(hInstance, + (LPCDLGTEMPLATE)b, + parent, + dlgProc, + lParam); + } + + return DialogBoxParam(hInstance, + boxName, + parent, + dlgProc, + lParam); + */ + return 0; +} + +int winResDialogBox(LPCTSTR boxName, HWND parent, DLGPROC dlgProc) +{ + return winResDialogBox(boxName, + parent, + dlgProc, + 0); +} + +CString winResLoadString(UINT id) +{ + int stId = id / 16 + 1; + HINSTANCE inst = winResGetInstance(RT_STRING, MAKEINTRESOURCE(stId)); + + CString res; + if (!res.LoadString(id)) + { + // TODO: handle case where string is only in the default English + res = ""; + } + + res.Replace('_', '|'); + + return res; +} + +CString winResLoadFilter(UINT id) +{ + CString res = winResLoadString(id); + res.Replace('_', '|'); + + return res; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/WinResUtil.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/WinResUtil.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,14 @@ +#ifndef VBA_WIN32_WINRESUTIL_H +#define VBA_WIN32_WINRESUTIL_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +extern HMENU winResLoadMenu(LPCTSTR menuName); +extern int winResDialogBox(LPCTSTR boxName, HWND parent, DLGPROC dlgProc); +extern int winResDialogBox(LPCTSTR boxName, HWND parent, DLGPROC dlgProc, LPARAM lParam); +extern CString winResLoadString(UINT id); +extern CString winResLoadFilter(UINT id); + +#endif // VBA_WIN32_WINRESUTIL_H diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ZoomControl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ZoomControl.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,166 @@ +// ZoomControl.cpp : implementation file +// + +#include "stdafx.h" +#include "ZoomControl.h" + +bool ZoomControl::isRegistered = false; + +///////////////////////////////////////////////////////////////////////////// +// ZoomControl + +ZoomControl::ZoomControl() +{ + ZeroMemory(colors, 3*64); + selected = -1; + registerClass(); +} + +ZoomControl::~ZoomControl() +{} + +BEGIN_MESSAGE_MAP(ZoomControl, CWnd) +//{{AFX_MSG_MAP(ZoomControl) +ON_WM_PAINT() +ON_WM_LBUTTONDOWN() +ON_WM_ERASEBKGND() +//}}AFX_MSG_MAP +END_MESSAGE_MAP() + +///////////////////////////////////////////////////////////////////////////// +// ZoomControl message handlers + +void ZoomControl::registerClass() +{ + if (!isRegistered) + { + WNDCLASS wc; + ZeroMemory(&wc, sizeof(wc)); + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; + wc.lpfnWndProc = (WNDPROC) ::DefWindowProc; + wc.hInstance = AfxGetInstanceHandle(); + wc.hIcon = NULL; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wc.lpszMenuName = NULL; + wc.lpszClassName = "VbaZoomControl"; + AfxRegisterClass(&wc); + isRegistered = true; + } +} + +void ZoomControl::OnPaint() +{ + CPaintDC dc(this); // device context for painting + + RECT rect; + GetClientRect(&rect); + + int w = rect.right - rect.left; + int h = rect.bottom - rect.top; + + CDC memDC ; + memDC.CreateCompatibleDC(&dc); + CBitmap bitmap, *pOldBitmap; + bitmap.CreateCompatibleBitmap(&dc, w, h); + + pOldBitmap = memDC.SelectObject(&bitmap); + + int multX = w / 8; + int multY = h / 8; + + int i; + for (i = 0; i < 64; i++) + { + CBrush b; + b.CreateSolidBrush(RGB(colors[i*3+2], colors[i*3+1], colors[i*3])); + + RECT r; + int x = i & 7; + int y = i / 8; + r.top = y*multY; + r.left = x*multX; + r.bottom = r.top + multY; + r.right = r.left + multX; + memDC.FillRect(&r, &b); + b.DeleteObject(); + } + + CPen pen; + pen.CreatePen(PS_SOLID, 1, RGB(192, 192, 192)); + CPen *old = (CPen *)memDC.SelectObject(&pen); + + for (i = 0; i < 8; i++) + { + memDC.MoveTo(0, i * multY); + memDC.LineTo(w, i * multY); + memDC.MoveTo(i * multX, 0); + memDC.LineTo(i * multX, h); + } + + if (selected != -1) + { + CPen pen2; + pen2.CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); + CPen *old2 = (CPen *)memDC.SelectObject(&pen2); + + int startX = (selected & 7)*multX+1; + int startY = (selected / 8)*multY+1; + int endX = startX + multX-2; + int endY = startY + multY-2; + + memDC.MoveTo(startX, startY); + memDC.LineTo(endX, startY); + memDC.LineTo(endX, endY); + memDC.LineTo(startX, endY); + memDC.LineTo(startX, startY-1); + memDC.SelectObject(old2); + pen2.DeleteObject(); + } + memDC.SelectObject(old); + pen.DeleteObject(); + + dc.BitBlt(0, 0, w, h, + &memDC, 0, 0, SRCCOPY); + + memDC.SelectObject(pOldBitmap); + bitmap.DeleteObject(); + memDC.DeleteDC(); +} + +void ZoomControl::OnLButtonDown(UINT nFlags, CPoint point) +{ + RECT rect; + GetClientRect(&rect); + + int height = rect.bottom - rect.top; + int width = rect.right - rect.left; + + int multX = width / 8; + int multY = height / 8; + + selected = point.x / multX + 8 * (point.y / multY); + + int c = point.x / multX + 8 * (point.y/multY); + u16 color = colors[c*3] << 7 | + colors[c*3+1] << 2 | + (colors[c*3+2] >> 3); + + GetParent()->PostMessage(WM_COLINFO, + color, + 0); + + Invalidate(); +} + +BOOL ZoomControl::OnEraseBkgnd(CDC*pDC) +{ + return TRUE; +} + +void ZoomControl::setColors(const u8 *c) +{ + memcpy(colors, c, 3*64); + selected = -1; + Invalidate(); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ZoomControl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ZoomControl.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,56 @@ +#if !defined(AFX_ZOOMCONTROL_H__BC193230_D2D6_4240_93AE_28C2EF2C641A__INCLUDED_) +#define AFX_ZOOMCONTROL_H__BC193230_D2D6_4240_93AE_28C2EF2C641A__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 +// ZoomControl.h : header file +// +#ifndef WM_COLINFO +#define WM_COLINFO WM_APP+100 +#endif + +///////////////////////////////////////////////////////////////////////////// +// ZoomControl window + +class ZoomControl : public CWnd +{ + // Construction +public: + ZoomControl(); + + // Attributes +public: + // Operations +public: + // Overrides + // ClassWizard generated virtual function overrides + //{{AFX_VIRTUAL(ZoomControl) + //}}AFX_VIRTUAL + + // Implementation +public: + void setColors(const u8 *c); + static bool isRegistered; + virtual ~ZoomControl(); + + // Generated message map functions +protected: + //{{AFX_MSG(ZoomControl) + afx_msg void OnPaint(); + afx_msg void OnLButtonDown(UINT nFlags, CPoint point); + afx_msg BOOL OnEraseBkgnd(CDC*pDC); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() +private: + int selected; + u8 colors[3*64]; + void registerClass(); +}; + +///////////////////////////////////////////////////////////////////////////// + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_ZOOMCONTROL_H__BC193230_D2D6_4240_93AE_28C2EF2C641A__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/gbadvance.ico Binary file src/win32/gbadvance.ico has changed diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ram_search.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ram_search.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,2127 @@ +// A few notes about this implementation of a RAM search window: +// +// Speed of update was one of the highest priories. +// This is because I wanted the RAM search window to be able to +// update every single value in RAM every single frame, and +// keep track of the exact number of frames across which each value has changed, +// without causing the emulation to run noticeably slower than normal. +// +// The data representation was changed from one entry per valid address +// to one entry per contiguous range of uneliminated addresses +// which references uniform pools of per-address properties. +// - This saves time when there are many items because +// it minimizes the amount of data that needs to be stored and processed per address. +// - It also saves time when there are few items because +// it ensures that no time is wasted in iterating through +// addresses that have already been eliminated from the search. +// +// The worst-case scenario is when every other item has been +// eliminated from the search, maximizing the number of regions. +// This implementation manages to handle even that pathological case +// acceptably well. In fact, it still updates faster than the previous implementation. +// The time spent setting up or clearing such a large number of regions +// is somewhat horrendous, but it seems reasonable to have poor worst-case speed +// during these sporadic "setup" steps to achieve an all-around faster per-update speed. +// (You can test this case by performing the search: Modulo 2 Is Specific Address 0) + + +#ifdef _WIN32 + #include "stdafx.h" + #include "resource.h" + #include "VBA.h" + //#include + #include + #include "BaseTsd.h" + #include "GBACheatsDlg.h" + #include "GBCheatsDlg.h" + typedef INT_PTR intptr_t; +#else + #include "stdint.h" +#endif +#include +#include +#include + +#include "ram_search.h" +#include "ramwatch.h" +#include "../gba/GBAGlobals.h" +#include "../gb/gbGlobals.h" +#include "../common/vbalua.h" +#include "Reg.h" + +static inline u8* HardwareToSoftwareAddress(HWAddressType address) +{ + if(!emulating) + return NULL; + + // note: this currently follows the "quick" memory rules, + // meaning it will miss whatever special cases aren't handled by read/writeMemoryQuick. + // if this is made more accurate, it may be necessary to reduce regionSearchGranularity. + if(systemCartridgeType == 0) + { + // GBA + HWAddressType mask = ::map[address >> 24].mask; + if(!mask || (address & 0xFFFFFF) > mask) + return NULL; + return &::map[address >> 24].address[address & mask]; + } + else + { + // GB + extern int32 gbEchoRAMFixOn; + if (gbEchoRAMFixOn) + if (address >= 0xe000 && address < 0xfe00) + address -= 0x2000; + if((address>>12) >= sizeof(gbMemoryMap)/sizeof(*gbMemoryMap)) + return NULL; + return &gbMemoryMap[address>>12][address&0xfff]; + } +} + + +struct MemoryRegion +{ + HWAddressType hardwareAddress; // hardware address of the start of this region + unsigned int size; // number of bytes to the end of this region + unsigned char* softwareAddress; // pointer to the start of the live emulator source values for this region + + unsigned int virtualIndex; // index into s_prevValues, s_curValues, and s_numChanges, valid after being initialized in ResetMemoryRegions() + unsigned int itemIndex; // index into listbox items, valid when s_itemIndicesInvalid is false +}; + +int MAX_RAM_SIZE = 0; +static unsigned char* s_prevValues = 0; // values at last search or reset +static unsigned char* s_curValues = 0; // values at last frame update +static unsigned short* s_numChanges = 0; // number of changes of the item starting at this virtual index address +static MemoryRegion** s_itemIndexToRegionPointer = 0; // used for random access into the memory list (trading memory size to get speed here, too bad it's so much memory), only valid when s_itemIndicesInvalid is false +static BOOL s_itemIndicesInvalid = true; // if true, the link from listbox items to memory regions (s_itemIndexToRegionPointer) and the link from memory regions to list box items (MemoryRegion::itemIndex) both need to be recalculated +static BOOL s_prevValuesNeedUpdate = true; // if true, the "prev" values should be updated using the "cur" values on the next frame update signaled +static unsigned int s_maxItemIndex = 0; // max currently valid item index, the listbox sometimes tries to update things past the end of the list so we need to know this to ignore those attempts +static int s_prevSelCount = -1; + +HWND RamSearchHWnd; +#define hWnd AfxGetMainWnd()->GetSafeHwnd() +#define hInst AfxGetInstanceHandle() +static char Str_Tmp [1024]; + +int disableRamSearchUpdate = false; + + + +//static const MemoryRegion s_prgRegion = { 0x020000, SEGACD_RAM_PRG_SIZE, (unsigned char*)Ram_Prg, true}; +//static const MemoryRegion s_word1MRegion = { 0x200000, SEGACD_1M_RAM_SIZE, (unsigned char*)Ram_Word_1M, true}; +//static const MemoryRegion s_word2MRegion = { 0x200000, SEGACD_2M_RAM_SIZE, (unsigned char*)Ram_Word_2M, true}; +//static const MemoryRegion s_z80Region = { 0xA00000, Z80_RAM_SIZE, (unsigned char*)Ram_Z80, true}; +//static const MemoryRegion s_68kRegion = { 0xFF0000, _68K_RAM_SIZE, (unsigned char*)Ram_68k, true}; +//static const MemoryRegion s_32xRegion = {0x06000000, _32X_RAM_SIZE, (unsigned char*)_32X_Ram, false}; + +// list of contiguous uneliminated memory regions +typedef std::list MemoryList; +static MemoryList s_activeMemoryRegions; +static CRITICAL_SECTION s_activeMemoryRegionsCS; + +// for undo support (could be better, but this way was really easy) +static MemoryList s_activeMemoryRegionsBackup; +static int s_undoType = 0; // 0 means can't undo, 1 means can undo, 2 means can redo + +void RamSearchSaveUndoStateIfNotTooBig(HWND hDlg); +static const int tooManyRegionsForUndo = 10000; + +void ResetMemoryRegions() +{ + systemSoundClearBuffer(); + EnterCriticalSection(&s_activeMemoryRegionsCS); + + s_activeMemoryRegions.clear(); + + // use HardwareToSoftwareAddress to figure out what all the possible memory regions are, + // split up wherever there's a discontinuity in the address in our software RAM. + static const int regionSearchGranularity = 0x100; // if this is too small, we'll waste time (in this function only), but if any region in RAM isn't evenly divisible by this, we might crash. + HWAddressType hwRegionStart = 0; + u8* regionStart = NULL; + u8* regionEnd = NULL; + for(HWAddressType addr = 0; addr != 0x10000000+regionSearchGranularity; addr += regionSearchGranularity) + { + u8* swAddr = HardwareToSoftwareAddress(addr); + if(regionEnd && swAddr != regionEnd+regionSearchGranularity) + { + // hit end of region + // check to see if it mirrors an existing one (in which case we discard it) + bool discard = false; + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ++iter) + { + MemoryRegion& region = *iter; + if(region.softwareAddress == regionStart) + { + unsigned int size = regionSearchGranularity + (regionEnd - regionStart); + if(size <= region.size) + { + discard = true; + } + else + { + hwRegionStart += region.size; + regionStart += region.size; + } + break; + } + } + + // don't include ROM in our RAM search (it's too huge) + if(regionStart == rom || regionStart == gbRom) + discard = true; + + // create the region + if(!discard) + { + MemoryRegion region = { hwRegionStart, regionSearchGranularity + (regionEnd - regionStart), regionStart }; + s_activeMemoryRegions.push_back(region); + } + + hwRegionStart = 0; + regionStart = NULL; + regionEnd = NULL; + } + if(swAddr) + { + if(regionStart) + { + // continue region + regionEnd = swAddr; + } + else + { + // start new region + hwRegionStart = addr; + regionStart = swAddr; + regionEnd = swAddr; + } + } + } + + + int nextVirtualIndex = 0; + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ++iter) + { + MemoryRegion& region = *iter; + region.virtualIndex = nextVirtualIndex; + assert(((intptr_t)region.softwareAddress & 1) == 0 && "somebody needs to reimplement ReadValueAtSoftwareAddress()"); + nextVirtualIndex = region.virtualIndex + region.size; + } + //assert(nextVirtualIndex <= MAX_RAM_SIZE); + + if(nextVirtualIndex > MAX_RAM_SIZE) + { + s_prevValues = (unsigned char*)realloc(s_prevValues, sizeof(char)*(nextVirtualIndex+4)); + memset(s_prevValues, 0, sizeof(char)*(nextVirtualIndex+4)); + + s_curValues = (unsigned char*)realloc(s_curValues, sizeof(char)*(nextVirtualIndex+4)); + memset(s_curValues, 0, sizeof(char)*(nextVirtualIndex+4)); + + s_numChanges = (unsigned short*)realloc(s_numChanges, sizeof(short)*(nextVirtualIndex+4)); + memset(s_numChanges, 0, sizeof(short)*(nextVirtualIndex+4)); + + s_itemIndexToRegionPointer = (MemoryRegion**)realloc(s_itemIndexToRegionPointer, sizeof(MemoryRegion*)*(nextVirtualIndex+4)); + memset(s_itemIndexToRegionPointer, 0, sizeof(MemoryRegion*)*(nextVirtualIndex+4)); + + MAX_RAM_SIZE = nextVirtualIndex; + } + LeaveCriticalSection(&s_activeMemoryRegionsCS); +} + +// eliminates a range of hardware addresses from the search results +// returns 2 if it changed the region and moved the iterator to another region +// returns 1 if it changed the region but didn't move the iterator +// returns 0 if it had no effect +// warning: don't call anything that takes an itemIndex in a loop that calls DeactivateRegion... +// doing so would be tremendously slow because DeactivateRegion invalidates the index cache +int DeactivateRegion(MemoryRegion& region, MemoryList::iterator& iter, HWAddressType hardwareAddress, unsigned int size) +{ + if(hardwareAddress + size <= region.hardwareAddress || hardwareAddress >= region.hardwareAddress + region.size) + { + // region is unaffected + return 0; + } + else if(hardwareAddress > region.hardwareAddress && hardwareAddress + size >= region.hardwareAddress + region.size) + { + // erase end of region + region.size = hardwareAddress - region.hardwareAddress; + return 1; + } + else if(hardwareAddress <= region.hardwareAddress && hardwareAddress + size < region.hardwareAddress + region.size) + { + // erase start of region + int eraseSize = (hardwareAddress + size) - region.hardwareAddress; + region.hardwareAddress += eraseSize; + region.size -= eraseSize; + region.softwareAddress += eraseSize; + region.virtualIndex += eraseSize; + return 1; + } + else if(hardwareAddress <= region.hardwareAddress && hardwareAddress + size >= region.hardwareAddress + region.size) + { + // erase entire region + iter = s_activeMemoryRegions.erase(iter); + s_itemIndicesInvalid = TRUE; + return 2; + } + else //if(hardwareAddress > region.hardwareAddress && hardwareAddress + size < region.hardwareAddress + region.size) + { + // split region + int eraseSize = (hardwareAddress + size) - region.hardwareAddress; + MemoryRegion region2 = {region.hardwareAddress + eraseSize, region.size - eraseSize, region.softwareAddress + eraseSize, region.virtualIndex + eraseSize}; + region.size = hardwareAddress - region.hardwareAddress; + iter = s_activeMemoryRegions.insert(++iter, region2); + s_itemIndicesInvalid = TRUE; + return 2; + } +} + +/* +// eliminates a range of hardware addresses from the search results +// this is a simpler but usually slower interface for the above function +void DeactivateRegion(HWAddressType hardwareAddress, unsigned int size) +{ + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ) + { + MemoryRegion& region = *iter; + if(2 != DeactivateRegion(region, iter, hardwareAddress, size)) + ++iter; + } +} +*/ + +struct AutoCritSect +{ + AutoCritSect(CRITICAL_SECTION* cs) : m_cs(cs) { EnterCriticalSection(m_cs); } + ~AutoCritSect() { LeaveCriticalSection(m_cs); } + CRITICAL_SECTION* m_cs; +}; + +// warning: can be slow +void CalculateItemIndices(int itemSize) +{ + AutoCritSect cs(&s_activeMemoryRegionsCS); + unsigned int itemIndex = 0; + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ++iter) + { + MemoryRegion& region = *iter; + region.itemIndex = itemIndex; + int startSkipSize = ((unsigned int)(itemSize - (unsigned int)region.hardwareAddress)) % itemSize; // FIXME: is this still ok? + unsigned int start = startSkipSize; + unsigned int end = region.size; + for(unsigned int i = start; i < end; i += itemSize) + s_itemIndexToRegionPointer[itemIndex++] = ®ion; + } + s_maxItemIndex = itemIndex; + s_itemIndicesInvalid = FALSE; +} + +template +void UpdateRegionT(const MemoryRegion& region, const MemoryRegion* nextRegionPtr) +{ + //if(GetAsyncKeyState(VK_SHIFT) & 0x8000) // speed hack + // return; + + if(s_prevValuesNeedUpdate) + memcpy(s_prevValues + region.virtualIndex, s_curValues + region.virtualIndex, region.size + sizeof(compareType) - sizeof(stepType)); + + unsigned int startSkipSize = ((unsigned int)(sizeof(stepType) - region.hardwareAddress)) % sizeof(stepType); + + + unsigned char* sourceAddr = region.softwareAddress - region.virtualIndex; + + unsigned int indexStart = region.virtualIndex + startSkipSize; + unsigned int indexEnd = region.virtualIndex + region.size; + + if(sizeof(compareType) == 1) + { + for(unsigned int i = indexStart; i < indexEnd; i++) + { + if(s_curValues[i] != sourceAddr[i]) // if value changed + { + s_curValues[i] = sourceAddr[i]; // update value + //if(s_numChanges[i] != 0xFFFF) + s_numChanges[i]++; // increase change count + } + } + } + else // it's more complicated for non-byte sizes because: + { // - more than one byte can affect a given change count entry + // - when more than one of those bytes changes simultaneously the entry's change count should only increase by 1 + // - a few of those bytes can be outside the region + + unsigned int endSkipSize = ((unsigned int)(startSkipSize - region.size)) % sizeof(stepType); + unsigned int lastIndexToRead = indexEnd + endSkipSize + sizeof(compareType) - sizeof(stepType); + unsigned int lastIndexToCopy = lastIndexToRead; + if(nextRegionPtr) + { + const MemoryRegion& nextRegion = *nextRegionPtr; + int nextStartSkipSize = ((unsigned int)(sizeof(stepType) - nextRegion.hardwareAddress)) % sizeof(stepType); + unsigned int nextIndexStart = nextRegion.virtualIndex + nextStartSkipSize; + if(lastIndexToCopy > nextIndexStart) + lastIndexToCopy = nextIndexStart; + } + + unsigned int nextValidChange [sizeof(compareType)]; + for(unsigned int i = 0; i < sizeof(compareType); i++) + nextValidChange[i] = indexStart + i; + + for(unsigned int i = indexStart, j = 0; i < lastIndexToRead; i++, j++) + { + if(s_curValues[i] != sourceAddr[i]) // if value of this byte changed + { + if(i < lastIndexToCopy) + s_curValues[i] = sourceAddr[i]; // update value + for(int k = 0; k < sizeof(compareType); k++) // loop through the previous entries that contain this byte + { + if(i >= indexEnd+k) + continue; + int m = (j-k+sizeof(compareType)) & (sizeof(compareType)-1); + if(nextValidChange[m] <= i) // if we didn't already increase the change count for this entry + { + //if(s_numChanges[i-k] != 0xFFFF) + s_numChanges[i-k]++; // increase the change count for this entry + nextValidChange[m] = i-k+sizeof(compareType); // and remember not to increase it again + } + } + } + } + } +} + +template +void UpdateRegionsT() +{ + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end();) + { + const MemoryRegion& region = *iter; + ++iter; + const MemoryRegion* nextRegion = (iter == s_activeMemoryRegions.end()) ? NULL : &*iter; + + UpdateRegionT(region, nextRegion); + } + + s_prevValuesNeedUpdate = false; +} + +template +int CountRegionItemsT() +{ + AutoCritSect cs(&s_activeMemoryRegionsCS); + if(sizeof(stepType) == 1) + { + if(s_activeMemoryRegions.empty()) + return 0; + + if(s_itemIndicesInvalid) + CalculateItemIndices(sizeof(stepType)); + + MemoryRegion& lastRegion = s_activeMemoryRegions.back(); + return lastRegion.itemIndex + lastRegion.size; + } + else // the branch above is faster but won't work if the step size isn't 1 + { + int total = 0; + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ++iter) + { + MemoryRegion& region = *iter; + int startSkipSize = ((unsigned int)(sizeof(stepType) - region.hardwareAddress)) % sizeof(stepType); + total += (region.size - startSkipSize + (sizeof(stepType)-1)) / sizeof(stepType); + } + return total; + } +} + +// returns information about the item in the form of a "fake" region +// that has the item in it and nothing else +template +void ItemIndexToVirtualRegion(unsigned int itemIndex, MemoryRegion& virtualRegion) +{ + if(s_itemIndicesInvalid) + CalculateItemIndices(sizeof(stepType)); + + if(itemIndex >= s_maxItemIndex) + { + memset(&virtualRegion, 0, sizeof(MemoryRegion)); + return; + } + + const MemoryRegion* regionPtr = s_itemIndexToRegionPointer[itemIndex]; + const MemoryRegion& region = *regionPtr; + + int bytesWithinRegion = (itemIndex - region.itemIndex) * sizeof(stepType); + int startSkipSize = ((unsigned int)(sizeof(stepType) - region.hardwareAddress)) % sizeof(stepType); + bytesWithinRegion += startSkipSize; + + virtualRegion.size = sizeof(compareType); + virtualRegion.hardwareAddress = region.hardwareAddress + bytesWithinRegion; + virtualRegion.softwareAddress = region.softwareAddress + bytesWithinRegion; + virtualRegion.virtualIndex = region.virtualIndex + bytesWithinRegion; + virtualRegion.itemIndex = itemIndex; + return; +} + +template +unsigned int ItemIndexToVirtualIndex(unsigned int itemIndex) +{ + MemoryRegion virtualRegion; + ItemIndexToVirtualRegion(itemIndex, virtualRegion); + return virtualRegion.virtualIndex; +} + +template +T ReadLocalValue(const unsigned char* data) +{ + return *(const T*)data; +} +//template<> signed char ReadLocalValue(const unsigned char* data) { return *data; } +//template<> unsigned char ReadLocalValue(const unsigned char* data) { return *data; } + + +template +compareType GetPrevValueFromVirtualIndex(unsigned int virtualIndex) +{ + return ReadLocalValue(s_prevValues + virtualIndex); + //return *(compareType*)(s_prevValues+virtualIndex); +} +template +compareType GetCurValueFromVirtualIndex(unsigned int virtualIndex) +{ + return ReadLocalValue(s_curValues + virtualIndex); +// return *(compareType*)(s_curValues+virtualIndex); +} +template +unsigned short GetNumChangesFromVirtualIndex(unsigned int virtualIndex) +{ + unsigned short num = s_numChanges[virtualIndex]; + //for(unsigned int i = 1; i < sizeof(stepType); i++) + // if(num < s_numChanges[virtualIndex+i]) + // num = s_numChanges[virtualIndex+i]; + return num; +} + +template +compareType GetPrevValueFromItemIndex(unsigned int itemIndex) +{ + int virtualIndex = ItemIndexToVirtualIndex(itemIndex); + return GetPrevValueFromVirtualIndex(virtualIndex); +} +template +compareType GetCurValueFromItemIndex(unsigned int itemIndex) +{ + int virtualIndex = ItemIndexToVirtualIndex(itemIndex); + return GetCurValueFromVirtualIndex(virtualIndex); +} +template +unsigned short GetNumChangesFromItemIndex(unsigned int itemIndex) +{ + int virtualIndex = ItemIndexToVirtualIndex(itemIndex); + return GetNumChangesFromVirtualIndex(virtualIndex); +} +template +unsigned int GetHardwareAddressFromItemIndex(unsigned int itemIndex) +{ + MemoryRegion virtualRegion; + ItemIndexToVirtualRegion(itemIndex, virtualRegion); + return virtualRegion.hardwareAddress; +} + +// this one might be unreliable, haven't used it much +template +unsigned int HardwareAddressToItemIndex(HWAddressType hardwareAddress) +{ + if(s_itemIndicesInvalid) + CalculateItemIndices(sizeof(stepType)); + + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ++iter) + { + MemoryRegion& region = *iter; + if(hardwareAddress >= region.hardwareAddress && hardwareAddress < region.hardwareAddress + region.size) + { + int indexWithinRegion = (hardwareAddress - region.hardwareAddress) / sizeof(stepType); + return region.itemIndex + indexWithinRegion; + } + } + + return -1; +} + + + +// workaround for MSVC 7 that doesn't support varadic C99 macros +#define CALL_WITH_T_SIZE_TYPES_0(functionName, sizeTypeID, isSigned, requiresAligned) \ + (sizeTypeID == 'b' \ + ? (isSigned \ + ? functionName() \ + : functionName()) \ + : sizeTypeID == 'w' \ + ? (isSigned \ + ? (requiresAligned \ + ? functionName() \ + : functionName()) \ + : (requiresAligned \ + ? functionName() \ + : functionName())) \ + : sizeTypeID == 'd' \ + ? (isSigned \ + ? (requiresAligned \ + ? functionName() \ + : functionName()) \ + : (requiresAligned \ + ? functionName() \ + : functionName())) \ + : functionName()) + +#define CALL_WITH_T_SIZE_TYPES_1(functionName, sizeTypeID, isSigned, requiresAligned, p0) \ + (sizeTypeID == 'b' \ + ? (isSigned \ + ? functionName(p0) \ + : functionName(p0)) \ + : sizeTypeID == 'w' \ + ? (isSigned \ + ? (requiresAligned \ + ? functionName(p0) \ + : functionName(p0)) \ + : (requiresAligned \ + ? functionName(p0) \ + : functionName(p0))) \ + : sizeTypeID == 'd' \ + ? (isSigned \ + ? (requiresAligned \ + ? functionName(p0) \ + : functionName(p0)) \ + : (requiresAligned \ + ? functionName(p0) \ + : functionName(p0))) \ + : functionName(p0)) + +#define CALL_WITH_T_SIZE_TYPES_3(functionName, sizeTypeID, isSigned, requiresAligned, p0, p1, p2) \ + (sizeTypeID == 'b' \ + ? (isSigned \ + ? functionName(p0, p1, p2) \ + : functionName(p0, p1, p2)) \ + : sizeTypeID == 'w' \ + ? (isSigned \ + ? (requiresAligned \ + ? functionName(p0, p1, p2) \ + : functionName(p0, p1, p2)) \ + : (requiresAligned \ + ? functionName(p0, p1, p2) \ + : functionName(p0, p1, p2))) \ + : sizeTypeID == 'd' \ + ? (isSigned \ + ? (requiresAligned \ + ? functionName(p0, p1, p2) \ + : functionName(p0, p1, p2)) \ + : (requiresAligned \ + ? functionName(p0, p1, p2) \ + : functionName(p0, p1, p2))) \ + : functionName(p0, p1, p2)) + +#define CALL_WITH_T_SIZE_TYPES_4(functionName, sizeTypeID, isSigned, requiresAligned, p0, p1, p2, p3) \ + (sizeTypeID == 'b' \ + ? (isSigned \ + ? functionName(p0, p1, p2, p3) \ + : functionName(p0, p1, p2, p3)) \ + : sizeTypeID == 'w' \ + ? (isSigned \ + ? (requiresAligned \ + ? functionName(p0, p1, p2, p3) \ + : functionName(p0, p1, p2, p3)) \ + : (requiresAligned \ + ? functionName(p0, p1, p2, p3) \ + : functionName(p0, p1, p2, p3))) \ + : sizeTypeID == 'd' \ + ? (isSigned \ + ? (requiresAligned \ + ? functionName(p0, p1, p2, p3) \ + : functionName(p0, p1, p2, p3)) \ + : (requiresAligned \ + ? functionName(p0, p1, p2, p3) \ + : functionName(p0, p1, p2, p3))) \ + : functionName(p0, p1, p2, p3)) + +// version that takes a forced comparison type +#define CALL_WITH_T_STEP_3(functionName, sizeTypeID, type, requiresAligned, p0, p1, p2) \ + (sizeTypeID == 'b' \ + ? functionName(p0, p1, p2) \ + : sizeTypeID == 'w' \ + ? (requiresAligned \ + ? functionName(p0, p1, p2) \ + : functionName(p0, p1, p2)) \ + : sizeTypeID == 'd' \ + ? (requiresAligned \ + ? functionName(p0, p1, p2) \ + : functionName(p0, p1, p2)) \ + : functionName(p0, p1, p2)) + +// version that takes a forced comparison type +#define CALL_WITH_T_STEP_4(functionName, sizeTypeID, type, requiresAligned, p0, p1, p2, p3) \ + (sizeTypeID == 'b' \ + ? functionName(p0, p1, p2, p3) \ + : sizeTypeID == 'w' \ + ? (requiresAligned \ + ? functionName(p0, p1, p2, p3) \ + : functionName(p0, p1, p2, p3)) \ + : sizeTypeID == 'd' \ + ? (requiresAligned \ + ? functionName(p0, p1, p2, p3) \ + : functionName(p0, p1, p2, p3)) \ + : functionName(p0, p1, p2, p3)) + +// basic comparison functions: +template inline bool LessCmp (T x, T y, T i) { return x < y; } +template inline bool MoreCmp (T x, T y, T i) { return x > y; } +template inline bool LessEqualCmp (T x, T y, T i) { return x <= y; } +template inline bool MoreEqualCmp (T x, T y, T i) { return x >= y; } +template inline bool EqualCmp (T x, T y, T i) { return x == y; } +template inline bool UnequalCmp (T x, T y, T i) { return x != y; } +template inline bool DiffByCmp (T x, T y, T p) { return x - y == p || y - x == p; } +template inline bool ModIsCmp (T x, T y, T p) { return p && x % p == y; } + +// compare-to type functions: +template +void SearchRelative (bool(*cmpFun)(T,T,T), T ignored, T param) +{ + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ) + { + MemoryRegion& region = *iter; + int startSkipSize = ((unsigned int)(sizeof(stepType) - region.hardwareAddress)) % sizeof(stepType); + unsigned int start = region.virtualIndex + startSkipSize; + unsigned int end = region.virtualIndex + region.size; + for(unsigned int i = start, hwaddr = region.hardwareAddress; i < end; i += sizeof(stepType), hwaddr += sizeof(stepType)) + if(!cmpFun(GetCurValueFromVirtualIndex(i), GetPrevValueFromVirtualIndex(i), param)) + if(2 == DeactivateRegion(region, iter, hwaddr, sizeof(stepType))) + goto outerContinue; + ++iter; +outerContinue: + continue; + } +} +template +void SearchSpecific (bool(*cmpFun)(T,T,T), T value, T param) +{ + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ) + { + MemoryRegion& region = *iter; + int startSkipSize = ((unsigned int)(sizeof(stepType) - region.hardwareAddress)) % sizeof(stepType); + unsigned int start = region.virtualIndex + startSkipSize; + unsigned int end = region.virtualIndex + region.size; + for(unsigned int i = start, hwaddr = region.hardwareAddress; i < end; i += sizeof(stepType), hwaddr += sizeof(stepType)) + if(!cmpFun(GetCurValueFromVirtualIndex(i), value, param)) + if(2 == DeactivateRegion(region, iter, hwaddr, sizeof(stepType))) + goto outerContinue; + ++iter; +outerContinue: + continue; + } +} +template +void SearchAddress (bool(*cmpFun)(T,T,T), T address, T param) +{ + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ) + { + MemoryRegion& region = *iter; + int startSkipSize = ((unsigned int)(sizeof(stepType) - region.hardwareAddress)) % sizeof(stepType); + unsigned int start = region.virtualIndex + startSkipSize; + unsigned int end = region.virtualIndex + region.size; + for(unsigned int i = start, hwaddr = region.hardwareAddress; i < end; i += sizeof(stepType), hwaddr += sizeof(stepType)) + if(!cmpFun(hwaddr, address, param)) + if(2 == DeactivateRegion(region, iter, hwaddr, sizeof(stepType))) + goto outerContinue; + ++iter; +outerContinue: + continue; + } +} +template +void SearchChanges (bool(*cmpFun)(T,T,T), T changes, T param) +{ + for(MemoryList::iterator iter = s_activeMemoryRegions.begin(); iter != s_activeMemoryRegions.end(); ) + { + MemoryRegion& region = *iter; + int startSkipSize = ((unsigned int)(sizeof(stepType) - region.hardwareAddress)) % sizeof(stepType); + unsigned int start = region.virtualIndex + startSkipSize; + unsigned int end = region.virtualIndex + region.size; + for(unsigned int i = start, hwaddr = region.hardwareAddress; i < end; i += sizeof(stepType), hwaddr += sizeof(stepType)) + if(!cmpFun(GetNumChangesFromVirtualIndex(i), changes, param)) + if(2 == DeactivateRegion(region, iter, hwaddr, sizeof(stepType))) + goto outerContinue; + ++iter; +outerContinue: + continue; + } +} + +char rs_c='s'; +char rs_o='='; +char rs_t='s'; +int rs_param=0, rs_val=0, rs_val_valid=0; +char rs_type_size = 'b', rs_last_type_size = rs_type_size; +bool noMisalign = true, rs_last_no_misalign = noMisalign; +//bool littleEndian = false; +int last_rs_possible = -1; +int last_rs_regions = -1; + +void prune(char c,char o,char t,int v,int p) +{ + // repetition-reducing macros + #define DO_SEARCH(sf) \ + switch (o) \ + { \ + case '<': DO_SEARCH_2(LessCmp,sf); break; \ + case '>': DO_SEARCH_2(MoreCmp,sf); break; \ + case '=': DO_SEARCH_2(EqualCmp,sf); break; \ + case '!': DO_SEARCH_2(UnequalCmp,sf); break; \ + case 'l': DO_SEARCH_2(LessEqualCmp,sf); break; \ + case 'm': DO_SEARCH_2(MoreEqualCmp,sf); break; \ + case 'd': DO_SEARCH_2(DiffByCmp,sf); break; \ + case '%': DO_SEARCH_2(ModIsCmp,sf); break; \ + default: assert(!"Invalid operator for this search type."); break; \ + } + + // perform the search, eliminating nonmatching values + switch (c) + { + #define DO_SEARCH_2(CmpFun,sf) CALL_WITH_T_SIZE_TYPES_3(sf, rs_type_size, t, noMisalign, CmpFun,v,p) + case 'r': DO_SEARCH(SearchRelative); break; + case 's': DO_SEARCH(SearchSpecific); break; + + #undef DO_SEARCH_2 + #define DO_SEARCH_2(CmpFun,sf) CALL_WITH_T_STEP_3(sf, rs_type_size, unsigned int, noMisalign, CmpFun,v,p) + case 'a': DO_SEARCH(SearchAddress); break; + + #undef DO_SEARCH_2 + #define DO_SEARCH_2(CmpFun,sf) CALL_WITH_T_STEP_3(sf, rs_type_size, unsigned short, noMisalign, CmpFun,v,p) + case 'n': DO_SEARCH(SearchChanges); break; + + default: assert(!"Invalid search comparison type."); break; + } + + s_prevValuesNeedUpdate = true; + + int prevNumItems = last_rs_possible; + + CompactAddrs(); + + if(prevNumItems == last_rs_possible) + { + SetRamSearchUndoType(RamSearchHWnd, 0); // nothing to undo + } +} + + + + +template +bool CompareRelativeAtItem (bool(*cmpFun)(T,T,T), int itemIndex, T ignored, T param) +{ + return cmpFun(GetCurValueFromItemIndex(itemIndex), GetPrevValueFromItemIndex(itemIndex), param); +} +template +bool CompareSpecificAtItem (bool(*cmpFun)(T,T,T), int itemIndex, T value, T param) +{ + return cmpFun(GetCurValueFromItemIndex(itemIndex), value, param); +} +template +bool CompareAddressAtItem (bool(*cmpFun)(T,T,T), int itemIndex, T address, T param) +{ + return cmpFun(GetHardwareAddressFromItemIndex(itemIndex), address, param); +} +template +bool CompareChangesAtItem (bool(*cmpFun)(T,T,T), int itemIndex, T changes, T param) +{ + return cmpFun(GetNumChangesFromItemIndex(itemIndex), changes, param); +} + +int ReadControlInt(int controlID, bool forceHex, BOOL& success) +{ + int rv = 0; + BOOL ok = false; + + if(!forceHex) + { + rv = GetDlgItemInt(RamSearchHWnd,controlID,&ok,(rs_t == 's')); + } + + if(!ok) + { + if(GetDlgItemText(RamSearchHWnd,controlID,Str_Tmp,16)) + { + for(int i = 0; Str_Tmp[i]; i++) {if(toupper(Str_Tmp[i]) == 'O') Str_Tmp[i] = '0';} + const char* strPtr = Str_Tmp; + bool negate = false; + while(strPtr[0] == '-') + strPtr++, negate = !negate; + if(strPtr[0] == '+') + strPtr++; + if(strPtr[0] == '0' && tolower(strPtr[1]) == 'x') + strPtr += 2, forceHex = true; + if(strPtr[0] == '$') + strPtr++, forceHex = true; + if(!forceHex) + { + const char* strSearchPtr = strPtr; + while(*strSearchPtr) + { + int c = tolower(*strSearchPtr++); + if(c >= 'a' && c <= 'f') + forceHex = true; + } + } + const char* formatString = forceHex ? "%X" : ((rs_t=='s') ? "%d" : "%u"); + if(sscanf(strPtr, formatString, &rv) > 0) + ok = true; + if(negate) + rv = -rv; + } + } + + success = ok; + return rv; +} + + +bool Set_RS_Val() +{ + BOOL success; + + // update rs_val + switch(rs_c) + { + case 'r': + default: + rs_val = 0; + break; + case 's': + rs_val = ReadControlInt(IDC_EDIT_COMPAREVALUE, rs_t == 'h', success); + if(!success) + return false; + if((rs_type_size == 'b' && rs_t == 's' && (rs_val < -128 || rs_val > 127)) || + (rs_type_size == 'b' && rs_t != 's' && (rs_val < 0 || rs_val > 255)) || + (rs_type_size == 'w' && rs_t == 's' && (rs_val < -32768 || rs_val > 32767)) || + (rs_type_size == 'w' && rs_t != 's' && (rs_val < 0 || rs_val > 65535))) + return false; + break; + case 'a': + rs_val = ReadControlInt(IDC_EDIT_COMPAREADDRESS, true, success); + if(!success || rs_val < 0 || rs_val > 0x06040000) + return false; + break; + case 'n': { + rs_val = ReadControlInt(IDC_EDIT_COMPARECHANGES, false, success); + if(!success || rs_val < 0 || rs_val > 0xFFFF) + return false; + } break; + } + + // also update rs_param + switch(rs_o) + { + default: + rs_param = 0; + break; + case 'd': + rs_param = ReadControlInt(IDC_EDIT_DIFFBY, false, success); + if(!success) + return false; + if(rs_param < 0) + rs_param = -rs_param; + break; + case '%': + rs_param = ReadControlInt(IDC_EDIT_MODBY, false, success); + if(!success || rs_param == 0) + return false; + break; + } + + // validate that rs_param fits in the comparison data type + { + int appliedSize = rs_type_size; + int appliedSign = rs_t; + if(rs_c == 'n') + appliedSize = 'w', appliedSign = 'u'; + if(rs_c == 'a') + appliedSize = 'd', appliedSign = 'u'; + if((appliedSize == 'b' && appliedSize == 's' && (rs_param < -128 || rs_param > 127)) || + (appliedSize == 'b' && appliedSize != 's' && (rs_param < 0 || rs_param > 255)) || + (appliedSize == 'w' && appliedSize == 's' && (rs_param < -32768 || rs_param > 32767)) || + (appliedSize == 'w' && appliedSize != 's' && (rs_param < 0 || rs_param > 65535))) + return false; + } + + return true; +} + +bool IsSatisfied(int itemIndex) +{ + if(!rs_val_valid) + return true; + int o = rs_o; + switch (rs_c) + { + #undef DO_SEARCH_2 + #define DO_SEARCH_2(CmpFun,sf) return CALL_WITH_T_SIZE_TYPES_4(sf, rs_type_size,(rs_t=='s'),noMisalign, CmpFun,itemIndex,rs_val,rs_param); + case 'r': DO_SEARCH(CompareRelativeAtItem); break; + case 's': DO_SEARCH(CompareSpecificAtItem); break; + + #undef DO_SEARCH_2 + #define DO_SEARCH_2(CmpFun,sf) return CALL_WITH_T_STEP_4(sf, rs_type_size, unsigned int, noMisalign, CmpFun,itemIndex,rs_val,rs_param); + case 'a': DO_SEARCH(CompareAddressAtItem); break; + + #undef DO_SEARCH_2 + #define DO_SEARCH_2(CmpFun,sf) return CALL_WITH_T_STEP_4(sf, rs_type_size, unsigned short, noMisalign, CmpFun,itemIndex,rs_val,rs_param); + case 'n': DO_SEARCH(CompareChangesAtItem); break; + } + return false; +} + + + +unsigned int ReadValueAtSoftwareAddress(const unsigned char* address, unsigned int size) +{ + unsigned int value = 0; + if(address) + { + // assumes we're little-endian + memcpy(&value, address, size); + } + return value; +} +void WriteValueAtSoftwareAddress(unsigned char* address, unsigned int value, unsigned int size) +{ + if(address) + { + // assumes we're little-endian + memcpy(address, &value, size); + } +} +unsigned int ReadValueAtHardwareAddress(HWAddressType address, unsigned int size) +{ + return ReadValueAtSoftwareAddress(HardwareToSoftwareAddress(address), size); +} +bool WriteValueAtHardwareAddress(HWAddressType address, unsigned int value, unsigned int size) +{ + WriteValueAtSoftwareAddress(HardwareToSoftwareAddress(address), value, size); + return true; +} +bool IsHardwareAddressValid(HWAddressType address) +{ + return HardwareToSoftwareAddress(address) != NULL; +} + + + +int ResultCount=0; +bool AutoSearch=false; +bool AutoSearchAutoRetry=false; +LRESULT CALLBACK PromptWatchNameProc(HWND, UINT, WPARAM, LPARAM); +void UpdatePossibilities(int rs_possible, int regions); + + +void CompactAddrs() +{ + int size = (rs_type_size=='b' || !noMisalign) ? 1 : 2; + int prevResultCount = ResultCount; + + CalculateItemIndices(size); + ResultCount = CALL_WITH_T_SIZE_TYPES_0(CountRegionItemsT, rs_type_size,rs_t=='s',noMisalign); + + UpdatePossibilities(ResultCount, (int)s_activeMemoryRegions.size()); + + if(ResultCount != prevResultCount) + ListView_SetItemCount(GetDlgItem(RamSearchHWnd,IDC_RAMLIST),ResultCount); +} + +void soft_reset_address_info () +{ + s_prevValuesNeedUpdate = false; + ResetMemoryRegions(); + if(!RamSearchHWnd) + { + EnterCriticalSection(&s_activeMemoryRegionsCS); + s_activeMemoryRegions.clear(); + LeaveCriticalSection(&s_activeMemoryRegionsCS); + ResultCount = 0; + } + else + { + // force s_prevValues to be valid + signal_new_frame(); + s_prevValuesNeedUpdate = true; + signal_new_frame(); + } + if(s_numChanges) + memset(s_numChanges, 0, (sizeof(*s_numChanges)*(MAX_RAM_SIZE))); + CompactAddrs(); +} +void reset_address_info () +{ + SetRamSearchUndoType(RamSearchHWnd, 0); + EnterCriticalSection(&s_activeMemoryRegionsCS); + s_activeMemoryRegionsBackup.clear(); // not necessary, but we'll take the time hit here instead of at the next thing that sets up an undo + LeaveCriticalSection(&s_activeMemoryRegionsCS); + if(s_prevValues) + memcpy(s_prevValues, s_curValues, (sizeof(*s_prevValues)*(MAX_RAM_SIZE))); + s_prevValuesNeedUpdate = false; + ResetMemoryRegions(); + if(!RamSearchHWnd) + { + EnterCriticalSection(&s_activeMemoryRegionsCS); + s_activeMemoryRegions.clear(); + LeaveCriticalSection(&s_activeMemoryRegionsCS); + ResultCount = 0; + } + else + { + // force s_prevValues to be valid + signal_new_frame(); + s_prevValuesNeedUpdate = true; + signal_new_frame(); + } + memset(s_numChanges, 0, (sizeof(*s_numChanges)*(MAX_RAM_SIZE))); + CompactAddrs(); +} + +void signal_new_frame () +{ + EnterCriticalSection(&s_activeMemoryRegionsCS); + CALL_WITH_T_SIZE_TYPES_0(UpdateRegionsT, rs_type_size, rs_t=='s', noMisalign); + LeaveCriticalSection(&s_activeMemoryRegionsCS); +} + + + + + +bool RamSearchClosed = false; +bool RamWatchClosed = false; + +void ResetResults() +{ + reset_address_info(); + ResultCount = 0; + if (RamSearchHWnd) + ListView_SetItemCount(GetDlgItem(RamSearchHWnd,IDC_RAMLIST),ResultCount); +} +void CloseRamWindows() //Close the Ram Search & Watch windows when rom closes +{ + ResetWatches(); + ResetResults(); + if (RamSearchHWnd) + { + SendMessage(RamSearchHWnd,WM_CLOSE,NULL,NULL); + RamSearchClosed = true; + } + if (RamWatchHWnd) + { + SendMessage(RamWatchHWnd,WM_CLOSE,NULL,NULL); + RamWatchClosed = true; + } +} +void ReopenRamWindows() //Reopen them when a new Rom is loaded +{ + HWND hwnd = GetActiveWindow(); + + if (RamSearchClosed) + { + RamSearchClosed = false; + if(!RamSearchHWnd) + { + reset_address_info(); + LRESULT CALLBACK RamSearchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); + RamSearchHWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_RAMSEARCH), hWnd, (DLGPROC) RamSearchProc); + } + else + ::SetForegroundWindow(RamSearchHWnd); + } + if (RamWatchClosed || AutoRWLoad) + { + RamWatchClosed = false; + if(!RamWatchHWnd) + { + if (AutoRWLoad) OpenRWRecentFile(0); + LRESULT CALLBACK RamWatchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); + RamWatchHWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_RAMWATCH), hWnd, (DLGPROC) RamWatchProc); + } + else + ::SetForegroundWindow(RamWatchHWnd); + } + + if (hwnd == hWnd && hwnd != GetActiveWindow()) + SetActiveWindow(hWnd); // restore focus to the main window if it had it before +} + + + + + +void RefreshRamListSelectedCountControlStatus(HWND hDlg) +{ + int selCount = ListView_GetSelectedCount(GetDlgItem(hDlg,IDC_RAMLIST)); + if(selCount != s_prevSelCount) + { + if(selCount < 2 || s_prevSelCount < 2) + { + EnableWindow(GetDlgItem(hDlg, IDC_C_WATCH), (WatchCount < MAX_WATCH_COUNT) ? TRUE : FALSE); + EnableWindow(GetDlgItem(hDlg, IDC_C_ADDCHEAT), (selCount >= 1) ? TRUE : FALSE); + EnableWindow(GetDlgItem(hDlg, IDC_C_ELIMINATE), (selCount >= 1) ? TRUE : FALSE); + } + s_prevSelCount = selCount; + } +} + + + + +struct AddrRange +{ + unsigned int addr; + unsigned int size; + unsigned int End() const { return addr + size; } + AddrRange(unsigned int a, unsigned int s) : addr(a),size(s){} +}; + +void signal_new_size () +{ + HWND lv = GetDlgItem(RamSearchHWnd,IDC_RAMLIST); + + int oldSize = (rs_last_type_size=='b' || !rs_last_no_misalign) ? 1 : 2; + int newSize = (rs_type_size=='b' || !noMisalign) ? 1 : 2; + bool numberOfItemsChanged = (oldSize != newSize); + + unsigned int itemsPerPage = ListView_GetCountPerPage(lv); + unsigned int oldTopIndex = ListView_GetTopIndex(lv); + unsigned int oldSelectionIndex = ListView_GetSelectionMark(lv); + unsigned int oldTopAddr = CALL_WITH_T_SIZE_TYPES_1(GetHardwareAddressFromItemIndex, rs_last_type_size,rs_t=='s',rs_last_no_misalign, oldTopIndex); + unsigned int oldSelectionAddr = CALL_WITH_T_SIZE_TYPES_1(GetHardwareAddressFromItemIndex, rs_last_type_size,rs_t=='s',rs_last_no_misalign, oldSelectionIndex); + + std::vector selHardwareAddrs; + if(numberOfItemsChanged) + { + // store selection ranges + // unfortunately this can take a while if the user has a huge range of items selected + systemSoundClearBuffer(); + int selCount = ListView_GetSelectedCount(lv); + int size = (rs_last_type_size=='b' || !rs_last_no_misalign) ? 1 : 2; + int watchIndex = -1; + for(int i = 0; i < selCount; ++i) + { + watchIndex = ListView_GetNextItem(lv, watchIndex, LVNI_SELECTED); + int addr = CALL_WITH_T_SIZE_TYPES_1(GetHardwareAddressFromItemIndex, rs_last_type_size,rs_t=='s',rs_last_no_misalign, watchIndex); + if(!selHardwareAddrs.empty() && addr == selHardwareAddrs.back().End()) + selHardwareAddrs.back().size += size; + else if (!(noMisalign && oldSize < newSize && addr % newSize != 0)) + selHardwareAddrs.push_back(AddrRange(addr,size)); + } + } + + CompactAddrs(); + + rs_last_type_size = rs_type_size; + rs_last_no_misalign = noMisalign; + + if(numberOfItemsChanged) + { + // restore selection ranges + unsigned int newTopIndex = CALL_WITH_T_SIZE_TYPES_1(HardwareAddressToItemIndex, rs_type_size,rs_t=='s',noMisalign, oldTopAddr); + unsigned int newBottomIndex = newTopIndex + itemsPerPage - 1; + SendMessage(lv, WM_SETREDRAW, FALSE, 0); + ListView_SetItemState(lv, -1, 0, LVIS_SELECTED|LVIS_FOCUSED); // deselect all + for(unsigned int i = 0; i < selHardwareAddrs.size(); i++) + { + // calculate index ranges of this selection + const AddrRange& range = selHardwareAddrs[i]; + int selRangeTop = CALL_WITH_T_SIZE_TYPES_1(HardwareAddressToItemIndex, rs_type_size,rs_t=='s',noMisalign, range.addr); + int selRangeBottom = -1; + for(int endAddr = range.End()-1; endAddr >= selRangeTop && selRangeBottom == -1; endAddr--) + selRangeBottom = CALL_WITH_T_SIZE_TYPES_1(HardwareAddressToItemIndex, rs_type_size,rs_t=='s',noMisalign, endAddr); + if(selRangeBottom == -1) + selRangeBottom = selRangeTop; + if(selRangeTop == -1) + continue; + + // select the entire range + for (int j = selRangeTop; j <= selRangeBottom; j++) + { + ListView_SetItemState(lv, j, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); + } + } + + // restore previous scroll position + if(newBottomIndex != -1) + ListView_EnsureVisible(lv, newBottomIndex, 0); + if(newTopIndex != -1) + ListView_EnsureVisible(lv, newTopIndex, 0); + + SendMessage(lv, WM_SETREDRAW, TRUE, 0); + + RefreshRamListSelectedCountControlStatus(RamSearchHWnd); + + EnableWindow(GetDlgItem(RamSearchHWnd,IDC_MISALIGN), rs_type_size != 'b'); + } + else + { + ListView_Update(lv, -1); + } + InvalidateRect(lv, NULL, TRUE); + //SetFocus(lv); +} + + + + +LRESULT CustomDraw (LPARAM lParam) +{ + LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam; + + switch(lplvcd->nmcd.dwDrawStage) + { + case CDDS_PREPAINT : + return CDRF_NOTIFYITEMDRAW; + + case CDDS_ITEMPREPAINT: + { + int rv = CDRF_DODEFAULT; + + if(lplvcd->nmcd.dwItemSpec % 2) + { + // alternate the background color slightly + lplvcd->clrTextBk = RGB(248,248,255); + rv = CDRF_NEWFONT; + } + + if(!IsSatisfied(lplvcd->nmcd.dwItemSpec)) + { + // tint red any items that would be eliminated if a search were to run now + lplvcd->clrText = RGB(192,64,64); + rv = CDRF_NEWFONT; + } + + return rv; + } break; + } + return CDRF_DODEFAULT; +} + +void Update_RAM_Search() //keeps RAM values up to date in the search and watch windows +{ + if(disableRamSearchUpdate) + return; + + int prevValuesNeededUpdate; + if (AutoSearch && !ResultCount) + { + if(!AutoSearchAutoRetry) + { + systemSoundClearBuffer(); + int answer = MessageBox(RamSearchHWnd,"Choosing Retry will reset the search once and continue autosearching.\nChoose Ignore will reset the search whenever necessary and continue autosearching.\nChoosing Abort will reset the search once and stop autosearching.","Autosearch - out of results.",MB_ABORTRETRYIGNORE|MB_DEFBUTTON2|MB_ICONINFORMATION); + if(answer == IDABORT) + { + SendDlgItemMessage(RamSearchHWnd, IDC_C_AUTOSEARCH, BM_SETCHECK, BST_UNCHECKED, 0); + SendMessage(RamSearchHWnd, WM_COMMAND, IDC_C_AUTOSEARCH, 0); + } + if(answer == IDIGNORE) + AutoSearchAutoRetry = true; + } + reset_address_info(); + prevValuesNeededUpdate = s_prevValuesNeedUpdate; + } + else + { + prevValuesNeededUpdate = s_prevValuesNeedUpdate; + if (RamSearchHWnd) + { + // update active RAM values + signal_new_frame(); + } + + if (AutoSearch && ResultCount) + { + systemSoundClearBuffer(); + if(!rs_val_valid) + rs_val_valid = Set_RS_Val(); + if(rs_val_valid) + prune(rs_c,rs_o,rs_t=='s',rs_val,rs_param); + } + } + + if(RamSearchHWnd) + { + HWND lv = GetDlgItem(RamSearchHWnd,IDC_RAMLIST); + if(prevValuesNeededUpdate != s_prevValuesNeedUpdate) + { + // previous values got updated, refresh everything visible + ListView_Update(lv, -1); + } + else + { + // refresh any visible parts of the listview box that changed + static int changes[128]; + int top = ListView_GetTopIndex(lv); + int count = ListView_GetCountPerPage(lv); + int start = -1; + for(int i = top; i <= top+count; i++) + { + int changeNum = CALL_WITH_T_SIZE_TYPES_1(GetNumChangesFromItemIndex, rs_type_size,rs_t=='s',noMisalign, i); //s_numChanges[i]; + int changed = changeNum != changes[i-top]; + if(changed) + changes[i-top] = changeNum; + + if(start == -1) + { + if(i != top+count && changed) + { + start = i; + //somethingChanged = true; + } + } + else + { + if(i == top+count || !changed) + { + ListView_RedrawItems(lv, start, i-1); + start = -1; + } + } + } + } + } + + if(RamWatchHWnd) + { + Update_RAM_Watch(); + } +} + +static int rs_lastPercent = -1; +inline void UpdateRamSearchProgressBar(int percent) +{ + if(rs_lastPercent != percent) + { + rs_lastPercent = percent; + UpdateRamSearchTitleBar(percent); + } +} + +static void SelectEditControl(int controlID) +{ + HWND hEdit = GetDlgItem(RamSearchHWnd,controlID); + SetFocus(hEdit); + SendMessage(hEdit, EM_SETSEL, 0, -1); +} + +static BOOL SelectingByKeyboard() +{ + int a = GetKeyState(VK_LEFT); + int b = GetKeyState(VK_RIGHT); + int c = GetKeyState(VK_UP); + int d = GetKeyState(VK_DOWN); // space and tab are intentionally omitted + return (a | b | c | d) & 0x80; +} + + +LRESULT CALLBACK RamSearchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + static int watchIndex=0; + + switch(uMsg) + { + case WM_INITDIALOG: { + RamSearchHWnd = hDlg; + + SetWindowPos(hDlg, NULL, regQueryDwordValue("ramSearchX", 0), regQueryDwordValue("ramSearchY", 0), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + switch(rs_o) + { + case '<': + SendDlgItemMessage(hDlg, IDC_LESSTHAN, BM_SETCHECK, BST_CHECKED, 0); + break; + case '>': + SendDlgItemMessage(hDlg, IDC_MORETHAN, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'l': + SendDlgItemMessage(hDlg, IDC_NOMORETHAN, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'm': + SendDlgItemMessage(hDlg, IDC_NOLESSTHAN, BM_SETCHECK, BST_CHECKED, 0); + break; + case '=': + SendDlgItemMessage(hDlg, IDC_EQUALTO, BM_SETCHECK, BST_CHECKED, 0); + break; + case '!': + SendDlgItemMessage(hDlg, IDC_DIFFERENTFROM, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'd': + SendDlgItemMessage(hDlg, IDC_DIFFERENTBY, BM_SETCHECK, BST_CHECKED, 0); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),true); + break; + case '%': + SendDlgItemMessage(hDlg, IDC_MODULO, BM_SETCHECK, BST_CHECKED, 0); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),true); + break; + } + switch (rs_c) + { + case 'r': + SendDlgItemMessage(hDlg, IDC_PREVIOUSVALUE, BM_SETCHECK, BST_CHECKED, 0); + break; + case 's': + SendDlgItemMessage(hDlg, IDC_SPECIFICVALUE, BM_SETCHECK, BST_CHECKED, 0); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREVALUE),true); + break; + case 'a': + SendDlgItemMessage(hDlg, IDC_SPECIFICADDRESS, BM_SETCHECK, BST_CHECKED, 0); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREADDRESS),true); + break; + case 'n': + SendDlgItemMessage(hDlg, IDC_NUMBEROFCHANGES, BM_SETCHECK, BST_CHECKED, 0); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPARECHANGES),true); + break; + } + switch (rs_t) + { + case 's': + SendDlgItemMessage(hDlg, IDC_SIGNED, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'u': + SendDlgItemMessage(hDlg, IDC_UNSIGNED, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'h': + SendDlgItemMessage(hDlg, IDC_HEX, BM_SETCHECK, BST_CHECKED, 0); + break; + } + switch (rs_type_size) + { + case 'b': + SendDlgItemMessage(hDlg, IDC_1_BYTE, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'w': + SendDlgItemMessage(hDlg, IDC_2_BYTES, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'd': + SendDlgItemMessage(hDlg, IDC_4_BYTES, BM_SETCHECK, BST_CHECKED, 0); + break; + } + + s_prevValuesNeedUpdate = true; + + SendDlgItemMessage(hDlg,IDC_C_AUTOSEARCH,BM_SETCHECK,AutoSearch?BST_CHECKED:BST_UNCHECKED,0); + //const char* names[5] = {"Address","Value","Previous","Changes","Notes"}; + //int widths[5] = {62,64,64,55,55}; + const char* names[] = {"Address","Value","Previous","Changes"}; + int widths[4] = {68,76,76,68}; + if (!ResultCount) + reset_address_info(); + else + { + signal_new_frame(); + CompactAddrs(); + } + void init_list_box(HWND Box, const char* Strs[], int numColumns, int *columnWidths); + init_list_box(GetDlgItem(hDlg,IDC_RAMLIST),names,4,widths); + //ListView_SetItemCount(GetDlgItem(hDlg,IDC_RAMLIST),ResultCount); + if (!noMisalign) SendDlgItemMessage(hDlg, IDC_MISALIGN, BM_SETCHECK, BST_CHECKED, 0); + //if (littleEndian) SendDlgItemMessage(hDlg, IDC_ENDIAN, BM_SETCHECK, BST_CHECKED, 0); + last_rs_possible = -1; + + s_prevSelCount = -1; + RefreshRamListSelectedCountControlStatus(hDlg); + + // force misalign checkbox to refresh + signal_new_size(); + + // force undo button to refresh + int undoType = s_undoType; + SetRamSearchUndoType(hDlg, -2); + SetRamSearchUndoType(hDlg, undoType); + + // force possibility count to refresh + last_rs_possible--; + UpdatePossibilities(ResultCount, (int)s_activeMemoryRegions.size()); + + rs_val_valid = Set_RS_Val(); + + ListView_SetCallbackMask(GetDlgItem(hDlg,IDC_RAMLIST), LVIS_FOCUSED|LVIS_SELECTED); + + return true; + } break; + + case WM_NOTIFY: + { + LPNMHDR lP = (LPNMHDR) lParam; + switch (lP->code) + { + case LVN_ITEMCHANGED: // selection changed event + { + NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)lP; + if(pNMListView->uNewState & LVIS_FOCUSED || + (pNMListView->uNewState ^ pNMListView->uOldState) & LVIS_SELECTED) + { + // disable buttons that we don't have the right number of selected items for + RefreshRamListSelectedCountControlStatus(hDlg); + } + } break; + + case LVN_GETDISPINFO: + { + LV_DISPINFO *Item = (LV_DISPINFO *)lParam; + Item->item.mask = LVIF_TEXT; + Item->item.state = 0; + Item->item.iImage = 0; + const unsigned int iNum = Item->item.iItem; + static char num[11]; + switch (Item->item.iSubItem) + { + case 0: + { + int addr = CALL_WITH_T_SIZE_TYPES_1(GetHardwareAddressFromItemIndex, rs_type_size,rs_t=='s',noMisalign, iNum); + sprintf(num,"%08X",addr); + Item->item.pszText = num; + } return true; + case 1: + { + int i = CALL_WITH_T_SIZE_TYPES_1(GetCurValueFromItemIndex, rs_type_size,rs_t=='s',noMisalign, iNum); + const char* formatString = ((rs_t=='s') ? "%d" : (rs_t=='u') ? "%u" : (rs_type_size=='d' ? "%08X" : rs_type_size=='w' ? "%04X" : "%02X")); + switch (rs_type_size) + { + case 'b': + default: sprintf(num, formatString, rs_t=='s' ? (char)(i&0xff) : (unsigned char)(i&0xff)); break; + case 'w': sprintf(num, formatString, rs_t=='s' ? (short)(i&0xffff) : (unsigned short)(i&0xffff)); break; + case 'd': sprintf(num, formatString, rs_t=='s' ? (long)(i&0xffffffff) : (unsigned long)(i&0xffffffff)); break; + } + Item->item.pszText = num; + } return true; + case 2: + { + int i = CALL_WITH_T_SIZE_TYPES_1(GetPrevValueFromItemIndex, rs_type_size,rs_t=='s',noMisalign, iNum); + const char* formatString = ((rs_t=='s') ? "%d" : (rs_t=='u') ? "%u" : (rs_type_size=='d' ? "%08X" : rs_type_size=='w' ? "%04X" : "%02X")); + switch (rs_type_size) + { + case 'b': + default: sprintf(num, formatString, rs_t=='s' ? (char)(i&0xff) : (unsigned char)(i&0xff)); break; + case 'w': sprintf(num, formatString, rs_t=='s' ? (short)(i&0xffff) : (unsigned short)(i&0xffff)); break; + case 'd': sprintf(num, formatString, rs_t=='s' ? (long)(i&0xffffffff) : (unsigned long)(i&0xffffffff)); break; + } + Item->item.pszText = num; + } return true; + case 3: + { + int i = CALL_WITH_T_SIZE_TYPES_1(GetNumChangesFromItemIndex, rs_type_size,rs_t=='s',noMisalign, iNum); + sprintf(num,"%d",i); + + Item->item.pszText = num; + } return true; + //case 4: + // Item->item.pszText = rsaddrs[rsresults[iNum].Index].comment ? rsaddrs[rsresults[iNum].Index].comment : ""; + // return true; + default: + return false; + } + } + + case NM_CUSTOMDRAW: + { + SetWindowLong(hDlg, DWL_MSGRESULT, CustomDraw(lParam)); + return TRUE; + } break; + + //case LVN_ODCACHEHINT: //Copied this bit from the MSDN virtual listbox code sample. Eventually it should probably do something. + //{ + // LPNMLVCACHEHINT lpCacheHint = (LPNMLVCACHEHINT)lParam; + // return 0; + //} + //case LVN_ODFINDITEM: //Copied this bit from the MSDN virtual listbox code sample. Eventually it should probably do something. + //{ + // LPNMLVFINDITEM lpFindItem = (LPNMLVFINDITEM)lParam; + // return 0; + //} + } + } break; + + case WM_COMMAND: + { + int rv = false; + switch(LOWORD(wParam)) + { + case IDC_SIGNED: + rs_t='s'; + signal_new_size(); + {rv = true; break;} + case IDC_UNSIGNED: + rs_t='u'; + signal_new_size(); + {rv = true; break;} + case IDC_HEX: + rs_t='h'; + signal_new_size(); + {rv = true; break;} + case IDC_1_BYTE: + rs_type_size = 'b'; + signal_new_size(); + {rv = true; break;} + case IDC_2_BYTES: + rs_type_size = 'w'; + signal_new_size(); + {rv = true; break;} + case IDC_4_BYTES: + rs_type_size = 'd'; + signal_new_size(); + {rv = true; break;} + case IDC_MISALIGN: + noMisalign = !noMisalign; + //CompactAddrs(); + signal_new_size(); + {rv = true; break;} +// case IDC_ENDIAN: +//// littleEndian = !littleEndian; +//// signal_new_size(); +// {rv = true; break;} + case IDC_LESSTHAN: + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),false); + rs_o = '<'; + {rv = true; break;} + case IDC_MORETHAN: + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),false); + rs_o = '>'; + {rv = true; break;} + case IDC_NOMORETHAN: + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),false); + rs_o = 'l'; + {rv = true; break;} + case IDC_NOLESSTHAN: + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),false); + rs_o = 'm'; + {rv = true; break;} + case IDC_EQUALTO: + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),false); + rs_o = '='; + {rv = true; break;} + case IDC_DIFFERENTFROM: + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),false); + rs_o = '!'; + {rv = true; break;} + case IDC_DIFFERENTBY: + { + rs_o = 'd'; + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),true); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),false); + if(!SelectingByKeyboard()) + SelectEditControl(IDC_EDIT_DIFFBY); + } {rv = true; break;} + case IDC_MODULO: + { + rs_o = '%'; + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_DIFFBY),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_MODBY),true); + if(!SelectingByKeyboard()) + SelectEditControl(IDC_EDIT_MODBY); + } {rv = true; break;} + case IDC_PREVIOUSVALUE: + rs_c='r'; + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREVALUE),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREADDRESS),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPARECHANGES),false); + {rv = true; break;} + case IDC_SPECIFICVALUE: + { + rs_c = 's'; + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREVALUE),true); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREADDRESS),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPARECHANGES),false); + if(!SelectingByKeyboard()) + SelectEditControl(IDC_EDIT_COMPAREVALUE); + {rv = true; break;} + } + case IDC_SPECIFICADDRESS: + { + rs_c = 'a'; + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREADDRESS),true); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREVALUE),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPARECHANGES),false); + if(!SelectingByKeyboard()) + SelectEditControl(IDC_EDIT_COMPAREADDRESS); + } {rv = true; break;} + case IDC_NUMBEROFCHANGES: + { + rs_c = 'n'; + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPARECHANGES),true); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREVALUE),false); + EnableWindow(GetDlgItem(hDlg,IDC_EDIT_COMPAREADDRESS),false); + if(!SelectingByKeyboard()) + SelectEditControl(IDC_EDIT_COMPARECHANGES); + } {rv = true; break;} + case IDC_C_ADDCHEAT: + { + HWND ramListControl = GetDlgItem(hDlg,IDC_RAMLIST); + int watchItemIndex = ListView_GetNextItem(ramListControl, -1, LVNI_SELECTED); + while (watchItemIndex >= 0) + { + unsigned long address = CALL_WITH_T_SIZE_TYPES_1(GetHardwareAddressFromItemIndex, rs_type_size,rs_t=='s',noMisalign, watchItemIndex); + + int sizeType = -1; + if(rs_type_size == 'b') + sizeType = 0; + else if(rs_type_size == 'w') + sizeType = 1; + else if(rs_type_size == 'd') + sizeType = 2; + + int numberType = -1; + if(rs_t == 's') + numberType = 0; + else if(rs_t == 'u') + numberType = 1; + else if(rs_t == 'h') + numberType = 2; + + if(systemCartridgeType == 0) + { + AddCheat dlg (address/*, hDlg*/); + if(sizeType != -1) dlg.sizeType = sizeType; + if(numberType != -1) dlg.numberType = numberType; + dlg.DoModal(); + } + else + { + AddGBCheat dlg (address/*, hDlg*/); + if(sizeType != -1) dlg.sizeType = sizeType; + if(numberType != -1) dlg.numberType = numberType; + dlg.DoModal(); + } + watchItemIndex = ListView_GetNextItem(ramListControl, watchItemIndex, LVNI_SELECTED); + } + } {rv = true; break;} + case IDC_C_RESET: + { + RamSearchSaveUndoStateIfNotTooBig(RamSearchHWnd); + int prevNumItems = last_rs_possible; + + soft_reset_address_info(); + + if(prevNumItems == last_rs_possible) + SetRamSearchUndoType(RamSearchHWnd, 0); // nothing to undo + + ListView_SetItemState(GetDlgItem(hDlg,IDC_RAMLIST), -1, 0, LVIS_SELECTED); // deselect all + //ListView_SetItemCount(GetDlgItem(hDlg,IDC_RAMLIST),ResultCount); + ListView_SetSelectionMark(GetDlgItem(hDlg,IDC_RAMLIST), 0); + RefreshRamListSelectedCountControlStatus(hDlg); + {rv = true; break;} + } + case IDC_C_RESET_CHANGES: + memset(s_numChanges, 0, (sizeof(*s_numChanges)*(MAX_RAM_SIZE))); + ListView_Update(GetDlgItem(hDlg,IDC_RAMLIST), -1); + //SetRamSearchUndoType(hDlg, 0); + {rv = true; break;} + case IDC_C_UNDO: + if(s_undoType>0) + { + systemSoundClearBuffer(); + EnterCriticalSection(&s_activeMemoryRegionsCS); + if(s_activeMemoryRegions.size() < tooManyRegionsForUndo) + { + MemoryList tempMemoryList = s_activeMemoryRegions; + s_activeMemoryRegions = s_activeMemoryRegionsBackup; + s_activeMemoryRegionsBackup = tempMemoryList; + LeaveCriticalSection(&s_activeMemoryRegionsCS); + SetRamSearchUndoType(hDlg, 3 - s_undoType); + } + else + { + s_activeMemoryRegions = s_activeMemoryRegionsBackup; + LeaveCriticalSection(&s_activeMemoryRegionsCS); + SetRamSearchUndoType(hDlg, -1); + } + CompactAddrs(); + ListView_SetItemState(GetDlgItem(hDlg,IDC_RAMLIST), -1, 0, LVIS_SELECTED); // deselect all + ListView_SetSelectionMark(GetDlgItem(hDlg,IDC_RAMLIST), 0); + RefreshRamListSelectedCountControlStatus(hDlg); + } + {rv = true; break;} + case IDC_C_AUTOSEARCH: + AutoSearch = SendDlgItemMessage(hDlg, IDC_C_AUTOSEARCH, BM_GETCHECK, 0, 0) != 0; + AutoSearchAutoRetry = false; + if (!AutoSearch) {rv = true; break;} + case IDC_C_SEARCH: + { + systemSoundClearBuffer(); + + if(!rs_val_valid && !(rs_val_valid = Set_RS_Val())) + goto invalid_field; + + if(ResultCount) + { + RamSearchSaveUndoStateIfNotTooBig(hDlg); + + prune(rs_c,rs_o,rs_t=='s',rs_val,rs_param); + + RefreshRamListSelectedCountControlStatus(hDlg); + } + + if(!ResultCount) + { + + MessageBox(RamSearchHWnd,"Resetting search.","Out of results.",MB_OK|MB_ICONINFORMATION); + soft_reset_address_info(); + } + + {rv = true; break;} + +invalid_field: + MessageBox(RamSearchHWnd,"Invalid or out-of-bound entered value.","Error",MB_OK|MB_ICONSTOP); + if(AutoSearch) // stop autosearch if it just started + { + SendDlgItemMessage(hDlg, IDC_C_AUTOSEARCH, BM_SETCHECK, BST_UNCHECKED, 0); + SendMessage(hDlg, WM_COMMAND, IDC_C_AUTOSEARCH, 0); + } + {rv = true; break;} + } + case IDC_C_WATCH: + { + HWND ramListControl = GetDlgItem(hDlg,IDC_RAMLIST); + int selCount = ListView_GetSelectedCount(ramListControl); + + bool inserted = false; + int watchItemIndex = ListView_GetNextItem(ramListControl, -1, LVNI_SELECTED); + while (watchItemIndex >= 0) + { + AddressWatcher tempWatch; + tempWatch.Address = CALL_WITH_T_SIZE_TYPES_1(GetHardwareAddressFromItemIndex, rs_type_size,rs_t=='s',noMisalign, watchItemIndex); + tempWatch.Size = rs_type_size; + tempWatch.Type = rs_t; + tempWatch.WrongEndian = 0; //Replace when I get little endian working + tempWatch.comment = NULL; + + if (selCount == 1) + inserted |= InsertWatch(tempWatch, hDlg); + else + inserted |= InsertWatch(tempWatch, ""); + + watchItemIndex = ListView_GetNextItem(ramListControl, watchItemIndex, LVNI_SELECTED); + } + // bring up the ram watch window if it's not already showing so the user knows where the watch went + if(inserted && !RamWatchHWnd) + SendMessage(hWnd, WM_COMMAND, ID_RAM_WATCH, 0); + SetForegroundWindow(RamSearchHWnd); + {rv = true; break;} + } + + // eliminate all selected items + case IDC_C_ELIMINATE: + { + RamSearchSaveUndoStateIfNotTooBig(hDlg); + + HWND ramListControl = GetDlgItem(hDlg,IDC_RAMLIST); + int size = (rs_type_size=='b' || !noMisalign) ? 1 : 2; + int selCount = ListView_GetSelectedCount(ramListControl); + int watchIndex = -1; + + // time-saving trick #1: + // condense the selected items into an array of address ranges + std::vector selHardwareAddrs; + for(int i = 0, j = 1024; i < selCount; ++i, --j) + { + watchIndex = ListView_GetNextItem(ramListControl, watchIndex, LVNI_SELECTED); + int addr = CALL_WITH_T_SIZE_TYPES_1(GetHardwareAddressFromItemIndex, rs_type_size,rs_t=='s',noMisalign, watchIndex); + if(!selHardwareAddrs.empty() && addr == selHardwareAddrs.back().End()) + selHardwareAddrs.back().size += size; + else + selHardwareAddrs.push_back(AddrRange(addr,size)); + + if(!j) UpdateRamSearchProgressBar(i * 50 / selCount), j = 1024; + } + + // now deactivate the ranges + + // time-saving trick #2: + // take advantage of the fact that the listbox items must be in the same order as the regions + MemoryList::iterator iter = s_activeMemoryRegions.begin(); + int numHardwareAddrRanges = selHardwareAddrs.size(); + for(int i = 0, j = 16; i < numHardwareAddrRanges; ++i, --j) + { + int addr = selHardwareAddrs[i].addr; + int size = selHardwareAddrs[i].size; + bool affected = false; + while(iter != s_activeMemoryRegions.end()) + { + MemoryRegion& region = *iter; + int affNow = DeactivateRegion(region, iter, addr, size); + if(affNow) + affected = true; + else if(affected) + break; + if(affNow != 2) + ++iter; + } + + if(!j) UpdateRamSearchProgressBar(50 + (i * 50 / selCount)), j = 16; + } + UpdateRamSearchTitleBar(); + + // careful -- if the above two time-saving tricks aren't working, + // the runtime can absolutely explode (seconds -> hours) when there are lots of regions + + ListView_SetItemState(ramListControl, -1, 0, LVIS_SELECTED); // deselect all + signal_new_size(); + {rv = true; break;} + } + //case IDOK: + case IDCANCEL: + RamSearchHWnd = NULL; +/* if (theApp.pauseDuringCheatSearch) + EndDialog(hDlg, true); // this should never be called on a modeless dialog + else +*/ + DestroyWindow(hDlg); + {rv = true; break;} + } + + // check refresh for comparison preview color update + // also, update rs_val if needed + bool needRefresh = false; + switch(LOWORD(wParam)) + { + case IDC_LESSTHAN: + case IDC_MORETHAN: + case IDC_NOMORETHAN: + case IDC_NOLESSTHAN: + case IDC_EQUALTO: + case IDC_DIFFERENTFROM: + case IDC_DIFFERENTBY: + case IDC_MODULO: + case IDC_PREVIOUSVALUE: + case IDC_SPECIFICVALUE: + case IDC_SPECIFICADDRESS: + case IDC_NUMBEROFCHANGES: + case IDC_SIGNED: + case IDC_UNSIGNED: + case IDC_HEX: + rs_val_valid = Set_RS_Val(); + needRefresh = true; + break; + case IDC_EDIT_COMPAREVALUE: + case IDC_EDIT_COMPAREADDRESS: + case IDC_EDIT_COMPARECHANGES: + case IDC_EDIT_DIFFBY: + case IDC_EDIT_MODBY: + if(HIWORD(wParam) == EN_CHANGE) + { + rs_val_valid = Set_RS_Val(); + needRefresh = true; + } + break; + } + if(needRefresh) + ListView_Update(GetDlgItem(hDlg,IDC_RAMLIST), -1); + + + return rv; + } break; + + case WM_CLOSE: + RECT r; + GetWindowRect(hDlg, &r); + regSetDwordValue("ramSearchX", r.left); + regSetDwordValue("ramSearchY", r.top); + SendMessage(RamSearchHWnd, WM_DESTROY, 0, 0); + break; + case WM_DESTROY: + RamSearchHWnd = NULL; +// theApp.modelessCheatDialogIsOpen = false; +// return true; + break; + } + + return false; +} + +void UpdateRamSearchTitleBar(int percent) +{ +#define HEADER_STR " RAM Search - " +#define PROGRESS_STR " %d%% ... " +#define STATUS_STR "%d Possibilit%s (%d Region%s)" + + int poss = last_rs_possible; + int regions = last_rs_regions; + if(poss <= 0) + strcpy(Str_Tmp," RAM Search"); + else if(percent <= 0) + sprintf(Str_Tmp, HEADER_STR STATUS_STR, poss, poss==1?"y":"ies", regions, regions==1?"":"s"); + else + sprintf(Str_Tmp, PROGRESS_STR STATUS_STR, percent, poss, poss==1?"y":"ies", regions, regions==1?"":"s"); + SetWindowText(RamSearchHWnd, Str_Tmp); +} + +void UpdatePossibilities(int rs_possible, int regions) +{ + if(rs_possible != last_rs_possible) + { + last_rs_possible = rs_possible; + last_rs_regions = regions; + UpdateRamSearchTitleBar(); + } +} + +void SetRamSearchUndoType(HWND hDlg, int type) +{ + if(s_undoType != type) + { + if((s_undoType!=2 && s_undoType!=-1)!=(type!=2 && type!=-1)) + SendDlgItemMessage(hDlg,IDC_C_UNDO,WM_SETTEXT,0,(LPARAM)((type == 2 || type == -1) ? "Redo" : "Undo")); + if((s_undoType>0)!=(type>0)) + EnableWindow(GetDlgItem(hDlg,IDC_C_UNDO),type>0); + s_undoType = type; + } +} + +void RamSearchSaveUndoStateIfNotTooBig(HWND hDlg) +{ + EnterCriticalSection(&s_activeMemoryRegionsCS); + if(s_activeMemoryRegions.size() < tooManyRegionsForUndo) + { + s_activeMemoryRegionsBackup = s_activeMemoryRegions; + LeaveCriticalSection(&s_activeMemoryRegionsCS); + SetRamSearchUndoType(hDlg, 1); + } + else + { + LeaveCriticalSection(&s_activeMemoryRegionsCS); + SetRamSearchUndoType(hDlg, 0); + } +} + +struct InitRamSearch +{ + InitRamSearch() + { + InitializeCriticalSection(&s_activeMemoryRegionsCS); + } + ~InitRamSearch() + { + DeleteCriticalSection(&s_activeMemoryRegionsCS); + } +} initRamSearch; + + +void init_list_box(HWND Box, const char* Strs[], int numColumns, int *columnWidths) //initializes the ram search and/or ram watch listbox +{ + LVCOLUMN Col; + Col.mask = LVCF_FMT | LVCF_ORDER | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH; + Col.fmt = LVCFMT_CENTER; + for (int i = 0; i < numColumns; i++) + { + Col.iOrder = i; + Col.iSubItem = i; + Col.pszText = (LPSTR)(Strs[i]); + Col.cx = columnWidths[i]; + ListView_InsertColumn(Box,i,&Col); + } + + ListView_SetExtendedListViewStyle(Box, LVS_EX_FULLROWSELECT); +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ram_search.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ram_search.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,34 @@ +#ifndef RAM_SEARCH_H +#define RAM_SEARCH_H + + +extern char rs_type_size; +extern int ResultCount; +typedef unsigned int HWAddressType; + +unsigned int sizeConv(unsigned int index,char size, char *prevSize = &rs_type_size, bool usePrev = false); +unsigned int GetRamValue(unsigned int Addr,char Size); +void prune(char Search, char Operater, char Type, int Value, int OperatorParameter); +void CompactAddrs(); +void reset_address_info(); +void signal_new_frame(); +void signal_new_size(); +void UpdateRamSearchTitleBar(int percent = 0); +void SetRamSearchUndoType(HWND hDlg, int type); +unsigned int ReadValueAtHardwareAddress(HWAddressType address, unsigned int size); +bool WriteValueAtHardwareAddress(HWAddressType address, unsigned int value, unsigned int size); +bool IsHardwareAddressValid(HWAddressType address); +extern int curr_ram_size; +extern bool noMisalign; +extern HWND RamSearchHWnd; + + +void ResetResults(); +void CloseRamWindows(); //Close the Ram Search & Watch windows when rom closes +void ReopenRamWindows(); //Reopen them when a new Rom is loaded +void Update_RAM_Search(); //keeps RAM values up to date in the search and watch windows + + + +#endif + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ramwatch.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ramwatch.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1249 @@ +#include "stdafx.h" +#include "VBA.h" +#include "resource.h" +#include "WinMiscUtil.h" +#include "GBACheatsDlg.h" +#include "GBCheatsDlg.h" +#include "ram_search.h" +#include "ramwatch.h" +#include "reg.h" +#include "Sound.h" +#include +#include +#include + +/* +#include +#pragma comment(lib, "comctl32.lib") +#include +#pragma comment(lib, "shell32.lib") +#include +#pragma comment(lib, "comdlg32.lib") +*/ + +static HMENU ramwatchmenu; +static HMENU rwrecentmenu; +/*static*/ HACCEL RamWatchAccels = NULL; +char rw_recent_files[MAX_RECENT_WATCHES][1024]; +//char Watch_Dir[1024]=""; +bool RWfileChanged = false; //Keeps track of whether the current watch file has been changed, if so, ramwatch will prompt to save changes +bool AutoRWLoad = false; //Keeps track of whether Auto-load is checked +bool RWSaveWindowPos = false; //Keeps track of whether Save Window position is checked +char currentWatch[1024]; +int ramw_x, ramw_y; //Used to store ramwatch dialog window positions +AddressWatcher rswatches[MAX_WATCH_COUNT]; +int WatchCount=0; +static int s_prevSelCount=-1; + +HWND RamWatchHWnd; +#define gamefilename theApp.gameFilename +#define hWnd AfxGetMainWnd()->GetSafeHwnd() +#define hInst AfxGetInstanceHandle() +static char Str_Tmp [1024]; + +void init_list_box(HWND Box, const char* Strs[], int numColumns, int *columnWidths); //initializes the ram search and/or ram watch listbox + +#define MESSAGEBOXPARENT (RamWatchHWnd ? RamWatchHWnd : hWnd) + +bool QuickSaveWatches(); +bool ResetWatches(); + +void RefreshWatchListSelectedCountControlStatus(HWND hDlg); + +unsigned int GetCurrentValue(AddressWatcher& watch) +{ + return ReadValueAtHardwareAddress(watch.Address, watch.Size == 'd' ? 4 : watch.Size == 'w' ? 2 : 1); +} + +bool IsSameWatch(const AddressWatcher& l, const AddressWatcher& r) +{ + if (r.Size == 'S') return false; + return ((l.Address == r.Address) && (l.Size == r.Size) && (l.Type == r.Type)/* && (l.WrongEndian == r.WrongEndian)*/); +} + +bool VerifyWatchNotAlreadyAdded(const AddressWatcher& watch) +{ + for (int j = 0; j < WatchCount; j++) + { + if (IsSameWatch(rswatches[j], watch)) + { + if(RamWatchHWnd) + SetForegroundWindow(RamWatchHWnd); + return false; + } + } + return true; +} + + +bool InsertWatch(const AddressWatcher& Watch, char *Comment) +{ + if(WatchCount >= MAX_WATCH_COUNT) + return false; + + int i = WatchCount++; + AddressWatcher& NewWatch = rswatches[i]; + NewWatch = Watch; + //if (NewWatch.comment) free(NewWatch.comment); + NewWatch.comment = (char *) malloc(strlen(Comment)+2); + NewWatch.CurValue = GetCurrentValue(NewWatch); + strcpy(NewWatch.comment, Comment); + ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount); + RWfileChanged=true; + + return true; +} + +LRESULT CALLBACK PromptWatchNameProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) //Gets the description of a watched address +{ + RECT r; + RECT r2; + int dx1, dy1, dx2, dy2; + + switch(uMsg) + { + case WM_INITDIALOG: + //Clear_Sound_Buffer(); + + GetWindowRect(hWnd, &r); + dx1 = (r.right - r.left) / 2; + dy1 = (r.bottom - r.top) / 2; + + GetWindowRect(hDlg, &r2); + dx2 = (r2.right - r2.left) / 2; + dy2 = (r2.bottom - r2.top) / 2; + + //SetWindowPos(hDlg, NULL, max(0, r.left + (dx1 - dx2)), max(0, r.top + (dy1 - dy2)), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + strcpy(Str_Tmp,"Enter a name for this RAM address."); + SendDlgItemMessage(hDlg,IDC_PROMPT_TEXT,WM_SETTEXT,0,(LPARAM)Str_Tmp); + strcpy(Str_Tmp,""); + SendDlgItemMessage(hDlg,IDC_PROMPT_TEXT2,WM_SETTEXT,0,(LPARAM)Str_Tmp); + return true; + break; + + case WM_COMMAND: + switch(LOWORD(wParam)) + { + case IDOK: + { + GetDlgItemText(hDlg,IDC_PROMPT_EDIT,Str_Tmp,80); + InsertWatch(rswatches[WatchCount],Str_Tmp); + EndDialog(hDlg, true); + return true; + break; + } + case ID_CANCEL: + case IDCANCEL: + EndDialog(hDlg, false); + return false; + break; + } + break; + + case WM_CLOSE: + EndDialog(hDlg, false); + return false; + break; + } + + return false; +} + +bool InsertWatch(const AddressWatcher& Watch, HWND parent) +{ + if(!VerifyWatchNotAlreadyAdded(Watch)) + return false; + + if(!parent) + parent = RamWatchHWnd; + if(!parent) + parent = hWnd; + + int prevWatchCount = WatchCount; + + rswatches[WatchCount] = Watch; + rswatches[WatchCount].CurValue = GetCurrentValue(rswatches[WatchCount]); + systemSoundClearBuffer(); + DialogBox(hInst, MAKEINTRESOURCE(IDD_PROMPT), parent, (DLGPROC) PromptWatchNameProc); + + return WatchCount > prevWatchCount; +} + +void Update_RAM_Watch() +{ + BOOL watchChanged[MAX_WATCH_COUNT] = {0}; + + if(WatchCount) + { + // update cached values and detect changes to displayed listview items + + for(int i = 0; i < WatchCount; i++) + { + unsigned int prevCurValue = rswatches[i].CurValue; + unsigned int newCurValue = GetCurrentValue(rswatches[i]); + if(prevCurValue != newCurValue) + { + rswatches[i].CurValue = newCurValue; + watchChanged[i] = TRUE; + } + } + } + + // refresh any visible parts of the listview box that changed + HWND lv = GetDlgItem(RamWatchHWnd,IDC_WATCHLIST); + int top = ListView_GetTopIndex(lv); + int bottom = top + ListView_GetCountPerPage(lv) + 1; // +1 is so we will update a partially-displayed last item + if(top < 0) top = 0; + if(bottom > WatchCount) bottom = WatchCount; + int start = -1; + for(int i = top; i <= bottom; i++) + { + if(start == -1) + { + if(i != bottom && watchChanged[i]) + { + start = i; + //somethingChanged = true; + } + } + else + { + if(i == bottom || !watchChanged[i]) + { + ListView_RedrawItems(lv, start, i-1); + start = -1; + } + } + } +} + +bool AskSave() +{ + //This function asks to save changes if the watch file contents have changed + //returns false only if a save was attempted but failed or was cancelled + if (RWfileChanged) + { + systemSoundClearBuffer(); + int answer = MessageBox(MESSAGEBOXPARENT, "Save Changes?", "Ram Watch", MB_YESNOCANCEL); + if(answer == IDYES) + if(!QuickSaveWatches()) + return false; + return (answer != IDCANCEL); + } + return true; +} + +void WriteRecentRWFiles() +{ + char str[2048]; + for (int i = 0; i < MAX_RECENT_WATCHES; i++) + { + sprintf(str, "recentWatch%d", i+1); + regSetStringValue(str, &rw_recent_files[i][0]); + } +} + +void UpdateRW_RMenu(HMENU menu, unsigned int mitem, unsigned int baseid) +{ + MENUITEMINFO moo; + int x; + + moo.cbSize = sizeof(moo); + moo.fMask = MIIM_SUBMENU | MIIM_STATE; + + GetMenuItemInfo(GetSubMenu(ramwatchmenu, 0), mitem, FALSE, &moo); + moo.hSubMenu = menu; + moo.fState = strlen(rw_recent_files[0]) ? MFS_ENABLED : MFS_GRAYED; + + SetMenuItemInfo(GetSubMenu(ramwatchmenu, 0), mitem, FALSE, &moo); + + // Remove all recent files submenus + for(x = 0; x < MAX_RECENT_WATCHES; x++) + { + RemoveMenu(menu, baseid + x, MF_BYCOMMAND); + } + + // Recreate the menus + for(x = MAX_RECENT_WATCHES - 1; x >= 0; x--) + { + // Skip empty strings + if(!strlen(rw_recent_files[x])) + { + continue; + } + + moo.cbSize = sizeof(moo); + moo.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE; + +#if 0 + const int TEMP_STRING_LENGTH = 128 + 5; + char tmp[TEMP_STRING_LENGTH]; // FIXME? + + // Fill in the menu text. + if(strlen(rw_recent_files[x]) < 128) + { + sprintf(tmp, "&%d. %s", ( x + 1 ) % 10, rw_recent_files[x]); + } + else + { + sprintf(tmp, "&%d. %s", ( x + 1 ) % 10, rw_recent_files[x] + strlen( rw_recent_files[x] ) - 127); + } +#endif + // the ATL way; it is really pain to work out a MBCS-compatible string replace function in the pure c way + CString atltmp(rw_recent_files[x]); + atltmp.Replace("&", "&&"); + char *tmp = atltmp.GetBuffer(0); + + // Insert the menu item + moo.cch = strlen(tmp); + moo.fType = 0; + moo.wID = baseid + x; + moo.dwTypeData = tmp; + InsertMenuItem(menu, 0, 1, &moo); + + // atltmp.ReleaseBuffer(); + } + + // I don't think one function shall do so many things in a row +// WriteRecentRWFiles(); // write recent menu to ini +} + +void UpdateRWRecentArray(const char* addString, unsigned int arrayLen, HMENU menu, unsigned int menuItem, unsigned int baseId) +{ + const size_t len = 1024; // Avoid magic numbers + + // Try to find out if the filename is already in the recent files list. + for(unsigned int x = 0; x < arrayLen; x++) + { + if(strlen(rw_recent_files[x])) + { + if(!strncmp(rw_recent_files[x], addString, 1024)) // Item is already in list. + { + // If the filename is in the file list don't add it again. + // Move it up in the list instead. + + int y; + char tmp[len]; + + // Save pointer. + strncpy(tmp, rw_recent_files[x], len - 1); // assuming rw_recent_files[n] is 0-terminated + + for(y = x; y; y--) + { + // Move items down. + strncpy(rw_recent_files[y], rw_recent_files[y - 1], len); + } + + // Put item on top. + strncpy(rw_recent_files[0],tmp, len); + + // Update the recent files menu + UpdateRW_RMenu(menu, menuItem, baseId); + + return; + } + } + } + + // The filename wasn't found in the list. That means we need to add it. + + // Move the other items down. + for(unsigned int x = arrayLen - 1; x; x--) + { + strncpy(rw_recent_files[x],rw_recent_files[x - 1], len); + } + + // Add the new item. + strncpy(rw_recent_files[0], addString, len); + rw_recent_files[0][len - 1] = '\0'; // better not assume that + + // Update the recent files menu + UpdateRW_RMenu(menu, menuItem, baseId); +} + +void RWAddRecentFile(const char *filename) +{ + UpdateRWRecentArray(filename, MAX_RECENT_WATCHES, rwrecentmenu, RAMMENU_FILE_RECENT, RW_MENU_FIRST_RECENT_FILE); +} + +void OpenRWRecentFile(int memwRFileNumber) +{ + if(!ResetWatches()) + return; + + int rnum = memwRFileNumber; + if ((unsigned int)rnum >= MAX_RECENT_WATCHES) + return; //just in case + + char* x; + + while(true) + { + x = rw_recent_files[rnum]; + if (!*x) + return; //If no recent files exist just return. Useful for Load last file on startup (or if something goes screwy) + + if (rnum) //Change order of recent files if not most recent + { + RWAddRecentFile(x); + rnum = 0; + } + else + { + break; + } + } + + strcpy(currentWatch,x); + strcpy(Str_Tmp,currentWatch); + + //loadwatches here + FILE *WatchFile = fopen(Str_Tmp,"rb"); + if (!WatchFile) + { + systemSoundClearBuffer(); + int answer = MessageBox(MESSAGEBOXPARENT,"Error opening file.","ERROR",MB_OKCANCEL); + if (answer == IDOK) + { + rw_recent_files[rnum][0] = '\0'; //Clear file from list + if (rnum) //Update the ramwatch list + RWAddRecentFile(rw_recent_files[0]); + else + RWAddRecentFile(rw_recent_files[1]); + } + return; + } + const char DELIM = '\t'; + AddressWatcher Temp; + char mode; + fgets(Str_Tmp,1024,WatchFile); + sscanf(Str_Tmp,"%c%*s",&mode); + //if ((mode == '1' && !(SegaCD_Started)) || (mode == '2' && !(_32X_Started))) + //{ + // char Device[8]; + // strcpy(Device,(mode > '1')?"32X":"SegaCD"); + // sprintf(Str_Tmp,"Warning: %s not started. \nWatches for %s addresses will be ignored.",Device,Device); + // MessageBox(MESSAGEBOXPARENT,Str_Tmp,"Possible Device Mismatch",MB_OK); + //} + int WatchAdd; + fgets(Str_Tmp,1024,WatchFile); + sscanf(Str_Tmp,"%d%*s",&WatchAdd); + WatchAdd+=WatchCount; + for (int i = WatchCount; i < WatchAdd; i++) + { + while (i < 0) + i++; + do { + fgets(Str_Tmp,1024,WatchFile); + } while (Str_Tmp[0] == '\n'); + sscanf(Str_Tmp,"%*05X%*c%08X%*c%c%*c%c%*c%d",&(Temp.Address),&(Temp.Size),&(Temp.Type),&(Temp.WrongEndian)); + Temp.WrongEndian = 0; + char *Comment = strrchr(Str_Tmp,DELIM) + 1; + *strrchr(Comment,'\n') = '\0'; + InsertWatch(Temp,Comment); + } + + fclose(WatchFile); + if (RamWatchHWnd) { + ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount); + RefreshWatchListSelectedCountControlStatus(RamWatchHWnd); + } + RWfileChanged=false; + return; +} + +int Change_File_L(char *Dest, const char *Dir, const char *Titre, const char *Filter, const char *Ext, HWND hwnd) +{ + if (!strcmp(Dest, "")) + { + strcpy(Dest, "default."); + strcat(Dest, Ext); + } + + SetCurrentDirectory(winGetDestDir(IDS_WATCH_DIR)); + + OPENFILENAME ofn; + + memset(&ofn, 0, sizeof(OPENFILENAME)); + + ofn.lStructSize = sizeof(OPENFILENAME); + ofn.hwndOwner = hwnd; + ofn.hInstance = hInst; + ofn.lpstrFile = Dest; + ofn.nMaxFile = 2047; + ofn.lpstrFilter = Filter; + ofn.nFilterIndex = 1; + ofn.lpstrInitialDir = Dir; + ofn.lpstrTitle = Titre; + ofn.lpstrDefExt = Ext; + ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; + + systemSoundClearBuffer(); + + if (GetOpenFileName(&ofn)) return 1; + + return 0; +} + +int Change_File_S(char *Dest, const char *Dir, const char *Titre, const char *Filter, const char *Ext, HWND hwnd) +{ + if (!strcmp(Dest, "")) + { + strcpy(Dest, "default."); + strcat(Dest, Ext); + } + + SetCurrentDirectory(winGetDestDir(IDS_WATCH_DIR)); + + OPENFILENAME ofn; + + memset(&ofn, 0, sizeof(OPENFILENAME)); + + ofn.lStructSize = sizeof(OPENFILENAME); + ofn.hwndOwner = hwnd; + ofn.hInstance = hInst; + ofn.lpstrFile = Dest; + ofn.nMaxFile = 2047; + ofn.lpstrFilter = Filter; + ofn.nFilterIndex = 1; + ofn.lpstrInitialDir = Dir; + ofn.lpstrTitle = Titre; + ofn.lpstrDefExt = Ext; + ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY; + + if (GetSaveFileName(&ofn)) return 1; + + return 0; +} + +bool Save_Watches() +{ + const char* slash = max(strrchr(gamefilename, '|'), max(strrchr(gamefilename, '\\'), strrchr(gamefilename, '/'))); + strcpy(Str_Tmp,slash ? slash+1 : gamefilename); + char* dot = strrchr(Str_Tmp, '.'); + if(dot) *dot = 0; + strcat(Str_Tmp,".wch"); + if(Change_File_S(Str_Tmp, winGetDestDir(IDS_WATCH_DIR), "Save Watches", "Watchlist\0*.wch\0All Files\0*.*\0\0", "wch", RamWatchHWnd)) + { + FILE *WatchFile = fopen(Str_Tmp,"r+b"); + if (!WatchFile) WatchFile = fopen(Str_Tmp,"w+b"); + fputc('\n',WatchFile); + strcpy(currentWatch,Str_Tmp); + RWAddRecentFile(currentWatch); + sprintf(Str_Tmp,"%d\n",WatchCount); + fputs(Str_Tmp,WatchFile); + const char DELIM = '\t'; + for (int i = 0; i < WatchCount; i++) + { + sprintf(Str_Tmp,"%05X%c%08X%c%c%c%c%c%d%c%s\n",i,DELIM,rswatches[i].Address,DELIM,rswatches[i].Size,DELIM,rswatches[i].Type,DELIM,rswatches[i].WrongEndian,DELIM,rswatches[i].comment); + fputs(Str_Tmp,WatchFile); + } + + fclose(WatchFile); + RWfileChanged=false; + //TODO: Add to recent list function call here + return true; + } + return false; +} + +bool QuickSaveWatches() +{ +if (RWfileChanged==false) return true; //If file has not changed, no need to save changes +if (currentWatch[0] == NULL) //If there is no currently loaded file, run to Save as and then return + { + return Save_Watches(); + } + + strcpy(Str_Tmp,currentWatch); + FILE *WatchFile = fopen(Str_Tmp,"r+b"); + if (!WatchFile) WatchFile = fopen(Str_Tmp,"w+b"); + fputc('\n',WatchFile); + sprintf(Str_Tmp,"%d\n",WatchCount); + fputs(Str_Tmp,WatchFile); + const char DELIM = '\t'; + for (int i = 0; i < WatchCount; i++) + { + sprintf(Str_Tmp,"%05X%c%08X%c%c%c%c%c%d%c%s\n",i,DELIM,rswatches[i].Address,DELIM,rswatches[i].Size,DELIM,rswatches[i].Type,DELIM,rswatches[i].WrongEndian,DELIM,rswatches[i].comment); + fputs(Str_Tmp,WatchFile); + } + fclose(WatchFile); + RWfileChanged=false; + return true; +} + +bool Load_Watches(bool clear, const char* filename) +{ + const char DELIM = '\t'; + FILE* WatchFile = fopen(filename,"rb"); + if (!WatchFile) + { + systemSoundClearBuffer(); + MessageBox(MESSAGEBOXPARENT,"Error opening file.","ERROR",MB_OK); + return false; + } + if(clear) + { + if(!ResetWatches()) + { + fclose(WatchFile); + return false; + } + } + strcpy(currentWatch,filename); + RWAddRecentFile(currentWatch); + AddressWatcher Temp; + char mode; + fgets(Str_Tmp,1024,WatchFile); + sscanf(Str_Tmp,"%c%*s",&mode); + int WatchAdd; + fgets(Str_Tmp,1024,WatchFile); + sscanf(Str_Tmp,"%d%*s",&WatchAdd); + WatchAdd+=WatchCount; + for (int i = WatchCount; i < WatchAdd; i++) + { + while (i < 0) + i++; + do { + fgets(Str_Tmp,1024,WatchFile); + } while (Str_Tmp[0] == '\n'); + sscanf(Str_Tmp,"%*05X%*c%08X%*c%c%*c%c%*c%d",&(Temp.Address),&(Temp.Size),&(Temp.Type),&(Temp.WrongEndian)); + Temp.WrongEndian = 0; + char *Comment = strrchr(Str_Tmp,DELIM) + 1; + *strrchr(Comment,'\n') = '\0'; + InsertWatch(Temp,Comment); + } + + fclose(WatchFile); + if (RamWatchHWnd) + ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount); + RWfileChanged=false; + return true; +} + +bool Load_Watches(bool clear) +{ + const char* slash = max(strrchr(gamefilename, '|'), max(strrchr(gamefilename, '\\'), strrchr(gamefilename, '/'))); + strcpy(Str_Tmp,slash ? slash+1 : gamefilename); + char* dot = strrchr(Str_Tmp, '.'); + if(dot) *dot = 0; + strcat(Str_Tmp,".wch"); + if(Change_File_L(Str_Tmp, winGetDestDir(IDS_WATCH_DIR), "Load Watches", "Watchlist\0*.wch\0All Files\0*.*\0\0", "wch", RamWatchHWnd)) + { + return Load_Watches(clear, Str_Tmp); + } + return false; +} + +bool ResetWatches() +{ + if(!AskSave()) + return false; + for (;WatchCount>=0;WatchCount--) + { + free(rswatches[WatchCount].comment); + rswatches[WatchCount].comment = NULL; + } + WatchCount++; + if (RamWatchHWnd) { + ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount); + RefreshWatchListSelectedCountControlStatus(RamWatchHWnd); + } + RWfileChanged = false; + currentWatch[0] = NULL; + return true; +} + +void RemoveWatch(int watchIndex) +{ + free(rswatches[watchIndex].comment); + rswatches[watchIndex].comment = NULL; + for (int i = watchIndex; i <= WatchCount; i++) + rswatches[i] = rswatches[i+1]; + WatchCount--; +} + +LRESULT CALLBACK EditWatchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) //Gets info for a RAM Watch, and then inserts it into the Watch List +{ + RECT r; + RECT r2; + int dx1, dy1, dx2, dy2; + static int index; + static char s,t = s = 0; + + switch(uMsg) + { + case WM_INITDIALOG: + //Clear_Sound_Buffer(); + + + GetWindowRect(hWnd, &r); + dx1 = (r.right - r.left) / 2; + dy1 = (r.bottom - r.top) / 2; + + GetWindowRect(hDlg, &r2); + dx2 = (r2.right - r2.left) / 2; + dy2 = (r2.bottom - r2.top) / 2; + + //SetWindowPos(hDlg, NULL, max(0, r.left + (dx1 - dx2)), max(0, r.top + (dy1 - dy2)), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + index = (int)lParam; + sprintf(Str_Tmp,"%08X",rswatches[index].Address); + SetDlgItemText(hDlg,IDC_EDIT_COMPAREADDRESS,Str_Tmp); + if (rswatches[index].comment != NULL) + SetDlgItemText(hDlg,IDC_PROMPT_EDIT,rswatches[index].comment); + s = rswatches[index].Size; + t = rswatches[index].Type; + switch (s) + { + case 'b': + SendDlgItemMessage(hDlg, IDC_1_BYTE, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'w': + SendDlgItemMessage(hDlg, IDC_2_BYTES, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'd': + SendDlgItemMessage(hDlg, IDC_4_BYTES, BM_SETCHECK, BST_CHECKED, 0); + break; + default: + s = 0; + break; + } + switch (t) + { + case 's': + SendDlgItemMessage(hDlg, IDC_SIGNED, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'u': + SendDlgItemMessage(hDlg, IDC_UNSIGNED, BM_SETCHECK, BST_CHECKED, 0); + break; + case 'h': + SendDlgItemMessage(hDlg, IDC_HEX, BM_SETCHECK, BST_CHECKED, 0); + break; + default: + t = 0; + break; + } + + return true; + break; + + case WM_COMMAND: + switch(LOWORD(wParam)) + { + case IDC_SIGNED: + t='s'; + return true; + case IDC_UNSIGNED: + t='u'; + return true; + case IDC_HEX: + t='h'; + return true; + case IDC_1_BYTE: + s = 'b'; + return true; + case IDC_2_BYTES: + s = 'w'; + return true; + case IDC_4_BYTES: + s = 'd'; + return true; + case IDOK: + { + if (s && t) + { + AddressWatcher Temp; + Temp.Size = s; + Temp.Type = t; + Temp.WrongEndian = false; //replace this when I get little endian working properly + GetDlgItemText(hDlg,IDC_EDIT_COMPAREADDRESS,Str_Tmp,1024); + char *addrstr = Str_Tmp; + if (strlen(Str_Tmp) > 8) addrstr = &(Str_Tmp[strlen(Str_Tmp) - 9]); + for(int i = 0; addrstr[i]; i++) {if(toupper(addrstr[i]) == 'O') addrstr[i] = '0';} + sscanf(addrstr,"%08X",&(Temp.Address)); + + if((Temp.Address & ~0xFFFFFF) == ~0xFFFFFF) + Temp.Address &= 0xFFFFFF; + + if(IsHardwareAddressValid(Temp.Address)) + { + GetDlgItemText(hDlg,IDC_PROMPT_EDIT,Str_Tmp,80); + if (index < WatchCount) RemoveWatch(index); + InsertWatch(Temp,Str_Tmp); + if(RamWatchHWnd) + { + ListView_SetItemCount(GetDlgItem(RamWatchHWnd,IDC_WATCHLIST),WatchCount); + } + EndDialog(hDlg, true); + } + else + { + MessageBox(hDlg,"Invalid Address","ERROR",MB_OK); + } + } + else + { + strcpy(Str_Tmp,"Error:"); + if (!s) + strcat(Str_Tmp," Size must be specified."); + if (!t) + strcat(Str_Tmp," Type must be specified."); + MessageBox(hDlg,Str_Tmp,"ERROR",MB_OK); + } + RWfileChanged=true; + return true; + break; + } + case ID_CANCEL: + case IDCANCEL: + EndDialog(hDlg, false); + return false; + break; + } + break; + + case WM_CLOSE: + EndDialog(hDlg, false); + return false; + break; + } + + return false; +} + + + + +void RamWatchEnableCommand(HWND hDlg, HMENU hMenu, UINT uIDEnableItem, bool enable) +{ + EnableWindow(GetDlgItem(hDlg, uIDEnableItem), (enable?TRUE:FALSE)); + if (hMenu != NULL) { + if (uIDEnableItem == ID_WATCHES_UPDOWN) { + EnableMenuItem(hMenu, IDC_C_WATCH_UP, MF_BYCOMMAND | (enable?MF_ENABLED:MF_GRAYED)); + EnableMenuItem(hMenu, IDC_C_WATCH_DOWN, MF_BYCOMMAND | (enable?MF_ENABLED:MF_GRAYED)); + } + else + EnableMenuItem(hMenu, uIDEnableItem, MF_BYCOMMAND | (enable?MF_ENABLED:MF_GRAYED)); + } +} + +void RefreshWatchListSelectedCountControlStatus(HWND hDlg) +{ + int selCount = ListView_GetSelectedCount(GetDlgItem(hDlg,IDC_WATCHLIST)); + if(selCount != s_prevSelCount) + { + if(selCount < 2 || s_prevSelCount < 2) + { + RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_WATCH_EDIT, selCount == 1); + RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_WATCH_REMOVE, selCount >= 1); + RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_WATCH, WatchCount < MAX_WATCH_COUNT); + RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_WATCH_DUPLICATE, selCount == 1 && WatchCount < MAX_WATCH_COUNT); + RamWatchEnableCommand(hDlg, ramwatchmenu, IDC_C_ADDCHEAT, selCount == 1); + RamWatchEnableCommand(hDlg, ramwatchmenu, ID_WATCHES_UPDOWN, selCount == 1); + } + s_prevSelCount = selCount; + } +} + +LRESULT CALLBACK RamWatchProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + RECT r; + RECT r2; + int dx1, dy1, dx2, dy2; + static int watchIndex=0; + + switch(uMsg) + { + case WM_MOVE: { + RECT wrect; + GetWindowRect(hDlg,&wrect); + ramw_x = wrect.left; + ramw_y = wrect.top; + regSetDwordValue(RAMWX, ramw_x); + regSetDwordValue(RAMWY, ramw_y); + } break; + + case WM_INITDIALOG: { + GetWindowRect(hWnd, &r); //Ramwatch window + dx1 = (r.right - r.left) / 2; + dy1 = (r.bottom - r.top) / 2; + + GetWindowRect(hDlg, &r2); // TASer window + dx2 = (r2.right - r2.left) / 2; + dy2 = (r2.bottom - r2.top) / 2; + + + // push it away from the main window if we can + const int width = (r.right-r.left); + const int height = (r.bottom - r.top); + const int width2 = (r2.right-r2.left); + if(r.left+width2 + width < GetSystemMetrics(SM_CXSCREEN)) + { + r.right += width; + r.left += width; + } + else if((int)r.left - (int)width2 > 0) + { + r.right -= width2; + r.left -= width2; + } + + //----------------------------------------------------------------------------------- + //If user has Save Window Pos selected, override default positioning + if (RWSaveWindowPos) + { + //If ramwindow is for some reason completely off screen, use default instead + if (ramw_x > (-width*2) || ramw_x < (width*2 + GetSystemMetrics(SM_CYSCREEN)) ) + r.left = ramw_x; //This also ignores cases of windows -32000 error codes + //If ramwindow is for some reason completely off screen, use default instead + if (ramw_y > (0-height*2) ||ramw_y < (height*2 + GetSystemMetrics(SM_CYSCREEN)) ) + r.top = ramw_y; //This also ignores cases of windows -32000 error codes + } + //------------------------------------------------------------------------------------- + SetWindowPos(hDlg, NULL, r.left, r.top, NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW); + + ramwatchmenu=GetMenu(hDlg); + rwrecentmenu=CreateMenu(); + UpdateRW_RMenu(rwrecentmenu, RAMMENU_FILE_RECENT, RW_MENU_FIRST_RECENT_FILE); + + const char* names[3] = {"Address","Value","Notes"}; + int widths[3] = {62,64,64+51+53}; + init_list_box(GetDlgItem(hDlg,IDC_WATCHLIST),names,3,widths); + if (!ResultCount) + reset_address_info(); + else + signal_new_frame(); + ListView_SetItemCount(GetDlgItem(hDlg,IDC_WATCHLIST),WatchCount); + if (!noMisalign) SendDlgItemMessage(hDlg, IDC_MISALIGN, BM_SETCHECK, BST_CHECKED, 0); + //if (littleEndian) SendDlgItemMessage(hDlg, IDC_ENDIAN, BM_SETCHECK, BST_CHECKED, 0); + + RamWatchAccels = LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_ACCELERATOR1)); + + // due to some bug in windows, the arrow button width from the resource gets ignored, so we have to set it here + SetWindowPos(GetDlgItem(hDlg,ID_WATCHES_UPDOWN), 0,0,0, 30,60, SWP_NOMOVE); + + Update_RAM_Watch(); + + DragAcceptFiles(hDlg, TRUE); + + s_prevSelCount = -1; + RefreshWatchListSelectedCountControlStatus(hDlg); + return false; + } break; + + case WM_INITMENU: + CheckMenuItem(ramwatchmenu, RAMMENU_FILE_AUTOLOAD, AutoRWLoad ? MF_CHECKED : MF_UNCHECKED); + CheckMenuItem(ramwatchmenu, RAMMENU_FILE_SAVEWINDOW, RWSaveWindowPos ? MF_CHECKED : MF_UNCHECKED); + break; + + case WM_ENTERMENULOOP: + systemSoundClearBuffer(); + break; + + case WM_MENUSELECT: + case WM_ENTERSIZEMOVE: + //Clear_Sound_Buffer(); + break; + + case WM_NOTIFY: + { + switch(wParam) + { + case ID_WATCHES_UPDOWN: + { + switch(((LPNMUPDOWN)lParam)->hdr.code) + { + case UDN_DELTAPOS: { + int delta = ((LPNMUPDOWN)lParam)->iDelta; + SendMessage(hDlg, WM_COMMAND, delta<0 ? IDC_C_WATCH_UP : IDC_C_WATCH_DOWN,0); + } break; + } + } break; + + default: + { + LPNMHDR lP = (LPNMHDR) lParam; + switch (lP->code) + { + case LVN_ITEMCHANGED: // selection changed event + { + NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)lP; + if(pNMListView->uNewState & LVIS_FOCUSED || + (pNMListView->uNewState ^ pNMListView->uOldState) & LVIS_SELECTED) + { + // disable buttons that we don't have the right number of selected items for + RefreshWatchListSelectedCountControlStatus(hDlg); + } + } break; + + case LVN_GETDISPINFO: + { + LV_DISPINFO *Item = (LV_DISPINFO *)lParam; + Item->item.mask = LVIF_TEXT; + Item->item.state = 0; + Item->item.iImage = 0; + const unsigned int iNum = Item->item.iItem; + static char num[11]; + switch (Item->item.iSubItem) + { + case 0: + sprintf(num,"%08X",rswatches[iNum].Address); + Item->item.pszText = num; + return true; + case 1: { + int i = rswatches[iNum].CurValue; + int t = rswatches[iNum].Type; + int size = rswatches[iNum].Size; + const char* formatString = ((t=='s') ? "%d" : (t=='u') ? "%u" : (size=='d' ? "%08X" : size=='w' ? "%04X" : "%02X")); + switch (size) + { + case 'b': + default: sprintf(num, formatString, t=='s' ? (char)(i&0xff) : (unsigned char)(i&0xff)); break; + case 'w': sprintf(num, formatString, t=='s' ? (short)(i&0xffff) : (unsigned short)(i&0xffff)); break; + case 'd': sprintf(num, formatString, t=='s' ? (long)(i&0xffffffff) : (unsigned long)(i&0xffffffff)); break; + } + + Item->item.pszText = num; + } return true; + case 2: + Item->item.pszText = rswatches[iNum].comment ? rswatches[iNum].comment : ""; + return true; + + default: + return false; + } + } + case LVN_ODFINDITEM: + { + // disable search by keyboard typing, + // because it interferes with some of the accelerators + // and it isn't very useful here anyway + SetWindowLong(hDlg, DWL_MSGRESULT, ListView_GetSelectionMark(GetDlgItem(hDlg,IDC_WATCHLIST))); + return 1; + } + } + } + } + } break; + + case WM_COMMAND: + switch(LOWORD(wParam)) + { + case RAMMENU_FILE_SAVE: + QuickSaveWatches(); + break; + + case RAMMENU_FILE_SAVEAS: + //case IDC_C_SAVE: + return Save_Watches(); + case RAMMENU_FILE_OPEN: + return Load_Watches(true); + case RAMMENU_FILE_APPEND: + //case IDC_C_LOAD: + return Load_Watches(false); + case RAMMENU_FILE_NEW: + //case IDC_C_RESET: + ResetWatches(); + return true; + case IDC_C_WATCH_REMOVE: + { + HWND watchListControl = GetDlgItem(hDlg, IDC_WATCHLIST); + watchIndex = ListView_GetNextItem(watchListControl, -1, LVNI_ALL | LVNI_SELECTED); + while (watchIndex >= 0) + { + RemoveWatch(watchIndex); + ListView_DeleteItem(watchListControl, watchIndex); + watchIndex = ListView_GetNextItem(watchListControl, -1, LVNI_ALL | LVNI_SELECTED); + } + RWfileChanged=true; + SetFocus(GetDlgItem(hDlg,IDC_WATCHLIST)); + return true; + } + case IDC_C_WATCH_EDIT: + watchIndex = ListView_GetSelectionMark(GetDlgItem(hDlg,IDC_WATCHLIST)); + if(watchIndex != -1) + { + systemSoundClearBuffer(); + DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_EDITWATCH), hDlg, (DLGPROC) EditWatchProc,(LPARAM) watchIndex); + SetFocus(GetDlgItem(hDlg,IDC_WATCHLIST)); + } + return true; + case IDC_C_WATCH: + rswatches[WatchCount].Address = rswatches[WatchCount].WrongEndian = 0; + rswatches[WatchCount].Size = 'b'; + rswatches[WatchCount].Type = 's'; + systemSoundClearBuffer(); + DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_EDITWATCH), hDlg, (DLGPROC) EditWatchProc,(LPARAM) WatchCount); + SetFocus(GetDlgItem(hDlg,IDC_WATCHLIST)); + return true; + case IDC_C_WATCH_DUPLICATE: + watchIndex = ListView_GetSelectionMark(GetDlgItem(hDlg,IDC_WATCHLIST)); + if(watchIndex != -1) + { + rswatches[WatchCount].Address = rswatches[watchIndex].Address; + rswatches[WatchCount].WrongEndian = rswatches[watchIndex].WrongEndian; + rswatches[WatchCount].Size = rswatches[watchIndex].Size; + rswatches[WatchCount].Type = rswatches[watchIndex].Type; + systemSoundClearBuffer(); + DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_EDITWATCH), hDlg, (DLGPROC) EditWatchProc,(LPARAM) WatchCount); + SetFocus(GetDlgItem(hDlg,IDC_WATCHLIST)); + } + return true; + + case IDC_C_WATCH_SEPARATE: + AddressWatcher separator; + separator.Address = 0; + separator.WrongEndian = false; + separator.Size = 'S'; + separator.Type = 'S'; + InsertWatch(separator, "----------------------------"); + SetFocus(GetDlgItem(hDlg,IDC_WATCHLIST)); + return true; + + case IDC_C_WATCH_UP: + { + watchIndex = ListView_GetSelectionMark(GetDlgItem(hDlg,IDC_WATCHLIST)); + if (watchIndex == 0 || watchIndex == -1) + return true; + void *tmp = malloc(sizeof(AddressWatcher)); + memcpy(tmp,&(rswatches[watchIndex]),sizeof(AddressWatcher)); + memcpy(&(rswatches[watchIndex]),&(rswatches[watchIndex - 1]),sizeof(AddressWatcher)); + memcpy(&(rswatches[watchIndex - 1]),tmp,sizeof(AddressWatcher)); + free(tmp); + ListView_SetItemState(GetDlgItem(hDlg,IDC_WATCHLIST),watchIndex,0,LVIS_FOCUSED|LVIS_SELECTED); + ListView_SetSelectionMark(GetDlgItem(hDlg,IDC_WATCHLIST),watchIndex-1); + ListView_SetItemState(GetDlgItem(hDlg,IDC_WATCHLIST),watchIndex-1,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); + ListView_SetItemCount(GetDlgItem(hDlg,IDC_WATCHLIST),WatchCount); + RWfileChanged=true; + return true; + } + case IDC_C_WATCH_DOWN: + { + watchIndex = ListView_GetSelectionMark(GetDlgItem(hDlg,IDC_WATCHLIST)); + if (watchIndex >= WatchCount - 1 || watchIndex == -1) + return true; + void *tmp = malloc(sizeof(AddressWatcher)); + memcpy(tmp,&(rswatches[watchIndex]),sizeof(AddressWatcher)); + memcpy(&(rswatches[watchIndex]),&(rswatches[watchIndex + 1]),sizeof(AddressWatcher)); + memcpy(&(rswatches[watchIndex + 1]),tmp,sizeof(AddressWatcher)); + free(tmp); + ListView_SetItemState(GetDlgItem(hDlg,IDC_WATCHLIST),watchIndex,0,LVIS_FOCUSED|LVIS_SELECTED); + ListView_SetSelectionMark(GetDlgItem(hDlg,IDC_WATCHLIST),watchIndex+1); + ListView_SetItemState(GetDlgItem(hDlg,IDC_WATCHLIST),watchIndex+1,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); + ListView_SetItemCount(GetDlgItem(hDlg,IDC_WATCHLIST),WatchCount); + RWfileChanged=true; + return true; + } + case ID_WATCHES_UPDOWN: + { + int delta = ((LPNMUPDOWN)lParam)->iDelta; + SendMessage(hDlg, WM_COMMAND, delta<0 ? IDC_C_WATCH_UP : IDC_C_WATCH_DOWN,0); + break; + } + case RAMMENU_FILE_AUTOLOAD: + { + AutoRWLoad ^= 1; + CheckMenuItem(ramwatchmenu, RAMMENU_FILE_AUTOLOAD, AutoRWLoad ? MF_CHECKED : MF_UNCHECKED); + regSetDwordValue(AUTORWLOAD, AutoRWLoad); + break; + } + case RAMMENU_FILE_SAVEWINDOW: + { + RWSaveWindowPos ^=1; + CheckMenuItem(ramwatchmenu, RAMMENU_FILE_SAVEWINDOW, RWSaveWindowPos ? MF_CHECKED : MF_UNCHECKED); + regSetDwordValue(RWSAVEPOS, RWSaveWindowPos); + break; + } + case IDC_C_ADDCHEAT: + { + watchIndex = ListView_GetSelectionMark(GetDlgItem(hDlg,IDC_WATCHLIST)); + if(watchIndex >= 0) + { + unsigned int address = rswatches[watchIndex].Address; + + int sizeType = -1; + if(rswatches[watchIndex].Size == 'b') + sizeType = 0; + else if(rswatches[watchIndex].Size == 'w') + sizeType = 1; + else if(rswatches[watchIndex].Size == 'd') + sizeType = 2; + + int numberType = -1; + if(rswatches[watchIndex].Type == 's') + numberType = 0; + else if(rswatches[watchIndex].Type == 'u') + numberType = 1; + else if(rswatches[watchIndex].Type == 'h') + numberType = 2; + + if(systemCartridgeType == 0) + { + AddCheat dlg (address/*, hDlg*/); + if(sizeType != -1) dlg.sizeType = sizeType; + if(numberType != -1) dlg.numberType = numberType; + systemSoundClearBuffer(); + dlg.DoModal(); + } + else + { + AddGBCheat dlg (address/*, hDlg*/); + if(sizeType != -1) dlg.sizeType = sizeType; + if(numberType != -1) dlg.numberType = numberType; + systemSoundClearBuffer(); + dlg.DoModal(); + } + } + } + break; + case IDOK: + case IDCANCEL: + RamWatchHWnd = NULL; + DragAcceptFiles(hDlg, FALSE); + EndDialog(hDlg, true); + return true; + default: + if (LOWORD(wParam) >= RW_MENU_FIRST_RECENT_FILE && LOWORD(wParam) < RW_MENU_FIRST_RECENT_FILE+MAX_RECENT_WATCHES && LOWORD(wParam) <= RW_MENU_LAST_RECENT_FILE) + OpenRWRecentFile(LOWORD(wParam) - RW_MENU_FIRST_RECENT_FILE); + } + break; + +#if 0 + // this message is never received + case WM_KEYDOWN: // handle accelerator keys + { + SetFocus(GetDlgItem(hDlg,IDC_WATCHLIST)); + MSG msg; + msg.hwnd = hDlg; + msg.message = uMsg; + msg.wParam = wParam; + msg.lParam = lParam; + if(RamWatchAccels && TranslateAccelerator(hDlg, RamWatchAccels, &msg)) + return true; + } break; +#endif + + case WM_CLOSE: + SendMessage(RamWatchHWnd, WM_DESTROY, 0, 0); + break; + + case WM_DESTROY: + // this is the correct place + RamWatchHWnd = NULL; + DragAcceptFiles(hDlg, FALSE); + WriteRecentRWFiles(); // write recent menu to ini + break; + + case WM_DROPFILES: + { + HDROP hDrop = (HDROP)wParam; + DragQueryFile(hDrop, 0, Str_Tmp, 1024); + DragFinish(hDrop); + return Load_Watches(true, Str_Tmp); + } break; + } + + return false; +} diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/ramwatch.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/ramwatch.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,46 @@ +#ifndef RAMWATCH_H +#define RAMWATCH_H + +bool ResetWatches(); +void OpenRWRecentFile(int memwRFileNumber); +extern bool AutoRWLoad; +extern bool RWSaveWindowPos; +#define MAX_RECENT_WATCHES 5 +extern char rw_recent_files[MAX_RECENT_WATCHES][1024]; +extern bool AskSave(); +extern int ramw_x; +extern int ramw_y; +extern bool RWfileChanged; + +//Constants +#define AUTORWLOAD "RamWatchAutoLoad" +#define RWSAVEPOS "RamWatchSaveWindowPos" +#define RAMWX "RamwX" +#define RAMWY "RamwY" + +// AddressWatcher is self-contained now +struct AddressWatcher +{ + unsigned int Address; // hardware address + char Size; + char Type; + char* comment; // NULL means no comment, non-NULL means allocated comment + bool WrongEndian; + unsigned int CurValue; +}; +#define MAX_WATCH_COUNT 256 +extern AddressWatcher rswatches[MAX_WATCH_COUNT]; +extern int WatchCount; // number of valid items in rswatches + +extern char Watch_Dir[1024]; + +extern HWND RamWatchHWnd; +extern HACCEL RamWatchAccels; + +bool InsertWatch(const AddressWatcher& Watch, char *Comment); +bool InsertWatch(const AddressWatcher& Watch, HWND parent=NULL); // asks user for comment +void Update_RAM_Watch(); +bool Load_Watches(bool clear, const char* filename); +void RWAddRecentFile(const char *filename); + +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/resource.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/resource.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,1076 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by VBA.rc +// +#define IDS_UNSUPPORTED_VBA_SGM 1 +#define IDS_CANNOT_LOAD_SGM 2 +#define IDS_SAVE_GAME_NOT_USING_BIOS 3 +#define IDS_SAVE_GAME_USING_BIOS 4 +#define IDS_UNSUPPORTED_SAVE_TYPE 5 +#define IDS_CANNOT_OPEN_FILE 6 +#define IDS_BAD_ZIP_FILE 7 +#define IDS_NO_IMAGE_ON_ZIP 8 +#define IDS_ERROR_OPENING_IMAGE 9 +#define IDS_ERROR_READING_IMAGE 10 +#define IDS_UNSUPPORTED_BIOS_FUNCTION 11 +#define IDS_INVALID_BIOS_FILE_SIZE 12 +#define IDS_INVALID_CHEAT_CODE 13 +#define IDS_UNKNOWN_ARM_OPCDOE 14 +#define IDS_UNKNOWN_THUMB_OPCODE 15 +#define IDS_ERROR_CREATING_FILE 16 +#define IDS_FAILED_TO_READ_SGM 17 +#define IDS_FAILED_TO_READ_RTC 18 +#define IDS_UNSUPPORTED_VB_SGM 19 +#define IDS_CANNOT_LOAD_SGM_FOR 20 +#define IDS_ERROR_OPENING_IMAGE_FROM 21 +#define IDS_ERROR_READING_IMAGE_FROM 22 +#define IDS_UNSUPPORTED_ROM_SIZE 23 +#define IDS_UNSUPPORTED_RAM_SIZE 24 +#define IDS_UNKNOWN_CARTRIDGE_TYPE 25 +#define IDS_MAXIMUM_NUMBER_OF_CHEATS 26 +#define IDS_INVALID_GAMESHARK_CODE 27 +#define IDS_INVALID_GAMEGENIE_CODE 28 +#define IDS_INVALID_CHEAT_TO_REMOVE 29 +#define IDS_INVALID_CHEAT_CODE_ADDRESS 30 +#define IDS_UNSUPPORTED_CHEAT_LIST_VERSION 31 +#define IDS_UNSUPPORTED_CHEAT_LIST_TYPE 32 +#define IDS_INVALID_GSA_CODE 33 +#define IDS_CANNOT_IMPORT_SNAPSHOT_FOR 34 +#define IDS_UNSUPPORTED_SNAPSHOT_FILE 35 +#define IDS_UNSUPPORTED_ARM_MODE 36 +#define IDS_UNSUPPORTED_CODE_FILE 37 +#define IDS_GSA_CODE_WARNING 38 +#define IDS_INVALID_CBA_CODE 39 +#define IDS_CBA_CODE_WARNING 40 +#define IDS_OUT_OF_MEMORY 41 +#define IDS_STRING42 42 +#define IDI_ICON 101 +#define IDD_REGISTERS 102 +#define IDD_DEBUG 103 +#define IDR_MENU 104 +#define IDD_ABOUT 105 +#define IDR_ACCELERATOR 106 +#define IDD_CHEATS 107 +#define IDD_ADD_CHEAT 108 +#define IDD_DIRECTORIES 109 +#define IDD_CONFIG 110 +#define IDD_GS 111 +#define IDD_GG 112 +#define IDD_CHEAT_LIST 113 +#define IDD_ASSOCIATIONS 114 +#define IDR_GB_PRINTER 115 +#define IDD_GBA_ROM_INFO 116 +#define IDD_GB_ROM_INFO 117 +#define IDD_GB_CHEAT_LIST 118 +#define IDD_ADD_CHEAT_DLG 119 +#define IDD_GB_PRINTER 120 +#define IDD_MOTION_CONFIG 121 +#define IDD_LANG_SELECT 122 +#define IDD_CODE_SELECT 123 +#define IDD_OPENDLG 124 +#define IDD_MAP_VIEW 126 +#define IDD_PALETTE_VIEW 127 +#define IDD_MEM_VIEWER 128 +#define IDD_OAM_VIEW 130 +#define IDD_ACCEL_EDITOR 131 +#define IDD_TILE_VIEWER 132 +#define IDD_GB_COLORS 133 +#define IDD_DISASSEMBLE 134 +#define IDD_GDB_PORT 135 +#define IDD_GDB_WAITING 136 +#define IDD_LOGGING 137 +#define IDD_EXPORT_SPS 138 +#define IDD_ADDR_SIZE 139 +#define IDD_MODES 140 +#define IDD_DRIVERS 142 +#define IDD_THROTTLE 143 +#define IDD_GB_DISASSEMBLE 144 +#define IDD_GB_OAM_VIEW 145 +#define IDD_GB_TILE_VIEWER 146 +#define IDD_GB_MAP_VIEW 147 +#define IDD_GB_PALETTE_VIEW 148 +#define IDD_MODE_CONFIRM 149 +#define IDD_REWIND_INTERVAL 150 +#define IDD_IO_VIEWER 151 +#define IDD_MAX_SCALE 154 +#define IDD_BUG_REPORT 155 +#define IDD_MOVIECREATE 156 +#define IDD_MOVIEOPEN 157 +#define IDD_TEXTCONFIG 158 +#define IDD_LUA 159 +#define IDD_ARCHIVEFILECHOOSER 160 +#define IDD_RAMSEARCH 161 +#define IDD_EDITWATCH 162 +#define IDD_RAMWATCH 163 +#define IDD_PROMPT 164 +#define IDR_ACCELERATOR1 165 +#define IDC_R0 1000 +#define IDC_EDIT_UP 1000 +#define IDC_R1 1001 +#define IDC_EDIT_DOWN 1001 +#define IDC_R2 1002 +#define IDC_EDIT_LEFT 1002 +#define IDC_R3 1003 +#define IDC_EDIT_RIGHT 1003 +#define IDC_R4 1004 +#define IDC_EDIT_BUTTON_A 1004 +#define IDC_R5 1005 +#define IDC_EDIT_BUTTON_B 1005 +#define IDC_R6 1006 +#define IDC_EDIT_BUTTON_SELECT 1006 +#define IDC_R7 1007 +#define IDC_EDIT_BUTTON_START 1007 +#define IDC_R8 1008 +#define ID_OK 1008 +#define IDC_R9 1009 +#define ID_CANCEL 1009 +#define ID_SAVE 1009 +#define IDC_R10 1010 +#define IDC_EDIT_SPEED 1010 +#define IDC_R11 1011 +#define IDC_EDIT_CAPTURE 1011 +#define IDC_R12 1012 +#define IDC_EDIT_BUTTON_L 1012 +#define IDC_R13 1013 +#define IDC_EDIT_BUTTON_GS 1013 +#define IDC_R14 1014 +#define IDC_EDIT_BUTTON_R 1014 +#define IDC_R15 1015 +#define IDC_R16 1016 +#define IDC_R17 1017 +#define IDC_N_FLAG 1018 +#define IDC_Z_FLAG 1019 +#define IDC_NEXT 1019 +#define IDC_C_FLAG 1020 +#define IDC_CONTINUE 1020 +#define IDC_V_FLAG 1021 +#define IDC_CHEAT_LIST 1021 +#define IDC_IRQ 1022 +#define IDC_START 1022 +#define IDC_T_FLAG 1023 +#define IDC_SEARCH 1023 +#define IDS_DIRECTX_7_REQUIRED 1024 +#define IDC_ADD_CHEAT 1024 +#define IDC_OLD_VALUE 1025 +#define IDC_ADD_GS_CHEAT 1025 +#define IDS_DISABLING_VIDEO_MEMORY 1025 +#define IDC_ADD_GAMESHARK 1025 +#define IDC_SPECIFIC_VALUE 1026 +#define IDS_SETTING_WILL_BE_EFFECTIVE 1026 +#define IDS_DISABLING_EMULATION_ONLY 1027 +#define IDC_SIZE_8 1028 +#define IDS_FAILED_TO_OPEN_FILE 1028 +#define IDC_SIZE_16 1029 +#define IDS_FAILED_TO_READ_ZIP_DIR 1029 +#define IDC_SIZE_32 1030 +#define IDS_UNSUPPORTED_FILE_TYPE 1030 +#define IDC_EQ 1031 +#define IDS_CANNOT_CREATE_DIRECTSOUND 1031 +#define IDC_NE 1032 +#define IDS_CANNOT_SETCOOPERATIVELEVEL 1032 +#define IDC_LT 1033 +#define IDS_CANNOT_CREATESOUNDBUFFER 1033 +#define IDC_LE 1034 +#define IDS_CANNOT_SETFORMAT_PRIMARY 1034 +#define IDC_GT 1035 +#define IDS_CANNOT_CREATESOUNDBUFFER_SEC 1035 +#define IDC_GE 1036 +#define IDS_CANNOT_PLAY_PRIMARY 1036 +#define IDC_SIGNED 1037 +#define IDS_SEARCH_PRODUCED_TOO_MANY 1037 +#define IDC_UNSIGNED 1038 +#define IDS_NUMBER_CANNOT_BE_EMPTY 1038 +#define IDS_INVALID_ADDRESS 1039 +#define IDC_HEXADECIMAL 1040 +#define IDS_MISALIGNED_HALFWORD 1040 +#define IDC_VALUE 1041 +#define IDS_MISALIGNED_WORD 1041 +#define IDC_ADDRESS 1042 +#define IDS_VALUE_CANNOT_BE_EMPTY 1042 +#define IDS_ERROR_ON_STARTDOC 1043 +#define IDC_R 1043 +#define IDS_ERROR_ON_STARTPAGE 1044 +#define IDC_G 1044 +#define IDS_ERROR_PRINTING_ON_STRETCH 1045 +#define IDC_B 1045 +#define IDC_UPDATE 1046 +#define IDS_ERROR_ON_ENDPAGE 1046 +#define IDC_TILE_NUM 1046 +#define IDC_GGDESC 1047 +#define IDS_ERROR_ON_ENDDOC 1047 +#define IDC_FLIP 1047 +#define IDC_GGCODE 1048 +#define IDS_ERROR 1048 +#define IDC_PALETTE_NUM 1048 +#define IDC_GGADD 1049 +#define IDS_JOY_LEFT 1049 +#define IDC_GGDEL 1050 +#define IDS_JOY_RIGHT 1050 +#define IDC_GGLIST 1051 +#define IDS_JOY_UP 1051 +#define IDC_GGRES 1052 +#define IDS_JOY_DOWN 1052 +#define IDC_GGQUIT 1053 +#define IDS_JOY_BUTTON 1053 +#define IDC_GSDESC 1054 +#define IDC_GSCODE 1055 +#define IDC_GSADD 1056 +#define IDC_GSDEL 1057 +#define IDC_GSLIST 1058 +#define IDS_SELECT_BIOS_FILE 1058 +#define IDC_GSRES 1059 +#define IDS_RESET 1059 +#define IDC_GSQUIT 1060 +#define IDS_AUTOFIRE_A_DISABLED 1060 +#define IDC_FREEZE 1061 +#define IDS_AUTOFIRE_A 1061 +#define IDS_AUTOFIRE_B_DISABLED 1062 +#define IDS_AUTOFIRE_B 1063 +#define IDS_AUTOFIRE_L_DISABLED 1064 +#define IDS_AUTOFIRE_L 1065 +#define IDS_AUTOFIRE_R_DISABLED 1066 +#define IDC_REMOVE 1067 +#define IDS_AUTOFIRE_R 1067 +#define IDC_REMOVE_ALL 1068 +#define IDS_SELECT_ROM 1068 +#define IDS_SELECT_SAVE_GAME_NAME 1069 +#define IDC_ENABLE 1070 +#define IDS_LOADED_STATE 1070 +#define IDS_LOADED_STATE_N 1071 +#define IDS_WROTE_STATE 1072 +#define IDS_WROTE_STATE_N 1073 +#define IDC_RESTORE 1074 +#define IDS_LOADED_BATTERY 1074 +#define IDC_GBA 1075 +#define IDS_SELECT_CAPTURE_NAME 1075 +#define IDC_AGB 1076 +#define IDS_SCREEN_CAPTURE 1076 +#define IDC_BIN 1077 +#define IDS_ADDRESS 1077 +#define IDC_GB 1078 +#define IDS_OLD_VALUE 1078 +#define IDC_SGB 1079 +#define IDC_ROM_TITLE 1079 +#define IDS_NEW_VALUE 1079 +#define IDC_CGB 1080 +#define IDC_ROM_GAME_CODE 1080 +#define IDS_ADD_CHEAT_CODE 1080 +#define IDC_GBC 1081 +#define IDC_ROM_MAKER_CODE 1081 +#define IDS_CODE 1081 +#define IDC_ROM_UNIT_CODE 1082 +#define IDS_DESCRIPTION 1082 +#define IDC_ROM_DEVICE_TYPE 1083 +#define IDS_STATUS 1083 +#define IDC_ROM_VERSION 1084 +#define IDS_ADD_GG_CODE 1084 +#define IDC_ROM_CRC 1085 +#define IDS_ADD_GS_CODE 1085 +#define IDC_ROM_COLOR 1086 +#define IDC_CODE 1086 +#define IDS_POCKET_PRINTER 1086 +#define IDC_ROM_MAKER_NAME 1086 +#define IDC_ROM_SIZE 1087 +#define IDC_DESC 1087 +#define IDS_UNKNOWN 1087 +#define IDC_ROM_RAM_SIZE 1088 +#define IDC_ADD_GG_CHEAT 1088 +#define IDS_NONE 1088 +#define IDC_ROM_DEST_CODE 1089 +#define IDC_GB_PRINTER 1089 +#define IDS_FAILED_TO_LOAD_LIBRARY 1089 +#define IDC_ROM_LIC_CODE 1090 +#define IDC_1X 1090 +#define IDS_FAILED_TO_GET_LOCINFO 1090 +#define IDC_EDIT_AUTHOR 1090 +#define IDC_ROM_CHECKSUM 1091 +#define IDC_2X 1091 +#define IDS_SELECT_CHEAT_LIST_NAME 1091 +#define IDC_EDIT_DESCRIPTION 1091 +#define IDC_3X 1092 +#define IDS_FILTER_BIOS 1092 +#define IDC_4X 1093 +#define IDS_FILTER_ROM 1093 +#define IDC_ROM_MAKER_NAME2 1093 +#define ID_PRINT 1094 +#define IDS_FILTER_SGM 1094 +#define IDC_ADD_GSA 1095 +#define IDC_ADD_CODE 1095 +#define IDS_FILTER_CHEAT_LIST 1095 +#define IDC_TRANSLATION_BY 1096 +#define IDS_FILTER_PNG 1096 +#define IDC_LANG_STRING 1097 +#define IDS_LOADED_CHEATS 1097 +#define IDC_LANG_NAME 1098 +#define IDS_ERROR_DISP_COLOR 1098 +#define IDS_ADD_GSA_CODE 1099 +#define IDC_GAME_LIST 1099 +#define IDS_FILTER_SPS 1100 +#define IDS_SELECT_SNAPSHOT_FILE 1101 +#define IDC_ADD_CODEBREAKER 1101 +#define IDS_FILTER_SAV 1102 +#define IDS_SELECT_BATTERY_FILE 1103 +#define IDS_FILTER_GBS 1104 +#define IDS_FILTER_GCF 1105 +#define IDS_SELECT_CODE_FILE 1106 +#define IDS_SAVE_WILL_BE_LOST 1107 +#define IDS_CONFIRM_ACTION 1108 +#define IDS_CODES_WILL_BE_LOST 1109 +#define IDS_FILTER_SPC 1110 +#define IDS_ADD_CBA_CODE 1111 +#define IDS_FILTER_WAV 1112 +#define IDS_SELECT_WAV_NAME 1113 +#define IDC_FRAME_0 1113 +#define IDS_FILTER_GBROM 1114 +#define IDC_FRAME_1 1114 +#define IDC_BG0 1115 +#define IDS_FILTER_PAL 1115 +#define IDC_BG1 1116 +#define IDS_SELECT_PALETTE_NAME 1116 +#define IDC_BG2 1117 +#define IDS_SEARCH_PRODUCED_NO_RESULTS 1117 +#define IDC_BG3 1118 +#define IDS_ERROR_BINDING 1118 +#define IDS_ERROR_LISTENING 1119 +#define IDS_ERROR_CREATING_SOCKET 1120 +#define IDS_ACK_NOT_RECEIVED 1121 +#define IDS_ERROR_NOT_GBA_IMAGE 1122 +#define IDS_EEPROM_NOT_SUPPORTED 1123 +#define IDC_MAP_VIEW 1124 +#define IDS_FILTER_DUMP 1124 +#define IDC_PALETTE_VIEW 1125 +#define IDS_SELECT_DUMP_FILE 1125 +#define IDC_PALETTE_VIEW_OBJ 1126 +#define IDC_REFRESH 1126 +#define IDS_FILTER_AVI 1126 +#define IDC_SAVE 1127 +#define IDC_GOPC 1127 +#define IDS_SELECT_AVI_NAME 1127 +#define IDC_APPLY 1127 +#define IDS_INVALID_THROTTLE_VALUE 1128 +#define IDC_REFRESH2 1129 +#define IDS_FILTER_INI 1129 +#define IDS_SELECT_SKIN_FILE 1130 +#define IDC_CLOSE 1131 +#define IDS_FILTER_VMV 1131 +#define IDS_FILTER_MOVIE 1131 +#define IDS_SELECT_MOVIE_NAME 1132 +#define IDS_BUG_REPORT 1133 +#define IDS_UNSUPPORTED_MOVIE_VERSION 1134 +#define IDS_END_OF_MOVIE 1135 +#define IDC_COLOR 1136 +#define IDS_INVALID_INTERVAL_VALUE 1136 +#define IDC_SAVE_BG 1137 +#define IDS_REGISTRY 1137 +#define IDC_SAVE_OBJ 1138 +#define IDC_MAP_VIEW_ZOOM 1138 +#define IDS_MOVIE_PLAY 1138 +#define IDS_AUTOFIRE_DISABLED 1139 +#define IDC_VIEWER 1140 +#define IDS_AUTOFIRE_ALREADY_DISABLED 1140 +#define IDC_ADDRESSES 1141 +#define IDS_AUTOFIRE_START_DISABLED 1141 +#define IDS_AUTOFIRE_START 1142 +#define IDC_GO 1143 +#define IDS_AUTOFIRE_SELECT_DISABLED 1143 +#define IDC_8_BIT 1144 +#define IDS_AUTOFIRE_SELECT 1144 +#define IDC_16_BIT 1145 +#define IDS_AUTOFIRE_UP_DISABLED 1145 +#define IDC_32_BIT 1146 +#define IDS_AUTOFIRE_UP 1146 +#define IDC_OAM_VIEW 1147 +#define IDS_AUTOFIRE_DOWN_DISABLED 1147 +#define IDC_OAM_VIEW_ZOOM 1148 +#define IDS_AUTOFIRE_DOWN 1148 +#define IDS_AUTOFIRE_LEFT_DISABLED 1149 +#define IDC_SPRITE 1150 +#define IDS_AUTOFIRE_LEFT 1150 +#define IDC_POS 1151 +#define IDS_AUTOFIRE_RIGHT_DISABLED 1151 +#define IDC_MODE 1152 +#define IDS_AUTOFIRE_RIGHT 1152 +#define IDC_COLORS 1153 +#define IDC_MAPBASE 1153 +#define IDS_RERECORDED_STATE_N 1153 +#define IDC_PALETTE 1154 +#define IDC_CHARBASE 1154 +#define IDS_REPLAYED_STATE_N 1154 +#define IDC_TILE 1155 +#define IDC_DIM 1155 +#define IDC_PRIO 1156 +#define IDC_NUMCOLORS 1156 +#define IDC_SCROLLBAR 1157 +#define IDC_PRIORITY 1157 +#define IDC_MOSAIC 1158 +#define IDC_SIZE2 1159 +#define IDC_OVERFLOW 1159 +#define IDC_ROT 1160 +#define IDC_FLAGS 1161 +#define IDC_COMMANDS 1162 +#define IDC_BANK 1162 +#define IDC_CURRENTS 1163 +#define IDC_ASSIGN 1164 +#define IDC_RESET 1165 +#define IDC_EDIT_KEY 1166 +#define IDC_ALREADY_AFFECTED 1167 +#define IDC_TILE_VIEW 1168 +#define IDC_16_COLORS 1169 +#define IDC_256_COLORS 1170 +#define IDC_CHARBASE_0 1173 +#define IDC_CHARBASE_1 1174 +#define IDC_CHARBASE_2 1175 +#define IDC_CHARBASE_3 1176 +#define IDC_PALETTE_SLIDER 1177 +#define IDC_CHARBASE_4 1178 +#define IDC_COLOR_BG0 1178 +#define IDC_COLOR_BG1 1179 +#define IDC_URL 1179 +#define IDC_COLOR_BG2 1180 +#define IDC_STRETCH 1180 +#define IDC_COLOR_BG3 1181 +#define IDC_COLOR_OB0 1182 +#define IDC_COLOR_OB1 1183 +#define IDC_COLOR_OB2 1184 +#define IDC_COLOR_OB3 1185 +#define IDC_TRANSLATOR_URL 1186 +#define IDC_STATIC1 1187 +#define IDC_STATIC2 1188 +#define IDC_STATIC3 1189 +#define IDC_STATIC4 1190 +#define IDC_DEFAULT 1191 +#define IDC_USER1 1192 +#define IDC_USER2 1193 +#define IDC_DISASSEMBLE 1196 +#define IDC_AUTOMATIC 1199 +#define IDC_ARM 1200 +#define IDC_THUMB 1201 +#define IDC_RECNOW 1201 +#define IDC_RECRESET 1202 +#define IDC_JOYPAD1 1203 +#define IDC_RECSTART 1203 +#define IDC_REC_NOBIOS 1204 +#define IDC_AUTO_UPDATE 1204 +#define IDC_JOYPAD2 1204 +#define IDC_REC_EMUBIOS 1205 +#define IDC_JOYPAD3 1205 +#define IDC_REC_GBABIOS 1206 +#define IDC_JOYPAD4 1206 +#define IDC_REC_GBABIOSINTRO 1207 +#define IDC_MOVIE_FILENAME 1208 +#define IDC_N 1210 +#define IDC_LABEL_DATE 1210 +#define IDC_Z 1211 +#define IDC_LABEL_LENGTH 1211 +#define IDC_C 1212 +#define IDC_LABEL_FRAMES 1212 +#define IDC_V 1213 +#define IDC_LABEL_RERECORD 1213 +#define IDC_F 1214 +#define IDC_LABEL_FROM 1214 +#define IDC_I 1215 +#define IDC_T 1216 +#define IDC_LABEL_WARNING1 1216 +#define IDC_PORT 1217 +#define IDC_LABEL_WARNING2 1217 +#define IDC_VSCROLL 1218 +#define IDC_LABEL_FROM2 1218 +#define IDC_VERSION 1219 +#define IDC_VERBOSE_SWI 1223 +#define IDC_VERBOSE_UNALIGNED_ACCESS 1224 +#define IDC_VERBOSE_ILLEGAL_WRITE 1225 +#define IDC_VERBOSE_ILLEGAL_READ 1226 +#define IDC_LOG 1227 +#define IDC_CLEAR 1228 +#define IDC_VERBOSE_DMA0 1229 +#define IDC_VERBOSE_DMA1 1230 +#define IDC_TILE_NUMBER 1230 +#define IDC_VERBOSE_DMA2 1231 +#define IDC_XY 1231 +#define IDC_VERBOSE_DMA3 1232 +#define IDC_VERBOSE_UNDEFINED 1233 +#define IDC_TITLE 1234 +#define IDC_VERBOSE_AGBPRINT 1234 +#define IDC_CURRENT_ADDRESS 1235 +#define IDC_NOTES 1236 +#define IDC_CURRENT_ADDRESS_LABEL 1236 +#define IDC_LOAD 1238 +#define IDC_SIZE_CONTROL 1240 +#define IDC_MODES 1240 +#define IDC_DRIVERS 1241 +#define IDC_THROTTLE 1242 +#define IDC_READONLY 1242 +#define IDC_H 1243 +#define IDC_OAP 1244 +#define IDC_BANK_0 1245 +#define IDC_BANK_1 1246 +#define IDC_TIMER 1247 +#define IDC_INTERVAL 1248 +#define IDC_REWINDSLOTS 1249 +#define IDC_BIT_0 1250 +#define IDC_BIT_1 1251 +#define IDC_PREDEFINED 1251 +#define IDC_BIT_2 1252 +#define IDC_BUG_REPORT 1252 +#define IDC_BIT_3 1253 +#define IDC_COPY 1253 +#define IDC_BIT_4 1254 +#define IDC_BROWSE 1254 +#define IDC_BIT_5 1255 +#define IDC_COMBO1 1255 +#define IDC_MOVIE_REFRESH 1255 +#define IDC_BIT_6 1256 +#define IDC_REC_GBA 1257 +#define IDC_BIT_7 1257 +#define IDC_REC_GBC 1258 +#define IDC_BIT_8 1258 +#define IDC_REC_SGB 1259 +#define IDC_BIT_9 1259 +#define IDC_REC_SGB2 1260 +#define IDC_BIT_10 1260 +#define IDC_REC_GB 1260 +#define IDC_BIT_11 1261 +#define IDC_RADIO1 1261 +#define IDC_BIT_12 1262 +#define IDC_EDIT_PAUSEFRAME 1262 +#define IDC_BIT_13 1263 +#define IDC_CHECK_PAUSEFRAME 1263 +#define IDC_BIT_14 1264 +#define IDC_SLIDER1 1264 +#define IDC_BIT_15 1265 +#define IDC_CHECK_HIDEBORDER 1265 +#define IDC_RADIO_WHITE 1266 +#define IDC_RADIO_RED 1267 +#define IDC_RADIO_YELLOW 1268 +#define IDC_RADIO_GREEN 1269 +#define IDC_RADIO_CYAN 1270 +#define IDC_RADIO_BLUE 1271 +#define IDC_RADIO_MAGENTA 1272 +#define IDC_RADIO_BLACK 1273 +#define IDC_CHECK_OUTLINED 1274 +#define IDC_CHECK_TRANSPARENT 1275 +#define IDC_RADIO_PREFILTER 1276 +#define IDC_RADIO_POSTFILTER 1277 +#define IDC_RADIO_POSTRENDER 1278 +#define IDC_COMMANDS_HIDDEN 1279 +#define IDC_DECIMAL_DISPLAY 1281 +#define IDC_ALIGN 1282 +#define IDC_CHEATREFRESHBUTTON 1283 +#define IDC_EDIT_LUAPATH 1285 +#define IDC_BUTTON_LUABROWSE 1286 +#define IDS_FILTER_LUA 1287 +#define IDS_SELECT_LUA_NAME 1288 +#define IDS_SELECT_LUA_DIR 1289 +#define IDS_SELECT_MOVIE_DIR 1290 +#define IDS_SELECT_ROM_DIR 1291 +#define IDS_SELECT_GBXROM_DIR 1292 +#define IDS_SELECT_BATTERY_DIR 1293 +#define IDS_SELECT_SAVE_DIR 1294 +#define IDS_SELECT_CAPTURE_DIR 1295 +#define IDS_SELECT_AVI_DIR 1296 +#define IDS_SELECT_WAV_DIR 1297 +#define IDS_SELECT_CHEAT_DIR 1298 +#define IDS_SELECT_WATCH_DIR 1299 +#define IDS_SELECT_IPS_DIR 1300 +#define IDS_SELECT_PLUGIN_DIR 1301 +#define IDC_PLUGIN_DIR 1308 +#define IDC_WATCH_DIR 1309 +#define IDC_ROM_DIR 1310 +#define IDC_GBXROM_DIR 1311 +#define IDC_BATTERY_DIR 1312 +#define IDC_SAVE_DIR 1313 +#define IDC_MOVIE_DIR 1314 +#define IDC_CHEAT_DIR 1315 +#define IDC_LUA_DIR 1316 +#define IDC_AVI_DIR 1317 +#define IDC_WAV_DIR 1318 +#define IDC_CAPTURE_DIR 1319 +#define IDC_IPS_DIR 1320 +#define IDC_PLUGIN_PATH 1328 +#define IDC_WATCH_PATH 1329 +#define IDC_ROM_PATH 1330 +#define IDC_GBXROM_PATH 1331 +#define IDC_BATTERY_PATH 1332 +#define IDC_SAVE_PATH 1333 +#define IDC_MOVIE_PATH 1334 +#define IDC_CHEAT_PATH 1335 +#define IDC_LUA_PATH 1336 +#define IDC_AVI_PATH 1337 +#define IDC_WAV_PATH 1338 +#define IDC_CAPTURE_PATH 1339 +#define IDC_IPS_PATH 1340 +#define IDC_PLUGIN_DIR_RESET 1348 +#define IDC_WATCH_DIR_RESET 1349 +#define IDC_ROM_DIR_RESET 1350 +#define IDC_GBXROM_DIR_RESET 1351 +#define IDC_BATTERY_DIR_RESET 1352 +#define IDC_SAVE_DIR_RESET 1353 +#define IDC_MOVIE_DIR_RESET 1354 +#define IDC_CHEAT_DIR_RESET 1355 +#define IDC_LUA_DIR_RESET 1356 +#define IDC_AVI_DIR_RESET 1357 +#define IDC_WAV_DIR_RESET 1358 +#define IDC_CAPTURE_DIR_RESET 1359 +#define IDC_IPS_DIR_RESET 1360 +#define IDC_LIST1 1361 +#define IDC_BUTTON_LUARUN 1362 +#define IDC_BUTTON_LUASTOP 1363 +#define IDC_LUACONSOLE 1364 +#define IDC_BUTTON_LUAEDIT 1365 +#define IDC_LUACONSOLE_CLEAR 1366 +#define IDC_LUACONSOLE_CHOOSEFONT 1367 +#define IDC_ACCELEDIT_APPLY 1368 +#define IDC_ACCELEDIT_SELECTALL 1369 +#define IDC_ACCELEDIT_REPLACE 1370 +#define IDC_ACCELEDIT_ONFLYKEYEDIT 1371 +#define IDC_ACCELEDIT_PROGRESSBAR 1372 +#define IDC_ACCELEDIT_AUTOTIMEOUT 1373 +#define IDC_ACCELEDIT_UNDO 1374 +#define IDC_STATIC5 1375 +#define ID_HELP_ABOUT 40001 +#define ID_FILE_EXIT 40002 +#define ID_OPTIONS_SOUND_MUTE 40003 +#define ID_OPTIONS_VIDEO_VSYNC 40009 +#define ID_OPTIONS_VIDEO_X1 40010 +#define ID_OPTIONS_VIDEO_X2 40011 +#define ID_OPTIONS_VIDEO_X3 40012 +#define ID_OPTIONS_VIDEO_X4 40013 +#define ID_FILE_PAUSE 40014 +#define ID_OPTIONS_EMULATOR_DIRECTORIES 40015 +#define ID_OPTIONS_JOYPAD 40016 +#define ID_OPTIONS_EMULATOR_SYNCHRONIZE 40017 +#define ID_FILE_RESET 40018 +#define ID_FILE_LOAD 40019 +#define ID_OPTIONS_SOUND_DIRECTSOUNDA 40020 +#define ID_OPTIONS_SOUND_DIRECTSOUNDB 40021 +#define ID_OPTIONS_SOUND_DISABLE 40022 +#define ID_OPTIONS_SOUND_OFF 40023 +#define ID_OPTIONS_SOUND_ON 40024 +#define ID_OPTIONS_SOUND_CHANNEL1 40025 +#define ID_OPTIONS_SOUND_CHANNEL2 40026 +#define ID_OPTIONS_SOUND_CHANNEL3 40027 +#define ID_OPTIONS_SOUND_CHANNEL4 40028 +#define ID_OPTIONS_EMULATOR_USEBIOSFILE 40029 +#define ID_OPTIONS_EMULATOR_SELECTBIOSFILE 40030 +#define ID_CHEATS_SEARCHFORCHEATS 40031 +#define ID_CHEATS_ADDCHEAT 40032 +#define ID_OPTIONS_VIDEO_DISABLESFX 40033 +#define ID_OPTIONS_GAMEBOY_BORDER 40034 +#define ID_FILE_SAVEGAME_SLOT1 40035 +#define ID_FILE_SAVEGAME_SLOT2 40036 +#define ID_FILE_SAVEGAME_SLOT3 40037 +#define ID_FILE_SAVEGAME_SLOT4 40038 +#define ID_FILE_SAVEGAME_SLOT5 40039 +#define ID_FILE_SAVEGAME_SLOT6 40040 +#define ID_FILE_SAVEGAME_SLOT7 40041 +#define ID_FILE_SAVEGAME_SLOT8 40042 +#define ID_FILE_SAVEGAME_SLOT9 40043 +#define ID_FILE_SAVEGAME_SLOT10 40044 +#define ID_FILE_LOADGAME_SLOT1 40045 +#define ID_FILE_LOADGAME_SLOT2 40046 +#define ID_FILE_LOADGAME_SLOT3 40047 +#define ID_FILE_LOADGAME_SLOT4 40048 +#define ID_FILE_LOADGAME_SLOT5 40049 +#define ID_FILE_LOADGAME_SLOT6 40050 +#define ID_FILE_LOADGAME_SLOT7 40051 +#define ID_FILE_LOADGAME_SLOT8 40052 +#define ID_FILE_LOADGAME_SLOT9 40053 +#define ID_FILE_LOADGAME_SLOT10 40054 +#define ID_OPTIONS_GAMEBOY_AUTOMATIC 40057 +#define ID_OPTIONS_GAMEBOY_CGB 40058 +#define ID_OPTIONS_GAMEBOY_GBA 40059 +#define ID_OPTIONS_GAMEBOY_SGB 40060 +#define ID_OPTIONS_GAMEBOY_GB 40062 +#define ID_OPTIONS_GAMEBOY_REALCOLORS 40063 +#define ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS 40064 +#define ID_CHEATS_GAMEBOY 40065 +#define ID_OPTIONS_SOUND_11KHZ 40067 +#define ID_OPTIONS_SOUND_22KHZ 40068 +#define ID_OPTIONS_SOUND_44KHZ 40069 +#define ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY 40070 +#define ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY 40071 +#define ID_OPTIONS_PRIORITY_HIGHEST 40072 +#define ID_OPTIONS_PRIORITY_ABOVENORMAL 40073 +#define ID_OPTIONS_PRIORITY_NORMAL 40074 +#define ID_OPTIONS_PRIORITY_BELOWNORMAL 40075 +#define ID_OPTIONS_VIDEO_FULLSCREEN320X240 40076 +#define ID_OPTIONS_VIDEO_FULLSCREEN640X480 40077 +#define ID_OPTIONS_FILTER_NORMAL 40078 +#define ID_OPTIONS_FILTER_2XSAI 40079 +#define ID_OPTIONS_FILTER_SUPER2XSAI 40081 +#define ID_OPTIONS_FILTER_SUPEREAGLE 40082 +#define ID_OPTIONS_FILTER_TVMODE 40083 +#define ID_CHEATS_CHEATLIST 40084 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_A_OLD 40085 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_B_OLD 40086 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_L_OLD 40087 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_R_OLD 40088 +#define ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT 40089 +#define ID_OPTIONS_EMULATOR_ASSOCIATE 40091 +#define ID_OPTIONS_FILTER_DISABLEMMX 40093 +#define ID_OPTIONS_1X 40096 +#define ID_OPTIONS_2X 40097 +#define ID_OPTIONS_3X 40098 +#define ID_OPTIONS_4X 40099 +#define ID_FILE_ROMINFORMATION 40100 +#define ID_CHEATS_ADDCHEATCODE 40101 +#define ID_OPTIONS_VIDEO_DISABLESTATUSMESSAGES 40102 +#define ID_OPTIONS_JOYPAD_MOTIONCONFIGURE 40103 +#define ID_FILE_SCREENCAPTURE 40104 +#define ID_OPTIONS_LANGUAGE_SYSTEM 40105 +#define ID_OPTIONS_LANGUAGE_ENGLISH 40106 +#define ID_OPTIONS_LANGUAGE_OTHER 40107 +#define ID_OPTIONS_GAMEBOY_PRINTER 40108 +#define ID_FILE_RECENT_RESET 40109 +#define ID_CHEATS_SAVECHEATLIST 40110 +#define ID_CHEATS_LOADCHEATLIST 40111 +#define ID_CHEATS_AUTOMATICSAVELOADCHEATS 40112 +#define ID_FILE_IMPORT_GAMESHARKSNAPSHOT 40115 +#define ID_FILE_IMPORT_BATTERYFILE 40116 +#define ID_FILE_IMPORT_GAMESHARKCODEFILE 40117 +#define ID_FILE_EXPORT_BATTERYFILE 40118 +#define ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL 40121 +#define ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL 40122 +#define ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE 40123 +#define ID_TOOLS_SOUNDRECORDING 40124 +#define IDS_STARTSOUNDRECORDING 40125 +#define IDS_STOPSOUNDRECORDING 40126 +#define ID_OPTIONS_VIDEO_LAYERS_BG0 40127 +#define ID_OPTIONS_VIDEO_LAYERS_BG1 40128 +#define ID_OPTIONS_VIDEO_LAYERS_BG2 40129 +#define ID_OPTIONS_VIDEO_LAYERS_BG3 40130 +#define ID_OPTIONS_VIDEO_LAYERS_OBJ 40131 +#define ID_OPTIONS_VIDEO_LAYERS_WIN0 40132 +#define ID_OPTIONS_VIDEO_LAYERS_WIN1 40133 +#define ID_OPTIONS_VIDEO_LAYERS_OBJWIN 40134 +#define ID_FILE_OPENGAMEBOY 40135 +#define ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION 40136 +#define ID_DEBUG_NEXTFRAME 40137 +#define ID_TOOLS_MAPVIEW 40138 +#define ID_TOOLS_PALETTEVIEW 40139 +#define ID_OPTIONS_EMULATOR_PNGFORMAT 40140 +#define ID_OPTIONS_EMULATOR_BMPFORMAT 40141 +#define ID_TOOLS_CUSTOMIZE_COMMON 40142 +#define ID_TOOLS_MEMORYVIEWER 40143 +#define ID_TOOLS_OAMVIEWER 40144 +#define ID_TOOLS_TILEVIEWER 40146 +#define ID_OPTIONS_GAMEBOY_COLORS 40147 +#define ID_OPTIONS_SOUND_ECHO 40148 +#define ID_OPTIONS_SOUND_LOWPASSFILTER 40149 +#define ID_OPTIONS_SOUND_REVERSESTEREO 40150 +#define ID_TOOLS_DISASSEMBLE 40151 +#define ID_TOOLS_DEBUG_GDB 40152 +#define ID_TOOLS_DEBUG_LOADANDWAIT 40153 +#define ID_TOOLS_DEBUG_DISCONNECT 40154 +#define ID_TOOLS_DEBUG_BREAK 40155 +#define ID_TOOLS_LOGGING 40156 +#define ID_OPTIONS_EMULATOR_SPEEDHACK 40157 +#define ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE 40158 +#define ID_OPTIONS_EMULATOR_REMOVEINTROSGBA 40159 +#define ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X 40160 +#define ID_OPTIONS_FILTER16BIT_SIMPLE2X 40161 +#define ID_FILE_RECENT_FREEZE 40162 +#define ID_FILE_EXPORT_GAMESHARKSNAPSHOT 40163 +#define ID_OPTIONS_VIDEO_FULLSCREEN800X600 40164 +#define ID_OPTIONS_FILTER16BIT_SIMPLE3X 40165 +#define ID_OPTIONS_FILTER16BIT_SIMPLE4X 40166 +#define ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL3X 40167 +#define ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL4X 40168 +#define ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC 40169 +#define ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM 40170 +#define ID_OPTIONS_EMULATOR_SAVETYPE_SRAM 40171 +#define ID_OPTIONS_EMULATOR_SAVETYPE_FLASH 40172 +#define ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR 40173 +#define ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K 40174 +#define ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M 40175 +#define ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH 40176 +#define ID_TOOLS_AVIRECORDING 40177 +#define IDS_STARTAVIRECORDING 40178 +#define IDS_STOPAVIRECORDING 40179 +#define IDS_PAUSEAVIRECORDING 40180 +#define IDS_RESUMEAVIRECORDING 40181 +#define ID_OPTIONS_SOUND_VOLUME_1X 40182 +#define ID_OPTIONS_SOUND_VOLUME_2X 40183 +#define ID_OPTIONS_SOUND_VOLUME_3X 40184 +#define ID_OPTIONS_SOUND_VOLUME_4X 40185 +#define ID_OPTIONS_FILTER_BILINEAR 40186 +#define ID_OPTIONS_FILTER_BILINEARPLUS 40187 +#define ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE 40188 +#define ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR 40189 +#define ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART 40190 +#define ID_OPTIONS_VIDEO_FULLSCREEN 40191 +#define ID_OPTIONS_VIDEO_TRIPLEBUFFERING 40192 +#define ID_OPTIONS_FRAMESKIP_AUTOMATIC 40194 +#define ID_OPTIONS_EMULATOR_SHOWSPEED_NONE 40195 +#define ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE 40196 +#define ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED 40197 +#define ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT 40198 +#define ID_OPTIONS_JOYPAD_CONFIGURE_1 40199 +#define ID_OPTIONS_JOYPAD_CONFIGURE_2 40200 +#define ID_OPTIONS_JOYPAD_CONFIGURE_3 40201 +#define ID_OPTIONS_JOYPAD_CONFIGURE_4 40202 +#define ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1 40208 +#define ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_2 40209 +#define ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_3 40210 +#define ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4 40211 +#define ID_OPTIONS_EMULATOR_STORESETTINGSINREGISTRY 40214 +#define ID_FILE_EXPORT_SETTINGSTOINI 40215 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_NOTHROTTLE 40216 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER 40221 +#define ID_SYSTEM_MAXIMIZE 40222 +#define ID_OPTIONS_FILTER_SCANLINES 40223 +#define ID_OPTIONS_VIDEO_RENDERMETHOD_GDI 40228 +#define ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW 40229 +#define ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D 40230 +#define ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL 40231 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER 40233 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR 40234 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DTRILINEAR 40235 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DANISOTROPIC 40236 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST 40237 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR 40238 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE 40239 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS 40240 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_SELECTSKIN 40245 +#define ID_OPTIONS_VIDEO_RENDEROPTIONS_SKIN 40246 +#define ID_OPTIONS_EMULATOR_AGBPRINT 40247 +#define ID_OPTIONS_EMULATOR_REALTIMECLOCK 40248 +#define ID_OPTIONS_GAMEBOY_SGB2 40249 +#define ID_SYSTEM_MINIMIZE 40250 +#define ID_MOVIE_RECORD 40251 +#define ID_MOVIE_PLAY 40253 +#define ID_MOVIE_STOP 40254 +#define ID_OPTIONS_EMULATOR_AUTOHIDEMENU 40255 +#define ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC 40256 +#define ID_OPTIONS_EMULATOR_REWIND 40257 +#define ID_TOOLS_REWIND 40258 +#define ID_OPTIONS_EMULATOR_SKIPBIOS 40259 +#define ID_HELP_BUGREPORT 40260 +#define ID_HELP_FAQ 40261 +#define ID_OPTIONS_EMULATOR_REWINDINTERVAL 40262 +#define ID_FILE_TOGGLEMENU 40263 +#define ID_OPTIONS_EMULATOR_SAVETYPE_NONE 40264 +#define ID_OPTIONS_EMULATOR_SAVETYPE_ENHANCEDDETECTION 40265 +#define ID_TOOLS_IOVIEWER 40266 +#define ID_OPTIONS_SOUND_VOLUME_5X 40270 +#define ID_OPTIONS_SOUND_VOLUME_25X 40271 +#define ID_CHEATS_DISABLECHEATS 40272 +#define ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE 40273 +#define ID_OPTIONS_FILTER_LQ2X 40275 +#define ID_OPTIONS_FILTER_HQ2X 40276 +#define ID_OPTIONS_FILTER_HQ2X2 40277 +#define ID_OPTIONS_FILTER_HQ3X 40278 +#define ID_OPTIONS_FILTER_HQ3X2 40279 +#define ID_OPTIONS_FRAMESKIP_ACCURATEPITCH 40289 +#define ID_OPTIONS_FRAMESKIP_ACCURATESPEED 40290 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_6 40324 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_15 40325 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_25 40326 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_37 40327 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_50 40328 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_75 40329 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_87 40330 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_100 40331 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_112 40332 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_125 40333 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_150 40334 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_200 40335 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_300 40336 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_400 40337 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_600 40338 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_800 40339 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_1000 40340 +#define ID_TOOLS_FRAMECOUNTER 40341 +#define ID_TOOLS_INPUTDISPLAY 40342 +#define ID_OPTIONS_VIDEO_FRAMESKIP_0 40350 +#define ID_OPTIONS_VIDEO_FRAMESKIP_1 40351 +#define ID_OPTIONS_VIDEO_FRAMESKIP_2 40352 +#define ID_OPTIONS_VIDEO_FRAMESKIP_3 40353 +#define ID_OPTIONS_VIDEO_FRAMESKIP_4 40354 +#define ID_OPTIONS_VIDEO_FRAMESKIP_5 40355 +#define ID_OPTIONS_VIDEO_FRAMESKIP_6 40356 +#define ID_OPTIONS_VIDEO_FRAMESKIP_7 40357 +#define ID_OPTIONS_VIDEO_FRAMESKIP_8 40358 +#define ID_OPTIONS_VIDEO_FRAMESKIP_9 40359 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_INCREASE 40364 +#define ID_OPTIONS_FRAMESKIP_THROTTLE_DECREASE 40365 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_A 40366 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_B 40367 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_L 40368 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_R 40369 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_START 40370 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_SELECT 40371 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_UP 40372 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_DOWN 40373 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_LEFT 40374 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_RIGHT 40375 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR 40376 +#define ID_OPTIONS_SOUND_MUTEFRAMEADVANCE 40378 +#define ID_OPTIONS_JOYPAD_ALLOWLEFTRIGHT 40382 +#define ID_MOVIE_READONLY 40385 +#define ID_MOVIE_RESTART_PLAY 40387 +#define ID_MOVIE_RESUME_RECORD 40388 +#define ID_FILE_LOADGAME_AUTOLOADMOSTRECENT 40393 +#define ID_EMULATOR_GBALAG 40394 +#define ID_EMULATOR_USE_OLD_FRAME_TIMING 40395 +#define ID_EMULATOR_USE_GB_INPUT_KLUDGE 40396 +#define ID_OPTIONS_VIDEO_TEXTDISPLAYOPTIONS 40397 +#define ID_STICKY_A 40399 +#define ID_STICKY_B 40400 +#define ID_STICKY_R 40402 +#define ID_STICKY_START 40403 +#define ID_STICKY_SELECT 40404 +#define ID_STICKY_UP 40405 +#define ID_STICKY_DOWN 40406 +#define ID_STICKY_LEFT 40407 +#define ID_STICKY_RIGHT 40408 +#define ID_STICKY_L 40410 +#define ID_STICKY_CLEAR 40412 +#define ID_FILE_SAVEGAME_DECREMENTSLOT 40416 +#define ID_FILE_LOADGAME_MOSTRECENT 40420 +#define ID_FILE_LOADGAME_CURRENT 40423 +#define ID_FILE_LOADGAME_MAKECURRENT 40424 +#define ID_FILE_SAVEGAME_CURRENT 40427 +#define ID_FILE_SAVEGAME_MAKECURRENT 40428 +#define ID_FILE_SAVEGAME_OLDESTSLOT 40429 +#define ID_FILE_SAVEGAME_INCREMENTSLOT 40430 +#define ID_FILE_LOADGAME_MAKERECENT 40431 +#define ID_DEBUG_FRAMESEARCH 40432 +#define ID_DEBUG_FRAMESEARCHLOAD 40438 +#define ID_DEBUG_FRAMESEARCHPREV 40439 +#define ID_SELECT_SLOT1 40440 +#define ID_SELECT_SLOT2 40441 +#define ID_SELECT_SLOT3 40442 +#define ID_SELECT_SLOT4 40443 +#define ID_SELECT_SLOT5 40444 +#define ID_SELECT_SLOT6 40445 +#define ID_SELECT_SLOT7 40446 +#define ID_SELECT_SLOT8 40447 +#define ID_SELECT_SLOT9 40448 +#define ID_SELECT_SLOT10 40449 +#define ID_TOOLS_DEBUG 40450 +#define ID_TOOLS_CUSTOMIZE 40452 +#define ID_OPTIONS_SPEED 40453 +#define ID_SPEED_BLA 40454 +#define ID_SPEED_FRAMESKIP 40455 +#define ID_FRAMESKIP_BLA 40456 +#define ID_SPEED_SETSPEED 40457 +#define ID_SETSPEED_BLA 40458 +#define ID_SOUND_SOUNDCHANNELS 40459 +#define ID_CHEATS_PAUSEDURINGCHEATSEARCH 40463 +#define ID_TOOLS_LAGCOUNTER 40464 +#define ID_FILE_LUA_OPEN 40467 +#define ID_FILE_LUA_RELOAD 40468 +#define ID_FILE_LUA_CLOSE_ALL 40469 +#define ID_TOOLS_LAGCOUNTER_RESET 40470 +#define ID_FILE_QUICKSCREENCAPTURE 40471 +#define ID_MOVIE_END_EXIT 40480 +#define ID_MOVIE_END_RESET 40481 +#define ID_MOVIE_END_CLOSE 40482 +#define ID_MOVIE_END_OPENGBA 40483 +#define ID_MOVIE_END_OPENGBX 40484 +#define ID_MOVIE_END_LOADGAME 40485 +#define ID_MOVIE_END_SAVEGAME 40486 +#define ID_MOVIE_END_PAUSE 40487 +#define ID_MOVIE_END_RESTART 40488 +#define ID_MOVIE_END_APPEND 40489 +#define ID_MOVIE_END_STOP 40490 +#define ID_MOVIE_END_KEEP 40491 +#define ID_MOVIE_END_PLAY 40492 +#define ID_MOVIE_END_NEW 40493 +#define ID_MOVIE_TOOL_CONVERT 40501 +#define ID_MOVIE_TOOL_TRUNCATE 40504 +#define ID_MOVIE_TOOL_FIX_HEADER 40507 +#define ID_MOVIE_TOOL_SET_PAUSE_AT 40510 +#define ID_RAM_SEARCH 40522 +#define ID_RAM_WATCH 40523 +#define ID_MOVIE_TOOL_AUTO_CONVERT 40541 +#define ID_MOVIE_TOOL_EXTRACT_FROM_SAVEGAME 40560 +#define ID_MOVIE_TOOL_CONVERT_FILE 40561 +#define RAMMENU_FILE_RECENT 40653 +#define ID_WATCHES_MOVEUP 40664 +#define ID_WATCHES_MOVEDOWN 40665 +#define ID_WATCHES_EDITWATCH 40666 +#define ID_WATCHES_REMOVEWATCH 40667 +#define ID_WATCHES_NEWWATCH 40668 +#define ID_WATCHES_DUPLICATEWATCH 40669 +#define ID_WATCHES_UPDOWN 40670 +#define ID_FILE_LUA_STOP 40671 +#define IDC_EDIT_COMPAREVALUE 41090 +#define IDC_EDIT_COMPAREADDRESS 41091 +#define IDC_EDIT_COMPARECHANGES 41092 +#define IDC_EDIT_DIFFBY 41093 +#define IDC_C_AUTOSEARCH 41094 +#define IDC_EDIT_MODBY 41095 +#define IDC_RAMLIST 41133 +#define IDC_C_SEARCH 41134 +#define IDC_C_ADDCHEAT 41135 +#define IDC_C_RESET 41136 +#define IDC_1_BYTE 41137 +#define IDC_2_BYTES 41138 +#define IDC_C_WATCH 41139 +#define IDC_4_BYTES 41140 +#define IDC_LESSTHAN 41141 +#define IDC_MORETHAN 41142 +#define IDC_NOMORETHAN 41143 +#define IDC_NOLESSTHAN 41144 +#define IDC_EQUALTO 41145 +#define IDC_DIFFERENTFROM 41146 +#define IDC_DIFFERENTBY 41147 +#define IDC_MODULO 41148 +#define IDC_HEX 41151 +#define IDC_C_UNDO 41152 +#define IDC_C_RESET_CHANGES 41153 +#define IDC_PREVIOUSVALUE 42147 +#define IDC_SPECIFICVALUE 42148 +#define IDC_CHEATEDITVALUE 42156 +#define IDC_SPECIFICADDRESS 42157 +#define IDC_NUMBEROFCHANGES 42158 +#define IDC_MISALIGN 42159 +#define IDC_WATCHLIST 42171 +#define IDC_C_ELIMINATE 42174 +#define RAMMENU_FILE_AUTOLOAD 42300 +#define RAMMENU_FILE_APPEND 42301 +#define RAMMENU_FILE_SAVEWINDOW 42302 +#define RAMMENU_FILE_NEW 42303 +#define RAMMENU_FILE_OPEN 42304 +#define RAMMENU_FILE_SAVE 42305 +#define RAMMENU_FILE_SAVEAS 42306 +#define RW_MENU_FIRST_RECENT_FILE 42350 +#define RW_MENU_LAST_RECENT_FILE 42370 +#define ID_OPTIONS_PREFER_ARCHIVE_NAME 42400 +#define ID_OPTIONS_PREFER_ROM_NAME 42401 +#define ID_OPTIONS_EMULATOR_ALWAYSONTOP 42402 +#define ID_OPTIONS_EMULATOR_BACKGROUNDINPUT 42403 +#define ID_OPTIONS_SOUND_MUTEWHENINACTIVE 42404 +#define ID_FILE_SLOT_DISPLAYMODIFICATIONTIME 42405 +#define ID_TOOLS_PAUSEAVIRECORDING 42406 +#define ID_AUTO_TAKELAGINTOACCOUNT 42407 +#define ID_OPTIONS_JOYPAD_AUTOFIRE_ACCOUNTFORLAG 42408 +#define ID_FILE_FRAMEADVANCESETTINGS 42409 +#define ID_DEBUG_NEXTFRAME_ACCOUNTFORLAG 42410 +#define ID_MOVIE_ASSOCIATEMOVIESWITHSAVESTATES 42411 +#define ID_MOVIE_ASSC_WITH_SAVESTATE 42412 +#define ID_TOOLS_EXTRACOUNTER 42413 +#define ID_HEAD_EXTRACOUNTERRESET 42414 +#define ID_TOOLS_EXTRACOUNTERRESET 42415 +#define IDC_C_WATCH_DOWN 43400 +#define IDC_C_WATCH_DUPLICATE 43401 +#define IDC_C_WATCH_EDIT 43402 +#define IDC_C_WATCH_REMOVE 43403 +#define IDC_C_WATCH_UP 43404 +#define IDC_PROMPT_TEXT 44000 +#define IDC_PROMPT_TEXT2 44001 +#define IDC_PROMPT_EDIT 44005 +#define IDC_C_WATCH_SEPARATE 55555 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 171 +#define _APS_NEXT_COMMAND_VALUE 42416 +#define _APS_NEXT_CONTROL_VALUE 1376 +#define _APS_NEXT_SYMED_VALUE 103 +#endif +#endif diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/stdafx.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/stdafx.cpp Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// DialogSizer.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/stdafx.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/stdafx.h Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,47 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +// some build target defines: + +#ifndef _WIN32_WINDOWS +///# define _WIN32_WINDOWS 0x0410 // Windows 98 + +# define _WIN32_WINDOWS 0x0500 // Windows NT + +///# define _WIN32_WINDOWS 0x0501 // Windows XP + +#endif + +#ifndef _WIN32_WINNT +///# define _WIN32_WINNT 0x0410 // Windows 98 + +# define _WIN32_WINNT 0x0500 // Windows NT + +///# define _WIN32_WINNT 0x0501 // Windows XP + +#endif + +#ifndef WINVER +///# define WINVER 0x0410 // Windows 98 + +# define WINVER 0x0500 // Windows NT + +///# define WINVER 0x0501 // Windows XP + +#endif + +#if !defined(AFX_STDAFX_H__A7126ECB_A234_4116_A7D0_BE50547E87F8__INCLUDED_) +#define AFX_STDAFX_H__A7126ECB_A234_4116_A7D0_BE50547E87F8__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +// Insert your headers here +//#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers + +#include +#include +#include + +#include "../Port.h" + +// TODO: reference additional headers your program requires here + +//{{AFX_INSERT_LOCATION}} +// Microsoft Visual C++ will insert additional declarations immediately before the previous line. + +#endif // !defined(AFX_STDAFX_H__A7126ECB_A234_4116_A7D0_BE50547E87F8__INCLUDED_) diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/vba.rc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/vba.rc Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,2664 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" +#include "vba.rc2" +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (United States) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +#include "VersionInfo.h" +VS_VERSION_INFO VERSIONINFO + FILEVERSION VBA_VERSIONINFO_FILEVER + PRODUCTVERSION VBA_VERSIONINFO_PRODUCTVER + FILEFLAGSMASK 0x3fL + FILEFLAGS VBA_VERSIONINFO_FILEFLAGS + FILEOS 0x40004L + FILETYPE 0x1L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "Comments", "VBA-RR comes with NO WARRANTY. Use it at your own risk." + VALUE "CompanyName", VBA_VERSIONINFO_ORGANIZATION + VALUE "FileDescription", "VisualBoyAdvance-ReRecording Emulator" + VALUE "FileVersion", VBA_VERSIONINFO_STRFILEVER + VALUE "InternalName", "VBA-RR" + VALUE "LegalCopyright", VBA_VERSIONINFO_STRCOPYRIGHT + VALUE "OriginalFilename", "VisualBoyAdvance.exe" + VALUE "ProductName", "GB/C/A emulator for Windows" + VALUE "ProductVersion", VBA_VERSIONINFO_STRPRODUCTVER + VALUE "PrivateBuild", "0" + VALUE "SpecialBuild", "0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""afxres.h""\r\n" + "#include ""vba.rc2""\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Icon +// + +// Icon with lowest ID value placed first to ensure application icon +// remains consistent on all systems. +IDI_ICON ICON "gbadvance.ico" + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_OPENDLG DIALOG 36, 24, 202, 117 +STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | DS_CONTEXTHELP | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Open" +FONT 8, "MS Shell Dlg" +BEGIN + LTEXT "File &name:",1090,2,1,81,8 + EDITTEXT 1152,0,10,104,12,ES_AUTOHSCROLL | ES_OEMCONVERT + LISTBOX 1120,1,24,104,53,LBS_SORT | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP + LTEXT "&Folders:",-1,112,0,53,9 + LTEXT "",1088,113,10,86,9,SS_NOPREFIX + LISTBOX 1121,112,24,88,52,LBS_SORT | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_TABSTOP + LTEXT "List files of &type:",1089,1,75,81,9 + COMBOBOX 1136,1,87,104,13,CBS_DROPDOWNLIST | CBS_AUTOHSCROLL | WS_VSCROLL | WS_TABSTOP + LTEXT "Dri&ves:",1091,113,76,70,9 + COMBOBOX 1137,112,87,71,68,CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_AUTOHSCROLL | CBS_SORT | CBS_HASSTRINGS | WS_VSCROLL | WS_TABSTOP + DEFPUSHBUTTON "OK",IDOK,24,102,50,14,WS_GROUP + PUSHBUTTON "Cancel",IDCANCEL,90,102,50,14,WS_GROUP +END + +IDD_ABOUT DIALOGEX 0, 0, 164, 101 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "About VBA rerecording" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + DEFPUSHBUTTON "OK",IDOK,56,80,50,14 + ICON IDI_ICON,IDC_STATIC,5,12,20,20 + CTEXT "VisualBoyAdvance Emulator",IDC_STATIC,14,7,134,8 + CTEXT "Copyright (c) 2004 Forgotten and the VBA team",IDC_STATIC,7,35,150,8 + CTEXT "http://code.google.com/p/vba-rerecording/",IDC_URL,7,55,150,8 + CTEXT "Contribution by Costis",IDC_STATIC,30,45,102,8 + CTEXT "Version",IDC_STATIC,46,16,70,8 + CTEXT "",IDC_VERSION,36,25,98,8 + CTEXT "Re-recording additions by VBA-rr team",IDC_STATIC,14,66,136,8 +END + +IDD_DIRECTORIES DIALOGEX 0, 0, 284, 260 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Directories" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + LTEXT "GBA ROM:",IDC_STATIC,7,9,40,8 + EDITTEXT IDC_ROM_PATH,49,7,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_ROM_DIR,213,7,24,14 + PUSHBUTTON "Reset",IDC_ROM_DIR_RESET,242,7,35,14 + LTEXT "GBx ROM:",IDC_STATIC,7,28,40,8 + EDITTEXT IDC_GBXROM_PATH,49,26,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_GBXROM_DIR,213,26,24,14 + PUSHBUTTON "Reset",IDC_GBXROM_DIR_RESET,242,26,35,14 + LTEXT "Battery:",IDC_STATIC,7,47,40,8 + EDITTEXT IDC_BATTERY_PATH,49,45,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_BATTERY_DIR,213,45,24,14 + PUSHBUTTON "Reset",IDC_BATTERY_DIR_RESET,242,45,35,14 + LTEXT "Save Game:",IDC_STATIC,7,66,40,8 + EDITTEXT IDC_SAVE_PATH,49,64,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_SAVE_DIR,213,64,24,14 + PUSHBUTTON "Reset",IDC_SAVE_DIR_RESET,242,64,35,14 + LTEXT "Movies:",IDC_STATIC,7,85,40,8 + EDITTEXT IDC_MOVIE_PATH,49,83,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_MOVIE_DIR,213,83,24,14 + PUSHBUTTON "Reset",IDC_MOVIE_DIR_RESET,242,83,35,14 + LTEXT "Cheats:",IDC_STATIC,7,104,40,8 + EDITTEXT IDC_CHEAT_PATH,49,102,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_CHEAT_DIR,213,102,24,14 + PUSHBUTTON "Reset",IDC_CHEAT_DIR_RESET,242,102,35,14 + LTEXT "IPS:",IDC_STATIC,7,123,40,8 + EDITTEXT IDC_IPS_PATH,49,121,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_IPS_DIR,213,121,24,14 + PUSHBUTTON "Reset",IDC_IPS_DIR_RESET,242,121,35,14 + LTEXT "Lua:",IDC_STATIC,7,142,40,8 + EDITTEXT IDC_LUA_PATH,49,140,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_LUA_DIR,213,140,24,14 + PUSHBUTTON "Reset",IDC_LUA_DIR_RESET,242,140,35,14 + LTEXT "AVI:",IDC_STATIC,7,161,40,8 + EDITTEXT IDC_AVI_PATH,49,159,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_AVI_DIR,213,159,24,14 + PUSHBUTTON "Reset",IDC_AVI_DIR_RESET,242,159,35,14 + LTEXT "WAV:",IDC_STATIC,7,180,40,8 + EDITTEXT IDC_WAV_PATH,49,178,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_WAV_DIR,213,178,24,14 + PUSHBUTTON "Reset",IDC_WAV_DIR_RESET,242,178,35,14 + LTEXT "Capture:",IDC_STATIC,7,199,40,8 + EDITTEXT IDC_CAPTURE_PATH,49,197,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_CAPTURE_DIR,213,197,23,14 + PUSHBUTTON "Reset",IDC_CAPTURE_DIR_RESET,242,197,35,14 + LTEXT "Watch:",IDC_STATIC,7,218,40,8 + EDITTEXT IDC_WATCH_PATH,49,216,160,14,ES_AUTOHSCROLL + PUSHBUTTON "...",IDC_WATCH_DIR,213,216,23,14 + PUSHBUTTON "Reset",IDC_WATCH_DIR_RESET,242,216,35,14 + DEFPUSHBUTTON "OK",IDOK,88,236,50,16 + PUSHBUTTON "Cancel",IDCANCEL,145,236,50,16 +END + +IDD_CONFIG DIALOGEX 0, 0, 135, 211 +STYLE DS_SYSMODAL | DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Joypad setup" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + EDITTEXT IDC_EDIT_UP,47,5,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_DOWN,47,19,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_LEFT,47,33,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_RIGHT,47,47,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_A,47,61,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_B,47,75,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_L,47,89,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_R,47,103,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_SELECT,47,117,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_START,47,131,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_SPEED,47,145,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_CAPTURE,47,159,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_BUTTON_GS,47,173,81,12,ES_AUTOHSCROLL + PUSHBUTTON "OK",ID_OK,20,190,40,14 + PUSHBUTTON "Cancel",ID_CANCEL,74,190,40,14 + LTEXT "Up:",IDC_STATIC,5,5,35,10 + LTEXT "Down:",IDC_STATIC,5,19,35,10 + LTEXT "Left:",IDC_STATIC,5,33,35,10 + LTEXT "Right:",IDC_STATIC,5,47,35,10 + LTEXT "Button A:",IDC_STATIC,5,61,40,10 + LTEXT "Button B:",IDC_STATIC,5,75,40,10 + LTEXT "Button L:",IDC_STATIC,5,89,40,10 + LTEXT "Button R:",IDC_STATIC,5,103,40,10 + LTEXT "Select:",IDC_STATIC,5,117,40,10 + LTEXT "Start:",IDC_STATIC,5,131,40,10 + LTEXT "Speed:",IDC_STATIC,5,145,40,10 + LTEXT "Capture:",IDC_STATIC,5,159,40,10 + LTEXT "GameShark:",IDC_STATIC,5,173,40,10 +END + +IDD_CHEATS DIALOGEX 0, 0, 276, 253 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "Search for cheats" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + CONTROL "",IDC_CHEAT_LIST,"SysListView32",LVS_REPORT | LVS_SINGLESEL | WS_BORDER | WS_TABSTOP,3,5,265,111 + CONTROL "Ol&d value",IDC_OLD_VALUE,"Button",BS_AUTORADIOBUTTON | WS_GROUP,11,129,46,10 + CONTROL "Specifi&c value",IDC_SPECIFIC_VALUE,"Button",BS_AUTORADIOBUTTON,11,141,61,10 + CONTROL "&8 bits",IDC_SIZE_8,"Button",BS_AUTORADIOBUTTON | WS_GROUP,11,167,33,10 + CONTROL "&16 bits",IDC_SIZE_16,"Button",BS_AUTORADIOBUTTON,11,179,37,10 + CONTROL "&32 bits",IDC_SIZE_32,"Button",BS_AUTORADIOBUTTON,11,191,37,10 + CONTROL "&Equal",IDC_EQ,"Button",BS_AUTORADIOBUTTON | WS_GROUP,100,128,34,10 + CONTROL "&Not equal",IDC_NE,"Button",BS_AUTORADIOBUTTON,100,140,47,10 + CONTROL "&Less than",IDC_LT,"Button",BS_AUTORADIOBUTTON,100,152,47,10 + CONTROL "Le&ss or equal",IDC_LE,"Button",BS_AUTORADIOBUTTON,100,164,58,10 + CONTROL "&Greater than",IDC_GT,"Button",BS_AUTORADIOBUTTON,100,176,59,10 + CONTROL "G&reater or equal",IDC_GE,"Button",BS_AUTORADIOBUTTON,100,188,67,10 + CONTROL "S&igned",IDC_SIGNED,"Button",BS_AUTORADIOBUTTON | WS_GROUP,202,130,38,10 + CONTROL "&Unsigned",IDC_UNSIGNED,"Button",BS_AUTORADIOBUTTON,202,142,46,10 + CONTROL "&Hexadecimal",IDC_HEXADECIMAL,"Button",BS_AUTORADIOBUTTON,202,154,57,10 + CONTROL "search u&pdates vals",IDC_UPDATE,"Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,192,192,76,10 + EDITTEXT IDC_VALUE,95,211,172,14,ES_AUTOHSCROLL + PUSHBUTTON "&Start",IDC_START,15,237,50,14,WS_GROUP + PUSHBUTTON "S&earch",IDC_SEARCH,80,236,50,14 + PUSHBUTTON "&Add cheat",IDC_ADD_CHEAT,145,236,50,14 + DEFPUSHBUTTON "OK",ID_OK,210,236,50,14 + GROUPBOX "&Search type",IDC_STATIC,3,118,84,36 + GROUPBOX "&Data size",IDC_STATIC,3,158,84,44 + GROUPBOX "Compare type",IDC_STATIC,95,118,92,84 + GROUPBOX "Signed/Unsigned",IDC_STATIC,192,118,76,50 + LTEXT "Enter &value:",IDC_STATIC,3,214,69,8 + PUSHBUTTON "Refresh New &Values",IDC_CHEATREFRESHBUTTON,192,172,75,14,NOT WS_VISIBLE +END + +IDD_ADD_CHEAT DIALOG 0, 0, 186, 137 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Add cheat" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_ADDRESS,60,6,123,14,ES_AUTOHSCROLL + EDITTEXT IDC_VALUE,60,24,123,14,ES_AUTOHSCROLL + EDITTEXT IDC_DESC,60,42,123,14,ES_AUTOHSCROLL + CONTROL "8-bit",IDC_SIZE_8,"Button",BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,11,70,29,10 + CONTROL "16-bit",IDC_SIZE_16,"Button",BS_AUTORADIOBUTTON,62,70,33,10 + CONTROL "32-bit",IDC_SIZE_32,"Button",BS_AUTORADIOBUTTON,117,70,33,10 + CONTROL "&Signed",IDC_SIGNED,"Button",BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,11,97,38,10 + CONTROL "&Unsigned",IDC_UNSIGNED,"Button",BS_AUTORADIOBUTTON,62,98,46,10 + CONTROL "&Hexadecimal",IDC_HEXADECIMAL,"Button",BS_AUTORADIOBUTTON,117,98,57,10 + DEFPUSHBUTTON "&OK",ID_OK,36,116,50,14,WS_GROUP + PUSHBUTTON "&Cancel",ID_CANCEL,99,116,50,14 + LTEXT "&Value:",IDC_STATIC,3,27,54,8 + GROUPBOX "Number format",IDC_STATIC,3,88,180,24 + LTEXT "&Address:",IDC_STATIC,3,9,54,8 + GROUPBOX "Size",IDC_STATIC,3,60,180,24 + LTEXT "&Description:",IDC_STATIC,3,45,55,8 +END + +IDD_CHEAT_LIST DIALOG 0, 0, 280, 250 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Cheat list" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Restore &previous values",IDC_RESTORE,"Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,5,183,92,10 + PUSHBUTTON "&Code...",IDC_ADD_CODE,9,208,64,14,WS_GROUP + PUSHBUTTON "C&heat...",IDC_ADD_CHEAT,75,208,64,14 + PUSHBUTTON "&Gameshark...",IDC_ADD_GAMESHARK,141,208,64,14 + PUSHBUTTON "CodeBreaker...",IDC_ADD_CODEBREAKER,206,208,64,14 + PUSHBUTTON "&Remove",IDC_REMOVE,9,230,64,14 + PUSHBUTTON "Remove A&ll",IDC_REMOVE_ALL,75,230,64,14 + PUSHBUTTON "&Enable/Dis.",IDC_ENABLE,141,230,64,14 + DEFPUSHBUTTON "&OK",ID_OK,206,230,64,14,WS_GROUP + CONTROL "",IDC_CHEAT_LIST,"SysListView32",LVS_REPORT | WS_BORDER | WS_GROUP | WS_TABSTOP,5,15,269,156 + LTEXT "Status legend:",IDC_STATIC,6,3,46,8 + LTEXT "E: Enabled",IDC_STATIC,188,3,36,8 + LTEXT "D: Disabled",IDC_STATIC,234,3,38,8 + GROUPBOX "Add",IDC_STATIC,5,199,268,27 +END + +IDD_ASSOCIATIONS DIALOG 0, 0, 116, 95 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Associations" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL ".gb",IDC_GB,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,15,26,10 + CONTROL ".sgb",IDC_SGB,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,29,29,10 + CONTROL ".cgb",IDC_CGB,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,43,30,10 + CONTROL ".gbc",IDC_GBC,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,57,30,10 + CONTROL ".gba",IDC_GBA,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,75,13,30,10 + CONTROL ".agb",IDC_AGB,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,75,27,30,10 + CONTROL ".bin",IDC_BIN,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,75,41,27,10 + DEFPUSHBUTTON "OK",ID_OK,3,78,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,63,78,50,14 + GROUPBOX "GBA Types",IDC_STATIC,63,3,50,51 + GROUPBOX "GB Types",IDC_STATIC,3,3,50,69 +END + +IDD_GBA_ROM_INFO DIALOG 0, 0, 220, 142 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Rom information" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,84,121,50,14 + LTEXT "Game title:",IDC_STATIC,7,10,60,8 + LTEXT "Game code:",IDC_STATIC,7,24,60,8 + LTEXT "Maker code:",IDC_STATIC,7,38,60,8 + LTEXT "Main unit code:",IDC_STATIC,7,66,60,8 + LTEXT "Device type:",IDC_STATIC,7,80,60,8 + LTEXT "ROM version:",IDC_STATIC,7,94,60,8 + LTEXT "CRC:",IDC_STATIC,7,108,60,8 + LTEXT "",IDC_ROM_TITLE,80,10,133,8 + LTEXT "",IDC_ROM_GAME_CODE,80,24,133,8 + LTEXT "",IDC_ROM_MAKER_CODE,80,38,133,8 + LTEXT "",IDC_ROM_UNIT_CODE,80,66,133,8 + LTEXT "",IDC_ROM_DEVICE_TYPE,80,80,133,8 + LTEXT "",IDC_ROM_VERSION,80,94,133,8 + LTEXT "",IDC_ROM_CRC,80,108,133,8 + LTEXT "",IDC_ROM_MAKER_NAME,80,52,133,8 + LTEXT "Maker name:",IDC_STATIC,7,52,60,8 +END + +IDD_GB_ROM_INFO DIALOG 0, 0, 220, 225 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Rom information" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,84,200,50,14 + LTEXT "Game title:",IDC_STATIC,7,10,60,8 + LTEXT "Maker code:",IDC_STATIC,7,38,60,8 + LTEXT "Unit code:",IDC_STATIC,7,68,60,8 + LTEXT "Cartridge type:",IDC_STATIC,7,82,60,8 + LTEXT "ROM version:",IDC_STATIC,7,152,60,8 + LTEXT "CRC:",IDC_STATIC,7,166,60,8 + LTEXT "",IDC_ROM_TITLE,80,10,133,8 + LTEXT "",IDC_ROM_MAKER_CODE,80,38,133,8 + LTEXT "",IDC_ROM_UNIT_CODE,80,68,133,8 + LTEXT "",IDC_ROM_DEVICE_TYPE,80,82,133,8 + LTEXT "",IDC_ROM_VERSION,80,152,133,8 + LTEXT "",IDC_ROM_CRC,80,166,133,8 + LTEXT "Color:",IDC_STATIC,7,24,60,8 + LTEXT "",IDC_ROM_COLOR,80,24,133,8 + LTEXT "ROM size:",IDC_STATIC,7,96,60,8 + LTEXT "",IDC_ROM_SIZE,80,96,133,8 + LTEXT "RAM size:",IDC_STATIC,7,110,60,8 + LTEXT "",IDC_ROM_RAM_SIZE,80,110,133,8 + LTEXT "Dest. code:",IDC_STATIC,7,124,60,8 + LTEXT "",IDC_ROM_DEST_CODE,80,124,133,8 + LTEXT "License code:",IDC_STATIC,7,138,60,8 + LTEXT "",IDC_ROM_LIC_CODE,80,138,133,8 + LTEXT "Checksum:",IDC_STATIC,7,180,60,8 + LTEXT "",IDC_ROM_CHECKSUM,80,180,133,8 + LTEXT "",IDC_ROM_MAKER_NAME2,80,52,133,8 + LTEXT "Maker name:",IDC_STATIC,7,52,60,8 +END + +IDD_GB_CHEAT_LIST DIALOG 0, 0, 286, 221 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Gameboy Cheat List" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "",IDC_CHEAT_LIST,"SysListView32",LVS_REPORT | WS_BORDER | WS_GROUP | WS_TABSTOP,9,20,269,156 + PUSHBUTTON "Add &GameGenie...",IDC_ADD_GG_CHEAT,9,183,80,14,WS_GROUP + PUSHBUTTON "&Add GameShark...",IDC_ADD_GS_CHEAT,103,183,80,14,WS_GROUP + PUSHBUTTON "&Remove",IDC_REMOVE,197,183,80,14 + PUSHBUTTON "Remove A&ll",IDC_REMOVE_ALL,9,202,80,14 + PUSHBUTTON "&Enable/Dis.",IDC_ENABLE,103,202,80,14 + DEFPUSHBUTTON "&OK",ID_OK,197,202,80,14 + LTEXT "Status legend:",IDC_STATIC,10,9,46,8 + LTEXT "E: Enabled",IDC_STATIC,195,9,36,8 + LTEXT "D: Disabled",IDC_STATIC,241,9,38,8 +END + +IDD_ADD_CHEAT_DLG DIALOG 0, 0, 182, 107 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Title" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_DESC,60,7,120,14,ES_AUTOHSCROLL + EDITTEXT IDC_CODE,60,23,120,58,ES_MULTILINE | ES_UPPERCASE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN + DEFPUSHBUTTON "OK",ID_OK,33,86,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,99,86,50,14 + LTEXT "&Description:",IDC_STATIC,3,10,54,8 + LTEXT "&Code:",IDC_STATIC,3,29,54,8 +END + +IDD_GB_PRINTER DIALOG 0, 0, 178, 209 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "GB Printer" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "&1x",IDC_1X,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,166,22,10 + CONTROL "&2x",IDC_2X,"Button",BS_AUTORADIOBUTTON,55,166,23,10 + CONTROL "&3x",IDC_3X,"Button",BS_AUTORADIOBUTTON,98,166,23,10 + CONTROL "&4x",IDC_4X,"Button",BS_AUTORADIOBUTTON,141,166,23,10 + DEFPUSHBUTTON "&Print...",ID_PRINT,7,190,50,14,WS_GROUP + PUSHBUTTON "&Save...",ID_SAVE,64,190,50,14 + PUSHBUTTON "&Close",ID_OK,121,190,50,14 + CONTROL "",IDC_GB_PRINTER,"Static",SS_BLACKFRAME | WS_GROUP,7,6,162,146 + GROUPBOX "Print Size",IDC_STATIC,7,156,162,25 +END + +IDD_MOTION_CONFIG DIALOG 0, 0, 135, 78 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Motion Sensor" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_EDIT_UP,47,2,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_DOWN,47,16,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_LEFT,47,30,81,12,ES_AUTOHSCROLL + EDITTEXT IDC_EDIT_RIGHT,47,44,81,12,ES_AUTOHSCROLL + PUSHBUTTON "OK",ID_OK,20,60,40,14 + PUSHBUTTON "Cancel",ID_CANCEL,74,60,40,14 + LTEXT "Up:",IDC_STATIC,5,2,35,10 + LTEXT "Down:",IDC_STATIC,5,16,35,10 + LTEXT "Left:",IDC_STATIC,5,30,35,10 + LTEXT "Right:",IDC_STATIC,5,44,35,10 +END + +IDD_LANG_SELECT DIALOG 0, 0, 186, 68 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Language selection" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_LANG_STRING,140,25,40,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,30,49,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,104,49,50,14 + LTEXT "Current system language is:",IDC_STATIC,6,9,123,8 + LTEXT "Enter language name (3 letter):",IDC_STATIC,6,30,127,8 + LTEXT "",IDC_LANG_NAME,140,9,40,8 +END + +IDD_CODE_SELECT DIALOG 0, 0, 316, 235 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Select codes to import" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,91,214,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,174,214,50,14 + LISTBOX IDC_GAME_LIST,7,7,302,205,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP +END + +IDD_MAP_VIEW DIALOG 0, 0, 322, 238 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Map view" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Frame 0",IDC_FRAME_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,22,41,10 + CONTROL "Frame 1",IDC_FRAME_1,"Button",BS_AUTORADIOBUTTON,13,36,41,10 + CONTROL "BG0",IDC_BG0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,63,30,10 + CONTROL "BG1",IDC_BG1,"Button",BS_AUTORADIOBUTTON,13,77,30,10 + CONTROL "BG2",IDC_BG2,"Button",BS_AUTORADIOBUTTON,13,91,30,10 + CONTROL "BG3",IDC_BG3,"Button",BS_AUTORADIOBUTTON,13,105,30,10 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,7,122,68,10 + CONTROL "Auto update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,7,134,55,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,25,217,50,14,WS_GROUP + PUSHBUTTON "&Save...",IDC_SAVE,88,217,50,14,WS_GROUP + PUSHBUTTON "&Close",IDC_CLOSE,155,217,50,14 + CONTROL "MapView",IDC_MAP_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,187,15,128,128 + CONTROL "Zoom",IDC_MAP_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,7,148,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,187,164,48,47 + LTEXT "",IDC_R,245,173,50,8 + LTEXT "",IDC_G,245,185,50,8 + LTEXT "",IDC_B,245,197,50,8 + GROUPBOX "Frame",IDC_STATIC,7,11,63,37 + GROUPBOX "Background",IDC_STATIC,7,52,63,67 + LTEXT "",IDC_XY,129,95,53,8 + LTEXT "Mode:",IDC_STATIC,80,15,34,8 + LTEXT "",IDC_MODE,130,15,53,8 + LTEXT "Map Base:",IDC_STATIC,80,25,35,8 + LTEXT "",IDC_MAPBASE,130,25,53,8 + LTEXT "Char Base:",IDC_STATIC,80,35,36,8 + LTEXT "",IDC_CHARBASE,130,35,53,8 + LTEXT "Size:",IDC_STATIC,80,45,37,8 + LTEXT "",IDC_DIM,130,45,53,8 + LTEXT "Colors:",IDC_STATIC,80,55,37,8 + LTEXT "",IDC_NUMCOLORS,130,55,53,8 + LTEXT "Priority:",IDC_STATIC,80,65,37,8 + LTEXT "",IDC_PRIORITY,130,65,53,8 + LTEXT "Mosaic:",IDC_STATIC,80,75,37,8 + LTEXT "",IDC_MOSAIC,130,75,53,8 + LTEXT "Overflow:",IDC_STATIC,80,85,37,8 + LTEXT "",IDC_OVERFLOW,130,85,53,8 + LTEXT "Address:",IDC_STATIC,80,105,37,8 + LTEXT "",IDC_ADDRESS,130,105,53,8 + LTEXT "Tile:",IDC_STATIC,80,115,37,8 + LTEXT "",IDC_TILE_NUM,130,115,53,8 + LTEXT "Flip:",IDC_STATIC,80,125,37,8 + LTEXT "",IDC_FLIP,130,125,53,8 + LTEXT "Palette:",IDC_STATIC,80,135,37,8 + LTEXT "",IDC_PALETTE_NUM,130,135,53,8 +END + +IDD_PALETTE_VIEW DIALOG 0, 0, 316, 266 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Palette View" +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "Save BG...",IDC_SAVE_BG,30,245,50,14 + PUSHBUTTON "Save OBJ...",IDC_SAVE_OBJ,98,245,50,14 + PUSHBUTTON "&Refresh",IDC_REFRESH2,166,245,50,14 + PUSHBUTTON "&Close",IDC_CLOSE,234,245,50,14 + LTEXT "",IDC_ADDRESS,53,168,50,8 + LTEXT "",IDC_R,53,180,50,8 + LTEXT "",IDC_G,53,192,50,8 + LTEXT "",IDC_B,53,204,50,8 + LTEXT "",IDC_VALUE,53,216,50,8 + CONTROL "Custom1",IDC_COLOR,"VbaColorControl",WS_TABSTOP,161,168,50,50 + CONTROL "PaletteViewBG",IDC_PALETTE_VIEW,"VbaPaletteViewControl",WS_TABSTOP,12,30,128,128 + CONTROL "PaletteViewBG",IDC_PALETTE_VIEW_OBJ, + "VbaPaletteViewControl",WS_TABSTOP,166,30,128,128 + GROUPBOX "Background",IDC_STATIC,7,20,137,143 + GROUPBOX "Sprite",IDC_STATIC,161,20,137,143 + LTEXT "Address:",IDC_STATIC,7,168,38,8 + LTEXT "R:",IDC_STATIC,7,180,41,8 + LTEXT "G:",IDC_STATIC,7,192,43,8 + LTEXT "B:",IDC_STATIC,7,204,38,8 + LTEXT "Value:",IDC_STATIC,7,216,38,8 + LTEXT "Click on a color for more information",IDC_STATIC,7,7,302,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,229,71,10 +END + +IDD_MEM_VIEWER DIALOGEX 0, 0, 380, 178 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Memory viewer" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + COMBOBOX IDC_ADDRESSES,7,7,109,30,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + CONTROL "8-bit",IDC_8_BIT,"Button",BS_AUTORADIOBUTTON | WS_GROUP,120,9,29,10 + CONTROL "16-bit",IDC_16_BIT,"Button",BS_AUTORADIOBUTTON,154,9,33,10 + CONTROL "32-bit",IDC_32_BIT,"Button",BS_AUTORADIOBUTTON,192,9,33,10 + EDITTEXT IDC_ADDRESS,238,7,82,14,ES_UPPERCASE | ES_AUTOHSCROLL | ES_WANTRETURN | WS_GROUP + DEFPUSHBUTTON "&Go",IDC_GO,323,7,50,14,WS_GROUP + CONTROL "Viewer",IDC_VIEWER,"VbaMemoryViewer",WS_TABSTOP,7,22,366,112 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,139,71,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,67,157,50,14 + PUSHBUTTON "&Load...",IDC_LOAD,132,157,50,14 + PUSHBUTTON "&Save...",IDC_SAVE,197,157,50,14 + PUSHBUTTON "&Close",IDC_CLOSE,262,157,50,14 + LTEXT "Current address:",IDC_CURRENT_ADDRESS_LABEL,235,142,52,8 + EDITTEXT IDC_CURRENT_ADDRESS,291,139,82,14,ES_RIGHT | ES_AUTOHSCROLL | WS_DISABLED + CONTROL "Decimal Display",IDC_DECIMAL_DISPLAY,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,85,139,66,10 + CONTROL "Align to 16-byte",IDC_ALIGN,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,159,139,65,10 +END + +IDD_OAM_VIEW DIALOG 0, 0, 234, 185 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Oam Viewer" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_SPRITE,7,19,76,14,ES_RIGHT | ES_AUTOHSCROLL | ES_NUMBER + SCROLLBAR IDC_SCROLLBAR,7,33,76,11 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,138,79,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,7,164,50,14,WS_GROUP + PUSHBUTTON "&Save...",IDC_SAVE,91,164,50,14,WS_GROUP + PUSHBUTTON "&Close",IDC_CLOSE,177,164,50,14 + CONTROL "MapView",IDC_OAM_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,87,7,64,64 + CONTROL "Zoom",IDC_OAM_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,163,7,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,87,79,48,47 + LTEXT "",IDC_POS,31,47,50,8 + LTEXT "",IDC_MODE,31,57,50,8 + LTEXT "",IDC_COLORS,31,67,50,8 + LTEXT "",IDC_PALETTE,31,77,50,8 + LTEXT "",IDC_TILE,31,87,50,8 + LTEXT "",IDC_PRIO,31,97,50,8 + LTEXT "",IDC_SIZE2,31,107,50,8 + LTEXT "",IDC_ROT,31,117,50,8 + LTEXT "",IDC_FLAGS,31,127,50,8 + LTEXT "",IDC_R,145,88,50,8 + LTEXT "",IDC_G,145,100,50,8 + LTEXT "",IDC_B,145,112,50,8 + LTEXT "Pos:",IDC_STATIC,7,47,24,8 + LTEXT "Mode:",IDC_STATIC,7,57,24,8 + LTEXT "Colors:",IDC_STATIC,7,67,24,8 + LTEXT "Pal:",IDC_STATIC,7,77,24,8 + LTEXT "Tile:",IDC_STATIC,7,87,24,8 + LTEXT "Prio:",IDC_STATIC,7,97,24,8 + LTEXT "Size:",IDC_STATIC,7,107,24,8 + LTEXT "Sprite:",IDC_STATIC,7,7,50,8 + LTEXT "Rot.:",IDC_STATIC,7,117,24,8 + LTEXT "Flags:",IDC_STATIC,7,127,24,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,150,71,10 +END + +IDD_ACCEL_EDITOR DIALOGEX 0, 0, 320, 180 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Accelerator Editor" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + LTEXT "Commands:",IDC_STATIC,9,9,38,8 + LTEXT "Current Keys:",IDC_STATIC1,156,9,43,8 + LTEXT "Enter new accelerator:",IDC_STATIC3,156,87,99,8 + LTEXT "Static",IDC_ALREADY_AFFECTED,7,162,306,8,SS_CENTERIMAGE + LTEXT "Already assigned to:",IDC_STATIC2,7,148,85,8 + LTEXT "(0 to disable, millisec)",IDC_STATIC5,156,148,101,8 + LTEXT "Kill focus on timeout:",IDC_STATIC4,156,136,89,8 + CONTROL "",IDC_COMMANDS,"SysTreeView32",TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_DISABLEDRAGDROP | TVS_SHOWSELALWAYS | WS_BORDER | WS_HSCROLL | WS_TABSTOP,7,18,139,124 + EDITTEXT IDC_EDIT_KEY,156,100,100,12,ES_AUTOHSCROLL | NOT WS_TABSTOP,WS_EX_TRANSPARENT + CONTROL "",IDC_ACCELEDIT_PROGRESSBAR,"msctls_progress32",PBS_SMOOTH | WS_BORDER,156,116,100,14 + CONTROL "",IDC_CURRENTS,"SysListView32",LVS_REPORT | LVS_SHOWSELALWAYS | LVS_NOLABELWRAP | LVS_ALIGNLEFT | LVS_NOSCROLL | LVS_NOCOLUMNHEADER | WS_BORDER | WS_TABSTOP,156,19,100,63 + DEFPUSHBUTTON "&OK",ID_OK,263,7,50,14 + PUSHBUTTON "&Cancel",ID_CANCEL,263,25,50,14 + PUSHBUTTON "&Apply",IDC_ACCELEDIT_APPLY,264,44,49,14 + PUSHBUTTON "&Insert",IDC_ASSIGN,263,82,50,14 + PUSHBUTTON "&Delete",IDC_REMOVE,263,100,50,14 + PUSHBUTTON "&Replace",IDC_ACCELEDIT_REPLACE,263,118,50,14 + EDITTEXT IDC_ACCELEDIT_AUTOTIMEOUT,265,143,48,14,ES_AUTOHSCROLL | ES_NUMBER +END + +IDD_TILE_VIEWER DIALOG 0, 0, 326, 266 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Tile Viewer" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "16",IDC_16_COLORS,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,20,24,10 + CONTROL "256",IDC_256_COLORS,"Button",BS_AUTORADIOBUTTON,13,30,28,10 + CONTROL "0x6000000",IDC_CHARBASE_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,57,51,10 + CONTROL "0x6004000",IDC_CHARBASE_1,"Button",BS_AUTORADIOBUTTON,13,67,51,10 + CONTROL "0x6008000",IDC_CHARBASE_2,"Button",BS_AUTORADIOBUTTON,13,77,51,10 + CONTROL "0x600C000",IDC_CHARBASE_3,"Button",BS_AUTORADIOBUTTON,13,87,52,10 + CONTROL "0x6010000",IDC_CHARBASE_4,"Button",BS_AUTORADIOBUTTON,13,97,49,10 + CONTROL "Slider1",IDC_PALETTE_SLIDER,"msctls_trackbar32",TBS_AUTOTICKS | WS_GROUP | WS_TABSTOP,1,124,76,22 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,149,79,10 + PUSHBUTTON "Refresh",IDC_REFRESH,7,245,50,14,WS_GROUP + PUSHBUTTON "Save...",IDC_SAVE,138,245,50,14 + PUSHBUTTON "Close",IDC_CLOSE,269,245,50,14 + CONTROL "Custom1",IDC_TILE_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,191,7,128,128 + GROUPBOX "Colors",IDC_STATIC,7,7,66,38 + GROUPBOX "Char Base",IDC_STATIC,7,46,65,64 + CONTROL "Zoom",IDC_MAP_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,7,174,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,98,183,48,47 + LTEXT "",IDC_R,156,192,50,8 + LTEXT "",IDC_G,156,204,50,8 + LTEXT "",IDC_B,156,216,50,8 + LTEXT "Palette:",IDC_STATIC,7,113,65,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,160,71,10 + LTEXT "Tile:",IDC_STATIC,79,14,41,8 + LTEXT "Address:",IDC_STATIC,79,26,41,8 + LTEXT "",IDC_TILE_NUMBER,135,14,50,8 + LTEXT "",IDC_ADDRESS,135,26,50,8 +END + +IDD_GB_COLORS DIALOGEX 0, 0, 169, 121 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Gameboy Mono Colors" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + CONTROL "Default",IDC_DEFAULT,"Button",BS_AUTORADIOBUTTON | WS_GROUP,7,7,39,10 + CONTROL "User 1",IDC_USER1,"Button",BS_AUTORADIOBUTTON,67,7,37,10 + CONTROL "User 2",IDC_USER2,"Button",BS_AUTORADIOBUTTON,125,7,37,10 + COMBOBOX IDC_PREDEFINED,7,21,155,30,CBS_DROPDOWNLIST | CBS_SORT | WS_VSCROLL | WS_TABSTOP + PUSHBUTTON "",IDC_COLOR_BG0,15,47,28,14,WS_GROUP + PUSHBUTTON "",IDC_COLOR_BG1,52,47,28,14 + PUSHBUTTON "",IDC_COLOR_BG2,89,47,28,14 + PUSHBUTTON "",IDC_COLOR_BG3,126,47,28,14 + PUSHBUTTON "",IDC_COLOR_OB0,15,78,28,14 + PUSHBUTTON "",IDC_COLOR_OB1,52,78,28,14 + PUSHBUTTON "",IDC_COLOR_OB2,89,78,28,14 + PUSHBUTTON "",IDC_COLOR_OB3,126,78,28,14 + PUSHBUTTON "Reset",IDC_RESET,7,100,50,14 + DEFPUSHBUTTON "OK",ID_OK,59,100,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,111,100,50,14 + GROUPBOX "Background",IDC_STATIC,8,37,154,29 + GROUPBOX "Sprite",IDC_STATIC,8,67,154,30 +END + +IDD_DISASSEMBLE DIALOG 0, 0, 402, 225 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Disassemble" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Automatic",IDC_AUTOMATIC,"Button",BS_AUTORADIOBUTTON | WS_GROUP,7,9,47,10 + CONTROL "ARM",IDC_ARM,"Button",BS_AUTORADIOBUTTON,62,9,32,10 + CONTROL "THUMB",IDC_THUMB,"Button",BS_AUTORADIOBUTTON,103,9,42,10 + EDITTEXT IDC_ADDRESS,158,7,65,14,ES_UPPERCASE | ES_AUTOHSCROLL | WS_GROUP + DEFPUSHBUTTON "Go",IDC_GO,232,7,50,14 + LISTBOX IDC_DISASSEMBLE,7,25,276,161,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_TABSTOP + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,191,71,10 + PUSHBUTTON "Refresh",IDC_REFRESH,120,204,50,14 + PUSHBUTTON "Next",IDC_NEXT,233,204,50,14 + PUSHBUTTON "Close",IDC_CLOSE,346,204,50,14 + LTEXT "R0:",IDC_STATIC,309,7,18,8 + LTEXT "R1:",IDC_STATIC,309,15,18,8 + LTEXT "R2:",IDC_STATIC,309,23,18,8 + LTEXT "R3:",IDC_STATIC,309,31,18,8 + LTEXT "R4:",IDC_STATIC,309,39,18,8 + LTEXT "R5:",IDC_STATIC,309,47,18,8 + LTEXT "R6:",IDC_STATIC,309,55,18,8 + LTEXT "R7:",IDC_STATIC,309,63,18,8 + LTEXT "",IDC_R0,344,7,52,8 + LTEXT "",IDC_R1,344,15,52,8 + LTEXT "",IDC_R2,344,23,52,8 + LTEXT "",IDC_R3,344,31,52,8 + LTEXT "",IDC_R4,344,39,52,8 + LTEXT "",IDC_R5,344,47,52,8 + LTEXT "",IDC_R6,344,55,52,8 + LTEXT "",IDC_R7,344,63,52,8 + LTEXT "",IDC_R8,344,71,52,8 + LTEXT "",IDC_R9,344,79,52,8 + LTEXT "",IDC_R10,344,87,52,8 + LTEXT "",IDC_R11,344,95,52,8 + LTEXT "",IDC_R12,344,103,52,8 + LTEXT "",IDC_R13,344,111,52,8 + LTEXT "",IDC_R14,344,119,52,8 + LTEXT "",IDC_R15,344,127,52,8 + LTEXT "R8:",IDC_STATIC,309,71,18,8 + LTEXT "R9:",IDC_STATIC,309,79,18,8 + LTEXT "R10:",IDC_STATIC,309,87,18,8 + LTEXT "R11:",IDC_STATIC,309,95,18,8 + LTEXT "R12:",IDC_STATIC,309,103,18,8 + LTEXT "R13:",IDC_STATIC,309,111,18,8 + LTEXT "R14:",IDC_STATIC,309,119,18,8 + LTEXT "R15:",IDC_STATIC,309,127,18,8 + LTEXT "",IDC_R16,344,135,52,8 + LTEXT "R16:",IDC_STATIC,309,135,20,8 + CONTROL "N",IDC_N,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,309,146,21,10 + CONTROL "Z",IDC_Z,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,309,156,21,10 + CONTROL "C",IDC_C,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,309,166,21,10 + CONTROL "V",IDC_V,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,309,176,21,10 + CONTROL "F",IDC_F,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,342,156,20,10 + CONTROL "I",IDC_I,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,342,146,18,10 + CONTROL "T",IDC_T,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,342,166,21,10 + LTEXT "Mode:",IDC_STATIC,341,176,21,8 + LTEXT "",IDC_MODE,376,176,20,8 + SCROLLBAR IDC_VSCROLL,283,25,10,161,SBS_VERT + PUSHBUTTON "Goto R15",IDC_GOPC,7,204,50,14 +END + +IDD_GDB_PORT DIALOG 0, 0, 186, 51 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "GDB connection" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,34,30,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,102,30,50,14 + LTEXT "Port to wait for connection:",IDC_STATIC,7,10,105,8 + EDITTEXT IDC_PORT,125,7,54,14,ES_RIGHT | ES_AUTOHSCROLL +END + +IDD_GDB_WAITING DIALOG 0, 0, 186, 44 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Waiting..." +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "Cancel",ID_CANCEL,67,23,50,14 + LTEXT "Waiting for connection on port:",IDC_STATIC,7,7,117,8 + LTEXT "",IDC_PORT,143,7,36,8 +END + +IDD_LOGGING DIALOG 0, 0, 366, 218 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Logging" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "SWI",IDC_VERBOSE_SWI,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,17,87,10 + CONTROL "Unaligned memory",IDC_VERBOSE_UNALIGNED_ACCESS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,30,87,10 + CONTROL "Illegal write",IDC_VERBOSE_ILLEGAL_WRITE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,43,87,10 + CONTROL "Illegal read",IDC_VERBOSE_ILLEGAL_READ,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,56,87,10 + CONTROL "DMA 0",IDC_VERBOSE_DMA0,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,69,87,10 + CONTROL "DMA 1",IDC_VERBOSE_DMA1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,82,87,10 + CONTROL "DMA 2",IDC_VERBOSE_DMA2,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,95,87,10 + CONTROL "DMA 3",IDC_VERBOSE_DMA3,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,108,87,10 + CONTROL "Undefined instruction",IDC_VERBOSE_UNDEFINED,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,121,87,10 + CONTROL "AGBPrint",IDC_VERBOSE_AGBPRINT,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,134,87,10 + EDITTEXT IDC_LOG,107,7,252,183,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | WS_VSCROLL | WS_HSCROLL + PUSHBUTTON "Save...",IDC_SAVE,75,197,50,14 + PUSHBUTTON "Clear",IDC_CLEAR,137,197,50,14 + DEFPUSHBUTTON "OK",ID_OK,197,197,50,14 + GROUPBOX "Verbose",IDC_STATIC,7,7,93,142 +END + +IDD_EXPORT_SPS DIALOG 0, 0, 248, 148 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Export Gameshark Snapshot" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_TITLE,84,7,157,14,ES_AUTOHSCROLL + EDITTEXT IDC_DESC,84,27,157,14,ES_AUTOHSCROLL + EDITTEXT IDC_NOTES,84,47,157,73,ES_MULTILINE | ES_AUTOHSCROLL | ES_WANTRETURN + DEFPUSHBUTTON "OK",ID_OK,67,127,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,130,127,50,14 + LTEXT "Title:",IDC_STATIC,7,8,62,8 + LTEXT "Description:",IDC_STATIC,7,28,63,8 + LTEXT "Notes:",IDC_STATIC,7,48,60,8 +END + +IDD_ADDR_SIZE DIALOG 0, 0, 186, 67 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Enter address and size" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_ADDRESS,99,6,80,14,ES_AUTOHSCROLL + EDITTEXT IDC_SIZE_CONTROL,99,26,80,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,34,46,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,102,46,50,14 + LTEXT "Address:",IDC_STATIC,7,11,65,8 + LTEXT "Size:",IDC_STATIC,7,29,65,8 +END + +IDD_MODES DIALOG 0, 0, 208, 129 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Select video mode" +FONT 8, "MS Sans Serif" +BEGIN + LISTBOX IDC_MODES,7,18,194,80,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP + DEFPUSHBUTTON "OK",ID_OK,45,108,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,112,108,50,14 + LTEXT "Available video modes:",IDC_STATIC,7,7,194,8 +END + +IDD_DRIVERS DIALOG 0, 0, 208, 121 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Select video driver" +FONT 8, "MS Sans Serif" +BEGIN + LISTBOX IDC_DRIVERS,7,17,194,80,LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP + DEFPUSHBUTTON "OK",ID_OK,45,104,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,112,104,50,14 + LTEXT "Available drivers:",IDC_STATIC,7,7,194,8 +END + +IDD_THROTTLE DIALOGEX 0, 0, 186, 63 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Throttle" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + EDITTEXT IDC_THROTTLE,7,20,172,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,37,42,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,99,42,50,14 + LTEXT "Enter desired throttle (1%...1000%):",IDC_STATIC,7,7,172,8 +END + +IDD_GB_DISASSEMBLE DIALOG 0, 0, 344, 225 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "GB Disassemble" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_ADDRESS,7,7,65,14,ES_UPPERCASE | ES_AUTOHSCROLL | WS_GROUP + DEFPUSHBUTTON "Go",IDC_GO,81,7,50,14 + LISTBOX IDC_DISASSEMBLE,7,25,222,161,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_TABSTOP + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,191,71,10 + PUSHBUTTON "Refresh",IDC_REFRESH,100,204,50,14 + PUSHBUTTON "Next",IDC_NEXT,193,204,50,14 + PUSHBUTTON "Close",IDC_CLOSE,287,204,50,14 + LTEXT "AF:",IDC_STATIC,250,25,18,8 + LTEXT "BC:",IDC_STATIC,250,35,18,8 + LTEXT "DE:",IDC_STATIC,250,45,18,8 + LTEXT "HL:",IDC_STATIC,250,55,18,8 + LTEXT "IFF:",IDC_STATIC,250,85,18,8 + LTEXT "",IDC_R0,285,25,52,8 + LTEXT "",IDC_R1,285,35,52,8 + LTEXT "",IDC_R2,285,45,52,8 + LTEXT "",IDC_R3,285,55,52,8 + LTEXT "",IDC_R6,285,85,52,8 + CONTROL "N",IDC_N,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,250,109,21,10 + CONTROL "Z",IDC_Z,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,250,97,21,10 + CONTROL "C",IDC_C,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,250,133,21,10 + CONTROL "H",IDC_H,"Button",BS_AUTOCHECKBOX | WS_DISABLED | WS_TABSTOP,250,121,21,10 + SCROLLBAR IDC_VSCROLL,229,25,10,161,SBS_VERT + PUSHBUTTON "Goto PC",IDC_GOPC,7,204,50,14 + LTEXT "SP:",IDC_STATIC,250,65,18,8 + LTEXT "",IDC_R4,285,65,52,8 + LTEXT "PC:",IDC_STATIC,250,75,18,8 + LTEXT "",IDC_R5,285,75,52,8 +END + +IDD_GB_OAM_VIEW DIALOG 0, 0, 234, 185 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "GB Oam Viewer" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_SPRITE,7,19,76,14,ES_RIGHT | ES_AUTOHSCROLL | ES_NUMBER + SCROLLBAR IDC_SCROLLBAR,7,33,76,11 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,138,79,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,7,164,50,14,WS_GROUP + PUSHBUTTON "&Save...",IDC_SAVE,91,164,50,14,WS_GROUP + PUSHBUTTON "&Close",IDC_CLOSE,177,164,50,14 + CONTROL "MapView",IDC_OAM_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,87,7,64,64 + CONTROL "Zoom",IDC_OAM_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,163,7,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,87,79,48,47 + LTEXT "",IDC_POS,31,47,50,8 + LTEXT "",IDC_PALETTE,31,87,50,8 + LTEXT "",IDC_TILE,31,57,50,8 + LTEXT "",IDC_PRIO,31,67,50,8 + LTEXT "",IDC_OAP,31,77,50,8 + LTEXT "",IDC_FLAGS,31,97,50,8 + LTEXT "",IDC_R,145,88,50,8 + LTEXT "",IDC_G,145,100,50,8 + LTEXT "",IDC_B,145,112,50,8 + LTEXT "Pos:",IDC_STATIC,7,47,24,8 + LTEXT "Pal:",IDC_STATIC,7,87,24,8 + LTEXT "Tile:",IDC_STATIC,7,57,24,8 + LTEXT "Prio:",IDC_STATIC,7,67,24,8 + LTEXT "OAP:",IDC_STATIC,7,77,24,8 + LTEXT "Sprite:",IDC_STATIC,7,7,50,8 + LTEXT "Flags:",IDC_STATIC,7,97,24,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,150,71,10 + LTEXT "",IDC_BANK,31,107,50,8 + LTEXT "Bank:",IDC_STATIC,7,107,24,8 +END + +IDD_GB_TILE_VIEWER DIALOG 0, 0, 326, 238 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "GB Tile Viewer" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "0",IDC_BANK_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,20,20,10 + CONTROL "1",IDC_BANK_1,"Button",BS_AUTORADIOBUTTON,13,30,20,10 + CONTROL "0x8000",IDC_CHARBASE_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,57,39,10 + CONTROL "0x8800",IDC_CHARBASE_1,"Button",BS_AUTORADIOBUTTON,13,67,39,10 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,122,79,10 + PUSHBUTTON "Refresh",IDC_REFRESH,7,217,50,14,WS_GROUP + PUSHBUTTON "Save...",IDC_SAVE,138,217,50,14 + PUSHBUTTON "Close",IDC_CLOSE,269,217,50,14 + CONTROL "Custom1",IDC_TILE_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,191,7,128,128 + GROUPBOX "VRAM Bank",IDC_STATIC,7,7,66,38 + GROUPBOX "Char Base",IDC_STATIC,7,46,65,35 + CONTROL "Zoom",IDC_MAP_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,7,147,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,98,156,48,47 + LTEXT "",IDC_R,156,164,50,8 + LTEXT "",IDC_G,156,176,50,8 + LTEXT "",IDC_B,156,188,50,8 + LTEXT "Palette:",IDC_STATIC,7,86,65,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,133,71,10 + LTEXT "Tile:",IDC_STATIC,79,14,41,8 + LTEXT "Address:",IDC_STATIC,79,26,41,8 + LTEXT "",IDC_TILE_NUMBER,135,14,50,8 + LTEXT "",IDC_ADDRESS,135,26,50,8 + CONTROL "Slider1",IDC_PALETTE_SLIDER,"msctls_trackbar32",TBS_AUTOTICKS | WS_GROUP | WS_TABSTOP,1,98,76,22 +END + +IDD_GB_MAP_VIEW DIALOG 0, 0, 322, 238 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "GB Map Viewer" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "0x8000",IDC_BANK_0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,22,39,10 + CONTROL "0x8800",IDC_BANK_1,"Button",BS_AUTORADIOBUTTON,13,36,39,10 + CONTROL "0x9800",IDC_BG0,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,63,39,10 + CONTROL "0x9C00",IDC_BG1,"Button",BS_AUTORADIOBUTTON,13,77,40,10 + CONTROL "Stretch to fit",IDC_STRETCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,122,68,10 + PUSHBUTTON "&Refresh",IDC_REFRESH,25,217,50,14,WS_GROUP + PUSHBUTTON "&Save...",IDC_SAVE,88,217,50,14,WS_GROUP + PUSHBUTTON "&Close",IDC_CLOSE,155,217,50,14 + CONTROL "MapView",IDC_MAP_VIEW,"VbaBitmapControl",WS_GROUP | WS_TABSTOP,187,15,128,128 + CONTROL "Zoom",IDC_MAP_VIEW_ZOOM,"VbaZoomControl",WS_GROUP | WS_TABSTOP,7,148,64,64 + CONTROL "Color",IDC_COLOR,"VbaColorControl",WS_TABSTOP,187,164,48,47 + LTEXT "",IDC_R,245,173,50,8 + LTEXT "",IDC_G,245,185,50,8 + LTEXT "",IDC_B,245,197,50,8 + GROUPBOX "Char Base",IDC_STATIC,7,11,63,37 + GROUPBOX "Map Base",IDC_STATIC,7,52,63,41 + CONTROL "Auto update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,134,55,10 + LTEXT "",IDC_XY,129,18,53,8 + LTEXT "Priority:",IDC_STATIC,80,68,37,8 + LTEXT "",IDC_PRIORITY,130,68,53,8 + LTEXT "Address:",IDC_STATIC,80,28,37,8 + LTEXT "",IDC_ADDRESS,130,28,53,8 + LTEXT "Tile:",IDC_STATIC,80,38,37,8 + LTEXT "",IDC_TILE_NUM,130,38,53,8 + LTEXT "Flip:",IDC_STATIC,80,48,37,8 + LTEXT "",IDC_FLIP,130,48,53,8 + LTEXT "Palette:",IDC_STATIC,80,58,37,8 + LTEXT "",IDC_PALETTE_NUM,130,58,53,8 +END + +IDD_GB_PALETTE_VIEW DIALOG 0, 0, 196, 234 +STYLE DS_SETFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "GB Palette Viewer" +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "Save BG...",IDC_SAVE_BG,7,191,50,14 + PUSHBUTTON "Save OBJ...",IDC_SAVE_OBJ,73,191,50,14 + PUSHBUTTON "&Refresh",IDC_REFRESH2,139,191,50,14 + PUSHBUTTON "&Close",IDC_CLOSE,73,213,50,14 + LTEXT "",IDC_ADDRESS,53,117,50,8 + LTEXT "",IDC_R,53,129,50,8 + LTEXT "",IDC_G,53,141,50,8 + LTEXT "",IDC_B,53,153,50,8 + LTEXT "",IDC_VALUE,53,165,50,8 + CONTROL "Custom1",IDC_COLOR,"VbaColorControl",WS_TABSTOP,119,117,50,50 + CONTROL "PaletteViewBG",IDC_PALETTE_VIEW,"VbaPaletteViewControl",WS_TABSTOP,11,30,64,64 + CONTROL "PaletteViewBG",IDC_PALETTE_VIEW_OBJ, + "VbaPaletteViewControl",WS_TABSTOP,120,30,64,64 + GROUPBOX "BG",IDC_STATIC,6,20,74,81 + GROUPBOX "Sprite",IDC_STATIC,115,20,74,81 + LTEXT "Index:",IDC_STATIC,7,117,38,8 + LTEXT "R:",IDC_STATIC,7,129,41,8 + LTEXT "G:",IDC_STATIC,7,141,43,8 + LTEXT "B:",IDC_STATIC,7,153,38,8 + LTEXT "Value:",IDC_STATIC,7,165,38,8 + LTEXT "Click on a color for more information",IDC_STATIC,7,7,182,8 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,178,71,10 +END + +IDD_MODE_CONFIRM DIALOG 0, 0, 186, 57 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Confirm mode" +FONT 8, "MS Sans Serif" +BEGIN + DEFPUSHBUTTON "OK",ID_OK,31,36,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,103,36,50,14 + CTEXT "Do you want to keep the current mode?",IDC_STATIC,7,7,172,8 + CTEXT "",IDC_TIMER,7,19,172,8 +END + +IDD_REWIND_INTERVAL DIALOGEX 0, 0, 188, 132 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Rewind Settings" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + EDITTEXT IDC_INTERVAL,7,26,174,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,37,111,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,99,111,50,14 + LTEXT "Enter the rewind interval (0.2 ... 600) in seconds:",IDC_STATIC,7,7,174,8 + LTEXT "Or enter 0 to disable rewind.",IDC_STATIC,7,15,174,8 + EDITTEXT IDC_REWINDSLOTS,7,66,174,14,ES_AUTOHSCROLL + LTEXT "Enter number of rewind slots (1...256)",IDC_STATIC,7,46,174,8 + LTEXT "Or enter 0 to disable rewind.",IDC_STATIC,7,55,174,8 + LTEXT "If enabled, File->Rewind will rewind the state by seconds, as many as intervals back.",IDC_STATIC,7,87,173,17 +END + +IDD_IO_VIEWER DIALOG 0, 0, 269, 238 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "I/O Viewer" +FONT 8, "MS Sans Serif" +BEGIN + COMBOBOX IDC_ADDRESSES,7,7,255,30,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP + RTEXT "",IDC_VALUE,103,23,159,8 + CONTROL "",IDC_BIT_15,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,34,255,10 + CONTROL "",IDC_BIT_14,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,44,255,10 + CONTROL "",IDC_BIT_13,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,54,255,8 + CONTROL "",IDC_BIT_12,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,64,255,10 + CONTROL "",IDC_BIT_11,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,74,255,10 + CONTROL "",IDC_BIT_10,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,84,255,10 + CONTROL "",IDC_BIT_9,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,94,255,10 + CONTROL "",IDC_BIT_8,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,104,255,10 + CONTROL "",IDC_BIT_7,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,114,255,10 + CONTROL "",IDC_BIT_6,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,124,255,10 + CONTROL "",IDC_BIT_5,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,134,255,10 + CONTROL "",IDC_BIT_4,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,144,255,10 + CONTROL "",IDC_BIT_3,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,154,255,10 + CONTROL "",IDC_BIT_2,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,164,255,10 + CONTROL "",IDC_BIT_1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,174,255,10 + CONTROL "",IDC_BIT_0,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,184,255,10 + CONTROL "Automatic update",IDC_AUTO_UPDATE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,201,71,10 + DEFPUSHBUTTON "&Refresh",IDC_REFRESH,54,221,50,14 + DEFPUSHBUTTON "&Apply",IDC_APPLY,110,221,50,14 + PUSHBUTTON "&Close",IDC_CLOSE,166,221,50,14 + LTEXT "Value:",IDC_STATIC,7,23,72,8 +END + +IDD_MAX_SCALE DIALOG 0, 0, 186, 68 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Fullscreen scale" +FONT 8, "MS Sans Serif" +BEGIN + EDITTEXT IDC_VALUE,7,28,172,14,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",ID_OK,37,47,50,14 + PUSHBUTTON "Cancel",ID_CANCEL,99,47,50,14 + LTEXT "Enter the maxium fullscreen scale:",IDC_STATIC,7,7,172,8 + LTEXT "Enter 0 to use maximum scale.",IDC_STATIC,7,17,172,8 +END + +IDD_BUG_REPORT DIALOG 0, 0, 296, 186 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Bug Report" +FONT 8, "MS Sans Serif" +BEGIN + PUSHBUTTON "&OK",ID_OK,157,165,50,14 + EDITTEXT IDC_BUG_REPORT,11,22,278,131,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | WS_VSCROLL + LTEXT "Bug report data:",IDC_STATIC,7,7,282,8 + DEFPUSHBUTTON "&Copy",IDC_COPY,87,164,50,14 +END + +IDD_MOVIECREATE DIALOGEX 0, 0, 320, 168 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Record Movie" +FONT 8, "MS Sans Serif", 0, 0, 0x1 +BEGIN + DEFPUSHBUTTON "OK",IDOK,193,147,50,14 + PUSHBUTTON "Cancel",IDCANCEL,256,147,50,14 + EDITTEXT IDC_MOVIE_FILENAME,7,18,250,13,ES_AUTOHSCROLL + LTEXT "Movie File",-1,7,7,33,8 + PUSHBUTTON "&Browse...",IDC_BROWSE,264,17,48,14 + EDITTEXT IDC_EDIT_AUTHOR,57,35,200,14,ES_AUTOHSCROLL + LTEXT "Author:",-1,7,38,43,8,0,WS_EX_RIGHT + EDITTEXT IDC_EDIT_DESCRIPTION,57,52,255,14,ES_AUTOHSCROLL + LTEXT "Description:",-1,7,55,43,8,0,WS_EX_RIGHT + GROUPBOX "Game System",-1,116,74,85,64 + CONTROL "Game Boy Advance",IDC_REC_GBA,"Button",BS_AUTORADIOBUTTON | WS_GROUP,119,86,79,10 + CONTROL "Game Boy Color",IDC_REC_GBC,"Button",BS_AUTORADIOBUTTON,119,99,67,10 + CONTROL "Super Game Boy",IDC_REC_SGB,"Button",BS_AUTORADIOBUTTON,119,112,69,10 + CONTROL "Game Boy",IDC_REC_GB,"Button",BS_AUTORADIOBUTTON,119,124,49,10 + GROUPBOX "Bios Settings",-1,214,74,86,64 + GROUPBOX "Record Options",-1,18,74,85,64 + CONTROL "From now (snapshot)",IDC_RECNOW,"Button",BS_AUTORADIOBUTTON | WS_GROUP | WS_TABSTOP,21,118,77,10 + CONTROL "From reset (SRAM)",IDC_RECRESET,"Button",BS_AUTORADIOBUTTON | WS_TABSTOP,21,104,75,10 + CONTROL "From start (power-on)",IDC_RECSTART,"Button",BS_AUTORADIOBUTTON | WS_TABSTOP,21,90,78,10 + CONTROL "No BIOS",IDC_REC_NOBIOS,"Button",BS_AUTORADIOBUTTON | WS_GROUP,218,86,44,10 + CONTROL "Emulator's BIOS",IDC_REC_EMUBIOS,"Button",BS_AUTORADIOBUTTON,218,99,67,10 + CONTROL "GBA BIOS",IDC_REC_GBABIOS,"Button",BS_AUTORADIOBUTTON,218,112,49,10 + CONTROL "GBA BIOS with intro",IDC_REC_GBABIOSINTRO,"Button",BS_AUTORADIOBUTTON,218,124,79,10 +END + +IDD_MOVIEOPEN DIALOGEX 0, 0, 320, 240 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Play Movie" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + DEFPUSHBUTTON "OK",IDOK,196,214,50,14 + PUSHBUTTON "Cancel",IDCANCEL,255,214,50,14 + EDITTEXT IDC_MOVIE_FILENAME,7,18,250,13,ES_AUTOHSCROLL + LTEXT "Movie File",-1,7,7,33,8 + PUSHBUTTON "&Browse...",IDC_BROWSE,265,17,48,14 + LTEXT "Recording Date:",-1,12,87,53,8 + LTEXT "Length:",-1,40,99,25,8 + LTEXT "Frames:",-1,40,111,26,8 + LTEXT "Re-record Count:",-1,10,123,55,8 + LTEXT "DATE",IDC_LABEL_DATE,68,87,124,8 + LTEXT "LENGTH",IDC_LABEL_LENGTH,68,99,93,8 + LTEXT "FRAMES",IDC_LABEL_FRAMES,68,111,67,8 + LTEXT "RERECORD",IDC_LABEL_RERECORD,68,123,46,8 + CONTROL "Open Read-Only",IDC_READONLY,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,189,7,70,10 + LTEXT "Press OK to start playing the movie.\nNote that this will erase or overwrite your game's SRAM,\nand may change other emulator settings.",-1,8,209,177,24 + LTEXT "Movie ROM: ...",IDC_LABEL_WARNING1,8,34,300,8 + LTEXT "Your ROM: ...",IDC_LABEL_WARNING2,12,43,295,9,0,WS_EX_TRANSPARENT + EDITTEXT IDC_EDIT_AUTHOR,57,54,200,12,ES_AUTOHSCROLL | ES_READONLY + LTEXT "Author:",-1,7,56,43,8,0,WS_EX_RIGHT + EDITTEXT IDC_EDIT_DESCRIPTION,57,68,255,12,ES_AUTOHSCROLL + LTEXT "Description:",-1,7,69,43,8,0,WS_EX_RIGHT + GROUPBOX "Game System",-1,112,137,85,64 + RADIOBUTTON "Game Boy Advance",IDC_REC_GBA,116,149,79,10,WS_DISABLED | WS_GROUP + RADIOBUTTON "Game Boy Color",IDC_REC_GBC,116,162,67,10,WS_DISABLED + RADIOBUTTON "Super Game Boy",IDC_REC_SGB,116,175,69,10,WS_DISABLED + RADIOBUTTON "Game Boy",IDC_REC_GB,116,187,49,10,WS_DISABLED + GROUPBOX "Record Options",-1,18,137,77,64 + RADIOBUTTON "From snapshot",IDC_RECNOW,22,180,62,10,WS_DISABLED | WS_GROUP | WS_TABSTOP + RADIOBUTTON "From reset/SRAM",IDC_RECRESET,22,167,70,10,WS_DISABLED | WS_TABSTOP + RADIOBUTTON "From power-on",IDC_RECSTART,22,153,63,10,WS_DISABLED | WS_TABSTOP + RADIOBUTTON "No BIOS",IDC_REC_NOBIOS,219,149,44,10,WS_DISABLED | WS_GROUP + RADIOBUTTON "Emulator's BIOS",IDC_REC_EMUBIOS,219,162,67,10,WS_DISABLED + RADIOBUTTON "GBA BIOS",IDC_REC_GBABIOS,219,175,49,10,WS_DISABLED + RADIOBUTTON "GBA BIOS with intro",IDC_REC_GBABIOSINTRO,219,187,79,10,WS_DISABLED + GROUPBOX "Bios Settings",-1,214,137,86,64 + PUSHBUTTON "&Refresh",IDC_MOVIE_REFRESH,265,3,48,14 + EDITTEXT IDC_EDIT_PAUSEFRAME,254,109,45,12,ES_AUTOHSCROLL + CONTROL "Pause at frame:",IDC_CHECK_PAUSEFRAME,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,189,110,63,10 + CONTROL "Hide SGB Border",IDC_CHECK_HIDEBORDER,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,116,123,70,10 +END + +IDD_TEXTCONFIG DIALOGEX 0, 0, 139, 120 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Text Options" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + DEFPUSHBUTTON "OK",IDOK,69,99,63,14 + PUSHBUTTON "Cancel",IDCANCEL,7,99,53,14 + GROUPBOX "Color",IDC_STATIC,7,7,54,86 + CONTROL "White",IDC_RADIO_WHITE,"Button",BS_AUTORADIOBUTTON | WS_GROUP,13,16,35,8 + CONTROL "Red",IDC_RADIO_RED,"Button",BS_AUTORADIOBUTTON,13,25,29,8 + CONTROL "Yellow",IDC_RADIO_YELLOW,"Button",BS_AUTORADIOBUTTON,13,34,36,9 + CONTROL "Green",IDC_RADIO_GREEN,"Button",BS_AUTORADIOBUTTON,13,43,35,9 + CONTROL "Cyan",IDC_RADIO_CYAN,"Button",BS_AUTORADIOBUTTON,13,53,33,8 + CONTROL "Blue",IDC_RADIO_BLUE,"Button",BS_AUTORADIOBUTTON,13,63,29,8 + CONTROL "Magenta",IDC_RADIO_MAGENTA,"Button",BS_AUTORADIOBUTTON,13,72,44,8 + CONTROL "Black",IDC_RADIO_BLACK,"Button",BS_AUTORADIOBUTTON,13,82,32,8 + CONTROL "Outlined",IDC_CHECK_OUTLINED,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,74,17,43,10 + CONTROL "Transparent",IDC_CHECK_TRANSPARENT,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,74,28,55,10 + GROUPBOX "Mode",IDC_STATIC,69,7,63,36 + GROUPBOX "Display",IDC_STATIC,69,48,63,45 + CONTROL "In Game",IDC_RADIO_PREFILTER,"Button",BS_AUTORADIOBUTTON | WS_GROUP,73,58,43,10 + CONTROL "On Game",IDC_RADIO_POSTFILTER,"Button",BS_AUTORADIOBUTTON,73,69,45,10 + CONTROL "On Screen",IDC_RADIO_POSTRENDER,"Button",BS_AUTORADIOBUTTON,73,80,49,10 +END + +IDD_LUA DIALOGEX 0, 0, 270, 150 +STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_POPUP | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME +CAPTION "Lua Script" +MENU LUAWINDOW_MENU +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + PUSHBUTTON "Browse...",IDC_BUTTON_LUABROWSE,7,31,48,16 + PUSHBUTTON "Run",IDC_BUTTON_LUARUN,213,31,50,16 + PUSHBUTTON "Stop",IDC_BUTTON_LUASTOP,160,31,50,16 + EDITTEXT IDC_EDIT_LUAPATH,7,16,256,14,ES_AUTOHSCROLL + EDITTEXT IDC_LUACONSOLE,7,61,256,81,ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_VSCROLL + LTEXT "Output Console",IDC_STATIC,7,51,51,8 + LTEXT "Script File",IDC_STATIC,7,7,32,8 + PUSHBUTTON "Edit",IDC_BUTTON_LUAEDIT,58,31,46,16 +END + +IDD_RAMSEARCH DIALOGEX 0, 0, 287, 292 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION " RAM Search" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + CONTROL "",IDC_RAMLIST,"SysListView32",LVS_REPORT | LVS_SHOWSELALWAYS | LVS_OWNERDATA | WS_BORDER | WS_TABSTOP,9,9,214,151,WS_EX_CLIENTEDGE + PUSHBUTTON "&Search",IDC_C_SEARCH,226,9,52,16 + PUSHBUTTON "&Add Cheat",IDC_C_ADDCHEAT,226,145,52,16 + PUSHBUTTON "&Watch",IDC_C_WATCH,226,127,52,16 + PUSHBUTTON "&Reset",IDC_C_RESET,226,27,52,16 + PUSHBUTTON "&Eliminate",IDC_C_ELIMINATE,226,109,52,16 + GROUPBOX "Comparison Operator",IDC_STATIC,10,166,102,118,0,WS_EX_TRANSPARENT + CONTROL "Less Than",IDC_LESSTHAN,"Button",BS_AUTORADIOBUTTON | WS_GROUP,14,178,95,11 + CONTROL "Greater Than",IDC_MORETHAN,"Button",BS_AUTORADIOBUTTON,14,191,95,11 + CONTROL "Less Than or Equal To",IDC_NOMORETHAN,"Button",BS_AUTORADIOBUTTON,14,204,95,11 + CONTROL "Greater Than or Equal To",IDC_NOLESSTHAN,"Button",BS_AUTORADIOBUTTON,14,217,95,11 + CONTROL "Equal To",IDC_EQUALTO,"Button",BS_AUTORADIOBUTTON,14,230,95,11 + CONTROL "Not Equal To",IDC_DIFFERENTFROM,"Button",BS_AUTORADIOBUTTON,14,243,95,11 + CONTROL "Different By:",IDC_DIFFERENTBY,"Button",BS_AUTORADIOBUTTON,14,256,52,11 + CONTROL "Modulo",IDC_MODULO,"Button",BS_AUTORADIOBUTTON,14,269,35,11 + EDITTEXT IDC_EDIT_DIFFBY,69,255,38,12,ES_UPPERCASE | ES_AUTOHSCROLL | WS_DISABLED + EDITTEXT IDC_EDIT_MODBY,51,267,38,12,ES_UPPERCASE | ES_AUTOHSCROLL | WS_DISABLED + GROUPBOX "Compare To / By",IDC_STATIC,118,166,153,58,0,WS_EX_TRANSPARENT + CONTROL "Previous Value",IDC_PREVIOUSVALUE,"Button",BS_AUTORADIOBUTTON | WS_GROUP,121,176,67,10 + CONTROL "Specific Value:",IDC_SPECIFICVALUE,"Button",BS_AUTORADIOBUTTON,121,187,67,10 + CONTROL "Specific Address:",IDC_SPECIFICADDRESS,"Button",BS_AUTORADIOBUTTON,121,198,67,10 + CONTROL "Number of Changes:",IDC_NUMBEROFCHANGES,"Button",BS_AUTORADIOBUTTON,121,209,76,10 + EDITTEXT IDC_EDIT_COMPAREVALUE,203,183,63,12,ES_UPPERCASE | ES_AUTOHSCROLL | WS_DISABLED + EDITTEXT IDC_EDIT_COMPAREADDRESS,203,195,63,12,ES_UPPERCASE | ES_AUTOHSCROLL | WS_DISABLED + EDITTEXT IDC_EDIT_COMPARECHANGES,203,207,63,12,ES_UPPERCASE | ES_AUTOHSCROLL | WS_DISABLED + GROUPBOX "Data Type / Display",IDC_STATIC,196,227,75,44,0,WS_EX_TRANSPARENT + CONTROL "Signed",IDC_SIGNED,"Button",BS_AUTORADIOBUTTON | WS_GROUP,200,237,67,8 + CONTROL "Unsigned",IDC_UNSIGNED,"Button",BS_AUTORADIOBUTTON,200,248,67,8 + CONTROL "Hexadecimal",IDC_HEX,"Button",BS_AUTORADIOBUTTON,200,259,67,8 + CONTROL "Autosearch",IDC_C_AUTOSEARCH,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,200,273,52,11 + GROUPBOX "Data Size",IDC_STATIC,117,227,73,57,0,WS_EX_TRANSPARENT + CONTROL "1 byte",IDC_1_BYTE,"Button",BS_AUTORADIOBUTTON | WS_GROUP,121,237,61,11 + CONTROL "2 bytes",IDC_2_BYTES,"Button",BS_AUTORADIOBUTTON,121,248,61,11 + CONTROL "4 bytes",IDC_4_BYTES,"Button",BS_AUTORADIOBUTTON,121,259,61,11 + CONTROL "Check Misaligned",IDC_MISALIGN,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,121,272,65,8 + PUSHBUTTON "&Clear Change Counts",IDC_C_RESET_CHANGES,226,46,52,20,BS_MULTILINE + PUSHBUTTON "&Undo",IDC_C_UNDO,226,69,52,16,WS_DISABLED + LTEXT "Is",IDC_STATIC,92,270,12,8 +END + +IDD_EDITWATCH DIALOGEX 0, 0, 181, 95 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION " Edit Watch" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + CTEXT "Address:",IDC_SPECIFICADDRESS,18,12,35,10 + EDITTEXT IDC_EDIT_COMPAREADDRESS,55,10,65,12,ES_UPPERCASE | ES_AUTOHSCROLL + CTEXT "Notes:",IDC_PROMPT_TEXT,18,24,45,10 + EDITTEXT IDC_PROMPT_EDIT,55,22,65,12,ES_AUTOHSCROLL + GROUPBOX "Data Type",IDC_STATIC,14,37,75,42,0,WS_EX_TRANSPARENT + CONTROL "&Signed",IDC_SIGNED,"Button",BS_AUTORADIOBUTTON | WS_GROUP,18,47,67,8 + CONTROL "&Unsigned",IDC_UNSIGNED,"Button",BS_AUTORADIOBUTTON,18,57,67,8 + CONTROL "&Hexadecimal",IDC_HEX,"Button",BS_AUTORADIOBUTTON,18,67,67,8 + GROUPBOX "Data Size",IDC_STATIC,94,37,73,42,0,WS_EX_TRANSPARENT + CONTROL "&1 byte",IDC_1_BYTE,"Button",BS_AUTORADIOBUTTON | WS_GROUP,98,47,61,11 + CONTROL "&2 bytes",IDC_2_BYTES,"Button",BS_AUTORADIOBUTTON,98,57,61,11 + CONTROL "&4 bytes",IDC_4_BYTES,"Button",BS_AUTORADIOBUTTON,98,67,61,11 + DEFPUSHBUTTON "&OK",IDOK,66,80,50,14 + PUSHBUTTON "&Cancel",ID_CANCEL,120,80,50,14 +END + +IDD_RAMWATCH DIALOGEX 0, 0, 269, 242 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION " RAM Watch" +MENU RAMWATCH_MENU +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + CONTROL "",IDC_WATCHLIST,"SysListView32",LVS_REPORT | LVS_SHOWSELALWAYS | LVS_OWNERDATA | WS_BORDER | WS_TABSTOP,9,9,210,222,WS_EX_CLIENTEDGE + PUSHBUTTON "&Edit",IDC_C_WATCH_EDIT,226,66,34,14 + PUSHBUTTON "&Remove",IDC_C_WATCH_REMOVE,226,83,34,14 + PUSHBUTTON "&New",IDC_C_WATCH,226,100,34,14 + PUSHBUTTON "Duplic&ate",IDC_C_WATCH_DUPLICATE,226,117,34,14 + PUSHBUTTON "Add &Cheat",IDC_C_ADDCHEAT,222,156,42,16 + GROUPBOX "Watches",IDC_STATIC,222,11,42,144 + CONTROL "",ID_WATCHES_UPDOWN,"msctls_updown32",WS_TABSTOP,232,23,19,36 + PUSHBUTTON "Separator",IDC_C_WATCH_SEPARATE,225,135,36,14 +END + +IDD_PROMPT DIALOGEX 0, 0, 190, 70 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Input Prompt" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + EDITTEXT IDC_PROMPT_EDIT,10,30,170,15,ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",IDOK,100,50,50,15 + PUSHBUTTON "Cancel",ID_CANCEL,40,50,50,15 + CTEXT "",IDC_PROMPT_TEXT,0,3,190,8,SS_CENTERIMAGE + CTEXT "",IDC_PROMPT_TEXT2,0,15,190,8,SS_CENTERIMAGE +END + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO +BEGIN + IDD_OPENDLG, DIALOG + BEGIN + RIGHTMARGIN, 165 + END + + IDD_ABOUT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 157 + TOPMARGIN, 7 + BOTTOMMARGIN, 94 + END + + IDD_DIRECTORIES, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 277 + TOPMARGIN, 7 + BOTTOMMARGIN, 233 + END + + IDD_CONFIG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 128 + TOPMARGIN, 7 + BOTTOMMARGIN, 204 + END + + IDD_CHEATS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 269 + TOPMARGIN, 7 + BOTTOMMARGIN, 246 + END + + IDD_ADD_CHEAT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 130 + END + + IDD_CHEAT_LIST, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 273 + TOPMARGIN, 7 + BOTTOMMARGIN, 243 + END + + IDD_ASSOCIATIONS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 109 + TOPMARGIN, 7 + BOTTOMMARGIN, 88 + END + + IDD_GBA_ROM_INFO, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 213 + TOPMARGIN, 7 + BOTTOMMARGIN, 135 + END + + IDD_GB_ROM_INFO, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 213 + TOPMARGIN, 7 + BOTTOMMARGIN, 218 + END + + IDD_GB_CHEAT_LIST, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 279 + TOPMARGIN, 7 + BOTTOMMARGIN, 214 + END + + IDD_ADD_CHEAT_DLG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 175 + TOPMARGIN, 7 + BOTTOMMARGIN, 100 + END + + IDD_GB_PRINTER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 171 + TOPMARGIN, 7 + BOTTOMMARGIN, 202 + END + + IDD_MOTION_CONFIG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 128 + TOPMARGIN, 7 + BOTTOMMARGIN, 71 + END + + IDD_LANG_SELECT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 61 + END + + IDD_CODE_SELECT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 309 + TOPMARGIN, 7 + BOTTOMMARGIN, 228 + END + + IDD_MAP_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 315 + TOPMARGIN, 7 + BOTTOMMARGIN, 231 + END + + IDD_PALETTE_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 309 + TOPMARGIN, 7 + BOTTOMMARGIN, 259 + END + + IDD_MEM_VIEWER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 373 + TOPMARGIN, 7 + BOTTOMMARGIN, 171 + END + + IDD_OAM_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 227 + TOPMARGIN, 7 + BOTTOMMARGIN, 178 + END + + IDD_ACCEL_EDITOR, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 313 + TOPMARGIN, 7 + BOTTOMMARGIN, 172 + END + + IDD_TILE_VIEWER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 319 + TOPMARGIN, 7 + BOTTOMMARGIN, 259 + END + + IDD_GB_COLORS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 162 + TOPMARGIN, 7 + BOTTOMMARGIN, 114 + END + + IDD_DISASSEMBLE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 396 + TOPMARGIN, 7 + BOTTOMMARGIN, 218 + END + + IDD_GDB_PORT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 44 + END + + IDD_GDB_WAITING, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 37 + END + + IDD_LOGGING, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 359 + TOPMARGIN, 7 + BOTTOMMARGIN, 211 + END + + IDD_EXPORT_SPS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 241 + TOPMARGIN, 7 + BOTTOMMARGIN, 141 + END + + IDD_ADDR_SIZE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 60 + END + + IDD_MODES, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 201 + TOPMARGIN, 7 + BOTTOMMARGIN, 122 + END + + IDD_DRIVERS, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 201 + TOPMARGIN, 7 + BOTTOMMARGIN, 114 + END + + IDD_THROTTLE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 56 + END + + IDD_GB_DISASSEMBLE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 337 + TOPMARGIN, 7 + BOTTOMMARGIN, 218 + END + + IDD_GB_OAM_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 227 + TOPMARGIN, 7 + BOTTOMMARGIN, 178 + END + + IDD_GB_TILE_VIEWER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 319 + TOPMARGIN, 7 + BOTTOMMARGIN, 231 + END + + IDD_GB_MAP_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 315 + TOPMARGIN, 7 + BOTTOMMARGIN, 231 + END + + IDD_GB_PALETTE_VIEW, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 189 + TOPMARGIN, 7 + BOTTOMMARGIN, 227 + END + + IDD_MODE_CONFIRM, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 50 + END + + IDD_REWIND_INTERVAL, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 181 + TOPMARGIN, 7 + BOTTOMMARGIN, 125 + END + + IDD_IO_VIEWER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 262 + TOPMARGIN, 7 + BOTTOMMARGIN, 235 + END + + IDD_MAX_SCALE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 179 + TOPMARGIN, 7 + BOTTOMMARGIN, 61 + END + + IDD_BUG_REPORT, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 289 + TOPMARGIN, 7 + BOTTOMMARGIN, 179 + END + + IDD_MOVIECREATE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 312 + TOPMARGIN, 7 + BOTTOMMARGIN, 161 + END + + IDD_MOVIEOPEN, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 313 + TOPMARGIN, 7 + BOTTOMMARGIN, 235 + END + + IDD_TEXTCONFIG, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 132 + TOPMARGIN, 7 + BOTTOMMARGIN, 113 + END + + IDD_RAMSEARCH, DIALOG + BEGIN + END +END +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Menu +// + +IDR_MENU MENU +BEGIN + POPUP "&File" + BEGIN + MENUITEM "&Open GBA ROM...", ID_FILE_OPEN + MENUITEM "Open &GBx ROM...", ID_FILE_OPENGAMEBOY + POPUP "Rece&nt ROM" + BEGIN + MENUITEM "&Clear", ID_FILE_RECENT_RESET + MENUITEM "&Lock", ID_FILE_RECENT_FREEZE + MENUITEM SEPARATOR + MENUITEM "Open Recent ROM #&1", ID_FILE_MRU_FILE1 + MENUITEM "Open Recent ROM #&2", ID_FILE_MRU_FILE2 + MENUITEM "Open Recent ROM #&3", ID_FILE_MRU_FILE3 + MENUITEM "Open Recent ROM #&4", ID_FILE_MRU_FILE4 + MENUITEM "Open Recent ROM #&5", ID_FILE_MRU_FILE5 + MENUITEM "Open Recent ROM #&6", ID_FILE_MRU_FILE6 + MENUITEM "Open Recent ROM #&7", ID_FILE_MRU_FILE7 + MENUITEM "Open Recent ROM #&8", ID_FILE_MRU_FILE8 + MENUITEM "Open Recent ROM #&9", ID_FILE_MRU_FILE9 + MENUITEM "Open Recent ROM #1&0", ID_FILE_MRU_FILE10 + END + MENUITEM SEPARATOR + POPUP "&Load Game" + BEGIN + MENUITEM "&Current Slot", ID_FILE_LOADGAME_CURRENT + MENUITEM "Most &Recent Slot", ID_FILE_LOADGAME_MOSTRECENT + MENUITEM "Load From &File...", ID_FILE_LOAD + MENUITEM SEPARATOR + MENUITEM "Import &Battery File...", ID_FILE_IMPORT_BATTERYFILE + MENUITEM "Import &Gameshark Snapshot...", ID_FILE_IMPORT_GAMESHARKSNAPSHOT + MENUITEM SEPARATOR + MENUITEM "Slot #&1", ID_FILE_LOADGAME_SLOT1 + MENUITEM "Slot #&2", ID_FILE_LOADGAME_SLOT2 + MENUITEM "Slot #&3", ID_FILE_LOADGAME_SLOT3 + MENUITEM "Slot #&4", ID_FILE_LOADGAME_SLOT4 + MENUITEM "Slot #&5", ID_FILE_LOADGAME_SLOT5 + MENUITEM "Slot #&6", ID_FILE_LOADGAME_SLOT6 + MENUITEM "Slot #&7", ID_FILE_LOADGAME_SLOT7 + MENUITEM "Slot #&8", ID_FILE_LOADGAME_SLOT8 + MENUITEM "Slot #&9", ID_FILE_LOADGAME_SLOT9 + MENUITEM "Slot #1&0", ID_FILE_LOADGAME_SLOT10 + MENUITEM SEPARATOR + MENUITEM "&Make 'Current' On Load", ID_FILE_LOADGAME_MAKECURRENT + MENUITEM "Make 'Recent' On &Load", ID_FILE_LOADGAME_MAKERECENT + MENUITEM SEPARATOR + MENUITEM "&Auto Load Most Recent On Start", ID_FILE_LOADGAME_AUTOLOADMOSTRECENT + END + POPUP "&Save Game" + BEGIN + MENUITEM "&Current Slot", ID_FILE_SAVEGAME_CURRENT + MENUITEM "&Oldest Slot", ID_FILE_SAVEGAME_OLDESTSLOT + MENUITEM "Save To &File...", ID_FILE_SAVE + MENUITEM SEPARATOR + MENUITEM "Export &Battery File...", ID_FILE_EXPORT_BATTERYFILE + MENUITEM "Export &Gameshark Snapshot...", ID_FILE_EXPORT_GAMESHARKSNAPSHOT + MENUITEM SEPARATOR + MENUITEM "Slot #&1", ID_FILE_SAVEGAME_SLOT1 + MENUITEM "Slot #&2", ID_FILE_SAVEGAME_SLOT2 + MENUITEM "Slot #&3", ID_FILE_SAVEGAME_SLOT3 + MENUITEM "Slot #&4", ID_FILE_SAVEGAME_SLOT4 + MENUITEM "Slot #&5", ID_FILE_SAVEGAME_SLOT5 + MENUITEM "Slot #&6", ID_FILE_SAVEGAME_SLOT6 + MENUITEM "Slot #&7", ID_FILE_SAVEGAME_SLOT7 + MENUITEM "Slot #&8", ID_FILE_SAVEGAME_SLOT8 + MENUITEM "Slot #&9", ID_FILE_SAVEGAME_SLOT9 + MENUITEM "Slot #1&0", ID_FILE_SAVEGAME_SLOT10 + MENUITEM SEPARATOR + MENUITEM "&Make 'Current' On Save", ID_FILE_SAVEGAME_MAKECURRENT + END + POPUP "C&urrent Slot" + BEGIN + MENUITEM "&Increase Current Slot", ID_FILE_SAVEGAME_INCREMENTSLOT + MENUITEM "&Decrease Current Slot", ID_FILE_SAVEGAME_DECREMENTSLOT + MENUITEM SEPARATOR + MENUITEM "Slot #&1", ID_SELECT_SLOT1 + MENUITEM "Slot #&2", ID_SELECT_SLOT2 + MENUITEM "Slot #&3", ID_SELECT_SLOT3 + MENUITEM "Slot #&4", ID_SELECT_SLOT4 + MENUITEM "Slot #&5", ID_SELECT_SLOT5 + MENUITEM "Slot #&6", ID_SELECT_SLOT6 + MENUITEM "Slot #&7", ID_SELECT_SLOT7 + MENUITEM "Slot #&8", ID_SELECT_SLOT8 + MENUITEM "Slot #&9", ID_SELECT_SLOT9 + MENUITEM "Slot #1&0", ID_SELECT_SLOT10 + MENUITEM SEPARATOR + MENUITEM "Show &Timestamps", ID_FILE_SLOT_DISPLAYMODIFICATIONTIME + END + MENUITEM SEPARATOR + MENUITEM "&Pause", ID_FILE_PAUSE + MENUITEM "&Frame Advance", ID_DEBUG_NEXTFRAME + MENUITEM " &Account For Lag", ID_DEBUG_NEXTFRAME_ACCOUNTFORLAG + MENUITEM "Re&wind", ID_TOOLS_REWIND + MENUITEM "Rewind Se&ttings...", ID_OPTIONS_EMULATOR_REWINDINTERVAL + MENUITEM SEPARATOR + MENUITEM "&Reset", ID_FILE_RESET + MENUITEM "&Close", ID_FILE_CLOSE + MENUITEM SEPARATOR + MENUITEM "Rom &Information...", ID_FILE_ROMINFORMATION + MENUITEM SEPARATOR + MENUITEM "E&xit", ID_FILE_EXIT + END + POPUP "&Options" + BEGIN + POPUP "&Input" + BEGIN + POPUP "&Configure Controllers" + BEGIN + MENUITEM "Controller &1...", ID_OPTIONS_JOYPAD_CONFIGURE_1 + MENUITEM "Controller &2...", ID_OPTIONS_JOYPAD_CONFIGURE_2 + MENUITEM "Controller &3...", ID_OPTIONS_JOYPAD_CONFIGURE_3 + MENUITEM "Controller &4...", ID_OPTIONS_JOYPAD_CONFIGURE_4 + MENUITEM SEPARATOR + MENUITEM "&Motion Sensor...", ID_OPTIONS_JOYPAD_MOTIONCONFIGURE + END + POPUP "&Use Controller" + BEGIN + MENUITEM "Controller &1", ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_1 + MENUITEM "Controller &2", ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_2 + MENUITEM "Controller &3", ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_3 + MENUITEM "Controller &4", ID_OPTIONS_JOYPAD_DEFAULTJOYPAD_4 + END + MENUITEM SEPARATOR + MENUITEM "&Allow Left+Right / Up+Down", ID_OPTIONS_JOYPAD_ALLOWLEFTRIGHT + MENUITEM SEPARATOR + POPUP "Auto-&Fire" + BEGIN + MENUITEM "&A", ID_OPTIONS_JOYPAD_AUTOFIRE_A + MENUITEM "&B", ID_OPTIONS_JOYPAD_AUTOFIRE_B + MENUITEM "&L", ID_OPTIONS_JOYPAD_AUTOFIRE_L + MENUITEM "&R", ID_OPTIONS_JOYPAD_AUTOFIRE_R + MENUITEM "&Start", ID_OPTIONS_JOYPAD_AUTOFIRE_START + MENUITEM "Selec&t", ID_OPTIONS_JOYPAD_AUTOFIRE_SELECT + MENUITEM "&Up", ID_OPTIONS_JOYPAD_AUTOFIRE_UP + MENUITEM "&Down", ID_OPTIONS_JOYPAD_AUTOFIRE_DOWN + MENUITEM "L&eft", ID_OPTIONS_JOYPAD_AUTOFIRE_LEFT + MENUITEM "R&ight", ID_OPTIONS_JOYPAD_AUTOFIRE_RIGHT + MENUITEM SEPARATOR + MENUITEM "&Clear All", ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR + MENUITEM "Account &For Lag", ID_OPTIONS_JOYPAD_AUTOFIRE_ACCOUNTFORLAG + END + POPUP "Auto-&Hold" + BEGIN + MENUITEM "&A", ID_STICKY_A + MENUITEM "&B", ID_STICKY_B + MENUITEM "&L", ID_STICKY_L + MENUITEM "&R", ID_STICKY_R + MENUITEM "&Start", ID_STICKY_START + MENUITEM "Selec&t", ID_STICKY_SELECT + MENUITEM "&Up", ID_STICKY_UP + MENUITEM "&Down", ID_STICKY_DOWN + MENUITEM "L&eft", ID_STICKY_LEFT + MENUITEM "R&ight", ID_STICKY_RIGHT + MENUITEM SEPARATOR + MENUITEM "&Clear All", ID_STICKY_CLEAR + END + MENUITEM SEPARATOR + MENUITEM "Customi&ze All Commands...", ID_TOOLS_CUSTOMIZE + END + POPUP "&Speed" + BEGIN + POPUP "Set &Speed" + BEGIN + MENUITEM "6% (Very Slow)", ID_OPTIONS_FRAMESKIP_THROTTLE_6 + MENUITEM "15%", ID_OPTIONS_FRAMESKIP_THROTTLE_15 + MENUITEM "25%", ID_OPTIONS_FRAMESKIP_THROTTLE_25 + MENUITEM "50% (Half Speed)", ID_OPTIONS_FRAMESKIP_THROTTLE_50 + MENUITEM "75%", ID_OPTIONS_FRAMESKIP_THROTTLE_75 + MENUITEM "100% (Normal Speed)", ID_OPTIONS_FRAMESKIP_THROTTLE_100 + MENUITEM "150%", ID_OPTIONS_FRAMESKIP_THROTTLE_150 + MENUITEM "200% (Double Speed)", ID_OPTIONS_FRAMESKIP_THROTTLE_200 + MENUITEM "300%", ID_OPTIONS_FRAMESKIP_THROTTLE_300 + MENUITEM "400%", ID_OPTIONS_FRAMESKIP_THROTTLE_400 + MENUITEM "600% (Very Fast)", ID_OPTIONS_FRAMESKIP_THROTTLE_600 + MENUITEM "&Other...", ID_OPTIONS_FRAMESKIP_THROTTLE_OTHER + MENUITEM SEPARATOR + MENUITEM "&Increase", ID_OPTIONS_FRAMESKIP_THROTTLE_INCREASE + MENUITEM "&Decrease", ID_OPTIONS_FRAMESKIP_THROTTLE_DECREASE + END + POPUP "&Frame Skip" + BEGIN + MENUITEM "Skip &0 (Don't Skip Any Frames)", ID_OPTIONS_VIDEO_FRAMESKIP_0 + MENUITEM "Skip &1 (Skip Every Other Frame)", ID_OPTIONS_VIDEO_FRAMESKIP_1 + MENUITEM "Skip &2 (Skip 2 Out Of Every 3 Frames)", ID_OPTIONS_VIDEO_FRAMESKIP_2 + MENUITEM "Skip &3", ID_OPTIONS_VIDEO_FRAMESKIP_3 + MENUITEM "Skip &4", ID_OPTIONS_VIDEO_FRAMESKIP_4 + MENUITEM "Skip &5", ID_OPTIONS_VIDEO_FRAMESKIP_5 + MENUITEM "Skip &6", ID_OPTIONS_VIDEO_FRAMESKIP_6 + MENUITEM "Skip &7", ID_OPTIONS_VIDEO_FRAMESKIP_7 + MENUITEM "Skip &8", ID_OPTIONS_VIDEO_FRAMESKIP_8 + MENUITEM "Skip &9 (Skip 9 Out Of Every 10 Frames)", ID_OPTIONS_VIDEO_FRAMESKIP_9 + END + MENUITEM SEPARATOR + MENUITEM "&Turbo Mode", ID_OPTIONS_EMULATOR_SPEEDUPTOGGLE + MENUITEM SEPARATOR + MENUITEM "S&ynchronize Emulation Speed", ID_OPTIONS_EMULATOR_SYNCHRONIZE + MENUITEM " Accurate Sound &Pitch", ID_OPTIONS_FRAMESKIP_ACCURATEPITCH + MENUITEM " Accurate &Emulation Speed", ID_OPTIONS_FRAMESKIP_ACCURATESPEED + END + MENUITEM SEPARATOR + POPUP "&Video" + BEGIN + POPUP "&Render Method" + BEGIN + MENUITEM "&GDI", ID_OPTIONS_VIDEO_RENDERMETHOD_GDI + MENUITEM SEPARATOR + MENUITEM "&DirectDraw", ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECTDRAW + MENUITEM " DDraw &Emulation Only", ID_OPTIONS_VIDEO_DDRAWEMULATIONONLY + MENUITEM " DDraw &Use Video Memory", ID_OPTIONS_VIDEO_DDRAWUSEVIDEOMEMORY + MENUITEM " DDraw &Triple Buffering", ID_OPTIONS_VIDEO_TRIPLEBUFFERING + MENUITEM " DDraw &VSync", ID_OPTIONS_VIDEO_VSYNC + MENUITEM SEPARATOR + MENUITEM "Direct&3D", ID_OPTIONS_VIDEO_RENDERMETHOD_DIRECT3D + MENUITEM " D3D &Nearest", ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DNOFILTER + MENUITEM " D3D &Bilinear", ID_OPTIONS_VIDEO_RENDEROPTIONS_D3DBILINEAR + MENUITEM SEPARATOR + MENUITEM "&OpenGL", ID_OPTIONS_VIDEO_RENDERMETHOD_OPENGL + MENUITEM " GL Ne&arest", ID_OPTIONS_VIDEO_RENDEROPTIONS_GLNEAREST + MENUITEM " GL B&ilinear", ID_OPTIONS_VIDEO_RENDEROPTIONS_GLBILINEAR + MENUITEM " GL T&riangle", ID_OPTIONS_VIDEO_RENDEROPTIONS_GLTRIANGLE + MENUITEM " GL &Quads", ID_OPTIONS_VIDEO_RENDEROPTIONS_GLQUADS + END + MENUITEM SEPARATOR + MENUITEM "&1x Window Size", ID_OPTIONS_VIDEO_X1 + MENUITEM "&2x Window Size", ID_OPTIONS_VIDEO_X2 + MENUITEM "&3x Window Size", ID_OPTIONS_VIDEO_X3 + MENUITEM "&4x Window Size", ID_OPTIONS_VIDEO_X4 + MENUITEM SEPARATOR + MENUITEM "Full Screen (320x240)", ID_OPTIONS_VIDEO_FULLSCREEN320X240 + MENUITEM "Full Screen (640x480)", ID_OPTIONS_VIDEO_FULLSCREEN640X480 + MENUITEM "Full Screen (800x600)", ID_OPTIONS_VIDEO_FULLSCREEN800X600 + MENUITEM "&Other Full Screen...", ID_OPTIONS_VIDEO_FULLSCREEN + MENUITEM SEPARATOR + MENUITEM "Full Screen &Max Scale...", ID_OPTIONS_VIDEO_FULLSCREENMAXSCALE + MENUITEM "Full Screen &Ignore Ratio", ID_OPTIONS_VIDEO_FULLSCREENSTRETCHTOFIT + MENUITEM "Full Screen &Hide Menu", ID_FILE_TOGGLEMENU + MENUITEM "Full Screen &Auto Hide Menu", ID_OPTIONS_EMULATOR_AUTOHIDEMENU + MENUITEM SEPARATOR + MENUITEM "&Disable SFX", ID_OPTIONS_VIDEO_DISABLESFX + POPUP "Graphics &Layers" + BEGIN + MENUITEM "BG 0", ID_OPTIONS_VIDEO_LAYERS_BG0 + MENUITEM "BG 1", ID_OPTIONS_VIDEO_LAYERS_BG1 + MENUITEM "BG 2", ID_OPTIONS_VIDEO_LAYERS_BG2 + MENUITEM "BG 3", ID_OPTIONS_VIDEO_LAYERS_BG3 + MENUITEM "OBJ", ID_OPTIONS_VIDEO_LAYERS_OBJ + MENUITEM "WIN 0", ID_OPTIONS_VIDEO_LAYERS_WIN0 + MENUITEM "WIN 1", ID_OPTIONS_VIDEO_LAYERS_WIN1 + MENUITEM "OBJ WIN", ID_OPTIONS_VIDEO_LAYERS_OBJWIN + END + END + POPUP "Pixel &Filter" + BEGIN + POPUP "&Interframe Blending" + BEGIN + MENUITEM "&None", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_NONE + MENUITEM SEPARATOR + MENUITEM "&Motion Blur", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_MOTIONBLUR + MENUITEM "&Smart", ID_OPTIONS_FILTER_INTERFRAMEBLENDING_SMART + END + MENUITEM SEPARATOR + POPUP "&Miscellaneous 2x" + BEGIN + MENUITEM "&None", ID_OPTIONS_FILTER_NORMAL + MENUITEM SEPARATOR + MENUITEM "&TV Mode", ID_OPTIONS_FILTER_TVMODE + MENUITEM "Scan&lines", ID_OPTIONS_FILTER_SCANLINES + MENUITEM SEPARATOR + MENUITEM "&Bilinear", ID_OPTIONS_FILTER_BILINEAR + MENUITEM "Bilinear Pl&us", ID_OPTIONS_FILTER_BILINEARPLUS + MENUITEM SEPARATOR + MENUITEM "&Motion Blur", ID_OPTIONS_FILTER16BIT_MOTIONBLUREXPERIMENTAL + END + POPUP "&2x Magnification" + BEGIN + MENUITEM "Simple 2&x", ID_OPTIONS_FILTER16BIT_SIMPLE2X + MENUITEM "&Pixelate 2x", ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL + MENUITEM SEPARATOR + MENUITEM "&2xSaI", ID_OPTIONS_FILTER_2XSAI + MENUITEM "&Super 2xSaI", ID_OPTIONS_FILTER_SUPER2XSAI + MENUITEM "Super &Eagle", ID_OPTIONS_FILTER_SUPEREAGLE + MENUITEM "&AdvanceMAME Scale2x", ID_OPTIONS_FILTER16BIT_ADVANCEMAMESCALE2X + MENUITEM SEPARATOR + MENUITEM "LQ2x", ID_OPTIONS_FILTER_LQ2X + MENUITEM "HQ2x", ID_OPTIONS_FILTER_HQ2X + MENUITEM "HQ2xS", ID_OPTIONS_FILTER_HQ2X2 + END + POPUP "&3x Magnification" + BEGIN + MENUITEM "Simple 3&x", ID_OPTIONS_FILTER16BIT_SIMPLE3X + MENUITEM "&Pixelate 3x", ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL3X + MENUITEM SEPARATOR + MENUITEM "HQ3x", ID_OPTIONS_FILTER_HQ3X + MENUITEM "HQ3xS", ID_OPTIONS_FILTER_HQ3X2 + END + POPUP "&4x Magnification" + BEGIN + MENUITEM "Simple 4&x", ID_OPTIONS_FILTER16BIT_SIMPLE4X + MENUITEM "&Pixelate 4x", ID_OPTIONS_FILTER16BIT_PIXELATEEXPERIMENTAL4X + END + MENUITEM SEPARATOR + MENUITEM "&Disable MMX Optimizations", ID_OPTIONS_FILTER_DISABLEMMX + END + POPUP "&Audio" + BEGIN + MENUITEM "&Disable", ID_OPTIONS_SOUND_DISABLE + MENUITEM SEPARATOR + MENUITEM "&Mute All Channels", ID_OPTIONS_SOUND_MUTE + MENUITEM "Mute Frame &Advance", ID_OPTIONS_SOUND_MUTEFRAMEADVANCE + MENUITEM "Mute When &Inactive", ID_OPTIONS_SOUND_MUTEWHENINACTIVE + MENUITEM SEPARATOR + MENUITEM "&Use Old Synchronization", ID_OPTIONS_SOUND_USEOLDSYNCHRONIZATION + MENUITEM "&Reverse Stereo", ID_OPTIONS_SOUND_REVERSESTEREO + MENUITEM SEPARATOR + MENUITEM "&Echo", ID_OPTIONS_SOUND_ECHO + MENUITEM "&Low Pass Filter", ID_OPTIONS_SOUND_LOWPASSFILTER + MENUITEM SEPARATOR + POPUP "&Volume" + BEGIN + MENUITEM "&.25x", ID_OPTIONS_SOUND_VOLUME_25X + MENUITEM ".&5x", ID_OPTIONS_SOUND_VOLUME_5X + MENUITEM "&1x", ID_OPTIONS_SOUND_VOLUME_1X + MENUITEM "&2x", ID_OPTIONS_SOUND_VOLUME_2X + MENUITEM "&3x", ID_OPTIONS_SOUND_VOLUME_3X + MENUITEM "&4x", ID_OPTIONS_SOUND_VOLUME_4X + END + POPUP "&Channels" + BEGIN + MENUITEM "&Enable All Channels", ID_OPTIONS_SOUND_ON, CHECKED + MENUITEM "&Disable All Channels", ID_OPTIONS_SOUND_OFF + MENUITEM SEPARATOR + MENUITEM "Channel &1", ID_OPTIONS_SOUND_CHANNEL1, CHECKED + MENUITEM "Channel &2", ID_OPTIONS_SOUND_CHANNEL2, CHECKED + MENUITEM "Channel &3", ID_OPTIONS_SOUND_CHANNEL3, CHECKED + MENUITEM "Channel &4", ID_OPTIONS_SOUND_CHANNEL4, CHECKED + MENUITEM "Direct Sound &A", ID_OPTIONS_SOUND_DIRECTSOUNDA, CHECKED + MENUITEM "Direct Sound &B", ID_OPTIONS_SOUND_DIRECTSOUNDB, CHECKED + END + MENUITEM SEPARATOR + MENUITEM "&11 Khz", ID_OPTIONS_SOUND_11KHZ + MENUITEM "&22 Khz", ID_OPTIONS_SOUND_22KHZ + MENUITEM "&44 Khz", ID_OPTIONS_SOUND_44KHZ + END + MENUITEM SEPARATOR + POPUP "&Emulation" + BEGIN + MENUITEM "&Real Time Clock", ID_OPTIONS_EMULATOR_REALTIMECLOCK + POPUP "Save &Type" + BEGIN + MENUITEM "&Automatic", ID_OPTIONS_EMULATOR_SAVETYPE_AUTOMATIC + MENUITEM "&EEPROM", ID_OPTIONS_EMULATOR_SAVETYPE_EEPROM + MENUITEM "&SRAM", ID_OPTIONS_EMULATOR_SAVETYPE_SRAM + MENUITEM "&Flash", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH + MENUITEM "EE&PROM+Sensor", ID_OPTIONS_EMULATOR_SAVETYPE_EEPROMSENSOR + MENUITEM "&None", ID_OPTIONS_EMULATOR_SAVETYPE_NONE + MENUITEM SEPARATOR + MENUITEM "Flash &64K", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH512K + MENUITEM "Flash &128K", ID_OPTIONS_EMULATOR_SAVETYPE_FLASH1M + MENUITEM SEPARATOR + MENUITEM "Enhanced &Detection", ID_OPTIONS_EMULATOR_SAVETYPE_ENHANCEDDETECTION + END + MENUITEM SEPARATOR + MENUITEM "GBA &Lag Reduction", ID_EMULATOR_GBALAG + MENUITEM "Use &Old Frame Timing", ID_EMULATOR_USE_OLD_FRAME_TIMING + MENUITEM " Use &GB Null Input Kludge", ID_EMULATOR_USE_GB_INPUT_KLUDGE + MENUITEM SEPARATOR + MENUITEM "&Use BIOS File", ID_OPTIONS_EMULATOR_USEBIOSFILE + MENUITEM "Skip BIOS &Intro", ID_OPTIONS_EMULATOR_SKIPBIOS + MENUITEM "Select &BIOS File...", ID_OPTIONS_EMULATOR_SELECTBIOSFILE + MENUITEM SEPARATOR + MENUITEM "Automatic IPS &Patching", ID_OPTIONS_EMULATOR_AUTOMATICALLYIPSPATCH + END + POPUP "Gameboy &Mode" + BEGIN + MENUITEM "Automatic &Detection", ID_OPTIONS_GAMEBOY_AUTOMATIC + MENUITEM "Game Boy &Advance (GBA/AGB)", ID_OPTIONS_GAMEBOY_GBA + MENUITEM "Game Boy &Color (GBC/CGB)", ID_OPTIONS_GAMEBOY_CGB + MENUITEM "Game Boy Color + SGB b&orders", ID_OPTIONS_GAMEBOY_SGB2 + MENUITEM "&Super Game Boy (SGB)", ID_OPTIONS_GAMEBOY_SGB + MENUITEM "Game &Boy (GB)", ID_OPTIONS_GAMEBOY_GB + MENUITEM SEPARATOR + MENUITEM "Border A&utomatic", ID_OPTIONS_GAMEBOY_BORDERAUTOMATIC + MENUITEM "Border Al&ways", ID_OPTIONS_GAMEBOY_BORDER + MENUITEM SEPARATOR + MENUITEM "GB &Printer", ID_OPTIONS_GAMEBOY_PRINTER + MENUITEM SEPARATOR + MENUITEM "&Real Colors", ID_OPTIONS_GAMEBOY_REALCOLORS + MENUITEM "Ga&meboy Colors", ID_OPTIONS_GAMEBOY_GAMEBOYCOLORS + MENUITEM "Customi&ze Colors...", ID_OPTIONS_GAMEBOY_COLORS + END + MENUITEM SEPARATOR + POPUP "&Head-Up Display" + BEGIN + MENUITEM "&Frame Counter", ID_TOOLS_FRAMECOUNTER + MENUITEM " &Lag Counter", ID_TOOLS_LAGCOUNTER + MENUITEM " &Extra Counter", ID_TOOLS_EXTRACOUNTER + MENUITEM " Extra Counter &Reset", ID_TOOLS_EXTRACOUNTERRESET + MENUITEM "&Input Display", ID_TOOLS_INPUTDISPLAY + MENUITEM SEPARATOR + MENUITEM "&Text Display Options...", ID_OPTIONS_VIDEO_TEXTDISPLAYOPTIONS + MENUITEM "Disable Status &Messages", ID_OPTIONS_VIDEO_DISABLESTATUSMESSAGES + MENUITEM SEPARATOR + POPUP "Show &Speed" + BEGIN + MENUITEM "None", ID_OPTIONS_EMULATOR_SHOWSPEED_NONE + MENUITEM "Percentage", ID_OPTIONS_EMULATOR_SHOWSPEED_PERCENTAGE + MENUITEM "Detailed", ID_OPTIONS_EMULATOR_SHOWSPEED_DETAILED + MENUITEM SEPARATOR + MENUITEM "Transparent", ID_OPTIONS_EMULATOR_SHOWSPEED_TRANSPARENT + END + END + POPUP "&Preferences" + BEGIN + MENUITEM "Always On &Top", ID_OPTIONS_EMULATOR_ALWAYSONTOP + MENUITEM "Enable &Background Input", ID_OPTIONS_EMULATOR_BACKGROUNDINPUT + MENUITEM "Pause When &Inactive", ID_OPTIONS_EMULATOR_PAUSEWHENINACTIVE + POPUP "Process &Priority" + BEGIN + MENUITEM "&Highest", ID_OPTIONS_PRIORITY_HIGHEST + MENUITEM "&Above Normal", ID_OPTIONS_PRIORITY_ABOVENORMAL + MENUITEM "&Normal", ID_OPTIONS_PRIORITY_NORMAL + MENUITEM "&Below Normal", ID_OPTIONS_PRIORITY_BELOWNORMAL + END + MENUITEM SEPARATOR + POPUP "Relevant File &Naming" + BEGIN + MENUITEM "Prefer On-Disk &Archive Filename", ID_OPTIONS_PREFER_ARCHIVE_NAME + MENUITEM "Prefer Logical &ROM Filename", ID_OPTIONS_PREFER_ROM_NAME + END + MENUITEM "&Directories...", ID_OPTIONS_EMULATOR_DIRECTORIES + MENUITEM "&Associate...", ID_OPTIONS_EMULATOR_ASSOCIATE + MENUITEM SEPARATOR + POPUP "UI &Language" + BEGIN + MENUITEM "&System", ID_OPTIONS_LANGUAGE_SYSTEM + MENUITEM "&English", ID_OPTIONS_LANGUAGE_ENGLISH + MENUITEM "&Other...", ID_OPTIONS_LANGUAGE_OTHER + END + END + END + POPUP "&Cheats" + BEGIN + MENUITEM "&RAM Search...", ID_RAM_SEARCH + MENUITEM "RAM &Watch...", ID_RAM_WATCH + MENUITEM SEPARATOR + MENUITEM "Old &Cheat Search...", ID_CHEATS_SEARCHFORCHEATS + MENUITEM "&Pause During Cheat Search", ID_CHEATS_PAUSEDURINGCHEATSEARCH + MENUITEM SEPARATOR + MENUITEM "&View Cheat List...", ID_CHEATS_CHEATLIST + MENUITEM "&Load Cheat List...", ID_CHEATS_LOADCHEATLIST + MENUITEM "&Save Cheat List...", ID_CHEATS_SAVECHEATLIST + MENUITEM "&Auto Save/Load Cheat List", ID_CHEATS_AUTOMATICSAVELOADCHEATS + MENUITEM SEPARATOR + MENUITEM "Import &Gameshark Code File...", ID_FILE_IMPORT_GAMESHARKCODEFILE + MENUITEM SEPARATOR + MENUITEM "&Disable All Cheats", ID_CHEATS_DISABLECHEATS + END + POPUP "&Tools" + BEGIN + POPUP "&Debug" + BEGIN + MENUITEM "&Disassemble...", ID_TOOLS_DISASSEMBLE + MENUITEM "&Logging...", ID_TOOLS_LOGGING + MENUITEM "&IO Viewer...", ID_TOOLS_IOVIEWER + MENUITEM "&Map Viewer...", ID_TOOLS_MAPVIEW + MENUITEM "Memory &Viewer...", ID_TOOLS_MEMORYVIEWER + MENUITEM "&OAM Viewer...", ID_TOOLS_OAMVIEWER + MENUITEM "&Palette Viewer...", ID_TOOLS_PALETTEVIEW + MENUITEM "&Tile Viewer...", ID_TOOLS_TILEVIEWER + MENUITEM SEPARATOR + MENUITEM "&AGB Print", ID_OPTIONS_EMULATOR_AGBPRINT + POPUP "&GDB" + BEGIN + MENUITEM "&Wait For Connection...", ID_TOOLS_DEBUG_GDB + MENUITEM "&Load And Wait...", ID_TOOLS_DEBUG_LOADANDWAIT + MENUITEM "&Break Into GDB", ID_TOOLS_DEBUG_BREAK + MENUITEM "&Disconnect", ID_TOOLS_DEBUG_DISCONNECT + END + END + MENUITEM SEPARATOR + MENUITEM "Frame &Search", ID_DEBUG_FRAMESEARCH + MENUITEM "Frame Search Pre&v", ID_DEBUG_FRAMESEARCHPREV + MENUITEM "Frame Search &End", ID_DEBUG_FRAMESEARCHLOAD + MENUITEM SEPARATOR + POPUP "&Lua Scripting" + BEGIN + MENUITEM "&New Lua Script Window...", ID_FILE_LUA_OPEN + MENUITEM "Close &All Script Windows", ID_FILE_LUA_CLOSE_ALL + END + MENUITEM SEPARATOR + POPUP "&Movie" + BEGIN + MENUITEM "&Play Movie...", ID_MOVIE_PLAY + MENUITEM "&Watch From Beginning", ID_MOVIE_RESTART_PLAY + MENUITEM SEPARATOR + MENUITEM "Record &New Movie...", ID_MOVIE_RECORD + MENUITEM "&Resume/Exit Recording", ID_MOVIE_RESUME_RECORD + MENUITEM SEPARATOR + MENUITEM "Read-&Only Mode", ID_MOVIE_READONLY + POPUP "When Movie &Ends" + BEGIN + MENUITEM "&Pause Emulator", ID_MOVIE_END_PAUSE + MENUITEM SEPARATOR + MENUITEM "&Close Movie", ID_MOVIE_END_STOP + MENUITEM "&Watch From Beginning", ID_MOVIE_END_RESTART + MENUITEM "&Append If Modified", ID_MOVIE_END_APPEND + MENUITEM "&Leave Open", ID_MOVIE_END_KEEP + END + MENUITEM SEPARATOR + MENUITEM "&Stop Movie", ID_MOVIE_STOP + MENUITEM SEPARATOR + POPUP "Advanced &Tools" + BEGIN + MENUITEM "&Convert Current To VBM 1.1", ID_MOVIE_TOOL_CONVERT + MENUITEM SEPARATOR + MENUITEM "&Auto Convert Opened Movie", ID_MOVIE_TOOL_AUTO_CONVERT + MENUITEM SEPARATOR + MENUITEM "Fix &Header", ID_MOVIE_TOOL_FIX_HEADER + MENUITEM SEPARATOR + MENUITEM "&Truncate At Current Frame", ID_MOVIE_TOOL_TRUNCATE + END + MENUITEM "&Associate With Savestates", ID_MOVIE_ASSC_WITH_SAVESTATE + END + MENUITEM "Pause/Resume AVI &Recording", ID_TOOLS_PAUSEAVIRECORDING + MENUITEM "Start/Stop &AVI Recording...", ID_TOOLS_AVIRECORDING + MENUITEM "Start/Stop &WAV Recording...", ID_TOOLS_SOUNDRECORDING + MENUITEM SEPARATOR + MENUITEM "Screenshot &Capture...", ID_FILE_SCREENCAPTURE + POPUP "Screenshot &Format" + BEGIN + MENUITEM "&PNG", ID_OPTIONS_EMULATOR_PNGFORMAT + MENUITEM "&BMP", ID_OPTIONS_EMULATOR_BMPFORMAT + END + END + POPUP "&Help" + BEGIN + MENUITEM "&FAQ (Website)...", ID_HELP_FAQ + MENUITEM "&Bug Report...", ID_HELP_BUGREPORT + MENUITEM SEPARATOR + MENUITEM "&About...", ID_HELP_ABOUT + END +END + +RAMWATCH_MENU MENU +BEGIN + POPUP "&File" + BEGIN + MENUITEM "&New List\tCtrl+N", RAMMENU_FILE_NEW + MENUITEM "&Open...\tCtrl+O", RAMMENU_FILE_OPEN + MENUITEM "&Save\tCtrl+S", RAMMENU_FILE_SAVE + MENUITEM "Sa&ve As...\tCtrl+Shift+S", RAMMENU_FILE_SAVEAS + MENUITEM "&Append File...", RAMMENU_FILE_APPEND + MENUITEM "&Recent", RAMMENU_FILE_RECENT + MENUITEM SEPARATOR + MENUITEM "Auto-&Load", RAMMENU_FILE_AUTOLOAD + MENUITEM "Save &Window Position", RAMMENU_FILE_SAVEWINDOW + MENUITEM SEPARATOR + MENUITEM "&Close\tAlt+F4", IDCANCEL + END + POPUP "&Watches" + BEGIN + MENUITEM "&New Watch\tN", IDC_C_WATCH + MENUITEM "&Edit Watch\tE", IDC_C_WATCH_EDIT + MENUITEM "&Remove Watch\tR", IDC_C_WATCH_REMOVE + MENUITEM "Duplic&ate Watch\tA", IDC_C_WATCH_DUPLICATE + MENUITEM SEPARATOR + MENUITEM "Move &Up\tU", IDC_C_WATCH_UP + MENUITEM "Move &Down\tD", IDC_C_WATCH_DOWN + END +END + +LUAWINDOW_MENU MENU +BEGIN + POPUP "Console" + BEGIN + MENUITEM "&Font...", IDC_LUACONSOLE_CHOOSEFONT + MENUITEM "Clear", IDC_LUACONSOLE_CLEAR + END +END + + +///////////////////////////////////////////////////////////////////////////// +// +// Accelerator +// + +IDR_ACCELERATOR ACCELERATORS +BEGIN + "C", ID_CHEATS_SEARCHFORCHEATS, VIRTKEY, CONTROL, NOINVERT + VK_OEM_6, ID_DEBUG_FRAMESEARCH, VIRTKEY, NOINVERT + "L", ID_DEBUG_FRAMESEARCHLOAD, VIRTKEY, NOINVERT + VK_OEM_4, ID_DEBUG_FRAMESEARCHPREV, VIRTKEY, NOINVERT + "N", ID_DEBUG_NEXTFRAME, VIRTKEY, CONTROL, NOINVERT + VK_OEM_5, ID_DEBUG_NEXTFRAME, VIRTKEY, NOINVERT + "X", ID_FILE_EXIT, VIRTKEY, CONTROL, NOINVERT + "L", ID_FILE_LOAD, VIRTKEY, SHIFT, CONTROL, NOINVERT + "L", ID_FILE_LOADGAME_CURRENT, VIRTKEY, CONTROL, NOINVERT + VK_F1, ID_FILE_LOADGAME_SLOT1, VIRTKEY, NOINVERT + VK_F10, ID_FILE_LOADGAME_SLOT10, VIRTKEY, NOINVERT + VK_F2, ID_FILE_LOADGAME_SLOT2, VIRTKEY, NOINVERT + VK_F3, ID_FILE_LOADGAME_SLOT3, VIRTKEY, NOINVERT + VK_F4, ID_FILE_LOADGAME_SLOT4, VIRTKEY, NOINVERT + VK_F5, ID_FILE_LOADGAME_SLOT5, VIRTKEY, NOINVERT + VK_F6, ID_FILE_LOADGAME_SLOT6, VIRTKEY, NOINVERT + VK_F7, ID_FILE_LOADGAME_SLOT7, VIRTKEY, NOINVERT + VK_F8, ID_FILE_LOADGAME_SLOT8, VIRTKEY, NOINVERT + VK_F9, ID_FILE_LOADGAME_SLOT9, VIRTKEY, NOINVERT + VK_F1, ID_FILE_MRU_FILE1, VIRTKEY, CONTROL, NOINVERT + VK_F10, ID_FILE_MRU_FILE10, VIRTKEY, CONTROL, NOINVERT + VK_F2, ID_FILE_MRU_FILE2, VIRTKEY, CONTROL, NOINVERT + VK_F3, ID_FILE_MRU_FILE3, VIRTKEY, CONTROL, NOINVERT + VK_F4, ID_FILE_MRU_FILE4, VIRTKEY, CONTROL, NOINVERT + VK_F5, ID_FILE_MRU_FILE5, VIRTKEY, CONTROL, NOINVERT + VK_F6, ID_FILE_MRU_FILE6, VIRTKEY, CONTROL, NOINVERT + VK_F7, ID_FILE_MRU_FILE7, VIRTKEY, CONTROL, NOINVERT + VK_F8, ID_FILE_MRU_FILE8, VIRTKEY, CONTROL, NOINVERT + VK_F9, ID_FILE_MRU_FILE9, VIRTKEY, CONTROL, NOINVERT + "O", ID_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT + "G", ID_FILE_OPENGAMEBOY, VIRTKEY, CONTROL, NOINVERT + "P", ID_FILE_PAUSE, VIRTKEY, CONTROL, NOINVERT + VK_PAUSE, ID_FILE_PAUSE, VIRTKEY, NOINVERT + "R", ID_FILE_RESET, VIRTKEY, CONTROL, NOINVERT + "S", ID_FILE_SAVE, VIRTKEY, SHIFT, CONTROL, NOINVERT + "S", ID_FILE_SAVEGAME_CURRENT, VIRTKEY, CONTROL, NOINVERT + VK_F1, ID_FILE_SAVEGAME_SLOT1, VIRTKEY, SHIFT, NOINVERT + VK_F10, ID_FILE_SAVEGAME_SLOT10, VIRTKEY, SHIFT, NOINVERT + VK_F2, ID_FILE_SAVEGAME_SLOT2, VIRTKEY, SHIFT, NOINVERT + VK_F3, ID_FILE_SAVEGAME_SLOT3, VIRTKEY, SHIFT, NOINVERT + VK_F4, ID_FILE_SAVEGAME_SLOT4, VIRTKEY, SHIFT, NOINVERT + VK_F5, ID_FILE_SAVEGAME_SLOT5, VIRTKEY, SHIFT, NOINVERT + VK_F6, ID_FILE_SAVEGAME_SLOT6, VIRTKEY, SHIFT, NOINVERT + VK_F7, ID_FILE_SAVEGAME_SLOT7, VIRTKEY, SHIFT, NOINVERT + VK_F8, ID_FILE_SAVEGAME_SLOT8, VIRTKEY, SHIFT, NOINVERT + VK_F9, ID_FILE_SAVEGAME_SLOT9, VIRTKEY, SHIFT, NOINVERT + VK_ESCAPE, ID_FILE_TOGGLEMENU, VIRTKEY, NOINVERT + "8", ID_MOVIE_READONLY, VIRTKEY, SHIFT, NOINVERT + VK_OEM_MINUS, ID_OPTIONS_FRAMESKIP_THROTTLE_DECREASE, VIRTKEY, NOINVERT + VK_OEM_PLUS, ID_OPTIONS_FRAMESKIP_THROTTLE_INCREASE, VIRTKEY, NOINVERT + "1", ID_OPTIONS_JOYPAD_AUTOFIRE_A, VIRTKEY, ALT, NOINVERT + "2", ID_OPTIONS_JOYPAD_AUTOFIRE_B, VIRTKEY, ALT, NOINVERT + VK_OEM_3, ID_OPTIONS_JOYPAD_AUTOFIRE_CLEAR, VIRTKEY, ALT, NOINVERT + "8", ID_OPTIONS_JOYPAD_AUTOFIRE_DOWN, VIRTKEY, ALT, NOINVERT + "3", ID_OPTIONS_JOYPAD_AUTOFIRE_L, VIRTKEY, ALT, NOINVERT + "9", ID_OPTIONS_JOYPAD_AUTOFIRE_LEFT, VIRTKEY, ALT, NOINVERT + "4", ID_OPTIONS_JOYPAD_AUTOFIRE_R, VIRTKEY, ALT, NOINVERT + "0", ID_OPTIONS_JOYPAD_AUTOFIRE_RIGHT, VIRTKEY, ALT, NOINVERT + "6", ID_OPTIONS_JOYPAD_AUTOFIRE_SELECT, VIRTKEY, ALT, NOINVERT + "5", ID_OPTIONS_JOYPAD_AUTOFIRE_START, VIRTKEY, ALT, NOINVERT + "7", ID_OPTIONS_JOYPAD_AUTOFIRE_UP, VIRTKEY, ALT, NOINVERT + "1", ID_OPTIONS_VIDEO_LAYERS_BG0, VIRTKEY, CONTROL, NOINVERT + "2", ID_OPTIONS_VIDEO_LAYERS_BG1, VIRTKEY, CONTROL, NOINVERT + "3", ID_OPTIONS_VIDEO_LAYERS_BG2, VIRTKEY, CONTROL, NOINVERT + "4", ID_OPTIONS_VIDEO_LAYERS_BG3, VIRTKEY, CONTROL, NOINVERT + "5", ID_OPTIONS_VIDEO_LAYERS_OBJ, VIRTKEY, CONTROL, NOINVERT + "8", ID_OPTIONS_VIDEO_LAYERS_OBJWIN, VIRTKEY, CONTROL, NOINVERT + "6", ID_OPTIONS_VIDEO_LAYERS_WIN0, VIRTKEY, CONTROL, NOINVERT + "7", ID_OPTIONS_VIDEO_LAYERS_WIN1, VIRTKEY, CONTROL, NOINVERT + "1", ID_SELECT_SLOT1, VIRTKEY, NOINVERT + "0", ID_SELECT_SLOT10, VIRTKEY, NOINVERT + "2", ID_SELECT_SLOT2, VIRTKEY, NOINVERT + "3", ID_SELECT_SLOT3, VIRTKEY, NOINVERT + "4", ID_SELECT_SLOT4, VIRTKEY, NOINVERT + "5", ID_SELECT_SLOT5, VIRTKEY, NOINVERT + "6", ID_SELECT_SLOT6, VIRTKEY, NOINVERT + "7", ID_SELECT_SLOT7, VIRTKEY, NOINVERT + "8", ID_SELECT_SLOT8, VIRTKEY, NOINVERT + "9", ID_SELECT_SLOT9, VIRTKEY, NOINVERT + VK_OEM_PERIOD, ID_TOOLS_FRAMECOUNTER, VIRTKEY, NOINVERT + VK_OEM_COMMA, ID_TOOLS_INPUTDISPLAY, VIRTKEY, NOINVERT + "B", ID_TOOLS_REWIND, VIRTKEY, CONTROL, NOINVERT + "R", ID_MOVIE_RESTART_PLAY, VIRTKEY, SHIFT, NOINVERT +END + +IDR_ACCELERATOR1 ACCELERATORS +BEGIN + "N", IDC_C_WATCH, VIRTKEY, NOINVERT + "D", IDC_C_WATCH_DOWN, VIRTKEY, NOINVERT + "A", IDC_C_WATCH_DUPLICATE, VIRTKEY, NOINVERT + "E", IDC_C_WATCH_EDIT, VIRTKEY, NOINVERT + "R", IDC_C_WATCH_REMOVE, VIRTKEY, NOINVERT + "U", IDC_C_WATCH_UP, VIRTKEY, NOINVERT + "N", RAMMENU_FILE_NEW, VIRTKEY, CONTROL, NOINVERT + "O", RAMMENU_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT + "S", RAMMENU_FILE_SAVE, VIRTKEY, CONTROL, NOINVERT + "S", RAMMENU_FILE_SAVEAS, VIRTKEY, SHIFT, CONTROL, NOINVERT +END + + +///////////////////////////////////////////////////////////////////////////// +// +// RT_MANIFEST +// + +1 RT_MANIFEST "VisualBoyAdvance.exe.manifest" + +///////////////////////////////////////////////////////////////////////////// +// +// String Table +// + +STRINGTABLE +BEGIN + IDS_STARTAVIRECORDING "Start &AVI Recording..." + IDS_STOPAVIRECORDING "Stop &AVI Recording" + IDS_PAUSEAVIRECORDING "Pause AVI &Recording" + IDS_RESUMEAVIRECORDING "Resume AVI &Recording" +END + +STRINGTABLE +BEGIN + IDS_STARTSOUNDRECORDING "Start &WAV Recording..." + IDS_STOPSOUNDRECORDING "Stop &WAV Recording" +END + +STRINGTABLE +BEGIN + IDS_UNSUPPORTED_VBA_SGM "Unsupported VisualBoyAdvance save game version %d" + IDS_CANNOT_LOAD_SGM "Cannot load save game for %s" + IDS_SAVE_GAME_NOT_USING_BIOS "Save game is not using the BIOS file" + IDS_SAVE_GAME_USING_BIOS "Save game is using the BIOS file" + IDS_UNSUPPORTED_SAVE_TYPE "Unsupported save type %d" + IDS_CANNOT_OPEN_FILE "Cannot open file %s" + IDS_BAD_ZIP_FILE "Bad ZIP file %s" + IDS_NO_IMAGE_ON_ZIP "No image found on ZIP file %s" + IDS_ERROR_OPENING_IMAGE "Error opening image %s" + IDS_ERROR_READING_IMAGE "Error reading image %s" + IDS_UNSUPPORTED_BIOS_FUNCTION + "Unsupported BIOS function %02x called from %08x. A BIOS file is needed in order to get correct behaviour." + IDS_INVALID_BIOS_FILE_SIZE "Invalid BIOS file size" + IDS_INVALID_CHEAT_CODE "Invalid cheat code '%s'. Supported formats are:\nXXXXXXXX:YY, XXXXXXXX:YYYY, XXXXXXXX:YYYYYYYY." + IDS_UNKNOWN_ARM_OPCDOE "Unimplemented opcode %08x from %08x" + IDS_UNKNOWN_THUMB_OPCODE "Unknown opcode %04x from %08x" +END + +STRINGTABLE +BEGIN + IDS_ERROR_CREATING_FILE "Error creating file %s" + IDS_FAILED_TO_READ_SGM "Failed to read complete save game %s (%d)" + IDS_FAILED_TO_READ_RTC "Failed to read RTC from save game %s (continuing)" + IDS_UNSUPPORTED_VB_SGM "Unsupported VisualBoy save game version %d" + IDS_CANNOT_LOAD_SGM_FOR "Cannot load save game for %s. Playing %s" + IDS_ERROR_OPENING_IMAGE_FROM "Error opening image %s from zip file %s" + IDS_ERROR_READING_IMAGE_FROM "Error reading image %s from zip file %s" + IDS_UNSUPPORTED_ROM_SIZE "Unsupported rom size %02x" + IDS_UNSUPPORTED_RAM_SIZE "Unsupported ram size %02x" + IDS_UNKNOWN_CARTRIDGE_TYPE "Unknown cartridge type %02x" + IDS_MAXIMUM_NUMBER_OF_CHEATS "Maximum number of cheats reached." + IDS_INVALID_GAMESHARK_CODE "Invalid GameShark code: %s" + IDS_INVALID_GAMEGENIE_CODE "Invalid GameGenie code: %s" + IDS_INVALID_CHEAT_TO_REMOVE "Invalid cheat to remove %d" + IDS_INVALID_CHEAT_CODE_ADDRESS "Invalid cheat code address: %08x" + IDS_UNSUPPORTED_CHEAT_LIST_VERSION "Unsupported cheat list version %d" +END + +STRINGTABLE +BEGIN + IDS_DIRECTX_7_REQUIRED "DirectX 7.0 or greater is required to run.\nDownload at http://www.microsoft.com/directx.\n\nError found at: %s" + IDS_DISABLING_VIDEO_MEMORY "Disabling Use Video Memory setting" + IDS_SETTING_WILL_BE_EFFECTIVE + "Setting will be effective the next time you start the emulator" + IDS_DISABLING_EMULATION_ONLY "Disabling Emulation Only setting" + IDS_FAILED_TO_OPEN_FILE "Failed to open file %s" + IDS_FAILED_TO_READ_ZIP_DIR "Failed to read zip directory for file %s" + IDS_UNSUPPORTED_FILE_TYPE "The file ""%s"" is an unsupported type." + IDS_CANNOT_CREATE_DIRECTSOUND "Cannot create DirectSound %08x" + IDS_CANNOT_SETCOOPERATIVELEVEL "Cannot SetCooperativeLevel %08x" + IDS_CANNOT_CREATESOUNDBUFFER "Cannot CreateSoundBuffer %08x" + IDS_CANNOT_SETFORMAT_PRIMARY "Cannot SetFormat for primary %08x" + IDS_CANNOT_CREATESOUNDBUFFER_SEC "Cannot CreateSoundBuffer secondary %08x" + IDS_CANNOT_PLAY_PRIMARY "Cannot Play primary %08x" + IDS_SEARCH_PRODUCED_TOO_MANY + "Search produced %d results.\nThey have been remembered, but are too many to display.\nPlease refine it better by performing additional searches." + IDS_NUMBER_CANNOT_BE_EMPTY "Number cannot be empty" + IDS_INVALID_ADDRESS "Invalid address: %08x" +END + +STRINGTABLE +BEGIN + IDS_MISALIGNED_HALFWORD "Misaligned half-word address: %08x" + IDS_MISALIGNED_WORD "Misaligned word address: %08x" + IDS_VALUE_CANNOT_BE_EMPTY "Value cannot be empty" + IDS_ERROR_ON_STARTDOC "Error on StartDoc" + IDS_ERROR_ON_STARTPAGE "Error on StartPage" + IDS_ERROR_PRINTING_ON_STRETCH "Error printing on StretchDIBits" + IDS_ERROR_ON_ENDPAGE "Error on EndPage" + IDS_ERROR_ON_ENDDOC "Error on EndDoc" + IDS_ERROR "Error" + IDS_JOY_LEFT "Joy %d Left" + IDS_JOY_RIGHT "Joy %d Right" + IDS_JOY_UP "Joy %d Up" + IDS_JOY_DOWN "Joy %d Down" + IDS_JOY_BUTTON "Joy %d %s" +END + +STRINGTABLE +BEGIN + IDS_SELECT_BIOS_FILE "Select BIOS file" + IDS_RESET "Reset" + IDS_AUTOFIRE_A_DISABLED "turbo A off" + IDS_AUTOFIRE_A "turbo A on" + IDS_AUTOFIRE_B_DISABLED "turbo B off" + IDS_AUTOFIRE_B "turbo B on" + IDS_AUTOFIRE_L_DISABLED "turbo L off" + IDS_AUTOFIRE_L "turbo L on" + IDS_AUTOFIRE_R_DISABLED "turbo R off" + IDS_AUTOFIRE_R "turbo R on" + IDS_SELECT_ROM "Select ROM" + IDS_SELECT_SAVE_GAME_NAME "Select save game name" + IDS_LOADED_STATE "Loaded state" + IDS_LOADED_STATE_N "Loaded %d" +END + +STRINGTABLE +BEGIN + IDS_WROTE_STATE "Saved state" + IDS_WROTE_STATE_N "Saved %d" + IDS_LOADED_BATTERY "Loaded battery" + IDS_SELECT_CAPTURE_NAME "Select screen capture name" + IDS_SCREEN_CAPTURE "Screen capture" + IDS_ADDRESS "Address" + IDS_OLD_VALUE "Old Value" + IDS_NEW_VALUE "New Value" + IDS_ADD_CHEAT_CODE "Add cheat code" + IDS_CODE "Code" + IDS_DESCRIPTION "Description" + IDS_STATUS "Status" + IDS_ADD_GG_CODE "Add GameGenie code" + IDS_ADD_GS_CODE "Add GameShark code" + IDS_POCKET_PRINTER "Pocket Printer" + IDS_UNKNOWN "Unknown" +END + +STRINGTABLE +BEGIN + IDS_NONE "None" + IDS_FAILED_TO_LOAD_LIBRARY "Failed to load library %s" + IDS_FAILED_TO_GET_LOCINFO "Failed to get locale information" + IDS_SELECT_CHEAT_LIST_NAME "Select cheat list name" + IDS_FILTER_BIOS "Gameboy Advance_*.bin;*.agb;*.gba;*.bios;*.zip;*.z;*.gz;*.tar_All Files_*.*__" + IDS_FILTER_ROM "All Gameboy / Gameboy Advance_*.bin;*.agb;*.gba;*.mb;*.elf;*.gb;*.sgb;*.cgb;*.gbc;*.zip;*.z;*.gz;*.7z;*.rar;*.lzma;*.bz2;*.bzip2;*.tar_All GB / GBA (uncompressed only)_*.bin;*.agb;*.gba;*.mb;*.elf;*.gb;*.sgb;*.cgb;*.gbc_Gameboy Advance (uncompressed only)_*.bin;*.agb;*.gba_Gameboy (uncompressed only)_*.gb;*.sgb;*.cgb;*.gbc_Archive Files_*.zip;*.z;*.gz;*.7z;*.rar;*.lzma;*.bz2;*.bzip2;*.tar_All Files_*.*__" + IDS_FILTER_SGM "VisualBoyAdvance Save Game_*.sgm_All Files_*.*__" + IDS_FILTER_CHEAT_LIST "VisualBoyAdvance Cheat List_*.clt_All Files_*.*__" + IDS_FILTER_PNG "PNG Image_*.png_BMP Image_*.bmp_All Files_*.*__" + IDS_LOADED_CHEATS "Loaded cheats" + IDS_ERROR_DISP_COLOR "Unsupported display setting for color depth: %d bits. \nWindows desktop must be in either 16-bit, 24-bit or 32-bit mode for this program to work in window mode." + IDS_ADD_GSA_CODE "Add GamesharkAdvance code" + IDS_FILTER_SPS "Gameshark Snapshot_*.sps_All Files_*.*__" + IDS_SELECT_SNAPSHOT_FILE "Select snapshot file" + IDS_FILTER_SAV "Battery file_*.sav_Flash save_*.dat_All Files_*.*__" + IDS_SELECT_BATTERY_FILE "Select battery file" +END + +STRINGTABLE +BEGIN + IDS_UNSUPPORTED_CHEAT_LIST_TYPE "Unsupported cheat list type %d" + IDS_INVALID_GSA_CODE "Invalid GSA code. Format is XXXXXXXXYYYYYYYY." + IDS_CANNOT_IMPORT_SNAPSHOT_FOR + "Cannot import snapshot for %s. Current game is %s" + IDS_UNSUPPORTED_SNAPSHOT_FILE "Unsupported snapshot file %s" + IDS_UNSUPPORTED_ARM_MODE "Unsupported ARM mode %02x" + IDS_UNSUPPORTED_CODE_FILE "Unsupported code file %s" + IDS_GSA_CODE_WARNING "Warning: cheats are for game %s. Current game is %s.\nCodes may not work correctly." + IDS_INVALID_CBA_CODE "Invalid CBA code. Format is XXXXXXXX YYYY." + IDS_CBA_CODE_WARNING "Warning: Codes seem to be for a different game.\nCodes may not work correctly." + IDS_OUT_OF_MEMORY "Failed to allocate memory for %s" +END + +STRINGTABLE +BEGIN + IDS_FILTER_GBS "Gameboy Snapshot_*.gbs_All Files_*.*__" + IDS_FILTER_GCF "Gameshark Code File_*.gcf_All Files_*.*__" + IDS_SELECT_CODE_FILE "Select code file" + IDS_SAVE_WILL_BE_LOST "Importing a snapshot file will erase any saved games and reset the emulator. Do you want to continue?" + IDS_CONFIRM_ACTION "Please confirm action" + IDS_CODES_WILL_BE_LOST "Importing a code file will erase any entered codes. Do you want to continue?" + IDS_FILTER_SPC "Gameshark Code File_*.spc;*.xpc_All Files_*.*__" + IDS_ADD_CBA_CODE "Add CodeBreakerAdvance code" + IDS_FILTER_WAV "Wave file_*.wav_All Files_*.*__" + IDS_SELECT_WAV_NAME "Select wave file name" + IDS_FILTER_GBROM "All Gameboy_*.gb;*.sgb;*.cgb;*.gbc;*.zip;*.z;*.gz;*.7z;*.rar;*.lzma;*.bz2;*.bzip2;*.tar_Gameboy_*.gb_Super Gameboy_*.sgb_Color Gameboy_*.cgb;*.gbc_All Files_*.*__" + IDS_FILTER_PAL "Windows Palette (*.PAL)_*.pal_PaintShop Palette (*.PAL)_*.pal_Adobe Color Table (*.ACT)_*.act_All Files_*.*__" + IDS_SELECT_PALETTE_NAME "Select palette name:" + IDS_SEARCH_PRODUCED_NO_RESULTS "Search produced no results." + IDS_ERROR_BINDING "Error binding socket. Port probably in use." + IDS_ERROR_LISTENING "Error listening on socket." +END + +STRINGTABLE +BEGIN + IDS_ERROR_CREATING_SOCKET "Error creating socket." + IDS_ACK_NOT_RECEIVED "ACK not received from GDB." + IDS_ERROR_NOT_GBA_IMAGE "Error: not a GBA image." + IDS_EEPROM_NOT_SUPPORTED "EEPROM saves cannot be exported." + IDS_FILTER_DUMP "Memory Dump_*.dmp_All Files_*.*__" + IDS_SELECT_DUMP_FILE "Select dump file name" + IDS_FILTER_AVI "AVI File_*.avi_All Files_*.*__" + IDS_SELECT_AVI_NAME "Select AVI file name" + IDS_INVALID_THROTTLE_VALUE + "Invalid throttle value. Please enter a number between 5 and 1000." + IDS_FILTER_INI "Skin INI File_*.ini_All Files_*.*__" + IDS_SELECT_SKIN_FILE "Select the skin file name" + IDS_FILTER_MOVIE "VisualBoyAdvance Movie (.vbm)_*.vbm;*.zip;*.z;*.gz;*.7z;*.rar;*.lzma;*.bz2;*.bzip2;*.tar_VisualBoyAdvance Movie (uncompressed only)_*.vbm_Archive Files_*.zip;*.z;*.gz;*.7z;*.rar;*.lzma;*.bz2;*.bzip2;*.tar_All Files_*.*__" + IDS_SELECT_MOVIE_NAME "Select movie name" + IDS_BUG_REPORT "The bug report information is now available on the Windows Clipboard. Please paste it into any bug report made by email or on forums to help solve problems more easily." + IDS_UNSUPPORTED_MOVIE_VERSION "Unsupported movie version %d." + IDS_END_OF_MOVIE "end of movie" +END + +STRINGTABLE +BEGIN + IDS_INVALID_INTERVAL_VALUE + "Invalid rewind interval value. Please enter a number between 0 and 600 seconds." + IDS_REGISTRY "VisualBoyAdvance no longer uses the registry to store its settings. Your previous settings have been exported into the file: %s" + IDS_MOVIE_PLAY "Playing a movie will load a save state which may erase your previous battery saves. Please be sure to have a saved state if you don't want to loose any previous data." + IDS_AUTOFIRE_DISABLED "autofire off" + IDS_AUTOFIRE_ALREADY_DISABLED "autofire already off" + IDS_AUTOFIRE_START_DISABLED "turbo Start off" + IDS_AUTOFIRE_START "turbo Start on" + IDS_AUTOFIRE_SELECT_DISABLED "turbo Select off" + IDS_AUTOFIRE_SELECT "turbo Select on" + IDS_AUTOFIRE_UP_DISABLED "turbo Up off" + IDS_AUTOFIRE_UP "turbo Up on" + IDS_AUTOFIRE_DOWN_DISABLED "turbo Down off" + IDS_AUTOFIRE_DOWN "turbo Down on" + IDS_AUTOFIRE_LEFT_DISABLED "turbo Left off" + IDS_AUTOFIRE_LEFT "turbo Left on" + IDS_AUTOFIRE_RIGHT_DISABLED "turbo Right off" +END + +STRINGTABLE +BEGIN + IDS_AUTOFIRE_RIGHT "turbo Right on" + IDS_RERECORDED_STATE_N "Re-record from %d" + IDS_REPLAYED_STATE_N "Rewind to %d" +END + +STRINGTABLE +BEGIN + IDS_FILTER_LUA "Lua Script (.lua)_*.lua_All Files_*.*__" + IDS_SELECT_LUA_NAME "Select Lua script file name" + IDS_SELECT_LUA_DIR "Select Lua script file directory:" + IDS_SELECT_MOVIE_DIR "Select movie directory:" + IDS_SELECT_ROM_DIR "Select GBA ROM directory:" + IDS_SELECT_GBXROM_DIR "Select GBx ROM directory:" + IDS_SELECT_BATTERY_DIR "Select Battery directory:" + IDS_SELECT_SAVE_DIR "Select Save Directory:" + IDS_SELECT_CAPTURE_DIR "Select Capture directory:" +END + +STRINGTABLE +BEGIN + IDS_SELECT_AVI_DIR "Select AVI recording directory:" + IDS_SELECT_WAV_DIR "Select Sound recording directory:" + IDS_SELECT_CHEAT_DIR "Select Cheat list directory:" + IDS_SELECT_WATCH_DIR "Select Memory watch file directory:" + IDS_SELECT_IPS_DIR "Select IPS directory:" + IDS_SELECT_PLUGIN_DIR "Select Plugin directory:" +END + +#endif // English (United States) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff -r 8ced16adf2e1 -r f9f4f1b99eed src/win32/vba.rc2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/vba.rc2 Sat Mar 03 10:31:27 2012 -0600 @@ -0,0 +1,50 @@ + +// what the hell? all this file does is cause enigmatic build errors... + +/* +#ifndef _MAC +#include "../AutoBuild.h" +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION FILEVER + PRODUCTVERSION PRODUCTVER + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x29L +#else + FILEFLAGS 0x28L +#endif + FILEOS 0x40004L + FILETYPE 0x1L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "Comments", "VisualBoyAdvance comes with NO WARRANTY. Use it at your own risk.\0" + VALUE "CompanyName", "None\0" + VALUE "FileDescription", "VisualBoyAdvance emulator\0" + VALUE "FileVersion", STRFILEVER + VALUE "InternalName", "VisualBoyAdvance\0" + VALUE "LegalCopyright", "Copyright © 2004 Forgotten and the VBA team\0" + VALUE "LegalTrademarks", "\0" + VALUE "OriginalFilename", "VisualBoyAdvance.exe\0" + VALUE "PrivateBuild", "0\0" + VALUE "ProductName", "VisualBoyAdvance emulator\0" + VALUE "ProductVersion", STRPRODUCTVER + VALUE "SpecialBuild", "0\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + +#endif // !_MAC +*/ \ No newline at end of file