summaryrefslogtreecommitdiff
path: root/mappers/mapper_001_0.c
blob: a449fe71c853fbb8fc570f3fb85c0549810a0037 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
static uint8_t mapper_001_0_prg_rom_read(struct nes_state *state, uint32_t addr) {
	struct mapper_001_0 *mapper = &state->mapper_data.m001_0;

	if(addr >= 0x8000) {
		if(addr < 0xc000) {
			return mapper->prg_rom_0[addr & 0x3fff];
		} else {
			return mapper->prg_rom_1[addr & 0x3fff];
		}
	}
	return 0;
}

static void mapper_001_0_prg_rom_write(struct nes_state *state, uint32_t addr, uint8_t value) {
	struct mapper_001_0 *mapper = &state->mapper_data.m001_0;

printf("MMC1 write: addr=%04x val=%02x\n", addr, value);

	if(addr < 0x8000) {
		return;
	}

	if(value & 0x80) {
		mapper->shift = 0;
		mapper->shift_count = 0;
		mapper->control |= 0x0c;
		return;
	}

	mapper->shift |= (value & 1) << mapper->shift_count;
	mapper->shift_count++;

	if(mapper->shift_count == 5) {
		uint8_t reg = (addr >> 13) & 3;

		switch(reg) {
			case 0: {
				mapper->control = mapper->shift;
				switch(mapper->control & 3) {
					case 0: {
						state->ines.mirroring = MIRROR_ONESCREEN_LOW;
					} break;

					case 1: {
						state->ines.mirroring = MIRROR_ONESCREEN_HIGH;
					} break;

					case 2: {
						state->ines.mirroring = MIRROR_VERTICAL;
					} break;

					case 3: {
						state->ines.mirroring = MIRROR_HORIZONTAL;
					} break;
				}
			} break;

			case 1: {
				mapper->chr_bank0 = mapper->shift;
				mapper->chr_bank_0 = state->chr_rom + (mapper->chr_bank0 * 0x1000);
			} break;

			case 2: {
				mapper->chr_bank1 = mapper->shift;
				mapper->chr_bank_1 = state->chr_rom + (mapper->chr_bank1 * 0x1000);
			} break;

			case 3: {
				mapper->prg_bank = mapper->shift & 0x0f;

				if(mapper->control & 0x08) {
					if(mapper->control & 0x04) {
						mapper->prg_rom_0 = state->prg_rom;
						mapper->prg_rom_1 = state->prg_rom + (mapper->prg_bank * 0x4000);
					} else {
						mapper->prg_rom_0 = state->prg_rom + (mapper->prg_bank * 0x4000);
						mapper->prg_rom_1 = state->prg_rom + (state->ines.prg_size - 0x4000);
					}
				} else {
					uint32_t base = (mapper->prg_bank & 0x0e) * 0x4000;
					mapper->prg_rom_0 = state->prg_rom + base;
					mapper->prg_rom_1 = state->prg_rom + base + 0x4000;
				}
			} break;
		}

		mapper->shift = 0;
		mapper->shift_count = 0;
	}
}

static uint8_t mapper_001_0_chr_read(struct nes_state *state, uint32_t addr) {
	struct mapper_001_0 *mapper = &state->mapper_data.m001_0;

	if(mapper->control & 0x10) {
		if(addr < 0x1000) {
			return mapper->chr_bank_0[addr & 0xfff];
		} else {
			return mapper->chr_bank_1[addr & 0xfff];
		}
	} else {
		return mapper->chr_bank_0[addr & 0x1fff];
	}
}

static void mapper_001_0_chr_write(struct nes_state *state, uint32_t addr, uint8_t value) {
	state->chr_ram[addr] = value;
}

static uint8_t mapper_001_0_ciram_read(struct nes_state *state, uint32_t addr) {
	// MMC1 can set mirroring mode dynamically via control register
	switch(state->ines.mirroring) {
		case MIRROR_ONESCREEN_LOW:
			addr = addr & 0x3ff;
			break;
		case MIRROR_ONESCREEN_HIGH:
			addr = 0x400 | (addr & 0x3ff);
			break;
		case MIRROR_HORIZONTAL: // Horizontal: NT0↔NT1, NT2↔NT3
			addr = ((addr >> 1) & 0x400) | (addr & 0x3ff);
			break;
		case MIRROR_VERTICAL: // Vertical: NT0↔NT2, NT1↔NT3
			addr = addr & 0x7ff;
			break;
	}
	return state->ciram[addr];
}

static void mapper_001_0_ciram_write(struct nes_state *state, uint32_t addr, uint8_t value) {
	// MMC1 can set mirroring mode dynamically via control register
	switch(state->ines.mirroring) {
		case MIRROR_ONESCREEN_LOW:
			addr = addr & 0x3ff;
			break;
		case MIRROR_ONESCREEN_HIGH:
			addr = 0x400 | (addr & 0x3ff);
			break;
		case MIRROR_HORIZONTAL: // Horizontal: NT0↔NT1, NT2↔NT3
			addr = ((addr >> 1) & 0x400) | (addr & 0x3ff);
			break;
		case MIRROR_VERTICAL: // Vertical: NT0↔NT2, NT1↔NT3
			addr = addr & 0x7ff;
			break;
	}
	state->ciram[addr] = value;
}

static void mapper_001_0_init(struct nes_state *state) {
	struct mapper_001_0 *mapper = &state->mapper_data.m001_0;

	mapper->shift = 0;
	mapper->shift_count = 0;
	mapper->control = 0x0c;

	mapper->prg_bank = 0;
	mapper->chr_bank0 = 0;
	mapper->chr_bank1 = 0;

	mapper->prg_rom_0 = state->prg_rom;
	mapper->prg_rom_1 = state->prg_rom + (state->ines.prg_size - 0x4000);

	if(state->ines.chr_size) {
		mapper->chr_bank_0 = state->chr_rom;
		mapper->chr_bank_1 = state->chr_rom + 0x1000;
	} else {
		mapper->chr_bank_0 = state->chr_ram;
		mapper->chr_bank_1 = state->chr_ram + 0x1000;
	}

	state->mapper_function.prg_rom_read = mapper_001_0_prg_rom_read;
	state->mapper_function.prg_rom_write = mapper_001_0_prg_rom_write;
	state->mapper_function.chr_read = mapper_001_0_chr_read;
	state->mapper_function.chr_write = mapper_001_0_chr_write;
	state->mapper_function.ciram_read = mapper_001_0_ciram_read;
	state->mapper_function.ciram_write = mapper_001_0_ciram_write;
}