Subversion Repositories programming

Rev

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

Rev 194 Rev 195
Line 8... Line 8...
8
 
8
 
9
//Basic unit: 2-input or gate
9
//Basic unit: 2-input or gate
10
module OR_2 (in1, in2, out);
10
module OR_2 (in1, in2, out);
11
   input in1, in2;
11
   input in1, in2;
12
   output out;
12
   output out;
13
   assign out = in1 || in2;
13
   assign out = in1 | in2;
14
endmodule
14
endmodule
15
 
15
 
16
//Basic unit: 3-input or gate
16
//Basic unit: 3-input or gate
17
module OR_3(in1, in2, in3, out);
17
module OR_3(in1, in2, in3, out);
18
   input in1, in2, in3;
18
   input in1, in2, in3;
19
   output out;
19
   output out;
20
   assign out = in1 || in2 || in3;
20
   assign out = in1 | in2 | in3;
21
endmodule
21
endmodule
22
 
22
 
23
//Basic unit: 3-input and gate
23
//Basic unit: 3-input and gate
24
module AND_3 (in1, in2, in3, out);
24
module AND_3 (in1, in2, in3, out);
25
   input in1, in2, in3;
25
   input in1, in2, in3;
Line 29... Line 29...
29
 
29
 
30
//Basic unit: 4-input or gate
30
//Basic unit: 4-input or gate
31
module OR_4 (in1, in2, in3, in4, out);
31
module OR_4 (in1, in2, in3, in4, out);
32
   input in1, in2, in3, in4;
32
   input in1, in2, in3, in4;
33
   output out;
33
   output out;
34
   assign out = in1 || in2 || in3 || in4;
34
   assign out = in1 | in2 | in3 | in4;
35
endmodule
35
endmodule
36
 
36
 
37
//Basic unit: Invert
37
//Basic unit: Invert
38
module NOT_1 (in, out);
38
module NOT_1 (in, out);
39
   input in;
39
   input in;
Line 78... Line 78...
78
 
78
 
79
// basic unit: 2-input NOR gate
79
// basic unit: 2-input NOR gate
80
module NOR_2 ( in1, in2, out);
80
module NOR_2 ( in1, in2, out);
81
   input in1, in2;
81
   input in1, in2;
82
   output out;
82
   output out;
83
   assign out = ~(in1 || in2);
83
   assign out = ~(in1 | in2);
84
endmodule
84
endmodule
85
 
85
 
86
// module for a 4x1 multiplexer
86
// module for a 4x1 multiplexer
87
module Mux4 (a, b, c, d, op, out);
87
module Mux4 (a, b, c, d, op, out);
88
   input a, b, c, d;
88
   input a, b, c, d;
Line 137... Line 137...
137
   OR_4 or1 (f1, f2, f3, f4, sum);
137
   OR_4 or1 (f1, f2, f3, f4, sum);
138
 
138
 
139
endmodule
139
endmodule
140
 
140
 
141
//module a 1-bit adder
141
//module a 1-bit adder
142
module OneBitAdder (carryIn, a, b, carryOut, sum);
142
module OneBitAdder (carryIn, a, b, carryOut, result);
143
   input carryIn, a, b;
143
   input carryIn, a, b;
144
   output carryOut, sum;
144
   output carryOut, result;
145
 
145
 
146
   Sum sum (carryIn, a, b, sum);
146
   Sum sum (carryIn, a, b, result);
147
   CarryOut cout (carryIn, a, b, carryOut);
147
   CarryOut cout (carryIn, a, b, carryOut);
148
endmodule
148
endmodule
149
 
149
 
150
 
-
 
151
// module for the 1-bit ALU
150
// module for the 1-bit ALU
152
module ALU1 (a, b, cin, op, cout, result);
151
module ALU1 (a, b, cin, op, cout, result);
153
    input a, b, cin;
152
    input a, b, cin;
154
    input[0:1] op; /* 00=AND, 01=OR, 10=ADD, 11=SUB */
153
    input[0:1] op; /* 00=AND, 01=OR, 10=ADD, 11=ADD A+~B */
155
    output cout, result;
154
    output cout, result;
156
 
155
 
157
    wire w_or, w_and, w_binv, w_add_out, w_sub_out;
156
    wire w_or, w_and, w_binv, w_add_out, w_sub_out;
158
 
157
 
159
    /* AND, OP=00 */
158
    /* AND, OP=00 */
Line 161... Line 160...
161
 
160
 
162
    /* OR, OP=01 */
161
    /* OR, OP=01 */
163
    AND_2 and1 (a, b, w_and);
162
    AND_2 and1 (a, b, w_and);
164
 
163
 
165
    /* ADD, OP=10 */
164
    /* ADD, OP=10 */
166
    OneBitAdder add (cin, a, b, cout, w_add_out);
-
 
167
 
-
 
168
    /* SUB, OP=11 */
165
    /* SUB, OP=11 */
169
    NOT_1 not1 (b, w_binv);
166
    NOT_1 not1 (b, w_binv);
-
 
167
    Mux2 m_binv (b, w_binv, op[1], w_adder_b);
-
 
168
    
170
    OneBitAdder sub (cin, a, w_binv, cout, w_sub_out);
169
    OneBitAdder add (cin, a, w_adder_b, cout, w_add_out);
171
 
170
    
172
    /* MUX the output together */
171
    /* MUX the output together */
173
    Mux4 resMux (w_and, w_or, w_add_out, w_sub_out, op, result);
172
    Mux4 resMux (w_and, w_or, w_add_out, w_add_out, op, result);
174
 
173
 
175
endmodule
174
endmodule
176
 
175
 
