1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SystemVerilog ランダム検証(CRV/CDV)完全実践ガイド

Last updated at Posted at 2025-08-18

はじめに

この記事では、SystemVerilogを用いたランダム検証(Constrained Random Verification/Coverage Driven Verification)の実践的な手法について解説いたします。ランダム検証は「偶然に任せる」のではなく「戦略的に制御する技術」です。再現性・制約設計・カバレッジ駆動・デバッグ手法・自動化運用 の5つの柱を適切に実装することで、想定外のバグを効率的に発見できる強力な検証システムを構築できます。

CRV/CDV とは?

  • CRV = Constrained Random Verification(制約付き乱数検証)
    仕様を制約として書き、randomize()で合法入力空間を広く探索する手法

  • CDV = Coverage-Driven Verification(カバレッジ駆動検証)
    covergroup等で網羅目標を定義し、未達を埋めるよう刺激を調整して収束させる手法


1. ランダム検証の基本概念と価値

1.1 ランダム検証が必要な理由

従来の ディレクテッドテスト(手動設計テスト) では、エンジニアが想定したシナリオのみを検証することになります。しかし、実際のバグは「想定外の組み合わせ」で発生することが多いという課題があります。

ランダム検証がもたらす4つの利点:

  • 探索範囲の広さ:人間が思いつきにくい入力パターンを自動的に生成できます
  • マシンパワーの活用:コンピュータの処理能力を最大限に活用し、人間では不可能な量のテストケースを実行できます
  • 効率性:一度環境を構築すれば、数千から数万のテストケースを自動実行できます
  • 偏りの低減:統計的に広範囲をカバーし、検証の死角を減らすことができます

特に重要なのは、マシンパワーを活用した大規模な検証 という点です。例えば、32ビットのアドレス空間に対する全組み合わせテストは人間には不可能ですが、ランダム検証では統計的に有意な範囲を自動的にカバーできます。週末の間にサーバーで数百万のテストケースを実行し、月曜日には結果を確認できるという様な運用が可能になります。

1.2 検証環境の構成要素とその役割

各コンポーネントの責務について:

  • Generator:仕様に基づいた合法な入力データを生成します(制約付きランダム生成)
  • Driver:生成されたデータをプロトコルに従ってDUTへ投入します
  • Monitor:DUTの応答を観測し、トランザクション形式に変換します
  • Scoreboard:期待値と実測値を照合し、不一致があればバグとして検出します
  • Coverage:テストの網羅性を数値化し、検証の完成度を可視化します
  • Assertion:プロトコル違反を即座に検出し、デバッグを効率化します

2. スモールスタート戦略:最小実装から始める

検証環境の構築は、最初から完璧を目指すのではなく、段階的に進めることが成功の鍵となります。

2.1 実装の推奨順序

ステップ1:DUTとのインターフェース定義

まず、DUTとの通信に必要なインターフェースを定義します。

interface dut_if(input clk);
  logic [7:0] data;
  logic       valid;
  logic       ready;
  
  // タイミング検証用のプロパティも追加できます
  modport driver (output data, valid, input ready);
  modport monitor (input data, valid, ready);
  
  // クロッキングブロックを使用すると、タイミング制御が容易になります
  clocking driver_cb @(posedge clk);
    default input #1 output #1;
    output data, valid;
    input ready;
  endclocking
endinterface

ステップ2:基本トランザクションクラスの作成

次に、テストで使用するデータの基本構造を定義します。

class transaction extends uvm_object;  // UVMを使用する場合
  rand bit [7:0] data;
  rand bit [3:0] delay;
  
  constraint c_basic {
    delay inside {[0:15]};  // 基本的な遅延制約
  }
  
  // デバッグ用の表示メソッドも用意しましょう
  function string sprint();
    return $sformatf("data=0x%02x, delay=%0d", data, delay);
  endfunction
  
  // コピー機能も実装しておくと便利です
  function transaction copy();
    transaction tr = new();
    tr.data = this.data;
    tr.delay = this.delay;
    return tr;
  endfunction
endclass

ステップ3:シンプルなDriver実装

トランザクションをDUTに送信するDriverを実装します。

