Skip to content

Commit 878178e

Browse files
committed
GBA: Disable Self Modifying Code detection
When the dynamic recompiler isn't used we don't need to detect SMC. This saves 288KB of memory, giving us room for one more block of ROM and it makes it possible to now move a few more things to internal memory.
1 parent 2cc7adc commit 878178e

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

gbsp/components/gbsp-libretro/cpu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,8 +1454,8 @@ u16 oam_ram[512];
14541454
u16 palette_ram[512];
14551455
u16 palette_ram_converted[512];
14561456
#ifndef RETRO_GO
1457-
u8 ewram[1024 * 256 * 2];
1458-
u8 iwram[1024 * 32 * 2];
1457+
u8 ewram[(1024 * 256) << SMC_DETECTION];
1458+
u8 iwram[(1024 * 32) << SMC_DETECTION];
14591459
u8 vram[1024 * 96];
14601460
u8 *memory_map_read[8 * 1024];
14611461
#endif

gbsp/components/gbsp-libretro/gba_memory.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ u32 function_cc read_eeprom(void)
665665
\
666666
case 0x03: \
667667
/* internal work RAM */ \
668-
value = readaddress##type(iwram, (address & 0x7FFF) + 0x8000); \
668+
value = readaddress##type(iwram, (address & 0x7FFF) + (0x8000 * SMC_DETECTION));\
669669
break; \
670670
\
671671
case 0x04: \
@@ -1418,7 +1418,7 @@ void function_cc write_gpio(u32 address, u32 value) {
14181418
\
14191419
case 0x03: \
14201420
/* internal work RAM */ \
1421-
address##type(iwram, (address & 0x7FFF) + 0x8000) = eswap##type(value); \
1421+
address##type(iwram, (address & 0x7FFF) + (0x8000 * SMC_DETECTION)) = eswap##type(value); \
14221422
break; \
14231423
\
14241424
case 0x04: \
@@ -1730,7 +1730,7 @@ const dma_region_type dma_region_map[17] =
17301730
} \
17311731

17321732
#define dma_read_iwram(type, tfsize) \
1733-
read_value = readaddress##tfsize(iwram + 0x8000, type##_ptr & 0x7FFF) \
1733+
read_value = readaddress##tfsize(iwram + (0x8000 * SMC_DETECTION), type##_ptr & 0x7FFF) \
17341734

17351735
#define dma_read_vram(type, tfsize) { \
17361736
u32 rdaddr = type##_ptr & 0x1FFFF; \
@@ -1763,9 +1763,9 @@ const dma_region_type dma_region_map[17] =
17631763
read_value = read_memory##tfsize(type##_ptr) \
17641764

