forked from pConst/basic_verilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NDivide.v
97 lines (77 loc) · 2.15 KB
/
NDivide.v
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
//--------------------------------------------------------------------------------
// NDivide.v
// Konstantin Pavlov, [email protected]
//--------------------------------------------------------------------------------
// INFO --------------------------------------------------------------------------------
// Primitive integer divider
// Unsigned inputs, y should be < or == x
/* --- INSTANTIATION TEMPLATE BEGIN ---
NDivide ND1 (
.clk( ),
.nrst( 1'b1 ),
.d_start( ),
.d_busy( ),
.d_done( ),
.x( ),
.y( ),
.q( ),
.r( )
);
defparam ND1.XBITS = 32;
defparam ND1.YBITS = 32;
--- INSTANTIATION TEMPLATE END ---*/
module NDivide(clk,nrst,d_start,d_busy,d_done,x,y,q,r);
parameter XBITS = 32;
parameter YBITS = 32;
input wire clk;
input wire nrst;
input wire d_start;
output reg d_busy = 0;
output wire d_done;
input wire [(XBITS-1):0] x;
input wire [(YBITS-1):0] y;
output reg [(XBITS-1):0] q = 0;
output wire [(YBITS-1):0] r;
reg [(XBITS+YBITS-1):0] x_buf = 0;
reg [(YBITS-1):0] y_buf = 0;
reg [31:0] i = 0;
wire [(YBITS+XBITS-1):0] shift_y;
wire [(YBITS+XBITS-1):0] x_buf_sub_shift_y;
assign
shift_y[(YBITS+XBITS-1):0] = y_buf[(YBITS-1):0] << i[31:0],
x_buf_sub_shift_y[(YBITS+XBITS-1):0] = x_buf[(YBITS+XBITS-1):0] - shift_y[(YBITS+XBITS-1):0];
always @ (posedge clk) begin
if (~nrst) begin
q[(XBITS-1):0] <= 0;
i[31:0] <= 0;
x_buf[(XBITS+YBITS-1):0] <= 0;
y_buf[(YBITS-1):0] <= 0;
d_busy <= 0;
end else begin
if (~d_busy) begin
if (d_start) begin
i[31:0] <= (XBITS-1);
x_buf[(XBITS+YBITS-1):0] <= x[(XBITS-1):0];
y_buf[(YBITS-1):0] <= y[(YBITS-1):0];
d_busy <= 1;
end // d_start
end else begin
// this condition means crossing of zero boundary
if (x_buf_sub_shift_y[(YBITS+XBITS-1):0] > x_buf[(XBITS+YBITS-1):0]) begin
q[i[31:0]] <= 0;
end else begin
q[i[31:0]] <= 1;
x_buf[(XBITS+YBITS-1):0] <= x_buf_sub_shift_y[(YBITS+XBITS-1):0];
end
if (i[31:0] != 0) begin
i[31:0] <= i[31:0] - 1;
end else begin
d_busy <= 0;
end
end // ~d_busy
end // ~nrst
end
assign
d_done = d_busy && ( i[31:0] == 0 ),
r[(XBITS-1):0] = x_buf[(XBITS+YBITS-1):0];
endmodule