プログラミング言語VHDL
VHDLがハードウェア記述言語には思えなかったので遊んでみました。
処理系はGHDLです。LinuxですとコンパイルしてGNATともリンクできるっぽいのですが、Windowsですと無理そう。まあ遊ぶだけなら支障無し。
まずはHello World。
use std.textio.all; entity hello_world is end hello_world; architecture behaviour of hello_world is begin process variable l : line; begin write (l, String'("Hello world!")); writeline (output, l); wait; end process; end behaviour;
waitを外すと、エラー発生までprocessブロックを延々繰り返します。AWKを思い起こしますが、Ada譲りの記法のせいでAWKのようには使えそうもありません。AWK使ったことないですが。
line型の変数がいちいち入出力のバッファとして必要になるのも面倒です。
String'(〜)は、文字列リテラルで表す型がString以外にもあるため、書かないとオーバーロードを解決できないので書いてあります。
実行のしかたはこんな。
...>ghdl -a hello_world.vhdl ...>ghdl -r hello_world Hello world!
続いてecho。
use std.textio.all; entity echo is end echo; architecture behaviour of echo is begin process variable l : line; begin if endfile (input) then wait; end if; readline (input, l); writeline (output, l); end process; end behaviour;
先の性質があるため、ループを書かなくてもループしてくれます。本来は終端判定を省略してreadlineにエラー起こさせて終わらるべきですね。
続いてDelete Blank Lines。
use std.textio.all; entity delete_blank_lines is end delete_blank_lines; architecture behaviour of delete_blank_lines is begin process variable l : line; begin if endfile (input) then wait; end if; readline (input, l); if l.all /= "" then writeline (output, l); end if; end process; end behaviour;
line型は実はaccess Stringだったりします。まんまAda。
それからHamming Numbers。
use std.textio.all; entity hamming_numbers is end hamming_numbers; architecture behaviour of hamming_numbers is begin process variable i, n, x : integer := 0; variable l : line; begin readline (input, l); read (l, n); while n > 0 loop i := i + 1; x := i; for y in 2 to 5 loop while x rem y = 0 loop x := x / y; end loop; end loop; if x = 1 then write (l, i); writeline (output, l); n := n - 1; end if; end loop; wait; end process; end behaviour;
ここまでくるとふつーのプログラミング言語以外の何物でもないです。
ハードウェア記述言語らしさなどどこにもありません。
HDLをプログラミング言語として使えたら、処理系次第ではJITで処理の一部をGPUあたりに置いて云々とかちょっとでも夢想したのが信じられません。
というか、これは本当にハードウェア記述言語なのでしょうか?
気分次第で続く。