-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_rx.vhd
93 lines (81 loc) · 3 KB
/
uart_rx.vhd
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
-- uart_rx.vhd: UART controller - receiving (RX) side
-- Author(s): Vladyslav Yeroma (xyerom00)
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
-- Entity declaration (DO NOT ALTER THIS PART!)
entity UART_RX is
port(
CLK : in std_logic;
RST : in std_logic;
DIN : in std_logic;
DOUT : out std_logic_vector(7 downto 0);
DOUT_VLD : out std_logic
);
end entity;
-- Architecture implementation (INSERT YOUR IMPLEMENTATION HERE)
architecture behavioral of UART_RX is
signal cnt: std_logic_vector(4 downto 0);
signal cnt_bits: std_logic_vector(3 downto 0);
signal vld: std_logic;
signal rx_en: std_logic;
signal cnt_en: std_logic;
begin
FSM: entity work.UART_RX_FSM(behavioral)
port map(
CLK => CLK,
RST => RST,
DIN => DIN,
CNT => cnt,
CNT_BITS => cnt_bits,
DOUT_VLD => vld,
RX_EN => rx_en,
CNT_EN => cnt_en
);
-- Will set output to 0 and validation status to current status from FSM
DOUT_VLD <= vld;
process(CLK)
begin
-- If reset, set everything to 0
if (RST = '1') then
cnt <= (others => '0');
cnt_bits <= (others => '0');
DOUT <= (others => '0');
-- Check only on the rising edge of Clock
elsif rising_edge(CLK) then
-- If counter is disabled, set it to 0. Else (counter is enabled, which means we start counting Clock ticks)
-- check other embeded ifs ()
if (cnt_en = '0') then
cnt <= (others => '0');
else
-- We check if counter is = or > 16, so we can set it to 0 (we actually setting it to 1, because we have increment in the same
-- if-else structure, so if we setting it to actual 0, we would miss counting 1 Clock tick each time we set it to 0)
if (rx_en = '1' and cnt >= "10000") then
cnt <= "00001";
-- else if it under 24, we just increment it
elsif (cnt < "11000") then
cnt <= cnt + 1;
end if;
-- if Reciever is disabled, then we set Counter of Bits to 0
if (rx_en = '0') then
cnt_bits <= (others => '0');
-- Else if we counter if = or > 16 we pass data to output threw case
elsif (cnt >= "10000") then
case cnt_bits is
when "0000" => DOUT(0) <= DIN;
when "0001" => DOUT(1) <= DIN;
when "0010" => DOUT(2) <= DIN;
when "0011" => DOUT(3) <= DIN;
when "0100" => DOUT(4) <= DIN;
when "0101" => DOUT(5) <= DIN;
when "0110" => DOUT(6) <= DIN;
when "0111" => DOUT(7) <= DIN;
when others => null;
end case;
-- Increment to next bit
cnt_bits <= cnt_bits + 1;
end if;
end if;
end if;
end process;
end architecture behavioral;