from ieee754.part_mul_add.adder import PartitionedAdder
from ieee754.part_cmp.eq_gt_ge import PartitionedEqGtGe
+from ieee754.part_bits.xor import PartitionedXOR
+from ieee754.part_bits.bool import PartitionedBool
+from ieee754.part_bits.all import PartitionedAll
from ieee754.part_shift.part_shift_dynamic import PartitionedDynamicShift
from ieee754.part_shift.part_shift_scalar import PartitionedScalarShift
-from ieee754.part_mul_add.partpoints import make_partition
+from ieee754.part_mul_add.partpoints import make_partition2, PartitionPoints
+from ieee754.part_mux.part_mux import PMux
+from ieee754.part_ass.passign import PAssign
+from ieee754.part_cat.pcat import PCat
from operator import or_, xor, and_, not_
from nmigen import (Signal, Const)
+from nmigen.hdl.ast import UserValue
def getsig(op1):
def applyop(op1, op2, op):
- return op(getsig(op1), getsig(op2))
-
-
-class PartitionedSignal:
- def __init__(self, mask, *args, **kwargs):
+ if isinstance(op1, PartitionedSignal):
+ result = PartitionedSignal.like(op1)
+ else:
+ result = PartitionedSignal.like(op2)
+ result.m.d.comb += result.sig.eq(op(getsig(op1), getsig(op2)))
+ return result
+
+global modnames
+modnames = {}
+# for sub-modules to be created on-demand. Mux is done slightly
+# differently (has its own global)
+for name in ['add', 'eq', 'gt', 'ge', 'ls', 'xor', 'bool', 'all']:
+ modnames[name] = 0
+
+
+class PartitionedSignal(UserValue):
+ def __init__(self, mask, *args, src_loc_at=0, **kwargs):
+ super().__init__(src_loc_at=src_loc_at)
self.sig = Signal(*args, **kwargs)
- width = self.sig.shape()[0] # get signal width
+ width = len(self.sig) # get signal width
# create partition points
- self.partpoints = make_partition(mask, width)
- self.modnames = {}
- for name in ['add', 'eq', 'gt', 'ge', 'ls']:
- self.modnames[name] = 0
+ if isinstance(mask, PartitionPoints):
+ self.partpoints = mask
+ else:
+ self.partpoints = make_partition2(mask, width)
+
def set_module(self, m):
self.m = m
def get_modname(self, category):
- self.modnames[category] += 1
- return "%s_%d" % (category, self.modnames[category])
+ modnames[category] += 1
+ return "%s_%d" % (category, modnames[category])
+
+ @staticmethod
+ def like(other, *args, **kwargs):
+ """Builds a new PartitionedSignal with the same PartitionPoints and
+ Signal properties as the other"""
+ result = PartitionedSignal(PartitionPoints(other.partpoints))
+ result.sig = Signal.like(other.sig, *args, **kwargs)
+ result.m = other.m
+ return result
- def eq(self, val):
- return self.sig.eq(getsig(val))
+ def __len__(self):
+ return len(self.sig)
+ def shape(self):
+ return self.sig.shape()
+ def lower(self):
+ return self.sig
+ # now using __Assign__
+ #def eq(self, val):
+ # return self.sig.eq(getsig(val))
+
+ # nmigen-redirected constructs (Mux, Cat, Switch, Assign)
+
+ def __Mux__(self, val1, val2):
+ # print ("partsig mux", self, val1, val2)
+ assert len(val1) == len(val2), \
+ "PartitionedSignal width sources must be the same " \
+ "val1 == %d, val2 == %d" % (len(val1), len(val2))
+ return PMux(self.m, self.partpoints, self, val1, val2)
+
+ def __Assign__(self, val, *, src_loc_at=0):
+ # print ("partsig ass", self, val)
+ return PAssign(self.m, self, val, self.partpoints)
+
+ def __Cat__(self, *args, src_loc_at=0):
+ args = [self] + list(args)
+ for sig in args:
+ assert isinstance(sig, PartitionedSignal), \
+ "All PartitionedSignal.__Cat__ arguments must be " \
+ "a PartitionedSignal. %s is not." % repr(sig)
+ return PCat(self.m, args, self.partpoints)
# unary ops that do not require partitioning
def __invert__(self):
- return ~self.sig
+ result = PartitionedSignal.like(self)
+ self.m.d.comb += result.sig.eq(~self.sig)
+ return result
# unary ops that require partitioning
def __neg__(self):
- z = Const(0, self.sig.shape())
- result, _ = self.add_op(self, ~0, carry=z) # TODO, subop
+ z = Const(0, len(self.sig))
+ result, _ = self.sub_op(z, self)
return result
# binary ops that don't require partitioning
# TODO: detect if the 2nd operand is a Const, a Signal or a
# PartitionedSignal. if it's a Const or a Signal, a global shift
# can occur. if it's a PartitionedSignal, that's much more interesting.
- def ls_op(self, op1, op2, carry):
+ def ls_op(self, op1, op2, carry, shr_flag=0):
op1 = getsig(op1)
if isinstance(op2, Const) or isinstance(op2, Signal):
scalar = True
- shape = op1.shape()
- pa = PartitionedScalarShift(shape[0], self.partpoints)
+ pa = PartitionedScalarShift(len(op1), self.partpoints)
else:
scalar = False
op2 = getsig(op2)
- shape = op1.shape()
- pa = PartitionedDynamicShift(shape[0], self.partpoints)
+ pa = PartitionedDynamicShift(len(op1), self.partpoints)
+ # else:
+ # TODO: case where the *shifter* is a PartitionedSignal but
+ # the thing *being* Shifted is a scalar (Signal, expression)
+ # https://bugs.libre-soc.org/show_bug.cgi?id=718
setattr(self.m.submodules, self.get_modname('ls'), pa)
comb = self.m.d.comb
if scalar:
comb += pa.data.eq(op1)
comb += pa.shifter.eq(op2)
+ comb += pa.shift_right.eq(shr_flag)
else:
comb += pa.a.eq(op1)
comb += pa.b.eq(op2)
- # XXX TODO: carry-in, carry-out
+ comb += pa.shift_right.eq(shr_flag)
+ # XXX TODO: carry-in, carry-out (for arithmetic shift)
#comb += pa.carry_in.eq(carry)
return (pa.output, 0)
def __lshift__(self, other):
- result, _ = self.ls_op(self, other, carry=0)
+ z = Const(0, len(self.partpoints)+1)
+ result, _ = self.ls_op(self, other, carry=z) # TODO, carry
return result
def __rlshift__(self, other):
+ # https://bugs.libre-soc.org/show_bug.cgi?id=718
raise NotImplementedError
return Operator("<<", [other, self])
def __rshift__(self, other):
- raise NotImplementedError
- return Operator(">>", [self, other])
+ z = Const(0, len(self.partpoints)+1)
+ result, _ = self.ls_op(self, other, carry=z, shr_flag=1) # TODO, carry
+ return result
def __rrshift__(self, other):
+ # https://bugs.libre-soc.org/show_bug.cgi?id=718
raise NotImplementedError
return Operator(">>", [other, self])
def add_op(self, op1, op2, carry):
op1 = getsig(op1)
op2 = getsig(op2)
- shape = op1.shape()
- pa = PartitionedAdder(shape[0], self.partpoints)
+ pa = PartitionedAdder(len(op1), self.partpoints)
setattr(self.m.submodules, self.get_modname('add'), pa)
comb = self.m.d.comb
comb += pa.a.eq(op1)
comb += pa.b.eq(op2)
comb += pa.carry_in.eq(carry)
- return (pa.output, pa.carry_out)
+ result = PartitionedSignal.like(self)
+ comb += result.sig.eq(pa.output)
+ return result, pa.carry_out
def sub_op(self, op1, op2, carry=~0):
op1 = getsig(op1)
op2 = getsig(op2)
- shape = op1.shape()
- pa = PartitionedAdder(shape[0], self.partpoints)
+ pa = PartitionedAdder(len(op1), self.partpoints)
setattr(self.m.submodules, self.get_modname('add'), pa)
comb = self.m.d.comb
comb += pa.a.eq(op1)
comb += pa.b.eq(~op2)
comb += pa.carry_in.eq(carry)
- return (pa.output, pa.carry_out)
+ result = PartitionedSignal.like(self)
+ comb += result.sig.eq(pa.output)
+ return result, pa.carry_out
def __add__(self, other):
result, _ = self.add_op(self, other, carry=0)
return result
def __radd__(self, other):
+ # https://bugs.libre-soc.org/show_bug.cgi?id=718
result, _ = self.add_op(other, self)
return result
return result
def __rsub__(self, other):
+ # https://bugs.libre-soc.org/show_bug.cgi?id=718
result, _ = self.sub_op(other, self)
return result
def __mul__(self, other):
+ raise NotImplementedError # too complicated at the moment
return Operator("*", [self, other])
def __rmul__(self, other):
+ raise NotImplementedError # too complicated at the moment
return Operator("*", [other, self])
def __check_divisor(self):
return pa.output
def __eq__(self, other):
- width = self.sig.shape()[0]
+ width = len(self.sig)
return self._compare(width, self, other, "eq", PartitionedEqGtGe.EQ)
def __ne__(self, other):
- width = self.sig.shape()[0]
+ width = len(self.sig)
eq = self._compare(width, self, other, "eq", PartitionedEqGtGe.EQ)
ne = Signal(eq.width)
self.m.d.comb += ne.eq(~eq)
return ne
def __gt__(self, other):
- width = self.sig.shape()[0]
+ width = len(self.sig)
return self._compare(width, self, other, "gt", PartitionedEqGtGe.GT)
def __lt__(self, other):
- width = self.sig.shape()[0]
+ width = len(self.sig)
+ # swap operands, use gt to do lt
return self._compare(width, other, self, "gt", PartitionedEqGtGe.GT)
def __ge__(self, other):
- width = self.sig.shape()[0]
+ width = len(self.sig)
return self._compare(width, self, other, "ge", PartitionedEqGtGe.GE)
def __le__(self, other):
- width = self.sig.shape()[0]
+ width = len(self.sig)
+ # swap operands, use ge to do le
return self._compare(width, other, self, "ge", PartitionedEqGtGe.GE)
# useful operators
Value, out
``1`` if any bits are set, ``0`` otherwise.
"""
- raise NotImplementedError
- return Operator("b", [self])
+ width = len(self.sig)
+ pa = PartitionedBool(width, self.partpoints)
+ setattr(self.m.submodules, self.get_modname("bool"), pa)
+ self.m.d.comb += pa.a.eq(self.sig)
+ return pa.output
def any(self):
"""Check if any bits are ``1``.
Value, out
``1`` if any bits are set, ``0`` otherwise.
"""
- raise NotImplementedError
+ return self != Const(0) # leverage the __ne__ operator here
return Operator("r|", [self])
def all(self):
Value, out
``1`` if all bits are set, ``0`` otherwise.
"""
- raise NotImplementedError
- return Operator("r&", [self])
+ # something wrong with PartitionedAll, but self == Const(-1)"
+ # XXX https://bugs.libre-soc.org/show_bug.cgi?id=176#c17
+ #width = len(self.sig)
+ #pa = PartitionedAll(width, self.partpoints)
+ #setattr(self.m.submodules, self.get_modname("all"), pa)
+ #self.m.d.comb += pa.a.eq(self.sig)
+ #return pa.output
+ return self == Const(-1) # leverage the __eq__ operator here
def xor(self):
"""Compute pairwise exclusive-or of every bit.
``1`` if an odd number of bits are set, ``0`` if an
even number of bits are set.
"""
- # XXXX TODO: return partition-mask-sized set of bits
- raise NotImplementedError
- return Operator("r^", [self])
+ width = len(self.sig)
+ pa = PartitionedXOR(width, self.partpoints)
+ setattr(self.m.submodules, self.get_modname("xor"), pa)
+ self.m.d.comb += pa.a.eq(self.sig)
+ return pa.output
def implies(premise, conclusion):
"""Implication.