From 05f57ed93c0a640e01293f0b74f48390aae7f921 Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 29 Aug 2023 21:22:41 -0400 Subject: [PATCH] 2023-08-29 - Add info on how overflow flag should behave --- 2023-08-29--dev-notes.md | 63 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/2023-08-29--dev-notes.md b/2023-08-29--dev-notes.md index 815b27a..005348d 100644 --- a/2023-08-29--dev-notes.md +++ b/2023-08-29--dev-notes.md @@ -1,5 +1,65 @@ # Dev notes — 2023-08-29 +## Overflow flag + +This looks like a better (simpler to understand) resource: http://c-jump.com/CIS77/CPU/Overflow/lecture.html#O01_0090_signed_overflow + +> When two signed 2's complement numbers are added, overflow is detected if: +> 1. both operands are positive and the sum is negative, or +> 2. both operands are negative and the sum is positive. + +> Notice that overflow occurs only when +> `CARRYin ≠ CARRYout` +> or simply +> `V = Cin XOR Cout` +> where `V` is the overflow signal. + +And http://teaching.idallen.com/dat2343/11w/notes/040_overflow.txt: + +> The rules for turning on the overflow flag in binary/integer math are two: +> +> 1. If the sum of two numbers with the sign bits off yields a result number + with the sign bit on, the "overflow" flag is turned on. +> +> 0100 + 0100 = 1000 (overflow flag is turned on) +> +> 2. If the sum of two numbers with the sign bits on yields a result number +> with the sign bit off, the "overflow" flag is turned on. +> +> 1000 + 1000 = 0000 (overflow flag is turned on) +> +> Otherwise, the overflow flag is turned off. +> * 0100 + 0001 = 0101 (overflow flag is turned off) +> * 0110 + 1001 = 1111 (overflow flag is turned off) +> * 1000 + 0001 = 1001 (overflow flag is turned off) +> * 1100 + 1100 = 1000 (overflow flag is turned off) +> +> Note that you only need to look at the sign bits (leftmost) of the three +> numbers to decide if the overflow flag is turned on or off. +> +> If you are doing two's complement (signed) arithmetic, overflow flag on +> means the answer is wrong - you added two positive numbers and got a +> negative, or you added two negative numbers and got a positive. +> +> If you are doing unsigned arithmetic, the overflow flag means nothing +> and should be ignored. + +... + +> Overflow in two's complement may occur, not when a bit is carried out +of the left column, but when one is carried into it and no matching +carry out occurs. That is, overflow happens when there is a carry into +the sign bit but no carry out of the sign bit. +> +> The OVERFLOW flag is the XOR of the carry coming into the sign bit (if +any) with the carry going out of the sign bit (if any). Overflow happens +if the carry in does not equal the carry out. + +And https://stackoverflow.com/a/8966863: + +> The signed overflow flag value, however, must be the same for both A-B and A+(-B) because it depends on whether or not the result has the correct sign bit and in both cases the sign bit will be the same. + + ## Short-term goals: - [ ] **Update main TODO list** @@ -20,7 +80,8 @@ *** -- [ ] **fix overflow flag** +- [x] **fix overflow flag** +- [ ] **add 'dry run' mode to assembler - print debug info but don't write to file** - [ ] implement 'jmp ($FF)' on startup - [ ] implement ROM