-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathball.sv
More file actions
302 lines (264 loc) · 11.4 KB
/
ball.sv
File metadata and controls
302 lines (264 loc) · 11.4 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//-------------------------------------------------------------------------
// 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 [7:0] keycode, // The input from the keyboard
input [9:0] DrawX, DrawY, // Current pixel coordinates
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 <= 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.
unique case(keycode)
// W (up)
8'h1A:
begin
Ball_X_Motion_in = 10'd0; // Clear the x direction motion
// Ball is at the bottom edge, BOUNCE!
if( Ball_Y_Pos + Ball_Size >= Ball_Y_Max )
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1); // 2's complement.
// Ball is at the top edge, BOUNCE!
else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size )
Ball_Y_Motion_in = Ball_Y_Step;
// Ball is at the right edge, BOUNCE!
else if ((Ball_X_Pos + Ball_Size) >= Ball_X_Max)
begin
Ball_Y_Motion_in = 10'd0;
Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1);
end
// Ball is at the left edge, BOUNCE!
else if (Ball_X_Pos <= (Ball_X_Min + Ball_Size))
begin
Ball_Y_Motion_in = 10'd0;
Ball_X_Motion_in = Ball_X_Step;
end
// If it doesn't satisfy any bouncing condition, we would go to move the ball upward
else
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1);
end
// S (down)
8'h16:
begin
Ball_X_Motion_in = 10'd0; // Clear the x direction motion
// Ball is at the bottom edge, BOUNCE!
if( Ball_Y_Pos + Ball_Size >= Ball_Y_Max )
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1); // 2's complement.
// Ball is at the top edge, BOUNCE!
else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size )
Ball_Y_Motion_in = Ball_Y_Step;
// Ball is at the right edge, BOUNCE!
else if ((Ball_X_Pos + Ball_Size) >= Ball_X_Max)
begin
Ball_Y_Motion_in = 10'd0;
Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1);
end
// Ball is at the left edge, BOUNCE!
else if (Ball_X_Pos <= (Ball_X_Min + Ball_Size))
begin
Ball_Y_Motion_in = 10'd0;
Ball_X_Motion_in = Ball_X_Step;
end
// If it doesn't satisfy any bouncing condition, we would go to move the ball downward
else
Ball_Y_Motion_in = Ball_Y_Step;
end
// A (left)
8'h04:
begin
Ball_Y_Motion_in = 10'd0; // Clear the y direction motion
// Ball is at the bottom edge, BOUNCE!
if( Ball_Y_Pos + Ball_Size >= Ball_Y_Max )
begin
Ball_X_Motion_in = 10'd0;
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1); // 2's complement.
end
// Ball is at the top edge, BOUNCE!
else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size )
begin
Ball_X_Motion_in = 10'd0;
Ball_Y_Motion_in = Ball_Y_Step;
end
// Ball is at the right edge, BOUNCE!
else if ((Ball_X_Pos + Ball_Size) >= Ball_X_Max)
begin
Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1);
end
// Ball is at the left edge, BOUNCE!
else if (Ball_X_Pos <= (Ball_X_Min + Ball_Size))
begin
Ball_X_Motion_in = Ball_X_Step;
end
// If it doesn't satisfy any bouncing condition, we would go to move the ball upward
else
Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1);
end
// D (right)
8'h07:
begin
Ball_Y_Motion_in = 10'd0; // Clear the y direction motion
// Ball is at the bottom edge, BOUNCE!
if( Ball_Y_Pos + Ball_Size >= Ball_Y_Max )
begin
Ball_X_Motion_in = 10'd0;
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1); // 2's complement.
end
// Ball is at the top edge, BOUNCE!
else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size )
begin
Ball_X_Motion_in = 10'd0;
Ball_Y_Motion_in = Ball_Y_Step;
end
// Ball is at the right edge, BOUNCE!
else if ((Ball_X_Pos + Ball_Size) >= Ball_X_Max)
begin
Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1);
end
// Ball is at the left edge, BOUNCE!
else if (Ball_X_Pos <= (Ball_X_Min + Ball_Size))
begin
Ball_X_Motion_in = Ball_X_Step;
end
// If it doesn't satisfy any bouncing condition, we would go to move the ball upward
else
Ball_X_Motion_in = Ball_X_Step;
end
// When there is no key is pressed
default:
begin
// Ball is at the bottom edge, BOUNCE!
if( Ball_Y_Pos + Ball_Size >= Ball_Y_Max )
begin
Ball_X_Motion_in = 10'd0;
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1); // 2's complement.
end
// Ball is at the top edge, BOUNCE!
else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size )
begin
Ball_X_Motion_in = 10'd0;
Ball_Y_Motion_in = Ball_Y_Step;
end
// Ball is at the right edge, BOUNCE!
else if ((Ball_X_Pos + Ball_Size) >= Ball_X_Max)
begin
Ball_Y_Motion_in = 10'd0;
Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1);
end
// Ball is at the left edge, BOUNCE!
else if (Ball_X_Pos <= (Ball_X_Min + Ball_Size))
begin
Ball_Y_Motion_in = 10'd0;
Ball_X_Motion_in = Ball_X_Step;
end
else
begin
Ball_Y_Motion_in = Ball_Y_Motion;
Ball_X_Motion_in = Ball_X_Motion;
end
end
endcase
// The given code, left for further debugging use
/*
// Ball is at the bottom edge, BOUNCE!
if( Ball_Y_Pos + Ball_Size >= Ball_Y_Max )
Ball_Y_Motion_in = (~(Ball_Y_Step) + 1'b1); // 2's complement.
// Ball is at the top edge, BOUNCE!
else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size )
Ball_Y_Motion_in = Ball_Y_Step;
// IN-PROGRESS: Add other boundary detections and handle keypress here.
*/
// 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