HDL_BITS 练习(六)

这篇博客介绍了Verilog语言在实现各种逻辑运算中的应用,包括条件选择(1conditional)、位操作(reduction)、与或非运算(gate100)、向量反转(vector[100])、计数(Popcount255)、加法器(adder100)以及BCD加法器(bcdadder100)。通过示例代码展示了如何用Verilog进行组合逻辑和时序逻辑的设计。

1 conditional

module top_module (
    input [7:0] a, b, c, d,
    output [7:0] min);//

    wire [7:0] min1,min2;
    // assign intermediate_result1 = compare? true: false;
    assign min1=(a<b)?a:b;//min1 = min(a,b)
    assign min2=(c<d)?c:d;//min2=min(c,d)
    assign min=(min1<min2)?min1:min2;
endmodule

2 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]

These are unary operators that have only one operand (similar to the NOT operators ! and ~).  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
 

3 gate 100

和上一篇差不多;

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

endmodule

4 vector[100]

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

5Popcount255

官网答案呢就下面的。跳过去

module top_module (
    input [254:0] in,
    output reg [7:0] out
);

    always @(*) begin    // Combinational always block
        out = 0;
        for (int i=0;i<255;i++)
            out = out + in[i];
    end
    
endmodule

6 adder 100

hint 里面推荐了generate语句

摘抄下其他blog的介绍

generate生成语句可以动态的生成verilog代码,当对矢量中的多个位进行 重复操作 时,或者当进行多个模块的实例引用的重复操作时,或者根据参数的定义来确定程序中是否应该包含某段Verilog代码的时候,使用生成语句能大大简化程序的编写过程。

       生成语句生成的实例范围,关键字generate-endgenerate用来指定该范围。生成实例可以是以下的一个或多个类型:

       (1)模块;(2)用户定义原语;(3)门级语句;(4)连续赋值语句;(5)initial和always块。

       generate语句有generate-for,generate-if,generate-case三种语句。

generate-for语句

(1) 必须有genvar关键字定义for语句的变量。

(2)for语句的内容必须加begin和end(即使就一句)。

(3)for语句必须有个名字。(begin 后)

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:adder100
                if(i!=0)
                assign {cout[i],sum[i]} = a[i]+b[i]+cout[i-1];
                else
                    assign{cout[i],sum[i]}=a[0]+b[0]+cin;
            end
        endgenerate     
endmodule

7 bcdadder100


 

源码来自:https://pan.quark.cn/s/fdd21a41d74f 正方教务管理系统成绩推送 简介 使用本项目前: 早晨睡醒看一遍教务系统、上厕所看一遍教务系统、刷牙看一遍教务系统、洗脸看一遍教务系统、吃早餐看一遍教务系统、吃午饭看一遍教务系统、睡午觉前看一遍教务系统、午觉醒来看一遍教务系统、出门前看一遍教务系统、吃晚饭看一遍教务系统、洗澡看一遍教务系统、睡觉之前看一遍教务系统 使用本项目后: 成绩更新后自动发通知到微信 以节省您宝贵的时间 测试环境 正方教务管理系统 版本 V8.0、V9.0 如果你的教务系统页面与下图所示的页面完全一致或几乎一致,则代表你可以使用本项目。 目前支持的功能 主要功能 每隔 30 分钟自动检测一次成绩是否有更新,若有更新,将通过微信推送及时通知用户。 相较于教务系统增加了哪些功能? 显示成绩提交时间,即成绩何时被录入教务系统。 显示成绩提交人姓名,即成绩由谁录入进教务系统。 成绩信息按时间降序排序,确保最新的成绩始终在最上方,提升用户查阅效率。 计算 计算百分制 对于没有分数仅有级别的成绩,例如”及格、良好、优秀“,可以强制显示数字分数。 显示未公布成绩的课程,即已选课但尚未出成绩的课程。 使用方法 Fork 本仓库 → 开启 工作流读写权限 → → → → → 添加 Secrets → → → → → → Name = Name,Secret = 例子 程序会自动填充 尾部的 ,因此你无需重复添加 对于部分教务系统,可能需要在 中添加 路径,如: 开启 Actions → → → 运行 程序 → → 若你的程序正常运行且未报错,那么在此之后,程序将会每隔 30 分钟自动检测一次成绩是否有更新 若你看不懂上述使用...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值