6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Nov 14, 2024 2:19 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: 6502 branch
PostPosted: Thu Nov 26, 2015 11:31 pm 
Offline

Joined: Thu Nov 26, 2015 11:13 pm
Posts: 3
Hi everybody
i am writing simple emulator in (QBasic) for educational purpose (I don't care about speed)
I am now work in 6502 , it should reads an atari 2600 rom ,, yet every thing is ok
but I just have a problem with branches , for example BEQ I know it means branch if zero flag set
and the next byte is a signed address from $00 - $F7 forward branching and $80 - $FF backwards branching
all that ok , but how Implement backwards branching using QBasic ?
how to make my program branches backwards if values $80 - $FF ?


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 branch
PostPosted: Fri Nov 27, 2015 12:54 am 
Offline

Joined: Sun Feb 23, 2014 2:43 am
Posts: 78
Something like this should work:
Code:
if(value >= $80 AND value <= $FF)
{
  value = value XOR $FF
  value = value + 1
  value = -value
}


Edit: could also do:
Code:
value = $FF - value
instead of the XOR.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 branch
PostPosted: Fri Nov 27, 2015 3:35 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
I'm thinking something like:
Code:
if branch = true then pc = pc + operand + 256 * (operand > 127)

This example assumes that pc is your program counter, and that it's already pointing to the op-code of the instruction immediately following the branch instruction, which is where it should point if the branch is not taken. It also assumes that the conditional expression (operand > 127) is 0 if false and -1 if true.

The branch = true part prevents updating of pc if the branch is not taken.

The pc = pc + operand part adjusts the pc correctly for positive displacements.

The + 256 * (operand > 127) part corrects the adjustment for negative displacements only. It could be rewritten as - 2 * (operand AND 128) if that floats your boat.

Mike B.


Last edited by barrym95838 on Fri Nov 27, 2015 6:46 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 branch
PostPosted: Fri Nov 27, 2015 6:44 am 
Offline

Joined: Thu Nov 26, 2015 11:13 pm
Posts: 3
OK I believe all your posted solutions are great , I have worked on barrym post :
in QBasic of course
Code:
address = 128     rem given address
pc = 1000         rem current program counter position

pc = pc + address + 256 * (address > 127)


now if the address less than 128 , then it will not be changed and the branch is forward
else , the address will be negative and the result is branching backwards
*if the address given is 128 and the pc points to 1000 then the result is 872 (new pc content)
*if the address given is 255 the branch will be 1 step back
is this right results ?


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 branch
PostPosted: Sat Nov 28, 2015 4:59 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
That seems to be correct. In practice, it would be very unusual to branch with a value of 255, since this would branch back to the middle of your branch instruction and try to use the value of 255 as the next op-code!

I'm glad you got it working. Please consider sharing your code with us when you're ready.

Mike B.


Top
 Profile  
Reply with quote  
 Post subject: 6502 branch : solved
PostPosted: Sat Nov 28, 2015 7:10 am 
Offline

Joined: Thu Nov 26, 2015 11:13 pm
Posts: 3
Thanx Mike B
yes it works nicely ,, and the most funny part for me is 256*(operand>127)
that was really genius , and about sharing , of course I would be happy when sharing my work with you
however I have long way to finish .


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: gregorio and 2 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: