Search code examples
verilogsystem-verilogiverilog

Value changes based on clk doesn't work for random numbers


I am coding that put the value of 'd' into 'z' whenever 'clk' is changed to '1'.

For example,

clk=0 d=        15, z=         x
clk=1 d=        20, z=        20
clk=0 d=        25, z=        20
clk=1 d=        30, z=        30

it put value of 'd' into 'z' whenever clk is '1'.

So below is code that does repeat it for 20 times for random numbers.

module lab9;
reg [31:0] d;
reg clk, enable, flag;
wire [31:0] z;
reg [31:0] e;
register #(32) mine(z, d, clk, enable);

always begin
#5 clk = ~clk;

end

initial
#1 $monitor("%5d: clk=%b,d=%d,z=%d,expect=%d", $time,clk,d,z, e);

initial begin 
clk=0;
flag = $value$plusargs("enable=%b", enable);

repeat (20) begin
#2 d = $random;
end
$finish; 

end 

endmodule 

And the output I get:

    1: clk=0,d=         x,z=         x,expect=         x
    2: clk=0,d= 303379748,z=         x,expect=         x
    4: clk=0,d=3230228097,z=         x,expect=         x
    5: clk=1,d=3230228097,z=3230228097,expect=         x
    6: clk=1,d=2223298057,z=3230228097,expect=         x
    8: clk=1,d=2985317987,z=3230228097,expect=         x
   10: clk=0,d= 112818957,z=3230228097,expect=         x
   12: clk=0,d=1189058957,z=3230228097,expect=         x
   14: clk=0,d=2999092325,z=3230228097,expect=         x
   15: clk=1,d=2999092325,z=2999092325,expect=         x
   16: clk=1,d=2302104082,z=2999092325,expect=         x
   18: clk=1,d=  15983361,z=2999092325,expect=         x
   20: clk=0,d= 114806029,z=2999092325,expect=         x
   22: clk=0,d= 992211318,z=2999092325,expect=         x
   24: clk=0,d= 512609597,z=2999092325,expect=         x
   25: clk=1,d= 512609597,z= 512609597,expect=         x
   26: clk=1,d=1993627629,z= 512609597,expect=         x
   28: clk=1,d=1177417612,z= 512609597,expect=         x
   30: clk=0,d=2097015289,z= 512609597,expect=         x
   32: clk=0,d=3812041926,z= 512609597,expect=         x
   34: clk=0,d=3807872197,z= 512609597,expect=         x
   35: clk=1,d=3807872197,z=3807872197,expect=         x
   36: clk=1,d=3574846122,z=3807872197,expect=         x
   38: clk=1,d=1924134885,z=3807872197,expect=         x
   40: clk=0,d=3151131255,z=3807872197,expect=         x

On Line 6 of this output need to be '2223298057' but still have value of previous 'z' even though its clk is set to '1'.

How can I fix this?


Solution

  • It would seem from the behaviour of the outputs you have presented that

    register #(32) mine(z, d, clk, enable);
    

    is a set of 32 D-type flip-flops, which given its name and the names of the signals you have connected also seems to be the case. It would have been easier to answer your question if you had provided the code for register.

    So, if register is indeed a set of 32 D-type flip-flops, you would not expect z to change at time 6. That is not how any kind of flip-flop behaves: the output of a flip-flop only changes on one (rising or falling) edge of a clock.

    Given these D-type flip-flops were present before you started this exercise, it would seem that you are changing d far too quickly. You ought to be changing it once per clock (clk) cycle, ie once every #10. In other words, try chaninging

    #2 d = $random;
    

    to

    #10 d = $random;