| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
ROL'in a byte onto itself
What is the fastest and/or shortest code for rolling a pattern through a byte? I use it to trigger an action following a certain pattern. I came up with (18 cycles worst case / 12 bytes):
clc
lda pattern
bpl no_carry
sec
no_carry rol pattern
bcs take_action
if your pattern ends on a 1 (bit 0 = 1) then you could also do (17 cycles worst case / 13 bytes)
lda pattern
bne @cont
lda #%00110101 ; restore pattern
@cont asl
sta pattern
bcs take_action
any other variations? |
|
... 22 posts hidden. Click here to view all posts.... |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
Quote: I do like that (with a change of how the state is encoded) Groepaz' solution gives you arbitrary length sequences (as long as there are no more than 128 ones and no more than 128 zeros).
The table is contiguous, tooa fact that eluded me before I realised you could use a run of numbers ending with 127 for the zero states, and from 128 up for the ones (eg. use 128,125,126,129,127 to represent the sequence 10010)
dont think groepaz has put so much thought into it :) |
| |
ChristopherJam
Registered: Aug 2004 Posts: 1409 |
Quoting Oswalddont think groepaz has put so much thought into it :)
I probably should have said groepaz' code, (with mhindsbo's BMI appended), rather than his solution ;-) |
| |
mhindsbo
Registered: Dec 2014 Posts: 51 |
i like Hoogo's for both its speed and size and Groepaz if you have the mem and need the long patterns. this was good input. thanks all. |
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
Quote: asl pattern
bcc no_action
inc pattern
This is excellent. I never thought of it.
1) Smaller
2) Faster
3) Doesn't clobber any registers except Status.
=> Perfect optimization. 10/10 rating. |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
yup. +1 |
| |
Frantic
Registered: Mar 2003 Posts: 1648 |
Yes, very nice.
@Hoogo: I'd like to add this to codebase, but before doing so I just want to ask if you would rather prefer to do it yourself?
(A possible modification of this might be to use 0 as "take action flag" instead of 1, so that the inc is only performed when no particular action is to be taken, which might smoothen the averate cpu cycle consumtion each time the code is executed including the execution of the code for the "action".) |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
btw never thought of this, but RMW instructions work so, that the 6510 reads the byte into an 'inner' alu, so it can leave A alone ? |
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
Quote: btw never thought of this, but RMW instructions work so, that the 6510 reads the byte into an 'inner' alu, so it can leave A alone ?
Not quite. There is not a single ALU, there are increment unit, shift units etc. Each unit can be used by different sources. Check http://vignette1.wikia.nocookie.net/microchip/images/c/c9/MOS-6.. for more in-depth. |
| |
Bitbreaker
Registered: Oct 2002 Posts: 508 |
For a more readable version try this :-)
http://vignette1.wikia.nocookie.net/microchip/images/c/c9/MOS-6..
or:
http://www.weihenstephan.org/~michaste/pagetable/6502/6502.jpg |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
Quote: Not quite. There is not a single ALU, there are increment unit, shift units etc. Each unit can be used by different sources. Check http://vignette1.wikia.nocookie.net/microchip/images/c/c9/MOS-6.. for more in-depth.
;) |
Previous - 1 | 2 | 3 | 4 - Next |