ACC 子程序主要完成的功能有:
ACC 子程序可操作的對象類型有:
ACC 子程序的主要特征有:
ACC 子程序的主要類型有:
完整的 ACC 子程序及其簡單用法說明參考下一節(jié)《Verilog ACC 子程序列表》。
下面僅對一些 ACC 子程序進(jìn)行使用舉例,主要說明使用 ACC 子程序設(shè)計 Verilog 系統(tǒng)任務(wù)的基本流程。
本次設(shè)計一個計數(shù)器監(jiān)測的系統(tǒng)任務(wù)。
一方面該系統(tǒng)任務(wù)監(jiān)測 Verilog 中計數(shù)器的值,一方面在檢測到 Verilog 中計數(shù)時鐘后也進(jìn)行軟件計數(shù),并將軟、硬件計數(shù)值作對比,以檢查硬件邏輯的正確性。同時在計數(shù)器計數(shù)到特定值時,將一些信號變量值輸出。
該設(shè)計思想也是 PLI 接口經(jīng)常使用的場景。軟件完成一種邏輯功能,稱之為 CModel。然后硬件也使用 Verilog 完成相同的邏輯功能。通過 PLI 接口程序,軟硬件程序可以同時執(zhí)行并進(jìn)行結(jié)果對比,以驗證硬件邏輯設(shè)計的正確性。
PLI 接口部分使用值變鏈接子程序?qū)r鐘進(jìn)行監(jiān)視。時鐘上升沿一旦來臨,就調(diào)用軟件函數(shù),進(jìn)行軟件計數(shù),并對比軟硬件計數(shù)值。
軟件設(shè)計代碼如下,細(xì)節(jié)在注釋中說明,保存到文件 monitor_gyc.c 中。
#include "acc_user.h"
handle hand_rstn, hand_clk, hand_cnt, hand_cout ;
int cnt_soft = 0 ; //軟件計數(shù)器的值
int cout_soft = 0 ; //軟件計數(shù)器的溢出位
//軟件計數(shù)、對比函數(shù)
void act_monitor(){
p_acc_value value ; //聲明 ACC 中定義的結(jié)構(gòu)體變量
//讀取 Verilog 中信號變量的值,字符串類型
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) ;
//軟件計數(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 ; //計數(shù)10個
}
else {
cnt_soft = cnt_soft + 1 ; //計數(shù)
}
}
cout_soft = cnt_soft == 9 ? 1 : 0 ; //進(jìn)位
//在時鐘為低的時間對比軟硬件計數(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);
}
//軟硬件計數(shù)對比沒有錯誤,則輸出一次計數(shù)完結(jié)時信號的狀態(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)測時鐘變化
void my_monitor() {
acc_initialize(); //必須進(jìn)行初始化
//獲取系統(tǒng)任務(wù) my_monitor 第一個參數(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ù)
}
10 進(jìn)制計數(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 描述如下,保存到文件 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 下用如下命令對 monitor_gyc.c 進(jìn)行編譯,輸出 monitor_gyc.o 文件,注意相對路徑。
gcc -I ${VCS_HOME}/include -c ../tb/monitor_gyc.c
使用 VCS 編譯,需要創(chuàng)建 VCS 可識別的鏈接文件,文件命名為 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 表示可以對內(nèi)部數(shù)據(jù)進(jìn)行讀寫,冒號后面可以聲明該系統(tǒng)任務(wù)作用的模塊或信號區(qū)域,":*" 表示可以作用到所有模塊。
$my_monitor call=my_monitor acc=rw:*
VCS 編譯時增加如下參數(shù)行。
-P ../tb/pli_gyc.tab
仿真結(jié)果 log 如下。
由 log 可知,軟硬件計數(shù)對比正確,計數(shù)器完成一次周期計數(shù)的狀態(tài)也正常。
點(diǎn)擊這里下載源碼
更多建議: