6502 relative Branch = x86 jump short ?

Topics pertaining to the emulation or simulation of the 65xx microprocessors and their peripheral chips.
Sailor
Posts: 12
Joined: 07 Jul 2017

6502 relative Branch = x86 jump short ?

Post by Sailor »

hi every body
I am trying to simulate each instruction of the 6502 in C ( open watcom v1.8 )
by using inline assembly , my question is that :

are the 6502 branch instructions equal (the same) to the X86 jump short instructions ?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: 6502 relative Branch = x86 jump short ?

Post by GARTHWILSON »

Hopefully someone will give a better explanation comparing to x86; but the operand on 6502 relative-branch instructions is an offset relative to the first byte of the next instruction, meaning for example D0 00 (ie, BNE next_instruction) is a branch with no effect. An operand with the high bit set is to branch backwards; so D0 FE will conditionally branch back to itself.

No 6502 / 65c02 / 65816 programmer should be without the programmer's manual "Programming the 65816—Including the 6502, 65C02 and 65802" by David Eyes and Ron Lichty. It's way better than any other I know, and way better than the description lets on.
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?
Sailor
Posts: 12
Joined: 07 Jul 2017

Re: 6502 relative Branch = x86 jump short ?

Post by Sailor »

OK
then how to implement 6502 branching in C (if not in inline assembly) ?
is the following code do the job ? :-

Code: Select all

char addr ; //address of branching ,  char range is -128 to 127
unsigned int PC ; // word Program Counter
PC+=addr // if address is <=127 branching is forward , if address >127 branching is backward
// add 2 to addr ? because branching instructions are 2 bytes
your suggestions please .
Martin A
Posts: 197
Joined: 02 Jan 2016

Re: 6502 relative Branch = x86 jump short ?

Post by Martin A »

It would appear 8086 short jump and the 6502 branching instructions calculate their offsets the same way.

Using the build in assembler in BBC basic, I've produced 2 simple bits of code:

BBC Basic for DOS Produced:

Code: Select all

8086 Branching
099F                              OPT X%
099F B0 00                        MOV AL,0
09A1                    .branch
09A1 FE C0                        INC AL
09A3 75 FC                        JNE branch
09A5 C3                           RET
While BBC Basic on the 6502 produced:

Code: Select all

6502 Branching
08B4                    OPT X%
08B4 A9 00              LDA #0
08B6          .branch   
08B6 69 01              ADC #1
08B8 D0 FC              BNE branch
08BA 60                 RTS
The 4 byte backward branch on both CPUs usees an offset of &FC
dwight
Posts: 213
Joined: 08 Jun 2004

Re: 6502 relative Branch = x86 jump short ?

Post by dwight »

I don't see anyone mentioning page crossing
behavior?
Dwight
Sailor
Posts: 12
Joined: 07 Jul 2017

Re: 6502 relative Branch = x86 jump short ?

Post by Sailor »

Martin A wrote:
It would appear 8086 short jump and the 6502 branching instructions calculate their offsets the same way.

Using the build in assembler in BBC basic, I've produced 2 simple bits of code:

BBC Basic for DOS Produced:

Code: Select all

8086 Branching
099F                              OPT X%
099F B0 00                        MOV AL,0
09A1                    .branch
09A1 FE C0                        INC AL
09A3 75 FC                        JNE branch
09A5 C3                           RET
While BBC Basic on the 6502 produced:

Code: Select all

6502 Branching
08B4                    OPT X%
08B4 A9 00              LDA #0
08B6          .branch   
08B6 69 01              ADC #1
08B8 D0 FC              BNE branch
08BA 60                 RTS
The 4 byte backward branch on both CPUs usees an offset of &FC
that is what I have expected , so could I depend on that and continue ?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: 6502 relative Branch = x86 jump short ?

Post by GARTHWILSON »

