Electrical – How to transform the code for a read-write register to a read only register in Verilog

readregisterverilog

I have the code for read write register here

module read_write(
input             clk     ,
input             rst_n   ,
input      [31:0] data_in , //data to be written
input             rw      , //rw=1->write to data_out; otherwise, read from data_out
output reg [31:0] data_out 
);



always @(posedge clk or negedge rst_n) begin
if(~rst_n)
  data_out<=0;
else begin
  if (rw)               //write to data_out
    data_out<=data_in;
end
end
endmodule

How can I read data from a register? I am a bit confused, if I don't write the data to the register, how can I read something from the register?

Best Answer

To read its content at the given address...

I suspect your code has been reduced a bit too much, it is not looking at all like a CPU interface. This might be a school assignment so I only provide an outline of the code. (Which, by the way, has not been syntax checked)

...
input [7:0] read_only;
...
reg [7:0] write_only;
reg [7:0] read_write;


always @(posedge clk or negedge reset_n)
begin
   if (!reset_n)
   begin // set reset value of registers
      ...
   end
   else // clocked
   begin
      if (cpu_write)
         case (cpu_address[2:0])
         3'b000 : write_only <= cpu_write_data;
         3'b001 : read_write <= cpu_write_data;
         endcase
      else
      if (cpu_read)
         case (cpu_address[2:0])
         3'b001 : cpu_read_data <= read_write;
         3'b010 : cpu_read_data <= read_only;
         endcase
   end // clocked
end // always