Subversion Repositories programming

Rev

Rev 204 | Rev 206 | 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
        value = in;
35
    end
36
 
37
    assign out = value;
38
 
39
endmodule
40
 
41
module MUL4_PRODUCT (left4, right4, shift_op, write_op, out, clk);
42
 
43
    input[0:3] left4, right4;
44
    input shift_op, write_op, clk;
45
    output[0:7] out;
46
 
47
    reg[0:7] value;
48
 
49
    always @(right4) begin
50
        value[0:3] = 'b0000;
51
        value[4:7] = right4;
52
    end
53
 
204 ira 54
    always @(negedge clk) begin
198 ira 55
        if (shift_op == 1) begin
56
            if (write_op == 1) begin
57
                value[0:3] = left4[0:3];
58
            end
204 ira 59
 
198 ira 60
            value = value >> 1;
61
        end
62
    end
63
 
64
    assign out = value;
65
 
66
endmodule
67
 
68
module testme;
69
 
70
    reg[0:3] a, b;
205 ira 71
    wire[0:7] w_out;
198 ira 72
 
205 ira 73
    MUL4 mult (a, b, w_out);
198 ira 74
 
75
    initial begin
205 ira 76
        $monitor ("time=%0d a=%b b=%b prod=%b", $time, a, b, w_out);
198 ira 77
    end
205 ira 78
 
204 ira 79
    initial begin
205 ira 80
            a = 'b0011; b = 'b0011;
204 ira 81
        #10 a = 'b0001; b = 'b1100;
82
        #10 a = 'b1100; b = 'b0011;
83
        #10 a = 'b0101; b = 'b1010;
84
        #8
85
        #2 $finish;
86
    end
87
 
198 ira 88
endmodule
89
 
205 ira 90
module MUL4 (a, b, out);
204 ira 91
 
92
    input[0:3] a, b;
93
    output[0:7] out;
94
 
95
    reg[0:3] counter;
96
    reg clk;
97
 
98
    initial begin
99
        clk = 'b0;
100
        counter ='b0000;
101
        cin = 'b0;
102
    end
103
 
104
    always begin
105
        #1 clk = ~clk;
106
    end
107
 
108
    always @(negedge clk) begin
109
        counter = counter + 'b1;
110
    end
205 ira 111
/*
204 ira 112
    initial begin
113
        $monitor ("time=%0d counter=%b clk=%b w_mout=%b w_prod=%b w_aop=%b w_sop=%b w_wop=%b out=%b",
114
               $time, counter, clk, w_mout, w_prod, w_aop, w_sop, w_wop, out);
115
    end
205 ira 116
*/
204 ira 117
    wire[0:7] w_prod;
118
    wire[0:3] w_mout, w_alu_out;
119
    wire[0:1] w_aop;
120
    wire w_sop, w_wop, w_cout;
121
    reg cin;
122
 
123
    MUL4_PRODUCT mp (w_alu_out, a, w_sop, w_wop, w_prod, clk);
124
    MUL4_MULTIPLICAND mm (b, w_mout, clk);
125
    MUL4_CONTROL mc (w_prod[7], w_aop, w_wop, w_sop, clk, counter);
126
    ALU4 ma (w_prod[0:3], w_mout, cin, w_aop, w_cout, w_alu_out);
127
 
128
    always @(counter) begin
129
        if (counter == 4) begin
130
            force out = w_prod;
131
            counter = 'b1111; //reset counter
132
        end else begin
133
            release out;
134
        end
135
    end
136
 
137
endmodule
138