Showing posts with label GCD. Show all posts
Showing posts with label GCD. Show all posts

Digital "Greatest Common Divisor (GCD)" - Euclid’s algorithm


By popular demand!
Verilog module that uses Euclid's algorithm to iteratively compute the greatest common divisor of two 16-bit unsigned integer values Ain and Bin where Ain ≥ Bin.

module gcd (clk,start,Ain,Bin,answer,done);
input clk,start;
input [15:0] Ain,Bin;

output reg [15:0] answer;

output reg done;


reg [15:0] a,b;

always @ (posedge clk)
begin
if (start)
begin
a <= Ain; b <= Bin; done <= 0;
end
else if (b == 0)
begin
answer <= a;
done <= 1;
end
else if (a > b)
a <= a – b;
else
b <= b – a;
end
endmodule

--
Create your RupeeMail Account & refer your friends to earn launch referral bonus on every new registration.
Try this... http://www.rupeemail.in/rupeemail/invite.do?in=MTUyNjUlIyVSajZ5dlBocDhrU3ozWVZ3bTBhZWQyQ2ZF