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: 2980
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-01-29 21:39
Oswald

Registered: Apr 2002
Posts: 5094
I dont get it, Z flag is set according to result of inc, thus line should be $ff ?
2020-01-29 21:52
ChristopherJam

Registered: Aug 2004
Posts: 1409
Sure - depends how you measure it.
The last time the 54 cycles worth of code between the snippets are executed, they will be on line $0ff.

The code always exits a few cycles into line $100.
2020-01-30 00:22
Copyfault

Registered: Dec 2001
Posts: 478
Quote: Cheers, Copyfault.

I was wondering about the pagebreak myself, but didn't want to constrain the code (of course, as writ it needs to avoid a page break, but that's slightly easier).

I agree that the ldy#val you've proposed belongs to the init code, and hence is a valid way to avoid the RMW without increasing the code size.

All that said, my submission and your modifications to it are all vulnerable to failing if they enter in the middle of line zero - only a 0.3% chance, but still not ideal. The following take avoids that, albeit at the cost of re-introducing the write to $d012. Possibly a bonus if you want to set up a raster interrupt for line $000 or $100 mind :)
sync:
    inc $d012   ; result will be zero on cycles 0-7 or 8 of raster line $ff
    bne sync    ; (or very rarely, cycle 9..62)

    .res 27,$ea ; wait 62-8=54 cycles. Replace with 54 cycles of init code

    inc $d012   ; if result is nonzero then we are too late.
    bne sync    ; carry on if we read on cycle 62, 62 cycles after cycle 0


edit: this one probably also compresses better than the earlier versions - the two code snippets both start with EE 12 D0 D0


Ooh yes, I see... the uber-motivation was to huge yesterday;)

But... this should be fixable without INC-opcodes. The chance of failing comes from the fact that we used a BEQ-check after the stuffed-in init-code with the strategy of a unique overflow situation in mind.

Now if we reverse this strategy, we could check for a unique non-overflow-situation, by just prolonging the init code part by a suitable no. of cycles.
sync:
    ldy #val
line0x100_wait:
    lax $d012
    bne line0x100_wait
    /*
    // 56 cycles of init code go here
    */
    lax $d012   // the R-cycle occurs exactly after 62 cycles of the upper R-cycle of the lax $d012
    bne sync    // this gives 0 if and only if the upper $d012-read was exactly @cycle=0 of the rasterline
                // as rasterline 0 is only 62 cycles long, this will only be
                // the case if the upper $d012-read was @cyc=0 of line=$100
The [ldy #val] is still needed for ensuring coprimeness (i.e. no. of cycles between two upper $d012-checks must not have a common divisor with 63=7*9).

Should work and should need 63 frames in the worst case. Ahh, and the lower [lax $d012] was just for having two identical codeblocks, thus should also do them compressing algorithms a favor;)
2020-01-30 07:38
Oswald

Registered: Apr 2002
Posts: 5094
in max how many frames would this awesome inc solution would sync up ?

I'd be interested in a solution thats simple AND fast. Not necessarily shortest. Would be a nice addendum to codebase.

What I have currently by Ninja does have a lot of code checking on how it misses the end of a rasterline, a version that fits into a dozen lines would be neater.
2020-01-30 09:08
ChristopherJam

Registered: Aug 2004
Posts: 1409
The INC solution takes at most nine frames, and should average 4.5

Copyfault, there's really no harm in writing to d012, unless you wish to set it to some other value than zero in the init code between the two bookends.
2020-01-30 09:12
Oswald

Registered: Apr 2002
Posts: 5094
wow, then the inc solution is good for everything :)
2020-01-30 20:05
Copyfault

Registered: Dec 2001
Posts: 478
Quote: The INC solution takes at most nine frames, and should average 4.5

Copyfault, there's really no harm in writing to d012, unless you wish to set it to some other value than zero in the init code between the two bookends.


I somehow tend to avoid writing to a reg if there's no real purpose behind.

But back to your INC-based solution: why does it take 9 frames at most? If the upper INC happens to come at some cycle >=9 of Rasterline $ff, it should take longer, more or less comparable to the alternative I presented - or do I miss smth here? AFAIU, both approaches do the same, just the rasterline where the syncing finishes is different (yours at line $100, mine at $101).

Ofcourse, it might also be a wanted side-effect to set $D012=0 if the first Raster-IRQ at line 0 (or $100 resp.) makes sense.
2020-01-31 00:24
Copyfault

Registered: Dec 2001
Posts: 478
Quote: in max how many frames would this awesome inc solution would sync up ?

I'd be interested in a solution thats simple AND fast. Not necessarily shortest. Would be a nice addendum to codebase.

What I have currently by Ninja does have a lot of code checking on how it misses the end of a rasterline, a version that fits into a dozen lines would be neater.


So you basically look for a solution that has the least raster-time demand for syncing, or am I on the wrong path?

Something like this should finish in at most eight rasterlines:
        lda #$08
        sta zp_val
        
        ldx #$fe
loop:   
wait_startline:
        cpx $d012
        bne wait_startline
        inx
        bmi wait_startline
        //at cycle 6..12 of line $ff
        ldy zp_val
waste_cycles:
        dey
        bpl waste_cycles
        
        cpx $d012
        bne loop //leaves at cycle 2 of the first line in which raster is stable ($100..$106)
done:   
By debouncing the starting line, we can asure that the no. of cycles at the start of the actual syncing loop lies exactly in the interval [6..12] (and is never different). So the syncing can be done by variance cancelation, which needs one rasterline per correction cycle. As there are seven different possibilites for the variance (6,7,8,9,10,11,12), (up to) seven rasterlines are needed in total (plus the first one for ensuring a "save start").

Maybe this can be done with shorter code, but I think not really faster (unless you really want to do variance halfing which will blow up code size too much for my taste).
2020-01-31 00:38
Oswald

Registered: Apr 2002
Posts: 5094
sorry I did not construct it properly with fast I meant it stabilizes fast, with that I mean max ~0.3 seconds a time span that for us humans doesnt matter :) so 9 frames max will do. however looking at the new version and explanation: your skills at this are truly impressive sir.
2020-01-31 11:03
Rastah Bar
Account closed

Registered: Oct 2012
Posts: 336
What Krill said.

Here is another method (13 bytes, stabilizes in less than a frame). When entering from Basic, timer A of CIA#1 is running. That can be used to check if the last cycle of an RMW instruction falls on the first "BA low, AEC high" cycle of a badline, as follows:
sync: lda $dc04
      sec
      sta ZP    ;RMW instruction
      sbc $dc04
      cmp #51   
      bne sync:

If and only if the last cycle of STA ZP is executed on the first "BA low, AEC high" cycle of a badline will there be exactly 51 cycles between LDA $DC04 and SBC $DC04 and the routine will exit on the last cycle of a badline.
Previous - 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... | 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
Nith/TRIÉ…D
Flashback
CreaMD/React
pcollins/Quantum
LightSide
Shake/Role
Guests online: 87
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 Layers  (9.6)
2 No Listen  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Rainbow Connection  (9.5)
7 Dawnfall V1.1  (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 Webmasters
1 Slaygon  (9.6)
2 Perff  (9.6)
3 Sabbi  (9.5)
4 Morpheus  (9.4)
5 CreaMD  (9.1)

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