服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - 编程技术 - Verilog 8 种编译指令使用详解

Verilog 8 种编译指令使用详解

2023-04-12 16:36向阳逐梦 编程技术

这篇文章主要介绍了Verilog 8 种编译指令使用详解

Verilog 编译指令

编译指令为 Verilog 代码的撰写、编译、调试等提供了极大的便利。

下面介绍下完整的 8 种编译指令,其中前 4 种使用频率较高。

define, undef

在编译阶段,`define 用于文本替换,类似于 C 语言中的 #define

一旦 `define 指令被编译,其在整个编译过程中都会有效。例如,在一个文件中定义:

?
1
`define    DATA_DW     32

则在另一个文件中也可以直接使用 DATA_DW。

?
1
2
3
4
`define    S     $stop;  
//用`S来代替系统函数$stop; (包括分号)
`define    WORD_DEF   reg [31:0]      
//可以用`WORD_DEF来声明32bit寄存器变量

`undef 用来取消之前的宏定义,例如:

?
1
2
3
4
5
6
`define    DATA_DW     32
……
reg  [DATA_DW-1:0]    data_in   ;
……
`undef DATA_DW
`ifdef, `ifndef, `elsif, `else, `endif

这些属于条件编译指令。例如下面的例子中,如果定义了 MCU51,则使用第一种参数说明;如果没有定义 MCU、定义了 WINDOW,则使用第二种参数说明;如果 2 个都没有定义,则使用第三种参数说明。

?
1
2
3
4
5
6
7
`ifdef       MCU51
    parameter DATA_DW = 8   ;
`elsif       WINDOW
    parameter DATA_DW = 64  ;
`else
    parameter DATA_DW = 32  ;
`endif

elsif, else 编译指令对于 ifdef 指令是可选的,即可以只有 ifdef 和 `endif 组成一次条件编译指令块。

当然,也可用 `ifndef 来设置条件编译,表示如果没有相关的宏定义,则执行相关语句。

下面例子中,如果定义了 WINDOW,则使用第二种参数说明。如果没有定义 WINDOW,则使用第一种参数说明。

?
1
2
3
4
5
`ifndef     WINDOW
    parameter DATA_DW = 32 ; 
 `else
    parameter DATA_DW = 64 ;
 `endif

`include

使用 `include 可以在编译时将一个 Verilog 文件内嵌到另一个 Verilog 文件中,作用类似于 C 语言中的 #include 结构。该指令通常用于将全局或公用的头文件包含在设计文件里。

文件路径既可以使用相对路径,也可以使用绝对路径。

?
1
2
`include         "../../param.v"
`include         "header.v"

`timescale

在 Verilog 模型中,时延有具体的单位时间表述,并用 `timescale 编译指令将时间单位与实际时间相关联。

该指令用于定义时延、仿真的单位和精度,格式为:

time_unit 表示时间单位,time_precision 表示时间精度,它们均是由数字以及单位 s(秒),ms(毫秒),us(微妙),ns(纳秒),ps(皮秒)和 fs(飞秒)组成。时间精度可以和时间单位一样,但是时间精度大小不能超过时间单位大小,例如下面例子中,输出端 Z 会延迟 5.21ns 输出 A&B 的结果。

?
1
2
3
4
5
6
7
`timescale 1ns/100ps    //时间单位为1ns,精度为100ps,合法
//`timescale 100ps/1ns  //不合法
module AndFunc(Z, A, B);
    output Z;
    input A, B ;
    assign #5.207 Z = A & B
endmodule

在编译过程中,timescale 指令会影响后面所有模块中的时延值,直至遇到另一个 timescale 指令或 `resetall 指令。

由于在 Verilog 中没有默认的 timescale,如果没有指定 timescale,Verilog 模块就有会继承前面编译模块的 `timescale 参数。有可能导致设计出错。

如果一个设计中的多个模块都带有 `timescale 时,模拟器总是定位在所有模块的最小时延精度上,并且所有时延都相应地换算为最小时延精度,时延单位并不受影响。例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
`timescale 10ns/1ns     
module test;
    reg        A, B ;
    wire       OUTZ ;
 
    initial begin
        A     = 1;
        B     = 0;
        # 1.28    B = 1;
        # 3.1     A = 0;
    end
 
    AndFunc        u_and(OUTZ, A, B) ;
