Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
0
4-to-2 Binary Encoder
E
1
E
0
1
0
0
1
0
0
0
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0
all others
E
1
E
0
0 0
0 1
1 0
1 1
X X
module binary_encoder(i, e);
input [3:0] i;
output [1:0] e;
reg e;
always @(i)
begin
if (i[0]) e = 2b00;
else if (i[1]) e = 2b01;
else if (i[2]) e = 2b10;
else if (i[3]) e = 2b11;
else e = 2bxx;
end
endmodule
What is the resulting circuit?
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 18
Priority Logic Priority Logic
Intent: if more than one input is
1, the resuIt is a don't-care.
0
E
1
E
0
0 0 0 1 0 0
0 0 1 0 0 1
0 1 0 0 1 0
1 0 0 0 1 1
all others X X
Code: if i[0] is 1, the resuIt is 00
regardIess of the other inputs.
i[0] takes the highest priority.
if (i[0]) e = 2b00;
else if (i[1]) e = 2b01;
else if (i[2]) e = 2b10;
else if (i[3]) e = 2b11;
else e = 2bxx;
end
1
i[0]
0
2'b00
1
i[1]
0
2'b01
1
i[2]
0
2'b10
1
i[3]
0
2'b11
2'bxx
e[1:0]
Inferred
Result:
! if-else and case statements are interpreted very IiteraIIy!
Beware of unintended priority Iogic.
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 19
Avoiding (Unintended) Priority Logic Avoiding (Unintended) Priority Logic
! Make sure that if-else and case statements are parallel
" If mutuaIIy excIusive conditions are chosen for each branch...
" ...then synthesis tooI can generate a simpIer circuit that evaIuates
the branches in paraIIeI
ParaIIeI Code: Minimized ResuIt:
module binary_encoder(i, e);
input [3:0] i;
output [1:0] e;
reg e;
always @(i)
begin
if (i == 4b0001) e = 2b00;
else if (i == 4b0010) e = 2b01;
else if (i == 4b0100) e = 2b10;
else if (i == 4b1000) e = 2b11;
else e = 2bxx;
I
3
I
1
I
0
E
0
E
1
end
endmodule
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 20
Interconnecting ModuIes Interconnecting ModuIes
! ModuIarity is essentiaI to the success of Iarge designs
! A VeriIog module may contain submoduIes that are "wired together"
! High-IeveI primitives enabIe direct synthesis of behavioraI descriptions (functions such
as additions, subtractions, shifts (<< and >>), etc.
ExampIe: A 32-bit ALU Function TabIe
A[31:0] B[31:0]
+ - *
0 1 0 1
32'd1 32'd1
00 01 10
F[0]
F[2:1]
F2 F1 F0
0 0 0
0 0 1
0 1 0
F[2:0]
0 1 1
1 0 X
Function
A + B
A + 1
A - B
A - 1
A * B
R[31:0]
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 21
ModuIe Definitions ModuIe Definitions
2-to-1 MUX 3-to-1 MUX
module mux32two(i0,i1,sel,out);
module mux32three(i0,i1,i2,sel,out);
input [31:0] i0,i1,i2;
input [31:0] i0,i1;
input [1:0] sel;
input sel;
output [31:0] out;
output [31:0] out;
reg [31:0] out;
assign out = sel ? i1 : i0;
always @ (i0 or i1 or i2 or sel)
begin
endmodule
case (sel)
2b00: out = i0;
2b01: out = i1;
2b10: out = i2;
default: out = 32bx;
endcase
end
endmodule
32-bit Adder 32-bit Subtracter 16-bit MuItipIier
module mul16(i0,i1,prod);
module add32(i0,i1,sum); module sub32(i0,i1,diff);
input [15:0] i0,i1;
input [31:0] i0,i1; input [31:0] i0,i1;
output [31:0] prod;
output [31:0] sum; output [31:0] diff;
// this is a magnitude multiplier
assign sum = i0 + i1; assign diff = i0 - i1;
// signed arithmetic later
assign prod = i0 * i1;
endmodule endmodule
endmodule
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 22
Top Top- -LeveI ALU DecIaration LeveI ALU DecIaration
A[31:0] B[31:0]
! Given submoduIes:
alu
module mux32two(i0,i1,sel,out);
module mux32three(i0,i1,i2,sel,out);
module add32(i0,i1,sum);
module sub32(i0,i1,diff);
module mul16(i0,i1,prod);
+ - *
0 1 0 1
32'd1 32'd1
00 01 10
F[0]
F[2:1]
F[2:0]
! DecIaration of the ALU ModuIe:
module alu(a, b, f, r);
input [31:0] a, b;
input [2:0] f;
output [31:0] r;
R[31:0]
wire [31:0] addmux_out, submux_out;
wire [31:0] add_out, sub_out, mul_out;
mux32two adder_mux(b, 32'd1, f[0], addmux_out);
mux32two sub_mux(b, 32'd1, f[0], submux_out);
add32 our_adder(a, addmux_out, add_out);
sub32 our_subtracter(a, submux_out, sub_out);
mul16 our_multiplier(a[15:0], b[15:0], mul_out);
mux32three output_mux(add_out, sub_out, mul_out, f[2:1], r);
endmodule
moduIe
names
(unique)
instance
names
corresponding
wires/regs in
moduIe alu
intermediate output nodes
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 23
SimuIation SimuIation
addition subtraction muItipIier
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 24
More on ModuIe Interconnection More on ModuIe Interconnection
! ExpIicit port naming aIIows port mappings in arbitrary
order: better scaIing for Iarge, evoIving designs
Given Submodule Declaration:
module mux32three(i0,i1,i2,sel,out);
Module nstantiation with Ordered Ports:
mux32three output_mux(add_out, sub_out, mul_out, f[2:1], r);
Module nstantiation with Named Ports:
mux32three output_mux(.sel(f[2:1]), .out(r), .i0(add_out),
.i1(sub_out), .i2(mul_out));
submoduIe's
port name
corresponding
wire/reg in
outer moduIe
! BuiIt-in VeriIog gate primitives may be instantiated as weII
" Instantiations may omit instance name and must be ordered:
buf(out1,out2,...,outN, in); and(in1,in2,...inN,out);
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 25
UsefuI BooIean Operators UsefuI BooIean Operators
! Bitwise operators perform bit-sIiced operations on vectors
" ~(4'b0101) = {~0,~1,~0,~1} = 4'b1010
" 4'b0101 & 4'b0011 = 4'b0001
! LogicaI operators return one-bit (true/faIse) resuIts
" !(4'b0101) = ~1 = 1'b0
! Reduction operators act on each bit of a singIe input vector
" &(4'b0101) = 0 & 1 & 0 & 1 = 1'b0
! Comparison operators perform a BooIean test on two arguments
Bitwise LogicaI Reduction Comparison
~a NOT
a & b AND
a | b OR
a ^ b XOR
a ~^ b XNOR
!a NOT
a && b AND
a || b OR
&a AND
~& NAND
| OR
~| NOR
^ XOR
a < b
a > b
a <= b
Relational
a >= b
a == b
a != b
[in]equality
returns x when x
or z in bits. Else
returns 0 or 1
a === b case
a !== b [in]equality
returns 0 or 1
based on bit by bit
comparison
Note distinction between ~a and !a
L3: 6.111 Spring 2004 Introductory DigitaI Systems Laboratory 26
Combinational Logic Review Combinational Logic Review
Combinational
Circuit
in
0
in
1
in
N-1
in
0
in
1
in
M-1
Combinational logic circuits are memoryless
No feedback in combinational logic circuits
Output assumes the function implemented by the
logic network, assuming that the switching
transients have settled
Outputs can have multiple logical transitions
before settling to the correct value
L4: 6.111 Spring 2004 Introductory Digital Systems Laboratory 2
A Sequential System A Sequential System
Sequential circuits have memory (i.e., remember the past)
The current state is held in memory and the next state is
computed based the current state and the current inputs
In a synchronous systems, the clock signal orchestrates the
sequence of events
L4: 6.111 Spring 2004 Introductory Digital Systems Laboratory 3
A Simple Example A Simple Example
Adding N inputs (N-1 Adders)
in
0
in
1
in
2
in
N-1
Using a sequential (serial) approach
in
D Q
reset
clk
Current_Sum
L4: 6.111 Spring 2004 Introductory Digital Systems Laboratory 4
L5: 6.111 Spring 2004 4 Introductory Digital Systems Laboratory
The Sequential The Sequential always always Block Block
Edge-triggered circuits are described using a sequential
always block
Combinational Sequential
module combinational(a, b, sel,
out);
input a, b;
input sel;
output out;
reg out;
always @ (a or b or sel)
begin
if (sel) out = a;
else out = b;
end
endmodule
module sequential(a, b, sel,
clk, out);
input a, b;
input sel, clk;
output out;
reg out;
always @ (posedge clk)
begin
if (sel) out <= a;
else out <= b;
end
endmodule
1
0
sel
1
0
sel
out
a
D Q
a
out
b b
clk
L5: 6.111 Spring 2004 5 Introductory Digital Systems Laboratory
Importance of the Sensitivity List Importance of the Sensitivity List
The use of posedge and negedge makes an always block sequential
(edge-triggered)
Unlike a combinational always block, the sensitivity list does
determine behavior for synthesis!
D Flip-flop with synchronous clear D Flip-flop with asynchronous clear
module dff_sync_clear(d, clearb,
clock, q);
input d, clearb, clock;
output q;
reg q;
always @ (posedge clock)
begin
if (!clearb) q <= 1'b0;
else q <= d;
end
endmodule
module dff_async_clear(d, clearb, clock, q);
input d, clearb, clock;
output q;
reg q;
always @ (negedge clearb or posedge clock)
begin
if (!clearb) q <= 1b0;
else q <= d;
end
endmodule
always block entered only at
each positive clock edge
always block entered immediately
when (active-low) clearb is asserted
Note: The following is incorrect syntax: always @ (clear or negedge clock)
If one signal in the sensitivity list uses posedge/negedge, then all signals must.
Assign any signal or variable from only one always block, Be
wary of race conditions: always blocks execute in parallel
L5: 6.111 Spring 2004 6 Introductory Digital Systems Laboratory
Simulation Simulation
DFF with Synchronous Clear
t
c-q
Clear on Clock Edge
DFF with Asynchronous Clear
Clear happens on falling edge of clearb
L5: 6.111 Spring 2004 7 Introductory Digital Systems Laboratory
Blocking vs. Blocking vs. Nonblocking Nonblocking Assignments Assignments
1. Evaluate a | b but defer assignment of x
2. Evaluate a^b^c but defer assignment of y
3. Evaluate b&(~c) but defer assignment of z
1. Evaluate a | b, assign result to x
2. Evaluate a^b^c, assign result to y
3. Evaluate b&(~c), assign result to z
always @ (a or b or c)
begin
x = a | b;
y = a ^ b ^ c;
z = b & ~c;
end
always @ (a or b or c)
begin
x <= a | b;
y <= a ^ b ^ c;
z <= b & ~c;
end
Verilog supports two types of assignments within always blocks, with
subtly different behaviors.
Blocking assignment: evaluation and assignment are immediate
Nonblocking assignment: all assignments deferred until all right-hand
sides have been evaluated (end of simulation timestep)
Sometimes, as above, both produce the same result. Sometimes, not!
4. Assign x, y, and z with their new values
L5: 6.111 Spring 2004 8 Introductory Digital Systems Laboratory
Assignment Styles for Sequential Logic Assignment Styles for Sequential Logic
D Q D Q D Q in out
q1 q2
clk
Flip-Flop Based
Digital Delay
Line
Will nonblocking and blocking assignments both produce
the desired result?
module nonblocking(in, clk, out);
input in, clk;
output out;
reg q1, q2, out;
always @ (posedge clk)
begin
q1 <= in;
q2 <= q1;
out <= q2;
end
endmodule
module blocking(in, clk, out);
input in, clk;
output out;
reg q1, q2, out;
always @ (posedge clk)
begin
q1 = in;
q2 = q1;
out = q2;
end
endmodule
L5: 6.111 Spring 2004 9 Introductory Digital Systems Laboratory
Use Use Nonblocking Nonblocking for Sequential Logic for Sequential Logic
always @ (posedge clk)
begin
q1 = in;
q2 = q1;
out = q2;
end
always @ (posedge clk)
begin
q1 <= in;
q2 <= q1;
out <= q2;
end
At each rising clock edge, q1 = in.
After that, q2 = q1 = in.
After that, out = q2 = q1 = in.
Therefore out = in.
At each rising clock edge, q1, q2, and out
simultaneously receive the old values of in,
q1, and q2.
D Q D Q D Q
D Q
q1 q2
q1 q2
in in out out
clk clk
Blocking assignments do not reflect the intrinsic behavior of multi-stage
sequential logic
Guideline: use nonblocking assignments for sequential
always blocks
L5: 6.111 Spring 2004 10 Introductory Digital Systems Laboratory
Simulation Simulation
Non-blocking Simulation
Blocking Simulation
L5: 6.111 Spring 2004 11 Introductory Digital Systems Laboratory
Use Blocking for Combinational Logic Use Blocking for Combinational Logic
x <= a & b;
Assignment completion
(Given) Initial Condition
a changes;
always block triggered
a b c x y Deferred
1 1 0 1 1
0 1 0 1 1
0 1 0 1 1 x<=0
0 1 0 1 1 x<=0, y<=1
0 1 0 0 1
y <= x | c;
Nonblocking Behavior
x = a & b;
(Given) Initial Condition
a changes;
always block triggered
y = x | c;
Blocking Behavior a b c x y
1 1 0 1 1
0 1 0 1 1
0 1 0 0 1
0 1 0 0 0
module blocking(a,b,c,x,y);
input a,b,c;
output x,y;
reg x,y;
always @ (a or b or c)
begin
x = a & b;
y = x | c;
end
endmodule
a
b
c
x
y
module nonblocking(a,b,c,x,y);
input a,b,c;
output x,y;
reg x,y;
always @ (a or b or c)
begin
x <= a & b;
y <= x | c;
end
endmodule
Nonblocking and blocking assignments will synthesize correctly. Will both
styles simulate correctly?
Nonblocking assignments do not reflect the intrinsic behavior of multi-stage
combinational logic
While nonblocking assignments can be hacked to simulate correctly (expand
the sensitivity list), its not elegant
Guideline: use blocking assignments for combinational always blocks
L5: 6.111 Spring 2004 12 Introductory Digital Systems Laboratory
The Asynchronous Ripple Counter The Asynchronous Ripple Counter
Clock
D Q
Q
D Q
Q
D Q
Q
D Q
Q
Count[0]
Count [3:0]
A simple counter architecture
uses only registers
(e.g., 74HC393 uses T-register and
negative edge-clocking)
Toggle rate fastest for the LSB
but ripple architecture leads to
large skew between outputs
Clock
Count [3]
Count [2]
Count [1]
Count [0]
Skew
Count[1] Count[2] Count[3]
D register set up to
always toggle: i.e., T
Register with T=1
L5: 6.111 Spring 2004 13 Introductory Digital Systems Laboratory
The Ripple Counter in The Ripple Counter in Verilog Verilog
module dreg_async_reset (clk, clear, d, q, qbar);
input d, clk, clear;
output q, qbar;
reg q;
always @ (posedge clk or negedge clear)
begin
if (!clear)
q <= 1'b0;
else q <= d;
end
assign qbar = ~q;
endmodule
Clock
D Q
Q
D Q
Q
D Q
Q
D Q
Q
Count[0]
Count [3:0]
Count[1] Count[2] Count[3]
Single D Register with Asynchronous Clear:
Structural Description of Four-bit Ripple Counter:
Countbar[0] Countbar[1] Countbar[2]
Countbar[3]
module ripple_counter (clk, count, clear);
input clk, clear;
output [3:0] count;
wire [3:0] count, countbar;
dreg_async_reset bit0(.clk(clk), .clear(clear), .d(countbar[0]),
.q(count[0]), .qbar(countbar[0]));
dreg_async_reset bit1(.clk(countbar[0]), .clear(clear), .d(countbar[1]),
.q(count[1]), .qbar(countbar[1]));
dreg_async_reset bit2(.clk(countbar[1]), .clear(clear), .d(countbar[2]),
.q(count[2]), .qbar(countbar[2]));
dreg_async_reset bit3(.clk(countbar[2]), .clear(clear), .d(countbar[3]),
.q(count[3]), .qbar(countbar[3]));
endmodule
L5: 6.111 Spring 2004 14 Introductory Digital Systems Laboratory
Simulation of Ripple Effect Simulation of Ripple Effect