Professional Documents
Culture Documents
Sequential and Parallel Blocks
Sequential and Parallel Blocks
24
Block Statements
Block statements are used to group two or more
statements together so that they act as one statement,
syntactically. They are of two types.
Sequential block statements are enclosed between the
keywords
y begin
g and end. The statements in this block
are executed in a sequential manner.
P
Parallel
ll l block
bl k statements
t t t are enclosed
l d between
b t the
th
keywords fork and join. The statements in this block
are executed concurrently.
concurrently
25
Synthesis Support
Only beginend blocks are supported for
synthesis.
y
forkjoin is not. Therefore forkjoin
must never be used in circuit descriptions
but only in test fixtures.
26
Sequential Blocks
When delays are used in sequential blocks, the delay times are
accumulated sequentially:
initial begin
#5 x = 1b1
1b1;//assign
// i value
l att t = 5 units
it
#5 x = 1b0;//assign value at t = 10 units
#5 x = 1b1;//assign value at t = 15 units
end
28
Parallel Blocks
In a parallel block,
block assignments are concurrent:
initial fork
x = 1b0;
y = 1b1;
z = x & y;
w = x | yy;
join
33
Block Statements
initial Condition initial Condition
begin fork
------- -------
------- -------
end join
end j i
join
34
ForkJoin Example
35
ForkJoin Output
37
Output
38
Change it back to beginend
39
Beginend Output
reg CLK0
CLK0, CLK1;
initial begin
CLK0 = 1'b1;
CLK1 = 1'b1;
fork
forever #1 CLK0 = ~CLK0;;
forever #2 CLK1 = ~CLK1;
join
end
endmodule 42
It Works
43
Block Statements
fork-join and begin-end blocks could be
nested within each other.
Nesting begin-end
begin end within begin-end
begin end is not
useful because all statements will be
executed sequentially anyway.
anyway
begin
.
begin
..
end
end
44
Block Statements
Nesting begin-end within begin-end is
justified only if it is used to control flow.
begin
if (condition)
begin
..
end
end
47
Nesting,
g, Opposite
pp Case
module nesting2(clk, a, b, c, d, e, f); always @(posedge clk)
input clk; begin
output a, b, c, d, e, f; #2 a = 1;
reg a, b, c, d, e, f; #2 b = 1;
fork
initial #2 c = 1;
begin
g #2 d = 1;
1
a = 0; #2 e = 1;
b = 0; join
c = 0; #2 f = 1;
d = 0; end
e = 0; endmodule
f = 0;
end 48
Nesting Example 2 Results
fork
#20 c = 1;
#30 d = 1;
#10 e = 1;
j i
join
50
Event-Based Timing Control
events can be used to trigger execution of
statements or blocks.
Four types of events:
Regular
Named
OR
Level-sensitive
51
Regular Events
Regular Events are signaled by the use of the @
symbol:
always @(received
@(received_data)
data)
Data_buf = {data_pkt[0], data_pkt[1 ], data_pkt[2 ], data_pkt[3]};
//Await triggering of event received_data
gg
//When event is triggered, , store all four
// packets in data buffer.
55
OR Events
56
Level-sensitive Events
57
Named Blocks
Y
You can name a blblock
k bby adding:
ddi <
<name Of
block> after the keywords begin or fork.
end
initial
fork: block2 //parallel block named block2
reg i; // register i is local to block2
//can be accessed by y hierarchical naming,
g, top.block2.i
p
join
59
Scope of Local Variables
A block will use its local variable if it
exists. It will use a module variable if there
is no local variable of that name.
There is no conflict if a block local variable
and a module variable have the same name.
60
module localvariable;
iinteger i,
i A,
A B;
B
reg CLK;
initial begin
CLK = 1'b0;; Block variable i will form
forever #1 CLK = ~CLK; difference. Module variable i
end will form sum. There is no
initial begin conflict or ambiguity. There is
A = 4; B = 3; also no problem in assigning
#2 B = 0; the result of operations on
#2 A = 5;
#2 B = 8; i t
integers to
t a four-bit
f bit reg. This
Thi
end is Verilog, not VHDL.
always @(posedge CLK) begin: block1
reg [3:0] i;
i = A - B;
end
64
Disable Can be Tricky
66