Trying to write (yet another!) cpu emulator, this time in C so you don't have the luxury of opcodes to get the flags for you! So I'm struggling with the Overflow flag...
The docs seems to say:
"overflow is set if signed overflow occured"
so here's some examples I came up with:
0x20 0x20 = 0x040 - no carry, no overflow
0xc0 0xc0 = 0x180 - carry, no overflow
0x40 0x40 = 0x080 - no carry, overflow (because it should be 128 not -128)
0x80 0x80 = 0x100 - carry, overflow (because it should be -256 not 0)
0x40 0x80 = 0x0c0 - no carry, no overflow
Some emulators seem to do something with boolean operations on the operands and result, so I tried building a Karnaugh map of bit 7 in operands and result:
bit 7 in all:
x y z x ^ ~y x ^ z
0 0 0, no carry, no overflow 1 0
0 0 1, no carry, overflow 1 1
0 1 0, carry, no overflow 0 0
0 1 1, no carry, no overflow 0 1
1 0 0, carry, no overflow 0 1
1 0 1, no carry, no overflow 0 0
1 1 0, carry, overflow 1 1
1 1 1, carry, no overflow 1 0
overflow = (x ^ ~y) & (x ^ z) ?
Weirdly when I plug this into a Karnaugh map solver I just get (~x)(~y)z xy(~z) which seems worse than the above, so not sure if I've done my maths wrong, or if an xor is just considered more expensive??
I'm Googling and I can't find much information about whether this is right... has anyone done this before? What method did you use for the carry, half-carry and overflow flags? (Not too bothered about half carry since I working on 68000, but just curious for Z80). Noticed further down the board something was mentioned about xoring all three for half carry?
Any help much appreciated :-)
Newsdee's Love, Glory, and Discussion Boards