# Assertion to check if all bits get set atleast once in simulation for a multibit vector

**URL:** <https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, assertion-systemverilog\
**Created:** [May 19, 2016, 4:04pm UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836 "2016-05-19T16:04:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohitk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@rohitk](https://verificationacademy.com/forums/u/rohitk)\
**Post date:** [May 19, 2016, 4:04pm UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/1 "2016-05-19T16:04:25Z")

</div>

Hi,  
I need help in writing an assertion to check if a given variable in DUT is set atleast once in simulation cycle.

Variable to check : bit [3:0] p\_flags;  
Conditions:

1. variable bits might get set or reset at any clock and it is not guaranteed that at the end of simulation will remain at 1.

Thanks in advance for help.

Regards,  
Rohit

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [May 19, 2016, 9:12pm UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/2 "2016-05-19T21:12:09Z")

</div>

_In reply to [rohitk](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836):_

```verilog

// assertion to check if all bits get set atleast once in simulation for a multibit vector
import uvm_pkg::*; `include "uvm_macros.svh" 
module top; 
	bit clk, enb=1;  
	bit[3:0] fgs; // flags to check 
	default clocking @(posedge clk); endclocking
	initial forever #10 clk=!clk;   
	property p_flags; 
		bit[3:0] v=0; 
		enb |-> (1, v=v | fgs)[*1:$] ##0 (v==4'b1111 || enb==0); 
		// When enb gets set to 0, another attempt gets started in the Preponed region
	endproperty 
	ap: assert property(p_flags) enb=0;  

 initial begin 
     repeat(200) begin 
       @(posedge clk);   
       if (!randomize(fgs) with 
       		{$countones(fgs)==1;}
           ) `uvm_error("MYERR", "This is a randomize error")
       end 
       $finish; 
    end 
endmodule  	
  

```

Ben Cohen  
[http://www.systemverilog.us/](http://www.systemverilog.us/) [ben@systemverilog.us](mailto:ben@systemverilog.us)

- [SVA Handbook 4th Edition, 2016 ISBN 978-1518681448](http://goo.gl/JOfuEB)
- A Pragmatic Approach to VMM Adoption 2006 ISBN 0-9705394-9-5
- Using PSL/SUGAR for Formal and Dynamic Verification 2nd Edition, 2004, ISBN 0-9705394-6-0
- Real Chip Design and Verification Using Verilog and VHDL, 2002 isbn 0-9705394-2-8
- Component Design by Example ", 2001 ISBN 0-9705394-0-1
- VHDL Coding Styles and Methodologies, 2nd Edition, 1999 ISBN 0-7923-8474-1
- VHDL Answers to Frequently Asked Questions, 2nd Edition ISBN 0-7923-8115

* * *

---

<div class="post-metadata">

**Author:** ![user60](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user60/32/1096_2.png) [@user60](https://verificationacademy.com/forums/u/user60)\
**Post date:** [May 20, 2016, 4:09am UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/3 "2016-05-20T04:09:22Z")

</div>

_In reply to [rohitk](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/2):_

How about?

```verilog

  // Assuming default clock & disable

  property p_check_single_bit_to_be_set;
    s_eventually p_flags[0];
  endproperty : p_check_single_bit_to_be_set

  a_p_check_single_bit_to_be_set : assert property (p_check_single_bit_to_be_set) else
    `uvm_error ("SVA", "Bit was never set to 1")

```

Combine that with a generate loop for scalable bits

Regards  
Srini  
[http://www.verifworks.com](http://www.verifworks.com)

---

<div class="post-metadata">

**Author:** ![rohitk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@rohitk](https://verificationacademy.com/forums/u/rohitk)\
**Post date:** [May 20, 2016, 7:46am UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/4 "2016-05-20T07:46:50Z")

</div>

_In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/3):_

Hi Ben,

Thanks a ton for help, the assertion property is not failing is none of the bits gets set. I modified your constraint to {$countones(fgs)==0;} and no failure get generated.

Regards,  
Rohit

---

<div class="post-metadata">

**Author:** ![rohitk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@rohitk](https://verificationacademy.com/forums/u/rohitk)\
**Post date:** [May 20, 2016, 7:51am UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/5 "2016-05-20T07:51:13Z")

</div>

_In reply to [Srini @ CVCblr.com](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/4):_

Hi Srini,

Thanks for helping, your solution works fine but it always fails in the end if bits gets set to 0. I had to modify it a bit to get it working for my case:

property p\_check\_single\_bit\_to\_be\_set;  
enb |-\> s\_eventually fgs[0];  
endproperty : p\_check\_single\_bit\_to\_be\_set

a\_p\_check\_single\_bit\_to\_be\_set : assert property (p\_check\_single\_bit\_to\_be\_set)  
enb=0;  
else  
$display(“SVA”, “Bit was never set to 1”);

I incorporated this change from Ben’s solution.

This works fine for me but problem is eventually property is not yet supported by all tools AFAIK. Is there any way we can achieve the same without using eventually.

Thanks again for helping me out.

Regards,  
Rohit

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [May 20, 2016, 3:21pm UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/6 "2016-05-20T15:21:02Z")

</div>

_In reply to [rohitk](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/5):_  
OOPS! There are TWO errors in my solution, and one in Srini’s

1. My solution needs a **strong** sequence, as a weak sequence would not fail if not completed.
2. This assertion needs to be fired ONCE. In both of our original solutions, at every clock yo have a new attempt, and some of those later attempts could produce a failure.

Below is a solution that will work for you. I am assuming that during the first 45 time units, the system is being initialized, and after that, you have a **go** signal. You don’t need this **go** signal if you want the assertion to be fired once at time 0.

```verilog

// assertion to check if all bits get set atleast once in simulation for a multibit vector
import uvm_pkg::*; `include "uvm_macros.svh" 
module top; 
	bit clk, go;  
	bit[3:0] fgs; // flags to check 
	default clocking @(posedge clk); endclocking
	initial forever #10 clk=!clk;   
	property p_flags; 
		bit[3:0] v=0; 
		go |-> strong( (1, v=v | fgs)[*1:$] ##0 (v==4'b1111));  
	endproperty 
	
 initial begin // option 1
 	 wait(go);
 	 ap_flags: assert property(p_flags);  
 end
 
 generate for (genvar g_i=0; g_i<4; g_i++) 
 	 initial begin // opton2
       wait(go);
       ap_fgsi: assert property( go |-> s_eventually fgs[g_i]);;    
 	 end
 endgenerate 

 initial begin 
 	#45 go <= 1'b1;
     repeat(25) begin 
       @(posedge clk);   
       if (!randomize(fgs) with 
       		{$countones(fgs)==1;
       		  fgs <= 7;
       		}
           ) `uvm_error("MYERR", "This is a randomize error")
       end 
       $finish; 
    end 
endmodule    
  

```

Ben Cohen  
[http://www.systemverilog.us/](http://www.systemverilog.us/) [ben@systemverilog.us](mailto:ben@systemverilog.us)

- [SVA Handbook 4th Edition, 2016 ISBN 978-1518681448](http://goo.gl/JOfuEB)
- A Pragmatic Approach to VMM Adoption 2006 ISBN 0-9705394-9-5
- Using PSL/SUGAR for Formal and Dynamic Verification 2nd Edition, 2004, ISBN 0-9705394-6-0
- Real Chip Design and Verification Using Verilog and VHDL, 2002 isbn 0-9705394-2-8
- Component Design by Example ", 2001 ISBN 0-9705394-0-1
- VHDL Coding Styles and Methodologies, 2nd Edition, 1999 ISBN 0-7923-8474-1
- VHDL Answers to Frequently Asked Questions, 2nd Edition ISBN 0-7923-8115

* * *

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [May 21, 2016, 4:17am UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/7 "2016-05-21T04:17:49Z")

</div>

_In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/6):_

> _This works fine for me but problem is eventually property is not yet supported by all tools AFAIK. Is there any way we can achieve the same without using eventually_.

```verilog

generate for (genvar g_i=0; g_i<4; g_i++) 
  initial begin // opton2
     wait(go);
     ap_fgsbi: assert property( go |-> strong(fgs[g_i][-> 1]) );    
  end
 endgenerate  

```

Complete model is at [http://SystemVerilog.us/bitset.sv](http://SystemVerilog.us/bitset.sv)  
Results at end of sim

# \*\* Error: Assertion error.

# Time: 930 ns Started: 50 ns Scope: top.ap\_flags File: bitset.sv Line: 15

# \*\* Error: Assertion error.

# Time: 930 ns Started: 50 ns Scope: top.genblk1[3].ap\_fgsi File: bitset.sv Line: 21

# \*\* Error: Assertion error.

# Time: 930 ns Started: 50 ns Scope: top.genblk2[3].ap\_fgsbi File: bitset.sv Line: 28

* * *

Ben Cohen  
[http://www.systemverilog.us/](http://www.systemverilog.us/) [ben@systemverilog.us](mailto:ben@systemverilog.us)

- [SVA Handbook 4th Edition, 2016 ISBN 978-1518681448](http://goo.gl/JOfuEB)
- A Pragmatic Approach to VMM Adoption 2006 ISBN 0-9705394-9-5
- Using PSL/SUGAR for Formal and Dynamic Verification 2nd Edition, 2004, ISBN 0-9705394-6-0
- Real Chip Design and Verification Using Verilog and VHDL, 2002 isbn 0-9705394-2-8
- Component Design by Example ", 2001 ISBN 0-9705394-0-1
- VHDL Coding Styles and Methodologies, 2nd Edition, 1999 ISBN 0-7923-8474-1
- VHDL Answers to Frequently Asked Questions, 2nd Edition ISBN 0-7923-8115

* * *

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [May 25, 2016, 2:22pm UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/8 "2016-05-25T14:22:13Z")

</div>

_In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/7):_  
I received an interesting new approach in resolving this assertion:  
“I think that it be more efficient to use an always procedure that remembers that a bit has been set and then use an immediate assertion in a final block to check that all bits are set.” Below is code for that assertion. The complete model is at  
[http://SystemVerilog.us/bitset.sv](http://SystemVerilog.us/bitset.sv)

```verilog

module top; 
	bit clk, go;  
	bit[3:0] fgs; // flags to check 
	bit[3:0] chkfgs; 
	default clocking @(posedge clk); endclocking
...
   generate for (genvar g_i=0; g_i<4; g_i++)  
     always_ff @(posedge clk)  
   	 if(go && fgs[g_i]) chkfgs[g_i] <= 1'b1;   
   endgenerate 
   // ap_chkfgs: assert final (chkfgs==4'b1111); // Incorrect! see below
   final ap_chkfgs: assert (chkfgs==4'b1111); // OK 

```

Ben Cohen  
[http://www.systemverilog.us/](http://www.systemverilog.us/) [ben@systemverilog.us](mailto:ben@systemverilog.us)

- [SVA Handbook 4th Edition, 2016 ISBN 978-1518681448](http://goo.gl/JOfuEB)
- A Pragmatic Approach to VMM Adoption 2006 ISBN 0-9705394-9-5
- Using PSL/SUGAR for Formal and Dynamic Verification 2nd Edition, 2004, ISBN 0-9705394-6-0
- Real Chip Design and Verification Using Verilog and VHDL, 2002 isbn 0-9705394-2-8
- Component Design by Example ", 2001 ISBN 0-9705394-0-1
- VHDL Coding Styles and Methodologies, 2nd Edition, 1999 ISBN 0-7923-8474-1
- VHDL Answers to Frequently Asked Questions, 2nd Edition ISBN 0-7923-8115

* * *

---

<div class="post-metadata">

**Author:** ![rohitk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@rohitk](https://verificationacademy.com/forums/u/rohitk)\
**Post date:** [May 28, 2016, 8:51am UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/9 "2016-05-28T08:51:25Z")

</div>

_In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/8):_

Thanks Ben for help.

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [May 29, 2016, 4:42pm UTC](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/10 "2016-05-29T16:42:06Z")

</div>

_In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/assertion-to-check-if-all-bits-get-set-atleast-once-in-simulation-for-a-multibit-vector/31836/9):_

**OOPS! One more errata!**  
SHOULD BE

```verilog

final ap_chkfgs: assert (chkfgs==4'b1111); // OK 
// you do NOT need the 2nd final in 
// final ap_chkfgs: assert final (chkfgs==4'b1111); // superfulous 2nd final

```

Ben [SystemVerilog.us](http://SystemVerilog.us)
