
Before this commit, each simulation engine (which is only pysim at the moment, but also cxxsim soon) was a subclass of SimulatorCore, and every simulation engine module would essentially duplicate the complete structure of a simulator, with code partially shared. This was a really bad idea: it was inconvenient to use, with downstream code having to branch between e.g. PySettle and CxxSettle; it had no well-defined external interface; it had multiple virtually identical entry points; and it had no separation between simulation algorithms and glue code. This commit completely rearranges simulation code. 1. sim._base defines internal simulation interfaces. The clarity of these internal interfaces is important because simulation engines mix and match components to provide a consistent API regardless of the chosen engine. 2. sim.core defines the external simulation interface: the commands and the simulator facade. The facade provides a single entry point and, when possible, validates or lowers user input. It also imports built-in simulation engines by their symbolic name, avoiding eager imports of pyvcd or ctypes. 3. sim.xxxsim (currently, only sim.pysim) defines the simulator implementation: time and state management, process scheduling, and waveform dumping. The new simulator structure has none of the downsides of the old one. See #324.
55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
import functools
|
|
import inspect
|
|
from collections.abc import Iterable
|
|
from ...hdl.cd import ClockDomain
|
|
from ...hdl.ir import Fragment
|
|
from ...sim 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()
|
|
else:
|
|
fragment = fragment_or_module
|
|
|
|
fragment = Fragment.get(fragment, platform=None)
|
|
|
|
if not isinstance(generators, dict):
|
|
generators = {"sync": generators}
|
|
if "sync" not in fragment.domains:
|
|
fragment.add_domains(ClockDomain("sync"))
|
|
|
|
sim = Simulator(fragment)
|
|
for domain, period in clocks.items():
|
|
sim.add_clock(period / 1e9, domain=domain)
|
|
for domain, processes in generators.items():
|
|
def wrap(process):
|
|
def wrapper():
|
|
yield from process
|
|
return wrapper
|
|
if isinstance(processes, Iterable) and not inspect.isgenerator(processes):
|
|
for process in processes:
|
|
sim.add_sync_process(wrap(process), domain=domain)
|
|
else:
|
|
sim.add_sync_process(wrap(processes), domain=domain)
|
|
|
|
if vcd_name is not None:
|
|
with sim.write_vcd(vcd_name):
|
|
sim.run()
|
|
else:
|
|
sim.run()
|
|
|
|
|
|
def passive(generator):
|
|
@functools.wraps(generator)
|
|
def wrapper(*args, **kwargs):
|
|
yield Passive()
|
|
yield from generator(*args, **kwargs)
|
|
return wrapper
|