Ejemplo n.º 1
0
def bench():

    # Parameters
    ENABLE_PADDING = 1
    MIN_FRAME_LENGTH = 64

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

    input_axis_tdata = Signal(intbv(0)[8:])
    input_axis_tvalid = Signal(bool(0))
    input_axis_tlast = Signal(bool(0))
    input_axis_tuser = Signal(bool(0))
    ifg_delay = Signal(intbv(0)[8:])

    # Outputs
    input_axis_tready = Signal(bool(0))
    gmii_txd = Signal(intbv(0)[8:])
    gmii_tx_en = Signal(bool(0))
    gmii_tx_er = Signal(bool(0))

    # sources and sinks
    source_queue = Queue()
    source_pause = Signal(bool(0))
    sink_queue = Queue()

    source = axis_ep.AXIStreamSource(clk,
                                     rst,
                                     tdata=input_axis_tdata,
                                     tvalid=input_axis_tvalid,
                                     tready=input_axis_tready,
                                     tlast=input_axis_tlast,
                                     tuser=input_axis_tuser,
                                     fifo=source_queue,
                                     pause=source_pause,
                                     name='source')

    sink = gmii_ep.GMIISink(clk,
                            rst,
                            rxd=gmii_txd,
                            rx_dv=gmii_tx_en,
                            rx_er=gmii_tx_er,
                            fifo=sink_queue,
                            name='sink')

    # DUT
    dut = dut_eth_mac_1g_tx(clk,
                            rst,
                            current_test,

                            input_axis_tdata,
                            input_axis_tvalid,
                            input_axis_tready,
                            input_axis_tlast,
                            input_axis_tuser,

                            gmii_txd,
                            gmii_tx_en,
                            gmii_tx_er,

                            ifg_delay)

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

    @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

        ifg_delay.next = 12

        # testbench stimulus

        for payload_len in list(range(1,18))+list(range(64,82)):
            yield clk.posedge
            print("test 1: test packet, length %d" % payload_len)
            current_test.next = 1

            test_frame = eth_ep.EthFrame()
            test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame.eth_src_mac = 0x5A5152535455
            test_frame.eth_type = 0x8000
            test_frame.payload = bytearray(range(payload_len))
            test_frame.update_fcs()

            axis_frame = test_frame.build_axis()

            source_queue.put(axis_frame)
            yield clk.posedge
            yield clk.posedge

            while gmii_tx_en or input_axis_tvalid:
                yield clk.posedge

            yield clk.posedge
            yield clk.posedge
            yield clk.posedge

            rx_frame = None
            if not sink_queue.empty():
                rx_frame = sink_queue.get()

            assert rx_frame.data[0:8] == bytearray(b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == max(payload_len, 46)
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame.eth_src_mac
            assert eth_frame.eth_type == test_frame.eth_type
            assert eth_frame.payload.data.index(test_frame.payload.data) == 0

            assert sink_queue.empty()

            yield delay(100)

            yield clk.posedge
            print("test 2: back-to-back packets, length %d" % payload_len)
            current_test.next = 2

            test_frame1 = eth_ep.EthFrame()
            test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame1.eth_src_mac = 0x5A5152535455
            test_frame1.eth_type = 0x8000
            test_frame1.payload = bytearray(range(payload_len))
            test_frame1.update_fcs()
            test_frame2 = eth_ep.EthFrame()
            test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame2.eth_src_mac = 0x5A5152535455
            test_frame2.eth_type = 0x8000
            test_frame2.payload = bytearray(range(payload_len))
            test_frame2.update_fcs()

            axis_frame1 = test_frame1.build_axis()
            axis_frame2 = test_frame2.build_axis()

            source_queue.put(axis_frame1)
            source_queue.put(axis_frame2)
            yield clk.posedge
            yield clk.posedge

            while gmii_tx_en or input_axis_tvalid:
                yield clk.posedge

            yield clk.posedge
            yield clk.posedge
            yield clk.posedge

            rx_frame = None
            if not sink_queue.empty():
                rx_frame = sink_queue.get()

            assert rx_frame.data[0:8] == bytearray(b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == max(payload_len, 46)
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame1.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame1.eth_src_mac
            assert eth_frame.eth_type == test_frame1.eth_type
            assert eth_frame.payload.data.index(test_frame1.payload.data) == 0

            rx_frame = None
            if not sink_queue.empty():
                rx_frame = sink_queue.get()

            assert rx_frame.data[0:8] == bytearray(b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == max(payload_len, 46)
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame2.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame2.eth_src_mac
            assert eth_frame.eth_type == test_frame2.eth_type
            assert eth_frame.payload.data.index(test_frame2.payload.data) == 0

            assert sink_queue.empty()

            yield delay(100)

            yield clk.posedge
            print("test 3: tuser assert, length %d" % payload_len)
            current_test.next = 3

            test_frame1 = eth_ep.EthFrame()
            test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame1.eth_src_mac = 0x5A5152535455
            test_frame1.eth_type = 0x8000
            test_frame1.payload = bytearray(range(payload_len))
            test_frame1.update_fcs()
            test_frame2 = eth_ep.EthFrame()
            test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame2.eth_src_mac = 0x5A5152535455
            test_frame2.eth_type = 0x8000
            test_frame2.payload = bytearray(range(payload_len))
            test_frame2.update_fcs()

            axis_frame1 = test_frame1.build_axis()
            axis_frame2 = test_frame2.build_axis()

            axis_frame1.user = 1

            source_queue.put(axis_frame1)
            source_queue.put(axis_frame2)
            yield clk.posedge
            yield clk.posedge

            while gmii_tx_en or input_axis_tvalid:
                yield clk.posedge

            yield clk.posedge
            yield clk.posedge
            yield clk.posedge

            rx_frame = None
            if not sink_queue.empty():
                rx_frame = sink_queue.get()

            assert rx_frame.data[0:8] == bytearray(b'\x55\x55\x55\x55\x55\x55\x55\xD5')
            assert rx_frame.error[-1]

            # bad packet

            rx_frame = None
            if not sink_queue.empty():
                rx_frame = sink_queue.get()

            assert rx_frame.data[0:8] == bytearray(b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == max(payload_len, 46)
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame2.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame2.eth_src_mac
            assert eth_frame.eth_type == test_frame2.eth_type
            assert eth_frame.payload.data.index(test_frame2.payload.data) == 0

            assert sink_queue.empty()

            yield delay(100)

        raise StopSimulation

    return dut, source, sink, clkgen, check
Ejemplo n.º 2
0
def bench():

    # Parameters


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

    btnu = Signal(bool(0))
    btnl = Signal(bool(0))
    btnd = Signal(bool(0))
    btnr = Signal(bool(0))
    btnc = Signal(bool(0))
    sw = Signal(intbv(0)[4:])
    qsfp_rxd_1 = Signal(intbv(0)[64:])
    qsfp_rxc_1 = Signal(intbv(0)[8:])
    qsfp_rxd_2 = Signal(intbv(0)[64:])
    qsfp_rxc_2 = Signal(intbv(0)[8:])
    qsfp_rxd_3 = Signal(intbv(0)[64:])
    qsfp_rxc_3 = Signal(intbv(0)[8:])
    qsfp_rxd_4 = Signal(intbv(0)[64:])
    qsfp_rxc_4 = Signal(intbv(0)[8:])
    phy_gmii_clk = Signal(bool(0))
    phy_gmii_rst = Signal(bool(0))
    phy_gmii_clk_en = Signal(bool(0))
    phy_gmii_rxd = Signal(intbv(0)[8:])
    phy_gmii_rx_dv = Signal(bool(0))
    phy_gmii_rx_er = Signal(bool(0))
    phy_int_n = Signal(bool(1))
    uart_rxd = Signal(bool(0))
    uart_cts = Signal(bool(0))

    # Outputs
    led = Signal(intbv(0)[8:])
    qsfp_txd_1 = Signal(intbv(0)[64:])
    qsfp_txc_1 = Signal(intbv(0)[8:])
    qsfp_txd_2 = Signal(intbv(0)[64:])
    qsfp_txc_2 = Signal(intbv(0)[8:])
    qsfp_txd_3 = Signal(intbv(0)[64:])
    qsfp_txc_3 = Signal(intbv(0)[8:])
    qsfp_txd_4 = Signal(intbv(0)[64:])
    qsfp_txc_4 = Signal(intbv(0)[8:])
    phy_gmii_txd = Signal(intbv(0)[8:])
    phy_gmii_tx_en = Signal(bool(0))
    phy_gmii_tx_er = Signal(bool(0))
    phy_reset_n = Signal(bool(0))
    uart_txd = Signal(bool(0))
    uart_rts = Signal(bool(0))

    # sources and sinks
    qsfp_1_source = xgmii_ep.XGMIISource()
    qsfp_1_source_logic = qsfp_1_source.create_logic(clk, rst, txd=qsfp_rxd_1, txc=qsfp_rxc_1, name='qsfp_1_source')

    qsfp_1_sink = xgmii_ep.XGMIISink()
    qsfp_1_sink_logic = qsfp_1_sink.create_logic(clk, rst, rxd=qsfp_txd_1, rxc=qsfp_txc_1, name='qsfp_1_sink')

    qsfp_2_source = xgmii_ep.XGMIISource()
    qsfp_2_source_logic = qsfp_2_source.create_logic(clk, rst, txd=qsfp_rxd_2, txc=qsfp_rxc_2, name='qsfp_2_source')

    qsfp_2_sink = xgmii_ep.XGMIISink()
    qsfp_2_sink_logic = qsfp_2_sink.create_logic(clk, rst, rxd=qsfp_txd_2, rxc=qsfp_txc_2, name='qsfp_2_sink')

    qsfp_3_source = xgmii_ep.XGMIISource()
    qsfp_3_source_logic = qsfp_3_source.create_logic(clk, rst, txd=qsfp_rxd_3, txc=qsfp_rxc_3, name='qsfp_3_source')

    qsfp_3_sink = xgmii_ep.XGMIISink()
    qsfp_3_sink_logic = qsfp_3_sink.create_logic(clk, rst, rxd=qsfp_txd_3, rxc=qsfp_txc_3, name='qsfp_3_sink')

    qsfp_4_source = xgmii_ep.XGMIISource()
    qsfp_4_source_logic = qsfp_4_source.create_logic(clk, rst, txd=qsfp_rxd_4, txc=qsfp_rxc_4, name='qsfp_4_source')

    qsfp_4_sink = xgmii_ep.XGMIISink()
    qsfp_4_sink_logic = qsfp_4_sink.create_logic(clk, rst, rxd=qsfp_txd_4, rxc=qsfp_txc_4, name='qsfp_4_sink')

    gmii_source = gmii_ep.GMIISource()

    gmii_source_logic = gmii_source.create_logic(
        phy_gmii_clk,
        phy_gmii_rst,
        txd=phy_gmii_rxd,
        tx_en=phy_gmii_rx_dv,
        tx_er=phy_gmii_rx_er,
        clk_enable=phy_gmii_clk_en,
        name='gmii_source'
    )

    gmii_sink = gmii_ep.GMIISink()

    gmii_sink_logic = gmii_sink.create_logic(
        phy_gmii_clk,
        phy_gmii_rst,
        rxd=phy_gmii_txd,
        rx_dv=phy_gmii_tx_en,
        rx_er=phy_gmii_tx_er,
        clk_enable=phy_gmii_clk_en,
        name='gmii_sink'
    )

    # 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,

        btnu=btnu,
        btnl=btnl,
        btnd=btnd,
        btnr=btnr,
        btnc=btnc,
        sw=sw,
        led=led,

        qsfp_txd_1=qsfp_txd_1,
        qsfp_txc_1=qsfp_txc_1,
        qsfp_rxd_1=qsfp_rxd_1,
        qsfp_rxc_1=qsfp_rxc_1,
        qsfp_txd_2=qsfp_txd_2,
        qsfp_txc_2=qsfp_txc_2,
        qsfp_rxd_2=qsfp_rxd_2,
        qsfp_rxc_2=qsfp_rxc_2,
        qsfp_txd_3=qsfp_txd_3,
        qsfp_txc_3=qsfp_txc_3,
        qsfp_rxd_3=qsfp_rxd_3,
        qsfp_rxc_3=qsfp_rxc_3,
        qsfp_txd_4=qsfp_txd_4,
        qsfp_txc_4=qsfp_txc_4,
        qsfp_rxd_4=qsfp_rxd_4,
        qsfp_rxc_4=qsfp_rxc_4,

        phy_gmii_clk=phy_gmii_clk,
        phy_gmii_rst=phy_gmii_rst,
        phy_gmii_clk_en=phy_gmii_clk_en,
        phy_gmii_rxd=phy_gmii_rxd,
        phy_gmii_rx_dv=phy_gmii_rx_dv,
        phy_gmii_rx_er=phy_gmii_rx_er,
        phy_gmii_txd=phy_gmii_txd,
        phy_gmii_tx_en=phy_gmii_tx_en,
        phy_gmii_tx_er=phy_gmii_tx_er,
        phy_reset_n=phy_reset_n,
        phy_int_n=phy_int_n,

        uart_rxd=uart_rxd,
        uart_txd=uart_txd,
        uart_rts=uart_rts,
        uart_cts=uart_cts
    )

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

    clk_enable_rate = Signal(int(0))
    clk_enable_div = Signal(int(0))

    @always(clk.posedge)
    def clk_enable_gen():
        if clk_enable_div.next > 0:
            phy_gmii_clk_en.next = 0
            clk_enable_div.next = clk_enable_div - 1
        else:
            phy_gmii_clk_en.next = 1
            clk_enable_div.next = clk_enable_rate - 1

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

        # testbench stimulus

        yield clk.posedge
        print("test 1: test UDP RX packet")
        current_test.next = 1

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80181
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 5678
        test_frame.udp_dest_port = 1234
        test_frame.payload = bytearray(range(32))
        test_frame.build()

        qsfp_1_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5'+test_frame.build_eth().build_axis_fcs().data)

        # wait for ARP request packet
        while qsfp_1_sink.empty():
            yield clk.posedge

        rx_frame = qsfp_1_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = arp_ep.ARPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xFFFFFFFFFFFF
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0806
        assert check_frame.arp_htype == 0x0001
        assert check_frame.arp_ptype == 0x0800
        assert check_frame.arp_hlen == 6
        assert check_frame.arp_plen == 4
        assert check_frame.arp_oper == 1
        assert check_frame.arp_sha == 0x020000000000
        assert check_frame.arp_spa == 0xc0a80180
        assert check_frame.arp_tha == 0x000000000000
        assert check_frame.arp_tpa == 0xc0a80181

        # generate response
        arp_frame = arp_ep.ARPFrame()
        arp_frame.eth_dest_mac = 0x020000000000
        arp_frame.eth_src_mac = 0xDAD1D2D3D4D5
        arp_frame.eth_type = 0x0806
        arp_frame.arp_htype = 0x0001
        arp_frame.arp_ptype = 0x0800
        arp_frame.arp_hlen = 6
        arp_frame.arp_plen = 4
        arp_frame.arp_oper = 2
        arp_frame.arp_sha = 0xDAD1D2D3D4D5
        arp_frame.arp_spa = 0xc0a80181
        arp_frame.arp_tha = 0x020000000000
        arp_frame.arp_tpa = 0xc0a80180

        qsfp_1_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5'+arp_frame.build_eth().build_axis_fcs().data)

        while qsfp_1_sink.empty():
            yield clk.posedge

        rx_frame = qsfp_1_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xDAD1D2D3D4D5
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0800
        assert check_frame.ip_version == 4
        assert check_frame.ip_ihl == 5
        assert check_frame.ip_dscp == 0
        assert check_frame.ip_ecn == 0
        assert check_frame.ip_identification == 0
        assert check_frame.ip_flags == 2
        assert check_frame.ip_fragment_offset == 0
        assert check_frame.ip_ttl == 64
        assert check_frame.ip_protocol == 0x11
        assert check_frame.ip_source_ip == 0xc0a80180
        assert check_frame.ip_dest_ip == 0xc0a80181
        assert check_frame.udp_source_port == 1234
        assert check_frame.udp_dest_port == 5678
        assert check_frame.payload.data == bytearray(range(32))

        assert qsfp_1_source.empty()
        assert qsfp_1_sink.empty()

        yield delay(100)

        yield clk.posedge
        print("test 2: test gigabit tap")
        current_test.next = 2

        sw.next = 0x8 # enable tap on RX

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80181
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 5678
        test_frame.udp_dest_port = 1234
        test_frame.payload = bytearray(range(32))
        test_frame.build()

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5'+test_frame.build_eth().build_axis_fcs().data)

        # loop packet back through on XGMII interface
        while qsfp_1_sink.empty():
            yield clk.posedge

        qsfp_1_source.send(qsfp_1_sink.recv())

        while gmii_sink.empty():
            yield clk.posedge

        rx_frame = gmii_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xDAD1D2D3D4D5
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0800
        assert check_frame.ip_version == 4
        assert check_frame.ip_ihl == 5
        assert check_frame.ip_dscp == 0
        assert check_frame.ip_ecn == 0
        assert check_frame.ip_identification == 0
        assert check_frame.ip_flags == 2
        assert check_frame.ip_fragment_offset == 0
        assert check_frame.ip_ttl == 64
        assert check_frame.ip_protocol == 0x11
        assert check_frame.ip_source_ip == 0xc0a80180
        assert check_frame.ip_dest_ip == 0xc0a80181
        assert check_frame.udp_source_port == 1234
        assert check_frame.udp_dest_port == 5678
        assert check_frame.payload.data == bytearray(range(32))

        assert gmii_source.empty()
        assert gmii_sink.empty()
        assert qsfp_1_source.empty()
        assert qsfp_1_sink.empty()

        yield delay(100)

        sw.next = 0xc # enable tap on TX

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80181
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 5678
        test_frame.udp_dest_port = 1234
        test_frame.payload = bytearray(range(32))
        test_frame.build()

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5'+test_frame.build_eth().build_axis_fcs().data)

        # loop packet back through on XGMII interface
        while qsfp_1_sink.empty():
            yield clk.posedge

        qsfp_1_source.send(qsfp_1_sink.recv())

        while gmii_sink.empty():
            yield clk.posedge

        rx_frame = gmii_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xDAD1D2D3D4D5
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0800
        assert check_frame.ip_version == 4
        assert check_frame.ip_ihl == 5
        assert check_frame.ip_dscp == 0
        assert check_frame.ip_ecn == 0
        assert check_frame.ip_identification == 0
        assert check_frame.ip_flags == 2
        assert check_frame.ip_fragment_offset == 0
        assert check_frame.ip_ttl == 64
        assert check_frame.ip_protocol == 0x11
        assert check_frame.ip_source_ip == 0xc0a80180
        assert check_frame.ip_dest_ip == 0xc0a80181
        assert check_frame.udp_source_port == 1234
        assert check_frame.udp_dest_port == 5678
        assert check_frame.payload.data == bytearray(range(32))

        assert gmii_source.empty()
        assert gmii_sink.empty()
        assert qsfp_1_source.empty()
        assert qsfp_1_sink.empty()

        yield delay(100)

        raise StopSimulation

    return instances()
Ejemplo n.º 3
0
def bench():

    # Parameters
    TARGET = "SIM"
    IODDR_STYLE = "IODDR2"
    CLOCK_INPUT_STYLE = "BUFIO2"
    AXIS_DATA_WIDTH = 8
    AXIS_KEEP_ENABLE = (AXIS_DATA_WIDTH>8)
    AXIS_KEEP_WIDTH = (AXIS_DATA_WIDTH/8)
    ENABLE_PADDING = 1
    MIN_FRAME_LENGTH = 64
    TX_FIFO_DEPTH = 4096
    TX_FRAME_FIFO = 1
    TX_DROP_BAD_FRAME = TX_FRAME_FIFO
    TX_DROP_WHEN_FULL = 0
    RX_FIFO_DEPTH = 4096
    RX_FRAME_FIFO = 1
    RX_DROP_BAD_FRAME = RX_FRAME_FIFO
    RX_DROP_WHEN_FULL = RX_FRAME_FIFO

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

    gtx_clk = Signal(bool(0))
    gtx_rst = Signal(bool(0))
    logic_clk = Signal(bool(0))
    logic_rst = Signal(bool(0))
    tx_axis_tdata = Signal(intbv(0)[AXIS_DATA_WIDTH:])
    tx_axis_tkeep = Signal(intbv(1)[AXIS_KEEP_WIDTH:])
    tx_axis_tvalid = Signal(bool(0))
    tx_axis_tlast = Signal(bool(0))
    tx_axis_tuser = Signal(bool(0))
    rx_axis_tready = Signal(bool(0))
    gmii_rx_clk = Signal(bool(0))
    gmii_rxd = Signal(intbv(0)[8:])
    gmii_rx_dv = Signal(bool(0))
    gmii_rx_er = Signal(bool(0))
    mii_tx_clk = Signal(bool(0))
    ifg_delay = Signal(intbv(0)[8:])

    # Outputs
    tx_axis_tready = Signal(bool(0))
    rx_axis_tdata = Signal(intbv(0)[AXIS_DATA_WIDTH:])
    rx_axis_tkeep = Signal(intbv(1)[AXIS_KEEP_WIDTH:])
    rx_axis_tvalid = Signal(bool(0))
    rx_axis_tlast = Signal(bool(0))
    rx_axis_tuser = Signal(bool(0))
    gmii_tx_clk = Signal(bool(0))
    gmii_txd = Signal(intbv(0)[8:])
    gmii_tx_en = Signal(bool(0))
    gmii_tx_er = Signal(bool(0))
    tx_error_underflow = Signal(bool(0))
    tx_fifo_overflow = Signal(bool(0))
    tx_fifo_bad_frame = Signal(bool(0))
    tx_fifo_good_frame = Signal(bool(0))
    rx_error_bad_frame = Signal(bool(0))
    rx_error_bad_fcs = Signal(bool(0))
    rx_fifo_overflow = Signal(bool(0))
    rx_fifo_bad_frame = Signal(bool(0))
    rx_fifo_good_frame = Signal(bool(0))
    speed = Signal(intbv(0)[1:0])

    # sources and sinks
    axis_source_pause = Signal(bool(0))
    axis_sink_pause = Signal(bool(0))

    mii_select = Signal(bool(0))

    gmii_source = gmii_ep.GMIISource()

    gmii_source_logic = gmii_source.create_logic(
        gmii_rx_clk,
        rst,
        txd=gmii_rxd,
        tx_en=gmii_rx_dv,
        tx_er=gmii_rx_er,
        mii_select=mii_select,
        name='gmii_source'
    )

    gmii_sink = gmii_ep.GMIISink()

    gmii_sink_logic = gmii_sink.create_logic(
        gmii_tx_clk,
        rst,
        rxd=gmii_txd,
        rx_dv=gmii_tx_en,
        rx_er=gmii_tx_er,
        mii_select=mii_select,
        name='gmii_sink'
    )

    axis_source = axis_ep.AXIStreamSource()

    axis_source_logic = axis_source.create_logic(
        logic_clk,
        logic_rst,
        tdata=tx_axis_tdata,
        tkeep=tx_axis_tkeep,
        tvalid=tx_axis_tvalid,
        tready=tx_axis_tready,
        tlast=tx_axis_tlast,
        tuser=tx_axis_tuser,
        pause=axis_source_pause,
        name='axis_source'
    )

    axis_sink = axis_ep.AXIStreamSink()

    axis_sink_logic = axis_sink.create_logic(
        logic_clk,
        logic_rst,
        tdata=rx_axis_tdata,
        tkeep=rx_axis_tkeep,
        tvalid=rx_axis_tvalid,
        tready=rx_axis_tready,
        tlast=rx_axis_tlast,
        tuser=rx_axis_tuser,
        pause=axis_sink_pause,
        name='axis_sink'
    )

    # 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,

        gtx_clk=gtx_clk,
        gtx_rst=gtx_rst,
        logic_clk=logic_clk,
        logic_rst=logic_rst,

        tx_axis_tdata=tx_axis_tdata,
        tx_axis_tkeep=tx_axis_tkeep,
        tx_axis_tvalid=tx_axis_tvalid,
        tx_axis_tready=tx_axis_tready,
        tx_axis_tlast=tx_axis_tlast,
        tx_axis_tuser=tx_axis_tuser,

        rx_axis_tdata=rx_axis_tdata,
        rx_axis_tkeep=rx_axis_tkeep,
        rx_axis_tvalid=rx_axis_tvalid,
        rx_axis_tready=rx_axis_tready,
        rx_axis_tlast=rx_axis_tlast,
        rx_axis_tuser=rx_axis_tuser,

        gmii_rx_clk=gmii_rx_clk,
        gmii_rxd=gmii_rxd,
        gmii_rx_dv=gmii_rx_dv,
        gmii_rx_er=gmii_rx_er,

        gmii_tx_clk=gmii_tx_clk,
        mii_tx_clk=mii_tx_clk,
        gmii_txd=gmii_txd,
        gmii_tx_en=gmii_tx_en,
        gmii_tx_er=gmii_tx_er,

        tx_error_underflow=tx_error_underflow,
        tx_fifo_overflow=tx_fifo_overflow,
        tx_fifo_bad_frame=tx_fifo_bad_frame,
        tx_fifo_good_frame=tx_fifo_good_frame,
        rx_error_bad_frame=rx_error_bad_frame,
        rx_error_bad_fcs=rx_error_bad_fcs,
        rx_fifo_overflow=rx_fifo_overflow,
        rx_fifo_bad_frame=rx_fifo_bad_frame,
        rx_fifo_good_frame=rx_fifo_good_frame,
        speed=speed,

        ifg_delay=ifg_delay
    )

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

    rx_clk_hp = Signal(int(4))

    @instance
    def rx_clk_gen():
        while True:
            yield delay(int(rx_clk_hp))
            gmii_rx_clk.next = not gmii_rx_clk
            mii_tx_clk.next = not gmii_rx_clk

    rx_error_bad_frame_asserted = Signal(bool(0))
    rx_error_bad_fcs_asserted = Signal(bool(0))

    @always(clk.posedge)
    def monitor():
        if (rx_error_bad_frame):
            rx_error_bad_frame_asserted.next = 1
        if (rx_error_bad_fcs):
            rx_error_bad_fcs_asserted.next = 1

    clk_enable_rate = Signal(int(0))
    clk_enable_div = Signal(int(0))

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

        ifg_delay.next = 12

        # testbench stimulus

        for rate, mii in [(4, 0), (20, 1), (200, 1)]:
            rx_clk_hp.next = rate
            mii_select.next = mii

            yield delay(1000)

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

            test_frame = eth_ep.EthFrame()
            test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame.eth_src_mac = 0x5A5152535455
            test_frame.eth_type = 0x8000
            test_frame.payload = bytearray(range(32))
            test_frame.update_fcs()

            axis_frame = test_frame.build_axis_fcs()

            gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5'+bytearray(axis_frame))

            yield axis_sink.wait()
            rx_frame = axis_sink.recv()

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis(rx_frame)
            eth_frame.update_fcs()

            assert eth_frame == test_frame

            yield delay(100)

            yield clk.posedge
            print("test 2: test tx packet")
            current_test.next = 2

            test_frame = eth_ep.EthFrame()
            test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame.eth_src_mac = 0x5A5152535455
            test_frame.eth_type = 0x8000
            test_frame.payload = bytearray(range(32))
            test_frame.update_fcs()

            axis_frame = test_frame.build_axis()

            axis_source.send(axis_frame)

            yield gmii_sink.wait()
            rx_frame = gmii_sink.recv()

            assert rx_frame.data[0:8] == bytearray(b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == 46
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame.eth_src_mac
            assert eth_frame.eth_type == test_frame.eth_type
            assert eth_frame.payload.data.index(test_frame.payload.data) == 0

            yield delay(100)

        raise StopSimulation

    return instances()
Ejemplo n.º 4
0
def bench():

    # Parameters

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

    btnu = Signal(bool(0))
    btnl = Signal(bool(0))
    btnd = Signal(bool(0))
    btnr = Signal(bool(0))
    btnc = Signal(bool(0))
    sw = Signal(intbv(0)[8:])
    phy_gmii_clk = Signal(bool(0))
    phy_gmii_rst = Signal(bool(0))
    phy_gmii_clk_en = Signal(bool(0))
    phy_gmii_rxd = Signal(intbv(0)[8:])
    phy_gmii_rx_dv = Signal(bool(0))
    phy_gmii_rx_er = Signal(bool(0))
    uart_txd = Signal(bool(0))
    uart_rts = Signal(bool(0))

    # Outputs
    ledu = Signal(bool(0))
    ledl = Signal(bool(0))
    ledd = Signal(bool(0))
    ledr = Signal(bool(0))
    ledc = Signal(bool(0))
    led = Signal(intbv(0)[8:])
    phy_gmii_txd = Signal(intbv(0)[8:])
    phy_gmii_tx_en = Signal(bool(0))
    phy_gmii_tx_er = Signal(bool(0))
    phy_reset_n = Signal(bool(0))
    uart_rxd = Signal(bool(0))
    uart_cts = Signal(bool(0))

    # sources and sinks
    gmii_source = gmii_ep.GMIISource()

    gmii_source_logic = gmii_source.create_logic(phy_gmii_clk,
                                                 phy_gmii_rst,
                                                 txd=phy_gmii_rxd,
                                                 tx_en=phy_gmii_rx_dv,
                                                 tx_er=phy_gmii_rx_er,
                                                 clk_enable=phy_gmii_clk_en,
                                                 name='gmii_source')

    gmii_sink = gmii_ep.GMIISink()

    gmii_sink_logic = gmii_sink.create_logic(phy_gmii_clk,
                                             phy_gmii_rst,
                                             rxd=phy_gmii_txd,
                                             rx_dv=phy_gmii_tx_en,
                                             rx_er=phy_gmii_tx_er,
                                             clk_enable=phy_gmii_clk_en,
                                             name='gmii_sink')

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

    dut = Cosimulation("vvp -m myhdl %s.vvp -lxt2" % testbench,
                       clk_125mhz=clk_125mhz,
                       rst_125mhz=rst_125mhz,
                       current_test=current_test,
                       btnu=btnu,
                       btnl=btnl,
                       btnd=btnd,
                       btnr=btnr,
                       btnc=btnc,
                       sw=sw,
                       ledu=ledu,
                       ledl=ledl,
                       ledd=ledd,
                       ledr=ledr,
                       ledc=ledc,
                       led=led,
                       phy_gmii_clk=phy_gmii_clk,
                       phy_gmii_rst=phy_gmii_rst,
                       phy_gmii_clk_en=phy_gmii_clk_en,
                       phy_gmii_rxd=phy_gmii_rxd,
                       phy_gmii_rx_dv=phy_gmii_rx_dv,
                       phy_gmii_rx_er=phy_gmii_rx_er,
                       phy_gmii_txd=phy_gmii_txd,
                       phy_gmii_tx_en=phy_gmii_tx_en,
                       phy_gmii_tx_er=phy_gmii_tx_er,
                       phy_reset_n=phy_reset_n,
                       uart_rxd=uart_rxd,
                       uart_txd=uart_txd,
                       uart_rts=uart_rts,
                       uart_cts=uart_cts)

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

    clk_enable_rate = Signal(int(0))
    clk_enable_div = Signal(int(0))

    @always(clk.posedge)
    def clk_enable_gen():
        if clk_enable_div.next > 0:
            phy_gmii_clk_en.next = 0
            clk_enable_div.next = clk_enable_div - 1
        else:
            phy_gmii_clk_en.next = 1
            clk_enable_div.next = clk_enable_rate - 1

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

        # testbench stimulus

        yield clk.posedge

        current_test.next = 1

        RD_CMD = 0x01
        WR_CMD = 0x02

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80182
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 5678
        test_frame.udp_dest_port = 1234
        test_frame.payload = bytearray(
            array('B', struct.pack('>BII', RD_CMD, 1, 0x1000)))
        test_frame.build()

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         test_frame.build_eth().build_axis_fcs().data)

        # wait for ARP request packet
        while gmii_sink.empty():
            yield clk.posedge

        rx_frame = gmii_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = arp_ep.ARPFrame()
        check_frame.parse_eth(check_eth_frame)

        #print(check_frame)
        assert check_frame.eth_dest_mac == 0xFFFFFFFFFFFF
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0806
        assert check_frame.arp_htype == 0x0001
        assert check_frame.arp_ptype == 0x0800
        assert check_frame.arp_hlen == 6
        assert check_frame.arp_plen == 4
        assert check_frame.arp_oper == 1
        assert check_frame.arp_sha == 0x020000000000
        assert check_frame.arp_spa == 0xc0a80180
        assert check_frame.arp_tha == 0x000000000000
        assert check_frame.arp_tpa == 0xc0a80182

        # generate response
        arp_frame = arp_ep.ARPFrame()
        arp_frame.eth_dest_mac = 0x020000000000
        arp_frame.eth_src_mac = 0xDAD1D2D3D4D5
        arp_frame.eth_type = 0x0806
        arp_frame.arp_htype = 0x0001
        arp_frame.arp_ptype = 0x0800
        arp_frame.arp_hlen = 6
        arp_frame.arp_plen = 4
        arp_frame.arp_oper = 2
        arp_frame.arp_sha = 0xDAD1D2D3D4D5
        arp_frame.arp_spa = 0xc0a80182
        arp_frame.arp_tha = 0x020000000000
        arp_frame.arp_tpa = 0xc0a80180

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         arp_frame.build_eth().build_axis_fcs().data)

        while gmii_sink.empty():
            yield clk.posedge
        rx_frame = gmii_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)
        print '!Rec_size:', len(check_frame.payload.data), check_frame.payload

        # WR
        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80182
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 5678
        test_frame.udp_dest_port = 1234
        test_frame.payload = bytearray(
            array('B', struct.pack('>BII', WR_CMD, 1, 0x1000))) + bytearray(
                [2])
        test_frame.build()
        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         test_frame.build_eth().build_axis_fcs().data)
        while gmii_sink.empty():
            yield clk.posedge
        rx_frame = gmii_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print '!Rec_size:', len(check_frame.payload.data), check_frame.payload

        #RD 32
        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80182
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 5678
        test_frame.udp_dest_port = 1234
        test_frame.payload = bytearray(
            array('B', struct.pack('>BII', RD_CMD, 8, 0x80000000)))
        test_frame.build()
        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         test_frame.build_eth().build_axis_fcs().data)
        while gmii_sink.empty():
            yield clk.posedge
        rx_frame = gmii_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print '!Rec_size:', len(check_frame.payload.data), check_frame.payload

        assert gmii_source.empty()
        assert gmii_sink.empty()

        yield delay(100)

        raise StopSimulation

    return dut, gmii_source_logic, gmii_sink_logic, clkgen, clk_enable_gen, check
