2015-03-21 22:31:01 -07:00
|
|
|
#!/usr/bin/env python
|
2015-03-03 00:46:53 -08:00
|
|
|
"""
|
|
|
|
|
2018-02-26 12:50:51 -08:00
|
|
|
Copyright (c) 2015-2018 Alex Forencich
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from myhdl import *
|
|
|
|
import os
|
2015-03-04 12:58:22 -08:00
|
|
|
import struct
|
|
|
|
import zlib
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
import axis_ep
|
|
|
|
import eth_ep
|
|
|
|
|
|
|
|
module = 'axis_eth_fcs_insert'
|
2016-09-13 15:24:02 -07:00
|
|
|
testbench = 'test_%s_pad' % module
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
srcs = []
|
|
|
|
|
|
|
|
srcs.append("../rtl/%s.v" % module)
|
2016-06-28 17:31:58 -07:00
|
|
|
srcs.append("../rtl/lfsr.v")
|
2016-09-13 15:24:02 -07:00
|
|
|
srcs.append("%s.v" % testbench)
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
src = ' '.join(srcs)
|
|
|
|
|
2016-09-13 15:24:02 -07:00
|
|
|
build_cmd = "iverilog -o %s.vvp %s" % (testbench, src)
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
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:])
|
|
|
|
|
2018-11-07 22:35:06 -08:00
|
|
|
s_axis_tdata = Signal(intbv(0)[8:])
|
|
|
|
s_axis_tvalid = Signal(bool(0))
|
|
|
|
s_axis_tlast = Signal(bool(0))
|
|
|
|
s_axis_tuser = Signal(bool(0))
|
|
|
|
m_axis_tready = Signal(bool(0))
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
# Outputs
|
2018-11-07 22:35:06 -08:00
|
|
|
s_axis_tready = Signal(bool(0))
|
|
|
|
m_axis_tdata = Signal(intbv(0)[8:])
|
|
|
|
m_axis_tvalid = Signal(bool(0))
|
|
|
|
m_axis_tlast = Signal(bool(0))
|
|
|
|
m_axis_tuser = Signal(bool(0))
|
2015-03-03 00:46:53 -08:00
|
|
|
busy = Signal(bool(0))
|
|
|
|
|
|
|
|
# sources and sinks
|
|
|
|
source_pause = Signal(bool(0))
|
|
|
|
sink_pause = Signal(bool(0))
|
|
|
|
|
2016-09-13 15:24:02 -07:00
|
|
|
source = axis_ep.AXIStreamSource()
|
|
|
|
|
|
|
|
source_logic = source.create_logic(
|
|
|
|
clk,
|
|
|
|
rst,
|
2018-11-07 22:35:06 -08:00
|
|
|
tdata=s_axis_tdata,
|
|
|
|
tvalid=s_axis_tvalid,
|
|
|
|
tready=s_axis_tready,
|
|
|
|
tlast=s_axis_tlast,
|
|
|
|
tuser=s_axis_tuser,
|
2016-09-13 15:24:02 -07:00
|
|
|
pause=source_pause,
|
|
|
|
name='source'
|
|
|
|
)
|
|
|
|
|
|
|
|
sink = axis_ep.AXIStreamSink()
|
|
|
|
|
|
|
|
sink_logic = sink.create_logic(
|
|
|
|
clk,
|
|
|
|
rst,
|
2018-11-07 22:35:06 -08:00
|
|
|
tdata=m_axis_tdata,
|
|
|
|
tvalid=m_axis_tvalid,
|
|
|
|
tready=m_axis_tready,
|
|
|
|
tlast=m_axis_tlast,
|
|
|
|
tuser=m_axis_tuser,
|
2016-09-13 15:24:02 -07:00
|
|
|
pause=sink_pause,
|
|
|
|
name='sink'
|
|
|
|
)
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
# DUT
|
2016-09-13 15:24:02 -07:00
|
|
|
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,
|
2015-03-03 00:46:53 -08:00
|
|
|
|
2018-11-07 22:35:06 -08:00
|
|
|
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,
|
2015-03-03 00:46:53 -08:00
|
|
|
|
2018-11-07 22:35:06 -08:00
|
|
|
m_axis_tdata=m_axis_tdata,
|
|
|
|
m_axis_tvalid=m_axis_tvalid,
|
|
|
|
m_axis_tready=m_axis_tready,
|
|
|
|
m_axis_tlast=m_axis_tlast,
|
|
|
|
m_axis_tuser=m_axis_tuser,
|
2015-03-03 00:46:53 -08:00
|
|
|
|
2016-09-13 15:24:02 -07:00
|
|
|
busy=busy
|
|
|
|
)
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
@always(delay(4))
|
|
|
|
def clkgen():
|
|
|
|
clk.next = not clk
|
|
|
|
|
|
|
|
def wait_normal():
|
2018-11-07 22:35:06 -08:00
|
|
|
while s_axis_tvalid or m_axis_tvalid:
|
2015-03-03 00:46:53 -08:00
|
|
|
yield clk.posedge
|
|
|
|
|
|
|
|
def wait_pause_source():
|
2018-11-07 22:35:06 -08:00
|
|
|
while s_axis_tvalid or m_axis_tvalid:
|
2015-03-03 00:46:53 -08:00
|
|
|
yield clk.posedge
|
|
|
|
yield clk.posedge
|
|
|
|
source_pause.next = False
|
|
|
|
yield clk.posedge
|
2019-03-07 23:44:43 -08:00
|
|
|
source_pause.next = True
|
|
|
|
yield clk.posedge
|
|
|
|
|
|
|
|
source_pause.next = False
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
def wait_pause_sink():
|
2018-11-07 22:35:06 -08:00
|
|
|
while s_axis_tvalid or m_axis_tvalid:
|
2015-03-03 00:46:53 -08:00
|
|
|
sink_pause.next = True
|
|
|
|
yield clk.posedge
|
|
|
|
yield clk.posedge
|
|
|
|
yield clk.posedge
|
|
|
|
sink_pause.next = False
|
|
|
|
yield clk.posedge
|
|
|
|
|
|
|
|
@instance
|
|
|
|
def check():
|
|
|
|
yield delay(100)
|
|
|
|
yield clk.posedge
|
|
|
|
rst.next = 1
|
|
|
|
yield clk.posedge
|
|
|
|
rst.next = 0
|
|
|
|
yield clk.posedge
|
|
|
|
yield delay(100)
|
|
|
|
yield clk.posedge
|
|
|
|
|
|
|
|
# testbench stimulus
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
for wait in wait_normal, wait_pause_source, wait_pause_sink:
|
2016-09-13 15:24:02 -07:00
|
|
|
source.send(axis_frame)
|
2015-03-03 00:46:53 -08:00
|
|
|
yield clk.posedge
|
|
|
|
yield clk.posedge
|
|
|
|
|
|
|
|
yield wait()
|
|
|
|
|
2018-07-02 18:20:07 -07:00
|
|
|
yield sink.wait()
|
2016-09-13 15:24:02 -07:00
|
|
|
rx_frame = sink.recv()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
eth_frame = eth_ep.EthFrame()
|
|
|
|
eth_frame.parse_axis_fcs(rx_frame)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-09-13 15:24:02 -07:00
|
|
|
assert sink.empty()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
for wait in wait_normal, wait_pause_source, wait_pause_sink:
|
2016-09-13 15:24:02 -07:00
|
|
|
source.send(axis_frame1)
|
|
|
|
source.send(axis_frame2)
|
2015-03-03 00:46:53 -08:00
|
|
|
yield clk.posedge
|
|
|
|
yield clk.posedge
|
|
|
|
|
|
|
|
yield wait()
|
|
|
|
|
2018-07-02 18:20:07 -07:00
|
|
|
yield sink.wait()
|
2016-09-13 15:24:02 -07:00
|
|
|
rx_frame = sink.recv()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
eth_frame = eth_ep.EthFrame()
|
|
|
|
eth_frame.parse_axis_fcs(rx_frame)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-07-02 18:20:07 -07:00
|
|
|
yield sink.wait()
|
2016-09-13 15:24:02 -07:00
|
|
|
rx_frame = sink.recv()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
eth_frame = eth_ep.EthFrame()
|
|
|
|
eth_frame.parse_axis_fcs(rx_frame)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-09-13 15:24:02 -07:00
|
|
|
assert sink.empty()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
for wait in wait_normal, wait_pause_source, wait_pause_sink:
|
2016-09-13 15:24:02 -07:00
|
|
|
source.send(axis_frame1)
|
|
|
|
source.send(axis_frame2)
|
2015-03-03 00:46:53 -08:00
|
|
|
yield clk.posedge
|
|
|
|
yield clk.posedge
|
|
|
|
|
|
|
|
yield wait()
|
|
|
|
|
2018-07-02 18:20:07 -07:00
|
|
|
yield sink.wait()
|
2016-09-13 15:24:02 -07:00
|
|
|
rx_frame = sink.recv()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
assert rx_frame.user[-1]
|
|
|
|
|
2018-07-02 18:20:07 -07:00
|
|
|
yield sink.wait()
|
2016-09-13 15:24:02 -07:00
|
|
|
rx_frame = sink.recv()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
eth_frame = eth_ep.EthFrame()
|
|
|
|
eth_frame.parse_axis_fcs(rx_frame)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-09-13 15:24:02 -07:00
|
|
|
assert sink.empty()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
yield delay(100)
|
|
|
|
|
2015-03-04 12:58:22 -08:00
|
|
|
for payload_len in list(range(1,18)):
|
|
|
|
yield clk.posedge
|
|
|
|
print("test 4: test short packet, length %d" % payload_len)
|
|
|
|
current_test.next = 4
|
|
|
|
|
|
|
|
test_frame = bytearray(range(payload_len))
|
|
|
|
|
|
|
|
for wait in wait_normal, wait_pause_source, wait_pause_sink:
|
2016-09-13 15:24:02 -07:00
|
|
|
source.send(test_frame)
|
2015-03-04 12:58:22 -08:00
|
|
|
yield clk.posedge
|
|
|
|
yield clk.posedge
|
|
|
|
|
|
|
|
yield wait()
|
|
|
|
|
2018-07-02 18:20:07 -07:00
|
|
|
yield sink.wait()
|
2016-09-13 15:24:02 -07:00
|
|
|
rx_frame = sink.recv()
|
2015-03-04 12:58:22 -08:00
|
|
|
|
|
|
|
payload = rx_frame.data[:-4]
|
|
|
|
fcs = struct.unpack('<L', rx_frame.data[-4:])[0]
|
|
|
|
check_fcs = zlib.crc32(bytes(payload)) & 0xffffffff
|
|
|
|
|
|
|
|
print(hex(fcs))
|
|
|
|
print(hex(check_fcs))
|
|
|
|
|
|
|
|
assert len(payload) == 60
|
|
|
|
assert payload.index(test_frame) == 0
|
|
|
|
assert check_fcs == fcs
|
|
|
|
|
2016-09-13 15:24:02 -07:00
|
|
|
assert sink.empty()
|
2015-03-04 12:58:22 -08:00
|
|
|
|
|
|
|
yield delay(100)
|
|
|
|
|
2015-03-03 00:46:53 -08:00
|
|
|
raise StopSimulation
|
|
|
|
|
2018-06-13 22:43:11 -07:00
|
|
|
return instances()
|
2015-03-03 00:46:53 -08:00
|
|
|
|
|
|
|
def test_bench():
|
|
|
|
sim = Simulation(bench())
|
|
|
|
sim.run()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print("Running test...")
|
|
|
|
test_bench()
|