177
//Test bench for 1-bit ALU
-
 
178
module ALU1test;
-
 
179
        reg a, b, cin;
-
 
180
        reg [0:1]op;
-
 
181
        wire cout, result;
-
 
182
 
176
/*
183
initial begin
-
 
184
    a=0; b=0;op=00;cin=0;
-
 
185
    #1 a=0; b=1;
-
 
186
    #1 a=1; b=0;
-
 
187
    #1 a=1; b=1;
-
 
188
    #1 a=0; b=0; op=01;
-
 
189
    #1 a=0; b=1;
-
 
190
    #1 a=1; b=0;
177
module test_oba;
191
    #1 a=1; b=1;
-
 
192
    #1 a=0; b=0; op=10;
-
 
193
    #1 a=0; b=1;
-
 
194
    #1 a=1; b=0;
-
 
195
    #1 a=1; b=1;
-
 
196
    #1 a=0; b=0; cin=1;
-
 
197
    #1 a=0; b=1;
-
 
198
    #1 a=1; b=0;
-
 
199
    #1 a=1; b=1;
-
 
200
end
-
 
201
 
-
 
202
initial begin
-
 
203
   $display( "time     A     B    op     cin   cout result  ");
-
 
204
   $monitor("time=%0d a=%b  b=%b  op=%b  cin=%b  cout=%b, result=%b",
-
 
205
              $time, a, b, op, cin, cout, result);
-
 
206
end
-
 
207
 
178
 
-
 
179
    reg a, b, cin;
-
 
180
    wire w_cout, w_result;
-
 
181
 
-
 
182
    OneBitAdder oba (cin, a, b, w_cout, w_result);
-
 
183
 
-
 
184
    initial begin
-
 
185
        $monitor ("time=%0d a=%b b=%b cin=%b cout=%b result=%b",
-
 
186
            $time, a, b, cin, w_cout, w_result);
-
 
187
    end
-
 
188
 
-
 
189
    initial begin
-
 
190
           a=0; b=0; cin=0;
-
 
191
        #1 a=0; b=0; cin=1;
-
 
192
        #1 a=0; b=1; cin=0;
-
 
193
        #1 a=0; b=1; cin=1;
-
 
194
        #1 a=1; b=0; cin=0;
-
 
195
        #1 a=1; b=0; cin=1;
-
 
196
        #1 a=1; b=1; cin=0;
-
 
197
        #1 a=1; b=1; cin=1;
-
 
198
    end
-
 
199
 
-
 
200
endmodule
-
 
201
*/
-
 
202
/*
-
 
203
module test_alu1;
-
 
204
 
-
 
205
    reg a, b, cin;
-
 
206
    reg[0:1] op;
-
 
207
    wire cout, result;
-
 
208
 
208
   ALU1 g1(a,b,cin,op,cout,result);
209
    ALU1 alu1 (a, b, cin, op, cout, result);
-
 
210
 
-
 
211
    initial begin
-
 
212
        $monitor ("time=%0d a=%b b=%b op=%b cin=%b cout=%b result=%b",
-
 
213
            $time, a, b, op, cin, cout, result);
-
 
214
    end
-
 
215
 
-
 
216
    initial begin
-
 
217
           a=0; b=0; cin=0; op=00; $display; $display("AND");
-
 
218
        #1 a=0; b=0; cin=1; op=00;
-
 
219
        #1 a=0; b=1; cin=0; op=00;
-
 
220
        #1 a=0; b=1; cin=1; op=00;
-
 
221
        #1 a=1; b=0; cin=0; op=00;
-
 
222
        #1 a=1; b=0; cin=1; op=00;
-
 
223
        #1 a=1; b=1; cin=0; op=00;
-
 
224
        #1 a=1; b=1; cin=1; op=00;
-
 
225
 
-
 
226
        #1 a=0; b=0; cin=0; op=01; $display; $display ("OR");
-
 
227
        #1 a=0; b=0; cin=1; op=01;
-
 
228
        #1 a=0; b=1; cin=0; op=01;
-
 
229
        #1 a=0; b=1; cin=1; op=01;
-
 
230
        #1 a=1; b=0; cin=0; op=01;
-
 
231
        #1 a=1; b=0; cin=1; op=01;
-
 
232
        #1 a=1; b=1; cin=0; op=01;
-
 
233
        #1 a=1; b=1; cin=1; op=01;
-
 
234
 
-
 
235
        #1 a=0; b=0; cin=0; op=10; $display; $display ("ADD");
-
 
236
        #1 a=0; b=0; cin=1; op=10;
-
 
237
        #1 a=0; b=1; cin=0; op=10;
-
 
238
        #1 a=0; b=1; cin=1; op=10;
-
 
239
        #1 a=1; b=0; cin=0; op=10;
-
 
240
        #1 a=1; b=0; cin=1; op=10;
-
 
241
        #1 a=1; b=1; cin=0; op=10;
-
 
242
        #1 a=1; b=1; cin=1; op=10;
-
 
243
 
-
 
244
        #1 a=0; b=0; cin=0; op=11; $display; $display ("SUB");
-
 
245
        #1 a=0; b=0; cin=1; op=11;
-
 
246
        #1 a=0; b=1; cin=0; op=11;
-
 
247
        #1 a=0; b=1; cin=1; op=11;
-
 
248
        #1 a=1; b=0; cin=0; op=11;
-
 
249
        #1 a=1; b=0; cin=1; op=11;
-
 
250
        #1 a=1; b=1; cin=0; op=11;
-
 
251
        #1 a=1; b=1; cin=1; op=11;
-
 
252
    end
209
 
253
 
210
endmodule
254
endmodule
-
 
255
*/