OnesComplementAdder constructor
OnesComplementAdder constructor with an adder functor adderGen
.
- A subtractor is created if
subtract
is set to true. Alternatively, ifsubtract
configuration is false, and a Lgic control signalsubtractIn
is provided, then subtraction can be dynamically selected. Otherwise an adder is constructed. - If Logic
endAroundCarry
is provided, then the end-around carry is not performed and is provided as value onendAroundCarry
. IfendAroundCarry
is null, extra hardware takes care of adding the end-around carry to sum. carryIn
allows for another adder to chain into this one.chainable
tells this adder to not store theendAroundCarry
in the sign bit as well, but to zero that to allow adders to be chained such as for use in the CarrySelectCompoundAdder.
Implementation
OnesComplementAdder(super.a, super.b,
{Adder Function(Logic, Logic, {Logic? carryIn}) adderGen =
ParallelPrefixAdder.new,
Logic? subtractIn,
Logic? endAroundCarry,
super.carryIn,
bool subtract = false,
bool chainable = false,
String? definitionName,
super.name = 'ones_complement_adder'})
: super(
definitionName:
definitionName ?? 'OnesComplementAdder_W${a.width}') {
if (endAroundCarry != null) {
addOutput('endAroundCarry');
endAroundCarry <= this.endAroundCarry!;
}
if ((subtractIn != null) & subtract) {
throw RohdHclException(
"either provide a Logic signal 'subtractIn' for runtime "
" configuration, or a boolean parameter 'subtract' for "
'generation time configuration, but not both.');
}
this.subtractIn =
(subtractIn != null) ? addInput('subtractIn', subtractIn) : null;
_sign = addOutput('sign');
final doSubtract =
(this.subtractIn ?? (subtract ? Const(subtract) : Const(0)))
.named('dosubtract', naming: Naming.mergeable);
final adderSum =
adderGen(a, mux(doSubtract, ~b, b), carryIn: carryIn ?? Const(0))
.sum
.named('adderSum', naming: Naming.mergeable);
if (this.endAroundCarry != null) {
this.endAroundCarry! <= adderSum[-1];
}
final endAround = adderSum[-1].named('endaround');
final magnitude = adderSum.slice(a.width - 1, 0).named('magnitude');
final Logic magnitudep1;
if (this.endAroundCarry == null) {
final incrementer = ParallelPrefixIncr(magnitude);
magnitudep1 = incrementer.out.named('magnitude_plus1');
} else {
magnitudep1 = Const(0);
}
sum <=
mux(
doSubtract,
[
if (chainable) endAround else Const(0),
mux(
[if (chainable) Const(0) else endAround].first,
[if (this.endAroundCarry != null) magnitude else magnitudep1]
.first,
~magnitude)
].swizzle(),
adderSum);
_sign <= mux(doSubtract, ~endAround, Const(0));
}