class driver;
  virtual dut_if vif;
  mailbox #(transaction) gen2drv;  // Generatorからのデータ受信用
  
  task run();
    transaction tr;
    forever begin
      gen2drv.get(tr);  // トランザクションを取得
      drive_transaction(tr);
    end
  endtask
  
  task drive_transaction(transaction tr);
    // 遅延を適用
    repeat(tr.delay) @(posedge vif.clk);
    
    // 実際の駆動ロジック
    @(posedge vif.clk);
    vif.valid <= 1'b1;
    vif.data <= tr.data;
    
    // ready待ち
    wait(vif.ready);
    @(posedge vif.clk);
    vif.valid <= 1'b0;
  endtask
endclass

この最小構成から始めて、段階的に機能を追加していくことで、着実に検証環境を構築できます。


3. 乱数管理と再現性の確保

3.1 Seed管理の重要性

再現性は検証の生命線です。 バグを発見しても再現できなければ、修正の確認ができません。そのため、適切なseed管理が不可欠です。

# 実行時のseed指定例(各シミュレータ共通)
vsim +ntb_random_seed=12345 -do "run -all"

# または環境変数での指定
export SV_SEED=12345

3.2 階層的なSeed管理の実装

各コンポーネントに異なるseedを配布することで、独立した乱数ストリームを維持できます。

class base_test;
  int unsigned master_seed;
  
  function new();
    // コマンドラインからseed取得
    if (!$value$plusargs("ntb_random_seed=%d", master_seed)) begin
      master_seed = $urandom;
      $display("Info: Using random seed: %0d", master_seed);
    end else begin
      $display("Info: Using specified seed: %0d", master_seed);
    end
    
    // 各コンポーネントに派生seed配布
    // 固定オフセットを使用することで、再現性を保証
    env.set_seed(master_seed + 100);
    gen.set_seed(master_seed + 200);
    drv.set_seed(master_seed + 300);
  endfunction
  
  // seed情報をファイルに保存
  function void save_seed_info();
    int fd;
    fd = $fopen("seed_history.log", "a");
    $fwrite(fd, "%s: seed=%0d\n", $sformatf("%0t", $time), master_seed);
    $fclose(fd);
  endfunction
endclass

3.3 Random Stabilityの維持

乱数の使い方によっては、コードの小さな変更が検証結果に大きな影響を与えることがあります。これを防ぐための実践的な方法を紹介します。

アンチパターン(避けるべき実装):

// 悪い例:グローバル乱数の直接使用
always @(posedge clk) begin
  if ($urandom_range(0,99) < 10)  // 不安定な乱数使用
    inject_error <= 1;
end

ベストプラクティス(推奨される実装):

// 良い例:クラス内での管理された乱数
class error_injector;
  rand int error_rate;
  int seed;
  
  constraint c_error { 
    error_rate inside {[0:100]}; 
  }
  
  function new(int seed_val);
    this.seed = seed_val;
  endfunction
  
  function bit should_inject();
    // プロセスローカルな乱数生成器を使用
    process p = process::self();
    p.srandom(seed);
    return (error_rate > $urandom_range(0,99));
  endfunction
endclass

4. 制約設計の実践テクニック

4.1 制約の階層的設計

原則:制約は仕様を反映した構造にすることが重要です

class packet;
  // フィールド定義
  rand bit [15:0] length;
  rand bit [7:0]  payload[];
  rand bit        has_crc;
  rand pkt_type_e pkt_type;
  
  // 基本制約(プロトコル仕様)
  constraint c_protocol {
    length inside {[64:1518]};           // Ethernet MTU
    payload.size() == length - 14;       // ヘッダ分を除く
    has_crc == (pkt_type != JUMBO);     // Jumboフレーム以外はCRC必須
  }
  
  // 使用頻度制約(現実的な分布)
  constraint c_distribution {
    length dist {
      [64:127]    := 40,    // 小パケット:40%
      [128:511]   := 30,    // 中パケット:30%
      [512:1518]  := 30     // 大パケット:30%
    };
  }
  
  // シナリオ別制約(動的に有効/無効化)
  constraint c_stress_test {
    length inside {[1500:1518]};  // 境界値に集中
  }
  
  // デフォルトでストレステストは無効
  function new();
    c_stress_test.constraint_mode(0);
  endfunction
endclass

4.2 高度な制約テクニック

1. 依存関係の明示(solve…before)

constraint c_order {
  solve pkt_type before length;  // タイプ決定後に長さを決める
  if (pkt_type == CONTROL)
    length == 64;               // 制御パケットは固定長
}

2. 条件付き制約(implication)

constraint c_conditional {
  is_encrypted -> (length % 16 == 0);  // 暗号化時は16バイト境界
  has_error -> (error_type inside {CRC_ERR, LENGTH_ERR});
}

3. ソフト制約(デフォルト値の設定)

constraint c_defaults {
  soft priority == NORMAL;  // 通常は標準優先度ですが、必要に応じて上書き可能
}

4.3 制約デバッグのコツ

制約が複雑になると、randomize()が失敗することがあります。以下の方法で原因を特定できます。

// 制約の段階的無効化によるデバッグ
function void debug_constraints();
  packet pkt = new();
  
  // 全制約有効でテスト
  if (!pkt.randomize()) begin
    $display("Failed with all constraints enabled");
    
    // 個別に無効化して原因特定
    pkt.c_distribution.constraint_mode(0);
    if (pkt.randomize()) begin
      $display("c_distribution was causing the failure");
    end else begin
      pkt.c_distribution.constraint_mode(1);
      pkt.c_stress_test.constraint_mode(0);
      if (pkt.randomize())
        $display("c_stress_test was causing the failure");
    end
  end
endfunction

5. カバレッジ駆動検証(CDV)の実装

5.1 効果的なカバレッジモデル設計

カバレッジは検証の進捗を可視化する重要な指標です。適切に設計することで、テストの品質を定量的に評価できます。

covergroup cg_packet @(sample_event);
  option.per_instance = 1;
  option.name = "Packet Coverage";
  
  // 単一軸のカバレッジ
  cp_length: coverpoint pkt.length {
    bins small  = {[64:127]};
    bins medium = {[128:511]};
    bins large  = {[512:1518]};
    illegal_bins too_small = {[0:63]};    // 仕様違反を明示
  }
  
  // 状態遷移のカバレッジ
  cp_state_trans: coverpoint state {
    bins idle_to_active = (IDLE => ACTIVE);
    bins active_to_idle = (ACTIVE => IDLE);
    bins error_recovery = (ERROR => IDLE);
    bins back_to_back = (ACTIVE => ACTIVE);  // 連続転送
  }
  
  // クロスカバレッジ(組み合わせの爆発を防ぐ工夫付き)
  cx_type_length: cross pkt_type, cp_length {
    // 重要な組み合わせのみに限定
    bins important_combos = binsof(pkt_type) intersect {CONTROL, DATA} &&
                           binsof(cp_length) intersect {small};
    ignore_bins dont_care = binsof(pkt_type) intersect {DEBUG};
  }
endgroup

5.2 カバレッジ収束のための3段階戦略

カバレッジを効率的に100%に近づけるための段階的アプローチを紹介します。

Phase 1: 自然なランダム探索(0-80%のカバレッジ)

// 初期は制約を緩めて広く探索
pkt.c_stress_test.constraint_mode(0);
repeat(10000) begin  // マシンパワーを活用して大量実行
  assert(pkt.randomize());
  drive_packet(pkt);
  
  // 定期的にカバレッジを確認
  if (sim_time % 1000 == 0) begin
    $display("Current coverage: %0.2f%%", cg_packet.get_coverage());
  end
end

Phase 2: 未達成領域の特定(80-95%のカバレッジ)

// カバレッジレポートから未達成ビンを特定
function void analyze_coverage_holes();
  real cov_val;
  
  // 各カバーポイントを確認
  cov_val = cg_packet.cp_length.get_coverage();
  if (cov_val < 100) begin
    $display("Length coverage incomplete: %0.2f%%", cov_val);
    // ビンごとの詳細を確認
    void'(cg_packet.cp_length.get_coverage(bins));
  end
endfunction

Phase 3: 狙い撃ちによる収束(95-100%のカバレッジ)

// 特定のビンを狙う
task target_specific_bins();
  // largeビンが未達成の場合
  repeat(100) begin
    assert(pkt.randomize() with {
      length inside {[512:1518]};  // largeビンを狙う
    });
    drive_packet(pkt);
  end
  
  // 特定の状態遷移を狙う
  force_state(IDLE);
  force_state(ERROR);
  force_state(IDLE);  // ERROR => IDLE遷移を発生させる
endtask

6. 実践的な運用設計

6.1 テストの階層化戦略

