otsdaq_prepmodernization  v2_05_02_indev
event_analysis.vhd
1 ----------------------------------------------------------------------------------
2 -- Company:
3 -- Engineer:
4 --
5 -- Create Date: 14:01:44 09/19/2017
6 -- Design Name:
7 -- Module Name: event_analysis - Behavioral
8 -- Project Name:
9 -- Target Devices:
10 -- Tool versions:
11 -- Description:
12 -- This module is where the code for the data analysis lives. The data is meant to come into the module as it comes out
13 -- of the data_send module. The current configuration is for two 64 bit header blocks, so the code delays two clocks
14 -- before starting analysis.
15 --
16 -- Dependencies:
17 --
18 -- Revision:
19 -- Revision 0.01 - File Created
20 -- Additional Comments:
21 --
22 ----------------------------------------------------------------------------------
23 library IEEE;
24 use IEEE.STD_LOGIC_1164.ALL;
25 use IEEE.NUMERIC_STD.ALL;
26 use IEEE.STD_LOGIC_UNSIGNED.ALL;
27 
28 -- Uncomment the following library declaration if using
29 -- arithmetic functions with Signed or Unsigned values
30 --use IEEE.NUMERIC_STD.ALL;
31 
32 -- Uncomment the following library declaration if instantiating
33 -- any Xilinx primitives in this code.
34 --library UNISIM;
35 --use UNISIM.VComponents.all;
36 
37 entity event_analysis is
38  Port ( --default signals
39  clk : in STD_LOGIC;
40  reset : in STD_LOGIC;
41  clock_enable : in STD_LOGIC;
42  --incoming data signals
43  data_in : in STD_LOGIC_VECTOR (63 downto 0);
44  data_in_we : in STD_LOGIC;
45  data_in_end : in STD_LOGIC;
46  --incoming footer signal. This footer will be sent with the veto footer and can be attached to any signal.
47  footer_in : STD_LOGIC_VECTOR (63 downto 0);
48  --user register signals
49  zero_cross_thresh_high : in STD_LOGIC_VECTOR (7 downto 0);
50  zero_cross_thresh_low : in STD_LOGIC_VECTOR (7 downto 0);
51  zero_cross_veto_thresh : in STD_LOGIC_VECTOR (7 downto 0);
52  --clear and force veto signals
53  clear_veto : in STD_LOGIC;
54  force_veto : in STD_LOGIC;
55  veto_en : in STD_LOGIC;
56  --outgoing data signals
57  data_out : out STD_LOGIC_VECTOR (63 downto 0);
58  data_out_we : out STD_LOGIC;
59  data_out_end : out STD_LOGIC;
60  --outgoing analysis signals
61  zero_cross_count : out STD_LOGIC_VECTOR (7 downto 0);
62  --this signal is speciffic to the g-2 application and will veto the kicker signal in the event of a spark to
63  --ensure that no damage is done to the kicker and surrounding components.
64  veto : out STD_LOGIC;
65  --resets for the clear_veto and force_veto latches
66  reset_clear_veto : out STD_LOGIC;
67  reset_force_veto : out STD_LOGIC);
68 end event_analysis;
69 
70 architecture Behavioral of event_analysis is
71  --each individual data sample gets its own signal for easier processing
72  signal sampleOne : unsigned(7 downto 0);
73  signal sampleTwo : unsigned(7 downto 0);
74  signal sampleThree : unsigned(7 downto 0);
75  signal sampleFour : unsigned(7 downto 0);
76  signal sampleFive : unsigned(7 downto 0);
77  signal sampleSix : unsigned(7 downto 0);
78  signal sampleSeven : unsigned(7 downto 0);
79  signal sampleEight : unsigned(7 downto 0);
80  --repeated signals
81  signal userZeroCrossThreshHigh : unsigned(7 downto 0);
82  signal userZeroCrossThreshLow : unsigned(7 downto 0);
83  signal userZeroCrossVetoThresh : unsigned(7 downto 0);
84  signal dataOutEnd : std_logic;
85  signal resetClearVeto, resetForceVeto : std_logic;
86  signal del_clk_en : std_logic;
87  --module flags
88  signal busy : std_logic;
89  signal headerDelayOne, headerDelayTwo : std_logic;
90  signal analysisRunning : std_logic;
91  signal prepareEnd : std_logic;
92  signal sendFooter,sendPacketEnd : std_logic;
93  signal finish : std_logic;
94  --signal state
95  signal lastSigLow, lastSigHigh : std_logic;
96  signal zeroCrossCount : unsigned(7 downto 0);
97  signal vetoed : std_logic;
98  --footer signal
99  signal internalFooter : std_logic_vector(63 downto 0);
100 begin
101  --assign data signals
102  sampleOne <= unsigned(data_in(7 downto 0));
103  sampleTwo <= unsigned(data_in(15 downto 8));
104  sampleThree <= unsigned(data_in(23 downto 16));
105  sampleFour <= unsigned(data_in(31 downto 24));
106  sampleFive <= unsigned(data_in(39 downto 32));
107  sampleSix <= unsigned(data_in(47 downto 40));
108  sampleSeven <= unsigned(data_in(55 downto 48));
109  sampleEight <= unsigned(data_in(63 downto 56));
110  --assign repeated signals
111  userZeroCrossThreshHigh <= unsigned(zero_cross_thresh_high);
112  userZeroCrossThreshLow <= unsigned(zero_cross_thresh_low);
113  userZeroCrossVetoThresh <= unsigned(zero_cross_veto_thresh(7 downto 0));
114  zero_cross_count <= std_logic_vector(zeroCrossCount);
115  veto <= vetoed;
116  data_out_end <= dataOutEnd;
117  reset_clear_veto <= resetClearVeto;
118  reset_force_veto <= resetForceVeto;
119  --assign footer signals
120  internalFooter(7 downto 0) <= std_logic_vector(zeroCrossCount(7 downto 0));
121  internalFooter(8) <= vetoed;
122  internalFooter(9) <= veto_en;
123  internalFooter(19 downto 12) <= zero_cross_thresh_high;
124  internalFooter(27 downto 20) <= zero_cross_thresh_low;
125  internalFooter(35 downto 28) <= zero_cross_veto_thresh;
126 
127  process(clk) begin
128  --check for reset condition
129  if(rising_edge(clk)) then
130 
131  --====================
132  --====================
133  --Reset pulsed signals
134  data_out_we <= '0';
135  sendFooter <= '0';
136  dataOutEnd <= '0';
137  resetClearVeto <= '0';
138  resetForceVeto <= '0';
139  vetoed <= '0';
140  sendPacketEnd <= '0';
141 
142 
143  --====================
144  --====================
145  if(data_in_we = '1') then -- reset pulsed-clock-en data
146  headerDelayOne <= '0';
147  end if;
148 
149 
150  --====================
151  --====================
152  -- primaray path
153  if(reset = '1') then --reset can still happen even if data_in_we is low.
154  busy <= '0';
155  analysisRunning <= '0';
156  elsif(data_in_end = '1') then -- Indicates end of header/payload data. So send external footer!
157  data_out_we <= '1';
158  data_out <= footer_in; --we need one clock to figure out if we need to veto. The header signals coming from anything else in the firmware application can be sent at this time.
159 
160  sendFooter <= '1'; --send internal footer next clock
161 
162  --Check if we will veto the signal and take approperate action
163  if(zeroCrossCount > userZeroCrossVetoThresh and veto_en = '1') then
164  vetoed <= '1';
165  end if;
166  elsif(sendFooter = '1') then
167  data_out_we <= '1';
168  data_out <= internalFooter; --note: includes veto info setup in previous clock
169 
170  sendPacketEnd <= '1'; -- got to last state, note: force end must happen after last qw in packet
171 
172  elsif(sendPacketEnd = '1') then
173  dataOutEnd <= '1'; --DONE!!! end packet
174 
175  --DONE!!!
176  --wrap up things
177  busy <= '0';
178  analysisRunning <= '0';
179  zeroCrossCount <= (others => '0');
180 
181  elsif(data_in_we = '1') then
182 
183  data_out_we <= '1';
184  data_out <= data_in;
185 
186  --The data currently on the pins is still valid and needs to be compared. We won't give the final
187  -- count until the event is actually over.
188 
189  if(busy = '0') then -- first data! = data for header 1
190  headerDelayOne <= '1';
191  busy <= '1';
192  elsif(headerDelayOne = '1') then --data for header 2
193  --headerDelayTwo <= '1';
194  analysisRunning <= '1';
195  lastSigHigh <= '0';
196  lastSigLow <= '0';
197  zeroCrossCount <= (others => '0');
198  elsif(analysisRunning = '1') then --We have actual data! start the analysis process
199  --If last time was not high and this time is, incriment the counter
200  if(lastSigHigh = '0') then
201  if(sampleOne > userZeroCrossThreshHigh or sampleTwo > userZeroCrossThreshHigh or sampleThree > userZeroCrossThreshHigh or sampleFour > userZeroCrossThreshHigh
202  or sampleFive > userZeroCrossThreshHigh or sampleSix > userZeroCrossThreshHigh or sampleSeven > userZeroCrossThreshHigh or sampleEight > userZeroCrossThreshHigh) then
203  lastSigHigh <= '1';
204  zeroCrossCount <= zeroCrossCount + 1;
205  end if;
206  end if;
207 
208  --If the last signal set was not low and this time is low, incriment the counter
209  if(lastSigLow = '0') then
210  if(sampleOne < userZeroCrossThreshLow or sampleTwo < userZeroCrossThreshLow or sampleThree < userZeroCrossThreshLow or sampleFour < userZeroCrossThreshLow or sampleFive < userZeroCrossThreshLow
211  or sampleSix < userZeroCrossThreshLow or sampleSeven < userZeroCrossThreshLow or sampleEight < userZeroCrossThreshLow) then
212  lastSigLow <= '1';
213  zeroCrossCount <= zeroCrossCount + 1;
214  end if;
215  end if;
216 
217 
218  --Reset the high signal if we are no longer above threshold
219  if(lastSigHigh = '1' and sampleOne < userZeroCrossThreshHigh and sampleTwo < userZeroCrossThreshHigh and sampleThree < userZeroCrossThreshHigh and sampleFour < userZeroCrossThreshHigh
220  and sampleFive < userZeroCrossThreshHigh and sampleSix < userZeroCrossThreshHigh and sampleSeven < userZeroCrossThreshHigh and sampleEight < userZeroCrossThreshHigh) then
221  lastSigHigh <= '0';
222  end if;
223 
224  --Reset the Low signal if we are no longer below threshold
225  if(lastSigLow = '1' and sampleOne > userZeroCrossThreshLow and sampleTwo > userZeroCrossThreshLow and sampleThree > userZeroCrossThreshLow and sampleFour > userZeroCrossThreshLow and sampleFive > userZeroCrossThreshLow
226  and sampleSix > userZeroCrossThreshLow and sampleSeven > userZeroCrossThreshLow and sampleEight > userZeroCrossThreshLow) then
227  lastSigLow <= '0';
228  end if;
229  end if;
230 
231  end if; -- end elsif(data_in_we = '1') then
232 
233  --====================
234  --====================
235  ---user controlled signals:
236 
237  --Clear veto signal
238  if(force_veto = '1') then --Force veto signal
239  vetoed <= '1';
240  resetForceVeto <= '0'; --never reset flipflop
241  elsif(clear_veto = '1') then
242  vetoed <= '0';
243  resetClearVeto <= '0'; --never reset flipflop
244  end if;
245 
246 
247  end if;
248 
249  end process;
250 end Behavioral;
251 
252