Skip to content

Commit

Permalink
Highscore Cart (highscore.rom) supported for any game that uses it. M…
Browse files Browse the repository at this point in the history
…inor TIA speedups and improvements.
  • Loading branch information
wavemotion-dave committed Dec 13, 2020
1 parent 3766aeb commit 2972902
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 143 deletions.
Binary file modified A7800DS.nds
Binary file not shown.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=1.3a
VERSION=1.4
TARGNAME=A7800DS

#---------------------------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion arm9/source/a7800utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void dsLoadGame(char *filename)
#ifdef CART_INFO
char cart_info_buf[32];
dsPrintValue(0,22,0,cartridge_digest); //zzz
sprintf(cart_info_buf, "CT=%d PK=%d CO=%d%d RE=%d FL=%d ", cartridge_type, cartridge_pokey, cartridge_controller[0], cartridge_controller[1], cartridge_region, cartridge_flags);
sprintf(cart_info_buf, "CT=%d PK=%d CO=%d%d RE=%d FL=%d HS=%d ", cartridge_type, cartridge_pokey, cartridge_controller[0], cartridge_controller[1], cartridge_region, cartridge_flags, cartridge_hsc_enabled);
dsPrintValue(0,21,0,cart_info_buf); //zzz
#endif

Expand Down Expand Up @@ -753,6 +753,7 @@ void dsInstallSoundEmuFIFO(void)

void dsMainLoop(void)
{
static int check_hsc_save=0;
static int last_keys_pressed = 999;
char fpsbuf[32];
unsigned int keys_pressed,keys_touch=0, romSel;
Expand Down Expand Up @@ -889,6 +890,12 @@ void dsMainLoop(void)
dsPrintValue(0,0,0, fpsbuf);
}
DumpDebugData();

// Every 3 seconds check the HSC to see if SRAM needs snap-shot
if (++check_hsc_save % 3 == 0)
{
cartridge_SaveHighScoreSram();
}
}
break;
}
Expand Down
131 changes: 129 additions & 2 deletions arm9/source/emu/Cartridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ byte cartridge_type;
byte cartridge_region;
bool cartridge_pokey;
bool cartridge_pokey450;
bool cartridge_hsc_enabled;
byte cartridge_controller[2];
byte cartridge_bank;
uint cartridge_flags;
Expand All @@ -44,7 +45,7 @@ int cartridge_xOffset=0;
bool cartridge_wsync;
bool cartridge_cycle_stealing;


extern int debug[];
static byte* cartridge_buffer = NULL;
static uint cartridge_size = 0;

Expand Down Expand Up @@ -143,7 +144,8 @@ static void cartridge_ReadHeader(const byte* header) {
cartridge_controller[1] = header[56];
cartridge_region = header[57];
cartridge_flags = 0;

cartridge_hsc_enabled = (header[0x3A]&1 ? true:false);
cartridge_hsc_enabled=true;
cartridge_wsync=cartridge_cycle_stealing=true; // Guess as best we can...
}

Expand Down Expand Up @@ -424,6 +426,129 @@ void cartridge_StoreBank(byte bank) {
}
}



// The memory location of the high score cartridge SRAM
#define HS_SRAM_START 0x1000
// The size of the high score cartridge SRAM
#define HS_SRAM_SIZE 2048

byte high_score_cart_loaded = false;

u32 last_known_chksum = 0;

/*
* Saves the high score cartridge SRAM
*
* return Whether the save was successful
*/
bool cartridge_SaveHighScoreSram()
{
if( !high_score_cart_loaded)
{
// If we didn't load the high score cartridge, don't save.
return false;
}

// ------------------------------------------------------------
// Fast checksum to see if the SRAM for HSC has changed...
// don't write anything if it hasn't changed!
// ------------------------------------------------------------
u32 chksum = 0xDEADBEEF;
for (int i=0; i<HS_SRAM_SIZE; i++)
{
chksum += memory_ram[i];
}
if (chksum != last_known_chksum)
{
last_known_chksum = chksum;
FILE* file = fopen("A7800DS.sram", "wb");
if( file == NULL )
{
return false;
}

if( fwrite( &(memory_ram[HS_SRAM_START]), 1, HS_SRAM_SIZE, file ) != HS_SRAM_SIZE )
{
fclose( file );
return false;
}

fflush(file);
fclose(file);
}

return true;
}

/*
* Loads the high score cartridge SRAM
*
* return Whether the load was successful
*/
static bool cartridge_LoadHighScoreSram()
{
byte sram[HS_SRAM_SIZE];
FILE* file = fopen("A7800DS.sram", "rb" );
if( file == NULL )
{
return false;
}

if( fread( sram, 1, HS_SRAM_SIZE, file ) != HS_SRAM_SIZE )
{
fclose( file );
return false;
}

last_known_chksum = 0xDEADBEEF;
for( uint i = 0; i < HS_SRAM_SIZE; i++ )
{
memory_Write( HS_SRAM_START + i, sram[i] );
last_known_chksum += sram[i];
}

fclose(file);

return true;
}

/*
* Loads the high score cartridge
*
* return Whether the load was successful
*/
#define HSC_CART_ROM_SIZE 4096
byte high_score_buffer[HSC_CART_ROM_SIZE];
bool cartridge_LoadHighScoreCart()
{
if( !cartridge_hsc_enabled || cartridge_region != 0 )
{
// Only load the cart if it is enabled and the region is NTSC
return false;
}

FILE* file = fopen("highscore.rom", "rb" );

if( file != NULL )
{
fread(high_score_buffer, 1, HSC_CART_ROM_SIZE, file );
cartridge_LoadHighScoreSram();
for( uint i = 0; i < HSC_CART_ROM_SIZE; i++ )
{
memory_Write( 0x3000 + i, high_score_buffer[i] );
}
high_score_cart_loaded = true;
}
else
{
high_score_cart_loaded = false;
}

return high_score_cart_loaded;
}


// ----------------------------------------------------------------------------
// IsLoaded
// ----------------------------------------------------------------------------
Expand All @@ -447,6 +572,8 @@ void cartridge_Release( ) {
cartridge_region = 0;
cartridge_pokey = 0;
cartridge_pokey450 = 0;
cartridge_hsc_enabled = false;

memset( cartridge_controller, 0, sizeof( cartridge_controller ) );
cartridge_bank = 0;
cartridge_flags = 0;
Expand Down
3 changes: 3 additions & 0 deletions arm9/source/emu/Cartridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extern void cartridge_StoreBank(byte bank);
extern void cartridge_Write(word address, byte data);
extern bool cartridge_IsLoaded( );
extern void cartridge_Release( );
extern bool cartridge_LoadHighScoreCart();
extern bool cartridge_SaveHighScoreSram();
extern char cartridge_title[128];
extern char cartridge_description[128];
extern char cartridge_year[128];
Expand All @@ -65,6 +67,7 @@ extern byte cartridge_type;
extern byte cartridge_region;
extern bool cartridge_pokey;
extern bool cartridge_pokey450;
extern bool cartridge_hsc_enabled;
extern byte cartridge_controller[2];
extern byte cartridge_bank;
extern uint cartridge_flags;
Expand Down
Loading

0 comments on commit 2972902

Please sign in to comment.