Bird
Raised Fist0
Verilogprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary purpose of the $readmemh system task in Verilog?
easy
A. To write memory contents to a file in hexadecimal format.
B. To initialize registers with random values.
C. To convert binary data into hexadecimal format during simulation.
D. To load hexadecimal data from a file into a memory array before simulation.

Solution

  1. Step 1: Understand the function of $readmemh

    $readmemh is used to read hexadecimal data from a file and load it into a memory array in Verilog.
  2. 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.
  3. Final Answer:

    To load hexadecimal data from a file into a memory array before simulation. -> Option D
  4. Quick Check:

    $readmemh loads hex data [OK]
Hint: Remember: $readmemh reads hex data into memory [OK]
Common Mistakes:
  • Confusing $readmemh with $writememh
  • Thinking it initializes registers randomly
  • Assuming it converts data formats
2. Which of the following is the correct syntax to initialize a memory array named mem using $readmemh with a file named data.hex?
easy
A. $readmemh(mem, "data.hex");
B. $readmemh("data.hex", mem);
C. readmemh("data.hex", mem);
D. $readmemh("mem", data.hex);

Solution

  1. Step 1: Recall the correct order of arguments in $readmemh

    The syntax is $readmemh(filename, memory_array); where filename is a string and memory_array is the target memory.
  2. 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.
  3. Final Answer:

    $readmemh("data.hex", mem); -> Option B
  4. Quick Check:

    Syntax is $readmemh(filename, memory) [OK]
Hint: Filename first, memory second in $readmemh [OK]
Common Mistakes:
  • Swapping filename and memory arguments
  • Omitting the $ sign
  • Using incorrect quotes or missing parentheses
3. Given the following Verilog code snippet and a file 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);
end
medium
A. 8'h2C
B. 8'h1B
C. 8'h0A
D. 8'h3D

Solution

  1. 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.
  2. Step 2: Identify value at memory[2]

    memory[2] will hold the third value, which is 2C in hex.
  3. Final Answer:

    8'h2C -> Option A
  4. Quick Check:

    memory[2] = 2C hex [OK]
Hint: Index matches line number starting at 0 [OK]
Common Mistakes:
  • Confusing index with line number (off by one)
  • Misreading hex values
  • Assuming decimal instead of hex
4. Consider this Verilog code snippet:
reg [7:0] mem [0:3];
initial begin
  $readmemh("data.hex", mem, 1, 3);
end

If data.hex contains:
AA
BB
CC
DD
EE

What values will mem[0] and mem[1] hold after initialization?
medium
A. mem[0] = 8'h00, mem[1] = 8'hAA
B. mem[0] = 8'hAA, mem[1] = 8'hBB
C. mem[0] = 8'hBB, mem[1] = 8'hCC
D. mem[0] = 8'h00, mem[1] = 8'hCC

Solution

  1. 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].
  2. 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).
  3. Final Answer:

    mem[0] = 8'h00, mem[1] = 8'hAA -> Option A
  4. Quick Check:

    Start=1 loads from line 0 to mem[1] [OK]
Hint: Start address shifts file lines to memory indices [OK]
Common Mistakes:
  • Assuming mem[0] is loaded when start address is 1
  • Confusing file line numbers with memory indices
  • Ignoring default zero initialization
5. You want to initialize a 16-word memory array 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?
hard
A. Use $readmemh("init.hex", mem, 0, 19); and truncate memory size to 16.
B. Use $readmemh("init.hex", mem, 0, 19); and ignore extra lines automatically.
C. Use $readmemh("init.hex", mem, 0, 15); to load lines 0 to 15.
D. Manually edit the file to 16 lines before loading.

Solution

  1. 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.
  2. 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.
  3. 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.
  4. Final Answer:

    Use $readmemh("init.hex", mem, 0, 15); -> Option C
  5. Quick Check:

    Limit loading to memory size with start/end [OK]
Hint: Limit $readmemh range to memory size to avoid errors [OK]
Common Mistakes:
  • Loading more lines than memory size causing errors
  • Assuming $readmemh ignores extra lines automatically
  • Editing files manually instead of using range parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard