forked from M-Labs/artiq
1
0
Fork 0

pre-dac gain/offsets: detect overflow & underflow

And output maximum / minimum DAC code when over/underflow
This commit is contained in:
occheung 2023-09-18 23:19:21 -07:00 committed by Sébastien Bourdeauducq
parent 5c64eac8d2
commit a2fbcb8bfd
1 changed files with 19 additions and 3 deletions

View File

@ -111,6 +111,9 @@ class Dac(Module):
self.gain = Signal(16)
self.offset = Signal(16)
overflow = Signal()
underflow = Signal()
###
subs = [
@ -120,12 +123,25 @@ class Dac(Module):
# Infer signed multiplication
data_raw = Signal((14, True))
data_buf = Signal(14)
data_buf = Signal(16)
self.sync.rio += [
data_raw.eq(reduce(add, [sub.data for sub in subs])),
# Extra buffer for better DSP timing
# Extra buffer for timing for the DSP
data_buf.eq(((data_raw * Cat(self.gain, ~self.gain[-1])) + (self.offset << 16))[16:]),
self.data.eq(data_buf),
If(overflow,
self.data.eq(0x1fff),
).Elif(underflow,
self.data.eq(0x2000),
).Else(
self.data.eq(data_buf),
),
]
self.comb += [
# Overflow condition
overflow.eq(~data_buf[-1] & (data_buf[-2] | data_buf[-3])),
# Underflow condition
underflow.eq(data_buf[-1] & (~data_buf[-2] | ~data_buf[-3])),
]
self.i = [ sub.i for sub in subs ]