0
0
Verilogprogramming~10 mins

Memory initialization with $readmemh in Verilog - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Memory initialization with $readmemh
Start Simulation
Invoke $readmemh
Open Hex File
Read Each Line
Convert Hex to Binary
Store Data in Memory Array
Memory Initialized
Continue Simulation
The simulation starts and calls $readmemh to open a hex file, read each line, convert it to binary, and store it in the memory array before continuing.
Execution Sample
Verilog
reg [7:0] mem [0:3];
initial begin
  $readmemh("data.hex", mem);
end
This code initializes a 4-byte memory array with values from a hex file named data.hex.
Execution Table
StepActionFile Line ReadHex ValueBinary StoredMemory Index
1Open file data.hex----
2Read line 10A0A00001010mem[0]
3Read line 21F1F00011111mem[1]
4Read line 3FFFF11111111mem[2]
5Read line 4000000000000mem[3]
6End of file reached----
7Memory initialization complete----
💡 All lines read from data.hex and stored in memory array mem[0..3]
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
mem[0]x0000101000001010000010100000101000001010
mem[1]xx00011111000111110001111100011111
mem[2]xxx111111111111111111111111
mem[3]xxxx0000000000000000
Key Moments - 3 Insights
Why does mem[0] get its value at step 2 and not before?
Because $readmemh reads the first line of the file at step 2 and stores it into mem[0], before that mem[0] is uninitialized (x).
What happens if the hex file has fewer lines than the memory size?
Only the memory locations corresponding to the lines read are initialized; others remain unchanged or undefined, as shown by the variable tracker.
Why is the binary stored shown as 8 bits for each hex value?
Because mem is declared as 8 bits wide, each hex value is converted to an 8-bit binary number before storing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the binary value stored in mem[2] after step 4?
A00011111
B11111111
C00000000
D00001010
💡 Hint
Check the 'Binary Stored' column at step 4 for mem[2]
At which step does the memory initialization complete?
AStep 5
BStep 6
CStep 7
DStep 4
💡 Hint
Look for the row with 'Memory initialization complete' in the Action column
If the hex file had only 2 lines, what would be the value of mem[3] after initialization?
Ax (undefined)
B00000000
C11111111
D00001010
💡 Hint
Refer to variable_tracker and key moment about fewer lines than memory size
Concept Snapshot
Memory initialization with $readmemh:
- Use $readmemh("file.hex", memory_array) inside initial block
- Reads hex values line by line from file
- Converts hex to binary and stores in memory
- Memory size and file lines should match or handle undefined
- Used for simulation memory preload
Full Transcript
This visual execution shows how Verilog's $readmemh system task initializes a memory array from a hex file. The simulation starts and calls $readmemh, which opens the file and reads each line. Each hex value is converted to an 8-bit binary number and stored in the corresponding memory index. The execution table traces each step, showing the file line read, the hex value, the binary stored, and the memory index updated. The variable tracker shows how each memory location changes from undefined to the stored value after each step. Key moments clarify common confusions like why memory is undefined before reading and what happens if the file has fewer lines than the memory size. The visual quiz tests understanding of the stored values and the process completion step. The concept snapshot summarizes the syntax and behavior of $readmemh for quick reference.