Subversion Repositories programming

Rev

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

Rev 198 Rev 203
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);
4
module MUL4 (mul1, mul2, result, clk);
5
    input[0:3] mul1, mul2;
5
    input[0:3] mul1, mul2;
6
    output[0:7] result;
6
    output[0:7] result;
-
 
7
    input clk;
7
 
8
 
8
    wire[0:3] w_alu_out;
9
    wire[0:3] w_alu_out;
9
    wire[0:3] w_multiplicand;
10
    wire[0:3] w_multiplicand;
10
    wire[0:7] w_product;
11
    wire[0:7] w_product;
11
 
12
 
12
    wire[0:1] w_alu_op;
13
    wire[0:1] w_alu_op;
13
    wire w_write_op, w_shift_op;
14
    wire w_write_op, w_shift_op;
14
 
15
 
15
    reg clk;
-
 
16
    reg[0:3] ctr;
16
    reg[0:3] count;
17
    
-
 
18
    initial begin
-
 
19
        clk = 'b0;
-
 
20
        ctr = 'b0000;
-
 
21
    end
-
 
22
 
17
 
23
    MUL4_MULTIPLICAND m_mcand (mul1, w_multiplicand, clk);
18
    MUL4_MULTIPLICAND m_mcand (mul1, w_multiplicand, clk);
24
    MUL4_PRODUCT m_prod (w_alu_out, mul2, w_shift_op, w_write_op, w_product, clk);
19
    MUL4_PRODUCT m_prod (w_alu_out, mul2, w_shift_op, w_write_op, w_product, clk);
25
    MUL4_CONTROL m_ctrl (w_product[7], w_alu_op, w_write_op, w_shift_op, clk);
20
    MUL4_CONTROL m_ctrl (w_product[7], w_alu_op, w_write_op, w_shift_op, clk);
26
    ALU4 m_alu (w_product[0:3], w_multiplicand, cin, w_alu_op, cout, w_alu_out);
21
    ALU4 m_alu (w_product[0:3], w_multiplicand, cin, w_alu_op, cout, w_alu_out);
27
 
22
 
28
    always @(mul1 or mul2) begin
23
    always @(mul1 or mul2) begin
-
 
24
        count = 'b0000;
-
 
25
    end
-
 
26
 
29
        repeat (4) begin
27
    always @(clk) begin
30
            $display ("clocked");
28
        if (count == 4) begin
31
            clk = ~clk;
29
            $display ("set the result");
32
            ctr = ctr + 1;
30
            force result = w_product;
33
        end
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;
34
    end
47
    end
35
 
48
 
36
endmodule
49
endmodule
37
 
50
 
38
module MUL4_CONTROL (prod_in, alu_op, write_op, shift_op, clk);
51
module MUL4_CONTROL (prod_in, alu_op, write_op, shift_op, clk);
Line 101... Line 114...
101
        value[0:3] = 'b0000;
114
        value[0:3] = 'b0000;
102
        value[4:7] = right4;
115
        value[4:7] = right4;
103
        $display ("right4 changed");
116
        $display ("right4 changed");
104
    end
117
    end
105
 
118
 
106
    always @(/*shift_op or write_op or*/ clk) begin
119
    always @(clk) begin
107
        $display ("value=%b", value);
120
        $display ("value=%b", value);
108
        if (shift_op == 1) begin
121
        if (shift_op == 1) begin
109
            $display ("shift 1");
122
            $display ("shift 1");
110
            if (write_op == 1) begin
123
            if (write_op == 1) begin
111
                $display ("write 1");
124
                $display ("write 1");
Line 128... Line 141...
128
    initial begin
141
    initial begin
129
        $monitor ("time=%0d a=%b b=%b prod=%b", $time, a, b, prod);
142
        $monitor ("time=%0d a=%b b=%b prod=%b", $time, a, b, prod);
130
    end
143
    end
131
 
144
 
132
    initial begin
145
    initial begin
-
 
146
           clk = 'b0;
133
           a = 'b0000; b = 'b0000;
147
           a = 'b0000; b = 'b0000;
134
        #1 a = 'b0011; b = 'b1111;
148
        #4 a = 'b0011; b = 'b1111;
-
 
149
        #4
-
 
150
        $finish;
-
 
151
    end
-
 
152
 
-
 
153
    reg clk;
-
 
154
 
-
 
155
    // Clock Generator
-
 
156
    always begin
-
 
157
        #1 clk = ~clk;
135
    end
158
    end
136
 
159
 
137
    MUL4 m4 (a, b, prod);
160
    MUL4 m4 (a, b, prod, clk);
138
    
161
 
139
endmodule
162
endmodule
140
 
163