| |
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 |
|
| |
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. |
| |
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? |
| |
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). |
| |
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. |
| |
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 ;) |
| |
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. |
| |
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 :)) |
| |
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
|
| |
chatGPZ
Registered: Dec 2001 Posts: 11147 |
Or just count the index backwards :) |
| |
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 |