FPGA第 11 篇,Verilog 系统函数( Verilog 中的系统函数)

前言

Verilog 作为一种强大的硬件描述语言,不仅提供了用于设计和仿真数字电路的基础语法,还包含了丰富的系统函数,帮助我们高效地完成复杂的硬件操作。系统函数是 Verilog 语言中预定义的特殊函数,通常以 $ 开头,它们涵盖了多种用途,如仿真控制、数据转换、数学运算、时间获取和显示信息等。

系统函数在硬件设计和验证过程中发挥了不可或缺的作用。例如,我们可以通过系统函数方便地输出仿真信息、执行复杂的数学运算或获取仿真时间。常见的 Verilog 系统函数包括 $display 用于信息输出,$monitor 用于实时监控信号变化,$time 获取仿真时间,以及 $random 生成随机数。

通过灵活应用这些系统函数,我们能够简化代码,增强可读性,并提高调试效率。在硬件开发和验证过程中,充分利用 Verilog 提供的系统函数将大大提升设计的效率和可靠性。掌握这些开发技巧,我们可以高效地设计和验证 FPGA 电路,从而实现所需的数字系统功能。之前介绍了 Verilog 中的运算符和分支语句,请看,

Verilog 中的运算符和分支语句icon-default.png?t=O83Ahttps://blog.csdn.net/weixin_65793170/article/details/141829820?spm=1001.2014.3001.5502

这里我们来分享一下 Verilog 中的系统函数,记录一下


一. 分类介绍

1. Verilog 系统函数分类

        Verilog 系统函数(System Functions)通常以 $ 开头,并用于执行各种标准操作,例如格式化输出、仿真控制、时间管理等。Verilog-2001 标准定义了几十个系统函数,这些函数大致可分为以下几类:

  1. 显示相关函数
  2. 仿真时间相关函数
  3. 文件输入输出相关函数
  4. 数学函数
  5. 随机函数
  6. 仿真控制函数


二. 分类详细

1. 显示(Display)相关函数

(1)函数介绍

这些函数用于在仿真过程中输出信息,类似于 C 语言中的 printf

函数名 功能 用法
$display 输出一行文字并自动换行 $display(格式字符串, 参数1, 参数2, ...)
$write 输出一行文字但不换行 $write(格式字符串, 参数1, 参数2, ...)
$strobe 输出信号值,在当前时间步之后再显示 $strobe(格式字符串, 参数1, 参数2, ...)
$monitor 监控变量值的变化并输出 $monitor(格式字符串, 参数1, 参数2, ...)
$stop 停止仿真 $stop
$finish 结束仿真 $finish

(2)代码示例

module display_example;
    reg [7:0] a = 8'd15;
    reg [7:0] b = 8'd25;

    initial begin
        // 输出一行文字并自动换行
        $display("a=%0d, b=%0d", a, b);  // 输出: a=15, b=25

        // 输出一行文字但不换行
        $write("a=%0d", a);  // 输出: a=15
        $write(", b=%0d", b);  // 输出: , b=25

        // 输出信号值,在当前时间步之后再显示
        $strobe("a=%0d, b=%0d", a, b);  // 输出: a=15, b=25 (在当前时间步之后)

        // 监控变量值的变化并输出
        $monitor("a=%0d, b=%0d", a, b);  // 每次 a 或 b 变化时都会输出
    end
endmodule

2. 仿真时间相关函数

(1)函数介绍

这些函数用于获取仿真时间和延迟操作。

函数名 功能 用法
$time 返回当前仿真时间,单位为 timescale 设定 $time
$stime 返回当前仿真时间(32位整型) $stime
$realtime 返回当前仿真时间,精度为实数 $realtime
$realtobits 实数转换为位模式 $realtobits(实数)
$bitstoreal 位模式转换为实数 $bitstoreal(位模式)

(2)代码示例

module time_example;
    reg [7:0] a = 8'd15;

    initial begin
        #10;  // 延迟 10 时间单位
        // 输出当前仿真时间
        $display("Current simulation time: %0t", $time);  
// 输出: Current simulation time: 10

        // 输出当前仿真时间(32位整型)
        $display("Current simulation time (32-bit): %0d", $stime);  
// 输出: Current simulation time (32-bit): 10

        // 输出当前仿真时间,精度为实数
        $display("Current simulation time (real): %0f", $realtime);  
// 输出: Current simulation time (real): 10.000000

        // 实数转换为位模式
        real real_val = 3.14;
        reg [31:0] bits;
        bits = $realtobits(real_val);
        $display("Bits representation of real value: %h", bits);  
