0% found this document useful (0 votes)
2 views

_Mastering Control Flow in System Verilog With Examples

The document outlines various programming statements including selection, loop, and jump statements, along with their definitions and real-life scenarios. It provides examples of unique-if, priority-if, while, do-while, foreach, repeat, and forever loops, as well as break and continue jump statements. Each section includes code snippets demonstrating the functionality of these statements in a programming context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

_Mastering Control Flow in System Verilog With Examples

The document outlines various programming statements including selection, loop, and jump statements, along with their definitions and real-life scenarios. It provides examples of unique-if, priority-if, while, do-while, foreach, repeat, and forever loops, as well as break and continue jump statements. Each section includes code snippets demonstrating the functionality of these statements in a programming context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Selection, Loop & Jump Statements

Selection Statements Loop Statements Jump Statements


Unique-If while break
Priority-If do-while continue
foreach
enhanced for loop
repeat
forever

LinkedIn:- Gowtham Seela


Selection Statements
Unique-If
• unique- if is used to ensure that only one of the conditions is true.
• It helps the simulator to check for overlapping conditions. Code:
Code: module unique_if_example;
module unique_if_example; int a = 3, b = 1, result;
int a = 2, b = 2, result; initial begin
initial begin unique if (a == 1) begin
unique if (a == 1) begin result = 1;
result = 1; end else if (a == 2 && b == 1) begin
end else if (a == 2) begin result = 2;
result = 2; end else if (a == 3 || b == 2) begin
end else begin result = 3;
result = 3; end else begin
end result = 4;
$display("Result: %0d", result); end
end $display("Result: %0d", result);
endmodule end
endmodule
Output:
Output:
Result: 2 Result: 3
Selection Statements
Priority-If
• `priority` if is similar to `unique`, but it indicates that the conditions should be checked in order, and at
least one of them should be true. Code:
Code: module priority_if_example;
module priority_if_example; int a = 4, b = 2, result;
int a = 3, b = 3, result; initial begin
initial begin priority if (a == 1) begin
priority if (a == 1) begin result = 1;
result = 1; end else if (a == 2 || b == 2) begin
end else if (a == 2) begin result = 2;
result = 2; end else if (a == 3 && b == 2) begin
end else if (a == 3) begin result = 3;
result = 3; end else begin
end else begin result = 4;
result = 4; end
end $display("Result: %0d", result);
$display("Result: %0d", result); end
end endmodule
endmodule
Output:
Output: Result: 2
Result: 3
Loop Statements
while
• Executes as long as the condition is true. Code:
module while_example;
Code: int i = 0;
module while_example; initial begin
int i = 0; while (i < 10) begin
initial begin if (i % 2 == 0) begin
while (i < 5) begin $display("Even: %0d", i);
$display("i: %0d", i); end else begin
i++; $display("Odd: %0d", i);
end end Output:
end i++; Even: 0
endmodule end Odd: 1
end Even: 2
endmodule Odd: 3
Output: Even: 4
i: 0 Odd: 5
i: 1 Even: 6
i: 2 Odd: 7
i: 3 Even: 8
i: 4 LinkedIn:- Gowtham Seela Odd: 9
Loop Statements
Do-while
• Executes at least once and then continues as long as the condition is true.

