mirror of
https://github.com/corundum/corundum.git
synced 2025-01-30 08:32:52 +08:00
Add FCS field to eth_ep
This commit is contained in:
parent
7775733120
commit
f3ea7cd8ac
32
tb/eth_ep.py
32
tb/eth_ep.py
@ -26,19 +26,22 @@ from myhdl import *
|
|||||||
import axis_ep
|
import axis_ep
|
||||||
from Queue import Queue
|
from Queue import Queue
|
||||||
import struct
|
import struct
|
||||||
|
import zlib
|
||||||
|
|
||||||
class EthFrame(object):
|
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._payload = axis_ep.AXIStreamFrame()
|
||||||
self.eth_dest_mac = eth_dest_mac
|
self.eth_dest_mac = eth_dest_mac
|
||||||
self.eth_src_mac = eth_src_mac
|
self.eth_src_mac = eth_src_mac
|
||||||
self.eth_type = eth_type
|
self.eth_type = eth_type
|
||||||
|
self.eth_fcs = eth_fcs
|
||||||
|
|
||||||
if type(payload) is dict:
|
if type(payload) is dict:
|
||||||
self.payload = axis_ep.AXIStreamFrame(payload['eth_payload'])
|
self.payload = axis_ep.AXIStreamFrame(payload['eth_payload'])
|
||||||
self.eth_dest_mac = payload['eth_dest_mac']
|
self.eth_dest_mac = payload['eth_dest_mac']
|
||||||
self.eth_src_mac = payload['eth_src_mac']
|
self.eth_src_mac = payload['eth_src_mac']
|
||||||
self.eth_type = payload['eth_type']
|
self.eth_type = payload['eth_type']
|
||||||
|
self.eth_fcs = payload['eth_fcs']
|
||||||
if type(payload) is bytes:
|
if type(payload) is bytes:
|
||||||
payload = bytearray(payload)
|
payload = bytearray(payload)
|
||||||
if type(payload) is bytearray or type(payload) is axis_ep.AXIStreamFrame:
|
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_dest_mac = payload.eth_dest_mac
|
||||||
self.eth_src_mac = payload.eth_src_mac
|
self.eth_src_mac = payload.eth_src_mac
|
||||||
self.eth_type = payload.eth_type
|
self.eth_type = payload.eth_type
|
||||||
|
self.eth_fcs = payload.eth_fcs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def payload(self):
|
def payload(self):
|
||||||
@ -57,6 +61,14 @@ class EthFrame(object):
|
|||||||
def payload(self, value):
|
def payload(self, value):
|
||||||
self._payload = axis_ep.AXIStreamFrame(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):
|
def build_axis(self):
|
||||||
data = b''
|
data = b''
|
||||||
|
|
||||||
@ -68,6 +80,16 @@ class EthFrame(object):
|
|||||||
|
|
||||||
return axis_ep.AXIStreamFrame(data)
|
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):
|
def parse_axis(self, data):
|
||||||
data = axis_ep.AXIStreamFrame(data).data
|
data = axis_ep.AXIStreamFrame(data).data
|
||||||
self.eth_dest_mac = struct.unpack('>Q', '\x00\x00'+data[0:6])[0]
|
self.eth_dest_mac = struct.unpack('>Q', '\x00\x00'+data[0:6])[0]
|
||||||
@ -76,6 +98,12 @@ class EthFrame(object):
|
|||||||
data = data[14:]
|
data = data[14:]
|
||||||
self.payload = axis_ep.AXIStreamFrame(data)
|
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):
|
def __eq__(self, other):
|
||||||
if type(other) is EthFrame:
|
if type(other) is EthFrame:
|
||||||
return (self.eth_src_mac == other.eth_src_mac and
|
return (self.eth_src_mac == other.eth_src_mac and
|
||||||
@ -84,7 +112,7 @@ class EthFrame(object):
|
|||||||
self.payload == other.payload)
|
self.payload == other.payload)
|
||||||
|
|
||||||
def __repr__(self):
|
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,
|
def EthFrameSource(clk, rst,
|
||||||
eth_hdr_valid=None,
|
eth_hdr_valid=None,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user