Skip to content

Commit 1944d97

Browse files
committed
Refactor gamepad input handling in dino_game_top and ai_controller modules
1 parent b2562a2 commit 1944d97

File tree

3 files changed

+46
-325
lines changed

3 files changed

+46
-325
lines changed

src/ai_controller.v

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
module ai_controller
44
#( parameter CONV = 0, parameter GEN_LINE = 250, parameter PLAYER_OFFSET = 6, parameter OBSTACLE_TRESHOLD = 30 )
55
(
6-
input clk,
7-
input rst_n,
8-
input gamepad_is_present,
9-
input gamepad_up,
10-
input [9:CONV] obstacle1_pos,
11-
input [9:CONV] obstacle2_pos,
12-
input crash,
6+
input wire clk,
7+
input wire rst_n,
8+
input wire gamepad_is_present,
9+
input wire gamepad_start,
10+
input wire gamepad_up,
11+
input wire gamepad_down,
12+
input wire [9:CONV] obstacle1_pos,
13+
input wire [9:CONV] obstacle2_pos,
14+
input wire crash,
15+
output reg button_start,
1316
output reg button_up,
14-
output reg crash_out
17+
output reg button_down
1518
);
1619

1720
localparam RESTART_DELAY = 60; // Clock cycles to wait after crash to restart
@@ -21,30 +24,23 @@ reg [7:0] restart_counter;
2124

2225
always @(posedge clk) begin
2326
if (!rst_n) begin
27+
button_start <= 1'b0;
2428
button_up <= 1'b0;
25-
crash_out <= 1'b0;
26-
restart_counter <= 'b0;
29+
button_down <= 1'b0;
30+
restart_counter <= 8'b00000000;
2731
end else begin
2832
if (gamepad_is_present) begin
33+
button_start <= gamepad_start;
2934
button_up <= gamepad_up;
30-
crash_out <= crash;
35+
button_down <= gamepad_down;
36+
end else if (crash) begin
37+
button_start <= 1'b1;
38+
button_up <= 1'b0;
39+
button_down <= 1'b0;
40+
end else if ((obstacle1_pos <= OBSTACLE_TRESHOLD && obstacle1_pos > PLAYER_OFFSET) || (obstacle2_pos <= OBSTACLE_TRESHOLD && obstacle2_pos > PLAYER_OFFSET)) begin
41+
button_up <= 1'b1;
3142
end else begin
32-
if (crash_out) begin
33-
restart_counter <= restart_counter + 1;
34-
if (restart_counter == RESTART_DELAY) begin
35-
crash_out <= 1'b0;
36-
button_up <= 1'b1;
37-
restart_counter <= 'b0;
38-
end
39-
end else if (crash) begin
40-
crash_out <= 1'b1;
41-
end else begin
42-
if ((obstacle1_pos <= OBSTACLE_TRESHOLD && obstacle1_pos > PLAYER_OFFSET) || (obstacle2_pos <= OBSTACLE_TRESHOLD && obstacle2_pos > PLAYER_OFFSET)) begin
43-
button_up <= 1'b1;
44-
end else begin
45-
button_up <= 1'b0;
46-
end
47-
end
43+
button_up <= 1'b0;
4844
end
4945
end
5046
end

src/dino_game_top.v

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
2424

2525
// GAME STATE SIGNALS
2626
wire crash; // set to 1'b1 by rendering when collision occurs
27-
wire crash_out;
2827
wire [5:0] player_position;
2928
wire game_start_pulse;
3029
wire game_over_pulse;
@@ -65,6 +64,10 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
6564
wire gamepad_l;
6665
wire gamepad_r;
6766

67+
wire button_start;
68+
wire button_up;
69+
wire button_down;
70+
6871
// Synchronizes pmod_data, pmod_clk, pmod_latch signals to system clock
6972
// domain.
7073
gamepad_pmod_single gamepad_pmod (
@@ -79,7 +82,7 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
7982
.is_present(gamepad_is_present),
8083
.up(gamepad_up),
8184
.down(gamepad_down),
82-
.start(gamepad_up),
85+
.start(gamepad_start),
8386
.b(gamepad_b),
8487
.y(gamepad_y),
8588
.select(gamepad_select),
@@ -95,10 +98,10 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
9598
.clk(clk),
9699
.rst_n(rst_n),
97100
.game_tick(game_tick_20hz),
98-
.button_start(button_up),
101+
.button_start(button_start),
99102
.button_up(button_up),
100-
.button_down(gamepad_down),
101-
.crash(crash_out),
103+
.button_down(button_down),
104+
.crash(crash),
102105
.player_position(player_position),
103106
.game_frozen(game_frozen),
104107
.game_start_pulse(game_start_pulse),
@@ -289,12 +292,15 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
289292
.clk(clk),
290293
.rst_n(rst_n),
291294
.gamepad_is_present(gamepad_is_present),
295+
.gamepad_start(gamepad_start),
292296
.gamepad_up(gamepad_up),
297+
.gamepad_down(gamepad_down),
293298
.obstacle1_pos(obstacle1_pos),
294299
.obstacle2_pos(obstacle2_pos),
295300
.crash(crash),
301+
.button_start(button_start),
296302
.button_up(button_up),
297-
.crash_out(crash_out)
303+
.button_down(button_down)
298304
);
299305

300306
// TinyVGA PMOD

0 commit comments

Comments
 (0)