Subversion Repositories programming

Rev

Rev 203 | Rev 205 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
198 ira 1
/* A 4-bit multiplier, following the algorithm from
2
 * Pg. 179, Figure 3.7. */
3
 
204 ira 4
module MUL4_CONTROL (prod_in, alu_op, write_op, shift_op, clk, ctr);
198 ira 5
 
6
    input prod_in, clk;
204 ira 7
    input[0:3] ctr;
198 ira 8
    output[0:1] alu_op;
9
    output write_op, shift_op;
10
 
204 ira 11
    always @(negedge clk) begin
12
        if (ctr <= 4) begin
13
            force write_op = prod_in;
14
            force shift_op = 'b1;
15
            force alu_op = 'b10;
16
        end else begin
17
            release write_op;
18
            release shift_op;
19
            release alu_op;
20
        end
198 ira 21
    end
22
 
23
endmodule
24
 
25
module MUL4_MULTIPLICAND (in, out, clk);
26
 
27
    input[0:3] in;
28
    output[0:3] out;
29
    input clk;
30
 
31
    reg[0:3] value;
32
 
33
    always @(in) begin
34
        $display ("multiplicand changed");
35
        value = in;
36
    end
37
 
38
    assign out = value;
39
 
40
endmodule
41
 
42
module MUL4_PRODUCT (left4, right4, shift_op, write_op, out, clk);
43
 
44
    input[0:3] left4, right4;
45
    input shift_op, write_op, clk;
46
    output[0:7] out;
47
 
48
    reg[0:7] value;
49
 
50
    always @(right4) begin
51
        value[0:3] = 'b0000;
52
        value[4:7] = right4;
53
        $display ("right4 changed");
54
    end
55
 
204 ira 56
    always @(negedge clk) begin
198 ira 57
        if (shift_op == 1) begin
58
            if (write_op == 1) begin
204 ira 59
                $display ("write");
198 ira 60
                value[0:3] = left4[0:3];
61
            end
204 ira 62
 
63
            $display ("shift");
198 ira 64
            value = value >> 1;
65
        end
66
    end
67
 
68
    assign out = value;
69
 
70
endmodule
71
 
72
module testme;
73
 
204 ira 74
    /*
198 ira 75
    reg[0:3] a, b;
76
    wire[0:7] prod;
77
 
78
    initial begin
79
        $monitor ("time=%0d a=%b b=%b prod=%b", $time, a, b, prod);
80
    end
81
 
82
    initial begin
83
           a = 'b0000; b = 'b0000;
203 ira 84
        #4 a = 'b0011; b = 'b1111;
85
        #4
86
        $finish;
198 ira 87
    end
88
 
204 ira 89
    MUL4 m4 (a, b, prod);
90
    */
91
 
92
    /*
93
    wire[0:1] aop;
94
    wire wop, sop;
203 ira 95
    reg clk;
204 ira 96
    reg pin;
203 ira 97
 
98
    always begin
99
        #1 clk = ~clk;
100
    end
101
 
204 ira 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
203 ira 112
 
204 ira 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
 
198 ira 174
endmodule
175
 
204 ira 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;
182
    reg clk;
183
 
184
    initial begin
185
        clk = 'b0;
186
        counter ='b0000;
187
        cin = 'b0;
188
    end
189
 
190
    always begin
191
        #1 clk = ~clk;
192
    end
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);
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
226
 
227
endmodule
228