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
|
__attribute__((hot))
static uint8_t memory_read(struct nes_state *restrict state, uint32_t offset) {
state->cpu.cycles++;
ppu_tick(state);
// apu_tick(state);
if(LIKELY(offset >= 0x6000)) { // MOST
return state->mapper.prg_read(state, offset);
} else if(LIKELY(offset < 0x2000)) { // SECOND
return state->ram[offset & 0x07ff];
} else if(offset < 0x4000) { // THIRD
return ppu_read(state, offset);
} else if(offset == 0x4016 || offset == 0x4017) {
uint32_t index = offset & 1;
uint8_t value = (state->ppu.input_latch[index] >> state->ppu.input_bit[index]) & 1;
state->ppu.input_bit[index]++;
return value | 0x40; // Bit 6 open bus high, bit 7 low
}
#if 0
if(LIKELY(offset < 0x2000)) { // SECOND
return state->ram[offset & 0x07ff];
} else if(offset < 0x4000) { // THIRD
return ppu_read(state, offset);
} else if(offset == 0x4016 || offset == 0x4017) {
uint32_t index = offset & 1;
uint8_t value = (state->ppu.input_latch[index] >> state->ppu.input_bit[index]) & 1;
state->ppu.input_bit[index]++;
return value | 0x40; // Bit 6 open bus high, bit 7 low
// } else if(offset == 4015) {
// static uint32_t apuread = 0;
// // printf("%.5d apu\n", apuread++);
} else if(LIKELY(offset >= 0x6000)) { // MOST
return state->mapper.prg_read(state, offset);
}
#endif
return 0;
}
__attribute__((hot))
static void memory_write(struct nes_state *restrict state, uint32_t offset, uint8_t value) {
state->cpu.cycles++;
ppu_tick(state);
// apu_tick(state);
if(LIKELY(offset < 0x2000)) {
state->ram[offset & 0x07ff] = value;
} else if(offset < 0x4000) {
ppu_write(state, offset, value);
} else if(offset < 0x4018) {
// APU + joypad + DMA
if(offset == 0x4014) {
ppu_dma_4014(state, value);
// } else if(offset == 0x4015) { // DMC Enable (APU)
// state->apu.dmc_dma_enabled = (value & 0x10) ? 1 : 0;
} else if(offset == 0x4016) {
uint8_t prev = state->ppu.input_strobe;
state->ppu.input_strobe = value & 1;
if(prev == 1 && (value & 1) == 0) {
// Latch current inputs
state->ppu.input_latch[0] = state->ppu.input[0];
state->ppu.input_latch[1] = state->ppu.input[1];
state->ppu.input_bit[0] = 0;
state->ppu.input_bit[1] = 0;
}
// } else if(offset == 0x4017) { // Frame Counter (APU)
// state->apu.mode = (value >> 7) & 1;
// state->apu.irq_inhibit = (value >> 6) & 1;
// state->apu.frame_cycle = 0;
// if(state->apu.irq_inhibit) {
// state->apu.irq_pending = 0;
// }
}
} 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->cpu.cycles++;
ppu_tick(state);
// apu_tick(state);
if(UNLIKELY(offset >= 0x2000 && offset < 0x4000)) {
return ppu_read(state, offset);
}
return 0;
}
|