# IB/Group 4/Computer Science/Computer Organisation/The Instruction Cycle Layer
## What is a CPU ?

The CPU is the element in a computer that is responsible for processing and executing operations and instructions given by applications. This is done through registers, RAM, and ALUs. CPUs function similar to the brain of a computer and are present in every computer. CPUs are made from large amounts of transistors placed on chips that collectively perform operations to process instructions.
The rate at which CPUs can process information is known as their clock speed, which is measured in Hertz. Modern CPUs function at billions of Hertz meaning that they can run billions of operations per second.
CPUs can also have multiple "cores" which is the main element of the CPU. The number of cores that a CPU has, along with its clock speed, determines its operating speed. There is a wide range of core amounts that CPUs can have, with some CPUs having one core, and some having up to 256.

## What is the fetch decode execute cycle ?

Before looking at how a computer does what it does, let us look at what it can do. The definition of a computer outlines its capabilities; a computer is an electronic device that can store, retrieve, and process data. Therefore, all of the instructions that we give to the computer relate to storing, retrieving, and processing data.
The underlying principle of the von Neumann machine is that data and instructions are stored in memory and treated alike. This means that instructions and data are both addressable. Instructions are stored in contiguous memory locations; data to be manipulated are stored together in a different part of memory. To start the Fetch-Execute Cycle, first of all the data is loaded to the main memory by the operating system, the address of the first instruction is placed onto the program counter.
The process cycle includes four steps:
* Fetch the next instruction,
* Decode the instruction
* Get data if needed,
* Execute the instruction.
* Store result of execution and check for next instruction.

#### Fetch the Next Instruction

The PC increments one by one to point to the next instruction to be executed, so the control unit goes to the address in the memory address register (MAR) which holds the address of the next instruction specified in the PC. Then it takes it to the main memory through the address bus and returns it to the memory buffer register via the data bus. The MBR is a two way register that temporarily holds data fetched from the Memory(cache or RAM), makes a copy of the contents, and places the copy in the IR. Now, the IR contains the instruction to be executed.
Before going to the next step in the cycle, the PC must be updated to hold the address of the next instruction to be executed when the current instruction has been completed. There after the instruction register is responsible for the instruction to be solved by the CU. The CU checks on the status of the instruction and then allows execution. Because the instructions are stored contiguously in memory, adding the number of bytes in the current instruction to the PC should put the address of the next instruction into the PC. Thus the control unit increments the PC. (It is possible that the PC may be changed later by the instruction being executed)
In the case of an instruction that must get additional data from memory, the ALU sends an address to the memory bus, and the memory responds by returning the value at that location. In some computers, data retrieved from memory may immediately participate in an arithmetic or logical operation. Other computers simply save the data returned by the memory into a register for processing by a subsequent instruction. At the end of execution, any result from the instruction may be saved either in registers or in memory.

#### Decode the Instruction

To execute the instruction in the instruction register, the control unit has to determine what instruction it is. It might be an instruction to access data from an input device, to send data to an output device, or to perform some operation on a data value. At this phase, the instruction is decoded into control signals. That is, the logic of the circuitry in the CPU determines which operation is to be executed. This step shows why a computer can execute only instructions that are expressed in its own machine language. The instructions themselves are literally built into the circuit.
For example, lets take an imaginary RAM with registers of 8 bits in the table below. In the 8 bits, the first 4 bits are what we use to determine the instruction.
For the 1 address: 01010010
0101 is our opcode. For our case, 0101 corresponds to "load", meaning we are loading the value from the address 00000010 to a register.
Example continued in "Execute the instruction".
Instruction table :
| Instruction | opcode |
| load | 0101 |
RAM :
| Address | Data |
| 00000001 | 01010010 |
| 00000010 | 00000001 |
| 00000011 | 00000000 |

#### Get Data If Needed

In most programs, the instruction to be executed may potentially require additional memory accesses to complete its task.
For example, if the instruction says to add the contents of a memory location to a register, the control unit must get the contents of the memory location.

#### Execute the Instruction

