このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
非回復平方根 System object からの HDL コード生成
この例では、非回復平方根 System object™ をインスタンス化する MATLAB® コードをチェックし、そのコードから HDL コードを生成して検証する方法を示します。
MATLAB 設計
この例で使用する MATLAB コードは、FPGA または ASIC での実装に適した非回復平方根エンジンです。エンジンは、System object として実装される、[1] 判定畳み込み復号化に基づいた、乗算器なしの最小面積実装を使用します。この例では、エンジンをテストする MATLAB テスト ベンチも示します。
design_name = 'mlhdlc_sysobj_nonrestsqrt.m'; testbench_name = 'mlhdlc_sysobj_nonrestsqrt_tb.m'; sysobj_name = 'mlhdlc_msysobj_nonrestsqrt.m';
MATLAB 設計を確認します。
type(design_name);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MATLAB design: Non-restoring Square Root % % Key Design pattern covered in this example: % (1) Using a user-defined system object % (2) Call the object only once per design iteration %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [Q_o,Vld_o] = mlhdlc_sysobj_nonrestsqrt(D_i, Init_i) % Copyright 2014-2015 The MathWorks, Inc. persistent hSqrt; if isempty(hSqrt) hSqrt = mlhdlc_msysobj_nonrestsqrt(); end [Q_o,Vld_o] = hSqrt(D_i,Init_i); end
type(testbench_name);
% Nonrestoring Squareroot Testbench % Copyright 2014-2015 The MathWorks, Inc. % Generate some random data rng('default'); % set the random number generator to a consistent state nsamp = 100; %number of samples nbits = 32; % fixed-point word length nfrac = 31; % fixed-point fraction length data_i = fi(rand(1,nsamp), numerictype(0,nbits,nfrac)); % clear any persistent variables in the HDL function clear mlhdlc_sysobj_nonrestsqrt % Determine the "golden" sqrt results data_go = sqrt(data_i); % Commands for the sqrt engine LOAD_DATA = true; CALC_DATA = false; % Pre-allocate the result array data_o = zeros(1,nsamp, 'like', data_go); % Load in a sample, then iterate until the results are ready cyc_cnt = 0; for i = 1:nsamp % Load the new sample into the sqrt engine [~, vld] = mlhdlc_sysobj_nonrestsqrt(data_i(i),LOAD_DATA); cyc_cnt = cyc_cnt + 1; while(vld == false) % Iterate until the result has been found [data_o(i), vld] = mlhdlc_sysobj_nonrestsqrt(data_i(i),CALC_DATA); cyc_cnt = cyc_cnt + 1; end end % find the integer representation of the result data idt = numerictype(0,ceil(nbits/2),0); % find the error in terms of integer bits ierr = abs(double(reinterpretcast(data_o,idt))-double(reinterpretcast(data_go,idt))); % find the error in terms of real-world values derr = abs(double(data_o)- double(data_go)); pct_err = 100*derr ./ double(data_go); fprintf('Maximum Error: %d (%0.3f %%)\n', max(derr), max(pct_err)); fprintf('Maximum Error (as unsigned integer): %d\n', max(ierr)); fprintf('Number of cycles: %d ( %d per sample)\n', cyc_cnt, cyc_cnt / nsamp); %EOF
設計のシミュレーション
コードの生成前にテストベンチを使用して設計をシミュレートし、実行時エラーが発生しないことを確認します。
mlhdlc_sysobj_nonrestsqrt_tb
Maximum Error: 3.051758e-05 (0.028 %) Maximum Error (as unsigned integer): 1 Number of cycles: 2000 ( 20 per sample)
非回復平方根アルゴリズムのハードウェア実装
このアルゴリズムでは、(mux を必要とする回復アルゴリズムとは異なり) mux なしで単一の加算器/減算器を使用して最小面積の平方根演算を実装します。平方根は、一連のシフトと加算/減算を使用して計算されるため、(乗算器を必要とする他の実装とは異なり) 乗算器は使用されません。
このアルゴリズムの全体的なアーキテクチャを以下に示します。詳細については、[1] を参照してください。
このアルゴリズムの実装では、各結果を計算するために複数のサイクルを必要とする最小面積法を使用しています。全体的な計算時間は、[入力語長 / 2] として近似できます。入力データを読み込むために数サイクルのオーバーヘッドがあります。
HDL Coder プロジェクトの新規作成
新しいプロジェクトを作成するには、次のコマンドを入力します。
coder -hdlcoder -new mlhdlc_nonrestsqrt
次に、mlhdlc_sysobj_nonrestsqrt.m
ファイルを MATLAB 関数としてプロジェクトに追加し、mlhdlc_sysobj_nonrestsqrt_tb.m
を MATLAB テスト ベンチとして追加します。
MATLAB HDL Coder プロジェクトの作成と入力に関する詳細なチュートリアルについては、MATLAB から HDL へのワークフロー入門を参照してください。
固定小数点変換のスキップ
設計は既に固定小数点であるため、自動変換を実行する必要はありません。
HDL アドバイザーを起動し、オプション [固定小数点の変換] で [元の型を保持]
を選択します。
HDL コード生成の実行
ワークフロー アドバイザーを起動します。ワークフロー アドバイザーで、[コード生成] のステップを右クリックして [選択したタスクまで実行] オプションを選択し、最初から HDL コード生成までのすべてのステップを実行します。
ログ ウィンドウにあるリンクをクリックして、生成された HDL コードを確認します。
サポートされている System object
HDL コード生成でサポートされている System object のリストについては、HDL コード生成に使用できる事前定義済みの System Objectsを参照してください。
参照
[1] Li, Y. and Chu, W. (1996) "A New Non-Restoring Square Root Algorithm and Its VLSI Implementations".IEEE International Conference on Computer Design: VLSI in Computers and Processors, ICCD '96 Austin, Texas USA (7-9 October, 1996), pp. 538-544. doi: 10.1109/ICCD.1996.563604