工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> VHDL

VHDLにおける不純関数の活用:FSMの可読性と保守性向上

VHDLにおいて、不純関数(impure function)は、パラメータリストに含まれない信号を含む、そのレキシカルスコープ内のあらゆる信号にアクセスまたは変更できます。この能力により、関数に副作用が生じます:同じ引数を与えても戻り値が異なる可能性があり、直接返されない外部信号を変更することもできます。

純粋関数は決定論的振る舞いに限定されますが、不純関数は複雑なステートマシンロジックを整理し、繰り返しや副作用を伴うタスクを単一の適切に命名されたサブプログラムに移動するのに不可欠です。

注意: 不純関数はプロセス内で宣言するのが最適です。アーキテクチャやパッケージレベルで宣言すると、コンパイル時にスコープ内にある信号しかアクセスできず、利点がありません。

不純関数を使う場面と理由

典型的なシナリオ:

不純関数を使用すると、コアステートマシンロジックを可読に保ちながら、設計に必要な副作用のある振る舞いを維持できます。

構文

不純関数を宣言するには、functionの前にimpureキーワードを付けるだけです:

impure function CounterExpired(Minutes : integer := 0; Seconds : integer := 0) return boolean;

詳細な構文は関数チュートリアルを参照してください。

演習:不純関数によるFSMリファクタリング

以前は純粋関数CounterValで時間遅延を計算し、タイマー期限時に手動でカウンタをリセットしていました。以下の不純関数でこのロジックを統合します:

テストベンチ(T22_ImpureFunctionTb)

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity T22_ImpureFunctionTb is
end entity;
architecture sim of T22_ImpureFunctionTb is
  -- シミュレーション高速化のため低クロック周波数
  constant ClockFrequencyHz : integer := 100; -- 100 Hz
  constant ClockPeriod : time := 1000 ms / ClockFrequencyHz;
  signal Clk : std_logic := '1';
  signal nRst : std_logic := '0';
  signal NorthRed : std_logic;
  signal NorthYellow : std_logic;
  signal NorthGreen : std_logic;
  signal WestRed : std_logic;
  signal WestYellow : std_logic;
  signal WestGreen : std_logic;
begin
  -- DUTインスタンス化
  i_TrafficLights : entity work.T22_TrafficLights(rtl)
    generic map(ClockFrequencyHz => ClockFrequencyHz)
    port map (
      Clk => Clk,
      nRst => nRst,
      NorthRed => NorthRed,
      NorthYellow => NorthYellow,
      NorthGreen => NorthGreen,
      WestRed => WestRed,
      WestYellow => WestYellow,
      WestGreen => WestGreen
    );
  -- クロック生成
  Clk <= not Clk after ClockPeriod / 2;
  -- テストベンチシーケンス
  process is
  begin
    wait until rising_edge(Clk);
    wait until rising_edge(Clk);
    -- リセット解除
    nRst <= '1';
    wait;
  end process;
end architecture;

信号機モジュール(T22_TrafficLights)

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity T22_TrafficLights is
  generic(ClockFrequencyHz : integer);
  port(
    Clk : in std_logic;
    nRst : in std_logic; -- 負論理リセット
    NorthRed : out std_logic;
    NorthYellow : out std_logic;
    NorthGreen : out std_logic;
    WestRed : out std_logic;
    WestYellow : out std_logic;
    WestGreen : out std_logic
  );
end entity;
architecture rtl of T22_TrafficLights is
  -- 指定時間のクロックサイクル数を計算する純粋関数
  function CounterVal(Minutes : integer := 0; Seconds : integer := 0) return integer is
    variable TotalSeconds : integer;
  begin
    TotalSeconds := Seconds + Minutes * 60;
    return TotalSeconds * ClockFrequencyHz - 1;
  end function;
  -- FSM状態定義
  type t_State is (NorthNext, StartNorth, North, StopNorth, WestNext, StartWest, West, StopWest);
  signal State : t_State;
  -- クロック周期カウンタ、最大1分
  signal Counter : integer range 0 to ClockFrequencyHz * 60;
