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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [cpu/] [div.v] - Blame information for rev 45

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

Line No. Rev Author Line
1 21 dgisselq
///////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    div.v
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     Provide an Integer divide capability to the Zip CPU.
8
//
9
//
10
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12
//
13
///////////////////////////////////////////////////////////////////////////////
14
//
15
// Copyright (C) 2015, Gisselquist Technology, LLC
16
//
17
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// License:     GPL, v3, as defined and found on www.gnu.org,
28
//              http://www.gnu.org/licenses/gpl.html
29
//
30
//
31
///////////////////////////////////////////////////////////////////////////////
32
//
33
// `include "cpudefs.v"
34
//
35
module  div(i_clk, i_rst, i_wr, i_signed, i_numerator, i_denominator,
36
                o_busy, o_valid, o_err, o_quotient, o_flags);
37
        parameter       BW=32, LGBW = 5;
38
        input           i_clk, i_rst;
39
        // Input parameters
40
        input                   i_wr, i_signed;
41
        input   [(BW-1):0]       i_numerator, i_denominator;
42
        // Output parameters
43
        output  reg             o_busy, o_valid, o_err;
44
        output  reg [(BW-1):0]   o_quotient;
45
        output  wire    [3:0]    o_flags;
46
 
47
        reg     [(2*BW-2):0]     r_divisor;
48
        reg     [(BW-1):0]       r_dividend;
49
        wire    [(BW):0] diff; // , xdiff[(BW-1):0];
50
        assign  diff = r_dividend - r_divisor[(BW-1):0];
51
        // assign       xdiff= r_dividend - { 1'b0, r_divisor[(BW-1):1] };
52
 
53
        reg             r_sign, pre_sign, r_z, r_c;
54
        reg     [(LGBW):0]       r_bit;
55
 
56
        always @(posedge i_clk)
57
                if (i_rst)
58
                begin
59
                        o_busy <= 1'b0;
60
                end else if (i_wr)
61
                begin
62
                        o_busy <= 1'b1;
63
                end else if ((o_busy)&&((r_bit == 6'h0)||(o_err)))
64
                        o_busy <= 1'b0;
65
                // else busy is zero and stays at zero
66
 
67
        always @(posedge i_clk)
68
                if ((i_rst)||(i_wr))
69
                        o_valid <= 1'b0;
70
                else if (o_busy)
71
                begin
72
                        if ((r_bit == 6'h0)||(o_err))
73
                                o_valid <= (o_err)||(~r_sign);
74
                end else if (r_sign)
75
                begin
76
                        // if (o_err), o_valid is already one.
77
                        //      if not, o_valid has not yet become one.
78
                        o_valid <= (~o_err); // 1'b1;
79
                end else
80
                        o_valid <= 1'b0;
81
 
82
        always @(posedge i_clk)
83
                if((i_rst)||(o_valid))
84
                        o_err <= 1'b0;
85
                else if (o_busy)
86
                        o_err <= (r_divisor == 0);
87
 
88
        always @(posedge i_clk)
89
                if (i_wr)
90
                begin
91
                        o_quotient <= 0;
92
                        // r_bit <= { 1'b1, {(LGBW){1'b0}} };
93
                        r_bit <= { 1'b0, {(LGBW){1'b1}} };
94
                        r_divisor <= {  i_denominator, {(BW-1){1'b0}} };
95
                        r_dividend <=  i_numerator;
96
                        r_sign <= 1'b0;
97
                        pre_sign <= i_signed;
98
                        r_z <= 1'b1;
99
                end else if (pre_sign)
100
                begin
101
                        // r_bit <= r_bit - 1;
102
                        r_sign <= ((r_divisor[(2*BW-2)])^(r_dividend[(BW-1)]));;
103
                        if (r_dividend[BW-1])
104
                                r_dividend <= -r_dividend;
105
                        if (r_divisor[(2*BW-2)])
106
                                r_divisor[(2*BW-2):(BW-1)] <= -r_divisor[(2*BW-2):(BW-1)];
107
                        pre_sign <= 1'b0;
108
                end else if (o_busy)
109
                begin
110
                        r_bit <= r_bit + {(LGBW+1){1'b1}}; // r_bit = r_bit - 1;
111
                        r_divisor <= { 1'b0, r_divisor[(2*BW-2):1] };
112
                        if (|r_divisor[(2*BW-2):(BW)])
113
                        begin
114
                        end else if (diff[BW])
115
                        begin
116
                        end else begin
117
                                r_dividend <= diff[(BW-1):0];
118
                                o_quotient[r_bit[(LGBW-1):0]] <= 1'b1;
119
                                r_z <= 1'b0;
120
                        end
121
                end else if (r_sign)
122
                begin
123
                        r_sign <= 1'b0;
124
                        o_quotient <= -o_quotient;
125
                end
126
 
127
        // Set Carry on an exact divide
128
        wire    w_n;
129
        always @(posedge i_clk)
130
                r_c <= (o_busy)&&((diff == 0)||(r_dividend == 0));
131
        assign w_n = o_quotient[(BW-1)];
132
 
133
        assign o_flags = { 1'b0, w_n, r_c, r_z };
134
endmodule

powered by: WebSVN 2.1.0

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