AMC13
Firmwares for the different applications of the AMC13 uTCA board made at Boston University
 All Classes Variables
TTC_cntr.vhd
1 ----------------------------------------------------------------------------------
2 -- Company:
3 -- Engineer:
4 --
5 -- Create Date: 15:54:09 02/16/2016
6 -- Design Name:
7 -- Module Name: TTC_cntr - Behavioral
8 -- Project Name:
9 -- Target Devices:
10 -- Tool versions:
11 -- Description:
12 --
13 -- Dependencies:
14 --
15 -- Revision:
16 -- Revision 0.01 - File Created
17 -- Additional Comments:
18 --
19 ----------------------------------------------------------------------------------
20 library IEEE;
21 use IEEE.STD_LOGIC_1164.ALL;
22 use IEEE.STD_LOGIC_ARITH.ALL;
23 use IEEE.STD_LOGIC_UNSIGNED.ALL;
24 use IEEE.std_logic_misc.all;
25 use work.amc13_pack.all;
26 
27 -- Uncomment the following library declaration if using
28 -- arithmetic functions with Signed or Unsigned values
29 --use IEEE.NUMERIC_STD.ALL;
30 
31 -- Uncomment the following library declaration if instantiating
32 -- any Xilinx primitives in this code.
33 library UNISIM;
34 use UNISIM.VComponents.all;
35 
36 entity TTC_cntr is
37  Port ( sysclk : in STD_LOGIC;
38  clk125 : in STD_LOGIC;
39  ipb_clk : in STD_LOGIC;
40  reset : in STD_LOGIC;
41  rst_cntr : in STD_LOGIC;
42  DB_cmd : in STD_LOGIC;
43  inc_serr : in STD_LOGIC;
44  inc_derr : in STD_LOGIC;
45  inc_bcnterr : in STD_LOGIC;
46  inc_l1ac : in STD_LOGIC;
47  run : in STD_LOGIC;
48  state : in STD_LOGIC_VECTOR(3 downto 0);
49  ttc_resync : in STD_LOGIC;
50  ipb_addr : in STD_LOGIC_VECTOR(15 downto 0);
51  ipb_rdata : out STD_LOGIC_VECTOR(31 downto 0));
52 end TTC_cntr;
53 architecture Behavioral of TTC_cntr is
54 signal TTC_serr_cntr : std_logic_vector(8 downto 0) := (others =>'0');
55 signal TTC_derr_cntr : std_logic_vector(8 downto 0) := (others =>'0');
56 signal TTC_BcntErr_cntr : std_logic_vector(8 downto 0) := (others =>'0');
57 signal L1A_cntr : std_logic_vector(8 downto 0) := (others =>'0');
58 signal L1A_OFW_cntr : std_logic_vector(8 downto 0) := (others =>'0');
59 signal L1A_BUSY_cntr : std_logic_vector(8 downto 0) := (others =>'0');
60 signal L1A_LOS_cntr : std_logic_vector(8 downto 0) := (others =>'0');
61 signal run_cntr : std_logic_vector(8 downto 0) := (others =>'0');
62 signal ready_cntr : std_logic_vector(8 downto 0) := (others =>'0');
63 signal busy_cntr : std_logic_vector(8 downto 0) := (others =>'0');
64 signal sync_cntr : std_logic_vector(8 downto 0) := (others =>'0');
65 signal ovfl_cntr : std_logic_vector(8 downto 0) := (others =>'0');
66 signal daq_ovfl_cntr : std_logic_vector(8 downto 0) := (others =>'0');
67 signal state0x9_cntr : std_logic_vector(8 downto 0) := (others =>'0');
68 signal state0xa_cntr : std_logic_vector(8 downto 0) := (others =>'0');
69 signal state0xb_cntr : std_logic_vector(8 downto 0) := (others =>'0');
70 signal ReSync_cntr : std_logic_vector(8 downto 0) := (others =>'0');
71 signal state0x9_cntrb : std_logic_vector(8 downto 0) := (others =>'0');
72 signal state0xa_cntrb : std_logic_vector(8 downto 0) := (others =>'0');
73 signal state0xb_cntrb : std_logic_vector(8 downto 0) := (others =>'0');
74 signal state_was_9 : std_logic := '0';
75 signal state_was_a : std_logic := '0';
76 signal state_was_b : std_logic := '0';
77 signal startSyncRegs : std_logic_vector(3 downto 0) := (others =>'0');
78 signal div : std_logic_vector(4 downto 0) := (others =>'0');
79 signal start : std_logic := '0';
80 signal CntrRstCycle : std_logic := '0';
81 signal cntr : std_logic_vector(8 downto 0) := (others => '0');
82 signal ram_di : std_logic_vector(31 downto 0) := (others => '0');
83 signal ram_dpo : std_logic_vector(31 downto 0) := (others => '0');
84 signal ram_spo : std_logic_vector(31 downto 0) := (others => '0');
85 signal we_ram : std_logic := '0';
86 signal ram_wa : std_logic_vector(6 downto 0) := (others => '0');
87 signal ram_ra : std_logic_vector(6 downto 0) := (others => '0');
88 signal ec_div : std_logic := '0';
89 signal channel : std_logic_vector(4 downto 0) := (others => '0');
90 signal DB_cmd_l : std_logic := '0';
91 signal DB_en : std_logic := '0';
92 signal carry : std_logic := '0';
93 signal sr : std_logic_vector(5 downto 0) := (others => '0');
94 signal toggle : std_logic := '0';
95 signal toggle_q : std_logic := '0';
96 signal ec_rdata : std_logic := '0';
97 signal ec_wdata : std_logic := '0';
98 
99 begin
100 process(sysClk, rst_cntr,reset)
101 begin
102  if(reset = '1' or rst_cntr = '1')then
103  TTC_serr_cntr <= (others =>'0');
104  TTC_derr_cntr <= (others =>'0');
105  TTC_BcntErr_cntr <= (others =>'0');
106  L1A_cntr <= (others =>'0');
107  L1A_OFW_cntr <= (others =>'0');
108  L1A_BUSY_cntr <= (others =>'0');
109  L1A_LOS_cntr <= (others =>'0');
110  run_cntr <= (others =>'0');
111  ready_cntr <= (others =>'0');
112  busy_cntr <= (others =>'0');
113  sync_cntr <= (others =>'0');
114  ovfl_cntr <= (others =>'0');
115  daq_ovfl_cntr <= (others =>'0');
116  state0x9_cntr <= (others =>'0');
117  state0xa_cntr <= (others =>'0');
118  state0xb_cntr <= (others =>'0');
119  state0x9_cntrb <= (others =>'0');
120  state0xa_cntrb <= (others =>'0');
121  state0xb_cntrb <= (others =>'0');
122  ReSync_cntr <= (others =>'0');
123  elsif(sysClk'event and sysClk = '1')then
124  if(inc_serr = '1')then
125  TTC_serr_cntr <= TTC_serr_cntr + 1;
126  end if;
127  if(inc_derr = '1')then
128  TTC_derr_cntr <= TTC_derr_cntr + 1;
129  end if;
130  if(inc_bcnterr = '1')then
131  TTC_BcntErr_cntr <= TTC_BcntErr_cntr + 1;
132  end if;
133  if(inc_l1ac = '1')then
134  L1A_cntr <= L1A_cntr + 1;
135  if(state = x"1" or state = x"6")then
136  L1A_OFW_cntr <= L1A_OFW_cntr + 1;
137  end if;
138  if(state = x"4")then
139  L1A_BUSY_cntr <= L1A_BUSY_cntr + 1;
140  end if;
141  if(state = x"2")then
142  L1A_LOS_cntr <= L1A_LOS_cntr + 1;
143  end if;
144  end if;
145  if(run = '1')then
146  run_cntr <= run_cntr + 1;
147  if(state = x"8")then
148  ready_cntr <= ready_cntr + 1;
149  end if;
150  if(state = x"4")then
151  busy_cntr <= busy_cntr + 1;
152  end if;
153  if(state = x"2")then
154  sync_cntr <= sync_cntr + 1;
155  end if;
156  if(state = x"1" or state = x"6")then
157  ovfl_cntr <= ovfl_cntr + 1;
158  end if;
159  if(state = x"6")then
160  daq_ovfl_cntr <= daq_ovfl_cntr + 1;
161  end if;
162  if(state = x"9")then
163  state0x9_cntr <= state0x9_cntr + 1;
164  end if;
165  if(state = x"a")then
166  state0xa_cntr <= state0xa_cntr + 1;
167  end if;
168  if(state = x"b")then
169  state0xb_cntr <= state0xb_cntr + 1;
170  end if;
171  if(state = x"9" and state_was_9 = '0')then
172  state0x9_cntrb <= state0x9_cntrb + 1;
173  end if;
174  if(state = x"a" and state_was_a = '0')then
175  state0xa_cntrb <= state0xa_cntrb + 1;
176  end if;
177  if(state = x"b" and state_was_b = '0')then
178  state0xb_cntrb <= state0xb_cntrb + 1;
179  end if;
180  end if;
181  if(ttc_resync = '1')then
182  ReSync_cntr <= ReSync_cntr + 1;
183  end if;
184  end if;
185 end process;
186 process(sysClk)
187 begin
188  if(sysClk'event and sysClk = '1')then
189  if(state = x"9")then
190  state_was_9 <= '1';
191  else
192  state_was_9 <= '0';
193  end if;
194  if(state = x"a")then
195  state_was_a <= '1';
196  else
197  state_was_a <= '0';
198  end if;
199  if(state = x"b")then
200  state_was_b <= '1';
201  else
202  state_was_b <= '0';
203  end if;
204  end if;
205 end process;
206 start <= sr(0);
207 process(sysClk, CntrRstCycle)
208 begin
209  if(CntrRstCycle = '1')then
210  startSyncRegs <= (others => '0');
211  ec_div <= '0';
212  div <= (others => '0');
213  channel <= (others => '0');
214  cntr <= (others => '0');
215  elsif(sysclk'event and sysclk = '1')then
216  startSyncRegs <= startSyncRegs(2 downto 0) & start;
217  if(startSyncRegs(3 downto 2) = "01")then
218  ec_div <= '1';
219  else
220  ec_div <= '0';
221  end if;
222  if(ec_div = '1')then
223  if(div = "10011")then -- reached the last counter
224  div <= (others => '0');
225  else
226  div <= div + 1;
227  end if;
228  channel <= div;
229  if(div(4) = '0')then
230  case div(3 downto 0) is
231  when x"0" => cntr <= TTC_serr_cntr;
232  when x"1" => cntr <= TTC_derr_cntr;
233  when x"2" => cntr <= TTC_BcntErr_cntr;
234  when x"3" => cntr <= L1A_cntr;
235  when x"4" => cntr <= run_cntr;
236  when x"5" => cntr <= ready_cntr;
237  when x"6" => cntr <= busy_cntr;
238  when x"7" => cntr <= sync_cntr;
239  when x"8" => cntr <= ovfl_cntr;
240  when x"9" => cntr <= L1A_OFW_cntr;
241  when x"a" => cntr <= L1A_BUSY_cntr;
242  when x"b" => cntr <= L1A_LOS_cntr;
243  when x"c" => cntr <= ReSync_cntr;
244  when x"d" => cntr <= daq_ovfl_cntr;
245  when x"e" => cntr <= state0x9_cntr;
246  when others => cntr <= state0xa_cntr;
247  end case;
248  else
249  case div(3 downto 0) is
250  when x"0" => cntr <= state0xb_cntr;
251  when x"1" => cntr <= state0x9_cntrb;
252  when x"2" => cntr <= state0xa_cntrb;
253  when x"3" => cntr <= state0xb_cntrb;
254  when others => cntr <= (others => '0');
255  end case;
256  end if;
257  end if;
258  end if;
259 end process;
260 g_ram: for i in 0 to 31 generate
261  i_ram : RAM128X1D
262  port map (
263  DPO => ram_dpo(i), -- Read-only 1-bit data output
264  SPO => ram_spo(i), -- R/W 1-bit data output
265  A => ram_wa, -- R/W address[0] input bit
266  D => ram_di(i), -- Write 1-bit data input
267  DPRA => ram_ra, -- Read-only address[0] input bit
268  WCLK => clk125, -- Write clock input
269  WE => we_ram -- Write enable input
270  );
271 end generate;
272 ram_ra <= ipb_addr(15) & ipb_addr(5 downto 0);
273 process(ipb_clk)
274 begin
275  if(ipb_clk'event and ipb_clk = '1')then
276  toggle <= not toggle;
277  end if;
278 end process;
279 process(clk125)
280 begin
281  if(clk125'event and clk125 = '1')then
282  toggle_q <= toggle;
283  ec_rdata <= toggle xor toggle_q;
284  if(DB_cmd = '1' and ec_wdata = '1')then
285  DB_cmd_l <= '1';
286  elsif(sr(5) = '1' and ram_wa(5 downto 0) = "100111")then -- high word of the last counter
287  DB_cmd_l <= '0';
288  end if;
289  if(sr(5) = '1' and ram_wa(5 downto 0) = "100111")then -- high word of the last counter
290  DB_en <= DB_cmd_l;
291  end if;
292  if(rst_cntr = '1')then
293  CntrRstCycle <= '1';
294  elsif(ram_wa = "1111111")then
295  CntrRstCycle <= '0';
296  end if;
297  if(rst_cntr = '1')then
298  ram_wa <= (others => '0');
299  elsif(CntrRstCycle = '1')then
300  ram_wa <= ram_wa + 1;
301  elsif(sr(0) = '1')then
302  ram_wa <= '0' & channel & '0';
303  elsif(sr(2) = '1' or sr(5) = '1')then
304  ram_wa(6) <= '1';
305  elsif(sr(3) = '1')then
306  ram_wa(0) <= '1';
307  ram_wa(6) <= '0';
308  end if;
309  if(rst_cntr = '1')then
310  sr <= "000000";
311  elsif(sr(4 downto 0) = "00000")then
312  sr <= "000001";
313  else
314  sr <= sr(4 downto 0) & '0';
315  end if;
316  if(sr(2) = '1')then
317  carry <= not ram_di(31) and ram_spo(31);
318  end if;
319  if(CntrRstCycle = '1')then
320  ram_di <= (others => '0');
321  elsif(sr(0) = '1')then
322  ram_di(8 downto 0) <= cntr;
323  elsif(sr(1) = '1')then
324  if(ram_spo(8 downto 0) > ram_di(8 downto 0))then
325  ram_di(31 downto 9) <= ram_spo(31 downto 9) + 1;
326  else
327  ram_di(31 downto 9) <= ram_spo(31 downto 9);
328  end if;
329  elsif(sr(4) = '1')then
330  ram_di <= x"0000" & (ram_spo(15 downto 0) + carry);
331  end if;
332  we_ram <= CntrRstCycle or sr(1) or (sr(2) and DB_en) or sr(4) or (sr(5) and DB_en);
333  if(ipb_addr(14 downto 6) /= misc_cntr_addr(14 downto 6))then
334  ipb_rdata <= (others => '0');
335  elsif(ec_rdata = '1')then
336  ipb_rdata <= ram_dpo;
337  end if;
338  end if;
339 end process;
340 i_ec_wdata : SRL16E
341  generic map (
342  INIT => X"0000")
343  port map (
344  Q => ec_wdata, -- SRL data output
345  A0 => '1', -- Select[0] input
346  A1 => '0', -- Select[1] input
347  A2 => '0', -- Select[2] input
348  A3 => '0', -- Select[3] input
349  CE => '1', -- Clock enable input
350  CLK => clk125, -- Clock input
351  D => ec_rdata -- SRL data input
352  );
353 end Behavioral;
354