SMB-87 - Encoder Multiplier block - WIP (2): Basic behaviour looks good. Need to add velocity estimation block

This commit is contained in:
gianmarcogirau 2019-03-08 17:32:20 +00:00
commit 0e8151f3e1
5 changed files with 606 additions and 469 deletions

View file

@ -35,9 +35,9 @@ vcom -2008 -work $WORKLIB $COMMON_PATH/test/log_pkg.vhd
vcom -2008 -work $WORKLIB $COMMON_PATH//test/avalon_verify_pkg.vhd
# Compile the testbench
#vcom -2008 -work $WORKLIB ./../src/test/encoder_mulyiply_tb.vhd
vcom -2008 -work $WORKLIB ./../src/test/encoder_multiply_tb.vhd
#vsim -t ps work.encoder_multiply_tb
vsim -t ps work.encoder_multiply_tb
#add wave /encoder_multiply_tb/*
@ -47,9 +47,9 @@ vcom -2008 -work $WORKLIB $COMMON_PATH//test/avalon_verify_pkg.vhd
set StdArithNoWarnings 1
set NumericStdNoWarnings 1
#do wave_enc_mult.do
do wave_enc_mult.do
#when -label end_of_simulation {test_completed == true} {echo "Simulation Completed"; stop ;}
#run -all
when -label end_of_simulation {test_completed == true} {echo "Simulation Completed"; stop ;}
run -all
#run 100 us;
#run 3.0 ms;

View file

@ -0,0 +1,74 @@
onerror {resume}
quietly WaveActivateNextPane {} 0
add wave -noupdate /encoder_multiply_tb/reset_n
add wave -noupdate /encoder_multiply_tb/clk
add wave -noupdate /encoder_multiply_tb/init
add wave -noupdate /encoder_multiply_tb/enable
add wave -noupdate /encoder_multiply_tb/mult_sel
add wave -noupdate -radix hexadecimal /encoder_multiply_tb/delta_t
add wave -noupdate /encoder_multiply_tb/step_pos_in
add wave -noupdate /encoder_multiply_tb/step_neg_in
add wave -noupdate /encoder_multiply_tb/step_pos_out
add wave -noupdate /encoder_multiply_tb/step_neg_out
add wave -noupdate -radix decimal /encoder_multiply_tb/abs_pos
add wave -noupdate -radix decimal /encoder_multiply_tb/m_abs_pos
add wave -noupdate /encoder_multiply_tb/m_abs_pos_r
add wave -noupdate /encoder_multiply_tb/setup
add wave -noupdate /encoder_multiply_tb/running
add wave -noupdate /encoder_multiply_tb/passed
add wave -noupdate /encoder_multiply_tb/failed
add wave -noupdate /encoder_multiply_tb/test_timeout
add wave -noupdate /encoder_multiply_tb/test_completed
add wave -noupdate /encoder_multiply_tb/error_flag
add wave -noupdate /encoder_multiply_tb/iUUT/reset_n
add wave -noupdate /encoder_multiply_tb/iUUT/clk
add wave -noupdate /encoder_multiply_tb/iUUT/init
add wave -noupdate /encoder_multiply_tb/iUUT/enable
add wave -noupdate /encoder_multiply_tb/iUUT/mult_sel
add wave -noupdate -radix decimal /encoder_multiply_tb/iUUT/delta_t
add wave -noupdate /encoder_multiply_tb/iUUT/step_pos_in
add wave -noupdate /encoder_multiply_tb/iUUT/step_neg_in
add wave -noupdate /encoder_multiply_tb/iUUT/step_pos_out
add wave -noupdate /encoder_multiply_tb/iUUT/step_neg_out
add wave -noupdate /encoder_multiply_tb/iUUT/init_d
add wave -noupdate /encoder_multiply_tb/iUUT/step_pos
add wave -noupdate /encoder_multiply_tb/iUUT/step_neg
add wave -noupdate /encoder_multiply_tb/iUUT/m_step_pos
add wave -noupdate /encoder_multiply_tb/iUUT/m_step_neg
add wave -noupdate /encoder_multiply_tb/iUUT/kickoff
add wave -noupdate /encoder_multiply_tb/iUUT/pending_kickoff
add wave -noupdate /encoder_multiply_tb/iUUT/sign
add wave -noupdate /encoder_multiply_tb/iUUT/sign_i
add wave -noupdate /encoder_multiply_tb/iUUT/time_cnt_tc
add wave -noupdate /encoder_multiply_tb/iUUT/norm_pos
add wave -noupdate /encoder_multiply_tb/iUUT/norm_pos_d
add wave -noupdate /encoder_multiply_tb/iUUT/norm_neg
add wave -noupdate /encoder_multiply_tb/iUUT/norm_neg_d
add wave -noupdate -radix unsigned /encoder_multiply_tb/iUUT/mult_val
add wave -noupdate -radix unsigned /encoder_multiply_tb/iUUT/kmax
add wave -noupdate -radix decimal /encoder_multiply_tb/iUUT/pulse_cnt
add wave -noupdate -radix decimal /encoder_multiply_tb/iUUT/m_pulse_cnt
add wave -noupdate -radix unsigned /encoder_multiply_tb/iUUT/m_norm_value
add wave -noupdate -radix decimal /encoder_multiply_tb/iUUT/delta_m_pos
add wave -noupdate -radix decimal /encoder_multiply_tb/iUUT/delta_m_neg
add wave -noupdate /encoder_multiply_tb/iUUT/time_cnt
add wave -noupdate -radix unsigned /encoder_multiply_tb/iUUT/m_delta_t
add wave -noupdate -radix unsigned /encoder_multiply_tb/iUUT/delta_t_i
TreeUpdate [SetDefaultTree]
WaveRestoreCursors {{Cursor 1} {1010975000 ps} 0}
quietly wave cursor active 1
configure wave -namecolwidth 260
configure wave -valuecolwidth 126
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 ps
update
WaveRestoreZoom {1181469471 ps} {1245234239 ps}

