Showing posts with label modelsim. Show all posts
Showing posts with label modelsim. Show all posts

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 !!