1
votes

I' trying to store value from wire named 'in' into reg 'a'. But, the problem is value of reg 'a' is showing 'xxxx' in simulator. However, value of wire 'in' is showing correctly. My target is just to read value from input wire and store it into a register.

module test(
input [3:0] in,
output [3:0] out
);
reg [3:0] a;

initial
begin
a = in;
end
endmodule
1
Have you deliberately not connected a to out?mkrieger1
Why are you using a Reg ? What seems from your logic is that you want to copy input to reg so whenever the input changes the reg should change (your logic) so why not use the wire?Anirudh Roy

1 Answers

2
votes

The reason why the value of a is 'xxxx' in the simulation is probably that a is set to the value of in only a single time initially, and a may not yet have been set to any specific value at this time in the simulation.


Declaring a reg in Verilog does not necessarily mean that a hardware register is described by the code. That usually involves the use of a clock signal:

module test(
  input clk,
  input [3:0] in,
  output [3:0] out
);

// this describes a register with input "in" and output "a"
reg [3:0] a;
always @(posedge clk) begin
  a <= in;
end

// I assume you want "a" to be the output of the module
assign out = a;

endmodule

Here is a counter example where a reg is used to describe something which is not a register, but only a simple wire:

module not_a_register(
  input in,
  output out
);

reg a;
always @(in) begin
  a <= in;
end

assign out = a;

endmodule

Also note that I have used the non-blocking assignment operator <= inside the always block, which is good practice when describing synchronous logic. You can read more about it here.