manta/test/test_io_core_sim.py

108 lines
3.2 KiB
Python
Raw Normal View History

2024-02-18 22:50:26 +01:00
from amaranth import *
2023-12-28 23:22:29 +01:00
from amaranth.sim import Simulator
from manta.io_core import IOCore
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-02-18 22:50:26 +01:00
probe4 = Signal(1, reset=1)
probe5 = Signal(2, reset=2)
probe6 = Signal(8)
probe7 = Signal(20, reset=65538)
outputs = [probe4, probe5, probe6, probe7]
io_core = IOCore(base_addr=0, interface=None, inputs=inputs, outputs=outputs)
2023-12-28 23:22:29 +01:00
def pulse_strobe_register():
2024-02-18 22:50:26 +01:00
strobe_addr = io_core._memory_map["strobe"]["addrs"][0]
2023-12-28 23:22:29 +01:00
yield from write_register(io_core, strobe_addr, 0)
yield from write_register(io_core, strobe_addr, 1)
yield from write_register(io_core, strobe_addr, 0)
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2023-12-28 23:22:29 +01:00
def test_input_probe_buffer_initial_value():
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:
yield from verify_register(io_core, addr, 0)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2023-12-28 23:22:29 +01:00
def test_output_probe_buffer_initial_value():
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.reset, 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):
yield from verify_register(io_core, addr, data)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2023-12-28 23:22:29 +01:00
def test_output_probes_are_writeable():
2024-03-03 11:14:12 +01:00
for o in outputs:
addrs = io_core._memory_map[o.name]["addrs"]
test_value = getrandbits(o.width)
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):
yield from write_register(io_core, 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):
yield from verify_register(io_core, addr, data)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
@simulate(io_core)
2023-12-28 23:22:29 +01:00
def test_output_probes_update():
2024-03-03 11:14:12 +01:00
for o in outputs:
addrs = io_core._memory_map[o.name]["addrs"]
test_value = getrandbits(o.width)
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):
yield from write_register(io_core, addr, data)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# pulse strobe register
yield from pulse_strobe_register()
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# check that outputs took updated value
value = yield (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)
2023-12-28 23:22:29 +01:00
def test_input_probes_update():
2024-03-03 11:14:12 +01:00
for i in inputs:
# set input probe value
test_value = getrandbits(i.width)
2024-03-03 11:14:12 +01:00
yield i.eq(test_value)
2023-12-28 23:22:29 +01:00
2024-03-03 11:14:12 +01:00
# pulse strobe register
yield from pulse_strobe_register()
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):
yield from verify_register(io_core, addr, data)