tools/remove/etherbone: simplify/speed up encoding.

This commit is contained in:
Florent Kermarrec 2020-11-25 10:00:28 +01:00
parent 2a1df9beeb
commit 01e75addff
2 changed files with 34 additions and 27 deletions

View File

@ -58,7 +58,7 @@ class RemoteClient(EtherboneIPC, CSRBuilder):
packet = EtherbonePacket() packet = EtherbonePacket()
packet.records = [record] packet.records = [record]
packet.encode() packet.encode()
self.send_packet(self.socket, packet[:]) self.send_packet(self.socket, packet)
# receive response # receive response
packet = EtherbonePacket(self.receive_packet(self.socket)) packet = EtherbonePacket(self.receive_packet(self.socket))

View File

@ -108,11 +108,9 @@ class EtherboneWrites(Packet):
def encode(self): def encode(self):
if self.encoded: if self.encoded:
raise ValueError raise ValueError
for byte in self.base_addr.to_bytes(4, "big"): self.bytes = self.base_addr.to_bytes(4, "big")
self.append(byte)
for write in self.writes: for write in self.writes:
for byte in write.data.to_bytes(4, "big"): self.bytes += write.data.to_bytes(4, "big")
self.append(byte)
self.encoded = True self.encoded = True
def decode(self): def decode(self):
@ -163,11 +161,9 @@ class EtherboneReads(Packet):
def encode(self): def encode(self):
if self.encoded: if self.encoded:
raise ValueError raise ValueError
for byte in self.base_ret_addr.to_bytes(4, "big"): self.bytes = self.base_ret_addr.to_bytes(4, "big")
self.append(byte)
for read in self.reads: for read in self.reads:
for byte in read.addr.to_bytes(4, "big"): self.bytes += read.addr.to_bytes(4, "big")
self.append(byte)
self.encoded = True self.encoded = True
def decode(self): def decode(self):
@ -249,28 +245,39 @@ class EtherboneRecord(Packet):
def set_writes(self, writes): def set_writes(self, writes):
self.wcount = len(writes.writes) self.wcount = len(writes.writes)
writes.encode() writes.encode()
for byte in writes: for byte in writes.bytes:
self.append(byte) self.append(byte)
def set_reads(self, reads): def set_reads(self, reads):
self.rcount = len(reads.reads) self.rcount = len(reads.reads)
reads.encode() reads.encode()
for byte in reads: for byte in reads.bytes:
self.append(byte) self.append(byte)
def encode(self): def encode(self):
if self.encoded: if self.encoded:
raise ValueError raise ValueError
if self.writes is not None: # Set writes/reads count
self.set_writes(self.writes) self.wcount = 0 if self.writes is None else len(self.writes.writes)
if self.reads is not None: self.rcount = 0 if self.reads is None else len(self.reads.reads)
self.set_reads(self.reads)
# Encode header
header = 0 header = 0
for k, v in sorted(etherbone_record_header.fields.items()): for k, v in sorted(etherbone_record_header.fields.items()):
value = int.from_bytes(getattr(self, k).to_bytes(math.ceil(v.width/8), "big"), "little") value = int.from_bytes(getattr(self, k).to_bytes(math.ceil(v.width/8), "big"), "little")
header += (value << v.offset+(v.byte*8)) header += (value << v.offset+(v.byte*8))
for d in header.to_bytes(etherbone_record_header.length, "big"): self.bytes = header.to_bytes(etherbone_record_header.length, "little")
self.insert(0, d)
# Encode writes
if self.wcount:
self.writes.encode()
self.bytes += self.writes.bytes
# Encode reads
if self.rcount:
self.reads.encode()
self.bytes += self.reads.bytes
self.encoded = True self.encoded = True
def __repr__(self, n=0): def __repr__(self, n=0):
@ -326,22 +333,22 @@ class EtherbonePacket(Packet):
self.records = self.get_records() self.records = self.get_records()
self.encoded = False self.encoded = False
def set_records(self, records):
for record in records:
record.encode()
for byte in record:
self.append(byte)
def encode(self): def encode(self):
if self.encoded: if self.encoded:
raise ValueError raise ValueError
self.set_records(self.records)
# Encode header
header = 0 header = 0
for k, v in sorted(etherbone_packet_header.fields.items()): for k, v in sorted(etherbone_packet_header.fields.items()):
value = int.from_bytes(getattr(self, k).to_bytes(math.ceil(v.width/8), "big"), "little") value = int.from_bytes(getattr(self, k).to_bytes(math.ceil(v.width/8), "big"), "little")
header += (value << v.offset+(v.byte*8)) header += (value << v.offset+(v.byte*8))
for d in header.to_bytes(etherbone_packet_header.length, "big"): self.bytes = header.to_bytes(etherbone_packet_header.length, "little")
self.insert(0, d)
# Encode records
for record in self.records:
record.encode()
self.bytes += record.bytes
self.encoded = True self.encoded = True
def __repr__(self): def __repr__(self):
@ -361,7 +368,7 @@ class EtherbonePacket(Packet):
class EtherboneIPC: class EtherboneIPC:
def send_packet(self, socket, packet): def send_packet(self, socket, packet):
socket.sendall(bytes(packet)) socket.sendall(packet.bytes)
def receive_packet(self, socket): def receive_packet(self, socket):
header_length = etherbone_packet_header_length + etherbone_record_header_length header_length = etherbone_packet_header_length + etherbone_record_header_length