/
Testbench.vhdl
139 lines (111 loc) · 4.25 KB
/
Testbench.vhdl
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
library std;
use std.textio.all;
library ieee;
use ieee.std_logic_1164.all;
entity Testbench is
end entity;
architecture Behave of Testbench is
----------------------------------------------------------------
-- edit the following lines to set the number of i/o's of your
-- DUT.
----------------------------------------------------------------
constant number_of_inputs : integer := 1; -- # input bits to your design.
constant number_of_outputs : integer := 0; -- # output bits from your design.
----------------------------------------------------------------
----------------------------------------------------------------
-- Note that you will have to wrap your design into the DUT
-- as indicated in class.
component DUT is
port(input_vector: in std_logic_vector(number_of_inputs-1 downto 0));
end component;
signal input_vector : std_logic_vector(number_of_inputs-1 downto 0);
signal output_vector : std_logic_vector(number_of_outputs-1 downto 0);
-- create a constrained string
function to_string(x: string) return string is
variable ret_val: string(1 to x'length);
alias lx : string (1 to x'length) is x;
begin
ret_val := lx;
return(ret_val);
end to_string;
-- bit-vector to std-logic-vector and vice-versa
function to_std_logic_vector(x: bit_vector) return std_logic_vector is
alias lx: bit_vector(1 to x'length) is x;
variable ret_val: std_logic_vector(1 to x'length);
begin
for I in 1 to x'length loop
if(lx(I) = '1') then
ret_val(I) := '1';
else
ret_val(I) := '0';
end if;
end loop;
return ret_val;
end to_std_logic_vector;
function to_bit_vector(x: std_logic_vector) return bit_vector is
alias lx: std_logic_vector(1 to x'length) is x;
variable ret_val: bit_vector(1 to x'length);
begin
for I in 1 to x'length loop
if(lx(I) = '1') then
ret_val(I) := '1';
else
ret_val(I) := '0';
end if;
end loop;
return ret_val;
end to_bit_vector;
begin
process
variable err_flag : boolean := false;
File INFILE: text open read_mode is "TRACEFILE.txt";
FILE OUTFILE: text open write_mode is "outputs.txt";
-- bit-vectors are read from the file.
variable input_vector_var: bit_vector (number_of_inputs-1 downto 0);
variable output_vector_var: bit_vector (number_of_outputs-1 downto 0);
variable output_mask_var: bit_vector (number_of_outputs-1 downto 0);
-- for comparison of output with expected-output
variable output_comp_var: std_logic_vector (number_of_outputs-1 downto 0);
constant ZZZZ : std_logic_vector(number_of_outputs-1 downto 0) := (others => '0');
-- for read/write.
variable INPUT_LINE: Line;
variable OUTPUT_LINE: Line;
variable LINE_COUNT: integer := 0;
begin
while not endfile(INFILE) loop
-- will read a new line every 5ns, apply input,
-- wait for 1 ns for circuit to settle.
-- read output.
LINE_COUNT := LINE_COUNT + 1;
-- read input at current time.
readLine (INFILE, INPUT_LINE);
read (INPUT_LINE, input_vector_var);
read (INPUT_LINE, output_vector_var);
read (INPUT_LINE, output_mask_var);
-- apply input.
input_vector <= to_std_logic_vector(input_vector_var);
-- wait for the circuit to settle
wait for 20 ns;
-- check output.
output_comp_var := (to_std_logic_vector(output_mask_var) and
(output_vector xor to_std_logic_vector(output_vector_var)));
if (output_comp_var /= ZZZZ) then
write(OUTPUT_LINE,to_string("ERROR: line "));
write(OUTPUT_LINE, LINE_COUNT);
writeline(OUTFILE, OUTPUT_LINE);
err_flag := true;
end if;
write(OUTPUT_LINE, to_bit_vector(input_vector));
write(OUTPUT_LINE, to_string(" "));
write(OUTPUT_LINE, to_bit_vector(output_vector));
writeline(OUTFILE, OUTPUT_LINE);
-- advance time by 4 ns.
wait for 20 ns;
end loop;
assert (err_flag) report "SUCCESS, all tests passed." severity note;
assert (not err_flag) report "FAILURE, some tests failed." severity error;
wait;
end process;
dut_instance: DUT
port map(input_vector => input_vector);
end Behave;