-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_control.vhd
More file actions
104 lines (91 loc) · 3.58 KB
/
snake_control.vhd
File metadata and controls
104 lines (91 loc) · 3.58 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
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.all;
library UNISIM;
use UNISIM.vcomponents.all;
entity snake_control is
Port (
clk : in std_logic;
rst : in std_logic;
game_tick : in std_logic;
btn_up : in std_logic;
btn_down : in std_logic;
btn_left : in std_logic;
btn_right : in std_logic;
snake_head_x_o : out integer := 20; --range 0 to GRID_WIDTH-1
snake_head_y_o : out integer := 15 --range 0 to GRID_HEIGHT-1
);
end snake_control;
architecture arch of snake_control is
constant GRID_WIDTH : integer := 40;
constant GRID_HEIGHT : integer := 30;
-- Snake state
type direction_type is (DIR_UP, DIR_DOWN, DIR_LEFT, DIR_RIGHT);
signal snake_dir : direction_type := DIR_RIGHT;
signal snake_next_dir : direction_type := DIR_RIGHT;
-- Simple snake positions (just head for now, we'll add body later)
signal snake_head_x : integer range 0 to GRID_WIDTH-1 := 20;
signal snake_head_y : integer range 0 to GRID_HEIGHT-1 := 15;
begin
------------------------------------------------------------------
-- Button input handling (direction control)
------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if btn_up = '0' and snake_dir /= DIR_DOWN then
snake_next_dir <= DIR_UP;
elsif btn_down = '0' and snake_dir /= DIR_UP then
snake_next_dir <= DIR_DOWN;
elsif btn_left = '0' and snake_dir /= DIR_RIGHT then
snake_next_dir <= DIR_LEFT;
elsif btn_right = '0' and snake_dir /= DIR_LEFT then
snake_next_dir <= DIR_RIGHT;
end if;
end if;
end process;
------------------------------------------------------------------
-- Snake movement
------------------------------------------------------------------
process(clk)
begin
if rising_edge(clk) then
if rst = '1' then
snake_head_x <= 20;
snake_head_y <= 15;
snake_dir <= DIR_RIGHT;
elsif game_tick = '1' then
snake_dir <= snake_next_dir;
-- Move snake head
case snake_dir is
when DIR_UP =>
if snake_head_y > 0 then
snake_head_y <= snake_head_y - 1;
else
snake_head_y <= GRID_HEIGHT - 1; -- Wrap around
end if;
when DIR_DOWN =>
if snake_head_y < GRID_HEIGHT - 1 then
snake_head_y <= snake_head_y + 1;
else
snake_head_y <= 0; -- Wrap around
end if;
when DIR_LEFT =>
if snake_head_x > 0 then
snake_head_x <= snake_head_x - 1;
else
snake_head_x <= GRID_WIDTH - 1; -- Wrap around
end if;
when DIR_RIGHT =>
if snake_head_x < GRID_WIDTH - 1 then
snake_head_x <= snake_head_x + 1;
else
snake_head_x <= 0; -- Wrap around
end if;
end case;
end if;
end if;
end process;
snake_head_x_o <= snake_head_x;
snake_head_y_o <= snake_head_y;
end arch;