リソースと時間を効率的に使うため、テストを階層化して実行します。

# Makefile例:段階的なテスト実行
.PHONY: smoke daily nightly weekend

smoke:
	@echo "Running smoke test..."
	$(VSIM) +test=smoke +seed=12345 +timeout=5m +sim_count=100
	
daily:
	@echo "Running daily regression..."
	@for seed in 100 200 300 400 500; do \
		$(VSIM) +test=full +seed=$$seed +timeout=30m +sim_count=1000; \
	done
	
nightly:
	@echo "Running nightly test..."
	$(VSIM) +test=comprehensive +seed_range=1:100 +timeout=8h +sim_count=10000
	
weekend:
	@echo "Running weekend exhaustive test..."
	$(VSIM) +test=exhaustive +seed_range=1:1000 +timeout=48h +sim_count=1000000

6.2 失敗Seedの管理システム

バグを発見したseedは貴重な資産です。適切に管理することで、回帰テストの品質を向上させられます。

# failing_seeds.py: 失敗seed管理スクリプト
import json
from datetime import datetime
import sqlite3

class SeedManager:
    def __init__(self, db_path="seeds.db"):
        self.conn = sqlite3.connect(db_path)
        self.create_tables()
    
    def create_tables(self):
        """データベーステーブルの作成"""
        self.conn.execute('''
            CREATE TABLE IF NOT EXISTS failing_seeds (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                seed INTEGER NOT NULL,
                test_name TEXT NOT NULL,
                error_msg TEXT,
                date TEXT NOT NULL,
                status TEXT DEFAULT 'open',
                fixed_version TEXT
            )
        ''')
        self.conn.commit()
    
    def add_failing_seed(self, seed, test_name, error_msg):
        """失敗seedの記録"""
        self.conn.execute('''
            INSERT INTO failing_seeds (seed, test_name, error_msg, date)
            VALUES (?, ?, ?, ?)
        ''', (seed, test_name, error_msg, datetime.now().isoformat()))
        self.conn.commit()
        print(f"Recorded failing seed: {seed}")
    
    def get_regression_list(self):
        """未解決の失敗seedリストを取得"""
        cursor = self.conn.execute('''
            SELECT DISTINCT seed FROM failing_seeds 
            WHERE status = 'open'
        ''')
        return [row[0] for row in cursor.fetchall()]
    
    def mark_as_fixed(self, seed, version):
        """修正済みとしてマーク"""
        self.conn.execute('''
            UPDATE failing_seeds 
            SET status = 'fixed', fixed_version = ?
            WHERE seed = ? AND status = 'open'
        ''', (version, seed))
        self.conn.commit()

7. デバッグの実践手法

7.1 ランダム検証特有のデバッグフロー

ランダム検証では、エラー発生時の文脈情報が重要です。以下のヘルパークラスが有用です。

class debug_helper;
  // トランザクション履歴の記録
  transaction history[$];
  int max_history_size = 1000;
  
  function void record(transaction tr);
    history.push_back(tr.copy());  // コピーを保存
    if (history.size() > max_history_size)
      void'(history.pop_front());
  endfunction
  
  // エラー時の文脈情報出力
  function void dump_context(string error_msg);
    int start_idx;
    $display("========================================");
    $display("ERROR: %s", error_msg);
    $display("Time: %0t", $time);
    $display("========================================");
    
    $display("Last 10 transactions before error:");
    start_idx = (history.size() > 10) ? history.size() - 10 : 0;
    
    for (int i = start_idx; i < history.size(); i++) begin
      $display("[%4d] %s", i, history[i].sprint());
    end
    
    $display("========================================");
  endfunction
  
  // 波形ダンプのトリガー
  function void trigger_waveform_dump();
    $dumpvars(0);
    $dumpfile("error_debug.vcd");
    $display("Waveform dump triggered: error_debug.vcd");
  endfunction
endclass

7.2 制約解決失敗のトラブルシューティング

制約の矛盾を効率的に特定する方法を紹介します。

