verilogquartusintel-fpga

How do I load an FPGA's Registers with Data?


Note this question is not for when I am simulating. I have found numerous resources as to how to use readmemh which does not solve my problem. What I am trying to do is load the RAM for a processor that I designed with the program data. I believe that the FPGA is not getting any of the program data, just the HDL description of the processor.

I tried using Verilog's readmemh function, which I now realize is only useful for simulation. I have also tried using the /* synthesis ram_init_file = "file.mif" */; instruction (?). Either way, there was no difference in how the device worked. I simulated all these cases in ModelSim so I know the design works. I just am stumped as to how I can preload the data.


Solution

  • The answer is going to be tool specific because the initial blocks are, in general, not synthezisable. If you can do it, it is just because the tool has a specific template that is matched with your initial block. Initializing a memory is one of these special cases, where the 'initial' block is discarded from your logic but it is used to generate the initialization data passed along the bitstream.

    From the Intel Quartus documentation we can see that there are slightly differences on the actual implementation of the two kinds of memories, dedicated RAM and MLABs, however the general idea is to use an initial block:

    module ram_with_init(
       output reg [7:0] q,
       input [7:0] d,
       input [4:0] write_address, read_address,
       input we, clk
    );
       reg [7:0] mem [0:31];
       integer i;
    
       // Init the memory with these values
       initial begin
          for (i = 0; i < 32; i = i + 1)
             mem[i] = i[7:0];
       end
    
       always @ (posedge clk) begin
          if (we)
             mem[write_address] <= d;
          q <= mem[read_address];
       end
    endmodule
    

    Or, for the quartus pro, you can use readmemh, readmemb:

    reg [7:0] ram[0:15];
    initial 
    begin
        $readmemb("ram.txt", ram);
    end
    

    I suggest you look at the documentation linked as the most updated reference.