diff options
Diffstat (limited to 'mknes_memory.c')
| -rw-r--r-- | mknes_memory.c | 96 |
1 files changed, 54 insertions, 42 deletions
diff --git a/mknes_memory.c b/mknes_memory.c index a27fa04..7b74424 100644 --- a/mknes_memory.c +++ b/mknes_memory.c @@ -1,8 +1,9 @@ + __attribute__((hot)) static inline uint8_t memory_read(struct nes_state *state, uint32_t offset) { state->cpu.cycles++; - ppu_tick(state); apu_tick(state); + ppu_tick(state); if(offset <= 0x1fff) { return state->ram[offset & 0x07ff]; @@ -20,11 +21,11 @@ static inline uint8_t memory_read(struct nes_state *state, uint32_t offset) { return apu_read4015(state); } - if(offset == 0x4016 || offset == 0x4017) { + if(offset >= 0x4016 && offset <= 0x4017) { uint32_t index = offset & 1; uint8_t value = (state->ppu.input_latch[index] >> state->ppu.input_bit[index]) & 1; state->ppu.input_bit[index]++; - return value | 0x40; // Bit 6 open bus high, bit 7 low + return value | 0x40; // Bit 6 open bus high, bit 7 low } if(offset >= 0x6000 && offset <= 0x7fff) { @@ -34,65 +35,76 @@ static inline uint8_t memory_read(struct nes_state *state, uint32_t offset) { return 0; } - -__attribute__((hot)) +__attribute__((always_inline, hot)) static inline uint8_t memory_read_dummy(struct nes_state *state, uint32_t offset) { state->cpu.cycles++; - ppu_tick(state); apu_tick(state); + ppu_tick(state); + + uint8_t result = 0; if(offset >= 0x2000 && offset < 0x4000) { - return ppu_read(state, offset); + result = ppu_read(state, offset); } - return 0; + return result; } - __attribute__((hot, optimize("no-jump-tables"))) static inline void memory_write(struct nes_state *state, uint32_t offset, uint8_t value) { state->cpu.cycles++; - ppu_tick(state); apu_tick(state); + ppu_tick(state); - if(offset <= 0x1fff) { - state->ram[offset & 0x07ff] = value; - } + switch(offset) { + case 0x0000 ... 0x1fff: { + state->ram[offset & 0x07ff] = value; + } break; - if(offset >= 0x2000 && offset <= 0x3fff) { - ppu_write(state, offset, value); - } - if(offset >= 0x4000 && offset <= 0x4017) { - if(offset == 0x4014) { - ppu_dma_4014(state, value); - } - - if(offset == 0x4016) { - uint8_t s = value & 1; - uint8_t prev = state->ppu.input_strobe; - state->ppu.input_strobe = s; - - if(prev == 1 && s == 0) { - 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; + case 0x2000 ... 0x3fff: { + ppu_write(state, offset, value); + } break; + + case 0x4000 ... 0x4017: { + switch(offset) { + case 0x4014: { + ppu_dma_4014(state, value); + } break; + + case 0x4016: { + // joypad strobe + uint8_t s = value & 1; + + // if(s) { + uint8_t prev = state->ppu.input_strobe; + state->ppu.input_strobe = s; + + if(prev == 1 && (s) == 0) { + // state->ppu.input[0] = tas_input[tas_frame_count]; + + 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; + + default: { + apu_write(state, offset, value); + } break; } - } + } break; - if(offset != 0x4014 && offset != 0x4016) { - apu_write(state, offset, value); - } - } + case 0x6000 ... 0x7fff: { + state->mapper_function.prg_ram_write(state, offset, value); + } break; - if(offset >= 0x6000 && offset <= 0x7fff) { - state->mapper_function.prg_ram_write(state, offset, value); - } + case 0x8000 ... 0xffff: { + state->mapper_function.prg_rom_write(state, offset, value); + } break; - if(offset >= 0x8000) { - state->mapper_function.prg_rom_write(state, offset, value); } - } __attribute__((hot, flatten)) |
