Skip to content
asicguru.tech@gmail.com
REGISTER LOGIN
AsicGuru Ventures
  • All Courses
  • CONTACT
  • GALLERY
  • HOME
  • Tutorials

0

Verilog Tutorial

17
  • Verilog Introduction
  • Lexical Tokens
  • Gate-Level Modelling
  • Data Types
  • Operators
  • Operands
  • Modules
  • Behavioral Modeling
  • Timing Controls
  • Procedures: Always and Initial Blocks
  • Functions
  • Tasks
  • Component Inference
  • Finite State Machines.
  • Compiler Directives
  • System Tasks and Functions
  • Test Benches

System Verilog

23
  • System Verilog Introduction
  • Data Types
  • Type Casting
  • Arrays
  • Associative Arrays
  • Dynamic Arrays
  • Queue
  • Operators
  • Procedural statements and Control flow
  • Classes
  • Inheritance
  • Encapsulation
  • This and Super operator
  • In-Line Constraints
  • Class Constraints
  • Virtual Classes
  • Parameterized Classes
  • Classes Summary
  • Singleton Classes
  • Functional Coverage Introduction
  • Covergroup
  • Tools
  • Books

Scripting

15
  • Introduction
  • Perl Tutorial
  • What is Perl
  • Perl: Syntax And Variable
  • Perl Strings
  • Perl Arrays
  • Perl Associative Arrays
  • If/While Syntax
  • File Input
  • Print Output
  • String Processing with Regular Expressions
  • Subroutines
  • Running External Programs
  • References
  • Terse Perl

Makefile

1
  • Makefile Tutorial
View Categories
  • Home
  • Tutorials
  • System Verilog
  • Procedural statements and Control flow

Procedural statements and Control flow

4 min read

#

A procedural statement can be added in system verilog using :

  1. initial // enable this statement at the beginning of simulation and execute it only once
  2. final // do this statement once at the end of simulation
  3. always, always_comb, always_latch, always_ff // loop forever
  4. task // do these statements whenever the task is called
  5. function // do these statements whenever the function is called and return a value

SystemVerilog has the following types of control flow within a process:
— Selection, loops, and jumps
— Task and function calls
— Sequential and parallel blocks
— Timing control

Blocking and Non Blocking Statement : #

Following type of statement is allowed in both verilog and system verilog.

view source

print?

1.#1 r = a;

2.r = #1 a;

3.r <= #1 a;

4.r <= a;

5.@c r = a;

6. 

7.r = @c a;

8.r <= @c a;

SystemVerilog also allows a time unit to be specified in the assignment statement, as follows:

view source

print?

1.#1ns r = a;

2. 

3.r = #1ns a;

4. 

5.r <= #1ns a;

* Its illegal to make non blocking assignments to automatic variables

* If size of the left hand size is smaller than right hand size information will be lost

Selection Statements : #

1. IF ELSE statement :

view source

print?

1.if ((a==0) || (a==1))

2.$display("0 or 1");

3.else if (a == 2)

4.$display("2");<br>

5.else if (a == 4)

6.$display("4");

2. Case Statement

example:

view source

print?

1.bit [2:0] a;

2. 

3.unique case(a) // values 3,5,6,7 cause a warning

4.0,1: $display("0 or 1");

5.2: $display("2");

6.4: $display("4");

7.endcase

view source

print?

1.priority casez(a)

2.// values 4,5,6,7 cause a warning

3.3’b00?: $display("0 or 1");

4.3’b0??: $display("2 or 3");

5.endcase

SystemVerilog adds the keywords unique and priority, which can be used before an if. If either keyword
is used, it shall be a warning for no condition to match unless there is an explicit else.

For example:

view source

print?

01.unique if ((a==0) || (a==1))

02.$display("0 or 1");

03.else if (a == 2)

04.$display("2");

05.else if (a == 4)

06.$display("4");

07.// values 3,5,6,7 cause a warning

