-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtank2.sv
201 lines (173 loc) · 6.46 KB
/
tank2.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
module tank2 ( input Reset, frame_clk,hit,
input isWallBottom,isWallTop,isWallRight,isWallLeft,
input [1:0] game_end,
input [7:0] sin, cos,
input [31:0] keycode,
input [9:0] spawn_pos,
output [9:0] TankX, TankY, TankS,TankXStep,TankYStep,
output ShootBullet,
output [5:0] Angle );
logic [12:0] Tank_X_Pos, Tank_Y_Pos;
logic [9:0] Tank_Size,timer;
logic [12:0] Tank_X_Motion, Tank_Y_Motion,Tank_X_MotionP, Tank_Y_MotionP;
int Angle_new, Angle_Motion;
logic [7:0] key;
logic [15:0] Tank_X_Comp, Tank_Y_Comp;
logic signX, signY,ShootBulletP;
always_comb
begin
signX = Tank_X_Step[7] ^ cos[7];
signY = Tank_Y_Step[7] ^ sin[7];
Tank_X_Comp[15:0] = Tank_X_Step[6:0]*cos[6:0];
Tank_Y_Comp[15:0] = Tank_Y_Step[6:0]*sin[6:0];
end
parameter [12:0] Tank_X_Center=4800; // Center position on the X axis
parameter [12:0] Tank_Y_Center=3520; // Center position on the Y axis
parameter [9:0] Tank_X_Min=0; // Leftmost point on the X axis
parameter [9:0] Tank_X_Max=639; // Rightmost point on the X axis
parameter [9:0] Tank_Y_Min=0; // Topmost point on the Y axis
parameter [9:0] Tank_Y_Max=479; // Bottommost point on the Y axis
parameter [7:0] Tank_X_Step=8'b0001_0000; // Step size on the X axis
parameter [7:0] Tank_Y_Step=8'b0001_0000; // Step size on the Y axis
parameter [5:0] AngleStep= 5'b00001; //angle counter clockwise step 1 corresponds to 4 degrees. 22 is 360 set to 0
assign Tank_Size = 10; // assigns the value 4 as a 10-digit binary number, ie "0000000100"
always_ff @ (posedge Reset or posedge frame_clk)
begin
if (Reset) // Asynchronous Reset
begin
Tank_Y_Motion = 13'd0; //Ball_Y_Step;
Tank_X_Motion = 13'd0; //Ball_X_Step;
Angle_Motion = 5'd0; //Ball angle step;
Tank_Y_Pos <= Tank_Y_Center;
Tank_X_Pos <= Tank_X_Center;
Angle_new <= 0;
ShootBulletP <= 0;
end
else if(game_end>0)
begin
Tank_Y_Motion = 13'd0; //Ball_Y_Step;
Tank_X_Motion = 13'd0; //Ball_X_Step;
Angle_Motion = 5'd0; //Ball angle step;
Tank_Y_Pos <= Tank_Y_Center;
Tank_X_Pos <= Tank_X_Center;
Angle_new <= 0;
ShootBulletP <= 0;
end
else
begin
if ((keycode[31:24] ==8'h52 )||(keycode[23:16]==8'h52)||(keycode[15:8] ==8'h52)||(keycode[7:0]==8'h52))//up
begin
if(isWallBottom || isWallTop || isWallRight || isWallLeft )//up
begin
Tank_Y_MotionP[12:0] = ~({{6{~signY}},~Tank_Y_Comp[10:4]} + 1'b1) + 1'b1;
Tank_Y_Motion[12:0] = ~Tank_Y_Motion + ~Tank_Y_Motion;//up
Tank_X_MotionP[12:0] = ~({{6{signX}},Tank_X_Comp[10:4]}) + 1'b1;
Tank_X_Motion[12:0] = ~Tank_X_Motion+ ~Tank_X_Motion;
ShootBulletP = 0;
Angle_Motion = 0;
end
else
begin
Tank_Y_Motion[12:0] = {{6{~signY}},~Tank_Y_Comp[10:4]} + 1'b1;//up
Tank_X_Motion[12:0] = {{6{signX}},Tank_X_Comp[10:4]};
Angle_Motion = 0;
ShootBulletP <= 0;
end
end
else if ((keycode[31:24] ==8'h51 )||(keycode[23:16]==8'h51)||(keycode[15:8] ==8'h51)||(keycode[7:0]==8'h51))//down
begin
if(isWallBottom || isWallTop || isWallRight || isWallLeft )//down
begin
Tank_Y_MotionP[12:0] = ~({{6{signY}},Tank_Y_Comp[10:4]} + 1'b1);
Tank_Y_Motion[12:0] = ~Tank_Y_Motion+~Tank_Y_Motion;//down
Tank_X_MotionP[12:0] = ~({{6{~signX}}, ~Tank_X_Comp[10:4]} + 1'b1) + 1'b1;
Tank_X_Motion[12:0] = ~Tank_X_Motion+~Tank_X_Motion;
Angle_Motion = 0;
ShootBulletP <= 0;
end
else
begin
Tank_Y_Motion[12:0] = {{6{signY}},Tank_Y_Comp[10:4]};//down
Tank_X_Motion[12:0] = {{6{~signX}}, ~Tank_X_Comp[10:4]} + 1'b1;
Angle_Motion = 0;
ShootBulletP <= 0;
end
end
else if ((keycode[31:24] ==8'h50 )||(keycode[23:16]==8'h50)||(keycode[15:8] ==8'h50)||(keycode[7:0]==8'h50))//right increase angle
begin
if(isWallBottom || isWallTop || isWallRight || isWallLeft )
begin
Tank_Y_Motion = 0; // Update ball position
Tank_X_Motion = 0;
Angle_Motion = 0;
ShootBulletP <= 0;
end
else
begin
Tank_X_Motion = 0;
Tank_Y_Motion = 0;
Angle_Motion = 1;
ShootBulletP <= 0;
end
end
else if ((keycode[31:24] ==8'h4f )||(keycode[23:16]==8'h4f)||(keycode[15:8] ==8'h4f)||(keycode[7:0]==8'h4f))//left decrease angle
begin
if(!isWallBottom && !isWallTop && !isWallRight && !isWallLeft)
begin
Tank_Y_Motion = 0; // Update ball position
Tank_X_Motion = 0;
Angle_Motion = -1;
ShootBulletP <= 0;
end
else
begin
Tank_X_Motion = 0;//D
Tank_Y_Motion = 0;
Angle_Motion = 0;
ShootBulletP <= 0;
end
end
else if ((keycode[31:24] ==8'h2c )||(keycode[23:16]==8'h2c)||(keycode[15:8] ==8'h2c)||(keycode[7:0]==8'h2c))//shoot
begin
Tank_Y_Motion = 13'd0 ; // Tank is somewhere in the middle, don't move
Tank_X_Motion = 13'd0;
Angle_Motion = 5'd0;
ShootBulletP <= 1;
timer <= timer+1'b1;
end
else
begin
if(isWallBottom || isWallTop || isWallRight || isWallLeft )//down
begin
//Tank_Y_MotionP[12:0] = ~({{6{signY}},Tank_Y_Comp[10:4]} + 1'b1);
Tank_Y_Motion[12:0] = ~Tank_Y_Motion+~Tank_Y_Motion;//down
//Tank_X_MotionP[12:0] = ~({{6{~signX}}, ~Tank_X_Comp[10:4]} + 1'b1) + 1'b1;
Tank_X_Motion[12:0] = ~Tank_X_Motion+~Tank_X_Motion;
Angle_Motion = 0;
ShootBulletP <= 0;
end
Tank_Y_Motion = 13'd0 ; // Tank is somewhere in the middle, don't move
Tank_X_Motion = 13'd0;
Angle_Motion = 5'd0;
ShootBulletP <= 0;
timer <= 10'd0;
end
Tank_Y_Pos[12:0] <= (Tank_Y_Pos[12:0] + Tank_Y_Motion[12:0]); // Update ball position
Tank_X_Pos[12:0] <= (Tank_X_Pos[12:0] + Tank_X_Motion[12:0]);
Angle_new = Angle_new + Angle_Motion;
if(Angle_new > 44)
Angle_new = 0;
else if(Angle_new<0)
Angle_new = 44;
else
Angle_new = Angle_new;
end
end
assign TankX[9:0] = Tank_X_Pos[12:3];
assign TankY[9:0] = Tank_Y_Pos[12:3];
assign TankS = Tank_Size;
assign TankXStep = Tank_X_Motion;
assign TankYStep = Tank_Y_Motion;
assign Angle = Angle_new;
assign ShootBullet= ShootBulletP;
endmodule