endmodule

在模块 AndFunc 中,5.207 对应 5.21ns。

在模块 test 中,1.28 对应 13ns,3.1 对应 31ns。

但是,当仿真 test 时,由于 AndFunc 中的最小精度为 100ps,因此 test 中的时延精度将进行重新调整。13ns 将对应 130100ps,31ns 将对应 310100ps。仿真时,时延精度也会使用 100ps。仿真时间单位大小没有影响。

如果有并行子模块,子模块间的 `timescale 并不会相互影响。

例如在模块 test 中再例化一个子模块 OrFunc。仿真 test 时,OrFunc 中的 #5.207 延时依然对应 52ns。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//子模块:
`timescale 10ns/1ns      //时间单位为1ns,精度为100ps,合法
module OrFunc(Z, A, B);
    output Z;
    input A, B ;
    assign #5.207 Z = A | B
endmodule
 
//顶层模块:
`timescale 10ns/1ns     
module test;
    reg        A, B ;
    wire       OUTZ ;
    wire       OUTX ;
 
    initial begin
        A     = 1;
        B     = 0;
        # 1.28    B = 1;
        # 3.1     A = 0;
    end
 
    AndFunc        u_and(OUTZ, A, B) ;
    OrFunc         u_and(OUTX, A, B) ;
 
endmodule

此例中,仿真 test 时,OrFunc 中的 #5.207 延时依然对应 52ns。

`timescale 的时间精度设置是会影响仿真时间的。时间精度越小,仿真时占用内存越多,实际使用的仿真时间就越长。所以如果没有必要,应尽量将时间精度设置的大一些。

`default_nettype

该指令用于为隐式的线网变量指定为线网类型,即将没有被声明的连线定义为线网类型。

该实例定义的缺省的线网为线与类型。因此,如果在此指令后面的任何模块中的连线没有说明,那么该线网被假定为线与类型。

该实例定义后,将不再自动产生 wire 型变量。

例如下面第一种写法编译时不会报 Error,第二种写法编译将不会通过。

?
1
2
3
4
5
6
7
//Z1 无定义就使用,系统默认Z1为wire型变量,有 Warning 无 Error
module test_and(
        input      A,
        input      B,
        output     Z);
    assign Z1 = A & B ; 
endmodule
?
1
2
3
4
5
6
7
8
//Z1无定义就使用,由于编译指令的存在,系统会报Error,从而检查出书写错误
`default_nettype none
module test_and(
        input      A,
        input      B,
        output     Z);
    assign Z1 = A & B ; 
endmodule

`resetall

该编译器指令将所有的编译指令重新设置为缺省值。

`resetall 可以使得缺省连线类型为线网类型。

当 resetall 加到模块最后时,可以将当前的 timescale 取消防止进一步传递,只保证当前的 timescale 在局部有效,避免 timescale 的错误继承。

celldefine, endcelldefine

这两个程序指令用于将模块标记为单元模块,他们包含模块的定义。例如一些与、或、非门,一些 PLL 单元,PAD 模型,以及一些 Analog IP 等。

?
1
2
3
4
5
6
7
8
9
`celldefine
module (
    input      clk,
    input      rst,
    output     clk_pll,
    output     flag);
        ……
endmodule
`endcelldefine

unconnected_drive, nounconnected_drive

在模块实例化中,出现在这两个编译指令间的任何未连接的输入端口,为正偏电路状态或者为反偏电路状态。

?
1
2
3
4
5
6
7
8
`unconnected_drive pull1
. . .
 / *在这两个程序指令间的所有未连接的输入端口为正偏电路状态(连接到高电平) * /
`nounconnected_drive
`unconnected_drive pull0
. . .
 / *在这两个程序指令间的所有未连接的输入端口为反偏电路状态(连接到低电平) * /
`nounconnected_drive

以上就是Verilog 8 种编译指令使用详解的详细内容,更多关于Verilog 编译指令的资料请关注服务器之家其它相关文章!

原文链接:https://juejin.cn/post/7179941464032575525

延伸 · 阅读

精彩推荐