Skip to content

Commit

Permalink
Merge pull request #43 from negativeExponent/memory_map
Browse files Browse the repository at this point in the history
Add memory map descriptors
  • Loading branch information
LibretroAdmin authored Jul 20, 2024
2 parents 96fa9bc + b0af899 commit 081d956
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
27 changes: 27 additions & 0 deletions source/ports/libretro/smsplus_libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,31 @@ static void check_variables(bool startup)
bitmap.viewport.changed = 1;
}

static void retro_set_memory_map(void)
{
const uint64_t mem = RETRO_MEMDESC_SYSTEM_RAM;
struct retro_memory_map mmaps;
struct retro_memory_descriptor descs[64] = { 0 };
size_t i = 0, j = 0;

memset(descs, 0, sizeof(descs));
for (i = 0; i < 64; i++)
{
if (MMapPtrs[i] != NULL)
{
descs[j].ptr = MMapPtrs[i];
descs[j].start = i * 1024;
descs[j].len = 1024;
descs[j].select = 0;
j++;
}
}

mmaps.descriptors = descs;
mmaps.num_descriptors = j;
environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps);
}

bool retro_load_game(const struct retro_game_info *info)
{
enum retro_pixel_format rgb565 = RETRO_PIXEL_FORMAT_RGB565;
Expand Down Expand Up @@ -610,6 +635,8 @@ bool retro_load_game(const struct retro_game_info *info)

libretro_serialize_size = 0;

retro_set_memory_map();

return true;
}

Expand Down
32 changes: 32 additions & 0 deletions source/sms.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ t_coleco coleco;
uint8_t dummy_write[0x400];
uint8_t dummy_read[0x400];

uint8_t *MMapPtrs[64] = { 0 };

static void writemem_mapper_none(uint16_t offset, uint8_t data)
{
cpu_writemap[offset >> 10][offset & 0x03FF] = data;
Expand Down Expand Up @@ -128,6 +130,10 @@ static void writemem_mapper_4pak(uint16_t offset, uint8_t data)
cpu_writemap[offset >> 10][offset & 0x03FF] = data;
}

void MemoryAddRAM(int bank, int offset, void *ptr)
{
MMapPtrs[bank] = ptr + offset;
}

