-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.v
392 lines (354 loc) · 9.31 KB
/
main.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
SW9 = RESET - ON HIGH
PLOTS ON LOW
*/
module main(
CLOCK_50, // On Board 50 MHz
KEY,
SW,
LEDR,
HEX0,
HEX1,
HEX2,
HEX3,
HEX4,
HEX5,
PS2_CLK,
PS2_DAT,
// The ports below are for the VGA output. Do not change.
VGA_CLK, // VGA Clock
VGA_HS, // VGA H_SYNC
VGA_VS, // VGA V_SYNC
VGA_BLANK_N, // VGA BLANK
VGA_SYNC_N, // VGA SYNC
VGA_R, // VGA Red[9:0]
VGA_G, // VGA Green[9:0]
VGA_B // VGA Blue[9:0]
);
input CLOCK_50; // 50 MHz
input [9:0] SW;
input [3:0] KEY;
inout PS2_CLK;
inout PS2_DAT;
output [9:0] LEDR;
output [6:0] HEX0;
output [6:0] HEX1;
output [6:0] HEX2;
output [6:0] HEX3;
output [6:0] HEX4;
output [6:0] HEX5;
output VGA_CLK; // VGA Clock
output VGA_HS; // VGA H_SYNC
output VGA_VS; // VGA V_SYNC
output VGA_BLANK_N; // VGA BLANK
output VGA_SYNC_N; // VGA SYNC
output [9:0] VGA_R; // VGA Red[9:0]
output [9:0] VGA_G; // VGA Green[9:0]
output [9:0] VGA_B; // VGA Blue[9:0]
reg [9:0] birds = 10'b1111111111;
reg [3:0] birdsAlive = 0;
reg [3:0] birdsDead = 0;
reg [13:0] score = 0;
reg [3:0] round = 0;
reg gameOver = 0;
reg [7:0] regX;
reg [7:0] regY;
reg [23:0] regColour;
reg regEnable;
wire [7:0] X;
wire [7:0] Y;
wire resetn;
wire clk;
wire [23:0] colour;
wire [7:0] XPlayer;
wire [7:0] XBird;
wire [7:0] YPlayer;
wire [7:0] YBird;
wire [3:0] ControlMovement;
wire [3:0] ControlBird;
wire [2:0] ControlFiring;
wire [1:0] RemainingShots;
wire writeEn;
wire DelaySignal;
wire isShot;
wire PorB;
wire gunShot;
wire escape;
wire [3:0] scoreTens;
wire [3:0] scoreHundreds;
wire [3:0] scoreThousands;
wire [1:0] rand;
wire [3:0] movementControls;
assign resetn = ~SW[9];
assign clk = CLOCK_50;
assign gunShot = keyboardInput[8];//SW[0]; // PLAYER SHOOTING CONTROLS
assign movementControls = {keyboardInput[5], keyboardInput[4], keyboardInput[6], keyboardInput[7]}; // PLAYER MOVEMENT CONTROLS
assign scoreOnes = score % 10;
assign scoreTens = (score / 10) % 10;
assign scoreHundreds = (score / 100) % 10;
assign scoreThousands = (score / 1000) % 10;
assign LEDR[9:0] = birds[9:0];
wire [9:0] keyboardInput;
keyboard_interface_test_mode0(
.CLOCK_50(clk),
.KEY(4'b1111),
.PS2_CLK(PS2_CLK),
.PS2_DAT(PS2_DAT),
.LEDR(keyboardInput)
);
always @ (negedge resetn, posedge leave)
begin
if(~resetn)
begin
birds <= 10'b1111111111;
birdsAlive <= 0;
birdsDead <= 0;
score <= 0;
gameOver <= 0;
round <= 0;
end
else if(leave)
begin
if(escape)
begin
birdsDead <= birdsDead + 1;
if(score > 0)
score <= score - 10;
end
else if(isShot)
begin
birds[birdsAlive + birdsDead] = 0;
birdsAlive <= birdsAlive + 1;
score <= score + 50;
end
if(birdsAlive + birdsDead == 9)
begin
if(birdsAlive >= birdsDead && round != 4'b1111)
begin
birds <= 10'b1111111111;
birdsAlive <= 0;
birdsDead <= 0;
round <= round + 1;
end
else
gameOver <= 1;
end
end
end
reg [7:0] xPix = 0;
reg [7:0] yPix = 0;
always @ (posedge clk)
begin
if(gameOver)
begin
regEnable <= 1;
if ((yPix >= 1 && yPix <= 3) && (xPix >= 1 && xPix <= 1 ))
regColour <= 0;
else if ((yPix >= 3 && yPix <= 3 ) && (xPix >= 2 && xPix <= 2 ))
regColour <= 0;
else if ((yPix >= 0 && yPix <= 4 ) && (xPix >= 4 && xPix <= 4 ))
regColour <= 0;
else if ((yPix >= 1 && yPix <= 1 ) && (xPix >= 2 && xPix <= 3 ))
regColour <= 0;
else if ((yPix >= 1 && yPix <= 1 ) && (xPix >= 6 && xPix <= 6 ))
regColour <= 0;
else if ((yPix >= 3 && yPix <= 4 ) && (xPix >= 6 && xPix <= 7 ))
regColour <= 0;
else if ((yPix >= 0 && yPix <= 4 ) && (xPix >= 9 && xPix <= 9 ))
regColour <= 0;
else if ((yPix >= 1 && yPix <= 4 ) && (xPix >= 11 && xPix <= 11 ))
regColour <= 0;
else if ((yPix >= 1 && yPix <= 4 ) && (xPix >= 13 && xPix <= 13 ))
regColour <= 0;
else if ((yPix >= 0 && yPix <= 4 ) && (xPix >= 15 && xPix <= 15 ))
regColour <= 0;
else if ((yPix >= 1 && yPix <= 1 ) && (xPix >= 17 && xPix <= 19 ))
regColour <= 0;
else if ((yPix >= 3 && yPix <= 3 ) && (xPix >= 4 && xPix <= 19 ))
regColour <= 0;
else if ((yPix >= 1 && yPix <= 3 ) && (xPix >= 26 && xPix <= 27 ))
regColour <= 0;
else if ((yPix >= 0 && yPix <= 4 ) && (xPix >= 29 && xPix <= 29 ))
regColour <= 0;
else if ((yPix >= 4 && yPix <= 4 ) && (xPix >= 30 && xPix <= 30 ))
regColour <= 0;
else if ((yPix >= 0 && yPix <= 3 ) && (xPix >= 31 && xPix <= 32 ))
regColour <= 0;
else if ((yPix >= 4 && yPix <= 4 ) && (xPix >= 33 && xPix <= 33 ))
regColour <= 0;
else if ((yPix >= 4 && yPix <= 4 ) && (xPix >= 33 && xPix <= 33 ))
regColour <= 0;
else if ((yPix >= 0 && yPix <= 4 ) && (xPix >= 34 && xPix <= 34 ))
regColour <= 0;
else if ((yPix >= 1 && yPix <= 1 ) && (xPix >= 36 && xPix <= 38 ))
regColour <= 0;
else if ((yPix >= 3 && yPix <= 3 ) && (xPix >= 36 && xPix <= 38 ))
regColour <= 0;
else if ((yPix >= 0 && yPix <= 4 ) && (xPix >= 39 && xPix <= 39 ))
regColour <= 0;
else if ((yPix >= 1 && yPix <= 1 ) && (xPix >= 41 && xPix <= 42 ))
regColour <= 0;
else if ((yPix >= 3 && yPix <= 4 ) && (xPix >= 41 && xPix <= 42 ))
regColour <= 0;
else if ((yPix >= 2 && yPix <= 2 ) && (xPix >= 43 && xPix <= 43 ))
regColour <= 0;
else
regColour <= (255*65536) + (255*256) + 255;
if(xPix >= 43)
begin
xPix <= 75;
if(yPix != 4)
yPix <= yPix + 1;
end
else if(yPix == 5+75)
yPix <= 75;
else
xPix <= xPix + 1;
regX <= xPix +75;
regY <= yPix +75;
//xPix <= xPix + 1;
//pixel <= pixel + 1;
end
else
begin
regX <= X;
regY <= Y;
regColour <= colour;
regEnable <= writeEn;
end
end
RateDivider Pmove(
.clk(clk && ~gameOver),
.reset_n(resetn),
.enable(DelaySignal),
.delay(1666666) //833332; // 1/60 Hz //49999999; // 1 Hz
);
wire oneSec;
wire oneSec2;
wire oneSecSwitch;
RateDivider Rnd3(
.clk(clk),
.reset_n(resetn),
.enable(oneSec),
.delay(74999999)
);
RateDivider Rnd(
.clk(clk),
.reset_n(resetn),
.enable(oneSec2),
.delay(59999999/(round + 1))
);
RateDivider Rnd1(
.clk(clk),
.reset_n(resetn),
.enable(oneSecSwitch),
.delay(35999999/(round + 1))
);
lfsr_updown L0((oneSec2 ^ oneSec) && oneSecSwitch, ~resetn, 1'b1, 1'b1, rand, overflow);
MovementFSM mfsm0(
.clk(clk),
.reset_n(resetn),
.KEY(movementControls),
.STATE(ControlMovement),
.doneDrawing(nextState),
.delayedClk(DelaySignal),
.isShot(isShot),
.outOfAmmo(~|RemainingShots),
.PorB(PorB),
.RandX(rand[0]),
.RandY(rand[1]),
.escape(escape),
.fly(fly),
.fall(fall),
.leave(leave),
.round(round)
);
MovementDatapath mdp0(
.clk(clk),
.reset_n(resetn),
.control(ControlMovement),
.Xout(X),
.Yout(Y),
.Colour(colour),
.plot(writeEn),
.enable(nextState),
.PorB(PorB),
.isShot(isShot),
.XBhold(XBird),
.YBhold(YBird),
.XPhold(XPlayer),
.YPhold(YPlayer),
.fly(fly),
.fall(fall),
.leave(leave)
);
FiringFSM ffsm0(
.clk(DelaySignal),
.reset_n(resetn),
.gunShot(gunShot),
.STATE(ControlFiring),
.leave(leave)
);
FiringDatapath fdp0(
.clk(DelaySignal),
.reset_n(resetn),
.control(ControlFiring),
.RemainingShots(RemainingShots),
.XBird(XBird),
.YBird(YBird),
.XPlayer(XPlayer),
.YPlayer(YPlayer),
.isShot(isShot),
.escape(escape),
.fly(fly),
.fall(fall),
.leave(leave)
);
seg7display s0(
.HEX(HEX0),
.SW({2'b00, RemainingShots})
);
seg7display s1(
.HEX(HEX1),
.SW(round + 1)
);
seg7display s2(
.HEX(HEX2),
.SW(0)
);
seg7display s3(
.HEX(HEX3),
.SW(scoreTens)
);
seg7display s4(
.HEX(HEX4),
.SW(scoreHundreds)
);
seg7display s5(
.HEX(HEX5),
.SW(scoreThousands)
);
// Create an Instance of a VGA controller - there can be only one!
// Define the number of colours as well as the initial background
// image file (.MIF) for the controller.
vga_adapter VGA(
.resetn(resetn),
.clock(clk),
.colour(regColour),
.x(regX),
.y(regY),
.plot(regEnable),
/* Signals for the DAC to drive the monitor. */
.VGA_R(VGA_R),
.VGA_G(VGA_G),
.VGA_B(VGA_B),
.VGA_HS(VGA_HS),
.VGA_VS(VGA_VS),
.VGA_BLANK(VGA_BLANK_N),
.VGA_SYNC(VGA_SYNC_N),
.VGA_CLK(VGA_CLK));
defparam VGA.RESOLUTION = "160x120";
defparam VGA.MONOCHROME = "FALSE";
defparam VGA.BITS_PER_COLOUR_CHANNEL = 8;
defparam VGA.BACKGROUND_IMAGE = "black.mif";
endmodule