Skip to content

Commit

Permalink
applets.axi_writer_demo: add sample run and verify method
Browse files Browse the repository at this point in the history
  • Loading branch information
rroohhh committed Aug 16, 2021
1 parent 5128d23 commit 08952bf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
63 changes: 59 additions & 4 deletions applets/axi_writer_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
class Top(Elaboratable):
def __init__(self):
self.reset = ControlSignal()
self.to_write = ControlSignal(reset=32 * 1024 * 1024)
self.to_write = ControlSignal(32)
self.packet_size = ControlSignal(32, reset=1 * 1024 * 1024)
self.data_counter = StatusSignal(32)
self.data_valid = ControlSignal()
self.packet_counter = StatusSignal(32)
self.data_ready = StatusSignal()

def elaborate(self, platform: ZynqSocPlatform):
Expand All @@ -19,16 +20,70 @@ def elaborate(self, platform: ZynqSocPlatform):

stream = PacketizedStream(64)
m.d.comb += self.data_ready.eq(stream.ready)
m.d.comb += stream.valid.eq(self.data_valid)

axi_writer = m.submodules.axi_writer = DramPacketRingbufferStreamWriter(stream, max_packet_size=0x1200000, n_buffers=4)
axi_writer = m.submodules.axi_writer = DramPacketRingbufferStreamWriterV2(stream, max_packet_size=0x1200000, n_buffers=4)
self.axi_writer = axi_writer

with m.If(self.data_counter < self.to_write):
m.d.comb += stream.valid.eq(1)

with m.If(((self.packet_counter + 1) == self.packet_size) | ((self.data_counter + 1) == self.to_write)):
m.d.comb += stream.last.eq(1)


with m.If(axi_writer.input.ready & axi_writer.input.valid):
m.d.sync += self.data_counter.eq(self.data_counter + 1)

with m.If((self.packet_counter + 1) == self.packet_size):
m.d.sync += self.packet_counter.eq(0)
with m.Else():
m.d.sync += self.packet_counter.eq(self.packet_counter + 1)

m.d.comb += stream.payload.eq(Cat(self.data_counter, self.data_counter + 1000))

return m

@driver_method
def run_and_check(self, to_write = 4 * 1024 * 1024, packet_size = 1 * 1024 * 1024):
self.reset = 1
self.to_write = to_write
self.packet_size = packet_size
self.reset = 0

import time
time.sleep(0.5)

written_buffers = (to_write + packet_size - 1) // packet_size
assert self.data_counter == self.to_write
assert self.axi_writer.buffers_written == written_buffers

base_address = self.axi_writer.base_address
max_buffer = max(self.axi_writer.buffer_base_list_cpu)
map_len = max_buffer + packet_size * 8 - base_address

import mmap, os, sys
mem = mmap.mmap(
os.open('/dev/mem', os.O_RDWR | os.O_SYNC),
map_len, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE,
offset = base_address
)

last = ((written_buffers - 1) // self.axi_writer.n_buffers) * self.axi_writer.n_buffers * packet_size - 1
for buf_addr in self.axi_writer.buffer_base_list_cpu:
for w in range(packet_size):
if w % 1000 == 0:
print(".", end="")
sys.stdout.flush()
addr = buf_addr - base_address + 8 * w
val = int.from_bytes(mem[addr:addr+8], 'little')
lower = val & ((1 << 32) - 1)
upper = val >> 32
# print(lower, upper, last)
assert lower == last + 1, f"[{buf_addr:08x}, {w:08x}]: {lower} != {last} + 1"
assert upper == lower + 1000, f"[{buf_addr:0x8}, {w:08x}]: {upper} != {lower} + 1000"
last = lower



if __name__ == "__main__":
cli(Top, runs_on=(MicroR2Platform, BetaPlatform, ZyboPlatform), possible_socs=(ZynqSocPlatform,))
3 changes: 2 additions & 1 deletion naps/cores/dram_packet_ringbuffer/stream_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def __init__(
self.n_buffers = n_buffers
self.axi = axi

self.buffer_base_list = Array([base_address + max_packet_size * i for i in range(n_buffers)])
self.buffer_base_list_cpu = [base_address + max_packet_size * i for i in range(n_buffers)]
self.buffer_base_list = Array(self.buffer_base_list_cpu)
self.buffer_level_list = Array([Signal(range(max_packet_size), name=f'buffer{i}_level') for i in range(n_buffers)])
self.current_write_buffer = Signal(range(n_buffers))

Expand Down

0 comments on commit 08952bf

Please sign in to comment.