[myhdl-list] a few questions relating to recursive designs
Brought to you by:
jandecaluwe
|
From: Geoffrey B. <geo...@gm...> - 2009-05-13 20:32:03
|
I thought I'd plunge in with a recursive structure (an N-way mux) which
follows. I'm embarrassed
how long it took me to figure this out. Now here are a few questions.
1) I wanted to use a selector both as a bit vector and an integer.
Naturally
I made a signal from an intbv, but found the hard way that the obvious
thing, picking off bits by slicing, doesn't give you what is needed (in
this
context) -- another signal which has a subset of the bits from the
original
I resorted to the (ugly) approach of passing an integer through the
hierarchy
so I could do the bit selection at the leaves. Another ugly solution
would
be to define the selector as a list of signals and slice this up. The
problem
with this approach is that at the top level of the design one has to
bridge
from the single intbv to list of signals.
Any thoughts on a more elegant approach ?
2) It appears that to work with lists of signals, one must ensure that at
the
instance level the list has been unpacked into individual signals and at
the top level these lists are built from individual signals. Is this
correct ?
Geoffrey
--------------------------------------------------
def Mux0(sw, z, A0, A1, sel):
""" Generate A Two-way Multiplexor
sw -- sel bit to use at this level (...,3,2,1,0)
z -- data output
A1, A0 -- data inputs
sel -- (multibit) selector
"""
@always_comb
def muxLogic():
if sel[sw]:
z.next = A1
else:
z.next = A0
return muxLogic
def Mux(sw, z, A, sel):
""" Recursively Generate A Multiplexor Tree
sw -- sel bit to use at this level (...,3,2,1,0)
should match tree depth at top level
z -- output data
A -- array of input data
sel-- selector for array
"""
assert len(A) >= 2
if len(A) == 2:
return Mux0(sw, z, A[0], A[1], sel)
else:
# signals for two halves
Mid = [Signal(intbv(0)[len(z):]) for i in range(2)]
# recursively generate muxes for two data halves
l = len(A)/2
m1 = Mux(sw-1, Mid[1], A[l:], sel) # top
m0 = Mux(sw-1, Mid[0], A[:l], sel) # bottom
# generate top level mux
top = Mux(sw, z, Mid, sel)
return m0, m1, top
|