⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart_rxerr_tb.vhd

📁 vhdl的异步串口代码
💻 VHD
📖 第 1 页 / 共 3 页
字号:
-- --------------------------------------------------------------------
-- >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<
-- --------------------------------------------------------------------
-- Copyright (c) 2001 by Lattice Semiconductor Corporation
-- --------------------------------------------------------------------
--
-- Permission:
--
--   Lattice Semiconductor grants permission to use this code for use
--   in synthesis for any Lattice programmable logic product.  Other
--   use of this code, including the selling or duplication of any
--   portion is strictly prohibited.
--
-- Disclaimer:
--
--   This VHDL or Verilog source code is intended as a design reference
--   which illustrates how these types of functions can be implemented.
--   It is the user's responsibility to verify their design for
--   consistency and functionality through the use of formal
--   verification methods.  Lattice Semiconductor provides no warranty
--   regarding the use or functionality of this code.
--
-- --------------------------------------------------------------------
--           
--                     Lattice Semiconductor Corporation
--                     5555 NE Moore Court
--                     Hillsboro, OR 97214
--                     U.S.A
--
--                     TEL: 1-800-Lattice (USA and Canada)
--                          408-826-6000 (other locations)
--
--                     web: http://www.latticesemi.com/
--                     email: techsupport@latticesemi.com
--
-- --------------------------------------------------------------------
--
--  Project:           Universal Asynchronous Receiver Transmitter
--  File:              uart_rxErr_tb.vhd
--  Title:             uart_rxErr_tb
--  Design Library:    IEEE, generics
--  Dependencies:      IEEE.std_logic_1164.all
--                     IEEE.numeric_std.all
--                     generics.components.all
--  Description:       VHDL test bench for UART_top receiver error flags testing
--                     There are 7 tests in the following combinations:
--                     Test 1 : 8-bit data, Overrun Error test
--                     Test 2 : 8-bit data, Parity Error test, even parity
--                     Test 3 : 8-bit data, Parity Error test, odd parity
--                     Test 4 : 8-bit data, Parity Error test, stick even parity
--                     Test 5 : 8-bit data, Parity Error test, stick odd parity
--                     Test 6 : 8-bit data, Framing Error test, resync failed
--                     Test 7 : 8-bit data, Break Interrupt test
--
-- --------------------------------------------------------------------
--
-- Revision History :
-- --------------------------------------------------------------------
--   Ver  :| Author            :| Mod. Date :| Changes Made:
--   V1.1 :| J.H.              :| 06/19/01  :| Support ispMACH 5000VG
--   V1.0 :| J.H.              :| 06/01/01  :| First Release
-- --------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;

entity uart_rxErr_tb is
end uart_rxErr_tb;

