I am using the code shown here Verilog: Writing to a Register Happens A Clock Cycle Late and that user posted a picture where output would change 2 cycles after asserting write enable. That post got an answer which seemed reasonably good to me. However, I decided to simulate the exact same code and create a testbench for it and in mine, output appears only 1 clock cycle after asserting write enable. Could somebody explain to me why is different if I'm doing the exact same thing?

enter image description here

The only difference I can spot is we're not using the same simulator but how could that be the reason?

This is testbench:

`timescale 1ns / 1ps
module test_regblock_tb;

parameter tclk = 10;

reg clk = 1'b1;
reg rst = 1'b1;

    #(tclk/2) clk =~clk;

initial begin
    rst = 1'b0;

reg [4:0] RegA_Name = 4'd5;
reg [4:0] RegB_Name = 4'd6;
reg [4:0] RegC_Name = 4'd7;
reg [31:0] RegA_In = 32'd0;
reg [31:0] RegC_In = 32'd0;
reg RegA_WrtEn = 1'b0;
reg RegC_WrtEn = 1'b0;

wire [31:0] RegA_Out;
wire [31:0] RegB_Out;

RegisterFile regblock(

RegA_In = 32'd126;
RegA_WrtEn = 1'b1;
RegA_WrtEn = 1'b0;



1 Answer 1


The problem is with your testbench's use of #(tclk*N) followed by the use of blocking assignments to your design's inputs—it's a race condition. Your testbench should be using non-blocking assignments to any signals synchronized to the rising edge of tclk. Or you can adjust your testbench initial block so that the signal changes do not coincide with the rising edge.

  • \$\begingroup\$ I think your last suggestion is the best one. Moving all input signal changes away from the clock edges makes it a lot easier to see what is going on. \$\endgroup\$ Jul 29, 2020 at 20:24
  • \$\begingroup\$ I see. Correct me if I'm wrong but this race condition is unlikely (if not impossible) to happen in real hardware since signals won't change at the exact same time as the rising edge of the clock, so what I was seeing in simulation (output at only 1 clock cycle) would not happen if I tested it on real hardware, right? It wouldn't be ready until 2 clock cycles. \$\endgroup\$
    – simedro
    Jul 30, 2020 at 6:43
  • \$\begingroup\$ Simulation testbenches do not exist in real hardware. Real hardware can have race conditions if the hardware logic is not placed and routed correctly, so not impossible. We tend to take that for granted. \$\endgroup\$
    – dave_59
    Aug 1, 2020 at 0:10

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.