-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEDSnake.vhdp
More file actions
executable file
·315 lines (261 loc) · 10.9 KB
/
LEDSnake.vhdp
File metadata and controls
executable file
·315 lines (261 loc) · 10.9 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
303
304
305
306
307
308
309
310
311
312
313
314
315
Main
(
RX : IN STD_LOGIC;
TX : OUT STD_LOGIC;
CS_O : BUFFER STD_LOGIC;
CLK_O : OUT STD_LOGIC;
DIN_O : OUT STD_LOGIC;
RightButton : IN STD_LOGIC;
LeftButton : IN STD_LOGIC;
)
{
#region Components & Signals
SIGNAL Single_Port_RAM_Write : std_logic;
SIGNAL Single_Port_RAM_Address : natural range 0 to 256-1;
SIGNAL Single_Port_RAM_Data_IN : std_logic_vector (8-1 downto 0);
SIGNAL Single_Port_RAM_Data_OUT : std_logic_vector (8-1 downto 0);
NewComponent Single_Port_RAM
(
Width => 256,
Bits => 8,
Write => Single_Port_RAM_Write,
Address => Single_Port_RAM_Address,
Data_IN => Single_Port_RAM_Data_IN,
Data_OUT => Single_Port_RAM_Data_OUT,
);
SIGNAL UART_Interface_RX_Busy : STD_LOGIC;
SIGNAL UART_Interface_RX_Data : STD_LOGIC_VECTOR (8-1 DOWNTO 0);
SIGNAL UART_Interface_TX_Enable : STD_LOGIC;
SIGNAL UART_Interface_TX_Busy : STD_LOGIC;
SIGNAL UART_Interface_TX_Data : STD_LOGIC_VECTOR (8-1 DOWNTO 0);
NewComponent UART_Interface
(
Baud_Rate => 9600,
RX => RX,
TX => TX,
TX_Enable => UART_Interface_TX_Enable,
TX_Busy => UART_Interface_TX_Busy,
TX_Data => UART_Interface_TX_Data,
RX_Busy => UART_Interface_RX_Busy,
RX_Data =>UART_Interface_RX_Data,
);
CONSTANT LED_Matrix_Length : NATURAL := (4*8);
CONSTANT LED_Matrix_Height : NATURAL := 8;
SIGNAL LED_Matrix_Panel_Bitmap : LED_Matrix_Array (LED_Matrix_Length-1 downto 0) := (others => (others => '0'));
SIGNAL LED_Matrix_Update : STD_LOGIC;
SIGNAL LED_Matrix_Brightness : NATURAL range 0 to 15 := 5;
SIGNAL LED_Matrix_Shutdown : STD_LOGIC := '0';
SIGNAL LED_Matrix_Config : STD_LOGIC;
NewComponent LED_Matrix
(
CLK_Frequency => 12000000,
Panels => 4,
Rotate_seg => '1',
Mirror => '1',
Reset => '0',
CS_O => CS_O,
CLK_O => CLK_O,
DIN_O => DIN_O,
Panel_Bitmap => LED_Matrix_Panel_Bitmap,
Update => LED_Matrix_Update,
Brightness => LED_Matrix_Brightness,
Shutdown => LED_Matrix_Shutdown,
Config => LED_Matrix_Config,
);
SIGNAL UARTInputValue : NATURAL range 0 to 3;
SIGNAL Input_Debouncer_Inputs : STD_LOGIC_VECTOR (2-1 downto 0);
SIGNAL Input_Debouncer_Outputs : STD_LOGIC_VECTOR (2-1 downto 0);
Input_Debouncer_Inputs(0) <= LeftButton;
Input_Debouncer_Inputs(1) <= RightButton;
NewComponent Input_Debouncer
(
Counter_Size => 19,
Input_Number => 2,
Inputs => Input_Debouncer_Inputs,
Outputs => Input_Debouncer_Outputs,
);
TYPE Point IS RECORD
X : INTEGER range -1 to LED_Matrix_Length;
Y : INTEGER range -1 to LED_Matrix_Height;
END RECORD Point;
TYPE PointArray IS ARRAY (natural range <>) OF Point;
SIGNAL rng_xoshiro128plusplus_out_valid : std_logic;
SIGNAL rng_xoshiro128plusplus_out_ready : std_logic := '0';
SIGNAL rng_xoshiro128plusplus_out_data : std_logic_vector (31 downto 0);
NewComponent rng_xoshiro128plusplus
(
init_seed => x"3141592653589793fedcba9876543210",
pipeline => true,
clk => CLK,
rst => '0',
reseed => '0',
newseed => (others => '0'),
out_ready => rng_xoshiro128plusplus_out_ready,
out_valid => rng_xoshiro128plusplus_out_valid,
out_data => rng_xoshiro128plusplus_out_data,
);
SIGNAL SnakeLength : POSITIVE range 1 to (LED_Matrix_Length*LED_Matrix_Height-1) := 3;
#endregion
Process
(
--0 = Left, 1 = Top, 2 = Right, 3 = Bottom
VARIABLE Direction : NATURAL range 0 to 3 := 2;
VARIABLE Ready : BOOLEAN := false;
VARIABLE LastLeftButton : STD_LOGIC := '0';
VARIABLE LastRightButton : STD_LOGIC := '0';
VARIABLE Snack : Point;
)
{
Thread --Setup: Set Default values to ram and spawn random point
{
Single_Port_RAM_Data_IN <= "00000000";
Single_Port_RAM_Write <= '1';
Single_Port_RAM_Address <= 0;
--Set default ram
For(Single_Port_RAM_Address <= 0; Single_Port_RAM_Address < 10; Single_Port_RAM_Address <= Single_Port_RAM_Address + 1)
{
}
Single_Port_RAM_Write <= '0';
Wait(100ms);
--Spawn random dot
Snack := (X => TO_INTEGER(UNSIGNED(rng_xoshiro128plusplus_out_data(4 downto 0))), Y => TO_INTEGER(UNSIGNED(rng_xoshiro128plusplus_out_data(7 downto 5))));
Ready := true;
while(true){}
}
If(Ready)
{
Thread
{
VARIABLE Head : Point;
--read Snake Head from Ram
Single_Port_RAM_Write <= '0';
Single_Port_RAM_Address <= 0;
--Use step to generate random
rng_xoshiro128plusplus_out_ready <= '1';
Step
{
rng_xoshiro128plusplus_out_ready <= '0';
Head.X := TO_INTEGER(UNSIGNED(Single_Port_RAM_Data_OUT(4 downto 0))); --X;
Head.Y := TO_INTEGER(UNSIGNED(Single_Port_RAM_Data_OUT(7 downto 5))); --Y;
}
#region Move
if(Direction = 0)
{
Head.X := Head.X - 1;
}
elsif(Direction = 1)
{
Head.Y := Head.Y + 1;
}
elsif(Direction = 2)
{
Head.X := Head.X + 1;
}
elsif(Direction = 3)
{
Head.Y := Head.Y - 1;
}
#endregion
#region Collisions
--Check if snack is consumed
if(Snack.X = Head.X AND Snack.Y = Head.Y)
{
SnakeLength <= SnakeLength + 1;
--Spawn new snack
Snack := (X => TO_INTEGER(UNSIGNED(rng_xoshiro128plusplus_out_data(4 downto 0))),
Y => TO_INTEGER(UNSIGNED(rng_xoshiro128plusplus_out_data(7 downto 5))));
}
elsif(Head.X < 0
OR Head.Y >= LED_Matrix_Height
OR Head.X >= LED_Matrix_Length
OR Head.Y < 0
OR LED_Matrix_Panel_Bitmap(Head.X)(Head.Y) = '1')
{
--Reset
Direction := 2;
Head.X := 0;
Head.Y := 0;
SnakeLength <= 3;
Snack := (X => TO_INTEGER(UNSIGNED(rng_xoshiro128plusplus_out_data(4 downto 0))),
Y => TO_INTEGER(UNSIGNED(rng_xoshiro128plusplus_out_data(7 downto 5))));
NewFunction assignString (s"\nGame Over! Your Score: ", gameOverTextString);
NewFunction newString (scoreString);
NewFunction naturalToStringInst ((SnakeLength-3), scoreString, bcdEna, bcdBus, bcdBin, bcdDec);
NewFunction newString (gameOverString);
NewFunction stringConcat (gameOverTextString, scoreString, gameOverString);
NewFunction printString (gameOverString, UART_Interface_TX_Data, UART_Interface_TX_Busy, UART_Interface_TX_Enable);
}
#endregion
#region Update Snake Draw
--Reset Canvas
LED_Matrix_Panel_Bitmap <= (others => (others => '0'));
--Move snake one back and write whoole snake to Ram and draw
For (VARIABLE i : INTEGER := SnakeLength - 1; i > 0; i := i - 1)
{
Single_Port_RAM_Write <= '0';
Single_Port_RAM_Address <= i - 1;
Step
{
LED_Matrix_Panel_Bitmap(TO_INTEGER(UNSIGNED(Single_Port_RAM_Data_OUT(4 downto 0))))(TO_INTEGER(UNSIGNED(Single_Port_RAM_Data_OUT(7 downto 5)))) <= '1';
Single_Port_RAM_Data_IN <= Single_Port_RAM_Data_OUT;
Single_Port_RAM_Address <= i;
Single_Port_RAM_Write <= '1';
}
}
--Write Head to Ram and draw
Single_Port_RAM_Address <= 0;
Single_Port_RAM_Data_IN <= STD_LOGIC_VECTOR(TO_UNSIGNED(Head.Y, 3)) & STD_LOGIC_VECTOR(TO_UNSIGNED(Head.X, 5));
LED_Matrix_Panel_Bitmap(Head.X)(Head.Y) <= '1';
--Draw Snack
LED_Matrix_Panel_Bitmap(Snack.X)(Snack.Y) <= '1';
Step{ LED_Matrix_Update <= '0'; Single_Port_RAM_Write <= '0'; }
LED_Matrix_Update <= '1';
#endregion
Wait(100ms);
}
}
Thread
{
NewFunction newString (inputString);
NewFunction readString (inputString, UART_Interface_RX_Data, UART_Interface_RX_Busy);
NewFunction stringToNatural (inputString, UARTInputValue);
NewFunction printString (inputString, UART_Interface_TX_Data, UART_Interface_TX_Busy, UART_Interface_TX_Enable);
Direction := UARTInputValue;
}
Thread
{
if(Input_Debouncer_Outputs(0) /= LastLeftButton)
{
LastLeftButton := Input_Debouncer_Outputs(0);
if(Input_Debouncer_Outputs(0) = '1')
{
--LeftButton
if(Direction > 0)
{
Direction := Direction - 1;
}
else
{
Direction := 3;
}
}
}
if(Input_Debouncer_Outputs(1) /= LastRightButton)
{
LastRightButton := Input_Debouncer_Outputs(1);
if(Input_Debouncer_Outputs(1) = '1')
{
--RightButton
if(Direction < 3)
{
Direction := Direction + 1;
}
else
{
Direction := 0;
}
}
}
}
}
}