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

Subversion Repositories plasma

[/] [plasma/] [tags/] [V2_1/] [vhdl/] [ram.vhd] - Blame information for rev 352

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rhoads
---------------------------------------------------------------------
2
-- TITLE: Random Access Memory
3
-- AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
-- DATE CREATED: 4/21/01
5
-- FILENAME: ram.vhd
6 43 rhoads
-- PROJECT: Plasma CPU core
7 2 rhoads
-- COPYRIGHT: Software placed into the public domain by the author.
8
--    Software 'as is' without warranty.  Author liable for nothing.
9
-- DESCRIPTION:
10 48 rhoads
--    Implements the RAM, reads the executable from either "code.txt",
11
--    or for Altera "code[0-3].hex".
12 2 rhoads
--    Modified from "The Designer's Guide to VHDL" by Peter J. Ashenden
13
---------------------------------------------------------------------
14
library ieee;
15
use ieee.std_logic_1164.all;
16
use ieee.std_logic_misc.all;
17
use ieee.std_logic_arith.all;
18 85 rhoads
use ieee.std_logic_unsigned.all;
19 2 rhoads
use ieee.std_logic_textio.all;
20
use std.textio.all;
21 39 rhoads
use work.mlite_pack.all;
22 2 rhoads
 
23
entity ram is
24 48 rhoads
   generic(memory_type : string := "GENERIC");
25 2 rhoads
   port(clk          : in std_logic;
26
        mem_byte_sel : in std_logic_vector(3 downto 0);
27
        mem_write    : in std_logic;
28 48 rhoads
        mem_address  : in std_logic_vector(31 downto 0);
29 55 rhoads
        mem_data_w   : in std_logic_vector(31 downto 0);
30
        mem_data_r   : out std_logic_vector(31 downto 0));
31 2 rhoads
end; --entity ram
32
 
33
architecture logic of ram is
34 55 rhoads
   constant ADDRESS_WIDTH   : natural := 13;
35
   signal clk_inv           : std_logic;
36
   signal mem_sel           : std_logic;
37
   signal read_enable       : std_logic;
38 48 rhoads
   signal write_byte_enable : std_logic_vector(3 downto 0);
39 2 rhoads
begin
40 55 rhoads
   clk_inv <= not clk;
41 85 rhoads
   mem_sel <= '1' when mem_address(30 downto ADDRESS_WIDTH) = ZERO(30 downto ADDRESS_WIDTH) else
42 48 rhoads
              '0';
43
   read_enable <= mem_sel and not mem_write;
44
   write_byte_enable <= mem_byte_sel when mem_sel = '1' else
45
                        "0000";
46 2 rhoads
 
47 48 rhoads
   generic_ram:
48
   if memory_type = "GENERIC" generate
49
   ram_proc: process(clk, mem_byte_sel, mem_write,
50 55 rhoads
         mem_address, mem_data_w, mem_sel)
51 98 rhoads
      variable mem_size : natural := 2 ** ADDRESS_WIDTH;
52 48 rhoads
      variable data : std_logic_vector(31 downto 0);
