OpenCores
URL https://opencores.org/ocsvn/mlite/mlite/trunk

Subversion Repositories mlite

[/] [mlite/] [trunk/] [vhdl/] [ram.vhd] - Diff between revs 132 and 139

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 132 Rev 139
Line 17... Line 17...
use ieee.std_logic_arith.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_textio.all;
use ieee.std_logic_textio.all;
use std.textio.all;
use std.textio.all;
use work.mlite_pack.all;
use work.mlite_pack.all;
 
--Uncomment following two lines for Xilinx RAM16X1D
 
library UNISIM;              --Xilinx
 
use UNISIM.vcomponents.all;  --Xilinx
 
 
entity ram is
entity ram is
   generic(memory_type : string := "DEFAULT");
   generic(memory_type : string := "DEFAULT");
   port(clk          : in std_logic;
   port(clk          : in std_logic;
        mem_byte_sel : in std_logic_vector(3 downto 0);
        enable            : in std_logic;
        mem_write    : in std_logic;
        write_byte_enable : in std_logic_vector(3 downto 0);
        mem_address  : in std_logic_vector(31 downto 0);
        address           : in std_logic_vector(31 downto 2);
        mem_data_w   : in std_logic_vector(31 downto 0);
        data_write        : in std_logic_vector(31 downto 0);
        mem_data_r   : out std_logic_vector(31 downto 0));
        data_read         : out std_logic_vector(31 downto 0));
end; --entity ram
end; --entity ram
 
 
architecture logic of ram is
architecture logic of ram is
   constant ADDRESS_WIDTH   : natural := 13;
   constant ADDRESS_WIDTH   : natural := 13;
   signal clk_inv           : std_logic;
 
   signal mem_sel           : std_logic;
 
   signal read_enable       : std_logic;
 
   signal write_byte_enable : std_logic_vector(3 downto 0);
 
