Roniere

RAM Memory in VHDL

Aug 17th, 2021 (edited)
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 1.70 KB | None | 0 0
  1.  
  2. library IEEE;
  3.     use IEEE.std_logic_1164.all;
  4.     use IEEE.numeric_std.all;
  5.  
  6.  
  7. entity RAM_Shared_Addr_And_Data is
  8.    
  9.     generic (
  10.         data_address_size : integer := 8
  11.     );
  12.    
  13.     port(
  14.         clk        : in std_logic;
  15.         rst        : in std_logic;
  16.            
  17.         en_W_R     : in std_logic; -- 1 -> WRITE / 0 -> READ
  18.         en_Out     : in std_logic; -- 1 -> Enables Output / 0 -> Disables Output
  19.        
  20.         data_address_sel : in std_logic; -- 1 -> DATA / 0 -> ADDRESS
  21.        
  22.         data_address_io : inout std_logic_vector((data_address_size - 1) downto 0)
  23.     );
  24. end RAM_Shared_Addr_And_Data;
  25.  
  26. architecture behavioral of RAM_Shared_Addr_And_Data is
  27.  
  28.     type ram_t is array (7 downto 0) of std_logic_vector(7 downto 0);
  29.    
  30.     signal ram_s: ram_t := (others => (others => '0')); -- Clear memory
  31.  
  32.     signal data : std_logic_vector (7 downto 0);
  33.     signal address : std_logic_vector (2 downto 0);
  34.    
  35.     begin
  36.    
  37.         addr_process : process(clk)
  38.             begin
  39.                 if rising_edge(clk) then
  40.                     if data_address_sel = '0' then
  41.                         address <= data_address_io(2 downto 0);
  42.                     end if;
  43.                 end if;
  44.         end process;
  45.        
  46.         data_output_process : process (clk)
  47.             begin
  48.                 if rising_edge (clk) then
  49.                     data <= ram_s(to_integer(unsigned(address)));
  50.                 end if;
  51.         end process;
  52.        
  53.         data_input_process : process(clk)
  54.             begin
  55.                 if rising_edge (clk)  then
  56.                     if rst = '0' and data_address_sel = '0' and en_W_R = '1' then
  57.                         ram_s <= (others => (others => '0'));
  58.                     elsif data_address_sel = '0' and en_W_R = '1' then
  59.                         ram_s(to_integer(unsigned(address))) <= data_address_io;
  60.                     end if;
  61.                 end if;
  62.         end process;
  63.        
  64.         data_address_io <= data when data_address_sel = '0' and en_Out = '1' and rst = '1'
  65.                         else (others => 'Z');
  66.                
  67. end behavioral;
Add Comment
Please, Sign In to add comment