Subversion Repositories programming

Rev

Rev 194 | Rev 197 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
192 ira 1
 
2
//Basic unit: 2-input and gate
3
module AND_2 (in1, in2, out);
4
   input in1, in2;
194 ira 5
   output out;
192 ira 6
   assign out = in1 & in2;
7
endmodule
8
 
9
//Basic unit: 2-input or gate
10
module OR_2 (in1, in2, out);
11
   input in1, in2;
12
   output out;
195 ira 13
   assign out = in1 | in2;
192 ira 14
endmodule
15
 
16
//Basic unit: 3-input or gate
17
module OR_3(in1, in2, in3, out);
18
   input in1, in2, in3;
19
   output out;
195 ira 20
   assign out = in1 | in2 | in3;
192 ira 21
endmodule
22
 
23
//Basic unit: 3-input and gate
24
module AND_3 (in1, in2, in3, out);
25
   input in1, in2, in3;
194 ira 26
   output out;
192 ira 27
   assign out = in1 & in2 & in3;
28
endmodule
29
 
30
//Basic unit: 4-input or gate
31
module OR_4 (in1, in2, in3, in4, out);
32
   input in1, in2, in3, in4;
33
   output out;
195 ira 34
   assign out = in1 | in2 | in3 | in4;
192 ira 35
endmodule
36
 
37
//Basic unit: Invert
38
module NOT_1 (in, out);
39
   input in;
40
   output out;
41
   assign out = ~in;
42
endmodule
43
 
44
//Basic unit: module for a 2x1 multiplexer
45
module Mux2 (a, b, op, out);
46
   input a, b, op;
47
   output out;
48
   wire w1, w2, w3;
49
 
50
   NOT_1 not1 (op, w1);
194 ira 51
 
192 ira 52
   AND_2 and2 (a, w1, w2);
53
   AND_2 and1 (b, op, w3);
54
 
55
   OR_2 or1 (w2, w3, out);
56
endmodule
57
 
58
//basic unit: 3-input NAND gate
59
module NAND_3 (in1, in2, in3, out);
60
  input in1, in2, in3;
194 ira 61
  output out;
192 ira 62
  assign out = ~(in1 & in2 & in3);
63
endmodule
64
 
65
//basic unit: 4-input NAND gate
66
module NAND_4 (in1, in2, in3, in4, out);
67
  input in1, in2, in3, in4;
68
  output out;
69
  assign out = ~(in1 & in2 & in3 & in4);
70
endmodule
71
 
72
// basic unit: 2-input NAND gate
73
module NAND_2 (in1, in2, out);
74
   input in1, in2;
194 ira 75
   output out;
192 ira 76
   assign out = ~(in1 & in2);
77
endmodule
78
 
79
// basic unit: 2-input NOR gate
80
module NOR_2 ( in1, in2, out);
81
   input in1, in2;
82
   output out;
195 ira 83
   assign out = ~(in1 | in2);
192 ira 84
endmodule
85
 
86
// module for a 4x1 multiplexer
87
module Mux4 (a, b, c, d, op, out);
88
   input a, b, c, d;
89
   input [0:1]op;
90
   output out;
91
   wire w1, w2, f1, f2, f3, f4;
92
 
93
   //instantiate two NOT gates
194 ira 94
   NOT_1 not1 (op[0:0], w1);
192 ira 95
   NOT_1 not2 (op[1:1], w2);
96
 
97
   //instantiate four 3-input NAND gates
98
   NAND_3 nand3_1 (a, w1, w2, f1);
99
   NAND_3 nand3_2 (b, w1, op[1:1], f2);
100
   NAND_3 nand3_3 (c, op[0:0], w2, f3);
101
   NAND_3 nand3_4 (d, op[0:0], op[1:1], f4);
102
 
103
   //instantiate one 4-input NAND gate
104
   NAND_4 nand4_1 (f1, f2, f3, f4, out);
105
endmodule
106
 
107
// CarryOut module for the 1-bit Adder
108
//cout = cin . a + cin . b + a . b
109
module CarryOut (carryIn, a, b, carryOut);
110
   input carryIn, a, b;
111
   output carryOut;
112
 
113
   wire w1, w2, w3;
114
 
115
   AND_2  and1 (carryIn, a, w1);
116
   AND_2  and2 (carryIn, b, w2);
117
   AND_2  and3 (a, b, w3);
118
 
119
   OR_3   or1 (w1, w2, w3, carryOut);
120
endmodule
121
 
122
//Sum module for 1-bit ALU
123
module Sum (carryIn, a, b, sum);
124
   input a, b, carryIn;
125
   wire w1, w2, w3, f1, f2, f3, f4;
126
   output sum;
127
 
128
   NOT_1 not1 (carryIn, w1);
129
   NOT_1 not2 (a, w2);
130
   NOT_1 not3 (b, w3);
131
 
132
   AND_3 and1 (a, w3, w1, f1);
133
   AND_3 and2 (w2, b, w1, f2);
134
   AND_3 and3 (w2, w3, carryIn, f3);
135
   AND_3 and4 (a, b, carryIn, f4);
136
 
137
   OR_4 or1 (f1, f2, f3, f4, sum);
138
 
139
endmodule
140
 
141
//module a 1-bit adder
195 ira 142
module OneBitAdder (carryIn, a, b, carryOut, result);
192 ira 143
   input carryIn, a, b;
195 ira 144
   output carryOut, result;
192 ira 145
 
195 ira 146
   Sum sum (carryIn, a, b, result);
192 ira 147
   CarryOut cout (carryIn, a, b, carryOut);
148
endmodule
149
 
150
// module for the 1-bit ALU
194 ira 151
module ALU1 (a, b, cin, op, cout, result);
152
    input a, b, cin;
195 ira 153
    input[0:1] op; /* 00=AND, 01=OR, 10=ADD, 11=ADD A+~B */
194 ira 154
    output cout, result;
192 ira 155
 
194 ira 156
    wire w_or, w_and, w_binv, w_add_out, w_sub_out;
192 ira 157
 
194 ira 158
    /* AND, OP=00 */
159
    OR_2  or1  (a, b, w_or);
192 ira 160
 
194 ira 161
    /* OR, OP=01 */
162
    AND_2 and1 (a, b, w_and);
192 ira 163
 
194 ira 164
    /* ADD, OP=10 */
165
    /* SUB, OP=11 */
166
    NOT_1 not1 (b, w_binv);
195 ira 167
    Mux2 m_binv (b, w_binv, op[1], w_adder_b);
168
 
169
    OneBitAdder add (cin, a, w_adder_b, cout, w_add_out);
170
 
194 ira 171
    /* MUX the output together */
195 ira 172
    Mux4 resMux (w_and, w_or, w_add_out, w_add_out, op, result);
194 ira 173
 
192 ira 174
endmodule
175
 
195 ira 176
/*
177
module test_oba;
194 ira 178
 
195 ira 179
    reg a, b, cin;
180
    wire w_cout, w_result;
192 ira 181
 
195 ira 182
    OneBitAdder oba (cin, a, b, w_cout, w_result);
192 ira 183
 
195 ira 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
192 ira 188
 
195 ira 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
 
194 ira 200
endmodule
195 ira 201
*/
202
/*
203
module test_alu1;
204
 
205
    reg a, b, cin;
206
    reg[0:1] op;
207
    wire cout, result;
208
 
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
253
 
254
endmodule
255
*/