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 2

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

powered by: WebSVN 2.1.0

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