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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [core/] [div.v] - Blame information for rev 82

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

Line No. Rev Author Line
1 69 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
                        o_valid <= 1'b0;
61
                end else if (i_wr)
62
                begin
63
                        o_busy <= 1'b1;
64
                        o_valid <= 1'b0;
65
                end else if (o_busy)
66
                begin
67
                        if ((r_bit == 6'h0)||(o_err))
68
                        begin
69
                                o_busy <= 1'b0;
70
                                o_valid <= (o_err)||(~r_sign);
71
                        end
72
                end else if (r_sign)
73
                begin
74
                        // if (o_err), o_valid is already one.
75
                        //      if not, o_valid has not yet become one.
76
                        o_valid <= (~o_err); // 1'b1;
77
                        // r_sign <= 1'b0;
78
                end else begin
79
                        o_busy <= 1'b0;
80
                        o_valid <= 1'b0;
81
                end
82
 
83
        always @(posedge i_clk)
84
                if((i_rst)||(o_valid))
85
                        o_err <= 1'b0;
86
                else if (o_busy)
87
                        o_err <= (r_divisor == 0);
88
 
89
        always @(posedge i_clk)
90
                if (i_wr)
91
                begin
92
                        o_quotient <= 0;
93
                        // r_bit <= { 1'b1, {(LGBW){1'b0}} };
94
                        r_bit <= { 1'b0, {(LGBW){1'b1}} };
95
                        r_divisor <= {  i_denominator, {(BW-1){1'b0}} };
96
                        r_dividend <=  i_numerator;
97
                        r_sign <= 1'b0;
98
                        pre_sign <= i_signed;
99
                        r_z <= 1'b1;
100
                end else if (pre_sign)
101
                begin
102
                        // r_bit <= r_bit - 1;
103
                        r_sign <= ((r_divisor[(2*BW-2)])^(r_dividend[(BW-1)]));;
104
                        if (r_dividend[BW-1])
105
                                r_dividend <= -r_dividend;
106
                        if (r_divisor[(2*BW-2)])
107
                                r_divisor[(2*BW-2):(BW-1)] <= -r_divisor[(2*BW-2):(BW-1)];
108
                        pre_sign <= 1'b0;
109
                end else if (o_busy)
110
                begin
111 81 dgisselq
                        r_bit <= r_bit + {(LGBW+1){1'b1}}; // r_bit = r_bit - 1;
112 69 dgisselq
                        r_divisor <= { 1'b0, r_divisor[(2*BW-2):1] };
113
                        if (|r_divisor[(2*BW-2):(BW)])
114
                        begin
115
                        end else if (diff[BW])
116
                        begin
117
                        end else begin
118
                                r_dividend <= diff[(BW-1):0];
119
                                o_quotient[r_bit[(LGBW-1):0]] <= 1'b1;
120
                                r_z <= 1'b0;
121
                        end
122
                end else if (r_sign)
123
                begin
124
                        r_sign <= 1'b0;
125
                        o_quotient <= -o_quotient;
126
                end
127
 
128
        // Set Carry on an exact divide
129
        wire    w_n;
130
        always @(posedge i_clk)
131
                r_c <= (o_busy)&&((diff == 0)||(r_dividend == 0));
132
        assign w_n = o_quotient[(BW-1)];
133
 
134
        assign o_flags = { 1'b0, w_n, r_c, r_z };
135
endmodule

powered by: WebSVN 2.1.0

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