Saturday, August 18, 2007

Fundamental VHDL Sourse Codes

http://www.vhdlsoursecodes.blogspot.com/

UART coding in VHDL


--===========================================================================--
--
-- S Y N T H E Z I A B L E miniUART C O R E
--
-- Design units : UART_Def
--
-- File name : uart_lib.vhd
--
-- Purpose : Implements an miniUART device for communication purposes
-- between the OR1K processor and the Host computer through
-- an RS-232 communication protocol.
--
-- Library : uart_lib.vhd
--
-- Dependencies : IEEE.Std_Logic_1164
--
--===========================================================================--
-------------------------------------------------------------------------------
-- Revision list
-- Version Author Date Changes
--

-------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- package UART_Def
--------------------------------------------------------------------------------
library IEEE,STD;
use IEEE.Std_Logic_1164.all;
use IEEE.Numeric_Std.all;
--**--
package UART_Def is
-----------------------------------------------------------------------------
-- Converts unsigned Std_LOGIC_Vector to Integer, leftmost bit is MSB
-- Error message for unknowns (U, X, W, Z, -), converted to 0
-- Verifies whether vector is too long (> 16 bits)
-----------------------------------------------------------------------------
function ToInteger (
Invector : in Unsigned(3 downto 0))
return Integer;
end UART_Def; --==================== End of package header ======================--
package body UART_Def is
function ToInteger (
InVector : in Unsigned(3 downto 0))
return Integer is
constant HeaderMsg : String := "To_Integer:";
constant MsgSeverity : Severity_Level := Warning;
variable Value : Integer := 0;
begin
for i in 0 to 3 loop
if (InVector(i) = '1') then
Value := Value + (2**I);
end if;
end loop;
return Value;
end ToInteger;
end UART_Def; --================ End of package body ================--


--===========================================================================--
--
-- S Y N T H E Z I A B L E miniUART C O R E
--
-- Design units : miniUART core for the OCRP-1
--
-- File name : TxUnit.vhd
--
-- Purpose : Implements an miniUART device for communication purposes
-- between the OR1K processor and the Host computer through
-- an RS-232 communication protocol.
--
-- Library : uart_lib.vhd
--
-- Dependencies : IEEE.Std_Logic_1164
--
--===========================================================================--
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- Description :
-------------------------------------------------------------------------------
-- Entity for the Tx Unit --
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library work;
use work.Uart_Def.all;
-------------------------------------------------------------------------------
-- Transmitter unit
-------------------------------------------------------------------------------
entity TxUnit is
port (
Clk : in Std_Logic; -- Clock signal
Reset : in Std_Logic; -- Reset input
Enable : in Std_Logic; -- Enable input
Load : in Std_Logic; -- Load transmit data
TxD : out Std_Logic; -- RS-232 data output
TRegE : out Std_Logic; -- Tx register empty
TBufE : out Std_Logic; -- Tx buffer empty
DataO : in Std_Logic_Vector(7 downto 0));
end entity; --================== End of entity ==============================--
-------------------------------------------------------------------------------
-- Architecture for TxUnit
-------------------------------------------------------------------------------
architecture Behaviour of TxUnit is
-----------------------------------------------------------------------------
-- Signals
-----------------------------------------------------------------------------
signal TBuff : Std_Logic_Vector(7 downto 0); -- transmit buffer
signal TReg : Std_Logic_Vector(7 downto 0); -- transmit register
signal BitCnt : Unsigned(3 downto 0); -- bit counter
signal tmpTRegE : Std_Logic; --
signal tmpTBufE : Std_Logic; --
begin
-----------------------------------------------------------------------------
-- Implements the Tx unit
-----------------------------------------------------------------------------
process(Clk,Reset,Enable,Load,DataO,TBuff,TReg,tmpTRegE,tmpTBufE)
variable tmp_TRegE : Std_Logic;
constant CntOne : Unsigned(3 downto 0):="0001";
begin
if Rising_Edge(Clk) then
if Reset = '0' then
tmpTRegE <= '1'; tmpTBufE <= '1'; TxD <= '1'; BitCnt <= "0000"; elsif Load = '1' then TBuff <= DataO; tmpTBufE <= '0'; elsif Enable = '1' then if ( tmpTBufE = '0') and (tmpTRegE = '1') then TReg <= TBuff; tmpTRegE <= '0'; -- tmp_TRegE := '0'; tmpTBufE <= '1'; -- else -- tmp_TRegE := tmpTRegE; end if; if tmpTRegE = '0' then case BitCnt is when "0000" =>
TxD <= '0'; BitCnt <= BitCnt + CntOne; when "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" =>
TxD <= TReg(0); TReg <= '1' & TReg(7 downto 1); BitCnt <= BitCnt + CntOne; when "1001" =>
TxD <= '1'; TReg <= '1' & TReg(7 downto 1); BitCnt <= "0000"; tmpTRegE <= '1'; when others => null;
end case;
end if;
end if;
end if;
end process;
TRegE <= tmpTRegE; TBufE <= tmpTBufE; end Behaviour; --=================== End of architecture ====================--


