-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghost2.sv
180 lines (162 loc) · 6.16 KB
/
ghost2.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
//-------------------------------------------------------------------------
// 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 ghost2 ( input Reset, frame_clk,
input UpWall, DownWall, LeftWall, RightWall,
output [9:0] GhostX, GhostY, GhostS);
logic [9:0] Ghost_X_Pos, Ghost_X_Motion, Ghost_Y_Pos, Ghost_Y_Motion, Ghost_Size;
logic [1:0]randnum;
//added
// int rands;
//added
parameter [9:0] Ghost_X_Center=231; // Center position on the X axis
parameter [9:0] Ghost_Y_Center=240; // Center position on the Y axis
parameter [9:0] Ghost_X_Min=32; // Leftmost point on the X axis
parameter [9:0] Ghost_X_Max=431; // Rightmost point on the X axis
parameter [9:0] Ghost_Y_Min=64; // Topmost point on the Y axis
parameter [9:0] Ghost_Y_Max=447; // Bottommost point on the Y axis
parameter [9:0] Ghost_X_Step=1; // Step size on the X axis
parameter [9:0] Ghost_Y_Step=1; // Step size on the Y axis
assign Ghost_Size = 9;
//added
// lfsr LSFR(.clk(frame_clk),
// .reset(Reset),
// .enable(1'b1),
// .out(rands)
// );
// assign randnum = rands[0];
//added
always_ff @ (posedge Reset or posedge frame_clk )
begin
if (Reset) // Asynchronous Reset
begin
Ghost_Y_Motion <= 10'd1;
Ghost_X_Motion <= 10'd0;
Ghost_Y_Pos <= Ghost_Y_Center;
Ghost_X_Pos <= Ghost_X_Center;
randnum <= 0;
end
else
begin
randnum <= (Ghost_X_Pos+Ghost_Y_Pos) % 4;
if((Ghost_X_Pos[9:4] == 26 && Ghost_Y_Pos[9:4] == 15) && Ghost_X_Motion > 0) //hole
begin
Ghost_Y_Pos = 246;
Ghost_X_Pos = 42;
Ghost_X_Motion <= Ghost_X_Motion;
end
else if((Ghost_X_Pos[9:4] == 26 && Ghost_Y_Pos[9:4] == 13) && Ghost_X_Motion > 0) //hole
begin
Ghost_Y_Pos = 215;
Ghost_X_Pos = 42;
Ghost_X_Motion <= Ghost_X_Motion;
end
else if((Ghost_X_Pos[9:4] == 14 && Ghost_Y_Pos[9:4] == 16) || (Ghost_X_Pos[9:4] == 6 && Ghost_Y_Pos[9:4] == 25) ||
(Ghost_X_Pos[9:4] == 14 & Ghost_Y_Pos[9:4] == 22) || (Ghost_X_Pos[9:4] == 9 & Ghost_Y_Pos[9:4] == 19) ||
(Ghost_X_Pos[9:4] == 12 && Ghost_Y_Pos[9:4] == 13) || (Ghost_X_Pos[9:4] == 8 && Ghost_Y_Pos[9:4] == 10))
begin
Ghost_Y_Motion <= -1; //up
Ghost_X_Motion <= 0;
end
else if((Ghost_X_Pos[9:4] == 14 && Ghost_Y_Pos[9:4] == 13) || (Ghost_X_Pos[9:4] == 20 && Ghost_Y_Pos[9:4] == 15) ||
/*(Ghost_X_Pos[9:4] == 6 && Ghost_Y_Pos[9:4] == 22) ||*/ (Ghost_X_Pos[9:4] == 8 && Ghost_Y_Pos[9:4] == 8)
|| (Ghost_X_Pos[9:4] == 8 && Ghost_Y_Pos[9:4] == 13) || (Ghost_X_Pos[9:4] == 22 && Ghost_Y_Pos[9:4] == 13))
begin
Ghost_Y_Motion <= 0; //right
Ghost_X_Motion <= 1;
end
else if(Ghost_X_Pos[9:4] == 22 && Ghost_Y_Pos[9:4] == 4)
begin
Ghost_Y_Motion <= 1; //down
Ghost_X_Motion <= 0;
end
else if (((Ghost_Y_Pos + Ghost_Size) >= Ghost_Y_Max || DownWall == 1) && Ghost_Y_Motion > 0) // Ball is at the bottom edge, BOUNCE!
begin
if((randnum == 0 || randnum == 1) && UpWall == 0)
begin
Ghost_Y_Motion <= -1; //up
Ghost_X_Motion <= 0;
end
else if ((randnum == 2 || randnum == 3) && LeftWall == 0)
begin
Ghost_Y_Motion <= 0; //left
Ghost_X_Motion <= -1;
end
else
begin
Ghost_Y_Motion <= 0; //right
Ghost_X_Motion <= 1;
end
end
else if (((Ghost_Y_Pos - Ghost_Size) <= Ghost_Y_Min || UpWall == 1) && Ghost_Y_Motion[9] == 1) // Ball is at the top edge, BOUNCE!
begin
if((randnum == 0 || randnum == 1) && RightWall == 0)
begin
Ghost_Y_Motion <= 0; //right
Ghost_X_Motion <= 1;
end
else if((randnum == 2 || randnum == 3) && LeftWall == 0)
begin
Ghost_Y_Motion <= 0; //left
Ghost_X_Motion <= -1;
end
else
begin
Ghost_Y_Motion <= 1; //down
Ghost_X_Motion <= 0;
end
end
else if (((Ghost_X_Pos + Ghost_Size) >= Ghost_X_Max || RightWall == 1) && Ghost_X_Motion > 0) // Ball is at the Right edge, BOUNCE!
begin
if((randnum == 0 || randnum == 1) && LeftWall == 0)
begin
Ghost_Y_Motion <= 0; //left
Ghost_X_Motion <= -1;
end
else if((randnum == 2 || randnum == 3) && DownWall == 0)
begin
Ghost_X_Motion <= 0; //down
Ghost_Y_Motion <= 1;
end
else
begin
Ghost_Y_Motion <= -1; //up
Ghost_X_Motion <= 0;
end
end
else if (((Ghost_X_Pos - Ghost_Size) <= Ghost_X_Min || LeftWall == 1) && Ghost_X_Motion[9] == 1) // Ball is at the Left edge, BOUNCE!
begin
if((randnum == 0 || randnum == 1) && DownWall == 0)
begin
Ghost_X_Motion <= 0; //down
Ghost_Y_Motion <= 1;
end
else if((randnum == 2 || randnum == 3) && UpWall == 0)
begin
Ghost_X_Motion <= 0; //up
Ghost_Y_Motion <= -1;
end
else
begin
Ghost_Y_Motion <= 0; //right
Ghost_X_Motion <= 1;
end
end
Ghost_Y_Pos <= (Ghost_Y_Pos + Ghost_Y_Motion); // Update ball position
Ghost_X_Pos <= (Ghost_X_Pos + Ghost_X_Motion);
end
end
assign GhostX = Ghost_X_Pos;
assign GhostY = Ghost_Y_Pos;
assign GhostS = Ghost_Size;
endmodule