【HDLBits】Procedures

  • I. Always blocks(combinational)
    • 1.代码编写
    • 2.提交结果
    • 3.题目分析
  • II. Always blocks(clocked)
    • 1.代码编写
    • 2.提交结果
    • 3.题目分析
  • III. Always if (if statement)
    • 1.代码编写
    • 2.提交结果
    • 3.题目分析
  • IV. Always if2 (if statement latches)
    • 1.代码编写
    • 2.提交结果
    • 3.题目分析
  • V. Always case(Case statement)
    • 1.代码编写
    • 2.提交结果
    • 3.题目分析
  • VI.Always case2 (Priority encoder)
    • 1.代码编写
    • 2.提交结果
    • 3.题目分析
  • VII. Always casez (Priority encoder with casez)
    • 1.代码编写
    • 2.提交结果
    • 3.题目分析
  • VIII. Always nolatches(Avoiding latches)
    • 1.代码编写
    • 2.提交结果
    • 3.题目分析

I. Always blocks(combinational)

1.代码编写

// synthesis verilog_input_version verilog_2001
module top_module(input a, input b,output wire out_assign,output reg out_alwaysblock
);assign out_assign=a&b;always@(*) out_alwaysblock=a&b;
endmodule

2.提交结果

3.题目分析

Combinational always blocks are equivalent to assign statements.
组合逻辑always块等同于assign。
A note on wire vs. reg: The left-hand-side of an assign statement must be a net type (e.g., wire), while the left-hand-side of a procedural assignment (in an always block) must be a variable type (e.g., reg). These types (wire vs. reg) have nothing to do with what hardware is synthesized, and is just syntax left over from Verilog’s use as a hardware simulation language.
关于连线与reg的注释:赋值语句assign的左侧必须是网络类型(例如,wire),而过程赋值的左侧(在过程块中)必须是变量类型(例如reg)。 这些类型(wire vs. reg)与硬件合成无关,只是Verilog用作硬件模拟语言时遗留下来的语法。

II. Always blocks(clocked)

1.代码编写

// synthesis verilog_input_version verilog_2001
module top_module(input clk,input a,input b,output wire out_assign,output reg out_always_comb,output reg out_always_ff   );assign out_assign=a^b;always@(*) out_always_comb=a^b;always@(posedge clk) out_always_ff<=a^b;
endmodule

2.提交结果

3.题目分析

分别用assign、always(combinational)、always(clocked)来实现赋值。

III. Always if (if statement)

1.代码编写

// synthesis verilog_input_version verilog_2001
module top_module(input a,input b,input sel_b1,input sel_b2,output wire out_assign,output reg out_always   ); assign out_assign=(sel_b1&sel_b2)?b:a;always@(*) beginif(sel_b1&sel_b2) out_always=b;else out_always=a;end
endmodule

2.提交结果

3.题目分析

两种方法(assign_if,always_if)分别实现if语句。

IV. Always if2 (if statement latches)

1.代码编写

// synthesis verilog_input_version verilog_2001
module top_module (input      cpu_overheated,output reg shut_off_computer,input      arrived,input      gas_tank_empty,output reg keep_driving  ); //always @(*) beginif (cpu_overheated)shut_off_computer = 1;else shut_off_computer = 0;endalways @(*) beginif (~arrived)keep_driving = ~gas_tank_empty;else keep_driving=0;endendmodule

2.提交结果

3.题目分析

Syntactically-correct code does not necessarily result in a reasonable circuit (combinational logic + flip-flops). The usual reason is: “What happens in the cases other than those you specified?”. Verilog’s answer is: Keep the outputs unchanged.

This behaviour of “keep outputs unchanged” means the current state needs to be remembered, and thus produces a latch. Combinational logic (e.g., logic gates) cannot remember any state. Watch out for Warning (10240): … inferring latch(es)" messages. Unless the latch was intentional, it almost always indicates a bug. Combinational circuits must have a value assigned to all outputs under all conditions. This usually means you always need else clauses or a default value assigned to the outputs.
应确保if(case)分支中包含了所有情况,否则会生成锁存器。
题目要求:The following code contains incorrect behaviour that creates a latch. Fix the bugs so that you will shut off the computer only if it’s really overheated, and stop driving if you’ve arrived at your destination or you need to refuel.

