Forum Index | FAQ | New User | Login | Search

Make a New PostPrevious ThreadView All ThreadsNext Thread*Show in Threaded Mode


SubjectOverflow Flag new Reply to this message
Posted byfinaldave
Posted on09/12/07 03:18 AM



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



SubjectRe: Overflow Flag Reply to this message
Posted byJonemaan
Posted on09/17/07 09:00 AM



> 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?
>

You didn't clearly mention which CPU you're having problems with, but I assume it's the Z80. Mine passes ZEXALL, this is what I did:

for 8-bit add/adc, sub/sbc (it's similar with 16-bit opcodes)
A=accumulator
d=data to be added/subtracted, excluding carry flag
r=result after calculation

carry: simply A>r for add, A<r for sub (or if r>8bit: r>>8&1)
half carry: like you said it's (r^A^d)&0x10
overflow: ((A^r)&(d^r))>>5&4 for add, ((A^d)&(A^r))>>5&4 for sub



SubjectRe: Overflow Flag new Reply to this message
Posted byfinaldave
Posted on09/19/07 06:19 PM



> > 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?
> >
>
> You didn't clearly mention which CPU you're having problems with, but I assume
> it's the Z80. Mine passes ZEXALL, this is what I did:
>
> for 8-bit add/adc, sub/sbc (it's similar with 16-bit opcodes)
> A=accumulator
> d=data to be added/subtracted, excluding carry flag
> r=result after calculation
>
> carry: simply A>r for add, A8bit: r>>8&1)
> half carry: like you said it's (r^A^d)&0x10
> overflow: ((A^r)&(d^r))>>5&4 for add, ((A^d)&(A^r))>>5&4 for sub

Cool :-) I came up with someting like (A^r)&(d^r), but that's really interesting that subtraction is similar as well but just with the A d r switched round... very very cool!


Newsdee's Love, Glory, and Discussion Boards



Previous ThreadView All ThreadsNext Thread*Show in Threaded Mode