Update README.md
[mymicroprocessor.git] / Lab_1 / ParameterizableALU / ALU_param_TB.vhd
blob44529135707b37f57d63661529913d4e2734934c
1 ----------------------------------------------------------------------------------
2 --
3 -- Uni : University of York
4 -- Course : Electronic Engineering
5 -- Module : Computer Architectures
6 -- Engineers : Y3839090 & Y3840426
7 --
8 -- Create Date : 14:47:27 02/17/2017
9 -- Design Name : ALU_param_TB - TestBench
10 -- Description : A testbench for a 16 bit integer ALU.
12 ----------------------------------------------------------------------------------
13 LIBRARY ieee;
14 USE ieee.std_logic_1164.ALL;
15 USE ieee.numeric_std.ALL;
16 USE work.DigEng.ALL;
18 entity ALU_param_TB is
19 end ALU_param_TB;
21 architecture behavior of ALU_param_TB is
24 -- ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
25 -- From http://stackoverflow.com/a/24336034 By Morten Zilmer
26 -- Allows printing a std_logic_vector as a string that represents it's binary form.
27 -- ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
28 function to_bstring(sl : std_logic) return string is
29 variable sl_str_v : string(1 to 3); -- std_logic image with quotes around
30 begin
31 sl_str_v := std_logic'image(sl);
32 return "" & sl_str_v(2); -- "" & character to get string
33 end function;
35 function to_bstring(slv : std_logic_vector) return string is
36 alias slv_norm : std_logic_vector(1 to slv'length) is slv;
37 variable sl_str_v : string(1 to 1); -- String of std_logic
38 variable res_v : string(1 to slv'length);
39 begin
40 for idx in slv_norm'range loop
41 sl_str_v := to_bstring(slv_norm(idx));
42 res_v(idx) := sl_str_v(1);
43 end loop;
44 return res_v;
45 end function;
46 -- ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
49 -- converts an std_logic_vector to a string that represents it's signed value
50 function s_tostr(val : std_logic_vector) return string is
51 begin
52 return integer'image( to_integer(signed(val)) );
53 end function;
56 -- Constants
57 constant M : NATURAL := 16; -- We are testing a 16 bit ALU
58 constant wait_time : TIME := 10 ns;
60 -- Component Declaration for the Unit Under Test (UUT)
61 component ALU_param
62 generic
64 N : NATURAL -- The number of bits this alu will operate on
66 port
68 A : IN STD_LOGIC_VECTOR(M-1 downto 0);
69 B : IN STD_LOGIC_VECTOR(M-1 downto 0);
70 X : IN STD_LOGIC_VECTOR(log2(M)-1 downto 0);
71 ctrl : IN STD_LOGIC_VECTOR(3 downto 0);
72 O : OUT STD_LOGIC_VECTOR(M-1 downto 0);
73 flags : OUT STD_LOGIC_VECTOR(7 downto 0)
75 end component;
78 --Inputs
79 signal A : STD_LOGIC_VECTOR(M-1 downto 0) := (others => '0');
80 signal B : STD_LOGIC_VECTOR(M-1 downto 0) := (others => '0');
81 signal X : STD_LOGIC_VECTOR(log2(M)-1 downto 0) := (others => '0');
82 signal ctrl : STD_LOGIC_VECTOR(3 downto 0) := (others => '0');
84 --Outputs
85 signal O : STD_LOGIC_VECTOR(M-1 downto 0);
86 signal flags : STD_LOGIC_VECTOR(7 downto 0);
89 type TEST_VECTOR is RECORD
90 ctrl : STD_LOGIC_VECTOR(3 downto 0);
91 A : STD_LOGIC_VECTOR(M-1 downto 0);
92 B : STD_LOGIC_VECTOR(M-1 downto 0);
93 X : STD_LOGIC_VECTOR(log2(M)-1 downto 0);
94 O : STD_LOGIC_VECTOR(M-1 downto 0); -- The expected output of the alu
95 flags : STD_LOGIC_VECTOR(7 downto 0); -- The expected flags
96 end RECORD;
98 type TEST_VECTOR_ARRAY is ARRAY(NATURAL range <>) of TEST_VECTOR;
100 constant test_vectors : TEST_VECTOR_ARRAY := (
101 -- CTRL, A , B , X , O , FLAGS
103 -- A
104 ("0000", X"0000", X"0000", X"0", X"0000", "01100001"),
105 ("0000", X"FF9C", X"0000", X"0", X"FF9C", "00101010"),
106 ("0000", X"03E8", X"0000", X"0", X"03E8", "01010010"),
107 ("0000", X"0001", X"0000", X"0", X"0001", "01010110"),
109 -- A and B
110 ("0100", X"0000", X"0000", X"0", X"0000", "01100001"),
111 ("0100", X"FF94", X"FC17", X"0", X"FC14", "00101010"),
112 ("0100", X"03E8", X"5213", X"0", X"0200", "01010010"),
113 ("0100", X"FFFF", X"10E0", X"0", X"10E0", "01010010"),
114 ("0100", X"55FF", X"AAAA", X"0", X"00AA", "01010010"),
116 -- A or B
117 ("0101", X"0000", X"0000", X"0", X"0000", "01100001"),
118 ("0101", X"FF94", X"FC17", X"0", X"FF97", "00101010"),
119 ("0101", X"03E8", X"5213", X"0", X"53FB", "01010010"),
120 ("0101", X"FFFF", X"10E0", X"0", X"FFFF", "00101010"),
121 ("0101", X"5555", X"AAAA", X"0", X"FFFF", "00101010"),
123 -- A xor B
124 ("0110", X"0000", X"0000", X"0", X"0000", "01100001"),
125 ("0110", X"FF94", X"FC17", X"0", X"0383", "01010010"),
126 ("0110", X"03E8", X"5213", X"0", X"51FB", "01010010"),
127 ("0110", X"FFFF", X"10E0", X"0", X"EF1F", "00101010"),
128 ("0110", X"5555", X"AAAA", X"0", X"FFFF", "00101010"),
130 -- not A
131 ("0111", X"0000", X"0000", X"0", X"FFFF", "00101010"),
132 ("0111", X"FFFF", X"0000", X"0", X"0000", "01100001"),
133 ("0111", X"8111", X"0000", X"0", X"7EEE", "01010010"),
134 ("0111", X"0001", X"0000", X"0", X"FFFE", "00101010"),
136 -- A + 1
137 ("1000", X"0000", X"0000", X"0", X"0001", "01010110"),
138 ("1000", X"FFFF", X"0000", X"0", X"0000", "01100001"),
139 ("1000", X"7fff", X"0000", X"0", X"8000", "10101010"),
141 -- A - 1
142 ("1001", X"0000", X"0000", X"0", X"ffff", "00101010"),
143 ("1001", X"0001", X"0000", X"0", X"0000", "01100001"),
144 ("1001", X"8000", X"0000", X"0", X"7fff", "11010010"),
146 -- A + B
147 ("1010", X"0000", X"0000", X"0", X"0000", "01100001"),
148 ("1010", X"0310", X"0a00", X"0", X"0D10", "01010010"),
149 ("1010", X"09E2", X"f43e", X"0", X"FE20", "00101010"),
150 ("1010", X"7fff", X"0001", X"0", X"8000", "10101010"),
152 -- A - B
153 ("1011", X"0000", X"0000", X"0", X"0000", "01100001"),
154 ("1011", X"0310", X"0a00", X"0", X"F910", "00101010"),
155 ("1011", X"09E2", X"0BC2", X"0", X"FE20", "00101010"),
156 ("1011", X"8000", X"0001", X"0", X"7fff", "11010010"),
159 -- A sla x
160 ("1100", X"0000", X"0000", X"0", X"0000", "01100001"),
161 ("1100", X"0000", X"0000", X"4", X"0000", "01100001"),
162 ("1100", X"1111", X"0000", X"1", X"2222", "01010010"),
163 ("1100", X"1111", X"0000", X"3", X"8888", "00101010"),
164 ("1100", X"5555", X"0000", X"9", X"AA00", "00101010"),
166 -- A sra x
167 ("1101", X"0000", X"0000", X"0", X"0000", "01100001"),
168 ("1101", X"0000", X"0000", X"4", X"0000", "01100001"),
169 ("1101", X"8888", X"0000", X"1", X"C444", "00101010"),
170 ("1101", X"8888", X"0000", X"3", X"f111", "00101010"),
171 ("1101", X"AAAA", X"0000", X"9", X"FFD5", "00101010"),
173 -- A rotl x
174 ("1110", X"0000", X"0000", X"2", X"0000", "01100001"),
175 ("1110", X"8888", X"0000", X"1", X"1111", "01010010"),
176 ("1110", X"8101", X"0000", X"1", X"0203", "01010010"),
178 -- A rotr x
179 ("1111", X"0000", X"0000", X"2", X"0000", "01100001"),
180 ("1111", X"1111", X"0000", X"1", X"8888", "00101010"),
181 ("1111", X"1081", X"0000", X"1", X"8840", "00101010"),
183 -- THIS TEST WILL FAIL
184 ("1010", X"0000", X"0000", X"0", X"FFFF", "10000001")
188 begin
190 -- Instantiate the Unit Under Test (UUT)
191 uut: ALU_param
192 generic map
194 N => M
196 port map
198 A => A,
199 B => B,
200 X => X,
201 ctrl => ctrl,
202 O => O,
203 flags => flags
206 -- Stimulus process
207 stim_proc: process
208 begin
209 -- hold reset state for 100 ns.
210 wait for 100 ns;
212 -- run the test for every set of data
213 for i in test_vectors'range loop
215 -- assign test inputs
216 ctrl <= test_vectors(i).ctrl;
217 A <= test_vectors(i).A;
218 B <= test_vectors(i).B;
219 X <= test_vectors(i).X;
221 -- wait long enough for the ALU to process
222 wait for wait_time;
224 -- check that the actual output is the same as the expect output
225 assert O = test_vectors(i).O
226 report " [ERR!] Test " & integer'image(i)&
227 " Actual output did not equal expected output: Actual " & s_tostr(O) &
228 " , Expected " & s_tostr(test_vectors(i).O)
229 severity error;
231 -- check that the actual flags is the same as the expect flags
232 assert flags = test_vectors(i).flags
233 report " [ERR!] Test " & integer'image(i)&
234 " Actual flags did not equal expected flags : Actual " & to_bstring(flags) &
235 " , Expected " & to_bstring(test_vectors(i).flags)
236 severity error;
238 -- if there were no isses report that the test was successful
239 assert not ( O = test_vectors(i).O and flags = test_vectors(i).flags )
240 report " [ OK ] Test " & integer'image(i)& " was successful!"
241 severity note;
243 wait for wait_time;
245 end loop;
247 wait;
248 end process;
250 end;