Skip to content

Commit

Permalink
Implement persistent buffer support
Browse files Browse the repository at this point in the history
  • Loading branch information
inactive123 committed Jun 6, 2021
1 parent fb6d2f6 commit 1e0c4f9
Show file tree
Hide file tree
Showing 5 changed files with 794 additions and 57 deletions.
4 changes: 2 additions & 2 deletions gb_core/gb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ void gb::set_skip(int frame)
skip_buf=frame;
}

bool gb::load_rom(byte *buf,int size,byte *ram,int ram_size)
bool gb::load_rom(byte *buf,int size,byte *ram,int ram_size, bool persistent)
{
if (m_rom->load_rom(buf,size,ram,ram_size))
if (m_rom->load_rom(buf,size,ram,ram_size, persistent))
{
reset();
return true;
Expand Down
5 changes: 3 additions & 2 deletions gb_core/gb.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ friend class cpu;
void reset();
void set_skip(int frame);
void set_use_gba(bool use) { use_gba=use; }
bool load_rom(byte *buf,int size,byte *ram,int ram_size);
bool load_rom(byte *buf,int size,byte *ram,int ram_size, bool persistent);

void serialize(serializer &s);
void serialize_firstrev(serializer &s);
Expand Down Expand Up @@ -454,7 +454,7 @@ class rom

void set_first(int page) { first_page=dat+0x4000*page; }

bool load_rom(byte *buf,int size,byte *ram,int ram_size);
bool load_rom(byte *buf,int size,byte *ram,int ram_size, bool persistent);

void serialize(serializer &s);
private:
Expand All @@ -466,6 +466,7 @@ class rom
byte *first_page;

bool b_loaded;
bool b_persistent;
};

class cpu
Expand Down
27 changes: 18 additions & 9 deletions gb_core/rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@

rom::rom()
{
b_loaded=false;
b_loaded = false;
b_persistent = false;

dat=NULL;
sram=NULL;
dat = NULL;
sram = NULL;
}

rom::~rom()
{
free(dat);
if (!b_persistent)
free(dat);
free(sram);
}

Expand All @@ -54,12 +56,13 @@ int rom::get_sram_size()
return 0x2000*tbl_ram[info.ram_size];
}

bool rom::load_rom(byte *buf,int size,byte *ram,int ram_size)
bool rom::load_rom(byte *buf,int size,byte *ram,int ram_size, bool persistent)
{
byte momocol_title[16]={0x4D,0x4F,0x4D,0x4F,0x43,0x4F,0x4C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

if (b_loaded){
free(dat);
if (!persistent)
free(dat);
free(sram);
}

Expand All @@ -81,15 +84,21 @@ bool rom::load_rom(byte *buf,int size,byte *ram,int ram_size)
if (info.rom_size>8)
return false;

dat=(byte*)malloc(size);
memcpy(dat,buf,size);
if (persistent)
dat = (byte*)buf;
else
{
dat=(byte*)malloc(size);
memcpy(dat,buf,size);
}
first_page=dat;

sram=(byte*)malloc(get_sram_size());
if (ram)
memcpy(sram,ram,ram_size&0xffffff00);

b_loaded=true;
b_loaded = true;
b_persistent = persistent;

return true;
}
Expand Down
75 changes: 54 additions & 21 deletions libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,27 @@ retro_environment_t environ_cb;
retro_input_poll_t input_poll_cb;
retro_input_state_t input_state_cb;

static size_t _serialize_size[2] = { 0, 0 };
extern bool _screen_2p_vertical;
extern bool _screen_switched;
extern int _show_player_screens;
bool gblink_enable = false;
int audio_2p_mode = 0;
static size_t _serialize_size[2] = { 0, 0 };

bool gblink_enable = false;
int audio_2p_mode = 0;
// used to make certain core options only take effect once on core startup
bool already_checked_options = false;
bool libretro_supports_bitmasks = false;
struct retro_system_av_info *my_av_info = (retro_system_av_info*)malloc(sizeof(*my_av_info));
bool already_checked_options = false;
bool libretro_supports_persistent_buffer = false;
bool libretro_supports_bitmasks = false;
struct retro_system_av_info *my_av_info = (retro_system_av_info*)malloc(sizeof(*my_av_info));

void retro_get_system_info(struct retro_system_info *info)
{
info->library_name = "TGB Dual";
info->library_name = "TGB Dual";
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
info->library_version = "v0.8.3" GIT_VERSION;
info->need_fullpath = false;
info->library_version = "v0.8.3" GIT_VERSION;
info->need_fullpath = false;
info->valid_extensions = "gb|dmg|gbc|cgb|sgb";
}

Expand All @@ -104,10 +106,10 @@ void retro_get_system_av_info(struct retro_system_av_info *info)
w *= 2;
}

