We use $readmemh and $writememh to load and save data from files in Verilog. This helps test designs with real data easily.
File I/O for test vectors ($readmemh, $writememh) in Verilog
$readmemh("filename", memory_array); $writememh("filename", memory_array);
$readmemh reads a file with hexadecimal values into a memory array.
$writememh writes the contents of a memory array to a file in hexadecimal format.
input_data.hex into mem_array.$readmemh("input_data.hex", mem_array);mem_array into output_data.hex as hex values.$writememh("output_data.hex", mem_array);testvec.hex.reg [7:0] mem_array [0:15]; initial begin $readmemh("testvec.hex", mem_array); end
This program loads 4 bytes from input.hex into memory, prints them, changes two values, and saves the memory to output.hex.
module testmem; reg [7:0] mem [0:3]; integer i; initial begin // Load memory from file $readmemh("input.hex", mem); // Display loaded values $display("Memory contents after loading:"); for (i = 0; i < 4; i = i + 1) begin $display("mem[%0d] = %h", i, mem[i]); end // Modify memory mem[0] = 8'hAA; mem[1] = 8'hBB; // Save memory to file $writememh("output.hex", mem); $finish; end endmodule
Files used with $readmemh must contain hex values separated by spaces or newlines.
Memory arrays must be declared before using these system tasks.
Use $readmemb and $writememb for binary files instead of hex.
$readmemh loads hex data from a file into a memory array.
$writememh saves memory array data to a hex file.
These tasks help test your design with real data easily and save results for checking.
Practice
$readmemh?Solution
Step 1: Understand
$readmemhusage$readmemhis used to read hexadecimal data from a file and load it into a memory array in Verilog.Step 2: Compare with other options
Other options describe writing data, random initialization, or display, which are not the purpose of$readmemh.Final Answer:
To load hexadecimal data from a file into a memory array -> Option AQuick Check:
$readmemhloads hex data [OK]
$readmemh reads hex data into memory [OK]- Confusing
$readmemhwith$writememh - Thinking it writes data instead of reading
- Assuming it reads binary instead of hex
Solution
Step 1: Identify the correct system task for writing
$writememhis the Verilog system task used to write memory contents to a hex file.Step 2: Check syntax correctness
The syntax$writememh("output.hex", mem_array);correctly specifies the file name and memory array.Final Answer:
$writememh("output.hex", mem_array); -> Option BQuick Check:
Write memory to hex file =$writememh[OK]
$writememh to save memory to hex file [OK]- Using
$readmemhinstead of$writememh - Misspelling the system task name
- Using non-existent tasks like
$writehex
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
Solution
Step 1: Understand
$readmemhfile loading order$readmemhloads the first line of the file intomem[0], second line intomem[1], and so on.Step 2: Match file content to memory
The first line is0A, somem[0]will be8'h0A.Final Answer:
8'h0A -> Option DQuick Check:
First file line -> mem[0] = 0A [OK]
- Assuming mem[0] gets last file line
- Confusing hex values with decimal
- Forgetting zero-based indexing
reg [7:0] mem [0:3];
initial begin
$writememh(mem, "output.hex");
end
Solution
Step 1: Check
The correct syntax is$writememhargument order$writememh("filename", memory_array);. The code reverses these arguments.Step 2: Verify other aspects
Memory size and file extension are valid, and$writememhcan be used inside initial blocks.Final Answer:
Arguments to$writememhare in wrong order -> Option CQuick Check:
Filename first, memory second in$writememh[OK]
- Swapping filename and memory array arguments
- Thinking file extension is mandatory
- Believing $writememh can't be in initial block
Solution
Step 1: Load test vectors correctly
Use$readmemh("input.hex", mem);to load test vectors from the input file into memory.Step 2: Save modified memory correctly
Use$writememh("output.hex", mem);to write the memory contents to the output file.Final Answer:
$readmemh("input.hex", mem); // test
$writememh("output.hex", mem); // save -> Option AQuick Check:
Read first, then write with correct syntax [OK]
- Reversing read and write order
- Swapping filename and memory arguments
- Using wrong filenames for read/write
Ready to prove it?
Pass the test and this pattern moves unknown
