UVM Tutorial for Candy Lovers – 25. Using a C-Model

We often use a C-model as a reference model. Thanks to the direct programming interface (DPI) of SystemVerilog, using C-model has never been easier. We will show you how to use a C-model in our jelly bean scoreboard.

Original Scoreboard

This is our original scoreboard used in Jelly Bean Taster in UVM 1.2. The scoreboard checks if the combination of flavorsour, and taste are as expected or not (lines 9 and 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

Let’s delegate this checking to a C-model.

C-model

We defined a function called check_taste_in_c which takes the flavorsour, and taste as arguments and returns 0 if the combination is as expected. Otherwise it returns 1. For clarity, we defined the same enums as defined in SystemVerilog (lines 5 and 6). Since C does not know about the bit type of SystemVerilog, we replaced it with the svBit type, which is type-defined in the svdpi.h (line 8). The svdpi.h also provides other types and a number of helper macros and constants. For more information, please see Annex I of IEEE Std 1800™-2012. Note that we added extern "C" because we defined this model in a C++ file. If you define this model in a C file, you don’t need the 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
}

Scoreboard Using the C-model

Here is the new scoreboard that uses the C-model. In order to call a function implemented in C, we import the function (line 8). Once we import the function, we can call it as if it is a SystemVerilog function (line 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值