Nityanand's Weblog

  • view my stats

    free page hit counter
  • Earn Money by viewing ads

  • Useful Links

Archive for the ‘VERILOG’ Category

Using event based modeling in verilog to avoid Racing between always blocks.

Posted by Nityanand Dubey on November 28, 2009


In most of our designs, we have to perform some operation on the posedge of clock. To implement this, we normally use different always block with the posedge of clock in the sensitivity list.

Some times, we need to use the signal in one always block, which is getting assigned a value from another, and here the actual problem starts.

Since the all the always blocks execute concurrently in the design, which may create racing between signals, which is again varies among simulator to simulator.

For this scenario, the signals may get value from one always block before/after it is used in another one.

Using “event” in the verilog programming can be one way to take care this kind of situation.

Example 1 :

Here two always blocks are shown,

In this case we are trying to set some modes by looking into some conditions. Also, we want to use the modes at the posedge of clock and perform some other operation.

Since both always blockes are working at the same posedge of clock, which may create a racing between the mode signals.

This is a small example but in real-time scenario, it might be more complex and big.

It may take the new value of mode of continue with the older value, depends on the simulator’s algorithm.

Block – 1 :

always (posedge clock)
begin
    if (condition1)
        mode1 = 1’b1;
    else if (condition2)
        mode2 = 1’b1;
    else
        mode3 = 1’b0;
end

Block – 2 :

always (posedge clock)
begin
    if( mode1 && conditions3)
        output <= input1;
    else if (mode2 && condition4)
        output <= input2
    else
        output <= 2’b0;
end

Now, Lets try to avoid the racing between these two always blocks.

Here, we use an “event” to trigger the other always block, and the triggering will take place at the last of first always block.

Since we wanted to run the both always block at the posedge of the clock, which is also happening here.

By using the event in the sensitivity list of other always block makes sure that this block will execute when the first will be completed, and it will be able to use the new values assigned to the mode variables.

We can have multiple events in the design.

// event declaration
event ev_mode

Block – 1 :

always (posedge clock)
begin
    if (condition1)
        mode1 = 1’b1;
    else if (condition2)
        mode2 = 1’b1;
    else
        mode3 = 1’b0;
    -> ev_mode
end

Block – 2 :

always (ev_mode)
begin
    if( mode1 && conditions3)
        output <= input1;
    else if (mode2 && condition4)
        output <= input2
    else
        output <= 2’b0;
end

Advertisements

Posted in VERILOG | Tagged: , , , , , , | 1 Comment »

Design synthesis and relevant verilog code for DFF

Posted by Nityanand Dubey on March 31, 2009

For RTL Coding, one should know that what code will infer which hardware in the design and vise versa.

The Design engineer should be aware of relevant code and the output logic so he can be able to minimize the design and the no of gates using.

In this post we will see deferent DFF and their verilog codes

DFF ( D- Flip-Flop)

reg q;

always @ (posedge clk)
q <= d;

In this code, the value of D(data) will be assigned to output (q) at the posegde of the clock and it will remain untouched till next posedge of the clock since the q is defined as reg.

D-Flip Flop

D-Flip Flop

DFF ( D- Flip-Flop) with Asynchronous reset.

reg q;

always @ (posedge clk or posedge reset)
if (reset)
q <= 1’b0;
else
q <= d;

According to above code, the reset is positive edge triggered and asynchronous so it is kept in the sensitivity list of always block with the posedge.

Inside the always block, there are a conditional statement to check whether the reset is true (1), otherwise it behaves as normal DFF

DFF - Async Reset

DFF - Async Reset

DFF ( D- Flip-Flop) with synchronous reset

reg q;

always @ (posedge clk)
if (reset)
q <= 1’b0;
else
q <= d;

Synchronous reset means the flop will be reset only with the posedge of clock, hence the sensitivity list does not have the reset signal; However it check the reset for true whenever the posedg of clock comes.

DFF - Sync Reset

DFF - Sync Reset

DFF ( D- Flip-Flop) with gated clock.

reg q;
wire gtd_clk = enable && clk;

always @ (posedge gtd_clk)
q <= d;

Many times it becomes required to apply clock to the design only when the enable is active. This is done by the gated clock. The clock and enable both are the inputs to a and gate and the output of the gate goes to the clock input of the flop.

DFF - Gated Clock

DFF - Gated Clock

Data Enabled DFF

reg q;

always @ (posedge clk)
if (enable)
q <= d;