V. Always case(Case statement)

1.代码编写

// synthesis verilog_input_version verilog_2001
module top_module ( input [2:0] sel, input [3:0] data0,input [3:0] data1,input [3:0] data2,input [3:0] data3,input [3:0] data4,input [3:0] data5,output reg [3:0] out   );//always@(*) begin  // This is a combinational circuitcase(sel)3'b000: out=data0;3'b001: out=data1;3'b010: out=data2;3'b011: out=data3;3'b100: out=data4;3'b101: out=data5;default:out=4'b0000;endcaseendendmodule

2.提交结果

3.题目分析

  • The case statement begins with case and each “case item” ends with a colon. There is no “switch”.
    分支语句后为冒号,不用switch。
  • Each case item can execute exactly one statement. This makes the “break” used in C unnecessary. But this means that if you need more than one statement, you must use begin … end.
  • Duplicate (and partially overlapping) case items are permitted. The first one that matches is used. C does not allow duplicate case items.
    允许分支表达式重复(或部分重叠),会采用首个匹配的分支。(在C中不允许重复)

VI.Always case2 (Priority encoder)

1.代码编写

// synthesis verilog_input_version verilog_2001
module top_module (input [3:0] in,output reg [1:0] pos  );always@(*) begincase(in)4'h0: pos=2'd0;4'h1: pos=2'd0;4'h2: pos=2'd1;4'h3: pos=2'd0;4'h4: pos=2'd2;4'h5: pos=2'd0;4'h6: pos=2'd1;4'h7: pos=2'd0;4'h8: pos=2'd3;4'h9: pos=2'd0;4'hA: pos=2'd1;4'hB: pos=2'd0;4'hC: pos=2'd2;4'hD: pos=2'd0;4'hE: pos=2'd1;4'hF: pos=2'd0;endcaseend
endmodule

2.提交结果

3.题目分析

A priority encoder is a combinational circuit that, when given an input bit vector, outputs the position of the first 1 bit in the vector. For example, a 8-bit priority encoder given the input 8’b10010000 would output 3’d4, because bit[4] is first bit that is high.

Build a 4-bit priority encoder. For this problem, if none of the input bits are high (i.e., input is zero), output zero. Note that a 4-bit number has 16 possible combinations.
输出首个“高位”。

Hint: Using hexadecimal (4’hb) or decimal (4’d11) number literals would save typing vs. binary (4’b1011) literals.
同样是4bit长的数值,用16进制/10进制表示,就比用2进制表示更节省空间。

4'hC: pos=2'd2;
4'hC表示4bit宽的十进制12(二进制1100),右侧表示2bit宽的十进制2(二进制10)。

VII. Always casez (Priority encoder with casez)

1.代码编写

// synthesis verilog_input_version verilog_2001
module top_module (input [7:0] in,output reg [2:0] pos  );always@(*) begincasez(in)8'bzzzzzzz1: pos=3'd0;8'bzzzzzz10: pos=3'd1;8'bzzzzz100: pos=3'd2;8'bzzzz1000: pos=3'd3;8'bzzz10000: pos=3'd4;8'bzz100000: pos=3'd5;8'bz1000000: pos=3'd6;8'b10000000: pos=3'd7;default: pos=3'd0;endcaseend
endmodule

2.提交结果

3.题目分析

case、casez、casex的真值表:

可见:

  • 使用case时,需区分x,z。
  • 使用casez时,z可实现全匹配,需区分x。
  • 使用casex时,x,z均可实现全匹配。
  • 但一般使用casez而不用casex,前者还可以表示don‘t care的值。
  • 电路中可以用?代替 z。
  • e.g. 对于分支表达式2’b1?
    在case中,可匹配2’b1z;在casez中,可匹配2’b11、2’b10、2’b1x、2’b1z;在casex中,可匹配2’b11、2’b10、2’b1x、2’b1z。

VIII. Always nolatches(Avoiding latches)

1.代码编写

// synthesis verilog_input_version verilog_2001
module top_module (input [15:0] scancode,output reg left,output reg down,output reg right,output reg up  ); always@(*) beginleft=1'b0;down=1'b0;right=1'b0;up=1'b0; //别写在case里第一句了!!!case(scancode)16'he06b: left=1'b1;16'he072: down=1'b1;16'he074: right=1'b1;16'he075: up=1'b1;endcaseend
endmodule

