summaryrefslogtreecommitdiff
path: root/mapper.c
blob: a7954b1ac194bf454d804133470cc6aaf5d95a18 (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_001.c"
// #include "mapper_mmc1.c"
// #include "mapper_uxrom.c"
#include "mapper_066.c"

struct mapper_entry {
	int		id;
	uint8_t	(*prg_read)(struct nes_state *state, uint32_t addr);
	void		(*prg_write)(struct nes_state *state, uint32_t addr, uint8_t value);
	uint8_t	(*chr_read)(struct nes_state *state, uint32_t addr);
	void		(*chr_write)(struct nes_state *state, uint32_t addr, uint8_t value);
	uint8_t	(*ciram_read)(struct nes_state *state, uint32_t addr);
	void		(*ciram_write)(struct nes_state *state, uint32_t addr, uint8_t value);
	void		(*tick)(struct nes_state *state);
	void		(*init)(struct nes_state *state);
};

static struct mapper_entry mapper_table[] = {
	{  0, mapper_001_prg_read, mapper_001_prg_write, mapper_001_chr_read, mapper_001_chr_write, mapper_001_ciram_read, mapper_001_ciram_write, mapper_001_tick, mapper_001_init },
	{ 66, mapper_066_prg_read, mapper_066_prg_write, mapper_066_chr_read, mapper_066_chr_write, mapper_066_ciram_read, mapper_066_ciram_write, mapper_066_tick, mapper_066_init },
	// { 1, mapper_mmc1_read, ... }, etc
};

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.prg_read		= mapper_table[i].prg_read;
			state->mapper.prg_write		= mapper_table[i].prg_write;
			state->mapper.chr_read		= mapper_table[i].chr_read;
			state->mapper.chr_write		= mapper_table[i].chr_write;
			state->mapper.ciram_read	= mapper_table[i].ciram_read;
			state->mapper.ciram_write	= mapper_table[i].ciram_write;
			state->mapper.tick			= mapper_table[i].tick;
			state->mapper.init			= mapper_table[i].init;
			state->mapper.init(state);
			return;
		}
	}

	// NOTE(peter): Not sure how safe this is, but it sure is funny...
	printf("Unsupported mapper %d, falling back to NROM\n", mapper);
	state->mapper.prg_read		= mapper_001_prg_read;
	state->mapper.prg_write		= mapper_001_prg_write;
	state->mapper.chr_read		= mapper_001_chr_read;
	state->mapper.chr_write		= mapper_001_chr_write;
	state->mapper.ciram_read	= mapper_001_ciram_read;
	state->mapper.ciram_write	= mapper_001_ciram_write;
	state->mapper.tick			= mapper_001_tick;
	state->mapper.init			= mapper_001_init;
	state->mapper.init(state);
}