Esempio n. 1
0
def bench():

    # Parameters


    # Inputs
    clk = Signal(bool(0))
    rst = Signal(bool(0))
    current_test = Signal(intbv(0)[8:])

    m_axis_rq_tready = Signal(bool(0))
    s_axis_rc_tdata = Signal(intbv(0)[256:])
    s_axis_rc_tkeep = Signal(intbv(0)[8:])
    s_axis_rc_tlast = Signal(bool(0))
    s_axis_rc_tuser = Signal(intbv(0)[75:])
    s_axis_rc_tvalid = Signal(bool(0))
    s_axis_cq_tdata = Signal(intbv(0)[256:])
    s_axis_cq_tkeep = Signal(intbv(0)[8:])
    s_axis_cq_tlast = Signal(bool(0))
    s_axis_cq_tuser = Signal(intbv(0)[88:])
    s_axis_cq_tvalid = Signal(bool(0))
    m_axis_cc_tready = Signal(bool(0))
    cfg_max_payload = Signal(intbv(0)[2:])
    cfg_max_read_req = Signal(intbv(0)[3:])
    cfg_mgmt_read_data = Signal(intbv(0)[32:])
    cfg_mgmt_read_write_done = Signal(bool(0))
    cfg_interrupt_msi_enable = Signal(intbv(0)[4:])
    cfg_interrupt_msi_mmenable = Signal(intbv(0)[12:])
    cfg_interrupt_msi_mask_update = Signal(bool(0))
    cfg_interrupt_msi_data = Signal(intbv(0)[32:])
    cfg_interrupt_msi_sent = Signal(bool(0))
    cfg_interrupt_msi_fail = Signal(bool(0))

    # Outputs
    user_led_g = Signal(intbv(0)[2:])
    user_led_r = Signal(bool(0))
    front_led = Signal(intbv(0)[2:])
    m_axis_rq_tdata = Signal(intbv(0)[256:])
    m_axis_rq_tkeep = Signal(intbv(0)[8:])
    m_axis_rq_tlast = Signal(bool(0))
    m_axis_rq_tuser = Signal(intbv(0)[62:])
    m_axis_rq_tvalid = Signal(bool(0))
    s_axis_rc_tready = Signal(bool(0))
    s_axis_cq_tready = Signal(bool(0))
    m_axis_cc_tdata = Signal(intbv(0)[256:])
    m_axis_cc_tkeep = Signal(intbv(0)[8:])
    m_axis_cc_tlast = Signal(bool(0))
    m_axis_cc_tuser = Signal(intbv(0)[33:])
    m_axis_cc_tvalid = Signal(bool(0))
    status_error_cor = Signal(bool(0))
    status_error_uncor = Signal(bool(0))
    cfg_mgmt_addr = Signal(intbv(0)[10:])
    cfg_mgmt_function_number = Signal(intbv(0)[8:])
    cfg_mgmt_write = Signal(bool(0))
    cfg_mgmt_write_data = Signal(intbv(0)[32:])
    cfg_mgmt_byte_enable = Signal(intbv(0)[4:])
    cfg_mgmt_read = Signal(bool(0))
    cfg_interrupt_msi_int = Signal(intbv(0)[32:])
    cfg_interrupt_msi_pending_status = Signal(intbv(0)[32:])
    cfg_interrupt_msi_select = Signal(intbv(0)[2:])
    cfg_interrupt_msi_pending_status_function_num = Signal(intbv(0)[2:])
    cfg_interrupt_msi_pending_status_data_enable = Signal(bool(0))
    cfg_interrupt_msi_attr = Signal(intbv(0)[3:])
    cfg_interrupt_msi_tph_present = Signal(bool(0))
    cfg_interrupt_msi_tph_type = Signal(intbv(0)[2:])
    cfg_interrupt_msi_tph_st_tag = Signal(intbv(0)[8:])
    cfg_interrupt_msi_function_number = Signal(intbv(0)[8:])

    # Clock and Reset Interface
    user_clk=Signal(bool(0))
    user_reset=Signal(bool(0))
    sys_clk=Signal(bool(0))
    sys_reset=Signal(bool(0))

    # PCIe devices
    rc = pcie.RootComplex()

    mem_base, mem_data = rc.alloc_region(16*1024*1024)

    dev = pcie_usp.UltrascalePlusPCIe()

    dev.pcie_generation = 3
    dev.pcie_link_width = 8
    dev.user_clock_frequency = 256e6

    dev.functions[0].msi_multiple_message_capable = 5

    dev.functions[0].configure_bar(0, 4*1024*1024)
    dev.functions[0].configure_bar(1, 4*1024*1024)

    rc.make_port().connect(dev)

    pcie_logic = dev.create_logic(
        # Completer reQuest Interface
        m_axis_cq_tdata=s_axis_cq_tdata,
        m_axis_cq_tuser=s_axis_cq_tuser,
        m_axis_cq_tlast=s_axis_cq_tlast,
        m_axis_cq_tkeep=s_axis_cq_tkeep,
        m_axis_cq_tvalid=s_axis_cq_tvalid,
        m_axis_cq_tready=s_axis_cq_tready,
        #pcie_cq_np_req=pcie_cq_np_req,
        pcie_cq_np_req=Signal(intbv(1)[2:]),
        #pcie_cq_np_req_count=pcie_cq_np_req_count,

        # Completer Completion Interface
        s_axis_cc_tdata=m_axis_cc_tdata,
        s_axis_cc_tuser=m_axis_cc_tuser,
        s_axis_cc_tlast=m_axis_cc_tlast,
        s_axis_cc_tkeep=m_axis_cc_tkeep,
        s_axis_cc_tvalid=m_axis_cc_tvalid,
        s_axis_cc_tready=m_axis_cc_tready,

        # Requester reQuest Interface
        s_axis_rq_tdata=m_axis_rq_tdata,
        s_axis_rq_tuser=m_axis_rq_tuser,
        s_axis_rq_tlast=m_axis_rq_tlast,
        s_axis_rq_tkeep=m_axis_rq_tkeep,
        s_axis_rq_tvalid=m_axis_rq_tvalid,
        s_axis_rq_tready=m_axis_rq_tready,
        #pcie_rq_seq_num0=pcie_rq_seq_num0,
        #pcie_rq_seq_num_vld0=pcie_rq_seq_num_vld0,
        #pcie_rq_seq_num1=pcie_rq_seq_num1,
        #pcie_rq_seq_num_vld1=pcie_rq_seq_num_vld1,
        #pcie_rq_tag0=pcie_rq_tag0,
        #pcie_rq_tag1=pcie_rq_tag1,
        #pcie_rq_tag_av=pcie_rq_tag_av,
        #pcie_rq_tag_vld0=pcie_rq_tag_vld0,
        #pcie_rq_tag_vld1=pcie_rq_tag_vld1,

        # Requester Completion Interface
        m_axis_rc_tdata=s_axis_rc_tdata,
        m_axis_rc_tuser=s_axis_rc_tuser,
        m_axis_rc_tlast=s_axis_rc_tlast,
        m_axis_rc_tkeep=s_axis_rc_tkeep,
        m_axis_rc_tvalid=s_axis_rc_tvalid,
        m_axis_rc_tready=s_axis_rc_tready,

        # Transmit Flow Control Interface
        #pcie_tfc_nph_av=pcie_tfc_nph_av,
        #pcie_tfc_npd_av=pcie_tfc_npd_av,

        # Configuration Management Interface
        cfg_mgmt_addr=cfg_mgmt_addr,
        cfg_mgmt_function_number=cfg_mgmt_function_number,
        cfg_mgmt_write=cfg_mgmt_write,
        cfg_mgmt_write_data=cfg_mgmt_write_data,
        cfg_mgmt_byte_enable=cfg_mgmt_byte_enable,
        cfg_mgmt_read=cfg_mgmt_read,
        cfg_mgmt_read_data=cfg_mgmt_read_data,
        cfg_mgmt_read_write_done=cfg_mgmt_read_write_done,
        #cfg_mgmt_debug_access=cfg_mgmt_debug_access,

        # Configuration Status Interface
        #cfg_phy_link_down=cfg_phy_link_down,
        #cfg_phy_link_status=cfg_phy_link_status,
        #cfg_negotiated_width=cfg_negotiated_width,
        #cfg_current_speed=cfg_current_speed,
        cfg_max_payload=cfg_max_payload,
        cfg_max_read_req=cfg_max_read_req,
        #cfg_function_status=cfg_function_status,
        #cfg_vf_status=cfg_vf_status,
        #cfg_function_power_state=cfg_function_power_state,
        #cfg_vf_power_state=cfg_vf_power_state,
        #cfg_link_power_state=cfg_link_power_state,
        #cfg_err_cor_out=cfg_err_cor_out,
        #cfg_err_nonfatal_out=cfg_err_nonfatal_out,
        #cfg_err_fatal_out=cfg_err_fatal_out,
        #cfg_local_err_out=cfg_local_err_out,
        #cfg_local_err_valid=cfg_local_err_valid,
        #cfg_rx_pm_state=cfg_rx_pm_state,
        #cfg_tx_pm_state=cfg_tx_pm_state,
        #cfg_ltssm_state=cfg_ltssm_state,
        #cfg_rcb_status=cfg_rcb_status,
        #cfg_obff_enable=cfg_obff_enable,
        #cfg_pl_status_change=cfg_pl_status_change,
        #cfg_tph_requester_enable=cfg_tph_requester_enable,
        #cfg_tph_st_mode=cfg_tph_st_mode,
        #cfg_vf_tph_requester_enable=cfg_vf_tph_requester_enable,
        #cfg_vf_tph_st_mode=cfg_vf_tph_st_mode,

        # Configuration Received Message Interface
        #cfg_msg_received=cfg_msg_received,
        #cfg_msg_received_data=cfg_msg_received_data,
        #cfg_msg_received_type=cfg_msg_received_type,

        # Configuration Transmit Message Interface
        #cfg_msg_transmit=cfg_msg_transmit,
        #cfg_msg_transmit_type=cfg_msg_transmit_type,
        #cfg_msg_transmit_data=cfg_msg_transmit_data,
        #cfg_msg_transmit_done=cfg_msg_transmit_done,

        # Configuration Flow Control Interface
        #cfg_fc_ph=cfg_fc_ph,
        #cfg_fc_pd=cfg_fc_pd,
        #cfg_fc_nph=cfg_fc_nph,
        #cfg_fc_npd=cfg_fc_npd,
        #cfg_fc_cplh=cfg_fc_cplh,
        #cfg_fc_cpld=cfg_fc_cpld,
        #cfg_fc_sel=cfg_fc_sel,

        # Configuration Control Interface
        #cfg_hot_reset_in=cfg_hot_reset_in,
        #cfg_hot_reset_out=cfg_hot_reset_out,
        #cfg_config_space_enable=cfg_config_space_enable,
        #cfg_dsn=cfg_dsn,
        #cfg_ds_port_number=cfg_ds_port_number,
        #cfg_ds_bus_number=cfg_ds_bus_number,
        #cfg_ds_device_number=cfg_ds_device_number,
        #cfg_ds_function_number=cfg_ds_function_number,
        #cfg_power_state_change_ack=cfg_power_state_change_ack,
        #cfg_power_state_change_interrupt=cfg_power_state_change_interrupt,
        cfg_err_cor_in=status_error_cor,
        cfg_err_uncor_in=status_error_uncor,
        #cfg_flr_done=cfg_flr_done,
        #cfg_vf_flr_done=cfg_vf_flr_done,
        #cfg_flr_in_process=cfg_flr_in_process,
        #cfg_vf_flr_in_process=cfg_vf_flr_in_process,
        #cfg_req_pm_transition_l23_ready=cfg_req_pm_transition_l23_ready,
        #cfg_link_training_enable=cfg_link_training_enable,

        # Configuration Interrupt Controller Interface
        #cfg_interrupt_int=cfg_interrupt_int,
        #cfg_interrupt_sent=cfg_interrupt_sent,
        #cfg_interrupt_pending=cfg_interrupt_pending,
        cfg_interrupt_msi_enable=cfg_interrupt_msi_enable,
        cfg_interrupt_msi_mmenable=cfg_interrupt_msi_mmenable,
        cfg_interrupt_msi_mask_update=cfg_interrupt_msi_mask_update,
        cfg_interrupt_msi_data=cfg_interrupt_msi_data,
        cfg_interrupt_msi_select=cfg_interrupt_msi_select,
        cfg_interrupt_msi_int=cfg_interrupt_msi_int,
        cfg_interrupt_msi_pending_status=cfg_interrupt_msi_pending_status,
        cfg_interrupt_msi_pending_status_data_enable=cfg_interrupt_msi_pending_status_data_enable,
        cfg_interrupt_msi_pending_status_function_num=cfg_interrupt_msi_pending_status_function_num,
        cfg_interrupt_msi_sent=cfg_interrupt_msi_sent,
        cfg_interrupt_msi_fail=cfg_interrupt_msi_fail,
        #cfg_interrupt_msix_enable=cfg_interrupt_msix_enable,
        #cfg_interrupt_msix_mask=cfg_interrupt_msix_mask,
        #cfg_interrupt_msix_vf_enable=cfg_interrupt_msix_vf_enable,
        #cfg_interrupt_msix_vf_mask=cfg_interrupt_msix_vf_mask,
        #cfg_interrupt_msix_address=cfg_interrupt_msix_address,
        #cfg_interrupt_msix_data=cfg_interrupt_msix_data,
        #cfg_interrupt_msix_int=cfg_interrupt_msix_int,
        #cfg_interrupt_msix_vec_pending=cfg_interrupt_msix_vec_pending,
        #cfg_interrupt_msix_vec_pending_status=cfg_interrupt_msix_vec_pending_status,
        cfg_interrupt_msi_attr=cfg_interrupt_msi_attr,
        cfg_interrupt_msi_tph_present=cfg_interrupt_msi_tph_present,
        cfg_interrupt_msi_tph_type=cfg_interrupt_msi_tph_type,
        cfg_interrupt_msi_tph_st_tag=cfg_interrupt_msi_tph_st_tag,
        cfg_interrupt_msi_function_number=cfg_interrupt_msi_function_number,

        # Configuration Extend Interface
        #cfg_ext_read_received=cfg_ext_read_received,
        #cfg_ext_write_received=cfg_ext_write_received,
        #cfg_ext_register_number=cfg_ext_register_number,
        #cfg_ext_function_number=cfg_ext_function_number,
        #cfg_ext_write_data=cfg_ext_write_data,
        #cfg_ext_write_byte_enable=cfg_ext_write_byte_enable,
        #cfg_ext_read_data=cfg_ext_read_data,
        #cfg_ext_read_data_valid=cfg_ext_read_data_valid,

        # Clock and Reset Interface
        user_clk=user_clk,
        user_reset=user_reset,
        #user_lnk_up=user_lnk_up,
        sys_clk=sys_clk,
        sys_clk_gt=sys_clk,
        sys_reset=sys_reset,
        #phy_rdy_out=phy_rdy_out
    )

    # DUT
    if os.system(build_cmd):
        raise Exception("Error running build command")

    dut = Cosimulation(
        "vvp -m myhdl %s.vvp -lxt2" % testbench,
        clk=user_clk,
        rst=user_reset,
        current_test=current_test,
        user_led_g=user_led_g,
        user_led_r=user_led_r,
        front_led=front_led,
        m_axis_rq_tdata=m_axis_rq_tdata,
        m_axis_rq_tkeep=m_axis_rq_tkeep,
        m_axis_rq_tlast=m_axis_rq_tlast,
        m_axis_rq_tready=m_axis_rq_tready,
        m_axis_rq_tuser=m_axis_rq_tuser,
        m_axis_rq_tvalid=m_axis_rq_tvalid,
        s_axis_rc_tdata=s_axis_rc_tdata,
        s_axis_rc_tkeep=s_axis_rc_tkeep,
        s_axis_rc_tlast=s_axis_rc_tlast,
        s_axis_rc_tready=s_axis_rc_tready,
        s_axis_rc_tuser=s_axis_rc_tuser,
        s_axis_rc_tvalid=s_axis_rc_tvalid,
        s_axis_cq_tdata=s_axis_cq_tdata,
        s_axis_cq_tkeep=s_axis_cq_tkeep,
        s_axis_cq_tlast=s_axis_cq_tlast,
        s_axis_cq_tready=s_axis_cq_tready,
        s_axis_cq_tuser=s_axis_cq_tuser,
        s_axis_cq_tvalid=s_axis_cq_tvalid,
        m_axis_cc_tdata=m_axis_cc_tdata,
        m_axis_cc_tkeep=m_axis_cc_tkeep,
        m_axis_cc_tlast=m_axis_cc_tlast,
        m_axis_cc_tready=m_axis_cc_tready,
        m_axis_cc_tuser=m_axis_cc_tuser,
        m_axis_cc_tvalid=m_axis_cc_tvalid,
        cfg_max_payload=cfg_max_payload,
        cfg_max_read_req=cfg_max_read_req,
        cfg_mgmt_addr=cfg_mgmt_addr,
        cfg_mgmt_function_number=cfg_mgmt_function_number,
        cfg_mgmt_write=cfg_mgmt_write,
        cfg_mgmt_write_data=cfg_mgmt_write_data,
        cfg_mgmt_byte_enable=cfg_mgmt_byte_enable,
        cfg_mgmt_read=cfg_mgmt_read,
        cfg_mgmt_read_data=cfg_mgmt_read_data,
        cfg_mgmt_read_write_done=cfg_mgmt_read_write_done,
        cfg_interrupt_msi_enable=cfg_interrupt_msi_enable,
        cfg_interrupt_msi_int=cfg_interrupt_msi_int,
        cfg_interrupt_msi_sent=cfg_interrupt_msi_sent,
        cfg_interrupt_msi_fail=cfg_interrupt_msi_fail,
        cfg_interrupt_msi_mmenable=cfg_interrupt_msi_mmenable,
        cfg_interrupt_msi_pending_status=cfg_interrupt_msi_pending_status,
        cfg_interrupt_msi_mask_update=cfg_interrupt_msi_mask_update,
        cfg_interrupt_msi_select=cfg_interrupt_msi_select,
        cfg_interrupt_msi_data=cfg_interrupt_msi_data,
        cfg_interrupt_msi_pending_status_function_num=cfg_interrupt_msi_pending_status_function_num,
        cfg_interrupt_msi_pending_status_data_enable=cfg_interrupt_msi_pending_status_data_enable,
        cfg_interrupt_msi_attr=cfg_interrupt_msi_attr,
        cfg_interrupt_msi_tph_present=cfg_interrupt_msi_tph_present,
        cfg_interrupt_msi_tph_type=cfg_interrupt_msi_tph_type,
        cfg_interrupt_msi_tph_st_tag=cfg_interrupt_msi_tph_st_tag,
        cfg_interrupt_msi_function_number=cfg_interrupt_msi_function_number,
        status_error_cor=status_error_cor,
        status_error_uncor=status_error_uncor
    )

    @always(delay(5))
    def clkgen():
        clk.next = not clk

    @always_comb
    def clk_logic():
        sys_clk.next = clk
        sys_reset.next = not rst

    @instance
    def check():
        yield delay(100)
        yield clk.posedge
        rst.next = 1
        yield clk.posedge
        rst.next = 0
        yield clk.posedge
        yield delay(100)
        yield clk.posedge

        # testbench stimulus

        current_tag = 1

        yield clk.posedge
        print("test 1: enumeration")
        current_test.next = 1

        yield rc.enumerate(enable_bus_mastering=True, configure_msi=True)

        dev_pf0_bar0 = dev.functions[0].bar[0] & 0xfffffffc
        dev_pf0_bar1 = dev.functions[0].bar[1] & 0xfffffffc

        yield delay(100)

        yield clk.posedge
        print("test 2: memory write to bar 1")
        current_test.next = 2

        yield rc.mem_write(dev_pf0_bar1, b'\x11\x22\x33\x44')

        yield delay(100)

        yield clk.posedge
        print("test 3: memory read from bar 1")
        current_test.next = 3

        val = yield from rc.mem_read(dev_pf0_bar1, 4, 1000)
        print(val)
        assert val == b'\x11\x22\x33\x44'

        yield delay(100)

        yield clk.posedge
        print("test 4: test DMA")
        current_test.next = 4

        # write packet data
        mem_data[0:1024] = bytearray([x%256 for x in range(1024)])

        # enable DMA
        yield rc.mem_write(dev_pf0_bar0+0x100000, struct.pack('<L', 1))

        # write pcie read descriptor
        yield rc.mem_write(dev_pf0_bar0+0x100100, struct.pack('<L', (mem_base+0x0000) & 0xffffffff))
        yield rc.mem_write(dev_pf0_bar0+0x100104, struct.pack('<L', (mem_base+0x0000 >> 32) & 0xffffffff))
        yield rc.mem_write(dev_pf0_bar0+0x100108, struct.pack('<L', (0x100) & 0xffffffff))
        yield rc.mem_write(dev_pf0_bar0+0x10010C, struct.pack('<L', (0x100 >> 32) & 0xffffffff))
        yield rc.mem_write(dev_pf0_bar0+0x100110, struct.pack('<L', 0x400))
        yield rc.mem_write(dev_pf0_bar0+0x100114, struct.pack('<L', 0xAA))

        yield delay(2000)

        # read status
        val = yield from rc.mem_read(dev_pf0_bar0+0x100118, 4)
        print(val)

        # write pcie write descriptor
        yield rc.mem_write(dev_pf0_bar0+0x100200, struct.pack('<L', (mem_base+0x1000) & 0xffffffff))
        yield rc.mem_write(dev_pf0_bar0+0x100204, struct.pack('<L', (mem_base+0x1000 >> 32) & 0xffffffff))
        yield rc.mem_write(dev_pf0_bar0+0x100208, struct.pack('<L', (0x100) & 0xffffffff))
        yield rc.mem_write(dev_pf0_bar0+0x10020C, struct.pack('<L', (0x100 >> 32) & 0xffffffff))
        yield rc.mem_write(dev_pf0_bar0+0x100210, struct.pack('<L', 0x400))
        yield rc.mem_write(dev_pf0_bar0+0x100214, struct.pack('<L', 0x55))

        yield delay(2000)

        # read status
        val = yield from rc.mem_read(dev_pf0_bar0+0x100218, 4)
        print(val)

        data = mem_data[0x1000:(0x1000)+64]
        for i in range(0, len(data), 16):
            print(" ".join(("{:02x}".format(c) for c in bytearray(data[i:i+16]))))

        assert mem_data[0:1024] == mem_data[0x1000:0x1000+1024]

        yield delay(100)

        raise StopSimulation

    return instances()
