6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 5:05 am

All times are UTC




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: SBC and Z flag
PostPosted: Mon Aug 14, 2023 7:36 am 
Offline

Joined: Mon Aug 14, 2023 6:53 am
Posts: 4
Hello!

A few days ago YT suggested a 6502-emulation video to me, so now it looks like I'm doing an emulator. For fun, as you do :D.

I've read and seen a couple of explanations of how the SBC works, and that it's an ADC with ~op2, which is easy enough to implement as I already had the ADC implemented.

I've used the emulator at https://www.masswerk.at/6502/ to generate testdata for my instructions with this simple program:

SEC/CLC
LDA #xx
SBC #yy


However, one particular case seems weird to me:
CLC ;edited
LDA #$00
SBC #$FF
=> A: 00 N:0 V:0 Z:0 C:0

Why is Z 0?
I can solve it be only setting Z:1 if C:1, but I haven't seen any documentation mentioning this, just that it reflects the result of A.

Can somebody please enlighten a green 6502 nerd?

edit: CLC, not SEC


Last edited by pappabj0rn on Mon Aug 14, 2023 8:52 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 8:22 am 
Online
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
Welcome.

Z will reflect the zero or non-zero status of the result.  Your result is 0 which I assume is because you started with CLC instead of SEC as you show.  You can think of the C flag as an inverted "borrow."  $(1)00 minus $FF would result in 1; but starting with C clear, it will subtract another one, landing you back on 0.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 8:41 am 
Offline

Joined: Sun Jun 29, 2014 5:42 am
Posts: 352
papapbj0rn wrote:
However, one particular case seems weird to me:
SEC
LDA #$00
SBC #$FF
=> A: 00 N:0 V:0 Z:0 C:0

In this example, A should be 01 not 00,.


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 9:04 am 
Offline

Joined: Mon Aug 14, 2023 6:53 am
Posts: 4
You are right, it should be CLC, not SEC. I edited my first post.
I wrote the program in hex and translated to the wrong mnemonic for the forum post, sorry.

So, is ~C considered part of the result and thusly why Z:0, not just what is in register A?


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 10:04 am 
Offline

Joined: Sun Jun 29, 2014 5:42 am
Posts: 352
papapbj0rn wrote:
You are right, it should be CLC, not SEC. I edited my first post.
I wrote the program in hex and translated to the wrong mnemonic for the forum post, sorry.

So, is ~C considered part of the result and thusly why Z:0, not just what is in register A?


There is a bug in the emulator you are using for that particular case, because Z should always match the 8-bit result. So in this case it should be Z:1.

Here's the emulator's SBC code:
Code:
function opSub(oper) {
   var r=ac-oper-((flags&fCAR)?0:1),
      rb=r&0xff;
   if (flags&fDEC) {
      var h, c=0,
         l=(ac&15)-(oper&15)-((flags&fCAR)?0:1),
         h1=(ac>>4)&15,
         h2=(oper>>4)&15;
      flags &= ~(fCAR|fZER|fOVF|fNEG);
      if (l<0) {
         l+=10;
         c=1;
      }
      else if (l>9) {
         l=(l+6)&15;
      }
      h=h1-h2-c;
      if (h<0) {
         h+=10;
         flags|=fCAR;
      }
      else if (h>9) {
         h=(h+6)&15;
      }
      r=(h<<4)|l;
      if (r==0) {
         flags|=fZER|fCAR;
      }
      else {
         flags|=fCAR;
      }
      if (r&0x80) flags|=fNEG;
   }
   else {
      flags &= ~(fCAR|fZER|fOVF|fNEG);
      if (r==0) {
         flags |=fZER|fCAR;
      }
      else if (r>0) {
         flags |=fCAR;
      }
      flags|=r&0x80;
      r=rb;
   }
   if ((ac^rb)&((0xff-oper)^rb)&0x80) flags|=fOVF;
   ac=r;
}

Can you spot what the bug is?

I would not recommend using this emulator as a golden reference!


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 11:54 am 
Offline

