-
Notifications
You must be signed in to change notification settings - Fork 117
/
restoring_div_R2.v
93 lines (72 loc) · 1.38 KB
/
restoring_div_R2.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
module restoring_div_R2(clk, reset, Dividend, Divisor, Quotient, Remainder);
input clk, reset;
input [63:0] Dividend, Divisor;
output [63:0] Quotient, Remainder;
reg [63:0] Quotient, Remainder;
reg [63:0] p, a, temp;
integer i;
always @(posedge clk, negedge reset)
begin
if( !reset )
begin
Quotient <= 0;
Remainder <= 0;
end
else
begin
Quotient <= a;
Remainder <= p;
end
end
always @(*)
begin
a = Dividend;
p = 0;
for(i = 0; i < 64; i = i+1)
begin
//Shift Left carrying a's MSB into p's LSB
p = (p << 1) | a[63];
a = a << 1;
//store value in case we have to restore
temp = p;
//Subtract
p = p - Divisor;
if( p[63] ) // if p < 0
p = temp; //restore value
else
a = a | 1;
end
end
endmodule
module restoring_div_R2_tb;
reg clk, reset;
reg [63:0] dividend, divisor;
wire [63:0] quotient, remainder;
restoring_div_R2 divider(clk, reset, dividend, divisor, quotient, remainder);
initial
forever #1 clk = ~clk;
initial
$monitor("%0d / %0d: q = %0d, r = %0d", dividend, divisor, quotient, remainder);
initial
begin
clk = 0;
reset = 0;
#1;
reset = 1;
dividend = 87;
divisor = 5;
#5;
dividend = 59;
divisor = 20;
#5;
dividend = 64'hFFFF_FFFF_FFFF_FFFF;
divisor = 2;
#5;
dividend = 32'h1234_5678;
divisor = 1;
#5;
divisor = dividend;
#5;
$finish;
end
endmodule