Friday, June 28, 2013

Replication operator in Verilog

Replication operator is used to replicate a group of bits n times. The same number, it may be a wire, register or a number , can be repeated several times. The syntax is given below.
{n{bit_pattern}}
Some examples are given below
module replication ;
reg [1:0] reg1 ;
    initial begin 
        reg1 = 2'b10 ;
        $display(" {2{2'b10}} %b",{2{2'b10}}) ;
        $display(" reg1       %b",reg1) ;
        $display(" {2{reg1}}  %b",{2{reg1}}) ;
        $finish ;
    end
endmodule 
Download
Output is as follows
$ {2{2'b10}} 1010
$ reg1 10
$ {2{reg1}} 1010
In first $display the bit pattern 2'b10 is replicated twice and in third one the content of register reg1 is replicated twice.

Timescale in Verilog

Usually in testbenches we need to put some delay for testing the module under test. The `timescale directive controls the unit used to specify the time and the precision of it. The syntax is given below.
  

`timescale time_unit/time_precision

It can be explained with an example.

`timescale 1ns/1ps
module timescale ;
reg clk ;
    initial begin
        $timeformat(-9,2,"ns", 10);
                    $display("time : %t",$time) ;
        #10         $display("time : %t",$time) ;
        #1          $display("time : %t",$time) ;
        $finish ;

    end
endmodule
A delay is put with a preceeding #. In the given example the time_unit is 1ns and the precision is 1ps .
The first $display will print at time zero as no delay is introduced. Now a delay of #10 is introduced. The timescale says the time_unit is in 1ns and hence 10x1ns = 10ns delay is put there.
Now time_precision tells the how precise the time is, ie the minimum delay that can be given or the minimum time fraction that can be observed in the simulator.
In the above example it is possible to monitor the waveform with a resolution of 1ps and the minimum time for which an event can be delayed is 1ps. The output is given below.

$ time : 0.00ns
$ time : 10.00ns
$ time : 11.00ns

More info

Sunday, June 23, 2013

What is a module in verilog

The main building block in Verilog is the module. The circuits are build by connecting modules together.
Simply speaking, a module can be considered as an IC. Please have a look at this simple adder module which can be considered as an adder IC. It takes some data and produce output. Here, in case of adder, the output is sum.

Communicating with the outside world

Modules use ports to communicate with the outside world.
The data is passed to the module through input port. The port can be a single bit line or a bit vector.

The output of the modules are present in output port. This port can be a single bit or a bit vector.
There are inout ports which are capable of communicating in both directions.

The funtionality of a large circuit is divided into modules. Then each modules are designed and tested. Finally these modules are interconnected to make the final circuit. Different modules are interconnected with wires.

Representing numbers in verilog

The base of the number is a tricky part in verilog. If no base is specified for a number then it is a decimel number,ie it has a base of 10.

As we are dealing with either registers or wires with fixed bit width, the the number of bits must be also specified. If the number of bits is not specified, compiler consider it as 32 bit number, truncation or zero padding can occur.

In general a number can be represented as
   (width)'(base)(number)
  8'b11110000  represents a 8 bit binary number
  8'hFE        represents a 8 bit hexadecimel number
  4'd3         represents a 4 bit decimel number
  3'o7         represents a 3 bit octal number

It is not a strict rule to specify the bit width and base when a number is given. Simulation may pass with some warnings, but some synthesizers may check for it and pop up errors. So better follow this style.
module display ;
    initial begin 
        $display("8'b11110000 is %b",8'b11110000) ;
        $display("8'hFE       is %b",8'hFE) ;
        $display("4'd3        is %b",4'd3) ;
        $display("3'o7        is %b",3'o7) ;
    $finish ;
    end 
endmodule 
Download
The output is given below.
$ 8'b11110000 is 11110000
$ 8'hFE is 11111110
$ 4'd3 is 0011
$ 3'o7 is 111

Assigning numbers

Assigning numbers may lead to truncation or padding. It can be explained with an example.
module assignm ;
reg [1:0] reg1 ;
reg [1:0] reg2 ;
reg [1:0] reg3 ;
    initial begin 
        reg1 = 4 ;
        reg2 = 1 ;
        reg3 = {1'b1,1'b1} ;
        $display("reg1 %b",reg1) ;
        $display("reg2 %b",reg2) ;
        $display("reg2 %b",reg3) ;
    $finish ;
    end 
endmodule 
Download
The output is given below.
$ reg1 00
$ reg2 01
$ reg3 11
Number 4 (binary 100) is assigned to a 2 bit register reg1 and it is clear that the msb bit is truncated and the content of reg1 is displayed as 00.

Number 1 (binary 1) is assigned to a 2 bit register reg2 and a msb bit 0 is added which makes the result 01.

In case of reg3, two single bit numbers are concatenated to form a two bit number and assigned to a two bit resgister. More about concatenation operator is available here

Saturday, June 22, 2013

Commenting in verilog

Like other programming languages verilog supports both single-line comment and block comment.

Single line comment starts with // and ends at the end of line.
    ...........
    #4 ;
    $display("%d",out) ; //prints value 
    //$finish ;
    end
    ............
In the above code the parts in green color italics are commented. As you can see its possible to comment single line here

    ............
    initial begin 
    /*clock = 0 ;
    reset = 1 ;*/
    in1=1 ; in2=2 ;
    ............
Now, this is block comment which enables to comment a block of data.

Simulating verilog designs with Icarus in Ubuntu

Icarus Verilog is a free and opensource Verilog simulation and synthesis tool. It compiles the verilog source file to some target format known as vvp.

This intermediate format is simulated with vvp command. For synthesis netlists can be generated.

The verilog designs can be simulated as follows. Here I am going to simulate a simple helloword module.

$ iverilog -o hello hello.v   produce an intermediate format for simulation

$ vvp hello                   gives the result



Installing Icarus/iverilog(free verilog simulator) in Ubuntu

Icarus Verilog is a free and opensource Verilog simulation and synthesis tool. It compiles the verilog source file to some target format known as vvp.

This intermediate format is simulated with vvp command. For synthesis netlists can be generated.

Icarus verilog is now available in Ubuntu respository and can be installed with apt command

$ sudo apt-get install iverilog


Thursday, June 20, 2013

My first synthesizable verilog module (sequential)

I think you may have gone through my first combinational module here. Now I am introducing you to my first sequential module, of course an adder, but now its sequntial.
You will find the difference between them as you go ahead.

Here is a simple sequential adder module
module adder (
input clock ,
input reset ,
input [7:0] in1, //input one 8 bit width
input [7:0] in2, //input two
output reg [8:0] out //output sum 9bit
);

always @ (posedge clock) begin 
    if(reset) begin
        out <= 0 ;
    end
    else begin
        out <= in1 + in2 ;
    end
end 

endmodule
Download
It differs from the combinational adder circuit due to the presence of clock input. Here everything is synchonised to the clock, ie the input data is sampled and added with reference to the clock. The addition may occur in rising edge or falling edge.

In this adder, the block of statements inside the always block is executed at every positive edge or rising edge of clock. Inside the block it checks for reset signal and makes the out zero if the reset is high. If the reset is low then the sum is placed at out

One thing we shoule keep in mind that, if we place the data in the current cycle the output will be available after next rising edge. One clock cycle delay is preset in processing.

`timescale 1ns/1ns 
module adder_test ;

reg clock ;
reg reset ;
reg [7:0] in1 ; //declaration of register in1
reg [7:0] in2 ; //declaration of register in2
wire [8:0] out ; //declaration of wire out

adder adder_uut (
.clock(clock),      
.reset(reset),     
.in1(in1),      //register connect to input
.in2(in2),      //register connect to input
.out(out)       //wire connect to output
);
    initial begin 
    clock = 0 ;
    reset = 1 ;
    in1=1 ; in2=2 ; //test values assigned to regs
    #8 ;
    reset = 0 ;
    #4 ;
    $display("%d",out) ;
    $finish ;
    end

always #2 clock = ~clock ;

endmodule
Download

Leave the timescale for a while and only be aware that all time units are in nanoseconds.

The #8 is not a hashtag, it put a delay of 8ns. Intially the adder is resetted by making reset high. After 8ns the reset is deasserted. Oh, I forgot mention about the clock.

The main challenge here is clock. We need to supply clock to the adder module. The always block makes a clock of 4ns period. The statement instructs to toggle the clock signal with a delay 2ns which produce a 4ns clock.

From the waveform it is clear that the output changes only at the rising edge of clock eventhough inputs are present from the start. A 4ns delay is introduced before the display command to ensure that the process is over and result is ready.

wave
This is the difference between combinational and sequential modules.

Monday, June 10, 2013

My first synthesizable verilog module (combinational)

Here I am going to explain about the simulation of a simple synthesizable combinational module. What is synthesizable ? I will tell you in detail later.

Now keep in mind that each and every verilog module cannot be ported to fpga or asic, such as the module having for loop, while loop etc.

A simple adder module is given below.
module adder (
input [7:0] in1, //input one 8 bit width
input [7:0] in2, //input two
output [8:0] out //output sum 9bit
);

assign out = in1 + in2 ; //assigns sum to out

endmodule
Download
A module can be considered as an IC. A module communicates with outside world through ports. Data is passed to the module using input port and the result is available on output port. Here two inputs are present, in1 and in2, having a width of 8 bit. We apply input data to be added on in1 and in2. The sum is available on output port out.

Inorder to check the functionality of the module we need to supply the input and monitor the output right. The perform these functions the module is put inside another module called testbench.

The testbench supplies the data and checks or displays the result.First I will give the testbench and then I will explain.
module adder_test ;

reg [7:0] in1 ; //declaration of register in1
reg [7:0] in2 ; //declaration of register in2
wire [8:0] out ; //declaration of wire out

adder adder_uut (
.in1(in1),      //register connect to input
.in2(in2),      //register connect to input
.out(out)       //wire connect to output
);
    initial begin 
    in1=1 ; in2=2 ; //test values assigned to regs
    $display("%d",out) ;
    $finish ;
    end

endmodule
Download
In testbench values can be assigned only to register, ie reg . So input to the adder modules must be connected to registers.

The output is taken from the module using wire , so out port is connected to a wire type. Please go through the comments.

In the initial-end block, initially test values are assigned to inputs. The adder module process data and sum is placed in out port.

The $display command displays the data, in the given example if the output is 3 we could say that adder is working fine.

The $finish system task finishes the simulation.

wave
It is clear that the output changes as the input changes, the only delay preset will be the propagation delay which is not diplayed. This is a combinational circuit as no clock is present in the module.

Simulating Verilog design with modelsim in Ubuntu

I hope you have a installed modelsim software with its binaries added to the path.
If I am wrong then follow this tutorial.
Here I am proceeding with linux os, of course I will be covering windows too. In both case things are same but is done in different way.

Create a directory to mess up with
$ mkdir test

Now make a local copy of modelsim.ini file. Your preferences and lib paths are stored in this file.
$ cp /install/altera/12.1/modelsim_ase/modelsim.ini .

Its time to make a work library, all module are compiled to that library before simulation
$ vlib work

This work library must be pointed in the modelsim.ini file. It can be done as follows
$ vmap work work

Verilog file are compiled with vlog command. I am going to compile the helloworld module.
$ vlog hello.v

The erros will be displayed in the terminal and if error free simulate it.
$ vsim -c hello -do "run -a" //simulate in commandline
$ vsim hello -do "run -a" //simulate with gui

Here vsim starts the simulation and run -a runs the simulation.

Saturday, June 8, 2013

Installing modelsim in Ubuntu 13.04 (Raring Ringtail)

Modelsim is an industrial standard HDL simulator.It can simulate VHDL, Verilog, SystemVerilog and SystemC.
Here I am descring the installation of modelsom altera starter edition in Ubuntu.
If is a free licence version of modelsim which would be a better starting point for a newbie.

1.You need to register on the site to download the package, so be patient and complete
   the registration process. Follow the link.

2.Now download the compressed installation package.  Follow the download link
   under ModelSim-Altera Starter Edition.



3.For windows installation .exe is provided and follow the steps as usual. For linux zip   file is provided.
  $ tar -xvf 12.1_modelsim_ase_linux.tar.gz
  It may not be set with the permission to execute, so make it executable and run.
  $ chmod +x script.sh ; ./script.sh

4.Enjoy coding !!

HelloWorld in verilog

This is a HelloWorld module in verilog. I assume that you have an installed and configured verilog simulator. This is not the proper way of starting HDL coding, but, as every programming languages starts with HelloWorld let me start here.
module hello ;
    initial begin
        $disply("HelloWorld") ;
    end
endmodule

The output of the program will be "HelloWorld" displayed on terminal.
If you haven't installed a simulator yet follow the tutorial for modelsim or verilator.