summaryrefslogtreecommitdiff
path: root/mknes_memory.c
diff options
context:
space:
mode:
authorPeter Fors <peter.fors@mindkiller.com>2025-10-25 21:56:37 +0200
committerPeter Fors <peter.fors@mindkiller.com>2025-10-25 21:56:37 +0200
commit396b10dd5e206462ebeab5fed368ba3ae25c6a51 (patch)
tree9d3c759298de673fb6d778b8bbeae4bec2174a9b /mknes_memory.c
parenta4c261c6ee3940099e653a6f448dc952dfd5899f (diff)
Better benchmarking, some small optimizations
Diffstat (limited to 'mknes_memory.c')
-rw-r--r--mknes_memory.c79
1 files changed, 41 insertions, 38 deletions
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;
}
}