OpenCores
URL https://opencores.org/ocsvn/pcie_mini/pcie_mini/trunk

Subversion Repositories pcie_mini

[/] [pcie_mini/] [trunk/] [example_design/] [xilinx_pcie2wb.vhd] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 buenos
----------------------------------------------------------------------------------
2
-- Company:
3
-- Engineer: Istvan Nagy, buenos@freemail.hu
4
-- 
5 3 buenos
-- Create Date:    05/30/2010
6
-- Modify date:    04/26/2011
7 2 buenos
-- Design Name:    pcie_mini
8
-- Module Name:    xilinx_pcie2wb - Behavioral 
9 3 buenos
-- Version:        1.1
10 2 buenos
-- Project Name: 
11
-- Target Devices: Xilinx Series-5/6/7 FPGAs
12
-- Tool versions: ISE-DS 12.1
13
-- Description: 
14
--  PCI-express endpoint block, transaction layer logic and back-end logic. The main 
15
--  purpose of this file is to make a useable back-end interface and handle flow control
16
--  for the xilinx auto-generated PCIe endpoint IP.
17
--  The PCIe endpoint implements one 256MByte memory BAR (Base Address Register).
18
--  This 256MBytes size is set up in the core config, and also hardcoded in this 
19
--  file (search for: "256MBytes").
20
--  This 1 BAR is implemented as a Wishbone master interface with byte addressing,
21
--  where address [x:2] shows DWORD address, while sel[3:0] decodes the 2 LSBs.
22
--  ADDRESSES ARE BYTE ADDRESSES. 
23
--  The lower address bits are usually zero, so the slave (MCB) has to select bytes based 
24
--  on the byte select signals: sel[3:0]. The output address of the core contails the 2 
25
--  LSBs as well. The core was only tested with 32-bit accesses, byte-wide might work or not.
26
--  The TLP logic is capable of handling up to 1k bytes (256 DWORDs) payload data in a 
27
--  single PCIe transaction, and can handle only one request at a time. If a new request 
28
--  is arriving while processing the previous one (e.g. getting the data from a wishbone 
29
--  read), then the state machine will not process it immediately, or it will hang. So 
30
--  the user software has to wait for the previous read completion before issueing a new 
31
--  request. The multiple DWORDs are handled separately by the WB statemachine.
32
--  Performance: WishBone bus: 62.5MHz, 32bit, 2clk/access -> 125MBytes/sec. The maximum 
33
--  data throughput can be achieved when using the maximum data payload (block).
34
--  The core uses INTA wirtual wire to signal interrupts.
35
--  
36
-- x1 PCIe, legacy endpoint, uses a 100MHz ref clock. The generated core had to
37
-- be edited manually to support 100MHz, as per Xilinx AR#33761.
38
--
39
-- Dependencies: The CoreGenerator's configured PCIe core is included.
40
--  If we generate a new pcie endpoint, then copy the new files from the source
41
--  directory into the project's directory, and copy the generic section of the "pcie" 
42
--  from the file: xilinx_pcie_1_1_ep_s6.vhd, into this file.
43
-- Synthesis: Set the "FSM Encoding Algorithm" to "user".
44
--
45
-- Revision: 
46
-- Revision 0.01 - File Created
47
 
48
--
49
----------------------------------------------------------------------------------
50
library IEEE;
51
use IEEE.STD_LOGIC_1164.ALL;
52
use IEEE.STD_LOGIC_ARITH.ALL;
53
use IEEE.STD_LOGIC_UNSIGNED.ALL;
54
 
55
-- Uncomment the following library declaration if instantiating
56
-- any Xilinx primitives in this code.
57
library UNISIM;
58
use UNISIM.VComponents.all;
59
 
60
 
61
 
62
entity xilinx_pcie2wb is
63
    Port ( --FPGA PINS(EXTERNAL):
64
                         pci_exp_txp             : out std_logic;
65
                         pci_exp_txn             : out std_logic;
66
                         pci_exp_rxp             : in  std_logic;
67
                         pci_exp_rxn             : in  std_logic;
68
                         sys_clk_n                 : in  std_logic;
69
                         sys_clk_p                 : in  std_logic;
70
                         sys_reset_n             : in  std_logic;
71
                         --ON CHIP PORTS:
72
                         --DATA BUS for BAR0 (wishbone):
73
                         pcie_bar0_wb_data_o : out std_logic_vector(31 downto 0);
74
                         pcie_bar0_wb_data_i : in std_logic_vector(31 downto 0);
75
                         pcie_bar0_wb_addr_o : out std_logic_vector(27 downto 0);
76
                         pcie_bar0_wb_cyc_o : out std_logic;
77
                         pcie_bar0_wb_stb_o : out std_logic;
78
                         pcie_bar0_wb_wr_o : out std_logic;
79
                         pcie_bar0_wb_ack_i : in std_logic;
80
                         pcie_bar0_wb_clk_o : out std_logic; --62.5MHz          
81
                         pcie_bar0_wb_sel_o : out std_logic_vector(3 downto 0);
82
                         --OTHER:
83
                         pcie_irq : in std_logic;
84
                         pcie_resetout  : out std_logic --active high
85
                        );
86
end xilinx_pcie2wb;
87
 
88
 
89
 
90
 
91
architecture Behavioral of xilinx_pcie2wb is
92
 
93
 
94
 
95
 
96
   -- Internal Signals ------------------------------------------------------------
97
        --SIGNAL dummy : std_logic_vector(15 downto 0); --write data bus
98
        SIGNAL          cfg_do  :               std_logic_vector(31     downto  0);
99
        SIGNAL          cfg_rd_wr_done_n        :               std_logic;
100
        SIGNAL          cfg_dwaddr      :               std_logic_vector(9      downto  0);
101
        SIGNAL          cfg_rd_en_n     :               std_logic;
102
        SIGNAL          cfg_err_ur_n    :               std_logic;
103
        SIGNAL          cfg_err_cor_n   :               std_logic;
104
        SIGNAL          cfg_err_ecrc_n  :               std_logic;
105
        SIGNAL          cfg_err_cpl_timeout_n   :               std_logic;
106
        SIGNAL          cfg_err_cpl_abort_n     :               std_logic;
107
        SIGNAL          cfg_err_posted_n        :               std_logic;
108
        SIGNAL          cfg_err_locked_n        :               std_logic;
109
        SIGNAL          cfg_err_tlp_cpl_header  :               std_logic_vector(47     downto  0);
110
        SIGNAL          cfg_err_cpl_rdy_n       :               std_logic;
111
        SIGNAL          cfg_interrupt_n :               std_logic;
112
        SIGNAL          cfg_interrupt_rdy_n     :               std_logic;
113
        SIGNAL          cfg_interrupt_assert_n  :               std_logic;
114
        SIGNAL          cfg_interrupt_do        :               std_logic_vector(7      downto  0);
115
        SIGNAL          cfg_interrupt_di        :               std_logic_vector(7      downto  0);
116
        SIGNAL          cfg_interrupt_mmenable  :               std_logic_vector(2      downto  0);
117
        SIGNAL          cfg_interrupt_msienable :               std_logic;
118
        SIGNAL          cfg_turnoff_ok_n        :               std_logic;
119
        SIGNAL          cfg_to_turnoff_n        :               std_logic;
120
        SIGNAL          cfg_pm_wake_n   :               std_logic;
121
        SIGNAL          cfg_pcie_link_state_n   :               std_logic_vector(2      downto  0);
122
        SIGNAL          cfg_trn_pending_n       :               std_logic;
123
        SIGNAL          cfg_dsn :               std_logic_vector(63     downto  0);
124
        SIGNAL          cfg_bus_number  :               std_logic_vector(7      downto  0);
125
        SIGNAL          cfg_device_number       :               std_logic_vector(4      downto  0);
126
        SIGNAL          cfg_function_number     :               std_logic_vector(2      downto  0);
127
        SIGNAL          cfg_status      :               std_logic_vector(15     downto  0);
128
        SIGNAL          cfg_command     :               std_logic_vector(15     downto  0);
129
        SIGNAL          cfg_dstatus     :               std_logic_vector(15     downto  0);
130
        SIGNAL          cfg_dcommand    :               std_logic_vector(15     downto  0);
131
        SIGNAL          cfg_lstatus     :               std_logic_vector(15     downto  0);
132
        SIGNAL          cfg_lcommand    :               std_logic_vector(15     downto  0);
133
    -- System Interface
134
        SIGNAL      sys_clk                 :   std_logic;
135
        SIGNAL      trn_clk                 :  std_logic;
136
        SIGNAL      trn_reset_n             :  std_logic;
137
        SIGNAL      received_hot_reset      :  std_logic;
138
    -- Transaction (TRN) Interface
139
        SIGNAL      trn_lnk_up_n            :  std_logic;
140
        --      data interface Tx                                       
141
        SIGNAL          trn_td  :               std_logic_vector(31     downto  0);
142
        SIGNAL          trn_tsof_n      :               std_logic;
143
        SIGNAL          trn_teof_n      :               std_logic;
144
        SIGNAL          trn_tsrc_rdy_n  :               std_logic;
145
        SIGNAL          trn_tdst_rdy_n  :               std_logic;
146
        SIGNAL          trn_terr_drop_n :               std_logic;
147
        SIGNAL          trn_tsrc_dsc_n  :               std_logic;
148
        SIGNAL          trn_terrfwd_n   :               std_logic;
149
        SIGNAL          trn_tbuf_av     :               std_logic_vector(5      downto  0);
150
        SIGNAL          trn_tstr_n      :               std_logic;
151
        SIGNAL          trn_tcfg_req_n  :               std_logic;
152
        SIGNAL          trn_tcfg_gnt_n  :               std_logic;
153
        --      data interface Rx                                       
154
        SIGNAL          trn_rd  :               std_logic_vector(31     downto  0);
155
        SIGNAL          trn_rsof_n      :               std_logic;
156
        SIGNAL          trn_reof_n      :               std_logic;
157
        SIGNAL          trn_rsrc_rdy_n  :               std_logic;
158
        SIGNAL          trn_rsrc_dsc_n  :               std_logic;
159
        SIGNAL          trn_rdst_rdy_n  :               std_logic;
160
        SIGNAL          trn_rerrfwd_n   :               std_logic;
161
        SIGNAL          trn_rnp_ok_n    :               std_logic;
162
        SIGNAL          trn_rbar_hit_n  :               std_logic_vector(6      downto  0);
163
        -- flow control
164
        SIGNAL          trn_fc_sel      :               std_logic_vector(2      downto  0);
165
        SIGNAL          trn_fc_nph      :               std_logic_vector(7      downto  0);
166
        SIGNAL          trn_fc_npd      :               std_logic_vector(11     downto  0);
167
        SIGNAL          trn_fc_ph       :               std_logic_vector(7      downto  0);
168
        SIGNAL          trn_fc_pd       :               std_logic_vector(11     downto  0);
