Memory initialization with $readmemh in Verilog - Time & Space Complexity
When we use $readmemh in Verilog, it loads data into memory from a file. We want to understand how the time it takes grows as the memory size increases.
How does the loading time change when the memory gets bigger?
Analyze the time complexity of the following code snippet.
module memory_init;
reg [7:0] mem [0:255];
initial begin
$readmemh("data.hex", mem);
end
endmodule
This code loads 256 bytes of data from a hex file into a memory array at the start of simulation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each memory location from the file and storing it.
- How many times: Once for each memory element (256 times in this example).
Loading time grows as we add more memory locations to fill.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads and stores |
| 100 | 100 reads and stores |
| 1000 | 1000 reads and stores |
Pattern observation: The operations increase directly with the number of memory locations.
Time Complexity: O(n)
This means the time to load memory grows in a straight line as the memory size increases.
[X] Wrong: "The loading time is constant no matter how big the memory is."
[OK] Correct: Each memory element must be read and stored, so more memory means more work and more time.
Understanding how memory initialization scales helps you reason about hardware simulation and design efficiency. It shows you how data size impacts setup time, a useful skill in hardware programming.
"What if we used $readmemb instead of $readmemh? How would the time complexity change?"
Practice
$readmemh system task in Verilog?Solution
Step 1: Understand the function of
$readmemh$readmemhis used to read hexadecimal data from a file and load it into a memory array in Verilog.Step 2: Compare options with the function
To load hexadecimal data from a file into a memory array before simulation. correctly describes loading hex data into memory before simulation. Other options describe writing data, converting formats, or random initialization, which are incorrect.Final Answer:
To load hexadecimal data from a file into a memory array before simulation. -> Option DQuick Check:
$readmemhloads hex data [OK]
- Confusing $readmemh with $writememh
- Thinking it initializes registers randomly
- Assuming it converts data formats
mem using $readmemh with a file named data.hex?Solution
Step 1: Recall the correct order of arguments in
The syntax is$readmemh$readmemh(filename, memory_array);where filename is a string and memory_array is the target memory.Step 2: Match options to syntax
$readmemh("data.hex", mem); matches the correct syntax. $readmemh(mem, "data.hex"); reverses arguments. readmemh("data.hex", mem); misses the $ sign. $readmemh("mem", data.hex); swaps arguments and uses incorrect quotes.Final Answer:
$readmemh("data.hex", mem); -> Option BQuick Check:
Syntax is $readmemh(filename, memory) [OK]
- Swapping filename and memory arguments
- Omitting the $ sign
- Using incorrect quotes or missing parentheses
memfile.hex containing:0A 1B 2C 3D
What will be the value of
memory[2] after initial block executes?reg [7:0] memory [0:3];
initial begin
$readmemh("memfile.hex", memory);
endSolution
Step 1: Understand memory indexing and file data
The file has 4 hex values: 0A at index 0, 1B at index 1, 2C at index 2, 3D at index 3.Step 2: Identify value at memory[2]
memory[2] will hold the third value, which is 2C in hex.Final Answer:
8'h2C -> Option AQuick Check:
memory[2] = 2C hex [OK]
- Confusing index with line number (off by one)
- Misreading hex values
- Assuming decimal instead of hex
reg [7:0] mem [0:3];
initial begin
$readmemh("data.hex", mem, 1, 3);
endIf
data.hex contains:AA BB CC DD EE
What values will
mem[0] and mem[1] hold after initialization?Solution
Step 1: Understand $readmemh with start and end addresses
The call$readmemh("data.hex", mem, 1, 3);loads file lines starting at mem[1] up to mem[3].Step 2: Map file lines to memory
File lines: 0=AA, 1=BB, 2=CC, 3=DD, 4=EE.
mem[1] = AA, mem[2] = BB, mem[3] = CC.
mem[0] remains uninitialized (default 8'h00).Final Answer:
mem[0] = 8'h00, mem[1] = 8'hAA -> Option AQuick Check:
Start=1 loads from line 0 to mem[1] [OK]
- Assuming mem[0] is loaded when start address is 1
- Confusing file line numbers with memory indices
- Ignoring default zero initialization
mem with 8-bit width using $readmemh. The hex file init.hex contains 20 lines of data. Which approach correctly loads only the first 16 lines into mem without causing simulation errors?Solution
Step 1: Understand memory size and file length
The memory has 16 words (indices 0 to 15). The file has 20 lines, more than memory size.Step 2: Use $readmemh with start and end addresses
Using$readmemh("init.hex", mem, 0, 15);loads only the first 16 lines safely into memory without overflow.Step 3: Evaluate other options
Use$readmemh("init.hex", mem, 0, 19);and ignore extra lines automatically. tries to load 20 lines into 16 words, causing out-of-bounds error.
Use$readmemh("init.hex", mem, 0, 19);and truncate memory size to 16. tries to load 20 lines into 16 words, causing overflow.
Manually edit the file to 16 lines before loading. is manual and not always practical.Final Answer:
Use $readmemh("init.hex", mem, 0, 15); -> Option CQuick Check:
Limit loading to memory size with start/end [OK]
- Loading more lines than memory size causing errors
- Assuming $readmemh ignores extra lines automatically
- Editing files manually instead of using range parameters
Ready to prove it?
Pass the test and this pattern moves unknown
