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 )
mem[addr] <= data;
data <= mem[addr];

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
  • \$\begingroup\$ You probably need to set data to Z when you are not driving the bus. If this is meant for FPGA synthesis, you want to have separate data-in and data-out busses. \$\endgroup\$
    – The Photon
    Commented Oct 10, 2013 at 19:04

2 Answers 2


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;

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


You have made two ram instances, given them two separate names, wired one of their write enable ports to zero and the other to 1. This will give you two rams, one of which is always being written to and one of which is always being read from.

Are you trying to run a simulation? What simulator are you using, we'll help you through it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.