Esempio n. 2
0
def bench():

    # Parameters
    AXIS_PCIE_DATA_WIDTH = 512
    AXIS_PCIE_KEEP_WIDTH = (AXIS_PCIE_DATA_WIDTH / 32)
    AXIS_PCIE_RC_USER_WIDTH = 161
    AXIS_PCIE_RQ_USER_WIDTH = 137
    AXIS_PCIE_CQ_USER_WIDTH = 183
    AXIS_PCIE_CC_USER_WIDTH = 81
    RQ_SEQ_NUM_WIDTH = 6
    BAR0_APERTURE = 24

    # Inputs
    clk = Signal(bool(0))
    rst = Signal(bool(0))
    current_test = Signal(intbv(0)[8:])

    clk_250mhz = Signal(bool(0))
    rst_250mhz = Signal(bool(0))
    m_axis_rq_tready = Signal(bool(0))
    s_axis_rc_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    s_axis_rc_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    s_axis_rc_tlast = Signal(bool(0))
    s_axis_rc_tuser = Signal(intbv(0)[AXIS_PCIE_RC_USER_WIDTH:])
    s_axis_rc_tvalid = Signal(bool(0))
    s_axis_cq_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    s_axis_cq_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    s_axis_cq_tlast = Signal(bool(0))
    s_axis_cq_tuser = Signal(intbv(0)[AXIS_PCIE_CQ_USER_WIDTH:])
    s_axis_cq_tvalid = Signal(bool(0))
    m_axis_cc_tready = Signal(bool(0))
    s_axis_rq_seq_num_0 = Signal(intbv(0)[RQ_SEQ_NUM_WIDTH:])
    s_axis_rq_seq_num_valid_0 = Signal(bool(0))
    s_axis_rq_seq_num_1 = Signal(intbv(0)[RQ_SEQ_NUM_WIDTH:])
    s_axis_rq_seq_num_valid_1 = Signal(bool(0))
    pcie_tfc_nph_av = Signal(intbv(15)[4:])
    pcie_tfc_npd_av = Signal(intbv(15)[4:])
    cfg_max_payload = Signal(intbv(0)[2:])
    cfg_max_read_req = Signal(intbv(0)[3:])
    cfg_mgmt_read_data = Signal(intbv(0)[32:])
    cfg_mgmt_read_write_done = Signal(bool(0))
    cfg_fc_ph = Signal(intbv(0)[8:])
    cfg_fc_pd = Signal(intbv(0)[12:])
    cfg_fc_nph = Signal(intbv(0)[8:])
    cfg_fc_npd = Signal(intbv(0)[12:])
    cfg_fc_cplh = Signal(intbv(0)[8:])
    cfg_fc_cpld = Signal(intbv(0)[12:])
    cfg_interrupt_msi_enable = Signal(intbv(0)[4:])
    cfg_interrupt_msi_mmenable = Signal(intbv(0)[12:])
    cfg_interrupt_msi_mask_update = Signal(bool(0))
    cfg_interrupt_msi_data = Signal(intbv(0)[32:])
    cfg_interrupt_msi_sent = Signal(bool(0))
    cfg_interrupt_msi_fail = Signal(bool(0))
    qsfp0_tx_clk_1 = Signal(bool(0))
    qsfp0_tx_rst_1 = Signal(bool(0))
    qsfp0_rx_clk_1 = Signal(bool(0))
    qsfp0_rx_rst_1 = Signal(bool(0))
    qsfp0_rxd_1 = Signal(intbv(0)[64:])
    qsfp0_rxc_1 = Signal(intbv(0)[8:])
    qsfp0_tx_clk_2 = Signal(bool(0))
    qsfp0_tx_rst_2 = Signal(bool(0))
    qsfp0_rx_clk_2 = Signal(bool(0))
    qsfp0_rx_rst_2 = Signal(bool(0))
    qsfp0_rxd_2 = Signal(intbv(0)[64:])
    qsfp0_rxc_2 = Signal(intbv(0)[8:])
    qsfp0_tx_clk_3 = Signal(bool(0))
    qsfp0_tx_rst_3 = Signal(bool(0))
    qsfp0_rx_clk_3 = Signal(bool(0))
    qsfp0_rx_rst_3 = Signal(bool(0))
    qsfp0_rxd_3 = Signal(intbv(0)[64:])
    qsfp0_rxc_3 = Signal(intbv(0)[8:])
    qsfp0_tx_clk_4 = Signal(bool(0))
    qsfp0_tx_rst_4 = Signal(bool(0))
    qsfp0_rx_clk_4 = Signal(bool(0))
    qsfp0_rx_rst_4 = Signal(bool(0))
    qsfp0_rxd_4 = Signal(intbv(0)[64:])
    qsfp0_rxc_4 = Signal(intbv(0)[8:])
    qsfp1_tx_clk_1 = Signal(bool(0))
    qsfp1_tx_rst_1 = Signal(bool(0))
    qsfp1_rx_clk_1 = Signal(bool(0))
    qsfp1_rx_rst_1 = Signal(bool(0))
    qsfp1_rxd_1 = Signal(intbv(0)[64:])
    qsfp1_rxc_1 = Signal(intbv(0)[8:])
    qsfp1_tx_clk_2 = Signal(bool(0))
    qsfp1_tx_rst_2 = Signal(bool(0))
    qsfp1_rx_clk_2 = Signal(bool(0))
    qsfp1_rx_rst_2 = Signal(bool(0))
    qsfp1_rxd_2 = Signal(intbv(0)[64:])
    qsfp1_rxc_2 = Signal(intbv(0)[8:])
    qsfp1_tx_clk_3 = Signal(bool(0))
    qsfp1_tx_rst_3 = Signal(bool(0))
    qsfp1_rx_clk_3 = Signal(bool(0))
    qsfp1_rx_rst_3 = Signal(bool(0))
    qsfp1_rxd_3 = Signal(intbv(0)[64:])
    qsfp1_rxc_3 = Signal(intbv(0)[8:])
    qsfp1_tx_clk_4 = Signal(bool(0))
    qsfp1_tx_rst_4 = Signal(bool(0))
    qsfp1_rx_clk_4 = Signal(bool(0))
    qsfp1_rx_rst_4 = Signal(bool(0))
    qsfp1_rxd_4 = Signal(intbv(0)[64:])
    qsfp1_rxc_4 = Signal(intbv(0)[8:])

    # Outputs
    m_axis_rq_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    m_axis_rq_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    m_axis_rq_tlast = Signal(bool(0))
    m_axis_rq_tuser = Signal(intbv(0)[AXIS_PCIE_RQ_USER_WIDTH:])
    m_axis_rq_tvalid = Signal(bool(0))
    s_axis_rc_tready = Signal(bool(0))
    s_axis_cq_tready = Signal(bool(0))
    m_axis_cc_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    m_axis_cc_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    m_axis_cc_tlast = Signal(bool(0))
    m_axis_cc_tuser = Signal(intbv(0)[AXIS_PCIE_CC_USER_WIDTH:])
    m_axis_cc_tvalid = Signal(bool(0))
    status_error_cor = Signal(bool(0))
    status_error_uncor = Signal(bool(0))
    cfg_mgmt_addr = Signal(intbv(0)[10:])
    cfg_mgmt_function_number = Signal(intbv(0)[8:])
    cfg_mgmt_write = Signal(bool(0))
    cfg_mgmt_write_data = Signal(intbv(0)[32:])
    cfg_mgmt_byte_enable = Signal(intbv(0)[4:])
    cfg_mgmt_read = Signal(bool(0))
    cfg_fc_sel = Signal(intbv(4)[3:])
    cfg_interrupt_msi_int = Signal(intbv(0)[32:])
    cfg_interrupt_msi_pending_status = Signal(intbv(0)[32:])
    cfg_interrupt_msi_select = Signal(intbv(0)[2:])
    cfg_interrupt_msi_pending_status_function_num = Signal(intbv(0)[2:])
    cfg_interrupt_msi_pending_status_data_enable = Signal(bool(0))
    cfg_interrupt_msi_attr = Signal(intbv(0)[3:])
    cfg_interrupt_msi_tph_present = Signal(bool(0))
    cfg_interrupt_msi_tph_type = Signal(intbv(0)[2:])
    cfg_interrupt_msi_tph_st_tag = Signal(intbv(0)[8:])
    cfg_interrupt_msi_function_number = Signal(intbv(0)[8:])
    qsfp0_txd_1 = Signal(intbv(0)[64:])
    qsfp0_txc_1 = Signal(intbv(0)[8:])
    qsfp0_txd_2 = Signal(intbv(0)[64:])
    qsfp0_txc_2 = Signal(intbv(0)[8:])
    qsfp0_txd_3 = Signal(intbv(0)[64:])
    qsfp0_txc_3 = Signal(intbv(0)[8:])
    qsfp0_txd_4 = Signal(intbv(0)[64:])
    qsfp0_txc_4 = Signal(intbv(0)[8:])
    qsfp1_txd_1 = Signal(intbv(0)[64:])
    qsfp1_txc_1 = Signal(intbv(0)[8:])
    qsfp1_txd_2 = Signal(intbv(0)[64:])
    qsfp1_txc_2 = Signal(intbv(0)[8:])
    qsfp1_txd_3 = Signal(intbv(0)[64:])
    qsfp1_txc_3 = Signal(intbv(0)[8:])
    qsfp1_txd_4 = Signal(intbv(0)[64:])
    qsfp1_txc_4 = Signal(intbv(0)[8:])

    # sources and sinks
    qsfp0_1_source = xgmii_ep.XGMIISource()
    qsfp0_1_source_logic = qsfp0_1_source.create_logic(qsfp0_rx_clk_1,
                                                       qsfp0_rx_rst_1,
                                                       txd=qsfp0_rxd_1,
                                                       txc=qsfp0_rxc_1,
                                                       name='qsfp0_1_source')

    qsfp0_1_sink = xgmii_ep.XGMIISink()
    qsfp0_1_sink_logic = qsfp0_1_sink.create_logic(qsfp0_tx_clk_1,
                                                   qsfp0_tx_rst_1,
                                                   rxd=qsfp0_txd_1,
                                                   rxc=qsfp0_txc_1,
                                                   name='qsfp0_1_sink')

    qsfp0_2_source = xgmii_ep.XGMIISource()
    qsfp0_2_source_logic = qsfp0_2_source.create_logic(qsfp0_rx_clk_2,
                                                       qsfp0_rx_rst_2,
                                                       txd=qsfp0_rxd_2,
                                                       txc=qsfp0_rxc_2,
                                                       name='qsfp0_2_source')

    qsfp0_2_sink = xgmii_ep.XGMIISink()
    qsfp0_2_sink_logic = qsfp0_2_sink.create_logic(qsfp0_tx_clk_2,
                                                   qsfp0_tx_rst_2,
                                                   rxd=qsfp0_txd_2,
                                                   rxc=qsfp0_txc_2,
                                                   name='qsfp0_2_sink')

    qsfp0_3_source = xgmii_ep.XGMIISource()
    qsfp0_3_source_logic = qsfp0_3_source.create_logic(qsfp0_rx_clk_3,
                                                       qsfp0_rx_rst_3,
                                                       txd=qsfp0_rxd_3,
                                                       txc=qsfp0_rxc_3,
                                                       name='qsfp0_3_source')

    qsfp0_3_sink = xgmii_ep.XGMIISink()
    qsfp0_3_sink_logic = qsfp0_3_sink.create_logic(qsfp0_tx_clk_3,
                                                   qsfp0_tx_rst_3,
                                                   rxd=qsfp0_txd_3,
                                                   rxc=qsfp0_txc_3,
                                                   name='qsfp0_3_sink')

    qsfp0_4_source = xgmii_ep.XGMIISource()
    qsfp0_4_source_logic = qsfp0_4_source.create_logic(qsfp0_rx_clk_4,
                                                       qsfp0_rx_rst_4,
                                                       txd=qsfp0_rxd_4,
                                                       txc=qsfp0_rxc_4,
                                                       name='qsfp0_4_source')

    qsfp0_4_sink = xgmii_ep.XGMIISink()
    qsfp0_4_sink_logic = qsfp0_4_sink.create_logic(qsfp0_tx_clk_4,
                                                   qsfp0_tx_rst_4,
                                                   rxd=qsfp0_txd_4,
                                                   rxc=qsfp0_txc_4,
                                                   name='qsfp0_4_sink')

    qsfp1_1_source = xgmii_ep.XGMIISource()
    qsfp1_1_source_logic = qsfp1_1_source.create_logic(qsfp1_rx_clk_1,
                                                       qsfp1_rx_rst_1,
                                                       txd=qsfp1_rxd_1,
                                                       txc=qsfp1_rxc_1,
                                                       name='qsfp1_1_source')

    qsfp1_1_sink = xgmii_ep.XGMIISink()
    qsfp1_1_sink_logic = qsfp1_1_sink.create_logic(qsfp1_tx_clk_1,
                                                   qsfp1_tx_rst_1,
                                                   rxd=qsfp1_txd_1,
                                                   rxc=qsfp1_txc_1,
                                                   name='qsfp1_1_sink')

    qsfp1_2_source = xgmii_ep.XGMIISource()
    qsfp1_2_source_logic = qsfp1_2_source.create_logic(qsfp1_rx_clk_2,
                                                       qsfp1_rx_rst_2,
                                                       txd=qsfp1_rxd_2,
                                                       txc=qsfp1_rxc_2,
                                                       name='qsfp1_2_source')

    qsfp1_2_sink = xgmii_ep.XGMIISink()
    qsfp1_2_sink_logic = qsfp1_2_sink.create_logic(qsfp1_tx_clk_2,
                                                   qsfp1_tx_rst_2,
                                                   rxd=qsfp1_txd_2,
                                                   rxc=qsfp1_txc_2,
                                                   name='qsfp1_2_sink')

    qsfp1_3_source = xgmii_ep.XGMIISource()
    qsfp1_3_source_logic = qsfp1_3_source.create_logic(qsfp1_rx_clk_3,
                                                       qsfp1_rx_rst_3,
                                                       txd=qsfp1_rxd_3,
                                                       txc=qsfp1_rxc_3,
                                                       name='qsfp1_3_source')

    qsfp1_3_sink = xgmii_ep.XGMIISink()
    qsfp1_3_sink_logic = qsfp1_3_sink.create_logic(qsfp1_tx_clk_3,
                                                   qsfp1_tx_rst_3,
                                                   rxd=qsfp1_txd_3,
                                                   rxc=qsfp1_txc_3,
                                                   name='qsfp1_3_sink')

    qsfp1_4_source = xgmii_ep.XGMIISource()
    qsfp1_4_source_logic = qsfp1_4_source.create_logic(qsfp1_rx_clk_4,
                                                       qsfp1_rx_rst_4,
                                                       txd=qsfp1_rxd_4,
                                                       txc=qsfp1_rxc_4,
                                                       name='qsfp1_4_source')

    qsfp1_4_sink = xgmii_ep.XGMIISink()
    qsfp1_4_sink_logic = qsfp1_4_sink.create_logic(qsfp1_tx_clk_4,
                                                   qsfp1_tx_rst_4,
                                                   rxd=qsfp1_txd_4,
                                                   rxc=qsfp1_txc_4,
                                                   name='qsfp1_4_sink')

    # Clock and Reset Interface
    user_clk = Signal(bool(0))
    user_reset = Signal(bool(0))
    sys_clk = Signal(bool(0))
    sys_reset = Signal(bool(0))

    # PCIe devices
    rc = pcie.RootComplex()

    rc.max_payload_size = 0x1  # 256 bytes
    rc.max_read_request_size = 0x5  # 4096 bytes

    driver = mqnic.Driver(rc)

    dev = pcie_usp.UltrascalePlusPCIe()

    dev.pcie_generation = 3
    dev.pcie_link_width = 16
    dev.user_clock_frequency = 250e6

    dev.functions[0].msi_multiple_message_capable = 5

    dev.functions[0].configure_bar(0,
                                   2**BAR0_APERTURE,
                                   ext=True,
                                   prefetch=True)

    rc.make_port().connect(dev)

    cq_pause = Signal(bool(0))
    cc_pause = Signal(bool(0))
    rq_pause = Signal(bool(0))
    rc_pause = Signal(bool(0))

    pcie_logic = dev.create_logic(
        # Completer reQuest Interface
        m_axis_cq_tdata=s_axis_cq_tdata,
        m_axis_cq_tuser=s_axis_cq_tuser,
        m_axis_cq_tlast=s_axis_cq_tlast,
        m_axis_cq_tkeep=s_axis_cq_tkeep,
        m_axis_cq_tvalid=s_axis_cq_tvalid,
        m_axis_cq_tready=s_axis_cq_tready,
        #pcie_cq_np_req=pcie_cq_np_req,
        pcie_cq_np_req=Signal(intbv(3)[2:]),
        #pcie_cq_np_req_count=pcie_cq_np_req_count,

        # Completer Completion Interface
        s_axis_cc_tdata=m_axis_cc_tdata,
        s_axis_cc_tuser=m_axis_cc_tuser,
        s_axis_cc_tlast=m_axis_cc_tlast,
        s_axis_cc_tkeep=m_axis_cc_tkeep,
        s_axis_cc_tvalid=m_axis_cc_tvalid,
        s_axis_cc_tready=m_axis_cc_tready,

        # Requester reQuest Interface
        s_axis_rq_tdata=m_axis_rq_tdata,
        s_axis_rq_tuser=m_axis_rq_tuser,
        s_axis_rq_tlast=m_axis_rq_tlast,
        s_axis_rq_tkeep=m_axis_rq_tkeep,
        s_axis_rq_tvalid=m_axis_rq_tvalid,
        s_axis_rq_tready=m_axis_rq_tready,
        pcie_rq_seq_num0=s_axis_rq_seq_num_0,
        pcie_rq_seq_num_vld0=s_axis_rq_seq_num_valid_0,
        pcie_rq_seq_num1=s_axis_rq_seq_num_1,
        pcie_rq_seq_num_vld1=s_axis_rq_seq_num_valid_1,
        #pcie_rq_tag0=pcie_rq_tag0,
        #pcie_rq_tag1=pcie_rq_tag1,
        #pcie_rq_tag_av=pcie_rq_tag_av,
        #pcie_rq_tag_vld0=pcie_rq_tag_vld0,
        #pcie_rq_tag_vld1=pcie_rq_tag_vld1,

        # Requester Completion Interface
        m_axis_rc_tdata=s_axis_rc_tdata,
        m_axis_rc_tuser=s_axis_rc_tuser,
        m_axis_rc_tlast=s_axis_rc_tlast,
        m_axis_rc_tkeep=s_axis_rc_tkeep,
        m_axis_rc_tvalid=s_axis_rc_tvalid,
        m_axis_rc_tready=s_axis_rc_tready,

        # Transmit Flow Control Interface
        #pcie_tfc_nph_av=pcie_tfc_nph_av,
        #pcie_tfc_npd_av=pcie_tfc_npd_av,

        # Configuration Management Interface
        cfg_mgmt_addr=cfg_mgmt_addr,
        cfg_mgmt_function_number=cfg_mgmt_function_number,
        cfg_mgmt_write=cfg_mgmt_write,
        cfg_mgmt_write_data=cfg_mgmt_write_data,
        cfg_mgmt_byte_enable=cfg_mgmt_byte_enable,
        cfg_mgmt_read=cfg_mgmt_read,
        cfg_mgmt_read_data=cfg_mgmt_read_data,
        cfg_mgmt_read_write_done=cfg_mgmt_read_write_done,
        #cfg_mgmt_debug_access=cfg_mgmt_debug_access,

        # Configuration Status Interface
        #cfg_phy_link_down=cfg_phy_link_down,
        #cfg_phy_link_status=cfg_phy_link_status,
        #cfg_negotiated_width=cfg_negotiated_width,
        #cfg_current_speed=cfg_current_speed,
        cfg_max_payload=cfg_max_payload,
        cfg_max_read_req=cfg_max_read_req,
        #cfg_function_status=cfg_function_status,
        #cfg_vf_status=cfg_vf_status,
        #cfg_function_power_state=cfg_function_power_state,
        #cfg_vf_power_state=cfg_vf_power_state,
        #cfg_link_power_state=cfg_link_power_state,
        #cfg_err_cor_out=cfg_err_cor_out,
        #cfg_err_nonfatal_out=cfg_err_nonfatal_out,
        #cfg_err_fatal_out=cfg_err_fatal_out,
        #cfg_local_err_out=cfg_local_err_out,
        #cfg_local_err_valid=cfg_local_err_valid,
        #cfg_rx_pm_state=cfg_rx_pm_state,
        #cfg_tx_pm_state=cfg_tx_pm_state,
        #cfg_ltssm_state=cfg_ltssm_state,
        #cfg_rcb_status=cfg_rcb_status,
        #cfg_obff_enable=cfg_obff_enable,
        #cfg_pl_status_change=cfg_pl_status_change,
        #cfg_tph_requester_enable=cfg_tph_requester_enable,
        #cfg_tph_st_mode=cfg_tph_st_mode,
        #cfg_vf_tph_requester_enable=cfg_vf_tph_requester_enable,
        #cfg_vf_tph_st_mode=cfg_vf_tph_st_mode,

        # Configuration Received Message Interface
        #cfg_msg_received=cfg_msg_received,
        #cfg_msg_received_data=cfg_msg_received_data,
        #cfg_msg_received_type=cfg_msg_received_type,

        # Configuration Transmit Message Interface
        #cfg_msg_transmit=cfg_msg_transmit,
        #cfg_msg_transmit_type=cfg_msg_transmit_type,
        #cfg_msg_transmit_data=cfg_msg_transmit_data,
        #cfg_msg_transmit_done=cfg_msg_transmit_done,

        # Configuration Flow Control Interface
        cfg_fc_ph=cfg_fc_ph,
        cfg_fc_pd=cfg_fc_pd,
        cfg_fc_nph=cfg_fc_nph,
        cfg_fc_npd=cfg_fc_npd,
        cfg_fc_cplh=cfg_fc_cplh,
        cfg_fc_cpld=cfg_fc_cpld,
        cfg_fc_sel=cfg_fc_sel,

        # Configuration Control Interface
        #cfg_hot_reset_in=cfg_hot_reset_in,
        #cfg_hot_reset_out=cfg_hot_reset_out,
        #cfg_config_space_enable=cfg_config_space_enable,
        #cfg_dsn=cfg_dsn,
        #cfg_ds_port_number=cfg_ds_port_number,
        #cfg_ds_bus_number=cfg_ds_bus_number,
        #cfg_ds_device_number=cfg_ds_device_number,
        #cfg_ds_function_number=cfg_ds_function_number,
        #cfg_power_state_change_ack=cfg_power_state_change_ack,
        #cfg_power_state_change_interrupt=cfg_power_state_change_interrupt,
        cfg_err_cor_in=status_error_cor,
        cfg_err_uncor_in=status_error_uncor,
        #cfg_flr_done=cfg_flr_done,
        #cfg_vf_flr_done=cfg_vf_flr_done,
        #cfg_flr_in_process=cfg_flr_in_process,
        #cfg_vf_flr_in_process=cfg_vf_flr_in_process,
        #cfg_req_pm_transition_l23_ready=cfg_req_pm_transition_l23_ready,
        #cfg_link_training_enable=cfg_link_training_enable,

        # Configuration Interrupt Controller Interface
        #cfg_interrupt_int=cfg_interrupt_int,
        #cfg_interrupt_sent=cfg_interrupt_sent,
        #cfg_interrupt_pending=cfg_interrupt_pending,
        cfg_interrupt_msi_enable=cfg_interrupt_msi_enable,
        cfg_interrupt_msi_mmenable=cfg_interrupt_msi_mmenable,
        cfg_interrupt_msi_mask_update=cfg_interrupt_msi_mask_update,
        cfg_interrupt_msi_data=cfg_interrupt_msi_data,
        cfg_interrupt_msi_select=cfg_interrupt_msi_select,
        cfg_interrupt_msi_int=cfg_interrupt_msi_int,
        cfg_interrupt_msi_pending_status=cfg_interrupt_msi_pending_status,
        cfg_interrupt_msi_pending_status_data_enable=
        cfg_interrupt_msi_pending_status_data_enable,
        cfg_interrupt_msi_pending_status_function_num=
        cfg_interrupt_msi_pending_status_function_num,
        cfg_interrupt_msi_sent=cfg_interrupt_msi_sent,
        cfg_interrupt_msi_fail=cfg_interrupt_msi_fail,
        #cfg_interrupt_msix_enable=cfg_interrupt_msix_enable,
        #cfg_interrupt_msix_mask=cfg_interrupt_msix_mask,
        #cfg_interrupt_msix_vf_enable=cfg_interrupt_msix_vf_enable,
        #cfg_interrupt_msix_vf_mask=cfg_interrupt_msix_vf_mask,
        #cfg_interrupt_msix_address=cfg_interrupt_msix_address,
        #cfg_interrupt_msix_data=cfg_interrupt_msix_data,
        #cfg_interrupt_msix_int=cfg_interrupt_msix_int,
        #cfg_interrupt_msix_vec_pending=cfg_interrupt_msix_vec_pending,
        #cfg_interrupt_msix_vec_pending_status=cfg_interrupt_msix_vec_pending_status,
        cfg_interrupt_msi_attr=cfg_interrupt_msi_attr,
        cfg_interrupt_msi_tph_present=cfg_interrupt_msi_tph_present,
        cfg_interrupt_msi_tph_type=cfg_interrupt_msi_tph_type,
        cfg_interrupt_msi_tph_st_tag=cfg_interrupt_msi_tph_st_tag,
        cfg_interrupt_msi_function_number=cfg_interrupt_msi_function_number,

        # Configuration Extend Interface
        #cfg_ext_read_received=cfg_ext_read_received,
        #cfg_ext_write_received=cfg_ext_write_received,
        #cfg_ext_register_number=cfg_ext_register_number,
        #cfg_ext_function_number=cfg_ext_function_number,
        #cfg_ext_write_data=cfg_ext_write_data,
        #cfg_ext_write_byte_enable=cfg_ext_write_byte_enable,
        #cfg_ext_read_data=cfg_ext_read_data,
        #cfg_ext_read_data_valid=cfg_ext_read_data_valid,

        # Clock and Reset Interface
        user_clk=user_clk,
        user_reset=user_reset,
        sys_clk=sys_clk,
        sys_clk_gt=sys_clk,
        sys_reset=sys_reset,
        #phy_rdy_out=phy_rdy_out,
        cq_pause=cq_pause,
        cc_pause=cc_pause,
        rq_pause=rq_pause,
        rc_pause=rc_pause)

    # DUT
    if os.system(build_cmd):
        raise Exception("Error running build command")

    dut = Cosimulation(
        "vvp -m myhdl %s.vvp -lxt2" % testbench,
        clk=clk,
        rst=rst,
        current_test=current_test,
        clk_250mhz=user_clk,
        rst_250mhz=user_reset,
        m_axis_rq_tdata=m_axis_rq_tdata,
        m_axis_rq_tkeep=m_axis_rq_tkeep,
        m_axis_rq_tlast=m_axis_rq_tlast,
        m_axis_rq_tready=m_axis_rq_tready,
        m_axis_rq_tuser=m_axis_rq_tuser,
        m_axis_rq_tvalid=m_axis_rq_tvalid,
        s_axis_rc_tdata=s_axis_rc_tdata,
        s_axis_rc_tkeep=s_axis_rc_tkeep,
        s_axis_rc_tlast=s_axis_rc_tlast,
        s_axis_rc_tready=s_axis_rc_tready,
        s_axis_rc_tuser=s_axis_rc_tuser,
        s_axis_rc_tvalid=s_axis_rc_tvalid,
        s_axis_cq_tdata=s_axis_cq_tdata,
        s_axis_cq_tkeep=s_axis_cq_tkeep,
        s_axis_cq_tlast=s_axis_cq_tlast,
        s_axis_cq_tready=s_axis_cq_tready,
        s_axis_cq_tuser=s_axis_cq_tuser,
        s_axis_cq_tvalid=s_axis_cq_tvalid,
        m_axis_cc_tdata=m_axis_cc_tdata,
        m_axis_cc_tkeep=m_axis_cc_tkeep,
        m_axis_cc_tlast=m_axis_cc_tlast,
        m_axis_cc_tready=m_axis_cc_tready,
        m_axis_cc_tuser=m_axis_cc_tuser,
        m_axis_cc_tvalid=m_axis_cc_tvalid,
        s_axis_rq_seq_num_0=s_axis_rq_seq_num_0,
        s_axis_rq_seq_num_valid_0=s_axis_rq_seq_num_valid_0,
        s_axis_rq_seq_num_1=s_axis_rq_seq_num_1,
        s_axis_rq_seq_num_valid_1=s_axis_rq_seq_num_valid_1,
        pcie_tfc_nph_av=pcie_tfc_nph_av,
        pcie_tfc_npd_av=pcie_tfc_npd_av,
        cfg_max_payload=cfg_max_payload,
        cfg_max_read_req=cfg_max_read_req,
        cfg_mgmt_addr=cfg_mgmt_addr,
        cfg_mgmt_function_number=cfg_mgmt_function_number,
        cfg_mgmt_write=cfg_mgmt_write,
        cfg_mgmt_write_data=cfg_mgmt_write_data,
        cfg_mgmt_byte_enable=cfg_mgmt_byte_enable,
        cfg_mgmt_read=cfg_mgmt_read,
        cfg_mgmt_read_data=cfg_mgmt_read_data,
        cfg_mgmt_read_write_done=cfg_mgmt_read_write_done,
        cfg_fc_ph=cfg_fc_ph,
        cfg_fc_pd=cfg_fc_pd,
        cfg_fc_nph=cfg_fc_nph,
        cfg_fc_npd=cfg_fc_npd,
        cfg_fc_cplh=cfg_fc_cplh,
        cfg_fc_cpld=cfg_fc_cpld,
        cfg_fc_sel=cfg_fc_sel,
        cfg_interrupt_msi_enable=cfg_interrupt_msi_enable,
        cfg_interrupt_msi_int=cfg_interrupt_msi_int,
        cfg_interrupt_msi_sent=cfg_interrupt_msi_sent,
        cfg_interrupt_msi_fail=cfg_interrupt_msi_fail,
        cfg_interrupt_msi_mmenable=cfg_interrupt_msi_mmenable,
        cfg_interrupt_msi_pending_status=cfg_interrupt_msi_pending_status,
        cfg_interrupt_msi_mask_update=cfg_interrupt_msi_mask_update,
        cfg_interrupt_msi_select=cfg_interrupt_msi_select,
        cfg_interrupt_msi_data=cfg_interrupt_msi_data,
        cfg_interrupt_msi_pending_status_function_num=
        cfg_interrupt_msi_pending_status_function_num,
        cfg_interrupt_msi_pending_status_data_enable=
        cfg_interrupt_msi_pending_status_data_enable,
        cfg_interrupt_msi_attr=cfg_interrupt_msi_attr,
        cfg_interrupt_msi_tph_present=cfg_interrupt_msi_tph_present,
        cfg_interrupt_msi_tph_type=cfg_interrupt_msi_tph_type,
        cfg_interrupt_msi_tph_st_tag=cfg_interrupt_msi_tph_st_tag,
        cfg_interrupt_msi_function_number=cfg_interrupt_msi_function_number,
        status_error_cor=status_error_cor,
        status_error_uncor=status_error_uncor,
        qsfp0_tx_clk_1=qsfp0_tx_clk_1,
        qsfp0_tx_rst_1=qsfp0_tx_rst_1,
        qsfp0_txd_1=qsfp0_txd_1,
        qsfp0_txc_1=qsfp0_txc_1,
        qsfp0_rx_clk_1=qsfp0_rx_clk_1,
        qsfp0_rx_rst_1=qsfp0_rx_rst_1,
        qsfp0_rxd_1=qsfp0_rxd_1,
        qsfp0_rxc_1=qsfp0_rxc_1,
        qsfp0_tx_clk_2=qsfp0_tx_clk_2,
        qsfp0_tx_rst_2=qsfp0_tx_rst_2,
        qsfp0_txd_2=qsfp0_txd_2,
        qsfp0_txc_2=qsfp0_txc_2,
        qsfp0_rx_clk_2=qsfp0_rx_clk_2,
        qsfp0_rx_rst_2=qsfp0_rx_rst_2,
        qsfp0_rxd_2=qsfp0_rxd_2,
        qsfp0_rxc_2=qsfp0_rxc_2,
        qsfp0_tx_clk_3=qsfp0_tx_clk_3,
        qsfp0_tx_rst_3=qsfp0_tx_rst_3,
        qsfp0_txd_3=qsfp0_txd_3,
        qsfp0_txc_3=qsfp0_txc_3,
        qsfp0_rx_clk_3=qsfp0_rx_clk_3,
        qsfp0_rx_rst_3=qsfp0_rx_rst_3,
        qsfp0_rxd_3=qsfp0_rxd_3,
        qsfp0_rxc_3=qsfp0_rxc_3,
        qsfp0_tx_clk_4=qsfp0_tx_clk_4,
        qsfp0_tx_rst_4=qsfp0_tx_rst_4,
        qsfp0_txd_4=qsfp0_txd_4,
        qsfp0_txc_4=qsfp0_txc_4,
        qsfp0_rx_clk_4=qsfp0_rx_clk_4,
        qsfp0_rx_rst_4=qsfp0_rx_rst_4,
        qsfp0_rxd_4=qsfp0_rxd_4,
        qsfp0_rxc_4=qsfp0_rxc_4,
        qsfp1_tx_clk_1=qsfp1_tx_clk_1,
        qsfp1_tx_rst_1=qsfp1_tx_rst_1,
        qsfp1_txd_1=qsfp1_txd_1,
        qsfp1_txc_1=qsfp1_txc_1,
        qsfp1_rx_clk_1=qsfp1_rx_clk_1,
        qsfp1_rx_rst_1=qsfp1_rx_rst_1,
        qsfp1_rxd_1=qsfp1_rxd_1,
        qsfp1_rxc_1=qsfp1_rxc_1,
        qsfp1_tx_clk_2=qsfp1_tx_clk_2,
        qsfp1_tx_rst_2=qsfp1_tx_rst_2,
        qsfp1_txd_2=qsfp1_txd_2,
        qsfp1_txc_2=qsfp1_txc_2,
        qsfp1_rx_clk_2=qsfp1_rx_clk_2,
        qsfp1_rx_rst_2=qsfp1_rx_rst_2,
        qsfp1_rxd_2=qsfp1_rxd_2,
        qsfp1_rxc_2=qsfp1_rxc_2,
        qsfp1_tx_clk_3=qsfp1_tx_clk_3,
        qsfp1_tx_rst_3=qsfp1_tx_rst_3,
        qsfp1_txd_3=qsfp1_txd_3,
        qsfp1_txc_3=qsfp1_txc_3,
        qsfp1_rx_clk_3=qsfp1_rx_clk_3,
        qsfp1_rx_rst_3=qsfp1_rx_rst_3,
        qsfp1_rxd_3=qsfp1_rxd_3,
        qsfp1_rxc_3=qsfp1_rxc_3,
        qsfp1_tx_clk_4=qsfp1_tx_clk_4,
        qsfp1_tx_rst_4=qsfp1_tx_rst_4,
        qsfp1_txd_4=qsfp1_txd_4,
        qsfp1_txc_4=qsfp1_txc_4,
        qsfp1_rx_clk_4=qsfp1_rx_clk_4,
        qsfp1_rx_rst_4=qsfp1_rx_rst_4,
        qsfp1_rxd_4=qsfp1_rxd_4,
        qsfp1_rxc_4=qsfp1_rxc_4)

    @always(delay(5))
    def clkgen():
        clk.next = not clk

    @always(delay(3))
    def qsfp_clkgen():
        qsfp0_tx_clk_1.next = not qsfp0_tx_clk_1
        qsfp0_rx_clk_1.next = not qsfp0_rx_clk_1
        qsfp0_tx_clk_2.next = not qsfp0_tx_clk_2
        qsfp0_rx_clk_2.next = not qsfp0_rx_clk_2
        qsfp0_tx_clk_3.next = not qsfp0_tx_clk_3
        qsfp0_rx_clk_3.next = not qsfp0_rx_clk_3
        qsfp0_tx_clk_4.next = not qsfp0_tx_clk_4
        qsfp0_rx_clk_4.next = not qsfp0_rx_clk_4
        qsfp1_tx_clk_1.next = not qsfp1_tx_clk_1
        qsfp1_rx_clk_1.next = not qsfp1_rx_clk_1
        qsfp1_tx_clk_2.next = not qsfp1_tx_clk_2
        qsfp1_rx_clk_2.next = not qsfp1_rx_clk_2
        qsfp1_tx_clk_3.next = not qsfp1_tx_clk_3
        qsfp1_rx_clk_3.next = not qsfp1_rx_clk_3
        qsfp1_tx_clk_4.next = not qsfp1_tx_clk_4
        qsfp1_rx_clk_4.next = not qsfp1_rx_clk_4

    @always_comb
    def clk_logic():
        sys_clk.next = clk
        sys_reset.next = not rst

    loopback_enable = Signal(bool(0))

    @instance
    def loopback():
        while True:

            yield clk.posedge

            if loopback_enable:
                if not qsfp0_1_sink.empty():
                    pkt = qsfp0_1_sink.recv()
                    qsfp0_1_source.send(pkt)
                if not qsfp0_2_sink.empty():
                    pkt = qsfp0_2_sink.recv()
                    qsfp0_2_source.send(pkt)
                if not qsfp0_3_sink.empty():
                    pkt = qsfp0_3_sink.recv()
                    qsfp0_3_source.send(pkt)
                if not qsfp0_4_sink.empty():
                    pkt = qsfp0_4_sink.recv()
                    qsfp0_4_source.send(pkt)
                if not qsfp1_1_sink.empty():
                    pkt = qsfp1_1_sink.recv()
                    qsfp1_1_source.send(pkt)
                if not qsfp1_2_sink.empty():
                    pkt = qsfp1_2_sink.recv()
                    qsfp1_2_source.send(pkt)
                if not qsfp1_3_sink.empty():
                    pkt = qsfp1_3_sink.recv()
                    qsfp1_3_source.send(pkt)
                if not qsfp1_4_sink.empty():
                    pkt = qsfp1_4_sink.recv()
                    qsfp1_4_source.send(pkt)

    @instance
    def check():
        yield delay(100)
        yield clk.posedge
        rst.next = 1
        qsfp0_tx_rst_1.next = 1
        qsfp0_rx_rst_1.next = 1
        qsfp0_tx_rst_2.next = 1
        qsfp0_rx_rst_2.next = 1
        qsfp0_tx_rst_3.next = 1
        qsfp0_rx_rst_3.next = 1
        qsfp0_tx_rst_4.next = 1
        qsfp0_rx_rst_4.next = 1
        qsfp1_tx_rst_1.next = 1
        qsfp1_rx_rst_1.next = 1
        qsfp1_tx_rst_2.next = 1
        qsfp1_rx_rst_2.next = 1
        qsfp1_tx_rst_3.next = 1
        qsfp1_rx_rst_3.next = 1
        qsfp1_tx_rst_4.next = 1
        qsfp1_rx_rst_4.next = 1
        yield clk.posedge
        yield delay(100)
        rst.next = 0
        qsfp0_tx_rst_1.next = 0
        qsfp0_rx_rst_1.next = 0
        qsfp0_tx_rst_2.next = 0
        qsfp0_rx_rst_2.next = 0
        qsfp0_tx_rst_3.next = 0
        qsfp0_rx_rst_3.next = 0
        qsfp0_tx_rst_4.next = 0
        qsfp0_rx_rst_4.next = 0
        qsfp1_tx_rst_1.next = 0
        qsfp1_rx_rst_1.next = 0
        qsfp1_tx_rst_2.next = 0
        qsfp1_rx_rst_2.next = 0
        qsfp1_tx_rst_3.next = 0
        qsfp1_rx_rst_3.next = 0
        qsfp1_tx_rst_4.next = 0
        qsfp1_rx_rst_4.next = 0
        yield clk.posedge
        yield delay(100)
        yield clk.posedge

        # testbench stimulus

        current_tag = 1

        yield clk.posedge
        print("test 1: enumeration")
        current_test.next = 1

        yield rc.enumerate(enable_bus_mastering=True, configure_msi=True)

        yield delay(100)

        yield clk.posedge
        print("test 2: init NIC")
        current_test.next = 2

        yield from driver.init_dev(dev.functions[0].get_id())
        yield from driver.interfaces[0].open()
        #yield from driver.interfaces[1].open()

        # enable queues
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].hw_addr +
            mqnic.MQNIC_PORT_REG_SCHED_ENABLE, 0x00000001)
        for k in range(driver.interfaces[0].tx_queue_count):
            yield from rc.mem_write_dword(
                driver.interfaces[0].ports[0].schedulers[0].hw_addr + 4 * k,
                0x00000003)

        yield from rc.mem_read(driver.hw_addr,
                               4)  # wait for all writes to complete

        yield delay(100)

        yield clk.posedge
        print("test 3: send and receive a packet")
        current_test.next = 3

        # test bad packet
        #qsfp0_1_source.send(b'\x55\x55\x55\x55\x55\xd5'+bytearray(range(128)))

        data = bytearray([x % 256 for x in range(1024)])

        yield from driver.interfaces[0].start_xmit(data, 0)

        yield qsfp0_1_sink.wait()

        pkt = qsfp0_1_sink.recv()
        print(pkt)

        qsfp0_1_source.send(pkt)

        yield driver.interfaces[0].wait()

        pkt = driver.interfaces[0].recv()

        print(pkt)
        assert frame_checksum(pkt.data) == pkt.rx_checksum

        # yield from driver.interfaces[1].start_xmit(data, 0)

        # yield qsfp0_1_sink.wait()

        # pkt = qsfp0_1_sink.recv()
        # print(pkt)

        # qsfp0_1_source.send(pkt)

        # yield driver.interfaces[1].wait()

        # pkt = driver.interfaces[1].recv()

        # print(pkt)
        # assert frame_checksum(pkt.data) == pkt.rx_checksum

        yield delay(100)

        yield clk.posedge
        print("test 4: checksum tests")
        current_test.next = 4

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
        test_frame.eth_src_mac = 0x5A5152535455
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_length = None
        test_frame.ip_identification = 0
        test_frame.ip_flags = 2
        test_frame.ip_fragment_offset = 0
        test_frame.ip_ttl = 64
        test_frame.ip_protocol = 0x11
        test_frame.ip_header_checksum = None
        test_frame.ip_source_ip = 0xc0a80164
        test_frame.ip_dest_ip = 0xc0a80165
        test_frame.udp_source_port = 1
        test_frame.udp_dest_port = 2
        test_frame.udp_length = None
        test_frame.udp_checksum = None
        test_frame.payload = bytearray((x % 256 for x in range(256)))

        test_frame.set_udp_pseudo_header_checksum()

        axis_frame = test_frame.build_axis()

        yield from driver.interfaces[0].start_xmit(axis_frame.data, 0, 34, 6)

        yield qsfp0_1_sink.wait()

        pkt = qsfp0_1_sink.recv()
        print(pkt)

        qsfp0_1_source.send(pkt)

        yield driver.interfaces[0].wait()

        pkt = driver.interfaces[0].recv()

        print(pkt)

        assert pkt.rx_checksum == frame_checksum(pkt.data)

        check_frame = udp_ep.UDPFrame()
        check_frame.parse_axis(pkt.data)

        assert check_frame.verify_checksums()

        yield delay(100)

        yield clk.posedge
        print("test 5: multiple small packets")
        current_test.next = 5

        count = 64

        pkts = [
            bytearray([(x + k) % 256 for x in range(64)]) for k in range(count)
        ]

        loopback_enable.next = True

        for p in pkts:
            yield from driver.interfaces[0].start_xmit(p, 0)

        for k in range(count):
            pkt = driver.interfaces[0].recv()

            if not pkt:
                yield driver.interfaces[0].wait()
                pkt = driver.interfaces[0].recv()

            print(pkt)
            assert pkt.data == pkts[k]
            assert frame_checksum(pkt.data) == pkt.rx_checksum

        loopback_enable.next = False

        yield delay(100)

        yield clk.posedge
        print("test 6: multiple large packets")
        current_test.next = 6

        count = 64

        pkts = [
            bytearray([(x + k) % 256 for x in range(1514)])
            for k in range(count)
        ]

        loopback_enable.next = True

        for p in pkts:
            yield from driver.interfaces[0].start_xmit(p, 0)

        for k in range(count):
            pkt = driver.interfaces[0].recv()

            if not pkt:
                yield driver.interfaces[0].wait()
                pkt = driver.interfaces[0].recv()

            print(pkt)
            assert pkt.data == pkts[k]
            assert frame_checksum(pkt.data) == pkt.rx_checksum

        loopback_enable.next = False

        yield delay(100)

        raise StopSimulation

    return instances()
