TODOs und kleine anpassungen bei TBs fuer die ALU
[hwmod.git] / src / post_alu_tb.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4 use work.gen_pkg.all;
5
6 entity post_alu_tb is
7 end entity post_alu_tb;
8
9 architecture sim of post_alu_tb is
10         component alu is
11                 port
12                 (
13                         sys_clk : in std_logic;
14                         sys_res_n : in std_logic;
15                         opcode : in alu_ops;
16                         op1 : in std_logic_vector(31 downto 0);
17                         op2 : in std_logic_vector(31 downto 0);
18                         op3 : out std_logic_vector(31 downto 0);
19                         do_calc : in std_logic;
20                         calc_done : out std_logic
21                 );
22         end component alu;
23
24         signal sys_clk, sys_res_n, do_calc, calc_done : std_logic;
25         signal opcode : alu_ops;
26         signal op1, op2, op3 : std_logic_vector(31 downto 0);
27         signal stop : boolean := false;
28 begin
29         inst : alu
30         port map
31         (
32                 sys_clk => sys_clk,
33                 sys_res_n => sys_res_n,
34                 do_calc => do_calc,
35                 calc_done => calc_done,
36                 op1 => op1,
37                 op2 => op2,
38                 op3 => op3,
39                 opcode => opcode
40         );
41
42         process
43         begin
44                 sys_clk <= '0';
45                 wait for 15 ns;
46                 sys_clk <= '1';
47                 wait for 15 ns;
48                 if stop = true then
49                         wait;
50                 end if;
51         end process;
52
53         process
54                 type alu_testv is record
55                         o1 : cinteger;
56                         o : alu_ops;
57                         o2 : cinteger;
58                         expected : cinteger;
59                 end record alu_testv;
60
61                 -- ggf. groesse des arrays erhoehen
62                 type alu_testv_array is array (natural range 0 to 20) of alu_testv;
63
64                 variable testmatrix : alu_testv_array :=
65                         ( 0 => (-5, ALU_DIV, 3, -1),
66                           1 => (7, ALU_ADD, 3, 10),
67                           2 => (7, ALU_SUB, 1, 6),
68                           3 => (7, ALU_DIV, 1, 7),
69                           4 => (7, ALU_DIV, 3, 2),
70                           5 => (7, ALU_ADD, 1, 8),
71                           6 => (7, ALU_MUL, 3, 21),
72                           7 => (-7, ALU_MUL, 3, -21),
73                           8 => (268435456, ALU_MUL, -2, -536870912),
74                           9 => (268435456, ALU_MUL, 2**5, 0), -- um fuenf nach links shiften
75                           10 => (268435456 + 5, ALU_MUL, 2**5, 160), -- = 5 * (2^5)
76                           11 => (100, ALU_DIV, 10, 10),
77                           12 => (100, ALU_DIV, 51, 1),
78                           13 => (100, ALU_DIV, 49, 2),
79                           14 => (153156, ALU_DIV, 3543, 43),
80                           15 => (-153156, ALU_DIV, 3543, -43),
81                           16 => (153156, ALU_DIV, -3543, -43),
82                           17 => (-153156, ALU_DIV, -3543, 43),
83                           others => (0, ALU_ADD, 0, 0)
84                         );
85
86         begin
87                 -- init & reset
88                 sys_res_n <= '0';
89                 do_calc <= '0';
90                 opcode <= ALU_NOP;
91                 op1 <= (others => '0');
92                 op2 <= (others => '0');
93
94                 wait for 300 ns;
95                 sys_res_n <= '1';
96
97                 for i in testmatrix'range loop
98                         wait for 100 ns;
99                         op1 <= std_logic_vector(to_signed(testmatrix(i).o1,CBITS));
100                         opcode <= testmatrix(i).o;
101                         op2 <= std_logic_vector(to_signed(testmatrix(i).o2,CBITS));
102
103                         -- berechnung kann los gehen
104                         do_calc <= '1';
105
106                         -- warten auf die alu einheit
107                         wait on calc_done;
108                         wait for 80 ns;
109
110                         assert op3 = std_logic_vector(to_signed(testmatrix(i).expected,CBITS))
111                                 report "" & cinteger'image(testmatrix(i).o1) & 
112                                 " " & integer'image(to_integer(signed(opcode))) &
113                                 " " & cinteger'image(testmatrix(i).o2) &
114                                 "/= " & integer'image(to_integer(signed(op3))) &
115                                 " -- erwartet: " & cinteger'image(testmatrix(i).expected);
116
117                         wait for 5 ns;
118                         -- ack it!
119                         do_calc <= '0';
120                 end loop;
121
122                 assert false
123                         report "alle testfaelle der ALU waren erfolgreich!";
124                 stop <= true;
125                 wait;
126         end process;
127 end architecture sim;