// 制約の矛盾を段階的に特定
class constraint_debugger;
  
  static function void diagnose(uvm_object obj);
    string constraint_list[$];
    string failing_constraints[$];
    
    // 制約名のリストを取得
    obj.get_constraint_names(constraint_list);
    
    $display("Starting constraint diagnosis for %s", obj.get_name());
    $display("Total constraints: %0d", constraint_list.size());
    
    // 全制約を一時的に無効化
    obj.constraint_mode(0);
    
    // 基本的なランダマイズが成功することを確認
    if (!obj.randomize()) begin
      $error("Even with all constraints disabled, randomization fails!");
      return;
    end
    
    // 一つずつ有効化して問題のある制約を特定
    foreach(constraint_list[i]) begin
      obj.constraint_mode(1, constraint_list[i]);
      if (!obj.randomize()) begin
        failing_constraints.push_back(constraint_list[i]);
        $display("  FAIL: %s", constraint_list[i]);
        obj.constraint_mode(0, constraint_list[i]);
      end else begin
        $display("  OK  : %s", constraint_list[i]);
      end
    end
    
    if (failing_constraints.size() > 0) begin
      $display("Problematic constraints found:");
      foreach(failing_constraints[i])
        $display("  - %s", failing_constraints[i]);
    end else begin
      $display("All individual constraints are OK.");
      $display("The problem might be in constraint interactions.");
    end
  endfunction
  
endclass

8. パフォーマンス最適化

8.1 制約ソルバーの負荷軽減

制約ソルバーの処理時間を短縮するための実践的なテクニックを紹介します。

アンチパターン(避けるべき実装):

// 悪い例:複雑な相互依存
constraint c_complex {
  foreach(data[i]) {
    if (i > 0)
      data[i] > data[i-1];  // O(n^2)の計算複雑度
  }
}

最適化版(推奨される実装):

// 良い例:post_randomizeで調整
constraint c_simple {
  foreach(data[i])
    data[i] inside {[0:255]};
}

function void post_randomize();
  // ソート処理を後で実行(O(n log n))
  data.sort();
endfunction

8.2 シミュレーション全体の高速化

マシンパワーを効率的に活用するためのテクニックです。

// バッチ処理による効率化
class optimized_generator;
  transaction batch[$];
  semaphore batch_lock = new(1);
  
  task generate_batch(int count);
    transaction temp_batch[$];
    
    // 並列生成のための準備
    repeat(count) begin
      transaction tr = new();
      assert(tr.randomize());
      temp_batch.push_back(tr);
    end
    
    // クリティカルセクション
    batch_lock.get();
    batch = {batch, temp_batch};
    batch_lock.put();
  endtask
  
  function transaction get_next();
    transaction tr;
    
    batch_lock.get();
    if (batch.size() == 0) begin
      batch_lock.put();
      generate_batch(100);  // 自動補充
      batch_lock.get();
    end
    tr = batch.pop_front();
    batch_lock.put();
    
    return tr;
  endfunction
endclass

8.3 並列実行による検証時間の短縮

// 複数のテストを並列実行
class parallel_test_manager;
  
  task run_parallel_tests(int num_threads);
    fork
      begin : thread_group
        for (int i = 0; i < num_threads; i++) begin
          automatic int thread_id = i;
          fork
            run_test_thread(thread_id);
          join_none
        end
        wait fork;  // すべてのスレッドの完了を待つ
      end
    join
  endtask
  
  task run_test_thread(int id);
    int local_seed = master_seed + (id * 1000);
    $display("Thread %0d starting with seed %0d", id, local_seed);
    
    // 各スレッドで独立したテストを実行
    test_instance[id] = new();
    test_instance[id].set_seed(local_seed);
    test_instance[id].run();
  endtask
  
endclass

9. UVM環境での実装例

9.1 UVMでの制約付きシーケンス

UVM環境での実装例を示します。UVMを使用することで、再利用性の高い検証環境を構築できます。

class weighted_sequence extends uvm_sequence#(packet);
  `uvm_object_utils(weighted_sequence)
  
  rand int packet_count;
  rand distribution_t dist_mode;
  
  constraint c_scenario {
    packet_count inside {[100:1000]};
    dist_mode dist {
      NORMAL  := 70,
      CORNER  := 20,
      STRESS  := 10
    };
  }
  
  virtual task body();
    packet pkt;
    
    `uvm_info(get_name(), 
              $sformatf("Starting sequence with %0d packets in %s mode", 
                       packet_count, dist_mode.name()), 
              UVM_MEDIUM)
    
    repeat(packet_count) begin
      `uvm_create(pkt)
      
      // 分布モードに応じた制約適用
      case(dist_mode)
        NORMAL: begin
          // デフォルト制約のまま
        end
        
        CORNER: begin
          pkt.c_stress_test.constraint_mode(1);
        end
        
        STRESS: begin
          assert(pkt.randomize() with {
            length > 1500;
            has_error == 1;
          });
        end
      endcase
      
      `uvm_send(pkt)
    end
  endtask