Esempio n. 3
0
def bench():

    # Parameters
    dw = 128

    # Inputs
    clk = Signal(bool(0))
    rst = Signal(bool(0))
    current_test = Signal(intbv(0)[8:])

    # Outputs

    # Completer reQuest Interface
    m_axis_cq_tdata = Signal(intbv(0)[dw:])
    m_axis_cq_tuser = Signal(intbv(0)[88:])
    m_axis_cq_tlast = Signal(bool(0))
    m_axis_cq_tkeep = Signal(intbv(0)[int(dw / 32):])
    m_axis_cq_tvalid = Signal(bool(0))
    m_axis_cq_tready = Signal(bool(0))
    pcie_cq_np_req = Signal(intbv(3)[2:])
    pcie_cq_np_req_count = Signal(intbv(0)[6:])

    # Completer Completion Interface
    s_axis_cc_tdata = Signal(intbv(0)[dw:])
    s_axis_cc_tuser = Signal(intbv(0)[33:])
    s_axis_cc_tlast = Signal(bool(0))
    s_axis_cc_tkeep = Signal(intbv(0)[int(dw / 32):])
    s_axis_cc_tvalid = Signal(bool(0))
    s_axis_cc_tready = Signal(bool(0))

    # Requester reQuest Interface
    s_axis_rq_tdata = Signal(intbv(0)[dw:])
    s_axis_rq_tuser = Signal(intbv(0)[62:])
    s_axis_rq_tlast = Signal(bool(0))
    s_axis_rq_tkeep = Signal(intbv(0)[int(dw / 32):])
    s_axis_rq_tvalid = Signal(bool(0))
    s_axis_rq_tready = Signal(bool(0))
    pcie_rq_seq_num0 = Signal(intbv(0)[6:])
    pcie_rq_seq_num_vld0 = Signal(bool(0))
    pcie_rq_seq_num1 = Signal(intbv(0)[6:])
    pcie_rq_seq_num_vld1 = Signal(bool(0))
    pcie_rq_tag0 = Signal(intbv(0)[8:])
    pcie_rq_tag1 = Signal(intbv(0)[8:])
    pcie_rq_tag_av = Signal(intbv(0)[4:])
    pcie_rq_tag_vld0 = Signal(bool(0))
    pcie_rq_tag_vld1 = Signal(bool(0))

    # Requester Completion Interface
    m_axis_rc_tdata = Signal(intbv(0)[dw:])
    m_axis_rc_tuser = Signal(intbv(0)[75:])
    m_axis_rc_tlast = Signal(bool(0))
    m_axis_rc_tkeep = Signal(intbv(0)[int(dw / 32):])
    m_axis_rc_tvalid = Signal(bool(0))
    m_axis_rc_tready = Signal(bool(0))

    # Transmit Flow Control Interface
    pcie_tfc_nph_av = Signal(intbv(0)[4:])
    pcie_tfc_npd_av = Signal(intbv(0)[4:])

    # Configuration Management Interface
    cfg_mgmt_addr = Signal(intbv(0)[10:])
    cfg_mgmt_function_number = Signal(intbv(0)[8:])
    cfg_mgmt_write = Signal(bool(0))
    cfg_mgmt_write_data = Signal(intbv(0)[32:])
    cfg_mgmt_byte_enable = Signal(intbv(0)[4:])
    cfg_mgmt_read = Signal(bool(0))
    cfg_mgmt_read_data = Signal(intbv(0)[32:])
    cfg_mgmt_read_write_done = Signal(bool(0))
    cfg_mgmt_debug_access = Signal(bool(0))

    # Configuration Status Interface
    cfg_phy_link_down = Signal(bool(0))
    cfg_phy_link_status = Signal(intbv(0)[2:])
    cfg_negotiated_width = Signal(intbv(0)[3:])
    cfg_current_speed = Signal(intbv(0)[2:])
    cfg_max_payload = Signal(intbv(0)[2:])
    cfg_max_read_req = Signal(intbv(0)[3:])
    cfg_function_status = Signal(intbv(0)[16:])
    cfg_vf_status = Signal(intbv(0)[504:])
    cfg_function_power_state = Signal(intbv(0)[12:])
    cfg_vf_power_state = Signal(intbv(0)[756:])
    cfg_link_power_state = Signal(intbv(0)[2:])
    cfg_err_cor_out = Signal(bool(0))
    cfg_err_nonfatal_out = Signal(bool(0))
    cfg_err_fatal_out = Signal(bool(0))
    cfg_local_err_out = Signal(intbv(0)[5:])
    cfg_local_err_valid = Signal(bool(0))
    cfg_rx_pm_state = Signal(intbv(0)[2:])
    cfg_tx_pm_state = Signal(intbv(0)[2:])
    cfg_ltssm_state = Signal(intbv(0)[6:])
    cfg_rcb_status = Signal(intbv(0)[4:])
    cfg_obff_enable = Signal(intbv(0)[2:])
    cfg_pl_status_change = Signal(bool(0))
    cfg_tph_requester_enable = Signal(intbv(0)[4:])
    cfg_tph_st_mode = Signal(intbv(0)[12:])
    cfg_vf_tph_requester_enable = Signal(intbv(0)[252:])
    cfg_vf_tph_st_mode = Signal(intbv(0)[756:])

    # Configuration Received Message Interface
    cfg_msg_received = Signal(bool(0))
    cfg_msg_received_data = Signal(intbv(0)[8:])
    cfg_msg_received_type = Signal(intbv(0)[5:])

    # Configuration Transmit Message Interface
    cfg_msg_transmit = Signal(bool(0))
    cfg_msg_transmit_type = Signal(intbv(0)[3:])
    cfg_msg_transmit_data = Signal(intbv(0)[32:])
    cfg_msg_transmit_done = Signal(bool(0))

    # Configuration Flow Control Interface
    cfg_fc_ph = Signal(intbv(0)[8:])
    cfg_fc_pd = Signal(intbv(0)[12:])
    cfg_fc_nph = Signal(intbv(0)[8:])
    cfg_fc_npd = Signal(intbv(0)[12:])
    cfg_fc_cplh = Signal(intbv(0)[8:])
    cfg_fc_cpld = Signal(intbv(0)[12:])
    cfg_fc_sel = Signal(intbv(0)[3:])

    # Configuration Control Interface
    cfg_hot_reset_in = Signal(bool(0))
    cfg_hot_reset_out = Signal(bool(0))
    cfg_config_space_enable = Signal(bool(1))
    cfg_dsn = Signal(intbv(0)[64:])
    cfg_ds_port_number = Signal(intbv(0)[8:])
    cfg_ds_bus_number = Signal(intbv(0)[8:])
    cfg_ds_device_number = Signal(intbv(0)[5:])
    cfg_ds_function_number = Signal(intbv(0)[3:])
    cfg_power_state_change_ack = Signal(bool(0))
    cfg_power_state_change_interrupt = Signal(bool(0))
    cfg_err_cor_in = Signal(bool(0))
    cfg_err_uncor_in = Signal(bool(0))
    cfg_flr_done = Signal(intbv(0)[4:])
    cfg_vf_flr_done = Signal(intbv(0)[1:])
    cfg_flr_in_process = Signal(intbv(0)[4:])
    cfg_vf_flr_in_process = Signal(intbv(0)[252:])
    cfg_req_pm_transition_l23_ready = Signal(bool(0))
    cfg_link_training_enable = Signal(bool(1))

    # Configuration Interrupt Controller Interface
    cfg_interrupt_int = Signal(intbv(0)[4:])
    cfg_interrupt_sent = Signal(bool(0))
    cfg_interrupt_pending = Signal(intbv(0)[2:])
    cfg_interrupt_msi_enable = Signal(intbv(0)[4:])
    cfg_interrupt_msi_mmenable = Signal(intbv(0)[12:])
    cfg_interrupt_msi_mask_update = Signal(bool(0))
    cfg_interrupt_msi_data = Signal(intbv(0)[32:])
    cfg_interrupt_msi_select = Signal(intbv(0)[2:])
    cfg_interrupt_msi_int = Signal(intbv(0)[32:])
    cfg_interrupt_msi_pending_status = Signal(intbv(0)[32:])
    cfg_interrupt_msi_pending_status_data_enable = Signal(bool(0))
    cfg_interrupt_msi_pending_status_function_num = Signal(intbv(0)[2:])
    cfg_interrupt_msi_sent = Signal(bool(0))
    cfg_interrupt_msi_fail = Signal(bool(0))
    cfg_interrupt_msix_enable = Signal(intbv(0)[4:])
    cfg_interrupt_msix_mask = Signal(intbv(0)[4:])
    cfg_interrupt_msix_vf_enable = Signal(intbv(0)[252:])
    cfg_interrupt_msix_vf_mask = Signal(intbv(0)[252:])
    cfg_interrupt_msix_address = Signal(intbv(0)[64:])
    cfg_interrupt_msix_data = Signal(intbv(0)[32:])
    cfg_interrupt_msix_int = Signal(bool(0))
    cfg_interrupt_msix_vec_pending = Signal(intbv(0)[2:])
    cfg_interrupt_msix_vec_pending_status = Signal(bool(0))
    cfg_interrupt_msi_attr = Signal(intbv(0)[3:])
    cfg_interrupt_msi_tph_present = Signal(bool(0))
    cfg_interrupt_msi_tph_type = Signal(intbv(0)[2:])
    cfg_interrupt_msi_tph_st_tag = Signal(intbv(0)[8:])
    cfg_interrupt_msi_function_number = Signal(intbv(0)[8:])

    # Configuration Extend Interface
    cfg_ext_read_received = Signal(bool(0))
    cfg_ext_write_received = Signal(bool(0))
    cfg_ext_register_number = Signal(intbv(0)[10:])
    cfg_ext_function_number = Signal(intbv(0)[8:])
    cfg_ext_write_data = Signal(intbv(0)[32:])
    cfg_ext_write_byte_enable = Signal(intbv(0)[4:])
    cfg_ext_read_data = Signal(intbv(0)[32:])
    cfg_ext_read_data_valid = Signal(bool(0))

    # Clock and Reset Interface
    user_clk = Signal(bool(0))
    user_reset = Signal(bool(0))
    user_lnk_up = Signal(bool(0))
    sys_clk = Signal(bool(0))
    sys_reset = Signal(bool(0))
    phy_rdy_out = Signal(bool(0))

    # sources and sinks
    cq_sink = pcie_usp.CQSink()

    cq_sink_logic = cq_sink.create_logic(user_clk,
                                         user_reset,
                                         tdata=m_axis_cq_tdata,
                                         tuser=m_axis_cq_tuser,
                                         tlast=m_axis_cq_tlast,
                                         tkeep=m_axis_cq_tkeep,
                                         tvalid=m_axis_cq_tvalid,
                                         tready=m_axis_cq_tready,
                                         name='cq_sink')

    cc_source = pcie_usp.CCSource()

    cc_source_logic = cc_source.create_logic(user_clk,
                                             user_reset,
                                             tdata=s_axis_cc_tdata,
                                             tuser=s_axis_cc_tuser,
                                             tlast=s_axis_cc_tlast,
                                             tkeep=s_axis_cc_tkeep,
                                             tvalid=s_axis_cc_tvalid,
                                             tready=s_axis_cc_tready,
                                             name='cc_source')

    rq_source = pcie_usp.RQSource()

    rq_source_logic = rq_source.create_logic(user_clk,
                                             user_reset,
                                             tdata=s_axis_rq_tdata,
                                             tuser=s_axis_rq_tuser,
                                             tlast=s_axis_rq_tlast,
                                             tkeep=s_axis_rq_tkeep,
                                             tvalid=s_axis_rq_tvalid,
                                             tready=s_axis_rq_tready,
                                             name='rq_source')

    rc_sink = pcie_usp.RCSink()

    rc_sink_logic = rc_sink.create_logic(user_clk,
                                         user_reset,
                                         tdata=m_axis_rc_tdata,
                                         tuser=m_axis_rc_tuser,
                                         tlast=m_axis_rc_tlast,
                                         tkeep=m_axis_rc_tkeep,
                                         tvalid=m_axis_rc_tvalid,
                                         tready=m_axis_rc_tready,
                                         name='rc_sink')

    # PCIe devices
    rc = pcie.RootComplex()

    mem_base, mem_data = rc.alloc_region(1024 * 1024)
    io_base, io_data = rc.alloc_io_region(1024)

    dev = pcie_usp.UltrascalePlusPCIe()

    dev.pcie_generation = 3
    dev.pcie_link_width = 4
    dev.user_clock_frequency = 256e6

    regions = [None] * 6
    regions[0] = bytearray(1024)
    regions[1] = bytearray(1024 * 1024)
    regions[3] = bytearray(1024)

    dev.functions[0].msi_multiple_message_capable = 5

    dev.functions[0].configure_bar(0, len(regions[0]))
    dev.functions[0].configure_bar(1, len(regions[1]), True, True)
    dev.functions[0].configure_bar(3, len(regions[3]), False, False, True)

    rc.make_port().connect(dev)

    pcie_logic = dev.create_logic(
        # Completer reQuest Interface
        m_axis_cq_tdata=m_axis_cq_tdata,
        m_axis_cq_tuser=m_axis_cq_tuser,
        m_axis_cq_tlast=m_axis_cq_tlast,
        m_axis_cq_tkeep=m_axis_cq_tkeep,
        m_axis_cq_tvalid=m_axis_cq_tvalid,
        m_axis_cq_tready=m_axis_cq_tready,
        pcie_cq_np_req=pcie_cq_np_req,
        pcie_cq_np_req_count=pcie_cq_np_req_count,

        # Completer Completion Interface
        s_axis_cc_tdata=s_axis_cc_tdata,
        s_axis_cc_tuser=s_axis_cc_tuser,
        s_axis_cc_tlast=s_axis_cc_tlast,
        s_axis_cc_tkeep=s_axis_cc_tkeep,
        s_axis_cc_tvalid=s_axis_cc_tvalid,
        s_axis_cc_tready=s_axis_cc_tready,

        # Requester reQuest Interface
        s_axis_rq_tdata=s_axis_rq_tdata,
        s_axis_rq_tuser=s_axis_rq_tuser,
        s_axis_rq_tlast=s_axis_rq_tlast,
        s_axis_rq_tkeep=s_axis_rq_tkeep,
        s_axis_rq_tvalid=s_axis_rq_tvalid,
        s_axis_rq_tready=s_axis_rq_tready,
        pcie_rq_seq_num0=pcie_rq_seq_num0,
        pcie_rq_seq_num_vld0=pcie_rq_seq_num_vld0,
        pcie_rq_seq_num1=pcie_rq_seq_num1,
        pcie_rq_seq_num_vld1=pcie_rq_seq_num_vld1,
        pcie_rq_tag0=pcie_rq_tag0,
        pcie_rq_tag1=pcie_rq_tag1,
        pcie_rq_tag_av=pcie_rq_tag_av,
        pcie_rq_tag_vld0=pcie_rq_tag_vld0,
        pcie_rq_tag_vld1=pcie_rq_tag_vld1,

        # Requester Completion Interface
        m_axis_rc_tdata=m_axis_rc_tdata,
        m_axis_rc_tuser=m_axis_rc_tuser,
        m_axis_rc_tlast=m_axis_rc_tlast,
        m_axis_rc_tkeep=m_axis_rc_tkeep,
        m_axis_rc_tvalid=m_axis_rc_tvalid,
        m_axis_rc_tready=m_axis_rc_tready,

        # Transmit Flow Control Interface
        pcie_tfc_nph_av=pcie_tfc_nph_av,
        pcie_tfc_npd_av=pcie_tfc_npd_av,

        # Configuration Management Interface
        cfg_mgmt_addr=cfg_mgmt_addr,
        cfg_mgmt_function_number=cfg_mgmt_function_number,
        cfg_mgmt_write=cfg_mgmt_write,
        cfg_mgmt_write_data=cfg_mgmt_write_data,
        cfg_mgmt_byte_enable=cfg_mgmt_byte_enable,
        cfg_mgmt_read=cfg_mgmt_read,
        cfg_mgmt_read_data=cfg_mgmt_read_data,
        cfg_mgmt_read_write_done=cfg_mgmt_read_write_done,
        cfg_mgmt_debug_access=cfg_mgmt_debug_access,

        # Configuration Status Interface
        cfg_phy_link_down=cfg_phy_link_down,
        cfg_phy_link_status=cfg_phy_link_status,
        cfg_negotiated_width=cfg_negotiated_width,
        cfg_current_speed=cfg_current_speed,
        cfg_max_payload=cfg_max_payload,
        cfg_max_read_req=cfg_max_read_req,
        cfg_function_status=cfg_function_status,
        cfg_vf_status=cfg_vf_status,
        cfg_function_power_state=cfg_function_power_state,
        cfg_vf_power_state=cfg_vf_power_state,
        cfg_link_power_state=cfg_link_power_state,
        cfg_err_cor_out=cfg_err_cor_out,
        cfg_err_nonfatal_out=cfg_err_nonfatal_out,
        cfg_err_fatal_out=cfg_err_fatal_out,
        cfg_local_err_out=cfg_local_err_out,
        cfg_local_err_valid=cfg_local_err_valid,
        cfg_rx_pm_state=cfg_rx_pm_state,
        cfg_tx_pm_state=cfg_tx_pm_state,
        cfg_ltssm_state=cfg_ltssm_state,
        cfg_rcb_status=cfg_rcb_status,
        cfg_obff_enable=cfg_obff_enable,
        cfg_pl_status_change=cfg_pl_status_change,
        cfg_tph_requester_enable=cfg_tph_requester_enable,
        cfg_tph_st_mode=cfg_tph_st_mode,
        cfg_vf_tph_requester_enable=cfg_vf_tph_requester_enable,
        cfg_vf_tph_st_mode=cfg_vf_tph_st_mode,

        # Configuration Received Message Interface
        cfg_msg_received=cfg_msg_received,
        cfg_msg_received_data=cfg_msg_received_data,
        cfg_msg_received_type=cfg_msg_received_type,

        # Configuration Transmit Message Interface
        cfg_msg_transmit=cfg_msg_transmit,
        cfg_msg_transmit_type=cfg_msg_transmit_type,
        cfg_msg_transmit_data=cfg_msg_transmit_data,
        cfg_msg_transmit_done=cfg_msg_transmit_done,

        # Configuration Flow Control Interface
        cfg_fc_ph=cfg_fc_ph,
        cfg_fc_pd=cfg_fc_pd,
        cfg_fc_nph=cfg_fc_nph,
        cfg_fc_npd=cfg_fc_npd,
        cfg_fc_cplh=cfg_fc_cplh,
        cfg_fc_cpld=cfg_fc_cpld,
        cfg_fc_sel=cfg_fc_sel,

        # Configuration Control Interface
        cfg_hot_reset_in=cfg_hot_reset_in,
        cfg_hot_reset_out=cfg_hot_reset_out,
        cfg_config_space_enable=cfg_config_space_enable,
        cfg_dsn=cfg_dsn,
        cfg_ds_port_number=cfg_ds_port_number,
        cfg_ds_bus_number=cfg_ds_bus_number,
        cfg_ds_device_number=cfg_ds_device_number,
        cfg_ds_function_number=cfg_ds_function_number,
        cfg_power_state_change_ack=cfg_power_state_change_ack,
        cfg_power_state_change_interrupt=cfg_power_state_change_interrupt,
        cfg_err_cor_in=cfg_err_cor_in,
        cfg_err_uncor_in=cfg_err_uncor_in,
        cfg_flr_done=cfg_flr_done,
        cfg_vf_flr_done=cfg_vf_flr_done,
        cfg_flr_in_process=cfg_flr_in_process,
        cfg_vf_flr_in_process=cfg_vf_flr_in_process,
        cfg_req_pm_transition_l23_ready=cfg_req_pm_transition_l23_ready,
        cfg_link_training_enable=cfg_link_training_enable,

        # Configuration Interrupt Controller Interface
        cfg_interrupt_int=cfg_interrupt_int,
        cfg_interrupt_sent=cfg_interrupt_sent,
        cfg_interrupt_pending=cfg_interrupt_pending,
        cfg_interrupt_msi_enable=cfg_interrupt_msi_enable,
        cfg_interrupt_msi_mmenable=cfg_interrupt_msi_mmenable,
        cfg_interrupt_msi_mask_update=cfg_interrupt_msi_mask_update,
        cfg_interrupt_msi_data=cfg_interrupt_msi_data,
        cfg_interrupt_msi_select=cfg_interrupt_msi_select,
        cfg_interrupt_msi_int=cfg_interrupt_msi_int,
        cfg_interrupt_msi_pending_status=cfg_interrupt_msi_pending_status,
        cfg_interrupt_msi_pending_status_data_enable=
        cfg_interrupt_msi_pending_status_data_enable,
        cfg_interrupt_msi_pending_status_function_num=
        cfg_interrupt_msi_pending_status_function_num,
        cfg_interrupt_msi_sent=cfg_interrupt_msi_sent,
        cfg_interrupt_msi_fail=cfg_interrupt_msi_fail,
        cfg_interrupt_msix_enable=cfg_interrupt_msix_enable,
        cfg_interrupt_msix_mask=cfg_interrupt_msix_mask,
        cfg_interrupt_msix_vf_enable=cfg_interrupt_msix_vf_enable,
        cfg_interrupt_msix_vf_mask=cfg_interrupt_msix_vf_mask,
        cfg_interrupt_msix_address=cfg_interrupt_msix_address,
        cfg_interrupt_msix_data=cfg_interrupt_msix_data,
        cfg_interrupt_msix_int=cfg_interrupt_msix_int,
        cfg_interrupt_msix_vec_pending=cfg_interrupt_msix_vec_pending,
        cfg_interrupt_msix_vec_pending_status=
        cfg_interrupt_msix_vec_pending_status,
        cfg_interrupt_msi_attr=cfg_interrupt_msi_attr,
        cfg_interrupt_msi_tph_present=cfg_interrupt_msi_tph_present,
        cfg_interrupt_msi_tph_type=cfg_interrupt_msi_tph_type,
        cfg_interrupt_msi_tph_st_tag=cfg_interrupt_msi_tph_st_tag,
        cfg_interrupt_msi_function_number=cfg_interrupt_msi_function_number,

        # Configuration Extend Interface
        cfg_ext_read_received=cfg_ext_read_received,
        cfg_ext_write_received=cfg_ext_write_received,
        cfg_ext_register_number=cfg_ext_register_number,
        cfg_ext_function_number=cfg_ext_function_number,
        cfg_ext_write_data=cfg_ext_write_data,
        cfg_ext_write_byte_enable=cfg_ext_write_byte_enable,
        cfg_ext_read_data=cfg_ext_read_data,
        cfg_ext_read_data_valid=cfg_ext_read_data_valid,

        # Clock and Reset Interface
        user_clk=user_clk,
        user_reset=user_reset,
        user_lnk_up=user_lnk_up,
        sys_clk=sys_clk,
        sys_clk_gt=sys_clk,
        sys_reset=sys_reset,
        phy_rdy_out=phy_rdy_out)

    @always(delay(5))
    def clkgen():
        clk.next = not clk

    @always_comb
    def clk_logic():
        sys_clk.next = clk
        sys_reset.next = not rst

    @instance
    def user_logic():
        while True:
            yield clk.posedge

            # handle completer request
            if not cq_sink.empty():
                pkt = cq_sink.recv()

                tlp = pcie_usp.TLP_us().unpack_us_cq(pkt)

                print(tlp)

                if (tlp.fmt_type == pcie.TLP_IO_READ):
                    print("IO read")

                    cpl = pcie_usp.TLP_us()
                    cpl.set_completion(tlp, pcie_usp.PcieId(0, 0, 0))
                    cpl.fmt_type = pcie.TLP_CPL_DATA

                    region = tlp.bar_id
                    addr = tlp.address & 0xffff  # TODO
                    offset = 0
                    start_offset = None
                    mask = tlp.first_be

                    # perform operation
                    data = bytearray(4)

                    for k in range(4):
                        if mask & (1 << k):
                            if start_offset is None:
                                start_offset = offset
                        else:
                            if start_offset is not None and offset != start_offset:
                                data[start_offset:offset] = regions[region][
                                    addr + start_offset:addr + offset]
                            start_offset = None

                        offset += 1

                    if start_offset is not None and offset != start_offset:
                        data[start_offset:offset] = regions[region][
                            addr + start_offset:addr + offset]

                    cpl.set_data(data)
                    cpl.byte_count = 4
                    cpl.length = 1

                    cc_source.send(cpl.pack_us_cc())
                elif (tlp.fmt_type == pcie.TLP_IO_WRITE):
                    print("IO write")

                    cpl = pcie_usp.TLP_us()
                    cpl.set_completion(tlp, pcie_usp.PcieId(0, 0, 0))

                    region = tlp.bar_id
                    addr = tlp.address & 0xffff  # TODO
                    offset = 0
                    start_offset = None
                    mask = tlp.first_be

                    # perform operation
                    data = tlp.get_data()

                    for k in range(4):
                        if mask & (1 << k):
                            if start_offset is None:
                                start_offset = offset
                        else:
                            if start_offset is not None and offset != start_offset:
                                regions[region][addr + start_offset:addr +
                                                offset] = data[
                                                    start_offset:offset]
                            start_offset = None

                        offset += 1

                    if start_offset is not None and offset != start_offset:
                        regions[region][addr + start_offset:addr +
                                        offset] = data[start_offset:offset]

                    cc_source.send(cpl.pack_us_cc())
                if (tlp.fmt_type == pcie.TLP_MEM_READ
                        or tlp.fmt_type == pcie.TLP_MEM_READ_64):
                    print("Memory read")

                    # perform operation
                    region = tlp.bar_id
                    addr = tlp.address & 0xffff  # TODO
                    offset = 0
                    length = tlp.length

                    # perform read
                    data = regions[region][addr:addr + length * 4]

                    # prepare completion TLP(s)
                    n = 0
                    offset = 0
                    addr = tlp.address + offset
                    length = tlp.length * 4

                    while n < length:
                        cpl = pcie_usp.TLP_us()
                        cpl.set_completion(tlp, pcie_usp.PcieId(0, 0, 0))

                        byte_length = length - n
                        cpl.byte_count = byte_length
                        byte_length = min(
                            byte_length,
                            128 << dev.functions[0].max_payload_size
                        )  # max payload size
                        if byte_length > 128:
                            byte_length -= (addr +
                                            byte_length) % 128  # RCB align
                        byte_length = min(byte_length,
                                          0x1000 - (addr & 0xfff))  # 4k align

                        cpl.lower_address = addr & 0x7f

                        cpl.set_data(data[offset + n:offset + n + byte_length])

                        print("Completion: %s" % (repr(cpl)))
                        cc_source.send(cpl.pack_us_cc())

                        n += byte_length
                        addr += byte_length
                if (tlp.fmt_type == pcie.TLP_MEM_WRITE
                        or tlp.fmt_type == pcie.TLP_MEM_WRITE_64):
                    print("Memory write")

                    # perform operation
                    region = tlp.bar_id
                    addr = tlp.address & 0xffff  # TODO
                    offset = 0
                    start_offset = None
                    mask = tlp.first_be
                    length = tlp.length

                    # perform write
                    data = tlp.get_data()

                    # first dword
                    for k in range(4):
                        if mask & (1 << k):
                            if start_offset is None:
                                start_offset = offset
                        else:
                            if start_offset is not None and offset != start_offset:
                                regions[region][addr + start_offset:addr +
                                                offset] = data[
                                                    start_offset:offset]
                            start_offset = None

                        offset += 1

                    if length > 1:
                        # middle dwords
                        if start_offset is None:
                            start_offset = offset
                        offset += length * 4

                        # last dword
                        mask = tlp.last_be

                        for k in range(4):
                            if mask & (1 << k):
                                if start_offset is None:
                                    start_offset = offset
                            else:
                                if start_offset is not None and offset != start_offset:
                                    regions[region][addr + start_offset:addr +
                                                    offset] = data[
                                                        start_offset:offset]
                                start_offset = None

                            offset += 1

                    if start_offset is not None and offset != start_offset:
                        regions[region][addr + start_offset:addr +
                                        offset] = data[start_offset:offset]

            # haldle requester completion
            #if not rc_sink.empty():
            #    pkt = rc_sink.recv()

    @instance
    def check():
        yield delay(100)
        yield clk.posedge
        rst.next = 1
        yield clk.posedge
        rst.next = 0
        yield clk.posedge
        yield delay(100)
        yield clk.posedge

        current_tag = 1

        yield clk.posedge
        print("test 1: enumeration")
        current_test.next = 1

        yield rc.enumerate(enable_bus_mastering=True, configure_msi=True)

        yield delay(100)

        yield clk.posedge
        print("test 2: IO and memory read/write")
        current_test.next = 2

        yield from rc.io_write(0x80000000, bytearray(range(16)), 100)
        assert regions[3][0:16] == bytearray(range(16))

        val = yield from rc.io_read(0x80000000, 16, 100)
        assert val == bytearray(range(16))

        yield from rc.mem_write(0x80000000, bytearray(range(16)), 100)
        yield delay(100)
        assert regions[0][0:16] == bytearray(range(16))

        val = yield from rc.mem_read(0x80000000, 16, 100)
        assert val == bytearray(range(16))

        yield from rc.mem_write(0x8000000000000000, bytearray(range(16)), 100)
        yield delay(100)
        assert regions[1][0:16] == bytearray(range(16))

        val = yield from rc.mem_read(0x8000000000000000, 16, 100)
        assert val == bytearray(range(16))

        yield delay(100)

        # yield clk.posedge
        # print("test 3: Large read/write")
        # current_test.next = 3

        # yield from rc.mem_write(0x8000000000000000, bytearray(range(256))*32, 100)
        # yield delay(100)
        # assert ep.read_region(1, 0, 256*32) == bytearray(range(256))*32

        # val = yield from rc.mem_read(0x8000000000000000, 256*32, 100)
        # assert val == bytearray(range(256))*32

        # yield delay(100)

        yield clk.posedge
        print("test 4: DMA")
        current_test.next = 4

        #yield ep.io_write(io_base, bytearray(range(16)), 100)

        data = bytearray(range(16))
        addr = io_base
        n = 0

        while n < len(data):
            tlp = pcie_usp.TLP_us()
            tlp.fmt_type = pcie.TLP_IO_WRITE
            tlp.requester_id = pcie_usp.PcieId(dev.bus_num, dev.device_num, 0)
            tlp.tag = current_tag

            first_pad = addr % 4
            byte_length = min(len(data) - n, 4 - first_pad)
            tlp.set_be_data(addr, data[n:n + byte_length])

            tlp.address = addr & ~3

            current_tag = (current_tag % 31) + 1

            rq_source.send(tlp.pack_us_rq())
            yield rc_sink.wait(100)
            pkt = rc_sink.recv()

            if not pkt:
                raise Exception("Timeout")

            cpl = pcie_usp.TLP_us().unpack_us_rc(pkt)

            if cpl.status != pcie.CPL_STATUS_SC:
                raise Exception("Unsuccessful completion")

            n += byte_length
            addr += byte_length

        assert io_data[0:16] == bytearray(range(16))

        #val = yield from ep.io_read(io_base, 16, 100)

        length = 16
        data = b''
        addr = io_base
        n = 0

        while n < length:
            tlp = pcie_usp.TLP_us()
            tlp.fmt_type = pcie.TLP_IO_READ
            tlp.requester_id = pcie_usp.PcieId(dev.bus_num, dev.device_num, 0)
            tlp.tag = current_tag

            first_pad = addr % 4
            byte_length = min(length - n, 4 - first_pad)
            tlp.set_be(addr, byte_length)

            tlp.address = addr & ~3

            current_tag = (current_tag % 31) + 1

            rq_source.send(tlp.pack_us_rq())
            yield rc_sink.wait(100)
            pkt = rc_sink.recv()

            if not pkt:
                raise Exception("Timeout")

            cpl = pcie_usp.TLP_us().unpack_us_rc(pkt)

            if cpl.status != pcie.CPL_STATUS_SC:
                raise Exception("Unsuccessful completion")
            else:
                d = struct.pack('<L', cpl.data[0])

            data += d[first_pad:]

            n += byte_length
            addr += byte_length

        data = data[:length]

        assert val == bytearray(range(16))

        #yield ep.mem_write(mem_base, bytearray(range(16)), 100)

        data = bytearray(range(16))
        addr = io_base
        n = 0

        while n < len(data):
            tlp = pcie_usp.TLP_us()
            if addr > 0xffffffff:
                tlp.fmt_type = pcie.TLP_MEM_WRITE_64
            else:
                tlp.fmt_type = pcie.TLP_MEM_WRITE
            tlp.requester_id = pcie_usp.PcieId(dev.bus_num, dev.device_num, 0)
            tlp.tag = current_tag

            first_pad = addr % 4
            byte_length = len(data) - n
            byte_length = min(byte_length,
                              (128 << dev.functions[0].max_payload_size) -
                              first_pad)  # max payload size
            byte_length = min(byte_length, 0x1000 - (addr & 0xfff))  # 4k align
            tlp.set_be_data(addr, data[n:n + byte_length])

            tlp.address = addr & ~3

            current_tag = (current_tag % 31) + 1

            rq_source.send(tlp.pack_us_rq())

            n += byte_length
            addr += byte_length

        yield delay(100)
        assert mem_data[0:16] == bytearray(range(16))

        #val = yield from ep.mem_read(mem_base, 16, 100)

        length = 16
        data = b''
        addr = mem_base
        n = 0

        while n < length:
            tlp = pcie_usp.TLP_us()
            if addr > 0xffffffff:
                tlp.fmt_type = pcie.TLP_MEM_READ_64
            else:
                tlp.fmt_type = pcie.TLP_MEM_READ
            tlp.requester_id = pcie_usp.PcieId(dev.bus_num, dev.device_num, 0)
            tlp.tag = current_tag

            first_pad = addr % 4
            byte_length = length - n
            byte_length = min(byte_length,
                              (128 << dev.functions[0].max_read_request_size) -
                              first_pad)  # max read request size
            byte_length = min(byte_length, 0x1000 - (addr & 0xfff))  # 4k align
            tlp.set_be(addr, byte_length)

            tlp.address = addr & ~3

            current_tag = (current_tag % 31) + 1

            rq_source.send(tlp.pack_us_rq())

            m = 0

            while m < byte_length:
                yield rc_sink.wait(100)
                pkt = rc_sink.recv()

                if not pkt:
                    raise Exception("Timeout")

                cpl = pcie_usp.TLP_us().unpack_us_rc(pkt)

                if cpl.status != pcie.CPL_STATUS_SC:
                    raise Exception("Unsuccessful completion")
                else:
                    dw_len = cpl.length
                    if dw_len == 0:
                        dw_len = 1024
                    d = bytearray()

                    for k in range(dw_len):
                        d.extend(struct.pack('<L', cpl.data[k]))

                    offset = cpl.lower_address & 3
                    data += d[offset:offset + cpl.byte_count]

                m += len(d) - offset

            n += byte_length
            addr += byte_length

        assert val == bytearray(range(16))

        yield delay(100)

        yield clk.posedge
        print("test 5: MSI")
        current_test.next = 5

        yield user_clk.posedge
        cfg_interrupt_msi_int.next = 1 << 4
        yield user_clk.posedge
        cfg_interrupt_msi_int.next = 0

        yield rc.msi_get_signal(dev.functions[0].get_id(), 4)

        yield delay(100)

        raise StopSimulation

    return instances()
