Professional Documents
Culture Documents
Verilog II
Verilog II
1DT109 ASPLOC
2022 VT1-VT2
2022-09-17 2
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
Not1
B
output O; Or1 O
And
C 2
wire w_not, w_and1, w_and2;
OR
endmodule
2022-09-17 3
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
Not1
B
output O; Or1 O
And
C 2
wire w_not, w_and1, w_and2;
OR
endmodule
2022-09-17 4
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
Not1
B
output O; Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 5
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
Not1
B
output O; Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 6
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
output O;
input/output port definition B
Not1
Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 7
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
output O;
input/output port definition B
Not1
Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 8
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
output O;
input/output port definition B
Not1
Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 9
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
output O;
input/output port definition B
Not1
Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 10
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
output O;
input/output port definition B
Not1
Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 11
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
output O;
input/output port definition B
Not1
Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 12
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
output O;
input/output port definition B
Not1
Or1 O
And
C 2
my_module
declaration
OR
endmodule
2022-09-17 13
Module-based modelling in Verilog
my_module
module my_module(A, B, C, O);
A And
1
input A, B, C;
output O;
input/output port definition B
Not1
Or1 O
And
2
OR
endmodule
2022-09-17 14
Combinational vs Sequential logic
• Combinational logic is defined as the time independent
logic which do not depends upon previous inputs to
generate any output.
• Examples – Encoder, Decoder, Multiplexer, Demultiplexer
• Sequential logic is a type of logic circuit whose output
depends not only on the present value of its input signals
but on the sequence of past inputs, the input history as well.
• Examples – Flip-flops, counters, memory
2022-09-17 15
Sequential logic example: counter
2022-09-17 16
Sequential logic example: counter
2022-09-17 17
Sequential logic example: counter
2022-09-17 18
Sequential logic example: counter
Input
2022-09-17 19
Sequential logic example: counter
2022-09-17 20
Sequential logic example: counter
2022-09-17 21
Sequential logic example: counter
2022-09-17 22
Sequential logic example: counter
Mem component
such as Flip-Flop
(sequential logic)
2022-09-17 23
Sequential logic example: counter
Mem component
such as Flip-Flop
(sequential logic)
2022-09-17 24
Sequential logic example: counter
Mem component
such as Flip-Flop
(sequential logic)
2022-09-17 25
Sequential logic example: counter
Mem component
such as Flip-Flop
(sequential logic)
2022-09-17 clk 26
Sequential logic example: counter
Mem component
such as Flip-Flop
(sequential logic)
2022-09-17 clk 27
Sequential logic example: counter
Mem component
such as Flip-Flop
(sequential logic)
2022-09-17 clk 28
Sequential logic example: counter
Mem component
such as Flip-Flop
(sequential logic)
2022-09-17 clk 29
Sequential logic example: counter
Mem component
such as Flip-Flop
(sequential logic)
2022-09-17 clk 30
Sequential logic example: critical path break
input output
Combinational logic
2022-09-17 31
Sequential logic example: critical path break
input output
Combinational logic
FF
2022-09-17 32
Sequential logic example: critical path break
input output
Combinational logic
Combi
FF
logic
2022-09-17 33
Sequential logic example: critical path break
input output
Combinational logic
Combi
FF
logic
2022-09-17 34
Sequential logic example: critical path break
input output
Combinational logic
Combi
FF
logic
2022-09-17 35
Sequential logic example: critical path break
input output
Combinational logic
input
Combi
FF
logic
2022-09-17 36
Sequential logic example: critical path break
input output
Combinational logic
input output
Combi
FF
logic
2022-09-17 37
Sequential logic example: critical path break
input output
Combinational logic
input output
Combi
FF
logic
2022-09-17 38
Sequential logic example: critical path break
input output
Combinational logic
input output
Combi
FF
logic
2022-09-17 39
Sequential logic example: critical path break
input output
Combinational logic
input output
Combi
FF
logic
2022-09-17 40
Sequential logic example: critical path break
input output
Combinational logic
input output
Combi
FF
logic
input output
Combinational logic
input output
Combi
FF
logic
input output
Combinational logic
input output
Combi
FF
logic
input output
Combinational logic
input output
Combi
FF
logic
input output
Combinational logic
input output
Combi
FF
logic
input output
Combinational logic
input output
Combi
FF
logic
input output
Combinational logic
input output
Combi Combi
FF
logic logic
input output
Combinational logic
input output
Combi Combi
FF
logic logic
input output
Combinational logic
input output
Combi Combi
FF
logic logic
D Q
clk
2022-09-17 50
Model flip-flop (FF) using Verilog
• Step 1: Find the schema of an FF
• Step 2: Use gate-level or data-flow level modeling for FF
D Q
D_FF
clk
2022-09-17 51
Model flip-flop (FF) using Verilog
• Step 1: Find the schema of an FF
• Step 2: Use gate-level or data-flow level modeling for FF
D Q
D_FF
clk
2022-09-17 52
Model flip-flop (FF) using Verilog
• Step 1: Find the schema of an FF
• Step 2: Use gate-level or data-flow level modeling for FF
D_FF
clk
2022-09-17 53
Model flip-flop (FF) using Verilog
• We can use RTL-level modeling in Verilog to model FF
• The recommended way to model combinational logic in Verilog
module d_ff(clk, D, Q);
input clk, D;
output Q;
D Q
reg Q;
D_FF
always @ (posedge clk) begin
Q <= D;
clk
end
2022-09-17
endmodule 54
The always block
An always block is one of the procedural blocks in Verilog.
• Statements inside an always block are executed sequentially.
• But they may take effect simultaneously
a=1; a=1;
b=2; b=2;
always @ (a, b) begin always @ (a, b) begin
a <= b; a = b;
b <= a; b = a;
end // a=2, b=1; end // a=2, b=2;
2022-09-17 55
The sensitivity list
• A sensitivity list is the expression that defines when the always
block should be executed and is specified after the @ operator
within parentheses ( ).
• Sensitivity list is the key to differentiate combinational logic and
sequential logic.
Execute always block whenever value of Execute always block only on the rising
“a” or “b” change – combinational logic edge of signal clk – sequential lock
into the
always sensitivity
@ (a, b) list for always
sequential @ (posedge logic clk)
begin begin
c = a | b; c <= a | b; Can also use
negedge
end
2022-09-17
end for falling edge
58
The reg data type (vs wire)
• Verilog data types are divided into two main groups: nets (wire) and
variables (reg). The distinction comes from how they are intended to
represent different hardware structures.
• A net (wire) data type represents a physical connection between
structural entities. (Recall the my_module example)
• A variable (reg) data type represents a piece of storage.
assign O = reg_o;
reg reg_o;
endmodule
2022-09-17 61
my_module
Demo 1 A And
1
B Not
• my_module from Verilog I 1
And
Or1 O
module demo_1(A,B,C,O);
input A, B, C;
output O;
assign O = reg_o;
reg reg_o;
endmodule
2022-09-17 62
my_module
Demo 1 A And
1
B Not
• my_module from Verilog I 1
And
Or1 O
module demo_1(A,B,C,O);
input A, B, C; 𝑂 =𝐴∙𝐶+𝐵∙𝐶
output O;
assign O = reg_o;
reg reg_o;
endmodule
2022-09-17 63
my_module
Demo 1 A And
1
B Not
• my_module from Verilog I 1
And
Or1 O
module demo_1(A,B,C,O);
input A, B, C; 𝑂 =𝐴∙𝐶+𝐵∙𝐶
output O;
assign O = reg_o;
reg reg_o;
endmodule
2022-09-17 64
Putting them all together
• Q only reflects the value of D on a rising edge
• Rising edge means 0 in the past of input and 1 in the current
reg Q; D_FF
always @ (posedge clk) begin
Q <= D; clk
end
2022-09-17
endmodule 65
Putting them all together
• Q only reflects the value of D on a rising edge
• Rising edge means 0 in the past of input and 1 in the current
reg Q; D_FF
always @ (posedge clk) begin
Q <= D; clk
end
2022-09-17
endmodule 66
D_FF with synchronous reset
• Add a reset to the D_FF
• Synchronous reset means that the D_FF can only be reset on clock
edge (in our case, a rising edge)
module d_ff(clk, D, Q);
input clk, D;
output Q; Q
D
reg Q;
reset
always @ (posedge clk) begin
D_FF
if(!reset)
Q <= 1’b0;
clk
else
Q <= D;
end
endmodule
2022-09-17 67
D_FF with synchronous reset
• Add a reset to the D_FF
• Synchronous reset means that the D_FF can only be reset on clock
edge (in our case, a rising edge)
module d_ff(clk, D, Q);
input clk, D;
output Q; Q
D
reg Q;
reset
always @ (posedge clk) begin
D_FF
if(!reset)
Q <= 1’b0;
clk
else
Q <= D;
end Added gates!
endmodule
2022-09-17 68
D_FF with synchronous reset
• Add a reset to the D_FF
• Synchronous reset means that the D_FF can only be reset on clock
edge (in our case, a rising edge)
module d_ff(clk, D, Q);
How to add asynchronous reset?
input clk, D;
output Q; Q
D
reg Q;
reset
always @ (posedge clk) begin
D_FF
if(!reset)
Q <= 1’b0;
clk
else
Q <= D;
end Added gates!
endmodule
2022-09-17 69
D_FF with asynchronous reset
• Add a reset to the D_FF
• Asynchronous reset means that the D_FF can be reset on reset
edge (in our case, a falling edge)
module d_ff(clk, D, Q);
input clk, D;
output Q;
reg Q; D Q
always @ (posedge clk,
negedge reset) begin
D_FF
if(!reset)
Q <= 1’b0;
else clk
Q <= D;
end
reset
2022-09-17 endmodule 70
D_FF with asynchronous reset
• Add a reset to the D_FF
• Asynchronous reset means that the D_FF can be reset on reset
edge (in our case, a falling edge)
module d_ff(clk, D, Q);
input clk, D;
output Q;
reg Q; D Q
always @ (posedge clk,
negedge reset) begin
D_FF
if(!reset)
Q <= 1’b0;
else clk
Q <= D;
end
reset
2022-09-17 endmodule 71
D_FF with asynchronous reset
• Add a reset to the D_FF
• Asynchronous reset means that the D_FF can be reset on reset
edge (in our case, a falling edge)
module d_ff(clk, D, Q);
input clk, D;
output Q;
reg Q; D Q
always @ (posedge clk,
negedge reset) begin
D_FF
if(!reset)
Q <= 1’b0;
else clk
Q <= D; No added gates
end needed!
reset
2022-09-17 endmodule 72
D_FF with asynchronous reset
• Add a reset to the D_FF
• Asynchronous reset means that the D_FF can be reset on reset
edge (in our case, a falling edge)
Always use asynchronous D_FF.
module d_ff(clk, D, Q);
(why???)
reg Q;
always @ (posedge clk,
negedge reset) begin
D_FF
if(!reset)
Q <= 1’b0;
else clk
Q <= D; No added gates
end needed!
reset
2022-09-17 endmodule 73
Blocking vs. non-blocking assignment
• Different ways to drive wire and reg.
• = is called blocking assignment.
• Blocking assignment executes “in series” because a blocking
assignment blocks execution of the next statement until it completes.
• <= is called non-blocking assignment.
• Non-blocking assignment executes “in parallel” because it describes
assignments that all occur at the same time.
Blocking assignment Non-blocking assignment
wire c; reg c; reg c;
assign c = a | b; always @ (a, b) always @ (posedge clk)
begin begin
c = a | b; c <= a | b;
2022-09-17 end end 74
Blocking vs. non-blocking assignment
Rules of thumb No.3:
• Different ways to drive wire and reg.
1. Use blocking assignment to model combinational
• = is called blocking assignment.
logic.
• Blocking assignment executes “in series” because a blocking
2. Use non-blocking assignment
assignment blocks execution to model
of the next statement sequential
until it completes.
logic.
• <= is called non-blocking assignment.
• Non-blocking assignment executes “in parallel” because it describes
assignments that all occur at the same time.
Blocking assignment Non-blocking assignment
wire c; reg c; reg c;
assign c = a | b; always @ (a, b) always @ (posedge clk)
begin begin
c = a | b; c <= a | b;
2022-09-17 end end 75
Blocking vs. non-blocking assignment
Rules of thumb No.3:
• Different ways to drive wire and reg.
1. Use blocking assignment to model combinational
• = is called blocking assignment.
logic.
• Blocking assignment executes “in series” because a blocking
2. Use non-blocking assignment
assignment blocks execution to model
of the next statement sequential
until it completes.
logic.
• <= is called non-blocking assignment.
• Non-blocking assignment executes “in parallel” because it describes
Bad styles can lead to both buggy and
assignments that all occur at the same time.
Blocking assignment Non-blocking assignment
O
C
O
C
O
C
Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
O
C
Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
O
C
Behavioral Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
O
C
Behavioral Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
O
C
Behavioral Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
O
C
Behavioral Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
O
C
Behavioral Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
O
C
Behavioral Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
O
C
Behavioral Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
Results
always @ (A, B, C) begin Match!
always @ (A, B, C) begin
r_and = A & B; (no bug) r_and <= A & B;
r_or = r_and | C; r_or <= r_and | C;
end //demo_2 end //demo_2
2022-09-17 87
Post-synth Post-synth
Blocking assignment – Good and bad styles
A
B
O
C
Behavioral Behavioral
Good style Bad style
reg r_and, r_or; reg r_and, r_or;
assign O = r_or; assign O = r_or;
Results
Results Mismatch!
always @ (A, B, C) begin Match!
always @ (A, B, C) begin
r_and = A & B; r_and <= A & B; (bug in
(no bug) front-end)
r_or = r_and | C; r_or <= r_and | C;
end //demo_2 end //demo_2
2022-09-17 88
Post-synth Post-synth
Non-blocking assignment – Good and bad styles
D_1 D_2 Q
D_FF D_FF
clk
D_FF D_FF
clk
D_FF D_FF
clk
D_FF D_FF
clk
2022-09-17 93
Multi-drive always blocks
• Statements within always are
always @ (a, b) begin evaluated sequentially from
… the first to the last.
x = a | b;
… • However, different always
end blocks are executed
randomly.
always @ (c, d) begin • The execution order of the
green and blue always are
…
totally random run-to-run.
x = c & d;
…
end
2022-09-17 94
Multi-drive always blocks
• Statements within always are
always @ (a, b) begin evaluated sequentially from
… the first to the last.
x = a | b;
… • However, different always
end blocks are executed
randomly.
always @ (c, d) begin • The execution order of the
green and blue always are
…
totally random run-to-run.
x = c & d;
…
end
2022-09-17 95
Multi-drive always blocks
• Statements within always are
always @ (a, b) begin evaluated sequentially from
… the first to the last.
x = a | b;
… • However, different always
end blocks are executed
randomly.
always @ (c, d) begin • The execution order of the
green and blue always are
…
totally random run-to-run.
x = c & d; Critical warning
… generated during
end synthesis.
2022-09-17 96
Multi-drive always blocks
• Statements within always are
always @ (a, b) begin evaluated sequentially from
… the first to the last.
x = a | b;
• However, different always
… Rules of thumbblocksNo.4:
are executed
end
Each reg should only be driven by one always block.
randomly.
always @ (c, d) begin • The execution order of the
green and blue always are
…
totally random run-to-run.
x = c & d; Critical warning
… generated during
end synthesis.
2022-09-17 97
Bit-vector is the only data type in Verilog
• A bit can take on one of four values
2022-09-17 109
Bit literals
4’b10_11
2022-09-17 110
Bit literals
4’b10_11
2022-09-17 111
Bit literals
4’b10_11
Decimal number
Representing size in bits
2022-09-17 112
Bit literals
4’b10_11
Decimal number
Representing size in bits
2022-09-17 113
Bit literals
4’b10_11
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 114
Bit literals
4’b10_11
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 115
Bit literals
4’b10_11
Underscores
are ignored
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 116
Bit literals • Binary literals
4’b10_11
Underscores
are ignored
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 117
Bit literals • Binary literals
• 2’b01 (01)
4’b10_11
Underscores
are ignored
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 118
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
4’b10_11
Underscores
are ignored
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 119
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
4’b10_11
Underscores
are ignored
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 120
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
Underscores
are ignored
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 121
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
Underscores
are ignored
Base format
(B, O, H, D)
Decimal number
Representing size in bits
2022-09-17 122
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
2022-09-17 123
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
2022-09-17 124
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
2022-09-17 125
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
2022-09-17 126
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
2022-09-17 127
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
2022-09-17 128
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
2022-09-17 129
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
2022-09-17 130
Bit literals • Binary literals
• 2’b01 (01)
• 8’b0001_1111 (00011111)
• -3’b1 (111)
always @ (a, b)
begin
c = a | b;
end
always @ (a, b)
begin
c = a | b | c;
end //demo_4
2022-09-17 133
Combinational loop – the obvious case
always @ (a, b)
begin
c = a | b;
end
always @ (a, b)
begin
c = a | b | c;
end //demo_4
2022-09-17 134
Combinational loop – the obvious case
always @ (a, b)
begin
c = a | b;
end
always @ (a, b)
begin
c = a | b | c;
end //demo_4
2022-09-17 135
Combinational loop – the obvious case
always @ (a, b)
begin
c = a | b;
end
always @ (a, b)
begin
c = a | b | c;
end //demo_4
2022-09-17 136
Combinational loop – the obvious case
always @ (a, b)
begin
c = a | b;
end
always @ (a, b)
begin
c = a | b | c;
end //demo_4
2022-09-17 137
Combinational loop – the obvious case
always @ (a, b)
begin
c = a | b;
end
always @ (a, b)
begin
c = a | b | c;
end //demo_4
2022-09-17 138
Combinational loop – the obvious case
always @Hard
(a, b)
to use simulation
begin results to detect combi-
c = a | b; loop.
end
always @ (a, b)
begin
c = a | b | c;
end //demo_4
2022-09-17 139
Combinational loop – the obvious case
always @Hard
(a, b)
to use simulation
begin results to detect combi-
c = a | b; loop.
end
always @ (a, b)
begin
c = a | b | c;
end //demo_4
2022-09-17 140
Combinational loop – the obvious case
always @Hard
(a, b)
to use simulation
begin results to detect combi-
c = a | b; loop.
end No error generated at both pre
and post synthesis simulation.
But a non-critical warning is
alwaysgenerated
@ (a, b)during synthesis.
begin
c = a | b | c;
end //demo_4
2022-09-17 141
Combinational loop – the obvious case
always @Hard
(a, b)
to use simulation
begin results to detect combi-
c = a | b; loop.
end No error generated at both pre
and post synthesis simulation.
But a non-critical warning is
alwaysgenerated
@ (a, b)during synthesis.
begin
c = a | b | c;
end //demo_4
2022-09-17 142
Combinational loop – the obvious case
always @Hard
(a, b)
to use simulation
begin results to detect combi-
c = a | b; loop.
end No error generated at both pre
and post synthesis simulation.
But a non-critical warning is
alwaysgenerated
@ (a, b)during synthesis.
begin
c message
Or an error = a | b is| c;
generated during later
end
bit //demo_4
stream generation.
2022-09-17 143
Combinational loop – the obvious case
always @Hard
(a, b)
to use simulation
begin results to detect combi-
c = a | b; loop. Rules of thumb No.5:
Remember to check
end No error generated at both pre
the Vivado methodology report
at each step.
and post synthesis simulation.
But a non-critical warning is
alwaysgenerated
@ (a, b)during synthesis.
begin
c message
Or an error = a | b is| c;
generated during later
end
bit //demo_4
stream generation.
2022-09-17 144
Combinational loop – the covert case
always @ (a, x) begin
…
y = a| x;
…
y
end
ocean of
always @ (y, b) begin
gates in a large design.
… x
x = y | b;
…
end
2022-09-17 146
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
2022-09-17 147
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
2022-09-17 148
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
DUT/UUT
2022-09-17 149
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
DUT/UUT
2022-09-17 150
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
DUT/UUT
2022-09-17 151
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
DUT/UUT
2022-09-17 152
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
DUT/UUT
2022-09-17 153
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
inputs
DUT/UUT
2022-09-17 154
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
inputs outputs
DUT/UUT
2022-09-17 155
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
inputs outputs
DUT/UUT
2022-09-17 156
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
inputs outputs
DUT/UUT
2022-09-17 157
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
inputs outputs
DUT/UUT
2022-09-17 158
Writing testbench using Verilog
• Testbenches are used to test/debug Verilog design with
synthetic inputs and collect the outputs.
• Testbenches are usually top modules in a Verilog project.
• The design-under-test is a sub-modules in a testbench module
• Testbenches are not supposed to be synthesizable.
testbench
inputs outputs
DUT/UUT
2022-09-17 159
A testbench example
DUT.v tb.v
module demo_2(A, B, C, O); `timescale 1ns/1ps
module tb;
input A, B, C; reg a, b, c;
output O; wire o;
r_and = A & B; a = 0; b = 0; c = 0;
r_or = r_and | C; #period;
a = 1; b = 0; c = 0;
end #period;
a = 1; b = 1; c = 0;
Endmodule ...
end
2022-09-17 endmodule 160
A testbench example
DUT.v tb.v
module demo_2(A, B, C, O); `timescale 1ns/1ps
module tb; Top module, no
input A, B, C; reg a, b, c; input nor
output O; wire o; outpus
reg r_and, r_or; demo_2 UUT(a, b, c, o);
assign O = r_or; localparam period = 1;
r_and = A & B; a = 0; b = 0; c = 0;
r_or = r_and | C; #period;
a = 1; b = 0; c = 0;
end #period;
a = 1; b = 1; c = 0;
Endmodule ...
end
2022-09-17 endmodule 161
A testbench example
DUT.v tb.v
module demo_2(A, B, C, O); `timescale 1ns/1ps
module tb; Top module, no
input A, B, C; reg a, b, c; input nor
output O; wire o; outpus
reg r_and, r_or; demo_2 UUT(a, b, c, o);
assign O = r_or; localparam period = 1;
r_and = A & B; a = 0; b = 0; c = 0;
r_or = r_and | C; #period;
a = 1; b = 0; c = 0;
end #period;
a = 1; b = 1; c = 0;
Endmodule ...
end
2022-09-17 endmodule 162
A testbench example
DUT.v tb.v
module demo_2(A, B, C, O); `timescale 1ns/1ps
module tb; Top module, no
input A, B, C; reg a, b, c; input nor
output O; wire o; outpus
reg r_and, r_or; demo_2 UUT(a, b, c, o);
assign O = r_or; localparam period = 1;
r_and = A & B; a = 0; b = 0; c = 0;
r_or = r_and | C; #period;
a = 1; b = 0; c = 0;
end #period;
a = 1; b = 1; c = 0;
Endmodule ...
end
2022-09-17 endmodule 163
A testbench example
DUT.v tb.v What we get from DUT.v
module demo_2(A, B, C, O); `timescale 1ns/1ps
module tb; Top module, no
input A, B, C; reg a, b, c; input nor
output O; wire o; outpus
reg r_and, r_or; demo_2 UUT(a, b, c, o);
assign O = r_or; localparam period = 1;
r_and = A & B; a = 0; b = 0; c = 0;
r_or = r_and | C; #period;
a = 1; b = 0; c = 0;
end #period;
a = 1; b = 1; c = 0;
Endmodule ...
end
2022-09-17 endmodule 164
A testbench example
DUT.v tb.v What we get from DUT.v
module demo_2(A, B, C, O); `timescale 1ns/1ps
module tb; Top module, no A
input A, B, C; reg a, b, c; input nor B
output O; wire o; outpus
O
reg r_and, r_or; demo_2 UUT(a, b, c, o); C
assign O = r_or; localparam period = 1;
r_and = A & B; a = 0; b = 0; c = 0;
r_or = r_and | C; #period;
a = 1; b = 0; c = 0;
end #period;
a = 1; b = 1; c = 0;
Endmodule ...
end
2022-09-17 endmodule 165
A testbench example
DUT.v tb.v What we get from DUT.v
module demo_2(A, B, C, O); `timescale 1ns/1ps
module tb; Top module, no A
input A, B, C; reg a, b, c; input nor B
output O; wire o; outpus
O
reg r_and, r_or; demo_2 UUT(a, b, c, o); C
assign O = r_or; localparam period = 1;
initial begin
a = 0; b = 0; c = 0;
#period;
a = 1; b = 0; c = 0;
#period;
a = 1; b = 1; c = 0;
...
end
2022-09-17 175
endmodule
For loop statement
tb.v
`timescale 1ns/1ps
module tb;
reg a, b, c;
wire o;
initial begin
a = 0; b = 0; c = 0;
#period;
a = 1; b = 0; c = 0;
#period;
a = 1; b = 1; c = 0;
...
end
2022-09-17 176
endmodule
For loop statement
tb.v
`timescale 1ns/1ps
module tb;
reg a, b, c;
wire o;
initial begin
a = 0; b = 0; c = 0;
#period; Need 8 lines to
a = 1; b = 0; c = 0; exhaust all
#period; input patterns.
a = 1; b = 1; c = 0;
...
end
2022-09-17 177
endmodule
For loop statement
tb.v
`timescale 1ns/1ps What if the input
module tb; contains 16 signals?
reg a, b, c; 216 lines!
wire o;
initial begin
a = 0; b = 0; c = 0;
#period; Need 8 lines to
a = 1; b = 0; c = 0; exhaust all
#period; input patterns.
a = 1; b = 1; c = 0;
...
end
2022-09-17 178
endmodule
For loop statement
tb.v tb.v
`timescale 1ns/1ps What if the input `timescale 1ns/1ps
module tb; contains 16 signals? module tb;
reg a, b, c; 216 lines! reg [2:0] a;
wire o; wire o;
tb.v tb.v
`timescale 1ns/1ps What if the input `timescale 1ns/1ps
module tb; contains 16 signals? module tb;
reg a, b, c; 216 lines! reg [2:0] a;
wire o; wire o;
tb.v tb.v
`timescale 1ns/1ps What if the input `timescale 1ns/1ps
module tb; contains 16 signals? module tb; assign bit a[2] to the first port of
reg a, b, c; 216 lines! reg [2:0] a; UUT, and so on. That is:
wire o; wire o; a[2] to a, A[1] to b, A[0] to c.
tb.v tb.v
`timescale 1ns/1ps What if the input `timescale 1ns/1ps
module tb; contains 16 signals? module tb; assign bit a[2] to the first port of
reg a, b, c; 216 lines! reg [2:0] a; UUT, and so on. That is:
wire o; wire o; a[2] to a, A[1] to b, A[0] to c.
tb.v tb.v
`timescale 1ns/1ps What if the input `timescale 1ns/1ps
module tb; contains 16 signals? module tb; assign bit a[2] to the first port of
reg a, b, c; 216 lines! reg [2:0] a; UUT, and so on. That is:
wire o; wire o; a[2] to a, A[1] to b, A[0] to c.
tb.v tb.v
`timescale 1ns/1ps What if the input `timescale 1ns/1ps
module tb; contains 16 signals? module tb; assign bit a[2] to the first port of
reg a, b, c; 216 lines! reg [2:0] a; UUT, and so on. That is:
wire o; wire o; a[2] to a, A[1] to b, A[0] to c.
tb.v tb.v
`timescale 1ns/1ps What if the input `timescale 1ns/1ps
module tb; contains 16 signals? module tb; assign bit a[2] to the first port of
reg a, b, c; 216 lines! reg [2:0] a; UUT, and so on. That is:
wire o; wire o; a[2] to a, A[1] to b, A[0] to c.
tb.v tb.v
`timescale 1ns/1ps What if the input `timescale 1ns/1ps
module tb; contains 16 signals? module tb; assign bit a[2] to the first port of
reg a, b, c; 216 lines! reg [2:0] a; UUT, and so on. That is:
wire o; wire o; a[2] to a, A[1] to b, A[0] to c.
2022-09-17 187
While and repeat loop statements
tb.v
`timescale 1ns/1ps
module tb;
reg [2:0] a;
wire o;
demo_5 UUT(a[2], a[1], a[0], o); demo_5 UUT(a[2], a[1], a[0], o);
localparam period = 1; localparam period = 1;
integer i; integer i;
initial begin initial begin
i=0; i=0;
while (i < 8) begin Loop body repeat (8) begin
i = i + 1; using i = i + 1;
a = {i[2], i[1], i[0]}; while a = {i[2], i[1], i[0]};
#period; #period;
end end
end end
endmodule
2022-09-17 endmodule 191
While and repeat loop statements
tb.v tb.v
`timescale 1ns/1ps `timescale 1ns/1ps
module tb; module tb;
reg [2:0] a; reg [2:0] a;
wire o; wire o;
demo_5 UUT(a[2], a[1], a[0], o); demo_5 UUT(a[2], a[1], a[0], o);
localparam period = 1; localparam period = 1;
integer i; integer i;
initial begin initial begin
i=0; i=0;
while (i < 8) begin Loop body repeat (8) begin
i = i + 1; using i = i + 1;
a = {i[2], i[1], i[0]}; while a = {i[2], i[1], i[0]};
#period; #period;
end end
end end
endmodule
2022-09-17 endmodule 192
While and repeat loop statements
tb.v tb.v
`timescale 1ns/1ps `timescale 1ns/1ps
module tb; module tb;
reg [2:0] a; reg [2:0] a;
wire o; wire o;
demo_5 UUT(a[2], a[1], a[0], o); demo_5 UUT(a[2], a[1], a[0], o);
localparam period = 1; localparam period = 1;
integer i; integer i;
initial begin initial begin
i=0; i=0;
while (i < 8) begin Loop body repeat (8) begin Loop body
i = i + 1; using i = i + 1; using
a = {i[2], i[1], i[0]}; while a = {i[2], i[1], i[0]}; repeat
#period; #period;
end end
end end
endmodule
2022-09-17 endmodule 193
Verilog operators
2022-09-17 194
Verilog operators
Operator Name
[] Bit-select
~ Bit-Negation
& Bit-AND
| Bit-OR
~& Bit-NAND
~| Bit NOR
^ Bit XOR
~^ or ~^ Bit XNOR
2022-09-17 195
Verilog operators
Operator Name
[] Bit-select
~ Bit-Negation
& Bit-AND
| Bit-OR
~& Bit-NAND
~| Bit NOR
^ Bit XOR
~^ or ~^ Bit XNOR
wire w_reduce;
assign w_reduce = ~&tmp[3:0]
2022-09-17 196
Verilog operators
Operator Name Operator Name
[] Bit-select ! Logic-Negation
~ Bit-Negation < <= >= > Logic-Comparison
& Bit-AND == != Logic-Comparison
| Bit-OR && Logic-AND
~& Bit-NAND || Logic-OR
~| Bit NOR ?: Logic-Conditional
^ Bit XOR
~^ or ~^ Bit XNOR
wire w_reduce;
assign w_reduce = ~&tmp[3:0]
2022-09-17 197
Verilog operators
Operator Name Operator Name
[] Bit-select ! Logic-Negation
~ Bit-Negation < <= >= > Logic-Comparison
& Bit-AND == != Logic-Comparison
| Bit-OR && Logic-AND
~& Bit-NAND || Logic-OR
~| Bit NOR ?: Logic-Conditional
^ Bit XOR Operator Name
~^ or ~^ Bit XNOR {} concatenate
{{}} replication
wire w_reduce;
assign w_reduce = ~&tmp[3:0] + - * / % Arithmetic
2022-09-17 198
Verilog operators
module replication_operator();
Operator Name Operator Name reg [3:0] r_VAL_1 = 4'b0111;
initial
~ Bit-Negation < <= >= > Logic-Comparison begin
// Replication Only
& Bit-AND == != Logic-Comparison $display("Replication of 0x7, 3 times is 0x%h",
{3{r_VAL_1}} );
#20;
{{}} replication $finish;
end
wire w_reduce; endmodule // replication_operator
assign w_reduce = ~&tmp[3:0] + - * / % Arithmetic
2022-09-17 199
Verilog timing control
• There are four types of timing controls in Verilog
1. Delay control
2. Edge sensitive event control
3. Level sensitive event control
4. Named events
2022-09-17 200
Delay control
• # N: delay of N time units
• Example:
Example 1
always begin
a = 1’b0;
#5;
a = 1’b1;
#3;
a=1’b0;
end
2022-09-17 201
Delay control
• # N: delay of N time units
• Example:
Example 1
always begin
a = 1’b0;
#5;
a = 1’b1;
#3;
a=1’b0;
end
2022-09-17 202
Delay control
• # N: delay of N time units
• Example:
Example 1 Example 2
always begin …
a = 1’b0; localparam period = 1;
#5;
a = 1’b1; integer i;
#3; initial begin
a=1’b0; i=0;
end while (i < 8) begin
i = i + 1;
a = {i[2], i[1], i[0]};
#period;
end
end
2022-09-17 203
endmodule
Delay control
• # N: delay of N time units
• Example:
Example 1 Example 2
always begin …
a = 1’b0; localparam period = 1;
#5;
a = 1’b1; integer i;
#3; initial begin
a=1’b0; i=0;
end while (i < 8) begin
i = i + 1;
a = {i[2], i[1], i[0]}; Delay can be controlled
#period; using local var.
end Delay for 1 unit time at
end the end of each loop
2022-09-17
endmodule iteration. 204
Delay control
Example 3
• # N: delay of N time units …
localparam period = 5;
• Example: reg clk;
Example 1 Example 2
always begin … initial begin
a = 1’b0; localparam period = 1; clk=0;
#5; forever begin
a = 1’b1; integer i; #period clk = ~clk;
#3; initial begin end
a=1’b0; i=0; end
end while (i < 8) begin endmodule
i = i + 1;
a = {i[2], i[1], i[0]}; Delay can be controlled
#period; using local var.
end Delay for 1 unit time at
end the end of each loop
2022-09-17
endmodule iteration. 205
Delay control
Clock generator Example 3
• # N: delay of N time units …
localparam period = 5;
• Example: reg clk;
Example 1 Example 2
always begin … initial begin
a = 1’b0; localparam period = 1; clk=0;
#5; forever begin
a = 1’b1; integer i; #period clk = ~clk;
#3; initial begin end
a=1’b0; i=0; end
end while (i < 8) begin endmodule
i = i + 1;
a = {i[2], i[1], i[0]}; Delay can be controlled
#period; using local var.
end Delay for 1 unit time at
end the end of each loop
2022-09-17
endmodule iteration. 206
Delay control
Clock generator Example 3
• # N: delay of N time units …
localparam period = 5;
• Example: reg clk;
Example 1 Example 2
always begin … initial begin
a = 1’b0; localparam period = 1; clk=0;
#5; forever begin
a = 1’b1; integer i; #period clk = ~clk;
#3; initial begin end
a=1’b0; i=0; end
end while (i < 8) begin endmodule
i = i + 1;
a = {i[2], i[1], i[0]}; Delay can be controlled
#period; using local var.
end Delay for 1 unit time at
end the end of each loop
2022-09-17
endmodule iteration. 207
Delay control – Time unit
• `timescale time_unit base / precision base
• The first argument specifies “#1” delay
• The second argument specifies the how delay values are
rounded
• Base is in
• s 1s
• ms 1-3s
• us 1-6s
• ns 1-9s
• ps 1-12s
• fs 1-15s
2022-09-17 208
Delay control – Time unit
• `timescale time_unit base / precision base
• The first argument specifies “#1” delay
• The second argument specifies the how delay values are
rounded
• Base is in Example
• s 1s
• ms 1-3s
• us 1-6s
• ns 1-9s
• ps 1-12s
• fs 1-15s
2022-09-17 209
Delay control – Time unit
• `timescale time_unit base / precision base
• The first argument specifies “#1” delay
• The second argument specifies the how delay values are
rounded
• Base is in Example
• s 1s
• ms 1-3s
• us 1-6s
• ns 1-9s
• ps 1-12s
• fs 1-15s
2022-09-17 210
Delay control – Time unit
• `timescale time_unit base / precision base
• The first argument specifies “#1” delay
• The second argument specifies the how delay values are
rounded
• Base is in Example
• s 1s `timescale 1ns/1ns
• ms 1-3s
• us 1-6s
• ns 1-9s
• ps 1-12s
• fs 1-15s
2022-09-17 211
Delay control – Time unit
• `timescale time_unit base / precision base
• The first argument specifies “#1” delay
• The second argument specifies the how delay values are
rounded
• Base is in Example
• s 1s `timescale 1ns/1ns
• s 1s `timescale 1ns/1ns
• s 1s `timescale 1ns/1ns
• s 1s `timescale 1ns/1ns
• s 1s `timescale 1ns/1ns
• s 1s `timescale 1ns/1ns
2022-09-17 217
Delay control – Gate delay
• So far, we have assumed perfect signal response time
• It takes no time for signal to change from 0 to 1
• It takes no time for signal to change from 1 to 0
• This cannot be true in real life where the electronic properties of gates take
effect
• Three kinds of delays
2022-09-17 218
Delay control – Gate delay
• So far, we have assumed perfect signal response time
• It takes no time for signal to change from 0 to 1
• It takes no time for signal to change from 1 to 0
• This cannot be true in real life where the electronic properties of gates take
effect
• Three kinds of delays
• Rise delay
The time taken for the output of a gate to
change from 0,x,z, to 1 is called a rise delay.
2022-09-17 219
Delay control – Gate delay
• So far, we have assumed perfect signal response time
• It takes no time for signal to change from 0 to 1
• It takes no time for signal to change from 1 to 0
• This cannot be true in real life where the electronic properties of gates take
effect
• Three kinds of delays
• Rise delay
The time taken for the output of a gate to
change from 0,x,z, to 1 is called a rise delay.
2022-09-17 220
Delay control – Gate delay
• So far, we have assumed perfect signal response time
• It takes no time for signal to change from 0 to 1
• It takes no time for signal to change from 1 to 0
• This cannot be true in real life where the electronic properties of gates take
effect
• Three kinds of delays
• Rise delay
The time taken for the output of a gate to
change from 0,x,z, to 1 is called a rise delay.
• Fall delay
The time taken for the output of a gate to
change from 1,x,z, to 0 is called a fall delay.
2022-09-17 221
Delay control – Gate delay
• So far, we have assumed perfect signal response time
• It takes no time for signal to change from 0 to 1
• It takes no time for signal to change from 1 to 0
• This cannot be true in real life where the electronic properties of gates take
effect
• Three kinds of delays
• Rise delay
The time taken for the output of a gate to
change from 0,x,z, to 1 is called a rise delay.
• Fall delay
The time taken for the output of a gate to
change from 1,x,z, to 0 is called a fall delay.
2022-09-17 222
Delay control – Gate delay
• So far, we have assumed perfect signal response time
• It takes no time for signal to change from 0 to 1
• It takes no time for signal to change from 1 to 0
• This cannot be true in real life where the electronic properties of gates take
effect
• Three kinds of delays
• Rise delay
The time taken for the output of a gate to
change from 0,x,z, to 1 is called a rise delay.
• Fall delay
The time taken for the output of a gate to
change from 1,x,z, to 0 is called a fall delay.
• Turn-off delay
The time taken for the output of a gate to
change from 0,1,x, to z is called a turn-off delay.
2022-09-17 223
Delay control – Gate delay
• So far, we have assumed perfect signal response time
• It takes no time for signal to change from 0 to 1
• It takes no time for signal to change from 1 to 0
• This cannot be true in real life where the electronic properties of gates take
effect
• Three kinds of delays
• Rise delay
The time taken for the output of a gate to
change from 0,x,z, to 1 is called a rise delay.
• Fall delay
The time taken for the output of a gate to
change from 1,x,z, to 0 is called a fall delay.
• Turn-off delay
The time taken for the output of a gate to
change from 0,1,x, to z is called a turn-off delay.
2022-09-17 224
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
2022-09-17 225
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
• and and1(o,a,b) // Ideal “and” gate
2022-09-17 226
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
• and and1(o,a,b) // Ideal “and” gate
• and #1 and1(o,a,b) // All delays
2022-09-17 227
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
• and and1(o,a,b) // Ideal “and” gate
• and #1 and1(o,a,b) // All delays
• and #(1,1) and1(o,a,b) // t_rise=1, t_fall=1
2022-09-17 228
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
• and and1(o,a,b) // Ideal “and” gate
• and #1 and1(o,a,b) // All delays
• and #(1,1) and1(o,a,b) // t_rise=1, t_fall=1
• and #(1,2,3) and1(o,a,b) // t_rise=1, t_fall=2, t_off=3
2022-09-17 229
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
• and and1(o,a,b) // Ideal “and” gate
• and #1 and1(o,a,b) // All delays
• and #(1,1) and1(o,a,b) // t_rise=1, t_fall=1
• and #(1,2,3) and1(o,a,b) // t_rise=1, t_fall=2, t_off=3
• and #(1:2:3,2:3:4,3:4:5) and1(o,a,b) // min:typ:max
2022-09-17 230
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
• and and1(o,a,b) // Ideal “and” gate
• and #1 and1(o,a,b) // All delays
• and #(1,1) and1(o,a,b) // t_rise=1, t_fall=1
• and #(1,2,3) and1(o,a,b) // t_rise=1, t_fall=2, t_off=3
• and #(1:2:3,2:3:4,3:4:5) and1(o,a,b) // min:typ:max
Min Typ Max
t_rise t_fall t_max t_rise t_fall t_max t_rise t_fall t_max
1 2 3 2 3 4 3 4 5
2022-09-17 231
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
• and and1(o,a,b) // Ideal “and” gate
• and #1 and1(o,a,b) // All delays
• and #(1,1) and1(o,a,b) // t_rise=1, t_fall=1
• and #(1,2,3) and1(o,a,b) // t_rise=1, t_fall=2, t_off=3
• and #(1:2:3,2:3:4,3:4:5) and1(o,a,b) // min:typ:max
Min Typ Max
t_rise t_fall t_max t_rise t_fall t_max t_rise t_fall t_max
1 2 3 2 3 4 3 4 5
2022-09-17 232
Delay control – Gate delay
• Verilog uses # to control rise/fall/turn-off delay of signals
• Let’t talk about gate delay of an “and” gate
• and and1(o,a,b) // Ideal “and” gate
• and #1 and1(o,a,b) // All delays
• and #(1,1) and1(o,a,b) // t_rise=1, t_fall=1
• and #(1,2,3) and1(o,a,b) // t_rise=1, t_fall=2, t_off=3
• and #(1:2:3,2:3:4,3:4:5) and1(o,a,b) // min:typ:max
Min Typ Max
t_rise t_fall t_max t_rise t_fall t_max t_rise t_fall t_max
1 2 3 2 3 4 3 4 5
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 235
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 236
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 237
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 238
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 239
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 240
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 241
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 242
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
module tb;
reg a,b;
wire out1, out2;
demo_6 UUT(a, b, out1, out2);
initial begin
{a, b} <= 0;
#10 a <= 1;
#10 b <= 1;
#10 a <= 0;
#10 b <= 0;
end
endmodule
2022-09-17 243
Gate delay example Pre-synthesis behavioral simulation
module demo_6( input a, b, output out1, out2);
and #(1:2:3, 2:3:4) and1 (out1, a, b);
and and2 (out2, a, b);
endmodule
2022-09-17 260
Edge sensitive event control
• A statement happens only when the posedge and/or negedge
event happens
2022-09-17 261
Edge sensitive event control
• A statement happens only when the posedge and/or negedge
event happens
1. Can be attached to sensitivity list of an “always” block
always @ (posedge clk);
2022-09-17 262
Edge sensitive event control
• A statement happens only when the posedge and/or negedge
event happens
1. Can be attached to sensitivity list of an “always” block
always @ (posedge clk);
Execute the always block at each rising edge of signal “clk”
2022-09-17 263
Edge sensitive event control
• A statement happens only when the posedge and/or negedge
event happens
1. Can be attached to sensitivity list of an “always” block
always @ (posedge clk);
Execute the always block at each rising edge of signal “clk”
2. Or to individual assignments
@ (posedge clk) X <= Y;
2022-09-17 264
Edge sensitive event control
• A statement happens only when the posedge and/or negedge
event happens
1. Can be attached to sensitivity list of an “always” block
always @ (posedge clk);
Execute the always block at each rising edge of signal “clk”
2. Or to individual assignments
@ (posedge clk) X <= Y;
Delay assignment X<=Y until the positive edge of “clk”.
X <= @ (posedge clk) Y;
2022-09-17 265
Edge sensitive event control
• A statement happens only when the posedge and/or negedge
event happens
1. Can be attached to sensitivity list of an “always” block
always @ (posedge clk);
Execute the always block at each rising edge of signal “clk”
2. Or to individual assignments
@ (posedge clk) X <= Y;
Delay assignment X<=Y until the positive edge of “clk”.
X <= @ (posedge clk) Y;
Read y immediately but delay the assignment to posedge “clk”.
2022-09-17 266
Edge sensitive event control
• A statement happens only when the posedge and/or negedge
event happens
1. Can be attached to sensitivity list of an “always” block
always @ (posedge clk);
Execute the always block at each rising edge of signal “clk”
2. Or to individual assignments
@ (posedge clk) X <= Y;
Delay assignment X<=Y until the positive edge of “clk”.
X <= @ (posedge clk) Y;
Read y immediately but delay the assignment to posedge “clk”.
3. At positive and/or negative edges
@(posedge x and negedge y)
@(posedge x or negedge y)
2022-09-17 267
Edge sensitive event control
module tb; //demo_7 module tb; //demo_7
2022-09-17 273
Example of wait()
module tb;
reg [3:0] ctr;
reg clk;
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish;
end
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish;
end
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish;
end
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish;
end
2022-09-17 282
endmodule
Example of wait()
module tb;
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
2022-09-17
Blocked until 283
endmodule posedge clk.
Example of wait()
module tb;
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
2022-09-17
Blocked until 284
endmodule posedge clk.
Example of wait()
module tb; Initial block first
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
2022-09-17
Blocked until 285
endmodule posedge clk.
Example of wait()
module tb; Initial block first
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT.
always @ (posedge clk) Executed at
ctr <= ctr + 1; simulation time 0.
2022-09-17
Blocked until 286
endmodule posedge clk.
Example of wait()
module tb; Initial block first
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT.
always @ (posedge clk) Executed at
ctr <= ctr + 1; simulation time 0.
2022-09-17
Blocked until 287
endmodule posedge clk.
Example of wait()
module tb; Initial block first
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT.
always @ (posedge clk) Executed at
ctr <= ctr + 1; simulation time 0.
2022-09-17
Blocked until 10 UT: ctr = 1 288
endmodule posedge clk. Wait for posedge.
Example of wait()
module tb; Initial block first
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0
initial begin
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT.
always @ (posedge clk) Executed at
ctr <= ctr + 1; simulation time 0.
2022-09-17
Blocked until 10 UT: ctr = 1 289
endmodule posedge clk. Wait for posedge.
Example of wait()
module tb; Initial block first
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the
first $display.
initial begin Blocked until crt is 4
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT.
always @ (posedge clk) Executed at
ctr <= ctr + 1; simulation time 0.
2022-09-17
Blocked until 10 UT: ctr = 1 290
endmodule posedge clk. Wait for posedge.
Example of wait()
module tb; Initial block first
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the
first $display.
initial begin Blocked until crt is 4
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT.
always @ (posedge clk) Executed at
ctr <= ctr + 1; simulation time 0.
2022-09-17
Blocked until 10 UT: ctr = 1 291
endmodule posedge clk. Wait for posedge.
Example of wait()
module tb; Initial block first Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the
first $display.
initial begin Blocked until crt is 4
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT.
always @ (posedge clk) Executed at
ctr <= ctr + 1; simulation time 0.
2022-09-17
Blocked until 10 UT: ctr = 1 292
endmodule posedge clk. Wait for posedge.
Example of wait()
module tb; Initial block first Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the
first $display.
initial begin Blocked until crt is 4
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at
ctr <= ctr + 1; simulation time 0.
2022-09-17
Blocked until 10 UT: ctr = 1 293
endmodule posedge clk. Wait for posedge.
Example of wait()
module tb; Initial block first Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the
first $display.
initial begin Blocked until crt is 4
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 294
endmodule posedge clk. Wait for posedge.
Example of wait()
module tb; Initial block first Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the
first $display.
initial begin Blocked until crt is 4
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 295
endmodule posedge clk. Wait for posedge.
Example of wait()
module tb; Initial block first Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the
first $display.
initial begin Blocked until crt is 4
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:296
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Example of wait()
module tb; Initial block first Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the
first $display.
initial begin Blocked until crt is 4
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:297
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Example of wait()
module tb; Initial block first Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the 70 UT: execute the
first $display. second $display.
initial begin Blocked until crt is 4 Simulation finishes.
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:298
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Example of wait()
module tb; Initial block first Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the 70 UT: execute the
first $display. second $display.
initial begin Blocked until crt is 4 Simulation finishes.
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:299
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Example of wait()
module tb; Initial block first Trigger order Trigger order
reg [3:0] ctr;
reg clk; Executed at simulation
time 0. {ctr, clk} <=0 10 UT: execute the 70 UT: execute the
first $display. second $display.
initial begin Blocked until crt is 4 Simulation finishes.
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:300
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Example of wait()
module tb; Initial block first Trigger order Trigger order
reg [3:0] ctr;
A hideous Executed at simulation
reg clk; 10 UT: execute the 70 UT: execute the
bug here time 0. {ctr, clk} <=0
first $display. second $display.
initial begin Blocked until crt is 4 Simulation finishes.
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:301
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Example of wait()
module tb; Initial block first Trigger order Trigger order
reg [3:0] ctr;
A hideous Executed at simulation
reg clk; 10 UT: execute the 70 UT: execute the
bug here time 0. {ctr, clk} <=0
first $display. second $display.
initial begin Blocked until crt is 4 Simulation finishes.
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
$display ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
wait (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:302
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Example of wait()
module tb; Initial block first Trigger order Trigger order
reg [3:0] ctr;
A hideous Executed at simulation
reg clk; 10 UT: execute the 70 UT: execute the
bug here time 0. {ctr, clk} <=0
first $display. second $display.
initial begin Blocked until crt is 4 Simulation finishes.
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
Should$display
be ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
=, notwait
<= (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:303
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Example of wait()
module tb; Initial block first Trigger order Trigger order
reg [3:0] ctr;
A hideous Executed at simulation
reg clk; 10 UT: execute the 70 UT: execute the
bug here time 0. {ctr, clk} <=0
first $display. second $display.
initial begin Blocked until crt is 4 Simulation finishes.
{ctr, clk} <= {4’b0000, 1’b0};
wait (ctr);
Should$display
be ("T=%0t Counter reached non-zero value 0x%0h", $time, ctr);
=, notwait
<= (ctr == 4’b0100)
$display ("T=%0t Counter reached 0x%0h", $time, ctr);
$finish; Executed at simulation
end time 0.
10 UT: clk = 1 20 UT: clk = 0
always clk = #10 ~clk; Blocked for 10 UT.
Blocked for 10 UT. Blocked for 10 UT.
always @ (posedge clk) Executed at 70 UT: clk = 1
ctr <= ctr + 1; simulation time 0. Blocked for 10 UT.
2022-09-17
Blocked until 10 UT: ctr = 1 70 UT:304
ctr = 4
endmodule posedge clk. Wait for posedge. Blocked for 10 UT.
Verilog function vs task
• Verilog provides function and task for code reuse.
• Help to improve reusability readability in large projects.
• They are similar but differ in many aspects.
2022-09-17 305
Verilog function vs task
• Verilog provides Function
function and task for code reuse. Task
1 Do not allow initial or always block.
• Help to improve reusability readability in large projects.
• They are similar but differ in many aspects.
2022-09-17 306
Verilog function vs task
• Verilog provides Function function and task for code reuse. Task
1 Do not allow initial or always block.
• Help
2 A to improve
function reusability
takes 0 simulation readability
time to execute. intakes
A task large projects.
arbitrary simulation time to execute
depending on the timing control statements within.
• They are similar but differ in many aspects.
2022-09-17 307
Verilog function vs task
• Verilog provides Function function and task for code reuse. Task
1 Do not allow initial or always block.
• Help
2 A to improve
function reusability
takes 0 simulation readability
time to execute. intakes
A task large projects.
arbitrary simulation time to execute
depending on the timing control statements within.
• They
3
are similar but differ in many aspects.
A function cannot contain timing control statements A task can contain timing control statements.
such as: # delay, posedge, negedge, etc.
2022-09-17 308
Verilog function vs task
• Verilog provides Function function and task for code reuse. Task
1 Do not allow initial or always block.
• Help
2 A to improve
function reusability
takes 0 simulation readability
time to execute. intakes
A task large projects.
arbitrary simulation time to execute
depending on the timing control statements within.
• They
3
are similar but differ in many aspects.
A function cannot contain timing control statements A task can contain timing control statements.
such as: # delay, posedge, negedge, etc.
4 A function can call other functions but can not call A task can call other functions or tasks.
tasks. (Because of rule 2 and 3)
(Because of Rule 2 & 3)
2022-09-17 309
Verilog function vs task
• Verilog provides Function function and task for code reuse. Task
1 Do not allow initial or always block.
• Help
2 A to improve
function reusability
takes 0 simulation readability
time to execute. intakes
A task large projects.
arbitrary simulation time to execute
depending on the timing control statements within.
• They
3
are similar but differ in many aspects.
A function cannot contain timing control statements A task can contain timing control statements.
such as: # delay, posedge, negedge, etc.
4 A function can call other functions but can not call A task can call other functions or tasks.
tasks. (Because of rule 2 and 3)
(Because of Rule 2 & 3)
5 A function must have at least one input argument A task can have any number (zero or more) of
but cannot have output arguments. input and output ports.
2022-09-17 310
Verilog function vs task
• Verilog provides Function function and task for code reuse. Task
1 Do not allow initial or always block.
• Help
2 A to improve
function reusability
takes 0 simulation readability
time to execute. intakes
A task large projects.
arbitrary simulation time to execute
depending on the timing control statements within.
• They
3
are similar but differ in many aspects.
A function cannot contain timing control statements A task can contain timing control statements.
such as: # delay, posedge, negedge, etc.
4 A function can call other functions but can not call A task can call other functions or tasks.
tasks. (Because of rule 2 and 3)
(Because of Rule 2 & 3)
5 A function must have at least one input argument A task can have any number (zero or more) of
but cannot have output arguments. input and output ports.
6 A function can return a single value. A task cannot return a value but can achieve the
same effect using output arguments.
2022-09-17 311
Verilog function vs task
• Verilog provides Function function and task for code reuse. Task
1 Do not allow initial or always block.
• Help
2 A to improve
function reusability
takes 0 simulation readability
time to execute. intakes
A task large projects.
arbitrary simulation time to execute
depending on the timing control statements within.
• They
3
are similar but differ in many aspects.
A function cannot contain timing control statements A task can contain timing control statements.
such as: # delay, posedge, negedge, etc.
4 A function can call other functions but can not call A task can call other functions or tasks.
tasks. (Because of rule 2 and 3)
(Because of Rule 2 & 3)
5 A function must have at least one input argument A task can have any number (zero or more) of
but cannot have output arguments. input and output ports.
6 A function can return a single value. A task cannot return a value but can achieve the
same effect using output arguments.
7 A function can be called in an expression or can be A task must be specifically called with a statement.
called with a statement (disconnected function). (Because of Rule 5 and 6)
2022-09-17 (Because of Rule 5 and 6) 312
Verilog function vs task
• Verilog provides Function function and task for code reuse. Task
1 Do not allow initial or always block.
• Help
2 A to improve
function reusability
takes 0 simulation readability
time to execute. intakes
A task large projects.
arbitrary simulation time to execute
depending on the timing control statements within.
• They
3
are
Rules
similar but
of
differ
thumb
in many
No.6:
aspects.
A function cannot contain timing control statements A task can contain timing control statements.
such as: # delay, posedge, negedge, etc.
4 Usecanfunction
A function to
call other functions but can model
not call A taskcombi-logic.
can call other functions or tasks.
6 A function can return a single value. A task cannot return a value but can achieve the
same effect using output arguments.
7 A function can be called in an expression or can be A task must be specifically called with a statement.
called with a statement (disconnected function). (Because of Rule 5 and 6)
2022-09-17 (Because of Rule 5 and 6) 313
module has_function (x_in, rev_x); //demo_9 module has_task (x_in, rev_x); //demo_9
parameter maxbits = 8; parameter maxbits = 8;
integer k; integer k;
begin begin
for (k=0; k < maxbits; k = k +1) for (k=0; k < maxbits; k = k +1)
reverse_bits[maxbits-1-k] = din[k]; dout[maxbits-1-k] = din[k];
end end
endfunction endtask
endmodule endmodule
2022-09-17 314
module has_function (x_in, rev_x); //demo_9 module has_task (x_in, rev_x); //demo_9
parameter maxbits = 8; parameter maxbits = 8;
integer k; integer k;
begin begin
for (k=0; k < maxbits; k = k +1) for (k=0; k < maxbits; k = k +1)
reverse_bits[maxbits-1-k] = din[k]; dout[maxbits-1-k] = din[k];
end end
endfunction endtask
endmodule endmodule
2022-09-17 315
module has_function (x_in, rev_x); //demo_9 module has_task (x_in, rev_x); //demo_9
parameter maxbits = 8; parameter maxbits = 8;
endmodule endmodule
2022-09-17 316
module has_function (x_in, rev_x); //demo_9 module has_task (x_in, rev_x); //demo_9
parameter maxbits = 8; parameter maxbits = 8;
endmodule endmodule
2022-09-17 317
module has_function (x_in, rev_x); //demo_9 module has_task (x_in, rev_x); //demo_9
parameter maxbits = 8; parameter maxbits = 8;
endmodule endmodule
2022-09-17 318
Questions?
2022-09-17 319