SystemVerilog Sequence Generation with Loops & Arrays

Welcome to another System Verilog tutorial on VLSI 360! Today, we’re tackling a classic coding problem: Generate a below sequence using loops and array. (a)0102030405 (b)1122334455 without hardcoding the values. In this blog, we give you the full answer to make two 10-element sequences in SystemVerilog. This will help you do well in interviews or hardware testing jobs.

Generate a below sequence using loops and array

Question: Generate a below sequence using loops and array {Note: should not assign direct any value related to any sequence array}. (a)0102030405 (b)1122334455 

Understanding the Problem

We need to make two sequences, each with 10 elements:
  • First sequence: It switches between a fixed number and a number that goes up.
  • Second sequence: It repeats each number two times.

What we must do:

  • Use two arrays, each with 10 spaces, to save the sequences.
  • Use loops to make the sequences, not writing values like 1, 2, or 0 directly (ex., no direct numbers in array).
  • Show the arrays to check if the answer is right.

Approach

Here’s how we solve it:
  • Make two arrays with 10 spaces each to save the sequences.
  • Use a loop and a counter to make the numbers, so we don’t write them directly.
  • Put values in the arrays based on the pattern:
    First sequence: Switch between a fixed number (0) and the counter.
  • Second sequence: Use the counter two times for each pair.
  • Show the arrays to see if we did it right.
This way, we use loops to make the sequences, not writing the numbers directly.

Solution

Here is the SystemVerilog code to make the two sequences:

    module tb;
        int arr[10]; // Array for first sequence
        int arrb[10]; // Array for second sequence
        int i, j;

        initial begin
        j = 1; // Counter starts at 1

        // Loop to make both sequences
        for (i = 0; i < 10; i += 2) begin
            arr[i] = 0; // Put 0 in even places for first sequence
            arr[i+1] = j; // Put counter in odd places for first sequence
            arrb[i] = j; // Put counter in even places for second sequence
            arrb[i+1] = j; // Put counter in odd places for second sequence
            j++; // Add 1 to counter
        end

            // Show the arrays
            $display("First sequence: %p", arr);
            $display("Second sequence: %p", arrb);
        end
    endmodule

Explanation

Array Setup

  • arr[10]: Saves the first sequence, which switches between numbers.
  • arrb[10]: Saves the second sequence, which repeats numbers.
  • Both arrays have 10 spaces.
Variables
  • i: Used in the loop to move through the arrays, jumping by 2 each time.
  • j: A counter that starts at 1 and goes up to make the numbers.

Loop Work

The for loop starts at i = 0, goes to i < 10, and jumps by 2 (i += 2):
  • First Sequence (arr):
    • Even places (arr[i]) get 0 to make the switching pattern.
    • Odd places (arr[i+1]) get j, which goes from 1 to 5.
  • Second Sequence (arrb):
    • Both arrb[i] and arrb[i+1] get j, so the number repeats (ex., same number two times).
  • j goes up by 1 each time to give the next number.

Output

When we run the code, we see:
First sequence: '{0, 1, 0, 2, 0, 3, 0, 4, 0, 5}
Second sequence: '{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}
The output shows the right patterns: one switches, and the other repeats.

Post a Comment

0 Comments