forked from sudhamshu091/32-Verilog-Mini-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction_multiplication.v
75 lines (73 loc) · 1.01 KB
/
fraction_multiplication.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
`define M B[0]
module fraction_multiplication (CLK, St, Mplier, Mcand, Product, Done);
input CLK;
input St;
input[3:0] Mplier;
input[3:0] Mcand;
output[6:0] Product;
output Done;
reg[2:0] State;
reg[3:0] A;
reg[3:0] B;
reg[3:0] addout;
initial
begin
State = 0;
end
always @(posedge CLK)
begin
case (State)
0 :
begin
if (St == 1'b1)
begin
A <= 4'b0000 ;
B <= Mplier ;
State <= 1 ;
end
else
State <= 0;
end
1, 2, 3 :
begin
if (`M == 1'b1)
begin
addout = A + Mcand;
A <= {Mcand[3], addout[3:1]} ;
B <= {addout[0], B[3:1]} ;
end
else
begin
A <= {A[3], A[3:1]} ;
B <= {A[0], B[3:1]} ;
end
State <= State + 1 ;
end
4 :
begin
if (`M == 1'b1)
begin
addout = A + ~Mcand + 1;
A <= {~Mcand[3], addout[3:1]} ;
B <= {addout[0], B[3:1]} ;
end
else
begin
A <= {A[3], A[3:1]} ;
B <= {A[0], B[3:1]} ;
end
State <= 5 ;
end
5 :
begin
State <= 0 ;
end
default :
begin
State <= 0 ;
end
endcase
end
assign Done = (State == 5) ? 1'b1 : 1'b0 ;
assign Product = {A[2:0], B} ;
endmodule