void mapper_reset(void)
{
Expand Down Expand Up @@ -276,6 +282,7 @@ void sms_reset(void)
{
cpu_readmap[i] = &coleco.rom[i << 10];
cpu_writemap[i] = dummy_write;
MemoryAddRAM(i, i << 10, coleco.rom);
}

/* $2000-$5FFF mapped to expansion */
Expand All @@ -290,13 +297,15 @@ void sms_reset(void)
{
cpu_readmap[i] = &sms.wram[0];
cpu_writemap[i] = &sms.wram[0];
MemoryAddRAM(i, 0, sms.wram);
}

/* $8000-$FFFF mapped to Cartridge ROM (max. 32K) */
for(i = 0x20; i < 0x40; i++)
{
cpu_readmap[i] = &cart.rom[(i&0x1F) << 10];
cpu_writemap[i] = dummy_write;
MemoryAddRAM(i, (i&0x1F) << 10, cart.rom);
}

/* reset I/O */
Expand All @@ -315,13 +324,15 @@ void sms_reset(void)
{
cpu_readmap[i] = &coleco.rom[i << 10];
cpu_writemap[i] = dummy_write;
MemoryAddRAM(i, i << 10, coleco.rom);
}

/* $2000-$5FFF mapped to cartridge ROM (max. 16K) */
for(i = 0x08; i < 0x17; i++)
{
cpu_readmap[i] = &cart.rom[i << 10];
cpu_writemap[i] = dummy_write;
MemoryAddRAM(i, i << 10, cart.rom);
}

/* $6000-$6FFF : Reserved */
Expand All @@ -336,6 +347,7 @@ void sms_reset(void)
{
cpu_readmap[i] = &sms.wram[i << 10];
cpu_writemap[i] = &sms.wram[i << 10];
MemoryAddRAM(i, i << 10, sms.wram);
}
printf("Sord M5 mode\n");
break;
Expand All @@ -348,20 +360,23 @@ void sms_reset(void)
{
cpu_readmap[i] = &cart.rom[i << 10];
cpu_writemap[i] = dummy_write;
MemoryAddRAM(i, i << 10, cart.rom);
}

/* $8000-$BFFF mapped to external RAM (lower 16K) */
for(i = 0x20; i < 0x30; i++)
{
cpu_readmap[i] = &cart.sram[(i & 0x0F) << 10];
cpu_writemap[i] = &cart.sram[(i & 0x0F) << 10];
MemoryAddRAM(i, (i & 0x1f) << 10, cart.sram);
}

/* $C000-$FFFF mapped to internal RAM (2K) or external RAM (upper 16K) */
for(i = 0x30; i < 0x40; i++)
{
cpu_readmap[i] = &cart.sram[0x4000 + ((i & 0x0F) << 10)];
cpu_writemap[i] = &cart.sram[0x4000 + ((i & 0x0F) << 10)];
MemoryAddRAM(i, 0x4000 + ((i & 0x0F) << 10), cart.sram);
}

break;
Expand Down Expand Up @@ -399,13 +414,15 @@ void sms_reset(void)
{
cpu_readmap[i] = &slot.rom[(i & 0x1F) << 10];
cpu_writemap[i] = dummy_write;
MemoryAddRAM(i, (i & 0x1f) << 10, slot.rom);
}

/* enable internal RAM at $C000-$FFFF (8k mirrored) */
for(i = 0x30; i <= 0x3F; i++)
{
cpu_readmap[i] = &sms.wram[(i & 0x07) << 10];
cpu_writemap[i] = &sms.wram[(i & 0x07) << 10];
MemoryAddRAM(i, (i & 0x07) << 10, sms.wram);
}

/* reset cartridge paging registers */
Expand Down Expand Up @@ -466,24 +483,28 @@ void mapper_8k_w(uint16_t address, uint8_t data)
for(i = 0x20; i <= 0x27; i++)
{
cpu_readmap[i] = &slot.rom[(page << 13) | ((i & 0x07) << 10)];
MemoryAddRAM(i, (page << 13) | ((i & 0x07) << 10), slot.rom);
}
break;
case 1: /* cartridge ROM bank (16k) at $A000-$BFFF */
for(i = 0x28; i <= 0x2F; i++)
{
cpu_readmap[i] = &slot.rom[(page << 13) | ((i & 0x07) << 10)];
MemoryAddRAM(i, (page << 13) | ((i & 0x07) << 10), slot.rom);
}
break;
case 2: /* cartridge ROM bank (16k) at $4000-$5FFF */
for(i = 0x10; i <= 0x17; i++)
{
cpu_readmap[i] = &slot.rom[(page << 13) | ((i & 0x07) << 10)];
MemoryAddRAM(i, (page << 13) | ((i & 0x07) << 10), slot.rom);
}
break;
case 3: /* cartridge ROM bank (16k) at $6000-$7FFF */
for(i = 0x18; i <= 0x1F; i++)
{
cpu_readmap[i] = &slot.rom[(page << 13) | ((i & 0x07) << 10)];
MemoryAddRAM(i, (page << 13) | ((i & 0x07) << 10), slot.rom);
}
break;
}
Expand Down Expand Up @@ -525,6 +546,7 @@ void mapper_16k_w(uint16_t address, uint8_t data)
for(i = 0x20; i <= 0x2F; i++)
{
cpu_readmap[i] = cpu_writemap[i] = &cart.sram[offset + ((i & 0x0F) << 10)];
MemoryAddRAM(i, offset + ((i & 0x0F) << 10), cart.sram);
}
sms.save = 1;
}
Expand All @@ -542,6 +564,7 @@ void mapper_16k_w(uint16_t address, uint8_t data)
{
cpu_readmap[i] = &slot.rom[(page << 14) | ((i & 0x0F) << 10)];
cpu_writemap[i] = dummy_write;
MemoryAddRAM(i, (page << 14) | ((i & 0x0F) << 10), slot.rom);
}
}

