initial
count = 4�b0;
|
`�|7V `�|7V @W�|7V �>�|7V x`�|7V 0`�|7V @ 0`�|7V ont-family: Times New Roman; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin-bottom: .0001pt; line-height:150%">
If there are more than statements are there to execute, we
can use begin.....end or fork.....join statements to combine them.
Within begin....end, statements will executes one after another
sequentially. Within fork...join, all the statements will executes
simultaneously. More details about begin-end and fork-join you will learn
in next chapter.
initial
begin
a = 1;
c = a;
end
// Both a and c will become 1.
|
Initial block may contain if conditions, loops,
assignment statements etc., but not continuous assignments. You can have
any number of initial blocks in a module. All initial blocks execute
simultaneously.
Initial blocks are very useful in testbenches. Consider a code for
counter. Actually in a chip, initially counter will have a unpredicted
value. But after sometimes, value will increase to its maximum value,
where all the bits are zero. Then it will start counting through 0 to
maximum value. But in the simulation, initially value will be 'x', and if
we don't initialize, it will remain 'x' only. So, we can use initial
blocks to initialize all the signals to a valid value.
Always Block:
Always blocks
execute always. We can use delay or sensitivity list to control the
execution of always block. Syntax is as follows:
always @ (
sencitivity_list )
statements;
Sensitivity list
may contain signal names as follows:
always @ ( signal1
or signal2 )
statement;
Above statement
will execute whenever signal1 or signal2 changes. we can have 1 to any
number of signals in sensitivity list.
always @ (
posedge signal )
statement;
Above statement
executes at the positive edge of signal. Similarly we can use negedge for
negative edge of the signal.
Without sensitivity
list, always block will look like this:
always
#5 clk = ~clk;
This will toggle
the signal clk for every 5 nanoseconds.
More about always block, you will learn after studying some examples.
Blocking and Non-blocking Assignments:
If a set of
statements has to be executed one after another, we have to use blocking
statements. Commonly, we will use blocking statements in combinational
logics. Blocking assignments are done using '=', as in a = b;.
Nonblocking assignments
are commonly used to implement sequential logic. Non blocking statements
without delay in between will execute simultaneously. If you use
non-blocking assignments, expressions will be evaluated depending on the
delay added in-between and will be assigned simultaneously. Consider these
code:
always @( posedge clk )
begin
a <= b;
b <= a;
end
/*
Value of a and b will be swapped.
Commonly used in sequential logics
*/
|
always@
( b )
begin
a = b;
b = a;
end
/*
Value of b will be copied to both a and b. Commonly used in
combinational logic
*/ |
In the next
chapter, you will learn these things more clearly. Now, look back at the
counter code. Now you can realize how the counter is representation of the
circuit.
module counter_tb;
reg clk;
wire[3:0] count;
counter my_counter( .clk(clk), .count( count ) );
initial
begin
clk = 0;
#200 $finish;
end
always
begin
#2 clk = ~clk;
end
always @( posedge clk)
$display("Count = %b", count );
endmodule
|
You can test these codes and see the output.
Prev. : Verilog Operators
Verilog: Table of Contents
Ch: 1.