|
% cat rom64x8.vhd
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
use std.textio.all;
entity rom64x8 is
port(
cs : in std_logic;
addr : in std_logic_vector (5 downto 0);
data : out std_logic_vector (7 downto 0)
);
end rom64x8;
architecture logic of rom64x8 is
function i2v (ai : integer) return std_logic_vector is
variable result: std_logic_vector (7 downto 0);
variable temp : integer;
begin
temp := ai;
for i in 0 to 7 loop
if (temp mod 2) = 1 then
result(i) := '1';
else
result(i) := '0';
end if;
if result(i) = '1' then
temp := (temp - 1) / 2;
else
temp := temp / 2;
end if;
end loop;
return result;
end i2v;
begin
process (cs, addr)
file rom_data_file : TEXT is in "rom64x8.dat";
type dtype is array (0 to 63) of std_logic_vector(7 downto 0);
variable myline : line;
variable rom_init : boolean := false;
variable rom_data : dtype;
variable i, datain : integer :=0;
begin
if (rom_init = false) then
while not endfile(rom_data_file)
and (i < 64) loop
readline(rom_data_file, myline);
read(myline, datain);
rom_data(i) := i2v(datain);
i := i + 1;
end loop;
rom_init := true;
end if;
if cs='1' then
data <= rom_data(Conv_Integer(addr));
else
data <= "11111111";
end if;
end process;
end logic;
% cat rom64x8_tb.vhd
library IEEE;
use IEEE.std_logic_unsigned.all;
use IEEE.std_logic_1164.all;
entity rom64x8_tb is
end rom64x8_tb;
architecture TB_ARCHITECTURE of rom64x8_tb is
component rom64x8
port(
cs : in std_logic;
addr : in std_logic_vector(5 downto 0);
data : out std_logic_vector(7 downto 0)
);
end component;
signal clk,cs : std_logic;
signal addr : std_logic_vector(5 downto 0);
signal data : std_logic_vector(7 downto 0);
begin
UUT: rom64x8 port map (
cs => cs,
addr => addr,
data => data
);
clk0: process begin
clk <= '0'; wait for 5 ns;
clk <= '1'; wait for 5 ns;
end process;
addr0: process begin
if cs = '0' then
addr <= "000000";
else
addr <= addr + "000001";
end if;
wait for 10 ns;
end process;
datain: process begin
cs <= '0';
wait for 11 ns;
cs <= '1';
wait;
end process;
end TB_ARCHITECTURE;
% cat rom64x8.dat
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 |
|