/*************************************************************************** * MountDVD * * return 0 on error, 1 on success ***************************************************************************/ static int MountDVD(void) { GUI_MsgBoxOpen("Information", "Mounting DVD ...",1); /* check if DVD is already mounted */ if (dvd_mounted) { /* unmount DVD */ ISO9660_Unmount("dvd:"); dvd_mounted = 0; } /* check if disc is found */ if(!dvd->isInserted()) { GUI_WaitPrompt("Error","No Disc inserted !"); return 0; } /* mount DVD */ if(!ISO9660_Mount("dvd",dvd)) { GUI_WaitPrompt("Error","Disc can not be read !"); return 0; } /* DVD is mounted */ dvd_mounted = 1; GUI_MsgBoxClose(); return 1; }
int slot_save(int slot, int device) { char filename[MAXPATHLEN]; int filesize, done = 0; int offset = device ? 2112 : 0; u8 *savebuffer; if (slot > 0) { /* allocate buffer */ savebuffer = (u8 *)memalign(32,STATE_SIZE); if (!savebuffer) { GUI_WaitPrompt("Error","Unable to allocate memory !"); return 0; } GUI_MsgBoxOpen("Information","Saving State ...",1); filesize = state_save(&savebuffer[offset]); } else { if (!sram.on) { GUI_WaitPrompt("Error","SRAM is disabled !"); return 0; } /* allocate buffer */ savebuffer = (u8 *)memalign(32,0x10000+offset); if (!savebuffer) { GUI_WaitPrompt("Error","Unable to allocate memory !"); return 0; } GUI_MsgBoxOpen("Information","Saving SRAM ...",1); memcpy(&savebuffer[offset], sram.sram, 0x10000); sram.crc = crc32(0, sram.sram, 0x10000); filesize = 0x10000; } if (!device) { /* FAT support */ if (slot > 0) sprintf(filename, "%s/saves/%s.gp%d", DEFAULT_PATH, rom_filename, slot - 1); else sprintf(filename, "%s/saves/%s.srm", DEFAULT_PATH, rom_filename); /* Open file */ FILE *fp = fopen(filename, "wb"); if (!fp) { GUI_WaitPrompt("Error","Unable to open file !"); free(savebuffer); return 0; } /* Write from buffer (2k blocks) */ while (filesize > FILECHUNK) { fwrite(savebuffer + done, FILECHUNK, 1, fp); done += FILECHUNK; filesize -= FILECHUNK; } /* Write remaining bytes */ fwrite(savebuffer + done, filesize, 1, fp); done += filesize; fclose(fp); } else { /* Memory Card support */ if (slot > 0) sprintf(filename, "MD-%04X.gp%d", rominfo.realchecksum, slot - 1); else sprintf(filename, "MD-%04X.srm", rominfo.realchecksum); /* Initialise the CARD system */ char action[64]; memset(&SysArea, 0, CARD_WORKAREA); CARD_Init("GENP", "00"); /* CARD slot */ device--; /* Attempt to mount the card */ if (!CardMount(device)) { GUI_WaitPrompt("Error","Unable to mount memory card"); free(savebuffer); return 0; } /* Retrieve the sector size */ u32 SectorSize = 0; int CardError = CARD_GetSectorSize(device, &SectorSize); if (!SectorSize) { sprintf(action, "Invalid sector size (%d)", CardError); GUI_WaitPrompt("Error",action); CARD_Unmount(device); free(savebuffer); return 0; } /* Build the output buffer */ char comment[2][32] = { {"Genesis Plus GX"}, {"SRAM Save"} }; strcpy (comment[1], filename); memcpy (&savebuffer[0], &icon, 2048); memcpy (&savebuffer[2048], &comment[0], 64); /* Adjust file size */ filesize += 2112; if (filesize % SectorSize) filesize = ((filesize / SectorSize) + 1) * SectorSize; /* Check if file already exists */ card_file CardFile; if (CARD_Open(device, filename, &CardFile) == CARD_ERROR_READY) { int size = filesize - CardFile.len; CARD_Close(&CardFile); memset(&CardFile,0,sizeof(CardFile)); /* Check file new size */ if (size > 0) { CardError = CARD_Create(device, "TEMP", size, &CardFile); if (CardError) { sprintf(action, "Unable to increase file size (%d)", CardError); GUI_WaitPrompt("Error",action); CARD_Unmount(device); free(savebuffer); return 0; } /* delete temporary file */ CARD_Close(&CardFile); memset(&CardFile,0,sizeof(CardFile)); CARD_Delete(device, "TEMP"); } /* delete previously existing file */ CARD_Delete(device, filename); } /* Create a new file */ CardError = CARD_Create(device, filename, filesize, &CardFile); if (CardError) { sprintf(action, "Unable to create file (%d)", CardError); GUI_WaitPrompt("Error",action); CARD_Unmount(device); free(savebuffer); return 0; } /* Update file informations */ time_t rawtime; time(&rawtime); card_stat CardStatus; CARD_GetStatus(device, CardFile.filenum, &CardStatus); CardStatus.icon_addr = 0x0; CardStatus.icon_fmt = 2; CardStatus.icon_speed = 1; CardStatus.comment_addr = 2048; CardStatus.time = rawtime; CARD_SetStatus(device, CardFile.filenum, &CardStatus); /* Write file sectors */ while (filesize > 0) { CARD_Write(&CardFile, &savebuffer[done], SectorSize, done); filesize -= SectorSize; done += SectorSize; } /* Close file */ CARD_Close(&CardFile); CARD_Unmount(device); } GUI_MsgBoxClose(); free(savebuffer); /* Save screenshot */ if (slot && !device) { sprintf(filename,"%s/saves/%s__%d.png", DEFAULT_PATH, rom_filename, slot - 1); gxSaveScreenshot(filename); } return 1; }
int slot_load(int slot, int device) { char filename[MAXPATHLEN]; int filesize, done = 0; int offset = 0; u8 *savebuffer; if (slot > 0) { GUI_MsgBoxOpen("Information","Loading State ...",1); } else { if (!sram.on) { GUI_WaitPrompt("Error","SRAM is disabled !"); return 0; } GUI_MsgBoxOpen("Information","Loading SRAM ...",1); } if (!device) { /* FAT support */ if (slot > 0) sprintf (filename,"%s/saves/%s.gp%d", DEFAULT_PATH, rom_filename, slot - 1); else sprintf (filename,"%s/saves/%s.srm", DEFAULT_PATH, rom_filename); /* Open file */ FILE *fp = fopen(filename, "rb"); if (!fp) { GUI_WaitPrompt("Error","Unable to open file !"); return 0; } /* Read size */ fseek(fp, 0, SEEK_END); filesize = ftell(fp); fseek(fp, 0, SEEK_SET); /* allocate buffer */ savebuffer = (u8 *)memalign(32,filesize); if (!savebuffer) { GUI_WaitPrompt("Error","Unable to allocate memory !"); fclose(fp); return 0; } /* Read into buffer (2k blocks) */ while (filesize > FILECHUNK) { fread(savebuffer + done, FILECHUNK, 1, fp); done += FILECHUNK; filesize -= FILECHUNK; } /* Read remaining bytes */ fread(savebuffer + done, filesize, 1, fp); done += filesize; fclose(fp); } else { /* Memory Card support */ if (slot > 0) sprintf(filename, "MD-%04X.gp%d", rominfo.realchecksum, slot - 1); else sprintf(filename, "MD-%04X.srm", rominfo.realchecksum); /* Initialise the CARD system */ char action[64]; memset(&SysArea, 0, CARD_WORKAREA); CARD_Init("GENP", "00"); /* CARD slot */ device--; /* Attempt to mount the card */ if (!CardMount(device)) { GUI_WaitPrompt("Error","Unable to mount memory card"); return 0; } /* Retrieve the sector size */ u32 SectorSize = 0; int CardError = CARD_GetSectorSize(device, &SectorSize); if (!SectorSize) { sprintf(action, "Invalid sector size (%d)", CardError); GUI_WaitPrompt("Error",action); CARD_Unmount(device); return 0; } /* Open file */ card_file CardFile; CardError = CARD_Open(device, filename, &CardFile); if (CardError) { sprintf(action, "Unable to open file (%d)", CardError); GUI_WaitPrompt("Error",action); CARD_Unmount(device); return 0; } /* Retrieve file size */ filesize = CardFile.len; if (filesize % SectorSize) filesize = ((filesize / SectorSize) + 1) * SectorSize; /* Allocate buffer */ savebuffer = (u8 *)memalign(32,filesize); if (!savebuffer) { GUI_WaitPrompt("Error","Unable to allocate memory !"); CARD_Close(&CardFile); CARD_Unmount(device); return 0; } /* Read file sectors */ while (filesize > 0) { CARD_Read(&CardFile, &savebuffer[done], SectorSize, done); done += SectorSize; filesize -= SectorSize; } CARD_Close(&CardFile); CARD_Unmount(device); offset = 2112; } if (slot > 0) { /* Load state */ if (!state_load(&savebuffer[offset])) { free(savebuffer); GUI_WaitPrompt("Error","Unable to load state !"); return 0; } } else { /* Load SRAM & update CRC */ memcpy(sram.sram, &savebuffer[offset], 0x10000); sram.crc = crc32(0, sram.sram, 0x10000); } free(savebuffer); GUI_MsgBoxClose(); return 1; }
/***************************************************************************** * UnZipBuffer * * It should be noted that there is a limit of 5MB total size for any ROM ******************************************************************************/ int UnZipBuffer (unsigned char *outbuffer, FILE *fd) { PKZIPHEADER pkzip; int zipoffset = 0; int zipchunk = 0; char out[ZIPCHUNK]; z_stream zs; int res; int bufferoffset = 0; int have = 0; char readbuffer[ZIPCHUNK]; char msg[64]; /*** Read Zip Header ***/ fread(readbuffer, ZIPCHUNK, 1, fd); /*** Copy PKZip header to local, used as info ***/ memcpy (&pkzip, &readbuffer, sizeof (PKZIPHEADER)); if (FLIP32 (pkzip.uncompressedSize) > MAXROMSIZE) { GUI_WaitPrompt("Error","File is too large !"); return 0; } sprintf (msg, "Unzipping %d bytes ...", FLIP32 (pkzip.uncompressedSize)); GUI_MsgBoxOpen("Information",msg,1); /*** Prepare the zip stream ***/ memset (&zs, 0, sizeof (z_stream)); zs.zalloc = Z_NULL; zs.zfree = Z_NULL; zs.opaque = Z_NULL; zs.avail_in = 0; zs.next_in = Z_NULL; res = inflateInit2 (&zs, -MAX_WBITS); if (res != Z_OK) { GUI_WaitPrompt("Error","Unable to unzip file !"); return 0; } /*** Set ZipChunk for first pass ***/ zipoffset = (sizeof (PKZIPHEADER) + FLIP16 (pkzip.filenameLength) + FLIP16 (pkzip.extraDataLength)); zipchunk = ZIPCHUNK - zipoffset; /*** Now do it! ***/ do { zs.avail_in = zipchunk; zs.next_in = (Bytef *) & readbuffer[zipoffset]; /*** Now inflate until input buffer is exhausted ***/ do { zs.avail_out = ZIPCHUNK; zs.next_out = (Bytef *) & out; res = inflate (&zs, Z_NO_FLUSH); if (res == Z_MEM_ERROR) { inflateEnd (&zs); GUI_WaitPrompt("Error","Unable to unzip file !"); return 0; } have = ZIPCHUNK - zs.avail_out; if (have) { /*** Copy to normal block buffer ***/ memcpy (&outbuffer[bufferoffset], &out, have); bufferoffset += have; } } while (zs.avail_out == 0); /*** Readup the next 2k block ***/ zipoffset = 0; zipchunk = ZIPCHUNK; fread(readbuffer, ZIPCHUNK, 1, fd); } while (res != Z_STREAM_END); inflateEnd (&zs); GUI_MsgBoxClose(); if (res == Z_STREAM_END) { if (FLIP32 (pkzip.uncompressedSize) == (u32) bufferoffset) return bufferoffset; else return FLIP32 (pkzip.uncompressedSize); } return 0; }
/**************************************************************************** * LoadFile * * This function will load a game file into the ROM buffer. * This functions return the actual size of data copied into the buffer * ****************************************************************************/ int LoadFile(int selection) { int size, cd_mode1, filetype; char filename[MAXPATHLEN]; /* file path */ char *filepath = (deviceType == TYPE_RECENT) ? history.entries[selection].filepath : fileDir; /* full filename */ sprintf(filename, "%s%s", filepath, filelist[selection].filename); /* DVD hot swap */ if (!strncmp(filepath, rootdir[TYPE_DVD], strlen(rootdir[TYPE_DVD]))) { /* Check if file is still accessible */ struct stat filestat; if(stat(filename, &filestat) != 0) { /* If not, try to mount DVD */ if (!MountDVD()) return 0; } } /* open message box */ GUI_MsgBoxOpen("Information", "Loading game...", 1); /* no cartridge or CD game loaded */ size = cd_mode1 = 0; /* check if virtual CD tray was open */ if ((system_hw == SYSTEM_MCD) && (cdd.status == CD_OPEN)) { /* swap CD image file in (without changing region, system,...) */ size = cdd_load(filename, (char *)(cdc.ram)); /* check if a cartridge is currently loaded */ if (scd.cartridge.boot) { /* CD Mode 1 */ cd_mode1 = size; } else { /* update game informations from CD image file header */ getrominfo((char *)(cdc.ram)); } } /* no CD image file loaded */ if (!size) { /* close CD tray to force system reset */ cdd.status = NO_DISC; /* load game file */ size = load_rom(filename); } if (size > 0) { /* do not update game basename if a CD was loaded with a cartridge (Mode 1) */ if (cd_mode1) { /* add CD image file to history list */ filetype = 1; } else { /* auto-save previous game state */ slot_autosave(config.s_default,config.s_device); /* update game basename (for screenshot, save & cheat files) */ if (romtype & SYSTEM_SMS) { /* Master System ROM file */ filetype = 2; sprintf(rom_filename,"ms/%s",filelist[selection].filename); } else if (romtype & SYSTEM_GG) { /* Game Gear ROM file */ filetype = 3; sprintf(rom_filename,"gg/%s",filelist[selection].filename); } else if (romtype == SYSTEM_SG) { /* SG-1000 ROM file */ filetype = 4; sprintf(rom_filename,"sg/%s",filelist[selection].filename); } else if (romtype == SYSTEM_MCD) { /* CD image file */ filetype = 1; sprintf(rom_filename,"cd/%s",filelist[selection].filename); } else { /* by default, Genesis ROM file */ filetype = 0; sprintf(rom_filename,"md/%s",filelist[selection].filename); } /* remove file extension */ int i = strlen(rom_filename) - 1; while ((i > 0) && (rom_filename[i] != '.')) i--; if (i > 0) rom_filename[i] = 0; } /* add/move the file to the top of the history. */ history_add_file(filepath, filelist[selection].filename, filetype); /* recent file list may have changed */ if (deviceType == TYPE_RECENT) deviceType = -1; /* close message box */ GUI_MsgBoxClose(); /* valid image has been loaded */ return 1; } GUI_WaitPrompt("Error", "Unable to load game"); return 0; }