AMC13
Firmwares for the different applications of the AMC13 uTCA board made at Boston University
 All Classes Variables
TTS_if.vhd
1 ----------------------------------------------------------------------------------
2 -- Company:
3 -- Engineer:
4 --
5 -- Create Date: 09:49:10 06/14/2012
6 -- Design Name:
7 -- Module Name: TTS_if - 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 -- TTC Hamming encoding
20 -- hmg[0] = d[0]^d[1]^d[2]^d[3];
21 -- hmg[1] = d[0]^d[4]^d[5]^d[6];
22 -- hmg[2] = d[1]^d[2]^d[4]^d[5]^d[7];
23 -- hmg[3] = d[1]^d[3]^d[4]^d[6]^d[7];
24 -- hmg[4] = d[0]^d[2]^d[3]^d[5]^d[6]^d[7];
25 --
26 -- L1A trigger from CTR module takes the position of L1A in TTC
27 -- message format takes that of TTC broadcast message.(FMT = 0)
28 -- BcntRes uses brcst_data(0), TTS signals uses brcst_data(7 downto 4), brcst_data(3 downto 1) are reserved and are always 0
29 ----------------------------------------------------------------------------------
30 library IEEE;
31 use IEEE.STD_LOGIC_1164.ALL;
32 use IEEE.STD_LOGIC_ARITH.ALL;
33 use IEEE.STD_LOGIC_UNSIGNED.ALL;
34 use IEEE.std_logic_misc.all;
35 use work.amc13_pack.all;
36 library UNISIM;
37 use UNISIM.VComponents.all;
38 Library UNIMACRO;
39 use UNIMACRO.vcomponents.all;
40 
41 
43 
56 
57 entity TTS_if is
58  Port ( sysclk : in STD_LOGIC;
59  TTS_clk : in STD_LOGIC;
60  reset : in STD_LOGIC;
61  local_TTC : in STD_LOGIC; --! Controls TCDS DDR or TTC clock output
62  TTS : in STD_LOGIC_VECTOR (3 downto 0);
63  TTS_out_p : out STD_LOGIC;
64  TTS_out_n : out STD_LOGIC);
65 end TTS_if;
66 
67 architecture Behavioral of TTS_if is
68  COMPONENT RAM32x6D
69  PORT(
70  wclk : IN std_logic;
71  rclk : IN std_logic;
72  di : IN std_logic_vector(5 downto 0);
73  we : IN std_logic;
74  wa : IN std_logic_vector(4 downto 0);
75  ra : IN std_logic_vector(4 downto 0);
76  ceReg : IN std_logic;
77  do : OUT std_logic_vector(5 downto 0)
78  );
79  END COMPONENT;
80  COMPONENT Gray5
81  PORT(
82  d_current : IN std_logic_vector(4 downto 0);
83  d_next : OUT std_logic_vector(4 downto 0)
84  );
85  END COMPONENT;
86  signal TTS_out : std_logic := '0';
87  signal TTS_fifo_empty : std_logic := '1';
88  signal TTS_q : std_logic_vector(3 downto 0) := (others =>'0');
89  signal TTS_fifo_wa : std_logic_vector(4 downto 0) := (others =>'0');
90  signal TTS_fifo_ra : std_logic_vector(4 downto 0) := (others =>'0');
91  signal next_TTS_fifo_wa : std_logic_vector(4 downto 0) := (others =>'0');
92  signal next_TTS_fifo_ra : std_logic_vector(4 downto 0) := (others =>'0');
93  signal TTS_fifo_waSync : std_logic_vector(4 downto 0) := (others =>'0');
94  signal TTS_fifo_di : std_logic_vector(5 downto 0) := (others =>'0');
95  signal TTS_fifo_do : std_logic_vector(5 downto 0) := (others =>'0');
96  signal sr : std_logic_vector(9 downto 0) := (others =>'0');
97  signal TTS_fifo_we : std_logic := '0';
98  signal TTS_fifo_re : std_logic := '0';
99  signal NewTTS : std_logic := '0';
100  signal TTS_clk_div : std_logic_vector(2 downto 0) := (others =>'0');
101  signal SameTTS_cnt : std_logic_vector(1 downto 0) := (others =>'0');
102  signal IS_K : std_logic := '0';
103  signal enc_di : std_logic_vector(7 downto 0) := (others =>'0');
104  signal enc_do : std_logic_vector(9 downto 0) := (others =>'0');
105  signal bcnt : std_logic_vector(11 downto 0) := x"ff8";
106  signal LastBcnt : std_logic_vector(11 downto 0);
107  signal SendBC0 : std_logic := '0';
108  signal SendBC0_O : std_logic := '0';
109 begin
110 LastBcnt <= x"fff" when flavor = "G2" else x"deb";
111  process(sysclk)
112  begin
113  if(sysclk'event and sysclk = '1')then
114  if(TTS_q = TTS)then
115  TTS_FIFO_we <= '0';
116  else
117  TTS_FIFO_we <= '1';
118  end if;
119  end if;
120  end process;
121  process(sysclk,reset)
122  begin
123  if(reset = '1')then
124  TTS_q <= (others =>'0');
125  TTS_fifo_wa <= (others =>'0');
126  elsif(sysclk'event and sysclk = '1')then
127  TTS_q <= TTS;
128  if(TTS_FIFO_we = '1')then
129  TTS_fifo_wa <= next_TTS_fifo_wa;
130  end if;
131  end if;
132  end process;
133  i_next_TTS_fifo_wa : Gray5 PORT MAP(
134  d_current => TTS_fifo_wa,
135  d_next => next_TTS_fifo_wa
136  );
137  i_TTS_FIFO : RAM32X6D
138  port map (
139  wclk => sysclk,
140  rclk => TTS_clk,
141  di => TTS_FIFO_di,
142  we => TTS_FIFO_we,
143  wa => TTS_FIFO_wa,
144  ra => TTS_FIFO_ra,
145  ceReg => '1',
146  do => TTS_FIFO_do
147  );
148  TTS_FIFO_di(3 downto 0) <= TTS_q;
149  i_next_TTS_fifo_ra : Gray5 PORT MAP(
150  d_current => TTS_fifo_ra,
151  d_next => next_TTS_fifo_ra
152  );
153  process(TTS_clk,reset)
154  begin
155  if(reset = '1')then
156  TTS_fifo_waSync <= (others =>'0');
157  TTS_FIFO_re <= '0';
158  NewTTS <= '1';
159  IS_K <= '1';
160  enc_di <= x"bc";
161  TTS_fifo_ra <= (others =>'0');
162  SameTTS_cnt <= "00";
163  elsif(TTS_clk'event and TTS_clk = '1')then
164  if(TTS_clk_div(2) = '1')then
165  TTS_fifo_waSync <= TTS_fifo_wa;
166  if(NewTTS = '1')then
167  SameTTS_cnt <= "00";
168  else
169  SameTTS_cnt <= SameTTS_cnt + 1;
170  end if;
171  if(NewTTS = '1' or SameTTS_cnt = "11")then
172  enc_di <= x"0" & TTS_FIFO_do(3 downto 0);
173  IS_K <= '0';
174  else
175  enc_di <= x"bc"; -- K28.5
176  IS_K <= '1';
177  end if;
178  end if;
179  if(TTS_fifo_waSync /= TTS_fifo_ra and TTS_clk_div(1 downto 0) = "01")then
180  TTS_FIFO_re <= '1';
181  else
182  TTS_FIFO_re <= '0';
183  end if;
184  if(TTS_FIFO_re = '1')then
185  TTS_fifo_ra <= next_TTS_fifo_ra;
186  end if;
187  if(TTS_FIFO_re = '1')then
188  NewTTS <= '1';
189  elsif(TTS_clk_div(2) = '1')then
190  NewTTS <= '0';
191  end if;
192  end if;
193  end process;
194  i_8b10b_enc : ENTITY work.encode_8b10b_lut_base
195  GENERIC MAP (
196  C_HAS_DISP_IN => 0,
197  C_HAS_FORCE_CODE => 0,
198  C_FORCE_CODE_VAL => "1010101010",
199  C_FORCE_CODE_DISP => 0,
200  C_HAS_ND => 0,
201  C_HAS_KERR => 1
202  )
203  PORT MAP (
204  DIN => enc_di,
205  KIN => IS_K,
206  FORCE_DISP => '0',
207  FORCE_CODE => '0',
208  DISP_IN => '0',
209  CE => TTS_clk_div(2),
210  CLK => TTS_clk,
211  DOUT => enc_do,
212  KERR => open,
213  DISP_OUT => open,
214  ND => open
215  );
216  process(TTS_clk)
217  begin
218  if(TTS_clk'event and TTS_clk = '1')then
219  -- 5 state counter for 10bit 8b10char in groups of two
220  if((TTS_clk_div(2) = '1' and local_TTC = '0') or (TTS_clk_div(1 downto 0) = "11" and local_TTC = '1'))then
221  TTS_clk_div <= "000";
222  else
223  TTS_clk_div <= TTS_clk_div + 1;
224  end if;
225  if(TTS_clk_div(1 downto 0) = "11")then
226  if(local_TTC = '0' or bcnt = LastBcnt)then
227  bcnt <= (others => '0');
228  else
229  bcnt <= bcnt + 1;
230  end if;
231  if(local_TTC = '0')then
232  SendBC0 <= '0';
233  elsif(bcnt = LastBcnt)then
234  SendBC0 <= '1';
235  elsif(bcnt(3 downto 0) = x"f")then
236  SendBC0 <= '0';
237  end if;
238  end if;
239  if(local_TTC = '1')then
240 -- sr(1) <= not sr(0);
241 -- sr(0) <= not sr(0);
242  if(TTS_clk_div(0) = '0')then
243  sr(1) <= not sr(0);
244  sr(0) <= not sr(0);
245  elsif(TTS_clk_div(1) = '0' and SendBC0_O = '1')then
246  sr(1) <= not sr(0);
247  sr(0) <= not sr(0);
248  end if;
249  elsif(TTS_clk_div(2) = '1')then
250  sr <= enc_do;
251  else
252  for i in 0 to 3 loop
253  sr(i*2+1 downto i*2) <= sr(i*2+3 downto i*2+2);
254  end loop;
255  end if;
256  end if;
257  end process;
258  i_SendBC0 : ROM32X1
259  generic map (
260  INIT => X"e600ffff")
261  port map (
262  O => SendBC0_O, -- ROM output
263  A0 => bcnt(0), -- ROM address[0]
264  A1 => bcnt(1), -- ROM address[1]
265  A2 => bcnt(2), -- ROM address[2]
266  A3 => bcnt(3), -- ROM address[3]
267  A4 => SendBC0 -- ROM address[4]
268  );
269  i_TTS_out: OBUFDS generic map(IOSTANDARD => "LVDS_25") port map (O => TTS_out_p, OB => TTS_out_n, I => TTS_out);
270  ODDR_inst : ODDR
271  generic map(
272  DDR_CLK_EDGE => "SAME_EDGE", -- "OPPOSITE_EDGE" or "SAME_EDGE"
273  INIT => '0', -- Initial value for Q port ('1' or '0')
274  SRTYPE => "SYNC") -- Reset Type ("ASYNC" or "SYNC")
275  port map (
276  Q => TTS_out, -- 1-bit DDR output
277  C => TTS_clk, -- 1-bit clock input
278  CE => '1', -- 1-bit clock enable input
279  D1 => sr(0), -- 1-bit data input (positive edge)
280  D2 => sr(1), -- 1-bit data input (negative edge)
281  R => '0', -- 1-bit reset input
282  S => '0' -- 1-bit set input
283  );
284 end Behavioral;
285