diff --git a/zsnes/src/numconv.h b/zsnes/src/numconv.h new file mode 100644 index 00000000..b77f62ba --- /dev/null +++ b/zsnes/src/numconv.h @@ -0,0 +1,126 @@ +/* +Copyright (C) 1997-2005 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach ) + +http://www.zsnes.com +http://sourceforge.net/projects/zsnes + +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. +*/ + +#ifndef NUMCONV_H +#define NUMCONV_H + +#include + +//Get correct mask for particular bit +#define BIT(X) (1 << X) + +/* +Functions that the compiler should inline that will convert +uint32, uint24, uint16 to 4 byte, 3 byte, 2 byte arrays +and back. -Nach +*/ + +static unsigned char *uint32_to_bytes(unsigned int num) +{ + static unsigned char buffer[4]; + buffer[3] = (num >> 24) & 0xFF; + buffer[2] = (num >> 16) & 0xFF; + buffer[1] = (num >> 8) & 0xFF; + buffer[0] = num & 0xFF; + return(buffer); +} + +static unsigned int bytes_to_uint32(const unsigned char buffer[4]) +{ + unsigned int num = (unsigned int)buffer[0]; + num |= ((unsigned int)buffer[1]) << 8; + num |= ((unsigned int)buffer[2]) << 16; + num |= ((unsigned int)buffer[3]) << 24; + return(num); +} + +static unsigned char *uint24_to_bytes(unsigned int num) +{ + static unsigned char buffer[3]; + buffer[2] = (num >> 16) & 0xFF; + buffer[1] = (num >> 8) & 0xFF; + buffer[0] = num & 0xFF; + return(buffer); +} + +static unsigned int bytes_to_uint24(const unsigned char buffer[3]) +{ + unsigned int num = (unsigned int)buffer[0]; + num |= ((unsigned int)buffer[1]) << 8; + num |= ((unsigned int)buffer[2]) << 16; + return(num); +} + +static unsigned char *uint16_to_bytes(unsigned short num) +{ + static unsigned char buffer[2]; + buffer[1] = (num >> 8) & 0xFF; + buffer[0] = num & 0xFF; + return(buffer); +} + +static unsigned short bytes_to_uint16(const unsigned char buffer[2]) +{ + unsigned short num = (unsigned short)buffer[0]; + num |= ((unsigned short)buffer[1]) << 8; + return(num); +} + +//Functions to read 2, 3, 4 bytes and convert to uint16, uint24, uint32 +static unsigned short fread2(FILE *fp) +{ + unsigned char uint16buf[2]; + fread(uint16buf, 2, 1, fp); + return(bytes_to_uint16(uint16buf)); +} + +static unsigned int fread3(FILE *fp) +{ + unsigned char uint24buf[3]; + fread(uint24buf, 3, 1, fp); + return(bytes_to_uint24(uint24buf)); +} + +static unsigned int fread4(FILE *fp) +{ + unsigned char uint32buf[4]; + fread(uint32buf, 4, 1, fp); + return(bytes_to_uint32(uint32buf)); +} + +//Functions to write uint16, uint24, uint32 as 2, 3, 4 bytes +static void fwrite2(unsigned short var, FILE *fp) +{ + fwrite(uint16_to_bytes(var), 2, 1, fp); +} + +static void fwrite3(unsigned int var, FILE *fp) +{ + fwrite(uint24_to_bytes(var), 3, 1, fp); +} + +static void fwrite4(unsigned int var, FILE *fp) +{ + fwrite(uint32_to_bytes(var), 4, 1, fp); +} + +#endif diff --git a/zsnes/src/zstate.c b/zsnes/src/zstate.c index cbdce906..124a55b1 100755 --- a/zsnes/src/zstate.c +++ b/zsnes/src/zstate.c @@ -666,7 +666,49 @@ void ResetOffset() { Curtableaddr += (unsigned int)tableA; } + +void zst_save(FILE *fhandle, bool Thumbnail) +{ + fwrite(zst_header_cur, 1, sizeof(zst_header_cur)-1, fhandle); //-1 for null + + PrepareOffset(); + PrepareSaveState(); + unpackfunct(); + + if (SFXEnable) + { + SfxRomBuffer -= SfxCROM; + SfxLastRamAdr -= SfxRAMMem; + } + if (SA1Enable) + { + SaveSA1(); //Convert SA-1 stuff to standard, non displacement format + } + + copy_state_data(0, write_save_state_data, false); + + if (SFXEnable) + { + SfxRomBuffer += SfxCROM; + SfxLastRamAdr += SfxRAMMem; + } + + if (SA1Enable) + { + RestoreSA1(); //Convert back SA-1 stuff + } + + if (Thumbnail) + { + CapturePicture(); + fwrite(PrevPicture, 1, 64*56*sizeof(unsigned short), fhandle); + } + + ResetOffset(); + ResetState(); +} + void statesaver() { //'Auto increment savestate slot' code @@ -702,42 +744,8 @@ void statesaver() if ((fhandle = fopen(fnamest+1,"wb"))) { - fwrite(zst_header_cur, 1, sizeof(zst_header_cur)-1, fhandle); //-1 for null - - PrepareOffset(); - PrepareSaveState(); - unpackfunct(); - - if (SFXEnable) - { - SfxRomBuffer -= SfxCROM; - SfxLastRamAdr -= SfxRAMMem; - } - - if (SA1Enable) - { - SaveSA1(); //Convert SA-1 stuff to standard, non displacement format - } - - copy_state_data(0, write_save_state_data, false); - - if (SFXEnable) - { - SfxRomBuffer += SfxCROM; - SfxLastRamAdr += SfxRAMMem; - } - - if (SA1Enable) - { - RestoreSA1(); //Convert back SA-1 stuff - } - - if (cbitmode && !NoPictureSave) - { - CapturePicture(); - fwrite(PrevPicture, 1, 64*56*sizeof(unsigned short), fhandle); - } - + zst_save(fhandle, (cbitmode && !NoPictureSave) ? true : false) + fclose(fhandle); //Display message on the screen, 'STATE X SAVED.' @@ -752,9 +760,6 @@ void statesaver() Msgptr = txtsavemsg; MessageOn = MsgCount; - - ResetOffset(); - ResetState(); } else {