Belle II KLM Scint Firmware  1
PedestalFetcher.vhd
1 -- Module Name: PedestalFetcher - Behavioral
2 -- Create Date: 11:07:41 09/21/2018
3 library IEEE;
4 use IEEE.STD_LOGIC_1164.ALL;
5 use IEEE.NUMERIC_STD.ALL;
6 use IEEE.STD_LOGIC_MISC.ALL;
7 use IEEE.STD_LOGIC_UNSIGNED.ALL;
8 Library work;
9 use work.klm_scint_pkg.all;
48 
49 
50 entity PedestalFetcher is
51  Generic (
52  ped_ram_tAA : integer range 0 to 15 := 9 --(71 ns @ 127 MHz) : spec 55 ns
53  );
54  Port (
55  clk : in STD_logic;
56  rst : in STD_logic;
57  ena : in std_logic_vector(1 downto 0) := (others => '0');
58  ack : out std_logic_vector(1 downto 0) := (others => '0');
59  -- initial address
60  asic_addr : in slv3(1 downto 0);-- := (others => "000");
61  chan_addr : in slv4(1 downto 0) := (others => "0000");
62  win_samp_start : in slv14(1 downto 0) := (others => (others=>'0') );
63  -- wires with storage FIFOs
64  fifo_asic_sel : out std_logic_vector(4 downto 0) := (others => '0');
65  fifo_chan_sel : out std_logic_vector(3 downto 0) := (others => '0');
66  ped_fifo_wr_ena : out std_logic_vector(1 downto 0) := (others => '0');
67  ped_fifo_din : out std_logic_vector(11 downto 0) := (others => '0');
68  -- control registers
69  N_readout_samples : in std_logic_vector(7 downto 0);
70  --PedRAMaccess signals
71  RAM_do : in std_logic_vector(7 downto 0);
72  RAM_ADDR : out std_logic_vector(21 downto 0) := (others => '1')
73  );
74 end PedestalFetcher;
75 
76 architecture Behavioral of PedestalFetcher is
77 
78 
79  type pedestal_fetching_machine is (
80  IDLE,
81  PREPARING_NEXT_ADDR,
82  WAITING_TAA,
83  RESETTING
84  );
85  signal ped_fetch_state : pedestal_fetching_machine := IDLE;
86 
87  type xfer_to_fifo_machine is (
88  IDLE,
89  WRITE_NEXT_VAL,
90  FINISHING
91  );
92  signal fifo_fill_state : xfer_to_fifo_machine := IDLE;
93 
94  signal addr_MSBYTE : std_logic_vector(7 downto 0) := (others => '0'); -- initial address 8 MSBs
95  signal addr : slv22(1 downto 0) := (others => "0000000000000000000000"); -- initial address
96  signal next_addr : std_logic_vector(21 downto 0) := (others => '0');
97  signal nxt_wn_smp : std_logic_vector(13 downto 0) := (others => '0'); -- count over ROI
98  signal ped_asic_i : std_logic_vector(4 downto 0);
99  signal ped_ch_i : std_logic_vector(3 downto 0);
100  signal count3 : integer range 0 to 3 := 0; -- count groups of 3 adddresses
101  signal pedarr : slv8(2 downto 0) := (others => "00000000"); -- local storage for 3 consecutive ram outputs
102  signal xfer_to_fifo : std_logic := '0'; -- enable signal for xfer_to_fifo state machine
103  signal start_rx : std_logic := '0'; -- enable signal for SRAM_rx process
104  signal ack_i : std_logic_vector(1 downto 0) := "00"; -- remember last bus serviced for arbitration reasons
105  signal ack_ii : std_logic_vector(1 downto 0) := "00"; -- remember last bus serviced for arbitration reasons
106  signal ack_prev : std_logic_vector(1 downto 0) := "01"; -- remember last bus serviced for arbitration reasons
107  signal tx_dc : slv4(1 downto 0) := (others => "0000"); -- daughter card number for each bus
108  signal idx : integer range 0 to 2;
109 
110 begin
111 
112  ack <= ack_i;
113 
114  tx_dc(0) <= '0' & asic_addr(0);
115  tx_dc(1) <= "0101" + ('0' & asic_addr(1));
116 
117  addr(0) <= tx_dc(0) & chan_addr(0) & win_samp_start(0)(13 downto 1) & '0';
118  addr(1) <= tx_dc(1) & chan_addr(1) & win_samp_start(1)(13 downto 1) & '0';
119 
120 --------------------------------------------------------------------------------
121 -- Read ext. SRAM using address-transition-controled mode
122 --------------------------------------------------------------------------------
123  SRAM_tx : process(clk)
124  variable samp_cnt : std_logic_vector(7 downto 0);
125  variable count_tAA : integer range 0 to 15 := 0;
126  begin
127  if (rising_edge(clk)) then
128  if rst = '1' then
129  ped_fetch_state <= IDLE;
130  else
131 
132  case ped_fetch_state is
133 
134  when IDLE =>
135  idx <= 0;
136  samp_cnt := "00000000";
137  nxt_wn_smp <= (others=>'0');
138  ped_asic_i <= (others=>'0');
139  count_tAA := 0;
140 
141  -- Single bus enable
142  if (ena(0) xor ena(1)) = '1' then
143  ack_i <= ena;
144  if ena(0) = '1' then
145  addr_MSBYTE <= addr(0)(21 downto 14);
146  RAM_ADDR <= addr(0) + ('0' & addr(0)(21 downto 1));
147  next_addr <= addr(0) + ('0' & addr(0)(21 downto 1)) + "0000000000000000000001";
148  nxt_wn_smp <= win_samp_start(0) + "10";
149  ped_asic_i(to_integer(unsigned(asic_addr(0)))) <= '1';
150  ped_ch_i <= chan_addr(0);
151  else
152  addr_MSBYTE <= addr(1)(21 downto 14);
153  RAM_ADDR <= addr(1) + ('0' & addr(1)(21 downto 1));
154  next_addr <= addr(1) + ('0' & addr(1)(21 downto 1)) + "0000000000000000000001";
155  nxt_wn_smp <= win_samp_start(1) + "10";
156  ped_asic_i(to_integer(unsigned(asic_addr(1)))) <= '1';
157  ped_ch_i <= chan_addr(1);
158  end if;
159  -- Arbiter for collisions between busses
160  elsif (ena(0) and ena(1)) = '1' then
161  ack_i <= not ack_prev;
162  if ack_prev(0) = '1' then
163  addr_MSBYTE <= addr(1)(21 downto 14);
164  RAM_ADDR <= addr(1) + ('0' & addr(1)(21 downto 1));
165  next_addr <= addr(1) + ('0' & addr(1)(21 downto 1)) + "0000000000000000000001";
166  nxt_wn_smp <= win_samp_start(1) + "00000000000010";
167  ped_asic_i(to_integer(unsigned(asic_addr(1)))) <= '1';
168  ped_ch_i <= chan_addr(1);
169  else
170  addr_MSBYTE <= addr(0)(21 downto 14);
171  RAM_ADDR <= addr(0) + ('0' & addr(0)(21 downto 1));
172  next_addr <= addr(0) + ('0' & addr(0)(21 downto 1)) + "0000000000000000000001";
173  nxt_wn_smp <= win_samp_start(0) + "00000000000010";
174  ped_asic_i(to_integer(unsigned(asic_addr(0)))) <= '1';
175  ped_ch_i <= chan_addr(0);
176  end if;
177  else
178  ack_i <= "00";
179  end if;
180  if (ena(0) or ena(1)) = '1' then
181  start_rx <= '1';
182  count3 <= 0;
183  ped_fetch_state <= PREPARING_NEXT_ADDR;
184  else
185  ped_fetch_state <= IDLE;
186  end if;
187 
188  when PREPARING_NEXT_ADDR =>
189  start_rx <= '0';
190  if samp_cnt < N_readout_samples then
191  ped_fetch_state <= WAITING_TAA;
192  count3 <= 0;
193  else
194  ack_prev <= ack_i;
195  ped_fetch_state <= RESETTING;
196  end if;
197 
198  -- wait time 55 ns for address-transition-controlled mode
199  when WAITING_TAA =>
200  start_rx <= '0';
201  if count_tAA < ped_ram_tAA then
202  count_tAA := count_tAA + 1;
203  ped_fetch_state <= WAITING_TAA;
204  else -- data ready
205  -- scheme of 2 peds in 3 addresses handled here.
206  RAM_ADDR <= next_addr; -- if last sample done, read next anyway, it would be good that we wait 55 ns to return to IDLE so fifo xfer can finis
207  start_rx <= '1';
208  idx <= count3;
209  if count3 /= 1 then
210  next_addr <= next_addr + "0000000000000000000001";
211  else
212  next_addr <= (addr_MSBYTE & nxt_wn_smp(13 downto 0)) + ("0" & addr_MSBYTE & nxt_wn_smp(13 downto 1));
213  nxt_wn_smp <= nxt_wn_smp + "00000000000010";
214  end if;
215  if count3 < 2 then
216  count_tAA := 1; -- start counting on address transition.
217  count3 <= count3 + 1;
218  ped_fetch_state <= WAITING_TAA;
219  else
220  ack_ii <= ack_i;
221  count_tAA := 2; -- start counting on address transition.
222  count3 <= 0;
223  samp_cnt := samp_cnt + "00000010";
224  ped_fetch_state <= PREPARING_NEXT_ADDR;
225  end if;
226  end if;
227 
228  when RESETTING =>
229  if count_tAA < ped_ram_tAA then
230  count_tAA := count_tAA + 1;
231  ped_fetch_state <= RESETTING;
232  else -- data ready
233  ped_fetch_state <= IDLE;
234  end if;
235  end case;
236  end if;
237  end if;
238  end process;
239 
240  SRAM_rx : process(clk, start_rx, idx, RAM_do)
241  variable start_rx_shift : std_logic_vector(ped_ram_tAA downto 0);
242  begin
243  if rising_edge(clk) then
244  xfer_to_fifo <= '0';
245  start_rx_shift := start_rx_shift(ped_ram_tAA - 1 downto 0) & start_rx;
246  if start_rx_shift(start_rx_shift'left) = '1' then
247  pedarr(idx) <= RAM_do;
248  end if;
249  if start_rx_shift(start_rx_shift'left) = '1' and idx = 2 then
250  xfer_to_fifo <= '1';
251  end if;
252  end if;
253  end process;
254 
255  -- pack: pedarr(0) <= even_sample(*)(11 downto 4);
256  -- pack: pedarr(1) <= even_sample(*)(3 downto 0) & odd_sample(*)(11 downto 8);
257  -- pack: pedarr(2) <= odd_sample(*)(7 downto 0);
258  -- unpack(here): even_sample <= pedarr(0) & pedarr(1)(7 downto 4);
259  -- unpack(here): odd_sample <= pedarr(1)(3 downto 0) & pedarr(2);
260  moving_peds_to_fifos : process(clk, xfer_to_fifo, ack_ii, ped_asic_i, ped_ch_i, pedarr)
261  begin
262  if (rising_edge(clk)) then
263  case fifo_fill_state is
264  when IDLE =>
265  if xfer_to_fifo = '0' then
266  fifo_fill_state <= IDLE;
267  else
268  ped_fifo_wr_ena <= ack_ii;
269  fifo_asic_sel <= ped_asic_i;
270  fifo_chan_sel <= ped_ch_i;
271  ped_fifo_din <= pedarr(0) & pedarr(1)(7 downto 4);
272  fifo_fill_state <= WRITE_NEXT_VAL;
273  end if;
274 
275  when WRITE_NEXT_VAL =>
276  ped_fifo_din <= pedarr(1)(3 downto 0) & pedarr(2);
277  fifo_fill_state <= FINISHING;
278 
279  when FINISHING =>
280  ped_fifo_wr_ena <= "00";
281  fifo_fill_state <= IDLE;
282 
283  end case;
284  end if;
285  end process;
286 
287 
288 
289 
290 end Behavioral;