summaryrefslogtreecommitdiff
path: root/memory.c
blob: 28c51ada0754860be382b1e49904d97635c43e41 (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



__attribute__((hot))
static uint8_t memory_read(struct nes_state *restrict state, uint32_t offset) {
	state->cycles++;
	ppu_tick(state);

	if(LIKELY(offset < 0x2000)) {
		return state->ram[offset & 0x07ff];
	} else if(offset < 0x4000) {
		return ppu_read(state, offset);
	// } else if(offset < 0x4020) {
	// 	// TODO: APU and I/O reads
	// 	return 0;
	} else if(LIKELY(offset >= 0x6000)) {
		return state->mapper.prg_read(state, offset);
	}
	return 0;
}

__attribute__((hot))
static void memory_write(struct nes_state *restrict state, uint32_t offset, uint8_t value) {
	state->cycles++;
	ppu_tick(state);

	if(LIKELY(offset < 0x2000)) {
		state->ram[offset & 0x07ff] = value;
	} else if(offset < 0x4000) {
		ppu_write(state, offset, value);
	} else if(offset == 0x4014) {
		ppu_dma_4014(state, value);
	// } else if(offset < 0x4020) {
	// 	// TODO: APU and I/O writes
	} else if(offset >= 0x6000) {
		state->mapper.prg_write(state, offset, value);
	}
}

__attribute__((hot))
static uint8_t memory_read_dma(struct nes_state *restrict state, uint32_t offset) {
	// Do not tick CPU/PPU/APU — caller handles timing
	if(LIKELY(offset < 0x2000)) {
		return state->ram[offset & 0x07ff];
	} else if(offset >= 0x6000) {
		return state->mapper.prg_read(state, offset);
	}
	return 0;
}

__attribute__((hot))
static uint8_t memory_read_dummy(struct nes_state *restrict state, uint32_t offset) {
	state->cycles++;
	ppu_tick(state);

	if(UNLIKELY(offset >= 0x2000 && offset < 0x4000)) {
		return ppu_read(state, offset);
	}
	return 0;
}