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

Forums > C64 Coding > Most efficient (in cycles) 25Hz music player
2020-07-27 13:27

Registered: Aug 2003
Posts: 5
Most efficient (in cycles) 25Hz music player

Last night, just for funsies and obviously inspired by the 25Hz music compo, I began wondering what is the fastest way to skip playing every 2nd frame. I came up with this.

First in the music init, I also init a toggle counter on zeropage address $02 to 00:
lda #$00
sta $02

Then, in the music play IRQ, I do this:
lda #$ff // 2 cycles - set accu to FF
eor $02 // 3 cycles - XOR with toggle: accu becomes 00 if toggle is FF, FF if 00
sta $02 // 3 cycles - update toggle counter to either FF or 00
bne end_irq // 2 cycles - skip playing if counter is not 00
jsr music.play

Of course it'd be much more efficient to use this or some other code to skip the music IRQ completely every 2nd frame, but you guys get the point. :)

This was a beginner's 10 cycle approach to the problem. Any faster solutions?
... 8 posts hidden. Click here to view all posts....
2020-07-28 12:23

Registered: Aug 2003
Posts: 5
Apologies, even though the context of my question was very clear in my head, I completely failed to communicate that in my initial post. :)

So, in my head, the scenario is to play a 25Hz Goattracker (or some other tracker) tune in e.g. a demo. I was thinking of using a raster interrupt mainly because I haven't done any CIA timer based players, in fact as a n00b it didn't even cross my mind. Given this scenario, and the fact that I was using a raster interrupt which usually triggers 50 times per second, I wanted to come up with some piece of code that skips the jump to the music playroutine (or bypasses the music player IRQ completely) every second execution, with the least overhead compared to the 50Hz "jsr $1003 every time" player.

So in my example, the code is used to skip every 2nd jump to the playroutine within the player IRQ. I guess it could also be used in a previous IRQ to choose which IRQ gets executed next (that being either player IRQ, or the IRQ that comes after the player IRQ), but I'm guessing the overhead of the deciding/branching code itself would still be the same.

Anyway, a CIA timer IRQ (or just reading the CIA register in a raster IRQ, as Krill probably suggested? Some of this stuff goes already above my noob head...) seems like the least cycle-consuming way to get something done 25 times per second.

Sorry for this messy ramble... I know it is quite a pointless coding exercise anyway, given the benefits of maybe saving a few cycles per frame, but I just like this kind of tinkering. :) I was also inspired by the previous discussion about shortest code for stable raster timer setup, albeit a) it was perhaps a more useful coding exercise, and b) I admit I didn't understand half of it.
2020-07-28 12:56

Registered: Apr 2002
Posts: 4596
its quite simple you can tell CIA to generate an interrupt at every xth cpu cycle, evey xth can be a 16 bit number, you can read in the manuals how exactly this have to be done.
2020-07-28 12:58

Registered: Dec 2001
Posts: 465
Or for you playing music each frame:

jsr $1003
lda $1003
eor #$2c
sta $1003

This assumes $1003 has a jmp. Basically toggle $1003 opcode between whatever it has a $60 to do a rts.
2020-07-28 13:56

Registered: Dec 2001
Posts: 9394
not that it saves anything over the solution provided in the first post :)
2020-07-28 14:47

Registered: Dec 2001
Posts: 465
If you want to save more cycles, 2 raster interrupts in the same page so you only need to do lda #<rasterirq2 sta $0314 or $fffe and lda #<raster1 sta $0314 or $fffe so you have 6 cycles changing at the expense of more memory use
2020-07-28 15:11

Registered: Dec 2001
Posts: 298
Yes, I had the feeling a RASTER IRQ was kinda set. But since the options for this had more or less been posted already, I just wanted to put up the timer-irq approach;)

At the end it's all for fun, and I'm happy that the 25Hz-Compo also sparked something for the coders (be it this discussion or some disco... aehh. funky releases :)=) )
2020-07-28 17:49

Registered: Apr 2002
Posts: 4596
Quote: If you want to save more cycles, 2 raster interrupts in the same page so you only need to do lda #<rasterirq2 sta $0314 or $fffe and lda #<raster1 sta $0314 or $fffe so you have 6 cycles changing at the expense of more memory use

you should have read all the posts, the ultimate solution was found already.
2020-07-28 23:47

Registered: Jul 2009
Posts: 471
I'm also probably missing the point, but if it's cycles / frame, why is:

    jsr $1000
    lda $1000  // 4 cycles
    eor #$2c   // 2 cycles
    sta $1000  // 4 cycles

using more cycles than

    IRQ        // 7 cycles
    jsr $1003
    rti        // 7 cycles

or was it ment something like this (6,5 cycles):

    bit $dc0x
    bpl !+
        jsr $1003

Considering the overhead on the IRQ method there is no memory saved.

If memory is no concern, you could easily make it make it 0 cycles (add more raster-IRQ chains). <- New ultimate solution?

What was the point here?
2020-07-29 15:06

Registered: Apr 2002
Posts: 4596
bit $dc0x
bpl !+
jsr $1003

you will also need a jmp !-
2020-07-29 15:57

Registered: Jul 2009
Posts: 471
Quote: bit $dc0x
bpl !+
jsr $1003

you will also need a jmp !-

We are probably thinking about this differently?

I was thinking to set the timer so it is alternating between negative and positive each frame (so it skipps the call to the music routine every 2nd frame). i didn't do the math though and sure as hell didn't test it so it's out of my ass only.


and IRQ chains = 0 cycles:

Play music
Do stuff
IRQ pointer => IRQ2

Do not play music
Do stuff
IRQ pointer => IRQ1
Previous - 1 | 2 - 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
Users Online
Radd Maxx/SWIM
Guests online: 31
Top Demos
1 Coma Light 13  (9.7)
2 Uncensored  (9.6)
3 Edge of Disgrace  (9.6)
4 Comaland 100%  (9.6)
5 Unboxed  (9.6)
6 The Shores of Reflec..  (9.6)
7 Lunatico  (9.5)
8 Remains  (9.5)
9 NGC 1277 100%  (9.5)
10 C=Bit 18  (9.5)
Top onefile Demos
1 Smile to the Sky  (9.5)
2 Cuarentenauta  (9.5)
3 Listen to Your Eyes  (9.5)
4 MD202006 - Get Well ..  (9.5)
5 Dawnfall V1.1  (9.5)
6 The Tuneful Eight [u..  (9.5)
7 Instinct  (9.5)
8 Rewind  (9.5)
9 Crystal Gazer  (9.5)
10 Bad Boy  (9.5)
Top Groups
1 PriorArt  (9.6)
2 Performers  (9.5)
3 Booze Design  (9.4)
4 Fossil  (9.4)
5 Censor Design  (9.4)
Top Webmasters
1 Perff  (9.6)
2 Slaygon  (9.6)
3 Morpheus  (9.5)
4 Sabbi  (9.2)
5 CreaMD  (9.2)

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