如何在interface中处理DUT中的inout信号
如果在dut中声明为inout类型的信号,处理的方式如下:
例如dut中声明的信号为inout [7:0] data;
在interface声明3个信号,一个是wire型的信号: wire logic [7:0] data;一个是将外部信号赋值给data的 input 类型的信号:logic [7:0] in_data; 第三个是将data赋值给外部的信号:logic [7:0] out_data; 在interface中利用条件语句将外部变量赋值给data,将data赋值给外部变量,这里注意当外部变量不向data赋值时,需要将data赋值为高组态;当data不向外部变量赋值时,需要将外部变量赋值为0。赋值示例如下:
assign data=(a==1) ?in_data :'hzz;assign out_data=(a==0) ?data :'h00;
完整的示例如下:
module top(clk,a,data,rst,addr);input clk,a,rst;
inout [7:0] data;
input [7:0] addr;logic [7:0] data_reg ,data_reg_reg;always @(posedge clk or negedge rst)if(!rst)begindata_reg<=0; data_reg_reg<=0;endelse if (a==1)data_reg<=data;else if (a==0)begin//data_reg<=data_reg+1; data_reg_reg<=data_reg_reg+2;endassign data= (a==0) ? data_reg_reg : 'hzz;endmoduleinterface simple_bus;logic clk,a,rst;wire logic [7:0] data;logic [7:0] addr;logic [7:0] in_data;logic [7:0] out_data;assign data=(a==1) ?in_data :'hzz;assign out_data=(a==0) ?data :'h00;endinterfaceclass testbench ;virtual simple_bus this_s;function new (virtual simple_bus s);this_s=s;
endfunctiontask assignment;repeat(10) @(posedge this_s.clk);//force this_s.a=1;this_s.in_data=66;repeat (10) @(posedge this_s.clk);//force this_s.a=1;this_s.in_data=33;repeat(10) @(posedge this_s.clk);this_s.a=0;// force this_s.data='h77;
endtaskendclassmodule tb;simple_bus bus();top inst(.clk(bus.clk),.a(bus.a) ,.rst(bus.rst), .data(bus.data) ,.addr(bus.addr) );testbench class_test=new(bus);;//class_test =new(bus);initial beginbus.clk=0;bus.rst=0;#1000;bus.rst=1;class_test.assignment;//bus.a=0;/* #10000;bus.a=1; force bus.data='d8;#100; force bus.data='d3;#100 ; force bus.data='d7;#10000;bus.a=0; */endalways #5 bus.clk= ~bus.clk;endmodule
注意:
不能在class中的task中向wire型变量使用force强制赋值,或者不能使用assign向wire变量连续赋值。
不能在tb中的initial 语句中对interface中的wire型变量强制赋值。