endclass

9.2 Factory overrideによる柔軟な切り替え

UVMのファクトリ機能を使用すると、実行時に動的な振る舞いの変更が可能です。

// ベーステストクラス
class base_test extends uvm_test;
  `uvm_component_utils(base_test)
  
  test_env env;
  
  function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    env = test_env::type_id::create("env", this);
    
    // コンフィギュレーション設定
    uvm_config_db#(int)::set(this, "*", "num_packets", 1000);
  endfunction
  
  task run_phase(uvm_phase phase);
    weighted_sequence seq;
    phase.raise_objection(this);
    
    seq = weighted_sequence::type_id::create("seq");
    seq.start(env.agent.sequencer);
    
    phase.drop_objection(this);
  endtask
endclass

// 特定のテストケース
class directed_test extends base_test;
  `uvm_component_utils(directed_test)
  
  function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    
    // 特定のケースではランダムの代わりに固定シーケンスを使用
    packet::type_id::set_type_override(
      directed_packet::get_type()
    );
  endfunction
endclass

10. 実装サンプル:完全なミニマム環境

実際に動作する最小限のCRV/CDV環境の例を示します。

// 完全に動作する最小CRV/CDV環境
module crv_example;
  
  // シンプルなFIFOをDUTとして使用
  class dut_fifo;
    logic [7:0] mem[$];
    int max_depth = 16;
    
    function void push(logic [7:0] data);
      if (mem.size() < max_depth) begin
        mem.push_back(data);
        $display("[%0t] FIFO: Push data=0x%02x (depth=%0d)", 
                 $time, data, mem.size());
      end else begin
        $warning("[%0t] FIFO: Overflow - push ignored", $time);
      end
    endfunction
    
    function logic [7:0] pop();
      logic [7:0] data;
      if (mem.size() > 0) begin
        data = mem.pop_front();
        $display("[%0t] FIFO: Pop data=0x%02x (depth=%0d)", 
                 $time, data, mem.size());
        return data;
      end else begin
        $warning("[%0t] FIFO: Underflow - returning 0x00", $time);
        return 8'h00;
      end
    endfunction
    
    function int get_depth();
      return mem.size();
    endfunction
  endclass
  
  // トランザクション定義
  class transaction;
    rand bit       is_write;
    rand bit [7:0] data;
    rand int       delay;
    
    constraint c_basic {
      delay inside {[0:5]};
      is_write dist {1 := 70, 0 := 30};  // 書き込みを多めに
    }
    
    constraint c_data {
      // データの分布を現実的に
      data dist {
        [8'h00:8'h0F] := 10,  // 制御文字
        [8'h20:8'h7F] := 70,  // ASCII印字可能文字
        [8'h80:8'hFF] := 20   // 拡張文字
      };
    }
    
    function string sprint();
      return $sformatf("%s: data=0x%02x, delay=%0d",
                       is_write ? "WRITE" : "READ ", data, delay);
    endfunction
  endclass
  
  // カバレッジモデル
  covergroup cg_fifo(ref transaction tr);
    option.per_instance = 1;
    option.comment = "FIFO operation coverage";
    
    cp_operation: coverpoint tr.is_write {
      bins write = {1};
      bins read  = {0};
      bins write_to_read = (1 => 0);
      bins read_to_write = (0 => 1);
    }
    
    cp_data: coverpoint tr.data {
      bins low    = {[8'h00:8'h3F]};
      bins medium = {[8'h40:8'hBF]};
      bins high   = {[8'hC0:8'hFF]};
      bins special[] = {8'h00, 8'hFF};  // 境界値
    }
    
    cp_delay: coverpoint tr.delay {
      bins no_delay = {0};
      bins short_delay = {[1:2]};
      bins long_delay = {[3:5]};
    }
    
    cx_op_data: cross cp_operation, cp_data {
      option.weight = 2;  // このクロスを重視
    }
  endgroup
  
  // テストベンチ本体
  initial begin
    dut_fifo    dut = new();
    transaction tr  = new();
    cg_fifo     cg  = new(tr);
    
    int seed;
    int phase1_count = 1000;
    int phase2_count = 100;
    real coverage;
    
    // Seed管理
    if (!$value$plusargs("seed=%d", seed))
      seed = $urandom;
    $display("=====================================");
    $display("Running with seed: %0d", seed);
    $display("=====================================");
    
    // Phase 1: 自然なランダム探索
    $display("\n--- Phase 1: Random Exploration ---");
    repeat(phase1_count) begin
      assert(tr.randomize()) else begin
        $error("Randomization failed");
        $finish;
      end
      
      // デバッグ情報
      if (phase1_count <= 10 || phase1_count % 100 == 0)
        $display("[%4d] %s", phase1_count, tr.sprint());
      
      // DUT操作
      if (tr.is_write)
        dut.push(tr.data);
      else
        void'(dut.pop());
      
      // カバレッジサンプリング
      cg.sample();
      
      // 遅延
      #(tr.delay);
    end
    
    // カバレッジ確認
    coverage = cg.get_coverage();
    $display("\n--- Coverage after Phase 1: %0.2f%% ---", coverage);
    
    // Phase 2: カバレッジホールの狙い撃ち
    if (coverage < 100) begin
      $display("\n--- Phase 2: Targeting Coverage Holes ---");
      
      // データカバレッジが不足している場合
      if (cg.cp_data.get_coverage() < 100) begin
        $display("Targeting missing data bins...");
        repeat(phase2_count) begin
          assert(tr.randomize() with {
            data inside {[8'hC0:8'hFF]};  // highビンを狙う
          });
          if (tr.is_write) dut.push(tr.data);
          cg.sample();
        end
      end
      
      // 操作シーケンスカバレッジが不足している場合
      if (cg.cp_operation.get_coverage() < 100) begin
        $display("Targeting operation sequences...");
        // 意図的にread-write-readパターンを生成
        tr.is_write = 0; void'(dut.pop()); cg.sample(); #1;
        tr.is_write = 1; dut.push($urandom); cg.sample(); #1;
        tr.is_write = 0; void'(dut.pop()); cg.sample(); #1;
      end
    end
    
    // 最終カバレッジレポート
    $display("\n=====================================");
    $display("Final coverage: %0.2f%%", cg.get_coverage());
    $display("Final FIFO depth: %0d", dut.get_depth());
    $display("=====================================");
    
    $finish;
  end
  
