AMC13
Firmwares for the different applications of the AMC13 uTCA board made at Boston University
 All Classes Files Variables
ddr_rport.vhd
1 ----------------------------------------------------------------------------------
2 -- Company:
3 -- Engineer:
4 --
5 -- Create Date: 11:02:29 01/25/2013
6 -- Design Name:
7 -- Module Name: ddr_wportA - 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 Library UNIMACRO;
36 use UNIMACRO.vcomponents.all;
37 
38 entity ddr_rport is
39  Port ( memclk : in STD_LOGIC;
40  sysclk : in STD_LOGIC;
41  reset : in STD_LOGIC;
42  resetSys : in STD_LOGIC;
43  resetMem : in STD_LOGIC;
44  run : in STD_LOGIC;
45  test : in STD_LOGIC_VECTOR(1 downto 0); -- memory test
46  test_block_sent : in STD_LOGIC; -- indicating test writing progress. One pulse every 256 32-bit words
47  test_pause : OUT std_logic;
48  test_status : out STD_LOGIC_VECTOR(63 downto 0);
49 -- each TCP segment sent will have dest IP address (32-bit), TCP port(2x16-bit), SEQ number(32-bit), length(16 bit), time stamp(16 bit), control bits(6)
50 -- and memory starting address of the segment payload data(24-bit)
51 -- for alignment purposes, each entry occupies 32 bytes of memory space (256 bits)
52  TCP_dout : out STD_LOGIC_VECTOR(31 downto 0); -- TCP data are written in unit of 32-bit words
53  TCP_dout_type : out STD_LOGIC_VECTOR(2 downto 0); -- TCP data destination
54  TCP_addr : in STD_LOGIC_VECTOR(28 downto 0); -- 28-26 encoded request source 25-0 address in 64 bit word
55  TCP_length : in STD_LOGIC_VECTOR(10 downto 0); -- in 64 bit word, actual length - 1
56  TCP_dout_valid : out STD_LOGIC;
57  TCP_rqst : in STD_LOGIC;
58  TCP_ack : out STD_LOGIC;
59  TCP_lastword : out STD_LOGIC;
60 -- ipbus signals
61  ipb_clk : in STD_LOGIC;
62  ipb_write : in STD_LOGIC;
63  ipb_strobe : in STD_LOGIC;
64  ipb_addr : in STD_LOGIC_VECTOR(31 downto 0);
65  ipb_rdata : out STD_LOGIC_VECTOR(31 downto 0);
66  ipb_ack : out STD_LOGIC;
67  page_addr : in STD_LOGIC_VECTOR(9 downto 0); -- monitored event memory window
68  cs_out : out STD_LOGIC_VECTOR(511 downto 0);
69 -- ddr3 user interface
70  app_rqst : out STD_LOGIC; -- request to output data
71  app_ack : in STD_LOGIC; -- permission to output data
72  app_rdy : in STD_LOGIC;
73  app_en : out STD_LOGIC;
74  app_rd_data_valid : in STD_LOGIC;
75  app_rd_data : in STD_LOGIC_VECTOR(255 downto 0);
76  app_addr : out STD_LOGIC_VECTOR (27 downto 0) := (others => '0')); -- in unit of 32bit word, bit 27 is not used
77 end ddr_rport;
78 
79 architecture Behavioral of ddr_rport is
80 COMPONENT FIFO_RESET_7S
81  PORT(
82  reset : IN std_logic;
83  clk : IN std_logic;
84  fifo_rst : OUT std_logic;
85  fifo_en : OUT std_logic
86  );
87 END COMPONENT;
88 COMPONENT RAM32x6D
89  PORT(
90  wclk : IN std_logic;
91  rclk : IN std_logic;
92  di : IN std_logic_vector(5 downto 0);
93  we : IN std_logic;
94  wa : IN std_logic_vector(4 downto 0);
95  ra : IN std_logic_vector(4 downto 0);
96  ceReg : IN std_logic;
97  do : OUT std_logic_vector(5 downto 0)
98  );
99 END COMPONENT;
100 COMPONENT RAM32x6Db
101  PORT(
102  wclk : IN std_logic;
103  di : IN std_logic_vector(5 downto 0);
104  we : IN std_logic;
105  wa : IN std_logic_vector(4 downto 0);
106  ra : IN std_logic_vector(4 downto 0);
107  do : OUT std_logic_vector(5 downto 0)
108  );
109 END COMPONENT;
110 signal TCPqueue_we : std_logic := '0';
111 signal ipb_start_addr : std_logic_vector(26 downto 0) := (others => '0');
112 signal ipbReadSyncRegs : std_logic_vector(3 downto 0) := (others => '0');
113 signal ipbRead : std_logic := '0';
114 signal ipbRead_q : std_logic := '0';
115 signal ipb_ack_i : std_logic := '0';
116 signal ipb_rqst : std_logic := '0';
117 signal ipb_raddr : std_logic_vector(23 downto 0) := (others => '0');
118 signal ipb_seq : std_logic_vector(1 downto 0) := (others => '0');
119 signal ipb_rqst_cnt : std_logic_vector(1 downto 0) := (others => '0');
120 signal ipb_rqst_inqueue : std_logic_vector(1 downto 0) := (others => '0');
121 signal ipb_rqstSyncRegs : std_logic_vector(3 downto 0) := (others => '0');
122 signal queue_we : std_logic := '0';
123 signal queue_ceReg : std_logic := '0';
124 signal queue_do_vld : std_logic := '0';
125 signal TCP_addr1and0 : std_logic_vector(1 downto 0) := (others => '0');
126 signal queue_di : std_logic_vector(29 downto 0) := (others => '0');
127 signal queue_do : std_logic_vector(29 downto 0) := (others => '0');
128 signal queue_wa : std_logic_vector(4 downto 0) := (others => '0');
129 signal queue_ra : std_logic_vector(4 downto 0) := (others => '0');
130 signal queue_wap : std_logic_vector(3 downto 0) := (others => '0');
131 signal queue_wa0SyncRegs : std_logic_vector(2 downto 0) := (others => '0');
132 signal queue_wa1SyncRegs : std_logic_vector(2 downto 0) := (others => '0');
133 signal queue_wa2SyncRegs : std_logic_vector(2 downto 0) := (others => '0');
134 signal queue_wa3SyncRegs : std_logic_vector(2 downto 0) := (others => '0');
135 signal Oqueue_di : std_logic_vector(12 downto 0) := (others => '0');
136 signal Oqueue_dop : std_logic_vector(12 downto 0) := (others => '0');
137 signal Oqueue_do : std_logic_vector(12 downto 0) := (others => '0');
138 signal Oqueue_a : std_logic_vector(3 downto 0) := (others => '1');
139 signal Oqueue_re : std_logic := '0';
140 signal Oqueue_do_vld : std_logic := '0';
141 signal test_wc : std_logic_vector(3 downto 0) := (others => '0');
142 signal lfsr: std_logic_vector(31 downto 0) := (others => '0');
143 signal testErr : std_logic := '0';
144 signal testCntr: std_logic_vector(30 downto 0) := (others => '0');
145 signal app_rqst_i : std_logic := '0';
146 signal app_ren : std_logic := '0';
147 signal last_word : std_logic := '0';
148 signal s_demux : std_logic_vector(2 downto 0) := (others => '0');
149 signal data_demux : std_logic_vector(31 downto 0) := (others => '0');
150 signal lengthCntr : std_logic_vector(7 downto 0) := (others => '0');
151 signal rd_cnt : std_logic_vector(4 downto 0) := (others => '0');
152 signal app_addr_i : std_logic_vector(27 downto 0) := (others => '0');
153 signal fifo_en : std_logic := '0';
154 signal fifo_rst : std_logic := '0';
155 signal buf_empty : std_logic_vector(3 downto 0) := (others => '0');
156 signal buf_we : std_logic := '0';
157 signal buf_re : std_logic := '0';
158 signal buf_di : std_logic_vector(255 downto 0) := (others => '0');
159 signal buf_do : std_logic_vector(255 downto 0) := (others => '0');
160 signal TCPqueue_do_vld : std_logic := '0';
161 signal TCP_rrqst : std_logic := '0';
162 signal TCP_raddr : std_logic_vector(23 downto 0) := (others => '0');
163 signal TCP_raddr_end : std_logic_vector(10 downto 0) := (others => '0');
164 signal TCP_rlengthp : std_logic_vector(6 downto 0) := (others => '0');
165 signal TCP_rlength : std_logic_vector(7 downto 0) := (others => '0');
166 signal TCPqueue_di : std_logic_vector(39 downto 0) := (others => '0');
167 signal TCPqueue_dop : std_logic_vector(39 downto 0) := (others => '0');
168 signal TCPqueue_do : std_logic_vector(39 downto 0) := (others => '0');
169 signal TCPqueue_a : std_logic_vector(3 downto 0) := (others => '1');
170 signal Last_TCP_addr : std_logic_vector(10 downto 0) := (others => '0');
171 signal first_test_read : std_logic := '1';
172 signal sel_TCP : std_logic := '0';
173 signal rd_ipb : std_logic := '0';
174 signal ipbus_rbuf_wa : std_logic_vector(4 downto 0) := (others => '0');
175 signal ipbus_rbuf_ra : std_logic_vector(4 downto 0) := (others => '0');
176 signal ipbus_rbuf_di : std_logic_vector(257 downto 0) := (others => '0');
177 signal ipbus_rbuf_do : std_logic_vector(257 downto 0) := (others => '0');
178 signal ipbus_rbuf_wa2SyncRegs : std_logic_vector(2 downto 0) := (others => '0');
179 signal ipbus_rbuf_wa1SyncRegs : std_logic_vector(2 downto 0) := (others => '0');
180 signal ipbus_rbuf_wa0SyncRegs : std_logic_vector(2 downto 0) := (others => '0');
181 signal ipbus_rbuf_wap : std_logic_vector(2 downto 0) := (others => '0');
182 signal ipb_rdata_i : std_logic_vector(31 downto 0) := (others => '0');
183 signal rd_queue_o : std_logic_vector(2 downto 0) := (others => '0');
184 signal rd_queue_q : std_logic_vector(2 downto 0) := (others => '0');
185 signal rd_queue_wa : std_logic_vector(5 downto 0) := (others => '0');
186 signal rd_queue_ra : std_logic_vector(5 downto 0) := (others => '0');
187 signal rd_queue_q_vld : std_logic := '0';
188 signal WRCOUNT : array4x9;
189 signal RDCOUNT : array4x9;
190 signal TCP_dout_valid_i : std_logic := '0';
191 signal TCP_lastword_i : std_logic := '0';
192 COMPONENT chipscope
193  generic (N : integer := 5);
194  PORT(
195  clka : IN std_logic;
196  clkb : IN std_logic;
197  ina : IN std_logic_vector(135 downto 0);
198  inb : IN std_logic_vector(135 downto 0)
199  );
200 END COMPONENT;
201 COMPONENT chipscope1
202  generic (N : integer := 5);
203  PORT(
204  clk : IN std_logic;
205  Din : IN std_logic_vector(303 downto 0)
206  );
207 END COMPONENT;
208 COMPONENT chipscope1b
209  generic (USER2 : boolean := false);
210  PORT(
211  clk : IN std_logic;
212  Din : IN std_logic_vector(303 downto 0)
213  );
214 END COMPONENT;
215 signal chk_SEQ : std_logic := '0';
216 signal bad_SEQ : std_logic := '0';
217 signal NXT_SEQ : std_logic_vector(15 downto 0) := (others => '0');
218 signal ina : std_logic_vector(135 downto 0) := (others => '0');
219 signal inb : std_logic_vector(135 downto 0) := (others => '0');
220 signal cs_in : std_logic_vector(303 downto 0) := (others => '0');
221 begin
222 cs_out(165) <= app_rdy;
223 cs_out(164) <= app_ack;
224 cs_out(163) <= app_rqst_i;
225 cs_out(162 downto 159) <= queue_wap(3 downto 0);
226 cs_out(158 downto 155) <= queue_ra(3 downto 0);
227 cs_out(154 downto 149) <= cs_in(247 downto 242);
228 cs_out(148 downto 105) <= cs_in(166 downto 123);
229 cs_out(104 downto 0) <= cs_in(104 downto 0);
230 --i_chipscope1b: chipscope1b PORT MAP(
231 -- clk => sysclk,
232 -- din => cs_in
233 -- );
234 cs_in(297) <= Oqueue_do_vld;
235 cs_in(296) <= buf_empty(0);
236 cs_in(295 downto 292) <= Oqueue_do(12 downto 9);
237 cs_in(291) <= Oqueue_re;
238 cs_in(290) <= TCP_rrqst;
239 --cs_in(289) <= TCP_addr(29);
240 cs_in(288) <= bad_SEQ;
241 --cs_in() <= ;
242 --cs_in() <= ;
243 --cs_in() <= ;
244 cs_in(280) <= TCP_lastword_i;
245 --cs_in( downto ) <= ;
246 --cs_in( downto ) <= ;
247 cs_in(279 downto 251) <= TCP_addr(28 downto 0);
248 cs_in(250 downto 247) <= buf_empty;
249 cs_in(246) <= queue_we;
250 cs_in(245) <= Oqueue_re;
251 cs_in(244) <= Oqueue_do_vld;
252 cs_in(242) <= buf_re;
253 cs_in(241) <= TCP_rrqst;
254 cs_in(240 downto 239) <= TCP_addr1and0;
255 cs_in(238 downto 228) <= Last_TCP_addr;
256 cs_in(227 downto 220) <= TCP_rlength;
257 cs_in(219 downto 213) <= TCP_rlengthp;
258 cs_in(201 downto 191) <= TCP_raddr_end;
259 cs_in(190 downto 167) <= TCP_raddr;
260 cs_in(166 downto 163) <= TCPqueue_a;
261 cs_in(162 downto 123) <= TCPqueue_do;
262 --cs_in(114 downto 105) <= TCP_length;
263 cs_in(104 downto 73) <= data_demux;
264 cs_in(72 downto 70) <= s_demux;
265 cs_in(69 downto 62) <= lengthCntr;
266 cs_in(61 downto 58) <= Oqueue_a;
267 cs_in(57 downto 45) <= Oqueue_do;
268 cs_in(44 downto 32) <= Oqueue_di;
269 cs_in(31) <= TCP_dout_valid_i;
270 cs_in(30) <= TCPqueue_we;
271 cs_in(29) <= TCPqueue_do_vld;
272 cs_in(28 downto 0) <= queue_di(28 downto 0);
273 test_pause <= test_wc(3) or testErr;
274 ipb_rdata <= ipb_rdata_i;
275 ipb_ack <= ipb_ack_i;
276 app_rqst <= '0' when app_rqst_i = '0' or (last_word = '1' and app_ren = '1') else '1';
277 app_en <= app_ren;
278 app_ren <= app_ack and app_rdy;
279 TCP_ack <= TCPqueue_we;
280 --test_status(5 downto 0) <= rd_queue_ra;
281 --test_status(11 downto 6) <= rd_queue_wa;
282 --test_status(12) <= rd_queue_q_vld;
283 --test_status(13) <= queue_do_vld;
284 --test_status(14) <= Oqueue_do_vld;
285 --test_status(18 downto 15) <= Oqueue_a;
286 --test_status(22 downto 19) <= queue_wa(3 downto 0);
287 --test_status(26 downto 23) <= queue_ra(3 downto 0);
288 --test_status(30 downto 27) <= TCPqueue_a;
289 --test_status(40 downto 31) <= Oqueue_do(9 downto 0);
290 --test_status(48 downto 41) <= lengthCntr;
291 --test_status(51 downto 49) <= s_demux;
292 test_status(31 downto 0) <= testErr & testCntr;
293 --test_status(32) <= '0';
294 --test_status(33) <= first_test_read;
295 --test_status(40 downto 34) <= (others => '0');
296 --test_status(46 downto 41) <= rd_queue_ra;
297 --test_status(52 downto 47) <= rd_queue_wa;
298 --test_status(55 downto 53) <= ipbus_rbuf_ra(2 downto 0);
299 --test_status(58 downto 56) <= ipbus_rbuf_wap;
300 --test_status(62 downto 59) <= test_wc;
301 test_status(63 downto 32) <= (others => '0');
302 app_addr <= app_addr_i;
303 TCP_dout <= data_demux;
304 TCP_dout_valid <= TCP_dout_valid_i;
305 TCP_lastword <= TCP_lastword_i;
306 ipb_ack_i <= '0' when ipbRead = '0' or ipbus_rbuf_wap = ipbus_rbuf_ra(2 downto 0) else ipb_strobe;
307 process(ipb_clk)
308 begin
309  if(ipb_clk'event and ipb_clk = '1')then
310  if(reset = '1')then
311  ipb_rqst <= '0';
312  elsif((ipbRead = '1' and ipbRead_q = '0') or (first_test_read = '1' and test_wc(2) = '1'))then
313  ipb_rqst <= '1';
314  elsif((ipbus_rbuf_ra(2 downto 0) = "001" or ipbus_rbuf_ra(2 downto 0) = "111") and ipb_addr(2 downto 0) = "000" and ipb_ack_i = '1' and test(0) = '0')then
315  ipb_rqst <= '1';
316  elsif((ipbus_rbuf_ra(2 downto 0) = "001" or ipbus_rbuf_ra(2 downto 0) = "111") and testCntr(2 downto 0) = "001")then
317  ipb_rqst <= '1';
318  else
319  ipb_rqst <= '0';
320  end if;
321  if(test(0) = '0')then
322  first_test_read <= '1';
323  elsif(ipb_rqst = '1')then
324  first_test_read <= '0';
325  end if;
326  if(reset = '1' or test(0) = '0')then
327  test_wc <= (others => '0');
328  elsif(test_block_sent = '1' and ipb_rqst = '0')then
329  test_wc <= test_wc + 1;
330  elsif(test_block_sent = '0' and ipb_rqst = '1')then
331  test_wc <= test_wc - 1;
332  end if;
333  if(reset = '0' and test(0) = '0' and ipb_strobe = '1' and ipb_write = '0' and (ipb_addr(27) = '1' or ipb_addr(17) = '1'))then
334  ipbRead <= '1';
335  else
336  ipbRead <= '0';
337  end if;
338  ipbRead_q <= ipbRead;
339  if(ipbRead = '0')then
340  if(ipb_addr(27) = '1')then
341  ipb_start_addr <= ipb_addr(26 downto 0);
342  elsif(ipb_addr(17) = '1')then
343  ipb_start_addr <= page_addr & ipb_addr(16 downto 0);
344  end if;
345  end if;
346  if(ipbRead = '0' and test(0) = '0')then
347  ipbus_rbuf_ra <= (others => '0');
348  elsif((ipb_ack_i = '1' and ipb_addr(2 downto 0) = "111") or testCntr(2 downto 0) = "111")then
349  case ipbus_rbuf_ra(2 downto 0) is
350  when "000" => ipbus_rbuf_ra(2 downto 0) <= "001";
351  when "001" => ipbus_rbuf_ra(2 downto 0) <= "011";
352  when "011" => ipbus_rbuf_ra(2 downto 0) <= "010";
353  when "010" => ipbus_rbuf_ra(2 downto 0) <= "110";
354  when "110" => ipbus_rbuf_ra(2 downto 0) <= "111";
355  when "111" => ipbus_rbuf_ra(2 downto 0) <= "101";
356  when "101" => ipbus_rbuf_ra(2 downto 0) <= "100";
357  when others => ipbus_rbuf_ra(2 downto 0) <= "000";
358  end case;
359  end if;
360  if(ipbRead = '0' and test(0) = '0')then
361  ipbus_rbuf_wa2SyncRegs <= "000";
362  ipbus_rbuf_wa1SyncRegs <= "000";
363  ipbus_rbuf_wa0SyncRegs <= "000";
364  ipbus_rbuf_wap <= "000";
365  else
366  ipbus_rbuf_wa2SyncRegs <= ipbus_rbuf_wa2SyncRegs(1 downto 0) & ipbus_rbuf_wa(2);
367  ipbus_rbuf_wa1SyncRegs <= ipbus_rbuf_wa1SyncRegs(1 downto 0) & ipbus_rbuf_wa(1);
368  ipbus_rbuf_wa0SyncRegs <= ipbus_rbuf_wa0SyncRegs(1 downto 0) & ipbus_rbuf_wa(0);
369  ipbus_rbuf_wap(2) <= ipbus_rbuf_wa2SyncRegs(2);
370  ipbus_rbuf_wap(1) <= ipbus_rbuf_wa1SyncRegs(2);
371  ipbus_rbuf_wap(0) <= ipbus_rbuf_wa0SyncRegs(2);
372  end if;
373  if(reset = '1' or test(0) = '0')then
374  testErr <= '0';
375  testCntr <= (others => '0');
376  elsif(ipbus_rbuf_wap /= ipbus_rbuf_ra(2 downto 0))then
377  if(lfsr /= ipb_rdata_i)then
378  testErr <= '1';
379  end if;
380  if(testErr = '0')then
381  testCntr <= testCntr + 1;
382  end if;
383  end if;
384  if(test(0) = '0')then
385  lfsr <= (others => '0');
386  elsif(ipbus_rbuf_wap /= ipbus_rbuf_ra(2 downto 0))then
387  if(test(1) = '0')then
388  lfsr <= lfsr(30 downto 0) & not(lfsr(31) xor lfsr(21) xor lfsr(1) xor lfsr(0));
389  else
390  lfsr <= lfsr + 1;
391  end if;
392  end if;
393  end if;
394 end process;
395 process(ipb_addr,ipbus_rbuf_do,testCntr)
396 variable s : std_logic_vector(2 downto 0);
397 begin
398  if(test(0) = '0')then
399  s := ipb_addr(2 downto 0);
400  else
401  s := testCntr(2 downto 0);
402  end if;
403  case s is
404  when "000" => ipb_rdata_i <= ipbus_rbuf_do(31 downto 0);
405  when "001" => ipb_rdata_i <= ipbus_rbuf_do(63 downto 32);
406  when "010" => ipb_rdata_i <= ipbus_rbuf_do(95 downto 64);
407  when "011" => ipb_rdata_i <= ipbus_rbuf_do(127 downto 96);
408  when "100" => ipb_rdata_i <= ipbus_rbuf_do(159 downto 128);
409  when "101" => ipb_rdata_i <= ipbus_rbuf_do(191 downto 160);
410  when "110" => ipb_rdata_i <= ipbus_rbuf_do(223 downto 192);
411  when others => ipb_rdata_i <= ipbus_rbuf_do(255 downto 224);
412  end case;
413 end process;
414 g_TCPqueue: for i in 0 to 39 generate
415  i_TCPqueue : SRL16E
416  port map (
417  Q => TCPqueue_dop(i), -- SRL data output
418  A0 => TCPqueue_a(0), -- Select[0] input
419  A1 => TCPqueue_a(1), -- Select[1] input
420  A2 => TCPqueue_a(2), -- Select[2] input
421  A3 => TCPqueue_a(3), -- Select[3] input
422  CE => TCPqueue_we, -- Clock enable input
423  CLK => sysclk, -- Clock input
424  D => TCPqueue_di(i) -- SRL data input
425  );
426 end generate;
427 g_Oqueue: for i in 0 to 12 generate
428  i_Oqueue : SRL16E
429  port map (
430  Q => Oqueue_dop(i), -- SRL data output
431  A0 => Oqueue_a(0), -- Select[0] input
432  A1 => Oqueue_a(1), -- Select[1] input
433  A2 => Oqueue_a(2), -- Select[2] input
434  A3 => Oqueue_a(3), -- Select[3] input
435  CE => queue_we, -- Clock enable input
436  CLK => sysclk, -- Clock input
437  D => Oqueue_di(i) -- SRL data input
438  );
439 end generate;
440 g_queue : for i in 0 to 4 generate
441  i_queue : RAM32X6D
442  port map (
443  wclk => sysclk,
444  rclk => memclk,
445  di => queue_di(i*6+5 downto i*6),
446  we => queue_we ,
447  wa => queue_wa,
448  ra => queue_ra,
449  ceReg => queue_ceReg,
450  do => queue_do(i*6+5 downto i*6)
451  );
452 end generate;
453 queue_ceReg <= '1' when queue_do_vld = '0' or (last_word = '1' and app_ren = '1' and sel_TCP = '1') else '0';
454 TCP_rlengthp <= TCP_rlength(6 downto 0) + "0000011";
455 TCPqueue_di <= Last_TCP_addr & TCP_addr(28 downto 0);
456 process(sysclk)
457 variable dif : std_logic_vector(7 downto 0);
458 begin
459  dif := (Oqueue_do(8 downto 2) & '0') - lengthCntr;
460  if(sysclk'event and sysclk = '1')then
461  if(resetSys = '1' or run = '0' or TCPqueue_we = '1')then
462  TCPqueue_we <= '0';
463  elsif(TCP_rqst = '1')then
464  TCPqueue_we <= '1';
465  end if;
466  Last_TCP_addr <= TCP_addr(10 downto 0) + TCP_length;
467  if(resetSys = '1')then
468  TCPqueue_a <= (others => '1');
469  elsif(TCPqueue_we = '1' and (and_reduce(TCPqueue_a) = '1' or TCPqueue_do_vld = '1'))then
470  TCPqueue_a <= TCPqueue_a + 1;
471  elsif(TCPqueue_we = '0' and and_reduce(TCPqueue_a) = '0' and TCPqueue_do_vld = '0')then
472  TCPqueue_a <= TCPqueue_a - 1;
473  end if;
474  if(resetSys = '1' or (queue_we = '1' and Oqueue_di(9) = '1'))then
475  TCPqueue_do_vld <= '0';
476  elsif(and_reduce(TCPqueue_a) = '0')then
477  TCPqueue_do_vld <= '1';
478  end if;
479  if(TCPqueue_do_vld = '0')then
480  TCPqueue_do <= TCPqueue_dop;
481  end if;
482  if(resetSys = '1')then
483  TCP_rrqst <= '0';
484  elsif(queue_we = '1' and Oqueue_di(9) = '1')then
485  TCP_rrqst <= '0';
486  elsif(TCPqueue_do_vld = '1')then
487  TCP_rrqst <= '1';
488  end if;
489  if(TCP_rrqst = '0')then
490  TCP_addr1and0 <= TCPqueue_do(1 downto 0);
491  TCP_raddr <= TCPqueue_do(25 downto 2); -- in unit of 256 bit word
492  TCP_raddr_end(10 downto 7) <= TCPqueue_do(39 downto 36) - 1;
493  TCP_raddr_end(6 downto 0) <= TCPqueue_do(35 downto 29);
494  if(TCPqueue_do(10 downto 7) = TCPqueue_do(39 downto 36))then
495  TCP_rlength(7) <= '1';
496  TCP_rlength(6 downto 0) <= TCPqueue_do(35 downto 29) - TCPqueue_do(6 downto 0) + 1;
497  else
498  TCP_rlength(7) <= '0';
499  TCP_rlength(6 downto 0) <= "0000000" - TCPqueue_do(6 downto 0);
500  end if;
501  Oqueue_di(12 downto 10) <= TCPqueue_do(28 downto 26);
502  elsif(queue_we = '1')then
503  TCP_addr1and0 <= "00";
504  TCP_raddr(23 downto 5) <= TCP_raddr(23 downto 5) + 1;
505  TCP_raddr(4 downto 0) <= (others => '0');
506  if(TCP_raddr(8 downto 5) = TCP_raddr_end(10 downto 7))then
507  TCP_rlength(7) <= '1';
508  TCP_rlength(6 downto 0) <= (TCP_raddr_end(6 downto 0) + 1);
509  else
510  TCP_rlength <= (others => '0');
511  end if;
512  end if;
513  if(resetSys = '1' or buf_empty(0) = '1' or Oqueue_do_vld = '0' or Oqueue_re = '1' or (lengthCntr = x"01" and s_demux(2 downto 1) < Oqueue_do(1 downto 0)))then
514  TCP_dout_valid_i <= '0';
515  else
516  TCP_dout_valid_i <= '1';
517  end if;
518  case s_demux is
519  when "000" => data_demux <= buf_do(31 downto 0);
520  when "001" => data_demux <= buf_do(63 downto 32);
521  when "010" => data_demux <= buf_do(95 downto 64);
522  when "011" => data_demux <= buf_do(127 downto 96);
523  when "100" => data_demux <= buf_do(159 downto 128);
524  when "101" => data_demux <= buf_do(191 downto 160);
525  when "110" => data_demux <= buf_do(223 downto 192);
526  when others => data_demux <= buf_do(255 downto 224);
527  end case;
528  if(resetSys = '1' or (Oqueue_do(9) = '1' and Oqueue_re = '1'))then
529  chk_SEQ <= '0';
530  elsif(Oqueue_do(12) = '0' and s_demux = "001")then
531  chk_SEQ <= '1';
532  end if;
533  if(Oqueue_do(12) = '0' and s_demux(1 downto 0) = "10")then
534  NXT_SEQ <= data_demux(15 downto 0);
535  end if;
536  if(chk_SEQ = '1' and Oqueue_do(12) = '0' and s_demux(1 downto 0) = "01" and NXT_SEQ /= data_demux(31 downto 16))then
537  bad_SEQ <= '1';
538  else
539  bad_SEQ <= '0';
540  end if;
541  TCP_dout_type <= Oqueue_do(12 downto 10);
542  if(buf_empty(0) = '1' or Oqueue_re = '1')then
543  s_demux <= "000";
544  else
545  s_demux <= s_demux + 1;
546  end if;
547  if(resetSys = '1' or queue_we = '1')then
548  queue_we <= '0';
549  elsif(TCP_rrqst = '1' and Oqueue_a(3 downto 2) /= "01")then
550  queue_we <= '1';
551  else
552  queue_we <= '0';
553  end if;
554  queue_di(23 downto 0) <= TCP_raddr;
555  queue_di(28 downto 24) <= TCP_rlengthp(6 downto 2); -- length in 256-bit words
556  Oqueue_di(1 downto 0) <= TCP_addr1and0;
557  Oqueue_di(8 downto 2) <= TCP_rlength(6 downto 0); -- length in 64-bit words
558  Oqueue_di(9) <= TCP_rlength(7);
559  if(resetSys = '1')then
560  Oqueue_do_vld <= '0';
561  elsif(Oqueue_a /= x"f")then
562  Oqueue_do_vld <= '1';
563  elsif(Oqueue_re = '1')then
564  Oqueue_do_vld <= '0';
565  end if;
566  if(Oqueue_do_vld = '0' or Oqueue_re = '1')then
567  Oqueue_do <= Oqueue_dop;
568  end if;
569  if(resetSys = '1' or Oqueue_re = '1' or (lengthCntr = x"01" and s_demux(2 downto 1) < Oqueue_do(1 downto 0)))then
570  lengthCntr <= x"01";
571  elsif(buf_empty(0) = '0')then
572  lengthCntr <= lengthCntr + 1;
573  end if;
574  if(resetSys = '1')then
575  Oqueue_re <= '0';
576  TCP_lastword_i <= '0';
577  elsif(lengthCntr = (Oqueue_do(8 downto 2) & '0') and buf_empty(0) = '0')then
578  Oqueue_re <= '1';
579  TCP_lastword_i <= Oqueue_do(9);
580  else
581  Oqueue_re <= '0';
582  TCP_lastword_i <= '0';
583  end if;
584  if(resetSys = '1')then
585  Oqueue_a <= (others => '1');
586  elsif(queue_we = '1' and (Oqueue_a = x"f" or (Oqueue_do_vld = '1' and Oqueue_re = '0')))then
587  Oqueue_a <= Oqueue_a + 1;
588  elsif(queue_we = '0' and Oqueue_a /= x"f" and (Oqueue_do_vld = '0' or Oqueue_re = '1'))then
589  Oqueue_a <= Oqueue_a - 1;
590  end if;
591  if(resetSys = '1')then
592  queue_wa <= (others => '0');
593  elsif(queue_we = '1')then
594  queue_wa(1) <= queue_wa(0);
595  queue_wa(0) <= not queue_wa(1);
596  if(queue_wa(1 downto 0) = "10")then
597  queue_wa(3 downto 2) <= queue_wa(3 downto 2) + 1;
598  end if;
599  end if;
600  if(resetSys = '1' or fifo_en = '0' or buf_re = '1')then
601  buf_re <= '0';
602  elsif(s_demux = "110" or (lengthCntr = (Oqueue_do(8 downto 2) & '0') and Oqueue_do(9) = '1'))then
603  buf_re <= '1';
604  end if;
605  end if;
606 end process;
607 process(memclk)
608 begin
609  if(memclk'event and memclk = '1')then
610  if(resetMem = '1')then
611  queue_do_vld <= '0';
612  elsif(queue_wap /= queue_ra(3 downto 0))then
613  queue_do_vld <= '1';
614  elsif(last_word = '1' and app_ren = '1' and sel_TCP = '1')then
615  queue_do_vld <= '0';
616  end if;
617  if(resetMem = '1')then
618  queue_ra <= (others => '0');
619  elsif(queue_wap /= queue_ra(3 downto 0) and (queue_do_vld = '0' or (last_word = '1' and app_ren = '1' and sel_TCP = '1')))then
620  queue_ra(1) <= queue_ra(0);
621  queue_ra(0) <= not queue_ra(1);
622  if(queue_ra(1 downto 0) = "10")then
623  queue_ra(3 downto 2) <= queue_ra(3 downto 2) + 1;
624  end if;
625  end if;
626  if(resetMem = '1')then
627  queue_wa0SyncRegs <= (others => '0');
628  queue_wa1SyncRegs <= (others => '0');
629  queue_wap <= (others => '0');
630  else
631  queue_wa0SyncRegs <= queue_wa0SyncRegs(1 downto 0) & queue_wa(0);
632  queue_wa1SyncRegs <= queue_wa1SyncRegs(1 downto 0) & queue_wa(1);
633  queue_wap(0) <= queue_wa0SyncRegs(2);
634  queue_wap(1) <= queue_wa1SyncRegs(2);
635  if(queue_wap(1) = '1' and queue_wa1SyncRegs(2) = '0')then
636  queue_wap(3 downto 2) <= queue_wap(3 downto 2) + 1;
637  end if;
638  end if;
639  if(resetMem = '1' or (last_word = '1' and app_ren = '1'))then
640  app_rqst_i <= '0';
641  elsif(queue_do_vld = '1' or or_reduce(ipb_rqst_cnt) = '1')then
642  app_rqst_i <= '1';
643  end if;
644  if(app_rqst_i = '0')then
645  if(queue_do_vld = '1' and queue_do(28 downto 24) = "00001")then
646  last_word <= '1';
647  else
648  last_word <= '0';
649  end if;
650  elsif(app_ren = '1')then
651  if(rd_cnt = "00010")then
652  last_word <= '1';
653  else
654  last_word <= '0';
655  end if;
656  end if;
657  if(app_rqst_i = '0')then
658  if(or_reduce(ipb_rqst_cnt) = '1')then
659  app_addr_i <= '0' & ipb_raddr & "000";
660  rd_cnt <= "00100";
661  sel_TCP <= '0';
662  elsif(queue_do_vld = '1')then
663  app_addr_i <= '0' & queue_do(23 downto 0) & "000";
664  rd_cnt <= queue_do(28 downto 24); -- rd_cnt is number of read in unit of 256 bit word(equivalent to eight 32-bit word)
665  sel_TCP <= '1';
666  end if;
667  elsif(app_ren = '1')then
668  app_addr_i(24 downto 3) <= app_addr_i(24 downto 3) + 1;-- should never carry to app_addr_i(25), must wrap around
669  rd_cnt <= rd_cnt - 1;
670  end if;
671  end if;
672 end process;
673 i_FIFO_RESET_7S: FIFO_RESET_7S PORT MAP(
674  reset => resetMem,
675  clk => memclk ,
676  fifo_rst => fifo_rst,
677  fifo_en => fifo_en
678  );
679 g_rbuf : for i in 0 to 3 generate
680  i_rbuf : FIFO_DUALCLOCK_MACRO
681  generic map (
682  DEVICE => "7SERIES", -- Target Device: "VIRTEX5", "VIRTEX6", "7SERIES"
683  ALMOST_FULL_OFFSET => X"0080", -- Sets almost full threshold
684  ALMOST_EMPTY_OFFSET => X"0080", -- Sets the almost empty threshold
685  DATA_WIDTH => 64, -- Valid values are 1-72 (37-72 only valid when FIFO_SIZE="36Kb")
686  FIFO_SIZE => "36Kb", -- Target BRAM, "18Kb" or "36Kb"
687  FIRST_WORD_FALL_THROUGH => TRUE) -- Sets the FIFO FWFT to TRUE or FALSE
688  port map (
689  ALMOSTEMPTY => open, -- 1-bit output almost empty
690  ALMOSTFULL => open, -- 1-bit output almost full
691  DO => buf_do(i*64+63 downto i*64), -- Output data, width defined by DATA_WIDTH parameter
692  EMPTY => buf_empty(i), -- 1-bit output empty
693  FULL => open, -- 1-bit output full
694  RDCOUNT => RDCOUNT(i), -- Output read count, width determined by FIFO depth
695  RDERR => open, -- 1-bit output read error
696  WRCOUNT => WRCOUNT(i), -- Output write count, width determined by FIFO depth
697  WRERR => open, -- 1-bit output write error
698  DI => buf_di(i*64+63 downto i*64), -- Input data, width defined by DATA_WIDTH parameter
699  RDCLK => sysclk, -- 1-bit input read clock
700  RDEN => buf_re, -- 1-bit input read enable
701  RST => fifo_rst, -- 1-bit input reset
702  WRCLK => memclk, -- 1-bit input write clock
703  WREN => buf_we -- 1-bit input write enable
704  );
705 end generate;
706 buf_di <= app_rd_data;
707 buf_we <= '1' when app_rd_data_valid = '1' and fifo_en = '1' and rd_queue_q(2) = '1' else '0';
708 g_ipbus_rbuf : for i in 0 to 42 generate
709  i_ipbus_rbuf : RAM32x6Db PORT MAP(
710  wclk => memclk ,
711  di => ipbus_rbuf_di(i*6+5 downto i*6),
712  we => app_rd_data_valid,
713  wa => ipbus_rbuf_wa,
714  ra => ipbus_rbuf_ra,
715  do => ipbus_rbuf_do(i*6+5 downto i*6)
716  );
717 end generate;
718 ipbus_rbuf_di(255 downto 0) <= app_rd_data;
719 process(memclk,ipbRead,test)
720 begin
721  if(ipbRead = '0' and test(0) = '0')then
722  ipbus_rbuf_wa(2 downto 0) <= (others => '0');
723  ipb_rqstSyncRegs <= (others => '0');
724  ipb_rqst_cnt <= (others => '0');
725  elsif(memclk'event and memclk = '1')then
726  if(app_rd_data_valid = '1' and ipb_seq = rd_queue_q(1 downto 0) and rd_queue_q_vld = '1' and rd_queue_q(2) = '0')then
727  case ipbus_rbuf_wa(2 downto 0) is
728  when "000" => ipbus_rbuf_wa(2 downto 0) <= "001";
729  when "001" => ipbus_rbuf_wa(2 downto 0) <= "011";
730  when "011" => ipbus_rbuf_wa(2 downto 0) <= "010";
731  when "010" => ipbus_rbuf_wa(2 downto 0) <= "110";
732  when "110" => ipbus_rbuf_wa(2 downto 0) <= "111";
733  when "111" => ipbus_rbuf_wa(2 downto 0) <= "101";
734  when "101" => ipbus_rbuf_wa(2 downto 0) <= "100";
735  when others => ipbus_rbuf_wa(2 downto 0) <= "000";
736  end case;
737  end if;
738  ipb_rqstSyncRegs <= ipb_rqstSyncRegs(2 downto 0) & ipb_rqst;
739  if(ipb_rqstSyncRegs(3 downto 2) = "10" and (app_rqst_i = '1' or or_reduce(ipb_rqst_cnt) = '0'))then
740  ipb_rqst_cnt <= ipb_rqst_cnt + 1;
741  elsif(ipb_rqstSyncRegs(3 downto 2) /= "10" and app_rqst_i = '0' and or_reduce(ipb_rqst_cnt) = '1')then
742  ipb_rqst_cnt <= ipb_rqst_cnt - 1;
743  end if;
744  end if;
745 end process;
746 ipbus_rbuf_wa(4) <= rd_queue_q(2);
747 process(memclk)
748 begin
749  if(memclk'event and memclk = '1')then
750  ipbReadSyncRegs <= ipbReadSyncRegs(2 downto 0) & ipbRead;
751  if(ipbReadSyncRegs(3 downto 2) = "10")then -- ipb_seq is used to eject stalemate read data
752  ipb_seq <= ipb_seq + 1;
753  end if;
754  if(resetMem = '1')then
755  ipb_raddr <= (others => '0');
756  elsif(ipbReadSyncRegs(3 downto 2) = "01")then
757  ipb_raddr <= ipb_start_addr(26 downto 3);
758  elsif(sel_TCP = '0' and app_ren = '1')then
759  ipb_raddr <= ipb_raddr + 1;
760  end if;
761  if(resetMem = '1')then
762  rd_queue_wa <= (others => '0');
763  elsif(app_ren = '1')then
764  rd_queue_wa <= rd_queue_wa + 1;
765  end if;
766  if(resetMem = '1')then
767  rd_queue_ra <= (others => '0');
768  elsif(rd_queue_wa /= rd_queue_ra and (app_rd_data_valid = '1' or rd_queue_q_vld = '0'))then
769  rd_queue_ra <= rd_queue_ra + 1;
770  end if;
771  if(resetMem = '1')then
772  rd_queue_q_vld <= '0';
773  elsif(rd_queue_wa /= rd_queue_ra)then
774  rd_queue_q_vld <= '1';
775  elsif(app_rd_data_valid = '1')then
776  rd_queue_q_vld <= '0';
777  end if;
778  if(app_rd_data_valid = '1' or rd_queue_q_vld = '0')then
779  rd_queue_q <= rd_queue_o;
780  end if;
781  end if;
782 end process;
783 i_rd_queue : RAM64M
784  port map (
785  DOA => rd_queue_o(0), -- Read port A 1-bit output
786  DOB => rd_queue_o(1), -- Read port B 1-bit output
787  DOC => rd_queue_o(2), -- Read port C 1-bit output
788  DOD => open, -- Read/Write port D 1-bit output
789  ADDRA => rd_queue_ra, -- Read port A 6-bit address input
790  ADDRB => rd_queue_ra, -- Read port B 6-bit address input
791  ADDRC => rd_queue_ra, -- Read port C 6-bit address input
792  ADDRD => rd_queue_wa, -- Read/Write port D 6-bit address input
793  DIA => ipb_seq(0), -- RAM 1-bit data write input addressed by ADDRD,
794  -- read addressed by ADDRA
795  DIB => ipb_seq(1), -- RAM 1-bit data write input addressed by ADDRD,
796  -- read addressed by ADDRB
797  DIC => sel_TCP, -- RAM 1-bit data write input addressed by ADDRD,
798  -- read addressed by ADDRC
799  DID => '0', -- RAM 1-bit data write input addressed by ADDRD,
800  -- read addressed by ADDRD
801  WCLK => memclk , -- Write clock input
802  WE => app_ren -- Write enable input
803  );
804 end Behavioral;