ACC 子程序主要完成的功能有:
ACC 子程序可操作的對(duì)象類(lèi)型有:
ACC 子程序的主要特征有:
ACC 子程序的主要類(lèi)型有:
完整的 ACC 子程序及其簡(jiǎn)單用法說(shuō)明參考下一節(jié)《Verilog ACC 子程序列表》。
下面僅對(duì)一些 ACC 子程序進(jìn)行使用舉例,主要說(shuō)明使用 ACC 子程序設(shè)計(jì) Verilog 系統(tǒng)任務(wù)的基本流程。
本次設(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ì)的正確性。
PLI 接口部分使用值變鏈接子程序?qū)r(shí)鐘進(jìn)行監(jiān)視。時(shí)鐘上升沿一旦來(lái)臨,就調(diào)用軟件函數(shù),進(jìn)行軟件計(jì)數(shù),并對(duì)比軟硬件計(jì)數(shù)值。
軟件設(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ù)
}
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 描述如下,保存到文件 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)擊這里下載源碼
更多建議: