diff options
Diffstat (limited to 'mappers/mapper.c')
| -rw-r--r-- | mappers/mapper.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/mappers/mapper.c b/mappers/mapper.c new file mode 100644 index 0000000..d63e2a0 --- /dev/null +++ b/mappers/mapper.c @@ -0,0 +1,63 @@ + +static uint8_t mapper_default_ciram_read(struct nes_state *state, uint32_t addr) { + if(state->ines.mirroring == 0) { // Horizontal + addr = (addr & 0x800) | (addr & 0x3ff); + } else { // Vertical (default fallback) + addr = addr & 0x7ff; + } + + return state->ciram[addr]; +} + +static void mapper_default_ciram_write(struct nes_state *state, uint32_t addr, uint8_t value) { + if(state->ines.mirroring == 0) { + addr = (addr & 0x800) | (addr & 0x3ff); + } else { + addr = addr & 0x7ff; + } + state->ciram[addr] = value; +} + +static void mapper_default_prg_write(struct nes_state *state, uint32_t addr, uint8_t value) { } +static void mapper_default_chr_write(struct nes_state *state, uint32_t addr, uint8_t value) { } +static void mapper_default_tick(struct nes_state *state) { } + +#include "mapper_000_0.c" +#include "mapper_002_2.c" +#include "mapper_003_0.c" +#include "mapper_007_2.c" +#include "mapper_011_0.c" +#include "mapper_066_0.c" + +#define MAPPER_ID(mapper, submapper) (((mapper) << 4) | (submapper)) + +static void (*mapper_table[4096])(struct nes_state *state) = { + [MAPPER_ID( 0, 0)] = mapper_000_0_init, + [MAPPER_ID( 2, 2)] = mapper_002_2_init, + [MAPPER_ID( 3, 0)] = mapper_003_0_init, + [MAPPER_ID( 7, 2)] = mapper_007_2_init, + [MAPPER_ID(11, 0)] = mapper_011_0_init, + [MAPPER_ID(66, 0)] = mapper_011_0_init, +}; + +static void mapper_reset(struct nes_state *state) { + state->mapper.prg_read = 0; + state->mapper.prg_write = mapper_default_prg_write; + state->mapper.chr_read = 0; + state->mapper.chr_write = mapper_default_chr_write; + state->mapper.ciram_read = mapper_default_ciram_read; + state->mapper.ciram_write = mapper_default_ciram_write; + state->mapper.tick = mapper_default_tick; +} + +static void mapper_setup(struct nes_state *state) { + uint32_t mapper = state->ines.mapper << 4 | state->ines.submapper; + mapper_reset(state); + if(mapper_table[mapper]) { + mapper_table[mapper](state); + } else { + printf("Unsupported mapper %d_%x, falling back to NROM\n", state->ines.mapper, state->ines.submapper); + mapper_table[0](state); + } +} + |