08. 

09.// Priority

10. 

11.priority if (a[2:1]==0)

12.$display("0 or 1");

13.else if (a[2] == 0)

14.$display("2 or 3");<br>

15.else

16.$display("4 to 7");

17.//covers all other possible values, so no warning

Loops : #

1. do…while loop

view source

print?

1.do

2.statement

3.while(condition) // as C

The condition can be any expression that can be treated as a boolean. It is evaluated after the statement

2. For Loop

SystemVerilog adds the ability to declare the for loop control variable within the for loop. This creates a local variable within the loop. Other parallel loops cannot inadvertently affect the loop control variable. For example:

view source

print?

1.initial

2.begin

3.for (int i = 0; i <= 255; i++)

4.$display("I = %d \n", i);

5.end

The local variable declared within a for loop is equivalent to declaring an automatic variable in an unnamed block.

3. Foreach loop

The foreach construct specifies iteration over the elements of an array. Its argument is an identifier that
designates any type of array (fixed-size, dynamic, or associative) followed by a list of loop variables
enclosed in square brackets. Each loop variable corresponds to one of the dimensions of the array. The
foreach construct is similar to a repeat loop that uses the array bounds to specify the repeat count instead of an expression.
Examples:

view source

print?

1.string words [2] = '{ "hello", "world" };

2.int prod [1:8] [1:3];

3.foreach( words [ j ] )

4.$display( j , words[j] );

5.// print each index and value

6. 

7.foreach( prod[ k, m ] )

8.prod[k][m] = k * m; // initialize

Jump Statement : #

SystemVerilog adds the C jump statements break, continue, and return.
break                    // out of loop as C
continue               // skip to end of loop as C
return expression   // exit from a function
return                    // exit from a task or void function


The continue and break statements can only be used in a loop. The continue statement jumps to the end of the loop and executes the loop control if present. The break statement jumps out of the loop. The continue and break statements cannot be used inside a fork…join block to control a loop outside the
fork…join block. The return statement can only be used in a task or function. In a function returning a value, the return must have an expression of the correct type.

Final Block : #

The final block is like an initial block, defining a procedural block of statements, except that it occurs
at the end of simulation time and executes without delays. A final block is typically used to display statistical  information about the simulation.

The only statements allowed inside a final block are those permitted inside a function declaration. This
guarantees that they execute within a single simulation cycle. Unlike an initial block, the final block
does not execute as a separate process; instead, it executes in zero time, the same as a function call.

A final block executes when simulation ends due to an explicit or implicit call to $finish.
final
  begin
    $display(“Number of cycles executed %d”,$time/period);
    $display(“Final PC = %h”,PC);
  end

Disable Statement : #

SystemVerilog has break and continue to break out of or continue the execution of loops. The Verilog
disable can also be used to break out of or continue a loop, but is more awkward than using break or
continue. The disable is also allowed to disable a named block, which does not contain the disable
statement. If the block is currently executing, this causes control to jump to the statement immediately after the block. If the block is a loop body, it acts like a continue. If the block is not currently executing, the disable has no effect.


SystemVerilog has return from a task, but disable is also supported. If disable is applied to a named
task, all current executions of the task are disabled.
module …
always always1: begin … t1: task1( ); … end
…
endmodule
always begin
…
disable u1.always1.t1; // exit task1, which was called from always1
(static)
end

Updated on May 4, 2025

What are your Feelings

  • Happy
  • Normal
  • Sad
Share This Article :
  • Facebook
  • X
  • LinkedIn
  • Pinterest
OperatorsClasses

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Table of Contents
    • Blocking and Non Blocking Statement :
    • Selection Statements :
    • Loops :
    • Jump Statement :
    • Final Block :
    • Disable Statement :

Archives

  • May 2025

Categories

  • Slider Post
  • Uncategorized

Copyright @ Asicguru Ventures by Misbah WP | Proudly powered by WordPress