Case study · OpenHW on ALINX · Episode 12

CVE2 as a digital scope: a 1 kHz tone in, a 32-tap FIR and a 1,024-point FFT out

The previous eleven episodes wired CVE2 to memory, Ethernet and a camera. This one turns it inward: let the core generate the signal, sample it, do the arithmetic, and report the answer over a single UART line. A DDS builds a 1 kHz sine in fabric, 1,024 samples are captured at 100 kHz, a 32-tap Q14 FIR and a 1,024-point block-floating-point FFT run on the samples, and the firmware recomputes the same maths on the same data as a cross-check. It is a closed loop inside the board — no signal generator, no scope, no monitor on the desk.

Verified on hardware · 2026-11-23 CVE2 · 50 MHz · RV32IM 32-tap FIR · Q14 1,024-point FFT · BFP 12 / 12
0
max |hardware FIR − software FIR| over all 32 points
bin 10
FFT peak position for 1 kHz (1,024 × 1,000 / 100,000)
1 kHz @ 100 kHz
DDS tone and sample rate, 500-divider from 50 MHz
7,342 clk
1,024-point FFT in fabric, 7,353 counted end to end
+2.308 / +0.003 ns
WNS / WHS, 0 failing endpoints of 44,572
14,263 LUT · 29 DSP
68.57% LUT, 48.88% FF, 12 BRAM
1,626,731 B
final bitstream, sha256 5728ca9d…
Scope of the word “match”, stated up front. For the FIR it means: the fixed-point multiply-accumulate in fabric, and the firmware's full-precision sum with a single >>14, agree point by point (maxdiff=0). For the FFT it covers two things only — shape (cosine similarity 0.9999 across 64 bins) and peak position (bin 10 on both sides). Absolute magnitude still carries a scaling factor (hardware / software ≈ 0.46–0.52), because the hardware path uses block-floating-point scaling with an approximate magnitude estimate while the firmware sums a fixed-point cosine table. No unified calibration was done, and that is written here instead of being hidden.
EP12 measured plots: first 32 captured samples, hardware vs software FIR, 64-bin spectrum
All three panels come straight from this run's UART log, no data was invented. Top: the first 32 of 1,024 captured samples (the sine period is exactly 100 samples, so this window shows the rise to the peak). Middle: hardware FIR against the firmware result — the curves coincide. Bottom: 64 spectrum bins, each curve normalised to its own peak, both peaking at bin 10.

On-board results: six criteria, all passing

The chain was checked in the order “register window alive → capture actually runs → FIR numbers right → spectrum lands where it must”. Every figure below comes from one power-on UART capture.

CheckRequirementMeasured
1 · Register windowread 0x4006_0000, expect a known IDID=4609 (0x1201)
2 · DDS test tone1 kHz at 100 kHz samplingfs=100000 Hz f0=1000 Hz; W0..W31 follow a 100-sample sine, peak 16,376
3 · Capture1,024 points, CAP_DONE raisedcapture completes; the same samples feed both the hardware chain and the recompute
4 · Hardware FIR32 taps, equal to software point by pointFIR lat=5 clk maxdiff=0 @0 · all 32 points equal
5 · Hardware FFT1,024-point BFP, peak in the 1 kHz binhw_cycles=7342 exp=8; HWP peak bin=10 mag=7619072 (theory bin 10, theory amplitude 8.39 M)
6 · Exceptionsno trapTRAP=0 done

The waveform peaks at 16,376 rather than the full 16,383, a 98 LSB (0.6%) deviation caused by the DDS keeping only 12 of its 32 phase bits. It is the phase accumulator's word length, not a sampling or FIR error.

The UART evidence

The full 2,379 B log is in the file list (logs/ep12_serial_verified.log). Below, only the head, tail and a few of the 32 FIR rows survive.

EP12 digital scope / FIR+FFT bench
ID=4609
fs=100000 Hz  f0=1000 Hz
FIR 0 0 0 0
FIR 1 0 0 0
FIR 2 1 1 0
...
FIR 15 -87 -87 0
FIR 16 124 124 0
...
FIR 30 12240 12240 0
FIR 31 12895 12895 0
FIR lat=5 clk  maxdiff=0 @0
W 0 0
W 1 1008
...
W 25 16376
...
W 31 15208
FFT done, cycles=7353 hw_cycles=7342 exp=8
S k swmag hwmag
S 9 2879072 1431040
S 10 14933012 7619072
S 11 4832843 2470400
...
S 63 97766 50688
HWP peak bin=10 mag=7619072
TRAP=0 done

A FIR row is FIR index hardware software difference — the last column is zero for all 32 rows. W rows are captured samples; W 25 = 16376 is the peak and the waveform returns to the origin 100 samples later, matching 1 kHz. S rows are S bin software-magnitude hardware-magnitude, largest around bin 10.

The real difficulty: the FIR readback never changed

The first stage on hardware was uglier: 21 bytes of garbage on the UART and a CPU stuck waiting for capture. Once those were fixed and the log became readable, the FIR columns stayed byte-for-byte identical across four different RTL builds, with maxdiff pinned at 28,037. Changing RTL without changing the output means the change never touched the path that produces the output.

The root cause was one write enable:

// wrong: fir_wr is 10 bits wide, so "fir_wr < 1024" is always true
wire fir_we = fir_v && (fir_wr < N);

// right: gate on a busy flag, stop after N points
wire fir_we = fir_v && fir_busy;

