Salta al contenuto
Note per Studenti Simple Computer - Datapath, ALU and Register File Report

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 R1←R2+R3R_1\leftarrow R_2+R_3 reads R2R_2 and R3R_3, computes the sum and writes it into R1R_1 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 2m⋅n2^m\cdot n bits, with mm = address bits and nn = bits per location. Here m=3m=3 (8 registers R0…R7R_0\dots R_7) and n=16n=16. It has:

  • two read ports, with addresses AAAA and BABA (outputs bus A and bus B);
  • one write port, with address DADA, data D data and enable RWRW.

Example. m=3m=3, n=16n=16: 23⋅16=1282^3\cdot16=128 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: MB=0MB=0 selects the register (bus B), MB=1MB=1 a constant (constant in). At the output, MUX D selects with MDMD between the function-unit result FF (MD=0MD=0) and the data read from memory (data in, MD=1MD=1).

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 (S2S_2 = mode, S1S0S_1S_0 = operation, plus CinC_{in}). It is designed as an arithmetic part and a logic part, joined by a 2-to-1 MUX driven by S2S_2 (S2=0S_2=0 arithmetic, S2=1S_2=1 logic).

Formula (arithmetic part). An adder (nn full adders in cascade) with an input logic on BB producing YY and a carry-in CinC_{in}: G=A+Y+CinG=A+Y+C_{in}.

S1S_1 S0S_0 YY Cin=0C_{in}=0 Cin=1C_{in}=1
0 0 all 0 G=AG=A (transfer) G=A+1G=A+1 (increment)
0 1 BB G=A+BG=A+B (add) G=A+B+1G=A+B+1
1 0 B‾\overline B G=A+B‾G=A+\overline B G=A+B‾+1G=A+\overline B+1 (subtraction)
1 1 all 1 G=A−1G=A-1 (decrement) G=AG=A (transfer)

"All 1" is −1-1 in two's complement, so A+(−1)=A−1A+(-1)=A-1, and A+B‾+1=A−BA+\overline B+1=A-B.

Example. A=0110A=0110, B=0011B=0011, Y=B‾=1100Y=\overline B=1100, Cin=1C_{in}=1: G=0110+1100+1=(1) 0011G=0110+1100+1=(1)\,0011, i.e. A−B=3A-B=3.

The input logic can be synthesised from its truth table (YiY_i as a function of S1,S0,BiS_1,S_0,B_i) with a Karnaugh map: Yi=Bi S0+Bi‾ S1.Y_i=B_i\,S_0+\overline{B_i}\,S_1 . Check: S1S0=00⇒Yi=0S_1S_0=00\Rightarrow Y_i=0; 01⇒Bi01\Rightarrow B_i; 10⇒Bi‾10\Rightarrow\overline{B_i}; 11⇒Bi+Bi‾=111\Rightarrow B_i+\overline{B_i}=1.

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
GiG_i Ai∧BiA_i\wedge B_i Ai∨BiA_i\vee B_i Ai⊕BiA_i\oplus B_i Ai‾\overline{A_i}

One ALU stage = arithmetic stage + logic stage + output 2-to-1 MUX. Overall function (columns S2S1S0CinS_2S_1S_0C_{in}):

S2S_2 S1S_1 S0S_0 CinC_{in} operation
0 0 0 0 G=AG=A
0 0 0 1 G=A+1G=A+1
0 0 1 0 G=A+BG=A+B
0 0 1 1 G=A+B+1G=A+B+1
0 1 0 0 G=A+B‾G=A+\overline B
0 1 0 1 G=A+B‾+1G=A+\overline B+1 (subtraction)
0 1 1 0 G=A−1G=A-1
0 1 1 1 G=AG=A
1 X 0 0 G=A∧BG=A\wedge B
1 X 0 1 G=A∨BG=A\vee B
1 X 1 0 G=A⊕BG=A\oplus B
1 X 1 1 G=A‾G=\overline A

Example. S2=1S_2=1 with code 0000: A=0110A=0110, B=0011B=0011 give G=0010G=0010 (AND).

Definition (status bits N, Z, C, V). Computed from the function-unit result: NN = sign bit (MSB of the result); Z=1Z=1 if all result bits are 0 (NOR of all bits); CC = carry out of the adder; VV = overflow = carry into the sign bit XOR carry out of it.

Examples. 8 bits, 70+8070+80: G=10010110G=10010110 (150, read as −106-106), V=1V=1, C=0C=0, N=1N=1, Z=0Z=0. 8 bits, 255+1255+1: G=0G=0, C=1C=1, Z=1Z=1, V=0V=0, N=0N=0. 4 bits, 0111+0001=10000111+0001=1000: N=1N=1, Z=0Z=0, C=0C=0, V=1V=1.

Shifter

