当前位置:   article > 正文

【Verilog - 组合逻辑 - 基础3】4. 与或门-1

与或门

爱人,待周爱人,而后为爱人。不爱人,不待周不爱人《墨子.小取》

1. 介绍

1.1 与门介绍

与门和或门的逻辑可以同过墨子的兼爱思想来理解。

墨子曰:“爱人,待周爱人,而后为爱人”

就是说,如果爱全部周边的人,则算是一位爱人的仁者。

如果用1来代表爱人,那输入都得是1( 待周爱人)才能使 输出是1(而后为爱人)。这不就是一个与门吗?

输入1输入2输出
000
010
100
111

1.2 或门介绍

或门也可以同过墨子的兼爱思想来理解。这里是反例。

墨子曰:“不爱人,不待周不爱人”

这里就是说,如果周边有任何一个不爱的人,那就不能算是一位爱人的仁者了。

相反,如果用1来代表不爱人,那如果其中有一个输入是1(不待周),那输出就是1 (不爱人)。这不就是一个或门吗?

输入1输入2输出
000
011
101
111

2.0 与门在verilog的建模

在verilog,有三个层面的硬件的模型:行为,门,和电路极。

2.1 与门-行为极模型

module and2(input jia, input yi, output bing);
	assign bing = jia & yi;
endmodule
  • 1
  • 2
  • 3

2.2 与门-门极模型

module and2(input jia, input yi, output bing);
	and(bing, jia, yi);
endmodule
  • 1
  • 2
  • 3

2.3 与门-电路极模型

要注意,因为电路里,电极刚好是相反的,所以要实现一个与门(用cmos的话)就要再加一个非门,

module and2(input jia, input yi, output bing);
	wire yu_fei;
	wire zhong;

	//yu_fei men
	pmos(yu_fei, 1, jia ); pmos(yu_fei,1,yi);
	nmos(zhong, 0, jia);nmos(yu_fei, zhong, yi);

	//fei men
	pmos(bing, 1, yu_fei);
	nmos(bing, 0, yu_fei);
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

下一节会讨论其它的电路极实现方法。。

3.0 或门在verilog的建模

3.1 或门-行为极模型

module or2(input jia, input yi, output bing);
	assign bing = jia | yi;
endmodule
  • 1
  • 2
  • 3

3.2 或门-门极模型

module and2(input jia, input yi, output bing);
	or(bing, jia, yi);
endmodule
  • 1
  • 2
  • 3

3.3 或门-电路极模型

要注意,因为电路里,电极刚好是相反的,所以要实现一个与门(用cmos的话)就要再加一个非门,

module or2(input jia, input yi, output bing);
	wire huo_fei;
	wire zhong;

	//huo_fei men
	pmos(zhong, 1, jia ); pmos(huo_fei,zhong,yi);
	nmos(huo_fei, 0, jia); nmos(huo_fei, 0, yi);

	//fei men
	pmos(bing, 1, huo_fei);
	nmos(bing, 0, huo_fei);
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

下一节会讨论其它的电路极实现方法。。

3.4 测试结果

module or2(input jia, input yi, output bing);
        wire huo_fei;
        wire zhong;

        //huo_fei men
        pmos(zhong, 1, jia ); pmos(huo_fei,zhong,yi);
        nmos(huo_fei, 0, jia); nmos(huo_fei, 0, yi);

        //fei men
        pmos(bing, 1, huo_fei);
        nmos(bing, 0, huo_fei);
endmodule

module ceshi;
	reg jia,yi;
	wire bing;
	or2 dut(jia, yi, bing);
	integer i;
	initial begin
	        for (i = 0; i < 4; i++) begin
	                {jia,yi} = i;
	                #1;
	                $display("jiayi=%b%b,bing=%b", jia,yi,bing);
	        end
	end
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
liu2333hui@liu2333hui-PC:~/verilog/nihao$ epicsim or2.v
jiayi=00,bing=0
jiayi=01,bing=1
jiayi=10,bing=1
jiayi=11,bing=1
  • 1
  • 2
  • 3
  • 4
  • 5

4.0 与非门

与或门也可以联合非门,所为学而实习之不亦乐乎?

4.1 与非门

module nand2(input jia, input yi, output bing);
	assign bing = ~(jia & yi);
endmodule
  • 1
  • 2
  • 3

4.2 与非门-门极模型

module nand2(input jia, input yi, output bing);
	nand(bing, jia, yi);
endmodule
  • 1
  • 2
  • 3

4.3 与非门-电路极模型

module nand2(input jia, input yi, output bing);
	wire yu_fei;
	wire zhong;

	//yu_fei men
	pmos(yu_fei, 1, jia ); pmos(yu_fei,1,yi);
	nmos(zhong, 0, jia);nmos(yu_fei, zhong, yi);

	assign bing = yu_fei;
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5.0 或非门

