6502 relative Branch = x86 jump short ?
6502 relative Branch = x86 jump short ?
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 ?
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 ?
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: 6502 relative Branch = x86 jump short ?
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.
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?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: 6502 relative Branch = x86 jump short ?
OK
then how to implement 6502 branching in C (if not in inline assembly) ?
is the following code do the job ? :-
your suggestions please .
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 bytesRe: 6502 relative Branch = x86 jump short ?
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:
While BBC Basic on the 6502 produced:
The 4 byte backward branch on both CPUs usees an offset of &FC
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
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
Re: 6502 relative Branch = x86 jump short ?
I don't see anyone mentioning page crossing
behavior?
Dwight
behavior?
Dwight
Re: 6502 relative Branch = x86 jump short ?
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:
While BBC Basic on the 6502 produced:
The 4 byte backward branch on both CPUs usees an offset of &FC
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
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
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: 6502 relative Branch = x86 jump short ?
dwight wrote:
I don't see anyone mentioning page-crossing behavior?
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?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: 6502 relative Branch = x86 jump short ?
GARTHWILSON wrote:
dwight wrote:
I don't see anyone mentioning page-crossing behavior?
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
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: 6502 relative Branch = x86 jump short ?
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?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: 6502 relative Branch = x86 jump short ?
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 don't know why I thought this.
I just ran the experiment so I'm sure I was in error.
Dwight
Re: 6502 relative Branch = x86 jump short ?
Sailor wrote:
OK
then how to implement 6502 branching in C (if not in inline assembly) ?
is the following code do the job ? :-
.
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 bytesRe: 6502 relative Branch = x86 jump short ?
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
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
Re: 6502 relative Branch = x86 jump short ?
Dwight
thanks for reply , all reading done as binary .
thanks for reply , all reading done as binary .
Re: 6502 relative Branch = x86 jump short ?
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
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
Re: 6502 relative Branch = x86 jump short ?
OK , but I have no idea how to count cycles .