Esempio n. 4
0
def bench():

    # Parameters
    AXIS_PCIE_DATA_WIDTH = 512
    AXIS_PCIE_KEEP_WIDTH = (AXIS_PCIE_DATA_WIDTH / 32)
    AXIS_PCIE_RQ_USER_WIDTH = 137
    RQ_SEQ_NUM_WIDTH = 4 if AXIS_PCIE_RQ_USER_WIDTH == 60 else 6
    RQ_SEQ_NUM_ENABLE = 1
    SEG_COUNT = max(2, int(AXIS_PCIE_DATA_WIDTH * 2 / 128))
    SEG_DATA_WIDTH = AXIS_PCIE_DATA_WIDTH * 2 / SEG_COUNT
    SEG_ADDR_WIDTH = 12
    SEG_BE_WIDTH = int(SEG_DATA_WIDTH / 8)
    RAM_SEL_WIDTH = 2
    RAM_ADDR_WIDTH = SEG_ADDR_WIDTH + (SEG_COUNT - 1).bit_length() + (
        SEG_BE_WIDTH - 1).bit_length()
    PCIE_ADDR_WIDTH = 64
    LEN_WIDTH = 16
    TAG_WIDTH = 8
    OP_TABLE_SIZE = 2**(RQ_SEQ_NUM_WIDTH - 1)
    TX_LIMIT = 2**(RQ_SEQ_NUM_WIDTH - 1)
    TX_FC_ENABLE = 1

    # Inputs
    clk = Signal(bool(0))
    rst = Signal(bool(0))
    current_test = Signal(intbv(0)[8:])

    s_axis_rq_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    s_axis_rq_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    s_axis_rq_tvalid = Signal(bool(0))
    s_axis_rq_tlast = Signal(bool(0))
    s_axis_rq_tuser = Signal(intbv(0)[AXIS_PCIE_RQ_USER_WIDTH:])
    m_axis_rq_tready = Signal(bool(0))
    s_axis_rq_seq_num_0 = Signal(intbv(0)[RQ_SEQ_NUM_WIDTH:])
    s_axis_rq_seq_num_valid_0 = Signal(bool(0))
    s_axis_rq_seq_num_1 = Signal(intbv(0)[RQ_SEQ_NUM_WIDTH:])
    s_axis_rq_seq_num_valid_1 = Signal(bool(0))
    pcie_tx_fc_ph_av = Signal(intbv(0)[8:])
    pcie_tx_fc_pd_av = Signal(intbv(0)[12:])
    s_axis_write_desc_pcie_addr = Signal(intbv(0)[PCIE_ADDR_WIDTH:])
    s_axis_write_desc_ram_sel = Signal(intbv(0)[RAM_SEL_WIDTH:])
    s_axis_write_desc_ram_addr = Signal(intbv(0)[RAM_ADDR_WIDTH:])
    s_axis_write_desc_len = Signal(intbv(0)[LEN_WIDTH:])
    s_axis_write_desc_tag = Signal(intbv(0)[TAG_WIDTH:])
    s_axis_write_desc_valid = Signal(bool(0))
    ram_rd_cmd_ready = Signal(intbv(0)[SEG_COUNT:])
    ram_rd_resp_data = Signal(intbv(0)[SEG_COUNT * SEG_DATA_WIDTH:])
    ram_rd_resp_valid = Signal(intbv(0)[SEG_COUNT:])
    enable = Signal(bool(0))
    requester_id = Signal(intbv(0)[16:])
    requester_id_enable = Signal(bool(0))
    max_payload_size = Signal(intbv(0)[3:])

    # Outputs
    s_axis_rq_tready = Signal(bool(0))
    m_axis_rq_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    m_axis_rq_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    m_axis_rq_tvalid = Signal(bool(0))
    m_axis_rq_tlast = Signal(bool(0))
    m_axis_rq_tuser = Signal(intbv(0)[AXIS_PCIE_RQ_USER_WIDTH:])
    m_axis_rq_seq_num_0 = Signal(intbv(0)[RQ_SEQ_NUM_WIDTH:])
    m_axis_rq_seq_num_valid_0 = Signal(bool(0))
    m_axis_rq_seq_num_1 = Signal(intbv(0)[RQ_SEQ_NUM_WIDTH:])
    m_axis_rq_seq_num_valid_1 = Signal(bool(0))
    s_axis_write_desc_ready = Signal(bool(0))
    m_axis_write_desc_status_tag = Signal(intbv(0)[TAG_WIDTH:])
    m_axis_write_desc_status_valid = Signal(bool(0))
    ram_rd_cmd_sel = Signal(intbv(0)[SEG_COUNT * RAM_SEL_WIDTH:])
    ram_rd_cmd_addr = Signal(intbv(0)[SEG_COUNT * SEG_ADDR_WIDTH:])
    ram_rd_cmd_valid = Signal(intbv(0)[SEG_COUNT:])
    ram_rd_resp_ready = Signal(intbv(0)[SEG_COUNT:])

    # Clock and Reset Interface
    user_clk = Signal(bool(0))
    user_reset = Signal(bool(0))
    sys_clk = Signal(bool(0))
    sys_reset = Signal(bool(0))

    # PCIe DMA RAM
    dma_ram_inst = dma_ram.PSDPRam(2**16)
    dma_ram_pause = Signal(bool(0))

    dma_ram_port0 = dma_ram_inst.create_read_ports(
        user_clk,
        ram_rd_cmd_addr=ram_rd_cmd_addr,
        ram_rd_cmd_valid=ram_rd_cmd_valid,
        ram_rd_cmd_ready=ram_rd_cmd_ready,
        ram_rd_resp_data=ram_rd_resp_data,
        ram_rd_resp_valid=ram_rd_resp_valid,
        ram_rd_resp_ready=ram_rd_resp_ready,
        pause=dma_ram_pause,
        name='port0')

    # sources and sinks
    write_desc_source = axis_ep.AXIStreamSource()

    write_desc_source_logic = write_desc_source.create_logic(
        user_clk,
        user_reset,
        tdata=(s_axis_write_desc_pcie_addr, s_axis_write_desc_ram_sel,
               s_axis_write_desc_ram_addr, s_axis_write_desc_len,
               s_axis_write_desc_tag),
        tvalid=s_axis_write_desc_valid,
        tready=s_axis_write_desc_ready,
        name='write_desc_source')

    write_desc_status_sink = axis_ep.AXIStreamSink()

    write_desc_status_sink_logic = write_desc_status_sink.create_logic(
        user_clk,
        user_reset,
        tdata=(m_axis_write_desc_status_tag, ),
        tvalid=m_axis_write_desc_status_valid,
        name='write_desc_status_sink')

    # PCIe devices
    rc = pcie.RootComplex()

    mem_base, mem_data = rc.alloc_region(16 * 1024 * 1024)

    dev = pcie_usp.UltrascalePlusPCIe()

    dev.pcie_generation = 3
    dev.pcie_link_width = 16
    dev.user_clock_frequency = 256e6

    rc.make_port().connect(dev)

    cq_pause = Signal(bool(0))
    cc_pause = Signal(bool(0))
    rq_pause = Signal(bool(0))
    rc_pause = Signal(bool(0))

    pcie_logic = dev.create_logic(
        # Completer reQuest Interface
        m_axis_cq_tdata=Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:]),
        m_axis_cq_tuser=Signal(intbv(0)[183:]),
        m_axis_cq_tlast=Signal(bool(0)),
        m_axis_cq_tkeep=Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:]),
        m_axis_cq_tvalid=Signal(bool(0)),
        m_axis_cq_tready=Signal(bool(1)),
        pcie_cq_np_req=Signal(intbv(3)[2:]),
        pcie_cq_np_req_count=Signal(intbv(0)[6:]),

        # Completer Completion Interface
        s_axis_cc_tdata=Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:]),
        s_axis_cc_tuser=Signal(intbv(0)[81:]),
        s_axis_cc_tlast=Signal(bool(0)),
        s_axis_cc_tkeep=Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:]),
        s_axis_cc_tvalid=Signal(bool(0)),
        s_axis_cc_tready=Signal(bool(0)),

        # Requester reQuest Interface
        s_axis_rq_tdata=m_axis_rq_tdata,
        s_axis_rq_tuser=m_axis_rq_tuser,
        s_axis_rq_tlast=m_axis_rq_tlast,
        s_axis_rq_tkeep=m_axis_rq_tkeep,
        s_axis_rq_tvalid=m_axis_rq_tvalid,
        s_axis_rq_tready=m_axis_rq_tready,
        pcie_rq_seq_num0=s_axis_rq_seq_num_0,
        pcie_rq_seq_num_vld0=s_axis_rq_seq_num_valid_0,
        pcie_rq_seq_num1=s_axis_rq_seq_num_1,
        pcie_rq_seq_num_vld1=s_axis_rq_seq_num_valid_1,
        # pcie_rq_tag0=pcie_rq_tag0,
        # pcie_rq_tag1=pcie_rq_tag1,
        # pcie_rq_tag_av=pcie_rq_tag_av,
        # pcie_rq_tag_vld0=pcie_rq_tag_vld0,
        # pcie_rq_tag_vld1=pcie_rq_tag_vld1,

        # Requester Completion Interface
        m_axis_rc_tdata=Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:]),
        m_axis_rc_tuser=Signal(intbv(0)[161:]),
        m_axis_rc_tlast=Signal(bool(0)),
        m_axis_rc_tkeep=Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:]),
        m_axis_rc_tvalid=Signal(bool(0)),
        m_axis_rc_tready=Signal(bool(0)),

        # Transmit Flow Control Interface
        # pcie_tfc_nph_av=pcie_tfc_nph_av,
        # pcie_tfc_npd_av=pcie_tfc_npd_av,

        # Configuration Flow Control Interface
        cfg_fc_ph=pcie_tx_fc_ph_av,
        cfg_fc_pd=pcie_tx_fc_pd_av,
        #cfg_fc_nph=cfg_fc_nph,
        #cfg_fc_npd=cfg_fc_npd,
        #cfg_fc_cplh=cfg_fc_cplh,
        #cfg_fc_cpld=cfg_fc_cpld,
        cfg_fc_sel=Signal(intbv(0b100)[3:]),

        # Configuration Control Interface
        # cfg_hot_reset_in=cfg_hot_reset_in,
        # cfg_hot_reset_out=cfg_hot_reset_out,
        # cfg_config_space_enable=cfg_config_space_enable,
        # cfg_dsn=cfg_dsn,
        # cfg_ds_port_number=cfg_ds_port_number,
        # cfg_ds_bus_number=cfg_ds_bus_number,
        # cfg_ds_device_number=cfg_ds_device_number,
        # cfg_ds_function_number=cfg_ds_function_number,
        # cfg_power_state_change_ack=cfg_power_state_change_ack,
        # cfg_power_state_change_interrupt=cfg_power_state_change_interrupt,
        # cfg_err_cor_in=cfg_err_cor_in,
        # cfg_err_uncor_in=cfg_err_uncor_in,
        # cfg_flr_done=cfg_flr_done,
        # cfg_vf_flr_done=cfg_vf_flr_done,
        # cfg_flr_in_process=cfg_flr_in_process,
        # cfg_vf_flr_in_process=cfg_vf_flr_in_process,
        # cfg_req_pm_transition_l23_ready=cfg_req_pm_transition_l23_ready,
        # cfg_link_training_enable=cfg_link_training_enable,

        # Clock and Reset Interface
        user_clk=user_clk,
        user_reset=user_reset,
        #user_lnk_up=user_lnk_up,
        sys_clk=sys_clk,
        sys_clk_gt=sys_clk,
        sys_reset=sys_reset,
        cq_pause=cq_pause,
        cc_pause=cc_pause,
        rq_pause=rq_pause,
        rc_pause=rc_pause)

    # DUT
    if os.system(build_cmd):
        raise Exception("Error running build command")

    dut = Cosimulation(
        "vvp -m myhdl %s.vvp -lxt2" % testbench,
        clk=user_clk,
        rst=user_reset,
        current_test=current_test,
        s_axis_rq_tdata=s_axis_rq_tdata,
        s_axis_rq_tkeep=s_axis_rq_tkeep,
        s_axis_rq_tvalid=s_axis_rq_tvalid,
        s_axis_rq_tready=s_axis_rq_tready,
        s_axis_rq_tlast=s_axis_rq_tlast,
        s_axis_rq_tuser=s_axis_rq_tuser,
        m_axis_rq_tdata=m_axis_rq_tdata,
        m_axis_rq_tkeep=m_axis_rq_tkeep,
        m_axis_rq_tvalid=m_axis_rq_tvalid,
        m_axis_rq_tready=m_axis_rq_tready,
        m_axis_rq_tlast=m_axis_rq_tlast,
        m_axis_rq_tuser=m_axis_rq_tuser,
        s_axis_rq_seq_num_0=s_axis_rq_seq_num_0,
        s_axis_rq_seq_num_valid_0=s_axis_rq_seq_num_valid_0,
        s_axis_rq_seq_num_1=s_axis_rq_seq_num_1,
        s_axis_rq_seq_num_valid_1=s_axis_rq_seq_num_valid_1,
        m_axis_rq_seq_num_0=m_axis_rq_seq_num_0,
        m_axis_rq_seq_num_valid_0=m_axis_rq_seq_num_valid_0,
        m_axis_rq_seq_num_1=m_axis_rq_seq_num_1,
        m_axis_rq_seq_num_valid_1=m_axis_rq_seq_num_valid_1,
        pcie_tx_fc_ph_av=pcie_tx_fc_ph_av,
        pcie_tx_fc_pd_av=pcie_tx_fc_pd_av,
        s_axis_write_desc_pcie_addr=s_axis_write_desc_pcie_addr,
        s_axis_write_desc_ram_sel=s_axis_write_desc_ram_sel,
        s_axis_write_desc_ram_addr=s_axis_write_desc_ram_addr,
        s_axis_write_desc_len=s_axis_write_desc_len,
        s_axis_write_desc_tag=s_axis_write_desc_tag,
        s_axis_write_desc_valid=s_axis_write_desc_valid,
        s_axis_write_desc_ready=s_axis_write_desc_ready,
        m_axis_write_desc_status_tag=m_axis_write_desc_status_tag,
        m_axis_write_desc_status_valid=m_axis_write_desc_status_valid,
        ram_rd_cmd_sel=ram_rd_cmd_sel,
        ram_rd_cmd_addr=ram_rd_cmd_addr,
        ram_rd_cmd_valid=ram_rd_cmd_valid,
        ram_rd_cmd_ready=ram_rd_cmd_ready,
        ram_rd_resp_data=ram_rd_resp_data,
        ram_rd_resp_valid=ram_rd_resp_valid,
        ram_rd_resp_ready=ram_rd_resp_ready,
        enable=enable,
        requester_id=requester_id,
        requester_id_enable=requester_id_enable,
        max_payload_size=max_payload_size)

    @always(delay(4))
    def clkgen():
        clk.next = not clk

    @always_comb
    def clk_logic():
        sys_clk.next = clk
        sys_reset.next = not rst

    cq_pause_toggle = Signal(bool(0))
    cc_pause_toggle = Signal(bool(0))
    rq_pause_toggle = Signal(bool(0))
    rc_pause_toggle = Signal(bool(0))

    @instance
    def pause_toggle():
        while True:
            if (cq_pause_toggle or cc_pause_toggle or rq_pause_toggle
                    or rc_pause_toggle):
                cq_pause.next = cq_pause_toggle
                cc_pause.next = cc_pause_toggle
                rq_pause.next = rq_pause_toggle
                rc_pause.next = rc_pause_toggle

                yield user_clk.posedge
                yield user_clk.posedge
                yield user_clk.posedge

                cq_pause.next = 0
                cc_pause.next = 0
                rq_pause.next = 0
                rc_pause.next = 0

            yield user_clk.posedge

    @instance
    def check():
        yield delay(100)
        yield clk.posedge
        rst.next = 1
        yield clk.posedge
        rst.next = 0
        yield clk.posedge
        yield delay(100)
        yield clk.posedge

        # testbench stimulus

        cur_tag = 1

        max_payload_size.next = 0

        enable.next = 1

        yield user_clk.posedge
        print("test 1: enumeration")
        current_test.next = 1

        yield rc.enumerate(enable_bus_mastering=True)

        yield delay(100)

        yield user_clk.posedge
        print("test 2: PCIe write")
        current_test.next = 2

        pcie_addr = 0x00000000
        ram_addr = 0x00000000
        test_data = b'\x11\x22\x33\x44'

        dma_ram_inst.write_mem(ram_addr, test_data)

        data = dma_ram_inst.read_mem(ram_addr, 32)
        for i in range(0, len(data), 16):
            print(" ".join(
                ("{:02x}".format(c) for c in bytearray(data[i:i + 16]))))

        write_desc_source.send([(mem_base + pcie_addr, 0, ram_addr,
                                 len(test_data), cur_tag)])

        yield write_desc_status_sink.wait(1000)
        yield delay(50)

        status = write_desc_status_sink.recv()

        print(status)

        assert status.data[0][0] == cur_tag

        data = mem_data[pcie_addr:pcie_addr + 32]
        for i in range(0, len(data), 16):
            print(" ".join(
                ("{:02x}".format(c) for c in bytearray(data[i:i + 16]))))

        assert mem_data[pcie_addr:pcie_addr + len(test_data)] == test_data

        cur_tag = (cur_tag + 1) % 256

        yield delay(100)

        yield user_clk.posedge
        print("test 3: various writes")
        current_test.next = 3

        for length in list(range(1, 67)) + list(range(128 - 4,
                                                      128 + 4)) + [1024]:
            for pcie_offset in list(range(8, 13)) + list(
                    range(4096 - 4, 4096 + 4)):
                for ram_offset in list(range(8, 137)) + list(
                        range(4096 - 128, 4096)):
                    for pause in [False, True]:
                        print("length %d, pcie_offset %d, ram_offset %d" %
                              (length, pcie_offset, ram_offset))
                        #pcie_addr = length * 0x100000000 + pcie_offset * 0x10000 + offset
                        pcie_addr = pcie_offset
                        ram_addr = ram_offset
                        test_data = bytearray([x % 256 for x in range(length)])

                        dma_ram_inst.write_mem(
                            ram_addr & 0xffff80,
                            b'\x55' * (len(test_data) + 256))
                        mem_data[(pcie_addr - 1)
                                 & 0xffff80:((pcie_addr - 1) & 0xffff80) +
                                 len(test_data) +
                                 256] = b'\xaa' * (len(test_data) + 256)
                        dma_ram_inst.write_mem(ram_addr, test_data)

                        data = dma_ram_inst.read_mem(ram_addr & 0xfffff0, 64)
                        for i in range(0, len(data), 16):
                            print(" ".join(
                                ("{:02x}".format(c)
                                 for c in bytearray(data[i:i + 16]))))

                        rq_pause_toggle.next = pause

                        write_desc_source.send([
                            (mem_base + pcie_addr, 0, ram_addr, len(test_data),
                             cur_tag)
                        ])

                        yield write_desc_status_sink.wait(4000)
                        yield delay(50)

                        rq_pause_toggle.next = 0

                        status = write_desc_status_sink.recv()

                        print(status)

                        assert status.data[0][0] == cur_tag

                        data = mem_data[pcie_addr
                                        & 0xfffff0:(pcie_addr & 0xfffff0) + 64]
                        for i in range(0, len(data), 16):
                            print(" ".join(
                                ("{:02x}".format(c)
                                 for c in bytearray(data[i:i + 16]))))

                        print(mem_data[pcie_addr - 1:pcie_addr +
                                       len(test_data) + 1])
                        assert mem_data[pcie_addr - 1:pcie_addr +
                                        len(test_data) +
                                        1] == b'\xaa' + test_data + b'\xaa'

                        cur_tag = (cur_tag + 1) % 256

                        yield delay(100)

        raise StopSimulation

    return instances()
