Skip to content

Misc fixes - #29

Merged
rene merged 10 commits into
mainfrom
misc-fixes
Oct 18, 2026
Merged

rene merged 10 commits into
mainfrom
misc-fixes

Conversation

@rene

@rene rene commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Several fixes.

rene and others added 7 commits October 17, 2026 17:05
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 <rene@renesp.com.br>
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 <rene@renesp.com.br>
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 <noreply@anthropic.com>
Signed-off-by: Renê de Souza Pinto <rene@renesp.com.br>
/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 <rene@renesp.com.br>
This fixes error 2 of Blargg's ppu_open_bus test.

Signed-off-by: Renê de Souza Pinto <rene@renesp.com.br>
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 <rene@renesp.com.br>
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 <rene@renesp.com.br>
@rene

rene commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

@claude

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Claude finished @rene's task —— View job


Code Review

  • Gather context
  • Review changed files
  • Post findings

src/apu.c

> → >= for DMC timer (line ~562)
The fix is correct. The NES APU DMC timer fires every period CPU cycles (rate table values are exact counts), so the counter should fire when it reaches the period, not when it exceeds it. Using > stretched every rate by one extra cycle. The >= fix and the matching change in apu_reg_write are consistent with each other. ✅

Frame sequencer IRQ inhibit — removed mode condition (line ~1051)