endmodule

11. よくある失敗と対策

11.1 失敗パターンと解決策

検証環境構築でよく見られる問題と、その解決方法をまとめました。

失敗パターン 影響 解決策
randomize()戻り値無視 制約違反の見逃し assert(obj.randomize())を必ず使用
$urandom直接使用の乱立 再現性の喪失 クラス内のrand変数に統一
巨大クロスカバレッジ メモリ爆発とシミュレーション速度低下 条件付きクロス、ビン数制限を適用
実装詳細の制約混入 保守性低下と仕様変更への脆弱性 制約を仕様レベルに保つ
失敗seed情報の喪失 デバッグ不能 seed管理DBの運用を徹底
制約の過度な複雑化 ランダマイズ失敗の増加 制約を階層化し、シンプルに保つ

11.2 トラブルシューティングチェックリスト

問題が発生した際に確認すべき項目のリストです。

// 問題診断用ユーティリティクラス
class diagnostics;
  
  // ランダマイゼーションの診断
  static function void check_randomization(uvm_object obj);
    int success_count = 0;
    int fail_count = 0;
    
    $display("=== Randomization Diagnostics for %s ===", 
             obj.get_type_name());
    
    // 複数回試行して成功率を確認
    repeat(100) begin
      if (obj.randomize())
        success_count++;
      else
        fail_count++;
    end
    
    $display("Success rate: %0d%% (%0d/%0d)", 
             success_count, success_count, success_count + fail_count);
    
    if (fail_count > 0) begin
      $display("Constraint list:");
      foreach(obj.constraint_names[i])
        $display("  - %s", obj.constraint_names[i]);
        
      // 制約デバッガを呼び出し
      constraint_debugger::diagnose(obj);
    end
  endfunction
  
  // カバレッジの診断
  static function void analyze_coverage(covergroup cg);
    real total_coverage;
    
    total_coverage = cg.get_coverage();
    $display("=== Coverage Analysis ===");
    $display("Total coverage: %0.2f%%", total_coverage);
    
    if (total_coverage < 100) begin
      $display("Recommendation: Increase simulation cycles or");
      $display("                target specific bins with constraints");
    end
  endfunction
  
  // メモリ使用量の確認
  static function void check_memory_usage();
    $display("=== Memory Usage ===");
    $system("ps aux | grep simv | head -1");
  endfunction
  
