Welcome to VLSI 360, your trusted source for VLSI design, verification, and SystemVerilog tutorials! Today, we’re addressing a common VLSI verification interview question: "Write a SystemVerilog code to generate the pattern 9, 99, 999, 9999, 99999, ...." This pattern involves generating an infinite sequence of numbers where each number consists of an increasing number of 9s (e.g., 9, 99, 999, and so on). In this guide, we’ll provide two methods to tackle this problem: one using a dynamic array to store a portion of the sequence and another using a forever loop to generate the sequence indefinitely. Let’s dive in!
Question: Write a SystemVerilog code to generate the pattern 9, 99, 999, 9999, 99999, ...
Understanding the Problem:
The task is to generate the infinite sequence 9, 99, 999, 9999, 99999, ... using SystemVerilog. The pattern can be described as:
- Pattern: 9 (1 digit), 99 (2 digits), 999 (3 digits), 9999 (4 digits), 99999 (5 digits), and so on, continuing indefinitely.
- Requirement: Generate this sequence in SystemVerilog, either by storing a portion of it or printing it iteratively.
Mathematically, each number in the sequence can be expressed as (10^i - 1), where i is the number of digits (starting from 1).
For example,
9 = (10^1) - 1,
99 = (10^2) - 1,
999 = (10^3) - 1, and this pattern continues infinitely.
Since we can’t store or display an infinite sequence in practice, we’ll show two approaches:
1. Generates a finite portion of the sequence (with the potential to scale)
2. Generates the sequence indefinitely during simulation.
Method 1: Using a Dynamic Array to Store a Portion of the Sequence
This method uses a dynamic array (or queue) to store a portion of the sequence, which can be scaled to generate as many numbers as needed. Here, we’ll generate the first 10 numbers as an example, but you can adjust the limit to generate more.
module pattern_gen;
int pattern_queue[$]; // Dynamic array (queue) to store the sequence
initial begin
for (int i = 1; i <= 10; i++) begin // Generate first 10 numbers
pattern_queue.push_back(10**i - 1);
end
$display("Generated Pattern (Method 1, first 10 numbers): %p", pattern_queue);
end
endmodule
Explanation of Method 1:
We use a queue pattern_queue (denoted by [$]) to dynamically store the sequence.
The for loop runs from i = 1 to i = 10, computing each value as (10**i - 1) (e.g., 10^2 - 1 = 99).
Each computed value is pushed into the queue using push_back.
We display the first 10 numbers as an example, but you can increase the loop limit to generate more numbers.
The output will be:
Generated Pattern (Method 1, first 10 numbers): '{9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, 9999999999}
Note on Infinity: While the sequence is infinite, this method generates a finite portion based on the loop limit. You can adjust the limit (e.g., i <= 100) to generate more numbers, constrained only by memory and integer size limits.
Method 2: Using a Forever Loop to Generate the Sequence Indefinitely
This method uses a forever loop to dynamically generate and print the sequence indefinitely, perfectly matching the infinite nature of the pattern. It starts with 9 and repeatedly multiplies by 10 and adds 9 (e.g., 9 → 99 → 999).
module pattern_generator;
bit[1000:0] num = 9;
initial begin
forever begin
$display("%0d", num);
num = (num * 10) + 9;
#10; // Delay for better readability in simulation
end
end
initial begin
#100 $finish; // Stop after a short time to limit output for this example
end
endmodule
Explanation of Method 2:
- We declare a large bit vector num (1000 bits wide) initialized to 9 to handle large numbers.
- The forever loop:Prints the current value of num.
- Updates num by multiplying by 10 and adding 9 (e.g., 9 → 99, 99 → 999).
- Adds a #10 delay for simulation readability.
- A separate initial block stops the simulation after #100 time units to limit the output for this example (otherwise, it would run indefinitely).
- The output will include:
9
99
999
9999
99999
...
(The sequence continues until the simulation stops.)
Alignment with Infinite Sequence: This method naturally fits the infinite sequence requirement since the forever loop will continue generating numbers until the simulation is manually stopped. You can adjust the #100 $finish to let it run longer or add a counter to stop after a specific number of iterations.
Comparing the Two Methods
- Method 1 (Dynamic Array): Suitable for storing a portion of the sequence in memory for later use. It’s practical for finite segments of the infinite sequence and can be scaled by adjusting the loop limit.
- Method 2 (Forever Loop): Ideal for generating the infinite sequence in real-time during simulation. It doesn’t store the values (unless modified to do so) but continuously produces the pattern, aligning with the problem’s infinite nature.
Why These Solutions Work
- Method 1 uses the mathematical formula (10^i - 1) to compute each number, making it efficient for generating a specific number of terms. The use of a dynamic array allows flexibility in how many numbers you generate.
- Method 2 builds the pattern iteratively, starting from 9 and adding a digit of 9 each time. The forever loop ensures the sequence can continue indefinitely, limited only by simulation time or integer overflow.
Common Interview Follow-Up Questions
To prepare for VLSI verification interviews, consider these related questions:
- How would you modify the code to generate a pattern like 1, 11, 111, 1111, ...? (Hint: For Method 1, use (10**i - 1)/9; for Method 2, start with 1 and add 1 instead of 9.)
- Can you generate the same pattern in reverse order (e.g., starting from a large number of 9s and reducing digits)?
- How would you verify the generated pattern using assertions?
For Method 2, you could add a counter to stop after a specific number of iterations (e.g., 10 numbers) instead of using #100 $finish.
0 Comments