Codiode

The Implicit State Machine

MediumVerilog / SystemVerilogBuild

Control logic built from a growing set of flags (is_active, is_waiting, …) is a state machine in disguise. Writing it as an explicit FSM makes the states and transitions visible.

The target module manages a memory transaction sequence. It rests in an idle state until a start signal is received. It then asserts a read enable for one cycle, waits for an acknowledgment, asserts a write enable for one cycle, signals completion for one cycle, and returns to idle. If an error occurs during the wait phase, it traps in an error state until the start signal is deasserted.

The original design used six individual flags (flag_idle, flag_read, flag_wait, flag_write, flag_done, flag_error). The new specification requires a single 3-bit state register to handle the exact same cycle timing.

  • Clock edge: posedge clk
  • Reset type: Asynchronous, active-low (negedge rst_n)
  • On reset, the module enters the IDLE state and all control outputs (read_en, write_en, done, busy) are 0.
  • State transitions occur on posedge clk.
  • Outputs are combinational based purely on the current state (Moore machine).
  • busy is 1 in all states except IDLE.
  • Priority in WAIT state: err has priority over ack.

Cycle 1: rst_n=0 → state_out=0, busy=0, read_en=0, write_en=0, done=0
Cycle 2: rst_n=1, start=1 → state_out=1, busy=1, read_en=1, write_en=0, done=0
Cycle 3: start=0, ack=0, err=0 → state_out=2, busy=1, read_en=0, write_en=0, done=0
Cycle 4: ack=1 → state_out=3, busy=1, read_en=0, write_en=1, done=0
Cycle 5: ack=0 → state_out=4, busy=1, read_en=0, write_en=0, done=1
Cycle 6: none → state_out=0, busy=0, read_en=0, write_en=0, done=0

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset
start input 1 Initiates a transaction from IDLE
ack input 1 Acknowledges the read, allows transition to WRITE
err input 1 Flags an error during the WAIT state
busy output 1 High in all states except IDLE
read_en output 1 High only in the READ state
write_en output 1 High only in the WRITE state
done output 1 High only in the DONE state
state_out output 3 Current FSM state

Constraints

  • The state_out port must strictly follow this 3-bit encoding: IDLE (3'b000), READ (3'b001), WAIT (3'b010), WRITE (3'b011), DONE (3'b100), ERROR (3'b101).
  • Outputs busy, read_en, write_en, and done must be driven combinationally based on the current state.
  • Transitions from READ to WAIT, WRITE to DONE, and DONE to IDLE are unconditional.
  • If err and ack are asserted simultaneously in the WAIT state, the FSM must transition to ERROR.

Topics

FSMControl LogicRefactoring

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Synthesis Intuition

Problem 18 of 25 in the Synthesis Intuition track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.