169
        SIGNAL          trn_fc_cplh     :               std_logic_vector(7      downto  0);
170
        SIGNAL          trn_fc_cpld     :               std_logic_vector(11     downto  0);
171
 
172
        SIGNAL   start_read_wb0 :               std_logic;
173
        SIGNAL   start_write_wb0        :               std_logic;
174
        SIGNAL   wb_transaction_complete        :               std_logic;
175
        SIGNAL   pcie_bar0_wb_data_i_latched    :               std_logic_vector(31     downto  0);
176
        SIGNAL   pcie_bar0_wb_data_o_feed       :               std_logic_vector(31     downto  0);
177
        SIGNAL   pcie_bar0_wb_addr_o_feed       :               std_logic_vector(27     downto  0);
178
        SIGNAL   pcie_bar0_wb_sel_o_feed        :               std_logic_vector(3      downto  0);
179
        SIGNAL   start_read_wb1 :               std_logic;
180
        SIGNAL   start_write_wb1        :               std_logic;
181
        SIGNAL   rd_data_ready_wb1      :               std_logic;
182
 
183
    SIGNAL   pcie_just_received_a_new_tlp      :  std_logic  ;
184
    SIGNAL   pcie_start_reading_rx_tlp      :  std_logic  ;
185
    SIGNAL   pcie_there_is_a_new_tlp_to_transmit      :  std_logic  ;
186
    SIGNAL   rxtlp_decodedaddress      :  std_logic_vector(31 downto 0);
187
    SIGNAL   tlp_payloadsize_dwords      :  std_logic_vector(7 downto 0);
188
    SIGNAL   rxtlp_firstdw_be      :  std_logic_vector(3 downto 0);
189
    SIGNAL   rxtlp_lastdw_be      :  std_logic_vector(3 downto 0);
190
    SIGNAL   rxtlp_requesterid      :  std_logic_vector(15 downto 0);
191
    SIGNAL   tlp_state      :  std_logic_vector(7 downto 0);
192
    SIGNAL   tlp_state_copy      :  std_logic_vector(7 downto 0);
193
    SIGNAL   rxtlp_data_0      :  std_logic_vector(31 downto 0);
194
    SIGNAL   rxtlp_data_1      :  std_logic_vector(31 downto 0);
195
    SIGNAL   rxtlp_data_2      :  std_logic_vector(31 downto 0);
196
    SIGNAL   rxtlp_data_3      :  std_logic_vector(31 downto 0);
197
    SIGNAL   rxtlp_data_4      :  std_logic_vector(31 downto 0);
198
    SIGNAL   rxtlp_data_5      :  std_logic_vector(31 downto 0);
199
    SIGNAL   rxtlp_data_6      :  std_logic_vector(31 downto 0);
200
    SIGNAL   rxtlp_data_7      :  std_logic_vector(31 downto 0);
201
    SIGNAL   txtlp_data_0      :  std_logic_vector(31 downto 0);
202
    SIGNAL   txtlp_data_1      :  std_logic_vector(31 downto 0);
203
    SIGNAL   txtlp_data_2      :  std_logic_vector(31 downto 0);
204
    SIGNAL   txtlp_data_3      :  std_logic_vector(31 downto 0);
205
    SIGNAL   txtlp_data_4      :  std_logic_vector(31 downto 0);
206
    SIGNAL   txtlp_data_5      :  std_logic_vector(31 downto 0);
207
    SIGNAL   txtlp_data_6      :  std_logic_vector(31 downto 0);
208
    SIGNAL   txtlp_data_7      :  std_logic_vector(31 downto 0);
209
    SIGNAL   pcie_tlp_tx_complete       :  std_logic;
210
 
211
         SIGNAL  pcieirq_state    :  std_logic_vector(7 downto 0);
212
         SIGNAL  txtrn_counter   :  std_logic_vector(7 downto 0);
213
         SIGNAL  trn_rx_counter   :  std_logic_vector(7 downto 0);
214
         SIGNAL cfg_completer_id  :  std_logic_vector(15 downto 0);
215
         SIGNAL wb0_state :   std_logic_vector(7 downto 0);
216
         SIGNAL epif_tx_state :   std_logic_vector(7 downto 0);
217
         SIGNAL epif_rx_state :   std_logic_vector(7 downto 0);
218
         SIGNAL bit10 :   std_logic_vector(1 downto 0);
219
 
220
  SIGNAL bram_rxtlp_we : std_logic_vector(0 downto 0);
221
  SIGNAL bram_rxtlp_writeaddress : std_logic_vector(31 downto 0);
222
  SIGNAL bram_rxtlp_writedata : std_logic_vector(31 downto 0);
223
  SIGNAL bram_rxtlp_readaddress : std_logic_vector(31 downto 0);
224
  SIGNAL bram_rxtlp_readdata : std_logic_vector(31 downto 0);
225
  SIGNAL bram_txtlp_we : std_logic_vector(0 downto 0);
226
  SIGNAL bram_txtlp_writeaddress : std_logic_vector(8 downto 0);
227
  SIGNAL bram_txtlp_writedata : std_logic_vector(31 downto 0);
228
  SIGNAL bram_txtlp_readaddress : std_logic_vector(31 downto 0);
229
  SIGNAL bram_txtlp_readdata : std_logic_vector(31 downto 0);
230
 
231
  SIGNAL tlp_datacount :   std_logic_vector(7 downto 0);
232
  --SIGNAL bram_rxtlp_firstdata_address : std_logic_vector(8 downto 0);
233
  SIGNAL rxtlp_header_dw1 : std_logic_vector(31 downto 0);
234
  SIGNAL rxtlp_header_dw2 : std_logic_vector(31 downto 0);
235
  SIGNAL rxtlp_header_dw3 : std_logic_vector(31 downto 0);
236
  SIGNAL rxtlp_header_dw4 : std_logic_vector(31 downto 0);
237
  SIGNAL flag1 :      std_logic;
238
  SIGNAL rxdw1_23_0  : std_logic_vector(23 downto 0);
239
  SIGNAL pcie_rxtlp_tag  : std_logic_vector(7 downto 0);
240
  SIGNAL pciewb_localreset_n :      std_logic;
241
  SIGNAL cfg_interrupt_assert_n_1 :      std_logic;
242
  SIGNAL trn_tsrc_rdy_n_1 :      std_logic;
243
  SIGNAL trn_tsof_n1 :      std_logic;
244 3 buenos
  SIGNAL rcompl_bytecount_field  : std_logic_vector(9 downto 0);
245
  SIGNAL rxstm_readytoroll :      std_logic;
246
  SIGNAL tlpstm_isin_idle :      std_logic;
247 2 buenos
 
248
 
249
 
250
 
251
 
252
        -- COMPONENT DECLARATIONS (introducing the IPs) --------------------------------
253
 
254
  --this is the pcie endpoint core from coregenerator.
255
        --Core name: Xilinx Spartan-6 Integrated
256
        --Block for PCI Express
257
        --Version: 1.2
258
        --Release Date: September 16, 2009. ISE DS 11.4
259
  component pcie is
260
  generic (
261
    TL_TX_RAM_RADDR_LATENCY           : integer    := 0;
262
    TL_TX_RAM_RDATA_LATENCY           : integer    := 2;
263
    TL_RX_RAM_RADDR_LATENCY           : integer    := 0;
264
    TL_RX_RAM_RDATA_LATENCY           : integer    := 2;
265
    TL_RX_RAM_WRITE_LATENCY           : integer    := 0;
266
    VC0_TX_LASTPACKET                 : integer    := 14;
267
    VC0_RX_RAM_LIMIT                  : bit_vector := x"7FF";
268
    VC0_TOTAL_CREDITS_PH              : integer    := 32;
269
    VC0_TOTAL_CREDITS_PD              : integer    := 211;
270
    VC0_TOTAL_CREDITS_NPH             : integer    := 8;
271
    VC0_TOTAL_CREDITS_CH              : integer    := 40;
272
    VC0_TOTAL_CREDITS_CD              : integer    := 211;
273
    VC0_CPL_INFINITE                  : boolean    := TRUE;
274
    BAR0                              : bit_vector := x"F0000000";
275
    BAR1                              : bit_vector := x"00000000";
276
    BAR2                              : bit_vector := x"00000000";
277
    BAR3                              : bit_vector := x"00000000";
278
    BAR4                              : bit_vector := x"00000000";
279
    BAR5                              : bit_vector := x"00000000";
280
    EXPANSION_ROM                     : bit_vector := "0000000000000000000000";
281
    DISABLE_BAR_FILTERING             : boolean    := FALSE;
282
    DISABLE_ID_CHECK                  : boolean    := FALSE;
283
    TL_TFC_DISABLE                    : boolean    := FALSE;
284
    TL_TX_CHECKS_DISABLE              : boolean    := FALSE;
285
    USR_CFG                           : boolean    := FALSE;
286
    USR_EXT_CFG                       : boolean    := FALSE;
287
    DEV_CAP_MAX_PAYLOAD_SUPPORTED     : integer    := 2;
288
    CLASS_CODE                        : bit_vector := x"068000";
289
    CARDBUS_CIS_POINTER               : bit_vector := x"00000000";
290
    PCIE_CAP_CAPABILITY_VERSION       : bit_vector := x"1";
291
    PCIE_CAP_DEVICE_PORT_TYPE         : bit_vector := x"1";
292
    PCIE_CAP_SLOT_IMPLEMENTED         : boolean    := FALSE;
293
    PCIE_CAP_INT_MSG_NUM              : bit_vector := "00000";
294
    DEV_CAP_PHANTOM_FUNCTIONS_SUPPORT : integer    := 0;
295
    DEV_CAP_EXT_TAG_SUPPORTED         : boolean    := FALSE;
296
    DEV_CAP_ENDPOINT_L0S_LATENCY      : integer    := 7;
297
    DEV_CAP_ENDPOINT_L1_LATENCY       : integer    := 7;
298
    SLOT_CAP_ATT_BUTTON_PRESENT       : boolean    := FALSE;
299
    SLOT_CAP_ATT_INDICATOR_PRESENT    : boolean    := FALSE;
300
    SLOT_CAP_POWER_INDICATOR_PRESENT  : boolean    := FALSE;
301
    DEV_CAP_ROLE_BASED_ERROR          : boolean    := TRUE;
302
    LINK_CAP_ASPM_SUPPORT             : integer    := 1;
303
    LINK_CAP_L0S_EXIT_LATENCY         : integer    := 7;
304
    LINK_CAP_L1_EXIT_LATENCY          : integer    := 7;
305
    LL_ACK_TIMEOUT                    : bit_vector := x"0204";
306
    LL_ACK_TIMEOUT_EN                 : boolean    := FALSE;
307
    LL_REPLAY_TIMEOUT                 : bit_vector := x"0204";
308
    LL_REPLAY_TIMEOUT_EN              : boolean    := FALSE;
309
    MSI_CAP_MULTIMSGCAP               : integer    := 0;
310
    MSI_CAP_MULTIMSG_EXTENSION        : integer    := 0;
311
    LINK_STATUS_SLOT_CLOCK_CONFIG     : boolean    := FALSE;
312
    PLM_AUTO_CONFIG                   : boolean    := FALSE;
313
    FAST_TRAIN                        : boolean    := FALSE;
314
    ENABLE_RX_TD_ECRC_TRIM            : boolean    := FALSE;
315
    DISABLE_SCRAMBLING                : boolean    := FALSE;
316
    PM_CAP_VERSION                    : integer    := 3;
317
    PM_CAP_PME_CLOCK                  : boolean    := FALSE;
318
    PM_CAP_DSI                        : boolean    := FALSE;
319
    PM_CAP_AUXCURRENT                 : integer    := 0;
320
    PM_CAP_D1SUPPORT                  : boolean    := TRUE;
321
    PM_CAP_D2SUPPORT                  : boolean    := TRUE;
322
    PM_CAP_PMESUPPORT                 : bit_vector := x"0F";
323
    PM_DATA0                          : bit_vector := x"04";
324
    PM_DATA_SCALE0                    : bit_vector := x"0";
325
    PM_DATA1                          : bit_vector := x"00";
326
    PM_DATA_SCALE1                    : bit_vector := x"0";
327
    PM_DATA2                          : bit_vector := x"00";
328
    PM_DATA_SCALE2                    : bit_vector := x"0";
329
    PM_DATA3                          : bit_vector := x"00";
330
    PM_DATA_SCALE3                    : bit_vector := x"0";
331
    PM_DATA4                          : bit_vector := x"04";
332
    PM_DATA_SCALE4                    : bit_vector := x"0";
333
    PM_DATA5                          : bit_vector := x"00";
334
    PM_DATA_SCALE5                    : bit_vector := x"0";
335
    PM_DATA6                          : bit_vector := x"00";
336
    PM_DATA_SCALE6                    : bit_vector := x"0";
337
    PM_DATA7                          : bit_vector := x"00";
338
    PM_DATA_SCALE7                    : bit_vector := x"0";
339
    PCIE_GENERIC                      : bit_vector := "000011101111";
340
    GTP_SEL                           : integer    := 0;
341
    CFG_VEN_ID                        : std_logic_vector(15 downto 0) := x"10EE";
342
    CFG_DEV_ID                        : std_logic_vector(15 downto 0) := x"ABCD";
343
    CFG_REV_ID                        : std_logic_vector(7 downto 0)  := x"00";
344
    CFG_SUBSYS_VEN_ID                 : std_logic_vector(15 downto 0) := x"10EE";
345
    CFG_SUBSYS_ID                     : std_logic_vector(15 downto 0) := x"1234";
346
    REF_CLK_FREQ                      : integer    := 0
347
  );
