Skip to content
14 changes: 10 additions & 4 deletions src/apu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1042,7 +1048,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();
Expand All @@ -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;
Expand Down
20 changes: 7 additions & 13 deletions src/mappers/m0_NROM.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 14 additions & 5 deletions src/ppu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -368,14 +371,20 @@ 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;
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;

Expand All @@ -392,7 +401,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.scanline < 240 || ppu.scanline == 261)
if (is_rendering() && (ppu.scanline < 240 || ppu.scanline == 261))
break;
ppu.OAM.mem[ppu.OAMADDR] = value;
ppu.OAMADDR++;
Expand Down Expand Up @@ -630,11 +639,11 @@ 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 */
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;
Expand Down
Loading