Main Content

HDL コード生成および高位合成コード生成のステート マシンのモデル化

次の設計パターンは、MATLAB® において HDL コード生成および高位合成 (HLS) コード生成に適した Mealy および Moore ステート マシンの例を示しています。

これらのモデルの MATLAB コードは、HDL コード生成および HLS コード生成用の MATLAB モデルの作成に関するベスト プラクティスを説明しています。

  • switch ブロックを使用し、otherwise ステートメントにより、モデルがすべての条件に対応することを確認します。モデルがすべての条件に対応していない場合、生成された HDL コードでエラーが発生することがあります。

  • ステート マシンでステートを指定するには、数値が格納される変数を使用します。

Mealy ステート マシン用の MATLAB コード

Mealy ステート マシンでは、出力はステートと入力によって異なります。Moore ステート マシンでは、出力はステートによってのみ決定します。

次の MATLAB コードは関数 mlhdlc_fsm_mealy を定義します。現在のステートは永続変数により表されます。switch ブロックは現在のステートと入力を使用して出力と新しいステートを決定します。switch ブロックのそれぞれの case で、if-else ステートメントにより新しいステートと出力が計算されます。

MATLAB コード

%#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 テスト ベンチ

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

Moore ステート マシン用の MATLAB コード

次の MATLAB コードは関数 mlhdlc_fsm_moore を定義します。現在のステートは永続変数により表され、switch ブロックは現在のステートを使用して出力と新しいステートを決定します。switch ブロックのそれぞれの case で、if-else ステートメントにより新しいステートと出力が計算されます。ステートの値は数値変数により表されます。

MATLAB コード

%#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 テスト ベンチ

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

参考

|

関連するトピック