Ejemplo n.º 5
0
def bench():

    # Parameters
    TARGET = "SIM"

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

    btnu = Signal(bool(0))
    btnl = Signal(bool(0))
    btnd = Signal(bool(0))
    btnr = Signal(bool(0))
    btnc = Signal(bool(0))
    sw = Signal(intbv(0)[4:])
    i2c_scl_i = Signal(bool(1))
    i2c_sda_i = Signal(bool(1))
    phy_gmii_clk = Signal(bool(0))
    phy_gmii_rst = Signal(bool(0))
    phy_gmii_clk_en = Signal(bool(0))
    phy_gmii_rxd = Signal(intbv(0)[8:])
    phy_gmii_rx_dv = Signal(bool(0))
    phy_gmii_rx_er = Signal(bool(0))
    phy_int_n = Signal(bool(1))
    uart_rxd = Signal(bool(1))
    uart_cts = Signal(bool(1))

    s1_scl_i = Signal(bool(1))
    s1_sda_i = Signal(bool(1))

    s2_scl_i = Signal(bool(1))
    s2_sda_i = Signal(bool(1))

    # Outputs
    led = Signal(intbv(0)[8:])
    i2c_scl_o = Signal(bool(1))
    i2c_scl_t = Signal(bool(1))
    i2c_sda_o = Signal(bool(1))
    i2c_sda_t = Signal(bool(1))
    phy_gmii_txd = Signal(intbv(0)[8:])
    phy_gmii_tx_en = Signal(bool(0))
    phy_gmii_tx_er = Signal(bool(0))
    phy_reset_n = Signal(bool(0))
    uart_txd = Signal(bool(1))
    uart_rts = Signal(bool(1))

    s1_scl_o = Signal(bool(1))
    s1_scl_t = Signal(bool(1))
    s1_sda_o = Signal(bool(1))
    s1_sda_t = Signal(bool(1))

    s2_scl_o = Signal(bool(1))
    s2_scl_t = Signal(bool(1))
    s2_sda_o = Signal(bool(1))
    s2_sda_t = Signal(bool(1))

    # sources and sinks
    gmii_source = gmii_ep.GMIISource()

    gmii_source_logic = gmii_source.create_logic(phy_gmii_clk,
                                                 phy_gmii_rst,
                                                 txd=phy_gmii_rxd,
                                                 tx_en=phy_gmii_rx_dv,
                                                 tx_er=phy_gmii_rx_er,
                                                 clk_enable=phy_gmii_clk_en,
                                                 name='gmii_source')

    gmii_sink = gmii_ep.GMIISink()

    gmii_sink_logic = gmii_sink.create_logic(phy_gmii_clk,
                                             phy_gmii_rst,
                                             rxd=phy_gmii_txd,
                                             rx_dv=phy_gmii_tx_en,
                                             rx_er=phy_gmii_tx_er,
                                             clk_enable=phy_gmii_clk_en,
                                             name='gmii_sink')

    uart_source = uart_ep.UARTSource()

    uart_source_logic = uart_source.create_logic(clk,
                                                 rst,
                                                 txd=uart_rxd,
                                                 prescale=int(125000000 /
                                                              (115200 * 8)),
                                                 name='uart_source')

    uart_sink = uart_ep.UARTSink()

    uart_sink_logic = uart_sink.create_logic(clk,
                                             rst,
                                             rxd=uart_txd,
                                             prescale=int(125000000 /
                                                          (115200 * 8)),
                                             name='uart_sink')

    # I2C memory model 1
    i2c_mem1 = i2c.I2CMem(1024)

    i2c_mem_logic1 = i2c_mem1.create_logic(scl_i=s1_scl_i,
                                           scl_o=s1_scl_o,
                                           scl_t=s1_scl_t,
                                           sda_i=s1_sda_i,
                                           sda_o=s1_sda_o,
                                           sda_t=s1_sda_t,
                                           abw=2,
                                           address=0x50,
                                           latency=0,
                                           name='slave1')

    # I2C memory model 2
    i2c_mem2 = i2c.I2CMem(1024)

    i2c_mem_logic2 = i2c_mem2.create_logic(scl_i=s2_scl_i,
                                           scl_o=s2_scl_o,
                                           scl_t=s2_scl_t,
                                           sda_i=s2_sda_i,
                                           sda_o=s2_sda_o,
                                           sda_t=s2_sda_t,
                                           abw=2,
                                           address=0x51,
                                           latency=1000,
                                           name='slave2')

    # 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,
                       btnu=btnu,
                       btnl=btnl,
                       btnd=btnd,
                       btnr=btnr,
                       btnc=btnc,
                       sw=sw,
                       led=led,
                       i2c_scl_i=i2c_scl_i,
                       i2c_scl_o=i2c_scl_o,
                       i2c_scl_t=i2c_scl_t,
                       i2c_sda_i=i2c_sda_i,
                       i2c_sda_o=i2c_sda_o,
                       i2c_sda_t=i2c_sda_t,
                       phy_gmii_clk=phy_gmii_clk,
                       phy_gmii_rst=phy_gmii_rst,
                       phy_gmii_clk_en=phy_gmii_clk_en,
                       phy_gmii_rxd=phy_gmii_rxd,
                       phy_gmii_rx_dv=phy_gmii_rx_dv,
                       phy_gmii_rx_er=phy_gmii_rx_er,
                       phy_gmii_txd=phy_gmii_txd,
                       phy_gmii_tx_en=phy_gmii_tx_en,
                       phy_gmii_tx_er=phy_gmii_tx_er,
                       phy_reset_n=phy_reset_n,
                       phy_int_n=phy_int_n,
                       uart_rxd=uart_rxd,
                       uart_txd=uart_txd,
                       uart_rts=uart_rts,
                       uart_cts=uart_cts)

    @always_comb
    def bus():
        # emulate I2C wired AND
        i2c_scl_i.next = i2c_scl_o & s1_scl_o & s2_scl_o
        i2c_sda_i.next = i2c_sda_o & s1_sda_o & s2_sda_o

        s1_scl_i.next = i2c_scl_o & s1_scl_o & s2_scl_o
        s1_sda_i.next = i2c_sda_o & s1_sda_o & s2_sda_o

        s2_scl_i.next = i2c_scl_o & s1_scl_o & s2_scl_o
        s2_sda_i.next = i2c_sda_o & s1_sda_o & s2_sda_o

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

    clk_enable_rate = Signal(int(0))
    clk_enable_div = Signal(int(0))

    @always(clk.posedge)
    def clk_enable_gen():
        if clk_enable_div.next > 0:
            phy_gmii_clk_en.next = 0
            clk_enable_div.next = clk_enable_div - 1
        else:
            phy_gmii_clk_en.next = 1
            clk_enable_div.next = clk_enable_rate - 1

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

        # testbench stimulus

        yield clk.posedge
        print("test 1: enumerate via UDP")
        current_test.next = 1

        pkt = xfcp.XFCPFrame()
        pkt.path = []
        pkt.rpath = []
        pkt.ptype = 0xfe
        pkt.payload = b''

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80181
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 1234
        test_frame.udp_dest_port = 14000
        test_frame.payload = pkt.build_axis()
        test_frame.build()

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         test_frame.build_eth().build_axis_fcs().data)

        # wait for ARP request packet
        rx_frame = None
        while rx_frame is None:
            yield clk.posedge
            rx_frame = gmii_sink.recv()

        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = arp_ep.ARPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xFFFFFFFFFFFF
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0806
        assert check_frame.arp_htype == 0x0001
        assert check_frame.arp_ptype == 0x0800
        assert check_frame.arp_hlen == 6
        assert check_frame.arp_plen == 4
        assert check_frame.arp_oper == 1
        assert check_frame.arp_sha == 0x020000000000
        assert check_frame.arp_spa == 0xc0a80180
        assert check_frame.arp_tha == 0x000000000000
        assert check_frame.arp_tpa == 0xc0a80181

        # generate response
        arp_frame = arp_ep.ARPFrame()
        arp_frame.eth_dest_mac = 0x020000000000
        arp_frame.eth_src_mac = 0xDAD1D2D3D4D5
        arp_frame.eth_type = 0x0806
        arp_frame.arp_htype = 0x0001
        arp_frame.arp_ptype = 0x0800
        arp_frame.arp_hlen = 6
        arp_frame.arp_plen = 4
        arp_frame.arp_oper = 2
        arp_frame.arp_sha = 0xDAD1D2D3D4D5
        arp_frame.arp_spa = 0xc0a80181
        arp_frame.arp_tha = 0x020000000000
        arp_frame.arp_tpa = 0xc0a80180

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         arp_frame.build_eth().build_axis_fcs().data)

        rx_frame = None
        while rx_frame is None:
            yield clk.posedge
            rx_frame = gmii_sink.recv()

        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xDAD1D2D3D4D5
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0800
        assert check_frame.ip_version == 4
        assert check_frame.ip_ihl == 5
        assert check_frame.ip_dscp == 0
        assert check_frame.ip_ecn == 0
        assert check_frame.ip_identification == 0
        assert check_frame.ip_flags == 2
        assert check_frame.ip_fragment_offset == 0
        assert check_frame.ip_ttl == 64
        assert check_frame.ip_protocol == 0x11
        assert check_frame.ip_source_ip == 0xc0a80180
        assert check_frame.ip_dest_ip == 0xc0a80181
        assert check_frame.udp_source_port == 14000
        assert check_frame.udp_dest_port == 1234

        rx_pkt = xfcp.XFCPFrame()
        rx_pkt.parse_axis(check_frame.payload.data)

        print(rx_pkt)

        assert rx_pkt.ptype == 0xff
        assert rx_pkt.path == []
        assert rx_pkt.rpath == []
        assert len(rx_pkt.payload.data) == 64

        pkt = xfcp.XFCPFrame()
        pkt.path = [0]
        pkt.rpath = []
        pkt.ptype = 0xfe
        pkt.payload = b''

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80181
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 1234
        test_frame.udp_dest_port = 14000
        test_frame.payload = pkt.build_axis()
        test_frame.build()

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         test_frame.build_eth().build_axis_fcs().data)

        rx_frame = None
        while rx_frame is None:
            yield clk.posedge
            rx_frame = gmii_sink.recv()

        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xDAD1D2D3D4D5
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0800
        assert check_frame.ip_version == 4
        assert check_frame.ip_ihl == 5
        assert check_frame.ip_dscp == 0
        assert check_frame.ip_ecn == 0
        assert check_frame.ip_identification == 0
        assert check_frame.ip_flags == 2
        assert check_frame.ip_fragment_offset == 0
        assert check_frame.ip_ttl == 64
        assert check_frame.ip_protocol == 0x11
        assert check_frame.ip_source_ip == 0xc0a80180
        assert check_frame.ip_dest_ip == 0xc0a80181
        assert check_frame.udp_source_port == 14000
        assert check_frame.udp_dest_port == 1234

        rx_pkt = xfcp.XFCPFrame()
        rx_pkt.parse_axis(check_frame.payload.data)

        print(rx_pkt)

        assert rx_pkt.ptype == 0xff
        assert rx_pkt.path == [0]
        assert rx_pkt.rpath == []
        assert len(rx_pkt.payload.data) == 32

        assert gmii_source.empty()
        assert gmii_sink.empty()

        yield delay(100)

        yield clk.posedge
        print("test 1: test write and read RAM 0")
        current_test.next = 1

        pkt1 = xfcp.XFCPFrame()
        pkt1.path = [0]
        pkt1.ptype = 0x12
        pkt1.payload = bytearray(
            struct.pack('<BH', 0, 4) + b'\x11\x22\x33\x44')

        pkt2 = xfcp.XFCPFrame()
        pkt2.path = [0]
        pkt2.ptype = 0x10
        pkt2.payload = bytearray(struct.pack('<BH', 0, 4))

        test_frame1 = udp_ep.UDPFrame()
        test_frame1.eth_dest_mac = 0x020000000000
        test_frame1.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame1.eth_type = 0x0800
        test_frame1.ip_version = 4
        test_frame1.ip_ihl = 5
        test_frame1.ip_dscp = 0
        test_frame1.ip_ecn = 0
        test_frame1.ip_length = None
        test_frame1.ip_identification = 0
        test_frame1.ip_flags = 2
        test_frame1.ip_fragment_offset = 0
        test_frame1.ip_ttl = 64
        test_frame1.ip_protocol = 0x11
        test_frame1.ip_header_checksum = None
        test_frame1.ip_source_ip = 0xc0a80181
        test_frame1.ip_dest_ip = 0xc0a80180
        test_frame1.udp_source_port = 1234
        test_frame1.udp_dest_port = 14000
        test_frame1.payload = pkt1.build_axis()
        test_frame1.build()

        test_frame2 = udp_ep.UDPFrame(test_frame1)
        test_frame2.payload = pkt2.build_axis()
        test_frame2.build()

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         test_frame1.build_eth().build_axis_fcs().data)
        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         test_frame2.build_eth().build_axis_fcs().data)

        rx_frame = None
        while rx_frame is None:
            yield clk.posedge
            rx_frame = gmii_sink.recv()

        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        rx_pkt = xfcp.XFCPFrame()
        rx_pkt.parse_axis(check_frame.payload.data)

        print(rx_pkt)
        assert rx_pkt.ptype == 0x13
        assert rx_pkt.payload.data == struct.pack('<BH', 0, 4)

        rx_frame = None
        while rx_frame is None:
            yield clk.posedge
            rx_frame = gmii_sink.recv()

        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        rx_pkt = xfcp.XFCPFrame()
        rx_pkt.parse_axis(check_frame.payload.data)

        print(rx_pkt)
        assert rx_pkt.ptype == 0x11
        assert rx_pkt.payload.data == struct.pack('<BH', 0,
                                                  4) + b'\x11\x22\x33\x44'

        assert gmii_source.empty()
        assert gmii_sink.empty()

        yield delay(100)

        yield clk.posedge
        print("test 3: enumerate via UART")
        current_test.next = 3

        pkt = xfcp.XFCPFrame()
        pkt.path = []
        pkt.rpath = []
        pkt.ptype = 0xfe
        pkt.payload = b''

        uart_source.write(pkt.build_axis_cobs().data + b'\x00')

        yield clk.posedge

        rx_data = b''
        while True:
            if not uart_sink.empty():
                b = bytearray(uart_sink.read(1))
                rx_data += b
                if b[0] == 0:
                    break
            yield clk.posedge

        rx_pkt = xfcp.XFCPFrame()
        rx_pkt.parse_axis_cobs(rx_data[:-1])

        print(rx_pkt)

        assert rx_pkt.ptype == 0xff
        assert rx_pkt.path == []
        assert rx_pkt.rpath == []
        assert len(rx_pkt.payload.data) == 64

        pkt = xfcp.XFCPFrame()
        pkt.path = [0]
        pkt.rpath = []
        pkt.ptype = 0xfe
        pkt.payload = b''

        uart_source.write(pkt.build_axis_cobs().data + b'\x00')

        yield clk.posedge

        rx_data = b''
        while True:
            if not uart_sink.empty():
                b = bytearray(uart_sink.read(1))
                rx_data += b
                if b[0] == 0:
                    break
            yield clk.posedge

        rx_pkt = xfcp.XFCPFrame()
        rx_pkt.parse_axis_cobs(rx_data[:-1])

        print(rx_pkt)

        assert rx_pkt.ptype == 0xff
        assert rx_pkt.path == [0]
        assert rx_pkt.rpath == []
        assert len(rx_pkt.payload.data) == 32

        yield delay(100)

        raise StopSimulation

    return instances()
