From 396b10dd5e206462ebeab5fed368ba3ae25c6a51 Mon Sep 17 00:00:00 2001 From: Peter Fors Date: Sat, 25 Oct 2025 21:56:37 +0200 Subject: Better benchmarking, some small optimizations --- mknes_memory.c | 79 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) (limited to 'mknes_memory.c') diff --git a/mknes_memory.c b/mknes_memory.c index 7b74424..49546b2 100644 --- a/mknes_memory.c +++ b/mknes_memory.c @@ -5,6 +5,8 @@ static inline uint8_t memory_read(struct nes_state *state, uint32_t offset) { apu_tick(state); ppu_tick(state); + if(offset >= 0x10000) __builtin_unreachable(); + if(offset <= 0x1fff) { return state->ram[offset & 0x07ff]; } @@ -55,55 +57,56 @@ static inline void memory_write(struct nes_state *state, uint32_t offset, uint8_ apu_tick(state); ppu_tick(state); - switch(offset) { - case 0x0000 ... 0x1fff: { - state->ram[offset & 0x07ff] = value; - } break; + if(offset >= 0x10000) __builtin_unreachable(); + if(offset <= 0x1fff) { + state->ram[offset & 0x07ff] = value; + return; + } - case 0x2000 ... 0x3fff: { - ppu_write(state, offset, value); - } break; + if(offset >= 0x8000) { + state->mapper_function.prg_rom_write(state, offset, value); + return; + } - case 0x4000 ... 0x4017: { - switch(offset) { - case 0x4014: { - ppu_dma_4014(state, value); - } break; + if(offset >= 0x2000 && offset <= 0x3fff) { + ppu_write(state, offset, value); + return; + } - case 0x4016: { - // joypad strobe - uint8_t s = value & 1; + if(offset == 0x4014) { + ppu_dma_4014(state, value); + return; + } - // if(s) { - uint8_t prev = state->ppu.input_strobe; - state->ppu.input_strobe = s; + if(offset == 0x4016) { + // joypad strobe + uint8_t s = value & 1; - if(prev == 1 && (s) == 0) { - // state->ppu.input[0] = tas_input[tas_frame_count]; + // if(s) { + uint8_t prev = state->ppu.input_strobe; + state->ppu.input_strobe = s; - state->ppu.input_latch[0] = state->ppu.input[0]; - state->ppu.input_latch[1] = state->ppu.input[1]; - state->ppu.input_bit[0] = 0; - state->ppu.input_bit[1] = 0; - } - // } - } break; + if(prev == 1 && (s) == 0) { + // state->ppu.input[0] = tas_input[tas_frame_count]; - default: { - apu_write(state, offset, value); - } break; + state->ppu.input_latch[0] = state->ppu.input[0]; + state->ppu.input_latch[1] = state->ppu.input[1]; + state->ppu.input_bit[0] = 0; + state->ppu.input_bit[1] = 0; } - } break; - - case 0x6000 ... 0x7fff: { - state->mapper_function.prg_ram_write(state, offset, value); - } break; + // } + return; + } - case 0x8000 ... 0xffff: { - state->mapper_function.prg_rom_write(state, offset, value); - } break; + if(offset >= 0x4000 && offset <= 0x4017) { + apu_write(state, offset, value); + return; + } + if(offset >= 0x6000 && offset <= 0x7fff) { + state->mapper_function.prg_ram_write(state, offset, value); + return; } } -- cgit v1.2.3