Odd Parity State Machine

Easy · Finite State Machines · SystemVerilog Constructs

Bits arrive on din, one per clock cycle. Build a Moore state machine that tells whether it has seen an odd number of 1s since the last reset.

It needs only two states:

StateMeaningodd
EVENan even number of 1s so far (zero counts as even)0
ODDan odd number of 1s so far1
            din = 1
      ┌─────────────────┐
      │                 ▼
   [ EVEN ]          [ ODD ]
      ▲                 │
      └─────────────────┘
            din = 1

   din = 0: stay in the same state

Ports

PortDirWidthMeaning
clkinput1Clock. din is sampled at every rising edge.
rstinput1Synchronous, active-high reset (takes effect only at a rising edge).
dininput1The incoming bit.
oddoutput11 in state ODD, 0 in state EVEN.

Behavior

At every rising edge of clk:

  1. If rst = 1: go to EVEN. The bit on din at that edge is not counted.
  2. Else if din = 1: go to the other state (EVEN ↔ ODD).
  3. Else: stay in the same state.

odd depends only on the state (that is what makes it a Moore machine): it changes only right after a rising edge, never directly when din changes. So after the edge that samples a bit, odd already includes it.

Example

edge0 (rst)123456
din sampled1101110
state after the edgeEVENODDODDEVENODDEVENEVEN
odd after the edge0110100

Try writing the states as a typedef enum logic { EVEN, ODD } state_t;.

Sign up free to solve Odd Parity State Machine in your browser.

More Finite State Machines problems