-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCD.v
More file actions
124 lines (101 loc) · 2.25 KB
/
GCD.v
File metadata and controls
124 lines (101 loc) · 2.25 KB
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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 12:59:33 04/09/2015
// Design Name:
// Module Name: GCD
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module GCD(input [31:0] a, input [31:0] b, input reset, input clk, output reg [31:0] gcd, output reg isDone
);
reg [31:0] nextInputA;
reg [31:0] nextInputB;
reg [31:0] inputA;
reg [31:0] inputB;
reg [31:0] nextExponent;
reg [31:0] exponent;
reg [31:0] nextGcd;
reg nextIsDone;
initial begin
nextInputA = 0;
nextInputB = 0;
inputA = 0;
inputB = 0;
nextExponent = 0;
exponent = 0;
nextIsDone = 0;
isDone = 0;
nextGcd = 0;
gcd = 0;
end
always @(posedge clk) begin
inputA <= nextInputA;
inputB <= nextInputB;
exponent <= nextExponent;
isDone <= nextIsDone;
gcd <= nextGcd;
end
always @(*) begin
nextInputA = inputA;
nextInputB = inputB;
nextIsDone = isDone;
nextExponent = exponent;
nextGcd = gcd;
if (reset == 1) begin
nextInputA = 0;
nextInputB = 0;
nextExponent = 0;
nextIsDone = 0;
end
else begin
if (inputA == 0) begin
nextInputA = a;
nextInputB = b;
end
else begin
if ((inputA == inputB) & isDone == 0 & inputA > 0) begin
nextGcd = inputA << exponent;
nextIsDone = 1;
end
else begin
if ((inputA[0] == 0) & (inputB [0] == 0)) begin
nextExponent = exponent + 1;
nextInputA = inputA >> 1;
nextInputB = inputB >> 1;
end
else begin
nextExponent = exponent;
if (inputA[0] == 0) begin
nextInputA = inputA >> 1;
end
else begin
if (inputB[0] == 0) begin
nextInputB = inputB >> 1;
end
else begin
if (inputA > inputB) begin
nextInputA = (inputA - inputB) >> 1;
end
else begin
nextInputB = (inputB - inputA) >> 1;
end
end
end
end
end
end
end
end
endmodule