From: Sebastien Bourdeauducq Date: Sun, 20 Sep 2015 07:04:15 +0000 (+0800) Subject: sim: support generators yielding statements X-Git-Tag: 24jan2021_ls180~2099^2~3^2~61 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7f767095ec4332d6b1d4c9c351d8b58ae8cc3fe8;p=litex.git sim: support generators yielding statements --- diff --git a/examples/basic/graycounter.py b/examples/basic/graycounter.py index bedd4665..bf73e789 100644 --- a/examples/basic/graycounter.py +++ b/examples/basic/graycounter.py @@ -9,7 +9,7 @@ def tb(dut): for i in range(35): print("{0:0{1}b} CE={2} bin={3}".format((yield dut.q), flen(dut.q), (yield dut.ce), (yield dut.q_binary))) - yield dut.ce, prng.getrandbits(1) + yield dut.ce.eq(prng.getrandbits(1)) yield diff --git a/examples/sim/basic2.py b/examples/sim/basic2.py index 524f4435..51828602 100644 --- a/examples/sim/basic2.py +++ b/examples/sim/basic2.py @@ -18,9 +18,9 @@ def counter_test(dut): # Only assert CE every second cycle. # => each counter value is held for two cycles. if cycle % 2: - yield dut.ce, 0 # This is how you write to a signal. + yield dut.ce.eq(0) # This is how you write to a signal. else: - yield dut.ce, 1 + yield dut.ce.eq(1) print("Cycle: {} Count: {}".format(cycle, (yield dut.count))) yield diff --git a/examples/sim/fir.py b/examples/sim/fir.py index c9adcb2c..a7b98036 100644 --- a/examples/sim/fir.py +++ b/examples/sim/fir.py @@ -38,7 +38,7 @@ def fir_tb(dut, frequency, inputs, outputs): f = 2**(dut.wsize - 1) for cycle in range(200): v = 0.1*cos(2*pi*frequency*cycle) - yield dut.i, int(f*v) + yield dut.i.eq(int(f*v)) inputs.append(v) outputs.append((yield dut.o)/f) yield diff --git a/examples/sim/memory.py b/examples/sim/memory.py index 3071bc9e..4d8ca675 100644 --- a/examples/sim/memory.py +++ b/examples/sim/memory.py @@ -11,7 +11,7 @@ class Mem(Module): def memory_test(dut): # write (only first 5 values) for i in range(5): - yield dut.mem[i], 42 + i + yield dut.mem[i].eq(42 + i) # remember: values are written after the tick, and read before the tick. # wait one tick for the memory to update. yield diff --git a/migen/sim.py b/migen/sim.py index b34d4424..eee9a41b 100644 --- a/migen/sim.py +++ b/migen/sim.py @@ -1,7 +1,8 @@ import operator from migen.fhdl.structure import * -from migen.fhdl.structure import (_Value, _Operator, _Slice, _ArrayProxy, +from migen.fhdl.structure import (_Value, _Statement, + _Operator, _Slice, _ArrayProxy, _Assign, _Fragment) from migen.fhdl.bitcontainer import flen from migen.fhdl.tools import list_targets @@ -207,11 +208,14 @@ class Simulator: self.evaluator.execute(self.fragment.comb) modified = self.evaluator.commit() - def _eval_nested_lists(self, x): + def _evalexec_nested_lists(self, x): if isinstance(x, list): - return [self._eval_nested_lists(e) for e in x] + return [self._evalexec_nested_lists(e) for e in x] elif isinstance(x, _Value): return self.evaluator.eval(x) + elif isinstance(x, _Statement): + self.evaluator.execute([x]) + return None else: raise ValueError @@ -224,10 +228,8 @@ class Simulator: request = generator.send(reply) if request is None: break # next cycle - elif isinstance(request, tuple): - self.evaluator.assign(*request) else: - reply = self._eval_nested_lists(request) + reply = self._evalexec_nested_lists(request) except StopIteration: exhausted.append(generator) break diff --git a/migen/test/test_coding.py b/migen/test/test_coding.py index 965a7e97..84d92eb3 100644 --- a/migen/test/test_coding.py +++ b/migen/test/test_coding.py @@ -21,7 +21,7 @@ class EncCase(SimCase, unittest.TestCase): def gen(): for _ in range(256): if seq: - yield self.tb.dut.i, seq.pop(0) + yield self.tb.dut.i.eq(seq.pop(0)) if (yield self.tb.dut.n): self.assertNotIn((yield self.tb.dut.i), [1<