UVM糖果爱好者教程 - 25.使用C模型

我们经常使用C模型作为参考模型。 由于SystemVerilog的直接编程接口(DPI),使用C模型从未如此简单。 我们将向您展示如何在我们的 jelly bean 记分板中使用C模型。

原始记分板

这是我们在UVM 1.2中的Jelly Bean Taster中使用的原始记分板。 记分板检查风味,酸味和味道的组合是否符合预期(第9和10行)。

class jelly_bean_sb_subscriber extends uvm_subscriber#( jelly_bean_transaction );
  `uvm_component_utils( jelly_bean_sb_subscriber )
 
  function new( string name, uvm_component parent );
    super.new( name, parent );
  endfunction: new
 
  function void write( jelly_bean_transaction t );
    if (     t.flavor == CHOCOLATE && t.sour   && t.taste == YUMMY ||
         ! ( t.flavor == CHOCOLATE && t.sour ) && t.taste == YUCKY ) begin
      `uvm_error( get_name(), { "You lost sense of taste!", t.convert2string() } )
    end else begin
      `uvm_info( get_name(), { "You have a good sense of taste.", t.convert2string() }, UVM_LOW )
    end
  endfunction: write
 
endclass: jelly_bean_sb_subscriber

让我们把这个检查委托给一个C模型。

C模型

我们定义了一个名为check_taste_in_c的函数,它将flavor,sour和taste作为参数,如果组合符合预期,则返回0。否则它返回1.为清晰起见,我们定义了与SystemVerilog中定义的相同的枚举(第5行和第6行)。由于C不知道SystemVerilog的位类型,我们用svBit类型替换它,svBit类型是在svdpi.h中定义的类型(第8行)。 svdpi.h还提供了其他类型和一些辅助宏和常量。有关更多信息,请参阅IEEE Std 1800™-2012的附录I.请注意,我们添加了extern“C”,因为我们在C ++文件中定义了此模型。如果你在C文件中定义这个模型,你不需要extern“C”。

#include "svdpi.h"
 
// same enums as defined in jelly_bean_pkg.sv
 
enum flavor_e { NO_FLAVOR, APPLE, BLUEBERRY, BUBBLE_GUM, CHOCOLATE };
enum taste_e  { UNKNOWN, YUMMY, YUCKY };
 
extern "C" int check_taste_in_c( flavor_e flavor, svBit sour, taste_e taste ) {
   if (     flavor == CHOCOLATE && sour == 1   && taste == YUMMY ||
        ! ( flavor == CHOCOLATE && sour == 1 ) && taste == YUCKY ) 
     return 1; // error
   else
     return 0; // OK
}

使用C模型的记分板

这是使用C模型的新记分板。为了调用在C中实现的函数,我们导入函数(第8行)。一旦我们导入函数,我们可以将它称为SystemVerilog函数(第11行)。

class jelly_bean_sb_subscriber extends uvm_subscriber#( jelly_bean_transaction );
  `uvm_component_utils( jelly_bean_sb_subscriber )
 
  function new( string name, uvm_component parent );
    super.new( name, parent );
  endfunction: new
 
  import "DPI-C" function bit check_taste_in_c( flavor_e flavor, bit sour, taste_e taste );
 
  function void write( jelly_bean_transaction t );
    if ( check_taste_in_c( t.flavor, t.sour, t.taste ) ) begin
      `uvm_error( get_name(), { "You lost sense of taste!", t.convert2string() } )
    end else begin
      `uvm_info( get_name(), { "You have a good sense of taste.", t.convert2string() }, UVM_LOW )
    end
  endfunction: write
 
endclass: jelly_bean_sb_subscriber


您可以在EDA Playground上查看并运行代码。

猜你喜欢

转载自blog.csdn.net/zhajio/article/details/80798754