送受信 FIFO レジスタ
この例では、送受信先入れ先出し (FIFO) レジスタまたはバッファー間のデータ転送をモデル化する MATLAB® コードから HDL コードを生成する方法を示します。この例には受信 FIFO バッファーと送信 FIFO バッファーを表す 2 つの関数と、2 つのバッファー間で行われるデータ転送をシミュレートするテスト ベンチ mlhdlc_fifo_tb が含まれています。各関数はハードウェアに対応しており、効率的な HDL コードを生成するための MATLAB 関数を記述する際に従うべき適切な手法とガイドラインを示しています。従うべきガイドラインの詳細については、効率的な HDL コードおよび HLS コードを生成するための MATLAB コードの記述に関するガイドラインを参照してください。
関数の例およびテスト ベンチの表示
送信 FIFO および受信 FIFO の MATLAB 設計を開きます。
type('mlhdlc_rx_fifo');
function [dout, empty, byte_ready, full, bytes_available] = ...
mlhdlc_rx_fifo(get_byte, store_byte, byte_in, reset_fifo, fifo_enable)
%
% Copyright 2014-2015 The MathWorks, Inc.
%
% First In First Out (FIFO) structure.
% This FIFO stores integers.
% The FIFO is actually a circular buffer.
%
persistent head tail fifo byte_out handshake
if (reset_fifo || isempty(head))
head = 1;
tail = 2;
byte_out = 0;
handshake = 0;
end
if isempty(fifo)
fifo = zeros(1,1024);
end
full = 0;
empty = 0;
byte_ready = 0;
% Section for checking full and empty cases
if ((tail == 1 && head == 1024) || ((head + 1) == tail))
empty = 1;
end
if ((head == 1 && tail == 1024) || ((tail + 1) == head))
full = 1;
end
% handshaking logic
if get_byte == 0
handshake = 0;
end
if handshake == 1
byte_ready = 1;
end
if (fifo_enable == 1)
%%%%%%%%%%%%%%get%%%%%%%%%%%%%%%%%%%%%
if (get_byte && ~empty && handshake == 0 )
head = head + 1;
if head == 1025
head = 1;
end
byte_out = fifo(head);
byte_ready = 1;
handshake = 1;
end
%%%%%%%%%%%%%put%%%%%%%%%%%%%%%%%%%%%
if (store_byte && ~full)
fifo(tail) = byte_in;
tail = tail + 1;
if tail == 1025
tail = 1;
end
end
end
% Section for calculating num bytes in FIFO
if (head < tail)
bytes_available = (tail - head) - 1;
else
bytes_available = (1024 - head) + tail - 1;
end
dout = byte_out;
end
type('mlhdlc_tx_fifo');
function [dout, empty, byte_received, full, bytes_available, dbg_fifo_enable] = ...
mlhdlc_tx_fifo(get_byte, store_byte, byte_in, reset_fifo, fifo_enable)
%
% Copyright 2014-2015 The MathWorks, Inc.
%
% First In First Out (FIFO) structure.
% This FIFO stores integers.
% The FIFO is actually a circular buffer.
%
persistent head tail fifo byte_out handshake
if (reset_fifo || isempty(head))
head = 1;
tail = 2;
byte_out = 0;
handshake = 0;
end
if isempty(fifo)
fifo = zeros(1,1024);
end
full = 0;
empty = 0;
byte_received = 0;
% Section for checking full and empty cases
if ((tail == 1 && head == 1024) || ((head + 1) == tail))
empty = 1;
end
if ((head == 1 && tail == 1024) || ((tail + 1) == head))
full = 1;
end
% handshaking logic
if store_byte == 0
handshake = 0;
end
if handshake == 1
byte_received = 1;
end
if (fifo_enable == 1)
%%%%%%%%%%%%%%get%%%%%%%%%%%%%%%%%%%%%
if (get_byte && ~empty)
head = head + 1;
if head == 1025
head = 1;
end
byte_out = fifo(head);
end
%%%%%%%%%%%%%put%%%%%%%%%%%%%%%%%%%%%
if (store_byte && ~full && handshake == 0)
fifo(tail) = byte_in;
tail = tail + 1;
if tail == 1025
tail = 1;
end
byte_received = 1;
handshake = 1;
end
end
% Section for calculating num bytes in FIFO
if (head < tail)
bytes_available = (tail - head) - 1;
else
bytes_available = (1024 - head) + tail - 1;
end
dout = byte_out;
dbg_fifo_enable = fifo_enable;
end
両方の設計の動作を確認するテスト ベンチの MATLAB 設計を開きます。このテスト ベンチは送信 FIFO と受信 FIFO の両方をテストします。ただし、HDL コードの生成時には、送受信 FIFO の個別の関数があるため、両方の関数をテストしてコードを生成するには個別のテスト ベンチが必要です。シミュレーション目的の場合は、mlhdlc_fifo_tb を使用できますが、HDL コード生成の場合は、受信 FIFO テスト ベンチ mlhdlc_rx_fifo_tb を受信 FIFO 関数 mlhdlc_rx_fifo と共に使用し、送信 FIFO テスト ベンチ mlhdlc_tx_fifo を送信 FIFO 関数 mlhdlc_tx_fifo と共に使用します。
type('mlhdlc_fifo_tb');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% simulation parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% data payload creation
% Copyright 2014-2015 The MathWorks, Inc.
messageASCII = 'Hello World!';
message = double(unicode2native(messageASCII));
msgLength = length(message);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TX_FIFO core
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numBytesToFifo = 1;
tx_get_byte = 0;
tx_full = 0;
tx_byte_received = 0;
i1 = 1;
while (numBytesToFifo <= msgLength && ~tx_full)
% first thing the processor does is clear the internal tx fifo
if i1 == 1
tx_reset_fifo = 1;
mlhdlc_tx_fifo(0, 0, 0, tx_reset_fifo, 1);
else
tx_reset_fifo = 0;
end
if (i1 > 1)
tx_data_in = message(numBytesToFifo);
numBytesToFifo = numBytesToFifo + 1;
tx_store_byte = 1;
while (tx_byte_received == 0)
[tx_data_out, tx_empty, tx_byte_received, tx_full, tx_bytes_available] = ...
mlhdlc_tx_fifo(tx_get_byte, tx_store_byte, tx_data_in, tx_reset_fifo, 1);
end
tx_store_byte = 0;
while (tx_byte_received == 1)
[tx_data_out, tx_empty, tx_byte_received, tx_full, tx_bytes_available] = ...
mlhdlc_tx_fifo(tx_get_byte, tx_store_byte, tx_data_in, tx_reset_fifo, 1);
end
end
i1 = i1 + 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Transfer Bytes from TX FIFO to RX FIFO
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i1 = 1;
tx_get_byte = 0;
tx_store_byte = 0;
tx_data_in = 0;
tx_reset_fifo = 0;
rx_get_byte = 0;
rx_data_in = 0;
rx_reset_fifo = 0;
while (tx_bytes_available > 0)
if i1 == 1
rx_reset_fifo = 1;
mlhdlc_rx_fifo(0, 0, 0, rx_reset_fifo, 1);
else
rx_reset_fifo = 0;
end
if (i1 > 1)
tx_get_byte = 1;
rx_store_byte = 1;
[tx_data_out, tx_empty, tx_byte_received, tx_full, tx_bytes_available] = ...
mlhdlc_tx_fifo(tx_get_byte, tx_store_byte, tx_data_in, tx_reset_fifo, 1);
rx_data_in = tx_data_out;
[rx_data_out, rx_empty, rx_byte_ready, rx_full, rx_bytes_available] = ...
mlhdlc_rx_fifo(rx_get_byte, rx_store_byte, rx_data_in, rx_reset_fifo, 1);
end
i1 = i1 + 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RX_FIFO core
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numBytesFromFifo = 1;
rx_store_byte = 0;
rx_byte_received = 0;
i1 = 1;
msgBytes = zeros(1,msgLength);
while (~rx_empty)
% first thing the processor does is clear the internal rx fifo
if (i1 > 1)
rx_get_byte = 1;
while (rx_byte_ready == 0)
[rx_data_out, rx_empty, rx_byte_ready, rx_full, rx_bytes_available] = ...
mlhdlc_rx_fifo(rx_get_byte, rx_store_byte, rx_data_in, rx_reset_fifo, 1);
end
msgBytes(i1-1) = rx_data_out;
rx_get_byte = 0;
while (rx_byte_ready == 1)
[rx_data_out, rx_empty, rx_byte_ready, rx_full, rx_bytes_available] = ...
mlhdlc_rx_fifo(rx_get_byte, rx_store_byte, rx_data_in, rx_reset_fifo, 1);
end
end
i1 = i1 + 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numRecBytes = numBytesFromFifo;
if sum(msgBytes-message) == 0
disp('Received message correctly');
else
disp('Received message incorrectly');
end
native2unicode(msgBytes)
設計のシミュレーション
コードの生成前にテスト ベンチを使用して設計をシミュレートし、実行時エラーがないことを確認します。
mlhdlc_fifo_tb
Received message correctly
ans =
'Hello World!'
受信 FIFO に対する HDL Coder プロジェクトの新規作成
MATLAB コマンド プロンプトで次のコマンドを実行し、受信 FIFO に対する HDL Coder プロジェクトを新規作成します。
coder -hdlcoder -new mlhdlc_rx_fifo
[HDL コード生成] ペインが開いたら、HDL コードを生成する MATLAB 関数として、関数 mlhdlc_rx_fifo.m を設定します。mlhdlc_rx_fifo_tb.m スクリプトを MATLAB テスト ベンチとして設定します。[ワークフロー アドバイザー] をクリックします。
MATLAB HDL Coder™ プロジェクトの作成と入力に関する詳細なチュートリアルについては、MATLAB から HDL へのワークフロー入門を参照してください。
受信 FIFO に対する HDL コードの生成
[HDL コード生成] を右クリックし、[選択したタスクまで実行] を選択して、最初から HDL コード生成までのすべてのステップを実行します。
下部のペインにあるハイパーリンクをクリックして、受信 FIFO に対して生成された HDL コードを確認します。
送信 FIFO に対する HDL Coder プロジェクトの新規作成
coder -hdlcoder -new mlhdlc_tx_fifo
[HDL コード生成] ペインが開いたら、送信 FIFO 関数に対して HDL コードを生成する MATLAB 関数として mlhdlc_tx_fifo.m を指定し、MATLAB テスト ベンチとして mlhdlc_tx_fifo_tb.m を指定します。[ワークフロー アドバイザー] をクリックします。
送信 FIFO に対する HDL コードの生成
[HDL コード生成] を右クリックし、[選択したタスクまで実行] を選択して、最初から HDL コード生成までのすべてのステップを実行します。
下部のペインにあるハイパーリンクをクリックして、送信 FIFO に対して生成された HDL コードを確認します。