architecture behavior of uart_rxErr_tb is 

  component Uart_top
    port(
      MR     : in  std_logic;
      MCLK   : in  std_logic;

      CS     : in  std_logic;
      RDn    : in  std_logic;
      WRn    : in  std_logic;
      ADSn   : in  std_logic;
      A      : in  std_logic_vector(2 downto 0);

      DIN    : in  std_logic_vector(7 downto 0);
      DOUT   : out std_logic_vector(7 downto 0);
      DDIS   : out std_logic;
      INTR   : out std_logic;

      SIN    : in  std_logic;
      RxRDYn : out std_logic;

      SOUT   : out std_logic;      
      TxRDYn : out std_logic;

      DCDn   : in  std_logic;
      CTSn   : in  std_logic;
      DSRn   : in  std_logic;
      RIn    : in  std_logic;    
      DTRn   : out std_logic;
      RTSn   : out std_logic
    );
  end component;

  -- Clock Frequency of Test
  --   MCLK_MHZ : frequency of MCLK
  --   PLLO_MHZ : frequency of PLL clk_out
  --              When the UART design is targeted to devices without
  --              PLL feature or the PLL feature is not used, PLLO_MHZ
  --              should be set to the same value of MCLK_MHZ.
  constant MCLK_MHZ : real := 28.4000;
  constant PLLO_MHZ : real := 71.0000;

  constant ONE_K_NS : time := 1000 ns;

  -- Clock Period Declaration
  --   MCLK_CLK_PERIOD : clock period of MCLK
  --   CLK_PEROID      : clock period of internal clock
  constant MCLK_CLK_PERIOD : time := ONE_K_NS / MCLK_MHZ;
  constant CLK_PERIOD      : time := ONE_K_NS / PLLO_MHZ;

  -- UART Registers Address Map
  constant RBR : std_logic_vector(2 downto 0) := "000";
  constant THR : std_logic_vector(2 downto 0) := "000";
  constant IER : std_logic_vector(2 downto 0) := "001";
  constant IIR : std_logic_vector(2 downto 0) := "010";
  constant LCR : std_logic_vector(2 downto 0) := "011";
  constant MCR : std_logic_vector(2 downto 0) := "100";
  constant LSR : std_logic_vector(2 downto 0) := "101";
  constant MSR : std_logic_vector(2 downto 0) := "110";

  -- TimeOut Definition
  constant WAIT_TIMEOUT : integer := 1000;

  -- This procedure performs a write cycle over the internal register
  procedure write_reg (
    addr        : in  std_logic_vector(2 downto 0);
    data        : in  std_logic_vector(7 downto 0);
    signal CS   : out std_logic;
    signal ADSn : out std_logic;
    signal WRn  : out std_logic;
    signal A    : out std_logic_vector(2 Downto 0);
    signal DIN  : out std_logic_vector(7 DownTo 0)) is
  begin
    wait for CLK_PERIOD;
    ADSn <= '0';
    wait for CLK_PERIOD;
    A <= addr;
    CS <= '1';
    wait for CLK_PERIOD;
    ADSn <= '1';
    wait for CLK_PERIOD;
    A <= (others => '1');
    CS <= '0';
    wait for (2*CLK_PERIOD);
    WRn <= '0';
    wait for CLK_PERIOD;
    DIN <= data;
    wait for CLK_PERIOD;
    WRn <= '1';
    wait for CLK_PERIOD;
    DIN <= (others => '1');
    wait for (2*CLK_PERIOD);
  end write_reg;

  -- This procedure performs a read cycle over the internal register
  procedure read_reg (
    addr        : in  std_logic_vector(2 downto 0);
    signal data : out std_logic_vector(7 downto 0);
    signal CS   : out std_logic;
    signal ADSn : out std_logic;
    signal RDn  : out std_logic;
    signal A    : out std_logic_vector(2 Downto 0);
    signal DOUT : in  std_logic_vector(7 Downto 0)) is
  begin
    wait for CLK_PERIOD;
    ADSn <= '0';
    wait for CLK_PERIOD;
    A <= addr;
    CS <= '1';
    wait for CLK_PERIOD;
    ADSn <= '1';
    wait for CLK_PERIOD;
    A <= (others => '1');
    CS <= '0';
    wait for (2*CLK_PERIOD);
    RDn <= '0';
    wait for (2*CLK_PERIOD);
    data <= DOUT;
    RDn <= '1';
    wait for (3*CLK_PERIOD);
  end read_reg;

  -- This procedure generates a serial frame for testing
  procedure sin_gen (
    numDataBits        : integer range 5 to 8;
    data               : in std_logic_vector(7 downto 0);
    parity             : in std_logic;
    stopBitLength      : real;
    parityBitExist     : boolean;
    stopBitIsHigh      : boolean;
    constant cycleTime : in time;
    signal SIN         : out std_logic) is
  begin
    -- Start Bit
    SIN <= '0';
    wait for cycleTime;
    -- Data Bits
    for dataBit in 0 to numDataBits-1 loop
      SIN <= data(dataBit);
      wait for cycleTime;
    end loop;
    -- Parity Bit
    if (parityBitExist) then
      SIN <= parity;
      wait for cycleTime;
    end if;
    -- Stop Bit(s)
    if (stopBitIsHigh) then
      SIN <= '1';
    else
      SIN <= '0'; -- for BREAK & Framing Error generation
    end if;
    wait for stopBitLength*cycleTime;
    SIN <= '1';
  end sin_gen;

  signal MR     : std_logic := '1';
  signal MCLK   : std_logic := '0';
  signal CS     : std_logic := '0';
  signal RDn    : std_logic := '1';
  signal WRn    : std_logic := '1';
  signal ADSn   : std_logic := '1';
  signal A      : std_logic_vector(2 downto 0);
  signal DIN    : std_logic_vector(7 downto 0);
  signal DOUT   : std_logic_vector(7 downto 0);
  signal DDIS   : std_logic;
  signal INTR   : std_logic;
  signal SIN    : std_logic;
  signal RxRDYn : std_logic;
  signal SOUT   : std_logic;
  signal TxRDYn : std_logic;
  signal DCDn   : std_logic;
  signal CTSn   : std_logic;
  signal DSRn   : std_logic;
  signal RIn    : std_logic;
  signal DTRn   : std_logic;
  signal RTSn   : std_logic;

  signal regData_readBack : std_logic_vector(7 downto 0);
  signal okToReceiveSIN   : std_logic := '0';
  signal TestID           : integer   := 0;

  signal PCLK   : std_logic := '0';

