Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > ROL'in a byte onto itself
2016-03-01 17:04
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?
2016-03-01 17:10
chatGPZ

Registered: Dec 2001
Posts: 11386
ldx pattern
lda table,x
sta pattern
2016-03-01 17:26
mhindsbo

Registered: Dec 2014
Posts: 51
Like it. 15 cycles (once you add the branch to take action) and 19 bytes (incl. branch and 8 byte table) if I am correct. If you have different patterns then full page table.
2016-03-01 17:27
ChristopherJam

Registered: Aug 2004
Posts: 1409
Should be able to replace the first version with this?
    lda pattern
    asl a
    rol pattern
    bcs take_action
2016-03-01 17:39
mhindsbo

Registered: Dec 2014
Posts: 51
indeed ... now why did I miss that ;-)
2016-03-01 18:47
Oswald

Registered: Apr 2002
Posts: 5094
my 2 cents:

cmp #$80 will put highest bit into carry aswell. useful if you dont want to destroy a.
2016-03-01 18:56
JackAsser

Registered: Jun 2002
Posts: 2014
Quote: my 2 cents:

cmp #$80 will put highest bit into carry aswell. useful if you dont want to destroy a.


Was just about to write it!!
2016-03-01 19:21
Hoogo

Registered: Jun 2002
Posts: 105
asl pattern
bcc no_action
inc pattern
2016-03-01 19:47
Peiselulli

Registered: Oct 2006
Posts: 81
asl
adc #$00
2016-03-01 23:16
ChristopherJam

Registered: Aug 2004
Posts: 1409
Hoogo wins \O/
2016-03-02 01:49
ChristopherJam

Registered: Aug 2004
Posts: 1409
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, too—a 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)
2016-03-02 07:00
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, too—a 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 :)
2016-03-02 08:02
ChristopherJam

Registered: Aug 2004
Posts: 1409
Quoting Oswald
dont 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 ;-)
2016-03-02 12:16
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.
2016-03-02 14:58
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.
2016-03-03 06:13
Oswald

Registered: Apr 2002
Posts: 5094
yup. +1
2016-03-03 09:49
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".)
2016-03-03 13:14
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 ?
2016-03-03 13:25
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.
2016-03-03 14:01
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
2016-03-03 14:49
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.



;)
2016-03-03 19:32
Hoogo

Registered: Jun 2002
Posts: 105
Quoting Frantic
@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?
Don't bother, just add it. I'm a little surprised by all this reaction...
2016-03-03 20:04
Frantic

Registered: Mar 2003
Posts: 1648
Quote: Quoting Frantic
@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?
Don't bother, just add it. I'm a little surprised by all this reaction...


I guess nerdy coders like us find the simplicity of such small and simple tricks beautiful.

https://codebase64.org/doku.php?id=base:rotate_byte_and_act_on_..

(Couldn't come up with a sensible name for the article really, but hopefully it is comprehensible.)
2016-03-03 22:12
Digger

Registered: Mar 2005
Posts: 437
What's could be a practical application for that?
2016-03-04 02:29
mhindsbo

Registered: Dec 2014
Posts: 51
I use it for an alternating fire pattern. This piece of code sits in the fire function and decides if it fires or not when called
2016-03-06 00:36
Copyfault

Registered: Dec 2001
Posts: 478
Quoting Hoogo
asl pattern
bcc no_action
inc pattern


Wow, that's true art!

Thanks for being (or better getting) active again, Frank ;)
2016-03-06 09:51
Style

Registered: Jun 2004
Posts: 498
Quote: Quoting Hoogo
asl pattern
bcc no_action
inc pattern


Wow, that's true art!

Thanks for being (or better getting) active again, Frank ;)


Id use

ASL mem
ADC #0
2016-03-07 19:17
Hoogo

Registered: Jun 2002
Posts: 105
Quoting Style
ASL mem
ADC #0
It's easier, faster and shorter, but does not work :) Where's the Accu coming from, and where does it go?
2016-03-07 20:04
chatGPZ

Registered: Dec 2001
Posts: 11386
Quote:
It's easier, faster and shorter, but does not work

AEG would love it *g*
2016-03-08 23:29
Style

Registered: Jun 2004
Posts: 498
Quote: Quoting Style
ASL mem
ADC #0
It's easier, faster and shorter, but does not work :) Where's the Accu coming from, and where does it go?


Oh was that a prerequisite? :)


LDA groep
ASL
ADC #0
STA groep


There ;)

(wanders off to find copy of "coding drunk for beginners")
2016-03-09 13:41
mhindsbo

Registered: Dec 2014
Posts: 51
Yes the requirement was code that works (checking chapter 1 in "coding drunk for beginners" ;-) just need to add the branch to take action ;-)

All in all Hoogo's solution is both shortest and fastest. Part of the joy I get from coding on the C64 are finding elegant short, fast and clean solutions, within the constraints of the system, and I really like the aesthetics of this one :-)
2016-03-09 19:08
Perplex

Registered: Feb 2009
Posts: 255
Edit: nevermind.
2016-03-09 19:30
Oswald

Registered: Apr 2002
Posts: 5094
yeah hype it some more, most beautiful 3 lines of code I've ever seen. I mean, really! :)
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
csabanw
tlr
MWR/Visdom
JackAsser/Booze Design
Flex/Artline Designs
Guests online: 88
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Graphicians
1 Mirage  (9.8)
2 Archmage  (9.7)
3 Pal  (9.6)
4 Carrion  (9.6)
5 Sulevi  (9.6)

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