CMX
CMX firmware code in-line documentation
 All Classes Namespaces Files Functions Variables
glink_interface.vhd
Go to the documentation of this file.
1 
9 
10 library ieee;
11 use ieee.std_logic_1164.all;
12 use ieee.std_logic_unsigned.all;
13 use ieee.std_logic_arith.all;
14 library UNISIM;
15 use UNISIM.VCOMPONENTS.all;
16 
17 entity glink_interface is
18  port (
19  CLK_40MHz : in std_logic;
20  CLK_120MHz : in std_logic;
21  RST : in std_logic;
22  DAQ_IN : in std_logic_vector (19 DOWNTO 0);
23  ROI_IN : in std_logic_vector (19 DOWNTO 0);
24  DAQ_DAV : in std_logic;
25  ROI_DAV : in std_logic;
26  DAQ_BYTE : OUT std_logic_vector (7 downto 0);
27  ROI_BYTE : OUT std_logic_vector (7 downto 0);
28  DAQ_ENCODED_DIAG : OUT std_logic_vector (23 downto 0);
29  daq_byte_out : OUT std_logic_vector (1 downto 0);
30  byte_pos_out : OUT std_logic_vector (5 downto 0);
31  word_sel_out : OUT std_logic_vector (1 downto 0);
32  readout_rst_out : OUT std_logic
33 
34  );
35 end glink_interface;
36 
37 architecture RTL of glink_interface is
38 
39 component Glink_Encoder
40 port (
41  Reset : in std_logic; -- reset
42  Clock : in std_logic; -- clock
43  tx_in : in std_logic_vector(19 downto 0); -- Input data
44  TxData : in std_logic; -- DAV* pin
45  TxCntl : in std_logic; -- CAV* pin (always disabled on CMM)
46  tx_out : out std_logic_vector(23 downto 0); -- Output data --it will be modified.
47  TDisparity_Q : out std_logic_vector(5 downto 0); -- Debug ports
48  RDPos_Q : out std_logic; -- Debug ports
49  RDNeg_Q : out std_logic -- Debug ports
50 
51  );
52 end component;
53 
54 -- constant definitions
55 
56 constant zero_0 : std_logic := '0';
57 
58 attribute keep : string;
59 -- signal definitions
60 
61 signal RST_r : std_logic;
62 signal RST_synced : std_logic;
63 signal readout_rst : std_logic := '0';
64 
65 -- Circular 120 MHz readout buffers
66 -- new signal
67 
68 type circbuf is array (0 to 8) of std_logic_vector (7 downto 0);
69 signal daq_buf : circbuf;
70 signal roi_buf : circbuf;
71 signal word_sel : std_logic_vector(1 downto 0) :="00";
72 
73 signal byte_pos : std_logic_vector(5 downto 0) := (others => '0');
74 
75 -- G-link emulator signal
76 
77 signal roi_encoded : std_logic_vector(23 downto 0);
78 signal daq_encoded : std_logic_vector(23 downto 0);
79 
80 attribute keep of RST_r : signal is "TRUE";
81 
82 begin -- RTL
83 
84 -- diagnostic only
85 
90 
92  port map (
93  Reset => RST,
94  Clock => CLK_40MHz,
95  tx_in => ROI_IN,
96  TxData => ROI_DAV,
97  TxCntl => zero_0,
99  -- Debug ports:
100  TDisparity_Q => open,
101  RDPos_Q => open,
102  RDNeg_Q => open
103 
104  );
105 
107  port map (
108  Reset => RST,
109  Clock => CLK_40MHz,
110  tx_in => DAQ_IN,
111  TxData => DAQ_DAV,
112  TxCntl => zero_0,
113  tx_out => daq_encoded ,
114  -- Debug ports:
115  TDisparity_Q => open,
116  RDPos_Q => open,
117  RDNeg_Q => open
118 
119  );
120 
122 
123 begin
124 
125 if rising_edge (CLK_40MHz) then
126  if word_sel = "00" then
127 
128  roi_buf(0) <= roi_encoded (7 downto 0);
129  roi_buf(1) <= roi_encoded (15 downto 8);
130  roi_buf(2) <= roi_encoded (23 downto 16);
131 
132  daq_buf(0) <= daq_encoded (7 downto 0);
133  daq_buf(1) <= daq_encoded (15 downto 8);
134  daq_buf(2) <= daq_encoded (23 downto 16);
135 
136  elsif word_sel = "01" then
137 
138  roi_buf(3) <= roi_encoded (7 downto 0);
139  roi_buf(4) <= roi_encoded (15 downto 8);
140  roi_buf(5) <= roi_encoded (23 downto 16);
141 
142  daq_buf(3) <= daq_encoded (7 downto 0);
143  daq_buf(4) <= daq_encoded (15 downto 8);
144  daq_buf(5) <= daq_encoded (23 downto 16);
145 
146  else
147 
148  roi_buf(6) <= roi_encoded (7 downto 0);
149  roi_buf(7) <= roi_encoded (15 downto 8);
150  roi_buf(8) <= roi_encoded (23 downto 16);
151 
152  daq_buf(6) <= daq_encoded (7 downto 0);
153  daq_buf(7) <= daq_encoded (15 downto 8);
154  daq_buf(8) <= daq_encoded (23 downto 16);
155 
156 end if;
157 end if;
158 end process; -- capture buffer
159 
160 
162 
163 begin
164 
165 if rising_edge(CLK_40MHz) then
166 
167  if RST_synced = '1' then
168  readout_rst <= '1'; -- reset signal synchronous with the 40 MHz clock
169  else
170  readout_rst <= '0';
171  end if;
172 
173  RST_synced<=RST_r;
174  RST_r<=RST;
175 
176 end if;
177 
178 end process; -- reset_buffer_timing
179 
181 
182 variable phase : integer := 2;
183 
184 begin
185 
186 if rising_edge(CLK_120MHz) then
187 
188  if (readout_rst = '1') then
189  phase := 1;
190  elsif (phase = 8) then
191  phase := 0;
192  else
193  phase := phase + 1;
194  end if;
195 
196  if (phase < 3) then
197  word_sel <= "10"; -- write to word 2 while reading from word 0
198  elsif (phase > 5) then
199  word_sel <= "01"; -- write to word 1 while reading from word 2
200  else
201  word_sel <= "00"; -- write to word 0 while reading from word 1
202  end if;
203 
204  roi_byte <= roi_buf(phase);
205  daq_byte <= daq_buf(phase);
206 
207  byte_pos <= "0000" & word_sel;
208 
209 end if; -- clock 120 MHz
210 
211 end process; -- readout_buffer
212 
213 end RTL;
214 
215