def bench():

    # Parameters
    AXIS_PCIE_DATA_WIDTH = 512
    AXIS_PCIE_KEEP_WIDTH = (AXIS_PCIE_DATA_WIDTH/32)
    AXIS_PCIE_RQ_USER_WIDTH = 137
    AXI_DATA_WIDTH = AXIS_PCIE_DATA_WIDTH
    AXI_ADDR_WIDTH = 64
    AXI_STRB_WIDTH = (AXI_DATA_WIDTH/8)
    AXI_ID_WIDTH = 8
    AXI_MAX_BURST_LEN = 256
    PCIE_ADDR_WIDTH = 64
    LEN_WIDTH = 20
    TAG_WIDTH = 8

    # Inputs
    clk = Signal(bool(0))
    rst = Signal(bool(0))
    current_test = Signal(intbv(0)[8:])

    s_axis_rq_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    s_axis_rq_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    s_axis_rq_tvalid = Signal(bool(0))
    s_axis_rq_tlast = Signal(bool(0))
    s_axis_rq_tuser = Signal(intbv(0)[AXIS_PCIE_RQ_USER_WIDTH:])
    m_axis_rq_tready = Signal(bool(0))
    s_axis_write_desc_pcie_addr = Signal(intbv(0)[PCIE_ADDR_WIDTH:])
    s_axis_write_desc_axi_addr = Signal(intbv(0)[AXI_ADDR_WIDTH:])
    s_axis_write_desc_len = Signal(intbv(0)[LEN_WIDTH:])
    s_axis_write_desc_tag = Signal(intbv(0)[TAG_WIDTH:])
    s_axis_write_desc_valid = Signal(bool(0))
    m_axi_arready = Signal(bool(0))
    m_axi_rid = Signal(intbv(0)[AXI_ID_WIDTH:])
    m_axi_rdata = Signal(intbv(0)[AXI_DATA_WIDTH:])
    m_axi_rresp = Signal(intbv(0)[2:])
    m_axi_rlast = Signal(bool(0))
    m_axi_rvalid = Signal(bool(0))
    enable = Signal(bool(0))
    requester_id = Signal(intbv(0)[16:])
    requester_id_enable = Signal(bool(0))
    max_payload_size = Signal(intbv(0)[3:])

    # Outputs
    s_axis_rq_tready = Signal(bool(0))
    m_axis_rq_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    m_axis_rq_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    m_axis_rq_tvalid = Signal(bool(0))
    m_axis_rq_tlast = Signal(bool(0))
    m_axis_rq_tuser = Signal(intbv(0)[AXIS_PCIE_RQ_USER_WIDTH:])
    s_axis_write_desc_ready = Signal(bool(0))
    m_axis_write_desc_status_tag = Signal(intbv(0)[TAG_WIDTH:])
    m_axis_write_desc_status_valid = Signal(bool(0))
    m_axi_arid = Signal(intbv(0)[AXI_ID_WIDTH:])
    m_axi_araddr = Signal(intbv(0)[AXI_ADDR_WIDTH:])
    m_axi_arlen = Signal(intbv(0)[8:])
    m_axi_arsize = Signal(intbv(6)[3:])
    m_axi_arburst = Signal(intbv(1)[2:])
    m_axi_arlock = Signal(bool(0))
    m_axi_arcache = Signal(intbv(3)[4:])
    m_axi_arprot = Signal(intbv(2)[3:])
    m_axi_arvalid = Signal(bool(0))
    m_axi_rready = Signal(bool(0))

    # Clock and Reset Interface
    user_clk=Signal(bool(0))
    user_reset=Signal(bool(0))
    sys_clk=Signal(bool(0))
    sys_reset=Signal(bool(0))

    # AXI4 RAM model
    axi_ram_inst = axi.AXIRam(2**16)

    axi_ram_port0 = axi_ram_inst.create_port(
        user_clk,
        s_axi_arid=m_axi_arid,
        s_axi_araddr=m_axi_araddr,
        s_axi_arlen=m_axi_arlen,
        s_axi_arsize=m_axi_arsize,
        s_axi_arburst=m_axi_arburst,
        s_axi_arlock=m_axi_arlock,
        s_axi_arcache=m_axi_arcache,
        s_axi_arprot=m_axi_arprot,
        s_axi_arvalid=m_axi_arvalid,
        s_axi_arready=m_axi_arready,
        s_axi_rid=m_axi_rid,
        s_axi_rdata=m_axi_rdata,
        s_axi_rresp=m_axi_rresp,
        s_axi_rlast=m_axi_rlast,
        s_axi_rvalid=m_axi_rvalid,
        s_axi_rready=m_axi_rready,
        name='port0'
    )

    write_desc_source = axis_ep.AXIStreamSource()

    write_desc_source_logic = write_desc_source.create_logic(
        user_clk,
        user_reset,
        tdata=(s_axis_write_desc_pcie_addr, s_axis_write_desc_axi_addr, s_axis_write_desc_len, s_axis_write_desc_tag),
        tvalid=s_axis_write_desc_valid,
        tready=s_axis_write_desc_ready,
        name='write_desc_source'
    )

    write_desc_status_sink = axis_ep.AXIStreamSink()

    write_desc_status_sink_logic = write_desc_status_sink.create_logic(
        user_clk,
        user_reset,
        tdata=(m_axis_write_desc_status_tag,),
        tvalid=m_axis_write_desc_status_valid,
        name='write_desc_status_sink'
    )

    # PCIe devices
    rc = pcie.RootComplex()

    mem_base, mem_data = rc.alloc_region(16*1024*1024)

    dev = pcie_usp.UltrascalePlusPCIe()

    dev.pcie_generation = 3
    dev.pcie_link_width = 16
    dev.user_clock_frequency = 250e6

    rc.make_port().connect(dev)

    cq_pause = Signal(bool(0))
    cc_pause = Signal(bool(0))
    rq_pause = Signal(bool(0))
    rc_pause = Signal(bool(0))

    pcie_logic = dev.create_logic(
        # Completer reQuest Interface
        m_axis_cq_tdata=Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:]),
        m_axis_cq_tuser=Signal(intbv(0)[183:]),
        m_axis_cq_tlast=Signal(bool(0)),
        m_axis_cq_tkeep=Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:]),
        m_axis_cq_tvalid=Signal(bool(0)),
        m_axis_cq_tready=Signal(bool(1)),
        #pcie_cq_np_req=pcie_cq_np_req,
        #pcie_cq_np_req_count=pcie_cq_np_req_count,

        # Completer Completion Interface
        s_axis_cc_tdata=Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:]),
        s_axis_cc_tuser=Signal(intbv(0)[81:]),
        s_axis_cc_tlast=Signal(bool(0)),
        s_axis_cc_tkeep=Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:]),
        s_axis_cc_tvalid=Signal(bool(0)),
        s_axis_cc_tready=Signal(bool(0)),

        # Requester reQuest Interface
        s_axis_rq_tdata=m_axis_rq_tdata,
        s_axis_rq_tuser=m_axis_rq_tuser,
        s_axis_rq_tlast=m_axis_rq_tlast,
        s_axis_rq_tkeep=m_axis_rq_tkeep,
        s_axis_rq_tvalid=m_axis_rq_tvalid,
        s_axis_rq_tready=m_axis_rq_tready,
        # pcie_rq_seq_num0=pcie_rq_seq_num0,
        # pcie_rq_seq_num_vld0=pcie_rq_seq_num_vld0,
        # pcie_rq_seq_num1=pcie_rq_seq_num1,
        # pcie_rq_seq_num_vld1=pcie_rq_seq_num_vld1,
        # pcie_rq_tag0=pcie_rq_tag0,
        # pcie_rq_tag1=pcie_rq_tag1,
        # pcie_rq_tag_av=pcie_rq_tag_av,
        # pcie_rq_tag_vld0=pcie_rq_tag_vld0,
        # pcie_rq_tag_vld1=pcie_rq_tag_vld1,

        # Requester Completion Interface
        m_axis_rc_tdata=Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:]),
        m_axis_rc_tuser=Signal(intbv(0)[161:]),
        m_axis_rc_tlast=Signal(bool(0)),
        m_axis_rc_tkeep=Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:]),
        m_axis_rc_tvalid=Signal(bool(0)),
        m_axis_rc_tready=Signal(bool(0)),

        # Transmit Flow Control Interface
        # pcie_tfc_nph_av=pcie_tfc_nph_av,
        # pcie_tfc_npd_av=pcie_tfc_npd_av,

        # Configuration Control Interface
        # cfg_hot_reset_in=cfg_hot_reset_in,
        # cfg_hot_reset_out=cfg_hot_reset_out,
        # cfg_config_space_enable=cfg_config_space_enable,
        # cfg_dsn=cfg_dsn,
        # cfg_ds_port_number=cfg_ds_port_number,
        # cfg_ds_bus_number=cfg_ds_bus_number,
        # cfg_ds_device_number=cfg_ds_device_number,
        # cfg_ds_function_number=cfg_ds_function_number,
        # cfg_power_state_change_ack=cfg_power_state_change_ack,
        # cfg_power_state_change_interrupt=cfg_power_state_change_interrupt,
        # cfg_err_cor_in=cfg_err_cor_in,
        # cfg_err_uncor_in=cfg_err_uncor_in,
        # cfg_flr_done=cfg_flr_done,
        # cfg_vf_flr_done=cfg_vf_flr_done,
        # cfg_flr_in_process=cfg_flr_in_process,
        # cfg_vf_flr_in_process=cfg_vf_flr_in_process,
        # cfg_req_pm_transition_l23_ready=cfg_req_pm_transition_l23_ready,
        # cfg_link_training_enable=cfg_link_training_enable,

        # Clock and Reset Interface
        user_clk=user_clk,
        user_reset=user_reset,
        #user_lnk_up=user_lnk_up,
        sys_clk=sys_clk,
        sys_clk_gt=sys_clk,
        sys_reset=sys_reset,

        cq_pause=cq_pause,
        cc_pause=cc_pause,
        rq_pause=rq_pause,
        rc_pause=rc_pause
    )

    # DUT
    if os.system(build_cmd):
        raise Exception("Error running build command")

    dut = Cosimulation(
        "vvp -m myhdl %s.vvp -lxt2" % testbench,
        clk=user_clk,
        rst=user_reset,
        current_test=current_test,
        s_axis_rq_tdata=s_axis_rq_tdata,
        s_axis_rq_tkeep=s_axis_rq_tkeep,
        s_axis_rq_tvalid=s_axis_rq_tvalid,
        s_axis_rq_tready=s_axis_rq_tready,
        s_axis_rq_tlast=s_axis_rq_tlast,
        s_axis_rq_tuser=s_axis_rq_tuser,
        m_axis_rq_tdata=m_axis_rq_tdata,
        m_axis_rq_tkeep=m_axis_rq_tkeep,
        m_axis_rq_tvalid=m_axis_rq_tvalid,
        m_axis_rq_tready=m_axis_rq_tready,
        m_axis_rq_tlast=m_axis_rq_tlast,
        m_axis_rq_tuser=m_axis_rq_tuser,
        s_axis_write_desc_pcie_addr=s_axis_write_desc_pcie_addr,
        s_axis_write_desc_axi_addr=s_axis_write_desc_axi_addr,
        s_axis_write_desc_len=s_axis_write_desc_len,
        s_axis_write_desc_tag=s_axis_write_desc_tag,
        s_axis_write_desc_valid=s_axis_write_desc_valid,
        s_axis_write_desc_ready=s_axis_write_desc_ready,
        m_axis_write_desc_status_tag=m_axis_write_desc_status_tag,
        m_axis_write_desc_status_valid=m_axis_write_desc_status_valid,
        m_axi_arid=m_axi_arid,
        m_axi_araddr=m_axi_araddr,
        m_axi_arlen=m_axi_arlen,
        m_axi_arsize=m_axi_arsize,
        m_axi_arburst=m_axi_arburst,
        m_axi_arlock=m_axi_arlock,
        m_axi_arcache=m_axi_arcache,
        m_axi_arprot=m_axi_arprot,
        m_axi_arvalid=m_axi_arvalid,
        m_axi_arready=m_axi_arready,
        m_axi_rid=m_axi_rid,
        m_axi_rdata=m_axi_rdata,
        m_axi_rresp=m_axi_rresp,
        m_axi_rlast=m_axi_rlast,
        m_axi_rvalid=m_axi_rvalid,
        m_axi_rready=m_axi_rready,
        enable=enable,
        requester_id=requester_id,
        requester_id_enable=requester_id_enable,
        max_payload_size=max_payload_size
    )

    @always(delay(4))
    def clkgen():
        clk.next = not clk

    @always_comb
    def clk_logic():
        sys_clk.next = clk
        sys_reset.next = not rst

    cq_pause_toggle = Signal(bool(0))
    cc_pause_toggle = Signal(bool(0))
    rq_pause_toggle = Signal(bool(0))
    rc_pause_toggle = Signal(bool(0))

    @instance
    def pause_toggle():
        while True:
            if (cq_pause_toggle or cc_pause_toggle or rq_pause_toggle or rc_pause_toggle):
                cq_pause.next = cq_pause_toggle
                cc_pause.next = cc_pause_toggle
                rq_pause.next = rq_pause_toggle
                rc_pause.next = rc_pause_toggle

                yield user_clk.posedge
                yield user_clk.posedge
                yield user_clk.posedge

                cq_pause.next = 0
                cc_pause.next = 0
                rq_pause.next = 0
                rc_pause.next = 0

            yield user_clk.posedge

    @instance
    def check():
        yield delay(100)
        yield clk.posedge
        rst.next = 1
        yield clk.posedge
        rst.next = 0
        yield clk.posedge
        yield delay(100)
        yield clk.posedge

        # testbench stimulus

        cur_tag = 1

        max_payload_size.next = 0

        enable.next = 1

        yield user_clk.posedge
        print("test 1: enumeration")
        current_test.next = 1

        yield rc.enumerate(enable_bus_mastering=True)

        yield delay(100)

        yield user_clk.posedge
        print("test 2: PCIe write")
        current_test.next = 2

        pcie_addr = 0x00000000
        axi_addr = 0x00000000
        test_data = b'\x11\x22\x33\x44'

        axi_ram_inst.write_mem(axi_addr, test_data)

        data = axi_ram_inst.read_mem(axi_addr, 32)
        for i in range(0, len(data), 16):
            print(" ".join(("{:02x}".format(c) for c in bytearray(data[i:i+16]))))

        write_desc_source.send([(mem_base+pcie_addr, axi_addr, len(test_data), cur_tag)])

        yield write_desc_status_sink.wait(1000)
        yield delay(50)

        status = write_desc_status_sink.recv()

        print(status)

        assert status.data[0][0] == cur_tag

        data = mem_data[pcie_addr:pcie_addr+32]
        for i in range(0, len(data), 16):
            print(" ".join(("{:02x}".format(c) for c in bytearray(data[i:i+16]))))

        assert mem_data[pcie_addr:pcie_addr+len(test_data)] == test_data

        cur_tag = (cur_tag + 1) % 256

        yield delay(100)

        yield user_clk.posedge
        print("test 3: various writes")
        current_test.next = 3

        for length in list(range(1,67))+list(range(128-4,128+4))+[1024]:
            for pcie_offset in list(range(8,13))+list(range(4096-4,4096+4)):
                for axi_offset in list(range(8,73))+list(range(4096-64,4096)):
                    for pause in [False, True]:
                        print("length %d, pcie_offset %d, axi_offset %d"% (length, pcie_offset, axi_offset))
                        #pcie_addr = length * 0x100000000 + pcie_offset * 0x10000 + offset
                        pcie_addr = pcie_offset
                        axi_addr = axi_offset
                        test_data = bytearray([x%256 for x in range(length)])

                        axi_ram_inst.write_mem(axi_addr & 0xffff00, b'\x55'*(len(test_data)+512))
                        mem_data[(pcie_addr-1) & 0xffff00:((pcie_addr-1) & 0xffff00)+len(test_data)+512] = b'\xaa'*(len(test_data)+512)
                        axi_ram_inst.write_mem(axi_addr, test_data) 

                        data = axi_ram_inst.read_mem(axi_addr&0xfffff0, 64)
                        for i in range(0, len(data), 16):
                            print(" ".join(("{:02x}".format(c) for c in bytearray(data[i:i+16]))))

                        rq_pause_toggle.next = pause

                        write_desc_source.send([(mem_base+pcie_addr, axi_addr, len(test_data), cur_tag)])

                        yield write_desc_status_sink.wait(4000)
                        yield delay(50)

                        rq_pause_toggle.next = 0

                        status = write_desc_status_sink.recv()

                        print(status)

                        assert status.data[0][0] == cur_tag

                        data = mem_data[pcie_addr&0xfffff0:(pcie_addr&0xfffff0)+64]
                        for i in range(0, len(data), 16):
                            print(" ".join(("{:02x}".format(c) for c in bytearray(data[i:i+16]))))

                        assert mem_data[pcie_addr-1:pcie_addr+len(test_data)+1] == b'\xaa'+test_data+b'\xaa'

                        cur_tag = (cur_tag + 1) % 256

                        yield delay(100)

        raise StopSimulation

    return instances()
