Skip to content

Commit

Permalink
Add SC64 SD card support
Browse files Browse the repository at this point in the history
  • Loading branch information
devwizard64 committed May 7, 2023
1 parent d13a260 commit f34013d
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ SOURCES_C += \
$(CORE_DIR)/src/pi/sram.c \
$(CORE_DIR)/src/pi/flashram.c \
$(CORE_DIR)/src/pi/cart_rom.c \
$(CORE_DIR)/src/pi/is_viewer.c
$(CORE_DIR)/src/pi/is_viewer.c \
$(CORE_DIR)/src/pi/summercart.c

# $(CORE_DIR)/src/api/debugger.c \
# $(CORE_DIR)/src/main/ini_reader.c \
Expand Down
10 changes: 5 additions & 5 deletions mupen64plus-core/src/main/rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,20 @@ m64p_error open_rom(const unsigned char* romimage, unsigned int size)
/* Clear Byte-swapped flag, since ROM is now deleted. */
g_MemHasBeenBSwapped = 0;
/* allocate new buffer for ROM and copy into this buffer */
g_rom_size = size;
g_rom = (unsigned char *) malloc(size);
g_rom_size = size > 0x4000000 ? size : 0x4000000;
g_rom = (unsigned char *) malloc(g_rom_size);
alternate_vi_timing = 0;
g_vi_refresh_rate = DEFAULT_COUNT_PER_SCANLINE;
if (g_rom == NULL)
return M64ERR_NO_MEMORY;
memcpy(g_rom, romimage, size);
swap_rom(g_rom, &imagetype, g_rom_size);
swap_rom(g_rom, &imagetype, size);

memcpy(&ROM_HEADER, g_rom, sizeof(m64p_rom_header));

/* Calculate MD5 hash */
md5_init(&state);
md5_append(&state, (const md5_byte_t*)g_rom, g_rom_size);
md5_append(&state, (const md5_byte_t*)g_rom, size);
md5_finish(&state, digest);
for ( i = 0; i < 16; ++i )
sprintf(buffer+i*2, "%02X", digest[i]);
Expand Down Expand Up @@ -413,7 +413,7 @@ m64p_error open_rom(const unsigned char* romimage, unsigned int size)
DebugMessage(M64MSG_INFO, "MD5: %s", ROM_SETTINGS.MD5);
DebugMessage(M64MSG_INFO, "CRC: %x %x", sl(ROM_HEADER.CRC1), sl(ROM_HEADER.CRC2));
DebugMessage(M64MSG_INFO, "Imagetype: %s", buffer);
DebugMessage(M64MSG_INFO, "Rom size: %d bytes (or %d Mb or %d Megabits)", g_rom_size, g_rom_size/1024/1024, g_rom_size/1024/1024*8);
DebugMessage(M64MSG_INFO, "Rom size: %d bytes (or %d Mb or %d Megabits)", size, size/1024/1024, size/1024/1024*8);
DebugMessage(M64MSG_VERBOSE, "ClockRate = %x", sl(ROM_HEADER.ClockRate));
DebugMessage(M64MSG_INFO, "Version: %x", sl(ROM_HEADER.Release));
if(sl(ROM_HEADER.Manufacturer_ID) == 'N')
Expand Down
45 changes: 45 additions & 0 deletions mupen64plus-core/src/memory/m64p_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "../dd/dd_controller.h"

#include "../pi/is_viewer.h"
#include "../pi/summercart.h"

#ifdef DBG
#include "../debugger/dbg_types.h"
Expand Down Expand Up @@ -1046,6 +1047,46 @@ static void write_isvd(void) {
writed(write_is_viewer, NULL, mupencoreaddress, cpu_dword);
}

static void read_screg(void)
{
readw(read_summercart_regs, &g_dev.pi, mupencoreaddress, rdword);
}

static void read_scregb(void)
{
readb(read_summercart_regs, &g_dev.pi, mupencoreaddress, rdword);
}

static void read_scregh(void)
{
readh(read_summercart_regs, &g_dev.pi, mupencoreaddress, rdword);
}