Code:
Code: Output: module do_while_example;
module do_while_example; Square of 0: 0 int i = 0;
int i = 0; Square of 1: 1 initial begin
initial begin Square of 2: 4 do begin
do begin Square of 3: 9 $display("i: %0d", i);
$display("Square of %0d: Square of 4: 16 i++;
%0d", i, i*i); Square of 5: 25 end while (i < 5);
i++; Square of 6: 36 end
end while (i < 10); Square of 7: 49 Endmodule
end Square of 8: 64
endmodule Square of 9: 81 Output:
i: 0
i: 1
i: 2
i: 3
i: 4
Loop Statements
foreach
• Iterates over each element in an array. Code:
module foreach_example;
Code: int matrix[2][3] = '{{0, 1, 2}, {3, 4, 5}};
module foreach_example; initial begin
int arr[5] = {0, 1, 2, 3, 4}; foreach (matrix[i, j]) begin
initial begin $display("matrix[%0d][%0d]: %0d", i, j, matrix[i][j]);
foreach (arr[i]) begin end
$display("arr[%0d]: %0d", i, end
arr[i]); endmodule
end
end Output:
Endmodule matrix[0][0]: 0
matrix[0][1]: 1
Output: matrix[0][2]: 2
arr[0]: 0 matrix[1][0]: 3
arr[1]: 1 matrix[1][1]: 4
arr[2]: 2 matrix[1][2]: 5
arr[3]: 3
arr[4]: 4
LinkedIn:- Gowtham Seela
Loop Statements
for
• declaration of a loop variable within the for loop
• one or more initial declaration or assignment within the for loop
• one or more step assignment or modifier within the for loop
Code: Code:
module for_loop_example; module for_loop_example;
int arr[5] = {10, 20, 30, 40, 50}; int matrix[3][3] = '{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
initial begin int sum;
for (int i = 0; i < 5; i++) begin initial begin Output:
$display("arr[%0d]: %0d", i, arr[i]); sum = 0; matrix[0][0]: 1
end for (int i = 0; i < 3; i++) begin matrix[0][1]: 2
end for (int j = 0; j < 3; j++) begin matrix[0][2]: 3
endmodule sum += matrix[i][j]; matrix[1][0]: 4
$display("matrix[%0d][%0d]: %0d", i, j, matrix[i][j]); matrix[1][1]: 5
Output: end matrix[1][2]: 6
arr[0]: 10 end matrix[2][0]: 7
arr[1]: 20 $display("Total Sum: %0d", sum); matrix[2][1]: 8
arr[2]: 30 end matrix[2][2]: 9
arr[3]: 40 endmodule
arr[4]: 50
Loop Statements
repeat
Code
• Executes a block of code a fixed number of times.
module repeat_example;
Code: bit signal;
module repeat_example; initial begin
initial begin signal = 0;
int i = 0; repeat (5) begin
repeat (5) begin signal = ~signal;
$display("Iteration: %0d", i); $display("Signal: %b", signal);
i++; #5; // delay for simulation purposes
end end
end end
endmodule endmodule

Output: Output:
Iteration: 0 Signal: 1
Iteration: 1 Signal: 0
Iteration: 2 Signal: 1
Iteration: 3 Signal: 0
Iteration: 4 Signal: 1
Loop Statements
forever
• Executes a block of code indefinitely.

Code:
module forever_example;
int i = 0; Output:
initial begin i: 0
forever begin i: 1
$display("i: %0d", i); i: 2
i++; i: 3
if (i == 5) break; i: 4
end
end
endmodule

LinkedIn:- Gowtham Seela


Jump Statements
Code:
Break module break_example;
• Exits from the current loop initial begin
for (int i = 0; i < 5; i++) begin
Code:
for (int j = 0; j < 5; j++) begin
module break_example;
if (j == 2) break;
initial begin
$display("i: %0d, j: %0d", i, j);
for (int i = 0; i < 10; i++) begin
end
if (i == 5) break;
end
$display("i: %0d", i);
end
end
endmodule
end
Output:
endmodule
i: 0, j: 0
i: 0, j: 1
Output:
i: 1, j: 0
i: 0
i: 1, j: 1
i: 1
i: 2, j: 0
i: 2
i: 2, j: 1
i: 3
i: 3, j: 0
i: 4
i: 3, j: 1
i: 4, j: 0
i: 4, j: 1
Jump Statements
continue Output:
• Skips the rest of the current loop iteration and jumps to the next iteration. i: 0, j: 0
i: 0, j: 1
Code:
Code: i: 0, j: 3
module continue_example;
module continue_example; i: 0, j: 4
initial begin
initial begin i: 1, j: 0
for (int i = 0; i < 10; i++) begin
for (int i = 0; i < 5; i++) begin i: 1, j: 1
if (i % 2 == 0) continue;
for (int j = 0; j < 5; j++) begin i: 1, j: 3
$display("i: %0d", i);
if (j == 2) continue; i: 1, j: 4
end
$display("i: %0d, j: %0d", i, j); i: 2, j: 0
end
end i: 2, j: 1
endmodule
end i: 2, j: 3
Output:
end i: 2, j: 4
i: 1
endmodule i: 3, j: 0
i: 3
i: 3, j: 1
i: 5
i: 3, j: 3
i: 7
i: 3, j: 4
i: 9
i: 4, j: 0
i: 4, j: 1
i: 4, j: 3
i: 4, j: 4
Definitions & Real Life Scenarios

Statement Type Statement Definition Real-Life Scenario

Ensures only one of the Selecting a unique payment


Selection unique if conditions can be true; method from multiple
checks for overlaps. options in a system.

Conditions are checked in


Prioritizing customer service
Selection priority if order; ensures at least one
requests based on severity.
condition is true.

LinkedIn:- Gowtham Seela


Statement Type Statement Definition Real-Life Scenario

Continuously checking stock levels


Loop while Executes as long as the condition is true.
until a specific product is available.

Executes at least once and then continues Sending a message until an


Loop do-while
as long as the condition is true. acknowledgment is received.

Processing each item in a shopping


Loop foreach Iterates over each element in an array.
cart.

A loop that iterates over an array or


Sending a notification to each user
Loop enhanced for loop collection, often used with dynamic
in a list.
arrays.

Running a promotional
Executes a block of code a fixed number of
Loop repeat advertisement a set number of
times.
times.

Continuously monitoring a security


Loop forever Executes a block of code indefinitely.
system.
Statement Type Statement Definition Real-Life Scenario

Stopping a search operation once the target


Jump break Exits from the current loop immediately.
item is found.

Skips the rest of the current loop iteration Skipping over an invalid entry in a list and
Jump continue
and jumps to the next iteration. continuing with the rest.

LinkedIn:- Gowtham Seela


LinkedIn:- Gowtham Seela

UNIQUE IF PRIORITY IF

Scenario: Choosing a unique payment method. Scenario: Prioritizing customer service requests.

Code Code:
module unique_if_example; module priority_if_example;
typedef enum {CASH, CARD, ONLINE} PaymentMethod; int severity = 2;
PaymentMethod method = CARD; initial begin
initial begin priority if (severity == 1) begin
unique if (method == CASH) begin $display("Handle critical issue.");
$display("Payment by cash."); end else if (severity == 2) begin
end else if (method == CARD) begin $display("Handle major issue.");
$display("Payment by card."); end else if (severity == 3) begin
end else if (method == ONLINE) begin $display("Handle minor issue.");
$display("Payment online."); end
end end
end endmodule
endmodule
Output:-
Output:- Handle major issue.
Payment by card
DO-WHILE
WHILE
Scenario: Checking stock levels.
Scenario: Sending a message until acknowledgment.

Code Code
module while_example; module do_while_example;
int stock = 10; bit ack_received = 0;
initial begin int attempts = 0;
while (stock > 0) begin initial begin
$display("Stock available: %0d", stock);
do begin
stock--;
end
$display("Sending message, attempt %0d", attempts);
end attempts++;
endmodule if (attempts == 3) ack_received = 1; // Simulating an
Output:- acknowledgment
Stock available: 10 end while (!ack_received);
Stock available: 9 $display("Acknowledgment received.");
Stock available: 8 end
Stock available: 7
Endmodule
Stock available: 6
Stock available: 5
Stock available: 4 Output:-
Stock available: 3 Sending message, attempt 0
Stock available: 2 Sending message, attempt 1
Stock available: 1 Sending message, attempt 2
LinkedIn:- Gowtham Seela
FOREACH FOR LOOP

Scenario: Processing items in a shopping cart. Scenario: Sending notifications to users.

Code: Code
module foreach_example; module enhanced_for_example;
int cart[5] = {100, 200, 300, 400, 500}; int users[] = {101, 102, 103, 104, 105};
initial begin initial begin
foreach (cart[i]) begin for (int i = 0; i < users.size(); i++) begin
$display("Processing item: %0d", cart[i]); $display("Sending notification to user: %0d", users[i]);
end end
end end
endmodule endmodule

Output:- Output:-
Processing item: 100 Sending notification to user: 101
Processing item: 200 Sending notification to user: 102
Processing item: 300 Sending notification to user: 103
Processing item: 400 Sending notification to user: 104
Processing item: 500 Sending notification to user: 105
FOREVER
REPEAT
Scenario: Continuously monitoring a security system.
Scenario: Running an advertisement a set number of times.
Code:
Code: module forever_example;
module repeat_example; bit alert = 0;
initial begin initial begin
repeat (5) begin forever begin
$display("Displaying advertisement."); #5; // Check interval
#10; // Wait time between advertisements $display("Monitoring security system...");
end if (alert) begin
end $display("Alert triggered!");
endmodule break; // Exit the loop if alert is triggered
end
Output: end
Displaying advertisement. end
Displaying advertisement. endmodule
Displaying advertisement. Output:
Displaying advertisement. Monitoring security system......
Displaying advertisement. .
.
.
LinkedIn:- Gowtham Seela Monitoring security system...
LinkedIn:- Gowtham Seela
CONTINUE
BREAK
Scenario: Skipping invalid entries in a list.
Scenario: Stopping a search operation.
Code
Code
module continue_example;
module break_example;
int entries[10] = '{1, -1, 2, -2, 3, -3, 4, -4, 5, -5};
int items[10] = '{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
initial begin
int target = 5;
for (int i = 0; i < 10; i++) begin
initial begin
if (entries[i] < 0) continue;
for (int i = 0; i < 10; i++) begin
$display("Valid entry: %0d", entries[i]);
if (items[i] == target) begin
end
$display("Item found at index: %0d", i);
end
break;
endmodule
end
end
Output:-
end
Valid entry: 1
endmodule
Valid entry: 2
Valid entry: 3
Output:-
Valid entry: 4
Item found at index: 5
Valid entry: 5
EDA Playground:-
https://www.edaplayground.com/x/afLX

You might also like