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

Subversion Repositories xucpu

[/] [xucpu/] [trunk/] [src/] [components/] [BRAM/] [RAM.vhdl] - Blame information for rev 5

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 lcdsgmtr
-- Copyright 2015, Jürgen Defurne
2
--
3
-- This file is part of the Experimental Unstable CPU System.
4
--
5
-- The Experimental Unstable CPU System Is free software: you can redistribute
6
-- it and/or modify it under the terms of the GNU Lesser General Public License
7
-- as published by the Free Software Foundation, either version 3 of the
8
-- License, or (at your option) any later version.
9
--
10
-- The Experimental Unstable CPU System is distributed in the hope that it will
11
-- be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
13
-- General Public License for more details.
14
--
15
-- You should have received a copy of the GNU Lesser General Public License
16
-- along with Experimental Unstable CPU System. If not, see
17
-- http://www.gnu.org/licenses/lgpl.txt.
18
 
19
 
20
LIBRARY ieee;
21
USE ieee.std_logic_1164.ALL;
22
USE ieee.numeric_std.ALL;
23
 
24
PACKAGE ram_parts IS
25
 
26
  COMPONENT generic_ram IS
27
 
28
    GENERIC (
29
      filename : STRING                := "";
30
      w_data   : NATURAL RANGE 1 TO 32 := 16;
31
      w_addr   : NATURAL RANGE 8 TO 14 := 10);
32
    PORT (
33
      clk : IN  STD_LOGIC;
34
      we  : IN  STD_LOGIC;
35
      a1  : IN  STD_LOGIC_VECTOR(w_addr - 1 DOWNTO 0);  -- Data port address
36
      a2  : IN  STD_LOGIC_VECTOR(w_addr - 1 DOWNTO 0);  -- Instruction port address
37
      d1  : IN  STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);  -- Data port input
38
      q1  : OUT STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);  -- Data port output
39
      q2  : OUT STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0));  -- Instruction port output
40
 
41
  END COMPONENT generic_ram;
42
 
43
  COMPONENT RAM32K IS
44
 
45
    GENERIC (
46
      w_data : NATURAL RANGE 1 TO 32 := 16;
47
      file_1 : STRING                := "";
48
      file_2 : STRING                := "";
49
      file_3 : STRING                := "";
50
      file_4 : STRING                := "");
51
    PORT (
52
      clk : IN  STD_LOGIC;
53
      we  : IN  STD_LOGIC;
54
      a1  : IN  STD_LOGIC_VECTOR(14 DOWNTO 0);  -- Data port address
55
      a2  : IN  STD_LOGIC_VECTOR(14 DOWNTO 0);  -- Instruction port address
56
      d1  : IN  STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);   -- Data port input
57
      q1  : OUT STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);   -- Data port output
58
      q2  : OUT STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0));  -- Instruction port output
59
 
60
  END COMPONENT RAM32K;
61
 
62
END PACKAGE ram_parts;
63
 
64
LIBRARY ieee;
65
USE ieee.std_logic_1164.ALL;
66
USE ieee.numeric_std.ALL;
67
USE std.textio.ALL;
68
USE ieee.std_logic_textio.ALL;
69
USE work.arrayio.ALL;
70
 
71
ENTITY generic_ram IS
72
 
73
  -- Memory component based upon Xilinx Spartan-6 block RAM
74
  -- Maximum capacity is 16k words
75
  -- This component can be initialised by passing a file name as a generic
76
  -- parameter.
77
 
78
  GENERIC (
79
    filename : STRING                := "";
80
    w_data   : NATURAL RANGE 1 TO 32 := 16;
81
    w_addr   : NATURAL RANGE 8 TO 14 := 10);
82
  PORT (
83
    clk : IN  STD_LOGIC;
84
    we  : IN  STD_LOGIC;
85
    a1  : IN  STD_LOGIC_VECTOR(w_addr - 1 DOWNTO 0);  -- Data port address
86
    a2  : IN  STD_LOGIC_VECTOR(w_addr - 1 DOWNTO 0);  -- Instruction port address
87
    d1  : IN  STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);  -- Data port input
88
    q1  : OUT STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);  -- Data port output
89
    q2  : OUT STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0));  -- Instruction port output
90
 
91
END generic_ram;
92
 
93
ARCHITECTURE Behavioral OF generic_ram IS
94
 
95
  SIGNAL mem : cstr_array_type(0 TO (2**w_addr) - 1) := init_cstr(2**w_addr, filename);
96
 
97
  SIGNAL address_reg_1 : STD_LOGIC_VECTOR(w_addr - 1 DOWNTO 0);
98
  SIGNAL address_reg_2 : STD_LOGIC_VECTOR(w_addr - 1 DOWNTO 0);
99
 
100
BEGIN  -- Behavioral
101
 
102
  -- purpose: Try to describe a proper block ram without needing to instantiate a BRAM
103
  -- type   : sequential
104
  -- inputs : clk, we, a1, a2, d1
105
  -- outputs: q1, q2
106
  MP1 : PROCESS (clk, address_reg_1, address_reg_2, mem)
107
  BEGIN  -- PROCESS MP1
108
 
109
    -- Reading
