dfa2a3ef42eeb7744db5b6a3aaa7c8256543ffdf
1 """ Example 5: Making use of PyRTL and Introspection. """
3 from copy
import deepcopy
5 from migen
.fhdl
import verilog
8 # The following example shows how pyrtl can be used to make some interesting
9 # hardware structures using python introspection. In particular, this example
10 # makes a N-stage pipeline structure. Any specific pipeline is then a derived
11 # class of SimplePipeline where methods with names starting with "stage" are
12 # stages, and new members with names not starting with "_" are to be registered
15 class SimplePipeline(object):
16 """ Pipeline builder with auto generation of pipeline registers. """
18 def __init__(self
, pipe
):
20 self
._pipeline
_register
_map
= {}
21 self
._current
_stage
_num
= 0
24 stage_list
= [method
for method
in dir(self
) if method
.startswith('stage')]
25 for stage
in sorted(stage_list
):
26 stage_method
= getattr(self
, stage
)
28 self
._current
_stage
_num
+= 1
30 def __getattr__(self
, name
):
32 return self
._pipeline
_register
_map
[self
._current
_stage
_num
][name
]
35 'error, no pipeline register "%s" defined for stage %d'
36 % (name
, self
._current
_stage
_num
))
38 def __setattr__(self
, name
, value
):
39 if name
.startswith('_'):
40 # do not do anything tricky with variables starting with '_'
41 object.__setattr
__(self
, name
, value
)
43 next_stage
= self
._current
_stage
_num
+ 1
44 pipereg_id
= str(self
._current
_stage
_num
) + 'to' + str(next_stage
)
45 rname
= 'pipereg_' + pipereg_id
+ '_' + name
46 #new_pipereg = pyrtl.Register(bitwidth=len(value), name=rname)
47 new_pipereg
= Signal(len(value
), name_override
=rname
)
48 if next_stage
not in self
._pipeline
_register
_map
:
49 self
._pipeline
_register
_map
[next_stage
] = {}
50 self
._pipeline
_register
_map
[next_stage
][name
] = new_pipereg
51 self
._pipe
.sync
+= new_pipereg
.eq(value
)
54 class SimplePipelineExample(SimplePipeline
):
55 """ A very simple pipeline to show how registers are inferred. """
57 def __init__(self
, pipe
):
58 super(SimplePipelineExample
, self
).__init
__(pipe
)
59 self
._loopback
= Signal()
64 self
.n
= ~self
._loopback
76 self
._pipe
.sync
+= self
._loopback
.eq(self
.n
)
78 class PipeModule(Module
):
82 if __name__
== "__main__":
83 example
= PipeModule()
84 pipe
= SimplePipelineExample(example
)
85 print(verilog
.convert(example
,