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

Subversion Repositories theia_gpu

[/] [theia_gpu/] [trunk/] [rtl/] [Module_FixedPointSquareRoot.v] - Blame information for rev 213

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

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

powered by: WebSVN 2.1.0

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