`begin_keywords "1364-2005" /* * Copyright (c) 2002 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU * General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * * $Id: sqrt32synth.v,v 1.2 2007/08/30 01:25:29 stevewilliams Exp $" */ /* * This module approximates the square root of an unsigned 32bit * number. The algorithm works by doing a bit-wise binary search. * Starting from the most significant bit, the accumulated value * tries to put a 1 in the bit position. If that makes the square * too big for the input, the bit is left zero, otherwise it is set * in the result. This continues for each bit, decreasing in * significance, until all the bits are calculated or all the * remaining bits are zero. * * Since the result is an integer, this function really calculates * value of the expression: * * x = floor(sqrt(y)) * * where sqrt(y) is the exact square root of y and floor(N) is the * largest integer <= N. * * For 32bit numbers, this will never run more then 16 iterations, * which amounts to 16 clocks. */ module sqrt (input clk, output wire rdy, input reset, input [31:0] x, output reg [15:0] acc); //32 parameter ss=5; localparam w=1<= ((z + 1)*(z + 1))) begin $display("test=%d x=%d, y=%d ERROR: y is too small", idx, A, Z); $display("FAILED"); $finish; end end else begin $display ("Could not verify above number"); end end $display ("Running tests Amax=%d random input numbers", Amax); for (idx = 0 ; idx < Amax; idx = 1+ idx) begin A = $random; // A = A - ((A / Amax) * Amax); //this is needed only if <32 bit //A = idx; //sequential -- comment out to get random tests if (A < 1<<(w-1)) begin reset_dut; run_dut; //$display("%d: x=%d, y=%d", idx, A, Z); a = A; z = Z; if (a < (z *z)) begin $display("test=%d x=%d, y=%d ERROR:y is too big", idx, A, Z); $display("FAILED"); $finish; end if (z<65535) // at this number y*y overflows, so cannot test this way begin if (a >= ((z + 1)*(z + 1))) begin $display("test=%d x=%d, y=%d ERROR: y is too small", idx, A, Z); $display("FAILED"); $finish; end end else begin $display ("Could not verify above number"); end //$display("%d: x=%d, y=%d", idx, A, Z); if (idx%1000 == 0) $display("Finished %d tests", idx); end // if (A < Amax) begin end $display ("PASSED"); $finish; end endmodule `end_keywords