\chapter{Hardware} \begin{myquote} Hardware is the wiring and other physical components of a computer. \end{myquote} Modern computers have several typical components. There is a motherboard which has wiring printed on it in order to connect all of the other components. The central processing unit (CPU) is the main component that performs the instructions of computer programs. Graphics and sound cards provide the user with graphics on screen and sound through headphones or speakers. Input/output ports (I/O ports) transfer data between input devices such as a mouse or keyboard and output devices such as a printer or speakers. A popular I/O connector is the Universal Serial Bus (USB). A bus is a collection of parallel wires that transmit data as electrical signals. \section{von Neumann architecture} \footnote{Prof David Murray, University of Oxford, `A3 Computer Architecture', \url{https://www.robots.ox.ac.uk/~dwm/Courses/3CO_2000/3CO-L2.pdf}} In the 1940s von Neumann's (pronounced Noy-Man, not New-Man) created a design for general purpose computer, reliant on a CPU alongside \textbf{a single memory unit for both instructions and data}. Instructions are executed in series in the `fetch-decode-execute' cycle. It is very successful due to its need for only one memory unit and the ability to have general purpose rather than specific purpose hardware, which early computers were and had to be rewired to change. The design however does lead to the \textbf{`von Neumann bottleneck'} as data and instructions must be fetched from the same memory unit over the same bus. \section{Boolean logic} George Boole was able to devise a mathematical representation of arithmetic using only 1 (True) and 0 (False). Computers use circuits that can perform this logic by using on and off circuitry to represent 1s and 0s. We can represent these circuits as `gates' and describe the logic in truth tables to show all possibilities. Typically we consider: \begin{itemize} \item `And' gates, where the result is true if and only if both of the two inputs are true. \item 'Or' gates, where the result is true if either of the two inputs are true. \item 'Not' gates where the result is the inverse of the one input, so a 1 becomes a 0 and a 0 becomes a 1. \end{itemize} \section{Central processing unit and performance} The central processing unit (CPU) performs the instructions that we ask of a computer. It controls data and instructions through the \textit{fetch-decode-execute} cycle, where an instruction is fetched from main memory, decoded by the CPU so that it can perform the correct logic (such as adding two numbers), and then the instruction is executed giving the output of the instruction (say the result of the addition). There are some critical components of the CPU that we typically consider: \begin{itemize} \item The \textbf{control unit} `oversees' the operations of the CPU and organises the operations performed and the movement of data and instructions. \item The \textbf{arithmetic and logic unit} (ALU) carries out the boolean logic and mathematics via circuits that are designed to do so. \item \textit{Registers} are high speed, small memory locations which hold important data. \begin{itemize} \item The \textit{program counter} is a specific register which stores the location in memory of the next instruction to be executed. \end{itemize} \end{itemize} The CPU executes instructions at a rate dictated by the system \textit{clock}. The \textit{clock speed} determines how many \textit{clock cycles} (fetch-decode-execute cycles) occur per second. This is measured in Hertz (Hz), which means times per second. A CPU in a computer today cycles with a clock speed around 4 GHz or around 4 billion clock cycles per second \footnote{7-Zip LZMA Benchmark, Intel Ice Lake: \url{https://www.7-cpu.com/cpu/Ice_Lake.html}}. The performance of the CPU can be bettered by: \begin{itemize} \item Increasing the clock speed, so that more clock cycles occur per second (overclocking). This does, however, increase power consumption and CPU heat production. \item Increasing the number of CPU cores. A \textit{multicore} CPU has the ability to do multiple cycles at the same time, depending on the number of cores and, for programs that have the functionality, can perform parallel processing where a large algorithm or calculation is distributed between cores, thus improving performance. \item Increasing cache size, to allow faster access to data and instructions. \item Increasing cache/memory speed, to allow faster access to data and \end{itemize} \section{Memory} Volatile memory is erased when electrical power to the system is turned off or interrupted, meaning that all data is lost. Random access memory (RAM) is reasonably fast and is volatile. RAM holds current application and operating system data, as well as instructions for programs that need to be executed. Read only memory (ROM) is not volatile and cannot be changed by the system, as the name suggests. Modern computers use ROM to contain the Basic Input/Output System (BIOS) and other instructions that are required in the \textit{booting process} of a computer. This performs tasks such as initialising hardware and initialising the operating system. Virtual memory might be necessary when there is insufficient RAM for data and instructions. This stores data that would be stored in RAM in secondary storage. \textit{Paging} is the process of swapping chunks of data between RAM and storage. Because secondary storage is significantly slower than RAM and due to the paging process, when virtual memory is necessary, the computer is significantly slowed down. \section{Cache} Cache is memory that is much smaller and more expensive than RAM, but is significantly faster also. Cache is used to store current instructions and data in order to increase CPU performance. There are three levels of cache, each with varying size and speed \footnote{7-Zip LZMA Benchmark, Apple M1: \url{https://www.7-cpu.com/cpu/Apple_M1.html}; Intel Ice Lake: \url{https://www.7-cpu.com/cpu/Ice_Lake.html}}: \begin{itemize} \item Level 1 (L1) cache is very fast and small. It is located directly on CPU cores. It is in the range of 64kB. \item Level 2 (L2) cache is larger and slower than L1. It is shared between CPU cores but is still very close to them, It is in the range of 512kB. \item Level 3 (L3) cache is significantly slower and larger than L2, but is still much faster than normal RAM. It is in the range of 8MB. \end{itemize} \section{Storage media} --intro \subsection{Magnetic storage} Magnetic storage relies on rotating \textit{platters} coated with magnetic material. A \textit{read/write head} can move across the platter. To write data the head contains electromagnets that can give the magnetic particles a positive or negative charge, representing one or zero in binary. A platter is divided into \textit{tracks} which are concentric circles, and segments which are radial divisions. A single segment on a single track is called a \textit{block}. Data is generally organised by blocks. Magnetic storage is high capacity with the highest available capacity in 2021 being 20TB \footnote{\url{https://www.tomshardware.com/news/seagate-ships-hamr-hdds-increases-dual-actuator-shipments}}. Magnetic storage is reasonably cheap and fast. However, the read/write head of a modern hard drive can be only 5 nm from the platter, which can spin at up to 15,000 rpm \footnote{\url{https://en.wikipedia.org/wiki/Hard_disk_drive}; \url{https://en.wikipedia.org/wiki/Flying_height}}. This can create a lot of noise, power. The moving parts involved can fail and the read/write head can \textit{crash} into the platter causing significant data loss, particularly in the case the drive is impacted when running, such as if a laptop is dropped. \subsection{Solid state storage} \textit{Flash} memory chips store data with transistors that can reain their one or zero sate without power (non-volatile). As there are no moving parts, they are durable, silent, and portable. They are, however, very expensive per unit data and risk corruption of data in a power interruption event. \subsection{Optical storage} Optical media (such as a DVD) contains an inwardly spiralling track with pits and lands (small changes in height) to store data. A LASER is aimed at the media, and the reflection is captured by a sensor. The change from a pit to land can represent a zero or a one. This relies on the laser to be moved very precisely by motors to align with the track, and for the disc to be rotated at a precise speed (much like magnetic storage) in order to read data at the correct rate. Optical storage is very portable and can be read by many devices, but requires that they have a compatible drive. However, large quantities of data are expensive to store and discs can very easily be scratched or damaged, although error correcting methods are generally used \footnote{3Blue1Brown, `How to send a self-correcting message': \url{https://www.youtube.com/watch?v=X8jsijhllIA}}. Reading data from an optical disc is slower than solid state or magnetic storage. \subsection{Cloud storage} Businesses and other organisations (such as schools) traditionally use servers for data storage of all data and for running services such as email and websites. This however, depends on skilled IT staff and expensive equipment and maintenance. Cloud data storage involves the use of offsite servers around the world, provided by large organisations providing storage for a monthly cost. This can be cheaper than an in house operation and provides the benefit of the server model to individuals such as the ability to access data from many devices and reliable backups. However, a cloud storage solution requires a reliable internet connection to access data, can become expensive for large amounts of data and due to the repeat cost, and raises philosophical concerns such as those surrounding the ownership of data. \section{Embedded systems} An embedded system \footnote{\url{https://en.wikipedia.org/wiki/Embedded_system}} is \textbf{custom built hardware} designed to perform a \textbf{specific task as part of a wider system}, as opposed to a general purpose computer. Examples of embedded systems are visible all around us, such as in common electricals appliances such as printers, televisions, and cameras; in household appliances such as dishwashers and microwaves; and in advanced situations such as engine management in cars, guidance systems in aeroplanes, and medical equipment such as an MRI scanner. Embedded systems generally have no RAM and only have ROM which contains the \textit{firmware} for the specific program the system needs. They have \textbf{no operating system}. The have \textit{sensors} to gather informations about their surroundings (such as a thermometer or camera) and \textit{actuators} to act on or change their surroundings (such as the shutter in a camera or the motor in a washing machine).