The shift is usually done by a block separate from the ALU. A 4-bit shifter uses, for each output HiH_i, a 3-to-1 MUX: S=0S=0 no shift (passes BiB_i), S=1S=1 shift right (passes Bi+1B_{i+1}, with input IRI_R for the incoming bit), S=2S=2 shift left (passes Bi−1B_{i-1}, input ILI_L). Serial outputs LL and RR 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:

S1S0S_1S_0 Y3Y2Y1Y0Y_3Y_2Y_1Y_0 operation
00 D3D2D1D0D_3D_2D_1D_0 no rotation
01 D2D1D0D3D_2D_1D_0D_3 rotate by 1
10 D1D0D3D2D_1D_0D_3D_2 rotate by 2
11 D0D3D2D1D_0D_3D_2D_1 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 MFMF 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 (MFMF, G3…G0G_3\dots G_0, H1H0H_1H_0): a small combinational network derives the 7 signals from FSFS.

FS operation FS operation
0000 F=AF=A 1000 F=A∧BF=A\wedge B
0001 F=A+1F=A+1 1001 F=A∨BF=A\vee B
0010 F=A+BF=A+B 1010 F=A⊕BF=A\oplus B
0011 F=A+B+1F=A+B+1 1011 F=A‾F=\overline A
0100 F=A+B‾F=A+\overline B 1100 F=BF=B
0101 F=A+B‾+1F=A+\overline B+1 1101 F=sr BF=sr\,B
0110 F=A−1F=A-1 1110 F=sl BF=sl\,B
0111 F=AF=A

(FS=0111FS=0111 repeats the transfer of AA; FS=1111FS=1111 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: R0…R7=000…111R_0\dots R_7=000\dots111.

microoperation DA AA BA MB FS MD RW
R1←R2−R3R_1\leftarrow R_2-R_3 001 010 011 0 0101 0 1
R4←sl R6R_4\leftarrow sl\,R_6 100 XXX 110 0 1110 0 1
R7←R7+1R_7\leftarrow R_7+1 111 111 XXX X 0001 0 1
R1←R0+2R_1\leftarrow R_0+2 001 000 XXX 1 0010 0 1
Data out ←R3\leftarrow R_3 XXX XXX 011 0 XXXX X 0
R4←R_4\leftarrow Data in 100 XXX XXX X XXXX 1 1
R5←0R_5\leftarrow0 (R0⊕R0R_0\oplus R_0) 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 RW=1RW=1.

Common mistakes

  • Forgetting that subtraction is A+B‾+1A+\overline B+1 (the +1+1 is the CinC_{in}).
  • 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: 2m⋅n2^m\cdot n bits, here 8×168\times16; two read ports (AAAA on bus A, BABA on bus B), one write port (DADA, data, RWRW). Bus A is also the data-memory address, bus B the data to store. MUX B: MB=0MB=0 register, 11 constant. MUX D: MD=0MD=0 function result, 11 data from memory.
  • Function unit = ALU + shifter, MUX MFMF picks one. Status bits: NN (MSB), ZZ (all zero), CC (carry out), VV (carry into sign XOR carry out of sign). Example: 4 bits 0111+0001=10000111+0001=1000 gives N=1N=1, V=1V=1; 8 bits 255+1255+1 gives G=0G=0, C=1C=1, Z=1Z=1.
  • Arithmetic part: G=A+Y+CinG=A+Y+C_{in}, Y∈{0,B,B‾,1…1}Y\in\{0,B,\overline B,1\dots1\}, Yi=BiS0+Bi‾S1Y_i=B_iS_0+\overline{B_i}S_1. Gives AA, A+1A+1, A+BA+B, A+B+1A+B+1, A+B‾A+\overline B, A−BA-B (with Cin=1C_{in}=1), A−1A-1.
  • Logic part: 4-to-1 MUX per bit (00 AND, 01 OR, 10 XOR, 11 NOT); S2S_2 picks arithmetic (0) or logic (1). Example: 0110∧0011=00100110\wedge0011=0010.
  • 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: 00000000 AA, 00010001 A+1A+1, 00100010 A+BA+B, 00110011 A+B+1A+B+1, 01000100 A+B‾A+\overline B, 01010101 A−BA-B, 01100110 A−1A-1, 01110111 AA, 10001000 AND, 10011001 OR, 10101010 XOR, 10111011 NOT, 11001100 BB, 11011101 sr Bsr\,B, 11101110 sl Bsl\,B.
  • Control word, 16 bits: DA 3, AA 3, BA 3, MB 1, FS 4, MD 1, RW 1. Example: R1←R2−R3R_1\leftarrow R_2-R_3 is 001 010 011 0 0101 0 1; zero via R0⊕R0R_0\oplus R_0.
  • Mistakes: subtraction without +1+1; buses swapped; shift vs rotation; ALU taken as sequential.

Teoria collegata