RTL Studio Open IDE

Arithmetic & Math

8-Bit Count Trailing Zeros — L1 Upgrade — L2 Upgrade

Registered 8-bit Count Trailing Zeros unit with valid handshake and sticky saturation flag.

About this block

Simulate it in the Verilog IDE

Mediumverilogsimsyn

8-Bit Count Trailing Zeros — L1 Upgrade — L2 Upgrade is a Verilog reference block in the Arithmetic & Math catalog. Adders, multipliers, dividers, ALUs, and arithmetic datapaths. Marked Medium: more state or a wider datapath, still one workspace in the IDE.

This IP core implements a registered 8-bit Count Trailing Zeros (CTZ) unit. It calculates the number of consecutive zero bits starting from the least significant bit (LSB) of an 8-bit input.

The module features a synchronous pipeline stage, a valid handshake interface for data control, and a sticky saturation flag that indicates when the input data is zero.

The saturation flag can be manually cleared via an input signal or automatically cleared when non-zero data is processed.

  1. Choose Open in IDE. RTL Studio loads the packaged workspace for 8-Bit Count Trailing Zeros — L1 Upgrade — L2 Upgrade.
  2. Read the Verilog module in the editor. The catalog line for this block: Registered 8-bit Count Trailing Zeros unit with valid handshake and sticky saturation flag.
  3. Run the packaged testbench with the in-browser Verilator simulator, then open the VCD in the waveform viewer.
  4. Run Yosys in the same IDE for a generic netlist. Sky130 liberty mapping is a separate Pro library option.

RTL Studio is the Verilog simulator and IDE around this block: Slang lint, Verilator, VCD waveforms, and Yosys, in the browser. Simulation guide · Yosys guide.

Features

  • Input Width: 8 bits.
  • Output Width: 4 bits (covers range 0-8).
  • Pipeline: Single registered stage.
  • Reset: Active-low synchronous reset (rst_n).
  • Saturation Flag: Sticky behavior; set when input is 0, cleared when input is non-zero or via clear_sat.
  • Interface: Valid/Ready handshake (Valid only in this implementation).

Ports

Port NameDirectionWidthDescription
clkInput1Clock input
rst_nInput1Active-low synchronous reset
valid_iInput1Valid input signal; data is processed when high
data_iInput[7:0]8-bit input data
clear_satInput1Clear saturation flag (active high)
valid_oOutput1Valid output signal; high when data is valid
ctz_oOutput[3:0]Count Trailing Zeros result
sat_oOutput1Saturation flag; high if input was zero

Architecture

The module operates as a registered pipeline stage. On the rising edge of clk, if valid_i is high, the input data_i is sampled. The CTZ logic determines the index of the first set bit (LSB to MSB).

  • If data_i is zero, ctz_o is set to 8, and sat_o is set to 1.
  • If data_i is non-zero, ctz_o is set to the index of the first set bit, and sat_o is cleared to 0.
  • The valid_o signal is asserted high when valid_i is high, indicating that the output data is valid.
  • The sat_o flag is sticky. It remains high until clear_sat is asserted or until a non-zero input is processed.

Verification

The provided testbench verifies the following scenarios:

  1. Basic CTZ Values: Tests inputs that are powers of two (0x01, 0x02, 0x04, etc.).
  2. Saturation Flag: Tests input 0x00 to verify saturation flag is set.
  3. Clear Saturation: Tests manual clearing of the saturation flag.
  4. Sticky Behavior: Verifies that the saturation flag remains set until cleared or overwritten by non-zero data.
  5. Edge Cases: Tests 0xFF (ctz=0) and 0x80 (ctz=7).
  6. Reset: Verifies that all outputs are cleared on reset.

### Test Results

Test CaseDescriptionExpected ResultStatus
TC1adata=0x01ctz=0, valid=1PASS
TC1bdata=0x02ctz=1, valid=1PASS
TC1cdata=0x04ctz=2, valid=1PASS
TC1ddata=0x08ctz=3, valid=1PASS
TC1edata=0x10ctz=4, valid=1PASS
TC1fdata=0x80ctz=7, valid=1PASS
TC2adata=0x00sat=1, ctz=8, valid=1PASS
TC2bclear_satsat=0PASS
TC2cdata=0x00 againsat=1PASS
TC3data=0x01sat=0, ctz=0PASS
TC4adata=0xFFctz=0, valid=1PASS
TC4bdata=0x80ctz=7, valid=1PASS

More in Arithmetic & Math

All Arithmetic & Math blocks