2015-02-28 03:02:28 -05:00
|
|
|
from misoclib.com.liteeth.common import *
|
2015-02-11 10:21:06 -05:00
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-04-28 12:51:40 -04:00
|
|
|
class LiteEthEtherboneRecordPacketizer(Packetizer):
|
2015-04-13 03:53:43 -04:00
|
|
|
def __init__(self):
|
2015-04-28 12:51:40 -04:00
|
|
|
Packetizer.__init__(self,
|
2015-04-13 03:53:43 -04:00
|
|
|
eth_etherbone_record_description(32),
|
|
|
|
eth_etherbone_packet_user_description(32),
|
2015-04-27 09:06:37 -04:00
|
|
|
etherbone_record_header)
|
2015-02-11 10:21:06 -05:00
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-04-28 12:51:40 -04:00
|
|
|
class LiteEthEtherboneRecordDepacketizer(Depacketizer):
|
2015-04-13 03:53:43 -04:00
|
|
|
def __init__(self):
|
2015-04-28 12:51:40 -04:00
|
|
|
Depacketizer.__init__(self,
|
2015-04-13 03:53:43 -04:00
|
|
|
eth_etherbone_packet_user_description(32),
|
|
|
|
eth_etherbone_record_description(32),
|
2015-04-27 09:06:37 -04:00
|
|
|
etherbone_record_header)
|
2015-02-11 12:37:59 -05:00
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-02-11 12:37:59 -05:00
|
|
|
class LiteEthEtherboneRecordReceiver(Module):
|
2015-04-13 03:53:43 -04:00
|
|
|
def __init__(self, buffer_depth=256):
|
|
|
|
self.sink = sink = Sink(eth_etherbone_record_description(32))
|
|
|
|
self.source = source = Source(eth_etherbone_mmap_description(32))
|
2015-04-13 05:23:27 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
2015-04-13 07:02:04 -04:00
|
|
|
fifo = SyncFIFO(eth_etherbone_record_description(32), buffer_depth,
|
|
|
|
buffered=True)
|
2015-04-13 03:53:43 -04:00
|
|
|
self.submodules += fifo
|
|
|
|
self.comb += Record.connect(sink, fifo.sink)
|
2015-02-11 12:37:59 -05:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
self.submodules.base_addr = base_addr = FlipFlop(32)
|
|
|
|
self.comb += base_addr.d.eq(fifo.source.data)
|
2015-02-11 12:37:59 -05:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
self.submodules.counter = counter = Counter(max=512)
|
2015-02-11 10:21:06 -05:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
self.submodules.fsm = fsm = FSM(reset_state="IDLE")
|
|
|
|
fsm.act("IDLE",
|
|
|
|
fifo.source.ack.eq(1),
|
|
|
|
counter.reset.eq(1),
|
|
|
|
If(fifo.source.stb & fifo.source.sop,
|
|
|
|
base_addr.ce.eq(1),
|
|
|
|
If(fifo.source.wcount,
|
|
|
|
NextState("RECEIVE_WRITES")
|
|
|
|
).Elif(fifo.source.rcount,
|
|
|
|
NextState("RECEIVE_READS")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_WRITES",
|
|
|
|
source.stb.eq(fifo.source.stb),
|
|
|
|
source.sop.eq(counter.value == 0),
|
|
|
|
source.eop.eq(counter.value == fifo.source.wcount-1),
|
|
|
|
source.count.eq(fifo.source.wcount),
|
|
|
|
source.be.eq(fifo.source.byte_enable),
|
|
|
|
source.addr.eq(base_addr.q[2:] + counter.value),
|
|
|
|
source.we.eq(1),
|
|
|
|
source.data.eq(fifo.source.data),
|
|
|
|
fifo.source.ack.eq(source.ack),
|
|
|
|
If(source.stb & source.ack,
|
|
|
|
counter.ce.eq(1),
|
|
|
|
If(source.eop,
|
|
|
|
If(fifo.source.rcount,
|
|
|
|
NextState("RECEIVE_BASE_RET_ADDR")
|
|
|
|
).Else(
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_BASE_RET_ADDR",
|
|
|
|
counter.reset.eq(1),
|
|
|
|
If(fifo.source.stb & fifo.source.sop,
|
|
|
|
base_addr.ce.eq(1),
|
|
|
|
NextState("RECEIVE_READS")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("RECEIVE_READS",
|
|
|
|
source.stb.eq(fifo.source.stb),
|
|
|
|
source.sop.eq(counter.value == 0),
|
|
|
|
source.eop.eq(counter.value == fifo.source.rcount-1),
|
|
|
|
source.count.eq(fifo.source.rcount),
|
|
|
|
source.base_addr.eq(base_addr.q),
|
|
|
|
source.addr.eq(fifo.source.data[2:]),
|
|
|
|
fifo.source.ack.eq(source.ack),
|
|
|
|
If(source.stb & source.ack,
|
|
|
|
counter.ce.eq(1),
|
|
|
|
If(source.eop,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2015-02-11 10:21:06 -05:00
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-02-11 12:37:59 -05:00
|
|
|
class LiteEthEtherboneRecordSender(Module):
|
2015-04-13 03:53:43 -04:00
|
|
|
def __init__(self, buffer_depth=256):
|
|
|
|
self.sink = sink = Sink(eth_etherbone_mmap_description(32))
|
|
|
|
self.source = source = Source(eth_etherbone_record_description(32))
|
2015-04-13 05:23:27 -04:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
2015-04-27 09:06:37 -04:00
|
|
|
pbuffer = Buffer(eth_etherbone_mmap_description(32), buffer_depth)
|
2015-04-13 03:53:43 -04:00
|
|
|
self.submodules += pbuffer
|
|
|
|
self.comb += Record.connect(sink, pbuffer.sink)
|
2015-02-11 12:37:59 -05:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
self.submodules.fsm = fsm = FSM(reset_state="IDLE")
|
|
|
|
fsm.act("IDLE",
|
|
|
|
pbuffer.source.ack.eq(1),
|
|
|
|
If(pbuffer.source.stb & pbuffer.source.sop,
|
|
|
|
pbuffer.source.ack.eq(0),
|
|
|
|
NextState("SEND_BASE_ADDRESS")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.comb += [
|
|
|
|
source.byte_enable.eq(pbuffer.source.be),
|
|
|
|
If(pbuffer.source.we,
|
|
|
|
source.wcount.eq(pbuffer.source.count)
|
|
|
|
).Else(
|
|
|
|
source.rcount.eq(pbuffer.source.count)
|
|
|
|
)
|
|
|
|
]
|
2015-02-11 12:37:59 -05:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
fsm.act("SEND_BASE_ADDRESS",
|
|
|
|
source.stb.eq(pbuffer.source.stb),
|
|
|
|
source.sop.eq(1),
|
|
|
|
source.eop.eq(0),
|
|
|
|
source.data.eq(pbuffer.source.base_addr),
|
|
|
|
If(source.ack,
|
|
|
|
NextState("SEND_DATA")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("SEND_DATA",
|
|
|
|
source.stb.eq(pbuffer.source.stb),
|
|
|
|
source.sop.eq(0),
|
|
|
|
source.eop.eq(pbuffer.source.eop),
|
|
|
|
source.data.eq(pbuffer.source.data),
|
|
|
|
If(source.stb & source.ack,
|
|
|
|
pbuffer.source.ack.eq(1),
|
|
|
|
If(source.eop,
|
|
|
|
NextState("IDLE")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2015-02-11 10:21:06 -05:00
|
|
|
|
2015-04-13 04:20:02 -04:00
|
|
|
|
2015-02-11 19:12:52 -05:00
|
|
|
# Limitation: For simplicity we only support 1 record per packet
|
2015-02-11 10:21:06 -05:00
|
|
|
class LiteEthEtherboneRecord(Module):
|
2015-04-13 03:53:43 -04:00
|
|
|
def __init__(self, endianness="big"):
|
|
|
|
self.sink = sink = Sink(eth_etherbone_packet_user_description(32))
|
|
|
|
self.source = source = Sink(eth_etherbone_packet_user_description(32))
|
2015-04-13 05:23:27 -04:00
|
|
|
|
|
|
|
# # #
|
2015-02-11 19:12:52 -05:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
# receive record, decode it and generate mmap stream
|
|
|
|
self.submodules.depacketizer = depacketizer = LiteEthEtherboneRecordDepacketizer()
|
2015-04-13 04:48:59 -04:00
|
|
|
self.submodules.receiver = receiver = LiteEthEtherboneRecordReceiver()
|
2015-04-13 03:53:43 -04:00
|
|
|
self.comb += [
|
|
|
|
Record.connect(sink, depacketizer.sink),
|
|
|
|
Record.connect(depacketizer.source, receiver.sink)
|
|
|
|
]
|
|
|
|
if endianness is "big":
|
|
|
|
self.comb += receiver.sink.data.eq(reverse_bytes(depacketizer.source.data))
|
2015-02-11 12:37:59 -05:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
# save last ip address
|
|
|
|
last_ip_address = Signal(32)
|
|
|
|
self.sync += [
|
|
|
|
If(sink.stb & sink.sop & sink.ack,
|
|
|
|
last_ip_address.eq(sink.ip_address)
|
|
|
|
)
|
|
|
|
]
|
2015-02-11 15:51:25 -05:00
|
|
|
|
2015-04-13 03:53:43 -04:00
|
|
|
# receive mmap stream, encode it and send records
|
2015-04-13 04:48:59 -04:00
|
|
|
self.submodules.sender = sender = LiteEthEtherboneRecordSender()
|
2015-04-13 03:53:43 -04:00
|
|
|
self.submodules.packetizer = packetizer = LiteEthEtherboneRecordPacketizer()
|
|
|
|
self.comb += [
|
|
|
|
Record.connect(sender.source, packetizer.sink),
|
|
|
|
Record.connect(packetizer.source, source),
|
2015-04-13 07:02:04 -04:00
|
|
|
# XXX improve this
|
2015-04-27 09:06:37 -04:00
|
|
|
source.length.eq(sender.source.wcount*4 + 4 + etherbone_record_header.length),
|
2015-04-13 03:53:43 -04:00
|
|
|
source.ip_address.eq(last_ip_address)
|
|
|
|
]
|
|
|
|
if endianness is "big":
|
|
|
|
self.comb += packetizer.sink.data.eq(reverse_bytes(sender.source.data))
|