Subversion Repositories programming

Rev

Rev 203 | Go to most recent revision | Details | 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
 
4
module MUL4 (mul1, mul2, result);
5
    input[0:3] mul1, mul2;
6
    output[0:7] result;
7
 
8
    wire[0:3] w_alu_out;
9
    wire[0:3] w_multiplicand;
10
    wire[0:7] w_product;
11
 
12
    wire[0:1] w_alu_op;
13
    wire w_write_op, w_shift_op;
14
 
15
    reg clk;
16
    reg[0:3] ctr;
17
 
18
    initial begin
19
        clk = 'b0;
20
        ctr = 'b0000;
21
    end
22
 
23
    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);
25
    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);
27
 
28
    always @(mul1 or mul2) begin
29
        repeat (4) begin
30
            $display ("clocked");
31
            clk = ~clk;
32
            ctr = ctr + 1;
33
        end
34
    end
35
 
36
endmodule
37
 
38
module MUL4_CONTROL (prod_in, alu_op, write_op, shift_op, clk);
39
 
40
    input prod_in, clk;
41
    output[0:1] alu_op;
42
    output write_op, shift_op;
43
 
44
    reg[0:1] a_op;
45
    reg w_op, s_op;
46
 
47
    initial begin
48
        a_op = 'b00;
49
        w_op = 'b0;
50
        s_op = 'b0;
51
    end
52
 
53
    always @(clk) begin
54
        if (prod_in == 'b1)
55
            begin
56
                a_op = 'b10; // ADD
57
                w_op = 'b1;  // STORE
58
                s_op = 'b1;  // SHIFT
59
            end
60
        else
61
            begin
62
                a_op = 'b00; // AND, unnecessary
63
                w_op = 'b0;  // DO NOT WRITE
64
                s_op = 'b1;  // SHIFT
65
            end
66
 
67
    end //always
68
 
69
    assign alu_op = a_op;
70
    assign write_op = w_op;
71
    assign shift_op = s_op;
72
 
73
endmodule
74
 
75
module MUL4_MULTIPLICAND (in, out, clk);
76
 
77
    input[0:3] in;
78
    output[0:3] out;
79
    input clk;
80
 
81
    reg[0:3] value;
82
 
83
    always @(in) begin
84
        $display ("multiplicand changed");
85
        value = in;
86
    end
87
 
88
    assign out = value;
89
 
90
endmodule
91
 
92
module MUL4_PRODUCT (left4, right4, shift_op, write_op, out, clk);
93
 
94
    input[0:3] left4, right4;
95
    input shift_op, write_op, clk;
96
    output[0:7] out;
97
 
98
    reg[0:7] value;
99
 
100
    always @(right4) begin
101
        value[0:3] = 'b0000;
102
        value[4:7] = right4;
103
        $display ("right4 changed");
104
    end
105
 
106
    always @(/*shift_op or write_op or*/ clk) begin
107
        $display ("value=%b", value);
108
        if (shift_op == 1) begin
109
            $display ("shift 1");
110
            if (write_op == 1) begin
111
                $display ("write 1");
112
                value[0:3] = left4[0:3];
113
            end
114
 
115
            value = value >> 1;
116
        end
117
    end
118
 
119
    assign out = value;
120
 
121
endmodule
122
 
123
module testme;
124
 
125
    reg[0:3] a, b;
126
    wire[0:7] prod;
127
 
128
    initial begin
129
        $monitor ("time=%0d a=%b b=%b prod=%b", $time, a, b, prod);
130
    end
131
 
132
    initial begin
133
           a = 'b0000; b = 'b0000;
134
        #1 a = 'b0011; b = 'b1111;
135
    end
136
 
137
    MUL4 m4 (a, b, prod);
138
 
139
endmodule
140