AMC13
Firmwares for the different applications of the AMC13 uTCA board made at Boston University
 All Classes Files 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(7 downto 0) := (others =>'0');
55 signal TTC_derr_cntr : std_logic_vector(7 downto 0) := (others =>'0');
56 signal TTC_BcntErr_cntr : std_logic_vector(7 downto 0) := (others =>'0');
57 signal L1A_cntr : std_logic_vector(7 downto 0) := (others =>'0');
58 signal L1A_OFW_cntr : std_logic_vector(7 downto 0) := (others =>'0');
59 signal L1A_BUSY_cntr : std_logic_vector(7 downto 0) := (others =>'0');
60 signal L1A_LOS_cntr : std_logic_vector(7 downto 0) := (others =>'0');
61 signal run_cntr : std_logic_vector(7 downto 0) := (others =>'0');
62 signal ready_cntr : std_logic_vector(7 downto 0) := (others =>'0');
63 signal busy_cntr : std_logic_vector(7 downto 0) := (others =>'0');
64 signal sync_cntr : std_logic_vector(7 downto 0) := (others =>'0');
65 signal ovfl_cntr : std_logic_vector(7 downto 0) := (others =>'0');
66 signal ReSync_cntr : std_logic_vector(7 downto 0) := (others =>'0');
67 signal startSyncRegs : std_logic_vector(3 downto 0) := (others =>'0');
68 signal div : std_logic_vector(3 downto 0) := (others =>'0');
69 signal start : std_logic := '0';
70 signal CntrRstCycle : std_logic := '0';
71 signal cntr : std_logic_vector(7 downto 0) := (others => '0');
72 signal counter_wa : std_logic_vector(5 downto 0) := (others => '0');
73 signal counter_ra : std_logic_vector(5 downto 0) := (others => '0');
74 signal ram_di : std_logic_vector(31 downto 0) := (others => '0');
75 signal ram_dpo : std_logic_vector(31 downto 0) := (others => '0');
76 signal ram_spo : std_logic_vector(31 downto 0) := (others => '0');
77 signal we_ram : std_logic := '0';
78 signal ram_wa : std_logic_vector(5 downto 0) := (others => '0');
79 signal ec_div : std_logic := '0';
80 signal channel : std_logic_vector(3 downto 0) := (others => '0');
81 signal DB_cmd_l : std_logic := '0';
82 signal DB_en : std_logic := '0';
83 signal carry : std_logic := '0';
84 signal sr : std_logic_vector(5 downto 0) := (others => '0');
85 signal toggle : std_logic := '0';
86 signal toggle_q : std_logic := '0';
87 signal ec_rdata : std_logic := '0';
88 signal ec_wdata : std_logic := '0';
89 
90 begin
91 process(sysClk, rst_cntr,reset)
92 begin
93  if(reset = '1' or rst_cntr = '1')then
94  TTC_serr_cntr <= (others =>'0');
95  TTC_derr_cntr <= (others =>'0');
96  TTC_BcntErr_cntr <= (others =>'0');
97  L1A_cntr <= (others =>'0');
98  L1A_OFW_cntr <= (others =>'0');
99  L1A_BUSY_cntr <= (others =>'0');
100  L1A_LOS_cntr <= (others =>'0');
101  run_cntr <= (others =>'0');
102  ready_cntr <= (others =>'0');
103  busy_cntr <= (others =>'0');
104  sync_cntr <= (others =>'0');
105  ovfl_cntr <= (others =>'0');
106  ReSync_cntr <= (others =>'0');
107  elsif(sysClk'event and sysClk = '1')then
108  if(inc_serr = '1')then
109  TTC_serr_cntr <= TTC_serr_cntr + 1;
110  end if;
111  if(inc_derr = '1')then
112  TTC_derr_cntr <= TTC_derr_cntr + 1;
113  end if;
114  if(inc_bcnterr = '1')then
115  TTC_BcntErr_cntr <= TTC_BcntErr_cntr + 1;
116  end if;
117  if(inc_l1ac = '1')then
118  L1A_cntr <= L1A_cntr + 1;
119  if(state = x"1")then
120  L1A_OFW_cntr <= L1A_OFW_cntr + 1;
121  end if;
122  if(state = x"4")then
123  L1A_BUSY_cntr <= L1A_BUSY_cntr + 1;
124  end if;
125  if(state = x"2")then
126  L1A_LOS_cntr <= L1A_LOS_cntr + 1;
127  end if;
128  end if;
129  if(run = '1')then
130  run_cntr <= run_cntr + 1;
131  if(state(3 downto 2) = "10")then
132  ready_cntr <= ready_cntr + 1;
133  end if;
134  if(state(3 downto 2) = "01")then
135  busy_cntr <= busy_cntr + 1;
136  end if;
137  if(state(3) = '0' and state(1) = '1')then
138  sync_cntr <= sync_cntr + 1;
139  end if;
140  if(state(3) = '0' and state(0) = '1')then
141  ovfl_cntr <= ovfl_cntr + 1;
142  end if;
143  end if;
144  if(ttc_resync = '1')then
145  ReSync_cntr <= ReSync_cntr + 1;
146  end if;
147  end if;
148 end process;
149 start <= sr(0);
150 process(sysClk, CntrRstCycle)
151 begin
152  if(CntrRstCycle = '1')then
153  startSyncRegs <= (others => '0');
154  ec_div <= '0';
155  div <= (others => '0');
156  channel <= (others => '0');
157  cntr <= (others => '0');
158  elsif(sysclk'event and sysclk = '1')then
159  startSyncRegs <= startSyncRegs(2 downto 0) & start;
160  if(startSyncRegs(3 downto 2) = "01")then
161  ec_div <= '1';
162  else
163  ec_div <= '0';
164  end if;
165  if(ec_div = '1')then
166  if(div(3 downto 2) = "11")then
167  div <= (others => '0');
168  else
169  div <= div + 1;
170  end if;
171  channel <= div;
172  case div is
173  when x"0" => cntr <= TTC_serr_cntr;
174  when x"1" => cntr <= TTC_derr_cntr;
175  when x"2" => cntr <= TTC_BcntErr_cntr;
176  when x"3" => cntr <= L1A_cntr;
177  when x"4" => cntr <= run_cntr;
178  when x"5" => cntr <= ready_cntr;
179  when x"6" => cntr <= busy_cntr;
180  when x"7" => cntr <= sync_cntr;
181  when x"8" => cntr <= ovfl_cntr;
182  when x"9" => cntr <= L1A_OFW_cntr;
183  when x"a" => cntr <= L1A_BUSY_cntr;
184  when x"b" => cntr <= L1A_LOS_cntr;
185  when x"c" => cntr <= ReSync_cntr;
186  when others => cntr <= (others => '0');
187  end case;
188  end if;
189  end if;
190 end process;
191 g_ram: for i in 0 to 31 generate
192  i_ram : RAM64X1D
193  port map (
194  DPO => ram_dpo(i), -- Read-only 1-bit data output
195  SPO => ram_spo(i), -- R/W 1-bit data output
196  A0 => ram_wa(0), -- R/W address[0] input bit
197  A1 => ram_wa(1), -- R/W address[1] input bit
198  A2 => ram_wa(2), -- R/W address[2] input bit
199  A3 => ram_wa(3), -- R/W address[3] input bit
200  A4 => ram_wa(4), -- R/W address[4] input bit
201  A5 => ram_wa(5), -- R/W address[5] input bit
202  D => ram_di(i), -- Write 1-bit data input
203  DPRA0 => ipb_addr(0), -- Read-only address[0] input bit
204  DPRA1 => ipb_addr(1), -- Read-only address[1] input bit
205  DPRA2 => ipb_addr(2), -- Read-only address[2] input bit
206  DPRA3 => ipb_addr(3), -- Read-only address[3] input bit
207  DPRA4 => ipb_addr(4), -- Read-only address[4] input bit
208  DPRA5 => ipb_addr(15), -- Read-only address[5] input bit
209  WCLK => clk125, -- Write clock input
210  WE => we_ram -- Write enable input
211  );
212 end generate;
213 process(ipb_clk)
214 begin
215  if(ipb_clk'event and ipb_clk = '1')then
216  toggle <= not toggle;
217  end if;
218 end process;
219 process(clk125)
220 begin
221  if(clk125'event and clk125 = '1')then
222  toggle_q <= toggle;
223  ec_rdata <= toggle xor toggle_q;
224  if(DB_cmd = '1' and ec_wdata = '1')then
225  DB_cmd_l <= '1';
226  elsif(sr(5) = '1' and ram_wa(4 downto 0) = "11001")then
227  DB_cmd_l <= '0';
228  end if;
229  if(sr(5) = '1' and ram_wa(4 downto 0) = "11001")then
230  DB_en <= DB_cmd_l;
231  end if;
232  if(rst_cntr = '1')then
233  CntrRstCycle <= '1';
234  elsif(ram_wa = "11001")then
235  CntrRstCycle <= '0';
236  end if;
237  if(rst_cntr = '1')then
238  ram_wa <= (others => '0');
239  elsif(CntrRstCycle = '1')then
240  ram_wa <= ram_wa + 1;
241  elsif(sr(0) = '1')then
242  ram_wa <= '0' & channel & '0';
243  elsif(sr(2) = '1' or sr(5) = '1')then
244  ram_wa(5) <= '1';
245  elsif(sr(3) = '1')then
246  ram_wa(0) <= '1';
247  ram_wa(5) <= '0';
248  end if;
249  if(rst_cntr = '1')then
250  sr <= "000000";
251  elsif(sr(4 downto 0) = "00000")then
252  sr <= "000001";
253  else
254  sr <= sr(4 downto 0) & '0';
255  end if;
256  if(sr(2) = '1')then
257  carry <= not ram_di(31) and ram_spo(31);
258  end if;
259  if(CntrRstCycle = '1')then
260  ram_di <= (others => '0');
261  elsif(sr(0) = '1')then
262  ram_di(7 downto 0) <= cntr;
263  elsif(sr(1) = '1')then
264  if(ram_spo(7 downto 0) > ram_di(7 downto 0))then
265  ram_di(31 downto 8) <= ram_spo(31 downto 8) + 1;
266  else
267  ram_di(31 downto 8) <= ram_spo(31 downto 8);
268  end if;
269  elsif(sr(4) = '1')then
270  ram_di <= x"0000" & (ram_spo(15 downto 0) + carry);
271  end if;
272  we_ram <= CntrRstCycle or sr(1) or (sr(2) and DB_en) or sr(4) or (sr(5) and DB_en);
273  if(ipb_addr(14 downto 5) /= misc_cntr_addr(14 downto 5))then
274  ipb_rdata <= (others => '0');
275  elsif(ec_rdata = '1')then
276  ipb_rdata <= ram_dpo;
277  end if;
278  end if;
279 end process;
280 i_ec_wdata : SRL16E
281  generic map (
282  INIT => X"0000")
283  port map (
284  Q => ec_wdata, -- SRL data output
285  A0 => '1', -- Select[0] input
286  A1 => '0', -- Select[1] input
287  A2 => '0', -- Select[2] input
288  A3 => '0', -- Select[3] input
289  CE => '1', -- Clock enable input
290  CLK => clk125, -- Clock input
291  D => ec_rdata -- SRL data input
292  );
293 end Behavioral;
294