diff --git a/generic_ip/common/0/src/dpram_sclk.vhd b/generic_ip/common/0/src/dpram_sclk.vhd new file mode 100644 index 0000000..0ae1912 --- /dev/null +++ b/generic_ip/common/0/src/dpram_sclk.vhd @@ -0,0 +1,74 @@ +------------------------------------------------------------------------------- +-- Title : dpram_sclk +-- Project : Dual-Port, single clock RAM +------------------------------------------------------------------------------- +-- File : dpram_sclk.vhd +-- Author : GMG +-- Company : Global Inkjet Systems +-- Created : 2018-04-24 +-- Last update: 2019-03-12 +-- SVN ID : Git +-- Standard : VHDL'2008 +------------------------------------------------------------------------------- +-- Description: +-- True Dual-Port, single clock RAM +------------------------------------------------------------------------------- + +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; + +use work.common_pkg.all; + +entity dpram_sclk is + generic( + WIDTH : natural := 32; + DEPTH : natural := 256); + port( + clk : in std_logic; + -- Port-A + addr_a : in std_logic_vector(log2(DEPTH)-1 downto 0); + we_a : in std_logic := '1'; + data_a : in std_logic_vector(WIDTH-1 downto 0); + q_a : out std_logic_vector(WIDTH-1 downto 0); + -- Port-B + addr_b : in std_logic_vector(log2(DEPTH)-1 downto 0); + we_b : in std_logic := '1'; + data_b : in std_logic_vector(WIDTH-1 downto 0); + q_b : out std_logic_vector(WIDTH-1 downto 0)); +end dpram_sclk; + +architecture rtl of dpram_sclk is + + -- Build a 2-D array type for the RAM + subtype word_t is std_logic_vector(WIDTH-1 downto 0); + type memory_t is array(DEPTH-1 downto 0) of word_t; + + -- Declare the RAM + shared variable ram : memory_t; + +begin + + -- Port A + pPORT_A : process(clk) + begin + if(clk = '1' and clk'event) then + if(we_a = '1') then + ram(to_integer(unsigned(addr_a))) := data_a; + end if; + q_a <= ram(to_integer(unsigned(addr_a))); + end if; + end process pPORT_A; + + -- Port B + pPORT_B : process(clk) + begin + if(clk = '1' and clk'event) then + if(we_b = '1') then + ram(to_integer(unsigned(addr_b))) := data_b; + end if; + q_b <= ram(to_integer(unsigned(addr_b))); + end if; + end process pPORT_B; + +end rtl; diff --git a/generic_ip/encoder_multiply/0/sim/sim_enc_mult.do b/generic_ip/encoder_multiply/0/sim/sim_enc_mult.do index 5e75254..215fd64 100644 --- a/generic_ip/encoder_multiply/0/sim/sim_enc_mult.do +++ b/generic_ip/encoder_multiply/0/sim/sim_enc_mult.do @@ -37,8 +37,6 @@ vcom -2008 -work $WORKLIB $COMMON_PATH//test/avalon_verify_pkg.vhd # Compile the testbench vcom -2008 -work $WORKLIB ./../src/test/encoder_multiply_tb.vhd -vsim -t ps work.encoder_multiply_tb - if { $argc == 0 } { puts "Running default generic!" vsim -t ps work.encoder_multiply_tb diff --git a/generic_ip/encoder_multiply/0/sim/sim_speed_detect.do b/generic_ip/encoder_multiply/0/sim/sim_speed_detect.do new file mode 100644 index 0000000..d4b0bbf --- /dev/null +++ b/generic_ip/encoder_multiply/0/sim/sim_speed_detect.do @@ -0,0 +1,65 @@ +# Simulation macro + +#.main clear + +# Cleanup libraries +foreach lib [list work OSVVM] { + file delete -force -- $lib + vlib $lib +} + +set OSVVM_PATH ../../../common/0/src/test/OSVVM +set COMMON_PATH ../../../common/0/src + +set WORKLIB work + +vlib ./work +vmap work ./work + +# Compile the UUT +vcom -2008 -work $WORKLIB $COMMON_PATH/common_pkg.vhd +vcom -2008 -work $WORKLIB $COMMON_PATH/dpram_sclk.vhd +vcom -2008 -work $WORKLIB ./../src/intel/divider.vhd +vcom -2008 -work $WORKLIB ./../src/encoder_speed_detect_0.vhd + +# Compile the constrained random related packages +vlib OSVVM +vcom -2008 -work OSVVM \ + $OSVVM_PATH/RandomBasePkg.vhd \ + $OSVVM_PATH/SortListPkg_int.vhd \ + $OSVVM_PATH/RandomPkg.vhd \ + $OSVVM_PATH/NamePkg.vhd \ + $OSVVM_PATH/MessagePkg.vhd \ + $OSVVM_PATH/CoveragePkg.vhd + +# Compile support files +vcom -2008 -work $WORKLIB $COMMON_PATH/test/common_test_pkg.vhd +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_speed_detect_tb.vhd + +if { $argc == 0 } { + puts "Running default generic!" + vsim -t ps work.encoder_speed_detect_tb +} else { + puts "Running custom generic ($1)" + vsim -t ps work.encoder_speed_detect_tb -GMULT_SELECTION=$1 +} + + +#add wave /encoder_speed_detect_tb/* +#add wave /encoder_speed_detect_tb/iUUT/* + +# remove warnings from standard numeric/arithmetic packages (speed-up simulation) +set StdArithNoWarnings 1 +set NumericStdNoWarnings 1 + +do wave_enc_speed_detect.do + +when -label end_of_simulation {test_completed == true} {echo "Simulation Completed"; stop ;} +run -all + +#run 3.0 ms; + diff --git a/generic_ip/encoder_multiply/0/src/encoder_multiply_0.vhd b/generic_ip/encoder_multiply/0/src/encoder_multiply_0.vhd index c60279e..c799b77 100644 --- a/generic_ip/encoder_multiply/0/src/encoder_multiply_0.vhd +++ b/generic_ip/encoder_multiply/0/src/encoder_multiply_0.vhd @@ -32,7 +32,7 @@ library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; -entity encoder_multiply is +entity encoder_multiply_0 is port( reset_n : in std_logic; clk : in std_logic; @@ -46,9 +46,9 @@ entity encoder_multiply is step_neg_in : in std_logic; step_pos_out : out std_logic; step_neg_out : out std_logic); -end encoder_multiply; +end encoder_multiply_0; -architecture rtl of encoder_multiply is +architecture rtl of encoder_multiply_0 is /* function compute_mult(mult_sel : std_logic_vector(2 downto 0)) return std_logic_vector(7 downto 0) is diff --git a/generic_ip/encoder_multiply/0/src/encoder_speed_detect_0.vhd b/generic_ip/encoder_multiply/0/src/encoder_speed_detect_0.vhd new file mode 100644 index 0000000..7755a55 --- /dev/null +++ b/generic_ip/encoder_multiply/0/src/encoder_speed_detect_0.vhd @@ -0,0 +1,215 @@ +------------------------------------------------------------------------------- +-- Title : encoder_speed_detect +-- Project : Generic +------------------------------------------------------------------------------- +-- File : encoder_speed_detect_0.vhd +-- Author : Gianmarco Girau +-- Company : Global Inkjet Systems +-- Created : 2019-02-27 +-- Last update: 2019-03-19 +-- SVN ID : Git +-- Standard : VHDL'2008 +------------------------------------------------------------------------------- +-- Description: +-- Encoder Speed Detection +------------------------------------------------------------------------------- + +------------------------------------------------------------------------------- +-- Port Map: +-- init : clears the internal state when '1' +-- enable : do not update the internal state when '0' +-- avg_steps : number of encoder steps for the averaging process +-- delta_init : initial value of delta_t (first step or after timoe-out) +-- time_out : time-out value +-- step_pos : encoder positive step +-- step_neg : encoder negative step +-- delta_t : estimated average time between encoder steps +-- time_out_flag : time-out flag +------------------------------------------------------------------------------- + +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; + +library work; +use work.common_pkg.all; + +entity encoder_speed_detect_0 is + port( + reset_n : in std_logic; + clk : in std_logic; + init : in std_logic; + enable : in std_logic; + avg_steps : in std_logic_vector( 7 downto 0); + delta_init : in std_logic_vector(27 downto 0); + time_out : in std_logic_vector(27 downto 0); + step_pos : in std_logic; + step_neg : in std_logic; + delta_t : out std_logic_vector(39 downto 0); + step_pos_out : out std_logic; + step_neg_out : out std_logic; + time_out_flag : out std_logic); +end encoder_speed_detect_0; + +architecture rtl of encoder_speed_detect_0 is + + signal step_type, step_pos_i, step_neg_i : std_logic; + signal inc_last, inc_first, avg_transient_on : std_logic; + signal init_i, time_out_flag_i, new_step : std_logic; + signal inc_step_balance, dec_step_balance : std_logic; + signal inc_step_cnt, write_last : std_logic; + signal first_step, time_out_flag_d : std_logic; + signal last_step_type : std_logic; + -- + signal data_valid : std_logic_vector( 5 downto 0); + signal step_cnt, first_addr, last_addr : std_logic_vector( 7 downto 0); + signal div_remain : std_logic_vector( 7 downto 0); + signal step_balance, step_bal_m1 : std_logic_vector( 9 downto 0); + signal loc_time, time_out_cnt : std_logic_vector(27 downto 0); + signal time_diff : std_logic_vector(29 downto 0); + signal time_diff_i, delta_t_i : std_logic_vector(30 downto 0); + signal new_tag, tag_last_i : std_logic_vector(31 downto 0); + signal tag_first, tag_last : std_logic_vector(31 downto 0); + +begin + + pSEQ_LOGIC_RST : process(reset_n, clk) + begin + if(reset_n = '0') then + data_valid <= (others => '0'); + step_pos_out <= '0'; + step_neg_out <= '0'; + elsif(clk = '1' and clk'event) then + data_valid <= data_valid(data_valid'left-1 downto 0) & new_step; + step_pos_out <= '0'; + step_neg_out <= '0'; + if(data_valid(data_valid'left) = '1') then + if(last_step_type = '0') then + step_pos_out <= '1'; + else + step_neg_out <= '1'; + end if; + end if; + end if; + end process pSEQ_LOGIC_RST; + + pSEQ_LOGIC : process(clk) + begin + if(clk = '1' and clk'event) then + step_pos_i <= step_pos and enable; + step_neg_i <= step_neg and enable; + if(init_i = '1') then + loc_time <= (others => '0'); + else + loc_time <= std_logic_vector(unsigned(loc_time)+1); + end if; + if(init_i = '1') then + step_cnt <= (others => '0'); + elsif(inc_step_cnt = '1') then + step_cnt <= std_logic_vector(unsigned(step_cnt)+1); + end if; + if(init_i = '1') then + step_balance <= (others => '0'); + elsif(inc_step_balance = '1') then + step_balance <= std_logic_vector(signed(step_balance)+1); + elsif(dec_step_balance = '1') then + step_balance <= std_logic_vector(signed(step_balance)-1); + end if; + if(init_i = '1') then + first_addr <= (others => '0'); + elsif(inc_first = '1') then + first_addr <= std_logic_vector(unsigned(first_addr)+1); + end if; + if(init_i = '1') then + last_addr <= (others => '0'); + elsif(inc_last = '1') then + last_addr <= std_logic_vector(unsigned(last_addr)+1); + end if; + if(unsigned(step_cnt) < unsigned(avg_steps)) then + avg_transient_on <= '1'; + else + avg_transient_on <= '0'; + end if; + if(data_valid(0) = '1') then + tag_last_i <= tag_last; + end if; + if(step_balance(step_balance'left) = '0') then + step_bal_m1 <= std_logic_vector(signed(step_balance)-1); + else + step_bal_m1 <= std_logic_vector(-signed(step_balance)-1); + end if; + if(init = '1' or new_step = '1') then + time_out_cnt <= time_out; + elsif(time_out_flag_i = '0') then + time_out_cnt <= std_logic_vector(unsigned(time_out_cnt)-1); + end if; + time_out_flag_d <= time_out_flag_i; + if(init_i = '1') then + first_step <= '1'; + elsif(data_valid(data_valid'left) = '1') then + first_step <= '0'; + end if; + if(new_step = '1') then + last_step_type <= step_type; + end if; + if(init_i = '1') then + delta_t <= "0000" & delta_init & 8x"0"; + elsif(data_valid(data_valid'left) = '1') then + if(first_step = '1' or last_step_type /= step_balance(step_balance'left)) then + delta_t <= last_step_type & 3x"0" & delta_init & 8x"0"; + else + delta_t <= step_balance(step_balance'left) & delta_t_i & 8x"0"; + end if; + end if; + end if; + end process pSEQ_LOGIC; + + time_out_flag_i <= '1' when unsigned(time_out_cnt) = 1 else '0'; + + time_out_flag <= time_out_flag_d; + + init_i <= init or (time_out_flag_i and not time_out_flag_d); + + inc_step_balance <= step_pos_i or ((not avg_transient_on) and (data_valid(0) and tag_first(31))); + dec_step_balance <= step_neg_i or ((not avg_transient_on) and (data_valid(0) and not tag_first(31))); + + new_step <= step_pos_i or step_neg_i; + + inc_step_cnt <= new_step and avg_transient_on; + + inc_last <= new_step; + inc_first <= new_step and (not avg_transient_on); + + -- This RAM is for storing tag (time and encoder step-type + iTIMING_RAM: entity work.dpram_sclk + generic map ( + WIDTH => 32, + DEPTH => 256) + port map ( + clk => clk, + addr_a => last_addr, + we_a => write_last, + data_a => new_tag, + q_a => tag_last, + addr_b => first_addr, + we_b => '0', + data_b => (others => '0'), + q_b => tag_first); + + write_last <= step_pos_i or step_neg_i; + step_type <= '1' when step_neg_i = '1' else '0'; + new_tag <= step_type & std_logic_vector(resize(unsigned(loc_time), 31)); + + time_diff <= std_logic_vector(resize(unsigned(tag_last_i(loc_time'range))-unsigned(tag_first(loc_time'range)),30)); + + time_diff_i <= std_logic_vector(resize(unsigned(time_diff),31)); + + iDIVIDE: entity work.divider + port map ( + clock => clk, + denom => step_bal_m1(7 downto 0), + numer => time_diff_i, + quotient => delta_t_i, + remain => div_remain); + +end architecture rtl; diff --git a/generic_ip/encoder_multiply/0/src/intel/divider.vhd b/generic_ip/encoder_multiply/0/src/intel/divider.vhd new file mode 100644 index 0000000..25d4d68 --- /dev/null +++ b/generic_ip/encoder_multiply/0/src/intel/divider.vhd @@ -0,0 +1,140 @@ +-- megafunction wizard: %LPM_DIVIDE% +-- GENERATION: STANDARD +-- VERSION: WM1.0 +-- MODULE: LPM_DIVIDE + +-- ============================================================ +-- File Name: divider.vhd +-- Megafunction Name(s): +-- LPM_DIVIDE +-- +-- Simulation Library Files(s): +-- lpm +-- ============================================================ +-- ************************************************************ +-- THIS IS A WIZARD-GENERATED FILE. DO NOT EDIT THIS FILE! +-- +-- 16.0.2 Build 222 07/20/2016 SJ Lite Edition +-- ************************************************************ + + +--Copyright (C) 1991-2016 Altera Corporation. All rights reserved. +--Your use of Altera Corporation's design tools, logic functions +--and other software and tools, and its AMPP partner logic +--functions, and any output files from any of the foregoing +--(including device programming or simulation files), and any +--associated documentation or information are expressly subject +--to the terms and conditions of the Altera Program License +--Subscription Agreement, the Altera Quartus Prime License Agreement, +--the Altera MegaCore Function License Agreement, or other +--applicable license agreement, including, without limitation, +--that your use is for the sole purpose of programming logic +--devices manufactured by Altera and sold by Altera or its +--authorized distributors. Please refer to the applicable +--agreement for further details. + + +LIBRARY ieee; +USE ieee.std_logic_1164.all; + +LIBRARY lpm; +USE lpm.all; + +ENTITY divider IS + PORT + ( + clock : IN STD_LOGIC ; + denom : IN STD_LOGIC_VECTOR (7 DOWNTO 0); + numer : IN STD_LOGIC_VECTOR (30 DOWNTO 0); + quotient : OUT STD_LOGIC_VECTOR (30 DOWNTO 0); + remain : OUT STD_LOGIC_VECTOR (7 DOWNTO 0) + ); +END divider; + + +ARCHITECTURE SYN OF divider IS + + SIGNAL sub_wire0 : STD_LOGIC_VECTOR (30 DOWNTO 0); + SIGNAL sub_wire1 : STD_LOGIC_VECTOR (7 DOWNTO 0); + + + + COMPONENT lpm_divide + GENERIC ( + lpm_drepresentation : STRING; + lpm_hint : STRING; + lpm_nrepresentation : STRING; + lpm_pipeline : NATURAL; + lpm_type : STRING; + lpm_widthd : NATURAL; + lpm_widthn : NATURAL + ); + PORT ( + clock : IN STD_LOGIC ; + denom : IN STD_LOGIC_VECTOR (7 DOWNTO 0); + numer : IN STD_LOGIC_VECTOR (30 DOWNTO 0); + quotient : OUT STD_LOGIC_VECTOR (30 DOWNTO 0); + remain : OUT STD_LOGIC_VECTOR (7 DOWNTO 0) + ); + END COMPONENT; + +BEGIN + quotient <= sub_wire0(30 DOWNTO 0); + remain <= sub_wire1(7 DOWNTO 0); + + LPM_DIVIDE_component : LPM_DIVIDE + GENERIC MAP ( + lpm_drepresentation => "UNSIGNED", + lpm_hint => "MAXIMIZE_SPEED=6,LPM_REMAINDERPOSITIVE=TRUE", + lpm_nrepresentation => "UNSIGNED", + lpm_pipeline => 3, + lpm_type => "LPM_DIVIDE", + lpm_widthd => 8, + lpm_widthn => 31 + ) + PORT MAP ( + clock => clock, + denom => denom, + numer => numer, + quotient => sub_wire0, + remain => sub_wire1 + ); + + + +END SYN; + +-- ============================================================ +-- CNX file retrieval info +-- ============================================================ +-- Retrieval info: PRIVATE: INTENDED_DEVICE_FAMILY STRING "Cyclone V" +-- Retrieval info: PRIVATE: PRIVATE_LPM_REMAINDERPOSITIVE STRING "TRUE" +-- Retrieval info: PRIVATE: PRIVATE_MAXIMIZE_SPEED NUMERIC "6" +-- Retrieval info: PRIVATE: SYNTH_WRAPPER_GEN_POSTFIX STRING "0" +-- Retrieval info: PRIVATE: USING_PIPELINE NUMERIC "1" +-- Retrieval info: PRIVATE: VERSION_NUMBER NUMERIC "2" +-- Retrieval info: PRIVATE: new_diagram STRING "1" +-- Retrieval info: LIBRARY: lpm lpm.lpm_components.all +-- Retrieval info: CONSTANT: LPM_DREPRESENTATION STRING "UNSIGNED" +-- Retrieval info: CONSTANT: LPM_HINT STRING "MAXIMIZE_SPEED=6,LPM_REMAINDERPOSITIVE=TRUE" +-- Retrieval info: CONSTANT: LPM_NREPRESENTATION STRING "UNSIGNED" +-- Retrieval info: CONSTANT: LPM_PIPELINE NUMERIC "3" +-- Retrieval info: CONSTANT: LPM_TYPE STRING "LPM_DIVIDE" +-- Retrieval info: CONSTANT: LPM_WIDTHD NUMERIC "8" +-- Retrieval info: CONSTANT: LPM_WIDTHN NUMERIC "31" +-- Retrieval info: USED_PORT: clock 0 0 0 0 INPUT NODEFVAL "clock" +-- Retrieval info: USED_PORT: denom 0 0 8 0 INPUT NODEFVAL "denom[7..0]" +-- Retrieval info: USED_PORT: numer 0 0 31 0 INPUT NODEFVAL "numer[30..0]" +-- Retrieval info: USED_PORT: quotient 0 0 31 0 OUTPUT NODEFVAL "quotient[30..0]" +-- Retrieval info: USED_PORT: remain 0 0 8 0 OUTPUT NODEFVAL "remain[7..0]" +-- Retrieval info: CONNECT: @clock 0 0 0 0 clock 0 0 0 0 +-- Retrieval info: CONNECT: @denom 0 0 8 0 denom 0 0 8 0 +-- Retrieval info: CONNECT: @numer 0 0 31 0 numer 0 0 31 0 +-- Retrieval info: CONNECT: quotient 0 0 31 0 @quotient 0 0 31 0 +-- Retrieval info: CONNECT: remain 0 0 8 0 @remain 0 0 8 0 +-- Retrieval info: GEN_FILE: TYPE_NORMAL divider.vhd TRUE +-- Retrieval info: GEN_FILE: TYPE_NORMAL divider.inc FALSE +-- Retrieval info: GEN_FILE: TYPE_NORMAL divider.cmp FALSE +-- Retrieval info: GEN_FILE: TYPE_NORMAL divider.bsf FALSE +-- Retrieval info: GEN_FILE: TYPE_NORMAL divider_inst.vhd FALSE +-- Retrieval info: LIB_FILE: lpm diff --git a/generic_ip/encoder_multiply/0/src/test/encoder_multiply_tb.vhd b/generic_ip/encoder_multiply/0/src/test/encoder_multiply_tb.vhd index 5de07b5..cdf9687 100644 --- a/generic_ip/encoder_multiply/0/src/test/encoder_multiply_tb.vhd +++ b/generic_ip/encoder_multiply/0/src/test/encoder_multiply_tb.vhd @@ -321,7 +321,7 @@ begin -- architecture behav -- UUTs ----------------------------------------------------------------------------- - iUUT : entity work.encoder_multiply + iUUT : entity work.encoder_multiply_0 port map ( reset_n => reset_n, clk => clk, diff --git a/generic_ip/encoder_multiply/0/src/test/encoder_speed_detect_tb.vhd b/generic_ip/encoder_multiply/0/src/test/encoder_speed_detect_tb.vhd new file mode 100644 index 0000000..04bd6c6 --- /dev/null +++ b/generic_ip/encoder_multiply/0/src/test/encoder_speed_detect_tb.vhd @@ -0,0 +1,484 @@ +------------------------------------------------------------------------------- +-- Title : encoder_speed_detect_tb +-- Project : EMB/SMB +------------------------------------------------------------------------------- +-- File : encoder_speed_detect_tb.vhd +-- Author : GMG +-- Company : Global Inkjet Systems +-- Created : 2019-03-05 +-- Last update: 2019-03-19 +-- SVN ID : Git +-- Standard : VHDL'2008 +------------------------------------------------------------------------------- +-- Description: +-- Testbench for the encoder_speed_detect_0 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_speed_detect_tb is + generic( + FILT_AVG : integer range 0 to 255 := 8); + -- .. +end entity encoder_speed_detect_tb; + +architecture behav of encoder_speed_detect_tb is + + constant CLK_PERIOD : time := 10 ns; + + -- Logging Verbosity Level + constant VERBOSITY_LEVEL : verb_level_t := VMEDIUM; + + constant MAX_SIM_TIME : time := 40.0 ms; -- Maximum simulation time + + constant RUN_TEST : integer_vector(0 to 2) := (1, 0, 0); + + -- 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 avg_steps : std_logic_vector( 7 downto 0); + signal delta_init : std_logic_vector(27 downto 0); + signal time_out : std_logic_vector(27 downto 0); + signal step_pos : std_logic; + signal step_neg : std_logic; + signal delta_t : std_logic_vector(39 downto 0); + signal step_pos_out : std_logic; + signal step_neg_out : std_logic; + signal time_out_flag : std_logic; + -- + signal abs_pos : std_logic_vector(31 downto 0); + signal delta_t_norm : integer; + + -- 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; + + shared variable 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 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 + + if(error_flag = true) then + return; + end if; + + 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); + + 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 + 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 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 + + if(error_flag = true) then + return; + end if; + + 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); + + -- 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 + 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_speed_detect_0 + port map ( + reset_n => reset_n, + clk => clk, + init => init, + enable => enable, + avg_steps => avg_steps, + delta_init => delta_init, + time_out => time_out, + step_pos => step_pos, + step_neg => step_neg, + delta_t => delta_t, + step_pos_out => step_pos_out, + step_neg_out => step_neg_out, + time_out_flag => time_out_flag); + + ----------------------------------------------------------------------------- + -- 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_out = '1') then + abs_pos <= std_logic_vector(unsigned(abs_pos)+1); + elsif(step_neg_out = '1') then + abs_pos <= std_logic_vector(unsigned(abs_pos)-1); + end if; + end if; + end process pGEN_POS; + + delta_t_norm <= to_integer(unsigned(delta_t(38 downto 8))) when delta_t(39) = '0' else -1*to_integer(unsigned(delta_t(38 downto 8))); + + ----------------------------------------------------------------------------- + -- Testbench Logic + ----------------------------------------------------------------------------- + + pTEST_PROC : process + + variable period_variance : integer range 0 to 100; + + alias loc_time is <>; + + begin + + --------------------------------------------------------------------------- + -- Initialize & Configure + --------------------------------------------------------------------------- + + reset_n <= '0'; + init <= '0'; + enable <= '0'; + + avg_steps <= std_logic_vector(to_unsigned(FILT_AVG,8)); + delta_init <= std_logic_vector(to_unsigned(DELTA_TMIN, delta_init'length)); + -- + time_out <= std_logic_vector(to_unsigned(6000, time_out'length)); + + step_pos <= '0'; + step_neg <= '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-Speed-Detection verification:", VLOW); + log_separator_line(VLOW); + + wait_clk_cycles(10); + + wait until clk = '1' and clk'event; + -- assert init to provide a clean configuration + init <= '1'; + wait until clk = '1' and clk'event; + init <= '0'; + log("Average-Speed Filtering set to " & to_string(FILT_AVG) & "." , VLOW); + + wait_clk_cycles(10); + + if(RUN_TEST(0) = 1) then + log_separator_line(VLOW); + log("Tests at constant frequency:", VLOW); + log_separator_line(VLOW); + wait until clk = '1' and clk'event; + loc_time <= force std_logic_vector(28x"1"-to_unsigned(10000+1,28)); + wait until clk = '1' and clk'event; + loc_time <= release; + run_encoder_profile_1(100.0e3, '0', 50, enable, step_pos, step_neg, true); + wait for 65 us; + run_encoder_profile_1(100.0e3, '1', 50, enable, step_pos, step_neg, true); + wait for 65 us; + run_encoder_profile_1(100.0e3, '0', 25, enable, step_pos, step_neg); + wait for 50 us; + run_encoder_profile_1(100.0e3, '0', 25, enable, step_pos, step_neg); + wait for 50 us; + run_encoder_profile_1(100.0e3, '1', 25, enable, step_pos, step_neg); + wait for 50 us; + run_encoder_profile_1(100.0e3, '0', 1, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_1(100.0e3, '0', 1, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_1(100.0e3, '0', 1, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_1(100.0e3, '1', 1, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_1(100.0e3, '0', 1, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_1(100.0e3, '1', 1, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_1(100.0e3, '0', 1, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_1(100.0e3, '1', 3, enable, step_pos, step_neg); + wait for 20 us; + end if; + + period_variance := 45; + log_separator_line(VLOW); + + if(RUN_TEST(1) = 1) then + log("Tests at variable frequency (+/-" & to_string(period_variance) & "%):", VLOW); + log_separator_line(VLOW); + wait until clk = '1' and clk'event; + loc_time <= force std_logic_vector(28x"1"-to_unsigned(10000+1,28)); + wait until clk = '1' and clk'event; + loc_time <= release; + run_encoder_profile_2(100.0e3, '0', 50, period_variance, enable, step_pos, step_neg, true); + wait for 50 us; + run_encoder_profile_2(100.0e3, '0', 25, period_variance, enable, step_pos, step_neg); + wait for 50 us; + run_encoder_profile_2(100.0e3, '0', 25, period_variance, enable, step_pos, step_neg); + wait for 50 us; + run_encoder_profile_2(100.0e3, '1', 25, period_variance, enable, step_pos, step_neg); + wait for 50 us; + run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_2(100.0e3, '1', 1, period_variance, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_2(100.0e3, '1', 1, period_variance, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_2(100.0e3, '0', 1, period_variance, enable, step_pos, step_neg); + wait for 20 us; + run_encoder_profile_2(100.0e3, '1', 3, period_variance, enable, step_pos, step_neg); + wait for 20 us; + end if; + + --------------------------------------------------------------------------- + -- End + --------------------------------------------------------------------------- + + running <= false; + wait for 10 us; + if(error_flag = false) then + passed <= true; + end if; + 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;