Code Conversion
4-Bit Thermometer to Binary Encoder
Converts a 4-bit thermometer code to a 2-bit binary value with clamping for overflow.
About this block
Simulate it in the Verilog IDE
4-Bit Thermometer to Binary Encoder is a Verilog reference block in the Code Conversion catalog. Encoders, decoders, priority encoders, and binary/gray converters. Marked Easy: a small block you can read, simulate, and synthesize in one sitting.
This IP core implements a combinational logic block that converts a 4-bit thermometer code into a 2-bit binary value. It is designed for low-complexity digital systems requiring efficient code conversion. The module includes saturation clamping to handle invalid inputs (values > 3) by outputting the maximum representable 2-bit value (3), preventing overflow in downstream logic. The design is fully synthesizable, verified with Icarus Verilog, and ready for integration into larger SoC or FPGA projects.
The encoder accepts a 4-bit input (thermometer_in) where the number of high bits indicates the magnitude. It produces a 2-bit binary output (binary_out).
- Input Range: 0 to 15 (4-bit)
- Output Range: 0 to 3 (2-bit)
- Clamping Behavior: If the input represents a value greater than 3 (i.e., 4 or more bits are set), the output is clamped to
2'b11(3).
This behavior ensures that downstream 2-bit logic does not encounter undefined or overflow states.
- Choose Open in IDE. RTL Studio loads the packaged workspace for 4-Bit Thermometer to Binary Encoder.
- Read the Verilog module in the editor. The catalog line for this block: Converts a 4-bit thermometer code to a 2-bit binary value with clamping for overflow.
- Run the packaged testbench with the in-browser Verilator simulator, then open the VCD in the waveform viewer.
- 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.
Ports
| Port Name | Direction | Width | Description |
|---|---|---|---|
thermometer_in | Input | [3:0] | 4-bit thermometer encoded input |
binary_out | Output | [1:0] | 2-bit binary encoded output |
Integration
### Instantiation Example (Verilog)
### Simulation
The design includes a self-checking testbench (tb/tb_thermometer_to_binary_4b.v).
To run the simulation:
- Ensure Icarus Verilog (
iverilog) is installed. - Run the following commands in the project root:
- Check the console output for
INFO: PASSmessages. - A VCD file (
sim/dump.vcd) is generated for waveform analysis.
More in Code Conversion
- 16-Bit Priority Encoder — L1 Upgrade — L2 Upgrade
- 4-Bit BCD to Binary Converter — L2 Upgrade
- 4-Bit Binary to BCD Converter (Shift-and-Add-3)
- 8-Bit One-Hot Encoder
- 8-Bit Priority Decoder (One-Hot to Binary) — L1 Upgrade — L2 Upgrade
- 8-Bit Thermometer to Binary Encoder
- 8-Bit to 16-Bit Sign Extender
- 8-to-16 Zero Extension — L1 Upgrade
- Binary to Excess-3 4-bit — L1 Upgrade — L2 Upgrade
- Binary→Thermometer Compare-Gen (8→9)
- Binary→Thermometer LUT (4→15, Case) — L1 Upgrade — L2 Upgrade
- Gray-Code Incrementer 8-Bit
- N-Bit Gray to Binary Decoder
- N-bit Priority Encoder — L1 Upgrade — L2 Upgrade
- One-Hot→Binary Case Decode (4→2) — L1 Upgrade — L2 Upgrade
- One-Hot→Binary MSB-Priority OR Tree (8→3) — L1 Upgrade