Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
207 ira 1
/**
2
 * Support gates for CS365 Project #1.
3
 * Supplied by Prof. L. Yang
4
 */
5
 
6
//Basic unit: 2-input and gate
7
module AND_2 (in1, in2, out);
8
   input in1, in2;
9
   output out;
10
   assign out = in1 & in2;
11
endmodule
12
 
13
//Basic unit: 2-input or gate
14
module OR_2 (in1, in2, out);
15
   input in1, in2;
16
   output out;
17
   assign out = in1 | in2;
18
endmodule
19
 
20
//Basic unit: 3-input or gate
21
module OR_3(in1, in2, in3, out);
22
   input in1, in2, in3;
23
   output out;
24
   assign out = in1 | in2 | in3;
25
endmodule
26
 
27
//Basic unit: 3-input and gate
28
module AND_3 (in1, in2, in3, out);
29
   input in1, in2, in3;
30
   output out;
31
   assign out = in1 & in2 & in3;
32
endmodule
33
 
34
//Basic unit: 4-input or gate
35
module OR_4 (in1, in2, in3, in4, out);
36
   input in1, in2, in3, in4;
37
   output out;
38
   assign out = in1 | in2 | in3 | in4;
39
endmodule
40
 
41
//Basic unit: Invert
42
module NOT_1 (in, out);
43
   input in;
44
   output out;
45
   assign out = ~in;
46
endmodule
47
 
48
//Basic unit: module for a 2x1 multiplexer
49
module Mux2 (a, b, op, out);
50
   input a, b, op;
51
   output out;
52
   wire w1, w2, w3;
53
 
54
   NOT_1 not1 (op, w1);
55
 
56
   AND_2 and2 (a, w1, w2);
57
   AND_2 and1 (b, op, w3);
58
 
59
   OR_2 or1 (w2, w3, out);
60
endmodule
61
 
62
//basic unit: 3-input NAND gate
63
module NAND_3 (in1, in2, in3, out);
64
  input in1, in2, in3;
65
  output out;
66
  assign out = ~(in1 & in2 & in3);
67
endmodule
68
 
69
//basic unit: 4-input NAND gate
70
module NAND_4 (in1, in2, in3, in4, out);
71
  input in1, in2, in3, in4;
72
  output out;
73
  assign out = ~(in1 & in2 & in3 & in4);
74
endmodule
75
 
76
// basic unit: 2-input NAND gate
77
module NAND_2 (in1, in2, out);
78
   input in1, in2;
79
   output out;
80
   assign out = ~(in1 & in2);
81
endmodule
82
 
83
// basic unit: 2-input NOR gate
84
module NOR_2 ( in1, in2, out);
85
   input in1, in2;
86
   output out;
87
   assign out = ~(in1 | in2);
88
endmodule
89
 
90
// module for a 4x1 multiplexer
91
module Mux4 (a, b, c, d, op, out);
92
   input a, b, c, d;
93
   input [0:1]op;
94
   output out;
95
   wire w1, w2, f1, f2, f3, f4;
96
 
97
   //instantiate two NOT gates
98
   NOT_1 not1 (op[0:0], w1);
99
   NOT_1 not2 (op[1:1], w2);
100
 
101
   //instantiate four 3-input NAND gates
102
   NAND_3 nand3_1 (a, w1, w2, f1);
103
   NAND_3 nand3_2 (b, w1, op[1:1], f2);
104
   NAND_3 nand3_3 (c, op[0:0], w2, f3);
105
   NAND_3 nand3_4 (d, op[0:0], op[1:1], f4);
106
 
107
   //instantiate one 4-input NAND gate
108
   NAND_4 nand4_1 (f1, f2, f3, f4, out);
109
endmodule
110
 
111
// CarryOut module for the 1-bit Adder
112
//cout = cin . a + cin . b + a . b
113
module CarryOut (carryIn, a, b, carryOut);
114
   input carryIn, a, b;
115
   output carryOut;
116
 
117
   wire w1, w2, w3;
118
 
119
   AND_2  and1 (carryIn, a, w1);
120
   AND_2  and2 (carryIn, b, w2);
121
   AND_2  and3 (a, b, w3);
122
 
123
   OR_3   or1 (w1, w2, w3, carryOut);
124
endmodule
125
 
126
//Sum module for 1-bit ALU
127
module Sum (carryIn, a, b, sum);
128
   input a, b, carryIn;
129
   wire w1, w2, w3, f1, f2, f3, f4;
130
   output sum;
131
 
132
   NOT_1 not1 (carryIn, w1);
133
   NOT_1 not2 (a, w2);
134
   NOT_1 not3 (b, w3);
135
 
136
   AND_3 and1 (a, w3, w1, f1);
137
   AND_3 and2 (w2, b, w1, f2);
138
   AND_3 and3 (w2, w3, carryIn, f3);
139
   AND_3 and4 (a, b, carryIn, f4);
140
 
141
   OR_4 or1 (f1, f2, f3, f4, sum);
142
 
143
endmodule
144
 
145
//module a 1-bit adder
146
module OneBitAdder (carryIn, a, b, carryOut, result);
147
   input carryIn, a, b;
148
   output carryOut, result;
149
 
150
   Sum sum (carryIn, a, b, result);
151
   CarryOut cout (carryIn, a, b, carryOut);
152
endmodule
153