Subversion Repositories programming

Rev

Rev 203 | Rev 205 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 203 Rev 204
Line 1... Line 1...
1
/* A 4-bit multiplier, following the algorithm from
1
/* A 4-bit multiplier, following the algorithm from
2
 * Pg. 179, Figure 3.7. */
2
 * Pg. 179, Figure 3.7. */
3
 
3
 
4
module MUL4 (mul1, mul2, result, clk);
-
 
5
    input[0:3] mul1, mul2;
-
 
6
    output[0:7] result;
-
 
7
    input clk;
-
 
8
 
-
 
9
    wire[0:3] w_alu_out;
-
 
10
    wire[0:3] w_multiplicand;
-
 
11
    wire[0:7] w_product;
-
 
12
 
-
 
13
    wire[0:1] w_alu_op;
-
 
14
    wire w_write_op, w_shift_op;
-
 
15
 
-
 
16
    reg[0:3] count;
-
 
17
 
-
 
18
    MUL4_MULTIPLICAND m_mcand (mul1, w_multiplicand, clk);
-
 
19
    MUL4_PRODUCT m_prod (w_alu_out, mul2, w_shift_op, w_write_op, w_product, clk);
-
 
20
    MUL4_CONTROL m_ctrl (w_product[7], w_alu_op, w_write_op, w_shift_op, clk);
-
 
21
    ALU4 m_alu (w_product[0:3], w_multiplicand, cin, w_alu_op, cout, w_alu_out);
-
 
22
 
-
 
23
    always @(mul1 or mul2) begin
-
 
24
        count = 'b0000;
-
 
25
    end
-
 
26
 
-
 
27
    always @(clk) begin
-
 
28
        if (count == 4) begin
-
 
29
            $display ("set the result");
-
 
30
            force result = w_product;
-
 
31
        end
-
 
32
        count = count + 1;
-
 
33
    end
-
 
34
 
-
 
35
endmodule
-
 
36
 
-
 
37
module MUL4_2 (mul1, mul2, out);
-
 
38
    input[0:3] mul1, mul2;
-
 
39
    output[0:7] out;
-
 
40
 
-
 
41
    initial begin
-
 
42
        clk = 'b0;
-
 
43
    end
-
 
44
 
-
 
45
    always begin
-
 
46
        clk = ~clk;
-
 
47
    end
-
 
48
 
-
 
49
endmodule
-
 
50
 
-
 
51
module MUL4_CONTROL (prod_in, alu_op, write_op, shift_op, clk);
4
module MUL4_CONTROL (prod_in, alu_op, write_op, shift_op, clk, ctr);
52
 
5
 
53
    input prod_in, clk;
6
    input prod_in, clk;
-
 
7
    input[0:3] ctr;
54
    output[0:1] alu_op;
8
    output[0:1] alu_op;
55
    output write_op, shift_op;
9
    output write_op, shift_op;
56
 
10
 
-
 
11
    always @(negedge clk) begin
-
 
12
        if (ctr <= 4) begin
-
 
13
            force write_op = prod_in;
57
    reg[0:1] a_op;
14
            force shift_op = 'b1;
58
    reg w_op, s_op;
15
            force alu_op = 'b10;
59
 
-
 
60
    initial begin
16
        end else begin
-
 
17
            release write_op;
61
        a_op = 'b00;
18
            release shift_op;
62
        w_op = 'b0;
19
            release alu_op;
63
        s_op = 'b0;
20
        end
64
    end
21
    end
65
 
22
 
66
    always @(clk) begin
-
 
