Chuyển đến nội dung

Bài 13 · Combinational Logic

Multiplexers, Decoders and Encoders in Verilog

Design the classic data-routing blocks in Verilog: multiplexers, demultiplexers, decoders, encoders and priority encoders, with code, wide-bus examples and runnable C models.

A small set of combinational blocks appears in almost every digital design: multiplexers choose one of several inputs, decoders turn a binary number into a one-hot line, encoders do the reverse, and priority encoders handle several active requests. This lesson shows each in Verilog and in a runnable C model.

Multiplexer (mux)

A mux selects one of N data inputs using a select value.

VERILOG
module mux4 #(parameter W = 8) (
    input  wire [W-1:0] d0, d1, d2, d3,
    input  wire [1:0]   sel,
    output reg  [W-1:0] y
);
    always @* begin
        case (sel)
            2'd0: y = d0;
            2'd1: y = d1;
            2'd2: y = d2;
            default: y = d3;
        endcase
    end
endmodule

A one-line alternative uses an array and an index: assign y = in[sel];, where in is a packed bus and the index selects a slice. A wide mux is a tree of 2:1 muxes; its cost grows with the number of inputs and the data width.

flowchart LR
  d0 --> M["4:1 mux"]
  d1 --> M
  d2 --> M
  d3 --> M
  sel["sel[1:0]"] --> M
  M --> y

A 4:1 multiplexer: the select bits decide which input reaches the output.

Demultiplexer

A demux sends one input to one of several outputs:

VERILOG
module demux4 (
    input  wire       d,
    input  wire [1:0] sel,
    output wire [3:0] y
);
    assign y[0] = (sel == 2'd0) ? d : 1'b0;
    assign y[1] = (sel == 2'd1) ? d : 1'b0;
    assign y[2] = (sel == 2'd2) ? d : 1'b0;
    assign y[3] = (sel == 2'd3) ? d : 1'b0;
endmodule

Decoder

A n-to-2^n decoder raises exactly one output for each input code (one-hot). With an enable it is the building block of address decoding: which chip or register is selected?

VERILOG
module decoder3to8 (
    input  wire [2:0] a,
    input  wire       en,
    output reg  [7:0] y
);
    always @* begin
        y = 8'b0000_0000;            // default avoids a latch
        if (en) y[a] = 1'b1;
    end
endmodule

Here y[a] is a variable bit-select used as a write target. A shift does the same: assign y = en ? (8'd1 << a) : 8'd0;.

a y (en = 1)
000 00000001
001 00000010
010 00000100
111 10000000

A common use is a 7-segment decoder, mapping a 4-bit digit to the seven LED segment lines with a case statement.

VERILOG
always @* begin
    case (digit)
        4'd0: seg = 7'b1111110;   // gfedcba, segment a is bit 0
        4'd1: seg = 7'b0110000;
        4'd2: seg = 7'b1101101;
        4'd3: seg = 7'b1111001;
        default: seg = 7'b0000000;
    endcase
end

Encoder

An encoder turns a one-hot input into its binary index. A plain encoder assumes exactly one input is active:

VERILOG
always @* begin
    case (in)
        4'b0001: out = 2'd0;
        4'b0010: out = 2'd1;
        4'b0100: out = 2'd2;
        4'b1000: out = 2'd3;
        default: out = 2'd0;      // invalid input
    endcase
end

If two inputs are active the result is wrong, which is why most real designs use a priority encoder.

Priority encoder

A priority encoder gives the index of the highest active input and a valid flag when any input is active.

VERILOG
module prio_enc8 (
    input  wire [7:0] req,
    output reg  [2:0] idx,
    output wire       valid
);
    assign valid = |req;
    always @* begin
        idx = 3'd0;
        casez (req)
            8'b1???_????: idx = 3'd7;
            8'b01??_????: idx = 3'd6;
            8'b001?_????: idx = 3'd5;
            8'b0001_????: idx = 3'd4;
            8'b0000_1???: idx = 3'd3;
            8'b0000_01??: idx = 3'd2;
            8'b0000_001?: idx = 3'd1;
            8'b0000_0001: idx = 3'd0;
            default:      idx = 3'd0;
        endcase
    end
endmodule

Priority encoders resolve contention in arbiters and interrupt controllers. The valid flag matters because index 0 is ambiguous: it can mean "request 0" or "no request".

Stand-in: the four blocks in C

This stand-in (not Verilog) models each block as a function and prints the behavior of the decoder, the encoder and the priority encoder for sample inputs.

C Stand-in: mux, decoder, priority encoder (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Building big blocks from small ones

A 4-to-16 decoder can be built from five 2-to-4 decoders, using the high bits to enable one low decoder. This hierarchical pattern is how address maps are decoded: top bits choose a region, lower bits choose a register inside it.

C Stand-in: 4-to-16 decoder from 2-to-4 decoders (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Design tips

  • Always give a decoder an enable input in bus designs; it composes cleanly.
  • Treat out-of-range select values deliberately (default branch) rather than leaving them to chance.
  • Use parameters for the data width so one mux module serves every bus.
  • Remember cost: a 32-bit 8:1 mux is 32 copies of an 8:1 mux.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. How many outputs does a 3-to-8 decoder have, and how many are active at once?

    n inputs decode to 2^n one-hot outputs, with exactly one active when enabled.
  2. 2. Why does a priority encoder need a valid output?

    Without valid, an all-zero request vector looks identical to a request on input 0.
  3. 3. What does a 4:1 multiplexer need as select input?

    log2(4) = 2 select bits choose among four inputs.
  4. 4. What is the risk of a plain (non-priority) encoder?

    It assumes one-hot input; with several active inputs its output is not meaningful.

Summary

  • A mux selects one of N inputs; a demux routes one input to one of N outputs.
  • A decoder turns a binary code into a one-hot output and is the basis of address decoding.
  • An encoder reverses that, and a priority encoder returns the highest active index plus a valid flag.
  • Use defaults, parameters and enables to make these blocks safe and reusable.

Next: Adders, Subtractors and a Simple ALU

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.