From ae22606f8158821e678f9d50382051fe2adc3aad Mon Sep 17 00:00:00 2001 From: Nathaniel Walizer Date: Wed, 19 Mar 2025 10:47:23 -0700 Subject: [PATCH] ARM/STM32 Compatability improvements - Rename DEBUG to NESE_DEBUG - Split out allocators for internal RAM - SDL flushing (GPU compat) - Porting notes --- src/cart.c | 7 ++++--- src/f6502.c | 2 +- src/linux/port.c | 30 ++++++++++++++++++++++++++++++ src/log.h | 8 ++++---- src/map/map001.c | 2 +- src/map/map004.c | 2 +- src/nes.c | 2 +- src/port.h | 4 ++++ src/ppu.c | 2 +- 9 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/cart.c b/src/cart.c index f0c14da..598b5b4 100644 --- a/src/cart.c +++ b/src/cart.c @@ -2,10 +2,11 @@ #include #include "cart.h" +#include "port.h" #ifdef DEBUG_CART -#define DEBUG "Cart" +#define NESE_DEBUG "Cart" #endif #include "log.h" @@ -64,7 +65,7 @@ int nes_cart_load_mem(void* data, int size, nes* sys) { if (hdr->chr_size_lsb <= 0) { int chr_size = mapper->max_chr_banks * NES_CHR_ROM_PAGE_SIZE; - mem->ppu.chr_ram = calloc(1, chr_size); + mem->ppu.chr_ram = nese_alloc_gpu(chr_size); LOGI("CHR RAM: %d kB", chr_size / 1024); mem->ppu.chr = mem->ppu.chr_ram; mem->ppu.n_chr_banks = chr_size / @@ -75,7 +76,7 @@ int nes_cart_load_mem(void* data, int size, nes* sys) { mem->mapper = *mapper; mem->mapper.irq_arg = &sys->core; mem->mapper.irq_callback = (void(*)(void*, bool))f6502_set_IRQ; - mem->mapper.data = calloc(1, mapper->data_size); + mem->mapper.data = nese_alloc(mapper->data_size); if (NULL == mem->mapper.data) { LOGE("Failed to allocate mapper data"); status = -1; diff --git a/src/f6502.c b/src/f6502.c index 0751de7..fd27754 100644 --- a/src/f6502.c +++ b/src/f6502.c @@ -16,7 +16,7 @@ (type *)((char *)__mptr - offsetof(type,member));}) #endif -//#define DEBUG "Core" +//#define NESE_DEBUG "Core" #include "log.h" diff --git a/src/linux/port.c b/src/linux/port.c index 1064d7d..98b7139 100644 --- a/src/linux/port.c +++ b/src/linux/port.c @@ -15,6 +15,10 @@ #include "log.h" +/* OS-specific file operations + * Memory mapping specifically needs to be ported for each OS + */ + static void* nese_map_file(FILE* file, int size) { void* addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(file), 0); @@ -38,6 +42,29 @@ static int nese_file_size(FILE* file) { } +/* Platform-specific allocation + * GPU and CPU refer to emulator memory spaces + * Note: GPU (and possibly CPU) regions may need + * to be placed into internal ram for performance + */ + +void* nese_alloc_gpu(int size) { + return calloc(1, size); +} + +void* nese_alloc_cpu(int size) { + return calloc(1, size); +} + +void* nese_alloc(int size) { + return calloc(1, size); +} + + +/* SDL-specific init and entry point + * This should be maximally reusable across platforms + */ + typedef struct { const char* filename; FILE* file; @@ -137,6 +164,8 @@ int nese_frame_start(void* plat_data, uint8_t background) { platform_data* plat = (platform_data*)plat_data; SDL_Color ext = nes_palette[background]; + // TODO: Compose color according to GPU format? + // Why are R/B reversed on STM32? SDL_FillRect(plat->target, NULL, ((int)ext.r << 16) | ((int)ext.g << 8) | ext.b); @@ -218,6 +247,7 @@ int nese_frame_ready(void* plat_data) { } putc('\n', stdout); */ + SDL_RenderFlush(plat->renderer); SDL_BlitSurface(plat->screen, NULL, plat->target, NULL); diff --git a/src/log.h b/src/log.h index 6653590..a6b9749 100644 --- a/src/log.h +++ b/src/log.h @@ -1,15 +1,15 @@ #include -#ifdef DEBUG +#ifdef NESE_DEBUG -#define LOG(l, f, ...) fprintf(stderr, l ": " DEBUG ": " f "\n" __VA_OPT__(,) __VA_ARGS__) +#define LOG(l, f, ...) fprintf(stderr, l ": " NESE_DEBUG ": " f "\n" __VA_OPT__(,) __VA_ARGS__) -#else // !DEBUG +#else // !NESE_DEBUG #define LOG(...) -#endif // DEBUG +#endif // NESE_DEBUG #define LOGE(f, ...) LOG("E", f, __VA_ARGS__) diff --git a/src/map/map001.c b/src/map/map001.c index 4ca4c44..b5254f3 100644 --- a/src/map/map001.c +++ b/src/map/map001.c @@ -1,6 +1,6 @@ #include "memory.h" -//#define DEBUG "MMC1" +//#define NESE_DEBUG "MMC1" #include "log.h" diff --git a/src/map/map004.c b/src/map/map004.c index 3203cef..a1a7594 100644 --- a/src/map/map004.c +++ b/src/map/map004.c @@ -1,6 +1,6 @@ #include "memory.h" -#define DEBUG "MMC3" +#define NESE_DEBUG "MMC3" #include "log.h" diff --git a/src/nes.c b/src/nes.c index 8b12a71..72bee51 100644 --- a/src/nes.c +++ b/src/nes.c @@ -3,7 +3,7 @@ #include "nes.h" #include "port.h" -//#define DEBUG "NES" +//#define NESE_DEBUG "NES" #include "log.h" diff --git a/src/port.h b/src/port.h index a522a7c..4d91896 100644 --- a/src/port.h +++ b/src/port.h @@ -11,5 +11,9 @@ int nese_line_ready(void*, uint8_t* buffer, int line); int nese_frame_ready(void*); int nese_update_input(void*, nes_Input*); +void* nese_alloc_gpu(int); +void* nese_alloc_cpu(int); +void* nese_alloc(int); + #endif // NESE_PORT_H_ diff --git a/src/ppu.c b/src/ppu.c index 6079195..d14f1e3 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -2,7 +2,7 @@ #include "ppu.h" -//#define DEBUG "PPU" +//#define NESE_DEBUG "PPU" #include "log.h"