Docsity

Memory Organization, Addressing Modes, and Hardware Support for Higher Level Languages, Study notes of Logic

Toggle sidebar
bg1
Lecture 3
Control Unit, ALU, and Memory
In this lecture we develop models for the control unit, ALU and memory. It is the first
that will require most care.
3.1 The control unit
The CU will be a D-type flip-flop “one-hot” sequencer, of the sort illustrated in Lecture
1. Any sequencer, for example, one based on a PROM, would do: the advantage of
the one-hot method is that it makes very clearly what is going on.
The controller must first run the instruction fetch and appropriate decoding sequences
before executing the sequence for the particular opcode fetched into IR (opcode).
We already understand that the outputs from the CU must be levels (CSLs) to establish
data pathways between the various registers and memory, and pulses (CSPs) to fire
register transfers. In addition, the levels are also used to to set the modes of operation
for the ALU, PC and SP.
3.1.1 What levels and pulses are required?
To progress we need to look at the wiring in the CPU at a greater level of detail, as
given in Fig. 3.1.
Output Enable(s): We know that every register has an Output Enable (OE) input which
determines whether the tri-states on its output lines have a high or low resistance.
These are driven by levels such as OEac, OEpc and so on. We assume that OEac=1,
and similar, sets the tri-state for the AC in low resistance mode.
Next, as a more careful study of the CPU diagram Fig.3.1 shows, some extra tri-state
buffers are required to satisfy every register input that has more than one potential
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Memory Organization, Addressing Modes, and Hardware Support for Higher Level Languages and more Study notes Logic in PDF only on Docsity!

Lecture 3

Control Unit, ALU, and Memory

In this lecture we develop models for the control unit, ALU and memory. It is the first that will require most care.

3.1 The control unit

The CU will be a D-type flip-flop “one-hot” sequencer, of the sort illustrated in Lecture

  1. Any sequencer, for example, one based on a PROM, would do: the advantage of the one-hot method is that it makes very clearly what is going on.

The controller must first run the instruction fetch and appropriate decoding sequences before executing the sequence for the particular opcode fetched into IR (opcode).

We already understand that the outputs from the CU must be levels (CSLs) to establish data pathways between the various registers and memory, and pulses (CSPs) to fire register transfers. In addition, the levels are also used to to set the modes of operation for the ALU, PC and SP.

3.1.1 What levels and pulses are required?

To progress we need to look at the wiring in the CPU at a greater level of detail, as given in Fig. 3.1.

Output Enable(s): We know that every register has an Output Enable (OE) input which determines whether the tri-states on its output lines have a high or low resistance. These are driven by levels such as OEac, OEpc and so on. We assume that OEac=1, and similar, sets the tri-state for the AC in low resistance mode.

Next, as a more careful study of the CPU diagram Fig. 3.1 shows, some extra tri-state buffers are required to satisfy every register input that has more than one potential

1

3/2 LECTURE 3. CONTROL UNIT, ALU, AND MEMORY

path into it. These are labelled OE1 to OE7.

OEpc OEsp

OEac

OE

OE

OEad

OEop

OE

OEmbr

OEmar

OE4 OE

SETalu

OEmem

SETshft

OE6 OE

CLKmem WRITE/READ

MAR

PC SP

AC

PC

MBR

IR(opcode) IR(address)

Status

IR

CU

Control Lines

ALU

Memory

INCpc/LOADpc

Figure 3.1: Tri-state Output Enables. 8 extra buffers are required.

ALU: We develop internal hardware for the ALU in §3.2. For now, we treat it as a black box with 8 or fewer functions, which thus require 3 level bits SETalu[2:0] to define. These are specified in the following table.

SETalu Operation Comment 000 ALUnoop Do nothing. Let the AC input appear on output 001 ALUnot Invert each bit in the AC input 010 ALUor Output = AC .OR. MBR 011 ALUand Output = AC .AND. MBR 100 ALUadd Output = AC + MBR ... ...

PC: The Program Counter has a one-bit level input which tells it whether to load the input or to increment when the clock pulse is received.

SP: The Stack Pointer has a two-bit level input which tells it whether to load the input, increment, or decrement, when the clock pulse is received.

3/4 LECTURE 3. CONTROL UNIT, ALU, AND MEMORY

LDA

STA

ADD

AND

CSL1 CSP

CLK

CLK

8−to−

D 1

Q D 2

Q D 3

Q D 4

Q

IR(opcode)

D 10

Q (^) D 11

Q (^) D 12

Q

D 13

Q D 14

Q

D 15

Q (^) D 16

Q (^) D 17

Q

D 18

Q

Decoder

Figure 3.2: The control unit for the first four opcodes.

3.1.3 Hardware for Decoding

You will have spotted in Fig. 3.2 that to decode the opcode we use (what genius!) a 8-to-256 decoder. The example in Figure 3.3(a) considers just the low 3 bits of the opcode. If we ignore the long opcode problem:

Decoding (this is RTL)

  1. →(LDA,STA,ADD,AND, ..., SHR,HALT)/(10,13,15,18,...,25,99)

8−to−

ANDADD

STALDA

HALT

Bit2 Bit1 Bit0 IR(opcode)

and so on

IR(opcode)

Decoder

ADD

STA

LDA

HALT

Figure 3.3: Decoding the opcode. (a) With gates for using bits, and (b) as a black-box.

3.1. THE CONTROL UNIT 3/

3.1.4 Hooking up the CSPs and CSLs

What we have to do now is to figure out is to what to connect the various CSPs and CSLs. Don’t panic — this is quite straightforward ... For the first step of the Fetch cycle we need to Output Enable the PC and clock the MAR– so a 1 goes in these columns. All blank spaces are zeros, and X denotes “don’t care”. Then carry on filling in the 1’s for the remaining lines of Fetch and for the lines of the execute phases of the instructions — that is, filling in horizontally.

Levels Pulses

What LineLOADpcLOADspINCspOEpcOEspOEadOEopOEmarOEmbrOEacOE1OE2OE3OE4OE5OE6OE7SETalu[2]SETalu[1]SETalu[0]OEmemCLKpcCLKspCLKmarCLKmbrCLKirCLKacCLKmem Ftch 1. 1 X X X 1

  1. 1 X X X 1 1
  2. 1 X X X 1 1 Dcd 4. 1 X X X LDA 10. 1 1 X X X 1
  3. 1 X X X 1 1
  4. 1 1 X X X 1 STA 13. 1 1 1 1 0 0 0 1 1
  5. 1 1 1 1 ADD 15.

AND 18.

JMP 21. BZ 22.

NOT 24. SHR 25. HLT 99.

Now look down the columns for each signal. As a work in progress, after filling in the Fetch, Decode, and the execute phases of LDA and STA, we have already OEpc = CSL OEad = CSL10 .OR. CSL OEmar = CSL2 .OR. CSL11 .OR. CSL CLKmar = CSP1 .OR. CSP10 .OR. CSP

3.2. THE ARITHMETIC LOGIC UNIT 3/

carry is slow, and that speed-up can be achieved by inserting carry-look-ahead circuitry between a number of bits.

The ALU contains a none/left/right shifter at its output. This is operated separately from the other functions using a 2-bit setSHFT input (so one can add two numbers and rightshift them all in one pass). Some designs use a shift register, but here we regard it a black box which transparently allows bit[n] of the input to appear as one of bit[n], bit[n-1], or bit[n+1] of the output.

Slice Slice Slice Slice Slice

setSHFT

setALU

2

BA 15 14 2 1 0 0

Carry

3

16

ZNV generation

C N Z V Output

15 14 2 1 0

Shifter

BA BA BA BA

O O O O O

Figure 3.5: Multi-bit ALU. The carry-in on the right must be 0. A shifter is placed on the back end. In our design it is not a clocked register.

3.2.2 Flags set in the Status Register by the ALU

An important function of the ALU is to set up bits or flags which give information to the control unit about the result of an operation. The flags are grouped together in the status word.

As the ALU has only an adder, to subtract numbers one has to use 2s-complement arithmetic. The ALU has no knowledge of this at all — it simply adds two binary inputs and sets the flags. It is up to control unit (or really the programmer’s instructions executed by the control unit) to interpret the results.

Z Zero flag: This is set to 1 whenever the output from the ALU is zero.

N Negative flag: This is set to 1 whenever the most significant bit of the output is 1. Note that it is not correct to say that it is set when the output of the ALU is neg-

