LucaSkywalker

memory.vhdl

Apr 13th, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.12 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. entity memory is
  6.  
  7.     port (
  8.         clk : in std_logic;
  9.  
  10.         write_enable : in  std_logic;
  11.         address      : in  std_logic_vector(15 downto 0);
  12.         data_write   : in  std_logic_vector(7 downto 0);
  13.         data_read    : out std_logic_vector(7 downto 0));
  14.  
  15. end entity memory;
  16.  
  17. architecture behavioral of memory is
  18.  
  19.     -- ROM address space in hex: 0000-3fff
  20.     -- RAM address space in hex: 4000-ffff
  21.     type rom_t is array (0 to 16#3fff#) of std_logic_vector(7 downto 0);
  22.     type ram_t is array (16#4000# to 16#ffff#) of std_logic_vector(7 downto 0);
  23.  
  24.     -- ROM contains a machine code program. ROM is constant so it cannot
  25.     -- be changed.
  26.     constant rom : rom_t := (
  27.  
  28.         x"00",                          -- 0000: NOP
  29.         x"2b", x"04", x"00",            -- 0001: JMP 0004
  30.         x"1f",                          -- 0004: IN
  31.         x"2b", x"08", x"00",            -- 0005: JMP 0008
  32.           x"24",                                     --
  33.           x"2b", x"13", x"00",             --
  34.           x"25",                             -- 12
  35.           x"2b", x"13", x"00",             --
  36.           x"26",                                     -- 16
  37.           x"2c", x"20", x"00",               --
  38.           x"29",                                     -- 20
  39.         x"2c", x"24", x"00",                --
  40.           x"2b", x"27", x"00",             --
  41.           x"28",                                     -- 27
  42.         others => x"00");
  43.  
  44.     -- RAM is a signal, not a constant, so it can be modified.
  45.     signal ram : ram_t;
  46.  
  47.     signal rom_data_read    : std_logic_vector(7 downto 0);
  48.     signal ram_data_read    : std_logic_vector(7 downto 0);
  49.     signal ram_selected     : std_logic;
  50.     signal ram_selected_reg : std_logic;
  51.  
  52. begin  -- architecture behavioral
  53.  
  54.     -- ROM: 0000 <= address <= 3fff (decimal 0 <= address <= 16383)
  55.     -- RAM: 4000 <= address <= ffff (decimal 16384 <= address <= 65535)
  56.     -- The most-significant two bits of the address are thus zero for ROM,
  57.     -- and non-zero for RAM.
  58.  
  59.     ram_selected <= '0' when address(15 downto 14) = "00" else
  60.                     '1';
  61.     data_read <= rom_data_read when ram_selected_reg = '0' else
  62.                  ram_data_read;
  63.  
  64.     rom_process : process (clk) is
  65.         variable addr : integer range 16#0000# to 16#3fff#;
  66.     begin
  67.         if rising_edge(clk) then
  68.             if ram_selected = '0' then
  69.                 addr          := to_integer(unsigned(address));
  70.                 rom_data_read <= rom(addr);
  71.             end if;
  72.         end if;
  73.     end process rom_process;
  74.  
  75.     ram_process : process (clk) is
  76.         variable addr : integer range 16#4000# to 16#ffff#;
  77.     begin
  78.         if rising_edge(clk) then
  79.             if ram_selected = '1' then
  80.                 addr := to_integer(unsigned(address));
  81.                 if write_enable = '1' then
  82.                     ram(addr) <= data_write;
  83.                 end if;
  84.                 ram_data_read <= ram(addr);
  85.             end if;
  86.         end if;
  87.     end process ram_process;
  88.  
  89.     ram_selected_process : process (clk) is
  90.     begin
  91.         if rising_edge(clk) then
  92.             ram_selected_reg <= ram_selected;
  93.         end if;
  94.     end process ram_selected_process;
  95.  
  96. end architecture behavioral;
Advertisement
Add Comment
Please, Sign In to add comment