manta/test/test_io_core_sim.py

109 lines
3.2 KiB
Python
Raw Normal View History

2024-02-18 22:50:26 +01:00
from amaranth import *
2024-08-03 22:18:58 +02:00
from manta import *
2023-12-28 23:22:29 +01:00
from manta.utils import *
from random import getrandbits
2023-12-28 23:22:29 +01:00
2024-02-18 22:50:26 +01:00
probe0 = Signal(1)
probe1 = Signal(2)
probe2 = Signal(8)
probe3 = Signal(20)
inputs = [probe0, probe1, probe2, probe3]
2023-12-28 23:22:29 +01:00
2024-06-20 20:47:34 +02:00
probe4 = Signal(1, init=1)
probe5 = Signal(2, init=2)
2024-02-18 22:50:26 +01:00
probe6 = Signal(8)
2024-06-20 20:47:34 +02:00
probe7 = Signal(20, init=65538)
2024-02-18 22:50:26 +01:00
outputs = [probe4, probe5, probe6, probe7]
io_core = IOCore(inputs=inputs, outputs=outputs)
io_core.base_addr = 0
_ = io_core.max_addr
2023-12-28 23:22:29 +01:00
2024-06-20 20:47:34 +02:00
async def pulse_strobe_register(ctx):
2024-02-18 22:50:26 +01:00
strobe_addr = io_core._memory_map["strobe"]["addrs"][0]
2024-06-20 20:47:34 +02:00
await write_register(io_core, ctx, strobe_addr, 0)
await write_register(io_core, ctx, strobe_addr, 1)
await write_register(io_core, ctx, strobe_addr, 0)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2024-06-20 20:47:34 +02:00
async def test_input_probe_buffer_initial_value(ctx):
2024-03-03 11:14:12 +01:00
# Verify all input probe buffers initialize to zero
for i in inputs:
addrs = io_core._memory_map[i.name]["addrs"]
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
for addr in addrs:
2024-06-20 20:47:34 +02:00
await verify_register(io_core, ctx, addr, 0)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2024-06-20 20:47:34 +02:00
async def test_output_probe_buffer_initial_value(ctx):
2024-03-03 11:14:12 +01:00
# Verify all output probe buffers initialize to the values in the config
for o in outputs:
addrs = io_core._memory_map[o.name]["addrs"]
datas = value_to_words(o.init, len(addrs))
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
for addr, data in zip(addrs, datas):
2024-06-20 20:47:34 +02:00
await verify_register(io_core, ctx, addr, data)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2024-06-20 20:47:34 +02:00
async def test_output_probes_are_writeable(ctx):
2024-03-03 11:14:12 +01:00
for o in outputs:
addrs = io_core._memory_map[o.name]["addrs"]
2024-06-20 20:47:34 +02:00
test_value = getrandbits(len(o))
2024-03-03 11:14:12 +01:00
datas = value_to_words(test_value, len(addrs))
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# write value to registers
for addr, data in zip(addrs, datas):
2024-06-20 20:47:34 +02:00
await write_register(io_core, ctx, addr, data)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# read value back from registers
for addr, data in zip(addrs, datas):
2024-06-20 20:47:34 +02:00
await verify_register(io_core, ctx, addr, data)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2024-06-20 20:47:34 +02:00
async def test_output_probes_update(ctx):
2024-03-03 11:14:12 +01:00
for o in outputs:
addrs = io_core._memory_map[o.name]["addrs"]
2024-06-20 20:47:34 +02:00
test_value = getrandbits(len(o))
2024-03-03 11:14:12 +01:00
datas = value_to_words(test_value, len(addrs))
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# write value to registers
for addr, data in zip(addrs, datas):
2024-06-20 20:47:34 +02:00
await write_register(io_core, ctx, addr, data)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# pulse strobe register
2024-06-20 20:47:34 +02:00
await pulse_strobe_register(ctx)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# check that outputs took updated value
2024-06-20 20:47:34 +02:00
value = ctx.get(o)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
if value != test_value:
raise ValueError(
f"Output probe {o.name} took value {value} instead of {test_value} after pulsing strobe."
)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
else:
print(f"Output probe {o.name} took value {value} after pulsing strobe.")
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2024-06-20 20:47:34 +02:00
async def test_input_probes_update(ctx):
2024-03-03 11:14:12 +01:00
for i in inputs:
# set input probe value
2024-06-20 20:47:34 +02:00
test_value = getrandbits(len(i))
ctx.set(i, test_value)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# pulse strobe register
2024-06-20 20:47:34 +02:00
await pulse_strobe_register(ctx)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# check that values are as expected once read back
addrs = io_core._memory_map[i.name]["addrs"]
datas = value_to_words(test_value, len(addrs))
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
for addr, data in zip(addrs, datas):
2024-06-20 20:47:34 +02:00
await verify_register(io_core, ctx, addr, data)