Difference between if..else and case statement in VHDL

If..else:-

An if…else statement is a sequential statement in VHDL which got executed depending on the value of the condition.

The if condition tests each condition sequentially until the true condition is found. An if..else statement could also be treated as decision making statement and the statement associated with it could be either a Boolean expression or a condition.

It should contain one if statement,can have zero or more else if but at least one else condition,although optional.

The syntax in VHDL is :-

if boolean-expression/condition then 
 sequential statement 
{elsif boolean-expression/condition then 
 sequential statement} 
{else 
 sequential statement} 
end if; 

Let's took a simple example of MUX and it's hardware through which the difference can be drawn with the Case statement.

VHDL code of Mux using if else:-

library IEEE; 
use IEEE.STD_LOGIC_1164.all; 
 
entity 4_1_mux is 
port(X: in STD_LOGIC_VECTOR(3 downto 0); 
     S0,S1: in STD_LOGIC; 
     Z: out STD_LOGIC); 
end 4_1_mux 
 
architecture behav of 4_1_mux is 
 begin 
  MUX : process(X,S0,S1) 
  begin 
   if (S0 = '0' and S1 = '0') then 
    Z <= X(0); 
   elsif (S0 = '0' and S1 = '1') then 
    Z <= X(1); 
   elsif (S0 = '1' and S1 = '0') then 
    Z <= X(2); 
   else 
    Z <= X(3); 
   end if; 
end process; 
end behav;

Now let's look how the hardware structure of if else will look like after synthesis:-

No alt text provided for this image

So this clearly implies in case of a if else statement the type of hardware that it is going to produce is a Priority Encoded logic or a Priority block structure.

Case statement:-

A case statement is like a switch construct in C which selects a particular branch among many based on the value of expression.

The syntax for case in VHDL is:-case expression is  when alternative1 => sequential stmnts  [when others => sequential statements] 

case expression is 
 when alternative1 => sequential stmnts 
 
 [when others => Sequential statements]
  endcase

Let's look at code of 4:1 Mux using Case now:-

library declaration and entity part will remain same as if else:-

case {S0,S1} is 
when "00" => Z:= X(0); 
when "01" => Z:= X(1); 
when "10" => Z:= X(2); 
when "11" => Z:= X(3); 
when others => Z:= 'X' // don't care 
endcase

Let's look at the hardware structure of the same mux what case will generate:-

No alt text provided for this image

So after synthesis the hardware that is going to generate for case is a MUX and not a priority logic unlike if else.

Some other difference between if else and case are as follows:-

1] On the basis of Optimization and Speed:-

When the number of conditions are more it's a better practice to use Case statement since the compiler will automatically generates a Jump table or a hash table kind of structure from where the data can be fetched which optimised for speed and size.Also the hardware logic in case of CASE will use less resources as comparison to if else.

2] On the basis of readability:-

Case offers compact way of writing a code which offers simple way of readability as compared to ladder like structure of if else.

3] Multi-way branching and Expression:-

Case is useful in case of multi branching statements and if else is more useful for Boolean expression.


sergy pretetsky

R&D Engineer at Keysight Technologies

2 年

I think there is a mistake in this post. Your case statement code and if-else code look like they describe the same hardware. They both describe the bottom image. I decided to synthesize both using Synplify Pro through Lattice Diamond, they both mapped to the same primitives. See code below for better code example of how to create your first image.

  • 该图片无替代文字
Gaurav Sawant

MS in Electrical Engineering | 5G | Radio | RF Testing | Radio Integration | FPGA | Physical Design | Digital Design | Verification.

4 年

When we use the "If..else" structure for 4:1 mux it is synthesized as three 2:1 Mux cascaded structure. So there are three select lines considering one for each MUX. How do we control these three select lines using just S0 and S1 as an input? Will it generate extra hardware like a 2:4 decoder or something?

要查看或添加评论,请登录

Sougata Bhattacharjee的更多文章

  • Difference between Simulation and Synthesis in VHDL

    Difference between Simulation and Synthesis in VHDL

    VHDL is a hardware description Language and is originally designed for simulation.Therefore simulation and synthesis…

  • Methods to reduce Power dissipation in Circuit

    Methods to reduce Power dissipation in Circuit

    Due to increase of rate in frequency,the power dissipation of circuit will increase which will result in increase in…

  • Alternative Approach to solve a problem

    Alternative Approach to solve a problem

    The approach to this is known as Thinking outside the Box. There are in general two types of thinking process which…

    7 条评论
  • Reason why build phase is Top down in UVM

    Reason why build phase is Top down in UVM

    Build phase and Final phase are the phases in UVM phasing mechanism which are executed in top down fashion and rest all…

    5 条评论
  • Difference between LPDDR3 and LPDDR4

    Difference between LPDDR3 and LPDDR4

    Both LPDDR3 and LPDDR4 are high speed synchronous DRAM,but there a number of features on the basis of which they vary:-…

  • Difference between AXI3 and AXI4

    Difference between AXI3 and AXI4

    1] AXI3 supports Locked access while AXI4 does not support locked access primarily because of the complexity of the…

  • Difference between AXI and AHB : Complete Guide

    Difference between AXI and AHB : Complete Guide

    Both AXI (Advanced extensible Interface) and AHB (Advanced High Performance Bus) are part of the AMBA (Advanced…

    5 条评论
  • What to use in a Verification Environment : uvm_config_db or uvm_resource_db?

    What to use in a Verification Environment : uvm_config_db or uvm_resource_db?

    Configuration in UVM helps in building Reusable Testbenches by parameterizing the uvm_component hierarchy and each…

    5 条评论
  • Basics of VLSI

    Basics of VLSI

    With reference to my previous post which explains the importance of using SystemVerilog over Verilog for the purpose of…

社区洞察

其他会员也浏览了