From 7f767095ec4332d6b1d4c9c351d8b58ae8cc3fe8 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 20 Sep 2015 15:04:15 +0800 Subject: [PATCH] sim: support generators yielding statements --- examples/basic/graycounter.py | 2 +- examples/sim/basic2.py | 4 ++-- examples/sim/fir.py | 2 +- examples/sim/memory.py | 2 +- migen/sim.py | 14 ++++++++------ migen/test/test_coding.py | 10 +++++----- migen/test/test_divider.py | 8 ++++---- migen/test/test_fifo.py | 11 ++++++----- migen/test/test_signed.py | 4 ++-- migen/test/test_sort.py | 2 +- 10 files changed, 31 insertions(+), 28 deletions(-) diff --git a/examples/basic/graycounter.py b/examples/basic/graycounter.py index bedd46658..bf73e7899 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 524f4435b..518286020 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 c9adcb2cd..a7b98036c 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 3071bc9ec..4d8ca675b 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 b34d44248..eee9a41b2 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 965a7e975..84d92eb3d 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<