0
0
Verilogprogramming~20 mins

Memory initialization with $readmemh in Verilog - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Verilog memory initialization?

Given the following Verilog code snippet, what will be the value of mem[2] after initialization?

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

// Contents of data.hex:
// 0A
// 1B
// 2C
// 3D
A8'h1B
B8'h3D
C8'h2C
D8'h0A
Attempts:
2 left
💡 Hint

Remember that $readmemh loads data line by line starting at index 0.

🧠 Conceptual
intermediate
1:30remaining
How does $readmemh interpret the input file?

Which statement best describes how $readmemh initializes memory from a file?

AIt reads hexadecimal values line by line and stores them sequentially starting at the specified memory address.
BIt reads binary values separated by spaces and stores them randomly in memory.
CIt reads decimal values and stores them in reverse order in memory.
DIt reads ASCII characters and stores their decimal codes in memory.
Attempts:
2 left
💡 Hint

Think about the format of the file and how memory addresses are assigned.

🔧 Debug
advanced
2:00remaining
Why does this $readmemh initialization fail?

Consider this Verilog code snippet:

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

The file data.hex contains 4 lines of hex data. What is the problem with this code?

AThe $readmemh call must not include start and end addresses.
BThe ending address 4 is out of bounds for mem[0:3], causing an error.
CThe memory array must be declared as <code>reg [3:0] mem [0:7]</code> for $readmemh to work.
DThe file name must be an absolute path, not a relative one.
Attempts:
2 left
💡 Hint

Check the memory array size and the range specified in $readmemh.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this $readmemh usage

What is wrong with this Verilog code snippet?

reg [7:0] mem [0:3];
initial begin
  $readmemh(data.hex, mem);
end
AThe $readmemh function requires a third argument specifying the start address.
BThe memory array must be declared as <code>wire</code>, not <code>reg</code>.
CThe <code>initial</code> block must be replaced with an <code>always</code> block.
DThe file name must be a string literal enclosed in double quotes.
Attempts:
2 left
💡 Hint

Look carefully at how the file name is passed to $readmemh.

🚀 Application
expert
2:30remaining
How many memory locations are initialized by this $readmemh call?

Given the following code and file, how many memory locations in mem will be initialized?

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

// data.hex contains 8 lines of hex values.
A4
B6
C8
D5
Attempts:
2 left
💡 Hint

Count the number of addresses from start to end inclusive.