首页 > 解决方案 > 在 Verilog 中实现 floor 函数

问题描述

我在 Verilog 中实现需要整数的硬件。例如,如果 7 除以 3,则答案为 1。数字也可以是负数。任何人都知道如何实现该功能?

module test(clk, a, b, c);
 
input  [4 : 0] a,b;
output [4 : 0]   c;
wire   [4 : 0]   x;
always @(posedge clk)
begin
x = a / b;
*c = floor(x);*
end
endmodule

标签: verilog

解决方案


尝试使用重复减法执行除法算法。下面是一个伪代码。

//Example :- 7/2
temp1 = 7; cntr = 'd0; temp2 = 2  //Store the number in a register and init counter
//Perform temp1-temp2 and increment cntr until temp1>=temp2
if(temp1 >= temp2)
begin
 temp1 <= temp1 - temp2;
 cntr <= cntr + 1'd1;
end

//cntr holds the quotient, which is 3 for our case.

floor_val = (temp1 == 'd0) ? cntr : cntr - 1'd1; //Final divided value

推荐阅读