From 3d70e69f6c9fbdcb890c6986096330e4f6611a32 Mon Sep 17 00:00:00 2001 From: Peter Fors Date: Thu, 29 May 2025 19:19:59 +0200 Subject: added mapper_tick() functionality, regressed 200fps, and optimized it back up to ~1940fps --- memory.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'memory.c') diff --git a/memory.c b/memory.c index 55c7015..096189b 100644 --- a/memory.c +++ b/memory.c @@ -6,18 +6,18 @@ static inline uint8_t memory_read(struct nes_state *state, uint32_t offset) { ppu_tick(state); // apu_tick(state); - if(LIKELY(offset >= 0x6000)) { // MOST - if(UNLIKELY(offset < 0x8000)) { - return state->mapper_function.prg_ram_read(state, offset); - } + if(offset >= 0x8000) { // MOST return state->mapper_function.prg_rom_read(state, offset); - } else if(LIKELY(offset < 0x2000)) { // SECOND + } else if((offset < 0x2000)) { // SECOND return state->ram[offset & 0x07ff]; } else if(offset < 0x4000) { // THIRD return ppu_read(state, offset); + } else if(offset >= 0x6000) { + return state->mapper_function.prg_ram_read(state, offset); + } else if(offset == 0x4016 || offset == 0x4017) { uint32_t index = offset & 1; uint8_t value = (state->ppu.input_latch[index] >> state->ppu.input_bit[index]) & 1; @@ -28,13 +28,25 @@ static inline uint8_t memory_read(struct nes_state *state, uint32_t offset) { return 0; } +__attribute__((hot)) +static inline uint8_t memory_read_dummy(struct nes_state *state, uint32_t offset) { + state->cpu.cycles++; + ppu_tick(state); + // apu_tick(state); + + if(offset >= 0x2000 && offset < 0x4000) { + return ppu_read(state, offset); + } + return 0; +} + __attribute__((hot)) static inline void memory_write(struct nes_state *state, uint32_t offset, uint8_t value) { state->cpu.cycles++; ppu_tick(state); // apu_tick(state); - if(LIKELY(offset < 0x2000)) { + if(offset < 0x2000) { state->ram[offset & 0x07ff] = value; } else if(offset < 0x4000) { @@ -78,20 +90,8 @@ static inline void memory_write(struct nes_state *state, uint32_t offset, uint8_ } } - +__attribute__((hot, flatten)) static inline uint8_t memory_read_dma(struct nes_state *state, uint32_t offset) { - // Do not tick CPU/PPU/APU — caller handles timing + // NOTE(peter): DO NOT tick CPU/PPU/APU — caller handles timing return state->ram[offset & 0x07ff]; } - -static inline uint8_t memory_read_dummy(struct nes_state *state, uint32_t offset) { - state->cpu.cycles++; - ppu_tick(state); - // apu_tick(state); - - if(UNLIKELY(offset >= 0x2000 && offset < 0x4000)) { - return ppu_read(state, offset); - } - return 0; -} - -- cgit v1.2.3