# Reading data from register file in system verilog

I'm creating a simple register file in system verilog, with a total of 6 registers that can be written to/read from. When I run a simulation in ModelSim, the output never shows the correct data - it stays at 0 and ignores any read signals. This led me to believe that either a) the values I'm writing to the registers may not be getting stored, so when I try to read a value from one of the registers they don't output anything, or b) data is being written to the registers, but I'm not pulling the data from those registers correctly.

Here is my .sv file:

module register_file#(parameter bits = 16, registers = 6)(clock, write_enable, write_reg, data_in, read_reg0, read_reg1, data_out0, data_out1);

input clock, write_enable;
input [bits-1:0] data_in;
output [bits-1:0] data_out0, data_out1;

wire [bits-1:0] data_out0_wire, data_out1_wire;
reg [bits-1:0] data_out0_reg, data_out1_reg;
reg [bits-1:0] r1, r2, r3, r4, r5, r6;

assign data_out0 = data_out0_reg;
assign data_out1 = data_out1_reg;

always @(posedge clock)
begin
if(write_enable)
case(write_reg)
1: r1 = data_in;
2: r2 = data_in;
3: r3 = data_in;
4: r4 = data_in;
5: r5 = data_in;
6: r6 = data_in;
endcase
data_out0_reg <= data_out0_wire;
data_out1_reg <= data_out1_wire;
end

1: data_out0_wire = r1;
2: data_out0_wire = r2;
3: data_out0_wire = r3;
4: data_out0_wire = r4;
5: data_out0_wire = r5;
6: data_out0_wire = r6;
endcase

1: data_out1_wire = r1;
2: data_out1_wire = r2;
3: data_out1_wire = r3;
4: data_out1_wire = r4;
5: data_out1_wire = r5;
6: data_out1_wire = r6;
endcase

endmodule


Edit And here is my testbench:

timescale 1ns / 1ps
module test_partC();

reg  clk,write_enable;
reg [15:0] data_in;
reg [15:0] data_out0, data_out1;

// Toggle the clock every 10 ns
initial
begin
clk = 0;
forever #10 clk = !clk;
end

initial
begin
write_reg = 1;
write_enable = 1;
data_in = 10;
#20;
write_reg = 2;
write_enable = 1;
data_in = 15;
#20;
write_reg = 3;
write_enable = 1;
data_in = 20;
#20;
write_enable = 0;
#20;
#20;
write_reg = 1;
write_enable = 1;
data_in = 30;
end


endmodule

I added an extra set of registers from the data_out signals in trying to find a potential solution to the problem I'm having, but I'm not sure if that changes anything.

What I was trying to do was, based on the number from the read_reg signal, read the value from the corresponding register, which sends the signal down the data_out_wire, stores it in a data_out register, and then sends the signal out via a wire (output data_out).

I'm new to system verilog and I'm struggling to figure out where I went wrong, any help is greatly appreciated.

• Your code does not compile for me on 2 different simulators. If I change wire to reg for data_out0_wire, data_out1_wire, it compiles. – toolic Feb 7 '17 at 20:53
• I've added the testbench - also, I'm not sure why I'm not getting a compile error with the wire.. Is it even required for me to have the extra wires/reg for the output variables? Or could I have just passed the register values to the output signals in the case blocks? (i.e. 1: data_out0 = r1;) That is what I originally had, but the simulation didn't behave any differently. – Nick Borisenko Feb 7 '17 at 21:10
• What simulator are you using? Did you try your code on edaplayground? – toolic Feb 7 '17 at 21:31
• Oh wow that's really concerning.. it shouldn't be compiling but it still is on my end - I even created a new project and files, because I've had a similar problem before. I'm using Quartus Lite and ModelSim-Altera for simulations. – Nick Borisenko Feb 7 '17 at 21:50

You should connect the clk signal to the design instance in your testbench:
register_file dut (clk, write_enable, write_reg, data_in, read_reg0, read_reg1, data_out0, data_out1);
`