1 """ Example 5: Making use of PyRTL and Introspection. """
3 from nmigen
import Module
, Signal
, Const
4 from nmigen
.cli
import main
, verilog
, rtlil
7 from pipeline
import SimplePipeline
, ObjectProxy
, PipeManager
10 class SimplePipelineExample(SimplePipeline
):
11 """ A very simple pipeline to show how registers are inferred. """
13 def __init__(self
, pipe
):
14 SimplePipeline
.__init
__(self
, pipe
)
15 self
._loopback
= Signal(4)
19 self
.n
= ~self
._loopback
26 self
._pipe
.comb
+= localv
.eq(2)
27 self
.n
= self
.n
<< localv
33 self
._pipe
.sync
+= self
._loopback
.eq(self
.n
+ 3)
36 class ObjectBasedPipelineExample(SimplePipeline
):
37 """ A very simple pipeline to show how registers are inferred. """
39 def __init__(self
, m
):
40 SimplePipeline
.__init
__(self
, m
)
41 self
._loopback
= Signal(4)
49 self
.n
= ~self
._loopback
53 self
.n
= self
.n
+ self
.o
.a
54 o
= ObjectProxy(self
._m
)
56 o
.b
= self
.o
.b
+ self
.n
+ Const(5)
61 self
._m
.d
.comb
+= localv
.eq(2)
62 self
.n
= self
.n
<< localv
63 o
= ObjectProxy(self
._m
)
64 o
.b
= self
.n
+ self
.o
.a
+ self
.o
.b
70 self
.o
.b
= self
.o
.b
+ self
.n
73 self
._m
.d
.sync
+= self
._loopback
.eq(self
.n
+ 3 + self
.o
.b
)
80 self
.p
= ObjectBasedPipelineExample(self
.m
)
82 def get_fragment(self
, platform
=None):
86 class PipelineStageExample
:
89 self
._loopback
= Signal(4)
91 def get_fragment(self
, platform
=None):
95 with
PipeManager(m
, pipemode
=True) as pipe
:
97 with pipe
.Stage("first", ispec
=[self
._loopback
]) as (p
, m
):
99 with pipe
.Stage("second", p
) as (p
, m
):
100 #p.n = ~self._loopback + 2
102 with pipe
.Stage("third", p
) as (p
, m
):
103 #p.n = ~self._loopback + 5
105 m
.d
.comb
+= localv
.eq(2)
106 p
.n
= p
.n
<< localv
+ 1
113 class PipelineStageObjectExample
:
116 self
._loopback
= Signal(4)
118 def get_fragment(self
, platform
=None):
122 o
= ObjectProxy(None, pipemode
=False)
128 m
.d
.sync
+= localv2
.eq(localv2
+ 3)
130 #m.d.comb += self.obj.a.eq(localv2 + 1)
131 #m.d.sync += self._loopback.eq(localv2)
133 ispec
= [self
._loopback
, self
._obj
]
134 with
PipeManager(m
, pipemode
=True) as pipe
:
136 with pipe
.Stage("first",
137 ispec
=ispec
) as (p
, m
):
138 p
.n
= ~self
._loopback
140 with pipe
.Stage("second", p
) as (p
, m
):
141 #p.n = ~self._loopback + 2
143 o
= ObjectProxy(None, pipemode
=False)
145 o
.b
= p
.o
.b
+ p
.n
+ Const(5)
147 with pipe
.Stage("third", p
) as (p
, m
):
148 #p.n = ~self._loopback + 5
150 m
.d
.comb
+= localv
.eq(2)
152 o
= ObjectProxy(None, pipemode
=False)
153 o
.b
= p
.n
+ p
.o
.b
+ p
.o
.a
156 print ("stages", pipe
.stages
)
162 if __name__
== "__main__":
163 example
= PipeModule()
164 with
open("pipe_module.il", "w") as f
:
165 f
.write(rtlil
.convert(example
, ports
=[
168 example
= PipelineStageExample()
169 with
open("pipe_stage_module.il", "w") as f
:
170 f
.write(rtlil
.convert(example
, ports
=[
173 example
= PipelineStageObjectExample()
174 with
open("pipe_stage_object_module.il", "w") as f
:
175 f
.write(rtlil
.convert(example
, ports
=[