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

Subversion Repositories ft816float

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

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

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

powered by: WebSVN 2.1.0

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