chapter 4: behavioral modeling - wiley.com 4: behavioral modeling digital system designs and...

55
Chapter 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral Modeling Department of Electronic Engineering National Taiwan University of Science and Technology Prof. Ming-Bo Lin

Upload: phunghuong

Post on 15-Mar-2018

231 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1

Chapter 4: Behavioral Modeling

Department of Electronic Engineering

National Taiwan University of Science and Technology

Prof. Ming-Bo Lin

Page 2: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-2

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming controlSelection statementsIterative (loop) statements

Page 3: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-3

ObjectivesAfter completing this chapter, you will be able to:

Describe the behavioral modeling structuresDescribe procedural constructsUnderstand the features of initial blocksUnderstand the features of always blocksDistinguish the differences between blocking and nonblocking assignmentsUnderstand the features of timing controlsUnderstand the features of selection constructsUnderstand the features of loop constructs

Page 4: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-4

Syllabus

ObjectivesProcedural constructs

initial blockalways block

Procedural assignmentsTiming controlSelection statementsIterative (loop) statements

Page 5: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-5

An initial BlockExecutes exactly once during simulationStarts at simulation time 0

reg x, y, z;initial begin // complex statement

x = 1`b0; y = 1`b1; z = 1`b0;#10 x = 1`b1; y = 1`b1; z = 1`b1;endinitial x = 1`b0; // single statement

Page 6: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-6

Syllabus

ObjectivesProcedural constructs

initial blockalways block

Procedural assignmentsTiming controlSelection statementsIterative (loop) statements

Page 7: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-7

An always block

Starts at simulation time 0Executes continuously during simulation

reg clock; initial clock = 1`b0;

always #5 clock = ~clock;

Page 8: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-8

Syllabus

ObjectivesProcedural constructsProcedural assignments

General syntaxBlocking assignmentsNonblocking assignmentsBlocking vs. nonblocking assignments

Timing controlSelection statementsIterative (loop) statements

Page 9: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-9

Procedural Assignments

Syntaxvariable_lvalue = [timing_control] expressionvariable_lvalue <= [timing_control] expression[timing_control] variable_lvalue = expression[timing_control] variable_lvalue <= expression

Two typesblockingnonblocking

Page 10: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-10

Procedural Assignments

The bit widthsKeeping the least significant bitsZero-extended in the most significant bits

Page 11: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-11

Syllabus

ObjectivesProcedural constructsProcedural assignments

General syntaxBlocking assignmentsNonblocking assignmentsBlocking vs. nonblocking assignments

Timing controlSelection statementsIterative (loop) statements

Page 12: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-12

Blocking Assignments

Are executed in the specified order Use the “=“ operator

// blocking assignmentsinitial begin

x = #5 1'b0; // at time 5y = #3 1'b1; // at time 8z = #6 1'b0; // at time 14

end

Page 13: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-13

Blocking Assignments

output reg [3:0] sum; output reg c_out;reg [3:0] t;

always @(x, y, c_in) begin t = y ^ {4{c_in}};{c_out, sum} = x + t + c_in;

end

Q: What is wrong with: t = y ^ c_in ?Q: Does a reg variable correspond to a memory element to be synthesized?

Page 14: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-14

Syllabus

ObjectivesProcedural constructsProcedural assignments

General syntaxBlocking assignmentsNonblocking assignmentsBlocking vs. nonblocking assignments

Timing controlSelection statementsIterative (loop) statements

Page 15: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-15

Nonblocking Assignments

Use the <= operatorUsed to model several concurrent data transfers

reg x, y, z;// nonblocking assignmentsinitial begin

x <= #5 1'b0; // at time 5y <= #3 1'b1; // at time 3z <= #6 1'b0; // at time 6

end

Page 16: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-16

Nonblocking Assignments

input clk;input din;output reg [3:0] qout;// a 4-bit shift registeralways @(posedge clk)

qout <= {din, qout[3:1]}; // Right shift

Page 17: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-17

Syllabus

ObjectivesProcedural constructsProcedural assignments

