# How to write functional coverage for FSM (for both State coverage and Transition coverage)

**URL:** <https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915>\
**Category:** Coverage\
**Tags:** Coverage, SystemVerilog-FunctionalCoverage\
**Created:** [February 28, 2017, 7:07am UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915 "2017-02-28T07:07:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rohit\_kumar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@rohit\_kumar](https://verificationacademy.com/forums/u/rohit_kumar)\
**Post date:** [February 28, 2017, 7:07am UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/1 "2017-02-28T07:07:32Z")

</div>

Hi all,  
How to write functional coverage for FSM , im stuck at like how to extract the states into functional coverage.

```verilog

module example (
 output wire [1:0] out,
 input wire clk,
 input wire in,
input wire rst_n
 );

/ / s t a t e b i t s
 parameter
 S0 = 2’b00, / / out [1 : 0]=0 0
 S1 = 2’b01, / / out [1 : 0]=0 1
 S2 = 2’b10; / / out [1 : 0]=1 0

 reg [1:0] state;
 reg [1:0] nextstate;

 / / comb alway s b l o c k
 always @* begin
 nextstate = state;
 c as e (state)
 S0: begin
 i f (in = 1) begin
 nextstate = S1;
 end
 e l s e begin
 nextstate = S0;
 end
 end
 S1: begin
 i f (in = 1) begin
 nextstate = S2;
 end
 e l s e begin
 nextstate = S0;
 end
 end
 S2: begin
 begin
 nextstate = S0;
 end
 end
 endcase
 end

 / / As s ign reg ’ d o u t p u t s t o s t a t e b i t s
 a s s i g n out[1:0] = state[1:0];

 / / s e q u e n t i a l alway s b l o c k
 always @(posedge clk or negedge rst_n) begin
 i f (!rst_n)
 state <= S0;
 e l s e
 state <= nextstate;
 end

 endmodule

```

---

<div class="post-metadata">

**Author:** ![Tudor\_Timi](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/tudor_timi/32/1897_2.png) [@Tudor\_Timi](https://verificationacademy.com/forums/u/Tudor_Timi)\
**Post date:** [February 28, 2017, 7:50am UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/2 "2017-02-28T07:50:44Z")

</div>

You don’t need to write functional coverage for this. Tools can generate this kind of coverage for you automatically. It’s called FSM coverage and it consists of coverage that each state has been reached and that all possible state arcs have been traversed.

---

<div class="post-metadata">

**Author:** ![rohit\_kumar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@rohit\_kumar](https://verificationacademy.com/forums/u/rohit_kumar)\
**Post date:** [February 28, 2017, 8:48am UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/3 "2017-02-28T08:48:31Z")

</div>

_In reply to [Tudor Timi](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/2):_

I know that, but explicitly if i want to write functional coverage for FSM, then how to proceed?

---

<div class="post-metadata">

**Author:** ![kddholak](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kddholak/32/1363_2.png) [@kddholak](https://verificationacademy.com/forums/u/kddholak)\
**Post date:** [February 28, 2017, 5:16pm UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/4 "2017-02-28T17:16:40Z")

</div>

_In reply to [rohit\_kumar](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/3):_

if you don’t want to rely on the tool FSM coverage and want to define your own FSM coverage. collector , Inside the interface of testbench where you have access low level information.  
Define the transition coverage (Arc of the FSM) in cover group inside the interface.

---

<div class="post-metadata">

**Author:** ![rohit\_kumar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/c89c15/32.png) [@rohit\_kumar](https://verificationacademy.com/forums/u/rohit_kumar)\
**Post date:** [March 1, 2017, 5:02am UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/5 "2017-03-01T05:02:03Z")

</div>

_In reply to [kddholak](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/4):_

Hi,  
In interface only signals will be defined rite, no state representations. how to cover states transitions?

interface example\_intf(input bit clk);  
logic[1:0] out,  
logic in,  
logic rst\_n  
endinterface

---

<div class="post-metadata">

**Author:** ![chirag.hadiya](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chirag.hadiya](https://verificationacademy.com/forums/u/chirag.hadiya)\
**Post date:** [June 3, 2024, 5:34am UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/6 "2024-06-03T05:34:14Z")

</div>

Sir, Kindly explain if you got it.

---

<div class="post-metadata">

**Author:** ![chr\_sue](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chr_sue/32/1199_2.png) [@chr\_sue](https://verificationacademy.com/forums/u/chr_sue)\
**Post date:** [June 4, 2024, 6:42am UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/7 "2024-06-04T06:42:00Z")

</div>

I guess you are looking for FSM coverage in your DUT, right. In this case you have to make the internal signals visible on the border or in your testbench. You can reach this by binding a coverage component to your DUT. Using the tool coverage is not so good because this has no flexibility when canging your tool.

---

<div class="post-metadata">

**Author:** ![kddholak](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kddholak/32/1363_2.png) [@kddholak](https://verificationacademy.com/forums/u/kddholak)\
**Post date:** [June 4, 2024, 4:52pm UTC](https://verificationacademy.com/forums/t/how-to-write-functional-coverage-for-fsm-for-both-state-coverage-and-transition-coverage/32915/8 "2024-06-04T16:52:30Z")

</div>

if the requirement is not to probe into the internal signals. then based on input , and output user need to come up with FSM , and implement in interface logic. it can be done using assertions.
