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

Subversion Repositories s80186

[/] [s80186/] [trunk/] [rtl/] [Divider.sv] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jamieiles
// Copyright Jamie Iles, 2017
2
//
3
// This file is part of s80x86.
4
//
5
// s80x86 is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// s80x86 is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with s80x86.  If not, see .
17
 
18
// Non-restoring division
19
module Divider(input logic clk,
20
               input logic reset,
21
               input logic start,
22
               input logic is_8_bit,
23
               input logic is_signed,
24
               output logic busy,
25
               output logic complete,
26
               output logic error,
27
               input logic [31:0] dividend,
28
               input logic [15:0] divisor,
29
               output logic [15:0] quotient,
30
               output logic [15:0] remainder);
31
 
32
typedef enum bit[1:0] {
33
    INIT,
34
    WORKING,
35
    RESTORE,
36
    FIX_SIGN
37
} DivState_t;
38
 
39
DivState_t state, next_state;
40
 
41
// Remainder
42
wire [15:0] rem8 = {{8{P[15]}}, P[15:8]};
43
wire [15:0] rem16 = P[31:16];
44
assign remainder = is_8_bit ? rem8 : rem16;
45
 
46
// Shifted divisor
47
wire [31:0] udivshift8 = {16'b0, divisor[7:0], 8'b0};
48
wire [31:0] udivshift16 = {divisor, 16'b0};
49
wire [31:0] udivshift = is_8_bit ? udivshift8 : udivshift16;
50
 
51
wire [31:0] sdivshift8 = {16'b0, divisor_mag[7:0], 8'b0};
52
wire [31:0] sdivshift16 = {divisor_mag, 16'b0};
53
wire [31:0] sdivshift = is_8_bit ? sdivshift8 : sdivshift16;
54
 
55
wire [31:0] D = is_signed ? sdivshift : udivshift;
56
 
57
// Overflow
58
wire unsigned_overflow8 = dividend[15:0] >= {divisor[7:0], 8'b0};
59
wire unsigned_overflow16 = dividend >= {divisor, 16'b0};
60
wire unsigned_overflow = is_8_bit ? unsigned_overflow8 : unsigned_overflow16;
61
 
62
wire signed_overflow8 = in_signs_equal && dividend_mag[14:7] >= divisor_mag[7:0];
63
wire signed_overflow16 = in_signs_equal && dividend_mag[30:15] >= divisor_mag;
64
wire signed_overflow = is_8_bit ? signed_overflow8 : signed_overflow16;
65
 
66
wire overflow = is_signed ? signed_overflow : unsigned_overflow;
67
wire div_by_zero = divisor == 16'b0;
68
 
69
// Magnitudes
70
wire [31:0] dividend_mag = (dividend + {32{dividend[31]}}) ^ {32{dividend[31]}};
71
wire [15:0] divisor_mag = (divisor + {16{divisor[15]}}) ^ {16{divisor[15]}};
72
 
73
// Dividend
74
wire [63:0] P16 = is_signed ?
75
    {32'b0, dividend_mag} : {32'b0, dividend};
76
wire [63:0] P8 = is_signed ?
77
    {48'b0, dividend_mag[15:0]} : {48'b0, dividend[15:0]};
78
wire [63:0] P_init = is_8_bit ? P8 : P16;
79
 
80
// Sign bits
81
wire in_signs_equal8 = ~(dividend[15] ^ divisor[7]);
82
wire in_signs_equal16 = ~(dividend[31] ^ divisor[15]);
83
wire in_signs_equal = is_8_bit ? in_signs_equal8 : in_signs_equal16;
84
wire dividend_negative = is_8_bit ? dividend[15] : dividend[31];
85
 
86
reg [63:0] P;
87
reg [3:0] idx;
88
wire [15:0] restored_quotient;
89
wire [15:0] negative_quotient = ~quotient + 1'b1;
90
 
91
// Error condition
92
wire raise_error = div_by_zero | overflow;
93
 
94
assign busy = (start || (state != INIT) && !complete);
95
 
96
always_comb begin
97
    if (is_8_bit) begin
98
        restored_quotient = {8'b0, P[63] ?
99
            quotient[7:0] - ~quotient[7:0] - 8'b1 : quotient[7:0] - ~quotient[7:0]};
100
    end else begin
101
        restored_quotient = P[63] ?
102
            quotient - ~quotient - 16'b1 : quotient - ~quotient;
103
    end
104
end
105
 
106
always_comb begin
107
    case (state)
108
    INIT: next_state = start && !error && !raise_error ? WORKING : INIT;
109
    WORKING: next_state = idx == 4'b0 ? RESTORE : WORKING;
110
    RESTORE: next_state = is_signed ? FIX_SIGN : INIT;
111
    FIX_SIGN: next_state = INIT;
112
    endcase
113
 
114
    if (reset)
115
        next_state = INIT;
116
end
117
 
118
always_ff @(posedge clk or posedge reset) begin
119
    if (reset) begin
120
        error <= 1'b0;
121
        complete <= 1'b0;
122
    end else begin
123
        case (state)
124
        INIT: begin
125
            error <= 1'b0;
126
 
127
            if (start) begin
128
                quotient <= 16'b0;
129
                P <= P_init;
130
                idx <= !is_8_bit ? 4'hf : 4'h7;
131
                error <= raise_error;
132
            end
133
 
134
            complete <= start && raise_error;
135
        end
136
        WORKING: begin
137
            if (!P[63]) begin
138
                quotient[idx] <= 1'b1;
139
                P <= (P * 2) - {32'b0, D};
140
            end else begin
141
                P <= (P * 2) + {32'b0, D};
142
            end
143
            idx <= idx - 1'b1;
144
        end
145
        RESTORE: begin
146
            quotient <= restored_quotient;
147
            if (P[63])
148
                P <= P + {{32{D[31]}}, D};
149
            complete <= ~is_signed;
150
        end
151
        FIX_SIGN: begin
152
            if (~in_signs_equal) begin
153
                quotient <= negative_quotient;
154
                error <= is_signed & ~negative_quotient[is_8_bit ? 7 : 15];
155
            end else begin
156
                error <= is_signed & quotient[is_8_bit ? 7 : 15];
157
            end
158
            if (dividend_negative && is_8_bit)
159
                P[15:8] <= ~P[15:8] + 1'b1;
160
            else if (dividend_negative)
161
                P[31:16] <= ~P[31:16] + 1'b1;
162
            complete <= 1'b1;
163
        end
164
        endcase
165
    end
166
end
167
 
168
always_ff @(posedge clk)
169
    state <= next_state;
170
 
171
endmodule

powered by: WebSVN 2.1.0

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