当前位置: 首页 > news >正文

Verilog学习笔记-20250802

Verilog HDL学习

HDLBits刷题错误总结

  • always block里误用wire类型变量

  • 16-bits,每位都是1,误表示为{16{1}},应当表示为{16{1'b1}},事实上用'1更简单

  • 数组维度声明错误,比如,我想声明一个长度为8的chunks数组,数组里每个元素宽度为4位,

    • 错误示范
    wire [7:0] chunks [3:0];
    
    • 正确写法
    wire [3:0] chunks [7:0];
    
  • generate_for_loopfor循环的begin后容易忘记加:name_of_the_for_loop

HDLBits刷题新知

  • Use bitwise operators and part-select to do the entire calculation in one line of code.

    module top_module (input [3:0] in,output [2:0] out_both,output [3:1] out_any,output [3:0] out_different
    );assign out_any = in[3:1] | in[2:0];assign out_both = in[2:0] & in[3:1];assign out_different = in ^ {in[0], in[3:1]};	
    endmodule
    
  • 检查各bit是否相同可以用XOR^

  • Bit-slicing(Indexed vector part-select),切片,如:

    assign data_out = data_in[INDEX*4+3: INDEX*4];
    

    要求切片下标INDEX必须为静态常量表达式,不能是动态变量,如果需要通过变量进行批量选位操作(例如批量赋值、切片),则要采用generate_for_loop通过genvar静态展开;但是数组下标可以是动态变量

    Mux256to1v - HDLBits

    module top_module( input [1023:0] in,input [7:0] sel,output [3:0] out );wire [3:0] chunks [255:0];genvar i;generate for (i = 0; i < 256; i++) begin: loop_blockassign chunks[i] = in[i*4+3:i*4];endendgenerateassign out = chunks[sel];endmodule
    

    事实上,利用数组下标是动态变量,切片可以转化为拼接的数组,有更简洁的代码:

    module top_module( input [1023:0] in,input [7:0] sel,output [3:0] out );assign out = {in[sel*4+3], in[sel*4+2], in[sel*4+1], in[sel*4]};endmodule
    

    其实也并非不能用切片,但得满足特定的语法:

    module top_module( input [1023:0] in,input [7:0] sel,output [3:0] out );assign out = in[sel*4 +:4];//或者是://assign out = in[sel*4+3 -: 4];endmodule
    
  • assign sum = x + y;可以自动计算进位信号,比如为实现下图电路:

    Exams/m2014 q4j - HDLBits

    img

    可以编写如下简单的代码就能实现上述电路的功能,其中sum[4]就是自动进位信号。

    module top_module (input [3:0] x,input [3:0] y,output [4:0] sum
    );assign sum = x + y;endmodule
    

    但是,assign sum = {x + y};则会裁切掉”溢出“的高位,不会自动扩展加法进位,在此处不可取!

http://www.sczhlp.com/news/5485/

相关文章:

  • P8523 [IOI 2021] 位移寄存器 题解
  • 情绪
  • 8-4
  • GraphPad Prism 10 mac+win安装包
  • Flutter plugin开发小知识之:ActivityAware 详解
  • Motion 5 for mac(视频后期特效)v5.10中文版
  • 完整教程:【加解密与C】HASH系列(四)SHA-3
  • 8月4日总结
  • 22222222
  • 关于本人在博客园平台停更的公告
  • 8/4
  • Pwn2Own Automotive 2025 第二日战报:38.5万美元奖金与16个零日漏洞
  • 图分割算法荣获SC21时间检验奖
  • P6246 [IOI 2000] 邮局
  • k8s学习2
  • K8S学习(day1)
  • 单调栈
  • MySQL 常用SQL语句
  • 试卷最后一小问 sillation 造题的几分钟 | CTOI 成立两周年
  • 7-26 小测
  • 第二十一篇
  • 03端口占用:Windows电脑上的端口8080被占用,怎么释放出来?
  • 硬件工程师(笔试)
  • 5G、6G系统算法标准实习生
  • 硬件工程师(面试)
  • blender
  • Jupyter notebook
  • F. 1-1-1, Free Tree!
  • base64
  • 一条指令拿到Cocos Creator jsc加密密钥