From 0e8d8effdf28d6131d7a96fe230762294ff20225 Mon Sep 17 00:00:00 2001 From: John Regan Date: Tue, 20 Apr 2021 08:38:53 -0400 Subject: [PATCH 1/2] gbs: function to load from memory buffer --- Core/gb.c | 44 ++++++++++++++++++++++++++++++++------------ Core/gb.h | 1 + 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 722b0b1..0a0c1dd 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -369,24 +369,25 @@ void GB_gbs_switch_track(GB_gameboy_t *gb, uint8_t track) } } -int GB_load_gbs(GB_gameboy_t *gb, const char *path, GB_gbs_info_t *info) +int GB_load_gbs_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size, GB_gbs_info_t *info) { - FILE *f = fopen(path, "rb"); - if (!f) { - GB_log(gb, "Could not open GBS: %s.\n", strerror(errno)); - return errno; + if(size < sizeof(gb->gbs_header)) { + GB_log(gb, "Not a valid GBS file.\n"); + return -1; } - fread(&gb->gbs_header, sizeof(gb->gbs_header), 1, f); + + memcpy(&gb->gbs_header,buffer,sizeof(gb->gbs_header)); + if (gb->gbs_header.magic != BE32('GBS\x01') || ((LE16(gb->gbs_header.load_address) < GBS_ENTRY + GBS_ENTRY_SIZE || LE16(gb->gbs_header.load_address) >= 0x8000) && LE16(gb->gbs_header.load_address) != 0)) { GB_log(gb, "Not a valid GBS file.\n"); - fclose(f); return -1; } - fseek(f, 0, SEEK_END); - size_t data_size = ftell(f) - sizeof(gb->gbs_header); + + size_t data_size = size - sizeof(gb->gbs_header); + gb->rom_size = (data_size + LE16(gb->gbs_header.load_address) + 0x3FFF) & ~0x3FFF; /* Round to bank */ /* And then round to a power of two */ while (gb->rom_size & (gb->rom_size - 1)) { @@ -398,14 +399,14 @@ int GB_load_gbs(GB_gameboy_t *gb, const char *path, GB_gbs_info_t *info) if (gb->rom_size == 0) { gb->rom_size = 0x8000; } - fseek(f, sizeof(gb->gbs_header), SEEK_SET); + if (gb->rom) { free(gb->rom); } + gb->rom = malloc(gb->rom_size); memset(gb->rom, 0xFF, gb->rom_size); /* Pad with 0xFFs */ - fread(gb->rom + LE16(gb->gbs_header.load_address), 1, data_size, f); - fclose(f); + memcpy(gb->rom + LE16(gb->gbs_header.load_address), buffer + sizeof(gb->gbs_header), data_size); gb->cartridge_type = &GB_cart_defs[0x11]; if (gb->mbc_ram) { @@ -453,6 +454,25 @@ int GB_load_gbs(GB_gameboy_t *gb, const char *path, GB_gbs_info_t *info) return 0; } +int GB_load_gbs(GB_gameboy_t *gb, const char *path, GB_gbs_info_t *info) +{ + FILE *f = fopen(path, "rb"); + if (!f) { + GB_log(gb, "Could not open GBS: %s.\n", strerror(errno)); + return errno; + } + fseek(f, 0, SEEK_END); + size_t file_size = ftell(f); + fseek(f, 0, SEEK_SET); + uint8_t *file_data = malloc(file_size); + fread(file_data,1,file_size,f); + fclose(f); + + int r = GB_load_gbs_from_buffer(gb,file_data,file_size,info); + free(file_data); + return r; +} + int GB_load_isx(GB_gameboy_t *gb, const char *path) { FILE *f = fopen(path, "rb"); diff --git a/Core/gb.h b/Core/gb.h index a2e8db1..e7e0ca0 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -819,6 +819,7 @@ void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, int GB_load_rom(GB_gameboy_t *gb, const char *path); void GB_load_rom_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size); int GB_load_isx(GB_gameboy_t *gb, const char *path); +int GB_load_gbs_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size, GB_gbs_info_t *info); int GB_load_gbs(GB_gameboy_t *gb, const char *path, GB_gbs_info_t *info); void GB_gbs_switch_track(GB_gameboy_t *gb, uint8_t track); From 9b2dfe7ae276d441583c62d582725a7cbd483bd2 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 17 May 2021 16:52:55 +0300 Subject: [PATCH 2/2] Style fixes --- Core/gb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index 0a0c1dd..4bf9287 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -371,12 +371,12 @@ void GB_gbs_switch_track(GB_gameboy_t *gb, uint8_t track) int GB_load_gbs_from_buffer(GB_gameboy_t *gb, const uint8_t *buffer, size_t size, GB_gbs_info_t *info) { - if(size < sizeof(gb->gbs_header)) { + if (size < sizeof(gb->gbs_header)) { GB_log(gb, "Not a valid GBS file.\n"); return -1; } - memcpy(&gb->gbs_header,buffer,sizeof(gb->gbs_header)); + memcpy(&gb->gbs_header, buffer, sizeof(gb->gbs_header)); if (gb->gbs_header.magic != BE32('GBS\x01') || ((LE16(gb->gbs_header.load_address) < GBS_ENTRY + GBS_ENTRY_SIZE ||