Ejemplo n.º 6
0
def bench():

    # Parameters
    TARGET = "SIM"

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

    btnu = Signal(bool(0))
    btnl = Signal(bool(0))
    btnd = Signal(bool(0))
    btnr = Signal(bool(0))
    btnc = Signal(bool(0))
    sw = Signal(intbv(0)[8:])
    phy_rx_clk = Signal(bool(0))
    phy_rxd = Signal(intbv(0)[8:])
    phy_rx_dv = Signal(bool(0))
    phy_rx_er = Signal(bool(0))
    phy_tx_clk = Signal(bool(0))
    uart_txd = Signal(bool(0))
    uart_rts = Signal(bool(0))

    # Outputs
    ledu = Signal(bool(0))
    ledl = Signal(bool(0))
    ledd = Signal(bool(0))
    ledr = Signal(bool(0))
    ledc = Signal(bool(0))
    led = Signal(intbv(0)[8:])
    phy_gtx_clk = Signal(bool(0))
    phy_txd = Signal(intbv(0)[8:])
    phy_tx_en = Signal(bool(0))
    phy_tx_er = Signal(bool(0))
    phy_reset_n = Signal(bool(0))
    uart_rxd = Signal(bool(0))
    uart_cts = Signal(bool(0))

    # sources and sinks
    mii_select = Signal(bool(0))

    gmii_source = gmii_ep.GMIISource()

    gmii_source_logic = gmii_source.create_logic(phy_rx_clk,
                                                 rst,
                                                 txd=phy_rxd,
                                                 tx_en=phy_rx_dv,
                                                 tx_er=phy_rx_er,
                                                 mii_select=mii_select,
                                                 name='gmii_source')

    gmii_sink = gmii_ep.GMIISink()

    gmii_sink_logic = gmii_sink.create_logic(phy_tx_clk,
                                             rst,
                                             rxd=phy_txd,
                                             rx_dv=phy_tx_en,
                                             rx_er=phy_tx_er,
                                             mii_select=mii_select,
                                             name='gmii_sink')

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

    dut = Cosimulation("vvp -m myhdl %s.vvp -lxt2" % testbench,
                       clk_125mhz=clk_125mhz,
                       rst_125mhz=rst_125mhz,
                       current_test=current_test,
                       btnu=btnu,
                       btnl=btnl,
                       btnd=btnd,
                       btnr=btnr,
                       btnc=btnc,
                       sw=sw,
                       ledu=ledu,
                       ledl=ledl,
                       ledd=ledd,
                       ledr=ledr,
                       ledc=ledc,
                       led=led,
                       phy_rx_clk=phy_rx_clk,
                       phy_rxd=phy_rxd,
                       phy_rx_dv=phy_rx_dv,
                       phy_rx_er=phy_rx_er,
                       phy_gtx_clk=phy_gtx_clk,
                       phy_tx_clk=phy_tx_clk,
                       phy_txd=phy_txd,
                       phy_tx_en=phy_tx_en,
                       phy_tx_er=phy_tx_er,
                       phy_reset_n=phy_reset_n,
                       uart_rxd=uart_rxd,
                       uart_txd=uart_txd,
                       uart_rts=uart_rts,
                       uart_cts=uart_cts)

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

    rx_clk_hp = Signal(int(4))

    @instance
    def rx_clk_gen():
        while True:
            yield delay(int(rx_clk_hp))
            phy_rx_clk.next = not phy_rx_clk
            phy_tx_clk.next = not phy_tx_clk

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

        # testbench stimulus

        yield clk.posedge
        print("test 1: test UDP RX packet")
        current_test.next = 1

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80181
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 5678
        test_frame.udp_dest_port = 1234
        test_frame.payload = bytearray(range(32))
        test_frame.build()

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         test_frame.build_eth().build_axis_fcs().data)

        # wait for ARP request packet
        while gmii_sink.empty():
            yield clk.posedge

        rx_frame = gmii_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = arp_ep.ARPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xFFFFFFFFFFFF
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0806
        assert check_frame.arp_htype == 0x0001
        assert check_frame.arp_ptype == 0x0800
        assert check_frame.arp_hlen == 6
        assert check_frame.arp_plen == 4
        assert check_frame.arp_oper == 1
        assert check_frame.arp_sha == 0x020000000000
        assert check_frame.arp_spa == 0xc0a80180
        assert check_frame.arp_tha == 0x000000000000
        assert check_frame.arp_tpa == 0xc0a80181

        # generate response
        arp_frame = arp_ep.ARPFrame()
        arp_frame.eth_dest_mac = 0x020000000000
        arp_frame.eth_src_mac = 0xDAD1D2D3D4D5
        arp_frame.eth_type = 0x0806
        arp_frame.arp_htype = 0x0001
        arp_frame.arp_ptype = 0x0800
        arp_frame.arp_hlen = 6
        arp_frame.arp_plen = 4
        arp_frame.arp_oper = 2
        arp_frame.arp_sha = 0xDAD1D2D3D4D5
        arp_frame.arp_spa = 0xc0a80181
        arp_frame.arp_tha = 0x020000000000
        arp_frame.arp_tpa = 0xc0a80180

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         arp_frame.build_eth().build_axis_fcs().data)

        while gmii_sink.empty():
            yield clk.posedge

        rx_frame = gmii_sink.recv()
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xDAD1D2D3D4D5
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0800
        assert check_frame.ip_version == 4
        assert check_frame.ip_ihl == 5
        assert check_frame.ip_dscp == 0
        assert check_frame.ip_ecn == 0
        assert check_frame.ip_identification == 0
        assert check_frame.ip_flags == 2
        assert check_frame.ip_fragment_offset == 0
        assert check_frame.ip_ttl == 64
        assert check_frame.ip_protocol == 0x11
        assert check_frame.ip_source_ip == 0xc0a80180
        assert check_frame.ip_dest_ip == 0xc0a80181
        assert check_frame.udp_source_port == 1234
        assert check_frame.udp_dest_port == 5678
        assert check_frame.payload.data == bytearray(range(32))

        assert gmii_source.empty()
        assert gmii_sink.empty()

        yield delay(100)

        raise StopSimulation

    return instances()
