Goal

A machine fsm_led with two states:

  • IDLE (initial): LED off; go to ON when the input BTN is 1;
  • ON: LED on; go back to IDLE when BTN is 1.

This is the same logic as the Toggle example, built by hand. Note that while BTN is held at 1 the machine changes state on every clock: in a real design feed BTN with a one-clock pulse (for example the PULSE output of the Button debouncer example).


Step 1 - Create the machine

Click New State Machine in the toolbar of the Project panel, type fsm_led and press OK.

New machine name

Figure 1: naming the machine

The machine appears in the tree under Unused with one state, IDLE. Double-click the fsm_led node to open the editor. IDLE is the initial state (▶ INIT) and is terminal because it has no transitions yet.

The new machine

Figure 2: a new machine: one state, IDLE

Step 2 - Add the second state

Click + State in the graph toolbar (or double-click the empty canvas), type ON and press OK. Use Fit to view (F) if the new card is out of sight, and drag the cards where you like.

Add State

Figure 3: naming the new state

Step 3 - Draw the transitions

Hover IDLE: a small ● handle appears on the right edge of the card. Drag it onto ON. Do the same from ON back to IDLE. The two transitions get the default names IDLE_ON and ON_IDLE, priority 1.

They are red and dashed and the cards show 0/1 exits: each transition still needs its EXIT CONDITION block. The header lists the two problems.

Two transitions without exit condition

Figure 4: transitions drawn, exit conditions missing

Step 4 - Body of IDLE: input port and exit condition

  1. Click the IDLE card: its body opens on the right (BODY — IDLE).

  2. In the Blocks panel search “State Port” and double-click State Port (IN). The machine has no ports yet, so three short dialogs ask the name (BTN), the signal type (std_logic_vector) and the width (1). Click on the canvas to drop the block.

    New port name

    Figure 5: declaring the input port BTN

  3. Click + Exit Condition in the body caption; the chooser proposes IDLE_ON; press OK. Move the EXIT ► IDLE_ON block next to BTN (click its caption, then drag it).

  4. Drag a wire from the BTN pin to the input of EXIT ► IDLE_ON.

IDLE body

Figure 6: body of IDLE: the machine goes to ON when BTN = 1

Step 5 - Body of ON: reuse the port, add the LED output

  1. Click the ON card.

  2. Double-click State Port (IN) again: this time the chooser lists the port already declared, BTN [in, 1 bit]. Pick it: the same machine input is reused in this state.

    Reusing a State Port

    Figure 7: reusing BTN in the second state

  3. + Exit Condition → ON_IDLE, then wire BTN to it, as in step 4.

  4. Place a True/False constant (search “Bit Constant” or “True/False”, keep the value True).

  5. Right-click its output pin and choose Connect to new State Port; name it LED. An output State Port is placed next to the pin and wired. In IDLE nothing drives LED, so LED is 0 there.

ON body

Figure 8: body of ON: LED = 1, back to IDLE when BTN = 1

The header now shows no problems and both cards show 1/1 exits.

Finished machine

Figure 9: the finished machine

Step 6 - Place the machine in the design

  1. Open the top page (double-click it in the tree).
  2. Search State Machine (new) in the Blocks panel and double-click it, select fsm_led and press OK.
  3. Connect BTN and LED (for example to a debounced button and to a digital output); CLK and RESET may stay unconnected (default clock and reset). STATE_OUT and TRANSITION_OUT are optional.

Double-click the placed block whenever you want to edit the machine again. Save the project and compile it as usual: the compiler generates FSM_fsm_led and one body entity per state (see Generated HDL).


Going Further