Subversion Repositories programming

Rev

Rev 198 | Rev 204 | 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
 
203 ira 4
module MUL4 (mul1, mul2, result, clk);
198 ira 5
    input[0:3] mul1, mul2;
6
    output[0:7] result;
203 ira 7
    input clk;
198 ira 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
 
203 ira 16
    reg[0:3] count;
198 ira 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);
203 ira 22
 
198 ira 23
    always @(mul1 or mul2) begin
203 ira 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;
198 ira 31
        end
203 ira 32
        count = count + 1;
198 ira 33
    end
34
 
35
endmodule
36
 
203 ira 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
 
198 ira 51
module MUL4_CONTROL (prod_in, alu_op, write_op, shift_op, clk);
52
 
53
    input prod_in, clk;
54
    output[0:1] alu_op;
55
    output write_op, shift_op;
56
 
57
    reg[0:1] a_op;
58
    reg w_op, s_op;
59
 
60
    initial begin
61
        a_op = 'b00;
62
        w_op = 'b0;
63
        s_op = 'b0;
64
    end
65
 
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
87
 
88
module MUL4_MULTIPLICAND (in, out, clk);
89
 
90
    input[0:3] in;
91
    output[0:3] out;
92
    input clk;
93
 
94
    reg[0:3] value;
95
 
96
    always @(in) begin
97
        $display ("multiplicand changed");
98
        value = in;
99
    end
100
 
101
    assign out = value;
102
 
103
endmodule
104
 
105
module MUL4_PRODUCT (left4, right4, shift_op, write_op, out, clk);
106
 
107
    input[0:3] left4, right4;
108
    input shift_op, write_op, clk;
109
    output[0:7] out;
110
 
111
    reg[0:7] value;
112
 
113
    always @(right4) begin
114
        value[0:3] = 'b0000;
115
        value[4:7] = right4;
116
        $display ("right4 changed");
117
    end
118
 
203 ira 119
    always @(clk) begin
198 ira 120
        $display ("value=%b", value);
121
        if (shift_op == 1) begin
122
            $display ("shift 1");
123
            if (write_op == 1) begin
124
                $display ("write 1");
125
                value[0:3] = left4[0:3];
126
            end
127
 
128
            value = value >> 1;
129
        end
130
    end
131
 
132
    assign out = value;
133
 
134
endmodule
135
 
136
module testme;
137
 
138
    reg[0:3] a, b;
139
    wire[0:7] prod;
140
 
141
    initial begin
142
        $monitor ("time=%0d a=%b b=%b prod=%b", $time, a, b, prod);
143
    end
144
 
145
    initial begin
203 ira 146
           clk = 'b0;
198 ira 147
           a = 'b0000; b = 'b0000;
203 ira 148
        #4 a = 'b0011; b = 'b1111;
149
        #4
150
        $finish;
198 ira 151
    end
152
 
203 ira 153
    reg clk;
154
 
155
    // Clock Generator
156
    always begin
157
        #1 clk = ~clk;
158
    end
159
 
160
    MUL4 m4 (a, b, prod, clk);
161
 
198 ira 162
endmodule
163