Verilog ACC子程序

2022-05-20 14:50 更新

功能特性

ACC 子程序主要完成的功能有:

  • 從內(nèi)部數(shù)據(jù)結(jié)構(gòu)中讀取特定對(duì)象的相關(guān)信息
  • 把特定對(duì)象的相關(guān)信息寫(xiě)入內(nèi)部數(shù)據(jù)結(jié)構(gòu)中

ACC 子程序可操作的對(duì)象類(lèi)型有:

  • 模塊實(shí)例、模塊端口、模塊的端到端的路徑以及模塊之間的路徑
  • 頂層模塊
  • 原語(yǔ)實(shí)例和原語(yǔ)端口
  • wire、reg、parameter、integer、time、real 等變量類(lèi)型
  • 時(shí)序檢查
  • 命名事件

ACC 子程序的主要特征有:

  • 均以 acc_ 為前綴
  • 開(kāi)頭必須調(diào)用 acc_initialize() 以進(jìn)行環(huán)境初始化
  • 退出時(shí)必須調(diào)用 acc_close()
  • 使用 ACC 庫(kù)中的函數(shù)時(shí)必須包含頭文件 acc_user.h
  • 使用句柄的概念訪(fǎng)問(wèn)對(duì)象。句柄是預(yù)定義的指向設(shè)計(jì)中特定對(duì)象的數(shù)據(jù)類(lèi)型。獲取到句柄就能獲取到對(duì)象的全部信息。這與 C 語(yǔ)言中文件的句柄概念類(lèi)似。句柄用關(guān)鍵字 handle 聲明。

ACC 子程序的主要類(lèi)型有:

  • 句柄子程序(handle):將句柄返回設(shè)計(jì)中的對(duì)象,總是以 acc_handle_ 為前綴。
  • 后繼子程序(next):將句柄返回給設(shè)計(jì)中特定對(duì)象集合中的下一個(gè)對(duì)象,總是以 acc_next_ 為前綴,而且以引用的對(duì)象作為參數(shù)。
  • 值變鏈接子程序(VCL, Value Change Link):可以從監(jiān)視對(duì)象值變化的對(duì)象列表中添加和刪除對(duì)象,總是以 acc_vcl_ 為前綴,沒(méi)有返回值。
  • 取值子程序(fetch):能夠提取層次路徑等屬性信息,總是以 acc_fetch_ 為前綴。
  • 雜項(xiàng)子程序(miscellaneous):用于執(zhí)行與存取子程序相關(guān)的一些雜項(xiàng)操作,例如 acc_initialize() 與 acc_close() 都屬于雜項(xiàng)子程序。
  • 修改子程序(modify):可以修改內(nèi)部數(shù)據(jù)結(jié)構(gòu)。

完整的 ACC 子程序及其簡(jiǎn)單用法說(shuō)明參考下一節(jié)《Verilog ACC 子程序列表》。

ACC 子程序舉例

下面僅對(duì)一些 ACC 子程序進(jìn)行使用舉例,主要說(shuō)明使用 ACC 子程序設(shè)計(jì) Verilog 系統(tǒng)任務(wù)的基本流程。

設(shè)計(jì)需求

本次設(shè)計(jì)一個(gè)計(jì)數(shù)器監(jiān)測(cè)的系統(tǒng)任務(wù)。

一方面該系統(tǒng)任務(wù)監(jiān)測(cè) Verilog 中計(jì)數(shù)器的值,一方面在檢測(cè)到 Verilog 中計(jì)數(shù)時(shí)鐘后也進(jìn)行軟件計(jì)數(shù),并將軟、硬件計(jì)數(shù)值作對(duì)比,以檢查硬件邏輯的正確性。同時(shí)在計(jì)數(shù)器計(jì)數(shù)到特定值時(shí),將一些信號(hào)變量值輸出。

該設(shè)計(jì)思想也是 PLI 接口經(jīng)常使用的場(chǎng)景。軟件完成一種邏輯功能,稱(chēng)之為 CModel。然后硬件也使用 Verilog 完成相同的邏輯功能。通過(guò) PLI 接口程序,軟硬件程序可以同時(shí)執(zhí)行并進(jìn)行結(jié)果對(duì)比,以驗(yàn)證硬件邏輯設(shè)計(jì)的正確性。

設(shè)計(jì)分析

PLI 接口部分使用值變鏈接子程序?qū)r(shí)鐘進(jìn)行監(jiān)視。時(shí)鐘上升沿一旦來(lái)臨,就調(diào)用軟件函數(shù),進(jìn)行軟件計(jì)數(shù),并對(duì)比軟硬件計(jì)數(shù)值。

軟件設(shè)計(jì)

軟件設(shè)計(jì)代碼如下,細(xì)節(jié)在注釋中說(shuō)明,保存到文件 monitor_gyc.c 中。

#include "acc_user.h"

handle  hand_rstn, hand_clk, hand_cnt, hand_cout ;
int cnt_soft  = 0 ;   //軟件計(jì)數(shù)器的值
int cout_soft = 0 ;   //軟件計(jì)數(shù)器的溢出位