2.提交结果

3.题目分析

To avoid creating latches, all outputs must be assigned a value in all possible conditions (See also always_if2).
Simply having a default case is not enough. You must assign a value to all four outputs in all four cases and the default case. This can involve a lot of unnecessary typing. One easy way around this is to assign a “default value” to the outputs before the case statement:

always @(*) begin
up = 1’b0; down = 1’b0; left = 1’b0; right = 1’b0;
case (scancode)
… // Set to 1 as necessary.
endcase
end
This style of code ensures the outputs are assigned a value (of 0) in all possible cases unless the case statement overrides the assignment. This also means that a default: case item becomes unnecessary.
用default value 代替default case,节省unecessary typing(从前每个分支即default分支都需要规定四个信号的装填,现在只需要规定一个)。

【verilog学习12】HDLBits:Procedures (Always block/case+Avoiding latches)相关推荐

  1. Verilog学习笔记HDLBits——Shift Registers

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.Shift Registers 1.4-bit shift register 2.Left/right rotat ...

  2. 【verilog学习】HDLBits:Four Wires

    [HDLBits]Four Wires 1.代码编写 2.提交结果 3.题目分析 1.代码编写 module top_module( input a,b,c,output w,x,y,z );assi ...

  3. Verilog学习笔记HDLBits——Finite State Machines(1)

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.Finite State Machines 1.Simple FSM 1(asychronous reset) 2 ...

  4. verilog练习:hdlbits网站上的做题笔记(6)

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  5. verilog练习:hdlbits网站上的做题笔记(5)

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  6. Verilog学习之路(4)— Verilog HDL的程序设计语句

    Verilog HDL的程序设计语句 一.连续赋值语句 连续赋值语句通常用来描述组合逻辑电路,连续赋值的目标类型主要是标量线网和向量线网两种,标量线网如"wire a,b;",向量 ...

  7. verilog练习:hdlbits网站上的做题笔记(7)!强烈推荐!

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  8. verilog练习:hdlbits网站上的做题笔记(8)

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  9. Verilog学习笔记——入门

    Verilog学习笔记 01 基本逻辑门代码设计与仿真 Veriog基本逻辑门代码结构--以一位反相器为例 ModelSim仿真基本流程 02 组合逻辑代码设计与仿真--多路选择器 二选一逻辑--as ...

最新文章

  1. Xamarin Essentials教程使用加速度传感器Accelerometer
  2. 四种ASP网页跳转代码
  3. 80端口攻击_内网端口转发工具的使用总结
  4. 一个百亿级日志系统是怎么设计出来的?
  5. 2019 amazingdotnet 公众号回顾
  6. jira集成开发代码_7种JIRA集成可优化您的Java开发流程
  7. T-SQL语句学习(三)
  8. 转]移动视频监控(1)---项目综述
  9. 基于python的web应用开发-添加关注者
  10. c# 正则表代式的分组和批评模式 .
  11. 数据类型转换(面试题)
  12. 【实战】NLP命名实体识别开源实战教程
  13. HashMap 在 JDK 1.8 中新增的数据结构 – 红黑树
  14. 9. HTML DOM getElementsByName() 方法
  15. C#效率优化(2)-- 方法内联
  16. vscode的pip安装
  17. 背离意味着趋势正在减弱
  18. 如何关闭Steam的弹出广告
  19. 3.8 js过渡效果
  20. android 卸载残留代码,完全卸载AndroidStudio(示例代码)

热门文章

  1. 软件设计质量(一)容错设计
  2. iThoughtsX for mac(优秀的思维导图软件)v9.0中文激活版
  3. 关于XML解析报错问题(LF、CRLF)
  4. idea插件HttpClient插件如何使用?
  5. 2021年安全员-B证考试题库及安全员-B证模拟试题
  6. 关于PD4ML解决中文乱码的问题
  7. c++ 植物大战僵尸中文版call源码
  8. 血浆p-Tau181在Tau沉积中的预测作用
  9. error LNK2005: int dir (?dir@@3HA) already defined in GameStart.obj
  10. 基于videojs实现带有播放列表播放页面