diff --git a/tb/eth_ep.py b/tb/eth_ep.py index 4fd9fcdba..0a50edc22 100644 --- a/tb/eth_ep.py +++ b/tb/eth_ep.py @@ -26,19 +26,22 @@ from myhdl import * import axis_ep from Queue import Queue import struct +import zlib class EthFrame(object): - def __init__(self, payload=b'', eth_dest_mac=0, eth_src_mac=0, eth_type=0): + def __init__(self, payload=b'', eth_dest_mac=0, eth_src_mac=0, eth_type=0, eth_fcs=None): self._payload = axis_ep.AXIStreamFrame() self.eth_dest_mac = eth_dest_mac self.eth_src_mac = eth_src_mac self.eth_type = eth_type + self.eth_fcs = eth_fcs if type(payload) is dict: self.payload = axis_ep.AXIStreamFrame(payload['eth_payload']) self.eth_dest_mac = payload['eth_dest_mac'] self.eth_src_mac = payload['eth_src_mac'] self.eth_type = payload['eth_type'] + self.eth_fcs = payload['eth_fcs'] if type(payload) is bytes: payload = bytearray(payload) if type(payload) is bytearray or type(payload) is axis_ep.AXIStreamFrame: @@ -48,6 +51,7 @@ class EthFrame(object): self.eth_dest_mac = payload.eth_dest_mac self.eth_src_mac = payload.eth_src_mac self.eth_type = payload.eth_type + self.eth_fcs = payload.eth_fcs @property def payload(self): @@ -57,6 +61,14 @@ class EthFrame(object): def payload(self, value): self._payload = axis_ep.AXIStreamFrame(value) + def calc_fcs(self): + frame = self.build_axis().data + + return zlib.crc32(bytes(frame)) & 0xffffffff + + def update_fcs(self): + self.eth_fcs = self.calc_fcs() + def build_axis(self): data = b'' @@ -68,6 +80,16 @@ class EthFrame(object): return axis_ep.AXIStreamFrame(data) + def build_axis_fcs(self): + if self.eth_fcs is None: + self.update_fcs() + + data = self.build_axis().data + + data += struct.pack('Q', '\x00\x00'+data[0:6])[0] @@ -76,6 +98,12 @@ class EthFrame(object): data = data[14:] self.payload = axis_ep.AXIStreamFrame(data) + def parse_axis_fcs(self, data): + self.parse_axis(data) + data = self.payload.data + self.payload = axis_ep.AXIStreamFrame(data[:-4]) + self.eth_fcs = struct.unpack('