我在Verilog中需要一个用于32位输入的rightrotate函数,因为它没有被定义为运算符(x >>> y).
手动输入这样的输入很容易:
wire [31:0] test = 32'd12345;
wire [31:0] rotated_1 = {test[0:0],test[31:1]};
wire [31:0] rotated_3 = {test[2:0],test[31:3]};
Run Code Online (Sandbox Code Playgroud)
测试平台的输出符合预期:
original: 00000000000000000011000000111001
rotate_1: 10000000000000000001100000011100
rotate_3: 00100000000000000000011000000111
Run Code Online (Sandbox Code Playgroud)
我们看到,函数rotate(inp,x)应该像这样工作:
function rotate;
input [31:0] inp;
input [4:0] x;
begin
rotate = {inp[x-1:0],inp[31:x]};
end
endfunction
Run Code Online (Sandbox Code Playgroud)
问题是:x不是常量,所以它不能编译.要使用[a:b]指定范围,a和b都必须是常量.
解决方案似乎使用参数:
function rotate;
parameter integer x = 1;
input [31:0] inp;
begin
rotate = {inp[x-1:0],inp[31:x]};
end
endfunction
Run Code Online (Sandbox Code Playgroud)
好吧,它确实编译,但与模块不同,你可以使用像这样的已更改参数进行实例化
param_module #(3) pm_inst(...);
Run Code Online (Sandbox Code Playgroud)
,这不适用于功能.事实上,从阅读语法的Verilog,我看不到的方式在所有指定的参数:
<function_call>
::= <name_of_function> ( <expression> <,<expression>>* ) …
Run Code Online (Sandbox Code Playgroud)