6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu May 23, 2024 6:23 am

All times are UTC




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Fri Jul 07, 2017 11:43 pm 
Offline

Joined: Fri Jul 07, 2017 11:14 pm
Posts: 12
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 ?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 12:26 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8442
Location: Southern California
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?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 9:35 am 
Offline

Joined: Fri Jul 07, 2017 11:14 pm
Posts: 12
OK
then how to implement 6502 branching in C (if not in inline assembly) ?
is the following code do the job ? :-

Code:
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 .


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 11:17 am 
Offline

Joined: Sat Jan 02, 2016 10:22 am
Posts: 197
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:
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:
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


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 1:05 pm 
Offline

Joined: Tue Jun 08, 2004 11:51 pm
Posts: 213
I don't see anyone mentioning page crossing
behavior?
Dwight


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 1:49 pm 
Offline

Joined: Fri Jul 07, 2017 11:14 pm
Posts: 12
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:
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:
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 ?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 6:33 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8442
Location: Southern California
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?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 8:26 pm 
Offline

Joined: Tue Jun 08, 2004 11:51 pm
Posts: 213
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


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 8:38 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8442
Location: Southern California
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?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jul 08, 2017 9:05 pm 
Offline

Joined: Tue Jun 08, 2004 11:51 pm
Posts: 213
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 09, 2017 4:39 pm 
Offline

Joined: Fri Jul 07, 2017 11:14 pm
Posts: 12
Sailor wrote:
OK
then how to implement 6502 branching in C (if not in inline assembly) ?
is the following code do the job ? :-

Code:
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


.

:?:


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 09, 2017 5:30 pm 
Offline

Joined: Tue Jun 08, 2004 11:51 pm
Posts: 213
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Jul 09, 2017 6:31 pm 
Offline

Joined: Fri Jul 07, 2017 11:14 pm
Posts: 12
Dwight
thanks for reply , all reading done as binary .


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 10, 2017 5:29 am 
Offline

Joined: Tue Jun 08, 2004 11:51 pm
Posts: 213
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 10, 2017 6:13 pm 
Offline

Joined: Fri Jul 07, 2017 11:14 pm
Posts: 12
OK , but I have no idea how to count cycles .


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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: