DLD Module 7
DLD Module 7
1 2
• But by changing what goes into the adder inputs A, B and CI, we can
change the adder output S.
• This is also what we did to build the combined adder-subtractor circuit.
3 4
The multi-talented adder Modifying the adder inputs
• So we have one adder performing two separate functions. • By following the same approach, we can use an adder to compute other
• “Sub” acts like a function select input which determines whether the functions as well.
circuit performs addition or subtraction. • We just have to figure out which functions we want, and then put the
• Circuit-wise, all “Sub” does is modify the adder’s inputs A and CI. right circuitry into the “Input Logic” box .
5 6
A = 0000, B = X, CI = 0
7 8
Table of arithmetic functions Mapping the table to an adder
• Here are some of the different possible arithmetic operations. • This second table shows what the adder’s inputs should be for each of
• We’ll need some way to specify which function we’re interested in, so our eight desired arithmetic operations.
we’ve randomly assigned a selection code to each operation. – Adder input CI is always the same as selection code bit S0.
– B is always set to X.
– A depends only on S2 and S1.
S2 S1 S0 Arithmetic operation
• These equations depend on both the desired operations and the
0 0 0 X (transfer)
assignment of selection codes.
0 0 1 X+1 (increment)
0 1 0 X+Y (add) Selection code Desired arithmetic operation Required adder inputs
0 1 1 X+Y+1 S2 S1 S0 G (A + B + CI) A B CI
1 0 0 X + Y’ (1C subtraction) 0 0 0 X (transfer) 0000 X 0
1 0 1 X + Y’ + 1 (2C subtraction) 0 0 1 X+1 (increment) 0000 X 1
1 1 0 X–1 (decrement) 0 1 0 X+Y (add) Y X 0
1 1 1 X (transfer) 0 1 1 X+Y+1 Y X 1
1 0 0 X + Y’ (1C subtraction) Y’ X 0
1 0 1 X + Y’ + 1 (2C subtraction) Y’ X 1
1 1 0 X–1 (decrement) 1111 X 0
1 1 1 X (transfer) 1111 X 1
9 10
11 12
Primitive gate implementation Bitwise operations
• From the truth table, we can find • Most computers also support logical operations like AND, OR and NOT,
an MSP: but extended to multi-bit words instead of just single bits.
• To apply a logical operation to two words X and Y, apply the operation
S1 on each pair of bits Xi and Yi:
0 0 1 0
S2 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1
Yi AND 1 1 1 0 OR 1 1 1 0 XOR 1 1 1 0
1 0 1 0 1 1 1 1 01 01
13 14
13 && 25 = 1 because true && true = true • You can use bitwise-OR to generate a “broadcast address,” for sending
data to all machines on the local network.
• Bitwise operators are often used in programs to set a bunch of Boolean
options, or flags, with one argument. 192.168. 10. 43 = 11000000.10101000.00001010.00101011
• Easy to represent sets of fixed universe size with bits: | 0. 0. 0. 31 = 00000000.00000000.00000000.00011111
– 1: is member, 0 not a member. Unions: OR, Intersections: AND 192.168. 10. 63 = 11000000.10101000.00001010.00111111
15 16
Defining a logic unit Our simple logic unit
• A logic unit supports different logical
functions on two multi-bit inputs X and Y,
producing an output G.
• This abbreviated table shows four
possible functions and assigns a selection
code S to each. • Inputs:
– X (4 bits)
S1 S0 Output – Y (4 bits)
0 0 Gi = XiYi – S (2 bits)
0 1 Gi = Xi + Yi • Outputs:
1 0 Gi = Xi Yi – G (4 bits)
1 1 Gi = Xi’
17 18
Combining the arithmetic and logic units Our ALU function table
• Now we have two pieces of the puzzle: • This table shows a sample
– An arithmetic unit that can compute eight functions on 4-bit inputs. function table for an ALU.
S3 S2 S1 S0 Operation
– A logic unit that can perform four functions on 4-bit inputs. • All of the arithmetic operations
0 0 0 0 G=X
have S3=0, and all of the logical
0 0 0 1 G=X+1
operations have S3=1.
• We can combine these together into a single circuit, an arithmetic-logic 0 0 1 0 G=X+Y
unit (ALU). • These are the same functions we
0 0 1 1 G=X+Y+1
saw when we built our arithmetic
0 1 0 0 G = X + Y’
and logic units a few minutes ago.
0 1 0 1 G = X + Y’ + 1
• Since our ALU only has 4 logical 0 1 1 0 G=X–1
operations, we don’t need S2. The 0 1 1 1 G=X
operation done by the logic unit
1 x 0 0 G = X and Y
depends only on S1 and S0.
1 x 0 1 G = X or Y
1 x 1 0 G=XY
1 x 1 1 G = X’
19 20
A complete ALU circuit Comments on the multiplexer
21 22
23 24
1 2
Datapaths
• We’ll focus on computer architecture: how to assemble the
combinational and sequential components we’ve studied so far into a
complete computer.
• The datapath is the part of the central processing unit (CPU) that does
the actual computations.
3 4
Keeping it simple! An overview of CPU design
• Abstraction is very helpful in understanding processors. • We can divide the design of our CPU into three parts:
– Although we studied how devices like registers and muxes are built, – The datapath does all of the actual data processing.
we don’t need that level of detail here. – An instruction set is the programmer’s interface to CPU.
– You should focus more on what these component devices are doing, – A control unit uses the programmer’s instructions to tell the
and less on how they work. datapath what to do.
• Otherwise, it’s easy to get bogged down in the details, and datapath and
control units can be a little intimidating. • We’ll look in detail at a processor’s datapath, which is responsible for
doing all of the dirty work.
– An ALU does computations, as we’ve seen before.
– A limited set of registers serve as fast temporary storage.
– A larger, but slower, random-access memory is also available.
5 6
ALU ALU
Registers Registers
• A processor is just one big sequential circuit. • Fundamentally, the processor is just moving data between registers,
– Some registers are used to store values, which form the state. possibly with some ALU computations.
– An ALU performs various operations on the data stored in the • To describe this data movement more precisely, we’ll use a register
registers. transfer language.
– The objects in the language are registers.
– The basic operations are transfers, where data is copied from one
register to another.
• We can also use the ALU to perform arithmetic operations on the data
while we’re transferring it.
7 8
Register transfer language review (from Chapter 8) Register transfer operations (cont’d)
• Two-character names denote registers, such as R0, R1, DR, or SA. • We can apply arithmetic operations to registers.
• Arrows indicate data transfers. To copy the contents of the source
register R2 into the destination register R1 in one clock cycle: R1 R2 + R3
R3 R1 - 1
R1 R2
• Logical operations are applied bitwise. AND and OR are denoted with
• A conditional transfer is performed only if the Boolean condition in special symbols, to prevent confusion with arithmetic operations.
front of the colon is true. To transfer R3 to R2 when K = 1:
R2 R1 R2 bitwise AND
K: R2 R3
R3 R0 R1 bitwise OR
R2 sl R1 left shift
• Don’t confuse this register transfer language with assembly language, R2 sr R1 right shift
which we’ll discuss later.
9 10
Data input
n
• Much like words stored in a
RAM, individual registers are D data
n R0 WR Write
identified by an address. k
•
Data output DA D address
Here is a block symbol for a
k Register File
2 x n register file.
• We’ll use this block diagram to represent an n-bit register. – There are 2k registers, so k k
•
AA A address B address BA
There is a data input and a load input. register addresses are k
bits long.
– When Load = 1, the data input is stored into the register.
A data B data
• The register’s contents are always available on the output lines, inputs and outputs are n A B
regardless of the Load input. bits wide.
• The clock signal is not shown because it would make the diagram messy.
• Remember that the input and output lines are actually n bits wide!
11 12
Accessing the register file What’s inside the register file
• You can read two registers at • Here’s a 4 x n register file. (We’ll assume a 4 x n register file for all
once by supplying the AA and our examples.)
D
BA inputs. The data appears n
on the A and B outputs. n
n n
13 14
15 16
ALU functions Our first datapath
• For concrete examples, we’ll use the • Here is the most basic datapath.
ALU as it’s presented in the textbook. FS Operation
– The ALU’s two data inputs come D data
• The table of operations on the right is 00000 F=A from the register file. WR Write
17 18
R0 R1 + R3
WR
1
Write • Who exactly decides which registers WR Write
• Set the ALU’s function select input A data B data A data B data
FS = 00010 (A + B).
• Set DA = 00 and WR = 1. On the next A B A B
positive clock edge, the ALU result FS FS FS FS
00010
(R1 + R3) will be stored in R0.
ALU ALU
V V
C C
N N
Z Z
F F
19 20
We can access RAM also Reading from RAM
• Here’s a way to connect RAM n • To read from RAM, A data must n
into our existing datapath. supply the address.
• To write to RAM, we must give WR Write
D data • Set MW = 0 for reading. WR Write
D data
an address and a data value. DA D address • The incoming data will be sent DA D address
• These will come from the Register File to the register file for storage. Register File
registers. We connect A data AA A address B address BA • This means that the register AA A address B address BA
to the memory’s ADRS input, A data B data file’s D data input could come A data B data
and B data to the memory’s
RAM RAM
n
ADRS from either the ALU output or n
ADRS
n n
DATA input. DATA OUT the RAM. DATA OUT
•
+5V CS +5V CS
Set MW = 1 to write to the
FS FS
A B MW
1
WR
• A mux MD selects the source FS FS
A B MW
0
WR
D0
Q D1
n – When MD = 1, the RAM D0
Q D1
n
S MD output is sent to the S MD
register file instead.
21 22
•
N
For simplicity we’ll assume the Z
F
RAM is at least as fast as the n
CPU clock. (This is definitely D0
n
not the case in real processors Q D1
S MD
these days.)
23 24
Example sequence of operations R3 M[R0]
• Here is a simple series of register transfer instructions: • AA should be set to 00, to read n
register R0.
R3 M[R0] • The value in R0 will be sent to 1 D data
R3 R3 + 1 the RAM address input, so
WR Write
DA D address
M[R0] R3 M[R0] appears as the RAM 11 Register File
output OUT.
• This just increments the contents at address R0 in RAM.
• MD must be 1, so the RAM
AA
00
A address B address BA
– Again, our ALU only operates on registers, so the RAM contents output goes to the register file.
A data
n
B data
RAM
ADRS
must first be loaded into a register, and then saved back to RAM.
•
n
To store something into R3, DATA OUT
– R0 is the first register in our register file. We’ll assume it contains we’ll need to set DA = 11 and A B
+5V
MW
CS
WR
FS FS 0
a valid memory address. WR = 1.
•
V
•
ALU
How would these instructions execute in our datapath? MW should be 0, so nothing is C
N
Z
accidentally changed in RAM. F
• Here, we did not use the ALU n
(FS) or the second register file D0
n
Q D1
output (BA). S MD
1
25 26
R3 R3 + 1 M[R0] R3
• AA = 11, so R3 is read from the n • Finally, we want to store the n
register file and sent to the contents of R3 into RAM
ALU’s A input. 1 D data address R0. 0 D data
• •
WR Write WR Write
FS needs to be 00001 for the DA D address Remember the RAM address DA D address
operation A + 1. Then, R3 + 1 11 Register File comes from “A data,” and the Register File
appears as the ALU output F. AA A address B address BA
contents come from “B data.” AA A address B address BA
• If MD is set to 0, this output 11
A data B data
• So we have to set AA = 00 and 00
A data B data
11
RAM RAM
will go back to the register file. n
ADRS
BA = 11. This sends R0 to n
ADRS
•
n n
To write to R3, we need to DATA OUT ADRS, and R3 to DATA. DATA OUT
•
+5V CS +5V CS
make DA = 11 and WR = 1. 00001 A B MW WR MW must be 1 to write to A B MW WR
FS FS 0 FS FS 1
• Again, MW should be 0 so the V memory. V
•
ALU ALU
RAM isn’t inadvertently C
N No register updates are C
N
Z Z
changed. F needed, so WR should be 0, and F
• We didn’t use BA. n MD and DA are unused. n
D0
n
• We also didn’t use the ALU, so D0
n
Q D1 Q D1
S MD FS was ignored. S MD
0
27 28
Constant in Control units
• One last refinement is the • From these examples, you can see that different actions are
addition of a Constant input. WR Write
D data performed when we provide different inputs for the datapath
• The modified datapath is DA D address control signals.
shown on the right, with one Register File • The second question we had was “Who exactly decides which
extra control signal MB. AA A address B address BA registers are read and written and which ALU function is
• We’ll see how this is used A data B data
executed?”
later. Intuitively, it provides Constant
– In real computers, the datapath actions are determined by the
an easy way to initialize a MB program that’s loaded and running.
register or memory location S D1 D0 – A control unit is responsible for generating the correct control
with some arbitrary number. Q RAM
signals for a datapath, based on the program code.
ADRS
+5V
DATA OUT
CS
• We’ll talk about programs and control units later.
A B MW WR
FS FS
V
ALU
C
N
Z
F
D0
Q D1
S MD
29 30
Summary HW
• The datapath is the part of a processor where computation is done. 1. Design a 4 bit arithmetic circuit, with two selection variables S1
– The basic components are an ALU, a register file and some RAM. and S0, that generates the arithmetic operations below. Draw
– The ALU does all of the computations, while the register file and the logic diagram for a single bit stage. (Q 10-4)
RAM provide storage for the ALU’s operands and results. 2. A computer has a 32 bit instruction word broken into fields as
• Various control signals in the datapath govern its behavior. follows: opcode, 6 bits; two register fields, 6 bits each; and one
• Next, we’ll see how programmers can give commands to the processor, immediate operand/register field, 14 bits.
and how those commands are translated in control signals for the • (a) What is the maximum number of operations that can be
datapath. specified?
• (b) How many registers can be addressed?
• (c) What is the range of unsigned immediate operands that can
be provided? (Q 10-13) S1S0 Cin = 0 Cin = 1
00 F = A + B (add) F=A+B+1
01 F = A (transfer) F=A+1
(increment)
10 F = B’ F = B’ + 1
(complement) (negate)
11 F = A + B’ F = A + B’ + 1
31 32 (subtract)
Control units Datapath review
• Set WR = 1 to write one of WR D
• We introduced the basic structure of a control unit, and translated the registers. DA Register file
• The last piece of the processor is a control unit to convert these binary • AA and BA select the source constant
instructions into datapath signals. registers. 1 0
MB
• At the end we’ll have a complete example processor! • MB chooses a register or a Mux B
constant operand.
• FS selects an ALU operation.
• MW = 1 to write to memory. FS A B ADRS DATA
0 1
MD
Mux D
33 34
• The datapath also sends information back to the control unit. For
instance, the ALU status bits V, C, N, Z can be inspected by branch • Caches in modern CPUs often feature a Harvard architecture like this.
instructions to alter a program’s control flow. • However, there is usually a single main memory that holds both program
instructions and data, in a Von Neumann architecture.
35 36
Program counter Instruction decoder
• A program counter or PC addresses the instruction memory, to keep • The instruction decoder is a combinational Data
track of the instruction currently being executed. circuit that takes a machine language
• On each clock cycle, the counter does one of two things. instruction and produces the matching Load PC
– If Load = 0, the PC increments, so the next instruction in memory control signals for the datapath.
will be executed. • These signals tell the datapath which ADRS
– If Load = 1, the PC is updated with Data, which represents some registers or memory locations to access, Instruction
RAM
address specified in a jump or branch instruction. and what ALU operations to perform.
OUT
Data
Instruction Decoder
Load PC
DA AA BA MB FS MD WR MW
ADRS
Instruction
RAM (to the datapath)
OUT
37 38
39 40
The whole processor Instruction format
Control Unit Datapath • We have three different instruction formats, each 16 bits long with a
seven-bit opcode and nine bits for source registers or constants.
V
WR D • The first three bits of the opcode determine the instruction category,
DA Register file
C Branch
PC while the other four bits indicate the exact instruction.
– For ALU/shift instructions, the four bits choose an ALU operation.
N Control AA A B BA
Z
ADRS
constant – For branches, the bits select one of eight branch conditions.
Instruction
RAM
1 0
Mux B
MB – We only support one load, one store, and one jump instruction.
OUT
15 9 8 6 5 3 2 0
Register B (SB),
Destination (DR) or
FS A B ADRS DATA Opcode Register A (SA) Operand (OP), or
Instruction Decoder Address 5-3 (AD)
V ALU MW Data RAM Address 2-0 (AD)
C
N
Z G OUT
DA AA BA MB FS MD WR MW
0 1
MD
Mux D
41 42
(b) Immediate • Our binary representation for these instructions will include:
15 9 8 6 5 3 2 0 – A 7-bit opcode field, specifying the operation (e.g., ADD).
Opcode
Address (AD)
(Left)
Source reg-
ister A (SA)
Address (AD)
(Right)
– A 3-bit destination register, DR.
– Two 3-bit source registers, SA and SB.
(c) Jump and Branch
• The three formats are: Register, Immediate, and Jump and Branch
• All formats contain an Opcode field in bits 9 through 15.
• The Opcode specifies the operation to be performed
43 44
Immediate format Jump and branch format
15 9 8 6 5 3 2 0 15 9 8 6 5 3 2 0
Destination Source Address Source Address
Operand
Opcode Register Register A Opcode Bits 5-3 Register A Bits 2-0
(OP)
(DR) (SA) (AD) (SA) (AD)
45 46
– Immediate format instructions have one source register and one Move A R[SA ]
0000000 MOVA RD ,RA R [DR] N, Z
constant operand. R [SA]
Increment 0000001 INC R D,RA R[DR] +1 N, Z
– Jump and branch format instructions need one source register and Add 0000010 ADD R D,RA,RB R [DR] R[SA ] + R[ SB] N, Z
one constant address. Subtract 0000101 SUB R D,RA,RB R [DR] R[SA ] - R [SB] N, Z
• Even though there are three different instruction formats, it is best to D ecrement 0000110 DEC R D,RA R[DR] R[SA ] -1 N, Z
make their binary representations as similar as possible. AND 0001000 AND R D,RA,RB R [DR] R[SA ] R[SB ] N, Z
– This will make the control unit hardware simpler. OR 0001001 OR RD,RA,RB R[DR] R[SA] R[SB] N, Z
RD,RA,RB R[DR] R[SA] R[SB]
– For simplicity, all of our instructions are 16 bits long.
Exclusive OR 0001010 XOR N, Z
NO T 0001011 NO T R D,RA R[DR] R[SA ] N, Z
47 48
Summary
• We saw an outline of the control unit hardware.
– The program counter points into a special instruction memory, which
contains a machine language program.
– An instruction decoder looks at each instruction and generates the
correct control signals for the datapath and a branching unit.
– The branch control unit handles instruction sequencing.
• The control unit implementation depends on both the instruction set
architecture and the datapath.
– Careful selection of opcodes and instruction formats can make the
control unit simpler.
• We now have a whole processor! This is the culmination of everything
we did this semester, starting from primitive gates.
49