-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddr_nes.v
334 lines (286 loc) · 7.4 KB
/
ddr_nes.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 04/17/2022 04:35:57 PM
// Design Name:
// Module Name: ddr_nes
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module ddr_nes(
input wire clk, reset,
input wire data, // input data from nes controller to FPGA
output reg latch, nes_clk, // outputs from FPGA to nes controller
output wire A, B, select, start, up, down, left, right // output states of nes controller buttons
);
// FSM symbolic states
localparam [4:0]
latch_en = 4'h0, // assert latch for 12 us
read_A_wait = 4'h1, // read A / wait 6 us
read_B = 4'h2, //4'h2, // read B ...
read_select = 4'h3,
read_start = 4'h4,
read_up = 4'h5,
read_down = 4'h6,
read_left = 4'h7,
read_right = 4'h8,
final_pulse = 4'h9;
// register to count clock cycles to time latch assertion, nes_clk state, and FSM state transitions
reg [10:0] count_reg, count_next;
// FSM state register, and button state regs
reg [3:0] state_reg, state_next;
reg A_reg, B_reg, select_reg, start_reg,
up_reg, down_reg, left_reg, right_reg;
reg A_next, B_next, select_next, start_next,
up_next, down_next, left_next, right_next;
// infer all the registers
always @(posedge clk, posedge reset)
if (reset)
begin
count_reg <= 0;
state_reg <= 0;
A_reg <= 0;
B_reg <= 0;
select_reg <= 0;
start_reg <= 0;
up_reg <= 0;
down_reg <= 0;
left_reg <= 0;
right_reg <= 0;
end
else
begin
count_reg <= count_next;
state_reg <= state_next;
A_reg <= A_next;
B_reg <= B_next;
select_reg <= select_next;
start_reg <= start_next;
up_reg <= up_next;
down_reg <= down_next;
left_reg <= left_next;
right_reg <= right_next;
end
// FSM next-state logic and data path
always@(*)
begin
// defaults
latch = 0;
nes_clk = 0;
count_next = count_reg;
A_next = A_reg;
B_next = B_reg;
select_next = select_reg;
start_next = start_reg;
up_next = up_reg;
down_next = down_reg;
left_next = left_reg;
right_next = right_reg;
state_next = state_reg;
case(state_reg)
latch_en:
begin
// assert latch pin
latch = 1;
// count 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// once 12 us passed
else if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = read_A_wait; // go to read_A_wait state
end
end
read_A_wait:
begin
A_next = data; // read A
if(count_reg < 600) // count clk cycles for 6 us
count_next = count_reg + 1;
// once 6 us passed
else if(count_reg == 600)
begin
count_next = 0; // reset latch_count
state_next = read_B; // go to read_B state
end
end
read_B:
begin
// count clk cycles for 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// nes_clk state
if(count_reg <= 600)
nes_clk = 1;
else if(count_reg > 600)
nes_clk = 0;
// read B
if(count_reg == 600)
B_next = data;
// state over
if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = read_select; // go to read_select state
end
end
read_select:
begin
// count clk cycles for 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// nes_clk state
if(count_reg <= 600)
nes_clk = 1;
else if(count_reg > 600)
nes_clk = 0;
// read select
if(count_reg == 600)
select_next = data;
// state over
if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = read_start; // go to read_start state
end
end
read_start:
begin
// count clk cycles for 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// nes_clk state
if(count_reg <= 600)
nes_clk = 1;
else if(count_reg > 600)
nes_clk = 0;
// read start
if(count_reg == 600)
start_next = data;
// state over
if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = read_up; // go to read_up state
end
end
read_up:
begin
// count clk cycles for 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// nes_clk state
if(count_reg <= 600)
nes_clk = 1;
else if(count_reg > 600)
nes_clk = 0;
// read up
if(count_reg == 600)
up_next = data;
// state over
if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = read_down; // go to read_down state
end
end
read_down:
begin
// count clk cycles for 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// nes_clk state
if(count_reg <= 600)
nes_clk = 1;
else if(count_reg > 600)
nes_clk = 0;
// read down
if(count_reg == 600)
down_next = data;
// state over
if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = read_left; // go to read_left state
end
end
read_left:
begin
// count clk cycles for 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// nes_clk state
if(count_reg <= 600)
nes_clk = 1;
else if(count_reg > 600)
nes_clk = 0;
// read left
if(count_reg == 600)
left_next = data;
// state over
if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = read_right; // go to read_right state
end
end
read_right:
begin
// count clk cycles for 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// nes_clk state
if(count_reg <= 600)
nes_clk = 1;
else if(count_reg > 600)
nes_clk = 0;
// read right
if(count_reg == 600)
right_next = data;
// state over
if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = final_pulse; // go to latch_en state
end
end
final_pulse:
begin
// count clk cycles for 12 us
if(count_reg < 1200)
count_next = count_reg + 1;
// nes_clk state
if(count_reg <= 600)
nes_clk = 1;
else if(count_reg > 600)
nes_clk = 0;
// state over
if(count_reg == 1200)
begin
count_next = 0; // reset latch_count
state_next = latch_en; // go to latch_en state
end
end
endcase
end
// assign outputs, *normally asserted when unpressed
assign A = ~A_reg;
assign B = ~B_reg;
assign select = ~select_reg;
assign start = ~start_reg;
assign up = ~up_reg;
assign down = ~down_reg;
assign left = ~left_reg;
assign right = ~right_reg;
endmodule