赞
踩
爱人,待周爱人,而后为爱人。不爱人,不待周不爱人《墨子.小取》
与门和或门的逻辑可以同过墨子的兼爱思想来理解。
墨子曰:“爱人,待周爱人,而后为爱人”
就是说,如果爱全部周边的人,则算是一位爱人的仁者。
如果用1
来代表爱人
,那输入都得是1( 待周爱人)才能使 输出是1(而后为爱人)。这不就是一个与门吗?
输入1 | 输入2 | 输出 |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
或门也可以同过墨子的兼爱思想来理解。这里是反例。
墨子曰:“不爱人,不待周不爱人”
这里就是说,如果周边有任何一个不爱的人,那就不能算是一位爱人的仁者了。
相反,如果用1
来代表不爱人
,那如果其中有一个输入是1(不待周),那输出就是1 (不爱人)。这不就是一个或门吗?
输入1 | 输入2 | 输出 |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
在verilog,有三个层面的硬件的模型:行为,门,和电路极。
module and2(input jia, input yi, output bing);
assign bing = jia & yi;
endmodule
module and2(input jia, input yi, output bing);
and(bing, jia, yi);
endmodule
要注意,因为电路里,电极刚好是相反的,所以要实现一个与门(用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
下一节会讨论其它的电路极实现方法。。
module or2(input jia, input yi, output bing);
assign bing = jia | yi;
endmodule
module and2(input jia, input yi, output bing);
or(bing, jia, yi);
endmodule
要注意,因为电路里,电极刚好是相反的,所以要实现一个与门(用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
下一节会讨论其它的电路极实现方法。。
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
liu2333hui@liu2333hui-PC:~/verilog/nihao$ epicsim or2.v
jiayi=00,bing=0
jiayi=01,bing=1
jiayi=10,bing=1
jiayi=11,bing=1
与或门也可以联合非门,所为学而实习之不亦乐乎?
。
module nand2(input jia, input yi, output bing);
assign bing = ~(jia & yi);
endmodule
module nand2(input jia, input yi, output bing);
nand(bing, jia, yi);
endmodule
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
module nor2(input jia, input yi, output bing);
assign bing = ~(jia | yi);
endmodule
module nor2(input jia, input yi, output bing);
nor(bing, jia, yi);
endmodule
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
其实,一个门可以很容易的转变成其它的门。譬如,
看看逻辑,
yi = ~jia = ~(jia & jia)
所以,
module not2(input jia, output yi);
nand(yi, jia, jia);
// assign yi = ~(jia & jia);
endmodule
这个恐怕不可以了,因为非门只有一个输入的口。
看看逻辑,
bing = jia & yi = ~(~(jia & yi));
这个就是一个与非门和一个非门(可用与非门来实现)。
module and2(input jia, input yi, output bing);
wire zhong;
nand(zhong, jia, yi);
nand(bing, zhong, zhong);
// assign bing = ~(~(jia & yi));
endmodule
这个恐怕不可以了,因为没有转非的能力。
看看逻辑,
bing = jia | yi = ~(~(jia | yi)) = ~(~jia & ~yi);
不想信的可以去自己的证明一下。
所以,输出是从一个与非门来的,但是输入是带有两个非门。
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
这个恐怕也不可以了,因为没有转非的能力。
到这里,就可以来解一些有意思的关于非与门面试题了。
请用多个2-输入的与非来实现一个4-输入的与非门?
答:
我们想实现的功能是以下:
out = ~(a & b & c & d );
可以进行推测,
e = a & b = ~(~(a & b)) = not(nand(a,b));
f = c & d = ~(~(c & d) = not(nand(c,d));
out = ~(e & f) = nand(e,f);
这就容易实现了,用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
效果如下,
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。