begin


-----------------------------------------------------------------------
-- UUT Instantiation
-----------------------------------------------------------------------
  uut: Uart_top port map(
    MR     => MR,
    MCLK   => MCLK,
    CS     => CS,
    RDn    => RDn,
    WRn    => WRn,
    ADSn   => ADSn,
    A      => A,
    DIN    => DIN,
    DOUT   => DOUT,
    DDIS   => DDIS,
    INTR   => INTR,
    SIN    => SIN,
    RxRDYn => RxRDYn,
    SOUT   => SOUT,
    TxRDYn => TxRDYn,
    DCDn   => DCDn,
    CTSn   => CTSn,
    DSRn   => DSRn,
    RIn    => RIn,
    DTRn   => DTRn,
    RTSn   => RTSn
  );


-- Master Clock Generator
  MCLK <= not MCLK after (MCLK_CLK_PERIOD/2);

-- PLL Clock Generator (for simulation purpose)
  PCLK <= not PCLK after (CLK_PERIOD/2);


-----------------------------------------------------------------------
-- SIN Generation for UART Receiver Functions Tests
-----------------------------------------------------------------------
  SIN_proc: process
  begin

    -- SIN Initialization
    SIN<= '1';
           
    -- Test 1 ----------------------------------------------------
    --   8-bit data, Overrun Error test
    wait until rising_edge(okToReceiveSIN);
    sin_gen(8,"10101010",'0',1.0,false,true,CLK_PERIOD*16,SIN);
    sin_gen(8,"01010110",'0',1.0,false,true,CLK_PERIOD*16,SIN);

    -- Test 2 ----------------------------------------------------
    --   8-bit data, Parity Error test, even parity
    wait until rising_edge(okToReceiveSIN);
    sin_gen(8,"10010010",'1',1.0,true,true,CLK_PERIOD*16,SIN);
    sin_gen(8,"10010010",'0',1.0,true,true,CLK_PERIOD*16,SIN);

    -- Test 3 ----------------------------------------------------
    --   8-bit data, Parity Error test, odd parity
    wait until rising_edge(okToReceiveSIN);
    sin_gen(8,"10010010",'0',1.0,true,true,CLK_PERIOD*16,SIN);
    sin_gen(8,"10010010",'1',1.0,true,true,CLK_PERIOD*16,SIN);

    -- Test 4 ----------------------------------------------------
    --   8-bit data, Parity Error test, stick even parity
    wait until rising_edge(okToReceiveSIN);
    sin_gen(8,"10010010",'0',1.0,true,true,CLK_PERIOD*16,SIN);
    sin_gen(8,"10010010",'1',1.0,true,true,CLK_PERIOD*16,SIN);

    -- Test 5 ----------------------------------------------------
    --   8-bit data, Parity Error test, stick odd parity
    wait until rising_edge(okToReceiveSIN);
    sin_gen(8,"10010010",'1',1.0,true,true,CLK_PERIOD*16,SIN);
    sin_gen(8,"10010010",'0',1.0,true,true,CLK_PERIOD*16,SIN);

    -- Test 6 ----------------------------------------------------
    --   8-bit data, Framing Error test, resync failed
    wait until rising_edge(okToReceiveSIN);
    sin_gen(8,"01110100",'1',1.0,true,true,CLK_PERIOD*16,SIN);
    sin_gen(8,"01110100",'1',1.0,true,false,CLK_PERIOD*16,SIN);

    -- Test 7 ----------------------------------------------------
    --   8-bit data, Break Interrupt test
    wait until rising_edge(okToReceiveSIN);
    sin_gen(8,"11111111",'1',1.0,false,true,CLK_PERIOD*16,SIN);
    sin_gen(8,"00000000",'0',30.0,false,false,CLK_PERIOD*16,SIN);

    -- end of tests ----------------------------------------------
    wait;

  end process SIN_proc;


-----------------------------------------------------------------------
-- Test UART Transmitter/Receiver Functions
-----------------------------------------------------------------------
  UART_Stim_Proc : process
    variable i : integer;
  begin

    -- Reset and Intialization
    MR <= '1';

    CS <= '0';
    ADSn <= '1';
    A <= "111";
    DIN <= "11111111";

    CTSn <= '1';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -