-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataMemory.vhd
More file actions
77 lines (60 loc) · 2.7 KB
/
DataMemory.vhd
File metadata and controls
77 lines (60 loc) · 2.7 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
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.numeric_std.ALL;
ENTITY DataMemory IS
GENERIC (
ADDRESS_BITS : INTEGER := 12
);
PORT (
clk : IN STD_LOGIC;
memread : IN STD_LOGIC;
memwrite : IN STD_LOGIC;
protect : IN STD_LOGIC;
free : IN STD_LOGIC;
addr : IN STD_LOGIC_VECTOR(31 DOWNTO 0);
datain : IN STD_LOGIC_VECTOR(31 DOWNTO 0);
dataout : OUT STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0')
);
END DataMemory;
ARCHITECTURE DataMemoryArch OF DataMemory IS
-- extra bit is for protection and liberation
TYPE ram_type IS ARRAY(0 TO (2 ** ADDRESS_BITS - 1)) OF STD_LOGIC_VECTOR(16 DOWNTO 0);
SIGNAL ram : ram_type := (OTHERS => (OTHERS => '0'));
BEGIN
PROCESS (clk) IS
VARIABLE helper33 : STD_LOGIC_VECTOR(33 DOWNTO 0) := (OTHERS => '0');
VARIABLE helper32 : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0');
VARIABLE helper12 : STD_LOGIC_VECTOR(11 DOWNTO 0) := (OTHERS => '0');
BEGIN
IF falling_edge(clk) THEN
helper33(16 DOWNTO 0) := ram(to_integer(UNSIGNED(addr(11 DOWNTO 0))));
helper12 := STD_LOGIC_VECTOR(UNSIGNED(addr(11 DOWNTO 0)) + 1);
helper33(33 DOWNTO 17) := ram(to_integer(UNSIGNED(helper12)));
IF protect = '1' THEN
helper33(16) := '1';
helper33(33) := '1';
ram(to_integer(UNSIGNED(addr(11 DOWNTO 0)))) <= helper33(16 DOWNTO 0);
ram(to_integer(UNSIGNED(helper12))) <= helper33(33 DOWNTO 17);
ELSIF free = '1' THEN
helper33(16) := '0';
helper33(33) := '0';
ram(to_integer(UNSIGNED(addr(11 DOWNTO 0)))) <= helper33(16 DOWNTO 0);
ram(to_integer(UNSIGNED(helper12))) <= helper33(33 DOWNTO 17);
ELSIF memwrite = '1' THEN
IF helper33(16) = '0' AND helper33(33) = '0' THEN
helper33(15 DOWNTO 0) := datain(15 DOWNTO 0);
helper33(32 DOWNTO 17) := datain(31 DOWNTO 16);
ram(to_integer(UNSIGNED(addr(11 DOWNTO 0)))) <= helper33(16 DOWNTO 0);
ram(to_integer(UNSIGNED(helper12))) <= helper33(33 DOWNTO 17);
END IF;
ELSE
helper33(16 DOWNTO 0) := ram(to_integer(UNSIGNED(addr(11 DOWNTO 0))));
helper33(33 DOWNTO 17) := ram(to_integer(UNSIGNED(helper12)));
-- 32 to dataout
helper32(15 DOWNTO 0) := helper33(15 DOWNTO 0);
helper32(31 DOWNTO 16) := helper33(32 DOWNTO 17);
dataout <= helper32;
END IF;
END IF;
END PROCESS;
END DataMemoryArch; -- DataMemoryArch