summaryrefslogtreecommitdiff
path: root/mknes.c
diff options
context:
space:
mode:
authorPeter Fors <peter.fors@mindkiller.com>2025-04-16 12:37:40 +0200
committerPeter Fors <peter.fors@mindkiller.com>2025-04-16 12:37:40 +0200
commit6321f071ed2ab36242e857a9414b7f4c53092d72 (patch)
tree80eb868cc206c932d32943d101e7f337324fa7f4 /mknes.c
parent18a2c1406f1fa585f4574cd687b0791e52ab5d7a (diff)
Battletoads working
Diffstat (limited to 'mknes.c')
-rw-r--r--mknes.c68
1 files changed, 51 insertions, 17 deletions
diff --git a/mknes.c b/mknes.c
index b959230..9b58753 100644
--- a/mknes.c
+++ b/mknes.c
@@ -16,7 +16,7 @@
#define PRG_ROM_SIZE (512 * 1024)
#define CHR_ROM_SIZE (512 * 1024)
#define PIXELS_SIZE (256 * 240)
-#define RAM_SIZE 0x800
+#define RAM_SIZE 0x1000 // 0x800 in reality, but for aligned alloc it must be the size of the alignment (4096)
#define SRAM_SIZE 0x2000
#define CIRAM_SIZE 0x1000
#define CHR_RAM_SIZE 0x4000
@@ -89,6 +89,7 @@ static GLFWwindow *window;
#include "opengl.c"
#include "render.c"
+static uint32_t frames; // debug information
// NES core
#include "mapper.h"
@@ -104,33 +105,62 @@ static GLFWwindow *window;
#include "callbacks.c"
struct nes_state nstate;
-static uint32_t frames;
static struct nes_state *allocate_nes_state(void) {
struct nes_state *state = (struct nes_state*)calloc(1, sizeof(struct nes_state));
if(!state) return 0;
- size_t total_size = (PRG_ROM_SIZE + CHR_ROM_SIZE + PIXELS_SIZE + RAM_SIZE + SRAM_SIZE + CIRAM_SIZE + CHR_RAM_SIZE+ 4095) & ~0xfff;
-
- uint8_t *m = (uint8_t*)aligned_alloc(4096, total_size);
- memset(m, 0, total_size);
-
- size_t offset = 0;
- state->prg_rom = m + offset; offset += PRG_ROM_SIZE; // DO NOT MOVE THIS, as this is the pointer we free later.
- state->chr_rom = m + offset; offset += CHR_ROM_SIZE;
- state->pixels = m + offset; offset += PIXELS_SIZE;
- state->ram = m + offset; offset += RAM_SIZE;
- state->sram = m + offset; offset += SRAM_SIZE;
- state->ciram = m + offset; offset += CIRAM_SIZE;
- state->chr_ram = m + offset; offset += CHR_RAM_SIZE;
+ state->prg_rom = aligned_alloc(4096, PRG_ROM_SIZE);
+ state->chr_rom = aligned_alloc(4096, CHR_ROM_SIZE);
+ state->pixels = aligned_alloc(4096, PIXELS_SIZE);
+ state->ram = aligned_alloc(4096, RAM_SIZE);
+ state->sram = aligned_alloc(4096, SRAM_SIZE);
+ state->ciram = aligned_alloc(4096, CIRAM_SIZE);
+ state->chr_ram = aligned_alloc(4096, CHR_RAM_SIZE);
+ memset(state->prg_rom, 0, PRG_ROM_SIZE);
+ memset(state->chr_rom, 0, CHR_ROM_SIZE);
+ memset(state->pixels, 0, PIXELS_SIZE);
+ memset(state->ram, 0, RAM_SIZE);
+ memset(state->sram, 0, SRAM_SIZE);
+ memset(state->ciram, 0, CIRAM_SIZE);
+ memset(state->chr_ram, 0, CHR_RAM_SIZE);
return state;
}
static void free_nes_state(struct nes_state *s) {
free(s->prg_rom);
+ free(s->chr_rom);
+ free(s->pixels);
+ free(s->ram);
+ free(s->sram);
+ free(s->ciram);
+ free(s->chr_ram);
free(s);
}
+
+
+
+static void dump_nametable_text(struct nes_state *state, const char *filename) {
+ FILE *f = fopen(filename, "w");
+ if(!f) return;
+
+ for(int y = 0; y < 30; y++) {
+ for(int x = 0; x < 32; x++) {
+ uint16_t addr = y * 32 + x;
+ uint8_t tile = state->ciram[addr]; // assuming NT0 is mapped to 0x000
+ fprintf(f, "%02x ", tile);
+ }
+ fprintf(f, "\n");
+ }
+ fclose(f);
+}
+
+
+
+
+
+
int main(int argc, char **argv) {
#ifdef _WIN32
timeBeginPeriod(1);
@@ -173,7 +203,7 @@ int main(int argc, char **argv) {
// ines2_load(nstate, "data/0003/Friday the 13th (USA).zip");
// ines2_load(nstate, "data/0003/Ghostbusters (Japan).zip");
// ines2_load(nstate, "data/0003/Gradius (USA).zip");
- // ines2_load(nstate, "data/0007/Battletoads (USA).zip");
+ ines2_load(nstate, "data/0007/Battletoads (USA).zip");
// ines2_load(nstate, "data/0007/Beetlejuice (USA).zip");
// ines2_load(nstate, "data/0007/Cabal (USA).zip");
@@ -229,7 +259,7 @@ int main(int argc, char **argv) {
timer_start(timer);
-#if 1
+#if 0
for(uint32_t i = 0; i < 0x5000; ++ i) {
while(!nstate->ppu.frame_ready) {
// PROFILE_NAMED("nes emulator");
@@ -243,6 +273,8 @@ int main(int argc, char **argv) {
timer_wait(timer);
glfwPollEvents();
+// printf("Frame: %d\n", frames);
+
while(!nstate->ppu.frame_ready) {
// PROFILE_NAMED("nes emulator");
cpu_tick(nstate);
@@ -250,6 +282,8 @@ int main(int argc, char **argv) {
nstate->ppu.frame_ready = 0;
frames++;
+dump_nametable_text(nstate, "_foofbomb.txt");
+
uint32_t * restrict dst = display_buffer; //buffer;
uint8_t * restrict src = nstate->pixels;
for(uint32_t y = 0; y < 240; ++y) {