Verilog初级教程(22)赋值间延迟语句与赋值内延迟语句

前言

Verilog延迟语句可以在赋值运算符的左侧或右侧指定延迟。

所谓的左侧就是:

// Delay is specified on the left side
#<delay> <LHS> = <RHS>

右侧就是:

// Delay is specified on the right side
<LHS> = #<delay> <RHS>

下面详细讲解。

正文

赋值间延迟语句

// Delay is specified on the left side
#<delay> <LHS> = <RHS>

赋值间延迟语句在赋值运算符的LHS上有延迟值。这表示语句本身在延迟到期后执行,是最常用的延迟控制形式。

module tb;
  reg  a, b, c, q;

  initial begin
    $monitor("[%0t] a=%0b b=%0b c=%0b q=%0b", $time, a, b, c, q);

    // Initialize all signals to 0 at time 0
    a <= 0;
    b <= 0;
    c <= 0;
    q <= 0;

    // Inter-assignment delay: Wait for #5 time units
    // and then assign a and c to 1. Note that 'a' and 'c'
    // gets updated at the end of current timestep
    #5  a <= 1;
    	c <= 1;

    // Inter-assignment delay: Wait for #5 time units
    // and then assign 'q' with whatever value RHS gets
    // evaluated to
    #5 q <= a & b | c;

    #20;
  end

endmodule

请注意,q在时间10单位时变成了1,因为语句在10个时间单位时被计算,RHS是a、b和c的组合,计算为1。

这是其仿真结果:

[0] a=0 b=0 c=0 q=0
[5000] a=1 b=0 c=1 q=0
[10000] a=1 b=0 c=1 q=1

赋值间延迟仿真

注:看到代码的注释了吗?

Inter-assignment delay: Wait for #5 time units and then assign a and c to 1. Note that ‘a’ and ‘c’ gets updated at the end of current timestep

这是很基础的一句话,这句话说明了Verilog这门语言的基本特点,或者说Verilog中非阻塞赋值的基本特点,如下:

    // Inter-assignment delay: Wait for #5 time units
    // and then assign a and c to 1. Note that 'a' and 'c'
    // gets updated at the end of current timestep
    #5  a <= 1;
    	c <= 1;

这条语句,在第5ns时候虽然给a与c均赋值了1,但是此刻并不生效,而会在当前时间步长结束时生效,例如,我们在此刻加一个语句,使用a与c的值:

  // Inter-assignment delay: Wait for #5 time units
    // and then assign a and c to 1. Note that 'a' and 'c'
    // gets updated at the end of current timestep
    #5  a <= 1;
    	c <= 1;
    	q <= a&c;

在这里插入图片描述

此时,q的值不会为1,而时为0,这就是因为此刻q的值没有生效,如果在第6s在求q得值,我们在第6秒再看就可以看到生效了:

module tb;
  reg  a, b, c, q;

  initial begin
    $monitor("[%0t] a=%0b b=%0b c=%0b q=%0b", $time, a, b, c, q);

    // Initialize all signals to 0 at time 0
    a <= 0;
    b <= 0;
    c <= 0;
    q <= 0;

    // Inter-assignment delay: Wait for #5 time units
    // and then assign a and c to 1. Note that 'a' and 'c'
    // gets updated at the end of current timestep
    #5  a <= 1;
    	c <= 1;
    	q <= a&c;
    #1	
    q <= a&c;

    // Inter-assignment delay: Wait for #5 time units
    // and then assign 'q' with whatever value RHS gets
    // evaluated to
    #5 q <= a & b | c;

    #20;
  end

endmodule

在这里插入图片描述

由于一般timescale默认为1ns/1ps,因此,步长应该为1ns。也就是在1ns末生效。

赋值内延迟语句

// Delay is specified on the right side
<LHS> = #<delay> <RHS>

赋值内延迟是指在赋值运算符的RHS上有一个延迟。这表示语句被计算,RHS上的所有信号的值首先被捕获。然后在延时过后才对结果信号进行赋值。

module tb;
  reg  a, b, c, q;

  initial begin
    $monitor("[%0t] a=%0b b=%0b c=%0b q=%0b", $time, a, b, c, q);

	// Initialize all signals to 0 at time 0
    a <= 0;
    b <= 0;
    c <= 0;
    q <= 0;

    // Inter-assignment delay: Wait for #5 time units
    // and then assign a and c to 1. Note that 'a' and 'c'
    // gets updated at the end of current timestep
    #5  a <= 1;
    	c <= 1;

    // Intra-assignment delay: First execute the statement
    // then wait for 5 time units and then assign the evaluated
    // value to q
    q <= #5 a & b | c;

    #20;
  end
endmodule

仿真结果为:

在这里插入图片描述
很多人就感觉奇怪了,为什么q没有了为1的时候,不应该在10ns时候为1吗?

如果出现这个疑问?很正常,但是需要再次理解理解,这个赋值内延迟的含义与非阻塞赋值的特点。

在第5ns时候,a,b,q同时被赋值,a和c在第5s被非阻塞赋值,也就是在第5ns末有效。
第5ns时,q也被赋值,但是在第5ns时(起始),q经过计算为0,它经过5ns后被赋值,因此,会一直为0,好像1被吞掉了似的,其实理解了二者的含义,很好理解。

为了对比,我们在第5ns时,对a和c都进行阻塞赋值:

// Non-blocking changed to blocking and rest of the
// code remains the same
    #5  a = 1;
    	c = 1;

    q <= #5 a & b | c;

我们可以得到不一样的结果:

在这里插入图片描述
这才是你想要的结果。
什么原因呢?
还是在第5ns时候(初),a和c都已经为1了,此时,q经过计算也为1,然后延迟5ns,赋值给q,因此,q在10ns时候为1。

往期回顾

Verilog初级教程(21)Verilog中的延迟控制语句

Verilog初级教程(20)Verilog中的`ifdef 条件编译语句

Verilog初级教程(19)Verilog中的参数

Verilog初级教程(18)Verilog中的函数与任务

Verilog初级教程(17)Verilog中的case语句

Verilog初级教程(16)Verilog中的控制块

Verilog初级教程(15)Verilog中的阻塞与非阻塞语句

Verilog初级教程(14)Verilog中的赋值语句

Verilog初级教程(13)Verilog中的块语句

Verilog初级教程(12)Verilog中的generate块

Verilog初级教程(11)Verilog中的initial块

Verilog初级教程(10)Verilog的always块

Verilog初级教程(9)Verilog的运算符

Verilog初级教程(8)Verilog中的assign语句

Verilog初级教程(7)Verilog模块例化以及悬空端口的处理

Verilog初级教程(6)Verilog模块与端口

Verilog初级教程(5)Verilog中的多维数组和存储器

Verilog初级教程(4)Verilog中的标量与向量

Verilog初级教程(3)Verilog 数据类型

Verilog初级教程(2)Verilog HDL的初级语法

Verilog初级教程(1)认识 Verilog HDL

芯片设计抽象层及其设计风格

Verilog以及VHDL所倡导的的代码准则

FPGA/ASIC初学者应该学习Verilog还是VHDL?

参考资料及推荐关注

https://www.chipverify.com/verilog/verilog-inter-and-intra-assignment-delay

个人微信公众号: FPGA LAB

交个朋友

猜你喜欢

转载自blog.csdn.net/Reborn_Lee/article/details/107888798