3/8 LECTURE 3. CONTROL UNIT, ALU, AND MEMORY

ative: the ALU doesn’t know or care whether you are working in 2’s complement. However, this flag is used by the controller for just such interpretations.

C Carry flag: Set to 1 when there is a carry from the adder.

V oVerflow flag: Set to 1 when Amsb = 1, Bmsb = 1, but Omsb = 0; or when Amsb = 0, Bmsb = 0, but Omsb = 1. Allows the controller to detect overflow during 2’s complement addition. Note (i) that the ALU does not know why it is setting the flag, and (ii) in our BSA, the msb=15.

3.3 Counter Registers

The PC and SP are counting registers, which either act as loadable registers (loaded from the IR (address) register) or count, both on receipt of a clock pulse. The internal workings appear complicated, but are unremarkable, so we leave them as black boxes here.

3/10 LECTURE 3. CONTROL UNIT, ALU, AND MEMORY

The OE input is expected, but instead of a CLKmem input we now use the more usual name WRITE/READ. (By the way, this notation means WRITE is the “equivalent” of READ.)

At first this looks like a level selector signal but, by process of elimination, this must be the clocked input.

3.4.1 When reading:

WRITE=0, and the output of (CS.AND.WRITE)=0 so that all register CLK inputs are low (which is good). Now, CS=1 and OE=1, so that the 3-input AND gate enables the tri-state outputs. The actual register outputting is determined by the Address decoder’s output. Looking at the outputs from the latches, the enabled outputs are ORed with all the disabled outputs, and the three outputs head off towards the data bus. Notice that the outputs are Output-enabled using tri-state logic onto the bus.

The timing is shown in Fig. 3.7.

0

1

0

1

0

1

0

1

0

1

W/R

Address

Data

CS

OE

Address Valid

Read cycle time

Data Valid

Read access time Data hold time

Bus floating

Bus no longer floats

3−state output−enabled

Figure 3.7: Timing of signals for (a) memory read.

3.5. MEMORY ORGANIZATION IN HARDWARE 3/

3.4.2 When writing:

CS=1, OE=0, and WRITE changes from 0→1, so that the CLK inputs on the register selected by the address are all high. Then WRITE changes from 1→0 causing the clocks to fall triggering the register transfer.

The timing is shown in Fig. 3.8.

W/R

Address Valid

Write cycle time Address

Data Valid

Register Transfer here

Data

Data set−up time Data hold time

CS

OE

Figure 3.8: Time of signals for the memory write. Notice that the signal which clocks the register on the memory during write is the WRITE/READ signal.

3.5 Memory organization in hardware

The size of memory chips has risen over the years. Though technically feasible to build very large single chip memories, potential sales and problems of yield make it much more economical to produce “reasonably sized” chips that find application in small memories, but can be built up into larger memories.

Data Width: Standard memory chips are 1Byte wide, so our 16bit data bus requires two chips side by side. The same address lines enter both chips, but the data lines are split between the high 8 bits and low 8 bits. The arrangement is shown in Fig. 3.9.

Address Height: n address lines can access 2 n^ locations — in the case of 16 lines that is 16 M locations. Now suppose the available memory chips were 8 MByte. We need

3.6. MEMORY ORGANIZATION IN SOFTWARE: MEMORY ADDRESSING MODES 3/

3.5.1 Memory: address space versus physical memory

The n address lines give the ability to address 2 n^ different locations. These locations span the address space 0x0 to 0xFFFFFF for our 24-bit address bus. However, there is no need either for (i) for the entire address space to be occupied by physical memory, or (ii) for the physical memory that is fitted to be located contiguously in address space. There can be gaps.

Exactly how the physical memory is mapped onto the memory space depends on how the address lines are decoded.

