Belle II KLM Scint Firmware  1
KLMHitDataSerializer.vhd
1 library ieee;
2  use ieee.std_logic_1164.all;
3  use ieee.numeric_std.all;
4  use ieee.std_logic_unsigned.all;
5 
19 
21  port(
22  clk : in std_logic;
23  rst : in std_logic;
24 
25  run : in std_logic; -- 1) set high for one clock cycle to start single transaction
26  -- event ready signal should be sent when all hits are stored in the fifo
27  -- 2) keep high in streaming mode.
28  -- event resdy signal may be sent at the same time
29 
30  first_hit : in std_logic; -- '1' if hit is first in the packet
31  last_hit : in std_logic; -- '1' if hit is last hit in the packet
32 
33  null_hit : in std_logic; -- hit data is empty (first_hit and last_hit - don't care)
34 
35  -- nxt : out std_logic; -- ready to accept next hit data
36 
37  -- 4 words now TODO:implement actual data format here
38  word1 : in std_logic_vector(15 downto 0);
39  word2 : in std_logic_vector(15 downto 0);
40  word3 : in std_logic_vector(15 downto 0);
41  word4 : in std_logic_vector(15 downto 0);
42 
43  -- fifo ports
44  fifo_ren : in std_logic;
45  fifo_empty : out std_logic;
46  -- fifo_full : out std_logic; -- xst653: is used but never assigned
47  fifo_dout : out std_logic_vector(17 downto 0);
48 
49  fifo_err_cnt : out std_logic_vector(15 downto 0);
50 
51  -- serialization is in progress
52  busy : out std_logic
53  );
54 end KLMHitDataSerializer;
55 
56 architecture beh of KLMHitDataSerializer is
57 
58  signal i_busy : std_logic;
59 
60  -- headers for first and last words in the packet |eof_n|sof_n|
61  signal hdr_first : std_logic_vector(1 downto 0);
62  signal hdr_last : std_logic_vector(1 downto 0);
63 
64  -- fifo signals
65  signal qt_wr_en : std_logic;
66  signal qt_fifo_din : std_logic_vector(17 downto 0);
67 
68  type hit_states is ( IDLE, INI, SEND_WORD1, SEND_WORD2, SEND_WORD3, SEND_WORD4, SEND_NULL );
69  signal state : hit_states := IDLE;
70 
71  signal word1_r : std_logic_vector(15 downto 0);
72  signal word2_r : std_logic_vector(15 downto 0);
73  signal word3_r : std_logic_vector(15 downto 0);
74  signal word4_r : std_logic_vector(15 downto 0);
75 
76  signal i_qt_fifo_err_cnt : std_logic_vector(15 downto 0) := (others=>'0');
77  signal i_qt_fifo_full : std_logic;
78  signal i_qt_fifo_full_r : std_logic;
79 
80 begin
81  ------------------------------------------------
82  --- FIFO for DAQ data to be sent
83  ------------------------------------------------
84  -- FIXME move it to readout module
85  qtfifo_i : entity work.fifo_cc
86  generic map(
87  DATA_WIDTH => 18,
88  DEPTH => 8 -- was 13bit = 8192
89  )
90  port map (
91  clk => clk,
92  rst => rst,
93  din => qt_fifo_din,
94  wen => qt_wr_en,
95  ren => fifo_ren,
96  dout => fifo_dout,
97  full => i_qt_fifo_full,
98  empty => fifo_empty
99  );
100  ------------------------------------------------
101 
102 
103  hdr_first <= '1' & (not first_hit);
104  hdr_last <= (not last_hit) & '1';
105 
106 
107  SEND_DATA_PROC : process(clk)
108  begin
109  if rising_edge(clk) then
110  if rst = '1' then
111  state <= IDLE;
112  else
113  -- nxt <= '0';
114 
115  case state is
116 
117  when IDLE =>
118  if run = '1' then
119  if null_hit = '1' then
120  state <= SEND_NULL;
121  else
122  state <= INI;
123  end if;
124  i_busy <= '1';
125 
126  -- nxt <= '1';
127  else
128  state <= IDLE;
129  i_busy <= '0';
130  qt_wr_en <= '0';
131  end if;
132 
133  when INI =>
134  state <= SEND_WORD1;
135  word1_r <= word1;
136  word2_r <= word2;
137  word3_r <= word3;
138  word4_r <= word4;
139 
140  when SEND_WORD1 =>
141 
142 
143  -- i_busy <= '1';
144  qt_wr_en <= '1';
145  qt_fifo_din <= hdr_first & word1_r;
146  state <= SEND_WORD2;
147  -- if last_hit = '0' and run = '1' then
148  -- nxt <= '1';
149  -- end if;
150 
151  when SEND_WORD2 =>
152  qt_fifo_din <= B"11" & word2_r;
153  state <= SEND_WORD3;
154 
155  when SEND_WORD3 =>
156  qt_fifo_din <= "11" & word3_r;
157  state <= SEND_WORD4;
158  when SEND_WORD4 =>
159  qt_fifo_din <= hdr_last & word4_r;
160  -- if last_hit = '1' or run = '0' then
161  -- state <= IDLE;
162  -- else
163  -- state <= SEND_WORD1;
164  -- end if;
165  i_busy <= '0';
166  word1_r <= word1;
167  word2_r <= word2;
168  word3_r <= word3;
169  word4_r <= word4;
170  state <= IDLE;
171 
172  when SEND_NULL =>
173  qt_wr_en <= '1';
174  qt_fifo_din <= "00" & x"FFFF";
175  state <= IDLE;
176 
177  end case;
178  end if;
179  end if;
180 
181  end process SEND_DATA_PROC;
182 
183  busy <= i_busy or fifo_ren;
184 
185  ------------------------------------------------
186  -- count how many times qt fifo gets full
187  ------------------------------------------------
188  process(clk)
189  begin
190  if rising_edge(clk) then
191  i_qt_fifo_full_r <= i_qt_fifo_full;
192  if i_qt_fifo_full = '1' and i_qt_fifo_full_r = '0' then
193  i_qt_fifo_err_cnt <= i_qt_fifo_err_cnt + 1;
194  end if;
195  end if;
196  end process;
197  fifo_err_cnt <= i_qt_fifo_err_cnt;
198  ------------------------------------------------
199 
200 end beh;
Definition: mem.vhd:103