EmbDev.net

Forum: FPGA, VHDL & Verilog Testbench of d flip flop


von Fahim K. (fahimk)


Rate this post
useful
not useful
Hi,

I am trying to run vhdl code of d flip flop on ghdl.

I am not getting any error while compiling and executing,however when I 
try to run it, it doesnt come to prompt again it keeps on runing.
Please let me know where I am making mistake.

My code is as below.
1
---------------------------------------------
2
3
library ieee ;
4
use ieee.std_logic_1164.all;
5
6
7
---------------------------------------------
8
9
entity dff is
10
port ( din:     in std_logic;
11
       dclk:     in std_logic;
12
       qout:    out std_logic;
13
       nqout:   out std_logic
14
     );
15
16
end dff;
17
--------------------------------------------
18
19
architecture behavioral of dff is
20
21
begin
22
        process(din,dclk)
23
        begin
24
          --- clock rising edge
25
        if(dclk='1' and dclk'event) then
26
                qout <= din;
27
                nqout <= not din;
28
        end if;
29
        end process;
30
end behavioral;
31
32
------------------------------------------              
33
------TestBench of D Flip Flop------------                      
34
------------------------------------------
35
36
library ieee;
37
use ieee.std_logic_1164.all;
38
39
40
entity dff_tb is
41
end dff_tb;
42
43
44
------------------------------------------
45
46
architecture testbench of dff_tb is
47
48
        signal T_din:   std_logic;
49
        signal T_dclk:  std_logic;
50
        signal T_qout:  std_logic;
51
        signal T_nqout: std_logic;
52
53
        constant clk_period : time := 50 ns;
54
55
        component dff
56
        port ( din:     in std_logic;
57
               dclk:    in std_logic;
58
               qout:    out std_logic;
59
               nqout:   out std_logic
60
             );
61
        end component;
62
63
begin
64
65
    dut_dff: dff port map (T_din,T_dclk,T_qout,T_nqout);
66
67
    process
68
        begin
69
          T_dclk <= '0';
70
          wait for clk_period/2;
71
          T_dclk <= '1';
72
          wait for clk_period/2;
73
    end process;
74
75
    process
76
77
        begin
78
          --case1
79
          T_din <= '0';
80
          wait for clk_period*2;
81
82
          --case2
83
          T_din <= '1';
84
          wait for clk_period*1.5;
85
86
          --case3
87
          T_din <= '0';
88
          wait for clk_period*0.5;
89
90
          --case4
91
          T_din <= '1';
92
93
        wait;
94
   end process;
95
96
end testbench;

:
von P. K. (pek)


Rate this post
useful
not useful
You have to stop the testbench somehow at the end, e.g. by an assertion 
like:
1
    assert false
2
      report " ===== Verification run 'TESTBENCH' finished ====="
3
      severity failure;

Otherwise, your clock process will run forever...

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


Rate this post
useful
not useful
> it keeps on runing.
Which toolchain do you use?
Usually there's also a button "run for a specified time". There you can 
say: run my simulation for 100ns, then stop.

BTW:
1
     dut_dff: dff port map (T_din,T_dclk,T_qout,T_nqout);
Don't use positional assignment. I promise you: you will run into 
problems with this after the first minor change. Where's the problem 
here:
1
     dut_dff: dff port map (T_din,T_dclk,T_nqout,T_qout);
Write it with explicit assignments, and then you can twist and move 
every port:
1
dut_dff: dff port map (qout=>T_qout,din=>T_din,dclk=>T_dclk,nqout=>T_nqout);

von Fahim K. (fahimk)


Rate this post
useful
not useful
Hi,

I changed the code to use assert statement but still getting same issue.
1
---------------------------------------------
2
3
library ieee ;
4
use ieee.std_logic_1164.all;
5
6
7
---------------------------------------------
8
9
entity dff is
10
port ( din:  in std_logic;
11
       dclk:     in std_logic;
12
       qout:    out std_logic;
13
       nqout:   out std_logic
14
     );
15
16
end dff;
17
18
19
--------------------------------------------
20
21
architecture behavioral of dff is
22
23
begin
24
  process(din,dclk)
25
  begin
26
    --- clock rising edge
27
  if(dclk='1' and dclk'event) then
28
    qout <= din;
29
    nqout <= not din;
30
  end if;
31
  end process;
32
end behavioral;
33
34
------------------------------------------    
35
36
------TestBench of D Flip Flop------------      
37
------------------------------------------
38
39
library ieee;
40
use ieee.std_logic_1164.all;
41
42
43
entity dff_tb is
44
end dff_tb;
45
46
47
------------------------------------------
48
49
architecture testbench of dff_tb is
50
 
51
  signal T_din:  std_logic;
52
  signal T_dclk:  std_logic;
53
  signal T_qout:  std_logic;
54
  signal T_nqout: std_logic;
55
  
56
  component dff 
57
  port ( din:   in std_logic;
58
         dclk:  in std_logic;
59
         qout:    out std_logic;
60
         nqout:  out std_logic
61
       );
62
  end component;
63
64
begin
65
           
66
    dut_dff: dff port map (T_din,T_dclk,T_qout,T_nqout);
67
68
    process
69
  begin
70
    T_dclk <= '0';
71
    wait for 5 ns;
72
    T_dclk <= '1';
73
    wait for 5 ns;
74
    end process;
75
76
    process 
77
  
78
  variable err_cnt: integer := 0; 
79
80
  begin
81
    --case1
82
   T_din <= '1';
83
  wait for 12 ns;     
84
  assert (T_qout='1') report "Error1!" severity error;
85
  if (T_qout/='1') then
86
      err_cnt := err_cnt + 1;
87
  end if;
88
89
  -- case 2
90
  T_din <=  '0';   
91
  wait for 28 ns;
92
  assert (T_qout='0') report "Error2!" severity error;
93
  if (T_qout/='0') then
94
      err_cnt := err_cnt + 1;
95
  end if;
96
97
  -- case 3
98
  T_din <= '1';            
99
  wait for 2 ns;
100
  assert (T_qout='0') report "Error3!" severity error;
101
  if (T_qout/='0') then
102
      err_cnt := err_cnt + 1;
103
  end if;
104
    
105
  -- case 4
106
  T_din <= '0';
107
  wait for 10 ns;
108
  assert (T_qout='0') report "Error4!" severity error;
109
  if (T_qout/='0') then
110
      err_cnt := err_cnt + 1;
111
  end if;
112
113
  -- case 5
114
  T_din <=  '1';    
115
  wait for 20 ns;    
116
  assert (T_qout='1') report "Error5!" severity error;   
117
  if (T_qout/='0') then
118
      err_cnt := err_cnt + 1;
119
  end if;
120
   
121
  -- summary of all the tests
122
  if (err_cnt=0) then       
123
      assert false 
124
      report "Testbench of Adder completed successfully!" 
125
      severity note; 
126
  else 
127
      assert true 
128
      report "Something wrong, try again" 
129
      severity error; 
130
  end if; 
131
  
132
  wait;
133
   end process;
134
   
135
end testbench;

von Fahim K. (fahimk)


Rate this post
useful
not useful
Hi,

I am using ghdl.

von Fahim K. (fahimk)


Rate this post
useful
not useful
Got it I am stoping it after 500ns using

ghdl -r dff_tb --stop-time=500ns

von P. K. (pek)


Rate this post
useful
not useful
Fahim Khan wrote:
>   if (err_cnt=0) then
>       assert false
>       report "Testbench of Adder completed successfully!"
>       severity note;
>   else
>       assert true
>       report "Something wrong, try again"
>       severity error;
>   end if;

Wrong, needs to be:
1
   if (err_cnt=0) then
2
       assert false
3
       report "Testbench of Adder completed successfully!"
4
       severity failure;
5
   else
6
       assert false
7
       report "Something wrong, try again"
8
       severity failure;
9
   end if;

The assert condition has always to be "false", if you want it to trig...
...then it will work as expected. And to be sure it stops, use severity 
"failure", this is usually a break condition by default.

von berndl (Guest)


Rate this post
useful
not useful
With GHDL and other simulators (e.g. ModelSim) I usually have in my TB:
1
   clk_process : process
2
   begin
3
    wait for clk_period*5;
4
    loop
5
      clk50 <= '1';
6
      wait for clk_period/2;
7
      clk50 <= '0';
8
      wait for clk_period/2;
9
      exit when end_of_sim = '1';
10
    end loop;
11
    wait;
12
   end process;
...this (or something similar) for all special processes I have in the 
TB...

And in my 'main' TB execution process (snippet of the end of this 
process):
1
    --assert end_of_sim = '1'
2
    --  report "---- End of module simulation reached ----" severity note;
3
    end_of_sim <= '1';
4
    wait;
5
   end process;

This will stop GHDL and others, either with the 'assert' (commented out 
here) or 'automagically'... (of course, 'end_of_sim' 
initially/previously set to '0')

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.