Skip to content

Commit 9aebf49

Browse files
committed
sim.pysim: only import pyvcd when needed.
In some environments (e.g. Pyodide) it may be advantageous to not load this library, and with the import at file level, it makes the entire simulator unusable, not just `PySimEngine.write_vcd`. This might also help people whose Python environments are unusually broken, whom we've historically accommodated.
1 parent 5797643 commit 9aebf49

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

amaranth/sim/pysim.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from contextlib import contextmanager
22
import itertools
33
import re
4-
from vcd import VCDWriter
5-
from vcd.gtkw import GTKWSave
64

75
from ..hdl import *
86
from ..hdl._repr import *
@@ -39,6 +37,10 @@ def eval_field(field, signal, value):
3937
raise NotImplementedError
4038

4139
def __init__(self, fragment, *, vcd_file, gtkw_file=None, traces=()):
40+
# Although pyvcd is a mandatory dependency, be resilient and import it as needed, so that
41+
# the simulator is still usable if it's not installed for some reason.
42+
import vcd, vcd.gtkw
43+
4244
if isinstance(vcd_file, str):
4345
vcd_file = open(vcd_file, "w")
4446
if isinstance(gtkw_file, str):
@@ -47,13 +49,13 @@ def __init__(self, fragment, *, vcd_file, gtkw_file=None, traces=()):
4749
self.vcd_signal_vars = SignalDict()
4850
self.vcd_memory_vars = {}
4951
self.vcd_file = vcd_file
50-
self.vcd_writer = vcd_file and VCDWriter(self.vcd_file,
52+
self.vcd_writer = vcd_file and vcd.VCDWriter(self.vcd_file,
5153
timescale="1 ps", comment="Generated by Amaranth")
5254

5355
self.gtkw_signal_names = SignalDict()
5456
self.gtkw_memory_names = {}
5557
self.gtkw_file = gtkw_file
56-
self.gtkw_save = gtkw_file and GTKWSave(self.gtkw_file)
58+
self.gtkw_save = gtkw_file and vcd.gtkw.GTKWSave(self.gtkw_file)
5759

5860
self.traces = []
5961

0 commit comments

Comments
 (0)