Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • RTL Foundations36
  • Combinational Design25
    • Combinational Timing Overview
    • Muxes, Decoders and Encoders
      • Mux Theory and Design Patterns
      • A Parameterized Mux
      • Binary Decoder and One-Hot Enables
      • Priority Encoder
    • Arithmetic Blocks
    • Comparators and Shifters
    • Tri-State and Shared Buses
    • Latch Inference
    • Arithmetic Datapaths
  • Sequential Design27
  • FSM & Control27
  • Timing & Clocking39
  • Reset & CDC34
  • Memory & Buses57
  • Advanced RTL28
  • Senior & Interview76
  • Glossary and Notation
Loading lesson

‹Muxes, Decoders and Encoders
  1. Topics
  2. RTL Design
  3. Combinational Design
  4. Muxes, Decoders and Encoders
  5. Binary Decoder and One-Hot Enables

Part 2 · Combinational · Intermediate

Binary Decoder and One-Hot Enables

Decode addresses into one-hot enables that wake exactly one register.

On this page

    Why this matters

    A register-map write must wake exactly one register. If two enables are ever high together, you get a lost write or bus contention, and $onehot0 alone will not catch an unknown bit.

    After this lesson you can: write a decoder with enable for a register map, state the one-hot invariant, and check it with $onehot0 and !$isunknown.

    From code to one line

    A decoder with a k-bit input produces 2ᵏ outputs of which exactly one is high. With an enable input, all outputs are low when the enable is low, which is how a decoder gates a write strobe.

    sel[1:0]

    en

    y[3:0]

    00

    1

    0001

    01

    1

    0010

    10

    1

    0100

    11

    1

    1000

    xx

    0

    0000

    Waking one line at a time

    Watch the bottom row, y. Exactly one bit is ever high.

    The diagram shows y in hex, so the four one-hot values appear as 1, 2, 4 and 8.

    • Slot 1: en is 0, so y is 0000 and no line wakes.

    • Slot 2: en is 1 and sel is 0, so y is 0001.

    • Slot 3: sel is 1, so y is 0010.

    • Slot 4: sel is 2, so y is 0100.

    • Slot 5: sel is 3, so y is 1000.

    Address decode for a register map

    Four registers share one write strobe. A write to address 2 must wake register 2 and no other.

    A bus write to address addr should update exactly one register. The decoder turns the address into per-register enables; each register's clock-enable or write-enable comes from one output line.

    reg_write_decode.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    module reg_write_decode (
      input  logic       wr,
      input  logic [1:0] addr,
      output logic       we_ctrl, we_status, we_data, we_irq
    );
      logic [3:0] we;
      always_comb begin
        we = '0;
        if (wr) we[addr] = 1'b1;
      end
      assign {we_irq, we_data, we_status, we_ctrl} = we;
    endmodule

    Common pitfall

    Decoding with a long chain of equality comparisons, one per register (wr && addr == 2'd0, wr && addr == 2'd1, ...), is easy to read but easy to get wrong when the map changes. Use a single decoder or a case with a default, and keep the register map in one place.

    One-hot enables carry a strong invariant.

    • At each write-sampling edge, every enable must be known and at most one bit may be set. Check both: assert property (@(posedge clk) !$isunknown(we) && $onehot0(we)); during normal operation.

    • One-hot enables can safely drive a clock-enable or an AND-OR data select, because only one source is active.

    • If two enables can ever be active at once, you have a bus contention or a lost-write bug waiting to happen.

    Common mistakes

    $onehot0(x) is true when zero or one bits of x are set. $onehot(x) is true when exactly one bit is set.

    These functions count known ones; they do not reject unknown bits. For example, $onehot0(4'b00x1) is true. Pair the one-hot check with !$isunknown(we) so an unknown second enable cannot pass as safe. This is a sampled write-enable check; asynchronous bus drivers also need between-edge checks.

    Check yourself The select reads 2 and the enable is high. Which bit of y is high?

    Bit 2 only: y reads 0100. The decoder sets exactly the selected bit.

    Check yourself The enable drops low while the select still reads 2. What does y read now?

    All zeros. A disabled decoder wakes no line, which is how it gates a write strobe.

    Check yourself A decoder has a 4-bit select. How many output lines does it need, and how does the area grow as you add select bits?

    16 outputs. Every additional select bit doubles the number of outputs (and roughly the gate count), so wide address decodes are normally built hierarchically (decode the upper bits first, then the lower ones).

    Testbench

    The parameterized decoder plus a testbench that checks every select and the disabled case.

    decoder.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    module decoder #(parameter int K = 2) (
      input  logic [K-1:0]       sel,
      input  logic               en,
      output logic [(1<<K)-1:0]  y                 // one-hot
    );
      always_comb begin
        y = '0;
        if (en) y[sel] = 1'b1;                     // set exactly one bit
      end
    endmodule
    

    Remember

    • A k-bit decoder gives 2^k outputs with one high; with enable low, all are low.

    • $onehot0(we) is true for zero or one bit set, but it does not reject X, so also check !$isunknown(we).

    • Wide address decodes are built in stages, because each select bit doubles the outputs.

    Rule of thumb: use one decoder, not a chain of address comparisons that must be edited when the map changes.

    Keep going

    • Practice: A 3-to-8 decoder with enable: one-hot guarantees and X-propagation

    • Code lab: Build a 2:4 decoder

    • Interview: run a mock interview on this topic

    Related topics

    • Priority encoder

    Previous

    42 / 350

    Next: Priority Encoder

    Up next

    Up nextPriority Encoder

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Mux Theory and Design Patterns
    clkensel0123y01248
    A 2-to-4 decoder waking one line at a time