OpenCores
URL https://opencores.org/ocsvn/ft816float/ft816float/trunk

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [isqrt.v] - Blame information for rev 29

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2010-2019  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      isqrt.v
9
//      - integer square root
10
//  - uses the standard long form calc.
11
//      - geared towards use in an floating point unit
12
//      - calculates to FPWID fractional precision (double FPWIDth output)
13
//
14
//
15
// This source file is free software: you can redistribute it and/or modify 
16
// it under the terms of the GNU Lesser General Public License as published 
17
// by the Free Software Foundation, either version 3 of the License, or     
18
// (at your option) any later version.                                      
19
//                                                                          
20
// This source file is distributed in the hope that it will be useful,      
21
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
23
// GNU General Public License for more details.                             
24
//                                                                          
25
// You should have received a copy of the GNU General Public License        
26
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
27
//                                                                          
28
// ============================================================================
29
 
30
module isqrt(rst, clk, ce, ld, a, o, done);
31
parameter FPWID = 32;
32
localparam MSB = FPWID-1;
33
parameter IDLE=3'd0;
34
parameter CALC=3'd1;
35
parameter DONE=3'd2;
36
input rst;
37
input clk;
38
input ce;
39
input ld;
40
input [MSB:0] a;
41
output [FPWID*2-1:0] o;
42
output done;
43
 
44
reg [2:0] state;
45
reg [FPWID*2:0] root;
46
wire [FPWID*2-1:0] testDiv;
47
reg [FPWID*2-1:0] remLo;
48
reg [FPWID*2-1:0] remHi;
49
 
50
wire cnt_done;
51
assign testDiv = {root[FPWID*2-2:0],1'b1};
52
wire [FPWID*2-1:0] remHiShift = {remHi[FPWID*2-3:0],remLo[FPWID*2-1:FPWID*2-2]};
53
wire doesGoInto = remHiShift >= testDiv;
54
assign o = root[FPWID*2:1];
55
 
56
// Iteration counter
57
reg [7:0] cnt;
58
 
59
always @(posedge clk)
60
if (rst) begin
61
        cnt <= FPWID*2;
62
        remLo <= {FPWID*2{1'b0}};
63
        remHi <= {FPWID*2{1'b0}};
64
        root <= {FPWID*2+1{1'b0}};
65
        state <= IDLE;
66
end
67
else if (ce) begin
68
        if (!cnt_done)
69
                cnt <= cnt + 8'd1;
70
case(state)
71
IDLE:
72
        if (ld) begin
73
                cnt <= 8'd0;
74
                state <= CALC;
75
                remLo <= {a,32'h0};
76
                remHi <= {FPWID*2{1'b0}};
77
                root <= {FPWID*2+1{1'b0}};
78
        end
79
CALC:
80
        if (!cnt_done) begin
81
                // Shift the remainder low
82
                remLo <= {remLo[FPWID*2-3:0],2'd0};
83
                // Shift the remainder high
84
                remHi <= doesGoInto ? remHiShift - testDiv: remHiShift;
85
                // Shift the root
86
                root <= {root+doesGoInto,1'b0}; // root * 2 + 1/0
87
        end
88
        else begin
89
                cnt <= 8'h00;
90
                state <= DONE;
91
        end
92
DONE:
93
        begin
94
                cnt <= cnt + 8'd1;
95
                if (cnt == 8'd6)
96
                        state <= IDLE;
97
        end
98
endcase
99
end
100
assign cnt_done = (cnt==FPWID);
101
assign done = state==DONE;
102
 
103
endmodule
104
 
105
 
106
module isqrt_tb();
107
 
108
reg clk;
109
reg rst;
110
reg [31:0] a;
111
wire [63:0] o;
112
reg ld;
113
wire done;
114
reg [7:0] state;
115
 
116
initial begin
117
        clk = 1;
118
        rst = 0;
119
        #100 rst = 1;
120
        #100 rst = 0;
121
end
122
 
123
always #10 clk = ~clk;  //  50 MHz
124
 
125
always @(posedge clk)
126
if (rst) begin
127
        state <= 8'd0;
128
        a <= 32'h912345;
129
end
130
else
131
begin
132
ld <= 1'b0;
133
case(state)
134
8'd0:
135
        begin
136
                a <= 32'h9123456;
137
                ld <= 1'b1;
138
                state <= 8'd1;
139
        end
140
8'd1:
141
        if (done) begin
142
                $display("i=%h o=%h", a, o);
143
        end
144
endcase
145
end
146
 
147
isqrt #(32) u1 (.rst(rst), .clk(clk), .ce(1'b1), .ld(ld), .a(a), .o(o), .done(done));
148
 
149
endmodule
150
 
151
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.