From 07e195c0f749102bf47107304e7c702571558283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sat, 17 Oct 2026 17:05:08 +0200 Subject: [PATCH 01/10] m0_NROM.c: Always allocate PRG RAM MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We cannot rely on the iNES header to detect whether cartridge has PRG RAM or not, so always allocate PRG RAM for mapper 0. Signed-off-by: Renê de Souza Pinto --- src/mappers/m0_NROM.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/mappers/m0_NROM.c b/src/mappers/m0_NROM.c index b67a2c5..e54f26d 100644 --- a/src/mappers/m0_NROM.c +++ b/src/mappers/m0_NROM.c @@ -173,16 +173,11 @@ static int m0_mapper_init(struct _mapper_t *m, cartridge_t *c) return -EINVAL; } - /* Check PRG RAM */ - if (c->rom->header->flag10.ines.prg_ram) { - /* Allocates 8 KB of PRG RAM */ - m0->prg_ram = calloc(sizeof(uint8_t), 0x2000); - if (!m0->prg_ram) { - free(m0); - return -ENOMEM; - } - } else { - m0->prg_ram = m->cartridge->rom->prg_rom; + /* Allocates 8 KB of PRG RAM */ + m0->prg_ram = calloc(sizeof(uint8_t), 0x2000); + if (!m0->prg_ram) { + free(m0); + return -ENOMEM; } /* Check CHR RAM */ @@ -211,11 +206,10 @@ static int m0_mapper_finalize(struct _mapper_t *m) struct _m0_mapper *m0 = (struct _m0_mapper *)m->data; cartridge_t *cartridge = m->cartridge; - /* Free PRG and CHR roms only if they were allocated by the mapper */ - if (m0->prg_ram != cartridge->rom->prg_rom) - free(m0->prg_ram); + /* Free CHR rom only if it was allocated by the mapper */ if (m0->chr_mem != cartridge->rom->chr_rom) free(m0->chr_mem); + free(m0->prg_ram); free(m0); m->data = NULL; return 0; From 899959d7814aa6f81184089c51240b6419c8e3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sat, 17 Oct 2026 17:11:27 +0200 Subject: [PATCH 02/10] apu.c: Fix interrupt inhibit logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Write 0x00 or 0x80 to register $4017 should not affect interrupt flag. This was tested with Blargg's 3-irq_flag test. This commit fixes the logic and now the test passes. Signed-off-by: Renê de Souza Pinto --- src/apu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apu.c b/src/apu.c index c007d72..bef973e 100644 --- a/src/apu.c +++ b/src/apu.c @@ -1042,7 +1042,7 @@ void apu_reg_write(uint16_t reg, uint8_t value) else apu.frm_seq.mode = 1; - if (apu.frm_seq.mode || (value & 0x40)) { + if ((value & 0x40)) { apu.frm_seq.irq_inhibit = 1; apu.frm_seq.irq_flag = 0; cpu_clear_irq(); From 020c4f085973fafabdaf25018c89d9959b80de4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sun, 27 Sep 2026 18:24:40 +0200 Subject: [PATCH 03/10] apu.c: Fire the DMC timer on its period, not one cycle past it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The DMC output timer tested `++cycles > period`, so a rate whose period is 428 CPU cycles changed the output level every 429. Every one of the 16 rates ran a cycle slow, which stretches the playback rate of every DPCM sample in every game that uses one. Measured with a probe on the sample fetches while running blargg's apu_test 8-dmc_rates at rate 0 (period 428, so one fetch per 8 output cycles = 3424 cycles): before 3432 3432 3432 3432 (8 * 429) after 3424 3424 3424 3424 (8 * 428) https://www.nesdev.org/wiki/APU_DMC This does not make 8-dmc_rates pass on its own. That test also wants the $4015 DMC-active bit to clear when the last byte finishes playing, and rNES clears it when the last byte is *fetched* -- the memory reader refills the sample buffer as soon as it empties, so bytes_remaining reaches zero a full byte ahead of the output unit. Separating the sample buffer from the shift register is a larger change and is not attempted here. Co-Authored-By: Claude Opus 5 Signed-off-by: Renê de Souza Pinto --- src/apu.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/apu.c b/src/apu.c index bef973e..bb48f0a 100644 --- a/src/apu.c +++ b/src/apu.c @@ -552,9 +552,15 @@ static void dmc_clock(void) } } - /* Handle DMC unit clock cycle */ + /* Handle DMC unit clock cycle. + * + * The rate table is already in CPU cycles, so rate 0 is an output + * change every 428 of them. Firing on passing the period rather than + * on reaching it stretched every rate by one cycle. + * https://www.nesdev.org/wiki/APU_DMC + */ timer_fired = 0; - if (++apu.dmc.cycles > apu.dmc.period) { + if (++apu.dmc.cycles >= apu.dmc.period) { apu.dmc.cycles = 0; timer_fired = 1; } @@ -1068,7 +1074,7 @@ void apu_reg_write(uint16_t reg, uint8_t value) apu.dmc.dma.current_addr = apu.dmc.dma.sample_addr; apu.dmc.dma.bytes_remaining = apu.dmc.dma.sample_len; - if (apu.dmc.cycles > apu.dmc.period) + if (apu.dmc.cycles >= apu.dmc.period) apu.dmc.cycles = 0; } apu.dmc.out.silence = 0; From 45d9fe76ecb88005be4a7a43921f99021c9ec41f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sat, 17 Oct 2026 17:21:09 +0200 Subject: [PATCH 04/10] ppu.c: Release /NMI when the PPUCTRL enable bit is cleared MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /NMI is asserted for as long as (vblank flag AND PPUCTRL bit 7), and the CPU detects its edge. The line is asserted correctly but never released it on a $2000 write, so clearing bit 7 left it asserted. If bit is written again later during the same vblank, the second NMI will be lost. Signed-off-by: Renê de Souza Pinto --- src/ppu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ppu.c b/src/ppu.c index 75d8ced..fe3c8da 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -374,8 +374,10 @@ void ppu_reg_write(uint16_t reg, uint8_t value) ppu.iregs.t.reg.bits.nametable_X = ppu.PPUCTRL.reg.nametable_X; ppu.iregs.t.reg.bits.nametable_Y = ppu.PPUCTRL.reg.nametable_Y; /* Check for NMI */ - if (old_vblank == 0 && ppu.PPUCTRL.reg.nmi == 1 && - ppu.PPUSTATUS.reg.vblank == 1) + if (ppu.PPUCTRL.reg.nmi == 0) + cpu_clear_nmi(); + else if (old_vblank == 0 && ppu.PPUCTRL.reg.nmi == 1 && + ppu.PPUSTATUS.reg.vblank == 1) cpu_trigger_nmi(); break; From 12d8fd81020a74cbf5bd3b5ce2a6ee7029f0b905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sat, 17 Oct 2026 23:21:39 +0200 Subject: [PATCH 05/10] ppu.c: Set latch when writing to PPU registers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes error 2 of Blargg's ppu_open_bus test. Signed-off-by: Renê de Souza Pinto --- src/ppu.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ppu.c b/src/ppu.c index fe3c8da..6b12767 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -368,6 +368,10 @@ void ppu_reg_write(uint16_t reg, uint8_t value) /* Save vblank configuration */ uint8_t old_vblank = ppu.PPUCTRL.reg.nmi; + /* Write to any register should set the latch */ + if (reg >= REG_PPUCTRL && reg <= REG_PPUDATA) + ppu.gen_latch = value; + switch (reg) { case REG_PPUCTRL: /* PPUCTRL: Write-Only */ ppu.PPUCTRL.raw = value; From a8c3037212b862b1e7a25208a82acf2bad618d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sat, 17 Oct 2026 23:26:03 +0200 Subject: [PATCH 06/10] ppu.c: Ignore writes only when rendering is enabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A $2004 write is ignored on the pre-render line and during rendering, so the rendering must be enabled. Signed-off-by: Renê de Souza Pinto --- src/ppu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ppu.c b/src/ppu.c index 6b12767..2256f52 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -398,7 +398,8 @@ void ppu_reg_write(uint16_t reg, uint8_t value) case REG_OAMDATA: /* OAMDATA: Read/Write */ /* Ignore writes during rendering or pre-rendering */ - if (ppu.scanline < 240 || ppu.scanline == 261) + if ((ppu.PPUMASK.reg.show_background || ppu.PPUMASK.reg.show_sprites) && + (ppu.scanline < 240 || ppu.scanline == 261)) break; ppu.OAM.mem[ppu.OAMADDR] = value; ppu.OAMADDR++; From 121b6fc220f4fb02d6dfef2e371d26b30f931008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sun, 18 Oct 2026 21:55:35 +0200 Subject: [PATCH 07/10] ppu.c: Ignore OAMDATA writes during rendering or pre-rendering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rendering must be enabled to ignore writes on OAMDATA during rendering or pre-redering. This commits fixes the logic. Signed-off-by: Renê de Souza Pinto --- src/ppu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ppu.c b/src/ppu.c index 2256f52..04d7c50 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -398,8 +398,7 @@ void ppu_reg_write(uint16_t reg, uint8_t value) case REG_OAMDATA: /* OAMDATA: Read/Write */ /* Ignore writes during rendering or pre-rendering */ - if ((ppu.PPUMASK.reg.show_background || ppu.PPUMASK.reg.show_sprites) && - (ppu.scanline < 240 || ppu.scanline == 261)) + if (is_rendering() && (ppu.scanline < 240 || ppu.scanline == 261)) break; ppu.OAM.mem[ppu.OAMADDR] = value; ppu.OAMADDR++; From 15db053bd4a884a82335546083bd05327f941a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sun, 18 Oct 2026 21:56:44 +0200 Subject: [PATCH 08/10] ppu.c: Always set sprite attribute reserved bits to zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Byte 2 of OAM (Sprite) data are the attributes, where bits 2-4 are reserved bits and should be always read as zeros. Signed-off-by: Renê de Souza Pinto --- src/ppu.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ppu.c b/src/ppu.c index 04d7c50..aa3d416 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -321,6 +321,9 @@ uint8_t ppu_reg_read(uint16_t reg) case REG_OAMDATA: /* OAMDATA: Read/Write */ value = ppu.OAM.mem[ppu.OAMADDR]; + /* If we have byte 2 of a sprite, reserved bits must be zero */ + if ((ppu.OAMADDR & 0x03) == 0x02) + value &= 0xe3; ppu.gen_latch = value; break; From 1cca4cd60f68e41cfa1ecedc99f6e66ae468b6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sun, 18 Oct 2026 22:00:05 +0200 Subject: [PATCH 09/10] ppu.c: Only clear OAMADDR while rendering is enabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OAMADDR was forced to 0 on cycles 257-320 of every visible and pre-render scanline, whatever the PPU was doing. That clear is part of loading the sprite tiles, so it only happens while rendering is enabled; with the screen off, OAMADDR keeps whatever the CPU last wrote. Signed-off-by: Renê de Souza Pinto --- src/ppu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ppu.c b/src/ppu.c index aa3d416..0f9d4e7 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -639,7 +639,7 @@ void ppu_clock(void) } /* Clear OAMADDR */ - if (ppu.cycle >= 257 && ppu.cycle <= 320) + if (is_rendering() && ppu.cycle >= 257 && ppu.cycle <= 320) ppu.OAMADDR = 0; /* Evaluate sprites to the next scanline */ From 33c52c7386df027f30a4dfe4879639d9f16d4584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=AA=20de=20Souza=20Pinto?= Date: Sun, 18 Oct 2026 22:06:00 +0200 Subject: [PATCH 10/10] ppu.c: Evaluate sprites on the visible scanlines only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sprite evaluation ran on the pre-render line as well, comparing sprite Y coordinates against scanline 261, so hidden sprites were matching the hit condition. They should be evaluated only for visible scanlines. Signed-off-by: Renê de Souza Pinto --- src/ppu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ppu.c b/src/ppu.c index 0f9d4e7..3084651 100644 --- a/src/ppu.c +++ b/src/ppu.c @@ -643,7 +643,7 @@ void ppu_clock(void) ppu.OAMADDR = 0; /* Evaluate sprites to the next scanline */ - if (ppu.cycle == 256) { + if (ppu.cycle == 256 && ppu.scanline <= 239) { /* Clear secondary OAM */ for (i = 0; i < sizeof(ppu.OAM_aux); i++) ppu.OAM_aux.mem[i] = 0xff;