-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmc3.v
159 lines (143 loc) · 3.68 KB
/
mmc3.v
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
// MMC3 mapper
module MMC3 # (parameter USE_CHR_RAM = 1)
(
output led,
input m2,
input romsel,
input cpu_rw_in,
output reg [18:12] cpu_addr_out,
input [14:0] cpu_addr_in,
input [7:0] cpu_data_in,
output cpu_wr_out,
output cpu_rd_out,
output cpu_flash_ce,
output cpu_sram_ce,
input ppu_rd_in,
input ppu_wr_in,
input [13:10] ppu_addr_in,
output reg [18:10] ppu_addr_out,
output ppu_rd_out,
output ppu_wr_out,
output ppu_flash_ce,
output ppu_sram_ce,
output ppu_ciram_a10,
output ppu_ciram_ce,
output reg irq
);
reg [2:0] bank_select;
reg prg_mode;
reg chr_mode;
reg [7:0] r [0:7];
reg mirroring;
reg [7:6] ram_protect;
reg [7:0] irq_latch;
reg [7:0] irq_counter;
reg [2:0] a12_low_time;
reg irq_reload;
reg irq_reload_clear;
reg irq_enabled;
reg irq_value;
reg irq_ready;
assign cpu_wr_out = cpu_rw_in || ram_protect[6];
assign cpu_rd_out = ~cpu_rw_in;
assign cpu_flash_ce = romsel;
assign cpu_sram_ce = !(cpu_addr_in[14] && cpu_addr_in[13] && m2 && romsel && ram_protect[7]);
assign led = ~romsel;
assign ppu_rd_out = ppu_rd_in;
assign ppu_wr_out = ppu_wr_in;
// Enable CHR RAM or CHR ROM
assign ppu_sram_ce = USE_CHR_RAM ? ppu_addr_in[13] : 1'b1;
assign ppu_flash_ce = USE_CHR_RAM ? 1'b1 : ppu_addr_in[13];
assign ppu_ciram_ce = !ppu_addr_in[13];
// Mirroring control
assign ppu_ciram_a10 = mirroring ? ppu_addr_in[11] : ppu_addr_in[10];
initial
begin
irq_enabled = 0;
irq_ready = 0;
end
always @ (posedge romsel)
begin
if (cpu_rw_in == 0)
begin
case ({cpu_addr_in[14:13], cpu_addr_in[0]})
3'b000: begin // $8000-$9FFE, even
bank_select <= cpu_data_in[2:0];
prg_mode <= cpu_data_in[6];
chr_mode <= cpu_data_in[7];
end
3'b001: r[bank_select] <= cpu_data_in; // $8001-$9FFF, odd
3'b010: mirroring <= cpu_data_in[0]; // $A000-$BFFE, even
3'b011: ram_protect <= cpu_data_in[7:6]; // $A001-$BFFF, odd
3'b100: irq_latch <= cpu_data_in; // $C000-$DFFE, even
3'b101: irq_reload <= 1; // $C001-$DFFF, odd
3'b110: irq_enabled <= 0; // $E000-$FFFE, even
3'b111: irq_enabled <= 1; // $E001-$FFFF, odd
endcase
end
if (irq_reload_clear)
irq_reload <= 0;
end
// PRG banking
always @ (*)
begin
case ({cpu_addr_in[14:13], prg_mode})
3'b000: cpu_addr_out[18:13] <= r[6][5:0];
3'b001: cpu_addr_out[18:13] <= 6'b111110;
3'b010,
3'b011: cpu_addr_out[18:13] <= r[7][5:0];
3'b100: cpu_addr_out[18:13] <= 6'b111110;
3'b101: cpu_addr_out[18:13] <= r[6][5:0];
default: cpu_addr_out[18:13] <= 6'b111111;
endcase
cpu_addr_out[12] <= cpu_addr_in[12];
end
// CHR banking
always @ (*)
begin
if (ppu_addr_in[12] == chr_mode)
ppu_addr_out[17:10] <= {r[ppu_addr_in[11]][7:1], ppu_addr_in[10]};
else
ppu_addr_out[17:10] <= r[2+ppu_addr_in[11:10]];
ppu_addr_out[18] <= 0;
end
// Reenable IRQ only when PPU A12 is low
always @ (*)
begin
if (!irq_enabled)
begin
irq_ready = 0;
irq <= 1'bZ;
end else if (irq_enabled && !irq_value)
irq_ready = 1;
else if (irq_ready && irq_value)
irq <= 1'b0;
end
// IRQ counter
always @ (posedge ppu_addr_in[12])
begin
if (a12_low_time == 3)
begin
//irq_counter_last = irq_counter;
if ((irq_reload && !irq_reload_clear) || (irq_counter == 0))
begin
irq_counter = irq_latch;
if (irq_reload) irq_reload_clear <= 1;
end else
irq_counter = irq_counter-1;
if (irq_counter == 0 && irq_enabled)
irq_value = 1;
else
irq_value = 0;
end
if (!irq_reload) irq_reload_clear <= 0;
end
// A12 must be low for 3 rises of M2
always @ (posedge m2, posedge ppu_addr_in[12])
begin
if (ppu_addr_in[12])
a12_low_time <= 0;
else if (a12_low_time < 3)
a12_low_time <= a12_low_time + 1;
end
endmodule