Vengineerの妄想(準備期間)

人生は短いけど、長いです。人生を楽しみましょう!

Bluespec SystemVerilog : HELLO WORLDを学ぶ(その4)

Verification Engineerの戯言


次の例は、5回だけ、"Hello World" を表示します。
そのために、"Hello World"を表示した数を数えるカウンタ ctr を定義します。
この ctr は、5をカウントするので3ビットの unsigned int型のレジスタ( Reg#(UInt#(3)) ) です。
Reg、UIntはBluespecで定義済みです。Regがレジスタになり、型をは #(型) で指定します。
この例では、3ビットのUInt型( Uint#(3) )です。UIntのビット幅を #(3) で指定しています。
初期値は、<- mkReg(0) で指定しています。このmkRegがモジュール名です。
Version 1のモジュールはmkで始まると説明しましたね。

    package FirstAttempt;

        // My first design in the cool Bluespec language

        String s = "Hello world";

        (* synthesize *)
        module mkAttempt(Empty);

            Reg#(UInt#(3)) ctr <- mkReg(0);

            rule say_hello;
                $display(s);
                $finish(0);
            endrule
        endmodule
    endpackage 

上記の記述では、カウンタを定義し、初期値を指定しただけです。
カウンタのカウントアップさせるには、次のようにします。
ルールsay_helloが呼ばれる毎にカウンタctrが1づつカウントアップします。
カウンタctrが4になったら(ctrは0から始まるので)、$finishを呼びます

    package FirstAttempt;

        // My first design in the cool Bluespec language

        String s = "Hello world";

        (* synthesize *)
        module mkAttempt(Empty);

            Reg#(UInt#(3)) ctr <- mkReg(0);

            rule say_hello;
                ctr <= ctr + 1;
                $display(s);
         if (ctr == 4) $finish(0);
            endrule
        endmodule
    endpackage 

では、実行してみましょう!

    % bsc -sim FirstAttempt.bsv
    Elaborated module file created: mkAttempt.ba
    % bsc -e mkAttempt -sim -o ./sim mkAttempt.ba
    Bluesim object created: mkAttempt.{h,o}
    Bluesim object created: schedule.{h,o}
    Simulation shared library created: sim.so
    Simulation executable created: ./sim

    % ./sim
    Hello World
    Hello World
    Hello World
    Hello World
    Hello World

検証、Verification、Bluespec SystemVerilog