General syntaxBlocking assignmentsNonblocking assignmentsBlocking vs. nonblocking assignments

Timing controlSelection statementsIterative (loop) statements

Page 18: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-18

Race Conditions

// using blocking assignment statementsalways @(posedge clock) // has race condition

x = y;always @(posedge clock)

y = x;

// using nonblocking assignment statementsalways @(posedge clock) // has no race condition

x <= y;always @(posedge clock)

y <= x;

Page 19: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-19

Execution of Nonblocking Statements

ReadEvaluate and scheduleAssign

Page 20: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-20

Blocking vs. Nonblocking Assignments

// A 4-bit shift registeralways @(posedge clk) begin

qout[0] = sin;qout[1] = qout[0];qout[2] = qout[1];qout[3] = qout[2];

end

Page 21: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-21

Blocking vs. Nonblocking Assignments// A 4-bit shift registeralways @(posedge clk) begin

qout[0] <= sin; qout[1] <= qout[0]; qout[2] <= qout[1];qout[3] <= qout[2];

end

Page 22: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-22

Blocking vs. Nonblocking Assignments

Suppose that count is 1 and finish is 0 before entering the always blockalways @(posedge clk) begin: block_a

count = count – 1;if (count == 0) finish = 1;

end

always @(posedge clk) begin: block_bcount <= count – 1;if (count == 0) finish <= 1;

end

Result: finish = 1. (Different from that of gate-level.)

Result: finish = 0. (Same as that of gate-level.)

Page 23: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-23

Coding Styles

blocking operators (=)combinational logic

nonblocking operators (<=)sequential logic

Page 24: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-24

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming control

Delay timing controlEvent timing control

Selection statementsIterative (loop) statements

Page 25: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-25

Delay Timing Control

Delay timing controlRegular delay controlIntra assignment delay control

Page 26: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-26

Regular Delay Control

Regular delay controlDefers the execution of the entire statement

reg x, y;integer count;

#25 y <= ~x; // at time 25#15 count <= count + 1; // at time 40

Page 27: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-27

Intra-Assignment Delay Control

Intra-assignment delay controlDefers the assignment to the left-hand-side variable

y = #25 ~x; // assign to y at time 25count = #15 count + 1; // assign to count at time 40

y <= #25 ~x; // assign to y at time 25count <= #15 count + 1; // assign to count at time 15

Page 28: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-28

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming control

Delay timing controlEvent timing control

Selection statementsIterative (loop) statements

Page 29: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-29

Event Timing Control

Edge-triggered event controlNamed event controlEvent or control

Level-sensitive event control

Q: What is an event?

Page 30: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-30

Edge-Triggered Event Control

Edge-triggered event control@(posedge clock)@(negedge clock)

always @(posedge clock) beginreg1 <= #25 in_1;reg2 <= @(negedge clock) in_2 ^ in_3; reg3 <= in_1;

end

Page 31: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-31

Named Event Control

A named event

event received_data; // declarealways @(posedge clock)

if (last_byte) -> received_data; // trigger always @(received_data) // recognize

begin ... end

Page 32: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-32

Event or Control

Event or controlor,* or (*) means any signals

