0
\$\begingroup\$

I am trying to encapsulate my fast adder design with a top module containing input and output registers. All combinational logic is present in the instantiated module. The purpose of top design is to provide operand values to the adder and to store the calculated output at each posedge clock. When I try to do it as shown below, the output is delayed by one clock cycle. Is there any way to modify the code so that the output registers are updated with the results in the same clock cycle as input registers? I am new to Verilog and I don't totally understand different types of assignments. Also, I will be synthesizing this design later.

module four_bit_hca_reg(
    clk,
    S_out, C_out,
    A_in, B_in, C_in);

input [3:0] A_in, B_in;
input C_in, clk;
output [3:0] S_out;
output C_out;

reg [3:0] A, B, S;
reg Cin, Cout;

wire [3:0] Sum_wire;
wire Carry_out_wire;

four_bit_hca adder (.A(A), .B(B), .Cin(Cin), .S(Sum_wire), .Cout(Carry_out_wire));
assign S_out = S;
assign C_out = Cout;


always @ (posedge clk)
begin

A <= A_in;
B <= B_in;
Cin <= C_in;
S <= Sum_wire;
Cout <= Carry_out_wire;

end

endmodule
\$\endgroup\$
0
\$\begingroup\$

Let's consider the timing of the sum output (the timing of the carry output will be analogous). You have 3 sum signals in your code:

  • Sum_wire
  • S
  • S_out

Let's assume your clock period is 10ns and you drive all inputs at the posedge of clk in your testbench. When you run simulations, you will see S and S_out change at the same time as each other, and they will change 10ns after Sum_wire. That is what the always block does: adds one clock cycle delay to the Sum_wire signal. The always block will infer 4 D-flip-flops when it is synthesized, with Sum_wire bits as the D inputs and S bits as the Q outputs.

Regarding visibility, when you run simulations and dump waveforms, you can see all of the sum signals. If you really want to use the internal sum outside of the four_bit_hca module, you can add it as another output in the module port list.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. Let me ask the question in a different way. How does Verilog assign values outside always block? I was expecting to see the output in the same clock cycle because all continuous assignments are done within the same always block. Since there is no delay in calculating combinational output, aren't Sum_wire and Carry_out_wire signals available immediately after assigning values to A, B and C_in registers? \$\endgroup\$ – Karthikeya Darivemula May 10 '20 at 3:52
  • \$\begingroup\$ I changed the title of the question for accurate reflection of the question \$\endgroup\$ – Karthikeya Darivemula May 10 '20 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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