Once an instruction has been decoded and any operands (data) fetched, the control unit is ready to execute the instruction. Execution involves sending signals to the arithmetic/logic unit to carry out the processing. In the case of adding a number to a register, the operand is sent to the ALU and added to the contents of the register.
When the execution is complete, the cycle begins again. If the last instruction was to add a value to the contents of a register, the next instruction probably says to store the results into a place in memory. However, the next instruction might be a control instruction—that is, an instruction that asks a question about the result of the last instruction and perhaps changes the contents of the program counter.
Hardware has changed dramatically in the last half-century, yet the von Neumann model remains the basis of most computers today. As Alan Perlis, a well-known computer scientist once said;
Continuing with our example from above. We left off at where we read 0101's instruction from the instruction table.
After the decoding, we are executing the CPU. In our case, we are storing the value in the address 00000010 (2 in denary) to a register. So now in our register, we would have the value 2 in denary to use for later operations.
* Register
* 2
When this instruction cycle is over, all the circuits will be turned off and a new instruction cycle will begin when the value in the Instruction address register is increased by 1.

## What is a control unit ?

The Control Unit is the organising force in the computer, for it is in charge of the Fetch-Execute Cycle. There are two special registers in the control unit. The Instruction Register (IR) contains the instruction that is being executed, and the Program Counter (PC) contains the address of the next instruction to be executed. Because the ALU and the control unit work so closely together they are often thought of as one unit called the Central Processing Unit (CPU).

## What is Random access Memory (RAM) ?

Random Access Memory (RAM) is a storage device that functions as storage, and can give instructions to the CPU. RAM is used for temporary storage for the CPU, and the data that the RAM holds will only be stored while the program needing it is running, or if the computer is Running. This means that ram is volatile (the data will be erased if it is powered off).
The speed of RAM is defined by the storage amount, clock speed, and latency. The clock speed is how many operations it can handle per second (in hertz), with modern computers being able to handle millions of operations per second. The latency is how long it takes for data to be written to RAM to be available to use. The storage amount is how much data the RAM can hold, with modern RAM sticks being able to hold Gigabytes of data (billions of bytes).
It also functions and read and write. Meaning memory can both be read and accessed.

## What are instructions for the CPU ?

All CPUs come with an instruction set with associates many numbers, called opcodes, with different instructions. Instructions are read by the CPU from the RAM, where numbers are stored which have a certain number of bits reserved for opcodes and the rest dedicated to information on memory location.
Depending on how expansive a CPU's instruction set is, instructions can range from operations such as addition and division, to more complex logic tasks.
For example, the 8-bit instruction 00011100, which has opcode 0001, could mean, in the following case, to add the numbers at the register and at memory location 1100. The number 00110011 (opcode 0011) would then store the result in memory location 0011.
Example instruction set :
| Opcode | Instruction |
| 0000 | End |
| 0001 | Add |
| 0010 | Subtract |
| 0011 | Store |
| 0101 | Load |

## What is the MDR ?

The MDR, or Memory Data Register, temporarily stores data while it is being processed by the CPU, as an intermediary between the RAM and the CPU. It also temporarily holds data that is being placed in memory.
It can also be know as the MBR, or Memory Buffer Register.

## What is the MAR ?

First off, MAR stands for Memory Address Register. It is a subcomponent of the CPU component, registers which holds the Program counter, the Current Instruction Register, Accumulator, the Memory Data Register, and the Memory Address Register.
It is essentially a storage unit that holds the memory address, where something is in the memory of the CPU, of data that needs to be accessed. It also holds the memory address of data that will be sent to that address via system bus(next chapter). The MAR works hand-in-hand with the MDR, Memory Data Register, which holds the actual data which was held at the memory address from the MAR.

## What is a data bus and an address bus ?

In the diagram above all the parts are connected to one another by a collection of wires called a bus, through which data travels in the computer. Each bus carries three kinds of information; address, data, and control:
- An address is used to select the memory location or device to which data will go, or from which it will be taken.
- Data then flows over the bus between CPU, memory, and I/O Devices.
- The control information is used to manage the flow of addresses and data. For example, a control signal will typically be used to determine the direction in which the data is flowing either to or from the CPU.
The Bus Width is the number of bits that it can transfer simultaneously. The wider the bus, the more address or data bits it can move at once.
A widespread example of databus used billions of times every day is the 'Universal Serial Bus' aka USB. This bus, used to transfer data, charge your phone and power your electric pencil sharpener has been the standard on earth since 1996.

## What is cache memory ?

