source: vis_dev/vis-2.1/examples/gcd/gcd3.v @ 11

Last change on this file since 11 was 11, checked in by cecile, 13 years ago

Add vis

File size: 3.0 KB
Line 
1// Testbench for the gcd circuit.
2module 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
28endmodule // 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>
34module 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    reg [logN:0]  cpt;
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) | (cpt > 13)) & 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        cpt=0;
91    end // initial begin
92
93    // Data path.
94    always @(posedge clock) begin
95        if (load) begin
96            x = a;
97            y = b;
98            lsb = 0;
99            cpt=0;
100        end // if (load)
101        else if (busy & ~done) begin
102            cpt = cpt+1 ;
103            case (xy_lsb)
104              2'b00:
105                 if (lsb<7) begin
106                  lsb = lsb + 1;
107                 end             
108              2'b01:
109                  begin
110                  x[N-2:0] = x[N-1:1];
111                  x[N-1] = 0;
112                  end
113              2'b10:
114                  begin
115                  y[N-2:0] = y[N-1:1];
116                  y[N-1] = 0;
117                  end
118              2'b11: begin
119                  if (x < y) begin
120                      y[N-2:0] = diff[N-1:1];
121                      y[N-1] = 0;
122                  end // if (x < y)
123                  else begin
124                      x[N-2:0] = diff[N-1:1];
125                      x[N-1] = 0;
126                  end // else: !if(x < y)
127              end // case: 2b'11
128            endcase // case (xy_lsb)
129        end // if (~done)
130        else if (done) begin
131            o = (x < y) ? x : y;
132        end // else: !if(~done)
133    end // always @ (posedge clock)
134
135   
136
137    // Controller.
138    always @(posedge clock) begin
139        if (~busy) begin
140            if (start) begin
141                busy = 1;
142            end // if (start)
143        end // if (~busy)
144        else begin
145            if (done) begin
146                busy = 0;
147            end
148        end // else: !if(~busy)
149    end // always @ (posedge clock)
150
151endmodule // gcd
Note: See TracBrowser for help on using the repository browser.