6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Nov 21, 2024 5:03 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Fri Oct 19, 2018 1:30 pm 
Offline
User avatar

Joined: Wed Aug 17, 2005 12:07 am
Posts: 1250
Location: Soddy-Daisy, TN USA
So, my 6502 is a little rusty. Been a while since I was active.

I have a delay routine where I want to preserve A, X and Y. Using 65C02, here is what I have:

Code:
;------------------------------------------------------------------------------
;       Name:           DELAY1
;       Desc:           Slight delay (TODO, calculate and re-name)
;       Destroys:       Nothing
;------------------------------------------------------------------------------
DELAY1:
        PHA                     ; Push A to stack
        TXS                     ; Push X to stack
        TYA                     ; Transfer Y to A (why no PUSH Y to stack??)
        PHA                     ; Push A to stack (previous value of Y)

        LDX     #$00
        LDY     #$10
_DELAY1:
        DEX
        BNE     _DELAY1
        DEY
        BNE     _DELAY1

        PLA
        TAY                     ; Restore Y
        TSX                     ; Restore X
        PLA                     ; Restore A

        RTS


I'm not at my 6502 computer at the moment...but does this seem to be correct?

Any shorter way to do this?

Thanks!

_________________
Cat; the other white meat.


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 19, 2018 1:41 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
Oops: TXS is not pushing X to stack! It is copying X to the stack pointer, which is to say resetting the stack. On 6502 you need PHA, TXA, PHA, TYA, PHA or similar. On 65C02 I think you can use PHA, PHX, PHY.


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 19, 2018 1:44 pm 
Offline
User avatar

Joined: Wed Aug 17, 2005 12:07 am
Posts: 1250
Location: Soddy-Daisy, TN USA
Ah thanks!

I totally read that wrong. I thought the 65C02 had better stack support but I guess my searching skills are failing me.

Thanks again.

Now my routine is:

Code:
;------------------------------------------------------------------------------
;       Name:           DELAY1
;       Desc:           Slight delay (TODO, calculate and re-name)
;       Destroys:       Nothing
;------------------------------------------------------------------------------
DELAY1:
        PHA                     ; Push A to stack
        PHX                     ; Push X to stack
        PHY                     ; Push Y to stack

        LDX     #$00
        LDY     #$10
_DELAY1:
        DEX
        BNE     _DELAY1
        DEY
        BNE     _DELAY1

        PLY                     ; Restore Y
        PLX                     ; Restore X
        PLA                     ; Restore A

        RTS

_________________
Cat; the other white meat.


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 19, 2018 1:58 pm 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
That would work, but since you don't clobber the accumulator you can delete the PHA/PLA instructions.

What you do clobber is the status register. You can save and restore that with PHP/PLP, if it happens to matter. I advise saving it first and restoring it last.


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 19, 2018 2:02 pm 
Offline
User avatar

Joined: Wed Aug 17, 2005 12:07 am
Posts: 1250
Location: Soddy-Daisy, TN USA
Ah, that's true. I was still thinking that I needed A to hold Y so that's why I was pushing it to the stack.

Code:
;------------------------------------------------------------------------------
;       Name:           DELAY1
;       Desc:           Slight delay (TODO, calculate and re-name)
;       Destroys:       Nothing
;------------------------------------------------------------------------------
DELAY1:
        PHP                     ; Push status register
        PHX                     ; Push X to stack
        PHY                     ; Push Y to stack

        LDX     #$00
        LDY     #$10
_DELAY1:
        DEX
        BNE     _DELAY1
        DEY
        BNE     _DELAY1

        PLY                     ; Restore Y
        PLX                     ; Restore X
        PLP                     ; Restore status register
       
        RTS

_________________
Cat; the other white meat.


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 19, 2018 3:19 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
Here's one, from the impressive mind of dclxvi:
Code:
; Delay 9*(256*_a+_y)+26 cycles (+12 for jsr/rts)
; Assumes that the BCS does not cross a page boundary
; Credit to dclxvi for the inner mechanics
delay:
      pha
      phy
      lda   #_a
      ldy   #_y
dloop:
      cpy   #1
      dey
      sbc   #0
      bcs   dloop
      ply
      pla
      rts


You can fine-tune your delay anywhere from 26 to 589841 cycles, with a granularity of 9.

_________________
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)


Last edited by barrym95838 on Sat Oct 20, 2018 12:03 am, edited 6 times in total.

Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 19, 2018 3:24 pm 
Offline
User avatar

Joined: Wed Aug 17, 2005 12:07 am
Posts: 1250
Location: Soddy-Daisy, TN USA
Oh wow. That's really clever!
Thanks!

_________________
Cat; the other white meat.


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

All times are UTC


Who is online

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