Skip to content

Commit b2562a2

Browse files
authored
Merge branch 'main' into shaders
2 parents 255de65 + 7bbfdd1 commit b2562a2

File tree

5 files changed

+718
-31
lines changed

5 files changed

+718
-31
lines changed

info.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ project:
4040
- "bg_line.v"
4141
- "priority_encoder.v"
4242
- "color_decoder.v"
43+
- "ai_controller.v"
4344

4445

4546
# The pinout of your project. Leave unused pins blank. DO NOT delete or add any pins.

src/ai_controller.v

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 gamepad_is_present,
9+
input gamepad_up,
10+
input [9:CONV] obstacle1_pos,
11+
input [9:CONV] obstacle2_pos,
12+
input crash,
13+
output reg button_up,
14+
output reg crash_out
15+
);
16+
17+
localparam RESTART_DELAY = 60; // Clock cycles to wait after crash to restart
18+
19+
// reg [9:CONV] obstacle_threshold; // When an obstacle reaches this xpos, set button_up signal
20+
reg [7:0] restart_counter;
21+
22+
always @(posedge clk) begin
23+
if (!rst_n) begin
24+
button_up <= 1'b0;
25+
crash_out <= 1'b0;
26+
restart_counter <= 'b0;
27+
end else begin
28+
if (gamepad_is_present) begin
29+
button_up <= gamepad_up;
30+
crash_out <= crash;
31+
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
48+
end
49+
end
50+
end
51+
52+
53+
54+
endmodule

src/dino_game_top.v

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,11 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
2020
wire [1:0] game_tick_20hz; // two consecutive pulses generated ([0] and then [1]), enabling pipelining
2121

2222
wire debounce_countdown_en; // pulse on rising edge of 5th vpos bit
23-
wire button_up;
24-
wire button_down;
2523

26-
button_debounce button_up_debounce (
27-
.clk(clk),
28-
.rst_n(rst_n),
29-
.countdown_en(debounce_countdown_en),
30-
.button_in(ui_in[0]),
31-
.button_out(button_up)
32-
);
33-
34-
button_debounce button_down_debounce (
35-
.clk(clk),
36-
.rst_n(rst_n),
37-
.countdown_en(debounce_countdown_en),
38-
.button_in(ui_in[1]),
39-
.button_out(button_down)
40-
);
4124

4225
// GAME STATE SIGNALS
4326
wire crash; // set to 1'b1 by rendering when collision occurs
27+
wire crash_out;
4428
wire [5:0] player_position;
4529
wire game_start_pulse;
4630
wire game_over_pulse;
@@ -63,14 +47,58 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
6347
.lfsr_data(rng)
6448
);
6549

50+
// Gamepad Pmod support
51+
wire gamepad_pmod_latch = ui_in[4];
52+
wire gamepad_pmod_clk = ui_in[5];
53+
wire gamepad_pmod_data = ui_in[6];
54+
wire gamepad_is_present; // HIGH when gamepad is connected
55+
wire gamepad_up;
56+
wire gamepad_down;
57+
wire gamepad_start; // Can leverage start, select from SNES
58+
wire gamepad_b;
59+
wire gamepad_y;
60+
wire gamepad_select;
61+
wire gamepad_left;
62+
wire gamepad_right;
63+
wire gamepad_a;
64+
wire gamepad_x;
65+
wire gamepad_l;
66+
wire gamepad_r;
67+
68+
// Synchronizes pmod_data, pmod_clk, pmod_latch signals to system clock
69+
// domain.
70+
gamepad_pmod_single gamepad_pmod (
71+
// Inputs:
72+
.clk(clk),
73+
.rst_n(rst_n),
74+
.pmod_latch(gamepad_pmod_latch),
75+
.pmod_clk(gamepad_pmod_clk),
76+
.pmod_data(gamepad_pmod_data),
77+
78+
// Outputs:
79+
.is_present(gamepad_is_present),
80+
.up(gamepad_up),
81+
.down(gamepad_down),
82+
.start(gamepad_up),
83+
.b(gamepad_b),
84+
.y(gamepad_y),
85+
.select(gamepad_select),
86+
.left(gamepad_left),
87+
.right(gamepad_right),
88+
.a(gamepad_a),
89+
.x(gamepad_x),
90+
.l(gamepad_l),
91+
.r(gamepad_r)
92+
);
93+
6694
player_controller player_constroller_inst (
6795
.clk(clk),
6896
.rst_n(rst_n),
6997
.game_tick(game_tick_20hz),
7098
.button_start(button_up),
7199
.button_up(button_up),
72-
.button_down(button_down),
73-
.crash(crash),
100+
.button_down(gamepad_down),
101+
.crash(crash_out),
74102
.player_position(player_position),
75103
.game_frozen(game_frozen),
76104
.game_start_pulse(game_start_pulse),
@@ -79,7 +107,7 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
79107
.game_state(game_state)
80108
);
81109

82-
obstacles #(.GEN_LINE(71), .CONV(CONV)) obstacles_inst (
110+
obstacles #(.GEN_LINE(71), .CONV(CONV)) obstacles_inst (
83111
.clk(clk),
84112
.rst_n(rst_n),
85113
.game_frozen(game_frozen),
@@ -243,7 +271,7 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
243271
ScoreModule score_module_inst (
244272
.game_start(game_start_pulse),
245273
.game_frozen(game_frozen),
246-
.game_tick(game_tick_20hz[0]),
274+
.game_tick(game_tick_60hz),
247275
.clk(clk), // clock
248276
.rst_n(rst_n), // reset_n - low to reset
249277
.score(score)
@@ -256,6 +284,18 @@ module tt_um_uwasic_dinogame #(parameter CONV = 2) (
256284
.jump_pulse(jump_pulse),
257285
.sound(uio_out[7])
258286
);
287+
288+
ai_controller #(.CONV(CONV)) ai_controller_inst(
289+
.clk(clk),
290+
.rst_n(rst_n),
291+
.gamepad_is_present(gamepad_is_present),
292+
.gamepad_up(gamepad_up),
293+
.obstacle1_pos(obstacle1_pos),
294+
.obstacle2_pos(obstacle2_pos),
295+
.crash(crash),
296+
.button_up(button_up),
297+
.crash_out(crash_out)
298+
);
259299

260300
// TinyVGA PMOD
261301
assign uo_out = {hsync, B[0], G[0], R[0], vsync, B[1], G[1], R[1]};

0 commit comments

Comments
 (0)