1
0
mirror of https://github.com/corundum/corundum.git synced 2025-01-16 08:12:53 +08:00

Add FCS field to eth_ep

This commit is contained in:
Alex Forencich 2015-02-24 20:26:24 -08:00
parent 7775733120
commit f3ea7cd8ac

View File

@ -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('<L', self.eth_fcs)
return axis_ep.AXIStreamFrame(data)
def parse_axis(self, data):
data = axis_ep.AXIStreamFrame(data).data
self.eth_dest_mac = struct.unpack('>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('<L', data[-4:])[0]
def __eq__(self, other):
if type(other) is EthFrame:
return (self.eth_src_mac == other.eth_src_mac and
@ -84,7 +112,7 @@ class EthFrame(object):
self.payload == other.payload)
def __repr__(self):
return 'EthFrame(payload=%s, eth_dest_mac=0x%012x, eth_src_mac=0x%012x, eth_type=0x%04x)' % (repr(self.payload), self.eth_dest_mac, self.eth_src_mac, self.eth_type)
return 'EthFrame(payload=%s, eth_dest_mac=0x%012x, eth_src_mac=0x%012x, eth_type=0x%04x, eth_fcs=0x%08x)' % (repr(self.payload), self.eth_dest_mac, self.eth_src_mac, self.eth_type, self.eth_fcs)
def EthFrameSource(clk, rst,
eth_hdr_valid=None,