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: 2839
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?
 
... 177 posts hidden. Click here to view all posts....
 
2020-12-20 13:26
Rastah Bar

Registered: Oct 2012
Posts: 336
Yes, but the coder can choose to use A&X=1, or A&X=3, or something else. So in that sense there are less restrictions.
A priori he has more possibilities to choose from.

Some may not work, but he can always fall back to A&X = 1 or A&X = 3 if he can't make a data table work with the corresponding restrictions on A and X, and to A&X = 1 if A&X = 3 can't be met. Besides, A&X = 2^K, K = 2,...,7 also only fixes 1 bit.
2020-12-20 17:54
Copyfault

Registered: Dec 2001
Posts: 466
Ah, ok, looking from that meta-level I agree of course. The only case that causes trouble is A&X=2, everthing else will work.
2021-01-30 18:15
Krill

Registered: Apr 2002
Posts: 2839
Quoting Copyfault
Quoting Quiss
Neat! Right, no reason to make those two address bytes go to waste. :)

Another amusing thing to contemplate is how this code could be placed at, say, $08xx. Preferably without messing up the basic upstart.
[...]
Not necessarily the shortest piece of code, but it satisfies your requirement to have the sync routine at $08xx:
0843 A2 9E   LDX #$9E
0845 A0 08   LDY #$08
0847 E0 00   CPX #$00
0849 D0 F9   BNE $0844
Branching to $0844 leads to SHX $08A0,Y, so the operand byte of the CPX is altered continuously. As long as the "&(hi+1)" plays in, the compare operand will be =$08. When "&(hi+1)" disappears, the full $9E is written to the operand byte and the loop will end. This happens iff the critical SHX-cycle happens on a badline.
Nice trolljob there! =)

Took me a while and Rastah Bar's successive comment to figure out that code must sit at $08A3 instead, and not at $0843.
Seems to work nicely now in real-world code on realthing and x64sc, while on x64 the code just twiddles thumbs in an endless loop. =)
2021-01-30 20:43
Copyfault

Registered: Dec 2001
Posts: 466
Quote: Quoting Copyfault
Quoting Quiss
Neat! Right, no reason to make those two address bytes go to waste. :)

Another amusing thing to contemplate is how this code could be placed at, say, $08xx. Preferably without messing up the basic upstart.
[...]
Not necessarily the shortest piece of code, but it satisfies your requirement to have the sync routine at $08xx:
0843 A2 9E   LDX #$9E
0845 A0 08   LDY #$08
0847 E0 00   CPX #$00
0849 D0 F9   BNE $0844
Branching to $0844 leads to SHX $08A0,Y, so the operand byte of the CPX is altered continuously. As long as the "&(hi+1)" plays in, the compare operand will be =$08. When "&(hi+1)" disappears, the full $9E is written to the operand byte and the loop will end. This happens iff the critical SHX-cycle happens on a badline.
Nice trolljob there! =)

Took me a while and Rastah Bar's successive comment to figure out that code must sit at $08A3 instead, and not at $0843.
Seems to work nicely now in real-world code on realthing and x64sc, while on x64 the code just twiddles thumbs in an endless loop. =)


Oops, thought I corrected this in a later post, but it seems I forgot to.

However, other approaches have been discovered during the discussion that are comparable in size but more flexible regarding mem location.


So take my deep apologies... "trolljob" sounds really evil :(:(:(... and I really really did not intend to fool anyone.

Hopefully it won't happen again.
2021-01-30 21:27
Krill

Registered: Apr 2002
Posts: 2839
No worries, i actually took it for a mistake.

Though i wonder how that could happen, did you drunkenly type in some notes scribbled on a napkin, mistaking an A for a 4? =)
2021-01-31 00:42
Raistlin

Registered: Mar 2007
Posts: 557
So you're suggesting there was a fault in Copyfault's copy?
2021-01-31 00:51
Copyfault

Registered: Dec 2001
Posts: 466
Quoting Raistlin
So you're suggesting there was a fault in Copyfault's copy?
Yeah, must be the handle, obviously ;)
2021-01-31 10:24
Oswald

Registered: Apr 2002
Posts: 5017
0843 A2 9E LDX #$9E
0845 A0 08 LDY #$08
0847 E0 00 CPX #$00
0849 D0 F9 BNE $0844

could someone explain how this works ? :D
2021-01-31 11:34
Krill

Registered: Apr 2002
Posts: 2839
Quoting Oswald
0843 A2 9E LDX #$9E
0845 A0 08 LDY #$08
0847 E0 00 CPX #$00
0849 D0 F9 BNE $0844

could someone explain how this works ? :D
Corrected version of the code and Copyfault's explanation annotated:
08A3 A2 9E   LDX #$9E
08A5 A0 08   LDY #$08
08A7 E0 00   CPX #$00
08A9 D0 F9   BNE $08A4
Branching to $08A4 leads to SHX $08A0,Y with Y = 8, so the operand byte of the CPX #imm at $08A8 is altered continuously.
As long as the "&(hi+1)" plays in, the CPX #imm operand will be $9E & $09 = $08, which is not equal X = $9E, so branching back.
When "&(hi+1)" disappears, the full $9E is written to the CPX's operand byte and the loop will end (X = $9E with CPX #$9E will yield Z=1). This happens if and only if the critical SHX-cycle appears on a badline.
2021-01-31 12:35
Oswald

Registered: Apr 2002
Posts: 5017
why does the &hi disappear on a badline?
Previous - 1 | ... | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 - 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
Peacemaker/CENSOR/Hi..
MaD ][/Starship
Guests online: 139
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Bromance  (9.6)
10 Memento Mori  (9.6)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Onscreen 5k  (9.5)
8 Wafer Demo  (9.5)
9 Dawnfall V1.1  (9.5)
10 Quadrants  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Nostalgia  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Crackers
1 Mr. Z  (9.9)
2 S!R  (9.9)
3 Antitrack  (9.8)
4 Mr Zero Page  (9.8)
5 OTD  (9.8)

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