53 55 rhoads
      subtype word is std_logic_vector(mem_data_w'length-1 downto 0);
54 48 rhoads
      type storage_array is
55 98 rhoads
         array(natural range 0 to mem_size/4 - 1) of word;
56 48 rhoads
      variable storage : storage_array;
57
      variable index : natural := 0;
58
      file load_file : text is in "code.txt";
59
      variable hex_file_line : line;
60
   begin
61
      --load in the ram executable image
62
      if index = 0 then
63
         while not endfile(load_file) loop
64 55 rhoads
--The following two lines had to be commented out for synthesis
65 48 rhoads
            readline(load_file, hex_file_line);
66
            hread(hex_file_line, data);
67
            storage(index) := data;
68
            index := index + 1;
69
         end loop;
70 2 rhoads
      end if;
71
 
72 55 rhoads
      index := conv_integer(mem_address(ADDRESS_WIDTH-1 downto 2));
73 2 rhoads
      data := storage(index);
74
 
75 48 rhoads
      if mem_sel = '1' then
76
         if mem_write = '0' then
77 55 rhoads
            mem_data_r <= data;
78 48 rhoads
         end if;
79
         if mem_byte_sel(0) = '1' then
80 55 rhoads
            data(7 downto 0) := mem_data_w(7 downto 0);
81 48 rhoads
         end if;
82
         if mem_byte_sel(1) = '1' then
83 55 rhoads
            data(15 downto 8) := mem_data_w(15 downto 8);
84 48 rhoads
         end if;
85
         if mem_byte_sel(2) = '1' then
86 55 rhoads
            data(23 downto 16) := mem_data_w(23 downto 16);
87 48 rhoads
         end if;
88
         if mem_byte_sel(3) = '1' then
89 55 rhoads
            data(31 downto 24) := mem_data_w(31 downto 24);
90 48 rhoads
         end if;
91 2 rhoads
      end if;
92
 
93
      if rising_edge(clk) then
94
         if mem_write = '1' then
95
            storage(index) := data;
96
         end if;
97
      end if;
98 48 rhoads
   end process;
99
   end generate; --generic_ram
100 2 rhoads
 
101 48 rhoads
 
102
   altera_ram:
103
   if memory_type = "ALTERA" generate
104 55 rhoads
      --Quartus II does not allow asynchronous RAM to be initialized
105
      --since the RAM may see glitches on the write enable during powerup.
106
      --Making lpm_address_control="REGISTERED" makes the RAM synchronous
107
      --but then the reads are delayed by a clock cycle.
108
      --Inverting the RAM clock appears to solve the clock cycle delay problem.
109
      lpm_ram_io_component0 : lpm_ram_dq
110 48 rhoads
         GENERIC MAP (
111
            intended_device_family => "UNUSED",
112
            lpm_width => 8,
113 55 rhoads
            lpm_widthad => ADDRESS_WIDTH-2,
114 48 rhoads
            lpm_indata => "REGISTERED",
115 55 rhoads
            lpm_address_control => "REGISTERED",
116 48 rhoads
            lpm_outdata => "UNREGISTERED",
117
            lpm_file => "code0.hex",
118
            use_eab => "ON",
119
            lpm_type => "LPM_RAM_DQ")
120
         PORT MAP (
121 55 rhoads
            data    => mem_data_w(31 downto 24),
122
            address => mem_address(ADDRESS_WIDTH-1 downto 2),
123
            inclock => clk_inv,
124 48 rhoads
            we      => write_byte_enable(3),
125 55 rhoads
            q       => mem_data_r(31 downto 24));
126 48 rhoads
 
127 55 rhoads
      lpm_ram_io_component1 : lpm_ram_dq
128 48 rhoads
         GENERIC MAP (
129
            intended_device_family => "UNUSED",
130
            lpm_width => 8,
131 55 rhoads
            lpm_widthad => ADDRESS_WIDTH-2,
132 48 rhoads
            lpm_indata => "REGISTERED",
133 55 rhoads
            lpm_address_control => "REGISTERED",
134 48 rhoads
            lpm_outdata => "UNREGISTERED",
135
            lpm_file => "code1.hex",
136
            use_eab => "ON",
137
            lpm_type => "LPM_RAM_DQ")
138
         PORT MAP (
139 55 rhoads
            data    => mem_data_w(23 downto 16),
140
            address => mem_address(ADDRESS_WIDTH-1 downto 2),
141
            inclock => clk_inv,
142 48 rhoads
            we      => write_byte_enable(2),
143 55 rhoads
            q       => mem_data_r(23 downto 16));
144 48 rhoads
 
145 55 rhoads
      lpm_ram_io_component2 : lpm_ram_dq
146 48 rhoads
         GENERIC MAP (
147
            intended_device_family => "UNUSED",
148
            lpm_width => 8,
149 55 rhoads
            lpm_widthad => ADDRESS_WIDTH-2,
150 48 rhoads
            lpm_indata => "REGISTERED",
151 55 rhoads
            lpm_address_control => "REGISTERED",
152 48 rhoads
            lpm_outdata => "UNREGISTERED",
153
            lpm_file => "code2.hex",
154
            use_eab => "ON",
155
            lpm_type => "LPM_RAM_DQ")
156
         PORT MAP (
157 55 rhoads
            data    => mem_data_w(15 downto 8),
158
            address => mem_address(ADDRESS_WIDTH-1 downto 2),
159
            inclock => clk_inv,
160 48 rhoads
            we      => write_byte_enable(1),
161 55 rhoads
            q       => mem_data_r(15 downto 8));
162 48 rhoads
 
163 55 rhoads
      lpm_ram_io_component3 : lpm_ram_dq
164 48 rhoads
         GENERIC MAP (
165
            intended_device_family => "UNUSED",
166
            lpm_width => 8,
167 55 rhoads
            lpm_widthad => ADDRESS_WIDTH-2,
168 48 rhoads
            lpm_indata => "REGISTERED",
169 55 rhoads
            lpm_address_control => "REGISTERED",
170 48 rhoads
            lpm_outdata => "UNREGISTERED",
171
            lpm_file => "code3.hex",
172
            use_eab => "ON",
173
            lpm_type => "LPM_RAM_DQ")
174
         PORT MAP (
175 55 rhoads
            data    => mem_data_w(7 downto 0),
176
            address => mem_address(ADDRESS_WIDTH-1 downto 2),
177
            inclock => clk_inv,
178 48 rhoads
            we      => write_byte_enable(0),
179 55 rhoads
            q       => mem_data_r(7 downto 0));
180
 
181 48 rhoads
   end generate; --altera_ram
182
 
183 2 rhoads
end; --architecture logic
184
 

powered by: WebSVN 2.1.0

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