FPGA Based System Design PPT End Sem - Student - Updated
FPGA Based System Design PPT End Sem - Student - Updated
Text Book
Book1 -Samir Palnitkar, “Verilog HDL”, First Edition, Printice Hall India Private Limited,2003
Book2-Wayne Wolf, “FPGA-Based System Design”, First Edition, Prentice Hall India Private Limited,2004
Book3-Clive Maxfield, “The Design Warrior's Guide to FPGAs”, Newnes, 2004.
• Book1- (Samir Palnitkar, “Verilog HDL”, First Edition, Printice Hall India Private Limited,2003)
• Chapter 1, 2,3, 4, 5,6, 7
• Chapter 14.1, 14.3, 14.6.1
• Book2- (Wayne Wolf, “FPGA-Based System Design”, First Edition, Prentice Hall India Private Limited,2004)
• Chapter- 3.2, 3.3, 3.3.2, 3.4, 3.4.1, 3.4.2, 3.4.3
3.6.1, 4.7.1, 4.7.2 ( Only refer slide)
• Book3- (Clive Maxfield, “The Design Warrior's Guide to FPGAs”, Newnes, 2004)
Chapter-14
Slide number only for understanding
• 455, 458, 461, 463, 465, 469, 471, 473, 488, 491, 494, 496, 498 and
501.
1.1 Evolution of Computer Aided Digital Design
• VLSI systems are much smaller and consume less power than the
discrete components used to build electronic systems before the
1960s.
• Digital circuit design has evolved rapidly over the last 25 years. The
earliest digital circuits were designed with vacuum tubes and
transistors
• Integrated circuits were then invented where logic gates were placed
on a single chip.
1.1 Evolution of Computer Aided Digital Design
• SSI (Small Scale Integration) chips where the gate count was very small.
• MSI (Medium Scale Integration) chips-(100 gates)
• LSI (Large Scale Integration)-( thousands of gates)
• VLSI (Very Large Scale Integration) technology, designers could design
single chips with more than 100,000 transistors.
Moore’s Law.
Evolution of Computer Aided Digital Design
• Because of the complexity of these circuits, it was not possible to
verify these circuits on a breadboard.
• Computer-Aided Design (CAD) tools
• EDA tools.
• HDL Languages
1.2 Emergence of HDLs
module andgate(c,a,b);
// Port declarations from the I/O diagram
input a,b;
output c;
• // Specify the function of a design
and a1(c,a,b);
endmodule
l-bit Full Adder design using gate-level modeling
// Define a 1-bit full adder
module fulladd(sum, c_out, a, b, c_in);
// Port declarations from the I/O diagram
output sum, c_out;
input a, b, c_in;
// Internal nets
wire s1, s2, c1;
// Specify the function of a design
xor (s1, a, b);
and (c1, a, b);
xor (sum, s1, c_in);
and (s2, s1, c_in);
or (c_out, s2, c1);
endmodule
4-to-1 Multiplexer design using gate level modeling
Logic Diagram for Multiplexer
Logic Diagram for Multiplexer
• In a top-down design methodology, we define the top-level block and identify the
sub-blocks necessary to build the top-level block.
• We further subdivide the sub-blocks until we come to leaf cells, which are the
cells that cannot further be divided.
A top-down design methodology
bottom-up design methodology.
• In a bottom-up design methodology, we first identify the building
blocks that are available to us.
• We build bigger cells, using these building blocks.
• These cells are then used for higher-level blocks until we build the
top-level block in the design.
bottom-up design methodology.
2.2 4-bit Ripple Carry Counter
2.2 4-bit Ripple Carry Counter
• To illustrate these hierarchical modeling concepts, let us consider the
design of a negative edge-triggered 4-bit ripple carry counter
4-bit Ripple Carry Counter
T-flipflop
Design Hierarchy
4-bit Ripple Carry Counter
module ripple-carry-counter(q, clk,
reset);
output [3:0] q;
input clk, reset;
T-FF tffO(q[0],clk,reset);
T-FF tff1(q[1] ,q[0], reset); module T-FF(q, clk, reset) ;
output q;
T-FF tff2 (q[2] ,q[1], reset) ; input clk, reset;
wire d;
T-FF tff3(q[3] ,q[2], reset) ; D-FF dff0 (q, d, clk, reset) ;
not nl(d, q);
endmodule endmodule
T-Flip Flop
module T-FF(q, clk, reset) ;
output q;
input clk, reset;
wire d; module D-FF(q, d, clk, reset) ;
output q;
D-FF dff0 (q, d, clk, reset) ; input d, clk, reset;
reg q;
not nl(d, q); always @(posedge reset or negedge clk)
if (reset)
endmodule q = 1'bO;
else
q = d;
endmodule
D-Flip Flop
module D-FF(q, d, clk, reset) ;
output q;
input d, clk, reset;
reg q;
always @(posedge reset or negedge clk)
if (reset)
q = 1'b0;
else
q = d;
endmodule
2.5 Components of a Simulation
Components of a Simulation
endmodule
Stimulus Block
module stimulus;
reg clk;
reg reset;
wire[3:0] q;
// instantiate the design block
ripple-carry-counter rl(q, clk, reset);
// Control the clk signal that drives the design block.Cycle time = 10ns
initial
clk = 1'b0; //set clk to 0
always
#5 clk = -clk; //toggle clk every 5 time units
Stimulus Block
// Control the reset signal that drives the design block
initial
begin
reset = 1'b1;
#15 reset = 1'b0;
#180 reset = 1'b1;
#10 reset = 1'b0;
#20 $finish; //terminate the simulation
end
// Monitor the outputs
initial
$monitor ($time, " Output q = %d" , q) ;
endmodule
initial
begin
reset = 1'b1;
#15 reset = 1'b0;
#180 reset = 1'b1;
#10 reset = 1'b0;
#20 $finish; //terminate the simulation
end
// Monitor the outputs
initial
$monitor ($time, " Output q = %d" , q) ;
endmodule
initial
begin
reset = 1'b1;
#15 reset = 1'b0;
#180 reset = 1'b1;
#10 reset = 1'b0;
#20
$finish; //terminate the simulation
end
// Monitor the outputs
initial
$monitor ($time, " Output q = %d" , q) ;
endmodule
Modelsim Invoking step
vlip work
notation
delta = 2.13; // delta is assigned a value 2.13
end
integer i; // Define an integer i
initial
i = delta; // i gets the value 2 (rounded value of 2.13)
Time
• Verilog simulation is done with respect to simulation time.
• A special time register data type is used in Verilog to store
simulation time.
• A time variable is declared with the keyword time.
• The width for time register data types is implementation specific
but is at least 64 bits.
• The system function $time is invoked to get the current simulation
time.
Time
time save-sim-time; // Define a time variable save-sim-time
initial
save-sim-time = $time; // Save the current simulation time
Stimulus Block
// Control the reset signal that drives the design block
initial
begin
reset = 1'b1;
#15 reset = 1'b0;
#180 reset = 1'b1;
#10 reset = 1'b0;
#20 $finish; //terminate the simulation
end
// Monitor the outputs
initial
$monitor ($time, " Output q = %d" , q) ;
endmodule
One dimensional Arrays wire [7:0] bus; // 8-bit bus
Two dimensional Arrays
Arrays
Vector Vs Arrays
Vector: Arrays:
• A vector is a single
• arrays are multiple elements
element that is n-bits
wide. that are 1-bit or n-bits wide.
Arrays
• Arrays are allowed in Verilog for net, reg, integer, time, real and vector
register data types.
• Multi-dimensional arrays can also be declared with any
number of dimensions.
integer count[0:7]; // An array of 8 count variables
reg bool[31:0]; // Array of 32 one-bit boolean register variables
time chk_point[1:100]; // Array of 100 time checkpoint variables
Arrays
wire [7:0] w_array2 [5:0]; // Declare an array of 8 bit vector wire
reg [4:0] port_id[0:7]; // Array of 8 port_ids; each
port_id is 5 bits wide
endmodule
Strings
• Strings can be stored in reg.
• The width of the register variables must be large
enough to
hold the string.
• Each character in the string takes up 8 bits (1 byte).
• If the register width is smaller than the string width,
Verilog truncates the leftmost bits of the string.
Strings
reg [8*18:1] string_value; // Declare a variable that is 18 bytes wide
initial
string_value = "Hello Verilog World"; // String can be stored // in
variable
System Tasks and
Compiler Directives
System Tasks
• Verilog provides standard system tasks for certain
routine operations.
• All system tasks appear in the form $<keyword>.
• Operations such as displaying on the screen, monitoring
values of nets, stopping, and finishing are done by
system tasks.
System Tasks
• Displaying information
• Monitoring information
Displaying information
-- This is a
-- multiline string with a % sign
Monitoring information
• Verilog provides a mechanism to monitor a signal
when its value changes.
• This facility is provided by the $monitor task.
Usage: $monitor(p1,p2,p3,....,pn);
• $monitor continuously monitors the values of the
variables or signals specified in the parameter list
and displays all parameters in the list whenever the
value of any one variable or signal changes.
Monitoring information
//Monitor time and value of the signals clock and reset
//Clock toggles every 5 time units and reset goes down at 10 time units
initial
begin
$monitor($time," Value of signals clock = %b reset = %b", clock,reset);
end
Partial output of the monitor statement:
-- 0 Value of signals clock = 0 reset = 1
-- 5 Value of signals clock = 1 reset = 1
-- 10 Value of signals clock = 0 reset = 0
Monitoring information
• Unlike $display, $monitor needs to be invoked only
once.
• Only one monitoring list can be active at a time.
• If there is more than one $monitor statement in your
simulation, the last $monitor statement will be the
active statement.
• The earlier $monitor statements will be overridden.
• Two tasks are used to switch monitoring on and off.
• Usage:
• $monitoron;
• $monitoroff;
Stopping and finishing in a simulation
• The $stop task is used whenever the designer wants to
suspend the simulation and examine the values of
signals in the design.
• The $finish task terminates the simulation.
Stopping and finishing in a simulation
// Stop at time 100 in the simulation and examine the results
// Finish the simulation at time 1000.
initial // to be explained later. time = 0
begin
clock = 0;
reset = 1;
#100 $stop; // This will suspend the simulation at time = 100
#900 $finish; // This will terminate the simulation at time = 1000
end
Compiler Directives
Inputs
• Internally, input ports must always
be of the type net.
• Externally, the inputs can be
connected to a variable which is a
reg or a net.
Outputs
• Internally, outputs ports can be of
the type reg or net.
• Externally, outputs must always
be connected to a net. They
cannot be connected to a reg.
Stimulus Block
module stimulus;
reg clk;
reg reset;
wire[3:0] q;
// instantiate the design block
ripple-carry-counter rl(q, clk, reset);
// Control the clk signal that drives the design block.Cycle time = 10ns
initial
clk = 1'b0; //set clk to 0
always
4-bit Full adder design using 1-bit full adder
inouts
• Internally, inout ports
must always be of the
type net.
• Externally, inout ports
must always be connected
to a net.
Port Connection Rules
Width matching
• It is legal to connect internal and external items of
different sizes when making inter-module port
connections.
• However, a warning is typically issued that the widths
do not match.
Unconnected ports
• Verilog allows ports to remain unconnected.
fulladd4 fa0(SUM, , A, B, C_IN); // Output port c_out is unconnected
Example of illegal port connection
module Top;
• //Declare connection variables
reg [3:0]A,B;
reg C_IN;
reg [3:0] SUM;
wire C_OUT;
//Instantiate fulladd4, call it fa0
fulladd4 fa0(SUM, C_OUT, A, B, C_IN);
//Illegal connection because output port sum in module fulladd4
//is connected to a register variable SUM in module Top.
<stimulus>
• endmodule
Example of illegal port connection
This problem is rectified if the variable SUM is declared as a net (wire).
Connecting Ports to External Signals
bufif1 notif1
bufif0 notif0
bufif1 bufif0
notif0
notif1
Gate Instantiations of Bufif/Notif
Gates
//Instantiation of bufif gates.
bufif1 b1 (out, in, ctrl);
bufif0 b0 (out, in, ctrl);
//Instantiation of notif gates
notif1 n1 (out, in, ctrl);
notif0 n0 (out, in, ctrl);
Array of Instances
• There are many situations when repetitive instances
are required.
• These instances differ from each other only by the
index of the vector to which they are connected.
• To simplify specification of such instances, Verilog
HDL allows an array of primitive instances to be
defined.
Simple Array of Primitive Instances
wire [7:0] OUT, IN1, IN2;
// basic gate instantiations.
nand n_gate[7:0](OUT, IN1, IN2);
// This is equivalent to the following 8 instantiations
nand n_gate0(OUT[0], IN1[0], IN2[0]);
nand n_gate1(OUT[1], IN1[1], IN2[1]);
nand n_gate2(OUT[2], IN1[2], IN2[2]);
nand n_gate3(OUT[3], IN1[3], IN2[3]);
nand n_gate4(OUT[4], IN1[4], IN2[4]);
nand n_gate5(OUT[5], IN1[5], IN2[5]);
nand n_gate6(OUT[6], IN1[6], IN2[6]);
nand n_gate7(OUT[7], IN1[7], IN2[7]);
4-to-1 Multiplexer
Logic Diagram for Multiplexer
module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
output out;
input i0, i1, i2, i3;
Verilog Description of Multiplexer
input s1, s0;
wire s1n, s0n;
wire y0, y1, y2, y3;
not (s1n, s1);
not (s0n, s0);
and (y0, i0, s1n, s0n);
and (y1, i1, s1n, s0);
and (y2, i2, s1, s0n);
and (y3, i3, s1, s0);
or (out, y0, y1, y2, y3);
endmodule
Stimulus for Multiplexer
module stimulus;
reg IN0, IN1, IN2, IN3;
reg S1, S0;
wire OUTPUT;
mux4_to_1 mymux(OUTPUT, IN0, IN1, IN2, IN3, S1, S0);
initial
begin
IN0 = 1; IN1 = 0; IN2 = 1; IN3 = 0;
#5 S1 = 0; S0 = 0;
#5 S1 = 0; S0 = 1;
#5 S1 = 1; S0 = 0;
#5 S1 = 1; S0 = 1;
end
endmodule
The output of the simulation
• IN0= 1, IN1= 0, IN2= 1, IN3= 0
• S1 = 0, S0 = 0, OUTPUT = 1
• S1 = 0, S0 = 1, OUTPUT = 0
• S1 = 1, S0 = 0, OUTPUT = 1
• S1 = 1, S0 = 1, OUTPUT = 0
• 4-bit Ripple Carry Full Adder
Verilog Description for l-bit Full Adder
// Define a 1-bit full adder
module fulladd(sum, c_out, a, b, c_in);
output sum, c_out;
input a, b, c_in;
// Internal nets
wire s1, s2, c2;
0 A= 0000, B=0000, C_IN= 0, --- C_OUT= 0, SUM= 0000 A = 4'd0; B = 4'd0; C_IN = 1'b0;
5 A= 0011, B=0100, C_IN= 0, --- C_OUT= 0, SUM= 0111 #5 A = 4'd3; B = 4'd4;
10 A= 0010, B=0101, C_IN= 0, --- C_OUT= 0, SUM= 0111 #5 A = 4'd2; B = 4'd5;
15 A= 1001, B=1001, C_IN= 0, --- C_OUT= 1, SUM= 0010 #5 A = 4'd9; B = 4'd9;
20 A= 1010, B=1111, C_IN= 0, --- C_OUT= 1, SUM= 1001
#5 A = 4'd10; B = 4'd15;
25 A= 1010, B=0101, C_IN= 1 --- C_OUT= 1, SUM= 0000
#5 A = 4'd10; B = 4'd5; C_IN = 1'b1;
Gate Delays
module stimulus;
reg A, B, C;
wire OUT;
// Instantiate the module D
D d1( OUT, A, B, C);
Stimulus for Module D with Delay
initial
begin
A= 1'b0; B= 1'b0; C= 1'b0;
#10 A= 1'b1; B= 1'b1; C= 1'b1;
#10 A= 1'b1; B= 1'b0; C= 1'b0;
#20 $finish;
end
endmodule
Waveforms for Delay Simulation
module Top;
wire q, qbar;
reg set, reset;
SR_latch m1(q, qbar, ~set, ~reset);
-----------------------------------------
module SR_latch(Q, Qbar,
Sbar, Rbar);
output Q, Qbar;
input Sbar, Rbar;
nand n1(Q, Sbar, Qbar);
nand n2(Qbar, Rbar, Q);
endmodule
Hierarchical Names
• stimulus
stimulus.q
• stimulus.qbar
stimulus.set
• stimulus.reset
stimulus.m1
• stimulus.m1.Q
stimulus.m1.Qbar
• stimulus.m1.S
stimulus.m1.R
• stimulus.m1.n1
stimulus.m1.n2
Chapter 6. Dataflow
Modeling
6.1 Continuous Assignments
Continuous Assignments
• A continuous assignment is the most basic statement // Continuous assign. out is a net. i1
in dataflow modeling, used to drive a value onto a net. and i2 are nets.
assign out = i1 & i2;
• The assignment statement starts with the keyword
assign. The syntax of an assign statement is as follows. // Continuous assign for vector nets.
addr is a 16-bit vector net
// addr1 and addr2 are 16-bit vector
continuous_assign ::= assign [ drive_strength ] [ delay3 ] list_of_net_assignments ; registers.
list_of_net_assignments ::= net_assignment { , net_assignment } assign addr[15:0] = addr1_bits[15:0] ^
addr2_bits[15:0];
net_assignment ::= net_lvalue = expression
// Concatenation. Left-hand side is a
Notice that drive strength is optional concatenation of a scalar
• The default value for drive strength is strong1 and // net and a vector net.
• The delay value is also optional and can be used to specify delay on the
strong0.
assign statement. assign {c_out, sum[3:0]} = a[3:0] + b[3:0] +
c_in;
• This is like specifying delays for gates.
Continuous assignments have the following characteristics:
1. The left hand side of an assignment // Continuous assign. out is a net. i1 and i2 are nets.
must always be a scalar or vector net or assign out = i1 & i2;
a concatenation of scalar and vector // Continuous assign for vector nets. addr is a 16-bit vector
nets.It cannot be a scalar or vector net
register. // addr1 and addr2 are 16-bit vector registers.
A pulse of width less than the specified assignment delay is not propagated to the output.
Implicit Continuous Assignment Delay
• An equivalent method is to use an implicit continuous
assignment to specify both a delay and an assignment
on the net.
in1 = 4'b101x;
in2 = 4'b1010;
sum = in1 + in2; // sum will be evaluated to the value 4'bx
Binary operators
• Modulus operators produce the remainder from the division of two
numbers.
13 % 3 // Evaluates to 1
16 % 4 // Evaluates to 0
-7 % 2 // Evaluates to -1, takes sign of the first operand
7 % -2 // Evaluates to +1, takes sign of the first operand
Unary operators
• The operators + and - can also work as unary operators.
• They are used to specify the positive or negative sign of the operand.
• Unary + or ? operators have higher precedence than the binary + or ?
operators.
-4 // Negative 4
+5 // Positive 5
Unary operators
• Negative numbers are represented as 2's complement internally in
Verilog.
• It is advisable to use negative numbers only of the type integer or real
in expressions.
//Advisable to use integer or real numbers
-10 / 5// Evaluates to -2
Logical Operators
• The use of a similar set of symbols for logical (!, &&, ||),
bitwise (~, &, |, ^), and reduction operators (&, |, ^) is
somewhat confusing initially.
• The difference lies in the number of operands each operator
takes
Relational Operators
• Equality operators are logical equality (==), logical inequality (!=), case equality (===), and case
inequality (!==).
• When used in an expression, equality operators return logical value 1 if true, 0 if false.
• These operators compare the two operands bit by bit, with zero filling if the operands are of
unequal length.
• It is important to note the difference between the logical equality operators (==, !=) and case
equality operators (===, !==).
• The logical equality operators (==, !=) will yield an x if either operand has x or z in its bits.
• However, the case equality operators ( ===, !== ) compare both operands bit by bit and compare
all bits, including x and z. The result is 1 if the operands match exactly, including x and z bits.
• The result is 0 if the operands do not match exactly. Case equality operators never result in an x.
Equality Operators
Equality Operators
// A = 4, B = 3
// X = 4'b1010, Y = 4'b1101
// Z = 4'b1xxz, M = 4'b1xxz, N = 4'b1xxx
A == B // Results in logical 0
X != Y // Results in logical 1
X == Z // Results in x
Z === M // Results in logical 1 (all bits match, including x and z)
Z === N // Results in logical 0 (least significant bit does not match)
M !== N // Results in logical 1
Shift Operators
• Shift operators are right shift ( >>), left shift (<<), arithmetic right shift
(>>>), and arithmetic left shift (<<<).
• Regular shift operators shift a vector operand to the right or the left by
a specified number of bits.
• The operands are the vector.
• When the bits are shifted, the vacant bit positions are filled with zeros.
• arithmetic right shift (>>>) - shift right specified number of bits, fill with
value of sign bit if expression is signed, otherwise fill with zero
• arithmetic left shift (<<<) - shift left specified number of bits, fill with
zero.
Shift Operators
// X = 4'b1100
Y = X >> 1; //Y is 4'b0110. Shift right 1 bit. 0 filled in MSB position.
Y = X << 1; //Y is 4'b1000. Shift left 1 bit. 0 filled in LSB position.
Y = X << 2; //Y is 4'b0000. Shift left 2 bits.
Concatenation Operator
• The concatenation operator ( {, } ) provides a mechanism to
append multiple operands.
• The operands must be sized.
• Unsized operands are not allowed because the size of each
operand must be known for computation of the size of the
result.
Concatenation Operator
• The left-hand side of a procedural assignment <lvalue> can be one of the following:
• Event control discussed earlier waited for the change of a signal value or the
triggering of an event.
• The symbol @ provided edge-sensitive control.
• Verilog also allows level sensitive timing control, that is, the ability to wait for
a certain condition to be true before a statement or a block of statements is
executed.
• The keyword wait is used for level sensitive constructs.
Level-Sensitive Timing Control
always
wait (count_enable) #20 count = count + 1;
Conditional Statements
//Type 1 conditional statement. No else statement.
//Statement executes or does not execute.
if (<expression>) true_statement ;
//Type 2 conditional statement. One else statement
//Either true_statement or false_statement is evaluated
if (<expression>) true_statement ; else false_statement ;
Conditional Statements
//Type 3 conditional statement. Nested if-else-if.
//Choice of multiple statements. Only one is executed.
• if (<expression1>) true_statement1 ;
• else if (<expression2>) true_statement2 ;
• else if (<expression3>) true_statement3 ;
• else default_statement ;
Conditional Statements
//Type 1 statements
if(!lock) buffer = data;
if(enable) out = in;
//Type 2 statements
if (number_queued < MAX_Q_DEPTH)
begin
data_queue = data;
number_queued = number_queued + 1;
end
else
$display("Queue Full. Try again");
Conditional Statements
• //Type 3 statements
• //Execute statements based on ALU control signal.
if (alu_control == 0)
y = x + z;
else if (alu_control == 1)
y = x - z;
else if (alu_control == 2)
y = x * z;
else
$display("Invalid ALU control signal");
Multiway Branching
• case Statement
The keywords case, endcase, and default are used in the case statement..
case (expression)
alternative1: statement1;
alternative2: statement2;
alternative3: statement3;
...
...
default: default_statement;
endcase
Multiway Branching
//Execute statements based on the ALU control signal
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");
endcase
4-to-1 Multiplexer with Case Statement
module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
// Port declarations from the I/O diagram
output out;
input i0, i1, i2, i3;
input s1, s0;
reg out;
always @(s1 or s0 or i0 or i1 or i2 or i3)
case ({s1, s0}) //Switch based on concatenation of control signals
2'd0 : out = i0;
2'd1 : out = i1;
2'd2 : out = i2;
2'd3 : out = i3;
default: $display("Invalid control signals");
endcase
endmodule
Case Statement
• If none of the alternatives match, the default-statement is executed.
• The default-statement is optional.
• Placing of multiple default statements in one case statement is not
allowed.
casex, casez Difference
• casez treats all z, ?values in the case alternatives or
the case expression as don't cares.
• casex treats all x and z values in the case item or the
case expression as don't cares.
casex, casez Keywords
reg [3:0] encoding;
integer state;
casex (encoding) //logic value x represents a don't care bit.
4'b1xxx : next_state = 3;
4'bx1xx : next_state = 2;
4'bxx1x : next_state = 1;
4'bxxx1 : next_state = 0;
default : next_state = 0;
endcase
Only one bit is considered to determine the next state and the other bits are ignored.
Thus, an input encoding = 4'b10xz would cause next_state = 3 to be executed.
While Loop
The keyword repeat is used for this loop. //Illustration 1 : increment and
The repeat construct executes the loop a display count from 0 to 127
fixed number of times. integer count;
A repeat construct cannot be used to loop initial
on a general logical expression
• begin
A repeat construct must contain a
number, which can be a constant, a • count = 0;
variable or a signal value. • repeat(128)
• begin
• $display("Count = %d", count);
• count = count + 1;
• end
• end
Forever loop
• The keyword forever is used to express this loop. The loop does not
contain any expression and executes forever until the $finish task is
encountered.
//Example 1: Clock generation
//Use forever loop instead of always block
reg clock;
initial
begin
clock = 1'b0;
forever #10 clock = ~clock; //Clock with period of 20 units
end
Behavioral 4-bit Counter Description
//4-bit Binary counter
module counter(Q , clock, clear);
output [3:0] Q;
input clock, clear;
reg [3:0] Q;
always @( posedge clear or negedge clock)
begin
if (clear)
Q <= 4'd0; //Nonblocking assignments are recommended
//for creating sequential logic such as flipflops
else
Q <= Q + 1;
end
endmodule
Behavioral 4-bit Counter Description
//4-bit Binary counter
module counter(Q , clock, clear);
output [3:0] Q;
input clock, clear;
reg [3:0] Q;
always @( negedge clear or posedge clock)
begin
if (clear)
Q <= Q + 1; //Nonblocking assignments are recommended
//for creating sequential logic such as flipflops
else
Q <= 0;
end
endmodule
Sequential and Parallel Blocks
Sequential blocks
• The keywords begin and end are used to group statements into
sequential blocks.
Sequential blocks have the following characteristics:
• The statements in a sequential block are processed in the order they
are specified.
• A statement is executed only after its preceding statement completes
execution
(except for nonblocking assignments with intra-assignment timing
control).
• If delay or event control is specified, it is relative to the simulation
time when the previous statement in the block completed execution.
Nonblocking Assignments
reg x, y, z;
1. reg_a[2] = 0 is scheduled to
reg [15:0] reg_a, reg_b;
integer count;
execute after 15 units (i.e., time =
//All behavioral statements must be inside an initial or 15)
always block
initial 2. reg_b[15:13] = {x, y, z} is
begin scheduled to execute after 10
x = 0; y = 1; z = 1; //Scalar assignments
count = 0; //Assignment to integer variables
time units (i.e.,time = 10)
reg_a = 16'b0; reg_b = reg_a; //Initialize vectors 3. count = count + 1 is scheduled
reg_a[2] <= #15 1'b1; //Bit select assignment with delay
reg_b[15:13] <= #10 {x, y, z}; //Assign result of concatenation
to be executed without any delay
//to part select of a vector (i.e., time = 0)
count <= count + 1; //Assignment to an integer (increment)
end
//Illustration 1: Sequential block without
delay
reg x, y;
reg [1:0] z, w;
initial
begin
x = 1'b0;
y = 1'b1;
z = {x, y};
w = {y, x};
end
//Illustration 2: Sequential blocks with delay.
reg x, y;
reg [1:0] z, w;
initial
begin
x = 1'b0; //completes at simulation time 0
#5 y = 1'b1; //completes at simulation time 5
#10 z = {x, y}; //completes at simulation time 15
#20 w = {y, x}; //completes at simulation time 35
end
Parallel blocks
Parallel blocks, specified by keywords fork and join, provide interesting
simulation features.
Parallel blocks have the following characteristics:
• Statements in a parallel block are executed concurrently.
• Ordering of statements is controlled by the delay or event control
assigned to each statement.
• If delay or event control is specified, it is relative to the time the block
was entered.
fundamental difference
• Notice the fundamental difference between sequential and parallel
blocks. All statements in a parallel block start at the time when the
block was entered.
• Thus, the order in which the statements are written in the block is not
important.
//Example 1: Parallel blocks with delay.
reg x, y;
reg [1:0] z, w;
initial
fork
x = 1'b0; //completes at simulation time 0
#5 y = 1'b1; //completes at simulation time 5
#10 z = {x, y}; //completes at simulation time 10
#20 w = {y, x}; //completes at simulation time 20
join
Special Features of Blocks
• Three special features available with block statements:
• nested blocks,
• Named blocks, and
• disabling of named blocks.
Nested Blocks
• Blocks can be nested. Sequential and parallel blocks can be mixed,
//Nested blocks
initial
begin
x = 1'b0;
fork
#5 y = 1'b1;
#10 z = {x, y};
join
#20 w = {y, x};
end
fork….,join Nested Blocks
initial
begin
clk =0;
#5
fork
#5 a = 0;
#10 b = 0;
join // executes any all process inside the fork
clk= 1; // clk becomes 1 at t=15
end
Named blocks
module upordown_counter(Clk,
reset,UpOrDown, Count ); else //Down mode selected
input Clk,reset,UpOrDown; if(Count == 0)
output [3 : 0] Count; Count <= 15;
reg [3 : 0] Count = 0; else
always @(negedge(Clk) or posedge(reset))
Count <= Count - 1; //Decrement counter
begin
if(reset == 1)
end
Count <= 0; endmodule
else
if(UpOrDown == 1) //Up mode selected
if(Count == 15)
Count <= 0;
else
Count <= Count + 1;
Priorty Encoder
• If a, b, c, and out are 2-bit vectors [1:0], then the above assign
statement will frequently translate to two identical circuits for each
bit.
Arithmetic operators
• If arithmetic operators are used, each arithmetic operator is
implemented in terms of arithmetic hardware blocks available to the
logic synthesis tool.
• A 1-bit full adder
assign {c_out, sum} = a + b + c_in;
arithmetic operators
• If a multiple-bit adder is synthesized, the synthesis tool will perform
optimization and the designer might get a result that looks different
from the above figure.
conditional operator ?
• If a conditional operator ? is used, a multiplexer circuit is inferred.
assign out = (s) ? i1 : i0;
The if-else statement
• Single if-else statements translate to multiplexers where the control
signal is the signal or variable in the if clause.
if(s)
out = i1;
else
out = i0;
The if-else statement
• In general, multiple if-else-if statements do not synthesize to large
multiplexers.
The case statement
• The case statement also can used to infer multiplexers.
case (s)
1'b0 : out = i0;
1'b1 : out = i1;
endcase
The case statement
• Large case statements may be used to infer large multiplexers.
for loops
• The for loops can be used to build cascaded combinational logic. For
example, the following for loop builds an 8-bit full adder:
c = c_in;
for(i=0; i <=7; i = i + 1)
{c, sum[i]} = a[i] + b[i] + c; // builds an 8-bit ripple adder
c_out = c;
The always statement
• The always statement can be used to infer sequential and combinational
logic.
• For sequential logic, the always statement must be controlled by the
change in the value of a clock signal clk.
positive edge-triggered D-flipflop
always @(clk or d)
if (clk)
q <= d;
combinational logic
• For combinational logic, the always statement must be triggered by a
signal other than the clk, reset, or preset.
• For example, the following block will be interpreted as a 1-bit full adder:
always @(a or b or c_in)
{c_out, sum} = a + b + c_in;
14.6 Modeling Tips for Logic
Synthesis
Modeling Tips for Logic Synthesis
• The Verilog RTL design style used by the designer affects the
final gate-level netlist produced by logic synthesis.
• Logic synthesis can produce efficient or inefficient gate level
netlists, based on the style of RTL descriptions.
• Hence, the designer must be aware of techniques used to write
efficient circuit descriptions.
14.6.1 Verilog Coding Style
Code for a gated D latch
• module D_latch (D, Clk, Q);
• input D, Clk;
• output reg Q;
• always @(D, Clk)
• if (Clk)
• Q = D;
• endmodule
Code for a 2:1 mux
• module D_latch (D, Clk, Q);
• input D, Clk;
• output reg Q;
• always @(D, Clk)
• if (Clk)
• Q = D;
• Else
• Q=0;
• endmodule
Synthesis of Flip Flops
Code for a D flip-flop.
• module flipflop (D, Clock, Q);
• input D, Clock;
• output reg Q;
• always @(posedge Clock)
• Q = D;
• endmodule
Synthesis of Flip Flops
D flip-flop with asynchronous reset.
• module flipflop (D, rst, Clock, Q);
• input D, Clock,rst ;
• output reg Q;
• always @(posedge Clock, negedge rst)
• if(!rst)
• Q = 0;
• else
• Q = D;
• endmodule
Synthesis of Flip Flops
D flip-flop with synchronous reset.
• endmodule
Code for a 2:1 mux
• module D_latch (D, Clk, Q);
• input D, Clk;
• output reg Q;
• always @(D, Clk)
• if (Clk)
• Q = D;
• Else
• Q=0;
• endmodule
Structural Realization–
Adders
l-bit Full Adder design using gate-level modeling
// Define a 1-bit full adder
module fulladd(sum, c_out, a, b, c_in);
output sum, c_out;
input a, b, c_in;
// Internal nets
wire s1, s2, c2;
module upordown_counter(Clk,
reset,UpOrDown, Count ); else //Down mode selected
input Clk,reset,UpOrDown; if(Count == 0)
output [3 : 0] Count; Count <= 15;
reg [3 : 0] Count = 0; else
always @(negedge(Clk) or posedge(reset))
Count <= Count - 1; //Decrement counter
begin
if(reset == 1)
end
Count <= 0; endmodule
else
if(UpOrDown == 1) //Up mode selected
if(Count == 15)
Count <= 0;
else
Count <= Count + 1;
4-bit Johnson counter
4-bit Johnson counter
• ■ 16 LEs
• ■ LAB control signals
• ■ LE carry chains
• ■ Register chains
• ■ Local interconnect
Cyclone II LE
Cyclone II LE
• The smallest unit of logic in the Cyclone II
architecture, the LE, is compact and provides
advanced features with efficient logic
utilization.
Cyclone II LE
• Each LE features:
• A four-input look-up table (LUT), which is
a function generator that can implement
any function of four variables
■ A programmable register
■ A carry chain connection
■ A register chain connection
Cyclone II LE
• ■ Normal mode
• ■ Arithmetic mode
LE Operating Modes
Normal mode
Normal Mode
• Logic Elements
• The logic element of an FPGA is considerably more complex than a
standard CMOS gate.
A logic element built from a multiplexer
Programming the mux-based logic element
Shannon’s Expansion Theorem
• F=(A·B) + (B'·C) + D
• Expand F wrt B: F=B·(A + D) + B'·(C + D) =B·F2 + B’·F1
• Expand F2 wrt A, and F1 wrt C:
• F2=A + D=(A·1) + (A'·D);
• F1=C + D=(C·1) + (C'·D)
Shannon’s Expansion Theorem
4.7.1 Syntax-Directed Translation
Translating a logical expression in an HDL description.
Translating a conditional in an HDL description.
Book3- Chapter17-Intellectual
Property
Intellectual Property
• Any existing functional blocks are typically referred to as IP.
Three main sources of such IP are
(1) Internally created blocks from Previous design
(2) FPGA Vendors
(3) Third Party IP providers
Handcrafted IP