//軟件計(jì)數(shù)、對(duì)比函數(shù)
void act_monitor(){
    p_acc_value value ; //聲明 ACC 中定義的結(jié)構(gòu)體變量
    //讀取 Verilog 中信號(hào)變量的值,字符串類(lèi)型
    char *rstn_rtl_str = acc_fetch_value(hand_rstn, "%d", value);
    char *clk_rtl_str  = acc_fetch_value(hand_clk, "%d", value);
    char *cnt_rtl_str  = acc_fetch_value(hand_cnt, "%d", value);
    char *cout_rtl_str = acc_fetch_value(hand_cout, "%d", value);

    //字符串轉(zhuǎn)整型
    int rstn_rtl       = atoi(rstn_rtl_str) ;
    int clk_rtl        = atoi(clk_rtl_str) ;
    int cnt_rtl        = atoi(cnt_rtl_str) ;
    int cout_rtl       = atoi(cout_rtl_str) ;

    //軟件計(jì)數(shù)器
    //復(fù)位
    if (!rstn_rtl) {
        cnt_soft    = 0 ;
        cout_soft   = 0 ;
        io_printf("---iii--- Reset state! \n");
    }
    //正常工作
    else if (rstn_rtl && clk_rtl) {
        if (cnt_soft == 9) {
            cnt_soft    = 0 ;    //計(jì)數(shù)10個(gè)
        }
        else {
            cnt_soft    = cnt_soft + 1 ; //計(jì)數(shù)
        }
    }
    cout_soft = cnt_soft == 9 ? 1 : 0 ; //進(jìn)位

    //在時(shí)鐘為低的時(shí)間對(duì)比軟硬件計(jì)數(shù),此時(shí)為相對(duì)安全的時(shí)刻
    if (!clk_rtl && rstn_rtl) {
        if ((cnt_soft != cnt_rtl) || (cout_soft != cout_rtl)) {
            io_printf("--Err--- rtl cnt: %d, and soft cnt: %d\n", cnt_rtl, cnt_soft);
            io_printf("--Err--- rtl cout: %d, and soft cout: %d\n", cout_rtl, cout_soft);
        }
        //軟硬件計(jì)數(shù)對(duì)比沒(méi)有錯(cuò)誤,則輸出一次計(jì)數(shù)完結(jié)時(shí)信號(hào)的狀態(tài)值
        else if (cnt_soft == 9)  {
            io_printf("--Monitor--- rtl and soft cnt: %d\n", cnt_soft);
            io_printf("--Monitor--- rtl and soft cout: %d\n", cout_soft);
        }
    }
}

//PLI 接口,監(jiān)測(cè)時(shí)鐘變化
void my_monitor() {
    acc_initialize(); //必須進(jìn)行初始化
    //獲取系統(tǒng)任務(wù) my_monitor 第一個(gè)參數(shù)的 handle 變量
    hand_rstn   = acc_handle_tfarg(1);
    hand_clk    = acc_handle_tfarg(2);
    hand_cnt    = acc_handle_tfarg(3);
    hand_cout   = acc_handle_tfarg(4);
    //"#define VCL_VERILOG_LOGIC 2" in acc_user.h
    //Indicates the VCL callback mechanism shall report
    //hand_clk 一旦變化,則調(diào)用 act_monitor 函數(shù)
    acc_vcl_add(hand_clk, act_monitor, null, vcl_verilog_logic);
    acc_close(); //關(guān)閉任務(wù)
}

硬件設(shè)計(jì)

10 進(jìn)制計(jì)數(shù)器的 Verilog 描述如下,保存到文件 counter10.v 中。

module counter10(
   input                rstn,
   input                clk,
   output [3:0]         cnt,
   output               cout);

   reg [3:0]            cnt_temp ;
   always@(posedge clk or negedge rstn) begin
      if (! rstn) begin
         cnt_temp        <= 4'b0 ;
      end
      else if (cnt_temp==4'd9) begin
         cnt_temp        <=4'b000;
      end
      else begin
         cnt_temp        <= cnt_temp + 1'b1 ;
      end
   end

   assign  cout = (cnt_temp==4'd9) ;
   assign  cnt  = cnt_temp ;

endmodule

testbench

testbench 描述如下,保存到文件 test.v 中。

`timescale 1ns/1ps
module test ;
   reg          rstn, clk ;
   wire [3:0]   cnt ;
   wire         cout ;

   initial begin
      rstn      = 0 ;
      clk       = 0 ;
      #19.8;
      rstn      = 1 ;
   end
   always #5 clk = ~clk ;

   counter10 u_cnt(
     .rstn      (rstn),
     .clk       (clk),
     .cnt       (cnt),
     .cout      (cout));

   initial begin
      $my_monitor(test.rstn, test.clk, test.cnt, test.cout);
   end

   initial begin
      forever begin
         #100;
         if ($time >= 300)  $finish ;
      end
   end
endmodule

編譯仿真

Linux 下用如下命令對(duì) monitor_gyc.c 進(jìn)行編譯,輸出 monitor_gyc.o 文件,注意相對(duì)路徑。

gcc -I ${VCS_HOME}/include -c ../tb/monitor_gyc.c

使用 VCS 編譯,需要?jiǎng)?chuàng)建 VCS 可識(shí)別的鏈接文件,文件命名為 pli_gyc.tab, 內(nèi)容如下。

?$my_monitor? 是 Verilog 調(diào)用的系統(tǒng)任務(wù)名字;

call=my_monitor 表示調(diào)用軟件 C 程序中的函數(shù) my_monitor();

acc=rw:* 表示設(shè)置系統(tǒng)任務(wù)的屬性,rw 表示可以對(duì)內(nèi)部數(shù)據(jù)進(jìn)行讀寫(xiě),冒號(hào)后面可以聲明該系統(tǒng)任務(wù)作用的模塊或信號(hào)區(qū)域,":*" 表示可以作用到所有模塊。

$my_monitor call=my_monitor acc=rw:*

VCS 編譯時(shí)增加如下參數(shù)行。

-P ../tb/pli_gyc.tab

仿真結(jié)果 log 如下。

由 log 可知,軟硬件計(jì)數(shù)對(duì)比正確,計(jì)數(shù)器完成一次周期計(jì)數(shù)的狀態(tài)也正常。


點(diǎn)擊這里下載源碼


以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)