Professional Documents
Culture Documents
CSE369 Wi22 06 BuildingBlocks I
CSE369 Wi22 06 BuildingBlocks I
CSE369 Wi22 06 BuildingBlocks I
Teaching Assistants:
Andrea Dang Ethan Coffey
Hafsa Khan Jason Li
Tilboon Elberier Yui Suzuki
L6: Building Blocks I CSE369, Winter 2022
2
L6: Building Blocks I CSE369, Winter 2022
initial
divided_clocks = 0;
endmodule
3
L6: Building Blocks I CSE369, Winter 2022
Outline
❖ FSM Design
❖ Multiplexors
❖ Adders
4
L6: Building Blocks I CSE369, Winter 2022
5
L6: Building Blocks I CSE369, Winter 2022
6
L6: Building Blocks I CSE369, Winter 2022
Subdividing FSMs
❖ Some problems best solved with multiple pieces
❖ “Psychic Tester”
▪ Machine generates a 4-bit pattern
▪ User tries to guess 8 patterns in a row to be deemed psychic
❖ States?
7
L6: Building Blocks I CSE369, Winter 2022
Subdividing FSMs
❖ Pieces?
▪ Generate/pick pattern
▪ Check guess
8
L6: Building Blocks I CSE369, Winter 2022
Subdividing FSMs
❖ Pieces?
▪ Generate/pick pattern
• module genPatt(pattern, next, clock);
▪ User input (guess)
• module userIn(guess, enable, KEY);
▪ Check guess
• module checkGuess(correct, guess, pattern);
▪ Count correct guesses
• module countRight(psychic, next, correct,
enable, clock);
9
L6: Building Blocks I CSE369, Winter 2022
Outline
❖ FSM Design
❖ Multiplexors
❖ Adders
10
L6: Building Blocks I CSE369, Winter 2022
Data Multiplexor
❖ Multiplexor (“MUX”) is a selector
▪ Direct one of many (N = 2s) 𝑛-bit wide inputs onto output
▪ Called a 𝑛-bit, N-to-1 MUX
❖ Example: 𝑛-bit 2-to-1 MUX
▪ Input S (s bits wide) selects between two inputs of 𝑛 bits
each
This input is passed to
output if selector bits
N inputs match shown value
11
L6: Building Blocks I CSE369, Winter 2022
s a b c
❖ Truth Table: 0 0 0 0
0 0 1 0 ❖ Circuit Diagram:
0 1 0 1
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1
12
L6: Building Blocks I CSE369, Winter 2022
13
L6: Building Blocks I CSE369, Winter 2022
14
L6: Building Blocks I CSE369, Winter 2022
0
1
2
3
4 8:1
F
5 MUX
6
7
S
S1 0
S2
15
L6: Building Blocks I CSE369, Winter 2022
16
L6: Building Blocks I CSE369, Winter 2022
Outline
❖ FSM Design
❖ Multiplexors
❖ Adders
17
L6: Building Blocks I CSE369, Winter 2022
63 00111111 64 01000000
+ 8 +00001000 - 8 -00001000
71 01000111 56 00111000
18
L6: Building Blocks I CSE369, Winter 2022
❖ Properties:
▪ In 𝑛 bits, represent integers −2𝑛−1 to 2𝑛−1 − 1
▪ Positive number encodings match –1 +0
–2 1111 0000 +1
unsigned numbers
–3 1110 0001 +2
▪ Single zero (encoding = all zeros) 1101 0010
–4 +3
❖ Negation procedure: 1100
Two’s
0011
❖ 4-bit examples:
Two’s Un Two’s Un
0 0 1 0 +2 2 1 0 0 0 -8 8
+ 1 1 0 0 -4 12 + 0 1 0 0 +4 4
0 1 1 0 +6 6 1 1 1 1 -1 15
- 0 0 1 0 +2 2 - 1 1 1 0 -2 14
20
L6: Building Blocks I CSE369, Winter 2022
Carry = a0 b0
Sum = 𝑎0 ⊕ b0
Carry-out bit
a0 b 0 c1 s0
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
21
L6: Building Blocks I CSE369, Winter 2022
𝒔𝒊 = XOR 𝑎𝑖 , 𝑏𝑖 , 𝑐𝑖
𝒄𝒊+𝟏 = MAJ 𝑎𝑖 , 𝑏𝑖 , 𝑐𝑖
𝒄𝒊+𝟏 = 𝑎𝑖 𝑏𝑖 + 𝑎𝑖 𝑐𝑖 + 𝑏𝑖 𝑐𝑖
Carry-in Carry-out
ci ai bi ci+1 si
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
22
L6: Building Blocks I CSE369, Winter 2022
b0
+ + +
23
L6: Building Blocks I CSE369, Winter 2022
Subtraction?
❖ Can we use our multi-bit adder to do subtraction?
▪ Flip the bits and add 1?
• X⊕1=X ഥ
• CarryIn0 (using full adder in all positions)
b0
+ + +
24
L6: Building Blocks I CSE369, Winter 2022
Multi-bit Adder/Subtractor
𝑥 ⊕ 1 = 𝑥ҧ Add 1
(flips the bits)
+ + +
This signal is only
high when you
perform subtraction
25
L6: Building Blocks I CSE369, Winter 2022
❖ Unsigned Overflow
▪ Result of add/sub is > UMax or < Umin
❖ Signed Overflow
▪ Result of add/sub is > TMax or < TMin
▪ (+) + (+) = (−) or (−) + (−) = (+)
26
L6: Building Blocks I CSE369, Winter 2022
Two’s Two’s
0 1 0 1 +5 1 1 0 1 -3
+ 0 0 1 0 +2 + 1 0 1 1 -5
27
L6: Building Blocks I CSE369, Winter 2022
+ + +
28
L6: Building Blocks I CSE369, Winter 2022
29
L6: Building Blocks I CSE369, Winter 2022
30
L6: Building Blocks I CSE369, Winter 2022
always_comb begin
s = a + b;
end
endmodule
always_comb begin
{cout, s} = cin + a + b;
end
endmodule
always_comb begin
D = B ^ {N{sub}}; // replication operator
{C2, S[N-2:0]} = A[N-2:0] + D[N-2:0] + sub;
{CF, S[N-1]} = A[N-1] + D[N-1] + C2;
OF = CF ^ C2;
end
endmodule
initial begin
#100; sub = 0; A = 4'b0101; B = 4'b0010; // 5 + 2
#100; sub = 0; A = 4'b1101; B = 4'b1011; // -3 + -5
#100; sub = 0; A = 4'b0101; B = 4'b0011; // 5 + 3
#100; sub = 0; A = 4'b1001; B = 4'b1110; // -7 + -2
#100; sub = 1; A = 4'b0101; B = 4'b1110; // 5 -(-2)
#100; sub = 1; A = 4'b1101; B = 4'b0101; // -3 - 5
#100; sub = 1; A = 4'b0101; B = 4'b1101; // 5 -(-3)
#100; sub = 1; A = 4'b1001; B = 4'b0010; // -7 - 2
#100;
end
endmodule
34