-
Notifications
You must be signed in to change notification settings - Fork 0
/
ram.vhd
57 lines (50 loc) · 1.62 KB
/
ram.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-------------------------------------------------------
--! @ram.vhd
--! @brief Descrição da RAM do PoliLeg
--! @author Tiago M Lucio ([email protected])
--! @date 2022-06-12
-------------------------------------------------------
library ieee;
use ieee.numeric_bit.all;
use std.textio.all;
entity ram is
generic (
addr_s: natural := 64; -- Size in bits
word_s: natural := 32; -- Width in bits
init_f: string := "ram.dat"
);
port (
ck : in bit;
rd, wr: in bit; -- enables (read and write)
addr : in bit_vector(addr_s - 1 downto 0);
data_i : in bit_vector(word_s - 1 downto 0);
data_o : out bit_vector(word_s - 1 downto 0 )
);
end ram;
architecture arch of ram is
constant depth : natural := 2 ** addr_s;
type mem_type is array (0 to depth-1) of bit_vector(word_s-1 downto 0);
impure function init_mem(file_name : in string) return mem_type is
file f : text open read_mode is file_name;
variable l : line;
variable tmp_bv : bit_vector(word_s-1 downto 0);
variable tmp_mem : mem_type;
begin
for i in mem_type'range loop
readline(f, l);
read(l, tmp_bv);
tmp_mem(i) := tmp_bv;
end loop;
return tmp_mem;
end;
--! Memory matrix
signal mem : mem_type := init_mem(init_f);
begin
process(ck)
begin
if (rising_edge(ck) and wr='1') then
mem(to_integer(unsigned(addr))) <= data_i;
end if;
end process;
data_o <= mem(to_integer(unsigned(addr))) when rd = '1';
end arch ; -- arch