转载:计算机人网
http://www.computersren.com/material/MaterialDetails.aspx?id=20140317222345&id2=2014030320451027176
内容:
verilog 的function 例子
code_83top.v
module code_83top(din,dout);
input[7:0] din;
output[2:0] dout;
//程序文本
function[2:0] code; //函数定义
input[7:0] din; //函数只有输入,输出为函数名本身
casex(din)
8'b1xxx_xxxx : code = 3'h7;
8'b01xx_xxxx : code= 3'h6;
8'b001x_xxxx : code= 3'h5;
8'b0001_xxxx : code= 3'h4;
8'b0000_1xxx : code= 3'h3;
8'b0000_01xx : code= 3'h2;
8'b0000_001x : code= 3'h1;
8'b0000_000x : code= 3'h0;
default: code= 3'hx;
endcase
endfunction
assign dout = code(din) ; //函数调用
endmodule
code_83.v
`timescale 1ns/1ns
`include "code_83top.v"
module code_83;
reg[7:0] din;
wire[2:0] dout;
initial
begin //??????
din=8'b1xxx_xxxx;
end
code_83top mycount(din,dout); //??????
initial
$monitor($time,,,"dout=%d",dout);
endmodule
第一个.v的模块可以看作是module,另一个可以测试模块。测试结果为dout=7