""" Copyright (c) 2014-2017 Alex Forencich 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 axis_ep import struct import zlib class EthFrame(object): 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: self.payload = axis_ep.AXIStreamFrame(payload) if type(payload) is EthFrame: self.payload = axis_ep.AXIStreamFrame(payload.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 @property def payload(self): return self._payload @payload.setter 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'' data += struct.pack('>Q', self.eth_dest_mac)[2:] data += struct.pack('>Q', self.eth_src_mac)[2:] data += struct.pack('>H', self.eth_type) data += self.payload.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('Q', b'\x00\x00'+data[0:6])[0] self.eth_src_mac = struct.unpack('>Q', b'\x00\x00'+data[6:12])[0] self.eth_type = struct.unpack('>H', data[12:14])[0] 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(' 0: frame = self.queue.pop(0) eth_dest_mac.next = frame.eth_dest_mac eth_src_mac.next = frame.eth_src_mac eth_type.next = frame.eth_type self.payload_source.send(frame.payload) if name is not None: print("[%s] Sending frame %s" % (name, repr(frame))) eth_hdr_valid_int.next = True return logic, pause_logic, eth_payload_source class EthFrameSink(): def __init__(self): self.has_logic = False self.queue = [] self.payload_sink = axis_ep.AXIStreamSink() def recv(self): if len(self.queue) > 0: return self.queue.pop(0) return None def count(self): return len(self.queue) def empty(self): return self.count() == 0 def create_logic(self, clk, rst, eth_hdr_valid=None, eth_hdr_ready=None, eth_dest_mac=Signal(intbv(0)[48:]), eth_src_mac=Signal(intbv(0)[48:]), eth_type=Signal(intbv(0)[16:]), eth_payload_tdata=None, eth_payload_tkeep=Signal(bool(True)), eth_payload_tvalid=Signal(bool(True)), eth_payload_tready=Signal(bool(True)), eth_payload_tlast=Signal(bool(True)), eth_payload_tuser=Signal(bool(False)), pause=0, name=None ): assert not self.has_logic self.has_logic = True eth_hdr_ready_int = Signal(bool(False)) eth_hdr_valid_int = Signal(bool(False)) eth_payload_pause = Signal(bool(False)) eth_header_queue = [] eth_payload_sink = self.payload_sink.create_logic( clk=clk, rst=rst, tdata=eth_payload_tdata, tkeep=eth_payload_tkeep, tvalid=eth_payload_tvalid, tready=eth_payload_tready, tlast=eth_payload_tlast, tuser=eth_payload_tuser, pause=eth_payload_pause ) @always_comb def pause_logic(): eth_hdr_ready.next = eth_hdr_ready_int and not pause eth_hdr_valid_int.next = eth_hdr_valid and not pause eth_payload_pause.next = pause # or eth_hdr_valid_int @instance def logic(): while True: yield clk.posedge, rst.posedge if rst: eth_hdr_ready_int.next = False else: eth_hdr_ready_int.next = True if eth_hdr_ready_int and eth_hdr_valid_int: frame = EthFrame() frame.eth_dest_mac = int(eth_dest_mac) frame.eth_src_mac = int(eth_src_mac) frame.eth_type = int(eth_type) eth_header_queue.append(frame) if not self.payload_sink.empty() and len(eth_header_queue) > 0: frame = eth_header_queue.pop(0) frame.payload = self.payload_sink.recv() self.queue.append(frame) if name is not None: print("[%s] Got frame %s" % (name, repr(frame))) return logic, pause_logic, eth_payload_sink