VHDL 设计单口RAM

1 篇文章 0 订阅
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;
use IEEE.STD_LOGIC_ARITH.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity M_Ram is
    port
    (
    ----------------------------------------------------------------------------
    --rest and system clock
    ----------------------------------------------------------------------------
        CpSl_SysClk_i                   : in std_logic;                         -- system clock
        CpSl_Rst_i                      : in std_logic;                         -- rest
        
    ----------------------------------------------------------------------------
    --input addr and enable signal and ram_data
    ----------------------------------------------------------------------------
        CpSl_WEn_i                      : in std_logic;                         -- write enable
        CpSl_REn_i                      : in std_logic;                         -- read enable
        CpSl_CsEn_i                     : in std_logic;                         -- ram enable
        CpSv_WAddr_i                    : in std_logic_vector(7 downto 0);      -- write address
        CpSv_RAddr_i                    : in std_logic_vector(7 downto 0);      -- read address
        CpSv_RamData_i                  : in std_logic_vector(7 downto 0);      -- ram receive data
        
    ----------------------------------------------------------------------------
    --output ram data and ram data valid signal 
    ----------------------------------------------------------------------------
        CpSl_DataVld_o                  : out std_logic;                        -- read ram data valid
        CpSv_RamData_o                  : out std_logic_vector(7 downto 0)      -- read ram data
    );
end M_Ram;

architecture Behavioral of M_Ram is

--------------------------------------------------------------------------------
--constant declaration
--------------------------------------------------------------------------------
    type Ram_type is array (9 downto 0) of std_logic_vector (7 downto 0);       --Defining ram types 
    signal Ram_array                    : Ram_type;                             --create ram memory with a width of 8 and depth of 10
    
begin
    
    ----------------------------------------------------------------------------
    --data writing to ram 
    ----------------------------------------------------------------------------
    process(CpSl_SysClk_i,CpSl_Rst_i) begin
        if(CpSl_Rst_i = '0') then
            for i IN 0 TO 9 Loop
                Ram_array(conv_integer(i)) <= x"00";
            end LOOP;
        elsif rising_edge(CpSl_SysClk_i) then
            if(CpSl_WEn_i = '1' and CpSl_CsEn_i = '1') then
                Ram_array(conv_integer(CpSv_WAddr_i)) <= CpSv_RamData_i;
            else --hold
            end if;
        end if;
    end process;
    
    ----------------------------------------------------------------------------
    --reading ram data
    ----------------------------------------------------------------------------
    process(CpSl_SysClk_i,CpSl_Rst_i) begin
        if(CpSl_Rst_i = '0') then
            CpSv_RamData_o <= (others => '0');
        elsif rising_edge(CpSl_SysClk_i) then
            if(CpSl_REn_i = '1' and CpSl_CsEn_i = '1' and CpSl_WEn_i = '0') then
                CpSv_RamData_o <= Ram_array(conv_integer(CpSv_RAddr_i));
            else 
                CpSv_RamData_o <= (others => '0');
            end if;
        end if;
    end process;
    
    ----------------------------------------------------------------------------
    --read ram data valid
    ----------------------------------------------------------------------------
    process(CpSl_SysClk_i,CpSl_Rst_i) begin
        if(CpSl_Rst_i = '0') then
            CpSl_DataVld_o <= '0';
        elsif rising_edge(CpSl_SysClk_i) then
            if(CpSl_REn_i = '1' and CpSl_CsEn_i = '1' and CpSl_WEn_i = '0') then
                CpSl_DataVld_o <= '1';
            else
                CpSl_DataVld_o <= '0';
            end if;
        end if;
    end process;


end Behavioral;

testbench

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity M_Ram_tb is
end M_Ram_tb;

architecture Behavioral of M_Ram_tb is

    component M_Ram
    port
    (
    ----------------------------------------------------------------------------
    --rest and system clock
    ----------------------------------------------------------------------------
        CpSl_SysClk_i                   : in std_logic;                         -- system clock
        CpSl_Rst_i                      : in std_logic;                         -- rest
        
    ----------------------------------------------------------------------------
    --input addr and enable signal and ram_data
    ----------------------------------------------------------------------------
        CpSl_WEn_i                      : in std_logic;                         -- write enable
        CpSl_REn_i                      : in std_logic;                         -- read enable
        CpSl_CsEn_i                     : in std_logic;                         -- ram enable
        CpSv_WAddr_i                    : in std_logic_vector(7 downto 0);      -- write address
        CpSv_RAddr_i                    : in std_logic_vector(7 downto 0);      -- read address
        CpSv_RamData_i                  : in std_logic_vector(7 downto 0);      -- ram receive data
        
    ----------------------------------------------------------------------------
    --output ram data and ram data valid signal 
    ----------------------------------------------------------------------------
        CpSl_DataVld_o                  : out std_logic;                        -- read ram data valid
        CpSv_RamData_o                  : out std_logic_vector(7 downto 0)      -- read ram data
    );
    end component;
    
    --input
    signal CpSl_SysClk_i                : std_logic := '0';
    signal CpSl_Rst_i                   : std_logic := '0';
    signal CpSl_WEn_i                   : std_logic;
    signal CpSl_REn_i                   : std_logic;
    signal CpSl_CsEn_i                  : std_logic := '1';
    signal CpSv_WAddr_i                 : std_logic_vector(7 downto 0);
    signal CpSv_RAddr_i                 : std_logic_vector(7 downto 0);
    signal CpSv_RamData_i               : std_logic_vector(7 downto 0):= "01010110";
                                          
    --output                              
    signal CpSl_DataVld_o               : std_logic := '0';
    signal CpSv_RamData_o               : std_logic_vector(7 downto 0);
    signal PrSv_Cnt_s                   : std_logic_vector(3 downto 0);
    
    constant clk_period                 : time := 10ns;
