-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtank2.sv.bak
141 lines (113 loc) · 5.6 KB
/
tank2.sv.bak
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
//-------------------------------------------------------------------------
// 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 ball1 ( input Reset, frame_clk,
input [31:0] keycode,
output [9:0] BallX, BallY, BallS );
logic [9:0] Ball_X_Pos, Ball_X_Motion, Ball_Y_Pos, Ball_Y_Motion, Ball_Size;
logic [7:0] key;
parameter [9:0] Ball_X_Center=300; // Center position on the X axis
parameter [9:0] Ball_Y_Center=250; // 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 <= 10'd0 ; // Ball is somewhere in the middle, don't bounce, just keep moving
if ((keycode[31:24] ==8'h52 )||(keycode[23:16]==8'h52)||(keycode[15:8] ==8'h52)||(keycode[7:0]==8'h52))
key <= 8'h52;
else if ((keycode[31:24] ==8'h51 )||(keycode[23:16]==8'h51)||(keycode[15:8] ==8'h51)||(keycode[7:0]==8'h51))
key <= 8'h51;
else if ((keycode[31:24] ==8'h50 )||(keycode[23:16]==8'h50)||(keycode[15:8] ==8'h50)||(keycode[7:0]==8'h50))
key <= 8'h50;
else if ((keycode[31:24] ==8'h4f )||(keycode[23:16]==8'h4f)||(keycode[15:8] ==8'h4f)||(keycode[7:0]==8'h4f))
key <= 8'h4f;
else
key <= 8'h00;
case (key)
8'h04 : begin
if ( (Ball_X_Pos - Ball_Size) <= Ball_X_Min ) // Ball is at the Left edge, BOUNCE!
Ball_X_Motion <= Ball_X_Step;
else
begin
Ball_X_Motion <= -1;//A
Ball_Y_Motion<= 0;
end
end
8'h07 : begin
if ( (Ball_X_Pos + Ball_Size) >= Ball_X_Max ) // Ball is at the Right edge, BOUNCE!
Ball_X_Motion <= (~ (Ball_X_Step) + 1'b1);
else
begin
Ball_X_Motion <= 1;//D
Ball_Y_Motion <= 0;
end
end
8'h16 : 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);
else
begin
Ball_Y_Motion <= 1;//S
Ball_X_Motion <= 0;
end
end
8'h1A : begin
if ( (Ball_Y_Pos - Ball_Size) <= Ball_Y_Min ) // Ball is at the top edge, BOUNCE!
Ball_Y_Motion <= Ball_Y_Step;
else
begin
Ball_Y_Motion <= -1;//W
Ball_X_Motion <= 0;
end
end
default: ;
endcase
Ball_Y_Pos <= (Ball_Y_Pos + Ball_Y_Motion); // Update ball position
Ball_X_Pos <= (Ball_X_Pos + Ball_X_Motion);
/**************************************************************************************
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
end
assign BallX = Ball_X_Pos;
assign BallY = Ball_Y_Pos;
assign BallS = Ball_Size;
endmodule