fir_wr holds 0–1023, so fir_wr < 1024 never goes false. After capture finished the pointer kept walking, wrapped, and the buffer was overwritten round after round. The firmware was therefore not reading the first 32 transient outputs at all — it was reading one steady-state round from much later. That explains everything: why the numbers looked like a sine, why they were in a plausible range, and why changing the shift chain or the memory implementation changed nothing.

The expensive lesson: first ask whether the thing you edited sits on the data path, then argue about numeric precision. The first three edits (write port from fir_sum[35:4] to fir_out, signed shift, xpm to a plain array) were all correct, but the output was never being read, so none of them showed.

StageChangemaxdiffWhat it showed
startwrite port on fir_sum[35:4], logical shift28,037wrong scale, values looked like a random sine
write enablefir_busy gate, stop after 1,024 points1,071real first 32 points at last, but shifted by one sample
pipeline alignsoftware index i-j → i-1-j16hardware carries one extra pipeline stage; confirmed by a Python offset sweep
truncation alignper-tap >>14 → full-precision sum, one shift0all 32 points equal

Twelve defects, each reproduced before it was fixed

  1. Capture never started. cap_arm was level-sensitive (clearing the write pointer and busy flag inside else if (cap_arm)) while the firmware holds the ARM bit high, so the capture sat at its start state forever and the CPU spun on CAP_DONE. Replaced by a rising-edge pulse.
  2. Every string read back as 0x13. The top-level initial loaded only the instruction RAM; the data RAM was filled with 32'h13. The linker script puts all string constants in .rodata inside that same RAM, so each puts_() emitted one 0x13 byte and only numbers survived. Fixed by loading the same hex into dmem as well.
  3. FIR memory port off by 1,024×. The write port was fed by fir_sum[35:4], four bits further right than the Q14 convention.
  4. Negative right-shift exploded. fir_sum >> 14 is a logical shift in SystemVerilog, so negatives jumped to the 225 range; changed to $signed(fir_sum) >>> 14. Confirmed first in an isolated xsim run of the FIR chain on a 100-point sine: Y 6 33554425 before, Y 6 -7 after.
  5. NCO constant was 2 kHz. 0x051EB852 is 0.02×232 while the comment said 1 kHz, so the FFT peak sat in bin 20. After 0x028F5C29 it moved to bin 10.
  6. Software DFT scale off by 16×. The cosine table was shifted by >>10 instead of the Q14 >>14.
  7. BFP exponent not compensated. xfft is configured for block floating point and the outputs are scaled down by 2exp. A new CSR (0x4006_0034, address nibble 4'hD) returns the latched exponent, the firmware prints exp= and multiplies back with <<bexp.
  8. FIR history not cleared. The 32-stage delay line kept samples from the previous run; it is now cleared on arm.
  9. Write enable always true (the main root cause, see above).
  10. Hardware is one sample ahead of the textbook formula. The pipeline gives y[n]=Σc[j]x[n-1-j]; the software index follows with i-1-j.
  11. Software FIR truncated per tap. Each product was shifted separately, accumulating up to 16 LSB of truncation across 32 taps; now accumulated at full precision and shifted once.
  12. Hold at −0.032 ns. phys_opt_design -aggressive_hold_fix after route_design brings it to +0.003 ns.

What the on-chip chain looks like

At 50 MHz the FSDIV=499 divider produces the 100 kHz sampling tick. The DDS keeps a 32-bit phase accumulator and uses only the top 12 bits to index a sine table, which is where the 0.6% phase-truncation error comes from. Each captured sample goes two ways: into the 1,024-point capture buffer (read by software) and into the 32-stage FIR delay line.

The FIR uses the symmetric structure — xr[k]+xr[31-k] paired first to halve the multipliers, then 16 Q14 coefficients, a four-level adder tree and one final >>14 — and writes its own 1,024-point buffer. Latency is 5 clocks, and the firmware reads that value back from 0x4006_001C instead of hard-coding it.

For the spectrum the capture buffer feeds xfft (radix_2_burst_io, nonrealtime, natural-order output, block-floating-point scaling). When the core raises done, the firmware reads each magnitude and latches the exponent. 1,024 points take 7,342 clocks.

Reproducing it

# build (Vivado 2025.2 on the build host)
cd /media/alinx/nvme4t/fpga-love/pi_ar035_f1_1_cve2_ep12
vivado -mode batch -source build_ep12.tcl -log build_ep12.log

# program
vivado -mode batch -source program_ep12.tcl

# firmware: main.c is packed into BRAM through $readmemh, so edit means rebuild
cd sw && make -s

# serial capture: start cat before programming, or the first screen is lost
stty -F /dev/ttyUSB1 115200 cs8 -cstopb -parenb raw -echo
nohup timeout 70 cat /dev/ttyUSB1 > /tmp/ep12.log 2>/dev/null < /dev/null &
vivado -mode batch -source program_ep12.tcl
sleep 6 && cat /tmp/ep12.log

Two pitfalls worth repeating: the port only works at 115200 (230400 and 57600 both return zero bytes), and do not finish with pkill -f "cat /dev/ttyUSB1" — it takes your own SSH session down with it. The firmware hex must be loaded into both imem and dmem; instruction memory alone makes every string read 0x13.

Related

Evidence files

26 files, about 2.0 MB: the final bitstream, routed timing and utilisation reports, build / programming / serial logs (including the two intermediate captures that show maxdiff going 1,071 → 16 → 0), RTL, XDC, Tcl, firmware sources with ELF / HEX / disassembly, the three-panel measurement figure, structured metrics and the defect write-up.

Subscribe to fpga.love

Daily Briefing · Weekly Digest · Monthly Deep Dive