相关文章推荐
文武双全的荒野  ·  前端vue项目中$t()的意义_js ...·  12 月前    · 
微醺的硬币  ·  View与ViewGroup layout ...·  12 月前    · 
狂野的火车  ·  分片上传_对象存储-阿里云帮助中心·  1 年前    · 
千杯不醉的电脑桌  ·  使用python提取JSON数据指定内容_p ...·  1 年前    · 
任性的数据线  ·  shell解析curl返回header-掘金·  1 年前    · 
Code  ›  使用SystemVerilog简化FPGA中的接口开发者社区
接口 input fpga
https://cloud.tencent.com/developer/article/1666772
安静的苦瓜
1 年前
作者头像
猫叔Rex
0 篇文章

使用SystemVerilog简化FPGA中的接口

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 科学计算 > 正文

使用SystemVerilog简化FPGA中的接口

发布 于 2020-07-22 17:48:54
841 0
举报

FPGA工程师们应该都会吐槽Verilog的语法,相当的不友好,尤其是对于有很多接口的模块,像AXI4/AXI-Lite这种常用的总线接口,动不动就好几十根线,写起来是相当费劲。

当然现在Xilinx推荐使用纯bd文件的方式来设计FPGA,这样HDL代码就会少了很多。但我们大多数的工程还是无法避免使用HDL来连接两个module。所以本文就推荐使用SystemVerilog来简化FPGA中接口的连接方式。

也许很多FPGA工程师对SystemVerilog并不是很了解,因为以前的FPGA开发工具是不支持SystemVerilog的,导致大家都是用VHDL或者Verilog来开发,但现在Vivado对SystemVerilog的支持已经比较好了,完全可以使用SystemVerilog写出可综合的FPGA程序,而且FPGA开发中只会使用的SystemVerilog语法的一小部分,入门也很快,因此建议FPGA工程师学一下SystemVerilog。

本文中用到的 logic 关键字的解释可以参考 SystemVerilog教程之数据类型1

此次例程也比较简单,有两个模块 module1 和 module2 , module1 中输出 a 和 b ,在 module2 中完成加法后再返还给 module1 ,最终输出的 led=c | a 。

image-20200720203534072

首先用Verilog来实现,代码也比较简单,就简单解释一句:文件格式都是 .sv ,这是因为SystemVerilog的语法都是包含Verilog的。

//top.sv
module top(
 input        clk,
 input        rst,
 output [3:0] led
logic    [3:0] a ;
logic    [3:0] b ;
logic    [3:0] c ;
module1 inst_module1(
    .clk  (clk  ),
    .rst  (rst  ),
    .a    (a),
    .b    (b),
    .c    (c),
    .led  (led)
module2 inst_module2(
    .clk  (clk  ),
    .rst  (rst  ),
    .a    (a),
    .b    (b),
    .c    (c)
endmodule
// module1.sv
module module1(
 input clk,
 input rst,
 output logic [3:0] a,
 output logic [3:0] b,
 input  logic [3:0] c,
 output logic [3:0] led
 assign led = c | a;
 always @ ( posedge clk ) begin
    if(rst) begin
        a <= 4'd1;
        b <= 4'd2;
    else begin
        a <= a + 1'b1;
        b <= b + 1'b1;
endmodule
// module2.sv
module module2(
 input clk,
 input rst,
 input  logic [3:0] a,
 input  logic [3:0] b,
 output logic [3:0] c
 always @ ( posedge clk ) begin
    if(rst) 
        c <= 4'd1;
        c <= a + b;
endmodule

综合之后的Schematic如下图所示:(为了更好的表示电路结构,我将 flatten_hierarchy 选为了 none )

image-20200720192328527

下面我们把程序稍作改动,将a/b/c三个接口使用SystemVerilog中的 interface 来连接。

在工程中添加 my_itf.sv 文件如下:

// my_itf.sv
interface my_itf;
   logic [3:0] a, b, c;
   modport mod1 (input c,    output a, b);
   modport mod2 (input a, b, output c  );
endinterface : my_itf

关键字 interface 就表示要创建一个接口模块,里面包含了3个接口:a/b/c。

modport 定义了这三个接口的方向,对于 module1 来说,a和b是输出,c是输入;对于 module2 来说,a和b是输入,c是输出。

注:也可以不使用 modport ,Vivado会根据代码自动推断出接口的方向,但不建议这么做

修改 module1.sv 如下,其中a/b/c端口换成了 my_itf.mod1 itf_abc , my_itf.mod1 就表示my_itf接口的方向按照 mod1 中指定的,而且代码中的a、b、c要相应的换成 itf_abc.a 、 itf_abc.b 、 itf_abc.c .

// module1.sv
module module1(
 input clk,
 input rst,
 my_itf.mod1 itf_abc,
 output logic [3:0] led
 assign led = itf_abc.c | itf_abc.a;
 always @ ( posedge clk ) begin
    if(rst) begin
        itf_abc.a <= 4'd1;
        itf_abc.b <= 4'd2;
    else begin
        itf_abc.a <= itf_abc.a + 1'b1;
        itf_abc.b <= itf_abc.b + 1'b1;
endmodule

修改 module2.sv 代码如下,原则跟上面是一样的,不再赘述。

// module2.sv
module module2(
 input clk,
 input rst,
 my_itf.mod2 itf_abc
 always @ ( posedge clk ) begin
    if(rst) 
        itf_abc.c <= 4'd1;
        itf_abc.c <= itf_abc.a + itf_abc.b;
endmodule

修改 top.sv 如下,例化 my_itf 接口,将 itf_abc.mod1 传给 module1 ,将 itf_abc.mod2 传给 module2 .

// top.sv
module top(
 input        clk,
 input        rst,
 output [3:0] led
    logic    [3:0] a ;
    logic    [3:0] b ;
    logic    [3:0] c ;
    my_itf itf_abc();
    module1 inst_module1(
        .clk       (clk  ),
        .rst       (rst  ),
        .itf_abc   (itf_abc.mod1),
        .led       (led)
    module2 inst_module2(
 
推荐文章
文武双全的荒野  ·  前端vue项目中$t()的意义_js $t-CSDN博客
12 月前
微醺的硬币  ·  View与ViewGroup layout 过程_[anr warning]onmeasure time too long csdn-CSDN博客
12 月前
狂野的火车  ·  分片上传_对象存储-阿里云帮助中心
1 年前
千杯不醉的电脑桌  ·  使用python提取JSON数据指定内容_python获取json数据中某个字段_hrflex的博客-CSDN博客
1 年前
任性的数据线  ·  shell解析curl返回header-掘金
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号