Reduction
您已经熟悉了两个值之间的按位操作,例如,a & b 或 a ^ b。有时候,您想创建一个宽门,它对一个向量的所有位进行操作,如(a [0] & a [1] & a [2] & a [3] ...) ,如果向量很长,就会变得很乏味。
归约运算符可以对矢量的位进行 AND、 OR 和 XOR,从而产生一位输出:
& a [3:0] //AND: a [3] & a [2] & a [1] & a [0] . 相当于(a [3:0] = = 4‘ hf)
| b [3:0] //OR: b [3] | b [2] | b [1] | b [0] . 相当于(b [3:0] ! = 4‘ h0)
^ c [2:0] //XOR: c [2] ^ c [1] ^ c [0]
这些一元运算符只有一个操作数(类似于 NOT 运算符!)!及 ~)。您还可以反转这些门的输出以创建 NAND、 NOR 和 XNOR 门,例如(~ & d [7:0])。
A Bit of Practice
Parity checking is often used as a simple method of detecting errors when transmitting data through an imperfect channel. Create a circuit that will compute a parity bit for a 8-bit byte (which will add a 9th bit to the byte). We will use "even" parity, where the parity bit is just the XOR of all 8 data bits.
奇偶校验通常被用来作为一种简单的检测错误的方法时,传输数据通过一个不完美的信道。创建一个电路,将计算一个8位字节的奇偶位(这将增加第9位的字节)。我们将使用“偶数”奇偶校验,其中奇偶位只是所有8个数据位的异或。
Module Declaration
module top_module ( input [7:0] in, output parity);
答案:
module top_module (
input [7:0] in,
output parity);
assign parity = ^in[7:0];
endmodule
按位异或就可以求奇偶个数了
建立一个有100个输入的组合电路,在[99:0]。
有3项产出:
- Out _ AND: 100输入 AND gate 的输出。
- Out _ OR: 100输入 OR 门的输出。
- Out _ XOR: 100输入异或门的输出。
Module Declaration
module top_module( input [99:0] in, output out_and, output out_or, output out_xor );
按照要求写一下就可以,很简单
答案:
module top_module(
input [99:0] in,
output out_and,
output out_or,
output out_xor
);
assign out_and = & in[99:0];
assign out_or = | in[99:0];
assign out_xor = ^ in[99:0];endmodule