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

Subversion Repositories theia_gpu

[/] [theia_gpu/] [branches/] [beta_1.1/] [rtl/] [Collaterals/] [Module_FixedPointSquareRoot.v] - Blame information for rev 170

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

Line No. Rev Author Line
1 44 diegovalve
`timescale 1ns / 1ps
2
`include "aDefinitions.v"
3
 
4
 
5
`define SR_AFTER_RESET_STATE 0
6
//-----------------------------------------------------------------
7
/*
8
 
9
        Calcualtes the SquareRoot of a Fixed Point Number
10
        Input:  Q32.32
11
        Output: Q16.16
12
        Notice that the result has half the precicion as the operands!!
13
*/
14
module FixedPointSquareRoot
15
(
16
        input wire                                                      Clock,
17
        input wire                                                      Reset,
18
        input wire[`LONG_WIDTH-1:0]      Operand,
19
        input wire                                                      iInputReady,
20
        output  reg                                             OutputReady,
21
        output  reg [`WIDTH-1:0]         Result
22
);
23
 
24
reg[63:0]                        x;
25
reg[0:`WIDTH-1]  group,sum,diff;
26
reg[0:`WIDTH-1]          temp1,temp2;
27
reg     [5:0]                    CurrentState, NextState;
28
 
29
reg myInputReady;
30
 
31
//----------------------------------------
32
always @(posedge Clock)
33
begin
34
        myInputReady = iInputReady;
35
end
36
//----------------------------------------
37
//Next states logic
38
always @(negedge Clock)
39
begin
40
        if( Reset!=1 )
41
           CurrentState = NextState;
42
                         else
43
                                CurrentState = `SR_AFTER_RESET_STATE;
44
end
45
//----------------------------------------
46
 
47
always @ (posedge Clock)
48
begin
49
        case (CurrentState)
50
        //----------------------------------------
51
        `SR_AFTER_RESET_STATE:
52
        begin
53
                OutputReady = 0;
54
                Result          = 0;
55
                sum                     = 0;
56
                diff                    = 0;
57
                group=32;                               //WAS 16
58
                x = 0;
59
                if ( myInputReady == 1  )
60
                begin
61
                  // x[31:0] = Operand;
62
                  x = Operand;
63
                        x = x << `SCALE;
64
                        NextState = `SQUARE_ROOT_LOOP;
65
                end else
66
                        NextState = `SR_AFTER_RESET_STATE;
67
 
68
        end
69
        //----------------------------------------
70
        `SQUARE_ROOT_LOOP:
71
        begin
72
 
73
 
74
 
75
                sum = sum << 1;
76
                sum = sum +  1;
77
                temp1 = diff << 2;
78
                //diff = diff + (x>>(group*2)) &3;
79
                temp2 = group << 1;                             //group * 2 ??
80
                diff = temp1 + ((x >> temp2) &3);
81
 
82
                if (sum > diff)
83
                begin
84
                        sum = sum -1;
85
                end
86
                else
87
                begin
88
                        Result = Result + (1<<group);
89
                        diff = diff - sum;
90
                        sum = sum + 1;
91
                end//if
92
 
93
 
94
                if ( group != 0 )
95
                begin
96
                        group = group - 1;
97
                        NextState = `SQUARE_ROOT_LOOP;
98
                end
99
                else
100
                begin
101
                        NextState       = `WRITE_SQUARE_ROOT_RESULT;
102
 
103
                end
104
        end
105
        //----------------------------------------
106
        `WRITE_SQUARE_ROOT_RESULT:
107
        begin
108
                OutputReady = 1;
109
                NextState = (iInputReady == 0) ?
110
                 `SR_AFTER_RESET_STATE : `WRITE_SQUARE_ROOT_RESULT;
111
        end
112
        //----------------------------------------
113
endcase
114
end //always
115
endmodule
116
//-----------------------------------------------------------------

powered by: WebSVN 2.1.0

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