-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathalu_tb.vhd
95 lines (75 loc) · 2.9 KB
/
alu_tb.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
-- Greg Stitt
-- University of Florida
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity alu_tb is
end alu_tb;
architecture exhaustive of alu_tb is
constant WIDTH : integer := 8;
signal in0 : std_logic_vector(WIDTH-1 downto 0);
signal in1 : std_logic_vector(WIDTH-1 downto 0);
signal sel : std_logic_vector(1 downto 0);
signal zero, pos, neg : std_logic;
signal output : std_logic_vector(WIDTH-1 downto 0);
procedure reference_model
(
in0 : in std_logic_vector(WIDTH-1 downto 0);
in1 : in std_logic_vector(WIDTH-1 downto 0);
sel : in std_logic_vector(1 downto 0);
neg : out std_logic;
pos : out std_logic;
zero : out std_logic;
output : out std_logic_vector(WIDTH-1 downto 0)) is
variable temp : signed(WIDTH-1 downto 0);
begin
pos := '0';
neg := '0';
zero := '0';
case sel is
when "00" => temp := signed(in0) + signed(in1);
when "01" => temp := signed(in0) - signed(in1);
when "10" => temp := signed(in0 and in1);
when "11" => temp := signed(in0 or in1);
when others => null;
end case;
if (temp > 0) then pos := '1';
elsif (temp = 0) then zero := '1';
else neg := '1';
end if;
output := std_logic_vector(temp);
end procedure;
begin -- TB
UUT : entity work.alu
generic map (WIDTH => WIDTH)
port map (
in0 => in0,
in1 => in1,
sel => sel,
neg => neg,
pos => pos,
zero => zero,
output => output);
process
variable output_correct : std_logic_vector(WIDTH-1 downto 0);
variable neg_correct, pos_correct, zero_correct : std_logic;
begin
for i in 0 to 2**WIDTH-1 loop
for j in 0 to 2**WIDTH-1 loop
for k in 0 to (2**sel'length)-1 loop
in0 <= std_logic_vector(to_unsigned(i, WIDTH));
in1 <= std_logic_vector(to_unsigned(j, WIDTH));
sel <= std_logic_vector(to_unsigned(k, 2));
wait for 10 ns;
reference_model(in0, in1, sel, neg_correct, pos_correct, zero_correct, output_correct);
assert(output = output_correct);
assert(neg = neg_correct or sel = "10" or sel = "11");
assert(pos = pos_correct or sel = "10" or sel = "11");
assert(zero = zero_correct or sel = "10" or sel = "11");
end loop; --k
end loop; -- j
end loop; -- i
report "Tests completed.";
wait;
end process;
end exhaustive;