dwight wrote:
I don't see anyone mentioning page-crossing behavior?
Crossing a page boundary adds one cycle to the branch, from three cycles to four, for 65(c)02 and for the 65816 running in emulation mode. The 65816 running in native mode doesn't need the fourth clock. Regardless, most branches do not cross a page boundary.
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?
dwight
Posts: 213
Joined: 08 Jun 2004

Re: 6502 relative Branch = x86 jump short ?

Post by dwight »

GARTHWILSON wrote:
dwight wrote:
I don't see anyone mentioning page-crossing behavior?
Crossing a page boundary adds one cycle to the branch, from three cycles to four, for 65(c)02 and for the 65816 running in emulation mode. The 65816 running in native mode doesn't need the fourth clock. Regardless, most branches do not cross a page boundary.
I was talking about when the instruction it self crosses an page boundary. I recall that it had a similar
problem the the NMOS parts had for the indirect jump instruction where it didn't see that the high part
of the PC had changed and didn't do a correct page crossing.
I could be wrong because it has been many years. It may only be the indirect jump that had the problem.
Dwight
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: 6502 relative Branch = x86 jump short ?

Post by GARTHWILSON »

That was on JMP(xxFF). NMOS did not increment the page when reading the second byte, so the jump was wrong. That bug (and all others) were fixed in the CMOS version. There was no bug in the branch instructions though. I rounded up all the NMOS-CMOS differences I could find and put them on the page http://wilsonminesco.com/NMOS-CMOSdif/ .
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?
dwight
Posts: 213
Joined: 08 Jun 2004

Re: 6502 relative Branch = x86 jump short ?

Post by dwight »

GARTHWILSON wrote:
That was on JMP(xxFF). NMOS did not increment the page when reading the second byte, so the jump was wrong. That bug (and all others) were fixed in the CMOS version. There was no bug in the branch instructions though. I rounded up all the NMOS-CMOS differences I could find and put them on the page http://wilsonminesco.com/NMOS-CMOSdif/ .
I was in error this only effects the indirect jump and not the branch.
I don't know why I thought this.
I just ran the experiment so I'm sure I was in error.
Dwight
Sailor
Posts: 12
Joined: 07 Jul 2017

Re: 6502 relative Branch = x86 jump short ?

Post by Sailor »

Sailor wrote:
OK
then how to implement 6502 branching in C (if not in inline assembly) ?
is the following code do the job ? :-

Code: Select all

char addr ; //address of branching ,  char range is -128 to 127
unsigned int PC ; // word Program Counter
PC+=addr // if address is <=127 branching is forward , if address >127 branching is backward
// add 2 to addr ? because branching instructions are 2 bytes
.
:?:
dwight
Posts: 213
Joined: 08 Jun 2004

Re: 6502 relative Branch = x86 jump short ?

Post by dwight »

In a simulator, it might be better to increment the PC after each
instruction fetch. That way, all you have to do is add the branch offset as
a signed byte to the PC. If bit 8 changes in the PC add the
extra cycle to your cycle counter.
All this within C.
Oops, I just reread your original post. Are you simulating from
the text or the binary image after the assembly?
Dwight
Sailor
Posts: 12
Joined: 07 Jul 2017

Re: 6502 relative Branch = x86 jump short ?

Post by Sailor »

Dwight
thanks for reply , all reading done as binary .
dwight
Posts: 213
Joined: 08 Jun 2004

Re: 6502 relative Branch = x86 jump short ?

Post by dwight »

Then what I suggested should work fine.
I do hope you have a cycle counter. You also want an
easy way to connect to it with user code. Outside interaction
is the most important part of a simulator.
With the cycle count, you can tack on things like a usart, video
action or even a random input timed keyboard. Without it
it just runs instructions and little else.
Dwight
Sailor
Posts: 12
Joined: 07 Jul 2017

Re: 6502 relative Branch = x86 jump short ?

Post by Sailor »

OK , but I have no idea how to count cycles .
Post Reply