Simple Computer - Datapath, ALU and Register File
In questa pagina 3
This note and the next one (Simple Computer - Instruction Set, Single-Cycle and Multi-Cycle ControlThe 16-bit instruction word has an opcode of 7 bits plus register fields (DR, SA, SB or an immediate OP) or a branch format with a 6-bit PC-relative signed offset; the instruction set is load/store with ADD, SUB, logic, shifts, LDI, ADI, LD, ST, BRZ, BRN and JMP. In the single-cycle computer (separate instruction and data memories) a combinational instruction decoder derives MB, MD, RW, MW, PL, JB, BC from bits 15, 14, 13 and 9 and FS from bits 12 to 9, and the clock period is the sum of the delays on the critical path (9.8 ns in the example). In the multi-cycle computer (one memory) an instruction register, internal registers R8 to R15 and a sequential decoder are needed: fetch (INF) plus one or more execution states.Simple Computer - Instruction Set, Single-Cycle and Multi-Cycle Control →) design a simplified but fully functional computer out of blocks already known: registers, adders, multiplexers, decoders, memories and finite state machines. A computer consists of:
- a datapath, able to execute the operations used by algorithms (sums, logic operations, ...);
- a control unit, which drives the datapath according to the "program";
- a memory, holding both program and data.
The starting point is the Instruction Set Architecture (ISA), the interface towards the programmer. The datapath is defined by a set of registers, a list of microoperations and a control interface.
Datapath structure
The datapath is built from basic elements: registers, buses, multiplexers, decoders.
- register file: source and destination registers;
- ALU (combinational arithmetic-logic circuit) and shifter: together they form the function unit;
- multiplexers choosing the operands and the output data;
- status bits: V overflow, C carry, N negative, Z zero.
Every operation takes one clock cycle: for example reads and , computes the sum and writes it into at the next clock edge.
Register file
Definition (register file). A datapath usually has more than four registers (32 or more are common). They are built as a register file, a fast memory with simultaneous access to several locations. It holds bits, with = address bits and = bits per location. Here (8 registers ) and . It has:
- two read ports, with addresses and (outputs bus A and bus B);
- one write port, with address , data D data and enable .
Example. , : bits, i.e. 8 registers of 16 bits.
The value on bus A is also the address of the data memory (address out); bus B is the data to be written to memory (data out). The second operand of the function unit goes through MUX B: selects the register (bus B), a constant (constant in). At the output, MUX D selects with between the function-unit result () and the data read from memory (data in, ).
ALU
Definition (ALU). The ALU (Arithmetic Logic Unit) is a combinational circuit performing a set of basic arithmetic and logic operations chosen by select lines ( = mode, = operation, plus ). It is designed as an arithmetic part and a logic part, joined by a 2-to-1 MUX driven by ( arithmetic, logic).
Formula (arithmetic part). An adder ( full adders in cascade) with an input logic on producing and a carry-in : .
| 0 | 0 | all 0 | (transfer) | (increment) |
| 0 | 1 | (add) | ||
| 1 | 0 | (subtraction) | ||
| 1 | 1 | all 1 | (decrement) | (transfer) |
"All 1" is in two's complement, so , and .
Example. , , , : , i.e. .
The input logic can be synthesised from its truth table ( as a function of ) with a Karnaugh map: Check: ; ; ; .
Logic part. For each bit a 4-to-1 MUX with a 2-bit code selects among AND, OR, XOR, NOT:
| code | 00 | 01 | 10 | 11 |
|---|---|---|---|---|
One ALU stage = arithmetic stage + logic stage + output 2-to-1 MUX. Overall function (columns ):
| operation | ||||
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | |
| 0 | 0 | 0 | 1 | |
| 0 | 0 | 1 | 0 | |
| 0 | 0 | 1 | 1 | |
| 0 | 1 | 0 | 0 | |
| 0 | 1 | 0 | 1 | (subtraction) |
| 0 | 1 | 1 | 0 | |
| 0 | 1 | 1 | 1 | |
| 1 | X | 0 | 0 | |
| 1 | X | 0 | 1 | |
| 1 | X | 1 | 0 | |
| 1 | X | 1 | 1 |
Example. with code : , give (AND).
Definition (status bits N, Z, C, V). Computed from the function-unit result: = sign bit (MSB of the result); if all result bits are 0 (NOR of all bits); = carry out of the adder; = overflow = carry into the sign bit XOR carry out of it.
Examples. 8 bits, : (150, read as ), , , , . 8 bits, : , , , , . 4 bits, : , , , .
Shifter
The shift is usually done by a block separate from the ALU. A 4-bit shifter uses, for each output , a 3-to-1 MUX: no shift (passes ), shift right (passes , with input for the incoming bit), shift left (passes , input ). Serial outputs and hold the bits shifted out.
A barrel shifter also rotates (bits leaving one side re-enter on the other). Each output is a 4-to-1 MUX whose inputs are the operand bits in rotated order:
| operation | ||
|---|---|---|
| 00 | no rotation | |
| 01 | rotate by 1 | |
| 10 | rotate by 2 | |
| 11 | rotate by 3 |
Advantage: any number of positions in one cycle.
Function unit and FS codes
Property (FS codes). The function unit = ALU + shifter, with a MUX choosing the result (0 ALU, 1 shifter). There are 15 microoperations, so a 4-bit code FS suffices, instead of making the control unit drive the 7 signals (, , ): a small combinational network derives the 7 signals from .
| FS | operation | FS | operation |
|---|---|---|---|
| 0000 | 1000 | ||
| 0001 | 1001 | ||
| 0010 | 1010 | ||
| 0011 | 1011 | ||
| 0100 | 1100 | ||
| 0101 | 1101 | ||
| 0110 | 1110 | ||
| 0111 |
( repeats the transfer of ; is unused.)
Datapath control word
The control word gathers all datapath control signals: 16 bits.
| field | DA | AA | BA | MB | FS | MD | RW |
|---|---|---|---|---|---|---|---|
| bits | 3 | 3 | 3 | 1 | 4 | 1 | 1 |
| meaning | destination | source A | source B | 0: register, 1: constant | function | 0: function, 1: data in | 1: write |
Register codes: .
| microoperation | DA | AA | BA | MB | FS | MD | RW |
|---|---|---|---|---|---|---|---|
| 001 | 010 | 011 | 0 | 0101 | 0 | 1 | |
| 100 | XXX | 110 | 0 | 1110 | 0 | 1 | |
| 111 | 111 | XXX | X | 0001 | 0 | 1 | |
| 001 | 000 | XXX | 1 | 0010 | 0 | 1 | |
| Data out | XXX | XXX | 011 | 0 | XXXX | X | 0 |
| Data in | 100 | XXX | XXX | X | XXXX | 1 | 1 |
| () | 101 | 000 | 000 | 0 | 1010 | 0 | 1 |
The first one on 16 bits is 0010100110010101. Zero is obtained as the XOR of a register with itself. A sequence of microoperations is simulated by applying one control word per cycle: at each edge the register file stores the result if .
Common mistakes
- Forgetting that subtraction is (the is the ).
- Swapping bus A and bus B: bus A is also the memory address, bus B the data to write.
- Confusing shift (bits are lost) with rotation (barrel shifter).
- Thinking the ALU is sequential: it is combinational, and the result reaches the register at the clock edge.
Versione ripasso
- Computer: datapath (operations), control unit (drives the datapath according to the program), memory (program and data). Each datapath operation takes one clock cycle.
- Register file: bits, here ; two read ports ( on bus A, on bus B), one write port (, data, ). Bus A is also the data-memory address, bus B the data to store. MUX B: register, constant. MUX D: function result, data from memory.
- Function unit = ALU + shifter, MUX picks one. Status bits: (MSB), (all zero), (carry out), (carry into sign XOR carry out of sign). Example: 4 bits gives , ; 8 bits gives , , .
- Arithmetic part: , , . Gives , , , , , (with ), .
- Logic part: 4-to-1 MUX per bit (00 AND, 01 OR, 10 XOR, 11 NOT); picks arithmetic (0) or logic (1). Example: .
- Shifter / barrel shifter: 3-to-1 MUX per bit for no shift, right, left; the barrel shifter rotates by 0 to 3 positions in one cycle.
- FS (4 bits) replaces 7 control signals: , , , , , , , , AND, OR, XOR, NOT, , , .
- Control word, 16 bits: DA 3, AA 3, BA 3, MB 1, FS 4, MD 1, RW 1. Example: is
001 010 011 0 0101 0 1; zero via . - Mistakes: subtraction without ; buses swapped; shift vs rotation; ALU taken as sequential.