Introduction
The datapath is the piece of circuitry in the CPU responsible for data manipulation, registers storage and flags. The datapath is steered by the control unit to perform the CPU instructions.

Datapath in more detail
As we saw earlier the datapath is responsible to provide data operations for the control unit, now we're going to analyse it in greater detail. By describing each main component.
The signals that control the datapath operation, forms a word called Control Word, that will be generated by the Control unit, for each instruction.

Multiplexer
This element is responsible to select the signals from the external world, from the registers, or from the imediate operand.
Register File
This element will hold the 16 16-bit registers used in our CPU
ALU
This combinational circuit is responsible to provide the arithimetic and logic operations of the datapath. The table bellow will list all ALU operations.
| code | operation | Description |
|---|---|---|
| op_pass | ALUOutput = A | Does nothing |
| op_and | ALUOutput = A and B | And operation |
| op_or | ALUOutput = A or B | Or operation |
| op_not | ALUOutput = not A | Not operation |
| op_add | ALUOutput = A + B | Addition |
| op_sub | ALUOutput = A - B | Subtraction |
| op_inc | ALUOutput = A++ | Increment |
| op_dec | ALUOutput = A-- | Decrement |
Shifter
This element will provide the shift (left,right) and rotate operations.
| code | operation | Description |
|---|---|---|
| op_pass | ALUOutput = A | Does nothing |
| op_shift_left | ALUOutput = A << 1 | Shift left |
| op_shift_right | ALUOutput = A >> 1 | Shift right |
| op_rotate_right | ALUOutput = A ror 1 | Rotate right |
Flags circuit
This combinational circuit is responsible to provide tha flags (Zero, Carry, Sign and overflow) based on the datapath results.
Tristate Buffer
This buffer is used to apply logic levels high, low, impedance on the external line, this is used to allow a single external interface act as a input/output port.
Control Word
Each datapath module has is own control signals, all of those signals form the control word, the idea now is to find the control word that executes all instructions from our instruction set. In our case this is our Control Word.
| SelMux | RFWriteAdd | RFReadAAdd | RFReadBAdd | RFReadAE | RFReadBE | AluOP | ShifterOP | WriteRF | OE |
|---|---|---|---|---|---|---|---|---|---|
sel_External sel_Imediate sel_AluOut |
r0..r15 | r0..r15 | r0..r15 | Enable port A reading | Enable port B reading | op_pass op_and op_or op_not op_add op_sub op_inc op_dec |
op_pass op_shift_right op_shift_left op_rotate_right |
Write value in Register File | Output Enable |
Simple Algorithm sample
Now we're going to execute a simple algorithm generating all ControWords needed. Here is all simple algorithm ....
r1 = 5; input r2; // Consider the external input to be 1, so this while will only run once (r2=1) while (r2 != 0) { r1 = r1 + r2; r2--; } output r1; // Output should be 6 output r2; // Output should be 0
Now let's derive all control word bits needed to execute this algorithm.
Line 1 (r1 = 5)
- InImediate = 5
- SelMuxInput = sel_Imediate
- SelAluOp = op_pass
- SelShifterOp = op_pass
- RegFileWriteAddress = r1
- WriteRegistersFile = '1'
- RegFileReadAAddress = r0
- RegFileReadBAddress = r0
- RegFileReadAEnable = '0'
- RegFileReadBEnable = '0'
- OutputEnable = '0'
Line 2 (input r2)
- InExternal = 1
- SelMuxInput = sel_External
- SelAluOp = op_pass
- SelShifterOp = op_pass
- RegFileWriteAddress = r2
- WriteRegistersFile = '1'
- RegFileReadAAddress = r0
- RegFileReadBAddress = r0
- RegFileReadAEnable = '0'
- RegFileReadBEnable = '0'
- OutputEnable = '0'
Line 3 (r1 = r1 + r2)
- InExternal = Z
- InImediate = Z
- SelMuxInput = sel_AluOut
- RegFileReadAAddress = r1
- RegFileReadBAddress = r2
- RegFileReadAEnable = '1'
- RegFileReadBEnable = '1'
- SelAluOp = op_add
- SelShifterOp = op_pass
- RegFileWriteAddress = r1
- WriteRegistersFile = '1'
- OutputEnable = '0'
Line 4 (r2--)
- InExternal = Z
- InImediate = Z
- SelMuxInput = sel_AluOut
- RegFileReadAAddress = r2
- RegFileReadBAddress = r0
- RegFileReadAEnable = '1'
- RegFileReadBEnable = '0'
- SelAluOp = op_dec
- SelShifterOp = op_pass
- RegFileWriteAddress = r2
- WriteRegistersFile = '1'
- OutputEnable = '0'
Line 5 (output r1)
- InExternal = Z
- InImediate = Z
- SelMuxInput = sel_AluOut
- RegFileReadAAddress = r1
- RegFileReadBAddress = r0
- RegFileReadAEnable = '1'
- RegFileReadBEnable = '0'
- SelAluOp = op_pass
- SelShifterOp = op_pass
- RegFileWriteAddress = r0
- WriteRegistersFile = '0'
- OutputEnable = '1'
Line 6 (output r2)
- InExternal = Z
- InImediate = Z
- SelMuxInput = sel_AluOut
- RegFileReadAAddress = r2
- RegFileReadBAddress = r0
- RegFileReadAEnable = '1'
- RegFileReadBEnable = '0'
- SelAluOp = op_pass
- SelShifterOp = op_pass
- RegFileWriteAddress = r0
- WriteRegistersFile = '0'
- OutputEnable = '1'
Bellow we have a figure of the datapath been simulated with ModelSim

Timming Issues
Before we see the Control Unit we must know one of the most important problems when designing digital circuits. When the control unit is interfacing with the Datapath it generates control words for every instruction and the Datapath respond with flags results. Some care must be taken to ensure that the datapath generated the correct result. For exemple when you write a value to some register the proper value will be avaible only on the next clock cycle.
In order to correct timming issues you can add an extra state to wait for the valid result, or to place your flag circuits in other places. Let's see a real exemple to ilustrate this.
Imagine this scenario that we write to the register A the value '5' and in the next instruction it tests the zero flag.
A = 5
if (A == 5)
//Do something
Our Datapath look like this:
In the exact moment that we write in register A the value 5 the (A=5) flag will not present the correct result, actually the value 5 will be avaible in the register in the next clock cycle. The solution for this problem could be add some wait cycle in the control unit in order to get things settle down. Other solution is to replace the flag circuit directly in the input.
Next topic:
Now that we defined our Datapath we're going to see how the Control Unit works