HDLBits(8)9.20

2 Verilog语言

2.5 更多特点

2.5.1 三元运算符

(condition ? if_true : if_false)

判断a,b,c,d中的最小值

module top_module (
    input [7:0] a, b, c, d,
    output [7:0] min);
    wire [7:0] min1;
    wire [7:0] min2;
    assign min1 = (a<b)?a:b;
    assign min2 = (c<d)?c:d;
    assign min = (min1<min2)?min1:min2;
endmodule

2.5.2 归约运算符

 对向量的每一位进行逻辑运算

& a[3:0]     // AND: a[3]&a[2]&a[1]&a[0]. Equivalent to (a[3:0] == 4'hf)
| b[3:0]     // OR:  b[3]|b[2]|b[1]|b[0]. Equivalent to (b[3:0] != 4'h0)
^ c[2:0]     // XOR: c[2]^c[1]^c[0]

创建电路,用于计算8位字节的窘校验位(这将向字节添加第9位)。我们将使用“偶数”奇偶校验,其中奇偶校验位只是所有 8 个数据位的 XOR

module top_module (
    input [7:0] in,
    output parity); 

    assign parity = ^in;
    
endmodule

奇偶校验是检验传输数据中1的个数,当然有奇数有偶数,,这时候就需要用我们的校验位了,通过检验位将传输1的个数变成奇数就是奇校验,变成偶数就是偶校验。比如:

8'b01100100   //原数据
9'b01100100_0 //奇校验
9'b01100100_1 //偶校验

2.5.3 归约运算符(Gates 100)

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

 

posted @ 2022-09-20 16:07  LhTian21  阅读(34)  评论(0)    收藏  举报