6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu May 09, 2024 6:44 pm

All times are UTC




Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Generic for loop
PostPosted: Thu Feb 13, 2020 2:18 am 
Offline
User avatar

Joined: Sat Dec 01, 2018 1:53 pm
Posts: 727
Location: Tokyo, Japan
BillO wrote:
x is never changed so why are you doing sta(scrLow,x)?...Would this not work more efficiently?
Code:
           sta (scrLow)       ; put it on the screen

That addressing mode doesn't exist on the 6502. (At least, not in the NMOS version that the 2600 uses. Maybe they added it to some 65C02 variants.)

I find this reference useful for quickly looking up the addressing modes available for each instruction. (And what flags are set by each instruction, too.)

_________________
Curt J. Sampson - github.com/0cjs


Top
 Profile  
Reply with quote  
 Post subject: Re: Generic for loop
PostPosted: Thu Feb 13, 2020 2:19 am 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
Quote:
x is never changed so why are you doing sta(scrLow,x)?

I imagine it's because the NMOS 6502 core - which is what you'd get in a 2600 - doesn't have a non-indexed indirect addressing mode. You either get pre-indexed by X, or post-indexed by Y.

The approach by sark02 looks sound in principle, taking advantage of the index registers and the post-indexed addressing mode. I don't have time today to fettle the setup code around it, but the core is useful. Its one limitation is that it doesn't easily support a final inner loop of less than 256 bytes; you have to put the remainder portion in the first iteration. This may not matter in the end, though.


Top
 Profile  
Reply with quote  
 Post subject: Re: Generic for loop
PostPosted: Thu Feb 13, 2020 2:56 am 
Offline
User avatar

Joined: Fri Dec 12, 2008 10:40 pm
Posts: 1001
Location: Canada
Okay, did some study on the responses, and thank you both for taking the time. I did leave the parenthesis in and may have conflated things a touch ...

So why not the use zero page addressing mode: sta scrLow ? scrLow is only a byte value, so will still refer to page 0. There should be little change, except for saving a few cycles using the zero page mode.

like:
Code:
fill:      lda #3             ; set the color
           sta scrLow       ; put it on the screen
           inc scrLow         ; increment low byte
           bne fill           ; loop if not zero
           inc scrHigh        ; increment high byte
           lda scrHigh        ; load high byte
           cmp $03            ; compare high byte
           bne fill           ; != so increment low byte
           lda scrLow         ; load low byte
           cmp $02            ; compare low byte
           bne fill           ; != so increment low byte
     
end:       brk


Still trying to wrap my head around this, so pleae be patient...

_________________
Bill


Top
 Profile  
Reply with quote  
 Post subject: Re: Generic for loop
PostPosted: Thu Feb 13, 2020 4:04 am 
Offline

Joined: Tue Nov 10, 2015 5:46 am
Posts: 215
Location: Kent, UK
sark02 wrote:
Code:
L1:   
    sta ($20),y
    iny
    bne L1
    inc $21
    dex
    bpl L1
A quick addendum to my code:
Whereas sta (ind),y is 6 cycles, std abs,y is only 5, so if the loop can be in RAM rather than ROM, it can be modified to:
Code:
L1:   
    sta $1234,y   ; $1234 patched at runtime
    iny
    bne L1
    inc L1+2      ; increment upper byte of STA address
    dex
    bpl L1
This uses (L1+2,L1+1) as the write address, as opposed to ($21,$20). Saves 1 cycle/iteration.


Top
 Profile  
Reply with quote  
 Post subject: Re: Generic for loop
PostPosted: Thu Feb 13, 2020 4:17 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1928
Location: Sacramento, CA, USA
I don't know if it's just the kooky way my brain works, but I think you can save some code space but get 'er done just as quickly if your application can tolerate filling from high to low (untested):
Code:
; Fill X*256+Y bytes of memory with A, based
; at ($21,$20) ... don't try to exceed 33023 bytes!
; exit: Y=0, X=$FF, A and base pointer preserved
fill:
   pha
   txa
   clc
   adc $21
   sta $21
   pla
   cpy #0
   beq fill3
fill2:
   dey
   sta ($20),y
   bne fill2
fill3:
   dec $21
   dex
   bpl fill2
   inc $21
   rts

_________________
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 Thu Feb 13, 2020 5:48 am, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Generic for loop
PostPosted: Thu Feb 13, 2020 5:12 am 
Offline
User avatar

Joined: Sat Dec 01, 2018 1:53 pm
Posts: 727
Location: Tokyo, Japan
barrym95838 wrote:
I don't know if it's just the kooky way my brain works, but I think you can save some code space but get 'er done just as quickly if your application can tolerate filling from high to low....

Your brain works like a 6502! I guess that qualifies as "kooky...."

And yes, I've noticed this too. My bigint representation was initially little-endian, following the convention of the CPU. But I changed it to big-endian, going opposite the convention, specifically because on the 6502 you can save a CMP and perhaps more by counting down to zero rather than counting up to your target. A related technique is to use 1-based indexes for arrays rather than 0-based: the loop control is just DEY followed by BEQ done.

_________________
Curt J. Sampson - github.com/0cjs


Top
 Profile  
Reply with quote  
 Post subject: Re: Generic for loop
PostPosted: Thu Feb 13, 2020 6:22 am 
Offline

Joined: Tue Nov 10, 2015 5:46 am
Posts: 215
Location: Kent, UK
barrym95838 wrote:
I don't know if it's just the kooky way my brain works, but I think you can save some code space but get 'er done just as quickly if your application can tolerate filling from high to low [...]
Nice one.


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

All times are UTC


Who is online

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