speed up ==, hash, <, >, <=, and >= for plain_data
[nmutil.git] / src / nmutil / sim_tmp_alternative.py
1 """Run-time selection of simulator engine
2
3 Usage::
4
5 from nmutil.sim_tmp_alternative import Simulator
6
7 Then, use :py:class:`Simulator` as usual.
8
9 This should be backwards compatible to old developer versions of nMigen.
10
11 To use cxxsim, export ``NMIGEN_SIM_MODE=cxxsim`` from the shell.
12 Be sure to check out the ``cxxsim`` branch of nMigen, and update yosys
13 to the latest commit as well.
14
15 To use pysim, just keep ``NMIGEN_SIM_MODE`` unset.
16 Alternatively, export ``NMIGEN_SIM_MODE=pysim``.
17
18 Example::
19
20 $ export NMIGEN_SIM_MODE=... # pysim or cxxsim, default is pysim
21 $ python ...
22
23 or, even::
24
25 $ NMIGEN_SIM_MODE=... python ...
26 """
27
28 import os
29
30 try:
31 from nmigen.sim import (Simulator as RealSimulator, Delay, Settle, Tick,
32 Passive)
33 detected_new_api = True
34 except ImportError:
35 detected_new_api = False
36 try:
37 from nmigen.sim.pysim import (Simulator as RealSimulator,
38 Delay, Settle, Tick, Passive)
39 except ImportError:
40 from nmigen.back.pysim import (Simulator as RealSimulator,
41 Delay, Settle, Tick, Passive)
42
43 nmigen_sim_environ_variable = os.environ.get("NMIGEN_SIM_MODE") \
44 or "pysim"
45 """Detected run-time engine from environment"""
46
47
48 def Simulator(*args, **kwargs):
49 """Wrapper that allows run-time selection of simulator engine"""
50 if detected_new_api and 'engine' not in kwargs:
51 kwargs['engine'] = nmigen_sim_environ_variable
52 return RealSimulator(*args, **kwargs)
53
54
55 def is_engine_cxxsim():
56 """Returns ``True`` if the selected engine is cxxsim"""
57 return detected_new_api and nmigen_sim_environ_variable == "cxxsim"
58
59
60 def is_engine_pysim():
61 """Returns ``True`` if the selected engine is pysim"""
62 return not detected_new_api or nmigen_sim_environ_variable == "pysim"
63
64
65 nmigen_sim_top_module = "top." if is_engine_pysim() else ""
66 """Work-around for cxxsim not defining the top-level module"""