18EC56 Verilog HDL Module 4a 2020
18EC56 Verilog HDL Module 4a 2020
18EC56 Verilog HDL Module 4a 2020
Module 4(a)
Behavioral Modeling
Dinesh M.A.
dineshma_ece@mitmysore.in
•
/dinesh.ajay
/prof.dineshma
VISION OF THE DEPARTMENT
• Dataflow modeling
• Boolean function assigned to a net
• Starts at time 0
• Executes only once during a simulation
• Multiple initial blocks, execute in parallel
• All start at time 0
• Each finishes independently
• Syntax:
initial
begin
// behavioral statements
end
Verilog HDL 03/06/2023 12
Structured Procedures:
initial statement (cont’d)
• Example:
module stimulus; initial
reg x, y, a, b, m; #50 $finish;
endmodule
initial
m= 1’b0;
initial
begin
#5 a=1’b1;
#25 b=1’b0;
end
initial
begin
#10 x=1’b0;
#25 y=1’b1;
end
initial
What happens if such a
#1000 $finish;
$finish is not included?
endmodule
Verilog HDL 03/06/2023 14
Procedural Assignments
• Syntax
• <lvalue> = <expression>
• <lvalue> can be
• reg, integer, real, time
• A bit-select of the above (e.g., addr[0])
• A part-select of the above (e.g., addr[31:16])
• A concatenation of any of the above
• <expression> is the same as introduced in dataflow modeling
• What happens if the widths do not match?
• LHS wider than RHS => RHS is zero-extended
• RHS wider than LHS => RHS is truncated (Least significant part is kept)
• Race condition
• When the final result of simulating two (or more) concurrent processes depends on their order of execution
• Example:
always @(posedge clock)
b = a;
always @(posedge clock)
a = b;
• Solution: always @(posedge clock)
always @(posedge clock) begin
b <= a; temp_b = b;
always @(posedge clock) temp_a = a;
a <= b; b = temp_a;
a = temp_b;
end
• Recommendation
• Concurrent data transfers => race condition
• Use non-blocking assignments wherever concurrent data transfers
• Example: pipeline modeling
• Disadvantage:
• Lower simulation performance
• Higher memory usage in the simulator
• Intra-
assignment
delay
examples
• Zero-Delay Control
• Symbol: #0
• Different initial/always blocks in the same simulation time
• Execution order non-deterministic
• Zero-delay ensures execution after all other statements
• Eliminates race conditions
• Multiple zero-delay statements
• Non-deterministic execution order
• Zero-delay control
examples
• Event
• Change in the value of a register or net
• Used to trigger execution of a statement or block (reactive behavior/reactivity)
• Types of Event-based timing control
1. Regular event control
2. Named event control
3. Event OR control
4. Level-sensitive timing control (next section)
• Named
event
control
examples
if (<expression>) true_statement;
else false_statement;
if (<expression>) true_statement1;
else if (<expression>) true_statement2;
else if (<expression>) true_statement3;
else default_statement;
• True is 1 or non-zero
• False is 0 or ambiguous (x or z)
• More than one statement: begin end
Verilog HDL 03/06/2023 36
Conditional Statements (cont’d)
• Examples: • Examples:
if (!lock) buffer = data;
if (alu_control==0)
if (enable) out = in; y = x+z;
else if (alu_control==1)
if (number_queued < MAX_Q_DEPTH) y = x-z;
begin else if (alu_control==2)
data_queue = data; y = x*z;
number_queued = number_queued +1; else
end $display(“Invalid ALU control
signal.”);
else $display(“Queue full! Try
again.”);
• Examples:
reg [1:0] alu_control;
...
case (alu_control)
2’d0: y = x + z;
2’d1: y = x – z;
2’d2: y = x * z;
default: $display(“Invalid ALU control signal.”);
• Example 2:
module mux4_to_1(out, i0, i1, i2, i3, s1, s0);
output out;
input i0, i1, i2, i3, s1, s0;
reg out;
• Loops in Verilog
• while, for, repeat, forever
• Equivalent to while(1)
• Parallel Blocks
• Keywords: fork, join
• Statements in the blocks are executed concurrently
• Timing controls specify the order of execution of the statements
• All delays are relative to the time the block was entered
• The written order of statements is not important
initial
begin
x=1’b0;
#5 y=1’b1;
#10 z={x,y};
#20 w={y,x};
end
initial
fork
x=1’b0;
#5 y=1’b1;
#10 z={x,y};
Verilog HDL 03/06/2023 49
#20 w={y,x};
join
Sequential and Parallel Blocks (cont’d)
• Contents
• Nested blocks
• Named blocks
• Disabling named blocks
• Nested blocks
• Sequential and parallel blocks can be mixed
initial
begin
x=1’b0;
fork
#5 y=1’b1;
#10 z={x,y};
join
#20 w={y,x};
end
• Named blocks
• Syntax:
begin: <the_name> fork: <the_name>
… …
end join
• Advantages:
• Can have local variables
• Are part of the design hierarchy.
• Their local variables can be accessed using hierarchical names
• Can be disabled
module top;
initial
begin : block1
integer i; //hiera. name: top.block1.i
…
end
initial
fork : block2
reg i; //hierarchical name: top.block2.i
…
join
endmodule
• Can generate
• Modules
• User defined primitives
• Verilog gate primitives
• Continuous assignments
• initial and always blocks
Generate Loop
module bitwise_xor (output [N-1:0] out, input [N-1:0] i0, i1);
parameter N = 32; // 32-bit bus by default
genvar j; // This variable does not exist during simulation
genvar i;
generate for (i=0; i<N; i=i+1) begin: r_loop
wire t1, t2, t3;
xor g1 (t1, a0[i], a1[i]);
xor g2 (sum[i], t1, carry[i]);
and g3 (t2, a0[i], a1[i]);
and g4 (t3, t1, carry[i]);
or g5 (carry[i+1], t2, t3);
end
endgenerate //end of the generate block
assign co = carry[N];
endmodule
Generate Conditional
generate
if (a0_width <8) || (a1_width < 8)
cla_multiplier #(a0_width, a1_width) m0 (product, a0, a1);
else
tree_multiplier #(a0_width, a1_width) m0 (product, a0, a1);
endgenerate
endmodule
Generate Case
module adder(output co, output [N-1:0] sum,
input [N-1:0] a0, a1, input ci);
parameter N = 4;
endmodule
Behavioral Modeling
Examples
4-to-1 Multiplexer
// 4-to-1 multiplexer. Port list is taken exactly from
// the I/O diagram.
module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
// I/O ports
output [3:0] Q;
input clock, clear;
//output defined as register
reg [3:0] Q;
endmodule
Verilog HDL 03/06/2023 64
4-bit Down Counter
//Binary counter
module counter(Q , clock, clear);
// I/O ports
output [3:0] Q;
input clock, clear;
//output defined as register
reg [3:0] Q;
Q = (Q - 1) ;
end
endmodule
Verilog HDL 03/06/2023 65
Traffic Signal Controller
//Delays
`define Y2RDELAY 3 //Yellow to red delay
`define R2GDELAY 2 //Red to Green Delay
//I/O ports
output [1:0] hwy, cntry; //2 bit output for 3 states of signal GREEN, YELLOW, RED;
reg [1:0] hwy, cntry; //declare output signals are registers
input X; //if TRUE, indicates that there is car on the country road, otherwise FALSE