-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.sv
178 lines (128 loc) · 5.7 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
//-------------------------------------------------------------------------
// Ball.sv --
// Viral Mehta --
// Spring 2005 --
// --
// Modified by Stephen Kempf 03-01-2006 --
// 03-12-2007 --
// Translated by Joe Meng 07-07-2013 --
// Fall 2014 Distribution --
// --
// For use with ECE 298 Lab 7 --
// UIUC ECE Department --
//-------------------------------------------------------------------------
module pacman ( input Reset, frame_clk, isDefeated, victory, death, reversal,
input [7:0] keycode,
output [9:0] BallX, BallY, output logic [1:0] last_keypress, output logic hasMoved);
logic [9:0] Ball_X_Pos, Ball_X_Motion, Ball_Y_Pos, Ball_Y_Motion;
parameter [9:0] Ball_X_Center=136; // Center position on the X axis
parameter [9:0] Ball_Y_Center=248; // 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
int ball_size = 8;
logic stop_motion;
always_ff @ (posedge Reset or posedge frame_clk)
begin: Move_Pacman
if (Reset) // Asynchronous Reset
begin
Ball_Y_Motion <= 10'b0; //Ball_Y_Step;
Ball_X_Motion <= 10'b0; //Ball_X_Step;
Ball_Y_Pos <= Ball_Y_Center;
Ball_X_Pos <= Ball_X_Center;
last_keypress <= 0;
hasMoved <= 0;
end
else if (death == 1 || victory == 1)
begin
Ball_X_Motion <= 0;
Ball_Y_Motion <= 0;
end
else if (isDefeated == 1 && reversal == 0) //life lost
begin
Ball_Y_Motion <= 10'b0; //Ball_Y_Step;
Ball_X_Motion <= 10'b0; //Ball_X_Step;
Ball_Y_Pos <= Ball_Y_Center;
Ball_X_Pos <= Ball_X_Center;
hasMoved <= 0;
last_keypress <= 0;
end
else if (stop_motion == 1'b1)
begin
Ball_X_Motion <= 0;
Ball_Y_Motion <= 0;
end
// else
// begin
// if ( (Ball_Y_Pos + ball_size) >= Ball_Y_Max ) // Ball is at the bottom edge, BOUNCE!
// begin
// Ball_Y_Motion <= -1; // 2's complement.
// Ball_X_Motion <= 0;
// last_keypress <= 3;
// end
// else if ( (Ball_Y_Pos - ball_size) <= Ball_Y_Min ) // Ball is at the top edge, BOUNCE!
// begin
// Ball_Y_Motion <= 1;
// Ball_X_Motion <= 0;
// last_keypress <= 1;
// end
// else if ( (Ball_X_Pos + ball_size) >= Ball_X_Max ) // Ball is at the Right edge, BOUNCE!
// begin
// Ball_X_Motion <= -1; // 2's complement.
// Ball_Y_Motion <= 0;
// last_keypress <= 2;
// end
// else if ( (Ball_X_Pos - ball_size) <= Ball_X_Min ) // Ball is at the Left edge, BOUNCE!
// begin
// Ball_X_Motion <= 1;
// Ball_Y_Motion <= 0;
// last_keypress <= 0;
// end
else
begin // Ball is somewhere in the middle, don't bounce, just keep moving
unique case (keycode)
8'h04 : begin
Ball_X_Motion <= -1;//A
Ball_Y_Motion<= 0;
hasMoved <= 1;
last_keypress <= 2;
end
8'h07 : begin
Ball_X_Motion <= 1;//D
Ball_Y_Motion <= 0;
last_keypress <= 0;
hasMoved <= 1;
end
8'h16 : begin
Ball_Y_Motion <= 1;//S
Ball_X_Motion <= 0;
last_keypress <= 1;
hasMoved <= 1;
end
8'h1A : begin
Ball_Y_Motion <= -1;//W
Ball_X_Motion <= 0;
last_keypress <= 3;
hasMoved <= 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
/**************************************************************************************
ATTENTION! Please answer the following quesiton in your lab report! Points will be allocated for the answers!
Hidden Question #2/2:
Note that Ball_Y_Motion in the above statement may have been changed at the same clock edge
that is causing the assignment of Ball_Y_pos. Will the new value of Ball_Y_Motion be used,
or the old? How will this impact behavior of the ball during a bounce, and how might that
interact with a response to a keypress? Can you fix it? Give an answer in your Post-Lab.
**************************************************************************************/
end
assign BallX = Ball_X_Pos;
assign BallY = Ball_Y_Pos;
sprite_wall sw(.X(Ball_X_Pos), .Y(Ball_Y_Pos), .X_motion(Ball_X_Motion), .Y_motion(Ball_Y_Motion), .stop_motion);
endmodule