Skip to content

Commit

Permalink
Experimental ZIP support (#132)
Browse files Browse the repository at this point in the history
This adds zip support to GB, GBC, NES, SNES, SMS, GG, COL, PCE, LYNX, MD/GEN

The first file in the ZIP is loaded.

Still to fix:
    - Be smarter about picking a file in the zip
    - Reduce unzip memory usage to allow loading larger ROMs
    - Maybe use miniz' ZIP implementation instead of my own
  • Loading branch information
ducalex committed Jul 22, 2024
1 parent 2c0b8f4 commit 12a0e96
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 27 deletions.
22 changes: 13 additions & 9 deletions gwenesis/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,19 @@ void app_main(void)

RG_LOGI("Genesis start\n");

FILE *fp = fopen(app->romPath, "rb");
if (!fp)
RG_PANIC("Rom load failed");
fseek(fp, 0, SEEK_END);
size_t rom_size = ftell(fp);
void *rom_data = malloc((rom_size & ~0xFFFF) + 0x10000);
fseek(fp, 0, SEEK_SET);
fread(rom_data, 1, rom_size, fp);
fclose(fp);
size_t rom_size;
void *rom_data;

if (rg_extension_match(app->romPath, "zip"))
{
if (!rg_storage_unzip_file(app->romPath, NULL, &rom_data, &rom_size))
RG_PANIC("ROM file unzipping failed!");
}
else
{
if (!rg_storage_read_file(app->romPath, &rom_data, &rom_size))
RG_PANIC("ROM load failed!");
}

RG_LOGI("load_cartridge(%p, %d)\n", rom_data, rom_size);
load_cartridge(rom_data, rom_size);
Expand Down
20 changes: 10 additions & 10 deletions launcher/main/applications.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,18 +673,18 @@ static void application(const char *desc, const char *name, const char *exts, co

void applications_init(void)
{
application("Nintendo Entertainment System", "nes", "nes fc fds nsf", "retro-core", 16);
application("Super Nintendo", "snes", "smc sfc", "retro-core", 0);
application("Nintendo Gameboy", "gb", "gb gbc", "retro-core", 0);
application("Nintendo Gameboy Color", "gbc", "gbc gb", "retro-core", 0);
application("Nintendo Entertainment System", "nes", "nes fc fds nsf zip", "retro-core", 16);
application("Super Nintendo", "snes", "smc sfc zip", "retro-core", 0);
application("Nintendo Gameboy", "gb", "gb gbc zip", "retro-core", 0);
application("Nintendo Gameboy Color", "gbc", "gbc gb zip", "retro-core", 0);
application("Nintendo Game & Watch", "gw", "gw", "retro-core", 0);
// application("Sega SG-1000", "sg1", "sms sg sg1", "retro-core", 0);
application("Sega Master System", "sms", "sms sg", "retro-core", 0);
application("Sega Game Gear", "gg", "gg", "retro-core", 0);
application("Sega Mega Drive", "md", "md gen bin", "gwenesis", 0);
application("Coleco ColecoVision", "col", "col rom", "retro-core", 0);
application("NEC PC Engine", "pce", "pce", "retro-core", 0);
application("Atari Lynx", "lnx", "lnx", "retro-core", 64);
application("Sega Master System", "sms", "sms sg zip", "retro-core", 0);
application("Sega Game Gear", "gg", "gg zip", "retro-core", 0);
application("Sega Mega Drive", "md", "md gen bin zip", "gwenesis", 0);
application("Coleco ColecoVision", "col", "col rom zip", "retro-core", 0);
application("NEC PC Engine", "pce", "pce zip", "retro-core", 0);
application("Atari Lynx", "lnx", "lnx zip", "retro-core", 64);
// application("Atari 2600", "a26", "a26", "stella-go", 0);
// application("Neo Geo Pocket Color", "ngp", "ngp ngc", "ngpocket-go", 0);
application("DOOM", "doom", "wad", "prboom-go", 0);
Expand Down
13 changes: 12 additions & 1 deletion retro-core/main/main_gbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,19 @@ void gbc_main(void)
gnuboy_set_soundbuffer((void *)audioBuffer, sizeof(audioBuffer) / 2);

// Load ROM
if (gnuboy_load_rom_file(app->romPath) < 0)
if (rg_extension_match(app->romPath, "zip"))
{
void *data;
size_t size;
if (!rg_storage_unzip_file(app->romPath, NULL, &data, &size))
RG_PANIC("ROM file unzipping failed!");
if (gnuboy_load_rom(data, size) < 0)
RG_PANIC("ROM Loading failed!");
}
else if (gnuboy_load_rom_file(app->romPath) < 0)
{
RG_PANIC("ROM Loading failed!");
}

// Load BIOS
if (loadBIOSFile)
Expand Down
20 changes: 18 additions & 2 deletions retro-core/main/main_lynx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ static void set_display_mode(void)
updates[1]->height = height;
}

static CSystem *new_lynx(void)
{
CSystem *lynx;
if (rg_extension_match(app->romPath, "zip"))
{
void *data;
size_t size;
if (!rg_storage_unzip_file(app->romPath, NULL, &data, &size))
RG_PANIC("ROM file unzipping failed!");
CSystem *lynx = new CSystem((UBYTE*)data, size, MIKIE_PIXEL_FORMAT_16BPP_565_BE, app->sampleRate);
free(data);
return lynx;
}
return new CSystem(app->romPath, MIKIE_PIXEL_FORMAT_16BPP_565_BE, app->sampleRate);
}


static rg_gui_event_t rotation_cb(rg_gui_option_t *option, rg_gui_event_t event)
{
Expand Down Expand Up @@ -159,7 +175,7 @@ static bool reset_handler(bool hard)
{
// This isn't nice but lynx->Reset() crashes...
delete lynx;
lynx = new CSystem(app->romPath, MIKIE_PIXEL_FORMAT_16BPP_565_BE, app->sampleRate);
lynx = new_lynx();
return true;
}

Expand Down Expand Up @@ -190,7 +206,7 @@ extern "C" void lynx_main(void)
app->tickRate = 60;

// Init emulator
lynx = new CSystem(app->romPath, MIKIE_PIXEL_FORMAT_16BPP_565_BE, app->sampleRate);
lynx = new_lynx();

if (lynx->mFileType == HANDY_FILETYPE_ILLEGAL)
{
Expand Down
16 changes: 14 additions & 2 deletions retro-core/main/main_nes.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,23 @@ void nes_main(void)

nes = nes_init(SYS_DETECT, app->sampleRate, true, RG_BASE_PATH_BIOS "/fds_bios.bin");
if (!nes)
{
RG_PANIC("Init failed.");

int ret = -1;

if (rg_extension_match(app->romPath, "zip"))
{
void *data;
size_t size;
if (!rg_storage_unzip_file(app->romPath, NULL, &data, &size))
RG_PANIC("ROM file unzipping failed!");
ret = nes_insertcart(rom_loadmem(data, size));
}
else
{
ret = nes_loadfile(app->romPath);
}

int ret = nes_loadfile(app->romPath);
if (ret == -1)
RG_PANIC("ROM load failed.");
else if (ret == -2)
Expand Down
11 changes: 10 additions & 1 deletion retro-core/main/main_pce.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,16 @@ void pce_main(void)

InitPCE(app->sampleRate, true);

if (LoadFile(app->romPath) != 0)
if (rg_extension_match(app->romPath, "zip"))
{
void *data;
size_t size;
if (!rg_storage_unzip_file(app->romPath, NULL, &data, &size))
RG_PANIC("ROM file unzipping failed!");
if (LoadCard(data, size) != 0)
RG_PANIC("ROM loading failed");
}
else if (LoadFile(app->romPath) != 0)
{
RG_PANIC("ROM loading failed");
}
Expand Down
11 changes: 10 additions & 1 deletion retro-core/main/main_sms.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,16 @@ void sms_main(void)
else
option.console = 0;

if (!load_rom_file(app->romPath))
if (rg_extension_match(app->romPath, "zip"))
{
void *data;
size_t size;
if (!rg_storage_unzip_file(app->romPath, NULL, &data, &size))
RG_PANIC("ROM file unzipping failed!");
if (!load_rom(data, RG_MAX(0x4000, size), size))
RG_PANIC("ROM file loading failed!");
}
else if (!load_rom_file(app->romPath))
RG_PANIC("ROM file loading failed!");

bitmap.width = SMS_WIDTH;
Expand Down
12 changes: 11 additions & 1 deletion retro-core/main/main_snes.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,17 @@ void snes_main(void)
if (!S9xInitGFX())
RG_PANIC("Graphics init failed!");

if (!LoadROM(app->romPath))
const char *filename = app->romPath;

if (rg_extension_match(filename, "zip"))
{
free(Memory.ROM); // Would be nice to reuse it directly...
if (!rg_storage_unzip_file(filename, NULL, (void **)&Memory.ROM, &Memory.ROM_Size))
RG_PANIC("ROM file unzipping failed!");
filename = NULL;
}

if (!LoadROM(filename))
RG_PANIC("ROM loading failed!");

#ifdef USE_BLARGG_APU
Expand Down

0 comments on commit 12a0e96

Please sign in to comment.