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

Subversion Repositories pcie_mini

[/] [pcie_mini/] [trunk/] [main_sources/] [xilinx_pcie2wb.vhd] - Blame information for rev 9

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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