5.1 或非门-行为极模型

module nor2(input jia, input yi, output bing);
	assign bing = ~(jia | yi);
endmodule
  • 1
  • 2
  • 3

5.2 或非门-门极模型

module nor2(input jia, input yi, output bing);
	nor(bing, jia, yi);
endmodule
  • 1
  • 2
  • 3

5.3 与门-电路极模型

module nor2(input jia, input yi, output bing);
        wire huo_fei;
        wire zhong;

        //huo_fei men
        pmos(zhong, 1, jia ); pmos(huo_fei,zhong,yi);
        nmos(huo_fei, 0, jia); nmos(huo_fei, 0, yi);
		
		assign bing = huo_fei;
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

6.0 与非门和非门,与门和或门的关系

其实,一个门可以很容易的转变成其它的门。譬如,

6.1.1 与非门转非门

看看逻辑,

yi = ~jia = ~(jia & jia)
  • 1

所以,

module not2(input jia, output yi);
	nand(yi, jia, jia);
	// assign yi = ~(jia & jia);
endmodule
  • 1
  • 2
  • 3
  • 4

6.1.1 非门转与非门

这个恐怕不可以了,因为非门只有一个输入的口。

6.2.1 与非门转与门

看看逻辑,

bing = jia & yi = ~(~(jia & yi));
  • 1

这个就是一个与非门和一个非门(可用与非门来实现)。

module and2(input jia, input yi, output bing);
	wire zhong;
	nand(zhong, jia, yi);
	nand(bing, zhong, zhong);
	// assign bing = ~(~(jia & yi));
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

6.2.2 与门转与非门

这个恐怕不可以了,因为没有转非的能力。

6.3.1 与非门转或门

看看逻辑,

bing = jia | yi = ~(~(jia | yi)) = ~(~jia & ~yi);
  • 1

不想信的可以去自己的证明一下。
所以,输出是从一个与非门来的,但是输入是带有两个非门。

module or2(input jia, input yi, output bing);
	wire fei_jia, fei_yi;
	nand(fei_jia, jia, jia);
	nand(fei_yi, yi, yi);
	nand(bing, fei_jia, fei_yi);
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

6.3.2 或门转与非门

这个恐怕也不可以了,因为没有转非的能力。

7.0 真实面试题

到这里,就可以来解一些有意思的关于非与门面试题了。

请用多个2-输入的与非来实现一个4-输入的与非门?

答:
我们想实现的功能是以下:

out = ~(a & b & c & d );
  • 1

可以进行推测,

e = a & b = ~(~(a & b)) = not(nand(a,b));
f = c & d = ~(~(c & d) = not(nand(c,d));
out = ~(e & f) = nand(e,f);
  • 1
  • 2
  • 3

这就容易实现了,用verilog的门极建模,

module nand4_2(input a, input b, input c, input d, output out);
	wire e,f;
	wire g, h;
	nand(e, a, b);
	nand(f, c, d);
	nand(g, e, e);
	nand(h, f, f);
	nand(out, g, h);
endmodule

module nand4(input a, input b, input c, input d, output out);
	assign out = ~(a&b&c&d);
endmodule

module nand4_tb;
	reg a,b,c,d;
	wire out,out2;
	nand4 dut(a,b,c,d,out);
	nand4_2 dut1(a,b,c,d,out2);
	
	integer i;
	initial begin
		for (i = 0; i < 16; i++)begin
			{a, b, c, d} = i;
			#1;
			$display("abcd = %b%b%b%b, out = %b, out2 = %b", a,b,c,d,out, out2);
		end
	end
endmodule
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

效果如下,

liu2333hui@liu2333hui-PC:~/verilog/nihao$ epicsim nand.v
abcd = 0000, out = 1, out2 = 1
abcd = 0001, out = 1, out2 = 1
abcd = 0010, out = 1, out2 = 1
abcd = 0011, out = 1, out2 = 1
abcd = 0100, out = 1, out2 = 1
abcd = 0101, out = 1, out2 = 1
abcd = 0110, out = 1, out2 = 1
abcd = 0111, out = 1, out2 = 1
abcd = 1000, out = 1, out2 = 1
abcd = 1001, out = 1, out2 = 1
abcd = 1010, out = 1, out2 = 1
abcd = 1011, out = 1, out2 = 1
abcd = 1100, out = 1, out2 = 1
abcd = 1101, out = 1, out2 = 1
abcd = 1110, out = 1, out2 = 1
abcd = 1111, out = 0, out2 = 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/533752
推荐阅读
相关标签
  

闽ICP备14008679号