Main Content

Model a State Machine for HDL and SystemC Code Generation

The following design pattern shows MATLAB® examples of Mealy and Moore state machines which are suitable for HDL and SystemC code generation.

The MATLAB code in these models demonstrates best practices for writing MATLAB models for HDL and SystemC 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.

MATLAB Code

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

% y = f(x,u) : 
% all actions are condition actions and 
% outputs are function of state and input 

% 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

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

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

% y = f(x) : 
% 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

See Also

|

Related Topics