info->timing.fps = 4194304.0 / 70224.0;
info->timing.sample_rate = 44100.0f;
info->geometry.base_width = w;
info->geometry.base_height = h;
info->timing.fps = 4194304.0 / 70224.0;
info->timing.sample_rate = 44100.0f;
info->geometry.base_width = w;
info->geometry.base_height = h;
info->geometry.aspect_ratio = float(w) / float(h);
memcpy(my_av_info, info, sizeof(*my_av_info));
}
Expand All @@ -132,7 +134,7 @@ void retro_init(void)

void retro_deinit(void)
{
libretro_supports_bitmasks = false;
libretro_supports_bitmasks = false;
}

static void check_variables(void)
Expand Down Expand Up @@ -227,6 +229,9 @@ static void check_variables(void)

bool retro_load_game(const struct retro_game_info *info)
{
size_t rom_size;
byte *rom_data;
const struct retro_game_info_ext *info_ext = NULL;
environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars_single);
check_variables();

Expand Down Expand Up @@ -267,21 +272,37 @@ bool retro_load_game(const struct retro_game_info *info)

render[0] = new dmy_renderer(0);
g_gb[0] = new gb(render[0], true, true);
if (!g_gb[0]->load_rom((byte*)info->data, info->size, NULL, 0))

if (environ_cb(RETRO_ENVIRONMENT_GET_GAME_INFO_EXT, &info_ext) &&
info_ext->persistent_data)
{
rom_data = (byte*)info_ext->data;
rom_size = info_ext->size;
libretro_supports_persistent_buffer = true;
}
else
{
rom_data = (byte*)info->data;
rom_size = info->size;
}

if (!g_gb[0]->load_rom(rom_data, rom_size, NULL, 0,
libretro_supports_persistent_buffer))
return false;

for (i = 0; i < 2; i++)
_serialize_size[i] = 0;

if (gblink_enable)
{
mode = MODE_SINGLE_GAME_DUAL;
mode = MODE_SINGLE_GAME_DUAL;
environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars_dual);

render[1] = new dmy_renderer(1);
g_gb[1] = new gb(render[1], true, true);
g_gb[1] = new gb(render[1], true, true);

if (!g_gb[1]->load_rom((byte*)info->data, info->size, NULL, 0))
if (!g_gb[1]->load_rom(rom_data, rom_size, NULL, 0,
libretro_supports_persistent_buffer))
return false;

// for link cables and IR:
Expand All @@ -299,7 +320,6 @@ bool retro_load_game(const struct retro_game_info *info)

bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num_info)
{

if (type != RETRO_GAME_TYPE_GAMEBOY_LINK_2P)
return false; /* all other types are unhandled for now */

Expand Down Expand Up @@ -347,7 +367,7 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info,

render[0] = new dmy_renderer(0);
g_gb[0] = new gb(render[0], true, true);
if (!g_gb[0]->load_rom((byte*)info[0].data, info[0].size, NULL, 0))
if (!g_gb[0]->load_rom((byte*)info[0].data, info[0].size, NULL, 0, false))
return false;

for (i = 0; i < 2; i++)
Expand All @@ -358,7 +378,8 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info,
render[1] = new dmy_renderer(1);
g_gb[1] = new gb(render[1], true, true);

if (!g_gb[1]->load_rom((byte*)info[1].data, info[1].size, NULL, 0))
if (!g_gb[1]->load_rom((byte*)info[1].data, info[1].size, NULL, 0,
false))
return false;

// for link cables and IR:
Expand All @@ -385,6 +406,7 @@ void retro_unload_game(void)
}
}
free(my_av_info);
libretro_supports_persistent_buffer = false;
}

void retro_reset(void)
Expand Down Expand Up @@ -645,8 +667,19 @@ void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }

void retro_set_environment(retro_environment_t cb)
{
static const struct retro_system_content_info_override content_overrides[] = {
{
"gb|dmg|gbc|cgb|sgb", /* extensions */
false, /* need_fullpath */
true /* persistent_data */
},
{ NULL, false, false }
};
environ_cb = cb;
cb(RETRO_ENVIRONMENT_SET_SUBSYSTEM_INFO, (void*)subsystems);
/* Request a persistent content data buffer */
cb(RETRO_ENVIRONMENT_SET_CONTENT_INFO_OVERRIDE,
(void*)content_overrides);
}

// end boilerplate
Loading

0 comments on commit 1e0c4f9

Please sign in to comment.