gaochy1126 发表于 2023-5-29 16:09

FUNCTION —— VERILOG的函数

举个例子
先以如下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 to_val;
            input char;
        begin
            if ((char >= 7'h30) && (char <= 7'h39)) // 0-9
            begin
                      to_val   = 1'b0;
                        to_val = char;
            end else if (((char >= 7'h41) && (char <= 7'h46)) || // A-F
                        ((char >= 7'h61) && (char <= 7'h66)) )// a-f
                begin
                    to_val   = 1'b0;
                    to_val = char + 4'h9; // gives 10 - 15
            end else begin
                        to_val      = 5'b1_0000;
            end
        end
        endfunction
函数的语法为:

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

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

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

函数不能启动任务。

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

页: [1]
查看完整版本: FUNCTION —— VERILOG的函数