As an example, suppose we have 13 address lines A0–A12. These can address 8K (ie

  1. locations in memory. Suppose also that we have just two 1K (ie 1024) word memory chips M1 and M2. Each must use the lowest ten address lines A0–A9.

If all the lines were decoded (Fig. 3.11) the mapping between address and location is unique. The valid and invalid address ranges are shown in the table.

Address

Data

3−to− line decoder

1Kx16bit CS

1Kx16bit CS

A

A10 A0−

A

Into Dec Into Mem Hex A12A11A10 A9A8A7A6A5A4A3A2A1A0 Addr 1 1 1 1 1 1 1 1 1 1 1 1 1 0x1FFF No physical memory 0 1 1 0 0 0 0 0 0 0 0 0 0 0x0C 0 1 0 1 1 1 1 1 1 1 1 1 1 0x0BFF 0 1 0 0 0 0 0 0 0 0 0 0 0 0x 0 0 1 1 1 1 1 1 1 1 1 1 1 0x07FF No physical memory 0 0 1 0 0 0 0 0 0 0 0 0 0 0x 0 0 0 1 1 1 1 1 1 1 1 1 1 0x03FF 0 0 0 0 0 0 0 0 0 0 0 0 0 0x

Figure 3.11: Full address decoding, but using only 2 × 1K memories in an 8K address space.

3.6 Memory organization in software: memory addressing modes

Our knowledge of memory hardware tells us that a memory works in just one way — you stick the address on the address lines and then either read or write to the contents at that address. The different modes of memory addressing refer then not to the hardware level, but to different ways of using what you read from the memory. We shall consider

3/14 LECTURE 3. CONTROL UNIT, ALU, AND MEMORY

(1) Immediate, (2) Direct, (3) Indirect, and (4) Indexed addressing.

3.6.1 Immediate addressing

Immediate addressing does not involve further memory addressing after the instruction fetch. It provides a method of specifying a constant number rather than an address in the operand. For example LDA# x loads the accumulator immediately with the operand x.

LDA# x AC←IR (address)

ADD# 22

AC←AC + 22

CPU

Outside the CPU

SETalu

Address Bus

Data Bus

CLKmem

SP

MAR

AC

IR(opcode) IR(address)

Status

IR MBR

CU ALU

Memory

Control Lines

INCpc/LOADpc^ PC

to Registers, ALU, Memory, etc

Looking back at our Standard architecture, you will see that there is a direct link from the IR (address) to the AC to allow this to happen.

Immediate addressing allows statements like “n=n+10” written in some high level lan- guage to be turned into assembler, using modified versions of other instructions. For example ADD# 22. How would you actually realize this on our machine?

3.6.2 Direct addressing

We have already use direct addressing in the lectures. This is where the operand is the address of the data you require. Another way of saying this is that the operand is a pointer to the data.

LDA x MAR←IR (address) MBR←〈MAR 〉 AC←MBR

ADD x MAR←IR (address) MBR←〈MAR 〉 AC←MBR + AC

etc

♣ Quick Example: What does location 23 and the AC contain after this code snippet? Code AC Loc22 Loc LDA # STA 22 ADD # ADD 22 STA 23

3/16 LECTURE 3. CONTROL UNIT, ALU, AND MEMORY

LDX #0 // zero the index register Loop : LDA 100 , X // load AC with Xth of array ADD 200 , X // add the Xth of another array STA 300 , X // store as Xth element on a third array INX // increment X JMP Loop // do it again

What needs fixing?

3.6.5 Addressing Examples

The example shown in Figure 3.12 shows LDA (2), compared with LDA 2, LDA # and LDA 2,X. The contents at address 2 are 47, so that we look in 47 for the data, 38, which is then loaded into the accumulator.

AC 2

38

6 5 4 3 2 1 0

47

47 47

38

47 Immediate Direct

LDA #2 LDA 2 38

47

38

Indirect

LDA (2)

47

38

52 52 52 52

Indexed

LDA 2,X 52

4 Register X

Figure 3.12: Addressing modes: the contents of the AC after LDA immediate, direct, indirect and indexed

3.7. A SMALL PROGRAM 3/

3.7 A small program

It is worth pointing out that you now know enough to build (and program) a simple computer by looking at a small program written in assembler and seeing how it appears in the memory. LDA 20 // LOAD AC with contents at location 20 AGAIN : SUB 22 // SUBTRACT from AC contents of location 22 BZ STOP // If ALU gives 0 , Z =1 , so jump to label STOP LDA 20 // LOAD AC with contents of location 20 ADD 21 // ADD contents of location 21 STA 20 // STORE in location 20 JMP AGAIN // JUMP back to label AGAIN STOP : HALT

We have introduced another assembler mnemonic SUB — let it have opcode 9, ie %00001001. Also we will use just 8-bit operands, so that the high 16-bits of the address are all zero.

Let us also assume that the first instruction gets stored at location 0 in memory. (This is not a general requirement. All that is required is that the PC is initialized at the correct starting location.)

The location and contents of the memory relating to the program are as follows. Notice how the line labels are assembled as memory locations. This is because they are going to supply information to the PC during the JMP and BZ instructions. Data has been inserted at locations 20-22.

Memory contents Instru- Loca- High Byte Low Byte Comment ction tion OPCODE OPERAND LDA 20 0 00000001 00010100 Program starts SUB 22 1 00001001 00010110 BZ 7 2 00000110 00000111 LDA 20 3 00000001 00010100 ADD 21 4 00000011 00010101 STA 20 5 00000010 00010100 JMP 1 6 00000101 00000001 HALT 7 00000000 00000000 Program ends : : : 20 00000000 00000101 These are data: dec 5 21 00000000 00000001 dec 1 22 00000001 00101101 dec 300

3.8. HARDWARE SUPPORT FOR HIGHER LEVEL LANGUAGES (READING) 3/

3.8 Hardware support for higher level languages (Reading)

The emphasis in this short course is on the register-level operation of hardware in CPU and memory. However, using RTL we have been able to make the link between the register-level and what is called the “macro-level” or assembler language level. This has probably happened without your being aware of it. The link was made by writing down LDA x as an overall register transfer AC ←〈x〉, then breaking this down into the individual transfers that are reliaziable on our actual architecture.

Unfortunately, there has been and is no time to make the link between the macro-level and the way all of us programme computers using high-level languages such as C++, C, Java, Fortran, and so on; languages which allow complete abstraction from the details of the cpu that actually executes the program.

This section of the notes describes the process of compilation, which will in turn clarify why Indirect Addressing is useful. It ends with an explanation of how subroutines appear at the macro-level, and how they use the memory stack.

3.8.1 Compilation of programmes

Before you can execute a high-level program on a computer, it has to be compiled into a set of instructions for the particular cpu being used. In their binary opcodes and operand form, the program is said to be in machine code. One level higher than this is assembler code, which uses mnemonics for the opcodes, and allows one to label locations (eg the JMP AGAIN seen earlier: AGAIN was a label for a location). Now, each cpu comes with its own assembler program which turns assembler into machine code (you will use one of these on the lab course), and so there is no need for a compiler to produce machine code: it need only produce assembler.

The aim of the compiler, in the simplest cases, is to produce a program where instruc- tions occupy one section of memory and where allocated data memory lies above the instructions. When you execute the program, you rely on the computer’s operating system to be able to provide extra free space (called the Heap) above the program and allocated data, and to provide a stack. (In early days, when memories were small, the programmer could specify the amount of memory to be given to Heap and Stack, so that the OS knew exactly how much space to allocate to your program. The OS would refuse to run the program if there was insufficient space.)

The compiler works in two stages.

3/20 LECTURE 3. CONTROL UNIT, ALU, AND MEMORY

On the first pass

  • it reads the file of high level statements, checks the syntax, and replaces each statement with the relevant sequence of assembler instructions.
  • it places declared variables, and the names of labels, into a symbol table.
  • each time the variable or label is encountered, it replaces the name in the code by its location in the symbol table.

The instructions are written out into a temporary file. Since each instruction is of known length, the compiler can associate with it provisional memory addresses, relative to the Beginning of the Program (BOP). So, after the first pass, the compiler knows the full extent of the program — or, more strictly, the extent of the instructions. After the first pass, the compiler therefor knows where it can start placing allocated data. We will call this memory location BOD (for Beginning of Data).

On the second pass the compiler

  • replaces locations in the symbol table by proper locations in memory, relative to BOP, the Beginning of Program.

For example, consider the fragment

int a ,b , c ; // declaration of variables and their lengths a =1; b =2; c = a + b ; if ( c !=0) { a =3; } b =4;

After reading the declaration of variables, the compiler could start making the symbol table, where BOD indicates the unknown Beginning Of Data.

Name SymTab Actual Location Words a var0 BOD 1 b var1 BOD+1 1 c var2 BOD+2 1

Let the beginning of the program be located at BOP, and let us count in words. The compiler would start making its temporary file: