Skip to content

Commit d28805b

Browse files
committed
Add Gamepad PMOD and AI controller
1 parent 76048ae commit d28805b

File tree

3 files changed

+403
-1
lines changed

3 files changed

+403
-1
lines changed

src/ai_controller.v

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
`default_nettype none
2+
3+
module ai_controller
4+
#( parameter CONV = 0, parameter GEN_LINE = 250, parameter PLAYER_OFFSET = 6, parameter OBSTACLE_TRESHOLD = 30 )
5+
(
6+
input clk,
7+
input rst_n,
8+
input [9:CONV] obstacle1_pos,
9+
input [9:CONV] obstacle2_pos,
10+
input crash,
11+
output reg button_up,
12+
output reg crash_out
13+
);
14+
15+
localparam RESTART_DELAY = 60; // Clock cycles to wait after crash to restart
16+
17+
// reg [9:CONV] obstacle_threshold; // When an obstacle reaches this xpos, set button_up signal
18+
reg [7:0] restart_counter;
19+
20+
always @(posedge clk) begin
21+
if (!rst_n) begin
22+
button_up <= 1'b0;
23+
crash_out <= 1'b0;
24+
restart_counter <= 'b0;
25+
end else begin
26+
if (crash_out) begin
27+
restart_counter <= restart_counter + 1;
28+
if (restart_counter == RESTART_DELAY) begin
29+
crash_out <= 1'b0;
30+
button_up <= 1'b1;
31+
restart_counter <= 'b0;
32+
end
33+
end else if (crash) begin
34+
crash_out <= 1'b1;
35+
end else begin
36+
if ((obstacle1_pos <= OBSTACLE_TRESHOLD && obstacle1_pos > PLAYER_OFFSET) || (obstacle2_pos <= OBSTACLE_TRESHOLD && obstacle2_pos > PLAYER_OFFSET)) begin
37+
button_up <= 1'b1;
38+
end else begin
39+
button_up <= 1'b0;
40+
end
41+
end
42+
end
43+
end
44+
45+
46+
47+
endmodule

src/dino_game_top.v

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,50 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
6363
.lfsr_data(rng)
6464
);
6565

66+
// Gamepad Pmod support
67+
wire gamepad_pmod_latch = ui_in[4];
68+
wire gamepad_pmod_clk = ui_in[5];
69+
wire gamepad_pmod_data = ui_in[6];
70+
wire gamepad_is_present; // HIGH when gamepad is connected
71+
wire gamepad_up;
72+
wire gamepad_down;
73+
wire gamepad_start; // Can leverage start, select from SNES
74+
wire gamepad_b;
75+
wire gamepad_y;
76+
wire gamepad_select;
77+
wire gamepad_left;
78+
wire gamepad_right;
79+
wire gamepad_a;
80+
wire gamepad_x;
81+
wire gamepad_l;
82+
wire gamepad_r;
83+
84+
// Synchronizes pmod_data, pmod_clk, pmod_latch signals to system clock
85+
// domain.
86+
gamepad_pmod_single gamepad_pmod (
87+
// Inputs:
88+
.clk(clk),
89+
.rst_n(rst_n),
90+
.pmod_latch(gamepad_pmod_latch),
91+
.pmod_clk(gamepad_pmod_clk),
92+
.pmod_data(gamepad_pmod_data),
93+
94+
// Outputs:
95+
.is_present(gamepad_is_present),
96+
.up(gamepad_up),
97+
.down(gamepad_down),
98+
.start(gamepad_start),
99+
.b(gamepad_b),
100+
.y(gamepad_y),
101+
.select(gamepad_select),
102+
.left(gamepad_left),
103+
.right(gamepad_right),
104+
.a(gamepad_a),
105+
.x(gamepad_x),
106+
.l(gamepad_l),
107+
.r(gamepad_r)
108+
);
109+
66110
player_controller player_constroller_inst (
67111
.clk(clk),
68112
.rst_n(rst_n),
@@ -79,7 +123,7 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
79123
.game_state(game_state)
80124
);
81125

82-
obstacles #(.GEN_LINE(71), .CONV(CONV)) obstacles_inst (
126+
obstacles #(.GEN_LINE(71), .CONV(CONV)) obstacles_inst (
83127
.clk(clk),
84128
.rst_n(rst_n),
85129
.game_frozen(game_frozen),

0 commit comments

Comments
 (0)