FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

オープンソースのマイクロプロセッサ テストプログラム生成ツール MicroTESK 試行

マイクロプロセッサの設計をするとき(そもそも設計する人はあまりいないのだが)、検証のフェーズとして必ずランダムパタンを実行して検証するフェーズがある。

マイクロプロセッサのランダムテストは必ず実施したほうがいいのだが、生成ツールは結構作るのが難しくて、しかも(需要がないため)一般的にあまり公開されていない。

既存のツールとしては、Wikipedia で調べてみると以下のようなものがあった。

  • Obsidian Software RAVEN : こちらはARMに買収されてしまった。
  • MicroTESK an open-source ISG developed by the Russian Academy of Sciences

MicroTESKとは何だろうか?調査してみた。

なるほど、オープンソースで公開されているのはMIPSRISC-Vだ。RISC-V版が公開されているのか! 面白いので、試してみよう。

関連記事

MicroTESK のインストール

MicroTESKをインストールするには、ウェブサイトからダウンロードして展開する。それ以外にも、ツールがいくつか必要だ。z3とcvc4というツールをダウンロードしなければならない。

sudo apt  install -y ant

wget https://forge.ispras.ru/attachments/download/5584/microtesk-2.4.39-beta-180219.tar.gz
tar xvfz microtesk-2.4.39-beta-180219.tar.gz
cd microtesk-2.4.39-beta-180219/
# ツールのインストール : z3のインストール
cd tools/
mkdir z3
mkdir z3/unix
cd z3/unix/
wget https://github.com/Z3Prover/z3/releases/download/z3-4.6.0/z3-4.6.0-x64-ubuntu-16.04.zip
unzip z3-4.6.0-x64-ubuntu-16.04.zip
ln -s z3-4.6.0-x64-ubuntu-16.04 z3
# cvc4 のインストール
mkdir cvc4
mkdir cvc4/unix
cd cvc4/unix/
wget http://cvc4.cs.stanford.edu/downloads/builds/x86_64-linux-opt/unstable/cvc4-2018-03-15-x86_64-linux-opt
ln -s cvc4-2018-03-15-x86_64-linux-opt  cvc4

MicroTESK を試行する

まずはマニュアルに則って実行してみる。どうやら2つのステップを踏むらしい。

まずはコンパイルだ。minimipsというモデルをコンパイルする。

$ sh bin/compile.sh arch/demo/minimips/model/minimips.nml
Buildfile: /home/msyksphinz/work/microtesk/microtesk-2.4.39-beta-180219/bin/build.xml

clean:
   [delete] Deleting directory /home/msyksphinz/work/microtesk/microtesk-2.4.39-beta-180219/gen

BUILD SUCCESSFUL
Total time: 0 seconds
Translating: arch/demo/minimips/model/minimips.nml
Model name: minimips
Included: arch/demo/minimips/model/minimips.nml
Buildfile: /home/msyksphinz/work/microtesk/microtesk-2.4.39-beta-180219/bin/build.xml

build:
    [mkdir] Created dir: /home/msyksphinz/work/microtesk/microtesk-2.4.39-beta-180219/gen/bin
    [javac] Compiling 222 source files to /home/msyksphinz/work/microtesk/microtesk-2.4.39-beta-180219/gen/bin
    [mkdir] Created dir: /home/msyksphinz/work/microtesk/microtesk-2.4.39-beta-180219/gen/src/resources
      [jar] Building jar: /home/msyksphinz/work/microtesk/microtesk-2.4.39-beta-180219/lib/jars/models.jar

BUILD SUCCESSFUL
Total time: 1 second

次に、テストプログラムを生成してみよう。

$ sh bin/generate.sh minimips arch/demo/minimips/templates/euclid.rb
Error: Failed to load custom data generators for minimips.
Generation was aborted.

あれ?うまく行かない。どう頑張ってもうまく行かないので、とりあえず minimips のモデルは諦めて、一般的なCPUモデルで試してみた。

sh bin/compile.sh arch/demo/cpu/model/cpu.nml
sh bin/generate.sh cpu arch/demo/cpu/templates/example.rb

test_0000.s が生成されている。見てみよう。

...
  .text
  .org 0x10
  ; Initialization Section Starts
  mov (12) [15]
  ; Initialization Section Ends

  ;-------------------------------------------------------------------------------------------------
  ; External Code (example.rb:89)

  Main Section:
  GPR[0] = 00001111, M[0] = 00000000

  ; Main Section Starts
  /=
  Multiline comment. Line 1.
  Multiline comment. Line 2.
  Multiline comment. Line 3.
  =/
  mov R0 [15]
  mov R1 R0

  mov R2 [15]
  mov R3 R2

  mov R10 [5]
  add R4 R10

  add R5 R10
...

なるほど、いろいろ生成されている。これは便利そうだ。モデル生成ルールを勉強しておけば、役に立ちそう。

