Defining a carry and overflow flag in 6502 emulation in Java?

I am creating a 6502 emulator and I am stuck (or, in my opinion, I, at least) already at the beginning (realizing the ADC operation). The problem is that I have to determine if there is a carry or overflow. The fact is that I can not understand the difference between them in my implementation. I know that transfer when the 9th bit is present after the operation, and I know that overflow occurs when the result is greater than 255. Does this transfer definition and overflow flag do the same?

if(result > 255) { carry = 1; overflow = 1; } else { carry = 0; overflow = 0; } 

It is not right? And if this is not so, what is right and why?

+4
source share
1 answer

The overflow flag indicates that the sign of the number has changed incorrectly. So there is overflow if you add two positive numbers and get a negative result. There is also overflow if you add two negative numbers and get a positive result.

You cannot overflow when adding two numbers of different characters, because the range does not allow this. The smallest positive plus the largest negative is 0 + (-128), which fits perfectly - 0 plus everything that fits in 8 bits will obviously go in 8 bits. The smallest negative plus the largest positive is -1 + 127 = 126. What works.

(EDIT: and even with hyphenation, 0 + -128 + 1 = -127, which fits, and -1 + 127 + 1 = 127, which fits)

Thus, overflow occurs if two inputs with the same icon produce a result with a different sign. Otherwise, this is understandable.

You can express it as just a bitwise operation on the bits of a sign. Suppose you have + b = c.

 a ^ b 

will have 1 in the sign if the signs were different. You want to know the opposite. So:

 ~(a ^ b) 

gives a 1 in the sign if the signs were the same. This is the first part of the test. Assuming a and b have the same sign, you can check c against any of them. This time you want to check the difference, just to:

 a ^ c 

You need the checked bit to be set in both parts of the test, so you can combine those that have binary code (and leave a lot of brackets to refer to verbal reasoning):

 (~(a ^ b))&(a ^ c) 

You only need a sign bit, so cancel this:

 (~(a ^ b))&(a ^ c)&0x80 

This will evaluate to 0x00 if the overflow flag should be clear and 0x80 if it should be set. So just slide this place.

(aside: although many instructions set flags on 6502, and only one provides a status register, so emulators often store flags separately in any convenient form and make up a status register on demand, in which case you won’t worry about offset and layout, you just save this result)

+10
source

Source: https://habr.com/ru/post/1483671/


All Articles