EmbDev.net

Forum: FPGA, VHDL & Verilog Simulation delay unexpected & Stx value


von Blas M. (b_aprentice)


Attached files:

Rate this post
useful
not useful
Hi,
I have done a TB in Verilog with a selfcheck loop and I have 2 registers 
where I compare the expected and actual output value:
reg [MODULES:0] count_comp(expected), count(actual value);

Because the output from the UUT is registered, I am expecting 1 Clock 
PERIOD  delay but from the simulation I got 2 PERIODS and Undefined 
signal. The module is testing a "unsigned" adder with 4 bits

Any idea why I get the StX values after the Enable signal goes High?
And why there are 2 clock periods delay between the expected and actual 
signal?

TB self-check coding below

always begin
    for ( i=0; i < LOOP; i = i + 1 ) begin
          for (b=0; b < LOOP; b = b + 1) begin
                            in1 = i;
          in2 = b;
          count_comp = in1 + in2;
          #PERIOD;
          count = {carry, sum};
                if (count != count_comp) begin
                    $display ("Error at time %d", $time);
                    $display ("Expected value: %d, Value calculated: 
%d", count_comp, count);

von Klakx (Guest)


Rate this post
useful
not useful
I suggest you give us a better piece of code, even verilog requires at 
least a 'end' for 'if'/'for'/'begin'

von ElKo (Guest)


Rate this post
useful
not useful
Remove the clock from your adder.

Try to gather the result a split second earlier. Otherwise it is somehow 
random, which block is executed first.
1
          count_comp = in1 + in2;
2
          #PERIOD-1;
3
          count = {carry, sum};
4
                if (count != count_comp) begin
5
                    $display ("Error at time %d", $time);
6
                    $display ("Expected value: %d, Value calculated: 
7
%d", count_comp, count);
8
          #1;

Show your full code, including the adder!

von Blas M. (b_aprentice)


Rate this post
useful
not useful
Sorry for having posted a sort piece of code before, I am a newbie with 
Forums and posting.
Bellow is my full testbench in Verilog
The Adder is an unsigned adder in VHDL.
looking at the code, I would believe the only time the output values are 
registered (s and cy_out) is at the "Funct_NbitsAdder" module. So that, 
I was expecting to see 1 PERIOD delay between the calculated and the 
actual output value.
In fact , the value "sum" from the TB which it is defined as a wire, has 
the expected delay of a PERIOD ( this is the actual output from the 
"Funct_NbitsAdder". When that value is concatenated in the reg "comp" 
signal, the value has 2 PERIOD delay.
Is this due to the fact that reg definition works as a register and 
updates the values at the next clokc period?
If so, I have also tried to define "reg [MODULES:0] count_comp, count;" 
as a wires -> "wire [MODULES:0] count_comp, count;" but the tool gave me 
an error during compilation.
1
module tb_SignAdder();
2
3
parameter PERIOD = 10; // 10 ns -> 100MHz
4
parameter MODULES = 4;  // N number of adders/bits
5
parameter LOOP = 2**MODULES; // Max loop iterations 
6
7
8
reg [MODULES-1:0] in1;
9
reg [MODULES-1:0] in2;
10
reg cy_in;
11
reg clk;
12
reg en,rst;
13
wire [MODULES-1:0] sum;
14
wire carry;
15
integer i,b;
16
reg [MODULES:0] count_comp, count;
17
18
19
20
21
//Specify the VHDL architecture with the whole definition : <lib>.<module>.<architecture>
22
\xil_defaultlib.Funct_NbitsAdder(num_usigned) #(MODULES) uut ( 
23
                    .a(in1),
24
                    .b(in2),
25
                    .cy_in(cy_in),
26
                    .clk(clk),
27
                    .en(en),
28
                    .rst(rst),
29
                    .s(sum),
30
                    .cy_out(carry) );
31
32
// Init & Test En
33
initial begin 
34
$display("----------------------\n %d Bit Full-Adder\n----------------------\n", MODULES);
35
// Initialization of stimulus
36
in1 = 0; in2 = 0; cy_in = 0; clk= 1; en = 0; rst = 1;
37
#PERIOD rst = 0;
38
#(PERIOD*3) en = 1;
39
end
40
41
// Clock signal generation
42
always 
43
  #(PERIOD/2) clk = ~clk;
44
45
//Asyn process with input stimulus
46
always begin
47
    for ( i=0; i < LOOP; i = i + 1 ) begin
48
          for (b=0; b < LOOP; b = b + 1) begin
49
                in1 = i;
50
          in2 = b;
51
          count_comp = in1 + in2;
52
          #PERIOD;
53
          count = {carry, sum};
54
                if (count != count_comp) begin
55
                    $display ("Error at time %d", $time);
56
                    $display ("Expected value: %d, Value calculated: %d", count_comp, count);
57
                end
58
              end
59
    end
60
end
61
endmodule

With the Adder being represented by a unsigned module in VHDL
1
entity Funct_NbitsAdder is
2
generic (  numbits : integer := 2);
3
           
4
    Port ( a : in STD_LOGIC_VECTOR (numbits-1 downto 0):= (others=>'0');
5
           b : in std_logic_vector (numbits-1 downto 0):= (others=>'0');
6
           cy_in : in std_logic:='0';
7
         clk : in std_logic:='0';
8
         en : in std_logic:='0';
9
         rst : in std_logic:='0';
10
           s : out std_logic_vector (numbits-1 downto 0);
11
           cy_out : out std_logic);
12
end Funct_NbitsAdder;
13
14
-------------------------------
15
--- Unsigned addition!! -------
16
------------------------------- 
17
architecture num_usigned of Funct_NbitsAdder is
18
signal sum : unsigned (numbits downto 0):= (others=>'0');  
19
signal carry_out : unsigned (numbits downto 0):= (others=>'0');
20
constant zero : unsigned (numbits-1 downto 0):= (others =>'0');
21
22
begin
23
--Synchronous Reset and Enable
24
reg_outputs: process (clk)
25
begin
26
  if (clk'event and clk='1') then
27
      if (rst='1') then 
28
         s<=(others =>'0');
29
         cy_out <= '0';
30
      elsif (en='1') then
31
         carry_out <= zero & cy_in;
32
         sum <= ('0' & unsigned(a)) + ('0' & unsigned(b))+ carry_out;
33
          s <= std_logic_vector(sum(numbits-1 downto 0));
34
          cy_out <= sum(numbits);
35
      else
36
        s <= (others =>'0');
37
        cy_out <= '0';         
38
      end if;
39
  end if;
40
end process;
41
end num_usigned;

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Blas M. wrote:
> --- Unsigned addition!! -------
An addition is a completely combinatorial job. Why do you invoke a clock 
there?

von ElKo (Guest)


Rate this post
useful
not useful
Lothar M. wrote:
> An addition is a completely combinatorial job. Why do you invoke a clock
> there?
I agree.

Blas M. wrote:
> Is this due to the fact that reg definition works as a register and
> updates the values at the next clokc period?
No, it is not the definition as reg. It is the description of a clocked 
(registered) process in the adder: "if (clk'event and clk='1') then"

The second clock cycle might be a race condition. After #PERIOD the 
signals in1, in2 and clk are changing. But in which order? If clk 
changes first, the adder is executed with the old values. After that the 
new values are applied to the adder and the next wait-Period follows.
Change the testbench, that clk and the singlas are not changing in the 
same moment!

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.