--===========================================================================--
--
-- S Y N T H E Z I A B L E miniUART C O R E
-- Design units : miniUART core for the OCRP-1
--
-- File name : RxUnit.vhd
--
-- Purpose : Implements an miniUART device for communication purposes
-- between the OR1K processor and the Host computer through
-- an RS-232 communication protocol.
--
-- Library : uart_lib.vhd
--
-- Dependencies : IEEE.Std_Logic_1164
--

-- Description : Implements the receive unit of the miniUART core. Samples
-- 16 times the RxD line and retain the value in the middle of
-- the time interval.
-------------------------------------------------------------------------------
-- Entity for Receive Unit - 9600 baudrate --
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library work;
use work.UART_Def.all;
-------------------------------------------------------------------------------
-- Receive unit
-------------------------------------------------------------------------------
entity RxUnit is
port (
Clk : in Std_Logic; -- system clock signal
Reset : in Std_Logic; -- Reset input
Enable : in Std_Logic; -- Enable input
RxD : in Std_Logic; -- RS-232 data input
RD : in Std_Logic; -- Read data signal
FErr : out Std_Logic; -- Status signal
OErr : out Std_Logic; -- Status signal
DRdy : out Std_Logic; -- Status signal
DataIn : out Std_Logic_Vector(7 downto 0));
end entity; --================== End of entity ==============================--
-------------------------------------------------------------------------------
-- Architecture for receive Unit
-------------------------------------------------------------------------------
architecture Behaviour of RxUnit is
-----------------------------------------------------------------------------
-- Signals
-----------------------------------------------------------------------------
signal Start : Std_Logic; -- Syncro signal
signal tmpRxD : Std_Logic; -- RxD buffer
signal tmpDRdy : Std_Logic; -- Data ready buffer
signal outErr : Std_Logic; --
signal frameErr : Std_Logic; --
signal BitCnt : Unsigned(3 downto 0); --
signal SampleCnt : Unsigned(3 downto 0); -- samples on one bit counter
signal ShtReg : Std_Logic_Vector(7 downto 0); --
signal DOut : Std_Logic_Vector(7 downto 0); --
begin

