-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.v
153 lines (121 loc) · 5 KB
/
process.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
`define paddle_height 16
module process(
input rst,
input clk,
output reg[63:0] fb_col_w_data,
output reg[6:0] fb_col_sel,
output reg fb_write,
input [1:0] move_paddle_1,
input [1:0] move_paddle_2
);
reg[5:0] paddle_1;
reg[5:0] paddle_2;
integer i;
wire input_clk, ball_clk;
clk_div #(.DIVISION_RATE(32'h00A0000)) div_inst(clk, input_clk); //divide clock for user input
clk_div #(.DIVISION_RATE(32'h00A0000)) div_inst2(clk, ball_clk); // divide clock for ball movement
always @(posedge clk or posedge rst) begin //on each clock, update a column of the frame buffer
if(rst) begin
fb_col_sel <= 0;
fb_write <= 0;
end else begin
fb_write = ~fb_write;
fb_col_w_data = 64'h0;
if(fb_write) begin
// draw paddles
if(fb_col_sel == 7'd0 || fb_col_sel == 7'd1 || fb_col_sel == 7'd127) begin
fb_col_w_data = fb_col_w_data | {`paddle_height{1'b1}} << paddle_1;
end else if (fb_col_sel == 7'd124 || fb_col_sel == 7'd125 || fb_col_sel == 7'd126) begin
fb_col_w_data = fb_col_w_data | {`paddle_height{1'b1}} << paddle_2;
end
// draw ball
if(fb_col_sel == ball_x_pos - 1 || fb_col_sel == ball_x_pos || fb_col_sel == ball_x_pos + 1) begin
fb_col_w_data = fb_col_w_data | 3'b111 << (ball_y_pos - 1);
end
fb_col_sel = fb_col_sel + 1;
end
end
end
always @(posedge input_clk or posedge rst) begin //on each input_clk, update the paddle positions
if(rst) begin
paddle_1 <= 0;
paddle_2 <= 0;
end else begin
if(move_paddle_1[0] != 1 ^ move_paddle_1[1] != 1) begin
if (move_paddle_1[0] == 1 && paddle_1 < (64 - `paddle_height)) begin
paddle_1 <= paddle_1 + 1;
end else if (move_paddle_1[1] == 1 && paddle_1 != 0) begin
paddle_1 <= paddle_1 - 1;
end
end
if(move_paddle_2[0] != 1 ^ move_paddle_2[1] != 1) begin
if (move_paddle_2[0] == 1 && paddle_2 < (64 - `paddle_height)) begin
paddle_2 <= paddle_2 + 1;
end else if (move_paddle_2[1] == 1 && paddle_2 != 0) begin
paddle_2 <= paddle_2 - 1;
end
end
end
end
reg[6:0] ball_x_pos;
reg[5:0] ball_y_pos;
reg[2:0] ball_x_velocity;
reg[2:0] ball_y_velocity;
reg ball_x_dir;
reg ball_y_dir;
reg[1:0] start_dir;
always @(posedge ball_clk or posedge rst) begin // on each ball_clk, update the ball position
if(rst) begin
ball_x_velocity <= 3'b001;
ball_y_velocity <= 3'b001;
ball_x_pos <= 7'd64;
ball_y_pos <= 6'd32;
ball_x_dir <= 0;
ball_y_dir <= 0;
start_dir <= 2'b01;
end else begin
// check if ball hits paddle, if so, reverse x direction
if(ball_x_pos == 7'd2 && (ball_y_pos <= (paddle_1 + `paddle_height) && ball_y_pos >= paddle_1)) begin
ball_x_dir = ~ball_x_dir;
end else if(ball_x_pos == 7'd124 && (ball_y_pos <= (paddle_2 + `paddle_height) && ball_y_pos >= paddle_2)) begin
ball_x_dir = ~ball_x_dir;
end
if(ball_x_pos == 7'd0 || ball_x_pos == 7'd126) begin // if ball goes out of bounds in x direction, reset
ball_x_pos = 7'd64;
ball_y_pos = 6'd32;
case(start_dir) //'randomize' the direction of the ball
2'b00: begin
ball_x_dir = 0;
ball_y_dir = 0;
end
2'b01: begin
ball_x_dir = 1;
ball_y_dir = 0;
end
2'b10: begin
ball_x_dir = 0;
ball_y_dir = 1;
end
2'b11: begin
ball_x_dir = 1;
ball_y_dir = 1;
end
endcase
start_dir = start_dir + 1;
end
if(ball_y_pos == 6'd0 || ball_y_pos == 6'd63) begin // if ball hits top or bottom of screen, reverse y direction
ball_y_dir = ~ball_y_dir;
end
if(ball_x_dir) begin // move the ball in the x direction
ball_x_pos <= ball_x_pos + ball_x_velocity;
end else begin
ball_x_pos <= ball_x_pos - ball_x_velocity;
end
if(ball_y_dir) begin // move the ball in the y direction
ball_y_pos <= ball_y_pos + ball_y_velocity;
end else begin
ball_y_pos <= ball_y_pos - ball_y_velocity;
end
end
end
endmodule