Belle II KLM Scint Firmware  1
SamplingLgc.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_unsigned.all;
4 use ieee.numeric_std.all;
5 library unisim;
6 use unisim.vcomponents.all;
7 library work;
8 use work.klm_scint_pkg.all;
25 --
26 -- Deficiencies:
27 -- 1) generating SSTin using clock-counter probably not ideal. Should consider
28 -- using a divided clock from the pll_base.
29 --
30 -------------------------------------------------------------------------------
31 
32 
33 entity SamplingLgc is
34  port (
35  clk : in std_logic; --expect input clock to be 8xSST = 62.5 MHz (now it is 127 MHz)
36  reset : in std_logic := '0'; -- reset chip counters, it will reset the main counter and tells the TX chip to do so
37 
38  ana_wr_ena_mask : in TARGETX_analong_wr_ena_mask_t; -- ena & sel & win_start(8:0) & N_win(2:0)
39 
40  cur_win : out std_logic_vector(8 downto 0) := (others=>'0'); --COL & ROW write address- we just need this to keep track of the last sample taken
41  BUSA_WR_ADDRCLR : out std_logic := '0'; -- write address clear -- when asserted, it will reset the row and col on the TX chip
42  BUSB_WR_ADDRCLR : out std_logic := '0'; -- write address clear -- when asserted, it will reset the row and col on the TX chip
43  WR1_ENA : out std_logic_vector(9 downto 0) := (others=>'1'); -- Enable Write 1 procedure
44  WR2_ENA : out std_logic_vector(9 downto 0) := (others=>'1'); -- Enable Write 2 procedure
45 
46  SSTIN_N : out std_logic_vector(9 downto 0) := (others=>'0'); --SCA control signals
47  SSTIN_p : out std_logic_vector(9 downto 0) := (others=>'1') --SCA control signals
48  );
49 end SamplingLgc;
50 
51 architecture Behavioral of SamplingLgc is
52 
53 
54  --state machine drives SAMPLING logic signals directly
55  type state_type is (
56  RESETTING,
57  SAMPLING
58  );
59  signal analog_store_state : state_type := RESETTING;
60 
61  -- current write address
62  signal cur_win_i : UNSIGNED(8 downto 0) := "000000000";
63 
64  -- wires to TargetX
65  signal SSTIN_i : std_logic := '0'; --SCA control signals
66  signal wr_addrclr_i : std_logic := '0'; --Clear Write Address Counter
67  -- signal wr_ena : std_logic := '1'; --Enable Write procedure
68  signal wr_ena : std_logic_vector(9 downto 0) := (others => '1');
69 
70  signal clk_cntr : std_logic_vector(2 downto 0) := (others=>'0');
71 
72  signal wr_ena_mask : std_logic_vector(511 downto 0) := (others=>'1');
73 
74  signal i_reset : std_logic := '0';
75 
76 
77 --------------------------------------------------------------------------------
78 
79 begin
80 
81  process(clk, cur_win_i)
82  begin
83  if rising_edge(clk) then
84  cur_win <= std_logic_vector(cur_win_i(8 downto 0)); --output only row + colum sections
85  end if;
86  end process;
87 
88  SamplingMask : entity work.SamplingMask
89  port map (
90  clk => clk,
91  rst => i_reset,
92  ena => ana_wr_ena_mask.ena,
93  new_bit => ana_wr_ena_mask.mask_bit,
94  win_start => ana_wr_ena_mask.win_start,
95  N_win => ana_wr_ena_mask.n_win,
96  wr_ena_mask => wr_ena_mask
97  );
98 
99 
100  -- latch reset to local domain
101  process(clk, reset)
102  begin
103  if rising_edge(clk) then
104  i_reset <= reset;
105  end if;
106  end process;
107 
108 
109  -- Generate SSTIN
110  SSTIN_PROC : process(clk, clk_cntr)
111  begin
112  if rising_edge(clk) then
113  clk_cntr <= clk_cntr + "001";
114  SSTIN_i <= clk_cntr(2); -- clk is 127 MHz now, SSTIN period is clk*4
115  end if;
116  end process SSTIN_PROC;
117  -- SSTIN <= SSTIN_i;
118 
119 
120  -- Assert mask on write-enable pins
121  process(clk, wr_ena_mask, cur_win_i)
122  begin
123  if (rising_edge(clk)) then
124  for i in 0 to 9 loop
125  wr_ena(i) <= wr_ena_mask(to_integer(cur_win_i));
126  end loop;
127  end if;
128  end process;
129  WR1_ENA <= wr_ena;
130  WR2_ENA <= wr_ena;
131 
132 
133  -- Main Sampling FSM
134  process(clk, cur_win_i, i_reset, SSTIN_i, clk_cntr)
135  begin
136  if (rising_edge(clk)) then
137  if (i_reset='1') then
138  wr_addrclr_i <= '1';
139  cur_win_i <= (others=>'0');
140  analog_store_state <= RESETTING; -- do a one time thing to bring the counter up at the correct phase
141  else
142  case analog_store_state is
143 
144  when RESETTING =>
145  cur_win_i <= (others=>'0');
146  -- if ( clk_cntr(1 downto 0) = cfg_i(1 downto 0) ) then
147  if (SSTIN_i = '0' and clk_cntr(2) = '1') then -- rising edge of sstin
148  wr_addrclr_i <= '0';
149  analog_store_state <= SAMPLING;
150  else
151  --keep wr_addrclr high for afew clock cycles.
152  wr_addrclr_i <= '1';
153  analog_store_state <= RESETTING;
154  end if;
155 
156  when SAMPLING =>
157  wr_addrclr_i <= '0';
158  -- if (clk_cntr(1 downto 0) = "01" or clk_cntr(1 downto 0) = "11") then
159  if ( SSTIN_i = '0' and clk_cntr(2) = '1') or (SSTIN_i = '1' and clk_cntr(2) = '0') then -- rising or falling
160  cur_win_i <= cur_win_i + 1;
161  else
162  cur_win_i <= cur_win_i;
163  end if;
164  analog_store_state <= SAMPLING;
165 
166  when others =>
167  cur_win_i <= (others => '-');
168  analog_store_state <= SAMPLING;
169 
170  end case;
171  end if;
172  end if;
173  end process;
174  BUSA_WR_ADDRCLR <= wr_addrclr_i;
175  BUSB_WR_ADDRCLR <= wr_addrclr_i;
176 
177  GEN_SSTIN : for i in 0 to 9 generate
178  obuf_ds_sstin_i : OBUFDS port map ( i => SSTIN_i, o => SSTIN_P(i), ob => SSTIN_N(i));
179  end generate;
180 
181 end Behavioral;
182 
183 
184 
185 
186 
187 
188 
189 
190