-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemtest.vhd
147 lines (129 loc) · 3.42 KB
/
memtest.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- Quartus II VHDL Template
-- Four-State Moore State Machine
-- A Moore machine's outputs are dependent only on the current state.
-- The output is written only when the state changes. (State
-- transitions are synchronous.)
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity memtest is
port(
clk : in std_logic;
reset : in std_logic;
faster : in std_logic;
hold : in std_logic;
output : out std_logic_vector(7 downto 0)
);
end entity;
architecture rtl of memtest is
--component declaration of ROM
component memrom
PORT
(
address : IN STD_LOGIC_VECTOR (3 DOWNTO 0);
clock : IN STD_LOGIC := '1';
q : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
);
end component;
component memRAM
PORT
(
address : IN STD_LOGIC_VECTOR (3 DOWNTO 0);
clock : IN STD_LOGIC := '1';
data : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
wren : IN STD_LOGIC ;
q : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
);
end component;
--signals
signal ROMaddress : unsigned(3 downto 0);
signal ROMdata : std_logic_vector(7 downto 0);
signal ROMwire : std_logic_vector(7 downto 0);
signal RAMaddress : unsigned(3 downto 0);
signal RAMdata : std_logic_vector(7 downto 0);
signal RAMwire : std_logic_vector(7 downto 0);
signal slowclock : std_logic;
signal counter: unsigned (25 downto 0);
signal speed: integer := 24;
signal writeEnable : std_logic;
signal startupcounter : unsigned(1 downto 0);
-- state declaration
signal state : unsigned(3 downto 0);
--constants
constant zeros : unsigned(25 downto 0) := (others => '0');
begin
ROM1: memrom
port map(address => std_logic_vector(ROMaddress), clock => clk, q => ROMwire);
RAM1: memRAM
port map(address => std_logic_vector(RAMaddress), clock => clk, data => RAMdata, wren => writeEnable, q => RAMwire);
-- process to slow down clock cycle
process (clk, reset)
begin
if reset = '0' then
counter <= zeros;
elsif (rising_edge(clk)) then
counter <= counter + 1;
end if;
end process;
process (faster)
begin
if falling_edge(faster) and speed > 15 then
speed <= speed - 1;
elsif falling_edge(faster) and speed = 15 then
speed <= 25;
end if;
end process;
slowclock <= counter(speed);
-- Logic to advance to the next state
process (slowclock, reset)
begin
if reset = '0' then
ROMaddress <= "0000";
RAMaddress <= "0000";
state <= "0000";
ROMdata <= "00000000";
RAMdata <= "00000000";
startupcounter <= "00";
writeEnable <= '0';
elsif (rising_edge(slowclock)) then
if hold = '0' then
case state is
when "0000" =>
startupcounter <= startupcounter + 1;
if startupcounter = "10" then
state <= state + 1;
end if;
when "0001" =>
RAMdata <= RAMwire;
RAMaddress <= RAMaddress + 1;
if RAMaddress = "1111" then
state <= state + 1;
end if;
when "0010" =>
state <= state + 1;
when "0011" =>
ROMdata <= ROMwire;
state <= state + 1;
when "0100" =>
RAMdata <= ROMdata;
ROMaddress <= ROMaddress + 1;
state <= state + 1;
when "0101" =>
writeEnable <= '1';
state <= state + 1;
when "0110" =>
writeEnable <= '0';
RAMaddress <= RAMaddress + 1;
if RAMaddress = "1111" then
state <= "0001";
else
state <= "0011";
end if;
when others =>
state <= "0001";
end case;
end if;
end if;
end process;
output <= RAMdata;
end rtl;