From 829c622140e0e2d230b8f5697b69f8db303b4a75 Mon Sep 17 00:00:00 2001 From: Cesar Strauss Date: Thu, 24 Sep 2020 08:41:29 -0300 Subject: [PATCH] Run-time selecion of simulator engine Allows alternating between cxxsim and pysim at run-time, with minimal code changes to its users. --- src/nmutil/sim_tmp_alternative.py | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/nmutil/sim_tmp_alternative.py diff --git a/src/nmutil/sim_tmp_alternative.py b/src/nmutil/sim_tmp_alternative.py new file mode 100644 index 0000000..4af3c07 --- /dev/null +++ b/src/nmutil/sim_tmp_alternative.py @@ -0,0 +1,63 @@ +"""Run-time selection of simulator engine + +Usage:: + + from nmutil.sim_tmp_alternative import Simulator + +Then, use :py:class:`Simulator` as usual. + +This should be backwards compatible to old developer versions of nMigen. + +To use cxxsim, export ``NMIGEN_SIM_MODE=cxxsim`` from the shell. +Be sure to check out the ``cxxsim`` branch of nMigen, and update yosys +to the latest commit as well. + +To use pysim, just keep ``NMIGEN_SIM_MODE`` unset. +Alternatively, export ``NMIGEN_SIM_MODE=pysim``. + +Example:: + + $ export NMIGEN_SIM_MODE=... # pysim or cxxsim, default is pysim + $ python ... + +or, even:: + + $ NMIGEN_SIM_MODE=... python ... +""" + +import os + +try: + from nmigen.sim import Simulator as RealSimulator + detected_new_api = True +except ImportError: + detected_new_api = False + try: + from nmigen.sim.pysim import Simulator as RealSimulator + except ImportError: + from nmigen.back.pysim import Simulator as RealSimulator + +nmigen_sim_environ_variable = os.environ.get("NMIGEN_SIM_MODE") \ + or "pysim" +"""Detected run-time engine from environment""" + + +def Simulator(*args, **kwargs): + """Wrapper that allows run-time selection of simulator engine""" + if detected_new_api: + kwargs['engine'] = nmigen_sim_environ_variable + return RealSimulator(*args, **kwargs) + + +def is_engine_cxxsim(): + """Returns ``True`` if the selected engine is cxxsim""" + return nmigen_sim_environ_variable == "cxxsim" + + +def is_engine_pysim(): + """Returns ``True`` if the selected engine is pysim""" + return nmigen_sim_environ_variable == "pysim" + + +nmigen_sim_top_module = "top." if is_engine_pysim() else "" +"""Work-around for cxxsim not defining the top-level module""" -- 2.30.2