Skip to content

Instantly share code, notes, and snippets.

@jblang
Last active December 21, 2017 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jblang/ca99c800518591e9d32324efef6289fa to your computer and use it in GitHub Desktop.
Save jblang/ca99c800518591e9d32324efef6289fa to your computer and use it in GitHub Desktop.
Block ram
module ram(clk, addr, data_in, data_out, cs, we);
parameter ADDR_WIDTH = 11;
parameter DATA_WIDTH = 8;
parameter INIT_FILE = "";
input clk;
input [ADDR_WIDTH-1:0] addr;
input [DATA_WIDTH-1:0] data_in;
output [DATA_WIDTH-1:0] data_out;
input cs;
input we;
reg [DATA_WIDTH-1:0] mem[(1 << ADDR_WIDTH)-1:0];
initial
begin
if (INIT_FILE != "") begin
$readmemh(INIT_FILE, mem);
end
end
reg [ADDR_WIDTH-1:0] addr_reg;
always @(posedge clk)
begin
if (we)
mem[addr] <= data_in;
addr_reg <= addr;
end
assign data_out = (cs && !we) ? mem[addr_reg] : 8'bz;
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment