Now we've built the constituent parts, it's now time to put everything together into our ALU chip.
The ALU has two outputs: the 8-bit result of the operation that was performed, and a 4-bit "flags" value which contains information about the calculation.
You should set the flags according to the following logic:
Flag | Logic |
Overflow | 1 if the addition of two numbers with the same sign bit produces a result with a different sign, or if the subtraction of two numbers with different signs produces a result with the same sign as the number being subtracted. 0 otherwise. |
Carry / Borrow | 1 if an addition or subtraction operation results in a carry / borrow. 0 otherwise. |
Negative flag | 1 if the sign bit (most significant bit) of the result is 1. 0 otherwise. |
Zero flag | 1 if every bit of the result is 0. 0 otherwise. |
The ALU has three inputs: the first and second 8-bit operands, and a 4-bit "opcode" which selects the operation to be performed.
This processor's ALU will use the following opcodes:
Opcode | Operation | Explaiation |
0000 | add | Adds the second operand to the first, setting the carry in to 0. |
0001 | subtract (sub) | Subtracts the second operand from the first, setting the carry in to 0. |
0010 | add with carry (adc) | Adds the second operand to the first, setting the carry in to the value of the carry flag. |
0011 | subtract with borrow (subb) | Subtracts the second operand from the first, setting the carry in to the value of the carry flag. |
0100 | nand | Each bit of the output is the result of NANDing together the respective bits of each operand. |
0101 | xor | Each bit of the output is the result of XORing together the respective bits of each operand. |
0110 | shift left (sll) | Each bit of operand 1 is shifted to the left by the number of places specified by operand 2. |
0111 | shift right (srl) | Each bit of operand 1 is shifted to the right by the number of places specified by operand 2. |
1XXX | move (mov) | Operand 2 is passed straight through to the result. No operation is performed. |
This is the most complicated part of the process, and it's OK if you don't quite understand everything yet. You can use the green links to get more information about certain words, and now might be a good time to ask your tutor for help if you are truly stuck.
Once you are finished, you can return to the CPU.