接口
接口中使用modport
interface chip_bus(input logic clock,resetN);
logic interrupt_request,grant,ready;
logic[31:0] addres;
wire[63:0] data;
modport master(input interrupt_request,
input address ,
output grant,ready ,
inout data ,
input clock,resetN);
modport slave(output interrupt_request,
input grant,ready ,
inout data ,
input clock,resetN);
endinterface
modport中只定义信号方向,而忽略信号类型和位宽。信号类型和位宽在interface中定义。
使用方法
1
module primary(interface pins);//通用接口端口
……
endmodule
module secondary(chip_bus pins);//特定接口端口
……
endmodule
module chip(input logic clock,resetN);
chip_bus bus(clock,resetN);//接口实例
primary inst_1(bus.master);//使用chip_bus中的master方向定义
secondary inst_2(bus.slave);//使用slave方向定义
endmodule
2
module primary(chip_bus.master pins);//直接选择master
……
endmodule
module secondary(chip_bus.slave pins);//直接选择slvae
……
endmodule
module chip(input logic clock,resetN);
chip_bus bus(clock,resetN);//接口实例
primary inst_1(bus);//已经指定过modport类型后直接传实例化的bus
secondary inst_2(bus);
endmodule
若没有指定modport则线网信号方向默认为inout,所有变量类型默认为ref。这样模块就可以通过引用的方式访问接口中的变量。
模块通过modport只能访问modport中规定的信号,若未指定modport则可以访问整个interface。
接口中使用任务和函数
在接口中声明的任务和函数可以作为接口方法(method)来引用。
可以将模块间通信相关的细节转移到接口内描述,减少重复编写。
接口可以包含不同的接口方法,每个方法使用不同的协议或类型。可根据需要,向不同的modport中导入不同的方法。
//方法1
modport in(import Read,
import parity_gen,
input clock,resetN);
//方法2
modport in(import task Read(input[63:0] data,output[31:0]address),
import function parity_gen(input[63:0]data),
input clock,resetN);
ps
interface math_bus(input logic clock,resetN);
int a_int,b_int,result_int;
real a_real,b_real,result_real;
……
task IntegerRead(output int a_int,b_int);
……//进行握手来提取a和b的值
endtask
task FloatingPointRead(output real a_real,b_real);
……//进行握手来提取a和b的值
endtask
modport int_io(import IntegerRead,
input clock,resetN,
output result_int);
modport fp_io(import FloatingPointRead,
input clock,resetN,
output result_real);
endinterface
module dual_mu(input logic clock,resetN);
math_bus bus_a ;
math_bus bus_b ;
interger_math_unit i1(bus_a.int_io);
//使用整数类型连接到接口
floating_point_unit i2(bus_b.fp_io);
//使用实数类型连接到接口
endmodule
//*****************模块定义***********************
module integer_math_unit(interface io);
int a_reg,b_reg;
always @(posedge io.clock)
begin
io.IntegerRead(a_reg,b_reg);//调用接口中方法
……//进行运算
end
endmodule
module floating_math_unit(interface io);
int a_reg,b_reg;
always @(posedge io.clock)
begin
io.FloatingPointRead(a_reg,b_reg);//调用接口中方法
……//进行运算
end
endmodule
综合后导入的任务或函数会最为模块的局部副本
SV可使用导出export任务和函数,但不可综合
接口中使用过程块
可使用always,always_comb,always_ff,always_latch,initial,final,assign
接口可以参数化
interface math_bus #(parameter type DTYPE = int)
(input logic clock);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】