Esempio n. 6
0
def bench():

    # Parameters

    # Inputs
    clk = Signal(bool(0))
    rst = Signal(bool(0))
    current_test = Signal(intbv(0)[8:])

    clk_156mhz = Signal(bool(0))
    rst_156mhz = Signal(bool(0))
    clk_250mhz = Signal(bool(0))
    rst_250mhz = Signal(bool(0))
    user_sw = Signal(intbv(0)[2:])
    m_axis_rq_tready = Signal(bool(0))
    s_axis_rc_tdata = Signal(intbv(0)[256:])
    s_axis_rc_tkeep = Signal(intbv(0)[8:])
    s_axis_rc_tlast = Signal(bool(0))
    s_axis_rc_tuser = Signal(intbv(0)[75:])
    s_axis_rc_tvalid = Signal(bool(0))
    s_axis_cq_tdata = Signal(intbv(0)[256:])
    s_axis_cq_tkeep = Signal(intbv(0)[8:])
    s_axis_cq_tlast = Signal(bool(0))
    s_axis_cq_tuser = Signal(intbv(0)[88:])
    s_axis_cq_tvalid = Signal(bool(0))
    m_axis_cc_tready = Signal(bool(0))
    pcie_tfc_nph_av = Signal(intbv(15)[4:])
    pcie_tfc_npd_av = Signal(intbv(15)[4:])
    cfg_max_payload = Signal(intbv(0)[2:])
    cfg_max_read_req = Signal(intbv(0)[3:])
    cfg_mgmt_read_data = Signal(intbv(0)[32:])
    cfg_mgmt_read_write_done = Signal(bool(0))
    cfg_interrupt_msi_enable = Signal(intbv(0)[4:])
    cfg_interrupt_msi_mmenable = Signal(intbv(0)[12:])
    cfg_interrupt_msi_mask_update = Signal(bool(0))
    cfg_interrupt_msi_data = Signal(intbv(0)[32:])
    cfg_interrupt_msi_sent = Signal(bool(0))
    cfg_interrupt_msi_fail = Signal(bool(0))
    qsfp_0_tx_clk_0 = Signal(bool(0))
    qsfp_0_tx_rst_0 = Signal(bool(0))
    qsfp_0_rx_clk_0 = Signal(bool(0))
    qsfp_0_rx_rst_0 = Signal(bool(0))
    qsfp_0_rxd_0 = Signal(intbv(0)[64:])
    qsfp_0_rxc_0 = Signal(intbv(0)[8:])
    qsfp_0_tx_clk_1 = Signal(bool(0))
    qsfp_0_tx_rst_1 = Signal(bool(0))
    qsfp_0_rx_clk_1 = Signal(bool(0))
    qsfp_0_rx_rst_1 = Signal(bool(0))
    qsfp_0_rxd_1 = Signal(intbv(0)[64:])
    qsfp_0_rxc_1 = Signal(intbv(0)[8:])
    qsfp_0_tx_clk_2 = Signal(bool(0))
    qsfp_0_tx_rst_2 = Signal(bool(0))
    qsfp_0_rx_clk_2 = Signal(bool(0))
    qsfp_0_rx_rst_2 = Signal(bool(0))
    qsfp_0_rxd_2 = Signal(intbv(0)[64:])
    qsfp_0_rxc_2 = Signal(intbv(0)[8:])
    qsfp_0_tx_clk_3 = Signal(bool(0))
    qsfp_0_tx_rst_3 = Signal(bool(0))
    qsfp_0_rx_clk_3 = Signal(bool(0))
    qsfp_0_rx_rst_3 = Signal(bool(0))
    qsfp_0_rxd_3 = Signal(intbv(0)[64:])
    qsfp_0_rxc_3 = Signal(intbv(0)[8:])
    qsfp_0_modprs_l = Signal(bool(0))
    qsfp_1_tx_clk_0 = Signal(bool(0))
    qsfp_1_tx_rst_0 = Signal(bool(0))
    qsfp_1_rx_clk_0 = Signal(bool(0))
    qsfp_1_rx_rst_0 = Signal(bool(0))
    qsfp_1_rxd_0 = Signal(intbv(0)[64:])
    qsfp_1_rxc_0 = Signal(intbv(0)[8:])
    qsfp_1_tx_clk_1 = Signal(bool(0))
    qsfp_1_tx_rst_1 = Signal(bool(0))
    qsfp_1_rx_clk_1 = Signal(bool(0))
    qsfp_1_rx_rst_1 = Signal(bool(0))
    qsfp_1_rxd_1 = Signal(intbv(0)[64:])
    qsfp_1_rxc_1 = Signal(intbv(0)[8:])
    qsfp_1_tx_clk_2 = Signal(bool(0))
    qsfp_1_tx_rst_2 = Signal(bool(0))
    qsfp_1_rx_clk_2 = Signal(bool(0))
    qsfp_1_rx_rst_2 = Signal(bool(0))
    qsfp_1_rxd_2 = Signal(intbv(0)[64:])
    qsfp_1_rxc_2 = Signal(intbv(0)[8:])
    qsfp_1_tx_clk_3 = Signal(bool(0))
    qsfp_1_tx_rst_3 = Signal(bool(0))
    qsfp_1_rx_clk_3 = Signal(bool(0))
    qsfp_1_rx_rst_3 = Signal(bool(0))
    qsfp_1_rxd_3 = Signal(intbv(0)[64:])
    qsfp_1_rxc_3 = Signal(intbv(0)[8:])
    qsfp_1_modprs_l = Signal(bool(0))
    qsfp_int_l = Signal(bool(0))
    qsfp_i2c_scl_i = Signal(bool(1))
    qsfp_i2c_sda_i = Signal(bool(1))
    eeprom_i2c_scl_i = Signal(bool(1))
    eeprom_i2c_sda_i = Signal(bool(1))

    # Outputs
    user_led_g = Signal(intbv(0)[2:])
    user_led_r = Signal(bool(0))
    front_led = Signal(intbv(0)[2:])
    m_axis_rq_tdata = Signal(intbv(0)[256:])
    m_axis_rq_tkeep = Signal(intbv(0)[8:])
    m_axis_rq_tlast = Signal(bool(0))
    m_axis_rq_tuser = Signal(intbv(0)[62:])
    m_axis_rq_tvalid = Signal(bool(0))
    s_axis_rc_tready = Signal(bool(0))
    s_axis_cq_tready = Signal(bool(0))
    m_axis_cc_tdata = Signal(intbv(0)[256:])
    m_axis_cc_tkeep = Signal(intbv(0)[8:])
    m_axis_cc_tlast = Signal(bool(0))
    m_axis_cc_tuser = Signal(intbv(0)[33:])
    m_axis_cc_tvalid = Signal(bool(0))
    status_error_cor = Signal(bool(0))
    status_error_uncor = Signal(bool(0))
    cfg_mgmt_addr = Signal(intbv(0)[10:])
    cfg_mgmt_function_number = Signal(intbv(0)[8:])
    cfg_mgmt_write = Signal(bool(0))
    cfg_mgmt_write_data = Signal(intbv(0)[32:])
    cfg_mgmt_byte_enable = Signal(intbv(0)[4:])
    cfg_mgmt_read = Signal(bool(0))
    cfg_interrupt_msi_int = Signal(intbv(0)[32:])
    cfg_interrupt_msi_pending_status = Signal(intbv(0)[32:])
    cfg_interrupt_msi_select = Signal(intbv(0)[2:])
    cfg_interrupt_msi_pending_status_function_num = Signal(intbv(0)[2:])
    cfg_interrupt_msi_pending_status_data_enable = Signal(bool(0))
    cfg_interrupt_msi_attr = Signal(intbv(0)[3:])
    cfg_interrupt_msi_tph_present = Signal(bool(0))
    cfg_interrupt_msi_tph_type = Signal(intbv(0)[2:])
    cfg_interrupt_msi_tph_st_tag = Signal(intbv(0)[8:])
    cfg_interrupt_msi_function_number = Signal(intbv(0)[8:])
    qsfp_0_txd_0 = Signal(intbv(0)[64:])
    qsfp_0_txc_0 = Signal(intbv(0)[8:])
    qsfp_0_txd_1 = Signal(intbv(0)[64:])
    qsfp_0_txc_1 = Signal(intbv(0)[8:])
    qsfp_0_txd_2 = Signal(intbv(0)[64:])
    qsfp_0_txc_2 = Signal(intbv(0)[8:])
    qsfp_0_txd_3 = Signal(intbv(0)[64:])
    qsfp_0_txc_3 = Signal(intbv(0)[8:])
    qsfp_0_sel_l = Signal(bool(1))
    qsfp_1_txd_0 = Signal(intbv(0)[64:])
    qsfp_1_txc_0 = Signal(intbv(0)[8:])
    qsfp_1_txd_1 = Signal(intbv(0)[64:])
    qsfp_1_txc_1 = Signal(intbv(0)[8:])
    qsfp_1_txd_2 = Signal(intbv(0)[64:])
    qsfp_1_txc_2 = Signal(intbv(0)[8:])
    qsfp_1_txd_3 = Signal(intbv(0)[64:])
    qsfp_1_txc_3 = Signal(intbv(0)[8:])
    qsfp_1_sel_l = Signal(bool(1))
    qsfp_reset_l = Signal(bool(1))
    qsfp_i2c_scl_o = Signal(bool(1))
    qsfp_i2c_scl_t = Signal(bool(1))
    qsfp_i2c_sda_o = Signal(bool(1))
    qsfp_i2c_sda_t = Signal(bool(1))
    eeprom_i2c_scl_o = Signal(bool(1))
    eeprom_i2c_scl_t = Signal(bool(1))
    eeprom_i2c_sda_o = Signal(bool(1))
    eeprom_i2c_sda_t = Signal(bool(1))
    eeprom_wp = Signal(bool(1))

    # sources and sinks
    qsfp_0_0_source = xgmii_ep.XGMIISource()
    qsfp_0_0_source_logic = qsfp_0_0_source.create_logic(
        qsfp_0_rx_clk_0,
        qsfp_0_rx_rst_0,
        txd=qsfp_0_rxd_0,
        txc=qsfp_0_rxc_0,
        name='qsfp_0_0_source')

    qsfp_0_0_sink = xgmii_ep.XGMIISink()
    qsfp_0_0_sink_logic = qsfp_0_0_sink.create_logic(qsfp_0_tx_clk_0,
                                                     qsfp_0_tx_rst_0,
                                                     rxd=qsfp_0_txd_0,
                                                     rxc=qsfp_0_txc_0,
                                                     name='qsfp_0_0_sink')

    qsfp_0_1_source = xgmii_ep.XGMIISource()
    qsfp_0_1_source_logic = qsfp_0_1_source.create_logic(
        qsfp_0_rx_clk_1,
        qsfp_0_rx_rst_1,
        txd=qsfp_0_rxd_1,
        txc=qsfp_0_rxc_1,
        name='qsfp_0_1_source')

    qsfp_0_1_sink = xgmii_ep.XGMIISink()
    qsfp_0_1_sink_logic = qsfp_0_1_sink.create_logic(qsfp_0_tx_clk_1,
                                                     qsfp_0_tx_rst_1,
                                                     rxd=qsfp_0_txd_1,
                                                     rxc=qsfp_0_txc_1,
                                                     name='qsfp_0_1_sink')

    qsfp_0_2_source = xgmii_ep.XGMIISource()
    qsfp_0_2_source_logic = qsfp_0_2_source.create_logic(
        qsfp_0_rx_clk_2,
        qsfp_0_rx_rst_2,
        txd=qsfp_0_rxd_2,
        txc=qsfp_0_rxc_2,
        name='qsfp_0_2_source')

    qsfp_0_2_sink = xgmii_ep.XGMIISink()
    qsfp_0_2_sink_logic = qsfp_0_2_sink.create_logic(qsfp_0_tx_clk_2,
                                                     qsfp_0_tx_rst_2,
                                                     rxd=qsfp_0_txd_2,
                                                     rxc=qsfp_0_txc_2,
                                                     name='qsfp_0_2_sink')

    qsfp_0_3_source = xgmii_ep.XGMIISource()
    qsfp_0_3_source_logic = qsfp_0_3_source.create_logic(
        qsfp_0_rx_clk_3,
        qsfp_0_rx_rst_3,
        txd=qsfp_0_rxd_3,
        txc=qsfp_0_rxc_3,
        name='qsfp_0_3_source')

    qsfp_0_3_sink = xgmii_ep.XGMIISink()
    qsfp_0_3_sink_logic = qsfp_0_3_sink.create_logic(qsfp_0_tx_clk_3,
                                                     qsfp_0_tx_rst_3,
                                                     rxd=qsfp_0_txd_3,
                                                     rxc=qsfp_0_txc_3,
                                                     name='qsfp_0_3_sink')

    qsfp_1_0_source = xgmii_ep.XGMIISource()
    qsfp_1_0_source_logic = qsfp_1_0_source.create_logic(
        qsfp_1_rx_clk_0,
        qsfp_1_rx_rst_0,
        txd=qsfp_1_rxd_0,
        txc=qsfp_1_rxc_0,
        name='qsfp_1_0_source')

    qsfp_1_0_sink = xgmii_ep.XGMIISink()
    qsfp_1_0_sink_logic = qsfp_1_0_sink.create_logic(qsfp_1_tx_clk_0,
                                                     qsfp_1_tx_rst_0,
                                                     rxd=qsfp_1_txd_0,
                                                     rxc=qsfp_1_txc_0,
                                                     name='qsfp_1_0_sink')

    qsfp_1_1_source = xgmii_ep.XGMIISource()
    qsfp_1_1_source_logic = qsfp_1_1_source.create_logic(
        qsfp_1_rx_clk_1,
        qsfp_1_rx_rst_1,
        txd=qsfp_1_rxd_1,
        txc=qsfp_1_rxc_1,
        name='qsfp_1_1_source')

    qsfp_1_1_sink = xgmii_ep.XGMIISink()
    qsfp_1_1_sink_logic = qsfp_1_1_sink.create_logic(qsfp_1_tx_clk_1,
                                                     qsfp_1_tx_rst_1,
                                                     rxd=qsfp_1_txd_1,
                                                     rxc=qsfp_1_txc_1,
                                                     name='qsfp_1_1_sink')

    qsfp_1_2_source = xgmii_ep.XGMIISource()
    qsfp_1_2_source_logic = qsfp_1_2_source.create_logic(
        qsfp_1_rx_clk_2,
        qsfp_1_rx_rst_2,
        txd=qsfp_1_rxd_2,
        txc=qsfp_1_rxc_2,
        name='qsfp_1_2_source')

    qsfp_1_2_sink = xgmii_ep.XGMIISink()
    qsfp_1_2_sink_logic = qsfp_1_2_sink.create_logic(qsfp_1_tx_clk_2,
                                                     qsfp_1_tx_rst_2,
                                                     rxd=qsfp_1_txd_2,
                                                     rxc=qsfp_1_txc_2,
                                                     name='qsfp_1_2_sink')

    qsfp_1_3_source = xgmii_ep.XGMIISource()
    qsfp_1_3_source_logic = qsfp_1_3_source.create_logic(
        qsfp_1_rx_clk_3,
        qsfp_1_rx_rst_3,
        txd=qsfp_1_rxd_3,
        txc=qsfp_1_rxc_3,
        name='qsfp_1_3_source')

    qsfp_1_3_sink = xgmii_ep.XGMIISink()
    qsfp_1_3_sink_logic = qsfp_1_3_sink.create_logic(qsfp_1_tx_clk_3,
                                                     qsfp_1_tx_rst_3,
                                                     rxd=qsfp_1_txd_3,
                                                     rxc=qsfp_1_txc_3,
                                                     name='qsfp_1_3_sink')

    # Clock and Reset Interface
    user_clk = Signal(bool(0))
    user_reset = Signal(bool(0))
    sys_clk = Signal(bool(0))
    sys_reset = Signal(bool(0))

    # PCIe devices
    rc = pcie.RootComplex()

    rc.max_payload_size = 0x1  # 256 bytes
    rc.max_read_request_size = 0x5  # 4096 bytes

    driver = mqnic.Driver(rc)

    dev = pcie_usp.UltrascalePlusPCIe()

    dev.pcie_generation = 3
    dev.pcie_link_width = 8
    dev.user_clock_frequency = 256e6

    dev.functions[0].msi_multiple_message_capable = 5

    dev.functions[0].configure_bar(0, 16 * 1024 * 1024)
    dev.functions[0].configure_bar(1, 16 * 1024 * 1024)

    rc.make_port().connect(dev)

    cq_pause = Signal(bool(0))
    cc_pause = Signal(bool(0))
    rq_pause = Signal(bool(0))
    rc_pause = Signal(bool(0))

    pcie_logic = dev.create_logic(
        # Completer reQuest Interface
        m_axis_cq_tdata=s_axis_cq_tdata,
        m_axis_cq_tuser=s_axis_cq_tuser,
        m_axis_cq_tlast=s_axis_cq_tlast,
        m_axis_cq_tkeep=s_axis_cq_tkeep,
        m_axis_cq_tvalid=s_axis_cq_tvalid,
        m_axis_cq_tready=s_axis_cq_tready,
        #pcie_cq_np_req=pcie_cq_np_req,
        pcie_cq_np_req=Signal(intbv(3)[2:]),
        #pcie_cq_np_req_count=pcie_cq_np_req_count,

        # Completer Completion Interface
        s_axis_cc_tdata=m_axis_cc_tdata,
        s_axis_cc_tuser=m_axis_cc_tuser,
        s_axis_cc_tlast=m_axis_cc_tlast,
        s_axis_cc_tkeep=m_axis_cc_tkeep,
        s_axis_cc_tvalid=m_axis_cc_tvalid,
        s_axis_cc_tready=m_axis_cc_tready,

        # Requester reQuest Interface
        s_axis_rq_tdata=m_axis_rq_tdata,
        s_axis_rq_tuser=m_axis_rq_tuser,
        s_axis_rq_tlast=m_axis_rq_tlast,
        s_axis_rq_tkeep=m_axis_rq_tkeep,
        s_axis_rq_tvalid=m_axis_rq_tvalid,
        s_axis_rq_tready=m_axis_rq_tready,
        #pcie_rq_seq_num0=pcie_rq_seq_num0,
        #pcie_rq_seq_num_vld0=pcie_rq_seq_num_vld0,
        #pcie_rq_seq_num1=pcie_rq_seq_num1,
        #pcie_rq_seq_num_vld1=pcie_rq_seq_num_vld1,
        #pcie_rq_tag0=pcie_rq_tag0,
        #pcie_rq_tag1=pcie_rq_tag1,
        #pcie_rq_tag_av=pcie_rq_tag_av,
        #pcie_rq_tag_vld0=pcie_rq_tag_vld0,
        #pcie_rq_tag_vld1=pcie_rq_tag_vld1,

        # Requester Completion Interface
        m_axis_rc_tdata=s_axis_rc_tdata,
        m_axis_rc_tuser=s_axis_rc_tuser,
        m_axis_rc_tlast=s_axis_rc_tlast,
        m_axis_rc_tkeep=s_axis_rc_tkeep,
        m_axis_rc_tvalid=s_axis_rc_tvalid,
        m_axis_rc_tready=s_axis_rc_tready,

        # Transmit Flow Control Interface
        #pcie_tfc_nph_av=pcie_tfc_nph_av,
        #pcie_tfc_npd_av=pcie_tfc_npd_av,

        # Configuration Management Interface
        cfg_mgmt_addr=cfg_mgmt_addr,
        cfg_mgmt_function_number=cfg_mgmt_function_number,
        cfg_mgmt_write=cfg_mgmt_write,
        cfg_mgmt_write_data=cfg_mgmt_write_data,
        cfg_mgmt_byte_enable=cfg_mgmt_byte_enable,
        cfg_mgmt_read=cfg_mgmt_read,
        cfg_mgmt_read_data=cfg_mgmt_read_data,
        cfg_mgmt_read_write_done=cfg_mgmt_read_write_done,
        #cfg_mgmt_debug_access=cfg_mgmt_debug_access,

        # Configuration Status Interface
        #cfg_phy_link_down=cfg_phy_link_down,
        #cfg_phy_link_status=cfg_phy_link_status,
        #cfg_negotiated_width=cfg_negotiated_width,
        #cfg_current_speed=cfg_current_speed,
        cfg_max_payload=cfg_max_payload,
        cfg_max_read_req=cfg_max_read_req,
        #cfg_function_status=cfg_function_status,
        #cfg_vf_status=cfg_vf_status,
        #cfg_function_power_state=cfg_function_power_state,
        #cfg_vf_power_state=cfg_vf_power_state,
        #cfg_link_power_state=cfg_link_power_state,
        #cfg_err_cor_out=cfg_err_cor_out,
        #cfg_err_nonfatal_out=cfg_err_nonfatal_out,
        #cfg_err_fatal_out=cfg_err_fatal_out,
        #cfg_local_err_out=cfg_local_err_out,
        #cfg_local_err_valid=cfg_local_err_valid,
        #cfg_rx_pm_state=cfg_rx_pm_state,
        #cfg_tx_pm_state=cfg_tx_pm_state,
        #cfg_ltssm_state=cfg_ltssm_state,
        #cfg_rcb_status=cfg_rcb_status,
        #cfg_obff_enable=cfg_obff_enable,
        #cfg_pl_status_change=cfg_pl_status_change,
        #cfg_tph_requester_enable=cfg_tph_requester_enable,
        #cfg_tph_st_mode=cfg_tph_st_mode,
        #cfg_vf_tph_requester_enable=cfg_vf_tph_requester_enable,
        #cfg_vf_tph_st_mode=cfg_vf_tph_st_mode,

        # Configuration Received Message Interface
        #cfg_msg_received=cfg_msg_received,
        #cfg_msg_received_data=cfg_msg_received_data,
        #cfg_msg_received_type=cfg_msg_received_type,

        # Configuration Transmit Message Interface
        #cfg_msg_transmit=cfg_msg_transmit,
        #cfg_msg_transmit_type=cfg_msg_transmit_type,
        #cfg_msg_transmit_data=cfg_msg_transmit_data,
        #cfg_msg_transmit_done=cfg_msg_transmit_done,

        # Configuration Flow Control Interface
        #cfg_fc_ph=cfg_fc_ph,
        #cfg_fc_pd=cfg_fc_pd,
        #cfg_fc_nph=cfg_fc_nph,
        #cfg_fc_npd=cfg_fc_npd,
        #cfg_fc_cplh=cfg_fc_cplh,
        #cfg_fc_cpld=cfg_fc_cpld,
        #cfg_fc_sel=cfg_fc_sel,

        # Configuration Control Interface
        #cfg_hot_reset_in=cfg_hot_reset_in,
        #cfg_hot_reset_out=cfg_hot_reset_out,
        #cfg_config_space_enable=cfg_config_space_enable,
        #cfg_dsn=cfg_dsn,
        #cfg_ds_port_number=cfg_ds_port_number,
        #cfg_ds_bus_number=cfg_ds_bus_number,
        #cfg_ds_device_number=cfg_ds_device_number,
        #cfg_ds_function_number=cfg_ds_function_number,
        #cfg_power_state_change_ack=cfg_power_state_change_ack,
        #cfg_power_state_change_interrupt=cfg_power_state_change_interrupt,
        cfg_err_cor_in=status_error_cor,
        cfg_err_uncor_in=status_error_uncor,
        #cfg_flr_done=cfg_flr_done,
        #cfg_vf_flr_done=cfg_vf_flr_done,
        #cfg_flr_in_process=cfg_flr_in_process,
        #cfg_vf_flr_in_process=cfg_vf_flr_in_process,
        #cfg_req_pm_transition_l23_ready=cfg_req_pm_transition_l23_ready,
        #cfg_link_training_enable=cfg_link_training_enable,

        # Configuration Interrupt Controller Interface
        #cfg_interrupt_int=cfg_interrupt_int,
        #cfg_interrupt_sent=cfg_interrupt_sent,
        #cfg_interrupt_pending=cfg_interrupt_pending,
        cfg_interrupt_msi_enable=cfg_interrupt_msi_enable,
        cfg_interrupt_msi_mmenable=cfg_interrupt_msi_mmenable,
        cfg_interrupt_msi_mask_update=cfg_interrupt_msi_mask_update,
        cfg_interrupt_msi_data=cfg_interrupt_msi_data,
        cfg_interrupt_msi_select=cfg_interrupt_msi_select,
        cfg_interrupt_msi_int=cfg_interrupt_msi_int,
        cfg_interrupt_msi_pending_status=cfg_interrupt_msi_pending_status,
        cfg_interrupt_msi_pending_status_data_enable=
        cfg_interrupt_msi_pending_status_data_enable,
        cfg_interrupt_msi_pending_status_function_num=
        cfg_interrupt_msi_pending_status_function_num,
        cfg_interrupt_msi_sent=cfg_interrupt_msi_sent,
        cfg_interrupt_msi_fail=cfg_interrupt_msi_fail,
        #cfg_interrupt_msix_enable=cfg_interrupt_msix_enable,
        #cfg_interrupt_msix_mask=cfg_interrupt_msix_mask,
        #cfg_interrupt_msix_vf_enable=cfg_interrupt_msix_vf_enable,
        #cfg_interrupt_msix_vf_mask=cfg_interrupt_msix_vf_mask,
        #cfg_interrupt_msix_address=cfg_interrupt_msix_address,
        #cfg_interrupt_msix_data=cfg_interrupt_msix_data,
        #cfg_interrupt_msix_int=cfg_interrupt_msix_int,
        #cfg_interrupt_msix_vec_pending=cfg_interrupt_msix_vec_pending,
        #cfg_interrupt_msix_vec_pending_status=cfg_interrupt_msix_vec_pending_status,
        cfg_interrupt_msi_attr=cfg_interrupt_msi_attr,
        cfg_interrupt_msi_tph_present=cfg_interrupt_msi_tph_present,
        cfg_interrupt_msi_tph_type=cfg_interrupt_msi_tph_type,
        cfg_interrupt_msi_tph_st_tag=cfg_interrupt_msi_tph_st_tag,
        cfg_interrupt_msi_function_number=cfg_interrupt_msi_function_number,

        # Configuration Extend Interface
        #cfg_ext_read_received=cfg_ext_read_received,
        #cfg_ext_write_received=cfg_ext_write_received,
        #cfg_ext_register_number=cfg_ext_register_number,
        #cfg_ext_function_number=cfg_ext_function_number,
        #cfg_ext_write_data=cfg_ext_write_data,
        #cfg_ext_write_byte_enable=cfg_ext_write_byte_enable,
        #cfg_ext_read_data=cfg_ext_read_data,
        #cfg_ext_read_data_valid=cfg_ext_read_data_valid,

        # Clock and Reset Interface
        user_clk=user_clk,
        user_reset=user_reset,
        sys_clk=sys_clk,
        sys_clk_gt=sys_clk,
        sys_reset=sys_reset,
        #phy_rdy_out=phy_rdy_out,
        cq_pause=cq_pause,
        cc_pause=cc_pause,
        rq_pause=rq_pause,
        rc_pause=rc_pause)

    # DUT
    if os.system(build_cmd):
        raise Exception("Error running build command")

    dut = Cosimulation(
        "vvp -m myhdl %s.vvp -lxt2" % testbench,
        clk=clk,
        rst=rst,
        current_test=current_test,
        clk_156mhz=clk_156mhz,
        rst_156mhz=rst_156mhz,
        clk_250mhz=user_clk,
        rst_250mhz=user_reset,
        user_led_g=user_led_g,
        user_led_r=user_led_r,
        front_led=front_led,
        user_sw=user_sw,
        m_axis_rq_tdata=m_axis_rq_tdata,
        m_axis_rq_tkeep=m_axis_rq_tkeep,
        m_axis_rq_tlast=m_axis_rq_tlast,
        m_axis_rq_tready=m_axis_rq_tready,
        m_axis_rq_tuser=m_axis_rq_tuser,
        m_axis_rq_tvalid=m_axis_rq_tvalid,
        s_axis_rc_tdata=s_axis_rc_tdata,
        s_axis_rc_tkeep=s_axis_rc_tkeep,
        s_axis_rc_tlast=s_axis_rc_tlast,
        s_axis_rc_tready=s_axis_rc_tready,
        s_axis_rc_tuser=s_axis_rc_tuser,
        s_axis_rc_tvalid=s_axis_rc_tvalid,
        s_axis_cq_tdata=s_axis_cq_tdata,
        s_axis_cq_tkeep=s_axis_cq_tkeep,
        s_axis_cq_tlast=s_axis_cq_tlast,
        s_axis_cq_tready=s_axis_cq_tready,
        s_axis_cq_tuser=s_axis_cq_tuser,
        s_axis_cq_tvalid=s_axis_cq_tvalid,
        m_axis_cc_tdata=m_axis_cc_tdata,
        m_axis_cc_tkeep=m_axis_cc_tkeep,
        m_axis_cc_tlast=m_axis_cc_tlast,
        m_axis_cc_tready=m_axis_cc_tready,
        m_axis_cc_tuser=m_axis_cc_tuser,
        m_axis_cc_tvalid=m_axis_cc_tvalid,
        pcie_tfc_nph_av=pcie_tfc_nph_av,
        pcie_tfc_npd_av=pcie_tfc_npd_av,
        cfg_max_payload=cfg_max_payload,
        cfg_max_read_req=cfg_max_read_req,
        cfg_mgmt_addr=cfg_mgmt_addr,
        cfg_mgmt_function_number=cfg_mgmt_function_number,
        cfg_mgmt_write=cfg_mgmt_write,
        cfg_mgmt_write_data=cfg_mgmt_write_data,
        cfg_mgmt_byte_enable=cfg_mgmt_byte_enable,
        cfg_mgmt_read=cfg_mgmt_read,
        cfg_mgmt_read_data=cfg_mgmt_read_data,
        cfg_mgmt_read_write_done=cfg_mgmt_read_write_done,
        cfg_interrupt_msi_enable=cfg_interrupt_msi_enable,
        cfg_interrupt_msi_int=cfg_interrupt_msi_int,
        cfg_interrupt_msi_sent=cfg_interrupt_msi_sent,
        cfg_interrupt_msi_fail=cfg_interrupt_msi_fail,
        cfg_interrupt_msi_mmenable=cfg_interrupt_msi_mmenable,
        cfg_interrupt_msi_pending_status=cfg_interrupt_msi_pending_status,
        cfg_interrupt_msi_mask_update=cfg_interrupt_msi_mask_update,
        cfg_interrupt_msi_select=cfg_interrupt_msi_select,
        cfg_interrupt_msi_data=cfg_interrupt_msi_data,
        cfg_interrupt_msi_pending_status_function_num=
        cfg_interrupt_msi_pending_status_function_num,
        cfg_interrupt_msi_pending_status_data_enable=
        cfg_interrupt_msi_pending_status_data_enable,
        cfg_interrupt_msi_attr=cfg_interrupt_msi_attr,
        cfg_interrupt_msi_tph_present=cfg_interrupt_msi_tph_present,
        cfg_interrupt_msi_tph_type=cfg_interrupt_msi_tph_type,
        cfg_interrupt_msi_tph_st_tag=cfg_interrupt_msi_tph_st_tag,
        cfg_interrupt_msi_function_number=cfg_interrupt_msi_function_number,
        status_error_cor=status_error_cor,
        status_error_uncor=status_error_uncor,
        qsfp_0_tx_clk_0=qsfp_0_tx_clk_0,
        qsfp_0_tx_rst_0=qsfp_0_tx_rst_0,
        qsfp_0_txd_0=qsfp_0_txd_0,
        qsfp_0_txc_0=qsfp_0_txc_0,
        qsfp_0_rx_clk_0=qsfp_0_rx_clk_0,
        qsfp_0_rx_rst_0=qsfp_0_rx_rst_0,
        qsfp_0_rxd_0=qsfp_0_rxd_0,
        qsfp_0_rxc_0=qsfp_0_rxc_0,
        qsfp_0_tx_clk_1=qsfp_0_tx_clk_1,
        qsfp_0_tx_rst_1=qsfp_0_tx_rst_1,
        qsfp_0_txd_1=qsfp_0_txd_1,
        qsfp_0_txc_1=qsfp_0_txc_1,
        qsfp_0_rx_clk_1=qsfp_0_rx_clk_1,
        qsfp_0_rx_rst_1=qsfp_0_rx_rst_1,
        qsfp_0_rxd_1=qsfp_0_rxd_1,
        qsfp_0_rxc_1=qsfp_0_rxc_1,
        qsfp_0_tx_clk_2=qsfp_0_tx_clk_2,
        qsfp_0_tx_rst_2=qsfp_0_tx_rst_2,
        qsfp_0_txd_2=qsfp_0_txd_2,
        qsfp_0_txc_2=qsfp_0_txc_2,
        qsfp_0_rx_clk_2=qsfp_0_rx_clk_2,
        qsfp_0_rx_rst_2=qsfp_0_rx_rst_2,
        qsfp_0_rxd_2=qsfp_0_rxd_2,
        qsfp_0_rxc_2=qsfp_0_rxc_2,
        qsfp_0_tx_clk_3=qsfp_0_tx_clk_3,
        qsfp_0_tx_rst_3=qsfp_0_tx_rst_3,
        qsfp_0_txd_3=qsfp_0_txd_3,
        qsfp_0_txc_3=qsfp_0_txc_3,
        qsfp_0_rx_clk_3=qsfp_0_rx_clk_3,
        qsfp_0_rx_rst_3=qsfp_0_rx_rst_3,
        qsfp_0_rxd_3=qsfp_0_rxd_3,
        qsfp_0_rxc_3=qsfp_0_rxc_3,
        qsfp_0_modprs_l=qsfp_0_modprs_l,
        qsfp_0_sel_l=qsfp_0_sel_l,
        qsfp_1_tx_clk_0=qsfp_1_tx_clk_0,
        qsfp_1_tx_rst_0=qsfp_1_tx_rst_0,
        qsfp_1_txd_0=qsfp_1_txd_0,
        qsfp_1_txc_0=qsfp_1_txc_0,
        qsfp_1_rx_clk_0=qsfp_1_rx_clk_0,
        qsfp_1_rx_rst_0=qsfp_1_rx_rst_0,
        qsfp_1_rxd_0=qsfp_1_rxd_0,
        qsfp_1_rxc_0=qsfp_1_rxc_0,
        qsfp_1_tx_clk_1=qsfp_1_tx_clk_1,
        qsfp_1_tx_rst_1=qsfp_1_tx_rst_1,
        qsfp_1_txd_1=qsfp_1_txd_1,
        qsfp_1_txc_1=qsfp_1_txc_1,
        qsfp_1_rx_clk_1=qsfp_1_rx_clk_1,
        qsfp_1_rx_rst_1=qsfp_1_rx_rst_1,
        qsfp_1_rxd_1=qsfp_1_rxd_1,
        qsfp_1_rxc_1=qsfp_1_rxc_1,
        qsfp_1_tx_clk_2=qsfp_1_tx_clk_2,
        qsfp_1_tx_rst_2=qsfp_1_tx_rst_2,
        qsfp_1_txd_2=qsfp_1_txd_2,
        qsfp_1_txc_2=qsfp_1_txc_2,
        qsfp_1_rx_clk_2=qsfp_1_rx_clk_2,
        qsfp_1_rx_rst_2=qsfp_1_rx_rst_2,
        qsfp_1_rxd_2=qsfp_1_rxd_2,
        qsfp_1_rxc_2=qsfp_1_rxc_2,
        qsfp_1_tx_clk_3=qsfp_1_tx_clk_3,
        qsfp_1_tx_rst_3=qsfp_1_tx_rst_3,
        qsfp_1_txd_3=qsfp_1_txd_3,
        qsfp_1_txc_3=qsfp_1_txc_3,
        qsfp_1_rx_clk_3=qsfp_1_rx_clk_3,
        qsfp_1_rx_rst_3=qsfp_1_rx_rst_3,
        qsfp_1_rxd_3=qsfp_1_rxd_3,
        qsfp_1_rxc_3=qsfp_1_rxc_3,
        qsfp_1_modprs_l=qsfp_1_modprs_l,
        qsfp_1_sel_l=qsfp_1_sel_l,
        qsfp_reset_l=qsfp_reset_l,
        qsfp_int_l=qsfp_int_l,
        qsfp_i2c_scl_i=qsfp_i2c_scl_i,
        qsfp_i2c_scl_o=qsfp_i2c_scl_o,
        qsfp_i2c_scl_t=qsfp_i2c_scl_t,
        qsfp_i2c_sda_i=qsfp_i2c_sda_i,
        qsfp_i2c_sda_o=qsfp_i2c_sda_o,
        qsfp_i2c_sda_t=qsfp_i2c_sda_t,
        eeprom_i2c_scl_i=eeprom_i2c_scl_i,
        eeprom_i2c_scl_o=eeprom_i2c_scl_o,
        eeprom_i2c_scl_t=eeprom_i2c_scl_t,
        eeprom_i2c_sda_i=eeprom_i2c_sda_i,
        eeprom_i2c_sda_o=eeprom_i2c_sda_o,
        eeprom_i2c_sda_t=eeprom_i2c_sda_t,
        eeprom_wp=eeprom_wp)

    @always(delay(5))
    def clkgen():
        clk.next = not clk

    @always(delay(3))
    def qsfp_clkgen():
        qsfp_0_tx_clk_0.next = not qsfp_0_tx_clk_0
        qsfp_0_rx_clk_0.next = not qsfp_0_rx_clk_0
        qsfp_0_tx_clk_1.next = not qsfp_0_tx_clk_1
        qsfp_0_rx_clk_1.next = not qsfp_0_rx_clk_1
        qsfp_0_tx_clk_2.next = not qsfp_0_tx_clk_2
        qsfp_0_rx_clk_2.next = not qsfp_0_rx_clk_2
        qsfp_0_tx_clk_3.next = not qsfp_0_tx_clk_3
        qsfp_0_rx_clk_3.next = not qsfp_0_rx_clk_3
        qsfp_1_tx_clk_0.next = not qsfp_1_tx_clk_0
        qsfp_1_rx_clk_0.next = not qsfp_1_rx_clk_0
        qsfp_1_tx_clk_1.next = not qsfp_1_tx_clk_1
        qsfp_1_rx_clk_1.next = not qsfp_1_rx_clk_1
        qsfp_1_tx_clk_2.next = not qsfp_1_tx_clk_2
        qsfp_1_rx_clk_2.next = not qsfp_1_rx_clk_2
        qsfp_1_tx_clk_3.next = not qsfp_1_tx_clk_3
        qsfp_1_rx_clk_3.next = not qsfp_1_rx_clk_3

    @always_comb
    def clk_logic():
        sys_clk.next = clk
        sys_reset.next = not rst

    loopback_enable = Signal(bool(0))

    @instance
    def loopback():
        while True:

            yield clk.posedge

            if loopback_enable:
                if not qsfp_0_0_sink.empty():
                    pkt = qsfp_0_0_sink.recv()
                    qsfp_0_0_source.send(pkt)
                if not qsfp_0_1_sink.empty():
                    pkt = qsfp_0_1_sink.recv()
                    qsfp_0_1_source.send(pkt)
                if not qsfp_0_2_sink.empty():
                    pkt = qsfp_0_2_sink.recv()
                    qsfp_0_2_source.send(pkt)
                if not qsfp_0_3_sink.empty():
                    pkt = qsfp_0_3_sink.recv()
                    qsfp_0_3_source.send(pkt)
                if not qsfp_1_0_sink.empty():
                    pkt = qsfp_1_0_sink.recv()
                    qsfp_1_0_source.send(pkt)
                if not qsfp_1_1_sink.empty():
                    pkt = qsfp_1_1_sink.recv()
                    qsfp_1_1_source.send(pkt)
                if not qsfp_1_2_sink.empty():
                    pkt = qsfp_1_2_sink.recv()
                    qsfp_1_2_source.send(pkt)
                if not qsfp_1_3_sink.empty():
                    pkt = qsfp_1_3_sink.recv()
                    qsfp_1_3_source.send(pkt)

    @instance
    def check():
        yield delay(100)
        yield clk.posedge
        rst.next = 1
        qsfp_0_tx_rst_0.next = 1
        qsfp_0_rx_rst_0.next = 1
        qsfp_0_tx_rst_1.next = 1
        qsfp_0_rx_rst_1.next = 1
        qsfp_0_tx_rst_2.next = 1
        qsfp_0_rx_rst_2.next = 1
        qsfp_0_tx_rst_3.next = 1
        qsfp_0_rx_rst_3.next = 1
        qsfp_1_tx_rst_0.next = 1
        qsfp_1_rx_rst_0.next = 1
        qsfp_1_tx_rst_1.next = 1
        qsfp_1_rx_rst_1.next = 1
        qsfp_1_tx_rst_2.next = 1
        qsfp_1_rx_rst_2.next = 1
        qsfp_1_tx_rst_3.next = 1
        qsfp_1_rx_rst_3.next = 1
        yield clk.posedge
        yield delay(100)
        rst.next = 0
        qsfp_0_tx_rst_0.next = 0
        qsfp_0_rx_rst_0.next = 0
        qsfp_0_tx_rst_1.next = 0
        qsfp_0_rx_rst_1.next = 0
        qsfp_0_tx_rst_2.next = 0
        qsfp_0_rx_rst_2.next = 0
        qsfp_0_tx_rst_3.next = 0
        qsfp_0_rx_rst_3.next = 0
        qsfp_1_tx_rst_0.next = 0
        qsfp_1_rx_rst_0.next = 0
        qsfp_1_tx_rst_1.next = 0
        qsfp_1_rx_rst_1.next = 0
        qsfp_1_tx_rst_2.next = 0
        qsfp_1_rx_rst_2.next = 0
        qsfp_1_tx_rst_3.next = 0
        qsfp_1_rx_rst_3.next = 0
        yield clk.posedge
        yield delay(100)
        yield clk.posedge

        # testbench stimulus

        current_tag = 1

        yield clk.posedge
        print("test 1: enumeration")
        current_test.next = 1

        yield rc.enumerate(enable_bus_mastering=True, configure_msi=True)

        dev_pf0_bar0 = dev.functions[0].bar[0] & 0xfffffffc
        dev_pf0_bar1 = dev.functions[0].bar[1] & 0xfffffffc

        yield delay(100)

        yield clk.posedge
        print("test 2: init NIC")
        current_test.next = 2

        #data = yield from rc.mem_read(dev_pf0_bar0+0x20000+0x10, 4);
        #print(data)

        #yield delay(1000)

        #raise StopSimulation

        yield from driver.init_dev(dev.functions[0].get_id())
        yield from driver.interfaces[0].open()
        #yield from driver.interfaces[1].open()

        # enable queues
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x0200,
            0xffffffff)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x0300,
            0xffffffff)

        yield from rc.mem_read(driver.hw_addr,
                               4)  # wait for all writes to complete

        yield delay(100)

        yield clk.posedge
        print("test 3: send and receive a packet")
        current_test.next = 3

        # test bad packet
        #qsfp_0_0_source.send(b'\x55\x55\x55\x55\x55\xd5'+bytearray(range(128)))

        data = bytearray([x % 256 for x in range(1024)])

        yield from driver.interfaces[0].start_xmit(data, 0)

        yield qsfp_0_0_sink.wait()

        pkt = qsfp_0_0_sink.recv()
        print(pkt)

        qsfp_0_0_source.send(pkt)

        yield driver.interfaces[0].wait()

        pkt = driver.interfaces[0].recv()

        print(pkt)
        assert frame_checksum(pkt.data) == pkt.rx_checksum

        # yield from driver.interfaces[1].start_xmit(data, 0)

        # yield qsfp_1_0_sink.wait()

        # pkt = qsfp_1_0_sink.recv()
        # print(pkt)

        # qsfp_1_0_source.send(pkt)

        # yield driver.interfaces[1].wait()

        # pkt = driver.interfaces[1].recv()

        # print(pkt)
        # assert frame_checksum(pkt.data) == pkt.rx_checksum

        yield delay(100)

        yield clk.posedge
        print("test 4: multiple small packets")
        current_test.next = 4

        count = 64

        pkts = [
            bytearray([(x + k) % 256 for x in range(64)]) for k in range(count)
        ]

        loopback_enable.next = True

        for p in pkts:
            yield from driver.interfaces[0].start_xmit(p, 0)

        for k in range(count):
            pkt = driver.interfaces[0].recv()

            if not pkt:
                yield driver.interfaces[0].wait()
                pkt = driver.interfaces[0].recv()

            print(pkt)
            assert pkt.data == pkts[k]
            assert frame_checksum(pkt.data) == pkt.rx_checksum

        loopback_enable.next = False

        yield delay(100)

        yield clk.posedge
        print("test 5: multiple large packets")
        current_test.next = 5

        count = 64

        pkts = [
            bytearray([(x + k) % 256 for x in range(1514)])
            for k in range(count)
        ]

        loopback_enable.next = True

        for p in pkts:
            yield from driver.interfaces[0].start_xmit(p, 0)

        for k in range(count):
            pkt = driver.interfaces[0].recv()

            if not pkt:
                yield driver.interfaces[0].wait()
                pkt = driver.interfaces[0].recv()

            print(pkt)
            assert pkt.data == pkts[k]
            assert frame_checksum(pkt.data) == pkt.rx_checksum

        loopback_enable.next = False

        yield delay(1000)

        yield clk.posedge
        print("test 6: TDMA")
        current_test.next = 6

        count = 16

        pkts = [
            bytearray([(x + k) % 256 for x in range(1514)])
            for k in range(count)
        ]

        loopback_enable.next = True

        # configure TDMA

        # configure TDMA scheduler
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00120,
            0)  # schedule period fns
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00124,
            40000)  # schedule period ns
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00128,
            0)  # schedule period sec (low)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x0012c,
            0)  # schedule period sec (high)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00130,
            0)  # timeslot period fns
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00134,
            10000)  # timeslot period ns
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00138,
            0)  # timeslot period sec (low)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x0013c,
            0)  # timeslot period sec (high)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00140,
            0)  # active period fns
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00144,
            5000)  # active period ns
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00148,
            0)  # active period sec (low)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x0014c,
            0)  # active period sec (high)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00110,
            0)  # schedule start fns
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00114,
            200000)  # schedule start ns
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00118,
            0)  # schedule start sec (low)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x0011c,
            0)  # schedule start sec (high)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00100,
            0x00000001)

        # enable queues
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00200,
            0xffffffff)
        # disable global enable
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x00300,
            0x00000000)

        # configure slots
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x10000,
            0x00000001)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x10100,
            0x00000002)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x10200,
            0x00000004)
        yield from rc.mem_write_dword(
            driver.interfaces[0].ports[0].schedulers[0].hw_addr + 0x10300,
            0x00000008)

        yield from rc.mem_read(driver.hw_addr,
                               4)  # wait for all writes to complete

        # send packets
        for k in range(count):
            yield from driver.interfaces[0].start_xmit(pkts[k], k % 4)

        for k in range(count):
            pkt = driver.interfaces[0].recv()

            if not pkt:
                yield driver.interfaces[0].wait()
                pkt = driver.interfaces[0].recv()

            print(pkt)
            #assert pkt.data == pkts[k]
            #assert frame_checksum(pkt.data) == pkt.rx_checksum

        loopback_enable.next = False

        yield delay(100)

        raise StopSimulation

    return instances()
Esempio n. 7
0
def bench():

    # Parameters
    AXIS_PCIE_DATA_WIDTH = 512
    AXIS_PCIE_KEEP_WIDTH = (AXIS_PCIE_DATA_WIDTH / 32)
    AXIS_PCIE_CQ_USER_WIDTH = 183
    AXIS_PCIE_CC_USER_WIDTH = 81
    AXI_DATA_WIDTH = AXIS_PCIE_DATA_WIDTH
    AXI_ADDR_WIDTH = 64
    AXI_STRB_WIDTH = (AXI_DATA_WIDTH / 8)
    AXI_ID_WIDTH = 8
    AXI_MAX_BURST_LEN = 256

    # Inputs
    clk = Signal(bool(0))
    rst = Signal(bool(0))
    current_test = Signal(intbv(0)[8:])

    s_axis_cq_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    s_axis_cq_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    s_axis_cq_tvalid = Signal(bool(0))
    s_axis_cq_tlast = Signal(bool(0))
    s_axis_cq_tuser = Signal(intbv(0)[AXIS_PCIE_CQ_USER_WIDTH:])
    m_axis_cc_tready = Signal(bool(0))
    m_axi_arready = Signal(bool(0))
    m_axi_rid = Signal(intbv(0)[AXI_ID_WIDTH:])
    m_axi_rdata = Signal(intbv(0)[AXI_DATA_WIDTH:])
    m_axi_rresp = Signal(intbv(0)[2:])
    m_axi_rlast = Signal(bool(0))
    m_axi_rvalid = Signal(bool(0))
    completer_id = Signal(intbv(0)[16:])
    completer_id_enable = Signal(bool(0))
    max_payload_size = Signal(intbv(0)[3:])

    # Outputs
    s_axis_cq_tready = Signal(bool(0))
    m_axis_cc_tdata = Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:])
    m_axis_cc_tkeep = Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:])
    m_axis_cc_tvalid = Signal(bool(0))
    m_axis_cc_tlast = Signal(bool(0))
    m_axis_cc_tuser = Signal(intbv(0)[AXIS_PCIE_CC_USER_WIDTH:])
    m_axi_arid = Signal(intbv(0)[AXI_ID_WIDTH:])
    m_axi_araddr = Signal(intbv(0)[AXI_ADDR_WIDTH:])
    m_axi_arlen = Signal(intbv(0)[8:])
    m_axi_arsize = Signal(intbv(6)[3:])
    m_axi_arburst = Signal(intbv(1)[2:])
    m_axi_arlock = Signal(bool(0))
    m_axi_arcache = Signal(intbv(3)[4:])
    m_axi_arprot = Signal(intbv(2)[3:])
    m_axi_arvalid = Signal(bool(0))
    m_axi_rready = Signal(bool(0))
    status_error_cor = Signal(bool(0))
    status_error_uncor = Signal(bool(0))

    # Clock and Reset Interface
    user_clk = Signal(bool(0))
    user_reset = Signal(bool(0))
    sys_clk = Signal(bool(0))
    sys_reset = Signal(bool(0))

    # AXI4 RAM model
    axi_ram_inst = axi.AXIRam(2**16)

    axi_ram_port0 = axi_ram_inst.create_port(user_clk,
                                             s_axi_arid=m_axi_arid,
                                             s_axi_araddr=m_axi_araddr,
                                             s_axi_arlen=m_axi_arlen,
                                             s_axi_arsize=m_axi_arsize,
                                             s_axi_arburst=m_axi_arburst,
                                             s_axi_arlock=m_axi_arlock,
                                             s_axi_arcache=m_axi_arcache,
                                             s_axi_arprot=m_axi_arprot,
                                             s_axi_arvalid=m_axi_arvalid,
                                             s_axi_arready=m_axi_arready,
                                             s_axi_rid=m_axi_rid,
                                             s_axi_rdata=m_axi_rdata,
                                             s_axi_rresp=m_axi_rresp,
                                             s_axi_rlast=m_axi_rlast,
                                             s_axi_rvalid=m_axi_rvalid,
                                             s_axi_rready=m_axi_rready,
                                             name='port0')

    # PCIe devices
    rc = pcie.RootComplex()

    dev = pcie_usp.UltrascalePlusPCIe()

    dev.pcie_generation = 3
    dev.pcie_link_width = 16
    dev.user_clock_frequency = 250e6

    dev.functions[0].configure_bar(0, 16 * 1024 * 1024)
    dev.functions[0].configure_bar(1, 32, io=True)

    rc.make_port().connect(dev)

    cq_pause = Signal(bool(0))
    cc_pause = Signal(bool(0))
    rq_pause = Signal(bool(0))
    rc_pause = Signal(bool(0))

    pcie_logic = dev.create_logic(
        # Completer reQuest Interface
        m_axis_cq_tdata=s_axis_cq_tdata,
        m_axis_cq_tuser=s_axis_cq_tuser,
        m_axis_cq_tlast=s_axis_cq_tlast,
        m_axis_cq_tkeep=s_axis_cq_tkeep,
        m_axis_cq_tvalid=s_axis_cq_tvalid,
        m_axis_cq_tready=s_axis_cq_tready,
        #pcie_cq_np_req=pcie_cq_np_req,
        #pcie_cq_np_req_count=pcie_cq_np_req_count,

        # Completer Completion Interface
        s_axis_cc_tdata=m_axis_cc_tdata,
        s_axis_cc_tuser=m_axis_cc_tuser,
        s_axis_cc_tlast=m_axis_cc_tlast,
        s_axis_cc_tkeep=m_axis_cc_tkeep,
        s_axis_cc_tvalid=m_axis_cc_tvalid,
        s_axis_cc_tready=m_axis_cc_tready,

        # Requester reQuest Interface
        s_axis_rq_tdata=Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:]),
        s_axis_rq_tuser=Signal(intbv(0)[137:]),
        s_axis_rq_tlast=Signal(bool(0)),
        s_axis_rq_tkeep=Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:]),
        s_axis_rq_tvalid=Signal(bool(0)),
        s_axis_rq_tready=Signal(bool(1)),
        # pcie_rq_seq_num0=pcie_rq_seq_num0,
        # pcie_rq_seq_num_vld0=pcie_rq_seq_num_vld0,
        # pcie_rq_seq_num1=pcie_rq_seq_num1,
        # pcie_rq_seq_num_vld1=pcie_rq_seq_num_vld1,
        # pcie_rq_tag0=pcie_rq_tag0,
        # pcie_rq_tag1=pcie_rq_tag1,
        # pcie_rq_tag_av=pcie_rq_tag_av,
        # pcie_rq_tag_vld0=pcie_rq_tag_vld0,
        # pcie_rq_tag_vld1=pcie_rq_tag_vld1,

        # Requester Completion Interface
        m_axis_rc_tdata=Signal(intbv(0)[AXIS_PCIE_DATA_WIDTH:]),
        m_axis_rc_tuser=Signal(intbv(0)[161:]),
        m_axis_rc_tlast=Signal(bool(0)),
        m_axis_rc_tkeep=Signal(intbv(0)[AXIS_PCIE_KEEP_WIDTH:]),
        m_axis_rc_tvalid=Signal(bool(0)),
        m_axis_rc_tready=Signal(bool(0)),

        # Transmit Flow Control Interface
        # pcie_tfc_nph_av=pcie_tfc_nph_av,
        # pcie_tfc_npd_av=pcie_tfc_npd_av,

        # Configuration Control Interface
        # cfg_hot_reset_in=cfg_hot_reset_in,
        # cfg_hot_reset_out=cfg_hot_reset_out,
        # cfg_config_space_enable=cfg_config_space_enable,
        # cfg_dsn=cfg_dsn,
        # cfg_ds_port_number=cfg_ds_port_number,
        # cfg_ds_bus_number=cfg_ds_bus_number,
        # cfg_ds_device_number=cfg_ds_device_number,
        # cfg_ds_function_number=cfg_ds_function_number,
        # cfg_power_state_change_ack=cfg_power_state_change_ack,
        # cfg_power_state_change_interrupt=cfg_power_state_change_interrupt,
        # cfg_err_cor_in=cfg_err_cor_in,
        # cfg_err_uncor_in=cfg_err_uncor_in,
        # cfg_flr_done=cfg_flr_done,
        # cfg_vf_flr_done=cfg_vf_flr_done,
        # cfg_flr_in_process=cfg_flr_in_process,
        # cfg_vf_flr_in_process=cfg_vf_flr_in_process,
        # cfg_req_pm_transition_l23_ready=cfg_req_pm_transition_l23_ready,
        # cfg_link_training_enable=cfg_link_training_enable,

        # Clock and Reset Interface
        user_clk=user_clk,
        user_reset=user_reset,
        #user_lnk_up=user_lnk_up,
        sys_clk=sys_clk,
        sys_clk_gt=sys_clk,
        sys_reset=sys_reset,
        cq_pause=cq_pause,
        cc_pause=cc_pause,
        rq_pause=rq_pause,
        rc_pause=rc_pause)

    # DUT
    if os.system(build_cmd):
        raise Exception("Error running build command")

    dut = Cosimulation("vvp -m myhdl %s.vvp -lxt2" % testbench,
                       clk=user_clk,
                       rst=user_reset,
                       current_test=current_test,
                       s_axis_cq_tdata=s_axis_cq_tdata,
                       s_axis_cq_tkeep=s_axis_cq_tkeep,
                       s_axis_cq_tvalid=s_axis_cq_tvalid,
                       s_axis_cq_tready=s_axis_cq_tready,
                       s_axis_cq_tlast=s_axis_cq_tlast,
                       s_axis_cq_tuser=s_axis_cq_tuser,
                       m_axis_cc_tdata=m_axis_cc_tdata,
                       m_axis_cc_tkeep=m_axis_cc_tkeep,
                       m_axis_cc_tvalid=m_axis_cc_tvalid,
                       m_axis_cc_tready=m_axis_cc_tready,
                       m_axis_cc_tlast=m_axis_cc_tlast,
                       m_axis_cc_tuser=m_axis_cc_tuser,
                       m_axi_arid=m_axi_arid,
                       m_axi_araddr=m_axi_araddr,
                       m_axi_arlen=m_axi_arlen,
                       m_axi_arsize=m_axi_arsize,
                       m_axi_arburst=m_axi_arburst,
                       m_axi_arlock=m_axi_arlock,
                       m_axi_arcache=m_axi_arcache,
                       m_axi_arprot=m_axi_arprot,
                       m_axi_arvalid=m_axi_arvalid,
                       m_axi_arready=m_axi_arready,
                       m_axi_rid=m_axi_rid,
                       m_axi_rdata=m_axi_rdata,
                       m_axi_rresp=m_axi_rresp,
                       m_axi_rlast=m_axi_rlast,
                       m_axi_rvalid=m_axi_rvalid,
                       m_axi_rready=m_axi_rready,
                       completer_id=completer_id,
                       completer_id_enable=completer_id_enable,
                       max_payload_size=max_payload_size,
                       status_error_cor=status_error_cor,
                       status_error_uncor=status_error_uncor)

    @always(delay(4))
    def clkgen():
        clk.next = not clk

    @always_comb
    def clk_logic():
        sys_clk.next = clk
        sys_reset.next = not rst

    status_error_cor_asserted = Signal(bool(0))
    status_error_uncor_asserted = Signal(bool(0))

    @always(user_clk.posedge)
    def monitor():
        if (status_error_cor):
            status_error_cor_asserted.next = 1
        if (status_error_uncor):
            status_error_uncor_asserted.next = 1

    cq_pause_toggle = Signal(bool(0))
    cc_pause_toggle = Signal(bool(0))
    rq_pause_toggle = Signal(bool(0))
    rc_pause_toggle = Signal(bool(0))

    @instance
    def pause_toggle():
        while True:
            if (cq_pause_toggle or cc_pause_toggle or rq_pause_toggle
                    or rc_pause_toggle):
                cq_pause.next = cq_pause_toggle
                cc_pause.next = cc_pause_toggle
                rq_pause.next = rq_pause_toggle
                rc_pause.next = rc_pause_toggle

                yield user_clk.posedge
                yield user_clk.posedge
                yield user_clk.posedge

                cq_pause.next = 0
                cc_pause.next = 0
                rq_pause.next = 0
                rc_pause.next = 0

            yield user_clk.posedge

    @instance
    def check():
        yield delay(100)
        yield clk.posedge
        rst.next = 1
        yield clk.posedge
        rst.next = 0
        yield clk.posedge
        yield delay(100)
        yield clk.posedge

        # testbench stimulus

        max_payload_size.next = 0

        yield user_clk.posedge
        print("test 1: enumeration")
        current_test.next = 1

        yield rc.enumerate()

        dev_bar0 = rc.tree[0][0].bar[0]
        dev_bar1 = rc.tree[0][0].bar[1]

        yield delay(100)

        yield clk.posedge
        print("test 2: memory read")
        current_test.next = 2

        pcie_addr = 0x00000000
        test_data = b'\x11\x22\x33\x44'

        axi_ram_inst.write_mem(pcie_addr, test_data)

        data = axi_ram_inst.read_mem(0, 32)
        for i in range(0, len(data), 16):
            print(" ".join(
                ("{:02x}".format(c) for c in bytearray(data[i:i + 16]))))

        val = yield from rc.mem_read(dev_bar0 + pcie_addr, len(test_data),
                                     1000)

        print(val)

        assert val == test_data

        assert not status_error_cor_asserted
        assert not status_error_uncor_asserted

        yield delay(100)

        yield user_clk.posedge
        print("test 3: various reads")
        current_test.next = 3

        for length in list(range(1, 66)) + [1024]:
            for pcie_offset in list(range(8, 73)) + list(range(
                    4096 - 64, 4096)):
                for pause in [False, True]:
                    print("length %d, pcie_offset %d" % (length, pcie_offset))
                    #pcie_addr = length * 0x100000000 + pcie_offset * 0x10000 + offset
                    pcie_addr = pcie_offset
                    test_data = bytearray([x % 256 for x in range(length)])

                    axi_ram_inst.write_mem(pcie_addr & 0xffff80,
                                           b'\x55' * (len(test_data) + 256))
                    axi_ram_inst.write_mem(pcie_addr, test_data)

                    data = axi_ram_inst.read_mem(pcie_addr & 0xfffff0, 64)
                    for i in range(0, len(data), 16):
                        print(" ".join(("{:02x}".format(c)
                                        for c in bytearray(data[i:i + 16]))))

                    cq_pause_toggle.next = pause
                    cc_pause_toggle.next = pause

                    val = yield from rc.mem_read(dev_bar0 + pcie_addr,
                                                 len(test_data), 1000)

                    cq_pause_toggle.next = 0
                    cc_pause_toggle.next = 0

                    print(val)

                    assert val == test_data

                    assert not status_error_cor_asserted
                    assert not status_error_uncor_asserted

                    yield delay(100)

        yield clk.posedge
        print("test 4: bad requests")
        current_test.next = 4

        yield from rc.mem_write(dev_bar0, b'\x11\x22\x33\x44')

        yield delay(100)

        assert not status_error_cor_asserted
        assert status_error_uncor_asserted

        status_error_cor_asserted.next = False
        status_error_uncor_asserted.next = False

        try:
            yield from rc.io_write(dev_bar1, b'\x11\x22\x33\x44')
        except:
            print("Caught unsuccessful completion exception")
            pass
        else:
            assert False

        assert status_error_cor_asserted
        assert not status_error_uncor_asserted

        status_error_cor_asserted.next = False
        status_error_uncor_asserted.next = False

        yield delay(100)

        raise StopSimulation

    return instances()