amaranth/nmigen/compat/sim/__init__.py
whitequark 4922a73c5d test.compat: import tests from Migen as appropriate.
test_signed and test_coding are adjusted slightly to account for
differences in comb propagation between the simulators; we might want
to revert that eventually.
2019-01-26 01:01:03 +00:00

40 lines
1.3 KiB
Python

import functools
import collections
import inspect
from ...back.pysim import *
__all__ = ["run_simulation", "passive"]
def run_simulation(fragment_or_module, generators, clocks={"sync": 10}, vcd_name=None,
special_overrides={}):
assert not special_overrides
if hasattr(fragment_or_module, "get_fragment"):
fragment = fragment_or_module.get_fragment().get_fragment(platform=None)
else:
fragment = fragment_or_module
if not isinstance(generators, dict):
generators = {"sync": generators}
with Simulator(fragment, vcd_file=open(vcd_name, "w") if vcd_name else None) as sim:
for domain, period in clocks.items():
sim.add_clock(period / 1e9, domain=domain)
for domain, processes in generators.items():
if isinstance(processes, collections.Iterable) and not inspect.isgenerator(processes):
for process in processes:
sim.add_sync_process(process, domain=domain)
else:
sim.add_sync_process(processes, domain=domain)
sim.run()
def passive(generator):
@functools.wraps(generator)
def wrapper(*args, **kwargs):
yield Passive()
yield from generator(*args, **kwargs)
return wrapper