打印trans的小tips

在 UVM 验证平台中,打印 transaction(事务)通常需要结合 UVM 的打印机制和事务的自定义信息输出。以下是常用的实现方法:

基础方法:使用 uvm_info 配合 sprint()

transaction 通常继承于 uvm_sequence_item,而 uvm_sequence_item 提供了 sprint() 方法,可将事务的字段信息格式化为字符串。结合 uvm_info 打印:

// 在序列或驱动中打印 transaction
class my_sequence extends uvm_sequence#(my_transaction);
  `uvm_object_utils(my_sequence)
  
  virtual task body();
    my_transaction trans;
    trans = my_transaction::type_id::create("trans");
    
    // 随机化或赋值事务字段
    assert(trans.randomize());
    
    // 打印事务:使用 sprint() 转换为字符串
    `uvm_info("SEQ", $sformatf("发送事务:\n%s", trans.sprint()), UVM_LOW)
    
    // 发送事务(示例)
    start_item(trans);
    finish_item(trans);
  endtask
endclass
  • trans.sprint():返回事务所有字段的格式化字符串(包含字段名和值)。
  • 输出效果类似:
UVM_INFO SEQ (my_sequence.sv:10) [SEQ] 发送事务:
my_transaction@1234 {
  addr: 32'h1000_0000
  data: 32'h1234_5678
  wr_en: 1'b1
  ...
}

自定义打印格式:重写 do_print() 方法

如果需要自定义事务的打印内容(如筛选字段、调整格式),可以在 transaction 类中重写 do_print() 方法:

class my_transaction extends uvm_sequence_item;
  rand bit [31:0] addr;
  rand bit [31:0] data;
  rand bit        wr_en;
  
  `uvm_object_utils_begin(my_transaction)
    `uvm_field_int(addr, UVM_DEFAULT)
    `uvm_field_int(data, UVM_DEFAULT)
    `uvm_field_int(wr_en, UVM_DEFAULT)
  `uvm_object_utils_end
  
  function new(string name = "my_transaction");
    super.new(name);
  endfunction
  
  // 重写 do_print() 自定义打印格式
  virtual function void do_print(uvm_printer printer);
    super.do_print(printer);  // 保留默认打印(可选)
    
    // 添加自定义打印内容
    printer.print_string("操作类型", wr_en ? "写" : "读");
    printer.print_field("地址", addr, 32, UVM_HEX);  // 显式指定位宽和格式
    printer.print_field("数据", data, 32, UVM_HEX);
  endfunction
endclass

重写后,trans.sprint() 会包含自定义内容,打印更直观。

简化打印:使用 uvm_transaction 的 print() 方法

uvm_sequence_item 继承于 uvm_transaction,自带 print() 方法可直接打印事务信息(内部调用 sprint()):

trans.print();  // 直接打印事务,等效于 `uvm_info(..., trans.sprint(), ...)

该方法会自动输出事务的类型、名称和字段信息,无需手动拼接字符串。

posted @ 2025-08-12 19:56  MKYC  阅读(55)  评论(0)    收藏  举报