-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.vhd
More file actions
114 lines (99 loc) · 3.45 KB
/
Memory.vhd
File metadata and controls
114 lines (99 loc) · 3.45 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-- This file contains the description of a simple, single-port memory.
--
-- * The data_out port updates asynchronously when read_en is high.
-- * The memory is updated synchronously (on a rising clock edge) from
-- data_in when write_en is high.
--
-- This memory can be initialized from an input file, and can dump its
-- contents to an output file. To use this, you must assign the INPUT and
-- OUTPUT generics when instantiating a memory component, e.g.:
--
-- data_mem : entity work.memory
-- generic map (
-- INPUT => "in_data.txt",
-- OUTPUT => "out_data.txt")
-- port map ( ... );
--
-- The input file must have 33 lines, 8 bits per line. The output file is
-- written in the same format, whenever the mem_dump input goes high.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use std.textio.all;
entity Memory is
generic (
INPUT : string := "in.txt";
OUTPUT : string := "out.txt"
);
port (
clk : in std_logic;
read_en : in std_logic;
write_en : in std_logic;
addr : in std_logic_vector(15 downto 0);
data_in : in std_logic_vector(15 downto 0);
data_out : out std_logic_vector(15 downto 0);
mem_dump : in std_logic := '0'
);
end entity Memory;
architecture beh of Memory is
type mem_type is array (0 to 33) of std_logic_vector(7 downto 0);
impure function init_mem return mem_type is
file in_file : text;
variable in_line : line;
variable byte : bit_vector(7 downto 0);
variable mem : mem_type;
begin
if INPUT'length = 0 then
return mem;
end if;
file_open(in_file, INPUT, READ_MODE);
for row in mem_type'range loop
readline(in_file, in_line);
read(in_line, byte);
mem(row) := to_stdlogicvector(byte);
end loop;
return mem;
end function init_mem;
signal mem : mem_type := init_mem;
-- procedure dump_mem is
-- file out_file : text;
-- variable out_line : line;
-- variable byte : bit_vector(7 downto 0);
--
-- begin
-- if OUTPUT'length = 0 then
-- report "The mem_dump port was triggered, but no file name was";
-- report "given. Specify a file name for the generic OUTPUT in";
-- report "the entity declaration to enable memory dump.";
-- return;
-- end if;
--
-- file_open(out_file, OUTPUT, WRITE_MODE);
-- for row in mem_type'range loop
-- byte := to_bitvector(mem(row));
-- write(out_line, byte);
-- writeline(out_file, out_line);
-- end loop;
-- end procedure dump_mem;
begin
read_p : process (read_en, mem, addr) is
begin
if read_en = '1' then
data_out(7 downto 0) <= mem(conv_integer(addr(7 downto 0)));
data_out(15 downto 8) <= mem(conv_integer(addr(7 downto 0))+1);
end if;
end process read_p;
write_p : process (clk) is
begin
if rising_edge(clk) and write_en = '1' then
mem(conv_integer(addr(7 downto 0))) <= data_in(7 downto 0);
mem(conv_integer(addr(7 downto 0))+1) <= data_in(15 downto 8);
end if;
end process write_p;
-- mem_dump_p : process(mem_dump) is
-- begin
-- if rising_edge(mem_dump) then
-- dump_mem;
-- end if;
-- end process mem_dump_p;
end architecture beh;