Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user Nicron ! (Registered 2024-05-21) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Want to understand this way of looping through a table
2023-10-13 21:16
Flashback

Registered: Dec 2009
Posts: 4
Want to understand this way of looping through a table

Hi,

I would like some explanation about this way of looping a colour table:

colourcycle.asm

Basically it's this section I can't wrap my head around:
...
txa     ; select next colour index
adc #01
and #07 ; loop colour index
tax
...
colours  .byte 09, 08, 05, 13, 01, 13, 05, 08 ; colour table


As far as I understand, the start color is saved in a ZP address (startcol) which will is changed each frame. This is done by txa/ adc #$01 I guess. (BTW, why is there no clc before adc here?) But why do we need the and #$07?

Regards
2023-10-13 21:23
chatGPZ

Registered: Dec 2001
Posts: 11147
Quote:
BTW, why is there no clc before adc here?

To answer this first: it's a bug. It should be there :)

As for the AND... ANDing with any value that is (2^n)-1 will limit the value to 0...(2^n)-1. You basically throw away the upper bits.
2023-10-13 22:07
Flashback

Registered: Dec 2009
Posts: 4
Ok, so by ANDing with #$07 (%00000111) I delete bits 3-7, correct? So my result can't be > 7 as well.

Is this because I only have 8 entries in my colour table? So if I wanted to have more colours, I also would have to change this part?
2023-10-13 22:23
rosettif

Registered: Apr 2016
Posts: 10
Obviously, the author assumes C=0 for sure when entering the code, so it is just not needed (the CLC). Nevertheless, it would be better in this way (one byte shorter):

inx
txa
and #$07
tax

As long as you are thinking in power of two, it can be easily modified without the need of comparing (e.g. when having 16 colours instead of 8, the #$07 is replaced with #$0f).
2023-10-13 23:13
Copyfault

Registered: Dec 2001
Posts: 467
I'd also assume carry is taken care of somewhere in the code before that extracted section - but tbh, I did not check that asm yet.

So this is some index handling to get x = 0..7. In case one can live with an offset of 1, i.e. having the table starting at 1 instead of 0 of a page, the unintended sbx-opcode helps getting it even smaller:
lda #$07
sbx #$ff

This effetively slides through the values x = 1..8, with a footprint of 4 bytes and 4 cycles.
2023-10-14 10:04
Flashback

Registered: Dec 2009
Posts: 4
Thank you guys. Never used this approach, I would rather do something like

ldx tabindex
lda coltab,x
...
inx
cpx maxlength
bne skip
ldx #0
skip:
stx tabindex


Better understandable but of course longer ;)
2023-10-14 10:20
Oswald

Registered: Apr 2002
Posts: 5028
its a rule of thumb that when you get better in coding you write shorter more elegant and faster code, you need to move away from human thinking and think like the machine.
2023-10-14 11:28
chatGPZ

Registered: Dec 2001
Posts: 11147
Quote:
Obviously, the author assumes C=0 for sure when entering the code, so it is just not needed (the CLC).

Nothing in the source implies that :) It can only work correctly, if the main loop never sets C=1 (which is kinda unlikely :))
2023-10-16 10:33
Bitbreaker

Registered: Oct 2002
Posts: 501
i'd favour using sbx aswell when it is about incrementing x by any number and pplying an and operation.

Another option is to move the end condition of the loop into the table, and wrap the index depending on that. Color-register writes usually only need the lownibble.

        ...
        lda tab,x
        bpl +
        ldx #$ff
+
        inx
        ...

tab     !byte 9,8,5,13,1,13,5,$f8
2023-10-16 12:10
chatGPZ

Registered: Dec 2001
Posts: 11147
Or just count the index backwards :)
2023-10-16 13:06
Gordian

Registered: May 2022
Posts: 36
Another approach:

ldx index
...
lda #7
inx
sax index
 
... 27 posts hidden. Click here to view all posts....
 
Previous - 1 | 2 | 3 | 4 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
DjS/Silicon Ltd
t0m3000/HF^BOOM!^IBX
Mythus/Delysid
bepp/ΤRIΛD
Aomeba/Artline Desig..
Nicron
Sepa/OCD
Flavioweb/🇮🇹HF..
AlexC
psenough
Guests online: 123
Top Demos
1 Next Level  (9.8)
2 13:37  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Mojo  (9.6)
6 Comaland 100%  (9.6)
7 Uncensored  (9.6)
8 No Bounds  (9.6)
9 Bromance  (9.5)
10 Wonderland XII  (9.5)
Top onefile Demos
1 Layers  (9.6)
2 Cubic Dream  (9.6)
3 Party Elk 2  (9.6)
4 Copper Booze  (9.6)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 It's More Fun to Com..  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Booze Design  (9.3)
3 Censor Design  (9.3)
4 Crest  (9.3)
5 Performers  (9.3)
Top Swappers
1 Derbyshire Ram  (10)
2 Jerry  (9.8)
3 Violator  (9.8)
4 Acidchild  (9.7)
5 Starlight  (9.6)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.052 sec.