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