-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign.sv
261 lines (238 loc) · 8.29 KB
/
design.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 10xEngineers
// Engineer: Umer Shahid
//
// Create Date: 04/12/2022 09:39:13 PM
// Design Name: Floating Point Division
// Module Name: FP_div
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module fpdiv(
output logic [31:0] z_value_o,
input wire logic [31:0] a_value_i,
input wire logic [31:0] b_value_i
);
enum {IDLE, UNPACK, SPECIAL_CASES, NORMALIZE_A, NORMALIZE_B, DIVIDE_0, DIVIDE_1, DIVIDE_2, DIVIDE_3, NORMALIZE_0,
NORMALIZE_1, ROUND, PACK, DONE} state;
integer i=0;
logic [23:0] a_m, b_m, z_m;
logic signed [9:0] a_e, b_e, z_e;
logic a_s, b_s, z_s;
logic [50:0] quotient=0, divisor=0, dividend=0, remainder=0;
logic [5:0] count;
logic guard=0, round_bit=0, sticky=0;
always@(a_value_i,b_value_i) begin
state = IDLE;
guard=0;
round_bit=0;
sticky=0;
quotient=0;
divisor=0;
dividend=0;
remainder=0;
count=0;
for(i=0;i<500;i=i+1) begin
case (state)
IDLE: begin
state = UNPACK;
end
UNPACK: begin
a_m = {1'd0, a_value_i[22:0]};
b_m = {1'd0, b_value_i[22:0]};
a_e = {2'd0, a_value_i[30:23]} - 10'd127;
b_e = {2'd0, b_value_i[30:23]} - 10'd127;
a_s = a_value_i[31];
b_s = b_value_i[31];
state = SPECIAL_CASES;
end
SPECIAL_CASES: begin
// if a is NaN or b is NaN return NaN
if ((a_e == 128 && a_m != 0) || (b_e == 128 && b_m != 0)) begin
z_value_o[31] = 1;
z_value_o[30:23] = 255;
z_value_o[22] = 1;
z_value_o[21:0] = 0;
state = DONE;
end
// if a is inf and b is inf return NaN
else if (a_e == 128 && b_e == 128) begin
z_value_o[31] = 1;
z_value_o[30:23] = 255;
z_value_o[22] = 1;
z_value_o[21:0] = 0;
state = DONE;
end
// if a is inf return inf
else if (a_e == 128) begin
z_value_o[31] = a_s ^ b_s;
z_value_o[30:23] = 255;
z_value_o[22:0] = 0;
// if b is zero return NaN
if ((b_e == -127) && (b_m == 0)) begin
z_value_o[31] = 1;
z_value_o[30:23] = 255;
z_value_o[22] = 1;
z_value_o[21:0] = 0;
end
state = DONE;
end
// if b is inf return zero
else if (b_e == 128) begin
z_value_o[31] = a_s ^ b_s;
z_value_o[30:23] = 0;
z_value_o[22:0] = 0;
state = DONE;
end
// if a is zero return zero
else if (a_e == -127 && a_m == 0) begin
z_value_o[31] = a_s ^ b_s;
z_value_o[30:23] = 0;
z_value_o[22:0] = 0;
// if b is zero return NaN
if (b_e == -127 && b_m == 0) begin
z_value_o[31] = 1;
z_value_o[30:23] = 255;
z_value_o[22] = 1;
z_value_o[21:0] = 0;
end
state = DONE;
end
// if b is zero return inf
else if (b_e == -127 && b_m == 0) begin
z_value_o[31] = a_s ^ b_s;
z_value_o[30:23] = 255;
z_value_o[22:0] = 0;
state = DONE;
end else begin
// denormalized number
if (a_e == -127) begin
a_e = -126;
end else begin
a_m[23] = 1;
end
// denormalized number
if (b_e == -127) begin
b_e = -126;
end else begin
b_m[23] = 1;
end
state = NORMALIZE_A;
end
end
NORMALIZE_A: begin
if (a_m[23]) begin
state = NORMALIZE_B;
end else begin
a_m = a_m << 1;
a_e = a_e - 1;
end
end
NORMALIZE_B: begin
if (b_m[23]) begin
state = DIVIDE_0;
end else begin
b_m = b_m << 1;
b_e = b_e - 1;
end
end
DIVIDE_0: begin
z_s = a_s ^ b_s;
z_e = a_e - b_e;
quotient = 0;
remainder = 0;
count = 0;
dividend = {a_m, 27'd0};
divisor = {27'd0, b_m};
state = DIVIDE_1;
end
DIVIDE_1: begin
quotient = quotient << 1;
remainder = remainder << 1;
remainder[0] = dividend[50];
dividend = dividend << 1;
state = DIVIDE_2;
end
DIVIDE_2: begin
if (remainder >= divisor) begin
quotient[0] = 1;
remainder = remainder - divisor;
end
if (count == 49) begin
state = DIVIDE_3;
end else begin
count = count + 1;
state = DIVIDE_1;
end
end
DIVIDE_3: begin
z_m = quotient[26:3];
guard = quotient[2];
round_bit = quotient[1];
sticky = quotient[0] | (remainder != 0);
state = NORMALIZE_0;
end
NORMALIZE_0: begin
if (z_m[23] == 0 && z_e > -126) begin
z_e = z_e - 1;
z_m = z_m << 1;
z_m[0] = guard;
guard = round_bit;
round_bit = 0;
end else begin
state = NORMALIZE_1;
end
end
NORMALIZE_1: begin
if (z_e < -126) begin
z_e = z_e + 1;
z_m = z_m >> 1;
guard = z_m[0];
round_bit = guard;
sticky = sticky | round_bit;
end else begin
state = ROUND;
end
end
ROUND: begin
if (guard && (round_bit | sticky | z_m[0])) begin
z_m = z_m + 1;
if (z_m == 24'hffffff) begin
z_e = z_e + 1;
end
end
state = PACK;
end
PACK: begin
z_value_o[22:0] = z_m[22:0];
z_value_o[30:23] = z_e[7:0] + 127;
z_value_o[31] = z_s;
if (z_e == -126 && z_m[23] == 0) begin
z_value_o[30:23] = 0;
end
// if overflow occurs, return inf
if (z_e > 127) begin
z_value_o[22:0] = 0;
z_value_o[30:23] = 255;
z_value_o[31] = z_s;
end
state = DONE;
end
DONE: begin
state = DONE;
end
default: state = IDLE;
endcase
end
end
endmodule