# Sampling covergroup when a FSM state ends

**URL:** <https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804>\
**Category:** Coverage\
**Tags:** covergroup, Coverage\
**Created:** [August 31, 2022, 11:22am UTC](https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804 "2022-08-31T11:22:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArielElliassi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@ArielElliassi](https://verificationacademy.com/forums/u/ArielElliassi)\
**Post date:** [August 31, 2022, 11:22am UTC](https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804/1 "2022-08-31T11:22:07Z")

</div>

Hello,  
I have a covergroup that I want to sample when a specific state of the FSM ends.  
For example, when state\_1 ends (and the next state is different) then the group should be sampled.

One way I thought of doing it, is to have 2 variable of cur\_state and next\_state in my coverage file, and in the main\_phase task compare them and if they are different then sample.

Is there a more elegant way? I have inspiration from transition sampling in bins (STATE\_1 =\> STATE\_2) or iff of some kind.

Thank you

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 31, 2022, 5:36pm UTC](https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804/2 "2022-08-31T17:36:26Z")

</div>

_In reply to [Ariel Elliassi](https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804):_

The _elegant_ way is using a cover directive instead of a covergroup

```verilog
cover property (@(posedge clk) cur_state==STATE_1 |-> cur_state!=STATE_1[->1]);

```

But if this code needs be be in a class, you will have to resort to less elegant ways.

You could use a range if that works out for your states

```verilog
bins end_state_1 = (STATE_1 => [STATE_2:STATE_N]);

```

---

<div class="post-metadata">

**Author:** ![ArielElliassi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@ArielElliassi](https://verificationacademy.com/forums/u/ArielElliassi)\
**Post date:** [September 1, 2022, 8:28am UTC](https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804/3 "2022-09-01T08:28:55Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804/2):_

Thank you very much.  
About the second solution, is it possible/correct to write it as:

```verilog

bins end_state_1 = (STATE_1 => (~STATE_1));

```

So basically, transition from STATE\_1 to anything else?

Also, I need to sample a value when STATE\_1 ends, so if I’ll do a cross between a value and a transition, will it give the value at the time the state ends?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [September 1, 2022, 8:19pm UTC](https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804/4 "2022-09-01T20:19:32Z")

</div>

_In reply to [Ariel Elliassi](https://verificationacademy.com/forums/t/sampling-covergroup-when-a-fsm-state-ends/40804/3):_

That will not work because Kit just inverts the bits of the encoding for STATE\_1, which may or may not be another valid state encoding.

I think we are getting into sn [XY problem](https://xyproblem.info). You should call sample() on the variable you want covered the the code in your task detects that it has exited STATE\_1
