From 8947096eead36592eedc59462b2b1470a0c65032 Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 12 Feb 2020 14:42:06 +0000 Subject: [PATCH] back.pysim: accept write_vcd(vcd_file=None). Because write_vcd() is a context manager, this is useful if the VCD file should be sometimes not written, since it avoids awkward conditionals with duplicated code. It's not very elegant though. Fixes #319. --- nmigen/back/pysim.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/nmigen/back/pysim.py b/nmigen/back/pysim.py index 9ae476f..6fe3d9e 100644 --- a/nmigen/back/pysim.py +++ b/nmigen/back/pysim.py @@ -94,6 +94,9 @@ class _VCDWaveformWriter(_WaveformWriter): trace_names[trace] = trace.name self.traces.append(trace) + if self.vcd_writer is None: + return + for signal, names in itertools.chain(signal_names.items(), trace_names.items()): if signal.decoder: var_type = "string" @@ -139,7 +142,8 @@ class _VCDWaveformWriter(_WaveformWriter): self.vcd_writer.change(vcd_var, vcd_timestamp, var_value) def close(self, timestamp): - self.vcd_writer.close(self.timestamp_to_vcd(timestamp)) + if self.vcd_writer is not None: + self.vcd_writer.close(self.timestamp_to_vcd(timestamp)) if self.gtkw_save is not None: self.gtkw_save.dumpfile(self.vcd_file.name) @@ -153,7 +157,8 @@ class _VCDWaveformWriter(_WaveformWriter): suffix = "" self.gtkw_save.trace(".".join(self.gtkw_names[signal]) + suffix) - self.vcd_file.close() + if self.vcd_file is not None: + self.vcd_file.close() if self.gtkw_file is not None: self.gtkw_file.close() -- 2.30.2