LoginSignup
33
27

More than 5 years have passed since last update.

OSXでのVerilogHDL環境構築&4bitカウンターを作って波形を見る

Last updated at Posted at 2017-11-30

はじめに

この記事ではVerilog HDLというハードウェア記述言語の1つをIcarus VerilogというオープンソースのVerilog Simulatorを用いて実行し、GTKWaveで波形を観察する?ことを目標とします。

用語まとめ

  1. Verilog HDL
    ハードウェア記述言語の1つ

  2. Icarus Verilog
    Verilog の Simulator

  3. GTKWave
    Wave Viewer 波形描画ソフト

Icarus Verilog(イカルス・ヴェリログ) はオープンソースの Verilog シミュレータ。Verilog 1995, 2001, SystemVerilog 2005 をサポートしている。
ref : https://ja.wikipedia.org/wiki/Icarus_Verilog

HomeBrewによるIcarus VerilogのInstall

$ sudo xcodebuild -license accept
$ brew install icarus-verilog
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (homebrew/science, homebrew/core, caskroom/cask).
==> Downloading https://homebrew.bintray.com/bottles/icarus-verilog-10.1.1.high_
######################################################################## 100.0%
==> Pouring icarus-verilog-10.1.1.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/icarus-verilog/10.1.1: 59 files, 5.6MB

ref : http://iverilog.wikia.com/wiki/Installation_Guide

Installできていることを確認

$ iverilog
iverilog: no source files.

Usage: iverilog [-ESvV] [-B base] [-c cmdfile|-f cmdfile]
                [-g1995|-g2001|-g2005|-g2005-sv|-g2009|-g2012] [-g<feature>]
                [-D macro[=defn]] [-I includedir]
                [-M [mode=]depfile] [-m module]
                [-N file] [-o filename] [-p flag=value]
                [-s topmodule] [-t target] [-T min|typ|max]
                [-W class] [-y dir] [-Y suf] source_file(s)

See the man page for details.

GTKWaveのダウンロード

http://gtkwave.sourceforge.net/ からInstallします。Installといっても、zipを落として、.appファイルを開くだけです。
OSX版のダウンロードに移動して https://sourceforge.net/projects/gtkwave/files/

Screen Shot 2017-11-30 at 15.16.41.png

これの、gtkwave.zip をダウンロードします。これを展開して、Applications直下にgtkwave.appを移動させます。

Screen Shot 2017-11-30 at 15.19.28.png

これを、右クリックからopenで実行します。

Screen Shot 2017-11-30 at 15.27.26.png

サンプルを書いてコンパイル&実行

Verilogを書く

http://monoist.atmarkit.co.jp/mn/articles/0703/16/news148.html
の例に倣って、4bitカウンターのサンプルを記述します。

counter.vを以下のように記述します。これはカウンターのハードウェアを記述したことになります。

module counter(CLK, RST_X, cnt);
  input RST_X, CLK;
  output reg [3:0] cnt;
  always @(posedge CLK)  begin
    if (!RST_X)
      cnt <= 4'h0;
    else
      cnt <= cnt + 4'h1;
  end
endmodule

counter_test.vは以下のようにしました。

module top();
  reg CLK, RST_X;
  wire [3:0] w_cnt;
  counter counter_instance(CLK, RST_X, w_cnt);

  initial begin
    CLK = 0;
    forever #10 CLK = ~CLK;
  end

  initial begin
    RST_X = 0;
    #30 RST_X = 1;
  end

  initial begin
    #500 $finish();
  end

  always @(posedge CLK) begin
    $write("[%t] counter: %b\n", $time, w_cnt);
  end

  initial begin
      $dumpfile("counter_test.vcd");
      $dumpvars(0, counter_instance);
  end

endmodule

コンパイル&実行

次に、これをコンパイルして、実行してみます。

$ iverilog -o counter_test.out counter.v counter_test.v

これにて、counter_test.outという実行ファイルが作られます。
このcounter_test.outを用いて、vvpコマンドでSimulationの実行を行います。

$ vvp counter_test.out
VCD info: dumpfile counter_test.vcd opened for output.
[                  10] counter: xxxx
[                  30] counter: 0000
[                  50] counter: 0001
[                  70] counter: 0010
[                  90] counter: 0011
[                 110] counter: 0100
[                 130] counter: 0101
[                 150] counter: 0110
[                 170] counter: 0111
[                 190] counter: 1000
[                 210] counter: 1001
[                 230] counter: 1010
[                 250] counter: 1011
[                 270] counter: 1100
[                 290] counter: 1101
[                 310] counter: 1110
[                 330] counter: 1111
[                 350] counter: 0000
[                 370] counter: 0001
[                 390] counter: 0010
[                 410] counter: 0011
[                 430] counter: 0100
[                 450] counter: 0101
[                 470] counter: 0110
[                 490] counter: 0111

この時、counter_test.vcdというファイルが生成されています。

波形を見る

Screen Shot 2017-11-30 at 16.51.09.png

生成された、.vcdファイルを開くだけです。
こんな感じで波形が見れます。

Screen Shot 2017-11-30 at 16.58.26.png

終わりに

以上となります。
今後の発展の為にもご意見、ご感想、その他要望等お待ちしております。

33
27
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
33
27