Ejemplo n.º 7
0
def bench():

    # Parameters
    ENABLE_PADDING = 1
    MIN_FRAME_LENGTH = 64
    TX_FIFO_ADDR_WIDTH = 9
    RX_FIFO_ADDR_WIDTH = 9

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

    rx_clk = Signal(bool(0))
    rx_rst = Signal(bool(0))
    tx_clk = Signal(bool(0))
    tx_rst = Signal(bool(0))
    logic_clk = Signal(bool(0))
    logic_rst = Signal(bool(0))
    tx_axis_tdata = Signal(intbv(0)[8:])
    tx_axis_tvalid = Signal(bool(0))
    tx_axis_tlast = Signal(bool(0))
    tx_axis_tuser = Signal(bool(0))
    rx_axis_tready = Signal(bool(0))
    gmii_rxd = Signal(intbv(0)[8:])
    gmii_rx_dv = Signal(bool(0))
    gmii_rx_er = Signal(bool(0))
    rx_clk_enable = Signal(bool(1))
    tx_clk_enable = Signal(bool(1))
    rx_mii_select = Signal(bool(0))
    tx_mii_select = Signal(bool(0))
    ifg_delay = Signal(intbv(0)[8:])

    # Outputs
    tx_axis_tready = Signal(bool(0))
    rx_axis_tdata = Signal(intbv(0)[8:])
    rx_axis_tvalid = Signal(bool(0))
    rx_axis_tlast = Signal(bool(0))
    rx_axis_tuser = Signal(bool(0))
    gmii_txd = Signal(intbv(0)[8:])
    gmii_tx_en = Signal(bool(0))
    gmii_tx_er = Signal(bool(0))
    tx_fifo_overflow = Signal(bool(0))
    tx_fifo_bad_frame = Signal(bool(0))
    tx_fifo_good_frame = Signal(bool(0))
    rx_error_bad_frame = Signal(bool(0))
    rx_error_bad_fcs = Signal(bool(0))
    rx_fifo_overflow = Signal(bool(0))
    rx_fifo_bad_frame = Signal(bool(0))
    rx_fifo_good_frame = Signal(bool(0))

    # sources and sinks
    axis_source_pause = Signal(bool(0))
    axis_sink_pause = Signal(bool(0))

    gmii_source = gmii_ep.GMIISource()

    gmii_source_logic = gmii_source.create_logic(rx_clk,
                                                 rx_rst,
                                                 txd=gmii_rxd,
                                                 tx_en=gmii_rx_dv,
                                                 tx_er=gmii_rx_er,
                                                 clk_enable=rx_clk_enable,
                                                 mii_select=rx_mii_select,
                                                 name='gmii_source')

    gmii_sink = gmii_ep.GMIISink()

    gmii_sink_logic = gmii_sink.create_logic(tx_clk,
                                             tx_rst,
                                             rxd=gmii_txd,
                                             rx_dv=gmii_tx_en,
                                             rx_er=gmii_tx_er,
                                             clk_enable=tx_clk_enable,
                                             mii_select=tx_mii_select,
                                             name='gmii_sink')

    axis_source = axis_ep.AXIStreamSource()

    axis_source_logic = axis_source.create_logic(logic_clk,
                                                 logic_rst,
                                                 tdata=tx_axis_tdata,
                                                 tvalid=tx_axis_tvalid,
                                                 tready=tx_axis_tready,
                                                 tlast=tx_axis_tlast,
                                                 tuser=tx_axis_tuser,
                                                 pause=axis_source_pause,
                                                 name='axis_source')

    axis_sink = axis_ep.AXIStreamSink()

    axis_sink_logic = axis_sink.create_logic(logic_clk,
                                             logic_rst,
                                             tdata=rx_axis_tdata,
                                             tvalid=rx_axis_tvalid,
                                             tready=rx_axis_tready,
                                             tlast=rx_axis_tlast,
                                             tuser=rx_axis_tuser,
                                             pause=axis_sink_pause,
                                             name='axis_sink')

    # 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,
                       rx_clk=rx_clk,
                       rx_rst=rx_rst,
                       tx_clk=tx_clk,
                       tx_rst=tx_rst,
                       logic_clk=logic_clk,
                       logic_rst=logic_rst,
                       tx_axis_tdata=tx_axis_tdata,
                       tx_axis_tvalid=tx_axis_tvalid,
                       tx_axis_tready=tx_axis_tready,
                       tx_axis_tlast=tx_axis_tlast,
                       tx_axis_tuser=tx_axis_tuser,
                       rx_axis_tdata=rx_axis_tdata,
                       rx_axis_tready=rx_axis_tready,
                       rx_axis_tvalid=rx_axis_tvalid,
                       rx_axis_tlast=rx_axis_tlast,
                       rx_axis_tuser=rx_axis_tuser,
                       gmii_rxd=gmii_rxd,
                       gmii_rx_dv=gmii_rx_dv,
                       gmii_rx_er=gmii_rx_er,
                       gmii_txd=gmii_txd,
                       gmii_tx_en=gmii_tx_en,
                       gmii_tx_er=gmii_tx_er,
                       rx_clk_enable=rx_clk_enable,
                       tx_clk_enable=tx_clk_enable,
                       rx_mii_select=rx_mii_select,
                       tx_mii_select=tx_mii_select,
                       tx_fifo_overflow=tx_fifo_overflow,
                       tx_fifo_bad_frame=tx_fifo_bad_frame,
                       tx_fifo_good_frame=tx_fifo_good_frame,
                       rx_error_bad_frame=rx_error_bad_frame,
                       rx_error_bad_fcs=rx_error_bad_fcs,
                       rx_fifo_overflow=rx_fifo_overflow,
                       rx_fifo_bad_frame=rx_fifo_bad_frame,
                       rx_fifo_good_frame=rx_fifo_good_frame,
                       ifg_delay=ifg_delay)

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

    rx_error_bad_frame_asserted = Signal(bool(0))
    rx_error_bad_fcs_asserted = Signal(bool(0))

    @always(clk.posedge)
    def monitor():
        if (rx_error_bad_frame):
            rx_error_bad_frame_asserted.next = 1
        if (rx_error_bad_fcs):
            rx_error_bad_fcs_asserted.next = 1

    clk_enable_rate = Signal(int(0))
    clk_enable_div = Signal(int(0))

    @always(clk.posedge)
    def clk_enable_gen():
        if clk_enable_div.next > 0:
            rx_clk_enable.next = 0
            tx_clk_enable.next = 0
            clk_enable_div.next = clk_enable_div - 1
        else:
            rx_clk_enable.next = 1
            tx_clk_enable.next = 1
            clk_enable_div.next = clk_enable_rate - 1

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

        ifg_delay.next = 12

        # testbench stimulus

        for rate, mii in [(1, 0), (10, 0), (5, 1)]:
            clk_enable_rate.next = rate
            rx_mii_select.next = mii
            tx_mii_select.next = mii

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

            test_frame = eth_ep.EthFrame()
            test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame.eth_src_mac = 0x5A5152535455
            test_frame.eth_type = 0x8000
            test_frame.payload = bytearray(range(32))
            test_frame.update_fcs()

            axis_frame = test_frame.build_axis_fcs()

            gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                             bytearray(axis_frame))

            yield axis_sink.wait()
            rx_frame = axis_sink.recv()

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis(rx_frame)
            eth_frame.update_fcs()

            assert eth_frame == test_frame

            yield delay(100)

            yield clk.posedge
            print("test 2: test tx packet")
            current_test.next = 2

            test_frame = eth_ep.EthFrame()
            test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame.eth_src_mac = 0x5A5152535455
            test_frame.eth_type = 0x8000
            test_frame.payload = bytearray(range(32))
            test_frame.update_fcs()

            axis_frame = test_frame.build_axis()

            axis_source.send(axis_frame)

            yield gmii_sink.wait()
            rx_frame = gmii_sink.recv()

            assert rx_frame.data[0:8] == bytearray(
                b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == 46
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame.eth_src_mac
            assert eth_frame.eth_type == test_frame.eth_type
            assert eth_frame.payload.data.index(test_frame.payload.data) == 0

            yield delay(100)

        raise StopSimulation

    return instances()
Ejemplo n.º 8
0
def bench():

    # Parameters
    DATA_WIDTH = 8
    ENABLE_PADDING = 1
    MIN_FRAME_LENGTH = 64
    PTP_TS_ENABLE = 0
    PTP_TS_WIDTH = 96
    PTP_TAG_ENABLE = PTP_TS_ENABLE
    PTP_TAG_WIDTH = 16
    USER_WIDTH = (PTP_TAG_WIDTH if PTP_TAG_ENABLE else 0) + 1

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

    s_axis_tdata = Signal(intbv(0)[DATA_WIDTH:])
    s_axis_tvalid = Signal(bool(0))
    s_axis_tlast = Signal(bool(0))
    s_axis_tuser = Signal(intbv(0)[USER_WIDTH:])
    ptp_ts = Signal(intbv(0)[PTP_TS_WIDTH:])
    clk_enable = Signal(bool(1))
    mii_select = Signal(bool(0))
    ifg_delay = Signal(intbv(0)[8:])

    # Outputs
    s_axis_tready = Signal(bool(0))
    gmii_txd = Signal(intbv(0)[DATA_WIDTH:])
    gmii_tx_en = Signal(bool(0))
    gmii_tx_er = Signal(bool(0))
    m_axis_ptp_ts = Signal(intbv(0)[PTP_TS_WIDTH:])
    m_axis_ptp_ts_tag = Signal(intbv(0)[PTP_TAG_WIDTH:])
    m_axis_ptp_ts_valid = Signal(bool(0))
    start_packet = Signal(bool(0))
    error_underflow = Signal(bool(0))

    # sources and sinks
    source_pause = Signal(bool(0))

    source = axis_ep.AXIStreamSource()

    source_logic = source.create_logic(clk,
                                       rst,
                                       tdata=s_axis_tdata,
                                       tvalid=s_axis_tvalid,
                                       tready=s_axis_tready,
                                       tlast=s_axis_tlast,
                                       tuser=s_axis_tuser,
                                       pause=source_pause,
                                       name='source')

    sink = gmii_ep.GMIISink()

    sink_logic = sink.create_logic(clk,
                                   rst,
                                   rxd=gmii_txd,
                                   rx_dv=gmii_tx_en,
                                   rx_er=gmii_tx_er,
                                   clk_enable=clk_enable,
                                   mii_select=mii_select,
                                   name='sink')

    # 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,
                       s_axis_tdata=s_axis_tdata,
                       s_axis_tvalid=s_axis_tvalid,
                       s_axis_tready=s_axis_tready,
                       s_axis_tlast=s_axis_tlast,
                       s_axis_tuser=s_axis_tuser,
                       gmii_txd=gmii_txd,
                       gmii_tx_en=gmii_tx_en,
                       gmii_tx_er=gmii_tx_er,
                       ptp_ts=ptp_ts,
                       m_axis_ptp_ts=m_axis_ptp_ts,
                       m_axis_ptp_ts_tag=m_axis_ptp_ts_tag,
                       m_axis_ptp_ts_valid=m_axis_ptp_ts_valid,
                       clk_enable=clk_enable,
                       mii_select=mii_select,
                       ifg_delay=ifg_delay,
                       start_packet=start_packet,
                       error_underflow=error_underflow)

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

    clk_enable_rate = Signal(int(1))
    clk_enable_div = Signal(int(0))

    @always(clk.posedge)
    def clk_enable_gen():
        if clk_enable_div.next > 0:
            clk_enable.next = 0
            clk_enable_div.next = clk_enable_div - 1
        else:
            clk_enable.next = 1
            clk_enable_div.next = clk_enable_rate - 1

    @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

        ifg_delay.next = 12

        # testbench stimulus

        for rate, mii in [(1, 0), (10, 0), (5, 1)]:
            clk_enable_rate.next = rate
            mii_select.next = mii

            yield delay(100)

            for payload_len in list(range(1, 18)) + list(range(64, 82)):
                yield clk.posedge
                print("test 1: test packet, length %d" % payload_len)
                current_test.next = 1

                test_frame = eth_ep.EthFrame()
                test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
                test_frame.eth_src_mac = 0x5A5152535455
                test_frame.eth_type = 0x8000
                test_frame.payload = bytearray(range(payload_len))
                test_frame.update_fcs()

                axis_frame = test_frame.build_axis()

                source.send(axis_frame)

                yield sink.wait()
                rx_frame = sink.recv()

                assert rx_frame.data[0:8] == bytearray(
                    b'\x55\x55\x55\x55\x55\x55\x55\xD5')

                eth_frame = eth_ep.EthFrame()
                eth_frame.parse_axis_fcs(rx_frame.data[8:])

                print(hex(eth_frame.eth_fcs))
                print(hex(eth_frame.calc_fcs()))

                assert len(eth_frame.payload.data) == max(payload_len, 46)
                assert eth_frame.eth_fcs == eth_frame.calc_fcs()
                assert eth_frame.eth_dest_mac == test_frame.eth_dest_mac
                assert eth_frame.eth_src_mac == test_frame.eth_src_mac
                assert eth_frame.eth_type == test_frame.eth_type
                assert eth_frame.payload.data.index(
                    test_frame.payload.data) == 0

                assert sink.empty()

                yield delay(100)

                yield clk.posedge
                print("test 2: back-to-back packets, length %d" % payload_len)
                current_test.next = 2

                test_frame1 = eth_ep.EthFrame()
                test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5
                test_frame1.eth_src_mac = 0x5A5152535455
                test_frame1.eth_type = 0x8000
                test_frame1.payload = bytearray(range(payload_len))
                test_frame1.update_fcs()
                test_frame2 = eth_ep.EthFrame()
                test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5
                test_frame2.eth_src_mac = 0x5A5152535455
                test_frame2.eth_type = 0x8000
                test_frame2.payload = bytearray(range(payload_len))
                test_frame2.update_fcs()

                axis_frame1 = test_frame1.build_axis()
                axis_frame2 = test_frame2.build_axis()

                source.send(axis_frame1)
                source.send(axis_frame2)

                yield sink.wait()
                rx_frame = sink.recv()

                assert rx_frame.data[0:8] == bytearray(
                    b'\x55\x55\x55\x55\x55\x55\x55\xD5')

                eth_frame = eth_ep.EthFrame()
                eth_frame.parse_axis_fcs(rx_frame.data[8:])

                print(hex(eth_frame.eth_fcs))
                print(hex(eth_frame.calc_fcs()))

                assert len(eth_frame.payload.data) == max(payload_len, 46)
                assert eth_frame.eth_fcs == eth_frame.calc_fcs()
                assert eth_frame.eth_dest_mac == test_frame1.eth_dest_mac
                assert eth_frame.eth_src_mac == test_frame1.eth_src_mac
                assert eth_frame.eth_type == test_frame1.eth_type
                assert eth_frame.payload.data.index(
                    test_frame1.payload.data) == 0

                yield sink.wait()
                rx_frame = sink.recv()

                assert rx_frame.data[0:8] == bytearray(
                    b'\x55\x55\x55\x55\x55\x55\x55\xD5')

                eth_frame = eth_ep.EthFrame()
                eth_frame.parse_axis_fcs(rx_frame.data[8:])

                print(hex(eth_frame.eth_fcs))
                print(hex(eth_frame.calc_fcs()))

                assert len(eth_frame.payload.data) == max(payload_len, 46)
                assert eth_frame.eth_fcs == eth_frame.calc_fcs()
                assert eth_frame.eth_dest_mac == test_frame2.eth_dest_mac
                assert eth_frame.eth_src_mac == test_frame2.eth_src_mac
                assert eth_frame.eth_type == test_frame2.eth_type
                assert eth_frame.payload.data.index(
                    test_frame2.payload.data) == 0

                assert sink.empty()

                yield delay(100)

                yield clk.posedge
                print("test 3: tuser assert, length %d" % payload_len)
                current_test.next = 3

                test_frame1 = eth_ep.EthFrame()
                test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5
                test_frame1.eth_src_mac = 0x5A5152535455
                test_frame1.eth_type = 0x8000
                test_frame1.payload = bytearray(range(payload_len))
                test_frame1.update_fcs()
                test_frame2 = eth_ep.EthFrame()
                test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5
                test_frame2.eth_src_mac = 0x5A5152535455
                test_frame2.eth_type = 0x8000
                test_frame2.payload = bytearray(range(payload_len))
                test_frame2.update_fcs()

                axis_frame1 = test_frame1.build_axis()
                axis_frame2 = test_frame2.build_axis()

                axis_frame1.user = 1

                source.send(axis_frame1)
                source.send(axis_frame2)

                yield sink.wait()
                rx_frame = sink.recv()

                assert rx_frame.data[0:8] == bytearray(
                    b'\x55\x55\x55\x55\x55\x55\x55\xD5')
                assert rx_frame.error[-1]

                # bad packet

                yield sink.wait()
                rx_frame = sink.recv()

                assert rx_frame.data[0:8] == bytearray(
                    b'\x55\x55\x55\x55\x55\x55\x55\xD5')

                eth_frame = eth_ep.EthFrame()
                eth_frame.parse_axis_fcs(rx_frame.data[8:])

                print(hex(eth_frame.eth_fcs))
                print(hex(eth_frame.calc_fcs()))

                assert len(eth_frame.payload.data) == max(payload_len, 46)
                assert eth_frame.eth_fcs == eth_frame.calc_fcs()
                assert eth_frame.eth_dest_mac == test_frame2.eth_dest_mac
                assert eth_frame.eth_src_mac == test_frame2.eth_src_mac
                assert eth_frame.eth_type == test_frame2.eth_type
                assert eth_frame.payload.data.index(
                    test_frame2.payload.data) == 0

                assert sink.empty()

                yield delay(100)

        raise StopSimulation

    return instances()
