Professional Documents
Culture Documents
Lec05 ALU 1
Lec05 ALU 1
Spring 2022
Overview
Abstract Implementation View
3/30
Arithmetic
Where we’ve been: abstractions
• Instruction Set Architecture (ISA)
• Assembly and machine language
4/30
Arithmetic
Where we’ve been: abstractions
• Instruction Set Architecture (ISA)
• Assembly and machine language
zero ovf
1
1
A
32
ALU result
32
B
32 4
m (operation)
4/30
Review: VHDL
5/30
Review: VHDL (cont.)
Basic Structure
• Design entity-architecture descriptions
• Time-based execution (discrete event simulation) model
Entity == External
Design Entity-Architecture == Characteristics
Hardware Component
6/30
Review: Entity-Architecture Features
Entity
defines externally visible characteristics
7/30
Review: Entity-Architecture Features (cont.)
Architecture
defines the internal behavior or structure of the circuit
8/30
ALU VHDL Representation
entity ALU is
port(A, B: in std_logic_vector (31 downto 0);
m: in std_logic_vector (3 downto 0);
result: out std_logic_vector (31 downto 0);
zero: out std_logic;
ovf: out std_logic)
end ALU;
9/30
Machine Number Representation
• in real systems have to provide for more than just integers, e.g., fractions and real
numbers (and floating point) and alphanumeric (characters)
1 10/30
conventions define the relationships between bits and numbers
RISC-V Representation
11/30
Two’s Complement Operations
• Negating a two’s complement number – complement all the bits and then add a 1
• remember: “negate” and “invert” are quite different!
12/30
Design the RISC-V Arithmetic Logic Unit (ALU)
RV 32I:
add, sub, mul, mulh, mulhu, mulhsu, zero ovf
div, divu, rem, li, addi, sll, srl, 1
sra, or, xor, not, slt, sltu, slli, 1
A
srli, srai, andi, ori, xori, slti, 32
7 5 5 3 5 7
R-type: funct7 rs2 rs1 funct3 rd opcode
12 5 3 5 7
I-type: imm[11:0] rs1 funct3 rd opcode
14/30
Addition Unit
Building a 1-bit Binary Adder
c0=carry_in
A0 1-bit
FA S0
B0
c1
• Just connect the carry-out of the least significant bit FA to the
A1 1-bit
FA S1 carry-in of the next least significant bit and connect ...
B1
c2
A2 1-bit
FA S2
B2 • Ripple Carry Adder (RCA)
c3
• ,: simple logic, so small (low cost)
...
17/30
Glitch
Glitch
invalid and unpredicted output that can be read by the next stage and result in a wrong
action
18/30
Glitch in RCA
c0=carry_in
A0 1-bit A B carry_in carry_out S
FA S0
B0
c1
0 0 0 0 0
A1 1-bit
FA S1 0 0 1 0 1
B1
c2
0 1 0 0 1
A2 1-bit
FA S2 0 1 1 1 0
B2
c3 1 0 0 0 1
...
1 0 1 1 0
c31
1 1 0 1 0
A31 1-bit
FA S31 1 1 1 1 1
B31
c32=carry_out
19/30
But What about Performance?
CarryIn0
A0 1-bit Result0
B0 ALU
CarryOut0
CarryIn1
A1 1-bit Result1
B1 ALU
CarryOut1
CarryIn2
A2 1-bit Result2
B2 ALU
CarryOut2
CarryIn3
A3 1-bit Result3
B3 ALU
CarryOut3
20/30
A 32-bit Ripple Carry Adder/Subtractor
add/sub c0=carry_in
A0 1-bit
FA S0
l complement all the bits B0 c1
control A1 1-bit
(0=add,1=sub) B0 if control = 0 S1
FA
B0 !B0 if control = 1 B1
c2
A2 1-bit
FA S2
l add a 1 in the least significant bit B2 c3
...
A 0111 -> 0111
B - 0110 -> + 1001 c31
0001 1 A31 1-bit
1 0001 FA S31
B31
c32=carry_out
21/30
Minimal Implementation of a Full Adder
22/30
Tailoring the ALU to the ISA
• Also need to support the logic operations (and, nor, or, xor)
• Bit wise operations (no carry operation involved)
• Need a logic gate for each function and a mux to choose the output
• Also need to support the set-on-less-than instruction (slt)
• Uses subtraction to determine if (a − b) < 0 (implies a < b)
• Also need to support test for equality (bne, beq)
• Again use subtraction: (a − b) = 0 implies a = b
• Also need to add overflow detection hardware
• overflow detection enabled only for add, addi, sub
• Immediates are sign extended outside the ALU with wiring (i.e., no logic needed)
23/30
A Simple ALU Cell with Logic Op Support
add/subt carry_in op
result
1-bit
FA
B
add/subt carry_out
24/30
A Simple ALU Cell with Logic Op Support
add/subt carry_in op
A
0
1
2
3 result
1-bit
FA 6
B
less 7
add/subt carry_out
Modifying the ALU Cell for slt
24/30
Modifying the ALU for slt
A0
result0
B0 +
less
A1
• First perform a subtraction
• Make the result 1 if the subtraction yields a negative result1
result
B1 +
result . . .
• Tie the most significant sum bit (sign bit) to the low A31
order less input
result31
B31 +
less
0
set
25/30
Overflow Detection
Overflow occurs when the result is too large to represent in the number of bits
allocated
• adding two positives yields a negative
• or, adding two negatives gives a positive
• or, subtract a negative from a positive gives a negative
• or, subtract a positive from a negative gives a positive
0 1 1 1 1 0
0 1 1 1 7 1 1 0 0 –4
+ 0 0 1 1 3 + 1 0 1 1 –5
1 0 1 0 –6 0 1 1 1 7
26/30
Modifying the ALU for Overflow
op
add/subt
A0
result0
B0 +
less
A1
...
B1 + zero
• Enable overflow bit setting for signed 0 less
result31
+
B31
0 less overflow
set
27/30
Overflow Detection and Effects
28/30
New Instructions
29/30
30/30