6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Nov 14, 2024 4:54 am

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: PC push/pop with Branch?
PostPosted: Thu Aug 22, 2019 10:22 pm 
Offline

Joined: Sat Jan 05, 2019 12:33 pm
Posts: 15
Is it possible to manually push and pop the PC with the 65816?

I need to do PC relative jumps but can't really think of a reasonable way to jump back. Any ideas?


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 22, 2019 10:26 pm 
Offline

Joined: Sat Jan 05, 2019 12:33 pm
Posts: 15
PER?


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 22, 2019 10:32 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8540
Location: Southern California
Instructions that come to mind for pushing the program counter are PEA (push 16-bit effective absolute address, although it can also be data, or it can be the address of the PC if it's known at assembly time), PER (push 16-bit effective relative address, and can be your actual address if you don't give it an offset), PHK (push program-bank register), and of course JSR and JSL. Instructions that pull the PC off the stack are RTS, RTL, RTI. (Be careful about the one-byte difference between return-from-interrupt and return-from-subroutine.) There are more ways to skin this cat, but these are the main ones that immediately come to mind. Other members who have more '816 experience than I have will undoubtedly chime in with more ideas.

Chapter 13 of the 6502 stacks treatise, "65816's instructions and capabilities relevant to stacks, and 65c02 code which partially synthesizes some of them" http://wilsonminesco.com/stacks/816newinst.html addresses this kind of thing.

_________________
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: Thu Aug 22, 2019 11:18 pm 
Offline

Joined: Sat Jan 05, 2019 12:33 pm
Posts: 15
Thank you!

I did it with the PER instruction. Took me a moment to figure out how to calculate the relative offset correctly.

Code:
PER  return
BRA  subroutine
return: NOP

...

subroutine:
...
RTS


The NOP is needed for a correct return address.


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 23, 2019 3:29 am 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
foxchild wrote:
The NOP is needed for a correct return address.
The NOP is one solution, but not the only solution. :)

Code:
PER  return-1              ; <-- altered. ( Was PER return )
BRA  subroutine
return:                    ; <-- altered. ( NOP removed )

...

subroutine:
...
RTS

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 23, 2019 8:08 am 
Offline

Joined: Sat Jan 05, 2019 12:33 pm
Posts: 15
YES! That's even better, thank you! :)


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 23, 2019 8:19 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8483
Location: Midwestern USA
foxchild wrote:
Is it possible to manually push and pop the PC with the 65816?

I need to do PC relative jumps but can't really think of a reasonable way to jump back. Any ideas?

I'm late to the party here, but what you want is the equivalent of the BSR instruction found in the Motorola 68K MPUs. It can be easily written as a macro:

Code:
;   BSR: Branch to Subroutine
;   ——————————————————————————————————————————————————————————————————————
;   This macro synthesizes the BSR instruction implemented in the Motorola
;   6800 & 68000 microprocessors.  Programs in which subroutines are call-
;   ed via BSR are fully relocatable,  as the target address is calculated
;   relative to the program counter at run-time.   The target address must
;   be within the range of a long relative branch, +$7FFF or -$8000 bytes.
;   ——————————————————————————————————————————————————————————————————————
;
bsr      .macro .sr            ;BSR <addr>
.mib     =$82
.mip     =$62
.na      =*+3
.ra      .set .na+2
.ba      =.ra+1
.ra      .set .ra-.na
.ta      .set .sr-.ba
         .byte .mip,<.ra,>.ra,.mib,<.ta,>.ta
         .endm

The above was written to synthesize BSR <ADDR> on the '816, using the Kowalski simulator to assemble the source code. It takes advantage of BRL (long relative branch) and PER.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 23, 2019 8:29 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10977
Location: England
Thanks BDD. So, I think that's a variation with greater range than Jeff's:

Code:
PER  return-1              ; <-- altered. ( Was PER return )
BRL  subroutine
return:                    ; <-- altered. ( NOP removed )
...
subroutine:
...
RTS


versus
Dr Jefyll wrote:
Code:
PER  return-1              ; <-- altered. ( Was PER return )
BRA  subroutine
return:                    ; <-- altered. ( NOP removed )
...
subroutine:
...
RTS


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 23, 2019 11:28 am 
Offline

Joined: Sat Jan 05, 2019 12:33 pm
Posts: 15
BigEd wrote:
Thanks BDD. So, I think that's a variation with greater range than Jeff's:

Code:
PER  return-1              ; <-- altered. ( Was PER return )
BRL  subroutine
return:                    ; <-- altered. ( NOP removed )
...
subroutine:
...
RTS


versus
Dr Jefyll wrote:
Code:
PER  return-1              ; <-- altered. ( Was PER return )
BRA  subroutine
return:                    ; <-- altered. ( NOP removed )
...
subroutine:
...
RTS


Had to use the BRL in the end, yeah... What I do not understand is: Why do I exit with RTS and not RTL after BRL?


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 23, 2019 2:52 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
foxchild wrote:
Had to use the BRL in the end, yeah... What I do not understand is: Why do I exit with RTS and not RTL after BRL?


Not all "xxL"s are created equal.

BRL is the "16-bit flavor" of the 8-bit BRA. BRL can't break out of the current code bank, so there's no need to save that bit of state (which is a good thing, since PER only pushes 16 bits of address). So RTS is the proper return instruction.

JSL is the "24-bit flavor" of the 16-bit JSR. It does need to save the code bank state, so it pushes a 24-bit return address, which is expected by RTL.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
PostPosted: Sat Aug 24, 2019 1:39 am 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
If you need to emulate JSL, perhaps because you need a long-indirect (which JMP provides) rather than only a 16-bit indirect, then you can use the following:
Code:
 PHK
 PER return-1
 JMP [fn_ptr]
return:
In that case you would return with RTL.


Top
 Profile  
Reply with quote  
PostPosted: Sat Aug 24, 2019 5:03 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8483
Location: Midwestern USA
Chromatix wrote:
If you need to emulate JSL, perhaps because you need a long-indirect (which JMP provides) rather than only a 16-bit indirect, then you can use the following:
Code:
 PHK
 PER return-1
 JMP [fn_ptr]
return:
In that case you would return with RTL.

Why not just use the JSL instruction with the self-modifying code and let the '816 do the stack grunting?

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


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

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: