|
From: Scott D. <sc...@da...> - 2004-06-11 15:47:57
|
On Thu, 10 Jun 2004, Eric Smith wrote:
> In searching the archives for messages regarding LCDs, I see that there
> was some discussion of the overflow flag not too long ago. Perhaps
> this has already been resolved, but in case it hasn't...
>
> I don't know how Microchip has implemented this, and have not tried
> writing a test program, but I do know what most if not all other
> microprocessors do.
The only thing I know about their implementation is the data sheet's
description of the OV bit:
----
bit 3 OV: Overflow bit
This bit is used for signed arithmetic (2 s complement).
It indicates an overflow of the 7-bit magnitude, which causes
the sign bit (bit7) to change state.
1 = Overflow occurred for signed arithmetic (in this
arithmetic operation)
0 = No overflow occurred
----
>
> Suppose you're doing an eight-bit add, so your operands are a7..a0 (MSB
> through LSB), b7..b0, and c0 (carry in). Your result is r7..r0 and
> c8 (carry out). Inside the adder, there is a carry from each stage to
> the next. (Assume a ripple carry adder; the actual implementation may be
> different but the principle is the same.) So the carry from stage 6 to
> stage 7 is c7.
>
> The overflow is c7 xor c8.
>
> In C this can be done with two additions, some masking and shifting,
> and an xor:
>
> sum = a + b + c0;
> r = sum & 0xff;
> c8 = sum >> 8;
> v = (c8 ^ ((((a & 0x7f) + (b & 0x7f) + c0) & 0x80) >> 7)) & 1;
Another way to write it in C is:
sum = a + b + c0;
r = sum & 0xff;
v = ( ((~sum & a & b) | (sum & ~a & ~b)) & 0x80) ? 1 : 0;
In words, this says that if a and b are positive and the sum is negative,
then an overflow occurred. Similarly if a and b are negative and the sum
is positive that also is an overflow.
A perhaps somewhat simpler expression is:
v = ( ((sum ^ ~b) & (a ^ ~b)) & 0x80) ? 1 : 0;
and for subtract:
diff = a - b - (c0^1)
v = ( ((~diff & a & ~b) | (diff & ~a & b)) & 0x80) ? 1 : 0;
r = diff & 0xff;
or
v = ( ((sum ^ b) & (a ^ b)) & 0x80) ? 1 : 0;
In either case, the carry in (or borrow in for subtract) doesn't
BTW, I was looking at the OV flag for additions, and I see there's a bug
in gpsim.
Scott
|