110
    q1 <= STD_LOGIC_VECTOR(to_unsigned(mem(to_integer(UNSIGNED(address_reg_1))), w_data));
111
    q2 <= STD_LOGIC_VECTOR(to_unsigned(mem(to_integer(UNSIGNED(address_reg_2))), w_data));
112
 
113
    IF rising_edge(clk) THEN            -- rising clock edge
114
 
115
      -- These work like the block RAM registers
116
      address_reg_1 <= a1;
117
      address_reg_2 <= a2;
118
 
119
      -- Writing
120
      IF we = '1' THEN
121
        mem(to_integer(UNSIGNED(a1))) <= to_integer(UNSIGNED(d1));
122
      END IF;
123
 
124
    END IF;
125
 
126
  END PROCESS MP1;
127
 
128
 
129
END Behavioral;
130
 
131
LIBRARY ieee;
132
USE ieee.std_logic_1164.ALL;
133
USE ieee.numeric_std.ALL;
134
USE work.mux_parts.ALL;
135
USE work.ram_parts.ALL;
136
 
137
ENTITY RAM32K IS
138
 
139
  -- This component is based upon the above defined generic_ram
140
  -- It is constructed using a 4-to-1 multiplexer and 4 8k word
141
  -- generic_rams.
142
  -- In order to initialise it, a filename can be passed, which is then
143
  -- used to generate the names of four files which should have been
144
  -- prepared previously: filename_{0|1|2|3}.txt
145
  -- These will be used to initialise the four RAM components.
146
 
147
  GENERIC (
148
    w_data : NATURAL RANGE 1 TO 32 := 16;
149
    file_1 : STRING                := "";
150
    file_2 : STRING                := "";
151
    file_3 : STRING                := "";
152
    file_4 : STRING                := "");
153
  PORT (
154
    clk : IN  STD_LOGIC;
155
    we  : IN  STD_LOGIC;
156
    a1  : IN  STD_LOGIC_VECTOR(14 DOWNTO 0);  -- Data port address
157
    a2  : IN  STD_LOGIC_VECTOR(14 DOWNTO 0);  -- Instruction port address
158
    d1  : IN  STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);   -- Data port input
159
    q1  : OUT STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);   -- Data port output
160
    q2  : OUT STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0));  -- Instruction port output
161
 
162
END RAM32K;
163
 
164
ARCHITECTURE Structural OF RAM32K IS
165
 
166
  SIGNAL data_address  : STD_LOGIC_VECTOR(12 DOWNTO 0);
167
  SIGNAL data_select   : STD_LOGIC_VECTOR(1 DOWNTO 0);
168
  SIGNAL instr_address : STD_LOGIC_VECTOR(12 DOWNTO 0);
169
  SIGNAL instr_select  : STD_LOGIC_VECTOR(1 DOWNTO 0);
170
 
171
  SIGNAL wr_sel : STD_LOGIC_VECTOR(3 DOWNTO 0);
172
 
173
  TYPE bus_array_t IS ARRAY(0 TO 3) OF STD_LOGIC_VECTOR(w_data - 1 DOWNTO 0);
174
 
175
  SIGNAL data : bus_array_t;
176
  SIGNAL inst : bus_array_t;
177
 
178
  TYPE file_array IS ARRAY(INTEGER RANGE <>) OF STRING(1 TO 100);
179
 
180
  CONSTANT i_file : file_array(0 TO 3) := (file_1, file_2, file_3, file_4);
181
 
182
BEGIN  -- Structural
183
 
184
  data_address <= a1(12 DOWNTO 0);
185
  data_select  <= a1(14 DOWNTO 13);
186
 
187
  instr_address <= a2(12 DOWNTO 0);
188
  instr_select  <= a2(14 DOWNTO 13);
189
 
190
  wr_sel <= "0001" WHEN data_select = "00" AND we = '1' ELSE
191
            "0010" WHEN data_select = "01" AND we = '1' ELSE
192
            "0100" WHEN data_select = "10" AND we = '1' ELSE
193
            "1000" WHEN data_select = "11" AND we = '1' ELSE
194
            "0000";
195
 
196
  M1 : mux4to1
197
    PORT MAP (
198
      SEL => data_select,
199
      S0  => data(0),
200
      S1  => data(1),
201
      S2  => data(2),
202
      S3  => data(3),
203
      Y   => q1);
204
 
205
  M2 : mux4to1
206
    PORT MAP (
207
      SEL => instr_select,
208
      S0  => inst(0),
209
      S1  => inst(1),
210
      S2  => inst(2),
211
      S3  => inst(3),
212
      Y   => q2);
213
 
214
  RAM : FOR i IN 0 TO 3 GENERATE
215
 
216
    R0 : generic_ram
217
      GENERIC MAP (
218
        filename => i_file(i),
219
        w_data   => w_data,
220
        w_addr   => 13)
221
      PORT MAP (
222
        clk => clk,
223
        we  => wr_sel(i),
224
        a1  => data_address,
225
        a2  => instr_address,
226
        d1  => d1,
227
        q1  => data(i),
228
        q2  => inst(i));
229
 
230
  END GENERATE RAM;
231
 
232
END Structural;
233
 

powered by: WebSVN 2.1.0

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