makefile: verbessert
[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.NOP\ : in std_logic;
16                         \opcode.SUB\ : in std_logic;
17                         \opcode.ADD\ : in std_logic;
18                         \opcode.MUL\ : in std_logic;
19                         \opcode.DIV\ : in std_logic;
20                         \opcode.DONE\ : in std_logic;
21                         op1 : in std_logic_vector(31 downto 0);
22                         op2 : in std_logic_vector(31 downto 0);
23                         op3 : out std_logic_vector(31 downto 0);
24                         do_calc : in std_logic;
25                         calc_done : out std_logic
26                 );
27         end component alu;
28
29         signal sys_clk, sys_res_n, do_calc, calc_done : std_logic;
30         signal opcode : alu_ops;
31         signal \opcode.NOP\ : std_logic;
32         signal \opcode.SUB\ : std_logic;
33         signal \opcode.ADD\ : std_logic;
34         signal \opcode.MUL\ : std_logic;
35         signal \opcode.DIV\ : std_logic;
36         signal \opcode.DONE\ : std_logic;
37         signal op1, op2, op3 : std_logic_vector(31 downto 0);
38         signal stop : boolean := false;
39 begin
40         inst : alu
41         port map
42         (
43                 sys_clk => sys_clk,
44                 sys_res_n => sys_res_n,
45                 do_calc => do_calc,
46                 calc_done => calc_done,
47                 op1 => op1,
48                 op2 => op2,
49                 op3 => op3,
50                 \opcode.NOP\ => \opcode.NOP\,
51                 \opcode.SUB\ => \opcode.SUB\,
52                 \opcode.ADD\ => \opcode.ADD\,
53                 \opcode.MUL\ => \opcode.MUL\,
54                 \opcode.DIV\ => \opcode.DIV\,
55                 \opcode.DONE\ => \opcode.DONE\
56         );
57
58         process
59         begin
60                 sys_clk <= '0';
61                 wait for 15 ns;
62                 sys_clk <= '1';
63                 wait for 15 ns;
64                 if stop = true then
65                         wait;
66                 end if;
67         end process;
68
69         process
70                 type alu_testv is record
71                         o1 : cinteger;
72                         o : alu_ops;
73                         o2 : cinteger;
74                         expected : cinteger;
75                 end record alu_testv;
76
77                 -- ggf. groesse des arrays erhoehen
78                 type alu_testv_array is array (natural range 0 to 20) of alu_testv;
79
80                 variable testmatrix : alu_testv_array :=
81                         ( 0 => (-5, DIV, 3, -1),
82                           1 => (7, ADD, 3, 10),
83                           2 => (7, SUB, 1, 6),
84                           3 => (7, DIV, 1, 7),
85                           4 => (7, DIV, 3, 2),
86                           5 => (7, ADD, 1, 8),
87                           6 => (7, MUL, 3, 21),
88                           7 => (-7, MUL, 3, -21),
89                           8 => (268435456, MUL, -2, -536870912),
90                           9 => (268435456, MUL, 2**5, 0), -- um fuenf nach links shiften
91                           10 => (268435456 + 5, MUL, 2**5, 160), -- = 5 * (2^5)
92                           11 => (100, DIV, 10, 10),
93                           12 => (100, DIV, 51, 1),
94                           13 => (100, DIV, 49, 2),
95                           14 => (153156, DIV, 3543, 43),
96                           15 => (-153156, DIV, 3543, -43),
97                           16 => (153156, DIV, -3543, -43),
98                           17 => (-153156, DIV, -3543, 43),
99                           others => (0, ADD, 0, 0)
100                         );
101
102         begin
103                 sys_res_n <= '0';
104                 wait for 50 ns;
105                 sys_res_n <= '1';
106
107                 for i in testmatrix'range loop
108                         wait for 100 ns;
109                         \opcode.NOP\ <= '0';
110                         \opcode.SUB\ <= '0';
111                         \opcode.ADD\ <= '0';
112                         \opcode.MUL\ <= '0';
113                         \opcode.DIV\ <= '0';
114                         \opcode.DONE\ <= '0';
115                         op1 <= std_logic_vector(to_signed(testmatrix(i).o1,CBITS));
116                         case testmatrix(i).o is
117                                 when NOP => \opcode.NOP\ <= '1';
118                                 when SUB => \opcode.SUB\ <= '1';
119                                 when ADD => \opcode.ADD\ <= '1';
120                                 when MUL => \opcode.MUL\ <= '1';
121                                 when DIV => \opcode.DIV\ <= '1';
122                                 when DONE => \opcode.DONE\ <= '1';
123                         end case;
124                         op2 <= std_logic_vector(to_signed(testmatrix(i).o2,CBITS));
125
126                         -- berechnung kann los gehen
127                         do_calc <= '1';
128
129                         -- warten auf die alu einheit
130                         wait on calc_done;
131
132                         assert op3 = std_logic_vector(to_signed(testmatrix(i).expected,CBITS))
133                                 report "" & cinteger'image(testmatrix(i).o1) & 
134                                 " " & alu_ops'image(opcode) &
135                                 " " & cinteger'image(testmatrix(i).o2) &
136                                 "/= " & integer'image(to_integer(signed(op3))) &
137                                 " -- erwartet: " & cinteger'image(testmatrix(i).expected);
138
139                         wait for 5 ns;
140                         -- ack it!
141                         do_calc <= '0';
142                 end loop;
143
144                 assert false
145                         report "alle testfaelle der ALU waren erfolgreich!";
146                 stop <= true;
147                 wait;
148         end process;
149 end architecture sim;