static void read_scregd(void)
{
readd(read_summercart_regs, &g_dev.pi, mupencoreaddress, rdword);
}

static void write_screg(void)
{
writew(write_summercart_regs, &g_dev.pi, mupencoreaddress, cpu_word);
}

static void write_scregb(void)
{
writeb(write_summercart_regs, &g_dev.pi, mupencoreaddress, cpu_byte);
}

static void write_scregh(void)
{
writeh(write_summercart_regs, &g_dev.pi, mupencoreaddress, cpu_hword);
}

static void write_scregd(void)
{
writed(write_summercart_regs, &g_dev.pi, mupencoreaddress, cpu_dword);
}

#ifdef DBG
static int memtype[0x10000];
static void (*saved_readmemb[0x10000])(void);
Expand Down Expand Up @@ -1395,6 +1436,10 @@ void poweron_memory(void)

/* map IS-Viewer */
map_region(0xb3ff, M64P_MEM_NOTHING, RW(isv));

/* map SummerCart64 */
map_region(0x9fff, M64P_MEM_NOTHING, RW(screg));
map_region(0xbfff, M64P_MEM_NOTHING, RW(screg));
}

static void map_region_t(uint16_t region, int type)
Expand Down
97 changes: 95 additions & 2 deletions mupen64plus-core/src/pi/pi_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,71 @@ static void dma_pi_read(struct pi_controller *pi)
dma_write_flashram(pi);
}
}
else if (pi->regs[PI_CART_ADDR_REG] >= 0x10000000
&& pi->regs[PI_CART_ADDR_REG] < 0x14000000)
{
//CART ROM
length = (pi->regs[PI_RD_LEN_REG] & 0xFFFFFF) + 1;
i = (pi->regs[PI_CART_ADDR_REG] - 0x10000000);

length = (i + length) > pi->cart_rom.rom_size ?
(pi->cart_rom.rom_size - i) : length;
length = (pi->regs[PI_DRAM_ADDR_REG] + length) > 0x7FFFFF ?
(0x7FFFFF - pi->regs[PI_DRAM_ADDR_REG]) : length;

if (i > pi->cart_rom.rom_size || pi->regs[PI_DRAM_ADDR_REG] > 0x7FFFFF || !pi->summercart.cfg_rom_write)
{
/* mark both DMA and IO as busy */
pi->regs[PI_STATUS_REG] |=
PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY;

/* schedule end of dma interrupt event */
cp0_update_count();
add_interrupt_event(PI_INT, length / 8);

return;
}

dram_address = pi->regs[PI_DRAM_ADDR_REG];
rom_address = (pi->regs[PI_CART_ADDR_REG] - 0x10000000);
dram = (uint8_t*)pi->ri->rdram.dram;
rom = pi->cart_rom.rom;

for (i = 0; i < length; ++i)
rom[(rom_address + i) ^ S8] = dram[(dram_address + i) ^ S8];
}
else if (pi->regs[PI_CART_ADDR_REG] >= 0x1ffe0000
&& pi->regs[PI_CART_ADDR_REG] < 0x1fff0000)
{
//SC64 BUFFER
length = (pi->regs[PI_RD_LEN_REG] & 0xFFFFFF) + 1;
i = (pi->regs[PI_CART_ADDR_REG] - 0x1ffe0000);

length = (i + length) > 8192 ? (8192 - i) : length;
length = (pi->regs[PI_DRAM_ADDR_REG] + length) > 0x7FFFFF ?
(0x7FFFFF - pi->regs[PI_DRAM_ADDR_REG]) : length;

if (i > 8192 || pi->regs[PI_DRAM_ADDR_REG] > 0x7FFFFF || !pi->summercart.unlock)
{
/* mark both DMA and IO as busy */
pi->regs[PI_STATUS_REG] |=
PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY;

/* schedule end of dma interrupt event */
cp0_update_count();
add_interrupt_event(PI_INT, length / 8);

return;
}

dram_address = pi->regs[PI_DRAM_ADDR_REG];
rom_address = (pi->regs[PI_CART_ADDR_REG] - 0x1ffe0000);
dram = (uint8_t*)pi->ri->rdram.dram;
rom = pi->summercart.buffer;

for (i = 0; i < length; ++i)
rom[(rom_address + i) ^ S8] = dram[(dram_address + i) ^ S8];
}
else
{
DebugMessage(M64MSG_WARNING, "Unknown dma read at 0x%08X in dma_pi_read()", pi->regs[PI_CART_ADDR_REG]);
Expand Down Expand Up @@ -223,7 +288,7 @@ static void dma_pi_write(struct pi_controller *pi)
}

