6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Sep 29, 2024 1:29 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sat Jan 24, 2009 8:29 pm 
Offline

Joined: Sat Jan 24, 2009 7:56 pm
Posts: 1
Hello,

I'm new here, I'm an average-low 6502 coder. I request help :

I have multiple LDA #xx instructions in my program and I need to change frequently the #xx values (which are all differents). I made a small routine but I'm not content with. I could make a table of these values in zp area and make LDA tab_zp_value; LDA tab_zp_value+1 etc... but I run out of space in zp.
The current code works but it's inelegant, I'm quite sure it is possible to make something more packed and optimized.

infos :
pt_col 2 bytes and zp.
temp_store : 1 byte and zp.
in main program there are at random location : pt_col0: LDA #xx


You noticed that I have to store and reset .X before using the STA (pt_col,x), not very efficient and slow.

If you've got any idea, I would be please to discuss it (sorry for my bad english).

Code:
   
      LDX #22
      LDY #11
lp_col:
      LDA tab_pt_col,x
      STA pt_col
      LDA tab_pt_col+1,x
      STA pt_col+1
      STX temp_store
      LDX #0
      LDA tab_col_boss,y
      STA (pt_col,x)
      LDX temp_store
      
      DEX
      DEX
      DEY
      BPL lp_col
      RTS

tab_col_boss:
         .BYTE COLOR0_BOSS1,COLOR1_BOSS1,COLOR2_BOSS1,COLOR3_BOSS1,COLOR4_BOSS1,COLOR5_BOSS1
         .BYTE COLOR6_BOSS1,COLOR7_BOSS1,COLOR8_BOSS1,COLOR9_BOSS1,COLOR10_BOSS1,COLOR11_BOSS1

tab_pt_col:   .WORD pt_col0+1,pt_col1+1,pt_col2+1,pt_col3+1,pt_col4+1,pt_col5+1
         .WORD pt_col6+1,pt_col7+1,pt_col8+1,pt_col9+1,pt_col10+1,pt_col11+1


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jan 24, 2009 11:42 pm 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
The code, as written, is about as efficient as it gets, because you are scattering the color values throughout the program. If you can, however, coalesce them into a simple table sitting in RAM, so as to guarantee your palette entries are all adjacent to each other, you can free up a few cycles by using the (dp),Y addressing mode. See the code below:
Code:
   
    LDX #<table_col0
    STX pt_col
    LDX #>table_col0
    STX pt_col+1

    LDY #11
lp_col:
    LDA tab_col_boss,Y
    STA (pt_col),y
    DEY
    BPL lp_col
    RTS

tab_col_boss:
    .BYTE COLOR0_BOSS1,COLOR1_BOSS1,COLOR2_BOSS1,COLOR3_BOSS1,COLOR4_BOSS1,COLOR5_BOSS1
    .BYTE COLOR6_BOSS1,COLOR7_BOSS1,COLOR8_BOSS1,COLOR9_BOSS1,COLOR10_BOSS1,COLOR11_BOSS1

Then, for your various pt_col0, pt_col1, etc. instructions, replace LDA #col0 with LDA table_col0, etc.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Jan 25, 2009 1:13 am 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
Here's how I understand what you want.

Somewhere there's a table of addresses, called tab_pt_col,
for locations of lda # instructions in your code that you want
to alter depending on what you're doing now, and somewhere else
there's a table called tab_col_boss of values to stuff
in corresponding lda # locations (they correspond, therefore you
only need one index so the same index can be used for the
locations and their new values)
I'm not quite sure of the purpose of putting the tab_pt_col
table in zp and using X, I don't think it's faster but I didn't
count the cycles

Anyway, here's what I came up with

I split the tab_pt_col table in two, one for the low
byte and one for the high byte
And since you don't seem averse to self modifing code,
I used self modifing code.

With this code you pass a single byte base address parameter
in y. The parameter is the begining of the tab_col_boss table
and has to be somewhere in the tab_col_boss page
(so you can have more than one table as long as they're all
in the tab_col_boss page).


some where you have to initalize pt_col


Code:
 lda #tab_col_boss_page
 sta pt_col+1


and then you'd ldy parameter and jsr CHANGE_COL

Code:
CHANGE_COL
 sty pt_col
 ldy #$0B
LP_COL
 lda tab_pt_col_lo,y
 sta POINTER+1
 lda tab_pt_col_hi,y
 sta POINTER+2
 lda (pt_col),y
POINTER
 sta $0000
 dey
 bpl LP_COL
 rts

Edit: it occurs to me that I may have introduced
a source of some slight confusion in that I reused the name
"pt_col" but it's used for something else. POINTER+1
and POINTER+2, ie the $0000 in the sta $0000 instruction,
take the place of pt_col of the original code


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Jan 26, 2009 12:54 am 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
I still don't think I'd want to waste zp on a table of pointers,
but just for the hell of it, here's a routine that uses indexed
indirect addressing

Instead of splitting the pointer table into high and low bytes,
the table is split into odd and even pointers.
that is, all the odd pointers from the original tab_pt_col table
go (in order) into one table and all the even pointers into
another table

Code:
 ldx #$0B
LP_COL
 lda tbl_col_boss,x
 sta (tab_pt_col_odd,x)
 dex
 lda tbl_col_boss,x
 sta (tab_pt_col_even,x)
 dex
 bpl LP_COL


And dang if it ain't both smaller and (slightly) faster

Edit: heck, I forgot it's only going through the
loop half as many times so it's substantially faster.
hmm


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

All times are UTC


Who is online

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