---------------------------------------------------------------------
-- Receiver process
---------------------------------------------------------------------
RcvProc : process(Clk,Reset,Enable,RxD)
variable tmpBitCnt : Integer range 0 to 15;
variable tmpSampleCnt : Integer range 0 to 15;
constant CntOne : Unsigned(3 downto 0):="0001";
begin
if Rising_Edge(Clk) then
tmpBitCnt := ToInteger(BitCnt);
tmpSampleCnt := ToInteger(SampleCnt);
if Reset = '0' then
BitCnt <= "0000";
SampleCnt <= "0000";
Start <= '0';
tmpDRdy <= '0';
frameErr <= '0';
outErr <= '0';
ShtReg <= "00000000"; --
DOut <= "00000000"; --
else
if RD = '1' then
tmpDRdy <= '0'; -- Data was read
end if;
if Enable = '1' then
if Start = '0' then
if RxD = '0' then -- Start bit,
SampleCnt <= SampleCnt + CntOne;
Start <= '1';
end if;
else
if tmpSampleCnt = 8 then -- reads the RxD line
tmpRxD <= RxD;
SampleCnt <= SampleCnt + CntOne;
elsif tmpSampleCnt = 15 then
case tmpBitCnt is
when 0 =>
if tmpRxD = '1' then -- Start Bit
Start <= '0';
else
BitCnt <= BitCnt + CntOne;
end if;
SampleCnt <= SampleCnt + CntOne;
when 12345678 =>
BitCnt <= BitCnt + CntOne;
SampleCnt <= SampleCnt + CntOne;
ShtReg <= tmpRxD & ShtReg(7 downto 1);
when 9 =>
if tmpRxD = '0' then -- stop bit expected
frameErr <= '1';
else
frameErr <= '0';
end if;
if tmpDRdy = '1' then --
outErr <= '1';
else
outErr <= '0';
end if;
tmpDRdy <= '1';
DOut <= ShtReg;
BitCnt <= "0000";
Start <= '0';
when others =>
null;
end case;
else
SampleCnt <= SampleCnt + CntOne;
end if;
end if;
end if;
end if;
end if;
end process;
DRdy <= tmpDRdy;
DataIn <= DOut;
FErr <= frameErr;
OErr <= outErr;
end Behaviour; --==================== End of architecture ====================--
-- Design units : TestBench for miniUART device.
--
-- File name : UARTTest.vhd
--
-- Purpose : Implements the test bench for miniUART device.
--
-- Library : uart_Lib.vhd
--
-- Dependencies : IEEE.Std_Logic_1164
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

-- Clock generator
-------------------------------------------------------------------------------

