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

Subversion Repositories fixed_point_arithmetic_parameterized

[/] [fixed_point_arithmetic_parameterized/] [trunk/] [testfixtures/] [qdiv_tf.v] - Diff between revs 2 and 3

Show entire file | Details | Blame | View Log

Rev 2 Rev 3
Line 21... Line 21...
// Additional Comments:
// Additional Comments:
// 
// 
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
 
 
module qdiv_tf;
module qdiv_tf;
 
//Parameterized values
 
        parameter Q = 15;
 
        parameter N = 32;
 
 
        // Inputs
        // Inputs
        reg [31:0] dividend;
        reg [N-1:0] dividend;
        reg [31:0] divisor;
        reg [N-1:0] divisor;
        reg start;
        reg start;
        reg clk;
        reg clk;
 
        real top;
 
        real bottom;
 
 
        // Outputs
        // Outputs
        wire [31:0] quotient;
        wire [N-1:0] quotient;
 
        wire done;
 
        real result;
 
 
 
 
        // Instantiate the Unit Under Test (UUT)
        // Instantiate the Unit Under Test (UUT)
        //module  Params  Name  Signals
        //module  Params  Name  Signals
        qdiv #(15,32)   uut (dividend, divisor, start, clk, quotient);
        qdiv #(Q,N)     uut (dividend, divisor, start, clk, quotient,done);
 
 
        initial begin
        initial begin
                // Initialize Inputs
                // Initialize Inputs
                dividend[31] = 0;
                top = -32.5;
                dividend[30:15] = 64;
                bottom = 2.25;
                dividend[14:0] = 4096;//1048576;//4096;
 
 
                conv_rational(top,dividend);
                divisor[31] = 0;
                conv_rational(bottom,divisor);
                divisor[30:15] = 2;
 
                divisor[14:0] = 0;
 
                start = 1;
                start = 1;
                clk = 0;
                clk = 0;
 
 
                // Wait 100 ns for global reset to finish
                // Wait 100 ns for global reset to finish
                #100;
                #100;
 
 
                // Add stimulus here
                // Add stimulus here
                start = 0;
                start = 0;
 
 
 
                #366;
 
                conv_fixed(quotient,result);
 
                $display("%f / %f = %f",top,bottom,result);
        end
        end
 
 
        always
        always
        begin
        begin
                #5 clk = ~clk;
                #5 clk = ~clk;
        end
        end
 
 
endmodule
        /*
 
         *      Task to convert from rational to fixed point
 
         */
 
         task conv_rational;
 
         input real num;
 
         output [N-1:0] fixed;
 
 
 
         integer i;
 
         real tmp;
 
 
 
         begin
 
                 tmp = num;
 
 
 
                 //set sign
 
                 if(tmp < 0) begin
 
                 //if its negative, set the sign bit and make the temporary number position by multiplying by -1
 
                        fixed[N-1] = 1;
 
                        tmp = tmp * -1;
 
                 end
 
                 else begin
 
                 //if its positive, the sign bit is zero
 
                        fixed[N-1] = 0;
 
                 end
 
 
 No newline at end of file
 No newline at end of file
 
                 //check that the number isnt too large
 
                 if(tmp > (1 << N-Q-1)) begin
 
                        $display("Error!!! rational number %f is larger than %d whole bits can represent!",num,N-Q-1);
 
                 end
 
 
 
                 //set whole part
 
                 for(i=0; i<N-Q-1; i=i+1) begin
 
                        if(tmp >= (1 << N-Q-2-i)) begin
 
                        //if its greater or equal, subtract out this power of 2 and put a one at this position
 
                                fixed[N-2-i] = 1;
 
                                tmp = tmp - (1 << N-Q-2-i);
 
                        end
 
                        else begin
 
                        //if its less, put a zero at this position
 
                                fixed[N-2-i] = 0;
 
                        end
 
                 end
 
 
 
                 //set fractional part
 
                 for(i=1; i<=Q; i=i+1) begin
 
                        if(tmp >= 1.0/(1 << i)) begin
 
                        //if its greater or equal, subtract out this power of 2 and put a one at this position
 
                                fixed[Q-i] = 1;
 
                                tmp = tmp - 1.0/(1 << i);
 
                        end
 
                        else begin
 
                        //if its less, put a zero at this position
 
                                fixed[Q-i] = 0;
 
                        end
 
                 end
 
 
 
                 //check that the number isnt too small (loss of precision)
 
                 if(tmp > 0) begin
 
                        $display("Error!!! LOSS OF PRECISION converting rational number %f's fractional part using %d bits!",num,Q);
 
                 end
 
         end
 
         endtask;
 
 
 
         /*
 
         *      Task to convert from fixed point to rational
 
         */
 
         task conv_fixed;
 
         input [N-1:0] fixed;
 
         output real num;
 
 
 
         integer i;
 
 
 
         begin
 
                 num = 0;
 
 
 
                 //set whole part
 
                 for(i=0; i<N-Q-1; i=i+1) begin
 
                        if(fixed[N-2-i] == 1) begin
 
                        //if theres a one at this position, add the power of 2 to the rational number
 
                                num = num + (1 << N-Q-2-i);
 
                        end
 
                 end
 
 
 
                 //set fractional part
 
                 for(i=1; i<=Q; i=i+1) begin
 
                        if(fixed[Q-i] == 1) begin
 
                        //if theres a one at this position, add the power of 2 to the rational number
 
                                num = num + 1.0/(1 << i);
 
                        end
 
                 end
 
 
 
                 //set sign
 
                 if(fixed[N-1] == 1) begin
 
                 //if the sign bit is set, negate the rational number
 
                        num = num * -1;
 
                 end
 
         end
 
         endtask;
 
 
 
endmodule
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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