2.GHDLは、foreign宣言でCプログラムを呼び出せます。例えば、以下のCプログラムを動作させてみます。ファイル名は、helloc.cとしています。
#include
int helloc(void)
{
printf ("Hello! C...\n");
return 1;
}
次のようなVHDLファイルをhello_world.vhdlとして作成してみました。
package test is
function helloc return integer;
attribute foreign of helloc: function is
"VHPIDIRECT helloc";
end test;
package body test is
function helloc return integer is
begin
assert false severity failure;
end helloc;
end test;
use std.textio.all;
use work.test.helloc;
entity hello_world is
end hello_world;
architecture beh of hello_world is
begin
process
variable l : line;
variable v : integer;
begin
l := new string'("Hello! VHDL...");
writeline (output,l);
--invite to a function of C
v:=helloc;
wait;
end process;
end beh;
Makefileを以下のように作ります。
GHDL=ghdl
CC=gcc
all: hello_world
hello_world: test.o helloc.o
$(GHDL) -e -Wl,helloc.o hello_world
test.o: hello_world.vhdl
$(GHDL) -a $<
clean:
$(RM) -f hello_world *.o *.cf *~
makeすると、hello_worldの実行ファイルが出来上がります。./hello_worldと入力すると
Hello! VHDL...
Hello! C...
と表示されます。
(参考)
co-simulation
ghdl-simulation
ghdl-cosim
VHDL DPI