Skip to content

Commit

Permalink
SNES: Const some jump tables, saves 5KB RAM
Browse files Browse the repository at this point in the history
  • Loading branch information
ducalex committed Oct 17, 2022
1 parent d589510 commit b29db1c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions components/retro-go/rg_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern "C" {
#define SPI_DMA_CH_AUTO 1
#endif
#else
#define EXT_RAM_ATTR
#define IRAM_ATTR
#define RTC_NOINIT_ATTR
#endif
Expand Down
2 changes: 1 addition & 1 deletion components/snes9x/apu.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool S9xInitSound(int32_t buffer_ms, int32_t lag_ms);
void S9xPrintAPUState(void);
extern uint8_t S9xAPUCycles [256]; /* Scaled cycle lengths */
extern uint8_t S9xAPUCycleLengths [256]; /* Raw data. */
extern void (*S9xApuOpcodes [256])(void);
extern void (* const S9xApuOpcodes [256])(void);

#define APU_VOL_LEFT 0x00
#define APU_VOL_RIGHT 0x01
Expand Down
12 changes: 6 additions & 6 deletions components/snes9x/cpuexec.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef struct
typedef struct
{
uint8_t* UNUSED1;
SOpcodes* S9xOpcodes;
const SOpcodes* S9xOpcodes;
SRegisters Registers;
uint8_t _Carry;
uint8_t _Zero;
Expand All @@ -36,11 +36,11 @@ void S9xDoHBlankProcessing(void);
void S9xClearIRQ(uint32_t source);
void S9xSetIRQ(uint32_t source);

extern SOpcodes S9xOpcodesE1 [256];
extern SOpcodes S9xOpcodesM1X1 [256];
extern SOpcodes S9xOpcodesM1X0 [256];
extern SOpcodes S9xOpcodesM0X1 [256];
extern SOpcodes S9xOpcodesM0X0 [256];
extern const SOpcodes S9xOpcodesE1 [256];
extern const SOpcodes S9xOpcodesM1X1 [256];
extern const SOpcodes S9xOpcodesM1X0 [256];
extern const SOpcodes S9xOpcodesM0X1 [256];
extern const SOpcodes S9xOpcodesM0X0 [256];

extern SICPU ICPU;

Expand Down
10 changes: 5 additions & 5 deletions components/snes9x/cpuops.c
Original file line number Diff line number Diff line change
Expand Up @@ -3952,7 +3952,7 @@ static void Op42(void)
}

/* CPU-S9xOpcodes Definitions */
SOpcodes S9xOpcodesM1X1[256] =
const SOpcodes S9xOpcodesM1X1[256] =
{
{Op00}, {Op01M1}, {Op02}, {Op03M1}, {Op04M1},
{Op05M1}, {Op06M1}, {Op07M1}, {Op08}, {Op09M1},
Expand Down Expand Up @@ -4008,7 +4008,7 @@ SOpcodes S9xOpcodesM1X1[256] =
{OpFFM1}
};

SOpcodes S9xOpcodesE1[256] =
const SOpcodes S9xOpcodesE1[256] =
{
{Op00}, {Op01M1}, {Op02}, {Op03M1}, {Op04M1},
{Op05M1}, {Op06M1}, {Op07M1}, {Op08E1}, {Op09M1},
Expand Down Expand Up @@ -4064,7 +4064,7 @@ SOpcodes S9xOpcodesE1[256] =
{OpFFM1}
};

SOpcodes S9xOpcodesM1X0[256] =
const SOpcodes S9xOpcodesM1X0[256] =
{
{Op00}, {Op01M1}, {Op02}, {Op03M1}, {Op04M1},
{Op05M1}, {Op06M1}, {Op07M1}, {Op08}, {Op09M1},
Expand Down Expand Up @@ -4120,7 +4120,7 @@ SOpcodes S9xOpcodesM1X0[256] =
{OpFFM1}
};

SOpcodes S9xOpcodesM0X0[256] =
const SOpcodes S9xOpcodesM0X0[256] =
{
{Op00}, {Op01M0}, {Op02}, {Op03M0}, {Op04M0},
{Op05M0}, {Op06M0}, {Op07M0}, {Op08}, {Op09M0},
Expand Down Expand Up @@ -4176,7 +4176,7 @@ SOpcodes S9xOpcodesM0X0[256] =
{OpFFM0}
};

SOpcodes S9xOpcodesM0X1[256] =
const SOpcodes S9xOpcodesM0X1[256] =
{
{Op00}, {Op01M0}, {Op02}, {Op03M0}, {Op04M0},
{Op05M0}, {Op06M0}, {Op07M0}, {Op08}, {Op09M0},
Expand Down
13 changes: 11 additions & 2 deletions components/snes9x/memmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define MAP_RONLY_SRAM_OR_NONE (Memory.SRAMSize == 0 ? (uint8_t*) MAP_NONE : (uint8_t*) MAP_RONLY_SRAM)

static int32_t retry_count = 0;
static uint8_t bytes0x2000 [0x2000];
static uint8_t *bytes0x2000; // [0x2000];
static bool is_bsx(uint8_t*);
static bool bs_name(uint8_t*);

Expand Down Expand Up @@ -255,7 +255,10 @@ bool S9xInitMemory(void)
IPPU.TileCache = (uint8_t*) calloc(MAX_2BIT_TILES, 128);
IPPU.TileCached = (uint8_t*) calloc(MAX_2BIT_TILES, 1);

if (!Memory.RAM || !Memory.SRAM || !Memory.VRAM || !Memory.ROM || !IPPU.TileCache || !IPPU.TileCached)
bytes0x2000 = (uint8_t *)calloc(0x2000, 1);

if (!Memory.RAM || !Memory.SRAM || !Memory.VRAM || !Memory.ROM
|| !IPPU.TileCache || !IPPU.TileCached || !bytes0x2000)
{
S9xDeinitMemory();
return false;
Expand Down Expand Up @@ -305,6 +308,12 @@ void S9xDeinitMemory(void)
IPPU.TileCache = NULL;
}

if (bytes0x2000)
{
free(bytes0x2000);
bytes0x2000 = NULL;
}

/* Ensure that we free the static char
* array allocated by Safe() */
Safe(NULL);
Expand Down
4 changes: 4 additions & 0 deletions components/snes9x/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#include <string.h>
#include <sys/types.h>

#ifdef RETRO_GO
#include <rg_system.h>
#else
#define EXT_RAM_ATTR
#endif

#ifndef INLINE
#define INLINE inline
Expand Down
2 changes: 1 addition & 1 deletion components/snes9x/spc700.c
Original file line number Diff line number Diff line change
Expand Up @@ -2641,7 +2641,7 @@ void ApuFB(void)
IAPU.PC += 2;
}

void (*S9xApuOpcodes[256])(void) =
void (*const S9xApuOpcodes[256])(void) =
{
Apu00, Apu01, Apu02, Apu03, Apu04, Apu05, Apu06, Apu07,
Apu08, Apu09, Apu0A, Apu0B, Apu0C, Apu0D, Apu0E, Apu0F,
Expand Down

0 comments on commit b29db1c

Please sign in to comment.