-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.v
1458 lines (1163 loc) · 48.3 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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
`include "vga.v"
`include "vga_adapter/vga_pll.v"
`include "vga_adapter/vga_controller.v"
`include "vga_adapter/vga_adapter.v"
`include "ps2keyboard/PS2_Keyboard_Controller.v"
`include "counter.v"
`include "lfsr2.v"
/**
================================================================================
==Main=Test=====================================================================
================================================================================
**/
module main_test ();
// ### Wires. ###
wire clk, reset, rnd_reset;
wire go;
// wire [1:0] rate;
wire draw_scrn_start, draw_scrn_game_over, draw_scrn_game_bg, draw_frog;
wire draw_river_obj_1, draw_river_obj_2, draw_river_obj_3;
wire draw_score, draw_lives;
wire erase_frog;
wire move_objects;
wire draw_pot_obj_1_2, draw_pot_obj_1_3, draw_pot_obj_2_2, draw_pot_obj_2_3, draw_pot_obj_3_2, draw_pot_obj_3_3;
// wire [3:0] score, lives;
wire plot_done;
wire dne_signal_1, dne_signal_2;
wire plot;
wire [8:0] x, y;
wire [2:0] color;
// VGA wires.
wire VGA_CLK, VGA_HS, VGA_VS, VGA_BLANK_N, VGA_SYNC_N;
wire [9:0] VGA_R, VGA_G, VGA_B;
// ### Datapath and control. ###
datapath d0 (
.clk(clk), .reset(reset), .rnd_reset(rnd_reset),
.draw_scrn_start(draw_scrn_start), .draw_scrn_game_over(draw_scrn_game_over),
.draw_scrn_game_bg(draw_scrn_game_bg), .draw_frog(draw_frog),
.draw_river_obj_1(draw_river_obj_1), .draw_river_obj_2(draw_river_obj_2), .draw_river_obj_3(draw_river_obj_3),
.draw_score(draw_score), .draw_lives(draw_lives), .draw_level(draw_level),
.move_objects(move_objects),
.erase_frog(erase_frog),
.reset_game(reset_game),
.draw_pot_obj_1_2(draw_pot_obj_1_2), .draw_pot_obj_1_3(draw_pot_obj_1_3), .draw_pot_obj_2_2(draw_pot_obj_2_2),
.draw_pot_obj_2_3(draw_pot_obj_2_3), .draw_pot_obj_3_2(draw_pot_obj_3_2), .draw_pot_obj_3_3(draw_pot_obj_3_3),
// .rate(rate),
.ld_frog_loc(ld_frog_loc),
// .score(score), .lives(lives),
.frame_tick(frame_tick),
.left(left), .right(right), .up(up), .down(down),
.plot_done(plot_done),
.plot(plot), .x(x), .y(y), .color(color),
.dne_signal_1(dne_signal_1), .dne_signal_2(dne_signal_2),
.win(win), .die(die), .lose(lose)
);
control c0 (
.clk(clk), .reset(reset),
.go(go), .plot_done(plot_done), .space(space),
.win(win), .die(die), .lose(lose),
.dne_signal_1(dne_signal_1), .dne_signal_2(dne_signal_2),
.frame_tick(frame_tick),
.draw_scrn_start(draw_scrn_start), .draw_scrn_game_over(draw_scrn_game_over),
.draw_scrn_game_bg(draw_scrn_game_bg), .draw_frog(draw_frog),
.draw_river_obj_1(draw_river_obj_1), .draw_river_obj_2(draw_river_obj_2), .draw_river_obj_3(draw_river_obj_3),
.draw_score(draw_score), .draw_lives(draw_lives), .draw_level(draw_level),
.move_objects(move_objects),
.erase_frog(erase_frog),
.reset_game(reset_game),
.ld_frog_loc(ld_frog_loc),
.draw_pot_obj_1_2(draw_pot_obj_1_2), .draw_pot_obj_1_3(draw_pot_obj_1_3), .draw_pot_obj_2_2(draw_pot_obj_2_2),
.draw_pot_obj_2_3(draw_pot_obj_2_3), .draw_pot_obj_3_2(draw_pot_obj_3_2), .draw_pot_obj_3_3(draw_pot_obj_3_3),
.current_state(current_state)
);
// ### VGA adapter. ###
vga_adapter #(
.RESOLUTION("320x240"),
.MONOCHROME("FALSE"),
.BITS_PER_COLOUR_CHANNEL(1),
.BACKGROUND_IMAGE("mif_files/black.mif")
) vga (
.clock(clk), .resetn(!reset),
// Controlled signals.
.x(x), .y(y), .colour(color),
.plot(plot),
// VGA DAC signals.
.VGA_CLK(VGA_CLK),
.VGA_HS(VGA_HS), .VGA_VS(VGA_VS), .VGA_BLANK(VGA_BLANK_N), .VGA_SYNC(VGA_SYNC_N),
.VGA_R(VGA_R), .VGA_G(VGA_G), .VGA_B(VGA_B)
);
endmodule // main_test
/**
================================================================================
==Main=Test=End=================================================================
================================================================================
**/
/**
================================================================================
==Top=Module====================================================================
================================================================================
**/
module VeriFrogger (
CLOCK_50,
KEY, SW,
PS2_CLK, PS2_DAT,
LEDR, HEX0, HEX1, HEX2, HEX3, HEX4, HEX5,
VGA_CLK, VGA_HS, VGA_VS, VGA_BLANK_N, VGA_SYNC_N, VGA_R, VGA_G, VGA_B
);
// ### FPGA inputs and outputs. ###
input CLOCK_50;
// For auxilary input or debugging.
input [9:0] SW;
input [3:0] KEY;
// For keyboard input and output.
inout PS2_CLK;
inout PS2_DAT;
// For auxiliary output or debugging.
output [9:0] LEDR;
output [6:0] HEX0, HEX1, HEX2, HEX3, HEX4, HEX5;
// VGA DAC signals.
output VGA_CLK, VGA_HS, VGA_VS, VGA_BLANK_N, VGA_SYNC_N;
output [9:0] VGA_R, VGA_G, VGA_B;
// ### Wires. ###
wire clk = CLOCK_50;
wire go_key = !KEY[0];
wire reset = !KEY[3];
wire rnd_reset = !KEY[1];
wire enable = !KEY[2];
// ### Testing. ###
// wire [3:0] score = SW[3:0];
// wire [3:0] lives = SW[7:4];
// wire [1:0] rate = SW[9:8];
reg go;
wire draw_scrn_start, draw_scrn_game_over, draw_scrn_game_bg, draw_frog;
wire draw_river_obj_1, draw_river_obj_2, draw_river_obj_3;
wire draw_score, draw_lives;
wire erase_frog;
wire move_objects;
wire draw_pot_obj_1_2, draw_pot_obj_1_3, draw_pot_obj_2_2, draw_pot_obj_2_3, draw_pot_obj_3_2, draw_pot_obj_3_3;
wire plot_done;
wire dne_signal_1, dne_signal_2;
wire plot;
wire [8:0] x, y;
wire [2:0] color;
wire win, die, lose;
// VGA wires.
wire VGA_CLK, VGA_HS, VGA_VS, VGA_BLANK_N, VGA_SYNC_N;
wire [9:0] VGA_R, VGA_G, VGA_B;
// Keyboard wires.
wire w, a, s, d;
wire up, left, down, right;
wire space, enter;
// wire mov_key_pressed;
// assign mov_key_pressed = w | a | s | d | up | left | down | right;
// The output from the counter for the keyboard.
wire keyboard_clock;
// Since both w, a, s, d and up, left, down, right are the same controls, the wires can be combined.
// wire up_c, left_c, down_c, right_c;
// assign up_c = w || up;
// assign left_c = a || left;
// assign down_c = s || down;
// assign right_c = d || right;
// ### GO button stuff. ###
reg [1:0] go_state, go_next_state;
localparam GS_WAIT_GO = 0,
GS_GO_1 = 1,
GS_GO_2 = 2,
GS_WAIT = 3;
always @ (*) begin
case (go_state)
GS_WAIT_GO:
go_next_state = go_key ? GS_GO_1 : GS_WAIT_GO;
GS_GO_1:
go_next_state = GS_GO_2;
GS_GO_2:
go_next_state = GS_WAIT;
GS_WAIT:
go_next_state = !go_key ? GS_WAIT_GO : GS_WAIT;
endcase
end
always @ (*) begin
go = ((go_state == GS_GO_1) || (go_state == GS_GO_2)) ? 1 : 0;
end
always @ (posedge clk) begin
if (reset)
go_state <= GS_WAIT_GO;
else
go_state <= go_next_state;
end
// ### Debug stuff. ###
wire [4:0] current_state;
// ### Hex displays. ###
hex_dec hd0 (.in(current_state), .out(HEX0));
hex_dec hd1 (.in(go_state), .out(HEX1));
hex_dec hd2 (.in(go_key), .out(HEX2));
assign LEDR[9] = win;
assign LEDR[8] = die;
assign LEDR[3] = up;
assign LEDR[2] = left;
assign LEDR[1] = down;
assign LEDR[0] = right;
// ### Datapath and control. ###
datapath d0 (
.clk(clk), .reset(reset), .rnd_reset(rnd_reset),
.draw_scrn_start(draw_scrn_start), .draw_scrn_game_over(draw_scrn_game_over),
.draw_scrn_game_bg(draw_scrn_game_bg), .draw_frog(draw_frog),
.draw_river_obj_1(draw_river_obj_1), .draw_river_obj_2(draw_river_obj_2), .draw_river_obj_3(draw_river_obj_3),
.draw_score(draw_score), .draw_lives(draw_lives),
.move_objects(move_objects),
.erase_frog(erase_frog),
.draw_pot_obj_1_2(draw_pot_obj_1_2), .draw_pot_obj_1_3(draw_pot_obj_1_3), .draw_pot_obj_2_2(draw_pot_obj_2_2),
.draw_pot_obj_2_3(draw_pot_obj_2_3), .draw_pot_obj_3_2(draw_pot_obj_3_2), .draw_pot_obj_3_3(draw_pot_obj_3_3),
// .rate(rate),
.ld_frog_loc(ld_frog_loc),
// .score(score), .lives(lives),
.frame_tick(frame_tick),
.left(left), .right(right), .up(up), .down(down),
.plot_done(plot_done),
.plot(plot), .x(x), .y(y), .color(color),
.dne_signal_1(dne_signal_1), .dne_signal_2(dne_signal_2),
.win(win), .die(die), .lose(lose)
);
control c0 (
.clk(clk), .reset(reset),
.go(go), .plot_done(plot_done), .space(space),
.win(win), .die(die), .lose(lose),
.dne_signal_1(dne_signal_1), .dne_signal_2(dne_signal_2),
.frame_tick(frame_tick),
.draw_scrn_start(draw_scrn_start), .draw_scrn_game_over(draw_scrn_game_over),
.draw_scrn_game_bg(draw_scrn_game_bg), .draw_frog(draw_frog),
.draw_river_obj_1(draw_river_obj_1), .draw_river_obj_2(draw_river_obj_2), .draw_river_obj_3(draw_river_obj_3),
.draw_score(draw_score), .draw_lives(draw_lives), .move_objects(move_objects),
.erase_frog(erase_frog),
.ld_frog_loc(ld_frog_loc),
.draw_pot_obj_1_2(draw_pot_obj_1_2), .draw_pot_obj_1_3(draw_pot_obj_1_3), .draw_pot_obj_2_2(draw_pot_obj_2_2),
.draw_pot_obj_2_3(draw_pot_obj_2_3), .draw_pot_obj_3_2(draw_pot_obj_3_2), .draw_pot_obj_3_3(draw_pot_obj_3_3),
.current_state(current_state)
);
// ### VGA adapter. ###
vga_adapter #(
.RESOLUTION("320x240"),
.MONOCHROME("FALSE"),
.BITS_PER_COLOUR_CHANNEL(1),
.BACKGROUND_IMAGE("mif_files/black.mif")
) vga (
.clock(clk), .resetn(!reset),
// Controlled signals.
.x(x), .y(y), .colour(color),
.plot(plot),
// VGA DAC signals.
.VGA_CLK(VGA_CLK),
.VGA_HS(VGA_HS), .VGA_VS(VGA_VS), .VGA_BLANK(VGA_BLANK_N), .VGA_SYNC(VGA_SYNC_N),
.VGA_R(VGA_R), .VGA_G(VGA_G), .VGA_B(VGA_B)
);
// ### Keyboard tracker. ###
keyboard_tracker #(.PULSE_OR_HOLD(0)) tester(
.clock(CLOCK_50),
.reset(!reset),
.PS2_CLK(PS2_CLK),
.PS2_DAT(PS2_DAT),
.w(up),
.a(left),
.s(down),
.d(right),
.left(left),
.right(right),
.up(up),
.down(down),
.space(space),
.enter(enter)
);
// keyboard_tracker k0 (
// .clock(clk), .reset(!reset),
//
// .PS2_CLK(PS2_CLK), .PS2_DAT(PS2_DAT),
//
// .w(w), .a(a), .s(s), .d(d),
// .left(left), .right(right), .up(up), .down(down),
// .space(space), .enter(enter)
// );
endmodule // top
/**
================================================================================
==Top=Module=End================================================================
================================================================================
**/
/**
================================================================================
==Datapath======================================================================
================================================================================
**/
module datapath (
clk, reset, rnd_reset,
draw_scrn_start, draw_scrn_game_over, draw_scrn_game_bg, draw_frog,
draw_river_obj_1, draw_river_obj_2, draw_river_obj_3,
draw_score, draw_lives, draw_level,
move_objects,
erase_frog,
draw_pot_obj_1_2, draw_pot_obj_1_3, draw_pot_obj_2_2, draw_pot_obj_2_3, draw_pot_obj_3_2, draw_pot_obj_3_3,
ld_frog_loc,
reset_game,
frame_tick,
left, right, up, down,
plot_done,
plot, x, y, color,
dne_signal_1, dne_signal_2,
win, die, lose
);
// ### Inputs, outputs and wires. ###
input clk, reset, rnd_reset;
input draw_scrn_start, draw_scrn_game_over, draw_scrn_game_bg, draw_frog;
input draw_river_obj_1, draw_river_obj_2, draw_river_obj_3;
input draw_pot_obj_1_2, draw_pot_obj_1_3, draw_pot_obj_2_2, draw_pot_obj_2_3, draw_pot_obj_3_2, draw_pot_obj_3_3;
input draw_score, draw_lives, draw_level;
input move_objects;
input erase_frog;
input ld_frog_loc;
input reset_game;
input left, right, up, down;
output plot_done;
// does not exist signal 1 and 2, alternate usage to prevent timing issues
output reg dne_signal_1, dne_signal_2;
wire plot_done_scrn, plot_done_char, plot_done_river_obj, plot_done_frog;
assign plot_done = plot_done_scrn || plot_done_char || plot_done_river_obj || plot_done_frog;
wire [8:0] next_x_scrn, next_x_char, next_x_river_obj, next_x_frog;
wire [8:0] next_y_scrn, next_y_char, next_y_river_obj, next_y_frog;
output reg [2:0] color;
output plot;
output reg [8:0] x;
output reg [8:0] y;
output win, die, lose;
reg [6:0] rate;
reg [6:0] score, lives;
assign lose = score == 0;
reg pre_plot;
wire is_transparent;
assign plot = pre_plot && !is_transparent;
// ### Top left coordinates of objects in the game ###.
reg [8:0] frog_x, frog_y;
// ### First row of river objects ###.
// x coordinates are 1 bit longer for finer grain rate control
reg [9:0] river_object_1_x, river_object_1_x_2, river_object_1_x_3;
// y coordinates
reg [8:0] river_object_1_y, river_object_1_y_2, river_object_1_y_3;
// ### Second row of river objects ###.
// x coordinates
reg [9:0] river_object_2_x, river_object_2_x_2, river_object_2_x_3;
// y coordinates
reg [8:0]river_object_2_y, river_object_2_y_2, river_object_2_y_3;
// ### Third row of river objects ###.
// x coordinates
reg [9:0] river_object_3_x, river_object_3_x_2, river_object_3_x_3;
// y coordinates
reg [8:0] river_object_3_y, river_object_3_y_2, river_object_3_y_3;
wire draw_pot_obj_1_2, draw_pot_obj_1_3, draw_pot_obj_2_2, draw_pot_obj_2_3, draw_pot_obj_3_2, draw_pot_obj_3_3;
wire draw, draw_scrn, draw_char, draw_river_obj;
assign draw = draw_scrn || draw_char || draw_river_obj || draw_frog || erase_frog;
assign draw_scrn = draw_scrn_start || draw_scrn_game_over || draw_scrn_game_bg;
assign draw_char = draw_score || draw_lives || draw_level;
assign draw_river_obj = draw_river_obj_1 || draw_river_obj_2 || draw_river_obj_3 ||
draw_pot_obj_1_2 || draw_pot_obj_1_3 ||
draw_pot_obj_2_2 || draw_pot_obj_2_3 ||
draw_pot_obj_3_2 || draw_pot_obj_3_3;
// ### Frog collision detection signals. ###
// Center of frog is used for these locations, i.e. x = frog_x + 32 /2, y = frog_y + 24 / 2.
wire on_river;
assign on_river = (frog_y + 32 / 2 > 66) && (frog_y + 32 / 2 < 203); // vertical center of frog within river boundaries
assign win = frog_y < 66 - 24 - 2; // river top boundary - frog height - a few pixels
// Check on which river row the frog is.
wire on_river_object, on_river_row_1, on_river_row_2, on_river_row_3;
assign on_river_row_1 = (frog_y + 32 / 2 > 66) && (frog_y + 32 / 2 <= 111);
assign on_river_row_2 = (frog_y + 32 / 2 > 111) && (frog_y + 32 / 2 <= 158);
assign on_river_row_3 = (frog_y + 32 / 2 > 158) && (frog_y + 32 / 2 < 203);
reg row_1_object_2_exists, row_1_object_3_exists;
reg row_2_object_2_exists, row_2_object_3_exists;
reg row_3_object_2_exists, row_3_object_3_exists;
// Check whether the frog is on a river object and on which row.
wire on_river_object_row_1, on_river_object_row_2, on_river_object_row_3;
assign on_river_object_row_1 = on_river_row_1 && (
(frog_x + 24 / 2 > river_object_1_x[9:1] && frog_x + 24 / 2 < river_object_1_x[9:1] + 96) ||
(row_1_object_2_exists && frog_x + 24 / 2 > river_object_1_x_2[9:1] && frog_x + 24 / 2 < river_object_1_x_2[9:1] + 96) ||
(row_1_object_3_exists && frog_x + 24 / 2 > river_object_1_x_3[9:1] && frog_x + 24 / 2 < river_object_1_x_3[9:1] + 96));
assign on_river_object_row_2 = on_river_row_2 && (
(frog_x + 24 / 2 > river_object_2_x[9:1] && frog_x + 24 / 2 < river_object_2_x[9:1] + 96) ||
(row_2_object_2_exists && frog_x + 24 / 2 > river_object_2_x_2[9:1] && frog_x + 24 / 2 < river_object_2_x_2[9:1] + 96) ||
(row_2_object_3_exists && frog_x + 24 / 2 > river_object_2_x_3[9:1] && frog_x + 24 / 2 < river_object_2_x_3[9:1] + 96));
assign on_river_object_row_3 = on_river_row_3 && (
(frog_x + 24 / 2 > river_object_3_x[9:1] && frog_x + 24 / 2 < river_object_3_x[9:1] + 96) ||
(row_3_object_2_exists && frog_x + 24 / 2 > river_object_3_x_2[9:1] && frog_x + 24 / 2 < river_object_3_x_2[9:1] + 96) ||
(row_3_object_3_exists && frog_x + 24 / 2 > river_object_3_x_3[9:1] && frog_x + 24 / 2 < river_object_3_x_3[9:1] + 96));
assign on_river_object = on_river_object_row_1 || on_river_object_row_2 || on_river_object_row_3;
assign die = on_river && !on_river_object;
// ### Counter to delay the keyboard. ###
wire [20:0] frame_counter;
output frame_tick;
assign frame_tick = frame_counter == 834168;
counter counter0 (
.clk(clk),
.en(1),
.rst(reset),
.out(frame_counter)
);
// LFSR (Linear feedback shift register), outputs a random 13 bit number
// bits [5:0] determine if additional random objects are generated
//
wire [19:0] rnd_generator;
// Used for spawning river objects. Minimum distance is min_dist, max is
// min_dist + 15
wire [19:0] rnd_13_bit_num = rnd_generator;
LFSR #(20)
lfsr0 (
.i_Clk(clk),
.i_Enable(1),
.o_LFSR_Data(rnd_generator),
.o_LFSR_Done(lfsr_done)
);
// get the 4 least significant bit
// used in randomly generating river objects
// ### Timing adjustments. ###
wire [1:0] frog_x_r, frog_x_l, frog_y_d, frog_y_u;
assign frog_x_r = right + rate * on_river_object_row_1 + rate * on_river_object_row_3;
assign frog_x_l = left + rate * on_river_object_row_2;
assign frog_y_d = down;
assign frog_y_u = up;
always @ (posedge clk) begin
// Plot signal, x and y need to be delayed by one clock cycle
// due to delay of retrieving data from memory.
// The x and y offsets specify the top left corner of the sprite
// that is being drawn.
pre_plot <= draw;
// starting coordinates of the frog and river objects
if (reset || reset_game) begin
frog_x <= 320 / 2 - 32 / 2; // spawn frog in middle horizontally
frog_y <= 240 - 24 - 5; // spawn frog a few pixels from the bottom edge
// ### First object on row 1 ###.
river_object_1_x <= 0;
river_object_1_y <= 75;
river_object_1_x << 1 => river_object_1_x; // in case spawn x coordinate is not 0
// reset the data
// initial rate is 0.5 and increments by 0.5 everytime a win signal is triggered
rate <= 1;
lives <= 10;
score <= 0;
// potential river object on row 1
if (rnd_13_bit_num[0] == 1 || rnd_13_bit_num[19] == 1) begin
row_1_object_2_exists <= 1;
// check if the third river object in the row will spawn
// if it will spawn, then place the second river object so that
// it will not overlap with the third
if(rnd_13_bit_num[1] == 1) begin
// 96 (length of one river object) + 10 (minimum distance between two river objects) +
// X (random number), X < 64
river_object_1_x_2 <= 7'b1100000 + 4'b1010 + rnd_13_bit_num[19:14];
// if not, then the second river object can be placed in a wider margin
end else begin
// 96 (length of one river object) + 40 (minimum distance between two river objects
// if there is only two in a row) + X (random number), X < 256
river_object_1_x_2 <= 7'b1100000 + 6'b101000 + rnd_13_bit_num[19:12];
end
river_object_1_y_2 <= 75;
river_object_1_x_2 <= river_object_1_x_2 << 1'b1;
end else begin
row_1_object_2_exists <= 0;
end
// potential river object
if (rnd_13_bit_num[1] == 1) begin
row_1_object_3_exists <= 1;
// 96 + 10 + 63 (6 bit binary number max value) + 96 + 10 + 63 (6 bit binary number max value)
river_object_1_x_3 <= 7'b1100000 + 4'b1010 + 6'b111111 + 7'b1100000 + 4'b1010 + rnd_13_bit_num[14:9];
river_object_1_y_3 <= 75;
river_object_1_x_3 <= river_object_1_x_3 << 1'b1;
end else begin
row_1_object_3_exists <= 0;
end
/** Second row of river object(s) **/
river_object_2_x <= 319; // test spawn value = 120
river_object_2_y <= 115; // test spawn value = 150
river_object_2_x <= river_object_2_x << 1'b1;
// potential river object
if (rnd_13_bit_num[2] == 1 || rnd_13_bit_num[18] == 1) begin
row_2_object_2_exists <= 1;
// check if the third river object in the row will spawn
// if it will spawn, then place the second river object so that
// it will not overlap with the third
if(rnd_13_bit_num[3] == 1) begin
// 96 (length of one river object) + 10 (minimum distance between two river objects) +
// X (random number), X < 64
river_object_2_x_2 <= 319 - 7'b1100000 - 4'b1010 - rnd_13_bit_num[11:6];
// if not, then the second river object can be placed in a wider margin
end else begin
// 96 (length of one river object) + 40 (minimum distance between two river objects
// if there is only two in a row) + X (random number), X < 256
river_object_2_x_2 <= 319 - 7'b1100000 - 6'b101000 - rnd_13_bit_num[17:10];
end
river_object_2_y_2 <= 115;
river_object_2_x_2 <= river_object_2_x_2 << 1'b1;
end else begin
row_2_object_2_exists <= 0;
end
// potential river object
if (rnd_13_bit_num[3] == 1) begin
row_2_object_3_exists <= 1;
river_object_2_x_3 <= 319 - 7'b1100000 + 4'b1010 + rnd_13_bit_num[3:0];
river_object_2_x_3 <= 319 - 7'b1100000 - 4'b1010 - 6'b111111 - 7'b1100000 - 4'b1010 - rnd_13_bit_num[12:7];
river_object_2_y_3 <= 115;
river_object_2_x_3 <= river_object_2_x_3 << 1'b1;
end else begin
row_2_object_3_exists <= 0;
end
/** Third row of river object(s) **/
river_object_3_x <= 0;
river_object_3_y <= 155;
river_object_3_x <= river_object_3_x << 1'b1;
// potential river object
if (rnd_13_bit_num[4] == 1 || rnd_13_bit_num[17] == 1) begin
row_3_object_2_exists <= 1;
// check if the third river object in the row will spawn
// if it will spawn, then place the second river object so that
// it will not overlap with the third
if(rnd_13_bit_num[5] == 1) begin
// 96 (length of one river object) + 10 (minimum distance between two river objects) +
// X (random number), X < 64
river_object_3_x_2 <= 7'b1100000 + 4'b1010 + rnd_13_bit_num[15:10];
// if not, then the second river object can be placed in a wider margin
end else begin
// 96 (length of one river object) + 40 (minimum distance between two river objects
// if there is only two in a row) + X (random number), X < 256
river_object_3_x_2 <= 7'b1100000 + 4'b1010 + 6'b111111 + 7'b1100000 + 4'b1010 + rnd_13_bit_num[15:8];
end
river_object_3_y_2 <= 155;
river_object_3_x_2 <= river_object_3_x_2 << 1'b1;
end else begin
row_3_object_2_exists <= 0;
end
// potential river object
if (rnd_13_bit_num[5] == 1) begin
row_3_object_3_exists <= 1;
river_object_3_x_3 <= 7'b1100000 + 4'b1010 + 6'b111111 + 7'b1100000 + 4'b1010 + rnd_13_bit_num[10:5];
river_object_3_y_3 <= 155;
river_object_3_x_3 <= river_object_3_x_3 << 1'b1;
end else begin
row_3_object_3_exists <= 0;
end
end else if (draw_river_obj_1) begin
// river object 1 flows right at 60 pixels per second
// object only moves horizontally
x <= river_object_1_x[9:1] + next_x_river_obj;
y <= river_object_1_y + next_y_river_obj;
end else if (draw_river_obj_2) begin
// river object 2 flows left at 60 pixels per second
// object only moves horizontally
x <= river_object_2_x[9:1] + next_x_river_obj;
y <= river_object_2_y + next_y_river_obj;
end else if (draw_river_obj_3) begin
// river object 3 flows right at 60 pixels per second
// object only moves horizontally
x <= river_object_3_x[9:1] + next_x_river_obj;
y <= river_object_3_y + next_y_river_obj;
// move the river object for the next frame
end else if (draw_pot_obj_1_2) begin
dne_signal_2 <= 0;
if (row_1_object_2_exists) begin
x <= river_object_1_x_2[9:1] + next_x_river_obj;
y <= river_object_1_y_2 + next_y_river_obj;
end else begin
dne_signal_1 <= 1;
end
end else if (draw_pot_obj_1_3) begin
dne_signal_1 <= 0;
if (row_1_object_3_exists) begin
x <= river_object_1_x_3[9:1] + next_x_river_obj;
y <= river_object_1_y_3 + next_y_river_obj;
end else begin
dne_signal_2 <= 1;
end
end else if (draw_pot_obj_2_2) begin
dne_signal_2 <= 0;
if (row_2_object_2_exists) begin
x <= river_object_2_x_2[9:1] + next_x_river_obj;
y <= river_object_2_y_2 + next_y_river_obj;
end else begin
dne_signal_1 <= 1;
end
end else if (draw_pot_obj_2_3) begin
dne_signal_1 <= 0;
if (row_2_object_3_exists) begin
x <= river_object_2_x_3[9:1] + next_x_river_obj;
y <= river_object_2_y_3 + next_y_river_obj;
end else begin
dne_signal_2 <= 1;
end
end else if (draw_pot_obj_3_2) begin
dne_signal_2 <= 0;
if (row_3_object_2_exists) begin
x <= river_object_3_x_2[9:1] + next_x_river_obj;
y <= river_object_3_y_2 + next_y_river_obj;
end else begin
dne_signal_1 <= 1;
end
end else if (draw_pot_obj_3_3) begin
dne_signal_1 <= 0;
if (row_3_object_3_exists) begin
x <= river_object_3_x_3[9:1] + next_x_river_obj;
y <= river_object_3_y_3 + next_y_river_obj;
end else begin
dne_signal_2 <= 1;
end
end else if (draw_score) begin
x <= 300 + next_x_char;
y <= 8 + next_y_char;
end else if (draw_lives) begin
x <= 300 + next_x_char;
y <= 23 + next_y_char;
end else if (draw_level) begin
x <= 300 + next_x_char;
y <= 38 + next_y_char;
end else if (draw_frog) begin
x <= frog_x + next_x_frog;
y <= frog_y + next_y_frog;
end else if (move_objects) begin
// flows right
river_object_1_x <= river_object_1_x + rate;
// flows left
river_object_2_x <= river_object_2_x - rate;
// flows right
river_object_3_x <= river_object_3_x + rate;
if(row_1_object_2_exists) begin
river_object_1_x_2 <= river_object_1_x_2 + rate;
end
if(row_1_object_3_exists) begin
river_object_1_x_3 <= river_object_1_x_3 + rate;
end
if(row_2_object_2_exists) begin
river_object_2_x_2 <= river_object_2_x_2 - rate;
end
if(row_2_object_3_exists) begin
river_object_2_x_3 <= river_object_2_x_3 - rate;
end
if(row_3_object_2_exists) begin
river_object_3_x_2 <= river_object_3_x_2 + rate;
end
if(row_3_object_3_exists) begin
river_object_3_x_3 <= river_object_3_x_3 + rate;
end
// check left and right boundaries (max x = resolution width - frog width - 1)
if ((frog_x + frog_x_r - frog_x_l >= 0) && (frog_x + frog_x_r - frog_x_l <= 320 - 32 - 1)) begin
// update top left pixel's x coordinate if possible
frog_x <= frog_x + frog_x_r - frog_x_l;
end
// check up and down boundaries (max y = resolution height - frog height - 1)
if ((frog_y + frog_y_d - frog_y_u >= 0) && (frog_y + frog_y_d - frog_y_u <= 240 - 24 - 1)) begin
// update top left pixel's y coordinate if possible
frog_y <= frog_y + frog_y_d - frog_y_u;
end
end else if (draw_scrn_start || draw_scrn_game_over || draw_scrn_game_bg) begin
x <= next_x_scrn;
y <= next_y_scrn;
end else if (win) begin
score <= score + 1;
rate <= rate + 1;
frog_x <= 320 / 2 - 32 / 2; // spawn frog in middle horizontally
frog_y <= 240 - 24 - 5; // spawn frog a few pixels from the bottom edge
end
if (die) begin
lives <= lives - 1;
frog_x <= 320 / 2 - 32 / 2; // spawn frog in middle horizontally
frog_y <= 240 - 24 - 5; // spawn frog a few pixels from the bottom edge
end
end
// if(left && next_x_frog - 1 >= 0) begin
// x <= next_x_frog - 1;
// end else if(right && next_x_frog + 1 <= 288) begin
// x <= next_x_frog + 1;
// end else if(up && next_y_frog - 1 >= 0) begin
// y <= next_y_frog - 1;
// end else if(down && next_y_frog + 1 <= 216) begin
// y <= next_y_frog + 1;
// end
// end else if (erase_frog) begin
// x <= frog_x + next_x_frog;
// y <= frog_y + next_y_frog;
//
// if (ld_frog_loc) begin
// frog_x <= frog_x + right - left;
// frog_y <= frog_y - up + down;
// end
// ### Plotters. ###
plotter #(
.WIDTH_X(9),
.WIDTH_Y(9),
.MAX_X(320),
.MAX_Y(240)
) plt_scrn (
.clk(clk), .en(draw_scrn && !plot_done),
.x(next_x_scrn), .y(next_y_scrn),
.done(plot_done_scrn)
);
plotter #(
.WIDTH_X(9),
.WIDTH_Y(9),
.MAX_X(14),
.MAX_Y(10)
) plt_char (
.clk(clk), .en(draw_char && !plot_done),
.x(next_x_char), .y(next_y_char),
.done(plot_done_char)
);
plotter #(
.WIDTH_X(9),
.WIDTH_Y(9),
.MAX_X(96),
.MAX_Y(30)
) plt_river_obj (
.clk(clk), .en(draw_river_obj && !plot_done),
.x(next_x_river_obj), .y(next_y_river_obj),
.done(plot_done_river_obj)
);
plotter #(
.WIDTH_X(9),
.WIDTH_Y(9),
.MAX_X(32),
.MAX_Y(24)
) plt_frog (
.clk(clk), .en((draw_frog || erase_frog) && !plot_done),
.x(next_x_frog), .y(next_y_frog),
.done(plot_done_frog)
);
// ### Start screen. ###
wire [2:0] scrn_start_color;
sprite_ram_module #(
.WIDTH_X(9),
.WIDTH_Y(9),
.RESOLUTION_X(320),
.RESOLUTION_Y(240),
.MIF_FILE("graphics/game_start.mif")
) srm_scrn_start (
.clk(clk),
.x(next_x_scrn), .y(next_y_scrn),
.color_out(scrn_start_color)
);
// ### Game over screen. ###
wire [2:0] scrn_game_over_color;
sprite_ram_module #(
.WIDTH_X(9),
.WIDTH_Y(9),
.RESOLUTION_X(320),
.RESOLUTION_Y(240),
.MIF_FILE("graphics/game_over.mif")
) srm_scrn_game_over (
.clk(clk),
.x(next_x_scrn), .y(next_y_scrn),
.color_out(scrn_game_over_color)
);
// ### Game background screen. ###
wire [2:0] scrn_game_bg_color;
sprite_ram_module #(
.WIDTH_X(9),
.WIDTH_Y(9),
.RESOLUTION_X(320),
.RESOLUTION_Y(240),
.MIF_FILE("graphics/game_background.mif")
) srm_scrn_game_bg (
.clk(clk),
.x(erase_frog ? frog_x + next_x_frog : next_x_scrn), .y(erase_frog ? frog_y + next_y_frog : next_y_scrn),
.color_out(scrn_game_bg_color)
);
// ### Frog. ###
wire [2:0] frog_color;
sprite_ram_module #(
.WIDTH_X(5),
.WIDTH_Y(5),
.RESOLUTION_X(32),
.RESOLUTION_Y(24),