[Verilog HDL] FUNCTION —— VERILOG的函数

[复制链接]
10393|0
 楼主| gaochy1126 发表于 2023-5-29 16:09 | 显示全部楼层 |阅读模式
举个例子
先以如下function为例:

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

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

源码及注释为:
  1. //***************************************************************************
  2. // Tasks and Functions
  3. //***************************************************************************

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

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

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

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

函数不能启动任务。

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

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

1205

主题

11937

帖子

26

粉丝
快速回复 在线客服 返回列表 返回顶部