348
  port (
349
    -- PCI Express Fabric Interface
350
    pci_exp_txp             : out std_logic;
351
    pci_exp_txn             : out std_logic;
352
    pci_exp_rxp             : in  std_logic;
353
    pci_exp_rxn             : in  std_logic;
354
 
355
    -- Transaction (TRN) Interface
356
    trn_lnk_up_n            : out std_logic;
357
 
358
    -- Tx
359
    trn_td                  : in  std_logic_vector(31 downto 0);
360
    trn_tsof_n              : in  std_logic;
361
    trn_teof_n              : in  std_logic;
362
    trn_tsrc_rdy_n          : in  std_logic;
363
    trn_tdst_rdy_n          : out std_logic;
364
    trn_terr_drop_n         : out std_logic;
365
    trn_tsrc_dsc_n          : in  std_logic;
366
    trn_terrfwd_n           : in  std_logic;
367
    trn_tbuf_av             : out std_logic_vector(5 downto 0);
368
    trn_tstr_n              : in  std_logic;
369
    trn_tcfg_req_n          : out std_logic;
370
    trn_tcfg_gnt_n          : in  std_logic;
371
 
372
    -- Rx
373
    trn_rd                  : out std_logic_vector(31 downto 0);
374
    trn_rsof_n              : out std_logic;
375
    trn_reof_n              : out std_logic;
376
    trn_rsrc_rdy_n          : out std_logic;
377
    trn_rsrc_dsc_n          : out std_logic;
378
    trn_rdst_rdy_n          : in  std_logic;
379
    trn_rerrfwd_n           : out std_logic;
380
    trn_rnp_ok_n            : in  std_logic;
381
    trn_rbar_hit_n          : out std_logic_vector(6 downto 0);
382
    trn_fc_sel              : in  std_logic_vector(2 downto 0);
383
    trn_fc_nph              : out std_logic_vector(7 downto 0);
384
    trn_fc_npd              : out std_logic_vector(11 downto 0);
385
    trn_fc_ph               : out std_logic_vector(7 downto 0);
386
    trn_fc_pd               : out std_logic_vector(11 downto 0);
387
    trn_fc_cplh             : out std_logic_vector(7 downto 0);
388
    trn_fc_cpld             : out std_logic_vector(11 downto 0);
389
 
390
    -- Host (CFG) Interface
391
    cfg_do                  : out std_logic_vector(31 downto 0);
392
    cfg_rd_wr_done_n        : out std_logic;
393
    cfg_dwaddr              : in  std_logic_vector(9 downto 0);
394
    cfg_rd_en_n             : in  std_logic;
395
    cfg_err_ur_n            : in  std_logic;
396
    cfg_err_cor_n           : in  std_logic;
397
    cfg_err_ecrc_n          : in  std_logic;
398
    cfg_err_cpl_timeout_n   : in  std_logic;
399
    cfg_err_cpl_abort_n     : in  std_logic;
400
    cfg_err_posted_n        : in  std_logic;
401
    cfg_err_locked_n        : in  std_logic;
402
    cfg_err_tlp_cpl_header  : in  std_logic_vector(47 downto 0);
403
    cfg_err_cpl_rdy_n       : out std_logic;
404
    cfg_interrupt_n         : in  std_logic;
405
    cfg_interrupt_rdy_n     : out std_logic;
406
    cfg_interrupt_assert_n  : in  std_logic;
407
    cfg_interrupt_do        : out std_logic_vector(7 downto 0);
408
    cfg_interrupt_di        : in  std_logic_vector(7 downto 0);
409
    cfg_interrupt_mmenable  : out std_logic_vector(2 downto 0);
410
    cfg_interrupt_msienable : out std_logic;
411
    cfg_turnoff_ok_n        : in  std_logic;
412
    cfg_to_turnoff_n        : out std_logic;
413
    cfg_pm_wake_n           : in  std_logic;
414
    cfg_pcie_link_state_n   : out std_logic_vector(2 downto 0);
415
    cfg_trn_pending_n       : in  std_logic;
416
    cfg_dsn                 : in  std_logic_vector(63 downto 0);
417
    cfg_bus_number          : out std_logic_vector(7 downto 0);
418
    cfg_device_number       : out std_logic_vector(4 downto 0);
419
    cfg_function_number     : out std_logic_vector(2 downto 0);
420
    cfg_status              : out std_logic_vector(15 downto 0);
421
    cfg_command             : out std_logic_vector(15 downto 0);
422
    cfg_dstatus             : out std_logic_vector(15 downto 0);
423
    cfg_dcommand            : out std_logic_vector(15 downto 0);
424
    cfg_lstatus             : out std_logic_vector(15 downto 0);
425
    cfg_lcommand            : out std_logic_vector(15 downto 0);
426
 
427
    -- System Interface
428
    sys_clk                 : in  std_logic;
429
    sys_reset_n             : in  std_logic;
430
    trn_clk                 : out std_logic;
431
    trn_reset_n             : out std_logic;
432
    received_hot_reset      : out std_logic
433
    );
434
  end component pcie;
435
 
436
        COMPONENT blk_mem_gen_v4_1
437
        PORT(
438
                clka : IN std_logic;
439
                wea : IN std_logic_vector(0 to 0);
440
                addra : IN std_logic_vector(8 downto 0);
441
                dina : IN std_logic_vector(31 downto 0);
442
                clkb : IN std_logic;
443
                addrb : IN std_logic_vector(8 downto 0);
444
                doutb : OUT std_logic_vector(31 downto 0)
445
                );
446
        END COMPONENT;
447
 
448
 
449
 
450
---- ------- SYNTHESIS ATTRIBUTES: --------------------------------------------------
451
--attribute keep_hierarchy : string; 
452
--attribute keep_hierarchy of xilinx_pcie2wb: entity is "yes"; 
453
 
454
 
455
 
456
-- --------ARCHITECTURE BODY BEGINS -----------------------------------------------
457
begin
458
 
459
 
460
cfg_turnoff_ok_n <= '1';
461
 
462
        -- COMPONENT INSTALLATIONS (connecting the IPs to local signals) ---------------
463
 
464
 
465
        -- COMPONENT INSTALLATIONS (connecting the IPs to local signals) ---------------
466
 
467
  inst_pcie : pcie
