-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShooter.v.bak
509 lines (452 loc) · 11.5 KB
/
Shooter.v.bak
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
`timescale 1ns / 1ns // `timescale time_unit/time_precision
module Shooter(CLOCK, cursorX, cursorY, clicked,
oT1Data, oT2Data, oT3Data, oT4Data, oT5Data, oT6Data, oGameState);
// Inputs
input CLOCK;
input clicked;
input [9:0] cursorX, cursorY;
// Outputs
output [26:0] oT1Data;
output [26:0] oT2Data;
output [26:0] oT3Data;
output [26:0] oT4Data;
output [26:0] oT5Data;
output [26:0] oT6Data;
output [2:0] oGameState;
// For Rate dividing
parameter [23:0] MAXTIME = 24'd19240;
parameter START_S = 3'b000, PLAY_S = 3'b001, END_S = 3'b010, LOSE_S = 3'b011;
reg [2:0] currentState, nextState;
initial currentState = START_S;
assign oGameState = currentState;
//------------------------------------------------
// TARGET INSTANTIATIONS
//------------------------------------------------
// For reference
parameter DEAD_S = 3'b000, DYING_S = 3'b001, ALIVE1_S = 3'b010, ALIVE2_S = 3'b011;
parameter TARGET_WIDTH = 82, TARGET_HEIGHT = 40;
wire [26:0] T1Data;
reg T1Shot;
initial T1Shot = 0;
Target T1(
.CLOCK(CLOCK),
.shot(T1Shot),
.isGood(1'b0),
.targetData(T1Data[26:0]),
.seed(13'd17));
defparam
T1.MAXTIME = 24'd700000,
T1.FLAP_RATE = 24'd24;
wire [26:0] T2Data;
reg T2Shot;
initial T2Shot = 0;
Target T2(
.CLOCK(CLOCK),
.shot(T2Shot),
.isGood(1'b0),
.targetData(T2Data[26:0]),
.seed(13'd19));
defparam
T2.MAXTIME = 24'd400000,
T2.FLAP_RATE = 24'd26;
wire [26:0] T3Data;
reg T3Shot;
initial T3Shot = 0;
Target T3(
.CLOCK(CLOCK),
.shot(T3Shot),
.isGood(1'b0),
.targetData(T3Data[26:0]),
.seed(13'd27));
defparam
T3.MAXTIME = 24'd500000,
T3.FLAP_RATE = 24'd24;
wire [26:0] T4Data;
reg T4Shot;
initial T4Shot = 0;
Target T4(
.CLOCK(CLOCK),
.shot(T4Shot),
.isGood(1'b0),
.targetData(T4Data[26:0]),
.seed(13'd7));
defparam
T4.MAXTIME = 24'd600000,
T4.FLAP_RATE = 24'd20;
wire [26:0] T5Data;
reg T5Shot;
initial T5Shot = 0;
Target T5(
.CLOCK(CLOCK),
.shot(T5Shot),
.isGood(1'b1), // ISGOOD
.targetData(T5Data[26:0]),
.seed(13'd13));
defparam
T5.MAXTIME = 24'd650000,
T5.FLAP_RATE = 24'd14;
wire [26:0] T6Data;
reg T6Shot;
initial T6Shot = 0;
Target T6(
.CLOCK(CLOCK),
.shot(T6Shot),
.isGood(1'b1), // ISGOOD
.targetData(T6Data[26:0]),
.seed(13'd237));
defparam
T6.MAXTIME = 24'd380000,
T6.FLAP_RATE = 24'd20;
// Assign output data
assign oT1Data[26:0] = T1Data[26:0];
assign oT2Data[26:0] = T2Data[26:0];
assign oT3Data[26:0] = T3Data[26:0];
assign oT4Data[26:0] = T4Data[26:0];
assign oT5Data[26:0] = T5Data[26:0];
assign oT6Data[26:0] = T6Data[26:0];
//------------------------------------------------
// GAME LOGIC
//------------------------------------------------
// State Table
always@(*)
begin
case(currentState)
START_S:
begin
if (clicked == 1) nextState = PLAY_S;
else nextState = START_S;
end
PLAY_S:
begin
// Check to see if the good target was killed
if (T5Data[26:24] == DEAD_S) nextState = LOSE_S;
// Check to see if all bad targets are dead
else if (T1Data[26:24] == DEAD_S && T2Data[26:24] == DEAD_S && T3Data[26:24] == DEAD_S && T4Data[26:24] == DEAD_S) nextState = END_S;
// Continue playing
else nextState = PLAY_S;
end
END_S:
begin
nextState = START_S;
end
LOSE_S:
begin
nextState = START_S;
end
default:
begin
nextState = START_S;
end
endcase
end
// Rate divided move-to-next-state
reg [23:0] rateDivider;
initial rateDivider = 0;
always@(posedge CLOCK)
begin
if (rateDivider == MAXTIME)
begin
rateDivider <= 0;
// Goes to next state
currentState <= nextState;
end
else
rateDivider <= rateDivider + 1;
end
// Check if anything is shot
// If shot, stay shot to make sure the target goes to the dying state
reg hasShot, allowChange;
initial hasShot = 0;
initial allowChange = 0;
always@(posedge CLOCK)
begin
allowChange <= ~clicked;
// Only change hasShot to 1 if the previous value of allowChange was 1
if (clicked == 1 && allowChange == 1)
hasShot <= 1;
// Only allows one shot
else if (hasShot && allowChange == 0)
begin
hasShot <= 0;
if (clicked &&
cursorX > T5Data[20:11] && cursorX < T5Data[20:11] + TARGET_WIDTH &&
cursorY > T5Data[10:1] && cursorY < T5Data[10:1] + TARGET_HEIGHT)
T5Shot <= 1;
else
T5Shot <= T5Shot;
if (clicked &&
cursorX > T4Data[20:11] && cursorX < T4Data[20:11] + TARGET_WIDTH &&
cursorY > T4Data[10:1] && cursorY < T4Data[10:1] + TARGET_HEIGHT)
T4Shot <= 1;
else
T4Shot <= T4Shot;
if (clicked &&
cursorX > T3Data[20:11] && cursorX < T3Data[20:11] + TARGET_WIDTH &&
cursorY > T3Data[10:1] && cursorY < T3Data[10:1] + TARGET_HEIGHT)
T3Shot <= 1;
else
T3Shot <= T3Shot;
if (clicked &&
cursorX > T2Data[20:11] && cursorX < T2Data[20:11] + TARGET_WIDTH &&
cursorY > T2Data[10:1] && cursorY < T2Data[10:1] + TARGET_HEIGHT)
T2Shot <= 1;
else
T2Shot <= T2Shot;
if (clicked &&
cursorX > T1Data[20:11] && cursorX < T1Data[20:11] + TARGET_WIDTH &&
cursorY > T1Data[10:1] && cursorY < T1Data[10:1] + TARGET_HEIGHT)
T1Shot <= 1;
else
T1Shot <= T1Shot;
if (clicked &&
cursorX > T6Data[20:11] && cursorX < T6Data[20:11] + TARGET_WIDTH &&
cursorY > T6Data[10:1] && cursorY < T6Data[10:1] + TARGET_HEIGHT)
T6Shot <= 1;
else
T6Shot <= T6Shot;
end
end
endmodule
module Target(CLOCK, shot, isGood, targetData, seed);
// Inputs
input CLOCK, shot, isGood;
input [12:0] seed;
// For Rate dividing
parameter [23:0] MAXTIME = 24'd1000000;//24'd19240;
parameter [23:0] FLAP_RATE = 24'd10;
// Outputs
output [26:0] targetData;
// State definitions
parameter DOWNRIGHT_S = 3'b000, UPRIGHT_S = 3'b001, UPLEFT_S = 3'b010, DOWNLEFT_S = 3'b011;
parameter DEAD_S = 3'b000, DYING_S = 3'b001, ALIVE1_S = 3'b010, ALIVE2_S = 3'b011;
parameter XMAX = 10'd558, YMAX = 10'd440;
// Directions
reg [2:0] currentDir, nextDir;
initial currentDir = DOWNRIGHT_S;
// States
reg [2:0] currentState, nextState;
initial currentState = ALIVE1_S;
// Position
reg [9:0] X, Y;
initial X = 0;
initial Y = 0;
// Assign encoded outputs
assign targetData[0] = isGood;
assign targetData[10:1] = Y[9:0];
assign targetData[20:11] = X[9:0];
assign targetData[23:21] = currentDir[2:0];
assign targetData[26:24] = currentState[2:0];
//------------------------------------------------
// RANDOM NUMBERS
//------------------------------------------------
wire [12:0] randomNumber1, randomNumber2;
wire [2:0] randomNextDirection;
assign randomNextDirection = randomNumber1 % 4;
wire [3:0] randomDirectionChange;
assign randomDirectionChange = randomNumber2 % 130;
LFSR randomGenerator1(
.seed(seed),
.out(randomNumber1[12:0]),
.enable(1'b1),
.clk(CLOCK),
.reset(1'b0));
LFSR randomGenerator2(
.seed(seed/3),
.out(randomNumber2[12:0]),
.enable(1'b1),
.clk(CLOCK),
.reset(1'b0));
//------------------------------------------------
// STATE
//------------------------------------------------
// State Table
always@(*)
begin
case(currentState)
DEAD_S:
begin
nextState = DEAD_S;
end
DYING_S:
begin
nextState = DEAD_S;
end
ALIVE1_S:
begin
if (shot == 1) nextState = DYING_S;
else nextState = ALIVE2_S;
end
ALIVE2_S:
begin
if (shot == 1) nextState = DYING_S;
else nextState = ALIVE1_S;
end
default:
begin
nextState = ALIVE2_S;
end
endcase
end
// State rate divider
reg [23:0] stateRateDivide;
initial stateRateDivide = 0;
reg [23:0] stateFrameCount;
initial stateFrameCount = 0;
always@(posedge CLOCK)
begin
// Nested rate divider in rate divider
// Counts clock ticks
if (stateRateDivide == MAXTIME)
begin
stateRateDivide <= 0;
// Counts frames
if (stateFrameCount == FLAP_RATE)
begin
stateFrameCount <= 0;
// Goes to next state once every 60 frames
currentState <= nextState;
end
else
stateFrameCount <= stateFrameCount + 1;
end
else
stateRateDivide <= stateRateDivide + 1;
end
//------------------------------------------------
// DIRECTION
//------------------------------------------------
// Direction state table
always@(*)
begin
case(currentDir)
DOWNRIGHT_S:
begin
if(Y == YMAX) nextDir = UPRIGHT_S;
else if(X == XMAX) nextDir = DOWNLEFT_S;
else if (randomDirectionChange == 3) nextDir = randomNextDirection;
else nextDir = DOWNRIGHT_S;
end
UPRIGHT_S:
begin
if(Y <= 2) nextDir = DOWNRIGHT_S;
else if(X == XMAX) nextDir = UPLEFT_S;
else if (randomDirectionChange == 2) nextDir = randomNextDirection;
else nextDir = UPRIGHT_S;
end
UPLEFT_S:
begin
if(Y <= 2) nextDir = DOWNLEFT_S;
else if(X <= 2) nextDir = UPRIGHT_S;
else if (randomDirectionChange == 3) nextDir = randomNextDirection;
else nextDir = UPLEFT_S;
end
DOWNLEFT_S:
begin
if(Y == YMAX) nextDir = UPLEFT_S;
else if(X <= 2) nextDir = DOWNRIGHT_S;
else if (randomDirectionChange == 4) nextDir = randomNextDirection;
else nextDir = DOWNLEFT_S;
end
endcase
end
// State rate divider
reg [23:0] directionRateDivide;
initial directionRateDivide = 0;
always@(posedge CLOCK)
begin
if (directionRateDivide == MAXTIME)
begin
directionRateDivide <= 0;
// Goes to next direction
currentDir <= nextDir;
// Moves X and Y coordinate
if (currentState == DEAD_S || currentState == DYING_S)
begin
// Do nothing if dead or dying
X <= X;
Y <= Y;
end
else
begin
// Actually move X and Y if not dead or dying
case(currentDir)
DOWNRIGHT_S:
begin
X <= X + 1;
Y <= Y + 1;
end
UPRIGHT_S:
begin
X <= X + 1;
Y <= Y - 1;
end
UPLEFT_S:
begin
X <= X - 1;
Y <= Y - 1;
end
DOWNLEFT_S:
begin
X <= X - 1;
Y <= Y + 1;
end
endcase
end
end
else
directionRateDivide <= directionRateDivide + 1;
end
endmodule
// Decent random number generator outputs every 13 clock ticks
module LFSR(seed, out, enable, clk, reset);
// Output
output reg [12:0] out;
initial out = 0;
// Inputs
input [12:0] seed;
input enable, clk, reset;
// Shift Register reg
reg [12:0] shiftReg;
initial shiftReg = 13'b0;
// Feedback logic reg
reg linear_feedback;
initial linear_feedback = 0;
// Other reg's
reg hasInitialized;
initial hasInitialized = 0;
reg [4:0] count;
initial count = 0;
// Always block
always@(posedge clk)
begin
// active high reset
if (reset)
begin
shiftReg <= 13'b0;
end
else if (enable)
begin
// Initialize with a seed
if (shiftReg == 13'b0 && hasInitialized == 0)
begin
shiftReg <= seed;
hasInitialized <= 1;
end
else
begin
// Feedback xors bit [7] and [3]
linear_feedback <= (shiftReg[7] ^ shiftReg[3]);
// Does shift
shiftReg <= {shiftReg[11:0], linear_feedback};
// Update the output every 13 shifts
if (count == 12)
begin
out <= shiftReg;
count <= 0;
end
else
count <= count + 1;
end
end
end
endmodule