2015-01-29 13:17:45 -05:00
|
|
|
import math
|
2015-01-28 03:14:01 -05:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from migen.fhdl.std import *
|
2015-01-28 18:25:55 -05:00
|
|
|
from migen.fhdl.decorators import ModuleDecorator
|
2015-01-28 03:14:01 -05:00
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
2015-01-28 05:45:19 -05:00
|
|
|
from migen.genlib.record import *
|
|
|
|
from migen.genlib.fsm import FSM, NextState
|
2015-01-28 13:07:59 -05:00
|
|
|
from migen.genlib.misc import chooser
|
2015-02-09 08:49:59 -05:00
|
|
|
from migen.flow.actor import *
|
|
|
|
from migen.flow.plumbing import Buffer
|
2015-01-28 05:45:19 -05:00
|
|
|
from migen.actorlib.structuring import Converter, Pipeline
|
|
|
|
from migen.actorlib.fifo import SyncFIFO, AsyncFIFO
|
|
|
|
from migen.bank.description import *
|
2015-01-27 18:33:26 -05:00
|
|
|
|
|
|
|
eth_mtu = 1532
|
2015-02-05 17:30:50 -05:00
|
|
|
eth_min_len = 46
|
2015-02-09 06:57:05 -05:00
|
|
|
eth_interpacket_gap = 12
|
2015-01-27 18:33:26 -05:00
|
|
|
eth_preamble = 0xD555555555555555
|
|
|
|
buffer_depth = 2**log2_int(eth_mtu, need_pow2=False)
|
|
|
|
|
2015-01-28 16:43:58 -05:00
|
|
|
class HField():
|
|
|
|
def __init__(self, byte, offset, width):
|
|
|
|
self.byte = byte
|
|
|
|
self.offset = offset
|
|
|
|
self.width = width
|
|
|
|
|
2015-01-29 13:17:45 -05:00
|
|
|
ethernet_type_ip = 0x800
|
|
|
|
ethernet_type_arp = 0x806
|
|
|
|
|
2015-01-28 16:43:58 -05:00
|
|
|
mac_header_len = 14
|
|
|
|
mac_header = {
|
2015-02-04 16:51:11 -05:00
|
|
|
"target_mac": HField(0, 0, 48),
|
|
|
|
"sender_mac": HField(6, 0, 48),
|
|
|
|
"ethernet_type": HField(12, 0, 16)
|
2015-01-28 16:43:58 -05:00
|
|
|
}
|
|
|
|
|
2015-01-29 13:17:45 -05:00
|
|
|
arp_hwtype_ethernet = 0x0001
|
|
|
|
arp_proto_ip = 0x0800
|
|
|
|
arp_opcode_request = 0x0001
|
|
|
|
arp_opcode_reply = 0x0002
|
2015-01-28 19:03:47 -05:00
|
|
|
|
2015-01-28 16:43:58 -05:00
|
|
|
arp_header_len = 28
|
|
|
|
arp_header = {
|
2015-02-04 16:51:11 -05:00
|
|
|
"hwtype": HField( 0, 0, 16),
|
|
|
|
"proto": HField( 2, 0, 16),
|
|
|
|
"hwsize": HField( 4, 0, 8),
|
|
|
|
"protosize": HField( 5, 0, 8),
|
|
|
|
"opcode": HField( 6, 0, 16),
|
|
|
|
"sender_mac": HField( 8, 0, 48),
|
|
|
|
"sender_ip": HField(14, 0, 32),
|
|
|
|
"target_mac": HField(18, 0, 48),
|
|
|
|
"target_ip": HField(24, 0, 32)
|
2015-01-28 16:43:58 -05:00
|
|
|
}
|
|
|
|
|
2015-02-04 13:03:49 -05:00
|
|
|
ipv4_header_len = 20
|
2015-01-28 16:43:58 -05:00
|
|
|
ipv4_header = {
|
2015-02-05 17:30:50 -05:00
|
|
|
"ihl": HField(0, 0, 4),
|
|
|
|
"version": HField(0, 4, 4),
|
2015-02-04 16:51:11 -05:00
|
|
|
"total_length": HField(2, 0, 16),
|
|
|
|
"identification": HField(4, 0, 16),
|
|
|
|
"ttl": HField(8, 0, 8),
|
|
|
|
"protocol": HField(9, 0, 8),
|
|
|
|
"checksum": HField(10, 0, 16),
|
|
|
|
"sender_ip": HField(12, 0, 32),
|
|
|
|
"target_ip": HField(16, 0, 32)
|
2015-01-28 16:43:58 -05:00
|
|
|
}
|
2015-02-04 10:31:37 -05:00
|
|
|
|
2015-02-09 16:37:41 -05:00
|
|
|
icmp_header_len = 8
|
|
|
|
icmp_header = {
|
|
|
|
"msgtype": HField( 0, 0, 8),
|
|
|
|
"code": HField( 1, 0, 8),
|
|
|
|
"checksum": HField( 2, 0, 16),
|
|
|
|
"quench": HField( 4, 0, 32)
|
|
|
|
}
|
|
|
|
icmp_protocol = 0x01
|
|
|
|
|
2015-01-28 16:43:58 -05:00
|
|
|
udp_header_len = 8
|
|
|
|
udp_header = {
|
2015-02-04 16:51:11 -05:00
|
|
|
"src_port": HField( 0, 0, 16),
|
|
|
|
"dst_port": HField( 2, 0, 16),
|
|
|
|
"length": HField( 4, 0, 16),
|
|
|
|
"checksum": HField( 6, 0, 16)
|
2015-01-28 16:43:58 -05:00
|
|
|
}
|
|
|
|
|
2015-02-04 14:30:17 -05:00
|
|
|
udp_protocol = 0x11
|
|
|
|
|
2015-02-09 16:37:41 -05:00
|
|
|
etherbone_magic = 0x4e6f
|
|
|
|
etherbone_version = 1
|
|
|
|
etherbone_header_len = 8
|
2015-02-10 03:25:36 -05:00
|
|
|
etherbone_header = {
|
2015-02-09 16:37:41 -05:00
|
|
|
"magic": HField( 0, 0, 16),
|
|
|
|
|
2015-02-10 15:29:14 -05:00
|
|
|
"version": HField( 2, 4, 4),
|
|
|
|
"nr": HField( 2, 2, 1),
|
|
|
|
"pr": HField( 2, 1, 1),
|
|
|
|
"pf": HField( 2, 0, 1),
|
2015-02-09 16:37:41 -05:00
|
|
|
|
2015-02-10 15:29:14 -05:00
|
|
|
"addr_size": HField( 3, 4, 4),
|
|
|
|
"port_size": HField( 3, 0, 4)
|
2015-02-10 03:25:36 -05:00
|
|
|
}
|
|
|
|
|
2015-02-10 15:29:14 -05:00
|
|
|
etherbone_record_header_len = 4
|
|
|
|
etherbone_record_header = {
|
|
|
|
"bca": HField( 0, 0, 1),
|
|
|
|
"rca": HField( 0, 1, 1),
|
|
|
|
"rff": HField( 0, 2, 1),
|
|
|
|
"cyc": HField( 0, 4, 1),
|
|
|
|
"wca": HField( 0, 5, 1),
|
|
|
|
"wff": HField( 0, 6, 1),
|
|
|
|
|
|
|
|
"byte_enable": HField( 1, 0, 8),
|
|
|
|
|
|
|
|
"wcount": HField( 2, 0, 8),
|
|
|
|
|
|
|
|
"rcount": HField( 3, 0, 8)
|
|
|
|
}
|
2015-02-05 19:38:30 -05:00
|
|
|
|
2015-01-30 04:48:56 -05:00
|
|
|
def reverse_bytes(v):
|
2015-01-30 10:27:56 -05:00
|
|
|
n = math.ceil(flen(v)/8)
|
2015-01-30 04:48:56 -05:00
|
|
|
r = []
|
|
|
|
for i in reversed(range(n)):
|
|
|
|
r.append(v[i*8:min((i+1)*8, flen(v))])
|
|
|
|
return Cat(iter(r))
|
|
|
|
|
2015-01-28 16:43:58 -05:00
|
|
|
# layouts
|
|
|
|
def _layout_from_header(header):
|
|
|
|
_layout = []
|
|
|
|
for k, v in sorted(header.items()):
|
|
|
|
_layout.append((k, v.width))
|
|
|
|
return _layout
|
|
|
|
|
2015-01-28 05:45:19 -05:00
|
|
|
def eth_phy_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-01-28 05:45:19 -05:00
|
|
|
("data", dw),
|
2015-01-28 16:43:58 -05:00
|
|
|
("last_be", dw//8),
|
2015-01-28 05:45:19 -05:00
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
return EndpointDescription(payload_layout, packetized=True)
|
2015-01-28 05:45:19 -05:00
|
|
|
|
|
|
|
def eth_mac_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = _layout_from_header(mac_header) + [
|
2015-01-28 16:43:58 -05:00
|
|
|
("data", dw),
|
|
|
|
("last_be", dw//8),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
return EndpointDescription(payload_layout, packetized=True)
|
2015-01-28 16:43:58 -05:00
|
|
|
|
|
|
|
def eth_arp_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-01-28 05:45:19 -05:00
|
|
|
("data", dw),
|
2015-01-27 18:33:26 -05:00
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
param_layout = _layout_from_header(arp_header)
|
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-01-28 16:43:58 -05:00
|
|
|
|
2015-01-30 07:23:06 -05:00
|
|
|
arp_table_request_layout = [
|
|
|
|
("ip_address", 32)
|
|
|
|
]
|
|
|
|
|
|
|
|
arp_table_response_layout = [
|
|
|
|
("failed", 1),
|
|
|
|
("mac_address", 48)
|
|
|
|
]
|
|
|
|
|
2015-01-28 16:43:58 -05:00
|
|
|
def eth_ipv4_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-01-28 16:43:58 -05:00
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
param_layout = _layout_from_header(ipv4_header)
|
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-01-28 16:43:58 -05:00
|
|
|
|
2015-01-30 13:14:05 -05:00
|
|
|
def eth_ipv4_user_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-01-30 13:14:05 -05:00
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
param_layout = [
|
|
|
|
("length", 16),
|
|
|
|
("protocol", 8),
|
|
|
|
("ip_address", 32)
|
|
|
|
]
|
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-01-30 13:14:05 -05:00
|
|
|
|
2015-02-05 07:31:06 -05:00
|
|
|
def convert_ip(s):
|
|
|
|
ip = 0
|
|
|
|
for e in s.split("."):
|
|
|
|
ip = ip << 8
|
|
|
|
ip += int(e)
|
|
|
|
return ip
|
|
|
|
|
2015-02-09 16:37:41 -05:00
|
|
|
def eth_icmp_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-02-09 16:37:41 -05:00
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
param_layout = _layout_from_header(icmp_header)
|
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-02-09 16:37:41 -05:00
|
|
|
|
|
|
|
def eth_icmp_user_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-02-09 16:37:41 -05:00
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
param_layout = _layout_from_header(icmp_header) + [
|
|
|
|
("ip_address", 32),
|
|
|
|
("length", 16)
|
|
|
|
]
|
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-02-09 16:37:41 -05:00
|
|
|
|
2015-01-28 16:43:58 -05:00
|
|
|
def eth_udp_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-01-28 16:43:58 -05:00
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
param_layout = _layout_from_header(udp_header)
|
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-01-28 16:43:58 -05:00
|
|
|
|
2015-02-04 14:50:49 -05:00
|
|
|
def eth_udp_user_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
|
|
|
param_layout = [
|
2015-02-04 16:51:11 -05:00
|
|
|
("src_port", 16),
|
|
|
|
("dst_port", 16),
|
2015-02-04 14:50:49 -05:00
|
|
|
("ip_address", 32),
|
2015-02-10 04:30:39 -05:00
|
|
|
("length", 16)
|
2015-02-04 14:50:49 -05:00
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-02-04 14:50:49 -05:00
|
|
|
|
2015-02-09 16:37:41 -05:00
|
|
|
def eth_etherbone_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-02-05 19:38:30 -05:00
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
param_layout = _layout_from_header(etherbone_header)
|
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-02-05 19:38:30 -05:00
|
|
|
|
2015-02-09 16:37:41 -05:00
|
|
|
def eth_etherbone_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
2015-02-09 16:37:41 -05:00
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
param_layout = _layout_from_header(etherbone_header)
|
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-02-09 16:37:41 -05:00
|
|
|
|
|
|
|
def eth_etherbone_user_description(dw):
|
2015-02-10 04:30:39 -05:00
|
|
|
payload_layout = [
|
|
|
|
("data", dw),
|
|
|
|
("error", dw//8)
|
|
|
|
]
|
|
|
|
param_layout = [
|
2015-02-05 19:38:30 -05:00
|
|
|
("length", 16),
|
2015-02-09 16:37:41 -05:00
|
|
|
("ip_address", 32),
|
|
|
|
("wcount", 8),
|
2015-02-10 04:30:39 -05:00
|
|
|
("rcount", 8)
|
2015-02-05 19:38:30 -05:00
|
|
|
]
|
2015-02-10 04:30:39 -05:00
|
|
|
return EndpointDescription(payload_layout, param_layout, packetized=True)
|
2015-02-05 19:38:30 -05:00
|
|
|
|
2015-02-09 16:37:41 -05:00
|
|
|
|
2015-02-10 09:37:29 -05:00
|
|
|
# Generic classes
|
|
|
|
class Port:
|
|
|
|
def connect(self, port):
|
|
|
|
r = [
|
|
|
|
Record.connect(self.source, port.sink),
|
|
|
|
Record.connect(port.source, self.sink)
|
|
|
|
]
|
|
|
|
return r
|
|
|
|
|
2015-01-28 18:02:50 -05:00
|
|
|
# Generic modules
|
2015-01-30 06:02:01 -05:00
|
|
|
@DecorateModule(InsertReset)
|
|
|
|
@DecorateModule(InsertCE)
|
|
|
|
class FlipFlop(Module):
|
2015-01-30 11:44:44 -05:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.d = Signal(*args, **kwargs)
|
|
|
|
self.q = Signal(*args, **kwargs)
|
2015-01-30 06:02:01 -05:00
|
|
|
self.sync += self.q.eq(self.d)
|
|
|
|
|
2015-01-28 18:02:50 -05:00
|
|
|
@DecorateModule(InsertReset)
|
|
|
|
@DecorateModule(InsertCE)
|
|
|
|
class Counter(Module):
|
|
|
|
def __init__(self, signal=None, **kwargs):
|
|
|
|
if signal is None:
|
|
|
|
self.value = Signal(**kwargs)
|
|
|
|
else:
|
|
|
|
self.value = signal
|
|
|
|
self.width = flen(self.value)
|
|
|
|
self.sync += self.value.eq(self.value+1)
|
|
|
|
|
|
|
|
@DecorateModule(InsertReset)
|
|
|
|
@DecorateModule(InsertCE)
|
|
|
|
class Timeout(Module):
|
|
|
|
def __init__(self, length):
|
|
|
|
self.reached = Signal()
|
|
|
|
###
|
|
|
|
value = Signal(max=length)
|
2015-01-30 06:02:01 -05:00
|
|
|
self.sync += If(~self.reached, value.eq(value+1))
|
|
|
|
self.comb += self.reached.eq(value == (length-1))
|
2015-01-28 18:02:50 -05:00
|
|
|
|
|
|
|
class BufferizeEndpoints(ModuleDecorator):
|
|
|
|
def __init__(self, submodule, *args):
|
|
|
|
ModuleDecorator.__init__(self, submodule)
|
|
|
|
|
|
|
|
endpoints = get_endpoints(submodule)
|
|
|
|
sinks = {}
|
|
|
|
sources = {}
|
|
|
|
for name, endpoint in endpoints.items():
|
|
|
|
if name in args or len(args) == 0:
|
|
|
|
if isinstance(endpoint, Sink):
|
|
|
|
sinks.update({name : endpoint})
|
|
|
|
elif isinstance(endpoint, Source):
|
|
|
|
sources.update({name : endpoint})
|
|
|
|
|
|
|
|
# add buffer on sinks
|
|
|
|
for name, sink in sinks.items():
|
|
|
|
buf = Buffer(sink.description)
|
|
|
|
self.submodules += buf
|
|
|
|
setattr(self, name, buf.d)
|
|
|
|
self.comb += Record.connect(buf.q, sink)
|
|
|
|
|
|
|
|
# add buffer on sources
|
|
|
|
for name, source in sources.items():
|
|
|
|
buf = Buffer(source.description)
|
|
|
|
self.submodules += buf
|
|
|
|
self.comb += Record.connect(source, buf.d)
|
|
|
|
setattr(self, name, buf.q)
|
2015-01-28 18:25:55 -05:00
|
|
|
|
2015-02-09 11:57:45 -05:00
|
|
|
class EndpointPacketStatus(Module):
|
|
|
|
def __init__(self, endpoint):
|
|
|
|
self.start = Signal()
|
|
|
|
self.done = Signal()
|
|
|
|
self.ongoing = Signal()
|
|
|
|
|
|
|
|
ongoing = Signal()
|
|
|
|
self.comb += [
|
|
|
|
self.start.eq(endpoint.stb & endpoint.sop & endpoint.ack),
|
|
|
|
self.done.eq(endpoint.stb & endpoint.eop & endpoint.ack)
|
|
|
|
]
|
|
|
|
self.sync += \
|
|
|
|
If(self.start,
|
|
|
|
ongoing.eq(1)
|
|
|
|
).Elif(self.done,
|
|
|
|
ongoing.eq(0)
|
|
|
|
)
|
|
|
|
self.comb += self.ongoing.eq((self.start | ongoing) & ~self.done)
|
|
|
|
|
|
|
|
class PacketBuffer(Module):
|
|
|
|
def __init__(self, description, data_depth, cmd_depth=4, almost_full=None):
|
|
|
|
self.sink = sink = Sink(description)
|
|
|
|
self.source = source = Source(description)
|
|
|
|
|
|
|
|
###
|
|
|
|
sink_status = EndpointPacketStatus(self.sink)
|
|
|
|
source_status = EndpointPacketStatus(self.source)
|
|
|
|
self.submodules += sink_status, source_status
|
|
|
|
|
|
|
|
# store incoming packets
|
|
|
|
# cmds
|
|
|
|
def cmd_description():
|
|
|
|
layout = [("error", 1)]
|
|
|
|
return EndpointDescription(layout)
|
|
|
|
cmd_fifo = SyncFIFO(cmd_description(), cmd_depth)
|
|
|
|
self.submodules += cmd_fifo
|
|
|
|
self.comb += [
|
|
|
|
cmd_fifo.sink.stb.eq(sink_status.done),
|
|
|
|
cmd_fifo.sink.error.eq(sink.error)
|
|
|
|
]
|
|
|
|
|
|
|
|
# data
|
|
|
|
data_fifo = SyncFIFO(description, data_depth, buffered=True)
|
|
|
|
self.submodules += data_fifo
|
|
|
|
self.comb += [
|
|
|
|
Record.connect(self.sink, data_fifo.sink),
|
|
|
|
data_fifo.sink.stb.eq(self.sink.stb & cmd_fifo.sink.ack),
|
|
|
|
self.sink.ack.eq(data_fifo.sink.ack & cmd_fifo.sink.ack),
|
|
|
|
]
|
|
|
|
|
|
|
|
# output packets
|
|
|
|
self.fsm = fsm = FSM(reset_state="IDLE")
|
|
|
|
self.submodules += fsm
|
|
|
|
fsm.act("IDLE",
|
|
|
|
If(cmd_fifo.source.stb,
|
|
|
|
NextState("SEEK_SOP")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEEK_SOP",
|
|
|
|
If(~data_fifo.source.sop,
|
|
|
|
data_fifo.source.ack.eq(1)
|
|
|
|
).Else(
|
|
|
|
NextState("OUTPUT")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("OUTPUT",
|
|
|
|
Record.connect(data_fifo.source, self.source),
|
|
|
|
self.source.error.eq(cmd_fifo.source.error),
|
|
|
|
If(source_status.done,
|
|
|
|
cmd_fifo.source.ack.eq(1),
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# compute almost full
|
|
|
|
if almost_full is not None:
|
|
|
|
self.almost_full = Signal()
|
|
|
|
self.comb += self.almost_full.eq(data_fifo.fifo.level > almost_full)
|