Mobile
Verilog Online Help Prev Page Prev Page
Table of Contents
Bit-select
Block Statements
Built-in Primitives
Case Statement
Continuous Assignments
Conversion Functions
Comments
Compiler Directives
Concatenations
Conditional Operator
Delays
Disable Statement
Display Tasks
Edge Sensitive Path
Expression Bit Length
File I/O Functions
Functions
Identifiers
If Statement
Integer Constants
Intra-assignment Timing Controls
Keywords
Loop Statements
Memories
min:typ:max Delays
Module Declaration
Module Instantiation
Module Path Declaration
Module Path Polarity
Net Data Types
Operators
Parameters
Part-select
PLA Modeling Tasks
Probabilistic Distribution Functions
Procedural Assignments
Procedural Continuous Assignments
Procedural Timing Control
Range Specification
Real Constants
Register Data Types
Simulation Control Tasks
Simulation Time Functions
Specify Block
State Dependent Path
Stochastic Analysis Tasks
Strengths
Strings
Structured Procedures
Tasks
Timescale System Tasks
Timing Check Tasks
UDP Declaration
UDP Instantiation
UDP State Table
Value Change Dump (VCD) File
Vectors

Loop Statements

Formal Definition

Loop statements provide a means of modeling blocks of procedural statements.

Simplified Syntax

forever statement;

repeat (expression) statement;

while (expression) statement;

for (assignment; expression; assignment) statement;

Description

There are four types of loop statements: forever, repeat, while, and for statements.

The forever instruction (Example 1) continuously repeats the statement that follows it. Therefore, it should be used with procedural timing controls (otherwise it hangs the simulation).

The repeat instruction (Example 2) executes a given statement a fixed number of times. The number of executions is set by the expression, which follows the repeat keyword. If the expression evaluates to unknown, high-impedance, or a zero value, then no statement will be executed.

The while instruction (Example 3) executes a given statement until the expression is true. If a while statement starts with a false value, then no statement will be executed.

The for instruction (Example 4) executes a given statement until the expression is true. At the initial step, the first assignment will be executed. At the second step, the expression will be evaluated. If the expression evaluates to an unknown, high-impedance, or zero value, then the for statement will be terminated. Otherwise, the statement and second assignment will be executed. After that, the second step is repeated.

 

Examples

Example 1

always begin
  counter = 0;
  forever #10 counter = counter + 1;
end

Example 2

initial begin
  repeat (10) a = a + ~b;
end

Example 3

module test;
parameter MSB = 8;
reg [MSB-1:0] Vector;
integer t;
initial

 
begin
  t = 0;
  while (t < MSB)
    begin
      //Initializes vector elelments
      Vector[t] = 1'b0;
      t = t + 1;
    end
end
endmodule

 

Example 4

initial begin
  for (index=0; index < 10; index = index + 2)
  mem[index] = index;
end

 

Important Notes

  • The forever statement should contain at least one procedural timing control.

 

Powered by IXwebhosting