-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.sv
127 lines (93 loc) · 3.54 KB
/
ball.sv
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
module ball ( input Reset, frame_clk,
input [7:0] keycod,
output [9:0] BallX, BallY, BallS );
logic [9:0] Ball_X_Pos, Ball_X_Motion, Ball_Y_Pos, Ball_Y_Motion, Ball_Size;
parameter [9:0] Ball_X_Center=320; // Center position on the X axis
parameter [9:0] Ball_Y_Center=240; // Center position on the Y axis
parameter [9:0] Ball_X_Min=0; // Leftmost point on the X axis
parameter [9:0] Ball_X_Max=639; // Rightmost point on the X axis
parameter [9:0] Ball_Y_Min=0; // Topmost point on the Y axis
parameter [9:0] Ball_Y_Max=479; // Bottommost point on the Y axis
parameter [9:0] Ball_X_Step=1; // Step size on the X axis
parameter [9:0] Ball_Y_Step=1; // Step size on the Y axis
assign Ball_Size = 4; // assigns the value 4 as a 10-digit binary number, ie "0000000100"
always_ff @ (posedge Reset or posedge frame_clk )
begin: Move_Ball
if (Reset) // Asynchronous Reset
begin
Ball_Y_Motion <= 10'd0; //Ball_Y_Step;
Ball_X_Motion <= 10'd0; //Ball_X_Step;
Ball_Y_Pos <= Ball_Y_Center;
Ball_X_Pos <= Ball_X_Center;
end
else
begin
if ( (Ball_Y_Pos + Ball_Size) >= Ball_Y_Max ) // Ball is at the bottom edge, BOUNCE!
Ball_Y_Motion <= (~ (Ball_Y_Step) + 1'b1); // 2's complement.
else if ( (Ball_Y_Pos - Ball_Size) <= Ball_Y_Min ) // Ball is at the top edge, BOUNCE!
Ball_Y_Motion <= Ball_Y_Step;
else if ( (Ball_X_Pos + Ball_Size) >= Ball_X_Max ) // Ball is at the Right edge, BOUNCE!
Ball_X_Motion <= (~ (Ball_X_Step) + 1'b1); // 2's complement.
else if ( (Ball_X_Pos - Ball_Size) <= Ball_X_Min ) // Ball is at the Left edge, BOUNCE!
Ball_X_Motion <= Ball_X_Step;
else
Ball_Y_Motion <= Ball_Y_Motion; // Ball is somewhere in the middle, don't bounce, just keep moving
case (keycod)
8'h61 : begin
Ball_X_Motion <= -1;//A
Ball_Y_Motion<= 0;
end
8'h64 : begin
Ball_X_Motion <= 1;//D
Ball_Y_Motion <= 0;
end
8'h77 : begin
Ball_Y_Motion <= 1;//S
Ball_X_Motion <= 0;
end
8'h78 : begin
Ball_Y_Motion <= -1;//W
Ball_X_Motion <= 0;
end
8'h16 : begin
Ball_X_Motion <= -1;//A
Ball_Y_Motion<= 0;
end
8'h46 : begin
Ball_X_Motion <= 1;//D
Ball_Y_Motion <= 0;
end
8'h77 : begin
Ball_Y_Motion <= 1;//S
Ball_X_Motion <= 0;
end
8'h87 : begin
Ball_Y_Motion <= -1;//W
Ball_X_Motion <= 0;
end
8'h65 : begin
Ball_Y_Motion <=1;
Ball_X_Motion <=1;
end
8'h71 : begin
Ball_Y_Motion <=1;
Ball_X_Motion <=-1;
end
8'h63 : begin
Ball_Y_Motion <=-1;
Ball_X_Motion <=1;
end
8'h7A : begin
Ball_Y_Motion <=-1;
Ball_X_Motion <=-1;
end
default: ;
endcase
Ball_Y_Pos <= (Ball_Y_Pos + Ball_Y_Motion); // Update ball position
Ball_X_Pos <= (Ball_X_Pos + Ball_X_Motion);
end
end
assign BallX = Ball_X_Pos;
assign BallY = Ball_Y_Pos;
assign BallS = Ball_Size;
endmodule