Computer Engineering I (ECE290) : Digital System Design With Verilog
Computer Engineering I (ECE290) : Digital System Design With Verilog
HCM
KHOA ĐIỆN – ĐIỆN TỬ
COMPUTER ENGINEERING I
(ECE290)
Digital System Design
with Verilog
Bộ môn Điện tử
16/05/2009
Combinational Circuit
2
Outline
• Conditional operator
• If-else statement
• Case statement
• Casex, casez
• Examples
3
Combinational circuits
inputs
0
1
outputs
w0 y0
w1 y1
y2
En y3
5
2-to-1 multiplexer
s s f
w0 w0
f ( s, w0 , w1 ) s w0 sw1
0 0
f
w1 1 1 w1
w0 w0
s f s
w1 w1 f
6
4-to-1 multiplexer
s0
s1 s1 s0 f
w0 00 0 0 w0
w1 01
s1
f 0 1 w1
w2 10 w2
w3 11
1 0 s0
1 1 w3
s0 w0 0
w0 w1 1
s1
0
w1 f
1
f
w2 0
w2
w3 1
w3
7
Multiplexer examples
endmodule 8
Nesting conditional operators
endmodule
9
if-else
• Format
if (conditional_expression) statement;
else statement; begin…end
• Behavior
– If the conditional_expression is true, then the
first statement is executed;
– Or else the second statement is executed.
• Used inside an always block, the
statements are procedural.
10
if-else examples
endmodule
11
If… else if…else if
module mux4to1 (w0, w1, w2, w3, S, f); module mux4to1 (W, S, f);
input w0, w1, w2, w3; input [3:0] W;
input [1:0] S; input [1:0] S;
output f; output f;
reg f; reg f;
w0
w3
w4 s2
s3
w7
w8
w11
w12
w15
A 16-to-1 multiplexer. 13
Hierarchical 16-to-1 Multiplexer
endmodule 14
Case statement
• Format
1) Many possible alternatives
case(expression) 2) Expression and each alternative are
alternative1: statement; compared bit by bit.
alternative2: statement; 3) If there is a match, the statement is
executed
… 4) If the alternatives do not cover all
alternativej: statement; possibilities, default should be included.
[default: statement;] Otherwise, a sequential circuit will be
generated
endcase
15
Multiplexer using case
input [1:0] S; w0 00 0 0 w0
w1 01 w1
0 1
output f; w2 10
f
1 0 w2
w3
reg f; 11
1 1 w3
always @(W or S)
case (S)
0: f = W[0];
1: f = W[1];
or binary numbers 2: f = W[2];
3: f = W[3];
endcase
endmodule
16
2-to-4 decoder
En w1 w0 y 0 y1 y 2 y 3
w0 y0
1 0 0 1 0 0 0
w1 y1
1 0 1 0 1 0 0
y2
1 1 0 0 0 1 0
En y3
1 1 1 0 0 0 1
0 x x 0 0 0 0
(a) Truth table (b) Graphical symbol
17
Decoder using case
endmodule 18
Decoder using if-else, case
endmodule
19
Hierarchical code for 4-16 decoder
20
BCD-to-7-segment code
w3 w2 w1 w0 a b c d e f g
0 0 0 0 1 1 1 1 1 1 0
a a 0 0 0 1 0 1 1 0 0 0 0
w0 b 0 0 1 0 1 1 0 1 1 0 1
c f b
w1 0 0 1 1 1 1 1 1 0 0 1
w2 d g 0 1 0 0 0 1 1 0 0 1 1
e e c 0 1 0 1 1 0 1 1 0 1 1
w3
f
0 1 1 0 1 0 1 1 1 1 1
g d
0 1 1 1 1 1 1 0 0 0 0
1 0 0 0 1 1 1 1 1 1 1
(a) Code converter (b) 7-segment display 1 0 0 1 1 1 1 1 0 1 1
always @(bcd)
case (bcd) //abcdefg
0: leds = 7'b1111110;
1: leds = 7'b0110000;
2: leds = 7'b1101101;
3: leds = 7'b1111001;
4: leds = 7'b0110011;
5: leds = 7'b1011011;
6: leds = 7'b1011111;
7: leds = 7'b1110000; X don’t-care
8: leds = 7'b1111111;
9: leds = 7'b1111011;
default: leds = 7'bx;
endcase
endmodule 22
ALU — 74381
// 74381 ALU
module alu(s, A, B, F);
input [2:0] s;
input [3:0] A, B;
output [3:0] F;
reg [3:0] F;
always @(s or A or B)
case (s)
0: F = 4'b0000;
1: F = B - A;
2: F = A - B;
3: F = A + B;
4: F = A ^ B;
5: F = A | B;
6: F = A & B;
7: F = 4'b1111;
endcase
endmodule
23
casex and casez
24
Priority encoder
module priority (W, Y, z);
input [3:0] W;
output [1:0] Y;
output z;
reg [1:0] Y;
reg z;
always @(W)
begin
z = 1;
casex(W)
4'b1xxx: Y = 3;
4'b01xx: Y = 2;
4'b001x: Y = 1;
4'b0001: Y = 0;
default: begin
z = 0;
Y = 2'bx;
end
endcase
end
25
endmodule
Sequential circuit blocks
(1/2)
26
Outline
27
Verilog for Latch
endmodule
No else clause
Sensitivity list should include D and Clk
28
always block (overview)
30
Assignments (overview)
• Continuous assignments – fixed connection
– assign f1 = a && b;
– assign f2 = ~ f1;
• Blocking assignments – evaluate in order
– = in always block
• begin
Q1 = D; // new Q1 will be used in evaluating all subsequent statements in this
block
Q2 = Q1; // new Q1 goes to Q2, so Q2 is equal to D.
• end
• No-blocking assignments – evaluate in parallel
– <= in always block
• begin
Q1<= D;
Q2<= Q1; // old Q1 goes to Q2
• end
– The order of statements doesn’t matter
31
Blocking and non-blocking assignments
What happens
if we change the
Q1=0, Q2=0 Q1=0, Q2=10 order of two statements?
Q1=Q2 Q1,Q2 exchange
32
Blocking assign
endmodule
33
Two cascaded flip-flops
Non-blocking assignments
begin
Q1 <= D; Clock Q Q
Q2 <= Q1;
end
endmodule 34
Example 7.5 (blocking)
endmodule
D Q f
Clock Q
35
Example 7.6 (non-blocking)
module example7_6 (x1, x2, x3, Clock, f, g);
input x1, x2, x3, Clock;
output f, g;
reg f, g;
endmodule
x1
D Q f
x2
Clock Q
36
Recommendations
37
T flip-flop
T Qt + 1
0 Qt
module tff(t, clk,q);
1 Qt
input t, clk;
(b) Truth table
output q;
reg q;
38
Quiz: write code for J-K ff
J K Q t + 1
0 0 Q t J Q
0 1 0
1 0 1
K Q
1 1 Q t
39
Flip-flop with clear capability
endmodule
40
Flip-flop with clear capability
endmodule
41
N-bit register
Clock 42
4-bit shift register
module muxdff (D0, D1, Sel, Clock, Q); module shift4 (R, L, w, Clock, Q);
input D0, D1, Sel, Clock; input [3:0] R;
output Q; input L, w, Clock;
reg Q; output [3:0] Q;
wire [3:0] Q;
always @(posedge Clock)
if (!Sel) muxdff Stage3 (w, R[3], L, Clock, Q[3]);
Q <= D0; muxdff Stage2 (Q[3], R[2], L, Clock, Q[2]);
else muxdff Stage1 (Q[2], R[1], L, Clock, Q[1]);
Q <= D1; muxdff Stage0 (Q[1], R[0], L, Clock, Q[0]);
endmodule endmodule
D
clock
sel
0 1
Circuit ?
D0 D1
43
Sequential circuit blocks
(2/2)
44
Outline
• Shift register
• Counter
• Bus structure
45
4-bit shift register
module muxdff (D0, D1, Sel, Clock, Q); module shift4 (R, L, w, Clock, Q);
input D0, D1, Sel, Clock; input [3:0] R;
output Q; input L, w, Clock;
reg Q; output [3:0] Q;
wire [3:0] Q;
always @(posedge Clock)
if (!Sel) muxdff Stage3 (w, R[3], L, Clock, Q[3]);
Q <= D0; muxdff Stage2 (Q[3], R[2], L, Clock, Q[2]);
else muxdff Stage1 (Q[2], R[1], L, Clock, Q[1]);
Q <= D1; muxdff Stage0 (Q[1], R[0], L, Clock, Q[0]);
endmodule endmodule
D
sel clock
0 1
46
D0 D1
4-bit shift register
module shift4 (R, L, w, Clock, Q); module shiftn (R, L, w, Clock, Q);
parameter n = 16;
input [3:0] R;
input [n-1:0] R;
input L, w, Clock; input L, w, Clock;
output [3:0] Q; output [n-1:0] Q;
reg [3:0] Q; reg [n-1:0] Q;
always @(posedge Clock) integer k;
if (L)
Q <= R; always @(posedge Clock)
else if (L)
begin Q <= R;
else
Q[0] <= Q[1];
begin
Q[1] <= Q[2]; for (k = 0; k < n-1; k = k+1)
Q[2] <= Q[3]; Q[k] <= Q[k+1];
Q[3] <= w; Q[n-1] <= w;
end end
endmodule endmodule 48
Up-counter
endmodule
Comments: asynchronous reset (why?); counting if enable (E) is high. 49
Up-counter with parallel load
51
Frequency Divider by N
reg SlowClock;
reg [19:0] ClockDiv;
parameter Maxcount = 4;
always @ (posedge Clock or negedge Reset)
if(!Reset)
begin
ClockDiv <= 0;
SlowClock <= 0;
end
else if (ClockDiv == Maxcount)
begin
SlowClock <= 1;
ClockDiv <=0;
end
else
begin
SlowClock <= 0;
ClockDiv <= ClockDiv+1;
end
endmodule
52
Simulation result
53
Bus structure for digital systems
Data
Extern
Bus
Clock
R1 R2 Rk
Control circuit
Function 54
2-bit bus connecting: 2 registers
55
Swapping operation
(2)
R1 R2
w D Q D Q D Q
Clock Q Q Q
Reset 56
Verilog for swapping
Data
regn Extern
Bus
Clock
R1 R2 Rk
trin
R1in R 1 out R2in R 2 out Rkin Rk out
shiftr
Control circuit
Function 57
N-bit register module
59
Control circuit module
Bus
Clock
Data
S0
Multiplexers
Sj – 1
Control circuit
62
Swap using multiplexers
Q[1]: R 2 out
Q[1]: R2 out R 3in
Q[2]: R 1 out
Q[2]: R 1 out R 2in Q [1:3]: Q[1], Q[2], Q[3] Q[3]: R 3 out
Q[3]: R 3 out R 1in 63
Simulation
64
Sequential circuit blocks
Desgin Examples
65
Sequential circuit blocks
Design examples:
1) Reaction timer
2) Ring counter
3) Linear-feedback shift register
Functionality of reaction timer
67
Specifications
68
100 Hz clock
100 Hz
c9 c1 c0
102.4 KHz
69
The complete timer circuit
Digit1 Digit0
LEDn
pushn
w
c9
reset
70
Verilog code: 7-segment code
BCD1
clear
BCD counter BCD0
E
clock
72
Verilog code: BCD counter
73
Verilog code: reaction timer
module reaction (c9, Reset, w, Pushn, LEDn, Digit1, Digit0);
input c9, Reset, w, Pushn;
output LEDn;
output [1:7] Digit1, Digit0;
wire LEDn;
wire [1:7] Digit1, Digit0;
reg LED;
wire [3:0] BCD1, BCD0;
pushn
always @(posedge c9)
begin
if (Pushn == 0)
LED <= 0;
else if (w)
LED <= 1;
end
assign LEDn = ~LED;
BCDcount counter (c9, Reset, LED, BCD1, BCD0);
seg7 seg1 (BCD1, Digit1);
seg7 seg0 (BCD0, Digit0);
endmodule
74
Simulation
a Digit1: abc_defg
W (rising)LED on
counting Digit0: abc_defg
f g b
Person pushed the button stop counting 1: 011_0000
LED off 2: 110_1101
e c
Display the elapsed time (result of counting)
d
Common cathode
75
Ring counter
76
Verilog code for ring counter
77
Linear-feedback shift register
D Q D Q D Q
q0 q1 q2
clock
78
Linear-feedback shift register
module lfsr(r,L,clock,q);
input [0:2] r;
input L, clock;
output [0:2] q;
reg [0:2] q;
79
quiz
module lfsr2(r,L,clock,q);
input [0:2] r;
input L, clock;
output [0:2] q;
reg [0:2] q;
1. Draw the circuit using D-flip flips, multiplexers, and xor gate(s)
2. What is the sequence after loading r=001?
80
Sequential circuits
Finite State Machines
Outline
82
Two types of FSMs
Moore-type
Combinational Z
Combinational Q
Flip-flops circuit
circuit
W
Clock
Mealy-type
Combinational Z
Combinational Q
Flip-flops circuit
circuit
W
Clock
83
Example: 1-1 detector
Moore-type Mealy-type
The output z be equal to 1 in the clock The output z be equal to 1 in the same
Cycle that follows the detection of the clock cycle when the second w=1 is
second w=1 detected
Clockcycle: t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10
w: 0 1 0 1 1 0 1 1 1 0 1
z: 0 0 0 0 0 1 0 0 1 1 0
Moore-type Mealy-type
Reset
w = 1 z = 0
A B
w = 0 z = 0 w = 1 z = 1
w = 0 z = 0
Less states
85
Example: 1-1 detector
Moore-type Mealy-type
86
Example: 1-1 detector
Moore-type Mealy-type
z
Y2 y2
D Q z
Q w D Q
y
Clock Q
Y1 y1
w D Q
Q Resetn
Clock
Resetn
87
Example: 1-1 detector
Timing diagram
Moore-type
A C
B
Mealy-type
A B
88
W=1
Example: control circuit for swapping
Moore-type Mealy-type
4 states 3 states 89
Example: control circuit for swapping
By inspection
y3y2y1
A:0 0 1 Y1=wy1+y3 R2out=R3in=wy1
B:0 1 0
C:1 0 0 Y2=wy1 R1out=R2in=y2
Moore-type Mealy-type
w R 1 in
Y1 y1
D Q
R 3 out
y1
Clock Q Done
R 1 out
R 2 in
Y2 y2
D Q
y2
Q
R 2 out
R 3 in
91
Summary of FSMs
92
FSM design by CAD tools
// Define output
assign output_signal = ….;
endmodule 95
Finite state machines
Outline
• Three Verilog code styles for FSMs
• Verilog codes for Moore and Mealy
• Two examples
– Sequence detector
– Control circuit for swapping
• Design process by problem 8.9
– Specifications
– State diagram
– Code
– simulation
97
Verilog template for Moore FSMs
module FSM_name (Clock, Resetn, input_signal, output_signal);
input Clock, Resetn, input_signal;
output output_signal;
reg [n:1] state_present, STATE_NEXT;
parameter [n:1] STATE1 = 2'b00, STATE2 = 2'b01…. ;
3 parallel blocks:
// Define the next state combinational circuit
always @(input_signal or state_present)
1) always block:
case (state_present) combinational circuit for
STATE1: if (input_signal) STATE_NEXT = …; next state
else STATE_NEXT = …; 2) always block:
STATE2: if (input_signal) STATE_NEXT = …;
else STATE_NEXT = …; update states
…… 3) assign:
default: STATE_NEXT = n'bxx; combinational circuit for
endcase
output
// Define the sequential block
always @(negedge Resetn or posedge Clock)
if (Resetn == 0)y <= STATE1; Y y
elsey <= Y; Next state Output z
w Flip-flops
circuit circuit
// Define output
assign output_signal = ….;
clock resetn
endmodule 98
Example: sequence detector
module simple (Clock, Resetn, w, z); y: present state
input Clock, Resetn, w;
output z;
Y: next state
reg [2:1] y, Y;
parameter [2:1] A = 2'b00, B = 2'b01, C = 2'b10;
State assignment
// Define the next state combinational circuit
always @(w or y)
case (y)
A: if (w) Y = B;
else Y = A;
B: if (w) Y = C;
else Y = A;
C: if (w) Y = C;
else Y = A;
default: Y = 2'bxx;
endcase
// Define output
output_signal=….; Y y
end Next state Output z
w Flip-flops
// Define the sequential block circuit circuit
always @(negedge Resetn or posedge Clock)
if (Resetn == 0)y <= STATE1;
elsey <= Y; clock resetn
endmodule 100
Example: sequence detector
module simple (Clock, Resetn, w, z); y: present state
input Clock, Resetn, w;
output z;
Y: next state
reg z;
reg [2:1] y, Y;
parameter [2:1] A = 2'b00, B = 2'b01, C = 2'b10;
State assignment
// Define the next state combinational circuit
always @(w or y)
begin
case (y)
A: if (w) Y = B;
else Y = A;
B: if (w) Y = C;
else Y = A;
C: if (w) Y = C;
else Y = A;
default: Y = 2'bxx;
endcase
// Define output
z = (y == C);
end
// Define the sequential block
Y y
always @(negedge Resetn or posedge Clock) Next state Output z
w Flip-flops
if (Resetn == 0)y <= A; circuit circuit
elsey <= Y;
101
endmodule clock resetn
Third Verilog template
module FSM_name (Clock, Resetn, input_signal, output_signal);
input Clock, Resetn, input_signal;
output output_signal; 2 parallel blocks:
reg [n:1] state; // don’t need state_present, STATE_NEXT; 1) always block:
parameter [n:1] STATE1 = 2'b00, STATE2 = 2'b01…. ;
sequential circuit for
// Define the sequential block next state
and update
always @(negedge Resetn or posedge Clock)
2) assign block:
if (Resetn == 0) state <= STATE1;
else for output
case (state)
STATE1: if (input_signal) state < = …; assign cannot be put inside
else state < = …; the always block
STATE2: if (input_signal) state <= …;
else state < = …; merge
……
default: state <= n'bxx;
endcase
Y y
// Define output Next state Output z
w Flip-flops
assign output_signal=….; circuit circuit
clock resetn
endmodule 102
Example: sequence detector
module simple (Clock, Resetn, w, z);
input Clock, Resetn, w;
output z;
reg [2:1] y;
parameter [2:1] A = 2'b00, B = 2'b01, C = 2'b10;
// Define the sequential block
always @(negedge Resetn or posedge Clock)
if (Resetn == 0) y <= A;
else
case (y)
A: if (w) y <= B;
else y <= A;
B: if (w) y <= C;
else y <= A;
C: if (w) y <= C;
else y <= A;
default: y <= 2'bxx;
endcase
// Define output
assign z = (y == C);
endmodule
103
Summary of Verilog for FSMs
106
Example: Verilog for Mealy
module mealy (Clock, Resetn, w, z);
input Clock, Resetn, w;
output z;
reg y, Y, z; // Define the sequential block
parameter A = 0, B = 1; always @(negedge Resetn or posedge Clock)
// Define the next state and output if (Resetn == 0) y <= A;
//combinational circuits else y <= Y;
always @(w or y)
case (y) endmodule
A: if (w)
begin
z = 0; Y = B;
end
else
begin
z = 0; Y = A;
Reset
end w = 1 z = 0
B: if (w)
begin
z = 1; Y = B; A B
end
else
begin w = 0 z = 0 w = 1 z = 1
w = 0 z = 0
z = 0; Y = A;
end
endcase
107
Verilog template for Mealy FSMs
module FSM_name (Clock, Resetn, input_signal, output_signal);
input Clock, Resetn, input_signal;
output output_signal;
reg [n:1] state_present, STATE_NEXT;
parameter [n:1] STATE1 = 2'b00, STATE2 = 2'b01…. ;
2 parallel blocks:
// Define the next state combinational circuit and outputs 1) always block:
always @(input_signal or state_present) combinational circuit for
case (state_present) next state and output
STATE1: if (input_signal) define output and next state;
else define output and next state; 2) always block:
STATE2: if (input_signal) define output and next state; update states
else define output and next state;
……
default: define output and next state;
endcase
Problem 8.9 a sequential circuit has two inputs, w1 and w2, and an output, z.
Its function is to compare the input sequences on the two inputs. If w1 =w2 during
any four consecutive clock cycles, the circuit produces z=1; otherwise, z=0.
For example
w1: 0 1 1 0 1 1 1 0 0 0 1 1 0
w2: 1 1 1 0 1 0 1 0 0 0 1 1 1
z: 0 0 0 0 1 0 0 0 0 1 1 1 0
109
Prob8.9: state diagram
W=1 resetn
w w1 w2
A W1 != w2
Mealy FSM
W=0
W=1 B 1 equal
W=0
W=1 C 2 equal
W=0
W=1
D 3 equal
112
Prob8.9: simulation
113
Prob8.9: simulation problem
114
Verilog
Functions and Tasks
115
behavioral modeling
116
behavioral modeling
Differences
• Functions • Tasks
– Can enable another function – Can enable other tasks and
but not another task functions
– Always executes in zero – May execute in non-zero
simulation time simulation time
– Can not contain any delay, – May contain delay(#),
event, or timing control event(@), or timing control
statements statements (wait)
– Must have at least one input – May have zero or more input,
argument output or inout arguments
– Always return a single value – Do not return a value
– Can not have output or inout
arguments
117
Differences between… (cont’d)
• Both
– are defined in a module
– are local to the module
– can have local variables (registers, but not nets) and
events
– contain only behavioral statements
– do not contain initial or always statements
– are called from initial or always statements or other
tasks or functions
118
Differences between… (cont’d)
119
Tasks
Tasks
121
Tasks (cont’d)
task <task_name>;
<I/O declarations>
<variable and event declarations>
begin // if more than one statement needed
<statement(s)>
end // if begin used!
endtask
122
Tasks (cont’d)
124
behavioral modeling
Task
• Example
module tasks;
task add; definition of a task :
input a, b;
task <task name>;
output c;
reg R; <argument ports>
begin <declarations>
R=1; <statements>
if(a==b) c=1&R; endtask
else c=0;
end
endtask
initial begin: init1 invocation of a task :
<name of task> (<port list>);
reg p;
add(1, 0, p);
$display (“p= %b”, p);
end
endmodule 125
Task Examples - Use of input and output arguments
126
Task Examples
Use of module local variables
endmodule 127
Functions
Functions
129
Functions (cont’d)
130
Functions (cont’d)
131
Functions (cont’d)
• Semantics
– much like function in Pascal
– An internal implicit reg is declared inside the
function with the same name
– The return value is specified by setting that
implicit reg
– <range_or_type> defines width and type of the
implicit reg
• type can be integer or real
• default bit width is 1
132
behavioral modeling
Function
• Example
module functions;
function [1:1] add2;
definition of a function :
input a, b;
reg R; task <range or type><function name>;
begin <argument ports>
R=1; <declarations>
if(a==b) c=1&R; <statements>
else c=0; endtask
end
endfunction
initial begin: init1 invocation of a function :
reg p; value = <name of function> (<port list>);
p = add2(1,0);
$display (“p= %b”, p);
end
endmodule
133
Function Examples - Parity Generator
134
Function Examples - Controllable Shifter
always @(addr)begin
left_addr =shift(addr, `LEFT_SHIFT);
right_addr =shift(addr,`RIGHT_SHIFT);
end
135
Tasks and functions – Summary
136