both_ones = Signal(self.width)
different = Signal(self.width)
- # build both_ones and different such that:
- # * if both_ones is set, then both_ones[i] and different[i] are both
- # ones in order to set the carry bit out.
- # * if different is set, then both_ones[i] and different[i] are both
- # zeros in order to clear the carry bit out.
- # * otherwise exactly one of both_ones[i] and different[i] are set and
+ # build `both_ones` and `different` such that:
+ # for every bit index `i`:
+ # * if `both_ones[i]` is set, then both addends bits at index `i` are
+ # set in order to set the carry bit out, since `cin + 1 + 1` always
+ # has a carry out.
+ # * if `different[i]` is set, then both addends bits at index `i` are
+ # zeros in order to clear the carry bit out, since `cin + 0 + 0`
+ # never has a carry out.
+ # * otherwise exactly one of the addends bits at index `i` is set and
# the other is clear in order to propagate the carry bit from
- # less significant bits.
- # * different is zero when both_ones is set, so we don't need to
- # OR-in both_ones
+ # less significant bits, since `cin + 1 + 0` has a carry out that is
+ # equal to `cin`.
# `both_ones` is set if both have no leading zeros so far
m.d.comb += both_ones.eq(self.a & self.b)
# now [ab]use add: the last bit [carry-out] is the result
csum = Signal(self.width + 1)
- carry_in = 1 # both have no leading zeros so far, so set carry
+ carry_in = 1 # both have no leading zeros so far, so set carry in
m.d.comb += csum.eq(both_ones + (~different) + carry_in)
m.d.comb += self.out.eq(csum[self.width]) # out is carry-out