Add the first version of the exercise with test bench
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
library ieee;
|
||||
use ieee.std_logic_1164.all;
|
||||
|
||||
library std;
|
||||
use std.env.all;
|
||||
|
||||
library work;
|
||||
use work.test_utility.all;
|
||||
|
||||
entity tb_top_entity_float_add is
|
||||
generic( CHECK_RESULTS : boolean; GUI_MODE : boolean := true );
|
||||
end;
|
||||
|
||||
architecture test of tb_top_entity_float_add is
|
||||
signal clk : std_logic := '0';
|
||||
signal reset : std_logic := '1';
|
||||
signal run_calc : std_logic := '0';
|
||||
signal operand_a : std_logic_vector(31 downto 0) := ( others => '0' );
|
||||
signal operand_b : std_logic_vector(31 downto 0) := ( others => '0' );
|
||||
signal result : std_logic_vector(31 downto 0);
|
||||
signal calc_complete : std_logic;
|
||||
begin
|
||||
|
||||
u_top_entity_float_add : entity work.top_entity_float_add
|
||||
port map (
|
||||
clk => clk,
|
||||
reset => reset,
|
||||
run_calc => run_calc,
|
||||
operand_a => operand_a,
|
||||
operand_b => operand_b,
|
||||
result => result,
|
||||
calc_complete => calc_complete
|
||||
);
|
||||
|
||||
a_clk: clk <= not clk after 10 ns;
|
||||
|
||||
a_reset: process( clk )
|
||||
begin
|
||||
if falling_edge( clk ) then
|
||||
reset <= '0';
|
||||
end if;
|
||||
end process;
|
||||
|
||||
delay : process
|
||||
variable res : std_logic_vector( 31 downto 0 );
|
||||
variable expected : std_logic_vector( 31 downto 0 );
|
||||
begin
|
||||
|
||||
wait until falling_edge( reset );
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
wait until rising_edge( clk );
|
||||
run_calc <= '1';
|
||||
operand_a <= x"01000000";
|
||||
operand_b <= x"02000000";
|
||||
wait until rising_edge( calc_complete );
|
||||
run_calc <= '0';
|
||||
if ( CHECK_RESULTS ) then
|
||||
res := result;
|
||||
expected := x"02200000";
|
||||
assert_eq( res, expected );
|
||||
end if;
|
||||
wait until rising_edge( clk );
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
wait until rising_edge( clk );
|
||||
run_calc <= '1';
|
||||
operand_a <= x"4048f5c3"; -- 3.14
|
||||
operand_b <= x"402d70a4"; -- 2.71
|
||||
wait until rising_edge( calc_complete );
|
||||
if ( CHECK_RESULTS ) then
|
||||
res := result;
|
||||
expected := x"40bb3333"; -- 5.85
|
||||
assert_eq( res, expected );
|
||||
end if;
|
||||
wait until rising_edge( clk );
|
||||
|
||||
if GUI_MODE = true then
|
||||
finish;
|
||||
else
|
||||
stop;
|
||||
end if;
|
||||
end process delay;
|
||||
|
||||
end architecture test;
|
||||
@@ -0,0 +1,71 @@
|
||||
library ieee;
|
||||
use ieee.std_logic_1164.all;
|
||||
use ieee.numeric_std.all;
|
||||
use ieee.float_pkg.all;
|
||||
|
||||
library std;
|
||||
use std.textio.all;
|
||||
|
||||
package test_utility is
|
||||
constant TEST_FAIL : string := "[ FAIL ]";
|
||||
constant TEST_OK : string := "[ OK ]" & LF;
|
||||
|
||||
type real_array is array ( natural range <> ) of real;
|
||||
|
||||
procedure assert_eq( variable a : in std_logic_vector; variable b : in std_logic_vector );
|
||||
|
||||
procedure assert_near( variable a : in real;
|
||||
variable b : in real;
|
||||
variable abs_err : in real );
|
||||
|
||||
procedure assert_element_near( variable a : in real;
|
||||
variable b : in real;
|
||||
variable abs_err : in real;
|
||||
variable index : in integer );
|
||||
|
||||
end package test_utility;
|
||||
|
||||
package body test_utility is
|
||||
|
||||
procedure assert_eq( variable a : in std_logic_vector; variable b : in std_logic_vector ) is
|
||||
begin
|
||||
|
||||
assert( a = b )
|
||||
report TEST_FAIL & "assert_eq" & LF &
|
||||
" a: " & to_string( a ) & LF &
|
||||
" b: " & to_string( b ) & LF
|
||||
severity error;
|
||||
end procedure assert_eq;
|
||||
|
||||
procedure assert_near( variable a : in real;
|
||||
variable b : in real;
|
||||
variable abs_err : in real ) is
|
||||
variable abs_diff : real;
|
||||
begin
|
||||
abs_diff := abs( a - b );
|
||||
assert( abs_diff <= abs_err )
|
||||
report TEST_FAIL & "assert_near" & LF &
|
||||
" a: " & to_string( a ) & LF &
|
||||
" b: " & to_string( b ) & LF &
|
||||
" " & to_string( abs_diff ) & " > " & to_string( abs_err ) & LF
|
||||
severity error;
|
||||
end procedure assert_near;
|
||||
|
||||
procedure assert_element_near( variable a : in real;
|
||||
variable b : in real;
|
||||
variable abs_err : in real;
|
||||
variable index : in integer ) is
|
||||
variable abs_diff : real;
|
||||
begin
|
||||
abs_diff := abs( a - b );
|
||||
assert( abs_diff <= abs_err )
|
||||
report TEST_FAIL & "assert_element_near" & LF &
|
||||
" element: " & integer'image( index ) & LF &
|
||||
" a: " & to_string( a ) & LF &
|
||||
" b: " & to_string( b ) & LF &
|
||||
" " & to_string( abs_diff ) & " > " & to_string( abs_err ) & LF
|
||||
severity error;
|
||||
end procedure assert_element_near;
|
||||
|
||||
end package body test_utility;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
onerror {resume}
|
||||
quietly WaveActivateNextPane {} 0
|
||||
add wave -noupdate /tb_top_entity_float_add/clk
|
||||
add wave -noupdate /tb_top_entity_float_add/reset
|
||||
add wave -noupdate /tb_top_entity_float_add/run_calc
|
||||
add wave -noupdate /tb_top_entity_float_add/operand_a
|
||||
add wave -noupdate /tb_top_entity_float_add/operand_b
|
||||
add wave -noupdate /tb_top_entity_float_add/result
|
||||
add wave -noupdate /tb_top_entity_float_add/calc_complete
|
||||
TreeUpdate [SetDefaultTree]
|
||||
WaveRestoreCursors {{Cursor 1} {0 ns} 0}
|
||||
quietly wave cursor active 1
|
||||
configure wave -namecolwidth 150
|
||||
configure wave -valuecolwidth 100
|
||||
configure wave -justifyvalue left
|
||||
configure wave -signalnamewidth 0
|
||||
configure wave -snapdistance 10
|
||||
configure wave -datasetprefix 0
|
||||
configure wave -rowmargin 4
|
||||
configure wave -childrowmargin 2
|
||||
configure wave -gridoffset 0
|
||||
configure wave -gridperiod 1
|
||||
configure wave -griddelta 40
|
||||
configure wave -timeline 0
|
||||
configure wave -timelineunits ns
|
||||
update
|
||||
WaveRestoreZoom {242 ns} {377 ns}
|
||||
Reference in New Issue
Block a user