Joined: Mon Aug 14, 2023 6:53 am
Posts: 4
Ok, great. That certainly clears that.
I thought it be best to assume existing emulators probably had it right and I'm wrong. But in cases like this it seems a second opinion was needed.

I'm guessing the bug is only setting fZER when r==0, when it should check rb?

Thanks for the help!


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 12:04 pm 
Offline

Joined: Sun Jun 29, 2014 5:42 am
Posts: 352
papapbj0rn wrote:
I'm guessing the bug is only setting fZER when r==0, when it should check rb?

Yes, except in your example r is -256, hence the r==0 test failing.

Dave


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 2:03 pm 
Offline

Joined: Fri Mar 18, 2022 6:33 pm
Posts: 491
Interesting; I've used that emulator a fair bit when I was away from home and wanted to test some code. That seems like a fairly bad bug!

_________________
"The key is not to let the hardware sense any fear." - Radical Brad


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 8:53 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
I pinged Norbert and he's fixed the bug!


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 9:53 pm 
Offline

Joined: Sun Jun 29, 2014 5:42 am
Posts: 352
BigEd wrote:
I pinged Norbert and he's fixed the bug!

Nice one Ed.

As this emulator is quite popular, I wonder if it's worth trying to run the Dormann tests?


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 11:41 pm 
Offline

Joined: Mon Aug 14, 2023 10:18 pm
Posts: 7
Hi, everyone!

I just fixed this.

What's somewhat interesting is that nobody noticed or reported this in all those years. Even more so, as the same emulation (with a few additions for cycle accurate timing) is powering the PET 2001 emulator at https://www.masswerk.at/pet and not a single program seems to have been affected by this. (So, for all practical means, this seems to be quite a rare edge case.)

Regarding running any test suites, this may be a bit problematic as the emulator runs at reduced speed in order to convey a glimpse on what's happening. If there's any test suite running on a PET, this would be more realistic and welcome.

As far as I know, the PET emulator runs every program there is, including the 2022 demos. (The only exceptions are those requiring a business keyboard. But I haven't seen a program fail or received any reports of it failing.) Especially "A Bright Shining Star" by Genesis Project is highly critical and VICE used to fail on this with timing issues. (I haven't verified this for the latest version of VICE, so, please, don't take my word for granted. I guess, they should have fixed this in the meantime.)
Still, this somewhat proves that real-life performance is not a perfect test and further tests may be required.

--Norbert


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Mon Aug 14, 2023 11:54 pm 
Offline

Joined: Mon Aug 14, 2023 10:18 pm
Posts: 7
PS: On a positive note, all the applications of the "virtual 6502" suite have recently received an update for improved import of hex-dumps, disassemblies, and Commodore PRG-files. (Meaning, hex-dumps, etc, should be cleaned up properly prior to import and any code start addresses found should be applied automatically. You will be prompted for confirmation, since every automation may fail over something, I haven thought of.)
Moreover, there's now also a small hex-editor for quick for quick edits and/or patch-up jobs. (Its special feature is that it comes with a few text tools.)


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Tue Aug 15, 2023 12:23 am 
Offline

Joined: Mon Aug 14, 2023 6:53 am
Posts: 4
Cool!


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Wed Aug 16, 2023 6:37 am 
Offline

Joined: Mon Aug 14, 2023 10:18 pm
Posts: 7
So, good news: the emulator passed the basic (functional) Dorman test – that is, w/o any BCD mode ADC/SBC tests.
I'll have a look into BCD later.

-- Norbert


Top
 Profile  
Reply with quote  
 Post subject: Re: SBC and Z flag
PostPosted: Wed Aug 16, 2023 6:42 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
That is good news!

(I have to say, Klaus Dormann's test suite has been, and continues to be, an excellent tool for showing up little bugs in emulators. Indeed, it's also been useful in showing up design or implementation bugs in hardware systems, as a consequence of being a thorough exerciser of opcodes. Note that it's not an exhaustive test - one might try Wolfgang Lorenz' suite for that. (See https://web.archive.org/web/20201112023 ... stPrograms))


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 guests


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: