Skip to content

VLSIFacts

Let's Program the Transistors

  • Home
  • DHD
    • Digital Electronics
    • Fault Tolerant System Design
    • TLM
    • Verification
    • Verilog
    • VHDL
    • Xilinx
  • Embedded System
    • 8085 uP
    • 8086 uP
    • 8051 uC
  • VLSI Technology
    • Analog Electronics
    • Memory Devices
    • VLSI Circuits
  • Interview
    • Interview Experience
    • Training Experience
    • Question Bank
  • Notifications
  • QUIZ
  • Community
  • Job Board
  • Contact Us

Case and Conditional Statements Synthesis CAUTION !!!

Posted on November 12, 2015June 16, 2025 By Priyadarshi 5 Comments on Case and Conditional Statements Synthesis CAUTION !!!

Case and Conditional Statements are available in both VHDL and Verilog. These are considered as significant features of behavioral modelling, be it in VHDL or Verilog. Behavioral modelling provides high level abstraction so that the circuit can be designed by programming its functionality.

Let’s say, we have to design a circuit that selects a particular input line to be transmitted to the output at a particular instant. To be more specific, let’s consider we have four input lines ‘a’,’b’,’c’,’d’ and and the circuit would decide which input line goes to the output. Now if you are spontaneous enough and you know digital a little bit then you will jump and say its a 4:1 MUX. Good enough; but now leave the MUX aside and look at the functionality.

There are two ways to code it in behavioral modelling – “conditional statements If and else” and “multiway branching case“. Lets use both in Verilog to construct the above circuit.

Code (A) Use of Conditional Statements

module MUX_4_1 (out,sel,a,b,c,d);
input a,b,c,d;
input [1:0] sel;
output reg out;
always @ (a,b,c,d,sel)
  begin
    if (sel==2'b00) // Use of conditional statement
      out=a;
    else if (sel==2'b01)
      out=b;
    else if (sel==2'b10)
      out=c;
    else
      out=d;
  end
endmodule

The RTL schematic implemented by the above code using Quartus II

Image (a)

Code (B) Use of case Statement

module MUX_4_1_case (out,sel,a,b,c,d);
  input a,b,c,d;
  input [1:0] sel;
  output reg out;
  always @ (a,b,c,d,sel)
    begin
      case (sel)
        2'b00 : out = a;
        2'b01 : out = b;
        2'b10 : out = c;
        default : out = d;
      endcase
    end
endmodule

The RTL schematic implemented by the above code using Quartus II

Image (b)

Let’s compare both the schematics from an electronic engineer point of view.

If we compare these two hardware, we can observe that the image (a) has unnecessary comparators and two multiplexers are joined like priority mux which is not a good design as it adds combinational delay to the design, while in image (b) it is a conventional multiplexer with very less delay. Then for obvious reason design (b) would be preferred over design (a).


To get the better feel of the comparison, let’s add schematics of 8:1 MUX for both the design styles:

8:1 MUX functionality structure : A schematic inferred by using conditional statements
8_1 mux edited
8:1 MUX functionality structure : A schematic inferred by using case statements

The difference can be easily observed.Now if you synthesize it on any other tool lets say ISE, you may probably get a mux for both, as tools are intelligent enough to figure out that this a non overlapping full case or if, but it doesn’t mean that you can go with it especially in an ASIC design.


If you forget to cover all the conditions that means if you forget to write “default” in a “case” or “else” condition in a “if” then the tool will infer a latch for that signal, now latches are not preferred for design if not implemented intentionally as they cause serious timing issues.

For eg. consider the following code:

module MUX_8_1_case (out,sel,a,b,c,d,e,f,g,h);
input a,b,c,d,e,f,g,h;
input [2:0] sel;
output reg out;
always @ (a,b,c,d,e,f,g,h,sel)
  begin
    case (sel)
       3'b000 : out = a;
       3'b001 : out = b;
       3'b010 : out = c;
       3'b011 : out = d;
       3'b100 : out = e;
       3'b101 : out = f;
       3'b110 : out = g;
    endcase //Default statement absent
  end
endmodule
Mux_latch_edited

The schematic inferred by the above code is as below:

Same happens when a “else” is absent in conditional statements as described in the following code:

module MUX_4_1 (out,sel,a,b,c,d,en);

input a,b,c,d,en;
input [1:0] sel;
output reg out;

always @ (a,b,c,d,sel,en)
  begin
    if (sel==2'b00 && en)
      out=a;
    else if (sel==2'b01 && en)
      out=b;
    else if (sel==2'b10 && en)
      out=c;
    else if (sel==2'b11 && en) //else statement absent
      out=d;
  end
endmodule

It implements this structure:

if_latch_edited

Even the synthesis toll will give you a warning for this. You may achieve the functionality that you want, but it may not be always correct; for example in the above code if the en pin is high the circuit works fine but when it is low it will latch the previous value instead of resetting the output to 0.


Also, when you have multiple outputs its important to cover each output in every condition of the code otherwise a latch will get inferred. For example look at the following code.

module MUX_4_1_case (out1,out2,sel,a,b,c,d,en);

  input a,b,c,d,en;
  input [1:0] sel;
  output reg out1,out2;

  always @ (a,b,c,d,sel,en)
    begin
       case ({sel,en})
          3'b001 : begin
             out1 = a;
             out2 = d;
          end
          3'b011 : begin
             out1 = b;
             out2 = c;
          end
          3'b101 : begin
             out1 = c;
           //out2 = b; // comment out this line for output 2
          end
          3'b111 : begin
            out1 = d;
            out2 = a;
          end
          default : begin
            out1 = 0;
            out2 = 0;
          end
       endcase
    end
endmodule

Look at the RTL it gives as a result

Latch_for_out2

Observe the latch marked in the schematic.

Now if we remove the comment then the RTL will be like in the image below.

Dual_Mux

 So these are some of the small concepts regarding the use of “Case” an “If” statements which should be taken care while designing. If not considered, the design may show proper functionality but would fail when a critical condition or timing mismatch happens.

Spread the Word

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related posts:

  1. Verilog vs VHDL
  2. Synthesis and Functioning of Blocking and Non-Blocking Assignments.
  3. Synopsys – Interview Questions – based on Synthesis and Simulation
  4. VLSI Design Flow
DHD, Digital Electronics, Verilog Tags:Altera, case, conditional, Digital Design, HDL, if-else, Quartus, Synthesis, Verilog, VHDL

Post navigation

Previous Post: Digilent Design Contest 12th Edition
Next Post: Training Experience – Mentor Graphics Higher Education Program

Comments (5) on “Case and Conditional Statements Synthesis CAUTION !!!”

  1. Atticus says:
    July 7, 2017 at 8:55 am

    Hello,

    Your tutorials are nice and well written. Thank you for taking the time out.

    You have not actually included the RTL schematic for the ones you have mentioned.
    If it is possible Please add them.

    Thanks

    Reply
  2. Rajeshkumar Athram says:
    October 5, 2018 at 12:50 pm

    hi,
    i have copied the same code which you have written in this page:
    <<>

    but the hardware you are mentioned is not matching with mine.

    i am not getting any compactor.for “sel”

    but i am getting mux with input buffers. in XILINX ISE.

    can you please explain how you got the hardware ??

    as of my knowledge if i don’t write compare code i wont get a compactor.

    please explain it.

    Reply
    1. Priyadarshi says:
      October 9, 2018 at 6:19 pm

      Hi, Rajeshkumar

      I have mentioned in the post itself that, if you youse ISE that can infer a mux for that. The point is, the generated RTL schematic differs from tool to tool and also with the directed fpgas. I hope this amswers your query

      Reply
  3. singaraju kiran kumar says:
    July 13, 2019 at 12:36 pm

    sir can i use case statement in data flow model.

    Reply
    1. Sidhartha says:
      November 13, 2019 at 3:14 pm

      No, you can not use case statements in data flow model. It can only be used in behavioral model.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top Posts & Pages

  • ASCII Code
  • Different Coding Styles of Verilog Language
  • Truth Tables, Characteristic Equations and Excitation Tables of Different Flipflops
  • NAND and NOR gate using CMOS Technology
  • Difference between $display, $monitor, $write and $strobe in Verilog

Copyright © 2025 VLSIFacts.

Powered by PressBook WordPress theme

Subscribe to Our Newsletter

%d