uio: support generator trampolining in simulation
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Sat, 17 Nov 2012 18:59:22 +0000 (19:59 +0100)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Sat, 17 Nov 2012 18:59:22 +0000 (19:59 +0100)
migen/uio/ioo.py
migen/uio/trampoline.py [new file with mode: 0644]

index 72dbb391e68d3c740999960528dd0cec732d79e2..c95c60ceb1e1b80b6bb90c2ab1ade8693ec783a3 100644 (file)
@@ -1,6 +1,7 @@
 from migen.fhdl.structure import *
 from migen.flow.actor import *
 from migen.bus import wishbone
+from migen.uio.trampoline import Trampoline
 
 class UnifiedIOObject(Actor):
        def __init__(self, dataflow=None, buses={}):
@@ -12,7 +13,7 @@ class UnifiedIOObject(Actor):
 
 class UnifiedIOSimulation(UnifiedIOObject):
        def __init__(self, generator, dataflow=None, buses={}):
-               self.generator = generator
+               self.generator = Trampoline(generator)
                super().__init__(dataflow, buses)
                
                self.callers = []
diff --git a/migen/uio/trampoline.py b/migen/uio/trampoline.py
new file mode 100644 (file)
index 0000000..912427d
--- /dev/null
@@ -0,0 +1,23 @@
+import types
+
+class Trampoline:
+       def __init__(self, g):
+               self.stack = [g]
+       
+       def __iter__(self):
+               return self
+       
+       def __next__(self):
+               while True:
+                       while True:
+                               try:
+                                       r = next(self.stack[-1])
+                                       break
+                               except StopIteration:
+                                       self.stack.pop()
+                                       if not self.stack:
+                                               raise
+                       if isinstance(r, types.GeneratorType):
+                               self.stack.append(r)
+                       else:
+                               return r