Because memory accesses are very time consuming relative to the speed of the processor, many architectures provide Cache Memory. Cache memory is a small amount of fast-access memory into which copies of frequently used data are stored. Before a main memory access is made, the CPU checks whether the data is stored in the cache memory. Pipelining is another technique used to speed up the Fetch-Execute Cycle. This technique splits an instruction into smaller steps that can be overlapped.
There are 3 types of cache memory. L1 cache is the smallest but the fastest cache memory including data and instructions. L2 cache has bigger memory, but slower and is data only. L3 cache as the biggest memory but the slowest speed and is data only.
In a personal computer, the component in a von Neumann machine reside physically a printed circuit board called the Motherboard. The motherboard also has connections for attaching other devices to the bus, such as a mouse, a keyboard, or additional storage devices.
So what does it mean to say that a machine is an n-bit processor? The variable n usually refers to the number of bits in the CPU general registers: Two n-bit numbers can be added with a single instruction. It also can refer to the width of the bus, which is the size of the addressable memory—but not always. In addition, n can refer to the width of the data bus—but not always.

## What is the Von Neumann Architecture ?

A major defining point in the history of computing was the realisation in 1944–1945 that data and instructions to manipulate data were logically the same and could be stored in the same place. The computer design built upon this principle, which became known as the von Neumann Architecture, is still the basis for computers today. Although the name honours John von Neumann, a brilliant mathematician who worked on the construction of the atomic bomb, the idea probably originated with J. Presper Echkert and John Mauchly, two other early pioneers who worked on the ENIAC at the Moore School at the University of Pennsylvania during the same time period.
Another major characteristic of the von Neumann architecture is that the units that process information are separate from the units that store information. This characteristic leads to the following components of the von Neumann architecture.
* The memory unit that holds both data and instructions.
* The arithmetic/logic gate unit that is capable of performing arithmetic and logic operations on data.
* The input unit that moves data from the outside world into the computer.
* The output unit that moves results from inside the computer to the outside world.
* The control unit that acts as the stage unit to ensure that all the other components act in concert.

## Memory, Process, and CPU Management

Recall that executing a program resides in main memory and its instructions are processed one after another in the fetch-decode-execute cycle. Multiprogramming is the technique of keeping multiple programs in main memory at the same time; these programs compete for access to the CPU so that they can do their work. All modern operating system employ multiprogramming to one degree or another. An operating system must therefore perform memory management to keep track of which programs are in memory and where in memory they reside.
Another key operating system concept is the idea of a process, which can be defined as a program in execution. A program is a static set of instructions. A process is a dynamic entity that represents the program while it is being executed. Through multiprogramming, a computer system might have many active processes at once. The operating system must manage these processes carefully. At any point in time a specific instruction may be the next to be executed. Intermediate values have been calculated. A process might be interrupted during its execution, so the operating system performs process management to carefully track the progress of a process and all of its intermediate states.
Related to the ideas of memory management and process management is the need for CPU scheduling, which determines which process in memory is executed by the CPU at any given point.
One must remember, however, that the OS itself is just a program that must be executed. Operating system processes must be managed and maintained in the main memory along with other system software and application programs. The OS executes on the same CPU as other programs, and it must take its turn among them.
The following sections (Single Contiguous Memory Management, Partition Memory Management, Paged Memory Management) are for interest purposes only. The Computer Science Guide clearly notes under 2.1.6 that "technical details are not needed. For example, memory management should be described but how this is handled in a multitasking environment is not expected." Therefore, these sections serve the purpose of illustrating memory management techniques and can be summarised rather than utilised in an exam.

#### Single Contiguous Memory Management

Has only the operating system and one other program in memory at one time. This is the most basic type of memory management.

#### Partition Memory Management

The memory is broken up into different parts. Has the operating system and any number of programs running at the same time through these different partitions. The partitions have a base and a bound register.
Base register
* A register that holds the beginning address of the current partition (the one that is running)
Bounds register
* A register that holds the length of the current partition
Partition selection First fit
* Allocate program to the first partition big enough to hold it
Best fit
* Allocated program to the smallest partition big enough to hold it
Worst fit
* Allocate program to the largest partition big enough to hold it

#### Paged Memory Management

A technique in which processes are divided into fixed-size pages and stored in memory frames when loaded.
Frame
* A fixed-size portion of main memory that holds a process page
Page
* A fixed-size portion of a process that is stored into a memory frame