MATLAB 設計へのカスタム HDL コードの統合
hdl.BlackBox を使用すると、古い形式のコードや手書きの HDL コードなどのカスタム HDL コードを、HDL コード生成用の MATLAB® 設計に含めることができます。
hdl.BlackBox から継承するユーザー定義の System object™ を作成するときに、カスタム HDL コードに合った端子インターフェイスとシミュレーション動作を指定します。
System object に定義した動作に基づいて、HDL Coder™ により MATLAB 内の設計がシミュレートされます。コードの生成時には、シミュレーションの動作のコードが生成されるのではなく、System object に指定した端子インターフェイスをもつモジュールがコード ジェネレーターでインスタンス化されます。
生成された HDL コードを大規模システムで使用するには、カスタムの HDL ソース ファイルを、生成された残りのコードと共に設計に含めます。
hdl.BlackBox System object の定義
hdl.BlackBoxから継承する、ユーザー定義の System object を作成します。System object の
hdl.BlackBoxプロパティを設定することで、カスタム HDL コードの端子インターフェイスに一致するブラック ボックス インターフェイスを構成します。シミュレーション動作が HDL コードと一致するように、
stepメソッドを定義します。または、
hdl.BlackBoxクラスから継承するように System object を定義し、カスタム HDL コードのシミュレーション動作が一致するようにoutputメソッドとupdateメソッドを定義します。
コード例
次のコードは、CounterBbox という System object を定義します。このオブジェクトは hdl.BlackBox から継承し、しきい値に達するまでインクリメントするカウンターのカスタム HDL コードを表します。CounterBbox の reset メソッドと step メソッドは、カスタム HDL コードの動作をモデル化します。
classdef CounterBbox < hdl.BlackBox % derive from hdl.BlackBox class
%Counter: Count up to a threshold.
%
% This is an example of a discrete-time System object with state
% variables.
%
properties (Nontunable)
Threshold = fi(1, 0, 1, 0, hdlfimath)
end
properties (DiscreteState)
% Define discrete-time states.
Count
end
methods
function obj = CounterBbox(varargin)
% Support name-value pair arguments
setProperties(obj,nargin,varargin{:});
obj.NumInputs = 1; % define number of inputs
obj.NumOutputs = 1; % define number of inputs
end
end
methods (Access=protected)
% Define simulation behavior.
% For code generation, the coder uses your custom HDL code instead.
function resetImpl(obj)
% Specify initial values for DiscreteState properties
obj.Count = fi(0, 0, 8, 0, hdlfimath);
end
function myout = stepImpl(obj, myin)
% Implement algorithm. Calculate y as a function of
% input u and state.
if (myin > obj.Threshold)
obj.Count = fi(obj.Count + fi(1, 0, 1, 0, hdlfimath), 0, 8, 0, hdlfimath);
end
myout = fi(obj.Count, 0, 8, 0, hdlfimath);
end
end
endMATLAB 設計関数での System object の使用
System object を定義後に MATLAB 設計関数で使用するには、そのインスタンスを作成して step メソッドを呼び出します。
コードを生成するには、最上位の設計関数を実行するテスト ベンチも作成する必要があります。
コード例
次のコード例では、CounterBbox のインスタンスを作成して、その step メソッドを呼び出す最上位の設計関数を説明します。
function [y1, y2] = topLevelDesign(u)
persistent mybboxObj myramObj
if isempty(mybboxObj)
mybboxObj = CounterBbox; % instantiate the black box
myramObj = hdl.RAM('RAMType', 'Dual port');
end
y1 = step(mybboxObj, u); % call the system object step method
[~, y2] = step(myramObj, uint8(10), uint8(0), true, uint8(20));次のコード例では、関数 topLevelDesign のテスト ベンチ関数を説明します。
y1 = fi(zeros(1,200), 0, 8, 0, hdlfimath);
y2 = fi(zeros(1,200), 0, 8, 0, hdlfimath);
for ii=1:200
[y1(ii), y2(ii)] = topLevelDesign(fi(ii, 0, 8, 0, hdlfimath));
endHDL コードの生成
設計関数とテスト ベンチ コードを使用して HDL コードを生成します。
hdlcfg = coder.config('hdl');
hdlcfg.TestBenchName = 'testBench';
codegen -config hdlcfg topLevelDesign生成された HDL コードを使用するときには、生成された HDL ファイルをカスタム HDL コードと共に設計に組み込みます。
コード例
次に、CounterBbox 用に生成された VHDL® コードの例を示します。この例では、MATLAB コード内の CounterBbox インスタンスが HDL コンポーネントの定義とインスタンス化にマッピングされていますが、step メソッドの HDL コードは生成されていません。
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;
ENTITY topLevelDesign IS
PORT( clk : IN std_logic;
reset : IN std_logic;
clk_enable : IN std_logic;
u : IN std_logic_vector(7 DOWNTO 0); -- ufix8
ce_out : OUT std_logic;
y1 : OUT std_logic_vector(7 DOWNTO 0); -- ufix8
y2 : OUT std_logic_vector(7 DOWNTO 0) -- uint8
);
END topLevelDesign;
ARCHITECTURE rtl OF topLevelDesign IS
-- Component Declarations
COMPONENT CounterBbox
PORT( clk : IN std_logic;
clk_enable : IN std_logic;
reset : IN std_logic;
myin : IN std_logic_vector(7 DOWNTO 0); -- ufix8
myout : OUT std_logic_vector(7 DOWNTO 0) -- ufix8
);
END COMPONENT;
COMPONENT DualPortRAM_generic
GENERIC( AddrWidth : integer;
DataWidth : integer
);
PORT( clk : IN std_logic;
enb : IN std_logic;
wr_din : IN std_logic_vector(DataWidth - 1 DOWNTO 0); -- generic width
wr_addr : IN std_logic_vector(AddrWidth - 1 DOWNTO 0); -- generic width
wr_en : IN std_logic;
rd_addr : IN std_logic_vector(AddrWidth - 1 DOWNTO 0); -- generic width
wr_dout : OUT std_logic_vector(DataWidth - 1 DOWNTO 0); -- generic width
rd_dout : OUT std_logic_vector(DataWidth - 1 DOWNTO 0) -- generic width
);
END COMPONENT;
-- Component Configuration Statements
FOR ALL : CounterBbox
USE ENTITY work.CounterBbox(rtl);
FOR ALL : DualPortRAM_generic
USE ENTITY work.DualPortRAM_generic(rtl);
-- Signals
SIGNAL enb : std_logic;
SIGNAL varargout_1 : std_logic_vector(7 DOWNTO 0); -- ufix8
SIGNAL tmp : unsigned(7 DOWNTO 0); -- uint8
SIGNAL tmp_1 : unsigned(7 DOWNTO 0); -- uint8
SIGNAL tmp_2 : std_logic;
SIGNAL tmp_3 : unsigned(7 DOWNTO 0); -- uint8
SIGNAL varargout_1_1 : std_logic_vector(7 DOWNTO 0); -- ufix8
SIGNAL varargout_2 : std_logic_vector(7 DOWNTO 0); -- ufix8
BEGIN
-- instantiate the black box
u_CounterBbox : CounterBbox
PORT MAP( clk => clk,
clk_enable => enb,
reset => reset,
myin => u, -- ufix8
myout => varargout_1 -- ufix8
);
u_dualPortRam_generic : DualPortRAM_generic
GENERIC MAP( AddrWidth => 8,
DataWidth => 8
)
PORT MAP( clk => clk,
enb => enb,
wr_din => std_logic_vector(tmp),
wr_addr => std_logic_vector(tmp_1),
wr_en => tmp_2,
rd_addr => std_logic_vector(tmp_3),
wr_dout => varargout_1_1,
rd_dout => varargout_2
);
enb <= clk_enable;
-- call the system object step method
tmp <= to_unsigned(16#0A#, 8);
tmp_1 <= to_unsigned(16#00#, 8);
tmp_2 <= '1';
tmp_3 <= to_unsigned(16#14#, 8);
ce_out <= clk_enable;
y1 <= varargout_1;
y2 <= varargout_2;
END rtl;hdl.BlackBox の制限
hdl.BlackBox を使用して、カスタム HDL コード内の VHDL generic、または Verilog® または SystemVerilog parameter に値を代入することはできません。