litex/migen/corelogic/misc.py

42 lines
887 B
Python
Raw Normal View History

2011-12-22 09:46:19 -05:00
from migen.fhdl.structure import *
from migen.fhdl.structure import _Operator
def multimux(sel, inputs, output):
n = len(inputs)
i = 0
statements = []
2011-12-22 09:46:19 -05:00
for osig in output:
choices = [x[i] for x in inputs]
cases = [[Constant(j, sel.bv), osig.eq(choices[j])] for j in range(n)]
cases[n-1][0] = Default()
statements.append(Case(sel, *cases))
2011-12-22 09:46:19 -05:00
i += 1
return statements
2011-12-22 09:46:19 -05:00
def optree(op, operands, lb=None, ub=None, default=None):
if lb is None:
lb = 0
if ub is None:
ub = len(operands)
l = ub - lb
if l == 0:
if default is None:
raise AttributeError
else:
return default
elif l == 1:
return operands[lb]
else:
s = lb + l//2
return _Operator(op,
[optree(op, operands, lb, s, default),
optree(op, operands, s, ub, default)])
2012-02-11 05:52:15 -05:00
def split(v, *counts):
r = []
offset = 0
for n in counts:
r.append(v[offset:offset+n])
offset += n
return tuple(r)