-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_top.v
309 lines (287 loc) · 6.57 KB
/
demo_top.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 15:41:41 10/21/2012
// Design Name:
// Module Name: demo_top
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
`define north 4'b1000
`define northeast 4'b1010
`define northwest 4'b1001
`define south 4'b0100
`define southeast 4'b0110
`define southwest 4'b0101
`define east 4'b0010
`define west 4'b0001
module demo_top
(input clk_100M, reset,
input [3:0] red_sw,
input [3:0] green_sw,
input [3:0] blue_sw,
input b1, b2, b3, b4, b5,
input up, down, left, right,
output wire [9:0] row, col,
output wire HS, VS,
output wire [3:0] red, green, blue);
wire clk_50M;
wire is_inbounds;
wire [3:0] dir;
wire [3:0] cp_red, cp_green, cp_blue;
reg [3:0] final_red, final_green, final_blue;
wire p1_start, p2_start, coin, fire, special;
freq_divider fd(clk_100M, reset, clk_50M);
vga v(.clk_50M(clk_50M), .reset(reset), .HS(HS), .VS(VS), .row(row), .col(col));
color_picker cp(clk_100M, reset, row, col, cp_red, cp_green, cp_blue, is_inbounds);
assign red = (is_inbounds) ? final_red : 4'b0;
assign green = (is_inbounds) ? final_green : 4'b0;
assign blue = (is_inbounds) ? final_blue : 4'b0;
always @(*) begin
final_red = cp_red;
final_blue = cp_blue;
final_green = cp_green;
case ({dir})
`north:
begin
final_red = cp_red;
final_blue = cp_red;
final_green = cp_red;
end
`northeast:
begin
final_red = cp_red;
final_blue = cp_blue;
final_green = cp_red;
end
`northwest:
begin
final_red = cp_red;
final_blue = cp_red;
final_green = cp_green;
end
`south:
begin
final_red = cp_blue;
final_blue = cp_blue;
final_green = cp_blue;
end
`southeast:
begin
final_red = cp_red;
final_blue = cp_blue;
final_green = cp_blue;
end
`southwest:
begin
final_red = cp_blue;
final_blue = cp_blue;
final_green = cp_green;
end
`east:
begin
final_red = cp_green;
final_blue = cp_green;
final_green = cp_green;
end
`west:
begin
final_red = 4'h0;
final_blue = 4'h0;
final_green = cp_green;
end
default:
begin
case ({fire, special, coin, p1_start, p2_start})
5'b00000:
begin
final_red = cp_red;
final_blue = cp_blue;
final_green = cp_green;
end
default:
begin
final_red = 4'hF;
final_green = 4'hF;
final_blue = 4'hF;
end
endcase
end
endcase
end
joystick_controller jc(
.up(up),
.down(down),
.left(left),
.right(right),
.b1(b1),
.b2(b2),
.b3(b3),
.b4(b4),
.b5(b5),
.clk(clk_100M),
.rst(reset),
.dir(dir), // [3:0]
.p1_start(p1_start),
.p2_start(p2_start),
.coin(coin),
.fire(fire),
.special(special));
endmodule
module color_picker
(
input clk_50M,
input reset,
input [9:0] row, col,
output wire [3:0] red_out, green_out, blue_out,
output wire is_inbounds
);
wire is_c1, is_c2, is_c3, is_c4, is_c5, is_c6, is_c7, is_c8,
is_r1, is_r2, is_r3;
reg [3:0] red, blue, green;
assign red_out = red;
assign green_out = green;
assign blue_out = blue;
assign is_inbounds = (is_c1 | is_c2 | is_c3 | is_c4 | is_c5 | is_c6 | is_c7 | is_c8)
& (is_r1 | is_r2 | is_r3);
always @(posedge clk_50M or negedge reset) begin
if (~reset) begin
red <= 'd0;
blue <= 'd0;
green <= 'd0;
end
else begin
red <= 4'h0;
blue <= 4'h0;
green <= 4'h0;
if (is_r1) begin
if (is_c1) begin
//Grey
red <= 4'h4;
green <= 4'h4;
blue <= 4'h4;
end
else if (is_c2) begin
//Yellow
red <= 4'hF;
green <= 4'hF;
blue <= 4'h0;
end
else if (is_c3) begin
//Cyan
red <= 4'h0;
green <= 4'hF;
blue <= 4'hF;
end
else if (is_c4) begin
//Green
red <= 4'h0;
green <= 4'hF;
blue <= 4'h0;
end
else if (is_c5) begin
//Magenta
red <= 4'hF;
green <= 4'h0;
blue <= 4'hF;
end
else if (is_c6) begin
//Red
red <= 4'hF;
green <= 4'h0;
blue <= 4'h0;
end
else if (is_c7) begin
//Blue
red <= 4'h0;
green <= 4'h0;
blue <= 4'hF;
end
else if (is_c8) begin
//Purple
red <= 4'h8;
green <= 4'h0;
blue <= 4'hF;
end
end
else if (is_r2) begin
if (is_c1) begin
//Blue
red <= 4'h0;
green <= 4'h0;
blue <= 4'hF;
end
else if (is_c2) begin
//Brown
red <= 4'h4;
green <= 4'h2;
blue <= 4'h8;
end
else if (is_c3) begin
//Magenta
red <= 4'hF;
green <= 4'h0;
blue <= 4'hF;
end
else if (is_c4) begin
//Orange
red <= 4'h8;
green <= 4'h4;
blue <= 4'h2;
end
else if (is_c5) begin
// Cyan
red <= 4'h0;
green <= 4'hF;
blue <= 4'hF;
end
else if (is_c6) begin
// Yellow
red <= 4'h9;
green <= 4'h9;
blue <= 4'h5;
end
else if (is_c7) begin
// Dark Brown
red <= 4'h2;
green <= 4'h2;
blue <= 4'h2;
end
else if (is_c8) begin
// Light brown
red <= 4'h3;
green <= 4'h2;
blue <= 4'h4;
end
end
// Color Gradient
else if (is_r3) begin
red <= col[4:0];
green <= col[6:4];
blue <= col[9:6];
end
end
end
range_check #(10) c1(.low(10'd0), .high(10'd299), .val(row), .is_between(is_r1));
range_check #(10) c2(.low(10'd300), .high(10'd379), .val(row), .is_between(is_r2));
range_check #(10) c3(.low(10'd380), .high(10'd500), .val(row), .is_between(is_r3));
range_check #(10) r1(.low(10'd0), .high(10'd79), .val(col), .is_between(is_c1));
range_check #(10) r2(.low(10'd80), .high(10'd159), .val(col), .is_between(is_c2));
range_check #(10) r3(.low(10'd160), .high(10'd239), .val(col), .is_between(is_c3));
range_check #(10) r4(.low(10'd240), .high(10'd319), .val(col), .is_between(is_c4));
range_check #(10) r5(.low(10'd320), .high(10'd399), .val(col), .is_between(is_c5));
range_check #(10) r6(.low(10'd400), .high(10'd479), .val(col), .is_between(is_c6));
range_check #(10) r7(.low(10'd480), .high(10'd559), .val(col), .is_between(is_c7));
range_check #(10) r8(.low(10'd560), .high(10'd639), .val(col), .is_between(is_c8));
endmodule