TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

Verilog Question: 3:1 Multiplexer in Dataflow and Other Styles

The Editor's Desk
by
0
Share Facebook X LinkedIn WhatsApp Email

A 3:1 mux has three data inputs D0, D1 and D2, two select inputs S0 and S1 and one data output Y.

The value of output Y is calculated as follows:

  • Y = D0 if S0 = 0 and S1 = 0
  • Y = D1 if S0 = 1 and S1 = 0
  • Y = D2 if S1 = 1 (the value of S0 doesn't matter)
  1. Write a Verilog module for the 3:1 multiplexer using dataflow style modeling (assign) that implements the sum-of-products equation for Y.
  2. Write a Verilog module for the 3:1 multiplexer that uses the "?" operator. Again use a dataflow style for your code (i.e., use "assign" to set the values of your signals).
  3. Write a Verilog module for the 3:1 multiplexer that uses the "case" statement. Remember that a "case" statement can only appear inside of a behavioral block.

What the question is testing

This is a classic interview and lab question because one small circuit exercises three different ways of describing hardware in Verilog:

  • Dataflow with Boolean equations: you derive the logic yourself and write it with a continuous assign.
  • Dataflow with the conditional operator: you describe the selection and let synthesis build the logic.
  • Behavioral with case: you describe the behavior inside an always block.

All three describe the same hardware. The differences show up in readability, in how easy it is to get the "don't care" on S0 right, and in how each style behaves in simulation when a select line is unknown (x).

Step 1: the sum-of-products equation

Write one product term for each row of the specification. D0 is selected when both selects are 0, D1 when S1 is 0 and S0 is 1, and D2 whenever S1 is 1:

Y = (~S1 & ~S0 & D0) | (~S1 & S0 & D1) | (S1 & D2)

Note that the D2 term does not include S0. That is the "S0 doesn't matter" part of the specification, and leaving it out makes the circuit smaller.

Answer 1: dataflow, sum of products

module mux3to1_sop (
  input  wire D0, D1, D2,
  input  wire S0, S1,
  output wire Y
);
  assign Y = (~S1 & ~S0 & D0) |
             (~S1 &  S0 & D1) |
             ( S1 &  D2);
endmodule

Answer 2: dataflow with the ? operator

module mux3to1_cond (
  input  wire D0, D1, D2,
  input  wire S0, S1,
  output wire Y
);
  assign Y = S1 ? D2 : (S0 ? D1 : D0);
endmodule

This reads almost exactly like the specification: if S1 is 1 pick D2, otherwise let S0 choose between D1 and D0.

Answer 3: behavioral with case

module mux3to1_case (
  input  wire D0, D1, D2,
  input  wire S0, S1,
  output reg  Y
);
  always @(*) begin
    case ({S1, S0})
      2'b00:   Y = D0;
      2'b01:   Y = D1;
      2'b10,
      2'b11:   Y = D2;
      default: Y = 1'bx;
    endcase
  end
endmodule

Y is declared reg here because it is assigned inside an always block; that does not make it a flip-flop. The @(*) sensitivity list and the assignment of Y in every branch keep the logic combinational.

Deep dive: the numbers and details

This part holds the figures, equations and worked examples. Skip it if the overview is all you need.

Full truth table and minimization

S1S0YProduct term
00D0~S1 & ~S0 & D0
01D1~S1 & S0 & D1
10D2S1 & D2 (rows merged, S0 eliminated)
11D2

The two S1 = 1 rows differ only in S0, so they combine: (S1 & ~S0 & D2) | (S1 & S0 & D2) = S1 & D2. Counted as discrete gates, the minimized form needs two inverters, two 3-input AND gates, one 2-input AND gate and one 3-input OR gate. Without the merge, you would need a third 3-input AND gate. A synthesis tool will find this simplification by itself, but an interviewer expects you to see it.

Rules each style must follow

  • A continuous assign drives a net. As the ChipVerify assign statement page puts it, the left-hand side must be a net (such as a wire), never a register. That is why answers 1 and 2 declare Y as wire.
  • A case statement is procedural and must be inside an always or initial block, as the ChipVerify case statement page explains. The same page notes that a case with no default and no matching item leaves the output unchanged, and synthesis then infers a latch.

What happens when a select is unknown

The three versions behave the same for 0 and 1 inputs, but not when S0 or S1 is x (for example before reset):

StyleIf S1 = x
Sum of productsY follows Boolean x rules: for example, if D2 = 0 and the S1 = 0 path would also give 0, Y is 0; otherwise Y is x
? operatorVerilog evaluates both branches and merges them bit by bit: Y is the common value if D2 equals the other branch, else x
case{S1, S0} matches none of 00, 01, 10, 11 exactly, so the default branch runs and Y = x

None of this changes the synthesized hardware, but it explains why the three versions can show different waveforms at the start of a simulation. Writing default: Y = 1'bx; makes unknown selects visible in simulation instead of hiding them.

A testbench that checks all three

module tb;
  reg D0, D1, D2, S0, S1;
  wire y1, y2, y3;
  integer i, errors = 0;
  reg exp;

  mux3to1_sop  u1 (.D0(D0), .D1(D1), .D2(D2), .S0(S0), .S1(S1), .Y(y1));
  mux3to1_cond u2 (.D0(D0), .D1(D1), .D2(D2), .S0(S0), .S1(S1), .Y(y2));
  mux3to1_case u3 (.D0(D0), .D1(D1), .D2(D2), .S0(S0), .S1(S1), .Y(y3));

  initial begin
    for (i = 0; i < 32; i = i + 1) begin
      {S1, S0, D2, D1, D0} = i[4:0];
      #1;
      exp = S1 ? D2 : (S0 ? D1 : D0);
      if (y1 !== exp || y2 !== exp || y3 !== exp) begin
        errors = errors + 1;
        $display("mismatch: S1=%b S0=%b D=%b%b%b -> %b %b %b",
                 S1, S0, D2, D1, D0, y1, y2, y3);
      end
    end
    $display("%0d errors in 32 vectors", errors);
    $finish;
  end
endmodule

With five inputs there are only 2^5 = 32 combinations, so the testbench checks every one. That is exhaustive verification, which is only practical for very small blocks.

Interview follow-ups

  • "Why not use if-else?" You can: if (S1) Y = D2; else if (S0) Y = D1; else Y = D0; describes the same priority as the ? version.
  • "How would you make it N bits wide?" Add a parameter for the width, declare D0, D1, D2 and Y as vectors, and the ? and case versions work unchanged. The sum-of-products version needs each select term replicated to the data width, for example {WIDTH{S1}} & D2.
  • "What does it synthesize to?" Usually two levels of 2:1 multiplexing or a small AND-OR structure; in an FPGA, the whole 1-bit function fits in a single look-up table, since it has only five inputs.

Further reading

The Editor's Desk
About the Author
The Editor's Desk
Share Facebook X LinkedIn WhatsApp Email

Post a Comment

0 Comments

Your comments will be moderated before it appears here.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!