0%

HDLBits-day3

Gates4

Description

Build a combinational circuit with four inputs, in[3:0].

There are 3 outputs:

out_and: output of a 4-input AND gate.
out_or: output of a 4-input OR gate.
out_xor: output of a 4-input XOR gate.
To review the AND, OR, and XOR operators, see andgate, norgate, and xnorgate.

Module Declaration

module top_module( 
    input [3:0] in,
    output out_and,
    output out_or,
    output out_xor
);

Answer

module top_module( 
    input [3:0] in,
    output out_and,
    output out_or,
    output out_xor
);
    assign out_and = in[3]&in[2]&in[1]&in[0]; //多位与
    assign out_or = in[3]|in[2]|in[1]|in[0];  //多位或
    assign out_xor = ^in;  //多位异或
endmodule

1664504182.jpg

异或运算⊕:两个逻辑变量相异为1,相同为0,$ F = A⊕B = A·B’ + A’· B$。

多异或连续运算:将前两个数的运算结果,与第三个数继续运算;再将结果与第四个运算;直到最后得出结果,其中的每一步都要按照相应运算的规则进行。

1⊕0⊕1⊕1⊕1=0
1⊕0⊕1⊕0⊕1=1
0⊕0⊕1⊕0=1
0⊕1⊕1⊕0=0
  1. 若含“奇数”个“真命题(1)”,则结果为“真(1)”;
  2. 若含“偶数”个“真命题(1)”,则结果为“假(0)”;(注:零个也是偶数个)

同或运算⊙:两个逻辑变量相异为0,相同为1,$ F = A⊙B = A·B + A’· B’ $。
多同或连续运算:$ a ⊙ 1 = a; a⊙ 0⊙ 0 = a $ 。同或运算就是观察输入中0的个数。奇数个0则结果为0,偶数个0则结果为1。

归约操作

  1. 归约与(&):全部是一。
  2. 归约与非(~&):有一个零。
  3. 归约或(|):有一个一。
  4. 归约或非(~|):全部是零。
  5. 归约异或(^):一的个数是奇数。
  6. 归约同或(~^):一的个数是偶数。
    1
    2
    3
    4
    A = 4'b1010 ;
    &A ; //结果为 1 & 0 & 1 & 0 = 1'b0,可用来判断变量A是否全1
    ~|A ; //结果为 ~(1 | 0 | 1 | 0) = 1'b0, 可用来判断变量A是否为全0
    ^A ; //结果为 1 ^ 0 ^ 1 ^ 0 = 1'b0

参考资料

  1. 多异或”和“多同或”连续运算问题
  2. verilog 归约操作符