Ejemplo n.º 9
0
def bench():

    # Parameters
    ENABLE_PADDING = 1
    MIN_FRAME_LENGTH = 64
    TX_FIFO_ADDR_WIDTH = 9
    RX_FIFO_ADDR_WIDTH = 9

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

    rx_clk = Signal(bool(0))
    rx_rst = Signal(bool(0))
    tx_clk = Signal(bool(0))
    tx_rst = Signal(bool(0))
    logic_clk = Signal(bool(0))
    logic_rst = Signal(bool(0))
    tx_axis_tdata = Signal(intbv(0)[8:])
    tx_axis_tvalid = Signal(bool(0))
    tx_axis_tlast = Signal(bool(0))
    tx_axis_tuser = Signal(bool(0))
    rx_axis_tready = Signal(bool(0))
    gmii_rxd = Signal(intbv(0)[8:])
    gmii_rx_dv = Signal(bool(0))
    gmii_rx_er = Signal(bool(0))
    ifg_delay = Signal(intbv(0)[8:])

    # Outputs
    tx_axis_tready = Signal(bool(0))
    rx_axis_tdata = Signal(intbv(0)[8:])
    rx_axis_tvalid = Signal(bool(0))
    rx_axis_tlast = Signal(bool(0))
    rx_axis_tuser = Signal(bool(0))
    gmii_txd = Signal(intbv(0)[8:])
    gmii_tx_en = Signal(bool(0))
    gmii_tx_er = Signal(bool(0))
    tx_fifo_overflow = Signal(bool(0))
    tx_fifo_bad_frame = Signal(bool(0))
    tx_fifo_good_frame = Signal(bool(0))
    rx_error_bad_frame = Signal(bool(0))
    rx_error_bad_fcs = Signal(bool(0))
    rx_fifo_overflow = Signal(bool(0))
    rx_fifo_bad_frame = Signal(bool(0))
    rx_fifo_good_frame = Signal(bool(0))

    # sources and sinks
    gmii_source_queue = Queue()
    gmii_sink_queue = Queue()
    axis_source_queue = Queue()
    axis_source_pause = Signal(bool(0))
    axis_sink_queue = Queue()

    gmii_source = gmii_ep.GMIISource(rx_clk,
                                     rx_rst,
                                     txd=gmii_rxd,
                                     tx_en=gmii_rx_dv,
                                     tx_er=gmii_rx_er,
                                     fifo=gmii_source_queue,
                                     name='gmii_source')

    gmii_sink = gmii_ep.GMIISink(tx_clk,
                                 tx_rst,
                                 rxd=gmii_txd,
                                 rx_dv=gmii_tx_en,
                                 rx_er=gmii_tx_er,
                                 fifo=gmii_sink_queue,
                                 name='gmii_sink')

    axis_source = axis_ep.AXIStreamSource(tx_clk,
                                          tx_rst,
                                          tdata=tx_axis_tdata,
                                          tvalid=tx_axis_tvalid,
                                          tready=tx_axis_tready,
                                          tlast=tx_axis_tlast,
                                          tuser=tx_axis_tuser,
                                          fifo=axis_source_queue,
                                          pause=axis_source_pause,
                                          name='axis_source')

    axis_sink = axis_ep.AXIStreamSink(rx_clk,
                                      rx_rst,
                                      tdata=rx_axis_tdata,
                                      tvalid=rx_axis_tvalid,
                                      tready=rx_axis_tready,
                                      tlast=rx_axis_tlast,
                                      tuser=rx_axis_tuser,
                                      fifo=axis_sink_queue,
                                      name='axis_sink')

    # DUT
    dut = dut_eth_mac_1g(
        clk, rst, current_test, rx_clk, rx_rst, tx_clk, tx_rst, logic_clk,
        logic_rst, tx_axis_tdata, tx_axis_tvalid, tx_axis_tready,
        tx_axis_tlast, tx_axis_tuser, rx_axis_tdata, rx_axis_tvalid,
        rx_axis_tready, rx_axis_tlast, rx_axis_tuser, gmii_rxd, gmii_rx_dv,
        gmii_rx_er, gmii_txd, gmii_tx_en, gmii_tx_er, tx_fifo_overflow,
        tx_fifo_bad_frame, tx_fifo_good_frame, rx_error_bad_frame,
        rx_error_bad_fcs, rx_fifo_overflow, rx_fifo_bad_frame,
        rx_fifo_good_frame, ifg_delay)

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

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

        ifg_delay.next = 12

        # testbench stimulus

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

        test_frame = eth_ep.EthFrame()
        test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
        test_frame.eth_src_mac = 0x5A5152535455
        test_frame.eth_type = 0x8000
        test_frame.payload = bytearray(range(32))
        test_frame.update_fcs()

        axis_frame = test_frame.build_axis_fcs()

        gmii_source_queue.put(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                              bytearray(axis_frame))
        yield clk.posedge
        yield clk.posedge

        while gmii_rx_dv:
            yield clk.posedge

        yield delay(100)

        while rx_axis_tvalid:
            yield clk.posedge

        yield clk.posedge
        yield clk.posedge

        rx_frame = None
        if not axis_sink_queue.empty():
            rx_frame = axis_sink_queue.get()

        eth_frame = eth_ep.EthFrame()
        eth_frame.parse_axis(rx_frame)
        eth_frame.update_fcs()

        assert eth_frame == test_frame

        yield delay(100)

        yield clk.posedge
        print("test 2: test tx packet")
        current_test.next = 2

        test_frame = eth_ep.EthFrame()
        test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
        test_frame.eth_src_mac = 0x5A5152535455
        test_frame.eth_type = 0x8000
        test_frame.payload = bytearray(range(32))
        test_frame.update_fcs()

        axis_frame = test_frame.build_axis()

        axis_source_queue.put(axis_frame)
        yield clk.posedge
        yield clk.posedge

        while tx_axis_tvalid:
            yield clk.posedge

        yield delay(100)

        while gmii_tx_en:
            yield clk.posedge

        yield clk.posedge
        yield clk.posedge

        rx_frame = None
        if not gmii_sink_queue.empty():
            rx_frame = gmii_sink_queue.get()

        assert rx_frame.data[0:8] == bytearray(
            b'\x55\x55\x55\x55\x55\x55\x55\xD5')

        eth_frame = eth_ep.EthFrame()
        eth_frame.parse_axis_fcs(rx_frame.data[8:])

        print(hex(eth_frame.eth_fcs))
        print(hex(eth_frame.calc_fcs()))

        assert len(eth_frame.payload.data) == 46
        assert eth_frame.eth_fcs == eth_frame.calc_fcs()
        assert eth_frame.eth_dest_mac == test_frame.eth_dest_mac
        assert eth_frame.eth_src_mac == test_frame.eth_src_mac
        assert eth_frame.eth_type == test_frame.eth_type
        assert eth_frame.payload.data.index(test_frame.payload.data) == 0

        yield delay(100)

        raise StopSimulation

    return dut, axis_source, axis_sink, gmii_source, gmii_sink, clkgen, check
