-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_rom.py
54 lines (42 loc) · 1.15 KB
/
generate_rom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python2.7 -u
from struct import unpack;
import sys
if len(sys.argv) < 2:
print "Usage: %s <rom file>" % sys.argv[0]
exit(1)
template = """/* AUTOGENERATED see generate_rom.py */
module rom (
input clk,
input mem_valid,
output reg mem_ready,
input [31:0] mem_addr,
output reg [31:0] mem_rdata,
input [31:0] mem_wdata,
input [3:0] mem_wstrb
);
localparam DEPTH = 8;
localparam WORDS = 1 << DEPTH;
reg [31:0] rom [0:WORDS-1];
integer i;
initial begin
for(i = 0; i < WORDS; i = i + 1) rom[i] <= 32'h00000000;
##ROM##
end
wire [DEPTH-1:0] addr = mem_addr[DEPTH-1+2:2];
always @(posedge clk) begin
mem_ready <= 0;
if(mem_valid && !mem_ready) begin
mem_ready <= 1;
mem_rdata <= rom[addr];
end
end
endmodule"""
rom = []
data = open(sys.argv[1], "r").read()
i = 0;
while len(data) >= 4:
word, data = data[:4], data[4:]
rom.append(" rom[%i] <= 32'h%s;" % (i, word[::-1].encode("hex")));
i += 1;
rom = "\n".join(rom)
print template.replace("##ROM##", rom)