FPGA開発日記

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

GTKWaveとVerilatorはSystemVerilogのunion型をどのように扱っているのか

ふと気になって、SystemVerilogのunion型をどのように扱っているのか調査しようと思った。現在私のデザインではunion型は使っていないけれども、今後使うことになるとデバッグ時にGTKWaveで波形を観察することになる。GTKWaveがどのようにunion型を扱っているのか観察してみよう。

手始めに、以下の簡単なデザインを作成してみる。以下のコードは2種類のunion型を使用して変数を共有している。 このコードをコンパイルしてVerilatorでシミュレーションし、FSTを出力してみた。

typedef struct packed {
  logic [31: 0]  d;
} typeA_t;

typedef struct packed {
  logic [15: 0]  d0;
  logic [15: 0]  d1;
} typeB_t;

typedef union packed {
  typeA_t ta;
  typeB_t tb;
  logic [$bits(typeA_t)-1: 0] raw;
} union_t;

module union_test
  (
   input logic         clk,
   input logic         reset_n,
   input logic [15: 0] in_A,
   input logic [15: 0] in_B,
   output union_t      out_C
   );

always_ff @(posedge clk, negedge reset_n) begin
  if (!reset_n) begin
    out_C <= '0;
  end else begin
    out_C.tb.d0 <= in_A;
    out_C.tb.d1 <= in_B;
  end
end

endmodule // union_test

このFSTファイルをGTKWaveで観察してみる。

f:id:msyksphinz:20211018000639p:plain

なるほど。こういう感じになるのか。階層構造表示欄では、union型の信号の定義に追加してunionの各変数型の定義が表示されている。これで必要に応じて型を変えながら確認するということか。参考になった。