From 4b4e5fdbfa4b0af912e228c79a0ec63e867e2441 Mon Sep 17 00:00:00 2001 From: Markus Koch Date: Fri, 13 May 2022 18:20:13 +0200 Subject: [PATCH] bench: hw_itl: Calculate and append FCS for incoming frames --- cocotb/hw_itl.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cocotb/hw_itl.py b/cocotb/hw_itl.py index 62a92eb..7f691e4 100644 --- a/cocotb/hw_itl.py +++ b/cocotb/hw_itl.py @@ -13,6 +13,7 @@ import time import os import asyncio import fcntl +import zlib ''' # Set up virtual device using @@ -37,11 +38,18 @@ class MacDevReceiver(): self.macdev.bind((dev, 0)) fcntl.fcntl(self.macdev, fcntl.F_SETFL, os.O_NONBLOCK) # Not the best way to poll, but I couldn't get asyncio to play nicely with threading... + def eth_fcs(self, data): + crc = zlib.crc32(data) & 0xFFFF_FFFF + return crc.to_bytes(4, byteorder='little') + async def main(self): ETH_HEAD = b'\x55\x55\x55\x55\xD5' while True: try: r = self.macdev.recv(2000) + if len(r) < 60: + r += b'\x00' * (60 - len(r)) + r += self.eth_fcs(r) await self.eth_tx.send(ETH_HEAD + r); except: await Timer(1, "us")