Attention

This documentation is a work in progress. Expect to see errors and unfinished things.

dpram Source File

 1`timescale 1ns / 1ns
 2// Dual port memory with independent clocks, port B is read-only
 3// Altera and Xilinx synthesis tools successfully "find" this as block memory
 4module dpram #(
 5     parameter aw=8,
 6     parameter dw=8,
 7     parameter initial_file = ""
 8) (
 9     input clka,
10     input clkb,
11     input [aw-1:0] addra,
12     output [dw-1:0] douta,
13     input [dw-1:0] dina,
14     input wena,
15     input [aw-1:0] addrb,
16     output [dw-1:0] doutb
17);
18localparam sz=32'b1<<aw;
19
20reg [dw-1:0] mem[0:sz-1];
21reg [aw-1:0] ala=0, alb=0;
22
23// In principle the zeroing loop should work OK for synthesis, but
24// there was a bug at least one version of Xilinx tools back in 2014.
25// No known current tool needs it disabled.
26integer k=0;
27initial begin
28     if (initial_file != "") $readmemh(initial_file, mem);
29`ifndef BUGGY_FORLOOP
30     else begin
31             for (k=0;k<sz;k=k+1) mem[k]=0;
32     end
33`endif
34end
35
36assign douta = mem[ala];
37assign doutb = mem[alb];
38always @(posedge clka) begin
39     ala <= addra;
40     if (wena) mem[addra]<=dina;
41end
42always @(posedge clkb) begin
43     alb <= addrb;
44end
45
46endmodule