always @(posedge clock or negedge reset_n) if (!reset_n) q <= 1`b0; else q <= d;

Page 33: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-33

Level-Sensitive Event Control

Level-sensitive event control

always wait (count_enable) count = count –1 ;

always wait (count_enable) #10 count = count –1 ;

Page 34: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-34

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming controlSelection statements

if…else statementcase (casex/casez) statement

Iterative (loop) statements

Page 35: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-35

if…else Statement

Syntaxif (<expression>) true_statement ;

if (<expression>) true_statement; else false_statement;

if (<expression1>) true_statement1; else if (<expression2>) true_statement2; else false_statement;

Page 36: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-36

if…else Statement

// using if…else statementalways @(*) begin

if (s1) beginif (s0) out = i3; else out = i2; end

else beginif (s0) out = i1; else out = i0; end

end

Page 37: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-37

A Simple 4-bit Counter// the body of the 4-bit counter.always @(negedge clock or posedge clear)

if (clear)qout <= 4'd0;

else qout <= (qout + 1) ; // qout = (qout + 1) % 16;

Page 38: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-38

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming controlSelection statements

if…else statementcase (casex/casez) statement

Iterative (loop) statements

Page 39: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-39

case (casex/casez) Statement

Syntaxcase (case_expression)

case_item1 {,case_item1}: procedural_statement1case_item2 {,case_item2}: procedural_statement2...case_itemn {,case_itemn}: procedural_statementn[default: procedural_statement]

endcase

Page 40: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-40

A 4-to-1 MUX Example

always @(I0 or I1 or I2 or I3 or S) case (S)

2'b00: Y = I0; 2'b01: Y = I1;2'b10: Y = I2; 2'b11: Y = I3;

endcase

Q: Model a 3-to-1 multiplexer.

Page 41: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-41

casex and casez Statements

casex and casez statementsCompare only non-x or z positions

casez treats all z values as don’t carescasex treats all x and z values as don’t cares

Page 42: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-42

casex and casez Statements

// count the trailing zeros in a nibblealways @(data)

casex (data) 4'bxxx1: out = 0;4'bxx10: out = 1;4'bx100: out = 2;4'b1000: out = 3;4'b0000: out = 4;default: out = 3'b111;

endcase

Q: Is the default statement necessary?

Page 43: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-43

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming controlSelection statementsIterative (loop) statements

while loop for loop repeat loop forever loop

Page 44: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-44

while Loop

Syntax

while (condition_expr) statement;

while (count < 12) count <= count + 1;while (count <= 100 && flag) begin

// put statements wanted to be carried out hereend

Page 45: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-45

while Loop

// count the zeros in a byteinteger i;always @(data) begin

out = 0; i = 0;while (i <= 7) begin // simple condition

if (data[i] == 0) out = out + 1; i = i + 1;

end end

Q: Can the if … be replaced with out = out + ~data[i] ?

Page 46: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-46

while Loop

// count the trailing zeros in a byteinteger i; always @(data) begin

out = 0; i = 0;while (data[i] == 0 && i <= 7) begin // complex condition

out = out + 1;i = i + 1;

endend

Page 47: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-47

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming controlSelection statementsIterative (loop) statements

while loop for looprepeat loop forever loop

Page 48: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-48

for Loop

Syntax

for (init_expr; condition_expr; update_expr) statement;

init_expr;while (condition_expr) begin

statement;update_expr;

end

Page 49: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-49

for Loop

// count the zeros in a byteinteger i;always @(data) begin

out = 0;for (i = 0; i <= 7; i = i + 1) // simple condition

if (data[i] == 0) out = out + 1; end

Page 50: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-50

for Loop

// count the trailing zeros in a byteinteger i; always @(data) begin

out = 0;for (i = 0; data[i] == 0 && i <= 7; i = i + 1)

out = out + 1;end

Page 51: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-51

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming controlSelection statementsIterative (loop) statements

while loop for loop repeat loopforever loop

Page 52: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-52

repeat Loop

Syntax repeat (counter_expr) statement;

i = 0;repeat (32) begin

state[i] = 0; i = i + 1;

endrepeat (cycles) begin

@(posedge clock) buffer[i] <= data; i <= i + 1;

end

Page 53: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-53

Syllabus

ObjectivesProcedural constructsProcedural assignmentsTiming controlSelection statementsIterative (loop) statements

while loop for loop repeat loop forever loop

Page 54: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-54

forever Loop

Syntax forever statement;

initial beginclock <= 0;forever begin

#10 clock <= 1; #5 clock <= 0;

endend

Page 55: Chapter 4: Behavioral Modeling - wiley.com 4: Behavioral Modeling Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-1 Chapter 4: Behavioral

Chapter 4: Behavioral Modeling

Digital System Designs and Practices Using Verilog HDL and FPGAs @ 2008~2010, John Wiley 4-55

The forever Loop Structure

reg clock, x, y;

initialforever @(posedge clock) x <= y;