library IEEE,work;
use IEEE.Std_Logic_1164.all;
--
entity ClkGen is
port (
Clk : out Std_Logic); -- Oscillator clock
end ClkGen;--==================== End of entity ==============================--
--------------------------------------------------------------------------------
-- Architecture for clock and reset signals generator
--------------------------------------------------------------------------------
architecture Behaviour of ClkGen is
begin --========================== Architecture ==============================--
------------------------------------------------------------------------------
-- Provide the system clock signal
------------------------------------------------------------------------------
ClkDriver : process
variable clktmp : Std_Logic := '1';
variable tpw_CI_posedge : Time := 12 ns; -- ~40 MHz
begin
Clk <= clktmp;
clktmp := not clktmp;
wait for tpw_CI_posedge;
end process;
end Behaviour; --=================== End of architecure =====================--
-------------------------------------------------------------------------------
-- LoopBack Device
-------------------------------------------------------------------------------
library IEEE,work;
use IEEE.Std_Logic_1164.all;
--
entity LoopBack is
port (
Clk : in Std_Logic; -- Oscillator clock
RxWr : in Std_Logic; -- Rx line
TxWr : out Std_Logic); -- Tx line
end LoopBack; --==================== End of entity ==========================--
--------------------------------------------------------------------------------
-- Architecture for clock and reset signals generator
--------------------------------------------------------------------------------
architecture Behaviour of LoopBack is
begin --========================== Architecture ==============================--
------------------------------------------------------------------------------
-- Provide the external clock signal
------------------------------------------------------------------------------
ClkTrig : process(Clk)
begin
TxWr <= RxWr;
end process;
end Behaviour; --=================== End of architecure =====================--
--------------------------------------------------------------------------------
-- Testbench for UART device
--------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library work;
use work.Uart_Def.all;
entity UARTTEST is
end UARTTEST;
architecture stimulus of UARTTEST is
-------------------------------------------------------------------
-- Signals
-------------------------------------------------------------------
signal Reset : Std_Logic; -- Synchro signal
signal Clk : Std_Logic; -- Clock signal
signal DataIn : Std_Logic_Vector(7 downto 0);
signal DataOut : Std_Logic_Vector(7 downto 0);
signal RxD : Std_Logic; -- RS-232 data input
signal TxD : Std_Logic; -- RS-232 data output
signal CS_N : Std_Logic;
signal RD_N : Std_Logic;
signal WR_N : Std_Logic;
signal IntRx_N : Std_Logic; -- Receive interrupt
signal IntTx_N : Std_Logic; -- Transmit interrupt
signal Addr : Std_Logic_Vector(1 downto 0); --
-------------------------------------------------------------------
-- Clock Divider
-------------------------------------------------------------------
component ClkGen is
port (
Clk : out Std_Logic); -- Oscillator clock
end component;
-------------------------------------------------------------------
-- LoopBack Device
-------------------------------------------------------------------
component LoopBack is
port (
Clk : in Std_Logic; -- Oscillator clock
RxWr : in Std_Logic; -- Rx line
TxWr : out Std_Logic); -- Tx line
end component;
-------------------------------------------------------------------
-- UART Device
-------------------------------------------------------------------
component miniUART is
port (
SysClk : in Std_Logic; -- System Clock
Reset : in Std_Logic; -- Reset input
CS_N : in Std_Logic;
RD_N : in Std_Logic;
WR_N : in Std_Logic;
RxD : in Std_Logic;
TxD : out Std_Logic;
IntRx_N : out Std_Logic; -- Receive interrupt
IntTx_N : out Std_Logic; -- Transmit interrupt
Addr : in Std_Logic_Vector(1 downto 0); --
DataIn : in Std_Logic_Vector(7 downto 0); --
DataOut : out Std_Logic_Vector(7 downto 0)); --
end component;
begin --======================== Architecture ========================--
---------------------------------------------------------------------
-- Instantiation of components
---------------------------------------------------------------------
Clock : ClkGen port map (Clk);
LoopDev : LoopBack port map (Clk,TxD,RxD);
miniUARTDev : miniUART port map (Clk,Reset,CS_N,RD_N,WR_N,RxD,TxD,
IntRx_N,IntTx_N,Addr,DataIn,DataOut);
---------------------------------------------------------------------
-- Reset cycle
---------------------------------------------------------------------
RstCyc : process
begin
Reset <= '1';
wait for 5 ns;
Reset <= '0';
wait for 250 ns;
Reset <= '1';
wait;
end process;
---------------------------------------------------------------------
--
---------------------------------------------------------------------
ProcCyc : process(Clk,IntRx_N,IntTx_N,Reset)
variable counter : unsigned(3 downto 0);
constant cone : unsigned(3 downto 0):= "0001";
variable temp : bit := '0';
begin
if Rising_Edge(Reset) then
counter := "0000";
WR_N <= '1';
RD_N <= '1';
CS_N <= '1';
elsif Rising_Edge(Clk) then
if IntTx_N = '0' then
if temp = '0' then
temp := '1';
case counter is
when "0000" =>
Addr <= "00";
DataIn <= x"AA";
WR_N <= '0';
CS_N <= '0';
counter := counter + cone;
when "0001" =>
Addr <= "00";
DataIn <= x"AF";
WR_N <= '0';
CS_N <= '0';
counter := counter + cone;
when "0010" =>
Addr <= "00";
DataIn <= x"55";
WR_N <= '0';
CS_N <= '0';
counter := counter + cone;
when "0011" =>
Addr <= "00";
DataIn <= x"E8";
WR_N <= '0';
CS_N <= '0';
counter := "0000";
when others => null;
end case;
elsif temp = '1' then
temp := '0';
end if;
elsif IntRx_N = '0' then
Addr <= "00";
RD_N <= '0';
CS_N <= '0';
else
RD_N <= '1';
CS_N <= '1';
WR_N <= '1';
DataIn <= "ZZZZZZZZ";
end if;
end if;
end process;
end stimulus; --================== End of TestBench ==================--