| 1 |
207 |
jshamlet |
-- Copyright (c)2020 Jeremy Seth Henry
|
| 2 |
|
|
-- All rights reserved.
|
| 3 |
|
|
--
|
| 4 |
|
|
-- Redistribution and use in source and binary forms, with or without
|
| 5 |
|
|
-- modification, are permitted provided that the following conditions are met:
|
| 6 |
|
|
-- * Redistributions of source code must retain the above copyright
|
| 7 |
|
|
-- notice, this list of conditions and the following disclaimer.
|
| 8 |
|
|
-- * Redistributions in binary form must reproduce the above copyright
|
| 9 |
|
|
-- notice, this list of conditions and the following disclaimer in the
|
| 10 |
|
|
-- documentation and/or other materials provided with the distribution,
|
| 11 |
|
|
-- where applicable (as part of a user interface, debugging port, etc.)
|
| 12 |
|
|
--
|
| 13 |
|
|
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
|
| 14 |
|
|
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
| 15 |
|
|
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 16 |
|
|
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
|
| 17 |
|
|
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
| 18 |
|
|
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
| 19 |
|
|
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
| 20 |
|
|
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 21 |
|
|
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
| 22 |
|
|
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 23 |
|
|
--
|
| 24 |
|
|
-- VHDL Units : o8_async_serial
|
| 25 |
|
|
-- Description: Provides a single 8-bit, asynchronous transceiver. While the
|
| 26 |
|
|
-- width is fixed at 8-bits, the bit rate and parity controls
|
| 27 |
|
|
-- are settable via generics.
|
| 28 |
|
|
|
| 29 |
|
|
library ieee;
|
| 30 |
|
|
use ieee.std_logic_1164.all;
|
| 31 |
|
|
use ieee.std_logic_unsigned.all;
|
| 32 |
|
|
use ieee.std_logic_arith.all;
|
| 33 |
|
|
use ieee.std_logic_misc.all;
|
| 34 |
|
|
|
| 35 |
|
|
library work;
|
| 36 |
|
|
use work.open8_pkg.all;
|
| 37 |
|
|
|
| 38 |
|
|
entity o8_async_serial is
|
| 39 |
|
|
generic(
|
| 40 |
|
|
Bit_Rate : real;
|
| 41 |
|
|
Enable_Parity : boolean;
|
| 42 |
|
|
Parity_Odd_Even_n : std_logic;
|
| 43 |
|
|
Sys_Freq : real;
|
| 44 |
|
|
Reset_Level : std_logic;
|
| 45 |
|
|
Address : ADDRESS_TYPE
|
| 46 |
|
|
);
|
| 47 |
|
|
port(
|
| 48 |
|
|
Clock : in std_logic;
|
| 49 |
|
|
Reset : in std_logic;
|
| 50 |
|
|
--
|
| 51 |
|
|
Bus_Address : in ADDRESS_TYPE;
|
| 52 |
|
|
Wr_Enable : in std_logic;
|
| 53 |
|
|
Wr_Data : in DATA_TYPE;
|
| 54 |
|
|
Rd_Enable : in std_logic;
|
| 55 |
|
|
Rd_Data : out DATA_TYPE;
|
| 56 |
|
|
--
|
| 57 |
|
|
TX_Out : out std_logic;
|
| 58 |
|
|
CTS_In : in std_logic;
|
| 59 |
|
|
RX_In : in std_logic;
|
| 60 |
|
|
RTS_Out : out std_logic
|
| 61 |
|
|
);
|
| 62 |
|
|
end entity;
|
| 63 |
|
|
|
| 64 |
|
|
architecture behave of o8_async_serial is
|
| 65 |
|
|
|
| 66 |
|
|
signal FIFO_Reset : std_logic := '0';
|
| 67 |
|
|
|
| 68 |
|
|
constant User_Addr : std_logic_vector(15 downto 1) :=
|
| 69 |
|
|
Address(15 downto 1);
|
| 70 |
|
|
alias Comp_Addr is Bus_Address(15 downto 1);
|
| 71 |
|
|
signal Addr_Match : std_logic := '0';
|
| 72 |
|
|
|
| 73 |
|
|
alias Reg_Addr is Bus_Address(0);
|
| 74 |
|
|
signal Reg_Sel : std_logic := '0';
|
| 75 |
|
|
signal Rd_En : std_logic := '0';
|
| 76 |
|
|
|
| 77 |
|
|
signal TX_FIFO_Wr_En : std_logic := '0';
|
| 78 |
|
|
alias TX_FIFO_Wr_Data is Wr_Data;
|
| 79 |
|
|
signal TX_FIFO_Rd_En : std_logic := '0';
|
| 80 |
|
|
signal TX_FIFO_Empty : std_logic := '0';
|
| 81 |
|
|
signal TX_FIFO_AFull : std_logic := '0';
|
| 82 |
|
|
signal TX_FIFO_Rd_Data : DATA_TYPE := x"00";
|
| 83 |
|
|
|
| 84 |
|
|
alias Tx_Data is TX_FIFO_Rd_Data;
|
| 85 |
|
|
|
| 86 |
|
|
type TX_CTRL_STATES is (IDLE, TX_BYTE, TX_START, TX_WAIT );
|
| 87 |
|
|
signal TX_Ctrl : TX_CTRL_STATES := IDLE;
|
| 88 |
|
|
|
| 89 |
|
|
signal TX_Xmit : std_logic := '0';
|
| 90 |
|
|
signal TX_Done : std_logic := '0';
|
| 91 |
|
|
|
| 92 |
|
|
constant BAUD_RATE_DIV : integer := integer(Sys_Freq / Bit_Rate);
|
| 93 |
|
|
|
| 94 |
|
|
signal CTS_sr : std_logic_vector(3 downto 0) := "0000";
|
| 95 |
|
|
alias CTS_Okay is CTS_sr(3);
|
| 96 |
|
|
|
| 97 |
|
|
signal RX_FIFO_Wr_En : std_logic := '0';
|
| 98 |
|
|
signal RX_FIFO_Wr_Data : DATA_TYPE;
|
| 99 |
|
|
signal RX_FIFO_Rd_En : std_logic;
|
| 100 |
|
|
signal RX_FIFO_Empty : std_logic;
|
| 101 |
|
|
signal RX_FIFO_AFull : std_logic;
|
| 102 |
|
|
signal RX_FIFO_Rd_Data : DATA_TYPE;
|
| 103 |
|
|
|
| 104 |
|
|
begin
|
| 105 |
|
|
|
| 106 |
|
|
Addr_Match <= '1' when Comp_Addr = User_Addr else '0';
|
| 107 |
|
|
|
| 108 |
|
|
io_reg: process( Clock, Reset )
|
| 109 |
|
|
begin
|
| 110 |
|
|
if( Reset = Reset_Level )then
|
| 111 |
|
|
Rd_En <= '0';
|
| 112 |
|
|
Rd_Data <= OPEN8_NULLBUS;
|
| 113 |
|
|
RTS_Out <= '0';
|
| 114 |
|
|
elsif( rising_edge( Clock ) )then
|
| 115 |
|
|
Rd_Data <= OPEN8_NULLBUS;
|
| 116 |
|
|
Rd_En <= Rd_Enable and Addr_Match;
|
| 117 |
|
|
Reg_Sel <= Reg_Addr;
|
| 118 |
|
|
if( Rd_En = '1' and Reg_Sel = '1' )then
|
| 119 |
|
|
Rd_Data(4) <= RX_FIFO_Empty;
|
| 120 |
|
|
Rd_Data(5) <= RX_FIFO_AFull;
|
| 121 |
|
|
Rd_Data(6) <= TX_FIFO_Empty;
|
| 122 |
|
|
Rd_Data(7) <= TX_FIFO_AFull;
|
| 123 |
|
|
end if;
|
| 124 |
|
|
if( Rd_En = '1' and Reg_Sel = '0' )then
|
| 125 |
|
|
Rd_Data <= RX_FIFO_Rd_Data;
|
| 126 |
|
|
end if;
|
| 127 |
|
|
RTS_Out <= not RX_FIFO_AFull;
|
| 128 |
|
|
end if;
|
| 129 |
|
|
end process;
|
| 130 |
|
|
|
| 131 |
|
|
TX_FIFO_Wr_En <= Wr_Enable and Addr_Match and not Reg_Addr;
|
| 132 |
|
|
|
| 133 |
|
|
FIFO_Reset <= '1' when Reset = Reset_Level else '0';
|
| 134 |
|
|
|
| 135 |
|
|
U_TX_FIFO : entity work.fifo_1k_core
|
| 136 |
|
|
port map(
|
| 137 |
|
|
aclr => FIFO_Reset,
|
| 138 |
|
|
clock => Clock,
|
| 139 |
|
|
data => TX_FIFO_Wr_Data,
|
| 140 |
|
|
rdreq => TX_FIFO_Rd_En,
|
| 141 |
|
|
wrreq => TX_FIFO_Wr_En,
|
| 142 |
|
|
empty => TX_FIFO_Empty,
|
| 143 |
|
|
almost_full => TX_FIFO_AFull,
|
| 144 |
|
|
q => TX_FIFO_Rd_Data
|
| 145 |
|
|
);
|
| 146 |
|
|
|
| 147 |
|
|
tx_FSM: process( Clock, Reset )
|
| 148 |
|
|
begin
|
| 149 |
|
|
if( Reset = Reset_Level )then
|
| 150 |
|
|
TX_Ctrl <= IDLE;
|
| 151 |
|
|
TX_Xmit <= '0';
|
| 152 |
|
|
TX_FIFO_Rd_En <= '0';
|
| 153 |
|
|
CTS_sr <= (others => '0');
|
| 154 |
|
|
elsif( rising_edge(Clock) )then
|
| 155 |
|
|
TX_Xmit <= '0';
|
| 156 |
|
|
TX_FIFO_Rd_En <= '0';
|
| 157 |
|
|
CTS_sr <= CTS_sr(2 downto 0) & CTS_In;
|
| 158 |
|
|
|
| 159 |
|
|
case( TX_Ctrl )is
|
| 160 |
|
|
when IDLE =>
|
| 161 |
|
|
if( TX_FIFO_Empty = '0' and CTS_Okay = '1' )then
|
| 162 |
|
|
TX_FIFO_Rd_En <= '1';
|
| 163 |
|
|
TX_Ctrl <= TX_BYTE;
|
| 164 |
|
|
end if;
|
| 165 |
|
|
|
| 166 |
|
|
when TX_BYTE =>
|
| 167 |
|
|
TX_Xmit <= '1';
|
| 168 |
|
|
TX_Ctrl <= TX_START;
|
| 169 |
|
|
|
| 170 |
|
|
when TX_START =>
|
| 171 |
|
|
if( Tx_Done = '0' )then
|
| 172 |
|
|
TX_Ctrl <= TX_WAIT;
|
| 173 |
|
|
end if;
|
| 174 |
|
|
|
| 175 |
|
|
when TX_WAIT =>
|
| 176 |
|
|
if( Tx_Done = '1' )then
|
| 177 |
|
|
TX_Ctrl <= IDLE;
|
| 178 |
|
|
end if;
|
| 179 |
|
|
|
| 180 |
|
|
when others => null;
|
| 181 |
|
|
end case;
|
| 182 |
|
|
|
| 183 |
|
|
end if;
|
| 184 |
|
|
end process;
|
| 185 |
|
|
|
| 186 |
|
|
U_TX : entity work.async_ser_tx
|
| 187 |
|
|
generic map(
|
| 188 |
|
|
Reset_Level => Reset_Level,
|
| 189 |
|
|
Enable_Parity => Enable_Parity,
|
| 190 |
|
|
Parity_Odd_Even_n => Parity_Odd_Even_n,
|
| 191 |
|
|
Clock_Divider => BAUD_RATE_DIV
|
| 192 |
|
|
)
|
| 193 |
|
|
port map(
|
| 194 |
|
|
Clock => Clock,
|
| 195 |
|
|
Reset => Reset,
|
| 196 |
|
|
--
|
| 197 |
|
|
Tx_Data => Tx_Data,
|
| 198 |
|
|
Tx_Valid => TX_Xmit,
|
| 199 |
|
|
--
|
| 200 |
|
|
Tx_Out => TX_Out,
|
| 201 |
|
|
Tx_Done => Tx_Done
|
| 202 |
|
|
);
|
| 203 |
|
|
|
| 204 |
|
|
U_RX : entity work.async_ser_rx
|
| 205 |
|
|
generic map(
|
| 206 |
|
|
Reset_Level => Reset_Level,
|
| 207 |
|
|
Enable_Parity => Enable_Parity,
|
| 208 |
|
|
Parity_Odd_Even_n => Parity_Odd_Even_n,
|
| 209 |
|
|
Clock_Divider => BAUD_RATE_DIV
|
| 210 |
|
|
)
|
| 211 |
|
|
port map(
|
| 212 |
|
|
Clock => Clock,
|
| 213 |
|
|
Reset => Reset,
|
| 214 |
|
|
--
|
| 215 |
|
|
Rx_In => RX_In,
|
| 216 |
|
|
--
|
| 217 |
|
|
Rx_Data => RX_FIFO_Wr_Data,
|
| 218 |
|
|
Rx_Valid => RX_FIFO_Wr_En,
|
| 219 |
|
|
Rx_PErr => open
|
| 220 |
|
|
);
|
| 221 |
|
|
|
| 222 |
|
|
RX_FIFO_Rd_En <= Rd_Enable and Addr_Match and not Reg_Addr;
|
| 223 |
|
|
|
| 224 |
|
|
U_RX_FIFO : entity work.fifo_1k_core
|
| 225 |
|
|
port map(
|
| 226 |
|
|
aclr => FIFO_Reset,
|
| 227 |
|
|
clock => Clock,
|
| 228 |
|
|
data => RX_FIFO_Wr_Data,
|
| 229 |
|
|
rdreq => RX_FIFO_Rd_En,
|
| 230 |
|
|
wrreq => RX_FIFO_Wr_En,
|
| 231 |
|
|
empty => RX_FIFO_Empty,
|
| 232 |
|
|
almost_full => RX_FIFO_AFull,
|
| 233 |
|
|
q => RX_FIFO_Rd_Data
|
| 234 |
|
|
);
|
| 235 |
|
|
|
| 236 |
|
|
end architecture;
|