begin
begin
   clk_inv <= not clk;
 
   mem_sel <= '1' when mem_address(30 downto ADDRESS_WIDTH) = ZERO(30 downto ADDRESS_WIDTH) else
 
              '0';
 
   read_enable <= mem_sel and not mem_write;
 
   write_byte_enable <= mem_byte_sel when mem_sel = '1' else
 
                        "0000";
 
 
 
   generic_ram:
   generic_ram:
   if memory_type = "DEFAULT" generate
   if memory_type /= "ALTERA_LPM" generate
   ram_proc: process(clk, mem_byte_sel, mem_write,
   --Simulate a synchronous RAM
         mem_address, mem_data_w, mem_sel)
   ram_proc: process(clk, enable, write_byte_enable,
 
         address, data_write) --mem_write, mem_sel
      variable mem_size : natural := 2 ** ADDRESS_WIDTH;
      variable mem_size : natural := 2 ** ADDRESS_WIDTH;
      variable data : std_logic_vector(31 downto 0);
      variable data : std_logic_vector(31 downto 0);
      subtype word is std_logic_vector(mem_data_w'length-1 downto 0);
      subtype word is std_logic_vector(data_write'length-1 downto 0);
      type storage_array is
      type storage_array is
         array(natural range 0 to mem_size/4 - 1) of word;
         array(natural range 0 to mem_size/4 - 1) of word;
      variable storage : storage_array;
      variable storage : storage_array;
      variable index : natural := 0;
      variable index : natural := 0;
      file load_file : text is in "code.txt";
      file load_file : text open read_mode is "code.txt";
      variable hex_file_line : line;
      variable hex_file_line : line;
   begin
   begin
 
 
      --load in the ram executable image
      --Load in the ram executable image
      if index = 0 then
      if index = 0 then
         while not endfile(load_file) loop
         while not endfile(load_file) loop
--The following two lines had to be commented out for synthesis
--The following two lines had to be commented out for synthesis
            readline(load_file, hex_file_line);
            readline(load_file, hex_file_line);
            hread(hex_file_line, data);
            hread(hex_file_line, data);
            storage(index) := data;
            storage(index) := data;
            index := index + 1;
            index := index + 1;
         end loop;
         end loop;
      end if;
      end if;
 
 
      index := conv_integer(mem_address(ADDRESS_WIDTH-1 downto 2));
      if rising_edge(clk) then
 
         index := conv_integer(address(ADDRESS_WIDTH-1 downto 2));
      data := storage(index);
      data := storage(index);
 
 
      if mem_sel = '1' then
         if enable = '1' then
         if mem_write = '0' then
            if write_byte_enable(0) = '1' then
            mem_data_r <= data;
               data(7 downto 0) := data_write(7 downto 0);
         end if;
 
         if mem_byte_sel(0) = '1' then
 
            data(7 downto 0) := mem_data_w(7 downto 0);
 
         end if;
         end if;
         if mem_byte_sel(1) = '1' then
            if write_byte_enable(1) = '1' then
            data(15 downto 8) := mem_data_w(15 downto 8);
               data(15 downto 8) := data_write(15 downto 8);
         end if;
         end if;
         if mem_byte_sel(2) = '1' then
            if write_byte_enable(2) = '1' then
            data(23 downto 16) := mem_data_w(23 downto 16);
               data(23 downto 16) := data_write(23 downto 16);
         end if;
         end if;
         if mem_byte_sel(3) = '1' then
            if write_byte_enable(3) = '1' then
            data(31 downto 24) := mem_data_w(31 downto 24);
               data(31 downto 24) := data_write(31 downto 24);
         end if;
         end if;
      end if;
      end if;
 
 
      if rising_edge(clk) then
         if write_byte_enable /= "0000" then
         if mem_write = '1' then
 
            storage(index) := data;
            storage(index) := data;
         end if;
         end if;
      end if;
      end if;
 
 
 
      data_read <= data;
   end process;
   end process;
   end generate; --generic_ram
   end generate; --generic_ram
 
 
 
 
   altera_ram:
   altera_ram:
   if memory_type = "ALTERA" generate
   if memory_type = "ALTERA_LPM" generate
      --Quartus II does not allow asynchronous RAM to be initialized
 
      --since the RAM may see glitches on the write enable during powerup.
 
      --Making lpm_address_control="REGISTERED" makes the RAM synchronous
 
      --but then the reads are delayed by a clock cycle.
 
      --Inverting the RAM clock appears to solve the clock cycle delay problem.
 
      lpm_ram_io_component0 : lpm_ram_dq
      lpm_ram_io_component0 : lpm_ram_dq
         GENERIC MAP (
         GENERIC MAP (
            intended_device_family => "UNUSED",
            intended_device_family => "UNUSED",
            lpm_width => 8,
            lpm_width => 8,
            lpm_widthad => ADDRESS_WIDTH-2,
            lpm_widthad => ADDRESS_WIDTH-2,
Line 117... Line 105...
            lpm_outdata => "UNREGISTERED",
            lpm_outdata => "UNREGISTERED",
            lpm_file => "code0.hex",
            lpm_file => "code0.hex",
            use_eab => "ON",
            use_eab => "ON",
            lpm_type => "LPM_RAM_DQ")
            lpm_type => "LPM_RAM_DQ")
         PORT MAP (
         PORT MAP (
            data    => mem_data_w(31 downto 24),
            data    => data_write(31 downto 24),
            address => mem_address(ADDRESS_WIDTH-1 downto 2),
            address => address(ADDRESS_WIDTH-1 downto 2),
            inclock => clk_inv,
            inclock => clk,
            we      => write_byte_enable(3),
            we      => write_byte_enable(3),
            q       => mem_data_r(31 downto 24));
            q       => data_read(31 downto 24));
 
 
      lpm_ram_io_component1 : lpm_ram_dq
      lpm_ram_io_component1 : lpm_ram_dq
         GENERIC MAP (
         GENERIC MAP (
            intended_device_family => "UNUSED",
            intended_device_family => "UNUSED",
            lpm_width => 8,
            lpm_width => 8,
Line 135... Line 123...
            lpm_outdata => "UNREGISTERED",
            lpm_outdata => "UNREGISTERED",
            lpm_file => "code1.hex",
            lpm_file => "code1.hex",
            use_eab => "ON",
            use_eab => "ON",
            lpm_type => "LPM_RAM_DQ")
            lpm_type => "LPM_RAM_DQ")
         PORT MAP (
         PORT MAP (
            data    => mem_data_w(23 downto 16),
            data    => data_write(23 downto 16),
            address => mem_address(ADDRESS_WIDTH-1 downto 2),
            address => address(ADDRESS_WIDTH-1 downto 2),
            inclock => clk_inv,
            inclock => clk,
            we      => write_byte_enable(2),
            we      => write_byte_enable(2),
            q       => mem_data_r(23 downto 16));
            q       => data_read(23 downto 16));
 
 
      lpm_ram_io_component2 : lpm_ram_dq
      lpm_ram_io_component2 : lpm_ram_dq
         GENERIC MAP (
         GENERIC MAP (
            intended_device_family => "UNUSED",
            intended_device_family => "UNUSED",
            lpm_width => 8,
            lpm_width => 8,
Line 153... Line 141...
            lpm_outdata => "UNREGISTERED",
            lpm_outdata => "UNREGISTERED",
            lpm_file => "code2.hex",
            lpm_file => "code2.hex",
            use_eab => "ON",
            use_eab => "ON",
            lpm_type => "LPM_RAM_DQ")
            lpm_type => "LPM_RAM_DQ")
         PORT MAP (
         PORT MAP (
            data    => mem_data_w(15 downto 8),
            data    => data_write(15 downto 8),
            address => mem_address(ADDRESS_WIDTH-1 downto 2),
            address => address(ADDRESS_WIDTH-1 downto 2),
            inclock => clk_inv,
            inclock => clk,
            we      => write_byte_enable(1),
            we      => write_byte_enable(1),
            q       => mem_data_r(15 downto 8));
            q       => data_read(15 downto 8));
 
 
      lpm_ram_io_component3 : lpm_ram_dq
      lpm_ram_io_component3 : lpm_ram_dq
         GENERIC MAP (
         GENERIC MAP (
            intended_device_family => "UNUSED",
            intended_device_family => "UNUSED",
            lpm_width => 8,
            lpm_width => 8,
Line 171... Line 159...
            lpm_outdata => "UNREGISTERED",
            lpm_outdata => "UNREGISTERED",
            lpm_file => "code3.hex",
            lpm_file => "code3.hex",
            use_eab => "ON",
            use_eab => "ON",
            lpm_type => "LPM_RAM_DQ")
            lpm_type => "LPM_RAM_DQ")
         PORT MAP (
         PORT MAP (
            data    => mem_data_w(7 downto 0),
            data    => data_write(7 downto 0),
            address => mem_address(ADDRESS_WIDTH-1 downto 2),
            address => address(ADDRESS_WIDTH-1 downto 2),
            inclock => clk_inv,
            inclock => clk,
            we      => write_byte_enable(0),
            we      => write_byte_enable(0),
            q       => mem_data_r(7 downto 0));
            q       => data_read(7 downto 0));
 
 
   end generate; --altera_ram
   end generate; --altera_ram
 
 
end; --architecture logic
 
 
 
 
   --For XILINX see ram_xilinx.vhd
 
 
 
end; --architecture logic
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.