diff --git a/generic_ip/common/0/sim/sim_queue_delay.do b/generic_ip/common/0/sim/sim_queue_delay.do new file mode 100644 index 0000000..c46d05b --- /dev/null +++ b/generic_ip/common/0/sim/sim_queue_delay.do @@ -0,0 +1,39 @@ +# Simulation macro + +.main clear + +set SRC_PATH ./../src + +vlib ./work +vmap work ./work + + + +# Compile the UUT +vcom -work work $SRC_PATH/common_pkg.vhd +vcom -work work $SRC_PATH/altera/sync_fifo.vhd +vcom -2008 -work work $SRC_PATH/queue_and_delay.vhd + +# Compile the Testbench +vcom -2008 -work work $SRC_PATH/test/queue_and_delay_tb.vhd + +# Simulate +vsim -t ps work.queue_and_delay_tb + +#add wave /queue_and_delay_tb/* +#add wave /queue_and_delay_tb/iUUT/* + +# remove warnings from standard numeric/arithmetic packages (speed-up simulation) +set StdArithNoWarnings 1 +set NumericStdNoWarnings 1 + +do wave_queue_delay.do + +when -label end_of_simulation {completed == true} {echo "Simulation Completed"; stop ;} +run -all + +#run 100 us; + + + + diff --git a/generic_ip/common/0/sim/wave_queue_delay.do b/generic_ip/common/0/sim/wave_queue_delay.do new file mode 100644 index 0000000..65f7808 --- /dev/null +++ b/generic_ip/common/0/sim/wave_queue_delay.do @@ -0,0 +1,64 @@ +onerror {resume} +quietly WaveActivateNextPane {} 0 +add wave -noupdate /queue_and_delay_tb/rstN +add wave -noupdate /queue_and_delay_tb/clk +add wave -noupdate /queue_and_delay_tb/init +add wave -noupdate /queue_and_delay_tb/cnt_dir +add wave -noupdate /queue_and_delay_tb/pIO_MNG/loc_cnt +add wave -noupdate -radix unsigned /queue_and_delay_tb/delay +add wave -noupdate -radix decimal /queue_and_delay_tb/ext_time +add wave -noupdate /queue_and_delay_tb/kickstart +add wave -noupdate /queue_and_delay_tb/kickstart_ref +add wave -noupdate /queue_and_delay_tb/wave_in +add wave -noupdate /queue_and_delay_tb/wave_delayed +add wave -noupdate /queue_and_delay_tb/wave_out +add wave -noupdate /queue_and_delay_tb/wave_ref +add wave -noupdate /queue_and_delay_tb/wave_error +add wave -noupdate /queue_and_delay_tb/error_detected +add wave -noupdate -radix decimal /queue_and_delay_tb/init_time +add wave -noupdate /queue_and_delay_tb/offset +add wave -noupdate /queue_and_delay_tb/pulse_width +add wave -noupdate /queue_and_delay_tb/passed +add wave -noupdate /queue_and_delay_tb/failed +add wave -noupdate /queue_and_delay_tb/completed +add wave -noupdate /queue_and_delay_tb/ovf_error +add wave -noupdate /queue_and_delay_tb/iUUT/rstN +add wave -noupdate /queue_and_delay_tb/iUUT/clk +add wave -noupdate /queue_and_delay_tb/iUUT/init +add wave -noupdate /queue_and_delay_tb/iUUT/cnt_dir +add wave -noupdate -radix unsigned /queue_and_delay_tb/iUUT/delay +add wave -noupdate -radix decimal /queue_and_delay_tb/iUUT/ext_time +add wave -noupdate /queue_and_delay_tb/iUUT/wave_in +add wave -noupdate /queue_and_delay_tb/iUUT/wave_out +add wave -noupdate /queue_and_delay_tb/iUUT/ovf_error +add wave -noupdate /queue_and_delay_tb/iUUT/toggle_in +add wave -noupdate /queue_and_delay_tb/iUUT/toggle_out +add wave -noupdate /queue_and_delay_tb/iUUT/time_elapsed +add wave -noupdate /queue_and_delay_tb/iUUT/wave_in_d +add wave -noupdate /queue_and_delay_tb/iUUT/wave_out_i +add wave -noupdate /queue_and_delay_tb/iUUT/wave_out_i_d +add wave -noupdate /queue_and_delay_tb/iUUT/new_event +add wave -noupdate /queue_and_delay_tb/iUUT/pending_trans +add wave -noupdate /queue_and_delay_tb/iUUT/fifo_rd_en +add wave -noupdate /queue_and_delay_tb/iUUT/fifo_empty +add wave -noupdate /queue_and_delay_tb/iUUT/fifo_full +add wave -noupdate -radix unsigned /queue_and_delay_tb/iUUT/local_time +add wave -noupdate -radix decimal /queue_and_delay_tb/iUUT/event_time +TreeUpdate [SetDefaultTree] +WaveRestoreCursors {{Cursor 1} {5695000 ps} 0} +quietly wave cursor active 1 +configure wave -namecolwidth 343 +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 ps +update +WaveRestoreZoom {814844 ps} {2822428 ps} diff --git a/generic_ip/common/0/src/queue_and_delay.vhd b/generic_ip/common/0/src/queue_and_delay.vhd index 5a59900..e5b67e4 100644 --- a/generic_ip/common/0/src/queue_and_delay.vhd +++ b/generic_ip/common/0/src/queue_and_delay.vhd @@ -6,7 +6,7 @@ -- Author : Gianmarco Girau -- Company : Global Inkjet Systems -- Created : 2016-05-28 --- Last update: 2019-05-23 +-- Last update: 2018-06-28 -- SVN ID : Git -- Standard : VHDL'93 ------------------------------------------------------------------------------- @@ -46,17 +46,30 @@ end queue_and_delay; architecture RTL of queue_and_delay is + component sync_fifo + generic ( + FIFO_WIDTH : natural range 1 to 32; + FIFO_DEPTH : natural range 1 to 8192); + port ( + clock : in std_logic; + data : in std_logic_vector (FIFO_WIDTH-1 downto 0); + rdreq : in std_logic; + sclr : in std_logic; + wrreq : in std_logic; + empty : out std_logic; + full : out std_logic; + q : out std_logic_vector (FIFO_WIDTH-1 downto 0); + usedw : out std_logic_vector (log2(FIFO_DEPTH)-1 downto 0)); + end component; + signal toggle_in, toggle_out,time_elapsed : std_logic; signal wave_in_d, wave_out_i, pending_trans : std_logic; signal fifo_rd_en, fifo_empty, fifo_full : std_logic; signal cnt_dir_i, wave_out_i_d, new_event : std_logic; signal wave_out_ii, shf_enable : std_logic; signal delay_line : std_logic_vector( 7 downto 0); - signal local_time, local_time_d, event_time : std_logic_vector(31 downto 0); - signal uevent_plus_udelay : unsigned(31 downto 0); - signal sevent_plus_sdelay : signed(31 downto 0); - signal sevent_minus_sdelay : signed(31 downto 0); - + signal int_time, local_time_d, event_time : std_logic_vector(31 downto 0); + begin -- RTL ----------------------------------------------------------------------------- @@ -74,13 +87,13 @@ begin -- RTL end if; end process pInputEdgeDetect; - iENTRY_QUEUE: entity work.sync_fifo + iENTRY_QUEUE: sync_fifo generic map( FIFO_WIDTH => 32, FIFO_DEPTH => queue_size) port map ( clock => clk, - data => local_time, + data => int_time, rdreq => fifo_rd_en, sclr => init, wrreq => toggle_in, @@ -105,33 +118,24 @@ begin -- RTL -- Time reference: either use the external one or generate it locally gTIME_REF0: if use_ext_time = true generate - - pLATCH_LOCTIME : process(clk) - begin - if(clk = '1' and clk'event) then - local_time <= ext_time; - end if; - end process pLATCH_LOCTIME; + int_time <= ext_time; - shf_enable <= '1' when local_time /= local_time_d else '0'; + shf_enable <= '1' when int_time /= local_time_d else '0'; - --pTimeElapsed : process(cnt_dir, local_time, event_time, delay) + --pTimeElapsed : process(cnt_dir, int_time, event_time, delay) pTimeElapsed : process(clk) begin if(clk = '1' and clk'event) then - local_time_d <= local_time; + local_time_d <= int_time; cnt_dir_i <= cnt_dir; - sevent_plus_sdelay <= signed(event_time) + signed(delay); - sevent_minus_sdelay <= signed(event_time) - signed(delay); - if(cnt_dir_i = '0') then - if(signed(local_time) >= sevent_plus_sdelay) then + if(signed(int_time) >= signed(event_time) + signed(delay)) then time_elapsed <= '1'; else time_elapsed <= '0'; end if; else - if(signed(local_time) <= sevent_minus_sdelay) then + if(signed(int_time) <= signed(event_time) - signed(delay)) then time_elapsed <= '1'; else time_elapsed <= '0'; @@ -148,25 +152,19 @@ begin -- RTL pGEN_LOC_REF : process(rstN, clk) begin if(rstN = '0') then - local_time <= (others => '0'); - uevent_plus_udelay <= (others => '0'); - time_elapsed <= '0'; + int_time <= (others => '0'); elsif(clk = '1' and clk'event) then if(init = '1') then - local_time <= (others => '0'); + int_time <= (others => '0'); else - local_time <= std_logic_vector(unsigned(local_time)+1); - end if; - uevent_plus_udelay <= unsigned(event_time) + unsigned(delay); - if unsigned(local_time) >= uevent_plus_udelay then - time_elapsed <= '1'; - else - time_elapsed <= '0'; + int_time <= std_logic_vector(unsigned(int_time)+1); end if; end if; end process pGEN_LOC_REF; new_event <= fifo_rd_en; + time_elapsed <= '1' when unsigned(int_time) >= unsigned(event_time) + unsigned(delay) else '0'; + shf_enable <= '1'; end generate gTIME_REF1; diff --git a/generic_ip/common/0/src/test/queue_and_delay_tb.vhd b/generic_ip/common/0/src/test/queue_and_delay_tb.vhd new file mode 100644 index 0000000..598bb7b --- /dev/null +++ b/generic_ip/common/0/src/test/queue_and_delay_tb.vhd @@ -0,0 +1,620 @@ +------------------------------------------------------------------------------- +-- Title : Testbench for queue_and_delay +-- Project : project independent +------------------------------------------------------------------------------- +-- File : .vhd +-- Author : Gianmarco Girau +-- Company : Global Inkjet Systems +-- Created : 2016-11-01 +-- Last update: 2017-11-06 +-- SVN ID : Git +-- Standard : VHDL'93 +------------------------------------------------------------------------------- +-- Description: +-- Simple Testbench for the queue and delay: Only corner cases of the delay +-- mechanism are checked: wrapping around of the counter, usage of the external +-- reference for the counter, up and down counting. +-- Check also latency issues when using the external counter: what happens if +-- the external counter is not updated every clock cycles. +------------------------------------------------------------------------------- + +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; + +entity queue_and_delay_tb is + -- ... +end queue_and_delay_tb; + +architecture behav of queue_and_delay_tb is + + ----------------------------------------------------------------------------- + -- Component declarations + ----------------------------------------------------------------------------- + + component queue_and_delay + generic ( + use_ext_time : boolean; + queue_size : natural range 16 to 8192); + port ( + rstN : in std_logic; + clk : in std_logic; + init : in std_logic; + cnt_dir : in std_logic; + delay : in std_logic_vector(31 downto 0); + ext_time : in std_logic_vector(31 downto 0); + wave_in : in std_logic; + wave_out : out std_logic; + ovf_error : out std_logic); + end component; + + + ----------------------------------------------------------------------------- + -- Constant, types and signals + ----------------------------------------------------------------------------- + + constant clk_period : time := 10.0 ns; -- ems clock + + signal rstN : std_logic; + signal clk : std_logic := '0'; + signal init : std_logic; + signal cnt_dir : std_logic; + signal delay : std_logic_vector(31 downto 0); + signal ext_time : std_logic_vector(31 downto 0); + signal wave_in : std_logic; + signal wave_out : std_logic; + signal ovf_error : std_logic; + -- + signal wave_delayed : std_logic; + signal wave_delayed_d : std_logic; + signal wave_ref : std_logic; + signal wave_error : std_logic; + signal init_time : std_logic_vector(31 downto 0); + signal offset : integer; + signal pulse_width : integer; + signal passed : boolean; + signal failed : boolean; + signal completed : boolean; + signal kickstart : std_logic; + signal kickstart_ref : std_logic; + signal error_detected : std_logic; + +begin -- behav + + -- clock generation + clk <= not clk after 0.5*clk_period; + + ----------------------------------------------------------------------------- + -- UUT + ----------------------------------------------------------------------------- + + iUUT: queue_and_delay + generic map ( + use_ext_time => true, + queue_size => 128) + port map ( + rstN => rstN, + clk => clk, + init => init, + cnt_dir => cnt_dir, + delay => delay, + ext_time => ext_time, + wave_in => wave_in, + wave_out => wave_out, + ovf_error => ovf_error); + + ----------------------------------------------------------------------------- + -- Stimuli generator and output checker + ----------------------------------------------------------------------------- + + pIO_MNG: process(rstN, init, clk) + variable loc_cnt : integer; + begin + if(rstN = '0') then + loc_cnt := 0; + ext_time <= (others => '0'); + kickstart <= '0'; + kickstart_ref <= '0'; + wave_error <= '0'; + elsif(init = '1') then + loc_cnt := 0; + ext_time <= init_time; + kickstart <= '0'; + kickstart_ref <= '0'; + wave_error <= '0'; + elsif(clk = '1' and clk'event) then + loc_cnt := loc_cnt+1; + if(cnt_dir = '0') then + ext_time <= std_logic_vector(signed(ext_time)+1); + else + ext_time <= std_logic_vector(signed(ext_time)-1); + end if; + if(loc_cnt = offset) then + kickstart <= '1'; + else + kickstart <= '0'; + end if; + --if(loc_cnt = (offset + to_integer(unsigned(delay))+1)) then + -- wave_delayed <= '1'; + --elsif(loc_cnt = offset + pulse_width + to_integer(unsigned(delay)+1)) then + -- wave_delayed <= '0'; + --end if; + if(loc_cnt = (offset + to_integer(unsigned(delay))+3)) then + kickstart_ref <= '1'; + --elsif(loc_cnt = offset + pulse_width + to_integer(unsigned(delay)+2)) then + else + kickstart_ref <= '0'; + end if; + if(wave_out /= wave_ref) then + wave_error <= '1'; + end if; + end if; + end process pIO_MNG; + + pREP : process(clk) + begin + if(clk = '1' and clk'event) then + wave_delayed_d <= wave_delayed; + if(init = '1') then + error_detected <= '0'; + elsif(wave_error = '1') then + error_detected <= '1'; + end if; + end if; + end process pREP; + + -- Generate the input wave + pWAVE_IN_GEN : process + begin + wave_in <= '0'; + -- wait for the kickstart + wait until kickstart = '1' and kickstart'event; + -- send pulse #1 + wave_in <= '1'; + for n in 1 to pulse_width loop + wait until clk = '1' and clk'event; + end loop; -- n + wave_in <= '0'; + -- send pulse #2 + for n in 1 to 6 loop + wait until ext_time'event; + end loop; -- n + wave_in <= '1'; + for n in 1 to pulse_width loop + wait until clk = '1' and clk'event; + end loop; -- n + wave_in <= '0'; + /* + -- send pulse #3 + for n in 1 to 3 loop + wait until ext_time'event; + end loop; -- n + wave_in <= '1'; + for n in 1 to pulse_width loop + wait until clk = '1' and clk'event; + end loop; -- n + wave_in <= '0'; + */ + end process pWAVE_IN_GEN; + + -- Generate the reference wave + pWAVE_REF_GEN : process + begin + wave_delayed <= '0'; + -- wait for the kickstart + wait until kickstart_ref = '1' and kickstart_ref'event; + -- send pulse #1 + wave_delayed <= '1'; + for n in 1 to pulse_width loop + wait until clk = '1' and clk'event; + end loop; -- n + wave_delayed <= '0'; + -- send pulse #2 + for n in 1 to 6 loop + wait until ext_time'event; + end loop; -- n + wave_delayed <= '1'; + for n in 1 to pulse_width loop + wait until clk = '1' and clk'event; + end loop; -- n + wave_delayed <= '0'; + /* + -- send pulse #3 + for n in 1 to 3 loop + wait until ext_time'event; + end loop; -- n + wave_delayed <= '1'; + for n in 1 to pulse_width loop + wait until clk = '1' and clk'event; + end loop; -- n + wave_delayed <= '0'; + */ + end process pWAVE_REF_GEN; + + wave_ref <= wave_delayed and not wave_delayed_d; + + ----------------------------------------------------------------------------- + -- Test the block via configuration & control through the C&S bus + ----------------------------------------------------------------------------- + + pCFG_and_CTRL : process + begin + + passed <= false; + failed <= false; + completed <= false; + + init <= '0'; + offset <= 0; + pulse_width <= 0; + cnt_dir <= '0'; + delay <= (others => '0'); + init_time <= (others => '0'); + + -- Apply reset + rstN <= '0'; + wait for 20*clk_period; + -- + wait for 10*clk_period; + wait until clk = '1' and clk'event; + rstN <= '1'; + + --------------------------------------------------------------------------- + -- Test-Case #1 + --------------------------------------------------------------------------- + -- Count Up, init count positive, no-wrapping all + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '0'; + delay <= std_logic_vector(to_signed(10, 32)); + init_time <= std_logic_vector(to_signed(0, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#1" severity error; + completed <= true; + else + assert false report "TC#1 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + --------------------------------------------------------------------------- + -- Test-Case #2 + --------------------------------------------------------------------------- + -- Count Up, init count negative, no-wrapping at all + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '0'; + delay <= std_logic_vector(to_signed(10, 32)); + init_time <= std_logic_vector(to_signed(-100, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#2" severity error; + completed <= true; + else + assert false report "TC#2 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + --------------------------------------------------------------------------- + -- Test-Case #3 + --------------------------------------------------------------------------- + -- Count Up, init count negative, counter wrapping between the rising and + -- falling edge of the input pulse + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '0'; + delay <= std_logic_vector(to_signed(10, 32)); + init_time <= std_logic_vector(to_signed(-52, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#3" severity error; + completed <= true; + else + assert false report "TC#3 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + --------------------------------------------------------------------------- + -- Test-Case #4 + --------------------------------------------------------------------------- + -- Count Up, init count negative, counter wrapping between the rising and + -- falling edge of the output pulse + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '0'; + delay <= std_logic_vector(to_signed(10, 32)); + init_time <= std_logic_vector(to_signed(-62, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#4" severity error; + completed <= true; + else + assert false report "TC#4 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + --------------------------------------------------------------------------- + -- Test-Case #5 + --------------------------------------------------------------------------- + -- Count Down, init count positive, no-wrapping all + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '1'; + delay <= std_logic_vector(to_signed(10, 32)); + init_time <= std_logic_vector(to_signed(100, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#5" severity error; + completed <= true; + else + assert false report "TC#5 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + --------------------------------------------------------------------------- + -- Test-Case #6 + --------------------------------------------------------------------------- + -- Count Down, init count negative, no-wrapping at all + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '1'; + delay <= std_logic_vector(to_signed(10, 32)); + init_time <= std_logic_vector(to_signed(-100, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#6" severity error; + completed <= true; + else + assert false report "TC#6 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + --------------------------------------------------------------------------- + -- Test-Case #7 + --------------------------------------------------------------------------- + -- Count Down, init count positive, counter wrapping between the rising and + -- falling edge of the input pulse + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '1'; + delay <= std_logic_vector(to_signed(10, 32)); + init_time <= std_logic_vector(to_signed(51, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#7" severity error; + completed <= true; + else + assert false report "TC#7 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + --------------------------------------------------------------------------- + -- Test-Case #8 + --------------------------------------------------------------------------- + -- Count Down, init count positive, counter wrapping between the rising and + -- falling edge of the output pulse + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '1'; + delay <= std_logic_vector(to_signed(10, 32)); + init_time <= std_logic_vector(to_signed(62, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#8" severity error; + completed <= true; + else + assert false report "TC#8 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + --------------------------------------------------------------------------- + -- Test-Case #9 + --------------------------------------------------------------------------- + -- Check the minimum latency + + -- Configure + init <= '1'; + -- + offset <= 50; + pulse_width <= 3; + cnt_dir <= '1'; + delay <= std_logic_vector(to_signed(3, 32)); + init_time <= std_logic_vector(to_signed(62, 32)); + + wait for 5*clk_period; + wait until clk = '1' and clk'event; + init <= '0'; + + -- run the test case + wait until wave_ref = '1'; + wait until wave_ref = '0'; + wait until wave_ref = '1'; + wait until wave_ref = '0'; + + -- check the results + wait until clk = '1' and clk'event; + if(error_detected = '1') then + failed <= true; + assert false report "ERROR on TC#9" severity error; + completed <= true; + else + assert false report "TC#9 passed" severity note; + end if; + + passed <= true; + wait for 5*clk_period; + wait until clk = '1' and clk'event; + + + --------------------------------------------------------------------------- + -- End of Testing + --------------------------------------------------------------------------- + + completed <= true; + + wait for 1 ms; + + end process pCFG_and_CTRL; + +end behav; + diff --git a/generic_ip/pulse_generator/2/doc/pulse_generator_2.json b/generic_ip/pulse_generator/2/doc/pulse_generator_2.json index 41c304c..957cd95 100644 --- a/generic_ip/pulse_generator/2/doc/pulse_generator_2.json +++ b/generic_ip/pulse_generator/2/doc/pulse_generator_2.json @@ -373,31 +373,50 @@ "reset": "0x00", "width": "32", "description": "Static value used by the logic to compute W&H wheel correction: need to be set to the number of 2^32/pixel_num, where pixel_num is the number of stripes programmed in the Pulse-Generator for a Print-Operation corresponding to a full wheel rotation" + }, + "wh_loop_filter_corr": { + "address": "21", + "width": "32", + "description": "Loop filter coefficient for W&H wheel correction", + "fields": { + "delta_error_coeff": { + "type": "w", + "bit_len": "16", + "reset": "0x0100", + "description": "Loop Filter delta-error coefficient" + }, + "tot_error_coeff": { + "type": "w", + "bit_len": "16", + "reset": "0x0100", + "description": "Loop Filter tot-error coefficient" + } + } }, "wh_pos_err_curr": { "type": "r", - "address": "21", + "address": "22", "reset": "0x00", "width": "24", "description": "W&H wheel misalignment measured on the last rotation [encoder steps], represented as signed, fixed point with 16-bits integer part and 8-bits mantissa" }, "wh_pos_err_max": { "type": "r", - "address": "22", + "address": "23", "reset": "0x00", "width": "24", "description": "Maximum recorded value of W&H wheel misalignment [encoder steps], represented as signed, fixed point with 16-bits integer part and 8-bits mantissa" }, "wh_div_ratio_corr": { "type": "r", - "address": "23", + "address": "24", "reset": "0x00", "width": "32", "description": "Division-Ratio Correction (32-LSBs)" }, "wh_rotation_cnt": { "type": "r", - "address": "24", + "address": "25", "reset": "0x00", "width": "16", "description": "Rotation Count" diff --git a/generic_ip/pulse_generator/2/sim/wave_ch_ctrl_cfg.do b/generic_ip/pulse_generator/2/sim/wave_ch_ctrl_cfg.do index aaf4246..9fbd349 100644 --- a/generic_ip/pulse_generator/2/sim/wave_ch_ctrl_cfg.do +++ b/generic_ip/pulse_generator/2/sim/wave_ch_ctrl_cfg.do @@ -7,11 +7,18 @@ add wave -noupdate /ch_ctrl_cfg_tb/rotation_idx add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/wh_rotat_cnt add wave -noupdate /ch_ctrl_cfg_tb/wheel_corr_on add wave -noupdate /ch_ctrl_cfg_tb/pos_error +add wave -noupdate /ch_ctrl_cfg_tb/pos_error_old add wave -noupdate /ch_ctrl_cfg_tb/div_ratio_corr_r add wave -noupdate /ch_ctrl_cfg_tb/div_ratio_r add wave -noupdate -radix hexadecimal /ch_ctrl_cfg_tb/iUUT/div_ratio add wave -noupdate -radix hexadecimal /ch_ctrl_cfg_tb/iUUT/div_ratio_i add wave -noupdate -radix hexadecimal /ch_ctrl_cfg_tb/iUUT/div_ratio_corr +add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/wh_loop_filt_tot +add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/wh_loop_filt_delta +add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/wh_pos_err_old +add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/wh_tot_err +add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/wh_delta_err +add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/wh_loop_err add wave -noupdate /ch_ctrl_cfg_tb/wh_pos_err_r add wave -noupdate /ch_ctrl_cfg_tb/init add wave -noupdate /ch_ctrl_cfg_tb/pd_in @@ -55,7 +62,7 @@ add wave -noupdate /ch_ctrl_cfg_tb/iUUT/wh_pos_err add wave -noupdate /ch_ctrl_cfg_tb/iUUT/wh_pos_err_max add wave -noupdate /ch_ctrl_cfg_tb/iUUT/wh_pos_err_abs add wave -noupdate /ch_ctrl_cfg_tb/iUUT/wh_pos_err_2_corr -add wave -noupdate /ch_ctrl_cfg_tb/iUUT/div_ratio_corr_i +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/div_ratio_corr_new add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/pos_error add wave -noupdate -radix hexadecimal /ch_ctrl_cfg_tb/iUUT/div_ratio_corr_ext add wave -noupdate -radix hexadecimal /ch_ctrl_cfg_tb/iUUT/div_ratio @@ -198,9 +205,38 @@ add wave -noupdate -radix hexadecimal /ch_ctrl_cfg_tb/iUUT/div_offset add wave -noupdate -radix hexadecimal /ch_ctrl_cfg_tb/iUUT/div_ratio add wave -noupdate /ch_ctrl_cfg_tb/iUUT/div_ratio_i add wave -noupdate /ch_ctrl_cfg_tb/iUUT/div_ratio_corr +add wave -noupdate -divider {PD Queue} +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/use_ext_time +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/queue_size +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/rstN +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/clk +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/init +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/cnt_dir +add wave -noupdate -radix decimal /ch_ctrl_cfg_tb/iUUT/iPD_queue/delay +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/ext_time +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/wave_in +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/wave_out +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/ovf_error +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/toggle_in +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/toggle_out +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/time_elapsed +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/wave_in_d +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/wave_out_i +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/pending_trans +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/fifo_rd_en +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/fifo_empty +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/fifo_full +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/cnt_dir_i +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/wave_out_i_d +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/new_event +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/wave_out_ii +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/shf_enable +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/delay_line +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/local_time_d +add wave -noupdate /ch_ctrl_cfg_tb/iUUT/iPD_queue/event_time TreeUpdate [SetDefaultTree] -WaveRestoreCursors {{Cursor 1} {6965000 ps} 1} {{Cursor 2} {6392035000 ps} 0} -quietly wave cursor active 2 +WaveRestoreCursors {{Cursor 1} {187312146 ps} 0} {{Cursor 2} {18003534285 ps} 0} +quietly wave cursor active 1 configure wave -namecolwidth 293 configure wave -valuecolwidth 216 configure wave -justifyvalue left @@ -215,4 +251,4 @@ configure wave -griddelta 40 configure wave -timeline 0 configure wave -timelineunits ps update -WaveRestoreZoom {13255799629 ps} {19418626335 ps} +WaveRestoreZoom {0 ps} {3081416704 ps} diff --git a/generic_ip/pulse_generator/2/src/pulse_generator_2.vhd b/generic_ip/pulse_generator/2/src/pulse_generator_2.vhd index 7ca1e2c..e5ecd3b 100644 --- a/generic_ip/pulse_generator/2/src/pulse_generator_2.vhd +++ b/generic_ip/pulse_generator/2/src/pulse_generator_2.vhd @@ -6,7 +6,7 @@ -- Author : Gianmarco Girau -- Company : Global Inkjet Systems -- Created : 2016-10-24 --- Last update: 2019-06-05 +-- Last update: 2019-06-11 -- SVN ID : Git -- Standard : VHDL'93 ------------------------------------------------------------------------------- @@ -101,8 +101,8 @@ architecture rtl of pulse_generator_2 is signal wh_meas_event, ch_mask_fe, first_po_started : std_logic; -- signal abs_pos_msb_old : std_logic_vector( 1 downto 0); - signal wh_corr_ready_v : std_logic_vector( 2 downto 0); signal ch_ctrl_csr_addr, ch_ctrl_trigger_in : std_logic_vector( 3 downto 0); + signal wh_corr_ready_v : std_logic_vector( 4 downto 0); signal avs_addr : std_logic_vector( 4 downto 0); signal pulse_width, pulse_width_ext, init_v : std_logic_vector( 7 downto 0); signal ch_ctrl_start_addr : std_logic_vector( 7 downto 0); @@ -111,9 +111,11 @@ architecture rtl of pulse_generator_2 is signal wh_rotat_cnt : std_logic_vector(15 downto 0); signal overdrive_events, overdrive_thres : std_logic_vector(15 downto 0); signal pulse_interval, basic_pos_error : std_logic_vector(15 downto 0); + signal wh_loop_filt_tot, wh_loop_filt_delta : std_logic_vector(15 downto 0); signal img_size, pixel_cnt : std_logic_vector(23 downto 0); signal ch_ctrl_vector_out : std_logic_vector(23 downto 0); signal wh_pos_err, wh_pos_err_max : std_logic_vector(23 downto 0); + signal wh_pos_err_old, wh_tot_err, wh_delta_err : std_logic_vector(23 downto 0); signal wh_pos_err_abs : std_logic_vector(23 downto 0); signal ch_offset, ch_delay, avs_readdata_i : std_logic_vector(31 downto 0); signal ch_ctrl_writedata, ch_ctrl_readdata : std_logic_vector(31 downto 0); @@ -127,8 +129,8 @@ architecture rtl of pulse_generator_2 is signal start_pos, abs_pos_d : std_logic_vector(31 downto 0); signal wh_pos_err_2_corr : std_logic_vector(31 downto 0); signal div_offset, div_ratio, div_ratio_i : std_logic_vector(33 downto 0); - signal div_ratio_corr, div_ratio_corr_i : std_logic_vector(33 downto 0); - signal pos_error : std_logic_vector(39 downto 0); + signal div_ratio_corr, div_ratio_corr_new : std_logic_vector(33 downto 0); + signal pos_error, wh_loop_err : std_logic_vector(39 downto 0); signal div_ratio_corr_ext : std_logic_vector(55 downto 0); begin @@ -411,7 +413,7 @@ begin if(init_wh = '1') then div_ratio_corr <= (others => '0'); elsif(wh_corr_ready = '1') then - div_ratio_corr <= div_ratio_corr_i; + div_ratio_corr <= std_logic_vector(signed(div_ratio_corr)+signed(div_ratio_corr_new)); end if; div_ratio_i <= std_logic_vector(signed(div_ratio)+signed(div_ratio_corr)); if(init_wh = '1' ) then @@ -443,7 +445,15 @@ begin end if; wh_corr_ready_v <= wh_corr_ready_v(wh_corr_ready_v'left-1 downto 0) & wh_error_meas_avail; pos_error <= std_logic_vector(signed(basic_pos_error & 24x"0") - signed(div_ratio)); - div_ratio_corr_ext <= std_logic_vector(signed(wh_pos_err)*signed(wh_pos_err_2_corr)); + if(init_wh = '1' ) then + wh_pos_err_old <= (others => '0'); + elsif(wh_corr_ready_v(0) = '1') then + wh_pos_err_old <= wh_pos_err; + end if; + if(wh_corr_ready_v(0) = '1') then + wh_loop_err <= std_logic_vector(signed(wh_delta_err)*signed(wh_loop_filt_delta)+signed(wh_tot_err)*signed(wh_loop_filt_tot)); + end if; + div_ratio_corr_ext <= std_logic_vector(signed(wh_loop_err(wh_loop_err'left-8 downto 8))*signed(wh_pos_err_2_corr)); -- Compute the absolute value of the position-error. Use a simple 1's -- complement if you need to do sign inversion if(wh_pos_err(wh_pos_err'left) = '1') then @@ -466,13 +476,16 @@ begin wh_meas_event <= loc_pd_d xor ch_mask_fe; - div_ratio_corr_i <= std_logic_vector(resize(signed(div_ratio_corr_ext(div_ratio_corr_ext'left downto 16)),div_ratio'length)); + div_ratio_corr_new <= std_logic_vector(resize(signed(div_ratio_corr_ext(div_ratio_corr_ext'left downto 16)),div_ratio'length)); wh_corr_ready <= wh_corr_ready_v(wh_corr_ready_v'left); -- No need to use 24 bits mantissa for pos_error, 8 bits are plenty wh_pos_err <= pos_error(pos_error'left downto 16); + wh_tot_err <= wh_pos_err; + wh_delta_err <= std_logic_vector(signed(wh_pos_err)-signed(wh_pos_err_old)); + ----------------------------------------------------------------------------- -- Spitting logic ----------------------------------------------------------------------------- @@ -729,6 +742,8 @@ begin -- wh_apply_corr_en <= '0'; wh_pos_err_2_corr <= (others => '0'); + wh_loop_filt_delta <= std_logic_vector(to_signed(256, 16)); + wh_loop_filt_tot <= std_logic_vector(to_signed(256, 16)); elsif(clk = '1' and clk'event) then -- CTRL registers if(avs_write_i = '1' and avs_address(9 downto 5) = "00000") then @@ -772,6 +787,9 @@ begin div_offset(23 downto 0) <= avs_writedata(23 downto 0); when WH_POS_ERR_2_CORR_REG => wh_pos_err_2_corr <= avs_writedata; + when WH_LOOP_FILT_COEFF => + wh_loop_filt_delta <= avs_writedata(15 downto 0); + wh_loop_filt_tot <= avs_writedata(31 downto 16); when others => null; end case; end if; @@ -827,6 +845,8 @@ begin avs_readdata_i <= fire_req_cnt; when WH_POS_ERR_2_CORR_REG => avs_readdata_i <= wh_pos_err_2_corr; + when WH_LOOP_FILT_COEFF => + avs_readdata_i <= wh_loop_filt_tot & wh_loop_filt_delta; when WH_POS_ERR_CURR_REG => avs_readdata_i <= X"00" & wh_pos_err; when WH_POS_ERR_MAX_REG => diff --git a/generic_ip/pulse_generator/2/src/pulse_generator_2_pkg.vhd b/generic_ip/pulse_generator/2/src/pulse_generator_2_pkg.vhd index e175630..067f04a 100644 --- a/generic_ip/pulse_generator/2/src/pulse_generator_2_pkg.vhd +++ b/generic_ip/pulse_generator/2/src/pulse_generator_2_pkg.vhd @@ -6,7 +6,7 @@ -- Author : GMG -- Company : Global Inkjet Systems -- Created : 2018-08-22 --- Last update: 2019-06-05 +-- Last update: 2019-06-10 -- SVN ID : Git -- Standard : VHDL'2008 ------------------------------------------------------------------------------- @@ -42,10 +42,11 @@ package pulse_generator_2_pkg is constant PD_IN_CNT_REG : std_logic_vector(4 downto 0) := 5x"12"; constant FIRE_REQ_CNT_REG : std_logic_vector(4 downto 0) := 5x"13"; constant WH_POS_ERR_2_CORR_REG : std_logic_vector(4 downto 0) := 5x"14"; - constant WH_POS_ERR_CURR_REG : std_logic_vector(4 downto 0) := 5x"15"; - constant WH_POS_ERR_MAX_REG : std_logic_vector(4 downto 0) := 5x"16"; - constant WH_DIV_RATIO_CORR_REG : std_logic_vector(4 downto 0) := 5x"17"; - constant WH_ROTATION_CNT_REG : std_logic_vector(4 downto 0) := 5x"18"; + constant WH_LOOP_FILT_COEFF : std_logic_vector(4 downto 0) := 5x"15"; + constant WH_POS_ERR_CURR_REG : std_logic_vector(4 downto 0) := 5x"16"; + constant WH_POS_ERR_MAX_REG : std_logic_vector(4 downto 0) := 5x"17"; + constant WH_DIV_RATIO_CORR_REG : std_logic_vector(4 downto 0) := 5x"18"; + constant WH_ROTATION_CNT_REG : std_logic_vector(4 downto 0) := 5x"19"; constant SEQ_CSR_OFFSET : std_logic_vector(9 downto 0) := 10x"20"; constant SEQ_RAM_OFFSET : std_logic_vector(9 downto 0) := 10x"200"; diff --git a/generic_ip/pulse_generator/2/src/test/ch_ctrl_cfg_tb.vhd b/generic_ip/pulse_generator/2/src/test/ch_ctrl_cfg_tb.vhd index 03216ed..7e11e90 100644 --- a/generic_ip/pulse_generator/2/src/test/ch_ctrl_cfg_tb.vhd +++ b/generic_ip/pulse_generator/2/src/test/ch_ctrl_cfg_tb.vhd @@ -6,7 +6,7 @@ -- Author : GMG -- Company : Global Inkjet Systems -- Created : 2019-05-17 --- Last update: 2019-06-07 +-- Last update: 2019-06-11 -- SVN ID : Git -- Standard : VHDL'2008 ------------------------------------------------------------------------------- @@ -68,9 +68,14 @@ architecture behav of ch_ctrl_cfg_tb is constant HALF_RNG_CROSS : std_logic_vector(31 downto 0) := X"7FFFFFFF"; constant FULL_RNG_CROSS : std_logic_vector(31 downto 0) := X"FFFFFFFF"; - constant INIT_ABS_POS : std_logic_vector(31 downto 0) := std_logic_vector(signed(HALF_RNG_CROSS)-50 -PD_OFFSET); - --constant INIT_ABS_POS : std_logic_vector(31 downto 0) := std_logic_vector(signed(X"7FFFFFFF")-421 -PD_OFFSET); - --constant INIT_ABS_POS : std_logic_vector(31 downto 0) := std_logic_vector(signed(X"FFFFFFFF")-421 -PD_OFFSET); + --constant INIT_ABS_POS : std_logic_vector(31 downto 0) := std_logic_vector(signed(HALF_RNG_CROSS)-50 -PD_OFFSET); + --constant INIT_ABS_POS : std_logic_vector(31 downto 0) := std_logic_vector(signed(HALF_RNG_CROSS)-421 -PD_OFFSET); + constant INIT_ABS_POS : std_logic_vector(31 downto 0) := std_logic_vector(signed(FULL_RNG_CROSS)-421 -PD_OFFSET); + --constant INIT_ABS_POS : std_logic_vector(31 downto 0) := (others => '0'); + + -- Correction Loop Filter constants + constant LOOP_FILT_DELTA : real := 1.0; + constant LOOP_FILT_TOT : real := 1.0; type wheel_error_t is array (0 to TOTAL_ROTATIONS-1) of integer; @@ -108,6 +113,7 @@ architecture behav of ch_ctrl_cfg_tb is 1, 1, 0, 0, -1, -1, -1, -1, 0, 0, -40, 0, 0, +80, others => 0); */ + /* signal wheel_error : wheel_error_t := ( 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, @@ -116,6 +122,16 @@ architecture behav of ch_ctrl_cfg_tb is -3, -3, -2, -2, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, 0, 0, -40, 0, 0, +80, others => +40); + */ + + signal wheel_error : wheel_error_t := ( + 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, + 3, 3, 2, 2, 1, 1, 0, 0, -1, -1, + -2, -2, -3, -3, -4, -4, -5, -5, -4, -4, + -3, -3, -2, -2, -1, -1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 1, 1, + 1, 1, 0, 0, -1, -1, -1, -1, 0, 0, -40, -40, -40, others => +40); ----------------------------------------------------------------------------- -- Signal declarations @@ -156,6 +172,7 @@ architecture behav of ch_ctrl_cfg_tb is signal wheel_pos : integer; signal pos_error : real; + signal pos_error_old : real; signal rotation_idx : natural; signal div_ratio_corr_r : real; signal div_ratio_r : real; @@ -520,14 +537,20 @@ begin -- architecture behav alias div_ratio_corr is <>; alias div_ratio_i is <>; + procedure apply_correction(pos_error: in real) is variable div_ratio_new : real; variable div_ratio_man : real; variable div_ratio_vec : std_logic_vector(33 downto 0); + variable tot_error : real; + variable delta_error : real; begin - -- read first the current value of the correction - --div_ratio_corr_r <= div_ratio_corr_r + (pos_error*(DIV_RATIO/WHEEL_CIRCUM)); - div_ratio_new := div_ratio_r + (pos_error*(1.0/real(IMG_LEN))); + -- compute the total and the delta loop error + tot_error := pos_error; + delta_error := pos_error-pos_error_old; + -- Compute the correction through a 2nd order loop filter + div_ratio_new := div_ratio_r + (delta_error*LOOP_FILT_DELTA+tot_error*LOOP_FILT_TOT)*(1.0/real(IMG_LEN)); + div_ratio_r <= div_ratio_new; div_ratio_corr_r <= div_ratio_new - DIV_RATIO; div_ratio_vec(33 downto 24) := std_logic_vector(to_unsigned(integer(floor(div_ratio_new)),10)); @@ -549,14 +572,29 @@ begin -- architecture behav variable div_ratio_man : real; variable div_ratio_vec : std_logic_vector(33 downto 0); variable pos_err_to_corr : integer := integer(round((1.0/real(IMG_LEN))*(2.0**32))); + variable tot_error : real; + variable delta_error : real; begin - -- read first the current value of the correction - div_ratio_new := div_ratio_r + (pos_error*real(pos_err_to_corr)/(2.0**32)); - div_ratio_corr_r <= div_ratio_new - DIV_RATIO; + -- compute the total and the delta loop error + tot_error := pos_error; + delta_error := pos_error-pos_error_old; + if(APPLY_EMUL_CORR = true) then + -- Compute the correction through a 2nd order loop filter + div_ratio_new := div_ratio_r + (delta_error*LOOP_FILT_DELTA+tot_error*LOOP_FILT_TOT)*real(pos_err_to_corr)/(2.0**32); + div_ratio_corr_r <= div_ratio_new - DIV_RATIO; + div_ratio_r <= div_ratio_new; + div_ratio_vec(33 downto 24) := std_logic_vector(to_unsigned(integer(floor(div_ratio_new)),10)); + div_ratio_man := (div_ratio_new - floor(div_ratio_new))*(2.0**24); + div_ratio_vec(23 downto 0) := std_logic_vector(to_unsigned(integer(floor(div_ratio_man)),24)); + else + div_ratio_new := real(to_integer(unsigned(div_ratio_i(33 downto 24)))) + real(to_integer(unsigned(div_ratio_i(23 downto 0))))/(2.0**24); + div_ratio_corr_r <= div_ratio_new - DIV_RATIO; + div_ratio_r <= div_ratio_new; + end if; + + --log("tot_error = " & to_string(tot_error) & ", delta_error = " & to_string(delta_error), VMEDIUM); + --log("tot_error-norm = " & to_string(256.0*tot_error) & ", delta_error-norm = " & to_string(256.0*delta_error), VMEDIUM); - div_ratio_vec(33 downto 24) := std_logic_vector(to_unsigned(integer(floor(div_ratio_new)),10)); - div_ratio_man := (div_ratio_new - floor(div_ratio_new))*(2.0**24); - div_ratio_vec(23 downto 0) := std_logic_vector(to_unsigned(integer(floor(div_ratio_man)),24)); -- force the correction into the UUT: if(APPLY_EMUL_CORR = true) then div_ratio_i <= force div_ratio_vec; @@ -569,6 +607,7 @@ begin -- architecture behav begin pos_error <= 0.0; + pos_error_old <= 0.0; rotation_idx <= 0; div_ratio_corr_r <= 0.0; @@ -578,23 +617,25 @@ begin -- architecture behav pos_error_avail := false; -- wait until the first PD; - wait until rising_edge(pd_in); + wait until rising_edge(ch_pd); -- measur the error, then apply correction while end_of_stimuli = false loop - wait until rising_edge(pd_in) or falling_edge(ch_mask); - if(rising_edge(pd_in) and falling_edge(ch_mask)) then - pos_error <= -DIV_RATIO; + wait until rising_edge(ch_pd) or falling_edge(ch_mask); + if(rising_edge(ch_pd) and falling_edge(ch_mask)) then + pos_error_old <= pos_error; + pos_error <= -DIV_RATIO; pos_error_avail := true; rotation_idx <= rotation_idx + 1; - elsif(rising_edge(pd_in)) then + elsif(rising_edge(ch_pd)) then if(measure_started = false) then start_pos := to_integer(signed(abs_position)); measure_started := true; else - pos_error <= real(to_integer(signed(abs_position)) - start_pos) - DIV_RATIO; - rotation_idx <= rotation_idx + 1; + pos_error_old <= pos_error; + pos_error <= real(to_integer(signed(abs_position)) - start_pos) - DIV_RATIO; + rotation_idx <= rotation_idx + 1; measure_started := false; pos_error_avail := true; end if; @@ -603,14 +644,17 @@ begin -- architecture behav start_pos := to_integer(signed(abs_position)); measure_started := true; else - pos_error <= -(real(to_integer(signed(abs_position)) - start_pos)) - DIV_RATIO; - rotation_idx <= rotation_idx + 1; + pos_error_old <= pos_error; + pos_error <= -(real(to_integer(signed(abs_position)) - start_pos)) - DIV_RATIO; + rotation_idx <= rotation_idx + 1; measure_started := false; pos_error_avail := true; end if; end if; if(pos_error_avail = true) then + -- Do not remove this wait statement: it makes sures previous signal + -- assignment are executed before computing the corrections wait until clk = '0' and clk'event; log("Rotation #" & to_string(rotation_idx) & ": pos_error = " & to_string(pos_error) & ".", VMEDIUM); if(APPLY_CORR_EN = true) then diff --git a/generic_ip/pulse_generator/2/src/test/wh_wheel_corr.m b/generic_ip/pulse_generator/2/src/test/wh_wheel_corr.m index eb5fc0a..6400ad2 100644 --- a/generic_ip/pulse_generator/2/src/test/wh_wheel_corr.m +++ b/generic_ip/pulse_generator/2/src/test/wh_wheel_corr.m @@ -22,17 +22,18 @@ clear all clc -head_pitch_um = 20.0 +%head_pitch_um = 20.0 +head_pitch_um = 4.23*5.0 encoder_res_um = 5.0 -image_pixel_lenght = 1000 +image_pixel_lenght = 100 div_ratio_nom = head_pitch_um/encoder_res_um wheel_circum = image_pixel_lenght*div_ratio_nom -total_rotations = 1000; +total_rotations = 100; % Define a error vector for wheel rotations -%{ + wheel_error_v = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, ... 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, ... 3, 3, 2, 2, 1, 1, 0, 0, -1, -1, ... @@ -43,14 +44,14 @@ wheel_error_v = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, ... N = total_rotations - length(wheel_error_v) -wheel_error_v = [wheel_error_v 80*ones(1,N)]; -%} - +wheel_error_v = [wheel_error_v 40*ones(1,N)]; +%{ wheel_error_v = zeros(1,total_rotations); %wheel_error_v(10) = 40 wheel_error_v(10:end) = 40; +%} %{ % Continuously changing @@ -71,9 +72,11 @@ wheel_error_v = (0.1*wheel_circum)*sin(2*pi*x/(total_rotations/k)); % Continuously changing +%{ error_rel_max = 0.1 x = 0:total_rotations-1; wheel_error_v = (0.1*wheel_circum)*sin(2*pi*x/total_rotations); +%} @@ -94,7 +97,7 @@ for n = 1:total_rotations % Basic Correction: FLL %div_ratio(n+1) = div_ratio_nom+pos_error(n+1)/image_pixel_lenght; % Correction Accumulation: - div_ratio(n+1) = div_ratio(n)+pos_error(n+1)/image_pixel_lenght; + %div_ratio(n+1) = div_ratio(n)+pos_error(n+1)/image_pixel_lenght; % PLL Correction: delta_error = pos_error(n+1)-pos_error(n); tot_error = pos_error(n+1);