/* XXX: why need special treatment ? */
if (pi->regs[PI_CART_ADDR_REG] >= 0x1fc00000) /* for paper mario */
if (pi->regs[PI_CART_ADDR_REG] >= 0x1fc00000 && pi->regs[PI_CART_ADDR_REG] < 0x1fd00000) /* for paper mario */
{
/* mark DMA as busy */
pi->regs[PI_STATUS_REG] |= PI_STATUS_DMA_BUSY;
Expand Down Expand Up @@ -259,6 +324,33 @@ static void dma_pi_write(struct pi_controller *pi)
dram = (uint8_t*)pi->ri->rdram.dram;
rom = pi->dd_rom.rom;
}
else if (pi->regs[PI_CART_ADDR_REG] >= 0x1ffe0000 && pi->regs[PI_CART_ADDR_REG] < 0x1fff0000)
{
/* SC64 BUFFER */
length = (pi->regs[PI_WR_LEN_REG] & 0xFFFFFE) + 2;
i = (pi->regs[PI_CART_ADDR_REG] - 0x1ffe0000);
length = (i + length) > 8192 ? (8192 - i) : length;
length = (pi->regs[PI_DRAM_ADDR_REG] + length) > 0x7FFFFF ?
(0x7FFFFF - pi->regs[PI_DRAM_ADDR_REG]) : length;

if (i > 8192 || pi->regs[PI_DRAM_ADDR_REG] > 0x7FFFFF || !pi->summercart.unlock)
{
/* mark both DMA and IO as busy */
pi->regs[PI_STATUS_REG] |=
PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY;

/* schedule end of dma interrupt event */
cp0_update_count();
add_interrupt_event(PI_INT, length / 8);

return;
}

dram_address = pi->regs[PI_DRAM_ADDR_REG];
rom_address = (pi->regs[PI_CART_ADDR_REG] - 0x1ffe0000);
dram = (uint8_t*)pi->ri->rdram.dram;
rom = pi->summercart.buffer;
}
else
{
/* CART ROM */
Expand Down Expand Up @@ -328,6 +420,7 @@ void init_pi(struct pi_controller* pi,
init_dd_rom(&pi->dd_rom, ddrom, ddrom_size);
init_flashram(&pi->flashram, flashram_user_data, flashram_save, flashram_data);
init_sram(&pi->sram, sram_user_data, sram_save, sram_data);
init_summercart(&pi->summercart);

pi->use_flashram = 0;

Expand All @@ -344,7 +437,7 @@ void poweron_pi(struct pi_controller* pi)
poweron_dd_rom(&pi->dd_rom);
poweron_flashram(&pi->flashram);
poweron_is_viewer();

poweron_summercart(&pi->summercart);
}

/* Reads a word from the PI MMIO register space. */
Expand Down
2 changes: 2 additions & 0 deletions mupen64plus-core/src/pi/pi_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "flashram.h"
#include "sram.h"
#include "../dd/dd_rom.h"
#include "summercart.h"

#ifndef PI_REG
#define PI_REG(a) ((a & 0xffff) >> 2)
Expand Down Expand Up @@ -63,6 +64,7 @@ struct pi_controller
struct flashram flashram;
struct sram sram;
struct dd_rom dd_rom;
struct summercart summercart;

int use_flashram;

Expand Down
Loading

0 comments on commit f34013d

Please sign in to comment.