Codiode

Typedef Enum State Machine

EasyVerilog / SystemVerilogBuildFree

State names like IDLE and READ make an FSM readable in code and in the waveform viewer. A typedef enum gives you the names and stops a stray integer from being assigned to the state.

The module is a simple memory access FSM that arbitrates between read and write requests. It waits in an idle state. When a read request arrives, it transitions to a read state and grants read access. If no read request exists but a write request is active, it transitions to a write state and grants write access. Read requests have strict priority over write requests. The FSM remains in the active state until its corresponding request is de-asserted, at which point it unconditionally returns to the idle state before serving any new requests.

Positive-edge triggered clock clk and asynchronous active-low reset rst_n govern the module. On reset, the state machine enters the idle state and all grants are de-asserted. Priority is evaluated only when transitioning out of the idle state.

Worked Trace

Cycle 1: rst_n=0 → current_state=0 (IDLE), grant_read=0, grant_write=0
Cycle 2: rst_n=1, req_read=0, req_write=1 → current_state=2 (WRITE), grant_write=1
Cycle 3: req_read=1, req_write=1 → current_state=2 (WRITE) (hold active state)
Cycle 4: req_write=0, req_read=0 → current_state=0 (IDLE) (return to idle)
Cycle 5: req_read=1, req_write=0 → current_state=1 (READ), grant_read=1
Cycle 6: req_read=1, req_write=1 → current_state=1 (READ) (hold active state)

State Diagram

Timing Diagram

Port Table

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; state goes to IDLE when asserted
req_read input 1 Request for read access
req_write input 1 Request for write access
grant_read output 1 Asserted when FSM is in the READ state
grant_write output 1 Asserted when FSM is in the WRITE state
current_state output 2 Current state encoding (0=IDLE, 1=READ, 2=WRITE)

Constraints

  • The design must use a strongly typed enum for the state variable.
  • The enum definition must be: typedef enum logic [1:0] {IDLE = 2'b00, READ = 2'b01, WRITE = 2'b10} state_t;
  • SystemVerilog strictly enforces typed enums. You cannot assign an untyped integer (e.g., 2'b01) directly to a variable of type state_t. You must use the enum labels (e.g., READ).
  • Read requests have strict priority over write requests when evaluated from the IDLE state.
  • Once in READ or WRITE, the FSM must ignore new requests until the active request drops and the FSM returns to IDLE.
  • Outputs grant_read and grant_write are combinational functions of the current state (Moore outputs).

Topics

FSMSystemVerilogEnumsTypedef

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.

Free to solve. A Codiode account keeps your progress.

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 FSM Coding

Problem 3 of 15 in the FSM Coding track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

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