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 > Shortest code for stable raster timer setup
2020-01-20 16:20
Krill

Registered: Apr 2002
Posts: 3098
Shortest code for stable raster timer setup

While working on my ICC 2019 4K entry (now postponed to ICC 2020, but i hope it'll be worth the wait), i came up with this (14 bytes):
initstabilise   lda $d012
                ldx #10          ; 2
-               dex              ;   (10 * 5) + 4
                bpl -            ; 54
                nop              ; 2
                eor $d012 - $ff,x; 5 = 63
                bne initstabilise; 7 = 70

                [...]; timer setup
The idea is to loop until the same current raster line is read at the very beginning (first cycle) and at the very end (last cycle) of a raster line, implying 0 cycles jitter.

With 63 cycles per line on PAL, the delay between the reads must be 63 cycles (and not 62), reading $d012 at cycle 0 and cycle 63 of a video frame's last line (311), which is one cycle longer due to the vertical retrace.

The downside is that effectively only one line per video frame is attempted, so the loop may take a few frames to terminate, and the worst case is somewhere just beyond 1 second.

The upside is that it always comes out at the same X raster position AND raster line (0), plus it leaves with accu = 0 and X = $ff, which can be economically re-used for further init code.

Now, is there an even shorter approach, or at least a same-size solution without the possibly-long wait drawback?
 
... 180 posts hidden. Click here to view all posts....
 
2021-02-01 10:19
Frantic

Registered: Mar 2003
Posts: 1661
Yes, this thread should have some kind of warning sign attached to it. :)
2021-02-01 15:52
Copyfault

Registered: Dec 2001
Posts: 487
Quoting Devia
What an awful thread.. fell down the rabbit hole, turned utterly insane, lost several days of sleep...
How can it be so awful when it guided you into Wonderland :) ??

Quoting Devia
Quiss' original approach is short and elegant, but opcode rewriting confuses my old brain.
Adding a single byte to it's size, the flexibility can be increased somewhat (from 64 to 112 possible pages) and no opcode rewrites, just operand rewrite.
	* = $xy00, where x=$0-$f and y=$0-$6
loop:	ldy	#$08
	ldx	#$0a
	shx	loop,y
	jmp	loop
$xx0A:
Loop times kept at either 10 or 12 cycles, depending on address choice.
Wow, loop times depending on the adress-highbyte. But yeah, a JMP-instruction is needed to change the basic structure from opcode-change to operand-change... (unless one wants to exit from the loop by jumping back, but let's not go that route)

Quoting Devia
As an added bonus, A is not touched - which may or may not matter in your overall timer setup.

So, it's obvious my priority is not size, but readability - and I find this approach a tad more readable ;-)
Hm, ok, I agree that readability was not the main focus most of the times... but at least some variants that leave A untouched were given already (basically those with a CPX#val).

Still, I think the sniplet presented in post#90 (and all variants thereof) is best regarding trade-off between readability and size. And it imposes almost no restriction on the highbyte that can be used;)

Thanks for your version; it just rang the "things that I wanted to do"-bell for me (writing it all up on codebase, that is;)).
2021-02-01 18:10
Devia

Registered: Oct 2004
Posts: 403
Quoting Copyfault
Still, I think the sniplet presented in post#90 (and all variants thereof) is best regarding trade-off between readability and size. And it imposes almost no restriction on the highbyte that can be used;)

That's what I mean about the insanity..I do remember reading that post and the following ones, but apparently didn't recognize their brilliancy the first time around - that one is a keeper! ;-)
2021-02-01 19:06
Oswald

Registered: Apr 2002
Posts: 5127
is there any requierement for the nr of cycles for the whole loop to make sure it will 'always' land on a different cycle of the shx opcode on the badline's 12th cycle?
2021-02-01 19:44
Rastah Bar
Account closed

Registered: Oct 2012
Posts: 336
Yes, see posts 61 and 72 (and 69 for an explanation):

"In the range 5-20, the lengths that do work are 5, 10, 12, 16, 18 and 19. But note that in particular, length 8 (a.k.a. branching directly to the SHX) does not."

"Indeed, loop length of 17 gets "fixed" by the border. Depending on which cycle you land on initially, loop exit gets delayed by up to three frames, but it'll eventually align.
A similar thing happens with 27, which takes up to four frames to align.
Those seem to be the only "special" (multi-frame) cases below 30."
2021-02-01 20:47
Oswald

Registered: Apr 2002
Posts: 5127
thanks for everyone for the explanations, this shx method is really cool :)
2023-05-11 19:00
Quiss