begin
  process(Clk) is
    -- Counterを読み書きする不純関数
    impure function CounterExpired(Minutes : integer := 0; Seconds : integer := 0) return boolean is
    begin
      if Counter = CounterVal(Minutes, Seconds) then
        Counter <= 0;
        return true;
      else
        return false;
      end if;
    end function;
  begin
    if rising_edge(Clk) then
      if nRst = '0' then
        -- リセット状態と信号
        State <= NorthNext;
        Counter <= 0;
        NorthRed <= '1'; NorthYellow <= '0'; NorthGreen <= '0';
        WestRed <= '1'; WestYellow <= '0'; WestGreen <= '0';
      else
        -- デフォルト出力
        NorthRed <= '0'; NorthYellow <= '0'; NorthGreen <= '0';
        WestRed <= '0'; WestYellow <= '0'; WestGreen <= '0';
        Counter <= Counter + 1;
        case State is
          when NorthNext =>
            NorthRed <= '1'; WestRed <= '1';
            if CounterExpired(Seconds => 5) then State <= StartNorth; end if;
          when StartNorth =>
            NorthRed <= '1'; NorthYellow <= '1'; WestRed <= '1';
            if CounterExpired(Seconds => 5) then State <= North; end if;
          when North =>
            NorthGreen <= '1'; WestRed <= '1';
            if CounterExpired(Minutes => 1) then State <= StopNorth; end if;
          when StopNorth =>
            NorthYellow <= '1'; WestRed <= '1';
            if CounterExpired(Seconds => 5) then State <= WestNext; end if;
          when WestNext =>
            NorthRed <= '1'; WestRed <= '1';
            if CounterExpired(Seconds => 5) then State <= StartWest; end if;
          when StartWest =>
            NorthRed <= '1'; WestRed <= '1'; WestYellow <= '1';
            if CounterExpired(Seconds => 5) then State <= West; end if;
          when West =>
            NorthRed <= '1'; WestGreen <= '1';
            if CounterExpired(Minutes => 1) then State <= StopWest; end if;
          when StopWest =>
            NorthRed <= '1'; WestYellow <= '1';
            if CounterExpired(Seconds => 5) then State <= NorthNext; end if;
        end case;
      end if;
    end if;
  end process;
end architecture;

波形の洞察

ModelSimでrun 5 minコマンドを実行すると、元のFSMと同一の振る舞いを示します。コードのみがクリーンになりました。カウンタリセットロジックがCounterExpiredにカプセル化され、重複と潜在的エラーが減少しました。

VHDLにおける不純関数の活用:FSMの可読性と保守性向上

分析

不純関数により、カウンタ比較とリセットが複数のFSM状態から単一サブプログラムに移されました。可読性が向上:CounterExpired(Seconds => 5)は意図を明確に伝えますが、Counter = CounterVal(Seconds => 5)は冗長です。

不純関数は乱用すると副作用を隠蔽し得ますが、プロセスローカルに保つことで周囲のコードは決定論的で監査しやすくなります。実際には、合成可能RTLよりもテストベンチでより頻繁に使用されます。テストベンチでは正確さが重要ですが、巧妙なバグのリスクはRTLより低いためです。

VHDLにおける不純関数の活用:FSMの可読性と保守性向上

重要なポイント

次のチュートリアルで高度なVHDLテクニックを探求します。

VHDL

  1. カッターグラインダー技術の習得:実践ガイド
  2. モリブデンが現代産業を形作る仕組み:用途、利点、需給動向
  3. C言語のrealloc()活用ガイド:構文、ベストプラクティス、実例
  4. C言語free()関数完全マスター:実践ガイドと例
  5. VHDLで文字列リストを作成する: ベスト・プラクティスと実装例
  6. VHDLテストベンチ・シミュレーションを優雅に終了させる方法
  7. VHDLでPWMコントローラを実装: 設計、シミュレーション、FPGAデモ
  8. VHDLで乱数生成: 一様分布からOSVVMまで
  9. プロセス内プロシージャでVHDLステートマシンをクリーンに設計
  10. VHDL関数マスター:効率的な設計のための実践ガイド
  11. VHDLで有限ステートマシンを設計:信号機の実践例