前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Verilog初级教程(22)赋值间延迟语句与赋值内延迟语句

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

作者头像
Reborn Lee
发布2020-08-11 16:58:09
1.8K0
发布2020-08-11 16:58:09
举报

前言

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

所谓的左侧就是:

代码语言:javascript
复制
// Delay is specified on the left side
#<delay> <LHS> = <RHS>

右侧就是:

代码语言:javascript
复制
// Delay is specified on the right side
<LHS> = #<delay> <RHS>

下面详细讲解。

正文

赋值间延迟语句

代码语言:javascript
复制
// Delay is specified on the left side
#<delay> <LHS> = <RHS>

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

代码语言:javascript
复制
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。

这是其仿真结果:

代码语言:javascript
复制
[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中非阻塞赋值的基本特点,如下:

代码语言:javascript
复制
    // 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的值:

代码语言:javascript
复制
  // 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的值没有生效,我们在第6秒再看就可以看到生效了:

非阻塞赋值特点仿真

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

赋值内延迟语句

代码语言:javascript
复制
// Delay is specified on the right side
<LHS> = #<delay> <RHS>

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

代码语言:javascript
复制
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都进行阻塞赋值:

代码语言:javascript
复制
// 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。

参考资料

[1]

Verilog初级教程(21)Verilog中的延迟控制语句: https://blog.csdn.net/Reborn_Lee/article/details/107752232

[2]

Verilog初级教程(20)Verilog中的ifdef 条件编译语句: https://reborn.blog.csdn.net/article/details/107746871

[3]

Verilog初级教程(19)Verilog中的参数: https://blog.csdn.net/Reborn_Lee/article/details/107448941

[4]

Verilog初级教程(18)Verilog中的函数与任务: https://reborn.blog.csdn.net/article/details/107447734

[5]

Verilog初级教程(17)Verilog中的case语句: https://reborn.blog.csdn.net/article/details/107446020

[6]

Verilog初级教程(16)Verilog中的控制块: https://blog.csdn.net/Reborn_Lee/article/details/107437358

[7]

Verilog初级教程(15)Verilog中的阻塞与非阻塞语句: https://blog.csdn.net/Reborn_Lee/article/details/107436015

[8]

Verilog初级教程(14)Verilog中的赋值语句: https://reborn.blog.csdn.net/article/details/107431511

[9]

Verilog初级教程(13)Verilog中的块语句: https://reborn.blog.csdn.net/article/details/107427568

[10]

Verilog初级教程(12)Verilog中的generate块: https://blog.csdn.net/Reborn_Lee/article/details/107308629

[11]

Verilog初级教程(11)Verilog中的initial块: https://reborn.blog.csdn.net/article/details/107307958

[12]

Verilog初级教程(10)Verilog的always块: https://blog.csdn.net/Reborn_Lee/article/details/107052261

[13]

Verilog初级教程(9)Verilog的运算符: https://blog.csdn.net/Reborn_Lee/article/details/106985414

[14]

Verilog初级教程(8)Verilog中的assign语句: https://reborn.blog.csdn.net/article/details/106985139

[15]

Verilog初级教程(7)Verilog模块例化以及悬空端口的处理: https://reborn.blog.csdn.net/article/details/106982280

[16]

Verilog初级教程(6)Verilog模块与端口: https://blog.csdn.net/Reborn_Lee/article/details/106977581

[17]

Verilog初级教程(5)Verilog中的多维数组和存储器: https://blog.csdn.net/Reborn_Lee/article/details/106974813

[18]

Verilog初级教程(4)Verilog中的标量与向量: https://blog.csdn.net/Reborn_Lee/article/details/106973708

[19]

Verilog初级教程(3)Verilog 数据类型: https://blog.csdn.net/Reborn_Lee/article/details/106970881

[20]

Verilog初级教程(2)Verilog HDL的初级语法: https://blog.csdn.net/Reborn_Lee/article/details/106960366

[21]

Verilog初级教程(1)认识 Verilog HDL: https://blog.csdn.net/Reborn_Lee/article/details/106960243

[22]

芯片设计抽象层及其设计风格: https://blog.csdn.net/Reborn_Lee/article/details/106958187

[23]

Verilog以及VHDL所倡导的的代码准则: https://blog.csdn.net/Reborn_Lee/article/details/106872978

[24]

FPGA/ASIC初学者应该学习Verilog还是VHDL?: https://blog.csdn.net/Reborn_Lee/article/details/106793593

[25]

交个朋友: https://blog.csdn.net/Reborn_Lee/article/details/107322084

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FPGA LAB 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 正文
    • 赋值间延迟语句
      • 赋值内延迟语句
        • 参考资料
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档