当前位置:网站首页>Verilog 过程赋值 区别 详解
Verilog 过程赋值 区别 详解
2022-07-02 03:02:00 【嗒曦】
Verilog 过程赋值
关键词:阻塞赋值,非阻塞赋值,并行
过程性赋值是在 initial 或 always 语句块里的赋值,赋值对象是寄存器、整数、实数等类型。
这些变量在被赋值后,其值将保持不变,直到重新被赋予新值。
连续性赋值总是处于激活状态,任何操作数的改变都会影响表达式的结果;过程赋值只有在语句执行的时候,才会起作用。这是连续性赋值与过程性赋值的区别。
Verilog 过程赋值包括 2 种语句:阻塞赋值与非阻塞赋值。
阻塞赋值
阻塞赋值属于顺序执行,即下一条语句执行前,当前语句一定会执行完毕。
阻塞赋值语句使用等号 = 作为赋值符。
前面的仿真中,initial 里面的赋值语句都是用的阻塞赋值。
非阻塞赋值
非阻塞赋值属于并行执行语句,即下一条语句的执行和当前语句的执行是同时进行的,它不会阻塞位于同一个语句块中后面语句的执行。
非阻塞赋值语句使用小于等于号 <= 作为赋值符。
利用下面代码,对阻塞、非阻塞赋值进行仿真,来说明 2 种过程赋值的区别。
实例
`timescale 1ns/1ns
module test ;
reg [3:0] ai, bi ;
reg [3:0] ai2, bi2 ;
reg [3:0] value_blk ;
reg [3:0] value_non ;
reg [3:0] value_non2 ;
initial begin
ai = 4'd1 ; //(1)
bi = 4'd2 ; //(2)
ai2 = 4'd7 ; //(3)
bi2 = 4'd8 ; //(4)
#20 ; //(5)
//non-block-assigment with block-assignment
ai = 4'd3 ; //(6)
bi = 4'd4 ; //(7)
value_blk = ai + bi ; //(8)
value_non <= ai + bi ; //(9)
//non-block-assigment itself
ai2 <= 4'd5 ; //(10)
bi2 <= 4'd6 ; //(11)
value_non2 <= ai2 + bi2 ; //(12)
end
//stop the simulation
always begin
#10 ;
if ($time >= 1000) $finish ;
end
endmodule
仿真结果如下:
语句(1)-(8)都是阻塞赋值,按照顺序执行。
20ns 之前,信号 ai,bi 值改变。由于过程赋值的特点,value_blk = ai + bi 并没有执行到,所以 20ns 之前,value_blk 值为 X(不确定状态)。
20ns 之后,信号 ai,bi 值再次改变。执行到 value_blk = ai + bi,信号 value_blk 利用信号 ai,bi 的新值得到计算结果 7。
语句(9)-(12)都是非阻塞赋值,并行执行。
首先,(9)-(12)虽然都是并发执行,但是执行顺序也是在(8)之后,所以信号 value_non = ai + bi 计算是也会使用信号 ai,bi 的新值,结果为 7。
其次,(10)-(12)是并发执行,所以 value_non2 = ai2 + bi2 计算时,并不关心信号 ai2,bi2 的最新非阻塞赋值结果。即 value_non2 计算时使用的是信号 ai2,bi2 的旧值,结果为 4'hF。

使用非阻塞赋值避免竞争冒险
上述仿真代码只是为了让读者更好的理解阻塞赋值与非阻塞赋值的区别。实际 Verilog 代码设计时,切记不要在一个过程结构中混合使用阻塞赋值与非阻塞赋值。两种赋值方式混用时,时序不容易控制,很容易得到意外的结果。
更多时候,在设计电路时,always 时序逻辑块中多用非阻塞赋值,always 组合逻辑块中多用阻塞赋值;在仿真电路时,initial 块中一般多用阻塞赋值。
如下所示,为实现在时钟上升沿交换 2 个寄存器值的功能,在 2 个 always 块中使用阻塞赋值。
因为 2 个 always 块中的语句是同时进行的,但是 a=b 与 b=a 是无法判定执行顺序的,这就造成了竞争的局面。
但不管哪个先执行(和编译器等有关系),不考虑 timing 问题时,他们执行顺序总有先后,最后 a 与 b 的值总是相等的。没有达到交换 2 个寄存器值的效果。
实例
always @(posedge clk) begin
a = b ;
end
always @(posedge clk) begin
b = a;
end
但是,如果在 always 块中使用非阻塞赋值,则可以避免上述竞争冒险的情况。
如下所示,2 个 always 块中语句并行执行,赋值操作右端操作数使用的是上一个时钟周期的旧值,此时 a<=b 与 b<=a 就可以相互不干扰的执行,达到交换寄存器值的目的。
实例
always @(posedge clk) begin
a <= b ;
end
always @(posedge clk) begin
b <= a;
end
当然,利用下面代码也可以实现交换寄存器值的功能,但是显然不如在 always 块中直接用非阻塞赋值简单直观。
实例
always @(posedge clk) begin
temp = a ;
a = b ;
b = temp ;
end
边栏推荐
- [learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)
- Use usedeferredvalue for asynchronous rendering
- 小米青年工程师,本来只是去打个酱油
- [question 008: what is UV in unity?]
- Après le mariage
- Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
- Mathematical calculation in real mode addressing
- [staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
- [staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
- 4. Find the median of two positive arrays
猜你喜欢

Formatting logic of SAP ui5 currency amount display

2022-2028 global soft capsule manufacturing machine industry research and trend analysis report

QT implementation interface jump

2022-2028 global nano abrasive industry research and trend analysis report

MMSegmentation系列之训练与推理自己的数据集(三)
![[JSON] gson use and step on the pit](/img/86/6ee2971715e0d29008a4b7b1a7aa45.jpg)
[JSON] gson use and step on the pit

Just a few simple steps - start playing wechat applet

Missing numbers from 0 to n-1 (simple difficulty)

How to create an instance of the control defined in SAP ui5 XML view at runtime?
![[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the](/img/fe/d97b25f702bbc05f941d08147259e0.jpg)
[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
随机推荐
MongoDB非關系型數據庫
Analysis of FLV packaging format
Mathematical calculation in real mode addressing
Provincial election + noi Part IV graph theory
Mongodb non relational database
ZABBIX API creates hosts in batches according to the host information in Excel files
[reading notes] programmer training manual - practical learning is the most effective (project driven)
4. Find the median of two positive arrays
Baohong industry | four basic knowledge necessary for personal finance
[JSON] gson use and step on the pit
Soul app released the annual report on generation Z behavior: nearly 20% of young people love shopping in the vegetable market
批量检测url是否存在cdn—高准确率
QT使用sqllite
es面试题
2022-2028 global nano abrasive industry research and trend analysis report
2022 low voltage electrician test question simulation test question bank simulation test platform operation
tarjan2
[Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials
2022-2028 global human internal visualization system industry research and trend analysis report
Ten minutes will take you in-depth understanding of multithreading - multithreaded teamwork: synchronous control