MicroTESK for RISC-V を試行してみる

別のパッケージには、RISC-Vに対応したものも含まれている。

forge.ispras.ru

こちらもとりあえず試行してみたが、どうもちゃんと生成できる段階ではない?

$ sh bin/compile.sh arch/riscv/model/riscv_rv32i.nml
...
mv|not|neg|seqz|snez|sltz|sgtz|j|jr|beqz|bnez|blez|bgez|bltz|bgtz|rv32i_pseudo_system'."
riscv_rv32i.nml 1316:3 ERROR (SEMANTIC): "The 'lui' item of the 'rv32i_load_upper_imm' OR-rule is not defined or is not a OP definition."
riscv_rv32i.nml 1316:3 ERROR (SEMANTIC): "Failed to recognize the grammar structure. It will be ignored: 'oprv32i_load_upper_imm=lui|auipc'."
riscv_rv32i.nml 1319:3 ERROR (SEMANTIC): "The 'add' item of the 'rv32i_arithmetic_rrr' OR-rule is not defined or is not a OP definition."
riscv_rv32i.nml 1319:3 ERROR (SEMANTIC): "Failed to recognize the grammar structure. It will be ignored: 'oprv32i_arithmetic_rrr=add|sub|xor|or|and'."
riscv_rv32i.nml 1325:3 ERROR (SEMANTIC): "The 'xori' item of the 'rv32i_arithmetic_rri' OR-rule is not defined or is not a OP definition."
riscv_rv32i.nml 1325:3 ERROR (SEMANTIC): "Failed to recognize the grammar structure. It will be ignored: 'oprv32i_arithmetic_rri=xori|ori|andi'."
Error: TRANSLATION WAS INTERRUPTED DUE TO SEMANTIC ERRORS.
Translation was aborted.
Buildfile: /home/msyksphinz/work/microtesk/microtesk-riscv-0.0.2-alpha-171206/bin/build.xml

build:
    [mkdir] Created dir: /home/msyksphinz/work/microtesk/microtesk-riscv-0.0.2-alpha-171206/gen/bin

BUILD FAILED
/home/msyksphinz/work/microtesk/microtesk-riscv-0.0.2-alpha-171206/bin/build.xml:47: srcdir "/home/msyksphinz/work/microtesk/microtesk-riscv-0.0.2-alpha-171206/gen/src/java" does not exist!

Total time: 0 seconds

MicroTESK の構成について

MicroTESKの構成は以下のようになっているらしい。

MicroTESK は、2つの記述によってテストパタンを生成する。

  • マイクロアーキテクチャの記述 (cpu.nml など)
  • テストパタンを記述 (example.rb など)

  • cpu.nml

////////////////////////////////////////////////////////////////////////////////
// Registers and Memory

// General-purpose registers.
reg GPR[REG_NUMBER, BYTE] // Format: <name>[<number>, <type>]

// A register for storing the program counter.
reg PC[INDEX] // This format means that there is 1 register of type INDEX

// A memory line
mem M[MEM_SIZE, BYTE]
...
////////////////////////////////////////////////////////////////////////////////
// Arithmetic and Logic Instructions

op ADD()
  syntax = "add"
  image  = "00"
  action = {
    DEST = SRC1 + SRC2;
    // Function 'trace' prints text messages to the simulator log
    trace("%d + %d = %d", SRC1, SRC2, DEST);
  }

op SUB()
  syntax = "sub"
  image  = "01"
  action = { DEST = SRC1 - SRC2; }
...
  • example.rb

example.rb を見ているのだが、sub命令などは生成リストに出てこないし、overflow などのsituation文についても言及がない。 どのような仕組みになっているんだろう?

...
    ############################################################################
    # Building instruction sequences

    # Instruction sequences are described as blocks that use two kind of
    # sequence generators: (1) compositors and (2) combinators.
    # Supported compositors: CATENATION, ROTATION, OVERLAP, NESTING, RANDOM
    # Supported combinators: PRODUCT, DIAGONAL, RANDOM

    # Randomized sequences of 2 instuctions
    block(:compositor => 'random', :combinator => 'product') {
      iterate {
        sub reg(6), reg(5)
        mov reg(7), reg(6)
      }

      iterate {
        add reg(3), reg(4) do situation('add', :case => 'overflow', :size => 8) end
        sub reg(3), reg(4) do situation('sub', :case => 'overflow', :size => 8) end
        mov reg(4), reg(3)
      }
    }.run
  • test_0000.s
  ;-------------------------------------------------------------------------------------------------
  ; Test Case 0 (example.rb:160)

  ; Preparation
  ; Initializer for REG: 85
  mov R6 [21]
  add R6 R6
  add R6 R6
  add R6 [1]
  ; Initializer for REG: 143
  mov R5 [-29]
  add R5 R5
  add R5 R5
  add R5 [3]

  ; Stimulus
  sub R6 R5