Main Content

Model a State Machine for HDL and High-Level Synthesis Code Generation

The following design pattern shows MATLAB® examples of Mealy and Moore state machines which are suitable for HDL and High-Level Synthesis (HLS) code generation.

The MATLAB code in these models demonstrates best practices for writing MATLAB models for HDL and HLS code generation.

  • With a switch block, use the otherwise statement to ensure that the model accounts for all conditions. If the model does not cover all conditions, the generated HDL code can contain errors.

  • To designate the states in a state machine, use variables with numerical values.

MATLAB Code for the Mealy State Machine

In a Mealy state machine, the output depends on the state and the input. In a Moore state machine, the output depends only on the state.

The following MATLAB code defines the mlhdlc_fsm_mealy function. A persistent variable represents the current state. A switch block uses the current state and input to determine the output and new state. In each case in the switch block, an if-else statement calculates the new state and output.

Mealy State Machine

The MATLAB function mlhdlc_fsm_mealy and test bench mlhdlc_fsm_mealy_tb implements a Mealy state machine, where the output Z is determined by both the current state and input A. It transitions between four states (S1 to S4) based on input A, updating the state and output accordingly.

MATLAB function

%#codegen
% Mealy State Machine
function Z = mlhdlc_fsm_mealy(A)

% define states
S1 = 0;
S2 = 1;
S3 = 2;
S4 = 3;

persistent current_state;
if isempty(current_state)
    current_state = S1;   
end

% switch to new state based on the value state register
switch (current_state)
    case S1,
        % value of output 'Z' depends both on state and inputs
        if (A)
            Z = true;
            current_state = S1;
        else
            Z = false;            
            current_state = S2;
        end
    case S2,
        if (A)
            Z = false;
            current_state = S3;
        else
            Z = true;
            current_state = S2;
        end
    case S3,
        if (A)
            Z = false;            
            current_state = S4;
        else
            Z = true;            
            current_state = S1;
        end
    case S4,
        if (A)
            Z = true;
            current_state = S1;
        else
            Z = false;            
            current_state = S3;
        end         
    otherwise,
        Z = false;
end

MATLAB Test Bench

for i = 1:100
    if mod(i,2) == 0
        val = mlhdlc_fsm_mealy(true);
    else
        val = mlhdlc_fsm_mealy(false);
    end
end

Use these commands to HLS generate code for the MATLAB function mlhdlc_fsm_mealy.

cfg = coder.config("hdl");
cfg.Workflow = "High Level Synthesis"; % To generate HDL code, set Workflow to "Generic ASIC/FPGA"
cfg.TestBenchName = "mlhdlc_fsm_mealy_tb";
codegen -config cfg mlhdlc_fsm_mealy -report

MATLAB Code for the Moore State Machine

The following MATLAB code defines the mlhdlc_fsm_moore function. A persistent variable represents the current state, and a switch block uses the current state to determine the output and new state. In each case in the switch block, an if-else statement calculates the new state and output. The value of the state is represented by numerical variables.

MATLAB Code

The MATLAB function mlhdlc_fsm_moore and test bench mlhdlc_fsm_moore_tb implements a Moore state machine, where the output Z depends solely on the current state, transitioning between four states (S1 to S4) based on input A. The persistent curr_state variable models the state register for hardware synthesis.

MATLAB function

%#codegen
function Z = mlhdlc_fsm_moore(A)
% Moore State Machine

% all actions are state actions and 
% outputs are pure functions of state only 

% define states
S1 = 0;
S2 = 1;
S3 = 2;
S4 = 3;

% using persistent keyword to model state registers in hardware
persistent curr_state;
if isempty(curr_state)
    curr_state = S1;   
end

% switch to new state based on the value state register
switch (curr_state)
    case S1,
        % value of output 'Z' depends only on state and not on inputs
        Z = true;
        
        % decide next state value based on inputs
        if (~A)
            curr_state = S1;
        else
            curr_state = S2;
        end
    case S2,
        Z = false;
        if (~A)
            curr_state = S1;
        else
            curr_state = S3;
        end
    case S3,
        Z = false;
        if (~A)
            curr_state = S2;
        else
            curr_state = S4;
        end
    case S4,
        Z = true;
        if (~A)
            curr_state = S3;
        else
            curr_state = S1;
        end
    otherwise,
        Z = false;
end

MATLAB Test Bench

for i = 1:100
    if mod(i,2) == 0
        val = mlhdlc_fsm_moore(true);
    else
        val = mlhdlc_fsm_moore(false);
    end
end

Use these commands to HLS generate code for the MATLAB function mlhdlc_fsm_moore.

cfg = coder.config("hdl");
cfg.Workflow = "High Level Synthesis"; % To generate HDL code, set Workflow to "Generic ASIC/FPGA"
cfg.TestBenchName = "mlhdlc_fsm_moore_tb";
codegen -config cfg mlhdlc_fsm_moore -report

See Also

|

Topics