在 Verilog 上实现倒数的方法
Ways to implement recipricals on Verilog
我想在 Verilog 上实现一个互易块,稍后将在 FPGA 上合成。输入应该是带符号的 32 位字长和 16 位小数长度。输出应具有相同的格式。
示例
input : x ---> output ---> 1/x
我已经使用内置的 IP 核分频器解决了这个问题。我想知道是否有 elegant/altenative 解决这个问题的方法,例如通过移位或 2 的补码与一些异或研磨。
我已经按照手册中的说明使用了 IP 内核来实现逆运算,但出于某种原因我不太理解结果是错误的,需要将其向左移动 1。例如; 1 的倒数为 0.5 。 2 的倒数为 1.
下面是手册的一部分和我的测试平台代码
测试台
module reciprical_tb;
// Inputs
reg clk;
reg [1:0] dividend;
reg [31:0] divisor;
// Outputs
wire rfd;
wire [1:0] quotient;
wire [31:0] fractional;
// Instantiate the Unit Under Test (UUT)
reciprical uut (
.rfd(rfd),
.clk(clk),
.dividend(dividend),
.quotient(quotient),
.divisor(divisor),
.fractional(fractional)
);
// clock
always begin
#5 clk = ~clk;
end
initial begin
// Initialize Inputs
clk = 0;
dividend = 2'b1; // 1
divisor = 2**16;; // = 1 when fraction length is 16bit
// Wait 100 ns for global reset to finish
#100;
// Add stimulus here :: Inverse of 2 should give 0.5
//$display("inv(%g) => %g || inv = %b",$itor(divisor)*2.0**-16, $itor(fractional)*2.0**-16, fractional); //gives zero
$monitor("inv(%d) => q = %d || inv = %b", divisor>>>16,fractional>>>16, fractional); //gives a wrong answer by a factor of 2
// Using the monitor i get inv(1) = 0.5 instead of 1.
#100;
end
endmodule
手册部分(第 4 页):
...
The divider can be used to implement the reciprocal of X; that is the 1/X function. To do this, the
dividend bit width is set to 2 and fractional mode is selected. The dividend input is then tied to 01 for
both unsigned or signed operation, and the X value is provided via the divisor input.
使用了 Ip 内核
IP 内核通常非常高效,但它们会使用架构乘法器和合理数量的逻辑。根据您需要的精度,也可以通过存储在块 RAM 中的查找 table 来完成。使用原始输入作为地址的 RAM 对于设备来说太大了。但是您可以存储倒数曲线并预先缩放输入并在输出处应用偏移。您可以通过在曲线上的两点之间进行线性插值来进一步减少内存使用量。
这取决于您拥有的资源以及您需要的准确性。
正在尝试调试部分问题:
你能试试吗:
// Wait 10 clock cycles
repeat (10) begin
@(posedge clk);
end
// Add stimulus here :: Inverse of 2 should give 0.5
$display("dsiplay inv(%g) => %g || inv = %b",$itor(divisor)*2.0**-16, $itor(fractional)*2.0**-16, fractional); //gives zero
$monitor("inv(%d) => q = %d || inv = %b", divisor>>>16,fractional>>>16, fractional); //gives a wrong answer by a factor of 2
// Using the monitor i get inv(1) = 0.5 instead of 1.
// Wait 10 clock cycles
repeat (10) begin
@(posedge clk);
end
$display("dsiplay inv(%g) => %g || inv = %b",$itor(divisor)*2.0**-16, $itor(fractional)*2.0**-16, fractional);
//End simulation
$finish();
由于监视器只发出一次,它可能在 200ns 后才真正触发并输出更新值。
我想在 Verilog 上实现一个互易块,稍后将在 FPGA 上合成。输入应该是带符号的 32 位字长和 16 位小数长度。输出应具有相同的格式。
示例
input : x ---> output ---> 1/x
我已经使用内置的 IP 核分频器解决了这个问题。我想知道是否有 elegant/altenative 解决这个问题的方法,例如通过移位或 2 的补码与一些异或研磨。
我已经按照手册中的说明使用了 IP 内核来实现逆运算,但出于某种原因我不太理解结果是错误的,需要将其向左移动 1。例如; 1 的倒数为 0.5 。 2 的倒数为 1.
下面是手册的一部分和我的测试平台代码
测试台
module reciprical_tb;
// Inputs
reg clk;
reg [1:0] dividend;
reg [31:0] divisor;
// Outputs
wire rfd;
wire [1:0] quotient;
wire [31:0] fractional;
// Instantiate the Unit Under Test (UUT)
reciprical uut (
.rfd(rfd),
.clk(clk),
.dividend(dividend),
.quotient(quotient),
.divisor(divisor),
.fractional(fractional)
);
// clock
always begin
#5 clk = ~clk;
end
initial begin
// Initialize Inputs
clk = 0;
dividend = 2'b1; // 1
divisor = 2**16;; // = 1 when fraction length is 16bit
// Wait 100 ns for global reset to finish
#100;
// Add stimulus here :: Inverse of 2 should give 0.5
//$display("inv(%g) => %g || inv = %b",$itor(divisor)*2.0**-16, $itor(fractional)*2.0**-16, fractional); //gives zero
$monitor("inv(%d) => q = %d || inv = %b", divisor>>>16,fractional>>>16, fractional); //gives a wrong answer by a factor of 2
// Using the monitor i get inv(1) = 0.5 instead of 1.
#100;
end
endmodule
手册部分(第 4 页):
... The divider can be used to implement the reciprocal of X; that is the 1/X function. To do this, the dividend bit width is set to 2 and fractional mode is selected. The dividend input is then tied to 01 for both unsigned or signed operation, and the X value is provided via the divisor input.
使用了 Ip 内核
IP 内核通常非常高效,但它们会使用架构乘法器和合理数量的逻辑。根据您需要的精度,也可以通过存储在块 RAM 中的查找 table 来完成。使用原始输入作为地址的 RAM 对于设备来说太大了。但是您可以存储倒数曲线并预先缩放输入并在输出处应用偏移。您可以通过在曲线上的两点之间进行线性插值来进一步减少内存使用量。
这取决于您拥有的资源以及您需要的准确性。
正在尝试调试部分问题:
你能试试吗:
// Wait 10 clock cycles
repeat (10) begin
@(posedge clk);
end
// Add stimulus here :: Inverse of 2 should give 0.5
$display("dsiplay inv(%g) => %g || inv = %b",$itor(divisor)*2.0**-16, $itor(fractional)*2.0**-16, fractional); //gives zero
$monitor("inv(%d) => q = %d || inv = %b", divisor>>>16,fractional>>>16, fractional); //gives a wrong answer by a factor of 2
// Using the monitor i get inv(1) = 0.5 instead of 1.
// Wait 10 clock cycles
repeat (10) begin
@(posedge clk);
end
$display("dsiplay inv(%g) => %g || inv = %b",$itor(divisor)*2.0**-16, $itor(fractional)*2.0**-16, fractional);
//End simulation
$finish();
由于监视器只发出一次,它可能在 200ns 后才真正触发并输出更新值。