- if (apu.frm_seq.mode || (value & 0x40)) {
+ if ((value & 0x40)) {

This is correct. Per NESdev, writing $4017 with bit 6 set clears the IRQ flag regardless of mode. The old code also inhibited/cleared IRQ whenever mode was 1 (5-step), which is wrong — the IRQ inhibit flag is set/cleared solely by bit 6. ✅


src/mappers/m0_NROM.c

PRG RAM always allocated (line ~177)
The old code conditionally allocated PRG RAM or fell back to pointing at PRG ROM (the flag10.ines.prg_ram iNES header bit). That header bit is unreliable in practice, so allocating unconditionally is a pragmatic choice.

However, the error path in m0_mapper_init at line 179 calls free(m0) but does not call free(m->data) first — m0 is m->data, so this is fine as long as nothing else was allocated yet. Worth double-checking that m->data is the only thing freed here (it is, since prg_ram failed). ✅

The finalization path is now simpler and correct — free(m0->prg_ram) is always called unconditionally to match the unconditional calloc. ✅


src/ppu.c

ppu.gen_latch set on write (line ~375)

if (reg >= REG_PPUCTRL && reg <= REG_PPUDATA)
    ppu.gen_latch = value;

REG_PPUCTRL is 0x2000 and REG_PPUDATA is 0x2007. REG_OAMDMA is 0x4014. Per NESdev, writes to $4014 (OAMDMA) also refresh the open bus / latch. The range check misses it. This was likely intentional (OAMDMA is handled via the sbus module), but it's worth noting that the latch won't be updated for OAMDMA writes.

Sprite byte 2 reserved-bits mask on OAMDATA read (line ~325)

if ((ppu.OAMDMA & 0x03) == 0x02)
    value &= 0xe3;

ppu.OAMDMA is the $4014 DMA register byte (the high byte of the DMA source page). It is not a counter of which OAM byte is being read. To determine if the current byte is byte 2 of a sprite (the attribute byte), the correct check should be against ppu.OAMADDR:

if ((ppu.OAMADDR & 0x03) == 0x02)
    value &= 0xe3;

Using OAMDMA & 0x03 will mask bits only when the DMA source address happens to have the low two bits equal to 2, which is coincidental and incorrect. This is a bug. 🐛

NMI cleared when PPUCTRL NMI bit is 0 (line ~384)

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();

Per NESdev, clearing the NMI enable bit in PPUCTRL while in vblank should suppress the NMI. This is correct. ✅

OAMDATA write guard (line ~404)

- if (ppu.scanline < 240 || ppu.scanline == 261)
+ if (is_rendering() && (ppu.scanline < 240 || ppu.scanline == 261))

Adding the is_rendering() guard is correct — OAMDATA writes should only be ignored during active rendering, not when rendering is disabled. ✅

OAMADDR clear guard (line ~639)

- if (ppu.cycle >= 257 && ppu.cycle <= 320)
+ if (is_rendering() && ppu.cycle >= 257 && ppu.cycle <= 320)

Correct — OAMADDR should only be cleared during rendering. ✅

Sprite evaluation gated to visible scanlines (line ~642)

- if (ppu.cycle == 256) {
+ if (ppu.cycle == 256 && ppu.scanline <= 239) {

Correct — sprite evaluation for the next scanline should only happen on visible scanlines (0–239), not on the pre-render scanline 261. ✅


Summary

One bug found:

  • src/ppu.c:325 — ppu.OAMDMA is the wrong field for determining the sprite attribute byte offset. It should be ppu.OAMADDR & 0x03.

All other changes look correct and well-motivated.

rene added 3 commits October 18, 2026 22:20
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 <rene@renesp.com.br>
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 <rene@renesp.com.br>
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 <rene@renesp.com.br>
@rene

rene commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

@claude

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Claude finished @rene's task —— View job


Code Review

  • Read diff and changed files
  • Review changes
  • Post findings

src/ppu.c

OAMDATA read — sprite attribute byte mask (line 325) ✅
The previous review flagged ppu.OAMDMA being used here; the diff correctly uses ppu.OAMADDR:

if ((ppu.OAMADDR & 0x03) == 0x02)
    value &= 0xe3;

This is the correct fix.

gen_latch range misses $4014 / OAMDMA (line 375)

if (reg >= REG_PPUCTRL && reg <= REG_PPUDATA)
    ppu.gen_latch = value;

REG_PPUCTRL is 0x2000, REG_PPUDATA is 0x2007, REG_OAMDMA is 0x4014. Per NESdev, a write to $4014 also refreshes the open bus latch. This range check silently skips OAMDMA. OAMDMA is typically driven by the sbus/CPU DMA path rather than ppu_reg_write, so this may be a deliberate design choice — but if anything ever calls ppu_reg_write(REG_OAMDMA, v) the latch won't update. Worth a comment or an explicit case REG_OAMDMA in the latch logic if intentional.

NMI clear on PPUCTRL write (line 384)

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();

One subtle edge case: if old_vblank == 1 and nmi is written as 1 again (no change), neither branch fires — correct. But if vblank is active and NMI enable transitions 1→0→1 within a few cycles, cpu_clear_nmi() is called on the first write and cpu_trigger_nmi() on the second. This matches expected NES behaviour, so this is fine. ✅

All other PPU changes (is_rendering() guards, scanline <= 239 sprite eval gate) are correct. ✅


src/apu.c

DMC timer >= fix (line 563) ✅ Correct.

Frame sequencer IRQ inhibit — removed mode condition (line 1051) ✅ Correct.

apu_reg_write DMC restart >= (line 1077)

if (apu.dmc.cycles >= apu.dmc.period)
    apu.dmc.cycles = 0;

Consistent with the dmc_clock fix. ✅


src/mappers/m0_NROM.c

Unconditional PRG RAM allocation (line 177)
Pragmatic and reasonable given the unreliability of the iNES prg_ram header bit. The init/finalize symmetry (calloc / unconditional free) is correct. ✅

Error path at line 179

if (!m0->prg_ram) {
    free(m0);   /* frees m->data, which is m0 */
    return -ENOMEM;
}

m0 == m->data, so free(m0) is correct here — no double-free or leak since chr_mem has not been allocated yet at this point. ✅


Summary

All changes look correct. The previously reported bug (ppu.OAMDMA vs ppu.OAMADDR) is fixed in this diff. The only minor nit is the gen_latch range in ppu_reg_write not covering REG_OAMDMA ($4014), which may be intentional given the DMA routing architecture.

@rene
rene merged commit c3a0d65 into main Oct 18, 2026
4 checks passed
@rene
rene deleted the misc-fixes branch October 18, 2026 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant