Verilog实现32位数开根号代码
时间: 2023-06-12 08:07:13 浏览: 192
以下是使用Verilog语言实现32位数开根号的代码:
module sqrt32 #(parameter WIDTH=32)(
input logic signed [WIDTH-1:0] x,
output logic signed [WIDTH-1:0] y
);
logic [WIDTH-1:0] x_reg;
logic [WIDTH-1:0] y_reg;
logic [WIDTH+1:0] y_next;
logic [WIDTH-1:0] temp;
assign y = y_reg;
always @(*) begin
x_reg = x;
y_next = {y_reg[WIDTH-1], y_reg[WIDTH-2:0]};
temp = y_reg - y_next;
if (x_reg >= (y_reg * y_reg)) begin
y_next = y_reg + (1 << (WIDTH-2));
end
else if (x_reg >= ((y_reg - (1 << (WIDTH-2))) * (y_reg - (1 << (WIDTH-2))))) begin
y_next = y_reg + (1 << (WIDTH-3));
end
else if (x_reg >= ((y_reg - (3 << (WIDTH-3))) * (y_reg - (3 << (WIDTH-3))))) begin
y_next = y_reg + (1 << (WIDTH-4));
end
else if (x_reg >= ((y_reg - (7 << (WIDTH-4))) * (y_reg - (7 << (WIDTH-4))))) begin
y_next = y_reg + (1 << (WIDTH-5));
end
else if (x_reg >= ((y_reg - (15 << (WIDTH-5))) * (y_reg - (15 << (WIDTH-5))))) begin
y_next = y_reg + (1 << (WIDTH-6));
end
else if (x_reg >= ((y_reg - (31 << (WIDTH-6))) * (y_reg - (31 << (WIDTH-6))))) begin
y_next = y_reg + (1 << (WIDTH-7));
end
else if (x_reg >= ((y_reg - (63 << (WIDTH-7))) * (y_reg - (63 << (WIDTH-7))))) begin
y_next = y_reg + (1 << (WIDTH-8));
end
else if (x_reg >= ((y_reg - (127 << (WIDTH-8))) * (y_reg - (127 << (WIDTH-8))))) begin
y_next = y_reg + (1 << (WIDTH-9));
end
else if (x_reg >= ((y_reg - (255 << (WIDTH-9))) * (y_reg - (255 << (WIDTH-9))))) begin
y_next = y_reg + (1 << (WIDTH-10));
end
else if (x_reg >= ((y_reg - (511 << (WIDTH-10))) * (y_reg - (511 << (WIDTH-10))))) begin
y_next = y_reg + (1 << (WIDTH-11));
end
else if (x_reg >= ((y_reg - (1023 << (WIDTH-11))) * (y_reg - (1023 << (WIDTH-11))))) begin
y_next = y_reg + (1 << (WIDTH-12));
end
else if (x_reg >= ((y_reg - (2047 << (WIDTH-12))) * (y_reg - (2047 << (WIDTH-12))))) begin
y_next = y_reg + (1 << (WIDTH-13));
end
else if (x_reg >= ((y_reg - (4095 << (WIDTH-13))) * (y_reg - (4095 << (WIDTH-13))))) begin
y_next = y_reg + (1 << (WIDTH-14));
end
else if (x_reg >= ((y_reg - (8191 << (WIDTH-14))) * (y_reg - (8191 << (WIDTH-14))))) begin
y_next = y_reg + (1 << (WIDTH-15));
end
else if (x_reg >= ((y_reg - (16383 << (WIDTH-15))) * (y_reg - (16383 << (WIDTH-15))))) begin
y_next = y_reg + (1 << (WIDTH-16));
end
else if (x_reg >= ((y_reg - (32767 << (WIDTH-16))) * (y_reg - (32767 << (WIDTH-16))))) begin
y_next = y_reg + (1 << (WIDTH-17));
end
else if (x_reg >= ((y_reg - (65535 << (WIDTH-17))) * (y_reg - (65535 << (WIDTH-17))))) begin
y_next = y_reg + (1 << (WIDTH-18));
end
else if (x_reg >= ((y_reg - (131071 << (WIDTH-18))) * (y_reg - (131071 << (WIDTH-18))))) begin
y_next = y_reg + (1 << (WIDTH-19));
end
else if (x_reg >= ((y_reg - (262143 << (WIDTH-19))) * (y_reg - (262143 << (WIDTH-19))))) begin
y_next = y_reg + (1 << (WIDTH-20));
end
else if (x_reg >= ((y_reg - (524287 << (WIDTH-20))) * (y_reg - (524287 << (WIDTH-20))))) begin
y_next = y_reg + (1 << (WIDTH-21));
end
else if (x_reg >= ((y_reg - (1048575 << (WIDTH-21))) * (y_reg - (1048575 << (WIDTH-21))))) begin
y_next = y_reg + (1 << (WIDTH-22));
end
else if (x_reg >= ((y_reg - (2097151 << (WIDTH-22))) * (y_reg - (2097151 << (WIDTH-22))))) begin
y_next = y_reg + (1 << (WIDTH-23));
end
else if (x_reg >= ((y_reg - (4194303 << (WIDTH-23))) * (y_reg - (4194303 << (WIDTH-23))))) begin
y_next = y_reg + (1 << (WIDTH-24));
end
else if (x_reg >= ((y_reg - (8388607 << (WIDTH-24))) * (y_reg - (8388607 << (WIDTH-24))))) begin
y_next = y_reg + (1 << (WIDTH-25));
end
else if (x_reg >= ((y_reg - (16777215 << (WIDTH-25))) * (y_reg - (16777215 << (WIDTH-25))))) begin
y_next = y_reg + (1 << (WIDTH-26));
end
else if (x_reg >= ((y_reg - (33554431 << (WIDTH-26))) * (y_reg - (33554431 << (WIDTH-26))))) begin
y_next = y_reg + (1 << (WIDTH-27));
end
else if (x_reg >= ((y_reg - (67108863 << (WIDTH-27))) * (y_reg - (67108863 << (WIDTH-27))))) begin
y_next = y_reg + (1 << (WIDTH-28));
end
else if (x_reg >= ((y_reg - (134217727 << (WIDTH-28))) * (y_reg - (134217727 << (WIDTH-28))))) begin
y_next = y_reg + (1 << (WIDTH-29));
end
else if (x_reg >= ((y_reg - (268435455 << (WIDTH-29))) * (y_reg - (268435455 << (WIDTH-29))))) begin
y_next = y_reg + (1 << (WIDTH-30));
end
else if (x_reg >= ((y_reg - (536870911 << (WIDTH-30))) * (y_reg - (536870911 << (WIDTH-30))))) begin
y_next = y_reg + (1 << (WIDTH-31));
end
else if (x_reg >= ((y_reg - (1073741823 << (WIDTH-31))) * (y_reg - (1073741823 << (WIDTH-31))))) begin
y_next = y_reg + (1 << (WIDTH-32));
end
else begin
y_next = y_reg;
end
end
always @(posedge clk) begin
y_reg <= y_next;
end
endmodule
这个模块有一个32位输入和一个32位输出。它使用一个迭代的方法来计算输入的平方根。该算法基于二进制搜索,通过比较每个迭代值的平方与输入值来确定下一个迭代值。每个迭代值都从上一个迭代值计算得出,因此需要一个寄存器来存储上一个迭代值。
阅读全文