# Digital Level-Crossing (dLC) The digital Level-Crossing block (dLC) is an external peripheral intended to be connected to the DMA's HW fifo interface. It can take data from the DMA (up to 16-bit words) and compare them against pre-defined levels, replicating in the digital domain the behavior of a [Level-Crossing ADC](https://ieeexplore.ieee.org/abstract/document/9911506). The block constantly tracks the last crossed level. While the input data remains at a small distance from that level, the dlC simply discards it. If the input data differs more than a specified amount instead, the dLC outputs outputs the difference in levels, the number of skipped sampled and triggers an interrupt signaling a crossing (xing). The dLC block can be used to filter input data, reduce the output data rate from an ADC, as a wake-up system or as part of a feedback loop. An additional output can be exposed to an IO pad with the direction of the crossing to form a dir/req pair to be fed to an off-chip SNN or for data transmission. ## Operation The dLC block works as a digital filter, that literally filters out data that is "redundant", and reduces the bit-depth of the data that is not redundant by computing the difference with the last output. How much this difference should be is configurable from SW by setting the `LVL W` (level width) register. If the input data differs more than the level width in any direction a new output will be generated, formatted and added to the write fifo. If this is not the case, the sample is discarded and a count controlling the samples' difference is increased. The data output format can be configured to specify 1. the number of bits dedicated to the level difference `dlvl` 1. the format of these (2's complement or sign + absolute value) 1. the number of bits dedicated to the samples' difference `dt` It is possible that the input data crosses more levels than the maximum number that can be stored in the assigned bits. To see how the dLC manages the `dlvl` overflow see [DLVL overflow](#dlvl-overflow). It is also possible that no sample crosses the any level for an extended period of time, exceeding the maximum possible count in the bits assigned to `dt`. To see how the dLC manages the `dt` overflow see [DT overflow](#dt-overflow). The dLC logic can still be used while preserving all the input data. For this, see [bypass](#bypass). ### General case At every new sample available to the DMA, the input data (`din`) is loaded in the the `read fifo`. This is popped by the dLC while it is on the `RUN` state. The input data is right shifted `LVL W` bits, so the `16 - LVL W` most significant bits of the input data are taken. This number of bits represent the width of the levels and force the level width to be a power of two. The piece of the input data corresponding to its level gets subtracted the last crossed level to obtain the level difference `dlvl`. This difference is used to decide how the dLC should proceed. 1. If `dlvl` is zero, no level was crossed. The samples' difference count is increased by one and the sample is discarded. If the count of samples' difference overflows the number of bits assigned to it, the dLC goes to the `DT OVF` state to add an output word with no difference in levels but full difference in samples. See [DT overflow](#dt-overflow). Then operation proceeds as usual. 1. If `dlvl` is different from zero, there was a crossing, so several things need to be done: * the sign of the crossing is extracted (most significant bit of `dlvl`) * the `xing` signal needs to be raised to generate an interrupt * if the `dlvl` value does not overflow, a sample is output * if the `dlvl` value is overflowed, the dLC goes to the `DLVL OVF` state 1. In the `DLVL OVF` state the dLC generates outputs with `dt=0` and subtracts from the `dlvl` value the `dlvl` mask (maximum possible value) until the result is negative (the reminder can be represented in the assigned bits). See [DLVL overflow](#dlvl-overflow). Then operation proceeds as usual. 1. In the `bypass` mode the dLC performs the comparisons in the `RUN` state to obtain the `xing` and `dir` signals, but does not generate outputs or changes states. 1. Every time the dLC returns to the `RUN` state or after sending outputs, the samples' difference counter is returned to 1 (to signify that the next sample is no longer associated with the same timestamp).  ## Formatting Before being pushed to the write fifo, samples are formatted according to sw-configurable format. The three variables that need to be specified are: 1. Number of bits assigned to `dlvl` (including sign) 1. Number of bits assigned to `dt` 1. If the `dlvl` should be expressed in twos complement or as sign + absolute value. The three pieces of information are placed as follows: ```