[Verilog HDL]

FUNCTION —— VERILOG的函数

[复制链接]
2519|0
手机看帖
扫描二维码
随时随地手机跟帖
gaochy1126|  楼主 | 2023-5-29 16:09 | 显示全部楼层 |阅读模式
举个例子
先以如下function为例:

它的主要功能是判断输入的字符是否为数字(包含0~9,A~F,a~f);

如果是,就输出数字;如果不是,就将最MSB置位;

源码及注释为:
//***************************************************************************
// Tasks and Functions
//***************************************************************************

        // This function takes the lower 7 bits of a character and converts them
        // to a hex digit. It returns 5 bits - the upper bit is set if the character
        // is not a valid hex digit (i.e. is not 0-9,a-f, A-F), and the remaining
        // 4 bits are the digit
        function [4:0] to_val;
            input [6:0] char;
        begin
            if ((char >= 7'h30) && (char <= 7'h39)) // 0-9
            begin
                      to_val[4]   = 1'b0;
                        to_val[3:0] = char[3:0];
            end else if (((char >= 7'h41) && (char <= 7'h46)) || // A-F
                        ((char >= 7'h61) && (char <= 7'h66)) )  // a-f
                begin
                    to_val[4]   = 1'b0;
                    to_val[3:0] = char[3:0] + 4'h9; // gives 10 - 15
            end else begin
                        to_val      = 5'b1_0000;
            end
        end
        endfunction
函数的语法为:

定义函数时至少要有一个输入参量;可以按照ANSI和module形式直接定义输入端口。例如:

function[63:0] alu (input[63:0] a, b, input [7:0] opcode);
1
在函数的定义中必须有一条赋值语句给函数名具备相同名字的变量赋值;

在函数的定义中不能有任何的时间控制语句,即任何用#,@或wait来标识的语句。

函数不能启动任务。

如果描述语句是可综合的,则必须所有分支均赋值,不予存在不赋值的情况,只能按照组合逻辑方式描述。

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:这个社会混好的两种人:一是有权有势,二是没脸没皮的。

1025

主题

11271

帖子

24

粉丝