Use the assignment statement

EDITORIAL words

The development process, we use the most is probably the assignment, there are two ways our usual assignment: blocking assignments and non-blocking assignments. When the brothers just started to learn wing dream would be confused by the assignment of these two ways, then my mind there are several issues always a mess - what is blocking assignments? What is non-blocking assignments? When using blocking assignment? When using non-blocking assignments? Both assignments What are the different ways in the end there? When combined with two assignment mode? At that time, less good teaching, so dream-wing brothers are these simple problem for a long time, and today through this lesson to learn, to dream wing brothers will illustrate these issues by some practical examples.

Non-blocking assignments

In assignment operator assignment "<=" is identified as "non-blocking assignment process (Nonblocking Assignment)". Non-blocking assignment process is characterized as follows:

(1)  in the begin-end serial statement block, an execution statement does not block the non-blocking process statement, the statement before executing the corresponding assignment that is non-blocking assignment process section, a lower statements can also be started.

(2) process simulation first calculates the value of its right assignment expression in the face of non-blocking assignment process, and then wait until the results variable assignment at the end of the simulation time. In other words, the assignment operator in this case is before the end of the other common operations on the same simulation time to be executed.

The program the following statement of 1:

Initial

begin

A <= B; // statement S1

B <= A; // statement S2

end

This statement contains two non-blocking assignment process S1 and S2, when the simulation process encounters an Initial Process block (time 0), the statement S1 is first started, assignment expression "B" is worth to calculate (but a variable is assigned the assignment until the end of the current time step was performed), and because S1 is a non-blocking assignment, it does not block the execution by execution of S1 and S2; S2 due also begins execution, the corresponding assignment expression of "a" is worth to calculate, since the time of the assignment a is S1 has not been performed, the assignment expression values calculated at this time is the initial value of a is obtained. Since S2 is a non-blocking assignment statement, which is corresponding to the variable assignment B will have to wait until the end of the current time to be implemented step; so the end of the current time step, S1, S2 corresponding to two assignment statements concurrently, the calculated a and B, respectively, to obtain the initial value assigned to the variable a and B, so that the exchange of the a and B values. Next, we dream wing brothers and watching one example, as follows

/****************************************************          

* Engineer:    Dream Brother Wing

*   QQ               :   761664056

* E_mail: [email protected]

* The module function: non-blocking assignment module

*****************************************************/

01  module Assignment1(clk,rst_n);

02 // system input

03   the INPUT  CLK ; // system clock input

04   INPUT  RST_N ; // low reset signal

05 // internal register definitions

06   REG  [ . 1 : 0 ] A ; // internal registers

07   REG  [ . 1 : 0 ] B ; // internal registers

08 // assignment block

09  always@(posedge clk or negedge rst_n)

10      begin

11          if(!rst_n)

12              begin

A 13 is <= 2 ; // register initial value

B 14 <= . 1 ; // register initial value

15              end

16          else 

17              begin

A 18 is <= B ; // register data exchange

B. 19 <= A ; // register data exchange

20              end 

21      end 

22  endmodule 

Write test code as follows

 

/****************************************************          

* Engineer:    Dream Brother Wing

*   QQ               :   761664056

* E_mail: [email protected]

*   The module function:非阻塞赋值测试模块

*****************************************************/

01  `timescale 1ns/1ps 

02  module tb;

03

04  reg clk;

05  reg rst_n;

06

07  initial 

08      begin

09          clk=0;

10          rst_n=0;

11          # 1000.1 rst_n=1;

12      end 

13

14  always #10 clk=~clk;    

15      

16  Assignment1 Assignment1(

17      .clk(clk),

18      .rst_n(rst_n)

19   );

20  endmodule 

 

查看仿真波形如下:

从仿真图我们可以看出,使用非阻塞型过程赋值语句,把a的初值给b,b的初值给a。因此可以证实我们前面的分析是正确的。非阻塞型过程赋值语句一般应用于时序逻辑。

阻塞赋值语句

以赋值操作符“=”来标识的赋值操作称为“阻塞型过程赋值(blocking Assignment)”。阻塞型过程赋值语句的特点是:

1)串行块(begin-end)中的各条阻塞型过程赋值语句将以它们在顺序块中排列次序依次得到执行。

2)阻塞型过程赋值语句的执行过程是:首先计算右端赋值表达式的值,然后立即将计算结果赋值给“=”左端的被赋值变量。

阻塞型过程赋值语句的这两个特点表明:仿真进程在遇到阻塞型过程赋值语句时将计算表达式的值并立即将其结果赋给等式左边的被赋值变量;在串行语句块中,下一条语句的执行会被本条阻塞型过程赋值语句所阻塞,只有在当前这条阻塞型过程赋值语句所对应的赋值操作执行完后下一条语句才能开始执行。

如以下语句程序2:

    initial

     begin

      a=0;//语句S1

      a=1;//语句s2

     end

在这段语句中包含两条阻塞型过程赋值语句S1和S2,它们都是在仿真零时刻得到执行的,其对应的赋值操作也都是在0时刻进行的。但由于它们是阻塞型赋值语句,所以在执行S1语句是S2被阻塞而不能得到执行;只有在S1执行完,a被赋值0之后,S2才能开始执行。而S2的执行将使a被重新赋值1,所以上面这个过程块执行后变量a的值终取值为1。

接下来,梦翼师兄和大家一起看一个实例,代码如下

/****************************************************          

*   Engineer        :   梦翼师兄

*   QQ               :   761664056

*   E_mail          :   [email protected]

*   The module function:阻塞赋值模块

*****************************************************/

01  module Assignment2(clk,rst_n);

02  //系统输入

03  input clk;//系统时钟输入

04  input rst_n;//低电平复位信号

05  //内部寄存器定义

06  reg [1:0]a;//内部寄存器

07  reg [1:0]b;//内部寄存器

08  //赋值语句块

09  always@(posedge clk or negedge rst_n)

10      begin

11          if(!rst_n)

12              begin

13                  b=1;//寄存器赋初值

14                  a=2;//寄存器赋初值

15              end

16          else 

17              begin

18                  b=a;//寄存器数据交换

19                  a=b;//寄存器数据交换

20              end 

21      end 

22  endmodule 

 

查看仿真波形如下:

 

可以看出,只是把赋值方式换成了阻塞型,结果就和非阻塞型的不同。阻塞型赋值语句一般用在组合逻辑中。

 

Guess you like

Origin www.cnblogs.com/mengyi1989/p/11502779.html