// Memory Array
|
// Memory Array
|
reg [7:0] meml[0:WORDS-1];
|
reg [7:0] meml [0:WORDS-1];
|
reg [7:0] memh[0:WORDS-1];
|
reg [7:0] memh [0:WORDS-1];
|
// If used as Rom then load a memory image at startup
|
// If used as Rom then load a memory image at startup
|
initial
|
initial
|
begin
|
begin
|
$display("SRAM def %m.mem");
|
$display("SRAM def %m.mem");
|
$display(" AddrBits=%d DataBits = 16 Words = %d ",ADDR,WORDS);
|
$display(" AddrBits=%d DataBits = 16 Words = %d ",ADDR,WORDS);
|
end
|
end
|
|
|
// Write function
|
// Write function
|
always@(posedge clk)
|
always@(posedge clk)
|
if( wr && cs && be[0]) meml[addr[ADDR:1]] <= wdata[7:0];
|
if( wr && cs && be[0]) meml[addr[ADDR:1]] <= wdata[7:0];
|
|
|
|
|
|
|
always@(posedge clk)
|
always@(posedge clk)
|
if( wr && cs && be[1]) memh[addr[ADDR:1]] <= wdata[15:8];
|
if( wr && cs && be[1]) memh[addr[ADDR:1]] <= wdata[15:8];
|
|
|
|
|
|
|
// Read function gets new data if also a write cycle
|
// Read function gets new data if also a write cycle
|
// latch the read addr for next cycle
|
// latch the read addr for next cycle
|
reg [ADDR:1] l_raddr;
|
reg [ADDR:1] l_raddr;
|
reg l_cycle;
|
reg l_cycle;
|
|
|
always@(posedge clk)
|
always@(posedge clk)
|
begin
|
begin
|
l_raddr <= addr;
|
l_raddr <= addr;
|
l_cycle <= rd && cs ;
|
l_cycle <= rd && cs ;
|
end
|
end
|
|
|
|
|
|
|
generate
|
generate
|
if( WRITETHRU)
|
if( WRITETHRU)
|
begin
|
begin
|
|
|
|
|
// Read into a wire and then pass to rdata because some synth tools can't handle a memory in a always block
|
// Read into a wire and then pass to rdata because some synth tools can't handle a memory in a always block
|
wire [15:0] tmp_rdata;
|
wire [15:0] tmp_rdata;
|
assign tmp_rdata = (l_cycle )?{memh[{l_raddr[ADDR:1]}],meml[{l_raddr[ADDR:1]}]}:16'hffff;
|
assign tmp_rdata = (l_cycle )?{memh[{l_raddr[ADDR:1]}],meml[{l_raddr[ADDR:1]}]}:16'hffff;
|
always@(*) rdata = tmp_rdata;
|
always@(*) rdata = tmp_rdata;
|
end
|
end
|
else
|
else
|
begin
|
begin
|
// Read function gets old data if also a write cycle
|
// Read function gets old data if also a write cycle
|
always@(posedge clk)
|
always@(posedge clk)
|
if( rd && cs ) rdata <= {memh[{addr[ADDR:1]}],meml[{addr[ADDR:1]}]} ;
|
if( rd && cs ) rdata <= {memh[{addr[ADDR:1]}],meml[{addr[ADDR:1]}]} ;
|
else rdata <= 16'hffff;
|
else rdata <= 16'hffff;
|
end
|
end
|
endgenerate
|
endgenerate
|
|
|
|
|
|
|
|
|