6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Apr 20, 2024 1:09 am

All times are UTC




Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Overflow confusion
PostPosted: Fri Oct 25, 2019 4:28 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
barrym95838 wrote:
However, the correct result will always still fit properly in nine bits, and this 9-bit twos complement result can be utilized by simply appending the C flag to the high end of the accumulator as the "ninth bit".

Not quite; the ninth bit of the result is N xor V.


Top
 Profile  
Reply with quote  
 Post subject: Re: Overflow confusion
PostPosted: Fri Oct 25, 2019 5:46 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1922
Location: Sacramento, CA, USA
Thanks, dclxvi. If V is clear then C is clearly not the ninth bit in the signed sense. But if V is set, is there a (signed) test case you can provide to us where C != N xor V ??

P.S. In other words, can you tell me if the following statements are true in the general case:

On the 65xx, the 8-bit twos complement result of the ADC and SBC instructions is contained in the accumulator, unless the V flag is set, in which case the 9-bit twos complement result is contained in the C flag appended to the most-significant edge of the accumulator. In the former case, the N flag contains the sign of the result, and in the latter case the C flag contains the sign. Also, the Z flag is invalid in the special 9-bit case of 1 00000000 (-256), but valid otherwise.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Last edited by barrym95838 on Fri Oct 25, 2019 5:49 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Overflow confusion
PostPosted: Sat Oct 26, 2019 12:42 am 
Offline
User avatar

Joined: Mon Apr 23, 2012 12:28 am
Posts: 760
Location: Huntsville, AL
A few months ago I presented a 16 x 16 Signed Multiplication routine for my M65C02A core and Bitwise provided one for th 65816. Initial testing provided good results, but Bitwise indicated that he wasn't getting the expected result for a product of -32768 x -32768.
Bitwise wrote:
I had a go for the 65C816. This works for normal values but not -32768 - Not sure where the extra bit P can be held.

Some time ago I had decided to implement an arithmetic right shift instruction for my M65C02A core. The intention was to use it improve the time required for multiplications using the Booth algorithm. A previous attempt at a 8 x 8 signed multiplication routine for the 65C02 using a Booth algorithm required keeping a "guard" bit for the sign bit.

In my M65C02A core, I also implemented an instruction to reverse the bits of a register which allows the C flag / bit to function as the Booth "recoding" memory bit and the N flag to sense the state of the next bit in the multiplier. The multiplier is shifted left after reversing it which allows the C flag / bit to hold the previously shifted value and the N flag to test the state of the next multiplier bit. A simple SEC instruction at the beginning of the routine correctly initializes the Booth "recoding" memory bit.

Regardless, a guard bit is required for the sign bit of the product register. In an hardware implementation, an additional bit duplicating the sign bit is very easily included. Because of the number of additional instructions and memory cycles / locations required to implement a guard bit, I decided to use the overflow flag V to indicate those cases where the addition / subtraction of the multiplicand to the double length product register overflowed the representation. I restore the sign of the product during the arithmetic right shift as described below. In short, I think that the sign is determined from the N and V flags rather than the C and V flags:
MichaelM wrote:
BitWise:

Something you said regarding the -32768 * -32768 case made me go back and check my implementation. I found that my result was 0xC000_0000 instead of the correct 0x4000_0000.

In getting the bugs out of the M65C02A Python model and the _imul() source, I had removed a feature of the M65C02A-specific asr.w a instruction that enables the restoration of the correct sign bit when the preceding add/sub operation of the Booth multiplication operation generates an arithmetic overflow. When -32768 is added to -32768 in the upper product register, the result is 0x8000_0000. The V flag correctly indicates an overflow.

I had modified the asr.w a to restore the sign by using the V flag and the N flag. If V is set, then the correct sign bit is the complement of the N flag. (If the V flag is not set, then the sign is the same as the N flag.) With this feature restored to the asr.w a instruction, the _imul() does correctly compute the product of -32768 * -32768.

_________________
Michael A.


Top
 Profile  
Reply with quote  
 Post subject: Re: Overflow confusion
PostPosted: Sat Oct 26, 2019 1:02 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1922
Location: Sacramento, CA, USA
MichaelM wrote:
... A previous attempt at a 8 x 8 signed multiplication routine for the 65C02 using a Booth algorithm required keeping a "guard" bit for the sign bit ...

Holy moly, Batman! That thing is ginormous!

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
 Post subject: Re: Overflow confusion
PostPosted: Sat Oct 26, 2019 1:49 am 
Offline
User avatar

Joined: Mon Apr 23, 2012 12:28 am
Posts: 760
Location: Huntsville, AL
Thing of beauty isn't it? :D

_________________
Michael A.


Top
 Profile  
Reply with quote  
 Post subject: Re: Overflow confusion
PostPosted: Sun Dec 18, 2022 10:55 am 
Offline

Joined: Tue Aug 04, 2020 10:35 pm
Posts: 6
Location: Verona, Italy
ttlworks wrote:
Also, see here.


Just wanted to say thank you because I was having a hard time trying to understand the Overflow thing while having some fun with a customization of the Ben Eater 8 bit computer based on TTL chips (https://tomnisbet.github.io/nqsap/docs/flags/ linking to your 74181 with V_Flag and related pages, that I found very difficult to comprehend at the beginning, but now I'm so happy I managed to understand let's say 95%) :D


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: