Verilog Coding Tips and Tricks: October 2015

Tuesday, October 27, 2015

Verilog code for BCD to 7-segment display converter

A seven-segment display (SSD) is a form of electronic display device for displaying decimal numbers. They can be used as an alternative to complex display's such as dot matrix.

A SSD has 7 segments and theoretically we can use it to display 2^7 = 128 combinations of characters. But most of these combinations, doesn't make sense to a human eye. Decimal numbers can be displayed correctly on a 7 segment panel as shown below:

 



The image on the right indicates the order in which the panels are accessed normally. This is done using a 7 bit vector.

Here, I have written a Verilog code which takes in a BCD number and converts it into a 6 bit vector format, which the seven segment panel understands. Note that, to light up an individual panel, we have to switch it OFF(pass '0' through it).

Seven segment display Code:

//Verilog module.
module segment7(
     bcd,
     seg
    );
     
     //Declare inputs,outputs and internal variables.
     input [3:0] bcd;
     output [6:0] seg;
     reg [6:0] seg;

//always block for converting bcd digit into 7 segment format
    always @(bcd)
    begin
        case (bcd) //case statement
            0 : seg = 7'b0000001;
            1 : seg = 7'b1001111;
            2 : seg = 7'b0010010;
            3 : seg = 7'b0000110;
            4 : seg = 7'b1001100;
            5 : seg = 7'b0100100;
            6 : seg = 7'b0100000;
            7 : seg = 7'b0001111;
            8 : seg = 7'b0000000;
            9 : seg = 7'b0000100;
            //switch off 7 segment character when the bcd digit is not a decimal number.
            default : seg = 7'b1111111; 
        endcase
    end
    
endmodule

Testbench:

module tb_segment7;

    reg [3:0] bcd;
    wire [6:0] seg;
    integer i;

    // Instantiate the Unit Under Test (UUT)
    segment7 uut (
        .bcd(bcd), 
        .seg(seg)
    );

//Apply inputs
    initial begin
        for(= 0;< 16;= i+1) //run loop for 0 to 15.
        begin
            bcd = i; 
            #10; //wait for 10 ns
        end     
    end
      
endmodule

Simulation Waveform:

The codes were correctly simulated in Xilinx ISE 13.1. Check the waveform below:


Monday, October 26, 2015

Loop statements in Verilog - forever,repeat,for and while

Loop statements are used for executing a block of statements repeatedly. If the block has more than one statement we can group them together under one loop using begin ... end keywords.

There are four loop statements in Verilog:

forever:

This type of looping is used to execute a block of statements forever, meaning until the end of simulation. Normally this is used for generating clock in a testbench. 

For example,

//The following block generates a Clock signal with 10*2=20 ns period 
//and 50 MHz frequency
initial 
begin 
   Clock = 0; 
   forever #10 Clock = ~Clock; 
end

repeat:

As the name suggests, this type of looping can be used to execute a block of statements repeatedly, for a fixed number of times. 

//Declare variables
    integer j=5;
    integer product;

//Code section for calculating product=4*j
//repeatedly add j, four times.
    initial 
    begin
        product = 0;
        repeat(4)
        begin
            product = product + j;
            $display("product = %d",product);
        end
    end

The following values will be printed in the console when you run the code:

product = 5
product = 10
product = 15

product = 20

for:

For loops are used, when you want to specify more conditions for the looping of the statements. Using a for loop, you can mention the starting value of a variable, the ending value of the variable and the step by which the value has to be incremented or decremented. It's similar to C or C++, except that you don't have ++ or -- operators.

    integer i;
    integer sum;
    //Add decimal numbers from 1 to 10
    initial 
    begin
        sum = 0; //initialize sum to zero.
        for(= 1 ; i <= 10 ; i = i + 1)
        begin
            sum = sum + i;
        end 
        $display("sum = %d",sum); //display the sum in simulation console
        $finish; //stop the simulation.
    end

The following will be printed in the console when you run the code:
sum = 55

while:

While loop is used mostly in testbenches. The block of statements are repeatedly executed as long as the conditional expression is true.

Look at the example for the, for loop. I will implement the same logic using while loop here.

    //Add decimal numbers from 1 to 10
    initial 
    begin
        sum = 0; //initialize sum to zero.
        i = 1;
         //as long as i is less than or equal to 10,the block of statements are executed.
        while(<= 10)
        begin
            sum = sum + i; //add i to sum
            i = i + 1; //increment i.
        end 
        $display("sum = %d",sum); //display the sum in simulation console
        $finish; //stop the simulation.
    end

The result will be the same,

sum = 55

Note:-

Its up to you, to decide which looping statement you want to use. But be careful with looping statements. Verilog is a Hardware Description Language(HDL) and most of the time ,the code you write has to be implemented in a real hardware. Large loops can cause excessive usage of resources, large delays and crazy synthesis times.