_Mastering Control Flow in System Verilog With Examples
_Mastering Control Flow in System Verilog With Examples
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
Running a promotional
Executes a block of code a fixed number of
Loop repeat advertisement a set number of
times.
times.
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.
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
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