Ejemplo n.º 10
0
def bench():

    # Parameters
    TARGET = "SIM"
    IODDR_STYLE = "IODDR2"
    CLOCK_INPUT_STYLE = "BUFIO2"
    ENABLE_PADDING = 1
    MIN_FRAME_LENGTH = 64

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

    gtx_clk = Signal(bool(0))
    gtx_rst = Signal(bool(0))
    tx_axis_tdata = Signal(intbv(0)[8:])
    tx_axis_tvalid = Signal(bool(0))
    tx_axis_tlast = Signal(bool(0))
    tx_axis_tuser = Signal(bool(0))
    gmii_rx_clk = Signal(bool(0))
    gmii_rxd = Signal(intbv(0)[8:])
    gmii_rx_dv = Signal(bool(0))
    gmii_rx_er = Signal(bool(0))
    mii_tx_clk = Signal(bool(0))
    ifg_delay = Signal(intbv(0)[8:])

    # Outputs
    rx_clk = Signal(bool(0))
    rx_rst = Signal(bool(0))
    tx_clk = Signal(bool(0))
    tx_rst = Signal(bool(0))
    tx_axis_tready = Signal(bool(0))
    rx_axis_tdata = Signal(intbv(0)[8:])
    rx_axis_tvalid = Signal(bool(0))
    rx_axis_tlast = Signal(bool(0))
    rx_axis_tuser = Signal(bool(0))
    gmii_tx_clk = Signal(bool(0))
    gmii_txd = Signal(intbv(0)[8:])
    gmii_tx_en = Signal(bool(0))
    gmii_tx_er = Signal(bool(0))
    rx_error_bad_frame = Signal(bool(0))
    rx_error_bad_fcs = Signal(bool(0))
    speed = Signal(intbv(0)[2:])

    # sources and sinks
    axis_source_pause = Signal(bool(0))

    mii_select = Signal(bool(0))

    gmii_source = gmii_ep.GMIISource()

    gmii_source_logic = gmii_source.create_logic(
        gmii_rx_clk,
        rst,
        txd=gmii_rxd,
        tx_en=gmii_rx_dv,
        tx_er=gmii_rx_er,
        mii_select=mii_select,
        name='gmii_source'
    )

    gmii_sink = gmii_ep.GMIISink()

    gmii_sink_logic = gmii_sink.create_logic(
        gmii_tx_clk,
        rst,
        rxd=gmii_txd,
        rx_dv=gmii_tx_en,
        rx_er=gmii_tx_er,
        mii_select=mii_select,
        name='gmii_sink'
    )

    axis_source = axis_ep.AXIStreamSource()

    axis_source_logic = axis_source.create_logic(
        tx_clk,
        tx_rst,
        tdata=tx_axis_tdata,
        tvalid=tx_axis_tvalid,
        tready=tx_axis_tready,
        tlast=tx_axis_tlast,
        tuser=tx_axis_tuser,
        pause=axis_source_pause,
        name='axis_source'
    )

    axis_sink = axis_ep.AXIStreamSink()

    axis_sink_logic = axis_sink.create_logic(
        gmii_rx_clk,
        rx_rst,
        tdata=rx_axis_tdata,
        tvalid=rx_axis_tvalid,
        tlast=rx_axis_tlast,
        tuser=rx_axis_tuser,
        name='axis_sink'
    )

    # 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,

        gtx_clk=gtx_clk,
        gtx_rst=gtx_rst,

        rx_clk=rx_clk,
        rx_rst=rx_rst,
        tx_clk=tx_clk,
        tx_rst=tx_rst,

        tx_axis_tdata=tx_axis_tdata,
        tx_axis_tvalid=tx_axis_tvalid,
        tx_axis_tready=tx_axis_tready,
        tx_axis_tlast=tx_axis_tlast,
        tx_axis_tuser=tx_axis_tuser,

        rx_axis_tdata=rx_axis_tdata,
        rx_axis_tvalid=rx_axis_tvalid,
        rx_axis_tlast=rx_axis_tlast,
        rx_axis_tuser=rx_axis_tuser,

        gmii_rx_clk=gmii_rx_clk,
        gmii_rxd=gmii_rxd,
        gmii_rx_dv=gmii_rx_dv,
        gmii_rx_er=gmii_rx_er,

        gmii_tx_clk=gmii_tx_clk,
        mii_tx_clk=mii_tx_clk,
        gmii_txd=gmii_txd,
        gmii_tx_en=gmii_tx_en,
        gmii_tx_er=gmii_tx_er,

        rx_error_bad_frame=rx_error_bad_frame,
        rx_error_bad_fcs=rx_error_bad_fcs,
        speed=speed,

        ifg_delay=ifg_delay
    )

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

    rx_clk_hp = Signal(int(4))

    @instance
    def rx_clk_gen():
        while True:
            yield delay(int(rx_clk_hp))
            gmii_rx_clk.next = not gmii_rx_clk
            mii_tx_clk.next = not gmii_rx_clk

    rx_error_bad_frame_asserted = Signal(bool(0))
    rx_error_bad_fcs_asserted = Signal(bool(0))

    @always(clk.posedge)
    def monitor():
        if (rx_error_bad_frame):
            rx_error_bad_frame_asserted.next = 1
        if (rx_error_bad_fcs):
            rx_error_bad_fcs_asserted.next = 1

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

        ifg_delay.next = 12

        # testbench stimulus

        for rate, mii in [(4, 0), (20, 1), (200, 1)]:
            rx_clk_hp.next = rate
            mii_select.next = mii

            yield delay(1000)

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

            test_frame = eth_ep.EthFrame()
            test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame.eth_src_mac = 0x5A5152535455
            test_frame.eth_type = 0x8000
            test_frame.payload = bytearray(range(32))
            test_frame.update_fcs()

            axis_frame = test_frame.build_axis_fcs()

            gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5'+bytearray(axis_frame))
            yield gmii_rx_clk.posedge
            yield gmii_rx_clk.posedge

            while not (gmii_rx_dv or gmii_tx_en):
                yield gmii_rx_clk.posedge
            yield gmii_rx_clk.posedge

            while gmii_rx_dv or gmii_tx_en or tx_axis_tvalid or rx_axis_tvalid:
                yield gmii_rx_clk.posedge

            yield gmii_rx_clk.posedge
            yield gmii_rx_clk.posedge
            yield gmii_rx_clk.posedge

            rx_frame = axis_sink.recv()

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis(rx_frame)
            eth_frame.update_fcs()

            assert eth_frame == test_frame

            yield delay(100)

            yield clk.posedge
            print("test 2: test tx packet")
            current_test.next = 2

            test_frame = eth_ep.EthFrame()
            test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame.eth_src_mac = 0x5A5152535455
            test_frame.eth_type = 0x8000
            test_frame.payload = bytearray(range(32))
            test_frame.update_fcs()

            axis_frame = test_frame.build_axis()

            axis_source.send(axis_frame)
            yield gmii_rx_clk.posedge
            yield gmii_rx_clk.posedge

            while not (gmii_rx_dv or gmii_tx_en):
                yield gmii_rx_clk.posedge
            yield gmii_rx_clk.posedge

            while gmii_rx_dv or gmii_tx_en or tx_axis_tvalid or rx_axis_tvalid:
                yield gmii_rx_clk.posedge

            yield gmii_rx_clk.posedge
            yield gmii_rx_clk.posedge
            yield gmii_rx_clk.posedge

            rx_frame = gmii_sink.recv()

            assert rx_frame.data[0:8] == bytearray(b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == 46
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame.eth_src_mac
            assert eth_frame.eth_type == test_frame.eth_type
            assert eth_frame.payload.data.index(test_frame.payload.data) == 0

            yield delay(100)

        raise StopSimulation

    return dut, monitor, axis_source_logic, axis_sink_logic, gmii_source_logic, gmii_sink_logic, clkgen, rx_clk_gen, check
def bench():

    # Parameters
    ENABLE_PADDING = 1
    MIN_FRAME_LENGTH = 64

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

    input_axis_tdata = Signal(intbv(0)[8:])
    input_axis_tvalid = Signal(bool(0))
    input_axis_tlast = Signal(bool(0))
    input_axis_tuser = Signal(bool(0))
    ifg_delay = Signal(intbv(0)[8:])

    # Outputs
    input_axis_tready = Signal(bool(0))
    gmii_txd = Signal(intbv(0)[8:])
    gmii_tx_en = Signal(bool(0))
    gmii_tx_er = Signal(bool(0))

    # sources and sinks
    source_pause = Signal(bool(0))

    source = axis_ep.AXIStreamSource()

    source_logic = source.create_logic(clk,
                                       rst,
                                       tdata=input_axis_tdata,
                                       tvalid=input_axis_tvalid,
                                       tready=input_axis_tready,
                                       tlast=input_axis_tlast,
                                       tuser=input_axis_tuser,
                                       pause=source_pause,
                                       name='source')

    sink = gmii_ep.GMIISink()

    sink_logic = sink.create_logic(clk,
                                   rst,
                                   rxd=gmii_txd,
                                   rx_dv=gmii_tx_en,
                                   rx_er=gmii_tx_er,
                                   name='sink')

    # 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,
                       input_axis_tdata=input_axis_tdata,
                       input_axis_tvalid=input_axis_tvalid,
                       input_axis_tready=input_axis_tready,
                       input_axis_tlast=input_axis_tlast,
                       input_axis_tuser=input_axis_tuser,
                       gmii_txd=gmii_txd,
                       gmii_tx_en=gmii_tx_en,
                       gmii_tx_er=gmii_tx_er,
                       ifg_delay=ifg_delay)

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

    @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

        ifg_delay.next = 12

        # testbench stimulus

        for payload_len in list(range(1, 18)) + list(range(64, 82)):
            yield clk.posedge
            print("test 1: test packet, length %d" % payload_len)
            current_test.next = 1

            test_frame = eth_ep.EthFrame()
            test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame.eth_src_mac = 0x5A5152535455
            test_frame.eth_type = 0x8000
            test_frame.payload = bytearray(range(payload_len))
            test_frame.update_fcs()

            axis_frame = test_frame.build_axis()

            source.send(axis_frame)

            yield sink.wait()
            rx_frame = sink.recv()

            assert rx_frame.data[0:8] == bytearray(
                b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == max(payload_len, 46)
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame.eth_src_mac
            assert eth_frame.eth_type == test_frame.eth_type
            assert eth_frame.payload.data.index(test_frame.payload.data) == 0

            assert sink.empty()

            yield delay(100)

            yield clk.posedge
            print("test 2: back-to-back packets, length %d" % payload_len)
            current_test.next = 2

            test_frame1 = eth_ep.EthFrame()
            test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame1.eth_src_mac = 0x5A5152535455
            test_frame1.eth_type = 0x8000
            test_frame1.payload = bytearray(range(payload_len))
            test_frame1.update_fcs()
            test_frame2 = eth_ep.EthFrame()
            test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame2.eth_src_mac = 0x5A5152535455
            test_frame2.eth_type = 0x8000
            test_frame2.payload = bytearray(range(payload_len))
            test_frame2.update_fcs()

            axis_frame1 = test_frame1.build_axis()
            axis_frame2 = test_frame2.build_axis()

            source.send(axis_frame1)
            source.send(axis_frame2)

            yield sink.wait()
            rx_frame = sink.recv()

            assert rx_frame.data[0:8] == bytearray(
                b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == max(payload_len, 46)
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame1.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame1.eth_src_mac
            assert eth_frame.eth_type == test_frame1.eth_type
            assert eth_frame.payload.data.index(test_frame1.payload.data) == 0

            yield sink.wait()
            rx_frame = sink.recv()

            assert rx_frame.data[0:8] == bytearray(
                b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == max(payload_len, 46)
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame2.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame2.eth_src_mac
            assert eth_frame.eth_type == test_frame2.eth_type
            assert eth_frame.payload.data.index(test_frame2.payload.data) == 0

            assert sink.empty()

            yield delay(100)

            yield clk.posedge
            print("test 3: tuser assert, length %d" % payload_len)
            current_test.next = 3

            test_frame1 = eth_ep.EthFrame()
            test_frame1.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame1.eth_src_mac = 0x5A5152535455
            test_frame1.eth_type = 0x8000
            test_frame1.payload = bytearray(range(payload_len))
            test_frame1.update_fcs()
            test_frame2 = eth_ep.EthFrame()
            test_frame2.eth_dest_mac = 0xDAD1D2D3D4D5
            test_frame2.eth_src_mac = 0x5A5152535455
            test_frame2.eth_type = 0x8000
            test_frame2.payload = bytearray(range(payload_len))
            test_frame2.update_fcs()

            axis_frame1 = test_frame1.build_axis()
            axis_frame2 = test_frame2.build_axis()

            axis_frame1.last_cycle_user = 1

            source.send(axis_frame1)
            source.send(axis_frame2)

            yield sink.wait()
            rx_frame = sink.recv()

            assert rx_frame.data[0:8] == bytearray(
                b'\x55\x55\x55\x55\x55\x55\x55\xD5')
            assert rx_frame.error[-1]

            # bad packet

            yield sink.wait()
            rx_frame = sink.recv()

            assert rx_frame.data[0:8] == bytearray(
                b'\x55\x55\x55\x55\x55\x55\x55\xD5')

            eth_frame = eth_ep.EthFrame()
            eth_frame.parse_axis_fcs(rx_frame.data[8:])

            print(hex(eth_frame.eth_fcs))
            print(hex(eth_frame.calc_fcs()))

            assert len(eth_frame.payload.data) == max(payload_len, 46)
            assert eth_frame.eth_fcs == eth_frame.calc_fcs()
            assert eth_frame.eth_dest_mac == test_frame2.eth_dest_mac
            assert eth_frame.eth_src_mac == test_frame2.eth_src_mac
            assert eth_frame.eth_type == test_frame2.eth_type
            assert eth_frame.payload.data.index(test_frame2.payload.data) == 0

            assert sink.empty()

            yield delay(100)

        raise StopSimulation

    return instances()
Ejemplo n.º 12
0
def bench():

    # Parameters
    TARGET_XILINX = 1

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

    btnu = Signal(bool(0))
    btnl = Signal(bool(0))
    btnd = Signal(bool(0))
    btnr = Signal(bool(0))
    btnc = Signal(bool(0))
    sw = Signal(intbv(0)[8:])
    phy_rx_clk = Signal(bool(0))
    phy_rxd = Signal(intbv(0)[8:])
    phy_rx_dv = Signal(bool(0))
    phy_rx_er = Signal(bool(0))
    uart_rxd = Signal(bool(0))

    # Outputs
    led = Signal(intbv(0)[8:])
    phy_gtx_clk = Signal(bool(0))
    phy_txd = Signal(intbv(0)[8:])
    phy_tx_en = Signal(bool(0))
    phy_tx_er = Signal(bool(0))
    phy_reset_n = Signal(bool(0))
    uart_txd = Signal(bool(0))

    # sources and sinks
    gmii_source_queue = Queue()
    gmii_sink_queue = Queue()

    gmii_source = gmii_ep.GMIISource(phy_rx_clk,
                                     rst,
                                     txd=phy_rxd,
                                     tx_en=phy_rx_dv,
                                     tx_er=phy_rx_er,
                                     fifo=gmii_source_queue,
                                     name='gmii_source')

    gmii_sink = gmii_ep.GMIISink(phy_gtx_clk,
                                 rst,
                                 rxd=phy_txd,
                                 rx_dv=phy_tx_en,
                                 rx_er=phy_tx_er,
                                 fifo=gmii_sink_queue,
                                 name='gmii_sink')

    # DUT
    dut = dut_fpga_core(clk, rst, current_test, btnu, btnl, btnd, btnr, btnc,
                        sw, led, phy_rx_clk, phy_rxd, phy_rx_dv, phy_rx_er,
                        phy_gtx_clk, phy_txd, phy_tx_en, phy_tx_er,
                        phy_reset_n, uart_rxd, uart_txd)

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

    @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

        yield clk.posedge
        print("test 1: test UDP RX packet")
        current_test.next = 1

        test_frame = udp_ep.UDPFrame()
        test_frame.eth_dest_mac = 0x020000000000
        test_frame.eth_src_mac = 0xDAD1D2D3D4D5
        test_frame.eth_type = 0x0800
        test_frame.ip_version = 4
        test_frame.ip_ihl = 5
        test_frame.ip_dscp = 0
        test_frame.ip_ecn = 0
        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 = 0xc0a80181
        test_frame.ip_dest_ip = 0xc0a80180
        test_frame.udp_source_port = 5678
        test_frame.udp_dest_port = 1234
        test_frame.payload = bytearray(range(32))
        test_frame.build()

        gmii_source_queue.put(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                              test_frame.build_eth().build_axis_fcs().data)

        # wait for ARP request packet
        while gmii_sink_queue.empty():
            yield clk.posedge

        rx_frame = gmii_sink_queue.get(False)
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = arp_ep.ARPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xFFFFFFFFFFFF
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0806
        assert check_frame.arp_htype == 0x0001
        assert check_frame.arp_ptype == 0x0800
        assert check_frame.arp_hlen == 6
        assert check_frame.arp_plen == 4
        assert check_frame.arp_oper == 1
        assert check_frame.arp_sha == 0x020000000000
        assert check_frame.arp_spa == 0xc0a80180
        assert check_frame.arp_tha == 0x000000000000
        assert check_frame.arp_tpa == 0xc0a80181

        # generate response
        arp_frame = arp_ep.ARPFrame()
        arp_frame.eth_dest_mac = 0x020000000000
        arp_frame.eth_src_mac = 0xDAD1D2D3D4D5
        arp_frame.eth_type = 0x0806
        arp_frame.arp_htype = 0x0001
        arp_frame.arp_ptype = 0x0800
        arp_frame.arp_hlen = 6
        arp_frame.arp_plen = 4
        arp_frame.arp_oper = 2
        arp_frame.arp_sha = 0xDAD1D2D3D4D5
        arp_frame.arp_spa = 0xc0a80181
        arp_frame.arp_tha = 0x020000000000
        arp_frame.arp_tpa = 0xc0a80180

        gmii_source_queue.put(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                              arp_frame.build_eth().build_axis_fcs().data)

        while gmii_sink_queue.empty():
            yield clk.posedge

        rx_frame = gmii_sink_queue.get(False)
        check_eth_frame = eth_ep.EthFrame()
        check_eth_frame.parse_axis_fcs(rx_frame.data[8:])
        check_frame = udp_ep.UDPFrame()
        check_frame.parse_eth(check_eth_frame)

        print(check_frame)

        assert check_frame.eth_dest_mac == 0xDAD1D2D3D4D5
        assert check_frame.eth_src_mac == 0x020000000000
        assert check_frame.eth_type == 0x0800
        assert check_frame.ip_version == 4
        assert check_frame.ip_ihl == 5
        assert check_frame.ip_dscp == 0
        assert check_frame.ip_ecn == 0
        assert check_frame.ip_identification == 0
        assert check_frame.ip_flags == 2
        assert check_frame.ip_fragment_offset == 0
        assert check_frame.ip_ttl == 64
        assert check_frame.ip_protocol == 0x11
        assert check_frame.ip_source_ip == 0xc0a80180
        assert check_frame.ip_dest_ip == 0xc0a80181
        assert check_frame.udp_source_port == 1234
        assert check_frame.udp_dest_port == 5678
        assert check_frame.payload.data == bytearray(range(32))

        assert gmii_source_queue.empty()
        assert gmii_sink_queue.empty()

        yield delay(100)

        raise StopSimulation

    return dut, gmii_source, gmii_sink, clkgen, check
Ejemplo n.º 13
0
def bench():

    # Parameters
    ENABLE_PADDING = 1
    MIN_FRAME_LENGTH = 64

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

    rx_clk = Signal(bool(0))
    rx_rst = Signal(bool(0))
    tx_clk = Signal(bool(0))
    tx_rst = Signal(bool(0))
    tx_axis_tdata = Signal(intbv(0)[8:])
    tx_axis_tvalid = Signal(bool(0))
    tx_axis_tlast = Signal(bool(0))
    tx_axis_tuser = Signal(bool(0))
    gmii_rxd = Signal(intbv(0)[8:])
    gmii_rx_dv = Signal(bool(0))
    gmii_rx_er = Signal(bool(0))
    ifg_delay = Signal(intbv(0)[8:])

    # Outputs
    tx_axis_tready = Signal(bool(0))
    rx_axis_tdata = Signal(intbv(0)[8:])
    rx_axis_tvalid = Signal(bool(0))
    rx_axis_tlast = Signal(bool(0))
    rx_axis_tuser = Signal(bool(0))
    gmii_txd = Signal(intbv(0)[8:])
    gmii_tx_en = Signal(bool(0))
    gmii_tx_er = Signal(bool(0))
    rx_error_bad_frame = Signal(bool(0))
    rx_error_bad_fcs = Signal(bool(0))

    # sources and sinks
    axis_source_pause = Signal(bool(0))

    gmii_source = gmii_ep.GMIISource()

    gmii_source_logic = gmii_source.create_logic(clk,
                                                 rst,
                                                 txd=gmii_rxd,
                                                 tx_en=gmii_rx_dv,
                                                 tx_er=gmii_rx_er,
                                                 name='gmii_source')

    gmii_sink = gmii_ep.GMIISink()

    gmii_sink_logic = gmii_sink.create_logic(clk,
                                             rst,
                                             rxd=gmii_txd,
                                             rx_dv=gmii_tx_en,
                                             rx_er=gmii_tx_er,
                                             name='gmii_sink')

    axis_source = axis_ep.AXIStreamSource()

    axis_source_logic = axis_source.create_logic(clk,
                                                 rst,
                                                 tdata=tx_axis_tdata,
                                                 tvalid=tx_axis_tvalid,
                                                 tready=tx_axis_tready,
                                                 tlast=tx_axis_tlast,
                                                 tuser=tx_axis_tuser,
                                                 pause=axis_source_pause,
                                                 name='axis_source')

    axis_sink = axis_ep.AXIStreamSink()

    axis_sink_logic = axis_sink.create_logic(clk,
                                             rst,
                                             tdata=rx_axis_tdata,
                                             tvalid=rx_axis_tvalid,
                                             tlast=rx_axis_tlast,
                                             tuser=rx_axis_tuser,
                                             name='axis_sink')

    # 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,
                       rx_clk=rx_clk,
                       rx_rst=rx_rst,
                       tx_clk=tx_clk,
                       tx_rst=tx_rst,
                       tx_axis_tdata=tx_axis_tdata,
                       tx_axis_tvalid=tx_axis_tvalid,
                       tx_axis_tready=tx_axis_tready,
                       tx_axis_tlast=tx_axis_tlast,
                       tx_axis_tuser=tx_axis_tuser,
                       rx_axis_tdata=rx_axis_tdata,
                       rx_axis_tvalid=rx_axis_tvalid,
                       rx_axis_tlast=rx_axis_tlast,
                       rx_axis_tuser=rx_axis_tuser,
                       gmii_rxd=gmii_rxd,
                       gmii_rx_dv=gmii_rx_dv,
                       gmii_rx_er=gmii_rx_er,
                       gmii_txd=gmii_txd,
                       gmii_tx_en=gmii_tx_en,
                       gmii_tx_er=gmii_tx_er,
                       rx_error_bad_frame=rx_error_bad_frame,
                       rx_error_bad_fcs=rx_error_bad_fcs,
                       ifg_delay=ifg_delay)

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

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

        ifg_delay.next = 12

        # testbench stimulus

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

        test_frame = eth_ep.EthFrame()
        test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
        test_frame.eth_src_mac = 0x5A5152535455
        test_frame.eth_type = 0x8000
        test_frame.payload = bytearray(range(32))
        test_frame.update_fcs()

        axis_frame = test_frame.build_axis_fcs()

        gmii_source.send(b'\x55\x55\x55\x55\x55\x55\x55\xD5' +
                         bytearray(axis_frame))
        yield clk.posedge
        yield clk.posedge

        while gmii_rx_dv or rx_axis_tvalid:
            yield clk.posedge

        yield clk.posedge
        yield clk.posedge

        rx_frame = axis_sink.recv()

        eth_frame = eth_ep.EthFrame()
        eth_frame.parse_axis(rx_frame)
        eth_frame.update_fcs()

        assert eth_frame == test_frame

        yield delay(100)

        yield clk.posedge
        print("test 2: test tx packet")
        current_test.next = 2

        test_frame = eth_ep.EthFrame()
        test_frame.eth_dest_mac = 0xDAD1D2D3D4D5
        test_frame.eth_src_mac = 0x5A5152535455
        test_frame.eth_type = 0x8000
        test_frame.payload = bytearray(range(32))
        test_frame.update_fcs()

        axis_frame = test_frame.build_axis()

        axis_source.send(axis_frame)
        yield clk.posedge
        yield clk.posedge

        while gmii_tx_en or tx_axis_tvalid:
            yield clk.posedge

        yield clk.posedge
        yield clk.posedge

        rx_frame = gmii_sink.recv()

        assert rx_frame.data[0:8] == bytearray(
            b'\x55\x55\x55\x55\x55\x55\x55\xD5')

        eth_frame = eth_ep.EthFrame()
        eth_frame.parse_axis_fcs(rx_frame.data[8:])

        print(hex(eth_frame.eth_fcs))
        print(hex(eth_frame.calc_fcs()))

        assert len(eth_frame.payload.data) == 46
        assert eth_frame.eth_fcs == eth_frame.calc_fcs()
        assert eth_frame.eth_dest_mac == test_frame.eth_dest_mac
        assert eth_frame.eth_src_mac == test_frame.eth_src_mac
        assert eth_frame.eth_type == test_frame.eth_type
        assert eth_frame.payload.data.index(test_frame.payload.data) == 0

        yield delay(100)

        raise StopSimulation

    return dut, axis_source_logic, axis_sink_logic, gmii_source_logic, gmii_sink_logic, clkgen, check