Download goodEE/CS120A homework and more Schemes and Mind Maps Energy and Environment in PDF only on Docsity!
1
CS 303 - Logic & Digital System Design
CS 303
Design with Verilog
Sabancı University
- Some slides are adapted from DDCA and earlier offerings of this course
2
CS 303
Verilog
Verilog is a Hardware Description Language (HDL).
It is like a programming language but not a
programming language.
It is used to describe any digital circuit.
You are describing connections between circuit
elements.
There are lots of details. However, you will learn
only basics.
For more, you may take EE 310 – Hardware
Description Languages.
Sabancı University
3
Module name Module ports
Verilog keywords
Taste of Verilog
module Add_half ( sum, c_out, a, b );
input a, b;
output sum, c_out;
wire c_out_bar;
xor (sum, a, b);
nand (c_out_bar, a, b);
not (c_out, c_out_bar);
endmodule
Declaration of port
modes
Declaration of internal
signal
Instantiation of primitive gates
c_out
a
b
sum
c_out_bar
4
CS 303
Design with Verilog
Gate Level Design (Decoder with enable input)
Sabancı University
module decoder_2x4_gates(D, A, B, e);
output [0:3] D;
input A, B, e;
wire A_n, B_n;
not G1(A_n, A);
not G2(B_n, B);
and G3(D[0], e, A_n, B_n);
and G4(D[1], e, A_n, B);
and G5(D[2], e, A, B_n);
and G6(D[3], e, A, B);
endmodule;
G
G
G
G
G
G
A
B
e
D
0
D
1
D
2
D
3
D
0
= eA’B’
D
1
= eA’B
D
2
= eAB’
D
3
= eAB
A_n
B_n
5
CS 303
Dataflow Modeling
Dataflow modeling uses a number of operators
that act on operands
About 30 different operators:
See the textbook for the operators
Describes combinational circuits by their functions
rather than their gate structure
Sabancı University
G
G
G
G
G
G
A
B
e
D
0
D
1
D
2
D
3
D
0
= eA’B’
D
1
= eA’B
D
2
= eAB’
D
3
= eAB
module decoder_2x4_dataflow(
output [0:3] D,
input A, B,
e);
assign D[0] = e & ~A & ~B;
assign D[1] = e & ~A & B;
assign D[2] = e & A & ~B;
assign D[3] = e & A & B;
endmodule;
6
CS 303
Dataflow Modeling
Data type “ net ”
Represents a physical connection between circuit elements
e.g., “ wire ”, “ output ”, “ input ”.
Continuous assignment “ assign ”
A statement that assigns a value to a net
e.g., assign D[0] = e & ~A & ~B;
e.g., assign A_lt_B = A < B;
Bus type
wire [0:3] T;
T[0], T[3], T[1..2];
Sabancı University
7 CS 303
Behavioral Modeling
Represents digital circuits at a functional and algorithmic
level
Mostly used to describe sequential circuits
Can also be used to describe combinational circuits
Sabancı University
module mux_2x1_beh(m, A, B, S);
output m;
input A, B, S;
reg m;
always @(A or B or S);
if(S == 1) m = A;
else m = B;
endmodule;
8 CS 303
Behavioral Modeling
Output must be declared as “ reg ” data type
“ always ” block
Procedural assignment statements are executed every time there is a change in any
of the variables listed after the “ @ ” symbol (i.e., sensitivity list).
Sabancı University
module mux_4x1_beh(output reg m,
input I0, I1, I2, I3;
input [1:0] S);
always @(I0 or I1 or I2 or I3 or S);
case (S)
2’b00: m = I0;
2’b01: m = I1;
2’b10: m = I2;
2’b11: m = I3;
endcase
endmodule;
9 CS 303
A Sample Design – Full Adder
Before introducing the rules, let’s make a sample
design in Verilog.
A full adder circuit
3 inputs
◼ A
◼ B
◼ C
2 outputs
◼ Sum
◼ Carry
Sabancı University
10
CS 303
A Sample Design – Full Adder
Sabancı University
module/endmodule is used to define the design
module FullAdder (A, B, Cin, Sum, Carry);
input A, B, Cin;
output Sum, Carry;
wire w1, w2, w3;
assign w1 = A & B;
assign w2 = A & Cin;
assign w3 = B & Cin;
assign Carry = w1 | w2| w3;
assign Sum = A ^ B ^ Cin;
endmodule
11
CS 303
Test Bench
Sabancı University
module ;
// Data type declaration
// Instantiate module ( call the module that is going to be
tested)
// Apply the stimulus
// Display results
endmodule
12
CS 303
Test Bench
Sabancı University
module test_my_nand;
// Test bench to test half adder
reg A, B; wire s, cOut;
Add_half test( s, cOut, A, B ); // instantiate my_NAND.
initial begin // apply the stimulus, test data
A = 1'b0; B = 1'b0;
#100 A = 1'b1; // delay one simulation cycle, then change A=>1.
#100 B = 1'b1;
#100 A = 1'b0;
end
initial #500 $finish;
initial begin
// setup monitoring
//$monitor("Time=%0d a=%b b=%b out1=%b", $time, A, B, F);
end
endmodule
6
6
Clocked Sequential Circuits
Memory elements are flip-flops which are logic devices, each
of which is capable of storing one bit of information.
Combinational
Circuit
Flip-Flops
inputs outputs
current
state
next
state
clock
Sabancı University
7
7
Clocked Sequential Circuits
The outputs of a clocked sequential circuit can come
from the combinational circuit, from the outputs of the
flip-flops or both.
The state of the flip-flops can change only during a clock
pulse transition
i.e. low-to-high and high-to-low
clock edge
When the clock maintains its value, the flip-flop output
does not change
The transition from one state to the next occurs at the
clock edge.
Sabancı University
8
8
Latches
The most basic types of memory elements are not
flip-flops, but latches.
A latch is a memory device that can maintain a
binary state indefinitely.
Latches are, in fact, asynchronous devices and they
usually do not require a clock to operate.
Therefore, they are not directly used in clocked
synchronous sequential circuits.
They rather be used to construct flip-flops.
Sabancı University
9
9
SR-Latches with NAND Gates
S
Q
1
R
Q
2
S R Q
Q
After S = 1, R = 0
After S = 0, R = 1
Undefined
Also known as S’R’-latch
Made of cross-coupled
NAND (NOR) gates
Q
2
= Q
1
Sabancı University
10
Undefined State of SR-Latch
S = R = 0 may result in an undefined state
the next state is unpredictable when both S and R goes to 1 at the same
time after the undefined state.
It may oscillate
Or the outcome state depend on which of S and R goes to 1 first.
S
Q
R
Q’
It oscillates
S
R
Q
Q’
Sabancı University
13
SR-Latch with Control Input
Sabancı University
Control inputs allow
the changes at S and R
to change the state of
the latch.
Q
Q’
R
S
C
C S R Q Q’
0 X X No change
1 0 0 No change
1 0 1 Q = 0 Reset state
1 1 0 Q = 1 Set state
1 1 1 Indeterminate
14
14
D-Latch
SR latches are seldom used in practice because the
indeterminate state may cause instability
Remedy: D-latches
R
Q
S
Q’
This circuit guarantees that the inputs to the S’R’-latch
is always complement of each other when C = 1.
C
D
Sabancı University
15
15
D-Latch
We say that the D input is sampled when C = 1
C D Next state of Q
0 X No change
1 0 Q = 0; reset state
1 1 Q = 1; set state
S
R
Q
Q’
SR-latch
S
R
Q
Q’
S’R’-latch
D
C
Q
Q’
D-latch
Sabancı University
16
16
D-Latch as a Storage Unit
D–latches can be used as temporary storage
The input of D-latch is transferred to the Q output when C = 1
When C = 0 the binary information is retained.
We call latches level-sensitive devices.
So long as C remains at logic-1 level, any change in data input will
change the state and the output of the latch.
Level sensitive latches may suffer from a serious problem.
Memory devices that are sensitive to the rising or falling edge
of control input is called flip-flops.
Sabancı University
17
17
Need for Flip-Flops 1/
Outputs may keep changing so long as C = 1
Combinational
Circuit
Latches
inputs outputs
current
state
next
state
C
Sabancı University
18
18
Need for Flip-Flops 2/
Another issue (related to the first one)
The states of the memory elements must change synchronously
memory elements should respond to the changes in input at
certain points in time.
This is the very characteristics of synchronous circuits.
To this end, we use flip-flops that change states during a signal
transition of control input (clock)
Sabancı University
19
19
Edge-Triggered D Flip-Flop
Using two D latches
D latch
(master)
D
C
Q
D latch
(slave)
D
C
Q
clk
D
Q
Y
clk’
Q = Y = D
Y = D
clk
Negative edge-triggered
D flip-flop
clk’
clk
D
Y
Q
Sabancı University
26
26
Example
clk
Q
D
time
D FF
D Q
clk C
Positive edge-triggered
D Flip-Flop
Characteristic equation
Q(t+1) = D
Sabancı University
27
27
Other Flip-Flops
D flip-flop is the most common
since it requires the fewest number of gates to construct.
Two other widely used flip-flops
JK flip-flops
T flip-flops
JK flip-flops
Three FF operations
1. Set
2. Reset
3. Complement
Sabancı University
28
28
JK Flip-Flops
Characteristic equation
Q(t+1) = JQ’(t) + K’Q(t)
J Q
C
K
J K Q(t+1) next state
0 0 Q(t) no change
0 1 0 Reset
1 0 1 Set
1 1 Q’(t) Complement
Characteristic Table
Sabancı University
29
29
T (Toggle) Flip-Flop
Complementing flip-flop
T
Q
C
T Q(t+1) next state
0 Q(t) no change
1 Q’(t) Complement
Characteristic Table
Characteristic equation
J Q
C
K
T
D Q
C
T
Sabancı University
30
Characteristic Equations
The logical properties of a flip-flop can be expressed
algebraically using characteristic equations
D flip-flop
Q(t+1) = D
JK flip-flop
Q(t+1) = JQ’ + K’Q
T flip-flop
Q(t+1) = T Q
Sabancı University
31
31
Asynchronous Inputs of Flip-Flops
They are used to force the flip-flop to a particular state
independent of the clock
“Preset” (direct set) set FF state to 1
“Clear” (direct reset) set FF state to 0
They are especially useful at startup.
In digital circuits when the power is turned on, the state of flip-flops
are unknown.
Asynchronous inputs are used to bring all flip-flops to a known
“starting” state prior to clock operation.
Sabancı University
32
32
Asynchronous Inputs
reset C D Q Q’
1 X X 0 1
Starting State
D
Q
C
data
reset
R
clk
D
Q
reset
Sabancı University
33
33
Analysis of Clocked Sequential Circuits
Goal:
to determine the behavior of clocked sequential circuits
“Behavior” is determined from
◼ Inputs
◼ Outputs
◼ State of the flip-flops
We have to obtain
◼ Boolean expressions for output and next state
◼ output & state equations
◼ (state) table
◼ (state) diagram
Sabancı University
34
State Equations
Sabancı University
Also known as “transition equations”
specify the next state as a function of the present state and inputs
Example
A(t+1)
B(t+1)
D Q
C
D Q
C
A
A’
B
B’
x
y
clk
35
Output and State Equations
Sabancı University
A(t+1)
B(t+1)
D Q
C
D Q
C
A
A’
B
B’
x
y
clk
A(t+1) = x(A B)
B(t+1) = xB’
y = xAB
36
Flip Flop Input Equations
Flip-Flop input (excitation) equations
Same as the state equations in D flip-flops
Sabancı University
37
37
Example: State (Transition) Table
A B x A B y
Present state input Next state output
A(t+1) = x(AB) B(t+1) = xB’ y = xAB
Sabancı University
49
Timing Diagram
Sabancı University
clk
x
C
y
S
reset
S = x y C
C(t+1) = xy + xC + yC
50
Timing Diagram
Sabancı University
clk
x
C
y
S
reset
S = x y C
C(t+1) = xy + xC + yC
51
Example: Moore Machines
Outputs are already synchronized with clock.
They change synchronously with the clock edge.
T Q
C
D Q
C
A
B
x
clk
T
Q
C
reset
y
Sabancı University
52
CS 303
Next: Sequential Logic
Sabancı University
1
CS 303 - Logic & Digital System Design
CS 303
Synchronous Sequential Logic
Part II
Sabancı University
- Some slides are adapted from DDCA and earlier offerings of this course
2
State Reduction and Assignment
In the design process of sequential circuits, certain
techniques are useful in reducing the circuit complexity
state reduction
state assignment
State reduction
Fewer states ➔ fewer number of flip-flops
m flip-flops ➔ 2
m
states
Example: m = 5 ➔ 2
m
◼ If we reduce the number of states to 21 do we reduce the
number of flip-flops?
3
Example: State Reduction
Note that we use
letters to designate
the states for
the time being
a
b
d
f
c
g e
4
State Reduction Technique 1/
Step 1: get a state table
present state next state Output
x = 0 x = 1 x = 0 x = 1
a a b 0 0
b c d 0 0
c c f 0 0
d e f 0 1
e a f 0 1
f g f 0 1
g a f 0 1
a
b
5
State Reduction Technique 2/
Step 2: Inspect the state table for equivalent states
Equivalent states: Two states,
1. that produce exactly the same output
2. whose next states are identical
for each input combination
6
State Reduction Technique 3/
present state next state Output
x = 0 x = 1 x = 0 x = 1
a a b 0 0
b c d 0 0
c c f 0 0
d e f 0 1
e a f 0 1
f g f 0 1
g a f 0 1
- States “e” and “g” are equivalent
- One of them can be removed
e
7
State Reduction Technique 4/
We keep looking for equivalent states
present state next state Output
x = 0 x = 1 x = 0 x = 1
a a b 0 0
b c d 0 0
c c f 0 0
d e f 0 1
e a f 0 1
f e f 0 1
d
d
d
8
State Reduction Technique 5/
present state next state Output
x = 0 x = 1 x = 0 x = 1
a a b 0 0
b c d 0 0
c c d 0 0
d e d 0 1
e a d 0 1
We keep looking for equivalent states
b
15
Combinational circuits
can be designed given a truth table
Sequential circuits
We need,
◼ state diagram or
◼ state table
Two parts
◼ flip-flops: number of flip-flops is determined by the number of
states
◼ combinational part:
◼ output equations
◼ flip-flop input equations
Designing Sequential Circuits
16
Design Process
Once we know the number of flip-flops, design process
is reduced to design process of combinational circuits
Therefore, we can apply the techniques of
combinational circuit design
The design steps
1. Given a verbal description of desired operation, derive
state diagram
2. Reduce the number of states if necessary and possible
3. State assignment
17
4. Obtain the encoded state table
5. Derive the simplified flip-flop input equations
6. Derive the simplified output equations
7. Draw the logic diagram
Example: Verbal description
“we want a (sequential) circuit that detects three or more
consecutive 1’s in a string of bits”
Input: string of bits of any length
Output:
◼ “1” if the circuit detects the pattern in the string
◼ “0” otherwise
Design Steps
18
Example: State Diagram
Step 1: Derive the state diagram
S
0
/0 S
1
S
2
S /
3
Moore Machine
19
Synthesis with D Flip-Flops 1/
The number of flip-flops
Four states
2 or 4 flip-flops
State reduction
not possible in this case
State Assignment
Use binary encoding
◼ S
0
◼ S
1
◼ S
2
◼ S
3
S 0
/0 S 1
/
S 2
S / 3
/
0
1
0
1
0
1
1
0
20
Synthesis with D Flip-Flops 2/
Step 4: Obtain the state table
Present state Input Next state Output
A B x A B y
S 0
/0 S 1
/
S 2
S / 3
/
0
1
0
1
0
1
1
0
21
Synthesis with D Flip-Flops 3/
Step 5: Choose the flip-flops
D flip-flops
Step 6: Derive the simplified flip-flop input equations
Boolean expressions for D
A
and D
B
Present state Input Next state Output
A B x A B y
0 0 0 0 0 0
0 0 1 0 1 0
0 1 0 0 0 0
0 1 1 1 0 0
1 0 0 0 0 0
1 0 1 1 1 0
1 1 0 0 0 1
1 1 1 1 1 1
Bx
A 00 01 11 10
D
A
= Ax + Bx
22
Synthesis with D Flip-Flops 4/
Bx
A 00 01 11 10
D
B
= Ax + B’x
Present state Input Next state Output
A B x A B y
0 0 0 0 0 0
0 0 1 0 1 0
0 1 0 0 0 0
0 1 1 1 0 0
1 0 0 0 0 0
1 0 1 1 1 0
1 1 0 0 0 1
1 1 1 1 1 1
• Step 7: Derive the simplified
output equations
- Boolean expressions for y.
Bx
A 00 01 11 10
y = AB
23
Synthesis with D Flip-Flops 5/
Step 8: Draw the logic diagram
D
A
= Ax + Bx
D
B
= Ax + B’x
y = AB
D Q
C
R
D
Q
C
R
A
B
y
D
A
D
B
x
clock reset
24
Synthesis with T Flip-Flops 1/
Example: 3-bit binary counter with T flip-flops
S
0
S
1
S
7
S
2
S
6
S
3
S
5
S
4
How many flip-flops?
State assignments:
• S
0
• S
1
• S
2
• S
7
State Diagram
25
Synthesis with T Flip-Flops 2/4 – State Table
T
0
T
1
T
2
A
0
A
1
A
2
A
0
A
1
A
2
present state next state FF inputs
26
Synthesis with T Flip-Flops 3/
Present state FF inputs
A 2
A 1
A 0
T 2
T 1
T 0
0 0 0 0 0 1
0 0 1 0 1 1
0 1 0 0 0 1
0 1 1 1 1 1
1 0 0 0 0 1
1 0 1 0 1 1
1 1 0 0 0 1
1 1 1 1 1 1
A
1
A
0
A
2
• Flip-Flop input equations
T
2
= A
1
A
0
A
1
A
0
A
2
T
1
= A
0
T
0
33
Unused States
S
0
S
1
S
2
S
3
S
4
Modulo-5 counter
Present State Next State
A B C A B C
34
Example: Unused States 1/
Present State Next State
A B C A B C
BC
A 00 01 11 10
1 0 X X X
A(t+1) = BC
BC
A 00 01 11 10
1 0 X X X
B(t+1) = B’C + BC’
BC
A 00 01 11 10
1 0 X X X
C(t+1) = A’C’
35
Example: Unused States 2/
A B C A B C
Present State Next State
A(t+1) = BC
B(t+1) = B C
C(t+1) = A’C’
36
Example: Unused States 3/
Not using don’t care conditions
Present State Next State
A B C A B C
BC
A 00 01 11 10
A(t+1) = A’BC
BC
A 00 01 11 10
B(t+1) = A’B’C + A’BC’
= A’(B C)
BC
A 00 01 11 10
C(t+1) = A’C’
37
Example: Unused States 4/
Present State Next State
A B C A B C
A(t+1) = A’BC
B(t+1) = A’(B C)
C(t+1) = A’C’
38
Sequential Circuit Timing 1/
clk
D
Q
t s
t
h
t p, FF
It is important to analyze the timing behavior of a sequential
circuit
Ultimate goal is to determine the maximum clock frequency
D Q
C
R
39
clock
inputs outputs
current
state Flip-Flop
inputs
Combinational Circuit
Flip-Flops
Q D
C
t
s
t p,COMB
t
p,FF
t p,FF
t p,COMB
t s
clk
t p
t
p
t
p,FF
+ t
p,COMB
s
Sequential Circuit Timing 2/
40
Sequential Circuit Timing 3/
Minimum clock period (or maximum clock frequency)
t
p
t p,FF
t
p,COMB
t
s
clk
t p,FF
t p,COMB
t
s
t
p
clk
41
Example: Counter
S
0
S
1
S
2
S
3
D
Q
C
x
1
D Q
C
x
0
x
0
t
p,XOR
= 2.0 ns
t
p,FF
= 2.0 ns
t
s
= 1.0 ns
t
p
= t
p,FF
p,XOR
s
= 2.0 + 2.0 + 1.0 = 5.0 ns
f max
= 1/t p
= 1/(5.0× 10
) 200 MHz
Binary encoding
42
Example: One-Hot-Encoding
S
0
S
1
S
2
S
3
t p,FF
= 2.0 ns
t
s
= 1.0 ns
t p
= t p,FF
= 2.0 + 1.0 = 3.0 ns
f max
= 1/t p
= 1/(3.0× 10
) 333 MHz
x
1
x
0
D Q
C
a
0
D Q
C
D
Q
C
D
Q
C
a
1
a
2
a
3
43
State-Diagram Based HDL Models
always @ () statement
For clocked sequential circuits we use “ always @ ()” statement
Inside we have signals such as “clock” and “reset”
always @ () statement may have a number of “edge events”
Examples:
// D flip-flop
module DFF(output reg Q, input D, clk, reset);
always @(posedge clk, posedge reset)
if (reset == 1) Q <= 1’b0;
else Q <= D;
endmodule
44
State-Diagram Based HDL Models
// T flip-flop
module TFF(output reg Q, input T, clk, reset);
always @(posedge clk, negedge reset)
if (reset == 0) Q <= 1’b0;
else Q <= T ^ Q;
endmodule
1
CS 303 - Logic & Digital System Design
CS 303
Registers and Counters
Sabancı University
- Some slides are adapted from DDCA and earlier offerings of this course
2
Registers
Registers are clocked sequential circuits
A register is a group of flip-flops
Each flip-flop capable of storing one bit of information
An n-bit register
◼ consists of n flip-flops
◼ capable of storing n bits of information
besides flip-flops, a register usually contains combinational logic to
perform some simple tasks
In summary
◼ flip-flops to hold information
◼ combinational logic to control the state transition
Sabancı University
3
Counters
A counter is essentially a register that goes through a
predetermined sequence of states → “Counting sequence”
Register
FF
FF
FF
n- 1
Combinational logic
Sabancı University
4
Uses of Registers and Counters
Registers are useful for storing and manipulating
information
internal registers in microprocessors to manipulate data
Counters are extensively used in control logic
PC (program counter) in microprocessors
Sabancı University
5
4 - bit Register
REG
Q
3
Q
2
Q
1
Q
0
D
3
D
2
D
1
D
0
clear
D
Q
clock
C
R
D
Q
C
R
D Q
C
R
D Q
C
R
clear
D
0
D
1
D
2
D
3
Q
0
Q
1
Q
2
Q
3
FD16CE
D[15:0]
Q[15:0]
CE
C
CLR
Sabancı University
6
Register with Parallel Load
Load
D Q
C
R
Q
0
D Q
C
R
Q
1
D Q
C
R
Q
2
D Q
C
R
Q
3
clock
clear
D
1
D
2
D
3
D
0
7
Register Transfer 1/
R
1
R
2
n
load
clock
load
clock
R
2
R
1
R
R 010…
2
8
Register Transfer 2/
R
1
R
1
+ R
2
clock
R
1
R
2
n
n-bit
adder
n
load
9
Shift Registers
A register capable of shifting its content in one or both
directions
Flip-flops in cascade
serial
input
serial
output
D
Q
C
SI
D
Q
C
D Q
C
D
Q
C
SO
clock
- The state of an n-bit shift register can be transferred in n clock cycles
10
Serial Mode
A digital system is said to operate in serial mode when
information is transferred and manipulated one bit a time.
shift register A shift register B
SO
SI
SI SO
clk clk
clock
shift
control
clock
shift
control
clk
T
1
T
2
T
3
T
4
11
Serial Transfer
Suppose we have two 4-bit shift registers
After T 4
After T
3
After T 2
After T
1
initial value 1 0 1 1 0 0 1 0
Timing pulse Shift register A Shift register B
B A
clock
shift
control
clk
T
1
T
2
T
3
T
4
A B
clk clk
clock
shift
control
12
12
Universal Shift Register
Capabilities:
1. A “clear” control to set the register to 0.
2. A “clock” input
3. A “shift-right” control
4. A “shift-left” control
5. n input lines & a “parallel-load” control
6. n parallel output lines