Line | |
---|
1 | |
---|
2 | // GCD circuit for unsigned N-bit numbers |
---|
3 | // a[0], b[0], and o[0] are the least significant bits |
---|
4 | // |
---|
5 | // Author: Fabio Somenzi <Fabio@Colorado.EDU> |
---|
6 | module gcd(clock,start,a,b,busy,o); |
---|
7 | parameter N = 8; |
---|
8 | parameter logN = 3; |
---|
9 | input clock; |
---|
10 | input start; |
---|
11 | input [N-1:0] a; |
---|
12 | input [N-1:0] b; |
---|
13 | output busy; |
---|
14 | output [N-1:0] o; |
---|
15 | |
---|
16 | reg [logN-1:0] lsb; |
---|
17 | reg [N-1:0] x; |
---|
18 | reg [N-1:0] y; |
---|
19 | wire done; |
---|
20 | wire load; |
---|
21 | reg busy; |
---|
22 | reg [N-1:0] o; |
---|
23 | |
---|
24 | wire [1:0] xy_lsb; |
---|
25 | wire [N-1:0] diff; |
---|
26 | |
---|
27 | function select; |
---|
28 | input [N-1:0] z; |
---|
29 | input [logN-1:0] lsb; |
---|
30 | begin: _select |
---|
31 | if (lsb == 3'd0) |
---|
32 | select = z[0]; |
---|
33 | else if (lsb == 3'd1) |
---|
34 | select = z[1]; |
---|
35 | else if (lsb == 3'd2) |
---|
36 | select = z[2]; |
---|
37 | else if (lsb == 3'd3) |
---|
38 | select = z[3]; |
---|
39 | else if (lsb == 3'd4) |
---|
40 | select = z[4]; |
---|
41 | else if (lsb == 3'd5) |
---|
42 | select = z[5]; |
---|
43 | else if (lsb == 3'd6) |
---|
44 | select = z[6]; |
---|
45 | else |
---|
46 | select = z[7]; |
---|
47 | end // block: _select |
---|
48 | endfunction // select |
---|
49 | |
---|
50 | assign xy_lsb[1] = select(x,lsb); |
---|
51 | assign xy_lsb[0] = select(y,lsb); |
---|
52 | assign diff = x < y ? y - x : x - y; |
---|
53 | |
---|
54 | initial begin |
---|
55 | busy = 0; |
---|
56 | x = 0; |
---|
57 | y = 0; |
---|
58 | o = 0; |
---|
59 | lsb = 0; |
---|
60 | end // initial begin |
---|
61 | |
---|
62 | assign done = ((x == y) | (x == 0) | (y == 0)) & busy; |
---|
63 | |
---|
64 | // Data path. |
---|
65 | always @(posedge clock) begin |
---|
66 | if (load) begin |
---|
67 | x = a; |
---|
68 | y = b; |
---|
69 | lsb = 0; |
---|
70 | end // if (load) |
---|
71 | else if (busy & ~done) begin |
---|
72 | case (xy_lsb) |
---|
73 | 2'b00: |
---|
74 | lsb = lsb + 1; |
---|
75 | 2'b01: |
---|
76 | begin |
---|
77 | x[N-2:0] = x[N-1:1]; |
---|
78 | x[N-1] = 0; |
---|
79 | end |
---|
80 | 2'b10: |
---|
81 | begin |
---|
82 | y[N-2:0] = y[N-1:1]; |
---|
83 | y[N-1] = 0; |
---|
84 | end |
---|
85 | 2'b11: begin |
---|
86 | if (x < y) begin |
---|
87 | y[N-2:0] = diff[N-1:1]; |
---|
88 | y[N-1] = 0; |
---|
89 | end // if (x < y) |
---|
90 | else begin |
---|
91 | x[N-2:0] = diff[N-1:1]; |
---|
92 | x[N-1] = 0; |
---|
93 | end // else: !if(x < y) |
---|
94 | end // case: 2b'11 |
---|
95 | endcase // case (xy_lsb) |
---|
96 | end // if (~done) |
---|
97 | else if (done) begin |
---|
98 | o = (x < y) ? x : y; |
---|
99 | end // else: !if(~done) |
---|
100 | end // always @ (posedge clock) |
---|
101 | |
---|
102 | assign load = start & ~busy; |
---|
103 | |
---|
104 | // Controller. |
---|
105 | always @(posedge clock) begin |
---|
106 | if (~busy) begin |
---|
107 | if (start) begin |
---|
108 | busy = 1; |
---|
109 | end // if (start) |
---|
110 | end // if (~busy) |
---|
111 | else begin |
---|
112 | if (done) begin |
---|
113 | busy = 0; |
---|
114 | end |
---|
115 | end // else: !if(~busy) |
---|
116 | end // always @ (posedge clock) |
---|
117 | |
---|
118 | endmodule // gcd |
---|
Note: See
TracBrowser
for help on using the repository browser.