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(m
, 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 with
PipeManager(m
, pipemode
=True) as pipe
:
135 with pipe
.Stage("first",
136 ispec
=[self
._loopback
, self
.obj
]) as (p
, m
):
137 p
.n
= ~self
._loopback
139 with pipe
.Stage("second", p
) as (p
, m
):
140 #p.n = ~self._loopback + 2
142 o
= ObjectProxy(m
, pipemode
=False)
144 o
.b
= p
.o
.b
+ p
.n
+ Const(5)
146 with pipe
.Stage("third", p
) as (p
, m
):
147 #p.n = ~self._loopback + 5
149 m
.d
.comb
+= localv
.eq(2)
151 o
= ObjectProxy(m
, pipemode
=False)
152 o
.b
= p
.n
+ p
.o
.b
+ p
.o
.a
155 print ("stages", pipe
.stages
)
161 if __name__
== "__main__":
162 example
= PipeModule()
163 with
open("pipe_module.il", "w") as f
:
164 f
.write(rtlil
.convert(example
, ports
=[
167 example
= PipelineStageExample()
168 with
open("pipe_stage_module.il", "w") as f
:
169 f
.write(rtlil
.convert(example
, ports
=[
172 example
= PipelineStageObjectExample()
173 with
open("pipe_stage_object_module.il", "w") as f
:
174 f
.write(rtlil
.convert(example
, ports
=[