Bird
Raised Fist0
Verilogprogramming~20 mins

File I/O for test vectors ($readmemh, $writememh) in Verilog - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File I/O Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output content of memory after $readmemh?

Given the following Verilog code snippet and the file data.hex containing hexadecimal values, what will be the content of mem[0] and mem[1] after the $readmemh call?

Verilog
reg [7:0] mem [0:3];
initial begin
  $readmemh("data.hex", mem);
end

// Contents of data.hex:
// 1A
// 2B
// 3C
// 4D
Amem[0] = 8'h1A, mem[1] = 8'h2B
Bmem[0] = 8'h00, mem[1] = 8'h00
Cmem[0] = 8'h3C, mem[1] = 8'h4D
Dmem[0] = 8'h2B, mem[1] = 8'h1A
Attempts:
2 left
💡 Hint

The $readmemh command loads file lines sequentially into memory starting at index 0.

❓ Predict Output
intermediate
2:00remaining
What does $writememh output to the file?

Consider the following Verilog code that writes memory content to a file using $writememh. What will be the content of the output file out.hex?

Verilog
reg [7:0] mem [0:2];
initial begin
  mem[0] = 8'hAA;
  mem[1] = 8'hBB;
  mem[2] = 8'hCC;
  $writememh("out.hex", mem);
end
A
AA
BB
CC
BAA BB CC
C
10101010
10111011
11001100
D
0xAA 0xBB 0xCC
Attempts:
2 left
💡 Hint

$writememh writes hexadecimal values line by line without prefixes.

❓ Predict Output
advanced
2:00remaining
What error occurs with incorrect $readmemh file format?

What error or behavior occurs when $readmemh tries to read a file containing invalid hexadecimal characters?

Verilog
reg [7:0] mem [0:1];
initial begin
  $readmemh("bad.hex", mem);
end

// Contents of bad.hex:
// 1G
// ZZ
AMemory locations contain garbage values
BSyntax error and simulation stops
CSimulation warning: invalid hex digit, memory location set to 0
DNo error, memory loaded as zeros
Attempts:
2 left
💡 Hint

Invalid hex digits cause warnings but do not stop simulation.

❓ Predict Output
advanced
2:00remaining
What is the value of mem[2] after $readmemh with offset?

Given the following code, what is the value of mem[2] after the $readmemh call?

Verilog
reg [7:0] mem [0:4];
initial begin
  $readmemh("data.hex", mem, 1, 3);
end

// Contents of data.hex:
// 10
// 20
// 30
// 40
// 50
A8'h40
B8'h20
C8'h30
D8'h10
Attempts:
2 left
💡 Hint

The $readmemh with start and end indices loads the first three lines from the file into memory mem[1] to mem[3].

🧠 Conceptual
expert
2:00remaining
Why use $readmemh instead of $readmemb for test vectors?

Which reason best explains why $readmemh is often preferred over $readmemb for loading test vectors in Verilog simulations?

A<code>$readmemh</code> automatically converts decimal to binary during loading
B<code>$readmemh</code> supports loading floating-point numbers directly
C<code>$readmemh</code> is faster because it reads files asynchronously
D<code>$readmemh</code> files are more compact and easier to read because hexadecimal uses fewer digits than binary
Attempts:
2 left
💡 Hint

Think about file size and readability of hex vs binary.

Practice

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

Solution

  1. Step 1: Understand $readmemh usage

    $readmemh is used to read hexadecimal data from a file and load it into a memory array in Verilog.
  2. Step 2: Compare with other options

    Other options describe writing data, random initialization, or display, which are not the purpose of $readmemh.
  3. Final Answer:

    To load hexadecimal data from a file into a memory array -> Option A
  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 writes data instead of reading
  • Assuming it reads binary instead of hex
2. Which of the following is the correct syntax to write memory contents to a hex file in Verilog?
easy
A. $readmemh("data.hex", mem_array);
B. $writememh("output.hex", mem_array);
C. $writehex("output.hex", mem_array);
D. $dumpmemh("output.hex", mem_array);

Solution

  1. Step 1: Identify the correct system task for writing

    $writememh is the Verilog system task used to write memory contents to a hex file.
  2. Step 2: Check syntax correctness

    The syntax $writememh("output.hex", mem_array); correctly specifies the file name and memory array.
  3. Final Answer:

    $writememh("output.hex", mem_array); -> Option B
  4. Quick Check:

    Write memory to hex file = $writememh [OK]
Hint: Use $writememh to save memory to hex file [OK]
Common Mistakes:
  • Using $readmemh instead of $writememh
  • Misspelling the system task name
  • Using non-existent tasks like $writehex
3. Given the following Verilog code snippet, what will be the content of mem[0] after $readmemh executes if the file data.hex contains:
0A
1F
2B

reg [7:0] mem [0:2];
initial begin
$readmemh("data.hex", mem);
end
medium
A. 8'h00
B. 8'h1F
C. 8'h2B
D. 8'h0A

Solution

  1. Step 1: Understand $readmemh file loading order

    $readmemh loads the first line of the file into mem[0], second line into mem[1], and so on.
  2. Step 2: Match file content to memory

    The first line is 0A, so mem[0] will be 8'h0A.
  3. Final Answer:

    8'h0A -> Option D
  4. Quick Check:

    First file line -> mem[0] = 0A [OK]
Hint: First file line loads into mem[0] with $readmemh [OK]
Common Mistakes:
  • Assuming mem[0] gets last file line
  • Confusing hex values with decimal
  • Forgetting zero-based indexing
4. Identify the error in the following Verilog code snippet that attempts to write memory contents to a hex file:
reg [7:0] mem [0:3];
initial begin
$writememh(mem, "output.hex");
end
medium
A. Missing file extension in filename
B. Memory array size is invalid
C. Arguments to $writememh are in wrong order
D. Cannot use $writememh inside initial block

Solution

  1. Step 1: Check $writememh argument order

    The correct syntax is $writememh("filename", memory_array);. The code reverses these arguments.
  2. Step 2: Verify other aspects

    Memory size and file extension are valid, and $writememh can be used inside initial blocks.
  3. Final Answer:

    Arguments to $writememh are in wrong order -> Option C
  4. Quick Check:

    Filename first, memory second in $writememh [OK]
Hint: Filename is first argument in $writememh [OK]
Common Mistakes:
  • Swapping filename and memory array arguments
  • Thinking file extension is mandatory
  • Believing $writememh can't be in initial block
5. You want to test a memory module by loading test vectors from a hex file and then save the modified memory contents back to another hex file. Which sequence of Verilog system tasks correctly achieves this?
hard
A. $readmemh("input.hex", mem); // test
$writememh("output.hex", mem); // save
B. $writememh("input.hex", mem); // test
$readmemh("output.hex", mem); // save
C. $readmemh(mem, "input.hex"); // test
$writememh(mem, "output.hex"); // save
D. $readmemh("output.hex", mem); // test
$writememh("input.hex", mem); // save

Solution

  1. Step 1: Load test vectors correctly

    Use $readmemh("input.hex", mem); to load test vectors from the input file into memory.
  2. Step 2: Save modified memory correctly

    Use $writememh("output.hex", mem); to write the memory contents to the output file.
  3. Final Answer:

    $readmemh("input.hex", mem); // test
    $writememh("output.hex", mem); // save
    -> Option A
  4. Quick Check:

    Read first, then write with correct syntax [OK]
Hint: Read test vectors first, write results second [OK]
Common Mistakes:
  • Reversing read and write order
  • Swapping filename and memory arguments
  • Using wrong filenames for read/write
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