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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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