| 1 |
2 |
panda_emc |
-----------------------------------------------------------------------------------------------
|
| 2 |
|
|
--
|
| 3 |
|
|
-- Copyright (C) 2011 Peter Lemmens, PANDA collaboration
|
| 4 |
|
|
-- p.j.j.lemmens@rug.nl
|
| 5 |
|
|
-- http://www-panda.gsi.de
|
| 6 |
|
|
--
|
| 7 |
|
|
-- As a reference, please use:
|
| 8 |
|
|
-- E. Guliyev, M. Kavatsyuk, P.J.J. Lemmens, G. Tambave, H. Loehner,
|
| 9 |
|
|
-- "VHDL Implementation of Feature-Extraction Algorithm for the PANDA Electromagnetic Calorimeter"
|
| 10 |
|
|
-- Nuclear Inst. and Methods in Physics Research, A ....
|
| 11 |
|
|
--
|
| 12 |
|
|
--
|
| 13 |
|
|
-- This program is free software; you can redistribute it and/or modify
|
| 14 |
|
|
-- it under the terms of the GNU Lesser General Public License as published by
|
| 15 |
|
|
-- the Free Software Foundation; either version 3 of the License, or
|
| 16 |
|
|
-- (at your option) any later version.
|
| 17 |
|
|
--
|
| 18 |
|
|
-- This program is distributed in the hope that it will be useful,
|
| 19 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 20 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 21 |
|
|
-- GNU Lesser General Public License for more details.
|
| 22 |
|
|
--
|
| 23 |
|
|
-- You should have received a copy of the GNU General Public License
|
| 24 |
|
|
-- along with this program; if not, write to the Free Software
|
| 25 |
|
|
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
|
| 26 |
|
|
--
|
| 27 |
|
|
-----------------------------------------------------------------------------------------------
|
| 28 |
|
|
-----------------------------------------------------------------------------------------------
|
| 29 |
|
|
-- Company : KVI (Kernfysisch Versneller Instituut -- Groningen, The Netherlands
|
| 30 |
|
|
-- Author : P.J.J. Lemmens
|
| 31 |
|
|
-- Design Name : Feature Extraction
|
| 32 |
|
|
-- Module Name : successive_interp.vhd
|
| 33 |
|
|
-- Description : Linear interpolation through the 'successive approximation' method. The actual
|
| 34 |
|
|
-- zero crossing of the signal will practically allways be located between samples.
|
| 35 |
|
|
-- The assumption is that the 'sub-sample time ratio' equals the ratio between
|
| 36 |
|
|
-- the absolute value of the older sample (negative) and the value of the
|
| 37 |
|
|
-- newer sample (positive). To maintain precision the choice was made to
|
| 38 |
|
|
-- multiply the sample values by 2 at each iteration, instead of dividing the
|
| 39 |
|
|
-- difference by 2. I theory you can calculate as many bits as you like; it's
|
| 40 |
|
|
-- up to you to decide what is meaningfull
|
| 41 |
|
|
-- The number of iterations also determines (to a large extent) the latency of
|
| 42 |
|
|
-- the processing of an event (and thus the dead-time of the system).
|
| 43 |
|
|
-- Room for improvement !
|
| 44 |
|
|
-----------------------------------------------------------------------------------------------
|
| 45 |
|
|
library IEEE;
|
| 46 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
| 47 |
|
|
use IEEE.STD_LOGIC_ARITH.ALL;
|
| 48 |
|
|
use IEEE.STD_LOGIC_SIGNED.ALL;
|
| 49 |
|
|
|
| 50 |
|
|
entity successive_interp is
|
| 51 |
|
|
generic( ITERATIONS : natural := 1);
|
| 52 |
|
|
Port ( rst : in STD_LOGIC;
|
| 53 |
|
|
clk : in STD_LOGIC;
|
| 54 |
|
|
enable : in STD_LOGIC := '1';
|
| 55 |
|
|
trigger : in STD_LOGIC;
|
| 56 |
|
|
samplenr_in : in STD_LOGIC_VECTOR;
|
| 57 |
|
|
base_in : in STD_LOGIC_VECTOR;
|
| 58 |
|
|
diff_in : in STD_LOGIC_VECTOR;
|
| 59 |
|
|
output_valid : out STD_LOGIC;
|
| 60 |
|
|
fraction_out : out STD_LOGIC_VECTOR;
|
| 61 |
|
|
eventnr_out : out STD_LOGIC_VECTOR
|
| 62 |
|
|
);
|
| 63 |
|
|
end successive_interp;
|
| 64 |
|
|
|
| 65 |
|
|
architecture Behavioral of successive_interp is
|
| 66 |
|
|
|
| 67 |
|
|
constant WIDTH : natural := diff_in'length;
|
| 68 |
|
|
constant E_WIDTH : natural := WIDTH + ITERATIONS;
|
| 69 |
|
|
constant LEADING_BITS : STD_LOGIC_VECTOR(ITERATIONS - 2 downto 0) := ('1', others => '0'); -- always <0 therefore signbit ='1'
|
| 70 |
|
|
constant FRACTION_SIZE : natural := ITERATIONS;
|
| 71 |
|
|
|
| 72 |
|
|
--------------------------------------------------------------------------------------------------
|
| 73 |
|
|
signal rst_S : std_logic := '1';
|
| 74 |
|
|
signal clk_S : std_logic := '0';
|
| 75 |
|
|
signal enable_S : std_logic := '0';
|
| 76 |
|
|
signal trigger_S : std_logic := '0';
|
| 77 |
|
|
signal sample_nr_S : std_logic_vector (samplenr_in'high downto 0) := (others => '0');
|
| 78 |
|
|
signal base_in_S : std_logic_vector (WIDTH -1 downto 0) := (others => '0');
|
| 79 |
|
|
signal diff_in_S : std_logic_vector (WIDTH -1 downto 0) := (others => '0');
|
| 80 |
|
|
signal fraction_out_S : std_logic_vector (ITERATIONS - 1 downto 0) := (others => '0'); -- holds index of last negative data
|
| 81 |
|
|
|
| 82 |
|
|
signal cstate_count_S : std_logic_vector(4 downto 0) := (others => '0'); -- extra states needed to wait for input signals
|
| 83 |
|
|
signal output_valid_S : std_logic := '0';
|
| 84 |
|
|
signal intermediate_S : std_logic_vector(WIDTH + ITERATIONS - 1 downto 0) := (others => '0');
|
| 85 |
|
|
signal stepsize_S : std_logic_vector(WIDTH - 1 downto 0) := (others => '0');
|
| 86 |
|
|
|
| 87 |
|
|
--------------------------------------------------------------------------------------------------
|
| 88 |
|
|
|
| 89 |
|
|
begin
|
| 90 |
|
|
|
| 91 |
|
|
rst_S <= rst;
|
| 92 |
|
|
clk_S <= clk;
|
| 93 |
|
|
enable_S <= enable;
|
| 94 |
|
|
trigger_S <= trigger;
|
| 95 |
|
|
base_in_S <= base_in;
|
| 96 |
|
|
diff_in_S <= diff_in;
|
| 97 |
|
|
fraction_out <= fraction_out_S(FRACTION_SIZE - 1 downto 0);
|
| 98 |
|
|
eventnr_out <= sample_nr_S;
|
| 99 |
|
|
output_valid <= output_valid_S;
|
| 100 |
|
|
|
| 101 |
|
|
interp_fsm : process(clk_S)
|
| 102 |
|
|
|
| 103 |
|
|
begin
|
| 104 |
|
|
if rising_edge(clk_S) then
|
| 105 |
|
|
if (rst_S = '1') then
|
| 106 |
|
|
cstate_count_S <= (others => '0');
|
| 107 |
|
|
elsif (cstate_count_S > 0) then
|
| 108 |
|
|
cstate_count_S <= cstate_count_S - 1;
|
| 109 |
|
|
elsif (trigger_S = '1' and enable_S = '1') then
|
| 110 |
|
|
cstate_count_S <= conv_std_logic_vector(ITERATIONS + 1, cstate_count_S'length); -- one cycle extra to get the delayed 'difference'
|
| 111 |
|
|
end if;
|
| 112 |
|
|
|
| 113 |
|
|
if (cstate_count_S = ITERATIONS + 1) then
|
| 114 |
|
|
intermediate_S <= (others => '0');
|
| 115 |
|
|
sample_nr_S <= samplenr_in - 4; -- remember the sample at the base of the window - 1.5*MovingWindowPower
|
| 116 |
|
|
stepsize_S <= diff_in_S;
|
| 117 |
|
|
intermediate_S <= conv_std_logic_vector(conv_integer(signed(base_in_S) & '0'), intermediate_S'length);
|
| 118 |
|
|
output_valid_S <= '0';
|
| 119 |
|
|
elsif ((cstate_count_S <= ITERATIONS) and cstate_count_S > 1) then
|
| 120 |
|
|
if ((signed(intermediate_S) + signed(stepsize_S)) <= 0) then
|
| 121 |
|
|
intermediate_S <= conv_std_logic_vector(conv_integer(signed((intermediate_S(intermediate_S'high - 1 downto 0) + stepsize_S) & '0')), intermediate_S'length);
|
| 122 |
|
|
fraction_out_S <= fraction_out_S(fraction_out_S'high - 1 downto 0) & '1';
|
| 123 |
|
|
else
|
| 124 |
|
|
intermediate_S <= conv_std_logic_vector(conv_integer(signed((intermediate_S(intermediate_S'high - 1 downto 0)) & '0')), intermediate_S'length);
|
| 125 |
|
|
fraction_out_S <= fraction_out_S(fraction_out_S'high - 1 downto 0) & '0';
|
| 126 |
|
|
end if;
|
| 127 |
|
|
elsif (cstate_count_S = 1) then
|
| 128 |
|
|
output_valid_S <= '1';
|
| 129 |
|
|
if ((signed(intermediate_S) + signed(stepsize_S)) <= 0) then
|
| 130 |
|
|
intermediate_S <= conv_std_logic_vector(conv_integer(signed((intermediate_S(intermediate_S'high - 1 downto 0) + stepsize_S) & '0')), intermediate_S'length);
|
| 131 |
|
|
fraction_out_S <= fraction_out_S(fraction_out_S'high - 1 downto 0) & '1';
|
| 132 |
|
|
else
|
| 133 |
|
|
intermediate_S <= conv_std_logic_vector(conv_integer(signed((intermediate_S(intermediate_S'high - 1 downto 0)) & '0')), intermediate_S'length);
|
| 134 |
|
|
fraction_out_S <= fraction_out_S(fraction_out_S'high - 1 downto 0) & '0';
|
| 135 |
|
|
end if;
|
| 136 |
|
|
elsif (cstate_count_S = 0) then
|
| 137 |
|
|
stepsize_S <= (others => '0');
|
| 138 |
|
|
intermediate_S <= (others => '0');
|
| 139 |
|
|
fraction_out_S <= (others => '0');
|
| 140 |
|
|
output_valid_S <= '0';
|
| 141 |
|
|
end if;
|
| 142 |
|
|
end if;
|
| 143 |
|
|
end process;
|
| 144 |
|
|
|
| 145 |
|
|
end Behavioral;
|