Data Enabled flops has a mux at the data input, which is controlled by the enable signal. Now even the posedge of clock comes to the flop, flop will not change the value until enable is not active.

DFF - Data Enable

DFF - Data Enable

Negative Edge triggered DFF

reg q;

always @ (negedge clk)
q <= d;

In this code, the value of D(data) will be assigned to output (q) at the negedge of the clock and it will remain untouched till next negedge of the clock since the q is defined as reg.

DFF -Negative Edge

DFF -Negative Edge



Posted in ASIC Design Flow, VERILOG | Tagged: , , , , , , | Leave a Comment »

What is Timescale in verilog codes

Posted by Nityanand Dubey on March 18, 2009

The ‘timescale is one of the compiler directive to specify the unit for the delays used in the design with their precision.

The timescale line is very important in the verilog simulation, because there are no any default delays specified by the verilog.

Syntax :

`timescale <time unit>/<time precision>

Here :

time unit : This is the time to be used as one unit for all the delays used in the design.

time precision : This represents the minimum delay which needs to be considered during simulation or it decides that how many decimal point would be used with the time unit.

Range of Timescale :

The range for time unit can be from seconds to femto-seconds, which includes all the time units including s (second), ms(mili-second), us(micro-second), ns(nano-second), ps(pico-second) and fs(femto-second).

Example :

`timescale 1ns/1ps

Here :

1ps = 0.001 ns

#1; // = 1ns delay

#1.003; // = will be considered as a valid delay

#1.0009; // = will be taken as 1 ns only since it is out of the precision value.

Posted in VERILOG | Tagged: , , , | 3 Comments »

What is the difference between a function and a task?

Posted by Nityanand Dubey on February 19, 2009

There are many differences in a function and a task in the verilog

1. Behavior :

Function : function call happens in real time OR no simulation delay can be inserted during the function call
Task : Tasks can be inserted with a delay

2. No of outputs :

Function : A function can have at least one input arg to be passed, and also it can have only one output to drive
Task : Task can have any no of inputs and outputs.

3. Nesting :

Function : A function can call a function inside it but not a task
Task : Task can call either a function or a task inside it.

4. Synthesis :

Function : A function can be synthesized
Task : Tasks are not synthesizable

5. Limitations :

Function : A function does not allow any delay, timing, event inside it
Task : Tasks can have delays, events inside it

6. Usage :

Function : A can be used for RTL as well as behavioral coding ( mostly for combinational logic)
Task : Tasks can be used for behavioral modeling only.

Posted in VERILOG | Tagged: , , , | Leave a Comment »

What are sequential and parallel blocks, what is fork and join statements? How it is deffer than begin and end?

Posted by Nityanand Dubey on August 2, 2008

 In verilog, we have two types of block – sequential blocks and parallel blocks. Lets look into both blocks one by one

1. Sequential Blocks –

In the sequential blocks, begin and end keywords are used to group the statements, All the statement in this group executes sequentially. ( this rule is not applicable for nonblocking assignments). If the statements are given with some timing/delays then the given delays get added into. It would be clearer with following examples.

Example -1 –

reg a,b,c;
initial
begin
     a = 1′b1;
     b = 1′b0;
     c = 1′b1;
end
The Example -1 is showing the sequential block without delays, All the statements written inside the begin-end will execute sequentially and after the execution of initial block, final values are a=1, b=0 and c=1

Example -2 –

reg a,b,c;
initial
begin
     #5 a = 1′b1;
     #10 b = 1′b0;
     #15 c = 1′b1;
end

The Example -2 is showing the sequential block with delays, In this case, the same statements are given with some delays, Since All the statements execute sequentially, the a will get value 1 after 5 time unit, b gets value after 15 time unit and c will take value 1 after 30 time unit

2. parallel Blocks –

The statements written inside the parallel block, execute parallel, If the sequencing is required then it can be given by providing some delays before the statements. In parallel blocks, all the statements occur within fork and join

Example -3 –

reg a,b,c;
initial
fork
     #5 a = 1′b1;
     #10 b = 1′b0;
     #15 c = 1′b1;
join

Form Example -3, all the statements written inside the fork and join, executes parallel, it means the c with have value ‘1′ after 15 time unit, in case of sequential blocks it was 30 time unit ( example 2)

The fork and join statements can be nested with begin-end

Example -4 ( Nested block)

reg a,b,c,d;
initial
begin
fork
     #5 a = 1′b1;
     #10 b = 1′b0;
     #15 c = 1′b1;
