# Counting number of events on clock a, while clock o is forbidden

**URL:** <https://verificationacademy.com/forums/t/counting-number-of-events-on-clock-a-while-clock-o-is-forbidden/30521>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, first\_match, assertion\
**Created:** [April 4, 2015, 8:22am UTC](https://verificationacademy.com/forums/t/counting-number-of-events-on-clock-a-while-clock-o-is-forbidden/30521 "2015-04-04T08:22:30Z")\
**Posts on this page:** 1\
**Showing post:** 18

<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:** [April 10, 2015, 11:09pm UTC](https://verificationacademy.com/forums/t/counting-number-of-events-on-clock-a-while-clock-o-is-forbidden/30521/18 "2015-04-10T23:09:49Z")

</div>

_In reply to [Jadoon](https://verificationacademy.com/forums/t/counting-number-of-events-on-clock-a-while-clock-o-is-forbidden/30521/17):_

OK, I worked out the AsFrom assertion and the tb for it.  
The png result is at [http://systemverilog.us/ccsl.png](http://systemverilog.us/ccsl.png)  
The code is at [http://systemverilog.us/ccsl.sv](http://systemverilog.us/ccsl.sv)  
I used constrained-randomization with count as the vehicle to define the types of constraints.  
Using constrained-randomization is a fast way of achieving a variety of test vectors.  
BTW, I added an error on count 16 to demonstrate the failure.

```verilog

module ccsl;
  bit a, o, clk, clk1, rst;
  int count; 
  // Initially, o must be 0
  ap_o0: assert property(@(posedge clk) 
  		$rose(rst) |-> o==0); 
  		                             
  // AsFrom: Initially O is 0, A comes at any time. On Kth arrival of A sampled on clk, 
  // O should be there, and afterwards O is exactly same as A.
  // Let k==5
  ap_AsFrom: assert property(@(posedge clk)  		                            
      (($rose(rst) ##1 a[->4]) intersect !o[*]) ##1 a[->1] |-> 
                                                    o==1'b1 ##1 a ~^ o[*1000]); 
 
 initial forever #5 clk=!clk;
 
 initial begin 
 	@(negedge clk) rst <= 1'b1;
 	@(negedge clk) rst <= 1'b0;
 end
  
  always @(posedge clk) 
     begin 
     	if (count <4) begin 
     	   if(a ) count<=count+1'b1; // increment before update of "a" 
     	   if(!randomize(rst, a, o) with 
       		{ rst dist {1'b1:=1, 1'b0:=999}; // can comment this line 
       		  a dist {1'b1:=25, 1'b0:=75};
       		  o dist {1'b1:=10, 1'b0:=70}; 
       		  0==1'b0;}) $display("error");    	   
           end
        
        else if (count == 4) begin 
        	if(!randomize(rst, a, o) with 
       		{ rst dist {1'b1:=1, 1'b0:=999};
       		  a dist {1'b1:=30, 1'b0:=70};
       		  o dist {1'b1:=10, 1'b0:=10};
       		  o==a; }) $display("error"); 
        	if(a) count<=count+1'b1;
            end 
            
        else if (count >= 5 && count <16 ) begin 
        	count<=count+1'b1;
        	if(!randomize(rst, a, o) with 
       		{ rst dist {1'b1:=1, 1'b0:=999};
       		  //a dist {1'b1:=30, 1'b0:=30};
       		  //o dist {1'b1:=10, 1'b0:=10};
       		  o==a; }) $display("error");             
            end 
        else if (count==16) begin 
        	count<=count+1'b1;
            if(!randomize(rst, a, o) with 
       		{ rst dist {1'b1:=1, 1'b0:=99};
       		  a dist {1'b1:=30, 1'b0:=30};
       		  o dist {1'b1:=10, 1'b0:=10};
       		  a!=o; }) $display("error");           
         end 
         else begin 
        	count<=count+1'b1;
        	if(!randomize(rst, a, o) with 
       		{ rst dist {1'b1:=1, 1'b0:=999};
       		  a dist {1'b1:=30, 1'b0:=30};
       		  o dist {1'b1:=10, 1'b0:=10};
       		  o==a; }) $display("error");  
        	end 
     end 
endmodule 

```

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

---

_[View the full topic](https://verificationacademy.com/forums/t/counting-number-of-events-on-clock-a-while-clock-o-is-forbidden/30521)._