Expand All @@ -551,6 +574,7 @@ void mapper_16k_w(uint16_t address, uint8_t data)
for(i = 0x30; i <= 0x3F; i++)
{
cpu_writemap[i] = cpu_readmap[i] = &cart.sram[(i & 0x0F) << 10];
MemoryAddRAM(i, (i & 0x0F) << 10, cart.sram);
}
sms.save = 1;
}
Expand All @@ -560,6 +584,7 @@ void mapper_16k_w(uint16_t address, uint8_t data)
for(i = 0x30; i <= 0x3F; i++)
{
cpu_writemap[i] = cpu_readmap[i] = &sms.wram[(i & 0x07) << 10];
MemoryAddRAM(i, (i & 0x07) << 10, sms.wram);
}
}
break;
Expand All @@ -568,17 +593,20 @@ void mapper_16k_w(uint16_t address, uint8_t data)
if (slot.mapper == MAPPER_CODIES || slot.mapper == MAPPER_4PAK)
{
cpu_readmap[0] = &slot.rom[(page << 14)];
MemoryAddRAM(0, (page << 14), slot.rom);
}

for(i = 0x01; i <= 0x0F; i++)
{
cpu_readmap[i] = &slot.rom[(page << 14) | ((i & 0x0F) << 10)];
MemoryAddRAM(i, (page << 14) | ((i & 0x0F) << 10), slot.rom);
}
break;
case 2: /* cartridge ROM bank (16k) at $4000-$7FFF */
for(i = 0x10; i <= 0x1F; i++)
{
cpu_readmap[i] = &slot.rom[(page << 14) | ((i & 0x0F) << 10)];
MemoryAddRAM(i, (page << 14) | ((i & 0x0F) << 10), slot.rom);
}

/* Ernie Elf's Golf external RAM switch */
Expand All @@ -590,6 +618,7 @@ void mapper_16k_w(uint16_t address, uint8_t data)
for(i = 0x28; i <= 0x2F; i++)
{
cpu_writemap[i] = cpu_readmap[i] = &cart.sram[(i & 0x0F) << 10];
MemoryAddRAM(i, (i & 0x0F) << 10, cart.sram);
}
sms.save = 1;
}
Expand All @@ -600,6 +629,7 @@ void mapper_16k_w(uint16_t address, uint8_t data)
{
cpu_readmap[i] = &slot.rom[((slot.fcr[3] % slot.pages) << 14) | ((i & 0x0F) << 10)];
cpu_writemap[i] = dummy_write;
MemoryAddRAM(i, ((slot.fcr[3] % slot.pages) << 14) | ((i & 0x0F) << 10), slot.rom);
}
}
}
Expand All @@ -612,6 +642,7 @@ void mapper_16k_w(uint16_t address, uint8_t data)
for(i = 0x20; i <= 0x27; i++)
{
cpu_readmap[i] = &slot.rom[(page << 14) | ((i & 0x0F) << 10)];
MemoryAddRAM(i, (page << 14) | ((i & 0x0F) << 10), slot.rom);
}

/* check that external RAM (8k) is not mapped at $A000-$BFFF (CODEMASTER mapper) */
Expand All @@ -621,6 +652,7 @@ void mapper_16k_w(uint16_t address, uint8_t data)
for(i = 0x28; i <= 0x2F; i++)
{
cpu_readmap[i] = &slot.rom[(page << 14) | ((i & 0x0F) << 10)];
MemoryAddRAM(i, (page << 14) | ((i & 0x0F) << 10), slot.rom);
}
break;
}
Expand Down
2 changes: 2 additions & 0 deletions source/sms.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,6 @@ extern void mapper_8k_w(uint16_t address, uint8_t data);
extern void mapper_16k_w(uint16_t address, uint8_t data);
extern int32_t sms_irq_callback(int32_t param);

extern uint8_t *MMapPtrs[64];

#endif /* _SMS_H_ */

0 comments on commit 081d956

Please sign in to comment.