Skip to content

Commit 0fff71e

Browse files
committed
add rom.sv
1 parent af53e6e commit 0fff71e

File tree

1 file changed

+45
-0
lines changed
  • python/ci-tests/resources

1 file changed

+45
-0
lines changed

python/ci-tests/resources/rom.sv

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// -----------------------------------------------------------------
2+
// Parameterized ROM (Read-Only Memory) with file initialization
3+
// This module defines a ROM whose contents are loaded from an
4+
// external hex file via the $readmemh system task. It is
5+
// parameterized for both address width and data width.
6+
//
7+
// In synthesis, many tools (Vivado/Quartus/Yosys) support this
8+
// initialization method and will infer appropriate memory blocks.
9+
// See references about $readmemh usage for memory initialization. [oai_citation:1‡Project F](https://projectf.io/posts/initialize-memory-in-verilog/?utm_source=chatgpt.com)
10+
// -----------------------------------------------------------------
11+
module rom #(
12+
parameter ADDR_WIDTH = 8, // Width of the address bus -> depth = 2^ADDR_WIDTH
13+
parameter DATA_WIDTH = 32, // Width of each memory word
14+
parameter INIT_FILE = "" // Optional hex file for memory initialization
15+
) (
16+
input logic [ADDR_WIDTH-1:0] addr, // Read address input
17+
output logic [DATA_WIDTH-1:0] data // Read data output
18+
);
19+
20+
// -----------------------------------------------------------------
21+
// Memory array declaration (unpacked array).
22+
// Depth = 2^ADDR_WIDTH, each location is DATA_WIDTH bits wide.
23+
// -----------------------------------------------------------------
24+
logic [DATA_WIDTH-1:0] mem [0:(1<<ADDR_WIDTH)-1];
25+
26+
// -----------------------------------------------------------------
27+
// Load contents from INIT_FILE using $readmemh in the initial block.
28+
// $readmemh loads hex values from the file into the memory array.
29+
// The file should have hex values (whitespace separated, per line).
30+
// This is a common technique to initialize ROM contents. [oai_citation:2‡Stack Overflow](https://stackoverflow.com/questions/67701135/how-to-make-a-synthesizable-instruction-memory-in-systemverilog?utm_source=chatgpt.com)
31+
// -----------------------------------------------------------------
32+
initial begin
33+
if (INIT_FILE != "") begin
34+
$readmemh(INIT_FILE, mem);
35+
end
36+
end
37+
38+
// -----------------------------------------------------------------
39+
// Combinational read logic: simply index the memory with 'addr'
40+
// NOTE: In some synthesis tools this may infer combinational ROM,
41+
// or memory blocks (BRAM/LUTRAM) depending on size and style.
42+
// -----------------------------------------------------------------
43+
assign data = mem[addr];
44+
45+
endmodule

0 commit comments

Comments
 (0)