2007年07月11日
VHDLでC言語の標準関数を使う
VHDLでC言語の標準関数に近いライブラリがココにある。
制約は多いが使い勝手がよく、私はModelSimにライブラリ登録して使用している。
以下はprintfを使用した例だ。
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
library c;
use c.stdio_h.all;
library modelsim_lib;
use modelsim_lib.util.all;
entity test_printf is
end test_printf;
architecture behavior of test_printf is
signal STV8 : std_logic_vector(7 downto 0);
begin
process begin
-- これはModelSimのファンクション
signal_force( "STV8", "00000000", 0 ns, deposit, -1 ms, 0 );
wait;
end process;
process begin
wait for 100 ns;
STV8 <= STV8 + '1';
printf("abc");
printf("def\n");
printf("std_logic_vector=0x%x(%d)\n", STV8, STV8);
end process;
end behavior;
これをModelSimでシミュレーションするとTranscriptウィンドウに次のように表示される。
# abcdef
# std_logic_vector=0x0(0)
# abcdef
# std_logic_vector=0x1(1)
〜
# abcdef
# std_logic_vector=0x12(18)
# abcdef
# std_logic_vector=0x13(19)
注意として、環境によってはModelSimでFatalエラーが出るときがある。
この時は、Fatalエラーが出た行(ライブラリの「regexp_h.vhd」内)の
t(character'pos(c)):=tset;
を次のように書き換え
tmpint := character'pos(c);
t(tmpint):=tset;
さらにprocedureの最初に次の変数定義を入れればOKとなる。
variable tmpint: integer;
これはたぶんModelSimの問題だと思う。
この記事へのコメント
これは便利ですね。参考にさせていただきます。
すごく便利でおもしろいので遊んでいます。
ただ、多用するとシミュレーションが劇的に遅くなってしまうのが難ですが...