Electronic – using ram verilog instantiation

ramverilog

I have implemented ram Verilog module. I made its instantiation and read the data from some address .After that I want to write the data to other address.
looks that I don`t need to made the instantiation of the ram twice,since each ram defines different registers array.The question is how (should I?) reuse the same ram instance?

module ram(we,addr,data);

input we;
input[DATA_WIDTH - 1:0] addr,
inout[DATA_WIDTH - 1:0] data;

reg [DATA_WIDTH - 1:0] mem[0:ADDR_DEPTH - 1];
always @ ( posedge clk )
begin
if(we)
mem[addr] <= data;
else
data <= mem[addr];
end
endmodule

module top (add1,add2);
wire [DATA_WIDTH - 1:0] w_data;
ram i_ram1(1b'0,add1,w_data);
ram i_ram2(1b'1,add2,w_data);//<- other mem array is used in this instance

Best Answer

You'll need to use the clock to break up the write and read operations. Here is an example of a test bench that writes data to two addresses then reads them back.

wire [DATA_WIDTH-1:0] data = we ? wdata : {DATA_WIDTH{1'bz}}; // tri-state write output driver
ram i_ram0(clk,we,addr,data);

initial begin
  clk   = 1'b1;
  data1 = 'h0F;
  data2 = 'hF0;
  addr1 = 0;
  addr2 = 2;
  we    = 1'b1;
  addr  = addr1;
  wdata = data1;
  @(posedge clk) #1;
  addr  = addr2;
  wdata = data2;
  @(posedge clk) #1;
  we    = 1'b0;
  addr  = addr1;
  @(posedge clk) #1;
  if(data !== data1) $display("ERROR data:%h != data1:%h @ addr:%h", data,data1,addr);
  addr  = addr2;
  @(posedge clk) #1;
  if(data !== data2) $display("ERROR data:%h != data2:%h @ addr:%h", data,data2,addr);
  @(posedge clk) #1;
  $finish;
end

FLY if data is an inout, then you need a tri-state driver in your ram module:

assign data = we ? {DATA_WIDTH{1'bz}} : rdata; // tri-state read output driver

Full working example here: http://www.edaplayground.com/s/6/221