summaryrefslogtreecommitdiff
path: root/mapper.c
blob: 09d9a628662b83b9d4ce0be27db01056e43f3d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54


#include "mapper_0000.c"
#include "mapper_0042.c"


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_tick(struct nes_state *state) { // No IRQ or timing logic needed
}

/*
 *  Mapper numbers are constructed like this:  (submapper << 12 | mapperid) both in hex.
 *
 *  NOTE(peter): Mapper 0 always has to be first!
 */
static struct mapper_entry mapper_table[] = {
/* Mapper:   0 */	{ 0x00, mapper_0000_prg_read, mapper_0000_prg_write, mapper_0000_chr_read, mapper_0000_chr_write, mapper_default_ciram_read, mapper_default_ciram_write, mapper_default_tick, mapper_0000_init },
/* Mapper:  66 */	{ 0x42, mapper_0042_prg_read, mapper_0042_prg_write, mapper_0042_chr_read, mapper_0042_chr_write, mapper_default_ciram_read, mapper_default_ciram_write, mapper_default_tick, mapper_0042_init },
};


static void mapper_setup(struct nes_state *state) {
	uint32_t mapper = state->ines.mapper;
	for(uint32_t i = 0; i < sizeof(mapper_table)/sizeof(mapper_table[0]); i++) {
		if(mapper_table[i].id == mapper) {
			state->mapper = mapper_table[i];
			state->mapper.init(state);
			return;
		}
	}

	printf("Unsupported mapper %d, falling back to NROM\n", mapper);
	state->mapper = mapper_table[0];
	state->mapper.init(state);
}