begin

    --gen system clock
    clk_gen: process
    begin
            CpSl_SysClk_i <= '0';
        wait for clk_period /2;
            CpSl_SysClk_i <= '1';
        wait for clk_period /2;
    end process;
    
    --gen rest signal
    rst_gen: process
    begin       
        -- hold reset state for 100 ns.
        wait for 100ns; 
            CpSl_Rst_i <= '1'; 
        wait;
    end process;
    
    --gen data
    process(CpSl_SysClk_i) begin
        if rising_edge(CpSl_SysClk_i) then
            CpSv_RamData_i <= CpSv_RamData_i(6 downto 0) & CpSv_RamData_i(7);
        end if;
    end process;
    
    -- counter Calculates write and read cycles
     process(CpSl_SysClk_i,CpSl_Rst_i) begin
        if(CpSl_Rst_i = '0') then
            PrSv_Cnt_s <= (others => '0');
        elsif rising_edge(CpSl_SysClk_i) then
            if(PrSv_Cnt_s = "1111") then
                PrSv_Cnt_s <= (others => '0');
            else
                PrSv_Cnt_s <= PrSv_Cnt_s + '1';
            end if;
        end if;
    end process;
    
    --gen write enable signal
    process(CpSl_SysClk_i,CpSl_Rst_i) begin
        if(CpSl_Rst_i = '0') then
            CpSl_WEn_i <= '0';
        elsif rising_edge(CpSl_SysClk_i) then
            if(PrSv_Cnt_s < "1000") then
                CpSl_WEn_i <= '1';
            else
                CpSl_WEn_i <= '0';
            end if;
        end if;
    end process;
    
    -- gen Write address
    process(CpSl_SysClk_i,CpSl_Rst_i) begin
        if(CpSl_Rst_i = '0') then
            CpSv_WAddr_i <= (others => '0');
        elsif rising_edge(CpSl_SysClk_i) then
            if(PrSv_Cnt_s < "1000") then
                CpSv_WAddr_i <= CpSv_WAddr_i + '1';
            else
                CpSv_WAddr_i <= (others => '0');
            end if;
        end if;
    end process;
    
    --gen read enable signal
    process(CpSl_SysClk_i,CpSl_Rst_i) begin
        if(CpSl_Rst_i = '0') then
            CpSl_REn_i <= '0';
        elsif rising_edge(CpSl_SysClk_i) then
            if(PrSv_Cnt_s > "0111") then
                CpSl_REn_i <= '1';
            else
                CpSl_REn_i <= '0';
            end if;
        end if;
    end process;

    --gen read address
    process(CpSl_SysClk_i,CpSl_Rst_i) begin
        if(CpSl_Rst_i = '0') then
            CpSv_RAddr_i <= (others => '0');
        elsif rising_edge(CpSl_SysClk_i) then
            if(PrSv_Cnt_s > "0111") then
                CpSv_RAddr_i <= CpSv_RAddr_i + '1';
            else
                CpSv_RAddr_i <= (others => '0');
            end if;
        end if;
    end process;

    uut:M_Ram
    port map(
        CpSl_SysClk_i                   => CpSl_SysClk_i ,                      
        CpSl_Rst_i                      => CpSl_Rst_i    ,                      
        CpSl_WEn_i                      => CpSl_WEn_i    ,                      
        CpSl_REn_i                      => CpSl_REn_i    ,                      
        CpSl_CsEn_i                     => CpSl_CsEn_i ,                        
        CpSv_WAddr_i                    => CpSv_WAddr_i ,                       
        CpSv_RAddr_i                    => CpSv_RAddr_i     ,                   
        CpSv_RamData_i                  => CpSv_RamData_i,                       
        CpSl_DataVld_o                  => CpSl_DataVld_o,                       
        CpSv_RamData_o                  => CpSv_RamData_o                       

    );
end Behavioral;

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值