Working on a 6502 ALU implementation and wanted to see if I could get another pair of eyeballs on my method of simulating overflow behaviour during an addition operation. In pseudocode, I was planning to use the following:
Code:
for A in range(0, 256):
for B in range(0, 256):
carry_in = 1
# Add with carry
C, carry_out, overflow = add(A, B, carry_in)
if not A[7] and not B[7] and carry_in:
assert C[7] == 1
assert overflow == 1
assert carry_out == 0
else if A[7] and B[7] and not carry_in:
assert C[7] == 0
assert overflow == 1
assert carry_out == 1
else:
assert overflow == 0
assert C == A + B
if C > 255:
assert carry_out == 1
For brevity, I'm not showing the case with the carry value set to 0. Does it look like I've captured the right conditions? The value of C at the end doesn't look correct to me, but I'm not sure what it should be. Also, does the output of the ALU always contain the binary sum of A and B, regardless of what the programmer might have intended? In other words, is it always the programmer's responsibility to determine the results based on what they knew when they added or subtracted something?
Thanks.