-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.sv
217 lines (192 loc) · 9.2 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//-------------------------------------------------------------------------
// Ball.sv --
// Viral Mehta --
// Spring 2005 --
// --
// Modified by Stephen Kempf 03-01-2006 --
// 03-12-2007 --
// Translated by Joe Meng 07-07-2013 --
// Modified by Po-Han Huang 12-08-2017 --
// Spring 2018 Distribution --
// --
// For use with ECE 385 Lab 8 --
// UIUC ECE Department --
//-------------------------------------------------------------------------
module ball ( input Clk, // 50 MHz clock
Reset, // Active-high reset signal
frame_clk, // The clock indicating a new frame (~60Hz)
input [9:0] DrawX, DrawY, // Current pixel coordinates
input [7:0] keycode,
output logic is_ball // Whether current pixel belongs to ball or background
);
parameter [9:0] Ball_X_Center = 10'd320; // Center position on the X axis
parameter [9:0] Ball_Y_Center = 10'd240; // Center position on the Y axis
parameter [9:0] Ball_X_Min = 10'd0; // Leftmost point on the X axis
parameter [9:0] Ball_X_Max = 10'd639; // Rightmost point on the X axis
parameter [9:0] Ball_Y_Min = 10'd0; // Topmost point on the Y axis
parameter [9:0] Ball_Y_Max = 10'd479; // Bottommost point on the Y axis
parameter [9:0] Ball_X_Step = 10'd1; // Step size on the X axis
parameter [9:0] Ball_Y_Step = 10'd1; // Step size on the Y axis
parameter [9:0] Ball_Size = 10'd4; // Ball size
logic [9:0] Ball_X_Pos, Ball_X_Motion, Ball_Y_Pos, Ball_Y_Motion;
logic [9:0] Ball_X_Pos_in, Ball_X_Motion_in, Ball_Y_Pos_in, Ball_Y_Motion_in;
//////// Do not modify the always_ff blocks. ////////
// Detect rising edge of frame_clk
logic frame_clk_delayed, frame_clk_rising_edge;
always_ff @ (posedge Clk) begin
frame_clk_delayed <= frame_clk;
frame_clk_rising_edge <= (frame_clk == 1'b1) && (frame_clk_delayed == 1'b0);
end
// Update registers
always_ff @ (posedge Clk)
begin
if (Reset)
begin
Ball_X_Pos <= Ball_X_Center;
Ball_Y_Pos <= Ball_Y_Center;
Ball_X_Motion <= 10'd0;
Ball_Y_Motion <= Ball_Y_Step;
end
else
begin
Ball_X_Pos <= Ball_X_Pos_in;
Ball_Y_Pos <= Ball_Y_Pos_in;
Ball_X_Motion <= Ball_X_Motion_in;
Ball_Y_Motion <= Ball_Y_Motion_in;
end
end
//////// Do not modify the always_ff blocks. ////////
// You need to modify always_comb block.
always_comb
begin
// By default, keep motion and position unchanged
Ball_X_Pos_in = Ball_X_Pos;
Ball_Y_Pos_in = Ball_Y_Pos;
Ball_X_Motion_in = Ball_X_Motion;
Ball_Y_Motion_in = Ball_Y_Motion;
// Update position and motion only at rising edge of frame clock
if (frame_clk_rising_edge)
begin
// Be careful when using comparators with "logic" datatype because compiler treats
// both sides of the operator as UNSIGNED numbers.
// e.g. Ball_Y_Pos - Ball_Size <= Ball_Y_Min
// If Ball_Y_Pos is 0, then Ball_Y_Pos - Ball_Size will not be -4, but rather a large positive number.
if( Ball_Y_Pos + Ball_Size >= Ball_Y_Max ) // Ball is at the bottom edge, BOUNCE!
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1); // 2's complement.
else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size ) // Ball is at the top edge, BOUNCE!
Ball_Y_Motion_in = Ball_Y_Step;
else if( Ball_X_Pos + Ball_Size >= Ball_X_Max ) // Ball is at the left edge, BOUNCE!
Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1); // 2's complement.
else if ( Ball_X_Pos <= Ball_X_Min + Ball_Size ) // Ball is at the right edge, BOUNCE!
Ball_X_Motion_in = Ball_X_Step;
else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size ) // Ball is at the top edge, BOUNCE!
Ball_Y_Motion_in = Ball_Y_Step;
//keycode
else if(keycode == 8'h00 ) // Ball is at the left edge, BOUNCE!
begin
Ball_X_Motion_in <= 10'd0;
Ball_Y_Motion_in <= 10'd0;
end
else if ( keycode == 8'd26 ) // Ball is at the right edge, BOUNCE!
begin
Ball_X_Motion_in <= 10'd0;
Ball_Y_Motion_in <= (~(Ball_Y_Step) + 1'b1);
end
else if(keycode == 8'h04 ) // Ball is at the left edge, BOUNCE!
begin
Ball_Y_Motion_in <= 10'd0;
Ball_X_Motion_in <= (~(Ball_X_Step) + 1'b1);
end
else if ( keycode == 8'd22 ) // Ball is at the right edge, BOUNCE!
begin
Ball_X_Motion_in <= 10'd0;
Ball_Y_Motion_in <= Ball_Y_Step;
end
else if ( keycode == 8'd07 ) // Ball is at the right edge, BOUNCE!
begin
Ball_Y_Motion_in <= 10'd0;
Ball_X_Motion_in <= Ball_X_Step;
end
//else
// begin
// unique case (keycode)
// //8'h00: //nothing
// // begin
//
// // Ball_X_Motion_in <= 10'd0;
// // Ball_Y_Motion_in <= 10'd0;
// //
// // end
// 8'd26: //w
// begin
// Ball_X_Motion_in <= 10'd0;
// Ball_Y_Motion_in <= (~(Ball_Y_Step) + 1'b1);
// end
// 8'd22: //s
// begin
// Ball_X_Motion_in <= 10'd0;
// Ball_Y_Motion_in <= Ball_Y_Step;
//
// end
// 8'h04: //a
// begin
// Ball_Y_Motion_in <= 10'd0;
// Ball_X_Motion_in <= (~(Ball_X_Step) + 1'b1);
// end
// 8'h07://d
// begin
// Ball_X_Motion_in <= 10'd0;
// Ball_X_Motion_in <= Ball_X_Step;
//
// end
// default :
// begin
// Ball_X_Motion_in <= 10'd0;
// Ball_Y_Motion_in <= 10'd0;
// end
// endcase
//end
// TODO: Add other boundary detections and handle keypress here.
/*
if (DrawX >= Ball_X_Pos )
Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1); // next x coordinate is right.
else if(DrawX <= Ball_X_Pos )
Ball_X_Motion_in = Ball_X_Step; //next x coordinate is on left.
else if (DrawY >=Ball_Y_Pos )
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1); // next x coordinate is right.
else if(DrawY <= Ball_Y_Pos )
Ball_Y_Motion_in = Ball_Y_Step; //next x coordinate is on left.
*/
// Update the ball's position with its motion
Ball_X_Pos_in = Ball_X_Pos + Ball_X_Motion;
Ball_Y_Pos_in = Ball_Y_Pos + Ball_Y_Motion;
end
/**************************************************************************************
ATTENTION! Please answer the following quesiton in your lab report! Points will be allocated for the answers!
Hidden Question #2/2:
Notice that Ball_Y_Pos is updated using Ball_Y_Motion.
Will the new value of Ball_Y_Motion be used when Ball_Y_Pos is updated, or the old?
What is the difference between writing
"Ball_Y_Pos_in = Ball_Y_Pos + Ball_Y_Motion;" and
"Ball_Y_Pos_in = Ball_Y_Pos + Ball_Y_Motion_in;"?
How will this impact behavior of the ball during a bounce, and how might that interact with a response to a keypress?
Give an answer in your Post-Lab.
**************************************************************************************/
end
// Compute whether the pixel corresponds to ball or background
/* Since the multiplicants are required to be signed, we have to first cast them
from logic to int (signed by default) before they are multiplied. */
int DistX, DistY, Size;
assign DistX = DrawX - Ball_X_Pos;
assign DistY = DrawY - Ball_Y_Pos;
assign Size = Ball_Size;
always_comb begin
if ( ( DistX*DistX + DistY*DistY) <= (Size*Size) )
is_ball = 1'b1;
else
is_ball = 1'b0;
/* The ball's (pixelated) circle is generated using the standard circle formula. Note that while
the single line is quite powerful descriptively, it causes the synthesis tool to use up three
of the 12 available multipliers on the chip! */
end
endmodule