HDL—Verilog Language—More Verliog Features—Reduction operators

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值