67
        if (prod_in == 'b1)
-
 
68
            begin
-
 
69
                a_op = 'b10; // ADD
-
 
70
                w_op = 'b1;  // STORE
-
 
71
                s_op = 'b1;  // SHIFT
-
 
72
            end
-
 
73
        else
-
 
74
            begin
-
 
75
                a_op = 'b00; // AND, unnecessary
-
 
76
                w_op = 'b0;  // DO NOT WRITE
-
 
77
                s_op = 'b1;  // SHIFT
-
 
78
            end
-
 
79
 
-
 
80
    end //always
-
 
81
 
-
 
82
    assign alu_op = a_op;
-
 
83
    assign write_op = w_op;
-
 
84
    assign shift_op = s_op;
-
 
85
 
-
 
86
endmodule
23
endmodule
87
 
24
 
88
module MUL4_MULTIPLICAND (in, out, clk);
25
module MUL4_MULTIPLICAND (in, out, clk);
89
 
26
 
90
    input[0:3] in;
27
    input[0:3] in;
Line 114... Line 51...
114
        value[0:3] = 'b0000;
51
        value[0:3] = 'b0000;
115
        value[4:7] = right4;
52
        value[4:7] = right4;
116
        $display ("right4 changed");
53
        $display ("right4 changed");
117
    end
54
    end
118
 
55
 
119
    always @(clk) begin
56
    always @(negedge clk) begin
120
        $display ("value=%b", value);
-
 
121
        if (shift_op == 1) begin
57
        if (shift_op == 1) begin
122
            $display ("shift 1");
-
 
123
            if (write_op == 1) begin
58
            if (write_op == 1) begin
124
                $display ("write 1");
59
                $display ("write");
125
                value[0:3] = left4[0:3];
60
                value[0:3] = left4[0:3];
126
            end
61
            end
127
 
62
            
-
 
63
            $display ("shift");
128
            value = value >> 1;
64
            value = value >> 1;
129
        end
65
        end
130
    end
66
    end
131
 
67
 
132
    assign out = value;
68
    assign out = value;
133
 
69
 
134
endmodule
70
endmodule
135
 
71
 
136
module testme;
72
module testme;
137
 
73
 
-
 
74
    /*
138
    reg[0:3] a, b;
75
    reg[0:3] a, b;
139
    wire[0:7] prod;
76
    wire[0:7] prod;
140
 
77
 
141
    initial begin
78
    initial begin
142
        $monitor ("time=%0d a=%b b=%b prod=%b", $time, a, b, prod);
79
        $monitor ("time=%0d a=%b b=%b prod=%b", $time, a, b, prod);
143
    end
80
    end
144
 
81
 
145
    initial begin
82
    initial begin
146
           clk = 'b0;
-
 
147
           a = 'b0000; b = 'b0000;
83
           a = 'b0000; b = 'b0000;
148
        #4 a = 'b0011; b = 'b1111;
84
        #4 a = 'b0011; b = 'b1111;
149
        #4
85
        #4
150
        $finish;
86
        $finish;
151
    end
87
    end
152
 
88
 
-
 
89
    MUL4 m4 (a, b, prod);
-
 
90
    */
-
 
91
 
-
 
92
    /*
-
 
93
    wire[0:1] aop;
-
 
94
    wire wop, sop;
-
 
95
    reg clk;
-
 
96
    reg pin;
-
 
97
 
-
 
98
    always begin
-
 
99
        #1 clk = ~clk;
-
 
100
    end
-
 
101
 
-
 
102
    initial begin
-
 
103
        pin = 'b0; clk='b0;
-
 
104
        #1 pin = 'b1;
-
 
105
        #1 pin = 'b0;
-
 
106
        #1 pin = 'b1;
-
 
107
        #1 pin = 'b1;
-
 
108
        #1 pin = 'b0;
-
 
109
        #1
-
 
110
        $finish;
-
 
111
    end
-
 
112
 
-
 
113
    initial begin
-
 
114
        $monitor ("time=%0d pin=%b aop=%b wop=%b sop=%b clk=%b", $time, pin, aop, wop, sop, clk);
-
 
115
    end
-
 
116
 
-
 
117
    MUL4_CONTROL mc (pin, aop, wop, sop, clk);
-
 
118
    */
-
 
119
 
-
 
120
    /*
-
 
121
    reg[0:3] l, r;
-
 
122
    wire sop, wop;
-
 
123
    wire[0:1] aop;
-
 
124
    wire[0:7] prod;
-
 
125
    reg clk;
-
 
126
 
-
 
127
    always begin
-
 
128
        #1 clk = ~clk;
-
 
129
    end
-
 
130
 
-
 
131
    initial begin
-
 
132
        clk = 'b0;
-
 
133
        #1 l = 'b1000; r='b1111;
-
 
134
        #1
-
 
135
        #1
-
 
136
        #1
-
 
137
        #1
-
 
138
        #1
-
 
139
        #1
-
 
140
        #1
-
 
141
        #1
-
 
142
        #1
-
 
143
        #1
-
 
144
        #1
-
 
145
        #1
-
 
146
        #1
-
 
147
        #1
-
 
148
        $finish;
-
 
149
    end
-
 
150
 
-
 
151
    initial begin
-
 
152
        $monitor ("time=%0d prod=%b aop=%b wop=%b sop=%b clk=%b l=%b r=%b",
-
 
153
                $time, prod, aop, wop, sop, clk, l, r);
-
 
154
    end
-
 
155
 
-
 
156
    MUL4_CONTROL mc (prod[7], aop, wop, sop, clk);
-
 
157
    MUL4_PRODUCT mp (l, r, sop, wop, prod, clk);
-
 
158
    */
-
 
159
 
-
 
160
    reg[0:3] a, b;
-
 
161
    wire[0:7] w_out;
-
 
162
 
-
 
163
    MUL4_2 mult (a, b, w_out);
-
 
164
 
-
 
165
    initial begin
-
 
166
           a = 'b0011; b = 'b0011;
-
 
167
        #10 a = 'b0001; b = 'b1100;
-
 
168
        #10 a = 'b1100; b = 'b0011;
-
 
169
        #10 a = 'b0101; b = 'b1010;
-
 
170
        #8
-
 
171
        #2 $finish;
-
 
172
    end
-
 
173
 
-
 
174
endmodule
-
 
175
 
-
 
176
module MUL4_2 (a, b, out);
-
 
177
 
-
 
178
    input[0:3] a, b;
-
 
179
    output[0:7] out;
-
 
180
 
-
 
181
    reg[0:3] counter;
153
    reg clk;
182
    reg clk;
154
 
183
 
-
 
184
    initial begin
155
    // Clock Generator
185
        clk = 'b0;
-
 
186
        counter ='b0000;
-
 
187
        cin = 'b0;
-
 
188
    end
-
 
189
 
156
    always begin
190
    always begin
157
        #1 clk = ~clk;
191
        #1 clk = ~clk;
158
    end
192
    end
159
 
193
 
-
 
194
    always @(negedge clk) begin
-
 
195
        counter = counter + 'b1;
-
 
196
    end
-
 
197
 
-
 
198
    initial begin
-
 
199
        $monitor ("time=%0d counter=%b clk=%b w_mout=%b w_prod=%b w_aop=%b w_sop=%b w_wop=%b out=%b",
-
 
200
               $time, counter, clk, w_mout, w_prod, w_aop, w_sop, w_wop, out);
-
 
201
    end
-
 
202
 
-
 
203
    always @(negedge clk) begin
-
 
204
        $display ("negedge clk");
-
 
205
    end
-
 
206
 
-
 
207
    wire[0:7] w_prod;
-
 
208
    wire[0:3] w_mout, w_alu_out;
-
 
209
    wire[0:1] w_aop;
-
 
210
    wire w_sop, w_wop, w_cout;
-
 
211
    reg cin;
-
 
212
 
-
 
213
    MUL4_PRODUCT mp (w_alu_out, a, w_sop, w_wop, w_prod, clk);
160
    MUL4 m4 (a, b, prod, clk);
214
    MUL4_MULTIPLICAND mm (b, w_mout, clk);
-
 
215
    MUL4_CONTROL mc (w_prod[7], w_aop, w_wop, w_sop, clk, counter);
-
 
216
    ALU4 ma (w_prod[0:3], w_mout, cin, w_aop, w_cout, w_alu_out);
-
 
217
 
-
 
218
    always @(counter) begin
-
 
219
        if (counter == 4) begin
-
 
220
            force out = w_prod;
-
 
221
            counter = 'b1111; //reset counter
-
 
222
        end else begin
-
 
223
            release out;
-
 
224
        end
-
 
225
    end
161
 
226
 
162
endmodule
227
endmodule
163
 
228