HDLBits MoreVerilogFeatures部分题目练习

1Conditional

Given four unsigned numbers, find the minimum. 

module top_module (
    input [7:0] a, b, c, d,
    output [7:0] min);//
    wire [7:0] group1, group2;
    // assign intermediate_result1 = compare? true: false;
    assign group1 = (a < b) ? a : b;
    assign group2 = (c < d) ? c : d;
    assign min = (group1 < group2) ? group1 : group2;
endmodule

2Reduction

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.

奇偶校验,这里是偶校验

module top_module (
    input [7:0] in,
    output parity); 
	assign parity = ^in;
endmodule

3Gates100

module top_module( 
    input [99:0] in,
    output out_and,
    output out_or,
    output out_xor 
);
	assign out_and = &in;
    assign out_or = |in;
    assign out_xor = ^in;
endmodule

4Vector100r

100bit reverse

module top_module( 
    input [99:0] in,
    output [99:0] out
);
    // 使用 always 块进行连续赋值
always @(*) begin
    for(int i = 0; i < 100; i = i + 1) begin
        out[i] = in[99 - i];  // 反转输入向量的位
    end
end
endmodule

5Popcount255

统计位中1的个数

module top_module( 
    input [254:0] in,
    output [7:0] out );
always@(*)begin
    out = 8'd0; 
    for(int i=0; i<255; i++)begin
        out = out + in[i];
    end
end
endmodule

6Adder100i

Create a 100-bit binary ripple-carry adder by instantiating 100 full adders

通过实例化100个全加法器来创建一个100位二进制纹波进位加法器。

纹波进位:纹波进位加法器计算进位输出的延迟相当大,第二级加法器在第一级加法器完成之前不能开始计算其进位输出。

module top_module( 
    input [99:0] a, b,
    input cin,
    output [99:0] cout,
    output [99:0] sum );
    always@(*)begin
        {cout[0], sum[0]} = a[0] + b[0] + cin; 
        for(int i=1; i<100; i=i+1)begin
            {cout[i], sum[i]} = a[i] + b[i] + cout[i-1]; 
        end
    end
endmodule

7Bcdadd100

Instantiate 100 copies of bcd_fadd to create a 100-digit BCD ripple-carry adder. 

实例化100个bcd_fadd副本以创建一个100位的BCD波纹进位加法器。

索引的用法

sum[i<<2 +: 4]表示从i*4这位开始,向高数4位,选择这些位。

module top_module( 
    input [399:0] a, b,
    input cin,
    output cout,
    output [399:0] sum );
    wire [99:0] carry;
    assign cout = carry[99];
    always@(*)begin
        for(integer i=0; i<100; i=i+1)begin
            if(a[i<<2 +: 4] + b[i<<2 +: 4] + (i==0 ? cin : carry[i-1]) >= 5'd10)begin
                carry[i] = 1'b1;
                sum[i<<2 +: 4] = a[i<<2 +: 4] + b[i<<2 +: 4] + (i==0 ? cin : carry[i-1]) - 5'd10;
            end
            else
                begin
                    carry[i] = 1'b0;
                    sum[i<<2 +: 4] = a[i<<2 +: 4] + b[i<<2 +: 4] + (i==0 ? cin : carry[i-1]);
                end
        end
    end
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值