17651765
#define dma_write_iwram(type, tfsize) \
1766-
address##tfsize(iwram + 0x8000, type##_ptr & 0x7FFF) = \
1766+
address##tfsize(iwram + (0x8000 * SMC_DETECTION), type##_ptr & 0x7FFF) = \
17671767
eswap##tfsize(read_value); \
1768-
if (address##tfsize(iwram, type##_ptr & 0x7FFF)) \
1768+
if (SMC_DETECTION && address##tfsize(iwram, type##_ptr & 0x7FFF)) \
17691769
alerts |= CPU_ALERT_SMC; \
17701770

17711771
#define dma_write_vram(type, tfsize) { \
@@ -1788,7 +1788,7 @@ const dma_region_type dma_region_map[17] =
17881788

17891789
#define dma_write_ewram(type, tfsize) \
17901790
address##tfsize(ewram, type##_ptr & 0x3FFFF) = eswap##tfsize(read_value); \
1791-
if (address##tfsize(ewram, (type##_ptr & 0x3FFFF) + 0x40000)) \
1791+
if (SMC_DETECTION && address##tfsize(ewram, (type##_ptr & 0x3FFFF) + 0x40000)) \
17921792
alerts |= CPU_ALERT_SMC; \
17931793

17941794
#define print_line() \
@@ -2261,7 +2261,7 @@ void init_memory(void)
22612261
map_region(read, 0x0000000, 0x1000000, 1, bios_rom);
22622262
map_null(read, 0x1000000, 0x2000000);
22632263
map_region(read, 0x2000000, 0x3000000, 8, ewram);
2264-
map_region(read, 0x3000000, 0x4000000, 1, &iwram[0x8000]);
2264+
map_region(read, 0x3000000, 0x4000000, 1, &iwram[0x8000 * SMC_DETECTION]);
22652265
map_region(read, 0x4000000, 0x5000000, 1, io_registers);
22662266
map_null(read, 0x5000000, 0x6000000);
22672267
map_null(read, 0x6000000, 0x7000000);
@@ -2381,7 +2381,7 @@ bool memory_read_savestate(const u8 *src)
23812381
return false;
23822382

23832383
if (!(
2384-
bson_read_bytes(memdoc, "iwram", &iwram[0x8000], 0x8000) &&
2384+
bson_read_bytes(memdoc, "iwram", &iwram[0x8000 * SMC_DETECTION], 0x8000) &&
23852385
bson_read_bytes(memdoc, "ewram", ewram, 0x40000) &&
23862386
bson_read_bytes(memdoc, "vram", vram, sizeof(vram)) &&
23872387
bson_read_bytes(memdoc, "oamram", oam_ram, sizeof(oam_ram)) &&
@@ -2443,7 +2443,7 @@ unsigned memory_write_savestate(u8 *dst)
24432443
u32 rtc_data_array[2] = { (u32)rtc_data, (u32)(rtc_data >> 32) };
24442444

24452445
bson_start_document(dst, "memory", wbptr);
2446-
bson_write_bytes(dst, "iwram", &iwram[0x8000], 0x8000);
2446+
bson_write_bytes(dst, "iwram", &iwram[0x8000 * SMC_DETECTION], 0x8000);
24472447
bson_write_bytes(dst, "ewram", ewram, 0x40000);
24482448
bson_write_bytes(dst, "vram", vram, sizeof(vram));
24492449
bson_write_bytes(dst, "oamram", oam_ram, sizeof(oam_ram));

gbsp/components/gbsp-libretro/gba_memory.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,23 @@ extern u32 oam_update;
258258
extern u32 gbc_sound_wave_update;
259259
extern dma_transfer_type dma[DMA_CHAN_CNT];
260260

261+
// If we're not using the dynamic recompiler we don't need to detect SMC (Self Modifying Code)
262+
// So we can save 288KB of memory by effectively disabling the SMC check.
263+
#ifdef HAVE_DYNAREC
264+
#define SMC_DETECTION 1
265+
#else /* RETRO_GO */
266+
#define SMC_DETECTION 0
267+
#endif
268+
261269
extern u16 palette_ram[512];
262270
extern u16 oam_ram[512];
263271
extern u16 palette_ram_converted[512];
264272
extern u16 io_registers[512];
265273
extern u8 vram[1024 * 96];
266274
extern u8 bios_rom[1024 * 16];
267275
// Double buffer used for SMC detection
268-
extern u8 ewram[1024 * 256 * 2];
269-
extern u8 iwram[1024 * 32 * 2];
276+
extern u8 ewram[(1024 * 256) << SMC_DETECTION];
277+
extern u8 iwram[(1024 * 32) << SMC_DETECTION];
270278

271279
extern u8 *memory_map_read[8 * 1024];
272280

@@ -336,8 +344,8 @@ typedef struct
336344
// TODO: Evaluate what is best left in internal memory for performance reasons (for the few that could fit)
337345
u8 vram[1024 * 96];
338346
u8 bios_rom[1024 * 16];
339-
u8 ewram[1024 * 256 * 2];
340-
u8 iwram[1024 * 32 * 2];
347+
u8 ewram[(1024 * 256) << SMC_DETECTION];
348+
u8 iwram[(1024 * 32) << SMC_DETECTION];
341349
u8 *memory_map_read[8 * 1024];
342350
u8 gamepak_backup[1024 * 128];
343351
// There's also stuff from video.cpp to consider:

0 commit comments

Comments
 (0)