468
  port map (
469
    pci_exp_txp             => pci_exp_txp,
470
    pci_exp_txn             => pci_exp_txn,
471
    pci_exp_rxp             => pci_exp_rxp,
472
    pci_exp_rxn             => pci_exp_rxn,
473
    trn_lnk_up_n            => trn_lnk_up_n,
474
    trn_td                  => trn_td,                   -- Bus [31 : 0]
475
    trn_tsof_n              => trn_tsof_n,
476
    trn_teof_n              => trn_teof_n,
477
    trn_tsrc_rdy_n          => trn_tsrc_rdy_n,
478
    trn_tdst_rdy_n          => trn_tdst_rdy_n,
479
    trn_terr_drop_n         => trn_terr_drop_n,
480
    trn_tsrc_dsc_n          => trn_tsrc_dsc_n,
481
    trn_terrfwd_n           => trn_terrfwd_n,
482
    trn_tbuf_av             => trn_tbuf_av,              -- Bus [31 : 0]
483
    trn_tstr_n              => trn_tstr_n,
484
    trn_tcfg_req_n          => trn_tcfg_req_n,
485
    trn_tcfg_gnt_n          => trn_tcfg_gnt_n,
486
    trn_rd                  => trn_rd,                   -- Bus [31 : 0]
487
    trn_rsof_n              => trn_rsof_n,
488
    trn_reof_n              => trn_reof_n,
489
    trn_rsrc_rdy_n          => trn_rsrc_rdy_n,
490
    trn_rsrc_dsc_n          => trn_rsrc_dsc_n,
491
    trn_rdst_rdy_n          => trn_rdst_rdy_n,
492
    trn_rerrfwd_n           => trn_rerrfwd_n,
493
    trn_rnp_ok_n            => trn_rnp_ok_n,
494
    trn_rbar_hit_n          => trn_rbar_hit_n,           -- Bus [31 : 0]
495
    trn_fc_sel              => trn_fc_sel,               -- Bus [31 : 0]
496
    trn_fc_nph              => trn_fc_nph,               -- Bus [31 : 0]
497
    trn_fc_npd              => trn_fc_npd,               -- Bus [31 : 0]
498
    trn_fc_ph               => trn_fc_ph,                -- Bus [31 : 0]
499
    trn_fc_pd               => trn_fc_pd,                -- Bus [31 : 0]
500
    trn_fc_cplh             => trn_fc_cplh,              -- Bus [31 : 0]
501
    trn_fc_cpld             => trn_fc_cpld,              -- Bus [31 : 0]
502
    cfg_do                  => cfg_do,                   -- Bus [31 : 0]
503
    cfg_rd_wr_done_n        => cfg_rd_wr_done_n,
504
    cfg_dwaddr              => cfg_dwaddr,               -- Bus [31 : 0]
505
    cfg_rd_en_n             => cfg_rd_en_n,
506
    cfg_err_ur_n            => cfg_err_ur_n,
507
    cfg_err_cor_n           => cfg_err_cor_n,
508
    cfg_err_ecrc_n          => cfg_err_ecrc_n,
509
    cfg_err_cpl_timeout_n   => cfg_err_cpl_timeout_n,
510
    cfg_err_cpl_abort_n     => cfg_err_cpl_abort_n,
511
    cfg_err_posted_n        => cfg_err_posted_n,
512
    cfg_err_locked_n        => cfg_err_locked_n,
513
    cfg_err_tlp_cpl_header  => cfg_err_tlp_cpl_header,   -- Bus [31 : 0]
514
    cfg_err_cpl_rdy_n       => cfg_err_cpl_rdy_n,
515
    cfg_interrupt_n         => cfg_interrupt_n,
516
    cfg_interrupt_rdy_n     => cfg_interrupt_rdy_n,
517
    cfg_interrupt_assert_n  => cfg_interrupt_assert_n,
518
    cfg_interrupt_do        => cfg_interrupt_do,         -- Bus [31 : 0]
519
    cfg_interrupt_di        => cfg_interrupt_di,         -- Bus [31 : 0]
520
    cfg_interrupt_mmenable  => cfg_interrupt_mmenable,   -- Bus [31 : 0]
521
    cfg_interrupt_msienable => cfg_interrupt_msienable,
522
    cfg_turnoff_ok_n        => cfg_turnoff_ok_n,
523
    cfg_to_turnoff_n        => cfg_to_turnoff_n,
524
    cfg_pm_wake_n           => cfg_pm_wake_n,
525
    cfg_pcie_link_state_n   => cfg_pcie_link_state_n,    -- Bus [31 : 0]
526
    cfg_trn_pending_n       => cfg_trn_pending_n,
527
    cfg_dsn                 => cfg_dsn,                  -- Bus [31 : 0]
528
    cfg_bus_number          => cfg_bus_number,           -- Bus [31 : 0]
529
    cfg_device_number       => cfg_device_number,        -- Bus [31 : 0]
530
    cfg_function_number     => cfg_function_number,      -- Bus [31 : 0]
531
    cfg_status              => cfg_status,               -- Bus [31 : 0]
532
    cfg_command             => cfg_command,              -- Bus [31 : 0]
533
    cfg_dstatus             => cfg_dstatus,              -- Bus [31 : 0]
534
    cfg_dcommand            => cfg_dcommand,             -- Bus [31 : 0]
535
    cfg_lstatus             => cfg_lstatus,              -- Bus [31 : 0]
536
    cfg_lcommand            => cfg_lcommand,             -- Bus [31 : 0]
537
    sys_clk                 => sys_clk,
538
    sys_reset_n             => sys_reset_n,
539
    trn_clk                 => trn_clk,
540
    trn_reset_n             => trn_reset_n,
541
    received_hot_reset      => received_hot_reset
542
  );
543
 
544
        --block ram for RX TLP:
545
        Inst_bram_rxtlp: blk_mem_gen_v4_1 PORT MAP(
546
                clka => trn_clk,
547
                wea => bram_rxtlp_we,
548
                addra => bram_rxtlp_writeaddress(8 downto 0),
549
                dina => bram_rxtlp_writedata,
550
                clkb => trn_clk,
551
                addrb => bram_rxtlp_readaddress(8 downto 0),
552
                doutb => bram_rxtlp_readdata
553
        );
554
 
555
        --block ram for TX TLP:
556
        Inst_bram_txtlp: blk_mem_gen_v4_1 PORT MAP(
557
                clka => trn_clk,
558
                wea => bram_txtlp_we,
559
                addra => bram_txtlp_writeaddress(8 downto 0),
560
                dina => bram_txtlp_writedata,
561
                clkb => trn_clk,
562
                addrb => bram_txtlp_readaddress(8 downto 0),
563
                doutb => bram_txtlp_readdata
564
        );
565
 
566
 
567
 
568
 
569
 
570
        -- MAIN LOGIC: ---------------------------------------------------------------------------------------------
571
 
572
 
573
 
574
        --System Signals:--------------------------------
575
 
576
  --Clock Input Buffer for differential system clock
577
   IBUFDS_inst : IBUFDS
578
   generic map (
579
      DIFF_TERM => TRUE, -- Differential Termination 
580
      IBUF_LOW_PWR => FALSE, -- Low power (TRUE) vs. performance (FALSE) setting for referenced I/O standards
581
      IOSTANDARD => "DEFAULT")
582
   port map (
583
      O => sys_clk,  -- Buffer output
584
      I => sys_clk_p,  -- Diff_p buffer input (connect directly to top-level port)
585
      IB => sys_clk_n -- Diff_n buffer input (connect directly to top-level port)
586
   );
587
 
588
  --wishbone clock output:
589
  pcie_bar0_wb_clk_o <= trn_clk;
590
  --pcie_bar1_wb_clk_o <= trn_clk; 
591
 
592
 
593
  --use one of these for resetting logic in this file:
594
    pciewb_localreset_n <= sys_reset_n; --dont wait for the PCIE-EP to finish its init.
595
    --pciewb_localreset_n <= trn_reset_n;
596
         --pciewb_localreset_n <= trn_reset_n and (not trn_lnk_up_n) and (not received_hot_reset);
597
  --reset to the core:
598
  --sys_reset_n comes from toplevel directly to the core. same name
599
  --reset output to other cores:
600
  pcie_resetout <= not pciewb_localreset_n;
601
 
602
  --trn_lnk_up_n    --not used.
603
 
604
 
605
  --pcie ep ip config port: ----------------------------------------------------------
606
 
607
   --trn_fc_sel             <= "000";
608
 
609
  trn_rnp_ok_n           <= '0';
610
  --trn_terrfwd_n          <= '1';
611
 
612
  --trn_tcfg_gnt_n         <= '0';
613
 
614
  cfg_err_cor_n          <= '1';
615
  cfg_err_ur_n           <= '1';
616
  cfg_err_ecrc_n         <= '1';
617
  cfg_err_cpl_timeout_n  <= '1';
618
  cfg_err_cpl_abort_n    <= '1';
619
  cfg_err_posted_n       <= '0';
620
  cfg_err_locked_n       <= '1';
621
  cfg_pm_wake_n          <= '1';
622
  cfg_trn_pending_n      <= '1';
623
 
624
  --trn_tstr_n             <= '0'; 
625
  --cfg_interrupt_assert_n <= '1'; --used in a process at the bottom of this file
626
  --cfg_interrupt_n        <= '1';
627
  --cfg_interrupt_di       <= x"00"; --intA used
628
 
629
  cfg_err_tlp_cpl_header <= (OTHERS => '0');
630
  cfg_dwaddr             <= (OTHERS => '0');
631
  cfg_rd_en_n            <= '1';
632
  --serial number:
633
  cfg_dsn                <= (OTHERS => '0');
634
 
635
 -- AT THE BOTTOM OF THIS FILE:
636
 --      --some fix values:
637
 --      trn_tsrc_dsc_n <= '1'; --no errors on trn bus
638
 --      trn_tstr_n <= '0'; --pipelining (0= link may begin before the entire packet has been written)
639
 --      trn_tcfg_gnt_n <= '0'; --no tlp priorities
640
 --      trn_terrfwd_n <= '1'; --no errors on trn
641
 --      --nc: trn_tbuf_av, trn_terr_drop_n, trn_tcfg_req_n
642
 
643
 
644
 
645
  --use this in read completion packets:
646
  cfg_completer_id       <= cfg_bus_number & cfg_device_number & cfg_function_number;
647
 
648
 
649
 
650
 
651
 
652
 
653
        -- WISBONE BACK-end INTERFACE ----------------------------------------------------
654
 
655
    --main state machine: set states, capture inputs, set addr/data outputs
656
         --minimum 2 clock cycles / transaction. writes are posted, reads have wait states.
657
    process (pciewb_localreset_n, trn_clk, wb0_state, start_read_wb0, start_write_wb0,
658
                                pcie_bar0_wb_addr_o_feed, pcie_bar0_wb_data_o_feed, pcie_bar0_wb_sel_o_feed)
659
    begin
660
    if (pciewb_localreset_n='0') then
661
       wb0_state <= "00000000";
662
       wb_transaction_complete <= '0';
663
                 pcie_bar0_wb_addr_o <= "0000000000000000000000000000";
664
                 pcie_bar0_wb_sel_o <= "0000";
665
                 pcie_bar0_wb_data_o <= "00000000000000000000000000000000";
666
                 wb_transaction_complete <='0';
667
    else
