HDLBits个人刷题详解合集5-Verilog Language-more verilog features-HDBits题目分析

Conditional

题目描述:条件语句(condition ? if_true : if_false)

给定4个无符号数,求出最小值;使用条件语句两两求出最小电路,最后再比较求出4路中的最小电路。

module top_module (

    input [7:0] a, b, c, d,

    output [7:0] min);

    // assign intermediate_result1 = compare? true: false;

    wire [7:0] w1,w2;//定义2个中间变量

    assign w1 = (a>b) ? b : a;

    assign w2 = (c>d) ? d : c;

    assign min = (w1>w2) ? w2 : w1;

endmodule

Reduction

缩位操作

双目运算符和单目运算符

& 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

Gates100

There are 3 outputs:

  • out_and: output of a 100-input AND gate.
  • out_or: output of a 100-input OR gate.
  • out_xor: output of a 100-input XOR gate.

代码:

module top_module(

    input [99:0] in,

    output out_and,

    output out_or,

    output out_xor

);

assign out_and = ∈

    assign out_or = |in;

    assign out_xor = ^in;

endmodule

Vector100r

题目描述:把输入的每一位翻转。

代码:

module top_module(

    input [99:0] in,

    output [99:0] out

);

integer i;

    always @(*)

        begin

            for(i=0;i<100;i++)

                out[i] = in[99-i];

        end

endmodule

Popcount255

“人口计数”电路对输入向量中的“1”数进行计数。为255 位输入向量计数电路。

代码如下:

module top_module(

    input [254:0] in,

    output [7:0] out );

    int i;

    always @(*)

        begin

            out = 0;

            for(i=0; i<255; i++)

                out = out + in[i];

        end

endmodule

Adder100i

通过实例化 100 个完整加法器来创建 100 位二进制波纹携带加法器。加法器将两个 100 位数字和一个随身携带数字相加,以生成 100 位总和并进行执行。为了鼓励您实际实例化完整加法器,还要输出波纹携带加法器中每个完整加法器的携带。cout[99]是最后一个完整加法器的最终传导,也是你通常看到的外带。

代码如下:

module top_module(

    input [99:0] a, b,

    input cin,

    output [99:0] cout,

    output [99:0] sum );

    generate

        genvar i;

        //只能使用在这个模块中的变量

        for(i=0; i<100; i++)begin:adder//循环名称adder

            if(i == 0)begin

                fulladder fulladder_inst(a[i],b[i],cin,cout[i],sum[i]);//位置式的实例化

            end

            else begin

                

                fulladder fullader_inst(a[i],b[i],cout[i-1],cout[i],sum[i]);

            end

        end

     endgenerate 

endmodule

//设置一个新的模块,全值累加器,3个输入两个输出

module fulladder(

    input a, b, cin,

    output cout, sum);

    

    assign {cout,sum} = a + b + cin;

    //使用行为级描述,cout与sum拼接,

    //3个输入累加的高位赋值给cout,低位赋值给sum

endmodule

Bcdadd100

题目描述:

您将获得一个名为bcd_fadd的BCD单位加法器,该加法器将两个BCD数字和结转相加,并产生总和和结转。

module bcd_fadd (

    input [3:0] a,

    input [3:0] b,

    input     cin,

    output   cout,

    output [3:0] sum );

实例化 100 个bcd_fadd副本以创建 100 位 BCD 波纹携带加法器。您的加法器应添加两个 100 位 BCD 编号(打包到 400 位向量中)和一个随入,以生成 100 位总和并执行。

代码如下:

module top_module(

    input [399:0] a, b,

    input cin,

    output cout,

    output [399:0] sum );

    wire [100:0] cin_mid;//设计一个中间变量

    assign cin_mid[0] = cin;//就第一位是直接输入赋值的

    generate

        genvar i;

        for(i=0; i<100; i++) begin:bcd_fadd_loop

            //使用名字式实例化的方式

            bcd_fadd bcd_fadd_inst (

                .a(a[4*i+3:4*i]),

                .b(b[4*i+3:4*i]),

                .cin(cin_mid[i]),

                .cout(cin_mid[i+1]),

                .sum(sum[4*i+3:4*i])

            );

        end

    endgenerate

    assign cout = cin_mid[100];

endmodule

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值