Belle II KLM Scint Firmware  1
CalculateROI.vhd
1 library IEEE;
2 use IEEE.STD_LOGIC_1164.ALL;
3 use IEEE.NUMERIC_STD.ALL;
4 use IEEE.STD_LOGIC_MISC.ALL;
5 use IEEE.STD_LOGIC_UNSIGNED.ALL;
6 Library work;
7 use work.klm_scint_pkg.all;
8 
21 
22 entity CalculateROI is
23  generic (
24  ARR_DEPTH_g : integer;
25  VEC_WIDTH_g : integer
26  );
27  port (
28  clk : in std_logic := '0';
29  ena : in std_logic := '0';
30  -- busy : out std_logic := '0';
31  vec_arr : in slv9(ARR_DEPTH_g - 1 downto 0);
32  arr_mask : in std_logic_vector(ARR_DEPTH_g - 1 downto 0);
33  first_vec : out std_logic_vector(VEC_WIDTH_g - 1 downto 0);
34  last_vec : out std_logic_vector(VEC_WIDTH_g - 1 downto 0)
35  );
36 end CalculateROI;
37 
38 
39 architecture Behavioral of CalculateROI is
40 
41 
42  signal i_first_vec : std_logic_vector(VEC_WIDTH_g - 1 downto 0) := (others=>'0');
43  signal i_last_vec : std_logic_vector(VEC_WIDTH_g - 1 downto 0) := (others=>'0');
44 
45  signal count : integer range 0 to ARR_DEPTH_g - 1;
46 
47  type calculate_ROI_state_machine is (
48  IDLE,
49  COMPARING
50  );
51  signal ROI_state : calculate_ROI_state_machine := IDLE;
52 
53  signal i_ena : std_logic := '1';
54 
55 ----------------------------------------
56 begin
57 
58  process(clk)
59  begin
60  if rising_edge(clk) then
61  i_ena <= ena;
62  first_vec <= i_first_vec;
63  last_vec <= i_last_vec;
64  end if;
65  end process;
66 
67 
68  process(clk)
69  begin
70  if rising_edge(clk) then
71 
72  case ROI_state is
73 
74  when IDLE =>
75  if i_ena = '0' and ena = '1' then
76  for i in 0 to ARR_DEPTH_g - 1 loop
77  if arr_mask(i) = '1' then
78  i_first_vec <= vec_arr(i);
79  i_last_vec <= vec_arr(i);
80  count <= i;
81  exit;
82  end if;
83  end loop;
84  -- busy <= '1';
85  ROI_state <= COMPARING;
86  else
87  -- busy <= '0';
88  count <= 0;
89  ROI_state <= IDLE;
90  end if;
91 
92  when COMPARING =>
93  if arr_mask(count) = '1' then
94 
95  -- find earliest
96  if vec_arr(count) <= i_first_vec then
97  if i_first_vec - vec_arr(count) > 2**(VEC_WIDTH_g - 1) then --rollover detected
98  i_first_vec <= i_first_vec;
99  else
100  i_first_vec <= vec_arr(count);
101  end if;
102  else
103  if vec_arr(count) - i_first_vec > 2**(VEC_WIDTH_g - 1) then --rollover detected
104  i_first_vec <= vec_arr(count);
105  else
106  i_first_vec <= i_first_vec;
107  end if;
108  end if;
109 
110  -- find latest
111  if vec_arr(count) >= i_last_vec then
112  if vec_arr(count) - i_last_vec > 2**(VEC_WIDTH_g - 1) then --rollover detected
113  i_last_vec <= i_last_vec;
114  else
115  i_last_vec <= vec_arr(count);
116  end if;
117  else
118  if i_last_vec - vec_arr(count) > 2**(VEC_WIDTH_g - 1) then --rollover detected
119  i_last_vec <= vec_arr(count);
120  else
121  i_last_vec <= i_last_vec;
122  end if;
123  end if;
124  end if;
125 
126  if count < ARR_DEPTH_g - 1 then
127  count <= count + 1;
128  ROI_state <= COMPARING;
129  else
130  count <= 0;
131  ROI_state <= IDLE;
132  end if;
133 
134  end case;
135  end if;
136  end process;
137 end Behavioral;