Array declaration in Verilog - Time & Space Complexity
When we declare an array in Verilog, we want to know how the time to create it changes as the array size grows.
We ask: How does the work needed to set up the array grow with its size?
Analyze the time complexity of the following code snippet.
module array_example;
reg [7:0] my_array [0:15]; // Declare an array of 16 bytes
endmodule
This code declares a fixed-size array of 16 elements, each 8 bits wide.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Declaring storage space for each element in the array.
- How many times: Once per element, here 16 times.
As the array size grows, the amount of work to declare it grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 declarations |
| 100 | 100 declarations |
| 1000 | 1000 declarations |
Pattern observation: The work grows in a straight line as the array size increases.
Time Complexity: O(n)
This means the time to declare the array grows directly with the number of elements.
[X] Wrong: "Declaring an array is instant and does not depend on size."
[OK] Correct: Each element needs space and setup, so bigger arrays take more time to declare.
Understanding how array size affects declaration time helps you reason about hardware resource use and design efficiency.
"What if we declared a two-dimensional array instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand bit width and size syntax
In Verilog, the bit width is declared first as [bit_width-1:0], so for 16 bits it is [15:0].Step 2: Declare array size correctly
The array size is declared after the variable name as [0:size-1], so for 8 elements it is [0:7].Final Answer:
reg [15:0] my_array [0:7]; -> Option CQuick Check:
Bit width then array size = reg [15:0] my_array [0:7]; [OK]
- Swapping bit width and array size positions
- Using incorrect bit range like [0:7] for bit width
- Confusing array size with bit width
Solution
Step 1: Check array size declaration
Array size must be declared as [start_index:end_index] with start_index less than end_index, usually [0:size-1].Step 2: Identify invalid range
reg [7:0] data_array [15:0]; uses [15:0], which is reversed and invalid for array indexing.Final Answer:
reg [7:0] data_array [15:0]; -> Option DQuick Check:
Array index must be ascending range [OK]
- Using descending ranges for array size
- Confusing bit width range with array index range
- Forgetting semicolon at end
reg [3:0] arr [0:3]; and the assignments arr[0] = 4'b1010;, arr[1] = 4'b0101;, what is the value of arr[1]?Solution
Step 1: Understand array declaration and assignments
The array arr has 4 elements indexed 0 to 3, each 4 bits wide. arr[0] is assigned 1010, arr[1] is assigned 0101.Step 2: Identify value of arr[1]
arr[1] was explicitly assigned 4'b0101, so its value is 0101.Final Answer:
4'b0101 -> Option AQuick Check:
arr[1] = 4'b0101 [OK]
- Mixing up indices and values
- Assuming default zero without assignment
- Confusing bit width with array size
reg [7:0] mem_array [7:0];
Solution
Step 1: Analyze array size range
The array size is declared as [7:0], which is a descending range and invalid for array indexing.Step 2: Correct array size range
The correct array size should be ascending, [0:7], to declare 8 elements indexed 0 to 7.Final Answer:
Array size range should be [0:7], not [7:0] -> Option AQuick Check:
Array index must be ascending [OK]
- Using descending ranges for array size
- Confusing bit width with array size
- Assuming [7:0] is valid for arrays
Solution
Step 1: Understand 2D array declaration syntax
In Verilog, multi-dimensional arrays are declared by stacking sizes after the variable name. The first dimension is rows, second is columns.Step 2: Apply correct sizes and bit width
Each element is 8 bits wide, so bit width is [7:0]. Rows are 4 elements indexed 0 to 3, columns are 8 elements indexed 0 to 7.Final Answer:
reg [7:0] matrix [0:3][0:7]; -> Option BQuick Check:
Bit width then rows then columns [OK]
- Swapping row and column indices
- Incorrect bit width declaration
- Using reversed index ranges
Ready to prove it?
Pass the test and this pattern moves unknown
