1.
使用assign和always块来实现或(OR)门。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
module top_module( input a, input b, output wire out_assign, output reg out_alwaysblock ); //assign //always @ (*) endmodule |
2.
用带有时钟的always块来实现a 或(OR) b。在时钟的上升沿来寄存结果。
|
1 2 3 4 5 6 7 8 9 10 11 |
module top_module( input clk, input a, input b, output reg out ); //always @(posedge clk) endmodule |
3.
同时使用assign语句和always @(*)引导的条件句,对下图的MUX进行描述。

|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
module top_module( input a, input b, input c, input d, input sel1, input sel2, output wire out_assign, output reg out_always ); //assign ... //always @(*) ... endmodule |
4.
在编写Verilog程序的时候,如果不是特意设计,尽量避免latch出现。这就要求在组合逻辑中,if 条件后需要加else的情况,case条件需要加default的情况。
举例:在以下情况下,综合器综合出来的电路会带有latch:
|
1 2 3 4 |
always @(*) begin if (cpu_overheated) //电脑过热 shut_off_computer = 1; //关机 end |

修改上述代码,使其不会出现latch。
参考波形:

扩展练习:修改下列代码,使其不会出现latch:
|
1 2 3 4 |
always @(*) begin if (~arrived) //没有到达目的地 keep_driving = ~gas_tank_empty; //只要油箱不空,继续行驶 end |
5.
使用case语句,将与3-bit sel值对应的数据(d0,d1,d2,d3,d4,d5)赋值给out。比如,如果sel 为3,将d3赋值给out,default情况下,将out输出0。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
module top_module ( input [2:0] sel, input [3:0] d0, input [3:0] d1, input [3:0] d2, input [3:0] d3, input [3:0] d4, input [3:0] d5, output reg [3:0] out ); // always@(*) ... endmodule |
6.
在问题4中,为了避免组合电路中出现latch,在使用if或是case时,在每一种可能出现的情况下,都需要给输出赋值。如果一个case逻辑需要同时给4个输出赋值,比如下图所示,scancode为输入,方向键 left,down,right,up都是输出:

那么在scancode所列举的四种情况和default的情况下都需要对4个方向键输出。这里提供一个简洁的办法,如下所示:在always块里先将四个方向键赋值为0,并在case语句里根据需要再将方向键修改为1。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
module top_module ( input [15:0] scancode, output reg left, output reg down, output reg right, output reg up ); always @(*) begin //先将四个方向键赋值为0 up = 1'b0; down = 1'b0; left = 1'b0; right = 1'b0; //在case语句中根据需要将方向键修改为1 case (scancode) //... endcase end endmodule |
补充未完成的代码。
7.
在4-bit输入向量a中,将最先出现 ‘ 1 ’ 的低位输出(优先编码器)。比如
- a = 4’b1010,a[1]最先有1,输出1
- a = 4’b1111,a[0]最先有1,输出0
- a = 4’b1000,输出3
- 如果a中没有任何1出现,输出0。
|
1 2 3 4 5 |
module top_module ( input [3:0] a, output reg [1:0] pos ); endmodule |
8.
参考文章 Verilog case语句 使用casez,casex解决问题7。
对应视频: