- private val delay0 = (uartOversampleFactor + uartNSamples) >> 1
- private val delay1 = uartOversampleFactor
+ // Compensate for the divisor not being a multiple of the oversampling period.
+ // Let remainder k = (io.div % uartOversampleFactor).
+ // For the last k samples, extend the sampling delay by 1 cycle.
+ val remainder = io.div(uartOversample-1, 0)
+ val extend = (sample_count < remainder) // Pad head: (sample_count > ~remainder)
+ val restore = start || pulse
+ val prescaler_in = Mux(restore, io.div >> uartOversample, prescaler)
+ val prescaler_next = prescaler_in - Mux(restore && extend, UInt(0), UInt(1))