From f1bd6a7d2f4ffe3e5263e0254bcf7522ab381264 Mon Sep 17 00:00:00 2001 From: Peter Fors Date: Sat, 5 Apr 2025 08:58:12 +0200 Subject: transform to switch case for ppu_tick() --- memory.c | 76 +++++++++++++++++----------------------------------------------- 1 file changed, 20 insertions(+), 56 deletions(-) (limited to 'memory.c') diff --git a/memory.c b/memory.c index e0ed85a..28c51ad 100644 --- a/memory.c +++ b/memory.c @@ -1,97 +1,61 @@ +__attribute__((hot)) static uint8_t memory_read(struct nes_state *restrict state, uint32_t offset) { state->cycles++; - ppu_tick(state); ppu_tick(state); ppu_tick(state); + ppu_tick(state); - if(offset > 0xffff) { - printf("%x\n", offset); - } - - // state->ram[0x301] = 0x1; - if(offset < 0x2000) { + if(LIKELY(offset < 0x2000)) { return state->ram[offset & 0x07ff]; } else if(offset < 0x4000) { return ppu_read(state, offset); - // switch(offset & 7) { - // case 2: return ppu_read_2002(state); - // case 4: return ppu_read_2004(state); - // case 7: return ppu_read_2007(state); - // default: return 0; - // } - } else if(offset < 0x4020) { - // TODO: APU and I/O reads - return 0; - } else if(offset >= 0x6000) { + // } else if(offset < 0x4020) { + // // TODO: APU and I/O reads + // return 0; + } else if(LIKELY(offset >= 0x6000)) { return state->mapper.prg_read(state, offset); - } else { - return 0; } + return 0; } +__attribute__((hot)) static void memory_write(struct nes_state *restrict state, uint32_t offset, uint8_t value) { state->cycles++; - ppu_tick(state); ppu_tick(state); ppu_tick(state); - - if(offset > 0xffff) { - printf("%x\n", offset); - } + ppu_tick(state); - if(offset < 0x2000) { + if(LIKELY(offset < 0x2000)) { state->ram[offset & 0x07ff] = value; } else if(offset < 0x4000) { ppu_write(state, offset, value); - // switch(offset & 7) { - // case 0: ppu_write_2000(state, value); break; - // case 1: ppu_write_2001(state, value); break; - // case 3: ppu_write_2003(state, value); break; - // case 4: ppu_write_2004(state, value); break; - // case 5: ppu_write_2005(state, value); break; - // case 6: ppu_write_2006(state, value); break; - // case 7: ppu_write_2007(state, value); break; - // default: break; - // } } else if(offset == 0x4014) { ppu_dma_4014(state, value); - } else if(offset < 0x4020) { - // TODO: APU and I/O writes + // } else if(offset < 0x4020) { + // // TODO: APU and I/O writes } else if(offset >= 0x6000) { state->mapper.prg_write(state, offset, value); } } +__attribute__((hot)) static uint8_t memory_read_dma(struct nes_state *restrict state, uint32_t offset) { // Do not tick CPU/PPU/APU — caller handles timing - if(offset < 0x2000) { + if(LIKELY(offset < 0x2000)) { return state->ram[offset & 0x07ff]; - } else if(offset < 0x4000) { - // PPU register reads are ignored during DMA - return 0; - } else if(offset < 0x4020) { - // APU and I/O — usually ignored or blocked during DMA - return 0; } else if(offset >= 0x6000) { return state->mapper.prg_read(state, offset); - } else { - return 0; } + return 0; } +__attribute__((hot)) static uint8_t memory_read_dummy(struct nes_state *restrict state, uint32_t offset) { state->cycles++; - ppu_tick(state); ppu_tick(state); ppu_tick(state); + ppu_tick(state); - if(offset < 0x2000) { - return 0; - } else if(offset < 0x4000) { + if(UNLIKELY(offset >= 0x2000 && offset < 0x4000)) { return ppu_read(state, offset); - } else if(offset < 0x4020) { - return 0; - } else if(offset >= 0x6000) { - return state->mapper.prg_read(state, offset); - } else { - return 0; } + return 0; } -- cgit v1.2.3