// 输出: Bits representation of real value: 4048f5c3

        // 位模式转换为实数
        real new_val;
        new_val = $bitstoreal(bits);
        $display("Real value from bits: %0f", new_val);  
// 输出: Real value from bits: 3.140000
    end
endmodule

3. 文件输入输出相关函数

(1)函数介绍

用于从文件中读取或向文件中写入数据。

函数名 功能 用法
$fopen 打开文件,返回文件句柄 integer file = $fopen("filename", "mode");
$fclose 关闭文件 $fclose(文件句柄);
$fdisplay 向文件写入数据并换行 $fdisplay(文件句柄, 格式字符串, 参数1, 参数2)
$fwrite 向文件写入数据不换行 $fwrite(文件句柄, 格式字符串, 参数1, 参数2)
$fscanf 从文件中读取格式化数据 $fscanf(文件句柄, 格式字符串, 参数1, 参数2)

(2)代码示例

module file_io_example;
    reg [7:0] a = 8'd15;
    integer file;

    initial begin
        // 打开文件以写入模式
        file = $fopen("output.txt", "w");

        // 向文件写入数据并换行
        $fdisplay(file, "a=%0d", a);  
// 将 "a=15" 写入 "output.txt"

        // 向文件写入数据不换行
        $fwrite(file, "Value of a is %0d", a);  
// 将 "Value of a is 15" 写入 "output.txt"

        // 关闭文件
        $fclose(file);
    end
endmodule

4. 数学函数

(1)函数介绍

用于执行一些常见的数学运算。

函数名 功能 用法
$clog2 返回大于等于输入值的最小对数2值 $clog2(值)
$ln 自然对数 $ln(值)
$log10 10为底的对数 $log10(值)
$exp 指数函数,返回 e 的 x 次幂 $exp(值)
$sqrt 返回平方根 $sqrt(值)
$pow 幂运算,返回 x 的 y 次幂 $pow(基数, 指数)

(2)代码示例

module math_functions_example;
    real num = 4.0;
    real result;

    initial begin
        // 计算对数2
        $display("clog2 of 16: %0d", $clog2(16));  
// 输出: clog2 of 16: 4

        // 计算自然对数
        $display("Natural log of 2.718: %0f", $ln(2.718));  
// 输出: Natural log of 2.718: 0.999

        // 计算10为底的对数
        $display("Log base 10 of 1000: %0f", $log10(1000));  
// 输出: Log base 10 of 1000: 3.000

        // 计算指数
        $display("Exp of 1: %0f", $exp(1));  
// 输出: Exp of 1: 2.718

        // 计算平方根
        $display("Square root of 16: %0f", $sqrt(16));  
// 输出: Square root of 16: 4.000

        // 计算幂运算
        result = $pow(2, 3);
        $display("2^3: %0f", result);  // 输出: 2^3: 8.000
    end
endmodule

5. 随机函数

(1)函数介绍

用于生成随机数,常用于测试仿真。

函数名 功能 用法
$random 返回一个随机数 integer r = $random;
$urandom 返回一个无符号随机数 integer ur = $urandom;
$urandom_range 返回一个在指定范围内的无符号随机数 integer ur = $urandom_range(下限, 上限);

(2)代码示例

module random_functions_example;
    integer r;
    integer ur;
    integer ur_range;

    initial begin
        // 生成一个随机数
        r = $random;
        $display("Random number: %0d", r);  
// 输出: Random number: <随机值>

        // 生成一个无符号随机数
        ur = $urandom;
        $display("Unsigned random number: %0d", ur);  
// 输出: Unsigned random number: <随机值>

        // 生成一个在指定范围内的无符号随机数
        ur_range = $urandom_range(1, 10);
        $display("Random number between 1 and 10: %0d", ur_range);  
// 输出: Random number between 1 and 10: <随机值>
    end
endmodule

6. 仿真控制函数

(1)函数介绍

用于控制仿真的执行。

函数名 功能 用法
$stop 暂停仿真,进入交互式调试模式 $stop;
$finish 结束仿真并关闭模拟器 $finish;

(2)代码示例

module simulation_control_example;
    initial begin
        // 暂停仿真,进入交互式调试模式
        $stop;  // 仿真将暂停,用户可以检查当前状态

        // 结束仿真并关闭模拟器
        $finish;  // 仿真将结束
    end
endmodule

这些只是 Verilog 中的一些常见系统函数,还存在其它一些更高级的系统任务或函数。学习编程语言还得多写多练,下面持续分享记录。

创作不易,感觉有用,就一键三连,感谢(●'◡'●)

猜你喜欢

转载自blog.csdn.net/weixin_65793170/article/details/141935432