forked from M-Labs/artiq
1
0
Fork 0

dsp/tools: clean up SatAddMixin logic

This commit is contained in:
Robert Jördens 2017-06-21 12:37:11 +02:00
parent f369cb97f7
commit b6569df02f
1 changed files with 9 additions and 6 deletions

View File

@ -37,9 +37,8 @@ class SatAddMixin:
carry = log2_int(len(a), need_pow2=False) carry = log2_int(len(a), need_pow2=False)
full = Signal((length + carry, True)) full = Signal((length + carry, True))
limited = Signal((length, True)) limited = Signal((length, True))
clip = Signal(2) if clipped is None:
if clipped is not None: clipped = Signal(2)
clipped.eq(clip)
self.comb += [ self.comb += [
full.eq(reduce(add, a)), full.eq(reduce(add, a)),
] ]
@ -47,15 +46,19 @@ class SatAddMixin:
self.comb += [ self.comb += [
If(full[-1-carry:] == Replicate(full[-1], carry + 1), If(full[-1-carry:] == Replicate(full[-1], carry + 1),
limited.eq(full), limited.eq(full),
clip.eq(0), clipped.eq(0),
).Else( ).Else(
limited.eq(Cat(Replicate(~full[-1], length - 1), full[-1])), limited.eq(Cat(Replicate(~full[-1], length - 1), full[-1])),
clip.eq(Cat(full[-1], ~full[-1])), clipped.eq(Cat(full[-1], ~full[-1])),
) )
] ]
else: else:
self.comb += [ self.comb += [
clip.eq(Cat(full < limits[0], full > limits[1])), clip.eq(Cat(full < limits[0], full > limits[1])),
limited.eq(Array([full, limits[0], limits[1], 0])[clip]), Case(clip, {
0b01, limited.eq(limits[0]),
0b10, limited.eq(limits[1]),
"default": limited.eq(full),
})
] ]
return limited return limited