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;
34 baseline_g : (11 downto 0);
35 WAVE_MEM_DEPTH : := 4;
36 B2TT_RST_PPLN_STGS : := 5;
37 STAT_REG_PPLN_STGS : := 5;
38 FIFO_RST_PPLN_STGS : := 6;
39 SPSBlockRAM_READ_LATENCY : := 6;
-- from pipelined output selected in ISE core generator 51 N_readout_samples : in (7 downto 0);
52 LE_time_thresh : in (11 downto 0) := "000000110010";
-- 50 56 debug_we : out := '0';
57 debug_wave : out (11 downto 0) := (others=>'0');
61 ch_mask : in slv15(4 downto 0) := (others=>"000000000000000");
65 fifo_asic_sel : out (4 downto 0) := (others=>'0');
66 fifo_chan_sel : out (3 downto 0) := (others=>'0');
67 fifo_rd_ena : out := '0';
68 fifo_dout : in (23 downto 0) := (others=>'0');
69 fifo_drdy : in := '0';
70 fifos_empty : in := '0';
74 rx_features_ena : out := '0';
75 last_hit : out := '0';
76 peak : out (11 downto 0) := (others => '0');
77 le_time : out (13 downto 0) := (others => '0');
78 daq_chan : out (6 downto 0) := (others => '0');
82 avg_peds_busy : out := '0';
83 wr_peds2sram_ena : out := '0';
84 wr_peds2sram_ack : in := '0';
85 even_ped : out (11 downto 0) := (others => '0');
86 odd_ped : out (11 downto 0) := (others => '0');
87 sram_asic_addr : out (2 downto 0) := (others => '0');
88 sram_chan_addr : out (3 downto 0) := (others => '0');
89 sram_samp_addr : out (3 downto 0) := (others => '0');
90 fe_dbg : out (1 downto 0) := "00";
94 SPS_hist_rd_addr : in (9 downto 0) := (others => '0');
95 SPS_hist_rd_data : out (15 downto 0) := (others => '0') 105 -- SYNCHRONOUS SIGNALS BY PROCESS WHICH DRIVES THEM 111 signal fifo_rst_r : (FIFO_RST_PPLN_STGS-1 downto 0) := (others=>'0');
115 signal wave_drdy : := '0';
116 signal wave_div_NBA : (11 downto 0) := (others=>'0');
117 signal ped_data : (11 downto 0) := (others=>'0');
121 signal avg_peds_ena_i : (1 downto 0) := (others => '0');
128 signal asics_hit : (4 downto 0) := (others=>'0');
129 signal ch_mask_q2 : slv15(4 downto 0) := (others=>"000000000000000");
133 LOOPING_JOBS, WAITING_FIFO_READ_LAG, LOOPING_SAMPLES, CHECK_DONE);
135 signal write_debug_wave_header : := '0';
136 signal write_debug_wave_footer : := '0';
137 signal fill_SPS_hist_ena : := '0';
138 signal ch_mask_q1 : slv15(4 downto 0) := (others=>"000000000000000");
139 signal tx_features_ena : := '0';
140 signal stream_N_ro_samps_ena : := '0';
141 signal peak_i : (11 downto 0) := (others => '0');
142 signal le_charge : (13 downto 0) := (others => '0');
143 --signal charge_p : std_logic_vector(19 downto 0) := (others => '0'); 144 --signal charge_n : std_logic_vector(19 downto 0) := (others => '0'); 145 signal le_time_i : (13 downto 0) := (others => '0');
146 signal last_hit_i : := '0';
147 signal cur_asic : range 0 to 4 := 0;
148 signal feat_ext_busy : := '0';
149 signal feat_ext_asic_mask : (4 downto 0) := "00001";
150 signal feat_ext_chn_sel : (3 downto 0) := (others=>'0');
151 signal daq_chan_i : (6 downto 0) := (others => '0');
152 signal win_samp_start_i : slv14(4 downto 0) := (others=>(others=>'0'));
156 WRITE_ASIC_AND_WINDOW, WRITING, WRITE_LAST_WORD, WRITE_EOF, STOP );
162 signal tx_features_busy : := '0';
167 --signal SPS_hist_address : std_logic_vector(13 downto 0) := (others=>'0'); 168 signal SPS_hist_address : (9 downto 0) := (others=>'0');
169 signal SPS_hist_wr_ena : (0 downto 0) := "0";
-- required type (due to ISE core gen) 170 signal SPS_hist_wr_data : (15 downto 0) := (others=>'0');
171 signal i_SPS_hist_rd_data : slv16(STAT_REG_PPLN_STGS - 1 downto 0) := (others=>(others=>'0'));
175 AVERAGE_EVEN_SAMPLE, AVERAGE_ODD_SAMPLE, WAIT_PED_WRITER);
177 signal avg_peds_rd_ena : := '0';
178 signal avg_peds_chn_sel : (3 downto 0) := (others => '0');
179 signal avg_peds_asic_mask : (4 downto 0) := "00001";
180 signal sram_asic_addr_i : (2 downto 0) := (others => '0');
181 signal sram_samp_addr_i : (3 downto 0) := (others => '0');
183 -- ASYNCHRONOUSLY DRIVEN SIGNALS 184 --signal charge_s : std_logic_vector(19 downto 0) := (others => '0'); 185 -- SIGNALS DRIVEN BY INSTANTIATED ENTITIES 192 --------------------- ASYNCHRONOUS LOGIC --------------------------------------------- 194 busy <= feat_ext_busy or tx_features_busy;
195 sram_asic_addr <= sram_asic_addr_i;
196 sram_chan_addr <= avg_peds_chn_sel;
197 sram_samp_addr <= sram_samp_addr_i;
198 -- charge_s <= charge_n - charge_p; 203 --------------------- SYNCHRONOUS LOGIC --------------------------------------------- 204 b2tt_rst_ppln:
process(clk, rst)
206 if rising_edge(clk) then 215 if rising_edge(clk) then 222 ped_sub:
process(clk,
ped_sub_ena, fifo_dout, fifo_drdy)
224 if rising_edge(clk) then 225 wave_drdy <= fifo_drdy;
226 wave_div_NBA <= fifo_dout(N_BITS_AVG_g + 11 downto N_BITS_AVG_g);
-- divide by 2**NBA 227 ped_data <= fifo_dout(23 downto 12);
-- for debug 229 wave_data <= baseline_g + fifo_dout(11 downto 0) - fifo_dout(23 downto 12);
238 proc_fsm_ena_sel:
process (clk, ena,
feat_ext_ena, avg_peds_ena_i, measure_peds,
240 avg_peds_asic_mask, feat_ext_chn_sel,
243 if rising_edge(clk) then 245 avg_peds_ena_i(1) <= avg_peds_ena_i(0);
246 if measure_peds = '1' then 248 fifo_rd_ena <= avg_peds_rd_ena;
249 fifo_chan_sel <= avg_peds_chn_sel;
250 fifo_asic_sel <= avg_peds_asic_mask;
254 fifo_chan_sel <= feat_ext_chn_sel;
255 fifo_asic_sel <= feat_ext_asic_mask;
258 end process proc_fsm_ena_sel;
262 read_wave:
process(clk, stream_N_ro_samps_ena, N_readout_samples)
263 variable count : range 0 to 255 := 0;
265 if rising_edge(clk) then 266 if (stream_N_ro_samps_ena = '1' or count > 0) and count <= N_readout_samples then 274 end process read_wave;
278 hit_chs_lgc:
process(clk, ch_mask_q1)
280 if rising_edge(clk) then 282 or_reduce(ch_mask_q1(1)) or 283 or_reduce(ch_mask_q1(2)) or 284 or_reduce(ch_mask_q1(3)) or 285 or_reduce(ch_mask_q1(4));
287 asics_hit(i) <= or_reduce(ch_mask_q1(i));
289 ch_mask_q2 <= ch_mask_q1;
297 wave_data, wave_drdy, win_samp_start_i, cur_asic,
298 peak_i, le_charge, N_readout_samples, --use_ftsw_trig, -- use_loop_trig,
300 variable int_count : range 0 to 4 := 0;
301 -- variable ch_mask_q1 : slv15(4 downto 0) := (others=>"000000000000000"); 302 variable count : (7 downto 0) := (others => '0');
303 variable le_time_found : := '0';
304 variable wave_mem : slv12(WAVE_MEM_DEPTH - 1 downto 0) := (others => "000000000000");
306 if rising_edge(clk) then 308 -- feat_ext_chn_sel <= "1111"; 309 ch_mask_q1 <= (others=>(others=>'0'));
310 feat_ext_state <= CHECK_FIFOS_RDY;
312 Case feat_ext_state is 314 When CHECK_FIFOS_RDY => 316 if fifos_empty = '1' then 318 feat_ext_state <= IDLE;
321 feat_ext_state <= CHECK_FIFOS_RDY;
327 feat_ext_chn_sel <= "0000";
328 -- feat_ext_asic_mask <= "00001"; 329 stream_N_ro_samps_ena <= '0';
330 fill_SPS_hist_ena <= '0';
331 -- write_debug_wave_ena <= '0'; 332 tx_features_ena <= '0';
333 -- null_hit_i <= '1'; 334 peak_i <= (others=>'0');
335 le_charge <= (others=>'0');
336 -- charge_p <= (others=>'0'); 337 -- charge_n <= (others=>'0'); 338 le_time_i <= (others=>'0');
342 ch_mask_q1 <= ch_mask;
344 feat_ext_busy <= '1';
345 feat_ext_state <= WAIT_VALID;
347 feat_ext_busy <= '0';
348 feat_ext_state <= IDLE;
352 if int_count < 2 then -- MAGIC NUMBER -- FIXME 353 int_count := int_count + 1;
354 feat_ext_state <= WAIT_VALID;
357 feat_ext_state <= CHECK_VALID;
361 if any_ch_hit = '0' then -- all hits on bus (single or multi) failed threshold check, send null packet 363 -- null_hit_i <= '1'; 364 tx_features_ena <= '1';
365 feat_ext_state <= CHECK_DONE;
368 -- null_hit_i <= '0'; 369 feat_ext_state <= LOOPING_JOBS;
374 write_debug_wave_footer <= '0';
375 fill_SPS_hist_ena <= '0';
377 count := (others => '0');
378 le_time_found := '0';
379 tx_features_ena <= '0';
380 if int_count = 4 then-- increment asic counter 383 int_count := int_count + 1;
385 feat_ext_asic_mask <= (others => '0');
-- 5 bit asic mask 386 feat_ext_asic_mask(int_count) <= '1';
387 cur_asic <= int_count;
388 priority_encoder:
for i
in 0 to 14 loop 389 if (ch_mask_q2(int_count)(i)) = '1' then 390 feat_ext_chn_sel <= (to_unsigned(i, 4));
391 daq_chan_i <= (to_unsigned( int_count*15 + i , 7));
392 ch_mask_q1(int_count)(i) <= '0';
393 exit priority_encoder;
396 if asics_hit(int_count) = '1' then 397 stream_N_ro_samps_ena <= '1';
398 feat_ext_state <= WAITING_FIFO_READ_LAG;
400 stream_N_ro_samps_ena <= '0';
401 feat_ext_state <= LOOPING_JOBS;
405 When WAITING_FIFO_READ_LAG => -- about 8 clocks 406 write_debug_wave_header <= '1';
-- x"50F", win, chan -- 8 clocks here covers case when still writing EOF 407 stream_N_ro_samps_ena <= '0';
408 if wave_drdy = '0' then 409 feat_ext_state <= WAITING_FIFO_READ_LAG;
411 le_time_i <= (win_samp_start_i(cur_asic));
--(others => '0'); 412 peak_i <= (others => '0');
413 le_charge <= (others => '0');
414 -- charge_p <= (others => '0'); 415 -- charge_n <= (others => '0'); 418 feat_ext_state <= LOOPING_SAMPLES;
422 When LOOPING_SAMPLES => 423 write_debug_wave_header <= '0';
425 -- time (ns) count-2 count-1 count 426 -- <-----------------------------------------------|---> 427 -- . . . wave_mem(1) wave_mem(0) wave_data 428 if ((count > 0) and (le_time_found = '0') and (wave_data < LE_time_thresh) and (wave_mem(0) > LE_time_thresh)) then 429 if (LE_time_thresh - wave_data) > (wave_mem(0) - LE_time_thresh) then 430 le_time_i <= (win_samp_start_i(cur_asic) + count);
432 le_time_i <= (win_samp_start_i(cur_asic) + count + '1');
434 le_time_found := '1';
436 -- check for extremum 437 if ((count > "00000001") 438 and (wave_mem(0) < wave_mem(1)) 440 and (wave_mem(0) < baseline_g - peak_i)) 442 peak_i <= baseline_g - wave_mem(0);
443 -- measure leading-edge charge 444 if (count > "00000100") then 445 le_charge <= (baseline_g & "00") 446 - ("00" & wave_mem(0)) 447 - ("00" & wave_mem(1)) 448 - ("00" & wave_mem(2)) 449 - ("00" & wave_mem(3));
453 -- if wave_data <= baseline_g then 454 -- charge_n <= charge_n + baseline_g - wave_data; 456 -- charge_p <= charge_p + wave_data - baseline_g; 458 -- check & increment counter, shift sample into memory 459 if count < N_readout_samples - '1' then 460 count := count + '1';
461 for i in WAVE_MEM_DEPTH - 1 downto 1 loop 462 wave_mem(i) := wave_mem(i-1);
465 feat_ext_state <= LOOPING_SAMPLES;
469 feat_ext_state <= CHECK_DONE;
471 feat_ext_state <= LOOPING_JOBS;
473 write_debug_wave_footer <= '1';
-- q, t, x"EOF" 474 tx_features_ena <= '1';
475 -- fill_SPS_hist_ena <= use_loop_trig and not use_ftsw_trig; 476 fill_SPS_hist_ena <= '1';
480 write_debug_wave_footer <= '0';
481 fill_SPS_hist_ena <= '0';
482 tx_features_ena <= '0';
483 if fifos_empty = '0' then 484 feat_ext_state <= CHECK_FIFOS_RDY;
486 feat_ext_state <= IDLE;
490 -- feat_ext_state <= IDLE; 495 end process Extract_Features;
499 --fill_debug_fifos_with_waveform_data 500 -- FSM enable issued in Extract_Features process block 501 wr_debug_wave:
process(clk, write_debug_wave_header, write_debug_wave_footer,
502 daq_chan_i, win_samp_start_i, cur_asic,
505 if rising_edge(clk) then 506 case debug_fifo_fill_state is 508 if write_debug_wave_header = '1' then 510 debug_wave <= x"50F";
-- i.e. "SOF" 511 debug_fifo_fill_state <= WRITE_CHAN_AND_SAMP;
514 debug_wave <= x"000";
515 debug_fifo_fill_state <= IDLE;
517 When WRITE_CHAN_AND_SAMP => 518 debug_wave <= daq_chan_i & win_samp_start_i(cur_asic)(4 downto 0);
519 debug_fifo_fill_state <= WRITE_ASIC_AND_WINDOW;
520 When WRITE_ASIC_AND_WINDOW => 521 debug_wave <= (to_unsigned(cur_asic, 3)) & win_samp_start_i(cur_asic)(13 downto 5);
522 debug_fifo_fill_state <= WRITING;
524 if write_debug_wave_footer = '0' then 525 debug_we <= wave_drdy;
-- aligned with wave/ped_data 526 if stream_peds = '1' then 527 debug_wave <= ped_data;
531 debug_fifo_fill_state <= WRITING;
534 debug_wave <= peak_i;
535 debug_fifo_fill_state <= WRITE_LAST_WORD;
537 When WRITE_LAST_WORD => 538 debug_wave <= le_time_i(11 downto 0);
539 debug_fifo_fill_state <= WRITE_EOF;
541 debug_wave <= x"E0F";
542 debug_fifo_fill_state <= IDLE;
544 -- debug_wave <= x"000"; 546 -- debug_fifo_fill_state <= IDLE; 548 debug_fifo_fill_state <= IDLE;
551 end process wr_debug_wave;
555 -- Transmit Features To Hit Builder 556 -- This FSM is enabled by the "Extract_Features" process block above 557 tx_features:
process(clk, tx_features_ena, peak_i, le_time_i,
558 daq_chan_i, last_hit_i, daq_chan_i, last_hit_i,
561 if rising_edge(clk) then 562 Case tx_features_state is 565 if tx_features_ena = '1' then 566 rx_features_ena <= '1';
567 tx_features_busy <= '1';
568 -- null_hit <= null_hit_i; 570 le_time <= le_time_i;
571 daq_chan <= daq_chan_i;
572 last_hit <= last_hit_i;
573 tx_features_state <= WAIT_TX;
575 rx_features_ena <= '0';
576 tx_features_busy <= '0';
577 tx_features_state <= IDLE;
582 tx_features_state <= WAIT_TX;
584 rx_features_ena <= '0';
585 tx_features_state <= IDLE;
589 -- tx_features_state <= IDLE; 593 end process tx_features;
597 -- create_single_photon_spectra 598 -- FSM enable issued in Extract_Features process block 599 -- FSM stays in IDLE state when transmitting data to status registers for readback 600 fill_SPS:
process(clk, SPS_hist_rd_addr, fill_SPS_hist_ena, --peak_i, charge_s, charge_n, charge_s,
601 le_charge,
sps_reset, i_SPS_hist_rd_data)
602 variable count : (9 downto 0);
604 if rising_edge(clk) then 606 -- pipelined output to reg interface 607 i_SPS_hist_rd_data(i_SPS_hist_rd_data'left downto 1) <= i_SPS_hist_rd_data(i_SPS_hist_rd_data'left - 1 downto 0);
608 SPS_hist_rd_data <= i_SPS_hist_rd_data(i_SPS_hist_rd_data'left);
611 count := (others=> '0');
612 fill_SPS_hist_state <= RESETTING;
615 case( fill_SPS_hist_state ) is 618 count := (others=>'0');
619 SPS_hist_wr_ena <= "0";
620 SPS_hist_address <= SPS_hist_rd_addr;
-- wired to regiter interface 621 -- if fill_SPS_hist_ena = '1' and peak_i <= "11011010" then -- 2^14 addressess allows 218 bins per daq_chan 622 -- if fill_SPS_hist_ena = '1' and charge_n > charge_p and charge_s <= "0000100000000000000" then -- resolution of 1 adc count for smallish waveforms, single chan 623 if fill_SPS_hist_ena = '1' and le_charge(13 downto 10) = "0000" then -- 1024 bins 624 -- SPS_hist_address <= std_logic_vector(to_unsigned(to_integer(unsigned(daq_chan_i))*218 + to_integer(unsigned(peak_i)), 14)); 625 -- SPS_hist_address <= charge_s(13 downto 0); 626 SPS_hist_address <= le_charge(9 downto 0);
627 fill_SPS_hist_state <= READ_RAM;
629 fill_SPS_hist_state <= IDLE;
633 if count < SPSBlockRAM_READ_LATENCY then 635 fill_SPS_hist_state <= READ_RAM;
637 count := (others=>'0');
638 SPS_hist_wr_data <= i_SPS_hist_rd_data(0) + "0000000000000001";
639 SPS_hist_wr_ena <= "1";
640 fill_SPS_hist_state <= WRITE_TO_RAM;
644 fill_SPS_hist_state <= IDLE;
647 SPS_hist_wr_ena <= "1";
648 SPS_hist_address <= count;
649 SPS_hist_wr_data <= (others=>'0');
650 -- if count = "11111111111111" then 651 if count = "1111111111" then 652 fill_SPS_hist_state <= IDLE;
654 count := count + '1';
655 fill_SPS_hist_state <= RESETTING;
662 end process fill_SPS;
669 avg_and_wr_peds:
process(clk, avg_peds_ena_i, sram_asic_addr_i, wave_div_NBA,
670 wave_drdy, avg_peds_rd_ena, avg_peds_chn_sel,
672 variable chan : range 0 to 15 := 0;
673 -- variable asic : integer range 0 to 4 := 0; 675 if rising_edge(clk) then 676 Case avg_peds_state is 681 avg_peds_asic_mask <= (others=>'0');
682 if avg_peds_ena_i = "01" then 683 avg_peds_busy <= '1';
684 avg_peds_asic_mask <= "00001";
685 avg_peds_rd_ena <= '1';
--keep high two clocks 686 sram_asic_addr_i <= "000";
687 sram_samp_addr_i <= "0000";
688 avg_peds_chn_sel <= "0000";
689 avg_peds_state <= READ_ONE_MORE;
691 avg_peds_rd_ena <= '0';
692 avg_peds_busy <= '0';
693 avg_peds_state <= IDLE;
697 avg_peds_chn_sel <= "0000";
698 sram_samp_addr_i <= "0000";
700 if avg_peds_asic_mask = "00000" then 701 avg_peds_state <= IDLE;
703 sram_asic_addr_i <= sram_asic_addr_i + "001";
704 -- avg_peds_asic_mask <= (others=>'0'); 705 -- avg_peds_asic_mask(asic) <= '1'; 706 avg_peds_rd_ena <= '1';
--keep high two clocks 707 avg_peds_state <= READ_ONE_MORE;
711 sram_samp_addr_i <= "0000";
713 avg_peds_chn_sel <= avg_peds_chn_sel + "0001";
714 avg_peds_rd_ena <= '1';
--keep high two clocks 715 avg_peds_state <= READ_ONE_MORE;
717 avg_peds_chn_sel <= "0000";
719 avg_peds_asic_mask <= avg_peds_asic_mask(3 downto 0) & '0';
721 avg_peds_state <= ASIC_LOOP;
724 When READ_ONE_MORE => 725 avg_peds_rd_ena <= '1';
--second clock high 726 avg_peds_state <= AVERAGE_EVEN_SAMPLE;
729 When AVERAGE_EVEN_SAMPLE => 730 avg_peds_rd_ena <= '0';
--two clocks elapsed, set low here. 731 if wave_drdy = '1' then 732 even_ped <= wave_div_NBA;
733 avg_peds_state <= AVERAGE_ODD_SAMPLE;
735 avg_peds_state <= AVERAGE_EVEN_SAMPLE;
738 When AVERAGE_ODD_SAMPLE => 739 wr_peds2sram_ena <= '1';
740 odd_ped <= wave_div_NBA;
741 avg_peds_state <= WAIT_PED_WRITER;
743 When WAIT_PED_WRITER => 744 wr_peds2sram_ena <= '1';
745 if wr_peds2sram_ack = '1' then 746 wr_peds2sram_ena <= '0';
747 if sram_samp_addr_i /= "1111" then 748 sram_samp_addr_i <= sram_samp_addr_i + "0001";
749 avg_peds_rd_ena <= '1';
--keep high two clocks 750 avg_peds_state <= READ_ONE_MORE;
752 sram_samp_addr_i <= "0000";
754 avg_peds_state <= CHANNEL_LOOP;
757 avg_peds_state <= WAIT_PED_WRITER;
761 avg_peds_state <= IDLE;
765 end process avg_and_wr_peds;
770 --------------------- BEGIN MODULES --------------------------------------------- 771 SPS_RAM :
entity work.SPSBlockRAM
775 addra => SPS_hist_address,
776 dina => SPS_hist_wr_data,
777 douta => i_SPS_hist_rd_data
(0),
778 wea => SPS_hist_wr_ena