668
      if (trn_clk'event and trn_clk = '1') then
669
                case ( wb0_state ) is
670
 
671
                --********** IDLE STATE  **********
672
                when "00000000" =>   --state 0        
673
                    wb_transaction_complete <='0';
674 3 buenos
                                                  pcie_bar0_wb_sel_o <= pcie_bar0_wb_sel_o_feed;
675 2 buenos
                                                  pcie_bar0_wb_addr_o <= pcie_bar0_wb_addr_o_feed;
676
                                                  if (start_read_wb0 ='1') then --go to read
677
                                                    wb0_state <= "00000001";
678
                                                  elsif (start_write_wb0 ='1') then --go to write
679
                                                    wb0_state <= "00000010";
680
                                                         --no endian swap: pcie_bar0_wb_data_o <= pcie_bar0_wb_data_o_feed;
681
                                                    pcie_bar0_wb_data_o (7 downto 0) <= pcie_bar0_wb_data_o_feed(31 downto 24); --swap endianism
682
                                                    pcie_bar0_wb_data_o (15 downto 8) <= pcie_bar0_wb_data_o_feed(23 downto 16); --swap endianism
683
                                                    pcie_bar0_wb_data_o (23 downto 16) <= pcie_bar0_wb_data_o_feed(15 downto 8); --swap endianism
684
                                                    pcie_bar0_wb_data_o (31 downto 24) <= pcie_bar0_wb_data_o_feed(7 downto 0); --swap endianism                                                           
685
                                                  end if;
686
 
687
                --********** READ STATE ********** 
688
                                         --set the outputs, 
689
                                         --if ACK asserted, sample the data input
690
                                         --The hold requirements are oversatisfyed by going back to idle, and by the fact that the slave uses the cyc/stb/wr strobes synchronously.
691
                when "00000001" =>   --state 1
692
                    if (pcie_bar0_wb_ack_i='1') then
693
                                                    --no endian swap: pcie_bar0_wb_data_i_latched <= pcie_bar0_wb_data_i; --sample the incoming data
694
                                                    pcie_bar0_wb_data_i_latched (7 downto 0) <= pcie_bar0_wb_data_i(31 downto 24); --swap endianism
695
                                                    pcie_bar0_wb_data_i_latched (15 downto 8) <= pcie_bar0_wb_data_i(23 downto 16); --swap endianism
696
                                                    pcie_bar0_wb_data_i_latched (23 downto 16) <= pcie_bar0_wb_data_i(15 downto 8); --swap endianism
697
                                                    pcie_bar0_wb_data_i_latched (31 downto 24) <= pcie_bar0_wb_data_i(7 downto 0); --swap endianism                                                      
698
                                                         wb_transaction_complete <='1'; --signalling ready, but only for one clock cycle
699
                                                         wb0_state <= "00000000"; --go to state 0
700
                                                  else
701
                                                         wb_transaction_complete <='0';
702
                                                  end if;
703
 
704
                --********** WRITE STATE **********     
705
                                         --if ACK asserted, go back to idle
706
                                         --The hold requirements are oversatisfyed by waiting for ACK to remove write data                                       
707
                when "00000010" =>   --state 2
708
                    if (pcie_bar0_wb_ack_i='1') then
709
                                                         wb0_state <= "00000000"; --go to state 0
710
                                                         wb_transaction_complete <='1';
711
                                                  else
712
                                                     wb_transaction_complete <='0';
713
                                                  end if;
714
 
715
                when others => --error
716
                      wb0_state <= "00000000"; --go to state 0
717
                end case;
718
       end if;
719
    end if;
720
    end process;
721
    --sync control on wb-control signals:
722
    process (pciewb_localreset_n, wb0_state)
723
    begin
724
    if (pciewb_localreset_n='0') then
725
                pcie_bar0_wb_cyc_o  <= '0';
726
                pcie_bar0_wb_stb_o  <= '0';
727
                pcie_bar0_wb_wr_o  <= '0';
728
    else
729
      if (wb0_state = "00000000") then --idle
730
                        pcie_bar0_wb_cyc_o  <= '0';
731
                        pcie_bar0_wb_stb_o  <= '0';
732
                        pcie_bar0_wb_wr_o  <= '0';
733
      elsif (wb0_state = "00000001") then --read 
734
                        pcie_bar0_wb_cyc_o  <= '1';
735
                        pcie_bar0_wb_stb_o  <= '1';
736
                        pcie_bar0_wb_wr_o  <= '0';
737
      elsif (wb0_state = "00000010") then --write 
738
                        pcie_bar0_wb_cyc_o  <= '1';
739
                        pcie_bar0_wb_stb_o  <= '1';
740
                        pcie_bar0_wb_wr_o  <= '1';
741
                else
742
                        pcie_bar0_wb_cyc_o  <= '0';
743
                        pcie_bar0_wb_stb_o  <= '0';
744
                        pcie_bar0_wb_wr_o  <= '0';
745
                end if;
746
    end if;
747
    end process;
748
 
749
 
750
 
751
 
752
 
753
 
754
 
755
 
756
        -- INTERFACE TO THE PCIE-EP IP --------------------------------------------------------
757
        --trn_clk and trn_reset_n are the same as the pcie_resetout and pcie_bar0_wb_clk_o,
758
        --so it is not a clock domain crossing.
759
 
760
 
761
        -- TX: INTERFACE TO THE PCIE-EP: TRANSMIT TLP PACKETS:-----
762
        --Read completion is 3DW header. This core only transmits read completion or Unbsupported request packets.
763
    process (pciewb_localreset_n, trn_clk, epif_tx_state, bram_txtlp_readdata , bram_txtlp_readaddress,
764
                                pcie_there_is_a_new_tlp_to_transmit, tlp_payloadsize_dwords, txtrn_counter)
765
    begin
766
    if (pciewb_localreset_n='0') then
767
      epif_tx_state <= "00000000";
768
      trn_tsrc_rdy_n_1 <='1';
769
                trn_tsof_n1 <= '1';
770
                trn_teof_n <= '1';
771
                trn_td <= (OTHERS => '0');
772
                pcie_tlp_tx_complete <= '0';
773
                txtrn_counter <= "00000001";
774
                bram_txtlp_readaddress <= (OTHERS => '0');
775
    else
776
      if (trn_clk'event and trn_clk = '1') then
777
                case ( epif_tx_state ) is
778
 
779
                --********** idle STATE  **********
780
                when "00000000" =>   --state 0        
781
                    --if there is a new TLP assembled and the EP is ready, 
782
                                                  --start the tx-trn bus transaction.
783
                                                  if (pcie_there_is_a_new_tlp_to_transmit='1') then
784
                                                    epif_tx_state <= "00000001"; --next state
785
                                                  end if;
786
                    trn_tsrc_rdy_n_1 <='1';
787
                                                  trn_tsof_n1 <= '1';
788
                                                  trn_teof_n <= '1';
789
                                                  trn_td <= (OTHERS => '0');
790
                                                  pcie_tlp_tx_complete <= '0';
791
                                                  txtrn_counter <= "00000001";
792
                                                  bram_txtlp_readaddress <= (OTHERS => '0');
793
 
794
                --********** ready-wait STATE  **********
795
                when "00000001" =>   --state 1        
796
                    --if there is a new TLP assembled and the EP is ready, 
797
                                                  --start the tx-trn bus transaction.
798
                                                  if (trn_tdst_rdy_n='0') then
799
                                                    epif_tx_state <= "00000010"; --next state
800
                                                  end if;
801
                    trn_tsrc_rdy_n_1 <='1';
802
                                                  trn_tsof_n1 <= '1';
803
                                                  trn_teof_n <= '1';
804
                                                  trn_td <= (OTHERS => '0');
805
                                                  pcie_tlp_tx_complete <= '0';
806
                                                  txtrn_counter <= "00000001";
807
                                                  bram_txtlp_readaddress <= (OTHERS => '0');
808
 
809
                --********** transfer STATE **********                                   
810
                when "00000010" =>   --state 2
811
                    trn_tsrc_rdy_n_1 <='0';
812
                                                  trn_td <= bram_txtlp_readdata;
813
                                                  if (trn_tdst_rdy_n='0') then
814
                                                    txtrn_counter <= txtrn_counter +1;
815
                                                         bram_txtlp_readaddress <= bram_txtlp_readaddress +1;
816
                                                  end if;
817
                                                  if (txtrn_counter = "00000010") then
818
                                                    trn_tsof_n1 <= '0'; --start
819
                                                  else
820
                                                    trn_tsof_n1 <= '1';
821
                                                  end if;
822
                                                  --test number of dwords:
823
                                                  if (txtrn_counter = tlp_payloadsize_dwords +4) then -- "+3" is the header and "+1" is for the delay
824
                                                  --this is the last dword, next clk is next state
825
                                                         epif_tx_state <= "00000000"; --back to idle, since finished
826
                                                    trn_teof_n <= '0'; --end
827
                                                    pcie_tlp_tx_complete <= '1'; --assert for 1 clk
828
                                                  else
829
                                                    trn_teof_n <= '1'; --not end yet
830
                                                    pcie_tlp_tx_complete <= '0'; --not complete yet
831
                                                  end if;
832
 
833
                when others => --error
834
                    epif_tx_state <= "00000000"; --back to idle
835
                    trn_tsrc_rdy_n_1 <='1';
836
                                                  trn_tsof_n1 <= '1';
837
                                                  trn_teof_n <= '1';
838
                                                  trn_td <= (OTHERS => '0');
839
                                                  pcie_tlp_tx_complete <= '0';
840
                                                  txtrn_counter <= "00000001";
841
 
842
                end case;
843
       end if;
844
    end if;
845
    end process;
846
 
847
        --this (little delay) is to fix a hold time violation created inside the pcie-ep ip:
848
        trn_tsrc_rdy_n <= trn_tsrc_rdy_n_1 or (not pciewb_localreset_n);
849
        trn_tsof_n <= trn_tsof_n1 or (not pciewb_localreset_n);
850
 
851
 
852
 
853
         --some fix values:
854
         trn_tsrc_dsc_n <= '1'; --no errors on trn bus
855
         trn_tstr_n <= '0'; --pipelining 
856
         trn_tcfg_gnt_n <= '0'; --no tlp priorities 
857
         trn_terrfwd_n <= '1'; --no errors on trn
858
         --nc: trn_tbuf_av, trn_terr_drop_n, trn_tcfg_req_n
859
 
860
 
861
 
862
 
863
 
864
        -- RX: INTERFACE TO THE PCIE-EP: GET thereceived TLP PACKETS:- ----
865 3 buenos
    process (pciewb_localreset_n, trn_clk, epif_rx_state, tlp_state, trn_rx_counter,
866
                                bram_rxtlp_writeaddress, rxstm_readytoroll, trn_rsof_n, tlpstm_isin_idle, trn_rdst_rdy_n)
867 2 buenos
    begin
868
    if (pciewb_localreset_n='0') then
869
                 pcie_just_received_a_new_tlp <= '0';
870
                 epif_rx_state  <= "00000000";
871
                 trn_rdst_rdy_n <= '1';
872
                 trn_rx_counter <= (OTHERS => '0');
873
                 bram_rxtlp_we <= "0";
874
                 bram_rxtlp_writeaddress <= (OTHERS => '0');
875
                 bram_rxtlp_writedata  <= (OTHERS => '0');
876 3 buenos
                 rxstm_readytoroll <= '0';
877 2 buenos
    else
878
      if (trn_clk'event and trn_clk = '1') then
879
 
880
                case ( epif_rx_state ) is
881
 
882
                --********** idle STATE  **********
883
                when "00000000" =>   --state 0
884
                                                  pcie_just_received_a_new_tlp <= '0';
885 3 buenos
                                                  bram_rxtlp_writedata  <= trn_rd;
886
                                                  if (trn_rsrc_rdy_n='0' and trn_rsof_n='0' and tlpstm_isin_idle = '1' and trn_rdst_rdy_n='0') then
887 2 buenos
                                                    trn_rx_counter <= trn_rx_counter +1;
888
                                                         bram_rxtlp_writeaddress <= bram_rxtlp_writeaddress +1;
889
                                                         epif_rx_state <= "00000001";
890
                                                  else
891
                                                    trn_rx_counter <= (OTHERS => '0');
892
                                                         bram_rxtlp_writeaddress  <= (OTHERS => '0');
893 3 buenos
                                                  end if;
894
                                                  --destination ready:
895
                                                  if (tlpstm_isin_idle = '1')then
896
                                                          trn_rdst_rdy_n <= '0';
897
                                                  else
898
                                                          trn_rdst_rdy_n <= '1';
899
                                                  end if;
900
                                                  --write into buffer:
901
                                                  if (trn_rsrc_rdy_n='0' and trn_rsof_n='0' and tlpstm_isin_idle = '1') then
902
                                                         bram_rxtlp_we <= "1";
903
                                                         rxstm_readytoroll <= '1';
904
                                                  else
905 2 buenos
                                                         bram_rxtlp_we <= "0";
906 3 buenos
                                                         rxstm_readytoroll <= '0';
907 2 buenos
                                                  end if;
908
 
909
                --********** read STATE ********** 
910
                when "00000001" =>   --state 1
911 3 buenos
                                                  rxstm_readytoroll <= '0';
912 2 buenos
                                                  if (trn_reof_n ='0') then --last dw
913
                                                    epif_rx_state <= "00000010"; --for the next clk cycle
914 3 buenos
                                                         trn_rdst_rdy_n <= '1'; --ok, dont send more yet
915 2 buenos
                                                  end if;
916
                                                  if (trn_rsrc_rdy_n='0') then --only act if the EP was ready
917
                                                          trn_rx_counter <= trn_rx_counter +1;
918
                                                          bram_rxtlp_writeaddress <= bram_rxtlp_writeaddress +1;
919
                                                          bram_rxtlp_writedata  <= trn_rd;
920
                                                  end if;
921
                                                  --in an early stage of this transfer, the scheduler can already
922
                                                  --start working on the data, this way its pipelined, so the latency is lower.
923
                                                  if (trn_rx_counter = "00000010") then
924
                                                   pcie_just_received_a_new_tlp <= '1';--assert for one clk only
925
                                                  else
926
                                                   pcie_just_received_a_new_tlp <= '0';
927
                                                  end if;
928
 
929
                --********** finished filling up RX TLP STATE **********                                 
930
                when "00000010" =>   --state 2
931
                                                  epif_rx_state <= "00000000";
932
                                                  trn_rx_counter <= (OTHERS => '0');
933
 
934
                when others => --error
935
                      epif_rx_state <= "00000000"; --go to state 0
936
                end case;
937
       end if;
938
    end if;
939
    end process;
940
 
941
         --fixed connections:
942
         --trn_rnp_ok_ntrn_rnp_ok_n <= '0'; --ready to receive non-posted
943
         --not connected: trn_rerrfwd_n, trn_rsrc_dsc_n, trn_rbar_hit_n
944
 
945
 
946
 
947
 
948
 
949
        -- flow control: INTERFACE TO THE PCIE-EP: - ----
950
        --not used. pcie-ep provides information about credit status.
951
        --unconnected: trn_fc_nph, trn_fc_npd, trn_fc_ph, trn_fc_pd, trn_fc_cplh, trn_fc_cpld
952
        trn_fc_sel <= "000";
953
 
954
 
955
 
956
 
957
 
958
        -- --- GLUE LOGIC BETWEEN THE PCIE CORE-IF AND THE WB INTERFACES -----------------------
959
        -- --- ALSO TLP PACKET PROCESSING.
960
        --Theory of operation:
961
        --RX: If we receive a TLP (pcie_just_received_a_new_tlp goes high for one clock cycle), 
962
        --then store it (pcie_received_tlp), decode it (to figure out if its read request, 
963
        --posted write or non-supported request), then assert a flag (start_write_wb0 or 
964
        --start_read_wb0)to initiate a wishbone cycle.
965
        --TX: At the completion of a wishbone read, the wishbone statemachine asserts the 
966
        --wb_transaction_complete flag, so we can assemble the TX TLP packet (pcie_to_transmit_tlp) 
967
        --and assert the flag named pcie_there_is_a_new_tlp_to_transmit. This packet will be 
968
        --a read completion packet on the PCIe link.
969
        --
970
        --This core can handle 1...8 DWORD accesses in one request (max 256bit payload ), 
971
        --and can handle only one request at a time. If a new request is arriving while
972
        --processing the previous one (e.g. getting the data from a wishbone read), then 
973
        --the state machine will not process it immediately, or it will hang. So the user 
974
        --software has to wait for the previous read completion before issueing a new request.
975
        --The multiple DWORDs are handled separately by the WB statemachine.
976
   --Performance: WishBone bus: 62.5MHz, 32bit, 3clk/access -> 83MBytes/sec
977
        --
978
        --TLP decoding: 
979
        --Header+Payload_data+TLP_DIGEST(ECRC). 
980
        --received Header:
981
        --First Dword: bit.30/29=format: 00=3DW-header+no_data, 01=4DW-header+no_data, 
982
        --10=3DW-header+data, 11=4DW-header+data. bit.28:24=type: 00000 or 00001 are memory 
983
        --read requests, 00000 or 00001 are memory write request if type=1x. read request 
984
        --completion is 01010 and type=10. bit.9:0 is payload size [DW]. 
985
        --Second Dword: bit.31:16 is requester ID. bit3:0 is first dword byte enable, bit.7:4 is 
986
        --byte enable for last dword data. intermediate dwords have all bytes enabled.
987
        --Third DWORD: address, where bit.1:0=00b. 4DW headers are for 64bit. 64bit adressing
988
        --uses 3rd-dword for addre63:32, 4th dword for addr31:0.
989
        --
990
        --The TLP variables in this core: BRAM memory used store TLP, up to 1-2kBytes
991
        --
992
        --Read completion is 3DW header and routed by completer-ID and requester-ID, not address.
993
        --The core has to store the requester ID and feed it back in the completion packet.
994
        --Completion status: 000=successful, 100=completer_abort, 001=unsupported request. byte
995
        --count is N.of bytes left. lower_address is the first enabled byte of data returned 
996
        --with the Completion.
997
        --
998
        --  Completion packet header:
999
        --DW1 >
1000
        --7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
1001
        --r FMT type----- r TC--- reserv- T E att r r lenght-------------
1002
        --  x 0                           D P rib
1003
        --DW2 >
1004
        --7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
1005
        --COMPLETER_ID------------------- statu B byte_count-------------
1006
        --                                      CM
1007
        --DW3 >
1008
        --7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
1009
        --REQUESTER_ID------------------- tag------------ r lower_address
1010
 
1011
 
1012
         --TLP-protocol statemachine:
1013
    process (pciewb_localreset_n, trn_clk, tlp_state,
1014
                                pcie_just_received_a_new_tlp, tlp_datacount,
1015
                                bram_rxtlp_readdata,  bram_txtlp_writeaddress, bram_rxtlp_readaddress,
1016
                                tlp_state_copy, rxtlp_decodedaddress,
1017
                                rxtlp_header_dw1, rxtlp_header_dw2, rxtlp_header_dw3, rxtlp_header_dw4,
1018
                                bit10, rxtlp_firstdw_be, wb_transaction_complete, flag1, rxdw1_23_0, pcie_rxtlp_tag,
1019
                                tlp_payloadsize_dwords, pcie_bar0_wb_data_i_latched, cfg_completer_id,
1020
                                rxtlp_requesterid)
1021
    begin
1022
    if (pciewb_localreset_n='0') then
1023
                start_read_wb0 <= '0';
1024
                start_write_wb0 <= '0';
1025
                pcie_bar0_wb_data_o_feed         <= (others => '0');
1026
                pcie_bar0_wb_addr_o_feed <= (others => '0');
1027
                pcie_bar0_wb_sel_o_feed  <= (others => '0');
1028
                pcie_there_is_a_new_tlp_to_transmit  <= '0';
1029
                rxtlp_decodedaddress<= (others => '0');
1030
                tlp_payloadsize_dwords <= (others => '0');
1031
                rxtlp_firstdw_be <= (others => '0');
1032
                rxtlp_lastdw_be <= (others => '0');
1033
                rxtlp_requesterid <= (others => '0');
1034
                tlp_state <= (others => '0');
1035
                tlp_state_copy  <= (others => '0');
1036
                bram_txtlp_we <= "0";
1037
                bram_txtlp_writeaddress    <= (others => '0');
1038
                bram_txtlp_writedata     <= (others => '0');
1039
                bram_rxtlp_readaddress   <= (others => '0');
1040
                rxtlp_header_dw1   <= "01111111000000000000000000000000";
1041
                rxtlp_header_dw2   <= (others => '0');
1042
                rxtlp_header_dw3   <= (others => '0');
1043
                rxtlp_header_dw4   <= (others => '0');
1044
                flag1 <= '0';
1045
                rxdw1_23_0 <= (others => '0');
1046
                pcie_rxtlp_tag <= (others => '0');
1047
                rcompl_bytecount_field  <= (others => '0');
1048 3 buenos
                tlpstm_isin_idle <= '1';
1049 2 buenos
    else
1050
      if (trn_clk'event and trn_clk = '1') then
1051
                case ( tlp_state ) is
1052
 
1053
                --********** IDLE STATE  **********
1054
                                         --also re-initialize signals...
1055
                when "00000000" =>   --state 0        
1056
                    if (pcie_just_received_a_new_tlp='1') then
1057
                                                    tlp_state <= "00000001"; --to tlp decoding state
1058 3 buenos
                                                         tlpstm_isin_idle <= '0';
1059
                                                  else
1060
                                                    tlpstm_isin_idle <= '1';
1061 2 buenos
                                                  end if;
1062
                                                  start_write_wb0 <= '0';
1063
                                                  start_read_wb0 <= '0';
1064
                                                  tlp_state_copy <= tlp_state;
1065
                                                        bram_txtlp_we <= "0";
1066
                                                        bram_txtlp_writeaddress   <= (others => '0');
1067
                                                        bram_txtlp_writedata     <= (others => '0');
1068
                                                        bram_rxtlp_readaddress    <= (others => '0');
1069
                                                        tlp_datacount <= "00000001";
1070
                                                        rxtlp_header_dw1   <= "01111111000000000000000000000000"; --this is to prevent false decode
1071
                                                        pcie_bar0_wb_data_o_feed         <= (others => '0');
1072
                                                        pcie_bar0_wb_addr_o_feed <= (others => '0');
1073
                                                        pcie_bar0_wb_sel_o_feed  <= (others => '0');
1074
                                                        rxtlp_header_dw2   <= (others => '0');
1075
                                                        rxtlp_header_dw3   <= (others => '0');
1076
                                                        rxtlp_header_dw4   <= (others => '0');
1077
                                                        rxdw1_23_0 <= (others => '0');
1078
                                                        pcie_rxtlp_tag <= (others => '0');
1079
                                                        pcie_there_is_a_new_tlp_to_transmit  <= '0';
1080
                                                        rxtlp_decodedaddress<= (others => '0');
1081
                                                        tlp_payloadsize_dwords <= (others => '0');
1082
                                                        rxtlp_firstdw_be <= (others => '0');
1083
                                                        rxtlp_lastdw_be <= (others => '0');
1084
                                                        rxtlp_requesterid <= (others => '0');
1085
                                                        rcompl_bytecount_field  <= (others => '0');
1086
 
1087
 
1088
                --********** TLP ARRIVED STATE **********
1089
                                         --read TLP out of EP, decode and decide,
1090
                                         --latch address/sel/wr_data
1091
                                         --All the "IF"-statements use address+1, because the BRAM read side has data available 1clk late!!!
1092
                                         --Added an ectra clock delay, based on testing, since the data is one more CLK late.
1093
                when "00000001" =>   --state 1
1094
                    --latch the header:
1095
                                                  bram_rxtlp_readaddress <= bram_rxtlp_readaddress +1;
1096
                                                  if (bram_rxtlp_readaddress = "000000010") then
1097
                                                    rxtlp_header_dw1 <= bram_rxtlp_readdata;
1098
                                                  elsif (bram_rxtlp_readaddress = "000000011") then
1099
                                                    rxtlp_header_dw2 <= bram_rxtlp_readdata;
1100
                                                  elsif (bram_rxtlp_readaddress = "000000100") then
1101
                                                    rxtlp_header_dw3 <= bram_rxtlp_readdata;
1102
                                                  elsif (bram_rxtlp_readaddress = "000000101") then
1103
                                                    rxtlp_header_dw4 <= bram_rxtlp_readdata;
1104
                                                  end if;
1105
                                                  --decode some parameters: 
1106
                                                  tlp_payloadsize_dwords <= rxtlp_header_dw1(7 downto 0);
1107
                                                  rxtlp_firstdw_be <= rxtlp_header_dw2(3 downto 0);
1108
                                                  rxtlp_lastdw_be <= rxtlp_header_dw2(7 downto 4);
1109
                                                  rxtlp_requesterid <= rxtlp_header_dw2(31 downto 16);
1110
                                                  flag1 <= rxtlp_header_dw1(31);
1111
                                                  rxdw1_23_0 <= rxtlp_header_dw1(23 downto 0); --various fields pcie_received_tlp (22 downto 0);
1112
                                                  pcie_rxtlp_tag <= rxtlp_header_dw2(15 downto 8) ; --pcie_received_tlp (47 downto 40);--tag
1113
                                                  --decide based on header:
1114
                                                  if (rxtlp_header_dw1(30 downto 24)="0000000") then --32bit read
1115
                                                         if (bram_rxtlp_readaddress = "000000100") then
1116
                                                                rxtlp_decodedaddress <= bram_rxtlp_readdata;
1117
                                                                bram_txtlp_writeaddress(8 downto 0) <= "000000011"; --point after the 3dw readcompl header
1118
                                                                tlp_state <= "00000011";
1119
                                                         end if;
1120
                                                  elsif (rxtlp_header_dw1(30 downto 24)="0100000") then --64bit read
1121
                                                         if (bram_rxtlp_readaddress = "000000101") then
1122
                                                                rxtlp_decodedaddress <= bram_rxtlp_readdata;
1123
                                                                bram_txtlp_writeaddress(8 downto 0) <= "000000011"; --point after the 3dw readcompl header
1124
                                                                tlp_state <= "00000011";
1125
                                                         end if;
1126
                                                  elsif (rxtlp_header_dw1(30 downto 24)="1000000") then --32bit write
1127
                                                         if (bram_rxtlp_readaddress = "000000100") then
1128
                                                                rxtlp_decodedaddress <= bram_rxtlp_readdata;
1129
                                                                tlp_state <= "00000010";
1130
                                                         end if;
1131
                                                  elsif (rxtlp_header_dw1(30 downto 24)="1100000") then --64bit write
1132
                                                         if (bram_rxtlp_readaddress = "000000101") then
1133
                                                                rxtlp_decodedaddress <= bram_rxtlp_readdata;
1134
                                                                tlp_state <= "00000010";
1135
                                                         end if;
1136
                                                  elsif (rxtlp_header_dw1(30 downto 24)="1111111") then --just wait until this gets a real value
1137
                                                    rxtlp_decodedaddress <= bram_rxtlp_readdata;
1138
                                                  else --unsupported request
1139
                                                    if (bram_rxtlp_readaddress = "000000100") then
1140
                                                           tlp_state <= "00000101";
1141
                                                                bram_txtlp_writeaddress <= "111111111";
1142
                                                         end if;
1143
                                                  end if;
1144
 
1145
 
1146
                --********** WRITE STATE **********
1147
                                         --initiate WB write(s) (1...N DWORD accesses)
1148
                when "00000010" =>   --state 2
1149
                                                pcie_bar0_wb_addr_o_feed(27 downto 2) <= rxtlp_decodedaddress(27 downto 2) + tlp_datacount -1; --256MBytes size is hardcoded here, by cutting 4-MSB off
1150
                                                pcie_bar0_wb_addr_o_feed(1 downto 0) <= bit10(1 downto 0);
1151
                                                pcie_bar0_wb_sel_o_feed  <= rxtlp_firstdw_be;
1152
                                                pcie_bar0_wb_data_o_feed <= bram_rxtlp_readdata;
1153
                                                tlp_state_copy <= tlp_state;
1154
                                                if (tlp_state_copy = tlp_state) then
1155
                                                  start_write_wb0 <= '0';
1156
                                                else --generate just one pulse, at the first clk cycle in this state
1157
                                                  start_write_wb0 <= '1';
1158
                                                end if;
1159
                                                if (wb_transaction_complete='1') then --one DW transfer completed
1160
 
1161
                                                        if (tlp_payloadsize_dwords = tlp_datacount) then --all data completed
1162
                                                          tlp_state <= "00000000"; --to idle
1163
                                                        else
1164
                                                          tlp_state <= "00010100"; --restart wb transaction with new data
1165
                                                          bram_rxtlp_readaddress <= bram_rxtlp_readaddress +1;
1166
                                                          tlp_datacount <= tlp_datacount +1;
1167
                                                        end if;
1168
                                                end if;
1169
                --* Write restart state *
1170
                when "00010100" =>   --state 20
1171
                                                tlp_state <= "00000010";
1172
 
1173
 
1174
                --********** READ STATE **********
1175
                                         --initiate WB read, then go to completion state
1176
                when "00000011" =>   --state 3
1177
                                                pcie_bar0_wb_addr_o_feed(27 downto 2) <= rxtlp_decodedaddress(27 downto 2) + tlp_datacount -1;
1178
                                                pcie_bar0_wb_addr_o_feed(1 downto 0) <= bit10(1 downto 0);
1179
                                                pcie_bar0_wb_sel_o_feed  <= rxtlp_firstdw_be;
1180
                                                tlp_state_copy <= tlp_state;
1181
                                                if (tlp_state_copy = tlp_state) then
1182
                                                  start_read_wb0 <= '0';
1183
                                                else --generate just one pulse
1184
                                                  start_read_wb0 <= '1';
1185
                                                end if;
1186
                                                if (wb_transaction_complete='1') then
1187
                                                        bram_txtlp_writedata <= pcie_bar0_wb_data_i_latched;
1188
                                                        bram_txtlp_we <= "1";
1189
                                                        if (tlp_payloadsize_dwords = tlp_datacount)then
1190
                                                          tlp_state <= "01111110"; --read completion
1191
                                                          --bram_txtlp_writeaddress remains the same to capture data in next clock cycle
1192
                                                        else
1193
                                                          tlp_state <= "00011110"; --one more wb read
1194
                                                          bram_txtlp_writeaddress <= bram_txtlp_writeaddress +1;
1195
                                                          tlp_datacount <= tlp_datacount +1;
1196
                                                        end if;
1197
                                                else
1198
                                                  bram_txtlp_we <= "0";
1199
                                                end if;
1200
                --* read restart STATE  *
1201
                when "00011110" =>   --state 30
1202
                                                tlp_state <= "00000011";
1203
                                                bram_txtlp_we <= "0";
1204
                --intermediate state before completion (to ensure data latch at address-4)
1205
                                         when "01111110" =>   --state 126
1206
                                                tlp_state <= "00000100";
1207
                                                bram_txtlp_writeaddress  <=  (OTHERS => '0');
1208
                                                --pre-write header-DW1:
1209
                                                bram_txtlp_writedata (31) <= flag1; --reserved
1210
                                                bram_txtlp_writedata (30 downto 24) <= "1001010"; --type= rd completion
1211
                                                bram_txtlp_writedata (23 downto 0) <= rxdw1_23_0; --various fields pcie_received_tlp (23 downto 0);
1212
                                                --Calculate completion header's "rcompl_bytecount_field" from rxtlp_firstdw_be, rxtlp_lastdw_be, tlp_payloadsize_dwords
1213
                                                if (rxtlp_lastdw_be="0000") then  --max 1DW
1214
                                                  if (rxtlp_firstdw_be="1111") then --4bytes
1215
                                                    rcompl_bytecount_field <= "0000000100";
1216
                                                  elsif (rxtlp_firstdw_be="0111" or rxtlp_firstdw_be="1110") then
1217
                                                    rcompl_bytecount_field <= "0000000011";
1218
                                                  elsif (rxtlp_firstdw_be="0011" or rxtlp_firstdw_be="1100" or rxtlp_firstdw_be="0110") then
1219
                                                    rcompl_bytecount_field <= "0000000010";
1220
                                                  else
1221
                                                    rcompl_bytecount_field <= "0000000001";
1222
                                                  end if;
1223
                                                else --more than 1DW: right now we dont support non-aligned multi-Dword accesses
1224
                                                  rcompl_bytecount_field(9 downto 2) <= tlp_payloadsize_dwords;
1225
                                                  rcompl_bytecount_field(1 downto 0) <= "00";
1226
                                                end if;
1227
 
1228
 
1229
                --********** READ COMPLETION STATE **********
1230
                                         --assemble the tx TLP and initiate the transmit
1231
                                         --buffer signals bram_txtlp_we, bram_txtlp_writeaddress, bram_txtlp_writedata
1232
                when "00000100" =>   --state 4
1233
                    tlp_state_copy <= tlp_state;
1234
                                                  bram_txtlp_writeaddress <= bram_txtlp_writeaddress +1;
1235
                                                  if (bram_txtlp_writeaddress="000000000") then --if address is 0: launch data for next lock/address(1): header-2.dw
1236
                                                          bram_txtlp_writedata (31 downto 16) <= cfg_completer_id; --completer ID
1237
                                                          bram_txtlp_writedata (15 downto 13) <= "000"; --status= succesful***
1238
                                                          bram_txtlp_writedata (12) <= '0'; --reserved
1239
                                                          bram_txtlp_writedata (11 downto 10) <= "00";
1240
                                                          bram_txtlp_writedata (9 downto 0) <= rcompl_bytecount_field; --total bytes returned
1241
                                                          bram_txtlp_we <= "1";
1242
                                                  elsif (bram_txtlp_writeaddress="000000001") then --if address is 1: launch data for next lock/address(2): header-3.dw
1243
                                                          bram_txtlp_writedata (31 downto 16) <= rxtlp_requesterid; --requester ID
1244
                                                          bram_txtlp_writedata (15 downto 8) <= pcie_rxtlp_tag ; --pcie_received_tlp (47 downto 40);--tag
1245
                                                          bram_txtlp_writedata (7) <= '0'; --reserved
1246
                                                          bram_txtlp_writedata (6 downto 2) <= rxtlp_decodedaddress(6 downto 2); --lower address
1247
                                                          bram_txtlp_writedata (1 downto 0) <= bit10(1 downto 0);                   --lower address
1248
                                                  else --data dwords, disable writes from next clock cycle
1249
                                                    bram_txtlp_we <= "0";
1250
                                                  end if;
1251
                                                  --one pulse to start the ep-if statemachine, upon arriving to this state
1252
                                                        if (tlp_state_copy = tlp_state) then
1253
                                                          pcie_there_is_a_new_tlp_to_transmit  <= '0';
1254
                                                        else
1255
                                                          pcie_there_is_a_new_tlp_to_transmit  <= '1';
1256
                                                        end if;
1257
                                                        --back to idle when the ep-if tx is finished: (wait to avoid overwrite)
1258
                                                        if (pcie_tlp_tx_complete='1') then
1259
                                                                tlp_state <= "00000000";
1260
                                                        end if;
1261
 
1262
 
1263
                --********** UNSUPPORTED REQUEST STATE **********
1264
                                         --completion response with status=001
1265
                when "00000101" =>   --state 5
1266
                    tlp_state_copy <= tlp_state;
1267
                                                  tlp_payloadsize_dwords <= "00000000";
1268 3 buenos
                                                  bram_txtlp_writeaddress <= bram_txtlp_writeaddress +1;
1269 2 buenos
                                                  --assembling the TLP packet:           )
1270
                                                  if (bram_txtlp_writeaddress="111111111") then --header 1.dw
1271
                                                    bram_txtlp_we <= "1";
1272
                                                          bram_txtlp_writedata (31) <= flag1; --reserved
1273
                                                          bram_txtlp_writedata (30 downto 24) <= "1001010"; --type= rd completion
1274
                                                          bram_txtlp_writedata (23 downto 0) <= rxdw1_23_0; --various fields pcie_received_tlp (23 downto 0);
1275
                                                  elsif (bram_txtlp_writeaddress="000000000") then --header 2.dw
1276
                                                    bram_txtlp_we <= "1";
1277
                                                          bram_txtlp_writedata (31 downto 16) <= cfg_completer_id; --completer ID
1278
                                                          bram_txtlp_writedata (15 downto 13) <= "000"; --status= UNSUPPORTED REQUEST ***
1279
                                                          bram_txtlp_writedata (12) <= '0'; --reserved
1280
                                                          bram_txtlp_writedata (11 downto 0) <= "000000000000"; --remaining byte count
1281
                                                  elsif (bram_txtlp_writeaddress="000000001") then --header 3.dw
1282
                                                    bram_txtlp_we <= "1";
1283
                                                          bram_txtlp_writedata (31 downto 16) <= rxtlp_requesterid; --requester ID
1284
                                                          bram_txtlp_writedata (15 downto 8) <= pcie_rxtlp_tag ; --pcie_received_tlp (47 downto 40);--tag
1285
                                                          bram_txtlp_writedata (7) <= '0'; --reserved
1286
                                                          bram_txtlp_writedata (6 downto 2) <= rxtlp_decodedaddress(6 downto 2); --lower address
1287
                                                          bram_txtlp_writedata (1 downto 0) <= bit10(1 downto 0);                   --lower address
1288
                                                  else --data dwords 
1289
                                                    bram_txtlp_we <= "0";
1290
                                                  end if;
1291
                                                        --one pulse to start the ep-if statemachine, upon arriving to this state
1292
                                                        if (tlp_state_copy = tlp_state) then
1293
                                                          pcie_there_is_a_new_tlp_to_transmit  <= '0';
1294
                                                        else
1295
                                                          pcie_there_is_a_new_tlp_to_transmit  <= '1';
1296
                                                        end if;
1297
                                                        --back to idle when finished:
1298
                                                        if (pcie_tlp_tx_complete='1') then
1299
                                                                tlp_state <= "00000000";
1300
                                                        end if;
1301
 
1302
                when others => --error
1303
                      tlp_state <= "00000000"; --go to state 0
1304
                end case;
1305
 
1306
       end if;
1307
    end if;
1308
    end process; --end tlp statemachine
1309
 
1310
 
1311
 
1312
 
1313
        --byte enable encoding to wb_address bit1:0
1314 3 buenos
        --this does not swap the endian, since only the data is swapped in the pcie packets.
1315
         process ( pciewb_localreset_n, rxtlp_firstdw_be )
1316
    begin
1317
       if (pciewb_localreset_n = '0') then
1318
           bit10(1 downto 0) <="00";
1319
       else
1320
         if (rxtlp_firstdw_be ="0001") then
1321
                          bit10(1 downto 0) <= "00";
1322
         elsif (rxtlp_firstdw_be ="0010") then
1323
                          bit10(1 downto 0) <= "01";
1324
         elsif (rxtlp_firstdw_be ="0100") then
1325
                          bit10(1 downto 0) <= "10";
1326
         elsif (rxtlp_firstdw_be ="1000") then
1327
                          bit10(1 downto 0) <= "11";
1328
         elsif (rxtlp_firstdw_be ="0011") then
1329
                          bit10(1 downto 0) <= "00";
1330
         elsif (rxtlp_firstdw_be ="1100") then
1331
                          bit10(1 downto 0) <= "10";
1332
         elsif (rxtlp_firstdw_be ="1111") then
1333
                          bit10(1 downto 0) <= "00";
1334
                        else --this should never happen
1335
                          bit10(1 downto 0) <= "00";
1336
                        end if;
1337
       end if;
1338 2 buenos
    end process;
1339
 
1340
 
1341
 
1342
 
1343
 
1344
        -- INTERRUPTS: -------------------------------------------------------------------------
1345
        --to assert an interrupt, use the cfg_interrupt_assert_n pin.
1346
        --datasheet text:
1347
        --As shown in Figure 6-30, the user application first asserts cfg_interrupt_n and
1348
        --cfg_interrupt_assert_n to assert the interrupt. The user application should select a
1349
        --specific interrupt (INTA, INTB, INTC, or INTD) using cfg_interrupt_di[7:0] as shown
1350
        --in Table 6-19.
1351
        -- The core then asserts cfg_interrupt_rdy_n to indicate the interrupt has been accepted.
1352
        --On the following clock cycle, the user application deasserts cfg_interrupt_n and, if the
1353
        --Interrupt Disable bit in the PCI Command register is set to 0, the core sends an assert
1354
        --interrupt message (Assert_INTA, Assert_INTB, and so forth).
1355
        -- After the user application has determined that the interrupt has been serviced, it
1356
        --asserts cfg_interrupt_n while deasserting cfg_interrupt_assert_n to deassert the
1357
        --interrupt. The appropriate interrupt must be indicated via cfg_interrupt_di[7:0].
1358
        -- The core then asserts cfg_interrupt_rdy_n to indicate the interrupt deassertion has
1359
        --been accepted. On the following clock cycle, the user application deasserts
1360
        --cfg_interrupt_n and the core sends a deassert interrupt message (Deassert_INTA,
1361
        --Deassert_INTB, and so forth).
1362
        --cfg_interrupt_di[7:0] value Legacy Interrupt
1363
        --00h INTA
1364
        --01h INTB
1365
        --02h INTC
1366
        --03h INTD 
1367
 
1368
        cfg_interrupt_di    <= "00000000"; --intA used
1369
 
1370
    process (pciewb_localreset_n, trn_clk, pcie_irq, pcieirq_state,
1371
                                cfg_interrupt_rdy_n)
1372
    begin
1373
    if (pciewb_localreset_n='0') then
1374
       pcieirq_state <= "00000000";
1375
       cfg_interrupt_n <= '1';
1376
                 cfg_interrupt_assert_n_1 <= '1';
1377
    else
1378
      if (trn_clk'event and trn_clk = '1') then
1379
                case ( pcieirq_state ) is
1380
 
1381
                --********** idle STATE  **********
1382
                when "00000000" =>   --state 0        
1383
                    if (pcie_irq = '1') then
1384
                                                    pcieirq_state <= "00000001";
1385
                                                         cfg_interrupt_n <= '0'; --active
1386
                                                  else
1387
                                                    cfg_interrupt_n <= '1'; --inactive
1388
                                                  end if;
1389
                                                  cfg_interrupt_assert_n_1 <= '0'; --0=assert, 1=deassert
1390
 
1391
                --********** assert STATE ********** 
1392
                when "00000001" =>   --state 1
1393
                                                 if (cfg_interrupt_rdy_n ='0') then --ep accepted it
1394
                                                         cfg_interrupt_n <= '1'; --deassert the request 
1395
                                                         pcieirq_state <= "00000010";
1396
                                                 else
1397
                                                         cfg_interrupt_n <= '0'; --request INTA assertion
1398
                                                 end if;
1399
 
1400
                --********** pcie_irq kept asserted STATE **********                                     
1401
                when "00000010" =>   --state 2
1402
                    if (pcie_irq = '0') then --pcie_irq gets deasserted
1403
                                                    pcieirq_state <= "00000011";
1404
                                                  end if;
1405
                                                 cfg_interrupt_n <= '1'; --inactive     
1406
                                                 cfg_interrupt_assert_n_1 <= '1'; --0=assert, 1=deassert
1407
 
1408
                --********** DEassert STATE ********** 
1409
                when "00000011" =>   --state 3
1410
                                                 if (cfg_interrupt_rdy_n ='0') then --ep accepted it
1411
                                                         cfg_interrupt_n <= '1'; --deassert the request 
1412
                                                         pcieirq_state <= "00000000";
1413
                                                 else
1414
                                                         cfg_interrupt_n <= '0'; --request INTA DEassertion
1415
                                                 end if;
1416
 
1417
                                                 when others => --error
1418
                      pcieirq_state <= "00000000"; --go to state 0
1419
                end case;
1420
       end if;
1421
    end if;
1422
    end process;
1423
 
1424
        --this (little delay) is to fix a hold time violation created inside the pcie-ep ip:
1425
        cfg_interrupt_assert_n <= cfg_interrupt_assert_n_1 or (not pciewb_localreset_n);
1426
 
1427
 
1428
 
1429
 
1430
 
1431
 
1432
-- -------- END OF FILE -------------------------------------------------------------------------------------
1433
end Behavioral;
1434
 
1435
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.