I have a module in which I generate multiple RAMs, and each one reads initial data from a file. I am having trouble passing the file name to the RAM module as a parameter. I can pass a specific file name no problem, but when I create a filename using $sformatf, it fails at run time. I have seen this thread here Is it not allowed to use $sformat inside generate statement? which discusses strings in generate loops but (I think) it does not address passing the string as a parameter.
So: how can I pass an arbitrary string (built from $sformatf or something similar) as a parameter?
Below is a cut-down example. Here, all the play_ram module does is display the parameter ram_init_file. What I want is to pass “ram0.dat” as the parameter when i==0, and “ram1.dat” as the parameter when i==1
module play_ram(a);
input a;
parameter ram_init_file = "default.dat";
initial begin
$display("file name is %s", ram_init_file);
end
endmodule // ram
module play;
wire [9:0] a;
genvar i;
generate
for (i=0; i<2; i=i+1) begin:ram_gen
play_ram #(.ram_init_file($sformatf("ram%1d.dat", i))) ram_inst(a[i]); // fails
end
endgenerate
endmodule // play
$sformatf is a SystemVerilog construct returning a string datatype, but you are using *.v file extensions, which are Verilog only. I suggest changing the file extensions to *.sv. Also, change your module header to