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?
reg [7:0] mem [0:3]; initial begin $readmemh("data.hex", mem); end // Contents of data.hex: // 1A // 2B // 3C // 4D
The $readmemh command loads file lines sequentially into memory starting at index 0.
The $readmemh reads the file line by line and stores each hexadecimal value into consecutive memory locations starting at mem[0]. So the first line 1A goes to mem[0], the second line 2B goes to mem[1].
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?
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
$writememh writes hexadecimal values line by line without prefixes.
The $writememh command writes each memory element as a hexadecimal string on its own line without any prefix or spacing.
What error or behavior occurs when $readmemh tries to read a file containing invalid hexadecimal characters?
reg [7:0] mem [0:1]; initial begin $readmemh("bad.hex", mem); end // Contents of bad.hex: // 1G // ZZ
Invalid hex digits cause warnings but do not stop simulation.
$readmemh issues a warning for invalid hex digits and sets the corresponding memory location to zero. It does not cause a syntax error or stop simulation.
Given the following code, what is the value of mem[2] after the $readmemh call?
reg [7:0] mem [0:4]; initial begin $readmemh("data.hex", mem, 1, 3); end // Contents of data.hex: // 10 // 20 // 30 // 40 // 50
The $readmemh with start and end indices loads the first three lines from the file into memory mem[1] to mem[3].
The call $readmemh("data.hex", mem, 1, 3) loads first line (10) to mem[1], second line (20) to mem[2], third line (30) to mem[3]. So mem[2] gets 8'h20.
Which reason best explains why $readmemh is often preferred over $readmemb for loading test vectors in Verilog simulations?
Think about file size and readability of hex vs binary.
Hexadecimal representation is more compact and easier to read and edit than binary, so $readmemh is preferred for test vectors. The other options are incorrect because $readmemh does not support floating-point directly, does not convert decimal, and file reading is synchronous.
