Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Retro-Go: Mega Drive in its launcher, at full speed

Mason363ab3aed2566ee
diff --git a/games/retrogo/Makefile b/games/retrogo/Makefile
index 96e3b65c..1158b20d 100644
--- a/games/retrogo/Makefile
+++ b/games/retrogo/Makefile
@@ -44,7 +44,9 @@ LDFLAGS += -Wl,--gc-sections -flinker-output=nolto-rel
CORE_O = $(patsubst src/%.c,$(BUILD_DIR)/%.o,$(CORE_SRC))
APP_O = $(patsubst src/%.c,$(BUILD_DIR)/%.o,$(APP_SRC))
-MOD_CORE_O = $(patsubst src/%.c,$(BUILD_DIR)/mod/%.o,$(CORE_SRC))
+# (inside NumPlay, no Mega Drive: the RAM it needs isn't there, main.c's HAS_MD)
+MOD_CORE_SRC = $(filter-out $(MD_SRC),$(CORE_SRC))
+MOD_CORE_O = $(patsubst src/%.c,$(BUILD_DIR)/mod/%.o,$(MOD_CORE_SRC))
MOD_APP_O = $(patsubst src/%.c,$(BUILD_DIR)/mod/%.o,$(APP_SRC))
.PHONY: build check clean test module sim-module FORCE
@@ -106,10 +108,10 @@ $(BUILD_DIR)/module.o: $(MOD_CORE_O) $(MOD_APP_O)
SIM_CFLAGS = -std=gnu11 -Isrc/nes -O2 -fPIC $(shell $(NWLINK) eadk-cflags-simulator) -DNP_MODULE=1 -w
sim-module: $(BUILD_DIR)/sim-module.o
-$(BUILD_DIR)/sim-module.o: $(CORE_SRC) $(APP_SRC) $(HEADERS)
+$(BUILD_DIR)/sim-module.o: $(MOD_CORE_SRC) $(APP_SRC) $(HEADERS)
$(Q) mkdir -p $(BUILD_DIR)/sim
$(Q) cc $(SIM_CFLAGS) -Dmain=np_retrogo_main -c src/main.c -o $(BUILD_DIR)/sim/main.o
- $(Q) for f in $(CORE_SRC) src/romtable.c src/pack.c; do cc $(SIM_CFLAGS) -c $$f -o $(BUILD_DIR)/sim/$$(echo $$f | tr / _).o || exit 1; done
+ $(Q) for f in $(MOD_CORE_SRC) src/romtable.c src/pack.c; do cc $(SIM_CFLAGS) -c $$f -o $(BUILD_DIR)/sim/$$(echo $$f | tr / _).o || exit 1; done
$(Q) $(PY) ../../tools/romtable.py --c $(BUILD_DIR)/sim/roms.c $(ROMS) && cc $(SIM_CFLAGS) -c $(BUILD_DIR)/sim/roms.c -o $(BUILD_DIR)/sim/roms.o
$(Q) ld -r $(BUILD_DIR)/sim/*.o -exported_symbol _np_retrogo_main -o $@
diff --git a/games/retrogo/src/main.c b/games/retrogo/src/main.c
index b38d86d6..c3a8a320 100644
--- a/games/retrogo/src/main.c
+++ b/games/retrogo/src/main.c
@@ -25,6 +25,7 @@
#include "nes/nes_core.h"
#include "sms/sms.h"
#include "gw/gw.h"
+#include "md/md.h"
#if NP_MODULE /* (np_file_read, np_file_write, np_storage_delete: NumPlay's, launcher/src/np.h) */
#define np_app_begin()
@@ -63,12 +64,19 @@ extern const uint32_t np_roms_size;
#endif
#define KEY(k) (1ull << (k))
+/* the Mega Drive: in RetroGo.nwa on its own and the other calculators (inside NumPlay, the RAM it
+ needs isn't there) */
+#if NP_MODULE
+#define HAS_MD 0
+#else
+#define HAS_MD 1
+#endif
#define WHITE 0xFFFF
#define BLACK 0x0000
/* ------------------------------------------------------------ memory */
static uint8_t heap[RG_HEAP_SIZE] __attribute__((aligned(8)));
-static uint32_t heap_used;
+static uint32_t heap_used, room_used;
bool rg_drawing = true;
void *rg_alloc(uint32_t size) {
@@ -79,7 +87,34 @@ void *rg_alloc(uint32_t size) {
memset(p, 0, size);
return p;
}
-void rg_alloc_reset(void) { heap_used = 0; }
+void rg_alloc_reset(void) { heap_used = 0, room_used = 0; }
+
+/* The Mega Drive's RAMs, in pages of 2 KB: from the heap, then from the RAM the calculator has
+ after the app's own (the most on Epsilon 23 and newer), then from a buffer on main's stack. */
+#if HAS_MD
+#if PLATFORM_DEVICE
+extern char _heap_start[], _heap_end[];
+#define MD_STACK_PAGES 13 /* (on main's stack: 26 KB of its 32) */
+#else
+#define MD_STACK_PAGES MD_PAGES
+#endif
+static uint8_t *stack_pages;
+void *rg_alloc_page(void) {
+ void *p = rg_alloc(MD_PAGE);
+ if (p) return p;
+ uint8_t *start, *end;
+#if PLATFORM_DEVICE
+ start = (uint8_t *)(((uintptr_t)_heap_start + 7) & ~(uintptr_t)7), end = (uint8_t *)_heap_end;
+#else
+ start = end = NULL;
+#endif
+ uint32_t after = end > start ? (uint32_t)(end - start) / MD_PAGE : 0;
+ if (room_used < after) return memset(start + MD_PAGE * room_used++, 0, MD_PAGE);
+ if (room_used < after + MD_STACK_PAGES && stack_pages)
+ return memset(stack_pages + MD_PAGE * (room_used++ - after), 0, MD_PAGE);
+ return NULL;
+}
+#endif
/* one strip of the screen: the launcher's drawing functions draw there, and the emulators'
pictures go through it */
@@ -329,6 +364,8 @@ static void set_view(void) {
sms_view(&sx, &sy, &sw, &sh); /* (the Game Gear's screen, or the taller pictures of some games) */
} else if (sys_now == RG_SYS_GW) {
sw = GW_WIDTH, sh = GW_HEIGHT;
+ } else if (sys_now == RG_SYS_MD) {
+ md_view(&sx, &sy, &sw, &sh); /* (256 or 320 wide, 224 or 240 high, as the game sets it) */
}
view.src_x = sx, view.src_y = sy, view.src_w = sw, view.src_h = sh;
int w = sw, h = sh;
@@ -780,18 +817,30 @@ static uint32_t autosave_at; /* when the battery save is due, 0 none */
/* the emulators: gnuboy, nofrendo, the Master System's (for the Game Gear, the SG-1000 and the
ColecoVision too) and LCD-Game-Emulator */
-enum { CORE_GB, CORE_NES, CORE_SMS, CORE_GW };
+enum { CORE_GB, CORE_NES, CORE_SMS, CORE_GW, CORE_MD };
+#if !HAS_MD /* (stand-ins: never called) */
+#define md_load(d, n) RG_ERR_MEMORY
+#define md_state_chunks(c) 0
+#define md_state_loaded()
+#define md_redraw()
+#define md_reset(h)
+#define md_view(x, y, w, h)
+#define md_frame_us() 16667
+#define md_set_pad(p)
+#define md_set_z80(z)
+#define md_run_frame(d)
+#endif
static int core;
static bool is_gb(void) { return core == CORE_GB; }
static int core_chunks(rg_chunk_t *c) {
return core == CORE_GB ? gb_state_chunks(c) : core == CORE_NES ? nes_state_chunks(c) : core == CORE_GW ? gw_state_chunks(c)
- : sms_state_chunks(c);
+ : core == CORE_MD ? md_state_chunks(c) : sms_state_chunks(c);
}
/* the battery RAM, NULL if none (a Sega cartridge's only once the game has turned it on) */
static uint8_t *core_sram(uint32_t *size) {
- if (core == CORE_GW) return *size = 0, NULL; /* (no battery RAM: the state is the save) */
+ if (core == CORE_GW || core == CORE_MD) return *size = 0, NULL; /* (no battery RAM: the state is the save) */
if (core == CORE_SMS) return sms_sram(size);
return core == CORE_GB ? gb_sram(size) : nes_sram(size);
}
@@ -810,6 +859,7 @@ static void core_redraw(void) {
if (core == CORE_GB) gb_redraw();
else if (core == CORE_NES) nes_redraw();
else if (core == CORE_GW) gw_redraw();
+ else if (core == CORE_MD) md_redraw();
else sms_redraw();
flush();
redraw_all = false;
@@ -888,6 +938,7 @@ static bool state_load(int slot) {
if (core == CORE_GB) gb_state_loaded();
else if (core == CORE_NES) nes_state_loaded();
else if (core == CORE_GW) gw_state_loaded();
+ else if (core == CORE_MD) md_state_loaded();
else sms_state_loaded();
last_slot = session_slot = slot, skip = 0;
return true;
@@ -903,11 +954,13 @@ static void core_reset(bool hard) {
if (core == CORE_GB) gb_reset(hard);
else if (core == CORE_NES) nes_reset_machine(hard);
else if (core == CORE_GW) gw_reset(hard); /* (soft: the ACL button) */
+ else if (core == CORE_MD) md_reset(hard);
else sms_reset(hard);
skip = 0;
}
static void apply_settings(void) {
+ if (core == CORE_MD) md_set_z80(cfg.md_z80);
if (core == CORE_GB) gb_set_palette(cfg.gb_palette);
else if (core == CORE_NES) nes_set_palette(cfg.nes_palette), nes_set_sprite_limit(cfg.sprite_limit);
}
@@ -927,6 +980,10 @@ static const char *button_name(int k) {
static const char *const cv[] = {T("Joystick"), T("Left button"), T("Right button"), NULL, NULL};
return cv[k];
}
+ if (sys_now == RG_SYS_MD) { /* (Retro-Go's: its A is the pad's C, Select its A) */
+ static const char *const md[] = {T("D-pad"), "C", "B", "A", T("Start")};
+ return md[k];
+ }
if (sys_now == RG_SYS_GW) { /* (Retro-Go's: Select is Time, Start is Game) */
static const char *const gw[] = {T("D-pad"), "A", "B", T("Time"), T("Game A")};
return gw[k];
@@ -1111,6 +1168,11 @@ static int sprite_limit_cb(item_t *it, int ev) {
strcpy(it->value, cfg.sprite_limit ? T("On") : T("Off"));
return CB_NONE;
}
+static int md_z80_cb(item_t *it, int ev) {
+ if (ev == EV_PREV || ev == EV_NEXT || ev == EV_ENTER) cfg.md_z80 ^= 1, apply_settings();
+ strcpy(it->value, cfg.md_z80 ? T("On") : T("Off"));
+ return CB_NONE;
+}
static int emulator_options_cb(item_t *it, int ev) {
if (ev != EV_ENTER) return CB_NONE;
int r;
@@ -1124,6 +1186,9 @@ static int emulator_options_cb(item_t *it, int ev) {
OPTION(T("Crop sides"), crop_cb, 0), OPTION(T("Sprite limit"), sprite_limit_cb, 0),
OPTION(T("SRAM autosave"), autosave_cb, 0)};
r = dialog_run(it->label, items, 5, 0);
+ } else if (core == CORE_MD) {
+ item_t items[] = {OPTION(T("Z80 emulation"), md_z80_cb, 0)};
+ r = dialog_run(it->label, items, 1, 0);
} else {
item_t items[] = {OPTION(T("SRAM autosave"), autosave_cb, 0)};
r = dialog_run(it->label, items, 1, 0);
@@ -1199,10 +1264,10 @@ static int about(void) {
item_t items[] = {
{0, T("Retro-Go"), "ducalex", IT_MESSAGE, 1, NULL, 0}, {0, "Game Boy", "gnuboy", IT_MESSAGE, 1, NULL, 0},
{0, "NES", "nofrendo", IT_MESSAGE, 1, NULL, 0}, {0, "SMS, GG, SG, COL", T("z80 by superzazu"), IT_MESSAGE, 1, NULL, 0},
- {0, "Game & Watch", "LCD-Game-Emulator", IT_MESSAGE, 1, NULL, 0},
+ {0, "Game & Watch", "LCD-Game-Emulator", IT_MESSAGE, 1, NULL, 0}, {0, "Mega Drive", "gwenesis", IT_MESSAGE, 1, NULL, 0},
{0, T("Port"), "NumPlay", IT_MESSAGE, 1, NULL, 0}, MESSAGE(T("Not affiliated with Retro-Go")),
ITEM(1, T("OK"))};
- return dialog_run(T("About Retro-Go"), items, 8, 7);
+ return dialog_run(T("About Retro-Go"), items, 9, 8);
}
/* Retro-Go's save slots: 0 to 3; the slot, DLG_BACK or DLG_HOME */
@@ -1300,12 +1365,14 @@ uint32_t perf_frames, perf_drawn;
static int play(const rg_rom_t *r, int slot) {
sys_now = r->system;
- core = sys_now == RG_SYS_NES ? CORE_NES : is_z80(sys_now) ? CORE_SMS : sys_now == RG_SYS_GW ? CORE_GW : CORE_GB;
+ core = sys_now == RG_SYS_NES ? CORE_NES : is_z80(sys_now) ? CORE_SMS : sys_now == RG_SYS_GW ? CORE_GW
+ : sys_now == RG_SYS_MD ? CORE_MD : CORE_GB;
game = r;
game_crc = rom_crc(r);
int err = core == CORE_GB ? gb_load(r->data, r->size)
: core == CORE_NES ? nes_load(r->data, r->size)
: core == CORE_GW ? gw_load(r->data, r->size)
+ : core == CORE_MD ? md_load(r->data, r->size)
: sms_load(r->data, r->size, game_crc,
sys_now == RG_SYS_GG ? SMS_GG
: sys_now == RG_SYS_SG ? SMS_SG
@@ -1313,7 +1380,8 @@ static int play(const rg_rom_t *r, int slot) {
: SMS_SMS);
if (err != RG_OK) {
game = NULL;
- const char *why = err == RG_ERR_MAPPER ? T("This cartridge type isn't supported.")
+ const char *why = core == CORE_MD && !HAS_MD ? T("Mega Drive games play in Retro-Go on its own.")
+ : err == RG_ERR_MAPPER ? T("This cartridge type isn't supported.")
: err == RG_ERR_MEMORY ? T("This game needs more memory than the calculator has.")
: T("This file isn't a game Retro-Go can read.");
return alert(T("Can't start the game"), why) == DLG_HOME ? GAME_EXIT : GAME_PICKER;
@@ -1337,7 +1405,8 @@ static int play(const rg_rom_t *r, int slot) {
/* a frame: the Game Boy's 70224 clocks at 4.194304 MHz, the NES's 29780.5 at 1.789773 MHz, the
Master System's 59736 at 3.579545 MHz (or 50 a second) */
- const uint32_t frame_us = core == CORE_GB ? 16742 : core == CORE_NES ? 16639 : core == CORE_GW ? 15625 : sms_frame_us();
+ uint32_t frame_us = core == CORE_GB ? 16742 : core == CORE_NES ? 16639 : core == CORE_GW ? 15625
+ : core == CORE_MD ? md_frame_us() : sms_frame_us();
bool acl_held = false;
uint32_t next_us = millis() * 1000u, second = millis(), frames = 0, busy = 0;
int result = GAME_ON;
@@ -1387,12 +1456,18 @@ static int play(const rg_rom_t *r, int slot) {
if (core == CORE_GB) gb_set_pad(pad), gb_run_frame(draw);
else if (core == CORE_NES) nes_set_pad(pad), nes_run_frame(draw);
else if (core == CORE_GW) gw_set_pad(pad, gw_keys_held), gw_run_frame(draw);
+ else if (core == CORE_MD) md_set_pad(pad), md_run_frame(draw);
else sms_set_pad(pad), sms_run_frame(draw);
if (draw) flush(), redraw_all = false, perf_drawn++;
if (core == CORE_SMS) { /* (a game that changes the picture's height) */
int x, y, w, h;
sms_view(&x, &y, &w, &h);
if (y != view.src_y || h != view.src_h) set_view(), game_screen();
+ } else if (core == CORE_MD) { /* (or its width, or goes to 50 frames a second) */
+ int x, y, w, h;
+ md_view(&x, &y, &w, &h);
+ if (w != view.src_w || h != view.src_h) set_view(), game_screen();
+ frame_us = md_frame_us();
}
perf_frames++, frames++;
uint32_t elapsed = millis() - t0;
@@ -1442,11 +1517,11 @@ static void picker_scene(void *ctx) {
#ifdef NP_PORT
const char *const lines[] = {T("Welcome to Retro-Go!"), "", "Add games to this folder:", rg_port_folder_shown(),
T("Game Boy, Game Boy Color, NES,"), T("Master System, Game Gear, SG-1000,"),
- T("ColecoVision, Game & Watch."), T("Home: quit")};
+ T("ColecoVision, Game & Watch, Mega Drive."), T("Home: quit")};
#else
static const char *const lines[] = {T("Welcome to Retro-Go!"), "", T("Add games on NumPlay's"),
T("customizer website:"), T("Game Boy, Game Boy Color, NES,"), T("Master System, Game Gear, SG-1000,"),
- T("ColecoVision, Game & Watch."), T("Home: quit")};
+ T("ColecoVision, Game & Watch, Mega Drive."), T("Home: quit")};
#endif
for (int i = 0; i < 8; i++) gfx_text(&np_font_body, 8, 80 + 18 * i, lines[i], i ? 0xAD55 : WHITE, 256);
return;
@@ -1549,6 +1624,10 @@ static int file_menu(const rg_rom_t *r) {
static void launcher_menu_redraw(void) { picker_draw(); }
int main(void) {
+#if HAS_MD
+ uint64_t md_pages[MD_STACK_PAGES * MD_PAGE / 8]; /* (the Mega Drive's last RAM pages, rg_alloc_page) */
+ stack_pages = (uint8_t *)md_pages;
+#endif
np_app_begin();
color_t *their_strip = gfx_buf;
gfx_buf = strip;
diff --git a/games/retrogo/src/md/gwenesis_bus.c b/games/retrogo/src/md/gwenesis_bus.c
index 6889ddae..36f4a96a 100644
--- a/games/retrogo/src/md/gwenesis_bus.c
+++ b/games/retrogo/src/md/gwenesis_bus.c
@@ -42,9 +42,9 @@ __license__ = "GPLv3"
#define gwenesis_SN76489_Reset() ((void)0)
#define printf(...) ((void)0)
#define vfprintf(...) ((void)0)
-/* (the Z80's 8 KB of RAM in two pages of 4 KB, md.c) */
-extern unsigned char *md_zram[2];
-#define ZRAM_AT(a) (md_zram[((a) >> 12) & 1][(a) & 0xFFF])
+/* (the Z80's 8 KB of RAM in four pages of 2 KB, md.c) */
+extern unsigned char *md_zram[4];
+#define ZRAM_AT(a) (md_zram[((a) >> 11) & 3][(a) & 0x7FF])
#if GNW_TARGET_MARIO !=0 || GNW_TARGET_ZELDA!=0
#pragma GCC optimize("Ofast")
diff --git a/games/retrogo/src/md/gwenesis_vdp_gfx.c b/games/retrogo/src/md/gwenesis_vdp_gfx.c
index 4b25f0de..323df162 100644
--- a/games/retrogo/src/md/gwenesis_vdp_gfx.c
+++ b/games/retrogo/src/md/gwenesis_vdp_gfx.c
@@ -138,12 +138,6 @@ void gwenesis_vdp_set_buffers(unsigned char *screen_buffer, unsigned char *scale
screen = screen_buffer;
scaled_screen = scaled_buffer;
}
-//embedded
-void gwenesis_vdp_set_buffer(unsigned short *ptr_screen_buffer)
-{
- screen_buffer_line = ptr_screen_buffer;
- screen_buffer = ptr_screen_buffer;
-}
/******************************************************************************
*
diff --git a/games/retrogo/src/md/m68k.h b/games/retrogo/src/md/m68k.h
index f400d3db..524c6e1a 100644
--- a/games/retrogo/src/md/m68k.h
+++ b/games/retrogo/src/md/m68k.h
@@ -158,8 +158,8 @@
#else
extern unsigned char *ROM_DATA;
- /* (NumPlay: the 68000's 64 KB of RAM in 16 pages of 4 KB, wherever the calculator has room) */
- extern unsigned char *md_ram[16];
+ /* (NumPlay: the 68000's 64 KB of RAM in 32 pages of 2 KB, wherever the calculator has room) */
+ extern unsigned char *md_ram[32];
#endif
/* (NumPlay: nothing read past the ROM's size rounded up to a power of two) */
@@ -181,12 +181,12 @@ extern unsigned int md_rom_mask;
#else
/* (the words swapped as gwenesis keeps them; a long word is two words, maybe in two pages) */
-#define MD_RAM16(A) (*(unsigned short *)&md_ram[((A) >> 12) & 15][(A) & 0xFFE])
-#define FETCH8RAM(A) (md_ram[((A) >> 12) & 15][((A) ^ 1) & 0xFFF])
+#define MD_RAM16(A) (*(unsigned short *)&md_ram[((A) >> 11) & 31][(A) & 0x7FE])
+#define FETCH8RAM(A) (md_ram[((A) >> 11) & 31][((A) ^ 1) & 0x7FF])
#define FETCH16RAM(A) (MD_RAM16(A))
#define FETCH32RAM(A) (((unsigned int)MD_RAM16(A) << 16) | MD_RAM16((A) + 2))
-#define WRITE8RAM(A, V) (md_ram[((A) >> 12) & 15][((A) ^ 1) & 0xFFF] = (V))
+#define WRITE8RAM(A, V) (md_ram[((A) >> 11) & 31][((A) ^ 1) & 0x7FF] = (V))
#define WRITE16RAM(A, V) (MD_RAM16(A) = (unsigned short)(V))
#define WRITE32RAM(A, V) (MD_RAM16(A) = (unsigned short)((V) >> 16), MD_RAM16((A) + 2) = (unsigned short)(V))
@@ -317,7 +317,7 @@ typedef struct
typedef struct
{
- cpu_memory_map memory_map[256]; /* memory mapping */
+ /* (NumPlay: no memory_map[256], gwenesis reads its memory through its bus: 5 KB of RAM) */
cpu_idle_t poll; /* polling detection */
diff --git a/games/retrogo/src/md/m68kcpu.h b/games/retrogo/src/md/m68kcpu.h
index 634e5004..9186324b 100644
--- a/games/retrogo/src/md/m68kcpu.h
+++ b/games/retrogo/src/md/m68kcpu.h
@@ -11,6 +11,14 @@
#if M68K_EMULATE_ADDRESS_ERROR
#include <setjmp.h>
+/* (NumPlay: on the calculator GCC's own, without the C library's and the unwinding it brings) */
+#if defined(__arm__) && defined(__GNUC__) && !defined(__clang__)
+#define M68K_SETJMP __builtin_setjmp
+#define M68K_LONGJMP __builtin_longjmp
+#else
+#define M68K_SETJMP setjmp
+#define M68K_LONGJMP longjmp
+#endif
#endif /* M68K_EMULATE_ADDRESS_ERROR */
#include "m68k.h"
@@ -312,7 +320,7 @@
/* Enable or disable Address error emulation */
#if M68K_EMULATE_ADDRESS_ERROR
#define m68ki_set_address_error_trap() \
- if(setjmp(m68ki_cpu.aerr_trap) != 0) \
+ if(M68K_SETJMP(m68ki_cpu.aerr_trap) != 0) \
{ \
m68ki_exception_address_error(); \
}
@@ -325,7 +333,7 @@
m68ki_cpu.aerr_address = ADDR; \
m68ki_cpu.aerr_write_mode = WRITE_MODE; \
m68ki_cpu.aerr_fc = FC; \
- longjmp(m68ki_cpu.aerr_trap, 1); \
+ M68K_LONGJMP(m68ki_cpu.aerr_trap, 1); \
} \
}
#else
diff --git a/games/retrogo/src/md/md.c b/games/retrogo/src/md/md.c
index 718acb42..a7b37fd2 100644
--- a/games/retrogo/src/md/md.c
+++ b/games/retrogo/src/md/md.c
@@ -16,8 +16,8 @@
#include "gwenesis_savestate.h"
#include "z80inst.h"
-unsigned char *VRAM; /* (gwenesis_vdp_mem.c's) */
-unsigned char *md_ram[16], *md_zram[2];
+extern unsigned char *VRAM; /* (gwenesis_vdp_mem.c) */
+unsigned char *md_ram[32], *md_zram[4];
extern unsigned char gwenesis_vdp_regs[];
extern unsigned short gwenesis_vdp_status;
extern int screen_width, screen_height, hint_pending;
@@ -32,7 +32,7 @@ int system_clock, scan_line; /* (the frame's master clock and line, as gwenesis'
static bool z80_on = true;
/* ---- the state: the parts gwenesis saves by name, here one after another in a block */
-#define MISC_SIZE 3072
+#define MISC_SIZE 2304
static uint8_t *misc;
static uint32_t misc_at, misc_size;
struct SaveState { int unused; };
@@ -69,8 +69,8 @@ static void save_misc(void) {
}
static void clear_rams(void) {
- for (int i = 0; i < 16; i++) memset(md_ram[i], 0, MD_PAGE);
- for (int i = 0; i < 2; i++) memset(md_zram[i], 0, MD_PAGE);
+ for (int i = 0; i < 32; i++) memset(md_ram[i], 0, MD_PAGE);
+ for (int i = 0; i < 4; i++) memset(md_zram[i], 0, MD_PAGE);
}
int md_load(const uint8_t *data, uint32_t size) {
@@ -79,9 +79,9 @@ int md_load(const uint8_t *data, uint32_t size) {
VRAM = rg_alloc(0x10000);
misc = rg_alloc(MISC_SIZE);
if (!VRAM || !misc) return RG_ERR_MEMORY;
- for (int i = 0; i < 16; i++)
+ for (int i = 0; i < 32; i++)
if (!(md_ram[i] = rg_alloc_page())) return RG_ERR_MEMORY;
- for (int i = 0; i < 2; i++)
+ for (int i = 0; i < 4; i++)
if (!(md_zram[i] = rg_alloc_page())) return RG_ERR_MEMORY;
clear_rams();
load_cartridge((unsigned char *)(uintptr_t)data, size);
@@ -176,8 +176,8 @@ int md_state_chunks(rg_chunk_t *out) {
out[n++] = (rg_chunk_t){misc, misc_size};
out[n++] = (rg_chunk_t){VRAM, 0x8000};
out[n++] = (rg_chunk_t){VRAM + 0x8000, 0x8000};
- for (int i = 0; i < 16; i++) out[n++] = (rg_chunk_t){md_ram[i], MD_PAGE};
- for (int i = 0; i < 2; i++) out[n++] = (rg_chunk_t){md_zram[i], MD_PAGE};
+ for (int i = 0; i < 32; i++) out[n++] = (rg_chunk_t){md_ram[i], MD_PAGE};
+ for (int i = 0; i < 4; i++) out[n++] = (rg_chunk_t){md_zram[i], MD_PAGE};
return n;
}
diff --git a/games/retrogo/src/md/md.h b/games/retrogo/src/md/md.h
index 7c63ef17..61477eec 100644
--- a/games/retrogo/src/md/md.h
+++ b/games/retrogo/src/md/md.h
@@ -9,7 +9,8 @@
/* the RAMs (64 KB for the 68000, 8 KB for the Z80) in pages of MD_PAGE bytes, from rg_alloc_page
(main.c: the heap, then the RAM after the app's own, then a buffer on main's stack) */
-#define MD_PAGE 4096
+#define MD_PAGE 2048
+#define MD_PAGES (32 + 4)
void *rg_alloc_page(void);
/* a game read in place, plain and its bytes swapped in pairs (tools/romtable.py). RG_OK or an
diff --git a/games/retrogo/src/md/md_z80.c b/games/retrogo/src/md/md_z80.c
index 51f9be97..75c56fa6 100644
--- a/games/retrogo/src/md/md_z80.c
+++ b/games/retrogo/src/md/md_z80.c
@@ -34,8 +34,8 @@ uint8_t md_z80_read(uint16_t addr);
#include "gwenesis_bus.h"
#include "gwenesis_savestate.h"
-extern unsigned char *md_zram[2];
-#define ZRAM_AT(a) (md_zram[((a) >> 12) & 1][(a) & 0xFFF])
+extern unsigned char *md_zram[4];
+#define ZRAM_AT(a) (md_zram[((a) >> 11) & 3][(a) & 0x7FF])
/* the machine around it, saved with it */
static struct {
diff --git a/games/retrogo/src/nes/nes.c b/games/retrogo/src/nes/nes.c
index 76868d85..3e610fc7 100644
--- a/games/retrogo/src/nes/nes.c
+++ b/games/retrogo/src/nes/nes.c
@@ -354,7 +354,7 @@ int nes_load(const uint8_t *data, uint32_t size)
r->sram = rg_alloc(0x2000);
r->vram = r->vrom_banks ? NULL : rg_alloc(0x2000);
nes6502_null_page = rg_alloc(NES6502_BANKSIZE);
- if (!ram || !r->sram || (!r->vrom_banks && !r->vram) || !nes6502_null_page)
+ if (!ram || !r->sram || (!r->vrom_banks && !r->vram) || !nes6502_null_page || !nes_ppu_alloc())
return RG_ERR_MEMORY;
if (r->flags & ROM_FLAG_TRAINER)
memcpy(r->sram + 0x1000, data + 16, 512);
diff --git a/games/retrogo/src/nes/nes_ppu.c b/games/retrogo/src/nes/nes_ppu.c
index 3990bb12..b82a268c 100644
--- a/games/retrogo/src/nes/nes_ppu.c
+++ b/games/retrogo/src/nes/nes_ppu.c
@@ -47,7 +47,7 @@
#define FULLBG (ppu.palette[0] | BG_TRANS)
/* the NES PPU */
-ppu_t ppu;
+ppu_t *nes_ppu_p;
bool ppu_limit_sprites = true;
bool ppu_pages_changed = true; /* (since the last line drawn: for the lines kept for the menus) */
@@ -873,11 +873,19 @@ const uint16 *nes_palette565 = NULL;
controls and the CHR and nametable banks (a few sets a frame, for the games that switch them
for a status bar) */
#define CHR_SETS 4
-static struct
+typedef struct
{
uint16 vaddr;
uint8 xofs, ctrl0, ctrl1, set, drawn;
-} snap[NES_SCREEN_HEIGHT];
+} snap_t;
+static snap_t *snap; /* (NES_SCREEN_HEIGHT of them, in Retro-Go's heap) */
+
+bool nes_ppu_alloc(void)
+{
+ nes_ppu_p = rg_alloc(sizeof(ppu_t));
+ snap = rg_alloc(sizeof(snap_t) * NES_SCREEN_HEIGHT);
+ return nes_ppu_p && snap;
+}
static uint8 *chr_sets[CHR_SETS][12];
static int chr_used;
@@ -887,7 +895,7 @@ static void ppu_snapshot(int scanline)
if (0 == scanline)
{
chr_used = 0;
- memset(snap, 0, sizeof snap);
+ memset(snap, 0, sizeof(snap_t) * NES_SCREEN_HEIGHT);
}
/* (most lines have the banks of the line before) */
i = scanline && snap[scanline - 1].drawn ? snap[scanline - 1].set : chr_used;
diff --git a/games/retrogo/src/nes/nes_ppu.h b/games/retrogo/src/nes/nes_ppu.h
index 5c819179..bbf095d7 100644
--- a/games/retrogo/src/nes/nes_ppu.h
+++ b/games/retrogo/src/nes/nes_ppu.h
@@ -108,7 +108,12 @@ typedef struct ppu_s
bool vram_present;
} ppu_t;
-extern ppu_t ppu;
+/* (Retro-Go on NumPlay: in Retro-Go's heap, nes_ppu_alloc, not in the app's RAM: the
+ Mega Drive takes it when it runs) */
+extern ppu_t *nes_ppu_p;
+#define ppu (*nes_ppu_p)
+/* the PPU's memory taken from Retro-Go's heap; false if it is full */
+bool nes_ppu_alloc(void);
extern bool ppu_pages_changed;
extern bool ppu_limit_sprites; /* 8 sprites a line at most, as the NES (an option) */
extern const uint16 *nes_palette565; /* the 64 NES colours in RGB565 */
diff --git a/games/retrogo/src/rg.h b/games/retrogo/src/rg.h
index 270b6dfe..f33407f3 100644
--- a/games/retrogo/src/rg.h
+++ b/games/retrogo/src/rg.h
@@ -23,7 +23,7 @@ typedef struct {
void *at;
uint32_t size;
} rg_chunk_t;
-#define RG_MAX_CHUNKS 24 /* (the Mega Drive: 21) */
+#define RG_MAX_CHUNKS 40 /* (the Mega Drive: 39) */
/* The cores' failures when loading a game, shown by the screens. */
enum {
diff --git a/games/retrogo/tests/host_core.c b/games/retrogo/tests/host_core.c
index c6a8655e..dc15f605 100644
--- a/games/retrogo/tests/host_core.c
+++ b/games/retrogo/tests/host_core.c
@@ -34,12 +34,12 @@ void *rg_alloc(uint32_t size) {
}
void rg_alloc_reset(void) { used = 0, pages_used = 0; }
/* (the Mega Drive's RAM pages: on the calculator from three places, here from a block of their own) */
-static uint8_t pages[18 * 4096] __attribute__((aligned(8)));
+static uint8_t pages[MD_PAGES * MD_PAGE] __attribute__((aligned(8)));
void *rg_alloc_page(void) {
- if (used + 4096 <= sizeof heap) return rg_alloc(4096);
- if (pages_used >= 18) return NULL;
- memset(pages + pages_used * 4096, 0, 4096);
- return pages + 4096 * pages_used++;
+ if (used + MD_PAGE <= sizeof heap) return rg_alloc(MD_PAGE);
+ if (pages_used >= MD_PAGES) return NULL;
+ memset(pages + pages_used * MD_PAGE, 0, MD_PAGE);
+ return pages + MD_PAGE * pages_used++;
}
void rg_line(int y, const uint16_t *px) {
@@ -161,7 +161,7 @@ int main(int argc, char **argv) {
if (getenv("STATE")) {
rg_chunk_t parts[RG_MAX_CHUNKS];
int n = chunks(parts);
- static uint8_t copy[RG_HEAP_SIZE + 18 * 4096 + 65536];
+ static uint8_t copy[RG_HEAP_SIZE + MD_PAGES * MD_PAGE + 65536];
uint32_t at = 0;
for (int i = 0; i < n; i++) memcpy(copy + at, parts[i].at, parts[i].size), at += parts[i].size;
static uint16_t first[FW * 240];