View file

@ -6,7 +6,7 @@
-- Author : Gianmarco Girau
-- Company : Global Inkjet Systems
-- Created : 2019-02-27
-- Last update: 2019-03-04
-- Last update: 2019-03-08
-- SVN ID : Git
-- Standard : VHDL'2008
-------------------------------------------------------------------------------
@ -39,7 +39,7 @@ entity encoder_multiply is
init : in std_logic;
enable : in std_logic;
mult_sel : in std_logic_vector( 2 downto 0);
delta_t : in std_logic_vector(47 downto 0);
delta_t : in std_logic_vector(39 downto 0);
step_pos_in : in std_logic;
step_neg_in : in std_logic;
step_pos_out : out std_logic;
@ -66,23 +66,24 @@ architecture rtl of encoder_multiply is
-- To prevent counter saturation, subtract a fixed amount each time the
-- counters exceed a given value
constant NORM_VALUE : natural := 2**(PULSE_CNT_W-2);
constant NORM_VALUE : natural := 2**(PULSE_CNT_W-3);
signal init_d, step_pos, step_neg : std_logic;
signal m_step_pos, m_step_neg : std_logic;
signal sign, sign_i : std_logic;
signal time_cnt_zero : std_logic;
signal time_cnt_tc : std_logic;
signal norm_pos, norm_pos_d : std_logic;
signal norm_neg, norm_neg_d : std_logic;
signal kickoff, pending_kickoff : std_logic;
signal mult_val, kmax : std_logic_vector(MULT_W-1 downto 0);
signal pulse_cnt : std_logic_vector(PULSE_CNT_W-1 downto 0);
signal m_pulse_cnt, m_norm_value : std_logic_vector(M_PULSE_CNT_W-1 downto 0);
signal delta_m_pos, delta_m_neg : std_logic_vector(M_PULSE_CNT_W-1 downto 0);
signal time_cnt : std_logic_vector(27 downto 0);
signal m_delta_t : std_logic_vector(38-MULT_W downto 0);
signal delta_t_i : std_logic_vector(38 downto 0);
signal m_delta_t : std_logic_vector(30 downto 0);
signal delta_t_i : std_logic_vector(30 downto 0);
begin
@ -102,7 +103,13 @@ begin
begin
if(clk = '1' and clk'event) then
init_d <= init;
kmax <= std_logic_vector(unsigned(mult_val)-1);
--kmax <= std_logic_vector(unsigned(mult_val)-1);
kmax <= (others => '0');
if(init = '1') then
pending_kickoff <= '1';
elsif(kickoff = '1') then
pending_kickoff <= '0';
end if;
if(init_d = '1') then
pulse_cnt <= (others => '0');
elsif(step_pos = '1') then
@ -127,27 +134,17 @@ begin
end if;
if(m_step_pos = '1' or m_step_neg = '1') then
time_cnt <= m_delta_t(27 downto 0);
elsif(time_cnt_zero = '0') then
elsif(time_cnt_tc = '0') then
time_cnt <= std_logic_vector(unsigned(time_cnt)-1);
end if;
m_delta_t <= delta_t_i srl to_integer(unsigned(mult_sel));
sign <= sign_i;
mult_pulse_cnt := pulse_cnt sll to_integer(unsigned(mult_sel));
mult_pulse_cnt := std_logic_vector(resize(signed(pulse_cnt), M_PULSE_CNT_W)) sll to_integer(signed('0' & mult_sel));
delta_m_pos <= std_logic_vector(signed(mult_pulse_cnt) + signed(kmax));
delta_m_neg <= std_logic_vector(signed(mult_pulse_cnt) - signed(kmax));
delta_m_neg <= std_logic_vector(signed(mult_pulse_cnt) - signed(kmax));
step_pos_out <= m_step_pos;
step_neg_out <= m_step_neg;
if(signed(pulse_cnt) = NORM_VALUE and norm_pos_d = '0') then
norm_pos <= '1';
else
norm_pos <= '0';
end if;
norm_pos_d <= norm_pos;
if(signed(pulse_cnt) = -NORM_VALUE and norm_neg_d = '0') then
norm_neg <= '1';
else
norm_neg <= '0';
end if;
norm_neg_d <= norm_neg;
m_norm_value <= std_logic_vector(to_unsigned(NORM_VALUE, M_PULSE_CNT_W)) sll (to_integer(unsigned(mult_sel)));
end if;
@ -156,12 +153,17 @@ begin
step_pos <= step_pos_in and enable;
step_neg <= step_neg_in and enable;
delta_t_i <= delta_t(46 downto 8);
sign_i <= delta_t(47);
delta_t_i <= delta_t(38 downto 8);
sign_i <= delta_t(39);
time_cnt_zero <= '1' when unsigned(time_cnt) = 0 else '0';
time_cnt_tc <= '1' when unsigned(time_cnt) = 1 else '0';
m_step_pos <= '1' when sign = '0' and time_cnt_zero = '1' and signed(m_pulse_cnt) < signed(delta_m_pos) else '0';
m_step_neg <= '1' when sign = '1' and time_cnt_zero = '1' and signed(m_pulse_cnt) > signed(delta_m_pos) else '0';
m_step_pos <= '1' when sign = '0' and (kickoff = '1' or (time_cnt_tc = '1' and signed(m_pulse_cnt) < signed(delta_m_pos))) else '0';
m_step_neg <= '1' when sign = '1' and (kickoff = '1' or (time_cnt_tc = '1' and signed(m_pulse_cnt) > signed(delta_m_pos))) else '0';
kickoff <= pending_kickoff and (step_pos or step_neg);
norm_pos <= '1' when signed(pulse_cnt) = 2*NORM_VALUE and norm_pos_d = '0' else '0';
norm_neg <= '1' when signed(pulse_cnt) = -2*NORM_VALUE and norm_neg_d = '0' else '0';
end architecture rtl;

View file

@ -1,438 +0,0 @@
-------------------------------------------------------------------------------
-- Title : encoder_multiply_tb
-- Project : EMB/SMB
-------------------------------------------------------------------------------
-- File : encoder_multiply_tb.vhd
-- Author : GMG
-- Company : Global Inkjet Systems
-- Created : 2019-03-05
-- Last update: 2019-03-05
-- SVN ID : Git
-- Standard : VHDL'2008
-------------------------------------------------------------------------------
-- Description:
-- Testbench for the encoder_multiply block
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;
library OSVVM;
use OSVVM.RandomBasePkg.all;
use OSVVM.RandomPkg.all;
use work.log_pkg.all;
entity encoder_multiply_tb is
-- ..
end entity encoder_multiply_tb;
architecture behav of encoder_multiply_tb is
constant CLK_PERIOD : time := 10 ns;
-- Logging Verbosity Level
constant VERBOSITY_LEVEL : verb_level_t := VMEDIUM;
constant MAX_DIST : natural := 10000;
-- define the percentage of reverse steps, with respect to the intended
-- direction, while advancing the position:
-- 0 => no reverse movement at all
-- 20 => 20% of position increments are in the opposite dirtection
-- 49 => Essentially this is an oscillation around the centre position with a
-- tiny bias in the intended direction!
constant POS_RANDOM_PERC : natural range 0 to 49 := 0; -- define
-----------------------------------------------------------------------------
-- TB Functions / Procedures
-----------------------------------------------------------------------------
procedure wait_latency(
signal clk : in std_logic;
cycles : in natural) is
begin
for i in 1 to cycles loop
wait until clk = '1' and clk'event;
end loop; -- i
end procedure wait_latency;
-----------------------------------------------------------------------------
-- Signal declarations
-----------------------------------------------------------------------------
--
signal test_completed : boolean;
signal configuring : boolean;
signal running : boolean;
signal passed : boolean;
signal failed : boolean;
begin -- architecture behav
clk <= not clk after 0.5*CLK_PERIOD;
-----------------------------------------------------------------------------
-- UUTs
-----------------------------------------------------------------------------
iUUT: entity work.encoder_multiply
port map (
reset_n => reset_n,
clk => clk,
init => init,
enable => enable,
mult_sel => mult_sel,
delta_t => delta_t,
step_pos_in => step_pos_in,
step_neg_in => step_neg_in,
step_pos_out => step_pos_out,
step_neg_out => step_neg_out);
-----------------------------------------------------------------------------
-- Support Logic
-----------------------------------------------------------------------------
pGEN_POS : process(clk)
begin
if(clk = '1' and clk'event) then
if(init = '1') then
abs_pos <= (others => '0');
elsif(step_pos_in = '1') then
abs_pos <= std_logic_vector(unsigned(abs_pos)+1);
elsif(step_neg_in = '1') then
abs_pos <= std_logic_vector(unsigned(abs_pos)-1);
end if;
if(init = '1') then
m_abs_pos <= (others => '0');
elsif(step_pos_out = '1') then
m_abs_pos <= std_logic_vector(unsigned(m_abs_pos)+1);
elsif(step_neg_out = '1') then
m_abs_pos <= std_logic_vector(unsigned(m_abs_pos)-1);
end if;
end if;
end process pGEN_POS;
-----------------------------------------------------------------------------
-- Testbench Logic
-----------------------------------------------------------------------------
pTB_CTRL : process
variable delta_pos : integer := 0;
variable init_pos : integer;
variable dir_m : integer;
variable tot_steps : integer;
variable result : boolean;
variable test_idx : natural;
variable rv : RandomPType;
procedure run_test is
variable steps : integer;
variable sel_dir : integer;
begin
log_header("Test-case " & to_string(test_idx), VMEDIUM);
wait_latency(clk, 1);
if dir = '0' then
dir_m := 1;
else
dir_m := -1;
end if;
init_pos := pos;
-- reset the absolute position memory
enable <= '0';
upd_max_pos <= '0';
upd_min_pos <= '0';
wait_latency(clk, 1);
upd_max_pos <= '1';
upd_min_pos <= '1';
wait_latency(clk, 1);
upd_max_pos <= '0';
upd_min_pos <= '0';
-- restart the divider
wait_latency(clk, 1);
enable <= '1';
wait_latency(clk, INTRA_POS_LATENCY);
-- run the steps
steps := 0;
loop_pos: while steps < tot_steps loop
wait_latency(clk, INTRA_POS_LATENCY);
/*
-- check the result and report
delta_pos := dir_m * (pos - init_pos);
result := check_result(div_ratio, pixel_offset, delta_pos, tot_fire_num, true);
exit loop_pos when result = false;
*/
sel_dir := rv.RandInt(1, 100);
if(sel_dir > POS_RANDOM_PERC) then
-- move in the intended direction
pos <= pos + dir_m;
steps := steps + 1;
else
-- move in the opposite direction
pos <= pos - dir_m;
steps := steps - 1;
end if;
end loop loop_pos;
-- wait the propagation of the latency
wait_latency(clk, INTRA_POS_LATENCY);
-- check the result and report
delta_pos := dir_m * (pos - init_pos);
if(result /= false) then
result := check_result(div_ratio, pixel_offset, delta_pos, tot_fire_num);
end if;
log("div_ratio = " & to_string(div_ratio), VMEDIUM);
end procedure run_test;
begin
-- Set the verbosity level for logging
set_log_verbosity(VERBOSITY_LEVEL);
--set_log_verbosity(VHIGH);
log_header("Fractional Divider verification:", VLOW);
test_completed <= false;
configuring <= false;
running <= false;
passed <= false;
failed <= false;
enable <= '0';
dir <= '1';
-- Reset
reset_n <= '0';
wait for 100 ns;
wait until clk = '1' and clk'event;
reset_n <= '1';
result := true;
test_idx := 0;
---------------------------------------------------------------------------
-- Test-00
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 0.0;
dir <= '0';
tot_steps := 100000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-01
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 1.001;
pixel_offset <= 0.0;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-02
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 1.002;
pixel_offset <= 0.9;
dir <= '1';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-03
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 1.333;
pixel_offset <= 1.5;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-04
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.008;
pixel_offset <= 0.25;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-05
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 0.0;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-06
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 0.25;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-07
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 0.5;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-08
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 0.75;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-09
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 1.0;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-10
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 0.12;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-11
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 0.13;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-12
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 126.0;
pixel_offset <= 0.99;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-13
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 126.0;
pixel_offset <= 0.0;
dir <= '0';
tot_steps := 10000;
run_test;
end if;
---------------------------------------------------------------------------
-- Test-14 - Very long test to check internal-reg-saturation
---------------------------------------------------------------------------
if(result /= false) then
test_idx := test_idx+1;
div_ratio <= 4.000;
pixel_offset <= 0.0;
dir <= '0';
tot_steps := 100000;
run_test;
end if;
---------------------------------------------------------------------------
-- Random Tests
---------------------------------------------------------------------------
for n in 1 to 20 loop
if(result /= false) then
test_idx := test_idx+1;
-- the div_ratio fractional part must be a multiple of 0.001
div_ratio <= 0.001*real(rv.RandInt(1000, 50000));
if((rv.RandInt(1,10) mod 2) = 0) then
dir <= '0';
else
dir <= '1';
end if;
tot_steps := rv.RandInt(5000, 20000);
run_test;
end if;
end loop; -- n
wait for 200 ns;
log_separator_line(VLOW);
if(result = false) then
failed <= true;
log("FAILURE: test completed with errors.", VLOW);
else
passed <= true;
log("SUCCESS: test successfully completed.", VLOW);
end if;
log_separator_line(VLOW);
test_completed <= true;
running <= false;
wait;
end process pTB_CTRL;
end architecture behav;

View file

@ -0,0 +1,499 @@
-------------------------------------------------------------------------------
-- Title : encoder_multiply_tb
-- Project : EMB/SMB
-------------------------------------------------------------------------------
-- File : encoder_multiply_tb.vhd
-- Author : GMG
-- Company : Global Inkjet Systems
-- Created : 2019-03-05
-- Last update: 2019-03-08
-- SVN ID : Git
-- Standard : VHDL'2008
-------------------------------------------------------------------------------
-- Description:
-- Testbench for the encoder_multiply block
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;
library OSVVM;
use OSVVM.RandomBasePkg.all;
use OSVVM.RandomPkg.all;
use work.log_pkg.all;
entity encoder_multiply_tb is
-- ..
end entity encoder_multiply_tb;
architecture behav of encoder_multiply_tb is
constant CLK_PERIOD : time := 10 ns;
-- Logging Verbosity Level
constant VERBOSITY_LEVEL : verb_level_t := VMEDIUM;
constant MAX_SIM_TIME : time := 30.0 ms; -- Maximum simulation time
constant MAX_DIST : natural := 1000;
-- delta_tmin : minimum value of delta_t, corresponding to the maximum tx
-- rate over the POF (4M events/sec => 250 ns)
constant DELTA_TMIN : natural := 25;
-- define the percentage of reverse steps, with respect to the intended
-- direction, while advancing the position:
-- 0 => no reverse movement at all
-- 20 => 20% of position increments are in the opposite dirtection
-- 49 => Essentially this is an oscillation around the centre position with a
-- tiny bias in the intended direction!
constant POS_RANDOM_PERC : natural range 0 to 49 := 0; -- define
-----------------------------------------------------------------------------
-- Signal declarations
-----------------------------------------------------------------------------
signal reset_n : std_logic;
signal clk : std_logic := '0';
signal init : std_logic;
signal enable : std_logic;
signal mult_sel : std_logic_vector(2 downto 0);
signal delta_t : std_logic_vector(39 downto 0);
signal step_pos_in : std_logic;
signal step_neg_in : std_logic;
signal step_pos_out : std_logic;
signal step_neg_out : std_logic;
--
signal abs_pos : std_logic_vector(31 downto 0);
signal m_abs_pos : std_logic_vector(39 downto 0);
signal m_abs_pos_r : real;
-- TB specific signals
signal setup : boolean;
signal running : boolean;
signal passed : boolean := false;
signal failed : boolean := false;
signal test_timeout : boolean := false;
signal test_completed : boolean := false;
signal error_flag : boolean := false;
-----------------------------------------------------------------------------
-- Subprograms
-----------------------------------------------------------------------------
procedure wait_clk_cycles(cycles : in natural) is
begin
for n in 0 to cycles-1 loop
wait until clk = '1' and clk'event;
end loop; -- n
end procedure wait_clk_cycles;
-----------------------------------------------------------------------------
-- Encoder Profile #1: fixed frequency
-----------------------------------------------------------------------------
procedure run_encoder_profile_1 (
freq : in real range 1.0e3 to 4000.0e3;
dir : in std_logic;
max_steps : in natural;
signal enable : out std_logic;
signal delta_t : out std_logic_vector(39 downto 0);
signal step_pos : out std_logic;
signal step_neg : out std_logic;
init : in boolean := false) is
variable step_cnt : natural := 0;
variable t_ref : time;
variable t_period : real;
begin
enable <= '0';
step_pos <= '0';
step_neg <= '0';
log("run_encoder_profile_1: Running " & to_string(max_steps) & " steps in direction " & to_string(dir) & ", at " & to_string(freq/1000.0) & " kHz ...", VMEDIUM);
-- normalize to us
t_period := 1.0e6/freq;
wait_clk_cycles(5);
-- set the initial velocity estimation
if(init = true) then
delta_t(delta_t'left-1 downto 0) <= std_logic_vector(to_unsigned(DELTA_TMIN*256, 39));
else
delta_t(delta_t'left-1 downto 0) <= std_logic_vector(to_unsigned(integer(t_period*100.0*256.0), delta_t'left));
end if;
delta_t(delta_t'left) <= dir;
wait_clk_cycles(1);
-- enable the multiplier
enable <= '1';
wait_clk_cycles(5);
-- issue the first step
t_ref := now;
if(dir = '0') then
step_pos <= '1';
else
step_neg <= '1';
end if;
step_cnt := step_cnt + 1;
wait_clk_cycles(1);
step_pos <= '0';
step_neg <= '0';
while step_cnt < max_steps loop
if(now-t_ref > (1.0 us)*(real(step_cnt)*t_period)) then
wait until clk = '1' and clk'event;
-- issue the next step
delta_t(delta_t'left-1 downto 0) <= std_logic_vector(to_unsigned(integer(t_period*100.0*256.0), delta_t'left));
delta_t(delta_t'left) <= dir;
if(dir = '0') then
step_pos <= '1';
else
step_neg <= '1';
end if;
step_cnt := step_cnt + 1;
wait_clk_cycles(1);
step_pos <= '0';
step_neg <= '0';
end if;
wait for 0.1*CLK_PERIOD;
end loop;
wait_clk_cycles(0);
-- disable the multiplier
enable <= '0';
end procedure run_encoder_profile_1;
-----------------------------------------------------------------------------
-- Encoder Profile #2: average frequency + noise
-----------------------------------------------------------------------------
procedure run_encoder_profile_2 (
freq : in real range 1.0e3 to 4000.0e3;
dir : in std_logic;
max_steps : in natural;
period_noise : in natural range 0 to 100;
signal enable : out std_logic;
signal delta_t : out std_logic_vector(39 downto 0);
signal step_pos : out std_logic;
signal step_neg : out std_logic;
init : in boolean := false) is
variable step_cnt : natural := 0;
variable t_ref : time;
variable t_period : real;
variable rv : RandomPType;
variable delta_period : integer;
begin
enable <= '0';
step_pos <= '0';
step_neg <= '0';
log("run_encoder_profile_2: Running " & to_string(max_steps) & " steps in direction " & to_string(dir) & ", at " & to_string(freq/1000.0) & " kHz ...", VMEDIUM);
-- normalize to us
t_period := 1.0e6/freq;
wait_clk_cycles(5);
-- set the initial velocity estimation
if(init = true) then
delta_t(delta_t'left-1 downto 0) <= std_logic_vector(to_unsigned(DELTA_TMIN*256, 39));
else
delta_t(delta_t'left-1 downto 0) <= std_logic_vector(to_unsigned(integer(t_period*100.0*256.0), delta_t'left));
end if;
delta_t(delta_t'left) <= dir;
wait_clk_cycles(1);
-- enable the multiplier
enable <= '1';
wait_clk_cycles(5);
-- issue the first step
t_ref := now;
if(dir = '0') then
step_pos <= '1';
else
step_neg <= '1';
end if;
step_cnt := step_cnt + 1;
wait_clk_cycles(1);
step_pos <= '0';
step_neg <= '0';
delta_period := 0;
while step_cnt < max_steps loop
if(now-t_ref > (1.0 us)*((real(step_cnt-1)+((100.0+real(delta_period))/100.0))*t_period)) then
wait until clk = '1' and clk'event;
-- issue the next step
delta_t(delta_t'left-1 downto 0) <= std_logic_vector(to_unsigned(integer(t_period*100.0*256.0), delta_t'left));
delta_t(delta_t'left) <= dir;
if(dir = '0') then
step_pos <= '1';
else
step_neg <= '1';
end if;
step_cnt := step_cnt + 1;
wait_clk_cycles(1);
step_pos <= '0';
step_neg <= '0';
delta_period := rv.RandInt(-period_noise, period_noise);
end if;
wait for 0.1*CLK_PERIOD;
end loop;
wait_clk_cycles(0);
-- disable the multiplier
enable <= '0';
end procedure run_encoder_profile_2;
begin -- architecture behav
clk <= not clk after 0.5*CLK_PERIOD;
-----------------------------------------------------------------------------
-- UUTs
-----------------------------------------------------------------------------
iUUT: entity work.encoder_multiply
port map (
reset_n => reset_n,
clk => clk,
init => init,
enable => enable,
mult_sel => mult_sel,
delta_t => delta_t,
step_pos_in => step_pos_in,
step_neg_in => step_neg_in,
step_pos_out => step_pos_out,
step_neg_out => step_neg_out);
-----------------------------------------------------------------------------
-- Support Logic
-----------------------------------------------------------------------------
pGEN_POS : process(clk)
variable mult_value : integer;
begin
if(clk = '1' and clk'event) then
if(init = '1') then
abs_pos <= (others => '0');
elsif(step_pos_in = '1') then
abs_pos <= std_logic_vector(unsigned(abs_pos)+1);
elsif(step_neg_in = '1') then
abs_pos <= std_logic_vector(unsigned(abs_pos)-1);
end if;
if(init = '1') then
m_abs_pos <= (others => '0');
elsif(step_pos_out = '1') then
m_abs_pos <= std_logic_vector(unsigned(m_abs_pos)+1);
elsif(step_neg_out = '1') then
m_abs_pos <= std_logic_vector(unsigned(m_abs_pos)-1);
end if;
--mult_value := 2**to_integer(unsigned(mult_sel));
mult_value := to_integer(unsigned(std_logic_vector(to_unsigned(1, 8)) sll(to_integer(unsigned(mult_sel)))));
m_abs_pos_r <= real(to_integer(signed(m_abs_pos)))/real(mult_value);
end if;
end process pGEN_POS;
-----------------------------------------------------------------------------
-- Testbench Logic
-----------------------------------------------------------------------------
pTEST_PROC : process
variable period_variance : natural := 20;
begin
---------------------------------------------------------------------------
-- Initialize & Configure
---------------------------------------------------------------------------
reset_n <= '0';
init <= '0';
enable <= '0';
mult_sel <= (others => '0');
delta_t <= (others => '0');
step_pos_in <= '0';
step_neg_in <= '0';
setup <= false;
running <= false;
--Set the verbosity level for logging
set_log_verbosity(VERBOSITY_LEVEL);
--
wait for 100 ns;
reset_n <= '1';
wait for 100 ns;
wait until clk = '1' and clk'event;
-- Nothing really to do here
setup <= true;
---------------------------------------------------------------------------
-- Running
---------------------------------------------------------------------------
running <= true;
log_separator_line(VLOW);
log("Encoder-Multiply verification:", VLOW);
log_separator_line(VLOW);
wait_clk_cycles(10);
wait until clk = '1' and clk'event;
mult_sel <= "011";
-- assert init to provide a clean configuration
init <= '1';
wait until clk = '1' and clk'event;
init <= '0';
log("Encoder-Multiplication set to 8", VLOW);
wait_clk_cycles(10);
/*
log_separator_line(VLOW);
log("Tests at constant frequency:", VLOW);
log_separator_line(VLOW);
run_encoder_profile_1(100.0e3, '0', 50, enable, delta_t, step_pos_in, step_neg_in, true);
wait for 50 us;
run_encoder_profile_1(100.0e3, '0', 25, enable, delta_t, step_pos_in, step_neg_in);
wait for 50 us;
run_encoder_profile_1(100.0e3, '0', 25, enable, delta_t, step_pos_in, step_neg_in);
wait for 50 us;
run_encoder_profile_1(100.0e3, '1', 25, enable, delta_t, step_pos_in, step_neg_in);
wait for 50 us;
run_encoder_profile_1(100.0e3, '0', 1, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '0', 1, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '0', 1, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '1', 1, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '0', 1, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '1', 1, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '0', 1, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '1', 3, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '0', 125, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '1', 250, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '0', 150, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '0', 500, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_1(100.0e3, '1', 1000, enable, delta_t, step_pos_in, step_neg_in);
wait for 100 us;
*/
period_variance := 30;
log_separator_line(VLOW);
log("Tests at variable frequency (+/-" & to_string(period_variance) & "%):", VLOW);
log_separator_line(VLOW);
run_encoder_profile_2(100.0e3, '0', 50, period_variance, enable, delta_t, step_pos_in, step_neg_in, true);
wait for 50 us;
run_encoder_profile_2(100.0e3, '0', 25, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 50 us;
run_encoder_profile_2(100.0e3, '0', 25, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 50 us;
run_encoder_profile_2(100.0e3, '1', 25, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 50 us;
run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '1', 1, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '1', 1, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '1', 3, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '0', 125, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '1', 250, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '0', 150, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '0', 500, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 20 us;
run_encoder_profile_2(100.0e3, '1', 1000, period_variance, enable, delta_t, step_pos_in, step_neg_in);
wait for 100 us;
---------------------------------------------------------------------------
-- End
---------------------------------------------------------------------------
passed <= true after 1 us;
wait;
end process pTEST_PROC;
pERROR_DETECT : process(clk)
begin
if(enable = '1') then
--if((m_abs_pos_r > real(to_integer(signed(abs_pos)))+1.0) or (m_abs_pos_r < real(to_integer(signed(abs_pos)))-1.0)) then
if((m_abs_pos_r > real(to_integer(signed(abs_pos)))+2.0) or (m_abs_pos_r < real(to_integer(signed(abs_pos)))-2.0)) then
error_flag <= true;
end if;
end if;
end process pERROR_DETECT;
failed <= error_flag after 100 ns;
CHECK_SIM_TIMEOUT: process
begin -- process CHECK_SIM_TIMEOUT
while now < MAX_SIM_TIME loop
wait for 100 us;
end loop;
test_timeout <= true;
wait;
end process CHECK_SIM_TIMEOUT;
pREPORT_RESULT : process
begin
wait until failed = true or passed = true or test_timeout = true;
wait for 1 us;
log_separator_line(VLOW);
if(failed = true) then
log("FAILURE: test completed with errors.", VLOW);
elsif(passed = true) then
log("SUCCESS: test successfully completed.", VLOW);
elsif(test_timeout = true) then
log("FAILURE: test achieved max-sim-time without reaching ", VLOW);
log(" failed/passed condition.", VLOW);
end if;
log_separator_line(VLOW);
test_completed <= true;
end process pREPORT_RESULT;
end architecture behav;