endclass

12. まとめ:成功への5つの鍵

ランダム検証を成功させるための重要なポイントを改めて整理します。

1. 再現性ファースト

  • Seed管理を最優先事項として扱いましょう
  • 失敗の再現こそが改善の第一歩です
  • すべてのテスト実行でseed情報を記録する習慣をつけましょう

2. 制約は仕様の鏡

  • 実装詳細ではなく、仕様を制約として表現しましょう
  • 階層的でモジュラーな設計を心がけましょう
  • 制約の複雑さは段階的に増やしていきましょう

3. カバレッジは地図

  • 達成度だけでなく、未踏領域の可視化に活用しましょう
  • 収束戦略と連携させることで効率を向上させましょう
  • カバレッジホールの分析から、テストの改善点を見つけましょう

4. 自動化による規模拡大

  • CI/CD統合で継続的検証を実現しましょう
  • 回帰テストの自動実行で品質を維持しましょう
  • マシンパワーを最大限活用して、人間では不可能な規模の検証を実現しましょう

5. データドリブンな改善

  • メトリクスの継続的収集を行いましょう
  • 失敗パターンの分析から対策を導きましょう
  • 検証の効率と品質を定量的に評価しましょう

ランダム検証は、適切に実装・運用すれば、人間の想像を超えたバグを効率的に発見する強力な武器となります。小さく始めて、継続的に改善することが成功への道です。


付録A:クイックリファレンス

制約構文チートシート

// 基本構文
inside { }              // 値の範囲指定
dist { }                // 重み付き分布
solve..before           // 解決順序
if/else                 // 条件分岐
-> (implication)        // 含意(条件付き制約)
soft                    // ソフト制約(上書き可能)

// 配列制約
foreach                 // 要素ごとの制約
array.size()           // サイズ制約
unique                  // 重複排除

// 高度な機能
constraint_mode()       // 動的ON/OFF
randomize() with {}     // インライン制約
pre_randomize()        // ランダマイズ前処理
post_randomize()       // ランダマイズ後処理

カバレッジ構文チートシート

// ビン定義
bins name = {values};           // 明示的ビン
bins name[] = {[range]};        // 自動分割ビン
illegal_bins                    // 違反検出用ビン
ignore_bins                     // 除外指定ビン

// クロスカバレッジ
cross a, b;                     // 基本クロス
binsof() intersect {}          // 選択的クロス

// 制御オプション
iff (condition)                 // 条件付きサンプリング
option.per_instance = 1;        // インスタンス別カバレッジ
option.weight = 2;              // 重み設定
option.goal = 90;               // 目標値設定
option.comment = "text";        // コメント追加

コマンドライン例

# VCS (Synopsys)
vcs -sverilog -ntb_opts rvm +define+UVM_TESTNAME=test1
./simv +ntb_random_seed=12345 -cm line+fsm+branch+cond

# Questa (Mentor/Siemens)
vlog -sv +define+UVM_TESTNAME=test1 *.sv
vsim -c -do "run -all" +ntb_random_seed=12345 -coverage

# Xcelium (Cadence)
xrun -sv +define+UVM_TESTNAME=test1 *.sv +svseed=12345 -coverage all

# Vivado Simulator (Xilinx/AMD)
xvlog -sv *.sv
xelab top -debug all
xsim -R -testplusarg "seed=12345"

パフォーマンス最適化のヒント

// 1. 制約の分離
constraint c_fast { /* シンプルな制約 */ }
constraint c_slow { /* 複雑な制約 */ }

// 2. バッチ処理
generate_batch(1000);  // 一度に大量生成

// 3. 並列実行
fork
  run_test(seed1);
  run_test(seed2);
join

// 4. メモリ管理
if (queue.size() > MAX_SIZE)
  void'(queue.pop_front());

このガイドが、皆様のランダム検証環境構築の一助となれば幸いです。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?