转载:http://blog.sina.com.cn/s/blog_6c7b6f030101cpgt.html

begin-end and fork-join are used to combine a group of statements in a single block. General syntax with begin-end is as follows:

type_of_block @(sensitivity_list) 
begin: group_name 
 local_variable_declarations; 
 statements; 
end
           type_of_block may be initial or always. sensitivity_list is optional and possible only in always block. You are knowing about initial and always block in the previous chapter.

Sensitivity list is used to model sequential or combinational logic. To make it more clear, observe these examples:

module comb(a, b, c); 
input c; 
input b; 
output a; 
reg a; 
always @( c or b) 
begin 
         a = c & b; 
end 
endmodule
module seq(a, b, c); 
output a; 
input b; 
input c; 
reg a; 
always @( posedge c) 
begin 
       a <= b; 
end 
endmodule
          In the left hand side example, whenever c or b changes, a will become c & b. So it is combinational logic, represents and gate. Note that actual hardware register won't be implemented while synthesizing in the left-hand side example, even though it is declared as the type reg. Right hand side example will be synthesized as D-type Flip-flop since b will be assigned to a when ever c goes high. Difference between "=" and "<=" is explained in the next chapter. These codes can be represented in the form of circuits as shown below.

Output of module comb

Output of the module seq
            Inside an initial or always block, we can group statements using begin--end or fork--join. begin--endgroups two or more statements together sequentially, so that statements are evaluated in the order they are listed. Each timing control is relative to the previous statement. fork--join groups two or more statements together in parallel, so that all statements are evaluated concurrently. Each timing control is absolute to when the group started.

These are some examples to understand how begin--end and fork--join executes.

module forkjoin(clk, a, b); 
  input clk; 
  output a; 
  output b; 
  reg a, b; 
  initial 
  begin 
    a = 0; 
    b = 0; 
  end 
  always @(posedge clk) 
  fork 
  #2 a = 1; 
  #1 b = 1; 
  join 
endmodule
module forkjoin1(clk, a, b); 
input clk; 
output a; 
output b; 
reg a, b; 
initial 
begin 
 a = 0; 
 b = 0; 
end 
always @(posedge clk) 
fork 
#2 a = 1; 
#1 b = a; 
join 
endmodule

Output of forkjoin ^

Output of forkjoin1 ^

module beginend(clk, a, b);
    input clk;
    output a;
    output b;

reg a, b;

initial
    begin
       a = 0;
       b = 0;
    end

always @(posedge clk)
    begin
        #2 a = 1;
        #1 b = a;
    end
endmodule 
Output of beginend ^

From these examples, you can understand the execution.

We can nest begin--end and fork--join. That is, inside a begin--end we can have fork--join and inside fork--join we can have begin--end.  Consider these codes to find out how nested begin--end and fork--join works.

module nesting1(clk, a, b, c, d, e, f); 
 input clk; 
 output a, b, c, d, e, f; 
 reg a, b, c, d, e, f; 
 initial 
  begin 
   a = 0; 
   b = 0; 
   c = 0; 
   d = 0; 
   e = 0; 
   f = 0; 
 end 
 always @(posedge clk) 
  fork 
   #2 a = 1; 
   #2 b = 1; 
  begin 
   #2 c = 1; 
   #2 d = 1; 
   #2 e = 1; 
  end 
 #2 f = 1; 
join 
endmodule
and here is the output: You can notice that a, b, c and f became high 2 ns after the clock, d 2ns after c and e 2ns after d.

module nesting2(clk, a, b, c, d, e, f);
    input clk;
    output a, b, c, d, e, f;

reg a, b, c, d, e, f;

initial
    begin
       a = 0;
       b = 0;
       c = 0;
       d = 0;
       e = 0;
       f = 0;
    end

always @(posedge clk)
    begin
        #2 a = 1;
        #2 b = 1;
        fork
            #2 c = 1;
            #2 d = 1;
            #2 e = 1;
        join
        #2 f = 1;
    end
endmodule
Output wave of the above code is here. Notice that a, b and c became 1 with 2ns delay in-between, d and e became 1 together with c, after that f with 2 ns delay.

Program control will come out of the fork--join block when all the statements finishes executing. in the case of code bellow, control will come out of block after 30ns.

fork
   #20 c = 1;
   #30 d = 1;
   #10 e = 1;
join

最新文章

  1. Eclipse使用Git教程
  2. x01.TestViewContent: 插件测试
  3. Python元组
  4. ftp相关资料
  5. iOS 视频录制、压缩、上传
  6. 40多个纯CSS绘制的图形
  7. JavaScript 数据类型判断
  8. Java并发编程核心方法与框架-CompletionService的使用
  9. JSON C# Class Generator ---由json字符串生成C#实体类的工具(转)
  10. http请求方法与响应状态码
  11. linux chgrp命令【转载】
  12. photoswipe
  13. _tkinter.TclError: no display name and no $DISPLAY environment variable
  14. Linux NetHogs监控工具介绍(转)
  15. CentOS7 安装zookeeper
  16. js到底new了点啥
  17. C# MVC的一种高效分页的html方法
  18. React 语法之let和const命令
  19. 机房收费系统之导出Excel
  20. SpringBoot使用Redis共享用户session信息

热门文章

  1. 普元eos、soa、esb
  2. 简述采用四次握手机制释放TCP连接的四个步骤
  3. django学习之——Model
  4. 2015-10-19 sql1
  5. MapServer Tutorial——MapServer7.2.1教程学习——第一节:MapServer的基本配置管理,静态地图应用以及MapFile文件
  6. 如何使用 Excel 对象将 DataGridView 数据导出到 Excel
  7. 查看selinux与关闭方法
  8. python基础之centos6.5 升级 python2.7, 安装pip, MySQLdb
  9. IDE Fix Pack 6.4.2 released (bugfix release)
  10. Task使用