Week 6 - Combinational Building Blocks
Week 6 - Combinational Building Blocks
1
One-hot representation
2
Decoder
Decoder
Binary input a to one-hot output b
b[i] = 1 if a = i a b
n m
b = 1<<a
n
m≤2
3
Example
2 4 Decoder
a1
a0
a1 a0 b3 b2 b1 b0
0 0 0 0 0 1
0 1 0 0 1 0 b3
1 0 0 1 0 0
1 1 1 0 0 0 b2
b1
b0
4
Can build a large decoder from several small
decoders
• Example – build a 6 : 64 decoder from three 2 : 4 decoders
• Each 2 : 4 decodes 2 bits
• a[5:4]→x[3:0], a[3:2]→y[3:0], a[1:0]→z[3:0]
• AND one bit each of x, y, and z to generate an output
• b[a] = x[a[5:4]] & y[a[3:2]] & z[a[1:0]]
a5 a4 x3 x2 x1 x0 a3 a2 y3 y2 y1 y0 a1 a0 z3 z2 z1 z0
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0
5
2-Stage decoders
a[5:0]
6 a[5:4] a[3:2] a[1:0]
2 2 2
4 4 4
z[3]
y[3] b63
x[3]
z[2]
y[3] b62
x[3]
x[3:0]
y[3:0]
z[3:0]
z[0]
y[0] b0
x[0]
6
Verilog implementation of a decoder
input [n-1:0] a ;
Decoder
output [m-1:0] b ;
7
Logic implementation using decoder
Example – 3-bit prime number function
3:8
in [2 :0 ] b [7 :0 ]
3 8 b [7 ]
b [5 ]
b [3 ] i sp ri me
b [2 ]
module Primed(in, isprime) ;
input [2:0] in ; b [1 ]
output isprime ;
wire [7:0] b ;
// compute the output as the OR of the required minterms
wire isprime = b[1] | b[2] | b[3] | b[5] | b[7] ;
// instantiate a 3->8 decoder
Dec #(3,8) d(in,b) ;
endmodule
8
Combinational Building Blocks – Encoder
9
Encoder
a3
a2
a1
a0
a3 a2 a1 a0 b1 b0
0 0 0 1 0 0
0 0 1 0 0 1
b1
0 1 0 0 1 0
1 0 0 0 1 1
b0
b0 = a3 a1
b1 = a3 a2
10
Designing a large encoder – 16 : 4 from five 4 : 2
encoders
Need an ‘any input true’ output on first rank of encoders
First rank encodes low bits, second rank encodes high bits
Repeat as needed
High bits
Low bits
4:2
a[15:12]
4 2
4:2
4:2
a[11:8] b[3:2]
4 2 2
One hot or all zero
4:2
a[7:4]
4 2
b[1:0]
4:2
a[3:0]
2
4 2
Summary
13
Multiplexer
• Multiplexer:
– n k-bit inputs
– n-bit one-hot select signal s
– Multiplexers are commonly used as data selectors
14
Multiplexer Implementation
15
// four input k-wide mux with one-hot select
module Mux4(a3, a2, a1, a0, s, b) ;
parameter k = 1 ;
input [k-1:0] a0, a1, a2, a3 ; // inputs
input [3:0] s ; // one-hot select
output[k-1:0] b ;
wire [k-1:0] b = ({k{s[0]}} & a0) |
({k{s[1]}} & a1) |
({k{s[2]}} & a2) |
({k{s[3]}} & a3) ;
endmodule
f h
# 0001 00
# 0010 01
# 0100 10
# 1000 11
module Mux3a(a2, a1, a0, s, b) ;
parameter k = 1 ;
input [k-1:0] a0, a1, a2 ; // inputs
input [2:0] s ; // one-hot select
output[k-1:0] b ;
reg [k-1:0] b ;
18
k-bit Binary-Select Multiplexer
s1
s0
a0
k b
k a3
an-1
k
a2
Decoder
b
s
a b
m n a1
a0
19
Implementing k-bit Binary-Select Multiplexer using Verilog
// 3:1 multiplexer with binary select (arbitrary width)
module Muxb3(a2, a1, a0, sb, b) ;
parameter k = 1 ;
input [k-1:0] a0, a1, a2 ; // inputs
input [1:0] sb ; // binary select
output[k-1:0] b ;
wire [2:0] s ;
a0
k b
an-1 k
k
Decoder
s a b
m n
// 3:1 multiplexer with binary select (arbitrary width)
module Muxb3a(a2, a1, a0, sb, b) ;
parameter k = 1 ;
input [k-1:0] a0, a1, a2 ; // inputs
input [1:0] sb ; // binary select
output[k-1:0] b ;
reg [k-1:0] b ;
endmodule
21
Implementing a large one-hot select mux using
small ones
assign b = ba | bb ;
22
Implementing a large binary-select mux using
small ones
23
Logic with Muxes
26
Arbiter
Arbiter
r g
n n
27
Logic Diagram of one Bit of an Arbiter
Two Implementations of a 4-bit Arbiter
29
Arbiter using Verilog with LSB as the highest priority
assign g = r & c ;
endmodule
30
Priority Encoder
• Priority encoder:
– n-bit input signal a
– m-bit output signal b
– b indicates the position of the first 1 bit in a
Encoder
Arbiter
r g a b
n n m
m= log2n
Encoder
Priority
a b
n m
31
Verilog for Priority Encoder
Encoder
Arbiter
r g a b
n n m
32
Summary
34
Equality Comparator
a3
a
Comparator
eq3
b3
n
eq a2
eq2
b2
eq
b a1
n eq1
b1
// equality comparator a0
eq0
module EqComp(a, b, eq) ; b0
parameter k=8;
input [k-1:0] a, b;
output eq ;
assign eq = (a==b) ;
endmodule
35
Magnitude Comparator
gtbi+1
a
Comparator
n Magnitude
gt
ai
b gti
n
eqi
// magnitude comparator bi
gtbi
module MagComp(a, b, gt) ;
parameter k=8 ;
input [k-1:0] a, b ;
output gt ;
wire [k-1:0] eqi = a ~^ b ;
wire [k-1:0] gti = a & ~b ;
wire [k:0] gtb {((eqi[k-1:0] & gtb[k-1:0]) | gti[k-1:0]), 1’b0} ;
wire gt = gtb[k] ;
endmodule
36
// Behavioral Magnitude comparator
module MagComp_b(a, b, gt) ;
parameter k=8 ;
input [k-1:0] a, b ;
output gt ;
assign gt = (a > b) ;
endmodule
37
Putting things together – Maximum unit
a
n n 1 ma x
Mux
0 n
n
a Comparator
Magnitude
a>b
gt
b
b
n
38
Shifters
module ShiftLeft(n, a, b) ;
parameter k=8 ;
parameter lk=3 ;
input [lk-1:0] n ;
input [k-1:0] a ;
output [2*k-2:0] b ;
assign b = a<<n ;
endmodule
39
module BarrelLeftShift(n, a, b) ;
parameter k=8 ;
parameter lk=3 ;
input [lk-1:0] n ;
input [k-1:0] a ;
output [k-1:0] b ;
wire [2*k-2:0] x = a<<n ;
assign b = x[k-1:0] | {1'b0, x[2*k-2:k]} ;
endmodule
40
Summary
42
Read-only memory (ROM)
a d
a ROM d
n m
43
Conceptual block diagram
w0
d0
w1
Decoder
d1
a
n
m
w 2n -1
d2n -1
m
2-D array implementation
w0
d0 d1 d2 d3
256 word x
16 bit/word ROM
64 rows x 64 columns
w1
Decoder
d4 d5 d6 d7
a7:2
6
a
8 w63
d252 d253 d254 d255
16 16 16 16
a1:0
Multiplexer
16
Read-write memory (RAM)
46
Dual-port RAM implementation
Programmable logic array