1 |
2 |
rhoads |
---------------------------------------------------------------------
|
2 |
|
|
-- TITLE: Register Bank
|
3 |
|
|
-- AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
|
4 |
|
|
-- DATE CREATED: 2/2/01
|
5 |
|
|
-- FILENAME: reg_bank.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 |
|
|
-- Implements a register bank with 32 registers that are 32-bits wide.
|
11 |
|
|
-- There are two read-ports and one write port.
|
12 |
|
|
---------------------------------------------------------------------
|
13 |
|
|
library ieee;
|
14 |
|
|
use ieee.std_logic_1164.all;
|
15 |
12 |
rhoads |
use ieee.std_logic_unsigned.all;
|
16 |
39 |
rhoads |
use work.mlite_pack.all;
|
17 |
2 |
rhoads |
|
18 |
|
|
entity reg_bank is
|
19 |
139 |
rhoads |
generic(memory_type : string := "XILINX_16X");
|
20 |
2 |
rhoads |
port(clk : in std_logic;
|
21 |
24 |
rhoads |
reset_in : in std_logic;
|
22 |
74 |
rhoads |
pause : in std_logic;
|
23 |
2 |
rhoads |
rs_index : in std_logic_vector(5 downto 0);
|
24 |
|
|
rt_index : in std_logic_vector(5 downto 0);
|
25 |
|
|
rd_index : in std_logic_vector(5 downto 0);
|
26 |
|
|
reg_source_out : out std_logic_vector(31 downto 0);
|
27 |
|
|
reg_target_out : out std_logic_vector(31 downto 0);
|
28 |
|
|
reg_dest_new : in std_logic_vector(31 downto 0);
|
29 |
|
|
intr_enable : out std_logic);
|
30 |
|
|
end; --entity reg_bank
|
31 |
|
|
|
32 |
9 |
rhoads |
|
33 |
8 |
rhoads |
--------------------------------------------------------------------
|
34 |
9 |
rhoads |
-- The ram_block architecture attempts to use TWO dual-port memories.
|
35 |
12 |
rhoads |
-- Different FPGAs and ASICs need different implementations.
|
36 |
|
|
-- Choose one of the RAM implementations below.
|
37 |
9 |
rhoads |
-- I need feedback on this section!
|
38 |
8 |
rhoads |
--------------------------------------------------------------------
|
39 |
|
|
architecture ram_block of reg_bank is
|
40 |
55 |
rhoads |
signal intr_enable_reg : std_logic;
|
41 |
8 |
rhoads |
type ram_type is array(31 downto 0) of std_logic_vector(31 downto 0);
|
42 |
115 |
rhoads |
|
43 |
9 |
rhoads |
--controls access to dual-port memories
|
44 |
139 |
rhoads |
signal addr_read1, addr_read2 : std_logic_vector(4 downto 0);
|
45 |
|
|
signal addr_write : std_logic_vector(4 downto 0);
|
46 |
|
|
signal data_out1, data_out2 : std_logic_vector(31 downto 0);
|
47 |
|
|
signal write_enable : std_logic;
|
48 |
115 |
rhoads |
|
49 |
8 |
rhoads |
begin
|
50 |
115 |
rhoads |
|
51 |
8 |
rhoads |
reg_proc: process(clk, rs_index, rt_index, rd_index, reg_dest_new,
|
52 |
88 |
rhoads |
intr_enable_reg, data_out1, data_out2, reset_in, pause)
|
53 |
8 |
rhoads |
begin
|
54 |
9 |
rhoads |
--setup for first dual-port memory
|
55 |
|
|
if rs_index = "101110" then --reg_epc CP0 14
|
56 |
139 |
rhoads |
addr_read1 <= "00000";
|
57 |
9 |
rhoads |
else
|
58 |
139 |
rhoads |
addr_read1 <= rs_index(4 downto 0);
|
59 |
9 |
rhoads |
end if;
|
60 |
8 |
rhoads |
case rs_index is
|
61 |
|
|
when "000000" => reg_source_out <= ZERO;
|
62 |
55 |
rhoads |
when "101100" => reg_source_out <= ZERO(31 downto 1) & intr_enable_reg;
|
63 |
139 |
rhoads |
--interrupt vector address = 0x3c
|
64 |
|
|
when "111111" => reg_source_out <= ZERO(31 downto 8) & "00111100";
|
65 |
9 |
rhoads |
when others => reg_source_out <= data_out1;
|
66 |
8 |
rhoads |
end case;
|
67 |
|
|
|
68 |
9 |
rhoads |
--setup for second dual-port memory
|
69 |
139 |
rhoads |
addr_read2 <= rt_index(4 downto 0);
|
70 |
8 |
rhoads |
case rt_index is
|
71 |
|
|
when "000000" => reg_target_out <= ZERO;
|
72 |
9 |
rhoads |
when others => reg_target_out <= data_out2;
|
73 |
8 |
rhoads |
end case;
|
74 |
|
|
|
75 |
139 |
rhoads |
--setup write port for both dual-port memories
|
76 |
74 |
rhoads |
if rd_index /= "000000" and rd_index /= "101100" and pause = '0' then
|
77 |
9 |
rhoads |
write_enable <= '1';
|
78 |
|
|
else
|
79 |
|
|
write_enable <= '0';
|
80 |
|
|
end if;
|
81 |
|
|
if rd_index = "101110" then --reg_epc CP0 14
|
82 |
139 |
rhoads |
addr_write <= "00000";
|
83 |
9 |
rhoads |
else
|
84 |
139 |
rhoads |
addr_write <= rd_index(4 downto 0);
|
85 |
9 |
rhoads |
end if;
|
86 |
|
|
|
87 |
55 |
rhoads |
if reset_in = '1' then
|
88 |
|
|
intr_enable_reg <= '0';
|
89 |
|
|
elsif rising_edge(clk) then
|
90 |
88 |
rhoads |
if rd_index = "101110" then --reg_epc CP0 14
|
91 |
|
|
intr_enable_reg <= '0'; --disable interrupts
|
92 |
24 |
rhoads |
elsif rd_index = "101100" then
|
93 |
55 |
rhoads |
intr_enable_reg <= reg_dest_new(0);
|
94 |
9 |
rhoads |
end if;
|
95 |
8 |
rhoads |
end if;
|
96 |
|
|
|
97 |
55 |
rhoads |
intr_enable <= intr_enable_reg;
|
98 |
9 |
rhoads |
end process;
|
99 |
8 |
rhoads |
|
100 |
9 |
rhoads |
|
101 |
115 |
rhoads |
--------------------------------------------------------------
|
102 |
|
|
---- Pick only ONE of the dual-port RAM implementations below!
|
103 |
|
|
--------------------------------------------------------------
|
104 |
12 |
rhoads |
|
105 |
|
|
-- Option #1
|
106 |
|
|
-- One tri-port RAM, two read-ports, one write-port
|
107 |
|
|
-- 32 registers 32-bits wide
|
108 |
47 |
rhoads |
tri_port_mem:
|
109 |
139 |
rhoads |
if memory_type = "TRI_PORT_X" generate
|
110 |
|
|
ram_proc: process(clk, addr_read1, addr_read2,
|
111 |
|
|
addr_write, reg_dest_new, write_enable)
|
112 |
344 |
rhoads |
variable tri_port_ram : ram_type := (others => ZERO);
|
113 |
47 |
rhoads |
begin
|
114 |
139 |
rhoads |
data_out1 <= tri_port_ram(conv_integer(addr_read1));
|
115 |
|
|
data_out2 <= tri_port_ram(conv_integer(addr_read2));
|
116 |
47 |
rhoads |
if rising_edge(clk) then
|
117 |
|
|
if write_enable = '1' then
|
118 |
139 |
rhoads |
tri_port_ram(conv_integer(addr_write)) := reg_dest_new;
|
119 |
47 |
rhoads |
end if;
|
120 |
12 |
rhoads |
end if;
|
121 |
47 |
rhoads |
end process;
|
122 |
|
|
end generate; --tri_port_mem
|
123 |
9 |
rhoads |
|
124 |
|
|
|
125 |
12 |
rhoads |
-- Option #2
|
126 |
|
|
-- Two dual-port RAMs, each with one read-port and one write-port
|
127 |
47 |
rhoads |
dual_port_mem:
|
128 |
139 |
rhoads |
if memory_type = "DUAL_PORT_" generate
|
129 |
|
|
ram_proc2: process(clk, addr_read1, addr_read2,
|
130 |
|
|
addr_write, reg_dest_new, write_enable)
|
131 |
344 |
rhoads |
variable dual_port_ram1 : ram_type := (others => ZERO);
|
132 |
|
|
variable dual_port_ram2 : ram_type := (others => ZERO);
|
133 |
47 |
rhoads |
begin
|
134 |
139 |
rhoads |
data_out1 <= dual_port_ram1(conv_integer(addr_read1));
|
135 |
|
|
data_out2 <= dual_port_ram2(conv_integer(addr_read2));
|
136 |
47 |
rhoads |
if rising_edge(clk) then
|
137 |
|
|
if write_enable = '1' then
|
138 |
139 |
rhoads |
dual_port_ram1(conv_integer(addr_write)) := reg_dest_new;
|
139 |
|
|
dual_port_ram2(conv_integer(addr_write)) := reg_dest_new;
|
140 |
47 |
rhoads |
end if;
|
141 |
|
|
end if;
|
142 |
|
|
end process;
|
143 |
|
|
end generate; --dual_port_mem
|
144 |
9 |
rhoads |
|
145 |
|
|
|
146 |
139 |
rhoads |
-- Option #3
|
147 |
|
|
-- RAM16X1D: 16 x 1 positive edge write, asynchronous read dual-port
|
148 |
|
|
-- distributed RAM for all Xilinx FPGAs
|
149 |
261 |
rhoads |
-- From library UNISIM; use UNISIM.vcomponents.all;
|
150 |
139 |
rhoads |
xilinx_16x1d:
|
151 |
|
|
if memory_type = "XILINX_16X" generate
|
152 |
|
|
signal data_out1A, data_out1B : std_logic_vector(31 downto 0);
|
153 |
|
|
signal data_out2A, data_out2B : std_logic_vector(31 downto 0);
|
154 |
|
|
signal weA, weB : std_logic;
|
155 |
344 |
rhoads |
signal no_connect : std_logic_vector(127 downto 0);
|
156 |
139 |
rhoads |
begin
|
157 |
|
|
weA <= write_enable and not addr_write(4); --lower 16 registers
|
158 |
|
|
weB <= write_enable and addr_write(4); --upper 16 registers
|
159 |
|
|
|
160 |
|
|
reg_loop: for i in 0 to 31 generate
|
161 |
|
|
begin
|
162 |
|
|
--Read port 1 lower 16 registers
|
163 |
|
|
reg_bit1a : RAM16X1D
|
164 |
|
|
port map (
|
165 |
|
|
WCLK => clk, -- Port A write clock input
|
166 |
|
|
WE => weA, -- Port A write enable input
|
167 |
|
|
A0 => addr_write(0), -- Port A address[0] input bit
|
168 |
|
|
A1 => addr_write(1), -- Port A address[1] input bit
|
169 |
|
|
A2 => addr_write(2), -- Port A address[2] input bit
|
170 |
|
|
A3 => addr_write(3), -- Port A address[3] input bit
|
171 |
|
|
D => reg_dest_new(i), -- Port A 1-bit data input
|
172 |
|
|
DPRA0 => addr_read1(0), -- Port B address[0] input bit
|
173 |
|
|
DPRA1 => addr_read1(1), -- Port B address[1] input bit
|
174 |
|
|
DPRA2 => addr_read1(2), -- Port B address[2] input bit
|
175 |
|
|
DPRA3 => addr_read1(3), -- Port B address[3] input bit
|
176 |
|
|
DPO => data_out1A(i), -- Port B 1-bit data output
|
177 |
344 |
rhoads |
SPO => no_connect(i) -- Port A 1-bit data output
|
178 |
139 |
rhoads |
);
|
179 |
|
|
--Read port 1 upper 16 registers
|
180 |
|
|
reg_bit1b : RAM16X1D
|
181 |
|
|
port map (
|
182 |
|
|
WCLK => clk, -- Port A write clock input
|
183 |
|
|
WE => weB, -- Port A write enable input
|
184 |
|
|
A0 => addr_write(0), -- Port A address[0] input bit
|
185 |
|
|
A1 => addr_write(1), -- Port A address[1] input bit
|
186 |
|
|
A2 => addr_write(2), -- Port A address[2] input bit
|
187 |
|
|
A3 => addr_write(3), -- Port A address[3] input bit
|
188 |
|
|
D => reg_dest_new(i), -- Port A 1-bit data input
|
189 |
|
|
DPRA0 => addr_read1(0), -- Port B address[0] input bit
|
190 |
|
|
DPRA1 => addr_read1(1), -- Port B address[1] input bit
|
191 |
|
|
DPRA2 => addr_read1(2), -- Port B address[2] input bit
|
192 |
|
|
DPRA3 => addr_read1(3), -- Port B address[3] input bit
|
193 |
|
|
DPO => data_out1B(i), -- Port B 1-bit data output
|
194 |
344 |
rhoads |
SPO => no_connect(32+i) -- Port A 1-bit data output
|
195 |
139 |
rhoads |
);
|
196 |
|
|
--Read port 2 lower 16 registers
|
197 |
|
|
reg_bit2a : RAM16X1D
|
198 |
|
|
port map (
|
199 |
|
|
WCLK => clk, -- Port A write clock input
|
200 |
|
|
WE => weA, -- Port A write enable input
|
201 |
|
|
A0 => addr_write(0), -- Port A address[0] input bit
|
202 |
|
|
A1 => addr_write(1), -- Port A address[1] input bit
|
203 |
|
|
A2 => addr_write(2), -- Port A address[2] input bit
|
204 |
|
|
A3 => addr_write(3), -- Port A address[3] input bit
|
205 |
|
|
D => reg_dest_new(i), -- Port A 1-bit data input
|
206 |
|
|
DPRA0 => addr_read2(0), -- Port B address[0] input bit
|
207 |
|
|
DPRA1 => addr_read2(1), -- Port B address[1] input bit
|
208 |
|
|
DPRA2 => addr_read2(2), -- Port B address[2] input bit
|
209 |
|
|
DPRA3 => addr_read2(3), -- Port B address[3] input bit
|
210 |
|
|
DPO => data_out2A(i), -- Port B 1-bit data output
|
211 |
344 |
rhoads |
SPO => no_connect(64+i) -- Port A 1-bit data output
|
212 |
139 |
rhoads |
);
|
213 |
|
|
--Read port 2 upper 16 registers
|
214 |
|
|
reg_bit2b : RAM16X1D
|
215 |
|
|
port map (
|
216 |
|
|
WCLK => clk, -- Port A write clock input
|
217 |
|
|
WE => weB, -- Port A write enable input
|
218 |
|
|
A0 => addr_write(0), -- Port A address[0] input bit
|
219 |
|
|
A1 => addr_write(1), -- Port A address[1] input bit
|
220 |
|
|
A2 => addr_write(2), -- Port A address[2] input bit
|
221 |
|
|
A3 => addr_write(3), -- Port A address[3] input bit
|
222 |
|
|
D => reg_dest_new(i), -- Port A 1-bit data input
|
223 |
|
|
DPRA0 => addr_read2(0), -- Port B address[0] input bit
|
224 |
|
|
DPRA1 => addr_read2(1), -- Port B address[1] input bit
|
225 |
|
|
DPRA2 => addr_read2(2), -- Port B address[2] input bit
|
226 |
|
|
DPRA3 => addr_read2(3), -- Port B address[3] input bit
|
227 |
|
|
DPO => data_out2B(i), -- Port B 1-bit data output
|
228 |
344 |
rhoads |
SPO => no_connect(96+i) -- Port A 1-bit data output
|
229 |
139 |
rhoads |
);
|
230 |
|
|
end generate; --reg_loop
|
231 |
115 |
rhoads |
|
232 |
139 |
rhoads |
data_out1 <= data_out1A when addr_read1(4)='0' else data_out1B;
|
233 |
|
|
data_out2 <= data_out2A when addr_read2(4)='0' else data_out2B;
|
234 |
|
|
end generate; --xilinx_16x1d
|
235 |
115 |
rhoads |
|
236 |
|
|
|
237 |
139 |
rhoads |
-- Option #4
|
238 |
|
|
-- Altera LPM_RAM_DP
|
239 |
|
|
altera_mem:
|
240 |
|
|
if memory_type = "ALTERA_LPM" generate
|
241 |
333 |
rhoads |
signal clk_delayed : std_logic;
|
242 |
|
|
signal addr_reg : std_logic_vector(4 downto 0);
|
243 |
|
|
signal data_reg : std_logic_vector(31 downto 0);
|
244 |
|
|
signal q1 : std_logic_vector(31 downto 0);
|
245 |
|
|
signal q2 : std_logic_vector(31 downto 0);
|
246 |
|
|
begin
|
247 |
|
|
-- Altera dual port RAMs must have the addresses registered (sampled
|
248 |
|
|
-- at the rising edge). This is very unfortunate.
|
249 |
|
|
-- Therefore, the dual port RAM read clock must delayed so that
|
250 |
|
|
-- the read address signal can be sent from the mem_ctrl block.
|
251 |
|
|
-- This solution also delays the how fast the registers are read so the
|
252 |
|
|
-- maximum clock speed is cut in half (12.5 MHz instead of 25 MHz).
|
253 |
|
|
|
254 |
|
|
clk_delayed <= not clk; --Could be delayed by 1/4 clock cycle instead
|
255 |
|
|
dpram_bypass: process(clk, addr_write, reg_dest_new)
|
256 |
|
|
begin
|
257 |
|
|
if rising_edge(clk) and write_enable = '1' then
|
258 |
|
|
addr_reg <= addr_write;
|
259 |
|
|
data_reg <= reg_dest_new;
|
260 |
|
|
end if;
|
261 |
|
|
end process; --dpram_bypass
|
262 |
|
|
|
263 |
|
|
-- Bypass dpram if reading what was just written (Altera limitation)
|
264 |
|
|
data_out1 <= q1 when addr_read1 /= addr_reg else data_reg;
|
265 |
|
|
data_out2 <= q2 when addr_read2 /= addr_reg else data_reg;
|
266 |
|
|
|
267 |
139 |
rhoads |
lpm_ram_dp_component1 : lpm_ram_dp
|
268 |
333 |
rhoads |
generic map (
|
269 |
|
|
LPM_WIDTH => 32,
|
270 |
|
|
LPM_WIDTHAD => 5,
|
271 |
|
|
--LPM_NUMWORDS => 0,
|
272 |
|
|
LPM_INDATA => "REGISTERED",
|
273 |
|
|
LPM_OUTDATA => "UNREGISTERED",
|
274 |
|
|
LPM_RDADDRESS_CONTROL => "REGISTERED",
|
275 |
|
|
LPM_WRADDRESS_CONTROL => "REGISTERED",
|
276 |
|
|
LPM_FILE => "UNUSED",
|
277 |
|
|
LPM_TYPE => "LPM_RAM_DP",
|
278 |
|
|
USE_EAB => "ON",
|
279 |
|
|
INTENDED_DEVICE_FAMILY => "UNUSED",
|
280 |
|
|
RDEN_USED => "FALSE",
|
281 |
|
|
LPM_HINT => "UNUSED")
|
282 |
|
|
port map (
|
283 |
|
|
RDCLOCK => clk_delayed,
|
284 |
|
|
RDCLKEN => '1',
|
285 |
|
|
RDADDRESS => addr_read1,
|
286 |
|
|
RDEN => '1',
|
287 |
|
|
DATA => reg_dest_new,
|
288 |
|
|
WRADDRESS => addr_write,
|
289 |
|
|
WREN => write_enable,
|
290 |
|
|
WRCLOCK => clk,
|
291 |
|
|
WRCLKEN => '1',
|
292 |
|
|
Q => q1);
|
293 |
139 |
rhoads |
lpm_ram_dp_component2 : lpm_ram_dp
|
294 |
333 |
rhoads |
generic map (
|
295 |
|
|
LPM_WIDTH => 32,
|
296 |
|
|
LPM_WIDTHAD => 5,
|
297 |
|
|
--LPM_NUMWORDS => 0,
|
298 |
|
|
LPM_INDATA => "REGISTERED",
|
299 |
|
|
LPM_OUTDATA => "UNREGISTERED",
|
300 |
|
|
LPM_RDADDRESS_CONTROL => "REGISTERED",
|
301 |
|
|
LPM_WRADDRESS_CONTROL => "REGISTERED",
|
302 |
|
|
LPM_FILE => "UNUSED",
|
303 |
|
|
LPM_TYPE => "LPM_RAM_DP",
|
304 |
|
|
USE_EAB => "ON",
|
305 |
|
|
INTENDED_DEVICE_FAMILY => "UNUSED",
|
306 |
|
|
RDEN_USED => "FALSE",
|
307 |
|
|
LPM_HINT => "UNUSED")
|
308 |
|
|
port map (
|
309 |
|
|
RDCLOCK => clk_delayed,
|
310 |
|
|
RDCLKEN => '1',
|
311 |
|
|
RDADDRESS => addr_read2,
|
312 |
|
|
RDEN => '1',
|
313 |
|
|
DATA => reg_dest_new,
|
314 |
|
|
WRADDRESS => addr_write,
|
315 |
|
|
WREN => write_enable,
|
316 |
|
|
WRCLOCK => clk,
|
317 |
|
|
WRCLKEN => '1',
|
318 |
|
|
Q => q2);
|
319 |
139 |
rhoads |
end generate; --altera_mem
|
320 |
115 |
rhoads |
|
321 |
12 |
rhoads |
end; --architecture ram_block
|