join

  1. 30 d = 1′b0;

end

From Example -4, the initial block contains begin-end and fork-join both. In this case c takes value after 15 time unit, and d takes the value after 30 time unit.

 

Posted in VERILOG | Tagged: , , , , , , , , | Leave a Comment »

What is sensitivity list in verilog?

Posted by Nityanand Dubey on July 20, 2008

 

 

  A simple always block runs forever it means as it touches the “end” again starts from beginning.Sensitivity list is a medium to make a controlled always block.
 
Example of normal always block
 
    always
    begin
        // statements
    end

 

     always @ ( sensitivity list)
      begin
           // statements
      end

 

The syntax of sensitivity list can be –
 
     A. always @ ( x or y or z)
     B. always @ ( posedge x )
     C. always @ ( posedge x or A )
     D. always @ ( posedge x or negedge y )
     E. always @ ( x, y, z)
     F. always @(*)
     G. always @*
    
The E, F and G are the new constructs added in the verilog 2001.

Usage of different syntaxes in verilog –

Point A and E are same in behavior, taking an example for syntax A and E

Example :1     Always @ ( x,y,z)
     Begin
         Sum = x + y + z
     End

 

Point B, C and D are normally used for sequential logic implementation

Example : 2

     always @ ( posedge clock or negedge reset )
     begin
     if (!reset)
         q<= 0;
     else
         q<= data
     end

Point F and G are equivalent in behavior. These are some easy options to use without bothering about the sensitivity list

Example : 3     Always @ (*)
     Begin
         Sum = x + y + z
     End

 

In this case whatever values are used in the right hand side (RHS) would be taken in the sensitivity list So there is any change in the x , y or z values, the always block would be executed..
 

 

 

 

Posted in VERILOG | Tagged: , , , | 2 Comments »

What is a module in verilog?

Posted by Nityanand Dubey on July 20, 2008

We can assume that a design is made of different modules/blocks.
 
  As a definition we can say
 
  “A module is a block with well defined inputs, outputs and interconnections (functionality)”
 
  The module is defined by keyword “module” and ends with endmodule Here is an example of module for a full adder
 
 
module adder (co, sum, x, y, cin);
  output co;
  output sum;
  input x;
  input y;
  input cin;
 
  reg co;
  reg sum;
  always @(*)
  {co, sum} = x + y + cin;
  endmodule
 

 
  In this example – module is a keyword to define the bock named “adder”
 
  All the inputs and outputs of block are defined within the ( …. ) and the block ends with “endmodule” statement
 
  Inside the block there is description of each I/O whether they are inputs or outputs. Functionality of adder is also given with the always statement. .

Posted in VERILOG | Tagged: , , | Leave a Comment »

What is the difference between a function and a task?

Posted by Nityanand Dubey on July 6, 2008

  1. behavior

Function : function call happens in real time OR no simulation delay can be inserted during the function call

Task : Tasks can be inserted with a delay

  1. No of outputs :

Function : A function can have at least one input arg to be passed, and also it can have only one output to drive

Task : Task can have any no of inputs and outputs.

  1. Nesting :

Function : A function can call a function inside it but not a task

Task : Task can call either a function or a task inside it.

  1. Synthesis :

Function : A function can be synthesized

Task : Tasks are not synthesizable

  1. Limitations :

Function : A function does not allow any delay, timing, event inside it

Task : Tasks can have delays, events inside it

  1. Usage :

Function : A can be used for RTL as well as behavioral coding ( mostly for combinational logic)

Task : Tasks can be used for behavioral modeling only.

Posted in VERILOG | Tagged: , , , , | Leave a Comment »

What is the difference between blocking and nonblocking statements in verilog

Posted by Nityanand Dubey on July 6, 2008

In verilog, we have two types of assignment operators i.e. blocking (=) and non- blocking (<=). These two have theirs special usages – Here are the differences

1. behavior  –

The blocking statements are as similar as any sequential programming language. In short, they execute sequentially.

The non-blocking statements are executed concurrently; it means if five statements are written together then it would depend on the simulator to execute which statement first. Ideally all the statements should execute at the same time.

  1. Synthesis –

The blocking statements infer simple “connection OR wire” during the synthesis,

Non-blocking statements infer Flop/latch.

  1. Usage

The blocking statements are used normally for combinational logic implementation OR whenever the synchronization/sequence required between the assignments,

Non blocking statements are used for sequential implementation

Posted in VERILOG | Tagged: , , , , , | Leave a Comment »