CMX
CMX firmware code in-line documentation
 All Classes Namespaces Files Functions Variables
glink_encoder.vhd
Go to the documentation of this file.
1 
8 
9 library ieee;
10 use ieee.std_logic_1164.all;
11 use ieee.std_logic_unsigned.all;
12 use ieee.std_logic_arith.all;
13 
14 entity Glink_Encoder is
15 
16 PORT(
17  Reset : in std_logic;
18  Clock : in std_logic;
19 
20  tx_in : in std_logic_vector(19 downto 0);
21  TxData : in std_logic; -- DAV pin, active high for this module
22  TxCntl : in std_logic; -- CAV pin (Not used for CMX, not functional in this module)
23 
24  tx_out : out std_logic_vector(23 downto 0);
25 
26  -- Debug outputs (kept for legacy purposes)
27  TDisparity_Q : out std_logic_vector(5 downto 0);
28  RDPos_Q : out std_logic;
29  RDNeg_Q : out std_logic
30 
31  );
32 END Glink_Encoder;
33 
34 architecture rtl of Glink_Encoder is
35 
36 
37 
38 signal Flag : std_logic := '0'; -- Flagsel always zero
39 signal TDisparity : std_logic_vector(7 downto 0) := (others => '0');
40 
41 begin
42 
43 -----------------------------------------------------------------------------------------------------------
44 -- Calculate accumulated disparity and produce correct output accordingly
45 -----------------------------------------------------------------------------------------------------------
46 
47  process(Clock,Reset)
48  variable tx_uninverted, tx_inverted : std_logic_vector(23 downto 0);
49  variable ones : std_logic_vector(7 downto 0);
50 
51  begin
52  if (Reset = '1') then
53  Flag <= '0';
54  TDisparity <= (OTHERS => '0');
55  tx_out <= x"000FFF";
56 
57  elsif rising_edge(Clock) then
58 
59  -- Construct inverted and non-inverted words
60 
61  if TxData = '0' then -- Idle (send fill frames)
62  tx_uninverted := "110000000000011111111111"; -- Idle Word 1a (C007FF)
63  tx_inverted := "110000000000000111111111"; -- Idle Word 1b (C001FF)
64  else -- DAV asserted (send data frame)
65  Flag <= not (Flag); -- toggle the Flag bit only when a data frame is sent
66  if Flag = '0' then
67  tx_uninverted := "1011" & tx_in;
68  tx_inverted := "0100" & not(tx_in);
69  else
70  tx_uninverted := "1101" & tx_in;
71  tx_inverted := "0010" & not(tx_in);
72  end if;
73  end if;
74 
75  -- calculate word disparity
76 
77  ones := (OTHERS => '0');
78  for i in 0 to 23 loop
79  if (tx_uninverted(i) = '1') then
80  ones := ones+1;
81  end if;
82  end loop;
83 
84  if ones = x"0C" then
85  tx_out <= tx_uninverted; -- If no disparity, send uninverted output
86  TDisparity <= TDisparity; -- No change to total disparity
87  elsif TDisparity(7) = '0' then -- Zero or positive total disparity
88  if ones > x"0C" then
89  tx_out <= tx_inverted; -- Positive word disparity, send inverted output
90  TDisparity <= TDisparity - ones + x"0C";
91  else
92  tx_out <= tx_uninverted; -- negative word disparity, send uninverted output
93  TDisparity <= TDisparity + ones - x"0C";
94  end if;
95  else -- Negative total disparity
96  if ones > x"0C" then
97  tx_out <= tx_uninverted; -- Positive word disparity, send uninverted output
98  TDisparity <= TDisparity + ones - x"0C";
99  else
100  tx_out <= tx_inverted; -- Negative word disparity, send inverted output
101  TDisparity <= TDisparity - ones + x"0C";
102  end if;
103  end if;
104 
105 -- Output to legacy ports
106 
107  TDisparity_Q <= TDisparity (5 downto 0);
108 
109  if ones > x"0C" then
110  RDPos_Q <= '1';
111  else
112  RDPos_Q <= '0';
113  end if;
114 
115  if ones < x"0C" then
116  RDPos_Q <= '0';
117  else
118  RDPos_Q <= '1';
119  end if;
120 
121  end if; -- Rising edge of Clock
122 
123  end process;
124 
125 end;
126 
127 
128 
_library_ IEEEIEEE
Definition: Delay.vhd:6