3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VScodeのCode-runnerでverilogの環境を作る

Last updated at Posted at 2020-05-10

環境

Windows10

動機

授業でハードウェア記述言語のVerilogを扱うとのことで、VSCodeをIDEにしたいと考えました。

ディレクトリ構造とファイル

root/
└ test.v

test.v
module AND(
  a, b, x
);

  input a, b;
  output x;

  assign x = a & b;

endmodule // AND

module Test();
  reg a, b;
  wire [0:0] out;

  initial begin
    a = 0;    b = 0;
    $dumpfile("test.vcd");
    $dumpvars(0);
    $monitor("%t: a = %b, b = %b, x = %b", $time, a, b, out);

    #10
      a = 1;
    #10
      b = 1;
    #10
      a = 0;
    #10
      $finish;

  end

  AND ad (a, b, out);

endmodule // Test

AND演算だけする素子とテストです。

方法

※ iverilog等のコンパイラはインストールされていて、パスが通っているものとします。

1. 必要なVSCodeの拡張機能をインストール

  • Verilog HDL/SystemVerilog
  • Code Runner

2. setting.jsonで設定します

※Code Runnerはデフォルトではverilogに対応していないため、以下のような記述をjson中に追加します。
設定の詳細は自分の別記事、Code Runnerを使いこなすをご覧ください。

setting.json
"code-runner.languageIdToFileExtensionMap": {
	"verilog": ".v"
},
"code-runner.executorMap": {
	"verilog": "cd $dir && iverilog $fileName && vvp a.out"
},
// ここから下はお好みで
"code-runner.clearPreviousOutput": true,
"code-runner.runInTerminal": true

一つ目の code-runner.languageIdToFileExtensionMap は恐らく拡張子を言語に対応させる設定です。
二つ目の code-runner.executorMap は言語ごとに実行するコマンドです。
.vファイル を 言語ID verilog に対応させたのち、実行時にはverilogのコマンドを呼び出しています。でも拡張子から実行するコマンドを決める方法もあるので、そっちの方が完結かもしれません。Code Runnerを使いこなす#ファイルの拡張子から実行コマンドを指定するを読んでみてください。

ちなみにコマンドについてですが
cd \$dir は現在編集しているファイルのあるフォルダに移動するコマンドです。
iverilog \$fileName でコンパイルしたのち、vvp a.out で実行になります。
※ $fileNameは実行するファイルの名前(拡張子含む)です。これも自分の記事を読んでみてください。
※ 出力ファイル名は iverilog -o b.out $fileName とすればb.outになります。それに合わせて vvp b.out に変更しましょう。

補足:この例では、iverilog直接を呼び出しましたが、その他ではbat, shを使用してもよいと思います。
その場合は

setting.json
"code-runner.executorMap": {
	"verilog": "verilog.bat $fileName"
}

などとすればよいでしょう。

3. 試してみる

編集した.vファイルを開いた状態で、右クリックメニューのRun CodeもしくはCtrl + alt + N で実行します。以下のような表示が出れば成功です。

VCD info: dumpfile test.vcd opened for output.
                   0: a = 0, b = 0, x = 0
                  10: a = 1, b = 0, x = 0
                  20: a = 1, b = 1, x = 1
                  30: a = 0, b = 1, x = 0

まとめ

以上で最低限のコンパイルできる環境が出来上がります。
VSCode自体のインストールやbatファイル, iverilogについてはその他のサイトに優れた解説が散見されるので、適当に検索してください。
また生成されるvcdファイルをVScodeの中で確認する拡張機能もあるようです→WaveTrace
ついでに導入してみてはいかがでしょうか。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?