diff --git a/CPU.md b/CPU.md new file mode 100644 index 0000000..b69f667 --- /dev/null +++ b/CPU.md @@ -0,0 +1,95 @@ +# Central processing unit (CPU) +Work in progress + +## CPU Architecture + +Work in progress + + +## Random-access memory + +Work in progress + + +## General purpose registers +The registers are 16-bits locations available to the CPU and are used in some instructions and system calls. Read [Basic Assembly Tutorial](https://github.com/simon987/Much-Assembly-Required-3/wiki/Basic-Assembly-tutorial) for more information. + +| Register | Purpose | +| -------- | ------- | +| **A** | General | +| **B** | General | +| **C** | General | +| **D** | General | +| **X** | General | +| **Y** | General | +| **BP** | Base pointer | +| **SP** | Stack Pointer | + +Read [Basic Assembly Tutorial](https://github.com/simon987/Much-Assembly-Required/wiki/Basic-Assembly-tutorial) for more information about the BP & SP registers and the stack. + +## The FLAGS register +The FLAGS register holds the **status** - information about the last executed instruction - of the CPU. The FLAGS register cannot be read or written directly. There are 5 flags that can modified or read by instructions (Read [Instruction set](https://github.com/simon987/Much-Assembly-Required/wiki/Instruction-set) to learn which instructions modifies the FLAGS register): + +| Flag | Description | +|:--------- |:----------- | +| **CF** (Carry flag) | Relevent for *unsigned* operations. Indicates that there was a carry out of the most significant (leftmost) bit | +| **ZF** (Zero flag) | Indicates that the result of an operation is 0 | +| **SF** (Sign flag) | Indicates that the most significant (leftmost) bit of the result of an operation is set | +| **OF** (Overflow flag) | Relevent for *signed* operations. Indicates that the sign of the result of an signed operation is wrong (See exemples below) | +| **BF** (Break flag) | Tells the CPU to stop the execution. This flag is set with the **BRK** instruction. | + +### Carry Flag +The carry flag is set when there is a carry out of the destination's most significant bit + + For example: +``` + + 1111 1111 1111 1111 # Signed:-1 Unsigned: 65535 + 0000 0000 0000 0001 # Signed: 1 Unsigned: 1 + ------------------- + 0000 0000 0000 0000 +``` +Note that the result of the result of the previous operation is perfectly valid for signed numbers (See [Basic Assembly Tutorial](https://github.com/simon987/Much-Assembly-Required/wiki/Basic-Assembly-tutorial)) but it is invalid for unsigned operations. + + +### Sign flag +The sign flag is set when the most significant (leftmost) bit of the result of an operation is set. + +For example: +``` + + 0100 0000 0000 0000 # Signed: 16384 Unsigned: 16384 + 0100 0000 0000 0000 # Signed: 16384 Unsigned: 16384 + ------------------- + 1000 0000 0000 0000 # Signed:-32768 Unsigned: 32768 + ^ + Sign flag is set +``` + +### Overflow flag +The carry flag is set when the signed two's complement result of an operation does not fit in the destination, or more specifically, when there is a carry/borrow **into** or **out** of the most significant (leftmost) bit, **but not both**. +The overflow flag is set when the result of an addition of two positive numbers is negative or when the result of an addition of two negative numbers is positive. + +For example: +``` + There is a carry out of the most significant (leftmost) bit, OF is set. + v + + 1111 1111 1111 1111 # Signed:-1 Unsigned: 65535 + 1000 0000 0000 0000 # Signed:-32768 Unsigned: 32768 + ------------------- + 0111 1111 1111 1111 # Signed: 32768 Unsigned: 32768 + + There is a carry out of the most significant bit + \ There is also a carry into the most significant bit, OF is not set + VV + + 1111 1111 1111 1111 + 1100 0000 0000 0000 + ------------------- + 1111 1111 1111 1111 + + There is also a carry into the most significant bit, OF is set + V + + 0100 0000 0000 0000 + 0100 0000 0000 0000 + ------------------- + 1000 0000 0000 0000 + +```