Registered: Nov 2016
Posts: 52
Sometimes, you want to set up a timer that loops once per frame, not per line, and on a stable cycle.
The shx code snippets posted so far give us a constant x position, but on a random y, so won't work for that.

But you can do:

        ldy #$01
loop:
        ldx #243  (or any other badline)
        cpx $d012
        bne *-3
        shy $ffff, x
        lda $ff, x
        beq loop

This always drops us out on line 243 cycle 62, and takes up to seven frames to do so.
2025-06-19 01:27
St0fF

Registered: Oct 2002
Posts: 42
Just finding my way back into C64coding ... I found the readability of this Kick Assembler variation of Copyfaults "after Basic"-Variation great:

.segment Copyfaults08a3 [start=$08a3]
		!:	ldx jump_in: #SHX_ABSY ; .by LDY_IMM, $08
			cpx #$00
			bne !-
// ... further init code, like starting a timer ...

... so you can have the Kickassembler check for you, if it fits in our demo when outputting segments ...

And to sum up the great methods from the middle of this thread at the end:
.macro Copyfaults_QuissSHXmethod_unrestricted( initval, zp_pos ) {
			ldx #initval
      			ldy #<(zp_pos+1)
		!:	shx $ffff,y
      			lda <($100 + zp_pos - initval),x
      			beq !-
	}

Although I have to point out, that I could not get this macro to work in RetroDebugger, nor in Vice. I.e. the loop never finishes.

Oh, and kudos! I can only take my hat off on all those findings... Great job, Krill, Quiss, Copyfault, Rastah Bar and CJam ... thank you so much for these valuable code snippets and explanations!
2025-06-19 04:15
St0fF

Registered: Oct 2002
Posts: 42
Quote: Sometimes, you want to set up a timer that loops once per frame, not per line, and on a stable cycle.
The shx code snippets posted so far give us a constant x position, but on a random y, so won't work for that.

But you can do:

        ldy #$01
loop:
        ldx #243  (or any other badline)
        cpx $d012
        bne *-3
        shy $ffff, x
        lda $ff, x
        beq loop

This always drops us out on line 243 cycle 62, and takes up to seven frames to do so.


Small checking program shows (at least in x64sc): does work on all badlines except the first.
BasicUpstart2( program )
program:		sei
		!:	lda runner: #24
			asl ; asl ; asl ; adc #$33
			sta badline
			ldy #1
		!:	ldx badline: #$3b  	// (or any other badline?)
		!:	cpx $d012
			bne !-
			shy $ffff, x
			lda $ff, x
			beq !--
			inc $d020
			dec $d020
			lda $dc01
			cmp #$ef
			bne !--
		!:	cmp $dc01
			beq !-
			dec runner
			bne !----		// change to "bpl": program gets stuck
			lda #24
			sta runner
			bne !----


P.S. if it isn't obvious: you need VIC debug view to see the $D020 effect. Pressing space moves to the previous badline (program starts at last badline). You should see a flickering in the border - every time it flickers, the loop had a hit. Committing the proposed change will show that the loop never hits at Line $33 i.e. the very first badline.
2025-06-19 07:39
Krill

Registered: Apr 2002
Posts: 3098
Quoting St0fF
.segment Copyfaults08a3 [start=$08a3]
		!:	ldx jump_in: #SHX_ABSY ; .by LDY_IMM, $08
			cpx #$00
			bne !-
FWIW, i keep using this snippet in 64tass:
                .cerror * != $08a3, "sync not at $08a3"
sync = * + 1
                ldx #$9e
                ldy #8
                cpx #0
                bne sync
Previous - 1 | ... | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 - 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
Sokrates
Acidchild/Padua
Krill/Plush
Guests online: 459
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Codeboys & Endians  (9.7)
4 Mojo  (9.6)
5 Coma Light 13  (9.6)
6 Edge of Disgrace  (9.6)
7 Signal Carnival  (9.6)
8 Wonderland XIV  (9.5)
9 Uncensored  (9.5)
10 Comaland 100%  (9.5)
Top onefile Demos
1 Nine  (9.7)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.5)
6 Scan and Spin  (9.5)
7 Onscreen 5k  (9.5)
8 Grey  (9.5)
9 Dawnfall V1.1  (9.5)
10 Rainbow Connection  (9.5)
Top Groups
1 Artline Designs  (9.3)
2 Booze Design  (9.3)
3 Performers  (9.3)
4 Oxyron  (9.3)
5 Censor Design  (9.3)
Top Organizers
1 Burglar  (9.9)
2 Sixx  (9.8)
3 Tim  (9.7)
4 Irata  (9.7)
5 hedning  (9.7)

Home - Disclaimer
Copyright © No Name 2001-2025
Page generated in: 0.06 sec.