And if you haven't seen it, this was recently posted.
http://www.righto.com/2012/12/the-6502-overflow-flag-explained.htmlNote that the javascript 6502 emulator you referenced has bugs, I have examined the code and it needs to be corrected.
Ken Shirrif's blog notes that for code, you can use
(A ^ sum) & (operand ^ sum) & 0x80 is nonzero. Overflow occurs when the signs of A and the operand are the same and when the sign of A and the sum are the same.
Another C++ formula is !((M^N) & 0x80) && ((M^result) & 0x80) "
The Stella emulator uses the latter formulation except it uses bitwise operators and eschews the logical && to avoid doing the & 0x80 twice.
C code executed after an add
V = ~(A ^ operand) & (A ^ sum) & 0x80;
Many emulators do the same thing that the 6502 does and use the same code for subtract but first do a ones complement of the operand prior to adding.
Here is some branchless javascript code to do SBC (in non decimal mode); It works similar to the 6502 in the aspect that it does the ones complement first and then just uses add logic. In this emulation, the flags are kept in variables and are reassembled into the actual P register when required for PHP
Code:
// javascript code for SBC , 'a' is the accumulator and 'operand' is the data to be subtracted
// ADC would be the same except the ones complement ~operand & 0xff is not needed
operand = ~operand & 0xff;
var sum = a + operand + (Cf>>8);
Nf = sum & 0x80;
Vf = (~(a ^ operand) & (a ^ sum) & 0x80);
Cf = (sum & 0x100);
a = sum & 0xff;
nZf = a;
note this keeps a NOT zero flag instead of a zero flag